Skip to content

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

Open

Description

@HeZ2z

Description

A prompt can remain indefinitely in the durable inbox if the process dies after admitting it but before its execution claim is recorded. Startup recovery scans claimed sessions, so this admitted prompt is not resumed until another action wakes the session. The prompt is durable but effectively stuck. The earlier source-review report #51746 was automatically closed for template compliance and invited a new report; this reproduction exercises the admission and recovery implementation.

Steps to reproduce

On dev commit 82666b6612cdb2ba13a7a26e150b21e897a80373, save this script to /tmp/oc-recovery-gap.ts and run from packages/core:

bun -e "$(cat /tmp/oc-recovery-gap.ts)"
import { Effect, Layer } from 'effect'
import { AppNodeBuilder } from '@opencode/core/effect/app-node-builder'
import { LayerNode } from '@opencode/util/effect/layer-node'
import { Database } from '@opencode/core/database/database'
import { Bus } from '@opencode/core/bus'
import { Job } from '@opencode/core/job'
import { Session } from '@opencode/core/session'
import { SessionRestart } from '@opencode/core/session/execution/restart'
import { SessionExecution } from '@opencode/core/session/execution'
import { SessionProjector } from '@opencode/core/session/projector'
import { SessionInbox } from '@opencode/core/session/inbox'
import { SessionMessage } from '@opencode/core/session/message'
import { SessionStore } from '@opencode/core/session/store'
import { SessionTable, SessionInboxTable } from '@opencode/core/session/sql'
import { Project } from '@opencode/core/project'
import { ProjectTable } from '@opencode/core/project/sql'
import { AbsolutePath } from '@opencode/core/schema'
const calls=[]
const layer=AppNodeBuilder.build(LayerNode.group([Database.node,Bus.node,SessionProjector.node,SessionInbox.node,SessionStore.node,Job.node,Session.node]),[Bus.node.replace(Bus.configured({persist:true}))])
await Effect.runPromise(Effect.gen(function*(){
 const database=yield* Database.Service;const db=database.db;const store=yield* SessionStore.Service;const bus=yield* Bus.Service;const jobs=yield* Job.Service;const sessions=yield* Session.Service
 const id=Session.ID.make('ses_crash_gap');yield* db.insert(ProjectTable).values({id:Project.ID.global,worktree:AbsolutePath.make('/project'),sandboxes:[]}).onConflictDoNothing().run();yield* db.insert(SessionTable).values({id,project_id:Project.ID.global,slug:id,directory:'/project',title:id,version:'test'}).run()
 const admission=yield* SessionInbox.Service; yield* admission.admit({id:SessionMessage.ID.create(),sessionID:id,item:{type:'user',payload:{text:'Please fix the bug'},delivery:'steer'}})
 const execution=SessionExecution.Service.of({active:Effect.succeed(new Set()),isActive:()=>Effect.succeed(false),resume:(id)=>Effect.sync(()=>calls.push(id)),wake:()=>Effect.void,interrupt:()=>Effect.succeed(false),awaitIdle:()=>Effect.void})
 const restart=yield* SessionRestart.Service.pipe(Effect.provide(SessionRestart.layer().pipe(Layer.provide(Layer.succeed(SessionStore.Service,store)),Layer.provide(Layer.succeed(Bus.Service,bus)),Layer.provide(Layer.succeed(Job.Service,jobs)),Layer.provide(Layer.succeed(Session.Service,sessions)),Layer.provide(Layer.succeed(SessionExecution.Service,execution)))))
 console.log('before',JSON.stringify({pending:yield* SessionInbox.list(db,id),suspended:yield* store.listSuspended()}))
 yield* restart.resumeSuspendedSessions
 console.log('after',JSON.stringify({pending:yield* SessionInbox.list(db,id),resumes:calls}))
}).pipe(Effect.provide(layer)))

The script uses real SQLite, durable event projection, SessionInbox.admit, SessionStore, and SessionRestart. It creates the database state corresponding to a crash between admission and execution claim, then runs startup recovery. SessionExecution is a recording fixture. It does not kill a real process or exercise Session.prompt end-to-end.

Actual: before recovery the prompt is pending and the suspended-claim list is empty; after recovery the prompt is still pending and no resume was requested. Expected: a prompt admitted with automatic execution intended should be recovered after restart, while preserving the separate resume: false admit-only behavior. Currently that intent is not represented in this simulated inbox row, which is relevant to designing a fix.

OpenCode version

Source dev commit 82666b6612cdb2ba13a7a26e150b21e897a80373; the current upstream restart file differs only in unrelated image notification metadata.

Operating system

macOS (Bun).

