From 03439cc5d96c84786ef0046e3794cfdd8649fc06 Mon Sep 17 00:00:00 2001 From: Dongdong Chen Date: Thu, 19 Nov 2026 13:45:52 +0800 Subject: [PATCH 1/3] feat(computer): provision Pi automatically for loaded local models 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, match the installer's 2 m status/plan and 20 m apply timeouts, surface RUNTIME_DIAGNOSTIC_FAILED after a failed setup attempt, and run localmodels harness status/plan/apply (--node with exponential backoff outside refreshMu) when a local model is loaded without Pi. --- apps/computer/local-daemon/README.md | 18 +- .../cmd/linger-computer-daemon/main.go | 59 ++- .../cmd/linger-computer-daemon/main_test.go | 43 +++ .../internal/localmodels/fake_tool_test.go | 87 ++++- .../internal/localmodels/library.go | 180 ++++++--- .../internal/localmodels/pi_runtime.go | 183 +++++++++ .../internal/localmodels/pi_runtime_test.go | 362 ++++++++++++++++++ .../internal/localmodels/source.go | 61 +-- .../local-daemon/internal/localmodels/tool.go | 115 +++++- 9 files changed, 973 insertions(+), 135 deletions(-) create mode 100644 apps/computer/local-daemon/internal/localmodels/pi_runtime.go create mode 100644 apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go diff --git a/apps/computer/local-daemon/README.md b/apps/computer/local-daemon/README.md index 63f5e43c5..6f86be651 100644 --- a/apps/computer/local-daemon/README.md +++ b/apps/computer/local-daemon/README.md @@ -159,11 +159,19 @@ model. with that code; the model's next accepted job, or a row that moved on, drops them. They live in memory, so after a Daemon restart the row shows only its code. -- Pi. When the loaded models change, the Daemon asks the Runtime monitor for a - full discovery, so TeamHub sees Pi's new model list. Each model's - `pi_model` (`/`, as an Agent's `runtime_model` stores it) is - passed through as `localmodels` reports it; the endpoint keeps only the - loaded engine's `base_url`. +- Pi. The Daemon sets `PI_CODING_AGENT_DIR` on `localmodels` tool calls + through `OverlayEnvironment` and `workspace.PiSourceEnvironment` so loaded + models land in the profile Pi probes and runs. When a model is loaded on a + Computer with Pi enabled and not yet installed, `internal/localmodels` + installs the pinned Pi runtime asynchronously in the background under + `HarnessRoot` (passing the bundled Node binary via `--node` when available, + matching the installer's 2 m status/plan and 20 m apply budgets, and backing + off from 30 s up to 5 m on failure while surfacing + `RUNTIME_DIAGNOSTIC_FAILED` after a failed attempt) before asking the Runtime + monitor for a full discovery, so TeamHub sees Pi become ready with the loaded + model. Each model's `pi_model` (`/`, as an Agent's + `runtime_model` stores it) is passed through as `localmodels` reports it; the + endpoint keeps only the loaded engine's `base_url`. ## Model source diff --git a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go index f3378a0e3..276dd4136 100644 --- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go +++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go @@ -139,9 +139,14 @@ func run(args []string) (resultErr error) { currentProviderEnvironment := func() map[string]string { return localmodels.OverlayEnvironment(baseProviderEnvironment, localModelsStateDir) } + var localModels *localmodels.Library currentDiscoveryOptions := func() provider.DiscoveryOptions { + var managedPi string + if localModels != nil { + managedPi = localModels.PiExecutable() + } return provider.DiscoveryOptions{ - ConfiguredPaths: localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir), + ConfiguredPaths: withManagedPiPath(localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir), managedPi), Runtimes: computer.EnabledRuntimes, PreferredRuntime: computer.PreferredRuntime, RuntimeHomeRoot: filepath.Join(computer.StateDirectory, "runtime-homes"), @@ -152,6 +157,9 @@ func run(args []string) (resultErr error) { result, err := provider.Discover(ctx, options) if err == nil { result.Diagnostics = byokRuntimeCatalogs(result.Diagnostics, localmodels.ReadOverlay(localModelsStateDir)) + if localModels != nil { + result.Diagnostics = withPiProvisionStatus(result.Diagnostics, localModels.PiProvisionFailed()) + } } return result, err } @@ -313,9 +321,21 @@ func run(args []string) (resultErr error) { } } jobDir := filepath.Join(computer.StateDirectory, "local-models", "jobs") - localModels := localmodels.New(localmodels.Options{ + var nodePath string + if toolInstallation != nil { + nodePath = toolInstallation.NodePath + } + piEnabled := len(computer.EnabledRuntimes) == 0 || slices.Contains(computer.EnabledRuntimes, "pi") + _, hasPi := discovered.Installations["pi"] + libraryHarnessDir := "" + if piEnabled && !hasPi { + libraryHarnessDir = localmodels.HarnessRoot(localModelsStateDir) + } + localModels = localmodels.New(localmodels.Options{ Tool: localModelsTool, Environment: baseProviderEnvironment, - JobDirectory: jobDir, + JobDirectory: jobDir, + HarnessDirectory: libraryHarnessDir, + NodePath: nodePath, // The library publishes only from Act and Refresh, which the Daemon // calls once it runs. Publish: func(snapshot wire.LocalModelsSnapshot) { localDaemon.PublishLocalModels(snapshot) }, @@ -336,10 +356,6 @@ func run(args []string) (resultErr error) { } return inventory.publish(publishCtx, nextRuntimes, next.Diagnostics) } - var nodePath string - if toolInstallation != nil { - nodePath = toolInstallation.NodePath - } modelSource := localmodels.NewModelSource(localmodels.ModelSourceOptions{ Tool: localModelsTool, Environment: baseProviderEnvironment, @@ -758,6 +774,35 @@ func installedTeamHubCLIBinAt(executable string) string { return directory } +func withManagedPiPath(paths map[string]string, managed string) map[string]string { + if managed == "" || paths["pi"] != "" { + return paths + } + cloned := maps.Clone(paths) + if cloned == nil { + cloned = make(map[string]string, 1) + } + cloned["pi"] = managed + return cloned +} + +func withPiProvisionStatus(diagnostics []provider.RuntimeDiagnostic, failed bool) []provider.RuntimeDiagnostic { + if !failed { + return diagnostics + } + cloned := slices.Clone(diagnostics) + for i := range cloned { + if cloned[i].Type == "pi" && cloned[i].Status == "not_installed" { + cloned[i].Status = "error" + cloned[i].Error = &provider.DiagnosticError{ + Code: "RUNTIME_DIAGNOSTIC_FAILED", + Message: "Automatic Pi runtime setup failed", + } + } + } + return cloned +} + func runtimeProviderEnvironment(source map[string]string, gatewayToken, codexAuthFile string) map[string]string { result := computerconfig.RuntimeEnvironment(source, gatewayToken) if codexAuthFile != "" { diff --git a/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go b/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go index 5630d5340..89626588a 100644 --- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go +++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go @@ -262,3 +262,46 @@ func TestByokRuntimeCatalogsShowTheEnteredModel(t *testing.T) { } } } + +func TestWithManagedPiPathRespectsConfiguredPiAndClonesSafely(t *testing.T) { + if got := withManagedPiPath(nil, ""); got != nil { + t.Fatalf("empty managed path returned %v", got) + } + configured := map[string]string{"codex": "/bin/codex", "pi": "/custom/pi"} + if got := withManagedPiPath(configured, "/managed/pi"); got["pi"] != "/custom/pi" { + t.Fatalf("managed Pi overwrote configured path: %v", got) + } + original := map[string]string{"codex": "/bin/codex"} + merged := withManagedPiPath(original, "/managed/pi") + if merged["pi"] != "/managed/pi" || original["pi"] != "" { + t.Fatalf("merged=%v original=%v", merged, original) + } +} + +func TestWithPiProvisionStatusMarksNotInstalledPiAsErrorWhenFailed(t *testing.T) { + original := []provider.RuntimeDiagnostic{ + {Type: "codex", Status: "not_installed", Error: &provider.DiagnosticError{Code: "RUNTIME_NOT_INSTALLED", Message: "missing"}}, + {Type: "pi", Status: "not_installed", Error: &provider.DiagnosticError{Code: "RUNTIME_NOT_INSTALLED", Message: "missing"}}, + {Type: "claude", Status: "ready"}, + } + unchanged := withPiProvisionStatus(original, false) + if unchanged[1].Status != "not_installed" { + t.Fatalf("expected not_installed when failed=false, got %+v", unchanged[1]) + } + + marked := withPiProvisionStatus(original, true) + if marked[1].Status != "error" || marked[1].Error == nil || marked[1].Error.Code != "RUNTIME_DIAGNOSTIC_FAILED" { + t.Fatalf("expected Pi error status when failed=true, got %+v", marked[1]) + } + if marked[0].Status != "not_installed" || marked[2].Status != "ready" { + t.Fatalf("non-Pi diagnostics changed unexpectedly: %+v", marked) + } + if original[1].Status != "not_installed" { + t.Fatalf("input diagnostics mutated in place: %+v", original[1]) + } + + readyPi := []provider.RuntimeDiagnostic{{Type: "pi", Status: "ready"}} + if got := withPiProvisionStatus(readyPi, true); got[0].Status != "ready" { + t.Fatalf("ready Pi overwritten: %+v", got[0]) + } +} diff --git a/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go b/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go index ea9655300..a6f6f653d 100644 --- a/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go +++ b/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go @@ -35,8 +35,9 @@ type fakeScript struct { } type fakeLine struct { - Line json.RawMessage `json:"line"` - DelayMS int `json:"delay_ms,omitempty"` + Line json.RawMessage `json:"line"` + DelayMS int `json:"delay_ms,omitempty"` + WaitFile string `json:"wait_file,omitempty"` // CancelAware lines stop the call with CANCELLED once a cancel ran. CancelAware bool `json:"cancel_aware,omitempty"` } @@ -46,13 +47,16 @@ func runFakeTool(args []string) int { if len(args) >= 6 && strings.Join(args[:5], " ") == "-P -s -m localmodels source" { return runFakeSourceTool(state, args[5:]) } + if len(args) >= 6 && strings.Join(args[:5], " ") == "-P -s -m localmodels harness" { + return runFakeHarness(state, args[5:]) + } if len(args) < 6 || strings.Join(args[:5], " ") != "-P -s -m localmodels library" { fmt.Fprintln(os.Stderr, "unexpected invocation", args) return 3 } verb := args[5] log, _ := os.OpenFile(filepath.Join(state, "calls.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) - _, _ = fmt.Fprintf(log, "%s|PYTHONPATH=%s|cwd=%s\n", strings.Join(args[5:], " "), os.Getenv("PYTHONPATH"), workingDirectory()) + _, _ = fmt.Fprintf(log, "%s|PYTHONPATH=%s|cwd=%s|PI_CODING_AGENT_DIR=%s\n", strings.Join(args[5:], " "), os.Getenv("PYTHONPATH"), workingDirectory(), os.Getenv("PI_CODING_AGENT_DIR")) _ = log.Close() if verb == "status" { if code, err := os.ReadFile(filepath.Join(state, "status-exit")); err == nil { @@ -80,6 +84,9 @@ func runFakeTool(args []string) int { _ = os.WriteFile(filepath.Join(state, "cancelled"), nil, 0o600) } for _, line := range script.Lines { + if line.WaitFile != "" { + waitForGateRemoval(filepath.Join(state, line.WaitFile), 30*time.Second) + } time.Sleep(time.Duration(line.DelayMS) * time.Millisecond) if _, err := os.Stat(filepath.Join(state, "cancelled")); line.CancelAware && err == nil { fmt.Printf(`{"schema":"localmodels.library-event/v1","event":"error","ok":false,"code":"CANCELLED","detail":"the download was cancelled","model":%q}`+"\n", args[6]) @@ -93,11 +100,85 @@ func runFakeTool(args []string) int { return script.Exit } +func waitForGateRemoval(gatePath string, timeout time.Duration) { + deadline := time.Now().Add(timeout) + for time.Now().Before(deadline) { + if _, err := os.Stat(gatePath); err != nil { + return + } + time.Sleep(10 * time.Millisecond) + } +} + func workingDirectory() string { directory, _ := os.Getwd() return directory } +func runFakeHarness(state string, args []string) int { + if len(args) < 1 { + return 3 + } + verb := args[0] + log, _ := os.OpenFile(filepath.Join(state, "calls.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) + _, _ = fmt.Fprintf(log, "harness %s|PI_CODING_AGENT_DIR=%s\n", strings.Join(args, " "), os.Getenv("PI_CODING_AGENT_DIR")) + _ = log.Close() + argValue := func(flag string) string { + for index := 0; index+1 < len(args); index++ { + if args[index] == flag { + return args[index+1] + } + } + return "" + } + root := argValue("--root") + installedPath := filepath.Join(root, "tools", "pi", "1.3.0", "pi") + statusJSON := func(installed bool) string { + if installed { + return fmt.Sprintf(`{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":true,"executable":%q,"argvPrefix":[]}]}`, installedPath) + } + return `{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":false,"executable":null,"argvPrefix":[]}]}` + } + switch verb { + case "status": + if _, err := os.Stat(filepath.Join(state, "harness-status-fail")); err == nil { + return 1 + } + _, err := os.Stat(filepath.Join(state, "harness-pi-installed")) + fmt.Println(statusJSON(err == nil)) + return 0 + case "plan": + waitGate := filepath.Join(state, "harness-plan-wait") + if _, err := os.Stat(waitGate); err == nil { + _ = os.WriteFile(filepath.Join(state, "harness-plan-entered"), []byte("1"), 0o600) + waitForGateRemoval(waitGate, 30*time.Second) + } + if _, err := os.Stat(filepath.Join(state, "harness-plan-fail")); err == nil { + return 1 + } + outPath := argValue("--out") + plan := fmt.Sprintf(`{"schema":"localmodels.harness-plan/v1","id":%q,"runtime":"pi","root":%q}`, strings.Repeat("a", 64), root) + if outPath != "" { + _ = os.WriteFile(outPath, []byte(plan), 0o600) + } + fmt.Println(plan) + return 0 + case "apply": + if _, err := os.Stat(filepath.Join(state, "harness-apply-fail")); err == nil { + return 1 + } + if argValue("--approve") != strings.Repeat("a", 64) { + return 2 + } + _ = os.WriteFile(filepath.Join(state, "harness-pi-installed"), []byte(installedPath), 0o600) + fmt.Println(`{"kind":"stage","stage":"install","state":"running"}`) + fmt.Println(statusJSON(true)) + return 0 + default: + return 3 + } +} + // fakeTool lays out a Tool that runs the fake. type fakeTool struct { t *testing.T diff --git a/apps/computer/local-daemon/internal/localmodels/library.go b/apps/computer/local-daemon/internal/localmodels/library.go index f2a1cd05b..27c943dbe 100644 --- a/apps/computer/local-daemon/internal/localmodels/library.go +++ b/apps/computer/local-daemon/internal/localmodels/library.go @@ -24,9 +24,12 @@ import ( // Defaults for Options left zero. const ( - defaultProgressInterval = 2 * time.Second - defaultStatusTimeout = 2 * time.Minute - defaultActionTimeout = 15 * time.Minute + defaultProgressInterval = 2 * time.Second + defaultStatusTimeout = 2 * time.Minute + defaultActionTimeout = 15 * time.Minute + defaultEnsurePiTimeout = 24 * time.Minute + defaultEnsurePiRetryInterval = 30 * time.Second + maxEnsurePiRetryInterval = 5 * time.Minute // maxRereadInterval bounds the backoff of status reads after failures. maxRereadInterval = time.Minute tailPollInterval = 100 * time.Millisecond @@ -41,6 +44,11 @@ type Options struct { Environment map[string]string // JobDirectory holds the output file of each running download or load. JobDirectory string + // HarnessDirectory holds the on-demand Pi runtime provisioned when a + // local model is loaded on a Computer without Pi. + HarnessDirectory string + // NodePath is an optional local Node executable passed via --node to harness plan. + NodePath string // Publish receives every new snapshot, in order. It is called only from // Act and Refresh and the jobs they start. Publish func(wire.LocalModelsSnapshot) @@ -48,9 +56,11 @@ type Options struct { // changes the model list Pi reports. RuntimeChanged func() // ProgressInterval bounds how often job progress is published. - ProgressInterval time.Duration - StatusTimeout time.Duration - ActionTimeout time.Duration + ProgressInterval time.Duration + StatusTimeout time.Duration + ActionTimeout time.Duration + EnsurePiTimeout time.Duration + EnsurePiRetryInterval time.Duration } // Library runs the localmodels tool for one Computer. @@ -72,29 +82,39 @@ type Options struct { // reads the job records localmodels keeps on disk, and polls it while a job // it did not start is running. type Library struct { - tool *Tool - environment map[string]string - jobDirectory string - publish func(wire.LocalModelsSnapshot) - runtimeChanged func() - progressInterval time.Duration - statusTimeout time.Duration - actionTimeout time.Duration - now func() time.Time - ctx context.Context - stop context.CancelFunc + tool *Tool + environment map[string]string + jobDirectory string + harnessDirectory string + nodePath string + publish func(wire.LocalModelsSnapshot) + runtimeChanged func() + progressInterval time.Duration + statusTimeout time.Duration + actionTimeout time.Duration + ensurePiTimeout time.Duration + ensurePiRetryInterval time.Duration + now func() time.Time + ctx context.Context + stop context.CancelFunc // refreshMu serializes status reads so an older read never replaces a // newer one. refreshMu sync.Mutex // publishMu orders updates of current with their Publish calls. publishMu sync.Mutex + piWg sync.WaitGroup mu sync.Mutex + closed bool current *wire.LocalModelsSnapshot // loaded identifies the loaded models of the last status read; nil // before the first one. - loaded *string - jobs map[string]*job + loaded *string + piExecutable string + piEnsuring bool + piFailures int + piRetryAt time.Time + jobs map[string]*job // causes holds, by model, the cause the last job's error line gave. The // tool's status keeps only the code, so each read puts the cause back on // a row that still fails with that code. @@ -116,8 +136,10 @@ func New(options Options) *Library { ctx, stop := context.WithCancel(context.Background()) library := &Library{ tool: options.Tool, environment: options.Environment, jobDirectory: options.JobDirectory, + harnessDirectory: options.HarnessDirectory, nodePath: options.NodePath, publish: options.Publish, runtimeChanged: options.RuntimeChanged, progressInterval: options.ProgressInterval, statusTimeout: options.StatusTimeout, actionTimeout: options.ActionTimeout, + ensurePiTimeout: options.EnsurePiTimeout, ensurePiRetryInterval: options.EnsurePiRetryInterval, now: time.Now, ctx: ctx, stop: stop, jobs: make(map[string]*job), causes: make(map[string]*wire.LocalModelError), } if library.progressInterval <= 0 { @@ -129,6 +151,12 @@ func New(options Options) *Library { if library.actionTimeout <= 0 { library.actionTimeout = defaultActionTimeout } + if library.ensurePiTimeout <= 0 { + library.ensurePiTimeout = defaultEnsurePiTimeout + } + if library.ensurePiRetryInterval <= 0 { + library.ensurePiRetryInterval = defaultEnsurePiRetryInterval + } if library.publish == nil { library.publish = func(wire.LocalModelsSnapshot) {} } @@ -146,25 +174,95 @@ func New(options Options) *Library { func (l *Library) Close() { l.stop() l.mu.Lock() + l.closed = true if l.progressTimer != nil { l.progressTimer.Stop() } l.mu.Unlock() + l.piWg.Wait() +} + +// PiExecutable returns the managed Pi executable provisioned for local +// models, or an empty string when none has been resolved. +func (l *Library) PiExecutable() string { + l.mu.Lock() + defer l.mu.Unlock() + return l.piExecutable +} + +// PiProvisionFailed reports whether automatic Pi setup has failed and not yet +// recovered, so Runtime diagnostics can surface an error instead of leaving Pi +// in not_installed indefinitely. +func (l *Library) PiProvisionFailed() bool { + l.mu.Lock() + defer l.mu.Unlock() + return l.piExecutable == "" && !l.piEnsuring && l.piFailures > 0 +} + +func (l *Library) scheduleEnsurePi() { + if l.harnessDirectory == "" || l.tool == nil || !l.tool.installed() { + return + } + l.mu.Lock() + now := l.now() + if l.closed || l.ctx.Err() != nil || l.piExecutable != "" || l.piEnsuring || (!l.piRetryAt.IsZero() && now.Before(l.piRetryAt)) { + l.mu.Unlock() + return + } + l.piEnsuring = true + l.piWg.Add(1) + l.mu.Unlock() + + go func() { + defer l.piWg.Done() + ctx, cancel := context.WithTimeout(l.ctx, l.ensurePiTimeout) + executable, err := l.tool.EnsurePi(ctx, l.environment, l.harnessDirectory, l.nodePath) + cancel() + + l.mu.Lock() + l.piEnsuring = false + if l.closed || l.ctx.Err() != nil { + l.mu.Unlock() + return + } + if err == nil && executable != "" { + l.piExecutable = executable + l.piFailures = 0 + l.piRetryAt = time.Time{} + l.mu.Unlock() + slog.Info("local_models.pi_ready", "executable", executable) + l.runtimeChanged() + return + } + l.piFailures++ + firstFailure := l.piFailures == 1 + backoff := min(l.ensurePiRetryInterval< 0 { // Local diagnostics only: tool prose can name local paths. - if diagnostic, readErr := readBounded(stderr.Name(), 4096); readErr == nil && len(diagnostic) > 0 { - slog.Debug("local_models.tool_stderr", "action", action.Name, "stderr", string(diagnostic)) - } + slog.Debug("local_models.tool_stderr", "action", action.Name, "stderr", string(stderrBytes)) } return output, exitCode, err } diff --git a/apps/computer/local-daemon/internal/localmodels/pi_runtime.go b/apps/computer/local-daemon/internal/localmodels/pi_runtime.go new file mode 100644 index 000000000..adeaa9109 --- /dev/null +++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime.go @@ -0,0 +1,183 @@ +package localmodels + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "log/slog" + "os" + "path/filepath" + "regexp" + "time" + + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/platform/privatefs" +) + +const ( + harnessStatusSchema = "localmodels.harness-status/v1" + harnessPlanSchema = "localmodels.harness-plan/v1" + maxHarnessOutput = 4 << 20 + harnessStatusTimeout = 2 * time.Minute + harnessPlanTimeout = 2 * time.Minute + harnessApplyTimeout = 20 * time.Minute +) + +var hex64Pattern = regexp.MustCompile(`^[a-f0-9]{64}$`) + +type harnessStatusDoc struct { + Schema string `json:"schema"` + Runtimes []struct { + Runtime string `json:"runtime"` + Installed bool `json:"installed"` + Executable string `json:"executable"` + ArgvPrefix []string `json:"argvPrefix"` + } `json:"runtimes"` +} + +type harnessPlanDoc struct { + Schema string `json:"schema"` + ID string `json:"id"` + Runtime string `json:"runtime"` +} + +// InspectPi reports the verified Pi executable managed under harnessDir, or an +// empty string when Pi is not installed there yet. +func (t Tool) InspectPi(ctx context.Context, environment map[string]string, harnessDir string) (string, error) { + if harnessDir == "" || !t.installed() { + return "", nil + } + info, err := os.Stat(harnessDir) + if errors.Is(err, os.ErrNotExist) { + return "", nil + } + if err != nil { + return "", err + } + if !info.IsDir() { + return "", errors.New("harness root is not a directory") + } + statusCtx, cancelStatus := context.WithTimeout(ctx, harnessStatusTimeout) + defer cancelStatus() + stdout, exitCode, err := t.runHarness( + statusCtx, + environment, + []string{"-P", "-s", "-m", "localmodels", "harness", "status", "--root", harnessDir, "--runtime", "pi", "--json"}, + ) + if err != nil { + return "", err + } + if exitCode != 0 { + return "", fmt.Errorf("harness status exited with code %d", exitCode) + } + return parsePiFromHarnessStatus(stdout) +} + +// EnsurePi returns the Pi executable managed under harnessDir, installing the +// pinned Pi runtime through localmodels harness when it is not installed yet. +// When nodePath is non-empty and names an existing file, it is passed via --node +// so harness does not need to download a separate Node binary. +func (t Tool) EnsurePi(ctx context.Context, environment map[string]string, harnessDir, nodePath string) (string, error) { + if harnessDir == "" || !t.installed() { + return "", nil + } + if existing, err := t.InspectPi(ctx, environment, harnessDir); err == nil && existing != "" { + return existing, nil + } + if err := privatefs.EnsureDir(harnessDir); err != nil { + return "", err + } + planDir, err := os.MkdirTemp(harnessDir, ".pi-plan-*") + if err != nil { + return "", err + } + defer func() { _ = os.RemoveAll(planDir) }() + if err := privatefs.EnsureDir(planDir); err != nil { + return "", err + } + planPath := filepath.Join(planDir, "plan.json") + planArgs := []string{"-P", "-s", "-m", "localmodels", "harness", "plan", "--runtime", "pi", "--root", harnessDir} + if nodePath != "" { + if info, statErr := os.Stat(nodePath); statErr == nil && !info.IsDir() { + planArgs = append(planArgs, "--node", nodePath) + } + } + planArgs = append(planArgs, "--out", planPath, "--json") + planCtx, cancelPlan := context.WithTimeout(ctx, harnessPlanTimeout) + _, planExitCode, err := t.runHarness(planCtx, environment, planArgs) + cancelPlan() + if err != nil { + return "", err + } else if planExitCode != 0 { + return "", fmt.Errorf("harness plan exited with code %d", planExitCode) + } + encodedPlan, err := os.ReadFile(planPath) + if err != nil { + return "", err + } + var plan harnessPlanDoc + if err := json.Unmarshal(encodedPlan, &plan); err != nil { + return "", fmt.Errorf("decode harness plan: %w", err) + } + if plan.Schema != harnessPlanSchema || plan.Runtime != "pi" || !hex64Pattern.MatchString(plan.ID) { + return "", errors.New("harness plan is outside the contract") + } + applyCtx, cancelApply := context.WithTimeout(ctx, harnessApplyTimeout) + stdout, exitCode, err := t.runHarness( + applyCtx, + environment, + []string{"-P", "-s", "-m", "localmodels", "harness", "apply", planPath, "--approve", plan.ID, "--jsonl"}, + ) + cancelApply() + if err != nil { + return "", err + } + if exitCode != 0 { + return "", fmt.Errorf("harness apply exited with code %d", exitCode) + } + if executable, parseErr := parseLastHarnessStatus(stdout); parseErr == nil && executable != "" { + return executable, nil + } + return t.InspectPi(ctx, environment, harnessDir) +} + +func (t Tool) runHarness(ctx context.Context, environment map[string]string, args []string) ([]byte, int, error) { + stdout, stderrBytes, exitCode, err := t.runBuffered(ctx, environment, "", "localmodels-harness", maxHarnessOutput, args, nil) + if exitCode != 0 && len(stderrBytes) > 0 { + slog.Debug("local_models.harness_stderr", "stderr", string(stderrBytes)) + } + return stdout, exitCode, err +} + +func parseLastHarnessStatus(stdout []byte) (string, error) { + lines := bytes.Split(stdout, []byte{'\n'}) + for index := len(lines) - 1; index >= 0; index-- { + line := bytes.TrimSpace(lines[index]) + if len(line) == 0 { + continue + } + return parsePiFromHarnessStatus(line) + } + return "", errors.New("harness output is empty") +} + +func parsePiFromHarnessStatus(encoded []byte) (string, error) { + var doc harnessStatusDoc + if err := json.Unmarshal(bytes.TrimSpace(encoded), &doc); err != nil { + return "", fmt.Errorf("decode harness status: %w", err) + } + if doc.Schema != harnessStatusSchema { + return "", fmt.Errorf("harness status schema is %q", doc.Schema) + } + for _, row := range doc.Runtimes { + if row.Runtime != "pi" { + continue + } + if !row.Installed || len(row.ArgvPrefix) != 0 || !filepath.IsAbs(row.Executable) { + return "", nil + } + return row.Executable, nil + } + return "", nil +} diff --git a/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go new file mode 100644 index 000000000..3e06821aa --- /dev/null +++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go @@ -0,0 +1,362 @@ +package localmodels + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "slices" + "strings" + "sync" + "testing" + "time" +) + +func TestToolCommandAlignsPiCodingAgentDir(t *testing.T) { + fake := newFakeTool(t) + home := filepath.Join(filepath.Dir(fake.state), "home") + explicit := filepath.Join(filepath.Dir(fake.state), "custom-pi") + + cmdDefault := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": ""}, Action{Name: ActionRefresh}) + wantDefault := "PI_CODING_AGENT_DIR=" + filepath.Join(home, ".pi", "agent") + if !slices.Contains(cmdDefault.Env, wantDefault) { + t.Fatalf("expected %q in env %v", wantDefault, cmdDefault.Env) + } + + cmdExplicit := fake.tool.command(map[string]string{"HOME": home, "pi_coding_agent_dir": explicit}, Action{Name: ActionRefresh}) + wantExplicit := "PI_CODING_AGENT_DIR=" + explicit + if !slices.Contains(cmdExplicit.Env, wantExplicit) { + t.Fatalf("expected %q in env %v", wantExplicit, cmdExplicit.Env) + } + + gatewayPi := filepath.Join(fake.tool.StateDirectory, "gateway-home", ".pi", "agent") + if err := os.MkdirAll(gatewayPi, 0o700); err != nil { + t.Fatal(err) + } + writeModelSourceJSON(t, fake.tool.StateDirectory, map[string]any{ + "schema": modelSourceSchema, + "mode": "local", + "runtime": "pi", + }) + cmdOverlay := fake.tool.command(map[string]string{"HOME": home}, Action{Name: ActionRefresh}) + wantOverlay := "PI_CODING_AGENT_DIR=" + gatewayPi + if !slices.Contains(cmdOverlay.Env, wantOverlay) { + t.Fatalf("expected local overlay %q in env %v", wantOverlay, cmdOverlay.Env) + } + + // Local model-source overlay beats an explicit PI_CODING_AGENT_DIR in the + // base environment, matching OverlayEnvironment. + cmdExplicitAndOverlay := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": explicit}, Action{Name: ActionRefresh}) + if !slices.Contains(cmdExplicitAndOverlay.Env, wantOverlay) { + t.Fatalf("expected local overlay %q to override explicit base env %v", wantOverlay, cmdExplicitAndOverlay.Env) + } + + // Switching away from local strips a managed gateway-home Pi dir and falls + // back to the source Pi profile (~/.pi/agent). + writeModelSourceJSON(t, fake.tool.StateDirectory, map[string]any{ + "schema": modelSourceSchema, + "mode": "subscription", + "runtime": "claude", + }) + cmdSubscription := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": gatewayPi}, Action{Name: ActionRefresh}) + if !slices.Contains(cmdSubscription.Env, wantDefault) { + t.Fatalf("expected stale gateway path stripped back to %q in %v", wantDefault, cmdSubscription.Env) + } +} + +func TestLoadProvisionsPiInHarnessDirectoryWithNodePathAndNotifiesRuntimeChanged(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "idle"))) + loaded, _ := json.Marshal(status(model(testModel, "loaded"))) + fake.script("load", fakeScript{ + Lines: []fakeLine{ + {Line: progressLine(testModel, "load", 0, 1)}, + {Line: resultLine(testModel), DelayMS: 20}, + }, + StatusAfter: loaded, + }) + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + home := filepath.Join(filepath.Dir(fake.state), "home") + nodePath := filepath.Join(t.TempDir(), "node") + if err := os.WriteFile(nodePath, []byte("#!/bin/sh\n"), 0o700); err != nil { + t.Fatal(err) + } + record := &recorder{} + var seenMu sync.Mutex + var seenPiAtChange string + library := New(Options{ + Tool: &fake.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": home, "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + NodePath: nodePath, + Publish: record.publish, + ProgressInterval: 20 * time.Millisecond, + }) + library.runtimeChanged = func() { + if executable := library.PiExecutable(); executable != "" { + seenMu.Lock() + seenPiAtChange = executable + seenMu.Unlock() + } + record.runtimeChanged() + } + t.Cleanup(library.Close) + + library.Refresh(context.Background()) + if library.PiExecutable() != "" { + t.Fatalf("idle library unexpectedly provisioned Pi: %q", library.PiExecutable()) + } + + if refusal, answered := library.Act(context.Background(), Action{Name: ActionLoad, Model: testModel}, soon()); refusal != nil || !answered { + t.Fatalf("load refusal=%+v answered=%v", refusal, answered) + } + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "Pi provisioned and runtimeChanged fired", func() bool { + seenMu.Lock() + seen := seenPiAtChange + seenMu.Unlock() + return record.changes() > 0 && library.PiExecutable() == want && seen == want + }) + calls := fake.calls() + if !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness status ") }) || + !slices.ContainsFunc(calls, func(c string) bool { + return strings.HasPrefix(c, "harness plan ") && strings.Contains(c, "--node "+nodePath) + }) || + !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness apply ") }) { + t.Fatalf("expected harness status/plan(--node)/apply in calls: %v", calls) + } + wantPiDir := "PI_CODING_AGENT_DIR=" + filepath.Join(home, ".pi", "agent") + if !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "load ") && strings.Contains(c, wantPiDir) }) { + t.Fatalf("expected load call with %s in %v", wantPiDir, calls) + } +} + +func TestInitialRefreshProvisionsPiAndInspectPiFindsExisting(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "loaded"))) + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + record := &recorder{} + env := map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")} + library := New(Options{ + Tool: &fake.tool, + Environment: env, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + Publish: record.publish, + RuntimeChanged: record.runtimeChanged, + ProgressInterval: time.Second, + }) + t.Cleanup(library.Close) + + library.Refresh(context.Background()) + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "initial loaded model provisions Pi asynchronously", func() bool { + return library.PiExecutable() == want && record.changes() == 1 + }) + + inspected, err := fake.tool.InspectPi(context.Background(), env, harnessDir) + if err != nil { + t.Fatalf("InspectPi error: %v", err) + } + if inspected != want { + t.Fatalf("InspectPi=%q want=%q", inspected, want) + } + + planCount := func() int { + count := 0 + for _, call := range fake.calls() { + if strings.HasPrefix(call, "harness plan ") { + count++ + } + } + return count + } + beforePlans := planCount() + + reusedRecord := &recorder{} + reused := New(Options{ + Tool: &fake.tool, + Environment: env, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + Publish: reusedRecord.publish, + RuntimeChanged: reusedRecord.runtimeChanged, + ProgressInterval: time.Second, + }) + t.Cleanup(reused.Close) + reused.Refresh(context.Background()) + waitFor(t, "existing Pi resolved via InspectPi without replanning", func() bool { + return reused.PiExecutable() == want && reusedRecord.changes() == 1 + }) + if afterPlans := planCount(); afterPlans != beforePlans { + t.Fatalf("expected InspectPi to reuse existing Pi without harness plan (%d -> %d)", beforePlans, afterPlans) + } +} + +func TestFailedPiProvisioningDoesNotFailLoadedSnapshot(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "loaded"))) + if err := os.WriteFile(filepath.Join(fake.state, "harness-plan-fail"), []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + library := New(Options{ + Tool: &fake.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + EnsurePiRetryInterval: time.Minute, + }) + t.Cleanup(library.Close) + + snapshot := library.Refresh(context.Background()) + if !snapshot.Available || modelState(snapshot, testModel).State != "loaded" { + t.Fatalf("snapshot=%+v", snapshot) + } + waitFor(t, "failed Pi attempt recorded", func() bool { + return library.PiProvisionFailed() + }) + if library.PiExecutable() != "" { + t.Fatalf("unexpected PiExecutable=%q", library.PiExecutable()) + } +} + +func TestEnsurePiRunsOutsideRefreshLockAndBacksOffOnFailure(t *testing.T) { + fake := newFakeTool(t) + const otherModel = "other-model:Q4_K_M" + fake.write("status.json", status(model(testModel, "loaded"), model(otherModel, "absent"))) + waitGate := filepath.Join(fake.state, "harness-plan-wait") + if err := os.WriteFile(waitGate, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + record := &recorder{} + library := New(Options{ + Tool: &fake.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + Publish: record.publish, + RuntimeChanged: record.runtimeChanged, + ProgressInterval: 10 * time.Millisecond, + }) + t.Cleanup(library.Close) + + first := library.Refresh(context.Background()) + if !first.Available || modelState(first, testModel).State != "loaded" { + t.Fatalf("first Refresh=%+v", first) + } + waitFor(t, "background harness plan started", func() bool { + _, err := os.Stat(filepath.Join(fake.state, "harness-plan-entered")) + return err == nil + }) + + // While EnsurePi is blocked on harness plan, another Refresh and a job's + // phaseStarted status reread must proceed without blocking on refreshMu. + second := library.Refresh(context.Background()) + if !second.Available || modelState(second, testModel).State != "loaded" { + t.Fatalf("second Refresh=%+v", second) + } + downloaded, _ := json.Marshal(status(model(testModel, "loaded"), model(otherModel, "idle"))) + downloadWait := filepath.Join(fake.state, "download-wait") + if err := os.WriteFile(downloadWait, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + fake.script("download", fakeScript{ + Lines: []fakeLine{ + {Line: progressLine(otherModel, "download", 10, 100)}, + {Line: resultLine(otherModel), WaitFile: "download-wait"}, + }, + StatusAfter: downloaded, + }) + if refusal, answered := library.Act(context.Background(), Action{Name: ActionDownload, Model: otherModel}, soon()); refusal != nil || !answered { + t.Fatalf("download refusal=%+v answered=%v", refusal, answered) + } + waitFor(t, "phaseStarted published downloading snapshot while EnsurePi waits", func() bool { + state := modelState(record.last(), otherModel) + return state != nil && state.State == "downloading" + }) + if got := library.PiExecutable(); got != "" { + t.Fatalf("EnsurePi finished before waitGate was released: %q", got) + } + + if err := os.Remove(downloadWait); err != nil { + t.Fatal(err) + } + if err := os.Remove(waitGate); err != nil { + t.Fatal(err) + } + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "blocked EnsurePi finished after gate released", func() bool { + return library.PiExecutable() == want && record.changes() == 1 + }) + waitFor(t, "gated download completed after gate released", func() bool { + state := modelState(record.last(), otherModel) + return state != nil && state.State == "idle" + }) + + // Failed installs back off instead of retrying on every Refresh, then retry + // once the backoff window elapses. + fake2 := newFakeTool(t) + fake2.write("status.json", status(model(testModel, "loaded"))) + failMarker := filepath.Join(fake2.state, "harness-plan-fail") + if err := os.WriteFile(failMarker, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir2 := filepath.Join(filepath.Dir(fake2.state), "harness") + record2 := &recorder{} + var clockMu sync.Mutex + currentTime := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC) + library2 := New(Options{ + Tool: &fake2.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir2, + Publish: record2.publish, + RuntimeChanged: record2.runtimeChanged, + EnsurePiRetryInterval: 30 * time.Second, + }) + library2.now = func() time.Time { + clockMu.Lock() + defer clockMu.Unlock() + return currentTime + } + t.Cleanup(library2.Close) + + library2.Refresh(context.Background()) + waitFor(t, "initial Pi install failure recorded", func() bool { + return library2.PiProvisionFailed() && record2.changes() == 1 + }) + planCalls := func() int { + count := 0 + for _, call := range fake2.calls() { + if strings.HasPrefix(call, "harness plan ") { + count++ + } + } + return count + } + if got := planCalls(); got != 1 { + t.Fatalf("expected 1 harness plan attempt, got %d (%v)", got, fake2.calls()) + } + + library2.Refresh(context.Background()) + library2.Refresh(context.Background()) + if got := planCalls(); got != 1 { + t.Fatalf("expected backoff to suppress retries before window elapses, got %d calls", got) + } + + if err := os.Remove(failMarker); err != nil { + t.Fatal(err) + } + clockMu.Lock() + currentTime = currentTime.Add(31 * time.Second) + clockMu.Unlock() + + library2.Refresh(context.Background()) + want2 := filepath.Join(harnessDir2, "tools", "pi", "1.3.0", "pi") + waitFor(t, "Pi install retried and succeeded after backoff elapsed", func() bool { + return library2.PiExecutable() == want2 && !library2.PiProvisionFailed() && record2.changes() == 2 + }) +} diff --git a/apps/computer/local-daemon/internal/localmodels/source.go b/apps/computer/local-daemon/internal/localmodels/source.go index 00c453561..e6ba4d4cb 100644 --- a/apps/computer/local-daemon/internal/localmodels/source.go +++ b/apps/computer/local-daemon/internal/localmodels/source.go @@ -1,7 +1,6 @@ package localmodels import ( - "bytes" "context" "encoding/json" "errors" @@ -15,7 +14,6 @@ import ( "sync" "time" - "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/process" localworkspace "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace" wire "github.com/simplexai-labs/linger/contracts-go/runnergateway" ) @@ -192,8 +190,8 @@ func OverlayRuntimePaths(configured map[string]string, stateDir string) map[stri } } } - if stateDir != "" { - binDir := filepath.Clean(filepath.Join(stateDir, "..", "..", "harness", "bin")) + if harness := HarnessRoot(stateDir); harness != "" { + binDir := filepath.Join(harness, "bin") for _, rt := range []string{"claude", "codex", "pi"} { if result[rt] != "" { continue @@ -251,8 +249,8 @@ type ModelSource struct { // NewModelSource constructs a ModelSource controller for the given localmodels Tool. func NewModelSource(options ModelSourceOptions) *ModelSource { harnessRoot := options.HarnessRoot - if harnessRoot == "" && options.Tool != nil && options.Tool.StateDirectory != "" { - harnessRoot = filepath.Clean(filepath.Join(options.Tool.StateDirectory, "..", "..", "harness")) + if harnessRoot == "" && options.Tool != nil { + harnessRoot = HarnessRoot(options.Tool.StateDirectory) } statusTimeout := options.StatusTimeout if statusTimeout <= 0 { @@ -660,53 +658,10 @@ func (m *ModelSource) runTool(ctx context.Context, label string, args []string, dir := m.jobDirectory if dir == "" { dir = os.TempDir() - } else if err := os.MkdirAll(dir, 0o700); err != nil { - return nil, -1, err } - stdout, err := os.CreateTemp(dir, "model-source-*.out") - if err != nil { - return nil, -1, err - } - defer func() { _ = os.Remove(stdout.Name()) }() - - cmd := exec.Command(m.tool.Python, args...) - cmd.Dir = m.tool.ModuleDirectory - values := make(map[string]string, len(m.environment)+4) - for key, value := range m.environment { - switch strings.ToUpper(key) { - case "PYTHONHOME", "PYTHONSTARTUP", "PYTHONINSPECT", "PYTHONPATH", "LOCALMODELS_STATE": - continue - } - values[key] = value - } - values["PYTHONPATH"] = m.tool.ModuleDirectory - values["LOCALMODELS_STATE"] = m.tool.StateDirectory - values["PYTHONNOUSERSITE"] = "1" - values["PYTHONDONTWRITEBYTECODE"] = "1" - cmd.Env = make([]string, 0, len(values)) - for key, value := range values { - cmd.Env = append(cmd.Env, key+"="+value) + output, _, exitCode, err := m.tool.runBuffered(ctx, m.environment, dir, "model-source", maxEventLine, args, stdin) + if err != nil && ctx.Err() != nil { + return nil, -1, errors.Join(fmt.Errorf("%s cancelled", label), err) } - if len(stdin) > 0 { - cmd.Stdin = bytes.NewReader(stdin) - } - cmd.Stdout = stdout - process.Prepare(cmd) - startErr := cmd.Start() - _ = stdout.Close() - if startErr != nil { - return nil, -1, startErr - } - exited := make(chan error, 1) - go func() { exited <- cmd.Wait() }() - select { - case <-exited: - case <-ctx.Done(): - _ = cmd.Process.Kill() - <-exited - return nil, -1, errors.Join(fmt.Errorf("%s cancelled", label), context.Cause(ctx)) - } - exitCode := cmd.ProcessState.ExitCode() - output, readErr := readBounded(stdout.Name(), maxEventLine) - return output, exitCode, readErr + return output, exitCode, err } diff --git a/apps/computer/local-daemon/internal/localmodels/tool.go b/apps/computer/local-daemon/internal/localmodels/tool.go index 3f938d9ba..4c579aaa6 100644 --- a/apps/computer/local-daemon/internal/localmodels/tool.go +++ b/apps/computer/local-daemon/internal/localmodels/tool.go @@ -6,11 +6,26 @@ package localmodels import ( + "bytes" + "context" "os" "os/exec" + "path/filepath" "strings" + + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/process" + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace" ) +// HarnessRoot returns the shared runtime/harness directory sibling to a +// localmodels StateDirectory, or an empty string when stateDir is unset. +func HarnessRoot(stateDir string) string { + if stateDir == "" { + return "" + } + return filepath.Clean(filepath.Join(stateDir, "..", "..", "harness")) +} + // Tool is where the installer put the localmodels tool and its state, as // the Computer config records it. type Tool struct { @@ -58,31 +73,107 @@ func (t Tool) arguments(action Action) []string { } } -// command builds one tool invocation the way the installer runs it: the -// sealed kit on PYTHONPATH and as the working directory, no user site -// packages, no bytecode writes into the sealed kit. +// command builds one library invocation for the tool. func (t Tool) command(environment map[string]string, action Action) *exec.Cmd { - cmd := exec.Command(t.Python, t.arguments(action)...) + return t.commandArgs(environment, t.arguments(action)) +} + +// commandArgs builds one tool invocation the way the installer runs it: the +// sealed kit on PYTHONPATH and as the working directory, no user site +// packages, no bytecode writes into the sealed kit, and PI_CODING_AGENT_DIR +// resolved through OverlayEnvironment and workspace.PiSourceEnvironment so +// localmodels reads and writes the same Pi profile the Daemon probes and runs. +func (t Tool) commandArgs(environment map[string]string, args []string) *exec.Cmd { + cmd := exec.Command(t.Python, args...) cmd.Dir = t.ModuleDirectory - values := make(map[string]string, len(environment)+4) + cleaned := make(map[string]string, len(environment)) for key, value := range environment { switch strings.ToUpper(key) { case "PYTHONHOME", "PYTHONSTARTUP", "PYTHONINSPECT", "PYTHONPATH", "LOCALMODELS_STATE": continue + case "PI_CODING_AGENT_DIR": + if trimmed := strings.TrimSpace(value); trimmed != "" { + cleaned["PI_CODING_AGENT_DIR"] = trimmed + } + continue } - values[key] = value + cleaned[key] = value } - values["PYTHONPATH"] = t.ModuleDirectory - values["LOCALMODELS_STATE"] = t.StateDirectory - values["PYTHONNOUSERSITE"] = "1" - values["PYTHONDONTWRITEBYTECODE"] = "1" - cmd.Env = make([]string, 0, len(values)) - for key, value := range values { + effective := OverlayEnvironment(cleaned, t.StateDirectory) + if piEnv, err := workspace.PiSourceEnvironment(effective); err == nil && piEnv["PI_CODING_AGENT_DIR"] != "" { + effective["PI_CODING_AGENT_DIR"] = piEnv["PI_CODING_AGENT_DIR"] + } + effective["PYTHONPATH"] = t.ModuleDirectory + effective["LOCALMODELS_STATE"] = t.StateDirectory + effective["PYTHONNOUSERSITE"] = "1" + effective["PYTHONDONTWRITEBYTECODE"] = "1" + cmd.Env = make([]string, 0, len(effective)) + for key, value := range effective { cmd.Env = append(cmd.Env, key+"="+value) } return cmd } +// runBuffered executes localmodels with stdout and stderr redirected to +// temporary files (never pipes) so any background gateway child spawned by +// localmodels survives after the CLI process exits. +func (t Tool) runBuffered( + ctx context.Context, + environment map[string]string, + tempDir, prefix string, + maxBytes int64, + args []string, + stdin []byte, +) (stdoutBytes, stderrBytes []byte, exitCode int, err error) { + if tempDir != "" { + if mkErr := os.MkdirAll(tempDir, 0o700); mkErr != nil { + return nil, nil, -1, mkErr + } + } + stdout, err := os.CreateTemp(tempDir, prefix+"-*.out") + if err != nil { + return nil, nil, -1, err + } + defer func() { _ = os.Remove(stdout.Name()) }() + stderr, err := os.CreateTemp(tempDir, prefix+"-*.err") + if err != nil { + _ = stdout.Close() + return nil, nil, -1, err + } + defer func() { _ = os.Remove(stderr.Name()) }() + + cmd := t.commandArgs(environment, args) + if len(stdin) > 0 { + cmd.Stdin = bytes.NewReader(stdin) + } + cmd.Stdout, cmd.Stderr = stdout, stderr + process.Prepare(cmd) + startErr := cmd.Start() + _ = stdout.Close() + _ = stderr.Close() + if startErr != nil { + return nil, nil, -1, startErr + } + exited := make(chan error, 1) + go func() { exited <- cmd.Wait() }() + select { + case <-exited: + case <-ctx.Done(): + _ = cmd.Process.Kill() + <-exited + return nil, nil, -1, context.Cause(ctx) + } + code := cmd.ProcessState.ExitCode() + out, readErr := readBounded(stdout.Name(), maxBytes) + var errOut []byte + if code != 0 { + if diagnostic, diagErr := readBounded(stderr.Name(), 4096); diagErr == nil { + errOut = diagnostic + } + } + return out, errOut, code, readErr +} + // installed reports whether the configured interpreter still exists. func (t Tool) installed() bool { info, err := os.Stat(t.Python) From b00a65c613c16cd190a6377c0160367e7073eec9 Mon Sep 17 00:00:00 2001 From: Dongdong Chen Date: Thu, 19 Nov 2026 13:45:54 +0800 Subject: [PATCH 2/3] feat(web): add Bifrost readiness and one-click Agent switching Show live Bifrost (Pi) Gateway readiness above Endpoint (distinguishing uninstalled, setting up, registering, and failed states), gate row/card Agent switching on ready Pi, add Use with and Open chat actions alongside Used by switching and Make room automatic Agent handoff (excluding busy Agents and surfacing busy-handoff warnings), reuse usedByBadge in local-models-rules, drop test-only useInRouterContext fallbacks, link Pi setup to Settings > Models, and resolve localmodels-library IDs via useResolvedAgentModelLabel. --- apps/web/AGENTS.md | 9 +- .../local-models/local-model-agents.test.ts | 264 +++++++++++ .../local-models/local-model-agents.ts | 190 ++++++++ .../local-models/local-model-details.tsx | 117 ++++- .../local-models/local-model-dialogs.tsx | 46 +- .../features/local-models/local-model-row.tsx | 108 ++++- .../local-models/local-models-page.test.tsx | 413 ++++++++++++++++++ .../local-models/local-models-page.tsx | 263 ++++++++++- .../local-models/local-models-rules.test.ts | 10 + .../local-models/local-models-rules.ts | 8 +- .../chat/direct-agent-model-switch.test.tsx | 96 +++- .../rooms/chat/direct-agent-model-switch.tsx | 19 +- .../features/rooms/members/actor-profile.tsx | 8 +- .../edit-agent-runtime-dialog.test.tsx | 124 +++++- .../members/edit-agent-runtime-dialog.tsx | 100 ++++- .../members/use-agent-model-selection.ts | 57 ++- 16 files changed, 1732 insertions(+), 100 deletions(-) create mode 100644 apps/web/src/features/local-models/local-model-agents.test.ts create mode 100644 apps/web/src/features/local-models/local-model-agents.ts diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md index 26fb3d878..f126140c4 100644 --- a/apps/web/AGENTS.md +++ b/apps/web/AGENTS.md @@ -39,9 +39,14 @@ build:assets` to check the initial JS budget. loaded row, renders tinted family icon tiles, Agent usage counts, structured capability/parameter badges (never matched against `name` or `description`), state-distinct cards (`loaded`, - `recommended`, `failed`, `unavailable`), download speed/ETA, and top + `recommended`, `failed`, `unavailable`), download speed/ETA, live Bifrost + (`Pi`) `Gateway` readiness, and top diagnostic callouts with a two-column model-card facts grid from structured - `LocalModel` fields in `local-models-rules.ts`, and maps every code and enum + `LocalModel` fields in `local-models-rules.ts`, resolves Sponsor-owned Agent + eligibility and `Pi` runtime switch payloads in `local-model-agents.ts` + (`Use with `, `Open chat`, `Switch Agent…` in `Used by`, and + `Make room` automatic Agent handoff once the replacement model loads when `Pi` + is ready), and maps every code and enum label to English in `local-models-copy.ts`. Without `?computer=` the desktop app opens its own Computer. Only the Computer's owner can act; actions return at once diff --git a/apps/web/src/features/local-models/local-model-agents.test.ts b/apps/web/src/features/local-models/local-model-agents.test.ts new file mode 100644 index 000000000..c8c3dd283 --- /dev/null +++ b/apps/web/src/features/local-models/local-model-agents.test.ts @@ -0,0 +1,264 @@ +import { describe, expect, it } from "vitest"; + +import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; +import { createTeamHubTestSeed } from "@/test/teamhub-seed"; + +import { + assignableLocalModelAgents, + buildLocalModelRuntimeUpdate, + isAgentUsingModel, + primaryLocalModelAgent, + usedByAgentNames, +} from "./local-model-agents"; +import { localModel } from "./local-models-test-support"; + +describe("local-model-agents", () => { + it("filters to Sponsor-owned Agents, places Another Me first, and keeps compatible workspace grants", () => { + const seed = createTeamHubTestSeed(); + const computer = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + workspace_grants: [ + { + grant_id: "grant-ro", + label: "Read only", + mode: "read-only" as const, + }, + { + grant_id: "grant-rw", + label: "Workspace", + mode: "workspace-write" as const, + }, + { + grant_id: "grant-other", + label: "Other workspace", + mode: "workspace-write" as const, + }, + ], + }; + const [codex, lev8, delegate] = seed.tenantAgents; + const codexOnLab = { + ...codex!, + runtime: { + ...codex!.runtime, + placement: { + computer_id: "computer-lab", + runtime_type: "codex" as const, + type: "local" as const, + workspace_grant_id: "grant-other", + }, + }, + }; + const result = assignableLocalModelAgents({ + agents: [codexOnLab, lev8!, delegate!], + anotherMeActorId: delegate!.actor.id, + availability: [ + { + actor_id: codexOnLab.actor.id, + availability: { + active_task_count: 1, + label: "Busy", + status: "online", + }, + }, + ], + computer, + computerId: "computer-lab", + currentActorId: seed.identity.actors[0]!.id, + }); + + // Lev8 GTM is sponsored by Maya, not Jenny. + expect(result.map((agent) => agent.actorId)).toEqual([ + delegate!.actor.id, + codexOnLab.actor.id, + ]); + expect(result[0]!).toMatchObject({ + busy: false, + grantId: "grant-rw", + isAnotherMe: true, + name: "Jenny's Delegate", + }); + expect(result[1]!).toMatchObject({ + busy: true, + grantId: "grant-other", + isAnotherMe: false, + name: "Codex", + }); + expect(primaryLocalModelAgent(result)?.actorId).toBe(delegate!.actor.id); + }); + + it("excludes all Agents on an upgrade-required or read-only Computer", () => { + const seed = createTeamHubTestSeed(); + const upgradeComputer = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + upgrade_required: true, + }; + expect( + assignableLocalModelAgents({ + agents: seed.tenantAgents, + anotherMeActorId: seed.tenantAgents[2]!.actor.id, + computer: upgradeComputer, + computerId: "computer-lab", + currentActorId: seed.identity.actors[0]!.id, + }), + ).toEqual([]); + + const readOnlyComputer = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + workspace_grants: [ + { + grant_id: "grant-ro", + label: "Read only", + mode: "read-only" as const, + }, + ], + }; + expect( + assignableLocalModelAgents({ + agents: seed.tenantAgents, + anotherMeActorId: seed.tenantAgents[2]!.actor.id, + computer: readOnlyComputer, + computerId: "computer-lab", + currentActorId: seed.identity.actors[0]!.id, + }), + ).toEqual([]); + }); + + it("matches Agents via either snapshot used_by or live Pi placement", () => { + const seed = createTeamHubTestSeed(); + const delegate = { + ...seed.tenantAgents[2]!, + runtime: { + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi" as const, + type: "local" as const, + workspace_grant_id: "grant-rw", + }, + }, + }; + const [agent] = assignableLocalModelAgents({ + agents: [delegate], + anotherMeActorId: delegate.actor.id, + computerId: "computer-lab", + currentActorId: seed.identity.actors[0]!.id, + }); + const entry = { + model: localModel({ + id: "qwen3-coder-30b:Q4_K_M", + name: "Qwen3 Coder 30B", + pi_model: "localmodels-library/qwen3-coder-30b", + state: "loaded", + }), + status: "loaded" as const, + used_by: [{ agent_id: "agent-research", name: "Research agent" }], + }; + + expect(isAgentUsingModel(entry, agent!, "computer-lab")).toBe(true); + expect(isAgentUsingModel(entry, agent!, "computer-other")).toBe(false); + expect(usedByAgentNames(entry, [agent!], "computer-lab")).toEqual([ + "Research agent", + "Jenny's Delegate", + ]); + }); + + it("omits incompatible Claude reasoning efforts when Pi has not listed the model yet, and preserves valid Pi efforts when listed", () => { + const seed = createTeamHubTestSeed(); + const computerWithoutPiModel = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + runtimes: [], + }; + const baseAgent = { + actorId: "actor-delegate", + busy: false, + configurationRevision: 3, + currentComputerId: "computer-lab", + currentModel: "claude-opus-4-1", + currentReasoningEffort: "ultra" as const, + currentRuntimeType: "claude" as const, + grantId: "grant-rw", + isAnotherMe: true, + name: "Jenny's Delegate", + }; + + expect( + buildLocalModelRuntimeUpdate( + baseAgent, + "localmodels-library/qwen3-coder-30b", + "computer-lab", + computerWithoutPiModel, + ), + ).toEqual({ + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-rw", + }, + }); + + const computerWithPiModel: TeamHubLocalComputerPage["items"][number] = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + runtimes: [ + { + models: [ + { + allowed_reasoning_effort: ["low", "medium", "high"], + default_reasoning_effort: "medium", + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + status: "ready", + type: "pi", + }, + ], + }; + + expect( + buildLocalModelRuntimeUpdate( + baseAgent, + "localmodels-library/qwen3-coder-30b", + "computer-lab", + computerWithPiModel, + ), + ).toEqual({ + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-rw", + }, + reasoning_effort: "medium", + }); + + expect( + buildLocalModelRuntimeUpdate( + { + ...baseAgent, + currentReasoningEffort: "high", + currentRuntimeType: "pi", + }, + "localmodels-library/qwen3-coder-30b", + "computer-lab", + computerWithPiModel, + ), + ).toEqual({ + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-rw", + }, + reasoning_effort: "high", + }); + }); +}); diff --git a/apps/web/src/features/local-models/local-model-agents.ts b/apps/web/src/features/local-models/local-model-agents.ts new file mode 100644 index 000000000..e1418492b --- /dev/null +++ b/apps/web/src/features/local-models/local-model-agents.ts @@ -0,0 +1,190 @@ +import type { + TeamHubAgentReasoningEffort, + TeamHubLocalComputerPage, + TeamHubLocalRuntimeType, + TeamHubTenantAgent, + TeamHubTenantAgentAvailabilitySnapshot, + TeamHubTenantAgentUpdateInput, +} from "@/features/teamhub/teamhub-types"; + +import type { ModelEntry } from "./local-models-rules"; + +/** A Sponsor-owned Agent eligible to run against a loaded Local Model on one Computer. */ +export interface AssignableLocalModelAgent { + actorId: string; + busy: boolean; + configurationRevision: number; + currentComputerId?: string; + currentModel: string; + currentReasoningEffort?: TeamHubAgentReasoningEffort; + currentRuntimeType?: TeamHubLocalRuntimeType; + grantId: string; + isAnotherMe: boolean; + name: string; +} + +export interface AssignableLocalModelAgentsInput { + agents?: readonly TeamHubTenantAgent[]; + anotherMeActorId?: string; + availability?: readonly TeamHubTenantAgentAvailabilitySnapshot[]; + computer?: TeamHubLocalComputerPage["items"][number]; + computerId: string; + currentActorId?: string; +} + +/** + * Lists Sponsor-owned Agents that can run on `computerId` with the Pi Runtime, + * placing Another Me first so row actions prioritize it consistently. + */ +export function assignableLocalModelAgents( + input: AssignableLocalModelAgentsInput, +): AssignableLocalModelAgent[] { + if ( + !input.currentActorId || + !input.agents?.length || + input.computer?.upgrade_required + ) { + return []; + } + const busyByActorId = new Map( + (input.availability ?? []).map((snapshot) => [ + snapshot.actor_id, + (snapshot.availability.active_task_count ?? 0) > 0, + ]), + ); + const writableGrant = input.computer?.workspace_grants.find( + (grant) => grant.mode === "workspace-write", + ); + const result: AssignableLocalModelAgent[] = []; + for (const agent of input.agents) { + if (agent.created_by.id !== input.currentActorId) continue; + const placement = + agent.runtime.placement?.type === "local" + ? agent.runtime.placement + : undefined; + let grantId: string | undefined; + if (placement?.computer_id === input.computerId) { + const existingGrantId = placement.workspace_grant_id; + const grantStillPresent = + !input.computer || + input.computer.workspace_grants.some( + (grant) => grant.grant_id === existingGrantId, + ); + grantId = grantStillPresent ? existingGrantId : writableGrant?.grant_id; + } else { + grantId = writableGrant?.grant_id; + } + if (!grantId) continue; + result.push({ + actorId: agent.actor.id, + busy: busyByActorId.get(agent.actor.id) ?? false, + configurationRevision: agent.configuration_revision, + currentComputerId: placement?.computer_id, + currentModel: agent.runtime.model, + currentReasoningEffort: agent.runtime.reasoning_effort, + currentRuntimeType: placement?.runtime_type, + grantId, + isAnotherMe: + agent.actor.id === input.anotherMeActorId || + agent.agent_type === "anotherme", + name: agent.actor.display_name, + }); + } + result.sort((a, b) => { + if (a.isAnotherMe !== b.isAnotherMe) return a.isAnotherMe ? -1 : 1; + return a.name.localeCompare(b.name); + }); + return result; +} + +/** + * Whether `agent` already uses `entry` on `computerId`, combining the snapshot's + * `used_by` list with live Agent placement so updates reflect before the next snapshot. + */ +export function isAgentUsingModel( + entry: ModelEntry, + agent: AssignableLocalModelAgent, + computerId: string, +): boolean { + if (entry.used_by.some((user) => user.agent_id === agent.actorId)) { + return true; + } + return ( + Boolean(entry.model.pi_model) && + agent.currentComputerId === computerId && + agent.currentRuntimeType === "pi" && + agent.currentModel === entry.model.pi_model + ); +} + +/** Display names of all Agents currently using `entry` on `computerId`. */ +export function usedByAgentNames( + entry: ModelEntry, + assignableAgents: readonly AssignableLocalModelAgent[], + computerId: string, +): string[] { + const names = entry.used_by.map((user) => user.name); + for (const agent of assignableAgents) { + if ( + isAgentUsingModel(entry, agent, computerId) && + !entry.used_by.some( + (user) => user.agent_id === agent.actorId || user.name === agent.name, + ) + ) { + names.push(agent.name); + } + } + return names; +} + +/** The featured Agent for one-click row actions: Another Me, or the sole Agent. */ +export function primaryLocalModelAgent( + assignableAgents: readonly AssignableLocalModelAgent[], +): AssignableLocalModelAgent | undefined { + return ( + assignableAgents.find((agent) => agent.isAnotherMe) ?? + (assignableAgents.length === 1 ? assignableAgents[0] : undefined) + ); +} + +/** + * Builds the `PATCH /agents/{agentId}` runtime payload that switches `agent` onto + * a Bifrost-backed local model via Pi without carrying incompatible effort settings. + */ +export function buildLocalModelRuntimeUpdate( + agent: AssignableLocalModelAgent, + piModel: string, + computerId: string, + computer?: TeamHubLocalComputerPage["items"][number], +): NonNullable { + const piRuntime = computer?.runtimes.find((item) => item.type === "pi"); + const catalogModel = piRuntime?.models.find((item) => item.id === piModel); + let reasoningEffort: TeamHubAgentReasoningEffort | undefined; + if (catalogModel) { + const allowed = catalogModel.allowed_reasoning_effort ?? []; + if ( + agent.currentRuntimeType === "pi" && + agent.currentReasoningEffort && + allowed.includes(agent.currentReasoningEffort) + ) { + reasoningEffort = agent.currentReasoningEffort; + } else if ( + catalogModel.default_reasoning_effort && + allowed.includes(catalogModel.default_reasoning_effort) + ) { + reasoningEffort = catalogModel.default_reasoning_effort; + } else { + reasoningEffort = allowed[0]; + } + } + return { + model: piModel, + ...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}), + placement: { + computer_id: computerId, + runtime_type: "pi", + type: "local", + workspace_grant_id: agent.grantId, + }, + }; +} diff --git a/apps/web/src/features/local-models/local-model-details.tsx b/apps/web/src/features/local-models/local-model-details.tsx index e74b9d880..d89b4b335 100644 --- a/apps/web/src/features/local-models/local-model-details.tsx +++ b/apps/web/src/features/local-models/local-model-details.tsx @@ -9,8 +9,16 @@ import { Button } from "@/components/ui/button"; import { FormFeedback } from "@/components/ui/form-feedback"; import { Icon } from "@/components/ui/icon"; import { ModalDialog } from "@/components/ui/modal-dialog"; +import { Select, SelectItem } from "@/components/ui/select"; +import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; import { cn } from "@/lib/cn"; +import { + type AssignableLocalModelAgent, + isAgentUsingModel, + primaryLocalModelAgent, + usedByAgentNames, +} from "./local-model-agents"; import { capabilityCopy, modelErrorCopy, @@ -19,7 +27,6 @@ import { } from "./local-models-copy"; import type { LocalModelPool, LocalModelsView } from "./local-models-queries"; import { - agentNames, contextLabel, downloadStopped, formatGigabytes, @@ -91,12 +98,56 @@ function CopyEndpoint({ endpoint, name }: { endpoint: string; name: string }) { ); } +function gatewayFactValue( + entry: ModelEntry, + view: LocalModelsView, + computer?: TeamHubLocalComputerPage["items"][number], +): ReactNode { + if (!computer || !entry.model.pi_model) return "Bifrost"; + const piRuntime = computer.runtimes.find((runtime) => runtime.type === "pi"); + if (!piRuntime) { + return ( + Bifrost · Pi not installed + ); + } + if (piRuntime.status === "ready") { + if (piRuntime.models.some((item) => item.id === entry.model.pi_model)) { + return "Ready · Bifrost (Pi)"; + } + return ( + + Bifrost · Registering with Pi… + + ); + } + if (piRuntime.status === "not_installed") { + return ( + + {view.computer.status === "online" + ? "Bifrost · Setting up Pi…" + : "Bifrost · Pi not installed"} + + ); + } + return Bifrost · Pi unavailable; +} + /** Everything about one model on one Computer, shown when its row expands. */ export function ModelFacts({ + assignableAgents = [], + assigningAgentId, + computer, + disabled = false, entry, + onAssignAgent, view, }: { + assignableAgents?: readonly AssignableLocalModelAgent[]; + assigningAgentId?: string; + computer?: TeamHubLocalComputerPage["items"][number]; + disabled?: boolean; entry: ModelEntry; + onAssignAgent?: (agent: AssignableLocalModelAgent) => void; view: LocalModelsView; }) { const { model, status } = entry; @@ -117,6 +168,24 @@ export function ModelFacts({ pool !== undefined && poolOtherBytes(pool) > 0 && poolFreeBytes(pool) < model.memory_bytes; + const computerId = view.computer.computer_id; + const usedNames = usedByAgentNames(entry, assignableAgents, computerId); + const unassigned = assignableAgents.filter( + (agent) => !isAgentUsingModel(entry, agent, computerId), + ); + const primaryAgent = primaryLocalModelAgent(assignableAgents); + const showAgentPicker = + status === "loaded" && + Boolean(model.pi_model) && + Boolean(onAssignAgent) && + (unassigned.some((agent) => !agent.isAnotherMe) || unassigned.length > 1); + const primaryBusy = + status === "loaded" && + Boolean(model.pi_model) && + Boolean(onAssignAgent) && + primaryAgent !== undefined && + !isAgentUsingModel(entry, primaryAgent, computerId) && + primaryAgent.busy; return (
{model.error || status === "unavailable" ? ( @@ -199,10 +268,47 @@ export function ModelFacts({ {model.license ? {model.license} : null} {onComputer ? ( 1 ? "@xl:col-span-2" : undefined} + className={ + usedNames.length > 1 || showAgentPicker || primaryBusy + ? "@xl:col-span-2" + : undefined + } label="Used by" > - {entry.used_by.length ? agentNames(entry) : "No Agent yet"} + + + {usedNames.length ? usedNames.join(", ") : "No Agent yet"} + + {showAgentPicker && onAssignAgent ? ( + + ) : primaryBusy ? ( + + Finish the current turn to switch + + ) : null} + ) : null} {sourceUrl ? ( @@ -218,6 +324,11 @@ export function ModelFacts({ ) : null} + {status === "loaded" && model.endpoint ? ( + + {gatewayFactValue(entry, view, computer)} + + ) : null} {model.endpoint ? ( void; - onConfirm: (unloadIds: string[]) => void; + onConfirm: (unloadIds: string[], switchAgentIds: string[]) => void; onRecheck?: () => void; rechecking?: boolean; view: LocalModelsView; }) { const [selected, setSelected] = useState(() => makeRoomSelection(check)); + const [switchAgents, setSwitchAgents] = useState(true); const roomFreeNow = check.freeBytes >= check.needBytes; const effectiveSelected = roomFreeNow ? [] : selected; const chosen = new Set(effectiveSelected); @@ -81,9 +88,19 @@ export function MakeRoomDialog({ ...(otherBytes ? [{ bytes: otherBytes, id: "", name: "Other apps" }] : []), ...check.kept, ]; - const affected = check.loaded.flatMap((item) => - chosen.has(item.model.id) ? item.used_by.map((user) => user.name) : [], + const chosenLoaded = check.loaded.filter((item) => chosen.has(item.model.id)); + const affected = chosenLoaded.flatMap((item) => + item.used_by.map((user) => user.name), + ); + const affectedAssignable = assignableAgents.filter( + (agent) => + !agent.busy && + chosenLoaded.some((item) => + isAgentUsingModel(item, agent, view.computer.computer_id), + ), ); + const canSwitchOnLoad = + Boolean(entry.model.pi_model) && affectedAssignable.length > 0; return ( Cancel
); diff --git a/apps/web/src/features/local-models/local-model-row.tsx b/apps/web/src/features/local-models/local-model-row.tsx index fca89c330..b6f064735 100644 --- a/apps/web/src/features/local-models/local-model-row.tsx +++ b/apps/web/src/features/local-models/local-model-row.tsx @@ -13,11 +13,19 @@ import { QwenIcon, } from "@hugeicons/core-free-icons"; import { useId, type MouseEvent } from "react"; +import { Link } from "react-router"; -import { Button } from "@/components/ui/button"; +import { Button, buttonClassName } from "@/components/ui/button"; import { Icon, type IconSvgElement } from "@/components/ui/icon"; +import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; import { cn } from "@/lib/cn"; +import { + type AssignableLocalModelAgent, + isAgentUsingModel, + primaryLocalModelAgent, + usedByAgentNames, +} from "./local-model-agents"; import { ModelFacts } from "./local-model-details"; import { unavailableShortCopy } from "./local-models-copy"; import type { @@ -26,7 +34,6 @@ import type { LocalModelsView, } from "./local-models-queries"; import { - agentNames, collapsedRowBadges, downloadPercent, downloadStatusDetail, @@ -166,6 +173,28 @@ function stopRowClick(event: MouseEvent) { event.stopPropagation(); } +function OpenChatLink({ + href, + onClick, +}: { + href: string; + onClick: (event: MouseEvent) => void; +}) { + return ( + + Open chat + + ); +} + function RowStatusLine({ detail, download, @@ -232,16 +261,16 @@ function RowStatusLine({ function RowMetadataPills({ badges, - entry, overflowTitle, status, usedBy, + usedByTitle, }: { badges: string[]; - entry: ModelEntry; overflowTitle?: string; status: ModelStatus; usedBy?: string; + usedByTitle?: string; }) { return (
{usedBy} @@ -311,18 +340,28 @@ function DownloadProgress({ * header carries the status and one action, and which expands to the details. */ export function ModelRow({ + anotherMeChatHref, + assignableAgents = [], + assigningAgentId, + computer, disabled, entry, expanded, + onAssignAgent, requested, switchingForAgents = false, view, ...handlers }: Handlers & { + anotherMeChatHref?: string; + assignableAgents?: readonly AssignableLocalModelAgent[]; + assigningAgentId?: string; + computer?: TeamHubLocalComputerPage["items"][number]; /** Actions are off: the Computer is offline, the viewer only reads, or one is pending. */ disabled: boolean; entry: ModelEntry; expanded: boolean; + onAssignAgent?: (agent: AssignableLocalModelAgent) => void; /** An action asked for and not yet in a snapshot. */ requested?: LocalModelAction; switchingForAgents?: boolean; @@ -359,7 +398,21 @@ export function ModelRow({ const slower = status === "available" && !model.recommended && model.fit === "system"; const brand = familyBrand(model.family); - const usedBy = usedByBadge(entry); + const computerId = view.computer.computer_id; + const usedNames = usedByAgentNames(entry, assignableAgents, computerId); + const usedBy = usedByBadge({ ...entry, status }, usedNames); + const usedByTitle = usedNames.length + ? `Used by ${usedNames.join(", ")}` + : undefined; + const primaryAgent = primaryLocalModelAgent(assignableAgents); + const primaryUsing = primaryAgent + ? isAgentUsingModel(entry, primaryAgent, computerId) + : false; + const canUsePrimary = + status === "loaded" && + Boolean(model.pi_model) && + Boolean(primaryAgent) && + Boolean(onAssignAgent); const { badges, overflowTitle } = collapsedRowBadges(model); const hasMeta = Boolean(usedBy) || badges.length > 0; const detail = { @@ -450,10 +503,10 @@ export function ModelRow({ {hasMeta ? ( ) : null}
Use for Agents + ) : canUsePrimary && + primaryAgent && + !primaryUsing && + onAssignAgent ? ( + + ) : !handlers.onUseForAgents && + canUsePrimary && + primaryAgent?.isAnotherMe && + primaryUsing && + anotherMeChatHref ? ( + ) : null} {action ? (