Skip to content

Commit 948855f

Browse files
committed
fix(server): preserve temporary branches during finalization
1 parent 96072c8 commit 948855f

2 files changed

Lines changed: 46 additions & 21 deletions

File tree

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ it.effect.each(
7474
activeRun: null,
7575
expected: ["/repo"],
7676
},
77+
{
78+
label: "discovers the renamed branch's PR in the same refresh",
79+
branch: "feature",
80+
checkedOut: "renamed",
81+
follow: true,
82+
activeRun: null,
83+
expected: ["/repo"],
84+
},
7785
{
7886
label: "leaves the default branch's PR cache alone",
7987
branch: "main",
@@ -99,6 +107,7 @@ it.effect.each(
99107
).map((scenario) => [scenario.label, scenario] as const),
100108
)("%s", ([, scenario]) => {
101109
const refreshed: string[] = [];
110+
let branch: string = scenario.branch;
102111
const lookedUp = Deferred.makeUnsafe<void>();
103112
const threadId = ThreadId.make("thread-pr-refresh");
104113
const runId = RunId.make("completed-run");
@@ -131,7 +140,7 @@ it.effect.each(
131140
getThreadShell: () =>
132141
Effect.succeed({
133142
id: threadId,
134-
branch: scenario.branch,
143+
branch,
135144
activeRunId: scenario.activeRun === null ? null : RunId.make(scenario.activeRun),
136145
} as OrchestrationV2ThreadShell),
137146
}),
@@ -140,7 +149,19 @@ it.effect.each(
140149
);
141150
return Effect.gen(function* () {
142151
const observer = yield* RunFinalization.RunFinalizationObserver;
143-
yield* observer.refresh({ cwd: "/repo", threadId, runId });
152+
yield* observer.refresh({
153+
cwd: "/repo",
154+
threadId,
155+
runId,
156+
...("follow" in scenario
157+
? {
158+
followBranch: (checkedOutBranch: string) =>
159+
Effect.sync(() => {
160+
branch = checkedOutBranch;
161+
}),
162+
}
163+
: {}),
164+
});
144165
if (scenario.expected.length > 0) yield* Deferred.await(lookedUp);
145166
assert.deepEqual(refreshed, [...scenario.expected]);
146167
}).pipe(Effect.provide(layer));
@@ -289,7 +310,7 @@ it.effect.each(
289310
const threadId = ThreadId.make("thread-branch-drift");
290311
const runId = RunId.make("run-branch-drift");
291312
const scopeId = CheckpointScopeId.make("scope-branch-drift");
292-
let refreshes = 0;
313+
const followed: string[] = [];
293314
const updates: Array<{
294315
branch: unknown;
295316
expectedBranch: unknown;
@@ -336,9 +357,10 @@ it.effect.each(
336357
}),
337358
}),
338359
Layer.succeed(RunFinalization.RunFinalizationObserver, {
339-
refresh: () =>
340-
Effect.sync(() => {
341-
refreshes += 1;
360+
refresh: ({ followBranch }) =>
361+
Effect.gen(function* () {
362+
if (followBranch) yield* followBranch(scenario.checkedOut);
363+
followed.push(scenario.checkedOut);
342364
return scenario.checkedOut;
343365
}),
344366
refreshAfterTurn: () => Effect.void,
@@ -357,7 +379,6 @@ it.effect.each(
357379
expectedWorktreePath: "/repo",
358380
})),
359381
);
360-
// An adopted branch is refreshed again so its PR is found this turn.
361-
assert.equal(refreshes, 1 + scenario.expected.length);
382+
assert.deepEqual(followed, [scenario.checkedOut]);
362383
}).pipe(Effect.provide(layer));
363384
});

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class RunFinalizationObserver extends Context.Reference<{
3636
readonly cwd: string;
3737
readonly threadId: ThreadId;
3838
readonly runId: RunId;
39+
readonly followBranch?: (branch: string) => Effect.Effect<void>;
3940
}) => Effect.Effect<string | null, RunFinalizationRefreshError>;
4041
}>("t3/orchestration-v2/RunFinalizationObserver", {
4142
defaultValue: () => ({
@@ -76,6 +77,7 @@ const make = Effect.gen(function* () {
7677
if (
7778
!thread ||
7879
thread.branch === null ||
80+
isTemporaryWorktreeBranch(thread.branch) ||
7981
thread.branch === input.checkedOutBranch ||
8082
thread.worktreePath !== input.cwd
8183
) {
@@ -95,8 +97,6 @@ const make = Effect.gen(function* () {
9597
expectedBranch: thread.branch,
9698
expectedWorktreePath: input.cwd,
9799
});
98-
// The first refresh skipped the PR lookup because the thread still had the old branch.
99-
yield* observer.refresh({ cwd: input.cwd, threadId: thread.id, runId: input.runId });
100100
},
101101
(effect, input) =>
102102
effect.pipe(
@@ -129,8 +129,19 @@ const make = Effect.gen(function* () {
129129
const cwd = projection.checkpointScopes.find((scope) => scope.id === input.scopeId)?.cwd;
130130
if (cwd !== undefined) {
131131
// Retrying a display refresh after the checkpoint is durable blocks the next turn.
132-
const checkedOutBranch = yield* observer
133-
.refresh({ cwd, threadId: input.threadId, runId: input.runId })
132+
yield* observer
133+
.refresh({
134+
cwd,
135+
threadId: input.threadId,
136+
runId: input.runId,
137+
followBranch: (checkedOutBranch) =>
138+
followWorktreeBranch({
139+
threadId: input.threadId,
140+
runId: input.runId,
141+
cwd,
142+
checkedOutBranch,
143+
}),
144+
})
134145
.pipe(
135146
Effect.catchTags({
136147
RunFinalizationRefreshError: (cause) =>
@@ -140,14 +151,6 @@ const make = Effect.gen(function* () {
140151
}).pipe(Effect.as(null)),
141152
}),
142153
);
143-
if (checkedOutBranch !== null) {
144-
yield* followWorktreeBranch({
145-
threadId: input.threadId,
146-
runId: input.runId,
147-
cwd,
148-
checkedOutBranch,
149-
});
150-
}
151154
}
152155
});
153156
return RunFinalizationService.of({ finalize });
@@ -166,12 +169,13 @@ export const layerObserver = Layer.effect(
166169
return {
167170
refreshAfterTurn: pullRequests.refreshAfterTurn,
168171
refresh: Effect.fn("RunFinalizationService.refresh")(
169-
function* ({ cwd, threadId, runId }) {
172+
function* ({ cwd, threadId, runId, followBranch }) {
170173
const [, local] = yield* Effect.all(
171174
[workspaceEntries.refresh(cwd), vcsStatus.refreshLocalStatus(cwd)],
172175
{ concurrency: "unbounded" },
173176
);
174177
if (local.refName === null) return null;
178+
if (followBranch) yield* followBranch(local.refName);
175179
if (local.isDefaultRef) return local.refName;
176180
const thread = yield* projections.getThreadShell(threadId);
177181
if (!thread || thread.branch !== local.refName) return local.refName;

0 commit comments

Comments
 (0)