From f700d1beb2bbff778cdf1450644c83b8b778f657 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 9 Oct 2026 21:34:47 +0100 Subject: [PATCH] Configure full Docker setup and confirm consequential actions --- Dockerfile | 4 +- README.md | 21 ++-- cmd/pipelines-tui/main.go | 2 +- compose.yaml | 13 ++- docs/cli.md | 21 +++- docs/docker.md | 15 ++- docs/install.md | 35 ++++++- docs/installation-verification.md | 17 ++- install.sh | 21 +++- internal/author/skill/SKILL.md | 3 + internal/cli/app.go | 21 +++- internal/cli/app_test.go | 3 + internal/cli/commands.go | 6 +- internal/cli/confirmation.go | 19 ++++ internal/cli/confirmation_test.go | 38 +++++++ internal/cli/draft_delete_test.go | 2 +- internal/server/cli_test.go | 4 +- internal/server/retry_test.go | 13 ++- internal/tui/action_confirm.go | 34 ++++++ internal/tui/action_confirm_test.go | 35 +++++++ internal/tui/cli_run_test.go | 6 +- internal/tui/home_failed_runs_test.go | 4 +- internal/tui/model.go | 11 +- internal/tui/model_test.go | 4 +- internal/tui/retry_test.go | 5 + internal/tui/runs.go | 17 ++- internal/tui/shortcut_scope_test.go | 21 ++++ scripts/install.py | 6 +- scripts/setup-docker.py | 143 ++++++++++++++++++++++++++ scripts/test_installation.py | 6 ++ scripts/test_setup_docker.py | 86 ++++++++++++++++ 31 files changed, 587 insertions(+), 49 deletions(-) create mode 100644 internal/cli/confirmation.go create mode 100644 internal/cli/confirmation_test.go create mode 100644 internal/tui/action_confirm.go create mode 100644 internal/tui/action_confirm_test.go create mode 100644 scripts/setup-docker.py create mode 100644 scripts/test_setup_docker.py diff --git a/Dockerfile b/Dockerfile index 197153ca..ba8527cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,15 +4,17 @@ COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 go build -trimpath -o /out/pipelines-server ./cmd/pipelines-server +RUN CGO_ENABLED=0 go build -trimpath -o /out/pipelines ./cmd/pipelines FROM ghcr.io/anomalyco/opencode:2.0.20 USER root -RUN apk add --no-cache ca-certificates tzdata git \ +RUN apk add --no-cache ca-certificates tzdata git curl \ && addgroup -g 10001 pipelines \ && adduser -D -u 10001 -G pipelines -h /home/pipelines pipelines \ && mkdir -p /data /workspace \ && chown -R pipelines:pipelines /data /workspace /home/pipelines COPY --from=build /out/pipelines-server /usr/local/bin/pipelines-server +COPY --from=build /out/pipelines /usr/local/bin/pipelines COPY --from=build /usr/local/go /usr/local/go COPY --chown=10001:10001 . /workspace ENV HOME=/home/pipelines \ diff --git a/README.md b/README.md index 4a7823c0..369aab19 100644 --- a/README.md +++ b/README.md @@ -5,24 +5,28 @@ server reuse your OpenCode login; Docker ntfy handles phone notifications. ## Install -Requires **Go 1.27+** to install. Use your existing OpenCode for model stages; -Docker/ntfy notifications are optional. +Interactive installation offers a full **Docker server + ntfy** setup (default) +or **native binaries only**. Docker mode needs Docker Compose and Python 3; native +mode needs Go 1.27+ and uses your existing OpenCode for model stages. ```sh git clone https://git.a-wan.net/alan/pipelines.git cd pipelines -./install.sh +./install.sh --native # host CLI/TUI/server + shared OpenCode skill pipelines help +# Or: ./install.sh --docker # configure/start both services and connect a provider ``` Go uses `GOBIN` or its normal `GOPATH/bin` default; put that directory on PATH. -`./install.sh` installs the Go binaries and the shared OpenCode `pipelines` skill. -There are no login checks, dependency installers, hardcoded install paths, or -automatic services. With plain `go install ./cmd/...`, also run `pipelines skill install`. +Native mode installs the Go binaries and shared OpenCode `pipelines` skill without +login checks or automatic services. Docker mode configures ntfy accounts/token, +checks publishing/health, and opens container OpenCode provider login on first setup; +existing settings, credentials and volumes are retained on reinstall. +With plain `go install ./cmd/...`, also run `pipelines skill install`. For system-wide use, install the binaries into your chosen system bin directory with the appropriate permissions. Configuration and data remain per-user. -Start explicitly; fresh databases contain no pipelines: +Start a native server explicitly; fresh databases contain no pipelines: ```sh pipelines serve # foreground server @@ -132,6 +136,9 @@ conversation or intermediate stage output. Pipelines does not automatically dele generation, agent, or formatting-repair sessions, including on failure/cancellation. Primary session IDs are retained in run steps (`pipelines inspect --json`). On Home, **r** opens all recent runs; both Home panels show the shortcut. +Quit the TUI with **:quit**; **q** does not exit. **Ctrl+C** remains an interrupt. +Starting/retrying/canceling runs and deleting drafts require confirmation. CLI +publication also prompts; use `--yes` only for explicitly approved automation. **n / :new** and creating from a template offer a Tab-navigable, fuzzy-searchable picker: **nvim** opens the draft Markdown file; **OpenCode** authors it with you. Canceling the picker creates no draft and publishes nothing. diff --git a/cmd/pipelines-tui/main.go b/cmd/pipelines-tui/main.go index 6b2434b1..9f67d031 100644 --- a/cmd/pipelines-tui/main.go +++ b/cmd/pipelines-tui/main.go @@ -18,7 +18,7 @@ func main() { name := flag.String("pipeline", "", "Registered pipeline name; omitted opens fuzzy selection") prompt := flag.String("prompt", "", "Task prompt; omitted opens a multiline composer") workspace := flag.String("workspace", "", "Repository directory (defaults to current directory with -run)") - yes := flag.Bool("yes", false, "Compatibility flag; supplied name/prompt start immediately") + yes := flag.Bool("yes", false, "Confirm the initial supplied pipeline/task without an interactive prompt") allow := flag.Bool("allow-tools", false, "Compatibility flag; tools follow the pipeline stages") paths := flag.String("write-paths", "", "Optional narrower edit scope; default is the run directory") flag.Parse() diff --git a/compose.yaml b/compose.yaml index 7cd4e405..0186aa30 100644 --- a/compose.yaml +++ b/compose.yaml @@ -6,7 +6,7 @@ services: stop_grace_period: 60s depends_on: ntfy: - condition: service_started + condition: service_healthy environment: TZ: ${TZ:?Set TZ to your host timezone; scripts/configure-compose.py detects it} PIPELINES_NTFY_URL: http://ntfy/pipelines @@ -20,6 +20,12 @@ services: - pipelines-data:/data - opencode-home:/home/pipelines - pipelines-workspace:/workspace + healthcheck: + test: ["CMD", "pipelines", "--url", "ws://127.0.0.1:8080/connect", "status", "--json"] + interval: 5s + timeout: 5s + retries: 12 + start_period: 10s ntfy: image: binwiederhier/ntfy:latest command: serve @@ -41,6 +47,11 @@ services: - "${NTFY_BIND_ADDRESS:-127.0.0.1}:8090:80" volumes: - ntfy-data:/var/lib/ntfy + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/v1/health"] + interval: 5s + timeout: 3s + retries: 12 secrets: ntfy_token: file: ./secrets/ntfy-token.txt diff --git a/docs/cli.md b/docs/cli.md index f518ee46..6d91c3ce 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -157,7 +157,7 @@ you invoked Pipelines from), alongside the task and any custom input fields. It is recorded by the server and shown in the run's Input section. Press `r` or use `:retry` on a failed run to restart it from the first stage. -CLI equivalent: `pipelines retry ` (optional `--json`, `--wait`, and +CLI equivalent: `pipelines retry ` (optional `--yes`, `--json`, `--wait`, and `--request-key`). The new attempt retains the initial input, directory, pinned pipeline version, and edit scope, and records `retry_of`. Original history is unchanged. Legacy runs gain `pwd` from their recorded workspace on retry. @@ -212,7 +212,8 @@ published definition. Submission remains separate and explicit. In the draft browser, `x` asks to delete the selected draft; `y` confirms and Esc or `n` keeps it. The CLI equivalent, `pipelines draft-delete `, is an explicit deletion command and needs no server connection; `--json` reports -the result. Only managed drafts inside the configured draft cache are accepted. +the result. It asks for confirmation; `--yes` explicitly approves scripted deletion. +Only managed drafts inside the configured draft cache are accepted. Source and metadata symlinks/directories and external Markdown files are rejected. Published definitions and run history are not deleted. Cleanup can also remove orphaned metadata when a draft source was already removed. @@ -230,14 +231,24 @@ an immutable version in JSON mode. Starting a run enables its stage-declared too without a second approval. Editing supports all file types and new files within the run directory; no file list is required. Shell commands use the repository's actual tooling. `--write-paths` is an optional narrower scope, not a prerequisite. -`--yes` and `--allow-tools` remain compatibility flags but are not required. +`--yes` skips action confirmation for explicitly approved automation; +`--allow-tools` remains a compatibility flag and is not required. `--wait` polls until terminal status, returns nonzero on failure/cancellation, and does not cancel a run when waiting times out. Reuse `--request-key` to prevent -duplicate work. A supplied pipeline/task starts immediately with a live inspector. -When a task is omitted, Enter inserts a newline and Ctrl+S runs the composed task. +duplicate work. A supplied pipeline/task opens an action-confirmation popup before +starting with a live inspector (unless `--yes` was supplied). When a task is omitted, +Enter inserts a newline and Ctrl+S opens the confirmation for the composed task. There is no file-list question or typed RUN confirmation. Shell runs with your user's authority: the run directory and tool policy are not an OS sandbox. +Run, retry, cancellation, CLI publication, and draft deletion require confirmation. +CLI prompts go to stderr and default to No; use `--yes` for non-interactive approval. +The TUI's run/retry/cancel popup accepts `y`; Enter does not implicitly approve, +and `n`/Esc cancels without sending the action. Retry restarts with new sessions +and original input/pwd, never the failed conversation; side effects may repeat. +Publication retains its explicit submission confirmation and version conflict guards. +Quit with `:quit`, not `q`; Ctrl+C remains an interrupt. + JSON results go to stdout, errors to stderr (JSON error envelopes when requested), and failures exit nonzero. Export/template `--source` deliberately prints raw Markdown. `--timeout` bounds each server request and the optional wait duration. diff --git a/docs/docker.md b/docs/docker.md index 33ce83d1..d41f4d53 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -1,5 +1,18 @@ # Pipeline server + ntfy in one Compose project (Android) +## Complete installer (recommended for both services in Docker) + +Run `./install.sh --docker` from an interactive terminal. It handles Compose +configuration, ntfy users/permissions/token, image build, container skill install, +first-time container OpenCode provider login, startup, and an authenticated test +notification. Choose your own provider in OpenCode; signup/login credentials stay +in the Docker volume, not your host OpenCode account. Re-run to update while +preserving `.env`, tokens, histories and volumes. Use `--relogin` to connect another +provider. No fixed OpenCode Go login is required. Health/publishing checks do not +prove model inference or phone reception. + +The steps below are for manual setup or a local Pipelines server with Docker ntfy. + ## Local pipeline server, Docker ntfy To reuse your host OpenCode login without container authentication: @@ -91,7 +104,7 @@ docker compose up -d docker compose logs -f pipelines ``` -Select **OpenCode Go** and follow its authentication flow. Credentials are stored +Select your preferred provider and follow its authentication flow. Credentials are stored in the dedicated `opencode-home` volume, separate from your host account. These container commands are **not tested** here. The image includes Go/git for the existing narrowly approved verification tools; the actual server runs as uid 10001. diff --git a/docs/install.md b/docs/install.md index 9876b412..5f4a760f 100644 --- a/docs/install.md +++ b/docs/install.md @@ -1,22 +1,46 @@ # Installation -Installation requires **Go only**. OpenCode is needed when authoring or executing -model stages. Docker/ntfy is optional and is configured separately. +The interactive installer offers **Docker server + ntfy** (default) or **native +binaries only**. Native installation requires Go; Docker setup requires Docker +Compose and Python 3, and includes OpenCode in the persistent server container. ```sh git clone https://git.a-wan.net/alan/pipelines.git cd pipelines ./install.sh -# Equivalent: go install ./cmd/... && pipelines skill install +# Explicit choices: +./install.sh --docker # complete server/ntfy setup, including provider login +./install.sh --native # binaries/skill only; honors GOBIN/GOPATH ``` -This installs `pipelines`, `pipelines-server`, and `pipelines-tui`. Go chooses the +Native mode 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 and bundled OpenCode skill without changing databases, login, or OpenCode configuration files. +Docker mode prompts for the phone-facing ntfy base URL, explicit bind IP, and +timezone if it cannot be detected. It preserves an existing `.env`, bootstraps +the publishing/phone accounts and publishing token, builds the server, installs +the skill inside the container, and opens **container OpenCode provider login** +on first setup. Choose any supported provider; signup is handled by that provider. +Provider credentials remain in the `opencode-home` volume. Reinstalls skip login +after completed setup; `./install.sh --docker --relogin` opens it again without +deleting accounts. The installer then starts/recreates the Compose services, +checks ntfy health through Pipelines, and sends an authenticated test notification. +It does not register an OS service, delete volumes, or seed pipeline definitions. +Phone reception/model inference are not verified by installation. + +For fresh configuration you can supply `--ntfy-url https://ntfy.example.com +--bind-address 127.0.0.1 --timezone Europe/London --port 8081` after `--docker`. +Use a LAN/VPN interface IP instead of loopback for direct phone access; only use +plain HTTP on a trusted network. Existing `.env` settings are never silently +replaced. Without a terminal, no-argument installation remains binary-only and +prints that limitation; use an interactive terminal for first Docker/provider setup. +Install native mode as well if you want host CLI/TUI binaries; Docker mode installs +the server and CLI in its image. See [Docker setup](docker.md). + For a private repository, use your normal Git credentials and `GOPRIVATE`: ```sh @@ -112,7 +136,8 @@ 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). +Full Docker installation configures this automatically via `./install.sh --docker`. +For native mode, use an existing ntfy server or the 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 diff --git a/docs/installation-verification.md b/docs/installation-verification.md index c113cc4f..466b962f 100644 --- a/docs/installation-verification.md +++ b/docs/installation-verification.md @@ -23,9 +23,24 @@ now references that skill directly instead of preloading instructions. 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 +tool calls, cancellation during inference, Docker provider authentication, privileged system-wide copying, and OS service-manager registration are **not tested**. +The full Docker installer was exercised in an isolated Compose project with real +ntfy and Pipelines containers: image build, account/ACL/token bootstrap, Compose +health checks, authenticated notification publishing from the Pipelines container, +CLI status, reinstall preservation of `.env`/token, and an empty pipeline database. +Provider-account inspection/login completion was mocked in that installer test. +A separate real-container PTY invocation of `opencode auth login` displayed its +searchable provider choices and was canceled without credentials. Provider login, +model inference and phone delivery remain **not tested**. + +Consequential-action regressions check run/retry/cancellation and CLI publication/ +draft deletion confirmation, default-No behavior, JSON stdout cleanliness, and +`--yes` for explicit automation. The compiled CLI/TUI PTY check verified that `q` +does not exit, `:quit` exits, canceled retry prompts create no run, and an explicit +`y` starts a new retry with the original input/directory and pinned version. + Autostart regression tests exercise both service-definition formats, scoped environment capture, escaping, private files, refusal to overwrite unmanaged files/symlinks, preview without side effects, and lifecycle calls using a mock OS diff --git a/install.sh b/install.sh index 9f965d75..3fd6f0ff 100755 --- a/install.sh +++ b/install.sh @@ -1,9 +1,26 @@ #!/bin/sh set -eu +cd "$(dirname "$0")" +mode=${1:-} +if [ -z "$mode" ] && [ -t 0 ]; then + printf 'Install Docker server + ntfy, or native binaries only? [docker/native] (docker): ' + read -r mode + mode=${mode:-docker} +fi +case "$mode" in + --docker|docker) + if [ "$#" -gt 0 ]; then shift; fi + exec python3 scripts/setup-docker.py "$@" + ;; + --native|native) + if [ "$#" -gt 0 ]; then shift; fi + ;; + '') echo 'Non-interactive binary-only install. Use ./install.sh --docker for the configured server + ntfy.' >&2 ;; + *) echo 'Usage: ./install.sh [--native | --docker [setup options]] (native honors GOBIN and GOPATH)' >&2; exit 2 ;; +esac if [ "$#" -ne 0 ]; then - echo 'Usage: ./install.sh (honors GOBIN and GOPATH; installs binaries and the OpenCode skill)' >&2 + echo 'Usage: ./install.sh --native (honors GOBIN and GOPATH)' >&2 exit 2 fi -cd "$(dirname "$0")" go install -trimpath ./cmd/... exec go run ./cmd/pipelines skill install diff --git a/internal/author/skill/SKILL.md b/internal/author/skill/SKILL.md index c184b145..4a8cc656 100644 --- a/internal/author/skill/SKILL.md +++ b/internal/author/skill/SKILL.md @@ -29,6 +29,9 @@ Do not edit repositories or shared OpenCode configuration while authoring. 5. Summarize the change, validation outcome, edit/tool permissions, and any schedule activation. Give the user the exact `pipelines submit DRAFT_PATH` command. 6. **Do not submit or run by default.** Leave a retained draft for human review. + Publication, execution, retry, cancellation and draft deletion ask for confirmation. + Use `--yes` for these CLI commands only after the user explicitly approves the + specific action; never treat approval of editing as approval of publishing/running. Submit only when the user explicitly asks you to submit. Run only when asked. The CLI supports `list`, `show`, `versions`, `export`, `templates`, `template`, diff --git a/internal/cli/app.go b/internal/cli/app.go index 3fb82352..16f0a067 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -59,6 +59,7 @@ func (a App) flags(command string) (*flag.FlagSet, *options) { case "template": f.BoolVar(&o.source, "source", false, "Print original Markdown instead of metadata") case "submit": + f.BoolVar(&o.yes, "yes", false, "Confirm publication without an interactive prompt") f.StringVar(&o.name, "name", "", "Name for an external Markdown file (managed drafts already know their name)") f.BoolVar(&o.createOnly, "create-only", false, "Reject existing names rather than creating another version") f.IntVar(&o.expected, "expected-version", 0, "Reject if the latest version changed") @@ -67,7 +68,7 @@ func (a App) flags(command string) (*flag.FlagSet, *options) { f.StringVar(&o.name, "pipeline", "", "Exact registered pipeline name or ID") f.StringVar(&o.prompt, "prompt", "", "Task prompt; omitted opens a multiline composer") f.StringVar(&o.workspace, "workspace", "", "Run directory (default: current directory)") - f.BoolVar(&o.yes, "yes", false, "Compatibility flag; supplied name/prompt already start immediately") + f.BoolVar(&o.yes, "yes", false, "Confirm execution without an interactive prompt") f.BoolVar(&o.allow, "allow-tools", false, "Compatibility flag; stage-declared tools are enabled automatically") f.StringVar(&o.paths, "write-paths", "", "Optional narrower edit scope; default is all files in the run directory") f.StringVar(&o.key, "request-key", "", "Idempotency key (default: generated unique key)") @@ -76,8 +77,11 @@ func (a App) flags(command string) (*flag.FlagSet, *options) { case "runs": f.StringVar(&o.name, "pipeline", "", "Filter by exact pipeline name or ID") case "retry": + f.BoolVar(&o.yes, "yes", false, "Confirm retry without an interactive prompt") f.StringVar(&o.key, "request-key", "", "Idempotency key (default: generated unique key)") f.BoolVar(&o.wait, "wait", false, "Wait for the new run to finish") + case "cancel", "draft-delete": + f.BoolVar(&o.yes, "yes", false, "Confirm this action without an interactive prompt") case "debug": f.StringVar(&o.opencode, "opencode", envOr("PIPELINES_OPENCODE", "opencode"), "Host OpenCode executable") f.BoolVar(&o.noOpen, "no-open", false, "Print the investigation prompt without launching OpenCode") @@ -202,6 +206,9 @@ func (a App) Run(ctx context.Context, args []string) error { if len(pos) != 1 { return fmt.Errorf("draft-delete requires one retained draft path") } + if err := a.confirmAction("Delete retained draft "+pos[0]+"?", o.yes); err != nil { + return err + } if err := draft.Delete(pos[0]); err != nil { return err } @@ -396,6 +403,9 @@ func (a App) Run(ctx context.Context, args []string) error { expected = 0 } request = protocol.Message{Type: protocol.Submit, Name: name, Source: source, CreateOnly: d.CreateOnly || o.createOnly, ExpectedVersion: expected} + if err := a.confirmAction("Publish "+name+" as an immutable version? Included schedules activate on acceptance.", o.yes); err != nil { + return err + } } r, err := call(request) if err != nil { @@ -524,10 +534,16 @@ func (a App) Run(ctx context.Context, args []string) error { } typeName := protocol.GetRun if command == "cancel" { + if err := a.confirmAction(fmt.Sprintf("Cancel run #%d? Previous side effects are not undone.", id), o.yes); err != nil { + return err + } typeName = protocol.CancelRun } key := "" if command == "retry" { + if err := a.confirmAction(fmt.Sprintf("Retry failed run #%d as a fresh run? Reuses original input/directory; side effects may repeat.", id), o.yes); err != nil { + return err + } typeName = protocol.RetryRun key = o.key if key == "" { @@ -759,6 +775,9 @@ func (a App) run(ctx context.Context, pos []string, o *options, call callFunc, r if version == 0 { version = p.Version } + if err := a.confirmAction(fmt.Sprintf("Run %s v%d in %s? This may edit files, execute commands and publish notifications.", p.Name, version, workspace), o.yes); err != nil { + return err + } key := o.key if key == "" { key = fmt.Sprintf("cli-%d-%d", os.Getpid(), time.Now().UnixNano()) diff --git a/internal/cli/app_test.go b/internal/cli/app_test.go index ca382bce..06099ef5 100644 --- a/internal/cli/app_test.go +++ b/internal/cli/app_test.go @@ -32,6 +32,9 @@ func setup(t *testing.T) (func(...string) (string, error), *pipeline.Store, stri url := "ws" + strings.TrimPrefix(http.URL, "http") + "/connect" t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) return func(args ...string) (string, error) { + if len(args) > 0 && args[0] == "submit" { + args = append(args, "--yes") + } var out, stderr bytes.Buffer app := cli.App{Out: &out, Err: &stderr, In: strings.NewReader("")} err := app.Run(ctx, append([]string{"--url", url}, args...)) diff --git a/internal/cli/commands.go b/internal/cli/commands.go index b4ab080b..98a61646 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -7,7 +7,7 @@ var usage = map[string]string{ "drafts": "drafts [--json]", "draft-delete": "draft-delete [--json]", "validate": "validate [--json]", - "submit": "submit [--name NAME] [--create-only] [--json]", + "submit": "submit [--name NAME] [--create-only] [--yes] [--json]", "list": "list [--json]", "show": "show [--version N] [--json]", "versions": "versions [--json]", @@ -32,8 +32,8 @@ var notes = map[string]string{ "new": "Create an optional template copy or a one-stage starter. Opens OpenCode with @pipelines; the skill asks any missing questions, including the name. No submission is performed.", "edit": "Omit the name to open fuzzy selection. Original immutable source is copied to a private draft. OpenCode edits that draft, never the live definition.", "validate": "Uses the same server parser as submission. Does not persist or activate schedules. Structural validity does not prove runtime/tool availability.", - "submit": "This command is explicit approval to publish; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.", - "run": "Runs in your current directory. Supplied name/prompt start immediately; otherwise select a pipeline and send a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or RUN confirmation. Cancellation does not undo edits.", + "submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.", + "run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits.", "retry": "Start a new attempt of a failed run with the same initial input, directory, pipeline version, and edit scope. Original history is retained. Already-completed side effects are not undone.", "debug": "Open a fresh OpenCode conversation to investigate a run using inspect/show/runs CLI queries. Does not resume the implementing session or retry the run. --json returns the investigation prompt without opening OpenCode.", "rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs.", diff --git a/internal/cli/confirmation.go b/internal/cli/confirmation.go new file mode 100644 index 00000000..cd09e4e4 --- /dev/null +++ b/internal/cli/confirmation.go @@ -0,0 +1,19 @@ +package cli + +import ( + "bufio" + "fmt" + "strings" +) + +func (a App) confirmAction(question string, yes bool) error { + if yes { + return nil + } + fmt.Fprint(a.Err, question+" [y/N]: ") + line, _ := bufio.NewReader(a.In).ReadString('\n') + if answer := strings.ToLower(strings.TrimSpace(line)); answer != "y" && answer != "yes" { + return fmt.Errorf("action canceled; use --yes for explicit non-interactive confirmation") + } + return nil +} diff --git a/internal/cli/confirmation_test.go b/internal/cli/confirmation_test.go new file mode 100644 index 00000000..23fb3522 --- /dev/null +++ b/internal/cli/confirmation_test.go @@ -0,0 +1,38 @@ +package cli + +import ( + "bytes" + "context" + "git.a-wan.net/alan/pipelines/internal/draft" + "os" + "strings" + "testing" +) + +func TestActionConfirmationDefaultsToNoAndKeepsJSONOutputClean(t *testing.T) { + for _, answer := range []string{"", "\n", "no\n", "yes\n", "y\n"} { + var out, stderr bytes.Buffer + a := App{Out: &out, Err: &stderr, In: strings.NewReader(answer)} + err := a.confirmAction("Retry run #42?", false) + want := answer == "yes\n" || answer == "y\n" + if (err == nil) != want || out.Len() != 0 || !strings.Contains(stderr.String(), "[y/N]") { + t.Fatal("unsafe confirmation", answer, err) + } + } +} + +func TestDraftDeletionNeedsConfirmation(t *testing.T) { + t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) + d, err := draft.Create("keep", "source", "server", true, 0) + if err != nil { + t.Fatal(err) + } + var out bytes.Buffer + a := App{Out: &out, Err: &out, In: strings.NewReader("\n")} + if err := a.Run(context.Background(), []string{"draft-delete", d.Path}); err == nil { + t.Fatal("unconfirmed deletion accepted") + } + if _, err := os.Stat(d.Path); err != nil { + t.Fatal("unconfirmed draft removed", err) + } +} diff --git a/internal/cli/draft_delete_test.go b/internal/cli/draft_delete_test.go index 57a3da3b..0adaeeb2 100644 --- a/internal/cli/draft_delete_test.go +++ b/internal/cli/draft_delete_test.go @@ -18,7 +18,7 @@ func TestDraftDeleteWorksWithoutServerConnection(t *testing.T) { } var out bytes.Buffer a := App{Out: &out, Err: &out, In: strings.NewReader("")} - if err := a.Run(context.Background(), []string{"draft-delete", d.Path, "--json", "--url", "not-a-server"}); err != nil { + if err := a.Run(context.Background(), []string{"draft-delete", d.Path, "--yes", "--json", "--url", "not-a-server"}); err != nil { t.Fatal(err) } if !strings.Contains(out.String(), "deleted") { diff --git a/internal/server/cli_test.go b/internal/server/cli_test.go index beb79e02..39162954 100644 --- a/internal/server/cli_test.go +++ b/internal/server/cli_test.go @@ -42,7 +42,7 @@ func TestCLIHeadlessRunInspectionCancellationAndRPC(t *testing.T) { err := app.Run(ctx, append([]string{"--url", url}, args...)) return out.String(), err } - output, err := run("run", "workflow", "exact task", "--json", "--request-key", "same-key") + output, err := run("run", "workflow", "exact task", "--yes", "--json", "--request-key", "same-key") if err != nil { t.Fatal(err) } @@ -88,7 +88,7 @@ func TestCLIHeadlessRunInspectionCancellationAndRPC(t *testing.T) { if _, err = run("run", "workflow", "exact task", "--yes", "--json", "--request-key", "same-key", "--wait", "--timeout", "100ms"); err == nil || !strings.Contains(err.Error(), "remains active") { t.Fatal("wait timeout canceled or incorrectly succeeded", err) } - if _, err = run("cancel", "1", "--json"); err != nil { + if _, err = run("cancel", "1", "--yes", "--json"); err != nil { t.Fatal(err) } r.wg.Wait() diff --git a/internal/server/retry_test.go b/internal/server/retry_test.go index c9e5701c..4a1417c7 100644 --- a/internal/server/retry_test.go +++ b/internal/server/retry_test.go @@ -166,7 +166,14 @@ func TestRetryReplaysInitialInputAndPinnedVersionThroughCLI(t *testing.T) { } return result, err } - result, err := runCLI("retry", fmt.Sprint(original.ID), "--json", "--wait", "--timeout", "5s", "--request-key", "retry-key") + if _, err := runCLI("retry", fmt.Sprint(original.ID), "--json"); err == nil || !strings.Contains(err.Error(), "action canceled") { + t.Fatal("unconfirmed retry accepted", err) + } + beforeRetry, err := store.ListRuns(ctx) + if err != nil || len(beforeRetry) != 1 { + t.Fatal("unconfirmed retry created a run", err) + } + result, err := runCLI("retry", fmt.Sprint(original.ID), "--yes", "--json", "--wait", "--timeout", "5s", "--request-key", "retry-key") if err != nil { t.Fatal(err) } @@ -175,7 +182,7 @@ func TestRetryReplaysInitialInputAndPinnedVersionThroughCLI(t *testing.T) { t.Fatal("retry did not preserve recorded run settings", retried) } r.wg.Wait() - again, err := runCLI("retry", fmt.Sprint(original.ID), "--json", "--request-key", "retry-key") + again, err := runCLI("retry", fmt.Sprint(original.ID), "--yes", "--json", "--request-key", "retry-key") if err != nil || again.RunDetail.ID != retried.ID { t.Fatal("retry lost idempotency", again, err) } @@ -183,7 +190,7 @@ func TestRetryReplaysInitialInputAndPinnedVersionThroughCLI(t *testing.T) { if err != nil || !reflect.DeepEqual(original, unchanged) { t.Fatal("retry modified failed history", unchanged, err) } - if _, err := runCLI("retry", fmt.Sprint(retried.ID), "--json"); err == nil { + if _, err := runCLI("retry", fmt.Sprint(retried.ID), "--yes", "--json"); err == nil { t.Fatal("succeeded run accepted for retry") } if _, err := r.retry(protocol.Message{RunID: original.ID}); err == nil { diff --git a/internal/tui/action_confirm.go b/internal/tui/action_confirm.go new file mode 100644 index 00000000..f89437bf --- /dev/null +++ b/internal/tui/action_confirm.go @@ -0,0 +1,34 @@ +package tui + +import ( + "fmt" + "git.a-wan.net/alan/pipelines/internal/protocol" + tea "github.com/charmbracelet/bubbletea" +) + +func (m Model) updateActionConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) { + switch msg.String() { + case "esc", "n": + m.actionConfirm = nil + return m, nil + case "y": + request := *m.actionConfirm + m.actionConfirm = nil + return m.sendRunRequest(request) + } + return m, nil +} + +func (m Model) actionConfirmView(height int) string { + r := m.actionConfirm + lines := []string{"Start pipeline run?", fmt.Sprintf("Pipeline #%d · v%d", r.PipelineID, r.Version), "Directory: " + r.Workspace, + "This may edit files, execute commands and publish notifications."} + if r.Type == protocol.RetryRun { + lines = []string{fmt.Sprintf("Retry failed run #%d?", r.RunID), "Starts a NEW run from the entry stage with fresh OpenCode sessions.", + "Reuses the original input, directory, pinned version and edit scope.", "Previous side effects are not undone; commands/notifications may repeat."} + } + if r.Type == protocol.CancelRun { + lines = []string{fmt.Sprintf("Cancel run #%d?", r.RunID), "Stops execution and interrupts its active agent.", "Sessions/history are retained; previous side effects are not undone."} + } + return popup(m.width, height, 96, append(lines, "", "y confirm · n / Esc cancel")) +} diff --git a/internal/tui/action_confirm_test.go b/internal/tui/action_confirm_test.go new file mode 100644 index 00000000..91acdf79 --- /dev/null +++ b/internal/tui/action_confirm_test.go @@ -0,0 +1,35 @@ +package tui + +import ( + "context" + "git.a-wan.net/alan/pipelines/internal/protocol" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/x/ansi" + "strings" + "testing" +) + +func TestConsequentialRunActionsRequireExplicitYes(t *testing.T) { + for _, action := range []string{protocol.RunPipeline, protocol.RetryRun, protocol.CancelRun} { + for _, cancel := range []string{"n", "esc"} { + m := New(context.Background(), "") + updated, cmd := m.runRequest(protocol.Message{Type: action, RunID: 42, PipelineID: 3, Version: 2, Workspace: "/original"}) + m = updated.(Model) + if cmd != nil || m.nextID != 0 || m.actionConfirm == nil { + t.Fatal("action sent before approval", action) + } + if !strings.Contains(ansi.Strip(m.View()), "y confirm") { + t.Fatal("missing confirmation", action) + } + updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) + m = updated.(Model) + if cmd != nil || m.actionConfirm == nil { + t.Fatal("Enter must not approve by default") + } + m = press(m, cancel) + if m.actionConfirm != nil || m.nextID != 0 || m.runStartRequest != "" { + t.Fatal("canceled action was sent") + } + } + } +} diff --git a/internal/tui/cli_run_test.go b/internal/tui/cli_run_test.go index 0667fc8c..32db7a09 100644 --- a/internal/tui/cli_run_test.go +++ b/internal/tui/cli_run_test.go @@ -61,12 +61,12 @@ func TestDashboardRunUsesInvocationDirectory(t *testing.T) { } } -func TestCLISuppliedArgumentsStartWithoutConfirmation(t *testing.T) { +func TestCLISuppliedArgumentsRequireConfirmation(t *testing.T) { m := NewRun(context.Background(), "", RunOptions{Pipeline: "test", Prompt: "task", Workspace: "/repos/a"}) m.detail = &protocol.PipelineDetail{PipelineSummary: protocol.PipelineSummary{ID: 1, Name: "test", Version: 1}} updated, _ := m.configureCLIRun() m = updated.(Model) - if m.runForm != 0 || !m.runOpen || m.runTask != "task" || m.runWorkspace != "/repos/a" { - t.Fatal("supplied task did not start immediately or lost repository") + if m.runForm != 0 || !m.runOpen || m.runTask != "task" || m.runWorkspace != "/repos/a" || m.actionConfirm == nil || m.actionConfirm.Workspace != "/repos/a" || m.runStartRequest != "" { + t.Fatal("supplied task bypassed confirmation or lost repository") } } diff --git a/internal/tui/home_failed_runs_test.go b/internal/tui/home_failed_runs_test.go index 9a29fc8b..6a303eba 100644 --- a/internal/tui/home_failed_runs_test.go +++ b/internal/tui/home_failed_runs_test.go @@ -49,8 +49,8 @@ func TestHomeShortcutOpensAllRunsAndDisplaysHintInBothPanels(t *testing.T) { m.runIndex = 1 updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}}) m = updated.(Model) - if cmd == nil || m.runStartRequest == "" { - t.Fatal("r on failed list no longer retries") + if cmd != nil || m.actionConfirm == nil || m.actionConfirm.Type != protocol.RetryRun { + t.Fatal("r on failed list should request retry confirmation") } m.closeRuns() if m.runFailedOnly { diff --git a/internal/tui/model.go b/internal/tui/model.go index d3302ee0..7af7eb4e 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -132,6 +132,7 @@ type Model struct { versionsOpen bool versionIndex int confirmOpen bool + actionConfirm *protocol.Message pendingDraft *editedMsg submitID string submitNotice string @@ -192,7 +193,7 @@ func New(ctx context.Context, url string) Model { enter: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "continue")), cancel: key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "cancel")), ping: key.NewBinding(key.WithKeys("t"), key.WithHelp("t", "test ping")), - quit: key.NewBinding(key.WithKeys("q", "ctrl+c"), key.WithHelp("q", "quit")), + quit: key.NewBinding(key.WithKeys(":quit"), key.WithHelp(":quit", "quit")), }, } } @@ -486,6 +487,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if msg.String() == "ctrl+c" { return m, tea.Quit } + if m.actionConfirm != nil { + return m.updateActionConfirm(msg) + } if m.editorChecking { return m, nil } @@ -547,8 +551,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.commandInput.SetValue(":") } return m, m.commandInput.Focus() - case m.homePage() && key.Matches(msg, m.keys.quit): - return m, tea.Quit case m.homePage() && key.Matches(msg, m.keys.create): if m.conn == nil { m.appendLine("not connected") @@ -712,6 +714,9 @@ func (m Model) View() string { if m.statusOpen { content = m.statusView(bodyHeight) } + if m.actionConfirm != nil { + content = m.actionConfirmView(bodyHeight) + } if m.editorChecking { content = popup(m.width, bodyHeight, 76, []string{"Checking published edits", m.loading("Refreshing pipeline information…"), "Local draft remains retained."}) } diff --git a/internal/tui/model_test.go b/internal/tui/model_test.go index 59648b49..5f4f8312 100644 --- a/internal/tui/model_test.go +++ b/internal/tui/model_test.go @@ -47,8 +47,8 @@ func TestTKeyPingRoundTrip(t *testing.T) { t.Fatalf("missing responses: %s", text) } _, quit := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}}) - if _, ok := quit().(tea.QuitMsg); !ok { - t.Fatal("q did not quit") + if quit != nil { + t.Fatal("q should not quit") } } diff --git a/internal/tui/retry_test.go b/internal/tui/retry_test.go index 63bcf839..904e9a13 100644 --- a/internal/tui/retry_test.go +++ b/internal/tui/retry_test.go @@ -66,6 +66,11 @@ func TestRetryShortcutAndPaletteSendServerRetryWithoutReplacingDirectory(t *test updated, send = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}}) } m = updated.(Model) + if send != nil || m.actionConfirm == nil || m.actionConfirm.RunID != id || m.runStartRequest != "" { + t.Fatal("retry must ask before sending") + } + updated, send = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'y'}}) + m = updated.(Model) if send == nil || m.runStartRequest == "" || m.runDetail != nil || m.requestedRunID != 0 { t.Fatal("retry did not start cleanly") } diff --git a/internal/tui/runs.go b/internal/tui/runs.go index c0cf38f4..12e3451a 100644 --- a/internal/tui/runs.go +++ b/internal/tui/runs.go @@ -16,6 +16,14 @@ type runTick struct{} func runPoll() tea.Cmd { return tea.Tick(time.Second, func(time.Time) tea.Msg { return runTick{} }) } func (m Model) runRequest(request protocol.Message) (tea.Model, tea.Cmd) { + if request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.CancelRun { + m.actionConfirm = &request + return m, nil + } + return m.sendRunRequest(request) +} + +func (m Model) sendRunRequest(request protocol.Message) (tea.Model, tea.Cmd) { if m.conn == nil { m.appendLine("not connected") return m, nil @@ -112,7 +120,14 @@ func (m *Model) startSelectedRun() tea.Cmd { m.runCollapsed = nil m.runDetail = nil m.runTop = 0 - updated, cmd := m.runRequest(protocol.Message{Type: protocol.RunPipeline, PipelineID: m.detail.ID, Version: m.detail.Version, RequestKey: fmt.Sprintf("tui-%d", time.Now().UnixNano()), Input: initialRunInput(m.runTask), AllowTools: m.runHasMutationTools(), WritePaths: m.runPaths, Workspace: m.runWorkspace}) + request := protocol.Message{Type: protocol.RunPipeline, PipelineID: m.detail.ID, Version: m.detail.Version, RequestKey: fmt.Sprintf("tui-%d", time.Now().UnixNano()), Input: initialRunInput(m.runTask), AllowTools: m.runHasMutationTools(), WritePaths: m.runPaths, Workspace: m.runWorkspace} + updated, cmd := m.runRequest(request) + if m.cliRun && m.cliOptions.Yes { + updated, cmd = m.sendRunRequest(request) + after := updated.(Model) + after.cliOptions.Yes = false + updated = after + } *m = updated.(Model) return cmd } diff --git a/internal/tui/shortcut_scope_test.go b/internal/tui/shortcut_scope_test.go index e06a8854..190611da 100644 --- a/internal/tui/shortcut_scope_test.go +++ b/internal/tui/shortcut_scope_test.go @@ -81,3 +81,24 @@ func TestCtrlCRemainsGlobal(t *testing.T) { t.Fatal("Ctrl+C did not quit") } } + +func TestQuitUsesCommandInsteadOfQ(t *testing.T) { + for _, m := range []Model{New(context.Background(), ""), panelModel(t), runInspectionModel()} { + _, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}}) + if cmd != nil { + t.Fatal("q must not quit") + } + if !slices.Contains(m.pageCommands(), ":quit") { + t.Fatal("quit command missing") + } + m = press(m, ":") + m = press(m, "quit") + _, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) + if cmd == nil { + t.Fatal("quit command did not exit") + } + if _, ok := cmd().(tea.QuitMsg); !ok { + t.Fatal("quit command did not return QuitMsg") + } + } +} diff --git a/scripts/install.py b/scripts/install.py index a0e63749..a0b59fe8 100644 --- a/scripts/install.py +++ b/scripts/install.py @@ -1,10 +1,8 @@ #!/usr/bin/env python3 -"""Compatibility entry point for the standard binary and skill installer.""" +"""Compatibility entry point for the native or full Docker installer.""" from pathlib import Path import subprocess import sys if __name__ == "__main__": - 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([str(Path(__file__).resolve().parents[1]/"install.sh")])) + sys.exit(subprocess.call([str(Path(__file__).resolve().parents[1]/"install.sh"), *sys.argv[1:]])) diff --git a/scripts/setup-docker.py b/scripts/setup-docker.py new file mode 100644 index 00000000..3406fa85 --- /dev/null +++ b/scripts/setup-docker.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +"""Configure, authenticate and verify the full Docker installation.""" +import argparse +import importlib.util +import json +from pathlib import Path +import subprocess +import sys + + +def module(name, file): + spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(file)) + result = importlib.util.module_from_spec(spec) + spec.loader.exec_module(result) + return result + + +configure = module("configure_compose", "configure-compose.py") +tokens = module("save_ntfy_token", "save-ntfy-token.py") + + +class Setup: + def __init__(self, directory, runner=subprocess.run, prompt=input): + self.directory = Path(directory) + self.runner, self.prompt = runner, prompt + + def compose(self, *args, capture=False, check=True, **kwargs): + result = self.runner(["docker", "compose", *args], cwd=self.directory, + capture_output=capture, text=True, **kwargs) + if check and result.returncode: + # Captured ntfy output may contain tokens. Never replay it on failure. + raise ValueError(f"Docker setup step failed (exit {result.returncode}); no credentials were replaced") + return result + + def settings(self, args): + settings = self.directory / ".env" + if settings.exists(): + if any((args.ntfy_url, args.bind_address, args.timezone, args.port)): + raise ValueError(".env already exists; edit it explicitly rather than replace existing configuration") + print("Keeping existing .env and credentials.") + return + url = args.ntfy_url or self.prompt("Phone-facing ntfy base URL (HTTPS or trusted LAN/VPN, no topic): ").strip() + bind = args.bind_address or self.prompt("ntfy bind IP (LAN/VPN IP for direct phone access; 127.0.0.1 for reverse proxy): ").strip() + if not bind: + raise ValueError("Choose an explicit ntfy bind address; setup will not expose it by guessing") + timezone = args.timezone + if not timezone: + try: + timezone = configure.host_timezone() + except ValueError: + timezone = self.prompt("Timezone (e.g. Europe/London): ").strip() + configure.configure(self.directory, url, bind, timezone, args.port or 8081) + + def bootstrap_ntfy(self): + token = self.directory / "secrets/ntfy-token.txt" + if token.is_symlink(): + raise ValueError("Refusing a symlink for the publishing token") + if token.exists() and token.stat().st_size: + print("Keeping existing ntfy publishing token.") + else: + print("Set a password for ntfy's pipeline-publisher account (not a model-provider account).") + self.compose("exec", "ntfy", "ntfy", "user", "add", "--ignore-exists", "pipeline-publisher") + self.compose("exec", "-T", "ntfy", "ntfy", "access", "pipeline-publisher", "pipelines", "write-only") + result = self.compose("exec", "-T", "ntfy", "ntfy", "token", "add", "pipeline-publisher", capture=True) + tokens.save_token(result.stdout + result.stderr, self.directory) + # --ignore-exists preserves the phone account's password on re-install. + print("Set the phone-reader password if this is a new ntfy account; existing accounts are preserved.") + self.compose("exec", "ntfy", "ntfy", "user", "add", "--ignore-exists", "phone-reader") + self.compose("exec", "-T", "ntfy", "ntfy", "access", "phone-reader", "pipelines", "read-only") + + def authenticate(self, relogin=False): + def connected(): + result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "opencode", + "pipelines", "auth", "list", "--format", "json", capture=True) + accounts = json.loads(result.stdout) + if not isinstance(accounts, list): + raise ValueError("Unexpected OpenCode account-list format; authentication not verified") + return any(isinstance(account, dict) and account.get("connections") for account in accounts) + + if connected() and not relogin: + print("Keeping existing OpenCode provider setup in its Docker volume.") + return + print("Choose and authenticate your provider in the container's OpenCode login below.") + self.compose("run", "--rm", "--no-deps", "--entrypoint", "opencode", "pipelines", "auth", "login") + if not connected(): + raise ValueError("Provider setup incomplete; rerun the installer to continue (credentials/volumes retained)") + + def verify(self): + # Read the token inside the container; never put it in argv or output. + probe = '''set -eu +test -s "$PIPELINES_NTFY_TOKEN_FILE" +printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FILE")" | + curl --config - --fail --silent --show-error --max-time 10 --output /dev/null \ + --data 'Pipelines Docker setup: authenticated publishing verified.' "$PIPELINES_NTFY_URL" +''' + self.compose("exec", "-T", "pipelines", "sh", "-c", probe, capture=True) + result = self.compose("exec", "-T", "pipelines", "pipelines", "--url", + "ws://127.0.0.1:8080/connect", "status", "--json", capture=True) + status = json.loads(result.stdout) + if (status.get("ntfy", {}).get("health") != "healthy" + or not status.get("ntfy", {}).get("url") or status.get("topic") != "pipelines"): + raise ValueError("Pipelines did not report healthy ntfy; check docker compose logs pipelines ntfy") + print("Verified: Pipelines reports healthy ntfy and authenticated publishing succeeded.") + print("On your phone: add the ntfy server from .env, log in as phone-reader, subscribe to pipelines.") + print("Phone delivery and model inference are not verified by this setup check.") + address = self.compose("port", "pipelines", "8080", capture=True).stdout.strip() + port = address.rsplit(":", 1)[-1] + if not port.isdigit(): + raise ValueError("Could not identify the published Pipelines port") + print(f"Host CLI/TUI connection: pipelines --url ws://127.0.0.1:{port}/connect tui") + print("Install host client binaries with ./install.sh --native if needed.") + + def run(self, args): + self.compose("version", capture=True) + self.settings(args) + # Validate the actual resolved Compose configuration before starting anything. + self.compose("config", "--quiet", capture=True) + self.compose("up", "-d", "--wait", "--wait-timeout", "60", "ntfy") + self.bootstrap_ntfy() + self.compose("build", "pipelines") + self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "skill", "install") + self.authenticate(args.relogin) + # Recreate to reload file secrets and environment, without deleting volumes. + self.compose("up", "-d", "--force-recreate", "--wait", "--wait-timeout", "60", "pipelines") + self.verify() + print("Docker setup complete. Use docker compose ps / logs to manage it; no OS user service was registered.") + + +def arguments(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--ntfy-url") + parser.add_argument("--bind-address") + parser.add_argument("--timezone") + parser.add_argument("--port", type=int) + parser.add_argument("--relogin", action="store_true", help="open OpenCode provider login again without deleting accounts") + return parser.parse_args(argv) + + +if __name__ == "__main__": + try: + Setup(Path(__file__).resolve().parents[1]).run(arguments()) + except (ValueError, OSError, EOFError, json.JSONDecodeError) as error: + sys.exit(f"Docker setup incomplete: {error}") diff --git a/scripts/test_installation.py b/scripts/test_installation.py index 850635eb..67103b45 100644 --- a/scripts/test_installation.py +++ b/scripts/test_installation.py @@ -7,6 +7,12 @@ import manage class InstallationTests(unittest.TestCase): + def test_docker_installer_is_available_through_main_entrypoint(self): + result=subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh"),"--docker","--help"],capture_output=True,text=True) + self.assertEqual(result.returncode,0) + self.assertIn("--ntfy-url",result.stdout) + self.assertIn("--relogin",result.stdout) + def test_standard_installer_needs_only_go_and_honors_gobin(self): with tempfile.TemporaryDirectory() as directory: root=Path(directory) diff --git a/scripts/test_setup_docker.py b/scripts/test_setup_docker.py new file mode 100644 index 00000000..04ffd1d1 --- /dev/null +++ b/scripts/test_setup_docker.py @@ -0,0 +1,86 @@ +import importlib.util +from pathlib import Path +import subprocess +import tempfile +import unittest + +spec = importlib.util.spec_from_file_location("setup", Path(__file__).with_name("setup-docker.py")) +setup = importlib.util.module_from_spec(spec) +spec.loader.exec_module(setup) + + +class DockerSetupTests(unittest.TestCase): + def exercise(self, directory, existing=False, fail=None, confirm="yes"): + calls = [] + token = "tk_" + "a" * 29 + logged = existing + + def runner(command, **kwargs): + nonlocal logged + args = command[2:] + calls.append(args) + code, out = 0, "" + if args[-4:] == ["auth", "list", "--format", "json"]: + out = '[{"connections":[{"id":"fixture"}]}]' if logged else '[]' + if args[-2:] == ["auth", "login"] and confirm == "yes": + logged = True + if "token" in args: + out = f"token {token} created\n" + if args[-2:] == ["status", "--json"]: + out = '{"ntfy":{"health":"healthy","url":"http://192.168.1.20:8090"},"topic":"pipelines"}' + if args[:1] == ["port"]: + out = "127.0.0.1:8081\n" + if fail and fail in args: + code, out = 9, "private provider data/token" + return subprocess.CompletedProcess(command, code, stdout=out, stderr="") + + args = setup.arguments([] if existing else ["--ntfy-url", "http://192.168.1.20:8090", + "--bind-address", "192.168.1.20", "--timezone", "Europe/London"]) + installer = setup.Setup(directory, runner=runner, prompt=lambda _: confirm) + installer.run(args) + return calls, token + + def test_fresh_setup_wires_notifications_and_runs_container_login(self): + with tempfile.TemporaryDirectory() as directory: + calls, token = self.exercise(directory) + self.assertIn(["run", "--rm", "--no-deps", "--entrypoint", "opencode", "pipelines", "auth", "login"], calls) + self.assertTrue(any("--force-recreate" in c for c in calls)) + self.assertEqual(Path(directory, "secrets/ntfy-token.txt").read_text(), token + "\n") + self.assertFalse(any(token in " ".join(c) for c in calls)) + self.assertLess(next(i for i,c in enumerate(calls) if "token" in c), next(i for i,c in enumerate(calls) if "build" in c)) + self.assertLess(next(i for i,c in enumerate(calls) if "login" in c), next(i for i,c in enumerate(calls) if "--force-recreate" in c)) + self.assertTrue(any("curl --config -" in " ".join(c) for c in calls)) + + def test_reinstall_preserves_configuration_tokens_and_provider_setup(self): + with tempfile.TemporaryDirectory() as directory: + self.exercise(directory) + files = [Path(directory, ".env"), Path(directory, "secrets/ntfy-token.txt")] + before = [p.read_bytes() for p in files] + calls, _ = self.exercise(directory, existing=True) + self.assertFalse(any("login" in c or "token" in c for c in calls)) + self.assertEqual(before, [p.read_bytes() for p in files]) + + def test_login_failure_or_cancel_does_not_mark_complete_or_start_server(self): + for fail, confirm in [("login", "yes"), (None, "no")]: + with tempfile.TemporaryDirectory() as directory: + with self.assertRaises(ValueError) as raised: + self.exercise(directory, fail=fail, confirm=confirm) + self.assertNotIn("private provider", str(raised.exception)) + self.assertTrue(Path(directory, "secrets/ntfy-token.txt").stat().st_size) + + def test_status_and_publication_failures_are_not_success(self): + for failure in ["status", "sh"]: + with tempfile.TemporaryDirectory() as directory, self.assertRaises(ValueError): + self.exercise(directory, fail=failure) + + def test_existing_env_is_not_replaced_by_flags(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory, ".env") + path.write_text("existing settings") + with self.assertRaises(ValueError): + setup.Setup(directory).settings(setup.arguments(["--ntfy-url", "https://example.com"])) + self.assertEqual(path.read_text(), "existing settings") + + +if __name__ == "__main__": + unittest.main()