Skip to content

fix(core): recover pending session execution intent - #63942

Open
HeZ2z wants to merge 5 commits into
anomalyco:devfrom
HeZ2z:session-inbox-recovery
Open

HeZ2z wants to merge 5 commits into
anomalyco:devfrom
HeZ2z:session-inbox-recovery

Conversation

@HeZ2z

@HeZ2z HeZ2z commented Nov 14, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #60596

Type of change

  • Bug fix

What does this PR do?

A prompt admitted right before a crash stays stuck when the process dies between Session.prompt enqueuing its inbox row and the turn writing its execution claim. Startup recovery only looked at claims, and the row itself did not record whether its admission asked to run, so nothing could tell that prompt apart from admit-only work.

Three changes:

  • session.inbox.enqueued gains an optional resume field, so the durable row records the wake its admission requested. Events written before this decode as admit-only, so existing rows stay ineligible.
  • Startup recovery also selects top-level Sessions that hold a pending row carrying that intent, and resumes them with the same forced, input-scoped drain the lost wake would have run. Candidates are deduplicated against claimed Sessions and against children a Subagent Job owns.
  • When recovery exhausts its attempt budget it retires the rows it considered: session.inbox.resume.retired records an admission watermark, and the projector stops treating rows at or below it as resumable. The event commits in the terminal's own transaction, so a crash cannot separate the terminal from the eligibility decision, and pending rows are kept rather than deleted.

A candidate recovered only from its row gets no "continuing after restart" synthetic: it never started a turn, so its pending prompt is the instruction. Only a claimed Session gets the notice.

Known limits, unchanged from the earlier review:

  • Rows admitted before this change carry no intent and stay admit-only.
  • A retried message ID keeps the intent recorded by its first admission.
  • synthetic reads the revert gate before admission, so a revert staged in between no longer suppresses the wake.
  • session.inbox.resume.retired and the resume flag are durable recovery state on the client-visible event log. Hiding them would break replay, and experimental.session.log already streams internal durable facts for the same reason. If you want that bookkeeping kept out of the public contract, say where the boundary should sit and I will move it.

No flag: this restores a wake the crashed process lost, and retirement bounds it. An opt-out for #56032 / #59635 covers this path too, because a recovered drain goes through the same prepareResume and the same forced, input-scoped execution.resume as the claim path.

Supersedes #60788, which the compliance bot closed because one template heading was wrong; the code review there had no open findings beyond the event-contract question above.

How did you verify your code works?

Rebased onto d759c548aa. Dev moved to Job-based subagent recovery in between, so I re-integrated the selection logic around it and re-ran everything at this head instead of carrying the earlier numbers over.

  • session-execution.test.ts, session-projector.test.ts, session-prompt.test.ts, session-run-coordinator.test.ts: 187 pass, 0 fail.
  • packages/schema suite: 93 pass, 0 fail.
  • typecheck:tsc in packages/core: only the errors already present on d759c548aa (@effect/sql-sqlite-node, undici/index.js, src/mcp/client.ts), none in the files this touches.
  • bun run generate in packages/client: zero diff.
  • bun run migration --check: no schema drift; one migration adds both resume and resume_retired.
  • oxlint over the changed files: 0 findings.

The crash matrix is still covered by tests: recovery in both crash windows, admit-only and retired rows not resumed, no duplicate when a Session is claimed and also a candidate, budget exhaustion (including a second sweep doing nothing), and the coordinator's owner re-check when the durable boundary read suspends.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #60245, #63875

Public API: adds optional resume to session.inbox.enqueued and a new durable session.inbox.resume.retired event (backwards compatible, additive)

Admission now records whether it requested automatic execution on the durable
inbox row, so restart recovery can tell a prompt that was meant to run from an
admit-only one. The intent is optional on the event and defaults to false in the
projection, so legacy events and existing rows stay ineligible.
A turn that keeps dying must not crash-loop across restarts, and the rows that
made it a candidate must stop being resumable once recovery terminalizes it.
Retirement is a durable event committed in the same transaction as the terminal,
scoped to an admission watermark captured at the decision, so a prompt admitted
while the turn settles stays eligible.
A prompt admitted to the durable inbox is lost when the process dies before the
execution claim is written: the row stays pending but the claim-only sweep never
sees it. Startup recovery now also selects top-level Sessions holding eligible
rows and resumes them with the same forced, input-scoped drain the lost wake
would have used. Only a claimed turn gets the restart-continuation notice, since
only it was actually working.
The interrupt reason now reads the durable admission watermark only after the
coordinator accepts an interruption, so an idle interrupt does no work. Because
that read suspends, the coordinator captures the owner first and re-checks it
before recording a reason or forking, treating a departed owner as the
settlement-window no-op instead of interrupting a stale fiber.
The regenerated types carry the admission resume flag and the internal
retirement event on experimental.session.log.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works. Startup recovery now resumes a top-level Session whose prompt was admitted with resume but never claimed, and leaves admit-only and retired rows alone. To check this, I put the base restart.ts back into the PR tree: the new recovery tests in session-execution.test.ts fail, and with the PR they pass. The session-execution, session-projector, session-prompt and session-run-coordinator tests and the packages/schema suite pass. typecheck:tsc in packages/core is clean.

Public API: two additive, backwards-compatible changes to the durable session event log and the generated client types:

  • session.inbox.enqueued gets an optional resume: boolean.
  • A new durable event, session.inbox.resume.retired (sessionID, reason: "exhausted" | "interrupted", through). It is published on budget exhaustion and also on every user interrupt while rows are pending.

Clients that switch over every event type will see the new type. The SQLite migration adds session_inbox.resume and session_inbox.resume_retired, both defaulting to false.

Concerns:

  • Design: writing the claim when the prompt is admitted might fix the same crash window much more simply. Details are inline on prepareResume.
  • Interrupt path: this is a live-path change beyond crash recovery and adds a new public event; inline on the interrupt code in execution.ts.
  • Tests: about 600 lines of tests for about 400 lines of code, including roughly 400 in session-execution.test.ts. Please trim them to the core cases: a row-only prompt is resumed once, an admit-only or retired row is not, and exhaustion retires the rows.

Related: #60245, #63875

})

const prepareResume = Effect.fnUntraced(function* (sessionID: SessionSchema.ID) {
const prepareResume = Effect.fnUntraced(function* (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was writing the execution claim in the same transaction as the admission (when resume !== false) considered? The #60596 handoff suggested it as one option. A crash before the wake would then leave a normal claim, which listSuspended, countResume and store.release already handle. That would make the separate row-only candidate path, the resume/resume_retired columns, the through watermark and the new public event unnecessary, as well as the interrupt-path changes. The retirement design adds a second source of truth for "should this Session run after a restart" alongside the claim, and the two need to agree. If that approach can't work, please say why.

// runs again on its own". The boundary is read lazily, only once the coordinator has
// accepted an interruption and while it still holds that execution, so an idle interrupt
// does no work and a boundary read never outlives its owner.
const interrupted = yield* coordinator.interrupt(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, a user interrupt reads the inbox before it is acknowledged, and run-coordinator's interrupt accepts an Effect for its reason, which adds the owner re-check. It can also publish session.inbox.resume.retired to the client-visible log on every interrupt while rows are pending. This is needed only because the per-row resume flag would otherwise re-run stopped work after a restart. A claim-based approach gets that for free: Interrupted already releases the claim. If the row-flag design stays, maintainers should decide whether this bookkeeping belongs on the public event contract (DurableDefinitions, generated client types) or should stay internal.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

session: admitted prompt can remain stuck after a crash before execution claim

1 participant