Skip to content

feat(local-models): seamless Agent switching and automatic Pi setup - #245

Closed
oldwinter53 wants to merge 2 commits into
mainfrom
ws/20261118-local-models-seamless-agent-ux
Closed

oldwinter53 wants to merge 2 commits into
mainfrom
ws/20261118-local-models-seamless-agent-ux

Conversation

@oldwinter53

Copy link
Copy Markdown

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"]
Loading
  • Computer Daemon (internal/localmodels, linger-computer-daemon):
    • Align PI_CODING_AGENT_DIR in Tool.commandArgs with OverlayEnvironment (local mode) and workspace.PiSourceEnvironment so localmodels writes loaded models to the same profile the Daemon probes and runs.
    • When a local model is loaded on a Computer without Pi, Library.scheduleEnsurePi asynchronously runs localmodels harness status/plan/apply (passing bundled --node when present and backing off from 30s up to 5m on failure, outside refreshMu, awaited on Close()), then notifies RuntimeChanged so Pi comes online with the loaded model.
    • user message -> provider_ready critical path: adds no network round trip or file walk while waiting on a turn; provisioning runs only in the background after localmodels library snapshots show a loaded model.
  • Web (Settings > Models, Room Composer & Runtime Dialog):
    • Show live Bifrost / Pi readiness in the expanded Gateway row (Ready · Bifrost (Pi), Bifrost · Registering with Pi…, Bifrost · Setting up Pi…, Bifrost · Pi not installed, Bifrost · Pi unavailable).
    • Add one-click Use with <Another Me> (or sole Sponsor-owned Agent) + Open chat shortcut on loaded model rows, multi-Agent Switch Agent… in Used by (disabling working Agents and omitting incompatible reasoning efforts), and checked-by-default automatic Agent handoff in Make room to load.
    • Resolve localmodels-library/... names from localModelsQueryOptions in useResolvedAgentModelLabel (DirectAgentModelSwitch and ActorProfileContent) before Pi lists them, keep AgentInfoContent runtimeDetails hidden per Town room-surface invariants, and link Pi in EditAgentRuntimeDialog to Settings > Models.

Evidence

  • Before: Loading a local model on a Codex/Claude-only Computer left Pi uninstalled or misaligned on PI_CODING_AGENT_DIR, showed no Bifrost readiness or Agent-switching controls on the model card, forced manual runtime setup in Computers + Agent settings, and displayed raw localmodels-library/... IDs in Room headers until Pi refreshed.
  • After: Passed locally on clean commits rebased onto origin/main (679bb2a7):
    • go test -race ./internal/localmodels ./cmd/linger-computer-daemon — passed (85.693s and 7.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) — passed
    • pnpm --filter @linger/web run build:assets — passed (547.45 kB minified / 185.40 kB gzip / 161.53 kB Brotli)
    • make check-repository-gate BASE=origin/main (commitlint, env inventory, Prettier/ESLint/English-only checks, fast script tests, doc links) — passed
  • Issues: Checked open issues #157 and #220 in simplexai-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-daemon localmodels supervision and apps/web Settings > Models and Agent runtime selection components.

…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.

@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 (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 / Close ordering: closed and piWg.Add both happen under mu. Close sets closed before Wait, and the provisioning context comes from l.ctx, so Close cancels a running install and waits for it. EnsurePi runs outside refreshMu as the PR states.
  • withManagedPiPath never overwrites a configured Pi path and does not modify the caller's map.
  • Harness status/plan parsing matches the installer's TS contract (schema, argvPrefix must be empty, 64-hex plan id, --approve). The plan temp directory is removed on every path.
  • The user message → provider_ready path is untouched. Provisioning runs only in the background after a Refresh with loaded models.
  • Web: assignableLocalModelAgents keeps the existing grant when it is still present, filters to Sponsor-owned Agents, and buildLocalModelRuntimeUpdate drops reasoning efforts the target model doesn't allow. The localmodels-library/ prefix check is on a provider ID, not on natural-language text. The edit dialog's query key change to localComputersQueryKey unifies a cache key that was previously split. apps/web/AGENTS.md and the daemon README are updated.
  • Tests use waitFor polling, 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.

@oldwinter53

Copy link
Copy Markdown
Author

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.

@oldwinter53

Copy link
Copy Markdown
Author

Superseded by #250 (rebased cleanly onto latest main with all review feedback addressed).

@oldwinter53
oldwinter53 deleted the ws/20261118-local-models-seamless-agent-ux branch November 19, 2026 06:04
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.

2 participants