Skip to content

Commit e05bb29

Browse files
bowdclaude
andcommitted
perf(server): visiting a thread no longer copies the whole thread into the event log
`thread.visited` carried the whole thread, so every visit wrote and sent every linked pull request. Emit `thread.visit-recorded` with only the read state ({ lastVisitedAt, opens? }) and apply it the way `thread.pull-request-link-updated` is applied (#19248): the projection reads the thread and writes it through the existing `thread.visited` path, and clients merge the read state. Old `thread.visited` events still replay, and older clients skip the unknown type. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 98b930f commit e05bb29

7 files changed

Lines changed: 118 additions & 7 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,19 +3059,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
30593059
command.searchQuery,
30603060
)
30613061
: null;
3062-
// Viewing a thread changes read state only. Loading its transcript (or
3063-
// bumping updatedAt) makes a routine read receipt scale with its history.
3062+
// Viewing a thread changes read state only. Loading its transcript, bumping
3063+
// updatedAt, or copying the whole thread into the event (its linked pull
3064+
// requests included) makes a routine read receipt scale with its history.
30643065
yield* emit(
30653066
events,
30663067
command,
30673068
)({
3068-
type: "thread.visited",
3069+
type: "thread.visit-recorded",
30693070
threadId: command.threadId,
30703071
providerInstanceId: thread.providerInstanceId,
30713072
occurredAt,
30723073
payload: {
3073-
...thread,
3074-
...(movesForward ? { lastVisitedAt: visitedAt.value } : {}),
3074+
lastVisitedAt: movesForward ? visitedAt.value : thread.lastVisitedAt,
30753075
...(opens ? { opens } : {}),
30763076
},
30773077
});

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ProjectThreadAwarenessV2Input } from "@t3tools/shared/agentAwareness";
2-
import { pendingGuestMessageCount } from "@t3tools/contracts";
2+
import { applyOrchestrationV2ThreadVisit, pendingGuestMessageCount } from "@t3tools/contracts";
33
import { applyThreadPullRequestLinkUpdate } from "../sourceControl/threadPullRequests.ts";
44
import { projectTurnItemForWire } from "./WireProjection.ts";
55
import * as Stream from "effect/Stream";
@@ -872,6 +872,11 @@ export function applyToProjection(
872872
...projection,
873873
thread: event.payload,
874874
};
875+
case "thread.visit-recorded":
876+
return {
877+
...projection,
878+
thread: applyOrchestrationV2ThreadVisit(projection.thread, event.payload),
879+
};
875880
case "run.created":
876881
case "run.updated":
877882
return withLocalVisibleTurnItems({
@@ -2006,6 +2011,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
20062011
});
20072012
break;
20082013
}
2014+
case "thread.visit-recorded": {
2015+
const thread = yield* getThread(event.threadId);
2016+
yield* apply({
2017+
...event,
2018+
type: "thread.visited",
2019+
payload: applyOrchestrationV2ThreadVisit(thread, event.payload),
2020+
});
2021+
break;
2022+
}
20092023
case "thread.created":
20102024
case "thread.archived":
20112025
case "thread.unarchived":
@@ -2835,6 +2849,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28352849
event.type !== "thread.active-reordered" &&
28362850
event.type !== "thread.tags-updated" &&
28372851
event.type !== "thread.visited" &&
2852+
event.type !== "thread.visit-recorded" &&
28382853
event.type !== "thread.marked-unread" &&
28392854
event.type !== "thread.metadata-updated" &&
28402855
event.type !== "thread.pull-request-synced" &&

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7557,6 +7557,19 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
75577557
assert.isNotNull(visited.thread.lastVisitedAt);
75587558
assert.equal(DateTime.formatIso(visited.thread.lastVisitedAt!), visitedAt);
75597559
assert.deepEqual(visited.thread.updatedAt, createdUpdatedAt);
7560+
const sql = yield* SqlClient.SqlClient;
7561+
// The event carries the read state only, not a copy of the thread.
7562+
const visitEvents = yield* sql<{
7563+
readonly event_type: string;
7564+
readonly payload_json: string;
7565+
}>`
7566+
SELECT event_type, payload_json FROM orchestration_events
7567+
WHERE command_id = ${"runtime-layer-visited-thread-visit"}
7568+
`;
7569+
assert.deepEqual(
7570+
visitEvents.map((row) => [row.event_type, JSON.parse(row.payload_json)]),
7571+
[["thread.visit-recorded", { lastVisitedAt: visitedAt }]],
7572+
);
75607573

