@devries-cameron20 / t3code-1 / commits / fc6c68b773
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(server): a secret or link card reads one run, not the whole thread (#38890)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Navid Nadali committed Nov 19, 2026 at 04:56 UTC fc6c68b773db0a3cc7292a703d337a44919164b5
4 files changed +190 -10
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+160
@@ -2153,6 +2153,166 @@ it.effect("starts and finishes a delegated task without reading the parent's his
2153 }).pipe(Effect.provide(layerTest)),
2154 );
2155
2156 +it.effect("records a secret and a link card without reading the thread's history", () =>
2157 + Effect.gen(function* () {
2158 + const orchestrator = yield* Orchestrator.OrchestratorV2;
2159 + const projections = yield* ProjectionStore.ProjectionStoreV2;
2160 + const sql = yield* SqlClient.SqlClient;
2161 + const threadId = ThreadId.make("thread:long-cards");
2162 + const now = yield* DateTime.now;
2163 + yield* orchestrator.dispatch({
2164 + type: "thread.create",
2165 + commandId: CommandId.make("create-long-cards"),
2166 + threadId,
2167 + projectId: ProjectId.make("project:long-cards"),
2168 + title: "Cards",
2169 + modelSelection,
2170 + runtimeMode: "full-access",
2171 + interactionMode: "default",
2172 + branch: null,
2173 + worktreePath: null,
2174 + createdBy: "user",
2175 + creationSource: "web",
2176 + });
2177 + yield* orchestrator.dispatch({
2178 + type: "message.dispatch",
2179 + commandId: CommandId.make("start-long-cards"),
2180 + threadId,
2181 + messageId: MessageId.make("message:long-cards"),
2182 + text: "Ask for a token and a link",
2183 + attachments: [],
2184 + dispatchMode: { type: "start_immediately" },
2185 + createdBy: "user",
2186 + creationSource: "web",
2187 + });
2188 + const run = (yield* projections.getThreadRecords(threadId, ["runs"])).runs[0]!;
2189 + const providerTurnId = ProviderTurnId.make("provider-turn:long-cards");
2190 + yield* projections.apply({
2191 + id: EventId.make("event:long-cards:provider-turn"),
2192 + type: "provider-turn.updated",
2193 + threadId,
2194 + runId: run.id,
2195 + occurredAt: now,
2196 + payload: {
2197 + id: providerTurnId,
2198 + providerThreadId: run.providerThreadId!,
2199 + nodeId: run.rootNodeId!,
2200 + runAttemptId: run.activeAttemptId,
2201 + nativeTurnRef: null,
2202 + ordinal: 1,
2203 + status: "running",
2204 + startedAt: now,
2205 + completedAt: null,
2206 + },
2207 + });
2208 +
2209 + const cardSpans = [
2210 + "orchestrationV2.dispatch.secretRequestRecord",
2211 + "orchestrationV2.dispatch.linkRequestRecord",
2212 + ];
2213 + const statements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
2214 + const recordStatements = Effect.provideService(
2215 + Statement.CurrentTransformer,
2216 + (statement, _make, _fiber, span) => {
2217 + for (
2218 + let parent = Option.some<Tracer.AnySpan>(span);
2219 + Option.isSome(parent) && parent.value._tag === "Span";
2220 + parent = parent.value.parent
2221 + ) {
2222 + if (cardSpans.includes(parent.value.name)) {
2223 + statements.push(statement.compile());
2224 + break;
2225 + }
2226 + }
2227 + return Effect.succeed(statement);
2228 + },
2229 + );
2230 + const card = {
2231 + threadId,
2232 + runId: run.id,
2233 + nodeId: run.rootNodeId!,
2234 + reason: "Needed for the work",
2235 + };
2236 + yield* orchestrator
2237 + .dispatch({
2238 + ...card,
2239 + type: "secret_request.record",
2240 + commandId: CommandId.make("record-long-cards:secret"),
2241 + turnItemId: TurnItemId.make("item:long-cards:secret"),
2242 + label: "Token",
2243 + secretStatus: "pending",
2244 + })
2245 + .pipe(recordStatements);
2246 + yield* orchestrator
2247 + .dispatch({
2248 + ...card,
2249 + type: "link_request.record",
2250 + commandId: CommandId.make("record-long-cards:link"),
2251 + turnItemId: TurnItemId.make("item:long-cards:link"),
2252 + url: "https://example.test",
2253 + linkStatus: "pending",
2254 + })
2255 + .pipe(recordStatements);
2256 + const { turnItems } = yield* projections.getThreadRecords(threadId, ["turnItems"], {
2257 + turnItemTypes: ["secret_request", "link_request"],
2258 + });
2259 + assert.deepEqual(
2260 + turnItems.map((item) => [item.type, item.providerTurnId]),
2261 + [
2262 + ["secret_request", providerTurnId],
2263 + ["link_request", providerTurnId],
2264 + ],
2265 + );
2266 +
2267 + // A card reads its own run, so the rows and VM steps of its reads, which are deterministic,
2268 + // must stay flat on a copy where the thread has thousands of older runs.
2269 + const copyPath = "file:/card-reads?vfs=memdb";
2270 + using copy = new NodeSqlite.DatabaseSync(copyPath);
2271 + yield* sql`VACUUM INTO ${copyPath}`;
2272 + const replay = () =>
2273 + statements
2274 + .filter(([query]) => /^\s*(SELECT|WITH)\b/.test(query))
2275 + .reduce(
2276 + (total, [query, params]) => {
2277 + const statement = copy.prepare(query);
2278 + const rows = statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
2279 + return {
2280 + rows: total.rows + rows.length,
2281 + steps: total.steps + statement.stat("vmStep"),
2282 + };
2283 + },
2284 + { rows: 0, steps: 0 },
2285 + );
2286 + const short = replay();
2287 + const history: ReadonlyArray<readonly [string, string, ReadonlyArray<string>]> = [
2288 + ["runs", "run_id", []],
2289 + ["run_attempts", "attempt_id", ["run_id"]],
2290 + ["nodes", "node_id", ["run_id"]],
2291 + ["provider_turns", "provider_turn_id", ["run_attempt_id"]],
2292 + ];
2293 + for (const [table, key, renamed] of history) {
2294 + const name = `orchestration_v2_projection_${table}`;
2295 + const columns = copy
2296 + .prepare(`SELECT name FROM pragma_table_info('${name}')`)
2297 + .all()
2298 + .map((column) => String(column.name));
2299 + const values = columns.map((column) =>
2300 + column === key || renamed.includes(column)
2301 + ? `${column} || ':history:' || history.n`
2302 + : column === "ordinal"
2303 + ? "ordinal + history.n"
2304 + : column,
2305 + );
2306 + copy.exec(`WITH RECURSIVE history(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 2000)
2307 + INSERT INTO ${name} (${columns.join(", ")})
2308 + SELECT ${values.join(", ")} FROM ${name}, history WHERE thread_id = '${threadId}'`);
2309 + }
2310 + const long = replay();
2311 + assert.equal(long.rows, short.rows);
2312 + assert.isAtMost(long.steps, short.steps * 1.1);
2313 + }).pipe(Effect.provide(layerTest)),
2314 +);
2315 +
2316 it.effect("replays a thread.create receipt without changing the thread", () =>
2317 Effect.gen(function* () {
2318 const orchestrator = yield* Orchestrator.OrchestratorV2;
apps/server/src/orchestration-v2/Orchestrator.ts
+2 -10
@@ -10576,11 +10576,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
10576 events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
10577 ) {
10578 const projection = yield* projectionStore
10579 - .getThreadRecords(
10580 - command.threadId,
10581 - ["runs", "nodes", "turnItems", "attempts", "providerTurns"],
10582 - { turnItemTypes: ["secret_request"], messageRoles: [] },
10583 - )
10579 + .getRunCardRecords(command.threadId, command.runId, command.nodeId, "secret_request")
10580 .pipe(
10581 Effect.mapError(
10582 (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
@@ -10685,11 +10681,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
10681 effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
10682 ) {
10683 const projection = yield* projectionStore
10688 - .getThreadRecords(
10689 - command.threadId,
10690 - ["runs", "nodes", "turnItems", "attempts", "providerTurns"],
10691 - { turnItemTypes: ["link_request"], messageRoles: [] },
10692 - )
10684 + .getRunCardRecords(command.threadId, command.runId, command.nodeId, "link_request")
10685 .pipe(
10686 Effect.mapError(
10687 (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
apps/server/src/orchestration-v2/ProjectionStore.ts
+27
@@ -728,6 +728,18 @@ export interface ProjectionStoreV2Shape {
728 ProjectionRecords<"runs" | "nodes" | "providerThreads" | "providerTurns" | "attempts">,
729 ProjectionStoreV2Error
730 >;
731 + /** `getRunProviderRecords` and the thread's cards of one type, read together. */
732 + readonly getRunCardRecords: (
733 + threadId: ThreadId,
734 + runId: RunId,
735 + nodeId: NodeId,
736 + cardType: "secret_request" | "link_request",
737 + ) => Effect.Effect<
738 + ProjectionRecords<
739 + "runs" | "nodes" | "providerThreads" | "providerTurns" | "attempts" | "turnItems"
740 + >,
741 + ProjectionStoreV2Error
742 + >;
743 readonly isNodeStored: (
744 node: OrchestrationV2ExecutionNode,
745 ) => Effect.Effect<boolean, ProjectionStoreV2Error>;
@@ -6678,6 +6690,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6690 sql.withTransaction(effect).pipe(Effect.mapError(controlReadError(threadId))),
6691 );
6692
6693 + const getRunCardRecords: ProjectionStoreV2Shape["getRunCardRecords"] = Effect.fn(
6694 + "ProjectionStore.getRunCardRecords",
6695 + )(
6696 + function* (threadId, runId, nodeId, cardType) {
6697 + const records = yield* getRunProviderRecords(threadId, runId, nodeId);
6698 + const { turnItems } = yield* getThreadRecords(threadId, ["turnItems"], {
6699 + turnItemTypes: [cardType],
6700 + });
6701 + return { ...records, turnItems };
6702 + },
6703 + (effect, threadId) =>
6704 + sql.withTransaction(effect).pipe(Effect.mapError(controlReadError(threadId))),
6705 + );
6706 +
6707 const getThreadSnapshot: ProjectionStoreV2Shape["getThreadSnapshot"] = (threadId) =>
6708 sql
6709 .withTransaction(
@@ -9488,6 +9514,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
9514 hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest),
9515 hasRunInterruptRequest: readOnReader(hasRunInterruptRequest),
9516 getRunProviderRecords: readOnReader(getRunProviderRecords),
9517 + getRunCardRecords: readOnReader(getRunCardRecords),
9518 isNodeStored: readOnReader(isNodeStored),
9519 getMessageCount: readOnReader(getMessageCount),
9520 getTranscriptByteFloor: readOnReader(getTranscriptByteFloor),
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+1
@@ -161,6 +161,7 @@ function layerProjectionStore(
161 hasUnpairedRunInterruptRequest: () => Effect.die("unused interrupt read"),
162 hasRunInterruptRequest: () => Effect.die("unused stop read"),
163 getRunProviderRecords: () => Effect.die("unused run provider read"),
164 + getRunCardRecords: () => Effect.die("unused run card read"),
165 getThreadAttachmentIds: () => Effect.die("Unused attachment lookup"),
166 searchThread: () => Effect.die("unused"),
167 searchThreadStream: () => Stream.empty,