From accbe63e39458658efb21a0ed99d18c3a0736a2c Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 8 Oct 2026 23:17:38 +0100 Subject: [PATCH] Simplify Go installation and support standard OpenCode releases --- Dockerfile | 2 +- README.md | 31 ++- cmd/{server => pipelines-server}/main.go | 14 +- cmd/{tui => pipelines-tui}/main.go | 2 +- cmd/pipelines/main.go | 30 +- docs/docker.md | 4 +- docs/install-review.md | 44 +++ docs/install.md | 153 ++++++----- docs/installation-verification.md | 66 ++--- docs/ntfy.md | 2 +- examples/catalog.go | 2 +- examples/catalog_test.go | 4 +- go.mod | 2 +- install.sh | 7 +- internal/author/author.go | 42 ++- internal/author/author_test.go | 6 +- internal/author/live_test.go | 68 +++++ internal/author/skill/SKILL.md | 2 +- internal/cli/app.go | 12 +- internal/cli/app_test.go | 12 +- internal/cli/commands.go | 3 +- internal/cli/help.go | 12 +- internal/client/client.go | 2 +- internal/installation/installation.go | 15 +- internal/installation/installation_test.go | 1 + internal/installation/uninstall.go | 66 +++++ internal/opencode/connection.go | 261 ++++++++++++++++++ internal/opencode/live_test.go | 67 +++++ internal/opencode/native.go | 183 +++++++++++++ internal/opencode/native_test.go | 101 +++++++ internal/paths/paths.go | 21 ++ internal/paths/paths_test.go | 15 + internal/pipeline/definition.go | 4 +- internal/pipeline/definition_test.go | 2 +- internal/pipeline/examples_test.go | 2 +- internal/pipeline/nested_forks_test.go | 2 +- internal/pipeline/notify.go | 2 +- internal/pipeline/runs.go | 50 +++- internal/pipeline/schedule.go | 2 +- internal/pipeline/schedule_store.go | 2 +- internal/pipeline/schedule_test.go | 2 +- internal/pipeline/stage.go | 2 +- internal/pipeline/stage_test.go | 4 +- internal/pipeline/store.go | 4 +- internal/pipeline/store_test.go | 4 +- internal/pipeline/tools_test.go | 2 +- internal/server/cli_test.go | 8 +- internal/server/notification_setup.go | 2 +- internal/server/notify.go | 4 +- internal/server/notify_test.go | 6 +- internal/server/ntfy_test.go | 4 +- internal/server/run_websocket_test.go | 6 +- internal/server/runner.go | 16 +- internal/server/runner_test.go | 6 +- internal/server/schedule_websocket_test.go | 4 +- internal/server/scheduler.go | 4 +- internal/server/scheduler_test.go | 4 +- internal/server/templates_test.go | 4 +- internal/server/tools_test.go | 6 +- internal/server/websocket.go | 6 +- internal/server/websocket_test.go | 2 +- internal/server/workspaces_test.go | 6 +- internal/stage/agent.go | 44 +++ internal/stage/opencode.go | 35 ++- internal/stage/tools.go | 2 +- internal/tui/cli_run.go | 2 +- internal/tui/cli_run_test.go | 4 +- internal/tui/create.go | 8 +- internal/tui/create_test.go | 4 +- internal/tui/dashboard.go | 2 +- internal/tui/dashboard_test.go | 2 +- internal/tui/examples_test.go | 2 +- internal/tui/graph.go | 2 +- internal/tui/graph_forks.go | 2 +- internal/tui/graph_forks_test.go | 4 +- internal/tui/graph_layout.go | 2 +- internal/tui/graph_test.go | 4 +- internal/tui/highlights_test.go | 4 +- internal/tui/model.go | 4 +- internal/tui/model_test.go | 2 +- internal/tui/notification_setup_test.go | 2 +- internal/tui/panels.go | 2 +- internal/tui/panels_test.go | 4 +- internal/tui/picker.go | 2 +- internal/tui/pipeline.go | 6 +- internal/tui/pipeline_test.go | 6 +- internal/tui/run_detail.go | 2 +- internal/tui/run_progress.go | 2 +- internal/tui/run_progress_test.go | 6 +- internal/tui/runs.go | 2 +- internal/tui/runs_test.go | 2 +- internal/tui/schedule_test.go | 2 +- internal/tui/templates.go | 4 +- internal/tui/templates_test.go | 4 +- internal/tui/theme.go | 2 +- internal/tui/ux_review_test.go | 4 +- scripts/install.py | 304 +-------------------- scripts/package.py | 2 +- scripts/run-local-server.sh | 2 +- scripts/test_installation.py | 98 ++----- uninstall.sh | 2 +- 101 files changed, 1331 insertions(+), 682 deletions(-) rename cmd/{server => pipelines-server}/main.go (80%) rename cmd/{tui => pipelines-tui}/main.go (97%) create mode 100644 docs/install-review.md create mode 100644 internal/author/live_test.go create mode 100644 internal/installation/uninstall.go create mode 100644 internal/opencode/connection.go create mode 100644 internal/opencode/live_test.go create mode 100644 internal/opencode/native.go create mode 100644 internal/opencode/native_test.go create mode 100644 internal/paths/paths.go create mode 100644 internal/paths/paths_test.go diff --git a/Dockerfile b/Dockerfile index d5f93570..197153ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 go build -trimpath -o /out/pipelines-server ./cmd/server +RUN CGO_ENABLED=0 go build -trimpath -o /out/pipelines-server ./cmd/pipelines-server FROM ghcr.io/anomalyco/opencode:2.0.20 USER root diff --git a/README.md b/README.md index 43e1d95e..1660f782 100644 --- a/README.md +++ b/README.md @@ -5,28 +5,34 @@ server reuse your OpenCode login; Docker ntfy handles phone notifications. ## Install -Requires **Go 1.27+**, Python 3, Docker with Compose running, and OpenCode V2 -connected to OpenCode Go. +Requires **Go 1.27+** to install. Use your existing OpenCode for model stages; +Docker/ntfy notifications are optional. ```sh git clone https://git.a-wan.net/alan/pipelines.git cd pipelines -./install.sh --system-command +go install ./cmd/... pipelines help ``` -The installer builds with `go install` and exposes the Go `pipelines` binary in `/usr/local/bin`. -Run as your normal user: sudo is used only for the system command link. Services -and credentials remain per-user. Setup asks for your phone username, password, -and notification topic. Fresh installations contain no pipelines. +Go uses `GOBIN` or its normal `GOPATH/bin` default; put that directory on PATH. +`./install.sh` runs the same installation. It installs binaries only—no login +checks, dependency installers, hardcoded install paths, or automatic services. +For system-wide use, install the binaries into your chosen system bin directory +with the appropriate permissions. Configuration and data remain per-user. -Prefer a user-local command? Use `./install.sh` instead (`~/.local/bin/pipelines`). +Start explicitly; fresh databases contain no pipelines: + +```sh +pipelines serve -workspace-root "$HOME" # foreground server +# In another terminal: +pipelines +``` ## Uninstall ```sh -pipelines uninstall # keep history, credentials, and ntfy data -pipelines uninstall --purge # explicitly delete this installation's data +pipelines uninstall # remove Go binaries; keep history, drafts, and ntfy data ``` ## A three-stage pipeline @@ -89,9 +95,8 @@ pipelines validate /path/to/draft.md pipelines submit /path/to/draft.md # explicit publication pipelines runs pipelines inspect 1 -pipelines start # manages server + ntfy together -pipelines stop -pipelines doctor +pipelines serve # foreground server; Ctrl+C stops it +pipelines ping pipelines help edit ``` diff --git a/cmd/server/main.go b/cmd/pipelines-server/main.go similarity index 80% rename from cmd/server/main.go rename to cmd/pipelines-server/main.go index c491d0dd..6f349e85 100644 --- a/cmd/server/main.go +++ b/cmd/pipelines-server/main.go @@ -12,13 +12,18 @@ import ( "syscall" "time" - "pipelines/internal/pipeline" - "pipelines/internal/server" + "git.a-wan.net/alan/pipelines/internal/paths" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/server" ) func main() { addr := flag.String("addr", "127.0.0.1:8080", "HTTP/WebSocket listen address") - db := flag.String("db", "pipelines.db", "SQLite database path") + defaultDB, err := paths.Database() + if err != nil { + log.Fatal(err) + } + db := flag.String("db", defaultDB, "SQLite database path (honors XDG_DATA_HOME)") var roots []string flag.Func("workspace-root", "Allow repository runs below this directory (repeatable; default is server working directory)", func(value string) error { absolute, err := filepath.Abs(value) @@ -40,6 +45,9 @@ func main() { return nil }) flag.Parse() + if err := os.MkdirAll(filepath.Dir(*db), 0700); err != nil { + log.Fatal(err) + } store, err := pipeline.Open(*db) if err != nil { log.Fatal(err) diff --git a/cmd/tui/main.go b/cmd/pipelines-tui/main.go similarity index 97% rename from cmd/tui/main.go rename to cmd/pipelines-tui/main.go index 61ee669a..735d6a28 100644 --- a/cmd/tui/main.go +++ b/cmd/pipelines-tui/main.go @@ -9,7 +9,7 @@ import ( tea "github.com/charmbracelet/bubbletea" - "pipelines/internal/tui" + "git.a-wan.net/alan/pipelines/internal/tui" ) func main() { diff --git a/cmd/pipelines/main.go b/cmd/pipelines/main.go index dc425f71..8c47c522 100644 --- a/cmd/pipelines/main.go +++ b/cmd/pipelines/main.go @@ -4,11 +4,12 @@ import ( "context" "encoding/json" "fmt" + "git.a-wan.net/alan/pipelines/internal/cli" + "git.a-wan.net/alan/pipelines/internal/installation" "os" + "os/exec" "os/signal" "path/filepath" - "pipelines/internal/cli" - "pipelines/internal/installation" "syscall" ) @@ -31,8 +32,31 @@ func main() { args = []string{"tui"} } } + if err == nil && config == nil && len(args) > 0 && args[0] == "uninstall" { + err = installation.Uninstall(executable, args[1:], os.Stdin, os.Stdout) + if err == nil { + return + } + } + if err == nil && config == nil && len(args) > 0 && args[0] == "serve" { + server, findErr := exec.LookPath("pipelines-server") + if findErr != nil { + resolved, resolveErr := filepath.EvalSymlinks(executable) + if resolveErr != nil { + err = resolveErr + } else { + server = filepath.Join(filepath.Dir(resolved), "pipelines-server") + } + } + if err == nil { + err = syscall.Exec(server, append([]string{server}, args[1:]...), os.Environ()) + } + } if err == nil && len(args) > 0 && installation.Service(args[0]) { - err = fmt.Errorf("%s requires a managed installation; run install.sh first", args[0]) + err = fmt.Errorf("%s is a legacy managed-service command; use pipelines serve, or select an existing installation with PIPELINES_PREFIX", args[0]) + } + if err == nil && len(args) == 0 { + args = []string{"tui"} } if err == nil { err = app.Run(ctx, args) diff --git a/docs/docker.md b/docs/docker.md index 0b08d5bf..33ce83d1 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -8,7 +8,7 @@ To reuse your host OpenCode login without container authentication: docker compose up -d ntfy # Bootstrap ntfy users/token as described below, then: sh scripts/run-local-server.sh -go run ./cmd/tui -url ws://127.0.0.1:8080/connect +go run ./cmd/pipelines-tui -url ws://127.0.0.1:8080/connect ``` Do not run `docker compose up -d` for this mode: that starts the pipeline container @@ -100,7 +100,7 @@ The Docker workspace is persistent and separate from your host checkout. Connect the native TUI: ```sh -go run ./cmd/tui -url ws://127.0.0.1:8081/connect +go run ./cmd/pipelines-tui -url ws://127.0.0.1:8081/connect ``` If the TUI is on another machine, use an SSH/VPN tunnel instead of exposing the diff --git a/docs/install-review.md b/docs/install-review.md new file mode 100644 index 00000000..7b63cb4d --- /dev/null +++ b/docs/install-review.md @@ -0,0 +1,44 @@ +# Installation review + +Two independent reviewers examined installation conventions and stable OpenCode +compatibility. Both agreed that removing the V2 gate alone would hide real API +incompatibility rather than fix it. + +## Findings addressed + +- Installation mixed binary building with Docker provisioning, provider login, + private npm installation, service registration, and startup. It is now a thin + standard `go install ./cmd/...` operation, with no runtime checks or side effects. +- Go's repository import path did not match `go.mod`. The module now matches the + repository, enabling normal module installation (with `GOPRIVATE` for private access). +- Generic `server`/`tui` commands required renaming and a custom layout. They are + now `pipelines-server` and `pipelines-tui`, installed directly by Go. +- Runtime configuration was guessed relative to the binary, and a saved PATH + overrode the caller. Standard locations/explicit flags now select data; legacy + setup requires an explicit `PIPELINES_PREFIX`, and the caller's PATH is preserved. +- Native stable OpenCode has no equivalent to the V2 CLI/API contract. It now has + a separate HTTP adapter, using the existing executable/account, strict JSON + contracts, explicit models, translated scoped permissions, and an owned loopback + service. The existing V2 adapter is retained rather than pretending APIs match. +- Crash ownership must include the backend and native service. Additive metadata + records those references without rewriting immutable definitions or histories. +- The skill name did not match its directory. It is now `pipelines`. Native + authoring preloads the skill/reference text without inference; V2 activates it. +- Ownership persistence failure and authoring `SESSION` write failure could leak + created sessions. Those failure paths now clean up only their owned sessions. + +## Deliberately removed from default installation + +Private dependency installs, login prompts, mandatory notifications, automatic +LAN HTTP exposure, generated system bin symlinks, private binary prefixes, and a +second background supervisor. Configure optional services separately using normal +OS tools. The old managed installation helper remains only for explicit legacy +data/lifecycle access; it is not the standard installer. + +## Verification limitations + +The reviewer checked official stable OpenCode 1.18.35 in isolation and found V2 +bridge URLs that return HTTP 200 **HTML**. The adapter rejects non-JSON responses. +No live inference or real model tool-permission enforcement was run. See +[verification scope](installation-verification.md); do not confuse successful +installation or stored permission rules with proven model execution. diff --git a/docs/install.md b/docs/install.md index 2dc901e8..fd543e5a 100644 --- a/docs/install.md +++ b/docs/install.md @@ -1,101 +1,104 @@ -# Install and test +# Installation -Run as your normal user, **not** with sudo. Requires Python 3, a running Docker -daemon with Compose, and OpenCode V2 connected to OpenCode Go. Source installs -also require Go 1.27+. Platform archives contain the server, CLI, and TUI binaries. If OpenCode -is absent and npm is available, the installer offers a private pinned CLI install; -Docker itself is not installed automatically. +Installation requires **Go only**. OpenCode is needed when authoring or executing +model stages. Docker/ntfy is optional and is configured separately. ```sh -./install.sh -# Optional: expose the command in /usr/local/bin (sudo for the symlink only): -./install.sh --system-command +git clone https://git.a-wan.net/alan/pipelines.git +cd pipelines +go install ./cmd/... +# ./install.sh is a thin wrapper around the same command. ``` -Setup prompts for phone username, password, and global notification topic. Blank -password generates one. `--yes` accepts defaults and generates a password; -`--password-file PATH` supplies one without putting it in shell arguments. -Phone connection details are saved privately in -`~/.local/share/pipelines/secrets/phone-login.txt`. Configure the ntfy phone app -with those credentials and subscribe to that topic. Phone delivery still needs -an actual device test. Prefer Tailscale or HTTPS; HTTP exposes credentials on an -untrusted network. +This installs `pipelines`, `pipelines-server`, and `pipelines-tui`. Go chooses the +destination using `GOBIN`, or the usual `GOPATH/bin` default. Put that directory +on PATH. No privileged operations, private dependency installation, login prompts, +Docker provisioning, service registration, or background processes are involved. +Re-running installation updates the binaries without changing data or configuration. -The native server and Docker ntfy are managed together: +For a private repository, use your normal Git credentials and `GOPRIVATE`: ```sh -pipelines # dashboard; b opens rendered templates -pipelines status -pipelines doctor -pipelines stop -pipelines start -pipelines restart -pipelines logs +GOPRIVATE=git.a-wan.net go install git.a-wan.net/alan/pipelines/cmd/...@latest ``` -Default command path is `~/.local/bin/pipelines`; add `~/.local/bin` to PATH if -needed. It points directly to a compiled Go binary, not a shell launcher. -Source installation builds all binaries with `go install`. Service registration uses -per-user launchd on macOS or systemd on Linux. -`--no-autostart` uses background supervision instead. Services retain the current -user's OpenCode config/login, without exporting credentials into Docker. -`--system-command` is a system PATH shortcut to this user's install, **not** a -shared multi-user service. Other users should install their own instance. +For a system-wide command, build as your normal user and install the three binaries +into an administrator-selected bin directory using your normal system permissions. +Services and OpenCode accounts still run as the logged-in user, never as root. +The installer does not hardcode a system destination or run sudo. -## Run in a repository +## Start explicitly ```sh -cd ~/repos/my-project -pipelines run # fuzzy pipeline picker, then prompt -pipelines run "bug fix" # prompt composer -pipelines run "bug fix" "Fix the layout" # explicit execution confirmation -pipelines run "bug fix" "Fix the layout" --yes +pipelines serve -workspace-root "$HOME" # foreground server; Ctrl+C stops it +# In another terminal: +pipelines ``` -The composer supports multiline typing/paste: **Enter** adds a newline, -**Ctrl+S** or **Alt+Enter** continues. The current directory is recorded as the -run workspace. After confirmation, the live run inspector opens. -`--yes` requires a supplied name and nonempty prompt and does not approve tools. -Use `--allow-tools` with `--yes` to explicitly approve declared tools; -`--write-paths "internal/example.go"` grants exact existing Go-file edits. -Current edit tooling is Go-specific, not arbitrary repository editing. +The server listens on loopback. Limit `-workspace-root` to trusted repositories; +repeat the flag for multiple roots. Omission permits only the server's working +directory. Runtime repository requests cannot escape these roots via symlinks. -Installer workspace roots default to the user's home directory. Restrict them -using repeatable `--workspace-root ~/repos` on initial setup. Runs outside those -roots (including symlink escapes) are rejected by the server. Raw native servers -default to their working directory; add `-workspace-root` to permit other repos. -Do not expose the unauthenticated pipeline server to untrusted users. +The SQLite database uses the standard user data location, honoring `XDG_DATA_HOME` +when set. On macOS it uses the user Application Support directory. `-db FILE` +selects another database, including an existing one; nothing is imported or moved +automatically. `pipelines serve --help` prints the actual defaults on your machine. +Drafts use the OS user cache; `PIPELINES_DRAFT_DIR` overrides that location. -## Templates and data +Use your OS's normal service manager if you want autostart. Pipelines does not +install a second supervisor. Native foreground startup is the default. -New databases are empty. Press **b** on Home or use `:templates` to browse optional -examples, with rendered Markdown previews and **v** for source. Select a template, -choose a unique name, edit it in `$VISUAL`/`$EDITOR` (or an available fallback), -then explicitly submit. Existing names cannot be overwritten by this flow. -Submitting a scheduled template activates its schedule; previewing never does. +## OpenCode -Uninstall retains pipeline history, credentials, and the isolated ntfy volume: +Use your existing `opencode` on PATH, or set `PIPELINES_OPENCODE` to an explicit +executable. No OpenCode version is required just to install binaries. At runtime: + +- The standard/native API release uses an owned, authenticated loopback + `opencode serve` process, scoped to the requested repository and using the + existing user's configuration/login. The API, not a version number, is checked. +- A CLI advertising `api` uses the V2 command interface and its normal discovery. +- Both adapters select the pipeline's configured model explicitly and preserve + stage tool permissions. Native `shell` permissions map to OpenCode's `bash`. +- Audited native sessions record their backend and a private service reference + so recovery addresses the same service, rather than a new unrelated instance. +- `new`/`edit` preload authoring instructions before opening the normal interface. + Native preloading uses `noReply=true`; V2 uses explicit skill activation. + +The bundled examples retain `opencode-go/deepseek-v4.1-flash`. Availability and +authentication for that model are runtime requirements, not installation checks. +No login or provider credentials are copied or exported by Pipelines. + +## Optional notifications + +Use an existing ntfy server or the optional Docker setup in [docs/ntfy.md](ntfy.md). +Configure `PIPELINES_NTFY_URL`, `PIPELINES_NTFY_PUBLIC_URL`, and a publishing token +via `PIPELINES_NTFY_TOKEN_FILE` in the server's environment. Keep secrets out of +pipeline definitions. Use HTTPS or an encrypted VPN for phone access; do not +expose an unauthenticated pipeline server or HTTP credentials to untrusted networks. + +## Uninstall + +Stop the foreground server first, then: ```sh -./uninstall.sh -# Explicitly delete this installation's retained data too: -./uninstall.sh --purge +pipelines uninstall +# or pipelines uninstall --yes ``` -Reinstall at the same prefix to reuse retained data. Host OpenCode remains -separate. Partial installations require inspection or explicit purge before retry. -Use `--prefix PATH --bin-dir PATH --server-port PORT --ntfy-port PORT` for isolated -testing; each prefix has its own Compose project and volume. +Only Go binaries whose embedded build metadata matches this repository are +removed from the current command's bin directory. History, drafts, OpenCode, +and optional ntfy data remain untouched. Use the permissions appropriate to your +chosen bin directory. Custom database paths cannot be inferred, so uninstallation +does not purge data. -## Tester archives +## Existing managed installations -```sh -python3 scripts/package.py --platform darwin-arm64 --version 0.1.0-beta.1 -# Also: darwin-amd64, linux-amd64, linux-arm64, source -``` +The previous Python-supervised setup is retained only for explicit legacy use. +Set `PIPELINES_PREFIX` to its existing directory for its lifecycle commands; +no command infers that directory from an executable location. The old +`--system-command`, provisioning, and autostart installer flags have been removed. -Outputs go to `dist/` with a SHA-256 checksum. Archives include only allowlisted -source, examples, docs, installation scripts, and optional release binaries—no -databases, runtime drafts, `.env`, logs, or secrets. Extract, then run `./install.sh`. -Cross-built Linux binaries, Linux systemd, system PATH installation, and automatic -npm installation require separate execution tests; build success is not a runtime test. +To migrate, stop the old server first, install normal Go binaries, and start with +`pipelines serve -db EXISTING_DATABASE -workspace-root TRUSTED_ROOT`. Keep the old +data and credentials until you have verified the new setup. There is no automatic +purge, database move, login migration, or shared OpenCode service modification. diff --git a/docs/installation-verification.md b/docs/installation-verification.md index 9a2291bb..31a3b072 100644 --- a/docs/installation-verification.md +++ b/docs/installation-verification.md @@ -1,52 +1,26 @@ -# Installation verification +# Verification scope -Executed on macOS (arm64), using an isolated prefix and ports 18080/18090; -the development database and services were not targets of these checks. +Regression commands: -- `go test -race -count=1 ./...`: passed, including fresh empty database, - template catalog/create-only protection, workspace allowlisting and symlink - rejection, multiline prompt, and run-state regressions. -- `go vet ./...`: passed. -- `python3 -m unittest discover -s scripts -p 'test_*.py'`: 14 passed (including - unchanged argument forwarding to the shared CLI). -- `./install.sh --yes --no-autostart --prefix --bin-dir `: - installed native binaries plus isolated Docker ntfy, with private credentials. -- Isolated `pipelines doctor`: both health endpoints and host OpenCode API passed. -- Actual pseudo-terminal check (`check-install-cli.py` in the session's temporary - storage): rendered template browser, editor return without automatic submission, - explicit submission, fuzzy CLI selection, multiline composer, and supplied - name/prompt confirmation passed. -- Isolated `pipelines restart`, retained-data uninstall, reinstall, and a second - terminal check: passed; the submitted pipeline survived reinstall. -- Authenticated HTTP publication and phone-reader polling: passed. Actual Android - app reception is **not tested**. +```sh +go test -race -count=1 ./... +go vet ./... +python3 -m unittest discover -s scripts -p 'test_*.py' +``` -Full model-driven repository changes, launchd autostart, Linux systemd, privileged -`--system-command`, and automatic npm CLI installation are **not tested**. -Existing Go-file edit-scope restrictions still apply. +Optional no-inference stable OpenCode checks: -## CLI authoring checks +```sh +PIPELINES_TEST_OPENCODE_V1=/path/to/opencode \ + go test ./internal/opencode ./internal/author -run TestStable -count=1 -v +``` -- `check-author-cli.py` (session temporary storage): isolated installed help, - JSON draft/validate/submit/export/history, and real OpenCode session/skill - activation passed. Interactive launch arguments were captured without a model turn. -- `check-author-picker.py`: actual terminal fuzzy `edit` selection, draft creation, - and real skill preload passed, again without a model turn. -- CLI/server regression tests cover stale-version rejection, invalid submission - rejection even with `--force`, create-only collisions, headless run idempotency, - inspection/cancellation, and bounded waiting that leaves an active run intact - on timeout. Execution is controlled in these tests, not a live model. +The stable checks use isolated home/config/data/cache directories; they never +copy login credentials or invoke a model. They exercise session creation, explicit +model selection, stored scoped permissions, abort on the same owned service, +session deletion, and authoring instruction preload with `noReply=true`. -Full interactive model-driven pipeline authoring is **not tested**. - -## Compiled Go command - -- `GOBIN= go install ./cmd/pipelines ./cmd/server ./cmd/tui`: passed. -- Source installer now uses `go install`; isolated installation and retained-data - reinstall passed. The command symlink resolved to a Mach-O Go binary, not a script. -- Compiled command `help`, `list --json`, README three-stage example validation / - submission, `doctor`, and `restart`: passed against isolated services. -- A separately Go-installed CLI selected the same installation using - `PIPELINES_PREFIX` and read the retained pipeline successfully. - -Privileged `/usr/local/bin` linking, launchd, and Linux remain **not tested**. +Native generation/assistant completion parsing is regression-tested against a +controlled HTTP server. Full live inference, permission enforcement by real model +tool calls, cancellation during inference, optional Docker deployment, privileged +system-wide copying, and OS service-manager registration are **not tested**. diff --git a/docs/ntfy.md b/docs/ntfy.md index a9d49cb7..e4fa78f4 100644 --- a/docs/ntfy.md +++ b/docs/ntfy.md @@ -74,7 +74,7 @@ Configure the **pipeline server process** outside the pipeline file: ```sh export PIPELINES_NTFY_ANIME_URL='https://ntfy.your-domain.example/anime' # Set PIPELINES_NTFY_ANIME_TOKEN securely to a publishing user's access token. -go run ./cmd/server +go run ./cmd/pipelines-server ``` The URL includes one topic, without credentials, query parameters, or a fragment. diff --git a/examples/catalog.go b/examples/catalog.go index 09b48d1c..b165f350 100644 --- a/examples/catalog.go +++ b/examples/catalog.go @@ -2,7 +2,7 @@ package examples import ( "embed" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "strings" ) diff --git a/examples/catalog_test.go b/examples/catalog_test.go index 256f306f..6e07f282 100644 --- a/examples/catalog_test.go +++ b/examples/catalog_test.go @@ -1,9 +1,9 @@ package examples_test import ( + "git.a-wan.net/alan/pipelines/examples" + "git.a-wan.net/alan/pipelines/internal/pipeline" "os" - "pipelines/examples" - "pipelines/internal/pipeline" "testing" ) diff --git a/go.mod b/go.mod index 3464f66e..e2a86399 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module pipelines +module git.a-wan.net/alan/pipelines go 1.27.0 diff --git a/install.sh b/install.sh index fcdfc39e..af63a8dc 100755 --- a/install.sh +++ b/install.sh @@ -1,3 +1,8 @@ #!/bin/sh set -eu -exec python3 "$(dirname "$0")/scripts/install.py" "$@" +if [ "$#" -ne 0 ]; then + echo 'Usage: ./install.sh (honors GOBIN and GOPATH; installs binaries only)' >&2 + exit 2 +fi +cd "$(dirname "$0")" +exec go install -trimpath ./cmd/... diff --git a/internal/author/author.go b/internal/author/author.go index 960b957e..1fae1c85 100644 --- a/internal/author/author.go +++ b/internal/author/author.go @@ -5,11 +5,13 @@ import ( "embed" "encoding/json" "fmt" + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/opencode" "os" "os/exec" "path/filepath" - "pipelines/internal/draft" "regexp" + "time" ) //go:embed skill/SKILL.md skill/references/format.md @@ -47,6 +49,14 @@ func Workspace(d draft.Draft) (string, error) { // PrepareSession explicitly loads the skill without starting a model turn. // The existing user's CLI handles discovery and authentication; no auth is exported. func PrepareSession(ctx context.Context, executable, workspace, name string) (string, error) { + connection, err := opencode.Open(ctx, executable, workspace, "") + if err != nil { + return "", err + } + defer connection.Close() + if connection.Backend == "v1" { + return prepareNative(ctx, connection, workspace, name) + } call := func(method, path string, body any) ([]byte, error) { args := []string{"api", method, path} if body != nil { @@ -82,11 +92,41 @@ func PrepareSession(ctx context.Context, executable, workspace, name string) (st return "", err } if err = os.WriteFile(filepath.Join(workspace, "SESSION"), []byte(id+"\n"), 0600); err != nil { + _, _ = call("delete", "/api/session/"+id, nil) return "", err } return id, nil } +func prepareNative(ctx context.Context, connection *opencode.Connection, workspace, name string) (id string, err error) { + data, err := connection.Native(ctx, "post", "/session", map[string]string{"title": "Pipeline draft: " + name}) + if err != nil { + return "", err + } + var result struct { + ID string `json:"id"` + } + if json.Unmarshal(data, &result) != nil || !regexp.MustCompile(`^ses[a-zA-Z0-9_-]+$`).MatchString(result.ID) { + return "", fmt.Errorf("invalid OpenCode native session") + } + id = result.ID + defer func() { + if err != nil { + cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) + defer cancel() + _, _ = connection.Native(cleanup, "delete", "/session/"+id, nil) + } + }() + skill, _ := content.ReadFile("skill/SKILL.md") + reference, _ := content.ReadFile("skill/references/format.md") + instructions := "Pipeline authoring skill (preloaded; no inference requested):\n" + string(skill) + "\nDefinition reference:\n" + string(reference) + if _, err = connection.Native(ctx, "post", "/session/"+id+"/message", map[string]any{"noReply": true, "parts": []map[string]string{{"type": "text", "text": instructions}}}); err != nil { + return "", err + } + err = os.WriteFile(filepath.Join(workspace, "SESSION"), []byte(id+"\n"), 0600) + return id, err +} + func Prompt(d draft.Draft, request, command string) string { if request == "" { request = "Ask me what this pipeline should do or what to change, then help me edit its draft." diff --git a/internal/author/author_test.go b/internal/author/author_test.go index 3c8ca14c..d9c2387d 100644 --- a/internal/author/author_test.go +++ b/internal/author/author_test.go @@ -2,9 +2,9 @@ package author import ( "context" + "git.a-wan.net/alan/pipelines/internal/draft" "os" "path/filepath" - "pipelines/internal/draft" "strings" "testing" ) @@ -21,7 +21,7 @@ func TestSkillActivationIsExplicitAndNoResume(t *testing.T) { } capture := filepath.Join(t.TempDir(), "calls") script := filepath.Join(t.TempDir(), "opencode") - os.WriteFile(script, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" >> '"+capture+"'\nif [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_test\"}}'; fi\n"), 0700) + os.WriteFile(script, []byte("#!/bin/sh\nif [ \"$1\" = --help ]; then printf '%s\\n' ' api commands'; exit 0; fi\nprintf '%s\\n' \"$@\" >> '"+capture+"'\nif [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_test\"}}'; fi\n"), 0700) id, err := PrepareSession(context.Background(), script, workspace, d.Name) if err != nil || id != "ses_test" { t.Fatal(id, err) @@ -40,7 +40,7 @@ func TestSkillFailureDoesNotLaunchAndCleansOnlyCreatedSession(t *testing.T) { workspace := t.TempDir() capture := filepath.Join(workspace, "calls") script := filepath.Join(workspace, "opencode") - os.WriteFile(script, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" >> '"+capture+"'\nif [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_new\"}}'; exit 0; fi\nif [ \"$2\" = delete ]; then exit 0; fi\nexit 1\n"), 0700) + os.WriteFile(script, []byte("#!/bin/sh\nif [ \"$1\" = --help ]; then printf '%s\\n' ' api commands'; exit 0; fi\nprintf '%s\\n' \"$@\" >> '"+capture+"'\nif [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_new\"}}'; exit 0; fi\nif [ \"$2\" = delete ]; then exit 0; fi\nexit 1\n"), 0700) if _, err := PrepareSession(context.Background(), script, workspace, "name"); err == nil { t.Fatal("missing skill allowed") } diff --git a/internal/author/live_test.go b/internal/author/live_test.go new file mode 100644 index 00000000..fbc6cd10 --- /dev/null +++ b/internal/author/live_test.go @@ -0,0 +1,68 @@ +package author + +import ( + "context" + "encoding/json" + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/opencode" + "os" + "strings" + "testing" + "time" +) + +func TestStableAuthoringInstructionsPreloadedWithoutInference(t *testing.T) { + executable := os.Getenv("PIPELINES_TEST_OPENCODE_V1") + if executable == "" { + t.Skip("set PIPELINES_TEST_OPENCODE_V1 for no-inference authoring preload test") + } + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", home+"/config") + t.Setenv("XDG_DATA_HOME", home+"/data") + t.Setenv("XDG_CACHE_HOME", home+"/cache") + t.Setenv("XDG_STATE_HOME", home+"/state") + t.Setenv("PIPELINES_DRAFT_DIR", home+"/drafts") + d, err := draft.Create("native authoring", "draft source", "ws://localhost", true, 0) + if err != nil { + t.Fatal(err) + } + workspace, err := Workspace(d) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + defer cancel() + id, err := PrepareSession(ctx, executable, workspace, d.Name) + if err != nil { + t.Fatal(err) + } + c, err := opencode.Open(ctx, executable, workspace, "") + if err != nil { + t.Fatal(err) + } + defer c.Close() + defer c.Native(context.Background(), "delete", "/session/"+id, nil) + data, err := c.Native(ctx, "get", "/session/"+id+"/message", nil) + if err != nil { + t.Fatal(err) + } + var messages []struct { + Info struct { + Role string `json:"role"` + } `json:"info"` + Parts []struct{ Type, Text string } `json:"parts"` + } + if json.Unmarshal(data, &messages) != nil || len(messages) != 1 || messages[0].Info.Role != "user" { + t.Fatal("preload ran inference or missing message", string(data)) + } + text := messages[0].Parts[0].Text + if !strings.Contains(text, "Do not submit or run by default") && !strings.Contains(text, "Do not submit or run") { + if !strings.Contains(text, "Do not submit") { + t.Fatal("draft-only instruction not preloaded", text) + } + } + if !strings.Contains(text, "Definition reference") || !strings.Contains(text, "provider=opencode-go") { + t.Fatal("format reference not preloaded") + } +} diff --git a/internal/author/skill/SKILL.md b/internal/author/skill/SKILL.md index 6cd6d9cc..18f4f1c6 100644 --- a/internal/author/skill/SKILL.md +++ b/internal/author/skill/SKILL.md @@ -1,5 +1,5 @@ --- -name: Pipeline authoring +name: pipelines description: Create or edit validated pipeline Markdown drafts with the pipelines CLI; preserve immutable versions and require explicit submission. --- diff --git a/internal/cli/app.go b/internal/cli/app.go index 27f4abf9..ec37ae87 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -15,13 +15,13 @@ import ( "strings" "time" + "git.a-wan.net/alan/pipelines/internal/author" + "git.a-wan.net/alan/pipelines/internal/client" + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/tui" tea "github.com/charmbracelet/bubbletea" - "pipelines/internal/author" - "pipelines/internal/client" - "pipelines/internal/draft" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/tui" ) type App struct { diff --git a/internal/cli/app_test.go b/internal/cli/app_test.go index b5feeed3..c0ca0889 100644 --- a/internal/cli/app_test.go +++ b/internal/cli/app_test.go @@ -11,11 +11,11 @@ import ( "strings" "testing" - "pipelines/internal/cli" - "pipelines/internal/draft" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/server" + "git.a-wan.net/alan/pipelines/internal/cli" + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/server" ) func setup(t *testing.T) (func(...string) (string, error), *pipeline.Store, string) { @@ -145,7 +145,7 @@ func TestCLIAuthorOpenCodeGetsScopedSkillAndLeavesDraft(t *testing.T) { root := t.TempDir() capture := filepath.Join(root, "invocation") script := filepath.Join(root, "opencode") - os.WriteFile(script, []byte("#!/bin/sh\nif [ \"$1\" = api ]; then\n if [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_test\"}}'; fi\n exit 0\nfi\nprintf '%s\\n' \"$@\" > "+"'"+capture+"'"+"\n"), 0700) + os.WriteFile(script, []byte("#!/bin/sh\nif [ \"$1\" = --help ]; then printf '%s\\n' ' api commands'; exit 0; fi\nif [ \"$1\" = api ]; then\n if [ \"$3\" = /api/session ]; then printf '%s' '{\"data\":{\"id\":\"ses_test\"}}'; fi\n exit 0\nfi\nprintf '%s\\n' \"$@\" > "+"'"+capture+"'"+"\n"), 0700) if _, err := run("new", "scoped skill", "Make a digest", "--opencode", script); err != nil { t.Fatal(err) } diff --git a/internal/cli/commands.go b/internal/cli/commands.go index 5028eed3..c76f3a98 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -33,11 +33,12 @@ var notes = map[string]string{ } var serviceHelp = map[string]string{ + "serve": "Run the native server in the foreground. Its flags are documented by pipelines serve --help. Installation never starts it automatically.", "start": "Start the per-user native pipeline server and this installation's Docker ntfy together.", "stop": "Stop both services. Active execution is interrupted; file changes are not rolled back.", "restart": "Stop, then start both services, retaining definitions, run history, and credentials.", "status": "Show pipeline/ntfy health and the public phone connection (no credentials).", "doctor": "Check both health endpoints and host OpenCode API discovery; does not execute a model.", "logs": "Follow the native server log. Press Ctrl+C to stop following.", - "uninstall": "Remove this installation; retain data by default. --purge explicitly deletes this installation and its ntfy volume; --yes skips prompts.", + "uninstall": "Remove verified Go binaries from this command's bin directory; retain data and drafts. --yes skips confirmation. Existing managed installations can be selected explicitly with PIPELINES_PREFIX.", } diff --git a/internal/cli/help.go b/internal/cli/help.go index 5670e1b6..41e5fa79 100644 --- a/internal/cli/help.go +++ b/internal/cli/help.go @@ -28,11 +28,13 @@ EXECUTE ping Check the server connection rpc Send any protocol command (automation escape hatch) -SERVICE / INTERFACE (managed installation) - tui Open the dashboard (default with no command) - start | stop | restart Manage the native server + Docker ntfy together - status | doctor | logs Health, OpenCode discovery, server logs - uninstall [--purge] Remove app; retain data unless explicitly purged +SERVER / INTERFACE + tui Open the dashboard (default with no command) + serve Start native server in the foreground (Ctrl+C stops it) + uninstall [--yes] Remove verified Go binaries; retain data and drafts + +Legacy managed installs: select PIPELINES_PREFIX explicitly for start/stop, +restart/status/doctor/logs and retained-data or --purge uninstallation. Options: --url ws://127.0.0.1:8080/connect --json --help Use pipelines help for its flags. Names with spaces must be quoted. diff --git a/internal/client/client.go b/internal/client/client.go index e393420c..5b7b1c83 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -8,9 +8,9 @@ import ( "strconv" "sync" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/coder/websocket" "github.com/coder/websocket/wsjson" - "pipelines/internal/protocol" ) func Dial(ctx context.Context, url string) (*websocket.Conn, error) { diff --git a/internal/installation/installation.go b/internal/installation/installation.go index 965c6c99..5bcc0e9d 100644 --- a/internal/installation/installation.go +++ b/internal/installation/installation.go @@ -19,18 +19,13 @@ type Config struct { Path string `json:"path"` } -// Load finds the manifest next to the actual Go binary, including via a system PATH symlink. -// A standalone go-installed CLI can also select a prefix with PIPELINES_PREFIX. +// Load selects a legacy managed installation only via an explicit PIPELINES_PREFIX. func Load(executable string) (*Config, error) { - resolved, err := filepath.EvalSymlinks(executable) - if err != nil { - return nil, err - } prefix := os.Getenv("PIPELINES_PREFIX") if prefix == "" { - prefix = filepath.Dir(filepath.Dir(resolved)) + return nil, nil } - prefix, err = filepath.Abs(prefix) + prefix, err := filepath.Abs(prefix) if err != nil { return nil, err } @@ -60,8 +55,8 @@ func (c Config) Environment() error { } } } - for key, value := range map[string]string{"PIPELINES_URL": "ws://127.0.0.1:" + strconv.Itoa(c.ServerPort) + "/connect", "PIPELINES_OPENCODE": c.OpenCode, "PATH": c.Path} { - if key != "PATH" && os.Getenv(key) != "" { + for key, value := range map[string]string{"PIPELINES_URL": "ws://127.0.0.1:" + strconv.Itoa(c.ServerPort) + "/connect", "PIPELINES_OPENCODE": c.OpenCode} { + if os.Getenv(key) != "" { continue } if err := os.Setenv(key, value); err != nil { diff --git a/internal/installation/installation_test.go b/internal/installation/installation_test.go index 45b155be..7e31c2f8 100644 --- a/internal/installation/installation_test.go +++ b/internal/installation/installation_test.go @@ -18,6 +18,7 @@ func TestCompiledCommandFindsManifestThroughSystemLink(t *testing.T) { os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600) link := filepath.Join(t.TempDir(), "pipelines") os.Symlink(binary, link) + t.Setenv("PIPELINES_PREFIX", root) loaded, err := Load(link) if err != nil || loaded == nil || loaded.Prefix != root { t.Fatal(loaded, err) diff --git a/internal/installation/uninstall.go b/internal/installation/uninstall.go new file mode 100644 index 00000000..8cf926ec --- /dev/null +++ b/internal/installation/uninstall.go @@ -0,0 +1,66 @@ +package installation + +import ( + "bufio" + "debug/buildinfo" + "flag" + "fmt" + "io" + "os" + "path/filepath" + "strings" +) + +// Uninstall removes only verified binaries from the current command's Go bin directory. +// Data is deliberately retained: custom server database paths cannot be safely inferred. +func Uninstall(executable string, args []string, in io.Reader, out io.Writer) error { + f := flag.NewFlagSet("uninstall", flag.ContinueOnError) + f.SetOutput(out) + yes := f.Bool("yes", false, "Skip confirmation") + if err := f.Parse(args); err != nil { + if err == flag.ErrHelp { + return nil + } + return err + } + if len(f.Args()) > 0 { + return fmt.Errorf("uninstall takes no positional arguments") + } + resolved, err := filepath.EvalSymlinks(executable) + if err != nil { + return err + } + dir := filepath.Dir(resolved) + var files []string + for _, command := range []string{"pipelines", "pipelines-server", "pipelines-tui"} { + path := filepath.Join(dir, command) + info, err := buildinfo.ReadFile(path) + if os.IsNotExist(err) { + continue + } + if err != nil { + return fmt.Errorf("cannot verify %s; no binaries removed: %w", path, err) + } + if info.Path != "git.a-wan.net/alan/pipelines/cmd/"+command { + return fmt.Errorf("%s is not a Pipelines Go binary; no binaries removed", path) + } + files = append(files, path) + } + if !*yes { + fmt.Fprintf(out, "Remove %s? Data and drafts are retained. [y/N] ", strings.Join(files, ", ")) + answer, err := bufio.NewReader(in).ReadString('\n') + if err != nil { + return err + } + if strings.TrimSpace(strings.ToLower(answer)) != "y" { + return nil + } + } + for _, path := range files { + if err := os.Remove(path); err != nil { + return fmt.Errorf("remove %s: %w (use the permissions appropriate to your bin directory)", path, err) + } + } + fmt.Fprintln(out, "Removed Go binaries. Pipeline history, drafts, and OpenCode configuration were retained.") + return nil +} diff --git a/internal/opencode/connection.go b/internal/opencode/connection.go new file mode 100644 index 00000000..f04ae6c6 --- /dev/null +++ b/internal/opencode/connection.go @@ -0,0 +1,261 @@ +// Package opencode adapts the two published OpenCode API families without installing a CLI. +package opencode + +import ( + "bufio" + "bytes" + "context" + "crypto/rand" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "syscall" + "time" +) + +const MaxRequest = 64 << 10 +const MaxResponse = 1 << 20 + +type Connection struct { + Backend, Executable, Directory string + URL string + password string + process *exec.Cmd + done chan error + models map[string]map[string]string + responses map[string][]byte +} + +// Reference records only our ephemeral loopback service credentials, never provider auth. +// The private file lets crash recovery abort the same owned service, not a new instance. +func (c *Connection) Reference() (string, error) { + cache, err := os.UserCacheDir() + if err != nil { + return "", err + } + dir := filepath.Join(cache, "pipelines", "private") + if err = os.MkdirAll(dir, 0700); err != nil { + return "", err + } + f, err := os.CreateTemp(dir, "opencode-connection-*.json") + if err != nil { + return "", err + } + data, _ := json.Marshal(map[string]string{"url": c.URL, "password": c.password, "directory": c.Directory}) + _, writeErr := f.Write(data) + closeErr := f.Close() + if writeErr != nil { + return "", writeErr + } + if closeErr != nil { + return "", closeErr + } + return f.Name(), nil +} + +func FromReference(path string) (*Connection, error) { + info, err := os.Lstat(path) + if err != nil { + return nil, err + } + if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size()>MaxRequest { + return nil, fmt.Errorf("invalid private OpenCode connection reference") + } + if stat, ok := info.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Getuid()) { + return nil, fmt.Errorf("OpenCode reference belongs to another user") + } + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + var reference struct{ URL, Password, Directory string } + if err = json.Unmarshal(data, &reference); err != nil { + return nil, err + } + u, err := url.Parse(reference.URL) + if err != nil || u.Scheme != "http" || u.Hostname() != "127.0.0.1" || u.Port() == "" || u.Path != "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || len(reference.Password) != 64 { + return nil, fmt.Errorf("invalid owned OpenCode service endpoint") + } + return &Connection{Backend: "v1", URL: reference.URL, password: reference.Password, Directory: reference.Directory}, nil +} + +func Executable() string { + if path := os.Getenv("PIPELINES_OPENCODE"); path != "" { + return path + } + return "opencode" +} + +// Detect checks the advertised command interface, not an assumed release number. +func Detect(ctx context.Context, executable string) (string, error) { + cmd := exec.CommandContext(ctx, executable, "--help") + data, err := cmd.CombinedOutput() + if err != nil { + return "", fmt.Errorf("cannot run the selected OpenCode CLI: %w", err) + } + if regexp.MustCompile(`(?m)^\s*(?:opencode\s+)?api\s`).Match(data) { + return "v2", nil + } + if regexp.MustCompile(`(?m)^\s*(?:opencode\s+)?serve\s`).Match(data) { + return "v1", nil + } + return "", fmt.Errorf("selected OpenCode CLI advertises neither api nor serve") +} + +func Open(ctx context.Context, executable, directory, backend string) (*Connection, error) { + if executable == "" { + executable = Executable() + } + if directory == "" { + var err error + directory, err = os.Getwd() + if err != nil { + return nil, err + } + } + if backend == "" { + var err error + backend, err = Detect(ctx, executable) + if err != nil { + return nil, err + } + } + c := &Connection{Backend: backend, Executable: executable, Directory: directory, models: map[string]map[string]string{}, responses: map[string][]byte{}} + if backend == "v2" { + return c, nil + } + if backend != "v1" { + return nil, fmt.Errorf("unknown OpenCode backend %q", backend) + } + secret := make([]byte, 32) + if _, err := rand.Read(secret); err != nil { + return nil, err + } + c.password = hex.EncodeToString(secret) + c.process = exec.Command(executable, "serve", "--hostname", "127.0.0.1", "--port", "0") + c.process.Dir = directory + for _, entry := range os.Environ() { + if !strings.HasPrefix(entry, "OPENCODE_SERVER_PASSWORD=") && !strings.HasPrefix(entry, "OPENCODE_SERVER_USERNAME=") { + c.process.Env = append(c.process.Env, entry) + } + } + c.process.Env = append(c.process.Env, "OPENCODE_SERVER_PASSWORD="+c.password, "OPENCODE_SERVER_USERNAME=pipelines") + stdout, err := c.process.StdoutPipe() + if err != nil { + return nil, err + } + c.process.Stderr = io.Discard + if err = c.process.Start(); err != nil { + return nil, err + } + c.done = make(chan error, 1) + go func() { c.done <- c.process.Wait() }() + address := make(chan string, 1) + go func() { + scanner := bufio.NewScanner(stdout) + pattern := regexp.MustCompile(`http://127\.0\.0\.1:[0-9]+`) + sent := false + for scanner.Scan() { + if !sent { + if found := pattern.FindString(scanner.Text()); found != "" { + address <- found + sent = true + } + } + } + }() + startup, stop := context.WithTimeout(ctx, 20*time.Second) + defer stop() + select { + case c.URL = <-address: + case <-c.done: + c.process = nil + return nil, fmt.Errorf("OpenCode serve exited before becoming ready") + case <-startup.Done(): + c.Close() + return nil, fmt.Errorf("OpenCode serve startup: %w", startup.Err()) + } + health, err := c.Native(startup, "get", "/global/health", nil) + var result struct { + Healthy bool `json:"healthy"` + Version string `json:"version"` + } + if err != nil || json.Unmarshal(health, &result) != nil || !result.Healthy || result.Version == "" { + c.Close() + return nil, fmt.Errorf("OpenCode serve did not return the expected native health contract") + } + return c, nil +} + +func (c *Connection) Close() { + if c.process == nil { + return + } + _ = c.process.Process.Signal(syscall.SIGTERM) + select { + case <-c.done: + case <-time.After(5 * time.Second): + _ = c.process.Process.Kill() + <-c.done + } + c.process = nil +} + +func (c *Connection) Native(ctx context.Context, method, path string, body any) ([]byte, error) { + u, err := url.Parse(c.URL + path) + if err != nil { + return nil, err + } + query := u.Query() + query.Set("directory", c.Directory) + u.RawQuery = query.Encode() + var data []byte + if body != nil { + data, err = json.Marshal(body) + if err != nil { + return nil, err + } + if len(data) > MaxRequest { + return nil, fmt.Errorf("OpenCode request exceeds 64 KiB") + } + } + request, err := http.NewRequestWithContext(ctx, strings.ToUpper(method), u.String(), bytes.NewReader(data)) + if err != nil { + return nil, err + } + request.Header.Set("Content-Type", "application/json") + request.SetBasicAuth("pipelines", c.password) + response, err := http.DefaultClient.Do(request) + if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, fmt.Errorf("OpenCode native API request failed") + } + defer response.Body.Close() + if response.StatusCode < 200 || response.StatusCode >= 300 { + return nil, fmt.Errorf("OpenCode %s %s failed (HTTP %d)", method, path, response.StatusCode) + } + if !strings.HasPrefix(response.Header.Get("Content-Type"), "application/json") { + return nil, fmt.Errorf("OpenCode %s %s returned a non-JSON API response", method, path) + } + data, err = io.ReadAll(io.LimitReader(response.Body, MaxResponse+1)) + if err != nil { + return nil, err + } + if len(data) > MaxResponse { + return nil, fmt.Errorf("OpenCode response exceeds 1 MiB") + } + if !json.Valid(data) { + return nil, fmt.Errorf("OpenCode returned invalid JSON") + } + return data, nil +} diff --git a/internal/opencode/live_test.go b/internal/opencode/live_test.go new file mode 100644 index 00000000..d91452f3 --- /dev/null +++ b/internal/opencode/live_test.go @@ -0,0 +1,67 @@ +package opencode + +import ( + "context" + "encoding/json" + "os" + "testing" + "time" +) + +// This test does no inference and never uses or copies the user's login. +func TestStableNativeSessionContractLive(t *testing.T) { + executable := os.Getenv("PIPELINES_TEST_OPENCODE_V1") + if executable == "" { + t.Skip("set PIPELINES_TEST_OPENCODE_V1 for no-inference stable CLI contract test") + } + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", home+"/config") + t.Setenv("XDG_DATA_HOME", home+"/data") + t.Setenv("XDG_CACHE_HOME", home+"/cache") + t.Setenv("XDG_STATE_HOME", home+"/state") + ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second) + defer cancel() + c, err := Open(ctx, executable, home, "") + if err != nil { + t.Fatal(err) + } + defer c.Close() + if c.Backend != "v1" { + t.Fatal("expected native serve backend") + } + data, err := c.Call(ctx, "post", "/api/session", map[string]any{"title": "Pipelines contract test", "model": map[string]string{"providerID": "opencode-go", "id": "deepseek-v4.1-flash"}, "permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}, {"action": "read", "resource": "*", "effect": "allow"}, {"action": "shell", "resource": "go test ./...", "effect": "allow"}}}) + if err != nil { + t.Fatal(err) + } + var result struct { + Data struct { + ID string `json:"id"` + } `json:"data"` + } + json.Unmarshal(data, &result) + defer c.Call(context.Background(), "delete", "/api/session/"+result.Data.ID, nil) + data, err = c.Native(ctx, "get", "/session/"+result.Data.ID, nil) + if err != nil { + t.Fatal(err) + } + var session struct { + Permission []map[string]string `json:"permission"` + } + json.Unmarshal(data, &session) + if len(session.Permission) != 3 || session.Permission[2]["permission"] != "bash" { + t.Fatal("scoped native permission contract not retained", string(data)) + } + ref, err := c.Reference() + if err != nil { + t.Fatal(err) + } + defer os.Remove(ref) + recovered, err := FromReference(ref) + if err != nil { + t.Fatal(err) + } + if _, err = recovered.Call(ctx, "post", "/api/session/"+result.Data.ID+"/interrupt", nil); err != nil { + t.Fatal("cannot abort same owned service via durable reference", err) + } +} diff --git a/internal/opencode/native.go b/internal/opencode/native.go new file mode 100644 index 00000000..d574dd40 --- /dev/null +++ b/internal/opencode/native.go @@ -0,0 +1,183 @@ +package opencode + +import ( + "context" + "encoding/json" + "fmt" + "regexp" + "strings" + "path/filepath" +) + +var sessionID = regexp.MustCompile(`^ses[A-Za-z0-9_-]+$`) + +func Permissions(rules []map[string]string) []map[string]string { + result := make([]map[string]string, 0, len(rules)) + for _, rule := range rules { + permission := rule["action"] + if permission == "shell" { + permission = "bash" + } + result = append(result, map[string]string{"permission": permission, "pattern": rule["resource"], "action": rule["effect"]}) + } + return result +} + +func messageText(data []byte) (string, error) { + var message struct { + Info struct { + Role string `json:"role"` + Finish string `json:"finish"` + Error json.RawMessage `json:"error"` + Time struct { + Completed int64 `json:"completed"` + } `json:"time"` + } `json:"info"` + Parts []struct{ Type, Text string } `json:"parts"` + } + if json.Unmarshal(data, &message) != nil || message.Info.Role != "assistant" || message.Info.Finish != "stop" || message.Info.Time.Completed == 0 { + return "", fmt.Errorf("OpenCode returned no completed assistant result") + } + if len(message.Info.Error) > 0 && string(message.Info.Error) != "null" { + return "", fmt.Errorf("OpenCode agent reported an execution error") + } + var text string + for _, part := range message.Parts { + if part.Type == "text" { + text += part.Text + } + } + if text == "" { + return "", fmt.Errorf("OpenCode returned no assistant text") + } + return text, nil +} + +// Call adapts the pipeline's existing V2 command contract to the native stable API. +// Sessions still use the same account/database; only our loopback serve process is owned. +func (c *Connection) Call(ctx context.Context, method, path string, body any) ([]byte, error) { + if c.Backend != "v1" { + return nil, fmt.Errorf("native adapter requires the serve backend") + } + if path == "/api/session" && method == "post" { + data, _ := json.Marshal(body) + var request struct { + Title, Agent string + Model map[string]string + Permissions []map[string]string + } + if err := json.Unmarshal(data, &request); err != nil { + return nil, err + } + rules := request.Permissions + if rules == nil { + rules = []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}} + } + nativeRules:=Permissions(rules) + // V1 tool implementations can match absolute file paths; preserve the exact + // approved scope in both forms, never grant a workspace-wide edit wildcard. + var scoped []map[string]string + for _,rule:=range nativeRules{scoped=append(scoped,rule);if rule["permission"]=="edit"&&rule["action"]=="allow"&&!filepath.IsAbs(rule["pattern"]){scoped=append(scoped,map[string]string{"permission":"edit","action":"allow","pattern":filepath.Join(c.Directory,rule["pattern"])})}} + payload := map[string]any{"title": request.Title, "permission": scoped, "model": request.Model} + if request.Agent != "" { + payload["agent"] = request.Agent + } + created, err := c.Native(ctx, "post", "/session", payload) + if err != nil { + return nil, err + } + var session struct { + ID string `json:"id"` + } + if json.Unmarshal(created, &session) != nil || !sessionID.MatchString(session.ID) { + return nil, fmt.Errorf("OpenCode returned an invalid native session") + } + c.models[session.ID] = map[string]string{"providerID": request.Model["providerID"], "modelID": request.Model["id"]} + return json.Marshal(map[string]any{"data": map[string]string{"id": session.ID}}) + } + clean := strings.SplitN(path, "?", 2)[0] + parts := strings.Split(strings.TrimPrefix(clean, "/api/session/"), "/") + if strings.HasPrefix(clean, "/api/experimental/session/") { + parts = strings.Split(strings.TrimPrefix(clean, "/api/experimental/session/"), "/") + } + if len(parts) == 0 || !sessionID.MatchString(parts[0]) { + return nil, fmt.Errorf("unsupported native OpenCode operation") + } + id := parts[0] + if len(parts) == 1 && method == "delete" { + data, err := c.Native(ctx, "delete", "/session/"+id, nil) + if err != nil { + return nil, err + } + var confirmed bool + if json.Unmarshal(data,&confirmed)!=nil||!confirmed { + return nil, fmt.Errorf("OpenCode session deletion was not confirmed") + } + delete(c.models, id) + delete(c.responses, id) + return nil, nil + } + if len(parts) != 2 { + return nil, fmt.Errorf("unsupported native OpenCode operation") + } + switch parts[1] { + case "interrupt": + data, err := c.Native(ctx, "post", "/session/"+id+"/abort", nil) + if err != nil { + return nil, err + } + var confirmed bool + if json.Unmarshal(data,&confirmed)!=nil||!confirmed { + return nil, fmt.Errorf("OpenCode abort was not confirmed") + } + return nil, nil + case "generate", "prompt": + data, _ := json.Marshal(body) + var request map[string]string + if err := json.Unmarshal(data, &request); err != nil { + return nil, err + } + text := request["prompt"] + if text == "" { + text = request["text"] + } + response, err := c.Native(ctx, "post", "/session/"+id+"/message", map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}}) + if err != nil { + return nil, err + } + c.responses[id] = response + if parts[1] == "prompt" { + return nil, nil + } + result, err := messageText(response) + if err != nil { + return nil, err + } + return json.Marshal(map[string]any{"data": map[string]string{"text": result}}) + case "wait": + _, err := messageText(c.responses[id]) + return nil, err + case "message": + data, err := c.Native(ctx, "get", "/session/"+id+"/message", nil) + if err != nil { + return nil, err + } + var messages []struct { + Info map[string]any `json:"info"` + Parts []map[string]any `json:"parts"` + } + if json.Unmarshal(data, &messages) != nil { + return nil, fmt.Errorf("invalid native messages") + } + var converted []map[string]any + for i := len(messages) - 1; i >= 0; i-- { + m := messages[i] + if m.Info["role"] != "assistant" { + continue + } + converted = append(converted, map[string]any{"finish": m.Info["finish"], "error": m.Info["error"], "content": m.Parts, "native_info": m.Info}) + } + return json.Marshal(map[string]any{"data": converted}) + } + return nil, fmt.Errorf("unsupported native OpenCode operation") +} diff --git a/internal/opencode/native_test.go b/internal/opencode/native_test.go new file mode 100644 index 00000000..fec320ca --- /dev/null +++ b/internal/opencode/native_test.go @@ -0,0 +1,101 @@ +package opencode + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestNativeSessionModelPermissionsGenerationAndAbort(t *testing.T) { + var created, prompted, deleted, aborted bool + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.URL.Query().Get("directory") != "/repo" { + t.Error("workspace lost") + } + switch r.Method + " " + r.URL.Path { + case "POST /session": + var body struct { + Model map[string]string + Permission []map[string]string + } + json.NewDecoder(r.Body).Decode(&body) + if body.Model["id"] != "model" || body.Model["providerID"] != "provider" || body.Permission[0]["action"] != "deny" || body.Permission[1]["permission"] != "bash" { + t.Error("model or scoped permissions lost", body) + } + created = true + w.Write([]byte(`{"id":"ses_native"}`)) + case "POST /session/ses_native/message": + var body struct { + Model map[string]string + Parts []map[string]string + } + json.NewDecoder(r.Body).Decode(&body) + if body.Model["modelID"] != "model" || body.Parts[0]["text"] != "exact prompt" { + t.Error("prompt or message model changed", body) + } + prompted = true + w.Write([]byte(`{"info":{"role":"assistant","finish":"stop","time":{"completed":1}},"parts":[{"type":"text","text":"result"}]}`)) + case "GET /session/ses_native/message": + w.Write([]byte(`[{"info":{"role":"assistant","finish":"stop","time":{"completed":1}},"parts":[{"type":"text","text":"result"}]}]`)) + case "POST /session/ses_native/abort": + aborted = true + w.Write([]byte("true")) + case "DELETE /session/ses_native": + deleted = true + w.Write([]byte("true")) + default: + t.Error("unexpected native operation", r.Method, r.URL.Path) + } + })) + defer server.Close() + c := &Connection{Backend: "v1", URL: server.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}} + ctx := context.Background() + _, err := c.Call(ctx, "post", "/api/session", map[string]any{"model": map[string]string{"providerID": "provider", "id": "model"}, "permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}, {"action": "shell", "resource": "go test ./...", "effect": "allow"}}}) + if err != nil { + t.Fatal(err) + } + data, err := c.Call(ctx, "post", "/api/session/ses_native/generate", map[string]string{"prompt": "exact prompt"}) + if err != nil || !strings.Contains(string(data), "result") { + t.Fatal(string(data), err) + } + if _, err = c.Call(ctx, "post", "/api/experimental/session/ses_native/wait", nil); err != nil { + t.Fatal(err) + } + if _, err = c.Call(ctx, "get", "/api/session/ses_native/message?order=desc", nil); err != nil { + t.Fatal(err) + } + if _, err = c.Call(ctx, "post", "/api/session/ses_native/interrupt", nil); err != nil { + t.Fatal(err) + } + if _, err = c.Call(ctx, "delete", "/api/session/ses_native", nil); err != nil { + t.Fatal(err) + } + if !created || !prompted || !deleted || !aborted { + t.Fatal("native lifecycle incomplete") + } +} + +func TestHTMLBridgeIsNotAnAPIAndErrorsAreRedacted(t *testing.T) { + for _, status := range []int{200, 500} { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(status) + w.Write([]byte("secret-token HTML")) + })) + c := &Connection{URL: server.URL} + _, err := c.Native(context.Background(), "get", "/api/session", nil) + server.Close() + if err == nil || strings.Contains(err.Error(), "secret-token") { + t.Fatal(err) + } + } + for _, body := range []string{`{"info":{"role":"assistant","finish":"tool-calls","time":{"completed":1}},"parts":[]}`, `{"info":{"role":"assistant","finish":"stop"},"parts":[{"type":"text","text":"unfinished"}]}`, `{"info":{"role":"assistant","finish":"stop","time":{"completed":1},"error":{"message":"bad"}},"parts":[{"type":"text","text":"failed"}]}`} { + if _, err := messageText([]byte(body)); err == nil { + t.Fatal("unfinished/error response accepted") + } + } +} diff --git a/internal/paths/paths.go b/internal/paths/paths.go new file mode 100644 index 00000000..f186a9d8 --- /dev/null +++ b/internal/paths/paths.go @@ -0,0 +1,21 @@ +// Package paths centralizes standard user locations; callers can override them. +package paths + +import ( + "os" + "path/filepath" + "runtime" +) + +func Data() (string, error) { + if dir := os.Getenv("XDG_DATA_HOME"); dir != "" { + return filepath.Abs(filepath.Join(dir, "pipelines")) + } + if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { + dir, err := os.UserConfigDir() + return filepath.Join(dir, "pipelines"), err + } + home, err := os.UserHomeDir() + return filepath.Join(home, ".local", "share", "pipelines"), err +} +func Database() (string, error) { dir, err := Data(); return filepath.Join(dir, "pipelines.db"), err } diff --git a/internal/paths/paths_test.go b/internal/paths/paths_test.go new file mode 100644 index 00000000..04da3478 --- /dev/null +++ b/internal/paths/paths_test.go @@ -0,0 +1,15 @@ +package paths + +import ( + "path/filepath" + "testing" +) + +func TestDataLocationIsIndependentOfExecutable(t *testing.T) { + dir := t.TempDir() + t.Setenv("XDG_DATA_HOME", dir) + db, err := Database() + if err != nil || db != filepath.Join(dir, "pipelines", "pipelines.db") { + t.Fatal(db, err) + } +} diff --git a/internal/pipeline/definition.go b/internal/pipeline/definition.go index 2ef8b5d8..5d922394 100644 --- a/internal/pipeline/definition.go +++ b/internal/pipeline/definition.go @@ -2,8 +2,8 @@ package pipeline import ( "fmt" - "pipelines/internal/protocol" - "pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "regexp" "strings" ) diff --git a/internal/pipeline/definition_test.go b/internal/pipeline/definition_test.go index 087add70..9a73d019 100644 --- a/internal/pipeline/definition_test.go +++ b/internal/pipeline/definition_test.go @@ -1,7 +1,7 @@ package pipeline import ( - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "strings" "testing" ) diff --git a/internal/pipeline/examples_test.go b/internal/pipeline/examples_test.go index 4cd3bd88..edfbd3a7 100644 --- a/internal/pipeline/examples_test.go +++ b/internal/pipeline/examples_test.go @@ -5,7 +5,7 @@ import ( "path/filepath" "testing" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" ) func TestShowcaseDefinitions(t *testing.T) { diff --git a/internal/pipeline/nested_forks_test.go b/internal/pipeline/nested_forks_test.go index 7ea698dc..f62c6295 100644 --- a/internal/pipeline/nested_forks_test.go +++ b/internal/pipeline/nested_forks_test.go @@ -1,8 +1,8 @@ package pipeline import ( + "git.a-wan.net/alan/pipelines/internal/protocol" "os" - "pipelines/internal/protocol" "testing" ) diff --git a/internal/pipeline/notify.go b/internal/pipeline/notify.go index 6f02ba80..124c0502 100644 --- a/internal/pipeline/notify.go +++ b/internal/pipeline/notify.go @@ -2,7 +2,7 @@ package pipeline import ( "fmt" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "regexp" "strings" ) diff --git a/internal/pipeline/runs.go b/internal/pipeline/runs.go index 382447b3..ff568c89 100644 --- a/internal/pipeline/runs.go +++ b/internal/pipeline/runs.go @@ -7,7 +7,7 @@ import ( "encoding/json" "fmt" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" ) func (s *Store) initRuns() error { @@ -27,6 +27,9 @@ started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT, PRIMARY KE CREATE INDEX IF NOT EXISTS run_latest ON pipeline_runs(id DESC); CREATE TABLE IF NOT EXISTS step_notifications ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, receipts_json TEXT NOT NULL, +PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); +CREATE TABLE IF NOT EXISTS run_session_owners ( +run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, backend TEXT NOT NULL, reference TEXT NOT NULL DEFAULT '', PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));`) return err } @@ -211,8 +214,49 @@ func (s *Store) BeginStep(ctx context.Context, id int64, sequence int, key strin return tx.Commit() } -func (s *Store) RunSession(ctx context.Context, id int64, sequence int, session string, active bool) error { - return affected(s.db.ExecContext(ctx, "UPDATE run_steps SET session_id=?,agent_active=? WHERE run_id=? AND sequence=? AND status='running'", session, active, id, sequence)) +func (s *Store) RunSession(ctx context.Context, id int64, sequence int, session string, active bool, metadata ...string) error { + tx, err := s.db.BeginTx(ctx, nil) + if err != nil { + return err + } + defer tx.Rollback() + if err = affected(tx.ExecContext(ctx, "UPDATE run_steps SET session_id=?,agent_active=? WHERE run_id=? AND sequence=? AND status='running'", session, active, id, sequence)); err != nil { + return err + } + if len(metadata) > 0 { + backend := metadata[0] + if backend == "" { + backend = "v2" + } + reference := "" + if len(metadata) > 1 { + reference = metadata[1] + } + if _, err = tx.ExecContext(ctx, "INSERT INTO run_session_owners(run_id,sequence,backend,reference) VALUES(?,?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET backend=excluded.backend,reference=excluded.reference", id, sequence, backend, reference); err != nil { + return err + } + } + return tx.Commit() +} + +type SessionOwner struct{ ID, Backend, Directory, Reference string } + +func (s *Store) ActiveSessionOwners(ctx context.Context) ([]SessionOwner, error) { + rows, err := s.db.QueryContext(ctx, `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,'') +FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.agent_active=1 AND s.session_id<>''`) + if err != nil { + return nil, err + } + defer rows.Close() + var owners []SessionOwner + for rows.Next() { + var owner SessionOwner + if err = rows.Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference); err != nil { + return nil, err + } + owners = append(owners, owner) + } + return owners, rows.Err() } func (s *Store) FinishStep(ctx context.Context, id int64, step protocol.RunStep) error { diff --git a/internal/pipeline/schedule.go b/internal/pipeline/schedule.go index 2a9aacb4..c644e53a 100644 --- a/internal/pipeline/schedule.go +++ b/internal/pipeline/schedule.go @@ -2,7 +2,7 @@ package pipeline import ( "fmt" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "strings" "time" ) diff --git a/internal/pipeline/schedule_store.go b/internal/pipeline/schedule_store.go index bf1dd36a..4ce6e4e7 100644 --- a/internal/pipeline/schedule_store.go +++ b/internal/pipeline/schedule_store.go @@ -2,7 +2,7 @@ package pipeline import ( "context" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "time" ) diff --git a/internal/pipeline/schedule_test.go b/internal/pipeline/schedule_test.go index fdfcbd6c..6d53b175 100644 --- a/internal/pipeline/schedule_test.go +++ b/internal/pipeline/schedule_test.go @@ -1,7 +1,7 @@ package pipeline import ( - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "strings" "testing" "time" diff --git a/internal/pipeline/stage.go b/internal/pipeline/stage.go index b94e73d1..7f8d361f 100644 --- a/internal/pipeline/stage.go +++ b/internal/pipeline/stage.go @@ -5,7 +5,7 @@ import ( "sort" "strings" - "pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/stage" ) // StageDefinition adapts normalized/persisted graph data for type implementations. diff --git a/internal/pipeline/stage_test.go b/internal/pipeline/stage_test.go index e933ea5d..dfaf0ddc 100644 --- a/internal/pipeline/stage_test.go +++ b/internal/pipeline/stage_test.go @@ -6,8 +6,8 @@ import ( "strings" "testing" - "pipelines/internal/protocol" - "pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" ) type constantStage struct{} diff --git a/internal/pipeline/store.go b/internal/pipeline/store.go index 67c7c463..d2c64639 100644 --- a/internal/pipeline/store.go +++ b/internal/pipeline/store.go @@ -6,8 +6,8 @@ import ( "database/sql" "encoding/json" "fmt" - "pipelines/internal/protocol" - "pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "strings" "sync" "time" diff --git a/internal/pipeline/store_test.go b/internal/pipeline/store_test.go index ed874788..4f21d15e 100644 --- a/internal/pipeline/store_test.go +++ b/internal/pipeline/store_test.go @@ -3,9 +3,9 @@ package pipeline import ( "context" "encoding/json" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "path/filepath" - "pipelines/internal/protocol" - "pipelines/internal/stage" "strings" "sync" "testing" diff --git a/internal/pipeline/tools_test.go b/internal/pipeline/tools_test.go index 2b2b5701..0952dcdb 100644 --- a/internal/pipeline/tools_test.go +++ b/internal/pipeline/tools_test.go @@ -2,8 +2,8 @@ package pipeline import ( "context" + "git.a-wan.net/alan/pipelines/internal/stage" "path/filepath" - "pipelines/internal/stage" "strings" "testing" ) diff --git a/internal/server/cli_test.go b/internal/server/cli_test.go index f1a76440..64e34e7b 100644 --- a/internal/server/cli_test.go +++ b/internal/server/cli_test.go @@ -4,12 +4,12 @@ import ( "bytes" "context" "encoding/json" + "git.a-wan.net/alan/pipelines/internal/cli" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "net/http/httptest" "path/filepath" - "pipelines/internal/cli" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/stage" "strings" "testing" ) diff --git a/internal/server/notification_setup.go b/internal/server/notification_setup.go index 0227bc57..77c54d28 100644 --- a/internal/server/notification_setup.go +++ b/internal/server/notification_setup.go @@ -1,9 +1,9 @@ package server import ( + "git.a-wan.net/alan/pipelines/internal/protocol" "net/url" "os" - "pipelines/internal/protocol" "regexp" "strings" ) diff --git a/internal/server/notify.go b/internal/server/notify.go index 67129b5e..3cf68541 100644 --- a/internal/server/notify.go +++ b/internal/server/notify.go @@ -3,12 +3,12 @@ package server import ( "context" "fmt" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "io" "log" "os" "path/filepath" - "pipelines/internal/protocol" - "pipelines/internal/stage" "strings" "syscall" "time" diff --git a/internal/server/notify_test.go b/internal/server/notify_test.go index 6dc036f7..c9ce89d0 100644 --- a/internal/server/notify_test.go +++ b/internal/server/notify_test.go @@ -4,15 +4,15 @@ import ( "context" "encoding/json" "fmt" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "io" "mime" "net/http" "net/http/httptest" "os" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/stage" "strings" "sync/atomic" "syscall" diff --git a/internal/server/ntfy_test.go b/internal/server/ntfy_test.go index 652ce81b..49659293 100644 --- a/internal/server/ntfy_test.go +++ b/internal/server/ntfy_test.go @@ -2,12 +2,12 @@ package server import ( "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" "io" "net/http" "net/http/httptest" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" "testing" ) diff --git a/internal/server/run_websocket_test.go b/internal/server/run_websocket_test.go index 1b55919f..73ab0880 100644 --- a/internal/server/run_websocket_test.go +++ b/internal/server/run_websocket_test.go @@ -8,11 +8,11 @@ import ( "testing" "time" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "github.com/coder/websocket" "github.com/coder/websocket/wsjson" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/stage" ) func TestRunWebSocketRemainsResponsiveAndAuditable(t *testing.T) { diff --git a/internal/server/runner.go b/internal/server/runner.go index 6280359e..b268d42b 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -10,9 +10,9 @@ import ( "sync" "time" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" ) type runner struct { @@ -27,16 +27,16 @@ type runner struct { } func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) { - sessions, err := s.ActiveAgentSessions(ctx) + sessions, err := s.ActiveSessionOwners(ctx) if err != nil { return nil, err } - for _, id := range sessions { + for _, owner := range sessions { stop, cancel := context.WithTimeout(ctx, 20*time.Second) - err := (stage.OpenCodeClient{}).Interrupt(stop, id) + err := (stage.OpenCodeClient{Backend: owner.Backend, Directory: owner.Directory, Reference: owner.Reference}).Interrupt(stop, owner.ID) cancel() if err != nil { - return nil, fmt.Errorf("cannot interrupt previously active agent %s: %w", id, err) + return nil, fmt.Errorf("cannot interrupt previously active agent %s: %w", owner.ID, err) } } if err := s.RecoverRuns(ctx); err != nil { @@ -241,7 +241,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, client.OnSession = func(session string, active bool) error { c, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - return r.store.RunSession(c, id, sequence, session, active) + return r.store.RunSession(c, id, sequence, session, active, client.Backend, client.Reference) } var result stage.Result var err error diff --git a/internal/server/runner_test.go b/internal/server/runner_test.go index 6f8312f9..011b970a 100644 --- a/internal/server/runner_test.go +++ b/internal/server/runner_test.go @@ -7,9 +7,9 @@ import ( "strings" "testing" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/stage" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" ) func TestRunnerRoutingPersistenceAndIdempotency(t *testing.T) { diff --git a/internal/server/schedule_websocket_test.go b/internal/server/schedule_websocket_test.go index 4fde21a1..c88af102 100644 --- a/internal/server/schedule_websocket_test.go +++ b/internal/server/schedule_websocket_test.go @@ -2,12 +2,12 @@ package server import ( "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/coder/websocket" "github.com/coder/websocket/wsjson" "net/http/httptest" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" "strings" "testing" "time" diff --git a/internal/server/scheduler.go b/internal/server/scheduler.go index 23e53ce1..11b7ec3a 100644 --- a/internal/server/scheduler.go +++ b/internal/server/scheduler.go @@ -2,9 +2,9 @@ package server import ( "fmt" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" "log" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" "time" ) diff --git a/internal/server/scheduler_test.go b/internal/server/scheduler_test.go index f7fec435..6b3fb848 100644 --- a/internal/server/scheduler_test.go +++ b/internal/server/scheduler_test.go @@ -2,10 +2,10 @@ package server import ( "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/stage" "os" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/stage" "strings" "testing" "time" diff --git a/internal/server/templates_test.go b/internal/server/templates_test.go index 079c9f6a..d9cb55c2 100644 --- a/internal/server/templates_test.go +++ b/internal/server/templates_test.go @@ -2,12 +2,12 @@ package server import ( "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/coder/websocket" "github.com/coder/websocket/wsjson" "net/http/httptest" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" "strings" "testing" "time" diff --git a/internal/server/tools_test.go b/internal/server/tools_test.go index 31f3e430..cd829c17 100644 --- a/internal/server/tools_test.go +++ b/internal/server/tools_test.go @@ -2,10 +2,10 @@ package server import ( "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/stage" "strings" "testing" "time" diff --git a/internal/server/websocket.go b/internal/server/websocket.go index e67c1282..bb2acf90 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -10,9 +10,9 @@ import ( "github.com/coder/websocket" "github.com/coder/websocket/wsjson" - "pipelines/examples" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/examples" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" ) // Handler serves the persistent connection. Canceling ctx stops active clients. diff --git a/internal/server/websocket_test.go b/internal/server/websocket_test.go index b5fbe3ca..daa15cd0 100644 --- a/internal/server/websocket_test.go +++ b/internal/server/websocket_test.go @@ -13,7 +13,7 @@ import ( "github.com/coder/websocket" "github.com/coder/websocket/wsjson" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" ) type logBuffer struct { diff --git a/internal/server/workspaces_test.go b/internal/server/workspaces_test.go index bb5041fa..7ce87a6a 100644 --- a/internal/server/workspaces_test.go +++ b/internal/server/workspaces_test.go @@ -2,11 +2,11 @@ package server import ( "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" "os" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/stage" "testing" ) diff --git a/internal/stage/agent.go b/internal/stage/agent.go index 4eb96510..610c8eae 100644 --- a/internal/stage/agent.go +++ b/internal/stage/agent.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "fmt" + "git.a-wan.net/alan/pipelines/internal/opencode" + "os" "strings" "time" ) @@ -23,11 +25,35 @@ func (c OpenCodeClient) Interrupt(ctx context.Context, id string) error { if !sessionIDPattern.MatchString(id) { return fmt.Errorf("invalid session ID") } + if c.command == nil && c.connection == nil { + var connection *opencode.Connection + var err error + if c.Backend == "v1" && c.Reference != "" { + connection, err = opencode.FromReference(c.Reference) + } else { + connection, err = opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend) + } + if err != nil { + return err + } + defer connection.Close() + c.connection = connection + c.Backend = connection.Backend + } _, err := c.call(ctx, "post", "/api/session/"+id+"/interrupt", map[string]any{}) return err } func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) { + if c.command == nil { + connection, err := opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend) + if err != nil { + return "", err + } + defer connection.Close() + c.connection = connection + c.Backend = connection.Backend + } rules := c.permissionRules() data, err := c.call(ctx, "post", "/api/session", map[string]any{"title": "Pipeline audited agent stage", "agent": "build", "location": map[string]string{"directory": c.Directory}, "model": map[string]string{"providerID": config.Provider, "id": config.Model}, "permissions": rules}) if err != nil { @@ -42,8 +68,22 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s return "", fmt.Errorf("invalid agent session") } c.SessionID = created.Data.ID + if c.connection != nil && c.Backend == "v1" { + c.Reference, err = c.connection.Reference() + if err != nil { + cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second) + defer cancel() + _, _ = c.call(cleanup, "delete", "/api/session/"+c.SessionID, nil) + return "", err + } + } if c.OnSession != nil { if err := c.OnSession(c.SessionID, true); err != nil { + cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second) + defer cancel() + if _, cleanupErr := c.call(cleanup, "delete", "/api/session/"+c.SessionID, nil); cleanupErr != nil { + return "", fmt.Errorf("agent ownership could not be recorded and session cleanup failed") + } return "", err } } @@ -60,8 +100,12 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s if persistErr := c.OnSession(c.SessionID, false); persistErr != nil { err = persistErr text = "" + return } } + if c.Reference != "" { + _ = os.Remove(c.Reference) + } }() tools := c.Tools if tools == nil { diff --git a/internal/stage/opencode.go b/internal/stage/opencode.go index 7be9f8d1..6f98c3b4 100644 --- a/internal/stage/opencode.go +++ b/internal/stage/opencode.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "git.a-wan.net/alan/pipelines/internal/opencode" "io" "os" "os/exec" @@ -17,13 +18,16 @@ import ( const maxModelRequest = 64 << 10 // CLI request body is passed as one OS argument. const maxModelResponse = 1 << 20 -// OpenCodeClient reuses OpenCode V2's service discovery and authenticated accounts. +// OpenCodeClient reuses the selected OpenCode CLI and its authenticated accounts. // No API keys are copied into pipeline definitions, subprocess arguments, or logs. // A fresh location-scoped session avoids unrelated user conversation history and // is deleted after generation. Generation is text-only: no agent tools are run. type OpenCodeClient struct { - Directory string // empty uses the pipeline server's working directory - command func(context.Context, string, ...string) ([]byte, error) + Directory string // empty uses the pipeline server's working directory + Backend string + Reference string + connection *opencode.Connection + command func(context.Context, string, ...string) ([]byte, error) } type openCodeError struct { @@ -48,6 +52,20 @@ var sessionIDPattern = regexp.MustCompile(`^ses[A-Za-z0-9_-]+$`) var httpStatusPattern = regexp.MustCompile(`HTTP (\d{3})`) func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) ([]byte, error) { + if c.connection != nil && c.connection.Backend == "v1" { + data, err := c.connection.Call(ctx, method, path, body) + if err != nil { + status := 0 + if match := httpStatusPattern.FindStringSubmatch(err.Error()); len(match) > 0 { + status, _ = strconv.Atoi(match[1]) + } + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, &openCodeError{operation: method + " " + path, status: status, cause: err} + } + return data, nil + } args := []string{"api", method, path} if body != nil { data, err := json.Marshal(body) @@ -70,7 +88,7 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) if c.command != nil { return c.command(ctx, directory, args...) } - cmd := exec.CommandContext(ctx, "opencode", args...) + cmd := exec.CommandContext(ctx, opencode.Executable(), args...) cmd.Dir = directory stdout := &boundedBuffer{limit: maxModelResponse} stderr := &boundedBuffer{limit: 8 << 10} @@ -94,6 +112,15 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) } func (c OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) { + if c.command == nil { + connection, err := opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend) + if err != nil { + return "", err + } + defer connection.Close() + c.connection = connection + c.Backend = connection.Backend + } directory := c.Directory if directory == "" { directory, err = os.Getwd() diff --git a/internal/stage/tools.go b/internal/stage/tools.go index 500a917f..625360ca 100644 --- a/internal/stage/tools.go +++ b/internal/stage/tools.go @@ -33,7 +33,7 @@ func ToolNames(def Definition) ([]string, bool, error) { return tools, true, nil } -var verificationCommands = []string{"go test ./...", "go test -count=1 ./...", "go test -race ./...", "go test -race -count=1 ./...", "go vet ./...", "go build ./cmd/server ./cmd/tui"} +var verificationCommands = []string{"go test ./...", "go test -count=1 ./...", "go test -race ./...", "go test -race -count=1 ./...", "go vet ./...", "go build ./cmd/..."} func (c *AgentClient) permissionRules() []map[string]string { rules := []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}} diff --git a/internal/tui/cli_run.go b/internal/tui/cli_run.go index e2aa3d94..4425204a 100644 --- a/internal/tui/cli_run.go +++ b/internal/tui/cli_run.go @@ -3,9 +3,9 @@ package tui import ( "context" "fmt" + "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/sahilm/fuzzy" - "pipelines/internal/protocol" "strings" ) diff --git a/internal/tui/cli_run_test.go b/internal/tui/cli_run_test.go index 649758ff..5133beb2 100644 --- a/internal/tui/cli_run_test.go +++ b/internal/tui/cli_run_test.go @@ -2,10 +2,10 @@ package tui import ( "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" "strings" "testing" ) diff --git a/internal/tui/create.go b/internal/tui/create.go index 9c9c3aa1..0ddadf0c 100644 --- a/internal/tui/create.go +++ b/internal/tui/create.go @@ -13,10 +13,10 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/client" - "pipelines/internal/draft" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/client" + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" ) type editedMsg struct { diff --git a/internal/tui/create_test.go b/internal/tui/create_test.go index 67070578..186be944 100644 --- a/internal/tui/create_test.go +++ b/internal/tui/create_test.go @@ -12,8 +12,8 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/pipeline" - "pipelines/internal/server" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/server" ) func TestCreateAndSubmit(t *testing.T) { diff --git a/internal/tui/dashboard.go b/internal/tui/dashboard.go index df8e0754..1c2bd131 100644 --- a/internal/tui/dashboard.go +++ b/internal/tui/dashboard.go @@ -10,7 +10,7 @@ import ( "github.com/charmbracelet/x/ansi" "github.com/sahilm/fuzzy" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" ) var commands = []string{":logs", ":new", ":templates", ":refresh", ":ping", ":edit", ":submit", ":versions", ":run", ":runs", ":home", ":quit"} diff --git a/internal/tui/dashboard_test.go b/internal/tui/dashboard_test.go index 51f4295d..194cbdc2 100644 --- a/internal/tui/dashboard_test.go +++ b/internal/tui/dashboard_test.go @@ -10,7 +10,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" ) func press(m Model, key string) Model { diff --git a/internal/tui/examples_test.go b/internal/tui/examples_test.go index bbb1e41c..f10bd770 100644 --- a/internal/tui/examples_test.go +++ b/internal/tui/examples_test.go @@ -6,8 +6,8 @@ import ( "strings" "testing" + "git.a-wan.net/alan/pipelines/internal/pipeline" "github.com/charmbracelet/lipgloss" - "pipelines/internal/pipeline" ) func TestShowcaseGraphsRender(t *testing.T) { diff --git a/internal/tui/graph.go b/internal/tui/graph.go index b05290dd..6553da2b 100644 --- a/internal/tui/graph.go +++ b/internal/tui/graph.go @@ -5,9 +5,9 @@ import ( "sort" "strings" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/protocol" ) func outcomes(stage protocol.Stage) []string { diff --git a/internal/tui/graph_forks.go b/internal/tui/graph_forks.go index c0c51ea9..a0c2fd45 100644 --- a/internal/tui/graph_forks.go +++ b/internal/tui/graph_forks.go @@ -1,7 +1,7 @@ package tui import ( - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "sort" ) diff --git a/internal/tui/graph_forks_test.go b/internal/tui/graph_forks_test.go index 8a6d4a12..a943eebf 100644 --- a/internal/tui/graph_forks_test.go +++ b/internal/tui/graph_forks_test.go @@ -6,8 +6,8 @@ import ( "strings" "testing" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" ) func nestedForkGraph(t *testing.T) protocol.Graph { diff --git a/internal/tui/graph_layout.go b/internal/tui/graph_layout.go index b9eef9a3..6c4c0019 100644 --- a/internal/tui/graph_layout.go +++ b/internal/tui/graph_layout.go @@ -2,7 +2,7 @@ package tui import ( "fmt" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "sort" ) diff --git a/internal/tui/graph_test.go b/internal/tui/graph_test.go index 016dddb8..e45f39b9 100644 --- a/internal/tui/graph_test.go +++ b/internal/tui/graph_test.go @@ -6,8 +6,8 @@ import ( "strings" "testing" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" ) func TestRenderGraphWithCyclesAndDefaults(t *testing.T) { diff --git a/internal/tui/highlights_test.go b/internal/tui/highlights_test.go index 4ca2e587..a72ae70f 100644 --- a/internal/tui/highlights_test.go +++ b/internal/tui/highlights_test.go @@ -7,11 +7,11 @@ import ( "strings" "testing" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "github.com/muesli/termenv" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" ) var sgr = regexp.MustCompile(`\x1b\[([0-9;]*)m`) diff --git a/internal/tui/model.go b/internal/tui/model.go index 64c5edff..543f1382 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -18,8 +18,8 @@ import ( "github.com/charmbracelet/x/ansi" "github.com/coder/websocket" - "pipelines/internal/client" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/client" + "git.a-wan.net/alan/pipelines/internal/protocol" ) type connectedMsg struct{ conn *websocket.Conn } diff --git a/internal/tui/model_test.go b/internal/tui/model_test.go index d449bfab..c6fad8bf 100644 --- a/internal/tui/model_test.go +++ b/internal/tui/model_test.go @@ -11,7 +11,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/server" + "git.a-wan.net/alan/pipelines/internal/server" ) func TestTKeyPingRoundTrip(t *testing.T) { diff --git a/internal/tui/notification_setup_test.go b/internal/tui/notification_setup_test.go index 9ec46b1a..5d789f5e 100644 --- a/internal/tui/notification_setup_test.go +++ b/internal/tui/notification_setup_test.go @@ -2,8 +2,8 @@ package tui import ( "context" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/x/ansi" - "pipelines/internal/protocol" "strings" "testing" ) diff --git a/internal/tui/panels.go b/internal/tui/panels.go index a948c362..97ae1eb8 100644 --- a/internal/tui/panels.go +++ b/internal/tui/panels.go @@ -9,7 +9,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" ) type paneGeometry struct { diff --git a/internal/tui/panels_test.go b/internal/tui/panels_test.go index b15b5a37..e3891474 100644 --- a/internal/tui/panels_test.go +++ b/internal/tui/panels_test.go @@ -10,8 +10,8 @@ import ( "github.com/charmbracelet/x/ansi" "github.com/muesli/termenv" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" ) func panelModel(t *testing.T) Model { diff --git a/internal/tui/picker.go b/internal/tui/picker.go index 396104d3..5d1824e4 100644 --- a/internal/tui/picker.go +++ b/internal/tui/picker.go @@ -3,10 +3,10 @@ package tui import ( "context" "fmt" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/sahilm/fuzzy" - "pipelines/internal/protocol" "strings" ) diff --git a/internal/tui/pipeline.go b/internal/tui/pipeline.go index 0413a4b8..d29eee90 100644 --- a/internal/tui/pipeline.go +++ b/internal/tui/pipeline.go @@ -9,9 +9,9 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/sahilm/fuzzy" - "pipelines/internal/draft" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" ) func selectionWindow(count, index, capacity int) (int, int) { diff --git a/internal/tui/pipeline_test.go b/internal/tui/pipeline_test.go index 5d684cf8..642da10c 100644 --- a/internal/tui/pipeline_test.go +++ b/internal/tui/pipeline_test.go @@ -12,9 +12,9 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/server" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/server" ) func TestSearchVersionsAndEditSubmission(t *testing.T) { diff --git a/internal/tui/run_detail.go b/internal/tui/run_detail.go index 4e91845b..527ab3c5 100644 --- a/internal/tui/run_detail.go +++ b/internal/tui/run_detail.go @@ -8,9 +8,9 @@ import ( "strings" "time" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/protocol" ) func (m *Model) closeRuns() { diff --git a/internal/tui/run_progress.go b/internal/tui/run_progress.go index 01e1665d..2fd0a82d 100644 --- a/internal/tui/run_progress.go +++ b/internal/tui/run_progress.go @@ -1,8 +1,8 @@ package tui import ( + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" - "pipelines/internal/protocol" ) // Animation is driven by a local timer, never by extra server requests. diff --git a/internal/tui/run_progress_test.go b/internal/tui/run_progress_test.go index 0d1ec647..44f1bed6 100644 --- a/internal/tui/run_progress_test.go +++ b/internal/tui/run_progress_test.go @@ -2,13 +2,13 @@ package tui import ( "context" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/server" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" "net/http/httptest" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" - "pipelines/internal/server" "strings" "testing" "time" diff --git a/internal/tui/runs.go b/internal/tui/runs.go index 72a64d42..e3cd856e 100644 --- a/internal/tui/runs.go +++ b/internal/tui/runs.go @@ -5,10 +5,10 @@ import ( "strings" "time" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/bubbles/textarea" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" - "pipelines/internal/protocol" ) type runTick struct{} diff --git a/internal/tui/runs_test.go b/internal/tui/runs_test.go index f961fd84..ecf1566f 100644 --- a/internal/tui/runs_test.go +++ b/internal/tui/runs_test.go @@ -2,10 +2,10 @@ package tui import ( "context" + "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/protocol" "strings" "testing" ) diff --git a/internal/tui/schedule_test.go b/internal/tui/schedule_test.go index 166ba673..d5cb45c9 100644 --- a/internal/tui/schedule_test.go +++ b/internal/tui/schedule_test.go @@ -1,7 +1,7 @@ package tui import ( - "pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/protocol" "strings" "testing" ) diff --git a/internal/tui/templates.go b/internal/tui/templates.go index b382c0be..e37f062f 100644 --- a/internal/tui/templates.go +++ b/internal/tui/templates.go @@ -2,12 +2,12 @@ package tui import ( "fmt" + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/glamour" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" - "pipelines/internal/draft" - "pipelines/internal/protocol" "strings" ) diff --git a/internal/tui/templates_test.go b/internal/tui/templates_test.go index 386f580d..82aa7b70 100644 --- a/internal/tui/templates_test.go +++ b/internal/tui/templates_test.go @@ -2,12 +2,12 @@ package tui import ( "context" + "git.a-wan.net/alan/pipelines/examples" + "git.a-wan.net/alan/pipelines/internal/protocol" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" "os" "path/filepath" - "pipelines/examples" - "pipelines/internal/protocol" "strings" "testing" ) diff --git a/internal/tui/theme.go b/internal/tui/theme.go index 61ec14bd..e2831507 100644 --- a/internal/tui/theme.go +++ b/internal/tui/theme.go @@ -1,10 +1,10 @@ package tui import ( + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/lipgloss" - "pipelines/internal/protocol" "sort" ) diff --git a/internal/tui/ux_review_test.go b/internal/tui/ux_review_test.go index 90943dcc..469ee0e7 100644 --- a/internal/tui/ux_review_test.go +++ b/internal/tui/ux_review_test.go @@ -3,11 +3,11 @@ package tui import ( "context" "fmt" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/x/ansi" "os" "path/filepath" - "pipelines/internal/pipeline" - "pipelines/internal/protocol" "strings" "testing" ) diff --git a/scripts/install.py b/scripts/install.py index c350832f..2bf00864 100644 --- a/scripts/install.py +++ b/scripts/install.py @@ -1,306 +1,10 @@ #!/usr/bin/env python3 -"""Install a clean native Pipelines service and authenticated Docker ntfy.""" -import argparse -import getpass -import hashlib -import importlib.util -import json -import os +"""Compatibility entry point: install Go binaries only, with standard Go paths.""" from pathlib import Path -import platform -import plistlib -import re -import secrets -import shutil -import socket import subprocess import sys -from manage import DEFAULT_PREFIX, MARKER, compose, load, run, start, wait_health - -SOURCE = Path(__file__).resolve().parents[1] -NAME = re.compile(r"^[A-Za-z][A-Za-z0-9_-]{0,63}$") -TOPIC = re.compile(r"^[-_A-Za-z0-9]{1,64}$") - - -def choose_port(preferred): - for port in range(preferred, min(preferred + 100, 65536)): - with socket.socket() as sock: - try: - sock.bind(("0.0.0.0", port)) - return port - except OSError: - pass - raise ValueError("No available local service port") - - -def default_host(): - tailscale = shutil.which("tailscale") - if tailscale: - try: - data = json.loads(run([tailscale, "status", "--json"], quiet=True, timeout=10).stdout) - if data.get("BackendState") == "Running": - return next(ip for ip in data["TailscaleIPs"] if ":" not in ip) - except (RuntimeError, ValueError, KeyError, StopIteration, subprocess.SubprocessError): - pass - with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: - try: - sock.connect(("192.0.2.1", 80)) - return sock.getsockname()[0] - except OSError: - return "127.0.0.1" - - -def timezone(): - spec = importlib.util.spec_from_file_location("configure", SOURCE / "scripts/configure-compose.py") - module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module) - return module.host_timezone() - - -def write_private(path, text): - path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) - with path.open("x") as file: - file.write(text) - path.chmod(0o600) - - -def ntfy_compose(config): - # JSON scalars are valid YAML scalars and prevent interpolation/injection. - q = lambda value: json.dumps(str(value)) - return f'''services: - ntfy: - image: binwiederhier/ntfy:v2.29.0 - command: serve - restart: unless-stopped - environment: - TZ: {q(config['timezone'])} - NTFY_BASE_URL: {q(config['public_url'])} - NTFY_LISTEN_HTTP: ":80" - NTFY_CACHE_FILE: /var/lib/ntfy/cache.db - NTFY_CACHE_DURATION: 168h - NTFY_AUTH_FILE: /var/lib/ntfy/auth.db - NTFY_AUTH_DEFAULT_ACCESS: deny-all - NTFY_ENABLE_LOGIN: "true" - NTFY_ATTACHMENT_CACHE_DIR: /var/lib/ntfy/attachments - NTFY_ATTACHMENT_EXPIRY_DURATION: 168h - NTFY_ATTACHMENT_TOTAL_SIZE_LIMIT: 1G - NTFY_ATTACHMENT_FILE_SIZE_LIMIT: 1M - ports: - - "0.0.0.0:{config['ntfy_port']}:80" - volumes: - - ntfy-data:/var/lib/ntfy -volumes: - ntfy-data: -''' - - -def make_registration(config): - root = Path(config["prefix"]) - command = [config["python"], str(root / "lib/manage.py"), "supervise", "--prefix", str(root)] - if platform.system() == "Darwin": - path = Path.home() / "Library/LaunchAgents" / (config["label"] + ".plist") - path.parent.mkdir(parents=True, exist_ok=True) - if path.exists(): - raise ValueError("Service registration already exists; refusing to overwrite") - with path.open("wb") as file: - plistlib.dump({"Label": config["label"], "ProgramArguments": command, - "RunAtLoad": True, "KeepAlive": {"SuccessfulExit": False}, - "ExitTimeOut": 75, "ThrottleInterval": 10, - "StandardOutPath": str(root / "logs/supervisor.log"), - "StandardErrorPath": str(root / "logs/supervisor.log")}, file) - else: - path = Path.home() / ".config/systemd/user" / (config["label"] + ".service") - path.parent.mkdir(parents=True, exist_ok=True) - if path.exists(): - raise ValueError("Service registration already exists; refusing to overwrite") - # systemd quotes its own command arguments, not shell strings. - quote = lambda value: '"' + value.replace("\\", "\\\\").replace('"', '\\"').replace("%", "%%") + '"' - path.write_text("[Unit]\nDescription=Pipelines server and ntfy\nAfter=default.target\n\n" - "[Service]\nType=simple\nExecStart=" + " ".join(map(quote, command)) + - "\nRestart=on-failure\nRestartSec=10\nTimeoutStopSec=90\n\n[Install]\nWantedBy=default.target\n") - return str(path) - - -def install(args): - if platform.system() not in ("Darwin", "Linux"): - raise ValueError("Supported: macOS and Linux (run as your normal user, not root)") - if os.getuid() == 0: - raise ValueError("Do not use sudo: this install reuses your user's OpenCode account") - root = args.prefix.expanduser().resolve() - if root in (Path("/"), Path.home().resolve(), SOURCE): - raise ValueError("Choose a dedicated installation directory") - docker = shutil.which("docker") - if not docker: - raise ValueError("Install/start Docker Desktop or Docker Engine first") - run([docker, "info", "--format", "{{.ServerVersion}}"], quiet=True, timeout=15) - run([docker, "compose", "version"], quiet=True, timeout=15) - if (root / MARKER).exists() and not load(root).get("uninstalled"): - config = load(root) - if not config.get("bootstrapped"): - raise ValueError("A partial installation exists; inspect it or run uninstall.sh --prefix ... --purge before retrying") - start(config) - print(f"Existing installation started; settings/history preserved: {root}") - return - retained = load(root) if (root / MARKER).exists() else None - if root.exists() and any(root.iterdir()) and retained is None: - raise ValueError("Installation directory is not empty; refusing to overwrite unrelated files") - if not args.no_autostart and platform.system() == "Linux": - run(["systemctl", "--user", "show-environment"], quiet=True) - opencode = args.opencode or shutil.which("opencode") or shutil.which("opencode2") - if not opencode: - npm=shutil.which("npm") - if not npm: - raise ValueError("Install OpenCode V2 first (npm install -g @opencode/cli), or install npm for automatic CLI setup") - if not args.yes and input("OpenCode V2 is missing. Install its pinned CLI privately using npm? [Y/n] ").strip().lower()=="n": - raise ValueError("OpenCode installation canceled") - root.mkdir(parents=True,exist_ok=True,mode=0o700) - run([npm,"install","--prefix",str(root / "tools"),"@opencode/cli@2.0.20"],quiet=True,timeout=240) - opencode=str(root / "tools/node_modules/.bin/opencode") - opencode = str(Path(opencode).resolve()) - version = run([opencode, "--version"], quiet=True, timeout=15).stdout - if not re.search(r"(?:^|\s)v?2\.\d+", version): - raise ValueError("OpenCode V2 is required; your existing login/config will be reused, never exported") - auth = run([opencode, "auth", "list"], quiet=True, timeout=30).stdout - if "OpenCode Go" not in auth: - if args.yes: - raise ValueError("Connect OpenCode Go first: opencode auth login") - print("OpenCode Go is not connected. Complete your existing OpenCode login now.") - subprocess.run([opencode, "auth", "login"], check=True) - auth = run([opencode, "auth", "list"], quiet=True, timeout=30).stdout - if "OpenCode Go" not in auth: - raise ValueError("OpenCode Go login not confirmed; installation canceled") - # This exercises discovery/start as the same user, without generating model output. - run([opencode, "api", "get", "/api/info"], quiet=True, timeout=30) - root.mkdir(parents=True, exist_ok=True, mode=0o700) - root.chmod(0o700) - for directory in ("bin", "lib", "data", "logs", "secrets", "workspace"): - (root / directory).mkdir(exist_ok=True, mode=0o700) - packaged = SOURCE / "release-bin" - if packaged.is_dir(): - metadata = json.loads((packaged / "platform.json").read_text()) - if metadata != {"os": platform.system(), "arch": platform.machine()}: - raise ValueError("Release archive is for a different OS/CPU") - for name in ("pipelines-server", "pipelines-tui", "pipelines"): - shutil.copy2(packaged / name, root / "bin" / name) - else: - go = shutil.which("go") - if not go: - raise ValueError("Source install needs Go 1.27+; alternatively use a platform release archive") - run([go, "install", "-trimpath", "./cmd/server", "./cmd/tui", "./cmd/pipelines"], - env=dict(os.environ, GOBIN=str(root / "bin")), quiet=True, timeout=240) - for command in ("server", "tui"): - (root / "bin" / command).replace(root / "bin" / ("pipelines-" + command)) - username = retained["username"] if retained else (args.username or ("phone-reader" if args.yes else input("Phone username [phone-reader]: ").strip() or "phone-reader")) - topic = retained["topic"] if retained else (args.topic or ("pipelines" if args.yes else input("Notification topic [pipelines]: ").strip() or "pipelines")) - if not NAME.fullmatch(username) or username == "pipeline-publisher" or not TOPIC.fullmatch(topic): - raise ValueError("Use safe alphanumeric usernames/topics; pipeline-publisher is reserved") - server_port = retained["server_port"] if retained else choose_port(args.server_port) - ntfy_port = retained["ntfy_port"] if retained else choose_port(args.ntfy_port) - if server_port == ntfy_port: - ntfy_port = choose_port(ntfy_port + 1) - public_url = retained["public_url"] if retained else args.public_url or f"http://{default_host()}:{ntfy_port}" - from urllib.parse import urlparse - url = urlparse(public_url) - if url.scheme not in ("http", "https") or not url.hostname or url.username or url.password or url.path not in ("", "/") or url.query or url.fragment or any(c in public_url for c in "$\n\r\x00"): - raise ValueError("Phone URL must be an HTTP(S) base URL without credentials, topic, query, or interpolation") - chosen_workspace=args.workspace or (Path(retained["workspace"]) if retained else root / "workspace") - workspace = str(chosen_workspace.expanduser().resolve()) - if not Path(workspace).is_dir(): - raise ValueError("Workspace directory must exist") - suffix = hashlib.sha256(str(root).encode()).hexdigest()[:12] - bindir = Path("/usr/local/bin") if args.system_command else args.bin_dir.expanduser().resolve() - launcher = bindir / "pipelines" - if launcher.exists() or launcher.is_symlink(): - if not launcher.is_symlink() or launcher.resolve() != root / "bin/pipelines": - raise ValueError("A pipelines command already exists in the target bin directory; choose --bin-dir") - # Only explicit server-selection variables are copied; never API keys/tokens. - opencode_env = {key: value for key, value in os.environ.items() if key in ("XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "OPENCODE_SERVER", "OPENCODE_DB")} - config = dict(format=1, prefix=str(root), docker=docker, python=sys.executable, - opencode=opencode, opencode_env=opencode_env, path=os.pathsep.join([str(root / "bin"), str(Path(opencode).parent), os.environ.get("PATH", "")]), - project="pipelines-" + suffix, label="io.pipelines." + suffix, - server_port=server_port, ntfy_port=ntfy_port, public_url=public_url.rstrip("/"), - topic=topic, username=username, timezone=timezone(), workspace=workspace, - workspace_roots=[str(path.expanduser().resolve()) for path in (args.workspace_root or ([Path(p) for p in retained["workspace_roots"]] if retained else [Path.home()]))], - launcher=str(launcher), registration=None, bootstrapped=bool(retained and retained.get("bootstrapped"))) - manifest = root / MARKER - manifest.write_text(json.dumps(config, indent=2)); manifest.chmod(0o600) - (root / "compose.yaml").write_text(ntfy_compose(config)) - shutil.copy2(SOURCE / "scripts/manage.py", root / "lib/manage.py") - alias = root / "bin/opencode" - if not alias.exists(): - alias.symlink_to(opencode) - # The PATH command is the compiled Go binary, not a shell/Python launcher. - if not config["bootstrapped"]: - if args.password_file: - password = args.password_file.read_text().strip() - elif args.yes: - password = secrets.token_urlsafe(18) - else: - password = getpass.getpass("Phone password (blank = generate): ") or secrets.token_urlsafe(18) - if len(password) < 8 or any(c in password for c in "\r\n\x00"): - raise ValueError("Phone password must have at least 8 characters and no line breaks") - compose(config, "up", "-d", "ntfy", quiet=True) - wait_health(f"http://127.0.0.1:{ntfy_port}/v1/health") - for user, secret in (("pipeline-publisher", secrets.token_urlsafe(32)), (username, password)): - compose(config, "exec", "-T", "-e", "NTFY_PASSWORD", "ntfy", "ntfy", "user", "add", user, - env=dict(os.environ, NTFY_PASSWORD=secret), quiet=True) - compose(config, "exec", "-T", "ntfy", "ntfy", "access", "pipeline-publisher", topic, "write-only", quiet=True) - compose(config, "exec", "-T", "ntfy", "ntfy", "access", username, topic, "read-only", quiet=True) - output = compose(config, "exec", "-T", "ntfy", "ntfy", "token", "add", "pipeline-publisher", quiet=True).stdout - tokens = re.findall(r"\btk_[A-Za-z0-9]{29}\b", output) - if len(tokens) != 1: - raise RuntimeError("Could not extract the publishing token; credentials were not printed") - write_private(root / "secrets/publisher-token", tokens[0] + "\n") - write_private(root / "secrets/phone-login.txt", f"Server: {public_url}\nTopic: {topic}\nUsername: {username}\nPassword: {password}\n") - config["bootstrapped"] = True - if not args.no_autostart: - config["registration"] = make_registration(config) - manifest.write_text(json.dumps(config, indent=2)) - if args.system_command and not bindir.exists(): - subprocess.run(["sudo", "mkdir", "-p", str(bindir)], check=True) - else: - bindir.mkdir(parents=True, exist_ok=True) - if not launcher.is_symlink(): - if args.system_command and not os.access(bindir, os.W_OK): - # Privilege is used ONLY for this command symlink, never for the server/auth. - subprocess.run(["sudo", "ln", "-s", str(root / "bin/pipelines"), str(launcher)], check=True) - else: - launcher.symlink_to(root / "bin/pipelines") - start(config) - print(f"Installed. Run: {launcher}\nManage both services: pipelines start|stop|restart|status|doctor|logs") - print(f"Phone server: {config['public_url']}\nTopic: {topic}\nUsername: {username}") - print(f"Password is private: {root / 'secrets/phone-login.txt'}") - print("Fresh installations have no pipelines. In the TUI press b for rendered examples/templates.") - print("ntfy is exposed on LAN/VPN with authentication. Use HTTPS or an encrypted VPN outside a trusted LAN.") - if str(bindir) not in os.environ.get("PATH", "").split(os.pathsep): - print(f"Add {bindir} to PATH, or use the full command path above.") - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--prefix", type=Path, default=DEFAULT_PREFIX) - parser.add_argument("--bin-dir", type=Path, default=Path.home() / ".local/bin") - parser.add_argument("--system-command",action="store_true",help="Put the command in /usr/local/bin (sudo only for the symlink; services remain per-user)") - parser.add_argument("--username") - parser.add_argument("--topic") - parser.add_argument("--password-file", type=Path) - parser.add_argument("--public-url") - parser.add_argument("--workspace", type=Path) - parser.add_argument("--workspace-root",type=Path,action="append",help="Allowed repository root; repeatable (default: your home)") - parser.add_argument("--opencode", help="Path to the host OpenCode V2 CLI") - parser.add_argument("--server-port", type=int, default=8080) - parser.add_argument("--ntfy-port", type=int, default=8090) - parser.add_argument("--no-autostart", action="store_true", help="Use a managed background process rather than launchd/systemd") - parser.add_argument("--yes", action="store_true", help="Accept defaults and generate a private phone password") - args = parser.parse_args() - if not 1024 <= args.server_port <= 65535 or not 1024 <= args.ntfy_port <= 65535: - parser.error("Use unprivileged ports 1024–65535") - os.chdir(SOURCE) - install(args) - - if __name__ == "__main__": - try: - main() - except (ValueError, RuntimeError, OSError, subprocess.SubprocessError) as error: - sys.exit(str(error)) + if len(sys.argv) != 1: + sys.exit("Install binaries with go install ./cmd/...; set GOBIN to choose the destination. Configure and start services separately.") + sys.exit(subprocess.call(["go", "install", "-trimpath", "./cmd/..."], cwd=Path(__file__).resolve().parents[1])) diff --git a/scripts/package.py b/scripts/package.py index a878bce4..834e2876 100755 --- a/scripts/package.py +++ b/scripts/package.py @@ -36,7 +36,7 @@ def package(output, version, target): if system not in ("darwin", "linux") or arch not in ("arm64", "amd64"): raise ValueError("Supported: source, darwin-arm64/amd64, linux-arm64/amd64") env = dict(os.environ, GOOS=system, GOARCH=arch, CGO_ENABLED="0") - for command, binary in (("server", "pipelines-server"), ("tui", "pipelines-tui"), ("pipelines", "pipelines")): + for command, binary in (("pipelines-server", "pipelines-server"), ("pipelines-tui", "pipelines-tui"), ("pipelines", "pipelines")): subprocess.run(["go", "build", "-trimpath", "-o", str(binaries / binary), "./cmd/" + command], cwd=ROOT, env=env, check=True) (binaries / "platform.json").write_text(json.dumps({"os": "Darwin" if system == "darwin" else "Linux", "arch": "arm64" if arch == "arm64" and system == "darwin" else "aarch64" if arch == "arm64" else "x86_64"})) diff --git a/scripts/run-local-server.sh b/scripts/run-local-server.sh index 5718ac17..bb95f19c 100644 --- a/scripts/run-local-server.sh +++ b/scripts/run-local-server.sh @@ -10,4 +10,4 @@ export PIPELINES_NTFY_URL="${PIPELINES_NTFY_URL:-http://127.0.0.1:8090/pipelines if [ -z "${PIPELINES_NTFY_TOKEN:-}" ]; then export PIPELINES_NTFY_TOKEN_FILE="${PIPELINES_NTFY_TOKEN_FILE:-$(pwd)/secrets/ntfy-token.txt}" fi -exec go run ./cmd/server "$@" +exec go run ./cmd/pipelines-server "$@" diff --git a/scripts/test_installation.py b/scripts/test_installation.py index 29ffd052..6b50d206 100644 --- a/scripts/test_installation.py +++ b/scripts/test_installation.py @@ -1,90 +1,36 @@ -import importlib.util -import json import os from pathlib import Path -import socket +import subprocess import tempfile import unittest -from unittest import mock -import install import manage class InstallationTests(unittest.TestCase): - def test_launcher_forwards_application_arguments_unchanged(self): - config = dict(prefix="/owned", server_port=12345, opencode="/host/opencode", path="/owned/bin:/usr/bin") - for command in (["edit", "a name", "change X", "--json"], ["--url", "ws://other/connect", "list", "--json"], ["--help"], ["run", "workflow", "task", "--yes", "--json"]): - with mock.patch.object(manage,"load",return_value=config), mock.patch.object(manage.sys,"argv",["manage.py","--prefix","/owned",*command]), mock.patch.object(manage.os,"execve") as execute: - manage.main() - binary,args,env=execute.call_args.args - self.assertEqual(args,["/owned/bin/pipelines",*command]) - self.assertEqual(env["PIPELINES_URL"],"ws://127.0.0.1:12345/connect") - def test_compose_has_only_ntfy_and_no_credentials(self): - config = dict(timezone="Europe/London", public_url="http://100.1.2.3:8090", ntfy_port=8090) - text = install.ntfy_compose(config) - self.assertIn("binwiederhier/ntfy:v2.29.0", text) - self.assertIn("deny-all", text) - self.assertNotIn("opencode", text) - self.assertNotIn("password", text.lower()) - self.assertNotIn("pipelines:", text) - - def test_detect_free_port_avoids_listener(self): - with socket.socket() as listener: - listener.bind(("0.0.0.0", 0)); listener.listen() - occupied = listener.getsockname()[1] - self.assertNotEqual(install.choose_port(occupied), occupied) - - def test_secret_files_private_and_not_overwritten(self): + def test_standard_installer_needs_only_go_and_honors_gobin(self): with tempfile.TemporaryDirectory() as directory: - path = Path(directory, "secrets/token") - install.write_private(path, "private") - self.assertEqual(path.stat().st_mode & 0o777, 0o600) - with self.assertRaises(FileExistsError): - install.write_private(path, "replacement") - self.assertEqual(path.read_text(), "private") + root=Path(directory) + capture=root/"args" + executable=root/"go" + executable.write_text('#!/bin/sh\nprintf "%s\\n" "$GOBIN" "$@" > "'+str(capture)+'"\n') + executable.chmod(0o700) + env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],GOBIN=str(root/"bin")) + subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh")],env=env,check=True) + self.assertEqual(capture.read_text().splitlines(),[str(root/"bin"),"install","-trimpath","./cmd/..."]) + self.assertFalse((root/"bin").exists()) - def test_management_requires_owned_installation_marker(self): - with tempfile.TemporaryDirectory() as directory: - with self.assertRaises(ValueError): - manage.load(directory) - root = Path(directory).resolve() - (root / manage.MARKER).write_text(json.dumps(dict(format=1, prefix=str(root)))) - self.assertEqual(manage.load(root)["prefix"], str(root)) - (root / manage.MARKER).write_text(json.dumps(dict(format=1, prefix="/"))) - with self.assertRaises(ValueError): - manage.load(root) + def test_standard_installer_rejects_legacy_provisioning_flags(self): + result=subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh"),"--system-command"],capture_output=True,text=True) + self.assertEqual(result.returncode,2) + self.assertIn("GOBIN",result.stderr) - def test_pid_reuse_cannot_signal_unrelated_process(self): + def test_legacy_management_requires_owned_marker(self): with tempfile.TemporaryDirectory() as directory: - Path(directory, "supervisor.pid").write_text(str(os.getpid())) + with self.assertRaises(ValueError):manage.load(directory) + + def test_legacy_pid_reuse_cannot_signal_unrelated_process(self): + with tempfile.TemporaryDirectory() as directory: + Path(directory,"supervisor.pid").write_text(str(os.getpid())) self.assertIsNone(manage.managed_pid(dict(prefix=directory))) - def test_default_uninstall_preserves_data_and_open_code(self): - with tempfile.TemporaryDirectory() as directory: - root = Path(directory, "app"); root.mkdir() - for name in ("bin", "lib", "data", "secrets"): - (root / name).mkdir() - (root / "data/pipelines.db").write_text("history") - (root / "secrets/publisher-token").write_text("credential") - config = dict(format=1, prefix=str(root), launcher=str(Path(directory, "pipelines")), registration=None) - Path(config["launcher"]).symlink_to(root / "bin/pipelines") - with mock.patch.object(manage, "stop"), mock.patch.object(manage, "compose") as compose: - manage.uninstall(config, yes=True) - compose.assert_called_once_with(config, "down", quiet=True) - self.assertEqual((root / "data/pipelines.db").read_text(), "history") - self.assertEqual((root / "secrets/publisher-token").read_text(), "credential") - self.assertFalse((root / "bin").exists()) - self.assertFalse(Path(config["launcher"]).is_symlink()) - - def test_service_registration_quotes_paths(self): - with tempfile.TemporaryDirectory(prefix="path with spaces ") as directory: - root = Path(directory);(root / "logs").mkdir() - config = dict(prefix=str(root), python="/usr/bin/python3", label="io.pipelines.test") - with mock.patch.object(install.platform,"system",return_value="Linux"),mock.patch.object(install.Path,"home",return_value=root): - path = Path(install.make_registration(config)) - self.assertIn('"'+str(root / 'lib/manage.py')+'"', path.read_text()) - self.assertIn("Restart=on-failure", path.read_text()) - - -if __name__ == "__main__": - unittest.main() +if __name__=="__main__":unittest.main() diff --git a/uninstall.sh b/uninstall.sh index f8943672..517ddab1 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -1,3 +1,3 @@ #!/bin/sh set -eu -exec python3 "$(dirname "$0")/scripts/manage.py" uninstall "$@" +exec pipelines uninstall "$@"