Skip to content

fix(installer): cancel jobs during initial persistence - #291

Merged
oldwinter53 merged 1 commit into
simplexai-labs:mainfrom
Afloat16:fix/cancel-queued-installer-jobs-20261119
Nov 19, 2026
Merged

oldwinter53 merged 1 commit into
simplexai-labs:mainfrom
Afloat16:fix/cancel-queued-installer-jobs-20261119

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What and why

A job becomes visible as queued before its initial private input and snapshot finish saving. Cancelling in that window marks it canceled, but start() then launches the installation anyway. shutdown() and wait() also ignored that initial save, so they could return before the queued job started.

Cancellation could additionally persist a public job before its private input existed. If the input save failed, this left an incomplete record and raced the persistence queue against deletion of the in-memory job.

Track the initial save as part of the job's lifetime. Treat it as busy, skip execution if cancellation was requested, and drain it during shutdown and wait. Cancellation during that save is included in the initial durable snapshot, after the input file is safely written, with a final save if the first snapshot was captured before cancellation. Shutdown requests cancellation for all tracked jobs and drains them. An initial input-save error already reported by start() does not interrupt the server's remaining cleanup; persistence failures for retained jobs still propagate. Completed waiting-user jobs keep their existing replacement behavior.

How it was verified

Rebased and squashed into one commit on 13cb493d94588151844125a5b2a376f27a21ded9 at the maintainer’s request. The conflict was confined to the installer lifecycle notes; both the pending native-sign-in guards and queued-job rules are preserved. The production and regression-test files are byte for byte identical to the previously approved head.

The original six real-storage regressions were reproduced on the base and pass with the change:

  • cancel during the initial input write never invokes the installation runner, persists the canceled state, and permits a later fresh start;
  • shutdown waits for that write and prevents the job from starting afterward;
  • cancellation after the first snapshot was already captured still persists the final canceled state and event;
  • a failed initial input write does not prevent shutdown from cancelling and draining another running job;
  • a failed input write publishes no orphan job and leaves the directory loadable;
  • waiting on a deduplicated queued start waits through both persistence and execution.

The review follow-up checks that shutdown then resolves, rather than repeating the initial input error already received by the start caller. That assertion fails on the previous PR head and passes after the correction. A seventh control fails the late canceled-snapshot write after the first snapshot was saved; shutdown still reports that storage error and retains the job record.

make check-installer passes: typecheck, lint, and 164 tests. Prettier and git diff --check pass. The tests pause the initial input or snapshot storage call and use real private-file writes and a controlled installation runner.

  • Focused tests for the changed behavior
  • Fast checks of the touched installer app

Boundary

The installer AGENTS.md records the queued-job persistence and shutdown invariant. No API contract changes. This affects installer job lifecycle; it adds no network round trip or file walk to the user-message-to-provider-ready path.

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve with nits (1 P3)

Scope: base main, merge base 8d02fe8c; 2 commits, 3 files (apps/linger-installer/AGENTS.md, src/server/jobs.ts, src/server/jobs-start-cancel.test.ts) reviewed at a3dd31d9. Helper skills loaded: coderbook, ponytail-review, karpathy-guidelines. Ran in a separate session.

Findings

P3

shutdown() rethrows an initial-write failure that start() already reported (apps/linger-installer/src/server/jobs.ts:565)

  • The drain loop deliberately swallows record.starting rejections ("start() reports a failed initial write to its caller"), but cancel() also awaits record.starting, so the same storage error comes back through cancellations and shutdown() throws it at the end. The new test shutdown still cancels and drains other jobs when an initial input save fails asserts this.
  • Failure scenario: SIGTERM, /api/stop, or /api/retire arrives while a job's <id>.input.json write is in flight, and that write fails (disk full or permissions). The HTTP caller of start() already gets the error. Then close() in server.ts:656 throws at await jobs.shutdown(), so it skips closeRuntimeLogins(), the launch.html/session.json cleanup, lock.close() and resolveClosed(). Because every caller runs it as void close(), the process exits on an unhandled rejection instead of shutting down cleanly. The storage failure is the cause, but here it has already been reported once.
  • Fix: in shutdown(), ignore a rejected cancellation when its record's start failed. For example, check !this.records.has(record.job.id) after draining, or let cancel() swallow record.starting's rejection the same way the drain does. Keep rethrowing persist failures for jobs that still exist. Then update the test to expect a clean shutdown.

