Skip to content

Commit 40641d7

Browse files
fix(github): Read again soon when a patch reads another head commit
When the patch read a head commit other than the cached one, it skipped the write and counted as done. Its pending merge state and checks then stayed fresh for the full hour. That patch is now dropped like one that finds no pull request: its pending fields stay fresh for 15 s, so the next card read asks GitHub. Co-Authored-By: BYK <[email protected]>
1 parent 53414b1 commit 40641d7

2 files changed

Lines changed: 40 additions & 18 deletions

File tree

‎packages/junior-github/src/pull-request-patches.ts‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,34 @@ export async function scheduleGitHubPullRequestPatch(args: {
105105
type HeadFactsNode = z.output<typeof pullRequestHeadFactsNodeSchema>;
106106

107107
// Write the read fields, only over an entry of the same head commit that
108-
// is not newer than the read. Return whether to read again: GitHub still
108+
// is not newer than the read. Return `again` to read again: GitHub still
109109
// computes the merge state, or a webhook came after the read and its
110-
// fields still wait. After the last attempt, a merge state that GitHub
111-
// still computes stays pending only briefly, so the next read asks GitHub.
110+
// fields still wait. Return `expire` when the read is of another head
111+
// commit than the entry, as for a dropped patch. After the last attempt, a
112+
// merge state that GitHub still computes stays pending only briefly, so
113+
// the next read asks GitHub.
112114
async function writeHeadFacts(
113115
cache: PluginCache,
114116
key: string,
115117
node: HeadFactsNode,
116118
last: boolean,
117-
): Promise<boolean> {
119+
): Promise<"again" | "done" | "expire"> {
118120
const mergeable = githubMergeable(node.mergeable);
119-
let readAgain = false;
121+
let outcome: "again" | "done" | "expire" = "done";
120122
await cache.update<GitHubPullRequestFields | null>(key, (current) => {
121-
if (!current || current.head?.sha !== node.headRefOid) return undefined;
123+
if (!current) return undefined;
124+
if (current.head?.sha !== node.headRefOid) {
125+
outcome = "expire";
126+
return undefined;
127+
}
122128
if (isOlderGitHubObject(current, { updated_at: node.updatedAt })) {
123-
readAgain =
124-
current.checks === "pending" || current.mergeable === "pending";
129+
if (current.checks === "pending" || current.mergeable === "pending") {
130+
outcome = "again";
131+
}
125132
return undefined;
126133
}
127134
const computing = mergeable === null && current.mergeable === "pending";
128-
readAgain = computing && !last;
135+
if (computing && !last) outcome = "again";
129136
return {
130137
...(computing && last
131138
? GITHUB_PULL_REQUEST_PENDING_CACHE
@@ -138,7 +145,7 @@ async function writeHeadFacts(
138145
},
139146
};
140147
});
141-
return readAgain;
148+
return outcome;
142149
}
143150

144151
// A dropped patch leaves its pending fields fresh only briefly, so the
@@ -156,8 +163,8 @@ async function expirePending(cache: PluginCache, key: string): Promise<void> {
156163
* to 25 pull requests. A patch whose merge state GitHub still computes,
157164
* whose read is older than its entry, or whose query fails, waits for the
158165
* next heartbeat. A failed query does not split, so an outage costs one
159-
* query per chunk. A patch with no pull request in the answer, or out of
160-
* attempts, is dropped, and its entry is read again
166+
* query per chunk. A patch with no pull request in the answer, of another
167+
* head commit, or out of attempts, is dropped, and its entry is read again
161168
* soon. A failed query is logged, not thrown, so the rest of the heartbeat
162169
* still runs.
163170
*/
@@ -214,9 +221,9 @@ export async function runGitHubPullRequestPatches(args: {
214221
}
215222
const last = patch.attempts + 1 >= PATCH_MAX_ATTEMPTS;
216223
// oxlint-disable-next-line no-await-in-loop -- Each write takes its own entry lock; patches are few.
217-
if (await writeHeadFacts(args.cache, key, node, last)) {
218-
again.push(entry);
219-
}
224+
const outcome = await writeHeadFacts(args.cache, key, node, last);
225+
if (outcome === "again") again.push(entry);
226+
if (outcome === "expire") dropped.push(key);
220227
}
221228
},
222229
);

‎packages/junior/tests/integration/api/github-object-refresh.test.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,23 @@ describe("GitHub pull request cache refresh", () => {
523523
expect(reads).toEqual(["pulls/34", "pulls/34"]);
524524
});
525525

526-
it("reads a pull request again soon after its patch finds no node", async () => {
526+
it.each([
527+
// The patch query answers with no node for #34.
528+
["finds no node", []],
529+
// A push that GitHub has and no webhook brought yet.
530+
[
531+
"reads another head commit",
532+
[
533+
{
534+
commits: checkRuns({ SUCCESS: 1 }),
535+
headRefOid: "ccc333",
536+
mergeable: "MERGEABLE",
537+
number: 34,
538+
updatedAt: "2026-01-02T03:00:11Z",
539+
},
540+
],
541+
],
542+
])("reads a pull request again soon after its patch %s", async (_, patch) => {
527543
stubGitHubApp();
528544
const reads: string[] = [];
529545
mswServer.use(
@@ -537,8 +553,7 @@ describe("GitHub pull request cache refresh", () => {
537553
);
538554
}),
539555
);
540-
// The patch query answers with no node for #34.
541-
const queries = stubGraphql({ patches: [[]] });
556+
const queries = stubGraphql({ patches: [patch] });
542557
const app = await createGitHubApp();
543558
setDashboardConversationLinkOptions({ baseURL: DASHBOARD });
544559
await linkGitHubMember();

0 commit comments

Comments
 (0)