@devries-cameron20 / t3code Mirror · read-only / commits / 4bab2f3ae7
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

perf(server): a new thread's first turn starts sooner (#35710)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Nov 13, 2026 at 16:27 UTC 4bab2f3ae71c1d255c507978e793695a78569615
6 files changed +254 -103
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+37 -19
@@ -12130,8 +12130,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12130 const openQuery = Effect.fnUntraced(function* (
12131 turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
12132 nativeThreadId: string,
12133 + queryPolicy: Effect.Success<ReturnType<typeof queryPolicyForTurn>>,
12134 ) {
12134 - const queryPolicy = yield* queryPolicyForTurn(turnInput);
12135 const mcpSession = yield* mcpSessions.read(turnInput.threadId);
12136 const mcpOverrides = claudeMcpQueryOverrides({
12137 mcpSession,
@@ -12208,18 +12208,31 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12208 }
12209
12210 // Read before closing the live process so a bad path leaves it running.
12211 - const appendSystemPromptFileText = yield* readClaudeAppendSystemPromptFile({
12212 - settings: adapterOptions.settings,
12213 - cwd: turnInput.runtimePolicy.cwd,
12214 - fileSystem,
12215 - path,
12216 - });
12217 - const launchArgSettings = yield* readClaudeLaunchArgSettings({
12218 - settings: adapterOptions.settings,
12219 - cwd: turnInput.runtimePolicy.cwd,
12220 - fileSystem,
12221 - path,
12222 - });
12211 + const [appendSystemPromptFileText, { launchArgSettings, toolSearchOverride }] =
12212 + yield* Effect.all(
12213 + [
12214 + readClaudeAppendSystemPromptFile({
12215 + settings: adapterOptions.settings,
12216 + cwd: turnInput.runtimePolicy.cwd,
12217 + fileSystem,
12218 + path,
12219 + }),
12220 + Effect.gen(function* () {
12221 + const launchArgSettings = yield* readClaudeLaunchArgSettings({
12222 + settings: adapterOptions.settings,
12223 + cwd: turnInput.runtimePolicy.cwd,
12224 + fileSystem,
12225 + path,
12226 + });
12227 + const toolSearchOverride = yield* resolveToolSearchOverride(
12228 + turnInput.runtimePolicy.cwd,
12229 + launchArgSettings,
12230 + );
12231 + return { launchArgSettings, toolSearchOverride };
12232 + }),
12233 + ],
12234 + { concurrency: "unbounded" },
12235 + );
12236 const closedExistingNativeThreadId = existing !== null ? existing.nativeThreadId : null;
12237 clearedNativeSessions.delete(nativeThreadId);
12238 if (existing !== null) {
@@ -12238,10 +12251,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12251 turnInput.nativeThreadHasTurns ?? turnInput.providerTurnOrdinal > 1;
12252 const shouldResume =
12253 resumeSessionAt !== undefined || openedWithResume || hasPersistedProviderTurn;
12241 - const toolSearchOverride = yield* resolveToolSearchOverride(
12242 - turnInput.runtimePolicy.cwd,
12243 - launchArgSettings,
12244 - );
12254 const queryOptions = makeClaudeQueryOptions({
12255 modelSelection: turnInput.modelSelection,
12256 modelCatalog,
@@ -12520,6 +12529,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12529 const used = turnInput.providerThread.nativeMetadata?.claudeAttachmentBudget;
12530 pdfBudgets.set(nativeThreadId, makeClaudeAttachmentBudget(used));
12531 }
12532 + const [skills, queryPolicy] = yield* Effect.all(
12533 + [
12534 + promptUuid === null
12535 + ? Effect.undefined
12536 + : userInvocableSkills(turnInput.runtimePolicy.cwd),
12537 + queryPolicyForTurn(turnInput),
12538 + ],
12539 + { concurrency: "unbounded" },
12540 + );
12541 const pdfReservation = yield* reserveClaudePdfBudget(
12542 pdfBudgets.get(nativeThreadId)!,
12543 persistAttachmentBudget(nativeThreadId),
@@ -12546,13 +12564,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12564 attachmentsDir,
12565 fileSystem,
12566 pdfReservation,
12549 - skills: yield* userInvocableSkills(turnInput.runtimePolicy.cwd),
12567 + ...(skills === undefined ? {} : { skills }),
12568 uuid: promptUuid,
12569 }).pipe(
12570 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
12571 Effect.provideService(Path.Path, path),
12572 );
12555 - const querySession = yield* openQuery(turnInput, nativeThreadId);
12573 + const querySession = yield* openQuery(turnInput, nativeThreadId, queryPolicy);
12574 const autoCompactWindow = yield* resolveAutoCompactWindow(
12575 querySession.settingsCwd,
12576 turnInput.modelSelection,
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+4 -1
@@ -1126,7 +1126,10 @@ function makeLocalCommandHarness(input: {
1126 ),
1127 }),
1128 Layer.mock(ProviderAuthService.ProviderAuthService)({ tryHandlePromptCommand }),
1129 - Layer.mock(RunExecutionService.RunExecutionServiceV2)({ startRootRun }),
1129 + Layer.mock(RunExecutionService.RunExecutionServiceV2)({
1130 + startRootRun,
1131 + forkBaseline: () => Effect.succeed(Effect.void),
1132 + }),
1133 Layer.succeed(ProviderUsageAnalytics.ProviderTurnAnalytics, {
1134 runStarted: () => Effect.void,
1135 turnCompleted: () => Effect.void,
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+7
@@ -697,6 +697,12 @@ export const layer: Layer.Layer<
697 });
698 if (!repaired.committed) return;
699 }
700 + // Opening a session does not prompt the provider, so the workspace is still unchanged.
701 + const baseline = yield* runExecution.forkBaseline({
702 + appThread: projection.thread,
703 + run,
704 + checkpointScope,
705 + });
706 const sessionResult = yield* Effect.result(
707 providerSessions.open({
708 threadId: projection.thread.id,
@@ -1716,6 +1722,7 @@ export const layer: Layer.Layer<
1722 providerThread: runningProviderThread,
1723 attempt: runningAttempt,
1724 attemptId: attempt.id,
1725 + baseline,
1726 loadInheritedBackgroundTurnItems: runControls.loadInheritedBackgroundTurnItems,
1727 relatedThreadIds: routableSubagents.flatMap((subagent) =>
1728 subagent.childThreadId === null ? [] : [subagent.childThreadId],
apps/server/src/orchestration-v2/RunExecutionService.test.ts
+92
@@ -1247,6 +1247,98 @@ it.effect("starts the provider when checkpoint baseline capture fails", () =>
1247 }),
1248 );
1249
1250 +it.effect("prompts the provider only after a forked baseline capture finishes", () =>
1251 + Effect.gen(function* () {
1252 + const threadId = ThreadId.make("thread:run-execution-forked-baseline");
1253 + const runId = RunId.make("run:run-execution-forked-baseline");
1254 + const attemptId = RunAttemptId.make("attempt:run-execution-forked-baseline");
1255 + const providerInstanceId = ProviderInstanceId.make("codex");
1256 + const checkpointScope = {
1257 + id: CheckpointScopeId.make("checkpoint-scope:run-execution-forked-baseline"),
1258 + } as OrchestrationV2CheckpointScope;
1259 + const run = { id: runId, threadId, ordinal: 1, providerInstanceId } as OrchestrationV2Run;
1260 + const order = yield* Ref.make<ReadonlyArray<string>>([]);
1261 + const layerTest = RunExecutionService.layer.pipe(
1262 + Layer.provide(
1263 + Layer.mergeAll(
1264 + McpAppModelContext.layerEmpty,
1265 + PreviewAgentActivity.layer,
1266 + Layer.mock(CheckpointService.CheckpointServiceV2)({
1267 + captureBaseline: () =>
1268 + Effect.sleep("1 second").pipe(
1269 + Effect.andThen(Ref.update(order, (current) => [...current, "baseline"])),
1270 + ),
1271 + }),
1272 + layerTurnItemPositionsTest,
1273 + mockEventSink({}),
1274 + IdAllocator.layer,
1275 + ThreadCommandExecutor.layer,
1276 + Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
1277 + ingestNormalized: () => Effect.succeed([]),
1278 + }),
1279 + ServerSettings.layerTest(),
1280 + ),
1281 + ),
1282 + );
1283 +
1284 + yield* Effect.gen(function* () {
1285 + const runExecution = yield* RunExecutionService.RunExecutionServiceV2;
1286 + const baseline = yield* runExecution.forkBaseline({
1287 + appThread: rootAppThread(threadId),
1288 + run,
1289 + checkpointScope,
1290 + });
1291 + const started = yield* runExecution
1292 + .startRootRun({
1293 + commandId: CommandId.make("command:run-execution-forked-baseline"),
1294 + appThread: rootAppThread(threadId),
1295 + providerSessionId: ProviderSessionId.make("session:run-execution-forked-baseline"),
1296 + session: {
1297 + events: Stream.never,
1298 + startTurn: () => Ref.update(order, (current) => [...current, "prompt"]),
1299 + } as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime,
1300 + run,
1301 + rootNode: {
1302 + id: NodeId.make("node:run-execution-forked-baseline"),
1303 + } as OrchestrationV2ExecutionNode,
1304 + checkpointScope,
1305 + providerThread: {
1306 + id: ProviderThreadId.make("provider-thread:run-execution-forked-baseline"),
1307 + driver,
1308 + } as OrchestrationV2ProviderThread,
1309 + attempt: { id: attemptId, providerTurnId: null } as OrchestrationV2RunAttempt,
1310 + attemptId,
1311 + baseline,
1312 + providerTurnOrdinal: 1,
1313 + message: {
1314 + messageId: MessageId.make("message:run-execution-forked-baseline"),
1315 + text: "Wait for the baseline.",
1316 + attachments: [],
1317 + createdBy: "user",
1318 + creationSource: "web",
1319 + },
1320 + modelSelection: { instanceId: providerInstanceId, model: "gpt-5.4" },
1321 + runtimePolicy: {
1322 + runtimeMode: "full-access",
1323 + interactionMode: "default",
1324 + cwd: process.cwd(),
1325 + approvalPolicy: "never",
1326 + sandboxPolicy: {
1327 + type: "readOnly",
1328 + access: { type: "fullAccess" },
1329 + networkAccess: false,
1330 + },
1331 + },
1332 + })
1333 + .pipe(Effect.forkChild);
1334 + yield* TestClock.adjust("1 second");
1335 + yield* Fiber.join(started);
1336 + }).pipe(Effect.provide(layerTest));
1337 +
1338 + assert.deepEqual(yield* Ref.get(order), ["baseline", "prompt"]);
1339 + }),
1340 +);
1341 +
1342 it.effect.each(["failure", "interruption", "stale-attempt", "start-guard"] as const)(
1343 "handles %s before the provider turn starts",
1344 (scenario) =>
apps/server/src/orchestration-v2/RunExecutionService.ts
+40 -18
@@ -578,6 +578,8 @@ export interface RunExecutionServiceV2StartRootRunInput {
578 readonly providerThread: OrchestrationV2ProviderThread;
579 readonly attempt: OrchestrationV2RunAttempt;
580 readonly attemptId: RunAttemptId;
581 + /** Waits for a baseline started with `forkBaseline`; without it the run captures one itself. */
582 + readonly baseline?: Effect.Effect<void>;
583 readonly providerTurnOrdinal: number;
584 readonly nativeThreadHasTurns?: boolean;
585 readonly loadInheritedBackgroundTurnItems?: () => Effect.Effect<
@@ -596,10 +598,20 @@ export interface RunExecutionServiceV2StartRootRunInput {
598 readonly runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
599 }
600
601 +type RunBaselineInput = Pick<
602 + RunExecutionServiceV2StartRootRunInput,
603 + "appThread" | "run" | "checkpointScope"
604 +>;
605 +
606 export interface RunExecutionServiceV2Shape {
607 readonly startRootRun: (
608 input: RunExecutionServiceV2StartRootRunInput,
609 ) => Effect.Effect<void, RunExecutionServiceV2Error>;
610 + /**
611 + * Starts the run's baseline capture on a child fiber so it overlaps provider session startup.
612 + * Pass the returned effect to `startRootRun`, which awaits it before prompting the provider.
613 + */
614 + readonly forkBaseline: (input: RunBaselineInput) => Effect.Effect<Effect.Effect<void>>;
615 }
616
617 export class RunExecutionServiceV2 extends Context.Service<
@@ -918,7 +930,34 @@ export const layer: Layer.Layer<
930 },
931 );
932
933 + const captureRunBaseline = Effect.fn("RunExecutionService.captureBaseline")(function* (
934 + input: RunBaselineInput,
935 + ) {
936 + // Side chats share the parent's worktree; a snapshot would capture the parent's edits.
937 + if (input.appThread.lineage.sideChat === true) return;
938 + yield* checkpointService
939 + .captureBaseline({
940 + scope: input.checkpointScope,
941 + ordinalWithinScope: Math.max(0, input.run.ordinal - 1),
942 + })
943 + .pipe(
944 + Effect.catchCause((cause) =>
945 + Cause.hasInterruptsOnly(cause)
946 + ? Effect.interrupt
947 + : Effect.logWarning(
948 + "orchestration V2 checkpoint baseline capture failed; starting provider without a baseline",
949 + { runId: input.run.id },
950 + ),
951 + ),
952 + );
953 + });
954 +
955 return RunExecutionServiceV2.of({
956 + forkBaseline: (input) =>
957 + captureRunBaseline(input).pipe(
958 + Effect.forkChild({ startImmediately: true }),
959 + Effect.map(Fiber.join),
960 + ),
961 startRootRun: Effect.fn("RunExecutionService.startRootRun")(function* (input) {
962 const rootTurnOrigin: ProviderUsageAnalytics.ProviderTurnOrigin = {
963 createdBy: input.message.createdBy,
@@ -966,24 +1005,7 @@ export const layer: Layer.Layer<
1005 .responseStreamingMode,
1006 ),
1007 );
969 - // Side chats share the parent's worktree; a snapshot would capture the parent's edits.
970 - yield* (
971 - input.appThread.lineage.sideChat === true
972 - ? Effect.void
973 - : checkpointService.captureBaseline({
974 - scope: input.checkpointScope,
975 - ordinalWithinScope: Math.max(0, input.run.ordinal - 1),
976 - })
977 - ).pipe(
978 - Effect.catchCauseIf(
979 - (cause) => !Cause.hasInterruptsOnly(cause),
980 - (_cause) =>
981 - Effect.logWarning(
982 - "orchestration V2 checkpoint baseline capture failed; starting provider without a baseline",
983 - { runId: input.run.id },
984 - ),
985 - ),
986 - );
1008 + yield* input.baseline ?? captureRunBaseline(input);
1009 if (
1010 input.shouldStartProviderTurn !== undefined &&
1011 !(yield* input.shouldStartProviderTurn())
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+74 -65
@@ -346,37 +346,54 @@ const make = Effect.gen(function* () {
346 "ThreadLaunchService.checkWorktreeBase",
347 )(function* (input) {
348 const strategy = input.workspaceStrategy;
349 - // launch() turns a Scratch worktree request into a root launch.
350 - if (strategy.type !== "worktree" || (yield* managedFolders.isScratchProject(input.projectId)))
351 - return;
352 - if (input.commandId !== undefined) {
353 - const receipt = yield* receipts
354 - .getByCommandId(input.commandId)
355 - .pipe(Effect.orElseSucceed(() => Option.none()));
356 - if (Option.isSome(receipt) && receipt.value.status === "accepted") return;
357 - }
358 - const project = Option.getOrUndefined(
359 - yield* projects.getById(input.projectId).pipe(Effect.orElseSucceed(() => Option.none())),
349 + if (strategy.type !== "worktree") return;
350 + const commandId = input.commandId;
351 + const [isScratch, receipt, projectOption] = yield* Effect.all(
352 + [
353 + managedFolders.isScratchProject(input.projectId),
354 + commandId === undefined
355 + ? Effect.succeedNone
356 + : receipts.getByCommandId(commandId).pipe(Effect.orElseSucceed(() => Option.none())),
357 + projects.getById(input.projectId).pipe(Effect.orElseSucceed(() => Option.none())),
358 + ],
359 + { concurrency: "unbounded" },
360 );
361 + // launch() turns a Scratch worktree request into a root launch.
362 + if (isScratch) return;
363 + if (Option.isSome(receipt) && receipt.value.status === "accepted") return;
364 + const project = Option.getOrUndefined(projectOption);
365 if (project === undefined) return;
366 const cwd = project.workspaceRoot;
363 - const repositoryRoot = yield* git
364 - .repositoryRoot(cwd)
365 - .pipe(Effect.orElseSucceed(() => undefined));
367 + const { baseRef, branch } = strategy;
368 + const checksBranch = branch !== undefined && !isTemporaryWorktreeBranch(branch);
369 + // Errors keep their order below: missing repository, then base ref, then branch conflict.
370 + const [repositoryRoot, hasBaseCommit, localBranches] = yield* Effect.all(
371 + [
372 + git.repositoryRoot(cwd).pipe(Effect.orElseSucceed(() => undefined)),
373 + // `:/text` is a commit search, which cannot be asked for a commit type.
374 + baseRef.startsWith(":")
375 + ? Effect.succeed(true)
376 + : // `git worktree add` reads `-` as the previous checkout.
377 + git
378 + .hasCommit({ cwd, refName: baseRef === "-" ? "@{-1}" : baseRef })
379 + .pipe(Effect.orElseSucceed(() => true)),
380 + checksBranch
381 + ? git
382 + .listLocalBranchNames(cwd)
383 + .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []))
384 + : Effect.succeed<ReadonlyArray<string>>([]),
385 + ],
386 + { concurrency: "unbounded" },
387 + );
388 if (repositoryRoot === null)
389 return yield* new ThreadLaunchNotRepositoryError({
390 projectId: project.id,
391 projectTitle: project.title,
392 workspaceRoot: cwd,
393 });
372 - const { baseRef, branch } = strategy;
394 const baseResolves =
374 - // `:/text` is a commit search, which cannot be asked for a commit type.
375 - baseRef.startsWith(":") ||
395 + hasBaseCommit ||
396 (yield* Effect.gen(function* () {
377 - // `git worktree add` reads `-` as the previous checkout.
378 - if (yield* git.hasCommit({ cwd, refName: baseRef === "-" ? "@{-1}" : baseRef }))
379 - return true;
397 if (strategy.startFromOrigin === true) {
398 const remote = yield* git.resolveWorktreeBaseRemote({ cwd, refName: baseRef });
399 // The remote may have deleted the branch; ask it rather than trusting the name.
@@ -398,10 +415,8 @@ const make = Effect.gen(function* () {
415 baseRef,
416 });
417 // Provisioning flattens temporary names that collide, so only explicit names are refused.
401 - if (branch === undefined || isTemporaryWorktreeBranch(branch)) return;
402 - const conflictingBranch = (yield* git
403 - .listLocalBranchNames(cwd)
404 - .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []))).find(
418 + if (!checksBranch) return;
419 + const conflictingBranch = localBranches.find(
420 (existing) => existing.startsWith(`${branch}/`) || branch.startsWith(`${existing}/`),
421 );
422 if (conflictingBranch !== undefined)
@@ -996,7 +1011,7 @@ const make = Effect.gen(function* () {
1011 return yield* prepare;
1012 }
1013 const afterSequence = yield* threads.getThreadEventSequence(threadId);
999 - const projection = yield* threads.getThreadRecords(threadId, ["runs"]);
1014 + const projection = yield* threads.getThreadRecords(threadId, ["runs"], { runIds: [runId] });
1015 if (projection.runs.find((run) => run.id === runId)?.status !== "preparing") return;
1016 const interruption = threads
1017 .streamStoredEventsFrom({ threadId, afterSequence, eventType: "run.updated" })
@@ -1224,49 +1239,43 @@ const make = Effect.gen(function* () {
1239 }
1240 }
1241
1242 + // Fork preparation before the full projection read; it is only part of the reply.
1243 + const shouldSchedule = runId === null ? Option.isNone(launchReceipt) : true;
1244 + if (shouldSchedule && (yield* reservePreparation(input.commandId))) {
1245 + yield* Effect.gen(function* () {
1246 + const current =
1247 + runId === null
1248 + ? null
1249 + : yield* threads
1250 + .getThreadRecords(threadId, ["runs"], { runIds: [runId] })
1251 + .pipe(Effect.mapError(mapError(input, "update-thread", threadId)));
1252 + if (
1253 + current !== null &&
1254 + !current.runs.some((run) => run.id === runId && run.status === "preparing")
1255 + ) {
1256 + return yield* releasePreparation(input.commandId);
1257 + }
1258 + const boundWorktreePath = current?.thread.worktreePath ?? null;
1259 + const preparationStrategy: ThreadLaunchWorkspaceStrategy =
1260 + Option.isSome(launchReceipt) &&
1261 + workspaceStrategy.type === "root" &&
1262 + boundWorktreePath !== null
1263 + ? {
1264 + type: "existing_worktree",
1265 + worktreePath: boundWorktreePath,
1266 + branch: workspaceStrategy.branch,
1267 + }
1268 + : workspaceStrategy;
1269 + yield* schedulePreparation(
1270 + { ...input, workspaceStrategy: preparationStrategy },
1271 + threadId,
1272 + runId,
1273 + );
1274 + }).pipe(Effect.onError(() => releasePreparation(input.commandId)));
1275 + }
1276 const projection = yield* threads
1277 .getThreadProjection(threadId)
1278 .pipe(Effect.mapError(mapError(input, "create-thread", threadId)));
1230 - const runIsPreparing =
1231 - runId !== null &&
1232 - projection.runs.some((run) => run.id === runId && run.status === "preparing");
1233 - const shouldSchedule = runId === null ? Option.isNone(launchReceipt) : runIsPreparing;
1234 - const boundWorktreePath = projection.thread.worktreePath;
1235 - const preparationStrategy: ThreadLaunchWorkspaceStrategy =
1236 - Option.isSome(launchReceipt) &&
1237 - workspaceStrategy.type === "root" &&
1238 - boundWorktreePath !== null
1239 - ? {
1240 - type: "existing_worktree",
1241 - worktreePath: boundWorktreePath,
1242 - branch: workspaceStrategy.branch,
1243 - }
1244 - : workspaceStrategy;
1245 - if (shouldSchedule) {
1246 - const ownsPreparation = yield* reservePreparation(input.commandId);
1247 - if (ownsPreparation) {
1248 - yield* Effect.gen(function* () {
1249 - const preparationStillRequired =
1250 - runId === null
1251 - ? true
1252 - : yield* threads.getThreadRecords(threadId, ["runs"], { runIds: [runId] }).pipe(
1253 - Effect.map((current) =>
1254 - current.runs.some((run) => run.id === runId && run.status === "preparing"),
1255 - ),
1256 - Effect.mapError(mapError(input, "update-thread", threadId)),
1257 - );
1258 - if (preparationStillRequired) {
1259 - yield* schedulePreparation(
1260 - { ...input, workspaceStrategy: preparationStrategy },
1261 - threadId,
1262 - runId,
1263 - );
1264 - } else {
1265 - yield* releasePreparation(input.commandId);
1266 - }
1267 - }).pipe(Effect.onError(() => releasePreparation(input.commandId)));
1268 - }
1269 - }
1279
1280 return {
1281 threadId,