Skip to content

Commit 0ac8024

Browse files
Surface runtime-reconcile cancellations as visible turn items
A run cancelled by the startup/shutdown reconcile was indistinguishable from a user cancel: the user's message simply never got an answer and no explanation appeared anywhere (audit plan #5, threads 721fc23c and 48663fb7 — cursor crash and server restart both ate the turn silently). The reconcile now appends a "Run interrupted" error item per terminalized run carrying the reason ("Cancelled because the server restarted/shut down before the provider work completed."), ordinal-appended after all projected items to respect the thread-wide position uniqueness. App-verified: killed the backend mid-turn; after restart the thread shows "Run interrupted — Cancelled because the server restarted before the provider work completed." Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent c86ed34 commit 0ac8024

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ it.effect(
515515
["provider-turn.updated", "cancelled"],
516516
["message.updated", null],
517517
["turn-item.updated", "cancelled"],
518+
// Synthesized "Run interrupted" notice: reconcile cancellations
519+
// must be user-visible, not silent.
520+
["turn-item.updated", "cancelled"],
518521
["provider-thread.updated", "idle"],
519522
["provider-session.updated", "stopped"],
520523
],

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CommandId,
33
type OrchestrationV2DomainEvent,
44
type OrchestrationV2ThreadProjection,
5+
type OrchestrationV2TurnItem,
56
ThreadId,
67
} from "@t3tools/contracts";
78
import * as Context from "effect/Context";
@@ -15,6 +16,7 @@ import * as EffectOutbox from "./EffectOutbox.ts";
1516
import * as EventSink from "./EventSink.ts";
1617
import * as IdAllocator from "./IdAllocator.ts";
1718
import * as ProjectionStore from "./ProjectionStore.ts";
19+
import { makeProviderFailure } from "./ProviderFailure.ts";
1820

1921
export class ProviderRuntimeRecoveryError extends Schema.TaggedErrorClass<ProviderRuntimeRecoveryError>()(
2022
"ProviderRuntimeRecoveryError",
@@ -120,6 +122,10 @@ export const make = Effect.gen(function* () {
120122
),
121123
);
122124
const events: Array<OrchestrationV2DomainEvent> = [];
125+
// Thread-wide ordinals are UNIQUE-constrained in the positions table;
126+
// synthesized cancellation notices append after everything projected.
127+
let nextSynthesizedItemOrdinal =
128+
(projection.turnItems ?? []).reduce((max, item) => Math.max(max, item.ordinal), 0) + 1;
123129
for (const request of requests) {
124130
events.push({
125131
id: yield* allocateEventId(),
@@ -252,6 +258,38 @@ export const make = Effect.gen(function* () {
252258
payload: { ...item, status: "cancelled", completedAt: now, updatedAt: now },
253259
});
254260
}
261+
// Without a visible notice, a reconcile-cancelled run is
262+
// indistinguishable from a user cancel: the user's message simply
263+
// never gets an answer (audit plan #5, threads 721fc23c/48663fb7).
264+
const cancellationNotice: OrchestrationV2TurnItem = {
265+
id: ids.derive.runSignalTurnItem({ runId: run.id, signal: "runtime-reconcile" }),
266+
threadId: projection.thread.id,
267+
runId: run.id,
268+
nodeId: run.rootNodeId,
269+
providerThreadId: run.providerThreadId,
270+
providerTurnId: null,
271+
nativeItemRef: null,
272+
parentItemId: null,
273+
ordinal: nextSynthesizedItemOrdinal,
274+
status: "cancelled",
275+
title: "Run interrupted",
276+
startedAt: now,
277+
completedAt: now,
278+
updatedAt: now,
279+
type: "error",
280+
failure: makeProviderFailure({ message: detail, class: "transport_error" }),
281+
};
282+
nextSynthesizedItemOrdinal += 1;
283+
events.push({
284+
id: yield* allocateEventId(),
285+
type: "turn-item.updated",
286+
threadId: projection.thread.id,
287+
runId: run.id,
288+
...(run.rootNodeId === null ? {} : { nodeId: run.rootNodeId }),
289+
providerInstanceId: run.providerInstanceId,
290+
occurredAt: now,
291+
payload: cancellationNotice,
292+
});
255293
}
256294
for (const providerThread of projection.providerThreads.filter(
257295
(candidate) => candidate.status === "active",

0 commit comments

Comments
 (0)