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 21, 22:15 UTC
fix(server): idle compaction no longer runs in the middle of delegated work (#33863)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Byntham committed
Nov 11, 2026 at 15:14 UTC
e8afdb0624d4e2efb47402921525d3c53a26d8cb
2 files changed
+88
apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+61
@@ -754,6 +754,67 @@ it.layer(layerTest)("delegated completion delivery repairs", (it) => {
754
}),
755
);
756
757
+ it.effect.each([
758
+ { name: "no result is waiting", delivery: "none", compacts: true },
759
+ { name: "a result is waiting to be sent", delivery: "pending", compacts: false },
760
+ { name: "a result was just sent", delivery: "sent", compacts: false },
761
+ ] as const)("compacts before the cache expires only when $name", ({ delivery, compacts }) =>
762
+ Effect.gen(function* () {
763
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
764
+ const now = yield* DateTime.now;
765
+ const threadId = ThreadId.make(`thread:idle-compaction-delivery-${delivery}`);
766
+ const runId = RunId.make(`run:idle-compaction-delivery-${delivery}`);
767
+ const taskId = NodeId.make(`node:idle-compaction-delivery-${delivery}-task`);
768
+
769
+ yield* seedParentWithTerminalTask({
770
+ threadId,
771
+ projectId: ProjectId.make(`project:idle-compaction-delivery-${delivery}`),
772
+ runId,
773
+ rootNodeId: NodeId.make(`node:idle-compaction-delivery-${delivery}-root`),
774
+ taskId,
775
+ deliveryState: delivery === "none" ? "delivered" : "claimed",
776
+ completionWake: "always",
777
+ parentRunStatus: "completed",
778
+ ...(delivery === "none" ? {} : { deliveryTaskIds: [taskId] }),
779
+ now,
780
+ });
781
+ if (delivery === "sent") {
782
+ yield* orchestrator.dispatch({
783
+ type: "message.dispatch",
784
+ commandId: CommandId.make(`command:idle-compaction-delivery-${delivery}`),
785
+ threadId,
786
+ messageId: MessageId.make(`message:delegated-delivery:${threadId}`),
787
+ text: `Delegated task ${taskId} reached a terminal state.`,
788
+ attachments: [],
789
+ dispatchMode: { type: "queue_after_active" },
790
+ createdBy: "agent",
791
+ creationSource: "server",
792
+ delegatedCompletion: { parentRunId: runId, generation: 1, taskIds: [taskId] },
793
+ });
794
+ }
795
+
796
+ const compactionId = MessageId.make(`idle-compact:${threadId}:${runId}`);
797
+ yield* orchestrator.dispatch({
798
+ type: "message.dispatch",
799
+ commandId: CommandId.make(`server:idle-compact:${threadId}:${runId}`),
800
+ threadId,
801
+ messageId: compactionId,
802
+ text: "/compact",
803
+ attachments: [],
804
+ createdBy: "agent",
805
+ creationSource: "server",
806
+ idleCompaction: "beforeCacheExpires",
807
+ dispatchMode: { type: "start_immediately" },
808
+ });
809
+
810
+ const projection = yield* orchestrator.getThreadProjection(threadId);
811
+ assert.equal(
812
+ projection.messages.some((message) => message.id === compactionId),
813
+ compacts,
814
+ );
815
+ }),
816
+ );
817
+
818
it.effect.each([
819
{
820
name: "oversized UTF-8 result",
apps/server/src/orchestration-v2/Orchestrator.ts
+27
@@ -6184,6 +6184,33 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
6184
}
6185
}
6186
6187
+ // The service checked idleness before this command; a run or a delegated result that landed
6188
+ // since makes the thread busy, and that turn refreshes the cache anyway.
6189
+ if (
6190
+ command.idleCompaction === "beforeCacheExpires" &&
6191
+ projection.runs.some(
6192
+ (run) =>
6193
+ run.status === "queued" ||
6194
+ isActiveRun(run) ||
6195
+ (run.delegatedCompletion?.disposition === "open" &&
6196
+ run.delegatedCompletion.delivery !== null &&
6197
+ !projection.messages.some(
6198
+ (message) => message.id === run.delegatedCompletion?.delivery?.messageId,
6199
+ )),
6200
+ )
6201
+ ) {
6202
+ yield* emit(
6203
+ events,
6204
+ command,
6205
+ )({
6206
+ type: "thread.metadata-updated",
6207
+ threadId: command.threadId,
6208
+ occurredAt: yield* DateTime.now,
6209
+ payload: projection.thread,
6210
+ });
6211
+ return;
6212
+ }
6213
+
6214
if (projection.thread.lastVisitedAt === null && !isNativeMaintenanceCommand(command)) {
6215
const now = yield* DateTime.now;
6216
// Null keeps imported history read. The first fresh dispatch establishes