Skip to content

Commit c2283ce

Browse files
t3dotggx1xhlol
andauthored
perf: make streaming projection and activity appends incremental (pingdotgg#9152)
Takes over pingdotgg#6608 by @x1xhlol. Server: thread.message-sent updates the projection row directly and folds latestUserMessageAt in as a running maximum instead of running refreshThreadShellSummary. Client: thread.activity-appended takes an O(1) append path for in-order, unseen activities on reducer-produced arrays, with a WeakMap id index, and falls back to filter and sort for out-of-order, re-delivered, context-window, and snapshot-loaded cases. Reducer benchmark: 10,000 in-order appends went from 1,180 ms to 70 ms. Change authored by Claude Fable 5.1 running in Claude Code, based on work by @x1xhlol. Co-authored-by: Lucas Valbuena <[email protected]>
1 parent feb3ea7 commit c2283ce

4 files changed

Lines changed: 464 additions & 31 deletions

File tree

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

Lines changed: 227 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,44 +2422,35 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
24222422
json_object('requestId', 'user-input-active'),
24232423
NULL,
24242424
'2026-02-26T12:35:07.000Z'
2425-
),
2426-
(
2427-
'activity-user-input-active-failed',
2428-
'thread-stale-user-input',
2429-
NULL,
2430-
'error',
2431-
'provider.user-input.respond.failed',
2432-
'Provider user input response failed',
2433-
json_object(
2434-
'requestId',
2435-
'user-input-active',
2436-
'detail',
2437-
'Provider is temporarily unavailable'
2438-
),
2439-
NULL,
2440-
'2026-02-26T12:35:08.000Z'
24412425
)
24422426
`;
24432427

2428+
// A user-input lifecycle activity is one of the events that still
2429+
// refreshes the shell summary, so it forces the read under test.
24442430
yield* appendAndProject({
2445-
type: "thread.message-sent",
2431+
type: "thread.activity-appended",
24462432
eventId: EventId.make("evt-stale-user-input-3"),
24472433
aggregateKind: "thread",
24482434
aggregateId: ThreadId.make("thread-stale-user-input"),
2449-
occurredAt: "2026-02-26T12:35:09.000Z",
2435+
occurredAt: "2026-02-26T12:35:08.000Z",
24502436
commandId: CommandId.make("cmd-stale-user-input-3"),
24512437
causationEventId: null,
24522438
correlationId: CorrelationId.make("cmd-stale-user-input-3"),
24532439
metadata: {},
24542440
payload: {
24552441
threadId: ThreadId.make("thread-stale-user-input"),
2456-
messageId: MessageId.make("message-stale-user-input"),
2457-
role: "user",
2458-
text: "Continue",
2459-
turnId: null,
2460-
streaming: false,
2461-
createdAt: "2026-02-26T12:35:09.000Z",
2462-
updatedAt: "2026-02-26T12:35:09.000Z",
2442+
activity: {
2443+
id: EventId.make("activity-user-input-active-failed"),
2444+
tone: "error",
2445+
kind: "provider.user-input.respond.failed",
2446+
summary: "Provider user input response failed",
2447+
payload: {
2448+
requestId: "user-input-active",
2449+
detail: "Provider is temporarily unavailable",
2450+
},
2451+
turnId: null,
2452+
createdAt: "2026-02-26T12:35:08.000Z",
2453+
},
24632454
},
24642455
});
24652456

@@ -2474,6 +2465,217 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
24742465
}),
24752466
);
24762467

2468+
it.effect("maintains shell summary fields across message and activity streams", () =>
2469+
Effect.gen(function* () {
2470+
const projectionPipeline = yield* OrchestrationProjectionPipeline;
2471+
const eventStore = yield* OrchestrationEventStore;
2472+
const sql = yield* SqlClient.SqlClient;
2473+
const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
2474+
eventStore
2475+
.append(event)
2476+
.pipe(Effect.flatMap((savedEvent) => projectionPipeline.projectEvent(savedEvent)));
2477+
2478+
yield* appendAndProject({
2479+
type: "project.created",
2480+
eventId: EventId.make("evt-shell-summary-1"),
2481+
aggregateKind: "project",
2482+
aggregateId: ProjectId.make("project-shell-summary"),
2483+
occurredAt: "2026-03-01T08:00:00.000Z",
2484+
commandId: CommandId.make("cmd-shell-summary-1"),
2485+
causationEventId: null,
2486+
correlationId: CorrelationId.make("cmd-shell-summary-1"),
2487+
metadata: {},
2488+
payload: {
2489+
projectId: ProjectId.make("project-shell-summary"),
2490+
title: "Project Shell Summary",
2491+
workspaceRoot: "/tmp/project-shell-summary",
2492+
defaultModelSelection: null,
2493+
scripts: [],
2494+
createdAt: "2026-03-01T08:00:00.000Z",
2495+
updatedAt: "2026-03-01T08:00:00.000Z",
2496+
},
2497+
});
2498+
2499+
yield* appendAndProject({
2500+
type: "thread.created",
2501+
eventId: EventId.make("evt-shell-summary-2"),
2502+
aggregateKind: "thread",
2503+
aggregateId: ThreadId.make("thread-shell-summary"),
2504+
occurredAt: "2026-03-01T08:00:01.000Z",
2505+
commandId: CommandId.make("cmd-shell-summary-2"),
2506+
causationEventId: null,
2507+
correlationId: CorrelationId.make("cmd-shell-summary-2"),
2508+
metadata: {},
2509+
payload: {
2510+
threadId: ThreadId.make("thread-shell-summary"),
2511+
projectId: ProjectId.make("project-shell-summary"),
2512+
title: "Thread Shell Summary",
2513+
modelSelection: {
2514+
instanceId: ProviderInstanceId.make("codex"),
2515+
model: "gpt-5-codex",
2516+
},
2517+
runtimeMode: "approval-required",
2518+
interactionMode: "default",
2519+
branch: null,
2520+
worktreePath: null,
2521+
createdAt: "2026-03-01T08:00:01.000Z",
2522+
updatedAt: "2026-03-01T08:00:01.000Z",
2523+
},
2524+
});
2525+
2526+
const readSummary = sql<{
2527+
readonly latestUserMessageAt: string | null;
2528+
readonly pendingUserInputCount: number;
2529+
readonly updatedAt: string;
2530+
}>`
2531+
SELECT
2532+
latest_user_message_at AS "latestUserMessageAt",
2533+
pending_user_input_count AS "pendingUserInputCount",
2534+
updated_at AS "updatedAt"
2535+
FROM projection_threads
2536+
WHERE thread_id = 'thread-shell-summary'
2537+
`;
2538+
2539+
yield* appendAndProject({
2540+
type: "thread.message-sent",
2541+
eventId: EventId.make("evt-shell-summary-3"),
2542+
aggregateKind: "thread",
2543+
aggregateId: ThreadId.make("thread-shell-summary"),
2544+
occurredAt: "2026-03-01T08:00:02.000Z",
2545+
commandId: CommandId.make("cmd-shell-summary-3"),
2546+
causationEventId: null,
2547+
correlationId: CorrelationId.make("cmd-shell-summary-3"),
2548+
metadata: {},
2549+
payload: {
2550+
threadId: ThreadId.make("thread-shell-summary"),
2551+
messageId: MessageId.make("message-shell-summary-user"),
2552+
role: "user",
2553+
text: "please do the thing",
2554+
turnId: TurnId.make("turn-shell-summary-1"),
2555+
streaming: false,
2556+
createdAt: "2026-03-01T08:00:02.000Z",
2557+
updatedAt: "2026-03-01T08:00:02.000Z",
2558+
},
2559+
});
2560+
2561+
assert.deepEqual(yield* readSummary, [
2562+
{
2563+
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
2564+
pendingUserInputCount: 0,
2565+
updatedAt: "2026-03-01T08:00:02.000Z",
2566+
},
2567+
]);
2568+
2569+
// Streaming assistant deltas bump updatedAt but must not disturb
2570+
// latestUserMessageAt or the pending counters.
2571+
yield* appendAndProject({
2572+
type: "thread.message-sent",
2573+
eventId: EventId.make("evt-shell-summary-4"),
2574+
aggregateKind: "thread",
2575+
aggregateId: ThreadId.make("thread-shell-summary"),
2576+
occurredAt: "2026-03-01T08:00:03.000Z",
2577+
commandId: CommandId.make("cmd-shell-summary-4"),
2578+
causationEventId: null,
2579+
correlationId: CorrelationId.make("cmd-shell-summary-4"),
2580+
metadata: {},
2581+
payload: {
2582+
threadId: ThreadId.make("thread-shell-summary"),
2583+
messageId: MessageId.make("message-shell-summary-assistant"),
2584+
role: "assistant",
2585+
text: "working on it",
2586+
turnId: TurnId.make("turn-shell-summary-1"),
2587+
streaming: true,
2588+
createdAt: "2026-03-01T08:00:03.000Z",
2589+
updatedAt: "2026-03-01T08:00:03.000Z",
2590+
},
2591+
});
2592+
2593+
assert.deepEqual(yield* readSummary, [
2594+
{
2595+
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
2596+
pendingUserInputCount: 0,
2597+
updatedAt: "2026-03-01T08:00:03.000Z",
2598+
},
2599+
]);
2600+
2601+
// Ordinary tool activities bump updatedAt without touching the
2602+
// user-input counter; user-input lifecycle activities update it.
2603+
yield* appendAndProject({
2604+
type: "thread.activity-appended",
2605+
eventId: EventId.make("evt-shell-summary-5"),
2606+
aggregateKind: "thread",
2607+
aggregateId: ThreadId.make("thread-shell-summary"),
2608+
occurredAt: "2026-03-01T08:00:04.000Z",
2609+
commandId: CommandId.make("cmd-shell-summary-5"),
2610+
causationEventId: null,
2611+
correlationId: CorrelationId.make("cmd-shell-summary-5"),
2612+
metadata: {},
2613+
payload: {
2614+
threadId: ThreadId.make("thread-shell-summary"),
2615+
activity: {
2616+
id: EventId.make("activity-shell-summary-command"),
2617+
tone: "tool",
2618+
kind: "command",
2619+
summary: "Ran a command",
2620+
payload: {},
2621+
turnId: TurnId.make("turn-shell-summary-1"),
2622+
createdAt: "2026-03-01T08:00:04.000Z",
2623+
},
2624+
},
2625+
});
2626+
2627+
assert.deepEqual(yield* readSummary, [
2628+
{
2629+
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
2630+
pendingUserInputCount: 0,
2631+
updatedAt: "2026-03-01T08:00:04.000Z",
2632+
},
2633+
]);
2634+
2635+
yield* appendAndProject({
2636+
type: "thread.activity-appended",
2637+
eventId: EventId.make("evt-shell-summary-6"),
2638+
aggregateKind: "thread",
2639+
aggregateId: ThreadId.make("thread-shell-summary"),
2640+
occurredAt: "2026-03-01T08:00:05.000Z",
2641+
commandId: CommandId.make("cmd-shell-summary-6"),
2642+
causationEventId: null,
2643+
correlationId: CorrelationId.make("cmd-shell-summary-6"),
2644+
metadata: {},
2645+
payload: {
2646+
threadId: ThreadId.make("thread-shell-summary"),
2647+
activity: {
2648+
id: EventId.make("activity-shell-summary-user-input"),
2649+
tone: "info",
2650+
kind: "user-input.requested",
2651+
summary: "User input requested",
2652+
payload: {
2653+
requestId: "user-input-request-shell-summary-1",
2654+
questions: [
2655+
{
2656+
id: "confirm",
2657+
header: "Confirm",
2658+
question: "Proceed?",
2659+
options: [{ label: "yes", description: "Proceed" }],
2660+
},
2661+
],
2662+
},
2663+
turnId: TurnId.make("turn-shell-summary-1"),
2664+
createdAt: "2026-03-01T08:00:05.000Z",
2665+
},
2666+
},
2667+
});
2668+
2669+
assert.deepEqual(yield* readSummary, [
2670+
{
2671+
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
2672+
pendingUserInputCount: 1,
2673+
updatedAt: "2026-03-01T08:00:05.000Z",
2674+
},
2675+
]);
2676+
}),
2677+
);
2678+
24772679
it.effect("ignores non-stale provider approval response failures", () =>
24782680
Effect.gen(function* () {
24792681
const projectionPipeline = yield* OrchestrationProjectionPipeline;

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,8 @@ function isStalePendingApprovalFailureDetail(detail: string | null): boolean {
130130
);
131131
}
132132

133-
// A refresh reads each persisted summary source, so skip events that cannot change the result.
133+
// A refresh reads each persisted summary source, so skip activities that cannot change the result.
134134
function shouldRefreshThreadShellSummary(event: OrchestrationEvent): boolean {
135-
if (event.type === "thread.message-sent") {
136-
return event.payload.role === "user";
137-
}
138-
139135
if (event.type !== "thread.activity-appended") {
140136
return true;
141137
}
@@ -897,7 +893,29 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
897893
return;
898894
}
899895

900-
case "thread.message-sent":
896+
// A message cannot change any summary field except latestUserMessageAt,
897+
// which is a monotonic maximum that folds in directly. The full refresh
898+
// would re-read every message body in the thread per user message.
899+
case "thread.message-sent": {
900+
const existingRow = yield* projectionThreadRepository.getById({
901+
threadId: event.payload.threadId,
902+
});
903+
if (Option.isNone(existingRow)) {
904+
return;
905+
}
906+
const previousLatest = existingRow.value.latestUserMessageAt;
907+
yield* projectionThreadRepository.upsert({
908+
...existingRow.value,
909+
updatedAt: event.occurredAt,
910+
latestUserMessageAt:
911+
event.payload.role === "user" &&
912+
(previousLatest === null || event.payload.createdAt > previousLatest)
913+
? event.payload.createdAt
914+
: previousLatest,
915+
});
916+
return;
917+
}
918+
901919
case "thread.proposed-plan-upserted":
902920
case "thread.activity-appended":
903921
case "thread.approval-response-requested":

0 commit comments

Comments
 (0)