@osfactory/har 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/README.md +43 -2
  2. package/dist/index.js +1240 -586
  3. package/dist/templates/agent-skills/har-maintain.md +36 -0
  4. package/dist/templates/agent-skills/har-wt.md +47 -0
  5. package/dist/templates/agent-skills/setup-har.md +70 -0
  6. package/dist/templates/agent-skills/skills.manifest.json +48 -0
  7. package/dist/templates/claude-worktree-guard.sh.template +38 -0
  8. package/dist/templates/har-boilerplate/CLAUDE.agent.md +4 -2
  9. package/dist/templates/har-boilerplate/STAGES.md +91 -0
  10. package/dist/templates/har-boilerplate/agent-slot.sh +35 -6
  11. package/dist/templates/har-boilerplate/stages/README.sh +7 -5
  12. package/dist/templates/har-boilerplate/verify.sh +9 -2
  13. package/dist/templates/har-boilerplate-cli/CLAUDE.agent.md +3 -1
  14. package/dist/templates/har-boilerplate-cli/STAGES.md +91 -0
  15. package/dist/templates/har-boilerplate-cli/agent-slot.sh +35 -6
  16. package/dist/templates/har-boilerplate-cli/stages/README.sh +7 -5
  17. package/dist/templates/har-boilerplate-cli/verify.sh +9 -2
  18. package/dist/templates/har-boilerplate-ios/CLAUDE.agent.md +2 -0
  19. package/dist/templates/har-boilerplate-ios/STAGES.md +91 -0
  20. package/dist/templates/har-boilerplate-ios/agent-slot.sh +35 -6
  21. package/dist/templates/har-boilerplate-ios/stages/README.sh +6 -7
  22. package/dist/templates/har-boilerplate-ios/verify.sh +9 -3
  23. package/dist/templates/stage-templates/custom-stage-skeleton.sh +65 -0
  24. package/dist/templates/stage-templates/playwright/template.manifest.json +9 -1
  25. package/dist/templates/stage-templates/rocketsim/template.manifest.json +2 -1
  26. package/package.json +1 -1
