Skip to content

Commit b08d559

Browse files
only21milclaude
andauthored
fix(server): command projection failures keep their cause (pingdotgg#28389)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 1c0cc43 commit b08d559

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
@@ -8881,7 +8881,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
88818881
const context = yield* projectionStore
88828882
.getRuntimeResponseContext(command.threadId, command.requestId)
88838883
.pipe(
8884-
Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })),
8884+
Effect.mapError(
8885+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
8886+
),
88858887
);
88868888
const runtimeRequest = context.request;
88878889
if (runtimeRequest === undefined) {
@@ -9294,7 +9296,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
92949296
const request = yield* projectionStore
92959297
.getRuntimeRequest(command.threadId, command.requestId)
92969298
.pipe(
9297-
Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })),
9299+
Effect.mapError(
9300+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
9301+
),
92989302
);
92999303
if (request === undefined || request.status !== "pending") {
93009304
return yield* new OrchestratorDispatchError({
@@ -9331,24 +9335,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
93319335
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
93329336
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
93339337
) {
9334-
const projection = yield* projectionStore
9335-
.getThreadRecords(
9336-
command.threadId,
9337-
[
9338-
"runs",
9339-
"nodes",
9340-
"attempts",
9341-
"messages",
9342-
"providerThreads",
9343-
"providerTurns",
9344-
"providerSessions",
9345-
"turnItems",
9346-
"runtimeRequests",
9347-
"subagents",
9348-
],
9349-
{ turnItemTypes: [], messageRoles: ["user"] },
9350-
)
9351-
.pipe(Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })));
9338+
const projection = yield* loadProjectionForCommand(
9339+
command,
9340+
[
9341+
"runs",
9342+
"nodes",
9343+
"attempts",
9344+
"messages",
9345+
"providerThreads",
9346+
"providerTurns",
9347+
"providerSessions",
9348+
"turnItems",
9349+
"runtimeRequests",
9350+
"subagents",
9351+
],
9352+
{ turnItemTypes: [], messageRoles: ["user"] },
9353+
);
93529354
if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) {
93539355
return yield* new OrchestratorDispatchError({
93549356
commandId: command.commandId,
@@ -9542,9 +9544,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
95429544
command: Extract<OrchestrationV2Command, { readonly type: "queued-run.reorder" }>,
95439545
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
95449546
) {
9545-
const projection = yield* projectionStore
9546-
.getThreadRecords(command.threadId, ["runs", "messages"], { messageRoles: ["user"] })
9547-
.pipe(Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })));
9547+
const projection = yield* loadProjectionForCommand(command, ["runs", "messages"], {
9548+
messageRoles: ["user"],
9549+
});
95489550
const queuedRuns = queuedRunsInDeliveryOrder(projection);
95499551
const moving = queuedRuns.find((run) => run.id === command.runId);
95509552
if (moving === undefined) {
@@ -9628,13 +9630,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
96289630
command: Extract<OrchestrationV2Command, { readonly type: "queued-run.cancel" }>,
96299631
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
96309632
) {
9631-
const projection = yield* projectionStore
9632-
.getThreadRecords(
9633-
command.threadId,
9634-
["runs", "messages", "nodes", "attempts", "subagents", "turnItems"],
9635-
{ turnItemTypes: [], messageRoles: ["user"] },
9636-
)
9637-
.pipe(Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })));
9633+
const projection = yield* loadProjectionForCommand(
9634+
command,
9635+
["runs", "messages", "nodes", "attempts", "subagents", "turnItems"],
9636+
{ turnItemTypes: [], messageRoles: ["user"] },
9637+
);
96389638
const queuedRun = projection.runs.find((candidate) => candidate.id === command.runId);
96399639
if (queuedRun === undefined || queuedRun.status !== "queued") {
96409640
return yield* new OrchestratorDispatchError({
@@ -9739,12 +9739,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
97399739
cause: `Queued run ${command.runId} cannot be edited to an empty message.`,
97409740
});
97419741
}
9742-
const projection = yield* projectionStore
9743-
.getThreadRecords(command.threadId, ["runs", "messages", "turnItems"], {
9744-
turnItemTypes: ["user_message"],
9745-
messageRoles: ["user"],
9746-
})
9747-
.pipe(Effect.mapError(() => new OrchestratorProjectionError({ threadId: command.threadId })));
9742+
const projection = yield* loadProjectionForCommand(command, ["runs", "messages", "turnItems"], {
9743+
turnItemTypes: ["user_message"],
9744+
messageRoles: ["user"],
9745+
});
97489746
const queuedRun = projection.runs.find((candidate) => candidate.id === command.runId);
97499747
if (queuedRun === undefined || queuedRun.status !== "queued") {
97509748
return yield* new OrchestratorDispatchError({
@@ -9842,7 +9840,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
98429840
.getThreadRecords(commandThreadId(command), fields, filter)
98439841
.pipe(
98449842
Effect.mapError(
9845-
() => new OrchestratorProjectionError({ threadId: commandThreadId(command) }),
9843+
(cause) => new OrchestratorProjectionError({ threadId: commandThreadId(command), cause }),
98469844
),
98479845
);
98489846

0 commit comments

Comments
 (0)