agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

bug(lifecycle): orchestrator terminated mid-turn by a stale-activity/probe race, then silently replaced with no linkage or notification

Open mukesher4 opened this issue 4 months ago • 2 comments

Bug

A long-running orchestrator session (pledg-turbo-monolith-4) was marked IsTerminated=true / activity.state=exited / status=terminated while it was still genuinely alive and mid-turn — its own Claude Code transcript shows real activity (a turn_duration/stop_hook_summary event, then an away_summary event) after the daemon's recorded termination timestamp. A brand-new orchestrator session was then spawned in its place 3 seconds after the old session's last transcript event, with zero linkage back to the original session and no notification to the user. The user only discovered the swap by asking "what happened to the orchestrator that was already working here?" and had to manually recover context by grepping raw .jsonl transcripts.

Source: reported directly in chat by the project owner | Analyzed against: a54003e7 (origin/main HEAD) Confidence: High — traced the exact code paths below and cross-checked them against live session records, the session's own transcript, and the daemon's hook-delivery log. Priority: high — core reliability of long-running orchestrator sessions; loses conversation continuity for the primary supervising agent with zero user-facing signal, no workaround other than manually diffing transcripts (no priority:* label applied automatically since I can't independently confirm "many users affected" — flagging severity in the body instead).

Timeline (UTC, all directly confirmed)

  • pledg-turbo-monolith-4 (kind: orchestrator) created 2026-07-04T06:47:24Z, ran ~3 days across several Claude Code session UUIDs in the same transcript directory (normal compaction/resume behavior).
  • 09cccbfd-e7de-413f-a3bb-efdcd4f86583.jsonl (that orchestrator's transcript): 2026-07-07T18:35:35.238Z last assistant message, 18:35:35.422Z system stop_hook_summary, 18:35:35.428Z system turn_duration, then 18:38:38.674Z system away_summary — all confirming the session was alive and had just finished a turn well past 18:30.
  • ao session get pledg-turbo-monolith-4 --json reports activity.state: "exited", isTerminated: true, updatedAt: "2026-07-07T18:30:46.569Z" — ~5 minutes before the transcript's stop_hook_summary/turn_duration and ~8 minutes before its away_summary.
  • pledg-turbo-monolith-19, a new kind: orchestrator session for the same project, was created 2026-07-07T18:38:41.164Z — 3 seconds after the old transcript's last event — with no reference to pledg-turbo-monolith-4 anywhere in its record.
  • ~/.ao/data/hooks.log has zero entries for pledg-turbo-monolith-4 anywhere near this window (its only entries are from 2026-07-05T00:49:24Z, an unrelated "daemon not running" failure) — ruling out a failed SessionEnd hook delivery as the trigger; no hook of any kind is logged as attempted around 18:30:46Z.

Root Cause

Two independent, compounding defects, both confirmed by code trace:

1. A stale-activity gate + non-sticky idle state lets a single probe reading kill a live session

  • lifecycle.Manager.ApplyRuntimeObservation (backend/internal/lifecycle/manager.go:100-110) sets IsTerminated=true and Activity.State=ActivityExited whenever runtimeClearlyDead(...) is true.
  • runtimeClearlyDead (backend/internal/lifecycle/runtime.go:25-28) requires f.Probe == ports.ProbeDead (from the reaper's 5-second-interval tmux has-session check, backend/internal/observe/reaper/reaper.go:20, backend/internal/adapters/runtime/tmux/tmux.go:176-190) AND !hasRecentActivity(activity, observedAt, window), where window = defaultRecentActivityWindow = 60 * time.Second (backend/internal/lifecycle/runtime.go:10,12-23).
  • Critically, ActivityIdle is not sticky (only ActivityWaitingInput is — backend/internal/domain/activity.go:19-21). Between a Stop hook (turn-end → idle) and the next user-prompt-submit (→ active), or whenever a hook delivery is dropped (hook delivery is explicitly documented as best-effort, manager.go:140-141), the recorded activity can go stale past 60s while the agent is still genuinely working (e.g. a long tool call, or Claude Code's own idle/away bookkeeping producing no AO-visible signal). If a single ProbeDead sample lands from the reaper during that window — for any transient reason (tmux server hiccup, a race in probe timing, anything that isn't cleanly ProbeFailed) — the session is killed immediately and irreversibly. There is no retry, no requirement for multiple consecutive dead readings, and no reconciliation if the pane turns out to still be there.
  • The adapter code is explicit that this exact race is expected by design: backend/internal/adapters/agent/claudecode/activity.go:57-59 — "SessionEnd is not guaranteed on crash/SIGKILL, so the reaper remains the backstop; both paths guard on IsTerminated, so whichever lands first wins." No safeguard exists for the case where the reaper wins incorrectly.
  • Given the transcript shows no SessionEnd event before 18:30:46Z and hooks.log shows no hook activity for this session in that window, the reaper/probe path is the far more likely trigger here, not a genuine SessionEnd hook.

2. The idempotent orchestrator-respawn path blindly spawns a replacement with no linkage, no clean-exit check, and no notification

  • SpawnOrchestrator(ctx, projectID, clean=false) (backend/internal/service/session/service.go:269-307) is the sole "ensure a live orchestrator exists" path. It checks only !IsTerminated across existing orchestrator sessions (service.go:279,291); if none are active, it calls Spawn(...) with no reference to the prior session's id, no reason code, and no check for whether the prior termination was "clean."
  • verifyOrchestratorReplacement (service.go:318-333) only validates the new session's own fields (harness, branch) — it never looks at the old session at all.
  • The retire-notice mechanism (sendRetireNotice/orchestratorRetireNotice, service.go:284,309-316) — the only user-facing "your old orchestrator was replaced" messaging that exists in this codebase — only fires on the explicit clean=true user-initiated replacement flow, not on this idempotent clean=false respawn.
  • No autonomous backend loop spawns orchestrators (confirmed: nothing in backend/internal/daemon, the reaper, or any observer creates a kind=orchestrator session). Every clean=false call site is a user-facing "open orchestrator" UI action (ShellTopbar.tsx, SessionsBoard.tsx, Sidebar.tsx) that finds nothing active in cache — consistent with the user clicking into the project a few seconds after the reaper had already (incorrectly) flipped the old session to terminated, and getting a cold, unlinked, unexplained new session instead of any indication something had gone wrong.

Suggested Fix

  • Require the runtime-probe kill path to see multiple consecutive ProbeDead readings (or a longer confirmation window) before flipping IsTerminated, rather than acting on one sample — especially for kind=orchestrator sessions, which are long-lived and higher-value than a typical worker.
  • Consider making the hasRecentActivity staleness window adaptive to whether a turn is actually in flight, or treat a session's own harness-reported "actively producing output" signal (if obtainable) as an override that suppresses ProbeDead verdicts.
  • On the SpawnOrchestrator(clean=false) respawn path: pass the terminated predecessor's session id into the new session's record, and extend the existing orchestratorRetireNotice mechanism (already built for the clean=true path) to also fire here, so the new session's first turn (and the user, via a UI toast/notification) is told "the previous orchestrator session ended unexpectedly at <time>; here's a link to its transcript" instead of starting cold with no explanation.

Impact

Loss of orchestrator continuity mid-task with zero notification anywhere (neither the dying session nor the replacement says anything) — for a background-running supervising agent, this is a real trust problem: work and context can silently vanish, discoverable only by the user manually grepping raw .jsonl transcript files. This is exactly the kind of incident that erodes confidence in leaving an orchestrator unattended.

Related

Two closed issues from the pre-rewrite TypeScript codebase describe the same class of failure (process-liveness probe false-positive → premature "killed"/"exited" verdict), though neither is this Go rewrite's code and neither is an exact duplicate — citing as precedent that this failure mode has recurred across rewrites:

  • "Orchestrator session immediately marked as 'killed' despite running fine" (closed, TS-era lifecycle-manager.ts/agent-claude-code race between session creation and first liveness poll)
  • "ps timeout conflated with 'process not found' — bulk-terminates all sessions when ps exceeds 5s" (closed, TS-era, same probe-failure-collapsed-to-dead pattern this issue's root-cause section describes for the current Go reaper)

Also potentially related in the current Go codebase (not independently verified against this issue's mechanism, flagging for triage cross-reference):

  • #1454 — "terminated sessions never revive when probes later disagree — causes dashboard state flicker"
  • #2025 — "indeterminate agent process-probe vetoes authoritative runtime-dead — sessions stuck forever in 'detecting/runtime_lost'"

mukesher4 avatar Jul 07 '26 18:07 mukesher4

Thanks for contributing to Agent Orchestrator.

This issue is being picked up by the current external contributor on-call pair:

  • @neversettle17-101
  • @somewherelostt

If someone is already working on this, please continue as usual. The on-call pair is added for visibility, tracking, and support, not to take over the work. If you need help with review, direction, reproduction, or next steps, please tag @neversettle17-101 and @somewherelostt here. You can also join the AO Discord if you want faster context, want to ask questions live, or just want to follow what the community is building.

You are also welcome to join the AO Discord community.

We do daily Discord calls at 10 PM IST, except Saturday.

Join the session here: https://discord.gg/H6ZDcUXmq

Come by if you want to see what is being built, ask questions, or just hang around with the community.

somewherelostt avatar Jul 13 '26 14:07 somewherelostt

Assigned to @shankywho

Rishet11 avatar Sep 14 '26 22:09 Rishet11