75617574
yield* orchestrator.dispatch({
75627575
type: "thread.visit",
@@ -7582,7 +7595,6 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
75827595
.pipe(Effect.flip);
75837596
assert.instanceOf(markUnread, Orchestrator.OrchestratorDispatchError);
75847597

7585-
const sql = yield* SqlClient.SqlClient;
75867598
yield* sql`INSERT INTO orchestration_v2_projection_turn_items (
75877599
turn_item_id, thread_id, ordinal, type, status, updated_at, payload_json
75887600
) VALUES (
@@ -7789,6 +7801,23 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("thread opens", (it)
77897801
{ query: "uploadthing docs", at: start + 60_000 },
77907802
{ query: "upl", at: start },
77917803
]);
7804+
7805+
const sql = yield* SqlClient.SqlClient;
7806+
const [lastOpen] = yield* sql<{
7807+
readonly event_type: string;
7808+
readonly payload_json: string;
7809+
}>`
7810+
SELECT event_type, payload_json FROM orchestration_events
7811+
WHERE command_id = ${`runtime-layer-opened-visit-${visits}`}
7812+
`;
7813+
assert.equal(lastOpen?.event_type, "thread.visit-recorded");
7814+
assert.sameMembers(Object.keys(JSON.parse(lastOpen!.payload_json)), [
7815+
"lastVisitedAt",
7816+
"opens",
7817+
]);
7818+
const replay = yield* ProjectionReplay.ProjectionReplay;
7819+
assert.isTrue((yield* replay.rebuild).valid);
7820+
assert.deepEqual(yield* opens, later);
77927821
}),
77937822
);
77947823
});

‎apps/server/src/relay/AgentAwarenessRelay.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function shouldPublishAgentAwarenessEvent(
111111
case "thread.active-reordered":
112112
case "thread.tags-updated":
113113
case "thread.visited":
114+
case "thread.visit-recorded":
114115
case "thread.marked-unread":
115116
case "thread.runtime-mode-updated":
116117
case "thread.interaction-mode-updated":

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,33 @@ describe("applyOrchestrationV2ProjectionEvent", () => {
134134
expect(next?.thread.pullRequests?.[0]).toBe(links[0]);
135135
});
136136

137+
it("applies a visit's read state without replacing the rest of the thread", () => {
138+
const lastVisitedAt = DateTime.makeUnsafe("2026-06-20T02:00:00.000Z");
139+
const opens = { openedAt: 1, score: 1, picks: [] };
140+
const projection = {
141+
...emptyProjection,
142+
thread: { ...emptyProjection.thread, title: "Kept", opens },
143+
};
144+
const visit = (payload: { lastVisitedAt: DateTime.Utc; opens?: typeof opens }) =>
145+
applyOrchestrationV2ProjectionEvent(projection, {
146+
id: "visit" as OrchestrationV2DomainEvent["id"],
147+
type: "thread.visit-recorded",
148+
threadId,
149+
occurredAt: lastVisitedAt,
150+
payload,
151+
});
152+
// Opens stay unless the visit was an open.
153+
expect(visit({ lastVisitedAt })?.thread).toEqual({ ...projection.thread, lastVisitedAt });
154+
const reopened = { ...opens, score: 2 };
155+
expect(visit({ lastVisitedAt, opens: reopened })?.thread).toEqual({
156+
...projection.thread,
157+
lastVisitedAt,
158+
opens: reopened,
159+
});
160+
// Read state is not activity.
161+
expect(visit({ lastVisitedAt })?.updatedAt).toEqual(projection.updatedAt);
162+
});
163+
137164
it("keeps live token usage when the terminal provider turn omits it", () => {
138165
const providerTurnId = ProviderTurnId.make("provider-turn-reducer");
139166
const running = {

‎packages/client-runtime/src/state/orchestrationV2Projection.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { applyThreadPullRequestLinkUpdate } from "../threadPullRequests.ts";
2+
import { applyOrchestrationV2ThreadVisit } from "@t3tools/contracts";
23
import type {
34
OrchestrationV2DomainEvent,
45
OrchestrationV2Run,
@@ -298,6 +299,11 @@ export function applyOrchestrationV2ProjectionEvent(
298299
case "thread.tags-updated":
299300
case "thread.marked-unread":
300301
return { ...projection, thread: event.payload };
302+
case "thread.visit-recorded":
303+
return {
304+
...projection,
305+
thread: applyOrchestrationV2ThreadVisit(projection.thread, event.payload),
306+
};
301307
case "run.created":
302308
case "run.updated": {
303309
const next = { ...base, runs: upsertEntity(base.runs, event.payload) };

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,24 @@ export const OrchestrationV2AppThread = Schema.Struct({
517517
});
518518
export type OrchestrationV2AppThread = typeof OrchestrationV2AppThread.Type;
519519

520+
/** A visit changes read state only, so its event leaves the rest of the thread out. */
521+
export const OrchestrationV2ThreadVisit = Schema.Struct({
522+
lastVisitedAt: OrchestrationV2AppThread.fields.lastVisitedAt,
523+
opens: OrchestrationV2AppThread.fields.opens,
524+
});
525+
export type OrchestrationV2ThreadVisit = typeof OrchestrationV2ThreadVisit.Type;
526+
527+
export function applyOrchestrationV2ThreadVisit<Thread extends OrchestrationV2AppThread>(
528+
thread: Thread,
529+
visit: OrchestrationV2ThreadVisit,
530+
): Thread {
531+
return {
532+
...thread,
533+
lastVisitedAt: visit.lastVisitedAt,
534+
...(visit.opens === undefined ? {} : { opens: visit.opens }),
535+
};
536+
}
537+
520538
/** Provider-native children cannot receive messages; T3 delegate_task children can. */
521539
export function isProviderNativeSubagentThread(
522540
thread: Pick<OrchestrationV2AppThread, "lineage" | "creationSource">,
@@ -1911,6 +1929,11 @@ export const OrchestrationV2DomainEvent = Schema.Union([
19111929
type: Schema.Literal("thread.pull-request-link-updated"),
19121930
payload: ThreadPullRequestLink,
19131931
}),
1932+
Schema.Struct({
1933+
...OrchestrationV2EventBase.fields,
1934+
type: Schema.Literal("thread.visit-recorded"),
1935+
payload: OrchestrationV2ThreadVisit,
1936+
}),
19141937
Schema.Struct({
19151938
...OrchestrationV2EventBase.fields,
19161939
type: Schema.Literal("run.created"),
@@ -2308,6 +2331,11 @@ export const OrchestrationV2AppThreadJson = OrchestrationV2AppThread.mapFields((
23082331
}));
23092332
export type OrchestrationV2AppThreadJson = typeof OrchestrationV2AppThreadJson.Type;
23102333

2334+
export const OrchestrationV2ThreadVisitJson = Schema.Struct({
2335+
lastVisitedAt: OrchestrationV2AppThreadJson.fields.lastVisitedAt,
2336+
opens: OrchestrationV2AppThreadJson.fields.opens,
2337+
});
2338+
23112339
export const OrchestrationV2RunJson = OrchestrationV2Run.mapFields((fields) => ({
23122340
...fields,
23132341
requestedAt: Schema.DateTimeUtcFromString,
@@ -2796,6 +2824,11 @@ export const OrchestrationV2DomainEventJson = Schema.Union([
27962824
type: Schema.Literal("thread.pull-request-link-updated"),
27972825
payload: ThreadPullRequestLink,
27982826
}),
2827+
Schema.Struct({
2828+
...OrchestrationV2JsonEventBaseFields,
2829+
type: Schema.Literal("thread.visit-recorded"),
2830+
payload: OrchestrationV2ThreadVisitJson,
2831+
}),
27992832
Schema.Struct({
28002833
...OrchestrationV2JsonEventBaseFields,
28012834
type: Schema.Literal("run.created"),

0 commit comments

Comments
 (0)