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

bug(lifecycle): sessions show no_signal right after the daemon restarts/app reopens, even when the agent process never stopped

Open mukesher4 opened this issue 4 months ago • 5 comments

Bug

Every pre-existing worker/orchestrator session shows status no_signal shortly after the AO daemon restarts (equivalently: after reopening the desktop app) — even when the underlying agent process (tmux session) was never actually stopped, only the daemon itself restarted.

Source: live observation, this environment | Confidence: Medium — symptom confirmed with direct database evidence; exact mechanism NOT confirmed despite a real investigation attempt (see below).

Reproduction

  1. Have several active worker/orchestrator sessions running normally (e.g. working/idle/needs_input).
  2. Restart the AO daemon (or reopen the desktop app).
  3. Within ~90s, ao session ls shows every pre-existing session as no_signal, even though their tmux panes/agent processes were never interrupted.

Evidence

Direct SQLite inspection (~/.ao/data/ao.db) immediately after a real daemon restart:

id                     activity_state  first_signal_at   activity_last_at    runtime_handle_id
agent-orchestrator-6   idle            (empty)            <restart time>      agent-orchestrator-6
agent-orchestrator-11  idle            (empty)            <restart time>      agent-orchestrator-11
agent-orchestrator-13  idle            (empty)            <restart time>      agent-orchestrator-13

