From 320f96a22da76b626637f7e7f124cab7a4edc0ae Mon Sep 17 00:00:00 2001 From: Dongdong Chen Date: Thu, 19 Nov 2026 10:46:49 +0800 Subject: [PATCH] feat(local-models): upgrade UX, agent setup, and model telemetry Task: 20261118-local-models-experience-upgrade --- apps/computer/local-daemon/README.md | 25 +- .../cmd/linger-computer-daemon/main.go | 111 ++- .../cmd/linger-computer-daemon/main_test.go | 97 +++ .../internal/localmodels/fake_tool_test.go | 148 +++- .../internal/localmodels/library.go | 252 ++++++- .../internal/localmodels/library_test.go | 223 ++++++ .../internal/localmodels/pi_runtime.go | 269 +++++++ .../internal/localmodels/pi_runtime_test.go | 679 ++++++++++++++++++ .../internal/localmodels/status.go | 37 +- .../internal/localmodels/status_test.go | 111 +++ .../local-daemon/internal/localmodels/tool.go | 70 +- .../internal/app/handlers_local_models.go | 17 + .../app/handlers_local_models_test.go | 115 +++ apps/web/AGENTS.md | 29 +- .../local-computer/computer-detail.tsx | 16 +- .../local-computer/computer-directory.tsx | 21 + .../local-computer/computers-page.test.tsx | 70 ++ .../local-models/local-model-agents.test.ts | 364 ++++++++++ .../local-models/local-model-agents.ts | 249 +++++++ .../local-models/local-model-details.tsx | 452 +++++++++--- .../local-models/local-model-dialogs.tsx | 63 +- .../features/local-models/local-model-row.tsx | 458 +++++++++--- .../local-models/local-models-page.test.tsx | 528 +++++++++++++- .../local-models/local-models-page.tsx | 598 ++++++++++++++- .../local-models/local-models-rules.test.ts | 443 +++++++++++- .../local-models/local-models-rules.ts | 295 +++++++- .../chat/direct-agent-model-switch.test.tsx | 564 ++++++++++++++- .../rooms/chat/direct-agent-model-switch.tsx | 474 +++++++++++- .../features/rooms/members/actor-profile.tsx | 8 +- .../edit-agent-runtime-dialog.test.tsx | 200 +++++- .../members/edit-agent-runtime-dialog.tsx | 137 +++- .../members/use-agent-model-selection.ts | 81 ++- .../quick-create-general-agent-page.test.tsx | 253 ++++++- .../rooms/quick-create-local-agent-dialog.tsx | 88 ++- .../generated/go/runnergateway/wire.gen.go | 18 + .../go/teamhubinternal/openapi.gen.go | 78 +- .../generated/go/teamhubpublic/openapi.gen.go | 78 +- .../src/generated/runner-gateway/schema.ts | 40 ++ .../src/generated/runner-gateway/types.ts | 17 + .../src/generated/teamhub-internal/types.ts | 19 + .../src/generated/teamhub-public/types.ts | 19 + .../openapi/components/local-models.yaml | 46 ++ contracts/runner-gateway/envelope.schema.json | 40 ++ 43 files changed, 7482 insertions(+), 418 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 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/computer/local-daemon/README.md b/apps/computer/local-daemon/README.md index a2db925ec..5796e07c0 100644 --- a/apps/computer/local-daemon/README.md +++ b/apps/computer/local-daemon/README.md @@ -151,11 +151,26 @@ model. Daemon rereads the status, which carries what the phase reserved or freed (pool use, placement, models unloaded first), and applies the job's line on top; later lines of the phase update that snapshot without another read. -- 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 aligns `PI_CODING_AGENT_DIR` on `localmodels` tool calls with + `workspace.PiSourceEnvironment` (`~/.pi/agent` when unset) so loaded models + land in the same Pi profile that runtime discovery and execution read. When + a snapshot reports a loaded model (or a load completes) on a Computer where Pi + is enabled, unconfigured in `runtime_paths`, and not already discovered + externally, `internal/localmodels` installs the pinned Pi runtime + asynchronously in the background under `state/local-models/harness` (passing + the bundled Node binary via `--node` when available, validating the plan + contract, retrying with exponential backoff on transient failures, and + cancelling/waiting cleanly on `Close()`) before asking the Runtime monitor for + a full discovery with the managed Pi executable and binary directory on + `PATH`. 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`. +- Shared weight cache. On multi-container Computer hosts, mount a shared cache + directory and pass `LINGER_LOCALMODELS_CACHE_DIR` or + `LINGER_LOCALMODELS_REUSE_DIRS` in the Daemon environment so `localmodels` + receives `LOCALMODELS_CACHE_DIR` and `LOCALMODELS_REUSE_DIRS` (when those + `LOCALMODELS_*` variables are not already set) and reuses SHA256-verified + GGUF weights without re-downloading. ## Codex models cache 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 3ee8887a9..deefc8e0a 100644 --- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go +++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go @@ -12,6 +12,7 @@ import ( "os/signal" "path/filepath" "runtime" + "slices" "sort" "strings" "sync" @@ -131,12 +132,32 @@ func run(args []string) (resultErr error) { slog.Info("pruned abandoned Operation temp directories", "removed", removed, "bytes_freed", bytesFreed) } providerEnvironment := runtimeProviderEnvironment(environment, computer.GatewayToken, computer.CodexAuthFile) + var localModelsTool *localmodels.Tool + if tool := computer.LocalModels; tool != nil { + localModelsTool = &localmodels.Tool{ + Python: tool.Python, ModuleDirectory: tool.ModuleDirectory, + StateDirectory: tool.StateDirectory, BaseDirectory: tool.BaseDirectory, + } + } + harnessDirectory := filepath.Join(computer.StateDirectory, "local-models", "harness") + piEnabled := piRuntimeEnabled(computer.EnabledRuntimes) + var managedPi string + if localModelsTool != nil && piEnabled && computer.RuntimePaths["pi"] == "" { + inspectCtx, cancelInspect := context.WithTimeout(context.Background(), 20*time.Second) + found, inspectErr := localModelsTool.InspectPi(inspectCtx, providerEnvironment, harnessDirectory) + cancelInspect() + if inspectErr != nil { + slog.Warn("local_models.pi_inspect_failed", "error", inspectErr) + } else { + managedPi = found + } + } discovered, err := provider.Discover(context.Background(), provider.DiscoveryOptions{ - ConfiguredPaths: computer.RuntimePaths, + ConfiguredPaths: withManagedPiPath(computer.RuntimePaths, managedPi), Runtimes: computer.EnabledRuntimes, PreferredRuntime: computer.PreferredRuntime, RuntimeHomeRoot: filepath.Join(computer.StateDirectory, "runtime-homes"), - Environment: providerEnvironment, + Environment: withManagedPiEnvironment(providerEnvironment, computer.RuntimePaths, managedPi), }) if err != nil { return err @@ -176,7 +197,7 @@ func run(args []string) (resultErr error) { if err != nil { return err } - providers, runtimes := providerInstallations(discovered, providerEnvironment) + providers, runtimes := providerInstallations(discovered, withManagedPiEnvironment(providerEnvironment, computer.RuntimePaths, managedPi)) providerCatalog := lingercontext.NewProviderCatalog(providers) workspaceGrants := make(map[string]lingercontext.WorkspaceGrant) wireGrants := make([]map[string]any, 0, len(computer.WorkspaceGrants)) @@ -285,16 +306,20 @@ func run(args []string) (resultErr error) { // full Runtime discovery. runtimeRefresh := make(chan struct{}, 1) var localDaemon *daemon.Daemon - var localModelsTool *localmodels.Tool - if tool := computer.LocalModels; tool != nil { - localModelsTool = &localmodels.Tool{ - Python: tool.Python, ModuleDirectory: tool.ModuleDirectory, - StateDirectory: tool.StateDirectory, BaseDirectory: tool.BaseDirectory, - } + libraryHarnessDir := "" + if shouldProvisionManagedPi(computer.EnabledRuntimes, computer.RuntimePaths, discovered, managedPi) { + libraryHarnessDir = harnessDirectory + } + var bundledNodePath string + if toolInstallation != nil { + bundledNodePath = toolInstallation.NodePath } localModels := localmodels.New(localmodels.Options{ Tool: localModelsTool, Environment: providerEnvironment, - JobDirectory: filepath.Join(computer.StateDirectory, "local-models", "jobs"), + JobDirectory: filepath.Join(computer.StateDirectory, "local-models", "jobs"), + HarnessDirectory: libraryHarnessDir, + InitialPiExecutable: managedPi, + NodePath: bundledNodePath, // The library publishes only from Act and Refresh, which the Daemon // calls once it runs. Publish: func(snapshot wire.LocalModelsSnapshot) { localDaemon.PublishLocalModels(snapshot) }, @@ -412,9 +437,15 @@ func run(args []string) (resultErr error) { return localDaemon.ActiveOperationCount() > 0 }, Refresh: runtimeRefresh, + Discover: func(discoverCtx context.Context, discoverOptions provider.DiscoveryOptions) (provider.DiscoveryResult, error) { + managed := localModels.PiExecutable() + discoverOptions.Environment = withManagedPiEnvironment(discoverOptions.Environment, discoverOptions.ConfiguredPaths, managed) + discoverOptions.ConfiguredPaths = withManagedPiPath(discoverOptions.ConfiguredPaths, managed) + return provider.Discover(discoverCtx, discoverOptions) + }, OnChanged: func(next provider.DiscoveryResult) error { logRuntimeDiagnostics(next.Diagnostics) - nextProviders, nextRuntimes := providerInstallations(next, providerEnvironment) + nextProviders, nextRuntimes := providerInstallations(next, withManagedPiEnvironment(providerEnvironment, computer.RuntimePaths, localModels.PiExecutable())) providerCatalog.Replace(nextProviders) controller.UpdateDiagnostics(next.Diagnostics) return inventory.publish(ctx, nextRuntimes, next.Diagnostics) @@ -710,11 +741,69 @@ func installedTeamHubCLIBinAt(executable string) string { return directory } +func piRuntimeEnabled(enabledRuntimes []string) bool { + return len(enabledRuntimes) == 0 || slices.Contains(enabledRuntimes, "pi") +} + +func shouldProvisionManagedPi(enabledRuntimes []string, runtimePaths map[string]string, discovered provider.DiscoveryResult, managedPi string) bool { + if !piRuntimeEnabled(enabledRuntimes) || runtimePaths["pi"] != "" { + return false + } + _, hasPi := discovered.Installations["pi"] + return !hasPi || managedPi != "" +} + +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 withManagedPiEnvironment(environment, configuredPaths map[string]string, managed string) map[string]string { + if managed == "" || (configuredPaths["pi"] != "" && configuredPaths["pi"] != managed) { + return environment + } + dir := filepath.Dir(managed) + if dir == "" || dir == "." { + return environment + } + cloned := maps.Clone(environment) + if cloned == nil { + cloned = make(map[string]string, 1) + } + existing := cloned["PATH"] + if existing == "" { + cloned["PATH"] = dir + return cloned + } + entries := filepath.SplitList(existing) + filtered := make([]string, 0, len(entries)+1) + filtered = append(filtered, dir) + cleanDir := filepath.Clean(dir) + for _, entry := range entries { + if entry == "" || filepath.Clean(entry) == cleanDir { + continue + } + filtered = append(filtered, entry) + } + cloned["PATH"] = strings.Join(filtered, string(os.PathListSeparator)) + return cloned +} + func runtimeProviderEnvironment(source map[string]string, gatewayToken, codexAuthFile string) map[string]string { result := computerconfig.RuntimeEnvironment(source, gatewayToken) if codexAuthFile != "" { result["CODEX_HOME"] = filepath.Dir(codexAuthFile) } + if piEnv, err := localworkspace.PiSourceEnvironment(result); err == nil && piEnv["PI_CODING_AGENT_DIR"] != "" { + result["PI_CODING_AGENT_DIR"] = piEnv["PI_CODING_AGENT_DIR"] + } return result } 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 2e525bf2a..84da78e97 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 @@ -16,6 +16,7 @@ import ( "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/computerconfig" "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/linger/assignment" "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/provider" + localworkspace "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace" ) // Extend the transient protocol failure regression through the real discovery @@ -233,3 +234,99 @@ func TestProviderInstallationsKeepDiscoveredContextWindows(t *testing.T) { t.Fatalf("context windows = %v", windows) } } + +func TestRuntimeProviderEnvironmentAlignsPiCodingAgentDir(t *testing.T) { + home := t.TempDir() + wantDefault, err := localworkspace.PiSourceEnvironment(map[string]string{"HOME": home}) + if err != nil { + t.Fatal(err) + } + gotDefault := runtimeProviderEnvironment(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": ""}, "", "") + if gotDefault["PI_CODING_AGENT_DIR"] != wantDefault["PI_CODING_AGENT_DIR"] { + t.Fatalf("default PI_CODING_AGENT_DIR = %q, want %q", gotDefault["PI_CODING_AGENT_DIR"], wantDefault["PI_CODING_AGENT_DIR"]) + } + + custom := filepath.Join(t.TempDir(), "pi-agent") + if err := os.MkdirAll(custom, 0o700); err != nil { + t.Fatal(err) + } + wantCustom, err := localworkspace.PiSourceEnvironment(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": custom + string(filepath.Separator) + "."}) + if err != nil { + t.Fatal(err) + } + gotCustom := runtimeProviderEnvironment(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": custom + string(filepath.Separator) + "."}, "", "") + if gotCustom["PI_CODING_AGENT_DIR"] != wantCustom["PI_CODING_AGENT_DIR"] { + t.Fatalf("custom PI_CODING_AGENT_DIR = %q, want %q", gotCustom["PI_CODING_AGENT_DIR"], wantCustom["PI_CODING_AGENT_DIR"]) + } +} + +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 TestWithManagedPiEnvironmentPrependsBinDirectoryOnPATH(t *testing.T) { + root := t.TempDir() + bin1 := filepath.Join(root, "usr", "bin") + bin2 := filepath.Join(root, "bin") + base := map[string]string{"HOME": root, "PATH": bin1 + string(os.PathListSeparator) + bin2} + managed := filepath.Join(root, "state", "local-models", "harness", "tools", "pi", "1.3.0", "pi") + managedDir := filepath.Dir(managed) + + if got := withManagedPiEnvironment(base, nil, ""); got["PATH"] != base["PATH"] { + t.Fatalf("empty managed path altered PATH: %v", got) + } + if got := withManagedPiEnvironment(base, map[string]string{"pi": filepath.Join(root, "custom", "pi")}, managed); got["PATH"] != base["PATH"] { + t.Fatalf("configured pi path was overridden: %v", got) + } + + updated := withManagedPiEnvironment(base, nil, managed) + want := managedDir + string(os.PathListSeparator) + bin1 + string(os.PathListSeparator) + bin2 + if updated["PATH"] != want { + t.Fatalf("updated PATH = %q, want %q", updated["PATH"], want) + } + if base["PATH"] != bin1+string(os.PathListSeparator)+bin2 { + t.Fatalf("caller environment mutated: %v", base) + } + + deduped := withManagedPiEnvironment(map[string]string{"PATH": bin1 + string(os.PathListSeparator) + managedDir}, map[string]string{"pi": managed}, managed) + if deduped["PATH"] != managedDir+string(os.PathListSeparator)+bin1 { + t.Fatalf("deduped PATH = %q", deduped["PATH"]) + } + + emptyPath := withManagedPiEnvironment(nil, nil, managed) + if emptyPath["PATH"] != managedDir { + t.Fatalf("empty PATH = %q, want %q", emptyPath["PATH"], managedDir) + } +} + +func TestShouldProvisionManagedPi(t *testing.T) { + noPi := provider.DiscoveryResult{Installations: map[string]provider.RuntimeDiagnostic{}} + externalPi := provider.DiscoveryResult{Installations: map[string]provider.RuntimeDiagnostic{"pi": {Type: "pi", ExecutablePath: "/usr/local/bin/pi"}}} + + if shouldProvisionManagedPi([]string{"codex", "claude"}, nil, noPi, "") { + t.Fatal("provisioned Pi when disabled by enabled_runtimes") + } + if shouldProvisionManagedPi(nil, map[string]string{"pi": "/custom/pi"}, noPi, "") { + t.Fatal("provisioned Pi when runtime_paths.pi is configured") + } + if shouldProvisionManagedPi(nil, nil, externalPi, "") { + t.Fatal("provisioned Pi when external Pi was already discovered") + } + if !shouldProvisionManagedPi(nil, nil, noPi, "") { + t.Fatal("did not provision Pi when enabled and absent") + } + if !shouldProvisionManagedPi([]string{"pi"}, nil, externalPi, "/state/local-models/harness/tools/pi/1.3.0/pi") { + t.Fatal("did not keep harness directory when managed Pi was already discovered") + } +} 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 cd21e1a23..39ee693a7 100644 --- a/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go +++ b/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go @@ -43,13 +43,21 @@ type fakeLine struct { func runFakeTool(args []string) int { state := os.Getenv("LOCALMODELS_STATE") - if len(args) < 6 || strings.Join(args[:5], " ") != "-P -s -m localmodels library" { + if len(args) < 6 || strings.Join(args[:4], " ") != "-P -s -m localmodels" { + fmt.Fprintln(os.Stderr, "unexpected invocation", args) + return 3 + } + group := args[4] + if group == "harness" { + return runFakeHarness(state, args[5:]) + } + if group != "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 { @@ -95,6 +103,136 @@ func workingDirectory() string { 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 "" + } + waitOnMarker := func(gateName, enteredName string) { + gate := filepath.Join(state, gateName) + if _, err := os.Stat(gate); err != nil { + return + } + _ = os.WriteFile(filepath.Join(state, enteredName), []byte("1"), 0o600) + deadline := time.Now().Add(10 * time.Second) + for time.Now().Before(deadline) { + if _, statErr := os.Stat(gate); statErr != nil { + break + } + time.Sleep(10 * time.Millisecond) + } + } + root := argValue("--root") + installedPathForRoot := func(baseRoot string) string { + return filepath.Join(baseRoot, "tools", "pi", "1.3.0", "pi") + } + statusJSON := func(baseRoot string, installed bool, source string, argvPrefix []string) string { + if !installed { + return `{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":false,"executable":null,"argvPrefix":[],"source":null}]}` + } + executable := installedPathForRoot(baseRoot) + if source == "host" { + executable = filepath.Join(filepath.Dir(baseRoot), "external", "pi") + } + prefixJSON, _ := json.Marshal(argvPrefix) + if argvPrefix == nil { + prefixJSON = []byte("[]") + } + if source == "" { + return fmt.Sprintf(`{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":true,"executable":%q,"argvPrefix":%s}]}`, executable, prefixJSON) + } + return fmt.Sprintf(`{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":true,"executable":%q,"argvPrefix":%s,"source":%q}]}`, executable, prefixJSON, source) + } + switch verb { + case "status": + if _, err := os.Stat(filepath.Join(state, "harness-status-fail")); err == nil { + if body, readErr := os.ReadFile(filepath.Join(state, "harness-status-error")); readErr == nil { + _, _ = os.Stdout.Write(body) + } + return 1 + } + if _, err := os.Stat(filepath.Join(state, "harness-pi-host")); err == nil { + fmt.Println(statusJSON(root, true, "host", nil)) + return 0 + } + if _, err := os.Stat(filepath.Join(state, "harness-pi-bad-prefix")); err == nil { + fmt.Println(statusJSON(root, true, "managed", []string{"--extra"})) + return 0 + } + _, err := os.Stat(filepath.Join(state, "harness-pi-installed")) + source := "managed" + if _, noSourceErr := os.Stat(filepath.Join(state, "harness-pi-omit-source")); noSourceErr == nil { + source = "" + } + fmt.Println(statusJSON(root, err == nil, source, nil)) + return 0 + case "plan": + waitOnMarker("harness-plan-wait", "harness-plan-entered") + if _, err := os.Stat(filepath.Join(state, "harness-plan-fail")); err == nil { + if body, readErr := os.ReadFile(filepath.Join(state, "harness-plan-error")); readErr == nil { + _, _ = os.Stdout.Write(body) + } + return 1 + } + if argValue("--runtime") != "pi" || root == "" { + return 2 + } + outPath := argValue("--out") + if outPath == "" { + return 2 + } + plan := fmt.Sprintf(`{"schema":"localmodels.harness-plan/v1","id":%q,"runtime":"pi","root":%q}`, strings.Repeat("a", 64), root) + if body, readErr := os.ReadFile(filepath.Join(state, "harness-plan-body")); readErr == nil { + plan = string(body) + } + _ = os.WriteFile(outPath, []byte(plan), 0o600) + fmt.Println(plan) + return 0 + case "apply": + waitOnMarker("harness-apply-wait", "harness-apply-entered") + if _, err := os.Stat(filepath.Join(state, "harness-apply-fail")); err == nil { + if body, readErr := os.ReadFile(filepath.Join(state, "harness-apply-error")); readErr == nil { + _, _ = os.Stdout.Write(body) + } + return 1 + } + if len(args) < 2 { + return 2 + } + planData, readErr := os.ReadFile(args[1]) + if readErr != nil { + return 2 + } + var plan struct { + ID string `json:"id"` + Root string `json:"root"` + } + if json.Unmarshal(planData, &plan) != nil || argValue("--approve") != plan.ID || !hex64Pattern.MatchString(plan.ID) { + return 2 + } + installedPath := installedPathForRoot(plan.Root) + _ = os.WriteFile(filepath.Join(state, "harness-pi-installed"), []byte(installedPath), 0o600) + fmt.Println(`{"kind":"stage","stage":"install","state":"running"}`) + if _, err := os.Stat(filepath.Join(state, "harness-apply-no-status")); err != nil { + fmt.Println(statusJSON(plan.Root, true, "managed", nil)) + } + return 0 + default: + return 3 + } +} + // fakeTool lays out a Tool that runs the fake. type fakeTool struct { t *testing.T @@ -192,7 +330,11 @@ func model(id, state string) map[string]any { } func progressLine(id, phase string, done, total int) json.RawMessage { - return json.RawMessage(fmt.Sprintf(`{"schema":"localmodels.library-event/v1","event":"progress","model":%q,"phase":%q,"done":%d,"total":%d,"bytes_per_second":1048576}`, id, phase, done, total)) + return progressLineExtra(id, phase, done, total, "") +} + +func progressLineExtra(id, phase string, done, total int, extra string) json.RawMessage { + return json.RawMessage(fmt.Sprintf(`{"schema":"localmodels.library-event/v1","event":"progress","model":%q,"phase":%q,"done":%d,"total":%d,"bytes_per_second":1048576%s}`, id, phase, done, total, extra)) } func resultLine(id string) json.RawMessage { diff --git a/apps/computer/local-daemon/internal/localmodels/library.go b/apps/computer/local-daemon/internal/localmodels/library.go index 58aee26a9..7c4dccab4 100644 --- a/apps/computer/local-daemon/internal/localmodels/library.go +++ b/apps/computer/local-daemon/internal/localmodels/library.go @@ -11,6 +11,7 @@ import ( "log/slog" "os" "path/filepath" + "slices" "sort" "strings" "sync" @@ -23,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 = 5 * 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 @@ -40,16 +44,25 @@ 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 + // InitialPiExecutable seeds a Pi executable already found in HarnessDirectory. + InitialPiExecutable 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) - // RuntimeChanged is told that a model was loaded or unloaded, which - // changes the model list Pi reports. + // RuntimeChanged is told that a model was loaded or unloaded, or that Pi + // was provisioned, which changes the runtime diagnostics reported to TeamHub. 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. @@ -71,31 +84,43 @@ 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 - lastProgress time.Time - progressTimer *time.Timer + loaded *string + piExecutable string + piEnsuring bool + piFailures int + piRetryAt time.Time + piRetryTimer *time.Timer + piRetryTimerID uint64 + jobs map[string]*job + lastProgress time.Time + progressTimer *time.Timer // progressTimerID tells a timer callback that already fired whether it // is still the pending one. progressTimerID uint64 @@ -111,8 +136,11 @@ func New(options Options) *Library { ctx, stop := context.WithCancel(context.Background()) library := &Library{ tool: options.Tool, environment: options.Environment, jobDirectory: options.JobDirectory, - publish: options.Publish, runtimeChanged: options.RuntimeChanged, + harnessDirectory: options.HarnessDirectory, piExecutable: options.InitialPiExecutable, + 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), } if library.progressInterval <= 0 { @@ -124,6 +152,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) {} } @@ -137,31 +171,153 @@ func New(options Options) *Library { return library } -// Close stops reading job output and polling. Running jobs continue. +// Close stops reading job output, polling, and any background Pi install. +// Running model jobs continue. func (l *Library) Close() { l.stop() l.mu.Lock() + l.closed = true if l.progressTimer != nil { l.progressTimer.Stop() + l.progressTimer = nil + } + l.piRetryTimerID++ + if l.piRetryTimer != nil { + l.piRetryTimer.Stop() + l.piRetryTimer = nil } 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 +} + +func (l *Library) scheduleEnsurePi() { + if l.harnessDirectory == "" || l.tool == nil || !l.tool.installed() { + return + } + l.mu.Lock() + defer l.mu.Unlock() + if l.closed || l.ctx.Err() != nil || l.piExecutable != "" || l.piEnsuring { + return + } + if !l.piRetryAt.IsZero() && l.now().Before(l.piRetryAt) { + return + } + l.startEnsurePiLocked() +} + +func (l *Library) retryEnsurePi(id uint64) { + if l.harnessDirectory == "" || l.tool == nil || !l.tool.installed() { + return + } + l.mu.Lock() + defer l.mu.Unlock() + if l.closed || l.ctx.Err() != nil || l.piRetryTimer == nil || l.piRetryTimerID != id { + return + } + l.piRetryTimer = nil + if l.piExecutable != "" || l.piEnsuring || l.loaded == nil || *l.loaded == "" { + return + } + if !l.piRetryAt.IsZero() && l.now().Before(l.piRetryAt) { + return + } + l.startEnsurePiLocked() +} + +// cancelPiRetryLocked stops any scheduled Pi install retry and resets backoff. +// It runs with l.mu held. +func (l *Library) cancelPiRetryLocked() { + l.piRetryTimerID++ + if l.piRetryTimer != nil { + l.piRetryTimer.Stop() + l.piRetryTimer = nil + } + l.piRetryAt = time.Time{} + l.piFailures = 0 +} + +// startEnsurePiLocked spawns one background Pi install attempt. It runs with l.mu held. +func (l *Library) startEnsurePiLocked() { + l.piRetryTimerID++ + if l.piRetryTimer != nil { + l.piRetryTimer.Stop() + l.piRetryTimer = nil + } + l.piEnsuring = true + l.piWg.Add(1) + go l.runEnsurePi() +} + +func (l *Library) runEnsurePi() { + 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.cancelPiRetryLocked() + l.mu.Unlock() + slog.Info("local_models.pi_ready", "executable", executable) + l.runtimeChanged() + return + } + l.piFailures++ + backoff := min(l.ensurePiRetryInterval, maxEnsurePiRetryInterval) + for range l.piFailures - 1 { + if backoff >= maxEnsurePiRetryInterval { + break + } + backoff = min(backoff*2, maxEnsurePiRetryInterval) + } + l.piRetryAt = l.now().Add(backoff) + if l.loaded != nil && *l.loaded != "" { + l.piRetryTimerID++ + id := l.piRetryTimerID + l.piRetryTimer = time.AfterFunc(backoff, func() { l.retryEnsurePi(id) }) + } + l.mu.Unlock() + if err != nil { + slog.Warn("local_models.pi_install_failed", "error", err, "retry_in", backoff) + } else { + slog.Warn("local_models.pi_install_incomplete", "retry_in", backoff) + } } // Refresh reads the library now, publishes the snapshot and returns it. func (l *Library) Refresh(ctx context.Context) wire.LocalModelsSnapshot { l.refreshMu.Lock() - defer l.refreshMu.Unlock() snapshot := l.read(ctx) if ctx.Err() != nil { // A read cut short by its caller says nothing about the library. + l.refreshMu.Unlock() return snapshot } changed := false + hasLoaded := false l.mu.Lock() if snapshot.Available { key := loadedKey(snapshot) + hasLoaded = key != "" changed = l.loaded != nil && *l.loaded != key l.loaded = &key + if !hasLoaded { + l.cancelPiRetryLocked() + } } l.mu.Unlock() l.commit(snapshot) @@ -171,6 +327,10 @@ func (l *Library) Refresh(ctx context.Context) wire.LocalModelsSnapshot { l.runtimeChanged() } l.reconcile(snapshot) + l.refreshMu.Unlock() + if hasLoaded { + l.scheduleEnsurePi() + } return cloneSnapshot(snapshot) } @@ -536,6 +696,9 @@ func (l *Library) superviseJob(action Action, events <-chan libraryEvent, ended l.Refresh(l.ctx) if action.Name == ActionLoad { l.runtimeChanged() + if final != nil && final.Event == "result" { + l.scheduleEnsurePi() + } } } @@ -643,10 +806,17 @@ func applyProgress(snapshot *wire.LocalModelsSnapshot, event libraryEvent) bool if event.Phase == "download" { rate := bytesValue(event.BytesPerSecond) progress.BytesPerSecond = &rate + if slices.Contains(downloadSources, event.Source) { + progress.Source = event.Source + } + if event.Connections != nil && *event.Connections >= 0 && *event.Connections <= 64 { + connections := uint64(*event.Connections) + progress.Connections = &connections + } } - model.State, model.Download, model.Endpoint, model.Error, model.Note = "downloading", progress, nil, nil, nil + model.State, model.Download, model.Load, model.Endpoint, model.Error, model.Note = "downloading", progress, nil, nil, nil, nil case "load", "warm": - model.State, model.Download, model.Endpoint, model.Error, model.Note = "loading", nil, nil, nil, nil + model.State, model.Download, model.Load, model.Endpoint, model.Error, model.Note = "loading", nil, buildLoadProgress(event), nil, nil, nil default: return false } @@ -655,6 +825,32 @@ func applyProgress(snapshot *wire.LocalModelsSnapshot, event libraryEvent) bool return false } +func buildLoadProgress(event libraryEvent) *wire.LocalModelLoadProgress { + if event.Phase == "warm" { + total := uint64(7) + done := min(total, uint64(5)+bytesValue(event.Done)) + return &wire.LocalModelLoadProgress{Step: "warm", DoneSteps: done, TotalSteps: total} + } + rawTotal := bytesValue(event.Total) + if rawTotal == 0 { + rawTotal = 5 + } + total := min(uint64(100), rawTotal+2) + done := min(total, bytesValue(event.Done)) + step := event.LoadStep + if !slices.Contains(loadSteps, step) { + switch { + case done < 3: + step = "prepare" + case done == 3: + step = "start" + default: + step = "load" + } + } + return &wire.LocalModelLoadProgress{Step: step, DoneSteps: done, TotalSteps: total} +} + func cloneSnapshot(snapshot wire.LocalModelsSnapshot) wire.LocalModelsSnapshot { encoded, err := json.Marshal(snapshot) if err != nil { diff --git a/apps/computer/local-daemon/internal/localmodels/library_test.go b/apps/computer/local-daemon/internal/localmodels/library_test.go index 8b9ca26ea..dfce33363 100644 --- a/apps/computer/local-daemon/internal/localmodels/library_test.go +++ b/apps/computer/local-daemon/internal/localmodels/library_test.go @@ -622,3 +622,226 @@ func TestRefreshMapsRichModelCardMetadataAndKeepsItAcrossProgress(t *testing.T) t.Fatalf("progress dropped model card fields: %+v", after) } } + +func TestApplyProgressDownloadSourceConnectionsAndLoadSteps(t *testing.T) { + intPtr := func(v int) *int { return &v } + rawModel := json.RawMessage(`"` + testModel + `"`) + newSnapshot := func() wire.LocalModelsSnapshot { + return wire.LocalModelsSnapshot{ + Available: true, + Models: []wire.LocalModel{{ + ID: testModel, Name: "Qwen3 4B", Quant: "Q4_K_M", Fit: "gpu", Pool: "gpu:0", State: "absent", + Availability: wire.LocalModelAvailability{Downloadable: true}, + Load: &wire.LocalModelLoadProgress{Step: "prepare", DoneSteps: 1, TotalSteps: 7}, + }}, + } + } + + t.Run("download sets source and connections and clears load", func(t *testing.T) { + snap := newSnapshot() + if !applyProgress(&snap, libraryEvent{ + Event: "progress", Model: rawModel, Phase: "download", + Done: 100, Total: 1000, BytesPerSecond: 500, + Source: "modelscope", Connections: intPtr(16), + }) { + t.Fatal("applyProgress returned false") + } + got := modelState(snap, testModel) + if got.State != "downloading" || got.Load != nil || got.Download == nil || + got.Download.Source != "modelscope" || got.Download.Connections == nil || *got.Download.Connections != 16 { + t.Fatalf("download model = %+v", got) + } + + // Out-of-contract source or connections are omitted; verify omits source/connections. + if !applyProgress(&snap, libraryEvent{ + Event: "progress", Model: rawModel, Phase: "download", + Done: 200, Total: 1000, Source: "unknown", Connections: intPtr(65), + }) { + t.Fatal("applyProgress returned false") + } + got = modelState(snap, testModel) + if got.Download.Source != "" || got.Download.Connections != nil { + t.Fatalf("out-of-contract download fields leaked: %+v", got.Download) + } + if !applyProgress(&snap, libraryEvent{ + Event: "progress", Model: rawModel, Phase: "download", + Done: 300, Total: 1000, Source: "cache", Connections: intPtr(-1), + }) { + t.Fatal("applyProgress returned false") + } + got = modelState(snap, testModel) + if got.Download.Source != "cache" || got.Download.Connections != nil { + t.Fatalf("cache download = %+v", got.Download) + } + + if !applyProgress(&snap, libraryEvent{ + Event: "progress", Model: rawModel, Phase: "verify", + Done: 1000, Total: 1000, Source: "huggingface", Connections: intPtr(8), + }) { + t.Fatal("applyProgress returned false for verify") + } + got = modelState(snap, testModel) + if got.Download.Step != "verify" || got.Download.Source != "" || got.Download.Connections != nil || got.Download.BytesPerSecond != nil { + t.Fatalf("verify progress = %+v", got.Download) + } + }) + + t.Run("load phases infer or preserve load_step and clamp bounds", func(t *testing.T) { + snap := newSnapshot() + for _, tc := range []struct { + name string + event libraryEvent + wantStep string + wantDone uint64 + wantTotal uint64 + }{ + { + name: "load done < 3 infers prepare and defaults zero total", + event: libraryEvent{Event: "progress", Model: rawModel, Phase: "load", Done: 2, Total: 0}, + wantStep: "prepare", + wantDone: 2, + wantTotal: 7, + }, + { + name: "load done == 3 infers start", + event: libraryEvent{Event: "progress", Model: rawModel, Phase: "load", Done: 3, Total: 5}, + wantStep: "start", + wantDone: 3, + wantTotal: 7, + }, + { + name: "load done > 3 infers load", + event: libraryEvent{Event: "progress", Model: rawModel, Phase: "load", Done: 4, Total: 5}, + wantStep: "load", + wantDone: 4, + wantTotal: 7, + }, + { + name: "explicit load_step overrides inference", + event: libraryEvent{Event: "progress", Model: rawModel, Phase: "load", Done: 1, Total: 5, LoadStep: "start"}, + wantStep: "start", + wantDone: 1, + wantTotal: 7, + }, + { + name: "unknown load_step falls back to inference", + event: libraryEvent{Event: "progress", Model: rawModel, Phase: "load", Done: 5, Total: 150, LoadStep: "invalid"}, + wantStep: "load", + wantDone: 5, + wantTotal: 100, + }, + { + name: "load done clamps to total", + event: libraryEvent{Event: "progress", Model: rawModel, Phase: "load", Done: 20, Total: 4}, + wantStep: "load", + wantDone: 6, + wantTotal: 6, + }, + { + name: "warm phase maps to 5+done out of 7", + event: libraryEvent{Event: "progress", Model: rawModel, Phase: "warm", Done: 1, Total: 2}, + wantStep: "warm", + wantDone: 6, + wantTotal: 7, + }, + { + name: "warm phase clamps done to 7", + event: libraryEvent{Event: "progress", Model: rawModel, Phase: "warm", Done: 10, Total: 2}, + wantStep: "warm", + wantDone: 7, + wantTotal: 7, + }, + } { + t.Run(tc.name, func(t *testing.T) { + if !applyProgress(&snap, tc.event) { + t.Fatal("applyProgress returned false") + } + got := modelState(snap, testModel) + if got.State != "loading" || got.Download != nil || got.Load == nil { + t.Fatalf("loading model = %+v", got) + } + if got.Load.Step != tc.wantStep || got.Load.DoneSteps != tc.wantDone || got.Load.TotalSteps != tc.wantTotal { + t.Fatalf("load progress = %+v, want step=%q %d/%d", got.Load, tc.wantStep, tc.wantDone, tc.wantTotal) + } + }) + } + }) +} + +func TestDownloadAndLoadEventsReachPublishedSnapshots(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "absent"))) + idleStatus, _ := json.Marshal(status(model(testModel, "idle"))) + fake.script("download", fakeScript{ + Lines: []fakeLine{ + {Line: progressLineExtra(testModel, "download", 100, 1000, `,"source":"huggingface","connections":8`)}, + {Line: progressLine(testModel, "verify", 1000, 1000), DelayMS: 150}, + {Line: resultLine(testModel), DelayMS: 150}, + }, + StatusAfter: idleStatus, + }) + record := &recorder{} + library := newLibrary(t, fake, record, 10*time.Millisecond) + library.Refresh(context.Background()) + + if refusal, answered := library.Act(context.Background(), Action{Name: ActionDownload, Model: testModel}, soon()); refusal != nil || !answered { + t.Fatalf("download refusal=%+v answered=%v", refusal, answered) + } + waitFor(t, "download finished", func() bool { + m := modelState(record.last(), testModel) + return m != nil && m.State == "idle" + }) + + record.mu.Lock() + var sawSourceAndConnections, sawVerify bool + for _, snap := range record.snapshots { + m := modelState(snap, testModel) + if m != nil && m.Download != nil { + if m.Download.Step == "download" && m.Download.Source == "huggingface" && m.Download.Connections != nil && *m.Download.Connections == 8 { + sawSourceAndConnections = true + } + if m.Download.Step == "verify" && m.Download.Source == "" && m.Download.Connections == nil { + sawVerify = true + } + } + } + record.mu.Unlock() + if !sawSourceAndConnections || !sawVerify { + t.Fatalf("missing expected download snapshots: sourceAndConnections=%v verify=%v", sawSourceAndConnections, sawVerify) + } + + loadedStatus, _ := json.Marshal(status(model(testModel, "loaded"))) + fake.script("load", fakeScript{ + Lines: []fakeLine{ + {Line: progressLineExtra(testModel, "load", 1, 5, `,"load_step":"prepare"`)}, + {Line: progressLine(testModel, "warm", 1, 2), DelayMS: 150}, + {Line: resultLine(testModel), DelayMS: 150}, + }, + StatusAfter: loadedStatus, + }) + 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) + } + waitFor(t, "load finished", func() bool { + m := modelState(record.last(), testModel) + return m != nil && m.State == "loaded" + }) + + record.mu.Lock() + var sawPrepare, sawWarm bool + for _, snap := range record.snapshots { + m := modelState(snap, testModel) + if m != nil && m.Load != nil { + if m.Load.Step == "prepare" && m.Load.DoneSteps == 1 && m.Load.TotalSteps == 7 && m.Download == nil { + sawPrepare = true + } + if m.Load.Step == "warm" && m.Load.DoneSteps == 6 && m.Load.TotalSteps == 7 && m.Download == nil { + sawWarm = true + } + } + } + record.mu.Unlock() + if !sawPrepare || !sawWarm { + t.Fatalf("missing expected load snapshots: prepare=%v warm=%v", sawPrepare, sawWarm) + } +} 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..dac208d34 --- /dev/null +++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime.go @@ -0,0 +1,269 @@ +package localmodels + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "log/slog" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "time" + + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/platform/privatefs" + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/process" +) + +const ( + harnessStatusSchema = "localmodels.harness-status/v1" + harnessPlanSchema = "localmodels.harness-plan/v1" + harnessErrorSchema = "localmodels.harness-error/v1" + maxHarnessOutput = 4 << 20 +) + +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"` + Source string `json:"source"` + } `json:"runtimes"` +} + +type harnessPlanDoc struct { + Schema string `json:"schema"` + ID string `json:"id"` + Runtime string `json:"runtime"` +} + +type harnessErrorDoc struct { + Schema string `json:"schema"` + Code string `json:"code"` +} + +// InspectPi reports the verified Pi executable managed under harnessDir, or an +// empty string when no managed Pi runtime is installed there yet. +func (t Tool) InspectPi(ctx context.Context, environment map[string]string, harnessDir string) (string, error) { + if harnessDir == "" || !t.installed() { + return "", nil + } + harnessDir = filepath.Clean(harnessDir) + if !filepath.IsAbs(harnessDir) { + return "", errors.New("harness root must be absolute") + } + 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") + } + stdout, exitCode, err := t.runHarness( + ctx, + environment, + []string{"-P", "-s", "-m", "localmodels", "harness", "status", "--root", harnessDir, "--runtime", "pi", "--json"}, + ) + if err != nil { + return "", err + } + if exitCode != 0 { + return "", harnessCommandError("status", exitCode, stdout) + } + return parsePiFromHarnessStatus(stdout, harnessDir, true) +} + +// EnsurePi returns a ready Pi executable provisioned via localmodels harness +// under harnessDir, planning and applying the pinned Pi install when needed. +// When nodePath names an existing regular non-symlink file, it is passed via +// --node so harness does not 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 + } + harnessDir = filepath.Clean(harnessDir) + if !filepath.IsAbs(harnessDir) { + return "", errors.New("harness root must be absolute") + } + 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 != "" && filepath.IsAbs(nodePath) { + if info, statErr := os.Lstat(nodePath); statErr == nil && info.Mode().IsRegular() { + planArgs = append(planArgs, "--node", nodePath) + } + } + planArgs = append(planArgs, "--out", planPath, "--json") + if stdout, exitCode, err := t.runHarness(ctx, environment, planArgs); err != nil { + return "", err + } else if exitCode != 0 { + return "", harnessCommandError("plan", exitCode, stdout) + } + 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") + } + stdout, exitCode, err := t.runHarness( + ctx, + environment, + []string{"-P", "-s", "-m", "localmodels", "harness", "apply", planPath, "--approve", plan.ID, "--jsonl"}, + ) + if err != nil { + return "", err + } + if exitCode != 0 { + return "", harnessCommandError("apply", exitCode, stdout) + } + if executable, parseErr := parseLastHarnessStatus(stdout, harnessDir, false); parseErr == nil && executable != "" { + return executable, nil + } + statusStdout, statusExit, statusErr := t.runHarness( + ctx, + environment, + []string{"-P", "-s", "-m", "localmodels", "harness", "status", "--root", harnessDir, "--runtime", "pi", "--json"}, + ) + if statusErr != nil { + return "", statusErr + } + if statusExit != 0 { + return "", harnessCommandError("status", statusExit, statusStdout) + } + return parsePiFromHarnessStatus(statusStdout, harnessDir, false) +} + +func (t Tool) runHarness(ctx context.Context, environment map[string]string, args []string) ([]byte, int, error) { + stdout, err := os.CreateTemp("", "localmodels-harness-*.out") + if err != nil { + return nil, -1, err + } + defer func() { _ = os.Remove(stdout.Name()) }() + stderr, err := os.CreateTemp("", "localmodels-harness-*.err") + if err != nil { + _ = stdout.Close() + return nil, -1, err + } + defer func() { _ = os.Remove(stderr.Name()) }() + cmd := t.commandArgs(environment, args) + cmd.Stdout, cmd.Stderr = stdout, stderr + runErr := process.Run(ctx, cmd, time.Second) + _ = stdout.Close() + _ = stderr.Close() + if ctx.Err() != nil { + return nil, -1, context.Cause(ctx) + } + var exitErr *exec.ExitError + if runErr != nil && !errors.As(runErr, &exitErr) { + return nil, -1, runErr + } + if cmd.ProcessState == nil { + return nil, -1, runErr + } + exitCode := cmd.ProcessState.ExitCode() + output, readErr := readBounded(stdout.Name(), maxHarnessOutput) + if exitCode != 0 { + if diagnostic, diagErr := readBounded(stderr.Name(), 4096); diagErr == nil && len(diagnostic) > 0 { + slog.Debug("local_models.harness_stderr", "stderr", string(diagnostic)) + } + } + return output, exitCode, readErr +} + +func harnessCommandError(action string, exitCode int, stdout []byte) 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 + } + var doc harnessErrorDoc + if json.Unmarshal(line, &doc) == nil && doc.Schema == harnessErrorSchema && doc.Code != "" { + return fmt.Errorf("harness %s failed (%s)", action, doc.Code) + } + break + } + return fmt.Errorf("harness %s exited with code %d", action, exitCode) +} + +func parseLastHarnessStatus(stdout []byte, harnessDir string, managedOnly bool) (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, harnessDir, managedOnly) + } + return "", errors.New("harness output is empty") +} + +func parsePiFromHarnessStatus(encoded []byte, harnessDir string, managedOnly bool) (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 + } + if managedOnly { + if row.Source != "" && row.Source != "managed" { + return "", nil + } + if row.Source == "" && !isWithinDirectory(harnessDir, row.Executable) { + return "", nil + } + } + return row.Executable, nil + } + return "", nil +} + +func isWithinDirectory(dir, target string) bool { + if rel, err := filepath.Rel(filepath.Clean(dir), filepath.Clean(target)); err == nil && + rel != "." && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + return true + } + resolvedDir, errDir := filepath.EvalSymlinks(dir) + resolvedTarget, errTarget := filepath.EvalSymlinks(target) + if errDir != nil || errTarget != nil { + return false + } + rel, err := filepath.Rel(resolvedDir, resolvedTarget) + return err == nil && rel != "." && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) +} 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..caff6c4e8 --- /dev/null +++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go @@ -0,0 +1,679 @@ +package localmodels + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "runtime" + "slices" + "strings" + "sync" + "testing" + "time" + + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace" +) + +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") + if err := os.MkdirAll(explicit, 0o700); err != nil { + t.Fatal(err) + } + + defaultInput := map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": ""} + wantDefaultEnv, err := workspace.PiSourceEnvironment(defaultInput) + if err != nil { + t.Fatal(err) + } + cmdDefault := fake.tool.command(defaultInput, Action{Name: ActionRefresh}) + wantDefault := "PI_CODING_AGENT_DIR=" + wantDefaultEnv["PI_CODING_AGENT_DIR"] + if !slices.Contains(cmdDefault.Env, wantDefault) { + t.Fatalf("expected %q in env %v", wantDefault, cmdDefault.Env) + } + + messyExplicit := explicit + string(filepath.Separator) + "." + string(filepath.Separator) + wantExplicitEnv, err := workspace.PiSourceEnvironment(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": messyExplicit}) + if err != nil { + t.Fatal(err) + } + cmdExplicit := fake.tool.command(map[string]string{"HOME": home, "pi_coding_agent_dir": messyExplicit}, Action{Name: ActionRefresh}) + wantExplicit := "PI_CODING_AGENT_DIR=" + wantExplicitEnv["PI_CODING_AGENT_DIR"] + if !slices.Contains(cmdExplicit.Env, wantExplicit) { + t.Fatalf("expected %q in env %v", wantExplicit, cmdExplicit.Env) + } +} + +func TestToolCommandForwardsLocalModelsCacheAndReuseEnv(t *testing.T) { + fake := newFakeTool(t) + home := filepath.Join(filepath.Dir(fake.state), "home") + envMap := func(cmdEnv []string) map[string]string { + out := make(map[string]string, len(cmdEnv)) + for _, entry := range cmdEnv { + if key, value, ok := strings.Cut(entry, "="); ok { + out[key] = value + } + } + return out + } + + t.Run("maps LINGER_LOCALMODELS_* when LOCALMODELS_* unset or blank", func(t *testing.T) { + cmd := fake.tool.command(map[string]string{ + "HOME": home, + "LOCALMODELS_CACHE_DIR": " ", + "LOCALMODELS_REUSE_DIRS": "", + "LINGER_LOCALMODELS_CACHE_DIR": " /shared/models/cache ", + "LINGER_LOCALMODELS_REUSE_DIRS": " /shared/models/a:/shared/models/b ", + }, Action{Name: ActionDownload, Model: testModel}) + got := envMap(cmd.Env) + if got["LOCALMODELS_CACHE_DIR"] != "/shared/models/cache" { + t.Fatalf("LOCALMODELS_CACHE_DIR = %q, want /shared/models/cache", got["LOCALMODELS_CACHE_DIR"]) + } + if got["LOCALMODELS_REUSE_DIRS"] != "/shared/models/a:/shared/models/b" { + t.Fatalf("LOCALMODELS_REUSE_DIRS = %q, want /shared/models/a:/shared/models/b", got["LOCALMODELS_REUSE_DIRS"]) + } + if _, ok := got["LINGER_LOCALMODELS_CACHE_DIR"]; !ok { + t.Fatalf("LINGER_LOCALMODELS_CACHE_DIR was dropped from cmd.Env") + } + if _, ok := got["LINGER_LOCALMODELS_REUSE_DIRS"]; !ok { + t.Fatalf("LINGER_LOCALMODELS_REUSE_DIRS was dropped from cmd.Env") + } + }) + + t.Run("does not override explicit LOCALMODELS_* settings", func(t *testing.T) { + cmd := fake.tool.command(map[string]string{ + "HOME": home, + "LOCALMODELS_CACHE_DIR": "/explicit/cache", + "LOCALMODELS_REUSE_DIRS": "/explicit/reuse", + "LINGER_LOCALMODELS_CACHE_DIR": "/linger/cache", + "LINGER_LOCALMODELS_REUSE_DIRS": "/linger/reuse", + }, Action{Name: ActionRefresh}) + got := envMap(cmd.Env) + if got["LOCALMODELS_CACHE_DIR"] != "/explicit/cache" { + t.Fatalf("LOCALMODELS_CACHE_DIR = %q, want /explicit/cache", got["LOCALMODELS_CACHE_DIR"]) + } + if got["LOCALMODELS_REUSE_DIRS"] != "/explicit/reuse" { + t.Fatalf("LOCALMODELS_REUSE_DIRS = %q, want /explicit/reuse", got["LOCALMODELS_REUSE_DIRS"]) + } + if got["LINGER_LOCALMODELS_CACHE_DIR"] != "/linger/cache" || got["LINGER_LOCALMODELS_REUSE_DIRS"] != "/linger/reuse" { + t.Fatalf("LINGER_LOCALMODELS_* were altered: %+v", got) + } + }) + + t.Run("skips empty fallback values", func(t *testing.T) { + cmd := fake.tool.command(map[string]string{ + "HOME": home, + "LINGER_LOCALMODELS_CACHE_DIR": " ", + "LINGER_LOCALMODELS_REUSE_DIRS": "", + }, Action{Name: ActionRefresh}) + got := envMap(cmd.Env) + if value, ok := got["LOCALMODELS_CACHE_DIR"]; ok { + t.Fatalf("unexpected LOCALMODELS_CACHE_DIR = %q", value) + } + if value, ok := got["LOCALMODELS_REUSE_DIRS"]; ok { + t.Fatalf("unexpected LOCALMODELS_REUSE_DIRS = %q", value) + } + }) +} + +func TestInspectPiReportsManagedOnlyAndSkipsMissingRoot(t *testing.T) { + fake := newFakeTool(t) + env := map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")} + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + + found, err := fake.tool.InspectPi(context.Background(), env, harnessDir) + if err != nil || found != "" { + t.Fatalf("missing harness dir InspectPi = (%q, %v)", found, err) + } + if _, statErr := os.Stat(filepath.Join(fake.state, "calls.log")); !os.IsNotExist(statErr) { + t.Fatalf("missing harness dir invoked localmodels: %v", fake.calls()) + } + + if err := os.MkdirAll(harnessDir, 0o700); err != nil { + t.Fatal(err) + } + + hostMarker := filepath.Join(fake.state, "harness-pi-host") + if err := os.WriteFile(hostMarker, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + found, err = fake.tool.InspectPi(context.Background(), env, harnessDir) + if err != nil || found != "" { + t.Fatalf("host-sourced Pi should be ignored by InspectPi: (%q, %v)", found, err) + } + _ = os.Remove(hostMarker) + + prefixMarker := filepath.Join(fake.state, "harness-pi-bad-prefix") + if err := os.WriteFile(prefixMarker, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + found, err = fake.tool.InspectPi(context.Background(), env, harnessDir) + if err != nil || found != "" { + t.Fatalf("non-empty argvPrefix should be rejected: (%q, %v)", found, err) + } + _ = os.Remove(prefixMarker) + + installedMarker := filepath.Join(fake.state, "harness-pi-installed") + if err := os.WriteFile(installedMarker, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + omitSourceMarker := filepath.Join(fake.state, "harness-pi-omit-source") + if err := os.WriteFile(omitSourceMarker, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + found, err = fake.tool.InspectPi(context.Background(), env, harnessDir) + if err != nil || found != want { + t.Fatalf("InspectPi without source inside harnessDir = (%q, %v), want %q", found, err, want) + } + _ = os.Remove(omitSourceMarker) + + found, err = fake.tool.InspectPi(context.Background(), env, harnessDir) + if err != nil || found != want { + t.Fatalf("InspectPi managed = (%q, %v), want %q", found, err, want) + } +} + +func TestEnsurePiValidatesPlanContractAndNodeBootstrap(t *testing.T) { + env := map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")} + for _, tc := range []struct { + name string + body string + }{ + {name: "bad schema", body: `{"schema":"localmodels.harness-plan/v0","id":"` + strings.Repeat("a", 64) + `","runtime":"pi"}`}, + {name: "wrong runtime", body: `{"schema":"localmodels.harness-plan/v1","id":"` + strings.Repeat("a", 64) + `","runtime":"codex"}`}, + {name: "non-hex id", body: `{"schema":"localmodels.harness-plan/v1","id":"not-64-hex","runtime":"pi"}`}, + {name: "uppercase hex id", body: `{"schema":"localmodels.harness-plan/v1","id":"` + strings.Repeat("A", 64) + `","runtime":"pi"}`}, + } { + t.Run(tc.name, func(t *testing.T) { + fake := newFakeTool(t) + if err := os.WriteFile(filepath.Join(fake.state, "harness-plan-body"), []byte(tc.body), 0o600); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + if got, err := fake.tool.EnsurePi(context.Background(), env, harnessDir, ""); err == nil || got != "" { + t.Fatalf("expected contract validation failure, got (%q, %v)", got, err) + } + if slices.ContainsFunc(fake.calls(), func(c string) bool { return strings.HasPrefix(c, "harness apply ") }) { + t.Fatalf("invalid plan still invoked harness apply: %v", fake.calls()) + } + }) + } + + t.Run("structured error from apply", func(t *testing.T) { + fake := newFakeTool(t) + if err := os.WriteFile(filepath.Join(fake.state, "harness-apply-fail"), []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(fake.state, "harness-apply-error"), []byte(`{"schema":"localmodels.harness-error/v1","code":"RUNTIME_DOWNLOAD_FAILED","message":"timeout"}`), 0o600); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + _, err := fake.tool.EnsurePi(context.Background(), env, harnessDir, "") + if err == nil || !strings.Contains(err.Error(), "RUNTIME_DOWNLOAD_FAILED") { + t.Fatalf("expected structured error code, got %v", err) + } + }) + + t.Run("falls back to harness status when apply omits status", func(t *testing.T) { + fake := newFakeTool(t) + if err := os.WriteFile(filepath.Join(fake.state, "harness-apply-no-status"), []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + got, err := fake.tool.EnsurePi(context.Background(), env, harnessDir, "") + if err != nil || got != want { + t.Fatalf("EnsurePi fallback = (%q, %v), want %q", got, err, want) + } + }) + + if runtime.GOOS != "windows" { + t.Run("skips symlink nodePath", func(t *testing.T) { + fake := newFakeTool(t) + realNode := filepath.Join(t.TempDir(), "real-node") + if err := os.WriteFile(realNode, []byte("#!/bin/sh\n"), 0o700); err != nil { + t.Fatal(err) + } + linkNode := filepath.Join(t.TempDir(), "node") + if err := os.Symlink(realNode, linkNode); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + if _, err := fake.tool.EnsurePi(context.Background(), env, harnessDir, linkNode); err != nil { + t.Fatal(err) + } + for _, call := range fake.calls() { + if strings.HasPrefix(call, "harness plan ") && strings.Contains(call, "--node") { + t.Fatalf("symlink nodePath passed to harness plan: %q", call) + } + } + }) + } +} + +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 plan ") && strings.Contains(c, "--node "+nodePath) + }) || + !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness apply ") }) { + t.Fatalf("expected harness 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 TestInitialRefreshProvisionsPiAndInspectPiSeedsInitialExecutable(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) + } + seeded := New(Options{ + Tool: &fake.tool, + Environment: env, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + InitialPiExecutable: inspected, + }) + t.Cleanup(seeded.Close) + if seeded.PiExecutable() != want { + t.Fatalf("InitialPiExecutable=%q want=%q", seeded.PiExecutable(), want) + } +} + +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 { + library.mu.Lock() + defer library.mu.Unlock() + return !library.piEnsuring && library.piFailures == 1 + }) + if library.PiExecutable() != "" { + t.Fatalf("unexpected PiExecutable=%q", library.PiExecutable()) + } +} + +func TestEnsurePiRunsOutsideRefreshLockAndSingleflightsConcurrentCalls(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 + }) + + // Concurrent Refreshes must neither block on refreshMu nor launch duplicate installs. + var wg sync.WaitGroup + for range 4 { + wg.Go(func() { + snap := library.Refresh(context.Background()) + if !snap.Available || modelState(snap, testModel).State != "loaded" { + t.Errorf("concurrent Refresh=%+v", snap) + } + }) + } + wg.Wait() + + downloaded, _ := json.Marshal(status(model(testModel, "loaded"), model(otherModel, "idle"))) + fake.script("download", fakeScript{ + Lines: []fakeLine{ + {Line: progressLine(otherModel, "download", 10, 100)}, + {Line: resultLine(otherModel), DelayMS: 40}, + }, + 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 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 + }) + + planCount := 0 + for _, call := range fake.calls() { + if strings.HasPrefix(call, "harness plan ") { + planCount++ + } + } + if planCount != 1 { + t.Fatalf("expected singleflight 1 harness plan call, got %d (%v)", planCount, fake.calls()) + } +} + +func TestEnsurePiRetriesWithExponentialBackoffAndStopsWhenUnloaded(t *testing.T) { + t.Run("fake clock exponential backoff", func(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "loaded"))) + failMarker := filepath.Join(fake.state, "harness-plan-fail") + if err := os.WriteFile(failMarker, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + record := &recorder{} + var clockMu sync.Mutex + currentTime := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC) + 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, + EnsurePiRetryInterval: 30 * time.Second, + }) + library.now = func() time.Time { + clockMu.Lock() + defer clockMu.Unlock() + return currentTime + } + t.Cleanup(library.Close) + + planCalls := func() int { + count := 0 + for _, call := range fake.calls() { + if strings.HasPrefix(call, "harness plan ") { + count++ + } + } + return count + } + + library.Refresh(context.Background()) + waitFor(t, "first Pi install failure recorded", func() bool { + library.mu.Lock() + defer library.mu.Unlock() + return !library.piEnsuring && library.piFailures == 1 + }) + if got := planCalls(); got != 1 { + t.Fatalf("expected 1 harness plan call, got %d", got) + } + + // Within the 30s backoff window, Refreshes are suppressed. + library.Refresh(context.Background()) + if got := planCalls(); got != 1 { + t.Fatalf("expected backoff to suppress retries before 30s, got %d calls", got) + } + + // After 31s, second failure doubles backoff to 60s. + clockMu.Lock() + currentTime = currentTime.Add(31 * time.Second) + clockMu.Unlock() + library.Refresh(context.Background()) + waitFor(t, "second Pi install failure recorded", func() bool { + library.mu.Lock() + defer library.mu.Unlock() + return !library.piEnsuring && library.piFailures == 2 + }) + if got := planCalls(); got != 2 { + t.Fatalf("expected 2 harness plan calls, got %d", got) + } + + // Advancing only 31s (less than doubled 60s backoff) still suppresses retries. + if err := os.Remove(failMarker); err != nil { + t.Fatal(err) + } + clockMu.Lock() + currentTime = currentTime.Add(31 * time.Second) + clockMu.Unlock() + library.Refresh(context.Background()) + if got := planCalls(); got != 2 { + t.Fatalf("expected doubled 60s backoff to suppress retry at +31s, got %d calls", got) + } + + clockMu.Lock() + currentTime = currentTime.Add(30 * time.Second) + clockMu.Unlock() + library.Refresh(context.Background()) + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "Pi install succeeded after doubled backoff elapsed", func() bool { + return library.PiExecutable() == want && record.changes() == 1 + }) + }) + + t.Run("timer retries automatically without another Refresh", func(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "loaded"))) + failMarker := filepath.Join(fake.state, "harness-plan-fail") + if err := os.WriteFile(failMarker, []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, + EnsurePiRetryInterval: 30 * time.Millisecond, + }) + t.Cleanup(library.Close) + + library.Refresh(context.Background()) + waitFor(t, "initial failure recorded", func() bool { + library.mu.Lock() + defer library.mu.Unlock() + return !library.piEnsuring && library.piFailures >= 1 + }) + + if err := os.Remove(failMarker); err != nil { + t.Fatal(err) + } + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "automatic timer retry provisioned Pi", func() bool { + return library.PiExecutable() == want && record.changes() == 1 + }) + }) + + t.Run("unloading models cancels pending Pi retry timer", func(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "loaded"))) + failMarker := filepath.Join(fake.state, "harness-plan-fail") + if err := os.WriteFile(failMarker, []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", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + EnsurePiRetryInterval: 80 * time.Millisecond, + }) + t.Cleanup(library.Close) + + library.Refresh(context.Background()) + waitFor(t, "initial failure recorded", func() bool { + library.mu.Lock() + defer library.mu.Unlock() + return !library.piEnsuring && library.piFailures == 1 + }) + + _ = os.Remove(failMarker) + fake.write("status.json", status(model(testModel, "idle"))) + library.Refresh(context.Background()) + + time.Sleep(180 * time.Millisecond) + if library.PiExecutable() != "" { + t.Fatalf("Pi was provisioned after models were unloaded: %q", library.PiExecutable()) + } + }) +} + +func TestLibraryCloseCancelsInFlightPiProvisionAndWaitsCleanly(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "loaded"))) + if err := os.WriteFile(filepath.Join(fake.state, "harness-apply-wait"), []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, + EnsurePiRetryInterval: 10 * time.Millisecond, + }) + + library.Refresh(context.Background()) + waitFor(t, "harness apply entered", func() bool { + _, err := os.Stat(filepath.Join(fake.state, "harness-apply-entered")) + return err == nil + }) + + done := make(chan struct{}) + go func() { + library.Close() + close(done) + }() + select { + case <-done: + case <-time.After(10 * time.Second): + _ = os.Remove(filepath.Join(fake.state, "harness-apply-wait")) + t.Fatal("Library.Close timed out waiting for in-flight Pi setup") + } + + library.mu.Lock() + ensuring := library.piEnsuring + retryTimer := library.piRetryTimer + library.mu.Unlock() + if ensuring || retryTimer != nil || library.PiExecutable() != "" || record.changes() != 0 { + t.Fatalf("unexpected post-close state: ensuring=%v timer=%v exec=%q changes=%d", + ensuring, retryTimer != nil, library.PiExecutable(), record.changes()) + } +} diff --git a/apps/computer/local-daemon/internal/localmodels/status.go b/apps/computer/local-daemon/internal/localmodels/status.go index be91b0f0b..69ddbd1e5 100644 --- a/apps/computer/local-daemon/internal/localmodels/status.go +++ b/apps/computer/local-daemon/internal/localmodels/status.go @@ -36,9 +36,11 @@ var errorCodes = []string{ } var ( - families = []string{"qwen", "gemma", "llama", "openai", "deepseek", "mistral", "phi", "glm", "kimi", "claude", "grok", "other"} - architectures = []string{"dense", "moe"} - capabilities = []string{"vision", "tool_use", "reasoning", "coding", "multilingual"} + families = []string{"qwen", "gemma", "llama", "openai", "deepseek", "mistral", "phi", "glm", "kimi", "claude", "grok", "other"} + architectures = []string{"dense", "moe"} + capabilities = []string{"vision", "tool_use", "reasoning", "coding", "multilingual"} + downloadSources = []string{"huggingface", "modelscope", "custom", "cache"} + loadSteps = []string{"prepare", "start", "load", "warm"} ) var ( @@ -149,6 +151,12 @@ func mapStatus(encoded []byte, now time.Time) (wire.LocalModelsSnapshot, error) if model.State != "loaded" { model.Endpoint = nil } + if model.State != "downloading" { + model.Download = nil + } + if model.State != "loading" { + model.Load = nil + } if model.Error != nil { model.Error.Detail = clip(model.Error.Detail, 4000) } @@ -192,8 +200,24 @@ func checkModel(model wire.LocalModel) error { !slices.Contains([]string{"MEMORY_INSUFFICIENT", "DISK_INSUFFICIENT", "PLATFORM_UNSUPPORTED"}, reason.Code) { return invalid("availability reason", reason.Code) } - if model.Download != nil && model.Download.Step != "download" && model.Download.Step != "verify" { - return invalid("download step", model.Download.Step) + if model.Download != nil { + if model.Download.Step != "download" && model.Download.Step != "verify" { + return invalid("download step", model.Download.Step) + } + if model.Download.Source != "" && !slices.Contains(downloadSources, model.Download.Source) { + return invalid("download source", model.Download.Source) + } + if model.Download.Connections != nil && *model.Download.Connections > 64 { + return invalid("download connections", strconv.FormatUint(*model.Download.Connections, 10)) + } + } + if model.Load != nil { + if !slices.Contains(loadSteps, model.Load.Step) { + return invalid("load step", model.Load.Step) + } + if model.Load.TotalSteps < 1 || model.Load.TotalSteps > 100 || model.Load.DoneSteps > model.Load.TotalSteps { + return fmt.Errorf("model %s load progress %d/%d is outside the contract", model.ID, model.Load.DoneSteps, model.Load.TotalSteps) + } } if model.Error != nil && !slices.Contains(errorCodes, model.Error.Code) { return invalid("error code", model.Error.Code) @@ -239,6 +263,9 @@ type libraryEvent struct { Done float64 `json:"done"` Total float64 `json:"total"` BytesPerSecond float64 `json:"bytes_per_second"` + Source string `json:"source"` + Connections *int `json:"connections"` + LoadStep string `json:"load_step"` OK bool `json:"ok"` Code string `json:"code"` Detail string `json:"detail"` diff --git a/apps/computer/local-daemon/internal/localmodels/status_test.go b/apps/computer/local-daemon/internal/localmodels/status_test.go index e05ab33d4..e202a496c 100644 --- a/apps/computer/local-daemon/internal/localmodels/status_test.go +++ b/apps/computer/local-daemon/internal/localmodels/status_test.go @@ -6,6 +6,8 @@ import ( "slices" "testing" "time" + + wire "github.com/simplexai-labs/linger/contracts-go/runnergateway" ) // testdata/library-status-dev38.json is what localmodels 0.1.0.dev38 printed @@ -67,3 +69,112 @@ func TestMapStatusKeepsTheModelCardOfARealDev38Status(t *testing.T) { } } } + +func TestMapStatusClearsProgressOutsideActiveStateAndKeepsValidProgress(t *testing.T) { + downloading := model("downloading-model:Q4_K_M", "downloading") + downloading["download"] = map[string]any{ + "step": "download", "done_bytes": 512, "total_bytes": 1024, "bytes_per_second": 2048, + "source": "modelscope", "connections": 16, + } + downloading["load"] = map[string]any{"step": "prepare", "done_steps": 1, "total_steps": 7} + + loading := model("loading-model:Q4_K_M", "loading") + loading["download"] = map[string]any{"step": "verify", "done_bytes": 1024, "total_bytes": 1024} + loading["load"] = map[string]any{"step": "load", "done_steps": 4, "total_steps": 7} + + idle := model("idle-model:Q4_K_M", "idle") + idle["download"] = map[string]any{"step": "download", "done_bytes": 1024, "total_bytes": 1024, "source": "cache", "connections": 0} + idle["load"] = map[string]any{"step": "warm", "done_steps": 7, "total_steps": 7} + + encoded, err := json.Marshal(status(downloading, loading, idle)) + if err != nil { + t.Fatal(err) + } + snapshot, err := mapStatus(encoded, time.Now()) + if err != nil { + t.Fatal(err) + } + + gotDownloading := modelState(snapshot, "downloading-model:Q4_K_M") + if gotDownloading == nil || gotDownloading.Download == nil || gotDownloading.Load != nil || + gotDownloading.Download.Source != "modelscope" || gotDownloading.Download.Connections == nil || *gotDownloading.Download.Connections != 16 { + t.Fatalf("downloading model = %+v", gotDownloading) + } + gotLoading := modelState(snapshot, "loading-model:Q4_K_M") + if gotLoading == nil || gotLoading.Load == nil || gotLoading.Download != nil || + gotLoading.Load.Step != "load" || gotLoading.Load.DoneSteps != 4 || gotLoading.Load.TotalSteps != 7 { + t.Fatalf("loading model = %+v", gotLoading) + } + gotIdle := modelState(snapshot, "idle-model:Q4_K_M") + if gotIdle == nil || gotIdle.Download != nil || gotIdle.Load != nil { + t.Fatalf("idle model kept stale progress = %+v", gotIdle) + } +} + +func TestCheckModelValidatesDownloadAndLoadProgress(t *testing.T) { + baseModel := func() wire.LocalModel { + return wire.LocalModel{ + ID: testModel, Name: "Qwen3 4B", Quant: "Q4_K_M", Fit: "gpu", Pool: "gpu:0", State: "idle", + Availability: wire.LocalModelAvailability{Downloadable: true}, + } + } + u64 := func(v uint64) *uint64 { return &v } + + for _, source := range []string{"", "huggingface", "modelscope", "custom", "cache"} { + m := baseModel() + m.State = "downloading" + m.Download = &wire.LocalModelDownloadProgress{Step: "download", Source: source, Connections: u64(64)} + if err := checkModel(m); err != nil { + t.Errorf("valid download source %q rejected: %v", source, err) + } + } + for _, step := range []string{"prepare", "start", "load", "warm"} { + m := baseModel() + m.State = "loading" + m.Load = &wire.LocalModelLoadProgress{Step: step, DoneSteps: 1, TotalSteps: 100} + if err := checkModel(m); err != nil { + t.Errorf("valid load step %q rejected: %v", step, err) + } + } + + for name, mutate := range map[string]func(*wire.LocalModel){ + "invalid download.step": func(m *wire.LocalModel) { + m.State = "downloading" + m.Download = &wire.LocalModelDownloadProgress{Step: "extract"} + }, + "invalid download.source": func(m *wire.LocalModel) { + m.State = "downloading" + m.Download = &wire.LocalModelDownloadProgress{Step: "download", Source: "s3"} + }, + "download.connections > 64": func(m *wire.LocalModel) { + m.State = "downloading" + m.Download = &wire.LocalModelDownloadProgress{Step: "download", Connections: u64(65)} + }, + "invalid load.step": func(m *wire.LocalModel) { + m.State = "loading" + m.Load = &wire.LocalModelLoadProgress{Step: "boot", DoneSteps: 1, TotalSteps: 5} + }, + "empty load.step": func(m *wire.LocalModel) { + m.State = "loading" + m.Load = &wire.LocalModelLoadProgress{Step: "", DoneSteps: 1, TotalSteps: 5} + }, + "load.total_steps == 0": func(m *wire.LocalModel) { + m.State = "loading" + m.Load = &wire.LocalModelLoadProgress{Step: "prepare", DoneSteps: 0, TotalSteps: 0} + }, + "load.total_steps > 100": func(m *wire.LocalModel) { + m.State = "loading" + m.Load = &wire.LocalModelLoadProgress{Step: "load", DoneSteps: 10, TotalSteps: 101} + }, + "load.done_steps > total_steps": func(m *wire.LocalModel) { + m.State = "loading" + m.Load = &wire.LocalModelLoadProgress{Step: "warm", DoneSteps: 8, TotalSteps: 7} + }, + } { + m := baseModel() + mutate(&m) + if err := checkModel(m); err == nil { + t.Errorf("%s: unexpectedly accepted", name) + } + } +} diff --git a/apps/computer/local-daemon/internal/localmodels/tool.go b/apps/computer/local-daemon/internal/localmodels/tool.go index 3f938d9ba..96aecfc15 100644 --- a/apps/computer/local-daemon/internal/localmodels/tool.go +++ b/apps/computer/local-daemon/internal/localmodels/tool.go @@ -9,6 +9,8 @@ import ( "os" "os/exec" "strings" + + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace" ) // Tool is where the installer put the localmodels tool and its state, as @@ -58,20 +60,56 @@ 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, PI_CODING_AGENT_DIR aligned +// with workspace.PiSourceEnvironment, and LINGER_LOCALMODELS_CACHE_DIR / +// LINGER_LOCALMODELS_REUSE_DIRS forwarded as LOCALMODELS_CACHE_DIR / +// LOCALMODELS_REUSE_DIRS when those LOCALMODELS_* settings are not already set. +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) + values := make(map[string]string, len(environment)+7) + var piAgentDir keyPreference + var localCacheDir, lingerCacheDir keyPreference + var localReuseDirs, lingerReuseDirs keyPreference for key, value := range environment { switch strings.ToUpper(key) { case "PYTHONHOME", "PYTHONSTARTUP", "PYTHONINSPECT", "PYTHONPATH", "LOCALMODELS_STATE": continue + case "PI_CODING_AGENT_DIR": + piAgentDir.record(key, "PI_CODING_AGENT_DIR", value) + continue + case "LOCALMODELS_CACHE_DIR": + localCacheDir.record(key, "LOCALMODELS_CACHE_DIR", value) + continue + case "LOCALMODELS_REUSE_DIRS": + localReuseDirs.record(key, "LOCALMODELS_REUSE_DIRS", value) + continue + case "LINGER_LOCALMODELS_CACHE_DIR": + lingerCacheDir.record(key, "LINGER_LOCALMODELS_CACHE_DIR", value) + case "LINGER_LOCALMODELS_REUSE_DIRS": + lingerReuseDirs.record(key, "LINGER_LOCALMODELS_REUSE_DIRS", value) } values[key] = value } + if piAgentDir.value != "" { + values["PI_CODING_AGENT_DIR"] = piAgentDir.value + } + if piEnv, err := workspace.PiSourceEnvironment(values); err == nil && piEnv["PI_CODING_AGENT_DIR"] != "" { + values["PI_CODING_AGENT_DIR"] = piEnv["PI_CODING_AGENT_DIR"] + } + if dir := firstNonEmpty(localCacheDir.value, lingerCacheDir.value); dir != "" { + values["LOCALMODELS_CACHE_DIR"] = dir + } + if dirs := firstNonEmpty(localReuseDirs.value, lingerReuseDirs.value); dirs != "" { + values["LOCALMODELS_REUSE_DIRS"] = dirs + } values["PYTHONPATH"] = t.ModuleDirectory values["LOCALMODELS_STATE"] = t.StateDirectory values["PYTHONNOUSERSITE"] = "1" @@ -83,6 +121,28 @@ func (t Tool) command(environment map[string]string, action Action) *exec.Cmd { return cmd } +type keyPreference struct { + exact bool + value string +} + +func (p *keyPreference) record(key, exactKey, value string) { + trimmed := strings.TrimSpace(value) + if key == exactKey { + p.exact = true + p.value = trimmed + } else if !p.exact && p.value == "" { + p.value = trimmed + } +} + +func firstNonEmpty(primary, fallback string) string { + if primary != "" { + return primary + } + return fallback +} + // installed reports whether the configured interpreter still exists. func (t Tool) installed() bool { info, err := os.Stat(t.Python) diff --git a/apps/teamhub/internal/app/handlers_local_models.go b/apps/teamhub/internal/app/handlers_local_models.go index 301ce4729..75cb72d48 100644 --- a/apps/teamhub/internal/app/handlers_local_models.go +++ b/apps/teamhub/internal/app/handlers_local_models.go @@ -324,6 +324,23 @@ func validLocalModelsSnapshot(snapshot teamhubinternal.LocalModelsSnapshot) bool if model.PromptTokensPerSecond != nil && *model.PromptTokensPerSecond < 0 { return false } + if model.Download != nil { + if !model.Download.Step.Valid() { + return false + } + if model.Download.Source != nil && !model.Download.Source.Valid() { + return false + } + if model.Download.Connections != nil && (*model.Download.Connections < 0 || *model.Download.Connections > 64) { + return false + } + } + if model.Load != nil { + if !model.Load.Step.Valid() || model.Load.TotalSteps < 1 || model.Load.TotalSteps > 100 || + model.Load.DoneSteps < 0 || model.Load.DoneSteps > model.Load.TotalSteps { + return false + } + } seen[model.Id] = true } return true diff --git a/apps/teamhub/internal/app/handlers_local_models_test.go b/apps/teamhub/internal/app/handlers_local_models_test.go index 690b750be..cda01cc1f 100644 --- a/apps/teamhub/internal/app/handlers_local_models_test.go +++ b/apps/teamhub/internal/app/handlers_local_models_test.go @@ -183,6 +183,51 @@ func TestValidLocalModelsSnapshot(t *testing.T) { p := float32(-1) s.Models[0].PromptTokensPerSecond = &p }, + "invalid download step": func(s *teamhubinternal.LocalModelsSnapshot) { + s.Models[0].State = teamhubinternal.LocalModelStateDownloading + s.Models[0].Download = &teamhubinternal.LocalModelDownloadProgress{Step: "extract", DoneBytes: 0, TotalBytes: 10} + }, + "invalid download source": func(s *teamhubinternal.LocalModelsSnapshot) { + src := teamhubinternal.LocalModelDownloadSource("s3") + s.Models[0].State = teamhubinternal.LocalModelStateDownloading + s.Models[0].Download = &teamhubinternal.LocalModelDownloadProgress{ + Step: teamhubinternal.LocalModelDownloadStepDownload, Source: &src, DoneBytes: 0, TotalBytes: 10, + } + }, + "negative download connections": func(s *teamhubinternal.LocalModelsSnapshot) { + conn := -1 + s.Models[0].State = teamhubinternal.LocalModelStateDownloading + s.Models[0].Download = &teamhubinternal.LocalModelDownloadProgress{ + Step: teamhubinternal.LocalModelDownloadStepDownload, Connections: &conn, DoneBytes: 0, TotalBytes: 10, + } + }, + "download connections > 64": func(s *teamhubinternal.LocalModelsSnapshot) { + conn := 65 + s.Models[0].State = teamhubinternal.LocalModelStateDownloading + s.Models[0].Download = &teamhubinternal.LocalModelDownloadProgress{ + Step: teamhubinternal.LocalModelDownloadStepDownload, Connections: &conn, DoneBytes: 0, TotalBytes: 10, + } + }, + "invalid load step": func(s *teamhubinternal.LocalModelsSnapshot) { + s.Models[0].State = teamhubinternal.LocalModelStateLoading + s.Models[0].Load = &teamhubinternal.LocalModelLoadProgress{Step: "boot", DoneSteps: 1, TotalSteps: 5} + }, + "load total_steps < 1": func(s *teamhubinternal.LocalModelsSnapshot) { + s.Models[0].State = teamhubinternal.LocalModelStateLoading + s.Models[0].Load = &teamhubinternal.LocalModelLoadProgress{Step: teamhubinternal.LocalModelLoadStepPrepare, DoneSteps: 0, TotalSteps: 0} + }, + "load total_steps > 100": func(s *teamhubinternal.LocalModelsSnapshot) { + s.Models[0].State = teamhubinternal.LocalModelStateLoading + s.Models[0].Load = &teamhubinternal.LocalModelLoadProgress{Step: teamhubinternal.LocalModelLoadStepLoad, DoneSteps: 5, TotalSteps: 101} + }, + "negative load done_steps": func(s *teamhubinternal.LocalModelsSnapshot) { + s.Models[0].State = teamhubinternal.LocalModelStateLoading + s.Models[0].Load = &teamhubinternal.LocalModelLoadProgress{Step: teamhubinternal.LocalModelLoadStepPrepare, DoneSteps: -1, TotalSteps: 5} + }, + "load done_steps > total_steps": func(s *teamhubinternal.LocalModelsSnapshot) { + s.Models[0].State = teamhubinternal.LocalModelStateLoading + s.Models[0].Load = &teamhubinternal.LocalModelLoadProgress{Step: teamhubinternal.LocalModelLoadStepWarm, DoneSteps: 8, TotalSteps: 7} + }, } { var copied teamhubinternal.LocalModelsSnapshot if err := json.Unmarshal([]byte(storedLocalModelsSnapshotJSON), &copied); err != nil { @@ -193,4 +238,74 @@ func TestValidLocalModelsSnapshot(t *testing.T) { t.Errorf("%s: accepted", name) } } + + for name, mutate := range map[string]func(*teamhubinternal.LocalModelsSnapshot){ + "download with source and zero connections": func(s *teamhubinternal.LocalModelsSnapshot) { + src := teamhubinternal.LocalModelDownloadSourceCache + conn := 0 + s.Models[0].State = teamhubinternal.LocalModelStateDownloading + s.Models[0].Download = &teamhubinternal.LocalModelDownloadProgress{ + Step: teamhubinternal.LocalModelDownloadStepDownload, Source: &src, Connections: &conn, DoneBytes: 10, TotalBytes: 10, + } + }, + "download with modelscope and max connections": func(s *teamhubinternal.LocalModelsSnapshot) { + src := teamhubinternal.LocalModelDownloadSourceModelscope + conn := 64 + s.Models[0].State = teamhubinternal.LocalModelStateDownloading + s.Models[0].Download = &teamhubinternal.LocalModelDownloadProgress{ + Step: teamhubinternal.LocalModelDownloadStepDownload, Source: &src, Connections: &conn, DoneBytes: 5, TotalBytes: 10, + } + }, + "load with valid steps": func(s *teamhubinternal.LocalModelsSnapshot) { + s.Models[0].State = teamhubinternal.LocalModelStateLoading + s.Models[0].Load = &teamhubinternal.LocalModelLoadProgress{ + Step: teamhubinternal.LocalModelLoadStepWarm, DoneSteps: 7, TotalSteps: 7, + } + }, + } { + var copied teamhubinternal.LocalModelsSnapshot + if err := json.Unmarshal([]byte(storedLocalModelsSnapshotJSON), &copied); err != nil { + t.Fatal(err) + } + mutate(&copied) + if !validLocalModelsSnapshot(copied) { + t.Errorf("%s: rejected", name) + } + } +} + +func TestLocalModelsViewProjectsDownloadAndLoadProgress(t *testing.T) { + now := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC) + seen := now.Add(-10 * time.Second) + snapshotJSON := `{"available":true,"observed_at":"2026-11-18T11:59:50Z","tool_version":"2.1.0", +"pools":[{"id":"gpu:0","kind":"gpu","gpu_index":0,"total_bytes":100,"used_bytes":40,"holders":[]}], +"models":[ + {"id":"dl:Q4","name":"DL","description":"","quant":"Q4","context_tokens":8192,"vision":false, + "download_bytes":10,"memory_bytes":40,"fit":"gpu","recommended":false,"availability":{"downloadable":true}, + "pool":"gpu:0","state":"downloading", + "download":{"step":"download","done_bytes":5,"total_bytes":10,"bytes_per_second":2,"source":"modelscope","connections":16}}, + {"id":"ld:Q4","name":"LD","description":"","quant":"Q4","context_tokens":8192,"vision":false, + "download_bytes":10,"memory_bytes":40,"fit":"gpu","recommended":false,"availability":{"downloadable":true}, + "pool":"gpu:0","state":"loading", + "load":{"step":"warm","done_steps":6,"total_steps":7}}]}` + view, err := localModelsView(agentruntimestate.LocalModelsRecord{ + ComputerID: "computer_1", DisplayName: "Studio", Status: "online", LastSeenAt: &seen, + Owner: agentruntimestate.LocalComputerOwner{TenantID: "tenant_1", OwnerActorID: "actor_owner"}, + Snapshot: json.RawMessage(snapshotJSON), + }, "actor_owner", now) + if err != nil { + t.Fatal(err) + } + if len(view.Models) != 2 { + t.Fatalf("len(models)=%d, want 2", len(view.Models)) + } + dl := view.Models[0].Model.Download + if dl == nil || dl.Source == nil || *dl.Source != httpapi.LocalModelDownloadSourceModelscope || + dl.Connections == nil || *dl.Connections != 16 { + t.Fatalf("download progress=%+v", dl) + } + ld := view.Models[1].Model.Load + if ld == nil || ld.Step != httpapi.LocalModelLoadStepWarm || ld.DoneSteps != 6 || ld.TotalSteps != 7 { + t.Fatalf("load progress=%+v", ld) + } } diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md index 3e8ab7258..dce0893d4 100644 --- a/apps/web/AGENTS.md +++ b/apps/web/AGENTS.md @@ -27,16 +27,25 @@ build:assets` to check the initial JS budget. one Computer's model library as TeamHub last stored it. The Computer decides fit, availability, placement, the recommendation and memory; the page only orders and labels them, picks the Make room default, renders family icons, - capability/parameter badges, download speed/ETA and model-card facts from - structured `LocalModel` fields in `local-models-rules.ts`, 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 - and progress arrives as `local-models` invalidation hints. From the click - until a snapshot with another status or observation time, or a refusal, the - row shows a requested load or download as started and spins the button of - any other action, with the row's actions off. A Computer's - detail on the Computers page links here and reads no model state itself. + capability/parameter badges, header pool breakdown, download speed/ETA, + download transfer source/connections, load step progress, and model-card facts + from structured `LocalModel` fields in `local-models-rules.ts`, and maps every + code and enum label to English in `local-models-copy.ts`. Without `?computer=`, + the desktop app opens its own Computer; otherwise the page falls back through + the Tenant's remembered Computer in `localStorage`, the first online Computer + with a `localmodels-library/` Pi model, the first online Computer, and the + first inventory entry. `local-model-agents.ts` connects loaded models to + sponsored Agents (personal delegate Agent first) and Bifrost/Pi gateway + readiness: rows offer immediate `Use with ` / `Open chat` actions, + `Use when ready` auto-switching on downloading/loading rows, Make room handoff + checkboxes, and expanded-row `Switch Agent…` / `New Agent` controls (lazily + loading `QuickCreateLocalAgentDialog`). Only the Computer's owner can act; + actions return at once and progress arrives as `local-models` invalidation + hints. From the click until a snapshot with another status or observation time, + or a refusal, the row shows a requested load or download as started and spins + the button of any other action, with the row's actions off. A Computer's + detail on the Computers page links here (showing only a compact runtime-based + summary) and reads no `/local-models` API state itself. - `src/test`: unit-test support, including the in-process TeamHub test server. - `e2e`: Playwright product workflows; every spec runs on the real TeamHub and Document Runtime stack (`make check-e2e-room-document`). diff --git a/apps/web/src/features/local-computer/computer-detail.tsx b/apps/web/src/features/local-computer/computer-detail.tsx index 72d40e22c..d9ebdf6f4 100644 --- a/apps/web/src/features/local-computer/computer-detail.tsx +++ b/apps/web/src/features/local-computer/computer-detail.tsx @@ -60,6 +60,15 @@ import { ComputerRow, ProgressChip, RowMark, StatusDot } from "./computer-row"; import { isUpgradeRecommended } from "./computer-version"; type LocalComputer = TeamHubLocalComputerPage["items"][number]; +const LOCAL_MODELS_LIBRARY_PREFIX = "localmodels-library/"; + +function loadedLocalModels(computer: LocalComputer) { + const piRuntime = computer.runtimes.find((runtime) => runtime.type === "pi"); + return (piRuntime?.models ?? []).filter((model) => + model.id.startsWith(LOCAL_MODELS_LIBRARY_PREFIX), + ); +} + interface AppSetup { app: TeamHubLocalRuntimeType; choosable: boolean; @@ -115,6 +124,7 @@ export function ComputerDetail({ const [setup, setSetup] = useState(null); const [updateOpen, setUpdateOpen] = useState(false); const online = displayStatus === "online"; + const localModels = loadedLocalModels(computer); const openSetup = (app: TeamHubLocalRuntimeType, choosable = false) => setSetup({ app, choosable }); @@ -159,7 +169,7 @@ export function ComputerDetail({ ))} {localModelsHref ? ( - + - Download and load models that run on this computer + {localModels.length > 0 + ? `${localModels.length} loaded · ${localModels.map((model) => model.label).join(", ")}` + : "Download and load models that run on this computer"} runtime.type === "pi"); + return (piRuntime?.models ?? []).filter((model) => + model.id.startsWith(LOCAL_MODELS_LIBRARY_PREFIX), + ); +} /** Every Computer as one Chats-list style row; a row opens its detail. */ export function ComputerDirectory({ @@ -91,10 +99,23 @@ function DirectoryRow({ const apps = appOrder.filter( (app) => appStatus(computer, app) !== "not_installed", ); + const localModels = loadedLocalModels(computer); return ( + {localModels.length > 0 ? ( + model.label).join(", ")} + > + {localModels.length} local{" "} + {localModels.length === 1 ? "model" : "models"} + + ) : null} {apps.map((app) => ( { ).not.toBeInTheDocument(); }); + it("shows loaded local model summaries from computer runtimes without fetching Local models", async () => { + const runtime = await authenticatedFixture(); + runtime.seed.localComputers[0]!.runtimes.push({ + models: [ + { id: "pi-default", label: "Pi Default" }, + { + id: "localmodels-library/qwen3.6-35b-a3b", + label: "Qwen3.6 35B-A3B", + }, + ], + status: "ready", + type: "pi", + }); + runtime.seed.localComputers[1]!.runtimes.push({ + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + { + id: "localmodels-library/gemma3-27b", + label: "Gemma 3 27B", + }, + ], + status: "ready", + type: "pi", + }); + const queryClient = renderPage(runtime); + + const directory = await screen.findByRole("list", { + name: "Local computers directory", + }); + const jennyRow = within(directory).getByRole("button", { + name: /Jenny's Mac/, + }); + expect(within(jennyRow).getByText("1 local model")).toBeVisible(); + const offlineRow = within(directory).getByRole("button", { + name: /Offline Lab Mac/, + }); + const offlineBadge = within(offlineRow).getByText("2 local models"); + expect(offlineBadge).toBeVisible(); + expect(offlineBadge).toHaveClass("opacity-45"); + + fireEvent.click(jennyRow); + const detail = await screen.findByRole("region", { + name: "Jenny's Mac details", + }); + const localModelsGroup = within(detail).getByRole("region", { + name: "Local models", + }); + expect(within(localModelsGroup).getByText("1")).toBeVisible(); + expect( + within(localModelsGroup).getByRole("link", { + name: /Manage local models/, + }), + ).toHaveTextContent("1 loaded · Qwen3.6 35B-A3B"); + + expect( + runtime.server.requestsTo( + "GET", + "/v0/tenants/{tenantId}/local-computers/{computerId}/local-models", + ), + ).toHaveLength(0); + expect( + queryClient + .getQueryCache() + .findAll({ queryKey: tenantLocalModelsQueryKey("tenant-northstar") }), + ).toHaveLength(0); + }); + it("lists several Computers by readiness and opens each one's detail", async () => { const runtime = await authenticatedFixture(); renderPage(runtime); 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..f9a0fa888 --- /dev/null +++ b/apps/web/src/features/local-models/local-model-agents.test.ts @@ -0,0 +1,364 @@ +import { describe, expect, it } from "vitest"; + +import type { + TeamHubLocalComputerPage, + TeamHubTenantAgent, + TeamHubTenantAgentAvailabilitySnapshot, +} from "@/features/teamhub/teamhub-types"; + +import { + assignableLocalModelAgents, + buildLocalModelRuntimeUpdate, + gatewayReadiness, + isAgentUsingModel, + primaryLocalModelAgent, + usedByAgentNames, +} from "./local-model-agents"; +import { localModel, modelView } from "./local-models-test-support"; + +type LocalComputer = TeamHubLocalComputerPage["items"][number]; + +function makeAgent( + id: string, + name: string, + { + agentType = "general", + computerId = "computer-lab", + createdBy = "actor-jenny", + model = "openai/gpt-5.4", + reasoningEffort, + revision = 1, + runtimeType = "codex", + workspaceGrantId = "grant-write", + }: { + agentType?: TeamHubTenantAgent["agent_type"]; + computerId?: string; + createdBy?: string; + model?: string; + reasoningEffort?: TeamHubTenantAgent["runtime"]["reasoning_effort"]; + revision?: number; + runtimeType?: "codex" | "claude" | "pi"; + workspaceGrantId?: string; + } = {}, +): TeamHubTenantAgent { + return { + actor: { + created_at: "2026-10-01T00:00:00Z", + display_name: name, + handle: `@${id}`, + id, + kind: "agent", + tenant_id: "tenant-1", + }, + agent_management_id: `mgmt-${id}`, + agent_type: agentType, + configuration_revision: revision, + created_at: "2026-10-01T00:00:00Z", + created_by: { + display_name: "Sponsor", + id: createdBy, + kind: "human", + tenant_id: "tenant-1", + }, + description: "", + instructions: "Help.", + runtime: { + model, + ...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}), + placement: { + computer_id: computerId, + runtime_type: runtimeType, + type: "local", + workspace_grant_id: workspaceGrantId, + }, + }, + skill_bindings: [], + updated_at: "2026-10-01T00:00:00Z", + visibility_scope: "sponsor", + }; +} + +function makeComputer(overrides: Partial = {}): LocalComputer { + return { + active_operations: 0, + computer_id: "computer-lab", + display_name: "lab-118", + runtimes: [ + { + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + status: "ready", + type: "pi", + }, + ], + status: "online", + upgrade_required: false, + workspace_grants: [ + { grant_id: "grant-ro", label: "Read only", mode: "read-only" }, + { grant_id: "grant-write", label: "Workspace", mode: "workspace-write" }, + ], + ...overrides, + }; +} + +describe("local-model-agents", () => { + it("filters sponsored agents, marks Another Me first, and tracks busy tasks", () => { + const codex = makeAgent("actor-codex", "Codex"); + const other = makeAgent("actor-other", "Maya's Agent", { + createdBy: "actor-maya", + }); + const delegate = makeAgent("actor-delegate", "Jenny's Delegate", { + agentType: "anotherme", + }); + const availabilities: TeamHubTenantAgentAvailabilitySnapshot[] = [ + { + actor_id: "actor-codex", + availability: { + active_task_count: 2, + label: "Working", + status: "online", + }, + }, + { + actor_id: "actor-delegate", + availability: { + active_task_count: 0, + label: "Online", + status: "online", + }, + }, + ]; + + const result = assignableLocalModelAgents([codex, other, delegate], { + availabilities, + currentActorId: "actor-jenny", + }); + + expect(result.map((item) => item.actorId)).toEqual([ + "actor-delegate", + "actor-codex", + ]); + expect(result[0]).toMatchObject({ busy: false, isAnotherMe: true }); + expect(result[1]).toMatchObject({ busy: true, isAnotherMe: false }); + expect( + assignableLocalModelAgents([codex], { currentActorId: undefined }), + ).toEqual([]); + }); + + it("picks primary agent preferring Another Me, then agents on the Computer", () => { + const onMac = makeAgent("actor-mac", "Mac Helper", { + computerId: "computer-mac", + }); + const onLab = makeAgent("actor-lab", "Lab Helper", { + computerId: "computer-lab", + }); + const delegate = makeAgent("actor-delegate", "Another Me", { + agentType: "anotherme", + computerId: "computer-mac", + }); + + const withoutAnotherMe = assignableLocalModelAgents([onMac, onLab], { + currentActorId: "actor-jenny", + }); + expect( + primaryLocalModelAgent(withoutAnotherMe, "computer-lab")?.actorId, + ).toBe("actor-lab"); + + const withAnotherMe = assignableLocalModelAgents([onLab, delegate], { + currentActorId: "actor-jenny", + }); + expect(primaryLocalModelAgent(withAnotherMe, "computer-lab")?.actorId).toBe( + "actor-delegate", + ); + expect(primaryLocalModelAgent([], "computer-lab")).toBeUndefined(); + }); + + it("checks whether an agent uses a specific model on a Computer via Pi", () => { + const [agent] = assignableLocalModelAgents( + [ + makeAgent("a1", "Coder", { + computerId: "computer-lab", + model: "localmodels-library/qwen3-coder-30b", + runtimeType: "pi", + }), + ], + { currentActorId: "actor-jenny" }, + ); + + expect( + isAgentUsingModel( + agent!, + "computer-lab", + "localmodels-library/qwen3-coder-30b", + ), + ).toBe(true); + expect( + isAgentUsingModel( + agent!, + "computer-mac", + "localmodels-library/qwen3-coder-30b", + ), + ).toBe(false); + expect(isAgentUsingModel(agent!, "computer-lab", undefined)).toBe(false); + }); + + it("reconciles snapshot used_by with live assignable agent placements", () => { + const delegate = makeAgent("actor-delegate", "Jenny's Delegate", { + agentType: "anotherme", + computerId: "computer-lab", + model: "localmodels-library/gemma3-27b", + runtimeType: "pi", + }); + const codex = makeAgent("actor-codex", "Codex", { + computerId: "computer-lab", + model: "localmodels-library/qwen3-coder-30b", + runtimeType: "pi", + }); + const assignable = assignableLocalModelAgents([delegate, codex], { + currentActorId: "actor-jenny", + }); + + const entry = modelView( + localModel({ + id: "qwen3-coder-30b:Q4_K_M", + name: "Qwen3 Coder 30B", + pi_model: "localmodels-library/qwen3-coder-30b", + state: "loaded", + }), + ); + // Stale snapshot still lists actor-delegate on Qwen while actor-codex just moved onto it. + entry.used_by = [ + { agent_id: "actor-delegate", name: "Jenny's Delegate" }, + { agent_id: "external-research", name: "Research agent" }, + ]; + + expect(usedByAgentNames(entry, assignable, "computer-lab")).toEqual([ + "Research agent", + "Codex", + ]); + }); + + it("builds PATCH runtime update using writable grants and valid reasoning effort", () => { + const [agent] = assignableLocalModelAgents( + [ + makeAgent("a1", "Coder", { + computerId: "computer-mac", + reasoningEffort: "high", + revision: 4, + runtimeType: "codex", + workspaceGrantId: "grant-mac", + }), + ], + { currentActorId: "actor-jenny" }, + ); + + const computer = makeComputer({ + runtimes: [ + { + models: [ + { + allowed_reasoning_effort: ["low", "medium"], + default_reasoning_effort: "medium", + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + status: "ready", + type: "pi", + }, + ], + }); + + expect( + buildLocalModelRuntimeUpdate( + agent!, + computer, + "localmodels-library/qwen3-coder-30b", + ), + ).toEqual({ + expected_configuration_revision: 4, + runtime: { + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-write", + }, + reasoning_effort: "medium", + }, + }); + + expect( + buildLocalModelRuntimeUpdate( + agent!, + makeComputer({ workspace_grants: [] }), + "localmodels-library/qwen3-coder-30b", + ), + ).toBeUndefined(); + }); + + it("computes Bifrost and Pi gateway readiness states for loaded models", () => { + const loadedWithEndpoint = localModel({ + endpoint: { base_url: "http://127.0.0.1:18100/v1" }, + id: "qwen3-coder-30b:Q4_K_M", + name: "Qwen3 Coder 30B", + pi_model: "localmodels-library/qwen3-coder-30b", + state: "loaded", + }); + + expect(gatewayReadiness(loadedWithEndpoint, makeComputer())).toEqual({ + label: "Ready · Bifrost (Pi)", + tone: "success", + }); + expect( + gatewayReadiness( + loadedWithEndpoint, + makeComputer({ + runtimes: [{ models: [], status: "ready", type: "pi" }], + }), + ), + ).toEqual({ + label: "Bifrost · Registering with Pi…", + tone: "info", + }); + expect( + gatewayReadiness( + loadedWithEndpoint, + makeComputer({ + runtimes: [{ models: [], status: "unknown", type: "pi" }], + }), + ), + ).toEqual({ + label: "Bifrost · Setting up Pi…", + tone: "info", + }); + expect( + gatewayReadiness( + loadedWithEndpoint, + makeComputer({ + runtimes: [{ models: [], status: "not_installed", type: "pi" }], + }), + ), + ).toEqual({ + label: "Bifrost · Pi unavailable", + tone: "muted", + }); + expect( + gatewayReadiness(loadedWithEndpoint, makeComputer({ status: "offline" })), + ).toEqual({ + label: "Bifrost · Pi unavailable", + tone: "muted", + }); + expect( + gatewayReadiness( + localModel({ id: "idle", name: "Idle", state: "idle" }), + makeComputer(), + ), + ).toBeUndefined(); + }); +}); 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..8c0115b64 --- /dev/null +++ b/apps/web/src/features/local-models/local-model-agents.ts @@ -0,0 +1,249 @@ +import type { + TeamHubAgentReasoningEffort, + TeamHubLocalComputerPage, + TeamHubLocalRuntimeType, + TeamHubTenantAgent, + TeamHubTenantAgentAvailabilitySnapshot, + TeamHubTenantAgentUpdateInput, +} from "@/features/teamhub/teamhub-types"; + +import type { LocalModel, LocalModelView } from "./local-models-queries"; + +type LocalComputer = TeamHubLocalComputerPage["items"][number]; + +export interface LocalModelAssignableAgent { + actorId: string; + name: string; + isAnotherMe: boolean; + busy: boolean; + configurationRevision: number; + computerId?: string; + runtimeType?: TeamHubLocalRuntimeType; + model: string; + workspaceGrantId?: string; + reasoningEffort?: TeamHubAgentReasoningEffort; +} + +export interface GatewayReadiness { + label: string; + tone: "success" | "info" | "muted"; +} + +/** + * Agents the signed-in Human sponsors and may retarget onto a local model. + * Another Me always sorts first so primary model actions highlight it. + */ +export function assignableLocalModelAgents( + agents: readonly TeamHubTenantAgent[], + { + anotherMeActorId, + availabilities = [], + currentActorId, + }: { + anotherMeActorId?: string; + availabilities?: readonly TeamHubTenantAgentAvailabilitySnapshot[]; + currentActorId?: string; + }, +): LocalModelAssignableAgent[] { + if (!currentActorId) return []; + const availabilityByActor = new Map( + availabilities.map((snapshot) => [snapshot.actor_id, snapshot]), + ); + return agents + .filter((agent) => agent.created_by.id === currentActorId) + .map((agent) => { + const availability = availabilityByActor.get(agent.actor.id); + return { + actorId: agent.actor.id, + busy: (availability?.availability.active_task_count ?? 0) > 0, + computerId: agent.runtime.placement?.computer_id, + configurationRevision: agent.configuration_revision, + isAnotherMe: + agent.agent_type === "anotherme" || + (anotherMeActorId !== undefined && + agent.actor.id === anotherMeActorId), + model: agent.runtime.model, + name: agent.actor.display_name, + reasoningEffort: agent.runtime.reasoning_effort, + runtimeType: agent.runtime.placement?.runtime_type, + workspaceGrantId: agent.runtime.placement?.workspace_grant_id, + }; + }) + .sort((a, b) => Number(b.isAnotherMe) - Number(a.isAnotherMe)); +} + +export function isAgentUsingModel( + agent: Pick< + LocalModelAssignableAgent, + "computerId" | "runtimeType" | "model" + >, + computerId: string, + piModel: string | undefined, +): boolean { + return ( + Boolean(piModel) && + agent.computerId === computerId && + agent.runtimeType === "pi" && + agent.model === piModel + ); +} + +/** + * Combines TeamHub's `used_by` snapshot with live sponsored Agent configs so + * switching an Agent reflects at once on both the old and new model rows. + */ +export function usedByAgentNames( + entry: Pick, + assignableAgents: readonly LocalModelAssignableAgent[], + computerId: string, +): string[] { + const piModel = entry.model.pi_model; + const assignableById = new Map( + assignableAgents.map((agent) => [agent.actorId, agent]), + ); + const seenActorIds = new Set(); + const names: string[] = []; + + for (const user of entry.used_by) { + const known = assignableById.get(user.agent_id); + if (known) { + if (isAgentUsingModel(known, computerId, piModel)) { + seenActorIds.add(known.actorId); + names.push(known.name); + } + continue; + } + seenActorIds.add(user.agent_id); + names.push(user.name); + } + + for (const agent of assignableAgents) { + if ( + !seenActorIds.has(agent.actorId) && + isAgentUsingModel(agent, computerId, piModel) + ) { + seenActorIds.add(agent.actorId); + names.push(agent.name); + } + } + + return names; +} + +/** + * The default Agent highlighted on row actions: Another Me if present, then + * the first sponsored Agent already on this Computer, else the first sponsored. + */ +export function primaryLocalModelAgent( + agents: readonly LocalModelAssignableAgent[], + computerId?: string, +): LocalModelAssignableAgent | undefined { + if (!agents.length) return undefined; + return ( + agents.find((agent) => agent.isAnotherMe) ?? + (computerId + ? agents.find((agent) => agent.computerId === computerId) + : undefined) ?? + agents[0] + ); +} + +/** + * Builds the `PATCH` payload switching an Agent onto `piModel` on `computer`, + * preserving an existing grant on the same Computer or picking a writable one. + */ +export function buildLocalModelRuntimeUpdate( + agent: Pick< + LocalModelAssignableAgent, + | "computerId" + | "configurationRevision" + | "reasoningEffort" + | "runtimeType" + | "workspaceGrantId" + >, + computer: + | Pick + | undefined, + piModel: string | undefined, +): TeamHubTenantAgentUpdateInput | undefined { + if (!computer || !piModel) return undefined; + const retainedGrant = + agent.computerId === computer.computer_id && + agent.workspaceGrantId && + computer.workspace_grants.some( + (grant) => grant.grant_id === agent.workspaceGrantId, + ) + ? agent.workspaceGrantId + : undefined; + const workspaceGrantId = + retainedGrant ?? + computer.workspace_grants.find((grant) => grant.mode === "workspace-write") + ?.grant_id ?? + computer.workspace_grants[0]?.grant_id; + if (!workspaceGrantId) return undefined; + + const piRuntime = computer.runtimes.find((runtime) => runtime.type === "pi"); + const piModelEntry = piRuntime?.models.find((item) => item.id === piModel); + const allowedEfforts = piModelEntry?.allowed_reasoning_effort ?? []; + let reasoningEffort: TeamHubAgentReasoningEffort | undefined; + if (allowedEfforts.length) { + if ( + agent.runtimeType === "pi" && + agent.reasoningEffort && + allowedEfforts.includes(agent.reasoningEffort) + ) { + reasoningEffort = agent.reasoningEffort; + } else if ( + piModelEntry?.default_reasoning_effort && + allowedEfforts.includes(piModelEntry.default_reasoning_effort) + ) { + reasoningEffort = piModelEntry.default_reasoning_effort; + } else { + reasoningEffort = allowedEfforts[0]; + } + } + + return { + expected_configuration_revision: agent.configurationRevision, + runtime: { + model: piModel, + ...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}), + placement: { + computer_id: computer.computer_id, + runtime_type: "pi", + type: "local", + workspace_grant_id: workspaceGrantId, + }, + }, + }; +} + +/** + * Summarizes whether a loaded model's local Bifrost gateway endpoint is + * already exposed to Pi on the Computer. + */ +export function gatewayReadiness( + model: Pick, + computer: Pick | undefined, +): GatewayReadiness | undefined { + if (model.state !== "loaded" || !model.endpoint) return undefined; + if (!computer || computer.status !== "online") { + return { label: "Bifrost · Pi unavailable", tone: "muted" }; + } + const piRuntime = computer.runtimes.find((runtime) => runtime.type === "pi"); + if (!piRuntime) { + return { label: "Bifrost · Pi unavailable", tone: "muted" }; + } + if (piRuntime.status === "ready") { + const registered = + Boolean(model.pi_model) && + piRuntime.models.some((item) => item.id === model.pi_model); + return registered + ? { label: "Ready · Bifrost (Pi)", tone: "success" } + : { label: "Bifrost · Registering with Pi…", tone: "info" }; + } + if (piRuntime.status === "unknown") { + return { label: "Bifrost · Setting up Pi…", tone: "info" }; + } + return { label: "Bifrost · Pi unavailable", tone: "muted" }; +} 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 951873dcf..7a1d392dc 100644 --- a/apps/web/src/features/local-models/local-model-details.tsx +++ b/apps/web/src/features/local-models/local-model-details.tsx @@ -1,15 +1,25 @@ -import { useState, type ReactNode } from "react"; import { + Add01Icon, + Alert02Icon, + AlertCircleIcon, Copy01Icon, LinkSquare02Icon, Tick02Icon, } from "@hugeicons/core-free-icons"; +import { useEffect, useRef, useState, type ReactNode } from "react"; import { Button } from "@/components/ui/button"; 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 { + gatewayReadiness, + isAgentUsingModel, + type LocalModelAssignableAgent, +} from "./local-model-agents"; import { capabilityCopy, modelErrorCopy, @@ -18,10 +28,11 @@ import { } from "./local-models-copy"; import type { LocalModelPool, LocalModelsView } from "./local-models-queries"; import { - agentNames, contextLabel, downloadStopped, + downloadTransferSummary, formatGigabytes, + isModelOnComputer, modelCapabilities, modelSourceLabel, parametersLabel, @@ -31,39 +42,133 @@ import { poolOf, safeModelSourceUrl, speedLabel, + type HeaderMemorySegment, type ModelEntry, } from "./local-models-rules"; +type LocalComputer = TeamHubLocalComputerPage["items"][number]; + /** Category colors from the Nature palette, one per model holding memory. */ -const segmentColors = ["bg-rain-500", "bg-lichen-500", "bg-sun-500"] as const; +export const segmentColors = [ + "bg-rain-500", + "bg-lichen-500", + "bg-sun-500", + "bg-bluebell-500", + "bg-lavender-500", +] as const; /** Foundation components hold memory too, but are not models of the list. */ -const componentColor = "bg-muted-foreground/40"; +export const componentColor = "bg-muted-foreground/45"; +export const otherAppsColor = "bg-muted-foreground/25"; + +export function memorySegmentColorClass( + segment: Pick, +): string { + if (segment.kind === "other") return otherAppsColor; + if (segment.kind === "component") return componentColor; + return segmentColors[(segment.modelIndex ?? 0) % segmentColors.length]!; +} -function Fact({ label, children }: { label: string; children: ReactNode }) { +function Fact({ + children, + className, + label, +}: { + children: ReactNode; + className?: string; + label: string; +}) { return ( -
- {label} +
+ {label} {children}
); } +function DiagnosticCallout({ + children, + label, + tone, +}: { + children: ReactNode; + label: string; + tone: "danger" | "warning"; +}) { + return ( +
+ +

+ {label}: + {children} +

+
+ ); +} + function CopyEndpoint({ endpoint, name }: { endpoint: string; name: string }) { const [copied, setCopied] = useState(false); + const mountedRef = useRef(true); + const timerRef = useRef | undefined>(undefined); + + useEffect(() => { + mountedRef.current = true; + return () => { + mountedRef.current = false; + if (timerRef.current !== undefined) { + clearTimeout(timerRef.current); + timerRef.current = undefined; + } + }; + }, []); + + function copy() { + void navigator.clipboard + ?.writeText(endpoint) + .then(() => { + if (!mountedRef.current) return; + setCopied(true); + if (timerRef.current !== undefined) { + clearTimeout(timerRef.current); + } + timerRef.current = setTimeout(() => { + timerRef.current = undefined; + if (mountedRef.current) setCopied(false); + }, 2_000); + }) + .catch(() => undefined); + } + return ( {endpoint} ); @@ -71,18 +176,33 @@ function CopyEndpoint({ endpoint, name }: { endpoint: string; name: string }) { /** Everything about one model on one Computer, shown when its row expands. */ export function ModelFacts({ + assignableAgents = [], + assigning = false, + computer, + disabled = false, entry, + offline = false, + onAssignAgent, + onCreateAgent, + readOnly = false, + usedNames, view, }: { + assignableAgents?: readonly LocalModelAssignableAgent[]; + assigning?: boolean; + computer?: LocalComputer; + disabled?: boolean; entry: ModelEntry; + offline?: boolean; + onAssignAgent?: (agent: LocalModelAssignableAgent) => void; + onCreateAgent?: (piModel: string) => void; + readOnly?: boolean; + usedNames?: readonly string[]; view: LocalModelsView; }) { const { model, status } = entry; - // A stopped download holds only a partial file, so it reads like a model - // not yet on the Computer. const stopped = downloadStopped(model); - const onComputer = - status !== "available" && status !== "unavailable" && !stopped; + const onComputer = isModelOnComputer(model); const pool = poolOf(view, model.pool); const plannedPlace = status === "available" || stopped ? placeName(view, model.pool) : undefined; @@ -90,85 +210,224 @@ export function ModelFacts({ const parameters = parametersLabel(model); const speed = speedLabel(model); const sourceUrl = safeModelSourceUrl(model.source_url); + const transfer = + status === "downloading" ? downloadTransferSummary(model) : undefined; + const resolvedUsedNames = usedNames ?? entry.used_by.map((user) => user.name); + const gateway = gatewayReadiness(model, computer); + const piModel = model.pi_model; + const canRouteAgents = + status === "loaded" && + Boolean(piModel) && + !disabled && + !readOnly && + !offline; + const hasDiagnostics = + Boolean(model.error) || Boolean(model.note) || status === "unavailable"; + const factSeparator = "border-b border-border/50"; + return ( -
- {model.publisher ? ( - {model.publisher} - ) : null} - {parameters ? {parameters} : null} - {capabilities.length ? ( - - - {capabilities.map((capability) => ( - - {capabilityCopy(capability)} - - ))} - - - ) : null} - {model.quant} - - {formatGigabytes(model.memory_bytes)} - {onComputer && pool - ? ` on ${poolLabel(view, pool)}` - : plannedPlace - ? ` on ${plannedPlace}` - : ""} - - - {formatGigabytes(model.download_bytes)} - - {contextLabel(model.context_tokens)} - {speed ? {speed} : null} - {model.license ? {model.license} : null} - {sourceUrl ? ( - - - {modelSourceLabel(sourceUrl)} - - - - ) : null} - {onComputer ? ( - - {entry.used_by.length ? agentNames(entry) : "No Agent yet"} - +
+ {hasDiagnostics ? ( +
+ {model.error ? ( + + {modelErrorCopy(model.error.code)} + + ) : null} + {model.note ? ( + + {modelNoteCopy(model.note.code, placeName(view, model.pool))} + + ) : null} + {status === "unavailable" ? ( + + {unavailableCopy(model.availability.reason)} + + ) : null} +
) : null} - {model.endpoint ? ( - - + +
+ {model.publisher ? ( + + {model.publisher} + + ) : null} + {parameters ? ( + + {parameters} + + ) : null} + + {model.quant} - ) : null} - {model.error ? ( - - - {modelErrorCopy(model.error.code)} + + + {formatGigabytes(model.memory_bytes)} + {onComputer && pool + ? ` on ${poolLabel(view, pool)}` + : plannedPlace + ? ` on ${plannedPlace}` + : ""} - ) : null} - {model.note ? ( - - - {modelNoteCopy(model.note.code, placeName(view, model.pool))} + + + {formatGigabytes(model.download_bytes)} - ) : null} - {status === "unavailable" ? ( - - - {unavailableCopy(model.availability.reason)} + {transfer ? ( + + {transfer} + + ) : null} + + + {contextLabel(model.context_tokens)} - ) : null} + {speed ? ( + + {speed} + + ) : null} + {model.license ? ( + + {model.license} + + ) : null} + {sourceUrl ? ( + + + {modelSourceLabel(sourceUrl)} + + + + ) : null} + {gateway ? ( + + + + + {gateway.label} + + + + ) : null} + {capabilities.length ? ( + + + {capabilities.map((capability) => ( + + {capabilityCopy(capability)} + + ))} + + + ) : null} + {onComputer ? ( + + + + {resolvedUsedNames.length + ? resolvedUsedNames.join(", ") + : "No Agent yet"} + + {canRouteAgents && + assignableAgents.length > 0 && + onAssignAgent ? ( + + ) : null} + {canRouteAgents && piModel && onCreateAgent ? ( + + ) : null} + + + ) : null} + {model.endpoint ? ( + + + + ) : null} +
); } @@ -194,7 +453,7 @@ function PoolBar({ `${pool.total_bytes ? Math.min(100, (bytes / pool.total_bytes) * 100) : 0}%`; return (
- + {pool.kind === "gpu" ? `GPU ${pool.gpu_index}` : label} @@ -286,17 +545,24 @@ export function AdvancedDialog({

Hardware

-
+
- {formatGigabytes(host.ram_total_bytes)} + + {formatGigabytes(host.ram_total_bytes)} + {host.gpus.map((gpu) => ( - {gpu.name} · {formatGigabytes(gpu.total_bytes)} + {gpu.name} ·{" "} + + {formatGigabytes(gpu.total_bytes)} + ))} - {formatGigabytes(host.disk_free_bytes)} + + {formatGigabytes(host.disk_free_bytes)} +
diff --git a/apps/web/src/features/local-models/local-model-dialogs.tsx b/apps/web/src/features/local-models/local-model-dialogs.tsx index e81bae3f2..6e2fe6b7f 100644 --- a/apps/web/src/features/local-models/local-model-dialogs.tsx +++ b/apps/web/src/features/local-models/local-model-dialogs.tsx @@ -6,6 +6,10 @@ import { ModalDialog } from "@/components/ui/modal-dialog"; import { sourceCheckClassName } from "@/features/settings/provider-selection-picker"; import { cn } from "@/lib/cn"; +import { + isAgentUsingModel, + type LocalModelAssignableAgent, +} from "./local-model-agents"; import type { LocalModelsView } from "./local-models-queries"; import { agentNames, @@ -39,27 +43,58 @@ function AffectedAgents({ /** Loading would overfill the pool: offer to unload loaded models first. */ export function MakeRoomDialog({ + assignableAgents = [], check, disabled, entry, onClose, onConfirm, + primaryAgent, view, }: { + assignableAgents?: readonly LocalModelAssignableAgent[]; check: LoadCheck; /** The model can no longer be loaded, or its Computer took no actions. */ disabled: boolean; entry: ModelEntry; onClose: () => void; - onConfirm: (unloadIds: string[]) => void; + onConfirm: (unloadIds: string[], switchAgentIds: string[]) => void; + primaryAgent?: LocalModelAssignableAgent; view: LocalModelsView; }) { const [selected, setSelected] = useState(() => makeRoomSelection(check)); + const [switchOverride, setSwitchOverride] = useState(null); const chosen = new Set(selected); const enough = makesRoom(check, chosen); - const affected = check.loaded.flatMap((item) => - chosen.has(item.model.id) ? item.used_by.map((user) => user.name) : [], + const computerId = view.computer.computer_id; + const chosenLoaded = check.loaded.filter((item) => chosen.has(item.model.id)); + const affectedAssignable = assignableAgents.filter( + (agent) => + !agent.busy && + chosenLoaded.some( + (item) => + isAgentUsingModel(agent, computerId, item.model.pi_model) || + item.used_by.some((user) => user.agent_id === agent.actorId), + ), ); + const affected = [ + ...new Set([ + ...chosenLoaded.flatMap((item) => item.used_by.map((user) => user.name)), + ...affectedAssignable.map((agent) => agent.name), + ]), + ]; + const switchTargets = affectedAssignable.length + ? affectedAssignable + : primaryAgent && + !primaryAgent.busy && + !isAgentUsingModel(primaryAgent, computerId, entry.model.pi_model) + ? [primaryAgent] + : []; + const defaultSwitch = affectedAssignable.length > 0; + const showSwitchOption = + check.loaded.length > 0 && selected.length > 0 && switchTargets.length > 0; + const switchChecked = switchOverride ?? defaultSwitch; + const switchLabel = switchTargets.map((agent) => agent.name).join(", "); 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 343e8ef55..8eb974150 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, useInRouterContext } from "react-router"; -import { Button } from "@/components/ui/button"; +import { AutoHeight } from "@/components/ui/auto-height"; +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 { + isAgentUsingModel, + usedByAgentNames, + type LocalModelAssignableAgent, +} from "./local-model-agents"; import { ModelFacts } from "./local-model-details"; import { unavailableShortCopy } from "./local-models-copy"; import type { @@ -29,14 +37,21 @@ import { downloadPercent, downloadStatusDetail, downloadStopped, + downloadTransferSummary, formatGigabytes, + loadPercent, + loadStatusDetail, placeName, rowBadges, + rowBadgesOverflowTitle, type ModelEntry, type ModelStatus, } from "./local-models-rules"; -const familyIcons: Record, IconSvgElement> = { +type LocalComputer = TeamHubLocalComputerPage["items"][number]; +type ModelFamily = NonNullable; + +const familyIcons: Record = { claude: ClaudeIcon, deepseek: DeepseekIcon, gemma: GoogleGeminiIcon, @@ -51,10 +66,29 @@ const familyIcons: Record, IconSvgElement> = { qwen: QwenIcon, }; +const familyTints: Record = { + claude: "bg-sun-500/12 text-sun-700", + deepseek: "bg-bluebell-500/12 text-bluebell-700", + gemma: "bg-rain-500/12 text-rain-700", + glm: "bg-lake-500/12 text-lake-700", + grok: "bg-foreground/8 text-foreground", + kimi: "bg-bubblegum-500/12 text-bubblegum-700", + llama: "bg-bluebell-500/12 text-bluebell-700", + mistral: "bg-acorn-500/12 text-acorn-700", + openai: "bg-grass-500/12 text-grass-700", + other: "bg-muted text-foreground", + phi: "bg-rain-500/12 text-rain-700", + qwen: "bg-lavender-500/12 text-lavender-700", +}; + function familyIcon(family: LocalModel["family"]) { return family ? familyIcons[family] : AiChipIcon; } +function familyTint(family: LocalModel["family"]) { + return family ? familyTints[family] : familyTints.other; +} + /** * The status word every row shows under its name, with the dot that colors * it. Models not on the Computer carry no dot, like an unconnected Connector. @@ -76,12 +110,76 @@ const statusTags: Record< unavailable: { label: "Can't download" }, }; +function cardSurfaceClass( + status: ModelStatus, + recommended: boolean, + expanded: boolean, +): string { + switch (status) { + case "loaded": + return expanded + ? "border-app-success/55 bg-card" + : "border-[var(--app-status-success-border)] bg-card hover:bg-accent/35"; + case "downloading": + case "loading": + return expanded + ? "border-app-info/55 bg-[var(--app-card-info-surface)]/60" + : "border-[var(--app-card-info-border)] bg-[var(--app-card-info-surface)]/40 hover:bg-[var(--app-card-info-surface)]/65"; + case "failed": + return expanded + ? "border-app-danger/55 bg-[var(--app-status-danger-surface)]/60" + : "border-[var(--app-status-danger-border)] bg-[var(--app-status-danger-surface)]/40 hover:bg-[var(--app-status-danger-surface)]/65"; + case "unavailable": + return expanded + ? "border-border-hover bg-muted/35" + : "border-border/70 bg-muted/25"; + default: + if (status === "available" && recommended) { + return expanded + ? "border-foreground/30 bg-card" + : "border-foreground/20 bg-card hover:bg-accent/40"; + } + return expanded + ? "border-border-hover bg-card" + : "border-border bg-card hover:bg-accent/40"; + } +} + +export function OpenChatLink({ + anotherMeRoomId, + tenantId, + tone = "secondary", +}: { + anotherMeRoomId: string; + tenantId: string; + tone?: "primary" | "secondary" | "subtle"; +}) { + const inRouter = useInRouterContext(); + const to = `/t/${tenantId}/rooms/${anotherMeRoomId}/chat`; + const className = buttonClassName({ size: "compact", tone }); + if (!inRouter) { + return ( + + Open chat + + ); + } + return ( + + Open chat + + ); +} + interface Handlers { + onAssignAgent?: (agent: LocalModelAssignableAgent) => void; onCancel: () => void; + onCreateAgent?: (piModel: string) => void; onDownload: () => void; onLoad: () => void; onRemove: () => void; onToggle: () => void; + onTogglePendingSwitch?: (agentId: string) => void; onUnload: () => void; } @@ -90,27 +188,48 @@ function stopRowClick(event: MouseEvent) { } /** - * One catalog model on one Computer: a full-width Connectors-style card whose - * header carries the status and one action, and which expands to the details. + * One catalog model on one Computer: a full-width card whose header carries + * status, badges, agent routing, and lifecycle action, expanding to full facts. */ export function ModelRow({ + anotherMeRoomId, + assignableAgents = [], + assigning = false, + computer, disabled, + elevateRecommended = false, entry, expanded, + offline = false, + pendingSwitchAgentIds = [], + primaryAgent, + readOnly = false, requested, + tenantId, view, ...handlers }: Handlers & { + anotherMeRoomId?: string; + assignableAgents?: readonly LocalModelAssignableAgent[]; + assigning?: boolean; + computer?: LocalComputer; /** Actions are off: the Computer is offline, the viewer only reads, or one is pending. */ disabled: boolean; + elevateRecommended?: boolean; entry: ModelEntry; expanded: boolean; + offline?: boolean; + pendingSwitchAgentIds?: readonly string[]; + primaryAgent?: LocalModelAssignableAgent; + readOnly?: boolean; /** An action asked for and not yet in a snapshot. */ requested?: LocalModelAction; + tenantId: string; view: LocalModelsView; }) { const panelId = useId(); const { model } = entry; + const computerId = view.computer.computer_id; // A requested load or download reads as started at once; the snapshot that // follows says how it went. const status: ModelStatus = @@ -126,8 +245,15 @@ export function ModelRow({ ? { ...statusTags.failed, label: "Download stopped" } : statusTags[status]; const progress = downloadPercent(model); + const loadProgress = loadPercent(model); + const transferSummary = + status === "downloading" && !started + ? downloadTransferSummary(model) + : undefined; const download = formatGigabytes(model.download_bytes); const badges = rowBadges(model); + const overflowTitle = rowBadgesOverflowTitle(model); + const usedNames = usedByAgentNames(entry, assignableAgents, computerId); const detail = { available: model.recommended ? `${download} · Recommended` @@ -143,7 +269,7 @@ export function ModelRow({ ] .filter(Boolean) .join(" · "), - loading: where ?? null, + loading: loadStatusDetail(model, where), unavailable: unavailableShortCopy(model.availability.reason), }[status]; @@ -167,27 +293,85 @@ export function ModelRow({ status === "idle" || (status === "failed" && !stopped); + const primaryUsingModel = Boolean( + primaryAgent && + model.pi_model && + isAgentUsingModel(primaryAgent, computerId, model.pi_model), + ); + const showOpenChat = + !readOnly && + status === "loaded" && + Boolean(primaryAgent?.isAnotherMe) && + primaryUsingModel && + Boolean(anotherMeRoomId); + const showUseWithPrimary = + !readOnly && + status === "loaded" && + Boolean(model.pi_model) && + Boolean(primaryAgent) && + !primaryUsingModel && + Boolean(handlers.onAssignAgent); + const showUseWhenReady = + !readOnly && + !offline && + (status === "downloading" || status === "loading") && + Boolean(model.pi_model) && + Boolean(primaryAgent) && + !primaryAgent!.busy && + !primaryUsingModel && + Boolean(handlers.onTogglePendingSwitch); + const isQueued = Boolean( + primaryAgent && pendingSwitchAgentIds.includes(primaryAgent.actorId), + ); + const primaryActionTone = + model.recommended && + elevateRecommended && + (status === "available" || status === "idle" || status === "failed") + ? "primary" + : status === "downloading" + ? "ghost" + : "secondary"; + const progressBar = + entry.status === "downloading" + ? { + ariaLabel: `${model.name} download progress`, + fillPercent: progress, + pulse: model.download?.step === "verify", + value: progress, + } + : status === "loading" && loadProgress !== null + ? { + ariaLabel: `${model.name} load progress`, + fillPercent: Math.max(loadProgress, 8), + pulse: false, + value: loadProgress, + } + : null; + return (
-
+
- {badges.length ? ( - - {badges.map((badge) => ( + + {usedNames.length > 0 || badges.length > 0 ? ( +
+ {usedNames.length > 0 ? ( - {badge} + Used by {usedNames.join(", ")} - ))} - + ) : null} + {badges.map((badge, index) => { + const isOverflow = + index === badges.length - 1 && + overflowTitle !== undefined && + badge.startsWith("+"); + return ( + + {badge} + + ); + })} +
) : null} - {action ? ( + +
+ {showOpenChat && anotherMeRoomId ? ( + + ) : null} + {showUseWithPrimary && primaryAgent && handlers.onAssignAgent ? ( + + ) : null} + {showUseWhenReady && + primaryAgent && + handlers.onTogglePendingSwitch ? ( + + ) : null} + {action ? ( + + ) : null} - ) : null} - +
- {entry.status === "downloading" ? ( + + {progressBar ? (
) : null} - {expanded ? ( -
- {model.description ? ( -

- {model.description} -

- ) : null} - - {removable ? ( - - ) : null} - {stopped ? ( - - ) : null} -
- ) : null} + entry={entry} + offline={offline} + onAssignAgent={handlers.onAssignAgent} + onCreateAgent={handlers.onCreateAgent} + readOnly={readOnly} + usedNames={usedNames} + view={view} + /> + {removable ? ( + + ) : null} + {stopped ? ( + + ) : null} +
+ ) : null} +
); } diff --git a/apps/web/src/features/local-models/local-models-page.test.tsx b/apps/web/src/features/local-models/local-models-page.test.tsx index b1b30e16c..7742b5da4 100644 --- a/apps/web/src/features/local-models/local-models-page.test.tsx +++ b/apps/web/src/features/local-models/local-models-page.test.tsx @@ -1,5 +1,6 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { + act, cleanup, fireEvent, render, @@ -9,12 +10,13 @@ import { } from "@testing-library/react"; import { Suspense } from "react"; import { MemoryRouter, Route, Routes, useLocation } from "react-router"; -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { TeamHubEventSync } from "@/features/rooms/event-sync"; import { TeamHubRuntimeProvider } from "@/features/teamhub/teamhub-runtime"; import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; import { + acceptTenantAgentUpdates, createTestTeamHubRuntime, type TestTeamHubRuntime, } from "@/test/teamhub-test-runtime"; @@ -22,6 +24,7 @@ import { teamHubErrorResponse } from "@/test/teamhub-test-server"; import { LocalModelsPage } from "./local-models-page"; import type { LocalModelsView } from "./local-models-queries"; +import { localModelsComputerStorageKey } from "./local-models-rules"; import { gb, gpuPool, @@ -103,6 +106,15 @@ function CurrentSearch() { return

Address search: {useLocation().search}

; } +function ChatDestination() { + const location = useLocation(); + return ( +

+ Chat route: {location.pathname}|{JSON.stringify(location.state ?? null)} +

+ ); +} + function renderPage(fixture: Fixture, search = "") { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } }, @@ -129,6 +141,10 @@ function renderPage(fixture: Fixture, search = "") { element={

Computers page

} path="/t/:tenantId/settings/computers" /> + } + path="/t/:tenantId/rooms/:roomId/chat" + /> @@ -219,7 +235,14 @@ function withModel( }; } +beforeEach(() => { + window.localStorage.clear(); +}); + afterEach(() => { + window.localStorage.clear(); + vi.useRealTimers(); + vi.restoreAllMocks(); delete (window as { lingerDesktop?: unknown }).lingerDesktop; }); @@ -1177,4 +1200,507 @@ describe("LocalModelsPage", () => { "GPT-OSS 20B: Downloading · 25% · 50 MB/s · 5m left", ); }); + + it("renders card skeletons while loading and a segmented GPU memory bar once loaded", async () => { + const fixture = setup(); + let releaseView: ((view: LocalModelsView) => void) | undefined; + fixture.runtime.server.on( + "GET", + viewPath, + () => + new Promise((resolve) => { + releaseView = resolve; + }), + ); + renderPage(fixture); + + const loadingStatus = await screen.findByRole("status", { + name: "Loading models", + }); + expect(loadingStatus).toHaveAttribute("aria-busy", "true"); + expect(within(loadingStatus).getByText("Loading models")).toBeVisible(); + + releaseView?.(labView()); + await findModels(); + + expect( + screen.getByText("22.5 GB of 48 GB GPU memory in use"), + ).toBeVisible(); + expect(screen.getByText(/25\.5 GB free/)).toBeVisible(); + expect( + screen.getByRole("img", { + name: "GPU memory breakdown: 22.5 GB of 48 GB in use", + }), + ).toBeVisible(); + expect(screen.getByText("Qwen3 Coder 30B · 21.4 GB")).toBeVisible(); + expect(screen.getByText("Qwen3 Embedding · 1.1 GB")).toBeVisible(); + }); + + it("persists chosen Computer, defaults to loaded online Pi Computer, and preserves computer param on offline link", async () => { + const lab = labView({ + computer: { ...labView().computer, status: "offline" }, + }); + const mac = macView(); + const labItem = { + ...inventoryComputer(lab), + status: "offline" as const, + }; + const macItem = { + ...inventoryComputer(mac), + runtimes: [ + { + models: [ + { + id: "localmodels-library/gpt-oss-20b", + label: "GPT-OSS 20B", + }, + ], + status: "ready" as const, + type: "pi" as const, + }, + ], + status: "online" as const, + }; + const fixture = setup([lab, mac], [labItem, macItem]); + renderPage(fixture); + await findModels(); + + const picker = screen.getByRole("combobox", { name: "Computer" }); + expect(picker).toHaveTextContent("Mac Studio · 1 loaded"); + fireEvent.click(picker); + expect( + await screen.findByRole("option", { name: "lab-118 · Offline" }), + ).toBeVisible(); + fireEvent.click(screen.getByRole("option", { name: "lab-118 · Offline" })); + + await waitFor(() => + expect( + window.localStorage.getItem(localModelsComputerStorageKey(tenantId)), + ).toBe("computer-lab"), + ); + expect( + await screen.findByRole("link", { name: "Computers" }), + ).toHaveAttribute( + "href", + "/t/tenant-northstar/settings/computers?computer=computer-lab", + ); + + cleanup(); + renderPage(fixture); + await waitFor(() => + expect( + screen.getByRole("combobox", { name: "Computer" }), + ).toHaveTextContent("lab-118 · Offline"), + ); + }); + + it("switches Another Me with Use with CTA, shows confirmation banner, and navigates to chat", async () => { + const fixture = setup(); + acceptTenantAgentUpdates(fixture.runtime); + renderPage(fixture); + await findModels(); + + const coderCard = card("Qwen3 Coder 30B"); + expect( + within(coderCard).getByText("Used by Research agent, Code reviewer"), + ).toBeVisible(); + fireEvent.click( + await within(coderCard).findByRole("button", { + name: "Use with Jenny's Delegate", + }), + ); + + expect( + await screen.findByText("Jenny's Delegate now uses Qwen3 Coder 30B."), + ).toBeVisible(); + expect(screen.queryByRole("status")).not.toBeInTheDocument(); + + const delegate = fixture.runtime.seed.tenantAgents.find( + (agent) => agent.actor.id === "actor-delegate", + )!; + expect(delegate.runtime).toEqual({ + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-linger-workspace", + }, + }); + + await waitFor(() => + expect( + within(card("Qwen3 Coder 30B")).getByRole("link", { + name: "Open chat", + }), + ).toBeVisible(), + ); + fireEvent.click( + within(card("Qwen3 Coder 30B")).getByRole("link", { name: "Open chat" }), + ); + expect( + await screen.findByText( + 'Chat route: /t/tenant-northstar/rooms/room-another-me/chat|{"focusComposer":true}', + ), + ).toBeVisible(); + }); + + it("queues Use when ready on loading models, auto-switches on load, and clears on cancel or failure", async () => { + const fixture = setup([ + withModel(labView(), "gemma3-27b:Q4_K_M", { + state: "loading", + }), + withModel(macView(), "llama3.3-70b:Q4_K_M", { + pi_model: "localmodels-library/llama3.3-70b", + state: "downloading", + }), + ]); + acceptTenantAgentUpdates(fixture.runtime); + renderPage(fixture); + await findModels(); + + const gemmaCard = card("Gemma 3 27B"); + const queueBtn = await within(gemmaCard).findByRole("button", { + name: "Use when ready", + }); + expect(queueBtn).toHaveAttribute("aria-pressed", "false"); + fireEvent.click(queueBtn); + expect( + within(gemmaCard).getByRole("button", { + name: "Queued for Jenny's Delegate", + }), + ).toHaveAttribute("aria-pressed", "true"); + + publishSnapshot( + fixture, + withModel(labView(), "gemma3-27b:Q4_K_M", { + state: "loaded", + }), + ); + expect( + await screen.findByText("Jenny's Delegate now uses Gemma 3 27B."), + ).toBeVisible(); + + cleanup(); + renderPage(fixture, "?computer=computer-mac"); + await findModels(); + const llamaCard = card("Llama 3.3 70B"); + fireEvent.click( + await within(llamaCard).findByRole("button", { name: "Use when ready" }), + ); + expect( + within(llamaCard).getByRole("button", { + name: "Queued for Jenny's Delegate", + }), + ).toBeVisible(); + fireEvent.click( + within(llamaCard).getByRole("button", { name: "Cancel Llama 3.3 70B" }), + ); + await waitFor(() => + expect(fixture.actions.at(-1)?.body).toEqual({ action: "cancel" }), + ); + publishSnapshot( + fixture, + withModel(macView(), "llama3.3-70b:Q4_K_M", { + pi_model: "localmodels-library/llama3.3-70b", + state: "loaded", + }), + ); + await waitFor(() => expect(statusOf("Llama 3.3 70B")).toMatch(/^Loaded/)); + expect( + screen.queryByText("Jenny's Delegate now uses Llama 3.3 70B."), + ).not.toBeInTheDocument(); + }); + + it("pre-checks MakeRoom handoff when unloading a model used by an assignable Agent and keeps it opt-in otherwise", async () => { + const fixture = setup(); + acceptTenantAgentUpdates(fixture.runtime); + renderPage(fixture, "?computer=computer-mac"); + await findModels(); + + fireEvent.click( + screen.getByRole("button", { name: "Retry Llama 3.3 70B" }), + ); + const optInDialog = screen.getByRole("dialog", { + name: "Make room to load", + }); + const optInCheckbox = within(optInDialog).getByRole("checkbox", { + name: "Switch Jenny's Delegate to Llama 3.3 70B once loaded", + }); + expect(optInCheckbox).not.toBeChecked(); + fireEvent.click( + within(optInDialog).getByRole("button", { name: "Cancel" }), + ); + + const delegate = fixture.runtime.seed.tenantAgents.find( + (agent) => agent.actor.id === "actor-delegate", + )!; + delegate.runtime = { + model: "localmodels-library/gpt-oss-20b", + placement: { + computer_id: "computer-mac", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-linger-workspace", + }, + }; + cleanup(); + renderPage(fixture, "?computer=computer-mac"); + await findModels(); + + fireEvent.click( + screen.getByRole("button", { name: "Retry Llama 3.3 70B" }), + ); + const dialog = screen.getByRole("dialog", { name: "Make room to load" }); + const handoff = await within(dialog).findByRole("checkbox", { + name: "Switch Jenny's Delegate to Llama 3.3 70B once loaded", + }); + expect(handoff).toBeChecked(); + fireEvent.click( + within(dialog).getByRole("button", { name: "Unload and load" }), + ); + await waitFor(() => expect(fixture.actions).toHaveLength(1)); + + publishSnapshot( + fixture, + withModel(macView(), "llama3.3-70b:Q4_K_M", { + error: undefined, + pi_model: "localmodels-library/llama3.3-70b", + state: "loaded", + }), + ); + expect( + await screen.findByText("Jenny's Delegate now uses Llama 3.3 70B."), + ).toBeVisible(); + }); + + it("shows Gateway readiness, Switch Agent select with busy guard, lazy New Agent dialog, and CopyEndpoint auto-reset", async () => { + const view = labView(); + const fixture = setup([view]); + acceptTenantAgentUpdates(fixture.runtime); + const labInventory = fixture.runtime.seed.localComputers[0]!; + labInventory.runtimes = [ + { + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + status: "ready", + type: "pi", + }, + ]; + const codexAgent = fixture.runtime.seed.tenantAgents.find( + (agent) => agent.actor.id === "actor-codex", + )!; + codexAgent.availability.active_task_count = 1; + + renderPage(fixture); + await findModels(); + + fireEvent.click(screen.getByRole("button", { name: "Qwen3 Coder 30B" })); + const details = screen.getByRole("region", { + name: "Qwen3 Coder 30B details", + }); + expect(details).toHaveTextContent("GatewayReady · Bifrost (Pi)"); + + fireEvent.click( + await within(details).findByRole("combobox", { name: "Switch Agent" }), + ); + expect( + await screen.findByRole("option", { name: "Codex · Working" }), + ).toBeDisabled(); + fireEvent.click(screen.getByRole("option", { name: "Jenny's Delegate" })); + expect( + await screen.findByText("Jenny's Delegate now uses Qwen3 Coder 30B."), + ).toBeVisible(); + + fireEvent.click(within(details).getByRole("button", { name: "New Agent" })); + const createDialog = await screen.findByRole("dialog", { + name: "Create local agent", + }); + expect(createDialog).toBeVisible(); + expect( + await within(createDialog).findByRole("combobox", { + name: "Local Computer", + }), + ).toHaveTextContent("lab-118"); + expect( + within(createDialog).getByRole("combobox", { name: "Runtime" }), + ).toHaveTextContent("Pi"); + expect( + within(createDialog).getByRole("combobox", { name: "Model" }), + ).toHaveTextContent("Qwen3 Coder 30B"); + fireEvent.click( + await within(createDialog).findByRole("button", { name: "Cancel" }), + ); + + const writeText = vi.fn().mockResolvedValue(undefined); + Object.defineProperty(navigator, "clipboard", { + configurable: true, + value: { writeText }, + }); + vi.useFakeTimers(); + const copyButton = within(details).getByRole("button", { + name: "Copy Qwen3 Coder 30B endpoint", + }); + fireEvent.click(copyButton); + await act(async () => { + await Promise.resolve(); + }); + expect(writeText).toHaveBeenCalledWith("http://127.0.0.1:18100/v1"); + expect(copyButton).toHaveAttribute("title", "Copied"); + await act(async () => { + await vi.advanceTimersByTimeAsync(2_000); + }); + expect(copyButton).toHaveAttribute("title", "Copy endpoint"); + }); + + it("elevates the Recommended Download button to primary when no model is loaded or active", async () => { + const view = withModel(labView(), "qwen3-coder-30b:Q4_K_M", { + endpoint: undefined, + state: "idle", + }); + renderPage(setup([view])); + await findModels(); + + expect( + screen.getByRole("button", { name: "Download GPT-OSS 20B" }), + ).toHaveClass("bg-primary"); + }); + + it("shows download transfer tooltip and Transfer fact row for mirror and cache downloads", async () => { + const mb = 1024 * 1024; + const fixture = setup([ + withModel(labView(), "gpt-oss-20b:Q4_K_M", { + download: { + bytes_per_second: 50 * mb, + connections: 4, + done_bytes: 5_000 * mb, + source: "modelscope", + step: "download", + total_bytes: 20_000 * mb, + }, + state: "downloading", + }), + ]); + renderPage(fixture); + await findModels(); + + expect(statusOf("GPT-OSS 20B")).toBe( + "Downloading · 25% · 50 MB/s · 5m left", + ); + const downloadingCard = card("GPT-OSS 20B"); + expect( + within(downloadingCard).getByTitle( + "ModelScope mirror · 4 connections · binary MB/s (MiB/s)", + ), + ).toBeVisible(); + + fireEvent.click(screen.getByRole("button", { name: "GPT-OSS 20B" })); + const details = screen.getByRole("region", { + name: "GPT-OSS 20B details", + }); + expect(details).toHaveTextContent( + "TransferModelScope mirror · 4 connections · binary MB/s (MiB/s)", + ); + + publishSnapshot( + fixture, + withModel(labView(), "gpt-oss-20b:Q4_K_M", { + download: { + connections: 0, + done_bytes: 0, + source: "cache", + step: "download", + total_bytes: 20_000 * mb, + }, + state: "downloading", + }), + ); + await waitFor(() => + expect(statusOf("GPT-OSS 20B")).toBe( + "Downloading · Reusing local cache…", + ), + ); + expect( + within(card("GPT-OSS 20B")).getByTitle("Reusing verified local cache"), + ).toBeVisible(); + expect( + screen.getByRole("region", { name: "GPT-OSS 20B details" }), + ).toHaveTextContent("TransferReusing verified local cache"); + }); + + it("shows granular loading step text and determinate progress bar while loading", async () => { + const fixture = setup([ + withModel(labView(), "gemma3-27b:Q4_K_M", { + state: "loading", + }), + ]); + renderPage(fixture); + await findModels(); + + expect(statusOf("Gemma 3 27B")).toBe("Loading · GPU 1"); + expect( + screen.queryByRole("progressbar", { name: "Gemma 3 27B load progress" }), + ).not.toBeInTheDocument(); + + publishSnapshot( + fixture, + withModel(labView(), "gemma3-27b:Q4_K_M", { + load: { done_steps: 0, step: "prepare", total_steps: 7 }, + pool: "gpu:0", + state: "loading", + }), + ); + await waitFor(() => + expect(statusOf("Gemma 3 27B")).toBe("Loading · Preparing server…"), + ); + const bar = screen.getByRole("progressbar", { + name: "Gemma 3 27B load progress", + }); + expect(bar).toHaveAttribute("aria-valuenow", "0"); + + publishSnapshot( + fixture, + withModel(labView(), "gemma3-27b:Q4_K_M", { + load: { done_steps: 1, step: "start", total_steps: 7 }, + pool: "gpu:0", + state: "loading", + }), + ); + await waitFor(() => + expect(statusOf("Gemma 3 27B")).toBe("Loading · Starting server…"), + ); + expect(bar).toHaveAttribute("aria-valuenow", "14"); + + publishSnapshot( + fixture, + withModel(labView(), "gemma3-27b:Q4_K_M", { + load: { done_steps: 4, step: "load", total_steps: 7 }, + pool: "gpu:0", + state: "loading", + }), + ); + await waitFor(() => + expect(statusOf("Gemma 3 27B")).toBe( + "Loading · Loading weights to GPU 0 · 57%", + ), + ); + expect(bar).toHaveAttribute("aria-valuenow", "57"); + + publishSnapshot( + fixture, + withModel(labView(), "gemma3-27b:Q4_K_M", { + load: { done_steps: 7, step: "warm", total_steps: 7 }, + pool: "gpu:0", + state: "loading", + }), + ); + await waitFor(() => + expect(statusOf("Gemma 3 27B")).toBe("Loading · Warming up · 100%"), + ); + expect(bar).toHaveAttribute("aria-valuenow", "100"); + }); }); diff --git a/apps/web/src/features/local-models/local-models-page.tsx b/apps/web/src/features/local-models/local-models-page.tsx index 745e734bf..521a0a7cc 100644 --- a/apps/web/src/features/local-models/local-models-page.tsx +++ b/apps/web/src/features/local-models/local-models-page.tsx @@ -1,4 +1,8 @@ -import { AiChipIcon } from "@hugeicons/core-free-icons"; +import { + AiChipIcon, + Cancel01Icon, + CheckmarkCircle02Icon, +} from "@hugeicons/core-free-icons"; import { useMutation, useMutationState, @@ -6,27 +10,57 @@ import { useQueryClient, useSuspenseQuery, } from "@tanstack/react-query"; -import { type ReactNode, useState } from "react"; +import { + type ReactNode, + Suspense, + lazy, + useEffect, + useEffectEvent, + useMemo, + useRef, + useState, +} from "react"; import { Link, useParams, useSearchParams } from "react-router"; import { Button } from "@/components/ui/button"; import { FormFeedback } from "@/components/ui/form-feedback"; import { Icon } from "@/components/ui/icon"; import { Select, SelectItem } from "@/components/ui/select"; +import { Skeleton } from "@/components/ui/skeleton"; import { relativeTime } from "@/features/local-computer/computer-relative-time"; import { useDesktopComputer } from "@/features/local-computer/desktop-computer"; import { localComputersQueryOptions } from "@/features/local-computer/local-computer-queries"; +import { + anotherMeQueryOptions, + teamHubIdentityQueryOptions, + tenantActorsQueryKey, + tenantAgentAvailabilityQueryKey, + tenantAgentAvailabilityQueryOptions, + tenantAgentsQueryKey, + tenantAgentsQueryOptions, + tenantRoomsQueryKey, +} from "@/features/rooms/room-queries"; import { requireRoomRouteParam } from "@/features/rooms/room-route-params"; +import { teamHubData } from "@/features/teamhub/teamhub-api"; import { TeamHubError } from "@/features/teamhub/teamhub-error"; import { useTeamHubRuntime } from "@/features/teamhub/teamhub-runtime"; +import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; +import { cn } from "@/lib/cn"; -import { AdvancedDialog } from "./local-model-details"; -import { ModelRow } from "./local-model-row"; +import { + assignableLocalModelAgents, + buildLocalModelRuntimeUpdate, + isAgentUsingModel, + primaryLocalModelAgent, + type LocalModelAssignableAgent, +} from "./local-model-agents"; +import { AdvancedDialog, memorySegmentColorClass } from "./local-model-details"; import { MakeRoomDialog, RemoveModelDialog, UnloadModelDialog, } from "./local-model-dialogs"; +import { ModelRow, OpenChatLink } from "./local-model-row"; import { actionErrorCopy, libraryUnavailableCopy } from "./local-models-copy"; import { actOnLocalComputerModel, @@ -38,21 +72,41 @@ import { type LocalModelsView, } from "./local-models-queries"; import { + computerPickerLabel, downloadStopped, formatGigabytes, + headerMemorySegments, loadCheck, memorySummary, modelEntries, + readRememberedLocalModelsComputerId, + rememberLocalModelsComputerId, + selectDefaultLocalModelsComputer, + type HeaderMemorySegment, type ModelEntry, type ModelStatus, } from "./local-models-rules"; +type LocalComputer = TeamHubLocalComputerPage["items"][number]; + +const QuickCreateLocalAgentDialog = lazy(async () => ({ + default: (await import("@/features/rooms/quick-create-local-agent-dialog")) + .QuickCreateLocalAgentDialog, +})); + type Dialog = | { kind: "advanced" } + | { kind: "createAgent"; piModel: string } | { kind: "makeRoom"; modelId: string } | { kind: "remove"; modelId: string } | { kind: "unload"; modelId: string }; +interface SwitchedNotice { + agentNames: string[]; + includesAnotherMe: boolean; + modelName: string; +} + /** * The model states each confirmed action applies to, as the row offers it. A * stopped download is failed too but offers none of them. @@ -63,13 +117,17 @@ const actionStatuses: Record<"load" | "remove" | "unload", ModelStatus[]> = { unload: ["loaded"], }; -function computersPath(tenantId: string) { - return `/t/${tenantId}/settings/computers`; +function computersPath(tenantId: string, computerId?: string) { + const base = `/t/${tenantId}/settings/computers`; + return computerId + ? `${base}?computer=${encodeURIComponent(computerId)}` + : base; } /** * The model library of one Computer, chosen by `?computer=`. Without it the - * desktop app opens its own Computer and a browser the first one. + * desktop app opens its own Computer and a browser opens the remembered or + * most ready owned Computer. */ export function LocalModelsPage() { const { client } = useTeamHubRuntime(); @@ -81,14 +139,56 @@ export function LocalModelsPage() { const [searchParams, setSearchParams] = useSearchParams(); const computers = inventory.items; const linkedId = searchParams.get("computer"); - const requestedId = linkedId ?? desktop?.status?.computerId; - const owned = computers.find((item) => item.computer_id === requestedId); + const ownedFromLink = linkedId + ? computers.find((item) => item.computer_id === linkedId) + : undefined; // A linked Computer the viewer does not own may be another member's: the // library API, not the owned inventory, decides whether they may read it. - const sharedId = linkedId && !owned ? linkedId : undefined; - const computer = sharedId ? undefined : (owned ?? computers[0]); + const sharedId = linkedId && !ownedFromLink ? linkedId : undefined; + const rememberedId = readRememberedLocalModelsComputerId(tenantId); + const computer = sharedId + ? undefined + : (ownedFromLink ?? + selectDefaultLocalModelsComputer(computers, { + desktopComputerId: desktop?.status?.computerId, + rememberedComputerId: rememberedId, + })); + const ownedLinkedComputerId = ownedFromLink?.computer_id; + const identityQuery = useQuery(teamHubIdentityQueryOptions(client)); + const agentsQuery = useQuery(tenantAgentsQueryOptions(client, tenantId)); + const availabilityQuery = useQuery( + tenantAgentAvailabilityQueryOptions(client, tenantId), + ); + const anotherMeQuery = useQuery(anotherMeQueryOptions(client, tenantId)); + const currentActorId = identityQuery.data?.actors.find( + (actor) => actor.tenant_id === tenantId, + )?.id; + const anotherMeActorId = anotherMeQuery.data?.agent_actor_id; + const anotherMeRoomId = anotherMeQuery.data?.room_id; + const assignableAgents = useMemo( + () => + assignableLocalModelAgents(agentsQuery.data?.items ?? [], { + anotherMeActorId, + availabilities: availabilityQuery.data?.items, + currentActorId, + }), + [ + agentsQuery.data?.items, + anotherMeActorId, + availabilityQuery.data?.items, + currentActorId, + ], + ); + const agentsLoaded = agentsQuery.data !== undefined; + + useEffect(() => { + if (linkedId && ownedLinkedComputerId) { + rememberLocalModelsComputerId(tenantId, ownedLinkedComputerId); + } + }, [linkedId, ownedLinkedComputerId, tenantId]); function select(computerId: string) { + rememberLocalModelsComputerId(tenantId, computerId); setSearchParams( (current) => { const next = new URLSearchParams(current); @@ -103,6 +203,9 @@ export function LocalModelsPage() {
{sharedId ? ( ) : computer ? ( 1 ? ( ) : null } + displayName={computer.display_name} key={computer.computer_id} tenantId={tenantId} /> @@ -189,12 +294,101 @@ function Notice({ children }: { children: ReactNode }) { ); } +function HeaderMemoryMeter({ + gpu, + segments, + totalBytes, + usedBytes, +}: { + gpu: boolean; + segments: readonly HeaderMemorySegment[]; + totalBytes: number; + usedBytes: number; +}) { + return ( +
+
+ {segments.map((segment) => ( + + ))} +
+ {segments.length ? ( +

+ {segments.map((segment) => ( + + + + {segment.name} · {formatGigabytes(segment.bytes)} + + + ))} +

+ ) : null} +
+ ); +} + +function ModelsSkeleton() { + return ( +
+ Loading models + {[0, 1, 2].map((index) => ( +
+ +
+ + +
+ +
+ ))} +
+ ); +} + function ComputerModels({ + agentsLoaded, + anotherMeRoomId, + assignableAgents, + computer, computerId, computerPicker, displayName, tenantId, }: { + agentsLoaded: boolean; + anotherMeRoomId?: string; + assignableAgents: readonly LocalModelAssignableAgent[]; + computer?: LocalComputer; computerId: string; computerPicker: ReactNode; /** Absent for a Computer outside the viewer's own inventory. */ @@ -206,18 +400,36 @@ function ComputerModels({ const view = query.data; const [dialog, setDialog] = useState(null); const summary = view?.available ? memorySummary(view) : undefined; + const segments = view?.available ? headerMemorySegments(view) : []; + const freeBytes = summary + ? Math.max(0, summary.totalBytes - summary.usedBytes) + : 0; return ( <>
-
+

Local models

{summary?.totalBytes ? ( -

- {formatGigabytes(summary.usedBytes)} of{" "} - {formatGigabytes(summary.totalBytes)}{" "} - {summary.gpu ? "GPU memory" : "memory"} in use -

+
+

+ + {formatGigabytes(summary.usedBytes)} of{" "} + {formatGigabytes(summary.totalBytes)}{" "} + {summary.gpu ? "GPU memory" : "memory"} in use + + + {" "} + · {formatGigabytes(freeBytes)} free + +

+ +
) : null}
@@ -235,6 +447,10 @@ function ComputerModels({ {view ? ( ) : ( -

- Loading models -

+ )} ); } function Library({ + agentsLoaded, + anotherMeRoomId, + assignableAgents, + computer, dialog, setDialog, tenantId, view, }: { + agentsLoaded: boolean; + anotherMeRoomId?: string; + assignableAgents: readonly LocalModelAssignableAgent[]; + computer?: LocalComputer; dialog: Dialog | null; setDialog: (dialog: Dialog | null) => void; tenantId: string; @@ -286,6 +508,16 @@ function Library({ code?: string; modelId: string; } | null>(null); + const [pendingSwitch, setPendingSwitch] = useState>( + {}, + ); + const [switchedNotice, setSwitchedNotice] = useState( + null, + ); + const [assignError, setAssignError] = useState(null); + const triggeredAutoSwitchRef = useRef>(new Set()); + const primaryAgent = primaryLocalModelAgent(assignableAgents, computerId); + // An action the Computer accepted, shown on its row until a snapshot moves // the model on: a different status or a newer snapshot. const [accepted, setAccepted] = useState< @@ -294,7 +526,10 @@ function Library({ { action: LocalModelAction; from?: ModelStatus; observedAt?: string } > >({}); - const entries = view.available ? modelEntries(view) : []; + const entries = useMemo( + () => (view.available ? modelEntries(view) : []), + [view], + ); const find = (modelId: string) => entries.find((entry) => entry.model.id === modelId); const mutationKey = localModelActionMutationKey(tenantId, computerId); @@ -302,11 +537,13 @@ function Library({ mutationFn: (input: LocalModelActionInput) => actOnLocalComputerModel(client, tenantId, computerId, input), mutationKey, - onError: (error, input) => + onError: (error, input) => { + clearPendingSwitch(input.modelId); setFailure({ code: error instanceof TeamHubError ? error.code : undefined, modelId: input.modelId, - }), + }); + }, onMutate: (input) => { setFailure(null); return { from: find(input.modelId)?.status }; @@ -329,6 +566,79 @@ function Library({ queryKey: localModelsQueryKey(tenantId, computerId), }), }); + + const assignMutation = useMutation({ + mutationFn: async (input: { + agents: readonly LocalModelAssignableAgent[]; + modelId: string; + modelName: string; + piModel: string; + }) => { + const updates = input.agents.flatMap((agent) => { + const body = buildLocalModelRuntimeUpdate( + agent, + computer, + input.piModel, + ); + return body ? [{ agent, body }] : []; + }); + if (!updates.length) { + throw new Error( + "This computer has no writable workspace for Agents yet.", + ); + } + await Promise.all( + updates.map(({ agent, body }) => + teamHubData( + client.api.PATCH("/v0/tenants/{tenantId}/agents/{agentId}", { + body, + params: { path: { agentId: agent.actorId, tenantId } }, + }), + ), + ), + ); + return { + agentNames: updates.map(({ agent }) => agent.name), + includesAnotherMe: updates.some(({ agent }) => agent.isAnotherMe), + modelName: input.modelName, + }; + }, + onError: (error) => { + setSwitchedNotice(null); + setAssignError( + error instanceof Error + ? error.message + : "Couldn't switch Agent to this model. Try again.", + ); + void queryClient.invalidateQueries({ + queryKey: tenantAgentsQueryKey(tenantId), + }); + }, + onMutate: () => { + setAssignError(null); + }, + onSuccess: async (result) => { + setSwitchedNotice(result); + await Promise.all([ + queryClient.invalidateQueries({ + queryKey: tenantAgentsQueryKey(tenantId), + }), + queryClient.invalidateQueries({ + queryKey: tenantAgentAvailabilityQueryKey(tenantId), + }), + queryClient.invalidateQueries({ + queryKey: tenantActorsQueryKey(tenantId), + }), + queryClient.invalidateQueries({ + queryKey: tenantRoomsQueryKey(tenantId), + }), + queryClient.invalidateQueries({ + queryKey: localModelsQueryKey(tenantId, computerId), + }), + ]); + }, + }); + const sending = useMutationState({ filters: { mutationKey, status: "pending" }, select: (mutation) => @@ -349,19 +659,146 @@ function Library({ } const offline = view.computer.status === "offline"; const readOnly = !view.viewer_is_owner; + const hasActiveOrLoadedModel = entries.some((entry) => { + const requested = requests.get(entry.model.id); + const effectiveStatus = + requested === "load" + ? "loading" + : requested === "download" + ? "downloading" + : entry.status; + return ( + effectiveStatus === "loaded" || + effectiveStatus === "loading" || + effectiveStatus === "downloading" + ); + }); + + function clearPendingSwitch(modelId: string) { + triggeredAutoSwitchRef.current.delete(modelId); + setPendingSwitch((current) => { + if (!(modelId in current)) return current; + const next = { ...current }; + delete next[modelId]; + return next; + }); + } + + function queuePendingSwitch(modelId: string, agentIds: string[]) { + triggeredAutoSwitchRef.current.delete(modelId); + setPendingSwitch((current) => { + if (!agentIds.length) { + if (!(modelId in current)) return current; + const next = { ...current }; + delete next[modelId]; + return next; + } + return { ...current, [modelId]: agentIds }; + }); + } + + function togglePendingSwitch(modelId: string, agentId: string) { + triggeredAutoSwitchRef.current.delete(modelId); + setPendingSwitch((current) => { + const existing = current[modelId] ?? []; + const nextIds = existing.includes(agentId) + ? existing.filter((id) => id !== agentId) + : [...existing, agentId]; + if (!nextIds.length) { + const next = { ...current }; + delete next[modelId]; + return next; + } + return { ...current, [modelId]: nextIds }; + }); + } + + const processPendingSwitch = useEffectEvent(() => { + const pendingEntries = Object.entries(pendingSwitch); + if (!pendingEntries.length) return; + const toClear: string[] = []; + for (const [modelId, agentIds] of pendingEntries) { + const entry = entries.find((item) => item.model.id === modelId); + const requested = requests.get(modelId); + const effectiveStatus = + requested === "load" + ? "loading" + : requested === "download" + ? "downloading" + : entry?.status; + if ( + !entry || + effectiveStatus === "failed" || + effectiveStatus === "available" || + effectiveStatus === "unavailable" + ) { + toClear.push(modelId); + continue; + } + if ( + !offline && + !readOnly && + entry.status === "loaded" && + entry.model.pi_model && + agentsLoaded && + !triggeredAutoSwitchRef.current.has(modelId) + ) { + triggeredAutoSwitchRef.current.add(modelId); + toClear.push(modelId); + const targetAgents = agentIds + .map((id) => assignableAgents.find((agent) => agent.actorId === id)) + .filter( + (agent): agent is LocalModelAssignableAgent => + agent !== undefined && + !agent.busy && + !isAgentUsingModel(agent, computerId, entry.model.pi_model), + ); + if (targetAgents.length) { + assignMutation.mutate({ + agents: targetAgents, + modelId, + modelName: entry.model.name, + piModel: entry.model.pi_model, + }); + } + } + } + if (toClear.length) { + for (const id of toClear) triggeredAutoSwitchRef.current.delete(id); + setPendingSwitch((current) => { + const next = { ...current }; + for (const id of toClear) delete next[id]; + return next; + }); + } + }); + + useEffect(() => { + processPendingSwitch(); + }, [ + agentsLoaded, + entries, + offline, + pendingSwitch, + readOnly, + view.observed_at, + ]); + // A dialog stays open across snapshots, so its confirm checks the model // and Computer as they are now, the way the row's buttons do. const confirmable = ( entry: ModelEntry, - action: "load" | "remove" | "unload", + actionName: "load" | "remove" | "unload", ) => !offline && !readOnly && !requests.has(entry.model.id) && !downloadStopped(entry.model) && - actionStatuses[action].includes(entry.status); + actionStatuses[actionName].includes(entry.status); const dialogEntry = - dialog && dialog.kind !== "advanced" ? find(dialog.modelId) : undefined; + dialog && dialog.kind !== "advanced" && dialog.kind !== "createAgent" + ? find(dialog.modelId) + : undefined; const makeRoom = dialog?.kind === "makeRoom" && dialogEntry ? loadCheck(view, dialogEntry.model) @@ -370,6 +807,13 @@ function Library({ function act(input: LocalModelActionInput) { setDialog(null); + if ( + input.action === "cancel" || + input.action === "unload" || + input.action === "remove" + ) { + clearPendingSwitch(input.modelId); + } action.mutate(input); } @@ -407,7 +851,7 @@ function Library({ Reconnect it in{" "} Computers {" "} @@ -435,16 +879,79 @@ function Library({
) : null} + {assignError ? ( +
+ {assignError} +
+ ) : null} + {switchedNotice ? ( +
+
+ + + {switchedNotice.agentNames.length === 1 + ? `${switchedNotice.agentNames[0]} now uses ${switchedNotice.modelName}.` + : `${switchedNotice.agentNames.join(", ")} now use ${switchedNotice.modelName}.`} + +
+
+ {switchedNotice.includesAnotherMe && anotherMeRoomId ? ( + + ) : null} + +
+
+ ) : null}
{entries.map((entry) => { const modelId = entry.model.id; + const assigningThisModel = + assignMutation.isPending && + assignMutation.variables?.modelId === modelId; return ( { + if (!entry.model.pi_model || assignMutation.isPending) + return; + assignMutation.mutate({ + agents: [agent], + modelId, + modelName: entry.model.name, + piModel: entry.model.pi_model, + }); + }} onCancel={() => act({ action: "cancel", modelId })} + onCreateAgent={(piModel) => + setDialog({ kind: "createAgent", piModel }) + } onDownload={() => act({ action: "download", modelId })} onLoad={() => load(entry)} onRemove={() => setDialog({ kind: "remove", modelId })} @@ -453,8 +960,15 @@ function Library({ current === modelId ? null : modelId, ) } + onTogglePendingSwitch={(agentId) => + togglePendingSwitch(modelId, agentId) + } onUnload={() => unload(entry)} + pendingSwitchAgentIds={pendingSwitch[modelId]} + primaryAgent={primaryAgent} + readOnly={readOnly} requested={requests.get(modelId)} + tenantId={tenantId} view={view} /> ); @@ -466,19 +980,35 @@ function Library({ {dialog?.kind === "advanced" ? ( setDialog(null)} view={view} /> ) : null} + {dialog?.kind === "createAgent" ? ( + + setDialog(null)} + tenantId={tenantId} + /> + + ) : null} {dialogEntry && makeRoom ? ( setDialog(null)} - onConfirm={(unloadFirst) => + onConfirm={(unloadFirst, switchAgentIds) => { + if (switchAgentIds.length) { + queuePendingSwitch(dialogEntry.model.id, switchAgentIds); + } act({ action: "load", modelId: dialogEntry.model.id, unloadFirst, - }) - } + }); + }} + primaryAgent={primaryAgent} view={view} /> ) : null} diff --git a/apps/web/src/features/local-models/local-models-rules.test.ts b/apps/web/src/features/local-models/local-models-rules.test.ts index f3a9b2733..743abff62 100644 --- a/apps/web/src/features/local-models/local-models-rules.test.ts +++ b/apps/web/src/features/local-models/local-models-rules.test.ts @@ -1,11 +1,20 @@ -import { describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { + computerPickerLabel, downloadPercent, + downloadSourceLabel, downloadStatusDetail, downloadStopped, + downloadTransferSummary, formatGigabytes, + headerMemorySegments, + isModelOnComputer, loadCheck, + loadPercent, + loadStatusDetail, + loadedLibraryModelCount, + localModelsComputerStorageKey, makeRoomSelection, makesRoom, memorySummary, @@ -15,8 +24,12 @@ import { parametersLabel, placeName, poolLabel, + readRememberedLocalModelsComputerId, + rememberLocalModelsComputerId, rowBadges, + rowBadgesOverflowTitle, safeModelSourceUrl, + selectDefaultLocalModelsComputer, speedLabel, } from "./local-models-rules"; import { @@ -183,6 +196,7 @@ describe("local models rules", () => { }); expect(parametersLabel(moe)).toBe("30B · 3B active (MoE)"); expect(rowBadges(moe)).toEqual(["3B active", "Vision", "Tool use", "+2"]); + expect(rowBadgesOverflowTitle(moe)).toBe("Reasoning, Coding"); const dense = localModel({ architecture: "dense", @@ -192,8 +206,8 @@ describe("local models rules", () => { parameters: "4B", }); expect(parametersLabel(dense)).toBe("4B (Dense)"); - // Avoid repeating "4B" next to a model whose title already includes it. - expect(rowBadges(dense)).toEqual(["Tool use"]); + expect(rowBadges(dense)).toEqual(["4B", "Tool use"]); + expect(rowBadgesOverflowTitle(dense)).toBeUndefined(); expect( rowBadges( @@ -248,6 +262,226 @@ describe("local models rules", () => { expect(safe).toBe("https://huggingface.co/Qwen/Qwen3-4B/"); expect(modelSourceLabel(safe!)).toBe("huggingface.co/Qwen/Qwen3-4B"); }); + + it("formats download sources, transfer summaries, and cache reuse details", () => { + const mb = 1024 * 1024; + expect(downloadSourceLabel("modelscope")).toBe("ModelScope mirror"); + expect(downloadSourceLabel("huggingface")).toBe("Hugging Face"); + expect(downloadSourceLabel("cache")).toBe("Local cache"); + expect(downloadSourceLabel("custom")).toBe("Custom source"); + expect(downloadSourceLabel(undefined)).toBeUndefined(); + + expect( + downloadTransferSummary(localModel({ id: "m", name: "m" })), + ).toBeUndefined(); + expect( + downloadTransferSummary( + localModel({ + download: { done_bytes: 10, step: "download", total_bytes: 100 }, + id: "m", + name: "m", + state: "downloading", + }), + ), + ).toBeUndefined(); + expect( + downloadTransferSummary( + localModel({ + download: { + connections: 4, + done_bytes: 10, + source: "modelscope", + step: "download", + total_bytes: 100, + }, + id: "m", + name: "m", + state: "downloading", + }), + ), + ).toBe("ModelScope mirror · 4 connections · binary MB/s (MiB/s)"); + expect( + downloadTransferSummary( + localModel({ + download: { + connections: 1, + done_bytes: 10, + source: "huggingface", + step: "download", + total_bytes: 100, + }, + id: "m", + name: "m", + state: "downloading", + }), + ), + ).toBe("Hugging Face · 1 connection · binary MB/s (MiB/s)"); + expect( + downloadTransferSummary( + localModel({ + download: { + connections: 0, + done_bytes: 10, + source: "custom", + step: "download", + total_bytes: 100, + }, + id: "m", + name: "m", + state: "downloading", + }), + ), + ).toBe("Custom source · binary MB/s (MiB/s)"); + expect( + downloadTransferSummary( + localModel({ + download: { + connections: 4, + done_bytes: 10, + step: "download", + total_bytes: 100, + }, + id: "m", + name: "m", + state: "downloading", + }), + ), + ).toBe("4 connections · binary MB/s (MiB/s)"); + expect( + downloadTransferSummary( + localModel({ + download: { + connections: 0, + done_bytes: 100, + source: "cache", + step: "download", + total_bytes: 100, + }, + id: "m", + name: "m", + state: "downloading", + }), + ), + ).toBe("Reusing verified local cache"); + + expect( + downloadStatusDetail( + localModel({ + download: { + done_bytes: 0, + source: "cache", + step: "download", + total_bytes: 100 * mb, + }, + id: "m", + name: "m", + state: "downloading", + }), + ), + ).toBe("Reusing local cache…"); + expect( + downloadStatusDetail( + localModel({ + download: { + done_bytes: 100 * mb, + source: "cache", + step: "verify", + total_bytes: 100 * mb, + }, + id: "m", + name: "m", + state: "downloading", + }), + ), + ).toBe("checking file"); + }); + + it("computes load percentages and step text with placement fallback", () => { + const base = localModel({ id: "m", name: "m", state: "loading" }); + expect(loadPercent(base)).toBeNull(); + expect(loadStatusDetail(base, "GPU 0")).toBe("GPU 0"); + expect(loadStatusDetail(base)).toBeNull(); + + expect( + loadPercent({ + ...base, + load: { done_steps: 0, step: "prepare", total_steps: 0 }, + }), + ).toBeNull(); + expect( + loadPercent({ + ...base, + load: { done_steps: 4, step: "load", total_steps: 7 }, + }), + ).toBe(57); + expect( + loadPercent({ + ...base, + load: { done_steps: 12, step: "warm", total_steps: 10 }, + }), + ).toBe(100); + + expect( + loadStatusDetail( + { + ...base, + load: { done_steps: 0, step: "prepare", total_steps: 7 }, + }, + "GPU 0", + ), + ).toBe("Preparing server…"); + expect( + loadStatusDetail( + { + ...base, + load: { done_steps: 1, step: "start", total_steps: 7 }, + }, + "GPU 0", + ), + ).toBe("Starting server…"); + expect( + loadStatusDetail( + { + ...base, + load: { done_steps: 0, step: "load", total_steps: 7 }, + }, + "GPU 0", + ), + ).toBe("Loading weights to GPU 0"); + expect( + loadStatusDetail( + { + ...base, + load: { done_steps: 4, step: "load", total_steps: 7 }, + }, + "GPU 0", + ), + ).toBe("Loading weights to GPU 0 · 57%"); + expect( + loadStatusDetail({ + ...base, + load: { done_steps: 4, step: "load", total_steps: 7 }, + }), + ).toBe("Loading weights · 57%"); + expect( + loadStatusDetail({ + ...base, + load: { done_steps: 0, step: "load", total_steps: 7 }, + }), + ).toBe("Loading weights"); + expect( + loadStatusDetail({ + ...base, + load: { done_steps: 7, step: "warm", total_steps: 7 }, + }), + ).toBe("Warming up · 100%"); + expect( + loadStatusDetail({ + ...base, + load: { done_steps: 0, step: "warm", total_steps: 0 }, + }), + ).toBe("Warming up"); + }); }); describe("downloadStopped", () => { @@ -281,3 +515,206 @@ describe("downloadStopped", () => { ).toBe(false); }); }); + +describe("headerMemorySegments and model presence", () => { + it("reports whether a model holds usable files on the Computer", () => { + expect( + isModelOnComputer(localModel({ id: "m", name: "M", state: "loaded" })), + ).toBe(true); + expect( + isModelOnComputer(localModel({ id: "m", name: "M", state: "idle" })), + ).toBe(true); + expect( + isModelOnComputer( + localModel({ + error: { code: "ENGINE_START_FAILED" }, + id: "m", + name: "M", + state: "failed", + }), + ), + ).toBe(true); + expect( + isModelOnComputer( + localModel({ + error: { code: "DOWNLOAD_FAILED" }, + id: "m", + name: "M", + state: "failed", + }), + ), + ).toBe(false); + expect( + isModelOnComputer( + localModel({ + availability: { downloadable: true }, + id: "m", + name: "M", + state: "absent", + }), + ), + ).toBe(false); + expect( + isModelOnComputer( + localModel({ + availability: { downloadable: false }, + id: "m", + name: "M", + state: "absent", + }), + ), + ).toBe(false); + }); + + it("aggregates GPU pool segments including models, components, and other apps", () => { + const view = labView({ + pools: [ + { + ...gpuPool(0, 24, [{ gb: 12, id: "coder", name: "Coder" }]), + used_bytes: gb(16), + }, + gpuPool(1, 24, [ + { component: true, gb: 2, id: "embedding", name: "Embedding" }, + ]), + ], + }); + const segments = headerMemorySegments(view); + expect(segments).toEqual([ + { + bytes: gb(12), + id: "coder", + kind: "model", + modelIndex: 0, + name: "Coder", + percent: 25, + }, + { + bytes: gb(2), + id: "embedding", + kind: "component", + name: "Embedding", + percent: (gb(2) / gb(48)) * 100, + }, + { + bytes: gb(4), + id: "other-apps", + kind: "other", + name: "Other apps", + percent: (gb(4) / gb(48)) * 100, + }, + ]); + }); +}); + +describe("computer selection and storage persistence", () => { + afterEach(() => { + vi.unstubAllGlobals(); + }); + + function computer( + id: string, + displayName: string, + status: "online" | "offline", + piModelIds: string[] = [], + ) { + return { + computer_id: id, + display_name: displayName, + runtimes: [ + { + models: piModelIds.map((modelId) => ({ + id: modelId, + label: modelId, + })), + status: "ready" as const, + type: "pi" as const, + }, + ], + status, + }; + } + + it("counts localmodels-library Pi models and formats picker labels", () => { + const idleBox = computer("c1", "lab-118", "online", ["pi-default"]); + const loadedBox = computer("c2", "Workstation", "online", [ + "pi-default", + "localmodels-library/qwen3", + "localmodels-library/gemma3", + ]); + const offlineBox = computer("c3", "Mac Studio", "offline", [ + "localmodels-library/gpt-oss", + ]); + + expect(loadedLibraryModelCount(idleBox)).toBe(0); + expect(loadedLibraryModelCount(loadedBox)).toBe(2); + expect(computerPickerLabel(idleBox)).toBe("lab-118"); + expect(computerPickerLabel(loadedBox)).toBe("Workstation · 2 loaded"); + expect(computerPickerLabel(offlineBox)).toBe("Mac Studio · Offline"); + }); + + it("follows desktop, remembered, online with library model, online, then first", () => { + const offline = computer("c-off", "Offline", "offline", [ + "localmodels-library/m1", + ]); + const onlineEmpty = computer("c-empty", "Empty", "online"); + const onlineLoaded = computer("c-loaded", "Loaded", "online", [ + "localmodels-library/m2", + ]); + const list = [offline, onlineEmpty, onlineLoaded]; + + expect( + selectDefaultLocalModelsComputer(list, { + desktopComputerId: "c-empty", + rememberedComputerId: "c-off", + })?.computer_id, + ).toBe("c-empty"); + + expect( + selectDefaultLocalModelsComputer(list, { + rememberedComputerId: "c-off", + })?.computer_id, + ).toBe("c-off"); + + expect( + selectDefaultLocalModelsComputer(list, { + rememberedComputerId: "c-missing", + })?.computer_id, + ).toBe("c-loaded"); + + expect( + selectDefaultLocalModelsComputer([offline, onlineEmpty])?.computer_id, + ).toBe("c-empty"); + + expect(selectDefaultLocalModelsComputer([offline])?.computer_id).toBe( + "c-off", + ); + }); + + it("reads and writes per-tenant computer preference safely", () => { + const store = new Map(); + vi.stubGlobal("localStorage", { + getItem: (key: string) => store.get(key) ?? null, + setItem: (key: string, value: string) => { + store.set(key, value); + }, + }); + rememberLocalModelsComputerId("tenant-1", "computer-mac"); + expect(store.get(localModelsComputerStorageKey("tenant-1"))).toBe( + "computer-mac", + ); + expect(readRememberedLocalModelsComputerId("tenant-1")).toBe( + "computer-mac", + ); + + vi.stubGlobal("localStorage", { + getItem: () => { + throw new Error("blocked"); + }, + setItem: () => { + throw new Error("blocked"); + }, + }); + expect(() => rememberLocalModelsComputerId("tenant-1", "c")).not.toThrow(); + expect(readRememberedLocalModelsComputerId("tenant-1")).toBeUndefined(); + }); +}); diff --git a/apps/web/src/features/local-models/local-models-rules.ts b/apps/web/src/features/local-models/local-models-rules.ts index e1cf23633..0f4212445 100644 --- a/apps/web/src/features/local-models/local-models-rules.ts +++ b/apps/web/src/features/local-models/local-models-rules.ts @@ -1,3 +1,4 @@ +import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; import { formatBytes } from "@/lib/format-bytes"; import { architectureCopy, capabilityCopy } from "./local-models-copy"; @@ -8,6 +9,9 @@ import type { LocalModelView, } from "./local-models-queries"; +type LocalComputer = TeamHubLocalComputerPage["items"][number]; +const piLibraryModelPrefix = "localmodels-library/"; + /** * Presentation rules of the Local models page. The Computer decides fit, * availability, placement, the recommendation and every memory figure; these @@ -118,10 +122,15 @@ export function poolFreeBytes(pool: LocalModelPool) { return Math.max(0, pool.total_bytes - pool.used_bytes); } +function summaryPools(view: LocalModelsView) { + const gpus = view.pools.filter((pool) => pool.kind === "gpu"); + return gpus.length ? gpus : view.pools; +} + /** What the header adds up: GPU memory when the Computer has GPUs, else its memory. */ export function memorySummary(view: LocalModelsView) { const gpus = view.pools.filter((pool) => pool.kind === "gpu"); - const pools = gpus.length ? gpus : view.pools; + const pools = summaryPools(view); return { gpu: gpus.length > 0, totalBytes: pools.reduce((sum, pool) => sum + pool.total_bytes, 0), @@ -129,6 +138,186 @@ export function memorySummary(view: LocalModelsView) { }; } +export interface HeaderMemorySegment { + id: string; + kind: "model" | "component" | "other"; + name: string; + bytes: number; + percent: number; + modelIndex?: number; +} + +/** + * Aggregated memory segments across the header's summary pools so users can + * distinguish loaded models, foundation components, and other-app memory + * without opening Details. + */ +export function headerMemorySegments( + view: LocalModelsView, +): HeaderMemorySegment[] { + const pools = summaryPools(view); + const totalBytes = pools.reduce((sum, pool) => sum + pool.total_bytes, 0); + if (totalBytes <= 0) return []; + + const modelHolders = new Map(); + const componentHolders = new Map(); + let otherBytes = 0; + + for (const pool of pools) { + let accounted = 0; + for (const holder of pool.holders) { + accounted += holder.bytes; + const target = holder.kind === "model" ? modelHolders : componentHolders; + const existing = target.get(holder.id); + if (existing) existing.bytes += holder.bytes; + else target.set(holder.id, { name: holder.name, bytes: holder.bytes }); + } + otherBytes += Math.max(0, pool.used_bytes - accounted); + } + + const rawSegments: Array> = []; + let modelIndex = 0; + for (const [id, holder] of modelHolders) { + if (holder.bytes > 0) { + rawSegments.push({ + bytes: holder.bytes, + id, + kind: "model", + modelIndex, + name: holder.name, + }); + modelIndex += 1; + } + } + for (const [id, holder] of componentHolders) { + if (holder.bytes > 0) { + rawSegments.push({ + bytes: holder.bytes, + id, + kind: "component", + name: holder.name, + }); + } + } + if (otherBytes > 0) { + rawSegments.push({ + bytes: otherBytes, + id: "other-apps", + kind: "other", + name: "Other apps", + }); + } + + const accountedTotal = rawSegments.reduce((sum, seg) => sum + seg.bytes, 0); + const scaleBase = Math.max(totalBytes, accountedTotal); + return rawSegments.map((segment) => ({ + ...segment, + percent: (segment.bytes / scaleBase) * 100, + })); +} + +/** Whether a model holds usable files on the Computer (not absent or a partial download). */ +export function isModelOnComputer(model: LocalModel): boolean { + const status = modelStatus(model); + return ( + status !== "available" && + status !== "unavailable" && + !downloadStopped(model) + ); +} + +export function localModelsComputerStorageKey(tenantId: string): string { + return `linger.local-models.computer:${tenantId}`; +} + +export function readRememberedLocalModelsComputerId( + tenantId: string, +): string | undefined { + try { + const value = globalThis.localStorage?.getItem( + localModelsComputerStorageKey(tenantId), + ); + return value || undefined; + } catch { + return undefined; + } +} + +export function rememberLocalModelsComputerId( + tenantId: string, + computerId: string, +): void { + try { + globalThis.localStorage?.setItem( + localModelsComputerStorageKey(tenantId), + computerId, + ); + } catch { + // Ignore storage quota or private-mode access restrictions. + } +} + +/** Counts library models exposed through the Computer's Pi runtime. */ +export function loadedLibraryModelCount( + computer: Pick, +): number { + const piRuntime = computer.runtimes.find((runtime) => runtime.type === "pi"); + if (!piRuntime) return 0; + return piRuntime.models.filter((model) => + model.id.startsWith(piLibraryModelPrefix), + ).length; +} + +export function computerPickerLabel( + computer: Pick, +): string { + if (computer.status !== "online") { + return `${computer.display_name} · Offline`; + } + const loadedCount = loadedLibraryModelCount(computer); + return loadedCount > 0 + ? `${computer.display_name} · ${loadedCount} loaded` + : computer.display_name; +} + +/** + * Picks the initial owned Computer when `?computer=` is omitted: the desktop's + * own Computer, then the last remembered Computer, then an online Computer + * already running a library model, then any online Computer, else the first. + */ +export function selectDefaultLocalModelsComputer< + T extends Pick, +>( + computers: readonly T[], + { + desktopComputerId, + rememberedComputerId, + }: { + desktopComputerId?: string; + rememberedComputerId?: string; + } = {}, +): T | undefined { + if (desktopComputerId) { + const desktopComputer = computers.find( + (item) => item.computer_id === desktopComputerId, + ); + if (desktopComputer) return desktopComputer; + } + if (rememberedComputerId) { + const remembered = computers.find( + (item) => item.computer_id === rememberedComputerId, + ); + if (remembered) return remembered; + } + return ( + computers.find( + (item) => item.status === "online" && loadedLibraryModelCount(item) > 0, + ) ?? + computers.find((item) => item.status === "online") ?? + computers[0] + ); +} + export interface LoadCheck { pool: LocalModelPool; needBytes: number; @@ -209,6 +398,83 @@ export function downloadPercent(model: LocalModel) { ); } +export type LocalModelDownloadSource = NonNullable< + NonNullable["source"] +>; + +const downloadSourceLabels: Record = { + cache: "Local cache", + custom: "Custom source", + huggingface: "Hugging Face", + modelscope: "ModelScope mirror", +}; + +/** Human-readable name for the mirror or cache serving a model download. */ +export function downloadSourceLabel( + source?: LocalModelDownloadSource, +): string | undefined { + return source ? downloadSourceLabels[source] : undefined; +} + +/** Source, parallelism, and binary throughput unit details for a live download. */ +export function downloadTransferSummary(model: LocalModel): string | undefined { + const download = model.download; + if (!download || (!download.source && download.connections === undefined)) { + return undefined; + } + if (download.source === "cache") { + return "Reusing verified local cache"; + } + const connections = download.connections; + const connectionLabel = + connections !== undefined && connections > 0 + ? `${connections} ${connections === 1 ? "connection" : "connections"}` + : undefined; + return [ + downloadSourceLabel(download.source), + connectionLabel, + "binary MB/s (MiB/s)", + ] + .filter((part): part is string => Boolean(part)) + .join(" · "); +} + +/** Whole-percent load progress across reported setup and weight-loading steps. */ +export function loadPercent(model: LocalModel): number | null { + const load = model.load; + if (!load || load.total_steps <= 0) return null; + return Math.max( + 0, + Math.min(100, Math.round((load.done_steps / load.total_steps) * 100)), + ); +} + +/** Live loading step text, falling back to pool placement when step progress is absent. */ +export function loadStatusDetail( + model: LocalModel, + where?: string, +): string | null { + const load = model.load; + if (!load) return where ?? null; + const pct = loadPercent(model); + switch (load.step) { + case "prepare": + return "Preparing server…"; + case "start": + return "Starting server…"; + case "load": { + const base = where ? `Loading weights to ${where}` : "Loading weights"; + return pct !== null && pct > 0 ? `${base} · ${pct}%` : base; + } + case "warm": + return pct !== null ? `Warming up · ${pct}%` : "Warming up"; + default: { + const _exhaustive: never = load.step; + return _exhaustive; + } + } +} + /** Context length in thousands of tokens, as model cards write it: 128K. */ export function contextLabel(tokens: number) { return `${Math.round(tokens / 1024)}K tokens`; @@ -253,26 +519,34 @@ export function parametersLabel(model: LocalModel) { return scale || arch; } -/** Concise badges for the collapsed row: parameter scale (when distinct) and capabilities. */ -export function rowBadges(model: LocalModel): string[] { +function allRowBadges(model: LocalModel): string[] { const badges: string[] = []; if (model.active_parameters) { badges.push(`${model.active_parameters} active`); - } else if ( - model.parameters && - !model.name.toLowerCase().includes(model.parameters.toLowerCase()) - ) { + } else if (model.parameters) { badges.push(model.parameters); } for (const capability of modelCapabilities(model)) { badges.push(capabilityCopy(capability)); } + return badges; +} + +/** Concise badges for the collapsed row: structured scale and verified capabilities. */ +export function rowBadges(model: LocalModel): string[] { + const badges = allRowBadges(model); if (badges.length > 3) { return [...badges.slice(0, 3), `+${badges.length - 3}`]; } return badges; } +/** Full names of collapsed overflow badges for the `+N` pill tooltip. */ +export function rowBadgesOverflowTitle(model: LocalModel): string | undefined { + const badges = allRowBadges(model); + return badges.length > 3 ? badges.slice(3).join(", ") : undefined; +} + function formatRemainingTime(seconds: number) { if (seconds < 60) return `${seconds}s left`; if (seconds < 3600) return `${Math.ceil(seconds / 60)}m left`; @@ -282,10 +556,13 @@ function formatRemainingTime(seconds: number) { return minutes > 0 ? `${hours}h ${minutes}m left` : `${hours}h left`; } -/** Live download progress summary: "checking file", or "25% · 50 MB/s · 5m left". */ -export function downloadStatusDetail(model: LocalModel) { +/** Live download progress summary: "checking file", "Reusing local cache…", or "25% · 50 MB/s · 5m left". */ +export function downloadStatusDetail(model: LocalModel): string | null { const download = model.download; if (download?.step === "verify") return "checking file"; + if (download?.step === "download" && download.source === "cache") { + return "Reusing local cache…"; + } const parts = [`${downloadPercent(model)}%`]; if (download?.bytes_per_second && download.bytes_per_second > 0) { parts.push(`${formatBytes(download.bytes_per_second)}/s`); diff --git a/apps/web/src/features/rooms/chat/direct-agent-model-switch.test.tsx b/apps/web/src/features/rooms/chat/direct-agent-model-switch.test.tsx index c979f3e1e..7165b25fa 100644 --- a/apps/web/src/features/rooms/chat/direct-agent-model-switch.test.tsx +++ b/apps/web/src/features/rooms/chat/direct-agent-model-switch.test.tsx @@ -6,11 +6,19 @@ import { waitFor, within, } from "@testing-library/react"; +import { MemoryRouter } from "react-router"; import { describe, expect, it, vi } from "vitest"; +import { localComputersQueryKey } from "@/features/local-computer/local-computer-queries"; +import { localModelsQueryKey } from "@/features/local-models/local-models-queries"; +import { labView } from "@/features/local-models/local-models-test-support"; import { createTestTeamHubRuntime } from "@/test/teamhub-test-runtime"; -import type { TestRouteHandler } from "@/test/teamhub-test-server"; +import { + teamHubErrorResponse, + type TestRouteHandler, +} from "@/test/teamhub-test-server"; import type { + TeamHubLocalComputerPage, TeamHubTenantAgent, TeamHubTenantAgentUpdateInput, } from "@/features/teamhub/teamhub-types"; @@ -103,18 +111,500 @@ describe("Direct Agent runtime settings", () => { fireEvent.click(screen.getByRole("button", { name: "Cancel" })); expect(updateTenantAgent).not.toHaveBeenCalled(); }); + + it("resolves a human-readable model name from Local models when Pi does not list it yet", async () => { + const localModelsHandler = vi.fn(() => labView()); + renderSwitch({ + computersHandler: () => ({ + items: [ + { + computer_id: "computer-1", + display_name: "Computer", + status: "online", + upgrade_required: false, + active_operations: 0, + runtimes: [{ type: "pi", status: "ready", models: [] }], + workspace_grants: [ + { + grant_id: "grant-1", + label: "Workspace", + mode: "workspace-write", + }, + ], + }, + ], + }), + localModelsHandler, + registration: piLocalModelAgent(), + }); + expect( + await screen.findByRole("button", { name: "Qwen3 Coder 30B Medium" }), + ).toBeVisible(); + expect(localModelsHandler).toHaveBeenCalledTimes(1); + }); + + it("prefers Pi's runtime model label when present in cache and skips Local models", async () => { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }); + const computers: TeamHubLocalComputerPage = { + items: [ + { + computer_id: "computer-1", + display_name: "Computer", + status: "online", + upgrade_required: false, + active_operations: 0, + runtimes: [ + { + type: "pi", + status: "ready", + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B (Pi)", + }, + ], + }, + ], + workspace_grants: [ + { + grant_id: "grant-1", + label: "Workspace", + mode: "workspace-write", + }, + ], + }, + ], + }; + queryClient.setQueryData( + localComputersQueryKey("tenant-northstar"), + computers, + ); + const localModelsHandler = vi.fn(() => labView()); + renderSwitch({ + computersHandler: () => computers, + localModelsHandler, + queryClient, + registration: piLocalModelAgent(), + }); + expect( + await screen.findByRole("button", { + name: "Qwen3 Coder 30B (Pi) Medium", + }), + ).toBeVisible(); + expect(localModelsHandler).not.toHaveBeenCalled(); + }); + + it("reads cached Local models immediately while Local Computers are still loading", async () => { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }); + queryClient.setQueryData( + localModelsQueryKey("tenant-northstar", "computer-1"), + labView(), + ); + renderSwitch({ + computersHandler: () => new Promise(() => {}), + localModelsHandler: () => new Promise(() => {}), + queryClient, + registration: piLocalModelAgent(), + }); + expect( + await screen.findByRole("button", { name: "Qwen3 Coder 30B Medium" }), + ).toBeVisible(); + }); + + it("does not request Local models for non-localmodels-library models", async () => { + const localModelsHandler = vi.fn(() => labView()); + renderSwitch({ localModelsHandler }); + expect( + await screen.findByRole("button", { name: "5.4 Sol Medium" }), + ).toBeVisible(); + expect(localModelsHandler).not.toHaveBeenCalled(); + }); + + it("opens the full dialog directly when only the active local model exists", async () => { + renderSwitch({ + computersHandler: () => ({ + items: [ + { + computer_id: "computer-1", + display_name: "Computer", + status: "online", + upgrade_required: false, + active_operations: 0, + runtimes: [ + { + type: "pi", + status: "ready", + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + }, + ], + workspace_grants: [ + { + grant_id: "grant-1", + label: "Workspace", + mode: "workspace-write", + }, + ], + }, + ], + }), + localModelsHandler: () => labView(), + registration: piLocalModelAgent(), + }); + const trigger = await screen.findByRole("button", { + name: "Qwen3 Coder 30B Medium", + }); + await waitFor(() => + expect(trigger).toHaveAttribute("aria-haspopup", "dialog"), + ); + fireEvent.click(trigger); + expect( + await screen.findByRole("dialog", { name: "Runtime settings" }), + ).toBeVisible(); + expect(screen.queryByRole("menu")).toBeNull(); + }); + + it("quick-switches to an alternative loaded local model in one click without opening the full dialog", async () => { + const updateTenantAgent = vi.fn(({ body }: { body: unknown }) => ({ + ...piLocalModelAgent(), + runtime: (body as TeamHubTenantAgentUpdateInput).runtime!, + configuration_revision: 5, + })); + renderSwitch({ + computersHandler: () => ({ + items: [ + { + computer_id: "computer-1", + display_name: "Lab Box", + status: "online", + upgrade_required: false, + active_operations: 0, + runtimes: [ + { + type: "pi", + status: "ready", + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + { + id: "localmodels-library/qwen3.6-35b-a3b", + label: "Qwen3.6 35B-A3B", + }, + ], + }, + ], + workspace_grants: [ + { + grant_id: "grant-1", + label: "Workspace", + mode: "workspace-write", + }, + ], + }, + ], + }), + localModelsHandler: () => labView(), + registration: piLocalModelAgent(), + updateTenantAgent, + }); + + const trigger = await screen.findByRole("button", { + name: "Qwen3 Coder 30B Medium", + }); + await waitFor(() => + expect(trigger).toHaveAttribute("aria-haspopup", "menu"), + ); + fireEvent.click(trigger); + + const menu = await screen.findByRole("menu", { + name: "Switch local model", + }); + expect( + within(menu).getByRole("menuitem", { name: "Qwen3 Coder 30B" }), + ).toBeDisabled(); + const targetOption = within(menu).getByRole("menuitem", { + name: "Qwen3.6 35B-A3B", + }); + expect(targetOption).toBeEnabled(); + fireEvent.click(targetOption); + + await waitFor(() => + expect(updateTenantAgent).toHaveBeenCalledWith( + expect.objectContaining({ + params: { tenantId: "tenant-northstar", agentId: "actor-sol" }, + body: { + expected_configuration_revision: 4, + runtime: { + model: "localmodels-library/qwen3.6-35b-a3b", + reasoning_effort: "medium", + placement: { + type: "local", + computer_id: "computer-1", + runtime_type: "pi", + workspace_grant_id: "grant-1", + }, + }, + }, + }), + ), + ); + expect(screen.queryByRole("dialog")).toBeNull(); + expect(screen.queryByRole("menu")).toBeNull(); + }); + + it("shows computer names across multiple machines, handles Runtime settings, and exposes Manage local models inside a router", async () => { + renderSwitch({ + computersHandler: () => ({ + items: [ + { + computer_id: "computer-1", + display_name: "Studio Mac", + status: "online", + upgrade_required: false, + active_operations: 0, + runtimes: [ + { type: "codex", status: "ready", models: runtimeModels }, + { + type: "pi", + status: "ready", + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + }, + ], + workspace_grants: [ + { + grant_id: "grant-1", + label: "Workspace", + mode: "workspace-write", + }, + ], + }, + { + computer_id: "computer-2", + display_name: "cdd-use", + status: "online", + upgrade_required: false, + active_operations: 0, + runtimes: [ + { + type: "pi", + status: "ready", + models: [ + { + id: "localmodels-library/qwen3.6-35b-a3b", + label: "Qwen3.6 35B-A3B", + }, + ], + }, + ], + workspace_grants: [ + { + grant_id: "grant-2", + label: "Workspace", + mode: "workspace-write", + }, + ], + }, + ], + }), + withRouter: true, + }); + + const trigger = await screen.findByRole("button", { + name: "5.4 Sol Medium", + }); + await waitFor(() => + expect(trigger).toHaveAttribute("aria-haspopup", "menu"), + ); + fireEvent.click(trigger); + + const menu = await screen.findByRole("menu", { + name: "Switch local model", + }); + expect( + within(menu).getByRole("menuitem", { + name: /Qwen3 Coder 30B.*Studio Mac/, + }), + ).toBeVisible(); + expect( + within(menu).getByRole("menuitem", { name: /Qwen3\.6 35B-A3B.*cdd-use/ }), + ).toBeVisible(); + expect( + within(menu).getByRole("menuitem", { name: "Manage local models…" }), + ).toHaveAttribute( + "href", + "/t/tenant-northstar/settings/local-models?computer=computer-1", + ); + + fireEvent.click( + within(menu).getByRole("menuitem", { name: "Runtime settings…" }), + ); + expect( + await screen.findByRole("dialog", { name: "Runtime settings" }), + ).toBeVisible(); + expect(screen.queryByRole("menu")).toBeNull(); + }); + + it("disables quick-switch options when busy while keeping Runtime settings accessible", async () => { + const updateTenantAgent = vi.fn(); + renderSwitch({ + busy: true, + computersHandler: () => ({ + items: [ + { + computer_id: "computer-1", + display_name: "Computer", + status: "online", + upgrade_required: false, + active_operations: 0, + runtimes: [ + { type: "codex", status: "ready", models: runtimeModels }, + { + type: "pi", + status: "ready", + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + }, + ], + workspace_grants: [ + { + grant_id: "grant-1", + label: "Workspace", + mode: "workspace-write", + }, + ], + }, + ], + }), + updateTenantAgent, + }); + + const trigger = await screen.findByRole("button", { + name: "5.4 Sol Medium", + }); + await waitFor(() => + expect(trigger).toHaveAttribute("aria-haspopup", "menu"), + ); + fireEvent.click(trigger); + + const menu = await screen.findByRole("menu", { + name: "Switch local model", + }); + expect(within(menu).getByText("Wait for reply to finish")).toBeVisible(); + const modelOption = within(menu).getByRole("menuitem", { + name: "Qwen3 Coder 30B", + }); + expect(modelOption).toBeDisabled(); + fireEvent.click(modelOption); + expect(updateTenantAgent).not.toHaveBeenCalled(); + + const runtimeSettingsItem = within(menu).getByRole("menuitem", { + name: "Runtime settings…", + }); + expect(runtimeSettingsItem).toBeEnabled(); + fireEvent.click(runtimeSettingsItem); + expect( + await screen.findByRole("dialog", { name: "Runtime settings" }), + ).toBeVisible(); + expect(screen.getByRole("button", { name: "Save Runtime" })).toBeDisabled(); + }); + + it("surfaces PATCH failures after quick-switching and clears on Escape", async () => { + renderSwitch({ + computersHandler: () => ({ + items: [ + { + computer_id: "computer-1", + display_name: "Computer", + status: "online", + upgrade_required: false, + active_operations: 0, + runtimes: [ + { type: "codex", status: "ready", models: runtimeModels }, + { + type: "pi", + status: "ready", + models: [ + { + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + }, + ], + workspace_grants: [ + { + grant_id: "grant-1", + label: "Workspace", + mode: "workspace-write", + }, + ], + }, + ], + }), + updateTenantAgent: () => + teamHubErrorResponse(409, "AGENT_BUSY", "Agent is busy right now."), + }); + + const trigger = await screen.findByRole("button", { + name: "5.4 Sol Medium", + }); + await waitFor(() => + expect(trigger).toHaveAttribute("aria-haspopup", "menu"), + ); + fireEvent.click(trigger); + + fireEvent.keyDown(document, { key: "Escape" }); + expect(screen.queryByRole("menu")).toBeNull(); + + fireEvent.click(trigger); + fireEvent.click( + await screen.findByRole("menuitem", { name: "Qwen3 Coder 30B" }), + ); + expect(await screen.findByRole("alert")).toHaveTextContent( + "Agent is busy right now.", + ); + }); }); function renderSwitch({ busy = false, + computersHandler, currentActorId = "actor-jenny", + localModelsHandler, + queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }), registration = localAgent(), updateTenantAgent = vi.fn(), + withRouter = false, }: { busy?: boolean; + computersHandler?: TestRouteHandler; currentActorId?: string; + localModelsHandler?: TestRouteHandler; + queryClient?: QueryClient; registration?: TeamHubTenantAgent; updateTenantAgent?: TestRouteHandler; + withRouter?: boolean; }) { const runtime = createTestTeamHubRuntime(); runtime.server.on( @@ -122,31 +612,39 @@ function renderSwitch({ "/v0/tenants/{tenantId}/agents/{agentId}", updateTenantAgent, ); - runtime.server.on("GET", "/v0/tenants/{tenantId}/local-computers", () => ({ - items: [ - { - computer_id: "computer-1", - display_name: "Computer", - status: "online", - runtimes: [{ type: "codex", status: "ready", models: runtimeModels }], - workspace_grants: [ + runtime.server.on( + "GET", + "/v0/tenants/{tenantId}/local-computers", + computersHandler ?? + (() => ({ + items: [ { - grant_id: "grant-1", - label: "Workspace", - mode: "workspace-write", + computer_id: "computer-1", + display_name: "Computer", + status: "online", + runtimes: [ + { type: "codex", status: "ready", models: runtimeModels }, + ], + workspace_grants: [ + { + grant_id: "grant-1", + label: "Workspace", + mode: "workspace-write", + }, + ], }, ], - }, - ], - })); - render( - + })), + ); + if (localModelsHandler) { + runtime.server.on( + "GET", + "/v0/tenants/{tenantId}/local-computers/{computerId}/local-models", + localModelsHandler, + ); + } + const content = ( + - , + ); + render(withRouter ? {content} : content); +} + +function piLocalModelAgent(): TeamHubTenantAgent { + const base = localAgent(); + return { + ...base, + runtime: { + model: "localmodels-library/qwen3-coder-30b", + placement: { + type: "local", + computer_id: "computer-1", + workspace_grant_id: "grant-1", + runtime_type: "pi", + }, + reasoning_effort: "medium", + }, + }; } function localAgent(): TeamHubTenantAgent { diff --git a/apps/web/src/features/rooms/chat/direct-agent-model-switch.tsx b/apps/web/src/features/rooms/chat/direct-agent-model-switch.tsx index 9b4c28f40..430916914 100644 --- a/apps/web/src/features/rooms/chat/direct-agent-model-switch.tsx +++ b/apps/web/src/features/rooms/chat/direct-agent-model-switch.tsx @@ -1,12 +1,36 @@ +import { + AiChipIcon, + Settings01Icon, + Tick02Icon, +} from "@hugeicons/core-free-icons"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; -import { lazy, Suspense, useState } from "react"; +import { + type KeyboardEvent as ReactKeyboardEvent, + lazy, + type RefObject, + Suspense, + useEffect, + useEffectEvent, + useLayoutEffect, + useRef, + useState, +} from "react"; +import { createPortal } from "react-dom"; +import { Link, useInRouterContext } from "react-router"; + +import { Icon } from "@/components/ui/icon"; +import { localComputersQueryOptions } from "@/features/local-computer/local-computer-queries"; import { teamHubData } from "@/features/teamhub/teamhub-api"; import { type TeamHubClient } from "@/features/teamhub/teamhub-client"; import type { - TeamHubAgentReasoningEffort, + TeamHubLocalComputerPage, TeamHubTenantAgent, } from "@/features/teamhub/teamhub-types"; import type { AgentRuntimeUpdateInput } from "../members/edit-agent-runtime-dialog"; +import { + reasoningEffortLabel, + useResolvedAgentModelLabel, +} from "../members/use-agent-model-selection"; const EditAgentRuntimeDialog = lazy(async () => ({ default: (await import("../members/edit-agent-runtime-dialog")) .EditAgentRuntimeDialog, @@ -14,15 +38,22 @@ const EditAgentRuntimeDialog = lazy(async () => ({ import { tenantAgentsQueryKey } from "../room-queries"; import "./direct-agent-model-switch.css"; -const EFFORT_LABEL: Record = { - none: "None", - low: "Low", - medium: "Medium", - high: "High", - xhigh: "Extra high", - max: "Maximum", - ultra: "Ultra", -}; +type LocalComputer = TeamHubLocalComputerPage["items"][number]; + +const LOCAL_MODELS_LIBRARY_PREFIX = "localmodels-library/"; +const MENU_GAP = 6; +const MENU_MARGIN = 8; +const DEFAULT_MENU_WIDTH = 248; +const MENU_ITEM_CLASS = + "flex min-h-[33px] w-full items-center gap-2 rounded-sm px-2 py-1.5 text-left text-sm text-foreground outline-none hover:bg-accent focus:bg-accent disabled:pointer-events-none disabled:opacity-50"; + +interface QuickLocalOption { + computerId: string; + computerName: string; + grantId: string; + modelId: string; + label: string; +} interface Props { client: TeamHubClient; @@ -39,21 +70,109 @@ export function DirectAgentModelSwitch(props: Props) { return ; } +function buildQuickLocalOptions( + computers: readonly LocalComputer[] | undefined, + registration: TeamHubTenantAgent, +): QuickLocalOption[] { + if (!computers?.length) return []; + const placement = registration.runtime.placement; + const options: QuickLocalOption[] = []; + const seen = new Set(); + for (const computer of computers) { + if (computer.status !== "online" || computer.upgrade_required) continue; + const retainedGrant = + placement?.type === "local" && + placement.computer_id === computer.computer_id && + placement.workspace_grant_id + ? computer.workspace_grants.find( + (grant) => + grant.grant_id === placement.workspace_grant_id && + grant.mode === "workspace-write", + ) + : undefined; + const grant = + retainedGrant ?? + computer.workspace_grants.find((item) => item.mode === "workspace-write"); + if (!grant) continue; + const piRuntime = computer.runtimes.find( + (runtime) => runtime.type === "pi" && runtime.status === "ready", + ); + if (!piRuntime) continue; + for (const model of piRuntime.models) { + if (!model.id.startsWith(LOCAL_MODELS_LIBRARY_PREFIX)) continue; + const key = `${computer.computer_id}\0${model.id}`; + if (seen.has(key)) continue; + seen.add(key); + options.push({ + computerId: computer.computer_id, + computerName: computer.display_name, + grantId: grant.grant_id, + modelId: model.id, + label: model.label || model.id, + }); + } + } + return options; +} + +function isSameTarget( + option: QuickLocalOption, + registration: TeamHubTenantAgent, +): boolean { + const placement = registration.runtime.placement; + return ( + placement?.type === "local" && + placement.runtime_type === "pi" && + placement.computer_id === option.computerId && + registration.runtime.model === option.modelId + ); +} + +function computePopoverPosition( + triggerRect: DOMRect, + menuRect: DOMRect | undefined, +): { left: number; top: number; side: "top" | "bottom" } { + const width = menuRect?.width || DEFAULT_MENU_WIDTH; + const height = menuRect?.height || 0; + const vw = window.innerWidth; + const vh = window.innerHeight; + const left = Math.max( + MENU_MARGIN, + Math.min(triggerRect.left, vw - width - MENU_MARGIN), + ); + const above = triggerRect.top - MENU_GAP - height; + const openBelow = height > 0 && above < MENU_MARGIN; + const rawTop = openBelow ? triggerRect.bottom + MENU_GAP : above; + const top = Math.max( + MENU_MARGIN, + Math.min(rawTop, vh - height - MENU_MARGIN), + ); + return { left, top, side: openBelow ? "bottom" : "top" }; +} + function RuntimeSettingsEntry({ client, registration, tenantId, busy }: Props) { const queryClient = useQueryClient(); - const [open, setOpen] = useState(false); - const computers = useQuery({ - queryFn: () => client.listTenantLocalComputers(tenantId), - queryKey: ["teamhub", "local-computers", tenantId], - retry: false, - }); - const placement = registration.runtime.placement; - const models = computers.data?.items - .find((item) => item.computer_id === placement?.computer_id) - ?.runtimes.find((item) => item.type === placement?.runtime_type)?.models; - const model = models?.find((item) => item.id === registration.runtime.model); + const [dialogOpen, setDialogOpen] = useState(false); + const [menuOpen, setMenuOpen] = useState(false); + const triggerRef = useRef(null); + + const computersQuery = useQuery(localComputersQueryOptions(client, tenantId)); + const quickLocalOptions = buildQuickLocalOptions( + computersQuery.data?.items, + registration, + ); + const hasAlternatives = quickLocalOptions.some( + (opt) => !isSameTarget(opt, registration), + ); + const showMenu = menuOpen && hasAlternatives; + const effort = registration.runtime.reasoning_effort; - const label = model?.label ?? registration.runtime.model; + const label = useResolvedAgentModelLabel({ + client, + computers: computersQuery.data?.items, + registration, + tenantId, + }); const update = useMutation({ mutationFn: (input: AgentRuntimeUpdateInput) => teamHubData( @@ -84,20 +203,54 @@ function RuntimeSettingsEntry({ client, registration, tenantId, busy }: Props) { void queryClient.invalidateQueries({ queryKey: tenantAgentsQueryKey(tenantId), }); - setOpen(false); + setDialogOpen(false); + setMenuOpen(false); }, }); + + function openRuntimeDialog() { + setMenuOpen(false); + update.reset(); + setDialogOpen(true); + } + + function handleTriggerClick() { + if (!hasAlternatives) { + openRuntimeDialog(); + return; + } + setMenuOpen((current) => !current); + } + + function handleQuickSelect(opt: QuickLocalOption) { + if (busy || update.isPending || isSameTarget(opt, registration)) return; + setMenuOpen(false); + update.reset(); + update.mutate({ + runtime: { + model: opt.modelId, + placement: { + computer_id: opt.computerId, + runtime_type: "pi", + type: "local", + workspace_grant_id: opt.grantId, + }, + ...(registration.runtime.reasoning_effort + ? { reasoning_effort: registration.runtime.reasoning_effort } + : {}), + }, + }); + } + return (
- {open ? ( + {!dialogOpen && update.error ? ( + + {update.error.message} + + ) : null} + {showMenu ? ( + setMenuOpen(false)} + onOpenRuntimeSettings={openRuntimeDialog} + onSelect={handleQuickSelect} + options={quickLocalOptions} + registration={registration} + tenantId={tenantId} + triggerRef={triggerRef} + /> + ) : null} + {dialogOpen ? ( @@ -126,7 +301,7 @@ function RuntimeSettingsEntry({ client, registration, tenantId, busy }: Props) { isPending={update.isPending} error={update.error?.message} onClose={() => { - if (!update.isPending) setOpen(false); + if (!update.isPending) setDialogOpen(false); }} onSave={(input) => update.mutate(input)} /> @@ -135,3 +310,236 @@ function RuntimeSettingsEntry({ client, registration, tenantId, busy }: Props) {
); } + +interface QuickLocalModelMenuProps { + busy?: boolean; + isPending: boolean; + onClose: () => void; + onOpenRuntimeSettings: () => void; + onSelect: (option: QuickLocalOption) => void; + options: readonly QuickLocalOption[]; + registration: TeamHubTenantAgent; + tenantId: string; + triggerRef: RefObject; +} + +function QuickLocalModelMenu({ + busy, + isPending, + onClose, + onOpenRuntimeSettings, + onSelect, + options, + registration, + tenantId, + triggerRef, +}: QuickLocalModelMenuProps) { + const inRouter = useInRouterContext(); + const [position, setPosition] = useState({ left: 8, top: 8 }); + const [menuSide, setMenuSide] = useState<"top" | "bottom">("top"); + const menuRef = useRef(null); + + const showComputerNames = options.some( + (other) => other.computerId !== options[0]?.computerId, + ); + const placement = registration.runtime.placement; + const primaryComputerId = + placement?.type === "local" && + options.some((opt) => opt.computerId === placement.computer_id) + ? placement.computer_id + : options[0]?.computerId; + const localModelsHref = primaryComputerId + ? `/t/${encodeURIComponent(tenantId)}/settings/local-models?computer=${encodeURIComponent(primaryComputerId)}` + : undefined; + + useLayoutEffect(() => { + const updatePosition = () => { + const triggerRect = triggerRef.current?.getBoundingClientRect(); + if (!triggerRect) return; + const next = computePopoverPosition( + triggerRect, + menuRef.current?.getBoundingClientRect(), + ); + setPosition({ left: next.left, top: next.top }); + setMenuSide(next.side); + }; + updatePosition(); + window.addEventListener("scroll", updatePosition, true); + window.addEventListener("resize", updatePosition); + const observer = + typeof ResizeObserver === "undefined" + ? undefined + : new ResizeObserver(updatePosition); + if (menuRef.current) observer?.observe(menuRef.current); + return () => { + window.removeEventListener("scroll", updatePosition, true); + window.removeEventListener("resize", updatePosition); + observer?.disconnect(); + }; + }, [triggerRef]); + + const closeMenu = useEffectEvent(onClose); + + useEffect(() => { + const frame = requestAnimationFrame(() => { + const firstEnabled = menuRef.current?.querySelector( + '[role="menuitem"]:not(:disabled)', + ); + firstEnabled?.focus({ preventScroll: true }); + }); + const closeOutside = (event: Event) => { + const target = event.target as Node | null; + if ( + target && + !triggerRef.current?.contains(target) && + !menuRef.current?.contains(target) + ) { + closeMenu(); + } + }; + const closeOnEscape = (event: KeyboardEvent) => { + if (event.key !== "Escape") return; + event.preventDefault(); + closeMenu(); + triggerRef.current?.focus(); + }; + document.addEventListener("pointerdown", closeOutside); + document.addEventListener("mousedown", closeOutside); + document.addEventListener("keydown", closeOnEscape); + return () => { + cancelAnimationFrame(frame); + document.removeEventListener("pointerdown", closeOutside); + document.removeEventListener("mousedown", closeOutside); + document.removeEventListener("keydown", closeOnEscape); + }; + }, [triggerRef]); + + function handleMenuKeyDown(event: ReactKeyboardEvent) { + if (event.key === "Escape") { + event.preventDefault(); + event.stopPropagation(); + onClose(); + triggerRef.current?.focus(); + return; + } + if (event.key === "Tab") { + onClose(); + return; + } + if ( + event.key !== "ArrowDown" && + event.key !== "ArrowUp" && + event.key !== "Home" && + event.key !== "End" + ) { + return; + } + event.preventDefault(); + const enabled = Array.from( + menuRef.current?.querySelectorAll( + '[role="menuitem"]:not(:disabled)', + ) ?? [], + ); + if (!enabled.length) return; + if (event.key === "Home") { + enabled[0]?.focus(); + return; + } + if (event.key === "End") { + enabled[enabled.length - 1]?.focus(); + return; + } + const direction = event.key === "ArrowDown" ? 1 : -1; + const current = enabled.indexOf(document.activeElement as HTMLElement); + const next = + current < 0 + ? direction > 0 + ? 0 + : enabled.length - 1 + : (current + direction + enabled.length) % enabled.length; + enabled[next]?.focus(); + } + + return createPortal( + <> +