Skip to content

Commit 2be91ff

Browse files
pc-styleclaude
andcommitted
fix(server): purge stale projections when a thread id is re-created
A re-created thread id (draft retry after a failed first-turn bootstrap) kept the old incarnation's message, activity, plan, session, turn, and pending-approval rows, so detail hydration could expose stale history. Each projector now clears its own rows when it applies thread.created, so per-projector replay from any cursor rebuilds the new incarnation correctly. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 911a6ef commit 2be91ff

2 files changed

Lines changed: 298 additions & 1 deletion

File tree

‎apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts‎

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,4 +2789,252 @@ engineLayer("OrchestrationProjectionPipeline via engine dispatch", (it) => {
27892789
]);
27902790
}),
27912791
);
2792+
2793+
it.effect("re-creating a deleted thread id purges the old incarnation's projections", () =>
2794+
Effect.gen(function* () {
2795+
const engine = yield* OrchestrationEngineService;
2796+
const sql = yield* SqlClient.SqlClient;
2797+
const createdAt = "2026-01-01T00:00:00.000Z";
2798+
const modelSelection = {
2799+
instanceId: ProviderInstanceId.make("codex"),
2800+
model: "gpt-5-codex",
2801+
};
2802+
2803+
yield* engine.dispatch({
2804+
type: "project.create",
2805+
commandId: CommandId.make("cmd-recreate-project"),
2806+
projectId: ProjectId.make("project-recreate"),
2807+
title: "Recreate Project",
2808+
workspaceRoot: "/tmp/project-recreate",
2809+
defaultModelSelection: modelSelection,
2810+
createdAt,
2811+
});
2812+
yield* engine.dispatch({
2813+
type: "thread.create",
2814+
commandId: CommandId.make("cmd-recreate-thread-1"),
2815+
threadId: ThreadId.make("thread-recreate"),
2816+
projectId: ProjectId.make("project-recreate"),
2817+
title: "First incarnation",
2818+
modelSelection,
2819+
interactionMode: "default",
2820+
runtimeMode: "full-access",
2821+
branch: null,
2822+
worktreePath: null,
2823+
createdAt,
2824+
});
2825+
yield* engine.dispatch({
2826+
type: "thread.turn.start",
2827+
commandId: CommandId.make("cmd-recreate-turn-1"),
2828+
threadId: ThreadId.make("thread-recreate"),
2829+
message: {
2830+
messageId: MessageId.make("msg-recreate-1"),
2831+
role: "user",
2832+
text: "old message",
2833+
attachments: [],
2834+
},
2835+
interactionMode: "default",
2836+
runtimeMode: "full-access",
2837+
createdAt,
2838+
});
2839+
yield* engine.dispatch({
2840+
type: "thread.delete",
2841+
commandId: CommandId.make("cmd-recreate-delete"),
2842+
threadId: ThreadId.make("thread-recreate"),
2843+
});
2844+
2845+
yield* engine.dispatch({
2846+
type: "thread.create",
2847+
commandId: CommandId.make("cmd-recreate-thread-2"),
2848+
threadId: ThreadId.make("thread-recreate"),
2849+
projectId: ProjectId.make("project-recreate"),
2850+
title: "Second incarnation",
2851+
modelSelection,
2852+
interactionMode: "default",
2853+
runtimeMode: "full-access",
2854+
branch: null,
2855+
worktreePath: null,
2856+
createdAt: "2026-01-01T00:00:01.000Z",
2857+
});
2858+
2859+
const threadRows = yield* sql<{
2860+
readonly title: string;
2861+
readonly deletedAt: string | null;
2862+
}>`
2863+
SELECT
2864+
title,
2865+
deleted_at AS "deletedAt"
2866+
FROM projection_threads
2867+
WHERE thread_id = 'thread-recreate'
2868+
`;
2869+
assert.deepEqual(threadRows, [{ title: "Second incarnation", deletedAt: null }]);
2870+
2871+
const messageRows = yield* sql<{ readonly messageId: string }>`
2872+
SELECT message_id AS "messageId"
2873+
FROM projection_thread_messages
2874+
WHERE thread_id = 'thread-recreate'
2875+
`;
2876+
assert.deepEqual(messageRows, []);
2877+
2878+
const turnRows = yield* sql<{ readonly threadId: string }>`
2879+
SELECT thread_id AS "threadId"
2880+
FROM projection_turns
2881+
WHERE thread_id = 'thread-recreate'
2882+
`;
2883+
assert.deepEqual(turnRows, []);
2884+
}),
2885+
);
27922886
});
2887+
2888+
it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-recreate-replay-")))(
2889+
"OrchestrationProjectionPipeline",
2890+
(it) => {
2891+
it.effect("per-projector replay keeps the new incarnation's rows after a re-create", () =>
2892+
Effect.gen(function* () {
2893+
const projectionPipeline = yield* OrchestrationProjectionPipeline;
2894+
const eventStore = yield* OrchestrationEventStore;
2895+
const sql = yield* SqlClient.SqlClient;
2896+
const now = "2026-01-01T00:00:00.000Z";
2897+
const modelSelection = {
2898+
instanceId: ProviderInstanceId.make("codex"),
2899+
model: "gpt-5-codex",
2900+
};
2901+
const threadCreatedPayload = {
2902+
threadId: ThreadId.make("thread-replay"),
2903+
projectId: ProjectId.make("project-replay"),
2904+
modelSelection,
2905+
runtimeMode: "full-access" as const,
2906+
branch: null,
2907+
worktreePath: null,
2908+
createdAt: now,
2909+
updatedAt: now,
2910+
};
2911+
2912+
yield* eventStore.append({
2913+
type: "project.created",
2914+
eventId: EventId.make("evt-replay-project"),
2915+
aggregateKind: "project",
2916+
aggregateId: ProjectId.make("project-replay"),
2917+
occurredAt: now,
2918+
commandId: CommandId.make("cmd-replay-project"),
2919+
causationEventId: null,
2920+
correlationId: CommandId.make("cmd-replay-project"),
2921+
metadata: {},
2922+
payload: {
2923+
projectId: ProjectId.make("project-replay"),
2924+
title: "Replay Project",
2925+
workspaceRoot: "/tmp/project-replay",
2926+
defaultModelSelection: null,
2927+
scripts: [],
2928+
createdAt: now,
2929+
updatedAt: now,
2930+
},
2931+
});
2932+
yield* eventStore.append({
2933+
type: "thread.created",
2934+
eventId: EventId.make("evt-replay-create-1"),
2935+
aggregateKind: "thread",
2936+
aggregateId: ThreadId.make("thread-replay"),
2937+
occurredAt: now,
2938+
commandId: CommandId.make("cmd-replay-create-1"),
2939+
causationEventId: null,
2940+
correlationId: CommandId.make("cmd-replay-create-1"),
2941+
metadata: {},
2942+
payload: { ...threadCreatedPayload, title: "First incarnation" },
2943+
});
2944+
yield* eventStore.append({
2945+
type: "thread.message-sent",
2946+
eventId: EventId.make("evt-replay-msg-old"),
2947+
aggregateKind: "thread",
2948+
aggregateId: ThreadId.make("thread-replay"),
2949+
occurredAt: now,
2950+
commandId: CommandId.make("cmd-replay-msg-old"),
2951+
causationEventId: null,
2952+
correlationId: CommandId.make("cmd-replay-msg-old"),
2953+
metadata: {},
2954+
payload: {
2955+
threadId: ThreadId.make("thread-replay"),
2956+
messageId: MessageId.make("msg-replay-old"),
2957+
role: "user",
2958+
text: "old incarnation",
2959+
turnId: null,
2960+
streaming: false,
2961+
createdAt: now,
2962+
updatedAt: now,
2963+
},
2964+
});
2965+
yield* eventStore.append({
2966+
type: "thread.deleted",
2967+
eventId: EventId.make("evt-replay-delete"),
2968+
aggregateKind: "thread",
2969+
aggregateId: ThreadId.make("thread-replay"),
2970+
occurredAt: now,
2971+
commandId: CommandId.make("cmd-replay-delete"),
2972+
causationEventId: null,
2973+
correlationId: CommandId.make("cmd-replay-delete"),
2974+
metadata: {},
2975+
payload: {
2976+
threadId: ThreadId.make("thread-replay"),
2977+
deletedAt: now,
2978+
},
2979+
});
2980+
yield* eventStore.append({
2981+
type: "thread.created",
2982+
eventId: EventId.make("evt-replay-create-2"),
2983+
aggregateKind: "thread",
2984+
aggregateId: ThreadId.make("thread-replay"),
2985+
occurredAt: now,
2986+
commandId: CommandId.make("cmd-replay-create-2"),
2987+
causationEventId: null,
2988+
correlationId: CommandId.make("cmd-replay-create-2"),
2989+
metadata: {},
2990+
payload: { ...threadCreatedPayload, title: "Second incarnation" },
2991+
});
2992+
yield* eventStore.append({
2993+
type: "thread.message-sent",
2994+
eventId: EventId.make("evt-replay-msg-new"),
2995+
aggregateKind: "thread",
2996+
aggregateId: ThreadId.make("thread-replay"),
2997+
occurredAt: now,
2998+
commandId: CommandId.make("cmd-replay-msg-new"),
2999+
causationEventId: null,
3000+
correlationId: CommandId.make("cmd-replay-msg-new"),
3001+
metadata: {},
3002+
payload: {
3003+
threadId: ThreadId.make("thread-replay"),
3004+
messageId: MessageId.make("msg-replay-new"),
3005+
role: "user",
3006+
text: "new incarnation",
3007+
turnId: null,
3008+
streaming: false,
3009+
createdAt: now,
3010+
updatedAt: now,
3011+
},
3012+
});
3013+
3014+
// Per-projector replay applies the whole log for each projector in
3015+
// turn, threads last. Rows the message projector rebuilt for the new
3016+
// incarnation must survive the threads projector's pass.
3017+
yield* projectionPipeline.bootstrap;
3018+
3019+
const messageRows = yield* sql<{ readonly messageId: string }>`
3020+
SELECT message_id AS "messageId"
3021+
FROM projection_thread_messages
3022+
WHERE thread_id = 'thread-replay'
3023+
`;
3024+
assert.deepEqual(messageRows, [{ messageId: "msg-replay-new" }]);
3025+
3026+
const threadRows = yield* sql<{
3027+
readonly title: string;
3028+
readonly deletedAt: string | null;
3029+
}>`
3030+
SELECT
3031+
title,
3032+
deleted_at AS "deletedAt"
3033+
FROM projection_threads
3034+
WHERE thread_id = 'thread-replay'
3035+
`;
3036+
assert.deepEqual(threadRows, [{ title: "Second incarnation", deletedAt: null }]);
3037+
}),
3038+
);
3039+
},
3040+
);

