Skip to content

Commit 19b891e

Browse files
committed
perf(server): select workspace leases with thread-only reads
1 parent 335659c commit 19b891e

5 files changed

Lines changed: 55 additions & 18 deletions

File tree

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,23 @@ it.effect.each(["initial", "waiting"])(
8080
],
8181
checkpointScopes: [{ id: scopeId, cwd: process.cwd() }],
8282
} as unknown as OrchestrationV2ThreadProjection;
83+
const getThreadRecords = vi.fn<ProjectionStore.ProjectionStoreV2Shape["getThreadRecords"]>(
84+
() => {
85+
const current = projection;
86+
readCount++;
87+
return readCount === 1
88+
? Deferred.succeed(readEntered, undefined).pipe(Effect.as(current))
89+
: Effect.succeed(current);
90+
},
91+
);
8392
const testLayer = checkpointRollbackServiceLayer.pipe(
8493
Layer.provide(
8594
Layer.mergeAll(
8695
Layer.mock(CheckpointService.CheckpointServiceV2)({ restore }),
8796
Layer.mock(EventSink.EventSinkV2)({}),
8897
IdAllocator.layer,
8998
Layer.mock(ProjectionStore.ProjectionStoreV2)({
90-
getThreadRecords: () => {
91-
const current = projection;
92-
readCount++;
93-
return readCount === 1
94-
? Deferred.succeed(readEntered, undefined).pipe(Effect.as(current))
95-
: Effect.succeed(current);
96-
},
99+
getThreadRecords,
97100
getShellSnapshot: () =>
98101
Effect.succeed({
99102
schemaVersion: 1,
@@ -138,6 +141,9 @@ it.effect.each(["initial", "waiting"])(
138141
}
139142
const error = yield* Fiber.join(rollback);
140143

144+
assert.equal(getThreadRecords.mock.calls.length, 2);
145+
assert.deepEqual(getThreadRecords.mock.calls[0]?.[1], []);
146+
assert.equal(getThreadRecords.mock.calls[1]?.[1].includes("checkpoints"), true);
141147
assert.equal(error.reason, "rollback-target-invalid");
142148
assert.equal(
143149
error.message,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export const layer: Layer.Layer<
376376
Effect.provideService(FileSystem.FileSystem, fileSystem),
377377
Effect.provideService(Path.Path, path),
378378
);
379-
const initial = yield* readProjection(input.threadId);
379+
const initial = yield* projections.getThreadRecords(input.threadId, []);
380380
let worktreePath = yield* resolvePath(initial.thread.worktreePath);
381381
while (true) {
382382
const effect = Effect.gen(function* () {

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ it("does not commit running state when inherited background routing cannot be re
135135
),
136136
}),
137137
Layer.mock(ProjectionStore.ProjectionStoreV2)({
138+
getThreadRecords: () => {
139+
projectionReadCount += 1;
140+
return Effect.succeed(projection);
141+
},
138142
getTurnStartContext: () => {
139143
projectionReadCount += 1;
140144
return Effect.succeed({
@@ -523,6 +527,9 @@ function makeLocalCommandHarness(input: {
523527
);
524528
const exists = vi.fn(() => Effect.succeed(true));
525529
const createWorktree = vi.fn(() => Effect.succeed({} as never));
530+
const getThreadRecords = vi.fn<ProjectionStore.ProjectionStoreV2Shape["getThreadRecords"]>(() =>
531+
Effect.succeed(projection),
532+
);
526533
const getTurnStartContext = vi.fn(() =>
527534
Effect.succeed({
528535
...projection,
@@ -554,6 +561,7 @@ function makeLocalCommandHarness(input: {
554561
Effect.succeed(Option.some({ workspaceRoot: "/tmp/native-account-project" } as never)),
555562
}),
556563
Layer.mock(ProjectionStore.ProjectionStoreV2)({
564+
getThreadRecords,
557565
getTurnStartContext,
558566
getRuntimeRecoveryProjection: () =>
559567
Effect.as(failReadIfRunning, {
@@ -582,6 +590,7 @@ function makeLocalCommandHarness(input: {
582590
),
583591
);
584592
return {
593+
getThreadRecords,
585594
getTurnStartContext,
586595
setProjection: (next: OrchestrationV2ThreadProjection) => {
587596
projection = next;
@@ -718,6 +727,19 @@ effectIt.effect.each(
718727
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
719728
);
720729

730+
effectIt.effect("reads only thread state before taking the startup lease", () =>
731+
Effect.gen(function* () {
732+
const harness = makeLocalCommandHarness({ text: "/logout", previousNativeSession: true });
733+
yield* harness.start;
734+
expect(harness.getThreadRecords).toHaveBeenCalledExactlyOnceWith(
735+
harness.projection().thread.id,
736+
[],
737+
);
738+
expect(harness.getTurnStartContext).toHaveBeenCalledOnce();
739+
expect(harness.projection().runs.at(-1)?.status).toBe("completed");
740+
}),
741+
);
742+
721743
effectIt.effect("reads current startup state after waiting for the workspace lease", () =>
722744
Effect.gen(function* () {
723745
const worktreePath = "/tmp/provider-start-fresh-state";
@@ -728,15 +750,15 @@ effectIt.effect("reads current startup state after waiting for the workspace lea
728750
worktreePath,
729751
failReadsAfterRunning: true,
730752
});
731-
const read = harness.getTurnStartContext.getMockImplementation()!;
732-
harness.getTurnStartContext.mockImplementationOnce(() =>
733-
Deferred.succeed(readEntered, undefined).pipe(Effect.andThen(read())),
753+
harness.getThreadRecords.mockImplementationOnce(() =>
754+
Deferred.succeed(readEntered, undefined).pipe(Effect.as(harness.projection())),
734755
);
735756
const holder = yield* withWorkspaceLease(worktreePath, Deferred.await(release)).pipe(
736757
Effect.forkChild({ startImmediately: true }),
737758
);
738759
const startup = yield* harness.start.pipe(Effect.forkChild({ startImmediately: true }));
739760
yield* Deferred.await(readEntered);
761+
expect(harness.getTurnStartContext).not.toHaveBeenCalled();
740762
const projection = harness.projection();
741763
harness.setProjection({
742764
...projection,
@@ -764,12 +786,11 @@ effectIt.effect.each(["worktree", "root"])(
764786
...(initial === "worktree" ? { worktreePath: oldPath } : {}),
765787
failReadsAfterRunning: true,
766788
});
767-
const read = harness.getTurnStartContext.getMockImplementation()!;
768-
harness.getTurnStartContext.mockImplementationOnce(() => {
769-
const projection = read();
789+
harness.getThreadRecords.mockImplementationOnce(() => {
790+
const projection = harness.projection();
770791
return Deferred.succeed(readEntered, undefined).pipe(
771792
Effect.andThen(Deferred.await(releaseOld)),
772-
Effect.andThen(projection),
793+
Effect.as(projection),
773794
);
774795
});
775796
let newLeaseReleased = false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ export const layer: Layer.Layer<
12701270
Effect.provideService(FileSystem.FileSystem, fileSystem),
12711271
Effect.provideService(Path.Path, path),
12721272
);
1273-
const initial = yield* projectionStore.getTurnStartContext(input.threadId, input.runId);
1273+
const initial = yield* projectionStore.getThreadRecords(input.threadId, []);
12741274
let worktreePath = yield* resolvePath(initial.thread.worktreePath);
12751275
while (true) {
12761276
const effect = Effect.gen(function* () {

‎apps/server/src/orchestration-v2/testkit/ProviderTurnStartMemory.fixture.mjs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const [Start, Projection, Run, Sessions, Policy, Id, Sink, Handoff, Git, Project
3030
]);
3131
let current;
3232
let fullReads = 0;
33+
let threadReads = 0;
3334
const liveRuns = [];
3435
const refs = [];
3536
const checkpoints = [];
@@ -58,6 +59,13 @@ const dependencies = Layer.mergeAll(
5859
throw new Error("full transcript read");
5960
}),
6061
hasUnpairedRunInterruptRequest: () => Effect.succeed(false),
62+
getThreadRecords: (threadId, fields) =>
63+
Effect.sync(() => {
64+
NodeAssert.equal(threadId, current.thread.id);
65+
NodeAssert.deepEqual(fields, []);
66+
threadReads++;
67+
return { thread: current.thread };
68+
}),
6169
getTurnStartContext: () =>
6270
Effect.sync(() => {
6371
fullReads++;
@@ -180,9 +188,11 @@ await Effect.runPromise(
180188
current = { ...current, runs: [{ ...liveRun, status: "completed" }] };
181189
NodeAssert.equal(yield* controls.shouldFinalizeRun(), false);
182190
NodeAssert.deepEqual(yield* controls.loadInheritedBackgroundTurnItems(), []);
183-
NodeAssert.equal(fullReads, (i + 1) * 2);
191+
NodeAssert.equal(fullReads, i + 1);
192+
NodeAssert.equal(threadReads, i + 1);
184193
NodeAssert.equal(yield* controls.hasUnpairedRunInterruptRequest(), false);
185-
NodeAssert.equal(fullReads, (i + 1) * 2);
194+
NodeAssert.equal(fullReads, i + 1);
195+
NodeAssert.equal(threadReads, i + 1);
186196
current = null;
187197
if (i === Math.floor(count / 2) - 1 || i === count - 1) {
188198
yield* Effect.promise(async () => {

0 commit comments

Comments
 (0)