Skip to content

Commit ab17a74

Browse files
committed
fix(server): keep ancestry cleanup when pull request evidence is missing
A failed pull request lookup, or a linked pull request for another branch, now leaves cleanup on the default ancestry check instead of skipping the worktree or measuring it against an unrelated target. Branch discovery also requests the head commit, so squash merges without a saved link qualify.
1 parent 983e1ad commit ab17a74

5 files changed

Lines changed: 62 additions & 25 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
20122012
updatedAt: "2026-03-10T07:00:00.000Z",
20132013
});
20142014
expect(ghCalls).toContain(
2015-
"pr list --head statemachine --state all --limit 100 --json number,title,url,baseRefName,headRefName,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
2015+
"pr list --head statemachine --state all --limit 100 --json number,title,url,baseRefName,headRefName,headRefOid,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
20162016
);
20172017
}),
20182018
20_000,
@@ -2078,7 +2078,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
20782078
updatedAt: "2026-03-10T07:00:00.000Z",
20792079
});
20802080
expect(ghCalls).toContain(
2081-
"pr list --head main --state all --limit 100 --json number,title,url,baseRefName,headRefName,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
2081+
"pr list --head main --state all --limit 100 --json number,title,url,baseRefName,headRefName,headRefOid,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
20822082
);
20832083
}),
20842084
20_000,

