Skip to content

Commit fa28310

Browse files
Handle running turn snapshots by latest turn id
- Query latest thread turns via projection_threads.latest_turn_id - Keep local dispatch active until the running turn is acknowledged - Use session activeTurnId when deriving active work start time
1 parent 62d9c35 commit fa28310

6 files changed

Lines changed: 338 additions & 25 deletions

File tree

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,4 +878,147 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
878878
]);
879879
}),
880880
);
881+
882+
it.effect("uses projection_threads.latest_turn_id for targeted thread latest turn queries", () =>
883+
Effect.gen(function* () {
884+
const snapshotQuery = yield* ProjectionSnapshotQuery;
885+
const sql = yield* SqlClient.SqlClient;
886+
887+
yield* sql`DELETE FROM projection_projects`;
888+
yield* sql`DELETE FROM projection_threads`;
889+
yield* sql`DELETE FROM projection_turns`;
890+
891+
yield* sql`
892+
INSERT INTO projection_projects (
893+
project_id,
894+
title,
895+
workspace_root,
896+
default_model_selection_json,
897+
scripts_json,
898+
created_at,
899+
updated_at,
900+
deleted_at
901+
)
902+
VALUES (
903+
'project-1',
904+
'Project 1',
905+
'/tmp/project-1',
906+
'{"provider":"codex","model":"gpt-5-codex"}',
907+
'[]',
908+
'2026-04-02T00:00:00.000Z',
909+
'2026-04-02T00:00:01.000Z',
910+
NULL
911+
)
912+
`;
913+
914+
yield* sql`
915+
INSERT INTO projection_threads (
916+
thread_id,
917+
project_id,
918+
title,
919+
model_selection_json,
920+
runtime_mode,
921+
interaction_mode,
922+
branch,
923+
worktree_path,
924+
latest_turn_id,
925+
latest_user_message_at,
926+
pending_approval_count,
927+
pending_user_input_count,
928+
has_actionable_proposed_plan,
929+
created_at,
930+
updated_at,
931+
archived_at,
932+
deleted_at
933+
)
934+
VALUES (
935+
'thread-1',
936+
'project-1',
937+
'Thread 1',
938+
'{"provider":"codex","model":"gpt-5-codex"}',
939+
'full-access',
940+
'default',
941+
NULL,
942+
NULL,
943+
'turn-running',
944+
'2026-04-02T00:00:04.000Z',
945+
0,
946+
0,
947+
0,
948+
'2026-04-02T00:00:02.000Z',
949+
'2026-04-02T00:00:03.000Z',
950+
NULL,
951+
NULL
952+
)
953+
`;
954+
955+
yield* sql`
956+
INSERT INTO projection_turns (
957+
thread_id,
958+
turn_id,
959+
pending_message_id,
960+
source_proposed_plan_thread_id,
961+
source_proposed_plan_id,
962+
assistant_message_id,
963+
state,
964+
requested_at,
965+
started_at,
966+
completed_at,
967+
checkpoint_turn_count,
968+
checkpoint_ref,
969+
checkpoint_status,
970+
checkpoint_files_json
971+
)
972+
VALUES
973+
(
974+
'thread-1',
975+
'turn-completed',
976+
'message-user-1',
977+
NULL,
978+
NULL,
979+
'message-assistant-1',
980+
'completed',
981+
'2026-04-02T00:00:05.000Z',
982+
'2026-04-02T00:00:06.000Z',
983+
'2026-04-02T00:00:20.000Z',
984+
5,
985+
'checkpoint-5',
986+
'ready',
987+
'[]'
988+
),
989+
(
990+
'thread-1',
991+
'turn-running',
992+
'message-user-2',
993+
NULL,
994+
NULL,
995+
NULL,
996+
'running',
997+
'2026-04-02T00:00:30.000Z',
998+
'2026-04-02T00:00:30.000Z',
999+
NULL,
1000+
NULL,
1001+
NULL,
1002+
NULL,
1003+
'[]'
1004+
)
1005+
`;
1006+
1007+
const threadShell = yield* snapshotQuery.getThreadShellById(ThreadId.make("thread-1"));
1008+
assert.equal(threadShell._tag, "Some");
1009+
if (threadShell._tag === "Some") {
1010+
assert.equal(threadShell.value.latestTurn?.turnId, asTurnId("turn-running"));
1011+
assert.equal(threadShell.value.latestTurn?.state, "running");
1012+
assert.equal(threadShell.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
1013+
}
1014+
1015+
const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
1016+
assert.equal(threadDetail._tag, "Some");
1017+
if (threadDetail._tag === "Some") {
1018+
assert.equal(threadDetail.value.latestTurn?.turnId, asTurnId("turn-running"));
1019+
assert.equal(threadDetail.value.latestTurn?.state, "running");
1020+
assert.equal(threadDetail.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
1021+
}
1022+
}),
1023+
);
8811024
});

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -624,22 +624,21 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
624624
execute: ({ threadId }) =>
625625
sql`
626626
SELECT
627-
thread_id AS "threadId",
628-
turn_id AS "turnId",
629-
state,
630-
requested_at AS "requestedAt",
631-
started_at AS "startedAt",
632-
completed_at AS "completedAt",
633-
assistant_message_id AS "assistantMessageId",
634-
source_proposed_plan_thread_id AS "sourceProposedPlanThreadId",
635-
source_proposed_plan_id AS "sourceProposedPlanId"
636-
FROM projection_turns
637-
WHERE thread_id = ${threadId}
638-
AND turn_id IS NOT NULL
639-
ORDER BY
640-
COALESCE(checkpoint_turn_count, -1) DESC,
641-
COALESCE(completed_at, started_at, requested_at) DESC,
642-
row_id DESC
627+
turns.thread_id AS "threadId",
628+
turns.turn_id AS "turnId",
629+
turns.state,
630+
turns.requested_at AS "requestedAt",
631+
turns.started_at AS "startedAt",
632+
turns.completed_at AS "completedAt",
633+
turns.assistant_message_id AS "assistantMessageId",
634+
turns.source_proposed_plan_thread_id AS "sourceProposedPlanThreadId",
635+
turns.source_proposed_plan_id AS "sourceProposedPlanId"
636+
FROM projection_threads threads
637+
JOIN projection_turns turns
638+
ON turns.thread_id = threads.thread_id
639+
AND turns.turn_id = threads.latest_turn_id
640+
WHERE threads.thread_id = ${threadId}
641+
AND threads.deleted_at IS NULL
643642
LIMIT 1
644643
`,
645644
});

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,142 @@ describe("hasServerAcknowledgedLocalDispatch", () => {
530530
).toBe(true);
531531
});
532532

