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
+52
View File
@@ -24,6 +24,12 @@ import (
"strings"
)
// NativeAutomationCallbackID is private to this adapter. Other hook callbacks
// (and operator permission requests) must never be acknowledged as snapshots.
const NativeAutomationCallbackID = "pipelines-native-session-automations-v1"
const MaxAutomationEntries = 128
// Version is the host protocol version reported in every status.
const Version = 1
@@ -114,6 +120,52 @@ type Status struct {
Stderr string `json:"stderr,omitempty"`
// Directory is the CLI's working directory.
Directory string `json:"directory,omitempty"`
// Automations is the last validated native Stop-hook snapshot. A nil value
// means unknown (including hosts that predate this capability).
Automations *AutomationSnapshot `json:"automations,omitempty"`
}
// AutomationSnapshot contains only bounded native schedule identities and a
// count. This snapshot omits prompts; native CLI events follow the existing
// bounded private spool policy.
type AutomationSnapshot struct {
SessionID string `json:"session_id"`
IDs []string `json:"ids"`
ObservedAtMS int64 `json:"observed_at_ms"`
}
// IsAutomationHook recognizes the private callback without making any claim
// about its optional metadata. Recognized callbacks must always be answered.
func IsAutomationHook(callbackID string) bool { return callbackID == NativeAutomationCallbackID }
// ParseAutomationHook accepts only the SDK's main-thread Stop input. It returns
// false for absent or malformed metadata; callers should still acknowledge a
// callback recognized by IsAutomationHook, while treating the snapshot as
// unknown.
func ParseAutomationHook(callbackID, expectedSession string, input json.RawMessage, nowMS int64) (*AutomationSnapshot, bool) {
if callbackID != NativeAutomationCallbackID || expectedSession == "" || len(input) == 0 || len(input) > 1<<20 {
return nil, false
}
var hook struct {
SessionID string `json:"session_id"`
HookEventName string `json:"hook_event_name"`
SessionCrons *[]struct {
ID string `json:"id"`
} `json:"session_crons"`
}
if json.Unmarshal(input, &hook) != nil || hook.SessionID != expectedSession || hook.HookEventName != "Stop" || hook.SessionCrons == nil || len(*hook.SessionCrons) > MaxAutomationEntries {
return nil, false
}
ids := make([]string, 0, len(*hook.SessionCrons))
seen := map[string]bool{}
for _, cron := range *hook.SessionCrons {
if cron.ID == "" || len(cron.ID) > 256 || seen[cron.ID] {
return nil, false
}
seen[cron.ID] = true
ids = append(ids, cron.ID)
}
return &AutomationSnapshot{SessionID: expectedSession, IDs: ids, ObservedAtMS: nowMS}, true
}
// Record is the registry file a host writes next to its socket so a