Repository navigation
feat(local-models): seamless Agent switching and automatic Pi setup - #250
oldwinter53 wants to merge 3 commits into
Conversation
Align PI_CODING_AGENT_DIR across OverlayEnvironment and workspace.PiSourceEnvironment so localmodels registers loaded models into the exact profile the daemon discovers and runs. Share HarnessRoot and Tool.runBuffered across library, source, and daemon startup, and asynchronously run localmodels harness status/plan/apply (--node with exponential backoff outside refreshMu) when a local model is loaded without Pi.
Show live Bifrost (Pi) Gateway readiness above Endpoint, add one-click Use with <Another Me> and Open chat actions alongside Used by Agent switching and Make room automatic Agent handoff, link Pi runtime setup to Settings > Models, and resolve localmodels-library IDs to friendly labels via useResolvedAgentModelLabel before Pi lists them.
There was a problem hiding this comment.
Request changes (1 P1, 3 P2, 2 P3)
Scope: base main, merge base 6bc19cff; 2 commits, 23 files reviewed at 73727f15. Loaded skills: coderbook (sections 2, 3, 5), ponytail-review (sections 1–3), karpathy-guidelines (sections 2–3). Reviewed in a separate session.
Findings
P1
1. The new daemon test waits for a short-lived state and turns check (computer-go) red (apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go:274)
TestEnsurePiRunsOutsideRefreshLockAndBacksOffOnFailure polls record.last() every 10 ms for a downloading snapshot. The scripted download publishes downloading and replaces it with the final idle snapshot about 40 ms later (DelayMS: 40, then StatusAfter). On a loaded runner the poll can miss that window. After that, last() never shows downloading again, so the test times out. CI failed exactly this way: timed out waiting for phaseStarted published downloading snapshot while EnsurePi waits, after the fake plan gate gave up at its own 10 s limit. Locally it passed 3/3 with -count=3, and the five new Pi tests passed 3/3 under -race, so the failure comes from the test's timing, not from the library. Still, it blocks a required check.
Fix: search record.snapshots for any downloading state of otherModel instead of reading last(), or hold the download open with a gate file the way the plan step does.
P2
2. Provisioning Pi gets 5 minutes in total, a quarter of what the installer gives apply alone (apps/computer/local-daemon/internal/localmodels/library.go:29)
scheduleEnsurePi runs harness status, plan and apply under one context.WithTimeout(l.ctx, 5*time.Minute). packages/local-installation/src/managed-runtimes.ts gives harness plan 2 minutes and harness apply 20 minutes. On a slow link the daemon kills apply partway through. Its own comment there says the kit discards its stage on failure, so the next attempt starts the download from scratch. Retries come every 30 s to 5 min, so a slow Computer keeps re-downloading Pi and never finishes.
Fix: match the installer's budgets, for example 2 minutes for plan and 20 for apply, or one total of about 20 minutes.
3. "Bifrost · Setting up Pi…" is a guess and can stay up forever (apps/web/src/features/local-models/local-model-details.tsx:122)
gatewayFactValue shows "Setting up Pi…" whenever the Computer is online and has no ready Pi runtime row. The daemon only provisions Pi when Pi is enabled, the localmodels tool is installed, and Pi was missing from the startup discovery (main.go:325-330, scheduleEnsurePi). Discovery drops disabled runtimes (internal/provider/discovery.go:122). So on a Computer whose EnabledRuntimes leaves out pi, the row reads "Setting up Pi…" forever. The same happens when installs keep failing: the failure only reaches the daemon log (local_models.pi_install_failed). The row also offers "Use with …" in that state. Fix: show "Setting up" only when a structured signal says provisioning is actually running, or at least show "Pi not installed" when the Computer reports no pi runtime at all, and keep "Setting up" for a reported not_installed row.
4. usedByBadge is now dead in production, and its logic is copied inline (apps/web/src/features/local-models/local-model-row.tsx:387)
The row now computes isModelOnComputer(...) && usedNames.length ? "1 Agent" / "N Agents" itself. That duplicates usedByBadge in local-models-rules.ts:378, which only local-models-rules.test.ts still calls. Fix: change usedByBadge to take the merged names (or a count) and call it from the row, so one rule and its tests stay in use.
P3
5. Fallbacks for rendering outside a router exist only for tests (apps/web/src/features/rooms/members/edit-agent-runtime-dialog.tsx:347, apps/web/src/features/local-models/local-model-row.tsx:177)
Both components render inside the app router in production. The fallback <a> in LocalModelsLink calls preventDefault() and only closes the dialog, so it would never navigate if this branch were ever reached. Fix: wrap the affected tests in MemoryRouter and drop both useInRouterContext branches.
6. "Make room" automatic handoff silently skips busy Agents (apps/web/src/features/local-models/local-models-page.tsx:713)
The checkbox says "Switch A, B to once loaded". If B is mid-turn when the load completes, the !agent.busy filter drops B with no message, and the pending switch is cleared. Fix: tell the user which Agents were not switched, or don't name busy Agents in the checkbox.
Verified as fine
scheduleEnsurePiandClose:closedis checked andpiWg.Addhappens undermubeforeWait, so nothing is added afterClosestarts waiting. Provisioning runs outsiderefreshMu, and Close cancels the context, which kills the running harness command. The new Pi tests pass under-race.- The
localModelsclosure inmain.gois assigned before the runtime monitor goroutine starts, socurrentDiscoveryOptionshas no data race.ResolveDiscoveryre-readsPiExecutable(), andproviderCatalog.Replacemakes a Pi installed later usable for turns. Tool.runBufferedkeeps the earlier temp-file (non-pipe) behavior for library, model-source and harness calls.HarnessRootreplaces the two hand-built paths without changing them.commandArgssettingPI_CODING_AGENT_DIRthroughOverlayEnvironmentandPiSourceEnvironmentmatches the environment the Pi runtime itself gets (currentProviderEnvironment).TestToolCommandAlignsPiCodingAgentDircovers it.- The harness plan is checked (schema, runtime, 64-hex ID) before
apply --approve, and runtime rows with anargvPrefixare rejected, as in the installer. - Web: the runtime PATCH sends
expected_configuration_revision, and reasoning effort is limited to the catalog's allowed values. Agent switching is limited to the viewer's own Agents with a valid workspace grant and tolocalsource mode. Switching is keyed on structured fields (pi_model,agent_type,mode), not display text. The dialog's query key now uses the sharedlocalComputersQueryKey. - Docs:
apps/computer/local-daemon/README.mdandapps/web/AGENTS.mdare updated. No contract, migration or environment variable changes. The PR body's claims (HarnessRoot/runBuffered extraction, startupInspectPiprobe dropped, the 30 s→5 min backoff, the hook overloads) match the diff.
Process notes
- The branch is 1 commit behind
main(63a918c4);mainrequires up-to-date branches, so it needs a rebase. - 2 commits, no merge or fixup commits.
CI
check (computer-go): failed onTestEnsurePiRunsOutsideRefreshLockAndBacksOffOnFailure(finding 1).- All other checks passed:
check (desktop), web tests 1–3,web-static,check (web),repository-gate, CodeQL. Contracts, installer, packages and TeamHub checks were skipped by path filter. - Run locally: the failing test passed 3/3 (
-count=3), and the five new Pi tests ininternal/localmodelspassed 3/3 under-race. CI'scomputer-gojob runs without-race.@linger/computerTS tests were not affected and not run.
Automated review by Claude Code
Automated review of 73727f1 by Claude Code.
There was a problem hiding this comment.
Approve with nits (0 new findings; 5 earlier P2/P3 findings still open)
Scope: follow-up review of 73727f1. Base is main, merge base 6bc19cf. The PR has 3 commits; the only new one since the last review is 1e4872e, which touches 2 test files. I did not load the helper skills (coderbook, ponytail-review, karpathy-guidelines) because this delta only changes tests. The review ran in a separate session.
Previous findings (follow-up on 73727f1)
- P1 – flaky
TestEnsurePiRunsOutsideRefreshLockAndBacksOffOnFailure(pi_runtime_test.go:274): Resolved. The download script now holds its result line behind adownload-waitgate file (fake_tool_test.goWaitFile/waitForGateRemoval). Thedownloadingsnapshot therefore stays put until the test deletes the gate; the test no longer races a 40 ms window. The test also checks thatPiExecutable()is still empty while the plan gate is held, then waits for the download to finish asidleonce the gate is released.check (computer-go)is green. - P2 – one 5-minute budget for the whole Pi install (
library.go:29): Not resolved. This file has not changed since73727f1. - P2 – "Setting up Pi…" can stay up forever (
local-model-details.tsx:122): Not resolved. No change. - P2 –
usedByBadgeis now dead code and has a second copy (local-model-row.tsx:387,rules.ts:378): Not resolved. No change. - P3 – fallback branch for rendering without a Router exists only for tests (
edit-agent-runtime-dialog.tsx:347): Not resolved. No change. - P3 – auto-switch after making room skips busy Agents without telling the user (
local-models-page.tsx:713): Not resolved. No change.
Findings
None in the new changes.
Verified as fine
waitForGateRemovalreplaces the inline polling loop in the fakeharness plangate, so the two gates share one helper instead of keeping a copy. The gate timeout went from 10s to 30s. That still fits insidewaitFor's 20s budget plus cleanup, and it only matters if a test fails partway.- The
WaitFilepath resolves under the per-teststatedirectory, so parallel tests cannot collide on it. - The new assertion that
PiExecutable() == ""while the plan gate is held is deterministic:harness plancannot return until the test removesharness-plan-wait.
Process notes
1e4872e test(computer): synchronize EnsurePi lock test…only fixes a test that6f54b317added in this same PR.maintakes rebase merges, so squash it into6f54b317(git rebase -i --autosquash).mergeStateStatusisBLOCKED. Ifmainhas moved, rebase to satisfy the up-to-date branch rule.- No merge commits.
CI
All required checks pass: check (computer-go), check (desktop), check (web), web tests (1/3–3/3), web-static, repository-gate and CodeQL. Contracts, installer, packages, teamhub and teamhub-cli were skipped because nothing in them changed. CI runs the daemon's Go tests without -race, so I ran go test -race -count=3 -p 4 -run 'TestEnsurePi' ./internal/localmodels myself, and it passed (45.8s).
Automated review by Claude Code
Automated review of 1e4872e by Claude Code.
|
Superseded by #251 (rebased cleanly onto latest main with all review feedback addressed). |
Summary
Supersedes
#245(rebased cleanly onto latestmainat6bc19cffalongside PR#242) and addresses everylinger-reviewnote:OverlayEnvironmentdirectly withworkspace.PiSourceEnvironmentsolocalmodelsuses the exactPI_CODING_AGENT_DIRprofile discovered by the daemon (apps/computer/local-daemon/internal/localmodels/tool.go).HarnessRootandTool.runBufferedacrosslibrary.go,source.go,pi_runtime.go, andmain.go.InspectPiprobe and simplified exponential backoff cleanly.useResolvedAgentModelLabelso direct Agent model switch callers need no redundant fallback.flowchart LR Load["localmodels load / Refresh"] --> Dir["Align PI_CODING_AGENT_DIR<br/>(OverlayEnvironment / PiSourceEnvironment)"] Load --> Bg["Background EnsurePi<br/>(harness status/plan/apply --node,<br/>exponential backoff on failure)"] Bg -->|pi_ready| Discover["RuntimeChanged -> Discover<br/>(withManagedPiPath)"] Discover --> UI["Settings > Models<br/>Gateway row + Use with Another Me<br/>Open chat + Switch Agent..."] UI -->|PATCH agents runtime| Room["Room Composer & Runtime Dialog<br/>useResolvedAgentModelLabel fallback<br/>+ Settings > Models link"]internal/localmodels,linger-computer-daemon):PI_CODING_AGENT_DIRinTool.commandArgswithOverlayEnvironment(localmode) andworkspace.PiSourceEnvironmentsolocalmodelswrites loaded models to the same profile the Daemon probes and runs.Pi,Library.scheduleEnsurePiasynchronously runslocalmodels harness status/plan/apply(passing bundled--nodewhen present and backing off from30sup to5mon failure, outsiderefreshMu, awaited onClose()), then notifiesRuntimeChangedsoPicomes online with the loaded model.user message -> provider_readycritical path: adds no network round trip or file walk while waiting on a turn; provisioning runs only in the background afterlocalmodelslibrary snapshots show a loaded model.Settings > Models, Room Composer & Runtime Dialog):Pireadiness in the expandedGatewayrow (Ready · Bifrost (Pi),Bifrost · Registering with Pi…,Bifrost · Setting up Pi…,Bifrost · Pi not installed,Bifrost · Pi unavailable).Use with <Another Me>(or sole Sponsor-owned Agent) +Open chatshortcut on loaded model rows alongsideUse for Agents, multi-AgentSwitch Agent…inUsed by(disabling working Agents and omitting incompatible reasoning efforts), and checked-by-default automatic Agent handoff inMake room to load.localmodels-library/...names fromlocalModelsQueryOptionsinuseResolvedAgentModelLabel(DirectAgentModelSwitchandActorProfileContent) beforePilists them, keepAgentInfoContentruntimeDetailshidden per Town room-surface invariants, and linkPiinEditAgentRuntimeDialogtoSettings > Models.Evidence
Piuninstalled or misaligned onPI_CODING_AGENT_DIR, showed noBifrostreadiness or Agent-switching controls on the model card, forced manual runtime setup inComputers+ Agent settings, and displayed rawlocalmodels-library/...IDs in Room headers untilPirefreshed.ws/20261118-local-models-seamless-agent-ux-r2(validated at71b42436, then cleanly rebased with zero file overlap onto6bc19cffand re-verified oncheck-repository-gate):make -C apps/teamhub lint-go-daemon BASE=origin/main—0 issuesgo test -race ./internal/localmodels ./cmd/linger-computer-daemon— passed (86.490sand4.242s)pnpm --filter @linger/web test src/features/local-models/local-model-agents.test.ts src/features/local-models/local-models-page.test.tsx src/features/rooms/chat/direct-agent-model-switch.test.tsx src/features/rooms/members/edit-agent-runtime-dialog.test.tsx— passed (4 files,86 tests)make check BASE=origin/main(check-repository,check-docs,check-desktop,check-web368 files/3374 tests,check-node-services,check-computer) — passedpnpm --filter @linger/web run build:assets— passed (547.62 kBminified /185.44 kBgzip /161.50 kBBrotli)make check-repository-gate BASE=origin/main(commitlint, env inventory, Prettier/ESLint/English-only checks, fast script tests, doc links) — passed on6bc19cff(142 passed | 3 skipped,Checked Markdown links in 99 files: all resolve)#157and#220insimplexai-labs/linger; deliberate skip (neither relates to Local models, automatic Pi setup, or Agent switching).Merge Danger
Door: Two-way
Revert removes the additive UI controls and background harness provisioning cleanly with no schema, OpenAPI contract, or database migration changes.
Blast Radius: Low
Scoped to
apps/computer/local-daemonlocalmodelssupervision andapps/webSettings > Modelsand Agent runtime selection components.