Skip to content

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

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

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

Conversation

@oldwinter53

Copy link
Copy Markdown

Summary

Supersedes #245 (rebased cleanly onto latest main at 6bc19cff alongside PR #242) and addresses every linger-review note:

  • Aligned OverlayEnvironment directly with workspace.PiSourceEnvironment so localmodels uses the exact PI_CODING_AGENT_DIR profile discovered by the daemon (apps/computer/local-daemon/internal/localmodels/tool.go).
  • Extracted shared HarnessRoot and Tool.runBuffered across library.go, source.go, pi_runtime.go, and main.go.
  • Dropped the redundant synchronous startup InspectPi probe and simplified exponential backoff cleanly.
  • Added function overloads on useResolvedAgentModelLabel so 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"]
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 alongside Use for Agents, 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 ws/20261118-local-models-seamless-agent-ux-r2 (validated at 71b42436, then cleanly rebased with zero file overlap onto 6bc19cff and re-verified on check-repository-gate):
    • make -C apps/teamhub lint-go-daemon BASE=origin/main — 0 issues
    • go test -race ./internal/localmodels ./cmd/linger-computer-daemon — passed (86.490s and 4.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-web 368 files / 3374 tests, check-node-services, check-computer) — passed
    • pnpm --filter @linger/web run build:assets — passed (547.62 kB minified / 185.44 kB gzip / 161.50 kB Brotli)
    • make check-repository-gate BASE=origin/main (commitlint, env inventory, Prettier/ESLint/English-only checks, fast script tests, doc links) — passed on 6bc19cff (142 passed | 3 skipped, Checked Markdown links in 99 files: all resolve)
  • 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.

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.

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

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

  • scheduleEnsurePi and Close: closed is checked and piWg.Add happens under mu before Wait, so nothing is added after Close starts waiting. Provisioning runs outside refreshMu, and Close cancels the context, which kills the running harness command. The new Pi tests pass under -race.
  • The localModels closure in main.go is assigned before the runtime monitor goroutine starts, so currentDiscoveryOptions has no data race. ResolveDiscovery re-reads PiExecutable(), and providerCatalog.Replace makes a Pi installed later usable for turns.
  • Tool.runBuffered keeps the earlier temp-file (non-pipe) behavior for library, model-source and harness calls. HarnessRoot replaces the two hand-built paths without changing them.
  • commandArgs setting PI_CODING_AGENT_DIR through OverlayEnvironment and PiSourceEnvironment matches the environment the Pi runtime itself gets (currentProviderEnvironment). TestToolCommandAlignsPiCodingAgentDir covers it.
  • The harness plan is checked (schema, runtime, 64-hex ID) before apply --approve, and runtime rows with an argvPrefix are 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 to local source mode. Switching is keyed on structured fields (pi_model, agent_type, mode), not display text. The dialog's query key now uses the shared localComputersQueryKey.
  • Docs: apps/computer/local-daemon/README.md and apps/web/AGENTS.md are updated. No contract, migration or environment variable changes. The PR body's claims (HarnessRoot/runBuffered extraction, startup InspectPi probe dropped, the 30 s→5 min backoff, the hook overloads) match the diff.

Process notes

  • The branch is 1 commit behind main (63a918c4); main requires up-to-date branches, so it needs a rebase.
  • 2 commits, no merge or fixup commits.

CI

  • check (computer-go): failed on TestEnsurePiRunsOutsideRefreshLockAndBacksOffOnFailure (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 in internal/localmodels passed 3/3 under -race. CI's computer-go job runs without -race. @linger/computer TS tests were not affected and not run.

Automated review by Claude Code


Automated review of 73727f1 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 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 a download-wait gate file (fake_tool_test.go WaitFile / waitForGateRemoval). The downloading snapshot therefore stays put until the test deletes the gate; the test no longer races a 40 ms window. The test also checks that PiExecutable() is still empty while the plan gate is held, then waits for the download to finish as idle once 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 since 73727f1.
  • P2 – "Setting up Pi…" can stay up forever (local-model-details.tsx:122): Not resolved. No change.
  • P2 – usedByBadge is 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

  • waitForGateRemoval replaces the inline polling loop in the fake harness plan gate, so the two gates share one helper instead of keeping a copy. The gate timeout went from 10s to 30s. That still fits inside waitFor's 20s budget plus cleanup, and it only matters if a test fails partway.
  • The WaitFile path resolves under the per-test state directory, so parallel tests cannot collide on it.
  • The new assertion that PiExecutable() == "" while the plan gate is held is deterministic: harness plan cannot return until the test removes harness-plan-wait.

Process notes

  • 1e4872e test(computer): synchronize EnsurePi lock test… only fixes a test that 6f54b317 added in this same PR. main takes rebase merges, so squash it into 6f54b317 (git rebase -i --autosquash).
  • mergeStateStatus is BLOCKED. If main has 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.

@oldwinter53
oldwinter53 marked this pull request as draft November 19, 2026 04:00
@oldwinter53

Copy link
Copy Markdown
Author

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

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