@@ -0,0 +1,36 @@
1
+ # Maintain / repair the har harness
2
+
3
+ The `.har/` harness has drifted from the repository (stack changed, launch/verify broken, stale scripts or docs). Bring it back in sync — do every step yourself.
4
+
5
+ ## 1. Run maintain
6
+
7
+ ```bash
8
+ har env maintain
9
+ ```
10
+
11
+ This compares the harness against the current templates and repository, refreshes har-owned files, and prints an adaptation prompt (also written to `.har/ADAPT-PROMPT.md`).
12
+
13
+ ## 2. Perform the adaptation yourself
14
+
15
+ Read `.har/ADAPT-PROMPT.md` and execute its instructions now, in this session: reconcile `.har/` scripts (`launch.sh`, `verify.sh`, `setup-infra.sh`, `harness.env`, `stages.json`) and `AGENT.md` with the repository's current stack, ports, and commands. Preserve intentional project-specific customizations — fix drift, don't blindly reset.
16
+
17
+ Also check stage drift: compare the repository's current check commands (package.json scripts, Makefile, CI) against the stages registered in `.har/stages.json`. Register missing checks (`har env add-stage <id> --custom --command "..." --verification`), and remove or fix stages whose commands no longer exist. `.har/STAGES.md` documents the contract.
18
+
19
+ ## 3. Finalize and prove it works
20
+
21
+ ```bash
22
+ har env maintain --finalize
23
+ har env launch 1
24
+ har env verify 1 --full
25
+ ```
26
+
27
+ Fix the harness until launch and full verify pass. Tear down afterwards if the slot isn't needed (`har env teardown 1`).
28
+
29
+ ## 4. Commit
30
+
31
+ After the user confirms:
32
+
33
+ ```bash
34
+ git add .har/ AGENT.md
35
+ git commit -m "chore: maintain har harness"
36
+ ```
@@ -0,0 +1,47 @@
1
+ # Work in a har worktree slot
2
+
3
+ This repository uses a `.har/` harness. All code changes happen inside a **har session worktree**, never in the main checkout. Follow this flow for the current task.
4
+
5
+ Use har's worktree, not your own: do not create git worktrees yourself and do not use any built-in worktree/isolation feature of your agent harness — the har slot already provides the worktree plus ports, env, and lifecycle.
6
+
7
+ ## 1. Pick a slot
8
+
9
+ ```bash
10
+ har env status # or ./.har/agent-cli.sh 1 status
11
+ ```
12
+
13
+ - One slot ≈ one task. If slot 1 is occupied by unrelated work, use slot 2+ (limit: `agentSlots` in `.har/stages.json`).
14
+ - Never replace an occupied slot (`--replace`/`--force`) without the user explicitly approving it.
15
+
16
+ ## 2. Launch — BEFORE editing any file
17
+
18
+ ```bash
19
+ har env launch <id> # or ./.har/launch.sh <id>
20
+ ```
21
+
22
+ Launch creates a fresh session worktree from HEAD and prints its **work dir** (also recorded in `.har/slots/agent-<id>.json`, path like `~/worktrees/<base>-<sha4>-har-agent-<id>-<rand4>`). If a previous launch failed partway, retry with `--resume` instead of replacing.
23
+
24
+ ## 3. Do ALL work in the work dir
25
+
26
+ - `cd` into the work dir; every read-modify-write of project files happens there.
27
+ - The main checkout must stay clean (`git status` there shows no changes from you).
28
+ - Edits hot-reload in the running slot; use `./.har/agent-cli.sh <id> restart` if a change doesn't take.
29
+ - Commit early and often on the session branch — teardown keeps the branch, not uncommitted work.
30
+ - After launch, read `.har/CLAUDE.agent.md` in the worktree for slot URLs and the definition of done.
31
+
32
+ ## 4. Verify through the harness
33
+
34
+ ```bash
35
+ har env verify <id> # fast loop while iterating
36
+ har env verify <id> --full # required before declaring work done
37
+ ```
38
+
39
+ Do not substitute ad-hoc test commands for harness verification. Any edit after a full verify — even one character — requires re-running it.
40
+
41
+ ## 5. Finish
42
+
43
+ ```bash
44
+ har env complete <id> # full verify + validation record + teardown, keeps the session branch
45
+ ```
46
+
47
+ Report to the user: verification result, the session branch name (so they can push / open a PR), and the slot preview URLs if the app is running.
@@ -0,0 +1,70 @@
1
+ # Set up the har harness in this repository
2
+
3
+ You are onboarding this repository onto har (an agent-harness orchestrator). Do every step yourself — do not ask the user to paste prompts or run commands unless a step needs their approval.
4
+
5
+ ## 1. Ensure the har CLI is installed
6
+
7
+ ```bash
8
+ har --version
9
+ ```
10
+
11
+ If missing, install it and re-check:
12
+
13
+ ```bash
14
+ npm install -g @osfactory/har
15
+ ```
16
+
17
+ ## 2. Pick a harness profile
18
+
19
+ Inspect the repository and choose the profile that matches how agents will run it:
20
+
21
+ | Profile | Use when |
22
+ |---------|----------|
23
+ | `default` | Web app / server with a long-running process (Next.js, Rails, Django, APIs) |
24
+ | `cli` | CLI tool, library, or npm package — no dev server to keep alive |
25
+ | `ios` | iOS / Swift app built with xcodebuild + Simulator |
26
+
27
+ Tell the user which profile you picked and why before continuing.
28
+
29
+ ## 3. Initialize the harness
30
+
31
+ ```bash
32
+ har env init --profile <profile> # omit --profile for default
33
+ ```
34
+
35
+ If `.har/` already exists, stop and suggest `/har-maintain` instead.
36
+
37
+ ## 4. Perform the adaptation yourself
38
+
39
+ `har env init` prints an adaptation prompt and writes it to `.har/ADAPT-PROMPT.md`. Read that file and **execute its instructions yourself, now, in this session** — tailor `.har/` scripts (`launch.sh`, `verify.sh`, `setup-infra.sh`, `harness.env`, `stages.json`) and `AGENT.md` to this repository's real stack, ports, and commands. Do not use `--auto` and do not ask the user to paste anything.
40
+
41
+ ## 5. Register the project's checks as stages
42
+
43
+ Convert the repository's real check commands (test, lint, typecheck, whatever CI runs) into registered stages so they run in `verify --full` and are visible to every agent. Read `.har/STAGES.md` for the contract, then:
44
+
45
+ ```bash
46
+ har env add-stage unit-tests --custom --kind test --command "npm test" --verification
47
+ ```
48
+
49
+ - Use `--command` for one-liner checks; use `--script` when a check needs the slot's env, ports, or artifacts (then implement the scaffolded `.har/stages/<id>.sh`).
50
+ - Rich integrations ship as templates: `har env add-stage --list`, then e.g. `har env add-stage playwright` (web) or `har env add-stage rocketsim` (iOS).
51
+
52
+ ## 6. Prove the harness works
53
+
54
+ ```bash
55
+ har env launch 1
56
+ har env verify 1
57
+ ```
58
+
59
+ Fix the harness scripts until both pass. Then tear down or keep the slot as the user prefers (`har env teardown 1` keeps the branch).
60
+
61
+ ## 7. Commit
62
+
63
+ After the user confirms, commit the harness:
64
+
65
+ ```bash
66
+ git add .har/ AGENT.md CLAUDE.md .claude/ .cursor/ 2>/dev/null || git add .har/ AGENT.md
67
+ git commit -m "chore: add har agent harness"
68
+ ```
69
+
70
+ Optionally recommend `har hooks install` (commit gate) and `har hooks install --claude` (worktree guard for Claude Code).
@@ -0,0 +1,48 @@
1
+ {
2
+ "version": 1,
3
+ "skills": [
4
+ {
5
+ "id": "setup-har",
6
+ "body": "setup-har.md",
7
+ "title": "Set up the har harness in this repository",
8
+ "claude": {
9
+ "frontmatter": {
10
+ "name": "setup-har",
11
+ "description": "Install the har CLI and scaffold the .har/ agent harness in this repository (init, adapt, launch, verify, commit). User-invoked onboarding.",
12
+ "disable-model-invocation": true,
13
+ "allowed-tools": "Bash(har *) Bash(npm *) Bash(git *) Read Grep Glob Edit Write"
14
+ }
15
+ },
16
+ "cursor": { "command": true },
17
+ "codex": { "promptName": "har-setup" }
18
+ },
19
+ {
20
+ "id": "har-wt",
21
+ "body": "har-wt.md",
22
+ "title": "Work in a har worktree slot",
23
+ "claude": {
24
+ "frontmatter": {
25
+ "name": "har-wt",
26
+ "description": "Use BEFORE making any code change in this repository. Launches a har harness slot, which creates an isolated git session worktree — all edits must happen there, never in the main checkout — and verifies the work through the harness when done."
27
+ }
28
+ },
29
+ "cursor": { "command": true },
30
+ "codex": { "promptName": "har-wt" }
31
+ },
32
+ {
33
+ "id": "har-maintain",
34
+ "body": "har-maintain.md",
35
+ "title": "Maintain / repair the har harness",
36
+ "claude": {
37
+ "frontmatter": {
38
+ "name": "har-maintain",
39
+ "description": "Repair or update the .har/ harness when it has drifted from the repository (stack changes, broken launch/verify, stale scripts). Runs har env maintain and applies the adaptation.",
40
+ "disable-model-invocation": true,
41
+ "allowed-tools": "Bash(har *) Bash(git *) Read Grep Glob Edit Write"
42
+ }
43
+ },
44
+ "cursor": { "command": true },
45
+ "codex": { "promptName": "har-maintain" }
46
+ }
47
+ ]
48
+ }
@@ -0,0 +1,38 @@
1
+ #!/bin/sh
2
+ # Managed by `har hooks install --claude` — do not edit; reinstall to update.
3
+ # Claude Code PreToolUse guard: block file edits in the MAIN checkout of a har repo.
4
+ # Edits belong in a har session worktree (launch one with /har-wt). Fails open on errors.
5
+
6
+ [ "$HAR_SKIP_WT_GUARD" = "1" ] && exit 0
7
+
8
+ input="$(cat 2>/dev/null)" || exit 0
9
+ [ -n "$input" ] || exit 0
10
+ command -v node >/dev/null 2>&1 || exit 0
11
+
12
+ file_path="$(printf '%s' "$input" | node -e '
13
+ let d = "";
14
+ process.stdin.on("data", (c) => (d += c)).on("end", () => {
15
+ try {
16
+ const t = JSON.parse(d).tool_input || {};
17
+ process.stdout.write(t.file_path || t.notebook_path || "");
18
+ } catch {}
19
+ });' 2>/dev/null)" || exit 0
20
+ [ -n "$file_path" ] || exit 0
21
+
22
+ dir="$(dirname "$file_path")"
23
+ while [ ! -d "$dir" ] && [ "$dir" != "/" ]; do dir="$(dirname "$dir")"; done
24
+ toplevel="$(cd "$dir" 2>/dev/null && git rev-parse --show-toplevel 2>/dev/null)" || exit 0
25
+ [ -n "$toplevel" ] || exit 0
26
+
27
+ # Only guard har-managed repos.
28
+ [ -f "$toplevel/.har/manifest.json" ] || [ -f "$toplevel/.har/stages.json" ] || exit 0
29
+
30
+ # Linked worktrees (git-dir != common-dir) are session worktrees — allowed.
31
+ gitdir="$(cd "$toplevel" && git rev-parse --absolute-git-dir 2>/dev/null)"
32
+ commondir="$(cd "$toplevel" && git rev-parse --path-format=absolute --git-common-dir 2>/dev/null)"
33
+ if [ -n "$gitdir" ] && [ -n "$commondir" ] && [ "$gitdir" != "$commondir" ]; then
34
+ exit 0
35
+ fi
36
+
37
+ echo "har: edit blocked — $file_path is in the MAIN checkout of a har-managed repo. Run /har-wt to launch a harness slot and make all edits in its session worktree. (Human bypass: HAR_SKIP_WT_GUARD=1)" >&2
38
+ exit 2
@@ -39,13 +39,15 @@ is alive; it does not automatically mean an agent can use the app.
39
39
 
