Observe native Claude automations and preserve pending timers
This commit is contained in:
+7
-3
@@ -545,14 +545,18 @@ sessions pipeline runs create. The protocol types are:
|
|||||||
rather than a stale count.
|
rather than a stale count.
|
||||||
- `chat_automations` reads provider-owned scheduled work for one operator chat.
|
- `chat_automations` reads provider-owned scheduled work for one operator chat.
|
||||||
Its body is `chat_query: {session_id}` and the reply is
|
Its body is `chat_query: {session_id}` and the reply is
|
||||||
`chat_automations: {session_id, provider, status, active_count?, reason?}`.
|
`chat_automations: {session_id, provider, status, active_count?, observed_at_ms?, reason?}`.
|
||||||
`status` is `supported` or `unavailable`; a supported empty result has
|
`status` is `supported` or `unavailable`; a supported empty result has
|
||||||
`active_count: 0`, while an unavailable result omits the count (unknown is
|
`active_count: 0`, while an unavailable result omits the count (unknown is
|
||||||
never represented as zero). OpenCode uses its documented
|
never represented as zero). OpenCode uses its documented
|
||||||
`GET /api/experimental/schedule?sessionID=...` read and counts only records
|
`GET /api/experimental/schedule?sessionID=...` read and counts only records
|
||||||
whose native `sessionID` matches and whose status is `active`. The older
|
whose native `sessionID` matches and whose status is `active`. The older
|
||||||
OpenCode serve surface, Claude Code (which currently exposes schedule data
|
OpenCode serve surface and Codex report their exact readback limitation.
|
||||||
only through events/hooks), and Codex report their exact readback limitation.
|
Claude Code is supported only after its native Stop-hook reports a bounded
|
||||||
|
`session_crons` snapshot; `observed_at_ms` is when that last snapshot arrived,
|
||||||
|
not a live query, and both one-shot and recurring entries count. Before that,
|
||||||
|
or when the host is stale/legacy or metadata is malformed, Claude reports
|
||||||
|
`unavailable` rather than guessing from transcript history or CronCreate.
|
||||||
The request is ownership-checked against the operator session registry,
|
The request is ownership-checked against the operator session registry,
|
||||||
bounded and read-only; it never starts or resumes a model turn and does not
|
bounded and read-only; it never starts or resumes a model turn and does not
|
||||||
inspect transcripts or count project/global work.
|
inspect transcripts or count project/global work.
|
||||||
|
|||||||
@@ -6,11 +6,12 @@ supported empty result (`active_count: 0`) from an unavailable provider
|
|||||||
(`active_count` omitted), so a composer must not render unknown as zero.
|
(`active_count` omitted), so a composer must not render unknown as zero.
|
||||||
|
|
||||||
OpenCode endpoints that expose scheduling use the documented experimental read endpoint and filters
|
OpenCode endpoints that expose scheduling use the documented experimental read endpoint and filters
|
||||||
the validated response to the requested session and `active` status. Claude
|
the validated response to the requested session and `active` status. Fresh
|
||||||
Code's installed control protocol has no authoritative read-only schedule
|
Claude CLI processes now register a private native Stop-hook observer; only a
|
||||||
operation; its event/hook metadata is therefore not promoted to a current
|
validated bounded `session_crons` snapshot is promoted, with `observed_at_ms`
|
||||||
count. Codex has no documented native scheduled-management read API. No
|
identifying when it was reported. Old hosts and sessions without that snapshot
|
||||||
schedule is created, changed, resumed, or inferred from a transcript or cwd.
|
remain unavailable. Codex has no documented native scheduled-management read
|
||||||
|
API. No schedule is created, changed, resumed, or inferred from a transcript or cwd.
|
||||||
|
|
||||||
Pipeline291 passed implementation and code review (UI review skipped: backend-only),
|
Pipeline291 passed implementation and code review (UI review skipped: backend-only),
|
||||||
then blocked at its read-only delivery-stage commit step. The manager retained the
|
then blocked at its read-only delivery-stage commit step. The manager retained the
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# Claude automation manager review and recovery
|
||||||
|
|
||||||
|
Run292 implemented the observer and completed two review/fix cycles, then failed
|
||||||
|
its third review with remaining timer-retention and exited-host issues. The
|
||||||
|
manager took over directly without creating a recovery workflow.
|
||||||
|
|
||||||
|
Resolved findings:
|
||||||
|
- Own observer callbacks acknowledge absent/invalid metadata and clear unknown
|
||||||
|
snapshots; unrelated callbacks keep their prior unsupported response.
|
||||||
|
- Direct snapshots belong only to the same live process. Hosted reads use the
|
||||||
|
actual non-attaching status and reject exited/unreachable hosts, with no
|
||||||
|
historical cache fallback. Observation time stays separate from query time.
|
||||||
|
- Status reads honor cancellation after connecting and bound responses to4MiB.
|
||||||
|
- Observed future native automations prevent adapter idle/excess reaping and
|
||||||
|
detached host idle cleanup. An empty native snapshot allows ordinary cleanup.
|
||||||
|
Existing explicit operator shutdown/max-detached policy remains unchanged.
|
||||||
|
|
||||||
|
Manager verification uses real owned scratch subprocesses and private Unix
|
||||||
|
sockets, not model inference or production sessions. It covers native callback
|
||||||
|
ACK without a server, retention/empty cleanup, missing metadata clearing, operator
|
||||||
|
permissions left pending, old/exited/replaced process refusal, active attachment
|
||||||
|
preservation, bounded-spool survival, cancellation and response-size refusal.
|
||||||
|
|
||||||
|
Checks passed: focused native automation tests; focused race tests for Claude and
|
||||||
|
claudehost; go build ./..., go vet ./..., go test ./...; git diff --check.
|
||||||
|
Logs: /tmp/claude-automation-manager-focused.log,
|
||||||
|
/tmp/claude-automation-manager-race.log, /tmp/claude-automation-go-final.log.
|
||||||
|
Native Claude model-auth probes failed before Stop; real native schedule delivery
|
||||||
|
remains unverified. No login/config changes or real chat prompts were performed.
|
||||||
|
Old retained hosts are not restarted or retrofitted. The composer feature and
|
||||||
|
combined deployment follow separately.
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# Claude native automation snapshots
|
||||||
|
|
||||||
|
`chat_automations` does not start a Claude turn or inspect a transcript. Fresh
|
||||||
|
Claude CLI initialization registers one private Stop hook callback. When the
|
||||||
|
native callback contains the expected main-thread session and a bounded
|
||||||
|
`session_crons` list, Pipelines retains only its IDs, count, and the report
|
||||||
|
timestamp. One-shot and recurring entries are both counted.
|
||||||
|
|
||||||
|
Detachable hosts acknowledge that exact observer callback themselves, including
|
||||||
|
while the Pipelines server is disconnected. The snapshot is independent of the
|
||||||
|
bounded output spool, so reattachment or spool trimming cannot erase it. Missing/malformed metadata clears the snapshot to unknown while the observer
|
||||||
|
still receives its nonblocking acknowledgement. Wrong-session data cannot update
|
||||||
|
a count. Permission callbacks remain operator-owned; other SDK callbacks retain
|
||||||
|
their existing response behavior. Missing data is never an empty schedule.
|
||||||
|
|
||||||
|
The API's `observed_at_ms` describes the last native Stop snapshot; it is not a
|
||||||
|
live schedule query and does not infer work from historical `CronCreate` calls.
|
||||||
|
|
||||||
|
Reported native timers prevent idle cleanup while pending. Empty snapshots permit
|
||||||
|
normal cleanup. The manager completed lifecycle fixes and integration checks after
|
||||||
|
run292; see claude-automation-manager-review.md for exact evidence and limits.
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
package claude
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/claudehost"
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
type automationInput struct{ bytes.Buffer }
|
||||||
|
|
||||||
|
func (*automationInput) Close() error { return nil }
|
||||||
|
|
||||||
|
func TestAutomationCallbacksAcknowledgeUnknownMetadataAndPreserveUnsupported(t *testing.T) {
|
||||||
|
c := newChat(context.Background())
|
||||||
|
defer c.cancel()
|
||||||
|
s := c.sessionFor("callback-cron", t.TempDir())
|
||||||
|
p := newProcess(s, "")
|
||||||
|
s.proc = p
|
||||||
|
input := &automationInput{}
|
||||||
|
p.stdin = input
|
||||||
|
call := func(id string, metadata map[string]any) map[string]any {
|
||||||
|
input.Reset()
|
||||||
|
request, _ := json.Marshal(map[string]any{"subtype": "hook_callback", "callback_id": id, "input": metadata})
|
||||||
|
s.handleControlRequest(p, lineEnvelope{RequestID: "hook", Request: request})
|
||||||
|
var response struct {
|
||||||
|
Response map[string]any `json:"response"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(input.Bytes(), &response); err != nil {
|
||||||
|
t.Fatal("callback was not acknowledged:", err)
|
||||||
|
}
|
||||||
|
return response.Response
|
||||||
|
}
|
||||||
|
metadata := map[string]any{"session_id": s.id, "hook_event_name": "Stop", "session_crons": []map[string]string{{"id": "timer"}}}
|
||||||
|
if r := call(claudehost.NativeAutomationCallbackID, metadata); r["subtype"] != "success" || s.automations == nil {
|
||||||
|
t.Fatalf("native observation failed: %+v", r)
|
||||||
|
}
|
||||||
|
delete(metadata, "session_crons")
|
||||||
|
if r := call(claudehost.NativeAutomationCallbackID, metadata); r["subtype"] != "success" || s.automations != nil {
|
||||||
|
t.Fatalf("absent metadata retained stale count or delayed ACK: %+v", r)
|
||||||
|
}
|
||||||
|
if r := call("unrelated-sdk-hook", metadata); r["subtype"] != "error" {
|
||||||
|
t.Fatalf("unrelated callback lost prior unsupported response: %+v", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNativeAutomationsPreventIdleAndExcessProcessReaping(t *testing.T) {
|
||||||
|
c := newChat(context.Background())
|
||||||
|
defer c.cancel()
|
||||||
|
for i := range maxLiveProcesses + 1 {
|
||||||
|
s := c.sessionFor(string(rune('a'+i)), t.TempDir())
|
||||||
|
p := newProcess(s, "")
|
||||||
|
s.proc, s.automationProcess = p, p
|
||||||
|
s.automations = &claudehost.AutomationSnapshot{SessionID: s.id, IDs: []string{"timer"}, ObservedAtMS: 123}
|
||||||
|
s.lastUsed = time.Now().Add(-2 * idleProcessExpiry)
|
||||||
|
}
|
||||||
|
c.reapIdleAt(time.Now())
|
||||||
|
for _, s := range c.sessions {
|
||||||
|
if s.proc == nil {
|
||||||
|
t.Fatal("native future work was reaped as idle or excess")
|
||||||
|
}
|
||||||
|
before := s.proc
|
||||||
|
s.releaseIdleProcess()
|
||||||
|
if s.proc != before {
|
||||||
|
t.Fatal("idle release bypassed native automation retention")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutomationReadRejectsDeadAndReplacedDirectProcesses(t *testing.T) {
|
||||||
|
c := newChat(context.Background())
|
||||||
|
defer c.cancel()
|
||||||
|
s := c.sessionFor("direct-cron", t.TempDir())
|
||||||
|
p := newProcess(s, "")
|
||||||
|
s.proc, s.automationProcess = p, p
|
||||||
|
s.automations = &claudehost.AutomationSnapshot{SessionID: s.id, IDs: []string{"timer"}, ObservedAtMS: 123}
|
||||||
|
info, err := c.Automations(context.Background(), s.id)
|
||||||
|
if err != nil || info.ActiveCount == nil || *info.ActiveCount != 1 {
|
||||||
|
t.Fatalf("live native snapshot: %+v, %v", info, err)
|
||||||
|
}
|
||||||
|
for _, state := range []string{"exited", "missing", "replacement"} {
|
||||||
|
s.proc = p
|
||||||
|
p.exited = state == "exited"
|
||||||
|
if state == "missing" {
|
||||||
|
s.proc = nil
|
||||||
|
} else if state == "replacement" {
|
||||||
|
s.proc = newProcess(s, "")
|
||||||
|
}
|
||||||
|
info, err = c.Automations(context.Background(), s.id)
|
||||||
|
if err != nil || info.Status != protocol.ChatAutomationUnavailable || info.ActiveCount != nil {
|
||||||
|
t.Fatalf("%s process reused stale timers: %+v, %v", state, info, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutomationRecordFallbackRejectsExitedHost(t *testing.T) {
|
||||||
|
dir, err := os.MkdirTemp("/tmp", "pcron")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
id := "exited-cron"
|
||||||
|
socket := filepath.Join(dir, "fixture.sock")
|
||||||
|
listener, err := net.Listen("unix", socket)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
record, _ := json.Marshal(claudehost.Record{SessionID: id, Socket: socket})
|
||||||
|
if err := os.WriteFile(claudehost.PathsFor(dir, id).Record, record, 0o600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
conn, err := listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
var request claudehost.Frame
|
||||||
|
_ = json.NewDecoder(conn).Decode(&request)
|
||||||
|
_ = json.NewEncoder(conn).Encode(claudehost.Frame{Op: claudehost.OpStatus, Status: &claudehost.Status{
|
||||||
|
SessionID: id, Exited: true,
|
||||||
|
Automations: &claudehost.AutomationSnapshot{SessionID: id, IDs: []string{"dead"}, ObservedAtMS: 123},
|
||||||
|
}})
|
||||||
|
}()
|
||||||
|
c := newChat(context.Background())
|
||||||
|
defer c.cancel()
|
||||||
|
c.hostDir = dir
|
||||||
|
info, err := c.Automations(context.Background(), id)
|
||||||
|
if err != nil || info.Status != protocol.ChatAutomationUnavailable || info.ActiveCount != nil {
|
||||||
|
t.Fatalf("exited host reused a stale native snapshot: %+v, %v", info, err)
|
||||||
|
}
|
||||||
|
<-done
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -90,20 +90,6 @@ func (c *Chat) Sessions(ctx context.Context) ([]protocol.ChatSession, error) {
|
|||||||
return sessions, nil
|
return sessions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Automations reports Claude Code's current limitation honestly. Claude's
|
|
||||||
// scheduled-task metadata is emitted through stop-hook/task events, but the
|
|
||||||
// installed control protocol has no authoritative read-only session schedule
|
|
||||||
// operation. Starting a process or inspecting transcripts would not establish
|
|
||||||
// the current schedule set, so this method never does either.
|
|
||||||
func (c *Chat) Automations(ctx context.Context, sessionID string) (protocol.ChatAutomationInfo, error) {
|
|
||||||
return protocol.ChatAutomationInfo{
|
|
||||||
SessionID: sessionID,
|
|
||||||
Provider: Backend,
|
|
||||||
Status: protocol.ChatAutomationUnavailable,
|
|
||||||
Reason: "Claude Code exposes scheduled-task metadata through events, but no authoritative read-only session schedule API is available",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *session) chatSessionLocked() protocol.ChatSession {
|
func (s *session) chatSessionLocked() protocol.ChatSession {
|
||||||
model := s.modelActual
|
model := s.modelActual
|
||||||
if model == "" {
|
if model == "" {
|
||||||
|
|||||||
@@ -288,7 +288,7 @@ func (c *Chat) reapIdleAt(now time.Time) {
|
|||||||
sort.Slice(live, func(i, j int) bool { return live[i].used.Before(live[j].used) })
|
sort.Slice(live, func(i, j int) bool { return live[i].used.Before(live[j].used) })
|
||||||
for _, entry := range live {
|
for _, entry := range live {
|
||||||
entry.s.mu.Lock()
|
entry.s.mu.Lock()
|
||||||
busy := entry.s.running
|
busy := entry.s.running || entry.s.hasAutomationsLocked()
|
||||||
for _, task := range entry.s.shells {
|
for _, task := range entry.s.shells {
|
||||||
if task.shell.Status == protocol.ChatShellRunning {
|
if task.shell.Status == protocol.ChatShellRunning {
|
||||||
busy = true
|
busy = true
|
||||||
@@ -352,8 +352,10 @@ type session struct {
|
|||||||
shellTranscriptMtime int64
|
shellTranscriptMtime int64
|
||||||
shellTranscriptTargets map[string]bool
|
shellTranscriptTargets map[string]bool
|
||||||
|
|
||||||
usage *protocol.ChatEventFrame
|
usage *protocol.ChatEventFrame
|
||||||
compaction *protocol.ChatEventFrame
|
compaction *protocol.ChatEventFrame
|
||||||
|
automations *claudehost.AutomationSnapshot
|
||||||
|
automationProcess *process
|
||||||
// account is the signed-in account the live process reported.
|
// account is the signed-in account the live process reported.
|
||||||
account accountIdentity
|
account accountIdentity
|
||||||
prompts []promptRecord
|
prompts []promptRecord
|
||||||
@@ -471,7 +473,7 @@ func (s *session) unsubscribe(ch chan protocol.ChatEventFrame) {
|
|||||||
// an explicit operator close remains the hard-close path below.
|
// an explicit operator close remains the hard-close path below.
|
||||||
func (s *session) releaseIdleProcess() {
|
func (s *session) releaseIdleProcess() {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
if s.running || len(s.subs) > 0 {
|
if s.running || len(s.subs) > 0 || s.hasAutomationsLocked() {
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -249,6 +249,7 @@ func (c *Chat) connectHostContext(ctx context.Context, record claudehost.Record)
|
|||||||
p.link.tasksIncomplete.Store(status.Tasks == nil && status.Base > 0)
|
p.link.tasksIncomplete.Store(status.Tasks == nil && status.Base > 0)
|
||||||
p.stderr.WriteString(status.Stderr)
|
p.stderr.WriteString(status.Stderr)
|
||||||
p.models, p.account, p.appliedModel = models, account, status.Model
|
p.models, p.account, p.appliedModel = models, account, status.Model
|
||||||
|
p.automations = status.Automations
|
||||||
plan := replayPlan{base: status.Base, silentUntil: status.Acked, catchUpEnd: status.End, pending: map[string]bool{}, busyAtAcked: status.BusyAtAcked, restore: true, tasks: status.Tasks}
|
plan := replayPlan{base: status.Base, silentUntil: status.Acked, catchUpEnd: status.End, pending: map[string]bool{}, busyAtAcked: status.BusyAtAcked, restore: true, tasks: status.Tasks}
|
||||||
for _, pending := range status.Pending {
|
for _, pending := range status.Pending {
|
||||||
plan.pending[pending.ID] = true
|
plan.pending[pending.ID] = true
|
||||||
@@ -267,6 +268,10 @@ func (c *Chat) connectHostContext(ctx context.Context, record claudehost.Record)
|
|||||||
func (s *session) adoptLocked(p *process) {
|
func (s *session) adoptLocked(p *process) {
|
||||||
s.models, s.account = p.models, p.account
|
s.models, s.account = p.models, p.account
|
||||||
s.appliedModel = p.appliedModel
|
s.appliedModel = p.appliedModel
|
||||||
|
if p.automations != nil {
|
||||||
|
s.automations = p.automations
|
||||||
|
s.automationProcess = p
|
||||||
|
}
|
||||||
if s.model == "" {
|
if s.model == "" {
|
||||||
s.model = p.appliedModel
|
s.model = p.appliedModel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ type process struct {
|
|||||||
models []protocol.ChatModel
|
models []protocol.ChatModel
|
||||||
account accountIdentity
|
account accountIdentity
|
||||||
appliedModel string
|
appliedModel string
|
||||||
|
automations *claudehost.AutomationSnapshot
|
||||||
}
|
}
|
||||||
|
|
||||||
func newProcess(s *session, mode string) *process {
|
func newProcess(s *session, mode string) *process {
|
||||||
@@ -192,7 +193,12 @@ func (s *session) startDirect(args []string, mode string, env []string) (*proces
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *process) initialize(ctx context.Context) ([]protocol.ChatModel, accountIdentity, error) {
|
func (p *process) initialize(ctx context.Context) ([]protocol.ChatModel, accountIdentity, error) {
|
||||||
raw, err := p.request(ctx, "initialize", map[string]any{"subtype": "initialize"})
|
raw, err := p.request(ctx, "initialize", map[string]any{
|
||||||
|
"hooks": map[string]any{"Stop": []any{map[string]any{
|
||||||
|
"hookCallbackIds": []string{claudehost.NativeAutomationCallbackID},
|
||||||
|
"timeout": 5, "onFailure": "continue",
|
||||||
|
}}},
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, accountIdentity{}, fmt.Errorf("initialize Claude Code: %w%s", err, p.diagnostic())
|
return nil, accountIdentity{}, fmt.Errorf("initialize Claude Code: %w%s", err, p.diagnostic())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/claudehost"
|
||||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -69,6 +70,7 @@ type messageBody struct {
|
|||||||
|
|
||||||
type toolRequest struct {
|
type toolRequest struct {
|
||||||
Subtype string `json:"subtype"`
|
Subtype string `json:"subtype"`
|
||||||
|
CallbackID string `json:"callback_id"`
|
||||||
ToolName string `json:"tool_name"`
|
ToolName string `json:"tool_name"`
|
||||||
DisplayName string `json:"display_name"`
|
DisplayName string `json:"display_name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
@@ -174,7 +176,22 @@ func (s *session) handleControlRequest(p *process, envelope lineEnvelope) {
|
|||||||
switch request.Subtype {
|
switch request.Subtype {
|
||||||
case "can_use_tool":
|
case "can_use_tool":
|
||||||
s.receivePermission(p, envelope.RequestID, request)
|
s.receivePermission(p, envelope.RequestID, request)
|
||||||
case "hook_callback", "mcp_message":
|
case "hook_callback":
|
||||||
|
if !claudehost.IsAutomationHook(request.CallbackID) {
|
||||||
|
s.respondUnsupported(p, envelope.RequestID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
snapshot, _ := claudehost.ParseAutomationHook(request.CallbackID, s.id, request.Input, time.Now().UnixMilli())
|
||||||
|
s.mu.Lock()
|
||||||
|
s.automations = snapshot
|
||||||
|
s.automationProcess = p
|
||||||
|
s.mu.Unlock()
|
||||||
|
// A detachable host observes and acknowledges its own callback before
|
||||||
|
// the server is involved. Direct processes need the adapter response.
|
||||||
|
if p.link == nil {
|
||||||
|
_ = p.respond(envelope.RequestID, map[string]any{})
|
||||||
|
}
|
||||||
|
case "mcp_message":
|
||||||
s.respondUnsupported(p, envelope.RequestID)
|
s.respondUnsupported(p, envelope.RequestID)
|
||||||
default:
|
default:
|
||||||
s.respondUnsupported(p, envelope.RequestID)
|
s.respondUnsupported(p, envelope.RequestID)
|
||||||
|
|||||||
@@ -0,0 +1,198 @@
|
|||||||
|
package claudehost
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func automationHost(t *testing.T) *host {
|
||||||
|
t.Helper()
|
||||||
|
dir, err := os.MkdirTemp("/tmp", "phcron")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
||||||
|
now := time.Now()
|
||||||
|
h := &host{dir: dir, id: "native-cron", paths: PathsFor(dir, "native-cron"),
|
||||||
|
idle: 50 * time.Millisecond, spoolLimit: 512,
|
||||||
|
pending: map[string]int64{}, modelReqs: map[string]string{}, modeReqs: map[string]string{},
|
||||||
|
changed: make(chan struct{}), stopped: make(chan struct{}), claudeEnd: make(chan struct{}),
|
||||||
|
activity: now, detached: now}
|
||||||
|
// A real subprocess sends the native SDK callback, waits for the host ACK,
|
||||||
|
// and remains alive without any Pipelines client attached.
|
||||||
|
script := `printf '%s\n' '{"type":"control_request","request_id":"observer","request":{"subtype":"hook_callback","callback_id":"pipelines-native-session-automations-v1","input":{"session_id":"native-cron","hook_event_name":"Stop","session_crons":[{"id":"future"}]}}}'; IFS= read -r ack; printf '%s\n' '{"type":"result"}'; sleep 30`
|
||||||
|
if err := h.start([]string{"/bin/sh", "-c", script}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { h.shutdown("automation test cleanup") })
|
||||||
|
go h.accept()
|
||||||
|
deadline := time.Now().Add(2 * time.Second)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
h.mu.Lock()
|
||||||
|
ready := h.automations != nil && !h.busy && len(h.pending) == 0 && len(h.lines) >= 2
|
||||||
|
h.mu.Unlock()
|
||||||
|
if ready {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
}
|
||||||
|
t.Fatal("native observer did not receive its ACK while detached")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNativeAutomationRetainsDetachedCLIUntilEmptySnapshot(t *testing.T) {
|
||||||
|
h := automationHost(t)
|
||||||
|
go h.watchIdle()
|
||||||
|
time.Sleep(180 * time.Millisecond)
|
||||||
|
select {
|
||||||
|
case <-h.stopped:
|
||||||
|
t.Fatal("idle cleanup killed a CLI with a future native automation")
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
if err := syscall.Kill(h.cmd.Process.Pid, 0); err != nil {
|
||||||
|
t.Fatal("the real native subprocess exited:", err)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
status, err := ReadStatus(ctx, h.paths.Socket)
|
||||||
|
if err != nil || status.Automations == nil || len(status.Automations.IDs) != 1 {
|
||||||
|
t.Fatalf("detached native snapshot: %+v, %v", status, err)
|
||||||
|
}
|
||||||
|
h.appendLine([]byte(`{"type":"control_request","request_id":"empty","request":{"subtype":"hook_callback","callback_id":"pipelines-native-session-automations-v1","input":{"session_id":"native-cron","hook_event_name":"Stop","session_crons":[]}}}`))
|
||||||
|
select {
|
||||||
|
case <-h.stopped:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("a known empty native snapshot did not allow idle cleanup")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutomationStatusDoesNotStealAttachmentAndSurvivesSpoolTrim(t *testing.T) {
|
||||||
|
h := automationHost(t)
|
||||||
|
client, _, err := Attach(h.paths.Socket, 0, time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
if _, err := client.Recv(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
h.mu.Lock()
|
||||||
|
attached := h.active
|
||||||
|
h.mu.Unlock()
|
||||||
|
for range 8 {
|
||||||
|
line, _ := json.Marshal(map[string]any{"type": "system", "subtype": "fixture", "padding": strings.Repeat("x", 300)})
|
||||||
|
h.appendLine(line)
|
||||||
|
h.mu.Lock()
|
||||||
|
h.acked = h.end
|
||||||
|
h.compactLocked()
|
||||||
|
h.mu.Unlock()
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
status, err := ReadStatus(ctx, h.paths.Socket)
|
||||||
|
if err != nil || status == nil || status.Base == 0 || status.Automations == nil || len(status.Automations.IDs) != 1 || len(status.Pending) != 0 {
|
||||||
|
t.Fatalf("trimmed native status: %+v, %v", status, err)
|
||||||
|
}
|
||||||
|
h.mu.Lock()
|
||||||
|
unchanged := h.active == attached
|
||||||
|
h.mu.Unlock()
|
||||||
|
if !unchanged {
|
||||||
|
t.Fatal("read-only status displaced the active delivery client")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutomationObserverLeavesPermissionsPendingAndMissingMetadataUnknown(t *testing.T) {
|
||||||
|
h := automationHost(t)
|
||||||
|
h.appendLine([]byte(`{"type":"control_request","request_id":"permission","request":{"subtype":"can_use_tool","tool_name":"Bash"}}`))
|
||||||
|
h.appendLine([]byte(`{"type":"control_request","request_id":"missing","request":{"subtype":"hook_callback","callback_id":"pipelines-native-session-automations-v1","input":{"session_id":"native-cron","hook_event_name":"Stop"}}}`))
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
status, err := ReadStatus(ctx, h.paths.Socket)
|
||||||
|
if err != nil || status.Automations != nil || len(status.Pending) != 1 || status.Pending[0].ID != "permission" {
|
||||||
|
t.Fatalf("observer consumed permission or retained absent metadata: %+v, %v", status, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadAutomationStatusCancelsAfterConnectionEstablished(t *testing.T) {
|
||||||
|
dir, err := os.MkdirTemp("/tmp", "phstatus")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
listener, err := net.Listen("unix", dir+"/host.sock")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
read := make(chan struct{})
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
conn, err := listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
var frame Frame
|
||||||
|
_ = json.NewDecoder(conn).Decode(&frame)
|
||||||
|
close(read)
|
||||||
|
_, _ = conn.Read(make([]byte, 1))
|
||||||
|
}()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
result := make(chan error, 1)
|
||||||
|
go func() { _, err := ReadStatus(ctx, dir+"/host.sock"); result <- err }()
|
||||||
|
<-read
|
||||||
|
cancel()
|
||||||
|
select {
|
||||||
|
case err := <-result:
|
||||||
|
if !errors.Is(err, context.Canceled) {
|
||||||
|
t.Fatalf("cancellation was not propagated: %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(500 * time.Millisecond):
|
||||||
|
t.Fatal("status read ignored cancellation after connecting")
|
||||||
|
}
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadAutomationStatusRejectsOversizedReply(t *testing.T) {
|
||||||
|
dir, err := os.MkdirTemp("/tmp", "phlimit")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
listener, err := net.Listen("unix", dir+"/host.sock")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
conn, err := listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
var frame Frame
|
||||||
|
_ = json.NewDecoder(conn).Decode(&frame)
|
||||||
|
_, _ = conn.Write([]byte(strings.Repeat("x", (4<<20)+1)))
|
||||||
|
}()
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if status, err := ReadStatus(ctx, dir+"/host.sock"); err == nil || status != nil {
|
||||||
|
t.Fatal("accepted an oversized status reply")
|
||||||
|
}
|
||||||
|
<-done
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package claudehost
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseAutomationHookRequiresExactCallbackAndSession(t *testing.T) {
|
||||||
|
input := json.RawMessage(`{"session_id":"ses_1","hook_event_name":"Stop","session_crons":[{"id":"once"},{"id":"repeat"}]}`)
|
||||||
|
if got, ok := ParseAutomationHook(NativeAutomationCallbackID, "ses_1", input, 123); !ok || len(got.IDs) != 2 || got.ObservedAtMS != 123 {
|
||||||
|
t.Fatalf("snapshot = %+v, ok=%v", got, ok)
|
||||||
|
}
|
||||||
|
for _, tc := range []struct{ callback, session string }{
|
||||||
|
{"other", "ses_1"}, {NativeAutomationCallbackID, "ses_2"},
|
||||||
|
} {
|
||||||
|
if _, ok := ParseAutomationHook(tc.callback, tc.session, input, 123); ok {
|
||||||
|
t.Fatalf("accepted wrong identity: %+v", tc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseAutomationHookDistinguishesEmptyFromAbsentAndBounds(t *testing.T) {
|
||||||
|
empty := json.RawMessage(`{"session_id":"ses_1","hook_event_name":"Stop","session_crons":[]}`)
|
||||||
|
got, ok := ParseAutomationHook(NativeAutomationCallbackID, "ses_1", empty, 123)
|
||||||
|
if !ok || got == nil || len(got.IDs) != 0 {
|
||||||
|
t.Fatalf("empty snapshot = %+v, ok=%v", got, ok)
|
||||||
|
}
|
||||||
|
absent := json.RawMessage(`{"session_id":"ses_1","hook_event_name":"Stop"}`)
|
||||||
|
if _, ok := ParseAutomationHook(NativeAutomationCallbackID, "ses_1", absent, 123); ok {
|
||||||
|
t.Fatal("accepted absent metadata")
|
||||||
|
}
|
||||||
|
bad := json.RawMessage(`{"session_id":"ses_1","hook_event_name":"Stop","session_crons":[{"id":""}]}`)
|
||||||
|
if _, ok := ParseAutomationHook(NativeAutomationCallbackID, "ses_1", bad, 123); ok {
|
||||||
|
t.Fatal("accepted malformed metadata")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutomationObserverAckDoesNotKeepDetachedHostPending(t *testing.T) {
|
||||||
|
dir, err := os.MkdirTemp("/tmp", "phauto")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
paths := PathsFor(dir, "ses_1")
|
||||||
|
spool, err := os.OpenFile(paths.Spool, os.O_CREATE|os.O_RDWR, 0o600)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer spool.Close()
|
||||||
|
listener, err := net.Listen("unix", paths.Socket)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
stdout, stdin, err := os.Pipe()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer stdin.Close()
|
||||||
|
defer stdout.Close()
|
||||||
|
go func() {
|
||||||
|
_, _ = io.Copy(io.Discard, stdout)
|
||||||
|
}()
|
||||||
|
h := &host{
|
||||||
|
dir: dir, id: "ses_1", paths: paths, idle: 50 * time.Millisecond,
|
||||||
|
spool: spool, listener: listener, stdin: stdin,
|
||||||
|
pending: map[string]int64{"hook-1": 0}, modelReqs: map[string]string{}, modeReqs: map[string]string{},
|
||||||
|
changed: make(chan struct{}), stopped: make(chan struct{}), claudeEnd: make(chan struct{}),
|
||||||
|
}
|
||||||
|
h.appendLine([]byte(`{"type":"control_request","request_id":"hook-1","request":{"subtype":"hook_callback","callback_id":"pipelines-native-session-automations-v1","input":{"session_id":"ses_1","hook_event_name":"Stop","session_crons":[]}}}`))
|
||||||
|
h.mu.Lock()
|
||||||
|
if _, ok := h.pending["hook-1"]; ok {
|
||||||
|
h.mu.Unlock()
|
||||||
|
t.Fatal("observer callback remained pending after host ACK")
|
||||||
|
}
|
||||||
|
h.activity = time.Now().Add(-time.Second)
|
||||||
|
h.detached = time.Now().Add(-time.Second)
|
||||||
|
h.mu.Unlock()
|
||||||
|
|
||||||
|
go h.watchIdle()
|
||||||
|
select {
|
||||||
|
case <-h.stopped:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("detached host was not released after observer ACK")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -63,6 +64,65 @@ func (c *Conn) Buffered() bool { return c.reader.Buffered() > 0 }
|
|||||||
|
|
||||||
func (c *Conn) Close() error { return c.conn.Close() }
|
func (c *Conn) Close() error { return c.conn.Close() }
|
||||||
|
|
||||||
|
// Status reads a snapshot without attaching or replacing the active delivery
|
||||||
|
// client. It is bounded by ctx and closes the short-lived connection.
|
||||||
|
func ReadStatus(ctx context.Context, socket string) (*Status, error) {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
|
deadline := time.Now().Add(3 * time.Second)
|
||||||
|
if d, ok := ctx.Deadline(); ok && d.Before(deadline) {
|
||||||
|
deadline = d
|
||||||
|
}
|
||||||
|
opCtx, cancel := context.WithDeadline(ctx, deadline)
|
||||||
|
defer cancel()
|
||||||
|
dialer := net.Dialer{}
|
||||||
|
conn, err := dialer.DialContext(opCtx, "unix", socket)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
stopCancel := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-opCtx.Done():
|
||||||
|
_ = conn.Close()
|
||||||
|
case <-stopCancel:
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
defer close(stopCancel)
|
||||||
|
if err := conn.SetDeadline(deadline); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(Frame{Op: OpStatus})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
data = append(data, '\n')
|
||||||
|
if _, err := conn.Write(data); err != nil {
|
||||||
|
if opCtx.Err() != nil {
|
||||||
|
return nil, opCtx.Err()
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
const maxStatusBytes = 4 << 20
|
||||||
|
line, err := bufio.NewReader(io.LimitReader(conn, maxStatusBytes+1)).ReadBytes('\n')
|
||||||
|
if err != nil {
|
||||||
|
if opCtx.Err() != nil {
|
||||||
|
return nil, opCtx.Err()
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(line) > maxStatusBytes {
|
||||||
|
return nil, fmt.Errorf("host status exceeds read limit")
|
||||||
|
}
|
||||||
|
var frame Frame
|
||||||
|
if err := json.Unmarshal(line, &frame); err != nil || frame.Op != OpStatus || frame.Status == nil {
|
||||||
|
return nil, fmt.Errorf("unexpected host status response")
|
||||||
|
}
|
||||||
|
return frame.Status, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Attach dials socket and starts delivery from offset. It returns the status
|
// Attach dials socket and starts delivery from offset. It returns the status
|
||||||
// the host replied with; output frames follow on the connection.
|
// the host replied with; output frames follow on the connection.
|
||||||
func Attach(socket string, offset int64, timeout time.Duration) (*Conn, *Status, error) {
|
func Attach(socket string, offset int64, timeout time.Duration) (*Conn, *Status, error) {
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ type host struct {
|
|||||||
modeReqs map[string]string
|
modeReqs map[string]string
|
||||||
model string
|
model string
|
||||||
mode string
|
mode string
|
||||||
|
automations *AutomationSnapshot
|
||||||
exited bool
|
exited bool
|
||||||
exitErr string
|
exitErr string
|
||||||
exitedAt time.Time
|
exitedAt time.Time
|
||||||
@@ -297,9 +298,11 @@ type snoop struct {
|
|||||||
RequestID string `json:"request_id"`
|
RequestID string `json:"request_id"`
|
||||||
ParentToolUseID string `json:"parent_tool_use_id"`
|
ParentToolUseID string `json:"parent_tool_use_id"`
|
||||||
Request struct {
|
Request struct {
|
||||||
Subtype string `json:"subtype"`
|
Subtype string `json:"subtype"`
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Mode string `json:"mode"`
|
Mode string `json:"mode"`
|
||||||
|
CallbackID string `json:"callback_id"`
|
||||||
|
Input json.RawMessage `json:"input"`
|
||||||
} `json:"request"`
|
} `json:"request"`
|
||||||
Response struct {
|
Response struct {
|
||||||
Subtype string `json:"subtype"`
|
Subtype string `json:"subtype"`
|
||||||
@@ -331,7 +334,7 @@ func (h *host) appendLine(line []byte) {
|
|||||||
var env snoop
|
var env snoop
|
||||||
_ = json.Unmarshal(line, &env)
|
_ = json.Unmarshal(line, &env)
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
ackObserver := false
|
||||||
start := h.end
|
start := h.end
|
||||||
data := append(append([]byte(nil), line...), '\n')
|
data := append(append([]byte(nil), line...), '\n')
|
||||||
h.fileMu.RLock()
|
h.fileMu.RLock()
|
||||||
@@ -349,6 +352,14 @@ func (h *host) appendLine(line []byte) {
|
|||||||
if env.RequestID != "" {
|
if env.RequestID != "" {
|
||||||
h.pending[env.RequestID] = start
|
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":
|
case "control_cancel_request":
|
||||||
delete(h.pending, env.RequestID)
|
delete(h.pending, env.RequestID)
|
||||||
case "control_response":
|
case "control_response":
|
||||||
@@ -377,6 +388,13 @@ func (h *host) appendLine(line []byte) {
|
|||||||
}
|
}
|
||||||
h.compactLocked()
|
h.compactLocked()
|
||||||
h.broadcastLocked()
|
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
|
// 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),
|
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,
|
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,
|
Exited: h.exited, ExitErr: h.exitErr, Stderr: h.stderr.String(), Directory: cwd,
|
||||||
|
Automations: h.automations,
|
||||||
}
|
}
|
||||||
if h.cmd != nil && h.cmd.Process != nil {
|
if h.cmd != nil && h.cmd.Process != nil {
|
||||||
status.ClaudePID = h.cmd.Process.Pid
|
status.ClaudePID = h.cmd.Process.Pid
|
||||||
@@ -708,7 +727,8 @@ func (h *host) watchIdle() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
attached := h.active != nil
|
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
|
tooLong := h.maxDetached > 0 && now.Sub(h.detached) > h.maxDetached
|
||||||
exitedLong := h.exited && now.Sub(h.exitedAt) > h.idle
|
exitedLong := h.exited && now.Sub(h.exitedAt) > h.idle
|
||||||
h.mu.Unlock()
|
h.mu.Unlock()
|
||||||
|
|||||||
@@ -24,6 +24,12 @@ import (
|
|||||||
"strings"
|
"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.
|
// Version is the host protocol version reported in every status.
|
||||||
const Version = 1
|
const Version = 1
|
||||||
|
|
||||||
@@ -114,6 +120,52 @@ type Status struct {
|
|||||||
Stderr string `json:"stderr,omitempty"`
|
Stderr string `json:"stderr,omitempty"`
|
||||||
// Directory is the CLI's working directory.
|
// Directory is the CLI's working directory.
|
||||||
Directory string `json:"directory,omitempty"`
|
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
|
// Record is the registry file a host writes next to its socket so a
|
||||||
|
|||||||
@@ -158,7 +158,11 @@ type ChatAutomationInfo struct {
|
|||||||
Provider string `json:"provider"`
|
Provider string `json:"provider"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
ActiveCount *int `json:"active_count,omitempty"`
|
ActiveCount *int `json:"active_count,omitempty"`
|
||||||
Reason string `json:"reason,omitempty"`
|
// ObservedAtMS is when the provider reported its last native snapshot. It
|
||||||
|
// is not the time this read was performed and therefore never implies a
|
||||||
|
// live query.
|
||||||
|
ObservedAtMS int64 `json:"observed_at_ms,omitempty"`
|
||||||
|
Reason string `json:"reason,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Content part types carried by ChatPart.Type and ChatEvent.Kind.
|
// Content part types carried by ChatPart.Type and ChatEvent.Kind.
|
||||||
|
|||||||
Reference in New Issue
Block a user