Skip to content

fix(server): prevent premature OpenCode turn completion - #10805

Open
NaveDanan wants to merge 2 commits into
pingdotgg:mainfrom
NaveDanan:fix/opencode-running-thread
Open

fix(server): prevent premature OpenCode turn completion#10805
NaveDanan wants to merge 2 commits into
pingdotgg:mainfrom
NaveDanan:fix/opencode-running-thread

Conversation

@NaveDanan

@NaveDanan NaveDanan commented Sep 8, 2026

Copy link
Copy Markdown

Persisted OpenCode prompts could be marked complete while their session loop had not started or delegated work was still running. Keep recovered prompts active until an idle event confirms completion.

When reconnect happens before prompt acceptance, retain completion recovery after admission clears. A reply tied to the current prompt establishes that processing started before checking idle status. Retry unavailable message history with capped backoff, and reject stale responses after steering, interruption, or a new turn.

Validation: 267 tests passed across nine OpenCode and related provider test files, including delayed work, reconnect ordering, lookup failures, steering, interruption, shutdown, and replacement turns. Targeted lint, formatting, and server typecheck passed. Independent standards and behavior reviews found no remaining actionable findings.

Model: GPT-6. Harness: Codex.

Summary by CodeRabbit

  • Bug Fixes
    • Improved OpenCode turn handling after reconnects and during delayed activity.
    • Prevented turns from completing prematurely while busy, subagent, or compaction work remains active.
    • Improved recovery when idle events occur during final status checks.
    • Ensured recovered steering remains active until the provider confirms the turn is idle.
    • Improved recovery across prompt-acceptance races and message-history lookup failures.
  • Tests
    • Expanded coverage for admission, completion, reconnection, compaction, and idle-event race conditions.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-08T16:01:51.508641Z 2841bac PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Sep 8, 2026
@macroscopeapp

macroscopeapp Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 2841bac

Macroscope's review found this PR approvable — This is a narrowly scoped OpenCode server bug fix that prevents recovered turns from being marked complete before delegated work finishes. The production change is confined to existing admission/reconciliation logic and is covered by focused regression tests, with no schema, deployment, security, billing, default, or static-analysis configuration impact.

You can add or adjust custom eligibility rules. Learn more.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2841bac938

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1400 to +1402
context.promptAdmission = undefined;
context.awaitingBusyAfterInterruption = false;
return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve completion recovery after clearing admission

When server.connected arrives while prompt submission is still settling, the reconnect handler sees promptAdmission and therefore does not start idle reconciliation. If recovery later finds the persisted user message after OpenCode completed during the outage, this branch clears the admission and returns without polling or scheduling reconciliation; because the idle event was missed and is not replayed, activeTurnId and the session remain running indefinitely. Keep a completion-recovery path armed after clearing this state, particularly for reconnect-before-acceptance flows.

AGENTS.md reference: AGENTS.md:L19-L21

Useful? React with 👍 / 👎.

@coderabbitai

coderabbitai Bot commented Sep 8, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 0c68207c-3100-4305-9d28-99d50d7fa485

📥 Commits

Reviewing files that changed from the base of the PR and between 2841bac and c7af1d7.

📒 Files selected for processing (2)
  • apps/server/src/provider/Layers/OpenCodeAdapter.test.ts
  • apps/server/src/provider/Layers/OpenCodeAdapter.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The OpenCode adapter changes prompt admission recovery to use idle reconciliation and message-history validation. Tests cover reconnect recovery, delegated child activity, compaction, pending status requests, lookup failures, and cancellation races.

Changes

OpenCode turn recovery

Layer / File(s) Summary
Admission recovery state handling
apps/server/src/provider/Layers/OpenCodeAdapter.ts
Removes idleStatusConfirmations. Clears incomplete prompt admission state and routes qualifying idle observations through scheduleIdleReconciliation.
Reconnect message reconciliation
apps/server/src/provider/Layers/OpenCodeAdapter.ts
Polls recent session messages after recovery and reconciles the turn only when an assistant reply references the admitted prompt.
Recovery race coverage
apps/server/src/provider/Layers/OpenCodeAdapter.test.ts
Adds runtime message-history controls and tests delayed busy status, child activity, compaction, pending status requests, reconnect scenarios, lookup failures, and aborted snapshots.

Priority: ⬇️ Low

Estimated code review effort: 4 (Complex) | ~45 minutes

Merge Risk: ⚪ Minimal · up to c7af1

The change keeps recovered OpenCode turns active until valid idle reconciliation and adds broad race coverage. No actionable merge-blocking risk is currently established.

Sequence Diagram(s)

sequenceDiagram
  participant OpenCodeAdapter
  participant OpenCodeSession
  participant RuntimeState
  OpenCodeAdapter->>OpenCodeSession: recover prompt admission
  OpenCodeAdapter->>OpenCodeSession: poll recent messages
  OpenCodeSession-->>OpenCodeAdapter: latest assistant reply with parentID
  OpenCodeAdapter->>RuntimeState: schedule idle reconciliation
  RuntimeState-->>OpenCodeAdapter: complete the matching turn
Loading

Suggested reviewers: t3dotgg, maria-rcks, juliusmarminge

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the primary change: preventing premature OpenCode turn completion.
Description check ✅ Passed The description explains what changed, why it changed, and how it was validated. It does not use the template headings or include the checklist, but the required change rationale is present and no UI …
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@cursor

cursor Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Bugbot is paused — on-demand spend limit reached

Bugbot uses usage-based billing for this team and has hit its on-demand spend limit.

A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue.

@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). and removed size:M 30-99 changed lines (additions + deletions). labels Sep 8, 2026
@NaveDanan

NaveDanan commented Sep 8, 2026

Copy link
Copy Markdown
Author

Note

🤖 GPT-6 Astra responding on behalf of NaveDanan

Codex review caught a real reconnect regression in this PR. While testing it, I also found a related bug that was already there before this PR. Both could leave a finished thread showing as running. Both are now fixed, with regression tests added. All 267 tests and the targeted checks pass.

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

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant