fix(server): stop routine events from rescanning thread history (#8150)

This commit is contained in:
Theo Browne authored and GitHub committed 2026-08-25 00:50:02 +00:00
1 parent 99960383d0
commit c034f51bb7
2 files changed
+98 -1

No files matched your search

@@ -174,6 +174,78 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
assert.equal(row.lastAppliedSequence, 3);
}
yield* sql`CREATE TABLE thread_shell_updates (count INTEGER NOT NULL)`;
yield* sql`INSERT INTO thread_shell_updates (count) VALUES (0)`;
yield* sql`
CREATE TRIGGER count_thread_shell_updates
AFTER UPDATE ON projection_threads
WHEN NEW.thread_id = 'thread-1'
BEGIN
UPDATE thread_shell_updates SET count = count + 1;
END;
`;
yield* eventStore.append({
type: "thread.message-sent",
eventId: EventId.make("evt-assistant-update"),
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
occurredAt: "2026-01-01T00:00:00.100Z",
commandId: CommandId.make("cmd-assistant-update"),
causationEventId: null,
correlationId: CommandId.make("cmd-assistant-update"),
metadata: {},
payload: {
threadId: ThreadId.make("thread-1"),
messageId: MessageId.make("message-2"),
role: "assistant",
text: "more work",
turnId: null,
streaming: false,
createdAt: "2026-01-01T00:00:00.100Z",
updatedAt: "2026-01-01T00:00:00.100Z",
},
});
yield* projectionPipeline.bootstrap;
let threadShellUpdates = yield* sql<{ readonly count: number }>`
SELECT count FROM thread_shell_updates
`;
assert.deepEqual(threadShellUpdates, [{ count: 1 }]);
yield* sql`UPDATE thread_shell_updates SET count = 0`;
yield* eventStore.append({
type: "thread.activity-appended",
eventId: EventId.make("evt-routine-activity"),
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
occurredAt: "2026-01-01T00:00:00.200Z",
commandId: CommandId.make("cmd-routine-activity"),
causationEventId: null,
correlationId: CommandId.make("cmd-routine-activity"),
metadata: {},
payload: {
threadId: ThreadId.make("thread-1"),
activity: {
id: EventId.make("activity-routine"),
tone: "tool",
kind: "tool.updated",
summary: "Tool made progress",
payload: {},
turnId: null,
createdAt: "2026-01-01T00:00:00.200Z",
},
},
});
yield* projectionPipeline.bootstrap;
threadShellUpdates = yield* sql<{ readonly count: number }>`
SELECT count FROM thread_shell_updates
`;
assert.deepEqual(threadShellUpdates, [{ count: 1 }]);
yield* sql`DROP TRIGGER count_thread_shell_updates`;
yield* sql`DROP TABLE thread_shell_updates`;
// Settled lifecycle through the DB pipeline: thread.settled writes the
// override + timestamp, thread.unsettled(user) flips to the active pin.
yield* eventStore.append({
@@ -130,6 +130,29 @@ function isStalePendingApprovalFailureDetail(detail: string | null): boolean {
);
}
// A full refresh loads all thread history, so skip events that cannot change the summary.
function shouldRefreshThreadShellSummary(event: OrchestrationEvent): boolean {
if (event.type === "thread.message-sent") {
return event.payload.role === "user";
}
if (event.type !== "thread.activity-appended") {
return true;
}
switch (event.payload.activity.kind) {
case "approval.requested":
case "approval.resolved":
case "provider.approval.respond.failed":
case "user-input.requested":
case "user-input.resolved":
case "provider.user-input.respond.failed":
return true;
default:
return false;
}
}
function derivePendingUserInputCountFromActivities(
activities: ReadonlyArray<ProjectionThreadActivity>,
): number {
@@ -865,7 +888,9 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
...existingRow.value,
updatedAt: event.occurredAt,
});
yield* refreshThreadShellSummary(event.payload.threadId);
if (shouldRefreshThreadShellSummary(event)) {
yield* refreshThreadShellSummary(event.payload.threadId);
}
return;
}