diff --git a/apps/computer/local-daemon/README.md b/apps/computer/local-daemon/README.md index da2de5d4..eac668ec 100644 --- a/apps/computer/local-daemon/README.md +++ b/apps/computer/local-daemon/README.md @@ -151,11 +151,17 @@ 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 sets `PI_CODING_AGENT_DIR` on `localmodels` tool calls to match + `OverlayEnvironment` (in local mode) and `workspace.PiSourceEnvironment` + (`~/.pi/agent` when unset) so loaded models land in the profile Pi probes + and runs. When a model is loaded on a Computer without Pi, + `internal/localmodels` installs the pinned Pi runtime asynchronously in the + background under the shared harness root (passing the bundled Node binary via + `--node` when available and backing off on failure) before asking the Runtime + monitor for a full discovery, so TeamHub sees Pi become ready with the loaded + model. Each model's `pi_model` (`/`, as an Agent's + `runtime_model` stores it) is passed through as `localmodels` reports it; the + endpoint keeps only the loaded engine's `base_url`. ## Model source diff --git a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go index f3378a0e..a6634e5b 100644 --- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go +++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go @@ -136,12 +136,40 @@ func run(args []string) (resultErr error) { if computer.LocalModels != nil { localModelsStateDir = computer.LocalModels.StateDirectory } + 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, + } + } currentProviderEnvironment := func() map[string]string { return localmodels.OverlayEnvironment(baseProviderEnvironment, localModelsStateDir) } + harnessDirectory := filepath.Join(computer.StateDirectory, "local-models", "harness") + if localModelsTool != nil && localModelsTool.StateDirectory != "" { + harnessDirectory = filepath.Clean(filepath.Join(localModelsTool.StateDirectory, "..", "..", "harness")) + } + piEnabled := len(computer.EnabledRuntimes) == 0 || slices.Contains(computer.EnabledRuntimes, "pi") + var managedPi string + if localModelsTool != nil && piEnabled && localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir)["pi"] == "" { + inspectCtx, cancelInspect := context.WithTimeout(context.Background(), 20*time.Second) + found, inspectErr := localModelsTool.InspectPi(inspectCtx, currentProviderEnvironment(), harnessDirectory) + cancelInspect() + if inspectErr != nil { + slog.Warn("local_models.pi_inspect_failed", "error", inspectErr) + } else { + managedPi = found + } + } + var localModels *localmodels.Library currentDiscoveryOptions := func() provider.DiscoveryOptions { + currentPi := managedPi + if localModels != nil && localModels.PiExecutable() != "" { + currentPi = localModels.PiExecutable() + } return provider.DiscoveryOptions{ - ConfiguredPaths: localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir), + ConfiguredPaths: withManagedPiPath(localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir), currentPi), Runtimes: computer.EnabledRuntimes, PreferredRuntime: computer.PreferredRuntime, RuntimeHomeRoot: filepath.Join(computer.StateDirectory, "runtime-homes"), @@ -305,17 +333,22 @@ func run(args []string) (resultErr error) { runtimeRefresh := make(chan struct{}, 1) var localDaemon *daemon.Daemon var controller *controlController - 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, - } - } jobDir := filepath.Join(computer.StateDirectory, "local-models", "jobs") - localModels := localmodels.New(localmodels.Options{ + var nodePath string + if toolInstallation != nil { + nodePath = toolInstallation.NodePath + } + _, hasPi := discovered.Installations["pi"] + libraryHarnessDir := "" + if piEnabled && (!hasPi || managedPi != "") { + libraryHarnessDir = harnessDirectory + } + localModels = localmodels.New(localmodels.Options{ Tool: localModelsTool, Environment: baseProviderEnvironment, - JobDirectory: jobDir, + JobDirectory: jobDir, + HarnessDirectory: libraryHarnessDir, + InitialPiExecutable: managedPi, + NodePath: nodePath, // The library publishes only from Act and Refresh, which the Daemon // calls once it runs. Publish: func(snapshot wire.LocalModelsSnapshot) { localDaemon.PublishLocalModels(snapshot) }, @@ -336,10 +369,6 @@ func run(args []string) (resultErr error) { } return inventory.publish(publishCtx, nextRuntimes, next.Diagnostics) } - var nodePath string - if toolInstallation != nil { - nodePath = toolInstallation.NodePath - } modelSource := localmodels.NewModelSource(localmodels.ModelSourceOptions{ Tool: localModelsTool, Environment: baseProviderEnvironment, @@ -758,6 +787,18 @@ func installedTeamHubCLIBinAt(executable string) string { return directory } +func withManagedPiPath(paths map[string]string, managed string) map[string]string { + if managed == "" || paths["pi"] != "" { + return paths + } + cloned := maps.Clone(paths) + if cloned == nil { + cloned = make(map[string]string, 1) + } + cloned["pi"] = managed + return cloned +} + func runtimeProviderEnvironment(source map[string]string, gatewayToken, codexAuthFile string) map[string]string { result := computerconfig.RuntimeEnvironment(source, gatewayToken) if codexAuthFile != "" { diff --git a/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go b/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go index 5630d534..e5309d76 100644 --- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go +++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go @@ -262,3 +262,18 @@ func TestByokRuntimeCatalogsShowTheEnteredModel(t *testing.T) { } } } + +func TestWithManagedPiPathRespectsConfiguredPiAndClonesSafely(t *testing.T) { + if got := withManagedPiPath(nil, ""); got != nil { + t.Fatalf("empty managed path returned %v", got) + } + configured := map[string]string{"codex": "/bin/codex", "pi": "/custom/pi"} + if got := withManagedPiPath(configured, "/managed/pi"); got["pi"] != "/custom/pi" { + t.Fatalf("managed Pi overwrote configured path: %v", got) + } + original := map[string]string{"codex": "/bin/codex"} + merged := withManagedPiPath(original, "/managed/pi") + if merged["pi"] != "/managed/pi" || original["pi"] != "" { + t.Fatalf("merged=%v original=%v", merged, original) + } +} 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 ea965530..00a05a0c 100644 --- a/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go +++ b/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go @@ -46,13 +46,16 @@ func runFakeTool(args []string) int { if len(args) >= 6 && strings.Join(args[:5], " ") == "-P -s -m localmodels source" { return runFakeSourceTool(state, args[5:]) } + if len(args) >= 6 && strings.Join(args[:5], " ") == "-P -s -m localmodels harness" { + return runFakeHarness(state, args[5:]) + } if len(args) < 6 || strings.Join(args[:5], " ") != "-P -s -m localmodels library" { fmt.Fprintln(os.Stderr, "unexpected invocation", args) return 3 } verb := args[5] log, _ := os.OpenFile(filepath.Join(state, "calls.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) - _, _ = fmt.Fprintf(log, "%s|PYTHONPATH=%s|cwd=%s\n", strings.Join(args[5:], " "), os.Getenv("PYTHONPATH"), workingDirectory()) + _, _ = fmt.Fprintf(log, "%s|PYTHONPATH=%s|cwd=%s|PI_CODING_AGENT_DIR=%s\n", strings.Join(args[5:], " "), os.Getenv("PYTHONPATH"), workingDirectory(), os.Getenv("PI_CODING_AGENT_DIR")) _ = log.Close() if verb == "status" { if code, err := os.ReadFile(filepath.Join(state, "status-exit")); err == nil { @@ -98,6 +101,76 @@ 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 "" + } + root := argValue("--root") + installedPath := filepath.Join(root, "tools", "pi", "1.3.0", "pi") + statusJSON := func(installed bool) string { + if installed { + return fmt.Sprintf(`{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":true,"executable":%q,"argvPrefix":[]}]}`, installedPath) + } + return `{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":false,"executable":null,"argvPrefix":[]}]}` + } + switch verb { + case "status": + if _, err := os.Stat(filepath.Join(state, "harness-status-fail")); err == nil { + return 1 + } + _, err := os.Stat(filepath.Join(state, "harness-pi-installed")) + fmt.Println(statusJSON(err == nil)) + return 0 + case "plan": + waitGate := filepath.Join(state, "harness-plan-wait") + if _, err := os.Stat(waitGate); err == nil { + _ = os.WriteFile(filepath.Join(state, "harness-plan-entered"), []byte("1"), 0o600) + deadline := time.Now().Add(10 * time.Second) + for time.Now().Before(deadline) { + if _, statErr := os.Stat(waitGate); statErr != nil { + break + } + time.Sleep(10 * time.Millisecond) + } + } + if _, err := os.Stat(filepath.Join(state, "harness-plan-fail")); err == nil { + return 1 + } + outPath := argValue("--out") + plan := fmt.Sprintf(`{"schema":"localmodels.harness-plan/v1","id":%q,"runtime":"pi","root":%q}`, strings.Repeat("a", 64), root) + if outPath != "" { + _ = os.WriteFile(outPath, []byte(plan), 0o600) + } + fmt.Println(plan) + return 0 + case "apply": + if _, err := os.Stat(filepath.Join(state, "harness-apply-fail")); err == nil { + return 1 + } + if argValue("--approve") != strings.Repeat("a", 64) { + return 2 + } + _ = os.WriteFile(filepath.Join(state, "harness-pi-installed"), []byte(installedPath), 0o600) + fmt.Println(`{"kind":"stage","stage":"install","state":"running"}`) + fmt.Println(statusJSON(true)) + return 0 + default: + return 3 + } +} + // fakeTool lays out a Tool that runs the fake. type fakeTool struct { t *testing.T diff --git a/apps/computer/local-daemon/internal/localmodels/library.go b/apps/computer/local-daemon/internal/localmodels/library.go index 58aee26a..07b3cf3b 100644 --- a/apps/computer/local-daemon/internal/localmodels/library.go +++ b/apps/computer/local-daemon/internal/localmodels/library.go @@ -23,9 +23,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,6 +43,13 @@ 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) @@ -47,9 +57,11 @@ type Options struct { // changes the model list Pi reports. RuntimeChanged func() // ProgressInterval bounds how often job progress is published. - ProgressInterval time.Duration - StatusTimeout time.Duration - ActionTimeout time.Duration + ProgressInterval time.Duration + StatusTimeout time.Duration + ActionTimeout time.Duration + EnsurePiTimeout time.Duration + EnsurePiRetryInterval time.Duration } // Library runs the localmodels tool for one Computer. @@ -71,28 +83,38 @@ 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 + piExecutable string + piEnsuring bool + piFailures int + piRetryAt time.Time jobs map[string]*job lastProgress time.Time progressTimer *time.Timer @@ -111,8 +133,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 +149,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) {} } @@ -141,25 +172,88 @@ func New(options Options) *Library { func (l *Library) Close() { l.stop() l.mu.Lock() + l.closed = true if l.progressTimer != nil { l.progressTimer.Stop() } l.mu.Unlock() + l.piWg.Wait() +} + +// PiExecutable returns the managed Pi executable provisioned for local +// models, or an empty string when none has been resolved. +func (l *Library) PiExecutable() string { + l.mu.Lock() + defer l.mu.Unlock() + return l.piExecutable +} + +func (l *Library) scheduleEnsurePi() { + if l.harnessDirectory == "" || l.tool == nil || !l.tool.installed() { + return + } + l.mu.Lock() + now := l.now() + if l.closed || l.ctx.Err() != nil || l.piExecutable != "" || l.piEnsuring || (!l.piRetryAt.IsZero() && now.Before(l.piRetryAt)) { + l.mu.Unlock() + return + } + l.piEnsuring = true + l.piWg.Add(1) + l.mu.Unlock() + + go func() { + defer l.piWg.Done() + ctx, cancel := context.WithTimeout(l.ctx, l.ensurePiTimeout) + executable, err := l.tool.EnsurePi(ctx, l.environment, l.harnessDirectory, l.nodePath) + cancel() + + l.mu.Lock() + l.piEnsuring = false + if l.closed || l.ctx.Err() != nil { + l.mu.Unlock() + return + } + if err == nil && executable != "" { + l.piExecutable = executable + l.piFailures = 0 + l.piRetryAt = time.Time{} + l.mu.Unlock() + slog.Info("local_models.pi_ready", "executable", executable) + l.runtimeChanged() + return + } + l.piFailures++ + 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) + l.mu.Unlock() + if err != nil { + slog.Warn("local_models.pi_install_failed", "error", err, "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 } @@ -171,6 +265,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) } 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 00000000..6d8b49ff --- /dev/null +++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime.go @@ -0,0 +1,205 @@ +package localmodels + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "log/slog" + "os" + "path/filepath" + "regexp" + + "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" + 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"` + } `json:"runtimes"` +} + +type harnessPlanDoc struct { + Schema string `json:"schema"` + ID string `json:"id"` + Runtime string `json:"runtime"` +} + +// InspectPi reports the verified Pi executable managed under harnessDir, or an +// empty string when Pi is not installed there yet. +func (t Tool) InspectPi(ctx context.Context, environment map[string]string, harnessDir string) (string, error) { + if harnessDir == "" || !t.installed() { + return "", nil + } + info, err := os.Stat(harnessDir) + if errors.Is(err, os.ErrNotExist) { + return "", nil + } + if err != nil { + return "", err + } + if !info.IsDir() { + return "", errors.New("harness root is not a directory") + } + 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 "", fmt.Errorf("harness status exited with code %d", exitCode) + } + return parsePiFromHarnessStatus(stdout) +} + +// EnsurePi returns the Pi executable managed under harnessDir, installing the +// pinned Pi runtime through localmodels harness when it is not installed yet. +// When nodePath is non-empty and names an existing file, it is passed via --node +// so harness does not need to download a separate Node binary. +func (t Tool) EnsurePi(ctx context.Context, environment map[string]string, harnessDir, nodePath string) (string, error) { + if harnessDir == "" || !t.installed() { + return "", nil + } + if existing, err := t.InspectPi(ctx, environment, harnessDir); err == nil && existing != "" { + return existing, nil + } + if err := privatefs.EnsureDir(harnessDir); err != nil { + return "", err + } + planDir, err := os.MkdirTemp(harnessDir, ".pi-plan-*") + if err != nil { + return "", err + } + defer func() { _ = os.RemoveAll(planDir) }() + if err := privatefs.EnsureDir(planDir); err != nil { + return "", err + } + planPath := filepath.Join(planDir, "plan.json") + planArgs := []string{"-P", "-s", "-m", "localmodels", "harness", "plan", "--runtime", "pi", "--root", harnessDir} + if nodePath != "" { + if info, statErr := os.Stat(nodePath); statErr == nil && !info.IsDir() { + planArgs = append(planArgs, "--node", nodePath) + } + } + planArgs = append(planArgs, "--out", planPath, "--json") + if _, exitCode, err := t.runHarness(ctx, environment, planArgs); err != nil { + return "", err + } else if exitCode != 0 { + return "", fmt.Errorf("harness plan exited with code %d", exitCode) + } + 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 "", fmt.Errorf("harness apply exited with code %d", exitCode) + } + if executable, parseErr := parseLastHarnessStatus(stdout); parseErr == nil && executable != "" { + return executable, nil + } + return t.InspectPi(ctx, environment, harnessDir) +} + +func (t Tool) runHarness(ctx context.Context, environment map[string]string, args []string) ([]byte, int, error) { + stdout, 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 + process.Prepare(cmd) + err = cmd.Start() + _ = stdout.Close() + _ = stderr.Close() + if err != nil { + return nil, -1, err + } + exited := make(chan error, 1) + go func() { exited <- cmd.Wait() }() + select { + case <-exited: + case <-ctx.Done(): + _ = cmd.Process.Kill() + <-exited + return nil, -1, context.Cause(ctx) + } + 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 parseLastHarnessStatus(stdout []byte) (string, error) { + lines := bytes.Split(stdout, []byte{'\n'}) + for index := len(lines) - 1; index >= 0; index-- { + line := bytes.TrimSpace(lines[index]) + if len(line) == 0 { + continue + } + return parsePiFromHarnessStatus(line) + } + return "", errors.New("harness output is empty") +} + +func parsePiFromHarnessStatus(encoded []byte) (string, error) { + var doc harnessStatusDoc + if err := json.Unmarshal(bytes.TrimSpace(encoded), &doc); err != nil { + return "", fmt.Errorf("decode harness status: %w", err) + } + if doc.Schema != harnessStatusSchema { + return "", fmt.Errorf("harness status schema is %q", doc.Schema) + } + for _, row := range doc.Runtimes { + if row.Runtime != "pi" { + continue + } + if !row.Installed || len(row.ArgvPrefix) != 0 || !filepath.IsAbs(row.Executable) { + return "", nil + } + return row.Executable, nil + } + return "", nil +} diff --git a/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go new file mode 100644 index 00000000..66488431 --- /dev/null +++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go @@ -0,0 +1,314 @@ +package localmodels + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "slices" + "strings" + "sync" + "testing" + "time" +) + +func TestToolCommandAlignsPiCodingAgentDir(t *testing.T) { + fake := newFakeTool(t) + home := filepath.Join(filepath.Dir(fake.state), "home") + explicit := filepath.Join(filepath.Dir(fake.state), "custom-pi") + + cmdDefault := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": ""}, Action{Name: ActionRefresh}) + wantDefault := "PI_CODING_AGENT_DIR=" + filepath.Join(home, ".pi", "agent") + if !slices.Contains(cmdDefault.Env, wantDefault) { + t.Fatalf("expected %q in env %v", wantDefault, cmdDefault.Env) + } + + cmdExplicit := fake.tool.command(map[string]string{"HOME": home, "pi_coding_agent_dir": explicit}, Action{Name: ActionRefresh}) + wantExplicit := "PI_CODING_AGENT_DIR=" + explicit + if !slices.Contains(cmdExplicit.Env, wantExplicit) { + t.Fatalf("expected %q in env %v", wantExplicit, cmdExplicit.Env) + } + + gatewayPi := filepath.Join(fake.tool.StateDirectory, "gateway-home", ".pi", "agent") + if err := os.MkdirAll(gatewayPi, 0o700); err != nil { + t.Fatal(err) + } + writeModelSourceJSON(t, fake.tool.StateDirectory, map[string]any{ + "schema": modelSourceSchema, + "mode": "local", + "runtime": "pi", + }) + cmdOverlay := fake.tool.command(map[string]string{"HOME": home}, Action{Name: ActionRefresh}) + wantOverlay := "PI_CODING_AGENT_DIR=" + gatewayPi + if !slices.Contains(cmdOverlay.Env, wantOverlay) { + t.Fatalf("expected local overlay %q in env %v", wantOverlay, cmdOverlay.Env) + } +} + +func TestLoadProvisionsPiInHarnessDirectoryWithNodePathAndNotifiesRuntimeChanged(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "idle"))) + loaded, _ := json.Marshal(status(model(testModel, "loaded"))) + fake.script("load", fakeScript{ + Lines: []fakeLine{ + {Line: progressLine(testModel, "load", 0, 1)}, + {Line: resultLine(testModel), DelayMS: 20}, + }, + StatusAfter: loaded, + }) + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + home := filepath.Join(filepath.Dir(fake.state), "home") + nodePath := filepath.Join(t.TempDir(), "node") + if err := os.WriteFile(nodePath, []byte("#!/bin/sh\n"), 0o700); err != nil { + t.Fatal(err) + } + record := &recorder{} + var seenMu sync.Mutex + var seenPiAtChange string + library := New(Options{ + Tool: &fake.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": home, "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + NodePath: nodePath, + Publish: record.publish, + ProgressInterval: 20 * time.Millisecond, + }) + library.runtimeChanged = func() { + if executable := library.PiExecutable(); executable != "" { + seenMu.Lock() + seenPiAtChange = executable + seenMu.Unlock() + } + record.runtimeChanged() + } + t.Cleanup(library.Close) + + library.Refresh(context.Background()) + if library.PiExecutable() != "" { + t.Fatalf("idle library unexpectedly provisioned Pi: %q", library.PiExecutable()) + } + + if refusal, answered := library.Act(context.Background(), Action{Name: ActionLoad, Model: testModel}, soon()); refusal != nil || !answered { + t.Fatalf("load refusal=%+v answered=%v", refusal, answered) + } + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "Pi provisioned and runtimeChanged fired", func() bool { + seenMu.Lock() + seen := seenPiAtChange + seenMu.Unlock() + return record.changes() > 0 && library.PiExecutable() == want && seen == want + }) + calls := fake.calls() + if !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness status ") }) || + !slices.ContainsFunc(calls, func(c string) bool { + return strings.HasPrefix(c, "harness plan ") && strings.Contains(c, "--node "+nodePath) + }) || + !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness apply ") }) { + t.Fatalf("expected harness status/plan(--node)/apply in calls: %v", calls) + } + wantPiDir := "PI_CODING_AGENT_DIR=" + filepath.Join(home, ".pi", "agent") + if !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "load ") && strings.Contains(c, wantPiDir) }) { + t.Fatalf("expected load call with %s in %v", wantPiDir, calls) + } +} + +func 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 TestEnsurePiRunsOutsideRefreshLockAndBacksOffOnFailure(t *testing.T) { + fake := newFakeTool(t) + const otherModel = "other-model:Q4_K_M" + fake.write("status.json", status(model(testModel, "loaded"), model(otherModel, "absent"))) + waitGate := filepath.Join(fake.state, "harness-plan-wait") + if err := os.WriteFile(waitGate, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + record := &recorder{} + library := New(Options{ + Tool: &fake.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + Publish: record.publish, + RuntimeChanged: record.runtimeChanged, + ProgressInterval: 10 * time.Millisecond, + }) + t.Cleanup(library.Close) + + first := library.Refresh(context.Background()) + if !first.Available || modelState(first, testModel).State != "loaded" { + t.Fatalf("first Refresh=%+v", first) + } + waitFor(t, "background harness plan started", func() bool { + _, err := os.Stat(filepath.Join(fake.state, "harness-plan-entered")) + return err == nil + }) + + // While EnsurePi is blocked on harness plan, another Refresh and a job's + // phaseStarted status reread must proceed without blocking on refreshMu. + second := library.Refresh(context.Background()) + if !second.Available || modelState(second, testModel).State != "loaded" { + t.Fatalf("second Refresh=%+v", second) + } + downloaded, _ := json.Marshal(status(model(testModel, "loaded"), model(otherModel, "idle"))) + 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 + }) + + // Failed installs back off instead of retrying on every Refresh, then retry + // once the backoff window elapses. + fake2 := newFakeTool(t) + fake2.write("status.json", status(model(testModel, "loaded"))) + failMarker := filepath.Join(fake2.state, "harness-plan-fail") + if err := os.WriteFile(failMarker, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir2 := filepath.Join(filepath.Dir(fake2.state), "harness") + record2 := &recorder{} + var clockMu sync.Mutex + currentTime := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC) + library2 := New(Options{ + Tool: &fake2.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir2, + Publish: record2.publish, + RuntimeChanged: record2.runtimeChanged, + EnsurePiRetryInterval: 30 * time.Second, + }) + library2.now = func() time.Time { + clockMu.Lock() + defer clockMu.Unlock() + return currentTime + } + t.Cleanup(library2.Close) + + library2.Refresh(context.Background()) + waitFor(t, "initial Pi install failure recorded", func() bool { + library2.mu.Lock() + defer library2.mu.Unlock() + return !library2.piEnsuring && library2.piFailures == 1 + }) + planCalls := func() int { + count := 0 + for _, call := range fake2.calls() { + if strings.HasPrefix(call, "harness plan ") { + count++ + } + } + return count + } + if got := planCalls(); got != 1 { + t.Fatalf("expected 1 harness plan attempt, got %d (%v)", got, fake2.calls()) + } + + library2.Refresh(context.Background()) + library2.Refresh(context.Background()) + if got := planCalls(); got != 1 { + t.Fatalf("expected backoff to suppress retries before window elapses, got %d calls", got) + } + + if err := os.Remove(failMarker); err != nil { + t.Fatal(err) + } + clockMu.Lock() + currentTime = currentTime.Add(31 * time.Second) + clockMu.Unlock() + + library2.Refresh(context.Background()) + want2 := filepath.Join(harnessDir2, "tools", "pi", "1.3.0", "pi") + waitFor(t, "Pi install retried and succeeded after backoff elapsed", func() bool { + return library2.PiExecutable() == want2 && record2.changes() == 1 + }) +} diff --git a/apps/computer/local-daemon/internal/localmodels/tool.go b/apps/computer/local-daemon/internal/localmodels/tool.go index 3f938d9b..c2f31049 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,47 @@ func (t Tool) arguments(action Action) []string { } } -// command builds one tool invocation the way the installer runs it: the -// sealed kit on PYTHONPATH and as the working directory, no user site -// packages, no bytecode writes into the sealed kit. +// command builds one library invocation for the tool. func (t Tool) command(environment map[string]string, action Action) *exec.Cmd { - cmd := exec.Command(t.Python, t.arguments(action)...) + return t.commandArgs(environment, t.arguments(action)) +} + +// commandArgs builds one tool invocation the way the installer runs it: the +// sealed kit on PYTHONPATH and as the working directory, no user site +// packages, no bytecode writes into the sealed kit, and PI_CODING_AGENT_DIR +// aligned with OverlayEnvironment (local mode) and workspace.PiSourceEnvironment +// so models written by localmodels reach the same Pi agent profile that the +// Daemon probes and runs. +func (t Tool) commandArgs(environment map[string]string, args []string) *exec.Cmd { + cmd := exec.Command(t.Python, args...) cmd.Dir = t.ModuleDirectory - values := make(map[string]string, len(environment)+4) + values := make(map[string]string, len(environment)+5) + var explicitPiDir string for key, value := range environment { - switch strings.ToUpper(key) { + upper := strings.ToUpper(key) + switch upper { case "PYTHONHOME", "PYTHONSTARTUP", "PYTHONINSPECT", "PYTHONPATH", "LOCALMODELS_STATE": continue + case "PI_CODING_AGENT_DIR": + if trimmed := strings.TrimSpace(value); trimmed != "" { + explicitPiDir = trimmed + } + continue } values[key] = value } + if explicitPiDir != "" { + values["PI_CODING_AGENT_DIR"] = explicitPiDir + } else if overlay := ReadOverlay(t.StateDirectory); overlay != nil && overlay.Mode == "local" { + if overlaid := OverlayEnvironment(values, t.StateDirectory); overlaid["PI_CODING_AGENT_DIR"] != "" { + values["PI_CODING_AGENT_DIR"] = overlaid["PI_CODING_AGENT_DIR"] + } + } + if values["PI_CODING_AGENT_DIR"] == "" { + if piEnv, err := workspace.PiSourceEnvironment(values); err == nil && piEnv["PI_CODING_AGENT_DIR"] != "" { + values["PI_CODING_AGENT_DIR"] = piEnv["PI_CODING_AGENT_DIR"] + } + } values["PYTHONPATH"] = t.ModuleDirectory values["LOCALMODELS_STATE"] = t.StateDirectory values["PYTHONNOUSERSITE"] = "1" diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md index c59ca0d6..9309361f 100644 --- a/apps/web/AGENTS.md +++ b/apps/web/AGENTS.md @@ -34,10 +34,14 @@ build:assets` to check the initial JS budget. orders and labels them, picks the Make room default, renders tinted family icon tiles, Agent usage counts, structured capability/parameter badges (never matched against `name` or `description`), state-distinct cards (`loaded`, - `recommended`, `failed`, `unavailable`), download speed/ETA, and top - diagnostic callouts with a two-column model-card facts grid from structured - `LocalModel` fields in `local-models-rules.ts`, and maps every code and enum - label to English in `local-models-copy.ts`. Without `?computer=` + `recommended`, `failed`, `unavailable`), download speed/ETA, live Bifrost + (`Pi`) `Gateway` readiness, and top diagnostic callouts with a two-column + model-card facts grid from structured `LocalModel` fields in + `local-models-rules.ts`, resolves Sponsor-owned Agent eligibility and `Pi` + runtime switch payloads in `local-model-agents.ts` (`Use with `, + `Open chat`, `Switch Agent…` in `Used by`, and `Make room` automatic Agent + handoff once the replacement model loads), 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 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 00000000..c8c3dd28 --- /dev/null +++ b/apps/web/src/features/local-models/local-model-agents.test.ts @@ -0,0 +1,264 @@ +import { describe, expect, it } from "vitest"; + +import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; +import { createTeamHubTestSeed } from "@/test/teamhub-seed"; + +import { + assignableLocalModelAgents, + buildLocalModelRuntimeUpdate, + isAgentUsingModel, + primaryLocalModelAgent, + usedByAgentNames, +} from "./local-model-agents"; +import { localModel } from "./local-models-test-support"; + +describe("local-model-agents", () => { + it("filters to Sponsor-owned Agents, places Another Me first, and keeps compatible workspace grants", () => { + const seed = createTeamHubTestSeed(); + const computer = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + workspace_grants: [ + { + grant_id: "grant-ro", + label: "Read only", + mode: "read-only" as const, + }, + { + grant_id: "grant-rw", + label: "Workspace", + mode: "workspace-write" as const, + }, + { + grant_id: "grant-other", + label: "Other workspace", + mode: "workspace-write" as const, + }, + ], + }; + const [codex, lev8, delegate] = seed.tenantAgents; + const codexOnLab = { + ...codex!, + runtime: { + ...codex!.runtime, + placement: { + computer_id: "computer-lab", + runtime_type: "codex" as const, + type: "local" as const, + workspace_grant_id: "grant-other", + }, + }, + }; + const result = assignableLocalModelAgents({ + agents: [codexOnLab, lev8!, delegate!], + anotherMeActorId: delegate!.actor.id, + availability: [ + { + actor_id: codexOnLab.actor.id, + availability: { + active_task_count: 1, + label: "Busy", + status: "online", + }, + }, + ], + computer, + computerId: "computer-lab", + currentActorId: seed.identity.actors[0]!.id, + }); + + // Lev8 GTM is sponsored by Maya, not Jenny. + expect(result.map((agent) => agent.actorId)).toEqual([ + delegate!.actor.id, + codexOnLab.actor.id, + ]); + expect(result[0]!).toMatchObject({ + busy: false, + grantId: "grant-rw", + isAnotherMe: true, + name: "Jenny's Delegate", + }); + expect(result[1]!).toMatchObject({ + busy: true, + grantId: "grant-other", + isAnotherMe: false, + name: "Codex", + }); + expect(primaryLocalModelAgent(result)?.actorId).toBe(delegate!.actor.id); + }); + + it("excludes all Agents on an upgrade-required or read-only Computer", () => { + const seed = createTeamHubTestSeed(); + const upgradeComputer = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + upgrade_required: true, + }; + expect( + assignableLocalModelAgents({ + agents: seed.tenantAgents, + anotherMeActorId: seed.tenantAgents[2]!.actor.id, + computer: upgradeComputer, + computerId: "computer-lab", + currentActorId: seed.identity.actors[0]!.id, + }), + ).toEqual([]); + + const readOnlyComputer = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + workspace_grants: [ + { + grant_id: "grant-ro", + label: "Read only", + mode: "read-only" as const, + }, + ], + }; + expect( + assignableLocalModelAgents({ + agents: seed.tenantAgents, + anotherMeActorId: seed.tenantAgents[2]!.actor.id, + computer: readOnlyComputer, + computerId: "computer-lab", + currentActorId: seed.identity.actors[0]!.id, + }), + ).toEqual([]); + }); + + it("matches Agents via either snapshot used_by or live Pi placement", () => { + const seed = createTeamHubTestSeed(); + const delegate = { + ...seed.tenantAgents[2]!, + runtime: { + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi" as const, + type: "local" as const, + workspace_grant_id: "grant-rw", + }, + }, + }; + const [agent] = assignableLocalModelAgents({ + agents: [delegate], + anotherMeActorId: delegate.actor.id, + computerId: "computer-lab", + currentActorId: seed.identity.actors[0]!.id, + }); + const entry = { + model: localModel({ + id: "qwen3-coder-30b:Q4_K_M", + name: "Qwen3 Coder 30B", + pi_model: "localmodels-library/qwen3-coder-30b", + state: "loaded", + }), + status: "loaded" as const, + used_by: [{ agent_id: "agent-research", name: "Research agent" }], + }; + + expect(isAgentUsingModel(entry, agent!, "computer-lab")).toBe(true); + expect(isAgentUsingModel(entry, agent!, "computer-other")).toBe(false); + expect(usedByAgentNames(entry, [agent!], "computer-lab")).toEqual([ + "Research agent", + "Jenny's Delegate", + ]); + }); + + it("omits incompatible Claude reasoning efforts when Pi has not listed the model yet, and preserves valid Pi efforts when listed", () => { + const seed = createTeamHubTestSeed(); + const computerWithoutPiModel = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + runtimes: [], + }; + const baseAgent = { + actorId: "actor-delegate", + busy: false, + configurationRevision: 3, + currentComputerId: "computer-lab", + currentModel: "claude-opus-4-1", + currentReasoningEffort: "ultra" as const, + currentRuntimeType: "claude" as const, + grantId: "grant-rw", + isAnotherMe: true, + name: "Jenny's Delegate", + }; + + expect( + buildLocalModelRuntimeUpdate( + baseAgent, + "localmodels-library/qwen3-coder-30b", + "computer-lab", + computerWithoutPiModel, + ), + ).toEqual({ + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-rw", + }, + }); + + const computerWithPiModel: TeamHubLocalComputerPage["items"][number] = { + ...seed.localComputers[0]!, + computer_id: "computer-lab", + runtimes: [ + { + models: [ + { + allowed_reasoning_effort: ["low", "medium", "high"], + default_reasoning_effort: "medium", + id: "localmodels-library/qwen3-coder-30b", + label: "Qwen3 Coder 30B", + }, + ], + status: "ready", + type: "pi", + }, + ], + }; + + expect( + buildLocalModelRuntimeUpdate( + baseAgent, + "localmodels-library/qwen3-coder-30b", + "computer-lab", + computerWithPiModel, + ), + ).toEqual({ + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-rw", + }, + reasoning_effort: "medium", + }); + + expect( + buildLocalModelRuntimeUpdate( + { + ...baseAgent, + currentReasoningEffort: "high", + currentRuntimeType: "pi", + }, + "localmodels-library/qwen3-coder-30b", + "computer-lab", + computerWithPiModel, + ), + ).toEqual({ + model: "localmodels-library/qwen3-coder-30b", + placement: { + computer_id: "computer-lab", + runtime_type: "pi", + type: "local", + workspace_grant_id: "grant-rw", + }, + reasoning_effort: "high", + }); + }); +}); diff --git a/apps/web/src/features/local-models/local-model-agents.ts b/apps/web/src/features/local-models/local-model-agents.ts new file mode 100644 index 00000000..e1418492 --- /dev/null +++ b/apps/web/src/features/local-models/local-model-agents.ts @@ -0,0 +1,190 @@ +import type { + TeamHubAgentReasoningEffort, + TeamHubLocalComputerPage, + TeamHubLocalRuntimeType, + TeamHubTenantAgent, + TeamHubTenantAgentAvailabilitySnapshot, + TeamHubTenantAgentUpdateInput, +} from "@/features/teamhub/teamhub-types"; + +import type { ModelEntry } from "./local-models-rules"; + +/** A Sponsor-owned Agent eligible to run against a loaded Local Model on one Computer. */ +export interface AssignableLocalModelAgent { + actorId: string; + busy: boolean; + configurationRevision: number; + currentComputerId?: string; + currentModel: string; + currentReasoningEffort?: TeamHubAgentReasoningEffort; + currentRuntimeType?: TeamHubLocalRuntimeType; + grantId: string; + isAnotherMe: boolean; + name: string; +} + +export interface AssignableLocalModelAgentsInput { + agents?: readonly TeamHubTenantAgent[]; + anotherMeActorId?: string; + availability?: readonly TeamHubTenantAgentAvailabilitySnapshot[]; + computer?: TeamHubLocalComputerPage["items"][number]; + computerId: string; + currentActorId?: string; +} + +/** + * Lists Sponsor-owned Agents that can run on `computerId` with the Pi Runtime, + * placing Another Me first so row actions prioritize it consistently. + */ +export function assignableLocalModelAgents( + input: AssignableLocalModelAgentsInput, +): AssignableLocalModelAgent[] { + if ( + !input.currentActorId || + !input.agents?.length || + input.computer?.upgrade_required + ) { + return []; + } + const busyByActorId = new Map( + (input.availability ?? []).map((snapshot) => [ + snapshot.actor_id, + (snapshot.availability.active_task_count ?? 0) > 0, + ]), + ); + const writableGrant = input.computer?.workspace_grants.find( + (grant) => grant.mode === "workspace-write", + ); + const result: AssignableLocalModelAgent[] = []; + for (const agent of input.agents) { + if (agent.created_by.id !== input.currentActorId) continue; + const placement = + agent.runtime.placement?.type === "local" + ? agent.runtime.placement + : undefined; + let grantId: string | undefined; + if (placement?.computer_id === input.computerId) { + const existingGrantId = placement.workspace_grant_id; + const grantStillPresent = + !input.computer || + input.computer.workspace_grants.some( + (grant) => grant.grant_id === existingGrantId, + ); + grantId = grantStillPresent ? existingGrantId : writableGrant?.grant_id; + } else { + grantId = writableGrant?.grant_id; + } + if (!grantId) continue; + result.push({ + actorId: agent.actor.id, + busy: busyByActorId.get(agent.actor.id) ?? false, + configurationRevision: agent.configuration_revision, + currentComputerId: placement?.computer_id, + currentModel: agent.runtime.model, + currentReasoningEffort: agent.runtime.reasoning_effort, + currentRuntimeType: placement?.runtime_type, + grantId, + isAnotherMe: + agent.actor.id === input.anotherMeActorId || + agent.agent_type === "anotherme", + name: agent.actor.display_name, + }); + } + result.sort((a, b) => { + if (a.isAnotherMe !== b.isAnotherMe) return a.isAnotherMe ? -1 : 1; + return a.name.localeCompare(b.name); + }); + return result; +} + +/** + * Whether `agent` already uses `entry` on `computerId`, combining the snapshot's + * `used_by` list with live Agent placement so updates reflect before the next snapshot. + */ +export function isAgentUsingModel( + entry: ModelEntry, + agent: AssignableLocalModelAgent, + computerId: string, +): boolean { + if (entry.used_by.some((user) => user.agent_id === agent.actorId)) { + return true; + } + return ( + Boolean(entry.model.pi_model) && + agent.currentComputerId === computerId && + agent.currentRuntimeType === "pi" && + agent.currentModel === entry.model.pi_model + ); +} + +/** Display names of all Agents currently using `entry` on `computerId`. */ +export function usedByAgentNames( + entry: ModelEntry, + assignableAgents: readonly AssignableLocalModelAgent[], + computerId: string, +): string[] { + const names = entry.used_by.map((user) => user.name); + for (const agent of assignableAgents) { + if ( + isAgentUsingModel(entry, agent, computerId) && + !entry.used_by.some( + (user) => user.agent_id === agent.actorId || user.name === agent.name, + ) + ) { + names.push(agent.name); + } + } + return names; +} + +/** The featured Agent for one-click row actions: Another Me, or the sole Agent. */ +export function primaryLocalModelAgent( + assignableAgents: readonly AssignableLocalModelAgent[], +): AssignableLocalModelAgent | undefined { + return ( + assignableAgents.find((agent) => agent.isAnotherMe) ?? + (assignableAgents.length === 1 ? assignableAgents[0] : undefined) + ); +} + +/** + * Builds the `PATCH /agents/{agentId}` runtime payload that switches `agent` onto + * a Bifrost-backed local model via Pi without carrying incompatible effort settings. + */ +export function buildLocalModelRuntimeUpdate( + agent: AssignableLocalModelAgent, + piModel: string, + computerId: string, + computer?: TeamHubLocalComputerPage["items"][number], +): NonNullable { + const piRuntime = computer?.runtimes.find((item) => item.type === "pi"); + const catalogModel = piRuntime?.models.find((item) => item.id === piModel); + let reasoningEffort: TeamHubAgentReasoningEffort | undefined; + if (catalogModel) { + const allowed = catalogModel.allowed_reasoning_effort ?? []; + if ( + agent.currentRuntimeType === "pi" && + agent.currentReasoningEffort && + allowed.includes(agent.currentReasoningEffort) + ) { + reasoningEffort = agent.currentReasoningEffort; + } else if ( + catalogModel.default_reasoning_effort && + allowed.includes(catalogModel.default_reasoning_effort) + ) { + reasoningEffort = catalogModel.default_reasoning_effort; + } else { + reasoningEffort = allowed[0]; + } + } + return { + model: piModel, + ...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}), + placement: { + computer_id: computerId, + runtime_type: "pi", + type: "local", + workspace_grant_id: agent.grantId, + }, + }; +} diff --git a/apps/web/src/features/local-models/local-model-details.tsx b/apps/web/src/features/local-models/local-model-details.tsx index 94ec5160..d8a2ee03 100644 --- a/apps/web/src/features/local-models/local-model-details.tsx +++ b/apps/web/src/features/local-models/local-model-details.tsx @@ -8,8 +8,16 @@ import { 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 { + type AssignableLocalModelAgent, + isAgentUsingModel, + primaryLocalModelAgent, + usedByAgentNames, +} from "./local-model-agents"; import { capabilityCopy, modelErrorCopy, @@ -18,7 +26,6 @@ import { } from "./local-models-copy"; import type { LocalModelPool, LocalModelsView } from "./local-models-queries"; import { - agentNames, contextLabel, downloadStopped, formatGigabytes, @@ -88,12 +95,51 @@ function CopyEndpoint({ endpoint, name }: { endpoint: string; name: string }) { ); } +function gatewayFactValue( + entry: ModelEntry, + view: LocalModelsView, + computer?: TeamHubLocalComputerPage["items"][number], +): ReactNode { + if (!computer || !entry.model.pi_model) return "Bifrost"; + const piRuntime = computer.runtimes.find((runtime) => runtime.type === "pi"); + if (piRuntime?.status === "ready") { + if (piRuntime.models.some((item) => item.id === entry.model.pi_model)) { + return "Ready · Bifrost (Pi)"; + } + return ( + + Bifrost · Registering with Pi… + + ); + } + if (!piRuntime || piRuntime.status === "not_installed") { + return ( + + {view.computer.status === "online" + ? "Bifrost · Setting up Pi…" + : "Bifrost · Pi not installed"} + + ); + } + return Bifrost · Pi unavailable; +} + /** Everything about one model on one Computer, shown when its row expands. */ export function ModelFacts({ + assignableAgents = [], + assigningAgentId, + computer, + disabled = false, entry, + onAssignAgent, view, }: { + assignableAgents?: readonly AssignableLocalModelAgent[]; + assigningAgentId?: string; + computer?: TeamHubLocalComputerPage["items"][number]; + disabled?: boolean; entry: ModelEntry; + onAssignAgent?: (agent: AssignableLocalModelAgent) => void; view: LocalModelsView; }) { const { model, status } = entry; @@ -108,6 +154,24 @@ export function ModelFacts({ const parameters = parametersLabel(model); const speed = speedLabel(model); const sourceUrl = safeModelSourceUrl(model.source_url); + const computerId = view.computer.computer_id; + const usedNames = usedByAgentNames(entry, assignableAgents, computerId); + const unassigned = assignableAgents.filter( + (agent) => !isAgentUsingModel(entry, agent, computerId), + ); + const primaryAgent = primaryLocalModelAgent(assignableAgents); + const showAgentPicker = + status === "loaded" && + Boolean(model.pi_model) && + Boolean(onAssignAgent) && + (unassigned.some((agent) => !agent.isAnotherMe) || unassigned.length > 1); + const primaryBusy = + status === "loaded" && + Boolean(model.pi_model) && + Boolean(onAssignAgent) && + primaryAgent !== undefined && + !isAgentUsingModel(entry, primaryAgent, computerId) && + primaryAgent.busy; return (
{model.error || status === "unavailable" ? ( @@ -180,10 +244,47 @@ export function ModelFacts({ {model.license ? {model.license} : null} {onComputer ? ( 1 ? "@xl:col-span-2" : undefined} + className={ + usedNames.length > 1 || showAgentPicker || primaryBusy + ? "@xl:col-span-2" + : undefined + } label="Used by" > - {entry.used_by.length ? agentNames(entry) : "No Agent yet"} + + + {usedNames.length ? usedNames.join(", ") : "No Agent yet"} + + {showAgentPicker && onAssignAgent ? ( + + ) : primaryBusy ? ( + + Finish the current turn to switch + + ) : null} + ) : null} {sourceUrl ? ( @@ -199,6 +300,11 @@ export function ModelFacts({ ) : null} + {status === "loaded" && model.endpoint ? ( + + {gatewayFactValue(entry, view, computer)} + + ) : null} {model.endpoint ? ( void; - onConfirm: (unloadIds: string[]) => void; + onConfirm: (unloadIds: string[], switchAgentIds: string[]) => void; view: LocalModelsView; }) { const [selected, setSelected] = useState(() => makeRoomSelection(check)); + const [switchAgents, setSwitchAgents] = useState(true); 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 chosenLoaded = check.loaded.filter((item) => chosen.has(item.model.id)); + const affected = chosenLoaded.flatMap((item) => + item.used_by.map((user) => user.name), + ); + const affectedAssignable = assignableAgents.filter((agent) => + chosenLoaded.some((item) => + isAgentUsingModel(item, agent, view.computer.computer_id), + ), ); + const canSwitchOnLoad = + Boolean(entry.model.pi_model) && affectedAssignable.length > 0; return ( Cancel
); diff --git a/apps/web/src/features/local-models/local-model-row.tsx b/apps/web/src/features/local-models/local-model-row.tsx index c4467cb5..a8b39522 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 { Button, buttonClassName } from "@/components/ui/button"; import { Icon, type IconSvgElement } from "@/components/ui/icon"; +import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; import { cn } from "@/lib/cn"; +import { + type AssignableLocalModelAgent, + isAgentUsingModel, + primaryLocalModelAgent, + usedByAgentNames, +} from "./local-model-agents"; import { ModelFacts } from "./local-model-details"; import { unavailableShortCopy } from "./local-models-copy"; import type { @@ -26,14 +34,13 @@ import type { LocalModelsView, } from "./local-models-queries"; import { - agentNames, collapsedRowBadges, downloadPercent, downloadStatusDetail, downloadStopped, formatGigabytes, + isModelOnComputer, placeName, - usedByBadge, type ModelEntry, type ModelStatus, } from "./local-models-rules"; @@ -154,6 +161,32 @@ function stopRowClick(event: MouseEvent) { event.stopPropagation(); } +function OpenChatLink({ + href, + onClick, +}: { + href: string; + onClick: (event: MouseEvent) => void; +}) { + const className = buttonClassName({ + className: "px-2.5", + size: "compact", + tone: "ghost", + }); + if (useInRouterContext()) { + return ( + + Open chat + + ); + } + return ( + + Open chat + + ); +} + function RowStatusLine({ detail, download, @@ -220,16 +253,16 @@ function RowStatusLine({ function RowMetadataPills({ badges, - entry, overflowTitle, status, usedBy, + usedByTitle, }: { badges: string[]; - entry: ModelEntry; overflowTitle?: string; status: ModelStatus; usedBy?: string; + usedByTitle?: string; }) { return (
{usedBy} @@ -299,17 +332,27 @@ function DownloadProgress({ * header carries the status and one action, and which expands to the details. */ export function ModelRow({ + anotherMeChatHref, + assignableAgents = [], + assigningAgentId, + computer, disabled, entry, expanded, + onAssignAgent, requested, view, ...handlers }: Handlers & { + anotherMeChatHref?: string; + assignableAgents?: readonly AssignableLocalModelAgent[]; + assigningAgentId?: string; + computer?: TeamHubLocalComputerPage["items"][number]; /** Actions are off: the Computer is offline, the viewer only reads, or one is pending. */ disabled: boolean; entry: ModelEntry; expanded: boolean; + onAssignAgent?: (agent: AssignableLocalModelAgent) => void; /** An action asked for and not yet in a snapshot. */ requested?: LocalModelAction; view: LocalModelsView; @@ -336,7 +379,26 @@ export function ModelRow({ const slower = status === "available" && !model.recommended && model.fit === "system"; const brand = familyBrand(model.family); - const usedBy = usedByBadge(entry); + const computerId = view.computer.computer_id; + const usedNames = usedByAgentNames(entry, assignableAgents, computerId); + const usedBy = + isModelOnComputer(model, status) && usedNames.length + ? usedNames.length === 1 + ? "1 Agent" + : `${usedNames.length} Agents` + : undefined; + const usedByTitle = usedNames.length + ? `Used by ${usedNames.join(", ")}` + : undefined; + const primaryAgent = primaryLocalModelAgent(assignableAgents); + const primaryUsing = primaryAgent + ? isAgentUsingModel(entry, primaryAgent, computerId) + : false; + const canUsePrimary = + status === "loaded" && + Boolean(model.pi_model) && + Boolean(primaryAgent) && + Boolean(onAssignAgent); const { badges, overflowTitle } = collapsedRowBadges(model); const hasMeta = Boolean(usedBy) || badges.length > 0; const detail = { @@ -427,10 +489,10 @@ export function ModelRow({ {hasMeta ? ( ) : null}
+ {canUsePrimary && primaryAgent && !primaryUsing && onAssignAgent ? ( + + ) : canUsePrimary && + primaryAgent?.isAnotherMe && + primaryUsing && + anotherMeChatHref ? ( + + ) : null} {action ? (