@devries-cameron20 / t3code Mirror · read-only / commits / b0f12e12e3
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 23:30 UTC

fix(server): PR sync and run-status checks stop reading whole thread histories (#25728)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 10, 2026 at 23:27 UTC b0f12e12e37947629278f62e623442c39eba1a3b
5 files changed +76 -27
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+2 -1
@@ -4676,7 +4676,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4676 );
4677 const settledPayload = (settledAt: string | null, archivedAt: string | null) => sql`
4678 UPDATE orchestration_v2_projection_threads
4679 - SET payload_json = json_set(payload_json, '$.settledAt', ${settledAt}, '$.archivedAt', ${archivedAt})
4679 + SET payload_json = json_set(payload_json, '$.settledAt', ${settledAt}, '$.archivedAt', ${archivedAt}),
4680 + archived_at = ${archivedAt}
4681 WHERE thread_id = ${readThreadId}
4682 `;
4683 yield* settledPayload(DateTime.formatIso(now), null);
apps/server/src/orchestration-v2/ProjectionStore.ts
+21 -1
@@ -678,6 +678,10 @@ export interface ProjectionStoreV2Shape {
678 threadId: ThreadId,
679 runId: RunId,
680 ) => Effect.Effect<OrchestrationV2ConversationMessage | undefined, ProjectionStoreV2Error>;
681 + readonly getRun: (
682 + threadId: ThreadId,
683 + runId: RunId,
684 + ) => Effect.Effect<OrchestrationV2Run | undefined, ProjectionStoreV2Error>;
685 readonly canStartQueuedRun: (
686 threadId: ThreadId,
687 ) => Effect.Effect<boolean, ProjectionStoreV2Error>;
@@ -5588,6 +5592,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5592 )
5593 .pipe(Effect.mapError(controlReadError(threadId)));
5594
5595 + const getRun: ProjectionStoreV2Shape["getRun"] = (threadId, runId) =>
5596 + sql
5597 + .withTransaction(
5598 + Effect.gen(function* () {
5599 + yield* requireThread(threadId);
5600 + const rows = yield* sql<PayloadRow>`
5601 + SELECT payload_json FROM orchestration_v2_projection_runs
5602 + WHERE run_id = ${runId} AND thread_id = ${threadId}`;
5603 + return rows[0] === undefined
5604 + ? undefined
5605 + : yield* decodeRunPayload(rows[0].payload_json);
5606 + }),
5607 + )
5608 + .pipe(Effect.mapError(controlReadError(threadId)));
5609 +
5610 const canStartQueuedRun: ProjectionStoreV2Shape["canStartQueuedRun"] = (threadId) =>
5611 sql
5612 .withTransaction(
@@ -7218,7 +7237,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7237 SELECT t.payload_json FROM orchestration_v2_projection_threads t
7238 WHERE t.deleted_at IS NULL${
7239 threadId === undefined
7221 - ? sql` AND json_extract(t.payload_json, '$.archivedAt') IS NULL`
7240 + ? sql` AND t.archived_at IS NULL`
7241 : sql` AND t.thread_id = ${threadId}`
7242 }${
7243 unsettledOnly
@@ -7639,6 +7658,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7658 getCheckpointCaptureContext: readOnReader(getCheckpointCaptureContext),
7659 getCheckpointRollbackContext: readOnReader(getCheckpointRollbackContext),
7660 getRunMessage: readOnReader(getRunMessage),
7661 + getRun: readOnReader(getRun),
7662 canStartQueuedRun: readOnReader(canStartQueuedRun),
7663 getHeldQueuedRuns: readOnReader(getHeldQueuedRuns),
7664 getPendingNativeUserInputs: readOnReader(getPendingNativeUserInputs),
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+1
@@ -268,6 +268,7 @@ it.effect(
268 getCheckpointRollbackContext: () => Effect.die("unused rollback context"),
269 getCheckpointCaptureContext: () => Effect.die("not used"),
270 getRunMessage: () => Effect.die("not used"),
271 + getRun: () => Effect.die("not used"),
272 isNodeStored: () => Effect.die("not used"),
273 canStartQueuedRun: () => Effect.die("not used"),
274 getHeldQueuedRuns: () => Effect.die("not used"),
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+42 -9
@@ -919,6 +919,18 @@ function makeLocalCommandHarness(input: {
919 ),
920 }),
921 );
922 + const getRuntimeRecoveryProjection = vi.fn<
923 + ProjectionStore.ProjectionStoreV2Shape["getRuntimeRecoveryProjection"]
924 + >(() =>
925 + Effect.as(failReadIfRunning, {
926 + ...projection,
927 + hasConversation: projection.messages.some(
928 + (m) =>
929 + m.role === "user" &&
930 + (m.text.trim().toLowerCase() !== "/compact" || m.attachments.length > 0),
931 + ),
932 + }),
933 + );
934 const layer = ProviderTurnStart.layer.pipe(
935 Layer.provide(
936 Layer.mergeAll(
@@ -968,15 +980,12 @@ function makeLocalCommandHarness(input: {
980 Layer.mock(ProjectionStore.ProjectionStoreV2)({
981 getThreadRecords,
982 getTurnStartContext,
971 - getRuntimeRecoveryProjection: () =>
972 - Effect.as(failReadIfRunning, {
973 - ...projection,
974 - hasConversation: projection.messages.some(
975 - (m) =>
976 - m.role === "user" &&
977 - (m.text.trim().toLowerCase() !== "/compact" || m.attachments.length > 0),
978 - ),
979 - }),
983 + getRuntimeRecoveryProjection,
984 + getRun: (_threadId, id) =>
985 + Effect.as(
986 + failReadIfRunning,
987 + projection.runs.find((candidate) => candidate.id === id),
988 + ),
989 getTurnStartHistory: () =>
990 input.sideChat !== undefined
991 ? Effect.succeed([])
@@ -1017,6 +1026,7 @@ function makeLocalCommandHarness(input: {
1026 settledRuns,
1027 getThreadRecords,
1028 getTurnStartContext,
1029 + getRuntimeRecoveryProjection,
1030 setProjection: (next: OrchestrationV2ThreadProjection) => {
1031 projection = next;
1032 },
@@ -1764,6 +1774,29 @@ effectIt.effect("does not overwrite a run interrupted while its provider session
1774 }),
1775 );
1776
1777 +effectIt.effect("checks the run's status without loading the thread's history", () =>
1778 + Effect.gen(function* () {
1779 + const harness = makeLocalCommandHarness({
1780 + text: "Continue",
1781 + previousTurn: { runStatus: "completed", turnStatus: "completed" },
1782 + });
1783 +
1784 + yield* harness.start;
1785 +
1786 + const controls = harness.startRootRun.mock.calls[0]?.[0];
1787 + expect(controls).toBeDefined();
1788 + if (controls === undefined) return;
1789 + expect(yield* controls.shouldStartProviderTurn!()).toBe(true);
1790 + expect(yield* controls.shouldFinalizeRun!()).toBe(true);
1791 + // Each check needs one run row; the recovery projection reads every run and open turn item.
1792 + // The one read left is the start's inherited background items snapshot, not a status check.
1793 + expect(
1794 + harness.getRuntimeRecoveryProjection.mock.calls.length,
1795 + "recovery projection reads beyond the inherited background items snapshot",
1796 + ).toBe(1);
1797 + }),
1798 +);
1799 +
1800 effectIt.effect("fails the run instead of replacing a native thread another client holds", () =>
1801 Effect.gen(function* () {
1802 const detail = "This conversation is open in another Codex client.";
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+10 -16
@@ -237,13 +237,16 @@ export const layer: Layer.Layer<
237 typeof RunExecutionService.selectInheritedBackgroundTurnItems
238 >;
239 }) => {
240 + const isCurrentAttemptIn = (statuses: ReadonlyArray<OrchestrationV2Run["status"]>) =>
241 + projectionStore
242 + .getRun(input.threadId, input.runId)
243 + .pipe(
244 + Effect.map(
245 + (run) => run?.activeAttemptId === input.attemptId && statuses.includes(run.status),
246 + ),
247 + );
248 const isCurrentAttemptInStatus = (expectedStatus: OrchestrationV2Run["status"]) =>
241 - projectionStore.getRuntimeRecoveryProjection(input.threadId).pipe(
242 - Effect.map((current) => {
243 - const run = current.runs.find((candidate) => candidate.id === input.runId);
244 - return run?.activeAttemptId === input.attemptId && run.status === expectedStatus;
245 - }),
246 - );
249 + isCurrentAttemptIn([expectedStatus]);
250 return {
251 isCurrentAttemptInStatus,
252 loadInheritedBackgroundTurnItems: () =>
@@ -260,16 +263,7 @@ export const layer: Layer.Layer<
263 Effect.catchCause(() => Effect.succeed(input.inheritedBackgroundTurnItems)),
264 ),
265 shouldStartProviderTurn: () => isCurrentAttemptInStatus("running"),
263 - shouldFinalizeRun: () =>
264 - projectionStore.getRuntimeRecoveryProjection(input.threadId).pipe(
265 - Effect.map((current) => {
266 - const run = current.runs.find((candidate) => candidate.id === input.runId);
267 - return (
268 - run?.activeAttemptId === input.attemptId &&
269 - (run.status === "starting" || run.status === "running")
270 - );
271 - }),
272 - ),
266 + shouldFinalizeRun: () => isCurrentAttemptIn(["starting", "running"]),
267 hasUnpairedRunInterruptRequest: () =>
268 projectionStore
269 .hasUnpairedRunInterruptRequest(