Repository navigation
Description
Summary
When the process running mecatl crashes mid-turn (confirmed cause: a full
machine crash), a session's persisted state snapshot is left at running
forever. /sessions (mecatui) and Service.ListSessions report the session
as still in-progress indefinitely, even across restarts, even if nobody ever
touches it again.
Confirmed repro
Session 645004ce13d2b8dd10adc384e161f786 (a background subagent child of an
interactive session), stored under
~/.local/state/mecatui/sessions/.../645004ce13d2b8dd10adc384e161f786.session.jsonl:
- The last snapshot line (108/108) reads
{"state":"running", ...}. - The corresponding
.events.jsonltail ends mid-flight on asubagent.tool
Greptool call event with no matching tool result ever recorded —
consistent with the whole machine dying mid-tool-call.
Root cause
e.save(ctx, sess) inside engine/agent/loop.go persists the session's
state mid-turn, well before a turn/run completes. If the process dies
before a terminal transition (terminate/terminateComplete) runs, the last
durable snapshot is stuck at StateRunning forever.
The run-entry funnel loadAndReopen (internal/adapter/server/service.go)
already has recovery seams for the three terminal states —
completed → Reopen, cancelled → Interrupt, failed → Recover — but none
for running. The state machine's own doc comment
(engine/session/session.go) documents exactly three recovery seams and
never mentions one for StateRunning at all. Service.ListSessions also
reads the persisted state directly with no liveness check.
A working precedent exists but is scoped only to the scheduler:
internal/app/scheduler_reconcile.go's stale-fire reconciler detects a stale
running schedule-fire session and settles it — but it never sees ordinary
interactive or subagent sessions, which have no ScheduleStore row.
Practically, the stuck snapshot's trailing assistant message can carry
unanswered tool_use calls (the confirmed case), so a follow-up prompt on
that session risks a provider HTTP 400 from a dangling tool call, in addition
to the /sessions display bug.
Fix
Tracked by an internal design/implementation plan generalizing the
scheduler's reconcile pattern into a 4th session recovery seam plus a
liveness-aware sweep, correct under both single-process and
multi-replica/leased deployments (mecak8s + Redis).
Activity
fix(session): add Abandon() recovery seam for crash-orphaned running …
feat(server): add session staleness test for crash-orphaned running s…
fix(server): correct SettleIfStale doc comment and add IsLive re-check
fix(server): repair crash-orphaned running sessions after the real lease
feat(app): sweep crash-orphaned running sessions at startup and on a …
fix(app): join the stale-session sweep goroutine on close and honor l…
test(app): pin the lease-sweep-disabled guard against a list-call cou…
docs: document the crash-orphaned session-recovery mechanism (AGENTS.…
docs: fix imprecise child-session residual wording in ADR 0027 Phase 6
test(app): add end-to-end proof the stale-running-session bug is fixed
test(app): pin the sweep's Build-time composition wiring, not just th…
fix(server): reject direct StartRunContent on delegation-child sessio…
fix(app,server): address panel-review mechanical findings (error hand…
Merge pull request #485 from stacklok/fix/stale-running-sessions
fix(authz): classify the stale-session sweep's new Service methods
Caller separation: enforce owner-scoped access across sessions, sched…
fix(server): let the stale-session sweep re-acquire a lost lease
fix(server): let the stale-session sweep re-acquire a lost lease