Skip to content

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

Closed
HeZ2z wants to merge 6 commits into
anomalyco:devfrom
HeZ2z:session-recovery-intent
Closed

HeZ2z wants to merge 6 commits into
anomalyco:devfrom
HeZ2z:session-recovery-intent

Conversation

@HeZ2z

@HeZ2z HeZ2z commented Oct 31, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #60596

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When a process crashes between Session.prompt enqueuing an inbox row and a turn acquiring an execution claim, the prompt remains stranded because recovery only scanned claimed sessions and inbox rows did not record wake intent.

  • Records wake intent via an optional resume field on session.inbox.enqueued.
  • Extends startup recovery to resume top-level sessions holding pending rows with wake intent, deduplicated against claimed sessions and background children.
  • Retires exhausted recovery attempts via session.inbox.resume.retired scoped by an admission watermark, preventing restart loops without deleting pending rows.
  • Omits CONTINUE_AFTER_SERVER_RESTART notices for sessions recovered solely from pending rows where no turn ever began.

Known limits, deliberate and unchanged

  • Pre-existing rows: Rows admitted before this change lack intent and remain admit-only; earlier stranded prompts are not rescued.
  • Retry intent: A retried message ID retains the intent recorded by its first admission; advisory wakes from retries do not make an admit-only row crash-recoverable.
  • Synthetic timing: synthetic determines revert-gated intent before admission. A revert staged between that read and publication no longer suppresses the wake.
  • Public event contract: session.inbox.resume.retired and the resume flag on session.inbox.enqueued are durable recovery state streamable via experimental.session.log; the unresolved question for maintainers is whether this recovery bookkeeping belongs in the public event contract. Hiding durable definitions from the log would break replay.

How was this verified?

Reported verification across session test suites:

  • Crash matrix verified: recovery on crash windows, admit-only/retired row non-resumption, claim/pending candidate deduplication, crash during counting vs. after claim, retry budget exhaustion, and coordinator interleaving.
  • Regressions checked by intentionally reverting the overlap predicate and owner re-checks.
  • Suite checks: session-execution.test.ts, session-projector.test.ts, session-prompt.test.ts, and session-run-coordinator.test.ts (158 pass); schema suite (91 pass); typecheck:tsc clean in core and schema; migration checks, oxlint, and ast-grep clean.
  • Full packages/core suite: 7,160 pass / 35 skip / 29 baseline failures (byte-identical to base commit 1908fcb8b7: 11 Ripgrep, 5 pty, 1 pty defaults, 12 search tools).

Checklist

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

Added by the opencode bot (AI-generated).

Related: #56032, #59635, #60245

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

@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.

This fixes the startup gap. I ran the #60596 reproduction on the base and with this PR. On the base, the stored prompt is never resumed. With the PR, a prompt stored with resume: true is resumed once, and resume: false or a missing flag stays admit-only. I had to change the script so the restart layer is built inside a scope that stays open; as written, the forked resume is interrupted. The four session test files (166 tests) and the schema suite (91) pass, and typecheck:tsc is clean in core and schema.

Public API (additive, backwards compatible):

  • session.inbox.enqueued gains an optional resume: boolean field. Older events decode as admit-only.
  • New durable event session.inbox.resume.retired with { sessionID, reason: "exhausted" | "interrupted", through }. It appears in ExperimentalSessionLogOutput and the generated promise client types.
  • New session_inbox columns resume and resume_retired, added by two migrations.

Consumers that switch exhaustively on event types will see a new variant. No existing caller breaks.

Concerns:

  1. Auto-resume after restart. This adds a second automatic-resume path at startup, for sessions that never started a turn. Users in #56032 / #59635 are asking for fewer unattended resumes, so any opt-out added for those issues must cover this path too. Also, the recovered turn gets the "Continuing after restart" notice although no turn was running. See the inline comment.
  2. Bookkeeping in the public event stream. The retirement event and the resume flag are internal recovery state, but they now become part of the durable, client-visible event log. See the inline comment.
  3. Two migrations. One feature adds two migrations for the same table. They could be folded into one before merge.
  4. Tests. The test additions are very large for what they check: about 650 lines in session-execution.test.ts, coordinated by many Deferreds and interleavings. Please cut them down to a few clear cases: resumes once, admit-only not resumed, retired not resumed, no duplicate when a session is claimed and also a candidate, plus the coordinator owner re-check. The rest is expensive to maintain and hard to follow.

Related: #56032, #59635, #60245

Comment thread packages/schema/src/session-event.ts Resolved
@HeZ2z
HeZ2z force-pushed the session-recovery-intent branch from c5a60ba to ca1e2cb Compare October 31, 2026 04:26
@HeZ2z

HeZ2z commented Oct 31, 2026

Copy link
Copy Markdown
Author

Reworked in ca1e2cb.

Concern 1 — fixed. The restart continuation notice is now gated on the claimed path only. CONTINUE_AFTER_SERVER_RESTART says work was in progress, which is false for a Session recovered purely from its pending row, and a synthetic admitted ahead of that row would promote first. A row-only candidate now gets no synthetic at all; its pending prompt is the instruction. New test covers both directions.

