Skip to content

Commit d554ec9

Browse files
fix(server): bound only the baseline snapshot, not the wait for the workspace lock
The 5 second budget wrapped the whole baseline capture, including the wait for the per-workspace checkpoint lock. After Stop, the next run's baseline could stop waiting on the stopped run's post-turn capture, which writes the same ref, and start the provider while that `git add -A` was still reading the tree, so the agent's first edits could land in the baseline. The budget now sits in CheckpointService on the snapshot alone, after the lock is held and the existing ref has been checked. The regression test moves to CheckpointService, where the budget lives. AI-assisted; verified with the tests and tools listed above.
1 parent 8aa3d49 commit d554ec9

4 files changed

Lines changed: 93 additions & 28 deletions

File tree

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import {
1212
VcsUnsupportedOperationError,
1313
} from "@t3tools/contracts";
1414
import * as DateTime from "effect/DateTime";
15+
import * as Deferred from "effect/Deferred";
1516
import * as Effect from "effect/Effect";
17+
import * as Fiber from "effect/Fiber";
1618
import * as Layer from "effect/Layer";
19+
import * as Ref from "effect/Ref";
20+
import * as TestClock from "effect/testing/TestClock";
1721

1822
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
1923
import * as CheckpointService from "./CheckpointService.ts";
@@ -98,6 +102,79 @@ it.effect(
98102
}),
99103
);
100104

105+
it.effect(
106+
"bounds a slow baseline snapshot but waits for an in-flight capture of the same ref",
107+
() =>
108+
Effect.gen(function* () {
109+
const scope: OrchestrationV2CheckpointScope = {
110+
id: CheckpointScopeId.make("checkpoint-scope:slow-baseline"),
111+
threadId: ThreadId.make("thread:slow-baseline"),
112+
runId: RunId.make("run:slow-baseline:1"),
113+
nodeId: NodeId.make("node:slow-baseline:1"),
114+
parentScopeId: null,
115+
providerThreadId: ProviderThreadId.make("provider-thread:slow-baseline"),
116+
kind: "root_run",
117+
ordinalWithinParent: 0,
118+
advancesAppRunCount: true,
119+
cwd: "/repo/slow",
120+
createdAt: DateTime.makeUnsafe("2026-07-28T00:00:00.000Z"),
121+
};
122+
const refs = yield* Ref.make<ReadonlySet<string>>(new Set());
123+
const snapshots = yield* Ref.make(0);
124+
const firstSnapshotStarted = yield* Deferred.make<void>();
125+
const layerTest = CheckpointService.layer.pipe(
126+
Layer.provide(
127+
Layer.mergeAll(
128+
IdAllocator.layer,
129+
Layer.mock(CheckpointStore.CheckpointStore)({
130+
isGitRepository: () => Effect.succeed(true),
131+
hasCheckpointRef: ({ checkpointRef }) =>
132+
Ref.get(refs).pipe(Effect.map((current) => current.has(checkpointRef))),
133+
captureCheckpoint: ({ checkpointRef }) =>
134+
Effect.gen(function* () {
135+
yield* Ref.update(snapshots, (count) => count + 1);
136+
yield* Deferred.succeed(firstSnapshotStarted, undefined);
137+
yield* Effect.sleep("10 seconds");
138+
yield* Ref.update(refs, (current) => new Set([...current, checkpointRef]));
139+
}),
140+
}),
141+
),
142+
),
143+
Layer.provideMerge(NodeCrypto.layer),
144+
);
145+
146+
yield* Effect.gen(function* () {
147+
const checkpoints = yield* CheckpointService.CheckpointServiceV2;
148+
// A stopped run's post-turn capture writes the next run's baseline ref while holding the lock.
149+
const postTurn = yield* checkpoints
150+
.capture({
151+
scope,
152+
ordinalWithinScope: 1,
153+
runId: scope.runId!,
154+
nodeId: scope.nodeId!,
155+
appRunOrdinal: 1,
156+
capturedAt: scope.createdAt,
157+
})
158+
.pipe(Effect.forkChild);
159+
yield* Deferred.await(firstSnapshotStarted);
160+
const waitingBaseline = yield* checkpoints
161+
.captureBaseline({ scope, ordinalWithinScope: 1 })
162+
.pipe(Effect.result, Effect.forkChild);
163+
yield* TestClock.adjust("10 seconds");
164+
assert.strictEqual((yield* Fiber.join(postTurn)).status, "ready");
165+
assert.strictEqual((yield* Fiber.join(waitingBaseline))._tag, "Success");
166+
167+
const slowBaseline = yield* checkpoints
168+
.captureBaseline({ scope, ordinalWithinScope: 2 })
169+
.pipe(Effect.result, Effect.forkChild);
170+
yield* TestClock.adjust("5 seconds");
171+
assert.strictEqual((yield* Fiber.join(slowBaseline))._tag, "Failure");
172+
assert.strictEqual(yield* Ref.get(snapshots), 2);
173+
assert.strictEqual((yield* Ref.get(refs)).size, 1);
174+
}).pipe(Effect.provide(layerTest));
175+
}),
176+
);
177+
101178
it.effect("records a repository probe failure as an error checkpoint, not a missing one", () =>
102179
Effect.gen(function* () {
103180
const scope: OrchestrationV2CheckpointScope = {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
2525

2626
const CHECKPOINT_REFS_PREFIX = "refs/t3/orchestration-v2/checkpoints";
2727
const ROOT_CHECKPOINT_SCOPE_NAME = "root";
28+
// The provider turn waits on the baseline, so a workspace too slow for this starts without one.
29+
// It bounds only the snapshot: waiting on the lock lets an in-flight capture finish the same ref.
30+
const BASELINE_CAPTURE_BUDGET = "5 seconds";
2831

2932
export class CheckpointRootScopePrepareError extends Schema.TaggedError<CheckpointRootScopePrepareError>()(
3033
"CheckpointRootScopePrepareError",
@@ -287,6 +290,7 @@ export const layer: Layer.Layer<
287290
checkpointRef,
288291
})
289292
.pipe(
293+
Effect.timeout(BASELINE_CAPTURE_BUDGET),
290294
Effect.catchTags({
291295
VcsUnsupportedOperationError: () => Effect.void,
292296
}),

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ it.effect(
11211121
}).pipe(Effect.provide(layerRunExecutionTest)),
11221122
);
11231123

1124-
it.effect("starts the provider without waiting for a baseline capture that times out", () =>
1124+
it.effect("starts the provider when checkpoint baseline capture fails", () =>
11251125
Effect.gen(function* () {
11261126
const threadId = ThreadId.make("thread:run-execution-baseline-failure");
11271127
const runId = RunId.make("run:run-execution-baseline-failure");
@@ -1135,7 +1135,7 @@ it.effect("starts the provider without waiting for a baseline capture that times
11351135
const checkpointScope = {
11361136
id: CheckpointScopeId.make("checkpoint-scope:run-execution-baseline-failure"),
11371137
} as OrchestrationV2CheckpointScope;
1138-
const providerStartedAt = yield* Ref.make<ReadonlyArray<number>>([]);
1138+
const providerStarts = yield* Ref.make(0);
11391139
const writes = yield* Ref.make<
11401140
ReadonlyArray<{
11411141
events: ReadonlyArray<OrchestrationV2DomainEvent>;
@@ -1149,16 +1149,12 @@ it.effect("starts the provider without waiting for a baseline capture that times
11491149
PreviewAgentActivity.layer,
11501150
Layer.mock(CheckpointService.CheckpointServiceV2)({
11511151
captureBaseline: () =>
1152-
Effect.sleep("30 seconds").pipe(
1153-
Effect.andThen(
1154-
Effect.fail(
1155-
new CheckpointService.CheckpointBaselineCaptureError({
1156-
scopeId: checkpointScope.id,
1157-
ordinalWithinScope: 0,
1158-
cause: new Error("VCS process timed out"),
1159-
}),
1160-
),
1161-
),
1152+
Effect.fail(
1153+
new CheckpointService.CheckpointBaselineCaptureError({
1154+
scopeId: checkpointScope.id,
1155+
ordinalWithinScope: 0,
1156+
cause: new Error("VCS process timed out"),
1157+
}),
11621158
),
11631159
}),
11641160
layerTurnItemPositionsTest,
@@ -1178,18 +1174,15 @@ it.effect("starts the provider without waiting for a baseline capture that times
11781174
),
11791175
);
11801176

1181-
const start = yield* Effect.gen(function* () {
1177+
yield* Effect.gen(function* () {
11821178
const runExecution = yield* RunExecutionService.RunExecutionServiceV2;
11831179
yield* runExecution.startRootRun({
11841180
commandId: CommandId.make("command:run-execution-baseline-failure"),
11851181
appThread: rootAppThread(threadId),
11861182
providerSessionId,
11871183
session: {
11881184
events: Stream.never,
1189-
startTurn: () =>
1190-
Clock.currentTimeMillis.pipe(
1191-
Effect.flatMap((now) => Ref.update(providerStartedAt, (times) => [...times, now])),
1192-
),
1185+
startTurn: () => Ref.update(providerStarts, (count) => count + 1),
11931186
} as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime,
11941187
run: {
11951188
id: runId,
@@ -1229,13 +1222,9 @@ it.effect("starts the provider without waiting for a baseline capture that times
12291222
},
12301223
},
12311224
});
1232-
}).pipe(Effect.provide(layerTest), Effect.forkChild);
1233-
yield* TestClock.adjust("30 seconds");
1234-
yield* Fiber.join(start);
1225+
}).pipe(Effect.provide(layerTest));
12351226

1236-
const startedAt = yield* Ref.get(providerStartedAt);
1237-
assert.equal(startedAt.length, 1);
1238-
assert.isBelow(startedAt[0]!, 30_000);
1227+
assert.equal(yield* Ref.get(providerStarts), 1);
12391228

12401229
const events = (yield* Ref.get(writes)).flatMap((write) => [...write.events]);
12411230
const failedEvent = events.find(

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ function isSettledSubagentStatus(status: OrchestrationV2Subagent["status"]): boo
9999
return !isOrchestrationV2WorkActive(status);
100100
}
101101

102-
// The prompt waits on the baseline, which must precede the agent's edits, so it cannot run alongside.
103-
// A workspace too slow for this budget starts without one rather than stalling every turn.
104-
const BASELINE_CAPTURE_BUDGET = Duration.seconds(5);
105-
106102
/** A provider-native subagent's turns arrive on its parent's run, but the parent agent started them. */
107103
const NATIVE_SUBAGENT_TURN_ORIGIN: ProviderUsageAnalytics.ProviderTurnOrigin = {
108104
createdBy: "agent",
@@ -972,7 +968,6 @@ export const layer: Layer.Layer<
972968
ordinalWithinScope: Math.max(0, input.run.ordinal - 1),
973969
})
974970
).pipe(
975-
Effect.timeout(BASELINE_CAPTURE_BUDGET),
976971
Effect.catchCauseIf(
977972
(cause) => !Cause.hasInterruptsOnly(cause),
978973
(_cause) =>

0 commit comments

Comments
 (0)