Skip to content

Commit 9217472

Browse files
braulioboclaude
andcommitted
perf(server): sending a prompt in a compacted thread reads only what follows the compaction
A message sent to a thread that was compacted read the thread's whole history before the Working status could appear. The command read now keeps only the runs since the last prompt before the latest completed compaction, plus live runs, fresh-context runs and the runs the command names. A fresh session or another provider still reads everything, as before. Migration 083 adds the indexes these reads need; it is idempotent for databases that already have them from earlier builds. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent e9df591 commit 9217472

8 files changed

Lines changed: 559 additions & 21 deletions

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14581458
});
14591459

14601460
// Runs in every command transaction; avoid reading thousands of finished leaf nodes.
1461-
const readCommandProjection = (threadId: ThreadId) =>
1461+
const readCommandProjection = (
1462+
threadId: ThreadId,
1463+
recent?: { keepRunIds: ReadonlyArray<RunId> },
1464+
) =>
14621465
projectionStore
14631466
.getThreadRecords(
14641467
threadId,
@@ -1483,6 +1486,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14831486
// Commands here read only undelivered or active tasks; a dispatcher thread keeps
14841487
// thousands of delivered ones, each carrying its full prompt.
14851488
omitSettledSubagents: true,
1489+
...(recent === undefined ? {} : { sinceLastCompaction: recent }),
14861490
},
14871491
)
14881492
.pipe(
@@ -1571,9 +1575,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15711575
Effect.fnUntraced(function* (
15721576
threadId: ThreadId,
15731577
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
1578+
recent?: { keepRunIds: ReadonlyArray<RunId> },
15741579
) {
15751580
const pending = (yield* Ref.get(events)).filter((event) => event.threadId === threadId);
1576-
const stored = yield* Effect.option(readCommandProjection(threadId));
1581+
const stored = yield* Effect.option(readCommandProjection(threadId, recent));
15771582
let projection: OrchestrationV2ThreadProjection;
15781583
if (Option.isSome(stored)) {
15791584
projection = stored.value;
@@ -6166,7 +6171,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
61666171
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
61676172
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
61686173
) {
6169-
let projection = yield* getProjectionWithPendingEvents(command.threadId, events);
6174+
// A compacted thread needs only its recent runs, plus any older run the command names.
6175+
let projection = yield* getProjectionWithPendingEvents(command.threadId, events, {
6176+
keepRunIds: [
6177+
command.manualContinuationOfRunId,
6178+
command.restartContinuationOfRunId,
6179+
command.usageLimitContinuationOfRunId,
6180+
command.delegatedCompletion?.parentRunId,
6181+
...(command.dispatchMode.type === "steer_active" ||
6182+
command.dispatchMode.type === "restart_active"
6183+
? [command.dispatchMode.targetRunId]
6184+
: []),
6185+
].flatMap((runId) => (runId === undefined ? [] : [runId])),
6186+
});
61706187
if (yield* isStaleQuestionWake(projection, command.messageId, command.notification)) {
61716188
yield* emit(
61726189
events,
@@ -6179,6 +6196,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
61796196
});
61806197
return;
61816198
}
6199+
const activeProviderInstanceId = projection.providerThreads.find(
6200+
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
6201+
)?.providerInstanceId;
6202+
// A fresh session or another provider is handed the whole history, so it reads all of it.
6203+
if (
6204+
command.freshContext === true ||
6205+
(activeProviderInstanceId !== undefined &&
6206+
activeProviderInstanceId !==
6207+
(command.modelSelection ?? projection.thread.modelSelection).instanceId)
6208+
) {
6209+
projection = yield* getProjectionWithPendingEvents(command.threadId, events);
6210+
}
61826211
if (command.manualContinuationOfRunId !== undefined) {
61836212
const source = projection.runs.find((run) => run.id === command.manualContinuationOfRunId);
61846213
const limited = latestRootProviderFailure(source ?? null, projection.turnItems);
@@ -6381,16 +6410,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
63816410
});
63826411
projection = applyToProjection(projection, event);
63836412
}
6384-
const userMessages = projection.messages.filter((message) => message.role === "user");
6385-
const onlyMaintenanceHistory =
6386-
userMessages.length > 0 && userMessages.every(isNativeMaintenanceCommand);
63876413
if (
63886414
!isNativeMaintenanceCommand(command) &&
63896415
((command.titleSeed !== undefined &&
63906416
(yield* projectionStore
63916417
.getMessageCount(command.threadId)
63926418
.pipe(mapDispatchError(command))) === 0) ||
6393-
(command.createdBy === "user" && onlyMaintenanceHistory))
6419+
(command.createdBy === "user" &&
6420+
(yield* projectionStore
6421+
.hasOnlyMaintenanceUserMessages(command.threadId)
6422+
.pipe(mapDispatchError(command)))))
63946423
) {
63956424
const now = yield* DateTime.now;
63966425
const thread: OrchestrationV2AppThread = {

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,206 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
762762
}).pipe(Effect.provide(layerTest)),
763763
);
764764