40
40
  - [ ] Full verification returns `"status": "pass"` (`har env verify ${AGENT_ID} --full`, MCP `har_run_verification` with `full: true`, or `./.har/verify.sh ${AGENT_ID} --full`)
41
41
  - [ ] The slot is agent-usable for this repo's documented smoke workflow, not only health-check green
42
- - [ ] When `stages/browser-e2e.sh` exists, full verify includes Playwright — adapt specs under `tests/` for UI changes
42
+ - [ ] Full verify runs every registered stage in `stages.json` `verificationStages` (Playwright, custom checks, …) — when `stages/browser-e2e.sh` exists, adapt specs under `tests/` for UI changes
43
43
  - [ ] New behavior has automated test coverage (unit and/or browser as appropriate)
44
44
  - [ ] Changes committed **in the session worktree** with a clear message
45
45
  - [ ] The user got the preview URLs to test the app themselves
46
46
  - [ ] Finish with `har env complete ${AGENT_ID}` (or MCP `har_complete_environment`) — records the validation and tears down while **keeping the session branch** for the user to push / open a PR
47
47
 
48
- Quick loop during development: MCP `har_run_verification`, `har env verify ${AGENT_ID}`, or `./.har/verify.sh ${AGENT_ID}` (stops before lint and browser-e2e).
48
+ Quick loop during development: MCP `har_run_verification`, `har env verify ${AGENT_ID}`, or `./.har/verify.sh ${AGENT_ID}` (smoke + health only; `--full` adds the registered verification stages).
49
+
50
+ Stages are the harness's single vocabulary for checks: templates and custom stages compile to generic kinds in `.har/stages.json`, and you interact with them only through the registry (`har_run_stage`, `verify`), never stack-specific tooling. Authoring guide: `.har/STAGES.md`.
49
51
 
