Skip to content

Commit 8bdeaf9

Browse files
mchwangclaude
andcommitted
Merge F6b cffdf4c (now on main) into the #48 browser fixes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 parents de8af78 + cffdf4c commit 8bdeaf9

4 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎docs/implementation/merge-handoff.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ A rebase moves the pull request's base. The guarded merge would then scan only t
6363
| Cancel while waiting | the wait stops at once; the task closes; the job ends `cancelled` |
6464
| Cancel during the guarded merge | before GitHub is asked, the merge stops and the job ends `cancelled`; once a merge attempt is submitting, a cancel is refused, as before |
6565
| Shutdown | in shutdown's first, synchronous step, before requests drain, the job is told to stop: a wait ends at once, and nothing after it asks GitHub to merge. A preparation in progress stops only when preparation does, after the drain, as for a `prepare-merge` click; the job then stops without merging. The job is awaited before the store closes. While preparing or waiting, it stays `running` on disk. A guarded merge in progress stops as a click's does: if GitHub was not asked yet, the job stays `running`; if it was, the job ends as the merge attempt records it: `blocked` if GitHub refused (`failed` if the head changed, or the review changed while GitHub was asked), otherwise `submitted`, and the attempt reconciles as before |
66-
| Ending the job fails | if the write that ends a job fails, or reading the task after the job failed does, the server logs "Could not end merge job …; it shows as running until the server restarts" with the store's message. The job stays `running` on disk: the page keeps following it, and a new click is refused while it runs. The next start settles it as below |
66+
| Ending the job fails | if the write that ends a job fails, or reading the task after the job failed does, the server logs, also during shutdown (after GitHub was asked to merge), "Could not end merge job …; it shows as running until the server restarts" with the store's message. The job stays `running` on disk: the page keeps following it, and a new click is refused while it runs. The next start settles it as below |
6767
| Restart | a `running` job that had not asked GitHub to merge becomes `failed` ("The server restarted before the merge finished"). One that had (its merge action ID is saved before the guarded merge starts) ends as its merge attempt records it: `merged`, `queued`, `submitted`, or `blocked` after a refusal, which also moves the task to approved, merge blocked (`failed`, task unchanged, when the head changed or the review changed while GitHub was asked). A `prepare-merge` action and a push marker are settled as before |
6868

6969
## Storage

‎test/merge-job.test.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ it('keeps a job running and reports it when even reading the task fails after th
376376
await run.loading;
377377
f.store.getTask = () => { throw new Error('database is locked'); };
378378
run.fail(new Error('History worker crashed.'));
379-
// The job's promise settles without rejecting, so the server's `.finally` and `close()` see no rejection.
379+
// The job's promise settles without rejecting, so `close()` and the server's `.finally` (whose callback catches its own
380+
// errors) see no rejection.
380381
await expect(f.coordinator.current).resolves.toBeUndefined();
381382
f.store.getTask = getTask;
382383
expect(logged).toHaveBeenCalledWith(`Could not end merge job ${started.id}; it shows as running until the server restarts: "database is locked"`);
@@ -497,6 +498,27 @@ it('ends blocked when GitHub refuses the merge while shutdown stops the job', as
497498
expect(f.store.getTask(identity).status).toBe('approved but merge blocked');
498499
});
499500