first_signal_at NULL and activity_last_at stamped to the restart time (not the session's real prior activity) is the fingerprint of MarkSpawned (backend/internal/session_manager/manager.go) having run for these sessions — which per deriveStatus() (backend/internal/service/session/status.go) produces no_signal once the 90s grace window elapses, since no new hook signal has fired since. runtime_handle_id was correctly populated in every case, ruling out "empty handle skips the alive-probe" as the cause.

Root cause — NOT confirmed; investigation attempted and inconclusive

The reconcile path that's supposed to prevent this (reconcileLive/Reconcile, backend/internal/session_manager/manager.go, ~lines 887/946) has a documented no-op "adopt" branch for any session where runtime.IsAlive() reports true — it should leave Activity/FirstSignalAt untouched for genuinely-alive sessions.

A dedicated investigation (isolated daemon instance, instrumented IsAlive/reconcileLive, deliberate kill+restart against known-alive tmux sessions) could not reproduce the bug: IsAlive correctly returned true in ~7ms every time, and reconcileLive correctly took the no-op branch. Several code-level hypotheses were ruled out (PATH resolution is one-time at daemon construction, not per-call; IsAlive correctly distinguishes "missing" from "probe error"; graceful shutdown doesn't touch live session rows).

Cross-checking the real production DB evidence against tmux's own session-creation timestamps found the affected session's tmux session was recreated at the exact moment of the reset — which is at least as consistent with the session having genuinely died and been correctly recovered as with a false "dead" probe reading, since tmux new-session fails if a name already exists. This specific data point doesn't clearly support a race-condition theory.

One more loose end flagged during investigation: the currently-running production daemon binary's build isn't cleanly verifiable against any specific worktree commit (non--trimpath builds aren't reproducible across build directories), so a source/version mismatch between the running binary and the code reviewed can't be ruled out as an alternative explanation either.

Impact

Session status becomes unreliable immediately after any daemon restart or app reopen — a class of event that isn't rare (updates, crashes, manual restarts). For up to 90 seconds (or longer, for sessions nobody happens to interact with soon after), perfectly healthy sessions are indistinguishable from ones with a genuinely broken hook pipeline, defeating the purpose of the no_signal status.

Recommended next steps

Since this couldn't be reproduced from a clean isolated instance, resolving it needs either:

  1. Instrumenting the actual production daemon across a real restart (not achievable from an isolated repro), or
  2. Confirming what commit actually built the currently-running production binary, to rule out (or confirm) a source-mismatch explanation.

Related

  • #2501 — a different, already-confirmed premature-termination mechanism (single bad ProbeDead reading + stale activity window). Adjacent subsystem, different mechanism — that one is confirmed root-caused; this one is not.

mukesher4 avatar Jul 11 '26 07:07 mukesher4

Root cause — confirmed

Traced this from the reporter's evidence to a concrete mechanism (checkout at 821cda64, live sessions agent-orchestrator-6/-7/-11/-12/-14/-15/-16/-17 all showing no_signal with first_signal_at NULL, activity_last_at clustered within ~5s of each other — a batch MarkSpawned from a RestoreAll pass, not the sessions' real prior activity).

The daemon's shutdown/startup cycle (SaveAndTeardownAll → RestoreAll, backend/internal/session_manager/manager.go:833) fully tears down and relaunches every live session's harness process on every daemon restart — it does not merely reattach to a still-running process. For Claude Code, the relaunch command is built by GetRestoreCommand (backend/internal/adapters/agent/claudecode/claudecode.go:215), which resumes via claude --resume <sessionId> without resubmitting a prompt — it only reopens the existing conversation and waits at the TUI prompt.

MarkSpawned (backend/internal/lifecycle/manager.go:248) unconditionally clears FirstSignalAt on every restore ("each spawn/restore must re-prove its hook pipeline"). But Claude Code's SessionStart hook carries no activity signal by design (backend/internal/adapters/agent/claudecode/activity.go:19-34 — only user-prompt-submit, stop, notification, and session-end map to a state). Since restore never submits a prompt, no qualifying hook can ever fire for a session that was idle at the time of the restart, until the user manually sends it a new message. deriveStatus (backend/internal/service/session/status.go:54) then downgrades it to no_signal after the 90s grace and it stays there permanently — not a transient hiccup, but a dead end with no self-healing path.

This also means the severity scales with how often the daemon restarts: ~/.ao/data/hooks.log in this environment shows daemon-down hook-delivery failures recurring almost daily over the past week, so most idle workers accumulate in no_signal between sessions.

Fix direction: either (a) have GetRestoreCommand/the restore flow treat SessionStart on a resume as a qualifying signal (it does prove the pipeline works, even without activity semantics), or (b) don't reset FirstSignalAt on a restore that resumes the same native session id (only reset it on a true fresh spawn), or (c) submit a lightweight no-op nudge on resume to force a real hook to fire.

Confidence: High — mechanism fully traced end to end and matches the reporter's DB fingerprint exactly.

mukesher4 avatar Jul 12 '26 07:07 mukesher4

Correction/nuance to my earlier comment

I said no qualifying hook can ever fire for an idle resumed session, but that's slightly imprecise. notification events with notification_type: idle_prompt ARE a qualifying signal (activity.go:46 maps them to waiting_input, true) — Claude Code's CLI does have a built-in idle-notification timer for exactly this situation.

The more precise finding: empirically, idle_prompt is not firing at all for sessions relaunched via --resume. Checked agent-orchestrator-6 live — idle since the last restore at 06:48:39 UTC, still zero signal received at 07:27:24 UTC (~39 minutes), well past whatever cooldown that notification normally uses on a fresh interactive launch.

So the actual gap is narrower than "no event type maps to a signal for this case" — it's "the idle_prompt notification hook never arrives after claude --resume, even though the event IS wired to be treated as a signal if it ever showed up." Worth having whoever picks this up check:

  • Does Claude Code's idle-notification timer start at all on a --resume launch vs. a fresh launch?
  • Is the hook config (which drives which native events get piped to ao hooks claude-code notification) actually still in effect after --resume, or does something about the restore path skip re-applying it?

This doesn't change the suggested fix direction from my prior comment (don't reset FirstSignalAt on a resume that reuses the same native session id) — that's still the more robust fix since it doesn't depend on Claude Code's own idle-notification behavior. But it's worth noting in case the assigned worker wants to also chase down why idle_prompt itself is silent on resume, as a secondary/defense-in-depth fix.

mukesher4 avatar Jul 12 '26 07:07 mukesher4

Fix pushed: PR #2622.

Implements the decoupled-signal approach discussed above rather than the weaker 'trust a resume of a known session id' shortcut: SessionStart now reports a wire-only ActivitySignalOnly marker that stamps FirstSignalAt (proof-of-wiring) without touching Activity.State, so it works for every launch (fresh or resumed, busy or idle) without masking a genuinely broken hook pipeline.

mukesher4 avatar Jul 12 '26 08: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

Closing as a duplicate of #3315. Both track the same root cause: metadata-only SessionStart callbacks after spawn/restore are not consistently recorded as first-signal receipts, producing false no_signal state after a daemon restart. Continuing the focused fix there.

codebanditssss avatar Sep 23 '26 14:09 codebanditssss