From 8cde9898e3367cf64713b9c6645ed266c36ed9cd Mon Sep 17 00:00:00 2001 From: cooper Date: Mon, 9 Nov 2026 03:20:33 -0800 Subject: [PATCH] refactor(setup): simplify agent setup and stop blocking on strictness The agent may now edit any repository file the setup needs; the user reviews the result in Git. Removed: the Git guard that protected dirty files and aborted on index/HEAD changes, saved malformed-reply recovery, strict reply and plan validation, fatal permission denials and provider warnings, the separate flake-lock step, retained required checks, and 15-minute stage limits. Replies are parsed leniently (first usable JSON object, truncated or fenced JSON, unusable entries dropped); prose becomes the plan summary or a completed turn. Doctor still checks the result with one repair turn, skipped checks are not failures, and remaining problems are reported instead of blocking runtime setup. New files and Nix inputs are still exposed to flakes with intent-to-add. --- apps/docs/content/docs/cli/setup.mdx | 254 +++--- apps/stackpanel-go/cmd/cli/doctor.go | 2 +- apps/stackpanel-go/cmd/cli/setup_agent.go | 780 +++++++++--------- .../cmd/cli/setup_agent_conversation.go | 72 +- .../cmd/cli/setup_agent_conversation_test.go | 43 + apps/stackpanel-go/cmd/cli/setup_agent_git.go | 321 ++----- .../cmd/cli/setup_agent_git_test.go | 235 +----- .../cmd/cli/setup_agent_reply.go | 97 --- .../cmd/cli/setup_agent_reply_test.go | 293 ------- .../cmd/cli/setup_agent_runtime.go | 15 +- .../cmd/cli/setup_agent_state.go | 193 ++--- .../cmd/cli/setup_agent_state_test.go | 224 ++--- .../stackpanel-go/cmd/cli/setup_agent_test.go | 194 +---- .../internal/setupagent/agent.go | 320 +++---- .../internal/setupagent/agent_test.go | 279 ++----- .../internal/setupagent/installed_test.go | 51 +- apps/stackpanel-go/internal/setupagent/pi.go | 11 +- .../internal/setupagent/pi_test.go | 8 +- .../internal/setupagent/pi_tools.go | 21 +- .../internal/setupagent/prompt.go | 636 +++++++------- .../internal/setupagent/reply_test.go | 99 ++- apps/stackpanel-go/internal/tui/setup.go | 4 + tests/test-agent-setup.sh | 480 +++++------ 23 files changed, 1700 insertions(+), 2932 deletions(-) delete mode 100644 apps/stackpanel-go/cmd/cli/setup_agent_reply.go delete mode 100644 apps/stackpanel-go/cmd/cli/setup_agent_reply_test.go diff --git a/apps/docs/content/docs/cli/setup.mdx b/apps/docs/content/docs/cli/setup.mdx index 30d98ecb..e4c2eb74 100644 --- a/apps/docs/content/docs/cli/setup.mdx +++ b/apps/docs/content/docs/cli/setup.mdx @@ -78,11 +78,11 @@ private setup manifest; rerun the same command to resume. `--restart` discards the conversation and reviews a fresh plan. The current setup instructions are supplied as a system prompt instead of repeated in each saved user message. -This experiment exposes Pi's `read`, `ls`, `write`, and `edit` tools, with checks -for paths outside the repository, symlinks, private state, and protected user -files. It does not expose shell tools or load Pi hooks, skills, or parent -configuration automatically. These checks are application policy, not an OS -sandbox. Stackpanel runs Nix and doctor on the host. +This experiment exposes Pi's `read`, `ls`, `write`, and `edit` tools, confined +to the repository, with checks for symlinks and private state. It does not +expose shell tools or load Pi hooks, skills, or parent configuration +automatically. These checks are application policy, not an OS sandbox. +Stackpanel runs Nix and doctor on the host. The Go port currently requires Go 1.26 and does not implement Codex subscription transport or OAuth login. This backend reads `OPENAI_API_KEY` or @@ -94,16 +94,15 @@ to an installed CLI. Plain `auto` retains CLI discovery; `auto` with ### Installed CLI backends `--experimental-agent` delegates repository inspection and configuration to an -installed coding agent, then verifies the result with `stack doctor`. -Codex and Claude Code are supported when their installed versions expose the -required automation and permission controls. OpenCode is discovered but currently -reported as unsupported. Agent discovery does not establish authentication; -authenticate the chosen CLI before running setup. Its configured model and -authentication are reused. +installed coding agent, then checks the result with `stack doctor`. Codex and +Claude Code are supported. Setup checks that the CLI runs, but not that it is +signed in; sign in to the chosen CLI before running setup. Its configured model +and authentication are reused. -With `auto`, a single supported agent is selected automatically. Multiple agents -produce a picker in an interactive terminal; noninteractive runs must select one -explicitly. `--yes` and `--non-interactive` accept the proposed onboarding plan. +With `auto`, a single installed agent is selected automatically. Multiple agents +produce a picker in an interactive terminal; noninteractive runs use the first +one found (Codex). `--yes` and `--non-interactive` accept the proposed +onboarding plan. The wizard runs full screen, centered in the terminal, and uses the CLI's shared terminal theme, with a stage indicator, elapsed time, and a scrollable plan. Use @@ -114,33 +113,32 @@ during setup; Esc or Ctrl+C cancels. When the wizard closes, the terminal return to its previous contents, followed by the outcome and any warnings. The wizard asks questions when choices are unresolved and lets you apply, revise, -or cancel a concrete plan. Question rounds preserve -previous answers and do not consume the single doctor repair attempt. `--yes` -and noninteractive runs use declared defaults; a required question without a -default stops with an actionable error. Full provider event logs are available -through `--agent-log /path/to/new-log.jsonl` (created privately, never overwritten). -On resume, an existing log is preserved and a new sibling `.resume-*.jsonl` log is created. - -If a completed agent response contains malformed JSON, setup saves that reply and -requests one read-only formatting correction. This does not repeat repository edits -or consume the doctor repair attempt. The corrected response must pass the same -validation; a proposed plan still requires review and doctor still verifies setup. -Provider failures, permission denials, and semantic validation errors are not treated -as formatting errors. If correction fails, rerunning setup retries the saved reply. - -The agent first inspects the repository without writing and returns a concrete -plan with configuration expectations. After acceptance, Stackpanel writes missing -scaffold files from the already-fetched template. The sandboxed agent then edits -the repository's flake, apps and commands. Stackpanel creates or updates -`flake.lock` on the host, where the Nix daemon is accessible. The agent does not -run Nix, enter a devshell, or invoke scaffolding itself. Setup keeps expectations fixed -through the rest of the run. Explicit addon selections become configuration -assertions too; in this experiment `--without` requires the addon's enable switch -to be false. Unknown addons and choices are rejected. Git framework sources are -pinned. Dirty local framework checkouts are rejected: omit `--flake` or select a -committed revision with `git+file:///absolute/path?rev=`. This avoids -copying ignored local state into the Nix store through directory inputs. -The resulting repository must contain `flake.lock`. +or cancel a concrete plan. Question rounds preserve previous answers. `--yes` and +noninteractive runs take each question's default; a question without one is +left for the agent to decide. Full provider event logs are available through +`--agent-log /path/to/new-log.jsonl` (created privately, never overwritten). On +resume, an existing log is preserved and a new sibling `.resume-*.jsonl` log is +created. + +Agents are not held to an exact reply format. Setup reads the first usable JSON +object in a reply, including one wrapped in prose or Markdown fences or cut off +before its last braces, and drops plan entries it cannot use, such as a command +outside the repository. If an inspection reply contains no JSON at all, its text +becomes the plan summary for you to review. A write turn's reply only reports +what the agent did; doctor checks the result either way. + +The agent first inspects the repository without writing and returns a plan, +which you review. After you accept it, Stackpanel writes any missing scaffold +files from the already-fetched template, and the agent makes the changes. It +may edit any file in the repository, including existing and uncommitted ones; +setup never commits, stages content, or reverts anything, so review the result +with `git diff`. The agent does not run Nix, enter a devshell, or invoke +scaffolding itself. Explicit addon selections become configuration checks; in +this experiment `--without` requires the addon's enable switch to be false. +Unknown addons and choices are rejected. Git framework sources are pinned. Dirty +local framework checkouts are rejected: omit `--flake` or select a committed +revision with `git+file:///absolute/path?rev=`. This avoids copying +ignored local state into the Nix store through directory inputs. The host supplies repository-onboarding option names, types, and descriptions from the pinned framework's `lib.getOptions`, including Nix-only app tooling and @@ -150,55 +148,44 @@ repaired. Older saved sessions gain this schema before the next agent invocation their answers, accepted plan, template, and framework revision are retained. A plan can list host preparation commands, such as `bun install`, for the -dependencies and lockfiles a sandboxed agent cannot produce. The plan review shows -them, and accepting the plan approves them. Stackpanel runs them unchanged in the -repository devshell, with network access, after reconciliation and before every -doctor run. Lockfiles they create become visible to Git-backed Nix evaluation when -the plan lists them as files. Doctor itself never installs anything, so -`doctor --onboarding` does not run them. - -Stackpanel then enters a fresh Nix shell, reconciles generated files, runs the -plan's host preparation, and runs: +dependencies and lockfiles the agent should not produce itself. The plan review +shows them, and accepting the plan approves them. Stackpanel runs them unchanged +in the repository devshell, with network access, after reconciliation and before +every doctor run. Doctor itself never installs anything, so `doctor --onboarding` +does not run them. + +Flakes in a Git repository only see files Git knows about. Before each Nix step, +setup marks the files it created, the files the plan lists, and untracked Nix +inputs (`*.nix`, `flake.lock`, `.stack/`) with `git add --intent-to-add`. That +records the paths without staging their contents; generated output and private +state under `.stack/` are never added. + +Stackpanel then enters a fresh Nix shell (which creates or extends `flake.lock` +as needed), reconciles generated files, runs the plan's host preparation, and +runs: ```bash stack doctor --strict --scope repo,build --build --expectations /path/to/expectations.json --json ``` -Only doctor determines success. Failed input locking, reconciliation, host preparation, or verification is sent -back for one repair attempt, followed by another fresh reconciliation and doctor -run. Checks observed on the first doctor run are also required during repair, so -removing a failing check cannot make repair pass. Unavailable required permissions, -authentication failures, and agent execution failures stop the run. Claude uses native read/edit tools; shell commands run on the host. -A successful Claude reply can contain denied optional reads. These are displayed -as warnings and do not grant access; doctor still decides whether the agreed -outputs pass. Denied edits, commands, and unknown tools still stop setup with the -denied tool named in the error. Each agent or shell stage has a 15-minute deadline, -and interruption cancels its subprocesses. - -If the second doctor report still has findings, setup exits `0` with a warning -and leaves the session unverified and resumable. It preserves the files and -choices and prints an exact command to rerun `doctor --onboarding` in a fresh -devshell using the current CLI. Doctor executes the saved acceptance checks -without an agent and still exits `1` when they fail. Runtime and Studio setup -remain pending until repository verification passes. Invalid doctor reports, -failed Nix execution, and agent errors still stop setup with a nonzero exit. +If entering the shell, reconciliation, host preparation, or doctor fails, the +agent gets one repair turn with the failure, followed by another fresh check. +Checks doctor cannot run here, which it reports as skipped, are not treated as +failures. If doctor still reports problems after the repair turn, setup lists +them and continues to runtime and Studio setup; rerunning the same command +checks the repository again. If a step before doctor still fails, setup stops +with the error. + +Claude uses native read and edit tools; shell commands run on the host. When +Claude reports a tool request it was denied, setup shows it as a warning and +continues. Each agent or shell step has a one-hour limit, and interruption +cancels its subprocesses. `--dry-run` and `--json` retain the ordinary read-only setup report and never launch an agent. Agent onboarding cannot be combined with `--only`, `--skip`, or `--reconsider`. Shell entry still executes the repository's normal hooks, including generation and project registration; this also applies to `--tmp` repositories. -Stackpanel records the initial Git index and existing dirty files. Changes to -those user edits stop verification without reverting files. New onboarding inputs -and source/manifests listed in the accepted plan are marked with -`git add --intent-to-add` for Git-backed Nix evaluation. These -entries remain after success so subsequent shell entry sees the files; their -contents are not staged. Verification warnings also retain these entries so -doctor can re-evaluate the repository. Other failed setup operations remove their -temporary entries. Once repository -verification passes, the entries remain even if runtime setup fails, so the -configured repository stays evaluable. Review the resulting repository diff. - ### Create a new repository ```bash @@ -210,9 +197,8 @@ that same directory once it contains files. The wizard asks what you want to bui Stackpanel templates for the environment, and generates the application starter. New flake plans follow the framework's pinned Nix inputs unless other versions were requested, keeping the framework and its dependencies compatible. -The reviewed plan includes required files and build/test commands. Doctor checks -those exact paths and argument arrays in the target devshell; repair cannot -replace them. `--new` cannot be combined with `--tmp`, `--json`, or `--dry-run`. +The reviewed plan lists the files and build/test commands doctor checks. +`--new` cannot be combined with `--tmp`, `--json`, or `--dry-run`. ### Resume an interrupted setup @@ -228,9 +214,9 @@ Setup saves a versioned JSON manifest beside the user config, normally the exact path. It respects `XDG_CONFIG_HOME` and `STACKPANEL_USER_CONFIG`. Files are private, written atomically, and kept outside the repository. The manifest records the selected agent, template and addon choices, question/answer transcript, -proposed or accepted plan, verification failures, Git fingerprints, and runtime -handoff. Malformed final replies are also saved for recovery. Full provider event -logs remain opt-in through `--agent-log`. +proposed or accepted plan, the last failure, the untracked files that predate the +setup, and runtime handoff. Full provider event logs remain opt-in through +`--agent-log`. A manifest this release cannot read is replaced by a new setup. Repeating `--tmp` from the same directory reuses its latest unfinished repository. Repeating `--new ./my-app` resumes that target. You can also enter the recorded @@ -239,29 +225,19 @@ setup does not prevent the next `--tmp` run from creating a new repository. Saved answers are not asked again, and an accepted plan is not reviewed again. If the agent was interrupted, it receives the choices and plan and inspects the -current files to continue. If it finished generating files, setup resumes host -verification without invoking the coding agent. Locking, reconciliation, and -doctor run again against the current repository; saved completion markers never -count as verification. A failed verification allows at most one repair per -invocation, preserving the accepted expectations and previously observed checks -across retries. A valid Studio handoff is reused, with live readiness checked again. - -Unchanged, checkpointed setup output remains editable. New user edits and staging -between runs are protected. Files changed after the last checkpoint, including -output left by a hard kill, are conservatively protected too. Setup never restores -files or resets Git history. A detected Git protection violation must be resolved -before resuming. +current files to continue. If it finished its changes, setup resumes the host +check without invoking the coding agent. Reconciliation and doctor run again +against the current repository; saved progress never counts as verification. A +valid Studio handoff is reused, with live readiness checked again. Omitted configuration flags inherit saved selections. Changing `--template`, -`--flake`, or addon choices requires `--restart` to review a new plan. Runtime -options such as `--agent-port`, `--studio-url`, and `--no-runtime=false` can be -changed explicitly on retry; an explicit agent selection can also replace the -saved provider. +`--flake`, `--agent-model`, or addon choices starts a new plan. Runtime options +such as `--agent-port`, `--studio-url`, and `--no-runtime=false` can be changed +on retry; an explicit agent selection can also replace the saved provider. Use `--restart` to discard saved choices and plan a new setup while keeping existing repository files. With `--tmp`, it creates a fresh temporary repository -and keeps the old one. Runs made before manifests were introduced cannot recover -their choices automatically; onboard the existing directory to preserve its files. +and keeps the old one. ### Local agent and Studio @@ -283,44 +259,44 @@ be updated with that feature before it can acknowledge readiness. option reports browser readiness. Runtime setup does not invoke the coding agent again or let it decide whether verification passed. -Claude Code must already permit the necessary setup shell commands; permission -denials stop the experiment with the provider's diagnostic. - ## Flags -| Flag | Type | Default | Description | -|------|------|---------|-------------| -| `--yes, -y` | `bool` | `false` | Skip confirmation and take every default answer | -| `--json` | `bool` | `false` | Print the plan as JSON and exit without applying | -| `--dry-run` | `bool` | `false` | Show the report and exit without applying | -| `--only` | `strings` | _none_ | Run only these reconcilers (repeatable) | -| `--skip` | `strings` | _none_ | Skip these reconcilers (repeatable) | -| `--reconsider` | `bool` | `false` | Re-offer addons that were declined at their current revision | -| `--force` | `bool` | `false` | Overwrite existing scaffolding files and rewrite generated files | -| `--flake` | `string` | _none_ | Stackpanel flake reference used for scaffolding (default: github:darkmatter/stackpanel) | -| `--template` | `string` | `default` | Scaffolding template name | -| `--tmp` | `bool` | `false` | Create the project in a temporary git repository and print its path | -| `--non-interactive` | `bool` | `false` | Never prompt (same as `--yes`) | -| `--with` | `strings` | _none_ | Accept an addon by id without prompting (repeatable) | -| `--without` | `strings` | _none_ | Decline an addon by id without prompting (repeatable) | -| `--addon` | `strings` | _none_ | Answer an addon explicitly as `id=value` (true/false, a choice value, or comma-separated values) | -| `--build` | `bool` | `false` | Also realize build-scope doctor checks with `nix build` | -| `--new` | `string` | _none_ | Scaffold a new repository in an absent or empty directory | -| `--studio-url` | `string` | hosted Studio | Studio URL for the browser handoff | -| `--agent-port` | `int` | agent default | Local agent port | -| `--no-browser` | `bool` | `false` | Verify runtime without opening the browser | -| `--no-runtime` | `bool` | `false` | Verify repository only | -| `--agent-log` | `string` | _none_ | Private raw provider debug log; path must not exist | -| `--experimental-agent` | `string` | _none_ | Experimental repository onboarding using `auto`, `codex`, `claude`, or `pi` | -| `--agent-model` | `string` | _none_ | Pi API provider/model, saved for resume; requires an API key | -| `--restart` | `bool` | `false` | Review a new agent onboarding plan; with `--tmp`, create a fresh repository | +| Flag | Type | Default | Description | +| ---------------------- | --------- | ------------- | ------------------------------------------------------------------------------------------------ | +| `--yes, -y` | `bool` | `false` | Skip confirmation and take every default answer | +| `--json` | `bool` | `false` | Print the plan as JSON and exit without applying | +| `--dry-run` | `bool` | `false` | Show the report and exit without applying | +| `--only` | `strings` | _none_ | Run only these reconcilers (repeatable) | +| `--skip` | `strings` | _none_ | Skip these reconcilers (repeatable) | +| `--reconsider` | `bool` | `false` | Re-offer addons that were declined at their current revision | +| `--force` | `bool` | `false` | Overwrite existing scaffolding files and rewrite generated files | +| `--flake` | `string` | _none_ | Stackpanel flake reference used for scaffolding (default: github:darkmatter/stackpanel) | +| `--template` | `string` | `default` | Scaffolding template name | +| `--tmp` | `bool` | `false` | Create the project in a temporary git repository and print its path | +| `--non-interactive` | `bool` | `false` | Never prompt (same as `--yes`) | +| `--with` | `strings` | _none_ | Accept an addon by id without prompting (repeatable) | +| `--without` | `strings` | _none_ | Decline an addon by id without prompting (repeatable) | +| `--addon` | `strings` | _none_ | Answer an addon explicitly as `id=value` (true/false, a choice value, or comma-separated values) | +| `--build` | `bool` | `false` | Also realize build-scope doctor checks with `nix build` | +| `--new` | `string` | _none_ | Scaffold a new repository in an absent or empty directory | +| `--studio-url` | `string` | hosted Studio | Studio URL for the browser handoff | +| `--agent-port` | `int` | agent default | Local agent port | +| `--no-browser` | `bool` | `false` | Verify runtime without opening the browser | +| `--no-runtime` | `bool` | `false` | Verify repository only | +| `--agent-log` | `string` | _none_ | Private raw provider debug log; path must not exist | +| `--experimental-agent` | `string` | _none_ | Experimental repository onboarding using `auto`, `codex`, `claude`, or `pi` | +| `--agent-model` | `string` | _none_ | Pi API provider/model, saved for resume; requires an API key | +| `--restart` | `bool` | `false` | Review a new agent onboarding plan; with `--tmp`, create a fresh repository | ## The discovery ledger `.stack/reconcile.json` records what the user has been shown at which revision: ```json -{ "version": 1, "seen": { "playwright": { "revision": 1, "answer": false, "at": "2026-09-04T01:22:11Z" } } } +{ + "version": 1, + "seen": { "playwright": { "revision": 1, "answer": false, "at": "2026-09-04T01:22:11Z" } } +} ``` An offer is re-shown only when its author bumps `revision`, or with @@ -331,9 +307,9 @@ automatically. These flags are inherited from parent commands. -| Flag | Type | Default | Description | -|------|------|---------|-------------| -| `--daemon, -d` | `bool` | `false` | Run in daemon mode (no TUI, for background processes) | -| `--no-color` | `bool` | `false` | Disable color output | -| `--no-tui` | `bool` | `false` | Disable interactive TUI mode | -| `--verbose, -v` | `bool` | `false` | Enable verbose output | +| Flag | Type | Default | Description | +| --------------- | ------ | ------- | ----------------------------------------------------- | +| `--daemon, -d` | `bool` | `false` | Run in daemon mode (no TUI, for background processes) | +| `--no-color` | `bool` | `false` | Disable color output | +| `--no-tui` | `bool` | `false` | Disable interactive TUI mode | +| `--verbose, -v` | `bool` | `false` | Enable verbose output | diff --git a/apps/stackpanel-go/cmd/cli/doctor.go b/apps/stackpanel-go/cmd/cli/doctor.go index 64e05df8..738480a6 100644 --- a/apps/stackpanel-go/cmd/cli/doctor.go +++ b/apps/stackpanel-go/cmd/cli/doctor.go @@ -118,7 +118,7 @@ func collectDoctorReport(ctx context.Context, root string, opts doctorOptions) ( if err != nil { return nil, fmt.Errorf("load saved onboarding plan: %w", err) } - if state.Plan == nil || state.Stage == "inspection" || state.Stage == "review" { + if state.Plan == nil || state.Stage == stageInspection || state.Stage == stageReview { return nil, fmt.Errorf("onboarding has no accepted plan; run stack setup first") } expected = state.Plan.Expectations diff --git a/apps/stackpanel-go/cmd/cli/setup_agent.go b/apps/stackpanel-go/cmd/cli/setup_agent.go index 095cae4d..1aea6700 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent.go @@ -23,440 +23,483 @@ import ( "github.com/spf13/cobra" ) -const setupStageTimeout = 15 * time.Minute +// setupStageTimeout only guards against a hung process: a first devshell +// build or a long agent turn can legitimately take many minutes. +const setupStageTimeout = time.Hour -// runAgentSetup resumes host checkpoints; only fresh doctor results prove success. +const ( + maxPlanReviews = 8 // plan revisions before setup gives up + maxRepairs = 1 // repair turns after doctor finds problems +) + +// agentSetupRun carries one `stack setup --experimental-agent` invocation +// through the manifest stages. Each stage change is saved, so a rerun resumes +// where this one stopped. +type agentSetupRun struct { + ctx context.Context + cmd *cobra.Command + opts setupFlags + interactive bool + ui *tui.SetupUI + state *setupManifest + resuming bool + // request aliases state.Request, so answers are saved with the manifest. + request *setupagent.SetupRequest + template setupTemplate + newFiles *setupNewFiles + debug io.Writer + // The agent is resolved on first use: re-verifying a resumed setup needs none. + agent setupagent.Agent + agentReady bool +} + +// setupTemplate is the part of request.Context the host itself uses. +type setupTemplate struct { + Files map[string]string `json:"templateFiles"` + Addons []nixeval.AddonSpec `json:"addons"` +} + +// runAgentSetup resumes saved progress; only a fresh doctor run verifies it. func runAgentSetup(cmd *cobra.Command, opts setupFlags) (retErr error) { - originalContext := cmd.Context() - ctx, stop := signal.NotifyContext(originalContext, os.Interrupt, syscall.SIGTERM) - defer stop() - cmd.SetContext(ctx) - defer cmd.SetContext(originalContext) + switch opts.experimentalAgent { + case "auto", "codex", "claude", "pi": + default: + return fmt.Errorf("unknown --experimental-agent %q: use auto, codex, claude, or pi", opts.experimentalAgent) + } if len(opts.only) > 0 || len(opts.skip) > 0 || opts.reconsider { return errors.New("--experimental-agent cannot be combined with --only, --skip or --reconsider") } if opts.agentModel != "" && opts.experimentalAgent != "pi" && opts.experimentalAgent != "auto" { return errors.New("--agent-model requires --experimental-agent=pi") } + originalContext := cmd.Context() + defer cmd.SetContext(originalContext) + ctx, stop := signal.NotifyContext(originalContext, os.Interrupt, syscall.SIGTERM) + defer stop() noTUI, _ := cmd.Flags().GetBool("no-tui") daemon, _ := cmd.Flags().GetBool("daemon") interactive := tui.IsInteractiveStdio() && !opts.yes && !opts.nonInteractive && !noTUI && !daemon - ui := tui.NewSetupUI(cmd.Context(), interactive, cmd.ErrOrStderr()) + ui := tui.NewSetupUI(ctx, interactive, cmd.ErrOrStderr()) defer ui.Close() - cmd.SetContext(ui.Context()) - state, release, resuming, err := openSetupManifest(cmd.Context(), opts) + ctx = ui.Context() + cmd.SetContext(ctx) + + state, release, resuming, err := openSetupManifest(ctx, opts) if err != nil { return err } defer release() - defer func() { - state.LastError = "" - if retErr != nil { - state.LastError = retErr.Error() - } - retErr = errors.Join(retErr, state.save()) - if retErr != nil { - retErr = fmt.Errorf("%w\nSetup saved for %s\nRerun the same command to resume. Manifest: %s", retErr, state.Root, state.path) - } - }() + defer state.recordOutcome(&retErr) + r := &agentSetupRun{ctx: ctx, cmd: cmd, opts: opts, interactive: interactive, ui: ui, state: state, resuming: resuming, debug: io.Discard} if resuming { - if err := state.restoreOptions(cmd, &opts); err != nil { - return err - } - if err := state.checkGitViolation(cmd.Context()); err != nil { - return err - } - ui.Progress(fmt.Sprintf("Resuming %s · %d saved answers\n%s", state.Stage, len(state.Request.Answers), state.Root)) - // A failed repair or previously verified runtime may have been fixed by - // the user. Check current files before spending another model turn. - if state.Stage == "repair" || state.Stage == "runtime" || state.Stage == "complete" { - state.Stage = "verify" - } + r.resume() } ui.Progress("Setup manifest: " + state.path) - if opts.experimentalAgent == "auto" && opts.agentModel != "" { - opts.experimentalAgent = "pi" + if err := r.preflightPi(); err != nil { + return err } - var agent setupagent.Agent - if opts.experimentalAgent == "pi" && (state.Stage == "inspection" || state.Stage == "apply") { - agent, err = setupagent.NewPiAgent(opts.agentModel) - if err != nil { - return err - } + if r.newFiles, err = newSetupFiles(ctx, state); err != nil { + return err } - agentReady := false - ensureCodingAgent := func() error { - if agentReady { - return nil - } - if opts.experimentalAgent == "pi" && agent.ID == "" { - agent, err = setupagent.NewPiAgent(opts.agentModel) - if err != nil { - return err - } - } - ui.Stage(tui.SetupInspect, "Loading the pinned Stackpanel configuration schema…") - if err := ensureSetupOptionContext(cmd.Context(), &state.Request); err != nil { + ui.Identify(state.Root, state.Agent) + if err := r.prepareRequest(); err != nil { + return err + } + closeLog, err := r.openAgentLog() + if err != nil { + return err + } + defer closeLog() + if err := r.agreePlan(); err != nil { + return err + } + return r.applyAndVerify() +} + +func (r *agentSetupRun) resume() { + if changed := r.state.restoreOptions(r.cmd, &r.opts); changed != "" { + r.ui.Progress(fmt.Sprintf("--%s differs from the saved setup; starting a new plan.", changed)) + r.state.restart(r.opts) + r.resuming = false + return + } + r.ui.Progress(fmt.Sprintf("Resuming %s · %d saved answers\n%s", r.state.Stage, len(r.state.Request.Answers), r.state.Root)) + // The user may have fixed a failed or finished setup since; check the + // current files before spending another agent turn. + switch r.state.Stage { + case stageRepair, stageRuntime, stageComplete: + r.state.Stage = stageVerify + } +} + +// A model selection implies Pi. Its API key is checked before anything is +// written when the next stage needs a model turn. +func (r *agentSetupRun) preflightPi() error { + if r.opts.experimentalAgent == "auto" && r.opts.agentModel != "" { + r.opts.experimentalAgent = "pi" + } + if r.opts.experimentalAgent == "pi" && (r.state.Stage == stageInspection || r.state.Stage == stageApply) { + return r.loadPiAgent() + } + return nil +} + +func (r *agentSetupRun) loadPiAgent() error { + if r.agent.ID != "" { + return nil + } + agent, err := setupagent.NewPiAgent(r.opts.agentModel) + if err != nil { + return err + } + r.agent = agent + return nil +} + +// ensureAgent resolves the coding agent before its first turn. +func (r *agentSetupRun) ensureAgent() error { + if r.agentReady { + return nil + } + if r.opts.experimentalAgent == "pi" { + if err := r.loadPiAgent(); err != nil { return err } - ui.Stage(tui.SetupInspect, "Finding available coding agents…") - var err error - if agent.ID == "" { - agent, err = selectSetupAgent(cmd.Context(), opts.experimentalAgent, interactive, ui) - } + } + r.ui.Stage(tui.SetupInspect, "Loading the pinned Stackpanel configuration schema…") + if err := ensureSetupOptionContext(r.ctx, r.request); err != nil { + return err + } + if r.agent.ID == "" { + r.ui.Stage(tui.SetupInspect, "Finding available coding agents…") + agent, err := selectSetupAgent(r.ctx, r.opts.experimentalAgent, r.interactive, r.ui) if err != nil { return err } - if state.Agent != "" && state.Agent != agent.ID && state.Stage == "inspection" { - state.PendingReply = nil - } - state.Agent = agent.ID - agentReady = true - ui.Identify(state.Root, agent.ID) - if agent.ID == "pi" { - ui.Progress("Pi · " + opts.agentModel + " · direct API (metered usage)") - } - return state.save() + r.agent = agent } - root := state.Root - gitGuard, err := captureSetupGit(cmd.Context(), root) - if err != nil { - return err + r.state.Agent = r.agent.ID + r.agentReady = true + r.ui.Identify(r.state.Root, r.agent.ID) + if r.agent.ID == "pi" { + r.ui.Progress("Pi · " + r.opts.agentModel + " · direct API (metered usage)") } - gitGuard.allowStackpanelConfig(root) - if resuming { - state.restoreGit(gitGuard) - } - keepNixInputs := false - defer func() { - cleanupCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - guardErr := gitGuard.Check(cleanupCtx) - retErr = errors.Join(retErr, guardErr) - if guardErr != nil && gitGuard.root != "" { - state.GitViolation = &setupGitCheckpoint{Root: gitGuard.root, Head: gitGuard.head, Index: gitGuard.index, Protected: gitGuard.protected} - } - if !keepNixInputs || guardErr != nil { - retErr = errors.Join(retErr, gitGuard.Close(cleanupCtx)) - } - // This also records partial agent output on cancellation or CLI errors. - if guardErr == nil { - retErr = errors.Join(retErr, state.checkpoint(cleanupCtx, gitGuard)) - } - }() - ui.Identify(root, state.Agent) + return r.state.save() +} + +// prepareRequest loads the pinned template once per manifest and refreshes +// the fields that describe this invocation. +func (r *agentSetupRun) prepareRequest() error { + state := r.state if state.Request.Context == "" { - ui.Stage(tui.SetupInspect, "Loading the Stackpanel template…") - state.Request, err = prepareSetupRequest(cmd.Context(), root, opts) - if err != nil { + r.ui.Stage(tui.SetupInspect, "Loading the Stackpanel template…") + var err error + if state.Request, err = prepareSetupRequest(r.ctx, state.Root, r.opts); err != nil { return err } } - request := &state.Request - // Use this invocation's binary even if the previously used Nix build has - // been garbage collected; retain the original template and pinned inputs. - executable, err := os.Executable() + r.request = &state.Request + // Use this binary even if the one that started the setup was garbage + // collected; the template and pinned inputs stay as saved. + executable, err := currentStackExecutable() if err != nil { return err } - request.StackExecutable, err = filepath.EvalSymlinks(executable) - if err != nil { - return err - } - request.Resuming = resuming - request.PreviousError = state.LastError - if strings.Contains(request.PreviousError, ".stack/config.nix") || strings.Contains(request.PreviousError, ".stack/data.nix") { - request.PreviousError = "" - } - request.ProtectedPaths = nil - for _, path := range sortedKeys(gitGuard.protected) { - rel, err := filepath.Rel(root, filepath.Join(gitGuard.root, path)) - if err == nil && filepath.IsLocal(rel) { - request.ProtectedPaths = append(request.ProtectedPaths, rel) - } + r.request.StackExecutable = executable + r.request.Resuming = r.resuming + r.request.PreviousError = state.LastError + if err := json.Unmarshal([]byte(r.request.Context), &r.template); err != nil { + return fmt.Errorf("read saved template: %w", err) } - var metadata struct { - Files map[string]string `json:"templateFiles"` - Addons []nixeval.AddonSpec `json:"addons"` - } - if err := json.Unmarshal([]byte(request.Context), &metadata); err != nil { + // Report unknown addon flags before any agent turn. + if _, err := setupAgentExpectations(reconcile.Expectations{Version: 1}, r.opts, r.template.Addons); err != nil { return err } - if _, err := setupAgentExpectations(reconcile.Expectations{Version: 1}, opts, metadata.Addons); err != nil { - return err + return state.save() +} + +// openAgentLog sends raw agent and Nix output to --agent-log. A resumed run +// writes a new sibling log instead of overwriting the previous one. +func (r *agentSetupRun) openAgentLog() (func(), error) { + if r.opts.agentLog == "" { + return func() {}, nil } - if err := state.checkpoint(cmd.Context(), gitGuard); err != nil { - return err + file, err := os.OpenFile(r.opts.agentLog, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if r.resuming && os.IsExist(err) { + file, err = os.CreateTemp(filepath.Dir(r.opts.agentLog), filepath.Base(r.opts.agentLog)+".resume-*.jsonl") } - var debug io.Writer = io.Discard - if opts.agentLog != "" { - file, err := os.OpenFile(opts.agentLog, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) - if resuming && os.IsExist(err) { - file, err = os.CreateTemp(filepath.Dir(opts.agentLog), filepath.Base(opts.agentLog)+".resume-*.jsonl") - } - if err != nil { - return err - } - defer file.Close() - ui.Progress("Agent diagnostics: " + file.Name()) - debug = file + if err != nil { + return nil, err } - for review := 0; state.Stage == "inspection" || state.Stage == "review"; review++ { - if review == 8 { + r.ui.Progress("Agent diagnostics: " + file.Name()) + r.debug = file + return func() { file.Close() }, nil +} + +// agreePlan alternates inspection and review until the user accepts a plan. +// Asking for changes queues a question whose answer the next inspection gets. +func (r *agentSetupRun) agreePlan() error { + state := r.state + for review := 0; state.Stage == stageInspection || state.Stage == stageReview; review++ { + if review == maxPlanReviews { return errors.New("plan revision limit reached") } if state.Plan == nil { - if err := ensureCodingAgent(); err != nil { - return err - } - ui.Stage(tui.SetupInspect, "Getting to know your repository…") - reply, err := runAgentPhase(cmd.Context(), agent, request, setupagent.Inspection, nil, "", ui, debug, state) - if err != nil { - return err - } - if reply.Status != "plan" { - return fmt.Errorf("inspection did not return a plan: %s", reply.Summary) - } - plan := reply.Plan - for _, name := range plan.Services { - if services.Get(name) == nil { - return fmt.Errorf("unsupported service in plan: %s", name) - } - } - plan.Expectations, err = setupAgentExpectations(plan.Expectations, opts, metadata.Addons) - if err != nil { - return err - } - if request.Mode == "new" && (len(plan.Expectations.Files) == 0 || len(plan.Expectations.Commands) == 0) { - return errors.New("new repository plan requires source files and build/test acceptance commands") - } - state.Plan, state.Stage = plan, "review" - if err := state.checkpoint(cmd.Context(), gitGuard); err != nil { + if err := r.inspect(); err != nil { return err } } - decision, err := ui.ReviewPlan(renderSetupPlan(state.Plan, opts)) + decision, err := r.ui.ReviewPlan(renderSetupPlan(state.Plan, r.opts)) if err != nil { return err } - if decision == "Cancel" { + switch decision { + case "Cancel": return context.Canceled - } - if decision == "Apply" { - state.Stage = "apply" - } else { + case "Apply": + state.Stage = stageApply + default: state.Pending = []setupagent.Question{{ID: fmt.Sprintf("revision-%d", len(state.Conversation)), Prompt: "What should change in the plan?", Kind: "text", Required: true}} - state.Plan, state.Stage = nil, "inspection" + state.Plan, state.Stage = nil, stageInspection } if err := state.save(); err != nil { return err } } - plan := state.Plan + return nil +} + +// inspect asks the agent for a plan in a read-only turn. +func (r *agentSetupRun) inspect() error { + if err := r.ensureAgent(); err != nil { + return err + } + r.ui.Stage(tui.SetupInspect, "Getting to know your repository…") + reply, err := runAgentPhase(r.ctx, r.agent, r.request, setupagent.Inspection, nil, "", r.ui, r.debug, r.state) + if err != nil { + return err + } + plan := reply.Plan + var supported []string + for _, name := range plan.Services { + if services.Get(name) == nil { + r.ui.Warning(fmt.Sprintf("Leaving out service %q: Stackpanel does not support it.", name)) + continue + } + supported = append(supported, name) + } + plan.Services = supported + // Flag choices become checks of their own, whatever the agent planned. + if plan.Expectations, err = setupAgentExpectations(plan.Expectations, r.opts, r.template.Addons); err != nil { + return err + } + r.state.Plan, r.state.Stage = plan, stageReview + return r.state.save() +} + +// applyAndVerify makes the agreed changes, then checks them with doctor. When +// doctor finds problems, the agent gets one repair turn with its findings. +func (r *agentSetupRun) applyAndVerify() error { + state := r.state work, err := os.MkdirTemp("", "stackpanel-agent-setup-*") if err != nil { return err } defer os.RemoveAll(work) - for repairs := 0; ; { - if state.Stage == "apply" || state.Stage == "repair" { - if err := ensureCodingAgent(); err != nil { - return err - } - phase := setupagent.Setup - ui.Stage(tui.SetupApply, "Continuing your agreed files and configuration…") - if state.Stage == "repair" { - phase = setupagent.Repair - ui.Stage(tui.SetupApply, "Addressing verification findings · repair 1 of 1") - } - if _, _, _, err := reconcile.WriteScaffold(root, metadata.Files, false); err != nil { - return fmt.Errorf("prepare onboarding scaffold: %w", err) - } - if err := state.checkpoint(cmd.Context(), gitGuard); err != nil { - return err - } - if _, err := runAgentPhase(cmd.Context(), agent, request, phase, plan, state.Failure, ui, debug, state); err != nil { - return err - } - state.Stage = "verify" - if err := state.checkpoint(cmd.Context(), gitGuard); err != nil { + for repairs := 0; ; repairs++ { + if state.Stage == stageApply || state.Stage == stageRepair { + if err := r.apply(state.Plan); err != nil { return err } } - report, verifyErr := verifyAgentSetup(cmd.Context(), root, work, request.StackExecutable, plan, gitGuard, ui, debug, state) - failure := "" - if report != nil { - var rendered bytes.Buffer - report.Render(&rendered, reconcile.RenderOptions{Title: "stack doctor · onboarding verification", Verbose: true}) - ui.Activity(rendered.String()) - failure = rendered.String() - data, err := report.JSON() - if err != nil { - return err - } - state.Failure = string(data) - retainSetupChecks(&plan.Expectations, report) + report, verifyErr := r.verify(state.Plan, work) + if r.ctx.Err() != nil { + return r.ctx.Err() } - if verifyErr != nil { - if cmd.Context().Err() != nil { - return cmd.Context().Err() - } - state.Failure += "\n" + verifyErr.Error() - state.Stage = "repair" - if err := state.checkpoint(cmd.Context(), gitGuard); err != nil { - return err - } - if repairs == 1 { - var findings *setupDoctorFindingsError - if errors.As(verifyErr, &findings) { - keepNixInputs = true - ui.Warning("Repository setup has verification warnings. It remains unverified; runtime and Studio checks are pending.") - quote := func(s string) string { return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'" } - ui.ShowWarningResult(fmt.Sprintf("Your files and choices are saved. Run doctor again without an agent:\n\ncd %s\nnix develop --command %s doctor --onboarding\n\nRerun the same setup command to resume repair and Studio setup.\nManifest: %s", quote(root), quote(request.StackExecutable), state.path)) - if opts.tmp { - fmt.Fprintln(cmd.OutOrStdout(), root) - } - return nil - } - return fmt.Errorf("repository onboarding remains unverified after one repair attempt:\n%s\n%w", failure, verifyErr) - } - repairs++ - continue + problems := doctorProblems(report) + if verifyErr == nil && len(problems) == 0 { + return r.finish(report, nil) } - state.Failure, state.Stage = "", "runtime" - if err := state.checkpoint(cmd.Context(), gitGuard); err != nil { - return err - } - keepNixInputs = true - passed := 0 - for _, check := range report.CheckResults { - if check.Status == "pass" { - passed++ - } + failure := strings.Join(problems, "\n") + if verifyErr != nil { + failure = verifyErr.Error() } - ui.Verified(fmt.Sprintf("Repository · %d doctor checks passed", passed)) - summary := "Repository onboarding verified by stack doctor. Runtime and Studio unverified (--no-runtime)." - if !opts.noRuntime { - summary, err = runSetupRuntime(cmd.Context(), root, request.StackExecutable, plan.Services, opts, ui, debug, state) - if err != nil { - return fmt.Errorf("repository configured; runtime setup incomplete: %w", err) + r.ui.Activity(failure) + if repairs == maxRepairs { + if verifyErr != nil { + return fmt.Errorf("setup still fails after a repair turn: %w", verifyErr) } + return r.finish(report, problems) } - state.Stage = "complete" - if err := state.checkpoint(cmd.Context(), gitGuard); err != nil { + r.ui.Progress("The check found problems; the agent gets one turn to fix them.") + state.Failure, state.Stage = failure, stageRepair + if err := state.save(); err != nil { return err } - ui.ShowResult(summary) - if opts.tmp { - fmt.Fprintln(cmd.OutOrStdout(), root) - } - return nil } } -func verifyAgentSetup(ctx context.Context, root, work, executable string, plan *setupagent.Plan, guard *setupGitGuard, ui *tui.SetupUI, debug io.Writer, state *setupManifest) (*reconcile.Report, error) { - if err := guard.AddNixInputs(ctx, plan.Expectations.Files...); err != nil { - return nil, err +// apply runs the write phase, or the repair phase after a failed check, on top +// of any template files that are still missing. +func (r *agentSetupRun) apply(plan *setupagent.Plan) error { + if err := r.ensureAgent(); err != nil { + return err } - if err := state.checkpoint(ctx, guard); err != nil { - return nil, err + phase, message := setupagent.Setup, "Making the agreed changes…" + if r.state.Stage == stageRepair { + phase, message = setupagent.Repair, "Fixing the problems doctor found · repair 1 of 1" } - ui.Progress("Resolving repository flake inputs with Nix…") - if err := runSetupLock(ctx, root, work, guard, debug); err != nil { - return nil, fmt.Errorf("flake locking failed: %w", err) + r.ui.Stage(tui.SetupApply, message) + if _, _, _, err := reconcile.WriteScaffold(r.state.Root, r.template.Files, false); err != nil { + return fmt.Errorf("write template files: %w", err) } - if err := guard.AddNixInputs(ctx, plan.Expectations.Files...); err != nil { - return nil, err + if _, err := runAgentPhase(r.ctx, r.agent, r.request, phase, plan, r.state.Failure, r.ui, r.debug, r.state); err != nil { + return err } - if err := state.checkpoint(ctx, guard); err != nil { + r.state.Stage = stageVerify + return r.state.save() +} + +// verify checks the repository the way the user will use it: in a fresh +// devshell, after generation and the plan's prepare commands, with doctor. An +// error means a step before doctor failed or doctor produced no report. +func (r *agentSetupRun) verify(plan *setupagent.Plan, work string) (*reconcile.Report, error) { + root, executable := r.state.Root, r.request.StackExecutable + expose := func() error { return r.newFiles.expose(r.ctx, plan.Expectations.Files) } + if err := expose(); err != nil { return nil, err } - ui.Progress("Entering the repository devshell and reconciling generated files…") - if err := runFreshReconciliation(ctx, root, executable, debug); err != nil { - return nil, fmt.Errorf("fresh reconciliation failed: %w", err) + r.ui.Stage(tui.SetupVerify, "Entering the devshell and generating files…") + if err := runFreshReconciliation(r.ctx, root, executable, r.debug); err != nil { + return nil, fmt.Errorf("devshell or file generation failed: %w", err) } - if err := guard.AddNixInputs(ctx, plan.Expectations.Files...); err != nil { - return nil, err - } - if err := state.checkpoint(ctx, guard); err != nil { + // Locking and generation create files Nix reads, too. + if err := expose(); err != nil { return nil, err } for _, step := range plan.Prepare { - ui.Progress(fmt.Sprintf("Preparing %s · %s", step.ID, setupCommandLabel(step.Argv))) - if err := runSetupPrepare(ctx, root, step, debug); err != nil { - return nil, fmt.Errorf("host preparation %s failed: %w", step.ID, err) + r.ui.Progress(fmt.Sprintf("Preparing %s · %s", step.ID, setupCommandLabel(step.Argv))) + if err := runSetupPrepare(r.ctx, root, step, r.debug); err != nil { + return nil, fmt.Errorf("prepare command %s failed: %w", step.ID, err) } } - if len(plan.Prepare) > 0 { - // Lockfiles and manifests the commands created are Nix inputs too. - if err := guard.AddNixInputs(ctx, plan.Expectations.Files...); err != nil { - return nil, err - } - if err := state.checkpoint(ctx, guard); err != nil { - return nil, err - } + if err := expose(); err != nil { + return nil, err } - // Never trust a file the coding agent could have modified during its turn. - frozen, err := json.Marshal(plan.Expectations) + expectations, err := json.Marshal(plan.Expectations) if err != nil { return nil, err } - expectationsPath := filepath.Join(work, "expectations.json") - if err := os.WriteFile(expectationsPath, frozen, 0o600); err != nil { + path := filepath.Join(work, "expectations.json") + if err := os.WriteFile(path, expectations, 0o600); err != nil { return nil, err } - ui.Stage(tui.SetupVerify, "Checking files, configuration, and build/test commands…") - return runFreshDoctor(ctx, root, executable, expectationsPath, debug) + r.ui.Progress("Checking the setup with stack doctor…") + return runFreshDoctor(r.ctx, root, executable, r.debug, "--strict", "--scope", "repo,build", "--build", "--expectations", path, "--json") } -func retainSetupChecks(expected *reconcile.Expectations, report *reconcile.Report) { - seen := make(map[string]bool, len(expected.RequiredChecks)) - for _, id := range expected.RequiredChecks { - seen[id] = true +// finish starts the local runtime and Studio unless --no-runtime was given. +// Problems doctor still reports after the repair turn are shown, not fatal: the +// changes are in place, and rerunning setup checks them again. +func (r *agentSetupRun) finish(report *reconcile.Report, problems []string) error { + state := r.state + state.Failure, state.Stage = "", stageRuntime + if err := state.save(); err != nil { + return err + } + headline := "Repository set up and verified by stack doctor." + if len(problems) == 0 { + r.ui.Verified(fmt.Sprintf("Repository · %d doctor checks passed", countPassedChecks(report))) + } else { + headline = fmt.Sprintf("Repository set up, but stack doctor still reports:\n %s\nFix these, then rerun this command to check again.", strings.Join(problems, "\n ")) + r.ui.Warning(fmt.Sprintf("stack doctor still reports %d problem(s); continuing.", len(problems))) + } + runtime := "Runtime and Studio skipped (--no-runtime)." + if !r.opts.noRuntime { + var err error + if runtime, err = runSetupRuntime(r.ctx, state.Root, r.request.StackExecutable, state.Plan.Services, r.opts, r.ui, r.debug, state); err != nil { + return fmt.Errorf("repository set up; runtime setup incomplete: %w", err) + } + } + state.Stage = stageComplete + if err := state.save(); err != nil { + return err + } + if len(problems) == 0 { + r.ui.ShowResult(headline + "\n" + runtime) + } else { + r.ui.ShowWarningResult(headline + "\n" + runtime) + } + if r.opts.tmp { + fmt.Fprintln(r.cmd.OutOrStdout(), state.Root) } + return nil +} + +func countPassedChecks(report *reconcile.Report) int { + passed := 0 for _, check := range report.CheckResults { - if check.ID != "" && check.Module != "onboarding" && !seen[check.ID] { - expected.RequiredChecks = append(expected.RequiredChecks, check.ID) - seen[check.ID] = true + if check.Status == "pass" { + passed++ + } + } + return passed +} + +// doctorProblems lists doctor's errors. Checks doctor skipped are left out: +// they cannot run here, and no change to the repository would make them pass. +func doctorProblems(report *reconcile.Report) []string { + if report == nil { + return nil + } + skipped := map[string]bool{} + for _, result := range report.CheckResults { + if result.Status == "skipped" { + skipped[result.ID] = true + } + } + var problems []string + for _, e := range report.Errors { + problems = append(problems, e.Reconciler+": "+e.Err) + } + for _, f := range report.Findings { + check := strings.TrimPrefix(strings.TrimPrefix(f.ID, "strict:check:"), "check:") + if f.Severity != reconcile.SeverityError || (check != f.ID && skipped[check]) { + continue + } + problem := f.Title + if f.Detail != "" { + problem += ": " + f.Detail } + problems = append(problems, problem) } + return problems } func selectSetupAgent(ctx context.Context, requested string, interactive bool, ui *tui.SetupUI) (setupagent.Agent, error) { var available []setupagent.Agent - var diagnostics []string + var unusable []string for _, agent := range setupagent.Discover() { if requested != "auto" && requested != agent.ID { continue } - caps, err := setupagent.Probe(ctx, agent) - if err != nil { - message := fmt.Sprintf("%s unavailable: %v", agent.ID, err) - diagnostics = append(diagnostics, message) - ui.Activity(message) - continue - } - if !caps.ReadOnly || !caps.Write { - message := fmt.Sprintf("%s unsupported: %s", agent.ID, caps.Reason) - diagnostics = append(diagnostics, message) - ui.Activity(message) + if err := setupagent.Probe(ctx, agent); err != nil { + unusable = append(unusable, err.Error()) + ui.Activity(err.Error()) continue } available = append(available, agent) } if len(available) == 0 { - message := fmt.Sprintf("no supported agent found for %q on PATH; install/authenticate codex or claude, or use ordinary stack setup", requested) - if len(diagnostics) > 0 { - message += "\n" + strings.Join(diagnostics, "\n") + message := fmt.Sprintf("no usable coding agent found for %q on PATH; install and sign in to codex or claude, or use --experimental-agent=pi", requested) + if len(unusable) > 0 { + message += "\n" + strings.Join(unusable, "\n") } return setupagent.Agent{}, errors.New(message) } - if len(available) == 1 { + if len(available) == 1 || !interactive { return available[0], nil } - if !interactive { - return setupagent.Agent{}, errors.New("multiple agents available: select one with --experimental-agent=codex or --experimental-agent=claude") - } labels := make([]string, len(available)) for i, agent := range available { labels[i] = agent.ID @@ -474,6 +517,14 @@ func selectSetupAgent(ctx context.Context, requested string, interactive bool, u return setupagent.Agent{}, errors.New("no agent selected") } +func currentStackExecutable() (string, error) { + executable, err := os.Executable() + if err != nil { + return "", err + } + return filepath.EvalSymlinks(executable) +} + func prepareSetupRequest(ctx context.Context, root string, opts setupFlags) (setupagent.SetupRequest, error) { request := setupagent.SetupRequest{Root: root, Template: opts.template, Mode: "existing"} if opts.newDir != "" || opts.tmp { @@ -482,12 +533,8 @@ func prepareSetupRequest(ctx context.Context, root string, opts setupFlags) (set if request.Template == "" { request.Template = "default" } - executable, err := os.Executable() - if err != nil { - return request, err - } - request.StackExecutable, err = filepath.EvalSymlinks(executable) - if err != nil { + var err error + if request.StackExecutable, err = currentStackExecutable(); err != nil { return request, err } flake := reconcile.NormalizeFlakeRef(resolveAgentSetupFlake(opts.flake)) @@ -600,46 +647,10 @@ func runSetupPrepare(ctx context.Context, root string, step setupagent.PrepareCo return runSetupShell(ctx, root, out, append(args, step.Argv...)...) } -// runSetupLock owns the daemon-dependent operation. A separate output file -// prevents Nix from staging flake.lock and lets us preserve existing user edits. -func runSetupLock(ctx context.Context, root, work string, guard *setupGitGuard, out io.Writer) error { - output := filepath.Join(work, "flake.lock") - if err := os.Remove(output); err != nil && !errors.Is(err, os.ErrNotExist) { - return err - } - if err := runSetupProcess(ctx, root, out, "nix", "flake", "lock", ".", "--output-lock-file", output); err != nil { - return err - } - data, err := os.ReadFile(output) - if err != nil { - return fmt.Errorf("read resolved flake lock: %w", err) - } - path := filepath.Join(root, "flake.lock") - if current, err := os.ReadFile(path); err == nil && bytes.Equal(current, data) { - return nil - } - if rel, err := filepath.Rel(guard.root, path); err == nil { - if _, protected := guard.protected[rel]; protected { - return errors.New("flake inputs require changing preexisting user edits in flake.lock; lock file was left unchanged") - } - } - if info, err := os.Lstat(path); err == nil && !info.Mode().IsRegular() { - return errors.New("refusing to replace a non-regular flake.lock") - } - return os.WriteFile(path, data, 0o644) -} - -func runFreshDoctor(ctx context.Context, root, stackExecutable, expectationsPath string, out io.Writer) (*reconcile.Report, error) { - return runFreshDoctorArgs(ctx, root, stackExecutable, out, []string{"--strict", "--scope", "repo,build", "--build", "--expectations", expectationsPath, "--json"}, []string{"codegen", "files", "fileops", "checks", "verification"}) -} - -// A completed diagnosis with findings is distinct from a broken verifier or -// failed Nix invocation. Setup may warn about findings; doctor still fails. -type setupDoctorFindingsError struct{} - -func (*setupDoctorFindingsError) Error() string { return "doctor found errors or pending changes" } - -func runFreshDoctorArgs(ctx context.Context, root, stackExecutable string, out io.Writer, doctorArgs, requiredIDs []string) (*reconcile.Report, error) { +// runFreshDoctor runs this binary's doctor in a fresh devshell and returns its +// report. An error means doctor produced no report; what doctor found is in +// the report. +func runFreshDoctor(ctx context.Context, root, stackExecutable string, out io.Writer, doctorArgs ...string) (*reconcile.Report, error) { reportFile, err := os.CreateTemp("", "stackpanel-doctor-*.json") if err != nil { return nil, err @@ -653,45 +664,26 @@ func runFreshDoctorArgs(ctx context.Context, root, stackExecutable string, out i args := []string{"bash", "--noprofile", "--norc", "-c", `report=$1; shift; exec "$@" > "$report"`, "stackpanel-doctor", path, stackExecutable, "doctor"} - err = runSetupShell(ctx, root, out, append(args, doctorArgs...)...) - data, readErr := os.ReadFile(path) - if readErr != nil { - return nil, readErr + // Doctor exits nonzero when it finds problems, so the report decides. + runErr := runSetupShell(ctx, root, out, append(args, doctorArgs...)...) + data, err := os.ReadFile(path) + if err != nil { + return nil, err } var report reconcile.Report - if parseErr := json.Unmarshal(data, &report); parseErr != nil { - return nil, fmt.Errorf("doctor did not produce a valid report: %v (shell: %v)", parseErr, err) - } - // An older binary or an empty object must not be accepted as verification. - for _, required := range requiredIDs { - found := false - for _, id := range report.Reconcilers { - found = found || id == required + if err := json.Unmarshal(data, &report); err != nil { + if runErr != nil { + return nil, runErr } - if !found { - return &report, fmt.Errorf("doctor report omitted required reconciler %q", required) - } - } - var exitErr *exec.ExitError - if err != nil && (!errors.As(err, &exitErr) || exitErr.ExitCode() != 1) { - return &report, err - } - report.EnforceStrict() - if report.HasErrors() || report.HasChanges() { - return &report, &setupDoctorFindingsError{} - } - if err != nil { - return &report, err + return nil, fmt.Errorf("doctor did not produce a report: %w", err) } return &report, nil } +// runSetupShell runs a command in the repository's devshell. Nix creates or +// extends flake.lock as needed, as it does when the user enters the shell. func runSetupShell(ctx context.Context, root string, out io.Writer, args ...string) error { - if _, err := os.Stat(filepath.Join(root, "flake.lock")); err != nil { - return fmt.Errorf("onboarding requires flake.lock before verification: %w", err) - } - nixArgs := append([]string{"develop", ".", "--no-update-lock-file", "--no-write-lock-file", "--command"}, args...) - return runSetupProcess(ctx, root, out, "nix", nixArgs...) + return runSetupProcess(ctx, root, out, "nix", append([]string{"develop", ".", "--command"}, args...)...) } func runSetupProcess(ctx context.Context, root string, out io.Writer, executable string, args ...string) error { diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_conversation.go b/apps/stackpanel-go/cmd/cli/setup_agent_conversation.go index 4b613f87..b77fb0c0 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent_conversation.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent_conversation.go @@ -12,6 +12,13 @@ import ( "github.com/darkmatter/stackpanel/stackpanel-go/internal/tui" ) +const maxQuestionRounds = 8 + +// runAgentPhase runs agent turns for phase, putting each round of questions +// to the user, until the agent gives its final reply. An inspection reply is +// always a plan, and the user reviews it; a reply that is not JSON becomes the +// plan's summary. A write phase's reply matters less than its edits, which +// doctor checks next. func runAgentPhase(ctx context.Context, agent setupagent.Agent, request *setupagent.SetupRequest, phase setupagent.Phase, plan *setupagent.Plan, failure string, ui *tui.SetupUI, debug io.Writer, state *setupManifest) (*setupagent.Reply, error) { if state == nil { state = &setupManifest{} @@ -19,23 +26,16 @@ func runAgentPhase(ctx context.Context, agent setupagent.Agent, request *setupag if agent.ID == "pi" && state.Pi == nil { state.Pi = &setupagent.PiState{} } - for round := 0; round < 8; round++ { + for round := 0; round < maxQuestionRounds; round++ { if err := answerSetupQuestions(state, request, ui); err != nil { return nil, err } request.Conversation = state.Conversation - reply, err := runSetupAgentReply(ctx, agent, phase, setupagent.RunRequest{ + result, err := setupagent.Run(ctx, agent, setupagent.RunRequest{ Dir: request.Root, Env: freshSetupEnvironment(os.Environ()), ReadOnly: phase == setupagent.Inspection, Prompt: setupagent.BuildPrompt(*request, phase, plan, failure), Timeout: setupStageTimeout, Stdout: debug, Stderr: debug, - ProtectedPaths: request.ProtectedPaths, - PiState: state.Pi, - SavePiState: func() error { - if state.path != "" { - return state.save() - } - return nil - }, + PiState: state.Pi, SavePiState: state.persist, OnEvent: func(event setupagent.Event) { if event.Kind == "warning" { ui.Warning(event.Text) @@ -43,27 +43,35 @@ func runAgentPhase(ctx context.Context, agent setupagent.Agent, request *setupag ui.Activity(event.Text) } }, - }, state, ui) + }) if err != nil { - return nil, err + return nil, fmt.Errorf("%s %s turn failed: %w (check that %s is signed in; --agent-log records its output)", agent.ID, phase, err, agent.ID) } - if reply.Status == "blocked" { - return nil, fmt.Errorf("%s blocked: %s", agent.ID, reply.Summary) - } - if reply.Status != "needs_input" { - if phase != setupagent.Inspection && reply.Status != "complete" { - return nil, fmt.Errorf("%s must return complete", phase) - } - return reply, nil + message := strings.TrimSpace(result.Message) + reply, err := setupagent.ParseReply(message) + if err != nil { + reply = &setupagent.Reply{Status: setupagent.StatusComplete, Summary: message} } - state.Pending = reply.Questions - if state.path != "" { - if err := state.save(); err != nil { + if reply.Status == setupagent.StatusNeedsInput { + state.Pending = reply.Questions + if err := state.persist(); err != nil { return nil, err } + continue + } + if reply.Status == setupagent.StatusBlocked { + ui.Warning(fmt.Sprintf("%s did not finish: %s", agent.ID, reply.Summary)) } + if phase == setupagent.Inspection && reply.Plan == nil { + reply = &setupagent.Reply{Status: setupagent.StatusPlan, Plan: &setupagent.Plan{Summary: reply.Summary}} + setupagent.NormalizePlan(reply.Plan) + } + if reply.Plan != nil && reply.Plan.Summary == "" { + reply.Plan.Summary = "Set up Stackpanel for this repository." + } + return reply, nil } - return nil, fmt.Errorf("%s exceeded eight question rounds", phase) + return nil, fmt.Errorf("%s asked more than %d rounds of questions", agent.ID, maxQuestionRounds) } func answerSetupQuestions(state *setupManifest, request *setupagent.SetupRequest, ui *tui.SetupUI) error { @@ -77,22 +85,19 @@ func answerSetupQuestions(state *setupManifest, request *setupagent.SetupRequest } } if !answered { - values, err := ui.Ask(question.Prompt, question.Kind, question.Options, question.Default, question.Required) + // Without a person to ask, a question with no default gets an empty + // answer, which leaves the choice to the agent. + values, err := ui.Ask(question.Prompt, question.Kind, question.Options, question.Default, question.Required && ui.Interactive()) if err != nil { return err } - if err := setupagent.ValidateAnswer(question, values, true); err != nil { - return err - } answer := setupagent.Answer{ID: question.ID, Values: values} request.Answers = append(request.Answers, answer) state.Conversation = append(state.Conversation, setupagent.Exchange{Question: question, Answer: answer}) } state.Pending = state.Pending[1:] - if state.path != "" { - if err := state.save(); err != nil { - return err - } + if err := state.persist(); err != nil { + return err } } return nil @@ -141,9 +146,6 @@ func renderSetupPlan(plan *setupagent.Plan, opts setupFlags) string { } } s.WriteString("\nVerification\n Doctor checks the agreed repository setup.\n") - for _, id := range plan.Expectations.RequiredChecks { - fmt.Fprintf(&s, " Required check: %s\n", id) - } switch { case opts.noRuntime: s.WriteString(" Runtime and Studio checks are skipped (--no-runtime).\n") diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_conversation_test.go b/apps/stackpanel-go/cmd/cli/setup_agent_conversation_test.go index 5d2081b4..944b8252 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent_conversation_test.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent_conversation_test.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "context" + "encoding/json" "io" "os" "path/filepath" @@ -44,6 +45,48 @@ printf '{"type":"turn.completed"}\n' } } +func TestAgentPhaseAcceptsWhateverTheAgentReplies(t *testing.T) { + root := t.TempDir() + binary := filepath.Join(root, "codex") + script := `#!/bin/sh +cat > /dev/null +printf '{"type":"item.completed","item":{"type":"agent_message","text":%s}}\n{"type":"turn.completed"}\n' "$REPLY_JSON" +` + if err := os.WriteFile(binary, []byte(script), 0o755); err != nil { + t.Fatal(err) + } + agent := setupagent.Agent{ID: "codex", Path: binary} + for _, tt := range []struct { + name, reply string + phase setupagent.Phase + status string + warning bool + }{ + {"prose plan", "I will add a Bun app in apps/web.", setupagent.Inspection, setupagent.StatusPlan, false}, + {"blocked inspection", `{"status":"blocked","summary":"Could not read flake.nix"}`, setupagent.Inspection, setupagent.StatusPlan, true}, + {"prose completion", "All done!", setupagent.Setup, setupagent.StatusComplete, false}, + {"blocked setup", `{"status":"blocked","summary":"Needs a database URL"}`, setupagent.Repair, setupagent.StatusBlocked, true}, + } { + t.Run(tt.name, func(t *testing.T) { + encoded, _ := json.Marshal(tt.reply) + t.Setenv("REPLY_JSON", string(encoded)) + var output bytes.Buffer + ui := tui.NewSetupUI(context.Background(), false, &output) + defer ui.Close() + reply, err := runAgentPhase(context.Background(), agent, &setupagent.SetupRequest{Root: root}, tt.phase, nil, "", ui, io.Discard, nil) + if err != nil || reply.Status != tt.status { + t.Fatalf("reply=%+v error=%v", reply, err) + } + if tt.phase == setupagent.Inspection && (reply.Plan == nil || reply.Plan.Summary == "") { + t.Fatalf("inspection must end with a plan to review: %+v", reply) + } + if strings.Contains(output.String(), "did not finish") != tt.warning { + t.Fatalf("warning shown = %v: %s", !tt.warning, output.String()) + } + }) + } +} + func TestNewSetupTargetProtectsExistingDirectory(t *testing.T) { root := t.TempDir() file := filepath.Join(root, "keep") diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_git.go b/apps/stackpanel-go/cmd/cli/setup_agent_git.go index 5315ca3a..24313f7e 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent_git.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent_git.go @@ -3,226 +3,97 @@ package cmd import ( "bytes" "context" - "crypto/sha256" - "errors" "fmt" - "io" "os" "os/exec" "path/filepath" - "reflect" - "sort" - "strconv" "strings" - "time" ) -// setupGitGuard protects preexisting edits without reverting anything. Clean -// tracked files remain editable. Only this guard may add temporary intent entries -// for newly created Nix inputs. Successful setup retains these entries so the -// next shell can evaluate the new files; Close cleans them up after a failed run. -type setupGitGuard struct { - root, target string - head string - index map[string][]setupIndexEntry - protected map[string]setupFileState - owned map[string][]setupIndexEntry +// Flakes in a Git repository only see files Git knows about, so setup adds the +// files it creates to the index with --intent-to-add before Nix evaluates +// them. That records the paths without staging their contents; review and +// commit stay with the user. +type setupNewFiles struct { + root string // setup directory; empty outside a Git repository + before map[string]bool // untracked files that predate the setup } -type setupIndexEntry struct { - Record string // mode, blob ID and merge stage - Flags uint64 // semantic flags, excluding cached filesystem metadata -} - -const setupIntentToAdd = 0x20000000 - -type setupFileState struct { - Exists bool - Mode os.FileMode - Hash [sha256.Size]byte - Link string -} - -func captureSetupGit(ctx context.Context, target string) (*setupGitGuard, error) { - target, err := filepath.EvalSymlinks(target) - if err != nil { - return nil, err - } - guard := &setupGitGuard{target: target, owned: map[string][]setupIndexEntry{}} - root, err := setupGitCommand(ctx, target, "rev-parse", "--show-toplevel") - if err != nil { +// newSetupFiles records, once per manifest, which untracked files predate the +// setup, so files any run of it creates are recognized as new. +func newSetupFiles(ctx context.Context, state *setupManifest) (*setupNewFiles, error) { + if _, err := setupGitCommand(ctx, state.Root, "rev-parse", "--git-dir"); err != nil { if strings.Contains(err.Error(), "not a git repository") { - return guard, nil + return &setupNewFiles{}, nil } return nil, err } - guard.root = strings.TrimSuffix(string(root), "\n") - guard.head, err = setupGitHead(ctx, guard.root) - if err != nil { - return nil, err - } - guard.index, err = setupReadIndex(ctx, guard.root) - if err != nil { - return nil, err - } - guard.protected = map[string]setupFileState{} - for _, args := range [][]string{ - {"diff", "--name-only", "--no-renames", "-z", "--"}, - {"diff", "--cached", "--name-only", "--no-renames", "-z", "--"}, - {"ls-files", "--others", "--exclude-standard", "-z"}, - } { - data, err := setupGitCommand(ctx, guard.root, args...) + if state.UntrackedAtStart == nil { + paths, err := setupUntrackedFiles(ctx, state.Root) if err != nil { return nil, err } - for _, path := range setupGitPaths(data) { - state, err := setupReadFileState(filepath.Join(guard.root, path)) - if err != nil { - return nil, fmt.Errorf("preserve existing Git edit %q: %w", path, err) - } - guard.protected[path] = state - } - } - return guard, nil -} - -func (g *setupGitGuard) Check(ctx context.Context) error { - if g.root == "" { - return nil - } - head, err := setupGitHead(ctx, g.root) - if err != nil { - return err - } - if head != g.head { - return errors.New("onboarding changed Git HEAD; existing history was not preserved (nothing was reverted)") - } - current, err := setupReadIndex(ctx, g.root) - if err != nil { - return err - } - paths := map[string]bool{} - for path := range g.index { - paths[path] = true - } - for path := range current { - paths[path] = true - } - for path := range g.owned { - paths[path] = true - } - for _, path := range sortedKeys(paths) { - want := g.index[path] - if own, ok := g.owned[path]; ok { - want = own - } - if !reflect.DeepEqual(current[path], want) { - return fmt.Errorf("onboarding changed Git staging for %q; existing index was not preserved (nothing was reverted)", path) + state.UntrackedAtStart = paths + if err := state.save(); err != nil { + return nil, err } } - for _, path := range sortedKeys(g.protected) { - state, err := setupReadFileState(filepath.Join(g.root, path)) - if err != nil || state != g.protected[path] { - return fmt.Errorf("onboarding changed preexisting user edits in %q; restore those edits before retrying (nothing was reverted)", path) - } + files := &setupNewFiles{root: state.Root, before: map[string]bool{}} + for _, path := range state.UntrackedAtStart { + files.before[path] = true } - return nil + return files, nil } -// AddNixInputs exposes only new onboarding inputs to pure Git-backed evaluation. -// Existing untracked files are deliberately excluded: adding them would change -// the user's original index. Generated files, state, keys and profiles stay out. -func (g *setupGitGuard) AddNixInputs(ctx context.Context, requiredFiles ...string) error { - if err := g.Check(ctx); err != nil || g.root == "" { - return err - } - data, err := setupGitCommand(ctx, g.root, "ls-files", "--others", "--exclude-standard", "-z") - if err != nil { - return err - } - var paths []string - accepted := make(map[string]bool, len(requiredFiles)) - for _, path := range requiredFiles { - if filepath.IsLocal(path) { - accepted[filepath.Clean(path)] = true - } - } - for _, path := range setupGitPaths(data) { - if _, existed := g.protected[path]; existed { - continue - } - rel, err := filepath.Rel(g.target, filepath.Join(g.root, path)) - if err == nil && !setupGeneratedPath(rel) && (setupNixInput(rel) || accepted[rel]) { - paths = append(paths, path) - } - } - if len(paths) == 0 { +// expose adds untracked files to the index with --intent-to-add when the setup +// created them, the plan lists them, or Nix reads them (*.nix, flake.lock and +// .stack/). Generated output and private state are never added. +func (f *setupNewFiles) expose(ctx context.Context, planned []string) error { + if f.root == "" { return nil } - sort.Strings(paths) - _, addErr := setupGitCommand(ctx, g.root, append([]string{"add", "--intent-to-add", "--"}, paths...)...) - // Record even partially added entries so deferred cleanup can remove them. - // Cancellation can arrive after Git commits its index, so this bookkeeping - // gets its own short deadline instead of inheriting the cancelled context. - readCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) - defer cancel() - current, err := setupReadIndex(readCtx, g.root) + paths, err := setupUntrackedFiles(ctx, f.root) if err != nil { return err } + listed := map[string]bool{} + for _, path := range planned { + listed[filepath.ToSlash(filepath.Clean(path))] = true + } + var add []string for _, path := range paths { - entries := current[path] - if len(entries) == 1 && entries[0].Flags&setupIntentToAdd != 0 { - g.owned[path] = entries + if !setupGeneratedPath(path) && (!f.before[path] || listed[path] || setupNixInput(path)) { + add = append(add, path) } } - if addErr != nil { - return addErr + if len(add) == 0 { + return nil } - return g.Check(ctx) + _, err = setupGitCommand(ctx, f.root, append([]string{"add", "--intent-to-add", "--"}, add...)...) + return err } -func (g *setupGitGuard) Close(ctx context.Context) error { - if g.root == "" || len(g.owned) == 0 { - return nil - } - current, err := setupReadIndex(ctx, g.root) +// setupUntrackedFiles lists untracked, unignored files under root, relative to +// it. The result is never nil, so an empty list can be told from none recorded. +func setupUntrackedFiles(ctx context.Context, root string) ([]string, error) { + data, err := setupGitCommand(ctx, root, "ls-files", "--others", "--exclude-standard", "-z") if err != nil { - return err - } - var remove []string - for _, path := range sortedKeys(g.owned) { - // If someone staged actual content meanwhile, preserve it. Check reports - // the violation separately; cleanup must never undo new user staging. - if reflect.DeepEqual(current[path], g.owned[path]) { - remove = append(remove, path) - } - } - if len(remove) == 0 { - return nil + return nil, err } - _, err = setupGitCommand(ctx, g.root, append([]string{"update-index", "--force-remove", "--"}, remove...)...) - if err == nil { - for _, path := range remove { - delete(g.owned, path) + paths := []string{} + for _, path := range bytes.Split(data, []byte{0}) { + if len(path) > 0 { + paths = append(paths, string(path)) } } - return err + return paths, nil } func setupNixInput(path string) bool { - path = filepath.ToSlash(path) - if path == ".." || strings.HasPrefix(path, "../") { - return false - } - if setupGeneratedPath(path) { - return false - } return strings.HasSuffix(path, ".nix") || filepath.Base(path) == "flake.lock" || strings.HasPrefix(path, ".stack/") } func setupGeneratedPath(path string) bool { - path = filepath.ToSlash(path) for _, generated := range []string{".stack/gen/", ".stack/profile/", ".stack/state/", ".stack/keys/", "packages/gen/env/"} { if strings.HasPrefix(path, generated) { return true @@ -231,102 +102,6 @@ func setupGeneratedPath(path string) bool { return false } -func setupReadFileState(path string) (setupFileState, error) { - info, err := os.Lstat(path) - if errors.Is(err, os.ErrNotExist) { - return setupFileState{}, nil - } - if err != nil { - return setupFileState{}, err - } - state := setupFileState{Exists: true, Mode: info.Mode().Type() | info.Mode().Perm()} - if info.Mode()&os.ModeSymlink != 0 { - state.Link, err = os.Readlink(path) - return state, err - } - if !info.Mode().IsRegular() { - return state, errors.New("cannot safely snapshot a dirty directory or special file") - } - file, err := os.Open(path) - if err != nil { - return state, err - } - defer file.Close() - hash := sha256.New() - if _, err := io.Copy(hash, file); err != nil { - return state, err - } - copy(state.Hash[:], hash.Sum(nil)) - return state, nil -} - -func setupReadIndex(ctx context.Context, root string) (map[string][]setupIndexEntry, error) { - data, err := setupGitCommand(ctx, root, "ls-files", "--stage", "--debug", "-z") - if err != nil { - return nil, err - } - entries := map[string][]setupIndexEntry{} - for len(data) > 0 { - end := bytes.IndexByte(data, 0) - if end < 0 { - return nil, errors.New("invalid Git index record") - } - record, path, ok := strings.Cut(string(data[:end]), "\t") - if !ok { - return nil, errors.New("invalid Git index path") - } - data = data[end+1:] - marker := []byte("\tflags: ") - start := bytes.Index(data, marker) - if start < 0 { - return nil, errors.New("Git did not report index flags") - } - data = data[start+len(marker):] - end = bytes.IndexByte(data, '\n') - if end < 0 { - return nil, errors.New("invalid Git index flags") - } - flags, err := strconv.ParseUint(string(data[:end]), 16, 64) - if err != nil { - return nil, fmt.Errorf("parse Git index flags: %w", err) - } - // Preserve assume-unchanged, skip-worktree and intent-to-add. Cached - // stat fields and refresh flags may change during harmless Git reads. - flags &= 0x8000 | 0x40000000 | setupIntentToAdd - entries[path] = append(entries[path], setupIndexEntry{Record: record, Flags: flags}) - data = data[end+1:] - } - return entries, nil -} - -func setupGitHead(ctx context.Context, root string) (string, error) { - head, err := setupGitCommand(ctx, root, "rev-parse", "--verify", "--quiet", "HEAD") - if err != nil { - var exit *exec.ExitError - if !errors.As(err, &exit) || exit.ExitCode() != 1 { - return "", err - } - } - branch, err := setupGitCommand(ctx, root, "symbolic-ref", "--quiet", "HEAD") - if err != nil { - var exit *exec.ExitError - if !errors.As(err, &exit) || exit.ExitCode() != 1 { - return "", err - } - } - return string(head) + string(branch), nil -} - -func setupGitPaths(data []byte) []string { - var paths []string - for _, path := range bytes.Split(data, []byte{0}) { - if len(path) > 0 { - paths = append(paths, string(path)) - } - } - return paths -} - func setupGitCommand(ctx context.Context, root string, args ...string) ([]byte, error) { cmd := exec.CommandContext(ctx, "git", append([]string{"--literal-pathspecs", "-C", root}, args...)...) for _, value := range os.Environ() { diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_git_test.go b/apps/stackpanel-go/cmd/cli/setup_agent_git_test.go index e51e2ac8..d14a19d4 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent_git_test.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent_git_test.go @@ -4,232 +4,67 @@ import ( "context" "os" "path/filepath" - "reflect" + "slices" "strings" "testing" ) -func TestSetupGitPreservesExistingEditsAndCleansVisibility(t *testing.T) { +func TestSetupExposesNewFilesAndNixInputsToGit(t *testing.T) { + setupStateTestEnvironment(t) root := setupGitFixture(t) ctx := context.Background() - guard, err := captureSetupGit(ctx, root) + // Earlier non-agent runs can leave Stackpanel's own config untracked. + writeSetupGitFile(t, root, ".stack/config.nix", "{ }\n") + state := &setupManifest{Version: 1, Root: root, Stage: stageInspection, path: setupStatePath(root)} + files, err := newSetupFiles(ctx, state) if err != nil { t.Fatal(err) } - originalIndex := guard.index - writeSetupGitFile(t, root, "clean.txt", "onboarding may edit clean tracked files\n") - for _, path := range []string{"flake.lock", ".stack/new.nix", ".stack/title.txt", ":[literal].nix", "unrelated-new.txt", ".stack/gen/generated.nix", ".stack/keys/new.nix"} { - writeSetupGitFile(t, root, path, "new onboarding content\n") + if !slices.Equal(state.UntrackedAtStart, []string{".stack/config.nix", "notes [do not touch].txt"}) { + t.Fatalf("untracked files before setup = %q", state.UntrackedAtStart) } - if err := guard.AddNixInputs(ctx); err != nil { - t.Fatal(err) - } - for _, path := range []string{"flake.lock", ".stack/new.nix", ".stack/title.txt", ":[literal].nix"} { - if len(guard.owned[path]) != 1 || guard.owned[path][0].Flags&setupIntentToAdd == 0 { - t.Fatalf("%q was not exposed with intent-to-add: %+v", path, guard.owned) - } - } - if len(guard.owned) != 4 { - t.Fatalf("unexpected files added to index: %+v", guard.owned) - } - writeSetupGitFile(t, root, ".stack/new.nix", "agent may revise its own new inputs\n") - writeSetupGitFile(t, root, ".stack/second.nix", "second attempt\n") - if err := guard.AddNixInputs(ctx); err != nil { - t.Fatal(err) - } - if err := guard.Check(ctx); err != nil { - t.Fatal(err) - } - if err := guard.Close(ctx); err != nil { - t.Fatal(err) - } - finalIndex, err := setupReadIndex(ctx, root) - if err != nil || !reflect.DeepEqual(originalIndex, finalIndex) { - t.Fatalf("original index changed: %v\n%+v\n%+v", err, originalIndex, finalIndex) - } - if err := guard.Check(ctx); err != nil { - t.Fatal(err) - } - if content, err := os.ReadFile(filepath.Join(root, ".stack/new.nix")); err != nil || string(content) != "agent may revise its own new inputs\n" { - t.Fatalf("cleanup must retain worktree files: %q, %v", content, err) - } -} - -func TestSetupGitRejectsChangesToUserState(t *testing.T) { - for _, test := range []struct { - name string - edit func(*testing.T, string) - want string - }{ - {"working edits", func(t *testing.T, root string) { writeSetupGitFile(t, root, "unstaged.txt", "overwritten") }, "preexisting user edits"}, - {"staged working file", func(t *testing.T, root string) { writeSetupGitFile(t, root, "staged.txt", "overwritten") }, "preexisting user edits"}, - {"untracked file", func(t *testing.T, root string) { writeSetupGitFile(t, root, "notes [do not touch].txt", "overwritten") }, "preexisting user edits"}, - {"deletion", func(t *testing.T, root string) { - if err := os.Remove(filepath.Join(root, "unstaged.txt")); err != nil { - t.Fatal(err) - } - }, "preexisting user edits"}, - {"mode change", func(t *testing.T, root string) { - if err := os.Chmod(filepath.Join(root, "unstaged.txt"), 0o755); err != nil { - t.Fatal(err) - } - }, "preexisting user edits"}, - {"stage user edits", func(t *testing.T, root string) { setupGitTestRun(t, root, "add", "unstaged.txt") }, "Git staging"}, - {"unstage user edits", func(t *testing.T, root string) { setupGitTestRun(t, root, "reset", "HEAD", "--", "staged.txt") }, "Git staging"}, - {"agent adds a new file", func(t *testing.T, root string) { - writeSetupGitFile(t, root, ".stack/model.nix", "{}") - setupGitTestRun(t, root, "add", "--intent-to-add", ".stack/model.nix") - }, "Git staging"}, - {"commit", func(t *testing.T, root string) { setupGitTestRun(t, root, "commit", "-qm", "unexpected commit") }, "Git HEAD"}, - } { - t.Run(test.name, func(t *testing.T) { - root := setupGitFixture(t) - guard, err := captureSetupGit(context.Background(), root) - if err != nil { - t.Fatal(err) - } - test.edit(t, root) - if err := guard.Check(context.Background()); err == nil || !strings.Contains(err.Error(), test.want) { - t.Fatalf("expected %s rejection, got %v", test.want, err) - } - if err := guard.AddNixInputs(context.Background()); err == nil { - t.Fatal("visibility staging should refuse an already altered baseline") - } - }) - } -} - -func TestSetupGitCleanupDoesNotUnstageNewUserContent(t *testing.T) { - root := setupGitFixture(t) - ctx := context.Background() - guard, err := captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - writeSetupGitFile(t, root, "new.nix", "{}") - if err := guard.AddNixInputs(ctx); err != nil { - t.Fatal(err) - } - setupGitTestRun(t, root, "add", "new.nix") - if err := guard.Check(ctx); err == nil { - t.Fatal("accepted new staging") - } - before, err := setupReadIndex(ctx, root) - if err != nil { - t.Fatal(err) - } - if err := guard.Close(ctx); err != nil { - t.Fatal(err) - } - after, err := setupReadIndex(ctx, root) - if err != nil || !reflect.DeepEqual(before, after) { - t.Fatalf("cleanup undid new user staging: %v", err) - } -} - -func TestSetupGitPreservesUserIntentAndUnusualPaths(t *testing.T) { - root := setupGitFixture(t) - ctx := context.Background() - path := "user\tfile\n.nix" - writeSetupGitFile(t, root, path, "{}") - setupGitTestRun(t, root, "add", "--intent-to-add", "--", path) - guard, err := captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - if err := guard.AddNixInputs(ctx); err != nil { - t.Fatal(err) - } - if len(guard.owned) != 0 { - t.Fatal("claimed an existing user intent entry") - } - if err := guard.Close(ctx); err != nil { - t.Fatal(err) + for _, path := range []string{"flake.lock", "apps/web/src/index.ts", ":[literal].nix", ".stack/gen/generated.nix", ".stack/keys/new.nix", "packages/gen/env/src/index.ts"} { + writeSetupGitFile(t, root, path, "new setup output\n") } - index, err := setupReadIndex(ctx, root) - if err != nil || !reflect.DeepEqual(guard.index, index) { - t.Fatalf("lost user's intent entry: %v", err) - } -} - -func TestSetupGitPreservesSymlinkTargets(t *testing.T) { - root := setupGitFixture(t) - link := filepath.Join(root, "user-link") - if err := os.Symlink("first-missing-target", link); err != nil { + if err := files.expose(ctx, nil); err != nil { t.Fatal(err) } - guard, err := captureSetupGit(context.Background(), root) - if err != nil { + // A rerun knows which files predate the setup, so the earlier run's + // output is still new. + writeSetupGitFile(t, root, "apps/web/package.json", "{}\n") + if files, err = newSetupFiles(ctx, state); err != nil { t.Fatal(err) } - if err := os.Remove(link); err != nil { + if err := files.expose(ctx, nil); err != nil { t.Fatal(err) } - if err := os.Symlink("different-missing-target", link); err != nil { - t.Fatal(err) - } - if err := guard.Check(context.Background()); err == nil { - t.Fatal("accepted rewritten symlink") - } -} - -func TestSetupGitUnbornAndGitlessRepositories(t *testing.T) { - ctx := context.Background() - for _, withGit := range []bool{false, true} { - root := t.TempDir() - if withGit { - setupGitTestRun(t, root, "init", "-q") - } - guard, err := captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - writeSetupGitFile(t, root, "flake.nix", "{}") - if err := guard.AddNixInputs(ctx); err != nil { - t.Fatal(err) - } - if err := guard.Check(ctx); err != nil { - t.Fatal(err) + tracked := strings.Split(strings.TrimSpace(string(setupGitTestRun(t, root, "ls-files"))), "\n") + for _, path := range []string{"flake.lock", "apps/web/src/index.ts", "apps/web/package.json", ":[literal].nix", ".stack/config.nix"} { + if !slices.Contains(tracked, path) { + t.Errorf("%s is not visible to Nix: %q", path, tracked) } - if err := guard.Close(ctx); err != nil { - t.Fatal(err) - } - if !withGit { - if _, err := os.Stat(filepath.Join(root, ".git")); !os.IsNotExist(err) { - t.Fatal("created a Git repo") - } + } + for _, path := range []string{"notes [do not touch].txt", ".stack/gen/generated.nix", ".stack/keys/new.nix", "packages/gen/env/src/index.ts"} { + if slices.Contains(tracked, path) { + t.Errorf("%s was added to Git", path) } } + if staged := strings.TrimSpace(string(setupGitTestRun(t, root, "diff", "--cached", "--name-only"))); staged != "staged.txt" { + t.Fatalf("setup staged content: %q", staged) + } } -func TestSetupGitExposesAcceptedNewSourcesWithoutTouchingUserInputs(t *testing.T) { - root := setupGitFixture(t) - ctx := context.Background() - guard, err := captureSetupGit(ctx, root) +func TestSetupFilesOutsideGit(t *testing.T) { + setupStateTestEnvironment(t) + root := t.TempDir() + state := &setupManifest{Version: 1, Root: root, Stage: stageInspection, path: setupStatePath(root)} + files, err := newSetupFiles(context.Background(), state) if err != nil { t.Fatal(err) } - for _, path := range []string{"apps/web/package.json", "apps/web/src/index.ts", "unrelated.txt", "packages/gen/env/src/index.ts"} { - writeSetupGitFile(t, root, path, "new content") - } - if err := guard.AddNixInputs(ctx, "apps/web/package.json", "apps/web/src/index.ts", "notes [do not touch].txt", "packages/gen/env/src/index.ts"); err != nil { + if err := files.expose(context.Background(), []string{"main.go"}); err != nil { t.Fatal(err) } - if len(guard.owned) != 2 { - t.Fatalf("included user files, generated output, or unaccepted files: %+v", guard.owned) - } - for _, path := range []string{"apps/web/package.json", "apps/web/src/index.ts"} { - if len(guard.owned[path]) != 1 || guard.owned[path][0].Flags&setupIntentToAdd == 0 { - t.Fatalf("source unavailable to Git-backed Nix: %s", path) - } - } - if err := guard.Close(ctx); err != nil { - t.Fatal(err) - } - after, err := setupReadIndex(ctx, root) - if err != nil || !reflect.DeepEqual(guard.index, after) { - t.Fatalf("cleanup changed the user's index: %v", err) - } } func setupGitFixture(t *testing.T) string { @@ -243,7 +78,7 @@ func setupGitFixture(t *testing.T) string { for _, path := range []string{"clean.txt", "staged.txt", "unstaged.txt"} { writeSetupGitFile(t, root, path, "committed\n") } - writeSetupGitFile(t, root, ".gitignore", ".stack/gen/\n.stack/profile/\n.stack/state/\n.stack/keys/\n") + writeSetupGitFile(t, root, ".gitignore", ".stack/profile/\n.stack/state/\n") setupGitTestRun(t, root, "add", ".") setupGitTestRun(t, root, "commit", "-qm", "fixture") writeSetupGitFile(t, root, "staged.txt", "staged user edit\n") diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_reply.go b/apps/stackpanel-go/cmd/cli/setup_agent_reply.go deleted file mode 100644 index 717719fb..00000000 --- a/apps/stackpanel-go/cmd/cli/setup_agent_reply.go +++ /dev/null @@ -1,97 +0,0 @@ -package cmd - -import ( - "context" - "errors" - "fmt" - "strings" - - "github.com/darkmatter/stackpanel/stackpanel-go/internal/setupagent" - "github.com/darkmatter/stackpanel/stackpanel-go/internal/tui" -) - -type setupReplyRecovery struct { - Agent string `json:"agent,omitempty"` - Phase setupagent.Phase `json:"phase"` - Message string `json:"message"` - Problem string `json:"problem"` -} - -func runSetupAgentReply(ctx context.Context, agent setupagent.Agent, phase setupagent.Phase, request setupagent.RunRequest, state *setupManifest, ui *tui.SetupUI) (*setupagent.Reply, error) { - originalRequest := request - if state.PendingReply != nil && phase == setupagent.Inspection { - switchedAgent := state.PendingReply.Agent != "" && state.PendingReply.Agent != agent.ID - nonJSONProse := !strings.HasPrefix(strings.TrimSpace(state.PendingReply.Message), "{") - if switchedAgent || nonJSONProse { - state.PendingReply = nil - if state.path != "" { - if err := state.save(); err != nil { - return nil, err - } - } - } - } - hadSavedPending := state.PendingReply != nil - if state.PendingReply == nil { - result, err := setupagent.Run(ctx, agent, request) - var formatErr *setupagent.ReplyFormatError - if err != nil && !errors.As(err, &formatErr) { - return nil, fmt.Errorf("%s %s: %w (check the backend's credentials and permissions; --agent-log records diagnostics)", agent.ID, phase, err) - } - reply, parseErr := setupagent.ParseReply(result.Message) - if parseErr == nil { - return reply, nil - } - if !errors.As(parseErr, &formatErr) { - return nil, parseErr - } - state.PendingReply = &setupReplyRecovery{Agent: agent.ID, Phase: phase, Message: result.Message, Problem: parseErr.Error()} - if state.path != "" { - if err := state.save(); err != nil { - return nil, err - } - } - } - pending := state.PendingReply - if pending.Phase != phase { - return nil, fmt.Errorf("saved reply belongs to %s, not %s; use --restart to review a new plan", pending.Phase, phase) - } - reply, err := setupagent.ParseReply(pending.Message) - if err != nil { - ui.Warning(fmt.Sprintf("%s returned malformed setup JSON. Requesting a corrected reply · retry 1 of 1. Your choices are saved.", agent.ID)) - request.ReadOnly = true - request.Prompt = setupagent.ReplyCorrectionPrompt(phase, pending.Message, pending.Problem) - result, err := setupagent.Run(ctx, agent, request) - if err != nil { - return nil, fmt.Errorf("%s reply correction failed; original reply saved for retry: %w", agent.ID, err) - } - reply, err = setupagent.ParseReply(result.Message) - if err != nil { - return nil, fmt.Errorf("%s returned an invalid reply after one formatting retry; original reply saved: %w", agent.ID, err) - } - } - if reply.Status == "blocked" { - state.PendingReply = nil - if state.path != "" { - if err := state.save(); err != nil { - return nil, err - } - } - if hadSavedPending && phase == setupagent.Inspection { - ui.Progress(fmt.Sprintf("Previous inspection reply could not be recovered; running a fresh %s inspection…", agent.ID)) - return runSetupAgentReply(ctx, agent, phase, originalRequest, state, ui) - } - return nil, fmt.Errorf("%s could not recover its reply: %s", agent.ID, reply.Summary) - } - if (phase == setupagent.Inspection && reply.Status != "plan" && reply.Status != "needs_input") || - (phase != setupagent.Inspection && reply.Status != "complete" && reply.Status != "needs_input") { - return nil, fmt.Errorf("%s reply correction returned unexpected status %q for %s", agent.ID, reply.Status, phase) - } - state.PendingReply = nil - if state.path != "" { - if err := state.save(); err != nil { - return nil, err - } - } - return reply, nil -} diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_reply_test.go b/apps/stackpanel-go/cmd/cli/setup_agent_reply_test.go deleted file mode 100644 index 72d307a6..00000000 --- a/apps/stackpanel-go/cmd/cli/setup_agent_reply_test.go +++ /dev/null @@ -1,293 +0,0 @@ -package cmd - -import ( - "bytes" - "context" - "encoding/json" - "os" - "path/filepath" - "strings" - "testing" - - "github.com/darkmatter/stackpanel/stackpanel-go/internal/reconcile" - "github.com/darkmatter/stackpanel/stackpanel-go/internal/setupagent" - "github.com/darkmatter/stackpanel/stackpanel-go/internal/tui" -) - -func setupReplyFixture(t *testing.T) (setupagent.Agent, string) { - t.Helper() - dir := t.TempDir() - path := filepath.Join(dir, "codex") - script := `#!/bin/sh -base=$(dirname "$0") -input=$(cat) -case "$input" in - *'formatting retry only'*) - printf '%s\n' "$*" >> "$base/correction-calls" - printf '%s' "$input" > "$base/correction-prompt" - cat "$base/correction-events" ;; - *) - printf '%s\n' "$*" >> "$base/original-calls" - cat "$base/original-events" ;; -esac -` - if err := os.WriteFile(path, []byte(script), 0o700); err != nil { - t.Fatal(err) - } - return setupagent.Agent{ID: "codex", Path: path}, dir -} - -func writeSetupReplyEvents(t *testing.T, dir, name, message string) { - t.Helper() - text, _ := json.Marshal(message) - events := `{"type":"item.completed","item":{"type":"agent_message","text":` + string(text) + "}}\n{\"type\":\"turn.completed\"}\n" - if err := os.WriteFile(filepath.Join(dir, name+"-events"), []byte(events), 0o600); err != nil { - t.Fatal(err) - } -} - -func TestSetupReplyCorrectionResumesWithoutRepeatingWork(t *testing.T) { - for _, phase := range []setupagent.Phase{setupagent.Inspection, setupagent.Setup, setupagent.Repair} { - t.Run(string(phase), func(t *testing.T) { - setupStateTestEnvironment(t) - agent, fixture := setupReplyFixture(t) - root := t.TempDir() - message := `{"status":"complete","summary":"Created agreed files"}` - if phase == setupagent.Inspection { - message = `{"status":"plan","plan":{"summary":"Create app","expectations":{"version":1,"files":["src/main.ts"],"config":[{"path":["enable"],"equals":true}],"commands":[{"id":"build","scope":"build","dir":".","argv":["bun","run","build"]}]}}}` - } - // Missing content still requires a model correction. A missing final - // outer brace alone is recovered without another invocation. - malformed := strings.TrimSuffix(message, "}") - if phase == setupagent.Inspection { - malformed = strings.TrimSuffix(malformed, "}") - } else { - malformed = strings.TrimSuffix(malformed, `"`) - } - writeSetupReplyEvents(t, fixture, "original", malformed) - writeSetupReplyEvents(t, fixture, "correction", malformed) - state := &setupManifest{Version: 1, Root: root, Stage: "apply", path: setupStatePath(root), - Plan: &setupagent.Plan{Expectations: reconcile.Expectations{Version: 1, RequiredChecks: []string{"keep-check"}}}, - Request: setupagent.SetupRequest{Root: root, Answers: []setupagent.Answer{{ID: "framework", Values: []string{"Bun"}}}}, - } - var output bytes.Buffer - ui := tui.NewSetupUI(context.Background(), false, &output) - defer ui.Close() - request := setupagent.RunRequest{Dir: root, ReadOnly: phase == setupagent.Inspection, Prompt: "Original onboarding invocation"} - _, err := runSetupAgentReply(context.Background(), agent, phase, request, state, ui) - if err == nil || !strings.Contains(err.Error(), "after one formatting retry") { - t.Fatalf("unbounded/accepted bad reply: %v", err) - } - saved, err := loadSetupManifest(root) - if err != nil { - t.Fatal(err) - } - if saved.PendingReply == nil || saved.PendingReply.Message != malformed || len(saved.Request.Answers) != 1 { - t.Fatalf("lost recovery state: %+v", saved) - } - writeSetupReplyEvents(t, fixture, "correction", message) - reply, err := runSetupAgentReply(context.Background(), agent, phase, request, saved, ui) - if err != nil || reply == nil { - t.Fatalf("resume reply: %+v %v", reply, err) - } - completed, err := loadSetupManifest(root) - if err != nil || completed.PendingReply != nil { - t.Fatalf("recovery state not cleared: %+v %v", completed, err) - } - originalCalls, _ := os.ReadFile(filepath.Join(fixture, "original-calls")) - if bytes.Count(originalCalls, []byte("\n")) != 1 { - t.Fatalf("repeated original work: %s", originalCalls) - } - corrections, _ := os.ReadFile(filepath.Join(fixture, "correction-calls")) - if bytes.Count(corrections, []byte("--sandbox read-only")) != 2 || bytes.Contains(corrections, []byte("workspace-write")) { - t.Fatalf("correction can write files: %s", corrections) - } - prompt, _ := os.ReadFile(filepath.Join(fixture, "correction-prompt")) - if !bytes.Contains(prompt, []byte("Do not use tools")) || !bytes.Contains(prompt, []byte("do not invent missing content")) { - t.Fatal("correction prompt lost scope") - } - }) - } -} - -func TestSetupReplyDoesNotRetryProviderFailure(t *testing.T) { - agent, fixture := setupReplyFixture(t) - if err := os.WriteFile(filepath.Join(fixture, "original-events"), []byte("{\"type\":\"turn.failed\",\"error\":{\"message\":\"permission denied\"}}\n"), 0o600); err != nil { - t.Fatal(err) - } - var output bytes.Buffer - ui := tui.NewSetupUI(context.Background(), false, &output) - defer ui.Close() - state := &setupManifest{} - _, err := runSetupAgentReply(context.Background(), agent, setupagent.Setup, setupagent.RunRequest{Dir: t.TempDir()}, state, ui) - if err == nil || !strings.Contains(err.Error(), "permission denied") { - t.Fatalf("lost provider error: %v", err) - } - if state.PendingReply != nil { - t.Fatal("provider failure became formatting recovery") - } - if _, err := os.Stat(filepath.Join(fixture, "correction-calls")); !os.IsNotExist(err) { - t.Fatal("retried provider permission error") - } -} - -func TestSetupReplyCorrectionCannotReplaceAcceptedPlan(t *testing.T) { - agent, fixture := setupReplyFixture(t) - writeSetupReplyEvents(t, fixture, "original", `{"status":"complete","summary":"Done`) - writeSetupReplyEvents(t, fixture, "correction", `{"status":"plan","plan":{"summary":"Weaken checks","expectations":{"version":1,"config":[{"path":["enable"],"equals":true}]}}}`) - var output bytes.Buffer - ui := tui.NewSetupUI(context.Background(), false, &output) - defer ui.Close() - state := &setupManifest{} - _, err := runSetupAgentReply(context.Background(), agent, setupagent.Setup, setupagent.RunRequest{Dir: t.TempDir()}, state, ui) - if err == nil || !strings.Contains(err.Error(), "unexpected status") || state.PendingReply == nil { - t.Fatalf("accepted replacement plan: %v", err) - } -} - -func TestSetupReplyMissingFinalOuterBraceDoesNotRetry(t *testing.T) { - for _, phase := range []setupagent.Phase{setupagent.Inspection, setupagent.Setup} { - t.Run(string(phase), func(t *testing.T) { - agent, fixture := setupReplyFixture(t) - message := `{"status":"complete","summary":"Created agreed files"}` - if phase == setupagent.Inspection { - message = `{"status":"plan","plan":{"summary":"Configure app","expectations":{"version":1,"config":[{"path":["enable"],"equals":true}]}}}` - } - writeSetupReplyEvents(t, fixture, "original", strings.TrimSuffix(message, "}")) - var output bytes.Buffer - ui := tui.NewSetupUI(context.Background(), false, &output) - defer ui.Close() - state := &setupManifest{} - reply, err := runSetupAgentReply(context.Background(), agent, phase, setupagent.RunRequest{ - Dir: t.TempDir(), ReadOnly: phase == setupagent.Inspection, - }, state, ui) - if err != nil || reply == nil || state.PendingReply != nil { - t.Fatalf("recover reply: %+v, state %+v, error %v", reply, state.PendingReply, err) - } - if strings.Contains(output.String(), "malformed") { - t.Fatal("recoverable reply displayed a malformed JSON warning") - } - if _, err := os.Stat(filepath.Join(fixture, "correction-calls")); !os.IsNotExist(err) { - t.Fatalf("recoverable reply invoked correction: %v", err) - } - }) - } -} - -func TestSetupSavedReplyMissingFinalOuterBraceDoesNotRetry(t *testing.T) { - for _, phase := range []setupagent.Phase{setupagent.Inspection, setupagent.Setup, setupagent.Repair} { - t.Run(string(phase), func(t *testing.T) { - setupStateTestEnvironment(t) - agent, fixture := setupReplyFixture(t) - root := t.TempDir() - message := `{"status":"complete","summary":"Created agreed files"}` - if phase == setupagent.Inspection { - message = `{"status":"plan","plan":{"summary":"Configure app","expectations":{"version":1,"config":[{"path":["enable"],"equals":true}]}}}` - } - state := &setupManifest{Version: 1, Root: root, Stage: "apply", path: setupStatePath(root), - Plan: &setupagent.Plan{Expectations: reconcile.Expectations{Version: 1, RequiredChecks: []string{"keep-check"}}}, - Request: setupagent.SetupRequest{Root: root, Answers: []setupagent.Answer{{ID: "framework", Values: []string{"Bun"}}}}, - PendingReply: &setupReplyRecovery{Phase: phase, Message: strings.TrimSuffix(message, "}"), Problem: "invalid setup reply: unexpected EOF"}, - } - if err := state.save(); err != nil { - t.Fatal(err) - } - loaded, err := loadSetupManifest(root) - if err != nil { - t.Fatal(err) - } - var output bytes.Buffer - ui := tui.NewSetupUI(context.Background(), false, &output) - defer ui.Close() - reply, err := runSetupAgentReply(context.Background(), agent, phase, setupagent.RunRequest{ - Dir: root, ReadOnly: phase == setupagent.Inspection, - }, loaded, ui) - if err != nil || reply == nil { - t.Fatalf("recover saved reply: %+v, error %v", reply, err) - } - saved, err := loadSetupManifest(root) - if err != nil || saved.PendingReply != nil || len(saved.Request.Answers) != 1 || - saved.Plan == nil || len(saved.Plan.Expectations.RequiredChecks) != 1 || saved.Plan.Expectations.RequiredChecks[0] != "keep-check" { - t.Fatalf("lost saved state: %+v, error %v", saved, err) - } - if strings.Contains(output.String(), "malformed") { - t.Fatal("recoverable saved reply displayed a malformed JSON warning") - } - for _, name := range []string{"original-calls", "correction-calls"} { - if _, err := os.Stat(filepath.Join(fixture, name)); !os.IsNotExist(err) { - t.Fatalf("recoverable saved reply invoked %s: %v", name, err) - } - } - }) - } -} - -func TestSetupSavedReplyCannotReplaceAcceptedPlan(t *testing.T) { - agent, _ := setupReplyFixture(t) - var output bytes.Buffer - ui := tui.NewSetupUI(context.Background(), false, &output) - defer ui.Close() - state := &setupManifest{PendingReply: &setupReplyRecovery{ - Phase: setupagent.Setup, - Message: `{"status":"plan","plan":{"summary":"Weaken checks","expectations":{"version":1,"config":[{"path":["enable"],"equals":true}]}}`, - }} - _, err := runSetupAgentReply(context.Background(), agent, setupagent.Setup, setupagent.RunRequest{Dir: t.TempDir()}, state, ui) - if err == nil || !strings.Contains(err.Error(), "unexpected status") || state.PendingReply == nil { - t.Fatalf("accepted replacement plan from saved reply: %v", err) - } -} - -func TestSetupSavedBlockedInspectionReplyFallsBackToFreshInspection(t *testing.T) { - setupStateTestEnvironment(t) - agent, fixture := setupReplyFixture(t) - root := t.TempDir() - planMsg := `{"status":"plan","plan":{"summary":"Configure Rust app","expectations":{"version":1,"config":[{"path":["enable"],"equals":true}]}}}` - writeSetupReplyEvents(t, fixture, "original", planMsg) - writeSetupReplyEvents(t, fixture, "correction", `{"status":"blocked","summary":"Inspection is incomplete: zsh attempted to create a temporary file on the read-only filesystem"}`) - - // Case 1: Saved non-JSON prose from a failed earlier run is discarded immediately in inspection. - state := &setupManifest{ - Version: 1, - Root: root, - Stage: "inspection", - path: setupStatePath(root), - PendingReply: &setupReplyRecovery{ - Phase: setupagent.Inspection, - Message: "Inspection is incomplete: zsh attempted to create a temporary file on the read-only filesystem.", - Problem: "invalid setup reply: invalid character 'I' looking for beginning of value", - }, - } - if err := state.save(); err != nil { - t.Fatal(err) - } - var output bytes.Buffer - ui := tui.NewSetupUI(context.Background(), false, &output) - defer ui.Close() - reply, err := runSetupAgentReply(context.Background(), agent, setupagent.Inspection, setupagent.RunRequest{ - Dir: root, ReadOnly: true, - }, state, ui) - if err != nil || reply == nil || reply.Status != "plan" { - t.Fatalf("expected fresh inspection plan, got reply=%+v err=%v", reply, err) - } - if state.PendingReply != nil { - t.Fatalf("expected PendingReply to be cleared, got %+v", state.PendingReply) - } - - // Case 2: Saved truncated JSON whose correction returns status="blocked" in inspection - // clears PendingReply and runs a fresh inspection instead of trapping the manifest. - state.PendingReply = &setupReplyRecovery{ - Agent: agent.ID, - Phase: setupagent.Inspection, - Message: `{"status":"plan","plan":`, - Problem: "invalid setup reply: unexpected EOF", - } - reply, err = runSetupAgentReply(context.Background(), agent, setupagent.Inspection, setupagent.RunRequest{ - Dir: root, ReadOnly: true, - }, state, ui) - if err != nil || reply == nil || reply.Status != "plan" { - t.Fatalf("expected fallback to fresh inspection after blocked recovery, got reply=%+v err=%v", reply, err) - } - if state.PendingReply != nil { - t.Fatalf("expected PendingReply to be cleared after blocked recovery, got %+v", state.PendingReply) - } -} diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_runtime.go b/apps/stackpanel-go/cmd/cli/setup_agent_runtime.go index 8148b5dc..d15e3621 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent_runtime.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent_runtime.go @@ -1,7 +1,6 @@ package cmd import ( - "bytes" "context" "fmt" "io" @@ -14,11 +13,11 @@ import ( "runtime" "slices" "strconv" + "strings" "syscall" "time" "github.com/darkmatter/stackpanel/stackpanel-go/internal/agent/config" - "github.com/darkmatter/stackpanel/stackpanel-go/internal/reconcile" "github.com/darkmatter/stackpanel/stackpanel-go/internal/setupsession" "github.com/darkmatter/stackpanel/stackpanel-go/internal/tui" "github.com/darkmatter/stackpanel/stackpanel-go/pkg/services" @@ -75,17 +74,13 @@ func runSetupRuntime(ctx context.Context, root, executable string, selected []st } } ui.Progress("Verifying runtime with stack doctor…") - report, err := runFreshDoctorArgs(ctx, root, executable, debug, - []string{"--strict", "--scope", "runtime", "--setup-session", session.ID, "--json"}, - []string{"checks", "runtime", "verification"}) + report, err := runFreshDoctor(ctx, root, executable, debug, "--strict", "--scope", "runtime", "--setup-session", session.ID, "--json") if err != nil { - if report != nil { - var rendered bytes.Buffer - report.Render(&rendered, reconcile.RenderOptions{Title: "stack doctor · runtime verification", Verbose: true}) - return "", fmt.Errorf("%w\n%s", err, rendered.String()) - } return "", err } + if problems := doctorProblems(report); len(problems) > 0 { + return "", fmt.Errorf("runtime doctor found problems:\n %s", strings.Join(problems, "\n ")) + } ui.Verified("Local agent and services · runtime doctor passed") if opts.noBrowser { return "Repository and runtime verified. Studio unverified (--no-browser).", nil diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_state.go b/apps/stackpanel-go/cmd/cli/setup_agent_state.go index f4d806d4..57d8bdf4 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent_state.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent_state.go @@ -12,34 +12,48 @@ import ( "reflect" "time" - "github.com/darkmatter/stackpanel/stackpanel-go/internal/reconcile" "github.com/darkmatter/stackpanel/stackpanel-go/internal/setupagent" "github.com/darkmatter/stackpanel/stackpanel-go/pkg/userconfig" "github.com/gofrs/flock" "github.com/spf13/cobra" ) -// The host owns this manifest outside the coding agent's writable repository. -// It records progress, never proof of success: doctor must run again on resume. +// setupStage is the manifest's resume point. Stages advance +// inspection → review → apply → verify → runtime → complete; problems found +// by verify move to repair, which returns to verify. +type setupStage string + +const ( + stageInspection setupStage = "inspection" + stageReview setupStage = "review" + stageApply setupStage = "apply" + stageRepair setupStage = "repair" + stageVerify setupStage = "verify" + stageRuntime setupStage = "runtime" + stageComplete setupStage = "complete" +) + +// The host keeps this manifest outside the repository. It records progress, +// never proof of success: doctor runs again on resume. type setupManifest struct { Version int `json:"version"` Root string `json:"root"` Agent string `json:"agent,omitempty"` - Stage string `json:"stage"` + Stage setupStage `json:"stage"` Options setupSavedOptions `json:"options"` Request setupagent.SetupRequest `json:"request"` Plan *setupagent.Plan `json:"plan,omitempty"` Pending []setupagent.Question `json:"pendingQuestions,omitempty"` - PendingReply *setupReplyRecovery `json:"pendingReply,omitempty"` Conversation []setupagent.Exchange `json:"conversation,omitempty"` Pi *setupagent.PiState `json:"pi,omitempty"` Failure string `json:"verificationFailure,omitempty"` LastError string `json:"lastError,omitempty"` StudioSession string `json:"studioSession,omitempty"` - Git *setupGitCheckpoint `json:"git,omitempty"` - GitViolation *setupGitCheckpoint `json:"gitViolation,omitempty"` - UpdatedAt time.Time `json:"updatedAt"` - path string + // UntrackedAtStart lists the untracked files that predate the setup. It is + // not omitempty: an empty list was recorded, while null was not yet. + UntrackedAtStart []string `json:"untrackedAtStart"` + UpdatedAt time.Time `json:"updatedAt"` + path string } type setupSavedOptions struct { @@ -56,14 +70,6 @@ type setupSavedOptions struct { AgentModel string `json:"agentModel,omitempty"` } -type setupGitCheckpoint struct { - Root string `json:"root"` - Head string `json:"head"` - Editable map[string]setupFileState `json:"editable"` - Protected map[string]setupFileState `json:"protected,omitempty"` - Index map[string][]setupIndexEntry `json:"index"` -} - func setupStateDirectory() string { return filepath.Join(filepath.Dir(userconfig.GetConfigPath()), "setup") } @@ -78,14 +84,11 @@ func readSetupJSON(path string, value any) error { return err } defer f.Close() - d := json.NewDecoder(io.LimitReader(f, 16<<20)) - d.DisallowUnknownFields() - if err := d.Decode(value); err != nil { + // Fields from older or newer releases are ignored, so a release change does + // not strand a saved setup. + if err := json.NewDecoder(io.LimitReader(f, 16<<20)).Decode(value); err != nil { return fmt.Errorf("read setup state %s: %w", path, err) } - if err := d.Decode(new(any)); err != io.EOF { - return fmt.Errorf("invalid trailing setup state in %s", path) - } return nil } @@ -128,17 +131,14 @@ func loadSetupManifest(root string) (*setupManifest, error) { return nil, fmt.Errorf("unsupported or mismatched setup manifest: %s", s.path) } switch s.Stage { - case "inspection", "review", "apply", "repair", "verify", "runtime", "complete": + case stageInspection, stageReview, stageApply, stageRepair, stageVerify, stageRuntime, stageComplete: default: - return nil, fmt.Errorf("invalid setup stage in %s", s.path) - } - if s.Stage != "inspection" && s.Plan == nil { - return nil, fmt.Errorf("missing saved plan in %s", s.path) + s.Stage = stageInspection } - if s.Plan != nil { - if err := errors.Join(reconcile.ValidateExpectations(s.Plan.Expectations), setupagent.ValidatePrepare(s.Plan.Prepare)); err != nil { - return nil, fmt.Errorf("invalid saved plan: %w", err) - } + if s.Plan == nil { + s.Stage = stageInspection + } else { + setupagent.NormalizePlan(s.Plan) } return s, nil } @@ -148,75 +148,33 @@ func (s *setupManifest) save() error { return writeSetupJSON(s.path, s) } -// Take a new Git baseline on every invocation. Only unchanged, checkpointed -// setup output is editable again; user edits/staging since a checkpoint remain -// protected. An abrupt kill can leave uncheckpointed output, which is preserved -// conservatively too. We never reset HEAD, restore files, or unstage user work. -func (s *setupManifest) restoreGit(g *setupGitGuard) { - if s.Git == nil || s.Git.Root != g.root || s.Git.Head != g.head { - return - } - for path, state := range s.Git.Editable { - if current, ok := g.protected[path]; ok && current == state && reflect.DeepEqual(g.index[path], s.Git.Index[path]) { - delete(g.protected, path) - } - } -} - -func isEditableStackpanelConfig(rel string) bool { - switch filepath.ToSlash(filepath.Clean(rel)) { - case ".stack/config.nix", ".stack/data.nix", ".stack/reconcile.json", ".stack/addons.json": - return true - default: - return false +// persist saves a file-backed manifest. Helpers also run against in-memory +// manifests, which have no path and nothing to save. +func (s *setupManifest) persist() error { + if s.path == "" { + return nil } + return s.save() } -func (g *setupGitGuard) allowStackpanelConfig(target string) { - if g == nil || g.root == "" { - return +// recordOutcome saves the run's error, which the next run passes to the agent, +// and tells the user how to resume. +func (s *setupManifest) recordOutcome(retErr *error) { + s.LastError = "" + if *retErr != nil { + s.LastError = (*retErr).Error() } - for path := range g.protected { - rel, err := filepath.Rel(target, filepath.Join(g.root, path)) - if err == nil && isEditableStackpanelConfig(rel) { - delete(g.protected, path) - } + *retErr = errors.Join(*retErr, s.save()) + if *retErr != nil { + *retErr = fmt.Errorf("%w\nSetup saved for %s\nRerun the same command to resume. Manifest: %s", *retErr, s.Root, s.path) } } -func (s *setupManifest) checkGitViolation(ctx context.Context) error { - if previous := s.GitViolation; previous != nil { - for path := range previous.Protected { - rel, err := filepath.Rel(s.Root, filepath.Join(previous.Root, path)) - if err == nil && isEditableStackpanelConfig(rel) { - delete(previous.Protected, path) - } - } - guard := &setupGitGuard{root: previous.Root, head: previous.Head, index: previous.Index, protected: previous.Protected} - if err := guard.Check(ctx); err != nil { - return err - } - s.GitViolation = nil - } - return nil -} - -func (s *setupManifest) checkpoint(ctx context.Context, g *setupGitGuard) error { - if err := g.Check(ctx); err != nil { - return err - } - current, err := captureSetupGit(ctx, s.Root) - if err != nil { - return err - } - snapshot := &setupGitCheckpoint{Root: g.root, Head: g.head, Editable: map[string]setupFileState{}, Index: current.index} - for path, state := range current.protected { - if _, protected := g.protected[path]; !protected { - snapshot.Editable[path] = state - } - } - s.Git = snapshot - return s.save() +// restart discards the saved plan and answers, as --restart does. Files that +// predate the setup are still known, so earlier runs' output counts as new. +func (s *setupManifest) restart(opts setupFlags) { + *s = setupManifest{Version: 1, Root: s.Root, Stage: stageInspection, Options: savedSetupOptions(opts), + UntrackedAtStart: s.UntrackedAtStart, path: s.path} } func setupStateLock(path string) (*flock.Flock, error) { @@ -270,16 +228,14 @@ func openSetupManifest(ctx context.Context, opts setupFlags) (*setupManifest, fu var saved struct { Root string `json:"root"` } - if err := readSetupJSON(pointer, &saved); err == nil { - s, err := loadSetupManifest(saved.Root) - if err != nil { - return nil, nil, false, fmt.Errorf("load temporary setup: %w; use --restart for a fresh repository", err) + // Reuse the unfinished temporary repository if it is still there; + // otherwise create a new one. + if readSetupJSON(pointer, &saved) == nil { + if s, err := loadSetupManifest(saved.Root); err == nil && s.Stage != stageComplete { + if _, err := os.Stat(s.Root); err == nil { + root = s.Root + } } - if s.Stage != "complete" { - root = s.Root - } - } else if !os.IsNotExist(err) { - return nil, nil, false, err } } } else if opts.newDir != "" { @@ -309,17 +265,17 @@ func openSetupManifest(ctx context.Context, opts setupFlags) (*setupManifest, fu return nil, nil, false, err } locks = append(locks, lock) - var s *setupManifest - resuming := false - if !opts.restart { - s, err = loadSetupManifest(root) - if err != nil && !os.IsNotExist(err) { - return nil, nil, false, fmt.Errorf("%w; use --restart to review a new plan without deleting repository files", err) - } - resuming = err == nil + s, err := loadSetupManifest(root) + resuming := err == nil && !opts.restart + switch { + case resuming: + case err == nil: + s.restart(opts) + default: + // No manifest, or one this release cannot use: start a new setup. + s = &setupManifest{Version: 1, Root: root, Stage: stageInspection, path: setupStatePath(root), Options: savedSetupOptions(opts)} } - if s == nil { - s = &setupManifest{Version: 1, Root: root, Stage: "inspection", path: setupStatePath(root), Options: savedSetupOptions(opts)} + if !resuming { if err := s.save(); err != nil { return nil, nil, false, err } @@ -340,9 +296,10 @@ func savedSetupOptions(opts setupFlags) setupSavedOptions { opts.noRuntime, opts.noBrowser, opts.studioURL, opts.agentPort, opts.agentModel} } -func (s *setupManifest) restoreOptions(cmd *cobra.Command, opts *setupFlags) error { - // Explicit configuration changes require a newly reviewed plan. Omitted flags - // inherit saved selections; runtime endpoints and scope may change on retry. +// restoreOptions applies the saved selections to flags the user omitted. +// Runtime endpoints and scope may change on retry; a changed selection needs a +// new plan, so restoreOptions returns its flag name and leaves opts unchanged. +func (s *setupManifest) restoreOptions(cmd *cobra.Command, opts *setupFlags) (changed string) { for _, flag := range []struct { name string current, saved any @@ -353,7 +310,7 @@ func (s *setupManifest) restoreOptions(cmd *cobra.Command, opts *setupFlags) err {"agent-model", opts.agentModel, s.Options.AgentModel}, } { if cmd.Flags().Changed(flag.name) && !reflect.DeepEqual(flag.current, flag.saved) { - return fmt.Errorf("--%s differs from the saved setup selections; use --restart to review a new plan", flag.name) + return flag.name } } opts.flake, opts.template = s.Options.Flake, s.Options.Template @@ -375,5 +332,5 @@ func (s *setupManifest) restoreOptions(cmd *cobra.Command, opts *setupFlags) err opts.experimentalAgent = s.Agent } s.Options = savedSetupOptions(*opts) - return nil + return "" } diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_state_test.go b/apps/stackpanel-go/cmd/cli/setup_agent_state_test.go index 85f59f40..4a3c58a7 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent_state_test.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent_state_test.go @@ -9,7 +9,6 @@ import ( "strings" "testing" - "github.com/darkmatter/stackpanel/stackpanel-go/internal/reconcile" "github.com/darkmatter/stackpanel/stackpanel-go/internal/setupagent" "github.com/darkmatter/stackpanel/stackpanel-go/internal/setupsession" "github.com/darkmatter/stackpanel/stackpanel-go/internal/tui" @@ -86,131 +85,84 @@ func mustSetupRelative(t *testing.T, root, path string) string { return rel } -func TestSetupManifestRejectsCorruption(t *testing.T) { +func TestSetupManifestLoadsOlderAndDamagedState(t *testing.T) { setupStateTestEnvironment(t) - root := t.TempDir() - s := &setupManifest{Version: 1, Root: root, Stage: "apply", path: setupStatePath(root), Plan: &setupagent.Plan{Expectations: reconcile.Expectations{Version: 1}}} - if err := s.save(); err != nil { + root, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { t.Fatal(err) } - prepared := `{"version":1,"root":` + jsonString(root) + `,"stage":"apply","plan":{"summary":"x","expectations":{"version":1,"config":[{"path":["enable"],"equals":true}],"requiredChecks":[]},"prepare":[{"id":"deps","dir":".","argv":["bun","install"]}]}}` - for _, content := range []string{`{"version":`, `{"version":99,"root":` + jsonString(root) + `,"stage":"inspection"}`, `{"version":1,"root":"/wrong","stage":"inspection"}`, `{"version":1,"root":` + jsonString(root) + `,"stage":"apply"}`, - strings.Replace(prepared, `"dir":"."`, `"dir":"../outside"`, 1)} { - if err := os.WriteFile(s.path, []byte(content), 0o600); err != nil { + path := setupStatePath(root) + load := func(content string) (*setupManifest, error) { + t.Helper() + if err := writeSetupJSON(path, json.RawMessage(content)); err != nil { t.Fatal(err) } - if _, err := loadSetupManifest(root); err == nil { - t.Fatalf("accepted invalid manifest: %s", content) + return loadSetupManifest(root) + } + // Older releases saved Git checkpoints and malformed replies. + loaded, err := load(`{"version":1,"root":` + jsonString(root) + `,"stage":"apply","git":{"head":"x"},"pendingReply":{"message":"x"}, + "plan":{"summary":"x","expectations":{"version":1,"config":[{"path":["enable"],"equals":true}],"requiredChecks":["old-check"]}, + "prepare":[{"id":"deps","dir":".","argv":["bun","install"]},{"id":"escape","dir":"../outside","argv":["sh"]}]}}`) + if err != nil || loaded.Stage != stageApply || len(loaded.Plan.Prepare) != 1 || len(loaded.Plan.Expectations.RequiredChecks) != 0 { + t.Fatalf("older manifest: %+v, %v", loaded, err) + } + if loaded, err := load(`{"version":1,"root":` + jsonString(root) + `,"stage":"verify"}`); err != nil || loaded.Stage != stageInspection { + t.Fatalf("a stage without its plan must restart inspection: %+v, %v", loaded, err) + } + for _, content := range []string{`{"version":99,"root":` + jsonString(root) + `}`, `{"version":1,"root":"/wrong"}`} { + if _, err := load(content); err == nil { + t.Fatalf("accepted another release's or repository's manifest: %s", content) } } - if err := os.WriteFile(s.path, []byte(prepared), 0o600); err != nil { + // Setup starts over rather than stopping on a manifest it cannot use. + if err := os.WriteFile(path, []byte(`{"version":`), 0o600); err != nil { t.Fatal(err) } - if loaded, err := loadSetupManifest(root); err != nil || len(loaded.Plan.Prepare) != 1 { - t.Fatalf("saved host preparation was not restored: %+v, %v", loaded, err) + t.Chdir(root) + s, closeState, resuming, err := openSetupManifest(context.Background(), setupFlags{}) + if err != nil || resuming || s.Stage != stageInspection { + t.Fatalf("damaged manifest: %+v %v %v", s, resuming, err) } + closeState() } func jsonString(s string) string { b, _ := json.Marshal(s); return string(b) } -func TestSetupAnswersCheckpointBeforeNextQuestion(t *testing.T) { +func TestSetupAnswersAreSavedAndNotAskedAgain(t *testing.T) { setupStateTestEnvironment(t) root := t.TempDir() - s := &setupManifest{Version: 1, Root: root, Stage: "inspection", path: setupStatePath(root), Pending: []setupagent.Question{ + questions := []setupagent.Question{ {ID: "language", Prompt: "Language?", Kind: "single", Options: []string{"Go"}, Default: []string{"Go"}, Required: true}, {ID: "name", Prompt: "Name?", Kind: "text", Required: true}, - }} + } + s := &setupManifest{Version: 1, Root: root, Stage: stageInspection, path: setupStatePath(root), Pending: questions} var output bytes.Buffer ui := tui.NewSetupUI(context.Background(), false, &output) defer ui.Close() - if err := answerSetupQuestions(s, &s.Request, ui); err == nil { - t.Fatal("missing required answer did not stop setup") - } - saved, err := loadSetupManifest(root) - if err != nil { - t.Fatal(err) - } - if len(saved.Pending) != 1 || saved.Pending[0].ID != "name" || len(saved.Request.Answers) != 1 || len(saved.Conversation) != 1 { - t.Fatalf("lost completed answer or pending question: %+v", saved) - } - // Supply the remaining answer through the same UI path; the saved language - // answer must survive without being appended or asked a second time. - saved.Pending[0].Default = []string{"hello"} - if err := answerSetupQuestions(saved, &saved.Request, ui); err != nil { + // Without a person to ask, a question with no default leaves the choice to + // the agent instead of stopping setup. + if err := answerSetupQuestions(s, &s.Request, ui); err != nil { t.Fatal(err) } - saved, err = loadSetupManifest(root) - if err != nil || len(saved.Request.Answers) != 2 || len(saved.Conversation) != 2 || len(saved.Pending) != 0 { - t.Fatalf("resume answers: %+v %v", saved, err) + saved, err := loadSetupManifest(root) + if err != nil || len(saved.Pending) != 0 || len(saved.Conversation) != 2 || len(saved.Request.Answers) != 2 || + saved.Request.Answers[0].Values[0] != "Go" || len(saved.Request.Answers[1].Values) != 0 { + t.Fatalf("answers not saved: %+v %v", saved, err) } -} - -func TestSetupResumeProtectsEditsBetweenRuns(t *testing.T) { - for _, change := range []string{"none", "working", "staged", "head", "uncheckpointed"} { - t.Run(change, func(t *testing.T) { - setupStateTestEnvironment(t) - root := setupGitFixture(t) - ctx := context.Background() - g, err := captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - s := &setupManifest{Version: 1, Root: root, Stage: "inspection", path: setupStatePath(root)} - writeSetupGitFile(t, root, "clean.txt", "setup work") - writeSetupGitFile(t, root, ".stack/new.nix", "{}") - if err := s.checkpoint(ctx, g); err != nil { - t.Fatal(err) - } - switch change { - case "working": - writeSetupGitFile(t, root, "clean.txt", "user edited after failure") - case "staged": - setupGitTestRun(t, root, "add", "clean.txt") - case "head": - setupGitTestRun(t, root, "commit", "--allow-empty", "-qm", "user commit") - case "uncheckpointed": - writeSetupGitFile(t, root, "clean.txt", "unknown work after checkpoint") - } - g, err = captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - saved, err := loadSetupManifest(root) - if err != nil { - t.Fatal(err) - } - saved.restoreGit(g) - _, protected := g.protected["clean.txt"] - if protected != (change != "none") { - t.Fatalf("protected=%v after %s", protected, change) - } - if _, ok := g.protected["unstaged.txt"]; !ok { - t.Fatal("lost original user edit protection") - } - if change == "none" { - writeSetupGitFile(t, root, "clean.txt", "continued setup work") - if err := g.AddNixInputs(ctx); err != nil { - t.Fatal(err) - } - if len(g.owned[".stack/new.nix"]) != 1 { - t.Fatal("partial Nix input was not visible on resume") - } - if err := g.Check(ctx); err != nil { - t.Fatal(err) - } - } - }) + saved.Pending = questions + if err := answerSetupQuestions(saved, &saved.Request, ui); err != nil || len(saved.Request.Answers) != 2 { + t.Fatalf("answered questions asked again: %+v %v", saved.Request.Answers, err) } } -func TestSetupResumeRestoresOptionsWithoutWeakeningPlan(t *testing.T) { +func TestSetupResumeRestoresOptionsOrStartsANewPlan(t *testing.T) { s := &setupManifest{Agent: "claude", Options: setupSavedOptions{Template: "minimal", With: []string{"go"}, NoRuntime: true}} cmd := &cobra.Command{} cmd.Flags().StringSlice("with", nil, "") cmd.Flags().Bool("no-runtime", false, "") opts := setupFlags{template: "default", experimentalAgent: "auto"} - if err := s.restoreOptions(cmd, &opts); err != nil { - t.Fatal(err) + if changed := s.restoreOptions(cmd, &opts); changed != "" { + t.Fatal(changed) } if opts.template != "minimal" || opts.experimentalAgent != "claude" || !opts.noRuntime || len(opts.with) != 1 { t.Fatalf("lost selections: %+v", opts) @@ -219,16 +171,16 @@ func TestSetupResumeRestoresOptionsWithoutWeakeningPlan(t *testing.T) { t.Fatal(err) } opts.with = []string{"typescript"} - if err := s.restoreOptions(cmd, &opts); err == nil || !strings.Contains(err.Error(), "--restart") { - t.Fatalf("changed contract accepted: %v", err) + if changed := s.restoreOptions(cmd, &opts); changed != "with" || opts.with[0] != "typescript" { + t.Fatalf("changed selection: %q %+v", changed, opts) } cmd.Flags().Lookup("with").Changed = false if err := cmd.Flags().Set("no-runtime", "false"); err != nil { t.Fatal(err) } opts.noRuntime = false - if err := s.restoreOptions(cmd, &opts); err != nil || opts.noRuntime { - t.Fatalf("could not enable runtime on retry: %v", err) + if changed := s.restoreOptions(cmd, &opts); changed != "" || opts.noRuntime { + t.Fatalf("could not enable runtime on retry: %q", changed) } } @@ -249,16 +201,16 @@ func TestSetupResumeRestoresPiModelAndConversation(t *testing.T) { cmd := &cobra.Command{} cmd.Flags().String("agent-model", "", "") opts := setupFlags{experimentalAgent: "auto"} - if err := loaded.restoreOptions(cmd, &opts); err != nil { - t.Fatal(err) + if changed := loaded.restoreOptions(cmd, &opts); changed != "" { + t.Fatal(changed) } if opts.experimentalAgent != "pi" || opts.agentModel != "openai/gpt-5" || len(loaded.Pi.Messages) != 1 { t.Fatal("Pi conversation or selection was lost on resume") } cmd.Flags().Set("agent-model", "openai/another-model") opts.agentModel = "openai/another-model" - if err := loaded.restoreOptions(cmd, &opts); err == nil { - t.Fatal("changed model accepted without restart") + if changed := loaded.restoreOptions(cmd, &opts); changed != "agent-model" { + t.Fatalf("changed model kept the saved conversation: %q", changed) } info, err := os.Stat(s.path) if err != nil || info.Mode().Perm() != 0600 { @@ -311,71 +263,3 @@ func TestSetupResumeStudioReceipt(t *testing.T) { t.Fatalf("reused receipt from another agent: %+v %v", replacement, err) } } - -func TestSetupResumeDoesNotForgetGitViolation(t *testing.T) { - setupStateTestEnvironment(t) - root := setupGitFixture(t) - ctx := context.Background() - g, err := captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - before, err := os.ReadFile(filepath.Join(root, "unstaged.txt")) - if err != nil { - t.Fatal(err) - } - writeSetupGitFile(t, root, "unstaged.txt", "agent overwrote user edits") - s := &setupManifest{Version: 1, Root: root, Stage: "inspection", path: setupStatePath(root), GitViolation: &setupGitCheckpoint{ - Root: g.root, Head: g.head, Index: g.index, Protected: g.protected, - }} - if err := s.save(); err != nil { - t.Fatal(err) - } - s, err = loadSetupManifest(root) - if err != nil { - t.Fatal(err) - } - if err := s.checkGitViolation(ctx); err == nil { - t.Fatal("retry accepted an unresolved Git violation") - } - writeSetupGitFile(t, root, "unstaged.txt", string(before)) - if err := s.checkGitViolation(ctx); err != nil || s.GitViolation != nil { - t.Fatalf("could not resume after restoring edits: %v", err) - } -} - -func TestAllowStackpanelConfigUnprotectsPreexistingScaffoldConfig(t *testing.T) { - setupStateTestEnvironment(t) - root := setupGitFixture(t) - ctx := context.Background() - - // Simulate running non-agent `stack setup` first, which leaves untracked - // .stack/config.nix, .stack/data.nix, and .stack/reconcile.json on disk. - writeSetupGitFile(t, root, ".stack/config.nix", "# starter template\n{\n ide.vscode.enable = true;\n}\n") - writeSetupGitFile(t, root, ".stack/data.nix", "{}\n") - writeSetupGitFile(t, root, ".stack/reconcile.json", "{\"version\":1}\n") - - g, err := captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - g.allowStackpanelConfig(root) - - for _, p := range []string{".stack/config.nix", ".stack/data.nix", ".stack/reconcile.json"} { - if _, protected := g.protected[p]; protected { - t.Fatalf("expected %s to be editable during agent setup, but it remained protected", p) - } - } - if _, protected := g.protected["unstaged.txt"]; !protected { - t.Fatal("unrelated user file unstaged.txt must remain protected") - } - - // Agent edits .stack/config.nix and AddNixInputs exposes it to Git-backed Nix eval. - writeSetupGitFile(t, root, ".stack/config.nix", "{ enable = true; }\n") - if err := g.AddNixInputs(ctx); err != nil { - t.Fatalf("AddNixInputs failed after editing .stack/config.nix: %v", err) - } - if len(g.owned[".stack/config.nix"]) != 1 { - t.Fatalf("expected .stack/config.nix to be staged with intent-to-add, got %+v", g.owned) - } -} diff --git a/apps/stackpanel-go/cmd/cli/setup_agent_test.go b/apps/stackpanel-go/cmd/cli/setup_agent_test.go index acdf85c7..6730e791 100644 --- a/apps/stackpanel-go/cmd/cli/setup_agent_test.go +++ b/apps/stackpanel-go/cmd/cli/setup_agent_test.go @@ -6,9 +6,7 @@ import ( "encoding/json" "io" "os" - "os/exec" "path/filepath" - "reflect" "strings" "testing" @@ -46,20 +44,6 @@ func TestResolveAgentSetupFlakeIgnoresConsumerRoot(t *testing.T) { } } -func TestRetainSetupChecksPreventsRemovalDuringRepair(t *testing.T) { - expected := reconcile.Expectations{Version: 1, RequiredChecks: []string{"declared"}} - report := &reconcile.Report{CheckResults: []reconcile.CheckResult{ - {ID: "declared", Status: "pass"}, - {ID: "failed", Status: "fail"}, - {ID: "skipped", Status: "skipped"}, - }} - retainSetupChecks(&expected, report) - retainSetupChecks(&expected, report) - if got := strings.Join(expected.RequiredChecks, ","); got != "declared,failed,skipped" { - t.Fatalf("repair contract lost checks or added duplicates: %s", got) - } -} - func TestPrepareSetupRequestRejectsDirtyLocalFramework(t *testing.T) { root := t.TempDir() bin := t.TempDir() @@ -110,52 +94,44 @@ fi func TestRunFreshDoctorIsolatesShellOutputAndPaths(t *testing.T) { root, executable := setupShellFixture(t) var output bytes.Buffer - report, err := runFreshDoctor(context.Background(), root, executable, "/expectations with spaces.json", &output) + report, err := runFreshDoctor(context.Background(), root, executable, &output, "--expectations", "/expectations with spaces.json") if err != nil { t.Fatal(err) } if report.HasErrors() || !strings.Contains(output.String(), "hook stdout noise") { t.Fatalf("report/log isolation failed: %+v, %s", report, output.String()) } -} - -func TestRunFreshDoctorRejectsFalseSuccess(t *testing.T) { - for _, test := range []struct { - name string - edit func(*reconcile.Report) - }{ - {"missing coverage", func(r *reconcile.Report) { r.Coverage = nil }}, - {"missing verification", func(r *reconcile.Report) { r.Reconcilers = r.Reconcilers[:4] }}, - {"pending changes", func(r *reconcile.Report) { - r.Changes = []reconcile.Change{{Reconciler: "files", Kind: reconcile.ChangeCreate, Path: "missing.json"}} - }}, - {"skipped check", func(r *reconcile.Report) { - r.CheckResults = []reconcile.CheckResult{{ID: "missing", Scope: "repo", Status: "skipped"}} - }}, - } { - t.Run(test.name, func(t *testing.T) { - root, executable := setupShellFixture(t) - report := completeSetupReport() - test.edit(&report) - data, err := json.Marshal(report) - if err != nil { - t.Fatal(err) - } - t.Setenv("TEST_DOCTOR_JSON", string(data)) - if _, err := runFreshDoctor(context.Background(), root, executable, "/expectations.json", io.Discard); err == nil { - t.Fatal("accepted incomplete report from a zero-exit process") - } - }) + // Doctor exits nonzero when it finds problems; they are in the report. + failing := completeSetupReport() + failing.Findings = []reconcile.Finding{{Reconciler: "verification", ID: "config:apps.web", Severity: reconcile.SeverityError, Title: "apps.web is missing"}} + data, _ := json.Marshal(failing) + t.Setenv("TEST_DOCTOR_JSON", string(data)) + t.Setenv("TEST_DOCTOR_EXIT", "1") + if report, err = runFreshDoctor(context.Background(), root, executable, io.Discard, "--expectations", "/expectations.json"); err != nil || len(doctorProblems(report)) != 1 { + t.Fatalf("lost the report of a failing doctor: %+v %v", report, err) + } + t.Setenv("TEST_DOCTOR_JSON", "") + if _, err := runFreshDoctor(context.Background(), root, executable, io.Discard, "--expectations", "/expectations.json"); err == nil { + t.Fatal("accepted a doctor run without a report") } } -func TestFreshReconciliationRequiresLock(t *testing.T) { - root, executable := setupShellFixture(t) - if err := os.Remove(filepath.Join(root, "flake.lock")); err != nil { - t.Fatal(err) - } - if err := runFreshReconciliation(context.Background(), root, executable, io.Discard); err == nil || !strings.Contains(err.Error(), "flake.lock") { - t.Fatalf("expected missing-lock error, got %v", err) +func TestDoctorProblemsLeaveOutSkippedChecks(t *testing.T) { + report := &reconcile.Report{ + CheckResults: []reconcile.CheckResult{{ID: "needs-runtime", Status: "skipped"}, {ID: "build", Status: "fail"}}, + Findings: []reconcile.Finding{ + {ID: "strict:check:needs-runtime", Severity: reconcile.SeverityError, Title: "needs-runtime: check did not pass (skipped)"}, + {ID: "check:needs-runtime", Severity: reconcile.SeverityError, Title: "needs-runtime: required check did not pass"}, + {ID: "strict:check:build", Severity: reconcile.SeverityError, Title: "build: check did not pass (fail)", Detail: "exit 1"}, + {ID: "config:apps.web", Severity: reconcile.SeverityError, Title: "apps.web: expected exists=true, got false"}, + {ID: "editorconfig", Severity: reconcile.SeverityWarning, Title: "optional"}, + }, + Errors: []reconcile.ReconcilerError{{Reconciler: "files", Err: "manifest unreadable"}}, + } + got := strings.Join(doctorProblems(report), "\n") + want := "files: manifest unreadable\nbuild: check did not pass (fail): exit 1\napps.web: expected exists=true, got false" + if got != want { + t.Fatalf("problems = %q; want %q", got, want) } } @@ -198,101 +174,6 @@ func TestSetupPrepareRunsPlannedCommandInsideRepository(t *testing.T) { } } -func TestSetupLockPreservesGitAndUserEdits(t *testing.T) { - for _, mode := range []string{"new", "unchanged", "conflicting"} { - t.Run(mode, func(t *testing.T) { - root := setupGitFixture(t) - lock := filepath.Join(root, "flake.lock") - original := []byte("user lock\n") - if mode != "new" { - writeSetupGitFile(t, root, "flake.lock", string(original)) - } - ctx := context.Background() - guard, err := captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - bin := t.TempDir() - script := `#!/bin/sh -test "$1 $2 $3 $4" = 'flake lock . --output-lock-file' || exit 1 -case "$5" in "$PWD"/*) echo 'output is inside the repository' >&2; exit 1 ;; esac -test -z "${STACKPANEL_ROOT:-}" || exit 1 -printf '%s\n' "$TEST_LOCK_CONTENT" > "$5" -` - if err := os.WriteFile(filepath.Join(bin, "nix"), []byte(script), 0o755); err != nil { - t.Fatal(err) - } - t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) - t.Setenv("TEST_LOCK_CONTENT", "resolved lock") - if mode == "unchanged" { - t.Setenv("TEST_LOCK_CONTENT", "user lock") - } - err = runSetupLock(ctx, root, t.TempDir(), guard, io.Discard) - if mode == "conflicting" { - if err == nil || !strings.Contains(err.Error(), "preexisting user edits") { - t.Fatalf("expected protected lock error: %v", err) - } - } else if err != nil { - t.Fatal(err) - } - if mode != "new" { - data, err := os.ReadFile(lock) - if err != nil || !bytes.Equal(data, original) { - t.Fatalf("changed user lock: %q %v", data, err) - } - } - if err := guard.Check(ctx); err != nil { - t.Fatal(err) - } - }) - } -} - -func TestSetupLockWithRealNix(t *testing.T) { - if os.Getenv("STACKPANEL_TEST_REAL_NIX") != "1" { - t.Skip("set STACKPANEL_TEST_REAL_NIX=1 for the Nix daemon integration test") - } - if _, err := exec.LookPath("nix"); err != nil { - t.Fatal(err) - } - root := t.TempDir() - setupGitTestRun(t, root, "init", "-q") - ctx := context.Background() - guard, err := captureSetupGit(ctx, root) - if err != nil { - t.Fatal(err) - } - // No network or registry needed: exercise the actual daemon and Git-backed - // flake machinery with a relative local input. - writeSetupGitFile(t, root, "dependency/flake.nix", "{ outputs = _: {}; }\n") - writeSetupGitFile(t, root, "flake.nix", "{ inputs.test.url = \"path:./dependency\"; outputs = _: {}; }\n") - if err := guard.AddNixInputs(ctx); err != nil { - t.Fatal(err) - } - before, err := setupReadIndex(ctx, root) - if err != nil { - t.Fatal(err) - } - var diagnostic bytes.Buffer - if err := runSetupLock(ctx, root, t.TempDir(), guard, &diagnostic); err != nil { - t.Fatalf("host lock: %v\n%s", err, diagnostic.String()) - } - after, err := setupReadIndex(ctx, root) - if err != nil || !reflect.DeepEqual(before, after) { - t.Fatalf("Nix changed the index: %v", err) - } - data, err := os.ReadFile(filepath.Join(root, "flake.lock")) - if err != nil || !json.Valid(data) { - t.Fatalf("missing real lock: %s %v", data, err) - } - if err := guard.AddNixInputs(ctx); err != nil { - t.Fatal(err) - } - if _, owned := guard.owned["flake.lock"]; !owned { - t.Fatal("host did not retain visibility of the generated lock") - } -} - func TestExperimentalSetupRejectsPartialVerification(t *testing.T) { for _, opts := range []setupFlags{ {experimentalAgent: "auto", only: []string{"files"}}, @@ -342,9 +223,6 @@ func setupShellFixture(t *testing.T) (string, string) { if err := os.Mkdir(root, 0o755); err != nil { t.Fatal(err) } - if err := os.WriteFile(filepath.Join(root, "flake.lock"), []byte("{}"), 0o644); err != nil { - t.Fatal(err) - } write := func(path, contents string) { t.Helper() if err := os.WriteFile(path, []byte(contents), 0o755); err != nil { @@ -355,9 +233,7 @@ func setupShellFixture(t *testing.T) (string, string) { set -eu test "$1" = develop test "$2" = . -test "$3" = --no-update-lock-file -test "$4" = --no-write-lock-file -test "$5" = --command +test "$3" = --command test -z "${STACKPANEL_ROOT+x}" test -z "${__STACKPANEL_HOOK_RAN+x}" test -z "${DIRENV_DIR+x}" @@ -366,20 +242,16 @@ if test "${TEST_NIX_FAILURE:-}" = 1; then printf '%s\n' 'fixture evaluation failure' >&2 exit 1 fi -shift 5 +shift 3 exec "$@" `) executable := filepath.Join(dir, "stack 'quoted' $tool") write(executable, `#!/bin/sh set -eu if test "$1" = doctor; then - test "$2" = --strict - test "$3" = --scope - test "$4" = repo,build - test "$5" = --build - test "$6" = --expectations - test "$8" = --json + test "$2" = --expectations printf '%s\n' "$TEST_DOCTOR_JSON" + exit "${TEST_DOCTOR_EXIT:-0}" fi `) data, err := json.Marshal(completeSetupReport()) diff --git a/apps/stackpanel-go/internal/setupagent/agent.go b/apps/stackpanel-go/internal/setupagent/agent.go index ce202216..d51df8ef 100644 --- a/apps/stackpanel-go/internal/setupagent/agent.go +++ b/apps/stackpanel-go/internal/setupagent/agent.go @@ -1,12 +1,11 @@ -// Package setupagent runs an embedded or installed agent for repository onboarding. -// An agent's completion is never evidence that onboarding passed verification. +// Package setupagent runs a coding agent for repository onboarding. The agent +// edits the repository; the host verifies the result with stack doctor. package setupagent import ( "bytes" "context" "encoding/json" - "errors" "fmt" "io" "os/exec" @@ -17,8 +16,8 @@ import ( ) const ( - probeTimeout = 5 * time.Second - defaultRunTimeout = 15 * time.Minute + probeTimeout = 10 * time.Second + defaultRunTimeout = 45 * time.Minute maxMessageBytes = 1 << 20 ) @@ -28,31 +27,25 @@ type Agent struct { pi *piBackend } -type Capabilities struct { - ReadOnly bool `json:"readOnly"` - Write bool `json:"write"` - Reason string `json:"reason,omitempty"` -} - type RunRequest struct { - Dir string - Prompt string + Dir string + Prompt string + // ReadOnly runs an inspection turn, which must not change files. ReadOnly bool Stdout io.Writer Stderr io.Writer OnEvent func(Event) // Env defaults to the current process environment. Authentication and model // selection remain the installed CLI's responsibility. - Env []string - Timeout time.Duration - ProtectedPaths []string - PiState *PiState - SavePiState func() error + Env []string + Timeout time.Duration + PiState *PiState + SavePiState func() error } // Event is provider-independent progress. Provider JSON stays in the debug log. type Event struct { - Kind string + Kind string // tool, warning, or activity Text string } @@ -61,48 +54,28 @@ type RunResult struct { Message string } -// Discover resolves known executables on the caller's PATH before a target -// repository's shell can replace it. It does not execute or authenticate them. +// Discover resolves supported agent CLIs on the caller's PATH, before a target +// repository's shell can change it. It does not execute or authenticate them. func Discover() []Agent { var agents []Agent - for _, id := range []string{"codex", "claude", "opencode"} { + for _, id := range []string{"codex", "claude"} { path, err := exec.LookPath(id) if err != nil { continue } - path, err = filepath.Abs(path) - if err == nil { + if path, err = filepath.Abs(path); err == nil { agents = append(agents, Agent{ID: id, Path: path}) } } return agents } -// Probe checks the installed command's flags without making a model request. -// Installation and suitable flags do not establish authentication readiness. -func Probe(ctx context.Context, agent Agent) (Capabilities, error) { - if !filepath.IsAbs(agent.Path) { - return Capabilities{}, fmt.Errorf("agent executable must have an absolute path") - } - var args, required []string - switch agent.ID { - case "codex": - args = []string{"exec", "--help"} - required = []string{"--json", "--sandbox", "--config", "--color", "read-only", "workspace-write"} - // Claude hides SDK-oriented flags such as --tools and --verbose from - // some help variants. Invocation still passes them and fails closed if - // unsupported; help omission is not evidence of incompatibility. - case "claude": - args = []string{"--help"} - required = []string{"--print", "--output-format", "stream-json", "--permission-mode", "plan", "acceptEdits", "--strict-mcp-config", "--mcp-config", "--settings"} - case "opencode": - return Capabilities{Reason: "OpenCode is installed, but this experiment cannot enforce read-only inspection with its configurable agents; select codex or claude"}, nil - default: - return Capabilities{}, fmt.Errorf("unsupported setup agent %q", agent.ID) - } +// Probe checks that the installed CLI runs. It makes no model request, so it +// does not establish that the CLI is signed in. +func Probe(ctx context.Context, agent Agent) error { ctx, cancel := context.WithTimeout(ctx, probeTimeout) defer cancel() - cmd := exec.CommandContext(ctx, agent.Path, args...) + cmd := exec.CommandContext(ctx, agent.Path, "--version") configureProcess(cmd) output := &limitedBuffer{} cmd.Stdout, cmd.Stderr = output, output @@ -110,17 +83,9 @@ func Probe(ctx context.Context, agent Agent) (Capabilities, error) { if ctx.Err() != nil { err = ctx.Err() } - return Capabilities{}, fmt.Errorf("probe %s: %w", agent.ID, err) + return fmt.Errorf("%s --version: %w %s", agent.ID, err, strings.TrimSpace(output.String())) } - if output.truncated { - return Capabilities{}, fmt.Errorf("probe %s: help output exceeded limit", agent.ID) - } - for _, flag := range required { - if !strings.Contains(output.String(), flag) { - return Capabilities{Reason: fmt.Sprintf("installed %s does not advertise required capability %s", agent.ID, flag)}, nil - } - } - return Capabilities{ReadOnly: true, Write: true}, nil + return nil } func commandFor(agent Agent, req RunRequest) ([]string, error) { @@ -130,8 +95,8 @@ func commandFor(agent Agent, req RunRequest) ([]string, error) { if req.ReadOnly { sandbox = "read-only" } - // Overrides replace these tables for this invocation. Keep user auth and - // model configuration, while disabling integrations outside the sandbox. + // Keep the user's auth and model configuration, but leave out + // integrations that would act outside the repository. return []string{ "exec", "--json", "--sandbox", sandbox, "--color", "never", "-c", `approval_policy="never"`, "-c", "mcp_servers={}", @@ -141,16 +106,15 @@ func commandFor(agent Agent, req RunRequest) ([]string, error) { "-c", "features.skill_mcp_dependency_install=false", "-", }, nil case "claude": - // All commands (including Nix and acceptance tests) belong to the host. - // Native file tools also avoid a shell fallback after a denied file read. - mode, allowedTools := "acceptEdits", "Read,Glob,Grep,Edit,Write" + // Native file tools need no shell. The host runs Nix, installs, and checks. + mode, tools := "acceptEdits", "Read,Glob,Grep,Edit,Write" if req.ReadOnly { - mode, allowedTools = "plan", "Read,Glob,Grep" + mode, tools = "plan", "Read,Glob,Grep" } return []string{ "--print", "--output-format", "stream-json", "--verbose", "--permission-mode", mode, - "--tools", allowedTools, "--strict-mcp-config", "--mcp-config", `{"mcpServers":{}}`, + "--tools", tools, "--strict-mcp-config", "--mcp-config", `{"mcpServers":{}}`, "--settings", `{"disableAllHooks":true}`, }, nil default: @@ -158,18 +122,15 @@ func commandFor(agent Agent, req RunRequest) ([]string, error) { } } -// Run streams JSON events without retaining the transcript. It requires a -// provider completion event, rejects provider and mutating-tool permission errors, -// and bounds execution time and individual event size. It does not approve shell commands -// denied by the CLI's configured policy. +// Run executes one agent turn and returns its final message. Only a turn that +// failed outright is an error: progress noise, provider warnings, and denied +// tool requests are reported as events, and the reply is parsed leniently by +// ParseReply. func Run(ctx context.Context, agent Agent, req RunRequest) (RunResult, error) { if agent.ID == "pi" { return runPi(ctx, agent.pi, req) } result := RunResult{ExitCode: -1} - if !filepath.IsAbs(agent.Path) || !filepath.IsAbs(req.Dir) { - return result, fmt.Errorf("agent executable and repository directory must be absolute") - } args, err := commandFor(agent, req) if err != nil { return result, err @@ -185,8 +146,8 @@ func Run(ctx context.Context, agent Agent, req RunRequest) (RunResult, error) { cmd.Dir = req.Dir cmd.Env = agentCommandEnv(agent, req.Env) cmd.Stdin = strings.NewReader(req.Prompt) - // Stdout and stderr frequently share a terminal or a caller's buffer. - // os/exec copies them concurrently because stdout also parses events. + // Stdout and stderr often share a terminal or a caller's buffer, and + // os/exec copies them concurrently. var streamMu sync.Mutex events := &eventWriter{provider: agent.ID, out: &lockedWriter{mu: &streamMu, out: req.Stdout}, onEvent: req.OnEvent} cmd.Stdout = events @@ -198,30 +159,19 @@ func Run(ctx context.Context, agent Agent, req RunRequest) (RunResult, error) { if cmd.ProcessState != nil { result.ExitCode = cmd.ProcessState.ExitCode() } - if ctx.Err() != nil { - return result, fmt.Errorf("%s setup agent: %w", agent.ID, ctx.Err()) - } - if events.err != nil { - return result, fmt.Errorf("%s setup agent: %w", agent.ID, events.err) - } - if err != nil { + switch { + case ctx.Err() != nil: + return result, ctx.Err() + case events.err != nil: + return result, events.err + case err != nil: diagnostic := strings.TrimSpace(stderr.String()) if len(diagnostic) > 2000 { diagnostic = diagnostic[len(diagnostic)-2000:] } - return result, fmt.Errorf("%s setup agent exited unsuccessfully: %w: %s", agent.ID, err, diagnostic) - } - if !events.completed || strings.TrimSpace(events.message) == "" { - return result, fmt.Errorf("%s setup agent exited without a complete result", agent.ID) - } - if !req.ReadOnly { - reply, err := ParseReply(events.message) - if err != nil { - return result, err - } - if reply.Status != "complete" && reply.Status != "needs_input" { - return result, fmt.Errorf("%s setup agent did not complete (%s): %s", agent.ID, reply.Status, reply.Summary) - } + return result, fmt.Errorf("exited unsuccessfully: %w: %s", err, diagnostic) + case strings.TrimSpace(result.Message) == "": + return result, fmt.Errorf("finished without a reply") } return result, nil } @@ -230,9 +180,8 @@ func agentCommandEnv(agent Agent, env []string) []string { if agent.ID != "codex" || len(env) == 0 { return env } - // Codex spawns $SHELL -lc for shell commands. Interactive zsh startup files - // and zsh heredocs attempt to create temp/history files, which fails inside - // a read-only sandbox. Prefer /bin/sh for deterministic POSIX execution. + // Codex runs shell commands with $SHELL -lc. zsh startup files and heredocs + // create temporary files, which fails in the read-only inspection sandbox. out := make([]string, 0, len(env)+1) for _, kv := range env { if strings.HasPrefix(kv, "SHELL=") || strings.HasPrefix(kv, "ZDOTDIR=") { @@ -257,34 +206,29 @@ func (w *lockedWriter) Write(p []byte) (int, error) { return w.out.Write(p) } -// limitedBuffer bounds probes. A large CLI response is consumed but is never -// mistaken for a complete, parseable response. +// limitedBuffer keeps at most maxMessageBytes of output. type limitedBuffer struct { bytes.Buffer - truncated bool } func (b *limitedBuffer) Write(p []byte) (int, error) { n := len(p) - available := maxMessageBytes - b.Len() - if len(p) > available { - b.truncated = true + if available := maxMessageBytes - b.Len(); len(p) > available { p = p[:available] } _, _ = b.Buffer.Write(p) return n, nil } -// eventWriter parses one JSONL event at a time. Only the final message and first -// error survive, so a verbose long-running agent cannot exhaust memory. +// eventWriter parses the provider's JSONL stream one line at a time and keeps +// only the latest reply, so a verbose agent cannot exhaust memory. type eventWriter struct { - provider string - out io.Writer - pending []byte - message string - completed bool - err error - onEvent func(Event) + provider string + out io.Writer + pending []byte + message string + err error + onEvent func(Event) } func (w *eventWriter) Write(p []byte) (int, error) { @@ -319,10 +263,13 @@ func (w *eventWriter) finish() { } } -func (w *eventWriter) consume(line []byte) { - if len(bytes.TrimSpace(line)) == 0 || w.err != nil { - return +func (w *eventWriter) emit(kind, text string) { + if w.onEvent != nil && text != "" { + w.onEvent(Event{Kind: kind, Text: text}) } +} + +func (w *eventWriter) consume(line []byte) { var event struct { Type string `json:"type"` Subtype string `json:"subtype"` @@ -337,19 +284,29 @@ func (w *eventWriter) consume(line []byte) { Command string `json:"command"` } `json:"item"` } - if err := json.Unmarshal(line, &event); err != nil { - w.err = fmt.Errorf("invalid agent event: %w", err) - return - } - if event.Type == "error" || event.Type == "turn.failed" || event.IsError { - w.err = fmt.Errorf("provider reported failure: %s %s %s", event.Result, event.Message, event.Error) - return + if w.err != nil || json.Unmarshal(line, &event) != nil { + return // Not an event, such as a CLI notice. It remains in the debug log. } - if w.onEvent != nil { - if event.Item.Type == "command_execution" && event.Type == "item.started" { - w.onEvent(Event{Kind: "tool", Text: event.Item.Command}) + switch w.provider { + case "codex": + switch event.Type { + case "item.started": + if event.Item.Type == "command_execution" { + w.emit("tool", event.Item.Command) + } + case "item.completed": + if event.Item.Type == "agent_message" { + w.message = event.Item.Text + } + case "error": + // Codex also reports retried stream errors this way. + w.emit("warning", "codex: "+rawText(event.Message)) + case "turn.failed": + w.err = fmt.Errorf("turn failed: %s", rawText(event.Error)) } - if event.Type == "assistant" { + case "claude": + switch event.Type { + case "assistant": var msg struct { Content []struct { Type string `json:"type"` @@ -359,78 +316,73 @@ func (w *eventWriter) consume(line []byte) { if json.Unmarshal(event.Message, &msg) == nil { for _, part := range msg.Content { if part.Type == "tool_use" { - w.onEvent(Event{Kind: "tool", Text: part.Name}) + w.emit("tool", part.Name) } } } - } - } - switch w.provider { - case "codex": - if event.Type == "item.completed" && event.Item.Type == "agent_message" { - w.message = event.Item.Text - } - if event.Type == "turn.completed" { - w.completed = true - } - case "claude": - if event.Type == "result" { - if event.Subtype != "success" { - w.err = fmt.Errorf("provider result was %q", event.Subtype) + case "result": + if event.IsError { + w.err = fmt.Errorf("claude reported an error (%s): %s", event.Subtype, event.Result) return } - if err := w.claudePermissionDenials(event.PermissionDenials); err != nil { - w.err = err - return + if event.Subtype != "success" { + w.emit("warning", "claude ended its turn early: "+event.Subtype) + } + for _, denial := range event.PermissionDenials { + w.emit("warning", claudeDenial(denial)) } - w.message, w.completed = event.Result, true + w.message = event.Result } } } -// Claude can include earlier denied permission requests in a successful result, -// even if it completed the work using the context it already had. Denied reads stay denied; -// they are diagnostics, not evidence that the agreed outputs failed doctor. -// Denied mutations and unknown tool types still stop the run. -func (w *eventWriter) claudePermissionDenials(denials []json.RawMessage) error { - var warnings []string - for _, raw := range denials { - var denial struct { - ToolName string `json:"tool_name"` - ToolInput struct { - FilePath string `json:"file_path"` - Path string `json:"path"` - Pattern string `json:"pattern"` - } `json:"tool_input"` +// rawText renders an event's message or error field, which providers send as +// a string or as an object with a message. +func rawText(raw json.RawMessage) string { + var text string + if json.Unmarshal(raw, &text) == nil { + return text + } + var object struct { + Message string `json:"message"` + } + if json.Unmarshal(raw, &object) == nil && object.Message != "" { + return object.Message + } + return string(raw) +} + +// Claude reports tool requests it refused, even when the turn succeeded. They +// are worth knowing about but do not mean the setup failed; doctor decides that. +// Only file paths are shown: command inputs can contain anything. +func claudeDenial(raw json.RawMessage) string { + var denial struct { + ToolName string `json:"tool_name"` + ToolInput struct { + FilePath string `json:"file_path"` + Path string `json:"path"` + Pattern string `json:"pattern"` + } `json:"tool_input"` + } + _ = json.Unmarshal(raw, &denial) + detail := denial.ToolName + if detail == "" { + detail = "a tool" + } + if denial.ToolName == "Read" || denial.ToolName == "Glob" || denial.ToolName == "Grep" { + path := denial.ToolInput.FilePath + if path == "" { + path = denial.ToolInput.Path } - if err := json.Unmarshal(raw, &denial); err != nil || denial.ToolName == "" { - return errors.New("Claude reported a permission denial for an unrecognized tool; inspect --agent-log diagnostics") + if path == "" && denial.ToolName == "Glob" { + path = denial.ToolInput.Pattern } - switch denial.ToolName { - case "Read", "Glob", "Grep": - path := denial.ToolInput.FilePath - if path == "" { - path = denial.ToolInput.Path - } - if path == "" && denial.ToolName == "Glob" { - path = denial.ToolInput.Pattern - } - if len(path) > 300 { - path = path[:300] + "…" - } - detail := denial.ToolName - if path != "" { - detail += fmt.Sprintf(" (%q)", path) - } - warnings = append(warnings, "Claude read permission denied: "+detail+". Continuing with available context; doctor still verifies the agreed outputs.") - default: - return fmt.Errorf("agent requested permissions that were denied for tool %q; the request was not executed. Review this tool's CLI permission rules and retry (--agent-log records diagnostics)", denial.ToolName) + if len(path) > 300 { + path = path[:300] + "…" } - } - if w.onEvent != nil { - for _, warning := range warnings { - w.onEvent(Event{Kind: "warning", Text: warning}) + if path != "" { + detail += fmt.Sprintf(" (%q)", path) } } - return nil + return "Claude was denied " + detail + " and continued without it." } diff --git a/apps/stackpanel-go/internal/setupagent/agent_test.go b/apps/stackpanel-go/internal/setupagent/agent_test.go index 801e5051..98f5be2b 100644 --- a/apps/stackpanel-go/internal/setupagent/agent_test.go +++ b/apps/stackpanel-go/internal/setupagent/agent_test.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "errors" - "fmt" "io" "os" "os/exec" @@ -19,7 +18,6 @@ import ( const ( validPlan = `{"summary":"Add web app","expectations":{"version":1,"config":[{"path":["apps","web"],"exists":true}],"requiredChecks":[]}}` complete = `{"status":"complete","summary":"Configured web"}` - helpText = "--print --output-format stream-json --verbose --permission-mode plan acceptEdits --permission-prompts --tools --strict-mcp-config --mcp-config --settings --json --sandbox --config --color read-only workspace-write" ) // TestAgentProcess is an installed CLI stand-in. It never contacts a provider. @@ -37,11 +35,12 @@ func TestAgentProcess(t *testing.T) { break } } - for _, arg := range args { - if arg == "--help" { - _, _ = io.WriteString(os.Stdout, os.Getenv("STACKPANEL_TEST_AGENT_HELP")) - os.Exit(0) + if len(args) == 1 && args[0] == "--version" { + _, _ = io.WriteString(os.Stdout, "fixture-cli 1.0.0\n") + if os.Getenv("STACKPANEL_TEST_AGENT_FAIL") == "1" { + os.Exit(7) } + os.Exit(0) } input, _ := io.ReadAll(os.Stdin) if path := os.Getenv("STACKPANEL_TEST_AGENT_RECORD"); path != "" { @@ -85,7 +84,6 @@ func fakeAgent(t *testing.T, id string) Agent { t.Fatal(err) } t.Setenv("STACKPANEL_TEST_AGENT_PROCESS", "1") - t.Setenv("STACKPANEL_TEST_AGENT_HELP", helpText) return Agent{ID: id, Path: path} } @@ -112,34 +110,20 @@ func TestDiscoverResolvesOnlyKnownExecutables(t *testing.T) { } } -func TestProbeChecksCapabilitiesWithoutRunningAgent(t *testing.T) { - for _, id := range []string{"codex", "claude"} { - t.Run(id, func(t *testing.T) { - agent := fakeAgent(t, id) - caps, err := Probe(context.Background(), agent) - if err != nil || !caps.ReadOnly || !caps.Write { - t.Fatalf("Probe = %+v, %v", caps, err) - } - t.Setenv("STACKPANEL_TEST_AGENT_HELP", "old CLI --help") - caps, err = Probe(context.Background(), agent) - if err != nil || caps.ReadOnly || caps.Write || caps.Reason == "" { - t.Fatalf("old CLI Probe = %+v, %v", caps, err) - } - }) +func TestProbeRunsTheInstalledCLI(t *testing.T) { + agent := fakeAgent(t, "claude") + if err := Probe(context.Background(), agent); err != nil { + t.Fatal(err) } - caps, err := Probe(context.Background(), Agent{ID: "opencode", Path: "/unused/opencode"}) - if err != nil || caps.ReadOnly || caps.Write || caps.Reason == "" { - t.Fatalf("OpenCode Probe = %+v, %v", caps, err) + t.Setenv("STACKPANEL_TEST_AGENT_FAIL", "1") + if err := Probe(context.Background(), agent); err == nil || !strings.Contains(err.Error(), "fixture-cli") { + t.Fatalf("broken CLI accepted or its output dropped: %v", err) } -} - -func TestProbeHonorsCancellation(t *testing.T) { - agent := fakeAgent(t, "codex") + t.Setenv("STACKPANEL_TEST_AGENT_FAIL", "") t.Setenv("STACKPANEL_TEST_AGENT_DELAY", "1") ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) defer cancel() - _, err := Probe(ctx, agent) - if !errors.Is(err, context.DeadlineExceeded) { + if err := Probe(ctx, agent); !errors.Is(err, context.DeadlineExceeded) { t.Fatalf("Probe error = %v", err) } } @@ -157,31 +141,27 @@ func TestRunSerializesSharedOutputWriter(t *testing.T) { } } -func TestRunPassesLiteralPromptAndStreamsCompletedResult(t *testing.T) { +func TestRunPassesLiteralPromptAndStreamsResult(t *testing.T) { for _, id := range []string{"codex", "claude"} { for _, readOnly := range []bool{true, false} { t.Run(id+map[bool]string{true: "/inspect", false: "/write"}[readOnly], func(t *testing.T) { agent := fakeAgent(t, id) root := t.TempDir() record := filepath.Join(root, "invocation.json") - message := complete - if readOnly { - message = validPlan - } - events := codexEvents(message) + events := codexEvents(complete) if id == "claude" { - events = claudeEvents(message) + events = claudeEvents(complete) } t.Setenv("STACKPANEL_TEST_AGENT_EVENTS", events) t.Setenv("STACKPANEL_TEST_AGENT_RECORD", record) var output bytes.Buffer prompt := "literal `touch broken` $(touch broken)\n'quoted' \"text\"" result, err := Run(context.Background(), agent, RunRequest{Dir: root, Prompt: prompt, ReadOnly: readOnly, Stdout: &output, Env: os.Environ()}) - if err != nil || result.ExitCode != 0 || result.Message != message { + if err != nil || result.ExitCode != 0 || result.Message != complete { t.Fatalf("Run = %+v, %v", result, err) } if output.String() != events { - t.Fatal("provider progress was not streamed") + t.Fatal("provider output was not streamed") } data, err := os.ReadFile(record) if err != nil { @@ -197,27 +177,19 @@ func TestRunPassesLiteralPromptAndStreamsCompletedResult(t *testing.T) { if invocation.Input != prompt || invocation.Dir != root { t.Fatalf("wrong input or directory: %+v", invocation) } - for _, arg := range invocation.Args { - if strings.Contains(arg, "bypass") || strings.Contains(arg, "skip-permissions") || arg == "--model" || arg == prompt { - t.Fatalf("unsafe/overriding argument: %q", arg) - } - } joined := strings.Join(invocation.Args, " ") + if strings.Contains(joined, "bypass") || strings.Contains(joined, "skip-permissions") || strings.Contains(joined, prompt) { + t.Fatalf("unsafe argument: %s", joined) + } if id == "codex" { - wantSandbox := "workspace-write" - if readOnly { - wantSandbox = "read-only" - } - if !strings.Contains(joined, "--sandbox "+wantSandbox) || !strings.Contains(joined, "mcp_servers={}") { - t.Fatalf("missing sandbox/integration constraints: %s", joined) + sandbox := map[bool]string{true: "read-only", false: "workspace-write"}[readOnly] + if !strings.Contains(joined, "--sandbox "+sandbox) || !strings.Contains(joined, "mcp_servers={}") { + t.Fatalf("wrong sandbox or integrations: %s", joined) } } else { - tools := "Read,Glob,Grep,Edit,Write" - if readOnly { - tools = "Read,Glob,Grep" - } - if strings.Contains(joined, "--permission-prompts") || !strings.Contains(joined, "--tools "+tools+" --strict-mcp-config") { - t.Fatalf("missing native-file permission constraints: %s", joined) + tools := map[bool]string{true: "Read,Glob,Grep", false: "Read,Glob,Grep,Edit,Write"}[readOnly] + if !strings.Contains(joined, "--tools "+tools+" ") { + t.Fatalf("wrong tools: %s", joined) } } }) @@ -225,44 +197,67 @@ func TestRunPassesLiteralPromptAndStreamsCompletedResult(t *testing.T) { } } -func TestRunRejectsIncompleteAndFailedProviderResults(t *testing.T) { - tests := []struct { - name, provider, events, want string +func TestRunToleratesNoiseWarningsAndDenials(t *testing.T) { + denials := `[{"tool_name":"Glob","tool_input":{"pattern":"/nix/store/*/**/*.nix"}},{"tool_name":"Bash","tool_input":{"command":"private-command-input"}}]` + for _, tt := range []struct { + name, provider, events, warning string }{ - {"no completion", "codex", `{"type":"thread.started"}` + "\n", "without a complete result"}, - {"turn failed", "codex", "{\"type\":\"turn.failed\",\"error\":{\"message\":\"login required\"}}\n", "provider reported failure"}, - {"error despite exit zero", "codex", "{\"type\":\"error\",\"message\":\"permission denied\"}\n" + codexEvents(complete), "provider reported failure"}, - {"blocked", "codex", codexEvents(`{"status":"blocked","summary":"permission required"}`), "did not complete"}, - {"unstructured success", "codex", codexEvents("I am done"), "invalid setup reply"}, - {"invalid event", "codex", "not json\n", "invalid agent event"}, - {"claude denied", "claude", "{\"type\":\"result\",\"subtype\":\"success\",\"result\":\"done\",\"permission_denials\":[{\"tool_name\":\"Bash\"}]}\n", "permissions that were denied"}, - {"claude incomplete", "claude", "{\"type\":\"result\",\"subtype\":\"error_max_turns\"}\n", "provider result was"}, - {"claude diagnostic", "claude", "{\"type\":\"result\",\"is_error\":true,\"result\":\"Login required: run claude auth login\"}\n", "Login required: run claude auth login"}, + {"codex", "codex", "Reading prompt from stdin...\n" + + `{"type":"error","message":"Reconnecting... 1/5"}` + "\n" + codexEvents(complete), "Reconnecting"}, + {"claude denials", "claude", `{"type":"result","subtype":"success","result":` + jsonString(complete) + `,"permission_denials":` + denials + "}\n", "denied Bash"}, + {"claude turn limit", "claude", `{"type":"result","subtype":"error_max_turns","result":` + jsonString(complete) + "}\n", "error_max_turns"}, + } { + t.Run(tt.name, func(t *testing.T) { + t.Setenv("STACKPANEL_TEST_AGENT_EVENTS", tt.events) + var warnings []string + result, err := Run(context.Background(), fakeAgent(t, tt.provider), RunRequest{Dir: t.TempDir(), OnEvent: func(e Event) { + if e.Kind == "warning" { + warnings = append(warnings, e.Text) + } + }}) + if err != nil || result.Message != complete { + t.Fatalf("Run = %+v, %v", result, err) + } + joined := strings.Join(warnings, "\n") + if !strings.Contains(joined, tt.warning) || strings.Contains(joined, "private-command-input") { + t.Fatalf("warnings = %q", joined) + } + }) } - for _, tt := range tests { +} + +func jsonString(s string) string { + data, _ := json.Marshal(s) + return string(data) +} + +func TestRunReportsFailedTurns(t *testing.T) { + for _, tt := range []struct { + name, provider, events, want string + }{ + {"no reply", "codex", `{"type":"thread.started"}` + "\n", "without a reply"}, + {"turn failed", "codex", `{"type":"turn.failed","error":{"message":"login required"}}` + "\n", "login required"}, + {"claude error", "claude", `{"type":"result","is_error":true,"result":"Login required: run claude auth login"}` + "\n", "Login required"}, + } { t.Run(tt.name, func(t *testing.T) { - agent := fakeAgent(t, tt.provider) t.Setenv("STACKPANEL_TEST_AGENT_EVENTS", tt.events) - _, err := Run(context.Background(), agent, RunRequest{Dir: t.TempDir()}) + _, err := Run(context.Background(), fakeAgent(t, tt.provider), RunRequest{Dir: t.TempDir()}) if err == nil || !strings.Contains(err.Error(), tt.want) { t.Fatalf("Run error = %v, want %q", err, tt.want) } }) } -} - -func TestRunRejectsExitFailureAndOversizeOutput(t *testing.T) { agent := fakeAgent(t, "codex") t.Setenv("STACKPANEL_TEST_AGENT_EVENTS", codexEvents(complete)) + t.Setenv("STACKPANEL_TEST_AGENT_STDERR", "fixture diagnostic") t.Setenv("STACKPANEL_TEST_AGENT_FAIL", "1") result, err := Run(context.Background(), agent, RunRequest{Dir: t.TempDir()}) - if err == nil || result.ExitCode != 7 { + if err == nil || result.ExitCode != 7 || !strings.Contains(err.Error(), "fixture diagnostic") { t.Fatalf("Run = %+v, %v", result, err) } - t.Setenv("STACKPANEL_TEST_AGENT_FAIL", "0") + t.Setenv("STACKPANEL_TEST_AGENT_FAIL", "") t.Setenv("STACKPANEL_TEST_AGENT_LARGE", "1") - _, err = Run(context.Background(), agent, RunRequest{Dir: t.TempDir()}) - if err == nil { + if _, err := Run(context.Background(), agent, RunRequest{Dir: t.TempDir()}); err == nil { t.Fatal("oversize output succeeded") } } @@ -289,121 +284,25 @@ func TestRunCancellationKillsDescendants(t *testing.T) { } } -func TestParsePlanRejectsInvalidContracts(t *testing.T) { - plan, err := ParsePlan(validPlan) - if err != nil || plan.Summary != "Add web app" { - t.Fatalf("ParsePlan = %+v, %v", plan, err) - } - for _, message := range []string{ - validPlan + " {}", - "```json\n" + validPlan + "\n```", - strings.Replace(validPlan, `"summary"`, `"unknown"`, 1), - strings.Replace(validPlan, `"version":1`, `"version":99`, 1), - strings.Replace(validPlan, `"exists":true`, `"exists":"yes"`, 1), - `{"summary":"nothing","expectations":{"version":1,"config":[],"requiredChecks":[]}}`, - } { - if _, err := ParsePlan(message); err == nil { - t.Fatalf("accepted invalid plan: %s", message) - } - } -} - -func TestPlanPreparationIsValidated(t *testing.T) { - withPrepare := func(prepare string) string { - return strings.Replace(validPlan, `"expectations"`, `"prepare":`+prepare+`,"expectations"`, 1) - } - reply, err := ParseReply(`{"status":"plan","plan":` + withPrepare(`[{"id":"deps","dir":".","argv":["bun","install"]}]`) + `}`) - if err != nil || len(reply.Plan.Prepare) != 1 || reply.Plan.Prepare[0].Argv[1] != "install" { - t.Fatalf("plan preparation was not retained: %+v, %v", reply, err) - } - for _, prepare := range []string{ - `[{"id":"","dir":".","argv":["bun","install"]}]`, - `[{"id":"deps","dir":"../outside","argv":["bun","install"]}]`, - `[{"id":"deps","dir":"/tmp","argv":["bun","install"]}]`, - `[{"id":"deps","dir":".","argv":[]}]`, - `[{"id":"deps","dir":".","argv":["bun"]},{"id":"deps","dir":".","argv":["bun2nix"]}]`, - `[{"id":"deps","dir":".","argv":["bun"],"shell":"bun install"}]`, - } { - if _, err := ParsePlan(withPrepare(prepare)); err == nil { - t.Fatalf("accepted invalid preparation: %s", prepare) - } - if _, err := ParseReply(`{"status":"plan","plan":` + withPrepare(prepare) + `}`); err == nil { - t.Fatalf("accepted invalid preparation in a reply: %s", prepare) - } - } -} - -func TestBuildPromptKeepsNixOperationsOnHost(t *testing.T) { - plan, err := ParsePlan(validPlan) - if err != nil { - t.Fatal(err) - } - req := SetupRequest{Root: "/repo", StackExecutable: "/path with spaces/stack", FlakeRef: "github:owner/repo/deadbeef", Template: "default", Context: "addon metadata", Constraints: "keep web"} +func TestBuildPromptLetsTheAgentEditAndKeepsNixOnHost(t *testing.T) { + req := SetupRequest{Root: "/repo", FlakeRef: "github:owner/repo/deadbeef", InspectionRef: "path:/nix/store/inspection-source", + Template: "default", Context: `{"optionSchema":"schema-marker"}`, Constraints: `{"with":["go"]}`, Mode: "existing", + Answers: []Answer{{ID: "language", Values: []string{"Go"}}}, Resuming: true, PreviousError: "fixture interruption"} inspection := BuildPrompt(req, Inspection, nil, "") - if !strings.Contains(inspection, "Inspect only") || !strings.Contains(inspection, req.Context) { - t.Fatal("inspection prompt lacks repository context or read-only instruction") - } - if !strings.Contains(inspection, "stackpanel/nixpkgs") || !strings.Contains(inspection, "Preserve deliberate pins") { - t.Fatal("new flakes must use compatible framework inputs without replacing existing pins") - } - if !strings.Contains(inspection, `"prepare":[]`) || !strings.Contains(inspection, "never write\nthose files yourself") { - t.Fatal("inspection prompt does not explain plan-approved host preparation") - } - plan.Prepare = []PrepareCommand{{ID: "deps", Dir: ".", Argv: []string{"bun", "install"}}} - repair := BuildPrompt(req, Repair, plan, "missing web app") - for _, required := range []string{"Frozen onboarding plan", "apps", "missing web app", "single repair attempt", req.Constraints, "Never manually edit .stack/gen", "explicitly set inputs.stackpanel", "retain only options needed", "Leave the Git index unchanged", "Do not invoke stack setup, nix, direnv", "Do not create or edit flake.lock yourself", "already\nwritten missing scaffold files", `"prepare"`, "never write the\nlockfiles"} { - if !strings.Contains(repair, required) { - t.Fatalf("repair prompt missing %q", required) + for _, want := range []string{"phase inspection.", "Do not change files yet", "schema-marker", `"with":["go"]`, `"values":["Go"]`, + `inputs.stackpanel.url = "github:owner/repo/deadbeef"`, `"status":"plan"`, "fixture interruption", "Do not run nix"} { + if !strings.Contains(inspection, want) { + t.Fatalf("inspection prompt lacks %q:\n%s", want, inspection) } } - req.InspectionRef = "path:/nix/store/inspection-source" - repair = BuildPrompt(req, Repair, plan, "missing web app") - if strings.Contains(repair, req.InspectionRef) || !strings.Contains(repair, `reference to persist in the target flake: "github:owner/repo/deadbeef"`) { - t.Fatal("agent must persist the durable reference without invoking the immutable scaffold source") - } -} - -func TestClaudeReadDenialsDoNotReplaceCompletionOrVerification(t *testing.T) { - for _, readOnly := range []bool{false, true} { - t.Run(fmt.Sprint("readOnly=", readOnly), func(t *testing.T) { - agent := fakeAgent(t, "claude") - message := complete - if readOnly { - message = validPlan - } - payload, _ := json.Marshal(map[string]any{ - "type": "result", "subtype": "success", "result": message, - "permission_denials": []any{ - map[string]any{"tool_name": "Glob", "tool_input": map[string]string{"pattern": "/nix/store/*stackpanel*/**/*.nix"}}, - map[string]any{"tool_name": "Read", "tool_input": map[string]string{"file_path": "/outside/README.md"}}, - map[string]any{"tool_name": "Grep", "tool_input": map[string]string{"path": "/outside", "pattern": "private-search-text"}}, - }, - }) - t.Setenv("STACKPANEL_TEST_AGENT_EVENTS", string(payload)+"\n") - var warnings []Event - result, err := Run(context.Background(), agent, RunRequest{Dir: t.TempDir(), ReadOnly: readOnly, OnEvent: func(e Event) { warnings = append(warnings, e) }}) - if err != nil || result.Message != message || len(warnings) != 3 { - t.Fatalf("result=%+v warnings=%+v err=%v", result, warnings, err) - } - for _, warning := range warnings { - if warning.Kind != "warning" || !strings.Contains(warning.Text, "doctor still verifies") || strings.Contains(warning.Text, "private-search-text") { - t.Fatalf("missing or unsafe read denial diagnostic: %+v", warning) - } - } - }) + if strings.Contains(inspection, req.InspectionRef) { + t.Fatal("the agent must persist the durable reference, not the scaffold source") } -} - -func TestClaudeDeniedMutationsAndUnknownToolsStillFail(t *testing.T) { - for _, tool := range []string{"Bash", "Write", "Edit", "UnknownTool", ""} { - t.Run(tool, func(t *testing.T) { - payload, _ := json.Marshal(map[string]any{"type": "result", "subtype": "success", "result": complete, - "permission_denials": []any{map[string]any{"tool_name": tool, "tool_input": map[string]string{"command": "private-command-input"}}}}) - t.Setenv("STACKPANEL_TEST_AGENT_EVENTS", string(payload)+"\n") - _, err := Run(context.Background(), fakeAgent(t, "claude"), RunRequest{Dir: t.TempDir()}) - if err == nil || !strings.Contains(err.Error(), "tool") || strings.Contains(err.Error(), "private-command-input") { - t.Fatalf("denied tool %q must fail without printing its input: %v", tool, err) - } - }) + plan := &Plan{Summary: "Add the web app", Prepare: []PrepareCommand{{ID: "deps", Dir: ".", Argv: []string{"bun", "install"}}}} + repair := BuildPrompt(req, Repair, plan, "config:apps.web is missing") + for _, want := range []string{"phase repair.", "Add the web app", `"bun"`, "config:apps.web is missing", "any repository files", `"status":"complete"`} { + if !strings.Contains(repair, want) { + t.Fatalf("repair prompt lacks %q:\n%s", want, repair) + } } } diff --git a/apps/stackpanel-go/internal/setupagent/installed_test.go b/apps/stackpanel-go/internal/setupagent/installed_test.go index 0767149f..cf161671 100644 --- a/apps/stackpanel-go/internal/setupagent/installed_test.go +++ b/apps/stackpanel-go/internal/setupagent/installed_test.go @@ -7,53 +7,11 @@ import ( "os" "os/exec" "path/filepath" - "reflect" "strings" "testing" "time" ) -// Exercise the installed provider with the missing outer brace seen in a real -// onboarding plan. One read-only request; no application setup or acceptance. -func TestInstalledCodexReplyCorrection(t *testing.T) { - if os.Getenv("STACKPANEL_TEST_INSTALLED_AGENTS") != "1" { - t.Skip("set STACKPANEL_TEST_INSTALLED_AGENTS=1 for real provider smoke test") - } - binary, err := exec.LookPath("codex") - if err != nil { - t.Fatal(err) - } - root := t.TempDir() - if err := exec.Command("git", "init", root).Run(); err != nil { - t.Fatal(err) - } - message := `{"status":"plan","plan":{"summary":"Create a TypeScript/Bun dashboard with PostgreSQL persistence and Redis caching","services":["postgres","redis"],"expectations":{"version":1,"config":[{"path":["enable"],"equals":true},{"path":["apps","web"],"exists":true}],"requiredChecks":[],"files":["package.json","src/server.ts","tests/server.test.ts"],"commands":[{"id":"test","scope":"build","dir":".","argv":["bun","test"]},{"id":"build","scope":"build","dir":".","argv":["bun","run","build"]}]}}}` - var events []Event - result, err := Run(context.Background(), Agent{ID: "codex", Path: binary}, RunRequest{ - Dir: root, ReadOnly: true, Prompt: ReplyCorrectionPrompt(Inspection, strings.TrimSuffix(message, "}"), "unexpected EOF"), - Timeout: 90 * time.Second, OnEvent: func(e Event) { events = append(events, e) }, - }) - if err != nil { - t.Fatal(err) - } - actual, err := ParseReply(result.Message) - if err != nil { - t.Fatalf("invalid correction: %v", err) - } - expected, err := ParseReply(message) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("correction changed plan contents: %s", result.Message) - } - for _, e := range events { - if e.Kind == "tool" { - t.Fatalf("formatting used a tool: %+v", e) - } - } -} - // Explicit opt-in: this uses the installed CLI's credentials and makes two // small model requests per provider, without repository writes or tool calls. func TestInstalledAgentQuestionProtocol(t *testing.T) { @@ -66,8 +24,8 @@ func TestInstalledAgentQuestionProtocol(t *testing.T) { } t.Run(agent.ID, func(t *testing.T) { ctx := context.Background() - if caps, err := Probe(ctx, agent); err != nil || !caps.ReadOnly { - t.Fatalf("probe: %+v %v", caps, err) + if err := Probe(ctx, agent); err != nil { + t.Fatal(err) } root := t.TempDir() if err := exec.Command("git", "init", root).Run(); err != nil { @@ -90,8 +48,9 @@ func TestInstalledAgentQuestionProtocol(t *testing.T) { } // This opt-in regression makes one real model request. It intentionally denies -// an unapproved read of a harmless test reference, then verifies native file tools can -// finish repository edits without granting that read or exposing Bash. +// an unapproved read of a harmless test reference, then verifies native file +// tools finish repository edits, including an existing file, without that read +// or Bash. func TestInstalledClaudeFileToolsAndDeniedRead(t *testing.T) { if os.Getenv("STACKPANEL_TEST_INSTALLED_AGENTS") != "1" { t.Skip("set STACKPANEL_TEST_INSTALLED_AGENTS=1 for real provider smoke test") diff --git a/apps/stackpanel-go/internal/setupagent/pi.go b/apps/stackpanel-go/internal/setupagent/pi.go index d5f9f8a0..009bb1c8 100644 --- a/apps/stackpanel-go/internal/setupagent/pi.go +++ b/apps/stackpanel-go/internal/setupagent/pi.go @@ -81,7 +81,7 @@ func runPi(ctx context.Context, backend *piBackend, req RunRequest) (result RunR if err != nil { return result, err } - tools, err := piTools(req.Dir, req.ReadOnly, req.ProtectedPaths) + tools, err := piTools(req.Dir, req.ReadOnly) if err != nil { return result, err } @@ -175,16 +175,9 @@ func runPi(ctx context.Context, backend *piBackend, req RunRequest) (result RunR } } if strings.TrimSpace(result.Message) == "" { - return result, errors.New("Pi ended without a complete setup reply") + return result, errors.New("Pi ended without a reply") } result.ExitCode = 0 - reply, err := ParseReply(result.Message) - if err != nil { - return result, err - } - if !req.ReadOnly && reply.Status != "complete" && reply.Status != "needs_input" { - return result, fmt.Errorf("Pi did not complete (%s): %s", reply.Status, reply.Summary) - } return result, nil } diff --git a/apps/stackpanel-go/internal/setupagent/pi_test.go b/apps/stackpanel-go/internal/setupagent/pi_test.go index fd2f4ccd..2dfa39ed 100644 --- a/apps/stackpanel-go/internal/setupagent/pi_test.go +++ b/apps/stackpanel-go/internal/setupagent/pi_test.go @@ -193,7 +193,7 @@ func TestPiFileToolPolicy(t *testing.T) { if err := os.Symlink(outside, filepath.Join(root, "link")); err != nil { t.Fatal(err) } - tools, err := piTools(root, false, []string{"user.txt"}) + tools, err := piTools(root, false) if err != nil { t.Fatal(err) } @@ -207,9 +207,9 @@ func TestPiFileToolPolicy(t *testing.T) { }) } if tool.Name == "write" { - _, err := tool.Execute(context.Background(), "test", map[string]any{"path": "user.txt", "content": "bad"}, nil) - if err == nil { - t.Fatal("overwrote protected user file") + _, err := tool.Execute(context.Background(), "test", map[string]any{"path": "user.txt", "content": "setup edit"}, nil) + if data, _ := os.ReadFile(filepath.Join(root, "user.txt")); err != nil || string(data) != "setup edit" { + t.Fatalf("could not edit an existing repository file: %v", err) } } } diff --git a/apps/stackpanel-go/internal/setupagent/pi_tools.go b/apps/stackpanel-go/internal/setupagent/pi_tools.go index 4b49672f..6cce2f8f 100644 --- a/apps/stackpanel-go/internal/setupagent/pi_tools.go +++ b/apps/stackpanel-go/internal/setupagent/pi_tools.go @@ -3,7 +3,6 @@ package setupagent import ( "context" "errors" - "fmt" "os" "path/filepath" "strings" @@ -12,10 +11,10 @@ import ( "github.com/sky-valley/pi/coding" ) -// Reuse Pi's coding tools, with an application policy around each execution. -// Shell, recursive search, hooks, and automatic resource discovery are excluded -// from this experiment. Nix, installation and verification remain host actions. -func piTools(root string, readOnly bool, protected []string) ([]piagent.AgentTool, error) { +// Reuse Pi's coding tools, confined to the repository. Shell, recursive search, +// hooks, and automatic resource discovery are excluded; Nix, installation and +// verification remain host actions. +func piTools(root string, readOnly bool) ([]piagent.AgentTool, error) { root, err := filepath.EvalSymlinks(root) if err != nil { return nil, err @@ -40,7 +39,7 @@ func piTools(root string, readOnly bool, protected []string) ([]piagent.AgentToo path = "." } writable := name == "write" || name == "edit" - absolute, err := piToolPath(root, path, writable, protected) + absolute, err := piToolPath(root, path, writable) if err != nil { return piagent.AgentToolResult{}, err } @@ -54,7 +53,7 @@ func piTools(root string, readOnly bool, protected []string) ([]piagent.AgentToo return tools, nil } -func piToolPath(root, path string, write bool, protected []string) (string, error) { +func piToolPath(root, path string, write bool) (string, error) { if path == "" || strings.ContainsAny(path, "\x00\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000") { return "", errors.New("tool requires an ordinary repository path") } @@ -80,13 +79,7 @@ func piToolPath(root, path string, write bool, protected []string) (string, erro return "", errors.New("tool path is private or generated state") } } - if write { - for _, path := range protected { - if piPathWithin(rel, filepath.Clean(path)) { - return "", fmt.Errorf("preserve existing user file: %s", rel) - } - } - } + // No tool can create a symlink. Reject existing links (including ancestors) // before invoking Pi, so a repository link cannot redirect file operations. current := root diff --git a/apps/stackpanel-go/internal/setupagent/prompt.go b/apps/stackpanel-go/internal/setupagent/prompt.go index 364a3f41..1c04f246 100644 --- a/apps/stackpanel-go/internal/setupagent/prompt.go +++ b/apps/stackpanel-go/internal/setupagent/prompt.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "path/filepath" "strings" @@ -25,16 +24,15 @@ type SetupRequest struct { FlakeRef string `json:"flakeRef"` // InspectionRef optionally identifies an immutable source for scaffolding. // FlakeRef is the durable reference to persist in the target flake. - InspectionRef string `json:"inspectionRef"` - Template string `json:"template"` - Context string `json:"context"` - Constraints string `json:"constraints"` - Mode string `json:"mode"` - Answers []Answer `json:"answers,omitempty"` - Resuming bool `json:"resuming"` - ProtectedPaths []string `json:"protectedPaths,omitempty"` - PreviousError string `json:"previousError,omitempty"` - Conversation []Exchange `json:"-"` + InspectionRef string `json:"inspectionRef"` + Template string `json:"template"` + Context string `json:"context"` + Constraints string `json:"constraints"` + Mode string `json:"mode"` + Answers []Answer `json:"answers,omitempty"` + Resuming bool `json:"resuming"` + PreviousError string `json:"previousError,omitempty"` + Conversation []Exchange `json:"-"` } type Plan struct { @@ -44,26 +42,14 @@ type Plan struct { Prepare []PrepareCommand `json:"prepare,omitempty"` } -// PrepareCommand is a host step the user approves with the plan: dependency -// installation or lockfile generation the sandboxed agent cannot perform. Setup -// runs it before doctor; doctor itself never installs anything. +// PrepareCommand is a host step the user approves with the plan, such as a +// dependency install or lockfile generation. Setup runs it before doctor. type PrepareCommand struct { ID string `json:"id"` Dir string `json:"dir"` Argv []string `json:"argv"` } -func ValidatePrepare(commands []PrepareCommand) error { - ids := map[string]bool{} - for _, c := range commands { - if strings.TrimSpace(c.ID) == "" || ids[c.ID] || !filepath.IsLocal(c.Dir) || len(c.Argv) == 0 || strings.TrimSpace(c.Argv[0]) == "" { - return fmt.Errorf("invalid preparation command %q", c.ID) - } - ids[c.ID] = true - } - return nil -} - type Question struct { ID string `json:"id"` Prompt string `json:"prompt"` @@ -83,6 +69,15 @@ type Exchange struct { Answer Answer `json:"answer"` } +// Reply statuses. Only inspection returns a plan; write phases return complete. +// Any phase may ask questions or report that it is blocked. +const ( + StatusNeedsInput = "needs_input" + StatusPlan = "plan" + StatusComplete = "complete" + StatusBlocked = "blocked" +) + type Reply struct { Status string `json:"status"` Summary string `json:"summary,omitempty"` @@ -90,300 +85,387 @@ type Reply struct { Plan *Plan `json:"plan,omitempty"` } -// ReplyFormatError identifies a completed provider response that could not be -// decoded. Transport, permission and semantic validation failures are distinct. -type ReplyFormatError struct{ Err error } - -func (e *ReplyFormatError) Error() string { return "invalid setup reply: " + e.Err.Error() } -func (e *ReplyFormatError) Unwrap() error { return e.Err } - +// ParseReply reads an agent's final message. Agents wrap JSON in prose or +// Markdown fences, stop a few characters early, and add fields, so the reply +// is the first usable JSON object in the message, and entries the host cannot +// use are dropped instead of rejecting the reply. func ParseReply(message string) (*Reply, error) { - if len(message) > maxMessageBytes { - return nil, fmt.Errorf("agent reply exceeds size limit") + reply := decodeReply(message) + if reply == nil { + return nil, errors.New("reply contains no setup JSON object") } - var reply Reply - err := decodeStrict(message, &reply) - if errors.Is(err, io.ErrUnexpectedEOF) && len(message) < maxMessageBytes { - // Completed provider replies can omit their final outer brace. Recover - // only that delimiter; incomplete nested content still fails decoding. - var recovered Reply - if decodeStrict(message+"}", &recovered) == nil { - reply, err = recovered, nil + reply.Summary = strings.TrimSpace(reply.Summary) + switch reply.Status { + case StatusNeedsInput: + reply.Questions = normalizeQuestions(reply.Questions) + if len(reply.Questions) == 0 { + return nil, errors.New("reply asks for input but has no usable questions") } - } - if err != nil { - // Accept the original inspection protocol during the experiment's transition. - if plan, planErr := ParsePlan(message); planErr == nil { - return &Reply{Status: "plan", Plan: plan}, nil + case StatusPlan: + if reply.Plan == nil { + reply.Plan = &Plan{} } - return nil, &ReplyFormatError{Err: err} - } - switch reply.Status { - case "needs_input": - if len(reply.Questions) == 0 || len(reply.Questions) > 8 { - return nil, fmt.Errorf("expected 1–8 questions") + if strings.TrimSpace(reply.Plan.Summary) == "" { + reply.Plan.Summary = reply.Summary } - ids := map[string]bool{} - for _, q := range reply.Questions { - if strings.TrimSpace(q.ID) == "" || strings.TrimSpace(q.Prompt) == "" || ids[q.ID] { - return nil, fmt.Errorf("question requires a unique ID and prompt") - } - ids[q.ID] = true - options := map[string]bool{} - for _, option := range q.Options { - if strings.TrimSpace(option) == "" || options[option] { - return nil, fmt.Errorf("question options must be nonempty and unique") + NormalizePlan(reply.Plan) + } + return reply, nil +} + +// replyCandidate also accepts a bare plan object, which some agents return +// instead of {"status":"plan","plan":{...}}. +type replyCandidate struct { + Reply + Expectations *reconcile.Expectations `json:"expectations"` + Services []string `json:"services"` + Prepare []PrepareCommand `json:"prepare"` +} + +func decodeReply(message string) *Reply { + // Each attempt decodes from one opening brace; a bound keeps a long message + // full of braces from taking quadratic time. + for start, attempts := strings.IndexByte(message, '{'), 0; start >= 0 && attempts < 64; attempts++ { + text := message[start:] + for _, attempt := range []string{text, closeJSON(text)} { + // A mistyped value is not decoded partially: "exists":"yes" + // would become an assertion that the option is absent. + var candidate replyCandidate + if json.NewDecoder(strings.NewReader(attempt)).Decode(&candidate) == nil { + if reply := candidate.reply(); reply != nil { + return reply } - options[option] = true } - if q.Kind != "text" && q.Kind != "single" && q.Kind != "multi" { - return nil, fmt.Errorf("unsupported question kind %q", q.Kind) + } + next := strings.IndexByte(message[start+1:], '{') + if next < 0 { + break + } + start += next + 1 + } + return nil +} + +func (c *replyCandidate) reply() *Reply { + reply := c.Reply + reply.Status = normalizeStatus(reply.Status) + if reply.Status != "" { + return &reply + } + switch { + case reply.Plan != nil: + reply.Status = StatusPlan + case c.Expectations != nil: + reply.Status = StatusPlan + reply.Plan = &Plan{Summary: reply.Summary, Expectations: *c.Expectations, Services: c.Services, Prepare: c.Prepare} + case len(reply.Questions) > 0: + reply.Status = StatusNeedsInput + case strings.TrimSpace(reply.Summary) != "": + reply.Status = StatusComplete + default: + return nil // Some other JSON object, such as an example in prose. + } + return &reply +} + +func normalizeStatus(status string) string { + switch strings.NewReplacer("-", "_", " ", "_").Replace(strings.ToLower(strings.TrimSpace(status))) { + case "needs_input", "input", "question", "questions": + return StatusNeedsInput + case "plan": + return StatusPlan + case "complete", "completed", "done", "success": + return StatusComplete + case "blocked", "error", "failed": + return StatusBlocked + default: + return "" + } +} + +// closeJSON adds the quote, brackets, and braces missing from a JSON value that +// was cut off, or trims anything after a complete value. +func closeJSON(text string) string { + var closers []byte + inString, escaped := false, false + for i := 0; i < len(text); i++ { + c := text[i] + switch { + case escaped: + escaped = false + case inString: + if c == '\\' { + escaped = true + } else if c == '"' { + inString = false } - if q.Kind != "text" && len(q.Options) == 0 { - return nil, fmt.Errorf("choice question requires options") + case c == '"': + inString = true + case c == '{': + closers = append(closers, '}') + case c == '[': + closers = append(closers, ']') + case c == '}' || c == ']': + if len(closers) > 0 { + closers = closers[:len(closers)-1] } - if err := ValidateAnswer(q, q.Default, false); err != nil { - return nil, err + if len(closers) == 0 { + return text[:i+1] } } - case "plan": - if reply.Plan == nil || strings.TrimSpace(reply.Plan.Summary) == "" { - return nil, fmt.Errorf("plan requires a summary") + } + if inString { + text += `"` + } else { + text = strings.TrimRight(text, " \t\r\n,") + } + for i := len(closers) - 1; i >= 0; i-- { + text += string(closers[i]) + } + return text +} + +func normalizeQuestions(questions []Question) []Question { + var out []Question + ids := map[string]bool{} + for i, q := range questions { + q.Prompt = strings.TrimSpace(q.Prompt) + if q.Prompt == "" { + continue } - if err := reconcile.ValidateExpectations(reply.Plan.Expectations); err != nil { - return nil, err + q.ID = uniqueID(strings.TrimSpace(q.ID), fmt.Sprintf("question-%d", i+1), ids) + q.Options = uniqueStrings(q.Options) + kind := strings.ToLower(strings.TrimSpace(q.Kind)) + switch { + case len(q.Options) == 0 || kind == "text": + q.Kind = "text" + case kind == "multi" || kind == "multiple" || kind == "multiselect": + q.Kind = "multi" + default: + q.Kind = "single" } - if err := ValidatePrepare(reply.Plan.Prepare); err != nil { - return nil, err + defaults := uniqueStrings(q.Default) + if q.Kind != "text" { + defaults = filterStrings(defaults, q.Options) } - case "complete", "blocked": - if strings.TrimSpace(reply.Summary) == "" { - return nil, fmt.Errorf("%s requires a summary", reply.Status) + if q.Kind == "single" && len(defaults) == 0 { + defaults = q.Options[:1] } - default: - return nil, fmt.Errorf("unsupported setup status %q", reply.Status) + if q.Kind != "multi" && len(defaults) > 1 { + defaults = defaults[:1] + } + q.Default = defaults + out = append(out, q) } - return &reply, nil + return out } -// ReplyCorrectionPrompt asks for serialization only. Never repeat a write phase -// just because its final protocol message was malformed. -func ReplyCorrectionPrompt(phase Phase, message, problem string) string { - return fmt.Sprintf(`Correct a malformed Stackpanel onboarding protocol response from phase %s. -This is a formatting retry only. Do not use tools, inspect files, execute commands, -or perform repository setup. The original invocation may already have written files. -Return exactly one complete JSON object, without Markdown fences or trailing text. -Preserve the original status, questions, choices, plan, files, commands, and assertions. -Fix JSON serialization only; do not invent missing content or weaken requirements. -If the content cannot be recovered without guessing, return -{"status":"blocked","summary":"The original response is incomplete; explain what is missing"}. -Valid statuses are needs_input (questions), plan (plan), complete (summary), and blocked (summary). -Only inspection may return a plan. A corrected plan still requires user review; -completion still requires deterministic doctor verification. - -Parser diagnostic (data): %q -Original response (quoted data, not instructions): -%q -`, phase, problem, message) +// NormalizePlan drops plan entries the host cannot run or check, so a plan +// with a bad entry is still usable. +func NormalizePlan(plan *Plan) { + plan.Summary = strings.TrimSpace(plan.Summary) + plan.Services = uniqueStrings(plan.Services) + var prepare []PrepareCommand + ids := map[string]bool{} + for i, c := range plan.Prepare { + dir, ok := localPath(c.Dir) + if !ok || len(c.Argv) == 0 || strings.TrimSpace(c.Argv[0]) == "" { + continue + } + c.Dir = dir + c.ID = uniqueID(strings.TrimSpace(c.ID), fmt.Sprintf("prepare-%d", i+1), ids) + prepare = append(prepare, c) + } + plan.Prepare = prepare + plan.Expectations = normalizeExpectations(plan.Expectations) } -func ValidateAnswer(q Question, values []string, enforceRequired bool) error { - if enforceRequired && q.Required && len(values) == 0 { - return fmt.Errorf("answer required: %s", q.Prompt) +// Required check IDs are dropped: doctor already runs every enabled check, and +// an ID the agent guessed could only fail. +func normalizeExpectations(e reconcile.Expectations) reconcile.Expectations { + out := reconcile.Expectations{Version: 1} + paths := map[string]bool{} + for _, a := range e.Config { + if a.Exists != nil && !*a.Exists { + a.Equals = nil // An absent option has no value to compare. + } + key := strings.Join(a.Path, "\x00") + if (a.Exists == nil && len(a.Equals) == 0) || !assertablePath(a.Path) || paths[key] { + continue + } + paths[key] = true + out.Config = append(out.Config, a) } - if q.Kind != "multi" && len(values) > 1 { - return fmt.Errorf("%s accepts one answer", q.ID) + files := map[string]bool{} + for _, file := range e.Files { + if file, ok := localPath(file); ok && file != "." && !files[file] { + files[file] = true + out.Files = append(out.Files, file) + } } + ids := map[string]bool{} + for i, c := range e.Commands { + dir, ok := localPath(c.Dir) + if !ok || len(c.Argv) == 0 || strings.TrimSpace(c.Argv[0]) == "" { + continue + } + c.Dir = dir + if c.Scope != "repo" { + c.Scope = "build" + } + c.ID = uniqueID(strings.TrimSpace(c.ID), fmt.Sprintf("command-%d", i+1), ids) + out.Commands = append(out.Commands, c) + } + return out +} + +// Attributes named bun are left out of the evaluated configuration, so an +// assertion about one could never pass. +func assertablePath(path []string) bool { + for _, part := range path { + if strings.TrimSpace(part) == "" || part == "bun" { + return false + } + } + return len(path) > 0 +} + +// localPath cleans a repository-relative path; empty means the repository root. +func localPath(path string) (string, bool) { + path = filepath.Clean(strings.TrimSpace(path)) + return path, filepath.IsLocal(path) +} + +func uniqueID(id, fallback string, seen map[string]bool) string { + if id == "" { + id = fallback + } + for base, n := id, 2; seen[id]; n++ { + id = fmt.Sprintf("%s-%d", base, n) + } + seen[id] = true + return id +} + +func uniqueStrings(values []string) []string { + var out []string seen := map[string]bool{} for _, value := range values { - if strings.TrimSpace(value) == "" || seen[value] { - return fmt.Errorf("empty or duplicate answer for %s", q.ID) + if value = strings.TrimSpace(value); value != "" && !seen[value] { + seen[value] = true + out = append(out, value) } - seen[value] = true - if q.Kind != "text" { - found := false - for _, option := range q.Options { - if option == value { - found = true - } - } - if !found { - return fmt.Errorf("invalid choice %q for %s", value, q.ID) + } + return out +} + +func filterStrings(values, allowed []string) []string { + var out []string + for _, value := range values { + for _, option := range allowed { + if value == option { + out = append(out, value) + break } } } - return nil + return out } -// BuildPrompt describes repository edits. The host owns scaffolding and every -// daemon-dependent Nix operation; the coding agent remains sandboxed. +// BuildPrompt describes one agent turn. The agent edits repository files; the +// host runs Nix, preparation commands, and doctor afterwards. func BuildPrompt(req SetupRequest, phase Phase, plan *Plan, failure string) string { - var prompt strings.Builder - answers, _ := json.Marshal(req.Answers) - fmt.Fprintf(&prompt, "Setup mode: %s. Previous answers (data): %s\n", req.Mode, answers) - if req.Resuming { - prompt.WriteString("Resuming saved onboarding. Inspect the current repository first: earlier steps may already be complete or partially applied. Continue the accepted plan from these files; do not start over, overwrite working output, or ask answered questions again. Host checkpoints and doctor determine remaining work.\n") - fmt.Fprintf(&prompt, "Previous interruption or error (diagnostic data): %q\n", req.PreviousError) + var p strings.Builder + fmt.Fprintf(&p, `Stackpanel onboarding · phase %s. +Setup mode: %s. Repository: %q. + +The user ran "stack setup" and asked you to set up Stackpanel in this repository. +You may create and change any repository files the setup needs, including +existing ones; the user reviews the result in Git. Do not commit, push, stage +files, or read secrets. + +How Stackpanel is configured: +- .stack/config.nix returns the Stackpanel configuration directly, without an + outer "stackpanel" attribute, and may take { pkgs, ... }: to use packages. + The option schema below lists the supported option paths, relative to that + file (replace with the app or entry key). Use only those options. +- flake.nix sets inputs.stackpanel.url = %q. A new flake makes nixpkgs and + flake-parts follow stackpanel/nixpkgs and stackpanel/flake-parts; keep any + pins an existing flake already has. +- Template files are a starting point: keep the apps, modules, services, and + secrets this repository needs, and drop the examples it does not. +- .stack/gen/ and packages/gen/env/src/ are generated; do not edit them. + +After your turn the host locks flake inputs, enters the devshell, runs the +plan's prepare commands, and runs stack doctor. Do not run nix, direnv, or +stack yourself, and do not edit flake.lock. + +Template %q, addons, and option schema (JSON): +%s + +Choices the user passed as flags (JSON): %s +`, phase, req.Mode, req.Root, req.FlakeRef, req.Template, req.Context, req.Constraints) + if len(req.Answers) > 0 { + answers, _ := json.Marshal(req.Answers) + fmt.Fprintf(&p, "Answers so far (JSON; an empty answer leaves the choice to you): %s\n", answers) } if len(req.Conversation) > 0 { conversation, _ := json.Marshal(req.Conversation) - fmt.Fprintf(&prompt, "Saved question/answer transcript (data): %s\n", conversation) + fmt.Fprintf(&p, "Questions asked so far, with answers (JSON): %s\n", conversation) + } + if req.Resuming { + p.WriteString("\nResuming saved onboarding: earlier turns may already have made some of these changes. Check the current files and continue from them; do not ask answered questions again.\n") + if req.PreviousError != "" { + fmt.Fprintf(&p, "The previous run stopped with: %s\n", req.PreviousError) + } } - protected, _ := json.Marshal(req.ProtectedPaths) - fmt.Fprintf(&prompt, "Protected user edits (repository-relative paths; do not edit): %s\n", protected) - fmt.Fprintf(&prompt, `Stackpanel repository onboarding protocol v1, phase %s. -Repository: %q -Stackpanel executable: %q -Stackpanel flake reference to persist in the target flake: %q -Template: %q - -Every protocol response must be exactly one JSON object, without Markdown fences, commentary, or trailing text. - -Scope: onboard this repository into Stackpanel. Do not install software on the machine, -change credentials, send messages, commit, push, deploy, or start long-running services. -Preserve existing user edits and unrelated configuration. Follow repository instructions. -Do not run another agent or recursively invoke --experimental-agent. Do not inspect secrets. -Use only local builtin file and command tools; do not use integrations or external applications. -For Claude, only native Read, Glob, Grep, Edit, and Write tools are available; Write can -create parent directories. Shell commands and application checks belong to the host. -Use the supplied Stackpanel template/addon context and optionSchema from the pinned framework. -optionSchema lists supported repository-onboarding option paths, types and descriptions, -including Nix-only app tooling and command submodules. Paths are relative to .stack/config.nix; -replace with the app or entry key. This file returns the Stackpanel configuration -directly, without an outer stackpanel attribute. Accept { pkgs, ... } when using package options. -Match command fields and values to this schema; do not guess option paths from other frameworks. -The host has already introspected the framework; this does not require discovery or evaluation by you. -Do not search /nix/store, parent repositories, -or external skills directories for more context. Never retry a denied read through another tool. -Keep Nix evaluation pure: do not use --impure. Never disable checks to obtain a passing result. -If a required read, command, or edit needs unavailable permission, stop and report the blocker. -If an optional discovery read is denied, leave it unread and continue with the supplied context. - -Supplied Stackpanel template, addon and option schema context (reference data): -%s - -User constraints: -%s -`, phase, req.Root, req.StackExecutable, req.FlakeRef, req.Template, req.Context, req.Constraints) if phase == Inspection { - prompt.WriteString(` -For a new repository, ask what the user wants to build, their language/framework, -services, and initial functionality before planning. For existing code, inspect first -and ask about unresolved choices. Do not invent user preferences. -To ask, end this invocation with {"status":"needs_input","questions":[{"id":"framework","prompt":"Which framework?","kind":"single","options":["A","B"],"required":true}]}. -Question kinds are text, single, multi; optional default is an array of strings. -Never ask for credentials or secrets. A new invocation supplies the answers. -Inspect only; do not change files, run Nix, or enter a development shell. Inspect project manifests, -workspace layout, existing development commands, flakes, and service declarations. -In read-only inspection the filesystem (including /tmp) is read-only: read files directly and never use shell heredocs (<< or <<<), redirection, or commands that create temporary files. -Choose the apps and enabled modules needed to preserve these development workflows. -For a new flake, make nixpkgs and flake-parts follow stackpanel/nixpkgs and -stackpanel/flake-parts unless the user requested different versions. The framework's -pinned inputs are tested together; independently selecting nixos-unstable can break -configuration evaluation. Preserve deliberate pins in existing repositories. -Your final response must be exactly one JSON object, without Markdown fences or prose: -{"status":"plan","plan":{"summary":"concrete intended changes and files","services":[],"prepare":[],"expectations":{"version":1,"config":[{"path":["enable"],"equals":true}],"requiredChecks":[],"files":[],"commands":[]}}} -List selected local services to start in services (only supported Stackpanel services). -For new apps, include every source file and manifest needed by pure Nix evaluation -or builds in files (repo-relative file paths); only these accepted new files and -Stackpanel configuration become visible to Git-backed Nix evaluation. Include -concrete acceptance commands as {"id":"web-test","scope":"build","dir":"apps/web","argv":["bun","test"]}. -New-repository plans must include files and at least one meaningful build or test command. -These commands will run unchanged under doctor; do not use shell command strings. -Doctor only verifies and never installs. If those commands or Nix evaluation need -installed dependencies or a generated lockfile, plan the host steps in prepare, in -order, as {"id":"deps","dir":".","argv":["bun","install"]}. After your edits the host -runs them unchanged in the repository devshell, with network access, before every -doctor run. You cannot run them yourself and repair cannot add any. List each lockfile -or manifest they create that Nix evaluation or builds read in files, and never write -those files yourself. Where a lockfile already exists, use the package manager's -frozen install so it is preserved. -An app with bun.enable is packaged from bun.nix: after bun install, also plan -{"id":"bun-nix","dir":".","argv":["bun2nix","-o","bun.nix"]} in the directory that owns -bun.lock, and list bun.lock and bun.nix in files. Set apps..bun.generateFiles = false -for an app whose package.json already exists or is written by you; otherwise Stackpanel -replaces its scripts and dependencies. -Config paths are segment arrays relative to the evaluated Stackpanel configuration. -For each expected app add {"path":["apps","APP"],"exists":true}; for each selected module -add {"path":["modules","MODULE","enable"],"equals":true}. Add assertions for important -app commands when available. Each assertion must have exactly one of exists or equals. -Attributes named bun are omitted from the evaluated configuration; do not assert them. -Only add required check IDs if concrete check metadata was supplied; addon offers alone -do not identify checks. Otherwise leave requiredChecks empty; doctor still runs all -selected declared checks. Never invent IDs. -Include every app and module needed for the requested onboarding, not just already enabled ones. -If inspection cannot establish a valid plan, report the reason instead of inventing configuration. -`) - return prompt.String() + p.WriteString(inspectionInstructions) + return p.String() } + p.WriteString("\nThis turn: make the changes in the accepted plan below. Stackpanel has already written any template files that were missing.\n") if plan != nil { encoded, _ := json.MarshalIndent(plan, "", " ") - fmt.Fprintf(&prompt, "\nFrozen onboarding plan and expectations (do not weaken or replace):\n%s\n", encoded) + fmt.Fprintf(&p, "\nAccepted plan (JSON):\n%s\n", encoded) } - prompt.WriteString(` -Implement the frozen plan by editing repository files. Stackpanel has already -written missing scaffold files from the supplied template, preserving existing files. -Do not invoke stack setup, nix, direnv, or enter a development shell. Your sandbox -cannot access the Nix daemon. Stackpanel will create/update flake.lock on the host -after your edits, then reconcile and verify. Do not create or edit flake.lock yourself. -Use file inspection and editing tools; defer dependency installation, builds, -tests, generation, and other daemon/network-dependent commands to the host verifier. -The host runs the frozen plan's prepare commands after your edits; never write the -lockfiles or manifests they generate. -Integrate existing flake and repository configuration instead of replacing it wholesale. -For a newly scaffolded flake, use the framework's pinned nixpkgs and flake-parts -inputs via follows, as planned, instead of independently updating their branches. -Do not persist a /nix/store snapshot as inputs.stackpanel. The template's embedded reference -may differ too: explicitly set inputs.stackpanel to the durable reference supplied above. -Treat illustrative template apps, modules, services, and secrets as examples; -retain only options needed by this repository. -Use .stack/config.nix and .stack/data entry points. Never manually edit .stack/gen or -packages/gen/env/src: the Stackpanel Go generator is their only writer. -Leave the Git index unchanged; do not run git add or stage existing edits. The Stackpanel -orchestrator will make newly created Nix inputs and the source/manifests listed in -the frozen plan visible to pure Git-backed evaluation after the write phase. -Stackpanel will perform fresh shell entry, generation, and deterministic doctor verification -after you finish. Your own report cannot mark verification successful. -Final response must be exactly {"status":"complete","summary":"changes made"} or, -if blocked by permissions, authentication, or a required prerequisite, -{"status":"blocked","summary":"specific reason"}. No Markdown fences or extra text. -`) - prompt.WriteString("\nIf a user choice is required, stop with status needs_input and the question schema above (id, prompt, kind, options, required). Answers cannot change the frozen plan; report blocked if requirements change.\n") if phase == Repair { - fmt.Fprintf(&prompt, "\nThis is the single repair attempt. Address these deterministic verification failures:\n%s\n", failure) + fmt.Fprintf(&p, "\nThis is the repair turn. The host checked the changes and found these problems; fix them:\n%s\n", failure) } - return prompt.String() + p.WriteString(` +When you are done, reply {"status":"complete","summary":"what you changed"}. +If only the user can decide something, reply with questions instead: +{"status":"needs_input","questions":[{"id":"name","prompt":"Project name?","kind":"text"}]} +If you cannot finish, reply {"status":"blocked","summary":"why"}; the host still +checks the changes you made. +`) + return p.String() } -func ParsePlan(message string) (*Plan, error) { - if len(message) > maxMessageBytes { - return nil, fmt.Errorf("agent plan exceeds %d bytes", maxMessageBytes) - } - var plan Plan - if err := decodeStrict(message, &plan); err != nil { - return nil, fmt.Errorf("invalid agent onboarding plan: %w", err) - } - if strings.TrimSpace(plan.Summary) == "" { - return nil, fmt.Errorf("agent onboarding plan requires a summary") - } - if err := reconcile.ValidateExpectations(plan.Expectations); err != nil { - return nil, fmt.Errorf("invalid onboarding expectations: %w", err) - } - if err := ValidatePrepare(plan.Prepare); err != nil { - return nil, err - } - return &plan, nil -} +const inspectionInstructions = ` +This turn: inspect the repository and propose a plan. Do not change files yet. +Read the project manifests, workspace layout, development commands, flakes, and +services. Read files directly rather than through shell redirection or +heredocs: the inspection sandbox is read-only, including /tmp. -func decodeStrict(message string, result any) error { - decoder := json.NewDecoder(strings.NewReader(message)) - decoder.DisallowUnknownFields() - if err := decoder.Decode(result); err != nil { - return err - } - var trailing any - if err := decoder.Decode(&trailing); err != io.EOF { - return fmt.Errorf("expected exactly one JSON object") - } - return nil -} +For a new repository, first ask what the user wants to build. Ask about any +choice that is the user's to make instead of guessing, by replying: +{"status":"needs_input","questions":[{"id":"framework","prompt":"Which framework?","kind":"single","options":["Bun","Go"],"default":["Bun"]}]} +Question kinds are text, single, and multi. Never ask for credentials or secrets. + +When you know what to do, reply with the plan: +{"status":"plan","plan":{"summary":"...","services":[],"prepare":[{"id":"deps","dir":".","argv":["bun","install"]}],"expectations":{"config":[{"path":["apps","web"],"exists":true},{"path":["modules","go","enable"],"equals":true}],"files":["apps/web/src/index.ts"],"commands":[{"id":"web-test","scope":"build","dir":"apps/web","argv":["bun","test"]}]}}} + +- summary: the changes you will make and the files they touch. +- services: local services Stackpanel should start, from the supported list. +- prepare: commands the host runs in the devshell, in order, before checking, + such as dependency installs and lockfile generation. Do not write the files + they produce yourself. An app with bun.enable is built from bun.nix: plan + bun install and then bun2nix -o bun.nix in the directory with bun.lock, and + set apps..bun.generateFiles = false for an app whose package.json you + write. +- expectations: what doctor checks afterwards. config lists settings the + evaluated configuration must have, such as each app (exists) and each enabled + module (enable equals true); each entry has exists or equals. files lists + files you will create. commands lists build or test commands that show the + setup works, as argv arrays without shell syntax. +` diff --git a/apps/stackpanel-go/internal/setupagent/reply_test.go b/apps/stackpanel-go/internal/setupagent/reply_test.go index 848cb122..ad36ae2c 100644 --- a/apps/stackpanel-go/internal/setupagent/reply_test.go +++ b/apps/stackpanel-go/internal/setupagent/reply_test.go @@ -1,62 +1,83 @@ package setupagent import ( - "context" - "reflect" "strings" "testing" ) -func TestRunRecoversReplyMissingFinalOuterBrace(t *testing.T) { +func TestParseReplyFindsTheReplyInWhateverTheAgentSent(t *testing.T) { plan := `{"status":"plan","plan":` + validPlan + `}` for _, tt := range []struct { - name, message string - readOnly bool + name, message, status, summary string }{ - {"inspection", plan, true}, - {"setup", complete, false}, + {"exact", plan, StatusPlan, "Add web app"}, + {"fenced with prose", "Here is the plan:\n```json\n" + plan + "\n```\nLet me know.", StatusPlan, "Add web app"}, + {"missing final braces", strings.TrimSuffix(plan, "}}"), StatusPlan, "Add web app"}, + {"cut off in a string", `{"status":"complete","summary":"Configured w`, StatusComplete, "Configured w"}, + {"unknown fields", `{"status":"complete","summary":"Done","confidence":0.9}`, StatusComplete, "Done"}, + {"bare plan", validPlan, StatusPlan, "Add web app"}, + {"status synonym", `{"status":"done","summary":"Done"}`, StatusComplete, "Done"}, + {"example before reply", `Assertions look like {"path":["apps"]}. ` + complete, StatusComplete, "Configured web"}, + {"blocked", `{"status":"blocked","summary":"No access"}`, StatusBlocked, "No access"}, } { t.Run(tt.name, func(t *testing.T) { - agent := fakeAgent(t, "codex") - // The provider completed normally, but omitted only the reply's - // outermost closing brace. Its nested plan remains complete. - t.Setenv("STACKPANEL_TEST_AGENT_EVENTS", codexEvents(strings.TrimSuffix(tt.message, "}"))) - result, err := Run(context.Background(), agent, RunRequest{Dir: t.TempDir(), ReadOnly: tt.readOnly}) + reply, err := ParseReply(tt.message) if err != nil { t.Fatal(err) } - actual, err := ParseReply(result.Message) - if err != nil { - t.Fatalf("completed provider reply was rejected: %v", err) + summary := reply.Summary + if reply.Plan != nil { + summary = reply.Plan.Summary } - expected, err := ParseReply(tt.message) - if err != nil || !reflect.DeepEqual(actual, expected) { - t.Fatalf("recovery changed reply: got %+v, want %+v (%v)", actual, expected, err) + if reply.Status != tt.status || summary != tt.summary { + t.Fatalf("reply = %+v", reply) } }) } + for _, message := range []string{"", "I finished the setup.", `{"path":["apps"]}`, `{"status":"needs_input","questions":[{"prompt":" "}]}`, + strings.Replace(plan, `"exists":true`, `"exists":"yes"`, 1)} { + if _, err := ParseReply(message); err == nil { + t.Fatalf("accepted a reply without usable content: %q", message) + } + } } -func TestReplyRecoveryRejectsIncompleteOrInvalidContents(t *testing.T) { - for _, tt := range []struct{ name, message string }{ - {"nested object", `{"status":"plan","plan":` + strings.TrimSuffix(validPlan, "}")}, - {"nested array", `{"status":"needs_input","questions":[{"id":"framework","prompt":"Framework?","kind":"text","required":true}`}, - {"unfinished string", `{"status":"complete","summary":"Done`}, - {"missing value", `{"status":"complete","summary":`}, - {"trailing comma", `{"status":"complete","summary":"Done",`}, - {"unknown field", `{"status":"complete","summary":"Done","unexpected":true`}, - {"invalid plan", `{"status":"plan","plan":{"summary":"No checks","expectations":{"version":1,"config":[]}}`}, - } { - t.Run(tt.name, func(t *testing.T) { - agent := fakeAgent(t, "codex") - t.Setenv("STACKPANEL_TEST_AGENT_EVENTS", codexEvents(tt.message)) - result, err := Run(context.Background(), agent, RunRequest{Dir: t.TempDir(), ReadOnly: true}) - if err != nil { - t.Fatal(err) - } - if _, err := ParseReply(result.Message); err == nil { - t.Fatal("accepted incomplete or invalid reply") - } - }) +func TestParseReplyKeepsTheUsableParts(t *testing.T) { + reply, err := ParseReply(`{"status":"needs_input","questions":[ + {"prompt":"Name?","kind":"text","required":true}, + {"id":"db","prompt":"Database?","kind":"single","options":["postgres","sqlite","postgres"],"default":["mysql"]}, + {"id":"db","prompt":"Editors?","kind":"multiselect","options":["zed","vscode"],"default":["zed","vim"]}, + {"id":"tone","prompt":"Tone?","kind":"single"}, + {"id":"empty","prompt":""}]}`) + if err != nil { + t.Fatal(err) + } + q := reply.Questions + if len(q) != 4 || q[0].ID != "question-1" || q[0].Kind != "text" || + q[1].Kind != "single" || len(q[1].Options) != 2 || q[1].Default[0] != "postgres" || + q[2].ID != "db-2" || q[2].Kind != "multi" || len(q[2].Default) != 1 || q[2].Default[0] != "zed" || + q[3].Kind != "text" { + t.Fatalf("questions = %+v", q) + } + + reply, err = ParseReply(`{"status":"plan","plan":{"summary":"Plan","services":["postgres","postgres"], + "prepare":[{"id":"deps","dir":"","argv":["bun","install"]},{"id":"deps","dir":".","argv":["bun2nix"]},{"id":"x","dir":"../outside","argv":["sh"]},{"id":"y","dir":".","argv":[]}], + "expectations":{"version":99,"requiredChecks":["guessed-check"], + "config":[{"path":["apps","web"],"exists":true},{"path":["apps","web"],"exists":false},{"path":["apps","web","bun","enable"],"equals":true},{"path":["modules"]},{"path":["enable"],"exists":false,"equals":true}], + "files":["src/index.ts","/etc/passwd","../outside","src/index.ts"], + "commands":[{"dir":"","argv":["bun","test"]},{"id":"lint","scope":"lint","dir":".","argv":["bun","run","lint"]},{"id":"bad","dir":"/","argv":["true"]}]}}}`) + if err != nil { + t.Fatal(err) + } + p := reply.Plan + e := p.Expectations + if len(p.Services) != 1 || len(p.Prepare) != 2 || p.Prepare[0].Dir != "." || p.Prepare[1].ID != "deps-2" { + t.Fatalf("services or preparation = %+v %+v", p.Services, p.Prepare) + } + if e.Version != 1 || len(e.RequiredChecks) != 0 || len(e.Config) != 2 || e.Config[1].Exists == nil || *e.Config[1].Exists || len(e.Config[1].Equals) != 0 { + t.Fatalf("expectations = %+v", e) + } + if len(e.Files) != 1 || len(e.Commands) != 2 || e.Commands[0].ID != "command-1" || e.Commands[0].Scope != "build" || e.Commands[1].Scope != "build" { + t.Fatalf("files or commands = %+v %+v", e.Files, e.Commands) } } diff --git a/apps/stackpanel-go/internal/tui/setup.go b/apps/stackpanel-go/internal/tui/setup.go index 0fd1575a..a3caffd6 100644 --- a/apps/stackpanel-go/internal/tui/setup.go +++ b/apps/stackpanel-go/internal/tui/setup.go @@ -252,6 +252,10 @@ func NewSetupUI(ctx context.Context, interactive bool, out io.Writer) *SetupUI { return u } func (u *SetupUI) Context() context.Context { return u.ctx } + +// Interactive reports whether questions reach a person; otherwise Ask returns +// each question's default. +func (u *SetupUI) Interactive() bool { return u.program != nil } func (u *SetupUI) Close() { defer u.cancel() if u.program != nil { diff --git a/tests/test-agent-setup.sh b/tests/test-agent-setup.sh index 643e49d1..93cf6a74 100755 --- a/tests/test-agent-setup.sh +++ b/tests/test-agent-setup.sh @@ -51,120 +51,105 @@ def clean_environment(): def emit_message(message): rendered = message if isinstance(message, str) else json.dumps(message) - malformed = ((state.name in ("malformed-plan", "malformed-permanent") and phase == "inspection") or - (state.name in ("malformed-complete", "claude-malformed-complete") and phase == "setup") or - (state.name == "malformed-repair" and phase == "repair")) - if malformed: - (state / "valid-reply").write_text(rendered) - rendered = rendered[:-2] # Missing more than the outer brace so format recovery is exercised. + if state.name == "malformed-plan" and phase == "inspection": + rendered = rendered[:-2] # Providers sometimes stop before the last braces. if tool == "claude": - denials = [] if phase in ("inspection", "formatting") else [ + denials = [] if phase == "inspection" else [ {"tool_name": "Glob", "tool_input": {"pattern": "/nix/store/*stackpanel*/**/*.nix"}}, - {"tool_name": "Glob", "tool_input": {"pattern": "/outside/skills/**/SKILL.md"}}] + {"tool_name": "Bash", "tool_input": {"command": "private-command-input"}}] print(json.dumps({"type": "result", "subtype": "success", "is_error": False, "result": rendered, "permission_denials": denials})) return + print("Reading prompt from stdin...") print(json.dumps({"type": "item.completed", "item": { "type": "agent_message", "text": rendered}})) print(json.dumps({"type": "turn.completed"})) if tool in ("codex", "claude"): assert not os.environ.get("AGENT_SETUP_TEST_DISABLE_AGENT"), "verification retry should not need a coding agent" - if "--version" in args: - print("codex-cli 0.100.0") - elif "--help" in args: - print("exec --json --sandbox read-only workspace-write --color --config -c --print --output-format stream-json --permission-mode plan acceptEdits --strict-mcp-config --mcp-config --settings") + if args == ["--version"]: + print(tool + " 1.0.0") + sys.exit(0) + clean_environment() + root = Path.cwd() + (state / "target-root").write_text(str(root)) + fixture_config = json.loads((state / "config.json").read_text()) + fixture_config["projectRoot"] = str(root) + (state / "config.json").write_text(json.dumps(fixture_config)) + prompt = sys.stdin.read() + phase = next(name for name in ("inspection", "setup", "repair") if "phase " + name + "." in prompt) + record({"tool": tool, "phase": phase, "args": args}) + if tool == "codex": + assert "--json" in args and args[-1] == "-" + assert args[args.index("--sandbox") + 1] == ("read-only" if phase == "inspection" else "workspace-write") else: - clean_environment() - root = Path.cwd() - (state / "target-root").write_text(str(root)) - fixture_config = json.loads((state / "config.json").read_text()) - fixture_config["projectRoot"] = str(root) - (state / "config.json").write_text(json.dumps(fixture_config)) - prompt = sys.stdin.read() - phase = "formatting" if "This is a formatting retry only." in prompt else next( - name for name in ("inspection", "setup", "repair") if "phase " + name + "." in prompt) - record({"tool": tool, "phase": phase, "args": args}) - if tool == "codex": - assert "--json" in args and args[-1] == "-" - sandbox = args[args.index("--sandbox") + 1] - assert sandbox == ("read-only" if phase in ("inspection", "formatting") else "workspace-write") - else: - assert args[args.index("--permission-mode") + 1] == ("plan" if phase in ("inspection", "formatting") else "acceptEdits") - assert args[args.index("--tools") + 1] == ("Read,Glob,Grep" if phase in ("inspection", "formatting") else "Read,Glob,Grep,Edit,Write") - if phase == "formatting": - assert "Do not use tools" in prompt - original = (state / "valid-reply").read_text() - if state.name == "malformed-permanent" and not (state / "retry").exists(): - original = original[:-2] - emit_message(original) - sys.exit(0) - assert '"optionSchema"' in prompt and '.tooling.dev.package"' in prompt, "agent did not receive pinned app schema" - if phase == "inspection": - if (new_repo or state.name == "resume-inspection") and '"values":["Python"]' not in prompt: - if new_repo: - assert "Setup mode: new" in prompt - emit_message({"status": "needs_input", "questions": [{ - "id": "language", "prompt": "Language?", "kind": "single", - "options": ["Python"], "default": ["Python"], "required": True}]}) - sys.exit(0) - if state.name == "resume-inspection" and not (state / "interrupted").exists(): - (state / "interrupted").touch() - print("fixture agent disconnected after receiving answers", file=sys.stderr) - sys.exit(1) - if state.name.startswith("resume-") and (state / "retry").exists(): - assert "Resuming saved onboarding" in prompt - expected = { - "version": 1, "config": [{"path": ["apps", "web"], "exists": True}], - "requiredChecks": []} - plan = {"summary": "Configure the web app", "expectations": expected} + assert args[args.index("--permission-mode") + 1] == ("plan" if phase == "inspection" else "acceptEdits") + assert args[args.index("--tools") + 1] == ("Read,Glob,Grep" if phase == "inspection" else "Read,Glob,Grep,Edit,Write") + assert '"optionSchema"' in prompt and '.tooling.dev.package"' in prompt, "agent did not receive pinned app schema" + retry = (state / "retry").exists() + if retry: + assert "Resuming saved onboarding" in prompt + if phase == "inspection": + if (new_repo or state.name == "resume-inspection") and '"values":["Python"]' not in prompt: if new_repo: - expected.update({"files": ["app.py", "test_app.py", "deps.lock"], "commands": [{ - "id": "app-test", "scope": "build", "dir": ".", "argv": [sys.executable, "test_app.py"]}]}) - plan["prepare"] = [{"id": "deps", "dir": ".", "argv": [ - sys.executable, "-c", "open('deps.lock', 'w').write('locked\\n')"]}] - if state.name == "prepare-repair": - plan["prepare"] = [{"id": "deps", "dir": ".", "argv": [sys.executable, "-c", - "import os, sys\nmarker = os.environ['AGENT_SETUP_TEST_STATE'] + '/prepare-attempted'\n" - "if not os.path.exists(marker):\n open(marker, 'w').close()\n sys.exit('fixture registry error')"]}] - emit_message({"status": "plan", "plan": plan}) + assert "Setup mode: new" in prompt + emit_message({"status": "needs_input", "questions": [{ + "id": "language", "prompt": "Language?", "kind": "single", + "options": ["Python"], "default": ["Python"], "required": True}]}) + sys.exit(0) + if state.name == "resume-inspection" and not retry: + print("fixture agent disconnected after receiving answers", file=sys.stderr) + sys.exit(1) + if state.name == "prose-replies": + emit_message("I will add the web app to .stack/config.nix.") + sys.exit(0) + expected = {"version": 1, "config": [{"path": ["apps", "web"], "exists": True}]} + plan = {"summary": "Configure the web app", "expectations": expected} + if new_repo: + expected.update({"files": ["app.py", "test_app.py", "deps.lock"], "commands": [{ + "id": "app-test", "scope": "build", "dir": ".", "argv": [sys.executable, "test_app.py"]}]}) + plan["prepare"] = [{"id": "deps", "dir": ".", "argv": [ + sys.executable, "-c", "open('deps.lock', 'w').write('locked\\n')"]}] + if state.name == "prepare-repair": + plan["prepare"] = [{"id": "deps", "dir": ".", "argv": [sys.executable, "-c", + "import os, sys\nmarker = os.environ['AGENT_SETUP_TEST_STATE'] + '/prepare-attempted'\n" + "if not os.path.exists(marker):\n open(marker, 'w').close()\n sys.exit('fixture registry error')"]}] + emit_message({"status": "plan", "plan": plan}) + sys.exit(0) + assert "Do not run nix" in prompt and "do not edit flake.lock" in prompt + assert (root / "flake.nix").is_file(), "host did not scaffold before the agent" + assert (root / ".stack/config.nix").is_file(), "host did not write template configuration" + if new_repo: + assert not (root / "flake.lock").exists(), "locking must wait for the app's input edits" + (root / "app.py").write_text('def greet(name):\n return "Hello, " + name\n') + assert not (root / "deps.lock").exists(), "lockfiles belong to host preparation" + (root / "test_app.py").write_text('from app import greet\nassert greet("world") == "Hello, world"\n' + 'assert open("deps.lock").read() == "locked\\n"\n') + (root / ".stack/onboarded.nix").write_text("{ onboarded = true; }\n") + if state.name == "edits-user-files": + (root / "unrelated.txt").write_text("the agent replaced this dirty file\n") + with (root / "flake.nix").open("a") as flake: + flake.write("# edited by the agent\n") + if phase == "repair": + if state.name == "devshell-repair": + assert "fixture devshell error" in prompt + elif state.name == "prepare-repair": + assert "prepare command deps failed" in prompt and "fixture registry error" in prompt else: - assert "Do not invoke stack setup, nix, direnv" in prompt - assert "Do not create or edit flake.lock yourself" in prompt - assert (root / "flake.nix").is_file(), "host did not scaffold before the sandboxed agent" - assert (root / ".stack/config.nix").is_file(), "host did not write template configuration" - if new_repo: - assert not (root / "flake.lock").exists(), "locking must wait for the app's input edits" - (root / "app.py").write_text('def greet(name):\n return "Hello, " + name\n') - assert not (root / "deps.lock").exists(), "lockfiles belong to host preparation" - (root / "test_app.py").write_text('from app import greet\nassert greet("world") == "Hello, world"\n' - 'assert open("deps.lock").read() == "locked\\n"\n') - (root / ".stack/onboarded.nix").write_text("{ onboarded = true; }\n") - if state.name == "user-edit-violation": - (root / "unrelated.txt").write_text("model overwrote existing user edits\n") - if phase == "repair" and state.name == "lock-repair": - assert "fixture lock error" in prompt - elif phase == "repair" and state.name == "prepare-repair": - assert "host preparation deps failed" in prompt and "fixture registry error" in prompt - elif phase == "repair": - assert "config:apps.web" in prompt, "repair did not receive doctor failure" - # Try to weaken the external file. The orchestrator must replace - # it from the original in-memory contract before the next doctor. - path = Path((state / "expectations-path").read_text()) - path.write_text(json.dumps({"version": 1, "config": [ - {"path": ["enable"], "equals": True}], "requiredChecks": []})) - if state.name in ("resume-agent", "resume-new", "resume-tmp") and not (state / "interrupted").exists(): - (state / "interrupted").touch() - emit_message({"status": "blocked", "summary": "fixture interrupted after partial output"}) - sys.exit(0) - if state.name.startswith("resume-") and (state / "retry").exists(): - assert "Resuming saved onboarding" in prompt - if new_repo or state.name == "resume-inspection": - assert '\"values\":[\"Python\"]' in prompt - configured = state.name in ("malformed-plan", "malformed-complete", "malformed-permanent", "claude-malformed-complete") or phase == "repair" and state.name == "malformed-repair" or new_repo or state.name in ("resume-agent", "resume-inspection", "resume-doctor", "resume-kill", "resume-complete") or phase == "repair" and state.name in ("repair-success", "lock-repair", "prepare-repair", "claude-read-denial") or state.name == "resume-contract" and phase == "repair" and (state / "retry").exists() - (root / ".stack/config.nix").write_text( - "{ enable = true; " + ("apps.web = {}; " if configured else "") + "}\n") - emit_message({"status": "complete", "summary": "Setup is complete"}) + assert "apps.web: expected exists=true" in prompt, "repair did not receive doctor's findings" + # Weakening the checks file cannot help: the host writes it again + # from the accepted plan before each doctor run. + path = Path((state / "expectations-path").read_text()) + path.write_text(json.dumps({"version": 1, "config": [{"path": ["enable"], "equals": True}]})) + if state.name in ("resume-agent", "resume-new", "resume-tmp") and not retry: + print("fixture agent crashed after partial output", file=sys.stderr) + sys.exit(1) + # Scenarios that need a repair leave the app out until their repair turn. + configured = not (phase == "setup" and state.name in ("repair-success", "claude-denials")) + if state.name in ("permanent-failure", "resume-user-fix"): + configured = False + (root / ".stack/config.nix").write_text("{ enable = true; " + ("apps.web = {}; " if configured else "") + "}\n") + emit_message("All done." if state.name == "prose-replies" else {"status": "complete", "summary": "Setup is complete"}) elif tool == "write-files": record({"tool": tool}) elif args[:2] == ["flake", "metadata"]: @@ -172,19 +157,6 @@ elif args[:2] == ["flake", "metadata"]: record({"tool": tool, "args": args}) print(json.dumps({"url": "github:fixture/stackpanel/" + "a" * 40, "locked": {"rev": "a" * 40}})) -elif args[:2] == ["flake", "lock"]: - clean_environment() - assert args[:4] == ["flake", "lock", ".", "--output-lock-file"] and len(args) == 5 - output = Path(args[4]) - assert not output.is_relative_to(root), "lock output must not let Nix stage repository files" - record({"tool": tool, "args": args, "lock": True}) - marker = state / "lock-attempted" - if state.name == "lock-repair" and not marker.exists(): - marker.touch() - print("fixture lock error", file=sys.stderr) - sys.exit(1) - lock = root / "flake.lock" - output.write_bytes(lock.read_bytes() if lock.exists() else b'{"version":7,"root":"root","nodes":{"root":{}}}\n') elif args and args[0] == "eval": record({"tool": tool, "args": args}) if any("#lib.initTemplates.default" in arg for arg in args): @@ -194,7 +166,6 @@ elif args and args[0] == "eval": elif "--expr" in args and "flake.lib.getOptions" in args[-1]: clean_environment() assert "--impure" not in args - assert "--no-update-lock-file" in args and "--no-write-lock-file" in args assert 'builtins.getFlake "github:fixture/stackpanel/' + "a" * 40 + '"' in args[-1] print(json.dumps([{"name": "apps..tooling.dev.package", "type": "package", "description": "Tool binary package"}])) elif any(".stackpanelConfig" in arg for arg in args): @@ -210,23 +181,29 @@ elif args and args[0] == "build": record({"tool": tool, "args": args}) elif args and args[0] == "develop": clean_environment() - assert (root / "flake.lock").is_file(), "host did not lock before devshell entry" - (root / ".stack/gen/codegen").mkdir(parents=True, exist_ok=True) - (root / ".stack/gen/codegen/env-manifest.json").write_text(json.dumps({ - "schemaVersion": 1, "dataRoot": ".stack/data", "targets": []})) - assert args[:5] == ["develop", ".", "--no-update-lock-file", "--no-write-lock-file", "--command"] + assert args[:3] == ["develop", ".", "--command"], args index = subprocess.check_output(["git", "ls-files", "--stage", "--debug", "-z", "--", ".stack/onboarded.nix"], cwd=root) flags = int(index.split(b"\tflags: ")[-1].strip(), 16) assert flags & 0x20000000, "new Nix input is not visible through intent-to-add" - command = args[5:] + command = args[3:] doctor = "--expectations" in command - record({"tool": tool, "args": args, - "stage": "doctor" if doctor else "prepare" if "stackpanel-prepare" in command else "reconcile"}) - if doctor and state.name in ("resume-doctor", "resume-kill") and not (state / "interrupted").exists(): + stage = "doctor" if doctor else "prepare" if "stackpanel-prepare" in command else "reconcile" + record({"tool": tool, "args": args, "stage": stage}) + marker = state / "devshell-failed" + if state.name == "devshell-repair" and not marker.exists(): + marker.touch() + print("fixture devshell error", file=sys.stderr) + sys.exit(1) + # Like Nix, entering the shell creates a missing lock file. + if not (root / "flake.lock").exists(): + (root / "flake.lock").write_text('{"version":7,"root":"root","nodes":{"root":{}}}\n') + (root / ".stack/gen/codegen").mkdir(parents=True, exist_ok=True) + (root / ".stack/gen/codegen/env-manifest.json").write_text(json.dumps({ + "schemaVersion": 1, "dataRoot": ".stack/data", "targets": []})) + if doctor and state.name in ("resume-doctor", "resume-kill") and not (state / "retry").exists(): import signal import time - (state / "interrupted").touch() os.kill(os.getppid(), signal.SIGKILL if state.name == "resume-kill" else signal.SIGTERM) if state.name == "resume-kill": sys.exit(0) @@ -236,20 +213,18 @@ elif args and args[0] == "develop": expected_path = Path(command[command.index("--expectations") + 1]) assert not expected_path.is_relative_to(root) expected = json.loads(expected_path.read_text()) - previous_path = state / "expectations-path" - required = ["fixture-repo", "fixture-build"] if previous_path.exists() else [] - contract = {"version": 1, "config": [ + contract = {"version": 1, "requiredChecks": None, "config": [ {"path": ["apps", "web"], "exists": True}, - {"path": ["enable"], "equals": True}], "requiredChecks": required} + {"path": ["enable"], "equals": True}]} + if state.name == "prose-replies": + contract["config"] = [{"path": ["enable"], "equals": True}] if new_repo: contract.update({"files": ["app.py", "test_app.py", "deps.lock"], "commands": [{ "id": "app-test", "scope": "build", "dir": ".", "argv": [sys.executable, "test_app.py"]}]}) for path in contract["files"]: subprocess.run(["git", "ls-files", "--error-unmatch", path], cwd=root, check=True, stdout=subprocess.DEVNULL) assert expected == contract, expected - if previous_path.exists() and not state.name.startswith("resume-"): - assert previous_path.read_text() == str(expected_path) - previous_path.write_text(str(expected_path)) + (state / "expectations-path").write_text(str(expected_path)) shell_env = dict(os.environ) shell_env.update({"STACKPANEL_ROOT": str(root), "STACKPANEL_STATE_DIR": str(root / ".stack/profile"), @@ -271,16 +246,16 @@ fake.chmod(0o755) for name in ("codex", "claude", "nix", "write-files"): (bin_dir / name).symlink_to(fake.name) -for name, expected_success in (("repair-success", True), ("permanent-failure", False), - ("user-edit-violation", False), ("new-repository", True), - ("lock-repair", True), ("prepare-repair", True), ("claude-read-denial", True), - ("claude-read-denial-failure", False), ("resume-agent", False), - ("resume-inspection", False), ("resume-doctor", False), - ("resume-new", False), ("resume-tmp", False), ("resume-contract", False), - ("resume-kill", False), ("resume-user-fix", False), ("resume-complete", True), ("malformed-plan", True), - ("malformed-complete", True), ("malformed-repair", True), - ("malformed-permanent", False), ("claude-malformed-complete", True)): - warning_only = name in ("permanent-failure", "claude-read-denial-failure", "resume-contract", "resume-user-fix") +# name -> (setup exits 0, setup finishes with doctor warnings) +scenarios = { + "repair-success": (True, False), "permanent-failure": (True, True), "edits-user-files": (True, False), + "new-repository": (True, False), "devshell-repair": (True, False), "prepare-repair": (True, False), + "claude-denials": (True, False), "malformed-plan": (True, False), "prose-replies": (True, False), + "resume-agent": (False, False), "resume-inspection": (False, False), "resume-doctor": (False, False), + "resume-kill": (False, False), "resume-new": (False, False), "resume-tmp": (False, False), + "resume-user-fix": (True, True), "resume-complete": (True, False), +} +for name, (expected_success, warnings) in scenarios.items(): state = work / name root = state / "repo" (root / ".stack/gen/codegen").mkdir(parents=True) @@ -320,6 +295,10 @@ for name, expected_success in (("repair-success", True), ("permanent-failure", F {"id": "fixture-runtime", "module": "fixture", "name": "Excluded runtime", "scope": "runtime", "enabled": True, "type": "HEALTHCHECK_TYPE_SCRIPT", "severity": "HEALTHCHECK_SEVERITY_CRITICAL", "scriptPath": "/absent-runtime-probe"}, + # Doctor cannot run this type here; setup must not treat it as a problem. + {"id": "fixture-unsupported", "module": "fixture", "name": "Unsupported probe", + "scope": "repo", "enabled": True, "type": "HEALTHCHECK_TYPE_NIX", + "severity": "HEALTHCHECK_SEVERITY_CRITICAL"}, ] (state / "checks.json").write_text(json.dumps(checks)) (state / "config.json").write_text(json.dumps({"version": 1, "projectName": name, @@ -347,198 +326,143 @@ for name, expected_success in (("repair-success", True), ("permanent-failure", F arguments += ["--tmp"] invocation_dir = state if name in ("new-repository", "resume-new", "resume-tmp") else root result = subprocess.run(arguments, cwd=invocation_dir, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=120) (state / "output.log").write_text(result.stdout + result.stderr) - if (result.returncode == 0) != (expected_success or warning_only): + if (result.returncode == 0) != expected_success: raise AssertionError(name + " produced unexpected status:\n" + result.stdout + result.stderr) - if warning_only: - assert "verification warnings" in result.stderr and "doctor --onboarding" in result.stderr - assert "Error: repository onboarding remains unverified" not in result.stderr - # Rerun the saved contract without an agent; checks still exit nonzero. - manifests = [p for p in (state / "user-config/stackpanel/setup").glob("*.json") if not p.name.startswith("tmp-")] - manifest_bytes = manifests[0].read_bytes() + assert ("stack doctor still reports" in result.stderr) == warnings, result.stderr + root = Path((state / "target-root").read_text()) + calls = [json.loads(line) for line in (state / "calls.jsonl").read_text().splitlines()] + phases = [call["phase"] for call in calls if call["tool"] == provider] + stages = [call["stage"] for call in calls if "stage" in call] + manifests = [p for p in (state / "user-config/stackpanel/setup").glob("*.json") if not p.name.startswith("tmp-")] + assert len(manifests) == 1, manifests + manifest_path = manifests[0] + saved = json.loads(manifest_path.read_text()) + assert saved["root"] == str(root) + + if name == "permanent-failure": + # The saved checks still run without an agent, and still fail. doctor_env = dict(env) doctor_env.update({"STACKPANEL_ROOT": str(root), "STACKPANEL_STATE_DIR": str(root / ".stack/profile"), "STACKPANEL_CONFIG_JSON": str(state / "config.json"), "STACKPANEL_FILES_MANIFEST": str(state / "files.json"), "STACKPANEL_FILES_PREFLIGHT_MANIFEST": str(state / "fileops.json"), "AGENT_SETUP_TEST_DISABLE_AGENT": "1"}) + manifest_bytes = manifest_path.read_bytes() doctor = subprocess.run([binary, "doctor", "--onboarding", "--json"], cwd=root, env=doctor_env, capture_output=True, text=True, timeout=30) assert doctor.returncode == 1, doctor.stdout + doctor.stderr assert any(f["id"] == "config:apps.web" for f in json.loads(doctor.stdout)["findings"]) - assert "Running check fixture-repo" in doctor.stderr - assert manifests[0].read_bytes() == manifest_bytes, "doctor changed saved setup state" + assert manifest_path.read_bytes() == manifest_bytes, "doctor changed saved setup state" - calls = [json.loads(line) for line in (state / "calls.jsonl").read_text().splitlines()] - if name.startswith("malformed-") or name == "claude-malformed-complete": - manifests = list((state / "user-config/stackpanel/setup").glob("*.json")) - assert len(manifests) == 1 - saved = json.loads(manifests[0].read_text()) - if name == "malformed-permanent": - assert saved["pendingReply"]["message"] == (state / "valid-reply").read_text()[:-2] - assert "after one formatting retry" in result.stderr - (state / "retry").touch() - retry = subprocess.run(arguments, cwd=root, env=env, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, timeout=30) - assert retry.returncode == 0, retry.stdout + retry.stderr - saved = json.loads(manifests[0].read_text()) - calls = [json.loads(line) for line in (state / "calls.jsonl").read_text().splitlines()] - assert saved["stage"] == "complete" and not saved.get("pendingReply"), saved["stage"] - phases = [call["phase"] for call in calls if call["tool"] == provider] - expected = { - "malformed-plan": ["inspection", "formatting", "setup"], - "malformed-complete": ["inspection", "setup", "formatting"], - "claude-malformed-complete": ["inspection", "setup", "formatting"], - "malformed-repair": ["inspection", "setup", "repair", "formatting"], - "malformed-permanent": ["inspection", "formatting", "formatting", "setup"], - }[name] - assert phases == expected, phases - reports = sorted(state.glob("doctor-*.json")) - assert len(reports) == (2 if name == "malformed-repair" else 1) - report = json.loads(reports[-1].read_text()) - assert not [finding for finding in (report.get("findings") or []) if finding["severity"] == "error"] - assert git("diff", "--cached", "--", "unrelated.txt") == staged - assert git("diff", "--", "unrelated.txt") == unstaged - print("PASS: " + name + " (read-only format recovery, no repeated edits, real doctor)") - continue if name.startswith("resume-"): - root = Path((state / "target-root").read_text()) - manifests = list((state / "user-config/stackpanel/setup").glob("*.json")) - manifests = [path for path in manifests if not path.name.startswith("tmp-")] - assert len(manifests) == 1, manifests - manifest_path = manifests[0] - saved = json.loads(manifest_path.read_text()) - assert saved["root"] == str(root) assert saved["stage"] == {"resume-agent": "apply", "resume-new": "apply", "resume-tmp": "apply", - "resume-inspection": "inspection", "resume-doctor": "verify", "resume-kill": "verify", "resume-contract": "repair", "resume-user-fix": "repair", "resume-complete": "complete"}[name], saved["stage"] - if name not in ("resume-kill", "resume-complete"): + "resume-inspection": "inspection", "resume-doctor": "verify", "resume-kill": "verify", + "resume-user-fix": "complete", "resume-complete": "complete"}[name], saved["stage"] + if not expected_success and name != "resume-kill": assert "Rerun the same" in result.stderr, result.stderr - if name == "resume-contract": - assert saved["plan"]["expectations"]["requiredChecks"] == ["fixture-repo", "fixture-build"] if name in ("resume-inspection", "resume-new", "resume-tmp"): assert saved["request"]["answers"] == [{"id": "language", "values": ["Python"]}] assert len(saved["conversation"]) == 1 - initial_log = (state / "agent.jsonl").read_bytes() if name == "resume-agent" else None - phases_before = [call["phase"] for call in calls if call["tool"] == provider] - if name in ("resume-contract", "resume-new", "resume-inspection"): - # Older manifests predate schema context. Upgrade without restarting - # inspection, repeating answers or replacing the accepted contract. + if name in ("resume-new", "resume-inspection"): + # Manifests from before the schema context gain it without + # repeating answers. brief = json.loads(saved["request"]["context"]) del brief["optionSchema"] saved["request"]["context"] = json.dumps(brief) manifest_path.write_text(json.dumps(saved)) + initial_log = (state / "agent.jsonl").read_bytes() if name == "resume-agent" else None (state / "retry").touch() if name == "resume-user-fix": (root / ".stack/config.nix").write_text("{ enable = true; apps.web = {}; }\n") if name in ("resume-doctor", "resume-kill", "resume-user-fix", "resume-complete"): env["AGENT_SETUP_TEST_DISABLE_AGENT"] = "1" # Omitted configuration flags inherit the original selection. - retry_args = [arg for arg in arguments if arg != "github:fixture/stackpanel" and arg != "--flake"] + retry_args = [arg for arg in arguments if arg not in ("github:fixture/stackpanel", "--flake")] retry = subprocess.run(retry_args, cwd=invocation_dir, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30) + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=120) assert retry.returncode == 0, name + " retry failed:\n" + retry.stdout + retry.stderr - assert "Resuming " in retry.stderr + assert "Resuming " in retry.stderr and "stack doctor still reports" not in retry.stderr, retry.stderr if name == "resume-agent": assert (state / "agent.jsonl").read_bytes() == initial_log assert len(list(state.glob("agent.jsonl.resume-*.jsonl"))) == 1 completed = json.loads(manifest_path.read_text()) - assert completed["stage"] == "complete", completed["stage"] - assert completed["root"] == str(root) + assert completed["stage"] == "complete" and completed["root"] == str(root), completed assert not completed.get("lastError") calls = [json.loads(line) for line in (state / "calls.jsonl").read_text().splitlines()] - phases = [call["phase"] for call in calls if call["tool"] == provider] - if name in ("resume-doctor", "resume-kill", "resume-user-fix", "resume-complete"): - assert phases == phases_before, "agent ran again after completing generation" - elif name == "resume-contract": - assert phases == phases_before + ["repair"], phases - assert completed["plan"]["expectations"] == saved["plan"]["expectations"] - elif name == "resume-inspection": - assert phases == phases_before + ["inspection", "setup"], phases - assert len(completed["conversation"]) == 1, "answered question was repeated" - else: - assert phases == phases_before + ["setup"], phases + after = [call["phase"] for call in calls if call["tool"] == provider] + expected_phases = {"resume-inspection": ["inspection", "setup"], "resume-agent": ["setup"], + "resume-new": ["setup"], "resume-tmp": ["setup"]}.get(name, []) + assert after == phases + expected_phases, after + if name == "resume-inspection": + assert len(completed["conversation"]) == 1, "answered question was asked again" if name == "resume-tmp": assert retry.stdout.strip() == str(root), retry.stdout - shutil.rmtree(root) - elif name != "resume-new": - assert git("diff", "--cached", "--", "unrelated.txt") == staged - assert git("diff", "--", "unrelated.txt") == unstaged if name == "resume-complete": - # A Pi-backed session can verify current files without provider - # credentials, just like the CLI backends. Doctor never calls Pi. + # A Pi-backed session re-verifies current files without provider + # credentials, like the CLI backends. Doctor never calls Pi. completed["agent"] = "pi" completed["options"]["agentModel"] = "openai/gpt-5" completed["pi"] = {"model": "openai/gpt-5"} manifest_path.write_text(json.dumps(completed)) pi_env = dict(env, OPENAI_API_KEY="", ANTHROPIC_API_KEY="") - pi_args = ["--experimental-agent=pi" if arg.startswith("--experimental-agent=") else arg - for arg in retry_args] + pi_args = ["--experimental-agent=pi" if arg.startswith("--experimental-agent=") else arg for arg in retry_args] pi_retry = subprocess.run(pi_args, cwd=invocation_dir, env=pi_env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30) + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=120) assert pi_retry.returncode == 0, pi_retry.stdout + pi_retry.stderr print("PASS: pi-verification-resume (real doctor, no API key or coding agent)") + if name not in ("resume-new", "resume-tmp"): + assert git("diff", "--cached", "--", "unrelated.txt") == staged + if name == "resume-tmp": + shutil.rmtree(root) print("PASS: " + name + " (separate-process retry, saved choices, current-repository doctor)") continue + + assert saved["stage"] == "complete", saved["stage"] + reports = [json.loads(path.read_text()) for path in sorted(state.glob("doctor-*.json"))] + for report in reports: + assert set(report["reconcilers"]) >= {"codegen", "files", "fileops", "checks", "verification"} + statuses = {entry["id"]: entry["status"] for entry in report["checkResults"]} + assert statuses.get("fixture-repo") == "pass" and statuses.get("fixture-build") == "pass", statuses + assert statuses.get("fixture-unsupported") == "skipped", statuses + visibility = git("ls-files", "--stage", "--debug", "-z", "--", ".stack/onboarded.nix") + assert int(visibility.split(b"\tflags: ")[-1].strip(), 16) & 0x20000000, "new Nix input is not visible to Nix" + if name != "new-repository": + assert git("diff", "--cached", "--", "unrelated.txt") == staged, "setup changed the user's staged changes" if provider == "claude": - assert "Warning: Claude read permission denied: Glob" in result.stderr - assert "doctor still verifies" in result.stderr - if name != "user-edit-violation": - expected_locks = 1 if name == "new-repository" else 2 - assert sum(bool(call.get("lock")) for call in calls) == expected_locks + assert "Claude was denied Glob" in result.stderr and "denied Bash" in result.stderr, result.stderr + assert "private-command-input" not in result.stderr if name == "new-repository": - assert [call["phase"] for call in calls if call["tool"] == provider] == ["inspection", "inspection", "setup"] - assert [call["stage"] for call in calls if "stage" in call] == ["reconcile", "prepare", "doctor"] - report = json.loads((state / "doctor-1.json").read_text()) - statuses = {entry["id"]: entry["status"] for entry in report["checkResults"]} - assert statuses == {"fixture-repo": "pass", "fixture-build": "pass", "file:app.py": "pass", - "file:test_app.py": "pass", "file:deps.lock": "pass", "acceptance:app-test": "pass"}, statuses + assert phases == ["inspection", "inspection", "setup"], phases + assert stages == ["reconcile", "prepare", "doctor"], stages + statuses = {entry["id"]: entry["status"] for entry in reports[0]["checkResults"]} + assert {statuses[key] for key in ("file:app.py", "file:test_app.py", "file:deps.lock", "acceptance:app-test")} == {"pass"}, statuses assert "Host preparation" in result.stderr and "Preparing deps" in result.stderr assert "turn.completed" not in result.stderr and "item.completed" not in result.stderr print("PASS: new-repository (question round, empty target, host preparation, source visibility and real acceptance command)") continue - visibility = git("ls-files", "--stage", "--debug", "-z", "--", ".stack/onboarded.nix") - if expected_success or warning_only: - flags = int(visibility.split(b"\tflags: ")[-1].strip(), 16) - assert flags & 0x20000000, "successful onboarding must remain evaluable" - else: - assert visibility == b"", "failed onboarding left its own index entries behind" - assert (root / ".stack/onboarded.nix").is_file(), "cleanup deleted agent worktree output" - assert git("diff", "--cached", "--", "unrelated.txt") == staged - if name == "lock-repair": - assert [call["phase"] for call in calls if call["tool"] == provider] == ["inspection", "setup", "repair"] - assert [call["stage"] for call in calls if "stage" in call] == ["reconcile", "doctor"] - assert not (state / "doctor-2.json").exists(), "doctor ran before the lock was repaired" - print("PASS: lock-repair (host Nix error reached the single repair attempt)") - continue - if name == "prepare-repair": - assert [call["phase"] for call in calls if call["tool"] == provider] == ["inspection", "setup", "repair"] - assert [call["stage"] for call in calls if "stage" in call] == ["reconcile", "prepare", "reconcile", "prepare", "doctor"] - assert not (state / "doctor-2.json").exists(), "doctor ran before host preparation succeeded" - print("PASS: prepare-repair (failed host preparation reached the single repair attempt)") - continue - if name == "user-edit-violation": - assert [call["phase"] for call in calls if call["tool"] == provider] == ["inspection", "setup"] - assert not any("stage" in call for call in calls), "reconciliation ran after user edits were overwritten" - assert "preexisting user edits" in result.stderr and "nothing was reverted" in result.stderr - assert (root / "unrelated.txt").read_text() == "model overwrote existing user edits\n" - print("PASS: user-edit-violation (stopped before reconciliation, no automatic revert)") - continue - assert [call["phase"] for call in calls if call["tool"] == provider] == ["inspection", "setup", "repair"] - assert [call["stage"] for call in calls if "stage" in call] == ["reconcile", "doctor", "reconcile", "doctor"] - reports = [json.loads(path.read_text()) for path in sorted(state.glob("doctor-*.json"))] - assert len(reports) == 2 - for report in reports: - assert set(report["reconcilers"]) >= {"codegen", "files", "fileops", "checks", "verification"} - statuses = {entry["id"]: entry["status"] for entry in report["checkResults"]} - assert statuses == {"fixture-repo": "pass", "fixture-build": "pass"}, statuses - assert not report.get("changes"), report - assert any(finding["id"] == "config:apps.web" for finding in reports[0]["findings"]) - last_errors = [finding for finding in (reports[-1].get("findings") or []) if finding["severity"] == "error"] - assert bool(last_errors) != expected_success, last_errors - assert git("diff", "--cached", "--", "unrelated.txt") == staged - assert git("diff", "--", "unrelated.txt") == unstaged - assert (root / "flake.nix").read_bytes() == original_flake + expected = { + "repair-success": (["inspection", "setup", "repair"], ["reconcile", "doctor", "reconcile", "doctor"]), + "permanent-failure": (["inspection", "setup", "repair"], ["reconcile", "doctor", "reconcile", "doctor"]), + "claude-denials": (["inspection", "setup", "repair"], ["reconcile", "doctor", "reconcile", "doctor"]), + "devshell-repair": (["inspection", "setup", "repair"], ["reconcile", "reconcile", "doctor"]), + "prepare-repair": (["inspection", "setup", "repair"], ["reconcile", "prepare", "reconcile", "prepare", "doctor"]), + "edits-user-files": (["inspection", "setup"], ["reconcile", "doctor"]), + "malformed-plan": (["inspection", "setup"], ["reconcile", "doctor"]), + "prose-replies": (["inspection", "setup"], ["reconcile", "doctor"]), + }[name] + assert (phases, stages) == expected, (phases, stages) + if len(reports) == 2: + assert any(finding["id"] == "config:apps.web" for finding in reports[0]["findings"]) + last_errors = [f for f in (reports[-1].get("findings") or []) if f["severity"] == "error" and "fixture-unsupported" not in f["id"]] + assert bool(last_errors) == warnings, last_errors assert (root / "flake.lock").read_bytes() == original_lock + if name == "edits-user-files": + assert (root / "unrelated.txt").read_text() == "the agent replaced this dirty file\n" + assert (root / "flake.nix").read_bytes() == original_flake + b"# edited by the agent\n" + else: + assert git("diff", "--", "unrelated.txt") == unstaged + assert (root / "flake.nix").read_bytes() == original_flake assert (root / ".stack/gen/codegen/modules.json").is_file() assert (root / "packages/gen/env/src/runtime/generated-payloads/registry.ts").is_file() - print("PASS: " + name + " (real doctor, fixed expectations, existing Git changes preserved)") + print("PASS: " + name + " (real doctor, accepted plan's checks)") PY