501+
it.each(['submitted', 'blocked'] as const)('reports a job it could not end during shutdown (%s), after GitHub was asked to merge', async kind => {
502+
// Shutdown's first step (stop) has run, but GitHub was already asked, so the job still ends through its ending write.
503+
let coordinatorRef: MergeJobCoordinator | null = null;
504+
const f = setup({ merge: kind === 'blocked'
505+
? async () => { coordinatorRef!.stop(); throw new MergeSubmissionError('Base branch was modified.', 'refused'); }
506+
: signal => new Promise((_resolve, reject) => signal?.addEventListener('abort', () => reject(signal.reason), { once: true })) });
507+
coordinatorRef = f.coordinator;
508+
const fail = vi.fn(() => { throw new Error('disk full'); });
509+
const { blockMergeJob, finishMergeJob } = f.store;
510+
if (kind === 'blocked') f.store.blockMergeJob = fail; else f.store.finishMergeJob = fail;
511+
const logged = vi.spyOn(console, 'error').mockImplementation(() => {});
512+
try {
513+
const started = await f.coordinator.start('review-token', randomUUID());
514+
if (kind === 'submitted') { await vi.waitFor(() => expect(f.gh.merged).toHaveLength(1)); await f.coordinator.close(); }
515+
else await f.coordinator.current;
516+
expect(fail).toHaveBeenCalledTimes(1);
517+
expect(logged).toHaveBeenCalledWith(`Could not end merge job ${started.id}; it shows as running until the server restarts: "disk full"`);
518+
expect(f.store.getMergeJob(identity, started.id)).toMatchObject({ state: 'running' });
519+
} finally { Object.assign(f.store, { blockMergeJob, finishMergeJob }); logged.mockRestore(); }
520+
});
521+
500522
it('settles a job a crash left in its merge step from its merge attempt at startup', async () => {
501523
for (const [settle, expected, status] of [
502524
[() => undefined, 'submitted', 'in review'],

‎test/runner-publishing.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,4 +2147,41 @@ describe('the one-click merge job (F6b of #22)', () => {
21472147
await vi.waitFor(() => expect(w.github.prs[0]!.open).toBe(false), { timeout: 20_000 });
21482148
expect(gh.merged).toEqual([]);
21492149
}, 60_000);
2150+
2151+
it('logs, and does not reject, when reading the task fails as the server follows a settled merge job', async () => {
2152+
const w = world(); mergeable(w);
2153+
let storeRef: Store | null = null, release!: () => void;
2154+
const answered = new Promise<void>(resolve => { release = resolve; });
2155+
const base = gateway(() => storeRef!, () => []);
2156+
// GitHub answers the merge only once the click was answered, so the job settles after the click's own review reload.
2157+
const gh: MergeGateway = { inspect: base.inspect, merge: async () => { await answered; return { url: 'https://github.com/acme/app/pull/100' }; } };
2158+
const served = await serve(w, { before: async service => { completeAll(service); await acceptAndApprove(service); }, issueGateway, preMerge: preparation, mergeGateway: gh,
2159+
runnerPublished: true, mergeJobTiming: { checkWaitMs: 20_000, firstDelayMs: 20, maxDelayMs: 50 } });
2160+
storeRef = served.store; createIdentity = () => served.identity;
2161+
await publishSettled(served.app, served.identity);
2162+
const unhandled: unknown[] = [], onUnhandled = (reason: unknown) => { unhandled.push(reason); };
2163+
process.on('unhandledRejection', onUnhandled);
2164+
const logged = vi.spyOn(console, 'error');
2165+
// The first task read after the job's ending write fails: that is the server's `.finally` following the job.
2166+
let failNext = false;
2167+
const getTask = served.store.getTask.bind(served.store), finishMergeJob = served.store.finishMergeJob.bind(served.store);
2168+
served.store.finishMergeJob = ((...args: Parameters<Store['finishMergeJob']>) => { const ended = finishMergeJob(...args); failNext = true; return ended; }) as Store['finishMergeJob'];
2169+
served.store.getTask = ((identity: PlanIdentity) => {
2170+
if (failNext) { failNext = false; throw new Error('database is locked'); }
2171+
return getTask(identity);
2172+
}) as Store['getTask'];
2173+
try {
2174+
expect((await merge(served.app, (await review(served.app)).token)).status).toBe(200);
2175+
release();
2176+
await vi.waitFor(() => expect(served.store.latestMergeJob(served.identity)).toMatchObject({ state: 'merged' }), { timeout: 10_000 });
2177+
// The server's callback runs a few microtasks after the ending write; an unhandled rejection is reported after them.
2178+
await new Promise(resolve => setTimeout(resolve, 50));
2179+
expect(failNext).toBe(false);
2180+
expect(unhandled).toEqual([]);
2181+
expect(logged).toHaveBeenCalledWith('Could not close the cancelled task\'s pull requests after its merge job: "database is locked"');
2182+
} finally {
2183+
process.off('unhandledRejection', onUnhandled);
2184+
served.store.getTask = getTask; served.store.finishMergeJob = finishMergeJob; logged.mockRestore();
2185+
}
2186+
}, 60_000);
21502187
});

‎web/server.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,12 @@ export async function startServer(config: ReviewConfig, port = 4318, questionAge
743743
const job=await mergeJobs.start(input.token,input.actionId,()=>untrusted??(runner?.isActive(identity)||executor?.busy(identity)||publishing?.busy(identity)
744744
?'The runner or publisher is busy; the merge cannot start yet.':null),requestAbort.signal);
745745
const running=mergeJobs.current;
746-
if(running)void running.finally(()=>{ if(publishing&&!stopping&&service.store.getTask(identity).status==='cancelled')publishing.taskCancelled(identity); });
746+
// The job's run never rejects, but this callback's own promise would: it must not throw (an unhandled rejection
747+
// exits the server), so a failed read or close start is logged instead.
748+
if(running)void running.finally(()=>{
749+
try { if(publishing&&!stopping&&service.store.getTask(identity).status==='cancelled')publishing.taskCancelled(identity); }
750+
catch (error) { console.error(`Could not close the cancelled task's pull requests after its merge job: ${JSON.stringify(error instanceof Error ? error.message : String(error))}`); }
751+
});
747752
// The job is saved, so its click is settled. The reload awaits a history worker (#170) while the job's preparation
748753
// writes, so it can fail without anything being wrong with the job: answer the job, and the page follows it and
749754
// asks for a refresh (as after a direct merge whose reload fails). A reload stopped by shutdown answers 503, and

0 commit comments

Comments
 (0)