Skip to content

Commit b90d138

Browse files
t3dotggclaude
andcommitted
perf(orchestration): thread snapshots send each long ID once
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 453bf01 commit b90d138

14 files changed

Lines changed: 365 additions & 55 deletions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type ThreadSnapshotStreamItem = Extract<
4242
export function boundedSnapshotResponseFields(input: {
4343
readonly bounded: BoundedProjectionResult;
4444
readonly compactTurnItems: boolean;
45+
readonly compactIds: boolean;
4546
}) {
4647
const { bounded } = input;
4748
const compactProjection = input.compactTurnItems
@@ -53,13 +54,15 @@ export function boundedSnapshotResponseFields(input: {
5354
hasMoreHistory: bounded.hasMoreHistory,
5455
latestLocalTurnOrdinal: bounded.latestLocalTurnOrdinal,
5556
...(compactProjection === null ? {} : { turnItemsOmitLocalVisible: true as const }),
57+
...(input.compactIds ? { idTable: true as const } : {}),
5658
};
5759
}
5860

5961
export function buildBoundedThreadStreamSnapshot(input: {
6062
readonly snapshotSequence: number;
6163
readonly projection: OrchestrationV2ThreadProjection;
6264
readonly compactTurnItems?: boolean;
65+
readonly compactIds?: boolean;
6366
}): ThreadSnapshotStreamItem {
6467
const bounded = buildBoundedThreadProjection({
6568
snapshotSequence: input.snapshotSequence,
@@ -71,6 +74,7 @@ export function buildBoundedThreadStreamSnapshot(input: {
7174
...boundedSnapshotResponseFields({
7275
bounded,
7376
compactTurnItems: input.compactTurnItems === true,
77+
compactIds: input.compactIds === true,
7478
}),
7579
};
7680
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ORCHESTRATION_PROTOCOL_HEADER,
1313
ORCHESTRATION_PROTOCOL_VERSION,
1414
OrchestrationV2ThreadBoundedSnapshot,
15+
OrchestrationV2ThreadHistoryPage,
1516
OrchestrationV2ThreadStreamItem,
1617
ProjectId,
1718
ProviderDriverKind,
@@ -48,6 +49,9 @@ import * as ThreadManagementService from "./ThreadManagementService.ts";
4849
const decodeBounded = Schema.decodeUnknownSync(
4950
Schema.fromJsonString(Schema.toCodecJson(OrchestrationV2ThreadBoundedSnapshot)),
5051
);
52+
const decodeHistoryPage = Schema.decodeUnknownSync(
53+
Schema.fromJsonString(Schema.toCodecJson(OrchestrationV2ThreadHistoryPage)),
54+
);
5155
const decodeStreamItem = Schema.decodeUnknownSync(
5256
Schema.toCodecJson(OrchestrationV2ThreadStreamItem),
5357
);
@@ -74,6 +78,10 @@ const management = Layer.unwrap(
7478
getThreadSnapshot: (id) => projections.getThreadSnapshot(id).pipe(Effect.orDie),
7579
getThreadSnapshotWindow: (id, options) =>
7680
projections.getThreadSnapshotWindow(id, options).pipe(Effect.orDie),
81+
getThreadHistoryPage: (id, cursor, throughEntryId, conversationOnly) =>
82+
projections
83+
.getThreadHistoryPage(id, cursor, throughEntryId, conversationOnly)
84+
.pipe(Effect.orDie),
7785
streamStoredEventsFrom: () => Stream.empty,
7886
});
7987
}),
@@ -332,6 +340,46 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => {
332340
),
333341
);
334342

343+
it.effect("HTTP: the ID table is opt-in and decodes to the same snapshot", () =>
344+
withHttp(async (get) => {
345+
const base = `/api/orchestration/threads/${encodeURIComponent(threadId)}/bounded?compactTurnItems=1`;
346+
const plainBody = await (await get(base)).text();
347+
const tableBody = await (await get(`${base}&compactIds=1`)).text();
348+
const wire = JSON.parse(tableBody);
349+
350+
assert.notProperty(JSON.parse(plainBody), "ids");
351+
assert.isArray(wire.ids);
352+
assert.isNumber(wire.projection.visibleTurnItems[0].item.id);
353+
assert.isNumber(wire.projection.thread.id);
354+
assert.isBelow(tableBody.length, plainBody.length);
355+
const decoded = decodeBounded(tableBody);
356+
assert.deepStrictEqual(decoded, decodeBounded(plainBody));
357+
358+
// Only the long thread has older history to page.
359+
if (threadId !== LONG) return;
360+
assert.isNotNull(decoded.historyCursor);
361+
const history = `/api/orchestration/threads/${encodeURIComponent(threadId)}/history?cursor=${encodeURIComponent(decoded.historyCursor ?? "")}`;
362+
const plainPage = await (await get(history)).text();
363+
const tablePage = await (await get(`${history}&compactIds=1`)).text();
364+
assert.isArray(JSON.parse(tablePage).ids);
365+
const page = decodeHistoryPage(tablePage);
366+
assert.isAbove(page.items.length, 0);
367+
assert.deepStrictEqual(page, decodeHistoryPage(plainPage));
368+
}),
369+
);
370+
371+
it.effect("WS: the ID table is opt-in and decodes to the same snapshot", () =>
372+
Effect.gen(function* () {
373+
const input = { threadId, acceptBoundedSnapshot: true, acceptCompactTurnItems: true };
374+
const plain = yield* firstSnapshot(input);
375+
const table = yield* firstSnapshot({ ...input, acceptCompactIds: true });
376+
377+
assert.notProperty(encodeStreamItem(plain.raw), "ids");
378+
assert.property(encodeStreamItem(table.raw), "ids");
379+
assert.deepStrictEqual(table.decoded, plain.decoded);
380+
}),
381+
);
382+
335383
it.effect("WS: bounded snapshots stay unchanged unless the client opts in", () =>
336384
Effect.gen(function* () {
337385
const legacy = yield* firstSnapshot({ threadId, acceptBoundedSnapshot: true });

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ export const layer = HttpApiBuilder.group(
219219
...boundedSnapshotResponseFields({
220220
bounded,
221221
compactTurnItems: args.query.compactTurnItems === "1",
222+
compactIds: args.query.compactIds === "1",
222223
}),
223224
};
224225
}),
@@ -243,6 +244,9 @@ export const layer = HttpApiBuilder.group(
243244
args.query.view === "conversation",
244245
)
245246
.pipe(
247+
Effect.map((page) =>
248+
args.query.compactIds === "1" ? { ...page, idTable: true as const } : page,
249+
),
246250
traceLocalHandlerWork,
247251
Effect.catch(
248252
Effect.fnUntraced(function* (cause) {

‎apps/server/src/ws.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
786786
readonly requestCompletionMarker?: boolean;
787787
readonly acceptBoundedSnapshot?: boolean;
788788
readonly acceptCompactTurnItems?: boolean;
789+
readonly acceptCompactIds?: boolean;
789790
}) {
790791
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
791792
const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
@@ -886,6 +887,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
886887
? buildBoundedThreadStreamSnapshot({
887888
...snapshot,
888889
compactTurnItems: input.acceptCompactTurnItems === true,
890+
compactIds: input.acceptCompactIds === true,
889891
})
890892
: {
891893
kind: "snapshot" as const,

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { ThreadId } from "@t3tools/contracts";
1+
import { OrchestrationV2ThreadBoundedSnapshot, ThreadId } from "@t3tools/contracts";
22
import { describe, expect, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
5+
import * as Schema from "effect/Schema";
56

67
import { PrimaryConnectionTarget, type PreparedConnection } from "../connection/model.ts";
78
import * as RpcHttp from "../rpc/http.ts";
@@ -117,22 +118,29 @@ const boundedBody = (compact: boolean) => ({
117118
latestLocalTurnOrdinal: 2,
118119
...(compact ? { turnItemsOmitLocalVisible: true } : {}),
119120
});
121+
const BoundedJson = Schema.toCodecJson(OrchestrationV2ThreadBoundedSnapshot);
122+
const idTableBody = () => {
123+
const snapshot = Schema.decodeUnknownSync(BoundedJson)(boundedBody(true));
124+
return Schema.encodeSync(BoundedJson)({ ...snapshot, idTable: true });
125+
};
120126

121127
describe("boundedThreadSnapshotLoader", () => {
122128
it.effect.each([
123-
["an older server that ignores the opt-in", false],
124-
["a server that sends compact turnItems", true],
125-
] as const)("requests compact turnItems and loads %s", ([, compact]) => {
129+
["an older server that ignores the opt-in", () => boundedBody(false)],
130+
["a server that sends compact turnItems", () => boundedBody(true)],
131+
["a server that also sends an ID table", idTableBody],
132+
] as const)("requests compact turnItems and loads %s", ([, body]) => {
126133
const urls: string[] = [];
127134
const fetchFn = ((input: RequestInfo | URL) => {
128135
urls.push(String(input));
129-
return Promise.resolve(Response.json(boundedBody(compact)));
136+
return Promise.resolve(Response.json(body()));
130137
}) satisfies typeof fetch;
131138

132139
return Effect.gen(function* () {
133140
const loader = yield* ThreadSnapshotLoader.ThreadSnapshotLoader;
134141
const result = yield* loader.load(PREPARED, THREAD_ID);
135142
expect(new URL(urls[0]!).searchParams.get("compactTurnItems")).toBe("1");
143+
expect(new URL(urls[0]!).searchParams.get("compactIds")).toBe("1");
136144
expect(result._tag).toBe("present");
137145
if (result._tag !== "present") return;
138146
expect(result.snapshot.projection.turnItems.map((item) => String(item.id))).toEqual([

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const fetchEnvironmentBoundedThreadSnapshot = Effect.fn(
3030
}) {
3131
const endpoint = {
3232
params: { threadId: input.threadId },
33-
query: { compactTurnItems: "1" },
33+
query: { compactTurnItems: "1", compactIds: "1" },
3434
};
3535
return yield* executeAuthenticatedEnvironmentHttpRequest({
3636
...input,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const fetchEnvironmentThreadHistoryPage = Effect.fn(
2828
params: { threadId: input.threadId },
2929
query: {
3030
cursor: input.cursor,
31+
compactIds: "1",
3132
...(input.view === undefined ? {} : { view: input.view }),
3233
...(input.throughEntryId === undefined ? {} : { throughEntryId: input.throughEntryId }),
3334
},

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,11 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
961961
...(canResume ? { afterSequence: sequence } : {}),
962962
...(supportsCompletionMarker ? { requestCompletionMarker: true as const } : {}),
963963
...(acceptBoundedSnapshot
964-
? { acceptBoundedSnapshot: true as const, acceptCompactTurnItems: true as const }
964+
? {
965+
acceptBoundedSnapshot: true as const,
966+
acceptCompactTurnItems: true as const,
967+
acceptCompactIds: true as const,
968+
}
965969
: {}),
966970
};
967971
}),

‎packages/contracts/src/baseSchemas.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ import * as Schema from "effect/Schema";
44
import * as SchemaGetter from "effect/SchemaGetter";
55
import * as SchemaTransformation from "effect/SchemaTransformation";
66

7+
// Hermes copies the string on every `trim()`, even when nothing changes.
8+
// Returning the original keeps repeated IDs shared after decode.
9+
const trim = (value: string) => {
10+
const trimmed = value.trim();
11+
return trimmed.length === value.length ? value : trimmed;
12+
};
13+
714
export const TrimmedString = Schema.String.pipe(
815
Schema.decodeTo(
916
Schema.String,
1017
SchemaTransformation.transformEffect({
11-
decode: (value) => Effect.succeed(value.trim()),
12-
encode: (value) => Effect.succeed(value.trim()),
18+
decode: (value) => Effect.succeed(trim(value)),
19+
encode: (value) => Effect.succeed(trim(value)),
1320
}),
1421
),
1522
);

‎packages/contracts/src/environmentHttp.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,14 @@ const EnvironmentOrchestrationThreadSnapshotParams = Schema.Struct({
669669

670670
const EnvironmentOrchestrationThreadBoundedSnapshotQuery = Schema.Struct({
671671
compactTurnItems: Schema.optionalKey(Schema.String),
672+
compactIds: Schema.optionalKey(Schema.String),
672673
});
673674

674675
const EnvironmentOrchestrationThreadHistoryQuery = Schema.Struct({
675676
cursor: TrimmedNonEmptyString,
676677
throughEntryId: Schema.optional(TrimmedNonEmptyString),
677678
view: Schema.optional(Schema.Literals(["conversation", "activity"])),
679+
compactIds: Schema.optionalKey(Schema.String),
678680
});
679681

680682
const EnvironmentOrchestrationThreadHistoryErrors = [

0 commit comments

Comments
 (0)