Skip to content

Commit 4bab2f3

Browse files
t3dotggclaude
andauthored
perf(server): a new thread's first turn starts sooner (#35710)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 44fefbc commit 4bab2f3

6 files changed

Lines changed: 254 additions & 103 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12130,8 +12130,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1213012130
const openQuery = Effect.fnUntraced(function* (
1213112131
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
1213212132
nativeThreadId: string,
12133+
queryPolicy: Effect.Success<ReturnType<typeof queryPolicyForTurn>>,
1213312134
) {
12134-
const queryPolicy = yield* queryPolicyForTurn(turnInput);
1213512135
const mcpSession = yield* mcpSessions.read(turnInput.threadId);
1213612136
const mcpOverrides = claudeMcpQueryOverrides({
1213712137
mcpSession,
@@ -12208,18 +12208,31 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1220812208
}
1220912209

1221012210
// 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+
);
1222312236
const closedExistingNativeThreadId = existing !== null ? existing.nativeThreadId : null;
1222412237
clearedNativeSessions.delete(nativeThreadId);
1222512238
if (existing !== null) {
@@ -12238,10 +12251,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1223812251
turnInput.nativeThreadHasTurns ?? turnInput.providerTurnOrdinal > 1;
1223912252
const shouldResume =
1224012253
resumeSessionAt !== undefined || openedWithResume || hasPersistedProviderTurn;
12241-
const toolSearchOverride = yield* resolveToolSearchOverride(
12242-
turnInput.runtimePolicy.cwd,
12243-
launchArgSettings,
12244-
);
1224512254
const queryOptions = makeClaudeQueryOptions({
1224612255
modelSelection: turnInput.modelSelection,
1224712256
modelCatalog,
@@ -12520,6 +12529,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1252012529
const used = turnInput.providerThread.nativeMetadata?.claudeAttachmentBudget;
1252112530
pdfBudgets.set(nativeThreadId, makeClaudeAttachmentBudget(used));
1252212531
}
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+
);
1252312541
const pdfReservation = yield* reserveClaudePdfBudget(
1252412542
pdfBudgets.get(nativeThreadId)!,
1252512543
persistAttachmentBudget(nativeThreadId),
@@ -12546,13 +12564,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1254612564
attachmentsDir,
1254712565
fileSystem,
1254812566
pdfReservation,
12549-
skills: yield* userInvocableSkills(turnInput.runtimePolicy.cwd),
12567+
...(skills === undefined ? {} : { skills }),
1255012568
uuid: promptUuid,
1255112569
}).pipe(
1255212570
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1255312571
Effect.provideService(Path.Path, path),
1255412572
);
12555-
const querySession = yield* openQuery(turnInput, nativeThreadId);
12573+
const querySession = yield* openQuery(turnInput, nativeThreadId, queryPolicy);
1255612574
const autoCompactWindow = yield* resolveAutoCompactWindow(
1255712575
querySession.settingsCwd,
1255812576
turnInput.modelSelection,

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,10 @@ function makeLocalCommandHarness(input: {
11261126
),
11271127
}),
11281128
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+
}),
11301133
Layer.succeed(ProviderUsageAnalytics.ProviderTurnAnalytics, {
11311134
runStarted: () => Effect.void,
11321135
turnCompleted: () => Effect.void,

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,12 @@ export const layer: Layer.Layer<
697697
});
698698
if (!repaired.committed) return;
699699
}
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+
});
700706
const sessionResult = yield* Effect.result(
701707
providerSessions.open({
702708
threadId: projection.thread.id,
@@ -1716,6 +1722,7 @@ export const layer: Layer.Layer<
17161722
providerThread: runningProviderThread,
17171723
attempt: runningAttempt,
17181724
attemptId: attempt.id,
1725+
baseline,
17191726
loadInheritedBackgroundTurnItems: runControls.loadInheritedBackgroundTurnItems,
17201727
relatedThreadIds: routableSubagents.flatMap((subagent) =>
17211728
subagent.childThreadId === null ? [] : [subagent.childThreadId],

‎apps/server/src/orchestration-v2/RunExecutionService.test.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,98 @@ it.effect("starts the provider when checkpoint baseline capture fails", () =>
12471247
}),
12481248
);
12491249

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+
12501342
it.effect.each(["failure", "interruption", "stale-attempt", "start-guard"] as const)(
12511343
"handles %s before the provider turn starts",
12521344
(scenario) =>

‎apps/server/src/orchestration-v2/RunExecutionService.ts‎

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@ export interface RunExecutionServiceV2StartRootRunInput {
578578
readonly providerThread: OrchestrationV2ProviderThread;
579579
readonly attempt: OrchestrationV2RunAttempt;
580580
readonly attemptId: RunAttemptId;
581+
/** Waits for a baseline started with `forkBaseline`; without it the run captures one itself. */
582+
readonly baseline?: Effect.Effect<void>;
581583
readonly providerTurnOrdinal: number;
582584
readonly nativeThreadHasTurns?: boolean;
583585
readonly loadInheritedBackgroundTurnItems?: () => Effect.Effect<
@@ -596,10 +598,20 @@ export interface RunExecutionServiceV2StartRootRunInput {
596598
readonly runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
597599
}
598600

601+
type RunBaselineInput = Pick<
602+
RunExecutionServiceV2StartRootRunInput,
603+
"appThread" | "run" | "checkpointScope"
604+
>;
605+
599606
export interface RunExecutionServiceV2Shape {
600607
readonly startRootRun: (
601608
input: RunExecutionServiceV2StartRootRunInput,
602609
) => 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>>;
603615
}
604616

605617
export class RunExecutionServiceV2 extends Context.Service<
@@ -918,7 +930,34 @@ export const layer: Layer.Layer<
918930
},
919931
);
920932

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+
921955
return RunExecutionServiceV2.of({
956+
forkBaseline: (input) =>
957+
captureRunBaseline(input).pipe(
958+
Effect.forkChild({ startImmediately: true }),
959+
Effect.map(Fiber.join),
960+
),
922961
startRootRun: Effect.fn("RunExecutionService.startRootRun")(function* (input) {
923962
const rootTurnOrigin: ProviderUsageAnalytics.ProviderTurnOrigin = {
924963
createdBy: input.message.createdBy,
@@ -966,24 +1005,7 @@ export const layer: Layer.Layer<
9661005
.responseStreamingMode,
9671006
),
9681007
);
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);
9871009
if (
9881010
input.shouldStartProviderTurn !== undefined &&
9891011
!(yield* input.shouldStartProviderTurn())

0 commit comments

Comments
 (0)