Skip to content

Commit e98cd24

Browse files
committed
fix(server): drop pending approvals when a thread reverts
Revert already dropped later-turn messages and activities. Pending approval rows stayed. The shell count still included them, so the user could answer a request the provider no longer had. On thread.reverted, drop pending approvals whose turn was discarded. Approvals with no turn stay, matching activities. The shell pending count then follows.
1 parent f6f2be3 commit e98cd24

2 files changed

Lines changed: 262 additions & 1 deletion

File tree

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

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,210 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
26262626
]);
26272627
}),
26282628
);
2629+
2630+
it.effect("drops later-turn pending approvals when a thread is reverted", () =>
2631+
Effect.gen(function* () {
2632+
const projectionPipeline = yield* OrchestrationProjectionPipeline;
2633+
const eventStore = yield* OrchestrationEventStore;
2634+
const sql = yield* SqlClient.SqlClient;
2635+
const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
2636+
eventStore
2637+
.append(event)
2638+
.pipe(Effect.flatMap((savedEvent) => projectionPipeline.projectEvent(savedEvent)));
2639+
2640+
yield* appendAndProject({
2641+
type: "project.created",
2642+
eventId: EventId.make("evt-revert-approval-1"),
2643+
aggregateKind: "project",
2644+
aggregateId: ProjectId.make("project-revert-approval"),
2645+
occurredAt: "2026-02-26T13:00:00.000Z",
2646+
commandId: CommandId.make("cmd-revert-approval-1"),
2647+
causationEventId: null,
2648+
correlationId: CorrelationId.make("cmd-revert-approval-1"),
2649+
metadata: {},
2650+
payload: {
2651+
projectId: ProjectId.make("project-revert-approval"),
2652+
title: "Project Revert Approval",
2653+
workspaceRoot: "/tmp/project-revert-approval",
2654+
defaultModelSelection: null,
2655+
scripts: [],
2656+
createdAt: "2026-02-26T13:00:00.000Z",
2657+
updatedAt: "2026-02-26T13:00:00.000Z",
2658+
},
2659+
});
2660+
2661+
yield* appendAndProject({
2662+
type: "thread.created",
2663+
eventId: EventId.make("evt-revert-approval-2"),
2664+
aggregateKind: "thread",
2665+
aggregateId: ThreadId.make("thread-revert-approval"),
2666+
occurredAt: "2026-02-26T13:00:01.000Z",
2667+
commandId: CommandId.make("cmd-revert-approval-2"),
2668+
causationEventId: null,
2669+
correlationId: CorrelationId.make("cmd-revert-approval-2"),
2670+
metadata: {},
2671+
payload: {
2672+
threadId: ThreadId.make("thread-revert-approval"),
2673+
projectId: ProjectId.make("project-revert-approval"),
2674+
title: "Thread Revert Approval",
2675+
modelSelection: {
2676+
instanceId: ProviderInstanceId.make("codex"),
2677+
model: "gpt-5-codex",
2678+
},
2679+
runtimeMode: "approval-required",
2680+
interactionMode: "default",
2681+
branch: null,
2682+
worktreePath: null,
2683+
createdAt: "2026-02-26T13:00:01.000Z",
2684+
updatedAt: "2026-02-26T13:00:01.000Z",
2685+
},
2686+
});
2687+
2688+
yield* appendAndProject({
2689+
type: "thread.turn-diff-completed",
2690+
eventId: EventId.make("evt-revert-approval-3"),
2691+
aggregateKind: "thread",
2692+
aggregateId: ThreadId.make("thread-revert-approval"),
2693+
occurredAt: "2026-02-26T13:00:02.000Z",
2694+
commandId: CommandId.make("cmd-revert-approval-3"),
2695+
causationEventId: null,
2696+
correlationId: CorrelationId.make("cmd-revert-approval-3"),
2697+
metadata: {},
2698+
payload: {
2699+
threadId: ThreadId.make("thread-revert-approval"),
2700+
turnId: TurnId.make("turn-keep"),
2701+
checkpointTurnCount: 1,
2702+
checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread-revert-approval/turn/1"),
2703+
status: "ready",
2704+
files: [],
2705+
assistantMessageId: MessageId.make("assistant-keep-approval"),
2706+
completedAt: "2026-02-26T13:00:02.000Z",
2707+
},
2708+
});
2709+
2710+
yield* appendAndProject({
2711+
type: "thread.activity-appended",
2712+
eventId: EventId.make("evt-revert-approval-4"),
2713+
aggregateKind: "thread",
2714+
aggregateId: ThreadId.make("thread-revert-approval"),
2715+
occurredAt: "2026-02-26T13:00:02.100Z",
2716+
commandId: CommandId.make("cmd-revert-approval-4"),
2717+
causationEventId: null,
2718+
correlationId: CorrelationId.make("cmd-revert-approval-4"),
2719+
metadata: {},
2720+
payload: {
2721+
threadId: ThreadId.make("thread-revert-approval"),
2722+
activity: {
2723+
id: EventId.make("activity-keep-approval"),
2724+
tone: "approval",
2725+
kind: "approval.requested",
2726+
summary: "Keep this approval",
2727+
payload: {
2728+
requestId: "approval-request-keep",
2729+
requestKind: "command",
2730+
},
2731+
turnId: TurnId.make("turn-keep"),
2732+
createdAt: "2026-02-26T13:00:02.100Z",
2733+
},
2734+
},
2735+
});
2736+
2737+
yield* appendAndProject({
2738+
type: "thread.turn-diff-completed",
2739+
eventId: EventId.make("evt-revert-approval-5"),
2740+
aggregateKind: "thread",
2741+
aggregateId: ThreadId.make("thread-revert-approval"),
2742+
occurredAt: "2026-02-26T13:00:03.000Z",
2743+
commandId: CommandId.make("cmd-revert-approval-5"),
2744+
causationEventId: null,
2745+
correlationId: CorrelationId.make("cmd-revert-approval-5"),
2746+
metadata: {},
2747+
payload: {
2748+
threadId: ThreadId.make("thread-revert-approval"),
2749+
turnId: TurnId.make("turn-drop"),
2750+
checkpointTurnCount: 2,
2751+
checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread-revert-approval/turn/2"),
2752+
status: "ready",
2753+
files: [],
2754+
assistantMessageId: MessageId.make("assistant-drop-approval"),
2755+
completedAt: "2026-02-26T13:00:03.000Z",
2756+
},
2757+
});
2758+
2759+
yield* appendAndProject({
2760+
type: "thread.activity-appended",
2761+
eventId: EventId.make("evt-revert-approval-6"),
2762+
aggregateKind: "thread",
2763+
aggregateId: ThreadId.make("thread-revert-approval"),
2764+
occurredAt: "2026-02-26T13:00:03.100Z",
2765+
commandId: CommandId.make("cmd-revert-approval-6"),
2766+
causationEventId: null,
2767+
correlationId: CorrelationId.make("cmd-revert-approval-6"),
2768+
metadata: {},
2769+
payload: {
2770+
threadId: ThreadId.make("thread-revert-approval"),
2771+
activity: {
2772+
id: EventId.make("activity-drop-approval"),
2773+
tone: "approval",
2774+
kind: "approval.requested",
2775+
summary: "Drop this approval",
2776+
payload: {
2777+
requestId: "approval-request-drop",
2778+
requestKind: "command",
2779+
},
2780+
turnId: TurnId.make("turn-drop"),
2781+
createdAt: "2026-02-26T13:00:03.100Z",
2782+
},
2783+
},
2784+
});
2785+
2786+
yield* appendAndProject({
2787+
type: "thread.reverted",
2788+
eventId: EventId.make("evt-revert-approval-7"),
2789+
aggregateKind: "thread",
2790+
aggregateId: ThreadId.make("thread-revert-approval"),
2791+
occurredAt: "2026-02-26T13:00:04.000Z",
2792+
commandId: CommandId.make("cmd-revert-approval-7"),
2793+
causationEventId: null,
2794+
correlationId: CorrelationId.make("cmd-revert-approval-7"),
2795+
metadata: {},
2796+
payload: {
2797+
threadId: ThreadId.make("thread-revert-approval"),
2798+
turnCount: 1,
2799+
},
2800+
});
2801+
2802+
const approvalRows = yield* sql<{
2803+
readonly requestId: string;
2804+
readonly status: string;
2805+
readonly turnId: string | null;
2806+
}>`
2807+
SELECT
2808+
request_id AS "requestId",
2809+
status,
2810+
turn_id AS "turnId"
2811+
FROM projection_pending_approvals
2812+
WHERE thread_id = 'thread-revert-approval'
2813+
ORDER BY request_id ASC
2814+
`;
2815+
assert.deepEqual(approvalRows, [
2816+
{
2817+
requestId: "approval-request-keep",
2818+
status: "pending",
2819+
turnId: "turn-keep",
2820+
},
2821+
]);
2822+
2823+
const threadRows = yield* sql<{
2824+
readonly pendingApprovalCount: number;
2825+
}>`
2826+
SELECT pending_approval_count AS "pendingApprovalCount"
2827+
FROM projection_threads
2828+
WHERE thread_id = 'thread-revert-approval'
2829+
`;
2830+
assert.deepEqual(threadRows, [{ pendingApprovalCount: 1 }]);
2831+
}),
2832+
);
26292833
});
26302834

