Skip to content

Commit 1184961

Browse files
committed
fold completed turn work in chat history
1 parent 65f080a commit 1184961

29 files changed

Lines changed: 620 additions & 69 deletions

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,11 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
816816
turnId: event.payload.turnId,
817817
role: event.payload.role,
818818
text: nextText,
819+
...(event.payload.assistantPhase !== undefined
820+
? { assistantPhase: event.payload.assistantPhase }
821+
: previousMessage?.assistantPhase !== undefined
822+
? { assistantPhase: previousMessage.assistantPhase }
823+
: {}),
819824
...(nextAttachments !== undefined ? { attachments: [...nextAttachments] } : {}),
820825
isStreaming: event.payload.streaming,
821826
createdAt: previousMessage?.createdAt ?? event.payload.createdAt,

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
306306
planId: "plan-1",
307307
},
308308
},
309+
turns: [],
309310
createdAt: "2026-02-24T00:00:02.000Z",
310311
updatedAt: "2026-02-24T00:00:03.000Z",
311312
archivedAt: null,
@@ -437,8 +438,26 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
437438

438439
const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
439440
assert.equal(threadDetail._tag, "Some");
440-
if (threadDetail._tag === "Some") {
441-
assert.deepEqual(threadDetail.value, snapshot.threads[0]);
441+
const snapshotThread = snapshot.threads[0];
442+
assert.isDefined(snapshotThread);
443+
if (threadDetail._tag === "Some" && snapshotThread) {
444+
assert.deepEqual(threadDetail.value, {
445+
...snapshotThread,
446+
turns: [
447+
{
448+
turnId: asTurnId("turn-1"),
449+
state: "completed",
450+
requestedAt: "2026-02-24T00:00:08.000Z",
451+
startedAt: "2026-02-24T00:00:08.000Z",
452+
completedAt: "2026-02-24T00:00:08.000Z",
453+
assistantMessageId: asMessageId("message-1"),
454+
sourceProposedPlan: {
455+
threadId: ThreadId.make("thread-1"),
456+
planId: "plan-1",
457+
},
458+
},
459+
],
460+
});
442461
}
443462
}),
444463
);
@@ -1150,6 +1169,10 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
11501169
assert.equal(threadDetail.value.latestTurn?.turnId, asTurnId("turn-running"));
11511170
assert.equal(threadDetail.value.latestTurn?.state, "running");
11521171
assert.equal(threadDetail.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
1172+
assert.deepEqual(
1173+
threadDetail.value.turns?.map((turn) => turn.turnId),
1174+
[asTurnId("turn-completed"), asTurnId("turn-running")],
1175+
);
11531176
}
11541177
}),
11551178
);

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
AssistantMessagePhase,
23
ChatAttachment,
34
CheckpointRef,
45
IsoDateTime,
@@ -70,6 +71,7 @@ const ProjectionProjectDbRowSchema = ProjectionProject.mapFields(
7071
const ProjectionThreadMessageDbRowSchema = ProjectionThreadMessage.mapFields(
7172
Struct.assign({
7273
isStreaming: Schema.Number,
74+
assistantPhase: Schema.NullOr(AssistantMessagePhase),
7375
attachments: Schema.NullOr(Schema.fromJsonString(Schema.Array(ChatAttachment))),
7476
}),
7577
);
@@ -414,6 +416,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
414416
turn_id AS "turnId",
415417
role,
416418
text,
419+
assistant_phase AS "assistantPhase",
417420
attachments_json AS "attachments",
418421
is_streaming AS "isStreaming",
419422
created_at AS "createdAt",
@@ -777,6 +780,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
777780
turn_id AS "turnId",
778781
role,
779782
text,
783+
assistant_phase AS "assistantPhase",
780784
attachments_json AS "attachments",
781785
is_streaming AS "isStreaming",
782786
created_at AS "createdAt",
@@ -877,6 +881,28 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
877881
`,
878882
});
879883

884+
const listTurnRowsByThread = SqlSchema.findAll({
885+
Request: ThreadIdLookupInput,
886+
Result: ProjectionLatestTurnDbRowSchema,
887+
execute: ({ threadId }) =>
888+
sql`
889+
SELECT
890+
thread_id AS "threadId",
891+
turn_id AS "turnId",
892+
state,
893+
requested_at AS "requestedAt",
894+
started_at AS "startedAt",
895+
completed_at AS "completedAt",
896+
assistant_message_id AS "assistantMessageId",
897+
source_proposed_plan_thread_id AS "sourceProposedPlanThreadId",
898+
source_proposed_plan_id AS "sourceProposedPlanId"
899+
FROM projection_turns
900+
WHERE thread_id = ${threadId}
901+
AND turn_id IS NOT NULL
902+
ORDER BY requested_at ASC, turn_id ASC
903+
`,
904+
});
905+
880906
const listCheckpointRowsByThread = SqlSchema.findAll({
881907
Request: ThreadIdLookupInput,
882908
Result: ProjectionCheckpointDbRowSchema,
@@ -1048,6 +1074,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
10481074
id: row.messageId,
10491075
role: row.role,
10501076
text: row.text,
1077+
...(row.assistantPhase !== null ? { assistantPhase: row.assistantPhase } : {}),
10511078
...(row.attachments !== null ? { attachments: row.attachments } : {}),
10521079
turnId: row.turnId,
10531080
streaming: row.isStreaming === 1,
@@ -1903,6 +1930,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
19031930
activityRows,
19041931
checkpointRows,
19051932
latestTurnRow,
1933+
turnRows,
19061934
sessionRow,
19071935
] = yield* Effect.all([
19081936
getActiveThreadRowById({ threadId }).pipe(
@@ -1953,6 +1981,14 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
19531981
),
19541982
),
19551983
),
1984+
listTurnRowsByThread({ threadId }).pipe(
1985+
Effect.mapError(
1986+
toPersistenceSqlOrDecodeError(
1987+
"ProjectionSnapshotQuery.getThreadDetailById:listTurns:query",
1988+
"ProjectionSnapshotQuery.getThreadDetailById:listTurns:decodeRows",
1989+
),
1990+
),
1991+
),
19561992
getThreadSessionRowByThread({ threadId }).pipe(
19571993
Effect.mapError(
19581994
toPersistenceSqlOrDecodeError(
@@ -1977,6 +2013,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
19772013
branch: threadRow.value.branch,
19782014
worktreePath: threadRow.value.worktreePath,
19792015
latestTurn: Option.isSome(latestTurnRow) ? mapLatestTurn(latestTurnRow.value) : null,
2016+
turns: turnRows.map(mapLatestTurn),
19802017
createdAt: threadRow.value.createdAt,
19812018
updatedAt: threadRow.value.updatedAt,
19822019
archivedAt: threadRow.value.archivedAt,
@@ -1986,6 +2023,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
19862023
id: row.messageId,
19872024
role: row.role,
19882025
text: row.text,
2026+
...(row.assistantPhase !== null ? { assistantPhase: row.assistantPhase } : {}),
19892027
turnId: row.turnId,
19902028
streaming: row.isStreaming === 1,
19912029
createdAt: row.createdAt,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ describe("ProviderRuntimeIngestion", () => {
703703
payload: {
704704
itemType: "assistant_message",
705705
status: "completed",
706+
messagePhase: "final_answer",
706707
},
707708
});
708709

@@ -1544,6 +1545,7 @@ describe("ProviderRuntimeIngestion", () => {
15441545
payload: {
15451546
itemType: "assistant_message",
15461547
status: "completed",
1548+
messagePhase: "final_answer",
15471549
},
15481550
});
15491551

@@ -2183,6 +2185,7 @@ describe("ProviderRuntimeIngestion", () => {
21832185
payload: {
21842186
itemType: "assistant_message",
21852187
status: "completed",
2188+
messagePhase: "final_answer",
21862189
},
21872190
});
21882191
harness.emit({
@@ -2223,6 +2226,11 @@ describe("ProviderRuntimeIngestion", () => {
22232226
);
22242227
});
22252228
expect(completionEvents).toHaveLength(1);
2229+
const completionEvent = completionEvents[0];
2230+
expect(completionEvent?.type).toBe("thread.message-sent");
2231+
if (completionEvent?.type === "thread.message-sent") {
2232+
expect(completionEvent.payload.assistantPhase).toBe("final_answer");
2233+
}
22262234
});
22272235

22282236
it("maps canonical request events into approval activities with requestKind", async () => {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ const make = Effect.gen(function* () {
904904
commandTag: string;
905905
finalDeltaCommandTag: string;
906906
fallbackText?: string;
907+
assistantPhase?: "commentary" | "final_answer";
907908
hasProjectedMessage?: boolean;
908909
}) =>
909910
Effect.gen(function* () {
@@ -934,6 +935,7 @@ const make = Effect.gen(function* () {
934935
commandId: yield* providerCommandId(input.event, input.commandTag),
935936
threadId: input.threadId,
936937
messageId: input.messageId,
938+
...(input.assistantPhase ? { assistantPhase: input.assistantPhase } : {}),
937939
...(input.turnId ? { turnId: input.turnId } : {}),
938940
createdAt: input.createdAt,
939941
});
@@ -1425,6 +1427,7 @@ const make = Effect.gen(function* () {
14251427
`assistant:${event.itemId ?? event.turnId ?? event.eventId}`,
14261428
),
14271429
fallbackText: event.payload.detail,
1430+
messagePhase: event.payload.messagePhase,
14281431
}
14291432
: undefined;
14301433
const proposedPlanCompletion =
@@ -1476,6 +1479,9 @@ const make = Effect.gen(function* () {
14761479
...(assistantCompletion.fallbackText !== undefined && shouldApplyFallbackCompletionText
14771480
? { fallbackText: assistantCompletion.fallbackText }
14781481
: {}),
1482+
...(assistantCompletion.messagePhase
1483+
? { assistantPhase: assistantCompletion.messagePhase }
1484+
: {}),
14791485
});
14801486

14811487
if (turnId) {

‎apps/server/src/orchestration/decider.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
646646
messageId: command.messageId,
647647
role: "assistant",
648648
text: "",
649+
...(command.assistantPhase ? { assistantPhase: command.assistantPhase } : {}),
649650
turnId: command.turnId ?? null,
650651
streaming: false,
651652
createdAt: command.createdAt,

‎apps/server/src/orchestration/projector.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ describe("orchestration projector", () => {
8686
branch: null,
8787
worktreePath: null,
8888
latestTurn: null,
89+
turns: [],
8990
createdAt: now,
9091
updatedAt: now,
9192
archivedAt: null,

‎apps/server/src/orchestration/projector.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ export function projectEvent(
260260
branch: payload.branch,
261261
worktreePath: payload.worktreePath,
262262
latestTurn: null,
263+
turns: [],
263264
createdAt: payload.createdAt,
264265
updatedAt: payload.updatedAt,
265266
archivedAt: null,
@@ -376,6 +377,9 @@ export function projectEvent(
376377
id: payload.messageId,
377378
role: payload.role,
378379
text: payload.text,
380+
...(payload.assistantPhase !== undefined
381+
? { assistantPhase: payload.assistantPhase }
382+
: {}),
379383
...(payload.attachments !== undefined ? { attachments: payload.attachments } : {}),
380384
turnId: payload.turnId,
381385
streaming: payload.streaming,
@@ -400,6 +404,9 @@ export function projectEvent(
400404
streaming: message.streaming,
401405
updatedAt: message.updatedAt,
402406
turnId: message.turnId,
407+
...(message.assistantPhase !== undefined
408+
? { assistantPhase: message.assistantPhase }
409+
: {}),
403410
...(message.attachments !== undefined
404411
? { attachments: message.attachments }
405412
: {}),

‎apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,39 @@ layer("ProjectionThreadMessageRepository", (it) => {
111111
assert.deepEqual(rows[0]?.attachments, []);
112112
}),
113113
);
114+
115+
it.effect("preserves an assistant phase when a later streaming upsert omits it", () =>
116+
Effect.gen(function* () {
117+
const repository = yield* ProjectionThreadMessageRepository;
118+
const threadId = ThreadId.make("thread-preserve-assistant-phase");
119+
const messageId = MessageId.make("message-preserve-assistant-phase");
120+
const createdAt = "2026-06-05T00:00:00.000Z";
121+
122+
yield* repository.upsert({
123+
messageId,
124+
threadId,
125+
turnId: null,
126+
role: "assistant",
127+
text: "done",
128+
assistantPhase: "final_answer",
129+
isStreaming: false,
130+
createdAt,
131+
updatedAt: "2026-06-05T00:00:01.000Z",
132+
});
133+
134+
yield* repository.upsert({
135+
messageId,
136+
threadId,
137+
turnId: null,
138+
role: "assistant",
139+
text: "done",
140+
isStreaming: false,
141+
createdAt,
142+
updatedAt: "2026-06-05T00:00:02.000Z",
143+
});
144+
145+
const rows = yield* repository.listByThreadId({ threadId });
146+
assert.equal(rows[0]?.assistantPhase, "final_answer");
147+
}),
148+
);
114149
});

0 commit comments

Comments
 (0)