Skip to content

Commit 4a62736

Browse files
committed
fix(history): bound human turn snapshots and cached history
1 parent 81493ef commit 4a62736

8 files changed

Lines changed: 173 additions & 47 deletions

File tree

‎apps/mobile/src/connection/environment-cache-store.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ORCHESTRATION_CACHE_SCHEMA_VERSION,
33
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
44
StoredOrchestrationThreadSnapshot,
5+
decodeStoredOrchestrationThreadSnapshot as decodeStoredThreadSnapshot,
56
Persistence,
67
} from "@t3tools/client-runtime/platform";
78
import { type EnvironmentId, ServerConfig, VcsListRefsResult } from "@t3tools/contracts";
@@ -33,9 +34,6 @@ const StoredVcsRefs = Schema.Struct({
3334
refs: VcsListRefsResult,
3435
});
3536

36-
const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(
37-
Schema.fromJsonString(StoredOrchestrationThreadSnapshot),
38-
);
3937
const encodeStoredThreadSnapshot = Schema.encodeEffect(
4038
Schema.fromJsonString(StoredOrchestrationThreadSnapshot),
4139
);

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import * as ProjectionStore from "./ProjectionStore.ts";
3838
import {
3939
buildBoundedThreadProjection,
4040
decodeThreadHistoryCursor,
41+
projectedRowEncodedBytes,
4142
selectHistoryPageFromCursor,
4243
THREAD_HISTORY_PAGE_POLICY,
4344
} from "./threadHistoryPaging.ts";
@@ -863,7 +864,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
863864
}),
864865
);
865866