On #56032 / #59635: the row-only path goes through the same prepareResume and the same forced input-scoped execution.resume as the claim path, so an opt-out added there covers this path without a second check.

Concern 3 — folded. Both migrations are gone; one migration now adds both resume and resume_retired.

Concern 4 — cut. session-execution.test.ts additions went 632 to 372 lines (−41%), and total test additions +841 to +622. Surviving cases are the ones with a regression they would catch: resumes once, admit-only/retired not resumed, claimed-and-candidate no duplicate, claimed resumes on its claim alone when its row is consumed, crash after counting before the claim keeps the budget, exhaustion then a second sweep does nothing, and the coordinator owner re-check. I kept two you did not list because each pins a bug that was caught during review rather than a hypothetical. One caution: my first cut used boundary markers that silently deleted pre-existing tests, so I rebuilt it and diffed the test names against the base commit — all base tests in the four files are still present.

Concern 2 — no change, with respect for the opposite view. Hiding the retirement event or the resume flag would break replay, and experimental.session.log already streams internal facts for the same reason (session.usage.recorded, session.message.content.updated). It is called out in the PR body so the trade-off is explicit rather than silent.

At your suggestion the history is now five commits, each independently green (147 / 150 / 157 / 158 / 158): schema intent, retirement, recovery selection, lazy interrupt resolution, and the regenerated client types.

@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 still works. I re-ran the #60596 reproduction at this head: a prompt stored with resume: true is resumed on startup and one stored with resume: false is not. The session test files (158 tests) pass and typecheck:tsc is clean in core.

Earlier points addressed:

  • The restart notice is no longer posted for a session recovered only from a pending prompt. That path shares the claim path's prepareResume and execution.resume, so an opt-out for #56032 / #59635 would apply to it as well.
  • The two migrations are now one.
  • The test additions are much smaller.

The event-contract thread stays open for maintainers to decide.

Public API: unchanged since the last review, all additive and backwards compatible. session.inbox.enqueued gets an optional resume field, and there is a new durable event session.inbox.resume.retired.

No new concerns.

HeZ2z added 5 commits October 31, 2026 12:35
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.
@HeZ2z

HeZ2z commented Oct 31, 2026

Copy link
Copy Markdown
Author

Rebased onto 1908fcb8b7 (upstream/dev had moved and the PR showed as conflicting). The five commits are rebuilt on the new base with the same titles; nothing in the change itself moved.

Two things the rebase surfaced, both worth knowing:

  • restart.ts merged cleanly — upstream's recoverToolJob/restartMessage refactor and our prepareResume changes touch disjoint regions. I verified rather than trusted the merge: the dispatch, both helpers and the reworded comment are intact, recoverShell/recoverImage are gone, and our retirement batching, notice gate, requireResumable pre-check and both unions are present.
  • The real conflicts were the migration artifacts: upstream added 20261030054400_job.ts at the same registry slot, so ours was renumbered and regenerated against the new base (one migration, both columns, as before).

The rebase also exposed a stale fixture, not a product bug: our claimed-candidate guard test seeded a background child into KV, but background jobs moved to a durable job table in this range, so the seed was silently ignored and the test failed. It now inserts the JobTable row, and I re-proved both guards after the port by reintroducing each regression and watching the test fail.

Re-verified on the new base: five commits each green in isolation, core typecheck:tsc clean, schema 91 pass, bun run generate produces zero diff, migration --check clean with exactly one new migration. Full core suite 7160 pass / 35 skip / 29 fail, and the failing set is byte-identical to 1908fcb8b7: 11 Ripgrep, 5 pty, 1 pty defaults, 12 search tools.

@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 still works after the rebase. Since my last review, the only changes are the migration (renamed and now ordered after the new job migration on dev) and one test that now seeds its background job in JobTable. Session source is unchanged. I re-ran the #60596 reproduction: a prompt stored with resume: true is resumed and one with resume: false is not. The session test files (158 tests) and the schema suite (91 tests) pass, and typecheck:tsc is clean in core.

The event-contract thread is not addressed and stays open for maintainers.

Public API: unchanged since the last review, all additive and backwards compatible. session.inbox.enqueued gets an optional resume field, and there is a new durable event session.inbox.resume.retired.

No new concerns.

@HeZ2z

HeZ2z commented Nov 1, 2026

Copy link
Copy Markdown
Author

Could a maintainer approve the pending CI workflows?

@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.

This push is a merge of the latest dev, and the fix still works. The projector conflict was resolved correctly: the PR's resume field and the session.inbox.resume.retired projection sit on top of dev's new helpers. The migration is still last in the list. I re-ran the #60596 reproduction: a prompt stored with resume: true is resumed and one with resume: false is not. The session test files (158 tests) and the schema suite (91 tests) pass, and typecheck:tsc is clean in core.

Public API: unchanged since the last review, all additive and backwards compatible. session.inbox.enqueued gets an optional resume field, and there is a new durable event session.inbox.resume.retired.

No new concerns.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 72 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 72-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Nov 12, 2026
@github-actions github-actions Bot closed this Nov 12, 2026
@HeZ2z
HeZ2z deleted the session-recovery-intent branch November 14, 2026 14:48
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