Verified as fine

  • start() sets record.starting synchronously right after records.set, so a concurrent cancel(), busy, shutdown() or deduplicated start() always sees it. No await falls between clearing starting in finally, the cancelRequested check, and executing = true, so a cancel can't slip in between.
  • Microtask order: start() registers on record.starting before any cancel() or wait(), so record.task is set before wait() reads it, and cancel() persists after start() has decided not to execute.
  • emit() skips persisting while starting is set. During that window only cancel() emits, and it awaits starting before calling persist() again. So a failed input write leaves no orphan <id>.json, and persist() never reads a deleted record. A cancel captured after the first snapshot is saved by the extra persist.
  • A double cancel during starting returns at once, because the job is no longer active. retry() on a starting job awaits the cancel and then creates a fresh job. The waiting-user replacement path (replaced awaited inside starting) is unchanged.
  • shutdown() now uses Promise.allSettled, so one failing cancel no longer stops the other jobs from being cancelled and drained.
  • The six regression tests use real private-file writes and control the storage call with promises. They don't rely on timing sleeps; setImmediate() is used only as a yield before negative assertions. Putting them in a separate file with vi.mock("./storage.js") keeps the mock away from the existing job tests.
  • The installer AGENTS.md records the new invariant. No contract, env or cross-app import changes, and nothing added to the user-message → provider_ready path.

Process notes

  • a3dd31d9 fix(installer): persist late queued cancellation and drain all shutdown tasks patches code added by 5acdc8de in this same PR (await (record.starting ?? this.persist(id)) → await record.starting; await this.persist(id), and the sequential shutdown loop). Squash it into the first commit (git rebase -i --autosquash) before the rebase merge.
  • The branch is up to date with main and has no merge commits. Both commits are linked to Afloat16.

CI

All checks that ran pass: check (installer), check (web), changes, repository-gate. The other checks were skipped by path selection. I ran no tests locally; the conclusions above come from reading the code.

Automated review by Claude Code


Automated review of a3dd31d by Claude Code.

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve (0 findings)

Scope: follow-up on a3dd31d, reviewing 574f87b against main (merge base 8d02fe8). 3 commits and 3 files reviewed (apps/linger-installer/{AGENTS.md, src/server/jobs.ts, src/server/jobs-start-cancel.test.ts}); the new-change pass covered only a3dd31d..574f87b. Helper skills coderbook, ponytail-review and karpathy-guidelines were not loaded separately; their review criteria were applied from the review procedure. Ran in a separate session.

Previous findings

  • P3 jobs.ts:565 shutdown rethrows the initial-write failure that start already reported: Resolved. shutdown() (apps/linger-installer/src/server/jobs.ts:566-571) now rethrows a rejected cancellation only when its record is still in this.records. start() deletes the record when the initial save fails (jobs.ts:354) and rethrows the error to its own caller, so shutdown no longer reports that error a second time and server.ts:656 can continue its cleanup. The test now expects undefined in that case (jobs-start-cancel.test.ts, "shutdown drains other jobs without repeating an input error reported by start"). The old code would fail that assertion.

Findings

None.

Verified as fine

  • Retained-job failures still propagate. If the final save of a late cancellation fails, the record stays in this.records and shutdown rethrows the error. This is covered by the new test "shutdown still reports a failed cancellation save for a retained job", which also checks that the runner never ran and that a later start recovers.
  • running[index] lines up with cancellations because both arrays come from the same running list. Records are removed only on a failed start, and IDs come from randomUUID, so a new record cannot reuse a deleted ID and get its error swallowed by mistake.
  • Shutdown still waits for every tracked task and for this.writes before it reports anything. The ordering does not change.
  • AGENTS.md now states the new invariant: start reports an initial input-save failure, and storage failures for retained jobs still propagate.
  • No CJK in added lines or commit messages. No merge commits.

Process notes

  • a3dd31d and 574f87b only patch behaviour that 5acdc8d introduced in this same PR. Because main takes rebase merges, squash them into the first commit (git rebase -i --autosquash) so main gets one coherent fix.
  • Merge state is BLOCKED while checks are pending.

CI

changes and check (web) pass. check (installer) and repository-gate are still pending, and the other checks are skipped as not applicable. I ran vitest run src/server/jobs-start-cancel.test.ts src/server/jobs.test.ts in apps/linger-installer myself: 2 files, 36 tests, all passed.

Automated review by Claude Code


Automated review of 574f87b by Claude Code.

NightRain233
NightRain233 previously approved these changes Nov 19, 2026
@NightRain233

Copy link
Copy Markdown
Contributor

Thanks for the fix and thorough tests! Everything looks good to merge, except there are now merge conflicts with main. Could you please rebase onto the latest main and resolve the conflicts (and squash into a single commit if convenient)? Once CI passes, we'll get this merged.

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve (0 P0, 0 P1, 0 P2, 0 P3)

Scope: follow-up on 574f87b. The head under review is the same commit, 574f87ba7b3f094e31a46a84dbe76cae8eeedd6e. Base main, merge base 8d02fe8c, 3 commits, 3 files (apps/linger-installer/AGENTS.md, src/server/jobs.ts, src/server/jobs-start-cancel.test.ts). Loaded coderbook only; ponytail-review and karpathy-guidelines weren't loaded because no code changed. Ran in a separate session.

Previous findings

The previous review of 574f87b had no findings, so there is nothing to track.

Findings

None. git diff 574f87b..HEAD is empty, so no new code has arrived since the previous review.

Verified as fine

  • No code changed since the previously approved review.
  • No CJK characters in added lines or in any commit message.
  • No merge commits and no fixup or "address review" commits in 8d02fe8c..HEAD.
  • All three commits link to the GitHub account Afloat16.

Process notes

  • Rebase needed. GitHub reports the merge state as DIRTY. git merge-tree against origin/main shows a content conflict in apps/linger-installer/AGENTS.md, caused by c6679e0c fix(installer): cancel pending native sign-in starts on main, which also adds lines to that file. Rebase onto main, keep both sets of invariant bullets, and re-run make check-installer.

CI

All required checks pass: check (installer), check (web), changes, repository-gate. The other checks were skipped by path selection. I ran no tests locally.

Automated review by Claude Code


Automated review of 574f87b by Claude Code.

@Afloat16
Afloat16 force-pushed the fix/cancel-queued-installer-jobs-20261119 branch from 574f87b to ecca61c Compare November 19, 2026 12:45

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve (0 findings)

Scope: follow-up on 574f87b. Base main, merge base 13cb493d (current origin/main tip, so the branch is up to date). 1 commit and 3 files reviewed (apps/linger-installer/AGENTS.md, src/server/jobs.ts, src/server/jobs-start-cancel.test.ts). Helper skills loaded: coderbook, ponytail-review, karpathy-guidelines. Ran in a separate session.

Previous findings

The previous review (574f87b) had no findings. Nothing to carry over.

Findings

None.

Verified as fine

  • Since the last review, the PR's three commits were squashed into ecca61c and rebased onto 13cb493d. 574f87b is no longer an ancestor, so a direct git diff 574f87b..HEAD mostly shows unrelated main changes. I compared the PR's own diff against each merge base instead (8d02fe8..574f87b vs 13cb493..ecca61c, limited to apps/linger-installer). They match exactly. The only difference is AGENTS.md hunk context: the new paragraph now comes after the runtime-login inspection paragraph that main added in the meantime, and both paragraphs are kept.
  • jobs.ts doesn't touch the newly merged runtime-login code (runtime-login.ts). The rebase introduces no interaction or conflict.
  • The earlier analysis still holds:
    • starting makes the job count as busy and blocks execute when cancelRequested is set.
    • appendEvent doesn't publish a snapshot before the private input is durable.
    • cancel waits for starting and then persists the final state.
    • shutdown cancels and drains both starting and executing jobs. It swallows only an initial-save error for a record that start() already removed, and it rethrows persistence failures for retained jobs.
    • wait waits for both starting and task.
  • Commit subject follows the conventional format. No CJK in the added lines or in the commit message.

Process notes

  • The fixup commits from the previous round are now squashed into one commit, rebased on the current main. Nothing to act on.

CI

check (installer), check (web), changes and repository-gate pass. The other checks were skipped because their paths are unaffected. I re-ran no tests, since the installer code is unchanged from the previously reviewed head and CI's installer check covers the test file.

Automated review by Claude Code


Automated review of ecca61c by Claude Code.

@oldwinter53 oldwinter53 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for rebasing and squashing cleanly onto main! Verified that tracking record.starting, guarding busy/initial persistence cancellation, and draining pending writes in shutdown() close the startup persistence races cleanly with green CI. Adding to the merge queue.

@oldwinter53
oldwinter53 added this pull request to the merge queue Nov 19, 2026

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve (0 findings)

Scope: follow-up on ecca61c. The reviewed head is the same commit, so nothing changed since the previous review. Base main, merge base 13cb493d, 1 commit, 3 files (apps/linger-installer/AGENTS.md, src/server/jobs.ts, src/server/jobs-start-cancel.test.ts). Loaded ponytail-review. I did not load coderbook or karpathy-guidelines because there was no new diff to apply them to. The review ran in a separate session.

Previous findings

The previous review of ecca61c approved the PR with no findings, so there is nothing to track.

Findings

None.

Verified as fine

  • No code changed since the previous review: the head SHA is ecca61c4150b2795e4d1f574953992aaf4c3bfa1 in both reviews.
  • I re-read the jobs.ts diff:
    • starting is set before the first await, so busy, cancel, shutdown and wait all see a job whose initial save is still in progress.
    • finally clears starting.
    • A cancel during the initial save skips execute().
    • cancel() waits for starting before the final persist, so the canceled state is saved after the input file exists.
    • shutdown() ignores the initial-write error that start() already reports to its caller, and still throws cancel failures for jobs it keeps.
  • Event persistence is held back while starting is set, so a failed input write leaves no orphan public job.
  • The installer AGENTS.md documents the new invariant.
  • Added lines and the commit message contain no CJK characters.
  • There are no merge or fixup commits.

Process notes

  • The branch is 1 commit behind main and needs a rebase before merge. The new main commit is ad63af00, which touches packages/local-installation/src/private-files.ts. git merge-tree shows it merges cleanly.

CI

Passing: check (installer), check (web), changes and repository-gate. The other app checks were skipped because their paths are unaffected. I ran no tests locally because nothing changed since the previous review.

Automated review by Claude Code


Automated review of ecca61c by Claude Code.

Merged via the queue into simplexai-labs:main with commit 2ef4c75 Nov 19, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants