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 23, 00:30 UTC
perf(server): stop each delegated child conversation once (#18826)
Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Alex Southwell committed
Oct 19, 2026 at 23:59 UTC
b2fa5d3e5ccb4a296fb51e569a5eb3023288e932
2 files changed
+75
apps/server/src/orchestration-v2/ThreadManagementService.ts
+4
@@ -1096,9 +1096,13 @@ const make = Effect.gen(function* () {
1096
const failures: Array<
1097
Orchestrator.OrchestratorV2Error | ThreadManagementProviderInstanceDisabledError
1098
> = [];
1099
+ const stoppedChildThreadIds = new Set<ThreadId>();
1100
for (const task of subagents) {
1101
if (task.origin !== "app_owned" || task.childThreadId === null) continue;
1102
const threadId = task.childThreadId;
1103
+ // Follow-up tasks reuse a child thread; walk its subtree only once.
1104
+ if (stoppedChildThreadIds.has(threadId)) continue;
1105
+ stoppedChildThreadIds.add(threadId);
1106
yield* dispatch({
1107
type: "thread.stop",
1108
commandId: CommandId.make(`${input.commandId}:stop:${threadId}`),
apps/server/src/orchestration-v2/ThreadStop.test.ts
+71
@@ -20,6 +20,7 @@ import * as DateTime from "effect/DateTime";
20
import * as Effect from "effect/Effect";
21
import * as Exit from "effect/Exit";
22
import * as Layer from "effect/Layer";
23
+import * as Option from "effect/Option";
24
import * as Schema from "effect/Schema";
25
import * as SqlClient from "effect/sql/SqlClient";
26
import * as Stream from "effect/Stream";
@@ -483,6 +484,76 @@ it.effect("Stop ends watches, holds queues, and stops the delegated tasks under
484
}).pipe(Effect.provide(layerTest)),
485
);
486
487
+it.effect.each([1, 2, 8])(
488
+ "Stop visits each descendant once with %s historical tasks per child",
489
+ (tasksPerChild) =>
490
+ Effect.gen(function* () {
491
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
492
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
493
+ const receipts = yield* CommandReceiptStore.CommandReceiptStoreV2;
494
+ const rootThreadId = ThreadId.make("thread:stop-history");
495
+ yield* createWatchingThread(rootThreadId, 30);
496
+ yield* send(rootThreadId, "work", "start_immediately");
497
+ const descendants: Array<ThreadId> = [];
498
+ let parentThreadId = rootThreadId;
499
+ for (let depth = 0; depth < 3; depth++) {
500
+ const childThreadId = yield* delegate(parentThreadId, `history child ${depth}`);
501
+ const task = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents[0]!;
502
+ // Published tasks and later follow-ups all retain the same child thread.
503
+ for (let history = 1; history < tasksPerChild; history++) {
504
+ const taskId = NodeId.make(`node:stop-history:${depth}:${history}`);
505
+ yield* projections.apply({
506
+ id: EventId.make(`event:stop-history:${depth}:${history}`),
507
+ type: "subagent.updated",
508
+ threadId: parentThreadId,
509
+ runId: task.runId!,
510
+ nodeId: taskId,
511
+ occurredAt: yield* DateTime.now,
512
+ payload: { ...task, id: taskId, status: "completed" },
513
+ });
514
+ }
515
+ descendants.push(childThreadId);
516
+ parentThreadId = childThreadId;
517
+ }
518
+
519
+ const stopped: Array<ThreadId> = [];
520
+ const read: Array<ThreadId> = [];
521
+ const measuredOrchestrator = Orchestrator.OrchestratorV2.of({
522
+ ...orchestrator,
523
+ dispatch: (command) =>
524
+ Effect.sync(() => {
525
+ if (command.type === "thread.stop") stopped.push(command.threadId);
526
+ }).pipe(Effect.andThen(orchestrator.dispatch(command))),
527
+ getThreadRecords: (threadId, fields, filter) =>
528
+ Effect.sync(() => read.push(threadId)).pipe(
529
+ Effect.andThen(orchestrator.getThreadRecords(threadId, fields, filter)),
530
+ ),
531
+ });
532
+ const commandId = CommandId.make("stop-history");
533
+ yield* Effect.gen(function* () {
534
+ const threads = yield* ThreadManagementService.ThreadManagementService;
535
+ yield* threads.stopDelegatedTasks({ threadId: rootThreadId, commandId });
536
+ }).pipe(
537
+ Effect.provide(
538
+ Layer.fresh(ThreadManagementService.layer).pipe(
539
+ Layer.provide(Layer.succeed(Orchestrator.OrchestratorV2, measuredOrchestrator)),
540
+ ),
541
+ ),
542
+ );
543
+
544
+ // Check real persisted state and receipts before asserting traversal cost.
545
+ for (const threadId of descendants) {
546
+ assert.deepEqual((yield* threadState(threadId)).runs, ["interrupted"]);
547
+ const receipt = yield* receipts.getByCommandId(
548
+ CommandId.make(`${commandId}:stop:${threadId}`),
549
+ );
550
+ assert.isTrue(Option.isSome(receipt));
551
+ }
552
+ assert.deepEqual(stopped, descendants);
553
+ assert.deepEqual(read, [rootThreadId, ...descendants]);
554
+ }).pipe(Effect.provide(layerTest)),
555
+);
556
+
557
it.effect.each(["thread.stop", "run.interrupt"] as const)(
558
"%s cancels buffered wakes and preserves durable queued messages",
559
(type) =>