fix(server): preserve historical latest turn
Fixes https://github.com/pingdotgg/t3code/issues/4399
Summary
- Keep session active-turn state and thread historical latest-turn state as separate concepts.
- Preserve projection_threads.latest_turn_id when thread.session-set carries a null activeTurnId.
- Continue replacing the historical pointer when a new non-null active turn arrives.
Why it was intermittent
ProjectionSnapshotQuery joins the latest turn through projection_threads.latest_turn_id. A terminal session event could clear that pointer, while a later thread.turn-diff-completed event restored it. Event ordering therefore determined whether shell snapshots retained the completed turn.
Commit 501ce27b81826960dcbb9a93a6b3407efcf09366 correctly clears stale session active-turn state for inactive sessions and should remain. This fix changes only the thread projection so historical latest-turn state survives that correct session transition.
Tests
- Added a running-to-ready regression with no later diff/checkpoint event, asserting null session activity, retained latest_turn_id, and a completed shell latestTurn with completedAt.
- Covered idle, stopped, interrupted, error, and starting states with null active turns.
- Covered new running turns replacing the pointer and threads with no prior turn remaining null.
Commands run:
- vp test run apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
- vp fmt --check apps/server/src/orchestration/Layers/ProjectionPipeline.ts apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
- vp lint --report-unused-disable-directives apps/server/src/orchestration/Layers/ProjectionPipeline.ts apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
- vp run --filter t3 typecheck
[!NOTE]
Preserve
latestTurnIdon threads whenthread.session-sethas no active turnWhen a
thread.session-setevent carries a nullactiveTurnId(e.g. on session transitions to idle, stopped, or ready), the projection previously clearedthreads.latestTurnId. The fix in ProjectionPipeline.ts usesactiveTurnId ?? existingRow.value.latestTurnIdso the last known turn is retained. New tests in ProjectionPipeline.test.ts cover completed-turn preservation, no-active-turn session statuses, and sequential turn replacement.Macroscope summarized 0e3f702.
[!IMPORTANT]
Review skipped
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
c8215ac6-524a-4d08-a48a-5a6605225f00You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
- [ ] 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
Approvability
Verdict: Approved
Single-line bug fix using null coalescing to preserve latestTurnId when activeTurnId is null, with comprehensive test coverage (288 lines of tests). Clear intent, limited scope, and obvious resolution.
You can customize Macroscope's approvability policy. Learn more.
Closing in favor of #2829 (orchestration V2).
#2829 deletes the V1 orchestration layer this PR builds on — apps/server/src/orchestration/**, provider/Layers/*Adapter.ts and provider/Services/** are removed and replaced by apps/server/src/orchestration-v2/**, with the IPC surface renamed to ORCHESTRATION_V2_WS_METHODS. The files this PR touches either no longer exist or are rewritten, so it can't be rebased — it would need reimplementing against the V2 adapters.
This is not a judgement on the change itself. Several of these are real gaps we still want fixed; the base just moved out from under them.
Once #2829 merges, please rebase onto main, port the change to the V2 equivalent, and reopen (or open a fresh PR). Ping me and I'll prioritise the review.