Skip to content

Commit 8045537

Browse files
t3dotggclaude
andauthored
perf(orchestration): thread snapshots send each long ID once (#24643)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 24c80f3 commit 8045537

14 files changed

Lines changed: 369 additions & 54 deletions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function boundedSnapshotResponseFields(input: {
4141
readonly bounded: BoundedProjectionResult;
4242
readonly compactTurnItems: boolean;
4343
readonly dedupeBodies: boolean;
44+
readonly compactIds: boolean;
4445
}) {
4546
const { bounded } = input;
4647
// Dedupe keeps shared items one object, which the compaction's identity check needs.
@@ -54,6 +55,7 @@ export function boundedSnapshotResponseFields(input: {
5455
hasMoreHistory: bounded.hasMoreHistory,
5556
latestLocalTurnOrdinal: bounded.latestLocalTurnOrdinal,
5657
...(compactProjection === null ? {} : { turnItemsOmitLocalVisible: true as const }),
58+
...(input.compactIds ? { idTable: true as const } : {}),
5759
};
5860
}
5961

@@ -62,6 +64,7 @@ export function buildBoundedThreadStreamSnapshot(input: {
6264
readonly projection: OrchestrationV2ThreadProjection;
6365
readonly compactTurnItems?: boolean;
6466
readonly dedupeBodies?: boolean;
67+
readonly compactIds?: boolean;
6568
}): ThreadSnapshotStreamItem {
6669
const bounded = buildBoundedThreadProjection({
6770
snapshotSequence: input.snapshotSequence,
@@ -74,6 +77,7 @@ export function buildBoundedThreadStreamSnapshot(input: {
7477
bounded,
7578
compactTurnItems: input.compactTurnItems === true,
7679
dedupeBodies: input.dedupeBodies === true,
80+
compactIds: input.compactIds === true,
7781
}),
7882
};
7983
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ORCHESTRATION_PROTOCOL_HEADER,
1616
ORCHESTRATION_PROTOCOL_VERSION,
1717
OrchestrationV2ThreadBoundedSnapshot,
18+
OrchestrationV2ThreadHistoryPage,
1819
OrchestrationV2ThreadStreamItem,
1920
ProjectId,
2021
ProviderDriverKind,
@@ -53,6 +54,9 @@ import * as ThreadManagementService from "./ThreadManagementService.ts";
5354
const decodeBounded = Schema.decodeUnknownSync(
5455
Schema.fromJsonString(Schema.toCodecJson(OrchestrationV2ThreadBoundedSnapshot)),
5556
);
57+
const decodeHistoryPage = Schema.decodeUnknownSync(
58+
Schema.fromJsonString(Schema.toCodecJson(OrchestrationV2ThreadHistoryPage)),
59+
);
5660
const decodeStreamItem = Schema.decodeUnknownSync(
5761
Schema.toCodecJson(OrchestrationV2ThreadStreamItem),
5862
);
@@ -81,6 +85,10 @@ const management = Layer.unwrap(
8185
getThreadSnapshot: (id) => projections.getThreadSnapshot(id).pipe(Effect.orDie),
8286
getThreadSnapshotWindow: (id, options) =>
8387
projections.getThreadSnapshotWindow(id, options).pipe(Effect.orDie),
88+
getThreadHistoryPage: (id, cursor, throughEntryId, conversationOnly) =>
89+
projections
90+
.getThreadHistoryPage(id, cursor, throughEntryId, conversationOnly)
91+
.pipe(Effect.orDie),
8492
streamStoredEventsFrom: (input) =>
8593
input?.threadId === BODIES ? Stream.fromIterable(bodiesLiveTail()) : Stream.empty,
8694
});
@@ -492,6 +500,46 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => {
492500
),
493501
);
494502

503+
it.effect("HTTP: the ID table is opt-in and decodes to the same snapshot", () =>
504+
withHttp(async (get) => {
505+
const base = `/api/orchestration/threads/${encodeURIComponent(threadId)}/bounded?compactTurnItems=1`;
506+
const plainBody = await (await get(base)).text();
507+
const tableBody = await (await get(`${base}&compactIds=1`)).text();
508+
const wire = JSON.parse(tableBody);
509+
510+
assert.notProperty(JSON.parse(plainBody), "ids");
511+
assert.isArray(wire.ids);
512+
assert.isNumber(wire.projection.visibleTurnItems[0].item.id);
513+
assert.isNumber(wire.projection.thread.id);
514+
assert.isBelow(tableBody.length, plainBody.length);
515+
const decoded = decodeBounded(tableBody);
516+
assert.deepStrictEqual(decoded, decodeBounded(plainBody));
517+
518+
// Only the long thread has older history to page.
519+
if (threadId !== LONG) return;
520+
assert.isNotNull(decoded.historyCursor);
521+
const history = `/api/orchestration/threads/${encodeURIComponent(threadId)}/history?cursor=${encodeURIComponent(decoded.historyCursor ?? "")}`;
522+
const plainPage = await (await get(history)).text();
523+
const tablePage = await (await get(`${history}&compactIds=1`)).text();
524+
assert.isArray(JSON.parse(tablePage).ids);
525+
const page = decodeHistoryPage(tablePage);
526+
assert.isAbove(page.items.length, 0);
527+
assert.deepStrictEqual(page, decodeHistoryPage(plainPage));
528+
}),
529+
);
530+
531+
it.effect("WS: the ID table is opt-in and decodes to the same snapshot", () =>
532+
Effect.gen(function* () {
533+
const input = { threadId, acceptBoundedSnapshot: true, acceptCompactTurnItems: true };
534+
const plain = yield* firstSnapshot(input);
535+
const table = yield* firstSnapshot({ ...input, acceptCompactIds: true });
536+
537+
assert.notProperty(encodeStreamItem(plain.raw), "ids");
538+
assert.property(encodeStreamItem(table.raw), "ids");
539+
assert.deepStrictEqual(table.decoded, plain.decoded);
540+
}),
541+
);
542+
495543
it.effect("WS: bounded snapshots stay unchanged unless the client opts in", () =>
496544
Effect.gen(function* () {
497545
const legacy = yield* firstSnapshot({ threadId, acceptBoundedSnapshot: true });
@@ -546,6 +594,15 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => {
546594
boundedSnapshotProjection(decodeBounded(dedupedBody)),
547595
boundedSnapshotProjection(decodeBounded(compactBody)),
548596
);
597+
// The client sends both opt-ins: bodies restore after the ID table resolves.
598+
const tabledBody = await (
599+
await get(`${base}?compactTurnItems=1&dedupeBodies=1&compactIds=1`)
600+
).text();
601+
assert.property(JSON.parse(tabledBody), "ids");
602+
assert.deepStrictEqual(
603+
boundedSnapshotProjection(decodeBounded(tabledBody)),
604+
boundedSnapshotProjection(decodeBounded(compactBody)),
605+
);
549606
}),
550607
);
551608

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export const layer = HttpApiBuilder.group(
228228
bounded,
229229
compactTurnItems: args.query.compactTurnItems === "1",
230230
dedupeBodies: args.query.dedupeBodies === "1",
231+
compactIds: args.query.compactIds === "1",
231232
}),
232233
};
233234
}),
@@ -252,6 +253,9 @@ export const layer = HttpApiBuilder.group(
252253
args.query.view === "conversation",
253254
)
254255
.pipe(
256+
Effect.map((page) =>
257+
args.query.compactIds === "1" ? { ...page, idTable: true as const } : page,
258+
),
255259
traceLocalHandlerWork,
256260
Effect.catch(
257261
Effect.fnUntraced(function* (cause) {

‎apps/server/src/ws.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
787787
readonly acceptBoundedSnapshot?: boolean;
788788
readonly acceptCompactTurnItems?: boolean;
789789
readonly acceptDedupedBodies?: boolean;
790+
readonly acceptCompactIds?: boolean;
790791
}) {
791792
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
792793
const dedupeBodies = input.acceptDedupedBodies === true;
@@ -890,6 +891,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
890891
...snapshot,
891892
compactTurnItems: input.acceptCompactTurnItems === true,
892893
dedupeBodies,
894+
compactIds: input.acceptCompactIds === true,
893895
})
894896
: (() => {
895897
const projection = projectThreadProjectionForWire(snapshot.projection);

‎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", dedupeBodies: "1" },
33+
query: { compactTurnItems: "1", dedupeBodies: "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
acceptDedupedBodies: true as const,
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
@@ -674,12 +674,14 @@ const EnvironmentOrchestrationShellSnapshotQuery = Schema.Struct({
674674
const EnvironmentOrchestrationThreadBoundedSnapshotQuery = Schema.Struct({
675675
compactTurnItems: Schema.optionalKey(Schema.String),
676676
dedupeBodies: Schema.optionalKey(Schema.String),
677+
compactIds: Schema.optionalKey(Schema.String),
677678
});
678679

679680
const EnvironmentOrchestrationThreadHistoryQuery = Schema.Struct({
680681
cursor: TrimmedNonEmptyString,
681682
throughEntryId: Schema.optional(TrimmedNonEmptyString),
682683
view: Schema.optional(Schema.Literals(["conversation", "activity"])),
684+
compactIds: Schema.optionalKey(Schema.String),
683685
});
684686

685687
const EnvironmentOrchestrationThreadHistoryErrors = [

0 commit comments

Comments
 (0)