diff --git a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts index 274c15a5f7089..5c2abb77cf752 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts @@ -2153,6 +2153,166 @@ it.effect("starts and finishes a delegated task without reading the parent's his }).pipe(Effect.provide(layerTest)), ); +it.effect("records a secret and a link card without reading the thread's history", () => + Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const projections = yield* ProjectionStore.ProjectionStoreV2; + const sql = yield* SqlClient.SqlClient; + const threadId = ThreadId.make("thread:long-cards"); + const now = yield* DateTime.now; + yield* orchestrator.dispatch({ + type: "thread.create", + commandId: CommandId.make("create-long-cards"), + threadId, + projectId: ProjectId.make("project:long-cards"), + title: "Cards", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + createdBy: "user", + creationSource: "web", + }); + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("start-long-cards"), + threadId, + messageId: MessageId.make("message:long-cards"), + text: "Ask for a token and a link", + attachments: [], + dispatchMode: { type: "start_immediately" }, + createdBy: "user", + creationSource: "web", + }); + const run = (yield* projections.getThreadRecords(threadId, ["runs"])).runs[0]!; + const providerTurnId = ProviderTurnId.make("provider-turn:long-cards"); + yield* projections.apply({ + id: EventId.make("event:long-cards:provider-turn"), + type: "provider-turn.updated", + threadId, + runId: run.id, + occurredAt: now, + payload: { + id: providerTurnId, + providerThreadId: run.providerThreadId!, + nodeId: run.rootNodeId!, + runAttemptId: run.activeAttemptId, + nativeTurnRef: null, + ordinal: 1, + status: "running", + startedAt: now, + completedAt: null, + }, + }); + + const cardSpans = [ + "orchestrationV2.dispatch.secretRequestRecord", + "orchestrationV2.dispatch.linkRequestRecord", + ]; + const statements: Array]> = []; + const recordStatements = Effect.provideService( + Statement.CurrentTransformer, + (statement, _make, _fiber, span) => { + for ( + let parent = Option.some(span); + Option.isSome(parent) && parent.value._tag === "Span"; + parent = parent.value.parent + ) { + if (cardSpans.includes(parent.value.name)) { + statements.push(statement.compile()); + break; + } + } + return Effect.succeed(statement); + }, + ); + const card = { + threadId, + runId: run.id, + nodeId: run.rootNodeId!, + reason: "Needed for the work", + }; + yield* orchestrator + .dispatch({ + ...card, + type: "secret_request.record", + commandId: CommandId.make("record-long-cards:secret"), + turnItemId: TurnItemId.make("item:long-cards:secret"), + label: "Token", + secretStatus: "pending", + }) + .pipe(recordStatements); + yield* orchestrator + .dispatch({ + ...card, + type: "link_request.record", + commandId: CommandId.make("record-long-cards:link"), + turnItemId: TurnItemId.make("item:long-cards:link"), + url: "https://example.test", + linkStatus: "pending", + }) + .pipe(recordStatements); + const { turnItems } = yield* projections.getThreadRecords(threadId, ["turnItems"], { + turnItemTypes: ["secret_request", "link_request"], + }); + assert.deepEqual( + turnItems.map((item) => [item.type, item.providerTurnId]), + [ + ["secret_request", providerTurnId], + ["link_request", providerTurnId], + ], + ); + + // A card reads its own run, so the rows and VM steps of its reads, which are deterministic, + // must stay flat on a copy where the thread has thousands of older runs. + const copyPath = "file:/card-reads?vfs=memdb"; + using copy = new NodeSqlite.DatabaseSync(copyPath); + yield* sql`VACUUM INTO ${copyPath}`; + const replay = () => + statements + .filter(([query]) => /^\s*(SELECT|WITH)\b/.test(query)) + .reduce( + (total, [query, params]) => { + const statement = copy.prepare(query); + const rows = statement.all(...(params as ReadonlyArray)); + return { + rows: total.rows + rows.length, + steps: total.steps + statement.stat("vmStep"), + }; + }, + { rows: 0, steps: 0 }, + ); + const short = replay(); + const history: ReadonlyArray]> = [ + ["runs", "run_id", []], + ["run_attempts", "attempt_id", ["run_id"]], + ["nodes", "node_id", ["run_id"]], + ["provider_turns", "provider_turn_id", ["run_attempt_id"]], + ]; + for (const [table, key, renamed] of history) { + const name = `orchestration_v2_projection_${table}`; + const columns = copy + .prepare(`SELECT name FROM pragma_table_info('${name}')`) + .all() + .map((column) => String(column.name)); + const values = columns.map((column) => + column === key || renamed.includes(column) + ? `${column} || ':history:' || history.n` + : column === "ordinal" + ? "ordinal + history.n" + : column, + ); + copy.exec(`WITH RECURSIVE history(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 2000) + INSERT INTO ${name} (${columns.join(", ")}) + SELECT ${values.join(", ")} FROM ${name}, history WHERE thread_id = '${threadId}'`); + } + const long = replay(); + assert.equal(long.rows, short.rows); + assert.isAtMost(long.steps, short.steps * 1.1); + }).pipe(Effect.provide(layerTest)), +); + it.effect("replays a thread.create receipt without changing the thread", () => Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index a95481da7dfd6..d4f8551796af0 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -10550,11 +10550,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio events: Ref.Ref>, ) { const projection = yield* projectionStore - .getThreadRecords( - command.threadId, - ["runs", "nodes", "turnItems", "attempts", "providerTurns"], - { turnItemTypes: ["secret_request"], messageRoles: [] }, - ) + .getRunCardRecords(command.threadId, command.runId, command.nodeId, "secret_request") .pipe( Effect.mapError( (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }), @@ -10659,11 +10655,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio effects: Ref.Ref>, ) { const projection = yield* projectionStore - .getThreadRecords( - command.threadId, - ["runs", "nodes", "turnItems", "attempts", "providerTurns"], - { turnItemTypes: ["link_request"], messageRoles: [] }, - ) + .getRunCardRecords(command.threadId, command.runId, command.nodeId, "link_request") .pipe( Effect.mapError( (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }), diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index bee84d714103a..1c4d0948934ec 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -728,6 +728,18 @@ export interface ProjectionStoreV2Shape { ProjectionRecords<"runs" | "nodes" | "providerThreads" | "providerTurns" | "attempts">, ProjectionStoreV2Error >; + /** `getRunProviderRecords` and the thread's cards of one type, read together. */ + readonly getRunCardRecords: ( + threadId: ThreadId, + runId: RunId, + nodeId: NodeId, + cardType: "secret_request" | "link_request", + ) => Effect.Effect< + ProjectionRecords< + "runs" | "nodes" | "providerThreads" | "providerTurns" | "attempts" | "turnItems" + >, + ProjectionStoreV2Error + >; readonly isNodeStored: ( node: OrchestrationV2ExecutionNode, ) => Effect.Effect; @@ -6678,6 +6690,20 @@ export const layer: Layer.Layer = sql.withTransaction(effect).pipe(Effect.mapError(controlReadError(threadId))), ); + const getRunCardRecords: ProjectionStoreV2Shape["getRunCardRecords"] = Effect.fn( + "ProjectionStore.getRunCardRecords", + )( + function* (threadId, runId, nodeId, cardType) { + const records = yield* getRunProviderRecords(threadId, runId, nodeId); + const { turnItems } = yield* getThreadRecords(threadId, ["turnItems"], { + turnItemTypes: [cardType], + }); + return { ...records, turnItems }; + }, + (effect, threadId) => + sql.withTransaction(effect).pipe(Effect.mapError(controlReadError(threadId))), + ); + const getThreadSnapshot: ProjectionStoreV2Shape["getThreadSnapshot"] = (threadId) => sql .withTransaction( @@ -9488,6 +9514,7 @@ export const layer: Layer.Layer = hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest), hasRunInterruptRequest: readOnReader(hasRunInterruptRequest), getRunProviderRecords: readOnReader(getRunProviderRecords), + getRunCardRecords: readOnReader(getRunCardRecords), isNodeStored: readOnReader(isNodeStored), getMessageCount: readOnReader(getMessageCount), getTranscriptByteFloor: readOnReader(getTranscriptByteFloor), diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts index 3b5beb4850e3c..cb9550efe5799 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts @@ -161,6 +161,7 @@ function layerProjectionStore( hasUnpairedRunInterruptRequest: () => Effect.die("unused interrupt read"), hasRunInterruptRequest: () => Effect.die("unused stop read"), getRunProviderRecords: () => Effect.die("unused run provider read"), + getRunCardRecords: () => Effect.die("unused run card read"), getThreadAttachmentIds: () => Effect.die("Unused attachment lookup"), searchThread: () => Effect.die("unused"), searchThreadStream: () => Stream.empty,