50
52
  ## Project commands
51
53
 
@@ -0,0 +1,91 @@
1
+ # HAR Stages — authoring guide
2
+
3
+ Stages are the harness's single vocabulary for runnable checks and lifecycle
4
+ actions. Everything — shipped templates (`playwright`, `rocketsim`), your
5
+ project's test/lint commands, bespoke validation scripts — registers in
6
+ `.har/stages.json` with the same schema, and agents interact with stages only
7
+ through that registry (CLI `har env verify`, MCP `har_run_stage` /
8
+ `har_run_verification`), never through stack-specific tooling.
9
+
10
+ ## The registry: `.har/stages.json`
11
+
12
+ ```jsonc
13
+ {
14
+ "verificationStages": ["typecheck", "unit-tests", "api-health", "browser-e2e"],
15
+ "stages": [
16
+ {
17
+ "id": "browser-e2e", // stable, shell-friendly slug
18
+ "kind": "test", // setup | launch | verify | test | inspect | reset | teardown | custom
19
+ "description": "Playwright E2E",
20
+ "script": "stages/browser-e2e.sh", // relative to .har/ — OR use "command"
21
+ "requiresAgentId": true, // default true for test/verify/custom kinds
22
+ "artifacts": [{ "path": ".har/artifacts/browser-e2e", "kind": "directory" }]
23
+ },
24
+ {
25
+ "id": "unit-tests-fast",
26
+ "kind": "test",
27
+ "command": "npm test -- --agent {agentId}" // {agentId} is substituted at run time
28
+ }
29
+ ]
30
+ }
31
+ ```
32
+
33
+ Optional stage fields: `cwd` (working directory), `env` (extra env vars),
34
+ `group`, `acceptsArgs` (extra CLI args the stage accepts, e.g. `["--full"]`).
35
+
36
+ ## Two ways to define a stage
37
+
38
+ **Command stages** — the default for simple checks (`npm test`, `swiftlint`,
39
+ `make check`). One JSON entry, zero files:
40
+
41
+ ```bash
42
+ har env add-stage unit-tests-fast --custom --kind test --command "npm test" --verification
43
+ ```
44
+
45
+ **Script stages** — for anything that needs the slot's env, ports, or
46
+ artifacts. Scaffold a contract-compliant skeleton:
47
+
48
+ ```bash
49
+ har env add-stage db-integrity --custom --script --description "Check DB invariants"
50
+ ```
51
+
52
+ then implement the TODO block in `.har/stages/db-integrity.sh`.
53
+
54
+ ## The stage script contract
55
+
56
+ Every script under `.har/stages/` must:
57
+
58
+ 1. Source `harness.env` and `agent-slot.sh` from `.har/`.
59
+ 2. Take the agent slot id as `$1` (validate with `validate_agent_id`); extra
60
+ args may follow.
61
+ 3. Load the slot env via `resolve_agent_env_file` and run checks from the
62
+ agent's work dir (`resolve_agent_work_dir`).
63
+ 4. Write artifacts (reports, screenshots, logs) under `.har/artifacts/<id>/`.
64
+ 5. Print **only** the normalized JSON result object on stdout
65
+ (`status`, `stageId`, `agent_id`, `total_ms`, …); log progress to stderr.
66
+ 6. Exit with the real status code (0 = pass).
67
+
68
+ The scaffolded skeleton implements all of this — replace its TODO block.
69
+
70
+ ## Verification membership
71
+
72
+ Listing a stage id in `verificationStages` is what includes it in
73
+ `har env verify <id> --full`. Ids that match a registered stage run via their
74
+ script/command; ids without a registry entry (e.g. `typecheck`, `api-health`)
75
+ are inline steps owned by `.har/verify.sh`. Lifecycle kinds
76
+ (`setup`/`launch`/`reset`/`teardown`/`inspect`) and `verify` itself never run
77
+ as part of verification, even if listed.
78
+
79
+ ## Commit gate
80
+
81
+ The registry also holds the optional `commitGate` config (installed via
82
+ `har hooks install`): `{ "commitGate": { "mode": "block" | "warn", "scope":
83
+ "worktrees" | "all" } }` controls whether unverified change batches may be
84
+ committed.
85
+
86
+ ## Shipped stage templates
87
+
88
+ `har env add-stage --list` shows available templates; `har env add-stage
89
+ playwright` (web) or `har env add-stage rocketsim` (iOS) installs one. A
90
+ template is just packaging: it copies files, merges `package.json` fragments,
91
+ and registers stages through the exact same registry as `--custom`.
@@ -662,14 +662,43 @@ escape_step_output() {
662
662
  printf '%s' "$1" | node -e "let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{const s=d.trim().split('\n').slice(0,50).join('\n');process.stdout.write(JSON.stringify(s))})" 2>/dev/null || echo '""'
663
663
  }
