Repository navigation
fix(local-models): share stop budget and retry failed quit stops on resume - #671
oldwinter53 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Approve with nits (2 P2, 1 P3)
Scope: base main, merge base 8b7fe39b. I reviewed 1 commit and 12 files. The coderbook, ponytail-review and karpathy-guidelines skills were not loaded, but I applied their checklists as the review recipe lists them. The review ran in a separate session at head a91ee4ee.
Findings
P2
1. The 15 s per-step floor lets library steps eat the stack down reserve. packages/local-installation/src/localmodels-tool.ts:1118
stepTimeoutMs returns max(1, min(remaining, max(minStepTimeoutMs, bounded))). The 15 s floor is capped only by remaining, not by remaining - reserveMs. Each wedged library step after the first can therefore cut into the protected tail.
Here is a concrete quit stop: 135 s budget, keepController, so the reserve is 45 s.
library statushangs for its 60 s cap. 75 s remain, 30 s of them above the reserve.- Three loaded models whose
library unloadhangs getmax(15, 30/3), thenmax(15, 15/2), thenmax(15, 0). That is 15 s each. stack downstarts with 30 s, not the 45 s reserve.- With five hung unloads, or a hung cancel plus a hung poll
library status(localmodels-tool.ts:1192-1197), the outerAbortSignal.timeout(QUIT_STOP_BUDGET_MS)fires first. Thenoptions.signal.throwIfAborted()instepthrows, andstack downnever runs.
That is the busy-GPU case the PR is fixing. It also contradicts the PR's claim that a wedged library step can't starve stack down.
Fix: once a non-authoritative step is past the reserve, cap it at remaining - reserveMs. Skip the remaining library steps when that is ≤ 0, instead of flooring them at 15 s. Add a test with three or more hanging unloads that asserts the timeout stack down gets.
2. A persistently failing stop is retried, blocking, on every launch. apps/linger-installer/src/server/resume.ts:80, packages/local-installation/src/local-models.ts:826-858
retryPendingLocalModelsStop clears stop-pending.json only when stopLocalmodels succeeds. resumeSourceInstance awaits it before the warm probe and restore. If a step keeps failing or keeps hanging, the marker stays forever. Examples:
- a model whose
library unloadalways fails; - a present kit that fails verification (
stopLocalmodelsreports that as a stop failure); - a
library statusthat keeps timing out.
Every later --resume then runs a full stop with the default 120 s budget before the app can come up. The quit-time stop re-creates the marker each time, so this never converges. The PR body says failures are logged "without blocking startup". They are not thrown, but startup still waits for the whole retry.
Fix: make the retry one-shot. Clear the marker, or record an attempt count in it, after one launch-time retry whatever its outcome. Alternatively, run it with a much smaller budget. Add a test for a retry that fails twice.
P3
3. The default timeoutMs parameter of step is dead code. packages/local-installation/src/localmodels-tool.ts:1127
All six step(...) calls now pass an explicit timeout, so timeoutMs = stepTimeoutMs(libraryReserveMs) never applies. Make the parameter required.
Smaller simplification in the same spot: Math.max(1, Math.max(minStepTimeoutMs, remaining)) in the authoritative branch could be one Math.max.
Verified as fine
- Marker home matches the stop's home. The marker lives under the lock root (
localModelsLockRoot,local-models.ts:137). Resume derives the home withpath.resolve(instance.root, "../.."), the same derivation the existingrestoreByokcall uses (resume.ts:128). - Marker lifecycle in the quit stop. It is written and cleared inside
withLocalModelsLock(stop.ts:146-150). A launch racing an in-flight quit stop waits onwaitForLocalModelsLock(180 s,local-models.ts:124) and re-checks the marker under the lock (local-models.ts:833), so it does not stop twice. - Hard exit leaves the marker for retry. The quit helper's 150 s
process.exit(1)(main.ts:384) leaves the marker behind, which is the intended retry trigger. - Retry abort handling. The retry rethrows only when the caller's signal aborted (
local-models.ts:848). Otherwise it logs the failure and returns it. The resume signal is the existing long--resumetimeout inmain.ts. - Budget forwarding. The budget passes the remaining wall-clock time to each home (
stop.ts:73-76,124-127).QUIT_STOP_BUDGET_MS(135 s) stays under the 150 s hard exit. - Docs. All three owning
AGENTS.mdfiles are updated. - Language and history. Added lines and the commit message contain no CJK, and there are no merge commits.
Unverified: I did not confirm from the localmodels kit source whether library status and stack down succeed against a stack that is already down. That decides how often finding 2 triggers in practice.
Process notes
None.
CI
All checks that ran pass: check (installer), check (desktop), check (packages), check (web), repository-gate, CodeQL. The others were skipped as out of scope. I ran no tests locally; finding 1 comes from tracing stepTimeoutMs by hand.
Automated review by Claude Code
Automated review of a91ee4e by Claude Code.
a91ee4e to
1207fd7
Compare
There was a problem hiding this comment.
Approve with nits (0 new findings; 3 previous P2/P3 findings still open)
Scope: follow-up on a91ee4e. Base main, merge base eafcfd41. Reviewed 1 commit (1207fd7b) and 12 files. Loaded the helper skills coderbook, ponytail-review and karpathy-guidelines. This review ran in a separate session.
The PR was force-pushed onto a newer main. git range-diff against a91ee4e shows only rebase context changes: an import line (isStoppedDownloadErrorCode) and the upstream unloadableLibraryModels call site. The PR's own logic did not change.
Previous findings
- P2: the 15 s per-step floor can eat into the
stack downreserve. Not resolved.packages/local-installation/src/localmodels-tool.ts:1117still computesMath.max(minStepTimeoutMs, bounded)withremainingas the only cap, notremaining - reserveMs. So several slowlibrary unload/cancelsteps can still each take 15 s out of the 45 sstack downreserve. - P2: a stop that keeps failing makes every launch wait for a retry. Not resolved.
apps/linger-installer/src/server/resume.ts:80still runsretryPendingStopsynchronously on every resume.retryPendingLocalModelsStopleaves the marker in place on failure (packages/local-installation/src/local-models.ts, thereturn { retried: true, failed }branch). A step that always fails therefore costs up to 120 s on every launch. - P3: the
timeoutMsdefault onstephas no caller left. Not resolved.packages/local-installation/src/localmodels-tool.ts:1127still declarestimeoutMs = stepTimeoutMs(libraryReserveMs), but every call site passes an explicit timeout.
Findings
No new findings. I re-checked the whole merge-base diff for P0/P1 problems and found none.
Verified as fine
- Marker location and private writes.
localModelsStopPendingFilelives underlocalModelsLockRoot(home)=<home>/runtime/localmodels(packages/local-installation/src/local-models.ts:138). It is written withprivateDirectory+atomicPrivate. It is written only after theexists(runtime/localmodels)guard, so a home that never ran localmodels gets no folder created (apps/linger-installer/src/server/stop.ts,stopDesktopLocalModels). - Marker lifecycle under the lock. The marker is set and cleared inside
withLocalModelsLockaroundstop(), so a failure or a hard exit at 150 s leaves it in place.stopDesktopInstallationclears it only afterstopModelsresolves. - No double stop when racing an in-flight quit.
retryPendingLocalModelsStopchecks the marker again after taking the lock throughwaitForLocalModelsLock(local-models.ts:749). That function rethrows only non-BUSY errors and gives up atLOCK_WAIT_MS. A quit stop that is still running therefore finishes first, and the retry sees the cleared marker. - Symlinked or foreign marker files are ignored.
hasPendingLocalModelsStopuseslstatand accepts only a regular file owned by the current uid. - Budgets fit the process limits. The quit helper's signal is
AbortSignal.timeout(QUIT_STOP_BUDGET_MS = 135_000), inside its 150 sprocess.exit(apps/linger-installer/src/main.ts:383-387).--stopuses the same 120 s for both signal and budget (main.ts:354-355). Resume runs under a 10-minute signal (main.ts:212), so a retry of up to 120 s fits. stack downalways gets the remaining time. In authoritative mode it getsmax(minStep, remaining), and the outer signal still bounds it.- The retry targets the right home. It uses
path.resolve(instance.root, "../.."), the same homerestoreByokuses in the same function. - No CJK text in added lines or in the commit message. No new env vars, no
contracts/generatededits, no cross-apps/*imports.
Process notes
- The branch is 2 commits behind
main, andmainrequires branches to be up to date, so a rebase is needed. - No merge commits and no fixup commits.
CI
8 checks pass, 7 are skipped, none fail. I ran no tests myself: the change touches no daemon concurrency code that CI skips.
Automated review by Claude Code
Automated review of 1207fd7 by Claude Code.
1207fd7 to
5035828
Compare
|
Rebased onto latest
|
There was a problem hiding this comment.
Request changes (1 P1, 1 P3)
Follow-up on 1207fd7. Scope: base main, merge base 6a511c15, 1 commit (50358288), 12 files. The PR was rebased since the previous review, so I compared the two versions with git range-diff and re-checked the whole merge-base diff. Helper skills: coderbook, ponytail-review and karpathy-guidelines were not loaded in this headless run; I applied the review checklist from the PR-review skill directly. The review ran in a separate session.
Previous findings
The previous review (1207fd7) had no findings. Changes since then: a failed launch-time retry now retires the marker (attempts counter), an aborted retry keeps the marker, and the docs were updated to match.
Findings
P1
apps/linger-installer/src/server/stop.ts:119together withpackages/local-installation/src/localmodels-tool.ts:1444and:1583: during a full--stop(uninstall), the localmodels stage gets only the time left of the 120 s budget after the manager and services stages.stopLocalmodelsthen skips any step whose share would fall inside the reserve, rather than trying it.- Failure scenario: on a busy machine, the PR's own target case, the services stage stops five native services, and each can take up to the 12 s SIGINT grace (
native-process.ts:586), plus the computer stop. If services take ≥ 60 s,remaining - libraryReserveMs (60 s) <= 0, solibrary statusis skipped and nothing is unloaded. If services take ≥ 75 s,foundations stopgetsstepTimeoutMs(45 s)= 0, so it is skipped and recorded aslocalmodels foundations stop: skipped to preserve stop budget. The uninstall then leaves the model controller running and reports a localmodels failure. - Before this PR, each of these commands ran with its own 30 s timeout under the 120 s signal, so a 1–2 s
foundations stopstill ran. This regression is limited tostopDesktopInstallation. The quit path calls onlystopLocalmodels, so its budget starts fresh. - Fix: give the localmodels stage its own budget in
stopDesktopInstallationinstead of the remainder after earlier stages. Alternatively, when the budget above the reserve is spent, run non-final steps with the minimum step timeout (still bounded bysignal) instead of skipping them. Add astop.test.tscase where the services stage is slow.
- Failure scenario: on a busy machine, the PR's own target case, the services stage stops five native services, and each can take up to the 12 s SIGINT grace (
P3
packages/local-installation/src/local-models.ts:849,:890,:957: the retry marker carries more than the design uses.retryPendingLocalModelsStop'sbudgetMsoption is never passed by its only caller (resume.ts:98).attemptswithMAX_PENDING_STOP_RETRY_ATTEMPTS = 1is really a boolean "retired" flag.- The comment about a "legacy" marker refers to a file this PR introduces, so no legacy shape exists.
- Simpler: a
{ retired: boolean }record (or two marker states), nobudgetMsoption, and no legacy wording.
Verified as fine
- Quit marker lifecycle: the marker is written and cleared inside
withLocalModelsLock(stop.ts:172-176), so a SIGKILL from the 150 s hard exit leaves it in place. A resume that races the in-flight quit waits on the same lock and re-reads the marker under it before stopping (local-models.ts:966-969). - Launch retry is bounded. It runs at most once per failure streak, and its timeout is
AbortSignal.any([signal, timeout(120 s)])(local-models.ts:975-978). A failure is logged and returned, not thrown. It rethrows only when the caller's own signal aborted (local-models.ts:987). stack downalways runs, with at leastmin(15 s, budget)and the full remainder (localmodels-tool.ts:1585-1590). The quit budget of 135 s sits inside the 150 s hard exit (main.ts:480-484).- No CJK in added lines or commit messages, and no merge commits.
Process notes
None.
CI
check (installer), check (web), repository-gate, CodeQL and Analyze passed. check (packages) and check (desktop) were still pending at review time. I ran no tests locally; the P1 follows from the budget arithmetic in the code cited above.
Automated review by Claude Code
Automated review of 5035828 by Claude Code.
Summary
Fixes #664.
Root Cause
stopLocalmodels(packages/local-installation/src/localmodels-tool.ts) ran everylocalmodelscommand (library status,library cancel,library unload,foundations stop,stack down) with a hardcodedtimeoutMs: 30_000. On a loaded machine (busy GPU or right after wake), individual steps exceed 30 s even though the detached quit helper already has a 150 s hard exit and resume/daemon lock waits allow 180 s. A timed-outlibrary statusskippedlibrary unload, and a timed-outstack downleftstrata-poly-serve/strata-polyand Bifrost running in GPU memory with no retry until the next quit.Changes
packages/local-installation/src/localmodels-tool.ts,apps/linger-installer/src/server/stop.ts,apps/linger-installer/src/main.ts):stopLocalmodelswith a shared overall budget (QUIT_STOP_BUDGET_MS = 135_000for--stop-local-modelsinside its 150 s hard exit;DEFAULT_STOP_BUDGET_MS = 120_000for--stop), forwarded with remaining wall-clock time per home.stack down(plus 15 s forfoundations stopon full stops), cappedlibrary statusat 60 s, and shared pre-reserve time across multipleloading/loadedmodels so busy-machine commands finish without letting a wedged library step starvestack down.--stop-local-modelsruns detached.packages/local-installation/src/local-models.ts,apps/linger-installer/src/server/stop.ts,apps/linger-installer/src/server/resume.ts):withLocalModelsLock,stopDesktopLocalModelswrites<home>/runtime/localmodels/stop-pending.jsonbefore stopping and removes it only whenstopLocalmodelssucceeds (a full uninstall--stopalso clears it on success).resumeSourceInstancecallsretryPendingLocalModelsStopbefore warm restore or cold start. Normal launches see no marker and return immediately; launches racing an in-flight quit stop wait on the lock and skip retrying once the marker is cleared; failures are logged without blocking startup.AGENTS.mdfiles (packages/local-installation,apps/linger-installer,apps/desktop).stack down/foundations stopreserves, multi-model sharing,stop-pending.jsonlifecycle, in-flight lock coordination, and pre-resume retry ordering.Verification
pnpm --filter @linger/local-installation exec vitest run src/localmodels-tool.test.ts src/local-models.test.ts(all 122 tests passed)pnpm --filter @linger/local-installation typecheck && pnpm --filter @linger/local-installation lint && pnpm --filter @linger/local-installation build(passed)make check-installer(typecheck, eslint, and all 28 test suites passed)pnpm --filter @linger/desktop typecheckandpnpm --filter @linger/linger-installer build(passed)commitlint,pnpm env:inventory:check, changed-file Prettier/ESLint/English-only checks, andmake check-docs(passed)