Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 98 additions & 4 deletions apps/server/src/orchestration/Layers/CheckpointReactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ describe("CheckpointReactor", () => {
readonly checkpointLookupFailure?: (
cwd: string,
) => VcsProcessTimeoutError | VcsProcessSpawnError | undefined;
readonly workspaceRefresh?: (cwd: string) => Effect.Effect<void>;
readonly hasSession?: boolean;
readonly seedFilesystemCheckpoints?: boolean;
readonly initializeGit?: boolean;
Expand Down Expand Up @@ -390,10 +391,10 @@ describe("CheckpointReactor", () => {
).pipe(Layer.provide(VcsDriverRegistry.layer)),
),
Layer.provideMerge(
WorkspaceEntries.layer.pipe(
Layer.provide(WorkspacePaths.layer),
Layer.provideMerge(VcsDriverRegistry.layer),
),
(options?.workspaceRefresh
? Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: options.workspaceRefresh })
: WorkspaceEntries.layer
).pipe(Layer.provide(WorkspacePaths.layer), Layer.provideMerge(VcsDriverRegistry.layer)),
),
Layer.provideMerge(WorkspacePaths.layer),
Layer.provideMerge(VcsProcess.layer),
Expand Down Expand Up @@ -664,6 +665,99 @@ describe("CheckpointReactor", () => {
}),
);

effectIt.effect(
"finalizes checkpoints in both workspaces while entry refresh is blocked and coalesces later scans",
() =>
Effect.gen(function* () {
const entered = yield* Deferred.make<void>();
const release = yield* Deferred.make<void>();
const refreshCalls: string[] = [];
let secondCwd = "";
const harness = yield* Effect.promise(() =>
createHarness({
seedFilesystemCheckpoints: false,
secondThreadSharingWorktree: true,
secondThreadWorktreePath: () => {
secondCwd = createGitRepository();
tempDirs.push(secondCwd);
return secondCwd;
},
workspaceRefresh: (cwd) =>
Effect.gen(function* () {
refreshCalls.push(cwd);
if (refreshCalls.length === 1) {
yield* Deferred.succeed(entered, undefined);
yield* Deferred.await(release);
}
}),
}),
);
for (const [index, threadId] of [
"thread-1",
"thread-2",
"thread-1",
"thread-1",
"thread-1",
].entries()) {
const turnId = asTurnId(`turn-refresh-${index}`);
const id = ThreadId.make(threadId);
harness.provider.emit({
type: "turn.started",
eventId: EventId.make(`evt-refresh-start-${index}`),
provider: ProviderDriverKind.make("codex"),
createdAt: "2026-01-01T00:00:00.000Z",
threadId: id,
turnId,
});
if (index < 2)
expect(yield* harness.nextReceipt).toMatchObject({
type: "checkpoint.baseline.captured",
threadId: id,
});
const cwd = threadId === "thread-1" ? harness.cwd : secondCwd;
NodeFS.writeFileSync(NodePath.join(cwd, "README.md"), `snapshot ${index}\n`);
harness.provider.emit({
type: "turn.completed",
eventId: EventId.make(`evt-refresh-complete-${index}`),
provider: ProviderDriverKind.make("codex"),
createdAt: "2026-01-01T00:00:01.000Z",
threadId: id,
turnId,
payload: { state: "completed" },
});
expect(yield* harness.nextReceipt).toMatchObject({
type: "checkpoint.diff.finalized",
threadId: id,
turnId,
});
expect(yield* harness.nextReceipt).toMatchObject({
type: "turn.processing.quiesced",
threadId: id,
turnId,
});
if (index === 0) yield* Deferred.await(entered);
}
expect(refreshCalls).toEqual([harness.cwd]);
expect(
gitShowFileAtRef(
harness.cwd,
checkpointRefForThreadTurn(ThreadId.make("thread-1"), 4),
"README.md",
),
).toBe("snapshot 4\n");
expect(
gitShowFileAtRef(
secondCwd,
checkpointRefForThreadTurn(ThreadId.make("thread-2"), 1),
"README.md",
),
).toBe("snapshot 1\n");
yield* Deferred.succeed(release, undefined);
yield* Effect.promise(harness.drain);
expect(refreshCalls).toEqual([harness.cwd, secondCwd, harness.cwd]);
}),
);

effectIt.effect("captures baseline and large turn summaries before completion receipts", () =>
Effect.gen(function* () {
const harness = yield* Effect.promise(() =>
Expand Down
29 changes: 25 additions & 4 deletions apps/server/src/orchestration/Layers/CheckpointReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ const make = Effect.gen(function* () {
const path = yield* Path.Path;
const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
const pullRequests = yield* PullRequestService.PullRequestService;
const queuedEntryRefreshes = new Set<string>();
const entryRefreshWorker = yield* makeDrainableWorker((cwd: string) =>
Effect.sync(() => queuedEntryRefreshes.delete(cwd)).pipe(
Effect.andThen(workspaceEntries.refresh(cwd)),
Effect.catchCause((cause) =>
Cause.hasInterruptsOnly(cause)
? Effect.failCause(cause)
: Effect.logWarning("failed to refresh checkpoint workspace entries", {
cwd,
}),
),
),
);
const refreshWorkspaceEntries = Effect.fn("refreshWorkspaceEntries")(function* (cwd: string) {
if (queuedEntryRefreshes.has(cwd)) return;
queuedEntryRefreshes.add(cwd);
yield* entryRefreshWorker.enqueue(cwd);
});

const startedTurns = new Map<ThreadId, TurnId>();
const pending = new Set<ThreadId>();

Expand Down Expand Up @@ -270,7 +289,7 @@ const make = Effect.gen(function* () {

// Refresh the workspace entry index so the @-mention file picker
// reflects files created or deleted during this turn.
yield* workspaceEntries.refresh(input.cwd);
yield* refreshWorkspaceEntries(input.cwd);

// Git may have been initialized during this turn, leaving no pre-turn
// snapshot. Keep the completion checkpoint for future turns, but do not
Expand Down Expand Up @@ -629,7 +648,6 @@ const make = Effect.gen(function* () {
? Effect.failCause(cause)
: Effect.logWarning("failed to refresh git status after turn completion", {
threadId: event.threadId,
cause: Cause.pretty(cause),
}),
),
),
Expand Down Expand Up @@ -850,7 +868,7 @@ const make = Effect.gen(function* () {

// Refresh the workspace entry index so the @-mention file picker
// reflects the reverted filesystem state.
yield* workspaceEntries.refresh(checkpointCwd);
yield* refreshWorkspaceEntries(checkpointCwd);
}

const rolledBackTurns = Math.max(0, currentTurnCount - event.payload.turnCount);
Expand Down Expand Up @@ -1041,7 +1059,10 @@ const make = Effect.gen(function* () {

return {
start,
drain: worker.drain.pipe(Effect.andThen(statusRefreshWorker.drain)),
drain: worker.drain.pipe(
Effect.andThen(statusRefreshWorker.drain),
Effect.andThen(entryRefreshWorker.drain),
),
} satisfies CheckpointReactorShape;
});

Expand Down
Loading