664
664
 
665
- # Run browser-e2e on verify --full when the Playwright stage template is installed.
666
- run_browser_e2e_if_present() {
665
+ # Emit "<id>\t<command>" for each registered stage listed in stages.json
666
+ # verificationStages (used by verify --full). Ids without a matching registered
667
+ # stage are inline verify.sh steps and skipped here; lifecycle stages
668
+ # (setup/launch/reset/teardown/inspect) and the verify stage itself never run.
669
+ # Authoring contract: .har/STAGES.md
670
+ list_registered_verification_stage_commands() {
667
671
  local script_dir="$1"
668
672
  local agent_id="$2"
669
- local e2e="$script_dir/stages/browser-e2e.sh"
670
- if [ -x "$e2e" ]; then
671
- "$e2e" "$agent_id"
672
- fi
673
+ local registry="$script_dir/stages.json"
674
+ [ -f "$registry" ] || return 0
675
+ HAR_STAGE_REGISTRY="$registry" HAR_SCRIPT_DIR="$script_dir" HAR_AGENT_ID="$agent_id" node <<'NODE' 2>/dev/null || true
676
+ const fs = require('fs');
677
+ const { HAR_STAGE_REGISTRY, HAR_SCRIPT_DIR, HAR_AGENT_ID } = process.env;
678
+ const shq = (s) => "'" + String(s).replace(/'/g, "'\\''") + "'";
679
+ let reg;
680
+ try { reg = JSON.parse(fs.readFileSync(HAR_STAGE_REGISTRY, 'utf8')); } catch { process.exit(0); }
681
+ const ids = Array.isArray(reg.verificationStages) ? reg.verificationStages : [];
682
+ const stages = Array.isArray(reg.stages) ? reg.stages : [];
683
+ const runnable = new Set(['test', 'custom']);
684
+ for (const id of ids) {
685
+ const stage = stages.find((s) => s && s.id === id);
686
+ if (!stage || stage.id === 'verify' || !runnable.has(stage.kind)) continue;
687
+ const needsAgent = stage.requiresAgentId !== false;
688
+ let cmd;
689
+ if (stage.script) {
690
+ cmd = shq(HAR_SCRIPT_DIR + '/' + stage.script) + (needsAgent ? ' ' + shq(HAR_AGENT_ID) : '');
691
+ } else if (stage.command) {
692
+ cmd = stage.command.split('{agentId}').join(HAR_AGENT_ID);
693
+ } else {
694
+ continue;
695
+ }
696
+ if (stage.cwd) cmd = 'cd ' + shq(stage.cwd) + ' && ' + cmd;
697
+ const env = stage.env && typeof stage.env === 'object' ? stage.env : {};
698
+ const prefix = Object.entries(env).map(([k, v]) => k + '=' + shq(v)).join(' ');
699
+ process.stdout.write(id + '\t' + (prefix ? prefix + ' ' : '') + cmd + '\n');
700
+ }
701
+ NODE
673
702
  }
674
703
 
675
704
  # Optional project-owned "agent usable" smoke beyond health.
@@ -1,9 +1,11 @@
1
1
  #!/usr/bin/env bash
2
- # Optional per-stage scripts live here.
3
- # Example: ./.har/stages/browser-e2e.sh <agent-id>
2
+ # Per-stage scripts live here (e.g. ./.har/stages/browser-e2e.sh <agent-id>).
4
3
  #
5
- # Register custom stages in .har/stages.json with a project-owned command:
6
- # "command": "./.har/stages/browser-e2e.sh {agentId}"
4
+ # Read .har/STAGES.md for the authoring guide: the stage script contract,
5
+ # command vs script stages, and how verificationStages controls verify --full.
6
+ #
7
+ # Scaffold a new one with:
8
+ # har env add-stage <id> --custom --script
7
9
 
8
- echo "Add stage scripts under .har/stages/ and register them in stages.json." >&2
10
+ echo "Add stage scripts under .har/stages/ and register them in stages.json — see .har/STAGES.md." >&2
9
11
  exit 1
@@ -5,7 +5,8 @@
5
5
  # Usage: ./.har/verify.sh <agent-id> [--full]
6
6
  #
7
7
  # Quick (default): ecosystem smoke + health only
8
- # Full (--full): + conventional tests, lint, optional readiness + browser-e2e
8
+ # Full (--full): + conventional tests, lint, readiness, and every registered
9
+ # stage in stages.json verificationStages (see .har/STAGES.md)
9
10
  # Stock steps are examples. Replace them during adaptation to match this repo.
10
11
  set -euo pipefail
11
12
 
@@ -229,7 +230,13 @@ run_http_step "api-health" "http://localhost:${API_PORT}${HARNESS_HEALTH_CHECK_P
229
230
  if [ -n "$FULL" ]; then
230
231
  run_full_checks
231
232
  run_step "readiness" "run_readiness_if_configured \"$AGENT_ID\"" || true
232
- run_step "browser-e2e" "run_browser_e2e_if_present \"$SCRIPT_DIR\" \"$AGENT_ID\"" || true
233
+ # Registered verification stages from .har/stages.json (see .har/STAGES.md).
234
+ # Every stage listed in verificationStages with a registered script/command
235
+ # runs here -- stage templates and custom stages alike.
236
+ while IFS=$'\t' read -r STAGE_ID STAGE_CMD; do
237
+ [ -n "$STAGE_ID" ] || continue
238
+ run_step "$STAGE_ID" "$STAGE_CMD" || true
239
+ done < <(list_registered_verification_stage_commands "$SCRIPT_DIR" "$AGENT_ID")
233
240
  fi
234
241
 
235
242
  # ── Output results ────────────────────────────────────────────────────────────
@@ -26,13 +26,15 @@ workflow, document the required credentials/default data and wire a smoke into
26
26
 
27
27
  - [ ] Full verification returns `"status": "pass"` (`har env verify ${AGENT_ID} --full`, MCP `har_run_verification` with `full: true`, or `./.har/verify.sh ${AGENT_ID} --full`)
28
28
  - [ ] The slot is agent-usable for this repo's documented smoke workflow when runtime services are involved
29
- - [ ] When `stages/browser-e2e.sh` exists, full verify includes Playwright — adapt specs under `tests/` for UI changes
29
+ - [ ] Full verify runs every registered stage in `stages.json` `verificationStages` (Playwright, custom checks, …) — when `stages/browser-e2e.sh` exists, adapt specs under `tests/` for UI changes
30
30
  - [ ] New behavior has automated test coverage
31
31
  - [ ] Changes committed **in the session worktree** with a clear message
32
32
  - [ ] Finish with `har env complete ${AGENT_ID}` (or MCP `har_complete_environment`) — records the validation and tears down while **keeping the session branch** for the user to push / open a PR
33
33
 
34
34
  Quick loop: MCP `har_run_verification`, `har env verify ${AGENT_ID}`, or `./.har/verify.sh ${AGENT_ID}`
35
35
 
36
+ Stages are the harness's single vocabulary for checks: templates and custom stages compile to generic kinds in `.har/stages.json`, and you interact with them only through the registry (`har_run_stage`, `verify`), never stack-specific tooling. Authoring guide: `.har/STAGES.md`.
37
+
36
38
  ## Project commands
37
39
 
38
40
  ```bash
@@ -0,0 +1,91 @@
1
+ # HAR Stages — authoring guide
2
+
3
+ Stages are the harness's single vocabulary for runnable checks and lifecycle
4
+ actions. Everything — shipped templates (`playwright`, `rocketsim`), your
5
+ project's test/lint commands, bespoke validation scripts — registers in
6
+ `.har/stages.json` with the same schema, and agents interact with stages only
7
+ through that registry (CLI `har env verify`, MCP `har_run_stage` /
8
+ `har_run_verification`), never through stack-specific tooling.
9
+
10
+ ## The registry: `.har/stages.json`
11
+
12
+ ```jsonc
13
+ {
14
+ "verificationStages": ["typecheck", "unit-tests", "api-health", "browser-e2e"],
15
+ "stages": [
16
+ {
17
+ "id": "browser-e2e", // stable, shell-friendly slug
18
+ "kind": "test", // setup | launch | verify | test | inspect | reset | teardown | custom
19
+ "description": "Playwright E2E",
20
+ "script": "stages/browser-e2e.sh", // relative to .har/ — OR use "command"
21
+ "requiresAgentId": true, // default true for test/verify/custom kinds
22
+ "artifacts": [{ "path": ".har/artifacts/browser-e2e", "kind": "directory" }]
23
+ },
24
+ {
25
+ "id": "unit-tests-fast",
26
+ "kind": "test",
27
+ "command": "npm test -- --agent {agentId}" // {agentId} is substituted at run time
28
+ }
29
+ ]
30
+ }
31
+ ```
32
+
33
+ Optional stage fields: `cwd` (working directory), `env` (extra env vars),
34
+ `group`, `acceptsArgs` (extra CLI args the stage accepts, e.g. `["--full"]`).
35
+
36
+ ## Two ways to define a stage
37
+
38
+ **Command stages** — the default for simple checks (`npm test`, `swiftlint`,
39
+ `make check`). One JSON entry, zero files:
40
+
41
+ ```bash
42
+ har env add-stage unit-tests-fast --custom --kind test --command "npm test" --verification
43
+ ```
44
+
45
+ **Script stages** — for anything that needs the slot's env, ports, or
46
+ artifacts. Scaffold a contract-compliant skeleton:
47
+
48
+ ```bash
49
+ har env add-stage db-integrity --custom --script --description "Check DB invariants"
50
+ ```
51
+
52
+ then implement the TODO block in `.har/stages/db-integrity.sh`.
53
+
54
+ ## The stage script contract
55
+
56
+ Every script under `.har/stages/` must:
57
+
58
+ 1. Source `harness.env` and `agent-slot.sh` from `.har/`.
59
+ 2. Take the agent slot id as `$1` (validate with `validate_agent_id`); extra
60
+ args may follow.
61
+ 3. Load the slot env via `resolve_agent_env_file` and run checks from the
62
+ agent's work dir (`resolve_agent_work_dir`).
63
+ 4. Write artifacts (reports, screenshots, logs) under `.har/artifacts/<id>/`.
64
+ 5. Print **only** the normalized JSON result object on stdout
65
+ (`status`, `stageId`, `agent_id`, `total_ms`, …); log progress to stderr.
66
+ 6. Exit with the real status code (0 = pass).
67
+
68
+ The scaffolded skeleton implements all of this — replace its TODO block.
69
+
70
+ ## Verification membership
71
+
72
+ Listing a stage id in `verificationStages` is what includes it in
73
+ `har env verify <id> --full`. Ids that match a registered stage run via their
74
+ script/command; ids without a registry entry (e.g. `typecheck`, `api-health`)
75
+ are inline steps owned by `.har/verify.sh`. Lifecycle kinds
76
+ (`setup`/`launch`/`reset`/`teardown`/`inspect`) and `verify` itself never run
77
+ as part of verification, even if listed.
78
+
79
+ ## Commit gate
80
+
81
+ The registry also holds the optional `commitGate` config (installed via
82
+ `har hooks install`): `{ "commitGate": { "mode": "block" | "warn", "scope":
83
+ "worktrees" | "all" } }` controls whether unverified change batches may be
84
+ committed.
85
+
86
+ ## Shipped stage templates
87
+
88
+ `har env add-stage --list` shows available templates; `har env add-stage
89
+ playwright` (web) or `har env add-stage rocketsim` (iOS) installs one. A
90
+ template is just packaging: it copies files, merges `package.json` fragments,
91
+ and registers stages through the exact same registry as `--custom`.
@@ -642,14 +642,43 @@ escape_step_output() {
642
642
  printf '%s' "$1" | node -e "let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{const s=d.trim().split('\n').slice(0,50).join('\n');process.stdout.write(JSON.stringify(s))})" 2>/dev/null || echo '""'
643
643
  }
644
644
 
645
- # Run browser-e2e on verify --full when the Playwright stage template is installed.
646
- run_browser_e2e_if_present() {
645
+ # Emit "<id>\t<command>" for each registered stage listed in stages.json
646
+ # verificationStages (used by verify --full). Ids without a matching registered
647
+ # stage are inline verify.sh steps and skipped here; lifecycle stages
648
+ # (setup/launch/reset/teardown/inspect) and the verify stage itself never run.
649
+ # Authoring contract: .har/STAGES.md
650
+ list_registered_verification_stage_commands() {
647
651
  local script_dir="$1"
648
652
  local agent_id="$2"
649
- local e2e="$script_dir/stages/browser-e2e.sh"
650
- if [ -x "$e2e" ]; then
651
- "$e2e" "$agent_id"
652
- fi
653
+ local registry="$script_dir/stages.json"
654
+ [ -f "$registry" ] || return 0
655
+ HAR_STAGE_REGISTRY="$registry" HAR_SCRIPT_DIR="$script_dir" HAR_AGENT_ID="$agent_id" node <<'NODE' 2>/dev/null || true
656
+ const fs = require('fs');
657
+ const { HAR_STAGE_REGISTRY, HAR_SCRIPT_DIR, HAR_AGENT_ID } = process.env;
658
+ const shq = (s) => "'" + String(s).replace(/'/g, "'\\''") + "'";
659
+ let reg;
660
+ try { reg = JSON.parse(fs.readFileSync(HAR_STAGE_REGISTRY, 'utf8')); } catch { process.exit(0); }
661
+ const ids = Array.isArray(reg.verificationStages) ? reg.verificationStages : [];
662
+ const stages = Array.isArray(reg.stages) ? reg.stages : [];
663
+ const runnable = new Set(['test', 'custom']);
664
+ for (const id of ids) {
665
+ const stage = stages.find((s) => s && s.id === id);
666
+ if (!stage || stage.id === 'verify' || !runnable.has(stage.kind)) continue;
667
+ const needsAgent = stage.requiresAgentId !== false;
668
+ let cmd;
669
+ if (stage.script) {
670
+ cmd = shq(HAR_SCRIPT_DIR + '/' + stage.script) + (needsAgent ? ' ' + shq(HAR_AGENT_ID) : '');
671
+ } else if (stage.command) {
672
+ cmd = stage.command.split('{agentId}').join(HAR_AGENT_ID);
673
+ } else {
674
+ continue;
675
+ }
676
+ if (stage.cwd) cmd = 'cd ' + shq(stage.cwd) + ' && ' + cmd;
677
+ const env = stage.env && typeof stage.env === 'object' ? stage.env : {};
678
+ const prefix = Object.entries(env).map(([k, v]) => k + '=' + shq(v)).join(' ');
679
+ process.stdout.write(id + '\t' + (prefix ? prefix + ' ' : '') + cmd + '\n');
680
+ }
681
+ NODE
653
682
  }
654
683
 
655
684
  # Optional project-owned "agent usable" smoke beyond health.
@@ -1,9 +1,11 @@
1
1
  #!/usr/bin/env bash
2
- # Optional per-stage scripts live here.
3
- # Example: ./.har/stages/browser-e2e.sh <agent-id>
2
+ # Per-stage scripts live here (e.g. ./.har/stages/browser-e2e.sh <agent-id>).
4
3
  #
5
- # Register custom stages in .har/stages.json with a project-owned command:
6
- # "command": "./.har/stages/browser-e2e.sh {agentId}"
4
+ # Read .har/STAGES.md for the authoring guide: the stage script contract,
5
+ # command vs script stages, and how verificationStages controls verify --full.
6
+ #
7
+ # Scaffold a new one with:
8
+ # har env add-stage <id> --custom --script
7
9
 
8
- echo "Add stage scripts under .har/stages/ and register them in stages.json." >&2
10
+ echo "Add stage scripts under .har/stages/ and register them in stages.json — see .har/STAGES.md." >&2
9
11
  exit 1
@@ -5,7 +5,8 @@
5
5
  # Usage: ./.har/verify.sh <agent-id> [--full]
6
6
  #
7
7
  # Quick (default): ecosystem smoke — compile/import/build only
8
- # Full (--full): + conventional tests, lint, optional readiness + browser-e2e
8
+ # Full (--full): + conventional tests, lint, readiness, and every registered
9
+ # stage in stages.json verificationStages (see .har/STAGES.md)
9
10
  # Stock steps are examples. Replace them during adaptation to match this repo.
10
11
  set -euo pipefail
11
12
 
@@ -185,7 +186,13 @@ run_quick_smoke || { [ -z "$FULL" ] && true; }
185
186
  if [ -n "$FULL" ]; then
186
187
  run_full_checks
187
188
  run_step "readiness" "run_readiness_if_configured \"$AGENT_ID\"" || true
188
- run_step "browser-e2e" "run_browser_e2e_if_present \"$SCRIPT_DIR\" \"$AGENT_ID\"" || true
189
+ # Registered verification stages from .har/stages.json (see .har/STAGES.md).
190
+ # Every stage listed in verificationStages with a registered script/command
191
+ # runs here -- stage templates and custom stages alike.
192
+ while IFS=$'\t' read -r STAGE_ID STAGE_CMD; do
193
+ [ -n "$STAGE_ID" ] || continue
194
+ run_step "$STAGE_ID" "$STAGE_CMD" || true
195
+ done < <(list_registered_verification_stage_commands "$SCRIPT_DIR" "$AGENT_ID")
189
196
  fi
190
197
 
191
198
  END_TOTAL=$(now_ms)