Observe native Claude automations and preserve pending timers
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/claudehost"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// Automations reports only the last native Stop-hook snapshot. It never starts
|
||||
// or resumes Claude, sends a prompt, or attaches a delivery reader.
|
||||
func (c *Chat) Automations(ctx context.Context, sessionID string) (protocol.ChatAutomationInfo, error) {
|
||||
info := protocol.ChatAutomationInfo{SessionID: sessionID, Provider: Backend, Status: protocol.ChatAutomationUnavailable,
|
||||
Reason: "Claude Code has not reported a native Stop snapshot for this session"}
|
||||
if sessionID == "" {
|
||||
return info, nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
var snapshot *claudehost.AutomationSnapshot
|
||||
var socket string
|
||||
direct := false
|
||||
s := c.session(sessionID)
|
||||
if s != nil {
|
||||
s.mu.Lock()
|
||||
p := s.proc
|
||||
cached := s.automations
|
||||
cachedProcess := s.automationProcess
|
||||
s.mu.Unlock()
|
||||
if p == nil || cachedProcess != p || processExited(p) {
|
||||
cached = nil
|
||||
}
|
||||
if p != nil && p.link != nil {
|
||||
socket = p.link.record.Socket
|
||||
} else if p != nil {
|
||||
direct = true
|
||||
snapshot = cached
|
||||
}
|
||||
}
|
||||
if socket == "" && !direct && c.hostDir != "" {
|
||||
path := claudehost.PathsFor(c.hostDir, sessionID).Record
|
||||
if data, err := os.ReadFile(path); err == nil {
|
||||
var record claudehost.Record
|
||||
if json.Unmarshal(data, &record) == nil && record.SessionID == sessionID && record.Socket != "" {
|
||||
socket = record.Socket
|
||||
}
|
||||
}
|
||||
}
|
||||
if socket != "" {
|
||||
statusCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
status, err := claudehost.ReadStatus(statusCtx, socket)
|
||||
cancel()
|
||||
if err == nil && status != nil && !status.Exited && status.SessionID == sessionID {
|
||||
snapshot = status.Automations
|
||||
}
|
||||
}
|
||||
if snapshot == nil || snapshot.SessionID != sessionID || snapshot.ObservedAtMS <= 0 {
|
||||
return info, nil
|
||||
}
|
||||
count := len(snapshot.IDs)
|
||||
info.Status, info.ActiveCount, info.ObservedAtMS = protocol.ChatAutomationSupported, &count, snapshot.ObservedAtMS
|
||||
info.Reason = "last native Claude Code Stop snapshot; not a live query"
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// Native future wakeups are live work even while no model turn is running.
|
||||
// A replacement process cannot inherit its predecessor's in-memory timers.
|
||||
// The caller holds s.mu.
|
||||
func (s *session) hasAutomationsLocked() bool {
|
||||
return s.proc != nil && s.automationProcess == s.proc &&
|
||||
s.automations != nil && len(s.automations.IDs) > 0
|
||||
}
|
||||
|
||||
func processExited(p *process) bool {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
return p.exited
|
||||
}
|
||||
Reference in New Issue
Block a user