Skip to content

Commit 1f9a6ed

Browse files
committed
refactor(history): use a fixed 150-item work threshold
1 parent 23c8c77 commit 1f9a6ed

14 files changed

Lines changed: 45 additions & 102 deletions

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ export interface OrchestratorV2Shape {
345345
throughEntryId?: string,
346346
conversationOnly?: boolean,
347347
workMode?: "deferred" | "work",
348-
inlineWorkItemLimit?: number,
349348
) => Effect.Effect<OrchestrationV2ThreadHistoryPage, OrchestratorV2Error>;
350349
readonly getTimelinePage: (
351350
threadId: ThreadId,
@@ -13283,16 +13282,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1328313282
);
1328413283
}),
1328513284
getThreadHistoryPage: Effect.fn("OrchestratorV2.getThreadHistoryPage")(
13286-
function (threadId, cursor, throughEntryId, conversationOnly, workMode, inlineWorkItemLimit) {
13285+
function (threadId, cursor, throughEntryId, conversationOnly, workMode) {
1328713286
return projectionStore
13288-
.getThreadHistoryPage(
13289-
threadId,
13290-
cursor,
13291-
throughEntryId,
13292-
conversationOnly,
13293-
workMode,
13294-
inlineWorkItemLimit,
13295-
)
13287+
.getThreadHistoryPage(threadId, cursor, throughEntryId, conversationOnly, workMode)
1329613288
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
1329713289
},
1329813290
),

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,6 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
20172017
rowLimit: sqlPageLimit,
20182018
userTurnLimit: 10,
20192019
deferWork: true,
2020-
inlineWorkItemLimit: 150,
20212020
});
20222021
assert.isUndefined(
20232022
window.projection.visibleTurnItems.find((row) => row.item.ordinal === 2)?.item
@@ -2038,7 +2037,6 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
20382037
undefined,
20392038
false,
20402039
"deferred",
2041-
150,
20422040
);
20432041
assert.isUndefined(page.items.find((row) => row.item.ordinal === 2)?.item.deferredWork);
20442042
}
@@ -2078,7 +2076,6 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
20782076
rowLimit: sqlPageLimit,
20792077
userTurnLimit: 10,
20802078
deferWork: true,
2081-
inlineWorkItemLimit: 150,
20822079
});
20832080
const oldWork = window.projection.visibleTurnItems.find((row) => row.item.deferredWork);
20842081
assert.strictEqual(oldWork?.item.deferredWork?.itemCount, 987);
@@ -2108,20 +2105,25 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
21082105
yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = NULL,
21092106
payload_json = json_set(payload_json, '$.runId', NULL)
21102107
WHERE thread_id = ${threadId} AND ordinal > 1 AND ordinal < 851`;
2111-
for (const inlineWorkItemLimit of [75, 150]) {
2112-
const window = yield* projectionStore.getThreadSnapshotWindow(threadId, {
2113-
rowLimit: sqlPageLimit,
2114-
userTurnLimit: 10,
2115-
deferWork: true,
2116-
inlineWorkItemLimit,
2117-
});
2118-
assert.strictEqual(
2119-
window.projection.visibleTurnItems.some(
2120-
(row) => row.item.deferredWork?.itemCount === 150,
2121-
),
2122-
inlineWorkItemLimit === 75,
2123-
);
2124-
}
2108+
const inlineWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
2109+
rowLimit: sqlPageLimit,
2110+
userTurnLimit: 10,
2111+
deferWork: true,
2112+
});
2113+
assert.isFalse(inlineWindow.projection.visibleTurnItems.some((row) => row.item.deferredWork));
2114+
yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = ${sharedRunId},
2115+
payload_json = json_set(payload_json, '$.runId', ${sharedRunId})
2116+
WHERE thread_id = ${threadId} AND ordinal = 850`;
2117+
const deferredWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
2118+
rowLimit: sqlPageLimit,
2119+
userTurnLimit: 10,
2120+
deferWork: true,
2121+
});
2122+
assert.isTrue(
2123+
deferredWindow.projection.visibleTurnItems.some(
2124+
(row) => row.item.deferredWork?.itemCount === 151,
2125+
),
2126+
);
21252127
yield* sql`UPDATE orchestration_v2_projection_turn_items
21262128
SET run_id = 'run:bounded-sql-history:1',
21272129
payload_json = json_set(payload_json, '$.runId', 'run:bounded-sql-history:1')

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import {
104104
selectHistoryPageFromCursor,
105105
selectRecentTimelineWindow,
106106
THREAD_HISTORY_PAGE_POLICY,
107+
THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT,
107108
THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
108109
} from "./threadHistoryPaging.ts";
109110

@@ -420,7 +421,6 @@ export interface ProjectionStoreV2Shape {
420421
throughEntryId?: string,
421422
conversationOnly?: boolean,
422423
workMode?: "deferred" | "work",
423-
inlineWorkItemLimit?: number,
424424
) => Effect.Effect<OrchestrationV2ThreadHistoryPage, ProjectionStoreV2Error>;
425425
readonly getTimelinePage: (
426426
threadId: ThreadId,
@@ -617,7 +617,6 @@ export interface ProjectionStoreV2Shape {
617617
readonly rowLimit: number;
618618
readonly userTurnLimit?: number | undefined;
619619
readonly deferWork?: boolean | undefined;
620-
readonly inlineWorkItemLimit?: number | undefined;
621620
readonly anchorItemId?: TurnItemId | undefined;
622621
readonly anchorThreadId?: ThreadId | undefined;
623622
readonly requiredRunId?: RunId | undefined;
@@ -2869,7 +2868,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28692868
SELECT run_id, segment, attempt_id, MIN(ordinal) AS first_ordinal,
28702869
MAX(ordinal) AS last_ordinal, COUNT(*) AS item_count
28712870
FROM work_segments WHERE can_defer = 1
2872-
GROUP BY run_id, segment, attempt_id HAVING COUNT(*) > ${window.inlineWorkItemLimit ?? THREAD_HISTORY_PAGE_POLICY.maxItems}
2871+
GROUP BY run_id, segment, attempt_id HAVING COUNT(*) > ${THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT}
28732872
), window_items AS NOT MATERIALIZED (
28742873
SELECT item_rowid, ordinal, turn_item_id, run_id, type
28752874
FROM eligible WHERE ${window.deferWork ? 1 : 0} = 0
@@ -3652,7 +3651,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
36523651
rowLimit: window.rowLimit,
36533652
userTurnLimit: window.userTurnLimit,
36543653
deferWork: window.deferWork,
3655-
inlineWorkItemLimit: window.inlineWorkItemLimit,
36563654
requiredRunId: forkedFrom.runId,
36573655
suppressLocal: anchor === undefined || anchorIsInDescendant,
36583656
...(anchor === undefined || anchorIsInDescendant
@@ -5454,7 +5452,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
54545452
throughEntryId,
54555453
conversationOnly,
54565454
workMode,
5457-
inlineWorkItemLimit,
54585455
) =>
54595456
sql
54605457
.withTransaction(
@@ -5473,10 +5470,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
54735470
position,
54745471
canonicalPosition: position,
54755472
}));
5476-
const historyItems =
5477-
workMode === "deferred"
5478-
? deferWorkHistoryRows(indexed, inlineWorkItemLimit)
5479-
: indexed;
5473+
const historyItems = workMode === "deferred" ? deferWorkHistoryRows(indexed) : indexed;
54805474
const selection = yield* Effect.try({
54815475
try: () => {
54825476
const policy = olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId });
@@ -5665,7 +5659,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56655659
rowLimit: options.rowLimit,
56665660
userTurnLimit: options.userTurnLimit,
56675661
deferWork: options.deferWork,
5668-
inlineWorkItemLimit: options.inlineWorkItemLimit,
56695662
...(historyAnchor?.threadId === threadId
56705663
? { anchorItemId: historyAnchor.itemId }
56715664
: {}),

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ const make = Effect.gen(function* () {
12781278
}),
12791279
dispatch,
12801280
getThreadHistoryPage: Effect.fn("ThreadManagementService.getThreadHistoryPage")(
1281-
function (threadId, cursor, throughEntryId, conversationOnly, workMode, inlineWorkItemLimit) {
1281+
function (threadId, cursor, throughEntryId, conversationOnly, workMode) {
12821282
return ensureProjectionTranscript(threadId).pipe(
12831283
Effect.andThen(
12841284
orchestrator.getThreadHistoryPage(
@@ -1287,7 +1287,6 @@ const make = Effect.gen(function* () {
12871287
throughEntryId,
12881288
conversationOnly,
12891289
workMode,
1290-
inlineWorkItemLimit,
12911290
),
12921291
),
12931292
);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,11 @@ export const layer = HttpApiBuilder.group(
134134
typeof threadManagement.getThreadSnapshotWindow
135135
>[1]["anchorThreadId"],
136136
deferWork = false,
137-
inlineWorkItemLimit?: number,
138137
) {
139138
return yield* threadManagement
140139
.getThreadSnapshotWindow(threadId, {
141140
rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
142141
deferWork,
143-
inlineWorkItemLimit,
144142
userTurnLimit:
145143
anchorItemId === undefined
146144
? THREAD_HISTORY_PAGE_POLICY.maxUserTurns
@@ -232,7 +230,6 @@ export const layer = HttpApiBuilder.group(
232230
undefined,
233231
undefined,
234232
args.query.deferWork === "1",
235-
args.query.inlineWorkItemLimit === "150" ? 150 : 75,
236233
).pipe(traceLocalHandlerWork);
237234
const bounded = buildBoundedThreadProjection({
238235
projection: snapshot.projection,
@@ -274,7 +271,6 @@ export const layer = HttpApiBuilder.group(
274271
: args.query.deferWork === "1"
275272
? "deferred"
276273
: undefined,
277-
args.query.inlineWorkItemLimit === "150" ? 150 : 75,
278274
)
279275
.pipe(
280276
Effect.map((page) =>

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,15 @@ function makeProjection(visibleTurnItems: OrchestrationV2ProjectedTurnItem[]) {
158158
}
159159

160160
describe("threadHistoryPaging", () => {
161-
it("defers completed work above the client limit and keeps conversation and persistent rows", () => {
162-
const work = Array.from({ length: 76 }, (_, index) => {
161+
it("defers completed work above 150 items and keeps conversation and persistent rows", () => {
162+
const work = Array.from({ length: 151 }, (_, index) => {
163163
const row = makeRow(index);
164164
return { ...row, item: { ...row.item, runId: RUN, runStatus: "completed" } };
165165
});
166-
expect(deferWorkHistoryRows(work.slice(0, 75))).toHaveLength(75);
167-
expect(deferWorkHistoryRows(work, 150)).toHaveLength(76);
168-
const longWork = Array.from({ length: 151 }, (_, index) => ({
169-
...makeRow(index),
170-
item: { ...makeRow(index).item, runId: RUN, runStatus: "completed" },
171-
}));
172-
expect(deferWorkHistoryRows(longWork.slice(0, 150), 150)).toHaveLength(150);
173-
const deferredLongWork = deferWorkHistoryRows(longWork, 150)[0]!.item.deferredWork!;
174-
expect(deferredLongWork.itemCount).toBe(151);
175-
expect(decodeThreadHistoryCursor(deferredLongWork.cursor).p).toBe(151);
166+
expect(deferWorkHistoryRows(work.slice(0, 150))).toHaveLength(150);
167+
const deferredWork = deferWorkHistoryRows(work)[0]!.item.deferredWork!;
168+
expect(deferredWork.itemCount).toBe(151);
169+
expect(decodeThreadHistoryCursor(deferredWork.cursor).p).toBe(151);
176170
const superseded = work.map((row) => ({
177171
...row,
178172
item: { ...row.item, runStatus: "running", attemptId: "old", attemptStatus: "superseded" },
@@ -184,7 +178,7 @@ describe("threadHistoryPaging", () => {
184178
item: { ...row.item, runStatus: "running", attemptId: "active", attemptStatus: "running" },
185179
}));
186180
const attempts = deferWorkHistoryRows([...superseded, ...active]);
187-
expect(attempts[0]?.item.deferredWork?.itemCount).toBe(76);
181+
expect(attempts[0]?.item.deferredWork?.itemCount).toBe(151);
188182
expect(attempts.slice(1)).toEqual(active);
189183
const compaction = {
190184
...work[0]!,
@@ -210,17 +204,17 @@ describe("threadHistoryPaging", () => {
210204
...work.slice(30),
211205
]);
212206
expect(deferred.map((row) => row.sourceItemId)).toEqual(["item-0", "compaction", "render"]);
213-
expect(deferred[0]?.item.deferredWork?.itemCount).toBe(76);
207+
expect(deferred[0]?.item.deferredWork?.itemCount).toBe(151);
214208
for (const runStatus of ["running", "failed", "interrupted", "cancelled"]) {
215209
expect(
216210
deferWorkHistoryRows(work.map((row) => ({ ...row, item: { ...row.item, runStatus } }))),
217-
).toHaveLength(76);
211+
).toHaveLength(151);
218212
}
219213
const interrupted = {
220214
...compaction,
221215
item: { ...compaction.item, type: "run_interrupt_result" as const },
222216
};
223-
expect(deferWorkHistoryRows([...work, interrupted])).toHaveLength(77);
217+
expect(deferWorkHistoryRows([...work, interrupted])).toHaveLength(152);
224218
const failed = {
225219
...compaction,
226220
item: {
@@ -231,7 +225,7 @@ describe("threadHistoryPaging", () => {
231225
parentItemId: null,
232226
},
233227
};
234-
expect(deferWorkHistoryRows([...work, failed])).toHaveLength(77);
228+
expect(deferWorkHistoryRows([...work, failed])).toHaveLength(152);
235229
});
236230

237231
it("keeps a human turn whole up to the turn row budget and pages a longer one from inside", () => {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export const THREAD_HISTORY_PAGE_POLICY = {
1919
maxEncodedBytes: 1_048_576,
2020
} as const;
2121

22+
export const THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT = 150;
23+
2224
export const OLDER_THREAD_USER_TURN_LIMIT = 20;
2325

2426
export function isConversationHistoryItem(item: Pick<OrchestrationV2TurnItem, "type">): boolean {
@@ -196,10 +198,7 @@ function isDeferrableWorkItem(item: HistoryRow["item"]): boolean {
196198
);
197199
}
198200

199-
export function deferWorkHistoryRows<Row extends HistoryRow>(
200-
items: ReadonlyArray<Row>,
201-
inlineWorkItemLimit: number = THREAD_HISTORY_PAGE_POLICY.maxItems,
202-
): Row[] {
201+
export function deferWorkHistoryRows<Row extends HistoryRow>(items: ReadonlyArray<Row>): Row[] {
203202
const blocked = new Set(
204203
items.flatMap(({ item }) =>
205204
item.runId != null &&
@@ -230,7 +229,7 @@ export function deferWorkHistoryRows<Row extends HistoryRow>(
230229
const omitted = new Set<Row>();
231230
const anchors = new Map<Row, Row>();
232231
for (const group of groups.values()) {
233-
if (group.length <= inlineWorkItemLimit) continue;
232+
if (group.length <= THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT) continue;
234233
const first = group[0]!;
235234
const last = group.at(-1)!;
236235
for (const row of group.slice(1)) omitted.add(row);

‎apps/server/src/ws.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,6 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
775775
readonly requestCompletionMarker?: boolean;
776776
readonly acceptBoundedSnapshot?: boolean;
777777
readonly acceptDeferredWork?: boolean;
778-
readonly inlineWorkItemLimit?: 75 | 150;
779778
readonly acceptCompactTurnItems?: boolean;
780779
readonly acceptDedupedBodies?: boolean;
781780
readonly acceptCompactIds?: boolean;
@@ -864,7 +863,6 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
864863
? threadManagement.getThreadSnapshotWindow(input.threadId, {
865864
rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
866865
deferWork: input.acceptDeferredWork === true,
867-
inlineWorkItemLimit: input.inlineWorkItemLimit,
868866
userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
869867
})
870868
: threadManagement.getThreadSnapshot(input.threadId)

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { PrimaryConnectionTarget, type PreparedConnection } from "../connection/
88
import * as RpcHttp from "../rpc/http.ts";
99
import * as BoundedThreadSnapshotHttp from "./boundedThreadSnapshotHttp.ts";
1010
import * as ThreadSnapshotLoader from "./threadSnapshotHttp.ts";
11-
import { ClientPresentation } from "../platform/capabilities.ts";
1211

1312
const TARGET = new PrimaryConnectionTarget({
1413
environmentId: "environment-bounded" as never,
@@ -131,10 +130,10 @@ const idTableBody = () => {
131130

132131
describe("boundedThreadSnapshotLoader", () => {
133132
it.effect.each([
134-
["an older server that ignores the opt-in", () => boundedBody(false), "web", "150"],
135-
["a server that sends compact turnItems", () => boundedBody(true), "mobile", "75"],
136-
["a server that also sends an ID table", idTableBody, "desktop", "150"],
137-
] as const)("requests compact turnItems and loads %s", ([, body, surface, workLimit]) => {
133+
["an older server that ignores the opt-in", () => boundedBody(false)],
134+
["a server that sends compact turnItems", () => boundedBody(true)],
135+
["a server that also sends an ID table", idTableBody],
136+
] as const)("requests compact turnItems and loads %s", ([, body]) => {
138137
const urls: string[] = [];
139138
const fetchFn = ((input: RequestInfo | URL) => {
140139
urls.push(String(input));
@@ -146,7 +145,6 @@ describe("boundedThreadSnapshotLoader", () => {
146145
const result = yield* loader.load(PREPARED, THREAD_ID);
147146
expect(new URL(urls[0]!).searchParams.get("compactTurnItems")).toBe("1");
148147
expect(new URL(urls[0]!).searchParams.get("compactIds")).toBe("1");
149-
expect(new URL(urls[0]!).searchParams.get("inlineWorkItemLimit")).toBe(workLimit);
150148
expect(result._tag).toBe("present");
151149
if (result._tag !== "present") return;
152150
expect(result.snapshot.projection.turnItems.map((item) => String(item.id))).toEqual([
@@ -164,7 +162,6 @@ describe("boundedThreadSnapshotLoader", () => {
164162
Effect.provide(
165163
Layer.provide(BoundedThreadSnapshotHttp.layer, RpcHttp.layerRemoteHttpClient(fetchFn)),
166164
),
167-
Effect.provideService(ClientPresentation, { metadata: { surface } }),
168165
);
169166
});
170167

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
withOrchestrationProtocolHeader,
1515
} from "./environmentHttpAuth.ts";
1616
import * as ThreadSnapshotLoader from "./threadSnapshotHttp.ts";
17-
import { clientInlineWorkItemLimit } from "./threadHistoryHttp.ts";
1817

1918
const DEFAULT_BOUNDED_THREAD_SNAPSHOT_TIMEOUT_MS = 6_000;
2019

@@ -29,15 +28,13 @@ export const fetchEnvironmentBoundedThreadSnapshot = Effect.fn(
2928
>;
3029
readonly timeoutMs?: number;
3130
}) {
32-
const inlineWorkItemLimit = yield* clientInlineWorkItemLimit;
3331
const endpoint = {
3432
params: { threadId: input.threadId },
3533
query: {
3634
compactTurnItems: "1",
3735
dedupeBodies: "1",
3836
compactIds: "1",
3937
deferWork: "1",
40-
inlineWorkItemLimit: inlineWorkItemLimit === 150 ? ("150" as const) : ("75" as const),
4138
},
4239
};
4340
return yield* executeAuthenticatedEnvironmentHttpRequest({

0 commit comments

Comments
 (0)