Skip to content

Commit 1ae00df

Browse files
t3dotggclaude
andcommitted
fix(server): thread.stop marks a turn it could not interrupt
When a turn's provider session was gone, thread.stop held the thread but left the live run unmarked, so a late agent watch was accepted. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e27ead9 commit 1ae00df

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8579,10 +8579,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
85798579
});
85808580
}
85818581
const now = yield* DateTime.now;
8582-
// Record that Stop reached the thread's last executed run, so a late tool call from its
8583-
// agent starts nothing, and every run a restart cut, so its continuation never starts.
8582+
// Record that Stop reached the turn it could not interrupt and the thread's last executed
8583+
// run, so a late tool call from their agent starts nothing, and every run a restart cut,
8584+
// so its continuation never starts.
85848585
const executed = latestExecutedRun(projection.runs);
85858586
const reached = new Map<RunId, OrchestrationV2Run>();
8587+
if (target !== undefined) reached.set(target.id, target);
85868588
if (executed !== null && !hasLiveRun({ runs: [executed] }))
85878589
reached.set(executed.id, executed);
85888590
for (const run of projection.runs) {

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ProjectId,
88
ProviderDriverKind,
99
ProviderInstanceId,
10+
ProviderTurnId,
1011
ThreadId,
1112
TurnItemId,
1213
} from "@t3tools/contracts";
@@ -459,3 +460,49 @@ it.effect("thread.stop on a finished thread refuses a late agent watch", () =>
459460
assert.deepEqual(yield* threadState(threadId), { runs: ["completed"], watched: [] });
460461
}).pipe(Effect.provide(testLayer)),
461462
);
463+
464+
it.effect("thread.stop marks a turn it cannot interrupt so a late agent watch is refused", () =>
465+
Effect.gen(function* () {
466+
const orchestrator = yield* Orchestrator.OrchestratorV2;
467+
const projections = yield* ProjectionStore.ProjectionStoreV2;
468+
const threadId = ThreadId.make("thread:stop-lost-session");
469+
yield* createWatchingThread(threadId, 11);
470+
yield* send(threadId, "work", "start_immediately");
471+
const now = yield* DateTime.now;
472+
const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
473+
// The turn is running, but its provider session is gone, so the interrupt fails.
474+
yield* projections.apply({
475+
id: EventId.make("event:stop-lost-session:running"),
476+
type: "run.updated",
477+
threadId,
478+
runId: run.id,
479+
occurredAt: now,
480+
payload: { ...run, status: "running", startedAt: now },
481+
});
482+
yield* projections.apply({
483+
id: EventId.make("event:stop-lost-session:turn"),
484+
type: "provider-turn.updated",
485+
threadId,
486+
occurredAt: now,
487+
payload: {
488+
id: ProviderTurnId.make("provider-turn:stop-lost-session"),
489+
providerThreadId: run.providerThreadId!,
490+
nodeId: run.rootNodeId!,
491+
runAttemptId: run.activeAttemptId!,
492+
nativeTurnRef: null,
493+
ordinal: 1,
494+
status: "running",
495+
startedAt: now,
496+
completedAt: null,
497+
},
498+
});
499+
500+
yield* orchestrator.dispatch({
501+
type: "thread.stop",
502+
commandId: CommandId.make("stop-lost-session"),
503+
threadId,
504+
});
505+
assert.isTrue(Exit.isFailure(yield* Effect.exit(watch(threadId, 12))));
506+
assert.deepEqual(yield* threadState(threadId), { runs: ["running"], watched: [] });
507+
}).pipe(Effect.provide(testLayer)),
508+
);

0 commit comments

Comments
 (0)