Skip to content
Merged
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
160 changes: 160 additions & 0 deletions apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<readonly [string, ReadonlyArray<unknown>]> = [];
const recordStatements = Effect.provideService(
Statement.CurrentTransformer,
(statement, _make, _fiber, span) => {
for (
let parent = Option.some<Tracer.AnySpan>(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<NodeSqlite.SQLInputValue>));
return {
rows: total.rows + rows.length,
steps: total.steps + statement.stat("vmStep"),
};
},
{ rows: 0, steps: 0 },
);
const short = replay();
const history: ReadonlyArray<readonly [string, string, ReadonlyArray<string>]> = [
["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;
Expand Down
12 changes: 2 additions & 10 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10550,11 +10550,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
) {
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 }),
Expand Down Expand Up @@ -10659,11 +10655,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
) {
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 }),
Expand Down
27 changes: 27 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean, ProjectionStoreV2Error>;
Expand Down Expand Up @@ -6678,6 +6690,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
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(
Expand Down Expand Up @@ -9488,6 +9514,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest),
hasRunInterruptRequest: readOnReader(hasRunInterruptRequest),
getRunProviderRecords: readOnReader(getRunProviderRecords),
getRunCardRecords: readOnReader(getRunCardRecords),
isNodeStored: readOnReader(isNodeStored),
getMessageCount: readOnReader(getMessageCount),
getTranscriptByteFloor: readOnReader(getTranscriptByteFloor),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading