Skip to content

Commit 2c48b09

Browse files
perf(server): idle compaction reads only the latest run, live work and newest usage
compactWhenDue read every run, provider turn, runtime request and context transfer of the thread on each completed run. It now asks ProjectionStore for the latest run plus active and rolled-back runs, the active provider thread, that thread's turn with the newest token usage, and pending runtime requests and context transfers. New ProjectionRecordFilter fields carry those narrowings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 065a433 commit 2c48b09

3 files changed

Lines changed: 222 additions & 7 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export const make = Effect.gen(function* () {
5050
)
5151
)
5252
return;
53+
const activeProviderThreadIds =
54+
thread.activeProviderThreadId === null ? [] : [thread.activeProviderThreadId];
5355
const records = yield* projections.getThreadRecords(
5456
threadId,
5557
[
@@ -61,7 +63,20 @@ export const make = Effect.gen(function* () {
6163
"turnItems",
6264
"contextTransfers",
6365
],
64-
{ messageRunIds: [runId], turnItemStatuses: ["pending", "running", "waiting"] },
66+
{
67+
messageRunIds: [runId],
68+
turnItemStatuses: ["pending", "running", "waiting"],
69+
// The latest run, active runs that hold background work, and rolled-back runs whose
70+
// items it ignores.
71+
runStatuses: ["preparing", "starting", "running", "rolled_back"],
72+
includeLatestRun: true,
73+
// Without an active provider thread there is no session to compact.
74+
providerThreadIds: activeProviderThreadIds,
75+
providerTurnThreadIds: activeProviderThreadIds,
76+
latestProviderTurnUsageOnly: true,
77+
runtimeRequestStatuses: ["pending"],
78+
contextTransferStatuses: ["pending"],
79+
},
6580
);
6681
if (
6782
resolveIdleCompaction(current, records.thread.providerInstanceId) !==

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

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
EventId,
55
CommandId,
66
CheckpointId,
7+
ContextTransferId,
78
CheckpointRef,
89
CheckpointScopeId,
910
MessageId,
@@ -307,6 +308,169 @@ it.effect("record reads restrict provider histories to delivery identities", ()
307308
}).pipe(Effect.provide(layerTest)),
308309
);
309310

311+
it.effect("idle compaction filters keep the latest run, live work and the newest usage", () =>
312+
Effect.gen(function* () {
313+
const store = yield* ProjectionStore.ProjectionStoreV2;
314+
// Its rolled-back run 1 still has a running item, which background work must ignore.
315+
const threadId = yield* addRolledBackRecoveryCandidate("idle-compaction");
316+
const now = yield* DateTime.now;
317+
const minutes = (count: number) => DateTime.add(now, { minutes: count });
318+
const selected = ProviderThreadId.make("provider-thread:idle-selected");
319+
const nodeId = NodeId.make("node:idle-compaction");
320+
for (const [ordinal, status] of [
321+
[2, "completed"],
322+
[3, "completed"],
323+
[4, "failed"],
324+
] as const) {
325+
const runId = RunId.make(`run:idle-compaction:${ordinal}`);
326+
yield* store.apply({
327+
id: EventId.make(`event:idle-compaction:run-${ordinal}`),
328+
type: "run.created",
329+
threadId,
330+
runId,
331+
nodeId,
332+
driver,
333+
providerInstanceId,
334+
occurredAt: now,
335+
payload: {
336+
id: runId,
337+
threadId,
338+
ordinal,
339+
providerInstanceId,
340+
modelSelection,
341+
providerThreadId: selected,
342+
userMessageId: MessageId.make(`message:idle-compaction:${ordinal}`),
343+
rootNodeId: nodeId,
344+
activeAttemptId: null,
345+
status,
346+
requestedAt: now,
347+
startedAt: now,
348+
completedAt: now,
349+
checkpointId: null,
350+
contextHandoffId: null,
351+
},
352+
});
353+
}
354+
for (const [id, providerThreadId, ordinal, usageAt] of [
355+
["turn:idle-old", selected, 1, minutes(1)],
356+
["turn:idle-newest", selected, 2, minutes(3)],
357+
["turn:idle-later-ordinal", selected, 3, minutes(2)],
358+
["turn:idle-no-usage", selected, 4, null],
359+
[
360+
"turn:idle-other-thread",
361+
ProviderThreadId.make("provider-thread:idle-other"),
362+
1,
363+
minutes(9),
364+
],
365+
] as const) {
366+
yield* store.apply({
367+
id: EventId.make(`event:${id}`),
368+
type: "provider-turn.updated",
369+
threadId,
370+
nodeId,
371+
driver,
372+
occurredAt: now,
373+
payload: {
374+
id: ProviderTurnId.make(id),
375+
providerThreadId,
376+
nodeId,
377+
runAttemptId: null,
378+
nativeTurnRef: null,
379+
ordinal,
380+
status: "completed",
381+
startedAt: now,
382+
completedAt: now,
383+
...(usageAt === null
384+
? {}
385+
: { tokenUsage: { usedTokens: 150_000, updatedAt: DateTime.formatIso(usageAt) } }),
386+
},
387+
});
388+
}
389+
for (const status of ["pending", "resolved"] as const) {
390+
yield* store.apply({
391+
id: EventId.make(`event:idle-compaction:request-${status}`),
392+
type: "runtime-request.updated",
393+
threadId,
394+
nodeId,
395+
occurredAt: now,
396+
payload: {
397+
id: RuntimeRequestId.make(`request:idle-compaction:${status}`),
398+
nodeId,
399+
providerTurnId: null,
400+
nativeRequestRef: null,
401+
kind: "user_input",
402+
status,
403+
responseCapability: { type: "message" },
404+
createdAt: now,
405+
resolvedAt: status === "pending" ? null : now,
406+
},
407+
});
408+
}
409+
for (const status of ["pending", "consumed"] as const) {
410+
yield* store.apply({
411+
id: EventId.make(`event:idle-compaction:transfer-${status}`),
412+
type: "context-transfer.created",
413+
threadId,
414+
occurredAt: now,
415+
payload: {
416+
id: ContextTransferId.make(`transfer:idle-compaction:${status}`),
417+
type: "merge_back",
418+
sourceThreadId: ThreadId.make("thread:idle-compaction:side"),
419+
targetThreadId: threadId,
420+
sourcePoint: { threadId: ThreadId.make("thread:idle-compaction:side") },
421+
basePoint: null,
422+
sourceProviderInstanceId: providerInstanceId,
423+
targetProviderInstanceId: providerInstanceId,
424+
targetRunId: null,
425+
status,
426+
resolution: null,
427+
createdBy: "user",
428+
error: null,
429+
createdAt: now,
430+
updatedAt: now,
431+
consumedAt: status === "pending" ? null : now,
432+
},
433+
});
434+
}
435+
436+
const records = yield* store.getThreadRecords(
437+
threadId,
438+
["runs", "providerTurns", "runtimeRequests", "contextTransfers"],
439+
{
440+
runStatuses: ["preparing", "starting", "running", "rolled_back"],
441+
includeLatestRun: true,
442+
providerTurnThreadIds: [selected],
443+
latestProviderTurnUsageOnly: true,
444+
runtimeRequestStatuses: ["pending"],
445+
contextTransferStatuses: ["pending"],
446+
},
447+
);
448+
// The failed latest run stays, so a completed earlier run is not mistaken for the latest.
449+
assert.deepEqual(
450+
records.runs.map((run) => run.id),
451+
["run:idle-compaction:rolled-back", "run:idle-compaction:4"],
452+
);
453+
assert.deepEqual(
454+
records.providerTurns.map((turn) => turn.id),
455+
["turn:idle-newest"],
456+
);
457+
assert.deepEqual(
458+
records.runtimeRequests.map((request) => request.id),
459+
["request:idle-compaction:pending"],
460+
);
461+
assert.deepEqual(
462+
records.contextTransfers.map((transfer) => transfer.id),
463+
["transfer:idle-compaction:pending"],
464+
);
465+
const empty = yield* store.getThreadRecords(threadId, ["runs", "providerTurns"], {
466+
runStatuses: [],
467+
providerTurnThreadIds: [],
468+
latestProviderTurnUsageOnly: true,
469+
});
470+
assert.deepEqual([empty.runs, empty.providerTurns], [[], []]);
471+
}).pipe(Effect.provide(layerTest)),
472+
);
473+
310474
it.effect("record reads filter notification sources without dropping matching message bodies", () =>
311475
Effect.gen(function* () {
312476
const store = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import type {
2525
OrchestrationV2PendingRuntimeRequestSummary,
2626
PlanId,
2727
OrchestrationV2ConversationMessage,
28+
OrchestrationV2ContextTransfer,
2829
OrchestrationV2DomainEvent,
2930
OrchestrationV2OlderSettledCount,
3031
OrchestrationV2OlderSettledCursor,
3132
OrchestrationV2ProjectedTurnItem,
3233
OrchestrationV2ProviderThread,
3334
OrchestrationV2ProviderTurn,
3435
OrchestrationV2Run,
36+
OrchestrationV2RuntimeRequest,
3537
OrchestrationV2Subagent,
3638
OrchestrationV2ThreadShellSnapshot,
3739
OrchestrationV2ShellThreadStatus,
@@ -339,6 +341,14 @@ export interface ProjectionRecordFilter {
339341
readonly turnItemRunIds?: ReadonlyArray<RunId | null>;
340342
readonly turnItemNodeIds?: ReadonlyArray<NodeId>;
341343
readonly runIds?: ReadonlyArray<RunId>;
344+
readonly runStatuses?: ReadonlyArray<OrchestrationV2Run["status"]>;
345+
/** Keeps the thread's highest-ordinal run even when `runStatuses` excludes it. */
346+
readonly includeLatestRun?: true;
347+
readonly providerTurnThreadIds?: ReadonlyArray<ProviderThreadId>;
348+
/** Only the provider turn whose token usage was updated last. */
349+
readonly latestProviderTurnUsageOnly?: true;
350+
readonly runtimeRequestStatuses?: ReadonlyArray<OrchestrationV2RuntimeRequest["status"]>;
351+
readonly contextTransferStatuses?: ReadonlyArray<OrchestrationV2ContextTransfer["status"]>;
342352
readonly subagentIds?: ReadonlyArray<NodeId>;
343353
readonly subagentRunIds?: ReadonlyArray<RunId>;
344354
readonly nodeIds?: ReadonlyArray<NodeId>;
@@ -3244,14 +3254,27 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
32443254
] = yield* Effect.all([
32453255
decodeThreadPayload(threadRow.payload_json),
32463256
(fields !== undefined && !fields.includes("runs")) ||
3247-
(window === undefined && matchesNothing(filter?.runIds))
3257+
(window === undefined &&
3258+
(matchesNothing(filter?.runIds) ||
3259+
(matchesNothing(filter?.runStatuses) && filter?.includeLatestRun !== true)))
32483260
? Effect.succeed([])
32493261
: window === undefined
32503262
? sql<PayloadRow>`
32513263
SELECT payload_json
32523264
FROM orchestration_v2_projection_runs
32533265
WHERE thread_id = ${threadId}
32543266
${filter?.runIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.runIds)}))`}
3267+
${
3268+
filter?.runStatuses === undefined
3269+
? sql``
3270+
: sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
3271+
OR (${filter.includeLatestRun === true ? 1 : 0} = 1 AND run_id = (
3272+
SELECT run_id FROM orchestration_v2_projection_runs
3273+
WHERE thread_id = ${threadId}
3274+
ORDER BY ordinal DESC
3275+
LIMIT 1
3276+
)))`
3277+
}
32553278
ORDER BY ordinal ASC
32563279
`
32573280
: sql<PayloadRow>`
@@ -3421,15 +3444,24 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
34213444
ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC
34223445
`,
34233446
(fields !== undefined && !fields.includes("providerTurns")) ||
3424-
(window === undefined && matchesNothing(filter?.providerTurnAttemptIds))
3447+
(window === undefined &&
3448+
matchesNothing(filter?.providerTurnAttemptIds, filter?.providerTurnThreadIds))
34253449
? Effect.succeed([])
34263450
: window === undefined
34273451
? sql<PayloadRow>`
34283452
SELECT payload_json
34293453
FROM orchestration_v2_projection_provider_turns
34303454
WHERE thread_id = ${threadId}
34313455
${filter?.providerTurnAttemptIds === undefined ? sql`` : sql`AND run_attempt_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerTurnAttemptIds)}))`}
3432-
ORDER BY provider_thread_id ASC, ordinal ASC
3456+
${filter?.providerTurnThreadIds === undefined ? sql`` : sql`AND provider_thread_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerTurnThreadIds)}))`}
3457+
${
3458+
filter?.latestProviderTurnUsageOnly === true
3459+
? sql`AND json_extract(payload_json, '$.tokenUsage') IS NOT NULL
3460+
ORDER BY julianday(json_extract(payload_json, '$.tokenUsage.updatedAt')) DESC,
3461+
provider_thread_id ASC, ordinal ASC
3462+
LIMIT 1`
3463+
: sql`ORDER BY provider_thread_id ASC, ordinal ASC`
3464+
}
34333465
`
34343466
: sql<PayloadRow>`
34353467
SELECT payload_json FROM orchestration_v2_projection_provider_turns
@@ -3439,13 +3471,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
34393471
OR node_id IN (SELECT value FROM json_each(${cohortNodeIds})))
34403472
ORDER BY provider_thread_id ASC, ordinal ASC
34413473
`,
3442-
fields !== undefined && !fields.includes("runtimeRequests")
3474+
(fields !== undefined && !fields.includes("runtimeRequests")) ||
3475+
(window === undefined && matchesNothing(filter?.runtimeRequestStatuses))
34433476
? Effect.succeed([])
34443477
: window === undefined
34453478
? sql<PayloadRow>`
34463479
SELECT payload_json
34473480
FROM orchestration_v2_projection_runtime_requests
34483481
WHERE thread_id = ${threadId}
3482+
${filter?.runtimeRequestStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runtimeRequestStatuses)}))`}
34493483
ORDER BY created_at ASC, runtime_request_id ASC
34503484
`
34513485
: sql<PayloadRow>`
@@ -3560,13 +3594,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
35603594
)
35613595
ORDER BY rowid ASC
35623596
`,
3563-
fields !== undefined && !fields.includes("contextTransfers")
3597+
(fields !== undefined && !fields.includes("contextTransfers")) ||
3598+
(window === undefined && matchesNothing(filter?.contextTransferStatuses))
35643599
? Effect.succeed([])
35653600
: window === undefined
35663601
? sql<PayloadRow>`
35673602
SELECT payload_json
35683603
FROM orchestration_v2_projection_context_transfers
3569-
WHERE source_thread_id = ${threadId} OR target_thread_id = ${threadId}
3604+
WHERE (source_thread_id = ${threadId} OR target_thread_id = ${threadId})
3605+
${filter?.contextTransferStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.contextTransferStatuses)}))`}
35703606
ORDER BY rowid ASC
35713607
`
35723608
: sql<PayloadRow>`

0 commit comments

Comments
 (0)