Skip to content
Closed
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
15 changes: 10 additions & 5 deletions apps/computer/local-daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` (`<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 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` (`<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
39 changes: 32 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 Down Expand Up @@ -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) },
Expand All @@ -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,
Expand Down Expand Up @@ -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 != "" {
Expand Down
15 changes: 15 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,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)
}
}
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