Observe native Claude automations and preserve pending timers

This commit is contained in:
2026-11-08 15:52:22 +00:00
parent 9d3358be47
commit 3fbfb85ace
17 changed files with 757 additions and 34 deletions
+25 -5
View File
@@ -94,6 +94,7 @@ type host struct {
modeReqs map[string]string
model string
mode string
automations *AutomationSnapshot
exited bool
exitErr string
exitedAt time.Time
@@ -297,9 +298,11 @@ type snoop struct {
RequestID string `json:"request_id"`
ParentToolUseID string `json:"parent_tool_use_id"`
Request struct {
Subtype string `json:"subtype"`
Model string `json:"model"`
Mode string `json:"mode"`
Subtype string `json:"subtype"`
Model string `json:"model"`
Mode string `json:"mode"`
CallbackID string `json:"callback_id"`
Input json.RawMessage `json:"input"`
} `json:"request"`
Response struct {
Subtype string `json:"subtype"`
@@ -331,7 +334,7 @@ func (h *host) appendLine(line []byte) {
var env snoop
_ = json.Unmarshal(line, &env)
h.mu.Lock()
defer h.mu.Unlock()
ackObserver := false
start := h.end
data := append(append([]byte(nil), line...), '\n')
h.fileMu.RLock()
@@ -349,6 +352,14 @@ func (h *host) appendLine(line []byte) {
if env.RequestID != "" {
h.pending[env.RequestID] = start
}
if env.Request.Subtype == "hook_callback" {
if IsAutomationHook(env.Request.CallbackID) {
snapshot, _ := ParseAutomationHook(env.Request.CallbackID, h.id, env.Request.Input, time.Now().UnixMilli())
h.automations = snapshot
delete(h.pending, env.RequestID)
ackObserver = true
}
}
case "control_cancel_request":
delete(h.pending, env.RequestID)
case "control_response":
@@ -377,6 +388,13 @@ func (h *host) appendLine(line []byte) {
}
h.compactLocked()
h.broadcastLocked()
h.mu.Unlock()
if ackObserver {
response, _ := json.Marshal(map[string]any{"type": "control_response", "response": map[string]any{
"subtype": "success", "request_id": env.RequestID, "response": map[string]any{},
}})
h.writeIn(response)
}
}
// compactLocked bounds the spool. Acknowledged output beyond the limit is
@@ -450,6 +468,7 @@ func (h *host) statusLocked() *Status {
Base: h.base, End: h.end, Acked: h.acked, Busy: h.busy, BusyAtAcked: h.busyAtLocked(h.acked),
Init: h.init, Model: h.model, Mode: h.mode, Pending: pending, Tasks: &tasks,
Exited: h.exited, ExitErr: h.exitErr, Stderr: h.stderr.String(), Directory: cwd,
Automations: h.automations,
}
if h.cmd != nil && h.cmd.Process != nil {
status.ClaudePID = h.cmd.Process.Pid
@@ -708,7 +727,8 @@ func (h *host) watchIdle() {
now := time.Now()
h.mu.Lock()
attached := h.active != nil
idle := !h.busy && len(h.pending) == 0 && len(h.tasks) == 0 && now.Sub(h.detached) > h.idle && now.Sub(h.activity) > h.idle
automationsPending := h.automations != nil && len(h.automations.IDs) > 0
idle := !h.busy && len(h.pending) == 0 && len(h.tasks) == 0 && !automationsPending && now.Sub(h.detached) > h.idle && now.Sub(h.activity) > h.idle
tooLong := h.maxDetached > 0 && now.Sub(h.detached) > h.maxDetached
exitedLong := h.exited && now.Sub(h.exitedAt) > h.idle
h.mu.Unlock()