The best way to control Claude Code, Codex and more from any computer or phone
perf(server): sending to a long compacted thread skips its old prompts (#36289)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Bráulio Oliveira committed
Nov 19, 2026 at 03:14 UTC
94245bc8ecc04d748e079f54839e808d8bfbc8be
5 files changed
+218
-35
apps/server/src/orchestration-v2/Orchestrator.ts
+4
-4
@@ -6856,9 +6856,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
6856
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
6857
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
6858
) {
6859
- // A compacted thread reads every run and turn item, but attempts, nodes, messages and provider
6860
- // turns only for its recent runs and the runs this command names. An idle compaction reads
6861
- // in full: its busy check below looks for a delegated result's message under any old run.
6859
+ // A compacted thread reads every run, but attempts, nodes, messages, provider turns and prompts
6860
+ // only for its recent runs and the runs this command names. An idle compaction reads in full:
6861
+ // its busy check below looks for a delegated result's message under any old run.
6862
let projection = yield* getProjectionWithPendingEvents(
6863
command.threadId,
6864
events,
@@ -7130,7 +7130,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
7130
}
7131
const userMessages = projection.messages.filter((message) => message.role === "user");
7132
// The recent runs of a compacted thread can hold only a `/compact` while an older prompt
7133
- // exists. Every prompt's turn item is loaded; without one, every user message is checked.
7133
+ // exists, so when every loaded prompt is one, every user message is checked.
7134
const onlyMaintenanceHistory = [
7135
...userMessages,
7136
...projection.turnItems.flatMap((item) => (item.type === "user_message" ? [item] : [])),
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+94
-11
@@ -859,7 +859,7 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
859
860
// One thread holds every way a run can still matter after a compaction. A run is requested at
861
// its ordinal in minutes and ends 30 seconds later, unless the table says otherwise.
862
-it.effect("a compacted thread reads every run, and run records only for its recent runs", () =>
862
+it.effect("a compacted thread reads every run, but records and prompts only for recent ones", () =>
863
Effect.gen(function* () {
864
const store = yield* ProjectionStore.ProjectionStoreV2;
865
const threadId = yield* addRolledBackRecoveryCandidate("compacted");
@@ -905,9 +905,10 @@ it.effect("a compacted thread reads every run, and run records only for its rece
905
driver,
906
},
907
});
908
- for (const [ordinal, status, endedAt] of [
908
+ for (const [ordinal, status, endedAt, fresh] of [
909
[2, "queued", null],
910
- [3, "completed"],
910
+ // A fresh-context run starts its own conversation, so its prompt is read for the schedule check.
911
+ [3, "completed", undefined, true],
912
// A held queue ran this old run after the compactions.
913
[4, "completed", at(11.5)],
914
[5, "completed"],
@@ -951,6 +952,7 @@ it.effect("a compacted thread reads every run, and run records only for its rece
952
completedAt: ended,
953
checkpointId: null,
954
contextHandoffId: null,
955
+ ...(fresh === true ? { freshContext: true } : {}),
956
},
957
});
958
yield* store.apply({
@@ -1086,19 +1088,44 @@ it.effect("a compacted thread reads every run, and run records only for its rece
1088
})),
1089
);
1090
const all = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
1089
- // Every run and every turn item is read, so a model switch is still measured from the last
1090
- // prompt. Run 1, the helper's, holds only a command item.
1091
- const expected = (recent: ReadonlyArray<number>) => ({
1091
+ // Every run is read. Turn items are the failed errors of every run, and the prompts of the
1092
+ // recent runs, of the fresh-context run 3 and of the run a model switch is measured from.
1093
+ // Run 1, the helper's, holds only a command item.
1094
+ const expected = (
1095
+ recent: ReadonlyArray<number>,
1096
+ prompts: ReadonlyArray<number> = [...recent, 3],
1097
+ ) => ({
1098
runs: 15,
1099
attempts: recent,
1100
nodes: recent,
1101
providerTurns: recent,
1102
messages: recent,
1097
- turnItems: [1, ...all],
1103
+ turnItems: [...new Set([5, ...prompts])].toSorted((a, b) => a - b),
1104
});
1105
1100
- // Nothing is compacted yet, so the whole history is current.
1101
- assert.deepEqual(yield* read(), expected(all));
1106
+ // Run 5 failed, and nothing is compacted yet, so the whole history is current.
1107
+ yield* store.apply({
1108
+ id: EventId.make("event:compacted:error-5"),
1109
+ type: "turn-item.updated",
1110
+ threadId,
1111
+ runId: runId(5),
1112
+ nodeId: rootNodeId(5),
1113
+ driver,
1114
+ occurredAt: at(5),
1115
+ payload: {
1116
+ ...item(5, 52, "failed"),
1117
+ id: TurnItemId.make("item:compacted:error-5"),
1118
+ type: "error",
1119
+ failure: {
1120
+ class: "usage_limit",
1121
+ message: "Plan limit reached.",
1122
+ resetAt: "2099-01-01T00:00:00.000Z",
1123
+ code: "usageLimitExceeded",
1124
+ retryable: null,
1125
+ },
1126
+ },
1127
+ });
1128
+ assert.deepEqual(yield* read(), { ...expected(all), turnItems: [1, ...all] });
1129
1130
// The window starts at the compaction's run. Queued run 2 and held run 4 are older but had
1131
// not ended by then.
@@ -1122,6 +1149,20 @@ it.effect("a compacted thread reads every run, and run records only for its rece
1149
expected([2, 3, 4, 6, 11, 12, 13, 14, 15]),
1150
);
1151
1152
+ // Only an unfiltered read with runs narrows its prompts; any other read keeps its filter.
1153
+ const itemRuns = (fields: ReadonlyArray<"runs" | "turnItems">, filter: object) =>
1154
+ store
1155
+ .getThreadRecords(threadId, fields, {
1156
+ sinceLastCompaction: { keepRunIds: [], keepMessageId: messageId(0) },
1157
+ ...filter,
1158
+ })
1159
+ .pipe(Effect.map((records) => ordinals(records.turnItems.map((row) => row.runId))));
1160
+ assert.deepEqual(yield* itemRuns(["runs", "turnItems"], { turnItemRunIds: [runId(5)] }), [5]);
1161
+ assert.deepEqual(yield* itemRuns(["runs", "turnItems"], { turnItemRunIds: [] }), []);
1162
+ assert.deepEqual(yield* itemRuns(["runs", "turnItems"], { turnItemRunId: runId(5) }), [5]);
1163
+ assert.deepEqual(yield* itemRuns(["runs", "turnItems"], { runIds: [runId(15)] }), [1, ...all]);
1164
+ assert.deepEqual(yield* itemRuns(["turnItems"], {}), [1, ...all]);
1165
+
1166
// A command also skips the node and the provider thread of each settled task. Tasks ended on
1167
// both sides of the compaction; one ran in the provider thread the thread's own runs use.
1168
const addProviderThread = (id: ProviderThreadId, appThreadId: ThreadId) =>
@@ -1268,11 +1309,12 @@ it.effect("a compacted thread reads every run, and run records only for its rece
1309
assert.deepEqual(unsettled.providerThreads, ["compacted", "old-pending", "recent-pending"]);
1310
assert.lengthOf(unsettled.rootNodes, all.length);
1311
assert.lengthOf(unsettled.runs, 15);
1271
- // Together: every run, every turn item and the provider thread the runs use stay, and the
1272
- // windowed records are read for the recent and named runs, less each settled task's node.
1312
+ // Together: every run and the provider thread the runs use stay, and the windowed records are
1313
+ // read for the recent and named runs, less each settled task's node.
1314
const recentRuns = ["11", "12", "13", "14", "15", "2", "3", "4", "6"];
1315
assert.deepEqual(both, {
1316
...unsettled,
1317
+ turnItems: windowed.turnItems,
1318
attempts: recentRuns,
1319
rootNodes: recentRuns,
1320
taskNodes: ["recent-pending"],
@@ -1289,6 +1331,47 @@ it.effect("a compacted thread reads every run, and run records only for its rece
1331
),
1332
both,
1333
);
1334
+
1335
+ // A prompt that clients do not show cannot anchor a model switch. With the recent ones, and
1336
+ // run 4's, folded into async answers, the switch is measured from run 10, so its prompt is
1337
+ // read, with run 9's from the same batch.
1338
+ const storedRuns = (yield* store.getThreadRecords(threadId, ["runs"])).runs;
1339
+ for (const ordinal of [4, 11, 12, 14, 15]) {
1340
+ yield* store.apply({
1341
+ id: EventId.make(`event:compacted:async-run-${ordinal}`),
1342
+ type: "run.updated",
1343
+ threadId,
1344
+ occurredAt: at(ordinal),
1345
+ payload: {
1346
+ ...storedRuns.find((run) => run.id === runId(ordinal))!,
1347
+ userMessageId: MessageId.make(`async-answer:${ordinal}`),
1348
+ },
1349
+ });
1350
+ yield* store.apply({
1351
+ id: EventId.make(`event:compacted:async-${ordinal}`),
1352
+ type: "turn-item.updated",
1353
+ threadId,
1354
+ runId: runId(ordinal),
1355
+ nodeId: rootNodeId(ordinal),
1356
+ driver,
1357
+ occurredAt: at(ordinal),
1358
+ payload: {
1359
+ createdBy: "user",
1360
+ creationSource: "web",
1361
+ ...item(ordinal, ordinal * 10, "completed"),
1362
+ id: TurnItemId.make(`item:compacted:prompt-${ordinal}`),
1363
+ type: "user_message",
1364
+ messageId: MessageId.make(`async-answer:${ordinal}`),
1365
+ inputIntent: "turn_start",
1366
+ text: `Answer ${ordinal}`,
1367
+ attachments: [],
1368
+ },
1369
+ });
1370
+ }
1371
+ assert.deepEqual(
1372
+ (yield* read()).turnItems,
1373
+ expected([2, 4, 11, 12, 13, 14, 15], [2, 3, 4, 9, 10, 11, 12, 13, 14, 15]).turnItems,
1374
+ );
1375
}).pipe(Effect.provide(layerTest)),
1376
);
1377
apps/server/src/orchestration-v2/ProjectionStore.ts
+95
-8
@@ -15,10 +15,16 @@ import type {
15
import {
16
latestRootProviderFailure,
17
latestUnheldRun,
18
+ runRanAfter,
19
threadErrorSummary,
20
usageLimitRunPresentedAsLatest,
21
} from "@t3tools/shared/orchestrationV2ThreadError";
22
import { threadPullRequestsOf } from "../sourceControl/threadPullRequests.ts";
23
+import {
24
+ anchorsModelSwitch,
25
+ isModelSwitchCandidate,
26
+ isShownUserMessageItem,
27
+} from "./modelSwitch.ts";
28
import type {
29
OrchestrationV2AppThread,
30
OrchestrationV2Checkpoint,
@@ -346,6 +352,17 @@ export interface ProjectionRuntimeResponseContext {
352
readonly session: OrchestrationV2ThreadProjection["providerSessions"][number] | undefined;
353
}
354
355
+const PROMPT_NARROWING_BLOCKING_FILTERS = [
356
+ "runIds",
357
+ "runStatuses",
358
+ "latestRunLimit",
359
+ "turnItemRunId",
360
+ "turnItemRunIds",
361
+ "turnItemNodeIds",
362
+ "turnItemRequestIds",
363
+ "turnItemStatuses",
364
+] as const satisfies ReadonlyArray<keyof ProjectionRecordFilter>;
365
+
366
export interface ProjectionRecordFilter {
367
readonly providerThreadIds?: ReadonlyArray<ProviderThreadId>;
368
readonly providerTurnIds?: ReadonlyArray<ProviderTurnId>;
@@ -392,9 +409,10 @@ export interface ProjectionRecordFilter {
409
*/
410
readonly omitSettledSubagentRecords?: boolean;
411
/**
395
- * For a compacted thread: reads attempts, nodes, messages and provider turns only for runs from
396
- * the latest completed compaction's run on, runs that had not ended when that run was
397
- * requested, and the runs named here. Runs and turn items are not narrowed.
412
+ * For a compacted thread, reads attempts, nodes, messages and provider turns of the recent runs
413
+ * and the runs named here. Turn items are the failed errors of every run and the prompts of the
414
+ * recent, fresh-context and model-switch runs. `turnItemTypes` does not apply. A read without `runs`, or with a run or turn-item
415
+ * filter, keeps its filtered items. Runs stay whole.
416
*/
417
readonly sinceLastCompaction?: {
418
readonly keepRunIds: ReadonlyArray<RunId>;
@@ -3701,7 +3719,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3719
const threadColumn = (ids: ReadonlyArray<unknown> | undefined, column = "thread_id") =>
3720
sql.literal(ids === undefined ? column : `+${column}`);
3721
3704
- /** The runs whose records a new message reads. Null when nothing was compacted. */
3722
+ /** The ids of the runs whose records a new message reads. Null when nothing was compacted. */
3723
const runIdsSinceLastCompaction = Effect.fn("ProjectionStore.runIdsSinceLastCompaction")(
3724
function* (
3725
threadId: ThreadId,
@@ -3740,10 +3758,67 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3758
// The compaction's own run is always recent, so no rows means no compaction.
3759
return recent.length === 0
3760
? null
3743
- : encodeIdList([...recent.map((row) => row.run_id), ...keep.keepRunIds]);
3761
+ : [...recent.map((row) => row.run_id), ...keep.keepRunIds];
3762
},
3763
);
3764
3765
+ /**
3766
+ * What a command reads of a compacted thread's prompts and errors: the failed errors of every
3767
+ * run, and the prompts of the recent runs, of fresh-context runs, and of the newest runs back
3768
+ * to the one `modelSwitchSince` measures from. That one can be older than the compaction.
3769
+ */
3770
+ const commandTurnItems = Effect.fn("ProjectionStore.commandTurnItems")(function* (
3771
+ threadId: ThreadId,
3772
+ runs: ReadonlyArray<OrchestrationV2Run>,
3773
+ recentRunIds: ReadonlyArray<RunId>,
3774
+ ) {
3775
+ const errors = yield* sql<PayloadRow>`
3776
+ SELECT payload_json
3777
+ FROM orchestration_v2_projection_turn_items
3778
+ INDEXED BY orchestration_v2_projection_turn_items_failed_error_idx
3779
+ WHERE thread_id = ${threadId} AND type = 'error' AND status = 'failed'
3780
+ `.pipe(Effect.flatMap(decodeRows(decodeTurnItemPayload, threadId)));
3781
+ const readPrompts = (runIds: ReadonlyArray<string>) =>
3782
+ sql<PayloadRow>`
3783
+ SELECT payload_json
3784
+ FROM orchestration_v2_projection_turn_items
3785
+ INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
3786
+ WHERE thread_id = ${threadId} AND type = 'user_message'
3787
+ AND run_id IN (SELECT value FROM json_each(${encodeIdList(runIds)}))
3788
+ `.pipe(Effect.flatMap(decodeRows(decodeTurnItemPayload, threadId)));
3789
+ const read = new Set<string>(recentRunIds);
3790
+ for (const run of runs) if (run.freshContext === true) read.add(run.id);
3791
+ const prompts = yield* readPrompts([...read]);
3792
+ const shownMessageIds = new Set<string>();
3793
+ const showPrompts = (items: ReadonlyArray<OrchestrationV2TurnItem>) => {
3794
+ for (const item of items) {
3795
+ if (item.type === "user_message" && isShownUserMessageItem(item)) {
3796
+ shownMessageIds.add(item.messageId);
3797
+ }
3798
+ }
3799
+ };
3800
+ showPrompts(prompts);
3801
+ const newestFirst = runs
3802
+ .filter(isModelSwitchCandidate)
3803
+ .toSorted((left, right) => (runRanAfter(left, right) ? -1 : 1));
3804
+ // The batches double, so a thread with no such run takes few queries.
3805
+ for (let start = 0, size = 1; start < newestFirst.length; start += size, size *= 2) {
3806
+ const batch = newestFirst.slice(start, start + size);
3807
+ const unread = batch.filter((run) => !read.has(run.id));
3808
+ if (unread.length > 0) {
3809
+ for (const run of unread) read.add(run.id);
3810
+ const more = yield* readPrompts(unread.map((run) => run.id));
3811
+ showPrompts(more);
3812
+ prompts.push(...more);
3813
+ }
3814
+ if (batch.some((run) => anchorsModelSwitch(run, shownMessageIds))) break;
3815
+ }
3816
+ return [...errors, ...prompts].toSorted(
3817
+ (left, right) =>
3818
+ left.ordinal - right.ordinal || (left.id < right.id ? -1 : left.id > right.id ? 1 : 0),
3819
+ );
3820
+ });
3821
+
3822
const readCanonicalProjection = Effect.fn("ProjectionStore.readCanonicalProjection")(
3823
function* (
3824
threadId: ThreadId,
@@ -3761,10 +3836,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3836
if (!threadRow) {
3837
return yield* new ProjectionStoreThreadNotFoundError({ threadId });
3838
}
3764
- const recentRunIds =
3839
+ const recentRuns =
3840
window === undefined && filter?.sinceLastCompaction !== undefined
3841
? yield* runIdsSinceLastCompaction(threadId, filter.sinceLastCompaction)
3842
: null;
3843
+ const recentRunIds = recentRuns === null ? null : encodeIdList(recentRuns);
3844
+ const readsWholeHistory =
3845
+ (fields === undefined || fields.includes("runs")) &&
3846
+ PROMPT_NARROWING_BLOCKING_FILTERS.every((key) => filter?.[key] === undefined);
3847
+ const promptRuns = readsWholeHistory ? recentRuns : null;
3848
const inRecentRuns = (column: string) =>
3849
recentRunIds === null
3850
? sql``
@@ -3782,6 +3862,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3862
};
3863
const boundedTurnItemRows: ReadonlyArray<WindowPayloadRow> =
3864
(fields !== undefined && !fields.includes("turnItems")) ||
3865
+ // A compacted thread reads its items once its runs are known.
3866
+ promptRuns !== null ||
3867
(window === undefined &&
3868
matchesNothing(
3869
filter?.turnItemRequestIds,
@@ -3831,7 +3913,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3913
decodeTurnItemPayload,
3914
threadId,
3915
)(boundedTurnItemRows);
3834
- const turnItems = decodedTurnItems.map((item, index) => {
3916
+ const storedTurnItems = decodedTurnItems.map((item, index) => {
3917
const row = boundedTurnItemRows[index]!;
3918
return row.work_last_id == null ||
3919
row.work_item_count == null ||
@@ -3854,7 +3936,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3936
});
3937
const cohortJson = (field: string) =>
3938
JSON.stringify(
3857
- turnItems.flatMap((payload) => {
3939
+ storedTurnItems.flatMap((payload) => {
3940
const value = nullableStringField(payload, field);
3941
return value === null ? [] : [value];
3942
}),
@@ -4523,12 +4605,17 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4605
decodeRows(decodeContextHandoffPayload, threadId)(contextHandoffRows),
4606
decodeRows(decodeContextTransferPayload, threadId)(contextTransferRows),
4607
]);
4608
+ const turnItems =
4609
+ promptRuns === null || (fields !== undefined && !fields.includes("turnItems"))
4610
+ ? storedTurnItems
4611
+ : yield* commandTurnItems(threadId, runs, promptRuns);
4612
const messageOrdinals = new Map<string, number>();
4613
// Only user items can name a user message, so a user-only read needs no assistant items.
4614
const userOnly = messages.every((message) => message.role === "user");
4615
// The loaded items already order the messages when they hold every item the lookup reads.
4616
const loadsEveryItemOf = (type: OrchestrationV2TurnItem["type"]) =>
4617
window === undefined &&
4618
+ promptRuns === null &&
4619
(fields === undefined || fields.includes("turnItems")) &&
4620
filter?.turnItemRequestIds === undefined &&
4621
filter?.turnItemStatuses === undefined &&
apps/server/src/orchestration-v2/contextBoundary.ts
+1
-1
@@ -7,7 +7,7 @@ export function isScheduledFreshRun(
7
run: Pick<OrchestrationV2Run, "freshContext" | "userMessageId">,
8
turnItems: OrchestrationV2ThreadProjection["turnItems"],
9
): boolean {
10
- // Turn items, unlike messages, are not narrowed to the runs since the latest compaction.
10
+ // A compacted thread's command read loads these prompts for every fresh-context run.
11
return (
12
run.freshContext === true &&
13
turnItems.some(
apps/server/src/orchestration-v2/modelSwitch.ts
+24
-11
@@ -7,6 +7,27 @@ import {
7
import { modelSelectionsEqual } from "@t3tools/shared/model";
8
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
9
10
+/**
11
+ * Clients hide pending queued messages, fold async answers into their question, and replace
12
+ * `/compact` with a compaction row.
13
+ */
14
+export const isShownUserMessageItem = (item: OrchestrationV2TurnItem): boolean =>
15
+ item.type === "user_message" &&
16
+ !(item.inputIntent === "queued_turn" && item.status === "pending") &&
17
+ !item.messageId.startsWith("async-answer:") &&
18
+ !isCompactionCommandMessage({ role: "user", ...item });
19
+
20
+type ModelSwitchRun = OrchestrationV2ThreadProjection["runs"][number];
21
+
22
+/** A run that has not been undone and has started or can start. */
23
+export const isModelSwitchCandidate = (run: ModelSwitchRun): boolean =>
24
+ run.status !== "rolled_back" && run.status !== "queued";
25
+
26
+/** A run `modelSwitchSince` can measure from: its prompt is shown, or its handoff row shows it. */
27
+export const anchorsModelSwitch = (run: ModelSwitchRun, shownMessageIds: ReadonlySet<string>) =>
28
+ isModelSwitchCandidate(run) &&
29
+ (shownMessageIds.has(run.userMessageId) || run.contextHandoffId !== null);
30
+
31
/**
32
* The model or effort change a new run brings, measured from the latest run the timeline shows
33
* through its message or its handoff row, so consecutive dividers always chain. `baseline` stands
@@ -18,23 +39,15 @@ export function modelSwitchSince(
39
to: ModelSelection,
40
baseline?: ModelSelection,
41
): Pick<Extract<OrchestrationV2TurnItem, { type: "user_message" }>, "modelSwitch"> {
21
- // Clients hide pending queued messages, fold async answers into their question, and replace
22
- // `/compact` with a compaction row.
42
const shown = new Set(
43
projection.turnItems.flatMap((item) =>
25
- item.type === "user_message" &&
26
- !(item.inputIntent === "queued_turn" && item.status === "pending") &&
27
- !item.messageId.startsWith("async-answer:") &&
28
- !isCompactionCommandMessage({ role: "user", ...item })
29
- ? [item.messageId]
30
- : [],
44
+ item.type === "user_message" && isShownUserMessageItem(item) ? [item.messageId] : [],
45
),
46
);
33
- let previous: OrchestrationV2ThreadProjection["runs"][number] | undefined;
47
+ let previous: ModelSwitchRun | undefined;
48
for (const run of projection.runs) {
35
- if (run.status === "rolled_back" || run.status === "queued") continue;
49
// A wake shows as a notification, but its context handoff row still shows the swap.
37
- if (!shown.has(run.userMessageId) && run.contextHandoffId === null) continue;
50
+ if (!anchorsModelSwitch(run, shown)) continue;
51
if (previous === undefined || runRanAfter(run, previous)) previous = run;
52
}
53
const from = previous?.modelSelection ?? baseline;