Skip to content

Commit a465772

Browse files
committed
perf(server): count pending questions without loading history
1 parent 5c23372 commit a465772

5 files changed

Lines changed: 402 additions & 37 deletions

File tree

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

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,7 +2513,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
25132513
}),
25142514
);
25152515

2516-
it.effect("reads only user-input activities when refreshing shell summaries", () =>
2516+
it.effect("counts pending questions without decoding retained question history", () =>
25172517
Effect.gen(function* () {
25182518
const projectionPipeline = yield* OrchestrationProjectionPipeline;
25192519
const eventStore = yield* OrchestrationEventStore;
@@ -2571,7 +2571,34 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
25712571
},
25722572
});
25732573

2574-
// Invalid JSON proves the summary query filters tool rows before decoding payloads.
2574+
// Large resolved payloads must not be loaded into the shell. Invalid tone
2575+
// values prove the count does not decode complete activity rows.
2576+
yield* sql`
2577+
WITH RECURSIVE history(n) AS (
2578+
SELECT 1
2579+
UNION ALL
2580+
SELECT n + 1 FROM history WHERE n < 1000
2581+
), lifecycle(kind) AS (
2582+
SELECT 'user-input.requested'
2583+
UNION ALL
2584+
SELECT 'user-input.resolved'
2585+
)
2586+
INSERT INTO projection_thread_activities (
2587+
activity_id, thread_id, turn_id, tone, kind, summary, payload_json, created_at
2588+
)
2589+
SELECT
2590+
'history-' || kind || '-' || n,
2591+
'thread-stale-user-input',
2592+
NULL,
2593+
'invalid-tone',
2594+
kind,
2595+
'Old question activity',
2596+
json_object('requestId', 'history-' || n, 'questions', ${"x".repeat(4096)}),
2597+
'2026-02-25T12:00:00.000Z'
2598+
FROM history CROSS JOIN lifecycle
2599+
`;
2600+
2601+
// Invalid JSON proves the count excludes unrelated tool payloads too.
25752602
yield* sql`
25762603
INSERT INTO projection_thread_activities (
25772604
activity_id,
@@ -3112,6 +3139,20 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
31123139
},
31133140
});
31143141

3142+
// A failed reply must not decode other requests or tool output.
3143+
yield* sql`
3144+
INSERT INTO projection_thread_activities (
3145+
activity_id, thread_id, turn_id, tone, kind, summary, payload_json, created_at
3146+
) VALUES
3147+
('nonstale-approval-tool', 'thread-nonstale-approval', NULL, 'info', 'tool.completed',
3148+
'Tool output', '{not-json', '2026-02-26T12:45:02.000Z'),
3149+
('nonstale-other-request', 'thread-nonstale-approval', NULL, 'invalid-tone',
3150+
'approval.requested', '', json_object('requestId', 'other-request'), '2026-02-26T12:45:02.000Z'),
3151+
('nonstale-other-thread', 'thread-nonstale-approval-other', NULL, 'info',
3152+
'approval.resolved', '', json_object('requestId', 'approval-request-nonstale-existing'),
3153+
'2026-02-26T12:45:02.000Z')
3154+
`;
3155+
31153156
yield* appendAndProject({
31163157
type: "thread.activity-appended",
31173158
eventId: EventId.make("evt-nonstale-approval-4"),
@@ -3268,7 +3309,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
32683309
}),
32693310
);
32703311

3271-
it.effect("does not fallback-retain messages whose turnId is removed by revert", () =>
3312+
it.effect("retains checkpointed messages and recomputes questions after revert", () =>
32723313
Effect.gen(function* () {
32733314
const projectionPipeline = yield* OrchestrationProjectionPipeline;
32743315
const eventStore = yield* OrchestrationEventStore;
@@ -3435,6 +3476,52 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
34353476
},
34363477
});
34373478

3479+
const questionActivities = [
3480+
{ kind: "user-input.requested", requestId: "revert-question", turnId: null },
3481+
{
3482+
kind: "user-input.resolved",
3483+
requestId: "revert-question",
3484+
turnId: TurnId.make("turn-2"),
3485+
},
3486+
{
3487+
kind: "user-input.requested",
3488+
requestId: "retained-question",
3489+
turnId: TurnId.make("turn-1"),
3490+
},
3491+
{ kind: "user-input.resolved", requestId: "retained-question", turnId: null },
3492+
];
3493+
for (const [index, activity] of questionActivities.entries()) {
3494+
yield* appendAndProject({
3495+
type: "thread.activity-appended",
3496+
eventId: EventId.make(`evt-revert-question-${index}`),
3497+
aggregateKind: "thread",
3498+
aggregateId: ThreadId.make("thread-revert"),
3499+
occurredAt: "2026-02-26T12:00:03.200Z",
3500+
commandId: null,
3501+
causationEventId: null,
3502+
correlationId: null,
3503+
metadata: {},
3504+
payload: {
3505+
threadId: ThreadId.make("thread-revert"),
3506+
activity: {
3507+
id: EventId.make(`activity-revert-question-${index}`),
3508+
tone: "info",
3509+
kind: activity.kind,
3510+
summary: "Question activity",
3511+
payload: { requestId: activity.requestId },
3512+
turnId: activity.turnId,
3513+
createdAt: "2026-02-26T12:00:03.200Z",
3514+
},
3515+
},
3516+
});
3517+
}
3518+
3519+
const beforeRevert = yield* sql<{ readonly count: number }>`
3520+
SELECT pending_user_input_count AS count
3521+
FROM projection_threads WHERE thread_id = 'thread-revert'
3522+
`;
3523+
assert.deepEqual(beforeRevert, [{ count: 0 }]);
3524+
34383525
yield* appendAndProject({
34393526
type: "thread.reverted",
34403527
eventId: EventId.make("evt-revert-8"),
@@ -3471,6 +3558,11 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
34713558
role: "assistant",
34723559
},
34733560
]);
3561+
const afterRevert = yield* sql<{ readonly count: number }>`
3562+
SELECT pending_user_input_count AS count
3563+
FROM projection_threads WHERE thread_id = 'thread-revert'
3564+
`;
3565+
assert.deepEqual(afterRevert, [{ count: 1 }]);
34743566
}),
34753567
);
34763568
});

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

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -141,31 +141,6 @@ function shouldRefreshThreadShellSummary(event: OrchestrationEvent): boolean {
141141
}
142142
}
143143

144-
function derivePendingUserInputCountFromActivities(
145-
activities: ReadonlyArray<ProjectionThreadActivity>,
146-
): number {
147-
const openRequestIds = new Set<string>();
148-
const closedRequestIds = new Set<string>();
149-
150-
for (const activity of activities) {
151-
const requestId = extractActivityRequestId(activity.payload);
152-
if (requestId === null) {
153-
continue;
154-
}
155-
if (activity.kind === "user-input.requested") {
156-
if (!closedRequestIds.has(requestId)) openRequestIds.add(requestId);
157-
continue;
158-
}
159-
160-
if (activity.kind === "user-input.resolved" || isRequestResponseStale(activity)) {
161-
closedRequestIds.add(requestId);
162-
openRequestIds.delete(requestId);
163-
}
164-
}
165-
166-
return openRequestIds.size;
167-
}
168-
169144
function deriveHasActionableProposedPlan(input: {
170145
readonly latestTurnId: string | null;
171146
readonly proposedPlans: ReadonlyArray<ProjectionThreadProposedPlan>;
@@ -564,15 +539,14 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
564539
return;
565540
}
566541

567-
const [latestUserMessageAt, proposedPlans, activities, pendingApprovalCount] =
542+
const [latestUserMessageAt, proposedPlans, pendingUserInputCount, pendingApprovalCount] =
568543
yield* Effect.all([
569544
projectionThreadMessageRepository.getLatestUserMessageAt({ threadId }),
570545
projectionThreadProposedPlanRepository.listByThreadId({ threadId }),
571-
projectionThreadActivityRepository.listUserInputLifecycleByThreadId({ threadId }),
546+
projectionThreadActivityRepository.countPendingUserInputsByThreadId({ threadId }),
572547
projectionPendingApprovalRepository.countPendingByThreadId({ threadId }),
573548
]);
574549

575-
const pendingUserInputCount = derivePendingUserInputCountFromActivities(activities);
576550
const hasActionableProposedPlan = deriveHasActionableProposedPlan({
577551
latestTurnId: existingRow.value.latestTurnId,
578552
proposedPlans,
@@ -1697,9 +1671,11 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
16971671
// Sending a reply clears the badge before the provider accepts it.
16981672
// A failed reply must restore the request unless a terminal event
16991673
// already closed it, including a reply from another client.
1700-
const requestActivities = (yield* projectionThreadActivityRepository.listByThreadId({
1701-
threadId: existingRow.value.threadId,
1702-
})).filter((activity) => extractActivityRequestId(activity.payload) === requestId);
1674+
const requestActivities =
1675+
yield* projectionThreadActivityRepository.listApprovalLifecycleByRequestId({
1676+
threadId: existingRow.value.threadId,
1677+
requestId,
1678+
});
17031679
const wasRequested = requestActivities.some(
17041680
(activity) => activity.kind === "approval.requested",
17051681
);
@@ -1722,8 +1698,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
17221698
// Only approval-requested activities should create pending-approval
17231699
// rows. Other activity kinds that happen to carry a requestId
17241700
// (e.g. user-input.requested / user-input.resolved) must not
1725-
// pollute this projection — they have their own accounting via
1726-
// derivePendingUserInputCountFromActivities.
1701+
// create pending approvals. Questions have a separate count.
17271702
if (event.payload.activity.kind !== "approval.requested") {
17281703
return;
17291704
}

0 commit comments

Comments
 (0)