Skip to content

Commit 5b48607

Browse files
committed
perf: make streaming projection and activity appends incremental
1 parent 184d8ef commit 5b48607

4 files changed

Lines changed: 367 additions & 2 deletions

File tree

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

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,217 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
20862086
}),
20872087
);
20882088

2089+
it.effect("maintains shell summary fields across message and activity streams", () =>
2090+
Effect.gen(function* () {
2091+
const projectionPipeline = yield* OrchestrationProjectionPipeline;
2092+
const eventStore = yield* OrchestrationEventStore;
2093+
const sql = yield* SqlClient.SqlClient;
2094+
const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
2095+
eventStore
2096+
.append(event)
2097+
.pipe(Effect.flatMap((savedEvent) => projectionPipeline.projectEvent(savedEvent)));
2098+
2099+
yield* appendAndProject({
2100+
type: "project.created",
2101+
eventId: EventId.make("evt-shell-summary-1"),
2102+
aggregateKind: "project",
2103+
aggregateId: ProjectId.make("project-shell-summary"),
2104+
occurredAt: "2026-03-01T08:00:00.000Z",
2105+
commandId: CommandId.make("cmd-shell-summary-1"),
2106+
causationEventId: null,
2107+
correlationId: CorrelationId.make("cmd-shell-summary-1"),
2108+
metadata: {},
2109+
payload: {
2110+
projectId: ProjectId.make("project-shell-summary"),
2111+
title: "Project Shell Summary",
2112+
workspaceRoot: "/tmp/project-shell-summary",
2113+
defaultModelSelection: null,
2114+
scripts: [],
2115+
createdAt: "2026-03-01T08:00:00.000Z",
2116+
updatedAt: "2026-03-01T08:00:00.000Z",
2117+
},
2118+
});
2119+
2120+
yield* appendAndProject({
2121+
type: "thread.created",
2122+
eventId: EventId.make("evt-shell-summary-2"),
2123+
aggregateKind: "thread",
2124+
aggregateId: ThreadId.make("thread-shell-summary"),
2125+
occurredAt: "2026-03-01T08:00:01.000Z",
2126+
commandId: CommandId.make("cmd-shell-summary-2"),
2127+
causationEventId: null,
2128+
correlationId: CorrelationId.make("cmd-shell-summary-2"),
2129+
metadata: {},
2130+
payload: {
2131+
threadId: ThreadId.make("thread-shell-summary"),
2132+
projectId: ProjectId.make("project-shell-summary"),
2133+
title: "Thread Shell Summary",
2134+
modelSelection: {
2135+
instanceId: ProviderInstanceId.make("codex"),
2136+
model: "gpt-5-codex",
2137+
},
2138+
runtimeMode: "approval-required",
2139+
interactionMode: "default",
2140+
branch: null,
2141+
worktreePath: null,
2142+
createdAt: "2026-03-01T08:00:01.000Z",
2143+
updatedAt: "2026-03-01T08:00:01.000Z",
2144+
},
2145+
});
2146+
2147+
const readSummary = sql<{
2148+
readonly latestUserMessageAt: string | null;
2149+
readonly pendingUserInputCount: number;
2150+
readonly updatedAt: string;
2151+
}>`
2152+
SELECT
2153+
latest_user_message_at AS "latestUserMessageAt",
2154+
pending_user_input_count AS "pendingUserInputCount",
2155+
updated_at AS "updatedAt"
2156+
FROM projection_threads
2157+
WHERE thread_id = 'thread-shell-summary'
2158+
`;
2159+
2160+
yield* appendAndProject({
2161+
type: "thread.message-sent",
2162+
eventId: EventId.make("evt-shell-summary-3"),
2163+
aggregateKind: "thread",
2164+
aggregateId: ThreadId.make("thread-shell-summary"),
2165+
occurredAt: "2026-03-01T08:00:02.000Z",
2166+
commandId: CommandId.make("cmd-shell-summary-3"),
2167+
causationEventId: null,
2168+
correlationId: CorrelationId.make("cmd-shell-summary-3"),
2169+
metadata: {},
2170+
payload: {
2171+
threadId: ThreadId.make("thread-shell-summary"),
2172+
messageId: MessageId.make("message-shell-summary-user"),
2173+
role: "user",
2174+
text: "please do the thing",
2175+
turnId: TurnId.make("turn-shell-summary-1"),
2176+
streaming: false,
2177+
createdAt: "2026-03-01T08:00:02.000Z",
2178+
updatedAt: "2026-03-01T08:00:02.000Z",
2179+
},
2180+
});
2181+
2182+
assert.deepEqual(yield* readSummary, [
2183+
{
2184+
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
2185+
pendingUserInputCount: 0,
2186+
updatedAt: "2026-03-01T08:00:02.000Z",
2187+
},
2188+
]);
2189+
2190+
// Streaming assistant deltas bump updatedAt but must not disturb
2191+
// latestUserMessageAt or the pending counters.
2192+
yield* appendAndProject({
2193+
type: "thread.message-sent",
2194+
eventId: EventId.make("evt-shell-summary-4"),
2195+
aggregateKind: "thread",
2196+
aggregateId: ThreadId.make("thread-shell-summary"),
2197+
occurredAt: "2026-03-01T08:00:03.000Z",
2198+
commandId: CommandId.make("cmd-shell-summary-4"),
2199+
causationEventId: null,
2200+
correlationId: CorrelationId.make("cmd-shell-summary-4"),
2201+
metadata: {},
2202+
payload: {
2203+
threadId: ThreadId.make("thread-shell-summary"),
2204+
messageId: MessageId.make("message-shell-summary-assistant"),
2205+
role: "assistant",
2206+
text: "working on it",
2207+
turnId: TurnId.make("turn-shell-summary-1"),
2208+
streaming: true,
2209+
createdAt: "2026-03-01T08:00:03.000Z",
2210+
updatedAt: "2026-03-01T08:00:03.000Z",
2211+
},
2212+
});
2213+
2214+
assert.deepEqual(yield* readSummary, [
2215+
{
2216+
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
2217+
pendingUserInputCount: 0,
2218+
updatedAt: "2026-03-01T08:00:03.000Z",
2219+
},
2220+
]);
2221+
2222+
// Ordinary tool activities bump updatedAt without touching the
2223+
// user-input counter; user-input lifecycle activities update it.
2224+
yield* appendAndProject({
2225+
type: "thread.activity-appended",
2226+
eventId: EventId.make("evt-shell-summary-5"),
2227+
aggregateKind: "thread",
2228+
aggregateId: ThreadId.make("thread-shell-summary"),
2229+
occurredAt: "2026-03-01T08:00:04.000Z",
2230+
commandId: CommandId.make("cmd-shell-summary-5"),
2231+
causationEventId: null,
2232+
correlationId: CorrelationId.make("cmd-shell-summary-5"),
2233+
metadata: {},
2234+
payload: {
2235+
threadId: ThreadId.make("thread-shell-summary"),
2236+
activity: {
2237+
id: EventId.make("activity-shell-summary-command"),
2238+
tone: "tool",
2239+
kind: "command",
2240+
summary: "Ran a command",
2241+
payload: {},
2242+
turnId: TurnId.make("turn-shell-summary-1"),
2243+
createdAt: "2026-03-01T08:00:04.000Z",
2244+
},
2245+
},
2246+
});
2247+
2248+
assert.deepEqual(yield* readSummary, [
2249+
{
2250+
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
2251+
pendingUserInputCount: 0,
2252+
updatedAt: "2026-03-01T08:00:04.000Z",
2253+
},
2254+
]);
2255+
2256+
yield* appendAndProject({
2257+
type: "thread.activity-appended",
2258+
eventId: EventId.make("evt-shell-summary-6"),
2259+
aggregateKind: "thread",
2260+
aggregateId: ThreadId.make("thread-shell-summary"),
2261+
occurredAt: "2026-03-01T08:00:05.000Z",
2262+
commandId: CommandId.make("cmd-shell-summary-6"),
2263+
causationEventId: null,
2264+
correlationId: CorrelationId.make("cmd-shell-summary-6"),
2265+
metadata: {},
2266+
payload: {
2267+
threadId: ThreadId.make("thread-shell-summary"),
2268+
activity: {
2269+
id: EventId.make("activity-shell-summary-user-input"),
2270+
tone: "info",
2271+
kind: "user-input.requested",
2272+
summary: "User input requested",
2273+
payload: {
2274+
requestId: "user-input-request-shell-summary-1",
2275+
questions: [
2276+
{
2277+
id: "confirm",
2278+
header: "Confirm",
2279+
question: "Proceed?",
2280+
options: [{ label: "yes", description: "Proceed" }],
2281+
},
2282+
],
2283+
},
2284+
turnId: TurnId.make("turn-shell-summary-1"),
2285+
createdAt: "2026-03-01T08:00:05.000Z",
2286+
},
2287+
},
2288+
});
2289+
2290+
assert.deepEqual(yield* readSummary, [
2291+
{
2292+
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
2293+
pendingUserInputCount: 1,
2294+
updatedAt: "2026-03-01T08:00:05.000Z",
2295+
},
2296+
]);
2297+
}),
2298+
);
2299+
20892300
it.effect("ignores non-stale provider approval response failures", () =>
20902301
Effect.gen(function* () {
20912302
const projectionPipeline = yield* OrchestrationProjectionPipeline;

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

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

133+
function activityAffectsShellSummary(kind: string): boolean {
134+
return (
135+
kind === "user-input.requested" ||
136+
kind === "user-input.resolved" ||
137+
kind === "provider.user-input.respond.failed" ||
138+
kind === "approval.requested" ||
139+
kind === "approval.resolved" ||
140+
kind === "provider.approval.respond.failed"
141+
);
142+
}
143+
133144
function derivePendingUserInputCountFromActivities(
134145
activities: ReadonlyArray<ProjectionThreadActivity>,
135146
): number {
@@ -850,9 +861,51 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
850861
return;
851862
}
852863

853-
case "thread.message-sent":
864+
// Message events cannot change any derived summary field except
865+
// latestUserMessageAt, which is a monotonic maximum that folds in
866+
// directly — the full refresh would re-read every message, plan,
867+
// activity, and approval on each streaming assistant delta.
868+
case "thread.message-sent": {
869+
const existingRow = yield* projectionThreadRepository.getById({
870+
threadId: event.payload.threadId,
871+
});
872+
if (Option.isNone(existingRow)) {
873+
return;
874+
}
875+
const previousLatest = existingRow.value.latestUserMessageAt;
876+
yield* projectionThreadRepository.upsert({
877+
...existingRow.value,
878+
updatedAt: event.occurredAt,
879+
latestUserMessageAt:
880+
event.payload.role === "user" &&
881+
(previousLatest === null || event.payload.createdAt > previousLatest)
882+
? event.payload.createdAt
883+
: previousLatest,
884+
});
885+
return;
886+
}
887+
888+
// Only approval and user-input lifecycle activities feed the pending
889+
// counters; the high-frequency progress/tool activities just bump
890+
// updatedAt.
891+
case "thread.activity-appended": {
892+
const existingRow = yield* projectionThreadRepository.getById({
893+
threadId: event.payload.threadId,
894+
});
895+
if (Option.isNone(existingRow)) {
896+
return;
897+
}
898+
yield* projectionThreadRepository.upsert({
899+
...existingRow.value,
900+
updatedAt: event.occurredAt,
901+
});
902+
if (activityAffectsShellSummary(event.payload.activity.kind)) {
903+
yield* refreshThreadShellSummary(event.payload.threadId);
904+
}
905+
return;
906+
}
907+
854908
case "thread.proposed-plan-upserted":
855-
case "thread.activity-appended":
856909
case "thread.approval-response-requested":
857910
case "thread.user-input-response-requested": {
858911
const existingRow = yield* projectionThreadRepository.getById({

‎packages/client-runtime/src/state/threadReducer.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,89 @@ describe("applyThreadDetailEvent", () => {
683683
}
684684
});
685685

686+
it("re-sorts when an activity arrives out of order", () => {
687+
const makeActivity = (id: string, sequence: number) => ({
688+
id: EventId.make(id),
689+
tone: "tool" as const,
690+
kind: "command",
691+
summary: `Ran command ${sequence}`,
692+
payload: {},
693+
turnId: TurnId.make("turn-1"),
694+
sequence,
695+
createdAt: "2026-04-01T11:00:00.000Z",
696+
});
697+
const result = applyThreadDetailEvent(
698+
{
699+
...baseThread,
700+
activities: [makeActivity("activity-a", 1), makeActivity("activity-c", 3)],
701+
},
702+
{
703+
...baseEventFields,
704+
sequence: 131,
705+
occurredAt: "2026-04-01T11:01:00.000Z",
706+
aggregateKind: "thread",
707+
aggregateId: ThreadId.make("thread-1"),
708+
type: "thread.activity-appended",
709+
payload: {
710+
threadId: ThreadId.make("thread-1"),
711+
activity: makeActivity("activity-b", 2),
712+
},
713+
},
714+
);
715+
716+
expect(result.kind).toBe("updated");
717+
if (result.kind === "updated") {
718+
expect(result.thread.activities.map((activity) => activity.id)).toEqual([
719+
"activity-a",
720+
"activity-b",
721+
"activity-c",
722+
]);
723+
}
724+
});
725+
726+
it("replaces a re-delivered activity instead of duplicating it", () => {
727+
const makeActivity = (id: string, sequence: number, summary: string) => ({
728+
id: EventId.make(id),
729+
tone: "tool" as const,
730+
kind: "command",
731+
summary,
732+
payload: {},
733+
turnId: TurnId.make("turn-1"),
734+
sequence,
735+
createdAt: "2026-04-01T11:00:00.000Z",
736+
});
737+
const result = applyThreadDetailEvent(
738+
{
739+
...baseThread,
740+
activities: [
741+
makeActivity("activity-a", 1, "first"),
742+
makeActivity("activity-b", 2, "second"),
743+
],
744+
},
745+
{
746+
...baseEventFields,
747+
sequence: 132,
748+
occurredAt: "2026-04-01T11:01:00.000Z",
749+
aggregateKind: "thread",
750+
aggregateId: ThreadId.make("thread-1"),
751+
type: "thread.activity-appended",
752+
payload: {
753+
threadId: ThreadId.make("thread-1"),
754+
activity: makeActivity("activity-b", 2, "second (redelivered)"),
755+
},
756+
},
757+
);
758+
759+
expect(result.kind).toBe("updated");
760+
if (result.kind === "updated") {
761+
expect(result.thread.activities.map((activity) => activity.id)).toEqual([
762+
"activity-a",
763+
"activity-b",
764+
]);
765+
expect(result.thread.activities[1]?.summary).toBe("second (redelivered)");
766+
}
767+
});
768+
686769
it("replaces earlier resolvable context-window updates for the same turn", () => {
687770
const contextWindowActivity = (id: string, sequence: number, usedTokens: unknown) => ({
688771
id: EventId.make(id),

0 commit comments

Comments
 (0)