Skip to content
TypeScript

perf(server): preparing a run is 15x faster and uses 200x less memory (#27105)

Julius Marminge22c37a8da456
diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.ts
index 5c51b8c48b8..b68eef4b2a4 100644
--- a/apps/server/src/orchestration-v2/ThreadLaunchService.ts
+++ b/apps/server/src/orchestration-v2/ThreadLaunchService.ts
@@ -1063,12 +1063,14 @@ const make = Effect.gen(function* () {
"ThreadLaunchService.prepareRun",
)(function* (input) {
const scheduled = yield* Effect.gen(function* () {
- const projection = yield* threads.getThreadProjection(input.threadId);
- const run = projection.runs.find((candidate) => candidate.id === input.runId);
+ const { thread, runs } = yield* threads.getThreadRecords(input.threadId, ["runs"], {
+ runIds: [input.runId],
+ });
+ const run = runs.find((candidate) => candidate.id === input.runId);
const workspacePreparation = run?.workspacePreparation;
if (run?.status !== "preparing" || workspacePreparation === undefined) return;
if (!(yield* reservePreparation(input.commandId))) return;
- yield* scheduleRunPreparation(input, projection, run, workspacePreparation).pipe(
+ yield* scheduleRunPreparation(input, thread, run, workspacePreparation).pipe(
Effect.onError(() => releasePreparation(input.commandId)),
);
}).pipe(Effect.exit);
@@ -1090,30 +1092,33 @@ const make = Effect.gen(function* () {
return dispatched;
});
- const scheduleRunPreparation = (
+ const scheduleRunPreparation = Effect.fn("ThreadLaunchService.scheduleRunPreparation")(function* (
input: ThreadLaunchPreparationInput,
- projection: OrchestrationV2ThreadProjection,
+ thread: OrchestrationV2ThreadProjection["thread"],
run: OrchestrationV2ThreadProjection["runs"][number],
workspacePreparation: ThreadLaunchWorkspaceStrategy,
- ) => {
- const message = projection.messages.find((candidate) => candidate.id === run.userMessageId);
+ ) {
+ const { messages } = yield* threads.getThreadRecords(input.threadId, ["messages"], {
+ messageIds: [run.userMessageId],
+ });
+ const message = messages.find((candidate) => candidate.id === run.userMessageId);
const reuse =
workspacePreparation.type === "worktree" &&
- projection.thread.worktreePath !== null &&
- projection.thread.branch !== null
+ thread.worktreePath !== null &&
+ thread.branch !== null
? {
strategy: {
type: "existing_worktree" as const,
- worktreePath: projection.thread.worktreePath,
- branch: projection.thread.branch,
+ worktreePath: thread.worktreePath,
+ branch: thread.branch,
},
reusedWorktree: { baseRef: workspacePreparation.baseRef },
}
: null;
- return schedulePreparation(
+ return yield* schedulePreparation(
{
commandId: input.commandId,
- projectId: projection.thread.projectId,
+ projectId: thread.projectId,
workspaceStrategy: reuse?.strategy ?? workspacePreparation,
...(reuse === null ? {} : { reusedWorktree: reuse.reusedWorktree }),
...(message === undefined
@@ -1129,7 +1134,7 @@ const make = Effect.gen(function* () {
input.threadId,
run.id,
);
- };
+ });
return ThreadLaunchService.of({ launch, checkWorktreeBase, retryPreparation, prepareRun });
});