Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions apps/server/integration/transferBudgetV2.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ const CLIENT_BOUNDED_OPT_INS = {
compactTurnItems: "1",
dedupeBodies: "1",
compactIds: "1",
compactWire: "1",
deferWork: "1",
} satisfies Required<typeof EnvironmentOrchestrationThreadBoundedSnapshotQuery.Type>;
const CLIENT_HISTORY_OPT_INS = { compactIds: "1", deferWork: "1" } satisfies Required<
const CLIENT_HISTORY_OPT_INS = {
dedupeBodies: "1",
compactIds: "1",
compactWire: "1",
deferWork: "1",
} satisfies Required<
Omit<
typeof EnvironmentOrchestrationThreadHistoryQuery.Type,
"cursor" | "throughEntryId" | "view" | "detach"
Expand All @@ -122,6 +128,7 @@ const CLIENT_THREAD_SUBSCRIPTION = {
acceptCompactIds: true,
acceptDedupedBodies: true,
acceptTextDeltas: true,
acceptCompactWire: true,
} satisfies Required<Omit<OrchestrationV2SubscribeThreadInput, "threadId" | "afterSequence">>;
type ShellResumeField = "afterSequence" | "httpSnapshotFallback" | "knownProjectIdentities";
const CLIENT_SHELL_SUBSCRIPTION = {
Expand Down Expand Up @@ -522,7 +529,7 @@ it.live("reports V2 thread HTTP and WebSocket transfer budgets", () =>
return yield* Effect.die("Expected the finished long turn to defer its work");
const longTurnWorkPage = yield* readHistoryPage(
workAnchor.item.deferredWork.cursor,
`view=work&throughEntryId=${encodeURIComponent(workAnchor.sourceItemId)}&compactIds=1`,
`view=work&throughEntryId=${encodeURIComponent(workAnchor.sourceItemId)}&dedupeBodies=1&compactIds=1&compactWire=1`,
);
// Mobile opens a thread over the socket alone.
const socketOpen = yield* open({ thread: LONG_TURN_THREAD_ID });
Expand Down
64 changes: 61 additions & 3 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import type {
import {
IDLE_COMPACTION_TEXT,
idleCompactionOf,
isProviderNativeSubagentThread,
isQueuedProviderThreadPlaceholder,
OrchestrationV2AppThreadJson as OrchestrationV2AppThreadJsonSchema,
OrchestrationV2CheckpointJson as OrchestrationV2CheckpointJsonSchema,
Expand Down Expand Up @@ -908,6 +909,7 @@ export interface ProjectionStoreV2Shape {
readonly rowLimit: number;
readonly userTurnLimit?: number | undefined;
readonly deferWork?: boolean | undefined;
readonly pruneOffWindowMessages?: boolean | undefined;
readonly anchorItemId?: TurnItemId | undefined;
readonly anchorThreadId?: ThreadId | undefined;
readonly requiredRunId?: RunId | undefined;
Expand Down Expand Up @@ -4339,8 +4341,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, ProjectionStoreService
FROM latest_owner AS owner
`;

const thread = yield* decodeThreadPayload(threadRow.payload_json);
const [
thread,
runRows,
attemptRows,
nodeRows,
Expand All @@ -4356,7 +4358,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, ProjectionStoreService
contextHandoffRows,
contextTransferRows,
] = yield* Effect.all([
decodeThreadPayload(threadRow.payload_json),
(fields !== undefined && !fields.includes("runs")) ||
(window === undefined &&
(matchesNothing(filter?.runIds) ||
Expand Down Expand Up @@ -4762,7 +4763,63 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, ProjectionStoreService
filter?.recentMessageLimit === undefined ? rows : rows.toReversed(),
),
)
: sql<PayloadRow>`
: window.pruneOffWindowMessages === true
? sql<PayloadRow>`
-- Compact snapshots drop off-window completed assistant prose from running turns.
SELECT payload_json FROM (
SELECT message.payload_json, message.created_at, message.message_id
FROM json_each(${cohortMessageIds}) AS wanted
CROSS JOIN orchestration_v2_projection_messages AS message
ON message.message_id = wanted.value
WHERE message.thread_id = ${threadId}
UNION
SELECT message.payload_json, message.created_at, message.message_id
FROM orchestration_v2_projection_runs AS run
CROSS JOIN orchestration_v2_projection_messages AS message
ON message.run_id = run.run_id
WHERE run.thread_id = ${threadId}
AND run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
AND message.thread_id = ${threadId}
AND (
message.role = 'user'
OR message.streaming = 1
OR message.message_id = json_extract(run.payload_json, '$.userMessageId')
OR json_extract(message.payload_json, '$.delegatedCompletion') IS NOT NULL
OR json_extract(message.payload_json, '$.workflowScript') = 1
OR message.message_id = (
SELECT latest_assistant.message_id
FROM orchestration_v2_projection_messages AS latest_assistant
WHERE latest_assistant.thread_id = ${threadId}
AND latest_assistant.run_id = run.run_id
AND latest_assistant.role = 'assistant'
ORDER BY latest_assistant.created_at DESC, latest_assistant.message_id DESC
LIMIT 1
)
)
UNION
SELECT message.payload_json, message.created_at, message.message_id
FROM orchestration_v2_projection_runs AS run
CROSS JOIN orchestration_v2_projection_messages AS message
ON message.message_id = json_extract(run.payload_json, '$.userMessageId')
WHERE run.thread_id = ${threadId}
AND run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
AND message.thread_id = ${threadId}
${
isProviderNativeSubagentThread(thread)
? sql`UNION
SELECT payload_json, created_at, message_id FROM (
SELECT message.payload_json, message.created_at, message.message_id
FROM orchestration_v2_projection_messages AS message
WHERE message.thread_id = ${threadId} AND message.role = 'user'
ORDER BY message.created_at ASC, message.message_id ASC
LIMIT 1
)`
: sql``
}
)
ORDER BY created_at ASC, message_id ASC
`
: sql<PayloadRow>`
-- Look the window's messages up by key and the live runs' by run. One filter
-- over the thread reads every message the thread ever had.
SELECT payload_json FROM (
Expand Down Expand Up @@ -8748,6 +8805,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, ProjectionStoreService
rowLimit: options.rowLimit,
userTurnLimit: options.userTurnLimit,
deferWork: options.deferWork,
pruneOffWindowMessages: options.pruneOffWindowMessages,
...(historyAnchor?.threadId === threadId
? { anchorItemId: historyAnchor.itemId }
: {}),
Expand Down
11 changes: 10 additions & 1 deletion apps/server/src/orchestration-v2/ThreadStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "./threadHistoryPaging.ts";
import {
capProjectionCheckpointFilesForWire,
compactThreadProjectionForWire,
projectThreadProjectionForWire,
} from "./WireProjection.ts";

Expand Down Expand Up @@ -61,11 +62,14 @@ export function boundedSnapshotResponseFields(input: {
readonly compactTurnItems: boolean;
readonly dedupeBodies: boolean;
readonly compactIds: boolean;
readonly compactWire?: boolean;
}) {
const { bounded } = input;
const compactWire = input.compactWire === true;
const capped = capProjectionCheckpointFilesForWire(bounded.projection);
const slimmed = compactWire ? compactThreadProjectionForWire(capped) : capped;
// Dedupe keeps shared items one object, which the compaction's identity check needs.
const projection = input.dedupeBodies ? dedupeSnapshotBodies(capped) : capped;
const projection = input.dedupeBodies ? dedupeSnapshotBodies(slimmed) : slimmed;
const compactProjection = input.compactTurnItems ? omitLocalVisibleTurnItems(projection) : null;
return {
projection: compactProjection ?? projection,
Expand All @@ -74,6 +78,7 @@ export function boundedSnapshotResponseFields(input: {
latestLocalTurnOrdinal: bounded.latestLocalTurnOrdinal,
...(compactProjection === null ? {} : { turnItemsOmitLocalVisible: true as const }),
...(input.compactIds ? { idTable: true as const } : {}),
...(compactWire ? { compactWire: true as const } : {}),
};
}

Expand All @@ -83,10 +88,13 @@ export function buildBoundedThreadStreamSnapshot(input: {
readonly compactTurnItems?: boolean;
readonly dedupeBodies?: boolean;
readonly compactIds?: boolean;
readonly compactWire?: boolean;
}): ThreadSnapshotStreamItem {
const compactWire = input.compactWire === true;
const bounded = buildBoundedThreadProjection({
snapshotSequence: input.snapshotSequence,
projection: projectThreadProjectionForWire(input.projection),
pruneOffWindowMessages: compactWire,
});
return {
kind: "snapshot",
Expand All @@ -96,6 +104,7 @@ export function buildBoundedThreadStreamSnapshot(input: {
compactTurnItems: input.compactTurnItems === true,
dedupeBodies: input.dedupeBodies === true,
compactIds: input.compactIds === true,
compactWire,
}),
};
}
Expand Down
Loading
Loading