765+
it.effect("a compacted thread reads its recent runs, live runs, fresh starts and named runs", () =>
766+
Effect.gen(function* () {
767+
const store = yield* ProjectionStore.ProjectionStoreV2;
768+
const threadId = yield* addRolledBackRecoveryCandidate("compacted");
769+
const now = yield* DateTime.now;
770+
const nodeId = NodeId.make("node:compacted");
771+
const runId = (ordinal: number) => RunId.make(`run:compacted:${ordinal}`);
772+
const read = (keepRunIds: ReadonlyArray<RunId>) =>
773+
store.getThreadRecords(threadId, ["runs", "messages"], {
774+
messageRoles: ["user"],
775+
sinceLastCompaction: { keepRunIds },
776+
});
777+
// Run 1 is the helper's rolled-back run. Run 2 is live and run 3 starts a fresh session.
778+
// Runs 6 and 9 are `/compact` runs that hold a compaction; run 8 is one that does not.
779+
for (const [ordinal, status] of [
780+
[2, "running"],
781+
[3, "completed"],
782+
[4, "completed"],
783+
[5, "completed"],
784+
[6, "completed"],
785+
[7, "completed"],
786+
[8, "completed"],
787+
[9, "completed"],
788+
] as const) {
789+
yield* store.apply({
790+
id: EventId.make(`event:compacted:run-${ordinal}`),
791+
type: "run.created",
792+
threadId,
793+
runId: runId(ordinal),
794+
nodeId,
795+
driver,
796+
providerInstanceId,
797+
occurredAt: now,
798+
payload: {
799+
id: runId(ordinal),
800+
threadId,
801+
ordinal,
802+
providerInstanceId,
803+
modelSelection,
804+
providerThreadId: null,
805+
userMessageId: MessageId.make(`message:compacted:${ordinal}`),
806+
rootNodeId: nodeId,
807+
activeAttemptId: null,
808+
status,
809+
requestedAt: now,
810+
startedAt: now,
811+
completedAt: status === "running" ? null : now,
812+
checkpointId: null,
813+
contextHandoffId: null,
814+
...(ordinal === 3 ? { freshContext: true } : {}),
815+
},
816+
});
817+
yield* store.apply({
818+
id: EventId.make(`event:compacted:message-${ordinal}`),
819+
type: "message.updated",
820+
threadId,
821+
runId: runId(ordinal),
822+
occurredAt: now,
823+
payload: {
824+
id: MessageId.make(`message:compacted:${ordinal}`),
825+
threadId,
826+
runId: runId(ordinal),
827+
nodeId,
828+
createdBy: "user",
829+
creationSource: "web",
830+
role: "user",
831+
text: [6, 8, 9].includes(ordinal) ? "/compact" : `Prompt ${ordinal}`,
832+
attachments: [],
833+
streaming: false,
834+
createdAt: now,
835+
updatedAt: now,
836+
},
837+
});
838+
}
839+
const rolledBackId = RunId.make("run:compacted:rolled-back");
840+
const ids = <T extends { readonly id: string }>(rows: ReadonlyArray<T>) =>
841+
rows.map((row) => row.id);
842+
const compact = (ordinal: number) =>
843+
store.apply({
844+
id: EventId.make(`event:compacted:compaction-${ordinal}`),
845+
type: "turn-item.updated",
846+
threadId,
847+
runId: runId(ordinal),
848+
nodeId,
849+
driver,
850+
occurredAt: now,
851+
payload: {
852+
id: TurnItemId.make(`item:compacted:compaction-${ordinal}`),
853+
threadId,
854+
runId: runId(ordinal),
855+
nodeId,
856+
providerThreadId: null,
857+
providerTurnId: null,
858+
nativeItemRef: null,
859+
parentItemId: null,
860+
ordinal,
861+
status: "completed",
862+
title: null,
863+
startedAt: now,
864+
completedAt: now,
865+
updatedAt: now,
866+
type: "compaction",
867+
driver,
868+
},
869+
});
870+
// Nothing is compacted yet, so the whole history is current.
871+
assert.lengthOf((yield* read([])).runs, 9);
872+
873+
yield* compact(6);
874+
// Legacy rows can hold unreadable JSON; the fresh-context index must not reject them.
875+
const sql = yield* SqlClient.SqlClient;
876+
yield* sql`
877+
INSERT INTO orchestration_v2_projection_runs
878+
(run_id, thread_id, ordinal, provider, status, requested_at, payload_json)
879+
VALUES ('run:compacted:legacy', ${threadId}, 0, 'codex', 'completed', '', '{')
880+
`;
881+
const runIdsOf = (ordinals: ReadonlyArray<number>) => ordinals.map(runId);
882+
// The window starts at run 5, the prompt before the compaction.
883+
const recent = yield* read([rolledBackId]);
884+
assert.deepEqual(ids(recent.runs), [rolledBackId, ...runIdsOf([2, 3, 5, 6, 7, 8, 9])]);
885+
assert.deepEqual(
886+
ids(recent.messages),
887+
[2, 3, 5, 6, 7, 8, 9].map((ordinal) => `message:compacted:${ordinal}`),
888+
);
889+
assert.deepEqual(ids((yield* read([])).runs), runIdsOf([2, 3, 5, 6, 7, 8, 9]));
890+
891+
// Run 8 is a `/compact` run, so the model-switch baseline is the prompt before it, run 7.
892+
yield* compact(9);
893+
assert.deepEqual(ids((yield* read([])).runs), runIdsOf([2, 3, 7, 8, 9]));
894+
895+
// The search for a prompt before the compaction may walk past an unreadable run.
896+
const legacyThreadId = yield* addRolledBackRecoveryCandidate("compacted-legacy");
897+
const legacyNodeId = NodeId.make("node:compacted-legacy");
898+
const compactionRunId = RunId.make("run:compacted-legacy:2");
899+
yield* sql`
900+
INSERT INTO orchestration_v2_projection_runs
901+
(run_id, thread_id, ordinal, provider, status, requested_at, payload_json)
902+
VALUES ('run:compacted-legacy:0', ${legacyThreadId}, 0, 'codex', 'completed', '', '{')
903+
`;
904+
yield* store.apply({
905+
id: EventId.make("event:compacted-legacy:run"),
906+
type: "run.created",
907+
threadId: legacyThreadId,
908+
runId: compactionRunId,
909+
nodeId: legacyNodeId,
910+
driver,
911+
providerInstanceId,
912+
occurredAt: now,
913+
payload: {
914+
id: compactionRunId,
915+
threadId: legacyThreadId,
916+
ordinal: 2,
917+
providerInstanceId,
918+
modelSelection,
919+
providerThreadId: null,
920+
userMessageId: MessageId.make("message:compacted-legacy:2"),
921+
rootNodeId: legacyNodeId,
922+
activeAttemptId: null,
923+
status: "completed",
924+
requestedAt: now,
925+
startedAt: now,
926+
completedAt: now,
927+
checkpointId: null,
928+
contextHandoffId: null,
929+
},
930+
});
931+
yield* store.apply({
932+
id: EventId.make("event:compacted-legacy:compaction"),
933+
type: "turn-item.updated",
934+
threadId: legacyThreadId,
935+
runId: compactionRunId,
936+
nodeId: legacyNodeId,
937+
driver,
938+
occurredAt: now,
939+
payload: {
940+
id: TurnItemId.make("item:compacted-legacy:compaction"),
941+
threadId: legacyThreadId,
942+
runId: compactionRunId,
943+
nodeId: legacyNodeId,
944+
providerThreadId: null,
945+
providerTurnId: null,
946+
nativeItemRef: null,
947+
parentItemId: null,
948+
ordinal: 2,
949+
status: "completed",
950+
title: null,
951+
startedAt: now,
952+
completedAt: now,
953+
updatedAt: now,
954+
type: "compaction",
955+
driver,
956+
},
957+
});
958+
const legacy = yield* store.getThreadRecords(legacyThreadId, ["runs"], {
959+
sinceLastCompaction: { keepRunIds: [] },
960+
});
961+
assert.deepEqual(ids(legacy.runs), [compactionRunId]);
962+
}).pipe(Effect.provide(layerTest)),
963+
);
964+
765965
it.effect("record reads filter notification sources without dropping matching message bodies", () =>
766966
Effect.gen(function* () {
767967
const store = yield* ProjectionStore.ProjectionStoreV2;

0 commit comments

Comments
 (0)