866-
it.effect("pages complete user turns through SQL regardless of tool count or payload size", () =>
867+
it.effect("bounds SQL history inside long user turns and recovers every item through paging", () =>
867868
Effect.gen(function* () {
868869
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
869870
const sql = yield* SqlClient.SqlClient;
@@ -1007,18 +1008,18 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
10071008
nodePlan.map((row) => row.detail),
10081009
"SEARCH orchestration_v2_projection_nodes USING INDEX orchestration_v2_projection_nodes_live_idx (thread_id=?)",
10091010
);
1010-
// Only the selected turn cohort and two lookahead anchors are decoded.
1011-
assert.lengthOf(initial.projection.turnItems, 12 * 102);
1011+
// Human turns must not bypass the SQL row limit before payload decoding.
1012+
assert.lengthOf(initial.projection.turnItems, 77);
10121013
const bounded = buildBoundedThreadProjection({
10131014
projection: initial.projection,
10141015
snapshotSequence: 0,
10151016
});
1016-
assert.lengthOf(bounded.projection.visibleTurnItems, 10 * 102);
1017-
assert.strictEqual(bounded.projection.visibleTurnItems[0]?.sourceItemId, allIds[35 * 102]);
1017+
assert.lengthOf(bounded.projection.visibleTurnItems, THREAD_HISTORY_PAGE_POLICY.maxItems);
1018+
assert.strictEqual(bounded.projection.visibleTurnItems[0]?.sourceItemId, allIds.at(-75));
10181019
const loaded = bounded.projection.visibleTurnItems.map((row) => String(row.sourceItemId));
10191020
let cursor = bounded.historyCursor;
1020-
for (const turns of [20, 15]) {
1021-
assert.isNotNull(cursor);
1021+
for (let pages = 0; cursor !== null; pages += 1) {
1022+
assert.isBelow(pages, allIds.length);
10221023
const anchor = decodeThreadHistoryCursor(cursor!);
10231024
const snapshot = yield* projectionStore.getThreadSnapshotWindow(threadId, {
10241025
rowLimit: 77,
@@ -1031,17 +1032,42 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
10311032
cursor: cursor!,
10321033
snapshotSequence: 0,
10331034
});
1034-
assert.lengthOf(page.items, turns * 102);
1035+
assert.isAtMost(snapshot.projection.turnItems.length, 77);
1036+
assert.isAbove(page.items.length, 0);
1037+
assert.isAtMost(page.items.length, THREAD_HISTORY_PAGE_POLICY.maxItems);
10351038
loaded.unshift(...page.items.map((row) => String(row.sourceItemId)));
10361039
cursor = page.nextCursor;
10371040
}
10381041
assert.isNull(cursor);
10391042
assert.deepEqual(loaded, allIds);
1043+
// Exercise the production history service after wire projection as well.
1044+
yield* sql`UPDATE orchestration_v2_projection_turn_items
1045+
SET payload_json = json_set(payload_json, '$.output', ${"x".repeat(32_000)})
1046+
WHERE thread_id = ${threadId} AND type = 'command_execution'
1047+
AND ordinal > ${allIds.length - 150} AND ordinal <= ${allIds.length - 75}`;
1048+
const recovered: string[] = [];
1049+
let historyCursor = bounded.historyCursor;
1050+
for (let pages = 0; recovered.length < 75; pages += 1) {
1051+
assert.isBelow(pages, 75);
1052+
assert.isNotNull(historyCursor);
1053+
const page = yield* projectionStore.getThreadHistoryPage(threadId, historyCursor!);
1054+
assert.isAbove(page.items.length, 0);
1055+
assert.isAtMost(page.items.length, THREAD_HISTORY_PAGE_POLICY.maxItems);
1056+
assert.isAtMost(
1057+
page.items.reduce((size, row) => size + projectedRowEncodedBytes(row), 0),
1058+
THREAD_HISTORY_PAGE_POLICY.maxEncodedBytes,
1059+
);
1060+
if (pages === 0) assert.isBelow(page.items.length, 75);
1061+
recovered.unshift(...page.items.map((row) => String(row.sourceItemId)));
1062+
historyCursor = page.nextCursor;
1063+
}
1064+
assert.deepEqual(recovered, allIds.slice(-75 - recovered.length, -75));
10401065
}),
10411066
);
10421067

10431068
it.effect("caps a SQL window by runs that a notification started", () =>
10441069
Effect.gen(function* () {
1070+
const policy = { ...THREAD_HISTORY_PAGE_POLICY, maxItems: 1_000 };
10451071
// A thread woken by PR watches: 12 user turns, then 400 runs that a
10461072
// notification started, with a user turn every 50 runs. The user-turn
10471073
// window reached back to the 12th-newest user turn and read almost every row.
@@ -1183,7 +1209,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
11831209

11841210
const { statements, tracer } = traceSqlStatements();
11851211
const initial = yield* projectionStore
1186-
.getThreadSnapshotWindow(threadId, { rowLimit: 77, userTurnLimit: 10 })
1212+
.getThreadSnapshotWindow(threadId, { rowLimit: 1_000, userTurnLimit: 10 })
11871213
.pipe(Effect.withTracer(tracer));
11881214
// The 152 newest turns are the 152 newest started runs, 261 to 412. Only
11891215
// three of them are user turns, short of the 12 the user-turn window needs.
@@ -1210,6 +1236,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
12101236
const bounded = buildBoundedThreadProjection({
12111237
projection: initial.projection,
12121238
snapshotSequence: 0,
1239+
policy,
12131240
});
12141241
assert.strictEqual(bounded.projection.visibleTurnItems[0]?.sourceItemId, startIds[262]);
12151242
const loaded = bounded.projection.visibleTurnItems.map((row) => String(row.sourceItemId));
@@ -1218,7 +1245,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
12181245
assert.isBelow(pages, 5);
12191246
const anchor = decodeThreadHistoryCursor(cursor);
12201247
const snapshot = yield* projectionStore.getThreadSnapshotWindow(threadId, {
1221-
rowLimit: 77,
1248+
rowLimit: 1_000,
12221249
userTurnLimit: 20,
12231250
anchorItemId: TurnItemId.make(anchor.si),
12241251
anchorThreadId: ThreadId.make(anchor.st),
@@ -1229,6 +1256,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
12291256
items: snapshot.projection.visibleTurnItems,
12301257
cursor,
12311258
snapshotSequence: 0,
1259+
policy,
12321260
});
12331261
loaded.unshift(...page.items.map((row) => String(row.sourceItemId)));
12341262
cursor = page.nextCursor;

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,11 +2937,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
29372937
FROM eligible
29382938
WHERE ordinal >= (SELECT ordinal FROM boundary)
29392939
ORDER BY ordinal DESC, turn_item_id DESC
2940-
LIMIT CASE
2941-
WHEN ${window.rowLimit} = 0 THEN 0
2942-
WHEN (SELECT COUNT(*) FROM user_anchors) > 0 THEN -1
2943-
ELSE ${window.rowLimit}
2944-
END
2940+
LIMIT ${window.rowLimit}
29452941
), retained AS MATERIALIZED (
29462942
SELECT item_rowid, ordinal, turn_item_id FROM selected
29472943
UNION
@@ -5095,7 +5091,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
50955091
FROM orchestration_events WHERE application_event_version = 2
50965092
AND aggregate_kind = 'thread' AND stream_id = ${threadId}`;
50975093
const snapshotSequence = revisions[0]?.sequence ?? 0;
5098-
// Choose complete turns from the light index before loading any bodies.
5094+
// Bound row count from the light index before loading any bodies.
50995095
// A history page does not need nodes, provider turns, or other control state.
51005096
const page = yield* Effect.try({
51015097
try: () =>
@@ -5154,13 +5150,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
51545150
};
51555151
}),
51565152
);
5157-
if (hasUserTurns)
5158-
return {
5159-
...page,
5160-
snapshotSequence,
5161-
items: items.map((row, position) => ({ ...row, position })),
5162-
};
5163-
// The no-turn fallback also respects the encoded byte budget after projection.
5153+
// Apply the byte budget after projecting bodies, including human turns.
51645154
const bounded = selectRecentTimelineWindow({
51655155
items,
51665156
snapshotSequence,
@@ -6820,12 +6810,13 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
68206810
(index) => index >= rawStart && isThreadHistoryUserTurn(candidates[index]!.item),
68216811
);
68226812
const anchorLimit = (options.userTurnLimit ?? 0) + 2;
6823-
const start =
6813+
const turnStart =
68246814
anchors.length > 0
68256815
? anchors.length < anchorLimit
68266816
? rawStart
68276817
: anchors.at(-anchorLimit)!
6828-
: Math.max(0, anchorIndex - options.rowLimit);
6818+
: 0;
6819+
const start = Math.max(turnStart, anchorIndex - options.rowLimit, 0);
68296820
const visibleTurnItems = candidates.slice(start);
68306821
return {
68316822
...snapshot,

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const NOW = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
3131
const THREAD = ThreadId.make("thread-1");
3232
const RUN = RunId.make("run-interrupt-1");
3333
const NODE = NodeId.make("node-interrupt-1");
34+
const TURN_CEILING_POLICY = { ...THREAD_HISTORY_PAGE_POLICY, maxItems: 1_000 };
3435

3536
function makeRow(
3637
index: number,
@@ -194,7 +195,11 @@ describe("threadHistoryPaging", () => {
194195
};
195196
return [prompt, makeRow(turn * 2 + 1)];
196197
}).flat();
197-
const first = selectRecentTimelineWindow({ items, snapshotSequence: 1 });
198+
const first = selectRecentTimelineWindow({
199+
items,
200+
snapshotSequence: 1,
201+
policy: TURN_CEILING_POLICY,
202+
});
198203
expect(first.items).toHaveLength(300);
199204
expect(first.items[0]?.sourceItemId).toBe("item-22");
200205
const older = selectHistoryPageFromCursor({
@@ -246,7 +251,11 @@ describe("threadHistoryPaging", () => {
246251
inRun(run * 4 + 2, run, notification(`Steer ${run}`)),
247252
inRun(run * 4 + 3, run),
248253
]).flat();
249-
const first = selectRecentTimelineWindow({ items, snapshotSequence: 1 });
254+
const first = selectRecentTimelineWindow({
255+
items,
256+
snapshotSequence: 1,
257+
policy: TURN_CEILING_POLICY,
258+
});
250259
// 150 runs from the start of run 11. Its earlier row goes to the older page.
251260
expect(first.items).toHaveLength(150 * 4 - 1);
252261
expect(first.items[0]?.sourceItemId).toBe(`item-${11 * 4 + 1}`);
@@ -260,15 +269,15 @@ describe("threadHistoryPaging", () => {
260269
);
261270
});
262271

263-
it("pages agent-only child transcripts instead of dropping their earlier activity", () => {
272+
it.each(["agent", "user"] as const)("bounds long %s transcripts", (createdBy) => {
264273
const commandRows = Array.from({ length: 90 }, (_, index) => makeRow(index + 1));
265274
const first = makeRow(0);
266275
const prompt = {
267276
...first,
268277
item: {
269278
...first.item,
270279
type: "user_message" as const,
271-
createdBy: "agent" as const,
280+
createdBy,
272281
creationSource: "provider" as const,
273282
inputIntent: "turn_start" as const,
274283
messageId: MessageId.make("child-prompt"),
@@ -290,6 +299,44 @@ describe("threadHistoryPaging", () => {
290299
expect([...older.items, ...recent.items]).toHaveLength(items.length);
291300
});
292301

302+
it("enforces the byte budget inside a human turn without losing earlier items", () => {
303+
const first = makeRow(0);
304+
const prompt = {
305+
...first,
306+
item: {
307+
...first.item,
308+
type: "user_message",
309+
createdBy: "user",
310+
creationSource: "web",
311+
inputIntent: "turn_start",
312+
messageId: MessageId.make("large-turn-prompt"),
313+
text: "Inspect",
314+
attachments: [],
315+
},
316+
} as OrchestrationV2ProjectedTurnItem;
317+
const items = [prompt, makeRow(1, { outputBytes: 2_048 }), makeRow(2, { outputBytes: 2_048 })];
318+
const policy = {
319+
...THREAD_HISTORY_PAGE_POLICY,
320+
maxEncodedBytes: projectedRowEncodedBytes(items[2]!),
321+
};
322+
const recent = selectRecentTimelineWindow({ items, snapshotSequence: 1, policy });
323+
expect(recent.items.map((row) => row.sourceItemId)).toEqual([items[2]!.sourceItemId]);
324+
const loaded = [...recent.items];
325+
let cursor = recent.nextCursor;
326+
for (let pages = 0; cursor !== null; pages += 1) {
327+
expect(pages).toBeLessThan(items.length);
328+
const page = selectHistoryPageFromCursor({ items, cursor, snapshotSequence: 1, policy });
329+
expect(
330+
page.items.reduce((size, row) => size + projectedRowEncodedBytes(row), 0),
331+
).toBeLessThanOrEqual(policy.maxEncodedBytes);
332+
loaded.unshift(...page.items);
333+
cursor = page.nextCursor;
334+
}
335+
expect(loaded.map((row) => ({ ...row, position: 0 }))).toEqual(
336+
items.map((row) => ({ ...row, position: 0 })),
337+
);
338+
});
339+
293340
it("encodes opaque cursors with stable source identity", () => {
294341
const cursor = encodeThreadHistoryCursor({
295342
snapshotSequence: 9,

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import type {
77
} from "@t3tools/contracts";
88

99
/**
10-
* Match the V1 conversation windows. Item/byte budgets only apply to histories
11-
* without turn starts; tool activity must not split an ordinary conversation turn.
10+
* Bound every history page, including a long-running conversation turn.
11+
* Earlier activity remains accessible through the history cursor.
1212
*/
1313
export const THREAD_HISTORY_PAGE_POLICY = {
1414
maxUserTurns: 10,
@@ -198,8 +198,8 @@ export function isThreadHistoryUserTurn(item: HistoryRow["item"]): boolean {
198198

199199
/**
200200
* Walk a chronological timeline backward from the exclusive end, collecting rows
201-
* through complete user turns. Histories without turn starts use row budgets
202-
* and always admit at least one row so oversized items cannot deadlock paging.
201+
* toward earlier user turns, within the row and byte budgets. Always admit at
202+
* least one row so an oversized item cannot deadlock paging.
203203
*/
204204
function selectOlderTimelinePage<Row extends HistoryRow>(input: {
205205
readonly items: ReadonlyArray<Row>;
@@ -226,12 +226,13 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
226226
: undefined;
227227
for (let index = end - 1; index >= 0; index -= 1) {
228228
const row = input.items[index]!;
229-
const rowBytes = turnLimit === undefined ? rowCost(row) : 0;
229+
const rowBytes = rowCost(row);
230230
if (
231231
selected.length > 0 &&
232-
(turnLimit === undefined
233-
? selected.length >= policy.maxItems || encodedBytes + rowBytes > policy.maxEncodedBytes
234-
: userTurns >= turnLimit || rawTurns >= THREAD_HISTORY_MAX_RAW_TURNS)
232+
(selected.length >= policy.maxItems ||
233+
encodedBytes + rowBytes > policy.maxEncodedBytes ||
234+
(turnLimit !== undefined &&
235+
(userTurns >= turnLimit || rawTurns >= THREAD_HISTORY_MAX_RAW_TURNS)))
235236
) {
236237
break;
237238
}

‎apps/web/src/connection/storage.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
77
StoredOrchestrationShellSnapshot,
88
StoredOrchestrationThreadSnapshot,
9+
decodeStoredOrchestrationThreadSnapshot as decodeStoredThreadSnapshot,
910
decodeOrDiscardOrchestrationCache,
1011
putRemoteDpopTokenInCatalog,
1112
registerConnectionInCatalog,
@@ -69,7 +70,6 @@ const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCat
6970
const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
7071
const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(StoredShellSnapshotJson);
7172
const encodeStoredShellSnapshot = Schema.encodeEffect(StoredShellSnapshotJson);
72-
const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(StoredThreadSnapshotJson);
7373
const encodeStoredThreadSnapshot = Schema.encodeEffect(StoredThreadSnapshotJson);
7474
const decodeStoredServerConfig = Schema.decodeUnknownEffect(StoredServerConfigJson);
7575
const encodeStoredServerConfig = Schema.encodeEffect(StoredServerConfigJson);

0 commit comments

Comments
 (0)