Skip to content

fix(opencode): recover stuck stop boundaries - #2738

Open
timigod wants to merge 7 commits into
getpaseo:mainfrom
timigod:review/opencode-stop-boundary-recovery
Open

fix(opencode): recover stuck stop boundaries#2738
timigod wants to merge 7 commits into
getpaseo:mainfrom
timigod:review/opencode-stop-boundary-recovery

Conversation

@timigod

@timigod timigod commented Aug 1, 2026

Copy link
Copy Markdown

Summary

  • classify OpenCode stop-boundary start failures and retry the unsent prompt exactly once
  • isolate the replacement OpenCode runtime generation so closing the old session cannot cancel replacement work
  • atomically register the replacement before closing the old session so Stop never loses agent ownership during recovery
  • revalidate the original registered agent immediately before replacement install so a concurrent close cannot be resurrected
  • close a stale replacement and preserve the durable closed record when reload loses ownership
  • make old-session close and recovery cancellation abortable and bounded for Stop, close, and daemon shutdown
  • fence asynchronous OpenCode turn startup with a generation canceled by Stop or shutdown before provider submission
  • reject pending local startup when autonomous OpenCode activity wins slash-command discovery
  • classify derived OpenCode providers through typed base-provider provenance rather than their visible provider ID
  • preserve current event-stream reconnect and provider-status reconciliation behavior

Testing

  • npx vitest run packages/server/src/server/agent/providers/opencode-agent.test.ts --bail=1 (92 passed)
  • npx vitest run packages/server/src/server/agent/agent-manager.test.ts --bail=1 (165 passed)
  • npx vitest run packages/server/src/server/agent/provider-registry-wrap.test.ts --bail=1 (2 passed)
  • held-storage regression for close before reload replacement commit
  • successful follow-up prompt after Stop during recovery handoff
  • named shutdown coverage during asynchronous slash-command discovery
  • npm run build:server
  • npm run typecheck
  • npm run lint
  • npm run format
  • npm run format:check
  • git diff --check

@timigod
timigod marked this pull request as ready for review August 1, 2026 09:38
@greptile-apps

greptile-apps Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR hardens OpenCode stop-boundary recovery and concurrent lifecycle handling.

  • Retries an unsent prompt once after a typed OpenCode stop-boundary rejection.
  • Adds atomic replacement-session registration, ownership revalidation, bounded cleanup, and cancellation handling.
  • Fences asynchronous OpenCode startup and supports derived OpenCode providers through base-provider provenance.
  • Expands regression coverage for Stop, close, shutdown, reload, and startup races.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains within the scope of the available follow-up review threads.

Important Files Changed

Filename Overview
packages/server/src/server/agent/agent-manager.ts Adds coordinated OpenCode recovery, replacement registration, cancellation admission, bounded cleanup, and lifecycle-race handling.
packages/server/src/server/agent/agent-run-state.ts Extends pending-run tracking to stage startup events and settle admitted turns safely.
packages/server/src/server/agent/agent-sdk-types.ts Adds typed startup rejection, admission, close, resume, and provider-provenance contracts.
packages/server/src/server/agent/provider-registry.ts Preserves typed base-provider provenance when wrapping derived provider sessions.
packages/server/src/server/agent/providers/opencode-agent.ts Classifies stop-boundary failures and fences asynchronous startup against cancellation and autonomous activity.
packages/server/src/server/agent/agent-manager.test.ts Adds broad behavioral coverage for recovery, replacement ownership, cancellation, and shutdown races.
packages/server/src/server/agent/providers/opencode-agent.test.ts Covers OpenCode startup generation fencing, stop-boundary classification, and slash-command discovery races.
packages/server/src/server/agent/provider-registry-wrap.test.ts Verifies wrapped sessions retain their derived identity and typed base-provider provenance.
packages/server/src/server/agent/providers/opencode/test-utils/test-opencode-harness.ts Extends the OpenCode test harness to coordinate and observe asynchronous startup scenarios.

Sequence Diagram

sequenceDiagram
  participant C as Client
  participant M as AgentManager
  participant O as Old OpenCode session
  participant R as Replacement session

  C->>M: Submit prompt
  M->>O: startTurn(prompt)
  O-->>M: previous_turn_still_stopping
  M->>R: Resume isolated provider generation
  M->>M: Revalidate ownership
  M->>M: Atomically register replacement
  M->>O: Close old session (bounded)
  alt Stop, close, or shutdown before admission
    C->>M: Cancel
    M->>M: Abort recovery
    M-->>C: turn_canceled
  else Retry admitted
    M->>R: Retry original prompt once
    R-->>M: Turn events
    M-->>C: Stream turn result
  end
Loading

Reviews (2): Last reviewed commit: "Merge latest main into OpenCode stop rec..." | Re-trigger Greptile

@timigod
timigod marked this pull request as draft August 1, 2026 13:45
@timigod
timigod marked this pull request as ready for review August 1, 2026 18:56
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.

1 participant