Skip to content

Commit b2fa5d3

Browse files
saphidcodexgithub-actions[bot]
authored
perf(server): stop each delegated child conversation once (#18826)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 9d4038f commit b2fa5d3

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,9 +1096,13 @@ const make = Effect.gen(function* () {
10961096
const failures: Array<
10971097
Orchestrator.OrchestratorV2Error | ThreadManagementProviderInstanceDisabledError
10981098
> = [];
1099+
const stoppedChildThreadIds = new Set<ThreadId>();
10991100
for (const task of subagents) {
11001101
if (task.origin !== "app_owned" || task.childThreadId === null) continue;
11011102
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);
11021106
yield* dispatch({
11031107
type: "thread.stop",
11041108
commandId: CommandId.make(`${input.commandId}:stop:${threadId}`),

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as DateTime from "effect/DateTime";
2020
import * as Effect from "effect/Effect";
2121
import * as Exit from "effect/Exit";
2222
import * as Layer from "effect/Layer";
23+
import * as Option from "effect/Option";
2324
import * as Schema from "effect/Schema";
2425
import * as SqlClient from "effect/sql/SqlClient";
2526
import * as Stream from "effect/Stream";
@@ -483,6 +484,76 @@ it.effect("Stop ends watches, holds queues, and stops the delegated tasks under
483484
}).pipe(Effect.provide(layerTest)),
484485
);
485486

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+
486557
it.effect.each(["thread.stop", "run.interrupt"] as const)(
487558
"%s cancels buffered wakes and preserves durable queued messages",
488559
(type) =>

0 commit comments

Comments
 (0)