Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions apps/computer/local-daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,19 @@ model.
with that code; the model's next accepted job, or a row that moved on,
drops them. They live in memory, so after a Daemon restart the row shows
only its code.
- Pi. When the loaded models change, the Daemon asks the Runtime monitor for a
full discovery, so TeamHub sees Pi's new model list. Each model's
`pi_model` (`<provider>/<id>`, as an Agent's `runtime_model` stores it) is
passed through as `localmodels` reports it; the endpoint keeps only the
loaded engine's `base_url`.
- Pi. The Daemon sets `PI_CODING_AGENT_DIR` on `localmodels` tool calls
through `OverlayEnvironment` and `workspace.PiSourceEnvironment` so loaded
models land in the profile Pi probes and runs. When a model is loaded on a
Computer with Pi enabled and not yet installed, `internal/localmodels`
installs the pinned Pi runtime asynchronously in the background under
`HarnessRoot` (passing the bundled Node binary via `--node` when available,
matching the installer's 2 m status/plan and 20 m apply budgets, and backing
off from 30 s up to 5 m on failure while surfacing
`RUNTIME_DIAGNOSTIC_FAILED` after a failed attempt) before asking the Runtime
monitor for a full discovery, so TeamHub sees Pi become ready with the loaded
model. Each model's `pi_model` (`<provider>/<id>`, 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

Expand Down
59 changes: 52 additions & 7 deletions apps/computer/local-daemon/cmd/linger-computer-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -152,6 +157,9 @@ func run(args []string) (resultErr error) {
result, err := provider.Discover(ctx, options)
if err == nil {
result.Diagnostics = byokRuntimeCatalogs(result.Diagnostics, localmodels.ReadOverlay(localModelsStateDir))
if localModels != nil {
result.Diagnostics = withPiProvisionStatus(result.Diagnostics, localModels.PiProvisionFailed())
}
}
return result, err
}
Expand Down Expand Up @@ -313,9 +321,21 @@ func run(args []string) (resultErr error) {
}
}
jobDir := filepath.Join(computer.StateDirectory, "local-models", "jobs")
localModels := localmodels.New(localmodels.Options{
var nodePath string
if toolInstallation != nil {
nodePath = toolInstallation.NodePath
}
piEnabled := len(computer.EnabledRuntimes) == 0 || slices.Contains(computer.EnabledRuntimes, "pi")
_, hasPi := discovered.Installations["pi"]
libraryHarnessDir := ""
if piEnabled && !hasPi {
libraryHarnessDir = localmodels.HarnessRoot(localModelsStateDir)
}
localModels = localmodels.New(localmodels.Options{
Tool: localModelsTool, Environment: baseProviderEnvironment,
JobDirectory: jobDir,
JobDirectory: jobDir,
HarnessDirectory: libraryHarnessDir,
NodePath: nodePath,
// The library publishes only from Act and Refresh, which the Daemon
// calls once it runs.
Publish: func(snapshot wire.LocalModelsSnapshot) { localDaemon.PublishLocalModels(snapshot) },
Expand All @@ -336,10 +356,6 @@ func run(args []string) (resultErr error) {
}
return inventory.publish(publishCtx, nextRuntimes, next.Diagnostics)
}
var nodePath string
if toolInstallation != nil {
nodePath = toolInstallation.NodePath
}
modelSource := localmodels.NewModelSource(localmodels.ModelSourceOptions{
Tool: localModelsTool,
Environment: baseProviderEnvironment,
Expand Down Expand Up @@ -758,6 +774,35 @@ func installedTeamHubCLIBinAt(executable string) string {
return directory
}

func withManagedPiPath(paths map[string]string, managed string) map[string]string {
if managed == "" || paths["pi"] != "" {
return paths
}
cloned := maps.Clone(paths)
if cloned == nil {
cloned = make(map[string]string, 1)
}
cloned["pi"] = managed
return cloned
}

func withPiProvisionStatus(diagnostics []provider.RuntimeDiagnostic, failed bool) []provider.RuntimeDiagnostic {
if !failed {
return diagnostics
}
cloned := slices.Clone(diagnostics)
for i := range cloned {
if cloned[i].Type == "pi" && cloned[i].Status == "not_installed" {
cloned[i].Status = "error"
cloned[i].Error = &provider.DiagnosticError{
Code: "RUNTIME_DIAGNOSTIC_FAILED",
Message: "Automatic Pi runtime setup failed",
}
}
}
return cloned
}

func runtimeProviderEnvironment(source map[string]string, gatewayToken, codexAuthFile string) map[string]string {
result := computerconfig.RuntimeEnvironment(source, gatewayToken)
if codexAuthFile != "" {
Expand Down
43 changes: 43 additions & 0 deletions apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,46 @@ func TestByokRuntimeCatalogsShowTheEnteredModel(t *testing.T) {
}
}
}

func TestWithManagedPiPathRespectsConfiguredPiAndClonesSafely(t *testing.T) {
if got := withManagedPiPath(nil, ""); got != nil {
t.Fatalf("empty managed path returned %v", got)
}
configured := map[string]string{"codex": "/bin/codex", "pi": "/custom/pi"}
if got := withManagedPiPath(configured, "/managed/pi"); got["pi"] != "/custom/pi" {
t.Fatalf("managed Pi overwrote configured path: %v", got)
}
original := map[string]string{"codex": "/bin/codex"}
merged := withManagedPiPath(original, "/managed/pi")
if merged["pi"] != "/managed/pi" || original["pi"] != "" {
t.Fatalf("merged=%v original=%v", merged, original)
}
}

func TestWithPiProvisionStatusMarksNotInstalledPiAsErrorWhenFailed(t *testing.T) {
original := []provider.RuntimeDiagnostic{
{Type: "codex", Status: "not_installed", Error: &provider.DiagnosticError{Code: "RUNTIME_NOT_INSTALLED", Message: "missing"}},
{Type: "pi", Status: "not_installed", Error: &provider.DiagnosticError{Code: "RUNTIME_NOT_INSTALLED", Message: "missing"}},
{Type: "claude", Status: "ready"},
}
unchanged := withPiProvisionStatus(original, false)
if unchanged[1].Status != "not_installed" {
t.Fatalf("expected not_installed when failed=false, got %+v", unchanged[1])
}

marked := withPiProvisionStatus(original, true)
if marked[1].Status != "error" || marked[1].Error == nil || marked[1].Error.Code != "RUNTIME_DIAGNOSTIC_FAILED" {
t.Fatalf("expected Pi error status when failed=true, got %+v", marked[1])
}
if marked[0].Status != "not_installed" || marked[2].Status != "ready" {
t.Fatalf("non-Pi diagnostics changed unexpectedly: %+v", marked)
}
if original[1].Status != "not_installed" {
t.Fatalf("input diagnostics mutated in place: %+v", original[1])
}

readyPi := []provider.RuntimeDiagnostic{{Type: "pi", Status: "ready"}}
if got := withPiProvisionStatus(readyPi, true); got[0].Status != "ready" {
t.Fatalf("ready Pi overwritten: %+v", got[0])
}
}
87 changes: 84 additions & 3 deletions apps/computer/local-daemon/internal/localmodels/fake_tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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])
Expand All @@ -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
Expand Down
Loading
Loading