26312835
it.layer(makeProjectionPipelinePrefixedTestLayer("t3-pending-turn-terminal-test-"))(

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import * as SqlClient from "effect/unstable/sql/SqlClient";
1515

1616
import { toPersistenceSqlError, type ProjectionRepositoryError } from "../../persistence/Errors.ts";
1717
import { OrchestrationEventStore } from "../../persistence/Services/OrchestrationEventStore.ts";
18-
import { ProjectionPendingApprovalRepository } from "../../persistence/Services/ProjectionPendingApprovals.ts";
18+
import {
19+
type ProjectionPendingApproval,
20+
ProjectionPendingApprovalRepository,
21+
} from "../../persistence/Services/ProjectionPendingApprovals.ts";
1922
import { ProjectionProjectRepository } from "../../persistence/Services/ProjectionProjects.ts";
2023
import { ProjectionStateRepository } from "../../persistence/Services/ProjectionState.ts";
2124
import { ProjectionThreadActivityRepository } from "../../persistence/Services/ProjectionThreadActivities.ts";
@@ -350,6 +353,26 @@ function retainProjectionProposedPlansAfterRevert(
350353
);
351354
}
352355

356+
function retainProjectionPendingApprovalsAfterRevert(
357+
approvals: ReadonlyArray<ProjectionPendingApproval>,
358+
turns: ReadonlyArray<ProjectionTurn>,
359+
turnCount: number,
360+
): ReadonlyArray<ProjectionPendingApproval> {
361+
const retainedTurnIds = new Set<string>(
362+
turns
363+
.filter(
364+
(turn) =>
365+
turn.turnId !== null &&
366+
turn.checkpointTurnCount !== null &&
367+
turn.checkpointTurnCount <= turnCount,
368+
)
369+
.flatMap((turn) => (turn.turnId === null ? [] : [turn.turnId])),
370+
);
371+
return approvals.filter(
372+
(approval) => approval.turnId === null || retainedTurnIds.has(approval.turnId),
373+
);
374+
}
375+
353376
function collectThreadAttachmentRelativePaths(
354377
threadId: string,
355378
messages: ReadonlyArray<ProjectionThreadMessage>,
@@ -1640,6 +1663,40 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
16401663
return;
16411664
}
16421665

