Skip to content
TypeScript

fix(server): keep corrupt plan JSON in the typed read error (#21969)

Julius Marmingec2fb438b38f5
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts
index 8f416f156f8..64635a75d9d 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts
@@ -11,6 +11,7 @@ import {
type OrchestrationV2ProviderThread,
type OrchestrationV2ThreadShellSnapshot,
NodeId,
+ PlanId,
ProjectId,
ProviderDriverKind,
ProviderInstanceId,
@@ -997,6 +998,30 @@ it.effect("memory shell snapshots scope to one project", () =>
);
it.layer(layerTest)("ProjectionStoreV2", (it) => {
+ it.effect("returns a typed read error for malformed stored plan JSON", () =>
+ Effect.gen(function* () {
+ const store = yield* ProjectionStore.ProjectionStoreV2;
+ const sql = yield* SqlClient.SqlClient;
+ const threadId = yield* addRolledBackRecoveryCandidate("corrupt-plan");
+ const planId = PlanId.make("plan:corrupt");
+ yield* sql`INSERT INTO orchestration_v2_projection_plans
+ (plan_id, thread_id, node_id, kind, status, payload_json)
+ VALUES (${planId}, ${threadId}, 'node:corrupt-plan', 'proposed_plan', 'active', '{broken')`;
+
+ const failure = yield* store
+ .getPlan(threadId, planId)
+ .pipe(
+ Effect.flip,
+ Effect.ensuring(
+ Effect.orDie(
+ sql`DELETE FROM orchestration_v2_projection_plans WHERE plan_id = ${planId}`,
+ ),
+ ),
+ );
+ assert.strictEqual(failure._tag, "ProjectionStoreReadError");
+ }),
+ );
+
it.effect(
"SQL recovery selects unfinished runless native subagents",
() => runlessNativeSubagentNeedsRecovery,
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts
index f29472aaad8..71251df7176 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts
@@ -1148,6 +1148,7 @@ const encodeMessagePayload = Schema.encodeEffect(
const encodePlanPayload = Schema.encodeEffect(
Schema.fromJsonString(OrchestrationV2PlanArtifactSchema),
);
+const decodePlanPayload = (json: string) => decodePlanJson(json);
const encodeTurnItemPayload = Schema.encodeEffect(
Schema.fromJsonString(OrchestrationV2TurnItemJsonSchema),
);
@@ -1194,8 +1195,9 @@ const decodeRuntimeRequestPayload = Schema.decodeUnknownEffect(
const decodeMessagePayload = Schema.decodeUnknownEffect(
Schema.fromJsonString(OrchestrationV2ConversationMessageJsonSchema),
);
-const decodePlanArtifact = Schema.decodeUnknownEffect(OrchestrationV2PlanArtifactSchema);
-const decodePlanPayload = (json: string) => decodePlanArtifact(parseEncodedPayload(json));
+const decodePlanJson = Schema.decodeUnknownEffect(
+ Schema.fromJsonString(OrchestrationV2PlanArtifactSchema),
+);
const decodeTurnItemPayload = Schema.decodeUnknownEffect(
Schema.fromJsonString(OrchestrationV2TurnItemJsonSchema),
);