533+
it("does not clear local dispatch while the session is running a newer turn than latestTurn", () => {
534+
const localDispatch = createLocalDispatchSnapshot({
535+
id: ThreadId.make("thread-1"),
536+
environmentId: localEnvironmentId,
537+
codexThreadId: null,
538+
projectId,
539+
title: "Thread",
540+
modelSelection: { provider: "codex", model: "gpt-5.4" },
541+
runtimeMode: "full-access",
542+
interactionMode: "default",
543+
session: previousSession,
544+
messages: [],
545+
proposedPlans: [],
546+
error: null,
547+
createdAt: "2026-03-29T00:00:00.000Z",
548+
archivedAt: null,
549+
updatedAt: "2026-03-29T00:00:10.000Z",
550+
latestTurn: previousLatestTurn,
551+
branch: null,
552+
worktreePath: null,
553+
turnDiffSummaries: [],
554+
activities: [],
555+
});
556+
557+
expect(
558+
hasServerAcknowledgedLocalDispatch({
559+
localDispatch,
560+
phase: "running",
561+
latestTurn: previousLatestTurn,
562+
session: {
563+
...previousSession,
564+
status: "running",
565+
orchestrationStatus: "running",
566+
activeTurnId: TurnId.make("turn-2"),
567+
updatedAt: "2026-03-29T00:01:00.000Z",
568+
},
569+
hasPendingApproval: false,
570+
hasPendingUserInput: false,
571+
threadError: null,
572+
}),
573+
).toBe(false);
574+
});
575+
576+
it("does not clear local dispatch while the session is running but latestTurn has not advanced yet", () => {
577+
const localDispatch = createLocalDispatchSnapshot({
578+
id: ThreadId.make("thread-1"),
579+
environmentId: localEnvironmentId,
580+
codexThreadId: null,
581+
projectId,
582+
title: "Thread",
583+
modelSelection: { provider: "codex", model: "gpt-5.4" },
584+
runtimeMode: "full-access",
585+
interactionMode: "default",
586+
session: previousSession,
587+
messages: [],
588+
proposedPlans: [],
589+
error: null,
590+
createdAt: "2026-03-29T00:00:00.000Z",
591+
archivedAt: null,
592+
updatedAt: "2026-03-29T00:00:10.000Z",
593+
latestTurn: previousLatestTurn,
594+
branch: null,
595+
worktreePath: null,
596+
turnDiffSummaries: [],
597+
activities: [],
598+
});
599+
600+
expect(
601+
hasServerAcknowledgedLocalDispatch({
602+
localDispatch,
603+
phase: "running",
604+
latestTurn: previousLatestTurn,
605+
session: {
606+
...previousSession,
607+
status: "running",
608+
orchestrationStatus: "running",
609+
activeTurnId: undefined,
610+
updatedAt: "2026-03-29T00:01:00.000Z",
611+
},
612+
hasPendingApproval: false,
613+
hasPendingUserInput: false,
614+
threadError: null,
615+
}),
616+
).toBe(false);
617+
});
618+
619+
it("clears local dispatch once the running latestTurn matches the active session turn", () => {
620+
const localDispatch = createLocalDispatchSnapshot({
621+
id: ThreadId.make("thread-1"),
622+
environmentId: localEnvironmentId,
623+
codexThreadId: null,
624+
projectId,
625+
title: "Thread",
626+
modelSelection: { provider: "codex", model: "gpt-5.4" },
627+
runtimeMode: "full-access",
628+
interactionMode: "default",
629+
session: previousSession,
630+
messages: [],
631+
proposedPlans: [],
632+
error: null,
633+
createdAt: "2026-03-29T00:00:00.000Z",
634+
archivedAt: null,
635+
updatedAt: "2026-03-29T00:00:10.000Z",
636+
latestTurn: previousLatestTurn,
637+
branch: null,
638+
worktreePath: null,
639+
turnDiffSummaries: [],
640+
activities: [],
641+
});
642+
643+
expect(
644+
hasServerAcknowledgedLocalDispatch({
645+
localDispatch,
646+
phase: "running",
647+
latestTurn: {
648+
...previousLatestTurn,
649+
turnId: TurnId.make("turn-2"),
650+
state: "running",
651+
requestedAt: "2026-03-29T00:01:00.000Z",
652+
startedAt: "2026-03-29T00:01:01.000Z",
653+
completedAt: null,
654+
},
655+
session: {
656+
...previousSession,
657+
status: "running",
658+
orchestrationStatus: "running",
659+
activeTurnId: TurnId.make("turn-2"),
660+
updatedAt: "2026-03-29T00:01:01.000Z",
661+
},
662+
hasPendingApproval: false,
663+
hasPendingUserInput: false,
664+
threadError: null,
665+
}),
666+
).toBe(true);
667+
});
668+
533669
it("clears local dispatch when the session changes without an observed running phase", () => {
534670
const localDispatch = createLocalDispatchSnapshot({
535671
id: ThreadId.make("thread-1"),

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,23 +322,37 @@ export function hasServerAcknowledgedLocalDispatch(input: {
322322
if (!input.localDispatch) {
323323
return false;
324324
}
325-
if (
326-
input.phase === "running" ||
327-
input.hasPendingApproval ||
328-
input.hasPendingUserInput ||
329-
Boolean(input.threadError)
330-
) {
325+
if (input.hasPendingApproval || input.hasPendingUserInput || Boolean(input.threadError)) {
331326
return true;
332327
}
333328

334329
const latestTurn = input.latestTurn ?? null;
335330
const session = input.session ?? null;
336-
337-
return (
331+
const latestTurnChanged =
338332
input.localDispatch.latestTurnTurnId !== (latestTurn?.turnId ?? null) ||
339333
input.localDispatch.latestTurnRequestedAt !== (latestTurn?.requestedAt ?? null) ||
340334
input.localDispatch.latestTurnStartedAt !== (latestTurn?.startedAt ?? null) ||
341-
input.localDispatch.latestTurnCompletedAt !== (latestTurn?.completedAt ?? null) ||
335+
input.localDispatch.latestTurnCompletedAt !== (latestTurn?.completedAt ?? null);
336+
337+
if (input.phase === "running") {
338+
if (!latestTurnChanged) {
339+
return false;
340+
}
341+
if (latestTurn?.startedAt === null || latestTurn === null) {
342+
return false;
343+
}
344+
if (
345+
session?.activeTurnId !== undefined &&
346+
session.activeTurnId !== null &&
347+
latestTurn?.turnId !== session.activeTurnId
348+
) {
349+
return false;
350+
}
351+
return true;
352+
}
353+
354+
return (
355+
latestTurnChanged ||
342356
input.localDispatch.sessionOrchestrationStatus !== (session?.orchestrationStatus ?? null) ||
343357
input.localDispatch.sessionUpdatedAt !== (session?.updatedAt ?? null)
344358
);

‎apps/web/src/session-logic.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,19 @@ describe("deriveActiveWorkStartedAt", () => {
12271227
).toBe("2026-02-27T21:10:00.000Z");
12281228
});
12291229

1230+
it("uses the new send start while the session is running a different turn", () => {
1231+
expect(
1232+
deriveActiveWorkStartedAt(
1233+
latestTurn,
1234+
{
1235+
orchestrationStatus: "running",
1236+
activeTurnId: TurnId.make("turn-2"),
1237+
},
1238+
"2026-02-27T21:11:00.000Z",
1239+
),
1240+
).toBe("2026-02-27T21:11:00.000Z");
1241+
});
1242+
12301243
it("falls back to sendStartedAt once the latest turn is settled", () => {
12311244
expect(
12321245
deriveActiveWorkStartedAt(

0 commit comments

Comments
 (0)