‎apps/server/src/orchestration/Layers/ProjectionPipeline.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
601601
"applyThreadsProjection",
602602
)(function* (event, attachmentSideEffects) {
603603
switch (event.type) {
604-
case "thread.created":
604+
case "thread.created": {
605605
yield* projectionThreadRepository.upsert({
606606
threadId: event.payload.threadId,
607607
projectId: event.payload.projectId,
@@ -630,6 +630,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
630630
deletedAt: null,
631631
});
632632
return;
633+
}
633634

634635
case "thread.archived": {
635636
const existingRow = yield* projectionThreadRepository.getById({
@@ -1019,6 +1020,15 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
10191020
return;
10201021
}
10211022

1023+
// A draft retry can re-create a soft-deleted thread id. Each projector
1024+
// purges its own rows on thread.created, so replay from any
1025+
// per-projector cursor rebuilds the new incarnation correctly.
1026+
case "thread.created":
1027+
yield* projectionThreadMessageRepository.deleteByThreadId({
1028+
threadId: event.payload.threadId,
1029+
});
1030+
return;
1031+
10221032
default:
10231033
return;
10241034
}
@@ -1070,6 +1080,12 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
10701080
return;
10711081
}
10721082

1083+
case "thread.created":
1084+
yield* projectionThreadProposedPlanRepository.deleteByThreadId({
1085+
threadId: event.payload.threadId,
1086+
});
1087+
return;
1088+
10731089
default:
10741090
return;
10751091
}
@@ -1122,6 +1138,12 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
11221138
return;
11231139
}
11241140

1141+
case "thread.created":
1142+
yield* projectionThreadActivityRepository.deleteByThreadId({
1143+
threadId: event.payload.threadId,
1144+
});
1145+
return;
1146+
11251147
default:
11261148
return;
11271149
}
@@ -1130,6 +1152,12 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
11301152
const applyThreadSessionsProjection: ProjectorDefinition["apply"] = Effect.fn(
11311153
"applyThreadSessionsProjection",
11321154
)(function* (event, _attachmentSideEffects) {
1155+
if (event.type === "thread.created") {
1156+
yield* projectionThreadSessionRepository.deleteByThreadId({
1157+
threadId: event.payload.threadId,
1158+
});
1159+
return;
1160+
}
11331161
if (event.type !== "thread.session-set") {
11341162
return;
11351163
}
@@ -1475,6 +1503,12 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
14751503
return;
14761504
}
14771505

1506+
case "thread.created":
1507+
yield* projectionTurnRepository.deleteByThreadId({
1508+
threadId: event.payload.threadId,
1509+
});
1510+
return;
1511+
14781512
default:
14791513
return;
14801514
}
@@ -1601,6 +1635,21 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
16011635
return;
16021636
}
16031637

1638+
case "thread.created": {
1639+
const existingRows = yield* projectionPendingApprovalRepository.listByThreadId({
1640+
threadId: event.payload.threadId,
1641+
});
1642+
yield* Effect.forEach(
1643+
existingRows,
1644+
(row) =>
1645+
projectionPendingApprovalRepository.deleteByRequestId({
1646+
requestId: row.requestId,
1647+
}),
1648+
{ concurrency: 1 },
1649+
).pipe(Effect.asVoid);
1650+
return;
1651+
}
1652+
16041653
default:
16051654
return;
16061655
}

0 commit comments

Comments
 (0)