Repository navigation
feat(local-models): seamless Agent switching and automatic Pi setup - #245
oldwinter53 wants to merge 2 commits into
Conversation
…l load Align PI_CODING_AGENT_DIR across localmodels library calls, model-source local overlays, and workspace.PiSourceEnvironment, and provision the pinned Pi runtime asynchronously under the shared harness root with bundled --node and exponential failure backoff when a local model is loaded without Pi.
…ness Add one-click Use with Another Me, Open chat, Used by Agent switching, live Bifrost (Pi) Gateway readiness, and Make room automatic Agent handoff in Settings > Models, resolve localmodels-library labels before Pi lists them in Room controls, and link Pi in EditAgentRuntimeDialog to Settings > Models.
There was a problem hiding this comment.
Approve with nits (3 P2, 1 P3)
Scope: base main, merge base 679bb2a7, 2 commits, 22 files reviewed at d5a2a8cf. Helper skills loaded: coderbook, ponytail-review, karpathy-guidelines. Ran in a separate session.
Findings
P2
1. PI_CODING_AGENT_DIR precedence in commandArgs is the opposite of OverlayEnvironment's (apps/computer/local-daemon/internal/localmodels/tool.go:83-103)
commandArgs lets a PI_CODING_AGENT_DIR from the base environment win, and consults the overlay only when none is set. OverlayEnvironment (source.go:140-176), which builds the environment the Daemon actually runs Pi with, works the other way: in local mode the gateway-home Pi profile (or overlay.Environment) replaces the base value, and stale gateway-home/byok-home paths are removed.
Failure: The Daemon's environment sets PI_CODING_AGENT_DIR=/x, and the model source is local with gateway-home/.pi/agent present. Pi runs with the gateway-home profile, but localmodels writes loaded models to /x, so the model never appears in Pi. That is the misalignment this PR sets out to fix. The same happens when the base value is a stale gateway-home path, which OverlayEnvironment would drop.
Fix (deletes code): compute effective := OverlayEnvironment(environment, t.StateDirectory) and pass it through workspace.PiSourceEnvironment. Then take PI_CODING_AGENT_DIR from the result. This removes the explicitPiDir branch and the overlay.Mode == "local" check, and uses the same rule as the Daemon by construction. Add a test case with both an explicit value and a local overlay.
2. runHarness is a third copy of the tool-process runner (apps/computer/local-daemon/internal/localmodels/pi_runtime.go:135-170)
ModelSource.runTool (source.go:659-…) already does the same work: temp-file stdout, process.Prepare, wait, kill on context cancel, and bounded reads. It also copies the environment-sanitizing block that Tool.command had. Because runTool builds its own environment instead of calling commandArgs, the new PI_CODING_AGENT_DIR alignment reaches library and harness calls but not source status/apply. The commit body says model-source local overlays are aligned too.
Fix: make one package-level runner built on t.commandArgs (with an optional stdin) and use it in both ModelSource and EnsurePi/InspectPi.
3. Harness-root derivation copied a third time (apps/computer/local-daemon/cmd/linger-computer-daemon/main.go:149-152)
filepath.Clean(filepath.Join(StateDirectory, "..", "..", "harness")) already exists twice in source.go (:196 in OverlayRuntimePaths, :255 in NewModelSource), and it must match the installer's home/runtime/harness (packages/local-installation/src/managed-runtimes.ts:41). The computer.StateDirectory/local-models/harness fallback is never used in practice: when localModelsTool is nil, both InspectPi and the Library skip provisioning.
Fix: add one localmodels.HarnessRoot(stateDir string) string, use it in all three places, and drop the fallback.
P3
4. Synchronous Python probe on every Daemon start (main.go:154-163, :343)
When the local-models tool is installed and no Pi path is configured, each Daemon start blocks for up to 20s on a python -m localmodels harness status run before the first discovery. EnsurePi already starts with InspectPi, so the background path finds an existing managed Pi without this probe. Consider dropping the startup probe, or moving it into the Library's first background run. Separately, the || managedPi != "" in main.go:343 changes nothing: once InitialPiExecutable is set, scheduleEnsurePi returns early. Two smaller simplifications: the backoff loop in library.go:226-233 can be min(interval<<(failures-1), max), and the ?? registration.runtime.model in direct-agent-model-switch.tsx is redundant because useResolvedAgentModelLabel already falls back to runtime.model.
Verified as fine
scheduleEnsurePi/Closeordering:closedandpiWg.Addboth happen undermu.ClosesetsclosedbeforeWait, and the provisioning context comes froml.ctx, soClosecancels a running install and waits for it.EnsurePiruns outsiderefreshMuas the PR states.withManagedPiPathnever overwrites a configured Pi path and does not modify the caller's map.- Harness status/plan parsing matches the installer's TS contract (
schema,argvPrefixmust be empty, 64-hex plan id,--approve). The plan temp directory is removed on every path. - The user message →
provider_readypath is untouched. Provisioning runs only in the background after a Refresh with loaded models. - Web:
assignableLocalModelAgentskeeps the existing grant when it is still present, filters to Sponsor-owned Agents, andbuildLocalModelRuntimeUpdatedrops reasoning efforts the target model doesn't allow. Thelocalmodels-library/prefix check is on a provider ID, not on natural-language text. The edit dialog's query key change tolocalComputersQueryKeyunifies a cache key that was previously split.apps/web/AGENTS.mdand the daemon README are updated. - Tests use
waitForpolling, not fixed sleeps.
Process notes
- The branch is up to date with
main(0 commits behind). No merge commits or fixup commits.
CI
All checks pass (contracts, installer, packages, teamhub, teamhub-cli skipped as not touched). CI's check (computer-go) runs without -race, so I ran go test -race -count=1 for the new pi_runtime_test.go tests in ./internal/localmodels and TestWithManagedPiPath in ./cmd/linger-computer-daemon. Both passed (29.5s / 1.1s). I did not run the full make check-computer process tests.
Automated review by Claude Code
Automated review of d5a2a8c by Claude Code.
|
Closing before the 0.1.4 release (decided by the author). Reasons: it re-does part of #240 (same EnsurePi, PI_CODING_AGENT_DIR alignment, Use with Agent / Switch Agent / Make room handoff) and conflicts with #242, which already added a Use for Agents button on loaded rows (which entry point to keep is a product decision); it self-approves and applies a Pi harness plan in the installer-managed root in the background, can race with desktop resume recovery, treats every harness failure as an exit code (RUNTIME_EXTENSIONS_FAILED, plan refused and pi-extensions-unavailable from localmodels dev40 are never read) and retries every 30 s up to 5 min for as long as a model is loaded; it needs real-machine verification on Windows and macOS that cannot be done for this release. A smaller follow-up built on the model source work is planned after 0.1.4. |
|
Superseded by #250 (rebased cleanly onto latest main with all review feedback addressed). |
Summary
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, 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.origin/main(679bb2a7):go test -race ./internal/localmodels ./cmd/linger-computer-daemon— passed (85.693sand7.456s)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,81 tests)make check BASE=origin/main(check-repository,check-docs,check-desktop,check-web,check-node-services,check-computer) — passedpnpm --filter @linger/web run build:assets— passed (547.45 kBminified /185.40 kBgzip /161.53 kBBrotli)make check-repository-gate BASE=origin/main(commitlint, env inventory, Prettier/ESLint/English-only checks, fast script tests, doc links) — passed#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.