Skip to content

Commit e27ead9

Browse files
t3dotggclaude
andcommitted
fix(server): Stop gates late agent watches on the last executed run
A queued message the user cancelled counted as the latest run, so a late agent watch checked it instead of the stopped run. The watch gate now uses the last executed run, and thread.stop marks that run plus any run a restart cut that still awaits its continuation. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c51f0d5 commit e27ead9

2 files changed

Lines changed: 65 additions & 31 deletions

File tree

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

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23712371
});
23722372
}
23732373
// Only the agent's watch_pull_request links as "agent". A call that raced a Stop may land
2374-
// after its run ended, so the latest run decides. A user can still watch after a Stop.
2374+
// after its run ended, so the latest run that executed decides. A user can still watch.
23752375
if (
23762376
command.type === "thread.pull-request.watch" &&
23772377
command.watching &&
@@ -2380,8 +2380,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23802380
const { runs } = yield* projectionStore
23812381
.getThreadRecords(command.threadId, ["runs"])
23822382
.pipe(mapDispatchError(command));
2383-
const latest = latestDequeuedRun(runs);
2384-
if (latest !== undefined && (yield* stopReachedRun(command, command.threadId, latest.id))) {
2383+
const latest = latestExecutedRun(runs);
2384+
if (latest !== null && (yield* stopReachedRun(command, command.threadId, latest.id))) {
23852385
return yield* new OrchestratorDispatchError({
23862386
commandId: command.commandId,
23872387
commandType: command.type,
@@ -7997,15 +7997,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
79977997
mapDispatchError(command),
79987998
);
79997999

8000-
/**
8001-
* The thread's latest run that left the queue: the one an agent's tool call or a restart
8002-
* continuation can come from. A run a restart cut before its provider started counts.
8003-
*/
8004-
const latestDequeuedRun = (runs: ReadonlyArray<OrchestrationV2Run>) =>
8005-
runs
8006-
.filter((run) => run.status !== "queued")
8007-
.toSorted((left, right) => right.ordinal - left.ordinal)[0];
8008-
80098000
/**
80108001
* What Stop holds besides the run it interrupts: queued runs wait for the user, the
80118002
* thread's pull request watches end, and every wake its delegated tasks still owe is
@@ -8588,39 +8579,53 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
85888579
});
85898580
}
85908581
const now = yield* DateTime.now;
8591-
// Record that Stop reached the thread's finished run too. A late tool call from its
8592-
// agent then starts nothing, and a restart that cut it does not continue it.
8593-
const latest = latestDequeuedRun(projection.runs);
8594-
if (
8595-
latest !== undefined &&
8596-
!hasLiveRun({ runs: [latest] }) &&
8597-
latest.rootNodeId !== null &&
8598-
latest.providerThreadId !== null &&
8599-
!(yield* stopReachedRun(command, command.threadId, latest.id))
8600-
) {
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.
8584+
const executed = latestExecutedRun(projection.runs);
8585+
const reached = new Map<RunId, OrchestrationV2Run>();
8586+
if (executed !== null && !hasLiveRun({ runs: [executed] }))
8587+
reached.set(executed.id, executed);
8588+
for (const run of projection.runs) {
8589+
if (
8590+
run.status === "cancelled" &&
8591+
(executed === null || run.ordinal >= executed.ordinal) &&
8592+
(yield* awaitsRestartContinuation(run).pipe(mapDispatchError(command)))
8593+
) {
8594+
reached.set(run.id, run);
8595+
}
8596+
}
8597+
let ordinal = yield* nextTurnItemOrdinal(projection);
8598+
for (const run of reached.values()) {
8599+
if (
8600+
run.rootNodeId === null ||
8601+
run.providerThreadId === null ||
8602+
(yield* stopReachedRun(command, command.threadId, run.id))
8603+
) {
8604+
continue;
8605+
}
86018606
yield* emit(
86028607
events,
86038608
command,
86048609
)({
86058610
type: "turn-item.updated",
86068611
threadId: command.threadId,
8607-
runId: latest.id,
8608-
nodeId: latest.rootNodeId,
8609-
providerInstanceId: latest.providerInstanceId,
8612+
runId: run.id,
8613+
nodeId: run.rootNodeId,
8614+
providerInstanceId: run.providerInstanceId,
86108615
occurredAt: now,
86118616
payload: {
86128617
id: idAllocator.derive.runSignalTurnItem({
8613-
runId: latest.id,
8618+
runId: run.id,
86148619
signal: "interrupt-request",
86158620
}),
86168621
threadId: command.threadId,
8617-
runId: latest.id,
8618-
nodeId: latest.rootNodeId,
8619-
providerThreadId: latest.providerThreadId,
8622+
runId: run.id,
8623+
nodeId: run.rootNodeId,
8624+
providerThreadId: run.providerThreadId,
86208625
providerTurnId: null,
86218626
nativeItemRef: null,
86228627
parentItemId: null,
8623-
ordinal: yield* nextTurnItemOrdinal(projection),
8628+
ordinal: ordinal++,
86248629
status: "completed",
86258630
title: "Interrupt requested",
86268631
startedAt: now,

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import * as DateTime from "effect/DateTime";
1414
import * as Effect from "effect/Effect";
1515
import * as Exit from "effect/Exit";
1616
import * as Layer from "effect/Layer";
17+
import * as Schema from "effect/Schema";
1718
import * as SqlClient from "effect/unstable/sql/SqlClient";
1819

1920
import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
2021
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
22+
import { OrchestrationEffectRequestV2 } from "./EffectOutbox.ts";
2123
import * as Orchestrator from "./Orchestrator.ts";
2224
import * as ProjectionStore from "./ProjectionStore.ts";
2325
import type { ProviderAdapterV2Shape } from "./ProviderAdapter.ts";
@@ -50,6 +52,8 @@ const testLayer = ThreadManagementService.layer.pipe(
5052
),
5153
);
5254

55+
const encodeEffectRequest = Schema.encodeSync(Schema.fromJsonString(OrchestrationEffectRequestV2));
56+
5357
const pullRequest = (number: number) => ({
5458
host: "github.com",
5559
repository: "pingdotgg/t3code",
@@ -269,6 +273,15 @@ it.effect("a run Stop reached cannot delegate or start a watch, even after it en
269273
occurredAt: now,
270274
payload: { ...run, status: "running" },
271275
});
276+
// A queued message the user cancelled does not count as the latest run.
277+
yield* send(threadId, "queued", "queue_after_active");
278+
const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
279+
yield* orchestrator.dispatch({
280+
type: "queued-run.cancel",
281+
commandId: CommandId.make("cancel-queued"),
282+
threadId,
283+
runId: queued.id,
284+
});
272285
// Stop reached the run, but its provider has not stopped it yet.
273286
yield* projections.apply({
274287
id: EventId.make("event:stop-barrier:interrupt-request"),
@@ -309,7 +322,10 @@ it.effect("a run Stop reached cannot delegate or start a watch, even after it en
309322
payload: { ...run, status: "interrupted", completedAt: now },
310323
});
311324
assert.isTrue(Exit.isFailure(yield* Effect.exit(watch(threadId, 5))));
312-
assert.deepEqual(yield* threadState(threadId), { runs: ["interrupted"], watched: [4] });
325+
assert.deepEqual(yield* threadState(threadId), {
326+
runs: ["interrupted", "cancelled"],
327+
watched: [4],
328+
});
313329

314330
// The user can still stop and restart a watch by hand.
315331
for (const watching of [false, true]) {
@@ -329,6 +345,7 @@ it.effect("thread.stop keeps a restart continuation of the stopped run from star
329345
Effect.gen(function* () {
330346
const orchestrator = yield* Orchestrator.OrchestratorV2;
331347
const projections = yield* ProjectionStore.ProjectionStoreV2;
348+
const sql = yield* SqlClient.SqlClient;
332349
const threadId = ThreadId.make("thread:stop-restart");
333350
yield* createWatchingThread(threadId, 8);
334351
yield* send(threadId, "work", "start_immediately");
@@ -343,6 +360,18 @@ it.effect("thread.stop keeps a restart continuation of the stopped run from star
343360
occurredAt: now,
344361
payload: { ...run, status: "cancelled", startedAt: null, completedAt: now },
345362
});
363+
const at = DateTime.formatIso(now);
364+
yield* sql`
365+
INSERT INTO orchestration_v2_effect_outbox (
366+
effect_id, command_id, thread_id, effect_type, payload_json, status,
367+
attempt_count, available_at, created_at, updated_at
368+
) VALUES (
369+
${`effect:restart-continuation:${run.id}`}, 'command:restart', ${threadId},
370+
'provider-runtime.continue',
371+
${encodeEffectRequest({ type: "provider-runtime.continue", sourceRunId: run.id })},
372+
'pending', 0, ${at}, ${at}, ${at}
373+
)
374+
`;
346375

347376
yield* orchestrator.dispatch({
348377
type: "thread.stop",

0 commit comments

Comments
 (0)