‎apps/server/src/orchestration/ThreadSettlementReactor.test.ts‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
DEFAULT_SERVER_SETTINGS,
33
EventId,
4+
GitManagerError,
45
ProjectId,
56
ProviderInstanceId,
67
ProviderDriverKind,
@@ -1577,6 +1578,8 @@ describe("storage cleanup", () => {
15771578
"unmerged",
15781579
"unchanged",
15791580
"unchanged-two-worktrees",
1581+
"unchanged-lookup-failed",
1582+
"unchanged-other-pr",
15801583
"diverged",
15811584
"head-moved",
15821585
"deleted",
@@ -1637,15 +1640,18 @@ describe("storage cleanup", () => {
16371640
yield* fs.writeFileString(recentImage, "recent");
16381641
const recent = DateTime.toDateUtc(DateTime.makeUnsafe(NOW));
16391642
yield* fs.utimes(recentImage, recent, recent);
1643+
const linkedUnchanged =
1644+
protection === "unchanged-lookup-failed" || protection === "unchanged-other-pr";
16401645
const thread = makeThread("storage-thread", {
1641-
branchPullRequest: protection.startsWith("squash-")
1642-
? {
1643-
projectId: PROJECT_ID,
1644-
repository: "owner/repo",
1645-
number: 42,
1646-
url: "https://example.test/owner/repo/pull/42",
1647-
}
1648-
: null,
1646+
branchPullRequest:
1647+
protection.startsWith("squash-") || linkedUnchanged
1648+
? {
1649+
projectId: PROJECT_ID,
1650+
repository: "owner/repo",
1651+
number: 42,
1652+
url: "https://example.test/owner/repo/pull/42",
1653+
}
1654+
: null,
16491655
branch: "feature",
16501656
worktreePath,
16511657
latestUserMessageAt:
@@ -1679,6 +1685,7 @@ describe("storage cleanup", () => {
16791685
const unchangedRule =
16801686
protection === "unchanged" ||
16811687
protection === "unchanged-two-worktrees" ||
1688+
linkedUnchanged ||
16821689
protection === "diverged" ||
16831690
protection === "head-moved";
16841691
let headReads = 0;
@@ -1831,19 +1838,33 @@ describe("storage cleanup", () => {
18311838
invalidateStatus: () => Effect.void,
18321839
branchPullRequest: (input, options) => {
18331840
assert.strictEqual(options?.refresh, true);
1834-
if (protection.startsWith("squash-")) {
1841+
if (protection.startsWith("squash-") || linkedUnchanged) {
18351842
assert.strictEqual(
18361843
input.reference,
18371844
"https://example.test/owner/repo/pull/42",
18381845
);
18391846
}
1847+
if (protection === "unchanged-lookup-failed") {
1848+
return Effect.fail(
1849+
new GitManagerError({
1850+
operation: "branchPullRequest",
1851+
cwd: input.cwd,
1852+
detail: "host unavailable",
1853+
}),
1854+
);
1855+
}
18401856
return Effect.succeed({
18411857
...makeBranchPullRequest(
18421858
protection === "unmerged" || protection === "squash-stale"
18431859
? "open"
18441860
: "merged",
18451861
),
1846-
headRef: protection === "squash-wrong-branch" ? "other" : "feature",
1862+
headRef:
1863+
protection === "squash-wrong-branch" || protection === "unchanged-other-pr"
1864+
? "other"
1865+
: "feature",
1866+
// Cleanup must not measure this checkout against another branch's target.
1867+
...(protection === "unchanged-other-pr" ? { baseRef: "release" } : {}),
18471868
url: "https://example.test/owner/repo/pull/42",
18481869
...(protection.startsWith("squash-") && protection !== "squash-missing-head"
18491870
? { headSha: (protection === "squash-extra" ? "c" : "a").repeat(40) }
@@ -2048,7 +2069,8 @@ describe("storage cleanup", () => {
20482069
protection === "merged" ||
20492070
protection === "squash-merged" ||
20502071
protection === "unchanged" ||
2051-
protection === "unchanged-two-worktrees";
2072+
protection === "unchanged-two-worktrees" ||
2073+
linkedUnchanged;
20522074
assert.strictEqual(yield* fs.exists(worktreePath), !removed);
20532075
assert.deepStrictEqual(
20542076
removals,

‎apps/server/src/sourceControl/GitHubSourceControlProvider.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ it.effect("uses gh json listing for non-open change request state queries", () =
153153
url: "https://github.com/pingdotgg/t3code/pull/7",
154154
baseRefName: "main",
155155
headRefName: "feature/merged",
156+
headRefOid: "a".repeat(40),
156157
state: "merged",
157158
mergedAt: "2026-01-01T00:00:00Z",
158159
updatedAt: "2026-01-02T00:00:00.000Z",
@@ -180,9 +181,10 @@ it.effect("uses gh json listing for non-open change request state queries", () =
180181
"--limit",
181182
"10",
182183
"--json",
183-
"number,title,url,baseRefName,headRefName,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
184+
"number,title,url,baseRefName,headRefName,headRefOid,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
184185
]);
185186
assert.strictEqual(changeRequests[0]?.provider, "github");
187+
assert.strictEqual(changeRequests[0]?.headSha, "a".repeat(40));
186188
assert.strictEqual(changeRequests[0]?.state, "merged");
187189
assert.strictEqual(changeRequests[0]?.mergedAt, "2026-01-01T00:00:00Z");
188190
assert.deepStrictEqual(

‎apps/server/src/sourceControl/GitHubSourceControlProvider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const make = Effect.gen(function* () {
169169
"--limit",
170170
String(input.limit ?? 20),
171171
"--json",
172-
"number,title,url,baseRefName,headRefName,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
172+
"number,title,url,baseRefName,headRefName,headRefOid,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
173173
],
174174
})
175175
.pipe(

‎apps/server/src/storageCleanup.ts‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,31 @@ export const make = Effect.gen(function* () {
251251
links.find((pr) => pr.snapshot?.headBranch === thread.branch) ??
252252
links.find((pr) => pr.snapshot === null);
253253
const reference = linked?.url ?? (!deleted ? thread.branchPullRequest?.url : undefined);
254-
const pullRequest =
254+
// A failed lookup is missing evidence, so the ancestry check still applies.
255+
const found =
255256
thread.branch !== null && (settings.worktreeOnMerge || reference !== undefined)
256-
? yield* gitManager.branchPullRequest(
257-
{
258-
cwd: worktreePath,
259-
branch: thread.branch,
260-
...(reference ? { reference } : {}),
261-
},
262-
{ refresh: true },
263-
)
257+
? yield* gitManager
258+
.branchPullRequest(
259+
{
260+
cwd: worktreePath,
261+
branch: thread.branch,
262+
...(reference ? { reference } : {}),
263+
},
264+
{ refresh: true },
265+
)
266+
.pipe(
267+
Effect.catch((error) =>
268+
Effect.logDebug("storage cleanup could not read pull request", {
269+
threadId: thread.id,
270+
error,
271+
}).pipe(Effect.as(null)),
272+
),
273+
)
264274
: null;
265-
const merged = pullRequest?.state === "merged" && pullRequest.headRef === thread.branch;
275+
// A link saved without its head branch can name another branch's PR,
276+
// which says nothing about this checkout's merge state or target.
277+
const pullRequest = found?.headRef === thread.branch ? found : null;
278+
const merged = pullRequest?.state === "merged";
266279
// A squash merge changes commit ancestry. Fresh host evidence still proves
267280
// that this exact checkout was merged; later local commits do not qualify.
268281
if (settings.worktreeOnMerge && merged && pullRequest.headSha === head.commitSha) {

0 commit comments

Comments
 (0)