1666+
case "thread.reverted": {
1667+
const existingRows = yield* projectionPendingApprovalRepository.listByThreadId({
1668+
threadId: event.payload.threadId,
1669+
});
1670+
if (existingRows.length === 0) {
1671+
return;
1672+
}
1673+
1674+
const existingTurns = yield* projectionTurnRepository.listByThreadId({
1675+
threadId: event.payload.threadId,
1676+
});
1677+
const keptRows = retainProjectionPendingApprovalsAfterRevert(
1678+
existingRows,
1679+
existingTurns,
1680+
event.payload.turnCount,
1681+
);
1682+
if (keptRows.length === existingRows.length) {
1683+
return;
1684+
}
1685+
1686+
const keptRequestIds = new Set(keptRows.map((row) => row.requestId));
1687+
yield* Effect.forEach(
1688+
existingRows,
1689+
(row) =>
1690+
keptRequestIds.has(row.requestId)
1691+
? Effect.void
1692+
: projectionPendingApprovalRepository.deleteByRequestId({
1693+
requestId: row.requestId,
1694+
}),
1695+
{ concurrency: 1 },
1696+
).pipe(Effect.asVoid);
1697+
return;
1698+
}
1699+
16431700
default:
16441701
return;
16451702
}

0 commit comments

Comments
 (0)