@devries-cameron20 / t3code Mirror · read-only / commits / bad610bec9
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 07:00 UTC

perf(server): idle compaction reads only the latest run, live work and newest usage (#29648)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 2, 2026 at 15:20 UTC bad610bec9c39ec5a43fe6598087dcefccc28893
3 files changed +222 -7
apps/server/src/orchestration-v2/IdleCompactionService.ts
+16 -1
@@ -50,6 +50,8 @@ export const make = Effect.gen(function* () {
50 )
51 )
52 return;
53 + const activeProviderThreadIds =
54 + thread.activeProviderThreadId === null ? [] : [thread.activeProviderThreadId];
55 const records = yield* projections.getThreadRecords(
56 threadId,
57 [
@@ -61,7 +63,20 @@ export const make = Effect.gen(function* () {
63 "turnItems",
64 "contextTransfers",
65 ],
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 + },
80 );
81 if (
82 resolveIdleCompaction(current, records.thread.providerInstanceId) !==
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+164
@@ -4,6 +4,7 @@ import {
4 EventId,
5 CommandId,
6 CheckpointId,
7 + ContextTransferId,
8 CheckpointRef,
9 CheckpointScopeId,
10 MessageId,
@@ -307,6 +308,169 @@ it.effect("record reads restrict provider histories to delivery identities", ()
308 }).pipe(Effect.provide(layerTest)),
309 );
310
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 +
474 it.effect("record reads filter notification sources without dropping matching message bodies", () =>
475 Effect.gen(function* () {
476 const store = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+42 -6
@@ -25,6 +25,7 @@ import type {
25 OrchestrationV2PendingRuntimeRequestSummary,
26 PlanId,
27 OrchestrationV2ConversationMessage,
28 + OrchestrationV2ContextTransfer,
29 OrchestrationV2DomainEvent,
30 OrchestrationV2OlderSettledCount,
31 OrchestrationV2OlderSettledCursor,
@@ -32,6 +33,7 @@ import type {
33 OrchestrationV2ProviderThread,
34 OrchestrationV2ProviderTurn,
35 OrchestrationV2Run,
36 + OrchestrationV2RuntimeRequest,
37 OrchestrationV2Subagent,
38 OrchestrationV2ThreadShellSnapshot,
39 OrchestrationV2ShellThreadStatus,
@@ -339,6 +341,14 @@ export interface ProjectionRecordFilter {
341 readonly turnItemRunIds?: ReadonlyArray<RunId | null>;
342 readonly turnItemNodeIds?: ReadonlyArray<NodeId>;
343 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"]>;
352 readonly subagentIds?: ReadonlyArray<NodeId>;
353 readonly subagentRunIds?: ReadonlyArray<RunId>;
354 readonly nodeIds?: ReadonlyArray<NodeId>;
@@ -3244,7 +3254,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3254 ] = yield* Effect.all([
3255 decodeThreadPayload(threadRow.payload_json),
3256 (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)))
3260 ? Effect.succeed([])
3261 : window === undefined
3262 ? sql<PayloadRow>`
@@ -3252,6 +3264,17 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3264 FROM orchestration_v2_projection_runs
3265 WHERE thread_id = ${threadId}
3266 ${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 + }
3278 ORDER BY ordinal ASC
3279 `
3280 : sql<PayloadRow>`
@@ -3421,7 +3444,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3444 ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC
3445 `,
3446 (fields !== undefined && !fields.includes("providerTurns")) ||
3424 - (window === undefined && matchesNothing(filter?.providerTurnAttemptIds))
3447 + (window === undefined &&
3448 + matchesNothing(filter?.providerTurnAttemptIds, filter?.providerTurnThreadIds))
3449 ? Effect.succeed([])
3450 : window === undefined
3451 ? sql<PayloadRow>`
@@ -3429,7 +3453,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3453 FROM orchestration_v2_projection_provider_turns
3454 WHERE thread_id = ${threadId}
3455 ${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 + }
3465 `
3466 : sql<PayloadRow>`
3467 SELECT payload_json FROM orchestration_v2_projection_provider_turns
@@ -3439,13 +3471,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3471 OR node_id IN (SELECT value FROM json_each(${cohortNodeIds})))
3472 ORDER BY provider_thread_id ASC, ordinal ASC
3473 `,
3442 - fields !== undefined && !fields.includes("runtimeRequests")
3474 + (fields !== undefined && !fields.includes("runtimeRequests")) ||
3475 + (window === undefined && matchesNothing(filter?.runtimeRequestStatuses))
3476 ? Effect.succeed([])
3477 : window === undefined
3478 ? sql<PayloadRow>`
3479 SELECT payload_json
3480 FROM orchestration_v2_projection_runtime_requests
3481 WHERE thread_id = ${threadId}
3482 + ${filter?.runtimeRequestStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runtimeRequestStatuses)}))`}
3483 ORDER BY created_at ASC, runtime_request_id ASC
3484 `
3485 : sql<PayloadRow>`
@@ -3560,13 +3594,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3594 )
3595 ORDER BY rowid ASC
3596 `,
3563 - fields !== undefined && !fields.includes("contextTransfers")
3597 + (fields !== undefined && !fields.includes("contextTransfers")) ||
3598 + (window === undefined && matchesNothing(filter?.contextTransferStatuses))
3599 ? Effect.succeed([])
3600 : window === undefined
3601 ? sql<PayloadRow>`
3602 SELECT payload_json
3603 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)}))`}
3606 ORDER BY rowid ASC
3607 `
3608 : sql<PayloadRow>`