Skip to content

Commit 0215f58

Browse files
only21milclaude
andcommitted
fix(server): command projection failures keep their cause
Seven Orchestrator projection reads mapped store failures to OrchestratorProjectionError without a cause, so SQLite and decode errors showed only the generic 'Failed to load orchestration projection' message in the client and in logs. Those reads now pass the store error as the cause, like the sibling sites do. Four queued-run and steer handlers now reuse loadProjectionForCommand instead of their own copy of the mapping. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 54c5f25 commit 0215f58

2 files changed

Lines changed: 84 additions & 37 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,55 @@ it.effect(
656656
}).pipe(Effect.provide(layerTest)),
657657
);
658658

659+
it.effect("keeps the store's read failure as the cause when a command projection fails", () =>
660+
Effect.gen(function* () {
661+
const orchestrator = yield* Orchestrator.OrchestratorV2;
662+
const sql = yield* SqlClient.SqlClient;
663+
const now = DateTime.formatIso(yield* DateTime.now);
664+
const unreadableMessage = ThreadId.make("thread:control-unreadable-message");
665+
const unreadableRequest = ThreadId.make("thread:control-unreadable-request");
666+
for (const threadId of [unreadableMessage, unreadableRequest]) {
667+
yield* orchestrator.dispatch({
668+
type: "thread.create",
669+
commandId: CommandId.make(`create:${threadId}`),
670+
threadId,
671+
projectId: ProjectId.make("project:control-unreadable"),
672+
title: "Unreadable",
673+
modelSelection,
674+
runtimeMode: "full-access",
675+
interactionMode: "default",
676+
branch: null,
677+
worktreePath: null,
678+
createdBy: "user",
679+
creationSource: "web",
680+
});
681+
}
682+
yield* sql`INSERT INTO orchestration_v2_projection_messages
683+
(message_id, thread_id, run_id, node_id, role, streaming, created_at, updated_at, payload_json)
684+
VALUES ('obsolete-user', ${unreadableMessage}, NULL, NULL, 'user', 0, ${now}, ${now}, '{"obsolete":true}')`;
685+
yield* sql`INSERT INTO orchestration_v2_projection_runtime_requests
686+
(runtime_request_id, thread_id, node_id, kind, status, created_at, payload_json)
687+
VALUES ('request:unreadable', ${unreadableRequest}, 'node:unreadable', 'user-input', 'pending', ${now}, '{}')`;
688+
for (const command of [
689+
{
690+
type: "queue.resume",
691+
commandId: CommandId.make("resume-unreadable"),
692+
threadId: unreadableMessage,
693+
},
694+
{
695+
type: "thread.user-input.dismiss",
696+
commandId: CommandId.make("dismiss-unreadable"),
697+
threadId: unreadableRequest,
698+
requestId: RuntimeRequestId.make("request:unreadable"),
699+
},
700+
] as const) {
701+
const failure = yield* orchestrator.dispatch(command).pipe(Effect.flip);
702+
assert.instanceOf(failure, Orchestrator.OrchestratorProjectionError);
703+
assert.instanceOf(failure.cause, ProjectionStore.ProjectionStoreReadError);
704+
}
705+
}).pipe(Effect.provide(layerTest)),
706+
);
707+
659708
it.effect("implements a proposed plan that the command projection leaves out", () =>
660709
Effect.gen(function* () {
661710
const orchestrator = yield* Orchestrator.OrchestratorV2;

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

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8793,7 +8793,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
87938793
const context = yield* projectionStore
87948794
.getRuntimeResponseContext(command.threadId, command.requestId)
87958795
.pipe(
8796-
Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })),
8796+
Effect.mapError(
8797+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
8798+
),
87978799
);
87988800
const runtimeRequest = context.request;
87998801
if (runtimeRequest === undefined) {
@@ -9206,7 +9208,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
92069208
const request = yield* projectionStore
92079209
.getRuntimeRequest(command.threadId, command.requestId)
92089210
.pipe(
9209-
Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })),
9211+
Effect.mapError(
9212+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
9213+
),
92109214
);
92119215
if (request === undefined || request.status !== "pending") {
92129216
return yield* new OrchestratorDispatchError({
@@ -9243,24 +9247,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
92439247
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
92449248
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
92459249
) {
9246-
const projection = yield* projectionStore
9247-
.getThreadRecords(
9248-
command.threadId,
9249-
[
9250-
"runs",
9251-
"nodes",
9252-
"attempts",
9253-
"messages",
9254-
"providerThreads",
9255-
"providerTurns",
9256-
"providerSessions",
9257-
"turnItems",
9258-
"runtimeRequests",
9259-
"subagents",
9260-
],
9261-
{ turnItemTypes: [], messageRoles: ["user"] },
9262-
)
9263-
.pipe(Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })));
9250+
const projection = yield* loadProjectionForCommand(
9251+
command,
9252+
[
9253+
"runs",
9254+
"nodes",
9255+
"attempts",
9256+
"messages",
9257+
"providerThreads",
9258+
"providerTurns",
9259+
"providerSessions",
9260+
"turnItems",
9261+
"runtimeRequests",
9262+
"subagents",
9263+
],
9264+
{ turnItemTypes: [], messageRoles: ["user"] },
9265+
);
92649266
if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) {
92659267
return yield* new OrchestratorDispatchError({
92669268
commandId: command.commandId,
@@ -9454,9 +9456,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
94549456
command: Extract<OrchestrationV2Command, { readonly type: "queued-run.reorder" }>,
94559457
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
94569458
) {
9457-
const projection = yield* projectionStore
9458-
.getThreadRecords(command.threadId, ["runs", "messages"], { messageRoles: ["user"] })
9459-
.pipe(Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })));
9459+
const projection = yield* loadProjectionForCommand(command, ["runs", "messages"], {
9460+
messageRoles: ["user"],
9461+
});
94609462
const queuedRuns = queuedRunsInDeliveryOrder(projection);
94619463
const moving = queuedRuns.find((run) => run.id === command.runId);
94629464
if (moving === undefined) {
@@ -9540,13 +9542,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
95409542
command: Extract<OrchestrationV2Command, { readonly type: "queued-run.cancel" }>,
95419543
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
95429544
) {
9543-
const projection = yield* projectionStore
9544-
.getThreadRecords(
9545-
command.threadId,
9546-
["runs", "messages", "nodes", "attempts", "subagents", "turnItems"],
9547-
{ turnItemTypes: [], messageRoles: ["user"] },
9548-
)
9549-
.pipe(Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })));
9545+
const projection = yield* loadProjectionForCommand(
9546+
command,
9547+
["runs", "messages", "nodes", "attempts", "subagents", "turnItems"],
9548+
{ turnItemTypes: [], messageRoles: ["user"] },
9549+
);
95509550
const queuedRun = projection.runs.find((candidate) => candidate.id === command.runId);
95519551
if (queuedRun === undefined || queuedRun.status !== "queued") {
95529552
return yield* new OrchestratorDispatchError({
@@ -9651,12 +9651,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
96519651
cause: `Queued run ${command.runId} cannot be edited to an empty message.`,
96529652
});
96539653
}
9654-
const projection = yield* projectionStore
9655-
.getThreadRecords(command.threadId, ["runs", "messages", "turnItems"], {
9656-
turnItemTypes: ["user_message"],
9657-
messageRoles: ["user"],
9658-
})
9659-
.pipe(Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })));
9654+
const projection = yield* loadProjectionForCommand(command, ["runs", "messages", "turnItems"], {
9655+
turnItemTypes: ["user_message"],
9656+
messageRoles: ["user"],
9657+
});
96609658
const queuedRun = projection.runs.find((candidate) => candidate.id === command.runId);
96619659
if (queuedRun === undefined || queuedRun.status !== "queued") {
96629660
return yield* new OrchestratorDispatchError({
@@ -9754,7 +9752,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
97549752
.getThreadRecords(commandThreadId(command), fields, filter)
97559753
.pipe(
97569754
Effect.mapError(
9757-
() => new OrchestratorProjectionError({ threadId: commandThreadId(command) }),
9755+
(cause) => new OrchestratorProjectionError({ threadId: commandThreadId(command), cause }),
97589756
),
97599757
);
97609758

0 commit comments

Comments
 (0)