Activity

  1. opencodejr commented on Oct 30, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report and script. I could reproduce this on the latest dev (95fc3c8).

    The script below follows yours. It uses the real SQLite, persisted bus, projector, SessionInbox, SessionStore, Job and Session services, admits a steer user prompt to a session with no execution claim, and then runs SessionRestart.resumeSuspendedSessions with an execution stub that records resume calls. The prompt is pending before and after recovery, listSuspended() is empty, and no resume is requested.

    In the code, Session.prompt writes the prompt to the inbox first and only then calls execution.wake, while startup recovery only looks at sessions that already have a claim (time_suspended) plus running subagent children. Nothing scans pending inbox items, and the inbox row does not record whether automatic execution was intended. So a crash between those two steps leaves the prompt pending until something else wakes the session.

    Related issues:

    Reproduction script (oc-recovery-gap.ts)
    // Run from /workspace/repo/packages/core:  bun /workspace/repro/oc-recovery-gap.ts
    import { Effect, Layer } from 'effect'
    import { AppNodeBuilder } from '@opencode/core/effect/app-node-builder'
    import { LayerNode } from '@opencode/util/effect/layer-node'
    import { Database } from '@opencode/core/database/database'
    import { Bus } from '@opencode/core/bus'
    import { Job } from '@opencode/core/job'
    import { Session } from '@opencode/core/session'
    import { SessionRestart } from '@opencode/core/session/execution/restart'
    import { SessionExecution } from '@opencode/core/session/execution'
    import { SessionProjector } from '@opencode/core/session/projector'
    import { SessionInbox } from '@opencode/core/session/inbox'
    import { SessionMessage } from '@opencode/core/session/message'
    import { SessionStore } from '@opencode/core/session/store'
    import { SessionTable } from '@opencode/core/session/sql'
    import { Project } from '@opencode/core/project'
    import { ProjectTable } from '@opencode/core/project/sql'
    import { AbsolutePath } from '@opencode/core/schema'
    const calls: string[] = []
    const layer = AppNodeBuilder.build(
      LayerNode.group([Database.node, Bus.node, SessionProjector.node, SessionInbox.node, SessionStore.node, Job.node, Session.node]),
      [Bus.node.replace(Bus.configured({ persist: true }))],
    )
    await Effect.runPromise(
      Effect.gen(function* () {
        const database = yield* Database.Service
        const db = database.db
        const store = yield* SessionStore.Service
        const bus = yield* Bus.Service
        const jobs = yield* Job.Service
        const sessions = yield* Session.Service
        const id = Session.ID.make('ses_crash_gap')
        yield* db.insert(ProjectTable).values({ id: Project.ID.global, worktree: AbsolutePath.make('/project'), sandboxes: [] }).onConflictDoNothing().run()
        yield* db.insert(SessionTable).values({ id, project_id: Project.ID.global, slug: id, directory: '/project', title: id, version: 'test' }).run()
        // State after a crash between admission and execution claim: inbox row persisted, no claim.
        const admission = yield* SessionInbox.Service
        yield* admission.admit({ id: SessionMessage.ID.create(), sessionID: id, item: { type: 'user', payload: { text: 'Please fix the bug' }, delivery: 'steer' } } as any)
        const execution = SessionExecution.Service.of({
          active: Effect.succeed(new Set()),
          isActive: () => Effect.succeed(false),
          resume: (sid: any) => Effect.sync(() => { calls.push(sid) }),
          wake: () => Effect.void,
          interrupt: () => Effect.succeed(false),
          awaitIdle: () => Effect.void,
        } as any)
        const restart = yield* SessionRestart.Service.pipe(
          Effect.provide(
            SessionRestart.layer().pipe(
              Layer.provide(Layer.succeed(SessionStore.Service, store)),
              Layer.provide(Layer.succeed(Bus.Service, bus)),
              Layer.provide(Layer.succeed(Job.Service, jobs)),
              Layer.provide(Layer.succeed(Session.Service, sessions)),
              Layer.provide(Layer.succeed(SessionExecution.Service, execution)),
            ),
          ),
        )
        const before = yield* SessionInbox.list(db, id)
        console.log('before: pending =', before.length, 'suspended =', JSON.stringify(yield* store.listSuspended()))
        yield* restart.resumeSuspendedSessions
        yield* Effect.sleep('200 millis')
        const after = yield* SessionInbox.list(db, id)
        console.log('after:  pending =', after.length, 'resume calls =', JSON.stringify(calls))
        console.log(after.length > 0 && calls.length === 0 ? 'BUG: admitted prompt not recovered' : 'OK: prompt recovered')
      }).pipe(Effect.provide(layer as any)) as any,
    )

    Run it: bun oc-recovery-gap.ts

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions