Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(opencode): restarted server stops an orphaned OpenCode server started through a wrapper #34959

Mergedjuliusmarmingemerged 5 commits intomain fromrefs/pull/34959/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

When OpenCode is launched through a wrapper, the ledger can record the server process while it still carries the wrapper's command line. After it execs, the full command no longer matches, so a restarted T3 server treated the group as a recycled pid and left the orphaned OpenCode server running. Ledger entries now also store the serve arguments (optional field, so version-1 entries without it still decode). On reap, a group is stopped when pid, start time and pgid match and either the exact command line or the saved serve arguments (as the command's suffix) match; the exact-command fallback is kept for wrappers that append their own options and for legacy entries. Author's approach kept unchanged; main merged in cleanly (merge commit), and one follow-up commit removes the test helper's now-unused ready/printed signal, since the wrapper test now synchronizes through fifos. The new wrapper test fails on main (Linux and Darwin observers) and passes with the fix. The legacy-entry and appended-options tests pass on main too; they guard the compatibility paths the competing PR #22209 removes. Note for the PR body: legacy ledger entries are retained and reaped by exact command, not dropped.

Fixes pingdotgg/t3code#22324

Reproduction on main

Proposed real-app procedure, not executed: On Linux or macOS, configure an OpenCode provider instance's Binary path to a wrapper that forks a child, exits immediately, and has the child wait on a FIFO before exec'ing the real OpenCode executable with the original arguments. Start an OpenCode thread in T3 Code; while startup is waiting, confirm the server has written the group's ledger entry, then release the FIFO and let OpenCode finish starting. Record the OpenCode PID and port, forcibly terminate only the owning T3 Code server to bypass graceful cleanup, and restart T3 Code with the same state directory. On main, the old OpenCode process and listening port remain; with this change, restart cleanup stops that recorded group. The gated fixture makes the otherwise intermittent fork-to-exec race deliberate.

Evidence

Real T3 server and OpenCode 1.19.5 on Linux, fresh synthetic state on each side. Launch an OpenCode thread through a FIFO-gated forking wrapper; confirm the ledger records its child before exec; release the gate and observe the same PID/start time/group running OpenCode on its recorded port; crash only T3 and restart with the same state. Both runs use a 200ms spawn-return delay, disable systemd agent scopes, and pause T3 after ledger creation while OpenCode boots to prevent separate wrapper-exit handling from racing the crash. No application source or ledger edits.

BeforeAfter
f313d0a6cb7be89fd3b0
Before: orphan survives restartAfter: orphan stopped and port closed
PID 58105 and port 34421 remain alive after ledger removal.PID 59825 stops and port 40429 closes; server logs identify cleanup of group 59824.
Before terminal recordingAfter terminal recording

Driver · Complete fixture sources, original terminal recordings, transcripts and sanitized server logs. Videos are rendered from timestamped live terminal output.

Not shown:

  • Unpaused wrapper startup was not demonstrated: T3's separate wrapper-exit handler ran before this OpenCode executable finished booting, so both proof runs paused T3 after ledger creation until the deliberate crash.
  • macOS behavior was not exercised; verification ran on Linux.
  • A completed model response was not verified; this proof covers real OpenCode process startup, crash survival and restart cleanup.

Tests

vp test run src/server/OpenCodeServerLedger.test.ts (packages/provider-opencode): 16/16 pass. Same tests with main's OpenCodeServerLedger.ts: 2 failed ('records a group whose spawned wrapper already exited', linux and darwin observers), 14 passed. vp run check:changed: format, lint, knip, typecheck (10 packages), tests (2 files, 18 tests) all pass.

Not in this change

  • Runtime proof with a real OpenCode wrapper binary is not done here (left to the proof step).
  • The PR description's statement about legacy ledger entries contradicts the code (entries are kept and matched by exact command) and should be rewritten when the PR body is updated.

Who did the work

  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
  • Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5

