diff --git a/apps/computer/local-daemon/README.md b/apps/computer/local-daemon/README.md index da2de5d4..e9771a34 100644 --- a/apps/computer/local-daemon/README.md +++ b/apps/computer/local-daemon/README.md @@ -151,11 +151,16 @@ 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 + through `OverlayEnvironment` and `workspace.PiSourceEnvironment` 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 `HarnessRoot` (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..f6caa9a5 100644 --- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go +++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go @@ -139,9 +139,14 @@ func run(args []string) (resultErr error) { currentProviderEnvironment := func() map[string]string { return localmodels.OverlayEnvironment(baseProviderEnvironment, localModelsStateDir) } + var localModels *localmodels.Library currentDiscoveryOptions := func() provider.DiscoveryOptions { + var managedPi string + if localModels != nil { + managedPi = localModels.PiExecutable() + } return provider.DiscoveryOptions{ - ConfiguredPaths: localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir), + ConfiguredPaths: withManagedPiPath(localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir), managedPi), Runtimes: computer.EnabledRuntimes, PreferredRuntime: computer.PreferredRuntime, RuntimeHomeRoot: filepath.Join(computer.StateDirectory, "runtime-homes"), @@ -313,9 +318,21 @@ func run(args []string) (resultErr error) { } } jobDir := filepath.Join(computer.StateDirectory, "local-models", "jobs") - localModels := localmodels.New(localmodels.Options{ + var nodePath string + if toolInstallation != nil { + nodePath = toolInstallation.NodePath + } + piEnabled := len(computer.EnabledRuntimes) == 0 || slices.Contains(computer.EnabledRuntimes, "pi") + _, hasPi := discovered.Installations["pi"] + libraryHarnessDir := "" + if piEnabled && !hasPi { + libraryHarnessDir = localmodels.HarnessRoot(localModelsStateDir) + } + localModels = localmodels.New(localmodels.Options{ Tool: localModelsTool, Environment: baseProviderEnvironment, - JobDirectory: jobDir, + JobDirectory: jobDir, + HarnessDirectory: libraryHarnessDir, + NodePath: nodePath, // The library publishes only from Act and Refresh, which the Daemon // calls once it runs. Publish: func(snapshot wire.LocalModelsSnapshot) { localDaemon.PublishLocalModels(snapshot) }, @@ -336,10 +353,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 +771,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..a6f6f653 100644 --- a/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go +++ b/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go @@ -35,8 +35,9 @@ type fakeScript struct { } type fakeLine struct { - Line json.RawMessage `json:"line"` - DelayMS int `json:"delay_ms,omitempty"` + Line json.RawMessage `json:"line"` + DelayMS int `json:"delay_ms,omitempty"` + WaitFile string `json:"wait_file,omitempty"` // CancelAware lines stop the call with CANCELLED once a cancel ran. CancelAware bool `json:"cancel_aware,omitempty"` } @@ -46,13 +47,16 @@ func runFakeTool(args []string) int { if len(args) >= 6 && strings.Join(args[:5], " ") == "-P -s -m localmodels source" { return runFakeSourceTool(state, args[5:]) } + if len(args) >= 6 && strings.Join(args[:5], " ") == "-P -s -m localmodels harness" { + return runFakeHarness(state, args[5:]) + } if len(args) < 6 || strings.Join(args[:5], " ") != "-P -s -m localmodels library" { fmt.Fprintln(os.Stderr, "unexpected invocation", args) return 3 } verb := args[5] log, _ := os.OpenFile(filepath.Join(state, "calls.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) - _, _ = fmt.Fprintf(log, "%s|PYTHONPATH=%s|cwd=%s\n", strings.Join(args[5:], " "), os.Getenv("PYTHONPATH"), workingDirectory()) + _, _ = fmt.Fprintf(log, "%s|PYTHONPATH=%s|cwd=%s|PI_CODING_AGENT_DIR=%s\n", strings.Join(args[5:], " "), os.Getenv("PYTHONPATH"), workingDirectory(), os.Getenv("PI_CODING_AGENT_DIR")) _ = log.Close() if verb == "status" { if code, err := os.ReadFile(filepath.Join(state, "status-exit")); err == nil { @@ -80,6 +84,9 @@ func runFakeTool(args []string) int { _ = os.WriteFile(filepath.Join(state, "cancelled"), nil, 0o600) } for _, line := range script.Lines { + if line.WaitFile != "" { + waitForGateRemoval(filepath.Join(state, line.WaitFile), 30*time.Second) + } time.Sleep(time.Duration(line.DelayMS) * time.Millisecond) if _, err := os.Stat(filepath.Join(state, "cancelled")); line.CancelAware && err == nil { fmt.Printf(`{"schema":"localmodels.library-event/v1","event":"error","ok":false,"code":"CANCELLED","detail":"the download was cancelled","model":%q}`+"\n", args[6]) @@ -93,11 +100,85 @@ func runFakeTool(args []string) int { return script.Exit } +func waitForGateRemoval(gatePath string, timeout time.Duration) { + deadline := time.Now().Add(timeout) + for time.Now().Before(deadline) { + if _, err := os.Stat(gatePath); err != nil { + return + } + time.Sleep(10 * time.Millisecond) + } +} + func workingDirectory() string { directory, _ := os.Getwd() return directory } +func runFakeHarness(state string, args []string) int { + if len(args) < 1 { + return 3 + } + verb := args[0] + log, _ := os.OpenFile(filepath.Join(state, "calls.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) + _, _ = fmt.Fprintf(log, "harness %s|PI_CODING_AGENT_DIR=%s\n", strings.Join(args, " "), os.Getenv("PI_CODING_AGENT_DIR")) + _ = log.Close() + argValue := func(flag string) string { + for index := 0; index+1 < len(args); index++ { + if args[index] == flag { + return args[index+1] + } + } + return "" + } + root := argValue("--root") + installedPath := filepath.Join(root, "tools", "pi", "1.3.0", "pi") + statusJSON := func(installed bool) string { + if installed { + return fmt.Sprintf(`{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":true,"executable":%q,"argvPrefix":[]}]}`, installedPath) + } + return `{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":false,"executable":null,"argvPrefix":[]}]}` + } + switch verb { + case "status": + if _, err := os.Stat(filepath.Join(state, "harness-status-fail")); err == nil { + return 1 + } + _, err := os.Stat(filepath.Join(state, "harness-pi-installed")) + fmt.Println(statusJSON(err == nil)) + return 0 + case "plan": + waitGate := filepath.Join(state, "harness-plan-wait") + if _, err := os.Stat(waitGate); err == nil { + _ = os.WriteFile(filepath.Join(state, "harness-plan-entered"), []byte("1"), 0o600) + waitForGateRemoval(waitGate, 30*time.Second) + } + if _, err := os.Stat(filepath.Join(state, "harness-plan-fail")); err == nil { + return 1 + } + outPath := argValue("--out") + plan := fmt.Sprintf(`{"schema":"localmodels.harness-plan/v1","id":%q,"runtime":"pi","root":%q}`, strings.Repeat("a", 64), root) + if outPath != "" { + _ = os.WriteFile(outPath, []byte(plan), 0o600) + } + fmt.Println(plan) + return 0 + case "apply": + if _, err := os.Stat(filepath.Join(state, "harness-apply-fail")); err == nil { + return 1 + } + if argValue("--approve") != strings.Repeat("a", 64) { + return 2 + } + _ = os.WriteFile(filepath.Join(state, "harness-pi-installed"), []byte(installedPath), 0o600) + fmt.Println(`{"kind":"stage","stage":"install","state":"running"}`) + fmt.Println(statusJSON(true)) + return 0 + default: + return 3 + } +} + // fakeTool lays out a Tool that runs the fake. type fakeTool struct { t *testing.T diff --git a/apps/computer/local-daemon/internal/localmodels/library.go b/apps/computer/local-daemon/internal/localmodels/library.go index 58aee26a..678ee573 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,11 @@ type Options struct { Environment map[string]string // JobDirectory holds the output file of each running download or load. JobDirectory string + // HarnessDirectory holds the on-demand Pi runtime provisioned when a + // local model is loaded on a Computer without Pi. + HarnessDirectory string + // NodePath is an optional local Node executable passed via --node to harness plan. + NodePath string // Publish receives every new snapshot, in order. It is called only from // Act and Refresh and the jobs they start. Publish func(wire.LocalModelsSnapshot) @@ -47,9 +55,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 +81,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 +131,10 @@ func New(options Options) *Library { ctx, stop := context.WithCancel(context.Background()) library := &Library{ tool: options.Tool, environment: options.Environment, jobDirectory: options.JobDirectory, + harnessDirectory: options.HarnessDirectory, nodePath: options.NodePath, publish: options.Publish, runtimeChanged: options.RuntimeChanged, progressInterval: options.ProgressInterval, statusTimeout: options.StatusTimeout, actionTimeout: options.ActionTimeout, + ensurePiTimeout: options.EnsurePiTimeout, ensurePiRetryInterval: options.EnsurePiRetryInterval, now: time.Now, ctx: ctx, stop: stop, jobs: make(map[string]*job), } if library.progressInterval <= 0 { @@ -124,6 +146,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 +169,82 @@ 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< 0 { // Local diagnostics only: tool prose can name local paths. - if diagnostic, readErr := readBounded(stderr.Name(), 4096); readErr == nil && len(diagnostic) > 0 { - slog.Debug("local_models.tool_stderr", "action", action.Name, "stderr", string(diagnostic)) - } + slog.Debug("local_models.tool_stderr", "action", action.Name, "stderr", string(stderrBytes)) } return output, exitCode, err } diff --git a/apps/computer/local-daemon/internal/localmodels/pi_runtime.go b/apps/computer/local-daemon/internal/localmodels/pi_runtime.go new file mode 100644 index 00000000..e25e7587 --- /dev/null +++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime.go @@ -0,0 +1,172 @@ +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" +) + +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, stderrBytes, exitCode, err := t.runBuffered(ctx, environment, "", "localmodels-harness", maxHarnessOutput, args, nil) + if exitCode != 0 && len(stderrBytes) > 0 { + slog.Debug("local_models.harness_stderr", "stderr", string(stderrBytes)) + } + return stdout, exitCode, err +} + +func parseLastHarnessStatus(stdout []byte) (string, error) { + lines := bytes.Split(stdout, []byte{'\n'}) + for index := len(lines) - 1; index >= 0; index-- { + line := bytes.TrimSpace(lines[index]) + if len(line) == 0 { + continue + } + return parsePiFromHarnessStatus(line) + } + return "", errors.New("harness output is empty") +} + +func parsePiFromHarnessStatus(encoded []byte) (string, error) { + var doc harnessStatusDoc + if err := json.Unmarshal(bytes.TrimSpace(encoded), &doc); err != nil { + return "", fmt.Errorf("decode harness status: %w", err) + } + if doc.Schema != harnessStatusSchema { + return "", fmt.Errorf("harness status schema is %q", doc.Schema) + } + for _, row := range doc.Runtimes { + if row.Runtime != "pi" { + continue + } + if !row.Installed || len(row.ArgvPrefix) != 0 || !filepath.IsAbs(row.Executable) { + return "", nil + } + return row.Executable, nil + } + return "", nil +} diff --git a/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go new file mode 100644 index 00000000..2c21d370 --- /dev/null +++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go @@ -0,0 +1,366 @@ +package localmodels + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "slices" + "strings" + "sync" + "testing" + "time" +) + +func TestToolCommandAlignsPiCodingAgentDir(t *testing.T) { + fake := newFakeTool(t) + home := filepath.Join(filepath.Dir(fake.state), "home") + explicit := filepath.Join(filepath.Dir(fake.state), "custom-pi") + + cmdDefault := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": ""}, Action{Name: ActionRefresh}) + wantDefault := "PI_CODING_AGENT_DIR=" + filepath.Join(home, ".pi", "agent") + if !slices.Contains(cmdDefault.Env, wantDefault) { + t.Fatalf("expected %q in env %v", wantDefault, cmdDefault.Env) + } + + cmdExplicit := fake.tool.command(map[string]string{"HOME": home, "pi_coding_agent_dir": explicit}, Action{Name: ActionRefresh}) + wantExplicit := "PI_CODING_AGENT_DIR=" + explicit + if !slices.Contains(cmdExplicit.Env, wantExplicit) { + t.Fatalf("expected %q in env %v", wantExplicit, cmdExplicit.Env) + } + + gatewayPi := filepath.Join(fake.tool.StateDirectory, "gateway-home", ".pi", "agent") + if err := os.MkdirAll(gatewayPi, 0o700); err != nil { + t.Fatal(err) + } + writeModelSourceJSON(t, fake.tool.StateDirectory, map[string]any{ + "schema": modelSourceSchema, + "mode": "local", + "runtime": "pi", + }) + cmdOverlay := fake.tool.command(map[string]string{"HOME": home}, Action{Name: ActionRefresh}) + wantOverlay := "PI_CODING_AGENT_DIR=" + gatewayPi + if !slices.Contains(cmdOverlay.Env, wantOverlay) { + t.Fatalf("expected local overlay %q in env %v", wantOverlay, cmdOverlay.Env) + } + + // Local model-source overlay beats an explicit PI_CODING_AGENT_DIR in the + // base environment, matching OverlayEnvironment. + cmdExplicitAndOverlay := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": explicit}, Action{Name: ActionRefresh}) + if !slices.Contains(cmdExplicitAndOverlay.Env, wantOverlay) { + t.Fatalf("expected local overlay %q to override explicit base env %v", wantOverlay, cmdExplicitAndOverlay.Env) + } + + // Switching away from local strips a managed gateway-home Pi dir and falls + // back to the source Pi profile (~/.pi/agent). + writeModelSourceJSON(t, fake.tool.StateDirectory, map[string]any{ + "schema": modelSourceSchema, + "mode": "subscription", + "runtime": "claude", + }) + cmdSubscription := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": gatewayPi}, Action{Name: ActionRefresh}) + if !slices.Contains(cmdSubscription.Env, wantDefault) { + t.Fatalf("expected stale gateway path stripped back to %q in %v", wantDefault, cmdSubscription.Env) + } +} + +func TestLoadProvisionsPiInHarnessDirectoryWithNodePathAndNotifiesRuntimeChanged(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "idle"))) + loaded, _ := json.Marshal(status(model(testModel, "loaded"))) + fake.script("load", fakeScript{ + Lines: []fakeLine{ + {Line: progressLine(testModel, "load", 0, 1)}, + {Line: resultLine(testModel), DelayMS: 20}, + }, + StatusAfter: loaded, + }) + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + home := filepath.Join(filepath.Dir(fake.state), "home") + nodePath := filepath.Join(t.TempDir(), "node") + if err := os.WriteFile(nodePath, []byte("#!/bin/sh\n"), 0o700); err != nil { + t.Fatal(err) + } + record := &recorder{} + var seenMu sync.Mutex + var seenPiAtChange string + library := New(Options{ + Tool: &fake.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": home, "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + NodePath: nodePath, + Publish: record.publish, + ProgressInterval: 20 * time.Millisecond, + }) + library.runtimeChanged = func() { + if executable := library.PiExecutable(); executable != "" { + seenMu.Lock() + seenPiAtChange = executable + seenMu.Unlock() + } + record.runtimeChanged() + } + t.Cleanup(library.Close) + + library.Refresh(context.Background()) + if library.PiExecutable() != "" { + t.Fatalf("idle library unexpectedly provisioned Pi: %q", library.PiExecutable()) + } + + if refusal, answered := library.Act(context.Background(), Action{Name: ActionLoad, Model: testModel}, soon()); refusal != nil || !answered { + t.Fatalf("load refusal=%+v answered=%v", refusal, answered) + } + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "Pi provisioned and runtimeChanged fired", func() bool { + seenMu.Lock() + seen := seenPiAtChange + seenMu.Unlock() + return record.changes() > 0 && library.PiExecutable() == want && seen == want + }) + calls := fake.calls() + if !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness status ") }) || + !slices.ContainsFunc(calls, func(c string) bool { + return strings.HasPrefix(c, "harness plan ") && strings.Contains(c, "--node "+nodePath) + }) || + !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness apply ") }) { + t.Fatalf("expected harness status/plan(--node)/apply in calls: %v", calls) + } + wantPiDir := "PI_CODING_AGENT_DIR=" + filepath.Join(home, ".pi", "agent") + if !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "load ") && strings.Contains(c, wantPiDir) }) { + t.Fatalf("expected load call with %s in %v", wantPiDir, calls) + } +} + +func TestInitialRefreshProvisionsPiAndInspectPiFindsExisting(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "loaded"))) + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + record := &recorder{} + env := map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")} + library := New(Options{ + Tool: &fake.tool, + Environment: env, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + Publish: record.publish, + RuntimeChanged: record.runtimeChanged, + ProgressInterval: time.Second, + }) + t.Cleanup(library.Close) + + library.Refresh(context.Background()) + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "initial loaded model provisions Pi asynchronously", func() bool { + return library.PiExecutable() == want && record.changes() == 1 + }) + + inspected, err := fake.tool.InspectPi(context.Background(), env, harnessDir) + if err != nil { + t.Fatalf("InspectPi error: %v", err) + } + if inspected != want { + t.Fatalf("InspectPi=%q want=%q", inspected, want) + } + + planCount := func() int { + count := 0 + for _, call := range fake.calls() { + if strings.HasPrefix(call, "harness plan ") { + count++ + } + } + return count + } + beforePlans := planCount() + + reusedRecord := &recorder{} + reused := New(Options{ + Tool: &fake.tool, + Environment: env, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + Publish: reusedRecord.publish, + RuntimeChanged: reusedRecord.runtimeChanged, + ProgressInterval: time.Second, + }) + t.Cleanup(reused.Close) + reused.Refresh(context.Background()) + waitFor(t, "existing Pi resolved via InspectPi without replanning", func() bool { + return reused.PiExecutable() == want && reusedRecord.changes() == 1 + }) + if afterPlans := planCount(); afterPlans != beforePlans { + t.Fatalf("expected InspectPi to reuse existing Pi without harness plan (%d -> %d)", beforePlans, afterPlans) + } +} + +func TestFailedPiProvisioningDoesNotFailLoadedSnapshot(t *testing.T) { + fake := newFakeTool(t) + fake.write("status.json", status(model(testModel, "loaded"))) + if err := os.WriteFile(filepath.Join(fake.state, "harness-plan-fail"), []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir := filepath.Join(filepath.Dir(fake.state), "harness") + library := New(Options{ + Tool: &fake.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir, + EnsurePiRetryInterval: time.Minute, + }) + t.Cleanup(library.Close) + + snapshot := library.Refresh(context.Background()) + if !snapshot.Available || modelState(snapshot, testModel).State != "loaded" { + t.Fatalf("snapshot=%+v", snapshot) + } + waitFor(t, "failed Pi attempt recorded", func() bool { + 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"))) + downloadWait := filepath.Join(fake.state, "download-wait") + if err := os.WriteFile(downloadWait, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + fake.script("download", fakeScript{ + Lines: []fakeLine{ + {Line: progressLine(otherModel, "download", 10, 100)}, + {Line: resultLine(otherModel), WaitFile: "download-wait"}, + }, + StatusAfter: downloaded, + }) + if refusal, answered := library.Act(context.Background(), Action{Name: ActionDownload, Model: otherModel}, soon()); refusal != nil || !answered { + t.Fatalf("download refusal=%+v answered=%v", refusal, answered) + } + waitFor(t, "phaseStarted published downloading snapshot while EnsurePi waits", func() bool { + state := modelState(record.last(), otherModel) + return state != nil && state.State == "downloading" + }) + if got := library.PiExecutable(); got != "" { + t.Fatalf("EnsurePi finished before waitGate was released: %q", got) + } + + if err := os.Remove(downloadWait); err != nil { + t.Fatal(err) + } + if err := os.Remove(waitGate); err != nil { + t.Fatal(err) + } + want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi") + waitFor(t, "blocked EnsurePi finished after gate released", func() bool { + return library.PiExecutable() == want && record.changes() == 1 + }) + waitFor(t, "gated download completed after gate released", func() bool { + state := modelState(record.last(), otherModel) + return state != nil && state.State == "idle" + }) + + // Failed installs back off instead of retrying on every Refresh, then retry + // once the backoff window elapses. + fake2 := newFakeTool(t) + fake2.write("status.json", status(model(testModel, "loaded"))) + failMarker := filepath.Join(fake2.state, "harness-plan-fail") + if err := os.WriteFile(failMarker, []byte("1"), 0o600); err != nil { + t.Fatal(err) + } + harnessDir2 := filepath.Join(filepath.Dir(fake2.state), "harness") + record2 := &recorder{} + var clockMu sync.Mutex + currentTime := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC) + library2 := New(Options{ + Tool: &fake2.tool, + Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")}, + JobDirectory: filepath.Join(t.TempDir(), "jobs"), + HarnessDirectory: harnessDir2, + Publish: record2.publish, + RuntimeChanged: record2.runtimeChanged, + EnsurePiRetryInterval: 30 * time.Second, + }) + library2.now = func() time.Time { + clockMu.Lock() + defer clockMu.Unlock() + return currentTime + } + t.Cleanup(library2.Close) + + library2.Refresh(context.Background()) + waitFor(t, "initial Pi install failure recorded", func() bool { + 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/source.go b/apps/computer/local-daemon/internal/localmodels/source.go index 00c45356..e6ba4d4c 100644 --- a/apps/computer/local-daemon/internal/localmodels/source.go +++ b/apps/computer/local-daemon/internal/localmodels/source.go @@ -1,7 +1,6 @@ package localmodels import ( - "bytes" "context" "encoding/json" "errors" @@ -15,7 +14,6 @@ import ( "sync" "time" - "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/process" localworkspace "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace" wire "github.com/simplexai-labs/linger/contracts-go/runnergateway" ) @@ -192,8 +190,8 @@ func OverlayRuntimePaths(configured map[string]string, stateDir string) map[stri } } } - if stateDir != "" { - binDir := filepath.Clean(filepath.Join(stateDir, "..", "..", "harness", "bin")) + if harness := HarnessRoot(stateDir); harness != "" { + binDir := filepath.Join(harness, "bin") for _, rt := range []string{"claude", "codex", "pi"} { if result[rt] != "" { continue @@ -251,8 +249,8 @@ type ModelSource struct { // NewModelSource constructs a ModelSource controller for the given localmodels Tool. func NewModelSource(options ModelSourceOptions) *ModelSource { harnessRoot := options.HarnessRoot - if harnessRoot == "" && options.Tool != nil && options.Tool.StateDirectory != "" { - harnessRoot = filepath.Clean(filepath.Join(options.Tool.StateDirectory, "..", "..", "harness")) + if harnessRoot == "" && options.Tool != nil { + harnessRoot = HarnessRoot(options.Tool.StateDirectory) } statusTimeout := options.StatusTimeout if statusTimeout <= 0 { @@ -660,53 +658,10 @@ func (m *ModelSource) runTool(ctx context.Context, label string, args []string, dir := m.jobDirectory if dir == "" { dir = os.TempDir() - } else if err := os.MkdirAll(dir, 0o700); err != nil { - return nil, -1, err } - stdout, err := os.CreateTemp(dir, "model-source-*.out") - if err != nil { - return nil, -1, err - } - defer func() { _ = os.Remove(stdout.Name()) }() - - cmd := exec.Command(m.tool.Python, args...) - cmd.Dir = m.tool.ModuleDirectory - values := make(map[string]string, len(m.environment)+4) - for key, value := range m.environment { - switch strings.ToUpper(key) { - case "PYTHONHOME", "PYTHONSTARTUP", "PYTHONINSPECT", "PYTHONPATH", "LOCALMODELS_STATE": - continue - } - values[key] = value - } - values["PYTHONPATH"] = m.tool.ModuleDirectory - values["LOCALMODELS_STATE"] = m.tool.StateDirectory - values["PYTHONNOUSERSITE"] = "1" - values["PYTHONDONTWRITEBYTECODE"] = "1" - cmd.Env = make([]string, 0, len(values)) - for key, value := range values { - cmd.Env = append(cmd.Env, key+"="+value) + output, _, exitCode, err := m.tool.runBuffered(ctx, m.environment, dir, "model-source", maxEventLine, args, stdin) + if err != nil && ctx.Err() != nil { + return nil, -1, errors.Join(fmt.Errorf("%s cancelled", label), err) } - if len(stdin) > 0 { - cmd.Stdin = bytes.NewReader(stdin) - } - cmd.Stdout = stdout - process.Prepare(cmd) - startErr := cmd.Start() - _ = stdout.Close() - if startErr != nil { - return nil, -1, startErr - } - exited := make(chan error, 1) - go func() { exited <- cmd.Wait() }() - select { - case <-exited: - case <-ctx.Done(): - _ = cmd.Process.Kill() - <-exited - return nil, -1, errors.Join(fmt.Errorf("%s cancelled", label), context.Cause(ctx)) - } - exitCode := cmd.ProcessState.ExitCode() - output, readErr := readBounded(stdout.Name(), maxEventLine) - return output, exitCode, readErr + return output, exitCode, err } diff --git a/apps/computer/local-daemon/internal/localmodels/tool.go b/apps/computer/local-daemon/internal/localmodels/tool.go index 3f938d9b..4c579aaa 100644 --- a/apps/computer/local-daemon/internal/localmodels/tool.go +++ b/apps/computer/local-daemon/internal/localmodels/tool.go @@ -6,11 +6,26 @@ package localmodels import ( + "bytes" + "context" "os" "os/exec" + "path/filepath" "strings" + + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/process" + "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace" ) +// HarnessRoot returns the shared runtime/harness directory sibling to a +// localmodels StateDirectory, or an empty string when stateDir is unset. +func HarnessRoot(stateDir string) string { + if stateDir == "" { + return "" + } + return filepath.Clean(filepath.Join(stateDir, "..", "..", "harness")) +} + // Tool is where the installer put the localmodels tool and its state, as // the Computer config records it. type Tool struct { @@ -58,31 +73,107 @@ func (t Tool) arguments(action Action) []string { } } -// command builds one tool invocation the way the installer runs it: the -// sealed kit on PYTHONPATH and as the working directory, no user site -// packages, no bytecode writes into the sealed kit. +// command builds one library invocation for the tool. func (t Tool) command(environment map[string]string, action Action) *exec.Cmd { - cmd := exec.Command(t.Python, t.arguments(action)...) + return t.commandArgs(environment, t.arguments(action)) +} + +// commandArgs builds one tool invocation the way the installer runs it: the +// sealed kit on PYTHONPATH and as the working directory, no user site +// packages, no bytecode writes into the sealed kit, and PI_CODING_AGENT_DIR +// resolved through OverlayEnvironment and workspace.PiSourceEnvironment so +// localmodels reads and writes the same Pi profile the Daemon probes and runs. +func (t Tool) commandArgs(environment map[string]string, args []string) *exec.Cmd { + cmd := exec.Command(t.Python, args...) cmd.Dir = t.ModuleDirectory - values := make(map[string]string, len(environment)+4) + cleaned := make(map[string]string, len(environment)) for key, value := range environment { switch strings.ToUpper(key) { case "PYTHONHOME", "PYTHONSTARTUP", "PYTHONINSPECT", "PYTHONPATH", "LOCALMODELS_STATE": continue + case "PI_CODING_AGENT_DIR": + if trimmed := strings.TrimSpace(value); trimmed != "" { + cleaned["PI_CODING_AGENT_DIR"] = trimmed + } + continue } - values[key] = value + cleaned[key] = value } - values["PYTHONPATH"] = t.ModuleDirectory - values["LOCALMODELS_STATE"] = t.StateDirectory - values["PYTHONNOUSERSITE"] = "1" - values["PYTHONDONTWRITEBYTECODE"] = "1" - cmd.Env = make([]string, 0, len(values)) - for key, value := range values { + effective := OverlayEnvironment(cleaned, t.StateDirectory) + if piEnv, err := workspace.PiSourceEnvironment(effective); err == nil && piEnv["PI_CODING_AGENT_DIR"] != "" { + effective["PI_CODING_AGENT_DIR"] = piEnv["PI_CODING_AGENT_DIR"] + } + effective["PYTHONPATH"] = t.ModuleDirectory + effective["LOCALMODELS_STATE"] = t.StateDirectory + effective["PYTHONNOUSERSITE"] = "1" + effective["PYTHONDONTWRITEBYTECODE"] = "1" + cmd.Env = make([]string, 0, len(effective)) + for key, value := range effective { cmd.Env = append(cmd.Env, key+"="+value) } return cmd } +// runBuffered executes localmodels with stdout and stderr redirected to +// temporary files (never pipes) so any background gateway child spawned by +// localmodels survives after the CLI process exits. +func (t Tool) runBuffered( + ctx context.Context, + environment map[string]string, + tempDir, prefix string, + maxBytes int64, + args []string, + stdin []byte, +) (stdoutBytes, stderrBytes []byte, exitCode int, err error) { + if tempDir != "" { + if mkErr := os.MkdirAll(tempDir, 0o700); mkErr != nil { + return nil, nil, -1, mkErr + } + } + stdout, err := os.CreateTemp(tempDir, prefix+"-*.out") + if err != nil { + return nil, nil, -1, err + } + defer func() { _ = os.Remove(stdout.Name()) }() + stderr, err := os.CreateTemp(tempDir, prefix+"-*.err") + if err != nil { + _ = stdout.Close() + return nil, nil, -1, err + } + defer func() { _ = os.Remove(stderr.Name()) }() + + cmd := t.commandArgs(environment, args) + if len(stdin) > 0 { + cmd.Stdin = bytes.NewReader(stdin) + } + cmd.Stdout, cmd.Stderr = stdout, stderr + process.Prepare(cmd) + startErr := cmd.Start() + _ = stdout.Close() + _ = stderr.Close() + if startErr != nil { + return nil, nil, -1, startErr + } + exited := make(chan error, 1) + go func() { exited <- cmd.Wait() }() + select { + case <-exited: + case <-ctx.Done(): + _ = cmd.Process.Kill() + <-exited + return nil, nil, -1, context.Cause(ctx) + } + code := cmd.ProcessState.ExitCode() + out, readErr := readBounded(stdout.Name(), maxBytes) + var errOut []byte + if code != 0 { + if diagnostic, diagErr := readBounded(stderr.Name(), 4096); diagErr == nil { + errOut = diagnostic + } + } + return out, errOut, code, readErr +} + // installed reports whether the configured interpreter still exists. func (t Tool) installed() bool { info, err := os.Stat(t.Python) diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md index 2a43eafc..ade20156 100644 --- a/apps/web/AGENTS.md +++ b/apps/web/AGENTS.md @@ -39,9 +39,13 @@ build:assets` to check the initial JS budget. loaded row, renders tinted family icon tiles, Agent usage counts, structured capability/parameter badges (never matched against `name` or `description`), state-distinct cards (`loaded`, - `recommended`, `failed`, `unavailable`), download speed/ETA, and top + `recommended`, `failed`, `unavailable`), download speed/ETA, live Bifrost + (`Pi`) `Gateway` readiness, and top diagnostic callouts with a two-column model-card facts grid from structured - `LocalModel` fields in `local-models-rules.ts`, and maps every code and enum + `LocalModel` fields in `local-models-rules.ts`, resolves Sponsor-owned Agent + eligibility and `Pi` runtime switch payloads in `local-model-agents.ts` + (`Use with `, `Open chat`, `Switch Agent…` in `Used by`, and + `Make room` automatic Agent handoff once the replacement model loads), and maps every code and enum label to English in `local-models-copy.ts`. Without `?computer=` the desktop app opens its own Computer. Only the Computer's owner can act; actions return at once diff --git a/apps/web/src/features/local-models/local-model-agents.test.ts b/apps/web/src/features/local-models/local-model-agents.test.ts new file mode 100644 index 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 61f276f8..f79a4e4c 100644 --- a/apps/web/src/features/local-models/local-model-details.tsx +++ b/apps/web/src/features/local-models/local-model-details.tsx @@ -9,8 +9,16 @@ import { Button } from "@/components/ui/button"; import { FormFeedback } from "@/components/ui/form-feedback"; import { Icon } from "@/components/ui/icon"; import { ModalDialog } from "@/components/ui/modal-dialog"; +import { Select, SelectItem } from "@/components/ui/select"; +import type { TeamHubLocalComputerPage } from "@/features/teamhub/teamhub-types"; import { cn } from "@/lib/cn"; +import { + type AssignableLocalModelAgent, + isAgentUsingModel, + primaryLocalModelAgent, + usedByAgentNames, +} from "./local-model-agents"; import { capabilityCopy, modelErrorCopy, @@ -19,7 +27,6 @@ import { } from "./local-models-copy"; import type { LocalModelPool, LocalModelsView } from "./local-models-queries"; import { - agentNames, contextLabel, downloadStopped, formatGigabytes, @@ -91,12 +98,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; @@ -117,6 +163,24 @@ export function ModelFacts({ pool !== undefined && poolOtherBytes(pool) > 0 && poolFreeBytes(pool) < model.memory_bytes; + const computerId = view.computer.computer_id; + const usedNames = usedByAgentNames(entry, assignableAgents, computerId); + const unassigned = assignableAgents.filter( + (agent) => !isAgentUsingModel(entry, agent, computerId), + ); + const primaryAgent = primaryLocalModelAgent(assignableAgents); + const showAgentPicker = + status === "loaded" && + Boolean(model.pi_model) && + Boolean(onAssignAgent) && + (unassigned.some((agent) => !agent.isAnotherMe) || unassigned.length > 1); + const primaryBusy = + status === "loaded" && + Boolean(model.pi_model) && + Boolean(onAssignAgent) && + primaryAgent !== undefined && + !isAgentUsingModel(entry, primaryAgent, computerId) && + primaryAgent.busy; return (
{model.error || status === "unavailable" ? ( @@ -199,10 +263,47 @@ export function ModelFacts({ {model.license ? {model.license} : null} {onComputer ? ( 1 ? "@xl:col-span-2" : undefined} + className={ + usedNames.length > 1 || showAgentPicker || primaryBusy + ? "@xl:col-span-2" + : undefined + } label="Used by" > - {entry.used_by.length ? agentNames(entry) : "No Agent yet"} + + + {usedNames.length ? usedNames.join(", ") : "No Agent yet"} + + {showAgentPicker && onAssignAgent ? ( + + ) : primaryBusy ? ( + + Finish the current turn to switch + + ) : null} + ) : null} {sourceUrl ? ( @@ -218,6 +319,11 @@ export function ModelFacts({ ) : null} + {status === "loaded" && model.endpoint ? ( + + {gatewayFactValue(entry, view, computer)} + + ) : null} {model.endpoint ? ( void; - onConfirm: (unloadIds: string[]) => void; + onConfirm: (unloadIds: string[], switchAgentIds: string[]) => void; onRecheck?: () => void; rechecking?: boolean; view: LocalModelsView; }) { const [selected, setSelected] = useState(() => makeRoomSelection(check)); + const [switchAgents, setSwitchAgents] = useState(true); const roomFreeNow = check.freeBytes >= check.needBytes; const effectiveSelected = roomFreeNow ? [] : selected; const chosen = new Set(effectiveSelected); @@ -68,9 +75,17 @@ export function MakeRoomDialog({ const unifiedFitsHardware = check.pool.kind === "unified" && (view.computer.host?.ram_total_bytes ?? 0) >= check.needBytes; - 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 f3b7405e..ec62226c 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"; @@ -155,6 +162,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, @@ -221,16 +254,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} @@ -300,18 +333,28 @@ function DownloadProgress({ * header carries the status and one action, and which expands to the details. */ export function ModelRow({ + anotherMeChatHref, + assignableAgents = [], + assigningAgentId, + computer, disabled, entry, expanded, + onAssignAgent, requested, switchingForAgents = false, view, ...handlers }: Handlers & { + anotherMeChatHref?: string; + assignableAgents?: readonly AssignableLocalModelAgent[]; + assigningAgentId?: string; + computer?: TeamHubLocalComputerPage["items"][number]; /** Actions are off: the Computer is offline, the viewer only reads, or one is pending. */ disabled: boolean; entry: ModelEntry; expanded: boolean; + onAssignAgent?: (agent: AssignableLocalModelAgent) => void; /** An action asked for and not yet in a snapshot. */ requested?: LocalModelAction; switchingForAgents?: boolean; @@ -339,7 +382,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 = { @@ -430,10 +492,10 @@ export function ModelRow({ {hasMeta ? ( ) : null}
Use for Agents + ) : canUsePrimary && + primaryAgent && + !primaryUsing && + onAssignAgent ? ( + + ) : !handlers.onUseForAgents && + canUsePrimary && + primaryAgent?.isAnotherMe && + primaryUsing && + anotherMeChatHref ? ( + ) : null} {action ? (