The best way to control Claude Code, Codex and more from any computer or phone
fix(server): subagents from earlier turns keep their model after loading history (#38724)
Julius Marminge committed
Nov 18, 2026 at 12:27 UTC
c2a7c5cbc3599f6b34873b49561250e9bae26bdf
7 files changed
+136
-13
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+65
@@ -4318,6 +4318,71 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4318
WHERE thread_id = ${threadId} AND ordinal IN (500, 580)
4319
`;
4320
4321
+ // A settled subagent of an old run is outside the window; its page carries its record.
4322
+ const subagentId = NodeId.make("node:bounded-sql-history:subagent");
4323
+ yield* sql`
4324
+ UPDATE orchestration_v2_projection_turn_items
4325
+ SET type = 'subagent', payload_json = json_set(payload_json,
4326
+ '$.type', 'subagent', '$.subagentId', ${subagentId}, '$.origin', 'provider_native',
4327
+ '$.driver', ${driver}, '$.providerInstanceId', ${providerInstanceId},
4328
+ '$.childThreadId', json('null'), '$.prompt', 'Look', '$.result', 'Done')
4329
+ WHERE thread_id = ${threadId} AND ordinal = 580
4330
+ `;
4331
+ yield* projectionStore.apply({
4332
+ id: EventId.make("event:bounded-sql-history:subagent"),
4333
+ type: "subagent.updated",
4334
+ threadId,
4335
+ nodeId: subagentId,
4336
+ driver,
4337
+ providerInstanceId,
4338
+ occurredAt: now,
4339
+ payload: {
4340
+ id: subagentId,
4341
+ threadId,
4342
+ runId: RunId.make("run:bounded-sql-history:580"),
4343
+ parentNodeId: NodeId.make("node:bounded-sql-history:580"),
4344
+ origin: "provider_native",
4345
+ createdBy: "agent",
4346
+ driver,
4347
+ providerInstanceId,
4348
+ providerThreadId: null,
4349
+ childThreadId: null,
4350
+ nativeTaskRef: null,
4351
+ prompt: "Look",
4352
+ title: "Old child",
4353
+ model: "claude-haiku-4-5-20251001",
4354
+ status: "completed",
4355
+ result: "Done",
4356
+ startedAt: now,
4357
+ completedAt: now,
4358
+ updatedAt: now,
4359
+ },
4360
+ });
4361
+ assert.deepEqual(
4362
+ (yield* projectionStore.getThreadSnapshotWindow(threadId, { rowLimit: 76 })).projection
4363
+ .subagents,
4364
+ [],
4365
+ );
4366
+ const subagentPage = yield* historyPage(
4367
+ threadId,
4368
+ encodeThreadHistoryCursor({
4369
+ snapshotSequence: 0,
4370
+ sourceThreadId: threadId,
4371
+ sourceItemId: TurnItemId.make("turn-item:bounded-sql-history:590"),
4372
+ position: 589,
4373
+ }),
4374
+ );
4375
+ assert.deepEqual(
4376
+ subagentPage.subagents?.map((agent) => [agent.id, agent.model]),
4377
+ [[subagentId, "claude-haiku-4-5-20251001"]],
4378
+ );
4379
+ yield* sql`DELETE FROM orchestration_v2_projection_subagents WHERE subagent_id = ${subagentId}`;
4380
+ yield* sql`
4381
+ UPDATE orchestration_v2_projection_turn_items
4382
+ SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')
4383
+ WHERE thread_id = ${threadId} AND ordinal = 580
4384
+ `;
4385
+
4386
// Older pages and timeline pages read the rows near the page, not the whole history. A
4387
// full index that another read left cached gives the same page.
4388
const pageStatements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
apps/server/src/orchestration-v2/ProjectionStore.ts
+18
-3
@@ -7519,13 +7519,28 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7519
...row,
7520
position,
7521
}));
7522
+ const rows = conversationOnly
7523
+ ? pageItems.filter((row) => isConversationHistoryItem(row.item))
7524
+ : pageItems;
7525
+ // A bounded snapshot keeps only recent or active subagents.
7526
+ const subagentIds = rows.flatMap(({ sourceThreadId, item }) =>
7527
+ item.type === "subagent" && sourceThreadId === threadId ? [item.subagentId] : [],
7528
+ );
7529
+ const subagents =
7530
+ subagentIds.length === 0
7531
+ ? []
7532
+ : yield* sql<PayloadRow>`
7533
+ SELECT payload_json FROM orchestration_v2_projection_subagents
7534
+ WHERE thread_id = ${threadId}
7535
+ AND subagent_id IN (SELECT value FROM json_each(${encodeIdList(subagentIds)}))
7536
+ ORDER BY COALESCE(started_at, ''), subagent_id ASC
7537
+ `.pipe(Effect.flatMap(decodeRows(decodeSubagentPayload, threadId)));
7538
return {
7539
snapshotSequence,
7524
- items: conversationOnly
7525
- ? pageItems.filter((row) => isConversationHistoryItem(row.item))
7526
- : pageItems,
7540
+ items: rows,
7541
nextCursor: bounded.hasMoreHistory ? bounded.nextCursor : page.nextCursor,
7542
hasMoreHistory: bounded.hasMoreHistory || page.hasMoreHistory,
7543
+ ...(subagents.length === 0 ? {} : { subagents }),
7544
};
7545
}),
7546
)
apps/web/src/components/guest/guestStream.logic.ts
+8
-2
@@ -18,7 +18,10 @@ import { msg } from "gt-react";
18
19
import { m, type MessageResolver } from "~/i18n/messages";
20
import { applyOrchestrationV2ProjectionEvent } from "@t3tools/client-runtime/state/orchestration-v2-projection";
21
-import { mergeOlderHistoryIntoProjection } from "@t3tools/client-runtime/state/thread-history-merge";
21
+import {
22
+ mergeOlderHistoryIntoProjection,
23
+ mergeOlderSubagentsIntoProjection,
24
+} from "@t3tools/client-runtime/state/thread-history-merge";
25
import { boundedSnapshotProjection } from "@t3tools/shared/orchestrationV2BoundedSnapshot";
26
import * as Option from "effect/Option";
27
import * as Schema from "effect/Schema";
@@ -107,7 +110,10 @@ export function applyGuestStreamEvent(
110
if (state.projection === null || state.historyCursor !== event.cursor) return state;
111
return {
112
...state,
110
- projection: mergeOlderHistoryIntoProjection(state.projection, event.page.items),
113
+ projection: mergeOlderSubagentsIntoProjection(
114
+ mergeOlderHistoryIntoProjection(state.projection, event.page.items),
115
+ event.page.subagents,
116
+ ),
117
awaitingRowIds: withoutPlaced(
118
state.awaitingRowIds,
119
event.page.items.map((row) => row.item),
packages/client-runtime/src/state/threadHistoryMerge.test.ts
+21
-2
@@ -1,9 +1,12 @@
1
-import type { OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts";
1
+import type { OrchestrationV2ProjectedTurnItem, OrchestrationV2Subagent } from "@t3tools/contracts";
2
import { describe, expect, it } from "@effect/vitest";
3
import * as DateTime from "effect/DateTime";
4
5
import { v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
6
-import { mergeOlderHistoryIntoProjection } from "./threadHistoryMerge.ts";
6
+import {
7
+ mergeOlderHistoryIntoProjection,
8
+ mergeOlderSubagentsIntoProjection,
9
+} from "./threadHistoryMerge.ts";
10
import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
11
import { derivePendingThreadRequests } from "./threadRequests.ts";
12
@@ -132,6 +135,22 @@ describe("threadHistoryMerge", () => {
135
]);
136
});
137
138
+ it("adds older subagent records and keeps the newer record of one already held", () => {
139
+ const subagent = (id: string, model: string) =>
140
+ ({ id, model, status: "completed" }) as unknown as OrchestrationV2Subagent;
141
+ const live = subagent("agent-live", "claude-sonnet-live");
142
+ const projection = { ...v2Projection, subagents: [live] };
143
+ const merged = mergeOlderSubagentsIntoProjection(projection, [
144
+ subagent("agent-old", "claude-haiku-4-5-20251001"),
145
+ subagent("agent-live", "claude-sonnet-stale"),
146
+ ]);
147
+ expect(merged.subagents.map((agent) => [agent.id, agent.model])).toEqual([
148
+ ["agent-old", "claude-haiku-4-5-20251001"],
149
+ ["agent-live", "claude-sonnet-live"],
150
+ ]);
151
+ expect(mergeOlderSubagentsIntoProjection(projection, undefined)).toBe(projection);
152
+ });
153
+
154
it("fills inherited activity without confusing identical item IDs from different source threads", () => {
155
const inherited = (index: number) => ({
156
...row(index),
packages/client-runtime/src/state/threadHistoryMerge.ts
+12
@@ -1,5 +1,6 @@
1
import type {
2
OrchestrationV2ProjectedTurnItem,
3
+ OrchestrationV2Subagent,
4
OrchestrationV2ThreadProjection,
5
} from "@t3tools/contracts";
6
import { isOrchestrationV2TurnItemVisible } from "@t3tools/shared/orchestrationV2Timeline";
@@ -196,6 +197,17 @@ export function mergeOlderHistoryIntoProjection(
197
};
198
}
199
200
+/** Adds the records of older subagents; a record already held is newer and stays. */
201
+export function mergeOlderSubagentsIntoProjection(
202
+ projection: OrchestrationV2ThreadProjection,
203
+ olderSubagents: ReadonlyArray<OrchestrationV2Subagent> | undefined,
204
+): OrchestrationV2ThreadProjection {
205
+ const held = new Set(projection.subagents.map((agent) => agent.id));
206
+ const added = olderSubagents?.filter((agent) => !held.has(agent.id)) ?? [];
207
+ if (added.length === 0) return projection;
208
+ return { ...projection, subagents: [...added, ...projection.subagents] };
209
+}
210
+
211
export function applyHistoryPageMeta(
212
current: ThreadHistoryMeta,
213
page: {
packages/client-runtime/src/state/threads.ts
+10
-6
@@ -52,6 +52,7 @@ import {
52
EMPTY_THREAD_HISTORY_META,
53
isActiveHistoryRequestCursor,
54
mergeOlderHistoryIntoProjection,
55
+ mergeOlderSubagentsIntoProjection,
56
type ThreadHistoryMeta,
57
} from "./threadHistoryMerge.ts";
58
import * as ThreadSnapshotLoader from "./threadSnapshotHttp.ts";
@@ -840,12 +841,15 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
841
{
842
...latest,
843
data: Option.some(
843
- mergeOlderHistoryIntoProjection(
844
- latest.data.value,
845
- page.items,
846
- workAnchorId === undefined
847
- ? undefined
848
- : { anchorId: workAnchorId, nextCursor: page.nextCursor },
844
+ mergeOlderSubagentsIntoProjection(
845
+ mergeOlderHistoryIntoProjection(
846
+ latest.data.value,
847
+ page.items,
848
+ workAnchorId === undefined
849
+ ? undefined
850
+ : { anchorId: workAnchorId, nextCursor: page.nextCursor },
851
+ ),
852
+ page.subagents,
853
),
854
),
855
status:
packages/contracts/src/orchestrationV2.ts
+2
@@ -4142,6 +4142,8 @@ export const OrchestrationV2ThreadHistoryPage = withRecordIdTable(
4142
),
4143
nextCursor: Schema.NullOr(TrimmedNonEmptyString),
4144
hasMoreHistory: Schema.Boolean,
4145
+ /** Records of the page's subagent items, which a bounded snapshot can omit. */
4146
+ subagents: Schema.optionalKey(Schema.Array(OrchestrationV2Subagent)),
4147
idTable: IdTableMarker,
4148
}),
4149
() => null,