Run by the t3code factory in T3 Code.

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Approved at 7be89fd

      Macroscope's review found this PR approvable — Orphan cleanup now recognizes wrapper-started OpenCode servers after their command line changes during exec. Existing owner, process identity, and process-group checks keep cleanup confined to recorded orphaned servers.

      Notes:

      • All code in this push has already been reviewed. Approvability was decided on eligibility alone.
      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. github-actions[bot](GitHub profile, opens in a new tab)commented

      Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      CodexLive turn WebSocket decoded21.5 KiB21.5 KiB0 B (0.0%)24.4 KiB✅
      CodexLive turn messages110 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.9 KiB21.9 KiB0 B (0.0%)24.4 KiB✅
      ClaudeLive turn messages110 (0.0%)8✅

      Other scenarios

      All 64 scenario measurements match main.

      All 64 scenario measurements
      ProviderScenarioMetricMain baselineThis PRImpactPR ceiling
      Codexshort turn resumeWebSocket decoded10.5 KiB10.5 KiB0 B (0.0%)12.7 KiB✅
      Codexshort turn resumeWebSocket messages220 (0.0%)2✅
      Codexlong turn running, cold openHTTP decoded281.7 KiB281.7 KiB0 B (0.0%)302.7 KiB✅
      Codexlong turn running, cold openrows75750 (0.0%)75✅
      Codexlong turn running, history pageHTTP decoded92.4 KiB92.4 KiB0 B (0.0%)107.4 KiB✅
      Codexlong turn running, history pagerows75750 (0.0%)75✅
      Codexlong turn finished, cold openHTTP decoded381.5 KiB381.5 KiB0 B (0.0%)410.2 KiB✅
      Codexlong turn finished, cold openrows1511510 (0.0%)151✅
      Codexlong turn finished, socket cold openWebSocket decoded381.6 KiB381.6 KiB0 B (0.0%)410.2 KiB✅
      Codexlong turn finished, socket cold openWebSocket messages220 (0.0%)2✅
      Codexlong turn finished, socket cold openrows1511510 (0.0%)151✅
      Codexlong turn finished, work pageHTTP decoded186.2 KiB186.2 KiB0 B (0.0%)200.2 KiB✅
      Codexlong turn finished, work pagerows1501500 (0.0%)150✅
      Codexidle thread resumeWebSocket decoded65 B65 B0 B (0.0%)200 B✅
      Codexidle thread resumeWebSocket messages110 (0.0%)1✅
      Codexfleet shell snapshotHTTP decoded42.4 KiB42.4 KiB0 B (0.0%)53.7 KiB✅
      Codexfleet shell snapshotrows27270 (0.0%)27✅
      Codexfleet shell subscribeWebSocket decoded65 B65 B0 B (0.0%)200 B✅
      Codexfleet shell subscribeWebSocket messages110 (0.0%)1✅
      Codexfleet shell resume checkHTTP decoded18 B18 B0 B (0.0%)100 B✅
      Codexfleet shell resume checkrows0000✅
      Codexstale shell reloadHTTP decoded42.4 KiB42.4 KiB0 B (0.0%)53.7 KiB✅
      Codexstale shell reloadrows27270 (0.0%)27✅
      Codexstale shell resumeWebSocket decoded140 B140 B0 B (0.0%)400 B✅
      Codexstale shell resumeWebSocket messages220 (0.0%)2✅
      Codexstale todo list cold openHTTP decoded72.4 KiB72.4 KiB0 B (0.0%)83.0 KiB✅
      Codexstale todo list cold openrows75750 (0.0%)75✅
      Codexstale todo list cold opennodes220 (0.0%)4✅
      Codexstale todo list cold opensubagents220 (0.0%)4✅
      Codexstale todo list cold openplans110 (0.0%)2✅
      Codexstale todo list cold openproviderTurns110 (0.0%)2✅
      Codexstale todo list cold opencheckpoints110 (0.0%)2✅
      Claudeshort turn resumeWebSocket decoded10.6 KiB10.6 KiB0 B (0.0%)12.7 KiB✅
      Claudeshort turn resumeWebSocket messages220 (0.0%)2✅
      Claudelong turn running, cold openHTTP decoded283.3 KiB283.3 KiB0 B (0.0%)302.7 KiB✅
      Claudelong turn running, cold openrows75750 (0.0%)75✅
      Claudelong turn running, history pageHTTP decoded93.4 KiB93.4 KiB0 B (0.0%)107.4 KiB✅
      Claudelong turn running, history pagerows75750 (0.0%)75✅
      Claudelong turn finished, cold openHTTP decoded384.2 KiB384.2 KiB0 B (0.0%)410.2 KiB✅
      Claudelong turn finished, cold openrows1511510 (0.0%)151✅
      Claudelong turn finished, socket cold openWebSocket decoded384.3 KiB384.3 KiB0 B (0.0%)410.2 KiB✅
      Claudelong turn finished, socket cold openWebSocket messages220 (0.0%)2✅
      Claudelong turn finished, socket cold openrows1511510 (0.0%)151✅
      Claudelong turn finished, work pageHTTP decoded188.1 KiB188.1 KiB0 B (0.0%)200.2 KiB✅
      Claudelong turn finished, work pagerows1501500 (0.0%)150✅
      Claudeidle thread resumeWebSocket decoded66 B66 B0 B (0.0%)200 B✅
      Claudeidle thread resumeWebSocket messages110 (0.0%)1✅
      Claudefleet shell snapshotHTTP decoded43.0 KiB43.0 KiB0 B (0.0%)53.7 KiB✅
      Claudefleet shell snapshotrows27270 (0.0%)27✅
      Claudefleet shell subscribeWebSocket decoded66 B66 B0 B (0.0%)200 B✅
      Claudefleet shell subscribeWebSocket messages110 (0.0%)1✅
      Claudefleet shell resume checkHTTP decoded18 B18 B0 B (0.0%)100 B✅
      Claudefleet shell resume checkrows0000✅
      Claudestale shell reloadHTTP decoded43.0 KiB43.0 KiB0 B (0.0%)53.7 KiB✅
      Claudestale shell reloadrows27270 (0.0%)27✅
      Claudestale shell resumeWebSocket decoded140 B140 B0 B (0.0%)400 B✅
      Claudestale shell resumeWebSocket messages220 (0.0%)2✅
      Claudestale todo list cold openHTTP decoded73.4 KiB73.4 KiB0 B (0.0%)83.0 KiB✅
      Claudestale todo list cold openrows75750 (0.0%)75✅
      Claudestale todo list cold opennodes220 (0.0%)4✅
      Claudestale todo list cold opensubagents220 (0.0%)4✅
      Claudestale todo list cold openplans110 (0.0%)2✅
      Claudestale todo list cold openproviderTurns110 (0.0%)2✅
      Claudestale todo list cold opencheckpoints110 (0.0%)2✅

      Baseline: f313d0a · PR result: 7be89fd · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 108.5 KiB
      • Claude decoded thread snapshot: 108.8 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    4. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      The ledger now records server arguments and uses them to match processes during tracking and orphan cleanup. Tests cover wrapper-exit timing, legacy entries without recorded arguments, appended log-level arguments, and repeated port arguments.

      Changes: OpenCode server ledger

      Layer / File(s)Summary
      Record and identify the server process
      packages/provider-opencode/src/server/OpenCodeServerLedger.ts, packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts
      The ledger stores server arguments and uses their command-line suffix to select a process-group member. The wrapper test uses FIFO gates to record the process group before the child continues, then waits for the ready-file write.
      Match orphan processes during cleanup
      packages/provider-opencode/src/server/OpenCodeServerLedger.ts, packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts
      Orphan cleanup accepts a matching serve-argument suffix when arguments are recorded. Entries without arguments still require an exact command match. Tests cover both cases, appended --log-level=ERROR arguments, and replacing every matching port argument.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix · Severity of issue fixed: Low

      Suggested reviewers: ishaanko

      Merge Risk: Minimal · up to 7be89

      This change makes orphan cleanup of OpenCode server processes more reliable when a wrapper process changes its command line. No actionable merge-blocking risk was identified.

      Pre-merge checks: Passed 3 · Inconclusive 1

      Failed Failed checks: 1 inconclusive
      Check nameStatusExplanationResolution
      Linked Issues checkInconclusive[#22324] The ledger child-command race is addressed. The reviewed diff in OpenCodeServerLedger.ts adds an optional args field and runsServe, a suffix check on the observed command. stopOrphan acce…Provide evidence at the reviewed head that the non-ledger items are implemented: the merged commits or files, and the passing tests for pointer state, SSH zombie-process setup, timeline fixture wiring, favicon loading, and update.test.ts Wi…
      Passed Passed checks: 3 passed

      Check nameStatusExplanation
      Title checkPassedThe title names the main change: a restarted server now stops an orphaned OpenCode server that was started through a wrapper. It follows the conventional commit form and matches the ledger race fix in…
      Out of Scope Changes checkPassedAll changes in the reviewed diff are in packages/provider-opencode/src/server/OpenCodeServerLedger.ts and its test file. The schema field args, runsServe, the stopOrphan condition, and the recording…
      Description checkPassedThe description covers the problem, the change, the scope link, and verification in detail. It describes the root cause (wrapper command line recorded before exec), the new optional serve-arguments fi…
      Full details: Title check

      Explanation

      The title names the main change: a restarted server now stops an orphaned OpenCode server that was started through a wrapper. It follows the conventional commit form and matches the ledger race fix in the changeset (serve-argument suffix matching for recorded process groups). The title is 91 characters, which is longer than ideal but still clear.

      Full details: Out of Scope Changes check

      Explanation

      All changes in the reviewed diff are in packages/provider-opencode/src/server/OpenCodeServerLedger.ts and its test file. The schema field args, runsServe, the stopOrphan condition, and the recording path all serve the ledger race in #22324. The replace-to-replaceAll change in the 'leaves a serve process on another port alone' test follows from the new args field, which stores the port in both command and args. The commit that removes the unused ready signal from the test helper supports the reproduction fixture. No unrelated change was found.

      Full details: Description check

      Explanation

      The description covers the problem, the change, the scope link, and verification in detail. It describes the root cause (wrapper command line recorded before exec), the new optional serve-arguments field with its suffix match, and the fallback kept for legacy entries and appended options. It links the fixed issue (#22324). The Verification section lists focused test results (16/16 pass, 2 failures on main) and check:changed results. Real-app before/after evidence and a proof procedure are provided, with explicit notes on what was not shown. The description also states one inconsistency: it says legacy entries are dropped, then later says they are retained and matched by exact command. The template's own header comment asks for a conventional commit title, and the PR title is not part of the supplied context, so this cannot be checked. The description also includes an attribution section naming the models used, which the template asks for. Overall the description is complete and specific, with one internal contradiction to fix.

      Full details: Linked Issues check

      Explanation

      [#22324] The ledger child-command race is addressed. The reviewed diff in OpenCodeServerLedger.ts adds an optional args field and runsServe, a suffix check on the observed command. stopOrphan accepts either an exact command match or a serve-argument suffix match, with PID, start time, and PGID checks kept. The test change in OpenCodeServerLedger.test.ts adds two cases for legacy entries and for wrappers that append options. The author reports a 16/16 pass, but the test run is not re-verified here. [#22324] The other requested items are not shown at the reviewed head. The author states that pointer state, SSH zombie-process setup, timeline fixture wiring, favicon loading, and the Windows update.test.ts path handling were fixed by #22523, #22427, #22534, #22529, or #22557. The recent history shown at head does not list those PRs, and no diff in this PR touches those areas. The lower-confidence orchestration failure is not addressed or explained. The item on flaky release checks is a reported impact, not a code change.

      Resolution

      Provide evidence at the reviewed head that the non-ledger items are implemented: the merged commits or files, and the passing tests for pointer state, SSH zombie-process setup, timeline fixture wiring, favicon loading, and update.test.ts Windows path handling. Also state whether the orchestration test failure is resolved or deliberately excluded from #22324.

      Finishing Touches

      Generate unit tests (beta)

      • Commit to this branch
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Recent review info

      Run configuration

      Commits

      Reviewing files that changed from the base of the PR and between a92ac703a5426246db9048e89f80aa6b859c6663 and 7be89fd3b0dfaef14774981367edcb3cbb99597b.

      Files selected for processing: 1

      • packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

    5. tanstack-source added labelsbug

    6. juliusmarminge added labelstakeover:in-progress

    7. juliusmarminge added labelstakeover:ready

    8. juliusmarminge removed labelstakeover:in-progress

    9. juliusmarminge merged thisd0957bc

    10. juliusmarminge closed this

    11. juliusmarminge removed labelstakeover:ready

Sign in to join this conversation.