From 0ac8024a02ca0bb28f506a82fc26f767e3f3e594 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 3 Jul 2026 09:50:26 -0700 Subject: [PATCH] Surface runtime-reconcile cancellations as visible turn items MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../ProviderRuntimeRecoveryService.test.ts | 3 ++ .../ProviderRuntimeRecoveryService.ts | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts b/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts index 932beec899..a048a8ec6e 100644 --- a/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts @@ -515,6 +515,9 @@ it.effect( ["provider-turn.updated", "cancelled"], ["message.updated", null], ["turn-item.updated", "cancelled"], + // Synthesized "Run interrupted" notice: reconcile cancellations + // must be user-visible, not silent. + ["turn-item.updated", "cancelled"], ["provider-thread.updated", "idle"], ["provider-session.updated", "stopped"], ], diff --git a/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts b/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts index 5dadaa7b01..e9bbc3e03a 100644 --- a/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts +++ b/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts @@ -2,6 +2,7 @@ import { CommandId, type OrchestrationV2DomainEvent, type OrchestrationV2ThreadProjection, + type OrchestrationV2TurnItem, ThreadId, } from "@t3tools/contracts"; import * as Context from "effect/Context"; @@ -15,6 +16,7 @@ import * as EffectOutbox from "./EffectOutbox.ts"; import * as EventSink from "./EventSink.ts"; import * as IdAllocator from "./IdAllocator.ts"; import * as ProjectionStore from "./ProjectionStore.ts"; +import { makeProviderFailure } from "./ProviderFailure.ts"; export class ProviderRuntimeRecoveryError extends Schema.TaggedErrorClass()( "ProviderRuntimeRecoveryError", @@ -120,6 +122,10 @@ export const make = Effect.gen(function* () { ), ); const events: Array = []; + // Thread-wide ordinals are UNIQUE-constrained in the positions table; + // synthesized cancellation notices append after everything projected. + let nextSynthesizedItemOrdinal = + (projection.turnItems ?? []).reduce((max, item) => Math.max(max, item.ordinal), 0) + 1; for (const request of requests) { events.push({ id: yield* allocateEventId(), @@ -252,6 +258,38 @@ export const make = Effect.gen(function* () { payload: { ...item, status: "cancelled", completedAt: now, updatedAt: now }, }); } + // Without a visible notice, a reconcile-cancelled run is + // indistinguishable from a user cancel: the user's message simply + // never gets an answer (audit plan #5, threads 721fc23c/48663fb7). + const cancellationNotice: OrchestrationV2TurnItem = { + id: ids.derive.runSignalTurnItem({ runId: run.id, signal: "runtime-reconcile" }), + threadId: projection.thread.id, + runId: run.id, + nodeId: run.rootNodeId, + providerThreadId: run.providerThreadId, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: nextSynthesizedItemOrdinal, + status: "cancelled", + title: "Run interrupted", + startedAt: now, + completedAt: now, + updatedAt: now, + type: "error", + failure: makeProviderFailure({ message: detail, class: "transport_error" }), + }; + nextSynthesizedItemOrdinal += 1; + events.push({ + id: yield* allocateEventId(), + type: "turn-item.updated", + threadId: projection.thread.id, + runId: run.id, + ...(run.rootNodeId === null ? {} : { nodeId: run.rootNodeId }), + providerInstanceId: run.providerInstanceId, + occurredAt: now, + payload: cancellationNotice, + }); } for (const providerThread of projection.providerThreads.filter( (candidate) => candidate.status === "active",