diff --git a/docs/notes/stage-execution-service-export.md b/docs/notes/stage-execution-service-export.md index 6ae7c5fe..2999c35e 100644 --- a/docs/notes/stage-execution-service-export.md +++ b/docs/notes/stage-execution-service-export.md @@ -1,8 +1,18 @@ # Stage export uses the execution service -V2 stage admission reads the existing local CLI service registration and pins -every `opencode api` subprocess with `--server` and private inherited auth. -Missing registration fails closed; stage execution does not provision a service. +V2 stage admission reads the local CLI service registration and pins every +session `opencode api` subprocess with `--server` and private inherited auth. +When registration is missing, only the admission path (`OpenStage`) first runs +the selected CLI's `api get /api/info` in the stage directory. This uses normal +CLI discovery/start of its managed service without creating a session or making +a provider request. The [V2 API guide](https://opencode.ai/v2/docs/api) documents +this discovery behavior and the [CLI guide](https://opencode.ai/v2/docs/cli) +documents the shared background service default. +Admission then requires the existing private registration before session work; +CLI success without registration is not enough. Invalid or insecure existing +registration fails closed without running discovery. Export, repair, evidence, +and restart interruption use read-only `Open`/reference resolution, never this +admission-only helper, and never start a service when registration is missing. The chat broker's `PIPELINES_OPENCODE_ENDPOINT` and password-file overrides do not select stage execution or export. @@ -20,4 +30,8 @@ remains failed/pending evidence rather than being recreated or replayed. Isolated HTTP A/B tests cover endpoint separation, authenticated export, reference recovery, wrong credentials and changed endpoints. Fake-CLI tests -cover matching endpoint/auth/directory and ownership before prompt admission. +cover matching endpoint/auth/directory and ownership before prompt admission, +cold registration discovery, refusal when discovery leaves no private +registration, and no discovery from retained-session read paths. +Reference-writing fixtures set both scratch `HOME` and `XDG_CACHE_HOME`, since +macOS's `os.UserCacheDir` uses `HOME/Library/Caches` regardless of XDG overrides. diff --git a/internal/cli/provider_check_test.go b/internal/cli/provider_check_test.go index da527975..75b6e378 100644 --- a/internal/cli/provider_check_test.go +++ b/internal/cli/provider_check_test.go @@ -46,6 +46,7 @@ esac func fakeNativeService(t *testing.T) { t.Helper() + t.Setenv("HOME", t.TempDir()) state := t.TempDir() t.Setenv("XDG_STATE_HOME", state) t.Setenv("XDG_CACHE_HOME", t.TempDir()) diff --git a/internal/opencode/execution_service_test.go b/internal/opencode/execution_service_test.go index ffb015fd..55e80342 100644 --- a/internal/opencode/execution_service_test.go +++ b/internal/opencode/execution_service_test.go @@ -8,11 +8,13 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "testing" ) func TestExecutionServiceReferencePinsIdentityAndAuth(t *testing.T) { + t.Setenv("HOME", t.TempDir()) c, calls := exportAPI(t, `{"data":{"info":{"id":"ses_ok"},"messages":[{"type":"user","text":"execution A"}]}}`, 200) chatCalls := 0 chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -50,6 +52,9 @@ func TestExecutionServiceReferencePinsIdentityAndAuth(t *testing.T) { if err != nil { t.Fatal(err) } + if runtime.GOOS == "darwin" && !strings.HasPrefix(ref, filepath.Join(os.Getenv("HOME"), "Library", "Caches")+string(filepath.Separator)) { + t.Fatal("reference escaped scratch macOS HOME cache") + } data, _ := os.ReadFile(ref) if strings.Contains(string(data), c.password) || strings.Contains(string(data), `"password"`) { t.Fatal("credential copied into reference") diff --git a/internal/opencode/stage_service.go b/internal/opencode/stage_service.go new file mode 100644 index 00000000..55b530b7 --- /dev/null +++ b/internal/opencode/stage_service.go @@ -0,0 +1,58 @@ +package opencode + +import ( + "context" + "fmt" + "os" +) + +// OpenStage is the admission-only connection path. Unlike retained-session +// reads and interruption, a new stage may discover/start the CLI-managed V2 +// service before creating any session. Chat endpoint overrides are not involved. +func OpenStage(ctx context.Context, executable, directory, backend string) (*Connection, error) { + c, err := newConnection(ctx, executable, directory, backend) + if err != nil { + return nil, err + } + if c.Backend != "v2" { + return Open(ctx, c.Executable, c.Directory, c.Backend) + } + return ensureStageService(ctx, c.Executable, c.Directory) +} + +// ensureStageService delegates cold-start discovery to the selected CLI's +// authoritative `api get /api/info` transport. Only a missing registration +// permits discovery: invalid/private-file failures must not be healed by +// overwriting security-sensitive state. Existing registrations stay pinned. +// Never call this from export, repair, evidence, or restart interruption. +func ensureStageService(ctx context.Context, executable, directory string) (*Connection, error) { + path, err := ServiceCredentialPath() + if err != nil { + return nil, err + } + if _, err := os.Lstat(path); os.IsNotExist(err) { + cmd := CommandContext(ctx, executable, "api", "get", "/api/info") + cmd.Dir = directory + stdout := &boundedOutput{limit: MaxResponse} + stderr := &boundedOutput{limit: 8 << 10} + cmd.Stdout, cmd.Stderr = stdout, stderr + if err := cmd.Run(); err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + // No CLI output, auth, environment, or endpoint is copied to evidence. + return nil, fmt.Errorf("OpenCode stage service discovery failed") + } + if stdout.overflow { + return nil, fmt.Errorf("OpenCode stage service discovery response exceeds limit") + } + } + // Success alone is not evidence of which service the CLI selected. Require + // the private registration, then pin its endpoint/auth for all session work. + c, err := serviceConnection(path, directory) + if err != nil { + return nil, err + } + c.Executable = executable + return c, nil +} diff --git a/internal/opencode/stage_service_test.go b/internal/opencode/stage_service_test.go new file mode 100644 index 00000000..6363c46d --- /dev/null +++ b/internal/opencode/stage_service_test.go @@ -0,0 +1,144 @@ +package opencode + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +func coldServiceCLI(t *testing.T, script string) (string, string) { + t.Helper() + t.Setenv("HOME", t.TempDir()) + root := t.TempDir() + t.Setenv(OpenCodeServiceStateEnvKey, root) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + marker := filepath.Join(root, "discovery-called") + t.Setenv("TEST_DISCOVERY_MARKER", marker) + cli := filepath.Join(root, "fixture-cli") + if err := os.WriteFile(cli, []byte("#!/bin/sh\n"+script), 0700); err != nil { + t.Fatal(err) + } + return cli, marker +} + +func TestStageAdmissionDiscoversColdServiceThenPinsHTTPExport(t *testing.T) { + directory := t.TempDir() + calls := 0 + service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + user, password, ok := r.BasicAuth() + if !ok || user != "opencode" || password != "execution-password" || r.URL.Query().Get("directory") != directory { + w.WriteHeader(http.StatusUnauthorized) + return + } + if r.Method != "GET" || r.URL.Path != "/api/experimental/session/ses_ok/export" { + t.Error("unexpected session work") + w.WriteHeader(http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":{"info":{"id":"ses_ok"},"messages":[{"type":"user","text":"from execution A"}]}}`)) + })) + defer service.Close() + chatCalls := 0 + chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + chatCalls++ + w.WriteHeader(http.StatusNotFound) + })) + defer chat.Close() + t.Setenv(EndpointEnvKey, chat.URL) + t.Setenv(EndpointPasswordFileEnvKey, "/must-not-read-chat-credential") + t.Setenv(EndpointUsernameEnvKey, "foreign-chat-user") + cli, marker := coldServiceCLI(t, ` +[ "$*" = 'api get /api/info' ] || exit 20 +[ "$(pwd -P)" = "$(cd "$TEST_EXECUTION_DIRECTORY" && pwd -P)" ] || exit 21 +echo called > "$TEST_DISCOVERY_MARKER" +umask 077 +mkdir -p "$XDG_STATE_HOME/opencode" +cat "$TEST_SERVICE_REGISTRATION" > "$XDG_STATE_HOME/opencode/service.json" +echo '{"data":{"version":"fixture"}}' +`) + registration, _ := json.Marshal(map[string]string{"url": service.URL, "password": "execution-password"}) + source := filepath.Join(t.TempDir(), "service-registration") + if err := os.WriteFile(source, registration, 0600); err != nil { + t.Fatal(err) + } + t.Setenv("TEST_SERVICE_REGISTRATION", source) + t.Setenv("TEST_EXECUTION_DIRECTORY", directory) + stage, err := OpenStage(context.Background(), cli, directory, "v2") + if err != nil { + t.Fatal(err) + } + if _, err := os.Stat(marker); err != nil { + t.Fatal("admission never allowed the CLI to discover the cold service") + } + if stage.URL != service.URL || stage.Directory != directory || stage.password != "execution-password" { + t.Fatal("execution context did not match discovered registration") + } + ref, err := stage.Reference() + if err != nil { + t.Fatal(err) + } + recovered, err := FromReference(ref) + if err != nil { + t.Fatal(err) + } + result, err := ExportSession(context.Background(), "/must-not-run-export-cli", directory, "v2", "ses_ok", recovered) + if err != nil || len(result.Messages) != 1 || calls != 1 || chatCalls != 0 { + t.Fatalf("discovered execution A was not pinned: %v", err) + } +} + +func TestStageDiscoveryRequiresPrivateRegistrationBeforeSessionWork(t *testing.T) { + for _, mode := range []string{"missing", "invalid", "public", "cli-failed"} { + t.Run(mode, func(t *testing.T) { + script := ` +[ "$*" = 'api get /api/info' ] || exit 20 +echo called > "$TEST_DISCOVERY_MARKER" +echo '{"data":{}}' +` + if mode == "cli-failed" { + script += "echo 'private-output' >&2\nexit 9\n" + } + cli, marker := coldServiceCLI(t, script) + if mode == "invalid" || mode == "public" { + path, _ := ServiceCredentialPath() + if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { + t.Fatal(err) + } + body, permission := []byte(`not-json`), os.FileMode(0600) + if mode == "public" { + body, permission = []byte(`{"url":"http://127.0.0.1:1","password":"private-password"}`), 0644 + } + if err := os.WriteFile(path, body, permission); err != nil { + t.Fatal(err) + } + } + if _, err := OpenStage(context.Background(), cli, t.TempDir(), "v2"); err == nil { + t.Fatal("admission accepted unavailable or insecure service context") + } + _, err := os.Stat(marker) + if (mode == "missing" || mode == "cli-failed") != (err == nil) { + t.Fatal("discovery was not confined to missing registration") + } + }) + } +} + +func TestRetainedSessionPathsNeverDiscoverColdService(t *testing.T) { + cli, marker := coldServiceCLI(t, `echo called > "$TEST_DISCOVERY_MARKER"; exit 99`) + t.Setenv(EndpointEnvKey, "http://127.0.0.1:1") + if _, err := Open(context.Background(), cli, t.TempDir(), "v2"); err == nil { + t.Fatal("read-only open accepted missing registration") + } + if _, err := ExportSession(context.Background(), cli, t.TempDir(), "v2", "ses_ok"); err == nil { + t.Fatal("export accepted missing service context") + } + if _, err := os.Stat(marker); !os.IsNotExist(err) { + t.Fatal("retained session path invoked CLI discovery") + } +} diff --git a/internal/server/session_export_test.go b/internal/server/session_export_test.go index 39686951..6ec7fcea 100644 --- a/internal/server/session_export_test.go +++ b/internal/server/session_export_test.go @@ -63,6 +63,7 @@ func sessionExportCLI(t *testing.T) { func localExportService(t *testing.T, endpoint string) { t.Helper() + t.Setenv("HOME", t.TempDir()) t.Setenv("XDG_CACHE_HOME", t.TempDir()) state := t.TempDir() t.Setenv(opencode.OpenCodeServiceStateEnvKey, state) diff --git a/internal/stage/agent.go b/internal/stage/agent.go index dab26886..7d70c9f8 100644 --- a/internal/stage/agent.go +++ b/internal/stage/agent.go @@ -81,7 +81,7 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s c.Operations.Provider, c.Operations.Model = config.Provider, config.Model } if c.command == nil { - connection, err := opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend) + connection, err := opencode.OpenStage(ctx, opencode.Executable(), c.Directory, c.Backend) if err != nil { // Connecting to the service failed before any session existed. return "", harnessUnreachable(err) diff --git a/internal/stage/execution_service_test.go b/internal/stage/execution_service_test.go index 979408d6..248a264e 100644 --- a/internal/stage/execution_service_test.go +++ b/internal/stage/execution_service_test.go @@ -15,6 +15,7 @@ import ( // implement the API and must never consult an operator's service registration. func fakeExecutionService(t *testing.T) { t.Helper() + t.Setenv("HOME", t.TempDir()) // os.UserCacheDir uses HOME on macOS, not XDG_CACHE_HOME. state := t.TempDir() t.Setenv(opencode.OpenCodeServiceStateEnvKey, state) t.Setenv("XDG_CACHE_HOME", t.TempDir()) @@ -29,6 +30,7 @@ func fakeExecutionService(t *testing.T) { func TestV2StagePersistsExecutionReferenceBeforePrompt(t *testing.T) { for _, agent := range []bool{false, true} { t.Run(map[bool]string{false: "generation", true: "agent"}[agent], func(t *testing.T) { + t.Setenv("HOME", t.TempDir()) state := t.TempDir() t.Setenv(opencode.OpenCodeServiceStateEnvKey, state) t.Setenv("XDG_CACHE_HOME", t.TempDir()) @@ -89,3 +91,47 @@ esac }) } } + +func TestV2ColdDiscoveryWithoutRegistrationNeverCreatesSession(t *testing.T) { + for _, agent := range []bool{false, true} { + t.Run(map[bool]string{false: "generation", true: "agent"}[agent], func(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + root := t.TempDir() + t.Setenv(opencode.OpenCodeServiceStateEnvKey, root) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + t.Setenv(opencode.EndpointEnvKey, "http://127.0.0.1:1") + marker := filepath.Join(root, "cli-calls") + t.Setenv("TEST_COLD_CALLS", marker) + cli := filepath.Join(root, "fixture-cli") + if err := os.WriteFile(cli, []byte(`#!/bin/sh +echo "$*" >> "$TEST_COLD_CALLS" +echo '{"data":{}}' +`), 0700); err != nil { + t.Fatal(err) + } + t.Setenv("PIPELINES_OPENCODE", cli) + client := OpenCodeClient{Backend: "v2", Directory: root} + var err error + if agent { + c := AgentClient{OpenCodeClient: client} + _, err = c.Generate(context.Background(), ModelConfig{}, "unused") + } else { + _, err = client.Generate(context.Background(), ModelConfig{}, "unused") + } + if err == nil { + t.Fatal("stage accepted discovery without registration") + } + before, err := os.ReadFile(marker) + if err != nil || string(before) != "api get /api/info\n" { + t.Fatalf("session work admitted before registration: %q %v", before, err) + } + if err := client.InterruptAndConfirm(context.Background(), "ses_owned"); err == nil { + t.Fatal("restart interruption accepted missing registration") + } + after, _ := os.ReadFile(marker) + if string(before) != string(after) { + t.Fatal("restart interruption invoked discovery") + } + }) + } +} diff --git a/internal/stage/opencode.go b/internal/stage/opencode.go index 618cf0ac..152d3c45 100644 --- a/internal/stage/opencode.go +++ b/internal/stage/opencode.go @@ -274,7 +274,7 @@ func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, promp c.Operations.Provider, c.Operations.Model = config.Provider, config.Model } if c.command == nil { - connection, err := opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend) + connection, err := opencode.OpenStage(ctx, opencode.Executable(), c.Directory, c.Backend) if err != nil { // Connecting to the service failed before any session existed. return "", harnessUnreachable(err) diff --git a/internal/stage/session_export_test.go b/internal/stage/session_export_test.go index de6c95c1..5db417fd 100644 --- a/internal/stage/session_export_test.go +++ b/internal/stage/session_export_test.go @@ -68,6 +68,7 @@ func sessionBody() string { } func TestExportSessionUsesRetainedExecutionReference(t *testing.T) { + t.Setenv("HOME", t.TempDir()) sessionCLI(t, "ses_ok", sessionBody()) dir := t.TempDir() t.Setenv("XDG_CACHE_HOME", t.TempDir())