Skip to content

Commit 77b586e

Browse files
committed
fix(server): stop routine events from rescanning thread history
Assistant messages and routine activities cannot change the current thread shell summary, but each previously reloaded all messages, plans, activities, and approvals. Keep the existing refresh for user messages and approval or user-input lifecycle events, and cover the bypass with the projection pipeline test.
1 parent a4cc136 commit 77b586e

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,78 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
174174
assert.equal(row.lastAppliedSequence, 3);
175175
}
176176

177+
// Assistant messages and routine activities only update the shell timestamp.
178+
// A summary refresh would produce a second projection_threads update.
179+
yield* sql`CREATE TABLE thread_shell_update_count (count INTEGER NOT NULL)`;
180+
yield* sql`INSERT INTO thread_shell_update_count (count) VALUES (0)`;
181+
yield* sql`
182+
CREATE TRIGGER count_thread_shell_updates
183+
AFTER UPDATE ON projection_threads
184+
WHEN NEW.thread_id = 'thread-1'
185+
BEGIN
186+
UPDATE thread_shell_update_count SET count = count + 1;
187+
END;
188+
`;
189+
190+
yield* eventStore.append({
191+
type: "thread.message-sent",
192+
eventId: EventId.make("evt-assistant-update"),
193+
aggregateKind: "thread",
194+
aggregateId: ThreadId.make("thread-1"),
195+
occurredAt: "2026-01-01T00:00:00.100Z",
196+
commandId: CommandId.make("cmd-assistant-update"),
197+
causationEventId: null,
198+
correlationId: CommandId.make("cmd-assistant-update"),
199+
metadata: {},
200+
payload: {
201+
threadId: ThreadId.make("thread-1"),
202+
messageId: MessageId.make("message-2"),
203+
role: "assistant",
204+
text: "more work",
205+
turnId: null,
206+
streaming: false,
207+
createdAt: "2026-01-01T00:00:00.100Z",
208+
updatedAt: "2026-01-01T00:00:00.100Z",
209+
},
210+
});
211+
yield* projectionPipeline.bootstrap;
212+
let threadUpdateRows = yield* sql<{ readonly count: number }>`
213+
SELECT count FROM thread_shell_update_count
214+
`;
215+
assert.deepEqual(threadUpdateRows, [{ count: 1 }]);
216+
217+
yield* sql`UPDATE thread_shell_update_count SET count = 0`;
218+
yield* eventStore.append({
219+
type: "thread.activity-appended",
220+
eventId: EventId.make("evt-routine-activity"),
221+
aggregateKind: "thread",
222+
aggregateId: ThreadId.make("thread-1"),
223+
occurredAt: "2026-01-01T00:00:00.200Z",
224+
commandId: CommandId.make("cmd-routine-activity"),
225+
causationEventId: null,
226+
correlationId: CommandId.make("cmd-routine-activity"),
227+
metadata: {},
228+
payload: {
229+
threadId: ThreadId.make("thread-1"),
230+
activity: {
231+
id: EventId.make("activity-routine"),
232+
tone: "tool",
233+
kind: "tool.updated",
234+
summary: "Tool made progress",
235+
payload: {},
236+
turnId: null,
237+
createdAt: "2026-01-01T00:00:00.200Z",
238+
},
239+
},
240+
});
241+
yield* projectionPipeline.bootstrap;
242+
threadUpdateRows = yield* sql<{ readonly count: number }>`
243+
SELECT count FROM thread_shell_update_count
244+
`;
245+
assert.deepEqual(threadUpdateRows, [{ count: 1 }]);
246+
yield* sql`DROP TRIGGER count_thread_shell_updates`;
247+
yield* sql`DROP TABLE thread_shell_update_count`;
248+
177249
// Settled lifecycle through the DB pipeline: thread.settled writes the
178250
// override + timestamp, thread.unsettled(user) flips to the active pin.
179251
yield* eventStore.append({

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ function isStalePendingApprovalFailureDetail(detail: string | null): boolean {
130130
);
131131
}
132132

133+
function eventCanChangeThreadShellSummary(event: OrchestrationEvent): boolean {
134+
// These are the only message and activity events that affect the fields
135+
// derived by refreshThreadShellSummary.
136+
if (event.type === "thread.message-sent") {
137+
return event.payload.role === "user";
138+
}
139+
if (event.type !== "thread.activity-appended") {
140+
return true;
141+
}
142+
switch (event.payload.activity.kind) {
143+
case "approval.requested":
144+
case "approval.resolved":
145+
case "provider.approval.respond.failed":
146+
case "user-input.requested":
147+
case "user-input.resolved":
148+
case "provider.user-input.respond.failed":
149+
return true;
150+
default:
151+
return false;
152+
}
153+
}
154+
133155
function derivePendingUserInputCountFromActivities(
134156
activities: ReadonlyArray<ProjectionThreadActivity>,
135157
): number {
@@ -865,7 +887,9 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
865887
...existingRow.value,
866888
updatedAt: event.occurredAt,
867889
});
868-
yield* refreshThreadShellSummary(event.payload.threadId);
890+
if (eventCanChangeThreadShellSummary(event)) {
891+
yield* refreshThreadShellSummary(event.payload.threadId);
892+
}
869893
return;
870894
}
871895

0 commit comments

Comments
 (0)