mirror of
https://github.com/garrytan/gbrain.git
synced 2026-08-14 08:53:22 +00:00
Compare commits
2
Commits
v0.45.10.0
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7fdcd8bd2e | ||
|
|
6411150071 |
@@ -1,7 +1,7 @@
|
||||
name: Heavy Tests
|
||||
|
||||
# Heavy ops-shape tests under tests/heavy/. Cost minutes per run; NOT part
|
||||
# of default PR CI. Two triggers:
|
||||
# of default PR CI. Three triggers:
|
||||
# - Nightly schedule (catches regressions within 24h of merge to master).
|
||||
# - On-demand opt-in via PR label `heavy-tests` (slow loop kept off by default).
|
||||
# - Manual workflow_dispatch for triage.
|
||||
@@ -102,14 +102,15 @@ jobs:
|
||||
retention-days: 14
|
||||
if-no-files-found: ignore
|
||||
|
||||
# Real-agent door e2e: drives the ACTUAL `claude` + `codex` binaries (no PATH
|
||||
# shims) against a real gbrain over MCP. These pay real API cost and need the
|
||||
# binaries installed + authed, which a stock GitHub runner does NOT have — so
|
||||
# both tests self-SKIP (describe.skipIf on binary/auth) and the job is a clean
|
||||
# no-op here. It exists so a self-hosted / manually-provisioned runner WITH
|
||||
# authed claude/codex (and ANTHROPIC/OPENAI creds) actually exercises the real
|
||||
# binaries. Heavy cadence only (nightly + `real-agent-e2e` label + dispatch);
|
||||
# NEVER the PR shard matrix.
|
||||
# Real-agent door e2e: drives the ACTUAL `claude` + `codex` + `hermes`
|
||||
# binaries (no PATH shims) against a real gbrain over MCP. These pay real API
|
||||
# cost and need the binaries installed + authed, which a stock GitHub runner
|
||||
# does NOT have — so the tests self-SKIP (describe.skipIf on binary/auth) and
|
||||
# the job is a clean no-op here. It exists so a self-hosted /
|
||||
# manually-provisioned runner WITH authed claude/codex/hermes (and
|
||||
# ANTHROPIC/OPENAI creds) actually exercises the real binaries. Heavy cadence
|
||||
# only (nightly + `real-agent-e2e` label + dispatch); NEVER the PR shard
|
||||
# matrix.
|
||||
real-agent-e2e:
|
||||
name: Real-agent door e2e (skips without authed binaries)
|
||||
if: |
|
||||
@@ -118,6 +119,10 @@ jobs:
|
||||
contains(github.event.pull_request.labels.*.name, 'heavy-tests')
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
env:
|
||||
# Open the hermes opt-in door here so binary/auth absence — not the
|
||||
# opt-in var — is what skips (same posture as the claude/codex doors).
|
||||
GBRAIN_REAL_HERMES_E2E: '1'
|
||||
steps:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
|
||||
@@ -125,15 +130,16 @@ jobs:
|
||||
bun-version: 1.3.13
|
||||
- run: bun install
|
||||
|
||||
# Reference both door tests; run only the ones present (the claude door
|
||||
# may land in a sibling PR). Missing binary/auth → the file self-skips, so
|
||||
# a stock runner reports a green no-op rather than failing.
|
||||
# Reference the door tests; run only the ones present (a door may land
|
||||
# in a sibling PR). Missing binary/auth → the file self-skips, so a
|
||||
# stock runner reports a green no-op rather than failing.
|
||||
- name: Run real-agent door tests
|
||||
run: |
|
||||
files=()
|
||||
for f in \
|
||||
test/e2e/bootstrap-real-claude.serial.test.ts \
|
||||
test/e2e/bootstrap-real-codex.serial.test.ts; do
|
||||
test/e2e/bootstrap-real-codex.serial.test.ts \
|
||||
test/e2e/install-real-hermes.serial.test.ts; do
|
||||
[ -f "$f" ] && files+=("$f")
|
||||
done
|
||||
if [ "${#files[@]}" -eq 0 ]; then
|
||||
@@ -144,3 +150,187 @@ jobs:
|
||||
# --timeout: real-agent turns are slow (live claude/codex); the door
|
||||
# tests self-skip without authed binaries so this is a no-op elsewhere.
|
||||
bun test --timeout=600000 "${files[@]}"
|
||||
|
||||
# Hermes door e2e: unlike real-agent-e2e above (best-effort, self-skipping),
|
||||
# this job PROVISIONS the real hermes binary itself — pinned installer digest,
|
||||
# non-interactive auth + model pin — and then requires the door tests to
|
||||
# actually execute. It pays real API cost, so it stays label-gated at heavy
|
||||
# cadence (nightly + `real-agent-e2e`/`heavy-tests` label + dispatch); NEVER
|
||||
# the PR shard matrix. Loud-fail throughout: a runner that cannot install or
|
||||
# auth hermes fails this job rather than skipping.
|
||||
hermes-door:
|
||||
name: Hermes door e2e (real binary, loud-fail)
|
||||
if: |
|
||||
github.event_name != 'pull_request' ||
|
||||
contains(github.event.pull_request.labels.*.name, 'real-agent-e2e') ||
|
||||
contains(github.event.pull_request.labels.*.name, 'heavy-tests')
|
||||
runs-on: ubuntu-latest
|
||||
# Four serial door tests at 600s each plus the installer budget cannot
|
||||
# fit the sibling job's 20 minutes.
|
||||
timeout-minutes: 40
|
||||
env:
|
||||
# Pin values documented in docs/mcp/HERMES-CLI-PIN.md — update them
|
||||
# together, deliberately, after reviewing upstream changes. The digest
|
||||
# pins the INSTALLER SCRIPT; the tag + commit pin the PAYLOAD it clones
|
||||
# (without them, the installer pulls upstream main into the runner that
|
||||
# later holds secrets). The commit is v2026.8.3's dereferenced SHA —
|
||||
# immutable even if the tag moves.
|
||||
HERMES_VERSION: "0.20.0"
|
||||
HERMES_GIT_TAG: "v2026.8.3"
|
||||
HERMES_GIT_COMMIT: "3c27eb6234bf91b8ceee9e9071591b31e9b148cb"
|
||||
HERMES_INSTALL_SHA256: "c118ff31618dc70339049ce71061b8f1351a1c70d9c2a236ed50d8a2550c550d"
|
||||
GBRAIN_REAL_HERMES_E2E: '1'
|
||||
steps:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
|
||||
with:
|
||||
bun-version: 1.3.13
|
||||
- run: bun install
|
||||
|
||||
# `runner.temp` is not an allowed context in job-level env, so the
|
||||
# evidence dir is derived here and exported for every later step (the
|
||||
# door test writes into it; the failure-path upload reads it — exporting
|
||||
# early keeps that upload working even when an install/precondition step
|
||||
# is what failed).
|
||||
- name: Prepare evidence dir
|
||||
run: |
|
||||
echo "GBRAIN_E2E_EVIDENCE_DIR=$RUNNER_TEMP/hermes-door-evidence" >> "$GITHUB_ENV"
|
||||
mkdir -p "$RUNNER_TEMP/hermes-door-evidence"
|
||||
|
||||
# NO secrets in this step's env: the installer is downloaded from the
|
||||
# network and executed, so it must never see credentials. The digest pin
|
||||
# turns an upstream installer change into a loud failure instead of
|
||||
# unreviewed code running in CI. Bound + retry the install run itself
|
||||
# (same posture as the e2e tier2 OpenClaw install): `timeout` kills a
|
||||
# hung attempt, 3 attempts ride out transient flakes, step cap backstops.
|
||||
- name: Install hermes (pinned installer digest)
|
||||
timeout-minutes: 15
|
||||
run: |
|
||||
curl -fsSL --retry 3 -o hermes-install.sh https://hermes-agent.nousresearch.com/install.sh
|
||||
if ! echo "$HERMES_INSTALL_SHA256 hermes-install.sh" | sha256sum -c -; then
|
||||
echo "::error::hermes installer digest drift — re-pin deliberately: update HERMES_INSTALL_SHA256 + HERMES_VERSION in this workflow and docs/mcp/HERMES-CLI-PIN.md after reviewing upstream changes" >&2
|
||||
exit 1
|
||||
fi
|
||||
for attempt in 1 2 3; do
|
||||
if timeout 600 bash hermes-install.sh --skip-setup --non-interactive --branch "$HERMES_GIT_TAG" --commit "$HERMES_GIT_COMMIT"; then
|
||||
# The branch/commit flags above are ASSERTED here, not trusted:
|
||||
# a shell installer that silently ignores unknown flags would
|
||||
# clone upstream main into a runner that later holds secrets.
|
||||
# Verify the actual checkout before anything else runs it.
|
||||
actual_commit=$(git -C "$HOME/.hermes/hermes-agent" rev-parse HEAD 2>/dev/null || echo "no-git-checkout")
|
||||
if [ "$actual_commit" != "$HERMES_GIT_COMMIT" ]; then
|
||||
echo "::error::hermes payload drift — installed checkout is $actual_commit, pinned $HERMES_GIT_COMMIT. Either the installer ignored its branch/commit flags or the layout moved from ~/.hermes/hermes-agent; re-pin deliberately (HERMES_GIT_TAG/HERMES_GIT_COMMIT + docs/mcp/HERMES-CLI-PIN.md) after reviewing upstream." >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
|
||||
exit 0
|
||||
fi
|
||||
echo "::warning::hermes install attempt $attempt failed or timed out; retrying in 10s" >&2
|
||||
sleep 10
|
||||
done
|
||||
echo "::error::hermes install failed after 3 attempts" >&2
|
||||
exit 1
|
||||
|
||||
- name: Preconditions (binary, secret, version pin)
|
||||
env:
|
||||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
run: |
|
||||
if ! command -v hermes >/dev/null 2>&1; then
|
||||
echo "::error::hermes did not resolve on PATH after install" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "$ANTHROPIC_API_KEY" ]; then
|
||||
echo "::error::ANTHROPIC_API_KEY secret is empty — fork PRs get no secrets from GitHub, and this labeled job cannot run without them" >&2
|
||||
exit 1
|
||||
fi
|
||||
version_output=$(hermes --version)
|
||||
echo "$version_output"
|
||||
# Observed shape: `Hermes Agent v0.20.0 (2026.8.3)`.
|
||||
if ! printf '%s' "$version_output" | grep -qF "v$HERMES_VERSION"; then
|
||||
echo "::error::hermes version drift — expected v$HERMES_VERSION in: $version_output" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Configure hermes (auth + model pin)
|
||||
env:
|
||||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
run: |
|
||||
mkdir -p ~/.hermes
|
||||
printf 'ANTHROPIC_API_KEY=%s\n' "$ANTHROPIC_API_KEY" > ~/.hermes/.env
|
||||
chmod 600 ~/.hermes/.env
|
||||
# `hermes model` is interactive-only; `config set` is the observed
|
||||
# non-interactive model pin.
|
||||
hermes config set model.default anthropic/claude-haiku-4.5
|
||||
# Global health check — informational only, never a gate here.
|
||||
hermes doctor || true
|
||||
|
||||
- name: Run hermes door tests
|
||||
env:
|
||||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
run: |
|
||||
# Redirect to a file, then tail — never pipe bun through tail (the
|
||||
# pipe eats the real exit code and truncates failure details).
|
||||
# `|| EXIT=$?` keeps the default `-e` shell from bailing before the
|
||||
# tail runs.
|
||||
EXIT=0
|
||||
bun test --timeout=600000 test/e2e/install-real-hermes.serial.test.ts > door.txt 2>&1 || EXIT=$?
|
||||
tail -40 door.txt
|
||||
if [ "$EXIT" -ne 0 ]; then
|
||||
# Preserve the FULL bun output for the failure artifact — bun
|
||||
# prints failure details before the summary, so the 40-line tail
|
||||
# above can drop exactly the lines a paid-CI triage needs.
|
||||
cp door.txt "$GBRAIN_E2E_EVIDENCE_DIR/" 2>/dev/null || true
|
||||
exit "$EXIT"
|
||||
fi
|
||||
# This job provisions the binary + auth above, so the door must
|
||||
# actually EXECUTE: a summary with zero passing tests means the
|
||||
# suite ran nothing or self-skipped everything — never let that
|
||||
# read as green.
|
||||
pass_count=$(grep -Eo '[0-9]+ pass' door.txt | tail -1 | grep -Eo '^[0-9]+' || true)
|
||||
if [ -z "$pass_count" ] || [ "$pass_count" -eq 0 ]; then
|
||||
echo "::error::hermes door summary shows no passing tests (nothing ran or everything skipped) — refusing to go green while testing nothing" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# The door test copies its evidence into GBRAIN_E2E_EVIDENCE_DIR; the
|
||||
# workflow only uploads it. The test already excludes credential files —
|
||||
# the scrub below is defensive belt-and-suspenders before upload. Both
|
||||
# steps also require the evidence-dir env (a failure before the prepare
|
||||
# step leaves it unset, and there is nothing to upload then anyway).
|
||||
- name: Scrub credentials from evidence (defensive)
|
||||
if: failure() && env.GBRAIN_E2E_EVIDENCE_DIR != ''
|
||||
env:
|
||||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
run: |
|
||||
# Three layers, because the evidence dir carries files WRITTEN BY
|
||||
# THE THIRD-PARTY HERMES BINARY (logs/sessions) and upload-artifact
|
||||
# follows symlinks:
|
||||
# 1. filename scrub (env files),
|
||||
# 2. symlink delete (an agent-dropped link could dereference to a
|
||||
# real credential file at upload time),
|
||||
# 3. content scrub (any file that embeds the key — auth-error dumps
|
||||
# are most likely exactly on the failure path that uploads).
|
||||
find "$GBRAIN_E2E_EVIDENCE_DIR" -type f \( -name '.env' -o -name '*.env' \) -exec rm -f {} + 2>/dev/null || true
|
||||
find "$GBRAIN_E2E_EVIDENCE_DIR" -type l -delete 2>/dev/null || true
|
||||
if [ -n "$ANTHROPIC_API_KEY" ]; then
|
||||
grep -rlF "$ANTHROPIC_API_KEY" "$GBRAIN_E2E_EVIDENCE_DIR" 2>/dev/null | while IFS= read -r f; do
|
||||
echo "::warning::removing evidence file containing the API key: ${f#"$GBRAIN_E2E_EVIDENCE_DIR"/}" >&2
|
||||
rm -f "$f"
|
||||
done
|
||||
fi
|
||||
- name: Upload hermes door evidence
|
||||
if: failure() && env.GBRAIN_E2E_EVIDENCE_DIR != ''
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
with:
|
||||
name: hermes-door-evidence
|
||||
path: ${{ env.GBRAIN_E2E_EVIDENCE_DIR }}
|
||||
retention-days: 14
|
||||
if-no-files-found: ignore
|
||||
|
||||
# Hosted ubuntu-latest runners are ephemeral, but this must not depend
|
||||
# on that: if the job ever moves to a self-hosted runner (the sibling
|
||||
# real-agent-e2e job is designed for one), a key left in ~/.hermes/.env
|
||||
# would persist for every later workload on that runner.
|
||||
- name: Remove hermes credentials (unconditional)
|
||||
if: always()
|
||||
run: rm -f ~/.hermes/.env
|
||||
|
||||
+31
-8
@@ -1,4 +1,4 @@
|
||||
<!-- gbrain-runbook-stamp: 0.45.10.0 -->
|
||||
<!-- gbrain-runbook-stamp: 0.45.12.0 -->
|
||||
<!-- This stamp must equal the VERSION file at every release; CI enforces it
|
||||
(scripts/check-bootstrap-tag.sh). `gbrain bootstrap status` compares it to
|
||||
the installed binary and warns on skew. -->
|
||||
@@ -82,8 +82,11 @@ you needed; report the count at the end (it feeds the install-time measurement).
|
||||
`gh auth login -h github.com -p https -w` (you run it; they click Authorize).
|
||||
Then `gbrain bootstrap status` — it is idempotent and resume-aware; after any
|
||||
partial failure, re-run it and continue where it points.
|
||||
2. **Engine.** `gbrain init --pglite` (2 seconds, no server). Search mode defaults
|
||||
to balanced silently — do NOT ask; the human can change it any time with
|
||||
2. **Engine.** `gbrain init --pglite` (2 seconds, no server). Search mode is
|
||||
auto-selected silently (conservative when keyless, tokenmax with an
|
||||
expansion key) and printed with an `[AGENT]` cost matrix — surface that
|
||||
matrix to the human and confirm before running high-volume queries (see
|
||||
INSTALL_FOR_AGENTS.md Step 3.5); they can change it any time with
|
||||
`gbrain search modes`. The one thing to raise here is the OPTIONAL provider
|
||||
key — with no key you run keyless: keyword search plus memory you author
|
||||
yourself through the write tools; everything works, one key upgrades search to
|
||||
@@ -147,7 +150,9 @@ you needed; report the count at the end (it feeds the install-time measurement).
|
||||
through the real write path, graph floor, token sweep, secret scan, repo
|
||||
privacy, hooks smoke, capability report (keyless or keyed). Exit 0 or it is not
|
||||
done. Paste the report. Then relay the first-run tour it prints (three prompts
|
||||
the human should try, starting with restarting the session).
|
||||
the human should try, starting with restarting the session) AND the hand-off
|
||||
block below it — the ownership line and the cold-start offer are the two
|
||||
things the human must actually understand, not fine print.
|
||||
|
||||
## Machine two
|
||||
|
||||
@@ -209,7 +214,25 @@ placeholder). Trust the CLI's detection over your own guesses.
|
||||
|
||||
## Hand off
|
||||
|
||||
Finish by telling the human: the private repo URL (or the local-only status), the
|
||||
capability mode (keyless vs keyed), the three commands they will actually reuse
|
||||
(`gbrain doctor`, `gbrain bootstrap verify`, `gbrain sources push`), and the
|
||||
first-run tour. Then delete nothing — this runbook was fetched, not installed.
|
||||
Two things the human must UNDERSTAND before you finish — say them plainly, in
|
||||
this order, and confirm they landed:
|
||||
|
||||
1. **They own the brain.** Every memory you keep is a markdown file in THEIR
|
||||
private GitHub repo — name the URL. Owning it means: they can read it any
|
||||
time, take it to a second machine (`gbrain bootstrap attach`), or delete the
|
||||
repo and the brain is gone. If they went local-only, say that instead, with
|
||||
`gbrain bootstrap repo` as the any-time upgrade.
|
||||
2. **The first skill to run is cold-start.** An empty brain is a database; a
|
||||
filled one is a memory — and every flagship skill (book-mirror, briefings,
|
||||
meeting prep) only becomes magical once the brain holds their real life.
|
||||
OFFER to run the cold-start skill now: it imports Gmail, calendar, and
|
||||
contacts through ClawVisor (clawvisor.com — an OAuth vault; you never hold
|
||||
raw tokens), or offline archives (Google Takeout, a notes folder) if they
|
||||
prefer no third-party gateway. Every phase is consent-gated and
|
||||
independently valuable — they can stop after any one. If they say "later",
|
||||
that is a complete install; they can say "fill my brain" any time.
|
||||
|
||||
Then the routine facts: the capability mode (keyless vs keyed), and the three
|
||||
commands they will actually reuse (`gbrain doctor`, `gbrain bootstrap verify`,
|
||||
`gbrain sources push`). Then delete nothing — this runbook was fetched, not
|
||||
installed.
|
||||
|
||||
+104
-1
@@ -2,6 +2,109 @@
|
||||
|
||||
All notable changes to GBrain will be documented in this file.
|
||||
|
||||
## [0.45.12.0] - 2026-08-13
|
||||
|
||||
**Hermes joins the tested-install club: a real-binary harness now proves gbrain works inside Hermes, and `gbrain friction diff` tells you whether an install problem is the agent's or ours.**
|
||||
|
||||
GBrain has long said "works with OpenClaw and Hermes." For OpenClaw that claim was
|
||||
backed by tests; for Hermes it was backed by hope. This release closes that gap with a
|
||||
full end-to-end harness: a Hermes runner for the claw-test friction lab, a real-binary
|
||||
"door" test that registers gbrain into an actual Hermes install over MCP and asks it to
|
||||
recall a seeded fact, and a CI job that installs a pinned Hermes release and runs the
|
||||
door on demand. Every Hermes CLI behavior the harness relies on was pinned by observing
|
||||
a real install — the flag-order traps, the interactive prompts, the exit-code quirks —
|
||||
and those observations ship as documentation so your own Hermes setup benefits too.
|
||||
|
||||
The live claw-test lane also got honest: it now stages the scenario workspace before the
|
||||
agent starts and verifies real outcomes after it finishes, so an agent that does nothing
|
||||
and exits cleanly finally FAILS the run instead of passing it. And with two runners in
|
||||
the registry, the new `gbrain friction diff --base openclaw --compare hermes` turns
|
||||
friction reports into a comparison instrument: pain unique to one agent is that agent's
|
||||
contract problem; pain common to both is ours.
|
||||
|
||||
## To take advantage of v0.45.12.0
|
||||
|
||||
`gbrain upgrade` is enough — no schema migration.
|
||||
|
||||
1. **Running Hermes?** Wire gbrain in with one command (full guide at
|
||||
`docs/mcp/HERMES.md`, including the non-obvious flag-order and prompt gotchas):
|
||||
```bash
|
||||
printf 'Y\n' | hermes mcp add gbrain --env GBRAIN_HOME=$HOME --connect-timeout 60 --command $(which gbrain) --args serve
|
||||
hermes mcp test gbrain
|
||||
```
|
||||
2. **Want the friction lab on your own agent?**
|
||||
```bash
|
||||
gbrain claw-test --live --agent hermes # or --agent openclaw
|
||||
gbrain friction diff --base openclaw --compare hermes
|
||||
```
|
||||
3. **If anything looks wrong,** file an issue at https://github.com/garrytan/gbrain/issues
|
||||
with `gbrain doctor` output.
|
||||
|
||||
### Itemized changes
|
||||
|
||||
**Added**
|
||||
- Hermes runner for the claw-test harness: `gbrain claw-test --live --agent hermes` drives a real Hermes install headlessly (`$HERMES_BIN` override supported; `--list-agents` shows availability for both runners).
|
||||
- `gbrain friction diff --base <run-or-agent> --compare <run-or-agent>`: cross-agent friction comparison with unique-to-each and changed sections, count deltas, and a compatibility banner that warns when runs cover different scenarios or versions. `--json` for machines.
|
||||
- Real-binary Hermes install door e2e (`test/e2e/install-real-hermes.serial.test.ts`): registers this checkout's gbrain into a hermetic Hermes home via a live MCP handshake (110 tools discovered), verifies both the CLI and direct-config registration surfaces, and proves recall of a seeded fact in a paid one-shot turn. Triple-gated so it can never burn tokens by accident.
|
||||
- Label-gated `hermes-door` CI job: installs a digest-and-tag-pinned Hermes release, refuses to go green if nothing actually ran, and uploads scrubbed evidence on failure.
|
||||
- Per-client MCP docs for Hermes (`docs/mcp/HERMES.md`) and OpenClaw (`docs/mcp/OPENCLAW.md`), plus a dev-facing pin of every observed Hermes CLI behavior (`docs/mcp/HERMES-CLI-PIN.md`) and an install snippet in `INSTALL_FOR_AGENTS.md`.
|
||||
- Generic agent-workspace compatibility test pinning the documented "any repo with a workspace" install flow (detection, scaffold additivity, resolver health).
|
||||
|
||||
**Changed**
|
||||
- claw-test live mode now stages the scenario before the agent runs (fresh-install: brain pages + routing file + init; upgrade: seed replay) and verifies outcomes after it exits — doctor health, a scenario-declared query returning results, expected files existing, and for upgrades a non-mutating schema-version probe that a do-nothing agent cannot satisfy.
|
||||
- The brief handed to live agents now matches the current CLI exactly (extract argument shape, doctor status vocabulary), and bare `gbrain` inside a live run resolves to the harness's own binary via a per-run PATH shim.
|
||||
- Every claw-test run opens and closes with a machine-readable marker carrying the agent name and scenario, so friction analytics can resolve runs by agent; scripted runs are now labeled `scripted` instead of borrowing an agent's name.
|
||||
- Scenario oracle configuration is validated on load — misdeclared oracles fail loudly instead of silently not being enforced.
|
||||
|
||||
**Fixed**
|
||||
- claw-test now works out of the box when gbrain runs from source (`bun run src/cli.ts`): child invocations resolve to a real gbrain launcher instead of the bun runtime itself, which previously made the default harness unusable outside compiled builds.
|
||||
- Upgrade-scenario runs in BOTH modes fail loudly when the scenario ships no seed dump, instead of quietly initializing a current database and reporting an "upgrade" that never exercised a migration.
|
||||
- Every harness child process now runs under a wall-clock timeout, and live-agent timeouts kill the agent's whole process tree — a hung child no longer wedges a run (or a CI job) forever.
|
||||
- Agent-side friction entries now survive the run's tempdir cleanup: they merge into your friction log before the workspace is deleted, so `friction render` and `friction diff` finally see both halves of a live run.
|
||||
- `claw-test --list-agents` no longer races CLI teardown; output is complete and ordered.
|
||||
- Live runs keep the agent's gbrain children pointed at the run's own hermetic brain even when the surrounding shell exports a database-pointing environment variable — the harness's verification and the agent's work can no longer land in two different places.
|
||||
- The test real-name guard now correctly distinguishes the public Hermes platform (documented and tested) from private deployment names (still banned).
|
||||
## [0.45.11.0] - 2026-08-12
|
||||
|
||||
**The install now ends by telling you the two things that matter: you own the brain, and here's the first skill to run.** A working install used to finish on a health report and three tour prompts — technically complete, but a new user walked away without the two facts that make gbrain worth trusting and worth using. Now `gbrain bootstrap verify` ends with a hand-off: **what you own** (every memory is a markdown file in YOUR private GitHub repo — read it, take it to a second machine, delete it and the brain is gone; or the local-only variant with the one command that gives it a durable home) and **what to do next** (run the `cold-start` skill — say "fill my brain" and your agent imports your Gmail, calendar, and contacts through ClawVisor, an OAuth vault so the agent never holds raw tokens, or offline archives like Google Takeout, one consented phase at a time).
|
||||
|
||||
The structural fix underneath: `cold-start` — the skill designed exactly for "I just installed this, now what?" — was excluded from the downstream skill bundle, so the paste-in install audience it was written for could never scaffold it. It's now bundled, it's the #1 recommended skill (ahead of the book-mirror flagship, because every flagship skill only becomes magical once the brain holds your real life), and a new drift guard fails CI if any recommended skill ever becomes unscaffoldable again.
|
||||
|
||||
To take advantage of v0.45.11.0: existing installs can run `gbrain skillpack scaffold cold-start` and say "fill my brain"; fresh installs get the full hand-off automatically.
|
||||
|
||||
### Added
|
||||
- **The verify hand-off block.** On PASS, `gbrain bootstrap verify` prints (and returns in `--json` as `handoff`) the ownership statement — with the actual repo URL, or the local-only variant pointing at `gbrain bootstrap repo` — followed by the cold-start next action. The runbook's Hand off section now instructs the installing agent to make both land ("say them plainly, confirm they landed") and to OFFER running cold-start on the spot.
|
||||
- **`cold-start` ships in the downstream bundle** (61 skills) and leads the recommended set, so the post-install advisory, `gbrain advisor`, and `gbrain skillpack scaffold --all` all surface it. Its prior bundle exclusion ("host onboarding flow") predated the personal-agent bootstrap and was reversed deliberately.
|
||||
- **Recommended-set drift guard**: every recommended slug must be scaffoldable from the plugin bundle — recommended-but-unscaffoldable is a dead-end call-to-action and now fails the suite.
|
||||
|
||||
### Changed
|
||||
- README's Codex and Claude Code paths spell out the same two follow-ups after the click moment: ownership (markdown in a repo you own) and cold-start as the first skill, with ClawVisor named as the credential path and offline archives as the no-gateway alternative.
|
||||
|
||||
**Also in this release — the first-five-minutes DX wave** (re-versioned from an unpublished 0.45.9.0 after the release queue moved):
|
||||
|
||||
**The first five minutes stop making you think.** We built a real-terminal harness that drives the actual install the way a new user does — every picker, prompt, silence window, and line of copy — and then fixed what it surfaced. Keyless `gbrain init` used to dead-end at an error before it created anything; now it just works, keyless, and says so. A fresh brain used to scroll ~240 lines of internal migration names; now it prints one line. The success screen used to bury the one thing to do next under eight competing calls to action; now the copy-paste memory demo is the last, obvious thing on screen. And the "here's the magic" moment in the README now points at the trick that only a brain can do — tell it something, restart, ask for it back — instead of a question your identity files answer for free.
|
||||
|
||||
Under the hood: the upgrade nudge now compares the version you're actually running (a stale or foreign cache can't tell you to upgrade to something you already have), a broken settings file makes the installer stop and tell you rather than quietly replace it, and `gbrain init --supabase` fails loudly in a script instead of pretending it worked. Every fix landed with a test, and a two-model adversarial review pass (Claude + Codex) caught a cluster of follow-on issues in the fixes themselves — a keyless upgrade command that pointed at a rejected path, a compiled-binary detection that broke for renamed binaries — which are fixed here too.
|
||||
|
||||
To take advantage of v0.45.11.0: nothing to do — `gbrain self-upgrade` (or your next `gbrain` invocation's upgrade nudge) brings you current, and the improvements are all in the install/first-run path a new brain hits automatically.
|
||||
|
||||
### Added
|
||||
- **A real-PTY DX exploration harness** (`test/helpers/tty-harness.ts` + `scripts/dx-explore.ts`). It spawns any CLI — gbrain, `claude`, `codex` — under a true pseudo-terminal, timestamps every output burst, and turns silence windows into a measurable stall report, so "the user stared at a frozen screen for nine seconds" is an artifact, not a hunch. A `drive` mode lets an agent steer a live TUI across separate tool calls. Developer instrument only; transcripts are gitignored and nothing in the shipped product depends on it.
|
||||
|
||||
### Changed
|
||||
- **Keyless is now the default when you have no embedding key**, on both the interactive and scripted paths: `gbrain init` completes with a loud, honest "keyless mode — keyword search plus memory your agent writes; everything works" notice instead of exiting with an error. A near-miss key typo still fails loudly (so a fat-fingered `OPENAPI_API_KEY` isn't silently buried). Multiple keys auto-pick the canonical default rather than refusing.
|
||||
- **Fresh-brain init prints one schema-setup line** instead of the full migration replay; upgrades keep the per-migration detail where it has diagnostic value (`GBRAIN_MIGRATE_VERBOSE=1` restores it).
|
||||
- **The init success screen leads with one action** — the three-command memory demo, last on screen — with import/scale-up/health collapsed into a single terse footer and the recommended-skills advisory reduced to a human-voiced pointer.
|
||||
- **The provider picker offers "continue keyless" explicitly** and probe-gates a local Ollama daemon (a running daemon that hasn't pulled the model is annotated, not silently selected); a bare Enter never picks a broken local provider.
|
||||
- **The upgrade nudge tells the truth about your binary**: it compares the running version to the latest and prints the running version, so a stale or foreign-written cache can't nag about an upgrade you already have. The raw machine marker stays off an interactive human's screen (override with `GBRAIN_FORCE_UPGRADE_MARKER=1` for PTY-based agent harnesses that parse it).
|
||||
- **Copy honesty pass**: provider capabilities are attributed per provider (OpenAI unlocks semantic search + fact extraction; Voyage semantic search; Anthropic fact extraction — it has no embeddings API); the install-time estimate reads ~15 minutes for the personal-agent path (~30 for the always-on setup); the first-run tour says to restart first and frames the genuine cross-session round-trip.
|
||||
|
||||
### Fixed
|
||||
- **A parse-broken `.claude/settings.local.json` aborts the hooks write** with a fix-and-re-run message instead of being replaced — your permissions and allowlist are never silently dropped.
|
||||
- **`gbrain init --supabase` in a non-interactive shell fails loudly** (exit 1, names the `--url` escape hatch) instead of the old silent exit-0 that wrote no config.
|
||||
- **`gbrain bootstrap hooks` with a missing harness CLI** now still installs per-turn hooks and reports the phase as partial (so a resuming agent re-runs it once the CLI is on PATH) instead of leaving a false "wire complete".
|
||||
- **`gbrain bootstrap interview --set/--skip` after a confirmation** warns that it voided the read-back instead of failing silently later at render.
|
||||
- Review-pass self-fixes: the keyless upgrade hint now names the re-init command that actually works (not the schema-sizing field `config set` rejects); compiled-binary detection for the detached update refresh no longer breaks for a renamed/official-named binary; the DX harness scrubs copied credentials even on interrupt and reaps the child's whole process tree.
|
||||
## [0.45.10.0] - 2026-08-13
|
||||
|
||||
**21 more community and maintainer bug fixes. Search answers get more complete, sync gets safer, and doctor learns to warn you before a provider dies.**
|
||||
@@ -413,7 +516,7 @@ answers. Ask before anything destructive. You are not done until
|
||||
`gbrain bootstrap verify` exits 0.
|
||||
```
|
||||
|
||||
The agent runs `gbrain bootstrap` — a new command family (`status`, `interview`, `render`, `repo`, `hooks`, `verify`, `attach`, `uninstall`) that drives the whole install. It works with **zero API keys**: your harness's model is the LLM, so the agent authors memory directly and search runs keyword-only; add one optional key (OpenAI, Anthropic, or Voyage) to unlock semantic search and automatic fact extraction. Everything is consent-gated — hooks, background push, MCP scope — and nothing runs while your harness is closed (the honest desktop contract; true 24/7 is what a hosted brain adds).
|
||||
The agent runs `gbrain bootstrap` — a new command family (`status`, `interview`, `render`, `repo`, `hooks`, `verify`, `attach`, `uninstall`) that drives the whole install. It works with **zero API keys**: your harness's model is the LLM, so the agent authors memory directly and search runs keyword-only; add one optional key to upgrade capabilities (OpenAI: semantic search + automatic fact extraction; Voyage: semantic search; Anthropic: fact extraction). Everything is consent-gated — hooks, background push, MCP scope — and nothing runs while your harness is closed (the honest desktop contract; true 24/7 is what a hosted brain adds).
|
||||
|
||||
### What you get
|
||||
|
||||
|
||||
@@ -506,7 +506,7 @@ four numeric segments are required first. Historical 3-segment versions
|
||||
| `CHANGELOG.md` | Top entry header `## [0.31.4.1] - YYYY-MM-DD` plus the "To take advantage of v0.31.4.1" block. | Standard Keep-a-Changelog header. |
|
||||
| `TODOS.md` | Any TODO entries that mention "follow-up from vX.Y.Z.W" use the version of the release that filed them. Update only when filing NEW follow-up TODOs. | Inline `vX.Y.Z.W` references in TODO bodies. |
|
||||
| `CLAUDE.md` | The Key Files section's per-file annotations carry `vX.Y.Z.W (#NNN)` tags noting which release introduced a behavior. Update whenever a wave's annotations get folded in. | Inline `vX.Y.Z.W (#NNN, contributed by @user)` references. |
|
||||
| `openclaw.plugin.json` | OpenClaw plugin manifest (v0.45.6.0, #4033). Hand-maintained; `test/openclaw-plugin-manifest.test.ts` fails the suite if it drifts from `package.json`. Merges from master auto-resolve it to master's version — re-bump it with the trio. | `"version": "0.45.10.0"` |
|
||||
| `openclaw.plugin.json` | OpenClaw plugin manifest (v0.45.6.0, #4033). Hand-maintained; `test/openclaw-plugin-manifest.test.ts` fails the suite if it drifts from `package.json`. Merges from master auto-resolve it to master's version — re-bump it with the trio. | `"version": "0.45.12.0"` |
|
||||
| `BOOTSTRAP_FOR_AGENTS.md` | Runbook stamp on line 1. `scripts/check-bootstrap-tag.sh` (in `bun run verify` + CI) fails when it drifts from `VERSION`; refresh it in the same commit as the bump. | `<!-- gbrain-runbook-stamp: X.Y.Z.W -->` |
|
||||
| `templates/bootstrap/template-repo/` | Vendored template tree with an embedded version stamp. Auto-derived, but NOT by `bun install`: run `bun run scripts/generate-template-repo.ts --out templates/bootstrap/template-repo` after the bump; `scripts/check-bootstrap-templates.sh` fails CI on drift. | `<!-- gbrain-template-stamp: X.Y.Z.W -->` in generated files. |
|
||||
|
||||
|
||||
+14
-3
@@ -90,9 +90,10 @@ NOT inside ~/gbrain.
|
||||
## Step 3.5: Confirm search mode with the user (DO NOT SKIP)
|
||||
|
||||
`gbrain init` auto-applied a default search mode (`tokenmax` unless your subagent
|
||||
tier is Haiku-class or no OpenAI key is configured). The init output included the
|
||||
cost matrix below preceded by `[AGENT]` markers. You must NOT silently accept the
|
||||
default. Stop and ask the operator.
|
||||
tier is Haiku-class or no expansion-capable API key — Anthropic, OpenAI, or
|
||||
Google — is configured). The init output included the cost matrix below preceded
|
||||
by `[AGENT]` markers. You must NOT silently accept the default. Stop and ask the
|
||||
operator.
|
||||
|
||||
**Present this matrix verbatim:**
|
||||
|
||||
@@ -216,6 +217,16 @@ diff against gbrain's bundle when you want upstream improvements. (The legacy
|
||||
`gbrain skillpack install` managed-block model was removed in v0.33 — run
|
||||
`gbrain skillpack migrate-fence` once if upgrading from an older release.)
|
||||
|
||||
**If you are Hermes:** register gbrain as your MCP server:
|
||||
|
||||
```bash
|
||||
printf 'Y\n' | hermes mcp add gbrain --env GBRAIN_HOME=$HOME --connect-timeout 60 --command $(which gbrain) --args serve
|
||||
```
|
||||
|
||||
Keep `--args` last (everything after it becomes server argv) and verify with
|
||||
`hermes mcp test gbrain` — the add exits 0 even on failure. Full reference:
|
||||
[docs/mcp/HERMES.md](docs/mcp/HERMES.md).
|
||||
|
||||
Whether you scaffolded or not, read `skills/RESOLVER.md` (in your workspace, or the
|
||||
bundled copy at `~/gbrain/skills/RESOLVER.md` when running from the cloned repo). It's
|
||||
the skill dispatcher — tells you which skill to read for any task. Save this to your
|
||||
|
||||
@@ -15,7 +15,7 @@ The point of building a 150K-page brain is to use it as a strategic moat. To nev
|
||||
|
||||
It's easier to ship a daemon that runs 24/7 to ingest, enrich, and consolidate than it is to keep an agent in chat working hard. GBrain is that daemon, generalized. Install in 30 minutes. Your agent does the work. As my personal agent gets smarter, so does yours.
|
||||
|
||||
> **~30 minutes to a fully working brain.** Database ready in 2 seconds (PGLite, no server). You just answer questions about API keys.
|
||||
> **~15 minutes to a working personal agent** on the recommended Codex / Claude Code path (mostly a short interview); ~30 minutes for the always-on OpenClaw / Hermes setup. Database ready in 2 seconds either way (PGLite, no server).
|
||||
|
||||
> **LLMs:** fetch [`llms.txt`](llms.txt) for the documentation map, or [`llms-full.txt`](llms-full.txt) for the same map with core docs inlined in one fetch. **Agents:** start with [`AGENTS.md`](AGENTS.md) (or [`CLAUDE.md`](CLAUDE.md) if you're Claude Code).
|
||||
|
||||
@@ -90,7 +90,9 @@ answers. Ask before anything destructive. You are not done until
|
||||
`gbrain bootstrap verify` exits 0.
|
||||
```
|
||||
|
||||
Codex will ask for command approvals during the install — approving them is the sandbox working as intended. What you get, in about 15 minutes: a short interview (6 required questions) → your agent's identity (SOUL.md, USER.md, MEMORY.md) rendered from your own answers, never invented → a local PGLite brain (2 seconds, no server, no Docker) → MCP wired so every session can search and write memory → a **private** GitHub repo, created and privacy-verified, as your agent's durable body. Works with **zero API keys** — keyword search plus memory your agent writes itself; one optional key (OpenAI, Anthropic, or Voyage) upgrades to semantic search and automatic fact extraction. Codex reads brain context through its tools each turn (pull-based).
|
||||
Codex will ask for command approvals during the install — approving them is the sandbox working as intended. What you get, in about 15 minutes: a short interview (6 required questions) → your agent's identity (SOUL.md, USER.md, MEMORY.md) rendered from your own answers, never invented → a local PGLite brain (2 seconds, no server, no Docker) → MCP wired so every session can search and write memory → a **private** GitHub repo, created and privacy-verified, as your agent's durable body. Works with **zero API keys** — keyword search plus memory your agent writes itself; one optional key upgrades capabilities (OpenAI: semantic search + automatic fact extraction; Voyage: semantic search; Anthropic: fact extraction). Codex reads brain context through its tools each turn (pull-based). The click moment: tell it one small thing to remember, restart Codex, then ask for it back — the answer comes from the brain, not from this chat's context (which the restart cleared). That cross-session round-trip is the whole product; "what's my name / my top jobs?" is answered from your identity files, which is nice but not the same trick.
|
||||
|
||||
Two things worth understanding once it's running: **you own the brain** — every memory is a markdown file in that private repo (read it, clone it to a second machine, delete it and the brain is gone) — and **the first skill to run is `cold-start`**: say "fill my brain" and your agent imports your Gmail, calendar, and contacts (via [ClawVisor](https://clawvisor.com), an OAuth vault so the agent never holds raw tokens) or offline archives like Google Takeout, one consented step at a time. An empty brain is a database; a filled one is a memory.
|
||||
|
||||
> **Prefer to make the repo yourself?** Create a new **empty** private repo **under your own GitHub account** (no README/.gitignore/license), clone it, open the clone in Codex, and paste the same block — bootstrap detects your empty repo and adopts it instead of creating one. The repo must be empty and personal-account-owned; org-owned repos are refused (create one under your account, or let bootstrap make it).
|
||||
|
||||
@@ -107,7 +109,7 @@ answers. Ask before anything destructive. You are not done until
|
||||
`gbrain bootstrap verify` exits 0.
|
||||
```
|
||||
|
||||
Everything from the Codex path applies — interview, identity from your own answers, local brain, private repo, keyless mode — plus Claude Code gets **per-turn context hooks**: your brain loads automatically into every prompt, and your work persists to your private repo on a per-turn cadence (debounced ~5 min locally, every turn in a cloud sandbox — this covers the `/exit` case the harness never fires a session-end hook on), with a notice on your next turn if a push ever fails. This works in a **Claude Code cloud session** too, not just on your laptop: verification falls back to pure git protocol when the sandbox blocks the GitHub API, and `gbrain bootstrap cloud-setup-script` prints the environment setup recipe. Restart the session after install and ask "what did I tell you my top jobs were?" — that's the moment it clicks. Full contract, security posture, cloud sandboxes, and uninstall: [docs/guides/bootstrap.md](docs/guides/bootstrap.md).
|
||||
Everything from the Codex path applies — interview, identity from your own answers, local brain, private repo, keyless mode — plus Claude Code gets **per-turn context hooks** (on by default, with an opt-out): your brain loads automatically into every prompt, and your work persists to your private repo on a per-turn cadence (debounced ~5 min locally, every turn in a cloud sandbox — this covers the `/exit` case the harness never fires a session-end hook on), with a notice on your next turn if a push ever fails. This works in a **Claude Code cloud session** too, not just on your laptop: verification falls back to pure git protocol when the sandbox blocks the GitHub API, and `gbrain bootstrap cloud-setup-script` prints the environment setup recipe. The click moment: tell it one small thing to remember, restart the session, then ask for it back — a fresh session has no chat context, so the answer can only come from the brain. That cross-session round-trip is the whole product ("what's my name?" is answered from your identity files — nice, but not the same trick). Same two follow-ups as the Codex path: you own the brain (markdown in your private repo), and `cold-start` is the first skill to run — "fill my brain" imports your email, calendar, and contacts (ClawVisor) or offline archives, one consented step at a time. Full contract, security posture, cloud sandboxes, and uninstall: [docs/guides/bootstrap.md](docs/guides/bootstrap.md).
|
||||
|
||||
> **Prefer to make the repo yourself?** Create a new **empty** private repo **under your own GitHub account** (no README/.gitignore/license), clone it, open the clone in Claude Code (CLI or the desktop app's open-a-repo flow), and paste the same block — bootstrap adopts your empty repo instead of creating one. The repo must be empty and personal-account-owned; org-owned repos are refused.
|
||||
|
||||
@@ -170,6 +172,8 @@ GBrain exposes nearly all of its 100+ operations as MCP tools (stdio and HTTP; a
|
||||
- **[Claude Code](docs/mcp/CLAUDE_CODE.md)** — local: one command, `claude mcp add gbrain -- gbrain serve` (zero server, zero tunnel). Remote with just a bearer token: `gbrain connect https://your-host/mcp --token gbrain_xxx` prints a paste-ready block (or `--install` wires it up and smoke-tests the token).
|
||||
- **[Codex](docs/mcp/CODEX.md)** — `gbrain connect https://your-host/mcp --token gbrain_xxx --agent codex` (or `--install`). Codex reads the bearer from `$GBRAIN_REMOTE_TOKEN` at runtime, so the token never lands in Codex config.
|
||||
- **[Cursor / Windsurf / any stdio MCP client](docs/mcp/CLAUDE_CODE.md)** — same shape, add `{"command": "gbrain", "args": ["serve"]}` to your MCP config.
|
||||
- **[Hermes](docs/mcp/HERMES.md)** — `printf 'Y\n' | hermes mcp add gbrain --env GBRAIN_HOME=$HOME --connect-timeout 60 --command $(which gbrain) --args serve`. Keep `--args` last, and verify with `hermes mcp test gbrain` (the add exits 0 even on failure).
|
||||
- **[OpenClaw](docs/mcp/OPENCLAW.md)** — the ClawHub bundle plugin registers gbrain automatically (`openclaw.plugin.json` ships in this repo), or add `{"command": "gbrain", "args": ["serve"]}` to `~/.openclaw/config.json`'s `mcpServers`.
|
||||
- **[Claude Desktop (Cowork)](docs/mcp/CLAUDE_DESKTOP.md)** — Settings → Integrations → add the URL of your HTTP server. Remote only; the local `claude_desktop_config.json` does not work for remote servers.
|
||||
- **[Claude Cowork (team plan)](docs/mcp/CLAUDE_COWORK.md)** — org Owner adds the connector under Organization Settings → Connectors.
|
||||
- **[Perplexity Computer](docs/mcp/PERPLEXITY.md)** — `gbrain connect https://your-host/mcp --agent perplexity --oauth --register` mints a least-privilege OAuth client and prints the Issuer/Client ID/Secret to paste into Settings → Connectors (OAuth is the right path for a cloud connector; a bearer token also works for local use). Pro subscription required.
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
# TODOS
|
||||
|
||||
## Onboarding DX follow-ups (filed v0.45.9.0)
|
||||
|
||||
- [ ] **Retire the `config set embedding_model` dead-end across ALL surfaces.** v0.45.9.0 fixed the keyless-init notice to point at `gbrain init --force --pglite --embedding-model <id>`, but `src/core/embed-preflight.ts` (lines ~73/83/90/115) and `src/core/embedding-dim-check.ts:78` still advertise `gbrain config set embedding_model <...>`, which `src/commands/config.ts:142` hard-refuses as a schema-sizing no-op. Same dead-end class, different surfaces. Sweep them to the re-init recipe. Priority: P2.
|
||||
- [ ] **`gbrain init --supabase` migrate-model dead-end doc.** The Postgres branch of config.ts points at `docs/embedding-migrations.md`; confirm that doc exists and describes a working switch, or write it. Priority: P3.
|
||||
- [ ] **DX harness binary cache keyed on nothing.** `scripts/dx-explore.ts` reuses `.context/dx-runs/bin/gbrain` unless `--rebuild` is passed, so a second run after code changes can produce transcripts from a stale binary. Key the cache by a source hash (or rebuild when any `src/` file is newer). Dev instrument only. Priority: P3.
|
||||
- [ ] **`verify` has no MCP-registration check.** v0.45.9.0 made `bootstrap status` report the wire phase `partial` when only hooks landed (host CLI missing), but `bootstrap verify` still exits 0 in that state. Add an MCP-registration probe to verify so the "done when verify exits 0" contract also covers MCP. Priority: P2.
|
||||
- [ ] **`hasExpansionKey` misses config-plane keys + init-before-key sequencing.** The mode picker reads `process.env` only; a key routed to the 0600 config by the interview (which runs AFTER init) never influences the auto-selected search mode, and the picker never re-fires. Resolve keys through the capability/gateway fold and consider re-running the recommendation when a key is first configured. Priority: P3.
|
||||
- [ ] **`findEnvKeyTypos` KEY_SHAPE misses no-underscore typos.** `OPENAI_APIKEY` (no `_` before `KEY`) escapes the near-miss net, so that typo class now completes keyless silently instead of failing loud. Widen the regex. Priority: P3.
|
||||
- [ ] **`init-nudge` stale "4 checks" comment + 6-probe accounting.** The header still says "4 onboard checks" but six probes now run; the partial-checks message counts the page-count probe. Cosmetic. Priority: P3.
|
||||
- [ ] **FIRST LIGHT (the real first-magical-moment feature).** The v0.45.9.0 tour rewrite is the ship-now slice; the full seed-phase → compendium → scout design is PR-A (seed phase + Mirror + baton) / PR-B (compendium + scout) with one-way-door decisions (new bootstrap phase, consent key, `skills/first-light/`, a one-time Gate-3 narration exemption). Priority: P2.
|
||||
|
||||
## Ambient recall follow-ups (filed v0.45.7.0, issue #1)
|
||||
|
||||
Deferred from the ambient-recall wave (`context_pack` + `delta` frozen verbs +
|
||||
@@ -3829,28 +3840,99 @@ After the sweep, both should be fixable and renameable back to plain `*.test.ts`
|
||||
|
||||
## claw-test E2E (v0.22.16 follow-ups)
|
||||
|
||||
### Hermes runner — `src/core/claw-test/runners/hermes.ts`
|
||||
**Priority:** P2
|
||||
|
||||
**What:** Add a Hermes implementation of the `AgentRunner` interface. v1 ships only OpenClaw; v1.1 lands hermes once we have real friction reports from openclaw to validate the contract against.
|
||||
|
||||
**Why:** Cross-agent diff (`gbrain friction diff --base openclaw --compare hermes`) is the highest-leverage next signal. Friction unique to one agent vs common-to-both separates "agent contract bug" from "gbrain bug" automatically.
|
||||
|
||||
**Effort:** S (CC ~30m). Depends on: v1 openclaw runner producing real friction reports first.
|
||||
### ~~Hermes runner — `src/core/claw-test/runners/hermes.ts`~~ DONE (hermes-harness wave)
|
||||
Shipped: `HermesRunner` (`hermes -z <brief>`, `$HERMES_BIN` > `which hermes`,
|
||||
`HERMES_HOME` env-allowlist delta) + the full hermes install door
|
||||
(`test/e2e/install-real-hermes.serial.test.ts`, opt-in-gated) + the label-gated
|
||||
`hermes-door` CI job in heavy-tests.yml. The cross-agent
|
||||
`gbrain friction diff --base openclaw --compare hermes` payoff shipped in the
|
||||
same wave (below). Observed-CLI pins live in `docs/mcp/HERMES-CLI-PIN.md` and
|
||||
`docs/mcp/HERMES.md`.
|
||||
|
||||
---
|
||||
|
||||
### Friction analytics suite — `diff` / `trend` / `migration-stub`
|
||||
### Friction analytics suite — `trend` / `migration-stub` (diff SHIPPED)
|
||||
**Priority:** P2
|
||||
|
||||
**What:** Three new `gbrain friction` subcommands deferred from v1:
|
||||
- `gbrain friction diff --base <run-or-agent> --compare <run-or-agent>` (cross-agent comparison; ~80 LOC)
|
||||
**What:** Two remaining `gbrain friction` subcommands deferred from v1
|
||||
(`diff` shipped in the hermes-harness wave — see `src/commands/friction.ts`):
|
||||
- `gbrain friction trend [--since <version-or-date>] [--phase <name>]` (time-series across runs; ~60 LOC)
|
||||
- `gbrain friction migration-stub [--threshold N]` (clusters friction by phase + tokens, emits `skills/migrations/v[N+1].md` stub; ~150 LOC)
|
||||
|
||||
**Why:** Turns point-in-time reports into a slope. Pairs with the v1.1 public scoreboard.
|
||||
|
||||
**Effort:** M (CC ~2h total).
|
||||
**Effort:** M (CC ~1.5h total).
|
||||
|
||||
---
|
||||
|
||||
### Promote hermes-door soft probes to hard assertions + build the REAL cron test
|
||||
**Priority:** P2
|
||||
|
||||
**What:** Two follow-ups now that the hermes CLI surface is pinned (v0.20.0,
|
||||
`docs/mcp/HERMES-CLI-PIN.md`): (1) promote the door's logged-evidence probes
|
||||
(`hermes mcp list` output shape; session-artifact tool-call traces under
|
||||
`<home>/.hermes/`) to hard assertions once a couple of CI runs confirm their
|
||||
stability across hermes releases; (2) build the real cron pairing test — the
|
||||
surface is fully non-interactive (`hermes cron create [--name N] [--no-agent]
|
||||
[--script PATH] <schedule> [prompt]` + `hermes cron tick` runs due jobs once
|
||||
and exits) — create a job that runs `gbrain sync --json`, tick, and assert the
|
||||
sync actually executed against the run's brain. (A self-skipping probe was
|
||||
deliberately CUT in review: a test that cannot fail is not coverage.)
|
||||
|
||||
**Why:** INSTALL_FOR_AGENTS.md's recurring-jobs step has zero coverage; the
|
||||
evidence sweep is the promotion signal the door already logs.
|
||||
|
||||
**Effort:** S-M (CC ~45m). Depends on: first labeled hermes-door CI runs.
|
||||
|
||||
---
|
||||
|
||||
### Wire the orphaned `voice-agent-install` ScenarioKind
|
||||
**Priority:** P2
|
||||
|
||||
**What:** `test/fixtures/claw-test-scenarios/voice-agent-install/` carries the
|
||||
richest install-assertion template in the repo (60-line expected.json:
|
||||
filesystem manifest, `.gbrain-source.json` sha256s, resolver rows, PII
|
||||
blocklist, health probe, tiered soft-fail) but `scenario.json` declares
|
||||
`kind: "voice-agent-install"`, which `ScenarioKind` rejects — the fixture
|
||||
cannot load. Extend `ScenarioKind` + `loadScenario` + a `postInstallHook`
|
||||
implementation so the scenario runs.
|
||||
|
||||
**Why:** Integrations-recipe install coverage (the `gbrain integrations
|
||||
install` path) has a fully-designed scenario sitting dead.
|
||||
|
||||
**Effort:** M (CC ~1h). Integrations-lane work, deliberately kept out of the
|
||||
hermes-harness wave.
|
||||
|
||||
---
|
||||
|
||||
### Cold-install container test — fill the `tests/docker/bootstrap-e2e.sh` placeholder
|
||||
**Priority:** P3
|
||||
|
||||
**What:** heavy-tests.yml carries a gated no-op step for
|
||||
`tests/docker/bootstrap-e2e.sh` (networkless cold-machine container install of
|
||||
gbrain itself: global install, PATH discovery, migrations). The file doesn't
|
||||
exist. Write it.
|
||||
|
||||
**Why:** The agent-platform door tests (claude/codex/hermes) all deliberately
|
||||
run gbrain from the dev tree / compiled binary — none of them proves gbrain's
|
||||
own cold install. That gap was re-flagged in the hermes-harness wave's outside
|
||||
review and scoped OUT of that wave on purpose.
|
||||
|
||||
**Effort:** M (CC ~1-2h, docker).
|
||||
|
||||
---
|
||||
|
||||
### BrainBench hermes adapter
|
||||
**Priority:** P3
|
||||
|
||||
**What:** ~50-100 lines in `src/eval/brainbench/adapters/hermes.ts` + an
|
||||
`ALL_HARNESSES` entry + baseline cells in `evals/brainbench/baselines/main.json`.
|
||||
|
||||
**Why:** Cross-harness memory-conformance coverage for the third platform.
|
||||
Eval seam (memory conformance), NOT install — kept out of the install wave on
|
||||
purpose; needs baseline-governance care per the BrainBench gate rules.
|
||||
|
||||
**Effort:** S-M (CC ~1h + baseline runs).
|
||||
|
||||
---
|
||||
|
||||
@@ -3870,7 +3952,7 @@ After the sweep, both should be fixable and renameable back to plain `*.test.ts`
|
||||
### Real v0.18 SQL dump for upgrade scenario
|
||||
**Priority:** P2
|
||||
|
||||
**What:** The `upgrade-from-v0.18` scenario ships scaffolded — `seed/dump.sql` is missing. The harness gracefully no-ops the seed phase when absent, so the scenario currently behaves like fresh-install. v1.1: generate a real v0.18-shape PGLite dump per the procedure documented in `test/fixtures/claw-test-scenarios/upgrade-from-v0.18/seed/README.md`.
|
||||
**What:** The `upgrade-from-v0.18` scenario ships scaffolded — `seed/dump.sql` is missing. Both scripted and live runs now FAIL LOUDLY on the missing dump (a silent skip used to init a current database and false-green the "upgrade"), so the shipped scenario is unrunnable until the dump lands. Generate a real v0.18-shape PGLite dump per the procedure documented in `test/fixtures/claw-test-scenarios/upgrade-from-v0.18/seed/README.md`.
|
||||
|
||||
**Why:** Without a real seed, the scenario doesn't actually exercise the migration chain forward-walk. That's the whole point of the upgrade scenario — proves issue #239/#243/#266/#357 class regressions stay fixed.
|
||||
|
||||
|
||||
+4
-2
@@ -13,7 +13,7 @@ Mix later if needed.
|
||||
|
||||
## 1. Run with an agent platform
|
||||
|
||||
Already running [OpenClaw](https://github.com/garrytan/openclaw) or [Hermes](https://github.com/garrytan/hermes)?
|
||||
Already running [OpenClaw](https://github.com/garrytan/openclaw) or [Hermes](https://github.com/NousResearch/hermes-agent)?
|
||||
|
||||
```bash
|
||||
bun install -g github:garrytan/gbrain#latest-stable
|
||||
@@ -71,7 +71,7 @@ claude mcp add gbrain -- gbrain serve --surface verbs # Claude Code
|
||||
codex mcp add gbrain -- gbrain serve --surface verbs # Codex
|
||||
```
|
||||
|
||||
The agent spawns `gbrain serve` as a stdio subprocess against your local brain. `--surface verbs` gives the agent the five-verb memory protocol (`recall`, `remember`, `entity`, `synthesize`, `forget` — [MEMORY_VERBS v1](protocol/MEMORY_VERBS_v1.md)) instead of the full tool catalog; drop the flag (default `full`) for every operation. Full walkthrough (both this local path and connecting to a remote brain), plus the brain-first protocol to paste into `CLAUDE.md` / `AGENTS.md`: **[Give your coding agent a memory](tutorials/connect-coding-agent.md)**.
|
||||
The agent spawns `gbrain serve` as a stdio subprocess against your local brain. `--surface verbs` gives the agent the seven-verb memory protocol (`recall`, `remember`, `entity`, `synthesize`, `forget`, `context_pack`, `delta` — [MEMORY_VERBS v1](protocol/MEMORY_VERBS_v1.md)) instead of the full tool catalog; drop the flag (default `full`) for every operation. Full walkthrough (both this local path and connecting to a remote brain), plus the brain-first protocol to paste into `CLAUDE.md` / `AGENTS.md`: **[Give your coding agent a memory](tutorials/connect-coding-agent.md)**.
|
||||
|
||||
## 3. MCP server (any MCP client)
|
||||
|
||||
@@ -98,6 +98,8 @@ Per-client setup guides live in [`docs/mcp/`](mcp/):
|
||||
- [`docs/mcp/CODEX.md`](mcp/CODEX.md)
|
||||
- [`docs/mcp/CLAUDE_DESKTOP.md`](mcp/CLAUDE_DESKTOP.md)
|
||||
- [`docs/mcp/CHATGPT.md`](mcp/CHATGPT.md)
|
||||
- [`docs/mcp/HERMES.md`](mcp/HERMES.md)
|
||||
- [`docs/mcp/OPENCLAW.md`](mcp/OPENCLAW.md)
|
||||
- [`docs/mcp/PERPLEXITY.md`](mcp/PERPLEXITY.md)
|
||||
- [`docs/mcp/DEPLOY.md`](mcp/DEPLOY.md) — production deploy patterns
|
||||
|
||||
|
||||
+4
-1
@@ -282,7 +282,9 @@ E2E tests live in `test/e2e/` and run against real Postgres+pgvector (require `D
|
||||
- `test/e2e/pglite-cli-exit.serial.test.ts` — real spawned-CLI exit behavior on PGLite (in-memory, no `DATABASE_URL`): read commands (`search`/`get`/`query`) exit 0 promptly; CLI_ONLY `capture` exits clean and frees the single-writer lock; the `#2084` describes pin every swept disconnect site — a failed op exits 1 with the error on stderr, and the dashboard, read-only-timeout, doctor, and `dream --dry-run` paths all exit with no force-exit banner.
|
||||
- `test/e2e/pgbouncer-teardown.test.ts` — PgBouncer TRANSACTION-mode teardown (#2084 / the #1972→#2015→#2084 class). Pins the bug CLASS, not timings: a CLI op against a txn-mode pooled URL exits 0 with intact stdout and does NOT ride the 10s hard-deadline backstop (the `engine.disconnect() did not return` banner is the smoking gun — pre-#2084 it printed on 100% of query-shaped ops). Gated by `GBRAIN_PGBOUNCER_URL` + `GBRAIN_PGBOUNCER_DIRECT_URL` (NOT `DATABASE_URL`) — set automatically by `bun run ci:local`'s `pgbouncer` compose service; skips gracefully elsewhere. Uses a DEDICATED `gbrain_pgbouncer` database so it never races the `gbrain_test` TRUNCATE fixtures.
|
||||
- `test/e2e/volunteer-context-postgres.test.ts` — `volunteer_context` on REAL Postgres (#2095; engine parity beyond the hermetic PGLite unit suite): resolution arms through the actual op handler, the fire-and-forget volunteer-event sink landing rows, the stats join, and the RLS pin that `context_volunteer_events` has ROW LEVEL SECURITY enabled (keeps the v35 auto-RLS event trigger honest for migration-created tables). `DATABASE_URL`-gated.
|
||||
- `test/e2e/openclaw-reference-compat.test.ts` — `check-resolvable` + `skillpack install` against a minimal AGENTS.md workspace fixture (`test/fixtures/openclaw-reference-minimal/`), regression guard for the OpenClaw deployment shape.
|
||||
- `test/e2e/openclaw-reference-compat.test.ts` — `check-resolvable` + skillpack install-model against a minimal AGENTS.md workspace fixture (`test/fixtures/openclaw-reference-minimal/`), regression guard for the OpenClaw deployment shape.
|
||||
- `test/e2e/workspace-generic-compat.test.ts` — always-on (PGLite, no binary): pins the INSTALL_FOR_AGENTS.md "any repo with a workspace" contract against `test/fixtures/generic-agents-workspace/` (Hermes is the motivating consumer): `cwd_walk_up` detection, the `GBRAIN_SKILLS_DIR` override, `check-resolvable` on a root AGENTS.md, and scaffold additivity + refuse-overwrite. The real Hermes-behavior proof is the door suite below.
|
||||
- `test/e2e/install-real-hermes.serial.test.ts` — the hermes "door": real `hermes` binary + real `hermes mcp add` handshake (full-catalog tool discovery; the count tracks the op catalog, so the test asserts discovery happened, not a number) + a paid `hermes -z` recall turn against a seeded brain. Triple-gated: `GBRAIN_REAL_HERMES_E2E=1` (explicit opt-in — run-e2e.sh scrubs GBRAIN_*, so it can never fire under `bun run test:e2e`) + resolvable binary + non-empty ANTHROPIC key (anthropic-pinned on purpose: a second provider key flips hermes provider-auto into a mis-routed 401). Hermetic HOME + HERMES_HOME with a tripwire on the operator's real config; evidence copies to `GBRAIN_E2E_EVIDENCE_DIR` for CI upload. Venue: heavy-tests.yml (`real-agent-e2e` + `hermes-door` jobs).
|
||||
- `test/e2e/search-swamp.test.ts` — reproduces the source-swamp case. Seeds a curated `originals/talks/article-outline-fat-code` page against two `<fork>/chat/` pages stuffed with the same multi-word phrase. Asserts the article wins keyword AND vector ranking, that `detail=high` lets the chat swamp re-surface, and that `source_id` passes through the two-stage CTE intact. PGLite in-memory.
|
||||
- `test/e2e/search-exclude.test.ts` — `test/` + `archive/` pages hidden by default, `include_slug_prefixes` opts back in, caller-supplied `exclude_slug_prefixes` adds to defaults. Both keyword and vector search paths.
|
||||
- `test/e2e/engine-parity.test.ts` — Postgres ↔ PGLite top-result and result-set parity for `searchKeyword` + `searchVector` (Postgres ranks pages then picks best chunk while PGLite returns chunks directly, so the source-boost behavior needs parity coverage). Skips without `DATABASE_URL`.
|
||||
@@ -297,6 +299,7 @@ E2E tests live in `test/e2e/` and run against real Postgres+pgvector (require `D
|
||||
- `test/e2e/think-source-isolation-pglite.test.ts` — PGLite in-memory suite pinning the `think` gather stage's source scope: seeds three sources with cross-source links and embedded takes, then asserts `runGather` under a federated `sourceIds` grant (and under a scalar `sourceId`) keeps every stream — hybrid retrieval, takes keyword + vector (`searchTakes`/`searchTakesVector`), and the `traversePaths` graph walk — inside the grant while still reaching authorized neighboring sources. No `DATABASE_URL` needed.
|
||||
- `test/e2e/skill-brain-first.test.ts` — doctor reports `skill_brain_first` check with structured issues; `--fix --dry-run` previews insertion without writing; `--fix` applies the canonical Convention callout idempotently; `brain_first: exempt` frontmatter resolves the warn; `brain_first_typo` surfaces a paste-ready hint; audit JSONL records `detected` / `resolved` / `fixed` transitions; stable brain emits 0 audit lines/run.
|
||||
- Tier 2 (`test/e2e/skills.test.ts`) requires OpenClaw + API keys, runs nightly in CI.
|
||||
- `test/e2e/claw-test.test.ts` also covers live mode token-free via shim agents (`OPENCLAW_BIN=<sh script>`): the success-oracle break path (a do-nothing agent now FAILS), the E0 child-friction merge surviving tempdir cleanup, and the upgrade staging + schema-version-probe regression.
|
||||
- If `.env.testing` doesn't exist in this directory, check sibling worktrees: `find ../ -maxdepth 2 -name .env.testing -print -quit` and copy it here if found.
|
||||
- **Run E2E tests without asking permission.** When you want to verify behavior, there's a relevant E2E test, or you're shipping anything covered by an E2E suite — spin up the test DB, run the tests, tear down. Don't ask, don't propose it, don't defer. The lifecycle is short (~2-30s startup, sub-minute tests, instant teardown) and the gate value is high. Skipping with "DATABASE_URL unset" is silent regression, not caution.
|
||||
|
||||
|
||||
@@ -320,8 +320,8 @@ per-release `**vX.Y.Z:**` narration — CI enforces this
|
||||
- `src/core/operations-descriptions.ts` — Constants module for tool descriptions. Pinned via `test/operations-descriptions.test.ts`. Houses `GET_RECENT_SALIENCE_DESCRIPTION`, `FIND_ANOMALIES_DESCRIPTION`, `GET_RECENT_TRANSCRIPTS_DESCRIPTION` plus `LIST_PAGES_DESCRIPTION`, `QUERY_DESCRIPTION`, `SEARCH_DESCRIPTION`. Stable surface for the Tier-2 LLM routing eval — extracting them keeps the test from binding to whatever was in `operations.ts` at test-run time.
|
||||
- `src/core/cycle/transcript-discovery.ts` — Pure filesystem walk for synthesize. `discoverTranscripts(opts)` filters `.txt` files by date range, min_chars, and word-boundary regex `excludePatterns` (`medical` matches "medical advice" but NOT "comedical"; power users may pass full regex). `readSingleTranscript(path)` is the `gbrain dream --input <file>` ad-hoc path. Self-consumption guard: `DREAM_OUTPUT_MARKER_RE` (anchored at frontmatter open `---\n`, optional BOM + CRLF tolerance, scans first 2000 chars for `dream_generated: true` with case-insensitive value and word boundary on `true`) drives `isDreamOutput(content, bypass=false)`. Both functions skip matching files and emit a `[dream] skipped <basename>: dream_generated marker` stderr log (no silent skips). `bypassGuard?: boolean` on `DiscoverOpts` and `readSingleTranscript`'s opts disables the guard for the explicit `--unsafe-bypass-dream-guard` escape hatch only — never auto-applied for `--input`.
|
||||
- `src/commands/dream.ts` — `gbrain dream` CLI; thin alias over `runCycle`. Flags: `--dry-run`, `--json`, `--phase <name>`, `--pull`, `--dir <path>`, `--input <file>` (ad-hoc transcript, implies `--phase synthesize`), `--date YYYY-MM-DD`, `--from <d> --to <d>` (backfill range), `--unsafe-bypass-dream-guard` (plumbed through `runCycle.synthBypassDreamGuard` → `SynthesizePhaseOpts.bypassDreamGuard` → `discoverTranscripts({bypassGuard})` / `readSingleTranscript({bypassGuard})`; loud stderr warning at synthesize-phase entry; never auto-applied for `--input`). Conflict detection: `--input` + `--date` exits 2. ISO date validation. `--dry-run` runs the Haiku significance verdict but skips Sonnet synthesis (NOT zero LLM calls). Exit 1 on status=failed. `resolveBrainDir` returns `string | null` (order: `--dir` → resolved `--source`'s `local_path` → global `sync.repo_path` → null); a checkout-less postgres/Supabase brain runs DB-only phases (incl. `resolve_symbol_edges`) and skips the 6 filesystem phases with `details.reason: 'no_brain_dir'`; `runDream` owns the only hard error (no checkout AND no engine). When `--source` resolves but has no on-disk checkout, returns null (DB-only) rather than borrowing another source's global `sync.repo_path` (would mix scopes). Pinned by `test/dream-postgres.serial.test.ts`. `--drain [--window <seconds>]` for `--phase extract_atoms`: `runDrain()` bypasses the pack-gate and runs the single-hold bounded drain from `src/core/cycle/extract-atoms-drain.ts` under the same `cycleLockIdFor(sourceId)` the routine cycle uses (concurrent autopilot tick defers with `cycle_already_running`), reporting `{extracted, skipped, remaining}`. Exits `EXIT_DRAIN_INCOMPLETE=3` while `remaining > 0`; a null backlog count (count query FAILED) is also exit 3, never a drained success; `LockUnavailableError` → `cycle_already_running` skip (also exit 3). The `extract_atoms_backlog` doctor check (`computeExtractAtomsBacklogCheck`) surfaces the silent pack-gated backlog with the exact `--drain` command; pack-gated cycle skips carry a greppable `pack_gated:true` marker.
|
||||
- `src/commands/friction.ts` + `src/core/friction.ts` — `gbrain friction {log,render,list,summary}` reporter. Append-only JSONL under `$GBRAIN_HOME/friction/<run-id>.jsonl`. Schema is a flat extension of `StructuredAgentError`. Render groups by severity → phase, defaults to `--redact` for md output (strips `$HOME`/`$CWD` to placeholders so reports paste safely in PRs). Run-id resolves from `--run-id` > `$GBRAIN_FRICTION_RUN_ID` > `standalone.jsonl`. Skills the claw-test exercises gain a `_friction-protocol.md` callout so agents know when to log friction.
|
||||
- `src/commands/claw-test.ts` + `src/core/claw-test/` — `gbrain claw-test [--scenario <name>] [--live --agent openclaw]`. End-to-end "fresh user" friction harness. Two modes: scripted (CI gate, agent-free) and live (real openclaw subprocess, $1–2 in tokens). Sets `GBRAIN_HOME=<tempdir>` for hermeticity and captures gbrain's `--progress-json` events from each child's stderr to verify expected phases ran (`import.files`, `extract.links_fs`, `doctor.db_checks`). Scripted phases: setup → install_brain (`gbrain init --pglite`) → import (`--no-embed`) → query → extract → verify (`gbrain doctor --json`, asserts `status: 'ok'`) → render. Live mode hands `BRIEF.md` from `test/fixtures/claw-test-scenarios/<name>/` to the agent runner. Ships with the OpenClaw runner only (`src/core/claw-test/runners/openclaw.ts`, invokes `openclaw agent --local --agent <name> --message <brief>`); hermes runner deferred. Transcript capture (`transcript-capture.ts`) uses `fs.createWriteStream` with `'drain'`-event backpressure (256KB-burst child-stall fix). Upgrade scenario seeded via `seed-pglite.ts` SQL replay.
|
||||
- `src/commands/friction.ts` + `src/core/friction.ts` — `gbrain friction {log,render,list,summary,diff}` reporter. Append-only JSONL under `$GBRAIN_HOME/.gbrain/friction/<run-id>.jsonl`. Schema is a flat extension of `StructuredAgentError`; every claw-test run opens with a `phase-marker`/`start` meta record carrying `agent` + `scenario` + `harness_schema` (agent-name resolution depends on it). Render groups by severity → phase, defaults to `--redact` for md output (strips `$HOME`/`$CWD` to placeholders so reports paste safely in PRs). `diff --base <run-or-agent> --compare <run-or-agent>` is the cross-agent instrument: exact run-id wins, else agent name resolves to that agent's latest run; identity is `(kind, phase, normalized 80-char message prefix — digit runs collapsed so durations/counts don't split identities)` over `kind ∈ {friction, delight}` as MULTISETS (per-severity counts + totals are the compared attributes: `count_changed` = volume, `severity_changed` = distribution shape via exact integer proportion test, so a delight→friction flip or a 2×error+1×nit → 1×error+2×nit redistribution always surfaces; markers/interrupted feed the compatibility banner, which warns on scenario/version mismatch); output labels sections "unique to <run>" — an instrument, never a blame-attributor. Run-id resolves from `--run-id` > `$GBRAIN_FRICTION_RUN_ID` > `standalone.jsonl`. Skills the claw-test exercises gain a `_friction-protocol.md` callout so agents know when to log friction.
|
||||
- `src/commands/claw-test.ts` + `src/core/claw-test/` — `gbrain claw-test [--scenario <name>] [--live --agent <name>]`. End-to-end "fresh user" friction harness. Two modes: scripted (CI gate, agent-free) and live (real agent subprocess, $1–2 in tokens). Sets `GBRAIN_HOME=<tempdir>` for hermeticity and captures gbrain's `--progress-json` events from each child's stderr to verify expected phases ran (`import.files`, `extract.links_fs`, `doctor.db_checks`). Scripted phases: setup → install_brain (`gbrain init --pglite`) → import (`--no-embed`) → query → extract → verify (`gbrain doctor --json`; top-level status is `healthy|warnings|unhealthy`) → render. Live mode STAGES the scenario before the agent turn (fresh-install: brain pages + AGENTS.md stub + init; upgrade: seed-first via `seed-pglite.ts`, NO init — the migration is the scenario under test), prepends a per-run `gbrain` PATH shim so the BRIEF's bare `gbrain` runs this checkout, hands `BRIEF.md` to the agent runner, then verifies a scenario-declared success ORACLE (`oracle: {query, min_results, files_exist}` in scenario.json; upgrade uses a non-mutating schema-version probe via `readPgliteSchemaVersion` — doctor would auto-migrate and pass a do-nothing agent). Child-side friction merges into the parent's friction file before tempdir cleanup. Two runners ship (`src/core/claw-test/runners/{openclaw,hermes}.ts`): openclaw invokes `openclaw agent --local --agent <name> --message <brief>`; hermes invokes `hermes -z <brief>` (`$HERMES_BIN` > `which hermes`; `HERMES_HOME` passthrough is the env-allowlist delta; shared `BASE_ENV_ALLOWLIST` + `validateBinPathEnv` live in `agent-runner.ts`). Live-lane posture: the OPERATOR's configured agent + hermetic brain; the fully hermetic lane is `test/e2e/install-real-hermes.serial.test.ts`. Transcript capture (`transcript-capture.ts`) uses `fs.createWriteStream` with `'drain'`-event backpressure (256KB-burst child-stall fix). Env knobs (harness escape hatches, all optional): `GBRAIN_BIN_OVERRIDE` (child gbrain binary; validated absolute/no-dotdot/no-metacharacter because it's interpolated into the PATH shim — under the bun runtime the harness otherwise synthesizes a launcher so children never exec bun itself), `GBRAIN_CLAW_PHASE_TIMEOUT_MS` (per-phase child wall clock, default 5 min), `GBRAIN_CLAW_AGENT_TIMEOUT_MS` (live agent turn wall clock, default 10 min).
|
||||
- `skills/_friction-protocol.md` — shared cross-cutting convention skill (like `_brain-filing-rules.md`). Tells agents when to call `gbrain friction log` and how to choose a severity. Routes to friction CLI from any skill the claw-test exercises.
|
||||
- `scripts/check-progress-to-stdout.sh` — CI guard against regressing to `\r`-on-stdout progress. Wired into `bun run test` via `scripts/check-progress-to-stdout.sh && bun test` in package.json.
|
||||
- `docs/progress-events.md` — Canonical JSON event schema reference. Additive only.
|
||||
|
||||
@@ -100,9 +100,11 @@ With zero API keys, everything works: the agent authors memory explicitly throug
|
||||
the brain's write tools (`put_page`, timeline entries, `## Facts` fences — your
|
||||
harness's model is the LLM, already paid for), and search runs keyword-only
|
||||
(BM25). `bootstrap verify` prints the capability report honestly. One optional key
|
||||
(OpenAI, Anthropic, or Voyage) unlocks semantic search and automatic fact
|
||||
extraction; the key goes to the 0600 config file, never into the repo or the
|
||||
interview answers. API spend is metered separately from your subscription and is
|
||||
upgrades capabilities per provider — OpenAI unlocks semantic search and
|
||||
automatic fact extraction; Voyage unlocks semantic search; Anthropic unlocks
|
||||
fact extraction (Anthropic has no embeddings API, so it does not enable
|
||||
semantic search). The key goes to the 0600 config file, never into the repo or
|
||||
the interview answers. API spend is metered separately from your subscription and is
|
||||
zero in keyless mode; with a key, the standard spend gates apply
|
||||
([spend-controls](../operations/spend-controls.md)).
|
||||
|
||||
@@ -214,3 +216,32 @@ Run locally (where both are installed + authed):
|
||||
```bash
|
||||
bun test test/e2e/bootstrap-real-codex.serial.test.ts
|
||||
```
|
||||
|
||||
## DX exploration harness (developer instrument, not a test)
|
||||
|
||||
The door tests prove the install WORKS; they say nothing about how it FEELS.
|
||||
`test/helpers/tty-harness.ts` spawns any CLI (gbrain, `claude`, `codex`) under a
|
||||
real pseudo-terminal (Bun's `terminal:` spawn option) and records every output
|
||||
burst with a millisecond timestamp, so unnecessary pauses become a measurable
|
||||
artifact (`computeStalls` → `stalls.md`) instead of a vibe. Same hermetic env as
|
||||
`agent-harness.ts`; pure helpers are unit-tested in `test/tty-harness.test.ts`
|
||||
(zero subprocesses, PTY smokes self-skip where `terminal:` is unavailable).
|
||||
|
||||
`scripts/dx-explore.ts` drives it to capture the fresh-user funnel as timestamped
|
||||
transcripts under `.context/dx-runs/` (gitignored — nothing asserts, no CI):
|
||||
|
||||
```bash
|
||||
bun run scripts/dx-explore.ts help # comprehension surfaces (no keys)
|
||||
bun run scripts/dx-explore.ts init [--keyless] # interactive init, naive-user autopilot
|
||||
bun run scripts/dx-explore.ts claude-install # REAL claude running the paste-in bootstrap
|
||||
bun run scripts/dx-explore.ts codex-install # REAL codex, same
|
||||
bun run scripts/dx-explore.ts drive -- gbrain init # manual: steer a live TUI via a file channel
|
||||
```
|
||||
|
||||
`drive` mode is how an agent in a Conductor workspace explores a live TUI across
|
||||
separate tool calls: `cat <dir>/session/screen.txt` to watch, append
|
||||
`{"line":"..."}` / `{"key":"Down"}` / `{"stop":true}` to `<dir>/session/input.jsonl`
|
||||
to steer. Each run writes `meta.json`, `visible.txt`, `frames.jsonl`, and
|
||||
`stalls.md`. `--keyless` strips provider keys so the true no-key first-touch path
|
||||
is exercised (a Conductor session's ambient `ANTHROPIC_API_KEY` would otherwise
|
||||
leak in). Install scenarios pay real API cost — launch them as background tasks.
|
||||
|
||||
@@ -20,7 +20,12 @@ schema. The user gets new capabilities automatically.
|
||||
|
||||
gbrain stays current the way gstack does: it rides invocation frequency. A
|
||||
throttled, cache-read-only check runs at the start of every `gbrain` invocation
|
||||
(CLI and MCP) and emits an `UPGRADE_AVAILABLE <old> <new>` marker on stderr. No
|
||||
(CLI and MCP) and emits an `UPGRADE_AVAILABLE <old> <new>` marker on stderr. The
|
||||
raw marker line is suppressed when stderr is an interactive TTY (a human sees
|
||||
only the plain `gbrain X -> Y available` sentence, not the machine token); set
|
||||
`GBRAIN_FORCE_UPGRADE_MARKER=1` if an agent harness parses the token but runs
|
||||
under a PTY. `<old>` is always the RUNNING binary's version, so a stale or
|
||||
foreign-written cache never nags about an upgrade this binary already has. No
|
||||
host cron required — every agent kind (Claude Code, Codex, OpenClaw, Hermes, the
|
||||
`gbrain serve` host behind a Perplexity thin client) converges to current by
|
||||
construction. The behavior is governed by one file-plane config key,
|
||||
|
||||
@@ -20,8 +20,8 @@ claude mcp add gbrain -- gbrain serve --surface verbs
|
||||
That's it. Claude Code spawns `gbrain serve` as a stdio subprocess. No server, no
|
||||
tunnel, no token needed. Works with both PGLite and Supabase engines.
|
||||
|
||||
`--surface verbs` exposes the five-verb memory protocol (`recall`, `remember`,
|
||||
`entity`, `synthesize`, `forget` — [MEMORY_VERBS v1](../protocol/MEMORY_VERBS_v1.md)),
|
||||
`--surface verbs` exposes the seven-verb memory protocol (`recall`, `remember`,
|
||||
`entity`, `synthesize`, `forget`, `context_pack`, `delta` — [MEMORY_VERBS v1](../protocol/MEMORY_VERBS_v1.md)),
|
||||
the surface built for agents and quickstarts. Drop the flag for the full
|
||||
operation catalog (`get_page`, `put_page`, `search`, graph ops, …) — `full` is
|
||||
the default and what existing installs already run.
|
||||
|
||||
+2
-2
@@ -24,8 +24,8 @@ gbrain serve --surface verbs # just the 7 memory verbs (quickstart surface)
|
||||
|
||||
Works with Claude Code, Cursor, Windsurf, and any MCP client that supports stdio.
|
||||
No server, no tunnel, no token needed. Works on both PGLite and Postgres engines.
|
||||
`--surface verbs` exposes exactly the five-verb memory protocol (`recall`,
|
||||
`remember`, `entity`, `synthesize`, `forget` —
|
||||
`--surface verbs` exposes exactly the seven-verb memory protocol (`recall`,
|
||||
`remember`, `entity`, `synthesize`, `forget`, `context_pack`, `delta` —
|
||||
[MEMORY_VERBS v1](../protocol/MEMORY_VERBS_v1.md)) instead of the full catalog;
|
||||
omit the flag (default `full`) for every operation.
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
# Hermes CLI pin — observed behavior notes (v0.20.0)
|
||||
|
||||
Dev-facing companion to [HERMES.md](HERMES.md): every fact below was OBSERVED
|
||||
against a real install (2026-08-12), not researched from docs. The claw-test
|
||||
HermesRunner, the install door e2e, and the heavy-tests hermes-door CI job
|
||||
assert exactly these shapes — when hermes releases change them, update this
|
||||
file, the workflow pins, and the affected assertions together.
|
||||
|
||||
## Pin
|
||||
- **Hermes Agent v0.20.0 (2026.8.3)**, observed against git checkout `3e09adb` at
|
||||
`~/.hermes/hermes-agent` (an upstream-main commit carrying the same v0.20.0/2026.8.3
|
||||
version stamp; CI installs the RELEASE TAG `v2026.8.3` = commit `3c27eb62` — the two
|
||||
differ by post-release main commits, same declared version. If a CI door run ever
|
||||
diverges from these notes, re-observe against the tag checkout.)
|
||||
- Installer sha256: `c118ff31618dc70339049ce71061b8f1351a1c70d9c2a236ed50d8a2550c550d`
|
||||
(download https://hermes-agent.nousresearch.com/install.sh to a file first; verify; then run)
|
||||
- Installer flags used: `--skip-setup --non-interactive`; binary lands at `~/.local/bin/hermes`
|
||||
- Python 3.11.15 via uv
|
||||
|
||||
## HERMES_HOME — HONORED (verified)
|
||||
Installer (`HERMES_HOME="${HERMES_HOME:-$HOME/.hermes}"`) AND runtime both honor it:
|
||||
`mcp add`/`mcp list`/`config set` under `HERMES_HOME=<tmp>` read+write `<tmp>/config.yaml`,
|
||||
populate `<tmp>/{SOUL.md,cron,logs,...}`, and do NOT touch `~/.hermes`. Belt-and-suspenders
|
||||
(HOME + HERMES_HOME both to tmp) stays in the door test anyway.
|
||||
|
||||
## One-shot (`-z`)
|
||||
- `hermes -z "<prompt>"` → **stdout = final text ONLY**; benign notices may appear on stderr
|
||||
("Shell cwd was reset to ..."). Verified reply fidelity ("B0-PROBE-OK").
|
||||
- Exit codes: 0 = success; **1 = no inference provider configured** (message: "agent failed:
|
||||
No inference provider configured. Run 'hermes model' ... or set an API key
|
||||
(OPENROUTER_API_KEY, OPENAI_API_KEY, etc.) in ~/.hermes/.env.")
|
||||
- `--usage-file PATH` exists; per-call `-m MODEL --provider PROVIDER` exist; also
|
||||
`--in DIR`, `--ignore-user-config`, `--safe-mode`, `-t TOOLSETS`, `--skills`.
|
||||
|
||||
## Auth + model pin (non-interactive)
|
||||
- `$HERMES_HOME/.env` with `ANTHROPIC_API_KEY=...` WORKS (verified end-to-end).
|
||||
- Model pin: `hermes config set model.default anthropic/claude-haiku-4.5` → exit 0,
|
||||
writes `model.default` into config.yaml. `hermes config get model.default` reads it back.
|
||||
(`hermes model` is INTERACTIVE-only — never use it in tests/CI.)
|
||||
- Valid model id format: `anthropic/claude-haiku-4.5` (hermes catalog naming, provider-prefixed).
|
||||
|
||||
## `hermes mcp add` — THE big observed facts
|
||||
- Shape: `hermes mcp add <name> [--env K=V K2=V2 ...] [--connect-timeout N] --command CMD --args ...`
|
||||
**`--args` MUST be the last option** — anything after it (incl. a misplaced `--env`) is
|
||||
swallowed into the server argv. (First rehearsal failed exactly this way.)
|
||||
**The env flag takes MULTIPLE KEY=VALUE values after ONE flag; REPEATING it REPLACES the
|
||||
first occurrence** (argparse nargs semantics) — a repeated-flag invocation silently drops
|
||||
the earlier vars, the handshake fails, and the piped Y then hits the save-anyway prompt →
|
||||
the entry is saved with `enabled: false`. (First real door run failed exactly this way.)
|
||||
- Add performs a REAL MCP handshake + tool discovery at add time. Against
|
||||
`--command bun --args run <abs>/src/cli.ts serve` with `--env GBRAIN_HOME=<tmp>`:
|
||||
connected, discovered **110 gbrain tools**.
|
||||
- On success it prompts `Enable all N tools? [Y/n/select]:` — **non-interactive: pipe
|
||||
`printf 'Y\n'`**. Piping Y saves: `✓ Saved 'gbrain' to <HERMES_HOME>/config.yaml (110/110
|
||||
tools enabled)`. EOF on the prompt = `Cancelled.`, nothing saved.
|
||||
- **EXIT CODE IS 0 EVEN ON CONNECTION FAILURE OR CANCEL.** Never assert on `mcp add`'s exit
|
||||
code. Hard assertions = (a) `config.yaml` contains `mcp_servers.<name>` after the add,
|
||||
(b) `hermes mcp test <name>` exits 0.
|
||||
|
||||
## Saved config schema (verbatim shape)
|
||||
```yaml
|
||||
_config_version: 34
|
||||
mcp_servers:
|
||||
gbrain:
|
||||
command: bun
|
||||
args:
|
||||
- run
|
||||
- /abs/path/src/cli.ts
|
||||
- serve
|
||||
env:
|
||||
GBRAIN_HOME: /tmp/gb-xxxx
|
||||
connect_timeout: 60.0
|
||||
enabled: true
|
||||
```
|
||||
(The generated file also contains commented template blocks — security, fallback_model.)
|
||||
|
||||
## Probes
|
||||
- `hermes mcp list` → table `Name / Transport / Tools / Status`, row shows `gbrain ... ✓ enabled`.
|
||||
- `hermes mcp test gbrain` → exit 0 + prints the tool list. THE targeted probe for Test 1b.
|
||||
- `hermes doctor` exists (global health; not a per-server assertion).
|
||||
|
||||
## Cron (for the post-pin F7 TODO — real test is buildable)
|
||||
`hermes cron create [--name NAME] [--deliver ...] [--repeat N] [--skill S] [--script PATH]
|
||||
[--no-agent] [--workdir DIR] [--model M] [--provider P] <schedule> [prompt]` — fully
|
||||
non-interactive. `hermes cron tick` = run due jobs once and exit. `hermes cron list` exists.
|
||||
|
||||
## CI pin values (heavy-tests.yml `hermes-door` job)
|
||||
- `HERMES_VERSION: "0.20.0"`
|
||||
- `HERMES_GIT_TAG: "v2026.8.3"` + `HERMES_GIT_COMMIT: "3c27eb6234bf91b8ceee9e9071591b31e9b148cb"` —
|
||||
the installer's `--branch`/`--commit` flags pin the cloned PAYLOAD (the sha256 below only
|
||||
pins the installer script; without the tag+commit the payload would be upstream main).
|
||||
The flags are asserted, not trusted: post-install the job runs
|
||||
`git -C ~/.hermes/hermes-agent rev-parse HEAD` and loud-fails on any mismatch, so an
|
||||
installer that silently ignores unknown flags (or a moved checkout layout) can never
|
||||
run unpinned upstream code on a runner that later holds secrets.
|
||||
- `HERMES_INSTALL_SHA256: "c118ff31618dc70339049ce71061b8f1351a1c70d9c2a236ed50d8a2550c550d"`
|
||||
- Door test asserts `hermes --version` output contains `v$HERMES_VERSION` when the env var is set.
|
||||
- `hermes --version` output shape: `Hermes Agent v0.20.0 (2026.8.3)` + install dir + python lines.
|
||||
|
||||
## Multi-provider 401 gotcha (door hermeticity)
|
||||
With `model.default` pinned to `anthropic/*` but a SECOND provider key visible (env or
|
||||
.env — e.g. `OPENAI_API_KEY`), hermes's provider-auto mis-routes the request and the turn
|
||||
returns `HTTP 401: Missing Authentication header` as final text with EXIT 0. The door
|
||||
suite therefore seeds exactly ONE key (anthropic) and scrubs all provider env vars from
|
||||
hermes children (`hermesChildEnv` in test/helpers/agent-harness.ts) — the seeded
|
||||
`$HERMES_HOME/.env` is the single auth source.
|
||||
|
||||
## mcp add save-anyway (correction to an earlier note)
|
||||
A piped `Y` saves the entry EVEN when the handshake failed — the save-anyway prompt
|
||||
writes it with `enabled: false`. The success discriminators are `enabled: true` in the
|
||||
saved YAML plus `hermes mcp test <name>` exit 0 — never the add's exit code, and not the
|
||||
mere presence of the config entry.
|
||||
@@ -0,0 +1,120 @@
|
||||
# Connect GBrain to Hermes
|
||||
|
||||
> This page is the MCP-registration reference for Hermes (the NousResearch
|
||||
> `hermes-agent`). For the full brain install — CLI, engine, skills, dream
|
||||
> cycle — follow [INSTALL_FOR_AGENTS.md](../../INSTALL_FOR_AGENTS.md) first;
|
||||
> this page wires the finished brain into Hermes over stdio MCP.
|
||||
|
||||
Hermes spawns `gbrain serve` as a local stdio subprocess. No server, no tunnel,
|
||||
no token needed. Works with both PGLite and Supabase engines.
|
||||
|
||||
## Register (recommended)
|
||||
|
||||
```bash
|
||||
printf 'Y\n' | hermes mcp add gbrain --env GBRAIN_HOME=$HOME --connect-timeout 60 --command $(which gbrain) --args serve
|
||||
```
|
||||
|
||||
`hermes mcp add` performs a real MCP handshake and tool discovery at add time,
|
||||
then prompts `Enable all N tools? [Y/n/select]:`. Three gotchas, all observed:
|
||||
|
||||
- **`--args` must be the LAST option.** Everything after it — including a
|
||||
misplaced `--env` — is swallowed into the server argv. To pass several
|
||||
environment variables, list them all after ONE `--env` flag
|
||||
(`--env A=1 B=2`); repeating the flag replaces the earlier values and the
|
||||
server is saved disabled when its handshake then fails. Put `--env` and
|
||||
`--connect-timeout` before `--command`, exactly as above.
|
||||
- **Pipe the `Y` in non-interactive contexts.** EOF on the enable-tools prompt
|
||||
prints `Cancelled.` and saves nothing. The piped `Y` saves the server with
|
||||
all tools enabled.
|
||||
- **The exit code is 0 even on connection failure or cancel.** Never assert on
|
||||
`mcp add`'s exit status — verify with `hermes mcp list` and
|
||||
`hermes mcp test gbrain` (below).
|
||||
|
||||
## Direct config (equally supported)
|
||||
|
||||
The add command writes an `mcp_servers` block into `$HERMES_HOME/config.yaml`
|
||||
(default `~/.hermes/config.yaml`). You can write it yourself instead:
|
||||
|
||||
```yaml
|
||||
mcp_servers:
|
||||
gbrain:
|
||||
command: gbrain
|
||||
args:
|
||||
- serve
|
||||
env:
|
||||
GBRAIN_HOME: /home/alice-example
|
||||
connect_timeout: 60.0
|
||||
enabled: true
|
||||
```
|
||||
|
||||
To remove gbrain, delete this block (or set `enabled: false` to disable
|
||||
without losing the config).
|
||||
|
||||
## Verify
|
||||
|
||||
```bash
|
||||
hermes mcp list # table row: gbrain ... ✓ enabled
|
||||
hermes mcp test gbrain # exits 0 and prints the discovered tool list
|
||||
```
|
||||
|
||||
Then one real round-trip:
|
||||
|
||||
```bash
|
||||
hermes -z "ask my gbrain brain: what did I import most recently?"
|
||||
```
|
||||
|
||||
`hermes -z` prints the final answer on stdout (benign notices may appear on
|
||||
stderr). Inside Hermes, gbrain's tools appear namespaced as
|
||||
`mcp_gbrain_<tool>` (e.g. `mcp_gbrain_search`).
|
||||
|
||||
## Headless auth + model pin
|
||||
|
||||
For cron jobs, CI, or any non-TTY run, Hermes needs a provider key and a
|
||||
default model configured without the interactive picker:
|
||||
|
||||
- Put the key in `$HERMES_HOME/.env`:
|
||||
|
||||
```bash
|
||||
ANTHROPIC_API_KEY=sk-ant-...
|
||||
# or OPENROUTER_API_KEY / OPENAI_API_KEY
|
||||
```
|
||||
|
||||
- Pin the model non-interactively (`hermes model` is interactive-only — never
|
||||
use it in scripts or CI):
|
||||
|
||||
```bash
|
||||
hermes config set model.default anthropic/claude-haiku-4.5
|
||||
hermes config get model.default # reads it back
|
||||
```
|
||||
|
||||
## Pair with cron
|
||||
|
||||
Hermes cron is fully non-interactive, which makes it a natural scheduler for
|
||||
brain maintenance:
|
||||
|
||||
```bash
|
||||
hermes cron create --name gbrain-sync '0 */4 * * *' 'Run gbrain sync and report anything unusual'
|
||||
hermes cron tick # run due jobs once and exit — deterministic testing
|
||||
hermes cron list
|
||||
```
|
||||
|
||||
See [docs/guides/cron-schedule.md](../guides/cron-schedule.md) for the full
|
||||
brain maintenance protocol (sync, embed, dream cycle).
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **`hermes doctor`** — global health check (installation, config, providers).
|
||||
It's not a per-server assertion; use `hermes mcp test gbrain` for that.
|
||||
- **`agent failed: No inference provider configured`** (exit 1) — Hermes has
|
||||
no model key. Set one in `$HERMES_HOME/.env` and pin `model.default` as
|
||||
above.
|
||||
- **Relocating Hermes** — both the installer and the runtime honor
|
||||
`HERMES_HOME`. All state (`config.yaml`, `.env`, `SOUL.md`, cron, logs)
|
||||
lives under it; the default is `~/.hermes`. Export it consistently or the
|
||||
gbrain registration lands in a config file the runtime never reads.
|
||||
|
||||
---
|
||||
|
||||
Documented against **Hermes Agent v0.20.0 (2026.8.3)**. Dev-facing observed-behavior
|
||||
notes (exact flag semantics, exit-code caveats, CI pin values) live in
|
||||
[HERMES-CLI-PIN.md](HERMES-CLI-PIN.md).
|
||||
@@ -0,0 +1,62 @@
|
||||
# Connect GBrain to OpenClaw
|
||||
|
||||
> This page is the MCP-registration reference card. For the full brain install
|
||||
> — CLI, engine, skills, dream cycle — follow
|
||||
> [INSTALL_FOR_AGENTS.md](../../INSTALL_FOR_AGENTS.md); the README covers the
|
||||
> bootstrap and connect paths.
|
||||
|
||||
Two supported shapes, both stdio.
|
||||
|
||||
## Option 1: ClawHub bundle plugin
|
||||
|
||||
GBrain ships [`openclaw.plugin.json`](../../openclaw.plugin.json) at the repo
|
||||
root. Installing the bundle plugin registers the MCP server for you — the
|
||||
manifest carries an `mcpServers.gbrain` entry (`./bin/gbrain serve`) plus the
|
||||
bundled skills — and declares the `gbrain-context` context engine. To route
|
||||
OpenClaw's context-engine slot through gbrain, set:
|
||||
|
||||
```
|
||||
plugins.slots.contextEngine = gbrain-context
|
||||
```
|
||||
|
||||
## Option 2: Direct `~/.openclaw/config.json`
|
||||
|
||||
The same shape gbrain's own CI uses (see the "Configure OpenClaw MCP" step in
|
||||
`.github/workflows/e2e.yml`):
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"gbrain": {
|
||||
"command": "gbrain",
|
||||
"args": ["serve"],
|
||||
"env": {
|
||||
"DATABASE_URL": "postgresql://...",
|
||||
"GBRAIN_HOME": "/home/alice-example"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `env` block is optional: a PGLite brain needs no `DATABASE_URL`, and
|
||||
`GBRAIN_HOME` only matters when the brain home isn't `~/.gbrain`. Append
|
||||
`"--surface", "verbs"` to `args` for the seven-verb memory protocol
|
||||
([MEMORY_VERBS v1](../protocol/MEMORY_VERBS_v1.md)) instead of the full
|
||||
operation catalog.
|
||||
|
||||
## Verify
|
||||
|
||||
Start an agent turn and ask it to use the brain:
|
||||
|
||||
```
|
||||
Call get_brain_identity, then search my brain for [topic].
|
||||
```
|
||||
|
||||
If the tools respond, the wiring works. `list_skills` shows everything the
|
||||
brain can do (gated by `mcp.publish_skills` on the host).
|
||||
|
||||
## Remove
|
||||
|
||||
Delete the `mcpServers.gbrain` block from `~/.openclaw/config.json`, or
|
||||
uninstall the bundle plugin.
|
||||
@@ -156,8 +156,8 @@ codex mcp add gbrain -- gbrain serve --surface verbs
|
||||
That's the whole wire-up. No token, no URL, no tunnel. The agent spawns
|
||||
`gbrain serve` as a stdio subprocess and talks to your local brain directly.
|
||||
|
||||
`--surface verbs` exposes exactly the five-verb memory protocol
|
||||
(`recall`, `remember`, `entity`, `synthesize`, `forget` —
|
||||
`--surface verbs` exposes exactly the seven-verb memory protocol
|
||||
(`recall`, `remember`, `entity`, `synthesize`, `forget`, `context_pack`, `delta` —
|
||||
[MEMORY_VERBS v1](../protocol/MEMORY_VERBS_v1.md), frozen + additive-forever)
|
||||
instead of the full operation catalog, so the agent sees a tight, stable surface
|
||||
instead of a 110-tool wall. Drop the flag (or pass `--surface full`) for every
|
||||
@@ -192,12 +192,12 @@ about people, companies, decisions, projects, or past context:
|
||||
tokens → `search` (cheap hybrid, no expansion). Concept, landscape, or
|
||||
"all the X that do Y" questions → `query` FIRST — it recovers synonym
|
||||
phrasings `search` misses, and a populated `search` result set is not proof
|
||||
of coverage. On the five-verb surface the same split is `recall` (retrieve)
|
||||
of coverage. On the seven-verb surface the same split is `recall` (retrieve)
|
||||
vs `synthesize` (reasoned answer). Check the brain BEFORE answering from
|
||||
memory or asking me. Never ask "who is X?" or "what did we decide about Y?"
|
||||
before checking — the brain probably already knows.
|
||||
2. **Write back.** When I make a decision, mention a new person/company, or land
|
||||
on an idea worth keeping, write it to the brain: `remember` on the five-verb
|
||||
on an idea worth keeping, write it to the brain: `remember` on the seven-verb
|
||||
surface (one fact, with provenance), or `put_page` on the full surface
|
||||
(entity pages under people/, companies/; decisions under decisions/ or
|
||||
notes/). One insight, one page, linked.
|
||||
@@ -222,7 +222,7 @@ hundreds of linked pages and patterns you didn't know were there.
|
||||
**3. Briefing from your brain (not from the internet).** *"What do I need to know
|
||||
before my 2pm with the Acme team?"* pulls your meeting history, the people,
|
||||
what's still open, what the brain doesn't know yet. The agent does your prep
|
||||
because it read your context. (`query` — `synthesize` on the five-verb surface —
|
||||
because it read your context. (`query` — `synthesize` on the seven-verb surface —
|
||||
gives you the synthesized answer with citations; this is the example on the
|
||||
[README](../../README.md).)
|
||||
|
||||
|
||||
+24
-9
@@ -661,7 +661,7 @@ four numeric segments are required first. Historical 3-segment versions
|
||||
| `CHANGELOG.md` | Top entry header `## [0.31.4.1] - YYYY-MM-DD` plus the "To take advantage of v0.31.4.1" block. | Standard Keep-a-Changelog header. |
|
||||
| `TODOS.md` | Any TODO entries that mention "follow-up from vX.Y.Z.W" use the version of the release that filed them. Update only when filing NEW follow-up TODOs. | Inline `vX.Y.Z.W` references in TODO bodies. |
|
||||
| `CLAUDE.md` | The Key Files section's per-file annotations carry `vX.Y.Z.W (#NNN)` tags noting which release introduced a behavior. Update whenever a wave's annotations get folded in. | Inline `vX.Y.Z.W (#NNN, contributed by @user)` references. |
|
||||
| `openclaw.plugin.json` | OpenClaw plugin manifest (v0.45.6.0, #4033). Hand-maintained; `test/openclaw-plugin-manifest.test.ts` fails the suite if it drifts from `package.json`. Merges from master auto-resolve it to master's version — re-bump it with the trio. | `"version": "0.45.10.0"` |
|
||||
| `openclaw.plugin.json` | OpenClaw plugin manifest (v0.45.6.0, #4033). Hand-maintained; `test/openclaw-plugin-manifest.test.ts` fails the suite if it drifts from `package.json`. Merges from master auto-resolve it to master's version — re-bump it with the trio. | `"version": "0.45.12.0"` |
|
||||
| `BOOTSTRAP_FOR_AGENTS.md` | Runbook stamp on line 1. `scripts/check-bootstrap-tag.sh` (in `bun run verify` + CI) fails when it drifts from `VERSION`; refresh it in the same commit as the bump. | `<!-- gbrain-runbook-stamp: X.Y.Z.W -->` |
|
||||
| `templates/bootstrap/template-repo/` | Vendored template tree with an embedded version stamp. Auto-derived, but NOT by `bun install`: run `bun run scripts/generate-template-repo.ts --out templates/bootstrap/template-repo` after the bump; `scripts/check-bootstrap-templates.sh` fails CI on drift. | `<!-- gbrain-template-stamp: X.Y.Z.W -->` in generated files. |
|
||||
|
||||
@@ -1106,9 +1106,10 @@ NOT inside ~/gbrain.
|
||||
## Step 3.5: Confirm search mode with the user (DO NOT SKIP)
|
||||
|
||||
`gbrain init` auto-applied a default search mode (`tokenmax` unless your subagent
|
||||
tier is Haiku-class or no OpenAI key is configured). The init output included the
|
||||
cost matrix below preceded by `[AGENT]` markers. You must NOT silently accept the
|
||||
default. Stop and ask the operator.
|
||||
tier is Haiku-class or no expansion-capable API key — Anthropic, OpenAI, or
|
||||
Google — is configured). The init output included the cost matrix below preceded
|
||||
by `[AGENT]` markers. You must NOT silently accept the default. Stop and ask the
|
||||
operator.
|
||||
|
||||
**Present this matrix verbatim:**
|
||||
|
||||
@@ -1232,6 +1233,16 @@ diff against gbrain's bundle when you want upstream improvements. (The legacy
|
||||
`gbrain skillpack install` managed-block model was removed in v0.33 — run
|
||||
`gbrain skillpack migrate-fence` once if upgrading from an older release.)
|
||||
|
||||
**If you are Hermes:** register gbrain as your MCP server:
|
||||
|
||||
```bash
|
||||
printf 'Y\n' | hermes mcp add gbrain --env GBRAIN_HOME=$HOME --connect-timeout 60 --command $(which gbrain) --args serve
|
||||
```
|
||||
|
||||
Keep `--args` last (everything after it becomes server argv) and verify with
|
||||
`hermes mcp test gbrain` — the add exits 0 even on failure. Full reference:
|
||||
[docs/mcp/HERMES.md](docs/mcp/HERMES.md).
|
||||
|
||||
Whether you scaffolded or not, read `skills/RESOLVER.md` (in your workspace, or the
|
||||
bundled copy at `~/gbrain/skills/RESOLVER.md` when running from the cloned repo). It's
|
||||
the skill dispatcher — tells you which skill to read for any task. Save this to your
|
||||
@@ -1596,7 +1607,7 @@ The point of building a 150K-page brain is to use it as a strategic moat. To nev
|
||||
|
||||
It's easier to ship a daemon that runs 24/7 to ingest, enrich, and consolidate than it is to keep an agent in chat working hard. GBrain is that daemon, generalized. Install in 30 minutes. Your agent does the work. As my personal agent gets smarter, so does yours.
|
||||
|
||||
> **~30 minutes to a fully working brain.** Database ready in 2 seconds (PGLite, no server). You just answer questions about API keys.
|
||||
> **~15 minutes to a working personal agent** on the recommended Codex / Claude Code path (mostly a short interview); ~30 minutes for the always-on OpenClaw / Hermes setup. Database ready in 2 seconds either way (PGLite, no server).
|
||||
|
||||
> **LLMs:** fetch [`llms.txt`](llms.txt) for the documentation map, or [`llms-full.txt`](llms-full.txt) for the same map with core docs inlined in one fetch. **Agents:** start with [`AGENTS.md`](AGENTS.md) (or [`CLAUDE.md`](CLAUDE.md) if you're Claude Code).
|
||||
|
||||
@@ -1671,7 +1682,9 @@ answers. Ask before anything destructive. You are not done until
|
||||
`gbrain bootstrap verify` exits 0.
|
||||
```
|
||||
|
||||
Codex will ask for command approvals during the install — approving them is the sandbox working as intended. What you get, in about 15 minutes: a short interview (6 required questions) → your agent's identity (SOUL.md, USER.md, MEMORY.md) rendered from your own answers, never invented → a local PGLite brain (2 seconds, no server, no Docker) → MCP wired so every session can search and write memory → a **private** GitHub repo, created and privacy-verified, as your agent's durable body. Works with **zero API keys** — keyword search plus memory your agent writes itself; one optional key (OpenAI, Anthropic, or Voyage) upgrades to semantic search and automatic fact extraction. Codex reads brain context through its tools each turn (pull-based).
|
||||
Codex will ask for command approvals during the install — approving them is the sandbox working as intended. What you get, in about 15 minutes: a short interview (6 required questions) → your agent's identity (SOUL.md, USER.md, MEMORY.md) rendered from your own answers, never invented → a local PGLite brain (2 seconds, no server, no Docker) → MCP wired so every session can search and write memory → a **private** GitHub repo, created and privacy-verified, as your agent's durable body. Works with **zero API keys** — keyword search plus memory your agent writes itself; one optional key upgrades capabilities (OpenAI: semantic search + automatic fact extraction; Voyage: semantic search; Anthropic: fact extraction). Codex reads brain context through its tools each turn (pull-based). The click moment: tell it one small thing to remember, restart Codex, then ask for it back — the answer comes from the brain, not from this chat's context (which the restart cleared). That cross-session round-trip is the whole product; "what's my name / my top jobs?" is answered from your identity files, which is nice but not the same trick.
|
||||
|
||||
Two things worth understanding once it's running: **you own the brain** — every memory is a markdown file in that private repo (read it, clone it to a second machine, delete it and the brain is gone) — and **the first skill to run is `cold-start`**: say "fill my brain" and your agent imports your Gmail, calendar, and contacts (via [ClawVisor](https://clawvisor.com), an OAuth vault so the agent never holds raw tokens) or offline archives like Google Takeout, one consented step at a time. An empty brain is a database; a filled one is a memory.
|
||||
|
||||
> **Prefer to make the repo yourself?** Create a new **empty** private repo **under your own GitHub account** (no README/.gitignore/license), clone it, open the clone in Codex, and paste the same block — bootstrap detects your empty repo and adopts it instead of creating one. The repo must be empty and personal-account-owned; org-owned repos are refused (create one under your account, or let bootstrap make it).
|
||||
|
||||
@@ -1688,7 +1701,7 @@ answers. Ask before anything destructive. You are not done until
|
||||
`gbrain bootstrap verify` exits 0.
|
||||
```
|
||||
|
||||
Everything from the Codex path applies — interview, identity from your own answers, local brain, private repo, keyless mode — plus Claude Code gets **per-turn context hooks**: your brain loads automatically into every prompt, and your work persists to your private repo on a per-turn cadence (debounced ~5 min locally, every turn in a cloud sandbox — this covers the `/exit` case the harness never fires a session-end hook on), with a notice on your next turn if a push ever fails. This works in a **Claude Code cloud session** too, not just on your laptop: verification falls back to pure git protocol when the sandbox blocks the GitHub API, and `gbrain bootstrap cloud-setup-script` prints the environment setup recipe. Restart the session after install and ask "what did I tell you my top jobs were?" — that's the moment it clicks. Full contract, security posture, cloud sandboxes, and uninstall: [docs/guides/bootstrap.md](docs/guides/bootstrap.md).
|
||||
Everything from the Codex path applies — interview, identity from your own answers, local brain, private repo, keyless mode — plus Claude Code gets **per-turn context hooks** (on by default, with an opt-out): your brain loads automatically into every prompt, and your work persists to your private repo on a per-turn cadence (debounced ~5 min locally, every turn in a cloud sandbox — this covers the `/exit` case the harness never fires a session-end hook on), with a notice on your next turn if a push ever fails. This works in a **Claude Code cloud session** too, not just on your laptop: verification falls back to pure git protocol when the sandbox blocks the GitHub API, and `gbrain bootstrap cloud-setup-script` prints the environment setup recipe. The click moment: tell it one small thing to remember, restart the session, then ask for it back — a fresh session has no chat context, so the answer can only come from the brain. That cross-session round-trip is the whole product ("what's my name?" is answered from your identity files — nice, but not the same trick). Same two follow-ups as the Codex path: you own the brain (markdown in your private repo), and `cold-start` is the first skill to run — "fill my brain" imports your email, calendar, and contacts (ClawVisor) or offline archives, one consented step at a time. Full contract, security posture, cloud sandboxes, and uninstall: [docs/guides/bootstrap.md](docs/guides/bootstrap.md).
|
||||
|
||||
> **Prefer to make the repo yourself?** Create a new **empty** private repo **under your own GitHub account** (no README/.gitignore/license), clone it, open the clone in Claude Code (CLI or the desktop app's open-a-repo flow), and paste the same block — bootstrap adopts your empty repo instead of creating one. The repo must be empty and personal-account-owned; org-owned repos are refused.
|
||||
|
||||
@@ -1751,6 +1764,8 @@ GBrain exposes nearly all of its 100+ operations as MCP tools (stdio and HTTP; a
|
||||
- **[Claude Code](docs/mcp/CLAUDE_CODE.md)** — local: one command, `claude mcp add gbrain -- gbrain serve` (zero server, zero tunnel). Remote with just a bearer token: `gbrain connect https://your-host/mcp --token gbrain_xxx` prints a paste-ready block (or `--install` wires it up and smoke-tests the token).
|
||||
- **[Codex](docs/mcp/CODEX.md)** — `gbrain connect https://your-host/mcp --token gbrain_xxx --agent codex` (or `--install`). Codex reads the bearer from `$GBRAIN_REMOTE_TOKEN` at runtime, so the token never lands in Codex config.
|
||||
- **[Cursor / Windsurf / any stdio MCP client](docs/mcp/CLAUDE_CODE.md)** — same shape, add `{"command": "gbrain", "args": ["serve"]}` to your MCP config.
|
||||
- **[Hermes](docs/mcp/HERMES.md)** — `printf 'Y\n' | hermes mcp add gbrain --env GBRAIN_HOME=$HOME --connect-timeout 60 --command $(which gbrain) --args serve`. Keep `--args` last, and verify with `hermes mcp test gbrain` (the add exits 0 even on failure).
|
||||
- **[OpenClaw](docs/mcp/OPENCLAW.md)** — the ClawHub bundle plugin registers gbrain automatically (`openclaw.plugin.json` ships in this repo), or add `{"command": "gbrain", "args": ["serve"]}` to `~/.openclaw/config.json`'s `mcpServers`.
|
||||
- **[Claude Desktop (Cowork)](docs/mcp/CLAUDE_DESKTOP.md)** — Settings → Integrations → add the URL of your HTTP server. Remote only; the local `claude_desktop_config.json` does not work for remote servers.
|
||||
- **[Claude Cowork (team plan)](docs/mcp/CLAUDE_COWORK.md)** — org Owner adds the connector under Organization Settings → Connectors.
|
||||
- **[Perplexity Computer](docs/mcp/PERPLEXITY.md)** — `gbrain connect https://your-host/mcp --agent perplexity --oauth --register` mints a least-privilege OAuth client and prints the Issuer/Client ID/Secret to paste into Settings → Connectors (OAuth is the right path for a cloud connector; a bearer token also works for local use). Pro subscription required.
|
||||
@@ -3986,8 +4001,8 @@ gbrain serve --surface verbs # just the 7 memory verbs (quickstart surface)
|
||||
|
||||
Works with Claude Code, Cursor, Windsurf, and any MCP client that supports stdio.
|
||||
No server, no tunnel, no token needed. Works on both PGLite and Postgres engines.
|
||||
`--surface verbs` exposes exactly the five-verb memory protocol (`recall`,
|
||||
`remember`, `entity`, `synthesize`, `forget` —
|
||||
`--surface verbs` exposes exactly the seven-verb memory protocol (`recall`,
|
||||
`remember`, `entity`, `synthesize`, `forget`, `context_pack`, `delta` —
|
||||
[MEMORY_VERBS v1](../protocol/MEMORY_VERBS_v1.md)) instead of the full catalog;
|
||||
omit the flag (default `full`) for every operation.
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"id": "gbrain-context-engine",
|
||||
"name": "gbrain",
|
||||
"version": "0.45.10.0",
|
||||
"version": "0.45.12.0",
|
||||
"description": "Personal knowledge brain with Postgres + pgvector hybrid search",
|
||||
"family": "bundle-plugin",
|
||||
"configSchema": {
|
||||
@@ -47,6 +47,7 @@
|
||||
"skills/capture",
|
||||
"skills/citation-fixer",
|
||||
"skills/citation-graph-ingest",
|
||||
"skills/cold-start",
|
||||
"skills/company-brainify",
|
||||
"skills/concept-synthesis",
|
||||
"skills/context-audit",
|
||||
|
||||
+1
-1
@@ -154,7 +154,7 @@
|
||||
"bun": ">=1.3.10"
|
||||
},
|
||||
"license": "MIT",
|
||||
"version": "0.45.10.0",
|
||||
"version": "0.45.12.0",
|
||||
"overrides": {
|
||||
"@hono/node-server": "^2.0.5",
|
||||
"fast-uri": "^3.1.5",
|
||||
|
||||
@@ -32,7 +32,12 @@ cd "$ROOT"
|
||||
BANNED_NAMES=(
|
||||
'Diana' # Diana Hu, named in CLAUDE.md privacy example
|
||||
'Wintermute' # private OpenClaw fork name (CLAUDE.md rule)
|
||||
'Hermes' # downstream agent fork name
|
||||
# 'Hermes' removed (hermes-harness wave): here it names NousResearch/hermes-agent,
|
||||
# a PUBLIC platform gbrain documents (README hero, INSTALL_FOR_AGENTS.md) and now
|
||||
# tests against (claw-test hermes runner, install door e2e).
|
||||
# test/readme-hero-anchors.test.ts REQUIRES the README to mention it. The original
|
||||
# scrub targeted conflating the public agent with PRIVATE deployment names — those
|
||||
# (Wintermute, and any future private fork names) remain banned above/below.
|
||||
'Technium' # real GP handle
|
||||
'McGrew' # ex-OpenAI exec
|
||||
'YC Labs' # internal team name
|
||||
@@ -57,9 +62,6 @@ ALLOWLIST=(
|
||||
"test/recency-decay.test.ts:Wintermute" # regression-prevention test asserting wintermute is absent (structural)
|
||||
"test/scripts/check-proposal-pii.test.ts:Wintermute" # privacy-guard test asserting docs/proposals/ rejects wintermute (structural; same meta-rule exception as check-privacy.sh)
|
||||
"test/scripts/check-proposal-pii.test.ts:WINTERMUTE" # case-insensitive sentinel literal for the same privacy-guard test
|
||||
"test/serve-stdio-lifecycle.test.ts:Hermes" # comment naming a downstream-agent scenario — pre-existing, low signal
|
||||
"test/extract.test.ts:Hermes" # markdown-link extraction test fixture — pre-existing, ambiguous (Greek god vs fork)
|
||||
"test/readme-hero-anchors.test.ts:Hermes" # v0.36.0.0 D9 anchor test — asserts README mentions Hermes as a credit
|
||||
"test/readme-hero-anchors.test.ts:OpenClaw" # v0.36.0.0 D9 anchor test — asserts README mentions OpenClaw as a credit
|
||||
# v0.36.0.0: skillpack-harvest privacy linter tests structurally
|
||||
# require the literal "Wintermute" to verify the linter catches it.
|
||||
|
||||
@@ -0,0 +1,723 @@
|
||||
/**
|
||||
* dx-explore — drive the REAL fresh-user experience under a PTY and record it.
|
||||
*
|
||||
* The e2e door tests (test/e2e/bootstrap-real-{claude,codex}.serial.test.ts)
|
||||
* prove the install WORKS headlessly. This script captures what installing
|
||||
* FEELS like: every picker, prompt, spinner, silence window, and line of copy
|
||||
* a fresh user sees, as timestamped transcripts ready for a
|
||||
* Don't-Make-Me-Think DX audit. It is a developer instrument, not a test —
|
||||
* transcripts land in .context/dx-runs/ (gitignored) and nothing asserts.
|
||||
*
|
||||
* Scenarios (all hermetic — temp HOME/GBRAIN_HOME/CLAUDE_CONFIG_DIR/CODEX_HOME;
|
||||
* the operator's real config is never WRITTEN. Two narrow reads exist for
|
||||
* auth: codex-install copies ~/.codex/auth.json into the temp CODEX_HOME, and
|
||||
* the claude seed records the API key's last 20 chars — both copies are
|
||||
* scrubbed at cleanup even under --keep, so no credential material outlives
|
||||
* the run):
|
||||
*
|
||||
* help First-touch comprehension surfaces: bare `gbrain`,
|
||||
* `gbrain --help`, `gbrain init --help`, `gbrain bootstrap
|
||||
* --help`, `gbrain bootstrap` bare. Cheap, no keys.
|
||||
* init Interactive `gbrain init` (keyless) with a naive-user
|
||||
* autopilot: wait for each screen to settle, snapshot it,
|
||||
* press Enter (accept the default), repeat. What a user who
|
||||
* "just hits Enter" experiences, with stall timing.
|
||||
* claude-install REAL interactive `claude` in a fresh empty workspace,
|
||||
* driven by the README paste block pointed at THIS repo's
|
||||
* BOOTSTRAP_FOR_AGENTS.md, with a scripted persona appendix
|
||||
* so the interview completes unattended. Pays real API cost;
|
||||
* takes 10-25 min. Run in background and watch session/screen.txt.
|
||||
* codex-install Same for REAL `codex` (interactive TUI).
|
||||
* drive -- <cmd> Manual mode: spawn ANY command under the PTY and steer it
|
||||
* across separate shell calls via a file control channel:
|
||||
* watch: cat <dir>/session/screen.txt
|
||||
* type: echo '{"line":"hello"}' >> <dir>/session/input.jsonl
|
||||
* keys: echo '{"key":"Down"}' >> <dir>/session/input.jsonl
|
||||
* note: echo '{"note":"picker confuses me"}' >> ...
|
||||
* stop: echo '{"stop":true}' >> ...
|
||||
* {"line": ...} sends text + Enter; {"send": ...} sends raw
|
||||
* bytes (mind that zsh `echo` mangles \r — prefer "line").
|
||||
* Launch as a background task; this is how an agent in
|
||||
* Conductor explores a live TUI across tool calls.
|
||||
*
|
||||
* Usage:
|
||||
* bun run scripts/dx-explore.ts help
|
||||
* bun run scripts/dx-explore.ts init
|
||||
* bun run scripts/dx-explore.ts claude-install
|
||||
* bun run scripts/dx-explore.ts codex-install
|
||||
* bun run scripts/dx-explore.ts drive [--no-hermetic-home] -- gbrain init
|
||||
* Options: --dir <out> transcript dir (default .context/dx-runs/<scenario>-<ts>)
|
||||
* --gbrain <bin> use an existing gbrain binary (default: compile+cache)
|
||||
* --rebuild force recompile of the cached binary
|
||||
* --keep keep hermetic temp homes for forensics
|
||||
*
|
||||
* Output bundle per scenario dir: meta.json, raw.txt, visible.txt,
|
||||
* frames.jsonl, stalls.md, events.jsonl (inputs/notes timeline), steps.md
|
||||
* (autopilot screen-by-screen), session/ (live: screen.txt, status.json).
|
||||
*
|
||||
* Progress prints to stderr; the transcript dir path is the only stdout line
|
||||
* (pipe-friendly), matching the repo's progress discipline.
|
||||
*/
|
||||
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import * as fs from 'node:fs';
|
||||
import * as os from 'node:os';
|
||||
import * as path from 'node:path';
|
||||
|
||||
import {
|
||||
launchTty,
|
||||
saveTranscript,
|
||||
seedClaudeTuiConfig,
|
||||
parseDriveCommand,
|
||||
type TtySession,
|
||||
} from '../test/helpers/tty-harness.ts';
|
||||
|
||||
const REPO_ROOT = path.resolve(import.meta.dir, '..');
|
||||
|
||||
/** Screen patterns that mean the paste-in install reached a passing verify —
|
||||
* ONE list shared by the claude-install and codex-install scenarios so the
|
||||
* two can't drift when the bootstrap's success copy changes. */
|
||||
const VERIFY_SUCCESS_PATTERNS: Array<RegExp | string> = [
|
||||
/bootstrap verify.*exit(?:ed|s)? 0/i,
|
||||
/verify\b.*\b(passed|0\b)/i,
|
||||
/All checks passed/i,
|
||||
];
|
||||
|
||||
// Same synthetic persona the door tests use — the interview can complete
|
||||
// unattended and nothing real about the operator ever enters a transcript.
|
||||
const PERSONA = {
|
||||
AGENT_NAME: 'Lighthouse',
|
||||
PRINCIPAL_NAME: 'Pat Example',
|
||||
AGENT_PURPOSE: 'Maintain the research corpus and draft the weekly memo without re-briefing.',
|
||||
AGENT_TOP_JOBS: 'corpus upkeep; weekly memo; meeting prep',
|
||||
PRINCIPAL_CONTEXT: 'Runs a small research group; builds internal tooling; values signal over noise.',
|
||||
VOICE_REGISTER: 'Direct: three options, the second one wins.',
|
||||
};
|
||||
|
||||
function log(msg: string): void {
|
||||
process.stderr.write(`[dx-explore] ${msg}\n`);
|
||||
}
|
||||
|
||||
function nowStamp(): string {
|
||||
return new Date().toISOString().replace(/[:.]/g, '-').replace('T', '-').slice(0, 19);
|
||||
}
|
||||
|
||||
// ── arg parsing ──────────────────────────────────────────────────────────────
|
||||
|
||||
interface CliArgs {
|
||||
scenario: string;
|
||||
dir?: string;
|
||||
gbrainBin?: string;
|
||||
rebuild: boolean;
|
||||
keep: boolean;
|
||||
/** Strip provider API keys from the child env — the TRUE keyless posture.
|
||||
* Without this, a Conductor session's ANTHROPIC_API_KEY leaks into the
|
||||
* hermetic run and the keyless first-touch path is never exercised. */
|
||||
keyless: boolean;
|
||||
hermeticHome: boolean;
|
||||
driveArgv: string[];
|
||||
}
|
||||
|
||||
/** Provider keys the hermetic base allows through; --keyless drops them. */
|
||||
const PROVIDER_KEY_NAMES = [
|
||||
'ANTHROPIC_API_KEY',
|
||||
'ANTHROPIC_AUTH_TOKEN',
|
||||
'OPENAI_API_KEY',
|
||||
'GSTACK_ANTHROPIC_API_KEY',
|
||||
'GSTACK_OPENAI_API_KEY',
|
||||
];
|
||||
|
||||
function parseArgs(argv: string[]): CliArgs {
|
||||
const out: CliArgs = {
|
||||
scenario: '',
|
||||
rebuild: false,
|
||||
keep: false,
|
||||
keyless: false,
|
||||
hermeticHome: true,
|
||||
driveArgv: [],
|
||||
};
|
||||
let i = 0;
|
||||
const sep = argv.indexOf('--');
|
||||
const own = sep >= 0 ? argv.slice(0, sep) : argv;
|
||||
out.driveArgv = sep >= 0 ? argv.slice(sep + 1) : [];
|
||||
while (i < own.length) {
|
||||
const a = own[i]!;
|
||||
if (a === '--dir') out.dir = own[++i];
|
||||
else if (a === '--gbrain') out.gbrainBin = own[++i];
|
||||
else if (a === '--rebuild') out.rebuild = true;
|
||||
else if (a === '--keep') out.keep = true;
|
||||
else if (a === '--keyless') out.keyless = true;
|
||||
else if (a === '--no-hermetic-home') out.hermeticHome = false;
|
||||
else if (!out.scenario && !a.startsWith('--')) out.scenario = a;
|
||||
else {
|
||||
log(`unknown argument: ${a}`);
|
||||
process.exit(2);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── compiled gbrain binary (what a real user runs) ───────────────────────────
|
||||
|
||||
/** Compile (or reuse) a standalone gbrain binary. `bun run src/cli.ts` adds a
|
||||
* multi-second transpile stall to EVERY invocation that a real install never
|
||||
* has — a compiled binary keeps the timing honest. Cached under
|
||||
* .context/dx-runs/bin/ keyed on nothing (use --rebuild after code changes). */
|
||||
function ensureGbrainBinary(explicit: string | undefined, rebuild: boolean): string {
|
||||
if (explicit) {
|
||||
fs.accessSync(explicit, fs.constants.X_OK);
|
||||
return path.resolve(explicit);
|
||||
}
|
||||
const binDir = path.join(REPO_ROOT, '.context', 'dx-runs', 'bin');
|
||||
const binPath = path.join(binDir, 'gbrain');
|
||||
if (!rebuild && fs.existsSync(binPath)) {
|
||||
log(`reusing compiled gbrain at ${binPath} (--rebuild to refresh)`);
|
||||
return binPath;
|
||||
}
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
log('compiling gbrain (bun build --compile)…');
|
||||
const res = spawnSync('bun', ['build', '--compile', '--outfile', binPath, 'src/cli.ts'], {
|
||||
cwd: REPO_ROOT,
|
||||
encoding: 'utf8',
|
||||
timeout: 300_000,
|
||||
maxBuffer: 64 * 1024 * 1024,
|
||||
});
|
||||
if (res.status !== 0 || !fs.existsSync(binPath)) {
|
||||
throw new Error(`bun build --compile failed (exit ${res.status}):\n${(res.stderr ?? '').slice(-2000)}`);
|
||||
}
|
||||
log(`compiled ${binPath}`);
|
||||
return binPath;
|
||||
}
|
||||
|
||||
// ── scenario plumbing ────────────────────────────────────────────────────────
|
||||
|
||||
interface ScenarioCtx {
|
||||
outDir: string;
|
||||
gbrainBin: string;
|
||||
keep: boolean;
|
||||
/** temp dirs to remove on completion unless --keep */
|
||||
cleanups: string[];
|
||||
/** Files carrying credential material (copied auth.json, seeded key
|
||||
* suffixes). ALWAYS deleted at cleanup — --keep keeps transcripts and
|
||||
* hermetic dirs for forensics, never credentials. */
|
||||
secretPaths: string[];
|
||||
events: Array<{ tMs: number; kind: 'input' | 'note' | 'screen'; data: string }>;
|
||||
t0: number;
|
||||
}
|
||||
|
||||
function newCtx(args: CliArgs, needsGbrain: boolean): ScenarioCtx {
|
||||
const outDir = path.resolve(
|
||||
args.dir ?? path.join(REPO_ROOT, '.context', 'dx-runs', `${args.scenario}-${nowStamp()}`),
|
||||
);
|
||||
fs.mkdirSync(outDir, { recursive: true });
|
||||
const ctx: ScenarioCtx = {
|
||||
outDir,
|
||||
gbrainBin: needsGbrain ? ensureGbrainBinary(args.gbrainBin, args.rebuild) : '',
|
||||
keep: args.keep,
|
||||
cleanups: [],
|
||||
secretPaths: [],
|
||||
events: [],
|
||||
t0: Date.now(),
|
||||
};
|
||||
installSignalScrub(ctx);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
function tmp(ctx: ScenarioCtx, prefix: string): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
ctx.cleanups.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
function event(ctx: ScenarioCtx, kind: 'input' | 'note' | 'screen', data: string): void {
|
||||
ctx.events.push({ tMs: Date.now() - ctx.t0, kind, data });
|
||||
}
|
||||
|
||||
/** Delete every credential copy. Idempotent; safe to call from a signal
|
||||
* handler AND from finishCtx (a second call is a no-op). This is the
|
||||
* "no credential outlives the run" guarantee — it must run even when a
|
||||
* 10-25min install is Ctrl-C'd (finally does NOT run on SIGINT default). */
|
||||
function scrubSecrets(ctx: ScenarioCtx): void {
|
||||
for (const p of ctx.secretPaths) {
|
||||
try {
|
||||
fs.rmSync(p, { force: true });
|
||||
} catch {
|
||||
/* best-effort */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Wire SIGINT/SIGTERM so an interrupted run still scrubs credentials before
|
||||
* the process dies. Registered once per scenario ctx. */
|
||||
function installSignalScrub(ctx: ScenarioCtx): void {
|
||||
const handler = (sig: NodeJS.Signals) => {
|
||||
scrubSecrets(ctx);
|
||||
process.stderr.write(`\n[dx-explore] ${sig}: scrubbed credential copies, exiting.\n`);
|
||||
process.exit(130);
|
||||
};
|
||||
process.once('SIGINT', handler);
|
||||
process.once('SIGTERM', handler);
|
||||
}
|
||||
|
||||
function finishCtx(ctx: ScenarioCtx): void {
|
||||
// Scrub credentials FIRST — before any other I/O that could throw (an
|
||||
// events.jsonl write failure must not strand auth files).
|
||||
scrubSecrets(ctx);
|
||||
fs.writeFileSync(
|
||||
path.join(ctx.outDir, 'events.jsonl'),
|
||||
ctx.events.map((e) => JSON.stringify(e)).join('\n') + (ctx.events.length ? '\n' : ''),
|
||||
);
|
||||
if (ctx.keep && ctx.secretPaths.length > 0) {
|
||||
log(`--keep: retained hermetic dirs, but scrubbed ${ctx.secretPaths.length} credential file(s)`);
|
||||
}
|
||||
if (!ctx.keep) {
|
||||
for (const d of ctx.cleanups) {
|
||||
try {
|
||||
fs.rmSync(d, { recursive: true, force: true });
|
||||
} catch {
|
||||
/* best-effort */
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fs.writeFileSync(
|
||||
path.join(ctx.outDir, 'hermetic-dirs.json'),
|
||||
JSON.stringify(ctx.cleanups, null, 2),
|
||||
);
|
||||
}
|
||||
// The one stdout line: where the transcript landed.
|
||||
console.log(ctx.outDir);
|
||||
}
|
||||
|
||||
/** Live session mirror so a watcher (or a Conductor agent) can follow along:
|
||||
* session/screen.txt (latest visible tail) + session/status.json. */
|
||||
function mirrorSession(dir: string, session: TtySession): () => void {
|
||||
const sessDir = path.join(dir, 'session');
|
||||
fs.mkdirSync(sessDir, { recursive: true });
|
||||
const timer = setInterval(() => {
|
||||
try {
|
||||
fs.writeFileSync(path.join(sessDir, 'screen.txt'), session.visible().slice(-8000));
|
||||
fs.writeFileSync(
|
||||
path.join(sessDir, 'status.json'),
|
||||
JSON.stringify(
|
||||
{
|
||||
running: !session.exited(),
|
||||
exitCode: session.exitCode(),
|
||||
elapsedMs: Date.now() - session.startedAtMs,
|
||||
frames: session.frames().length,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
/* best-effort */
|
||||
}
|
||||
}, 500);
|
||||
return () => clearInterval(timer);
|
||||
}
|
||||
|
||||
function saveSession(ctx: ScenarioCtx, name: string, session: TtySession, extraMeta: Record<string, unknown> = {}): void {
|
||||
const dir = name ? path.join(ctx.outDir, name) : ctx.outDir;
|
||||
saveTranscript(dir, {
|
||||
frames: session.frames(),
|
||||
raw: session.raw(),
|
||||
meta: {
|
||||
scenario: name || path.basename(ctx.outDir),
|
||||
argv: session.argv,
|
||||
startedAtIso: new Date(session.startedAtMs).toISOString(),
|
||||
exitCode: session.exitCode(),
|
||||
durationMs: Date.now() - session.startedAtMs,
|
||||
...extraMeta,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// ── scenario: help ───────────────────────────────────────────────────────────
|
||||
|
||||
async function scenarioHelp(ctx: ScenarioCtx, args: CliArgs): Promise<void> {
|
||||
const home = tmp(ctx, 'gb-dx-home-');
|
||||
const ws = tmp(ctx, 'gb-dx-ws-');
|
||||
const dropEnv = args.keyless ? PROVIDER_KEY_NAMES : undefined;
|
||||
const surfaces: Array<{ name: string; argv: string[] }> = [
|
||||
{ name: 'step-01-bare', argv: [ctx.gbrainBin] },
|
||||
{ name: 'step-02-help', argv: [ctx.gbrainBin, '--help'] },
|
||||
{ name: 'step-03-init-help', argv: [ctx.gbrainBin, 'init', '--help'] },
|
||||
{ name: 'step-04-bootstrap-help', argv: [ctx.gbrainBin, 'bootstrap', '--help'] },
|
||||
{ name: 'step-05-bootstrap-bare', argv: [ctx.gbrainBin, 'bootstrap'] },
|
||||
{ name: 'step-06-status-fresh', argv: [ctx.gbrainBin, 'status'] },
|
||||
];
|
||||
for (const s of surfaces) {
|
||||
log(`running ${s.name}: ${s.argv.join(' ')}`);
|
||||
const session = launchTty(s.argv, {
|
||||
cwd: ws,
|
||||
env: { HOME: home, GBRAIN_HOME: home },
|
||||
dropEnv,
|
||||
timeoutMs: 120_000,
|
||||
});
|
||||
await session.waitForExit(110_000);
|
||||
await session.close();
|
||||
saveSession(ctx, s.name, session);
|
||||
}
|
||||
}
|
||||
|
||||
// ── scenario: init (naive-user autopilot) ────────────────────────────────────
|
||||
|
||||
async function scenarioInit(ctx: ScenarioCtx, args: CliArgs): Promise<void> {
|
||||
const home = tmp(ctx, 'gb-dx-home-');
|
||||
const ws = tmp(ctx, 'gb-dx-ws-');
|
||||
log(
|
||||
`interactive \`gbrain init\` (${args.keyless ? 'TRUE keyless — provider keys stripped' : 'ambient keys allowed'}), ` +
|
||||
'naive-user autopilot: Enter accepts every default',
|
||||
);
|
||||
const session = launchTty([ctx.gbrainBin, 'init'], {
|
||||
cwd: ws,
|
||||
env: { HOME: home, GBRAIN_HOME: home },
|
||||
dropEnv: args.keyless ? PROVIDER_KEY_NAMES : undefined,
|
||||
timeoutMs: 600_000,
|
||||
});
|
||||
const stopMirror = mirrorSession(ctx.outDir, session);
|
||||
|
||||
const steps: string[] = [];
|
||||
let lastMarkPos = 0;
|
||||
const MAX_STEPS = 15;
|
||||
try {
|
||||
for (let step = 1; step <= MAX_STEPS && !session.exited(); step++) {
|
||||
const settled = await session.waitForQuiet({ quietMs: 2000, timeoutMs: 180_000 });
|
||||
const shot = session.visibleSince(lastMarkPos);
|
||||
lastMarkPos = session.mark();
|
||||
const tSec = ((Date.now() - session.startedAtMs) / 1000).toFixed(1);
|
||||
steps.push(
|
||||
`## Step ${step} (t+${tSec}s${settled ? '' : ', NEVER SETTLED within 180s'})\n\n` +
|
||||
'```\n' + shot.trim().slice(-3000) + '\n```\n',
|
||||
);
|
||||
event(ctx, 'screen', shot.slice(-2000));
|
||||
if (session.exited()) break;
|
||||
log(`step ${step}: screen settled at t+${tSec}s — pressing Enter (default)`);
|
||||
event(ctx, 'input', 'Enter');
|
||||
session.sendKey('Enter');
|
||||
await Bun.sleep(300);
|
||||
}
|
||||
await session.waitForExit(60_000);
|
||||
} finally {
|
||||
stopMirror();
|
||||
await session.close();
|
||||
}
|
||||
fs.writeFileSync(
|
||||
path.join(ctx.outDir, 'steps.md'),
|
||||
`# gbrain init — naive-user autopilot (Enter through every prompt)\n\n${steps.join('\n')}`,
|
||||
);
|
||||
saveSession(ctx, '', session, { autopilot: 'enter-through-defaults', keyless: args.keyless });
|
||||
}
|
||||
|
||||
// ── scenarios: claude-install / codex-install ────────────────────────────────
|
||||
|
||||
/**
|
||||
* Handle the harness's own first-run chrome dialogs (Claude Code: workspace
|
||||
* trust, bypass-permissions warning) so an unattended run reaches the input
|
||||
* prompt. Each handled dialog is recorded as a note — the dialogs ARE part of
|
||||
* the real first-run friction, just not gbrain's copy. Returns once the
|
||||
* screen has been quiet with no dialog visible, or at the deadline.
|
||||
*/
|
||||
async function settlePastBootDialogs(
|
||||
ctx: ScenarioCtx,
|
||||
session: TtySession,
|
||||
opts: { deadlineMs?: number } = {},
|
||||
): Promise<void> {
|
||||
const deadline = Date.now() + (opts.deadlineMs ?? 90_000);
|
||||
const handled = new Set<string>();
|
||||
while (Date.now() < deadline) {
|
||||
await session.waitForQuiet({ quietMs: 2000, timeoutMs: 30_000 });
|
||||
if (session.exited()) return;
|
||||
const tail = session.visible().slice(-2500);
|
||||
if (!handled.has('trust') && /trust this ?folder/i.test(tail.replace(/\s+/g, ' '))) {
|
||||
handled.add('trust');
|
||||
event(ctx, 'note', 'boot dialog: workspace trust — accepted (option 1)');
|
||||
session.send('1');
|
||||
await Bun.sleep(300);
|
||||
session.sendKey('Enter');
|
||||
continue;
|
||||
}
|
||||
if (!handled.has('bypass') && /Bypass ?Permissions ?mode/i.test(tail.replace(/\s+/g, ''))) {
|
||||
handled.add('bypass');
|
||||
event(ctx, 'note', 'boot dialog: bypass-permissions warning — accepted (option 2)');
|
||||
session.send('2');
|
||||
await Bun.sleep(300);
|
||||
session.sendKey('Enter');
|
||||
continue;
|
||||
}
|
||||
// Codex directory-trust dialog: "Do you trust the contents of this
|
||||
// directory? › 1. Yes, continue 2. No, quit".
|
||||
if (!handled.has('codex-trust') && /trust ?the ?contents ?of ?this ?directory/i.test(tail.replace(/\s+/g, ''))) {
|
||||
handled.add('codex-trust');
|
||||
event(ctx, 'note', 'boot dialog: codex directory trust — accepted (option 1)');
|
||||
session.send('1');
|
||||
await Bun.sleep(300);
|
||||
session.sendKey('Enter');
|
||||
continue;
|
||||
}
|
||||
return; // quiet + no dialog = at the input prompt
|
||||
}
|
||||
}
|
||||
|
||||
/** The README paste block, pointed at THIS repo's runbook, plus a persona
|
||||
* appendix so the interview completes unattended. The appendix is the ONLY
|
||||
* deviation from the shipped block — flagged in meta so the audit discounts it. */
|
||||
function installPrompt(): string {
|
||||
const runbook = path.join(REPO_ROOT, 'BOOTSTRAP_FOR_AGENTS.md');
|
||||
return (
|
||||
`Read and follow every step of: ${runbook}\n` +
|
||||
`Goal: set yourself up as my persistent personal agent in this folder, with gbrain ` +
|
||||
`as your memory. Interview me before writing any identity file — never invent ` +
|
||||
`answers. Ask before anything destructive. You are not done until ` +
|
||||
`\`gbrain bootstrap verify\` exits 0.\n\n` +
|
||||
`[Unattended-run appendix — I am stepping away; use these interview answers instead ` +
|
||||
`of asking me, and do not wait for my input: ` +
|
||||
`agent name: ${PERSONA.AGENT_NAME}; my name: ${PERSONA.PRINCIPAL_NAME}; ` +
|
||||
`purpose: ${PERSONA.AGENT_PURPOSE}; top jobs: ${PERSONA.AGENT_TOP_JOBS}; ` +
|
||||
`about me: ${PERSONA.PRINCIPAL_CONTEXT}; voice: ${PERSONA.VOICE_REGISTER}. ` +
|
||||
`gbrain is already installed and on PATH. If a step needs GitHub auth or an API key ` +
|
||||
`that is unavailable, take the documented keyless/local fallback and continue.]`
|
||||
);
|
||||
}
|
||||
|
||||
async function scenarioClaudeInstall(ctx: ScenarioCtx): Promise<void> {
|
||||
const home = tmp(ctx, 'gb-dx-home-');
|
||||
const cfg = tmp(ctx, 'gb-dx-ccfg-');
|
||||
const gbHome = tmp(ctx, 'gb-dx-gbhome-');
|
||||
const ws = tmp(ctx, 'gb-dx-ws-');
|
||||
const binDir = tmp(ctx, 'gb-dx-bin-');
|
||||
fs.copyFileSync(ctx.gbrainBin, path.join(binDir, 'gbrain'));
|
||||
fs.chmodSync(path.join(binDir, 'gbrain'), 0o755);
|
||||
|
||||
seedClaudeTuiConfig(cfg, {
|
||||
apiKey: process.env.ANTHROPIC_API_KEY ?? process.env.GSTACK_ANTHROPIC_API_KEY,
|
||||
// realpath: macOS tmpdirs live under /var → /private/var; claude compares
|
||||
// against the resolved path, so an unresolved seed misses.
|
||||
trustedDirs: [ws, fs.realpathSync(ws)],
|
||||
});
|
||||
// The seed records the key's last 20 chars — credential-adjacent, so it is
|
||||
// scrubbed at cleanup even with --keep.
|
||||
ctx.secretPaths.push(path.join(cfg, '.claude.json'));
|
||||
|
||||
log('REAL interactive claude running the paste-in bootstrap (10-25 min, real API cost)');
|
||||
log(`watch live: cat ${path.join(ctx.outDir, 'session', 'screen.txt')}`);
|
||||
const session = launchTty(
|
||||
// --dangerously-skip-permissions: v1 measures flow + copy + stalls without
|
||||
// permission-dialog babysitting. Permission-prompt COUNT is a separate
|
||||
// drive-mode pass (the dialogs are Claude Code's chrome, not gbrain copy).
|
||||
['claude', '--dangerously-skip-permissions'],
|
||||
{
|
||||
cwd: ws,
|
||||
env: {
|
||||
HOME: home,
|
||||
CLAUDE_CONFIG_DIR: cfg,
|
||||
GBRAIN_HOME: gbHome,
|
||||
PATH: `${binDir}:${process.env.PATH ?? ''}`,
|
||||
},
|
||||
timeoutMs: 1_800_000,
|
||||
},
|
||||
);
|
||||
const stopMirror = mirrorSession(ctx.outDir, session);
|
||||
try {
|
||||
// Get past first-run chrome (trust dialog, bypass warning), then paste.
|
||||
await settlePastBootDialogs(ctx, session);
|
||||
event(ctx, 'input', 'paste install prompt');
|
||||
session.send(installPrompt());
|
||||
await Bun.sleep(1500);
|
||||
session.sendKey('Enter');
|
||||
// Run until verify-success copy or exit or wall clock.
|
||||
const done = await Promise.race([
|
||||
session
|
||||
.waitForAny(VERIFY_SUCCESS_PATTERNS, {
|
||||
timeoutMs: 1_500_000,
|
||||
})
|
||||
.then(() => 'verify-signal')
|
||||
.catch(() => 'no-signal'),
|
||||
session.waitForExit(1_500_000).then(() => 'exited'),
|
||||
]);
|
||||
event(ctx, 'note', `terminal condition: ${done}`);
|
||||
// Let trailing output land.
|
||||
await session.waitForQuiet({ quietMs: 5000, timeoutMs: 60_000 });
|
||||
} finally {
|
||||
stopMirror();
|
||||
await session.close();
|
||||
}
|
||||
saveSession(ctx, '', session, {
|
||||
promptDeviation: 'unattended persona appendix + local runbook path + preinstalled binary',
|
||||
runbook: 'BOOTSTRAP_FOR_AGENTS.md (local)',
|
||||
});
|
||||
}
|
||||
|
||||
async function scenarioCodexInstall(ctx: ScenarioCtx): Promise<void> {
|
||||
const home = tmp(ctx, 'gb-dx-home-');
|
||||
const gbHome = tmp(ctx, 'gb-dx-gbhome-');
|
||||
const ws = tmp(ctx, 'gb-dx-ws-');
|
||||
const binDir = tmp(ctx, 'gb-dx-bin-');
|
||||
fs.copyFileSync(ctx.gbrainBin, path.join(binDir, 'gbrain'));
|
||||
fs.chmodSync(path.join(binDir, 'gbrain'), 0o755);
|
||||
|
||||
// Hermetic ~/.codex with ONLY the operator's auth (same posture as the
|
||||
// codex door test). codex refuses untrusted cwds — a git repo satisfies it.
|
||||
const codexHome = path.join(home, '.codex');
|
||||
fs.mkdirSync(codexHome, { recursive: true });
|
||||
const realAuth = path.join(os.homedir(), '.codex', 'auth.json');
|
||||
if (fs.existsSync(realAuth)) {
|
||||
const authCopy = path.join(codexHome, 'auth.json');
|
||||
fs.copyFileSync(realAuth, authCopy);
|
||||
fs.chmodSync(authCopy, 0o600); // copyFileSync doesn't preserve source mode
|
||||
ctx.secretPaths.push(authCopy); // scrubbed at cleanup, even with --keep
|
||||
}
|
||||
spawnSync('git', ['init', '-q', ws]);
|
||||
spawnSync('git', ['-C', ws, 'config', 'user.email', 'dx@example.com']);
|
||||
spawnSync('git', ['-C', ws, 'config', 'user.name', 'DX Explore']);
|
||||
|
||||
log('REAL interactive codex running the paste-in bootstrap (10-25 min, real API cost)');
|
||||
log(`watch live: cat ${path.join(ctx.outDir, 'session', 'screen.txt')}`);
|
||||
const session = launchTty(
|
||||
['codex', '--sandbox', 'workspace-write', '--ask-for-approval', 'never'],
|
||||
{
|
||||
cwd: ws,
|
||||
env: {
|
||||
HOME: home,
|
||||
CODEX_HOME: codexHome,
|
||||
GBRAIN_HOME: gbHome,
|
||||
PATH: `${binDir}:${process.env.PATH ?? ''}`,
|
||||
},
|
||||
extraAllow: ['OPENAI_API_KEY', 'CODEX_*'],
|
||||
timeoutMs: 1_800_000,
|
||||
},
|
||||
);
|
||||
const stopMirror = mirrorSession(ctx.outDir, session);
|
||||
try {
|
||||
await settlePastBootDialogs(ctx, session);
|
||||
event(ctx, 'input', 'paste install prompt');
|
||||
session.send(installPrompt());
|
||||
await Bun.sleep(1500);
|
||||
session.sendKey('Enter');
|
||||
const done = await Promise.race([
|
||||
session
|
||||
.waitForAny(VERIFY_SUCCESS_PATTERNS, {
|
||||
timeoutMs: 1_500_000,
|
||||
})
|
||||
.then(() => 'verify-signal')
|
||||
.catch(() => 'no-signal'),
|
||||
session.waitForExit(1_500_000).then(() => 'exited'),
|
||||
]);
|
||||
event(ctx, 'note', `terminal condition: ${done}`);
|
||||
await session.waitForQuiet({ quietMs: 5000, timeoutMs: 60_000 });
|
||||
} finally {
|
||||
stopMirror();
|
||||
await session.close();
|
||||
}
|
||||
saveSession(ctx, '', session, {
|
||||
promptDeviation: 'unattended persona appendix + local runbook path + preinstalled binary',
|
||||
runbook: 'BOOTSTRAP_FOR_AGENTS.md (local)',
|
||||
});
|
||||
}
|
||||
|
||||
// ── scenario: drive (manual control channel) ─────────────────────────────────
|
||||
|
||||
async function scenarioDrive(ctx: ScenarioCtx, args: CliArgs): Promise<void> {
|
||||
if (args.driveArgv.length === 0) {
|
||||
log('drive mode needs a command: dx-explore.ts drive -- gbrain init');
|
||||
process.exit(2);
|
||||
}
|
||||
// `gbrain` as argv[0] resolves to the compiled binary.
|
||||
const argv = [...args.driveArgv];
|
||||
if (argv[0] === 'gbrain') argv[0] = ctx.gbrainBin;
|
||||
|
||||
const sessDir = path.join(ctx.outDir, 'session');
|
||||
fs.mkdirSync(sessDir, { recursive: true });
|
||||
const inputPath = path.join(sessDir, 'input.jsonl');
|
||||
fs.writeFileSync(inputPath, '');
|
||||
|
||||
const env: Record<string, string | undefined> = {};
|
||||
if (args.hermeticHome) {
|
||||
const home = tmp(ctx, 'gb-dx-home-');
|
||||
env.HOME = home;
|
||||
env.GBRAIN_HOME = home;
|
||||
}
|
||||
|
||||
log(`driving: ${argv.join(' ')}`);
|
||||
log(`watch: cat ${path.join(sessDir, 'screen.txt')}`);
|
||||
log(`input: echo '{"line":"some text"}' >> ${inputPath} (sends text + Enter)`);
|
||||
log(` echo '{"key":"Down"}' >> ${inputPath}`);
|
||||
log(`stop: echo '{"stop":true}' >> ${inputPath}`);
|
||||
|
||||
const session = launchTty(argv, {
|
||||
cwd: process.cwd(),
|
||||
env,
|
||||
timeoutMs: 3_600_000,
|
||||
});
|
||||
const stopMirror = mirrorSession(ctx.outDir, session);
|
||||
|
||||
let offset = 0;
|
||||
let stopping = false;
|
||||
try {
|
||||
while (!session.exited() && !stopping) {
|
||||
await Bun.sleep(200);
|
||||
let content = '';
|
||||
try {
|
||||
content = fs.readFileSync(inputPath, 'utf8');
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (content.length <= offset) continue;
|
||||
const fresh = content.slice(offset);
|
||||
offset = content.length;
|
||||
for (const line of fresh.split('\n')) {
|
||||
if (!line.trim()) continue;
|
||||
const cmd = parseDriveCommand(line);
|
||||
if (!cmd) {
|
||||
log(`skipping malformed drive command: ${line.slice(0, 120)}`);
|
||||
continue;
|
||||
}
|
||||
if (cmd.kind === 'send') {
|
||||
event(ctx, 'input', cmd.data);
|
||||
session.send(cmd.data);
|
||||
} else if (cmd.kind === 'key') {
|
||||
event(ctx, 'input', `<${cmd.key}>`);
|
||||
session.sendKey(cmd.key);
|
||||
} else if (cmd.kind === 'note') {
|
||||
event(ctx, 'note', cmd.text);
|
||||
} else if (cmd.kind === 'stop') {
|
||||
stopping = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
stopMirror();
|
||||
await session.close();
|
||||
}
|
||||
saveSession(ctx, '', session, { mode: 'drive', command: argv.join(' ') });
|
||||
}
|
||||
|
||||
// ── main ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
const SCENARIOS: Record<string, { needsGbrain: boolean; run: (ctx: ScenarioCtx, args: CliArgs) => Promise<void> }> = {
|
||||
help: { needsGbrain: true, run: scenarioHelp },
|
||||
init: { needsGbrain: true, run: scenarioInit },
|
||||
'claude-install': { needsGbrain: true, run: scenarioClaudeInstall },
|
||||
'codex-install': { needsGbrain: true, run: scenarioCodexInstall },
|
||||
drive: { needsGbrain: true, run: scenarioDrive },
|
||||
};
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
const scenario = SCENARIOS[args.scenario];
|
||||
if (!scenario) {
|
||||
log(`usage: bun run scripts/dx-explore.ts <${Object.keys(SCENARIOS).join('|')}> [options] [-- cmd...]`);
|
||||
process.exit(2);
|
||||
}
|
||||
const ctx = newCtx(args, scenario.needsGbrain);
|
||||
log(`transcripts → ${ctx.outDir}`);
|
||||
try {
|
||||
await scenario.run(ctx, args);
|
||||
} finally {
|
||||
finishCtx(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
await main();
|
||||
@@ -34,6 +34,12 @@ export const E2E_TEST_MAP: Record<string, string[]> = {
|
||||
"test/e2e/openclaw-context-engine-plugin.test.ts",
|
||||
"test/e2e/openclaw-plugin-load-real.test.ts",
|
||||
],
|
||||
// claw-test harness (command + core: runners, scenarios, seeding, friction
|
||||
// merge) feeds the scripted + shim-live E2E. The hermes door
|
||||
// (install-real-hermes.serial.test.ts) is deliberately NOT mapped — it is
|
||||
// opt-in-gated (GBRAIN_REAL_HERMES_E2E) and self-skips in run-all anyway.
|
||||
"src/commands/claw-test.ts": ["test/e2e/claw-test.test.ts"],
|
||||
"src/core/claw-test/**": ["test/e2e/claw-test.test.ts"],
|
||||
// dream.ts is a thin alias over runCycle in cycle.ts.
|
||||
"src/core/cycle.ts": ["test/e2e/cycle.test.ts", "test/e2e/dream.test.ts"],
|
||||
// Multi-source sync writes share the per-source bookmark anchor.
|
||||
|
||||
+13
-10
@@ -68,16 +68,19 @@ mkdir -p "$E2E_TMP_HOME/.gbrain"
|
||||
|
||||
# --- Hermetic env scrub: operator/agent context must not bleed into E2E ---
|
||||
# A dev shell or a Conductor workspace exports CONDUCTOR_*, MCP_*, OPENCLAW_*,
|
||||
# and GBRAIN_* config overrides (e.g. a stray GBRAIN_BRAIN_ID, GBRAIN_SOURCE,
|
||||
# GBRAIN_*_THRESHOLD, GBRAIN_SUPERVISOR_PID_FILE) that would silently change
|
||||
# test behavior — making "hermetic" E2E non-hermetic and its failures
|
||||
# unreproducible across machines. Drop them before bun starts. This is a
|
||||
# DENYLIST of operator-context prefixes (not an allowlist rebuild), so PATH,
|
||||
# HOME, TMPDIR, CI, DATABASE_URL, and bun internals survive untouched. We keep
|
||||
# GBRAIN_HOME (just set above for HOME isolation); everything else GBRAIN_* is
|
||||
# an operator override the suite must not inherit. Adapts GStack's
|
||||
# buildHermeticEnv() allowlist to gbrain's shell E2E runner.
|
||||
for _e2e_var in $(env | grep -oE '^(CONDUCTOR_|MCP_|OPENCLAW_|GBRAIN_)[A-Za-z0-9_]*' | sort -u); do
|
||||
# HERMES_*, and GBRAIN_* config overrides (e.g. a stray GBRAIN_BRAIN_ID,
|
||||
# GBRAIN_SOURCE, GBRAIN_*_THRESHOLD, GBRAIN_SUPERVISOR_PID_FILE, an operator's
|
||||
# HERMES_BIN/HERMES_HOME) that would silently change test behavior — making
|
||||
# "hermetic" E2E non-hermetic and its failures unreproducible across machines.
|
||||
# Drop them before bun starts. This is a DENYLIST of operator-context prefixes
|
||||
# (not an allowlist rebuild), so PATH, HOME, TMPDIR, CI, DATABASE_URL, and bun
|
||||
# internals survive untouched. We keep GBRAIN_HOME (just set above for HOME
|
||||
# isolation); everything else GBRAIN_* is an operator override the suite must
|
||||
# not inherit — which also scrubs GBRAIN_REAL_HERMES_E2E, so the paid hermes
|
||||
# door suite structurally cannot fire under this runner (its venue is
|
||||
# heavy-tests.yml's direct bun test). Adapts GStack's buildHermeticEnv()
|
||||
# allowlist to gbrain's shell E2E runner.
|
||||
for _e2e_var in $(env | grep -oE '^(CONDUCTOR_|MCP_|OPENCLAW_|HERMES_|GBRAIN_)[A-Za-z0-9_]*' | sort -u); do
|
||||
case "$_e2e_var" in
|
||||
GBRAIN_HOME) ;; # required for HOME isolation (set above) — keep
|
||||
*) unset "$_e2e_var" || true ;;
|
||||
|
||||
@@ -55,6 +55,7 @@ gbrain friction list # recent runs with counts
|
||||
gbrain friction render --run-id <id> # markdown report (default)
|
||||
gbrain friction render --run-id <id> --json
|
||||
gbrain friction summary --run-id <id> # friction + delight side-by-side
|
||||
gbrain friction diff --base <run-or-agent> --compare <run-or-agent> # cross-run/cross-agent comparison
|
||||
```
|
||||
|
||||
`render` defaults to `--redact` for markdown (strips `$HOME`/`$CWD` to `<HOME>`/`<CWD>` placeholders) so reports paste safely into PRs and issues.
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
"schema-author": "schema-pack authoring is a brain-owner activity, not a client activity",
|
||||
"smoke-test": "host-runtime health checks (container/daemon assumptions)",
|
||||
"gbrain-upgrade": "host binary upgrade flow",
|
||||
"cold-start": "host onboarding flow",
|
||||
"schema-unify": "schema unification is a brain-owner migration activity",
|
||||
"skill-optimizer": "requires host-side skillopt engine access and benchmark files"
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"_AGENT_README.md": "62613f7f1e061576b6c1b18844f59bd35f2df96ca5c45c8c41fae0772b9ce4d3",
|
||||
"_brain-filing-rules.json": "cf850df6a7425464c6d63b3ace71991cc93497fa0cc8cd21acd31883e17939c6",
|
||||
"_brain-filing-rules.md": "2d2d75b7c76081c56f41b2c0a5a978c355ce957300f9b0a5575dc4079ef1f877",
|
||||
"_friction-protocol.md": "1b6e7cfa58725a6a5dc2dc787242141bc33f5fde524540d85b14ec22266140f7",
|
||||
"_friction-protocol.md": "51353207240142024ff1facc25f225712275ecdb4a034ffffdd83740c8d328e3",
|
||||
"_output-rules.md": "0722ec2ecea7f9fa2f065cf12dfe1347956a9709d29898bf9fe95e875c64b800",
|
||||
"academic-verify/SKILL.md": "1c19e27e75249d869da428ce8d060075feef8fbbfe146af58b305d11a260ebbc",
|
||||
"academic-verify/routing-eval.jsonl": "90d894a9829d9936e6ac7a6507e4de67ad26e46a1fe13b7a34e7dec1c0d887dd",
|
||||
@@ -137,7 +137,7 @@
|
||||
"minion-orchestrator/routing-eval.jsonl": "501ed2e19cb16847ff8425219d246b7a774de1accd42cb28fd44edbb64204992",
|
||||
"perplexity-research/SKILL.md": "c25f5c471cbe3c6e0f975d8397e8382b00a85f8aa75302231d53c52855369e97",
|
||||
"perplexity-research/routing-eval.jsonl": "f1a40d87e710d5d2acd602a372d83f46c95da022b6e635228fffeaacb3bb2b27",
|
||||
"plugin-exclusions.json": "585486aaaf9a87ec4b13bea5d03f5e9af34a9ac64283c878c234ba094126f793",
|
||||
"plugin-exclusions.json": "e8070da59bb4444304eb354c7421f0998383454d96d5e66e38484aae792c4775",
|
||||
"publish/SKILL.md": "e06b609db780a3cc93a1755a87b30ff08ffdc0fdbc834c1422b2ad2489b57497",
|
||||
"query/SKILL.md": "b12aae4e86b893038b1d9e97a977bd6a7939db9f5c57dde12c11d8d7451e0762",
|
||||
"query/routing-eval.jsonl": "74f5a91e52fabc54e0e9403fa17db87ee26bb7ebb8ae8005148c51142abc62fe",
|
||||
|
||||
+37
-9
@@ -14,6 +14,7 @@ import { spawn } from 'child_process';
|
||||
import {
|
||||
readUpdateCache,
|
||||
isCacheFresh,
|
||||
pendingUpgradeVersion,
|
||||
readSnooze,
|
||||
isSnoozeActive,
|
||||
resolveSelfUpgradeMode,
|
||||
@@ -259,12 +260,26 @@ function maybeEmitUpdateMarker(command: string): void {
|
||||
const now = Date.now();
|
||||
const entry = readUpdateCache();
|
||||
if (entry && isCacheFresh(entry, now)) {
|
||||
if (entry.marker.kind === 'upgrade_available' && entry.marker.latest) {
|
||||
// Shared stale/foreign-cache guard (pendingUpgradeVersion): only nag when
|
||||
// the cached latest is strictly newer than the RUNNING binary, and print
|
||||
// the running version — the cache records whatever binary WROTE it.
|
||||
const latest = pendingUpgradeVersion(VERSION, now);
|
||||
if (latest) {
|
||||
// notify mode honors a per-version snooze; auto mode ignores it.
|
||||
if (mode === 'notify' && isSnoozeActive(readSnooze(), entry.marker.latest, now)) return;
|
||||
process.stderr.write(`UPGRADE_AVAILABLE ${entry.marker.current} ${entry.marker.latest}\n`);
|
||||
if (mode === 'notify' && isSnoozeActive(readSnooze(), latest, now)) return;
|
||||
// The raw `UPGRADE_AVAILABLE <cur> <latest>` line is a MACHINE marker
|
||||
// (parsed by the self-upgrade skill / MCP via parseMarker). A human at
|
||||
// an interactive terminal should never see the token as the literal
|
||||
// first line of output — so emit it only when stderr is NOT a TTY
|
||||
// (agent harnesses capture stderr non-interactively and still get it).
|
||||
// GBRAIN_FORCE_UPGRADE_MARKER=1 forces it for the rarer agent harness
|
||||
// that allocates a PTY yet still parses the token. The human sentence
|
||||
// prints on both.
|
||||
if (!process.stderr.isTTY || process.env.GBRAIN_FORCE_UPGRADE_MARKER === '1') {
|
||||
process.stderr.write(`UPGRADE_AVAILABLE ${VERSION} ${latest}\n`);
|
||||
}
|
||||
process.stderr.write(
|
||||
`gbrain ${entry.marker.current} -> ${entry.marker.latest} available. Run: gbrain self-upgrade\n`,
|
||||
`gbrain ${VERSION} -> ${latest} available. Run: gbrain self-upgrade\n`,
|
||||
);
|
||||
}
|
||||
return;
|
||||
@@ -273,19 +288,32 @@ function maybeEmitUpdateMarker(command: string): void {
|
||||
// Stale/missing cache → kick a detached, single-flighted refresh. The child
|
||||
// (`check-update --refresh-cache`) single-flights via the refresh lock and
|
||||
// writes the cache for the NEXT invocation. We never wait on it.
|
||||
// Spawn OURSELVES (hook.ts spawnDetachedPush pattern), not `gbrain` from
|
||||
// PATH — a different (older) binary on PATH would write ITS version into
|
||||
// the cache and make the marker lie about what is installed here.
|
||||
try {
|
||||
const child = spawn('gbrain', ['check-update', '--refresh-cache'], {
|
||||
const exec = process.execPath ?? '';
|
||||
const refreshArgs = ['check-update', '--refresh-cache'];
|
||||
// Detect compiled-vs-dev by the RUNTIME's basename, not our own — a
|
||||
// published binary keeps its official name (`gbrain-darwin-arm64`, a
|
||||
// `gb` shim), so matching `/gbrain$/` on execPath would misfire and
|
||||
// prepend the `/$bunfs/root/...` virtual entrypoint (process.argv[1] in
|
||||
// a compiled Bun binary), producing an unknown-command child that never
|
||||
// refreshes. Dev mode runs under `bun`/`node`; anything else IS the
|
||||
// compiled binary and re-execs itself directly.
|
||||
const isDevRuntime = /[/\\](bun|node)(\.exe)?$/.test(exec);
|
||||
const argv = isDevRuntime ? [process.argv[1], ...refreshArgs] : refreshArgs;
|
||||
const child = spawn(exec, argv, {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
env: { ...process.env, GBRAIN_SKIP_STARTUP_HOOKS: '1' },
|
||||
});
|
||||
// ChildProcess is an EventEmitter — an unhandled 'error' (e.g. ENOENT when
|
||||
// gbrain isn't on PATH) would throw uncaught. Swallow it; the refresh is
|
||||
// best-effort.
|
||||
// ChildProcess is an EventEmitter — an unhandled 'error' would throw
|
||||
// uncaught. Swallow it; the refresh is best-effort.
|
||||
child.on('error', () => {});
|
||||
child.unref();
|
||||
} catch {
|
||||
/* gbrain not on PATH / spawn failed — fail-open, no refresh this run */
|
||||
/* spawn failed — fail-open, no refresh this run */
|
||||
}
|
||||
} catch {
|
||||
/* the update marker must never break a command */
|
||||
|
||||
+83
-17
@@ -429,7 +429,7 @@ async function runStatus(ws: string, rest: string[], home: string): Promise<numb
|
||||
if (report.next) {
|
||||
console.log(`\nNext: ${report.next}`);
|
||||
} else {
|
||||
console.log('\nAll phases done. Weekly self-check: `gbrain bootstrap verify`.');
|
||||
console.log('\nAll phases done. Weekly self-check: `gbrain bootstrap verify` (close agent sessions first — PGLite is single-writer).');
|
||||
}
|
||||
if (report.runbookSkew) {
|
||||
console.log(
|
||||
@@ -456,6 +456,15 @@ async function runStatus(ws: string, rest: string[], home: string): Promise<numb
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** One copy of the A8 invalidation warning — shared by --set and --skip so
|
||||
* the operator-facing instructions cannot drift between the two branches. */
|
||||
function warnInvalidatedConfirmation(): void {
|
||||
console.error(
|
||||
'note: this change voided the prior confirmation — read the full answer set back ' +
|
||||
'to the human, then `gbrain bootstrap interview --confirm <hash>` again before render.',
|
||||
);
|
||||
}
|
||||
|
||||
async function runInterview(ws: string, rest: string[]): Promise<number> {
|
||||
if (rest.includes('--init')) {
|
||||
const r = initState(ws);
|
||||
@@ -497,6 +506,7 @@ async function runInterview(ws: string, rest: string[]): Promise<number> {
|
||||
console.log(`${key}: routed to the 0600 config file (${routed.configKey}). Not recorded in interview state.`);
|
||||
return 0;
|
||||
}
|
||||
if (r.invalidatedConfirmation) warnInvalidatedConfirmation();
|
||||
console.log(`${key} recorded.`);
|
||||
return 0;
|
||||
}
|
||||
@@ -512,6 +522,7 @@ async function runInterview(ws: string, rest: string[]): Promise<number> {
|
||||
console.error(r.message);
|
||||
return 1;
|
||||
}
|
||||
if (r.invalidatedConfirmation) warnInvalidatedConfirmation();
|
||||
console.log(`${key} skipped.`);
|
||||
return 0;
|
||||
}
|
||||
@@ -839,6 +850,11 @@ async function runHooks(ws: string, rest: string[], home: string, runner: ExecRu
|
||||
|
||||
// 1. MCP registration — argv built by the host-format module, executed
|
||||
// through the runner seam, recorded on the receipt.
|
||||
// A missing host binary (exit 127) skips MCP registration but NOT the
|
||||
// hooks below — hooks only write .claude/settings.local.json and need no
|
||||
// binary. The old early-return silently dropped hooks while the copy said
|
||||
// only "MCP registration skipped".
|
||||
let mcpSkipped = false;
|
||||
const argvs =
|
||||
harness === 'claude-code'
|
||||
? registerClaudeMcp({ gbrainBin, scope: mcpScope, sourceId, ...(gbrainHome ? { gbrainHome } : {}) })
|
||||
@@ -848,10 +864,12 @@ async function runHooks(ws: string, rest: string[], home: string, runner: ExecRu
|
||||
const res = await runner(argv);
|
||||
if (res.code === 127) {
|
||||
console.error(
|
||||
`\`${argv[0]}\` is not on PATH — is ${harness} installed? MCP registration skipped; ` +
|
||||
`re-run \`gbrain bootstrap hooks --harness ${harness}\` once it is.`,
|
||||
`\`${argv[0]}\` is not on PATH — is ${harness} installed? MCP registration skipped ` +
|
||||
`(per-turn hooks still install below); re-run ` +
|
||||
`\`gbrain bootstrap hooks --harness ${harness}\` once it is.`,
|
||||
);
|
||||
return 2;
|
||||
mcpSkipped = true;
|
||||
break;
|
||||
}
|
||||
if (res.code !== 0) {
|
||||
const already = /already exists|already registered/i.test(res.stderr + res.stdout);
|
||||
@@ -869,12 +887,45 @@ async function runHooks(ws: string, rest: string[], home: string, runner: ExecRu
|
||||
console.error(
|
||||
`existing '${mcpName}' MCP registration targets a DIFFERENT workspace/binary — replacing it.`,
|
||||
);
|
||||
await runner([argv[0], 'mcp', 'remove', mcpName]);
|
||||
// The add above failed "already exists" in the CURRENT scope, so the
|
||||
// blocker lives there — target the remove at that scope on Claude
|
||||
// Code (a scope-less remove can resolve to a different scope's
|
||||
// registration and leave the blocker in place). Codex has no scope
|
||||
// flag. Fail loud if the remove doesn't land: the silent no-op loop
|
||||
// used to re-fail the add and report nothing actionable.
|
||||
const rmArgv =
|
||||
harness === 'claude-code'
|
||||
? [argv[0], 'mcp', 'remove', mcpName, '--scope', mcpScope]
|
||||
: [argv[0], 'mcp', 'remove', mcpName];
|
||||
const rm = await runner(rmArgv);
|
||||
if (rm.code !== 0) {
|
||||
console.error(
|
||||
`\`${rmArgv.join(' ')}\` failed (${rm.stderr.trim() || `exit ${rm.code}`}) — remove the stale ` +
|
||||
`registration by hand (\`${argv[0]} mcp get ${mcpName}\` shows where it lives), then re-run ` +
|
||||
`\`gbrain bootstrap hooks --harness ${harness} --repair\`.`,
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
const re = await runner(argv);
|
||||
if (re.code !== 0 && !/already exists|already registered/i.test(re.stderr + re.stdout)) {
|
||||
console.error(`MCP re-registration failed (${argv.join(' ')}): ${re.stderr.trim() || `exit ${re.code}`}`);
|
||||
return 1;
|
||||
}
|
||||
// Re-add can itself return "already exists" if a racing writer
|
||||
// re-claimed the name between our remove and add — that registration
|
||||
// is NOT ours. Re-verify and abort rather than bless a foreign
|
||||
// endpoint that would intercept memory ops. (Only the recorded
|
||||
// warn-then-continue step-2 smoke did this before; here it's fatal.)
|
||||
const post = await verifyMcpTargetsWorkspace(runner, harness, mcpName, gbrainBin, sourceId);
|
||||
if (post === 'mismatch') {
|
||||
console.error(
|
||||
`after replacing '${mcpName}', it STILL targets a different workspace/binary — ` +
|
||||
`refusing to continue (a racing registration may have re-claimed the name). ` +
|
||||
`Inspect \`${argv[0]} mcp get ${mcpName}\`, remove it by hand, then re-run ` +
|
||||
`\`gbrain bootstrap hooks --harness ${harness} --repair\`.`,
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
console.log(
|
||||
`MCP server '${mcpName}' already registered — could not confirm it targets this workspace ` +
|
||||
@@ -887,7 +938,7 @@ async function runHooks(ws: string, rest: string[], home: string, runner: ExecRu
|
||||
// 2. Registration smoke [FIX7]: confirm the EXPECTED server (binary path +
|
||||
// GBRAIN_SOURCE), not merely a 'gbrain' substring in `mcp list`. Falls back
|
||||
// to the list probe only when the host has no `mcp get`.
|
||||
try {
|
||||
if (!mcpSkipped) try {
|
||||
const listBin = harness === 'claude-code' ? 'claude' : 'codex';
|
||||
const scopeLabel = harness === 'claude-code' ? mcpScope : 'user-global';
|
||||
const verdict = await verifyMcpTargetsWorkspace(runner, harness, 'gbrain', gbrainBin, sourceId);
|
||||
@@ -926,9 +977,21 @@ async function runHooks(ws: string, rest: string[], home: string, runner: ExecRu
|
||||
// both files.
|
||||
const hookEnv = { GBRAIN_SOURCE: sourceId, ...(gbrainHome ? { GBRAIN_HOME: gbrainHome } : {}) };
|
||||
const cloudCarrier = detectExecutionEnvironment() === 'cloud-sandbox';
|
||||
const r = cloudCarrier
|
||||
? writeCommittedClaudeHooks(ws, { env: hookEnv })
|
||||
: writeClaudeHooks(ws, { gbrainBin, env: hookEnv });
|
||||
let r: ReturnType<typeof writeClaudeHooks> | ReturnType<typeof writeCommittedClaudeHooks>;
|
||||
try {
|
||||
r = cloudCarrier
|
||||
? writeCommittedClaudeHooks(ws, { env: hookEnv })
|
||||
: writeClaudeHooks(ws, { gbrainBin, env: hookEnv });
|
||||
} catch (e) {
|
||||
// Fail-closed on an unparseable settings file (either carrier): MCP
|
||||
// (step 1) still landed; record that, surface the fix, and exit
|
||||
// nonzero so the paste-in flow knows hooks are NOT installed.
|
||||
console.error((e as Error).message);
|
||||
if (!mcpSkipped) {
|
||||
appendReceiptRegistration(home, ws, { host: harness, scope: mcpScope, detail: 'mcp' });
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
hooksWritten = true;
|
||||
console.log(
|
||||
`hooks installed (${r.installed.length} event(s)) in ${r.settingsPath}${repair ? ' [repair]' : ''} — your brain now loads every turn. Turn off any time with GBRAIN_HOOKS=0, or re-run with --no-hooks.`,
|
||||
@@ -951,15 +1014,18 @@ async function runHooks(ws: string, rest: string[], home: string, runner: ExecRu
|
||||
console.log('Codex has no hook system — per-turn context is the AGENTS.md pull protocol (stated plainly, not a bug).');
|
||||
}
|
||||
|
||||
// 4. Receipt registration record [CX2-12].
|
||||
appendReceiptRegistration(home, ws, {
|
||||
host: harness,
|
||||
scope: harness === 'claude-code' ? mcpScope : 'user',
|
||||
detail: hooksWritten ? 'mcp+hooks' : 'mcp',
|
||||
});
|
||||
// 4. Receipt registration record [CX2-12]. Detail records what actually
|
||||
// landed; nothing landed at all (127 + no hooks) → no receipt entry.
|
||||
if (!mcpSkipped || hooksWritten) {
|
||||
appendReceiptRegistration(home, ws, {
|
||||
host: harness,
|
||||
scope: harness === 'claude-code' ? mcpScope : 'user',
|
||||
detail: hooksWritten ? (mcpSkipped ? 'hooks' : 'mcp+hooks') : 'mcp',
|
||||
});
|
||||
}
|
||||
|
||||
abortIfInjected('wire');
|
||||
return 0;
|
||||
return mcpSkipped ? 2 : 0;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -981,7 +1047,7 @@ async function runVerify(ws: string, rest: string[], home: string): Promise<numb
|
||||
const sourceId = state.state === 'initialized' ? state.manifest.source_id : 'workspace';
|
||||
const result = await verifyWorkspace(engine, ws, { sourceId, gbrainHomeDir: home });
|
||||
if (jsonMode) {
|
||||
console.log(JSON.stringify({ ok: result.ok, checks: result.checks, capability: result.capability, tour: result.tour }, null, 2));
|
||||
console.log(JSON.stringify({ ok: result.ok, checks: result.checks, capability: result.capability, tour: result.tour, handoff: result.handoff }, null, 2));
|
||||
} else {
|
||||
console.log(result.report);
|
||||
}
|
||||
|
||||
+608
-69
@@ -17,19 +17,22 @@
|
||||
*/
|
||||
|
||||
import { spawn } from 'child_process';
|
||||
import { mkdtempSync, writeFileSync, mkdirSync, rmSync, existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { mkdtempSync, writeFileSync, mkdirSync, rmSync, existsSync, readFileSync, appendFileSync, chmodSync, cpSync, lstatSync } from 'fs';
|
||||
import { join, resolve, basename, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { tmpdir } from 'os';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { logFriction, frictionDir } from '../core/friction.ts';
|
||||
import { logFriction, frictionDir, frictionFile } from '../core/friction.ts';
|
||||
import { loadScenario, listScenarios, readBrief, type ScenarioConfig } from '../core/claw-test/scenarios.ts';
|
||||
import { parseProgressEvents, verifyExpectedPhases } from '../core/claw-test/progress-tail.ts';
|
||||
import { resolveAgentRunner, listRegisteredAgents, registerAgentRunner } from '../core/claw-test/agent-runner.ts';
|
||||
import { resolveAgentRunner, listRegisteredAgents, registerAgentRunner, validateBinPathEnv } from '../core/claw-test/agent-runner.ts';
|
||||
import { OpenClawRunner } from '../core/claw-test/runners/openclaw.ts';
|
||||
import { HermesRunner } from '../core/claw-test/runners/hermes.ts';
|
||||
import { createTranscriptSink } from '../core/claw-test/transcript-capture.ts';
|
||||
|
||||
// Ensure built-in runners are registered.
|
||||
registerAgentRunner('openclaw', () => new OpenClawRunner());
|
||||
registerAgentRunner('hermes', () => new HermesRunner());
|
||||
|
||||
interface HarnessOpts {
|
||||
scenario: string;
|
||||
@@ -38,8 +41,11 @@ interface HarnessOpts {
|
||||
keepTempdir: boolean;
|
||||
listAgents: boolean;
|
||||
help: boolean;
|
||||
/** Path to the gbrain binary used to invoke child commands. Defaults to argv[0]. */
|
||||
gbrainBin?: string;
|
||||
/** Path to the gbrain binary used to invoke child commands (always set by
|
||||
* parseArgs: GBRAIN_BIN_OVERRIDE when valid; else the compiled gbrain
|
||||
* binary, or a synthesized launcher when running under the bun runtime —
|
||||
* see resolveGbrainBin). */
|
||||
gbrainBin: string;
|
||||
}
|
||||
|
||||
interface PhaseOutcome {
|
||||
@@ -49,10 +55,24 @@ interface PhaseOutcome {
|
||||
stderrEvents: number;
|
||||
stdoutTail: string;
|
||||
stderrTail: string;
|
||||
/** Full stdout, only populated when invokeGbrain is asked to capture it. */
|
||||
stdoutFull?: string;
|
||||
}
|
||||
|
||||
const TAIL_BYTES = 4_096;
|
||||
const SUBPROCESS_TIMEOUT_MS = 5 * 60_000; // 5 minutes per phase
|
||||
/** Per-phase cap for the harness's own gbrain children (staging, scripted
|
||||
* phases, oracle probes). Env override is a test/incident escape hatch. */
|
||||
const SUBPROCESS_TIMEOUT_MS = envTimeoutMs('GBRAIN_CLAW_PHASE_TIMEOUT_MS', 5 * 60_000);
|
||||
/** Wall clock for the live agent turn — real fresh-install turns run long
|
||||
* (help text promises "5 to 10 min"), so the agent gets double the phase cap. */
|
||||
const LIVE_AGENT_TIMEOUT_MS = envTimeoutMs('GBRAIN_CLAW_AGENT_TIMEOUT_MS', 10 * 60_000);
|
||||
|
||||
function envTimeoutMs(name: string, fallback: number): number {
|
||||
const raw = process.env[name];
|
||||
if (!raw) return fallback;
|
||||
const n = Number(raw);
|
||||
return Number.isFinite(n) && n > 0 ? n : fallback;
|
||||
}
|
||||
|
||||
export async function runClawTest(args: string[]): Promise<number> {
|
||||
const opts = parseArgs(args);
|
||||
@@ -66,6 +86,18 @@ export async function runClawTest(args: string[]): Promise<number> {
|
||||
return cmdListAgents();
|
||||
}
|
||||
|
||||
// Charset guard: both values flow into filesystem paths (scenario → the
|
||||
// fixtures root join, agent → the run-id → the tempdir template), so a
|
||||
// traversal-shaped value would either escape the fixtures root or crash
|
||||
// sanitizeRunId mid-run. Usage error, exit 2.
|
||||
const NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
||||
for (const [flag, value] of [['scenario', opts.scenario], ['agent', opts.agent]] as const) {
|
||||
if (!NAME_RE.test(value)) {
|
||||
console.error(`invalid --${flag} value ${JSON.stringify(value)}: letters, digits, dot, dash, underscore only`);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
let scenario: ScenarioConfig;
|
||||
try {
|
||||
scenario = loadScenario(opts.scenario);
|
||||
@@ -83,6 +115,26 @@ export async function runClawTest(args: string[]): Promise<number> {
|
||||
console.log(`run-id: ${runId}`);
|
||||
console.log(`tempdir: ${runRoot}`);
|
||||
|
||||
// Run-start meta record. Agent-name resolution in `gbrain friction diff`
|
||||
// depends on this: a fully clean run otherwise writes zero agent-stamped
|
||||
// entries and could never be resolved by agent name. Uses the existing
|
||||
// phase-marker kind (no new FrictionKind) + additive scenario/harness_schema
|
||||
// fields.
|
||||
const agentLabel = opts.live ? opts.agent : 'scripted';
|
||||
try {
|
||||
logFriction({
|
||||
runId,
|
||||
phase: 'harness',
|
||||
kind: 'phase-marker',
|
||||
marker: 'start',
|
||||
message: `run start: scenario=${scenario.name} agent=${agentLabel}`,
|
||||
source: 'harness',
|
||||
agent: agentLabel,
|
||||
scenario: scenario.name,
|
||||
harnessSchema: 1,
|
||||
});
|
||||
} catch { /* best effort */ }
|
||||
|
||||
// SIGINT/SIGTERM finalization (D11)
|
||||
let interrupted = false;
|
||||
const onSignal = () => {
|
||||
@@ -94,7 +146,7 @@ export async function runClawTest(args: string[]): Promise<number> {
|
||||
message: 'run interrupted by signal',
|
||||
kind: 'interrupted',
|
||||
source: 'harness',
|
||||
agent: opts.agent,
|
||||
agent: agentLabel,
|
||||
});
|
||||
} catch { /* best effort */ }
|
||||
};
|
||||
@@ -108,9 +160,48 @@ export async function runClawTest(args: string[]): Promise<number> {
|
||||
} else {
|
||||
exitCode = await runScripted(opts, scenario, { runId, runRoot, gbrainHome });
|
||||
}
|
||||
} catch (e) {
|
||||
// Without this, a thrown run (spawn failure, runner detect race) would
|
||||
// reach the finally block with exitCode still 0 and stamp a
|
||||
// `run complete … exit=0` meta record — the friction log (diff/render's
|
||||
// input) silently recording success for a crashed run.
|
||||
exitCode = 1;
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
console.error(`claw-test: run crashed: ${msg}`);
|
||||
if (e instanceof Error && e.stack) console.error(e.stack);
|
||||
try {
|
||||
logFriction({
|
||||
runId,
|
||||
phase: 'harness',
|
||||
message: `harness crashed: ${msg}`,
|
||||
severity: 'blocker',
|
||||
source: 'harness',
|
||||
agent: agentLabel,
|
||||
});
|
||||
} catch { /* best effort */ }
|
||||
} finally {
|
||||
process.off('SIGINT', onSignal);
|
||||
process.off('SIGTERM', onSignal);
|
||||
// Run-completion meta record (pairs with the start marker above).
|
||||
try {
|
||||
logFriction({
|
||||
runId,
|
||||
phase: 'harness',
|
||||
kind: 'phase-marker',
|
||||
marker: 'end',
|
||||
message: `run complete: scenario=${scenario.name} agent=${agentLabel} exit=${exitCode}`,
|
||||
source: 'harness',
|
||||
agent: agentLabel,
|
||||
scenario: scenario.name,
|
||||
});
|
||||
} catch { /* best effort */ }
|
||||
// Persist agent/child-side friction BEFORE the tempdir is deleted. The
|
||||
// children run with GBRAIN_HOME=<runRoot>, so their friction lands under
|
||||
// <runRoot>/.gbrain/friction/<runId>.jsonl — rmSync below would silently
|
||||
// destroy it on every run, leaving `friction render`/`diff` with only the
|
||||
// harness's half of the story. Merge into the parent's friction file
|
||||
// (same runId; the two sides write disjoint entries).
|
||||
mergeChildFriction(runRoot, runId);
|
||||
if (!opts.keepTempdir && !interrupted) {
|
||||
try { rmSync(runRoot, { recursive: true, force: true }); } catch { /* best effort */ }
|
||||
} else {
|
||||
@@ -148,6 +239,36 @@ export async function runClawTest(args: string[]): Promise<number> {
|
||||
*/
|
||||
const POSTGRES_POLLUTION_ENV_VARS = ['DATABASE_URL', 'GBRAIN_DATABASE_URL'];
|
||||
|
||||
/**
|
||||
* Child env for gbrain invocations (scripted phases AND live-mode staging /
|
||||
* oracle probes): parent env minus Postgres-pointing vars AND minus every
|
||||
* other GBRAIN_* routing/tuning var — a stray operator GBRAIN_BRAIN_ID /
|
||||
* GBRAIN_SOURCE / threshold override would misroute the staging and oracle
|
||||
* probes and produce false verify verdicts (the same class scripts/run-e2e.sh
|
||||
* scrubs for e2e hermeticity). The two vars the harness owns are re-applied
|
||||
* last so a parent override can't win.
|
||||
*/
|
||||
function buildChildEnv(ctx: { runId: string; gbrainHome: string }): Record<string, string> {
|
||||
const parentEnv = process.env as Record<string, string | undefined>;
|
||||
const childEnv: Record<string, string> = {};
|
||||
for (const [k, v] of Object.entries(parentEnv)) {
|
||||
if (v === undefined) continue;
|
||||
if (POSTGRES_POLLUTION_ENV_VARS.includes(k)) continue;
|
||||
if (k.startsWith('GBRAIN_')) continue;
|
||||
childEnv[k] = v;
|
||||
}
|
||||
childEnv.GBRAIN_HOME = ctx.gbrainHome;
|
||||
childEnv.GBRAIN_FRICTION_RUN_ID = ctx.runId;
|
||||
return childEnv;
|
||||
}
|
||||
|
||||
/** The hermetic run's PGLite path (configDir appends '.gbrain'). One spelling
|
||||
* for all probe/seed sites — a drifted copy would silently probe a
|
||||
* nonexistent db and fail the upgrade oracle as "unreadable". */
|
||||
function pgliteDbPath(gbrainHome: string): string {
|
||||
return join(gbrainHome, '.gbrain', 'brain.pglite');
|
||||
}
|
||||
|
||||
async function runScripted(
|
||||
opts: HarnessOpts,
|
||||
scenario: ScenarioConfig,
|
||||
@@ -157,17 +278,7 @@ async function runScripted(
|
||||
// The harness is PGLite-only by design; an inherited DATABASE_URL
|
||||
// would force loadConfig() to flip the engine to 'postgres' at the
|
||||
// next phase boundary and break the hermetic-tempdir contract.
|
||||
const parentEnv = process.env as Record<string, string | undefined>;
|
||||
const childEnv: Record<string, string> = { GBRAIN_HOME: ctx.gbrainHome, GBRAIN_FRICTION_RUN_ID: ctx.runId };
|
||||
for (const [k, v] of Object.entries(parentEnv)) {
|
||||
if (v === undefined) continue;
|
||||
if (POSTGRES_POLLUTION_ENV_VARS.includes(k)) continue;
|
||||
childEnv[k] = v;
|
||||
}
|
||||
// Re-apply the explicit overrides so a parent GBRAIN_HOME / GBRAIN_FRICTION_RUN_ID
|
||||
// can't accidentally win the merge.
|
||||
childEnv.GBRAIN_HOME = ctx.gbrainHome;
|
||||
childEnv.GBRAIN_FRICTION_RUN_ID = ctx.runId;
|
||||
const childEnv = buildChildEnv(ctx);
|
||||
|
||||
const phases: { name: string; argv: string[] }[] = [];
|
||||
// Phase 2: install_brain. `--no-embedding` defers embedding setup so the
|
||||
@@ -200,35 +311,51 @@ async function runScripted(
|
||||
// Phase 6: verify
|
||||
phases.push({ name: 'verify', argv: ['doctor', '--json', '--progress-json'] });
|
||||
|
||||
// Pre-phase: upgrade scenario seeds the database
|
||||
if (scenario.kind === 'upgrade' && scenario.seedRelative) {
|
||||
const seedSql = join(scenario.dir, scenario.seedRelative, 'dump.sql');
|
||||
if (existsSync(seedSql)) {
|
||||
const dbPath = join(ctx.gbrainHome, '.gbrain', 'brain.pglite');
|
||||
mkdirSync(join(ctx.gbrainHome, '.gbrain'), { recursive: true });
|
||||
const { seedPgliteFromFile } = await import('../core/claw-test/seed-pglite.ts');
|
||||
try {
|
||||
await seedPgliteFromFile({ dbPath, sqlPath: seedSql });
|
||||
console.log(`[seed] replayed ${seedSql} → ${dbPath}`);
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
logFriction({
|
||||
runId: ctx.runId,
|
||||
phase: 'seed',
|
||||
message: `seed replay failed: ${msg}`,
|
||||
severity: 'blocker',
|
||||
source: 'harness',
|
||||
agent: opts.agent,
|
||||
});
|
||||
return 1;
|
||||
}
|
||||
// Pre-phase: upgrade scenario seeds the database. A missing dump is a LOUD
|
||||
// failure, not a skip: skipping would init a current-version database and
|
||||
// report a false-green "upgrade" that never exercised a migration.
|
||||
if (scenario.kind === 'upgrade') {
|
||||
const seedSql = scenario.seedRelative ? join(scenario.dir, scenario.seedRelative, 'dump.sql') : null;
|
||||
if (!seedSql || !existsSync(seedSql)) {
|
||||
const msg = seedSql
|
||||
? `upgrade scenario has no seed dump at ${seedSql}`
|
||||
: 'upgrade scenario declares no seed dir';
|
||||
logFriction({
|
||||
runId: ctx.runId,
|
||||
phase: 'seed',
|
||||
message: msg,
|
||||
severity: 'blocker',
|
||||
hint: 'upgrade runs need a real dump.sql to measure the migration (see the TODOS entry for the v0.18 seed dump)',
|
||||
source: 'harness',
|
||||
agent: 'scripted',
|
||||
});
|
||||
console.error(`[seed] ${msg}`);
|
||||
return 1;
|
||||
}
|
||||
const dbPath = pgliteDbPath(ctx.gbrainHome);
|
||||
mkdirSync(join(ctx.gbrainHome, '.gbrain'), { recursive: true });
|
||||
const { seedPgliteFromFile } = await import('../core/claw-test/seed-pglite.ts');
|
||||
try {
|
||||
await seedPgliteFromFile({ dbPath, sqlPath: seedSql });
|
||||
console.log(`[seed] replayed ${seedSql} → ${dbPath}`);
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
logFriction({
|
||||
runId: ctx.runId,
|
||||
phase: 'seed',
|
||||
message: `seed replay failed: ${msg}`,
|
||||
severity: 'blocker',
|
||||
source: 'harness',
|
||||
agent: 'scripted',
|
||||
});
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
const allStderr: string[] = [];
|
||||
const outcomes: PhaseOutcome[] = [];
|
||||
for (const phase of phases) {
|
||||
const outcome = await invokeGbrain(opts.gbrainBin ?? 'gbrain', phase.argv, ctx.runRoot, childEnv);
|
||||
const outcome = await invokeGbrain(opts.gbrainBin, phase.argv, ctx.runRoot, childEnv);
|
||||
outcome.phase = phase.name;
|
||||
outcomes.push(outcome);
|
||||
allStderr.push(outcome.stderrTail);
|
||||
@@ -240,7 +367,7 @@ async function runScripted(
|
||||
severity: 'error',
|
||||
hint: outcome.stderrTail.trim().slice(0, 500),
|
||||
source: 'harness',
|
||||
agent: opts.agent,
|
||||
agent: 'scripted',
|
||||
});
|
||||
return 1;
|
||||
} else {
|
||||
@@ -251,7 +378,7 @@ async function runScripted(
|
||||
kind: 'phase-marker',
|
||||
marker: 'end',
|
||||
source: 'harness',
|
||||
agent: opts.agent,
|
||||
agent: 'scripted',
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -268,7 +395,7 @@ async function runScripted(
|
||||
severity: 'blocker',
|
||||
hint: 'either the command did not run or it did not emit progress events; check phase log above',
|
||||
source: 'harness',
|
||||
agent: opts.agent,
|
||||
agent: 'scripted',
|
||||
});
|
||||
}
|
||||
return 1;
|
||||
@@ -281,6 +408,19 @@ async function runScripted(
|
||||
// Live mode
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Per-agent install hints for the agent_detect blocker. */
|
||||
const AGENT_INSTALL_HINTS: Record<string, string> = {
|
||||
openclaw: 'install openclaw or set OPENCLAW_BIN',
|
||||
hermes: 'install hermes (https://hermes-agent.nousresearch.com) or set HERMES_BIN',
|
||||
};
|
||||
|
||||
/**
|
||||
* Live mode. Hermeticity posture (deliberate, documented): live mode runs the
|
||||
* OPERATOR's configured agent — the real agent home (~/.openclaw, ~/.hermes,
|
||||
* model settings, skills) is inherited — against a HERMETIC BRAIN
|
||||
* (GBRAIN_HOME=tempdir). The fully hermetic lane is the door e2e
|
||||
* (install-real-hermes.serial.test.ts), which isolates the agent home too.
|
||||
*/
|
||||
async function runLive(
|
||||
opts: HarnessOpts,
|
||||
scenario: ScenarioConfig,
|
||||
@@ -302,17 +442,49 @@ async function runLive(
|
||||
phase: 'agent_detect',
|
||||
message: `agent ${opts.agent} not available: ${detected.reason ?? 'unknown'}`,
|
||||
severity: 'blocker',
|
||||
hint: opts.agent === 'openclaw' ? 'install openclaw or set OPENCLAW_BIN' : undefined,
|
||||
hint: AGENT_INSTALL_HINTS[opts.agent],
|
||||
source: 'harness',
|
||||
agent: opts.agent,
|
||||
});
|
||||
return 2;
|
||||
}
|
||||
|
||||
// ---- Stage the scenario (scenario-driven; mirrors the scripted branch) ----
|
||||
// The BRIEF's preconditions must actually exist before the agent reads it:
|
||||
// fresh-install promises "workspace already has an AGENTS.md", "3 small
|
||||
// markdown pages already there" (./brain), and "user just ran gbrain init".
|
||||
// Without staging, every live run starts in an empty tempdir and the run
|
||||
// measures recovery-from-broken-fixture, not gbrain friction.
|
||||
const childEnv = buildChildEnv(ctx);
|
||||
const gbrainBin = opts.gbrainBin;
|
||||
const stageFailed = await stageLiveScenario(opts, scenario, ctx, childEnv, gbrainBin);
|
||||
if (stageFailed !== 0) return stageFailed;
|
||||
|
||||
// Upgrade oracle needs the pre-turn schema version (non-mutating probe —
|
||||
// any gbrain CLI connect would auto-apply migrations and do the agent's
|
||||
// work for it).
|
||||
const dbPath = pgliteDbPath(ctx.gbrainHome);
|
||||
let preVersion: number | null = null;
|
||||
if (scenario.kind === 'upgrade') {
|
||||
const { readPgliteSchemaVersion } = await import('../core/claw-test/seed-pglite.ts');
|
||||
preVersion = await readPgliteSchemaVersion(dbPath);
|
||||
}
|
||||
|
||||
// ---- PATH shim: the BRIEF says `gbrain …`; make bare `gbrain` resolve to
|
||||
// THIS harness's binary (operator PATH may have none, or a stale global). ----
|
||||
const shimDir = join(ctx.runRoot, '.harness-bin');
|
||||
mkdirSync(shimDir, { recursive: true });
|
||||
const shimPath = join(shimDir, 'gbrain');
|
||||
// Single-quoted: validateBinPathEnv rejects quote/metacharacter values, so
|
||||
// the interpolation cannot break out of the quoting.
|
||||
writeFileSync(shimPath, `#!/bin/sh\nexec '${gbrainBin}' "$@"\n`, 'utf-8');
|
||||
chmodSync(shimPath, 0o755);
|
||||
|
||||
const sink = createTranscriptSink(ctx.transcriptPath);
|
||||
const env: Record<string, string> = {
|
||||
GBRAIN_HOME: ctx.gbrainHome,
|
||||
GBRAIN_FRICTION_RUN_ID: ctx.runId,
|
||||
PATH: `${shimDir}:${process.env.PATH ?? ''}`,
|
||||
};
|
||||
|
||||
const brief = readBrief(scenario);
|
||||
@@ -322,7 +494,7 @@ async function runLive(
|
||||
cwd: ctx.runRoot,
|
||||
brief,
|
||||
env,
|
||||
timeoutMs: SUBPROCESS_TIMEOUT_MS,
|
||||
timeoutMs: LIVE_AGENT_TIMEOUT_MS,
|
||||
transcriptSink: sink,
|
||||
});
|
||||
} finally {
|
||||
@@ -340,9 +512,270 @@ async function runLive(
|
||||
});
|
||||
return result.exitCode;
|
||||
}
|
||||
|
||||
// ---- Success oracle: exit code alone passes an agent that did nothing. ----
|
||||
return verifyLiveOutcome(opts, scenario, ctx, childEnv, gbrainBin, preVersion);
|
||||
}
|
||||
|
||||
/** Stage the workspace per scenario.kind before the agent turn. Returns 0 or a failing exit code. */
|
||||
async function stageLiveScenario(
|
||||
opts: HarnessOpts,
|
||||
scenario: ScenarioConfig,
|
||||
ctx: { runId: string; runRoot: string; gbrainHome: string },
|
||||
childEnv: Record<string, string>,
|
||||
gbrainBin: string,
|
||||
): Promise<number> {
|
||||
const failStage = (message: string, hint?: string): number => {
|
||||
logFriction({
|
||||
runId: ctx.runId,
|
||||
phase: 'stage',
|
||||
message,
|
||||
severity: 'blocker',
|
||||
hint,
|
||||
source: 'harness',
|
||||
agent: opts.agent,
|
||||
});
|
||||
console.error(`[stage] ${message}`);
|
||||
return 1;
|
||||
};
|
||||
|
||||
if (scenario.kind === 'upgrade') {
|
||||
// Seed ONLY — running init here would walk the migration chain forward
|
||||
// and do the very upgrade the agent turn is supposed to perform (any
|
||||
// gbrain connect auto-migrates). Same seed-first order as scripted mode.
|
||||
if (scenario.seedRelative) {
|
||||
const seedSql = join(scenario.dir, scenario.seedRelative, 'dump.sql');
|
||||
if (existsSync(seedSql)) {
|
||||
const dbPath = pgliteDbPath(ctx.gbrainHome);
|
||||
mkdirSync(join(ctx.gbrainHome, '.gbrain'), { recursive: true });
|
||||
const { seedPgliteFromFile } = await import('../core/claw-test/seed-pglite.ts');
|
||||
try {
|
||||
await seedPgliteFromFile({ dbPath, sqlPath: seedSql });
|
||||
console.log(`[stage] replayed ${seedSql} → ${dbPath}`);
|
||||
} catch (e) {
|
||||
return failStage(`seed replay failed: ${e instanceof Error ? e.message : String(e)}`);
|
||||
}
|
||||
} else {
|
||||
return failStage(`upgrade scenario has no seed dump at ${seedSql}`, 'upgrade runs need a real dump.sql to measure the migration (see the TODOS entry for the v0.18 seed dump)');
|
||||
}
|
||||
} else {
|
||||
return failStage('upgrade scenario declares no seed dir');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// fresh-install: copy the scenario's brain pages + an AGENTS.md stub, then
|
||||
// init the brain (the BRIEF says the user "just ran gbrain init").
|
||||
if (scenario.brainRelative) {
|
||||
const src = join(scenario.dir, scenario.brainRelative);
|
||||
if (!existsSync(src)) {
|
||||
// Fail loudly, matching the upgrade branch's missing-seed blocker: a
|
||||
// silent skip here would fail the query oracle later with the
|
||||
// misleading hint "the agent likely skipped the import step" when the
|
||||
// real cause is a broken fixture.
|
||||
return failStage(`fresh-install scenario declares brain dir ${scenario.brainRelative} but it does not exist at ${src}`);
|
||||
}
|
||||
cpSync(src, join(ctx.runRoot, 'brain'), { recursive: true });
|
||||
}
|
||||
const agentsMd = join(ctx.runRoot, 'AGENTS.md');
|
||||
if (!existsSync(agentsMd)) {
|
||||
// Deliberately references NO skill files: staging creates none, and a row
|
||||
// pointing at a missing SKILL.md flips doctor's resolver_health to fail
|
||||
// (caught in rehearsal). Post-v0.33 scaffolded skills route via their own
|
||||
// frontmatter triggers, so a prose stub satisfies the BRIEF's
|
||||
// "workspace already has an AGENTS.md routing file" precondition.
|
||||
writeFileSync(
|
||||
agentsMd,
|
||||
'# Workspace routing\n\nSkills scaffolded under `skills/` route via their frontmatter `triggers:`.\n',
|
||||
'utf-8',
|
||||
);
|
||||
}
|
||||
const init = await invokeGbrain(gbrainBin, ['init', '--pglite', '--no-embedding'], ctx.runRoot, childEnv);
|
||||
if (init.exitCode !== 0) {
|
||||
return failStage(`gbrain init failed during staging (exit ${init.exitCode})`, init.stderrTail.trim().slice(0, 500));
|
||||
}
|
||||
console.log('[stage] fresh-install workspace staged (brain pages + AGENTS.md + init)');
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Post-turn verification. Logs friction (phase 'verify') and returns 1 on any failure. */
|
||||
async function verifyLiveOutcome(
|
||||
opts: HarnessOpts,
|
||||
scenario: ScenarioConfig,
|
||||
ctx: { runId: string; runRoot: string; gbrainHome: string },
|
||||
childEnv: Record<string, string>,
|
||||
gbrainBin: string,
|
||||
preVersion: number | null,
|
||||
): Promise<number> {
|
||||
const failures: { message: string; hint?: string }[] = [];
|
||||
|
||||
if (scenario.kind === 'upgrade') {
|
||||
// The upgrade oracle is the schema version reaching LATEST during the
|
||||
// agent turn, read via the non-mutating direct-PGLite probe (doctor/any
|
||||
// CLI connect would apply the migrations itself and mask a do-nothing
|
||||
// agent). MUST run before any declared query oracle below — the query's
|
||||
// own CLI connect migrates, which would corrupt a later version read.
|
||||
const { readPgliteSchemaVersion } = await import('../core/claw-test/seed-pglite.ts');
|
||||
const { LATEST_VERSION } = await import('../core/migrate.ts');
|
||||
const dbPath = pgliteDbPath(ctx.gbrainHome);
|
||||
const postVersion = await readPgliteSchemaVersion(dbPath);
|
||||
if (preVersion === null || postVersion === null) {
|
||||
failures.push({ message: `upgrade oracle: schema version unreadable (pre=${preVersion} post=${postVersion})` });
|
||||
} else if (postVersion <= preVersion) {
|
||||
failures.push({
|
||||
message: `upgrade oracle: schema version did not advance during the agent turn (pre=${preVersion} post=${postVersion})`,
|
||||
hint: 'the agent never ran a gbrain command that walks the migration chain',
|
||||
});
|
||||
} else if (postVersion < LATEST_VERSION) {
|
||||
// Advancing one step is not an upgrade: any gbrain connect migrates to
|
||||
// latest, so a partial version means the agent's run died mid-chain.
|
||||
failures.push({
|
||||
message: `upgrade oracle: schema version advanced but stopped short of latest (pre=${preVersion} post=${postVersion} latest=${LATEST_VERSION})`,
|
||||
hint: 'a gbrain command started the migration chain but did not complete it',
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// doctor: fresh --no-embedding brains report status "warnings" (observed
|
||||
// — embedding setup is deferred by design), so healthy AND warnings both
|
||||
// pass. Anything else — including output the harness cannot parse — is a
|
||||
// failure: an oracle that shrugs at unparsable output fails open.
|
||||
const doc = await invokeGbrain(gbrainBin, ['doctor', '--json'], ctx.runRoot, childEnv, { captureFullStdout: true });
|
||||
if (doc.exitCode !== 0) {
|
||||
failures.push({ message: `verify: doctor exited ${doc.exitCode}`, hint: doc.stderrTail.trim().slice(0, 300) });
|
||||
} else {
|
||||
const report = parseLastJson(doc.stdoutFull ?? doc.stdoutTail);
|
||||
const status = report && typeof report === 'object' ? (report as Record<string, unknown>).status : undefined;
|
||||
if (report === null || typeof status !== 'string') {
|
||||
failures.push({ message: 'verify: doctor exited 0 but its JSON output was unparsable' });
|
||||
} else if (status !== 'healthy' && status !== 'warnings') {
|
||||
failures.push({ message: `verify: doctor reports status ${JSON.stringify(status)}` });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A declared oracle is enforced for EVERY kind (loadScenario validates it
|
||||
// for every kind — accepting config it never enforces would be a silent
|
||||
// no-op; an upgrade agent that migrates the schema but loses the seeded
|
||||
// data must still fail a declared query oracle).
|
||||
const oracle = scenario.oracle;
|
||||
if (oracle?.query) {
|
||||
const q = await invokeGbrain(gbrainBin, ['query', oracle.query, '--json'], ctx.runRoot, childEnv, { captureFullStdout: true });
|
||||
const parsed = q.exitCode === 0 ? parseLastJson(q.stdoutFull ?? q.stdoutTail) : null;
|
||||
const min = oracle.minResults ?? 1;
|
||||
// query --json emits a bare array; anything else on a zero exit means the
|
||||
// command's contract broke — fail even when min_results is 0, because
|
||||
// "0 results required" never licenses unparsable output.
|
||||
if (q.exitCode === 0 && !Array.isArray(parsed)) {
|
||||
failures.push({
|
||||
message: `verify: query ${JSON.stringify(oracle.query)} exited 0 but its JSON output was unparsable`,
|
||||
});
|
||||
} else {
|
||||
const count = Array.isArray(parsed) ? parsed.length : 0;
|
||||
if (q.exitCode !== 0 || count < min) {
|
||||
failures.push({
|
||||
message: `verify: query ${JSON.stringify(oracle.query)} returned ${count} result(s), expected >= ${min} (exit ${q.exitCode})`,
|
||||
hint: 'the agent likely skipped the import step from the brief',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const rel of oracle?.filesExist ?? []) {
|
||||
if (!existsSync(join(ctx.runRoot, rel))) {
|
||||
failures.push({
|
||||
message: `verify: expected file missing after run: ${rel}`,
|
||||
hint: 'the agent likely skipped a brief step that produces this file',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const f of failures) {
|
||||
logFriction({
|
||||
runId: ctx.runId,
|
||||
phase: 'verify',
|
||||
message: f.message,
|
||||
severity: 'error',
|
||||
hint: f.hint,
|
||||
source: 'harness',
|
||||
agent: opts.agent,
|
||||
});
|
||||
console.error(`[verify] ${f.message}`);
|
||||
}
|
||||
return failures.length ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the trailing JSON document from CLI stdout (defensive: banners or
|
||||
* notices may precede the payload).
|
||||
*/
|
||||
function parseLastJson(stdout: string): unknown {
|
||||
const text = stdout.trim();
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch { /* fall through */ }
|
||||
const starts = ['{', '['];
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
if (starts.includes(text[i])) {
|
||||
try {
|
||||
return JSON.parse(text.slice(i));
|
||||
} catch { /* keep scanning */ }
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Refuse to import child friction files larger than this — the file lives in
|
||||
* a workspace the AGENT writes to, so its size is untrusted. */
|
||||
const CHILD_FRICTION_MAX_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* E0: merge the child-side friction file (written under the run's hermetic
|
||||
* GBRAIN_HOME) into the parent process's friction dir so it survives tempdir
|
||||
* cleanup. Best-effort — a merge failure never fails the run.
|
||||
*
|
||||
* The child file is UNTRUSTED input (in live mode the agent can write
|
||||
* arbitrary bytes at that path), and the destination is the operator's
|
||||
* permanent friction log: require a regular file (no symlink — an agent-
|
||||
* dropped link could import any readable file on the box), cap the size, and
|
||||
* append only lines that parse as JSON objects so the log stays valid JSONL.
|
||||
* Exported for tests.
|
||||
*/
|
||||
export function mergeChildFriction(runRoot: string, runId: string): void {
|
||||
try {
|
||||
const childFile = join(runRoot, '.gbrain', 'friction', `${runId}.jsonl`);
|
||||
const st = lstatSync(childFile, { throwIfNoEntry: false });
|
||||
if (!st) return;
|
||||
if (!st.isFile()) {
|
||||
console.error(`[friction] skipping child friction merge: ${childFile} is not a regular file`);
|
||||
return;
|
||||
}
|
||||
if (st.size > CHILD_FRICTION_MAX_BYTES) {
|
||||
console.error(`[friction] skipping child friction merge: ${childFile} is ${st.size} bytes (cap ${CHILD_FRICTION_MAX_BYTES})`);
|
||||
return;
|
||||
}
|
||||
const parentFile = frictionFile(runId);
|
||||
if (resolve(childFile) === resolve(parentFile)) return;
|
||||
const raw = readFileSync(childFile, 'utf-8');
|
||||
if (!raw.trim()) return;
|
||||
const kept: string[] = [];
|
||||
let skipped = 0;
|
||||
for (const line of raw.split('\n')) {
|
||||
if (!line.trim()) continue;
|
||||
try {
|
||||
const parsed = JSON.parse(line);
|
||||
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) kept.push(line);
|
||||
else skipped++;
|
||||
} catch {
|
||||
skipped++;
|
||||
}
|
||||
}
|
||||
if (skipped) console.error(`[friction] child friction merge skipped ${skipped} non-JSONL line(s)`);
|
||||
if (!kept.length) return;
|
||||
const dir = frictionDir();
|
||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
||||
appendFileSync(parentFile, kept.join('\n') + '\n', 'utf-8');
|
||||
} catch { /* best effort */ }
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Subprocess helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -352,17 +785,60 @@ function invokeGbrain(
|
||||
argv: string[],
|
||||
cwd: string,
|
||||
env: Record<string, string>,
|
||||
invokeOpts?: { captureFullStdout?: boolean },
|
||||
): Promise<PhaseOutcome> {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise((resolvePromise) => {
|
||||
const start = Date.now();
|
||||
const child = spawn(bin, argv, { cwd, env, stdio: ['ignore', 'pipe', 'pipe'], shell: false });
|
||||
const stdout: Buffer[] = [];
|
||||
const stderr: Buffer[] = [];
|
||||
child.stdout?.on('data', (b: Buffer) => stdout.push(b));
|
||||
child.stderr?.on('data', (b: Buffer) => stderr.push(b));
|
||||
|
||||
// A hung child (e.g. a leaked PGLite lock holder from the agent turn)
|
||||
// must not wedge the harness/CI job forever: SIGTERM at the phase cap,
|
||||
// SIGKILL if it lingers.
|
||||
let timedOut = false;
|
||||
let killTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
const wallClockTimer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
try { child.kill('SIGTERM'); } catch { /* already gone */ }
|
||||
killTimer = setTimeout(() => {
|
||||
try { child.kill('SIGKILL'); } catch { /* already gone */ }
|
||||
}, 10_000);
|
||||
}, SUBPROCESS_TIMEOUT_MS);
|
||||
const clearTimers = () => {
|
||||
clearTimeout(wallClockTimer);
|
||||
if (killTimer) clearTimeout(killTimer);
|
||||
};
|
||||
|
||||
let settled = false;
|
||||
const settle = (code: number | null) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimers();
|
||||
let stderrText = Buffer.concat(stderr).toString('utf-8');
|
||||
if (timedOut) stderrText += `\nharness: killed after ${SUBPROCESS_TIMEOUT_MS}ms phase timeout`;
|
||||
const stdoutText = Buffer.concat(stdout).toString('utf-8');
|
||||
resolvePromise({
|
||||
phase: '',
|
||||
exitCode: typeof code === 'number' ? code : (timedOut ? 124 : 1),
|
||||
durationMs: Date.now() - start,
|
||||
stderrEvents: parseProgressEvents(stderrText).length,
|
||||
stdoutTail: tailOf(stdoutText),
|
||||
stderrTail: stderrText,
|
||||
// The 4KB tail is fine for logging but NOT for parsing JSON payloads
|
||||
// (doctor --json exceeds it and would lose its opening brace).
|
||||
...(invokeOpts?.captureFullStdout ? { stdoutFull: stdoutText } : {}),
|
||||
});
|
||||
};
|
||||
|
||||
child.on('error', (err) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimers();
|
||||
const stderrJoined = Buffer.concat(stderr).toString('utf-8') + '\nspawn error: ' + err.message;
|
||||
resolve({
|
||||
resolvePromise({
|
||||
phase: '',
|
||||
exitCode: 127,
|
||||
durationMs: Date.now() - start,
|
||||
@@ -371,16 +847,13 @@ function invokeGbrain(
|
||||
stderrTail: tailOf(stderrJoined),
|
||||
});
|
||||
});
|
||||
child.on('close', (code) => {
|
||||
const stderrText = Buffer.concat(stderr).toString('utf-8');
|
||||
resolve({
|
||||
phase: '',
|
||||
exitCode: typeof code === 'number' ? code : 1,
|
||||
durationMs: Date.now() - start,
|
||||
stderrEvents: parseProgressEvents(stderrText).length,
|
||||
stdoutTail: tailOf(Buffer.concat(stdout).toString('utf-8')),
|
||||
stderrTail: stderrText,
|
||||
});
|
||||
// 'close' (pipes drained) is the clean path; 'exit' + grace covers a
|
||||
// grandchild that inherits the pipes and outlives the kill — without it a
|
||||
// timed-out phase whose child leaked a subprocess would wedge forever.
|
||||
child.on('close', (code) => settle(code));
|
||||
child.on('exit', (code) => {
|
||||
const t = setTimeout(() => settle(code), 2_000);
|
||||
t.unref?.();
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -402,7 +875,7 @@ function parseArgs(args: string[]): HarnessOpts {
|
||||
keepTempdir: false,
|
||||
listAgents: false,
|
||||
help: args.includes('--help') || args.includes('-h'),
|
||||
gbrainBin: process.env.GBRAIN_BIN_OVERRIDE || process.execPath,
|
||||
gbrainBin: resolveGbrainBin(),
|
||||
};
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const a = args[i];
|
||||
@@ -415,6 +888,60 @@ function parseArgs(args: string[]): HarnessOpts {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the gbrain binary child invocations use. GBRAIN_BIN_OVERRIDE goes
|
||||
* through the same absolute/no-dotdot/no-metacharacter validation as the
|
||||
* *_BIN runner overrides — the value is interpolated into a generated
|
||||
* PATH-shim script in live mode, so a relative value would re-resolve through
|
||||
* the shimmed PATH and self-exec forever, and quoting-hostile characters
|
||||
* would become code. Invalid overrides are rejected loudly (stderr) and the
|
||||
* harness falls back to the current executable.
|
||||
*
|
||||
* The fallback is NOT bare process.execPath: under `bun run src/cli.ts` (the
|
||||
* canonical source install) or a bun-global launcher, execPath is the Bun
|
||||
* RUNTIME, and children would run `bun init` / `bun import` instead of gbrain
|
||||
* — scaffolding a Bun project in the hermetic workspace and failing the rest
|
||||
* of the run. When execPath looks like bun, synthesize a launcher shim that
|
||||
* re-enters this checkout's cli.ts; only a compiled gbrain binary returns
|
||||
* execPath directly.
|
||||
*/
|
||||
let cachedGbrainBin: string | null = null;
|
||||
|
||||
function resolveGbrainBin(): string {
|
||||
if (cachedGbrainBin) return cachedGbrainBin;
|
||||
cachedGbrainBin = resolveGbrainBinUncached();
|
||||
return cachedGbrainBin;
|
||||
}
|
||||
|
||||
function resolveGbrainBinUncached(): string {
|
||||
const override = process.env.GBRAIN_BIN_OVERRIDE?.trim();
|
||||
if (override) {
|
||||
const invalid = validateBinPathEnv('GBRAIN_BIN_OVERRIDE', override);
|
||||
if (!invalid) return override;
|
||||
console.error(`ignoring ${invalid}; falling back to the current executable`);
|
||||
}
|
||||
const exe = process.execPath;
|
||||
if (/^bun(-profile)?(\.exe)?$/i.test(basename(exe))) {
|
||||
// src/commands/claw-test.ts → ../cli.ts. Under a compiled binary this
|
||||
// branch never fires (execPath is the gbrain binary itself); under bun
|
||||
// (dev checkout or bun-global install) import.meta resolves to the real
|
||||
// source file next to cli.ts.
|
||||
const cliTs = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'cli.ts');
|
||||
if (existsSync(cliTs) && !/['\n\r]/.test(exe) && !/['\n\r]/.test(cliTs)) {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'gbrain-launcher-'));
|
||||
const launcher = join(dir, 'gbrain');
|
||||
writeFileSync(launcher, `#!/bin/sh\nexec '${exe}' '${cliTs}' "$@"\n`, 'utf-8');
|
||||
chmodSync(launcher, 0o755);
|
||||
return launcher;
|
||||
}
|
||||
console.error(
|
||||
'claw-test: running under the bun runtime but the gbrain CLI entrypoint could not be located — ' +
|
||||
'child gbrain invocations would run bun itself. Set GBRAIN_BIN_OVERRIDE to a gbrain binary.',
|
||||
);
|
||||
}
|
||||
return exe;
|
||||
}
|
||||
|
||||
function newRunId(agent: string): string {
|
||||
const now = new Date();
|
||||
const ts = now.toISOString().replace(/[-:]/g, '').replace(/\..*/, '').replace('T', '-');
|
||||
@@ -422,23 +949,30 @@ function newRunId(agent: string): string {
|
||||
return `claw-test-${ts}-${agent}-${suf}`;
|
||||
}
|
||||
|
||||
function cmdListAgents(): number {
|
||||
async function cmdListAgents(): Promise<number> {
|
||||
const names = listRegisteredAgents();
|
||||
if (!names.length) {
|
||||
console.log('no agents registered');
|
||||
return 0;
|
||||
}
|
||||
for (const name of names) {
|
||||
// Detect concurrently but AWAIT all of them, then print in
|
||||
// listRegisteredAgents() order (the accessor sorts alphabetically).
|
||||
// The prior fire-and-forget .then() version returned before any detection
|
||||
// resolved, so output could vanish in CLI teardown.
|
||||
const lines = await Promise.all(names.map(async (name) => {
|
||||
try {
|
||||
const runner = resolveAgentRunner(name);
|
||||
runner.detect().then((d) => {
|
||||
const status = d.available ? `available at ${d.binPath}` : `unavailable: ${d.reason}`;
|
||||
console.log(`${name}: ${status}`);
|
||||
}).catch(() => { /* best effort */ });
|
||||
try {
|
||||
const d = await runner.detect();
|
||||
return `${name}: ${d.available ? `available at ${d.binPath}` : `unavailable: ${d.reason}`}`;
|
||||
} catch {
|
||||
return `${name}: (detect error)`;
|
||||
}
|
||||
} catch {
|
||||
console.log(`${name}: (factory error)`);
|
||||
return `${name}: (factory error)`;
|
||||
}
|
||||
}
|
||||
}));
|
||||
for (const line of lines) console.log(line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -456,8 +990,13 @@ Defaults:
|
||||
Scripted mode runs canonical commands without an LLM (CI gate).
|
||||
Live mode spawns a real agent and lets it drive (~5–10 min, costs tokens).
|
||||
|
||||
Live mode runs YOUR configured agent (it may read/write your real agent home,
|
||||
e.g. ~/.openclaw or ~/.hermes) against a hermetic brain. The door e2e suite is
|
||||
the fully hermetic lane.
|
||||
|
||||
Examples:
|
||||
gbrain claw-test --scenario fresh-install
|
||||
gbrain claw-test --scenario upgrade-from-v0.18 --keep-tempdir
|
||||
gbrain claw-test --live --agent openclaw`);
|
||||
gbrain claw-test --live --agent openclaw
|
||||
gbrain claw-test --live --agent hermes`);
|
||||
}
|
||||
|
||||
@@ -1285,8 +1285,7 @@ export function checkSelfUpgradeHealth(): Check {
|
||||
const { loadConfig } = require('../core/config.ts');
|
||||
const {
|
||||
resolveSelfUpgradeMode,
|
||||
readUpdateCache,
|
||||
isCacheFresh,
|
||||
pendingUpgradeVersion,
|
||||
} = require('../core/self-upgrade.ts');
|
||||
const { readRecentSelfUpgrades } = require('../core/audit/self-upgrade-audit.ts');
|
||||
|
||||
@@ -1301,9 +1300,11 @@ export function checkSelfUpgradeHealth(): Check {
|
||||
}
|
||||
|
||||
const parts: string[] = [`mode=${mode}`];
|
||||
const entry = readUpdateCache();
|
||||
if (entry && isCacheFresh(entry, Date.now()) && entry.marker.kind === 'upgrade_available') {
|
||||
parts.push(`update available: ${entry.marker.current} -> ${entry.marker.latest} (run: gbrain self-upgrade)`);
|
||||
// Shared stale/foreign-cache guard: only report an upgrade strictly newer
|
||||
// than the RUNNING binary (pendingUpgradeVersion owns the rule).
|
||||
const pendingLatest = pendingUpgradeVersion(GBRAIN_BINARY_VERSION, Date.now());
|
||||
if (pendingLatest) {
|
||||
parts.push(`update available: ${GBRAIN_BINARY_VERSION} -> ${pendingLatest} (run: gbrain self-upgrade)`);
|
||||
}
|
||||
const failedVersions: string[] = cfg?.self_upgrade?.failed_versions ?? [];
|
||||
if (failedVersions.length > 0) {
|
||||
|
||||
+324
-5
@@ -1,21 +1,24 @@
|
||||
/**
|
||||
* gbrain friction — friction reporter CLI.
|
||||
*
|
||||
* Four subcommands in v1 (analytical/clustering ones move to v1.1):
|
||||
* Five subcommands (remaining analytical ones — trend, migration-stub — stay v1.1):
|
||||
* gbrain friction log Append a friction or delight entry
|
||||
* gbrain friction render Render a run as markdown or JSON
|
||||
* gbrain friction list List recent runs with counts
|
||||
* gbrain friction summary Side-by-side friction + delight summary
|
||||
* gbrain friction diff Compare two runs (or agents): unique-to-each + shared-but-changed
|
||||
*
|
||||
* Subcommands stay thin (≤ ~30 LOC each). Core logic lives in src/core/friction.ts.
|
||||
* Subcommands stay thin (≤ ~30 LOC each). Reader/writer/redaction logic lives
|
||||
* in src/core/friction.ts; the diff computation lives here (it is CLI-only).
|
||||
*
|
||||
* The CLI is dispatched from src/cli.ts. See `gbrain friction --help`.
|
||||
*/
|
||||
|
||||
import { existsSync } from 'fs';
|
||||
import {
|
||||
logFriction, readFriction, listRuns, renderReport, renderSummary,
|
||||
activeRunId, frictionFile,
|
||||
type FrictionKind, type FrictionSeverity,
|
||||
activeRunId, frictionFile, frictionDir, redactEntry,
|
||||
type FrictionKind, type FrictionSeverity, type FrictionEntry, type ReadResult,
|
||||
} from '../core/friction.ts';
|
||||
|
||||
const VALID_KINDS = new Set<FrictionKind>(['friction', 'delight', 'phase-marker', 'interrupted']);
|
||||
@@ -28,6 +31,7 @@ export function runFriction(args: string[]): number {
|
||||
case 'render': return cmdRender(rest);
|
||||
case 'list': return cmdList(rest);
|
||||
case 'summary': return cmdSummary(rest);
|
||||
case 'diff': return cmdDiff(rest);
|
||||
case undefined:
|
||||
case '--help':
|
||||
case '-h':
|
||||
@@ -150,6 +154,317 @@ function cmdSummary(args: string[]): number {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// diff
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Entry identity for diffing: (kind, phase, normalized message prefix) —
|
||||
* lowercase, whitespace-collapsed, digit runs collapsed (durations, counts,
|
||||
* and tempdir suffixes would otherwise make the same friction land in
|
||||
* "unique to each" across runs), first 80 chars, with redaction applied
|
||||
* FIRST (the caller redacts via redactEntry). Kind IS identity: a delight
|
||||
* and a friction with the same text are different findings, and a
|
||||
* delight→friction flip must surface, never compare equal. Severity is
|
||||
* deliberately EXCLUDED from identity — it is the compared attribute, as a
|
||||
* PER-SEVERITY MULTISET (two errors + one nit vs one error + two nits is a
|
||||
* reported difference even though the severity sets and totals match).
|
||||
*/
|
||||
const IDENTITY_PREFIX_CHARS = 80;
|
||||
|
||||
interface DiffIdentityRecord {
|
||||
kind: string;
|
||||
phase: string;
|
||||
/** Redacted message of the first occurrence (display sample). */
|
||||
message: string;
|
||||
count: number;
|
||||
/** Unique severities, sorted (display); counts live in severity_counts. */
|
||||
severities: string[];
|
||||
severity_counts: Record<string, number>;
|
||||
}
|
||||
|
||||
interface DiffChangedRecord {
|
||||
kind: string;
|
||||
phase: string;
|
||||
message: string;
|
||||
base_count: number;
|
||||
compare_count: number;
|
||||
base_severities: string[];
|
||||
compare_severities: string[];
|
||||
base_severity_counts: Record<string, number>;
|
||||
compare_severity_counts: Record<string, number>;
|
||||
count_changed: boolean;
|
||||
severity_changed: boolean;
|
||||
}
|
||||
|
||||
interface DiffRunBanner {
|
||||
run_id: string;
|
||||
agent?: string;
|
||||
scenario?: string;
|
||||
gbrain_version?: string;
|
||||
interrupted: boolean;
|
||||
/** Malformed JSONL lines skipped by the reader (surfaced, never hidden). */
|
||||
malformed: number;
|
||||
}
|
||||
|
||||
export interface FrictionDiffResult {
|
||||
base: string;
|
||||
compare: string;
|
||||
banner: { base: DiffRunBanner; compare: DiffRunBanner; warnings: string[] };
|
||||
unique_to_base: DiffIdentityRecord[];
|
||||
unique_to_compare: DiffIdentityRecord[];
|
||||
changed: DiffChangedRecord[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a run spec: an exact run-id wins; otherwise treat the spec as an
|
||||
* agent name and pick the LATEST run (listRuns is mtime-sorted, newest first)
|
||||
* whose entries carry that agent — the run-start marker stamps agent on clean
|
||||
* runs, and any agent-stamped entry also counts. Returns undefined if nothing
|
||||
* matches.
|
||||
*/
|
||||
export function resolveRunSpec(spec: string): string | undefined {
|
||||
try {
|
||||
if (existsSync(frictionFile(spec))) return spec;
|
||||
} catch { /* spec has characters a run-id can't; fall through to agent-name resolution */ }
|
||||
for (const run of listRuns()) {
|
||||
try {
|
||||
const { entries } = readFriction(run.runId);
|
||||
if (entries.some(e => e.agent === spec)) return run.runId;
|
||||
} catch { /* unreadable file; skip */ }
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function normalizeForIdentity(message: string): string {
|
||||
return message.toLowerCase().replace(/\d+/g, '#').replace(/\s+/g, ' ').trim().slice(0, IDENTITY_PREFIX_CHARS);
|
||||
}
|
||||
|
||||
interface IdentityAccum { kind: string; phase: string; message: string; count: number; severities: Map<string, number> }
|
||||
|
||||
/** Sorted-key plain object from a severity count map (deterministic JSON). */
|
||||
function severityCounts(m: Map<string, number>): Record<string, number> {
|
||||
return Object.fromEntries([...m.entries()].sort(([a], [b]) => a.localeCompare(b)));
|
||||
}
|
||||
|
||||
/** Diff operates ONLY on kind friction|delight; markers/interruptions feed the banner. */
|
||||
function collectIdentities(entries: FrictionEntry[]): Map<string, IdentityAccum> {
|
||||
const map = new Map<string, IdentityAccum>();
|
||||
for (const raw of entries) {
|
||||
// Entries from older writers may omit kind; they are friction by contract.
|
||||
const kind = raw.kind ?? 'friction';
|
||||
if (kind !== 'friction' && kind !== 'delight') continue;
|
||||
const e = redactEntry(raw);
|
||||
const key = `${kind}\u0000${e.phase}\u0000${normalizeForIdentity(e.message)}`;
|
||||
let acc = map.get(key);
|
||||
if (!acc) {
|
||||
acc = { kind, phase: e.phase, message: e.message, count: 0, severities: new Map() };
|
||||
map.set(key, acc);
|
||||
}
|
||||
acc.count++;
|
||||
if (e.severity) acc.severities.set(e.severity, (acc.severities.get(e.severity) ?? 0) + 1);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
function toIdentityRecord(acc: IdentityAccum): DiffIdentityRecord {
|
||||
return {
|
||||
kind: acc.kind,
|
||||
phase: acc.phase,
|
||||
message: acc.message,
|
||||
count: acc.count,
|
||||
severities: [...acc.severities.keys()].sort(),
|
||||
severity_counts: severityCounts(acc.severities),
|
||||
};
|
||||
}
|
||||
|
||||
function bannerFor(runId: string, read: ReadResult): DiffRunBanner {
|
||||
const start = read.entries.find(e => e.kind === 'phase-marker' && e.marker === 'start');
|
||||
const agent = start?.agent ?? read.entries.find(e => e.agent)?.agent;
|
||||
return {
|
||||
run_id: runId,
|
||||
agent,
|
||||
scenario: start?.scenario,
|
||||
gbrain_version: start?.gbrain_version ?? read.entries[0]?.gbrain_version,
|
||||
interrupted: read.entries.some(e => e.kind === 'interrupted'),
|
||||
malformed: read.malformed,
|
||||
};
|
||||
}
|
||||
|
||||
/** Compute the diff between two resolved run-ids. Throws on read errors. */
|
||||
export function computeFrictionDiff(baseRunId: string, compareRunId: string): FrictionDiffResult {
|
||||
const baseRead = readFriction(baseRunId);
|
||||
const compareRead = readFriction(compareRunId);
|
||||
const baseBanner = bannerFor(baseRunId, baseRead);
|
||||
const compareBanner = bannerFor(compareRunId, compareRead);
|
||||
|
||||
const warnings: string[] = [];
|
||||
if ((baseBanner.scenario ?? '') !== (compareBanner.scenario ?? '')) {
|
||||
warnings.push(`scenario differs: ${baseRunId} ran ${baseBanner.scenario ?? '(unknown)'}, ${compareRunId} ran ${compareBanner.scenario ?? '(unknown)'} — entries may not be comparable`);
|
||||
}
|
||||
if ((baseBanner.gbrain_version ?? '') !== (compareBanner.gbrain_version ?? '')) {
|
||||
warnings.push(`gbrain version differs: ${baseRunId} ran ${baseBanner.gbrain_version ?? '(unknown)'}, ${compareRunId} ran ${compareBanner.gbrain_version ?? '(unknown)'}`);
|
||||
}
|
||||
|
||||
const baseIds = collectIdentities(baseRead.entries);
|
||||
const compareIds = collectIdentities(compareRead.entries);
|
||||
const uniqueToBase: DiffIdentityRecord[] = [];
|
||||
const uniqueToCompare: DiffIdentityRecord[] = [];
|
||||
const changed: DiffChangedRecord[] = [];
|
||||
for (const [key, b] of baseIds) {
|
||||
const c = compareIds.get(key);
|
||||
if (!c) { uniqueToBase.push(toIdentityRecord(b)); continue; }
|
||||
const bCounts = severityCounts(b.severities);
|
||||
const cCounts = severityCounts(c.severities);
|
||||
const countChanged = b.count !== c.count;
|
||||
// severity_changed = the per-severity DISTRIBUTION SHAPE changed — a new
|
||||
// severity appeared/disappeared or the mix redistributed (2×error+1×nit →
|
||||
// 1×error+2×nit, which the unique-severity set hides). Uniform scaling
|
||||
// (1×error → 10×error) is purely a count change and count_changed already
|
||||
// reports it. Integer cross-multiplication keeps the proportion test
|
||||
// exact.
|
||||
const bTotal = Object.values(bCounts).reduce((a, n) => a + n, 0);
|
||||
const cTotal = Object.values(cCounts).reduce((a, n) => a + n, 0);
|
||||
let severityChanged = (bTotal === 0) !== (cTotal === 0);
|
||||
if (!severityChanged) {
|
||||
for (const s of new Set([...Object.keys(bCounts), ...Object.keys(cCounts)])) {
|
||||
if ((bCounts[s] ?? 0) * cTotal !== (cCounts[s] ?? 0) * bTotal) { severityChanged = true; break; }
|
||||
}
|
||||
}
|
||||
if (countChanged || severityChanged) {
|
||||
changed.push({
|
||||
kind: b.kind, phase: b.phase, message: b.message,
|
||||
base_count: b.count, compare_count: c.count,
|
||||
base_severities: Object.keys(bCounts), compare_severities: Object.keys(cCounts),
|
||||
base_severity_counts: bCounts, compare_severity_counts: cCounts,
|
||||
count_changed: countChanged, severity_changed: severityChanged,
|
||||
});
|
||||
}
|
||||
}
|
||||
for (const [key, c] of compareIds) {
|
||||
if (!baseIds.has(key)) uniqueToCompare.push(toIdentityRecord(c));
|
||||
}
|
||||
const byPhaseThenMessage = (a: { phase: string; message: string }, b: { phase: string; message: string }) =>
|
||||
a.phase.localeCompare(b.phase) || a.message.localeCompare(b.message);
|
||||
uniqueToBase.sort(byPhaseThenMessage);
|
||||
uniqueToCompare.sort(byPhaseThenMessage);
|
||||
changed.sort(byPhaseThenMessage);
|
||||
|
||||
return {
|
||||
base: baseRunId,
|
||||
compare: compareRunId,
|
||||
banner: { base: baseBanner, compare: compareBanner, warnings },
|
||||
unique_to_base: uniqueToBase,
|
||||
unique_to_compare: uniqueToCompare,
|
||||
changed,
|
||||
};
|
||||
}
|
||||
|
||||
function groupByPhase<T extends { phase: string }>(records: T[]): Map<string, T[]> {
|
||||
const map = new Map<string, T[]>();
|
||||
for (const r of records) {
|
||||
if (!map.has(r.phase)) map.set(r.phase, []);
|
||||
map.get(r.phase)!.push(r);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
function fmtSeverityCounts(counts: Record<string, number>): string {
|
||||
const parts = Object.entries(counts).map(([s, n]) => (n > 1 ? `${s}×${n}` : s));
|
||||
return parts.length > 0 ? parts.join('/') : '(none)';
|
||||
}
|
||||
|
||||
/** Render the diff as markdown. Sections are labeled by run-id — the diff is
|
||||
* an instrument, not a judge; it never attributes blame to either side. */
|
||||
function renderDiff(diff: FrictionDiffResult): string {
|
||||
const lines: string[] = [];
|
||||
lines.push(`# Friction diff — base \`${diff.base}\` vs compare \`${diff.compare}\``);
|
||||
lines.push('');
|
||||
for (const side of [diff.banner.base, diff.banner.compare]) {
|
||||
const bits = [
|
||||
`agent=${side.agent ?? '(unknown)'}`,
|
||||
`scenario=${side.scenario ?? '(unknown)'}`,
|
||||
`gbrain=${side.gbrain_version ?? '(unknown)'}`,
|
||||
];
|
||||
if (side.interrupted) bits.push('interrupted');
|
||||
if (side.malformed > 0) bits.push(`${side.malformed} malformed line(s) skipped`);
|
||||
lines.push(`- \`${side.run_id}\`: ${bits.join(' · ')}`);
|
||||
}
|
||||
lines.push('');
|
||||
for (const w of diff.banner.warnings) lines.push(`> ⚠ WARN: ${w}`);
|
||||
if (diff.banner.warnings.length > 0) lines.push('');
|
||||
|
||||
if (diff.unique_to_base.length === 0 && diff.unique_to_compare.length === 0 && diff.changed.length === 0) {
|
||||
lines.push('No differences.');
|
||||
lines.push('');
|
||||
}
|
||||
|
||||
const renderIdentitySection = (title: string, records: DiffIdentityRecord[]) => {
|
||||
lines.push(`## ${title} (${records.length})`);
|
||||
lines.push('');
|
||||
if (records.length === 0) { lines.push('(none)'); lines.push(''); return; }
|
||||
for (const [phase, rs] of groupByPhase(records)) {
|
||||
lines.push(`### \`${phase}\``);
|
||||
lines.push('');
|
||||
for (const r of rs) {
|
||||
const sev = r.severities.length > 0 ? `[${r.severities.join('/')}] ` : '';
|
||||
const kind = r.kind === 'delight' ? '[delight] ' : '';
|
||||
const count = r.count > 1 ? ` ×${r.count}` : '';
|
||||
lines.push(`- ${kind}${sev}${r.message}${count}`);
|
||||
}
|
||||
lines.push('');
|
||||
}
|
||||
};
|
||||
renderIdentitySection(`Unique to \`${diff.compare}\``, diff.unique_to_compare);
|
||||
renderIdentitySection(`Unique to \`${diff.base}\``, diff.unique_to_base);
|
||||
|
||||
lines.push(`## Shared but changed (${diff.changed.length})`);
|
||||
lines.push('');
|
||||
if (diff.changed.length === 0) { lines.push('(none)'); lines.push(''); }
|
||||
for (const [phase, rs] of groupByPhase(diff.changed)) {
|
||||
lines.push(`### \`${phase}\``);
|
||||
lines.push('');
|
||||
for (const r of rs) {
|
||||
const deltas: string[] = [];
|
||||
if (r.severity_changed) deltas.push(`severity ${fmtSeverityCounts(r.base_severity_counts)} → ${fmtSeverityCounts(r.compare_severity_counts)}`);
|
||||
if (r.count_changed) deltas.push(`count ${r.base_count} → ${r.compare_count}`);
|
||||
const kind = r.kind === 'delight' ? '[delight] ' : '';
|
||||
lines.push(`- ${kind}${r.message} — ${deltas.join('; ')}`);
|
||||
}
|
||||
lines.push('');
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function cmdDiff(args: string[]): number {
|
||||
const flags = parseFlags(args);
|
||||
const baseSpec = flags.string('--base');
|
||||
const compareSpec = flags.string('--compare');
|
||||
if (!baseSpec || !compareSpec) {
|
||||
console.error('usage: gbrain friction diff --base <run-or-agent> --compare <run-or-agent> [--json]');
|
||||
return 2;
|
||||
}
|
||||
const baseRun = resolveRunSpec(baseSpec);
|
||||
const compareRun = resolveRunSpec(compareSpec);
|
||||
if (!baseRun || !compareRun) {
|
||||
const unresolved = [!baseRun ? baseSpec : undefined, !compareRun ? compareSpec : undefined]
|
||||
.filter((s): s is string => s !== undefined)
|
||||
.map(s => JSON.stringify(s)).join(', ');
|
||||
const runs = listRuns();
|
||||
const available = runs.length > 0 ? runs.map(r => ` ${r.runId}`).join('\n') : ' (none)';
|
||||
console.error(`friction diff failed: ${unresolved} matched no run-id or agent\navailable runs under ${frictionDir()}:\n${available}`);
|
||||
return 1;
|
||||
}
|
||||
try {
|
||||
const diff = computeFrictionDiff(baseRun, compareRun);
|
||||
process.stdout.write((flags.bool('--json') ? JSON.stringify(diff, null, 2) : renderDiff(diff)) + '\n');
|
||||
return 0;
|
||||
} catch (e) {
|
||||
console.error(`friction diff failed: ${e instanceof Error ? e.message : String(e)}`);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -174,12 +489,16 @@ Subcommands:
|
||||
render Render a run's entries as markdown (default) or JSON
|
||||
list List recent runs with friction/delight counts
|
||||
summary Two-column summary of friction + delight for a run
|
||||
diff Compare two runs: unique-to-each + shared-but-changed entries
|
||||
|
||||
Examples:
|
||||
gbrain friction log --severity confused --phase install --message "init didn't say which engine"
|
||||
gbrain friction render --run-id claw-test-20260428-... --transcripts
|
||||
gbrain friction list --json
|
||||
gbrain friction summary
|
||||
gbrain friction diff --base openclaw --compare hermes --json
|
||||
|
||||
Run-id resolution: --run-id > $GBRAIN_FRICTION_RUN_ID > 'standalone'.`);
|
||||
Run-id resolution: --run-id > $GBRAIN_FRICTION_RUN_ID > 'standalone'.
|
||||
Diff run resolution: an exact run-id wins; otherwise the value is treated as
|
||||
an agent name and resolves to that agent's latest run.`);
|
||||
}
|
||||
|
||||
@@ -5,15 +5,17 @@
|
||||
* config writes work [CDX-7]. Idempotent: if `search.mode` is already set
|
||||
* (re-init / second run), the picker is skipped entirely.
|
||||
*
|
||||
* TTY flow shows the menu. Non-TTY (CI, scripted init, --mcp-only) writes
|
||||
* `balanced` and prints the one-line hint pointing at `gbrain config set
|
||||
* search.mode`. The mode picker NEVER blocks an init run — readLineSafe
|
||||
* caps at 60s and falls back to `balanced` on timeout / EOF.
|
||||
* TTY flow shows the menu. Non-TTY (CI, scripted init, --mcp-only) applies
|
||||
* the auto-recommendation, prints the cost matrix + an [AGENT] directive to
|
||||
* confirm with the operator, and points at `gbrain config set search.mode`.
|
||||
* The mode picker NEVER blocks an init run — readLineSafe caps at 60s and
|
||||
* falls back to the recommendation on timeout / EOF.
|
||||
*
|
||||
* Smart auto-suggestion: reads models.tier.subagent / models.default /
|
||||
* OPENAI_API_KEY presence + brain size hint to RECOMMEND a mode. The
|
||||
* recommendation is informational only — the user picks. This is the
|
||||
* "agents perfectly tune for user needs" piece at install time.
|
||||
* expansion-capable key presence (Anthropic/OpenAI/Google) + brain size hint
|
||||
* to RECOMMEND a mode. The recommendation is informational only — the user
|
||||
* picks. This is the "agents perfectly tune for user needs" piece at
|
||||
* install time.
|
||||
*/
|
||||
|
||||
import type { BrainEngine } from '../core/engine.ts';
|
||||
@@ -36,8 +38,11 @@ export interface ModePickerInputs {
|
||||
subagentModel?: string | null;
|
||||
/** Configured default model id. */
|
||||
defaultModel?: string | null;
|
||||
/** True iff an OpenAI API key is configured. */
|
||||
hasOpenAIKey?: boolean;
|
||||
/** True iff an expansion-capable API key (Anthropic / OpenAI / Google) is
|
||||
* configured. LLM query expansion routes through the gateway's chat lane,
|
||||
* not the embedding lane — an OpenAI-only gate wrongly told Anthropic-keyed
|
||||
* installs "no LLM expansion possible". */
|
||||
hasExpansionKey?: boolean;
|
||||
/** Approximate page count of the brain (after initSchema, before bulk import). */
|
||||
pageCount?: number;
|
||||
}
|
||||
@@ -50,8 +55,8 @@ export interface ModePickerInputs {
|
||||
* shape per the v0.32.3 install-picker directive):
|
||||
* - Opus / Frontier model OR Sonnet / unknown → tokenmax (max-quality default)
|
||||
* - Haiku subagent → conservative (cost-sensitive setups)
|
||||
* - No OpenAI key configured → conservative (LLM expansion not possible
|
||||
* anyway, so tight budget makes more sense)
|
||||
* - No expansion-capable key (Anthropic/OpenAI/Google) → conservative
|
||||
* (LLM expansion cannot run anyway, so tight budget makes more sense)
|
||||
*
|
||||
* Rationale: the previous "default to balanced unless Opus detected" logic
|
||||
* silently downgraded users who were running Sonnet-tier work and expected
|
||||
@@ -67,10 +72,10 @@ export function recommendModeFor(inputs: ModePickerInputs): { mode: SearchMode;
|
||||
reason: 'Haiku subagent tier detected — tight 4K budget keeps per-call cost down.',
|
||||
};
|
||||
}
|
||||
if (inputs.hasOpenAIKey === false) {
|
||||
if (inputs.hasExpansionKey === false) {
|
||||
return {
|
||||
mode: 'conservative',
|
||||
reason: 'No OpenAI key configured — semantic cache still works, but no LLM expansion possible.',
|
||||
reason: 'No expansion-capable API key (Anthropic/OpenAI/Google) — semantic cache still works, but LLM query expansion cannot run.',
|
||||
};
|
||||
}
|
||||
const opus = /opus/i.test(inputs.defaultModel ?? '') || /opus/i.test(inputs.subagentModel ?? '');
|
||||
@@ -109,7 +114,12 @@ async function resolveInputs(engine: BrainEngine): Promise<ModePickerInputs> {
|
||||
return {
|
||||
subagentModel,
|
||||
defaultModel,
|
||||
hasOpenAIKey: Boolean(process.env.OPENAI_API_KEY),
|
||||
hasExpansionKey: Boolean(
|
||||
process.env.ANTHROPIC_API_KEY ||
|
||||
process.env.OPENAI_API_KEY ||
|
||||
process.env.GOOGLE_GENERATIVE_AI_API_KEY ||
|
||||
process.env.GEMINI_API_KEY, // gateway accepts GEMINI_API_KEY as a first-class alias
|
||||
),
|
||||
pageCount,
|
||||
};
|
||||
}
|
||||
@@ -214,7 +224,9 @@ export async function runModePicker(
|
||||
// 2 stderr lines that agents typically miss / don't relay to the human.
|
||||
// Now: the agent sees the same matrix the human-TTY picker shows, plus
|
||||
// a directive saying "show this to your operator and confirm before
|
||||
// moving on." Default-applied mode is tokenmax (preserves v0.31.x shape).
|
||||
// moving on." The applied default is the recommendation above — the
|
||||
// banner interpolates it (a static "preserves the v0.31.x shape" line
|
||||
// used to claim tokenmax semantics even when conservative was applied).
|
||||
if (!process.stdin.isTTY) {
|
||||
try { await engine.setConfig(SEARCH_MODE_KEY, rec.mode); } catch { /* swallow */ }
|
||||
console.log('');
|
||||
@@ -224,10 +236,10 @@ export async function runModePicker(
|
||||
console.log('═══════════════════════════════════════════════════════════════');
|
||||
console.log('');
|
||||
console.log('[AGENT] Before continuing, SHOW this matrix to your operator and');
|
||||
console.log('[AGENT] ask which mode they want. The default above preserves the');
|
||||
console.log('[AGENT] v0.31.x retrieval shape. Run `gbrain config set search.mode');
|
||||
console.log('[AGENT] <conservative|balanced|tokenmax>` if the operator picks');
|
||||
console.log('[AGENT] differently. See INSTALL_FOR_AGENTS.md Step 3.5 for the');
|
||||
console.log(`[AGENT] ask which mode they want. The default above (${rec.mode}) was`);
|
||||
console.log('[AGENT] auto-selected from the Reason line. Run `gbrain config set');
|
||||
console.log('[AGENT] search.mode <conservative|balanced|tokenmax>` if the operator');
|
||||
console.log('[AGENT] picks differently. See INSTALL_FOR_AGENTS.md Step 3.5 for the');
|
||||
console.log('[AGENT] full protocol.');
|
||||
console.log('');
|
||||
console.log('Per-query cost @ 10K queries/mo (search payload only, no cache savings):');
|
||||
|
||||
@@ -8,13 +8,22 @@
|
||||
* picker's UI and `gbrain providers list` can't drift.
|
||||
*
|
||||
* Trust contract:
|
||||
* - TTY-only. Callers must not invoke this in non-TTY contexts; D3 says
|
||||
* non-TTY with zero keys exits 1 from `resolveAIOptions` before we
|
||||
* reach here. A defensive guard returns null if no TTY anyway.
|
||||
* - Filters candidates to env-ready recipes (codex finding #3). The
|
||||
* picker is for choosing among providers the user CAN run, not for
|
||||
* walking them through key setup.
|
||||
* - On Ctrl-D / EOF / timeout: returns null, caller treats as exit 1.
|
||||
* - TTY-only. Callers must not invoke this in non-TTY contexts (non-TTY
|
||||
* zero-key resolves keyless in `resolveEmbeddingByEnv` before reaching
|
||||
* here). A defensive guard returns null if no TTY anyway.
|
||||
* - Filters candidates to env-ready recipes (codex finding #3), and
|
||||
* probe-gates LOCAL daemons (ollama): daemon-up ≠ model-pulled, so an
|
||||
* unreachable daemon is dropped and a missing model is annotated with
|
||||
* its `ollama pull` fix inline.
|
||||
* - Embedding pickers always offer `0) none — continue keyless`. When no
|
||||
* KEYED provider is ready, keyless is the DEFAULT (bare Enter / 60s
|
||||
* timeout / EOF all resolve to 0 → null), so a local daemon is never
|
||||
* auto-selected. When a keyed provider IS ready the default is `1`, so an
|
||||
* unattended timeout picks that first keyed provider — NOT null; explicit
|
||||
* `0` is still keyless.
|
||||
* - Returns null on the keyless choice (and on invalid input); the embedding
|
||||
* caller continues keyless with a loud notice on BOTH the zero-key and the
|
||||
* multi-key paths (other touchpoints treat null as no-pick).
|
||||
* - When the user picks a non-Anthropic chat-capable recipe AND
|
||||
* `ANTHROPIC_API_KEY` is missing, prints the subagent caveat from D7
|
||||
* BEFORE returning the choice so the user sees the implication.
|
||||
@@ -23,6 +32,7 @@
|
||||
import { listRecipes } from '../core/ai/recipes/index.ts';
|
||||
import { envReady, formatRecipeTable } from './providers.ts';
|
||||
import { readLineSafe } from './init.ts';
|
||||
import { probeOllama, type ProbeResult } from '../core/ai/probes.ts';
|
||||
import type { Recipe } from '../core/ai/types.ts';
|
||||
|
||||
export interface PickedProvider {
|
||||
@@ -46,6 +56,10 @@ export interface PickProviderOpts {
|
||||
isTTY?: boolean;
|
||||
/** Stderr override for tests (capturing prompts). Defaults to process.stderr.write. */
|
||||
writeStderr?: (s: string) => void;
|
||||
/** Local-daemon probe seam (injected for tests; defaults to probeOllama).
|
||||
* Keeps the unit suite off the network — and off any REAL ollama that
|
||||
* happens to be running on the test machine. */
|
||||
probeLocal?: () => Promise<ProbeResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,19 +121,57 @@ export async function pickProvider(opts: PickProviderOpts): Promise<PickedProvid
|
||||
}
|
||||
|
||||
const all = listRecipes();
|
||||
const ready = readyRecipesForTouchpoint(all, opts.touchpoint, env);
|
||||
let ready = readyRecipesForTouchpoint(all, opts.touchpoint, env);
|
||||
|
||||
if (ready.length === 0) {
|
||||
// Probe-gate the ollama daemon: `envReady` treats no-key-required as
|
||||
// ready, but daemon-up ≠ model-pulled — the exact trap that let a keyless
|
||||
// Enter "choose" a broken ollama config and continue silently degraded.
|
||||
// Drop ollama when its daemon doesn't answer; annotate it when the daemon
|
||||
// answers but hasn't pulled the recipe's model. Scoped to ollama — other
|
||||
// local recipes (claude-cli) have no daemon to probe.
|
||||
const localHints = new Map<string, string>();
|
||||
const localRecipes = ready.filter((r) => r.id === 'ollama');
|
||||
if (localRecipes.length > 0) {
|
||||
const probe = opts.probeLocal ?? probeOllama;
|
||||
let probeResult: ProbeResult;
|
||||
try {
|
||||
probeResult = await probe();
|
||||
} catch {
|
||||
probeResult = { reachable: false };
|
||||
}
|
||||
if (!probeResult.models_endpoint_valid) {
|
||||
ready = ready.filter((r) => r.id !== 'ollama');
|
||||
} else {
|
||||
for (const r of localRecipes) {
|
||||
const tp = r.touchpoints[opts.touchpoint];
|
||||
const wanted = tp && 'models' in tp && Array.isArray(tp.models) ? tp.models[0] : undefined;
|
||||
const served = probeResult.models ?? [];
|
||||
if (wanted && !served.some((m) => m === wanted || m.startsWith(`${wanted}:`))) {
|
||||
localHints.set(r.id, `model not pulled — run: ollama pull ${wanted}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keyless is always a valid embedding choice — the brain works with
|
||||
// keyword search + agent-authored memory. Offer it explicitly instead of
|
||||
// forcing a keypress through a provider menu.
|
||||
const keylessOption = opts.touchpoint === 'embedding';
|
||||
|
||||
if (ready.length === 0 && !keylessOption) {
|
||||
writeStderr(`\nNo ${opts.touchpoint}-capable providers are env-ready.\n`);
|
||||
writeStderr('Set one of the env vars below and re-run init:\n\n');
|
||||
writeStderr(formatRecipeTable(all, env) + '\n\n');
|
||||
return null;
|
||||
}
|
||||
|
||||
writeStderr(`\nPick a ${opts.touchpoint} provider (env-ready providers shown):\n\n`);
|
||||
writeStderr(formatRecipeTable(ready, env) + '\n\n');
|
||||
// Article-aware: touchpoint is 'embedding' | 'expansion' | 'chat' — a
|
||||
// hardcoded article renders "an chat provider".
|
||||
const article = /^[aeiou]/i.test(opts.touchpoint) ? 'an' : 'a';
|
||||
writeStderr(`\nPick ${article} ${opts.touchpoint} provider (env-ready providers shown):\n\n`);
|
||||
if (ready.length > 0) writeStderr(formatRecipeTable(ready, env) + '\n\n');
|
||||
|
||||
// Build numbered options
|
||||
// Build numbered options (0 = keyless skip for embedding).
|
||||
const lines = ready.map((r, i) => {
|
||||
const tp = r.touchpoints[opts.touchpoint];
|
||||
let label = ` ${i + 1}) ${r.id}`;
|
||||
@@ -129,21 +181,35 @@ export async function pickProvider(opts: PickProviderOpts): Promise<PickedProvid
|
||||
if (tp && 'models' in tp && Array.isArray(tp.models) && tp.models.length > 0) {
|
||||
label += ` ${tp.models[0]}`;
|
||||
}
|
||||
const hint = localHints.get(r.id);
|
||||
if (hint) label += ` [${hint}]`;
|
||||
return label;
|
||||
});
|
||||
if (keylessOption) {
|
||||
lines.unshift(' 0) none — continue keyless (keyword search; add a key later)');
|
||||
}
|
||||
writeStderr(lines.join('\n') + '\n\n');
|
||||
|
||||
// Default: keyless when no remote (keyed) provider is ready — a bare Enter
|
||||
// must never select a local daemon the user didn't ask for.
|
||||
const hasKeyedReady = ready.some((r) => (r.auth_env?.required ?? []).length > 0);
|
||||
const defaultChoice = keylessOption && !hasKeyedReady ? '0' : '1';
|
||||
const low = keylessOption ? 0 : 1;
|
||||
|
||||
const answer = await readLineSafe(
|
||||
`Choice [1-${ready.length}, default 1]: `,
|
||||
'1',
|
||||
`Choice [${low}-${ready.length}, default ${defaultChoice}]: `,
|
||||
defaultChoice,
|
||||
/* timeoutMs */ 60_000,
|
||||
);
|
||||
|
||||
const choice = parseInt(answer.trim(), 10);
|
||||
if (!Number.isFinite(choice) || choice < 1 || choice > ready.length) {
|
||||
writeStderr(`\nInvalid choice "${answer}". Aborting.\n`);
|
||||
if (!Number.isFinite(choice) || choice < low || choice > ready.length) {
|
||||
writeStderr(`\nInvalid choice "${answer}".\n`);
|
||||
return null;
|
||||
}
|
||||
if (keylessOption && choice === 0) {
|
||||
return null; // caller continues keyless with its own notice
|
||||
}
|
||||
|
||||
const picked = ready[choice - 1];
|
||||
const tp = picked.touchpoints[opts.touchpoint];
|
||||
|
||||
+126
-51
@@ -491,9 +491,16 @@ export async function findEnvKeyTypos(
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Emit the fail-loud "no embedding provider" message + paste-ready setup. */
|
||||
/** Emit the "no embedding provider" message + paste-ready setup. Keyless
|
||||
* continue leads (it always works); key setup follows for the upgrade. */
|
||||
function printNoEmbeddingProviderHint(typos: Array<{ userSet: string; suggested: string }>): void {
|
||||
console.error('\nNo embedding provider configured. Set one of:');
|
||||
console.error('\nNo embedding provider configured.');
|
||||
console.error('Continue without one (keyless — keyword search + memory your agent writes):');
|
||||
console.error(' gbrain init --pglite --no-embedding');
|
||||
console.error(' (enable semantic search later by re-running with a key:');
|
||||
console.error(' gbrain init --force --pglite --embedding-model <id>)');
|
||||
console.error('');
|
||||
console.error('Or set a key for semantic search:');
|
||||
console.error(' export OPENAI_API_KEY=sk-… # openai:text-embedding-3-large (1536d)');
|
||||
console.error(' export ZEROENTROPY_API_KEY=ze-… # zeroentropyai:zembed-1 (2560d, Matryoshka)');
|
||||
console.error(' export VOYAGE_API_KEY=pa-… # voyage:voyage-3-large (1024d)');
|
||||
@@ -501,9 +508,6 @@ function printNoEmbeddingProviderHint(typos: Array<{ userSet: string; suggested:
|
||||
console.error('');
|
||||
console.error('Or pick explicitly:');
|
||||
console.error(' gbrain init --pglite --embedding-model openai:text-embedding-3-large');
|
||||
console.error('');
|
||||
console.error('Or defer setup: gbrain init --pglite --no-embedding');
|
||||
console.error(' (you can configure later with `gbrain config set embedding_model <id>`)');
|
||||
// D13: surface near-miss env vars (e.g. OPENAPI_API_KEY → OPENAI_API_KEY).
|
||||
if (typos.length > 0) {
|
||||
console.error('');
|
||||
@@ -513,6 +517,19 @@ function printNoEmbeddingProviderHint(typos: Array<{ userSet: string; suggested:
|
||||
}
|
||||
}
|
||||
|
||||
/** Loud keyless-continue notice for the no-keys default path. The upgrade
|
||||
* command is `init --force` re-init, NOT `config set embedding_model` — that
|
||||
* key is a schema-sizing file-plane field that `gbrain config set` refuses
|
||||
* (it would be a silent no-op), so pointing users there is a dead end. */
|
||||
function printKeylessContinueNotice(): void {
|
||||
console.error(
|
||||
'No embedding provider keys detected — continuing in keyless mode:\n' +
|
||||
' keyword search + memory your agent writes down itself. Everything works.\n' +
|
||||
' One optional key upgrades search to semantic — set the key, then re-run\n' +
|
||||
' `gbrain init --force --pglite --embedding-model <id>` (re-imports via `gbrain sync`).',
|
||||
);
|
||||
}
|
||||
|
||||
async function resolveEmbeddingByEnv(out: ResolvedAIOptions, nonInteractive: boolean): Promise<void> {
|
||||
const ready = await groupReadyByProvider('embedding');
|
||||
const isTTY = !nonInteractive && !!process.stdin.isTTY;
|
||||
@@ -548,28 +565,56 @@ async function resolveEmbeddingByEnv(out: ResolvedAIOptions, nonInteractive: boo
|
||||
}
|
||||
}
|
||||
|
||||
// Zero or multi — pick or fail loud.
|
||||
// Zero keys — keyless is a first-class posture (the whole paste-in
|
||||
// bootstrap runs on it), so the DEFAULT is to continue keyless with a loud
|
||||
// notice, not exit 1. Fail-loud survives in exactly one zero-key case: a
|
||||
// near-miss env var (OPENAPI_API_KEY → OPENAI_API_KEY) signals the user
|
||||
// MEANT to configure a key — completing keyless there would silently bury
|
||||
// their typo.
|
||||
if (ready.length === 0) {
|
||||
if (!isTTY) {
|
||||
const typos = await findEnvKeyTypos();
|
||||
const typos = await findEnvKeyTypos();
|
||||
if (typos.length > 0) {
|
||||
printNoEmbeddingProviderHint(typos);
|
||||
process.exit(1);
|
||||
}
|
||||
// TTY → picker; on null (user aborted) still fail loud.
|
||||
if (!isTTY) {
|
||||
printKeylessContinueNotice();
|
||||
out.noEmbedding = true;
|
||||
return;
|
||||
}
|
||||
// TTY → picker (local providers like ollama may be selectable); a null
|
||||
// pick (nothing offered, user skipped, or EOF) continues keyless.
|
||||
const { pickProvider } = await import('./init-provider-picker.ts');
|
||||
const picked = await pickProvider({ touchpoint: 'embedding', env: process.env, isTTY: true });
|
||||
if (!picked) {
|
||||
const typos = await findEnvKeyTypos();
|
||||
printNoEmbeddingProviderHint(typos);
|
||||
process.exit(1);
|
||||
printKeylessContinueNotice();
|
||||
out.noEmbedding = true;
|
||||
return;
|
||||
}
|
||||
out.embedding_model = picked.fullModel;
|
||||
out.embedding_dimensions = picked.dim;
|
||||
return;
|
||||
}
|
||||
|
||||
// ready.length > 1 — picker (TTY) or fail-loud (non-TTY) per D2/D3.
|
||||
// ready.length > 1 — picker (TTY); non-TTY auto-picks the canonical default
|
||||
// when its key is present (the most common agent/dev setup is 2+ provider
|
||||
// keys — failing there blocked scripted installs), else fail-loud per D2/D3
|
||||
// (a genuinely ambiguous set with no canonical candidate stays explicit).
|
||||
if (!isTTY) {
|
||||
const { DEFAULT_EMBEDDING_MODEL, DEFAULT_EMBEDDING_DIMENSIONS } =
|
||||
await import('../core/ai/defaults.ts');
|
||||
const canonicalProvider = DEFAULT_EMBEDDING_MODEL.split(':')[0];
|
||||
const canonical = ready.find((p) => p.recipeId === canonicalProvider);
|
||||
if (canonical) {
|
||||
out.embedding_model = DEFAULT_EMBEDDING_MODEL;
|
||||
out.embedding_dimensions = DEFAULT_EMBEDDING_DIMENSIONS;
|
||||
console.error(
|
||||
`Multiple embedding providers env-ready (${ready.map(p => p.recipeId).join(', ')}). ` +
|
||||
`Using the default ${DEFAULT_EMBEDDING_MODEL} (${DEFAULT_EMBEDDING_DIMENSIONS}d). ` +
|
||||
`Override with --embedding-model.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
console.error(`Multiple embedding providers env-ready: ${ready.map(p => p.recipeId).join(', ')}.`);
|
||||
console.error(`Disambiguate by passing --embedding-model <provider>:<model>, or unset extra env vars.`);
|
||||
process.exit(1);
|
||||
@@ -577,8 +622,13 @@ async function resolveEmbeddingByEnv(out: ResolvedAIOptions, nonInteractive: boo
|
||||
const { pickProvider } = await import('./init-provider-picker.ts');
|
||||
const picked = await pickProvider({ touchpoint: 'embedding', env: process.env, isTTY: true });
|
||||
if (!picked) {
|
||||
console.error('Init aborted: no embedding provider picked.');
|
||||
process.exit(1);
|
||||
// The embedding picker offers an explicit "0) none — continue keyless"
|
||||
// option (and returns null on it). Honor that instead of aborting: a user
|
||||
// with multiple keys who deliberately chose keyless gets keyless, matching
|
||||
// the zero-key path. (Ctrl-D / EOF / invalid also land here → keyless.)
|
||||
printKeylessContinueNotice();
|
||||
out.noEmbedding = true;
|
||||
return;
|
||||
}
|
||||
out.embedding_model = picked.fullModel;
|
||||
out.embedding_dimensions = picked.dim;
|
||||
@@ -1080,18 +1130,16 @@ async function initPGLite(opts: {
|
||||
} else {
|
||||
console.log(`\nBrain ready at ${dbPath}`);
|
||||
console.log(`${stats.page_count} pages. Engine: PGLite (local Postgres).`);
|
||||
// Reference/status blocks print FIRST and terse; the ONE primary action
|
||||
// (the memory demo) prints LAST so it is the final, unmistakable thing on
|
||||
// screen. Krug: one obvious next action, everything else subordinate.
|
||||
if (stats.page_count > 0) {
|
||||
console.log('');
|
||||
console.log('Existing brain detected. To wire up the v0.10.3 knowledge graph:');
|
||||
console.log('Existing brain detected. Wire up the knowledge graph:');
|
||||
console.log(' gbrain extract links --source db (typed link backfill)');
|
||||
console.log(' gbrain extract timeline --source db (structured timeline backfill)');
|
||||
console.log(' gbrain stats (verify links > 0)');
|
||||
} else {
|
||||
console.log('Next: gbrain import <dir>');
|
||||
}
|
||||
printMemoryVerbsQuickstart();
|
||||
console.log('');
|
||||
console.log('When you outgrow local: gbrain migrate --to supabase');
|
||||
reportModStatus();
|
||||
const { printAdvisoryIfRecommended } = await import('../core/skillpack/post-install-advisory.ts');
|
||||
const { VERSION } = await import('../version.ts');
|
||||
@@ -1101,6 +1149,9 @@ async function initPGLite(opts: {
|
||||
// Fail-open; 3s wallclock cap. Skipped silently in non-TTY contexts.
|
||||
const { runInitNudge } = await import('../core/onboard/init-nudge.ts');
|
||||
await runInitNudge(engine);
|
||||
|
||||
// The single primary action, last-on-screen.
|
||||
printMemoryVerbsQuickstart({ emptyBrain: stats.page_count === 0, onPglite: true });
|
||||
}
|
||||
} finally {
|
||||
try { await engine.disconnect(); } catch { /* best-effort */ }
|
||||
@@ -1108,23 +1159,33 @@ async function initPGLite(opts: {
|
||||
}
|
||||
|
||||
/**
|
||||
* MEMORY_VERBS v1 quickstart funnel (E3 + D4B + T1 consent). Printed at the
|
||||
* end of both init epilogues. The copy-next block is EXACTLY three commands
|
||||
* (codex DX 9): wire the harness, write a memory, prove the resurrection.
|
||||
* The demo uses the facts arm only, so it works with NO embedding key [F-B].
|
||||
* MEMORY_VERBS v1 quickstart funnel (E3 + D4B + T1 consent). Printed LAST in
|
||||
* both init epilogues as the ONE primary action. The copy-next block is
|
||||
* EXACTLY three commands (codex DX 9): wire the harness, write a memory, prove
|
||||
* the resurrection. The demo uses the facts arm only, so it works with NO
|
||||
* embedding key [F-B]. Secondary paths (import, migrate) ride a single terse
|
||||
* "More:" footer so they never compete with the primary action.
|
||||
*/
|
||||
function printMemoryVerbsQuickstart(): void {
|
||||
function printMemoryVerbsQuickstart(opts: { emptyBrain?: boolean; onPglite?: boolean } = {}): void {
|
||||
console.log('');
|
||||
console.log('Give your agent memory (copy these three commands):');
|
||||
console.log('→ Do this next — give your agent memory (copy these three commands):');
|
||||
console.log(' claude mcp add gbrain -- gbrain serve --surface verbs');
|
||||
console.log(' gbrain remember "I prefer dark mode in every editor" --provenance demo --entity people/me');
|
||||
console.log(' gbrain recall --entity people/me');
|
||||
console.log('Now ask your agent in a NEW session — it remembers.');
|
||||
console.log('Then ask your agent in a NEW session — it remembers.');
|
||||
console.log('');
|
||||
console.log('Note: memories agents save are readable by every agent connected to');
|
||||
console.log('this brain; use visibility:"private" for local-only facts.');
|
||||
console.log('Other harnesses (Codex, OpenClaw): docs/protocol/MEMORY_VERBS_v1.md');
|
||||
console.log('If `claude` is not found: install Claude Code first, or use the per-harness blocks in that doc.');
|
||||
// Secondary paths, one line, clearly subordinate to the action above.
|
||||
console.log('');
|
||||
console.log(
|
||||
'More: ' +
|
||||
(opts.emptyBrain ? 'bulk-load notes `gbrain import <dir>` · ' : '') +
|
||||
(opts.onPglite ? 'scale up `gbrain migrate --to supabase` · ' : '') +
|
||||
'health `gbrain doctor`',
|
||||
);
|
||||
}
|
||||
|
||||
async function initPostgres(opts: {
|
||||
@@ -1350,14 +1411,11 @@ async function initPostgres(opts: {
|
||||
console.log(`\nBrain ready. ${stats.page_count} pages. Engine: Postgres (Supabase).`);
|
||||
if (stats.page_count > 0) {
|
||||
console.log('');
|
||||
console.log('Existing brain detected. To wire up the v0.10.3 knowledge graph:');
|
||||
console.log('Existing brain detected. Wire up the knowledge graph:');
|
||||
console.log(' gbrain extract links --source db (typed link backfill)');
|
||||
console.log(' gbrain extract timeline --source db (structured timeline backfill)');
|
||||
console.log(' gbrain stats (verify links > 0)');
|
||||
} else {
|
||||
console.log('Next: gbrain import <dir>');
|
||||
}
|
||||
printMemoryVerbsQuickstart();
|
||||
reportModStatus();
|
||||
const { printAdvisoryIfRecommended } = await import('../core/skillpack/post-install-advisory.ts');
|
||||
const { VERSION } = await import('../version.ts');
|
||||
@@ -1367,6 +1425,9 @@ async function initPostgres(opts: {
|
||||
// Fail-open; 3s wallclock cap. Skipped silently in non-TTY contexts.
|
||||
const { runInitNudge } = await import('../core/onboard/init-nudge.ts');
|
||||
await runInitNudge(engine);
|
||||
|
||||
// The single primary action, last-on-screen.
|
||||
printMemoryVerbsQuickstart({ emptyBrain: stats.page_count === 0 });
|
||||
}
|
||||
} finally {
|
||||
try { await engine.disconnect(); } catch { /* best-effort */ }
|
||||
@@ -1402,6 +1463,15 @@ function countMarkdownFiles(dir: string, maxScan = 1500): number {
|
||||
}
|
||||
|
||||
async function supabaseWizard(): Promise<string> {
|
||||
// Non-TTY guard: without a terminal the URL prompt below can never be
|
||||
// answered — the legacy behavior was a silent exit-0 no-op (stdin closed →
|
||||
// readLine never resolved data → process ended with NO config written), the
|
||||
// worst failure shape for a scripted/agent caller. Fail loud with the fix.
|
||||
if (!process.stdin.isTTY) {
|
||||
console.error('gbrain init --supabase needs an interactive terminal to prompt for the connection URL.');
|
||||
console.error('Non-interactive: pass --url <connection_string>, or set GBRAIN_DATABASE_URL and use --non-interactive.');
|
||||
process.exit(1);
|
||||
}
|
||||
try {
|
||||
execSync('bunx supabase --version', { stdio: 'pipe' });
|
||||
console.log('Supabase CLI detected.');
|
||||
@@ -1427,12 +1497,21 @@ function readLine(prompt: string): Promise<string> {
|
||||
return new Promise((resolve) => {
|
||||
process.stdout.write(prompt);
|
||||
let data = '';
|
||||
let settled = false;
|
||||
const settle = (value: string) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
process.stdin.pause();
|
||||
resolve(value);
|
||||
};
|
||||
process.stdin.setEncoding('utf-8');
|
||||
process.stdin.once('data', (chunk) => {
|
||||
data = chunk.toString().trim();
|
||||
process.stdin.pause();
|
||||
resolve(data);
|
||||
settle(data);
|
||||
});
|
||||
// EOF (Ctrl-D mid-prompt) resolves empty instead of hanging — the caller's
|
||||
// "No URL provided." guard then fails loud.
|
||||
process.stdin.once('end', () => settle(''));
|
||||
process.stdin.resume();
|
||||
});
|
||||
}
|
||||
@@ -1578,23 +1657,18 @@ export function reportModStatus(): void {
|
||||
skillCount = manifest.skills?.length || 0;
|
||||
} catch { /* manifest not found */ }
|
||||
|
||||
// One line per fact, one pointer per optional extra — this block sits on
|
||||
// the init success screen, where every extra call-to-action competes with
|
||||
// the memory-verbs funnel (the one action that matters). Krug: one screen,
|
||||
// one primary action.
|
||||
console.log('');
|
||||
console.log('--- GBrain Mod Status ---');
|
||||
console.log(`Skills: ${skillCount} loaded`);
|
||||
console.log(`GStack: ${gstack.found ? `found (${gstack.host})` : 'not found'}`);
|
||||
if (!gstack.found) {
|
||||
console.log(' Install GStack for coding skills:');
|
||||
console.log(' git clone https://github.com/garrytan/gstack.git ~/.claude/skills/gstack');
|
||||
console.log(' cd ~/.claude/skills/gstack && ./setup');
|
||||
}
|
||||
console.log('Resolver: skills/RESOLVER.md');
|
||||
console.log('Soul audit: ask your agent to "run a soul audit" to customize its identity (see skills/soul-audit)');
|
||||
console.log(`Skills: ${skillCount} loaded (router: skills/RESOLVER.md)`);
|
||||
console.log(`GStack: ${gstack.found ? `found (${gstack.host})` : 'not found (coding skills — see github.com/garrytan/gstack)'}`);
|
||||
// Retrieval Reflex (#1981): the deterministic pointer layer is ON by default
|
||||
// (no action needed). The policy skill is installed into the HOST repo on
|
||||
// request — we PRINT the command rather than silently mutating the host repo.
|
||||
console.log('Retrieval reflex: on by default (entity pointers injected per turn)');
|
||||
console.log(' Install the policy skill into your agent repo:');
|
||||
console.log(' gbrain integrations install retrieval-reflex --target <host-repo>');
|
||||
// request — we PRINT the pointer rather than silently mutating the host repo.
|
||||
console.log('Retrieval reflex: on by default. More: `gbrain integrations` (policy skill), skills/soul-audit (identity).');
|
||||
console.log('');
|
||||
}
|
||||
|
||||
@@ -1606,7 +1680,7 @@ USAGE
|
||||
gbrain init [flags]
|
||||
|
||||
ENGINE SELECTION (mutually exclusive)
|
||||
--pglite Use embedded PGLite (zero-config, default for <1000 .md files)
|
||||
--pglite Use embedded PGLite (zero-config, the default)
|
||||
--supabase Use Supabase Postgres (recommended for 1000+ files)
|
||||
--url <URL> Use a manual Postgres connection string
|
||||
--mcp-only Thin-client mode: connect to a remote gbrain MCP, no local engine
|
||||
@@ -1639,9 +1713,10 @@ EXAMPLES
|
||||
gbrain init --mcp-only --url https://... # Thin-client mode
|
||||
|
||||
NOTES
|
||||
- Bare \`gbrain init\` in a directory with 1000+ .md files defaults to Supabase
|
||||
interactive setup. With <1000 files (or with --pglite explicitly), defaults
|
||||
to PGLite at ~/.gbrain/brain.pglite.
|
||||
- Bare \`gbrain init\` always defaults to PGLite at ~/.gbrain/brain.pglite.
|
||||
In a directory with 1000+ .md files it prints a suggestion to use
|
||||
\`gbrain init --supabase\` (faster search at scale) but still proceeds
|
||||
with PGLite.
|
||||
- Existing config is preserved unless --force is passed.
|
||||
`.trim());
|
||||
}
|
||||
|
||||
@@ -6,21 +6,16 @@
|
||||
* the self-upgrade refresh path.
|
||||
*/
|
||||
|
||||
import { readUpdateCache } from '../self-upgrade.ts';
|
||||
import { pendingUpgradeVersion } from '../self-upgrade.ts';
|
||||
import type { AdvisorCollector } from './types.ts';
|
||||
|
||||
export const collectVersion: AdvisorCollector = {
|
||||
id: 'version',
|
||||
collect: async (ctx) => {
|
||||
let latest: string | undefined;
|
||||
try {
|
||||
const entry = readUpdateCache();
|
||||
if (entry && entry.marker.kind === 'upgrade_available' && entry.marker.latest) {
|
||||
latest = entry.marker.latest;
|
||||
}
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
// Shared stale/foreign-cache guard: fresh cache only, and only an upgrade
|
||||
// strictly newer than the RUNNING version (pendingUpgradeVersion owns the
|
||||
// rule; never throws).
|
||||
const latest = pendingUpgradeVersion(ctx.version, Date.now());
|
||||
if (!latest) return [];
|
||||
return [
|
||||
{
|
||||
|
||||
@@ -16,6 +16,14 @@ export interface RecommendedSkill {
|
||||
}
|
||||
|
||||
export const RECOMMENDED: RecommendedSkill[] = [
|
||||
{
|
||||
// First on purpose: the day-one "now what?" answer. Every other skill in
|
||||
// this list gets dramatically better once the brain holds the user's real
|
||||
// life — cold-start is what fills it.
|
||||
slug: 'cold-start',
|
||||
description:
|
||||
'START HERE. Day-one brain filling: imports your Gmail, calendar, and contacts (via ClawVisor — an OAuth vault, the agent never holds raw tokens) or offline archives, one consented phase at a time. Run it right after install.',
|
||||
},
|
||||
{
|
||||
slug: 'book-mirror',
|
||||
description:
|
||||
|
||||
+18
-3
@@ -6,6 +6,10 @@
|
||||
export interface ProbeResult {
|
||||
reachable: boolean;
|
||||
models_endpoint_valid?: boolean;
|
||||
/** Model ids the endpoint reports as served/pulled (when the models
|
||||
* endpoint is valid). Lets callers check "is the recipe's model actually
|
||||
* available" instead of treating daemon-up as model-ready. */
|
||||
models?: string[];
|
||||
error?: string;
|
||||
}
|
||||
|
||||
@@ -22,14 +26,25 @@ export async function probeOpenAICompat(baseUrl: string, timeoutMs: number = 100
|
||||
signal: controller.signal,
|
||||
headers: { accept: 'application/json' },
|
||||
});
|
||||
clearTimeout(timer);
|
||||
if (!res.ok) return { reachable: true, models_endpoint_valid: false, error: `HTTP ${res.status}` };
|
||||
if (!res.ok) {
|
||||
clearTimeout(timer);
|
||||
return { reachable: true, models_endpoint_valid: false, error: `HTTP ${res.status}` };
|
||||
}
|
||||
// Keep the abort timer live through the BODY read — a daemon that accepts,
|
||||
// returns headers, then stalls the body would otherwise hang past the
|
||||
// advertised timeout (the probe sits on init's interactive critical path).
|
||||
const body = await res.json().catch(() => null);
|
||||
clearTimeout(timer);
|
||||
if (!body || typeof body !== 'object') {
|
||||
return { reachable: true, models_endpoint_valid: false, error: 'non-JSON response' };
|
||||
}
|
||||
const isList = (body as any).object === 'list' && Array.isArray((body as any).data);
|
||||
return { reachable: true, models_endpoint_valid: isList };
|
||||
const models = isList
|
||||
? ((body as any).data as Array<{ id?: unknown }>)
|
||||
.map((m) => (typeof m?.id === 'string' ? m.id : ''))
|
||||
.filter(Boolean)
|
||||
: undefined;
|
||||
return { reachable: true, models_endpoint_valid: isList, models };
|
||||
} catch (e) {
|
||||
clearTimeout(timer);
|
||||
return { reachable: false, error: e instanceof Error ? e.message : String(e) };
|
||||
|
||||
+19
-15
@@ -10,8 +10,9 @@
|
||||
* and dedupe match on the marker (surviving reordering and command-string
|
||||
* drift), and foreign hooks / permissions / every other settings key are
|
||||
* never touched. Writes are atomic (tmp + rename) with a `.bak` of the
|
||||
* previous file; a parse-broken existing file is backed up aside and the
|
||||
* write starts clean with a loud note in the result [G5].
|
||||
* previous file; a parse-broken existing file ABORTS the write with
|
||||
* fix-and-re-run instructions (fail-closed, matching removal's stance — a
|
||||
* rewrite could drop permissions/allowlist entries gbrain cannot parse) [G5].
|
||||
*
|
||||
* MCP registration helpers BUILD ARGV ONLY — the bootstrap dispatcher execs
|
||||
* them (and records the registration in the install receipt). Precedent:
|
||||
@@ -70,7 +71,9 @@ export interface WriteClaudeHooksResult {
|
||||
removedPrior: number;
|
||||
/** `.bak` of the pre-write file (null when no file existed). */
|
||||
backupPath: string | null;
|
||||
/** Where a parse-broken original was moved (null when parse succeeded). */
|
||||
/** Always null since the fail-closed change (a parse-broken file now
|
||||
* aborts the write instead of being moved aside). Kept for result-shape
|
||||
* stability. */
|
||||
brokenBackupPath: string | null;
|
||||
notes: string[];
|
||||
}
|
||||
@@ -247,10 +250,13 @@ interface LoadedSettings {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the existing settings file. Absent/empty → `{}`. Parse error → the
|
||||
* broken file is MOVED to a timestamped `.broken-*` backup and the caller
|
||||
* starts clean, with a loud note (the user's broken-by-hand file is never
|
||||
* silently destroyed, and never silently half-merged) [G5].
|
||||
* Parse the existing settings file. Absent/empty → `{}`. Parse error →
|
||||
* THROW, fail-closed [G5]: the file may carry permissions/allowlist entries
|
||||
* gbrain cannot see, so replacing it with a fresh file (the old behavior —
|
||||
* backup + start clean) silently dropped the user's live settings. Removal
|
||||
* (`removeClaudeHooks`) already refuses to touch what it cannot parse; the
|
||||
* write path now matches that stance. The user fixes the JSON, re-runs, and
|
||||
* the structural merge preserves everything.
|
||||
*/
|
||||
function loadSettings(path: string): LoadedSettings {
|
||||
const notes: string[] = [];
|
||||
@@ -273,14 +279,12 @@ function loadSettings(path: string): LoadedSettings {
|
||||
}
|
||||
return { settings: parsed as SettingsObject, existed: true, brokenBackupPath: null, notes };
|
||||
} catch (e) {
|
||||
const broken = `${path}.broken-${Date.now()}`;
|
||||
copyFileSync(path, broken);
|
||||
notes.push(
|
||||
`WARNING: ${path} was not valid JSON (${(e as Error).message}); ` +
|
||||
`the original was backed up to ${broken} and hooks were written to a fresh file. ` +
|
||||
`Restore any hand-made settings from the backup.`,
|
||||
throw new Error(
|
||||
`${path} is not valid JSON (${(e as Error).message}) — refusing to rewrite a settings file ` +
|
||||
`gbrain cannot parse (it may carry your permissions/allowlist entries). Fix the JSON by ` +
|
||||
`hand, then re-run \`gbrain bootstrap hooks --harness claude-code --repair\` ` +
|
||||
`(the structural merge preserves your settings).`,
|
||||
);
|
||||
return { settings: {}, existed: true, brokenBackupPath: broken, notes };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,7 +370,7 @@ export function writeClaudeHooks(
|
||||
settings.hooks = hooks;
|
||||
|
||||
let backupPath: string | null = null;
|
||||
if (existed && brokenBackupPath === null) {
|
||||
if (existed) {
|
||||
backupPath = `${settingsPath}.bak`;
|
||||
copyFileSync(settingsPath, backupPath);
|
||||
}
|
||||
|
||||
@@ -239,7 +239,7 @@ function requiredKeys(bank: QuestionBank): string[] {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export type SetAnswerResult =
|
||||
| { ok: true; sink: 'state'; key: string; value: string }
|
||||
| { ok: true; sink: 'state'; key: string; value: string; invalidatedConfirmation?: boolean }
|
||||
/** Config-sink keys [CX2-13]: nothing persisted here; the caller routes the
|
||||
* value via `routeProviderKeyToConfig`. The value is deliberately NOT
|
||||
* echoed back in this result. */
|
||||
@@ -305,20 +305,23 @@ export function setAnswer(workspaceDir: string, key: string, rawValue: string):
|
||||
if (!read.ok) return read;
|
||||
const state = read.state;
|
||||
state.answers[key] = { value: stored, set_at: new Date().toISOString() };
|
||||
// Any change invalidates a prior read-back confirmation [A8].
|
||||
// Any change invalidates a prior read-back confirmation [A8]. Surfaced to
|
||||
// the caller so the CLI can WARN — silently voiding the confirmation used
|
||||
// to fail much later, at render, with no pointer back to this --set.
|
||||
const invalidatedConfirmation = state.confirmed !== undefined;
|
||||
delete state.confirmed;
|
||||
try {
|
||||
writeInterviewState(workspaceDir, state);
|
||||
} catch (e) {
|
||||
return { ok: false, code: 'io_error', message: `could not write interview state: ${(e as Error).message}` };
|
||||
}
|
||||
return { ok: true, sink: 'state', key, value: stored };
|
||||
return { ok: true, sink: 'state', key, value: stored, invalidatedConfirmation };
|
||||
}
|
||||
|
||||
export function skipAnswer(
|
||||
workspaceDir: string,
|
||||
key: string
|
||||
): { ok: true; sink: 'state' | 'config'; key: string; skipped: true } | InterviewError {
|
||||
): { ok: true; sink: 'state' | 'config'; key: string; skipped: true; invalidatedConfirmation?: boolean } | InterviewError {
|
||||
const bank = loadQuestionBank();
|
||||
const spec = bankSpec(bank, key);
|
||||
if (!spec) {
|
||||
@@ -339,13 +342,15 @@ export function skipAnswer(
|
||||
if (!read.ok) return read;
|
||||
const state = read.state;
|
||||
state.answers[key] = { value: '', set_at: new Date().toISOString(), skipped: true };
|
||||
// Same [A8] invalidation-surfacing as setAnswer.
|
||||
const invalidatedConfirmation = state.confirmed !== undefined;
|
||||
delete state.confirmed;
|
||||
try {
|
||||
writeInterviewState(workspaceDir, state);
|
||||
} catch (e) {
|
||||
return { ok: false, code: 'io_error', message: `could not write interview state: ${(e as Error).message}` };
|
||||
}
|
||||
return { ok: true, sink: 'state', key, skipped: true };
|
||||
return { ok: true, sink: 'state', key, skipped: true, invalidatedConfirmation };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -270,7 +270,20 @@ export const PHASES: PhaseSpec[] = [
|
||||
detect: (ws, ctx) => {
|
||||
const regs = ctx.receipt?.registrations ?? [];
|
||||
if (regs.length > 0) {
|
||||
return { state: 'done', detail: regs.map((r) => `${r.host} (${r.scope})`).join(', ') };
|
||||
// A registration whose detail carries 'mcp' ('mcp' or 'mcp+hooks')
|
||||
// means MCP actually registered. A 'hooks'-only detail means the host
|
||||
// binary was missing at wire time (hooks landed, MCP did not) — the
|
||||
// phase is PARTIAL, not done, so a resuming agent re-runs it once the
|
||||
// CLI is on PATH instead of trusting a false "done".
|
||||
const mcpRegistered = regs.some((r) => (r.detail ?? '').includes('mcp'));
|
||||
if (mcpRegistered) {
|
||||
return { state: 'done', detail: regs.map((r) => `${r.host} (${r.scope})`).join(', ') };
|
||||
}
|
||||
return {
|
||||
state: 'partial',
|
||||
detail: 'hooks installed but MCP not registered (the harness CLI was not on PATH) — ' +
|
||||
're-run `gbrain bootstrap hooks --harness <claude-code|codex>` once it is',
|
||||
};
|
||||
}
|
||||
if (hooksInstalled(ws)) return { state: 'done', detail: 'hooks present in .claude/settings.local.json' };
|
||||
return { state: 'pending' };
|
||||
|
||||
@@ -75,6 +75,9 @@ export interface VerifyReport {
|
||||
capability: CapabilityReport;
|
||||
/** The three scripted first-run prompts [D3.6/A4]. */
|
||||
tour: string[];
|
||||
/** The OOBE hand-off lines (ownership + the cold-start next action) —
|
||||
* unconditional in the shape like `tour`; printed in the report on PASS. */
|
||||
handoff: string[];
|
||||
}
|
||||
|
||||
export interface VerifyOpts {
|
||||
@@ -97,11 +100,15 @@ export const VERIFY_PROBE_ENTITY_SLUG = 'wiki/bootstrap-verify-probe-entity';
|
||||
/** Deterministic magic-moment token the fence fact carries [CX-P0.5]. */
|
||||
export const VERIFY_MAGIC_TOKEN = 'verify-lighthouse-passphrase';
|
||||
|
||||
/** The three scripted first-run prompts [D3.6] — pinned by the A4 snapshot test. */
|
||||
/** The three scripted first-run prompts [D3.6] — pinned by the A4 snapshot test.
|
||||
* Exactly three (the count is copy-pinned here, in BOOTSTRAP_FOR_AGENTS.md,
|
||||
* and the A4 plan). Prompt 3 must be TRUE on day one — the brain is empty at
|
||||
* install, so "everything ingested so far" would be an anticlimax; the
|
||||
* round-trip fact from prompt 2 is the honest day-one payoff. */
|
||||
export const FIRST_RUN_TOUR: readonly string[] = [
|
||||
'"Who am I to you?" — identity from SOUL.md/USER.md, no lookup needed.',
|
||||
'"Remember that <one small true fact>." Then restart the session and ask me about it — that round-trip is the whole product.',
|
||||
'"What do you know about this project?" — brain recall over everything ingested so far.',
|
||||
'"Remember that <one small true fact>." — it lands in the brain, not this chat.',
|
||||
'"What do you remember about me?" — asked in the NEW session: on day one that is the fact from prompt 2, recalled from the brain. Every session after this adds more. That round-trip is the whole product.',
|
||||
];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -948,9 +955,11 @@ export async function verifyWorkspace(
|
||||
|
||||
checks.push(checkPushProbe(ws));
|
||||
checks.push(checkInertSkills(ws, caps));
|
||||
checks.push({ id: 'first_run_tour', ok: true, detail: 'three scripted prompts appended to the report [D3.6]' });
|
||||
const tourCheck = { id: 'first_run_tour', ok: true, detail: 'three scripted prompts appended to the report' };
|
||||
checks.push(tourCheck);
|
||||
|
||||
const ok = checks.every((c) => c.ok || c.warn === true);
|
||||
if (!ok) tourCheck.detail = 'tour withheld — prints on PASS';
|
||||
|
||||
const ts = new Date().toISOString();
|
||||
persistVerifyRun(gbrainHomeDir, { ts, ok, checks });
|
||||
@@ -964,8 +973,57 @@ export async function verifyWorkspace(
|
||||
lines.push('');
|
||||
lines.push(renderCapabilityReport(caps));
|
||||
lines.push('');
|
||||
lines.push('First-run tour — hand these three prompts to your human, in order:');
|
||||
FIRST_RUN_TOUR.forEach((p, i) => lines.push(` ${i + 1}. ${p}`));
|
||||
// The tour celebrates a WORKING install — under a FAIL banner it reads as
|
||||
// a mixed signal ("broken, but go enjoy it"). Gate the report lines on ok;
|
||||
// the returned `tour` array (and --json field) stays unconditional so
|
||||
// machine consumers keep a stable shape.
|
||||
const handoff = buildHandoff(ws);
|
||||
if (ok) {
|
||||
lines.push('First-run tour — have your human RESTART the session first');
|
||||
lines.push('(a fresh session proves the files and the brain, not this chat),');
|
||||
lines.push('then try these three prompts in order:');
|
||||
FIRST_RUN_TOUR.forEach((p, i) => lines.push(` ${i + 1}. ${p}`));
|
||||
lines.push('');
|
||||
for (const h of handoff) lines.push(h);
|
||||
} else {
|
||||
lines.push('Fix the FAIL checks above and re-run — the first-run tour prints on PASS.');
|
||||
}
|
||||
|
||||
return { ok, checks, report: lines.join('\n'), capability: caps, tour: [...FIRST_RUN_TOUR] };
|
||||
return { ok, checks, report: lines.join('\n'), capability: caps, tour: [...FIRST_RUN_TOUR], handoff };
|
||||
}
|
||||
|
||||
/**
|
||||
* The post-tour hand-off block [OOBE]: the two things a fresh user must walk
|
||||
* away UNDERSTANDING, in priority order —
|
||||
* 1. OWNERSHIP: the brain is markdown in a repo THEY own (or local-only,
|
||||
* with the one command that gives it a durable home). Ownership is the
|
||||
* trust story; say the URL, say what owning it means.
|
||||
* 2. THE ONE NEXT ACTION: run the cold-start skill. An empty brain is a
|
||||
* database; every flagship skill (book-mirror, briefings, meeting prep)
|
||||
* only becomes magical once the brain holds the user's real life —
|
||||
* cold-start is the designed filler (Gmail/calendar/contacts via
|
||||
* ClawVisor, or offline archives), one consented phase at a time.
|
||||
* Returned unconditionally in the machine shape (like `tour`); printed in
|
||||
* the report only on PASS. Relay it to the human verbatim.
|
||||
*/
|
||||
export function buildHandoff(ws: string): string[] {
|
||||
const origin = gitOriginUrl(ws);
|
||||
const ownership = origin
|
||||
? [
|
||||
`What you own: every memory your agent keeps is a markdown file in YOUR private repo — ${origin}.`,
|
||||
'Read it any time, take it to a second machine (`gbrain bootstrap attach`), or delete it and the brain is gone. It is yours.',
|
||||
]
|
||||
: [
|
||||
'What you own: your agent\'s memory is markdown on this machine only (no remote yet).',
|
||||
'Run `gbrain bootstrap repo` any time to give it a private GitHub home you own — readable, portable, deletable.',
|
||||
];
|
||||
return [
|
||||
...ownership,
|
||||
'',
|
||||
'Fill it next: an empty brain is a database; a filled one is a memory.',
|
||||
'Ask your agent to run the cold-start skill — it imports your real life',
|
||||
'(Gmail, calendar, contacts via ClawVisor, an OAuth vault so the agent never',
|
||||
'holds raw tokens; or offline archives like Google Takeout), one consented',
|
||||
'phase at a time. Each phase is independently valuable — stop whenever.',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* AgentRunner — pluggable contract for invoking external agents (openclaw,
|
||||
* hermes, codex, …) inside the claw-test harness. v1 ships a single
|
||||
* implementation (openclaw); the interface stays narrow and concrete so
|
||||
* adding a second runner in v1.1 is a ~50-line file.
|
||||
* hermes, codex, …) inside the claw-test harness. Two implementations ship
|
||||
* (openclaw, hermes); the interface stays narrow and concrete so adding
|
||||
* another runner is a ~100-line file.
|
||||
*
|
||||
* The harness wraps spawn/timeout/transcript-capture; runners only have to
|
||||
* answer "where's your binary?" and "how do I invoke it with this prompt?".
|
||||
@@ -82,6 +82,50 @@ export interface TranscriptEvent {
|
||||
bytes: Buffer;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Shared runner helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Env keys every runner forwards to its agent subprocess. Runners compose
|
||||
* `[...BASE_ENV_ALLOWLIST, ...delta]` instead of duplicating the list — the
|
||||
* allowlist (not a denylist) is the leak barrier: anything not named here
|
||||
* never reaches the agent.
|
||||
*
|
||||
* GBRAIN_DATABASE_URL is deliberately ABSENT (removed in the hermes-harness
|
||||
* wave's adversarial review): live mode's staging + success oracle operate on
|
||||
* the hermetic PGLite under GBRAIN_HOME=tempdir, and an inherited
|
||||
* GBRAIN_DATABASE_URL would flip only the AGENT's gbrain children to the
|
||||
* operator's real Postgres — polluting the real brain while the oracle probes
|
||||
* the untouched PGLite and fails with a misleading verdict.
|
||||
*/
|
||||
export const BASE_ENV_ALLOWLIST = [
|
||||
'PATH', 'HOME', 'USER', 'LANG', 'TZ', 'NODE_ENV',
|
||||
'ANTHROPIC_API_KEY', 'OPENAI_API_KEY',
|
||||
'GBRAIN_HOME', 'GBRAIN_FRICTION_RUN_ID',
|
||||
// Proxy plumbing (both spellings — Node reads upper, Python/curl read
|
||||
// lower): an operator behind a corporate proxy runs their agent through
|
||||
// these, and dropping them turns live mode into a misleading network
|
||||
// failure blamed on the agent.
|
||||
'HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY',
|
||||
'http_proxy', 'https_proxy', 'no_proxy',
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Validate a *_BIN env override: must be absolute, free of `..` segments, and
|
||||
* free of shell-active characters. The value is interpolated into generated
|
||||
* sh shim scripts (single-quoted), so quotes/backslashes/dollar/backtick or a
|
||||
* newline would break out of the quoting and become code — reject them
|
||||
* outright rather than trying to escape. Spaces are fine (quoted).
|
||||
* Returns an error string (naming the env var) or null when valid.
|
||||
*/
|
||||
export function validateBinPathEnv(envName: string, p: string): string | null {
|
||||
if (!p.startsWith('/')) return `${envName} must be absolute; got ${p}`;
|
||||
if (p.split('/').includes('..')) return `${envName} must not contain '..' segments; got ${p}`;
|
||||
if (/['"`$\\\n\r]/.test(p)) return `${envName} must not contain quotes, backslashes, dollar signs, backticks, or newlines; got ${p}`;
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Registry
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Hermes runner — invokes the real `hermes` binary (NousResearch
|
||||
* hermes-agent, the public platform) in a tempdir with a BRIEF.md prompt.
|
||||
* Live mode only.
|
||||
*
|
||||
* Invocation pattern (verified against a pinned local install, v0.20.0 —
|
||||
* see docs/mcp/HERMES-CLI-PIN.md):
|
||||
* hermes -z "<brief>"
|
||||
*
|
||||
* The z flag is Hermes's headless one-shot: single prompt in, final response
|
||||
* text on stdout, nothing else. We deliberately do NOT pass Hermes's
|
||||
* working-directory flag (spelled "in") — `spawnWithCapture` already sets
|
||||
* `cwd`. `opts.agentName` is unused: the one-shot mode has no sub-agent
|
||||
* selector.
|
||||
*
|
||||
* Hermeticity posture (deliberate): live mode runs the OPERATOR's configured
|
||||
* Hermes — the real ~/.hermes (model settings, skills, sessions) is inherited
|
||||
* unless HERMES_HOME points elsewhere — against a hermetic BRAIN. The fully
|
||||
* hermetic lane is the door e2e (install-real-hermes.serial.test.ts).
|
||||
*
|
||||
* Binary resolution: $HERMES_BIN > `which hermes` > unavailable.
|
||||
* Path validation: must be absolute, must be executable, no '..' segments.
|
||||
*/
|
||||
|
||||
import { execSync } from 'child_process';
|
||||
import { statSync } from 'fs';
|
||||
import {
|
||||
BASE_ENV_ALLOWLIST,
|
||||
validateBinPathEnv,
|
||||
type AgentRunner,
|
||||
type DetectResult,
|
||||
type InvokeOpts,
|
||||
type InvokeResult,
|
||||
} from '../agent-runner.ts';
|
||||
import { spawnWithCapture } from '../transcript-capture.ts';
|
||||
|
||||
/**
|
||||
* Allow-list for env propagation when spawning hermes. Delta from the shared
|
||||
* base: HERMES_HOME, so callers (door e2e, CI) can point Hermes at an
|
||||
* isolated home instead of the operator's real ~/.hermes; and
|
||||
* OPENROUTER_API_KEY, because OpenRouter is a Hermes-documented auth path
|
||||
* (docs/mcp/HERMES.md) — an operator whose hermes auths only via that env var
|
||||
* would otherwise see "no inference provider" and the harness would blame the
|
||||
* agent. Caveat (observed, docs/mcp/HERMES-CLI-PIN.md): live mode forwards
|
||||
* whatever provider keys the operator's shell exports, mirroring a direct
|
||||
* hermes run — with MULTIPLE keys visible and no model pinned in config,
|
||||
* Hermes's provider auto-routing can mis-route and fail with an HTTP 401 in
|
||||
* the final text. The operator's own config.yaml model pin is what prevents
|
||||
* that, same as it does outside the harness.
|
||||
*/
|
||||
const ENV_ALLOWLIST = [...BASE_ENV_ALLOWLIST, 'HERMES_HOME', 'OPENROUTER_API_KEY'];
|
||||
|
||||
export class HermesRunner implements AgentRunner {
|
||||
readonly name = 'hermes';
|
||||
|
||||
async detect(): Promise<DetectResult> {
|
||||
const fromEnv = process.env.HERMES_BIN?.trim();
|
||||
let binPath: string | undefined;
|
||||
|
||||
if (fromEnv) {
|
||||
const validation = validateBinPathEnv('HERMES_BIN', fromEnv);
|
||||
if (validation) return { available: false, reason: validation };
|
||||
binPath = fromEnv;
|
||||
} else {
|
||||
try {
|
||||
const out = execSync('which hermes', { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] });
|
||||
const found = out.trim();
|
||||
if (!found || !found.startsWith('/')) {
|
||||
return { available: false, reason: 'hermes not on PATH' };
|
||||
}
|
||||
binPath = found;
|
||||
} catch {
|
||||
return { available: false, reason: 'hermes not on PATH' };
|
||||
}
|
||||
}
|
||||
|
||||
if (!binPath) return { available: false, reason: 'no binary resolved' };
|
||||
|
||||
try {
|
||||
const s = statSync(binPath);
|
||||
if (!s.isFile()) return { available: false, reason: `not a regular file: ${binPath}` };
|
||||
// eslint-disable-next-line no-bitwise
|
||||
if (!(s.mode & 0o111)) return { available: false, reason: `not executable: ${binPath}` };
|
||||
} catch (e) {
|
||||
return { available: false, reason: `stat failed: ${e instanceof Error ? e.message : String(e)}` };
|
||||
}
|
||||
|
||||
return { available: true, binPath };
|
||||
}
|
||||
|
||||
async invoke(opts: InvokeOpts): Promise<InvokeResult> {
|
||||
const detected = await this.detect();
|
||||
if (!detected.available || !detected.binPath) {
|
||||
throw new Error(`hermes runner unavailable: ${detected.reason ?? 'unknown'}`);
|
||||
}
|
||||
const args = ['-z', opts.brief];
|
||||
|
||||
// Filter env to allow-list, then merge caller overrides.
|
||||
const baseEnv: Record<string, string> = {};
|
||||
for (const key of ENV_ALLOWLIST) {
|
||||
const v = process.env[key];
|
||||
if (typeof v === 'string') baseEnv[key] = v;
|
||||
}
|
||||
const env: Record<string, string> = { ...baseEnv, ...opts.env };
|
||||
|
||||
const result = await spawnWithCapture(detected.binPath, args, {
|
||||
cwd: opts.cwd,
|
||||
env,
|
||||
timeoutMs: opts.timeoutMs,
|
||||
transcriptSink: opts.transcriptSink,
|
||||
});
|
||||
|
||||
return { exitCode: result.exitCode, durationMs: result.durationMs };
|
||||
}
|
||||
}
|
||||
@@ -15,16 +15,19 @@
|
||||
|
||||
import { execSync } from 'child_process';
|
||||
import { statSync } from 'fs';
|
||||
import type { AgentRunner, DetectResult, InvokeOpts, InvokeResult } from '../agent-runner.ts';
|
||||
import {
|
||||
BASE_ENV_ALLOWLIST,
|
||||
validateBinPathEnv,
|
||||
type AgentRunner,
|
||||
type DetectResult,
|
||||
type InvokeOpts,
|
||||
type InvokeResult,
|
||||
} from '../agent-runner.ts';
|
||||
import { spawnWithCapture } from '../transcript-capture.ts';
|
||||
|
||||
const DEFAULT_AGENT_NAME = 'default';
|
||||
/** Allow-list for env propagation when spawning openclaw. */
|
||||
const ENV_ALLOWLIST = [
|
||||
'PATH', 'HOME', 'USER', 'LANG', 'TZ', 'NODE_ENV',
|
||||
'ANTHROPIC_API_KEY', 'OPENAI_API_KEY',
|
||||
'GBRAIN_HOME', 'GBRAIN_FRICTION_RUN_ID', 'GBRAIN_DATABASE_URL',
|
||||
];
|
||||
/** Allow-list for env propagation when spawning openclaw (no delta from base). */
|
||||
const ENV_ALLOWLIST = [...BASE_ENV_ALLOWLIST];
|
||||
|
||||
export class OpenClawRunner implements AgentRunner {
|
||||
readonly name = 'openclaw';
|
||||
@@ -34,7 +37,7 @@ export class OpenClawRunner implements AgentRunner {
|
||||
let binPath: string | undefined;
|
||||
|
||||
if (fromEnv) {
|
||||
const validation = validateAbsolutePath(fromEnv);
|
||||
const validation = validateBinPathEnv('OPENCLAW_BIN', fromEnv);
|
||||
if (validation) return { available: false, reason: validation };
|
||||
binPath = fromEnv;
|
||||
} else {
|
||||
@@ -91,8 +94,3 @@ export class OpenClawRunner implements AgentRunner {
|
||||
}
|
||||
}
|
||||
|
||||
function validateAbsolutePath(p: string): string | null {
|
||||
if (!p.startsWith('/')) return `OPENCLAW_BIN must be absolute; got ${p}`;
|
||||
if (p.split('/').includes('..')) return `OPENCLAW_BIN must not contain '..' segments; got ${p}`;
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,22 @@ import { fileURLToPath } from 'url';
|
||||
|
||||
export type ScenarioKind = 'fresh-install' | 'upgrade';
|
||||
|
||||
/**
|
||||
* Live-mode success oracle, declared per scenario. The harness verifies these
|
||||
* AFTER the agent exits 0 — exit code alone would pass an agent that did
|
||||
* nothing. All fields optional; a scenario with no oracle gets the kind's
|
||||
* default verification (fresh-install: doctor only; upgrade: schema-version
|
||||
* probe must ADVANCE during the agent turn).
|
||||
*/
|
||||
export interface ScenarioOracle {
|
||||
/** Query the staged brain must answer post-run (live mode). */
|
||||
query?: string;
|
||||
/** Minimum result count for `query` (default 1 when query is set). */
|
||||
minResults?: number;
|
||||
/** Workspace-relative paths that must exist post-run (proves brief steps ran). */
|
||||
filesExist?: string[];
|
||||
}
|
||||
|
||||
export interface ScenarioConfig {
|
||||
/** Directory the scenario was loaded from. Always absolute. */
|
||||
dir: string;
|
||||
@@ -33,6 +49,8 @@ export interface ScenarioConfig {
|
||||
brainRelative?: string;
|
||||
/** Path to seed dir for upgrade scenarios. */
|
||||
seedRelative?: string;
|
||||
/** Live-mode success oracle (see ScenarioOracle). */
|
||||
oracle?: ScenarioOracle;
|
||||
}
|
||||
|
||||
/** Default fixtures root, override via $GBRAIN_CLAW_SCENARIOS_DIR for tests. */
|
||||
@@ -61,10 +79,30 @@ export function listScenarios(root?: string): string[] {
|
||||
.sort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Confine a scenario-declared relative path inside the scenario dir. The
|
||||
* scenario pack is a trust boundary (loadable from arbitrary dirs via the
|
||||
* scenarios-dir env override, and its content flows to an external agent):
|
||||
* a traversal-shaped brief/brain/seed would let a pack read — and, via the
|
||||
* BRIEF, exfiltrate to the agent — arbitrary operator files.
|
||||
*/
|
||||
function confineToScenarioDir(name: string, dir: string, rel: string, field: string): void {
|
||||
const abs = resolve(dir, rel);
|
||||
if (abs !== dir && !abs.startsWith(dir + '/')) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: ${field} must stay inside the scenario dir; got ${JSON.stringify(rel)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Load and validate one scenario by name. */
|
||||
export function loadScenario(name: string, root?: string): ScenarioConfig {
|
||||
// The name is a directory segment — never a path. A traversal-shaped name
|
||||
// would resolve scenario.json (and everything the scenario references)
|
||||
// outside the fixtures root.
|
||||
if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(name) || name.includes('..')) {
|
||||
throw new Error(`scenario name ${JSON.stringify(name)} not found (names are letters, digits, dot, dash, underscore)`);
|
||||
}
|
||||
const r = root ?? defaultFixturesRoot();
|
||||
const dir = join(r, name);
|
||||
const dir = resolve(join(r, name));
|
||||
const cfgPath = join(dir, 'scenario.json');
|
||||
if (!existsSync(cfgPath)) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)} not found at ${cfgPath}`);
|
||||
@@ -86,6 +124,7 @@ export function loadScenario(name: string, root?: string): ScenarioConfig {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: expected_phases must be a string[]`);
|
||||
}
|
||||
const briefRel = typeof cfg.brief === 'string' ? cfg.brief : 'BRIEF.md';
|
||||
confineToScenarioDir(name, dir, briefRel, 'brief');
|
||||
if (!existsSync(join(dir, briefRel))) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: BRIEF.md missing at ${briefRel}`);
|
||||
}
|
||||
@@ -98,8 +137,60 @@ export function loadScenario(name: string, root?: string): ScenarioConfig {
|
||||
};
|
||||
if (typeof cfg.from_version === 'string') out.fromVersion = cfg.from_version;
|
||||
if (typeof cfg.description === 'string') out.description = cfg.description;
|
||||
if (typeof cfg.brain === 'string') out.brainRelative = cfg.brain;
|
||||
if (typeof cfg.seed === 'string') out.seedRelative = cfg.seed;
|
||||
if (typeof cfg.brain === 'string') {
|
||||
confineToScenarioDir(name, dir, cfg.brain, 'brain');
|
||||
out.brainRelative = cfg.brain;
|
||||
}
|
||||
if (typeof cfg.seed === 'string') {
|
||||
confineToScenarioDir(name, dir, cfg.seed, 'seed');
|
||||
out.seedRelative = cfg.seed;
|
||||
}
|
||||
if (cfg.oracle !== undefined) {
|
||||
if (!cfg.oracle || typeof cfg.oracle !== 'object' || Array.isArray(cfg.oracle)) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: oracle must be a JSON object`);
|
||||
}
|
||||
const o = cfg.oracle as Record<string, unknown>;
|
||||
const oracle: ScenarioOracle = {};
|
||||
if (o.query !== undefined) {
|
||||
if (typeof o.query !== 'string' || !o.query.trim()) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: oracle.query must be a non-empty string`);
|
||||
}
|
||||
// The query lands in gbrain argv — a leading dash would parse as a CLI
|
||||
// flag instead of a query (scenario packs load from arbitrary dirs via
|
||||
// the scenarios-dir env override, so treat this as a trust boundary).
|
||||
if (o.query.trim().startsWith('-')) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: oracle.query must not start with a dash`);
|
||||
}
|
||||
oracle.query = o.query;
|
||||
oracle.minResults = 1;
|
||||
}
|
||||
if (o.min_results !== undefined) {
|
||||
// Rejecting min_results without query keeps the config honest: the
|
||||
// harness only reads minResults when a query is declared, and accepting
|
||||
// config it never enforces would be a silent no-op.
|
||||
if (o.query === undefined) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: oracle.min_results requires oracle.query`);
|
||||
}
|
||||
if (typeof o.min_results !== 'number' || !Number.isFinite(o.min_results) || o.min_results < 0) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: oracle.min_results must be a number >= 0`);
|
||||
}
|
||||
oracle.minResults = o.min_results;
|
||||
}
|
||||
if (o.files_exist !== undefined) {
|
||||
if (!Array.isArray(o.files_exist) || !o.files_exist.every(x => typeof x === 'string' && x.trim())) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: oracle.files_exist must be a string[]`);
|
||||
}
|
||||
// Paths are resolved relative to the run's workspace; confine them so a
|
||||
// scenario pack can't probe arbitrary operator paths.
|
||||
for (const p of o.files_exist as string[]) {
|
||||
if (p.startsWith('/') || p.split('/').includes('..')) {
|
||||
throw new Error(`scenario ${JSON.stringify(name)}: oracle.files_exist entries must be workspace-relative (no absolute paths, no '..'): ${JSON.stringify(p)}`);
|
||||
}
|
||||
}
|
||||
oracle.filesExist = o.files_exist as string[];
|
||||
}
|
||||
out.oracle = oracle;
|
||||
}
|
||||
// Default brain path conventions
|
||||
if (!out.brainRelative && existsSync(join(dir, 'brain'))) out.brainRelative = 'brain';
|
||||
if (!out.seedRelative && out.kind === 'upgrade' && existsSync(join(dir, 'seed'))) {
|
||||
|
||||
@@ -66,6 +66,34 @@ export async function seedPgliteFromFile(opts: { dbPath: string; sqlPath: string
|
||||
return seedPglite({ dbPath: opts.dbPath, sql });
|
||||
}
|
||||
|
||||
/**
|
||||
* Non-mutating schema-version probe for the live-mode upgrade oracle.
|
||||
*
|
||||
* The migration runner records its position via setConfig('version', …), i.e.
|
||||
* `config.key = 'version'`. Reading it through the normal CLI is NOT valid as
|
||||
* an upgrade oracle: every CLI connect routes through connectEngine → initSchema,
|
||||
* which auto-applies pending migrations — the verifier would perform the very
|
||||
* upgrade a do-nothing agent skipped. This probe opens PGLite directly (same
|
||||
* path as the seeder) and reads the row without touching the migration chain.
|
||||
*
|
||||
* Returns the recorded version, or null when the db/table/row doesn't exist.
|
||||
*/
|
||||
export async function readPgliteSchemaVersion(dbPath: string): Promise<number | null> {
|
||||
if (!existsSync(dbPath)) return null;
|
||||
const engine = new PGLiteEngine();
|
||||
try {
|
||||
await engine.connect({ engine: 'pglite', database_path: dbPath });
|
||||
const rows = await (engine as any).db.query(`SELECT value FROM config WHERE key = 'version'`);
|
||||
const value = rows?.rows?.[0]?.value;
|
||||
const n = typeof value === 'string' ? Number.parseInt(value, 10) : typeof value === 'number' ? value : NaN;
|
||||
return Number.isFinite(n) ? n : null;
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
try { await engine.disconnect(); } catch { /* best effort */ }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a SQL dump into individual statements. Naïve `;` split that respects
|
||||
* single-quoted strings and `--` line comments. Sufficient for canonical
|
||||
|
||||
@@ -105,6 +105,8 @@ export interface SpawnResult {
|
||||
}
|
||||
|
||||
const SIGTERM_GRACE_MS = 5_000;
|
||||
/** After 'exit', how long to wait for 'close' (pipe drain) before resolving anyway. */
|
||||
const STREAM_DRAIN_GRACE_MS = 2_000;
|
||||
|
||||
export async function spawnWithCapture(bin: string, args: string[], opts: SpawnOpts): Promise<SpawnResult> {
|
||||
const start = Date.now();
|
||||
@@ -116,19 +118,33 @@ export async function spawnWithCapture(bin: string, args: string[], opts: SpawnO
|
||||
env: opts.env,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
shell: false,
|
||||
// Own process group so timeout kills reach grandchildren too — agents
|
||||
// spawn MCP servers and gbrain children; signalling only the direct
|
||||
// PID leaves those orphaned holding the stdout/stderr pipes open.
|
||||
detached: true,
|
||||
});
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Signal the whole process group (negative pid); fall back to the direct
|
||||
// child if the group is already gone or grouping failed.
|
||||
const killTree = (sig: NodeJS.Signals) => {
|
||||
const pid = child.pid;
|
||||
if (pid) {
|
||||
try { process.kill(-pid, sig); return; } catch { /* group gone or not a leader */ }
|
||||
}
|
||||
try { child.kill(sig); } catch { /* already gone */ }
|
||||
};
|
||||
|
||||
let timedOut = false;
|
||||
let killTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
const wallClockTimer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
try { child.kill('SIGTERM'); } catch { /* already gone */ }
|
||||
killTree('SIGTERM');
|
||||
killTimer = setTimeout(() => {
|
||||
try { child.kill('SIGKILL'); } catch { /* already gone */ }
|
||||
killTree('SIGKILL');
|
||||
}, SIGTERM_GRACE_MS);
|
||||
}, opts.timeoutMs);
|
||||
|
||||
@@ -151,15 +167,17 @@ export async function spawnWithCapture(bin: string, args: string[], opts: SpawnO
|
||||
reject(e);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// No payload: close stdin anyway. An agent build that waits for stdin
|
||||
// EOF would otherwise block silently until the wall-clock kill — a paid
|
||||
// live turn burned as a timeout.
|
||||
try { child.stdin?.end(); } catch { /* stream already gone */ }
|
||||
}
|
||||
|
||||
child.on('error', (err) => {
|
||||
clearTimeout(wallClockTimer);
|
||||
if (killTimer) clearTimeout(killTimer);
|
||||
reject(err);
|
||||
});
|
||||
|
||||
child.on('close', (code) => {
|
||||
let settled = false;
|
||||
const settle = (code: number | null) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(wallClockTimer);
|
||||
if (killTimer) clearTimeout(killTimer);
|
||||
resolve({
|
||||
@@ -167,6 +185,25 @@ export async function spawnWithCapture(bin: string, args: string[], opts: SpawnO
|
||||
durationMs: Date.now() - start,
|
||||
timedOut,
|
||||
});
|
||||
};
|
||||
|
||||
child.on('error', (err) => {
|
||||
clearTimeout(wallClockTimer);
|
||||
if (killTimer) clearTimeout(killTimer);
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
// 'close' (all pipes drained) is the clean path. But a grandchild that
|
||||
// survives the group kill can hold the pipes open forever, so 'exit' arms
|
||||
// a short drain grace and then settles regardless — the harness must not
|
||||
// hang AFTER its own timeout already fired.
|
||||
child.on('close', (code) => settle(code));
|
||||
child.on('exit', (code) => {
|
||||
const t = setTimeout(() => settle(code), STREAM_DRAIN_GRACE_MS);
|
||||
t.unref?.();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
|
||||
'check-backlinks': ['--background', '--brain', '--brain-wide-max-cost-usd', '--dir', '--dry-run', '--explain', '--follow', '--help', '--include-frontmatter', '--json', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--source', '--stale', '--timeout', '--type'],
|
||||
'check-resolvable': ['--brain', '--dry-run', '--fix', '--help', '--json', '--skills-dir', '--source', '--strict', '--verbose'],
|
||||
'check-update': ['--all', '--brain', '--check', '--dim', '--ff-only', '--help', '--json', '--markdown', '--migrate-only', '--non-interactive', '--refresh-cache', '--source', '--swap-only', '--to', '--version', '--yes'],
|
||||
'claw-test': ['--agent', '--brain', '--dir', '--help', '--json', '--keep-tempdir', '--list-agents', '--live', '--local', '--message', '--no-embed', '--no-embedding', '--path', '--pglite', '--progress-json', '--prompt-file', '--run-id', '--scenario', '--source', '--transcripts'],
|
||||
'claw-test': ['--ab', '--agent', '--all', '--auto-update', '--brain', '--break-lock', '--build-index', '--by-mention', '--compile', '--days', '--dir', '--exclusive', '--force-retry', '--force-schema', '--from-meetings', '--help', '--history', '--http', '--json', '--keep-tempdir', '--lang', '--list-agents', '--live', '--local', '--locks', '--markdown', '--max-age', '--message', '--multimodal', '--no-embed', '--no-embedding', '--no-extract', '--path', '--pglite', '--phase', '--priority', '--progress-json', '--prompt-file', '--refresh-unqualified', '--remediate', '--rollback', '--run-id', '--scenario', '--skip-verify', '--source', '--stale', '--transcripts', '--undo-wave', '--use-captured-snapshot', '--with-calibration', '--yes'],
|
||||
'code-callees': ['--aliases', '--all', '--all-sources', '--brain', '--chunker-debug', '--clone-dir', '--confirm-destructive', '--federated', '--force', '--help', '--include-null-signature', '--json', '--limit', '--no-extract', '--no-federated', '--no-json', '--path', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--thin', '--url', '--url-managed', '--yes'],
|
||||
'code-callers': ['--aliases', '--all', '--all-sources', '--brain', '--chunker-debug', '--clone-dir', '--confirm-destructive', '--federated', '--force', '--help', '--include-null-signature', '--json', '--limit', '--no-extract', '--no-federated', '--no-json', '--path', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--thin', '--url', '--url-managed', '--yes'],
|
||||
'code-def': ['--aliases', '--all', '--brain', '--chunker-debug', '--help', '--include-null-signature', '--json', '--lang', '--limit', '--no-extract', '--no-json', '--pattern', '--pending', '--pretty', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--yes'],
|
||||
@@ -47,12 +47,12 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
|
||||
'files': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--dry-run', '--explain', '--fast', '--follow', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--no-embedding', '--no-extract', '--no-pointer', '--page', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--retry-failed', '--source', '--stale', '--supersessions', '--surface', '--thin', '--timeout', '--token-ttl', '--type', '--yes'],
|
||||
'forget': ['--aliases', '--all', '--allow-empty', '--apply', '--as-context', '--asof', '--auto', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-tokens', '--budget-usd-per-day', '--by-mention', '--content', '--date', '--days', '--entities', '--fast', '--federated', '--file', '--follow', '--force', '--from-pages', '--grep', '--help', '--http', '--image', '--include-expired', '--include-null-signature', '--json', '--kind', '--limit', '--max-usd', '--mcp-only', '--mode', '--multimodal', '--no-embedding', '--no-extract', '--no-federated', '--offset', '--path', '--pattern', '--pending', '--progress-interval', '--progress-json', '--query', '--quiet', '--reason', '--repo', '--reset', '--resolve', '--restore-only', '--rollup', '--save', '--session', '--session-id', '--since', '--since-last-run', '--slug', '--slugs', '--source', '--stale', '--stats', '--supersessions', '--surface', '--thin', '--timeout', '--today', '--token-ttl', '--trusted-extraction', '--url', '--watch', '--with-db', '--yes'],
|
||||
'founder': ['--aliases', '--all', '--brain', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--mcp-only', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--since', '--source', '--stale', '--supersessions', '--surface', '--thin', '--timeout', '--token-ttl', '--until'],
|
||||
'friction': ['--agent', '--brain', '--help', '--hint', '--json', '--kind', '--message', '--no-redact', '--phase', '--redact', '--run-id', '--severity', '--source', '--transcript-path', '--transcripts'],
|
||||
'friction': ['--agent', '--base', '--brain', '--compare', '--help', '--hint', '--json', '--kind', '--message', '--no-redact', '--phase', '--redact', '--run-id', '--severity', '--source', '--transcript-path', '--transcripts'],
|
||||
'frontmatter': ['--aliases', '--all', '--allow-catch-all', '--brain', '--cached', '--diff-filter', '--dry-run', '--exclude-standard', '--fast', '--fix', '--force', '--from-pages', '--get', '--help', '--http', '--include-catch-all', '--include-null-signature', '--json', '--name-only', '--name-status', '--no-embedding', '--no-extract', '--no-verify', '--others', '--pattern', '--pending', '--reset', '--resolve', '--source', '--stale', '--strategy', '--supersessions', '--surface', '--thin', '--timeout', '--token-ttl', '--uninstall', '--write-back'],
|
||||
'graph-query': ['--aliases', '--all', '--brain', '--depth', '--direction', '--explain', '--fast', '--force', '--from-pages', '--help', '--http', '--include-foreign', '--include-null-signature', '--json', '--lang', '--markdown', '--mcp-only', '--multimodal', '--near-symbol', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--surface', '--symbol-kind', '--thin', '--timeout', '--token-ttl', '--type'],
|
||||
'hook': ['--aliases', '--all', '--allow-unverified-remote', '--batch-limit', '--brain', '--budget-ms', '--cached', '--count', '--delete-brain', '--detach', '--diff-filter', '--end-of-options', '--env', '--exclude-standard', '--fast', '--force', '--from-pages', '--get', '--harness', '--help', '--http', '--include-null-signature', '--jq', '--json', '--name-only', '--no-embedding', '--no-extract', '--once', '--others', '--path', '--pattern', '--pending', '--porcelain', '--quiet', '--reset', '--resolve', '--show-current', '--show-toplevel', '--source', '--stale', '--stats', '--supersessions', '--surface', '--thin', '--timeout', '--token-ttl'],
|
||||
'import': ['--aliases', '--all', '--asof', '--background', '--brain', '--brain-wide-max-cost-usd', '--by-mention', '--cached', '--code', '--compile', '--concurrency', '--embedding-dimensions', '--embedding-model', '--exclude', '--exclude-standard', '--explain', '--fast', '--federated', '--fix', '--follow', '--force', '--force-rechunk', '--fresh', '--from-pages', '--full', '--help', '--http', '--include-gitignored', '--include-null-signature', '--json', '--lang', '--markdown', '--max-age', '--multimodal', '--name-status', '--no-embed', '--no-embedding', '--no-extract', '--no-federated', '--older-than', '--others', '--path', '--pattern', '--pending', '--pglite', '--priority', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--repo', '--reset', '--resolve', '--respect-gitignore', '--restore-only', '--since', '--skip-failed', '--source', '--source-id', '--stale', '--strategy', '--supabase', '--supersessions', '--surface', '--thin', '--timeout', '--token-ttl', '--url', '--workers'],
|
||||
'init': ['--all', '--brain', '--chat-model', '--check', '--ctx-size', '--embedding-dimensions', '--embedding-model', '--entity', '--expansion-model', '--fast', '--force', '--from-pages', '--grant-types', '--help', '--http', '--issuer-url', '--json', '--judge-model', '--key', '--mcp-only', '--mcp-url', '--migrate-only', '--model', '--multimodal', '--no', '--no-embed', '--no-embedding', '--non-interactive', '--oauth-client-id', '--oauth-client-secret', '--path', '--pglite', '--provenance', '--schema-pack', '--scopes', '--skip-embed-check', '--source', '--stale', '--supabase', '--surface', '--target', '--to', '--token-ttl', '--touchpoint', '--url', '--version'],
|
||||
'init': ['--all', '--brain', '--chat-model', '--check', '--ctx-size', '--embedding-dimensions', '--embedding-model', '--entity', '--expansion-model', '--fast', '--flag', '--force', '--from-pages', '--grant-types', '--help', '--http', '--issuer-url', '--json', '--judge-model', '--key', '--mcp-only', '--mcp-url', '--migrate-only', '--model', '--multimodal', '--no', '--no-embed', '--no-embedding', '--non-interactive', '--oauth-client-id', '--oauth-client-secret', '--path', '--pglite', '--provenance', '--schema-pack', '--scopes', '--skip-embed-check', '--source', '--stale', '--supabase', '--surface', '--to', '--token-ttl', '--touchpoint', '--url', '--version'],
|
||||
'integrations': ['--auto', '--brain', '--dry-run', '--embeddings', '--fast', '--force', '--from-pages', '--help', '--http', '--json', '--no-embedding', '--overwrite', '--refresh', '--reranking', '--source', '--surface', '--target', '--token-ttl'],
|
||||
'integrity': ['--aliases', '--all', '--auto', '--backend', '--background', '--brain', '--brain-wide-max-cost-usd', '--check', '--confidence', '--cost', '--dry-run', '--explain', '--fast', '--follow', '--force', '--fresh', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--limit', '--no-embedding', '--no-extract', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--review-lower', '--skip-bare-tweet', '--skip-urls', '--source', '--stale', '--supabase', '--supersessions', '--surface', '--thin', '--timeout', '--token-ttl', '--type', '--url'],
|
||||
'jobs': ['--abbrev-ref', '--aliases', '--all', '--allow-empty', '--allow-protected', '--allow-shell-jobs', '--apply', '--asof', '--auto', '--auto-fix', '--auto-with-prompt', '--background', '--backoff-delay', '--backoff-jitter', '--backoff-type', '--batch', '--batch-size', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--break-lock', '--budget-usd', '--budget-usd-per-day', '--by-mention', '--by-type', '--cached', '--catch-up', '--check', '--cli-path', '--cluster', '--cluster-errors', '--code', '--concurrency', '--confidence', '--confirm-destructive', '--content', '--date', '--days', '--delay', '--detach', '--diff-filter', '--dim', '--dimensions', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--empty', '--entities', '--exclude', '--exclude-standard', '--explain', '--fast', '--federated', '--federated-read', '--ff-only', '--file', '--fix', '--follow', '--force', '--force-break-lock', '--force-retry', '--format', '--fresh', '--from-meetings', '--from-pages', '--full', '--hard-deadline', '--health-interval', '--held-out', '--help', '--http', '--idempotency-key', '--image', '--include-frontmatter', '--include-gitignored', '--include-null-signature', '--infer-dates', '--inject-bootstrap', '--inline', '--input', '--install', '--interval', '--is-ancestor', '--json', '--kind', '--lang', '--limit', '--lock', '--markdown', '--max-age', '--max-attempts', '--max-cost-usd', '--max-crashes', '--max-rss', '--max-runtime-min', '--max-sources', '--max-stalled', '--max-usd', '--max-waiting', '--mcp-only', '--migrate-only', '--min-context', '--missing-path', '--mode', '--model', '--multimodal', '--name-only', '--name-status', '--near-symbol', '--ner', '--nice', '--no', '--no-auto-embed', '--no-embed', '--no-embedding', '--no-extract', '--no-federate', '--no-federated', '--no-gpg-sign', '--no-hard-deadline', '--no-inject', '--no-mutate', '--no-pull', '--no-renames', '--no-schema-pack', '--no-verify', '--no-worker', '--non-interactive', '--now', '--offset', '--older-than', '--once', '--order', '--orphan', '--others', '--output', '--override-disabled', '--pace', '--pace-max-concurrency', '--pack', '--parallel', '--params', '--path', '--pattern', '--pending', '--phase', '--pid-file', '--priority', '--progress-interval', '--progress-json', '--queue', '--quiet', '--redact-secrets', '--reenrich-after', '--refresh-cache', '--refresh-ms', '--remediate', '--remediation-plan', '--repo', '--reset', '--resolve', '--respect-gitignore', '--restore-only', '--resume', '--retry-failed', '--review-lower', '--run-id', '--save', '--segment-limit', '--serial', '--session', '--session-id', '--short', '--show-toplevel', '--sigkill-rescue', '--since', '--skip-bare-tweet', '--skip-failed', '--skip-urls', '--sleep', '--slug', '--slugs', '--source', '--source-id', '--src-subpath', '--stale', '--stats', '--status', '--strategy', '--supersessions', '--surface', '--swap-only', '--symbol-kind', '--target', '--target-score', '--thin', '--thin-threshold', '--timeout', '--timeout-ms', '--to', '--token-ttl', '--trusted-extraction', '--type', '--types', '--uninstall', '--unsafe-bypass-dream-guard', '--url', '--user', '--verbose', '--verify', '--version', '--watch', '--wedge-rescue', '--with-db', '--workers', '--yes'],
|
||||
@@ -67,7 +67,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
|
||||
'orphans': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--count', '--explain', '--follow', '--help', '--include-null-signature', '--include-pseudo', '--json', '--no-extract', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--timeout'],
|
||||
'pages': ['--aliases', '--all', '--brain', '--dry-run', '--help', '--include-null-signature', '--json', '--no-extract', '--older-than', '--pattern', '--pending', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin'],
|
||||
'pglite-repair': ['--brain', '--break-lock', '--dry-rnu', '--dry-run', '--fast', '--force', '--from-pages', '--help', '--http', '--json', '--no-embedding', '--path', '--quiet', '--source', '--surface', '--token-ttl', '--yes'],
|
||||
'post-upgrade': ['--all', '--apply-clean-hunks', '--brain', '--check', '--code', '--compile', '--concurrency', '--detail', '--dim', '--embedding-dimensions', '--embedding-model', '--fast', '--ff-only', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--format', '--from-pages', '--help', '--host-dir', '--http', '--inject-bootstrap', '--inline', '--install', '--interval', '--json', '--limit', '--list', '--markdown', '--max-rss', '--migrate-only', '--migration', '--mode', '--model', '--multimodal', '--name-only', '--no', '--no-autopilot-install', '--no-embed', '--no-embedding', '--no-inject', '--no-worker', '--non-interactive', '--now', '--path', '--pglite', '--quiet', '--repo', '--reset', '--since', '--skills-dir', '--skip-verify', '--source', '--stale', '--status', '--supabase', '--surface', '--swap-only', '--target', '--to', '--token-ttl', '--uninstall', '--user', '--verbose', '--verify', '--version', '--workers', '--yes'],
|
||||
'post-upgrade': ['--all', '--apply-clean-hunks', '--brain', '--check', '--code', '--compile', '--concurrency', '--detail', '--dim', '--embedding-dimensions', '--embedding-model', '--fast', '--ff-only', '--flag', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--format', '--from-pages', '--help', '--host-dir', '--http', '--inject-bootstrap', '--inline', '--install', '--interval', '--json', '--limit', '--list', '--markdown', '--max-rss', '--migrate-only', '--migration', '--mode', '--model', '--multimodal', '--name-only', '--no', '--no-autopilot-install', '--no-embed', '--no-embedding', '--no-inject', '--no-worker', '--non-interactive', '--now', '--path', '--pglite', '--quiet', '--repo', '--reset', '--since', '--skills-dir', '--skip-verify', '--source', '--stale', '--status', '--supabase', '--surface', '--swap-only', '--target', '--to', '--token-ttl', '--uninstall', '--user', '--verbose', '--verify', '--version', '--workers', '--yes'],
|
||||
'protocol': ['--all', '--allow-empty', '--apply', '--asof', '--auto', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-usd-per-day', '--by-mention', '--content', '--date', '--days', '--entities', '--fast', '--federated', '--file', '--follow', '--force', '--from-pages', '--help', '--http', '--image', '--json', '--kind', '--limit', '--max-usd', '--mode', '--multimodal', '--no-embedding', '--offset', '--path', '--progress-interval', '--progress-json', '--quiet', '--save', '--session', '--session-id', '--since', '--slug', '--slugs', '--source', '--stats', '--surface', '--synthesize', '--target', '--timeout', '--token', '--token-ttl', '--trusted-extraction', '--url', '--with-db', '--yes'],
|
||||
'providers': ['--brain', '--ctx-size', '--embedding-dimensions', '--embedding-model', '--embeddings', '--fast', '--force', '--from-pages', '--help', '--http', '--json', '--model', '--multimodal', '--no', '--no-embed', '--no-embedding', '--reranking', '--source', '--surface', '--token-ttl', '--touchpoint', '--version'],
|
||||
'publish': ['--accent', '--bg', '--border', '--brain', '--card-bg', '--code-bg', '--error', '--fg', '--help', '--json', '--link', '--muted', '--out', '--password', '--source', '--title'],
|
||||
@@ -78,7 +78,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
|
||||
'reindex-code': ['--abi', '--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--chunker-debug', '--code', '--compile', '--concurrency', '--dry-run', '--embedding-dimensions', '--embedding-model', '--explain', '--fix', '--follow', '--force', '--force-rechunk', '--help', '--include-null-signature', '--json', '--judge-model', '--lang', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-runtime', '--model', '--multimodal', '--no', '--no-embed', '--no-extract', '--older-than', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--serial', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--version', '--workers', '--yes'],
|
||||
'reindex-frontmatter': ['--aliases', '--all', '--brain', '--concurrency', '--dry-run', '--force', '--help', '--include-null-signature', '--json', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--slug-prefix', '--source', '--stale', '--supersessions', '--thin', '--workers', '--yes'],
|
||||
'reindex-search-vector': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--dry-run', '--explain', '--follow', '--help', '--include-null-signature', '--json', '--migrate-only', '--no-extract', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--yes'],
|
||||
'reinit-pglite': ['--abbrev-ref', '--all', '--allow-empty', '--brain', '--break-lock', '--chat-model', '--concurrency', '--confirm-destructive', '--diff-filter', '--dir', '--embedding-dimensions', '--embedding-model', '--empty', '--entity', '--exclude', '--exclude-standard', '--expansion-model', '--fast', '--force', '--force-break-lock', '--format', '--from-pages', '--full', '--grant-types', '--hard-deadline', '--help', '--http', '--include-gitignored', '--interval', '--is-ancestor', '--issuer-url', '--json', '--key', '--lock', '--max-age', '--max-sources', '--mcp-only', '--mcp-url', '--migrate-only', '--missing-path', '--model', '--name-only', '--name-status', '--no-auto-embed', '--no-embed', '--no-embedding', '--no-extract', '--no-gpg-sign', '--no-hard-deadline', '--no-pull', '--no-renames', '--no-schema-pack', '--no-sync', '--no-verify', '--non-interactive', '--oauth-client-id', '--oauth-client-secret', '--orphan', '--parallel', '--path', '--pglite', '--priority', '--provenance', '--quiet', '--repo', '--retry-failed', '--schema-pack', '--scopes', '--serial', '--short', '--show-toplevel', '--skip-embed-check', '--skip-failed', '--slugs', '--source', '--source-id', '--src-subpath', '--stale', '--strategy', '--supabase', '--surface', '--target', '--timeout', '--to', '--token-ttl', '--url', '--verify', '--version', '--watch', '--workers', '--yes'],
|
||||
'reinit-pglite': ['--abbrev-ref', '--all', '--allow-empty', '--brain', '--break-lock', '--chat-model', '--concurrency', '--confirm-destructive', '--diff-filter', '--dir', '--embedding-dimensions', '--embedding-model', '--empty', '--entity', '--exclude', '--exclude-standard', '--expansion-model', '--fast', '--force', '--force-break-lock', '--format', '--from-pages', '--full', '--grant-types', '--hard-deadline', '--help', '--http', '--include-gitignored', '--interval', '--is-ancestor', '--issuer-url', '--json', '--key', '--lock', '--max-age', '--max-sources', '--mcp-only', '--mcp-url', '--migrate-only', '--missing-path', '--model', '--name-only', '--name-status', '--no-auto-embed', '--no-embed', '--no-embedding', '--no-extract', '--no-gpg-sign', '--no-hard-deadline', '--no-pull', '--no-renames', '--no-schema-pack', '--no-sync', '--no-verify', '--non-interactive', '--oauth-client-id', '--oauth-client-secret', '--orphan', '--parallel', '--path', '--pglite', '--priority', '--provenance', '--quiet', '--repo', '--retry-failed', '--schema-pack', '--scopes', '--serial', '--short', '--show-toplevel', '--skip-embed-check', '--skip-failed', '--slugs', '--source', '--source-id', '--src-subpath', '--stale', '--strategy', '--supabase', '--surface', '--timeout', '--to', '--token-ttl', '--url', '--verify', '--version', '--watch', '--workers', '--yes'],
|
||||
'remote': ['--all', '--background', '--brain', '--break-lock', '--by-mention', '--column', '--concurrency', '--content-audit', '--count', '--detach', '--dim', '--dir', '--drain', '--embedding-dimensions', '--embedding-model', '--fast', '--fix', '--force', '--force-retry', '--force-schema', '--from-pages', '--full', '--help', '--http', '--include-flagged', '--index-audit', '--json', '--locks', '--markdown', '--max-cost', '--max-cost-usd', '--max-jobs', '--max-rss', '--max-usd', '--mcp-only', '--migrate-only', '--multimodal', '--nice', '--no-embedding', '--older-than', '--parallel', '--params', '--path', '--pglite', '--phase', '--pid-file', '--porcelain', '--probe-pglite', '--progress-json', '--query', '--queue', '--rebuild-rollup', '--regenerate', '--remediate', '--remediation-plan', '--reset', '--resume', '--scope', '--scopes', '--since', '--skills-dir', '--skip-failed', '--slugs', '--source', '--source-id', '--stale', '--status', '--supabase', '--surface', '--target', '--target-score', '--timeout', '--to', '--token-ttl', '--top-k', '--url', '--window', '--workers', '--yes'],
|
||||
'repair-jsonb': ['--background', '--brain', '--brain-wide-max-cost-usd', '--dry-run', '--explain', '--fast', '--follow', '--force', '--from-pages', '--help', '--http', '--json', '--lang', '--markdown', '--multimodal', '--near-symbol', '--no-embedding', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--restore-only', '--source', '--stale', '--supabase', '--surface', '--symbol-kind', '--thin', '--timeout', '--token-ttl', '--url'],
|
||||
'report': ['--brain', '--content', '--dir', '--help', '--json', '--source', '--title', '--type'],
|
||||
@@ -103,7 +103,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
|
||||
'takes': ['--aliases', '--all', '--brain', '--bucket-size', '--by', '--claim', '--dir', '--domain', '--dry-run', '--evidence', '--expired', '--fast', '--federated', '--force', '--from-pages', '--help', '--holder', '--http', '--include-covered', '--include-null-signature', '--json', '--kind', '--limit', '--max-pages', '--no-embedding', '--no-extract', '--no-federated', '--outcome', '--path', '--pattern', '--pending', '--quality', '--refresh', '--repo', '--reset', '--resolve', '--restore-only', '--row', '--since', '--slugs', '--sort', '--source', '--source-id', '--stale', '--supersessions', '--surface', '--thin', '--token-ttl', '--unit', '--until', '--value', '--weight', '--who', '--yes'],
|
||||
'think': ['--aliases', '--all', '--anchor', '--brain', '--calibration-holder', '--explain', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--max-usd', '--mcp-only', '--model', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--rounds', '--save', '--since', '--source', '--stale', '--supersessions', '--surface', '--take', '--thin', '--timeout', '--token-ttl', '--until', '--with-calibration'],
|
||||
'transcripts': ['--aliases', '--all', '--brain', '--days', '--full', '--help', '--include-null-signature', '--json', '--limit', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--timeout'],
|
||||
'upgrade': ['--all', '--apply-clean-hunks', '--brain', '--check', '--code', '--compile', '--concurrency', '--detail', '--dim', '--embedding-dimensions', '--embedding-model', '--fast', '--ff-only', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--format', '--from-pages', '--help', '--host-dir', '--http', '--inject-bootstrap', '--inline', '--install', '--interval', '--json', '--limit', '--list', '--markdown', '--max-rss', '--migrate-only', '--migration', '--mode', '--model', '--multimodal', '--name-only', '--no', '--no-autopilot-install', '--no-embed', '--no-embedding', '--no-inject', '--no-worker', '--non-interactive', '--now', '--path', '--pglite', '--quiet', '--repo', '--reset', '--since', '--skills-dir', '--skip-verify', '--source', '--stale', '--status', '--supabase', '--surface', '--swap-only', '--target', '--to', '--token-ttl', '--uninstall', '--user', '--verbose', '--verify', '--version', '--workers', '--yes'],
|
||||
'upgrade': ['--all', '--apply-clean-hunks', '--brain', '--check', '--code', '--compile', '--concurrency', '--detail', '--dim', '--embedding-dimensions', '--embedding-model', '--fast', '--ff-only', '--flag', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--format', '--from-pages', '--help', '--host-dir', '--http', '--inject-bootstrap', '--inline', '--install', '--interval', '--json', '--limit', '--list', '--markdown', '--max-rss', '--migrate-only', '--migration', '--mode', '--model', '--multimodal', '--name-only', '--no', '--no-autopilot-install', '--no-embed', '--no-embedding', '--no-inject', '--no-worker', '--non-interactive', '--now', '--path', '--pglite', '--quiet', '--repo', '--reset', '--since', '--skills-dir', '--skip-verify', '--source', '--stale', '--status', '--supabase', '--surface', '--swap-only', '--target', '--to', '--token-ttl', '--uninstall', '--user', '--verbose', '--verify', '--version', '--workers', '--yes'],
|
||||
'watch': ['--aliases', '--all', '--brain', '--fast', '--federated', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--max-pages', '--min-confidence', '--no-embedding', '--no-extract', '--no-federated', '--path', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--stats', '--supersessions', '--surface', '--thin', '--token-ttl', '--window-turns'],
|
||||
'ze-switch': ['--aliases', '--all', '--brain', '--confirm-reembed', '--dry-run', '--force', '--help', '--ignore-env-override', '--ignore-missing-key', '--include-null-signature', '--json', '--markdown', '--no-extract', '--non-interactive', '--pattern', '--pending', '--reset', '--resolve', '--resume', '--source', '--stale', '--supersessions', '--thin', '--undo', '--yes'],
|
||||
};
|
||||
|
||||
+14
-4
@@ -1,11 +1,13 @@
|
||||
/**
|
||||
* Friction reporter — JSONL-backed signal capture for the claw-test feedback loop.
|
||||
*
|
||||
* The friction CLI (`gbrain friction log/render/list/summary`) writes here.
|
||||
* The claw-test harness reads here. The agent calls `gbrain friction log`
|
||||
* directly when it hits something confusing, missing, or wrong.
|
||||
* The friction CLI (`gbrain friction log/render/list/summary/diff`) writes
|
||||
* and reads here. The claw-test harness reads here. The agent calls
|
||||
* `gbrain friction log` directly when it hits something confusing, missing,
|
||||
* or wrong; `friction diff` compares two runs cross-agent.
|
||||
*
|
||||
* Storage shape: append-only JSONL files under `$GBRAIN_HOME/friction/`.
|
||||
* Storage shape: append-only JSONL files under `$GBRAIN_HOME/.gbrain/friction/`
|
||||
* (configDir() appends the '.gbrain' segment).
|
||||
* - `<run-id>.jsonl` for each harness run (run-id from $GBRAIN_FRICTION_RUN_ID)
|
||||
* - `standalone.jsonl` for entries logged outside a harness run
|
||||
*
|
||||
@@ -61,6 +63,10 @@ export interface FrictionEntry {
|
||||
transcript_offset?: number;
|
||||
/** For phase-marker entries only. */
|
||||
marker?: PhaseMarker;
|
||||
/** Scenario name the run executed (stamped on the run-start marker). */
|
||||
scenario?: string;
|
||||
/** Harness meta-record schema version (stamped on the run-start marker). */
|
||||
harness_schema?: number;
|
||||
}
|
||||
|
||||
export interface FrictionLogInput {
|
||||
@@ -74,6 +80,8 @@ export interface FrictionLogInput {
|
||||
agent?: string;
|
||||
transcriptOffset?: number;
|
||||
marker?: PhaseMarker;
|
||||
scenario?: string;
|
||||
harnessSchema?: number;
|
||||
/** When the writer is called from the harness wrapping a child error. */
|
||||
errorClass?: string;
|
||||
errorCode?: string;
|
||||
@@ -141,6 +149,8 @@ export function logFriction(input: FrictionLogInput): void {
|
||||
if (input.agent) entry.agent = input.agent;
|
||||
if (input.transcriptOffset !== undefined) entry.transcript_offset = input.transcriptOffset;
|
||||
if (input.marker) entry.marker = input.marker;
|
||||
if (input.scenario) entry.scenario = input.scenario;
|
||||
if (input.harnessSchema !== undefined) entry.harness_schema = input.harnessSchema;
|
||||
|
||||
const line = JSON.stringify(entry) + '\n';
|
||||
appendFileSync(frictionFile(runId), line, 'utf-8');
|
||||
|
||||
+76
-16
@@ -10,6 +10,26 @@ import {
|
||||
} from './retry-matcher.ts';
|
||||
import { repairTimelineDedupIndex } from './timeline-dedup-repair.ts';
|
||||
|
||||
/**
|
||||
* When true, per-migration explanatory notices (e.g. the v123/v124 "here is
|
||||
* what this migration changed" lines that specific handlers write to stderr)
|
||||
* are suppressed. Set by runMigrations for a FRESH-install full replay — those
|
||||
* notices are useful diagnostics on an UPGRADE but pure noise as a new user's
|
||||
* first-run output. Module-level (not threaded through the Migration type)
|
||||
* because only a couple of handlers emit them. Guarded via `migrationNotice`.
|
||||
* Known limitation: concurrent runMigrations calls in one process (two engines
|
||||
* migrating simultaneously) share this flag — worst case is a suppressed or
|
||||
* extra stderr NOTICE line; migration execution/stamping is unaffected.
|
||||
*/
|
||||
let quietMigrationNotices = false;
|
||||
|
||||
/** Write a per-migration explanatory notice unless fresh-install quiet mode is
|
||||
* on. Handlers should route their "what changed" lines through this. */
|
||||
function migrationNotice(line: string): void {
|
||||
if (quietMigrationNotices) return;
|
||||
process.stderr.write(line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schema migrations — run automatically on initSchema().
|
||||
*
|
||||
@@ -5511,7 +5531,7 @@ export const MIGRATIONS: Migration[] = [
|
||||
// stderr, NOT stdout: migrations run lazily inside any command's
|
||||
// first DB connect — a console.log here polluted `doctor --json`
|
||||
// stdout and broke jq consumers (heavy-tests fm_wallclock).
|
||||
process.stderr.write(` v123: trigger functions recreated with language='english' (default — no backfill needed)\n`);
|
||||
migrationNotice(` v123: trigger functions recreated with language='english' (default — no backfill needed)\n`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -5532,7 +5552,7 @@ export const MIGRATIONS: Migration[] = [
|
||||
WHERE search_vector IS NOT NULL;
|
||||
`);
|
||||
|
||||
process.stderr.write(` v123: trigger functions recreated with language='${lang}' + backfilled existing rows\n`);
|
||||
migrationNotice(` v123: trigger functions recreated with language='${lang}' + backfilled existing rows\n`);
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -5600,7 +5620,7 @@ export const MIGRATIONS: Migration[] = [
|
||||
END;
|
||||
$fn$ LANGUAGE plpgsql;
|
||||
`);
|
||||
process.stderr.write(` v124: update_page_search_vector() no longer indexes compiled_truth (was overflowing tsvector on large pages, #2704)
|
||||
migrationNotice(` v124: update_page_search_vector() no longer indexes compiled_truth (was overflowing tsvector on large pages, #2704)
|
||||
`);
|
||||
},
|
||||
},
|
||||
@@ -6012,17 +6032,64 @@ export async function runMigrations(engine: BrainEngine): Promise<{ applied: num
|
||||
return { applied: 0, current };
|
||||
}
|
||||
|
||||
// Fresh install vs upgrade: a never-migrated brain (schema blob seeds
|
||||
// version='1'; every migration is >= 2) replays the FULL history — printing
|
||||
// ~240 lines of internal migration names as the user's first-run experience.
|
||||
// That wall makes a 2-second init read as complex and fragile ("1 → 125"
|
||||
// implies the brand-new install was 124 versions stale). Fresh installs get
|
||||
// one summary line; EXISTING brains keep the full per-migration detail
|
||||
// (upgrades are where the names carry diagnostic value).
|
||||
// GBRAIN_MIGRATE_VERBOSE=1 is the incident escape hatch (env-first, matching
|
||||
// the GBRAIN_SYNC_*/GBRAIN_PACE_* pattern).
|
||||
const freshInstall = current <= 1 && pending.length === sorted.length;
|
||||
const quietReplay = freshInstall && process.env.GBRAIN_MIGRATE_VERBOSE !== '1';
|
||||
// Suppress per-migration explanatory notices during a fresh-install replay
|
||||
// (they are upgrade diagnostics, noise on a new user's first run). Restored
|
||||
// in the finally so an in-process upgrade after a fresh init still narrates.
|
||||
quietMigrationNotices = quietReplay;
|
||||
|
||||
// Progress messages route to stderr so callers parsing stdout (e.g.
|
||||
// `gbrain jobs submit --json | jq`) aren't polluted by migration noise.
|
||||
process.stderr.write(` Schema version ${current} → ${LATEST_VERSION} (${pending.length} migration(s) pending)\n`);
|
||||
|
||||
// Pre-flight: warn about connections that might block DDL
|
||||
await checkForBlockingConnections(engine);
|
||||
if (quietReplay) {
|
||||
process.stderr.write(` Setting up brain schema (v${LATEST_VERSION})...\n`);
|
||||
} else {
|
||||
process.stderr.write(` Schema version ${current} → ${LATEST_VERSION} (${pending.length} migration(s) pending)\n`);
|
||||
}
|
||||
|
||||
let applied = 0;
|
||||
for (const m of pending) {
|
||||
process.stderr.write(` [${m.version}] ${m.name}...\n`);
|
||||
try {
|
||||
// Pre-flight: warn about connections that might block DDL
|
||||
await checkForBlockingConnections(engine);
|
||||
|
||||
for (const m of pending) {
|
||||
if (!quietReplay) process.stderr.write(` [${m.version}] ${m.name}...\n`);
|
||||
try {
|
||||
await applyOneMigration(engine, m);
|
||||
// Update version after both SQL and handler succeed. Inside the same
|
||||
// catch so a stamp-write failure is also NAMED in quiet mode.
|
||||
await engine.setConfig('version', String(m.version));
|
||||
} catch (err) {
|
||||
// Quiet fresh-install replay: name the failing migration — without the
|
||||
// per-step lines, the error would otherwise be anonymous.
|
||||
if (quietReplay) process.stderr.write(` [${m.version}] ${m.name} failed\n`);
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (!quietReplay) process.stderr.write(` [${m.version}] ✓ ${m.name}\n`);
|
||||
applied++;
|
||||
}
|
||||
} finally {
|
||||
// Never leak the fresh-install quiet flag into a later in-process run —
|
||||
// covers every exit path from here on (incl. the pre-flight probe).
|
||||
quietMigrationNotices = false;
|
||||
}
|
||||
|
||||
return { applied, current: LATEST_VERSION };
|
||||
}
|
||||
|
||||
/** One migration's full body (SQL + handler + verify), extracted so the
|
||||
* runMigrations loop can name the failing migration in quiet-replay mode. */
|
||||
async function applyOneMigration(engine: BrainEngine, m: Migration): Promise<void> {
|
||||
// Pick SQL: engine-specific `sqlFor` wins over engine-agnostic `sql`.
|
||||
const sql = m.sqlFor?.[engine.kind] ?? m.sql;
|
||||
|
||||
@@ -6093,11 +6160,4 @@ export async function runMigrations(engine: BrainEngine): Promise<{ applied: num
|
||||
}
|
||||
}
|
||||
|
||||
// Update version after both SQL and handler succeed
|
||||
await engine.setConfig('version', String(m.version));
|
||||
process.stderr.write(` [${m.version}] ✓ ${m.name}\n`);
|
||||
applied++;
|
||||
}
|
||||
|
||||
return { applied, current: LATEST_VERSION };
|
||||
}
|
||||
|
||||
@@ -43,6 +43,9 @@ export async function runInitNudge(engine: BrainEngine): Promise<void> {
|
||||
let linkedCount = 0;
|
||||
let timelineCount = 0;
|
||||
let takesCount = 0;
|
||||
// -1 = the page-count probe failed: fail-open sentinel, treat as non-empty
|
||||
// so current behavior is preserved when the count is unknown.
|
||||
let totalPages = -1;
|
||||
let checksRan = 0;
|
||||
let checksAttempted = 0;
|
||||
let partial = false;
|
||||
@@ -82,6 +85,11 @@ export async function runInitNudge(engine: BrainEngine): Promise<void> {
|
||||
[],
|
||||
{ signal: controller.signal },
|
||||
),
|
||||
engine.executeRaw<{ count: string | number }>(
|
||||
`SELECT COUNT(*) AS count FROM pages WHERE deleted_at IS NULL`,
|
||||
[],
|
||||
{ signal: controller.signal },
|
||||
),
|
||||
]);
|
||||
clearTimeout(timer);
|
||||
|
||||
@@ -99,8 +107,16 @@ export async function runInitNudge(engine: BrainEngine): Promise<void> {
|
||||
else if (i === 2) linkedCount = n;
|
||||
else if (i === 3) timelineCount = n;
|
||||
else if (i === 4) takesCount = n;
|
||||
else if (i === 5) totalPages = n;
|
||||
}
|
||||
|
||||
// A brand-new EMPTY brain has no "opportunities" — telling a fresh user
|
||||
// "0 takes" at the end of their first init is jargon-noise on the
|
||||
// activation surface. Suppress the ENTIRE nudge on empty (including the
|
||||
// partial-checks notice below).
|
||||
const brainEmpty = totalPages === 0;
|
||||
if (brainEmpty) return;
|
||||
|
||||
// Aggregate: any non-zero metric triggers the nudge.
|
||||
const linkCoverage = totalEntities > 0 ? linkedCount / totalEntities : 1;
|
||||
const timelineCoverage = totalEntities > 0 ? timelineCount / totalEntities : 1;
|
||||
@@ -109,7 +125,6 @@ export async function runInitNudge(engine: BrainEngine): Promise<void> {
|
||||
|| (totalEntities > 0 && linkCoverage < 0.7)
|
||||
|| (totalEntities > 0 && timelineCoverage < 0.9)
|
||||
|| takesCount === 0;
|
||||
|
||||
if (!hasRecommendations && !partial) return;
|
||||
|
||||
// Emit one-line nudge. Be terse — init is the activation surface.
|
||||
@@ -123,6 +138,13 @@ export async function runInitNudge(engine: BrainEngine): Promise<void> {
|
||||
}
|
||||
if (takesCount === 0) parts.push('0 takes');
|
||||
|
||||
if (parts.length === 0 && partial) {
|
||||
process.stderr.write(
|
||||
`\n[onboard] Init checks incomplete (${checksRan}/${checksAttempted}) — run 'gbrain onboard --check' for full recommendations.\n`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
process.stderr.write(
|
||||
`\n[onboard] Brain has opportunities: ${parts.join(', ')}.\n` +
|
||||
`[onboard] Run 'gbrain onboard --check' to see the plan.` +
|
||||
|
||||
@@ -2776,10 +2776,12 @@ const get_brain_identity: Operation = {
|
||||
let latest_version: string | null = null;
|
||||
try {
|
||||
const su = await import('./self-upgrade.ts');
|
||||
const entry = su.readUpdateCache();
|
||||
if (entry && su.isCacheFresh(entry, Date.now()) && entry.marker.kind === 'upgrade_available') {
|
||||
// Shared stale/foreign-cache guard (pendingUpgradeVersion): only an
|
||||
// upgrade strictly newer than the RUNNING version counts.
|
||||
const latest = su.pendingUpgradeVersion(VERSION, Date.now());
|
||||
if (latest) {
|
||||
update_available = true;
|
||||
latest_version = entry.marker.latest ?? null;
|
||||
latest_version = latest;
|
||||
}
|
||||
} catch {
|
||||
/* never let the banner break the op */
|
||||
|
||||
@@ -252,6 +252,8 @@ export async function acquireLock(dataDir: string | undefined, opts?: { timeoutM
|
||||
`GBrain's local database is already open through \`gbrain serve\` (MCP, PID ${lockPid}). ` +
|
||||
`This brain uses PGLite, so a separate CLI process cannot open it at the same time. ` +
|
||||
`Stop \`gbrain serve\`, then retry this CLI command. ` +
|
||||
`(\`gbrain serve\` is usually spawned by your agent harness — close or exit that ` +
|
||||
`Claude Code/Codex session to release the database.) ` +
|
||||
`Or keep it running and use its MCP tools instead. ` +
|
||||
`A process with the recorded PID is still running, so GBrain will not remove ${lockDir} automatically.`,
|
||||
);
|
||||
|
||||
@@ -30,7 +30,7 @@ import { closeSync, mkdirSync, openSync, readFileSync, renameSync, statSync, unl
|
||||
import { dirname, join } from 'node:path';
|
||||
import { gbrainPath } from './config.ts';
|
||||
import { acquirePackLock, type PackLockOpts } from './schema-pack/pack-lock.ts';
|
||||
import { isValidVersionString, parseSemver, semverGt, semverLte } from './semver.ts';
|
||||
import { isNewerVersion, isValidVersionString, parseSemver, semverGt, semverLte } from './semver.ts';
|
||||
|
||||
// ── Constants ───────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -322,6 +322,29 @@ export function isCacheFresh(entry: CacheEntry, now: number): boolean {
|
||||
return now - entry.mtimeMs < ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* The one shared "is an upgrade actually pending for THIS binary?" predicate.
|
||||
* Returns the latest version string when the cache is present, fresh, marks an
|
||||
* upgrade, AND that upgrade is strictly newer than the RUNNING binary — else
|
||||
* null. The running-version comparison is the load-bearing part: the cache
|
||||
* records the version of whatever binary WROTE it (an older gbrain on PATH can
|
||||
* write it via the detached refresh), so consumers must never trust
|
||||
* `marker.current` to describe themselves. Every upgrade-nag surface (CLI
|
||||
* startup marker, doctor, advisor, get_brain_identity) routes through here so
|
||||
* the suppression rule cannot drift per-surface. Never throws.
|
||||
*/
|
||||
export function pendingUpgradeVersion(runningVersion: string, now: number = Date.now()): string | null {
|
||||
try {
|
||||
const entry = readUpdateCache();
|
||||
if (!entry || !isCacheFresh(entry, now)) return null;
|
||||
if (entry.marker.kind !== 'upgrade_available' || !entry.marker.latest) return null;
|
||||
if (!isNewerVersion(runningVersion, entry.marker.latest)) return null;
|
||||
return entry.marker.latest;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Snooze (interactive prompting only; never overrides mode=off) ────────────
|
||||
|
||||
export function readSnooze(): SnoozeRecord | null {
|
||||
|
||||
@@ -118,6 +118,13 @@ function buildAdvisoryWithoutWorkspace(
|
||||
/**
|
||||
* Print the advisory to stderr at the end of init / post-upgrade.
|
||||
* No-op when buildAdvisory returns null.
|
||||
*
|
||||
* `init` prints a COMPACT 3-line pointer: the init success screen already
|
||||
* competes for one primary action (the memory-verbs funnel), and the full
|
||||
* 55-line agent-addressed banner buried it. The full banner remains the
|
||||
* `upgrade` surface (its designed audience) and stays available any time
|
||||
* via `gbrain advisor`. buildAdvisory itself is unchanged — it is the
|
||||
* agent-readable document, pinned by tests and shared with `gbrain advisor`.
|
||||
*/
|
||||
export function printAdvisoryIfRecommended(opts: {
|
||||
version: string;
|
||||
@@ -125,7 +132,51 @@ export function printAdvisoryIfRecommended(opts: {
|
||||
targetWorkspace?: string | null;
|
||||
targetSkillsDir?: string | null;
|
||||
}): void {
|
||||
const advisory = buildAdvisory(opts);
|
||||
if (!advisory) return;
|
||||
process.stderr.write(advisory);
|
||||
// Fail-open: this is decoration on the init success screen and runs AFTER
|
||||
// the brain is created (and, since the memory-verbs quickstart now prints
|
||||
// last, BEFORE it). An unreadable RESOLVER.md must never throw here and
|
||||
// starve the primary CTA — same posture as runInitNudge.
|
||||
try {
|
||||
const advisory = buildAdvisory(opts);
|
||||
if (!advisory) return;
|
||||
if (opts.context === 'init') {
|
||||
// Derive the counts for the compact form from the same detection the
|
||||
// full banner used. Detection is hoisted OUT of the filter (one receipt
|
||||
// read+parse total, matching buildAdvisory's own pattern).
|
||||
let workspace = opts.targetWorkspace ?? null;
|
||||
let skillsDir = opts.targetSkillsDir ?? null;
|
||||
if (!skillsDir) {
|
||||
const detected = autoDetectSkillsDir();
|
||||
if (detected.dir) {
|
||||
skillsDir = detected.dir;
|
||||
if (!workspace) workspace = resolvePath(skillsDir, '..');
|
||||
}
|
||||
}
|
||||
const all = currentRecommendedSet();
|
||||
const installed = workspace && skillsDir ? detectInstalledSlugs(skillsDir, workspace) : null;
|
||||
const missing = installed ? all.filter((s) => !installed.has(s.slug)) : all;
|
||||
if (missing.length === 0) return;
|
||||
const names = missing.map((s) => s.slug);
|
||||
const preview = names.slice(0, 4).join(', ') + (names.length > 4 ? ', …' : '');
|
||||
// No workspace detected → scaffold has no target; say so (the full
|
||||
// banner carries the same caveat via workspaceNotDetected).
|
||||
const noWorkspace = installed === null;
|
||||
// Human-voiced (prints on the init success screen where a person may read
|
||||
// it) — no `[AGENT]` stage-direction leaking to the human. An agent reading
|
||||
// the same line still knows the command to offer.
|
||||
process.stderr.write(
|
||||
`\n${missing.length} recommended skill(s) not installed yet (${preview}).\n` +
|
||||
// NOTE: no bare `--flag` tokens in this string — the flag-registry
|
||||
// generator harvests them from source strings and would register a
|
||||
// phantom flag on every command that imports this module.
|
||||
(noWorkspace
|
||||
? `Open your agent workspace first (scaffold needs a target), then \`${scaffoldCommandFor(missing, all)}\`; full list: gbrain advisor\n`
|
||||
: `Ask me to run \`${scaffoldCommandFor(missing, all)}\`, or see the full list: gbrain advisor\n`),
|
||||
);
|
||||
return;
|
||||
}
|
||||
process.stderr.write(advisory);
|
||||
} catch {
|
||||
/* advisory is best-effort decoration — never break init */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@
|
||||
"consent": true,
|
||||
"phase": "engine",
|
||||
"persist": false,
|
||||
"question": "Optional: one API key (OpenAI, Anthropic, or Voyage) unlocks semantic search and automatic fact extraction. With no key, I run keyless: keyword search plus memory I write down myself — everything still works. Paste a key or say skip.",
|
||||
"question": "Optional: one API key upgrades me — OpenAI (semantic search + automatic fact extraction), Voyage (semantic search), or Anthropic (fact extraction). With no key, I run keyless: keyword search plus memory I write down myself — everything still works. Paste a key or say skip.",
|
||||
"default": "",
|
||||
"sink": "config",
|
||||
"maxLength": 256
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# gbrain agent workspace — template
|
||||
|
||||
<!-- gbrain-template-stamp: 0.45.10.0 -->
|
||||
<!-- gbrain-template-stamp: 0.45.12.0 -->
|
||||
|
||||
This repository is the **"Use this template"** distribution artifact for a
|
||||
[gbrain](https://github.com/garrytan/gbrain) personal-agent workspace — the same
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Unit tests for src/core/ai/probes.ts (probeOpenAICompat).
|
||||
*
|
||||
* Runs against a local Bun.serve fixture on an ephemeral port (port: 0) so
|
||||
* no real daemon is needed. Pins:
|
||||
* - models extraction from a valid {object:'list', data:[...]} body, with
|
||||
* non-string / missing ids filtered out
|
||||
* - non-list JSON body → models_endpoint_valid false, models undefined
|
||||
* - connection refused → reachable false
|
||||
*/
|
||||
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { probeOpenAICompat } from '../src/core/ai/probes.ts';
|
||||
|
||||
describe('probeOpenAICompat — models extraction', () => {
|
||||
test('valid list body extracts string ids only', async () => {
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch() {
|
||||
return Response.json({ object: 'list', data: [{ id: 'm1' }, { id: 42 }, {}] });
|
||||
},
|
||||
});
|
||||
try {
|
||||
const r = await probeOpenAICompat(`http://127.0.0.1:${server.port}`);
|
||||
expect(r.reachable).toBe(true);
|
||||
expect(r.models_endpoint_valid).toBe(true);
|
||||
expect(r.models).toEqual(['m1']);
|
||||
} finally {
|
||||
server.stop(true);
|
||||
}
|
||||
});
|
||||
|
||||
test('non-list JSON body → valid false, models undefined', async () => {
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch() {
|
||||
return Response.json({ hello: 'world' });
|
||||
},
|
||||
});
|
||||
try {
|
||||
const r = await probeOpenAICompat(`http://127.0.0.1:${server.port}`);
|
||||
expect(r.reachable).toBe(true);
|
||||
expect(r.models_endpoint_valid).toBe(false);
|
||||
expect(r.models).toBeUndefined();
|
||||
} finally {
|
||||
server.stop(true);
|
||||
}
|
||||
});
|
||||
|
||||
test('connection refused → reachable false', async () => {
|
||||
// Grab an ephemeral port by binding, then release it before probing so
|
||||
// the connection is refused (nothing else claims the port that fast).
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch() {
|
||||
return new Response('unused');
|
||||
},
|
||||
});
|
||||
const port = server.port;
|
||||
try {
|
||||
await server.stop(true);
|
||||
const r = await probeOpenAICompat(`http://127.0.0.1:${port}`);
|
||||
expect(r.reachable).toBe(false);
|
||||
expect(r.error).toBeDefined();
|
||||
} finally {
|
||||
server.stop(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -22,6 +22,7 @@ import { runBootstrap, workspaceBrainStats } from '../src/commands/bootstrap.ts'
|
||||
import type { ExecRunner } from '../src/core/bootstrap/repo.ts';
|
||||
import { attachWorkspace } from '../src/core/bootstrap/attach.ts';
|
||||
import { readReceipt, receiptPath, writeManifest, type InstallReceipt } from '../src/core/bootstrap/format.ts';
|
||||
import { GBRAIN_HOOK_MARKER_KEY, GBRAIN_HOOK_MARKER_VALUE } from '../src/core/bootstrap/host-specs.ts';
|
||||
import { deriveWorkspaceSourceId } from '../src/core/bootstrap/verify.ts';
|
||||
import { initState, setAnswer, skipAnswer, confirm, readBackHash } from '../src/core/bootstrap/interview.ts';
|
||||
|
||||
@@ -578,13 +579,43 @@ describe('MCP registration verification [FIX7]', () => {
|
||||
const r = await runHooks(runner);
|
||||
expect(r.result).toBe(0);
|
||||
expect(r.err).toContain('targets a DIFFERENT workspace');
|
||||
expect(calls.some((c) => c[1] === 'mcp' && c[2] === 'remove' && c[3] === 'gbrain')).toBe(true);
|
||||
// The remove must be SCOPED on claude-code: a scope-less remove can resolve
|
||||
// to a different scope's registration and leave the blocker in place.
|
||||
const removes = calls.filter((c) => c[1] === 'mcp' && c[2] === 'remove' && c[3] === 'gbrain');
|
||||
expect(removes.length).toBeGreaterThan(0);
|
||||
for (const c of removes) {
|
||||
const scopeIdx = c.indexOf('--scope');
|
||||
expect(scopeIdx).toBeGreaterThan(3);
|
||||
expect(c[scopeIdx + 1]).toBe('project');
|
||||
}
|
||||
const adds = calls.filter((c) => c[1] === 'mcp' && c[2] === 'add').length;
|
||||
expect(adds).toBe(2); // initial (foreign) + re-add after remove
|
||||
// After the fix, the smoke confirms the corrected registration.
|
||||
expect(r.out).toContain('verified targeting this workspace');
|
||||
}, 30_000);
|
||||
|
||||
test('mismatch + failed remove → exit 1 with the by-hand fix instruction; add never retried', async () => {
|
||||
// Stateful failure host: add refuses ("already exists"), get shows a
|
||||
// FOREIGN registration (mismatch), and the scoped remove itself fails.
|
||||
const calls: string[][] = [];
|
||||
const runner: ExecRunner = async (argv: string[]) => {
|
||||
calls.push(argv);
|
||||
if (argv[1] !== 'mcp') return { code: 0, stdout: '', stderr: '' };
|
||||
if (argv[2] === 'add') return { code: 1, stdout: '', stderr: 'MCP server gbrain already exists' };
|
||||
if (argv[2] === 'get') return { code: 0, stdout: FOREIGN, stderr: '' };
|
||||
if (argv[2] === 'remove') return { code: 1, stdout: '', stderr: 'nope' };
|
||||
return { code: 0, stdout: '', stderr: '' };
|
||||
};
|
||||
const r = await runHooks(runner);
|
||||
expect(r.result).toBe(1);
|
||||
expect(r.err).toContain('targets a DIFFERENT workspace');
|
||||
// Fail LOUD, not the old silent no-op loop: the message hands the human
|
||||
// the manual off-ramp instead of re-failing the add.
|
||||
expect(r.err).toContain('remove the stale registration by hand');
|
||||
const adds = calls.filter((c) => c[1] === 'mcp' && c[2] === 'add').length;
|
||||
expect(adds).toBe(1); // the failed remove halts the flow before any re-add
|
||||
}, 30_000);
|
||||
|
||||
test('host without `mcp get` → inconclusive, kept with a note (never a false bless)', async () => {
|
||||
const { runner } = mcpHost({ initialReg: OURS, getSupported: false });
|
||||
const r = await runHooks(runner);
|
||||
@@ -595,6 +626,108 @@ describe('MCP registration verification [FIX7]', () => {
|
||||
}, 30_000);
|
||||
});
|
||||
|
||||
describe('MCP host failure × hooks at the dispatcher (exit-127 skip / broken settings fail-closed)', () => {
|
||||
// Self-contained fixtures (the flip pattern): HOOKS_CONSENT left at its bank
|
||||
// default ('yes') so the hooks half of the phase is live in both tests.
|
||||
const scratch: string[] = [];
|
||||
afterAll(() => {
|
||||
for (const d of scratch) rmSync(d, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function failWorkspace(): { fws: string; fhome: string; fparent: string } {
|
||||
const fparent = mkdtempSync(join(tmpdir(), 'gb-fail-'));
|
||||
const fhome = join(fparent, '.gbrain');
|
||||
mkdirSync(fhome, { recursive: true });
|
||||
const fws = mkdtempSync(join(tmpdir(), 'gb-fail-ws-'));
|
||||
scratch.push(fparent, fws);
|
||||
const prev = process.env.GBRAIN_HOME;
|
||||
process.env.GBRAIN_HOME = fparent;
|
||||
try {
|
||||
expect(initState(fws).ok).toBe(true);
|
||||
for (const [key, value] of Object.entries(REQUIRED_ANSWERS)) {
|
||||
const r = setAnswer(fws, key, value);
|
||||
if (!r.ok) throw new Error(r.message);
|
||||
}
|
||||
expect(setAnswer(fws, 'MCP_SCOPE', 'project').ok).toBe(true);
|
||||
const h = readBackHash(fws);
|
||||
if (!h.ok) throw new Error(h.message);
|
||||
expect(confirm(fws, h.hash).ok).toBe(true);
|
||||
} finally {
|
||||
if (prev === undefined) delete process.env.GBRAIN_HOME;
|
||||
else process.env.GBRAIN_HOME = prev;
|
||||
}
|
||||
return { fws, fhome, fparent };
|
||||
}
|
||||
|
||||
async function withFailHome<T>(parent: string, fn: () => Promise<T>): Promise<T> {
|
||||
const prev = process.env.GBRAIN_HOME;
|
||||
process.env.GBRAIN_HOME = parent;
|
||||
try {
|
||||
return await fn();
|
||||
} finally {
|
||||
if (prev === undefined) delete process.env.GBRAIN_HOME;
|
||||
else process.env.GBRAIN_HOME = prev;
|
||||
}
|
||||
}
|
||||
|
||||
test('`claude` missing (exit 127 on mcp add) → MCP skipped, hooks STILL install, exit 2, receipt detail hooks', async () => {
|
||||
const { fws, fhome, fparent } = failWorkspace();
|
||||
const r = await withFailHome(fparent, async () => {
|
||||
expect((await capture(() => runBootstrap(['render', '--workspace', fws]))).result).toBe(0);
|
||||
const runner: ExecRunner = async (argv: string[]) => {
|
||||
if (argv[0] === 'claude' && argv[1] === 'mcp' && argv[2] === 'add') {
|
||||
return { code: 127, stdout: '', stderr: 'claude: command not found' };
|
||||
}
|
||||
return { code: 0, stdout: '', stderr: '' };
|
||||
};
|
||||
return capture(() =>
|
||||
runBootstrap(['hooks', '--workspace', fws, '--harness', 'claude-code', '--gbrain-bin', process.execPath], {
|
||||
runner,
|
||||
}),
|
||||
);
|
||||
});
|
||||
expect(r.result).toBe(2);
|
||||
expect(r.err).toContain('is not on PATH');
|
||||
// The old early-return silently dropped hooks; now they install anyway
|
||||
// (hooks only write settings.local.json and need no host binary).
|
||||
expect(r.out).toContain('hooks installed');
|
||||
const settingsPath = join(fws, '.claude', 'settings.local.json');
|
||||
expect(existsSync(settingsPath)).toBe(true);
|
||||
const settings = JSON.parse(readFileSync(settingsPath, 'utf8')) as {
|
||||
hooks?: Record<string, Array<{ hooks?: Array<Record<string, unknown>> }>>;
|
||||
};
|
||||
const entries = Object.values(settings.hooks ?? {}).flatMap((groups) => groups.flatMap((g) => g.hooks ?? []));
|
||||
expect(entries.length).toBeGreaterThan(0);
|
||||
expect(entries.some((e) => e[GBRAIN_HOOK_MARKER_KEY] === GBRAIN_HOOK_MARKER_VALUE)).toBe(true);
|
||||
// Receipt records what actually landed: hooks only, no MCP.
|
||||
expect(readReceipt(fhome)?.registrations).toEqual([{ host: 'claude-code', scope: 'project', detail: 'hooks' }]);
|
||||
}, 30_000);
|
||||
|
||||
test('unparseable settings.local.json → hooks fail CLOSED (exit 1), file byte-identical, receipt detail mcp', async () => {
|
||||
const { fws, fhome, fparent } = failWorkspace();
|
||||
const broken = '{ definitely broken';
|
||||
const settingsPath = join(fws, '.claude', 'settings.local.json');
|
||||
const r = await withFailHome(fparent, async () => {
|
||||
expect((await capture(() => runBootstrap(['render', '--workspace', fws]))).result).toBe(0);
|
||||
mkdirSync(join(fws, '.claude'), { recursive: true });
|
||||
writeFileSync(settingsPath, broken, 'utf8');
|
||||
const { runner } = makeRunner();
|
||||
return capture(() =>
|
||||
runBootstrap(['hooks', '--workspace', fws, '--harness', 'claude-code', '--gbrain-bin', process.execPath], {
|
||||
runner,
|
||||
}),
|
||||
);
|
||||
});
|
||||
expect(r.result).toBe(1);
|
||||
// The refusal explains WHY (the file may carry permissions/allowlist
|
||||
// entries gbrain must not clobber) and names the repair path.
|
||||
expect(r.err).toContain('not valid JSON');
|
||||
expect(readFileSync(settingsPath, 'utf8')).toBe(broken);
|
||||
// MCP (step 1) landed before the hook failure — the receipt says exactly that.
|
||||
expect(readReceipt(fhome)?.registrations).toEqual([{ host: 'claude-code', scope: 'project', detail: 'mcp' }]);
|
||||
}, 30_000);
|
||||
});
|
||||
|
||||
describe('receipt overwrite guard wired into every writer [CX2-12]', () => {
|
||||
function writeNewerReceipt(dir: string): void {
|
||||
mkdirSync(join(dir, 'bootstrap'), { recursive: true });
|
||||
|
||||
@@ -170,18 +170,18 @@ describe('writeClaudeHooks [G5, CX2-17]', () => {
|
||||
expect(bak).toEqual({ permissions: { allow: ['X'] } });
|
||||
});
|
||||
|
||||
test('broken JSON: original backed up aside, loud note, clean file written', () => {
|
||||
test('broken JSON: write ABORTS fail-closed, file untouched, fix named', () => {
|
||||
// A parse-broken settings.local.json may carry permissions/allowlist
|
||||
// entries gbrain cannot see — rewriting it (the old backup-and-start-clean
|
||||
// behavior) silently dropped them from the live file. The write path now
|
||||
// matches removeClaudeHooks: refuse, name the fix, change nothing.
|
||||
const dir = ws();
|
||||
mkdirSync(join(dir, '.claude'), { recursive: true });
|
||||
writeFileSync(claudeSettingsPath(dir), '{ definitely broken json !!!');
|
||||
const res = writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
||||
expect(res.brokenBackupPath).not.toBeNull();
|
||||
expect(existsSync(res.brokenBackupPath!)).toBe(true);
|
||||
expect(readFileSync(res.brokenBackupPath!, 'utf8')).toContain('definitely broken');
|
||||
expect(res.notes.join(' ')).toContain('not valid JSON');
|
||||
// Fresh file is valid and carries our hooks.
|
||||
const settings = readSettings(dir);
|
||||
expect(markerEntries(settings, 'SessionStart')).toHaveLength(1);
|
||||
const original = '{ definitely broken json !!!';
|
||||
writeFileSync(claudeSettingsPath(dir), original);
|
||||
expect(() => writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV })).toThrow(/not valid JSON.*re-run/s);
|
||||
// Byte-identical after the refused write — nothing moved, nothing rewritten.
|
||||
expect(readFileSync(claudeSettingsPath(dir), 'utf8')).toBe(original);
|
||||
});
|
||||
|
||||
test('relative gbrainBin refused (GUI hosts inherit no PATH)', () => {
|
||||
|
||||
@@ -310,6 +310,49 @@ describe('[A8] provenance + read-back confirm', () => {
|
||||
expect(parsed.confirmed).toBeUndefined();
|
||||
});
|
||||
|
||||
test('setAnswer surfaces invalidatedConfirmation ONLY when a confirm existed', () => {
|
||||
const ws = makeWs();
|
||||
answerAllRequired(ws);
|
||||
// No prior confirmation → nothing was invalidated (falsy flag).
|
||||
const r0 = setAnswer(ws, 'SOUL_WINCE', 'Filler openers.');
|
||||
expect(r0.ok).toBe(true);
|
||||
if (!r0.ok || r0.sink !== 'state') throw new Error('expected a state-sink result');
|
||||
expect(r0.invalidatedConfirmation).toBeFalsy();
|
||||
// Full confirm, then a later set → the result SAYS it voided the confirm
|
||||
// (the CLI warns at --set time instead of failing much later at render).
|
||||
const h = readBackHash(ws);
|
||||
if (!h.ok) throw new Error(h.message);
|
||||
expect(confirm(ws, h.hash).ok).toBe(true);
|
||||
const r1 = setAnswer(ws, 'SOUL_GOOD_OUTPUT', 'A finished artifact.');
|
||||
expect(r1.ok).toBe(true);
|
||||
if (!r1.ok || r1.sink !== 'state') throw new Error('expected a state-sink result');
|
||||
expect(r1.invalidatedConfirmation).toBe(true);
|
||||
const st = status(ws);
|
||||
if (!st.ok) throw new Error(st.message);
|
||||
expect(st.confirmed).toBe(false);
|
||||
});
|
||||
|
||||
test('skipAnswer surfaces invalidatedConfirmation ONLY when a confirm existed (optional key — required keys refuse skip)', () => {
|
||||
const ws = makeWs();
|
||||
answerAllRequired(ws);
|
||||
// No prior confirmation → falsy flag on an optional-key skip.
|
||||
const r0 = skipAnswer(ws, 'SOUL_WINCE');
|
||||
expect(r0.ok).toBe(true);
|
||||
if (!r0.ok) throw new Error('unreachable');
|
||||
expect(r0.invalidatedConfirmation).toBeFalsy();
|
||||
// Full confirm, then a later optional-key skip → invalidation surfaced.
|
||||
const h = readBackHash(ws);
|
||||
if (!h.ok) throw new Error(h.message);
|
||||
expect(confirm(ws, h.hash).ok).toBe(true);
|
||||
const r1 = skipAnswer(ws, 'SOUL_WORLDVIEW');
|
||||
expect(r1.ok).toBe(true);
|
||||
if (!r1.ok) throw new Error('unreachable');
|
||||
expect(r1.invalidatedConfirmation).toBe(true);
|
||||
const st = status(ws);
|
||||
if (!st.ok) throw new Error(st.message);
|
||||
expect(st.confirmed).toBe(false);
|
||||
});
|
||||
|
||||
test('show returns the read-back payload with the hash once complete', () => {
|
||||
const ws = makeWs();
|
||||
answerAllRequired(ws);
|
||||
|
||||
@@ -158,6 +158,15 @@ describe('verifyWorkspace — keyless pass', () => {
|
||||
}
|
||||
expect(res.tour).toEqual([...FIRST_RUN_TOUR]);
|
||||
|
||||
// The OOBE hand-off block prints after the tour on PASS: ownership (this
|
||||
// ws has no origin remote → the local-only variant with the repo upgrade
|
||||
// path) and the ONE next action (the cold-start skill via ClawVisor).
|
||||
expect(res.report).toContain('What you own');
|
||||
expect(res.report).toContain('gbrain bootstrap repo');
|
||||
expect(res.report).toContain('cold-start');
|
||||
expect(res.report).toContain('ClawVisor');
|
||||
expect(res.handoff.length).toBeGreaterThan(0);
|
||||
|
||||
// Probe cleanup [G13]: pages, files, and the reconciled fact are gone.
|
||||
expect(existsSync(join(ws, 'brain', `${VERIFY_PROBE_SLUG}.md`))).toBe(false);
|
||||
expect(existsSync(join(ws, 'brain', `${VERIFY_PROBE_ENTITY_SLUG}.md`))).toBe(false);
|
||||
@@ -249,6 +258,21 @@ describe('verifyWorkspace — keyless pass', () => {
|
||||
expect(scan.detail).not.toContain('sk-AAAAAAAAAAAAAAAAAAAAAAAA');
|
||||
|
||||
expect(res.ok).toBe(false);
|
||||
|
||||
// Tour gating on FAIL: the report says fix-first and withholds the
|
||||
// celebration prompts ("broken, but go enjoy it" is a mixed signal) …
|
||||
expect(res.report).toContain('Fix the FAIL checks above');
|
||||
expect(res.report).not.toContain('Who am I to you?');
|
||||
// … the hand-off block is withheld with the tour (celebrating ownership
|
||||
// of a FAILED install is the same mixed signal) …
|
||||
expect(res.report).not.toContain('What you own');
|
||||
expect(res.report).not.toContain('cold-start');
|
||||
// … while the returned tour + handoff arrays stay unconditional so
|
||||
// machine consumers (--json) keep a stable shape, and the check names
|
||||
// the gate.
|
||||
expect(res.tour).toEqual([...FIRST_RUN_TOUR]);
|
||||
expect(res.handoff.length).toBeGreaterThan(0);
|
||||
expect(check(res.checks, 'first_run_tour')[0].detail).toContain('withheld');
|
||||
} finally {
|
||||
rmSync(githubPath, { force: true });
|
||||
writeFileSync(userPath, userOriginal);
|
||||
|
||||
+241
-2
@@ -9,16 +9,17 @@
|
||||
*/
|
||||
|
||||
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
|
||||
import { mkdtempSync, rmSync, existsSync, readFileSync } from 'fs';
|
||||
import { mkdtempSync, rmSync, existsSync, readFileSync, writeFileSync, chmodSync, mkdirSync, symlinkSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import { runFriction } from '../src/commands/friction.ts';
|
||||
import { listScenarios, loadScenario } from '../src/core/claw-test/scenarios.ts';
|
||||
import {
|
||||
registerAgentRunner, resolveAgentRunner, listRegisteredAgents,
|
||||
_resetRegistryForTests,
|
||||
_resetRegistryForTests, validateBinPathEnv,
|
||||
type AgentRunner, type DetectResult, type InvokeOpts, type InvokeResult,
|
||||
} from '../src/core/claw-test/agent-runner.ts';
|
||||
import { mergeChildFriction } from '../src/commands/claw-test.ts';
|
||||
|
||||
let tmp: string;
|
||||
const ORIG_HOME = process.env.GBRAIN_HOME;
|
||||
@@ -163,3 +164,241 @@ describe('OpenClawRunner detection (reliable on box without openclaw)', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('HermesRunner detection (reliable on box without hermes)', () => {
|
||||
test('detect returns the contract shape when HERMES_BIN unset', async () => {
|
||||
const orig = process.env.HERMES_BIN;
|
||||
delete process.env.HERMES_BIN;
|
||||
try {
|
||||
const { HermesRunner } = await import('../src/core/claw-test/runners/hermes.ts');
|
||||
const d = await new HermesRunner().detect();
|
||||
// Available when hermes IS on the dev's PATH, unavailable otherwise —
|
||||
// both are valid; assert the contract shape only.
|
||||
expect(typeof d.available).toBe('boolean');
|
||||
if (!d.available) expect(typeof d.reason).toBe('string');
|
||||
else expect(d.binPath?.startsWith('/')).toBe(true);
|
||||
} finally {
|
||||
if (orig !== undefined) process.env.HERMES_BIN = orig;
|
||||
}
|
||||
});
|
||||
|
||||
test('detect rejects relative HERMES_BIN', async () => {
|
||||
const orig = process.env.HERMES_BIN;
|
||||
process.env.HERMES_BIN = 'relative/hermes';
|
||||
try {
|
||||
const { HermesRunner } = await import('../src/core/claw-test/runners/hermes.ts');
|
||||
const d = await new HermesRunner().detect();
|
||||
expect(d.available).toBe(false);
|
||||
expect(d.reason).toMatch(/HERMES_BIN must be absolute/);
|
||||
} finally {
|
||||
if (orig !== undefined) process.env.HERMES_BIN = orig;
|
||||
else delete process.env.HERMES_BIN;
|
||||
}
|
||||
});
|
||||
|
||||
test("detect rejects '..' segments in HERMES_BIN", async () => {
|
||||
const orig = process.env.HERMES_BIN;
|
||||
process.env.HERMES_BIN = '/tmp/foo/../hermes';
|
||||
try {
|
||||
const { HermesRunner } = await import('../src/core/claw-test/runners/hermes.ts');
|
||||
const d = await new HermesRunner().detect();
|
||||
expect(d.available).toBe(false);
|
||||
expect(d.reason).toMatch(/'\.\.' segments/);
|
||||
} finally {
|
||||
if (orig !== undefined) process.env.HERMES_BIN = orig;
|
||||
else delete process.env.HERMES_BIN;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('HermesRunner invoke argv/env (shim — no hermes binary needed)', () => {
|
||||
test('argv starts with the one-shot flag; HERMES_HOME propagates; unlisted env does not', async () => {
|
||||
const orig = {
|
||||
HERMES_BIN: process.env.HERMES_BIN,
|
||||
HERMES_HOME: process.env.HERMES_HOME,
|
||||
LEAK_CANARY: process.env.LEAK_CANARY,
|
||||
GBRAIN_DATABASE_URL: process.env.GBRAIN_DATABASE_URL,
|
||||
OPENROUTER_API_KEY: process.env.OPENROUTER_API_KEY,
|
||||
};
|
||||
const shim = join(tmp, 'hermes-shim');
|
||||
// Echo the argv and the env probes, then exit 0. The transcript sink
|
||||
// captures stdout, so assertions read the sink's events.
|
||||
writeFileSync(shim, '#!/bin/sh\nprintf "ARGV:%s\\n" "$@"\nprintf "HH:[%s] CANARY:[%s] DBURL:[%s] OR:[%s]\\n" "$HERMES_HOME" "$LEAK_CANARY" "$GBRAIN_DATABASE_URL" "$OPENROUTER_API_KEY"\n', 'utf-8');
|
||||
chmodSync(shim, 0o755);
|
||||
process.env.HERMES_BIN = shim;
|
||||
process.env.HERMES_HOME = '/tmp/hh-canary-test';
|
||||
process.env.LEAK_CANARY = 'must-not-leak';
|
||||
// Hermes-documented auth path (docs/mcp/HERMES.md): the hermes delta must
|
||||
// forward it or env-only OpenRouter operators get "no inference provider".
|
||||
process.env.OPENROUTER_API_KEY = 'or-sentinel-91c4';
|
||||
// Removed from BASE_ENV_ALLOWLIST in the adversarial review: an inherited
|
||||
// GBRAIN_DATABASE_URL would flip only the AGENT's gbrain to Postgres while
|
||||
// staging + the oracle stay on the hermetic PGLite (split-brain).
|
||||
process.env.GBRAIN_DATABASE_URL = 'postgres://must-not-leak';
|
||||
try {
|
||||
const { HermesRunner } = await import('../src/core/claw-test/runners/hermes.ts');
|
||||
const chunks: Buffer[] = [];
|
||||
const result = await new HermesRunner().invoke({
|
||||
cwd: tmp,
|
||||
brief: 'BRIEF BODY sentinel-7c2f',
|
||||
env: {},
|
||||
timeoutMs: 10_000,
|
||||
transcriptSink: {
|
||||
write: (e) => { if (e.channel === 'stdout') chunks.push(e.bytes); },
|
||||
nextOffset: () => 0,
|
||||
close: async () => {},
|
||||
},
|
||||
});
|
||||
expect(result.exitCode).toBe(0);
|
||||
const stdout = Buffer.concat(chunks).toString('utf-8');
|
||||
// First argv token is the one-shot flag, second is the brief itself.
|
||||
expect(stdout).toContain('ARGV:-z\nARGV:BRIEF BODY sentinel-7c2f');
|
||||
// Allowlist held: HERMES_HOME + OPENROUTER_API_KEY (the hermes delta)
|
||||
// pass; the canary and the deliberately-delisted GBRAIN_DATABASE_URL
|
||||
// don't.
|
||||
expect(stdout).toContain('HH:[/tmp/hh-canary-test]');
|
||||
expect(stdout).toContain('CANARY:[] DBURL:[] OR:[or-sentinel-91c4]');
|
||||
} finally {
|
||||
for (const [k, v] of Object.entries(orig)) {
|
||||
if (v !== undefined) process.env[k] = v;
|
||||
else delete process.env[k];
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('OpenClawRunner invoke env (shim — pins the shared-allowlist leak barrier)', () => {
|
||||
test('GBRAIN_DATABASE_URL does not propagate through the openclaw runner either', async () => {
|
||||
// The split-brain fix removed GBRAIN_DATABASE_URL from BASE_ENV_ALLOWLIST;
|
||||
// the hermes shim test pins the hermes side — this pins the openclaw side
|
||||
// so an openclaw-specific delta re-adding it (the exact one-line
|
||||
// regression) cannot pass silently.
|
||||
const orig = { OPENCLAW_BIN: process.env.OPENCLAW_BIN, GBRAIN_DATABASE_URL: process.env.GBRAIN_DATABASE_URL };
|
||||
const shim = join(tmp, 'openclaw-shim');
|
||||
writeFileSync(shim, '#!/bin/sh\nprintf "DBURL:[%s]\\n" "$GBRAIN_DATABASE_URL"\n', 'utf-8');
|
||||
chmodSync(shim, 0o755);
|
||||
process.env.OPENCLAW_BIN = shim;
|
||||
process.env.GBRAIN_DATABASE_URL = 'postgres://must-not-leak';
|
||||
try {
|
||||
const { OpenClawRunner } = await import('../src/core/claw-test/runners/openclaw.ts');
|
||||
const chunks: Buffer[] = [];
|
||||
const result = await new OpenClawRunner().invoke({
|
||||
cwd: tmp,
|
||||
brief: 'brief',
|
||||
env: {},
|
||||
timeoutMs: 10_000,
|
||||
transcriptSink: {
|
||||
write: (e) => { if (e.channel === 'stdout') chunks.push(e.bytes); },
|
||||
nextOffset: () => 0,
|
||||
close: async () => {},
|
||||
},
|
||||
});
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(Buffer.concat(chunks).toString('utf-8')).toContain('DBURL:[]');
|
||||
} finally {
|
||||
for (const [k, v] of Object.entries(orig)) {
|
||||
if (v !== undefined) process.env[k] = v;
|
||||
else delete process.env[k];
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateBinPathEnv — shim-quoting hardening', () => {
|
||||
test('rejects quote/metacharacter values that would break out of the generated shim quoting', () => {
|
||||
// The value is interpolated single-quoted into sh shim scripts; each of
|
||||
// these would otherwise become shell code.
|
||||
for (const bad of [
|
||||
"/tmp/x'; rm -rf /tmp/pwn; '",
|
||||
'/tmp/x"double',
|
||||
'/tmp/x`tick`',
|
||||
'/tmp/x$HOME',
|
||||
'/tmp/x\\backslash',
|
||||
'/tmp/x\nnewline',
|
||||
]) {
|
||||
expect(validateBinPathEnv('X_BIN', bad)).not.toBeNull();
|
||||
}
|
||||
// Spaces stay legal (macOS paths); quoting handles them.
|
||||
expect(validateBinPathEnv('X_BIN', '/Applications/App Support/gbrain')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('mergeChildFriction — untrusted child file hardening', () => {
|
||||
// The child file lives in a workspace the AGENT writes to; the destination
|
||||
// is the operator's permanent friction log.
|
||||
const runId = 'claw-test-merge-hardening';
|
||||
|
||||
function childPath(runRoot: string): string {
|
||||
const dir = join(runRoot, '.gbrain', 'friction');
|
||||
mkdirSync(dir, { recursive: true });
|
||||
return join(dir, `${runId}.jsonl`);
|
||||
}
|
||||
|
||||
function parentFile(): string {
|
||||
return join(tmp, '.gbrain', 'friction', `${runId}.jsonl`);
|
||||
}
|
||||
|
||||
test('valid JSONL lines merge; non-JSON and non-object lines are dropped', () => {
|
||||
const runRoot = join(tmp, 'runroot-valid');
|
||||
const entry = JSON.stringify({ phase: 'agent-side', message: 'kept', kind: 'friction' });
|
||||
writeFileSync(childPath(runRoot), `${entry}\nnot json at all\n"a json string scalar"\n[1,2]\n`, 'utf-8');
|
||||
mergeChildFriction(runRoot, runId);
|
||||
const merged = readFileSync(parentFile(), 'utf-8').split('\n').filter(l => l.trim());
|
||||
expect(merged).toEqual([entry]);
|
||||
});
|
||||
|
||||
test('a symlinked child file is refused (an agent-dropped link could import any readable file)', () => {
|
||||
const runRoot = join(tmp, 'runroot-symlink');
|
||||
const target = join(tmp, 'outside-secret.jsonl');
|
||||
writeFileSync(target, JSON.stringify({ phase: 'x', message: 'secret' }) + '\n', 'utf-8');
|
||||
const cp = childPath(runRoot);
|
||||
symlinkSync(target, cp);
|
||||
mergeChildFriction(runRoot, runId);
|
||||
expect(existsSync(parentFile())).toBe(false);
|
||||
});
|
||||
|
||||
test('an oversized child file is refused (size cap)', () => {
|
||||
const runRoot = join(tmp, 'runroot-huge');
|
||||
const line = JSON.stringify({ phase: 'x', message: 'y'.repeat(1024) });
|
||||
const lines = Math.ceil((5 * 1024 * 1024) / line.length) + 1;
|
||||
writeFileSync(childPath(runRoot), Array(lines).fill(line).join('\n') + '\n', 'utf-8');
|
||||
mergeChildFriction(runRoot, runId);
|
||||
expect(existsSync(parentFile())).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('spawnWithCapture — stdin EOF (no payload)', () => {
|
||||
test('an agent that waits for stdin EOF exits promptly instead of hanging to the timeout', async () => {
|
||||
const orig = process.env.HERMES_BIN;
|
||||
const shim = join(tmp, 'stdin-wait-shim');
|
||||
// `cat` blocks until stdin EOF; with stdin left open this burns the whole
|
||||
// timeout and exits 124 via the kill path.
|
||||
writeFileSync(shim, '#!/bin/sh\ncat > /dev/null\necho done\n', 'utf-8');
|
||||
chmodSync(shim, 0o755);
|
||||
process.env.HERMES_BIN = shim;
|
||||
try {
|
||||
const { HermesRunner } = await import('../src/core/claw-test/runners/hermes.ts');
|
||||
const start = Date.now();
|
||||
const result = await new HermesRunner().invoke({
|
||||
cwd: tmp,
|
||||
brief: 'brief',
|
||||
env: {},
|
||||
timeoutMs: 15_000,
|
||||
transcriptSink: { write: () => {}, nextOffset: () => 0, close: async () => {} },
|
||||
});
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(Date.now() - start).toBeLessThan(10_000);
|
||||
} finally {
|
||||
if (orig !== undefined) process.env.HERMES_BIN = orig;
|
||||
else delete process.env.HERMES_BIN;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// NOTE deliberately absent: a "command module registers openclaw + hermes"
|
||||
// unit test. Any in-process version is a tautology — this file's beforeEach
|
||||
// wipes the registry and bun caches the command module, so the test would
|
||||
// have to re-register the runners itself and would pass even if the command
|
||||
// module dropped its registrations. The HONEST integration check lives in
|
||||
// test/e2e/claw-test.test.ts ("--list-agents reports both built-in runners"),
|
||||
// which spawns the real CLI and asserts both runner lines.
|
||||
|
||||
@@ -208,6 +208,9 @@ describe('thin-client scratch-DB guard — jobs partial dispatch + config refusa
|
||||
expect(existsSync(join(tmp, '.gbrain', 'brain.pglite'))).toBe(false);
|
||||
expect(r.stdout + r.stderr).not.toContain('Schema version');
|
||||
expect(r.stdout + r.stderr).not.toContain('migration(s) pending');
|
||||
// A scratch store is a FRESH install, so a re-regression would print the
|
||||
// quiet-replay summary line, not the verbose header — pin both shapes.
|
||||
expect(r.stdout + r.stderr).not.toContain('Setting up brain schema');
|
||||
});
|
||||
|
||||
test('`gbrain jobs list` never fabricates a scratch local engine', async () => {
|
||||
@@ -216,5 +219,6 @@ describe('thin-client scratch-DB guard — jobs partial dispatch + config refusa
|
||||
const { existsSync } = await import('fs');
|
||||
expect(existsSync(join(tmp, '.gbrain', 'brain.pglite'))).toBe(false);
|
||||
expect(r.stdout + r.stderr).not.toContain('Schema version');
|
||||
expect(r.stdout + r.stderr).not.toContain('Setting up brain schema');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ import { withEnv } from './helpers/with-env.ts';
|
||||
import { checkSelfUpgradeHealth } from '../src/commands/doctor.ts';
|
||||
import { writeUpdateCache } from '../src/core/self-upgrade.ts';
|
||||
import { logSelfUpgrade } from '../src/core/audit/self-upgrade-audit.ts';
|
||||
import { VERSION } from '../src/version.ts';
|
||||
|
||||
async function withHome<T>(fn: (home: string) => T | Promise<T>): Promise<T> {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'gbrain-doctor-su-'));
|
||||
@@ -40,7 +41,18 @@ describe('checkSelfUpgradeHealth', () => {
|
||||
const c = checkSelfUpgradeHealth();
|
||||
expect(c.status).toBe('ok');
|
||||
expect(c.message).toContain('update available');
|
||||
expect(c.message).toContain('0.99.0');
|
||||
expect(c.message).toContain('-> 0.99.0');
|
||||
});
|
||||
});
|
||||
|
||||
test('fresh cache with latest == running version → suppressed (no update-available nag)', async () => {
|
||||
await withHome(() => {
|
||||
// Stale/foreign cache: the recorded latest is the version we are already
|
||||
// running. The shared pendingUpgradeVersion guard must suppress the nag.
|
||||
writeUpdateCache({ kind: 'upgrade_available', current: VERSION, latest: VERSION });
|
||||
const c = checkSelfUpgradeHealth();
|
||||
expect(c.status).toBe('ok');
|
||||
expect(c.message).not.toContain('update available');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -71,6 +71,14 @@ describe('gbrain claw-test --scenario fresh-install (scripted)', () => {
|
||||
console.error('unexpected friction entries:', blockers);
|
||||
}
|
||||
expect(blockers.length).toBe(0);
|
||||
|
||||
// REGRESSION pin: scripted runs stamp agent 'scripted' (previously the
|
||||
// misleading parseArgs default 'openclaw'). friction diff's agent-name
|
||||
// resolution reads this stamp off the start marker.
|
||||
expect(entries[0].kind).toBe('phase-marker');
|
||||
expect(entries[0].marker).toBe('start');
|
||||
expect(entries[0].agent).toBe('scripted');
|
||||
expect(entries[0].scenario).toBe('fresh-install');
|
||||
} finally {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
@@ -114,6 +122,292 @@ describe('gbrain claw-test --scenario fresh-install (scripted)', () => {
|
||||
}, 90_000);
|
||||
});
|
||||
|
||||
describe('gbrain claw-test --list-agents', () => {
|
||||
test('reports both built-in runners (available or not — both valid states)', () => {
|
||||
// HERMES_BIN/OPENCLAW_BIN point at a nonexistent path so the output shape
|
||||
// is deterministic regardless of what's installed on the box (detect
|
||||
// rejects a non-stat-able absolute path with a specific reason).
|
||||
const result = spawnSync(BIN_PATH, ['claw-test', '--list-agents'], {
|
||||
cwd: REPO_ROOT,
|
||||
env: { ...process.env, HERMES_BIN: '/nonexistent/hermes', OPENCLAW_BIN: '/nonexistent/openclaw' },
|
||||
encoding: 'utf-8',
|
||||
timeout: 60_000,
|
||||
});
|
||||
expect(result.status).toBe(0);
|
||||
expect(result.stdout).toMatch(/^hermes: unavailable: /m);
|
||||
expect(result.stdout).toMatch(/^openclaw: unavailable: /m);
|
||||
// Alphabetical print order (the awaited-detection fix pins this).
|
||||
expect(result.stdout.indexOf('hermes:')).toBeLessThan(result.stdout.indexOf('openclaw:'));
|
||||
}, 60_000);
|
||||
});
|
||||
|
||||
describe('gbrain claw-test --live (shim agents; no real agent binary, no tokens)', () => {
|
||||
// The OpenClaw runner honors an absolute $OPENCLAW_BIN pointing at any
|
||||
// executable, so these tests drive live mode with tiny sh shims. That
|
||||
// exercises the REAL live path: staging, the PATH shim, the agent turn,
|
||||
// the success oracle, and the E0 friction merge.
|
||||
|
||||
function runLiveWithShim(shimBody: string, scenario: string, extraEnv: Record<string, string> = {}) {
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'claw-test-e2e-live-'));
|
||||
const shim = join(tmp, 'agent-shim');
|
||||
writeFileSync(shim, shimBody, 'utf-8');
|
||||
chmodSync(shim, 0o755);
|
||||
const result = spawnSync(BIN_PATH, ['claw-test', '--live', '--agent', 'openclaw', '--scenario', scenario], {
|
||||
cwd: REPO_ROOT,
|
||||
env: {
|
||||
...process.env,
|
||||
GBRAIN_HOME: tmp,
|
||||
OPENCLAW_BIN: shim,
|
||||
GBRAIN_BIN_OVERRIDE: BIN_PATH,
|
||||
GBRAIN_CLAW_SCENARIOS_DIR: extraEnv.GBRAIN_CLAW_SCENARIOS_DIR ?? SCENARIOS_DIR,
|
||||
...extraEnv,
|
||||
},
|
||||
encoding: 'utf-8',
|
||||
timeout: 180_000,
|
||||
});
|
||||
const frictionDirPath = join(tmp, '.gbrain', 'friction');
|
||||
const entries: any[] = [];
|
||||
if (existsSync(frictionDirPath)) {
|
||||
for (const f of readdirSync(frictionDirPath).filter(f => f.endsWith('.jsonl'))) {
|
||||
for (const line of readFileSync(join(frictionDirPath, f), 'utf-8').split('\n')) {
|
||||
if (line.trim()) entries.push(JSON.parse(line));
|
||||
}
|
||||
}
|
||||
}
|
||||
return { tmp, result, entries };
|
||||
}
|
||||
|
||||
test('oracle break path: a do-nothing agent that exits 0 now FAILS the run', () => {
|
||||
const { tmp, result, entries } = runLiveWithShim('#!/bin/sh\nexit 0\n', 'fresh-install');
|
||||
try {
|
||||
expect(result.status).not.toBe(0);
|
||||
const verifyErrors = entries.filter(e => e.phase === 'verify' && e.severity === 'error');
|
||||
expect(verifyErrors.length).toBeGreaterThan(0);
|
||||
// The run-start meta record (agent-name resolution depends on it) is the
|
||||
// FIRST line and carries agent + scenario.
|
||||
expect(entries[0].kind).toBe('phase-marker');
|
||||
expect(entries[0].marker).toBe('start');
|
||||
expect(entries[0].agent).toBe('openclaw');
|
||||
expect(entries[0].scenario).toBe('fresh-install');
|
||||
} finally {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
}, 180_000);
|
||||
|
||||
test('working agent passes the oracle AND its child-side friction survives tempdir cleanup (E0 merge)', () => {
|
||||
// The shim does the brief's work via BARE `gbrain` (proving the PATH shim
|
||||
// resolves to this checkout) and logs one agent-side friction entry, which
|
||||
// lands under the run's hermetic GBRAIN_HOME — the dir the harness deletes.
|
||||
const shim = [
|
||||
'#!/bin/sh',
|
||||
'set -e',
|
||||
'gbrain import ./brain --no-embed',
|
||||
'gbrain skillpack scaffold query --workspace "$PWD"',
|
||||
'gbrain friction log --phase agent-side --severity nit --message "child entry survives cleanup"',
|
||||
'',
|
||||
].join('\n');
|
||||
const { tmp, result, entries } = runLiveWithShim(shim, 'fresh-install');
|
||||
try {
|
||||
if (result.status !== 0) {
|
||||
console.error('STDOUT:', result.stdout);
|
||||
console.error('STDERR:', result.stderr);
|
||||
}
|
||||
expect(result.status).toBe(0);
|
||||
const blockers = entries.filter(e => e.kind === 'friction' && (e.severity === 'error' || e.severity === 'blocker'));
|
||||
expect(blockers.length).toBe(0);
|
||||
// E0: the child-side entry was written under the (deleted) runRoot but
|
||||
// must appear in the parent's friction file.
|
||||
const childSide = entries.filter(e => e.phase === 'agent-side' && e.message.includes('child entry survives cleanup'));
|
||||
expect(childSide.length).toBe(1);
|
||||
// Completion marker paired with the start marker.
|
||||
const endMarkers = entries.filter(e => e.kind === 'phase-marker' && e.marker === 'end' && e.phase === 'harness');
|
||||
expect(endMarkers.length).toBe(1);
|
||||
} finally {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
}, 180_000);
|
||||
|
||||
test('REGRESSION (upgrade): staging seeds first and the schema-version oracle fails a do-nothing agent', () => {
|
||||
// Synthetic upgrade scenario: a minimal "old brain" dump that records
|
||||
// config.version = 1. The do-nothing agent never advances the migration
|
||||
// chain, so the non-mutating pre/post probe must fail the run. (A
|
||||
// doctor-based oracle would auto-migrate on connect and pass vacuously —
|
||||
// the exact bug class this oracle exists to prevent.)
|
||||
const scenRoot = mkdtempSync(join(tmpdir(), 'claw-test-e2e-upg-scen-'));
|
||||
const scenDir = join(scenRoot, 'upgrade-synthetic');
|
||||
mkdirSync(join(scenDir, 'seed'), { recursive: true });
|
||||
writeFileSync(join(scenDir, 'scenario.json'), JSON.stringify({
|
||||
kind: 'upgrade',
|
||||
from_version: '0.0.1',
|
||||
expected_phases: [],
|
||||
seed: 'seed',
|
||||
}), 'utf-8');
|
||||
writeFileSync(join(scenDir, 'BRIEF.md'), '# Upgrade brief\n\nRun `gbrain doctor --json` to walk the migration chain forward.\n', 'utf-8');
|
||||
writeFileSync(join(scenDir, 'seed', 'dump.sql'), [
|
||||
"CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT);",
|
||||
"INSERT INTO config (key, value) VALUES ('version', '1');",
|
||||
'',
|
||||
].join('\n'), 'utf-8');
|
||||
|
||||
const { tmp, result, entries } = runLiveWithShim('#!/bin/sh\nexit 0\n', 'upgrade-synthetic', {
|
||||
GBRAIN_CLAW_SCENARIOS_DIR: scenRoot,
|
||||
});
|
||||
try {
|
||||
expect(result.status).not.toBe(0);
|
||||
const verifyErrors = entries.filter(e => e.phase === 'verify' && e.severity === 'error');
|
||||
expect(verifyErrors.length).toBe(1);
|
||||
expect(verifyErrors[0].message).toContain('did not advance');
|
||||
// pre=1 proves the seeded PGLite existed BEFORE the agent turn.
|
||||
expect(verifyErrors[0].message).toContain('pre=1');
|
||||
} finally {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
rmSync(scenRoot, { recursive: true, force: true });
|
||||
}
|
||||
}, 180_000);
|
||||
});
|
||||
|
||||
describe('gbrain claw-test — adversarial-gate regression pins', () => {
|
||||
test('P1: without GBRAIN_BIN_OVERRIDE the harness synthesizes a bun launcher (children run gbrain, not bun)', () => {
|
||||
// Under `bun run src/cli.ts`, process.execPath is the Bun runtime; the
|
||||
// pre-fix fallback handed children `bun init …`, which scaffolds a Bun
|
||||
// project and fails the rest of the run. Exit 0 proves the synthesized
|
||||
// launcher resolved children to real gbrain.
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'claw-test-e2e-noovr-'));
|
||||
try {
|
||||
const env: Record<string, string | undefined> = {
|
||||
...process.env,
|
||||
GBRAIN_HOME: tmp,
|
||||
GBRAIN_CLAW_SCENARIOS_DIR: SCENARIOS_DIR,
|
||||
};
|
||||
delete env.GBRAIN_BIN_OVERRIDE;
|
||||
const result = spawnSync(BIN_PATH, ['claw-test', '--scenario', 'fresh-install'], {
|
||||
cwd: REPO_ROOT,
|
||||
env: env as NodeJS.ProcessEnv,
|
||||
encoding: 'utf-8',
|
||||
timeout: 150_000,
|
||||
});
|
||||
if (result.status !== 0) {
|
||||
console.error('STDOUT:', result.stdout);
|
||||
console.error('STDERR:', result.stderr);
|
||||
}
|
||||
expect(result.status).toBe(0);
|
||||
} finally {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
}, 180_000);
|
||||
|
||||
test('scripted upgrade with the shipped fixture fails LOUDLY (no false-green upgrade without a seed dump)', () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'claw-test-e2e-upgloud-'));
|
||||
try {
|
||||
const result = spawnSync(BIN_PATH, ['claw-test', '--scenario', 'upgrade-from-v0.18'], {
|
||||
cwd: REPO_ROOT,
|
||||
env: {
|
||||
...process.env,
|
||||
GBRAIN_HOME: tmp,
|
||||
GBRAIN_BIN_OVERRIDE: BIN_PATH,
|
||||
GBRAIN_CLAW_SCENARIOS_DIR: SCENARIOS_DIR,
|
||||
},
|
||||
encoding: 'utf-8',
|
||||
timeout: 60_000,
|
||||
});
|
||||
expect(result.status).toBe(1);
|
||||
expect(result.stderr).toContain('no seed dump');
|
||||
} finally {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
}, 90_000);
|
||||
|
||||
test('charset guard: traversal-shaped scenario and agent values are usage errors, exit 2', () => {
|
||||
for (const argv of [
|
||||
['claw-test', '--scenario', '../evil'],
|
||||
['claw-test', '--agent', 'x/y', '--scenario', 'fresh-install'],
|
||||
]) {
|
||||
const result = spawnSync(BIN_PATH, argv, {
|
||||
cwd: REPO_ROOT,
|
||||
env: { ...process.env, GBRAIN_CLAW_SCENARIOS_DIR: SCENARIOS_DIR },
|
||||
encoding: 'utf-8',
|
||||
timeout: 30_000,
|
||||
});
|
||||
expect(result.status).toBe(2);
|
||||
expect(result.stderr).toContain('invalid');
|
||||
}
|
||||
}, 60_000);
|
||||
|
||||
test('phase timeout: a hung gbrain child is killed at GBRAIN_CLAW_PHASE_TIMEOUT_MS instead of wedging the run', () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'claw-test-e2e-hang-'));
|
||||
const hangBin = join(tmp, 'hang-gbrain');
|
||||
try {
|
||||
writeFileSync(hangBin, `#!/bin/sh\nif [ "$1" = "init" ]; then sleep 45; fi\nexec "${BIN_PATH}" "$@"\n`, 'utf-8');
|
||||
chmodSync(hangBin, 0o755);
|
||||
const result = spawnSync(BIN_PATH, ['claw-test', '--scenario', 'fresh-install'], {
|
||||
cwd: REPO_ROOT,
|
||||
env: {
|
||||
...process.env,
|
||||
GBRAIN_HOME: tmp,
|
||||
GBRAIN_BIN_OVERRIDE: hangBin,
|
||||
GBRAIN_CLAW_SCENARIOS_DIR: SCENARIOS_DIR,
|
||||
GBRAIN_CLAW_PHASE_TIMEOUT_MS: '2000',
|
||||
},
|
||||
encoding: 'utf-8',
|
||||
timeout: 35_000,
|
||||
});
|
||||
// If the phase timeout were unwired, init would sleep past spawnSync's
|
||||
// own kill and status would be null with signal SIGTERM.
|
||||
expect(result.signal).toBeNull();
|
||||
expect(result.status).toBe(1);
|
||||
} finally {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
}, 60_000);
|
||||
|
||||
test('fail-closed doctor oracle: doctor exiting 0 with unparsable output FAILS the live run', () => {
|
||||
// A wrapper gbrain that answers `doctor` with a non-JSON banner and exit 0
|
||||
// — the pre-fix oracle only rejected literal "unhealthy" and passed this.
|
||||
const scratch = mkdtempSync(join(tmpdir(), 'claw-test-e2e-docgarb-'));
|
||||
const wrapper = join(scratch, 'doctor-garbage-gbrain');
|
||||
writeFileSync(wrapper, `#!/bin/sh\nif [ "$1" = "doctor" ]; then echo "banner: everything is fine (not json)"; exit 0; fi\nexec "${BIN_PATH}" "$@"\n`, 'utf-8');
|
||||
chmodSync(wrapper, 0o755);
|
||||
const workShim = [
|
||||
'#!/bin/sh',
|
||||
'set -e',
|
||||
'gbrain import ./brain --no-embed',
|
||||
'gbrain skillpack scaffold query --workspace "$PWD"',
|
||||
'',
|
||||
].join('\n');
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'claw-test-e2e-live-docgarb-'));
|
||||
const shim = join(tmp, 'agent-shim');
|
||||
writeFileSync(shim, workShim, 'utf-8');
|
||||
chmodSync(shim, 0o755);
|
||||
try {
|
||||
const result = spawnSync(BIN_PATH, ['claw-test', '--live', '--agent', 'openclaw', '--scenario', 'fresh-install'], {
|
||||
cwd: REPO_ROOT,
|
||||
env: {
|
||||
...process.env,
|
||||
GBRAIN_HOME: tmp,
|
||||
OPENCLAW_BIN: shim,
|
||||
GBRAIN_BIN_OVERRIDE: wrapper,
|
||||
GBRAIN_CLAW_SCENARIOS_DIR: SCENARIOS_DIR,
|
||||
},
|
||||
encoding: 'utf-8',
|
||||
timeout: 180_000,
|
||||
});
|
||||
expect(result.status).not.toBe(0);
|
||||
const frictionDirPath = join(tmp, '.gbrain', 'friction');
|
||||
const entries: any[] = [];
|
||||
for (const f of readdirSync(frictionDirPath).filter(f => f.endsWith('.jsonl'))) {
|
||||
for (const line of readFileSync(join(frictionDirPath, f), 'utf-8').split('\n')) {
|
||||
if (line.trim()) entries.push(JSON.parse(line));
|
||||
}
|
||||
}
|
||||
const unparsable = entries.filter(e => e.phase === 'verify' && typeof e.message === 'string' && e.message.includes('unparsable'));
|
||||
expect(unparsable.length).toBe(1);
|
||||
} finally {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}, 180_000);
|
||||
});
|
||||
|
||||
describe('gbrain friction render integration', () => {
|
||||
test('render produces a markdown report with the redact placeholder', () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'claw-test-e2e-render-'));
|
||||
|
||||
@@ -89,36 +89,111 @@ describe('v0.37 T12 — fresh init env-detection (D1, D2, D3) + persistence (D5)
|
||||
|
||||
// ============================================================================
|
||||
|
||||
describe('v0.37 T12 — D3 non-TTY no-key fail-loud', () => {
|
||||
describe('v0.45 DX wave — non-TTY no-key defaults to keyless (typo still fail-loud)', () => {
|
||||
let tmpHome: string;
|
||||
let typoHome: string;
|
||||
|
||||
beforeAll(() => { tmpHome = makeTempHome(); });
|
||||
afterAll(() => { rmSync(tmpHome, { recursive: true, force: true }); });
|
||||
beforeAll(() => { tmpHome = makeTempHome(); typoHome = makeTempHome(); });
|
||||
afterAll(() => {
|
||||
rmSync(tmpHome, { recursive: true, force: true });
|
||||
rmSync(typoHome, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test('--non-interactive with zero provider keys → exit 1 + paste-ready hint', async () => {
|
||||
test('--non-interactive with zero provider keys → keyless init succeeds with loud notice', async () => {
|
||||
const r = await runCli(['init', '--pglite', '--non-interactive'], {
|
||||
gbrainHome: tmpHome,
|
||||
env: {}, // no provider keys
|
||||
});
|
||||
expect(r.exitCode).toBe(1);
|
||||
// Fail-loud message includes the canonical env var list.
|
||||
expect(r.stderr).toContain('No embedding provider configured');
|
||||
expect(r.stderr).toContain('OPENAI_API_KEY');
|
||||
expect(r.stderr).toContain('ZEROENTROPY_API_KEY');
|
||||
expect(r.stderr).toContain('VOYAGE_API_KEY');
|
||||
// Suggests --no-embedding alternative.
|
||||
expect(r.stderr).toContain('--no-embedding');
|
||||
}, 60000);
|
||||
// Keyless is a first-class posture: the naive first command completes.
|
||||
expect(r.exitCode).toBe(0);
|
||||
expect(r.stderr).toContain('keyless mode');
|
||||
// The notice names the upgrade affordance — the re-init recipe that
|
||||
// actually works, NOT `config set embedding_model` (which config.ts
|
||||
// hard-refuses as a schema-sizing no-op).
|
||||
expect(r.stderr).toContain('gbrain init --force --pglite --embedding-model');
|
||||
expect(r.stderr).not.toContain('config set embedding_model');
|
||||
// Config persisted with the deferred-embedding sentinel.
|
||||
const cfg = JSON.parse(readFileSync(join(tmpHome, '.gbrain', 'config.json'), 'utf-8'));
|
||||
expect(cfg.embedding_disabled).toBe(true);
|
||||
expect(cfg.engine).toBe('pglite');
|
||||
}, 240000);
|
||||
|
||||
test('--non-interactive with env-key typo surfaces Levenshtein hint', async () => {
|
||||
test('--non-interactive with env-key typo stays fail-loud with Levenshtein hint', async () => {
|
||||
const r = await runCli(['init', '--pglite', '--non-interactive'], {
|
||||
gbrainHome: tmpHome,
|
||||
gbrainHome: typoHome,
|
||||
env: { OPENAPI_API_KEY: 'sk-test-typo' },
|
||||
});
|
||||
// A near-miss key signals the user MEANT to configure a provider —
|
||||
// completing keyless would silently bury their typo.
|
||||
expect(r.exitCode).toBe(1);
|
||||
// D13 typo detection: surfaces "did you mean OPENAI_API_KEY"
|
||||
expect(r.stderr).toMatch(/did you mean OPENAI_API_KEY/i);
|
||||
// The hint leads with the keyless-continue option.
|
||||
expect(r.stderr).toContain('--no-embedding');
|
||||
}, 60000);
|
||||
|
||||
test('--non-interactive with multiple provider keys auto-picks the canonical default', async () => {
|
||||
const multiHome = makeTempHome();
|
||||
try {
|
||||
const r = await runCli(['init', '--pglite', '--non-interactive'], {
|
||||
gbrainHome: multiHome,
|
||||
env: {
|
||||
OPENAI_API_KEY: 'sk-test-only-for-init-resolution-NOT-CALLED',
|
||||
ZEROENTROPY_API_KEY: 'ze-test-only-for-init-resolution-NOT-CALLED',
|
||||
},
|
||||
});
|
||||
expect(r.exitCode).toBe(0);
|
||||
expect(r.stderr).toContain('Multiple embedding providers env-ready');
|
||||
expect(r.stderr).toContain('Override with --embedding-model');
|
||||
const cfg = JSON.parse(readFileSync(join(multiHome, '.gbrain', 'config.json'), 'utf-8'));
|
||||
// Canonical default (DEFAULT_EMBEDDING_MODEL) wins when its key is present.
|
||||
expect(cfg.embedding_model).toBe('zeroentropyai:zembed-1');
|
||||
} finally {
|
||||
rmSync(multiHome, { recursive: true, force: true });
|
||||
}
|
||||
}, 240000);
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
|
||||
describe('v0.45 DX wave — --supabase non-TTY guard + multi-key no-canonical fail-loud', () => {
|
||||
test('init --supabase without a TTY fails loud and names the --url escape hatch', async () => {
|
||||
// Legacy behavior was a silent exit-0 no-op (stdin closed → readLine
|
||||
// never resolved → process ended with NO config written) — the worst
|
||||
// failure shape for a scripted/agent caller.
|
||||
const home = makeTempHome();
|
||||
try {
|
||||
const r = await runCli(['init', '--supabase'], { gbrainHome: home, env: {} });
|
||||
expect(r.exitCode).toBe(1);
|
||||
expect(r.stderr).toContain('needs an interactive terminal');
|
||||
expect(r.stderr).toContain('--url');
|
||||
} finally {
|
||||
rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
}, 120000);
|
||||
|
||||
test('multiple provider keys with NO canonical candidate stays fail-loud with disambiguation hint', async () => {
|
||||
// The canonical default provider (zeroentropyai) has no key here, so the
|
||||
// non-TTY auto-pick cannot resolve the ambiguity — it must fail loud
|
||||
// (D2/D3), not guess between openai and voyage.
|
||||
const home = makeTempHome();
|
||||
try {
|
||||
const r = await runCli(['init', '--pglite', '--non-interactive'], {
|
||||
gbrainHome: home,
|
||||
env: {
|
||||
OPENAI_API_KEY: 'sk-test-only-for-init-resolution-NOT-CALLED',
|
||||
VOYAGE_API_KEY: 'pa-test-only-for-init-resolution-NOT-CALLED',
|
||||
},
|
||||
});
|
||||
expect(r.exitCode).toBe(1);
|
||||
expect(r.stderr).toMatch(/Multiple embedding providers env-ready/);
|
||||
expect(r.stderr).toMatch(/Disambiguate by passing --embedding-model/);
|
||||
// Fail-loud path exits BEFORE any config write.
|
||||
expect(existsSync(join(home, '.gbrain', 'config.json'))).toBe(false);
|
||||
} finally {
|
||||
rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
}, 120000);
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
|
||||
@@ -0,0 +1,345 @@
|
||||
/**
|
||||
* install-real-hermes door e2e — drives the REAL `hermes` binary (NousResearch
|
||||
* hermes-agent) against THIS checkout's gbrain over stdio MCP.
|
||||
*
|
||||
* WHAT THIS PROVES: gbrain WIRED INTO hermes + recall through MCP, using the
|
||||
* dev tree / compiled binary — the same posture as the claude/codex doors.
|
||||
* It does NOT prove (a) gbrain's own cold install (bun global install, PATH,
|
||||
* migrations — that's the networkless-container placeholder in
|
||||
* heavy-tests.yml), nor (b) an agent FOLLOWING INSTALL_FOR_AGENTS.md
|
||||
* end-to-end (guide-following is the claw-test live lane's job).
|
||||
*
|
||||
* GATING (fail-SKIP, never fail-hard): requires ALL of
|
||||
* - GBRAIN_REAL_HERMES_E2E=1 — explicit opt-in. This file matches the
|
||||
* ordinary test/e2e glob, and unlike the claude/codex doors (whose auth
|
||||
* probes die with a redirected HOME) an env-key auth gate alone would fire
|
||||
* paid multi-minute turns on any dev box with hermes + a key. CI's
|
||||
* hermes-door job and provisioned boxes set the var deliberately;
|
||||
* run-e2e.sh scrubs GBRAIN_* so this suite structurally cannot fire there.
|
||||
* - a resolvable hermes binary
|
||||
* - hermes auth with a NON-EMPTY provider key (blank CI secret ⇒ skip)
|
||||
*
|
||||
* Isolation: every child gets BOTH HOME=<tmp> and HERMES_HOME=<tmp>/.hermes
|
||||
* (HERMES_HOME honoring verified against v0.20.0; the double-set covers either
|
||||
* derivation). A tripwire hashes the operator's real ~/.hermes/config.yaml
|
||||
* before/after — if isolation ever breaks, the suite fails loudly instead of
|
||||
* silently mutating the operator's agent.
|
||||
*
|
||||
* Observed-reality notes (B0, docs/mcp/HERMES-CLI-PIN.md, v0.20.0):
|
||||
* - `hermes mcp add` does a REAL MCP handshake + tool discovery at add time,
|
||||
* then prompts to enable tools; a piped "Y" answers it non-interactively.
|
||||
* Its EXIT CODE IS 0 EVEN ON FAILURE/CANCEL — and a piped "Y" saves the
|
||||
* entry EVEN when the handshake failed (the save-anyway prompt), just with
|
||||
* `enabled: false`. So the hard success discriminators are
|
||||
* (1) `mcp_servers.gbrain.enabled === true` in config.yaml (only a
|
||||
* successful handshake enables) and (2) `hermes mcp test gbrain` exits 0.
|
||||
* - the args flag must be the LAST option (later options are swallowed into
|
||||
* the server argv).
|
||||
*/
|
||||
|
||||
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import {
|
||||
cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync,
|
||||
} from 'node:fs';
|
||||
import { homedir, tmpdir } from 'node:os';
|
||||
import { join, resolve } from 'node:path';
|
||||
import * as yaml from 'js-yaml';
|
||||
|
||||
import {
|
||||
resolveHermesBinary,
|
||||
hasHermesAuth,
|
||||
seedHermesHome,
|
||||
pinHermesModel,
|
||||
hermesOneShotTurn,
|
||||
hermeticChildEnv,
|
||||
hermesChildEnv,
|
||||
resolveGbrainServerCommand,
|
||||
ensureCompiledGbrain,
|
||||
seedBrainForAgent,
|
||||
} from '../helpers/agent-harness.ts';
|
||||
|
||||
const REPO_ROOT = resolve(import.meta.dir, '..', '..');
|
||||
const CLI = join(REPO_ROOT, 'src', 'cli.ts');
|
||||
const HERMES_BIN = resolveHermesBinary();
|
||||
const CAN_RUN = process.env.GBRAIN_REAL_HERMES_E2E === '1' && !!HERMES_BIN && hasHermesAuth();
|
||||
|
||||
if (!CAN_RUN) {
|
||||
const why = process.env.GBRAIN_REAL_HERMES_E2E !== '1'
|
||||
? 'GBRAIN_REAL_HERMES_E2E is not 1 (explicit opt-in required — paid agent turns)'
|
||||
: !HERMES_BIN
|
||||
? 'hermes binary not found'
|
||||
: 'no non-empty hermes provider key (env or ~/.hermes/.env)';
|
||||
console.warn(`[install-real-hermes] SKIP: ${why}`);
|
||||
}
|
||||
|
||||
const ENV_KEYS = [
|
||||
'GBRAIN_HOME', 'GBRAIN_DATABASE_URL', 'DATABASE_URL', 'GBRAIN_BRAIN_ID',
|
||||
'GBRAIN_SOURCE', 'GBRAIN_HOOKS', 'HERMES_HOME',
|
||||
];
|
||||
const SAVED_ENV: Record<string, string | undefined> = {};
|
||||
|
||||
// Tripwire over the operator's REAL hermes config: hash before, compare after.
|
||||
const REAL_HERMES_CONFIG = join(homedir(), '.hermes', 'config.yaml');
|
||||
let realConfigHashBefore: string | null = null;
|
||||
function hashFile(p: string): string | null {
|
||||
try { return createHash('sha256').update(readFileSync(p)).digest('hex'); } catch { return null; }
|
||||
}
|
||||
|
||||
// Evidence trail: homes created during the run get copied (minus .env) into
|
||||
// GBRAIN_E2E_EVIDENCE_DIR when set. The CI workflow uploads that stable path
|
||||
// on failure; copying unconditionally is fine (upload is failure-gated) and
|
||||
// avoids per-test failure plumbing.
|
||||
const EVIDENCE_DIR = process.env.GBRAIN_E2E_EVIDENCE_DIR;
|
||||
const createdHomes: { label: string; home: string }[] = [];
|
||||
function trackHome(label: string): string {
|
||||
const home = mkdtempSync(join(tmpdir(), `gb-hermes-${label}-`));
|
||||
createdHomes.push({ label, home });
|
||||
return home;
|
||||
}
|
||||
function copyEvidence(): void {
|
||||
if (!EVIDENCE_DIR) return;
|
||||
for (const { label, home } of createdHomes) {
|
||||
try {
|
||||
const dst = join(EVIDENCE_DIR, label);
|
||||
mkdirSync(dst, { recursive: true });
|
||||
for (const sub of ['.hermes/logs', '.hermes/sessions', 'usage.json', 'door-config.yaml']) {
|
||||
const src = join(home, sub);
|
||||
if (existsSync(src)) {
|
||||
try { cpSync(src, join(dst, sub.replace(/\//g, '_')), { recursive: true }); } catch { /* best-effort */ }
|
||||
}
|
||||
}
|
||||
// Defensive: never let a provider key land in the artifact.
|
||||
try { rmSync(join(dst, '.env'), { force: true }); } catch { /* best-effort */ }
|
||||
} catch { /* best-effort */ }
|
||||
}
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
for (const k of ENV_KEYS) SAVED_ENV[k] = process.env[k];
|
||||
for (const k of ENV_KEYS) delete process.env[k];
|
||||
realConfigHashBefore = hashFile(REAL_HERMES_CONFIG);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
copyEvidence();
|
||||
for (const { home } of createdHomes) {
|
||||
try { rmSync(home, { recursive: true, force: true }); } catch { /* best-effort */ }
|
||||
}
|
||||
for (const k of ENV_KEYS) {
|
||||
if (SAVED_ENV[k] === undefined) delete process.env[k];
|
||||
else process.env[k] = SAVED_ENV[k];
|
||||
}
|
||||
// Tripwire LAST: if any child escaped the hermetic HERMES_HOME, the
|
||||
// operator's real config changed and this run must scream about it.
|
||||
const after = hashFile(REAL_HERMES_CONFIG);
|
||||
if (realConfigHashBefore !== after) {
|
||||
throw new Error(
|
||||
'HERMETICITY BREACH: the operator\'s real ~/.hermes/config.yaml changed during the door run — ' +
|
||||
'HERMES_HOME isolation failed; investigate before trusting this suite again.',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
/** Run the real hermes binary under a hermetic home. `stdinText` answers
|
||||
* interactive prompts (the enable-tools "Y"). All provider keys are scrubbed
|
||||
* from the child env (hermesChildEnv) — the seeded .env is the single auth
|
||||
* source; a stray second provider key mis-routes hermes's provider-auto
|
||||
* (observed "HTTP 401: Missing Authentication header"). */
|
||||
function runHermes(home: string, argv: string[], stdinText?: string): { code: number | null; stdout: string; stderr: string } {
|
||||
const res = spawnSync(HERMES_BIN!, argv, {
|
||||
env: hermesChildEnv(home),
|
||||
encoding: 'utf8',
|
||||
timeout: 180_000,
|
||||
input: stdinText,
|
||||
});
|
||||
return { code: res.status, stdout: res.stdout ?? '', stderr: res.stderr ?? '' };
|
||||
}
|
||||
|
||||
/** Keyless PGLite brain init via the preferred launcher (compiled binary when
|
||||
* the PGLite-embed probe passes, matching resolveGbrainServerCommand's
|
||||
* preference; bun-run fallback otherwise). */
|
||||
function initBrain(home: string): { code: number | null; stderr: string } {
|
||||
const { binPath } = ensureCompiledGbrain(REPO_ROOT);
|
||||
const argv = binPath
|
||||
? [binPath, 'init', '--pglite', '--no-embedding', '--non-interactive']
|
||||
: ['bun', 'run', CLI, 'init', '--pglite', '--no-embedding', '--non-interactive'];
|
||||
const res = spawnSync(argv[0], argv.slice(1), {
|
||||
cwd: REPO_ROOT,
|
||||
env: hermeticChildEnv({ HOME: home, GBRAIN_HOME: home, GBRAIN_SKIP_STARTUP_HOOKS: '1' }),
|
||||
encoding: 'utf8',
|
||||
timeout: 180_000,
|
||||
});
|
||||
return { code: res.status, stderr: `${res.stdout ?? ''}\n${res.stderr ?? ''}` };
|
||||
}
|
||||
|
||||
interface HermesMcpConfig {
|
||||
mcp_servers?: Record<string, { command?: string; args?: string[]; env?: Record<string, string>; enabled?: boolean }>;
|
||||
}
|
||||
|
||||
function readHermesConfig(home: string): HermesMcpConfig {
|
||||
const p = join(home, '.hermes', 'config.yaml');
|
||||
expect(existsSync(p)).toBe(true);
|
||||
return (yaml.safeLoad(readFileSync(p, 'utf-8')) ?? {}) as HermesMcpConfig;
|
||||
}
|
||||
|
||||
/** Shared registration: real `hermes mcp add` with the piped-Y confirmation.
|
||||
* Observed gotchas (door run + isolation, v0.20.0):
|
||||
* - the env flag takes MULTIPLE KEY=VALUE values after ONE flag; REPEATING
|
||||
* the flag replaces the first occurrence (argparse), which drops
|
||||
* GBRAIN_HOME, kills the server handshake, and the piped Y then answers
|
||||
* the save-anyway prompt as enabled:false — a silently-disabled entry.
|
||||
* - the args flag must be the LAST option (later options are swallowed
|
||||
* into the server argv). */
|
||||
function registerGbrainIntoHermes(
|
||||
home: string,
|
||||
server: { command: string; args: string[] },
|
||||
sourceId: string,
|
||||
): { code: number | null; stdout: string; stderr: string } {
|
||||
return runHermes(home, [
|
||||
'mcp', 'add', 'gbrain',
|
||||
'--env', `GBRAIN_HOME=${home}`, `GBRAIN_SOURCE=${sourceId}`,
|
||||
'--connect-timeout', '60',
|
||||
'--command', server.command,
|
||||
'--args', ...server.args,
|
||||
], 'Y\n');
|
||||
}
|
||||
|
||||
describe.skipIf(!CAN_RUN)('install real-hermes door (serial e2e)', () => {
|
||||
test('version pin: hermes --version matches HERMES_VERSION when the CI pin is set', () => {
|
||||
const pinned = SAVED_ENV.HERMES_VERSION ?? process.env.HERMES_VERSION;
|
||||
const res = runHermes(trackHome('ver'), ['--version']);
|
||||
expect(res.code).toBe(0);
|
||||
if (pinned) {
|
||||
expect(res.stdout).toContain(`v${pinned}`);
|
||||
} else {
|
||||
// Local run without a pin: still assert the observed output shape.
|
||||
expect(res.stdout).toMatch(/Hermes Agent v\d+\.\d+\.\d+/);
|
||||
}
|
||||
}, 120_000);
|
||||
|
||||
test('INSTALL: keyless init → real `hermes mcp add` handshake → config carries server + env → mcp test passes', () => {
|
||||
const home = trackHome('install');
|
||||
seedHermesHome(home);
|
||||
|
||||
const init = initBrain(home);
|
||||
expect(init.code).toBe(0);
|
||||
expect(existsSync(join(home, '.gbrain', 'brain.pglite'))).toBe(true);
|
||||
|
||||
const server = resolveGbrainServerCommand(REPO_ROOT);
|
||||
const add = registerGbrainIntoHermes(home, server, 'default');
|
||||
// Deliberately NO assertion on add.code: observed 0 even on failure — and
|
||||
// the piped Y saves even a FAILED handshake (as enabled:false via the
|
||||
// save-anyway prompt). The handshake success is asserted via
|
||||
// enabled===true below plus the independent mcp test probe…
|
||||
expect(add.stdout).toContain('tool(s)'); // discovery banner fired
|
||||
const cfg = readHermesConfig(home);
|
||||
try { cpSync(join(home, '.hermes', 'config.yaml'), join(home, 'door-config.yaml')); } catch { /* evidence */ }
|
||||
expect(cfg.mcp_servers?.gbrain).toBeDefined();
|
||||
expect(cfg.mcp_servers!.gbrain.command).toBe(server.command);
|
||||
expect(cfg.mcp_servers!.gbrain.args).toEqual(server.args);
|
||||
expect(cfg.mcp_servers!.gbrain.env?.GBRAIN_HOME).toBe(home);
|
||||
expect(cfg.mcp_servers!.gbrain.env?.GBRAIN_SOURCE).toBe('default');
|
||||
expect(cfg.mcp_servers!.gbrain.enabled).toBe(true);
|
||||
|
||||
// …and a second, independent connection: `hermes mcp test` re-spawns the
|
||||
// server and lists tools (observed exit 0 + tool list).
|
||||
const probe = runHermes(home, ['mcp', 'test', 'gbrain']);
|
||||
expect(probe.code).toBe(0);
|
||||
|
||||
// Soft probe (exit-0-only; output shape logged, not asserted).
|
||||
const list = runHermes(home, ['mcp', 'list']);
|
||||
expect(list.code).toBe(0);
|
||||
if (!list.stdout.includes('gbrain')) {
|
||||
console.warn('[install-real-hermes] mcp list output did not mention gbrain — shape drift? output:', list.stdout.slice(0, 400));
|
||||
}
|
||||
}, 300_000);
|
||||
|
||||
test('INSTALL 1b: the direct config.yaml surface (documented, not a fallback) is accepted independently', () => {
|
||||
const home = trackHome('yaml');
|
||||
seedHermesHome(home);
|
||||
|
||||
const init = initBrain(home);
|
||||
expect(init.code).toBe(0);
|
||||
|
||||
const server = resolveGbrainServerCommand(REPO_ROOT);
|
||||
const configPath = join(home, '.hermes', 'config.yaml');
|
||||
const doc = {
|
||||
mcp_servers: {
|
||||
gbrain: {
|
||||
command: server.command,
|
||||
args: server.args,
|
||||
env: { GBRAIN_HOME: home, GBRAIN_SOURCE: 'default' },
|
||||
connect_timeout: 60,
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
writeFileSync(configPath, yaml.safeDump(doc), 'utf-8');
|
||||
|
||||
// Targeted probe, asserted HARD (eng D3): `mcp test` connects to exactly
|
||||
// the entry under test. Global `doctor` is NOT asserted here — it
|
||||
// diagnoses hermes-wide health and can fail a hermetic home for reasons
|
||||
// unrelated to our entry; run it as logged evidence only.
|
||||
const probe = runHermes(home, ['mcp', 'test', 'gbrain']);
|
||||
expect(probe.code).toBe(0);
|
||||
expect(probe.stdout.length).toBeGreaterThan(0);
|
||||
|
||||
const doctor = runHermes(home, ['doctor']);
|
||||
console.warn(`[install-real-hermes] doctor (evidence only): exit ${doctor.code}`);
|
||||
}, 300_000);
|
||||
|
||||
test('SMOKE: real hermes -z answers the seeded fact through the gbrain MCP server', async () => {
|
||||
const home = trackHome('smoke');
|
||||
const seeded = await seedBrainForAgent(home, 'workspace');
|
||||
seedHermesHome(home);
|
||||
const pin = pinHermesModel(HERMES_BIN!, home);
|
||||
expect(pin.code).toBe(0);
|
||||
|
||||
const server = resolveGbrainServerCommand(REPO_ROOT);
|
||||
registerGbrainIntoHermes(home, server, 'workspace');
|
||||
const cfg = readHermesConfig(home);
|
||||
expect(cfg.mcp_servers?.gbrain?.enabled).toBe(true);
|
||||
|
||||
// Negative-control channel: hermes's one-shot prints only final text (no
|
||||
// tool-call stream to parse), so the prompt makes tool-absence loudly
|
||||
// detectable. The fact is 100% synthetic and the turn's cwd is the TEMP
|
||||
// HOME — never the repo checkout, where the committed fact in
|
||||
// agent-harness.ts would be greppable without MCP.
|
||||
const prompt =
|
||||
'You have an MCP server named gbrain connected to a knowledge brain. ' +
|
||||
`Using ONLY that brain (no general knowledge, no filesystem search), answer: ${seeded.query} ` +
|
||||
'Report exactly what the brain says. If no gbrain tool is available to you, reply with exactly: NO-GBRAIN-TOOL';
|
||||
|
||||
let finalText = '';
|
||||
let lastExit: number | null = null;
|
||||
for (let attempt = 1; attempt <= 2; attempt++) {
|
||||
const turn = await hermesOneShotTurn({
|
||||
prompt,
|
||||
cwd: home,
|
||||
home,
|
||||
timeoutMs: 240_000,
|
||||
usageFile: join(home, 'usage.json'),
|
||||
});
|
||||
finalText = turn.finalText;
|
||||
lastExit = turn.exitCode;
|
||||
if (turn.exitCode === 0 && finalText.toLowerCase().includes('rivermouth')) break;
|
||||
console.warn(`[install-real-hermes] SMOKE attempt ${attempt}: exit=${turn.exitCode} text=${finalText.slice(0, 200)}`);
|
||||
if (attempt < 2) await new Promise((r) => setTimeout(r, 3_000));
|
||||
}
|
||||
|
||||
// Never-soften criteria: the synthetic fact surfaced AND the no-tool
|
||||
// control token did not.
|
||||
expect(lastExit).toBe(0);
|
||||
expect(finalText.toLowerCase()).toContain('rivermouth');
|
||||
expect(finalText).not.toContain('NO-GBRAIN-TOOL');
|
||||
|
||||
// Best-effort evidence sweep (logged, not asserted — first provisioned
|
||||
// run tells us whether session artifacts are promotable to hard asserts).
|
||||
try {
|
||||
const usage = JSON.parse(readFileSync(join(home, 'usage.json'), 'utf-8'));
|
||||
console.warn('[install-real-hermes] usage:', JSON.stringify(usage).slice(0, 300));
|
||||
} catch { /* absent — fine */ }
|
||||
}, 480_000);
|
||||
});
|
||||
@@ -77,6 +77,21 @@ describe('self-upgrade marker on a real invocation', () => {
|
||||
expect(stderr).not.toContain('UPGRADE_AVAILABLE');
|
||||
});
|
||||
|
||||
test('cache latest == running version → suppressed (no marker, no human sentence)', () => {
|
||||
writeCache(`UPGRADE_AVAILABLE ${VERSION} ${VERSION}`);
|
||||
const { stderr } = runGbrain('notify');
|
||||
expect(stderr).not.toContain('UPGRADE_AVAILABLE');
|
||||
expect(stderr).not.toContain('Run: gbrain self-upgrade');
|
||||
});
|
||||
|
||||
test('foreign-writer cache → marker prints the RUNNING version, not the writer\'s', () => {
|
||||
// An older gbrain on PATH wrote the cache: marker.current is 0.0.1, not us.
|
||||
writeCache('UPGRADE_AVAILABLE 0.0.1 0.99.0');
|
||||
const { stderr } = runGbrain('notify');
|
||||
expect(stderr).toContain(`UPGRADE_AVAILABLE ${VERSION} 0.99.0`);
|
||||
expect(stderr).not.toContain('UPGRADE_AVAILABLE 0.0.1');
|
||||
});
|
||||
|
||||
test('active snooze for the version → no marker (notify mode honors snooze)', () => {
|
||||
writeCache(`UPGRADE_AVAILABLE ${VERSION} 0.99.0`);
|
||||
// snooze record: "<version> <level> <epoch-ms>" — fresh ts so it's active.
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* test/e2e/workspace-generic-compat.test.ts — generic-workspace compat gate.
|
||||
*
|
||||
* Pins gbrain's DOCUMENTED CONTRACT for the INSTALL_FOR_AGENTS.md
|
||||
* "any repo with a workspace" flow: the cwd_walk_up detection tier
|
||||
* (src/core/repo-root.ts tier 1b), scaffold additivity
|
||||
* (src/core/skillpack/scaffold.ts contracts 1–3), and check-resolvable
|
||||
* against a root AGENTS.md with no manifest.json. Hermes is the
|
||||
* motivating consumer of this flow; the real Hermes-behavior proof is
|
||||
* the Phase B door test (test/e2e/install-real-hermes.serial.test.ts),
|
||||
* not this file — this one runs everywhere, PGLite/no-DB, ungated.
|
||||
*
|
||||
* Fixture: `test/fixtures/generic-agents-workspace/` — AGENTS.md at
|
||||
* workspace root, two skills below, deliberately nothing
|
||||
* OpenClaw-specific. Structural template: openclaw-reference-compat.test.ts.
|
||||
*/
|
||||
|
||||
import { describe, expect, it, afterEach } from 'bun:test';
|
||||
import { existsSync, mkdtempSync, rmSync, writeFileSync, appendFileSync, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import { spawnSync } from 'child_process';
|
||||
|
||||
import { checkResolvable } from '../../src/core/check-resolvable.ts';
|
||||
import { autoDetectSkillsDir } from '../../src/core/repo-root.ts';
|
||||
import { runScaffold } from '../../src/core/skillpack/scaffold.ts';
|
||||
import { findGbrainRoot } from '../../src/core/skillpack/bundle.ts';
|
||||
|
||||
const FIXTURE = join(import.meta.dir, '..', 'fixtures', 'generic-agents-workspace');
|
||||
const SKILLS_DIR = join(FIXTURE, 'skills');
|
||||
const REPO = join(import.meta.dir, '..', '..');
|
||||
const CLI = join(REPO, 'src', 'cli.ts');
|
||||
|
||||
const created: string[] = [];
|
||||
afterEach(() => {
|
||||
while (created.length) {
|
||||
const d = created.pop();
|
||||
if (d && existsSync(d)) rmSync(d, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe('generic agent-workspace compat (INSTALL_FOR_AGENTS.md flow)', () => {
|
||||
it('fixture shape: root AGENTS.md, skills/ below, no manifest.json', () => {
|
||||
expect(existsSync(FIXTURE)).toBe(true);
|
||||
expect(existsSync(join(FIXTURE, 'AGENTS.md'))).toBe(true);
|
||||
expect(existsSync(SKILLS_DIR)).toBe(true);
|
||||
expect(existsSync(join(SKILLS_DIR, 'manifest.json'))).toBe(false);
|
||||
});
|
||||
|
||||
it('auto-detects skills dir via the cwd_walk_up tier (no env needed)', () => {
|
||||
// IMPORTANT: the replacement env guards against the OPERATOR's
|
||||
// GBRAIN_SKILLS_DIR (tier 0) / OPENCLAW_WORKSPACE (tier 1) leaking
|
||||
// in from the test runner's environment; the home tier sits BELOW
|
||||
// cwd_walk_up in the priority order and could never preempt it.
|
||||
const emptyHome = mkdtempSync(join(tmpdir(), 'generic-ws-home-'));
|
||||
created.push(emptyHome);
|
||||
const detected = autoDetectSkillsDir(FIXTURE, { HOME: emptyHome });
|
||||
expect(detected.dir).toBe(SKILLS_DIR);
|
||||
expect(detected.source).toBe('cwd_walk_up');
|
||||
});
|
||||
|
||||
it('GBRAIN_SKILLS_DIR explicit override wins over cwd_walk_up', () => {
|
||||
// Tier 0 requires a resolver file inside the pointed-at dir, so the
|
||||
// override target ships its own AGENTS.md. Starting from FIXTURE
|
||||
// (which WOULD match cwd_walk_up), the explicit env must win.
|
||||
const emptyHome = mkdtempSync(join(tmpdir(), 'generic-ws-home-'));
|
||||
created.push(emptyHome);
|
||||
const override = mkdtempSync(join(tmpdir(), 'generic-ws-override-'));
|
||||
created.push(override);
|
||||
writeFileSync(join(override, 'AGENTS.md'), '# AGENTS\n');
|
||||
|
||||
const detected = autoDetectSkillsDir(FIXTURE, {
|
||||
GBRAIN_SKILLS_DIR: override,
|
||||
HOME: emptyHome,
|
||||
});
|
||||
expect(detected.dir).toBe(override);
|
||||
expect(detected.source).toBe('env_explicit');
|
||||
});
|
||||
|
||||
it('checkResolvable accepts root AGENTS.md — all skills reachable, no errors', () => {
|
||||
const report = checkResolvable(SKILLS_DIR);
|
||||
expect(report.ok).toBe(true);
|
||||
expect(report.errors).toEqual([]);
|
||||
expect(report.summary.total_skills).toBe(2);
|
||||
expect(report.summary.reachable).toBe(2);
|
||||
expect(report.summary.unreachable).toBe(0);
|
||||
});
|
||||
|
||||
it('CLI subprocess: check-resolvable with json + skills-dir flags exits 0, JSON clean', () => {
|
||||
const r = spawnSync(
|
||||
'bun',
|
||||
[CLI, 'check-resolvable', '--json', '--skills-dir', SKILLS_DIR],
|
||||
{ encoding: 'utf-8', cwd: REPO, maxBuffer: 10 * 1024 * 1024 },
|
||||
);
|
||||
expect(r.status).toBe(0);
|
||||
const env = JSON.parse(r.stdout);
|
||||
expect(env.ok).toBe(true);
|
||||
expect(env.report.errors).toEqual([]);
|
||||
expect(env.report.summary.total_skills).toBe(2);
|
||||
});
|
||||
|
||||
it('scaffold is additive into an AGENTS.md-shell workspace and refuses overwrite on re-run', () => {
|
||||
// Fresh workspace with only the AGENTS.md shell — the documented
|
||||
// starting state for a generic repo adopting gbrain skills.
|
||||
const target = mkdtempSync(join(tmpdir(), 'generic-ws-scaffold-'));
|
||||
created.push(target);
|
||||
const shell = '# AGENTS\n\n| Trigger | Skill |\n|---------|-------|\n';
|
||||
writeFileSync(join(target, 'AGENTS.md'), shell);
|
||||
|
||||
const gbrainRoot = findGbrainRoot();
|
||||
expect(gbrainRoot).not.toBeNull();
|
||||
|
||||
const first = runScaffold({
|
||||
gbrainRoot: gbrainRoot!,
|
||||
targetWorkspace: target,
|
||||
skillSlug: 'query',
|
||||
});
|
||||
expect(first.summary.wroteNew).toBeGreaterThan(0);
|
||||
// The skill's SKILL.md lands under skills/query/; every file the
|
||||
// scaffold reports as written actually exists; paired sources (from
|
||||
// frontmatter, when the skill declares any) are written, not skipped.
|
||||
const skillMd = join(target, 'skills', 'query', 'SKILL.md');
|
||||
expect(existsSync(skillMd)).toBe(true);
|
||||
for (const f of first.files) {
|
||||
if (f.outcome === 'wrote_new') expect(existsSync(f.target)).toBe(true);
|
||||
}
|
||||
expect(first.summary.pairedSourcesWritten).toBe(
|
||||
first.files.filter(f => f.outcome === 'wrote_new' && f.pairedSource).length,
|
||||
);
|
||||
|
||||
// check-resolvable stays clean after scaffold: the scaffolded skill
|
||||
// is reachable via its own frontmatter triggers (scaffold contract 1
|
||||
// — no managed-block writes needed).
|
||||
const report = checkResolvable(join(target, 'skills'));
|
||||
expect(report.ok).toBe(true);
|
||||
expect(report.errors).toEqual([]);
|
||||
expect(report.summary.unreachable).toBe(0);
|
||||
|
||||
// Contract 2 (scaffold.ts header): once a file lands, the user owns
|
||||
// it — a re-run must skip every existing file and preserve edits.
|
||||
const marker = '\n<!-- user-owned edit: alice-example -->\n';
|
||||
appendFileSync(skillMd, marker);
|
||||
const second = runScaffold({
|
||||
gbrainRoot: gbrainRoot!,
|
||||
targetWorkspace: target,
|
||||
skillSlug: 'query',
|
||||
});
|
||||
expect(second.summary.wroteNew).toBe(0);
|
||||
expect(second.summary.skippedExisting).toBe(first.summary.wroteNew);
|
||||
expect(readFileSync(skillMd, 'utf-8')).toContain(marker.trim());
|
||||
// Byproduct check: the host resolver file was never touched.
|
||||
expect(readFileSync(join(target, 'AGENTS.md'), 'utf-8')).toBe(shell);
|
||||
});
|
||||
});
|
||||
@@ -3,11 +3,11 @@
|
||||
You are testing gbrain on a brand-new install. The user just ran `gbrain init` for the first time. Walk through the canonical first-day flow:
|
||||
|
||||
1. **Verify install:** confirm `gbrain --version` works and `gbrain doctor --json` returns a valid JSON object with a `status` field.
|
||||
2. **Install skillpack:** run `gbrain skillpack install --workspace $PWD`. The workspace already has an `AGENTS.md` routing file.
|
||||
2. **Scaffold the skillpack:** run `gbrain skillpack scaffold --all --workspace $PWD`. The workspace already has an `AGENTS.md` routing file.
|
||||
3. **Import the brain:** run `gbrain import ./brain --no-embed --progress-json`. There are 3 small markdown pages already there.
|
||||
4. **Query the brain:** run `gbrain query "alice"` and verify >0 results.
|
||||
5. **Extract links:** run `gbrain extract --source fs --progress-json`.
|
||||
6. **Verify health:** run `gbrain doctor --json`. The `status` field should be `"ok"`.
|
||||
5. **Extract links:** run `gbrain extract all --source fs --dir ./brain --progress-json`.
|
||||
6. **Verify health:** run `gbrain doctor --json`. The `status` field should be `"healthy"` or `"warnings"` — never `"unhealthy"`.
|
||||
|
||||
## Friction protocol
|
||||
|
||||
|
||||
@@ -6,5 +6,10 @@
|
||||
"extract.links_fs",
|
||||
"doctor.db_checks"
|
||||
],
|
||||
"brain": "brain"
|
||||
"brain": "brain",
|
||||
"oracle": {
|
||||
"query": "alice",
|
||||
"min_results": 1,
|
||||
"files_exist": ["skills/query/SKILL.md"]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ You inherit a gbrain v0.18 brain (the harness has already replayed a seed SQL du
|
||||
|
||||
1. **Run `gbrain doctor --json`** first. Note any warnings or fix-hints.
|
||||
2. **Run `gbrain init --pglite`** with the existing database path. The migration chain should detect the old `schema_version` and walk forward to the latest.
|
||||
3. **Run `gbrain doctor --json` again.** The `status` field should be `"ok"`.
|
||||
3. **Run `gbrain doctor --json` again.** The `status` field should be `"healthy"` or `"warnings"` — never `"unhealthy"`.
|
||||
4. **Verify queries still work:** `gbrain query "alice"` should return results from the seeded brain.
|
||||
|
||||
## Friction protocol
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"min_pages_after_migration": 1,
|
||||
"doctor_status": "ok"
|
||||
}
|
||||
@@ -6,5 +6,9 @@
|
||||
"doctor.db_checks"
|
||||
],
|
||||
"seed": "seed",
|
||||
"brain": "brain"
|
||||
"brain": "brain",
|
||||
"oracle": {
|
||||
"query": "alice",
|
||||
"min_results": 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
# AGENTS.md
|
||||
|
||||
Minimal fixture mimicking a GENERIC agent workspace — the shape
|
||||
INSTALL_FOR_AGENTS.md's "any repo with a workspace" flow targets.
|
||||
AGENTS.md lives at workspace root; skills live under `skills/`. No
|
||||
manifest.json (the auto-derive path in `src/core/skill-manifest.ts`
|
||||
handles this). Unlike `openclaw-reference-minimal/`, nothing here is
|
||||
OpenClaw-specific: no OPENCLAW_WORKSPACE env, no plugin layout — just
|
||||
a repo with a root AGENTS.md and a bare `skills/` directory. All
|
||||
content is synthetic (alice-example style placeholders only).
|
||||
|
||||
## Brain operations
|
||||
|
||||
| Trigger | Skill |
|
||||
|---------|-------|
|
||||
| "what do we know about", "search for", "lookup" | `skills/query/SKILL.md` |
|
||||
| any brain read/write/lookup/citation | `skills/brain-ops/SKILL.md` |
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
name: brain-ops
|
||||
description: Core read/write cycle for the generic agent-workspace fixture.
|
||||
triggers:
|
||||
- any brain read/write/lookup/citation
|
||||
writes_pages: true
|
||||
writes_to:
|
||||
- people/
|
||||
- companies/
|
||||
---
|
||||
|
||||
# brain-ops
|
||||
|
||||
Fixture skill for `test/e2e/workspace-generic-compat.test.ts`.
|
||||
Example filing targets: `people/alice-example.md`, `companies/acme-example.md`.
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
name: query
|
||||
description: Look up brain pages in the generic agent-workspace fixture.
|
||||
triggers:
|
||||
- "what do we know about"
|
||||
- "search for"
|
||||
- "lookup"
|
||||
---
|
||||
|
||||
# query
|
||||
|
||||
Fixture skill for `test/e2e/workspace-generic-compat.test.ts`.
|
||||
Example lookup: "what do we know about alice-example".
|
||||
@@ -0,0 +1,378 @@
|
||||
/**
|
||||
* gbrain friction diff — cross-run comparison tests (Phase E).
|
||||
*
|
||||
* Zero-binary: exercises runFriction dispatch + the exported diff helpers
|
||||
* directly against a tmp GBRAIN_HOME, same conventions as friction-cli.test.ts.
|
||||
*
|
||||
* Spec anchors (plan Phase E + adversarial gate): identity is (kind, phase,
|
||||
* normalized message prefix — digits collapsed) with severity EXCLUDED (it is
|
||||
* the compared attribute, as a per-severity multiset); identities are
|
||||
* MULTISETS (count deltas are differences); only kind friction|delight is
|
||||
* diffable (markers/interruptions feed the compatibility banner); exit codes
|
||||
* 0 = ran, 1 = resolution/IO error, 2 = usage error.
|
||||
*/
|
||||
|
||||
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
|
||||
import { mkdtempSync, rmSync, appendFileSync, utimesSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
import { runFriction, computeFrictionDiff, resolveRunSpec } from '../src/commands/friction.ts';
|
||||
import { logFriction, frictionFile } from '../src/core/friction.ts';
|
||||
import type { FrictionSeverity } from '../src/core/friction.ts';
|
||||
|
||||
const ORIG_HOME = process.env.GBRAIN_HOME;
|
||||
const ORIG_RUN_ID = process.env.GBRAIN_FRICTION_RUN_ID;
|
||||
let tmp: string;
|
||||
let stdoutLines: string[];
|
||||
let stderrLines: string[];
|
||||
let origStdoutWrite: typeof process.stdout.write;
|
||||
let origConsoleLog: typeof console.log;
|
||||
let origConsoleError: typeof console.error;
|
||||
|
||||
beforeEach(() => {
|
||||
tmp = mkdtempSync(join(tmpdir(), 'friction-diff-'));
|
||||
process.env.GBRAIN_HOME = tmp;
|
||||
delete process.env.GBRAIN_FRICTION_RUN_ID;
|
||||
stdoutLines = [];
|
||||
stderrLines = [];
|
||||
origStdoutWrite = process.stdout.write.bind(process.stdout);
|
||||
origConsoleLog = console.log;
|
||||
origConsoleError = console.error;
|
||||
process.stdout.write = ((chunk: string) => { stdoutLines.push(String(chunk)); return true; }) as any;
|
||||
console.log = (...args: unknown[]) => { stdoutLines.push(args.join(' ') + '\n'); };
|
||||
console.error = (...args: unknown[]) => { stderrLines.push(args.join(' ') + '\n'); };
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.env.GBRAIN_HOME = ORIG_HOME;
|
||||
if (ORIG_RUN_ID !== undefined) process.env.GBRAIN_FRICTION_RUN_ID = ORIG_RUN_ID;
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
process.stdout.write = origStdoutWrite;
|
||||
console.log = origConsoleLog;
|
||||
console.error = origConsoleError;
|
||||
});
|
||||
|
||||
/** Stamp the run-start marker the harness writes on every run. */
|
||||
function startMarker(runId: string, opts: { agent?: string; scenario?: string } = {}): void {
|
||||
logFriction({
|
||||
runId, phase: 'run', kind: 'phase-marker', marker: 'start',
|
||||
message: 'run start', source: 'harness',
|
||||
agent: opts.agent, scenario: opts.scenario,
|
||||
});
|
||||
}
|
||||
|
||||
function friction(runId: string, phase: string, message: string, severity: FrictionSeverity = 'error'): void {
|
||||
logFriction({ runId, phase, message, severity, kind: 'friction', source: 'claw' });
|
||||
}
|
||||
|
||||
function diffJson(base: string, compare: string): { code: number; parsed: any } {
|
||||
stdoutLines.length = 0;
|
||||
const code = runFriction(['diff', '--base', base, '--compare', compare, '--json']);
|
||||
const out = stdoutLines.join('').trim();
|
||||
return { code, parsed: out ? JSON.parse(out) : undefined };
|
||||
}
|
||||
|
||||
describe('three sections', () => {
|
||||
test('unique-to-compare, unique-to-base, and severity change land in the right buckets', () => {
|
||||
startMarker('run-base', { agent: 'scripted', scenario: 'fresh-install' });
|
||||
friction('run-base', 'install', 'init did not say which engine', 'error');
|
||||
startMarker('run-cmp', { agent: 'scripted', scenario: 'fresh-install' });
|
||||
friction('run-cmp', 'install', 'init did not say which engine', 'blocker'); // X, severity bumped
|
||||
friction('run-cmp', 'query', 'query returned nothing for a seeded doc', 'confused'); // Y
|
||||
|
||||
const { code, parsed } = diffJson('run-base', 'run-cmp');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.unique_to_base).toEqual([]);
|
||||
expect(parsed.unique_to_compare.length).toBe(1);
|
||||
expect(parsed.unique_to_compare[0].phase).toBe('query');
|
||||
expect(parsed.unique_to_compare[0].message).toContain('query returned nothing');
|
||||
expect(parsed.changed.length).toBe(1);
|
||||
expect(parsed.changed[0].phase).toBe('install');
|
||||
expect(parsed.changed[0].severity_changed).toBe(true);
|
||||
expect(parsed.changed[0].count_changed).toBe(false);
|
||||
expect(parsed.changed[0].base_severities).toEqual(['error']);
|
||||
expect(parsed.changed[0].compare_severities).toEqual(['blocker']);
|
||||
});
|
||||
|
||||
test('human output labels sections by run-id (instrument, not judge) and groups by phase', () => {
|
||||
startMarker('run-base', { agent: 'scripted', scenario: 'fresh-install' });
|
||||
friction('run-base', 'install', 'only in base run');
|
||||
startMarker('run-cmp', { agent: 'scripted', scenario: 'fresh-install' });
|
||||
friction('run-cmp', 'query', 'only in compare run');
|
||||
|
||||
stdoutLines.length = 0;
|
||||
const code = runFriction(['diff', '--base', 'run-base', '--compare', 'run-cmp']);
|
||||
expect(code).toBe(0);
|
||||
const out = stdoutLines.join('');
|
||||
expect(out).toContain('Unique to `run-cmp`');
|
||||
expect(out).toContain('Unique to `run-base`');
|
||||
expect(out).toContain('Shared but changed');
|
||||
expect(out).toContain('### `install`');
|
||||
expect(out).toContain('### `query`');
|
||||
});
|
||||
});
|
||||
|
||||
describe('multiset counts', () => {
|
||||
test('1×error vs 10×error of the same identity is a count delta, not equality', () => {
|
||||
startMarker('run-a', { agent: 'scripted' });
|
||||
friction('run-a', 'embed', 'embed batch rejected by provider', 'error');
|
||||
startMarker('run-b', { agent: 'scripted' });
|
||||
for (let i = 0; i < 10; i++) friction('run-b', 'embed', 'embed batch rejected by provider', 'error');
|
||||
|
||||
const { code, parsed } = diffJson('run-a', 'run-b');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.unique_to_base).toEqual([]);
|
||||
expect(parsed.unique_to_compare).toEqual([]);
|
||||
expect(parsed.changed.length).toBe(1);
|
||||
expect(parsed.changed[0].base_count).toBe(1);
|
||||
expect(parsed.changed[0].compare_count).toBe(10);
|
||||
expect(parsed.changed[0].count_changed).toBe(true);
|
||||
expect(parsed.changed[0].severity_changed).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('kind filter', () => {
|
||||
test('phase-marker and interrupted entries never enter the three sections; they feed the banner', () => {
|
||||
startMarker('run-a', { agent: 'scripted' });
|
||||
logFriction({ runId: 'run-a', phase: 'run', kind: 'phase-marker', marker: 'end', message: 'MARKER SENTINEL A', source: 'harness' });
|
||||
logFriction({ runId: 'run-a', phase: 'run', kind: 'interrupted', message: 'INTERRUPT SENTINEL A', source: 'harness' });
|
||||
friction('run-a', 'install', 'real base friction');
|
||||
startMarker('run-b', { agent: 'scripted' });
|
||||
logFriction({ runId: 'run-b', phase: 'run', kind: 'phase-marker', marker: 'end', message: 'MARKER SENTINEL B', source: 'harness' });
|
||||
logFriction({ runId: 'run-b', phase: 'run', kind: 'interrupted', message: 'INTERRUPT SENTINEL B', source: 'harness' });
|
||||
friction('run-b', 'install', 'real compare friction');
|
||||
|
||||
const { code, parsed } = diffJson('run-a', 'run-b');
|
||||
expect(code).toBe(0);
|
||||
const sections = JSON.stringify([parsed.unique_to_base, parsed.unique_to_compare, parsed.changed]);
|
||||
expect(sections).not.toContain('SENTINEL');
|
||||
expect(parsed.unique_to_base.length).toBe(1);
|
||||
expect(parsed.unique_to_compare.length).toBe(1);
|
||||
expect(parsed.banner.base.interrupted).toBe(true);
|
||||
expect(parsed.banner.compare.interrupted).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('run resolution', () => {
|
||||
test('agent name resolves to the LATEST run carrying that agent', () => {
|
||||
startMarker('older-run', { agent: 'hermes' });
|
||||
friction('older-run', 'install', 'old friction');
|
||||
startMarker('newer-run', { agent: 'hermes' });
|
||||
friction('newer-run', 'install', 'new friction');
|
||||
// Deterministic mtimes: older-run well in the past, newer-run now.
|
||||
const past = new Date(Date.now() - 60_000);
|
||||
utimesSync(frictionFile('older-run'), past, past);
|
||||
|
||||
expect(resolveRunSpec('hermes')).toBe('newer-run');
|
||||
|
||||
const { code, parsed } = diffJson('hermes', 'older-run');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.base).toBe('newer-run');
|
||||
expect(parsed.compare).toBe('older-run');
|
||||
});
|
||||
|
||||
test('an exact run-id wins over agent-name interpretation', () => {
|
||||
// A run whose run-id IS the string 'hermes', carrying a different agent...
|
||||
startMarker('hermes', { agent: 'scripted' });
|
||||
// ...and a newer run whose AGENT is 'hermes'.
|
||||
startMarker('agent-stamped-run', { agent: 'hermes' });
|
||||
const past = new Date(Date.now() - 60_000);
|
||||
utimesSync(frictionFile('hermes'), past, past);
|
||||
|
||||
expect(resolveRunSpec('hermes')).toBe('hermes');
|
||||
});
|
||||
});
|
||||
|
||||
describe('failure semantics', () => {
|
||||
test('unknown run or agent exits 1 and stderr lists available runs', () => {
|
||||
startMarker('run-known', { agent: 'scripted' });
|
||||
stderrLines.length = 0;
|
||||
const code = runFriction(['diff', '--base', 'no-such-run', '--compare', 'run-known']);
|
||||
expect(code).toBe(1);
|
||||
const err = stderrLines.join('');
|
||||
expect(err).toContain('no-such-run');
|
||||
expect(err).toContain('available runs');
|
||||
expect(err).toContain('run-known');
|
||||
});
|
||||
|
||||
test('no runs at all: unknown spec exits 1 with an empty available list', () => {
|
||||
const code = runFriction(['diff', '--base', 'ghost-a', '--compare', 'ghost-b']);
|
||||
expect(code).toBe(1);
|
||||
expect(stderrLines.join('')).toContain('(none)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('empty and identical runs', () => {
|
||||
test('a run with only a start marker produces a valid empty diff, exit 0', () => {
|
||||
startMarker('empty-a', { agent: 'scripted', scenario: 'fresh-install' });
|
||||
startMarker('empty-b', { agent: 'scripted', scenario: 'fresh-install' });
|
||||
const { code, parsed } = diffJson('empty-a', 'empty-b');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.unique_to_base).toEqual([]);
|
||||
expect(parsed.unique_to_compare).toEqual([]);
|
||||
expect(parsed.changed).toEqual([]);
|
||||
expect(parsed.banner.warnings).toEqual([]);
|
||||
});
|
||||
|
||||
test('base equals compare: valid no-difference output, exit 0', () => {
|
||||
startMarker('run-same', { agent: 'scripted' });
|
||||
friction('run-same', 'install', 'a friction entry');
|
||||
const { code, parsed } = diffJson('run-same', 'run-same');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.base).toBe('run-same');
|
||||
expect(parsed.compare).toBe('run-same');
|
||||
expect(parsed.unique_to_base).toEqual([]);
|
||||
expect(parsed.unique_to_compare).toEqual([]);
|
||||
expect(parsed.changed).toEqual([]);
|
||||
|
||||
stdoutLines.length = 0;
|
||||
expect(runFriction(['diff', '--base', 'run-same', '--compare', 'run-same'])).toBe(0);
|
||||
expect(stdoutLines.join('')).toContain('No differences');
|
||||
});
|
||||
});
|
||||
|
||||
describe('malformed JSONL tolerance', () => {
|
||||
test('a malformed line is skipped, its count surfaces, and the diff is still produced', () => {
|
||||
startMarker('mal-run', { agent: 'scripted' });
|
||||
appendFileSync(frictionFile('mal-run'), '{this is not json\n', 'utf-8');
|
||||
friction('mal-run', 'install', 'friction after the bad line');
|
||||
startMarker('clean-run', { agent: 'scripted' });
|
||||
|
||||
const { code, parsed } = diffJson('mal-run', 'clean-run');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.banner.base.malformed).toBe(1);
|
||||
expect(parsed.banner.compare.malformed).toBe(0);
|
||||
expect(parsed.unique_to_base.length).toBe(1);
|
||||
|
||||
stdoutLines.length = 0;
|
||||
runFriction(['diff', '--base', 'mal-run', '--compare', 'clean-run']);
|
||||
expect(stdoutLines.join('')).toContain('1 malformed line(s) skipped');
|
||||
});
|
||||
});
|
||||
|
||||
describe('compatibility banner', () => {
|
||||
test('scenario mismatch warns loudly, naming both scenarios', () => {
|
||||
startMarker('scen-a', { agent: 'scripted', scenario: 'fresh-install' });
|
||||
startMarker('scen-b', { agent: 'scripted', scenario: 'upgrade-from-v0.18' });
|
||||
|
||||
const { code, parsed } = diffJson('scen-a', 'scen-b');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.banner.warnings.length).toBeGreaterThanOrEqual(1);
|
||||
const joined = parsed.banner.warnings.join('\n');
|
||||
expect(joined).toContain('fresh-install');
|
||||
expect(joined).toContain('upgrade-from-v0.18');
|
||||
|
||||
stdoutLines.length = 0;
|
||||
runFriction(['diff', '--base', 'scen-a', '--compare', 'scen-b']);
|
||||
const out = stdoutLines.join('');
|
||||
expect(out).toContain('WARN');
|
||||
expect(out).toContain('fresh-install');
|
||||
expect(out).toContain('upgrade-from-v0.18');
|
||||
});
|
||||
|
||||
test('banner carries agent, scenario, and gbrain version from the start markers', () => {
|
||||
startMarker('meta-a', { agent: 'hermes', scenario: 'fresh-install' });
|
||||
startMarker('meta-b', { agent: 'openclaw', scenario: 'fresh-install' });
|
||||
const { parsed } = diffJson('meta-a', 'meta-b');
|
||||
expect(parsed.banner.base.agent).toBe('hermes');
|
||||
expect(parsed.banner.compare.agent).toBe('openclaw');
|
||||
expect(parsed.banner.base.scenario).toBe('fresh-install');
|
||||
expect(typeof parsed.banner.base.gbrain_version).toBe('string');
|
||||
// Same version on both sides (same process) → no version warning.
|
||||
expect(parsed.banner.warnings).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('identity normalization', () => {
|
||||
test('case, whitespace, and redaction differences collapse to the same identity', () => {
|
||||
startMarker('norm-a', { agent: 'scripted' });
|
||||
friction('norm-a', 'install', `Engine Init FAILED at ${process.cwd()}/x.ts`, 'error');
|
||||
startMarker('norm-b', { agent: 'scripted' });
|
||||
friction('norm-b', 'install', 'engine init failed at <CWD>/x.ts', 'error');
|
||||
|
||||
const diff = computeFrictionDiff('norm-a', 'norm-b');
|
||||
expect(diff.unique_to_base).toEqual([]);
|
||||
expect(diff.unique_to_compare).toEqual([]);
|
||||
expect(diff.changed).toEqual([]);
|
||||
});
|
||||
|
||||
test('messages sharing the first 80 normalized chars are the same identity', () => {
|
||||
const prefix = 'p'.repeat(80);
|
||||
startMarker('pre-a', { agent: 'scripted' });
|
||||
friction('pre-a', 'query', prefix + ' tail one', 'error');
|
||||
startMarker('pre-b', { agent: 'scripted' });
|
||||
friction('pre-b', 'query', prefix + ' completely different tail', 'error');
|
||||
|
||||
const diff = computeFrictionDiff('pre-a', 'pre-b');
|
||||
expect(diff.unique_to_base).toEqual([]);
|
||||
expect(diff.unique_to_compare).toEqual([]);
|
||||
expect(diff.changed).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('CLI dispatch', () => {
|
||||
test('diff with base and compare run-ids returns 0', () => {
|
||||
startMarker('cli-a', { agent: 'scripted' });
|
||||
startMarker('cli-b', { agent: 'scripted' });
|
||||
expect(runFriction(['diff', '--base', 'cli-a', '--compare', 'cli-b'])).toBe(0);
|
||||
});
|
||||
|
||||
test('missing compare flag is a usage error, exit 2', () => {
|
||||
const code = runFriction(['diff', '--base', 'cli-a']);
|
||||
expect(code).toBe(2);
|
||||
expect(stderrLines.join('')).toContain('usage');
|
||||
});
|
||||
|
||||
test('missing both flags is a usage error, exit 2', () => {
|
||||
expect(runFriction(['diff'])).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('identity hardening (adversarial-gate pins)', () => {
|
||||
test('a delight→friction flip surfaces as unique-to-each, never equality', () => {
|
||||
startMarker('run-base', { agent: 'scripted' });
|
||||
logFriction({ runId: 'run-base', phase: 'query', message: 'results ranked well', kind: 'delight', source: 'claw' });
|
||||
startMarker('run-cmp', { agent: 'scripted' });
|
||||
logFriction({ runId: 'run-cmp', phase: 'query', message: 'results ranked well', kind: 'friction', severity: 'confused', source: 'claw' });
|
||||
|
||||
const { code, parsed } = diffJson('run-base', 'run-cmp');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.changed).toEqual([]);
|
||||
expect(parsed.unique_to_base.length).toBe(1);
|
||||
expect(parsed.unique_to_base[0].kind).toBe('delight');
|
||||
expect(parsed.unique_to_compare.length).toBe(1);
|
||||
expect(parsed.unique_to_compare[0].kind).toBe('friction');
|
||||
});
|
||||
|
||||
test('severity redistribution with equal totals and equal severity sets is a reported difference', () => {
|
||||
startMarker('run-base', { agent: 'scripted' });
|
||||
friction('run-base', 'import', 'import warned', 'error');
|
||||
friction('run-base', 'import', 'import warned', 'error');
|
||||
friction('run-base', 'import', 'import warned', 'nit');
|
||||
startMarker('run-cmp', { agent: 'scripted' });
|
||||
friction('run-cmp', 'import', 'import warned', 'error');
|
||||
friction('run-cmp', 'import', 'import warned', 'nit');
|
||||
friction('run-cmp', 'import', 'import warned', 'nit');
|
||||
|
||||
const { code, parsed } = diffJson('run-base', 'run-cmp');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.changed.length).toBe(1);
|
||||
expect(parsed.changed[0].severity_changed).toBe(true);
|
||||
expect(parsed.changed[0].count_changed).toBe(false);
|
||||
expect(parsed.changed[0].base_severity_counts).toEqual({ error: 2, nit: 1 });
|
||||
expect(parsed.changed[0].compare_severity_counts).toEqual({ error: 1, nit: 2 });
|
||||
});
|
||||
|
||||
test('volatile digits (durations, retry counts) do not split identities across runs', () => {
|
||||
startMarker('run-base', { agent: 'scripted' });
|
||||
friction('run-base', 'agent_invoke', 'agent exited with code 1 after 5123ms');
|
||||
startMarker('run-cmp', { agent: 'scripted' });
|
||||
friction('run-cmp', 'agent_invoke', 'agent exited with code 1 after 98764ms');
|
||||
|
||||
const { code, parsed } = diffJson('run-base', 'run-cmp');
|
||||
expect(code).toBe(0);
|
||||
expect(parsed.unique_to_base).toEqual([]);
|
||||
expect(parsed.unique_to_compare).toEqual([]);
|
||||
expect(parsed.changed).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,9 @@
|
||||
*
|
||||
* The drop-list is the security contract: CONDUCTOR_* / CLAUDE_* / GSTACK_* /
|
||||
* MCP_* / GBRAIN_* never reach a child except via the explicit overrides the
|
||||
* caller passes (which spread LAST and always win).
|
||||
* caller passes (which spread LAST and always win). HERMES_HOME is handled the
|
||||
* same way — not in any allowlist, so it only reaches a child via an explicit
|
||||
* override (the hermes door test always sets it to a temp home).
|
||||
*/
|
||||
|
||||
import { spawnSync } from 'node:child_process';
|
||||
@@ -162,6 +164,24 @@ export function resolveClaudeBinary(): string | null {
|
||||
]);
|
||||
}
|
||||
|
||||
/** Locate the real `hermes` binary (NousResearch hermes-agent). Bun.which
|
||||
* first, then the installer's known landing spots. */
|
||||
export function resolveHermesBinary(): string | null {
|
||||
const which = whichBin('hermes');
|
||||
if (which) return which;
|
||||
const home = process.env.HOME ?? os.homedir();
|
||||
const candidates = [
|
||||
'/opt/homebrew/bin/hermes',
|
||||
'/usr/local/bin/hermes',
|
||||
`${home}/.local/bin/hermes`, // where the official installer symlinks (observed v0.20.0)
|
||||
`${home}/.hermes/bin/hermes`,
|
||||
];
|
||||
for (const dir of (process.env.PATH ?? '').split(path.delimiter)) {
|
||||
if (dir) candidates.push(path.join(dir, 'hermes'));
|
||||
}
|
||||
return firstExecutable(candidates);
|
||||
}
|
||||
|
||||
/** Locate the real `codex` binary. Bun.which first, then known install dirs
|
||||
* (adds ~/.nvm + common node bin dirs where the npm global lands). */
|
||||
export function resolveCodexBinary(): string | null {
|
||||
@@ -215,6 +235,56 @@ export function hasCodexAuth(): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
/** Every provider key hermes recognizes — scrubbed from child env so the
|
||||
* seeded .env is the SINGLE auth source. Observed (v0.20.0): with model
|
||||
* pinned to anthropic/* but MULTIPLE provider keys visible, hermes's
|
||||
* provider-auto mis-routes the request and the turn returns
|
||||
* "HTTP 401: Missing Authentication header" as final text (exit 0). */
|
||||
const HERMES_ALL_PROVIDER_KEYS = [
|
||||
'ANTHROPIC_API_KEY', 'ANTHROPIC_BASE_URL', 'ANTHROPIC_AUTH_TOKEN',
|
||||
'OPENAI_API_KEY', 'OPENROUTER_API_KEY',
|
||||
] as const;
|
||||
|
||||
/** Parse KEY=VALUE lines from a dotenv-style file. Ignores comments, blanks,
|
||||
* and export prefixes; strips single/double quotes. Never throws. */
|
||||
export function parseDotenvFile(file: string): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
try {
|
||||
for (const rawLine of fs.readFileSync(file, 'utf-8').split('\n')) {
|
||||
const line = rawLine.trim();
|
||||
if (!line || line.startsWith('#')) continue;
|
||||
const m = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line);
|
||||
if (!m) continue;
|
||||
let v = m[2].trim();
|
||||
if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) {
|
||||
v = v.slice(1, -1);
|
||||
}
|
||||
out[m[1]] = v;
|
||||
}
|
||||
} catch {
|
||||
/* unreadable → empty */
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hermes is usable BY THE DOOR SUITE if an ANTHROPIC key with a NON-EMPTY
|
||||
* value is available — either exported (GSTACK_ promotion applies) or present
|
||||
* in the operator's real ~/.hermes/.env.
|
||||
*
|
||||
* Anthropic-only on purpose: the door pins model.default to anthropic/*, and
|
||||
* seeding any second provider key makes hermes's provider-auto mis-route the
|
||||
* pinned model ("HTTP 401: Missing Authentication header", observed). Bare
|
||||
* file existence is deliberately NOT auth: a blank CI secret writes an empty
|
||||
* .env, and that must produce a SKIP, not a paid failing test.
|
||||
*/
|
||||
export function hasHermesAuth(): boolean {
|
||||
const env = promotedEnv(process.env);
|
||||
if (env.ANTHROPIC_API_KEY?.trim()) return true;
|
||||
const parsed = parseDotenvFile(path.join(os.homedir(), '.hermes', '.env'));
|
||||
return Boolean(parsed.ANTHROPIC_API_KEY?.trim());
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// 4. Stream parsers (pure — exercised by the unit test with fixtures)
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
@@ -471,6 +541,135 @@ export async function codexExecTurn(opts: CodexTurnOpts): Promise<CodexTurnResul
|
||||
};
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// 5a-bis. Hermes home seeding + one-shot turn (mirror of the codex trio)
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface SeedHermesHomeOpts {
|
||||
/** Test-only injection point: read provider keys from this dotenv file
|
||||
* instead of the operator's real ~/.hermes/.env (lets the unit test assert
|
||||
* the allowlist-only copy against a fixture without touching real homes). */
|
||||
sourceEnvPath?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seed a hermetic <home>/.hermes for a spawned hermes. Copies EXACTLY ONE
|
||||
* key — a non-empty ANTHROPIC_API_KEY, from the operator's real ~/.hermes/.env
|
||||
* when present, falling back to the (promoted) process env. One key on
|
||||
* purpose: the door pins an anthropic/* model, and a second provider key
|
||||
* flips hermes's provider-auto into a mis-routed request (observed 401).
|
||||
* Never the whole .env file (other creds / endpoints / behavior knobs stay
|
||||
* behind), NEVER config.yaml (the operator's private MCP servers). The model
|
||||
* pin is a separate step (`pinHermesModel`) because hermes owns config.yaml's
|
||||
* schema — hand-writing it risks drift; `hermes config set` round-trips
|
||||
* safely.
|
||||
*/
|
||||
export function seedHermesHome(home: string, opts?: SeedHermesHomeOpts): string {
|
||||
const hermesHome = path.join(home, '.hermes');
|
||||
fs.mkdirSync(hermesHome, { recursive: true });
|
||||
|
||||
const fromFile = parseDotenvFile(opts?.sourceEnvPath ?? path.join(os.homedir(), '.hermes', '.env'));
|
||||
const env = promotedEnv(process.env);
|
||||
const key = fromFile.ANTHROPIC_API_KEY?.trim() || env.ANTHROPIC_API_KEY?.trim();
|
||||
if (key) {
|
||||
fs.writeFileSync(path.join(hermesHome, '.env'), `ANTHROPIC_API_KEY=${key}\n`, { mode: 0o600 });
|
||||
}
|
||||
return hermesHome;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hermetic env for spawning hermes itself: standard scrub + HOME/HERMES_HOME
|
||||
* overrides, then ALL provider keys deleted so the seeded .env is the single
|
||||
* auth source (provider-auto determinism — see HERMES_ALL_PROVIDER_KEYS).
|
||||
*/
|
||||
export function hermesChildEnv(home: string): NodeJS.ProcessEnv {
|
||||
const env = hermeticChildEnv({ HOME: home, HERMES_HOME: path.join(home, '.hermes') });
|
||||
for (const k of HERMES_ALL_PROVIDER_KEYS) delete env[k];
|
||||
return env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Non-interactive model/provider pin for a hermetic hermes home. A virgin
|
||||
* install refuses `-z` with "No inference provider configured" (exit 1,
|
||||
* observed), and `hermes model` is interactive-only — `config set` is the
|
||||
* scriptable path (observed working against v0.20.0).
|
||||
*/
|
||||
export function pinHermesModel(hermesBin: string, home: string, model = 'anthropic/claude-haiku-4.5'): { code: number | null; stderr: string } {
|
||||
const res = spawnSync(hermesBin, ['config', 'set', 'model.default', model], {
|
||||
env: hermesChildEnv(home),
|
||||
encoding: 'utf8',
|
||||
timeout: 60_000,
|
||||
});
|
||||
return { code: res.status, stderr: res.stderr ?? '' };
|
||||
}
|
||||
|
||||
export interface HermesTurnOpts {
|
||||
prompt: string;
|
||||
cwd: string;
|
||||
home: string;
|
||||
timeoutMs?: number;
|
||||
/** When set, the turn passes hermes's usage-report flag targeting this path. */
|
||||
usageFile?: string;
|
||||
}
|
||||
|
||||
export interface HermesTurnResult {
|
||||
/** hermes's one-shot mode prints ONLY the final response text on stdout. */
|
||||
finalText: string;
|
||||
exitCode: number | null;
|
||||
timedOut: boolean;
|
||||
stderrText: string;
|
||||
/** Parsed usage-report JSON when usageFile was requested and parseable. */
|
||||
usage?: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drive one `hermes -z` turn against a hermetic HOME + HERMES_HOME. The
|
||||
* RESOLVED binary path is used (never the bare literal), so resolution and
|
||||
* execution can't disagree. stdout is plain final text — NOT NDJSON; there is
|
||||
* no per-event tool-call stream to parse (door tests use a negative-control
|
||||
* prompt instead).
|
||||
*/
|
||||
export async function hermesOneShotTurn(opts: HermesTurnOpts): Promise<HermesTurnResult> {
|
||||
const bin = resolveHermesBinary();
|
||||
if (!bin) throw new Error('hermesOneShotTurn: hermes binary not found');
|
||||
const timeoutMs = opts.timeoutMs ?? 240_000;
|
||||
|
||||
const argv = [bin, '-z', opts.prompt, ...(opts.usageFile ? ['--usage-file', opts.usageFile] : [])];
|
||||
const proc = Bun.spawn(argv, {
|
||||
cwd: opts.cwd,
|
||||
env: hermesChildEnv(opts.home),
|
||||
stdout: 'pipe',
|
||||
stderr: 'pipe',
|
||||
stdin: 'ignore',
|
||||
});
|
||||
|
||||
let timedOut = false;
|
||||
const timer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
try { proc.kill(); } catch { /* already dead */ }
|
||||
}, timeoutMs);
|
||||
|
||||
const [stdout, stderrText] = await Promise.all([
|
||||
new Response(proc.stdout).text(),
|
||||
new Response(proc.stderr).text().catch(() => ''),
|
||||
]);
|
||||
const exitCode = await proc.exited;
|
||||
clearTimeout(timer);
|
||||
|
||||
let usage: unknown;
|
||||
if (opts.usageFile) {
|
||||
try { usage = JSON.parse(fs.readFileSync(opts.usageFile, 'utf-8')); } catch { /* best-effort */ }
|
||||
}
|
||||
|
||||
return {
|
||||
finalText: stdout.trim(),
|
||||
exitCode: timedOut ? 124 : exitCode,
|
||||
timedOut,
|
||||
stderrText,
|
||||
usage,
|
||||
};
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// 5b. Fast gbrain MCP server command (compiled binary, cached; bun-run fallback)
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -16,13 +16,21 @@
|
||||
* leaks into sibling tests.
|
||||
*/
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import {
|
||||
parseClaudeStream,
|
||||
parseCodexJsonl,
|
||||
hermeticChildEnv,
|
||||
hermesChildEnv,
|
||||
promotedEnv,
|
||||
resolveClaudeBinary,
|
||||
resolveCodexBinary,
|
||||
resolveHermesBinary,
|
||||
hasHermesAuth,
|
||||
parseDotenvFile,
|
||||
seedHermesHome,
|
||||
} from './agent-harness.ts';
|
||||
import { withEnv } from './with-env.ts';
|
||||
|
||||
@@ -173,4 +181,156 @@ describe('binary resolution SMOKE', () => {
|
||||
expect(bin === null || typeof bin === 'string').toBe(true);
|
||||
if (bin) console.log(`[smoke] codex resolved at: ${bin}`);
|
||||
});
|
||||
|
||||
test('resolveHermesBinary returns a string or null', () => {
|
||||
const bin = resolveHermesBinary();
|
||||
expect(bin === null || typeof bin === 'string').toBe(true);
|
||||
if (bin) console.log(`[smoke] hermes resolved at: ${bin}`);
|
||||
});
|
||||
|
||||
test('hasHermesAuth returns a boolean', () => {
|
||||
expect(typeof hasHermesAuth()).toBe('boolean');
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasHermesAuth truth table (env leg — pins the anthropic-only, non-empty-value gate)', () => {
|
||||
// The .env-file leg reads the operator's real ~/.hermes/.env, so only the
|
||||
// env-var leg is exercised hermetically here; the file PARSING contract is
|
||||
// pinned by the parseDotenvFile + seedHermesHome describes below.
|
||||
const CLEAR = {
|
||||
ANTHROPIC_API_KEY: undefined,
|
||||
GSTACK_ANTHROPIC_API_KEY: undefined,
|
||||
OPENAI_API_KEY: undefined,
|
||||
GSTACK_OPENAI_API_KEY: undefined,
|
||||
OPENROUTER_API_KEY: undefined,
|
||||
} as const;
|
||||
|
||||
/** True only when the operator's real ~/.hermes/.env carries an anthropic key. */
|
||||
const fileLegHasAnthropicKey = () =>
|
||||
Boolean(parseDotenvFile(join(process.env.HOME ?? '', '.hermes', '.env')).ANTHROPIC_API_KEY?.trim());
|
||||
|
||||
test('non-empty anthropic env key → true', async () => {
|
||||
await withEnv({ ...CLEAR, ANTHROPIC_API_KEY: 'sk-test-nonempty' }, () => {
|
||||
expect(hasHermesAuth()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test('GSTACK_-prefixed anthropic key promotes → true', async () => {
|
||||
await withEnv({ ...CLEAR, GSTACK_ANTHROPIC_API_KEY: 'sk-test-promoted' }, () => {
|
||||
expect(hasHermesAuth()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test('BLANK env value → does NOT count as auth (a blank CI secret must skip, not fail paid)', async () => {
|
||||
await withEnv({ ...CLEAR, ANTHROPIC_API_KEY: ' ' }, () => {
|
||||
expect(hasHermesAuth()).toBe(fileLegHasAnthropicKey());
|
||||
});
|
||||
});
|
||||
|
||||
test('a NON-anthropic provider key alone → false (door is anthropic-pinned; a second provider mis-routes provider-auto)', async () => {
|
||||
await withEnv({ ...CLEAR, OPENAI_API_KEY: 'sk-openai-only' }, () => {
|
||||
expect(hasHermesAuth()).toBe(fileLegHasAnthropicKey());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('hermesChildEnv — the single-auth-source enforcement point', () => {
|
||||
test('scrubs EVERY provider key (incl. allowlisted anthropic vars) and pins HOME/HERMES_HOME', async () => {
|
||||
// A regression here (dropping the delete loop) re-leaks the operator's
|
||||
// real key into every hermes child and reintroduces the provider-auto
|
||||
// 401 mis-route — detectable only in the triple-gated paid lane, so the
|
||||
// contract is pinned hermetically here.
|
||||
await withEnv({
|
||||
ANTHROPIC_API_KEY: 'sk-operator',
|
||||
ANTHROPIC_BASE_URL: 'https://operator.example',
|
||||
ANTHROPIC_AUTH_TOKEN: 'tok-operator',
|
||||
OPENAI_API_KEY: 'sk-openai',
|
||||
OPENROUTER_API_KEY: 'sk-openrouter',
|
||||
GSTACK_ANTHROPIC_API_KEY: undefined,
|
||||
GSTACK_OPENAI_API_KEY: undefined,
|
||||
HERMES_HOME: '/operator/.hermes',
|
||||
}, () => {
|
||||
const env = hermesChildEnv('/tmp/hermes-child-test');
|
||||
for (const k of ['ANTHROPIC_API_KEY', 'ANTHROPIC_BASE_URL', 'ANTHROPIC_AUTH_TOKEN', 'OPENAI_API_KEY', 'OPENROUTER_API_KEY']) {
|
||||
expect(env[k]).toBeUndefined();
|
||||
}
|
||||
expect(env.HOME).toBe('/tmp/hermes-child-test');
|
||||
expect(env.HERMES_HOME).toBe('/tmp/hermes-child-test/.hermes');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseDotenvFile', () => {
|
||||
test('parses KEY=VALUE, skips comments/blanks, strips quotes and export prefixes', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'gb-dotenv-'));
|
||||
try {
|
||||
const f = join(dir, '.env');
|
||||
writeFileSync(f, [
|
||||
'# comment',
|
||||
'',
|
||||
'ANTHROPIC_API_KEY=sk-plain',
|
||||
'export OPENAI_API_KEY="sk-quoted"',
|
||||
"OPENROUTER_API_KEY='sk-single'",
|
||||
'BLANK_KEY=',
|
||||
'not a kv line',
|
||||
].join('\n'), 'utf-8');
|
||||
const parsed = parseDotenvFile(f);
|
||||
expect(parsed.ANTHROPIC_API_KEY).toBe('sk-plain');
|
||||
expect(parsed.OPENAI_API_KEY).toBe('sk-quoted');
|
||||
expect(parsed.OPENROUTER_API_KEY).toBe('sk-single');
|
||||
expect(parsed.BLANK_KEY).toBe('');
|
||||
expect(Object.keys(parsed)).not.toContain('not a kv line');
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('unreadable file → empty object, never throws', () => {
|
||||
expect(parseDotenvFile('/nonexistent/path/.env')).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('seedHermesHome single-key copy (injectable source — never the operator home)', () => {
|
||||
test('copies EXACTLY the anthropic key; other providers and behavior knobs stay behind', async () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'gb-seedhh-'));
|
||||
try {
|
||||
const src = join(dir, 'source.env');
|
||||
writeFileSync(src, [
|
||||
'ANTHROPIC_API_KEY=sk-copy-me',
|
||||
'OPENAI_API_KEY=sk-openai-stays-home', // second provider → dropped (mis-routes provider-auto)
|
||||
'OPENROUTER_API_KEY=sk-or-stays-home', // second provider → dropped
|
||||
'TELEGRAM_BOT_TOKEN=secret-stays-home', // unlisted → dropped
|
||||
'HERMES_BASE_URL=https://internal', // unlisted → dropped
|
||||
].join('\n'), 'utf-8');
|
||||
|
||||
const home = join(dir, 'home');
|
||||
const hermesHome = await withEnv({
|
||||
ANTHROPIC_API_KEY: undefined, GSTACK_ANTHROPIC_API_KEY: undefined,
|
||||
OPENAI_API_KEY: undefined, GSTACK_OPENAI_API_KEY: undefined, OPENROUTER_API_KEY: undefined,
|
||||
}, () => seedHermesHome(home, { sourceEnvPath: src }));
|
||||
|
||||
expect(hermesHome).toBe(join(home, '.hermes'));
|
||||
const written = readFileSync(join(hermesHome, '.env'), 'utf-8');
|
||||
expect(written).toBe('ANTHROPIC_API_KEY=sk-copy-me\n');
|
||||
// seedHermesHome never writes config.yaml (hermes owns that schema —
|
||||
// the model pin goes through the hermes CLI instead).
|
||||
expect(existsSync(join(hermesHome, 'config.yaml'))).toBe(false);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('no source file + no env keys → no .env written (gate stays closed)', async () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'gb-seedhh2-'));
|
||||
try {
|
||||
const home = join(dir, 'home');
|
||||
await withEnv({
|
||||
ANTHROPIC_API_KEY: undefined, GSTACK_ANTHROPIC_API_KEY: undefined,
|
||||
OPENAI_API_KEY: undefined, GSTACK_OPENAI_API_KEY: undefined, OPENROUTER_API_KEY: undefined,
|
||||
}, () => seedHermesHome(home, { sourceEnvPath: join(dir, 'missing.env') }));
|
||||
expect(existsSync(join(home, '.hermes', '.env'))).toBe(false);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,585 @@
|
||||
/**
|
||||
* Real-PTY TTY harness — the interactive sibling of agent-harness.ts.
|
||||
*
|
||||
* agent-harness.ts drives the REAL `claude` / `codex` binaries HEADLESSLY
|
||||
* (`claude -p`, `codex exec`) — perfect for pass/fail door proofs, blind to
|
||||
* everything a human actually experiences: pickers, spinners, silence windows,
|
||||
* permission dialogs, interview prompts, rendered copy. This harness spawns any
|
||||
* CLI (gbrain itself, `claude`, `codex`) under a REAL pseudo-terminal via Bun's
|
||||
* built-in `terminal:` spawn option (Bun >= 1.3.10, pinned in package.json
|
||||
* engines + CI), so the child renders exactly what a user's terminal shows —
|
||||
* and records WHEN every byte arrived, so "the user stared at a frozen screen
|
||||
* for 9 seconds" is a measurable artifact, not a vibe.
|
||||
*
|
||||
* Built for two consumers:
|
||||
* 1. DX-exploration runs (`scripts/dx-explore.ts`) — capture the fresh-user
|
||||
* install funnel as timestamped transcripts for Don't-Make-Me-Think
|
||||
* audits (stall report + verbatim rendered copy per step).
|
||||
* 2. Future PTY e2e tests — the same waitFor/sendKey primitives the gstack
|
||||
* plan-mode PTY suite uses (pattern adapted from gstack's
|
||||
* test/helpers/claude-pty-runner.ts; no node-pty, no native modules).
|
||||
*
|
||||
* Hermeticity matches agent-harness.ts: every spawn goes through
|
||||
* hermeticChildEnv, so a DX run can NEVER see (or mutate) the operator's real
|
||||
* ~/.claude, ~/.codex, or ~/.gbrain unless the caller explicitly wires a
|
||||
* temp-dir override in.
|
||||
*
|
||||
* Pure helpers (stripAnsi, computeStalls, parseDriveCommand,
|
||||
* renderStallsReport, buildClaudeTuiSeed) are exported for the zero-subprocess
|
||||
* unit suite (test/tty-harness.test.ts).
|
||||
*/
|
||||
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
|
||||
import { hermeticChildEnv, type HermeticEnvOpts } from './agent-harness.ts';
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// 1. Pure text helpers
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/** Strip ANSI escapes (CSI, OSC, charset selection, cursor save/restore) so
|
||||
* pattern matching runs against the text a human would read. Same sequence
|
||||
* classes the gstack PTY runner strips — cursor-POSITIONING escapes render
|
||||
* visually as whitespace but leave no character behind, so matched copy can
|
||||
* arrive with collapsed spacing ("ready to execute" → "readytoexecute").
|
||||
* Match copy with that in mind. */
|
||||
export function stripAnsi(s: string): string {
|
||||
return s
|
||||
.replace(/\x1b\[[\d;?]*[a-zA-Z]/g, '')
|
||||
.replace(/\x1b\][^\x07\x1b]*(\x07|\x1b\\)/g, '')
|
||||
.replace(/\x1b[()][AB012]/g, '')
|
||||
.replace(/\x1b[78=>]/g, '');
|
||||
}
|
||||
|
||||
/** One captured PTY output burst. tMs is milliseconds since spawn. */
|
||||
export interface PtyFrame {
|
||||
tMs: number;
|
||||
data: string;
|
||||
}
|
||||
|
||||
/** A window of output silence long enough that a user would notice it. */
|
||||
export interface Stall {
|
||||
/** ms since spawn when the silence began. */
|
||||
startMs: number;
|
||||
durationMs: number;
|
||||
/** Last visible (ANSI-stripped) text on screen when the silence began —
|
||||
* what the user was staring at. '(no output yet)' for startup silence. */
|
||||
context: string;
|
||||
}
|
||||
|
||||
/** Tail of the stripped cumulative buffer, trimmed for a stall report. */
|
||||
function stallContext(cumulative: string): string {
|
||||
const visible = stripAnsi(cumulative);
|
||||
const lines = visible.split('\n').map((l) => l.trimEnd());
|
||||
while (lines.length > 0 && lines[lines.length - 1]!.trim() === '') lines.pop();
|
||||
return lines.slice(-4).join('\n').slice(-400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find every output gap >= thresholdMs (default 2000) in a frame sequence.
|
||||
* Counts three gap kinds a user actually experiences:
|
||||
* - startup silence: spawn → first byte
|
||||
* - mid-run silence: between consecutive frames
|
||||
* - trailing silence: last byte → endMs (pass the session duration to count
|
||||
* "it printed a question and then sat there" at the end of a run)
|
||||
*/
|
||||
export function computeStalls(
|
||||
frames: readonly PtyFrame[],
|
||||
opts: { thresholdMs?: number; endMs?: number } = {},
|
||||
): Stall[] {
|
||||
const threshold = opts.thresholdMs ?? 2000;
|
||||
const stalls: Stall[] = [];
|
||||
let cumulative = '';
|
||||
|
||||
if (frames.length === 0) {
|
||||
if (opts.endMs !== undefined && opts.endMs >= threshold) {
|
||||
stalls.push({ startMs: 0, durationMs: opts.endMs, context: '(no output yet)' });
|
||||
}
|
||||
return stalls;
|
||||
}
|
||||
|
||||
const first = frames[0]!;
|
||||
if (first.tMs >= threshold) {
|
||||
stalls.push({ startMs: 0, durationMs: first.tMs, context: '(no output yet)' });
|
||||
}
|
||||
cumulative += first.data;
|
||||
|
||||
for (let i = 1; i < frames.length; i++) {
|
||||
const prev = frames[i - 1]!;
|
||||
const cur = frames[i]!;
|
||||
const gap = cur.tMs - prev.tMs;
|
||||
if (gap >= threshold) {
|
||||
stalls.push({ startMs: prev.tMs, durationMs: gap, context: stallContext(cumulative) });
|
||||
}
|
||||
cumulative += cur.data;
|
||||
}
|
||||
|
||||
if (opts.endMs !== undefined) {
|
||||
const last = frames[frames.length - 1]!;
|
||||
const gap = opts.endMs - last.tMs;
|
||||
if (gap >= threshold) {
|
||||
stalls.push({ startMs: last.tMs, durationMs: gap, context: stallContext(cumulative) });
|
||||
}
|
||||
}
|
||||
|
||||
return stalls;
|
||||
}
|
||||
|
||||
/** Markdown stall report for a transcript dir — the audit-facing artifact. */
|
||||
export function renderStallsReport(stalls: readonly Stall[], totalMs: number): string {
|
||||
const header =
|
||||
`# Stall report\n\n` +
|
||||
`Total session: ${(totalMs / 1000).toFixed(1)}s. ` +
|
||||
`${stalls.length} silence window(s) a user would notice.\n`;
|
||||
if (stalls.length === 0) return header + '\nNo stalls at threshold.\n';
|
||||
const body = stalls
|
||||
.map(
|
||||
(s, i) =>
|
||||
`\n## Stall ${i + 1}: ${(s.durationMs / 1000).toFixed(1)}s at t+${(s.startMs / 1000).toFixed(1)}s\n\n` +
|
||||
'Screen when the silence began:\n\n```\n' +
|
||||
(s.context || '(blank screen)') +
|
||||
'\n```\n',
|
||||
)
|
||||
.join('');
|
||||
return header + body;
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// 2. Drive-mode control protocol (file-based, so a Conductor agent can steer
|
||||
// a live TUI across separate tool calls)
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
export const KEY_MAP = {
|
||||
Enter: '\r',
|
||||
Up: '\x1b[A',
|
||||
Down: '\x1b[B',
|
||||
Right: '\x1b[C',
|
||||
Left: '\x1b[D',
|
||||
Esc: '\x1b',
|
||||
Tab: '\t',
|
||||
ShiftTab: '\x1b[Z',
|
||||
Space: ' ',
|
||||
Backspace: '\x7f',
|
||||
CtrlC: '\x03',
|
||||
CtrlD: '\x04',
|
||||
} as const satisfies Record<string, string>;
|
||||
|
||||
/** Literal union of key names (not plain string) so sendKey('Entr') is a
|
||||
* compile error in test code; drive mode's runtime-string path keeps the
|
||||
* `| string` overload with its runtime throw. */
|
||||
export type KeyName = keyof typeof KEY_MAP;
|
||||
|
||||
export type DriveCommand =
|
||||
| { kind: 'send'; data: string }
|
||||
| { kind: 'key'; key: string }
|
||||
| { kind: 'note'; text: string }
|
||||
| { kind: 'stop' };
|
||||
|
||||
/**
|
||||
* Parse one line of the drive-mode control channel (`input.jsonl`). Accepted
|
||||
* shapes — exactly one of:
|
||||
* {"line": "text"} → sends text + Enter (the common case)
|
||||
* {"send": "raw text (include \r yourself for Enter)"}
|
||||
* {"key": "Enter" | "Up" | ... (KEY_MAP names)}
|
||||
* {"note": "free-text annotation recorded into the transcript timeline"}
|
||||
* {"stop": true}
|
||||
* Raw control bytes inside the line are re-escaped before parsing — zsh's
|
||||
* builtin `echo` expands `\r` to a literal CR, which would otherwise make
|
||||
* the JSON unparseable and silently eat the command. Returns null for
|
||||
* malformed JSON, unknown keys, or unknown key names — drive mode skips
|
||||
* those lines loudly (stderr) instead of guessing.
|
||||
*/
|
||||
export function parseDriveCommand(line: string): DriveCommand | null {
|
||||
// Raw C0 control chars are never valid inside JSON strings; shells (zsh
|
||||
// echo, printf format strings) produce them from typed `\r`/`\n`/`\t`.
|
||||
// Re-escaping is strictly more accepting than rejecting the line.
|
||||
const sanitized = line.replace(/[\x00-\x1f]/g, (c) => {
|
||||
return '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0');
|
||||
});
|
||||
let obj: unknown;
|
||||
try {
|
||||
obj = JSON.parse(sanitized);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (typeof obj !== 'object' || obj === null) return null;
|
||||
const rec = obj as Record<string, unknown>;
|
||||
if (typeof rec.line === 'string') {
|
||||
return { kind: 'send', data: rec.line.replace(/[\r\n]+$/, '') + '\r' };
|
||||
}
|
||||
if (typeof rec.send === 'string') return { kind: 'send', data: rec.send };
|
||||
if (typeof rec.key === 'string') {
|
||||
if (!(rec.key in KEY_MAP)) return null;
|
||||
return { kind: 'key', key: rec.key };
|
||||
}
|
||||
if (typeof rec.note === 'string') return { kind: 'note', text: rec.note };
|
||||
if (rec.stop === true) return { kind: 'stop' };
|
||||
return null;
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// 3. Claude Code TUI seed config
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Minimal `$CLAUDE_CONFIG_DIR/.claude.json` so an INTERACTIVE claude session
|
||||
* in a hermetic config dir skips first-run TUI prompts that would otherwise
|
||||
* hang an unattended DX run (shape adapted from gstack's hermetic-env.ts,
|
||||
* verified against claude 2.1.x):
|
||||
* - hasCompletedOnboarding: suppresses theme/onboarding flow
|
||||
* - customApiKeyResponses.approved (last 20 chars): suppresses the
|
||||
* "use this API key?" prompt when a key is exported
|
||||
* - projects[dir].hasTrustDialogAccepted: pre-trusts the workspace
|
||||
* Callers auditing FIRST-RUN friction itself should skip the seed on purpose.
|
||||
*/
|
||||
export function buildClaudeTuiSeed(opts: {
|
||||
apiKey?: string;
|
||||
trustedDirs: string[];
|
||||
}): Record<string, unknown> {
|
||||
const seed: Record<string, unknown> = {
|
||||
hasCompletedOnboarding: true,
|
||||
projects: Object.fromEntries(
|
||||
opts.trustedDirs.map((dir) => [
|
||||
dir,
|
||||
{ hasTrustDialogAccepted: true, hasCompletedProjectOnboarding: true },
|
||||
]),
|
||||
),
|
||||
};
|
||||
if (opts.apiKey) {
|
||||
seed.customApiKeyResponses = { approved: [opts.apiKey.slice(-20)] };
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
|
||||
/** Write the seed into a hermetic CLAUDE_CONFIG_DIR. */
|
||||
export function seedClaudeTuiConfig(
|
||||
configDir: string,
|
||||
opts: { apiKey?: string; trustedDirs: string[] },
|
||||
): void {
|
||||
fs.mkdirSync(configDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(configDir, '.claude.json'),
|
||||
JSON.stringify(buildClaudeTuiSeed(opts), null, 2),
|
||||
);
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// 4. The PTY session
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface TtyLaunchOpts {
|
||||
cwd?: string;
|
||||
/** Terminal size. 120x40 default — TUIs lay out cleanly at this size. */
|
||||
cols?: number;
|
||||
rows?: number;
|
||||
/** Env overrides layered LAST onto the hermetic base (HOME, GBRAIN_HOME,
|
||||
* CLAUDE_CONFIG_DIR, CODEX_HOME, API keys the scenario needs...). */
|
||||
env?: Record<string, string | undefined>;
|
||||
/** Extra allowlist entries for hermeticChildEnv (e.g. ['OPENAI_API_KEY',
|
||||
* 'CODEX_*'] for a codex child). */
|
||||
extraAllow?: HermeticEnvOpts['extraAllow'];
|
||||
/** Names to DELETE from the final env (applied after overrides). The
|
||||
* hermetic base deliberately passes auth keys through — a true-keyless DX
|
||||
* run must strip them, and overrides can't unset (undefined is skipped). */
|
||||
dropEnv?: string[];
|
||||
/** Wall-clock kill switch. Default 15 min. */
|
||||
timeoutMs?: number;
|
||||
/** Observer for every output burst (for callers that want to stream frames
|
||||
* somewhere as they arrive; saveTranscript already persists them at end). */
|
||||
onFrame?: (frame: PtyFrame) => void;
|
||||
}
|
||||
|
||||
export interface TtySession {
|
||||
argv: readonly string[];
|
||||
/** Date.now() at spawn — pair with frame tMs for absolute timestamps. */
|
||||
startedAtMs: number;
|
||||
send(data: string): void;
|
||||
sendKey(key: KeyName | string): void;
|
||||
/** Raw accumulated output (with ANSI). Forensics + replay. */
|
||||
raw(): string;
|
||||
/** ANSI-stripped output for pattern matching / human reading. */
|
||||
visible(): string;
|
||||
/** Timestamped output bursts captured so far. */
|
||||
frames(): readonly PtyFrame[];
|
||||
/** Mark current buffer position; visibleSince/waitFor can scope after it. */
|
||||
mark(): number;
|
||||
visibleSince(marker?: number): string;
|
||||
waitForAny(
|
||||
patterns: Array<RegExp | string>,
|
||||
opts?: { timeoutMs?: number; pollMs?: number; since?: number },
|
||||
): Promise<{ matched: RegExp | string; index: number }>;
|
||||
waitFor(
|
||||
pattern: RegExp | string,
|
||||
opts?: { timeoutMs?: number; pollMs?: number; since?: number },
|
||||
): Promise<void>;
|
||||
/** Resolve true once no output has arrived for quietMs (the screen has
|
||||
* settled — a picker/question is likely waiting). Resolves true immediately
|
||||
* if the process exited; false only on timeout. Never throws. */
|
||||
waitForQuiet(opts?: { quietMs?: number; timeoutMs?: number }): Promise<boolean>;
|
||||
/** Await process exit (bounded). Returns exit code or null if still alive. */
|
||||
waitForExit(timeoutMs?: number): Promise<number | null>;
|
||||
exited(): boolean;
|
||||
exitCode(): number | null;
|
||||
pid(): number | undefined;
|
||||
/** SIGINT, then SIGKILL after 2s. Safe to call repeatedly. */
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
/** Does this Bun expose the `terminal:` spawn option? Probed once. Callers
|
||||
* (tests) skip PTY paths on false instead of hard-failing. */
|
||||
let _ptySupport: boolean | null = null;
|
||||
export function ptySupported(): boolean {
|
||||
if (_ptySupport !== null) return _ptySupport;
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const proc = (Bun as any).spawn(['true'], {
|
||||
terminal: { cols: 20, rows: 5, data() {} },
|
||||
});
|
||||
_ptySupport = typeof proc?.terminal?.write === 'function' || proc?.terminal !== undefined;
|
||||
try {
|
||||
proc.kill?.('SIGKILL');
|
||||
} catch {
|
||||
/* already gone */
|
||||
}
|
||||
} catch {
|
||||
_ptySupport = false;
|
||||
}
|
||||
return _ptySupport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn argv under a real PTY with a hermetic env. The caller owns lifetime:
|
||||
* always `await session.close()` (or waitForExit) — the wall timer is a
|
||||
* backstop, not a lifecycle.
|
||||
*/
|
||||
export function launchTty(argv: string[], opts: TtyLaunchOpts = {}): TtySession {
|
||||
if (argv.length === 0) throw new Error('launchTty: empty argv');
|
||||
const cols = opts.cols ?? 120;
|
||||
const rows = opts.rows ?? 40;
|
||||
const timeoutMs = opts.timeoutMs ?? 900_000;
|
||||
const startedAtMs = Date.now();
|
||||
|
||||
let buffer = '';
|
||||
const frames: PtyFrame[] = [];
|
||||
let lastFrameAt = 0; // ms since spawn; 0 until first byte
|
||||
let exited = false;
|
||||
let exitCodeCaptured: number | null = null;
|
||||
|
||||
const childEnv = hermeticChildEnv(opts.env ?? {}, { extraAllow: opts.extraAllow });
|
||||
for (const k of opts.dropEnv ?? []) delete childEnv[k];
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const proc = (Bun as any).spawn(argv, {
|
||||
terminal: {
|
||||
cols,
|
||||
rows,
|
||||
data(_t: unknown, chunk: Buffer) {
|
||||
const frame: PtyFrame = { tMs: Date.now() - startedAtMs, data: chunk.toString('utf-8') };
|
||||
buffer += frame.data;
|
||||
frames.push(frame);
|
||||
lastFrameAt = frame.tMs;
|
||||
try {
|
||||
opts.onFrame?.(frame);
|
||||
} catch {
|
||||
/* observer errors never kill the session */
|
||||
}
|
||||
},
|
||||
},
|
||||
cwd: opts.cwd ?? process.cwd(),
|
||||
env: childEnv,
|
||||
});
|
||||
|
||||
let exitedPromise: Promise<void> = Promise.resolve();
|
||||
if (proc.exited && typeof proc.exited.then === 'function') {
|
||||
exitedPromise = proc.exited
|
||||
.then((code: number | null) => {
|
||||
exitCodeCaptured = code;
|
||||
exited = true;
|
||||
})
|
||||
.catch(() => {
|
||||
exited = true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Best-effort kill of the child's whole process TREE, not just the parent.
|
||||
* A PTY child (claude/codex/a shell) is normally the session/group leader of
|
||||
* its pty, so `process.kill(-pid, sig)` reaches its descendants (MCP servers,
|
||||
* sub-shells) — otherwise SIGKILL to the parent orphans them, leaking API
|
||||
* spend and PGLite locks. If the child isn't a group leader the negative-pid
|
||||
* kill throws (EPERM/ESRCH) and we fall back to killing the parent alone.
|
||||
*/
|
||||
function killTree(sig: NodeJS.Signals): void {
|
||||
const pid = proc.pid as number | undefined;
|
||||
if (typeof pid === 'number') {
|
||||
try {
|
||||
process.kill(-pid, sig);
|
||||
return;
|
||||
} catch {
|
||||
/* not a group leader — fall through to parent-only */
|
||||
}
|
||||
}
|
||||
try {
|
||||
proc.kill?.(sig);
|
||||
} catch {
|
||||
/* already dead */
|
||||
}
|
||||
}
|
||||
|
||||
const wallTimer = setTimeout(() => killTree('SIGKILL'), timeoutMs);
|
||||
|
||||
function send(data: string): void {
|
||||
if (exited) return;
|
||||
try {
|
||||
proc.terminal?.write?.(data);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
function sendKey(key: KeyName | string): void {
|
||||
const seq = (KEY_MAP as Record<string, string>)[key];
|
||||
if (seq === undefined) throw new Error(`sendKey: unknown key ${JSON.stringify(key)}`);
|
||||
send(seq);
|
||||
}
|
||||
|
||||
let lastMark = 0;
|
||||
function mark(): number {
|
||||
lastMark = buffer.length;
|
||||
return lastMark;
|
||||
}
|
||||
|
||||
function visibleSince(marker?: number): string {
|
||||
return stripAnsi(buffer.slice(marker ?? lastMark));
|
||||
}
|
||||
|
||||
async function waitForAny(
|
||||
patterns: Array<RegExp | string>,
|
||||
waitOpts?: { timeoutMs?: number; pollMs?: number; since?: number },
|
||||
): Promise<{ matched: RegExp | string; index: number }> {
|
||||
const wTimeout = waitOpts?.timeoutMs ?? 60_000;
|
||||
const poll = waitOpts?.pollMs ?? 200;
|
||||
const since = waitOpts?.since;
|
||||
const start = Date.now();
|
||||
for (;;) {
|
||||
const visible = since !== undefined ? stripAnsi(buffer.slice(since)) : stripAnsi(buffer);
|
||||
for (let i = 0; i < patterns.length; i++) {
|
||||
const p = patterns[i]!;
|
||||
const idx = typeof p === 'string' ? visible.indexOf(p) : visible.search(p);
|
||||
if (idx >= 0) return { matched: p, index: idx };
|
||||
}
|
||||
if (exited) {
|
||||
throw new Error(
|
||||
`process exited (code=${exitCodeCaptured}) before any pattern matched. ` +
|
||||
`Last visible:\n${stripAnsi(buffer).slice(-2000)}`,
|
||||
);
|
||||
}
|
||||
if (Date.now() - start >= wTimeout) {
|
||||
throw new Error(
|
||||
`Timed out after ${wTimeout}ms waiting for any of: ${patterns
|
||||
.map((p) => (typeof p === 'string' ? JSON.stringify(p) : p.source))
|
||||
.join(', ')}\nLast visible:\n${stripAnsi(buffer).slice(-2000)}`,
|
||||
);
|
||||
}
|
||||
await Bun.sleep(poll);
|
||||
}
|
||||
}
|
||||
|
||||
async function waitFor(
|
||||
pattern: RegExp | string,
|
||||
waitOpts?: { timeoutMs?: number; pollMs?: number; since?: number },
|
||||
): Promise<void> {
|
||||
await waitForAny([pattern], waitOpts);
|
||||
}
|
||||
|
||||
async function waitForQuiet(quietOpts?: {
|
||||
quietMs?: number;
|
||||
timeoutMs?: number;
|
||||
}): Promise<boolean> {
|
||||
const quietMs = quietOpts?.quietMs ?? 1500;
|
||||
const wTimeout = quietOpts?.timeoutMs ?? 120_000;
|
||||
const start = Date.now();
|
||||
for (;;) {
|
||||
if (exited) return true;
|
||||
const sinceLast = Date.now() - startedAtMs - lastFrameAt;
|
||||
if (frames.length > 0 && sinceLast >= quietMs) return true;
|
||||
if (Date.now() - start >= wTimeout) return false;
|
||||
await Bun.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForExit(exitTimeoutMs?: number): Promise<number | null> {
|
||||
await Promise.race([exitedPromise, Bun.sleep(exitTimeoutMs ?? timeoutMs)]);
|
||||
return exitCodeCaptured;
|
||||
}
|
||||
|
||||
async function close(): Promise<void> {
|
||||
clearTimeout(wallTimer);
|
||||
if (exited) return;
|
||||
killTree('SIGINT');
|
||||
await Promise.race([exitedPromise, Bun.sleep(2000)]);
|
||||
if (!exited) {
|
||||
killTree('SIGKILL');
|
||||
await Promise.race([exitedPromise, Bun.sleep(1000)]);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
argv,
|
||||
startedAtMs,
|
||||
send,
|
||||
sendKey,
|
||||
raw: () => buffer,
|
||||
visible: () => stripAnsi(buffer),
|
||||
frames: () => frames,
|
||||
mark,
|
||||
visibleSince,
|
||||
waitForAny,
|
||||
waitFor,
|
||||
waitForQuiet,
|
||||
waitForExit,
|
||||
exited: () => exited,
|
||||
exitCode: () => exitCodeCaptured,
|
||||
pid: () => proc.pid as number | undefined,
|
||||
close,
|
||||
};
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// 5. Transcript persistence — the audit-facing artifact bundle
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface TranscriptMeta {
|
||||
scenario: string;
|
||||
argv: readonly string[];
|
||||
startedAtIso: string;
|
||||
exitCode: number | null;
|
||||
durationMs: number;
|
||||
notes?: string[];
|
||||
[k: string]: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a transcript bundle into `dir`:
|
||||
* meta.json — scenario, argv, timing, exit code, notes
|
||||
* raw.txt — full output with ANSI (replayable)
|
||||
* visible.txt — ANSI-stripped (grep/read this one)
|
||||
* frames.jsonl — one {tMs, data} per output burst (timing analysis)
|
||||
* stalls.md — the rendered silence report (thresholdMs = 2000)
|
||||
*/
|
||||
export function saveTranscript(
|
||||
dir: string,
|
||||
data: { frames: readonly PtyFrame[]; raw: string; meta: TranscriptMeta },
|
||||
): void {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, 'meta.json'), JSON.stringify(data.meta, null, 2));
|
||||
fs.writeFileSync(path.join(dir, 'raw.txt'), data.raw);
|
||||
fs.writeFileSync(path.join(dir, 'visible.txt'), stripAnsi(data.raw));
|
||||
fs.writeFileSync(
|
||||
path.join(dir, 'frames.jsonl'),
|
||||
data.frames.map((f) => JSON.stringify(f)).join('\n') + (data.frames.length ? '\n' : ''),
|
||||
);
|
||||
const stalls = computeStalls(data.frames, { endMs: data.meta.durationMs });
|
||||
fs.writeFileSync(path.join(dir, 'stalls.md'), renderStallsReport(stalls, data.meta.durationMs));
|
||||
}
|
||||
@@ -49,14 +49,17 @@ describe('recommendModeFor — auto-suggestion heuristic', () => {
|
||||
expect(r.reason).toMatch(/Haiku/);
|
||||
});
|
||||
|
||||
test('No OpenAI key → conservative (no LLM expansion possible)', () => {
|
||||
const r = recommendModeFor({ hasOpenAIKey: false });
|
||||
test('No expansion-capable key → conservative (LLM expansion cannot run)', () => {
|
||||
const r = recommendModeFor({ hasExpansionKey: false });
|
||||
expect(r.mode).toBe('conservative');
|
||||
expect(r.reason).toMatch(/No OpenAI/);
|
||||
// Provider-neutral copy: expansion routes through the chat lane, so an
|
||||
// Anthropic or Google key counts — the reason must not say "No OpenAI".
|
||||
expect(r.reason).toMatch(/expansion-capable/i);
|
||||
expect(r.reason).not.toMatch(/No OpenAI key/);
|
||||
});
|
||||
|
||||
test('Sonnet / unknown → tokenmax (preserve-v0.31.x default)', () => {
|
||||
const r = recommendModeFor({ subagentModel: 'anthropic:claude-sonnet-4-6', hasOpenAIKey: true });
|
||||
const r = recommendModeFor({ subagentModel: 'anthropic:claude-sonnet-4-6', hasExpansionKey: true });
|
||||
expect(r.mode).toBe('tokenmax');
|
||||
expect(r.reason).toMatch(/v0\.31\.x|preserve/i);
|
||||
});
|
||||
@@ -75,7 +78,7 @@ describe('recommendModeFor — auto-suggestion heuristic', () => {
|
||||
const r = recommendModeFor({
|
||||
defaultModel: 'anthropic:claude-opus-4-7',
|
||||
subagentModel: 'anthropic:claude-haiku-4-5',
|
||||
hasOpenAIKey: true,
|
||||
hasExpansionKey: true,
|
||||
});
|
||||
expect(r.mode).toBe('conservative');
|
||||
});
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Unit tests for src/core/onboard/init-nudge.ts (runInitNudge).
|
||||
*
|
||||
* runInitNudge fires 6 parallel COUNT probes (stale chunks, entities, linked
|
||||
* entities, timeline entities, takes, total pages) and prints a one-line
|
||||
* nudge to stderr. These tests pin the DX-wave behavior:
|
||||
* - EMPTY brain (pages count 0) → the whole nudge is suppressed, even
|
||||
* when takes === 0 (no "0 takes" jargon-noise on first init)
|
||||
* - pages probe REJECTS → fail-open sentinel treats the brain as
|
||||
* non-empty, so the takes nudge still fires
|
||||
* - non-empty + healthy + one rejected arm → partial-checks notice
|
||||
* - non-empty + takes 0 → "0 takes" opportunity nudge
|
||||
*
|
||||
* The gate is process.stderr.isTTY (NOT process.env), so monkeypatching it
|
||||
* here does not trip the serial-isolation rules for env-mutating tests.
|
||||
* Both isTTY and stderr.write are restored in finally.
|
||||
*/
|
||||
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { runInitNudge } from '../src/core/onboard/init-nudge.ts';
|
||||
import type { BrainEngine } from '../src/core/engine.ts';
|
||||
|
||||
/** Per-probe result: a count, or an Error to make that arm reject. */
|
||||
interface ProbeCounts {
|
||||
stale?: number | Error;
|
||||
entities?: number | Error;
|
||||
linked?: number | Error;
|
||||
timeline?: number | Error;
|
||||
takes?: number | Error;
|
||||
pages?: number | Error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stub engine shaped like { executeRaw: async (sql) => [...] }. Routes each
|
||||
* of runInitNudge's 6 COUNT queries by a distinctive SQL fragment. Order
|
||||
* matters: the linked/timeline queries also contain "type IN ('person'",
|
||||
* so they are matched first.
|
||||
*/
|
||||
function stubEngine(counts: ProbeCounts): BrainEngine {
|
||||
const route = (sql: string): number | Error => {
|
||||
if (sql.includes('content_chunks')) return counts.stale ?? 0;
|
||||
if (sql.includes('FROM takes')) return counts.takes ?? 0;
|
||||
if (sql.includes('FROM links')) return counts.linked ?? 0;
|
||||
if (sql.includes('timeline_entries')) return counts.timeline ?? 0;
|
||||
if (sql.includes("type IN ('person'")) return counts.entities ?? 0;
|
||||
// 6th probe: SELECT COUNT(*) FROM pages WHERE deleted_at IS NULL
|
||||
return counts.pages ?? 0;
|
||||
};
|
||||
return {
|
||||
executeRaw: async (sql: string) => {
|
||||
const r = route(sql);
|
||||
if (r instanceof Error) throw r;
|
||||
return [{ count: r }];
|
||||
},
|
||||
} as unknown as BrainEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the nudge with stderr forced to look like a TTY and its writes
|
||||
* captured. Restores both in finally so no other test sees the patch.
|
||||
*/
|
||||
async function runNudgeCaptured(engine: BrainEngine): Promise<string> {
|
||||
const origIsTTY = process.stderr.isTTY;
|
||||
const origWrite = process.stderr.write;
|
||||
let out = '';
|
||||
try {
|
||||
(process.stderr as unknown as { isTTY: boolean }).isTTY = true;
|
||||
process.stderr.write = ((chunk: unknown) => {
|
||||
out += String(chunk);
|
||||
return true;
|
||||
}) as typeof process.stderr.write;
|
||||
await runInitNudge(engine);
|
||||
} finally {
|
||||
process.stderr.write = origWrite;
|
||||
(process.stderr as unknown as { isTTY: boolean | undefined }).isTTY = origIsTTY;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
describe('runInitNudge — empty-brain suppression', () => {
|
||||
test('empty brain (pages 0, takes 0) prints NOTHING', async () => {
|
||||
const out = await runNudgeCaptured(
|
||||
stubEngine({ stale: 0, entities: 0, linked: 0, timeline: 0, takes: 0, pages: 0 }),
|
||||
);
|
||||
expect(out).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('runInitNudge — pages probe failure is fail-open', () => {
|
||||
test('pages probe REJECTS with takes 0 → nudge still fires with "0 takes"', async () => {
|
||||
// The -1 sentinel means "count unknown" — treat as non-empty so the
|
||||
// pre-existing behavior (nudge on 0 takes) is preserved.
|
||||
const out = await runNudgeCaptured(
|
||||
stubEngine({
|
||||
stale: 0,
|
||||
entities: 0,
|
||||
linked: 0,
|
||||
timeline: 0,
|
||||
takes: 0,
|
||||
pages: new Error('pages probe failed'),
|
||||
}),
|
||||
);
|
||||
expect(out).toContain('Brain has opportunities');
|
||||
expect(out).toContain('0 takes');
|
||||
});
|
||||
});
|
||||
|
||||
describe('runInitNudge — partial-checks notice', () => {
|
||||
test('non-empty healthy brain with one rejected arm → "Init checks incomplete"', async () => {
|
||||
// pages > 0 (non-empty), takes > 0 (no opportunity part), entities 0
|
||||
// (coverage arms vacuous), stale 0 — but the linked probe rejects, so
|
||||
// partial=true with zero parts → the incomplete-checks line.
|
||||
const out = await runNudgeCaptured(
|
||||
stubEngine({
|
||||
stale: 0,
|
||||
entities: 0,
|
||||
linked: new Error('linked probe failed'),
|
||||
timeline: 0,
|
||||
takes: 5,
|
||||
pages: 10,
|
||||
}),
|
||||
);
|
||||
expect(out).toContain('Init checks incomplete');
|
||||
expect(out).toContain('(5/6)');
|
||||
expect(out).toContain('gbrain onboard --check');
|
||||
expect(out).not.toContain('Brain has opportunities');
|
||||
});
|
||||
});
|
||||
|
||||
describe('runInitNudge — non-empty brain opportunities', () => {
|
||||
test('non-empty brain with takes 0 → "Brain has opportunities: 0 takes"', async () => {
|
||||
const out = await runNudgeCaptured(
|
||||
stubEngine({ stale: 0, entities: 0, linked: 0, timeline: 0, takes: 0, pages: 10 }),
|
||||
);
|
||||
expect(out).toContain('Brain has opportunities: 0 takes');
|
||||
expect(out).toContain("Run 'gbrain onboard --check' to see the plan");
|
||||
// All 6 probes succeeded — no partial-checks suffix.
|
||||
expect(out).not.toContain('checks complete');
|
||||
});
|
||||
});
|
||||
@@ -44,22 +44,62 @@ describe('pickProvider — defensive paths', () => {
|
||||
// OPENAI_API_KEY set → openai is env-ready. readLineSafe returns the
|
||||
// default '1' in non-stdin-TTY bun:test mode, so picker picks the first
|
||||
// ready recipe deterministically. We mostly want to verify NO null
|
||||
// return and a sensible payload shape.
|
||||
// return and a sensible payload shape. probeLocal is stubbed unreachable
|
||||
// so ollama drops out and the unit test never touches the network.
|
||||
let stderr = '';
|
||||
const got = await pickProvider({
|
||||
touchpoint: 'embedding',
|
||||
env: { OPENAI_API_KEY: 'sk-test' },
|
||||
isTTY: true,
|
||||
writeStderr: (s) => { stderr += s; },
|
||||
probeLocal: async () => ({ reachable: false }),
|
||||
});
|
||||
expect(got).not.toBeNull();
|
||||
if (got) {
|
||||
expect(got.fullModel).toMatch(/:/); // provider:model shape
|
||||
expect(got.dim).toBeGreaterThan(0); // embedding always has dims
|
||||
expect(stderr).toContain('Pick a embedding provider');
|
||||
expect(stderr).toContain('Pick an embedding provider');
|
||||
// Keyless is always an explicit option for embedding.
|
||||
expect(stderr).toContain('0) none — continue keyless');
|
||||
}
|
||||
});
|
||||
|
||||
test('keyless machine (no keys, ollama daemon down) → keyless default, returns null', async () => {
|
||||
let stderr = '';
|
||||
const got = await pickProvider({
|
||||
touchpoint: 'embedding',
|
||||
env: {},
|
||||
isTTY: true,
|
||||
writeStderr: (s) => { stderr += s; },
|
||||
probeLocal: async () => ({ reachable: false }),
|
||||
});
|
||||
// No keyed provider ready → default is 0 (keyless) → null return; the
|
||||
// caller continues keyless. A bare Enter can no longer select a broken
|
||||
// local daemon. (readLineSafe resolves the default in bun:test's
|
||||
// non-stdin-TTY mode, so the null return IS the default-path proof.)
|
||||
expect(got).toBeNull();
|
||||
expect(stderr).toContain('0) none — continue keyless');
|
||||
});
|
||||
|
||||
test('ollama daemon up but model not pulled → annotated with the pull fix, keyless still default', async () => {
|
||||
let stderr = '';
|
||||
const got = await pickProvider({
|
||||
touchpoint: 'embedding',
|
||||
env: {},
|
||||
isTTY: true,
|
||||
writeStderr: (s) => { stderr += s; },
|
||||
probeLocal: async () => ({
|
||||
reachable: true,
|
||||
models_endpoint_valid: true,
|
||||
models: ['some-other-model'],
|
||||
}),
|
||||
});
|
||||
expect(stderr).toContain('model not pulled — run: ollama pull');
|
||||
// Daemon-up-model-missing must NOT be the bare-Enter default: with no
|
||||
// keyed provider ready, the default resolves to 0 (keyless) → null.
|
||||
expect(got).toBeNull();
|
||||
});
|
||||
|
||||
test('caveat fires when picking non-Anthropic chat without ANTHROPIC_API_KEY', async () => {
|
||||
let stderr = '';
|
||||
const got = await pickProvider({
|
||||
@@ -118,6 +158,7 @@ describe('pickProvider — defensive paths', () => {
|
||||
env: { OPENAI_API_KEY: 'sk-test' },
|
||||
isTTY: true,
|
||||
writeStderr: (s) => { stderr += s; },
|
||||
probeLocal: async () => ({ reachable: false }),
|
||||
});
|
||||
expect(stderr).toContain('embedding provider');
|
||||
});
|
||||
|
||||
@@ -40,6 +40,7 @@ import { CONFORMANCE_CASES } from '../src/core/verbs/conformance-fixtures.ts';
|
||||
import { writeSingleFact } from '../src/core/facts/write-single.ts';
|
||||
import {
|
||||
configureGateway,
|
||||
resetGateway,
|
||||
__setChatTransportForTests,
|
||||
__setEmbedTransportForTests,
|
||||
} from '../src/core/ai/gateway.ts';
|
||||
@@ -61,6 +62,15 @@ beforeAll(async () => {
|
||||
afterAll(async () => {
|
||||
await engine.disconnect();
|
||||
__setUsageLogPathForTests(null);
|
||||
// The deterministic-embedder tests configureGateway() with a FAKE OpenAI
|
||||
// key on the MODULE-GLOBAL gateway. Without a reset, every later file in
|
||||
// this shard process inherits "embeddings configured" and (with the test
|
||||
// transport also cleared) fires a REAL API call with the fake key — the
|
||||
// shard-8 turn-context 401 flake. Reset config AND both transports so the
|
||||
// file leaves the process exactly as it found it.
|
||||
resetGateway();
|
||||
__setChatTransportForTests(null);
|
||||
__setEmbedTransportForTests(null);
|
||||
try { rmSync(home, { recursive: true, force: true }); } catch { /* best-effort */ }
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Quiet fresh-install migration replay (src/core/migrate.ts runMigrations).
|
||||
*
|
||||
* Pins:
|
||||
* - Fresh brain (version 1, all migrations pending) replays quietly: ONE
|
||||
* "Setting up brain schema (vN)..." line, no per-migration "[N] name..."
|
||||
* lines, and the v123/v124 handler notices are suppressed.
|
||||
* - GBRAIN_MIGRATE_VERBOSE=1 escape hatch restores full verbose output on
|
||||
* a fresh brain.
|
||||
* - Upgrades (current > 1) keep the full verbose "Schema version X → Y"
|
||||
* narration — including an in-process upgrade AFTER a quiet fresh replay,
|
||||
* which proves the module-level quietMigrationNotices flag is reset in
|
||||
* the finally and does not leak.
|
||||
* - A no-pending run applies nothing and stays silent.
|
||||
*
|
||||
* Serial: mutates process.env (GBRAIN_MIGRATE_VERBOSE, GBRAIN_PGLITE_SNAPSHOT)
|
||||
* and monkey-patches process.stderr.write; everything is saved in beforeAll /
|
||||
* restored in afterAll or per-test try/finally (repo rule R1).
|
||||
*/
|
||||
|
||||
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
|
||||
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
|
||||
import { runMigrations, LATEST_VERSION } from '../src/core/migrate.ts';
|
||||
|
||||
let engineA: PGLiteEngine; // fresh quiet replay + later upgrade re-run
|
||||
let engineB: PGLiteEngine | null = null; // fresh verbose replay (test 2)
|
||||
|
||||
let prevVerbose: string | undefined;
|
||||
let prevSnapshot: string | undefined;
|
||||
|
||||
/** Capture everything written to process.stderr.write while fn runs. */
|
||||
async function captureStderr(fn: () => Promise<void>): Promise<string> {
|
||||
const orig = process.stderr.write.bind(process.stderr);
|
||||
let out = '';
|
||||
process.stderr.write = ((chunk: string | Uint8Array): boolean => {
|
||||
out += typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8');
|
||||
return true;
|
||||
}) as typeof process.stderr.write;
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
process.stderr.write = orig;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
prevVerbose = process.env.GBRAIN_MIGRATE_VERBOSE;
|
||||
prevSnapshot = process.env.GBRAIN_PGLITE_SNAPSHOT;
|
||||
// Default state for the file: quiet mode active, and NO snapshot fast-path —
|
||||
// a snapshot-restored engine skips runMigrations entirely, which would make
|
||||
// every assertion below vacuous.
|
||||
delete process.env.GBRAIN_MIGRATE_VERBOSE;
|
||||
delete process.env.GBRAIN_PGLITE_SNAPSHOT;
|
||||
|
||||
engineA = new PGLiteEngine();
|
||||
await engineA.connect({});
|
||||
// NOTE: initSchema() deliberately NOT called here — test 1 captures its
|
||||
// stderr output as the fresh-install replay under test.
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (prevVerbose === undefined) delete process.env.GBRAIN_MIGRATE_VERBOSE;
|
||||
else process.env.GBRAIN_MIGRATE_VERBOSE = prevVerbose;
|
||||
if (prevSnapshot === undefined) delete process.env.GBRAIN_PGLITE_SNAPSHOT;
|
||||
else process.env.GBRAIN_PGLITE_SNAPSHOT = prevSnapshot;
|
||||
|
||||
await engineA.disconnect();
|
||||
if (engineB) await engineB.disconnect();
|
||||
});
|
||||
|
||||
describe('quiet fresh-install replay', () => {
|
||||
test('fresh brain prints one summary line, no per-migration or notice lines', async () => {
|
||||
const out = await captureStderr(async () => {
|
||||
await engineA.initSchema();
|
||||
});
|
||||
|
||||
expect(out).toContain('Setting up brain schema (v');
|
||||
// No per-migration " [N] name..." progress lines.
|
||||
expect(out).not.toMatch(/\[\d+\] \S+\.\.\./);
|
||||
// No " [N] ✓ name" completion lines.
|
||||
expect(out).not.toContain('✓');
|
||||
// The verbose header is replaced, not printed alongside.
|
||||
expect(out).not.toContain('Schema version 1 →');
|
||||
// v123/v124 handler notices are suppressed via quietMigrationNotices.
|
||||
expect(out).not.toContain('v123:');
|
||||
expect(out).not.toContain('v124:');
|
||||
|
||||
// The replay actually ran to completion.
|
||||
expect(await engineA.getConfig('version')).toBe(String(LATEST_VERSION));
|
||||
});
|
||||
|
||||
test('GBRAIN_MIGRATE_VERBOSE=1 keeps full output on a fresh brain', async () => {
|
||||
process.env.GBRAIN_MIGRATE_VERBOSE = '1';
|
||||
try {
|
||||
engineB = new PGLiteEngine();
|
||||
await engineB.connect({});
|
||||
const out = await captureStderr(async () => {
|
||||
await engineB!.initSchema();
|
||||
});
|
||||
|
||||
expect(out).toContain('Schema version 1 →');
|
||||
expect(out).toMatch(/\[\d+\] ✓ /);
|
||||
expect(out).not.toContain('Setting up brain schema');
|
||||
} finally {
|
||||
delete process.env.GBRAIN_MIGRATE_VERBOSE;
|
||||
}
|
||||
});
|
||||
|
||||
test('upgrade path (current > 1) stays verbose — and the quiet flag did not leak from test 1', async () => {
|
||||
// Rewind engineA one version so exactly the last migration is pending.
|
||||
// This runs in the SAME process AFTER test 1's quiet replay, so verbose
|
||||
// output here also proves runMigrations' finally reset quietMigrationNotices.
|
||||
await engineA.setConfig('version', String(LATEST_VERSION - 1));
|
||||
|
||||
let result: { applied: number; current: number } | undefined;
|
||||
const out = await captureStderr(async () => {
|
||||
result = await runMigrations(engineA);
|
||||
});
|
||||
|
||||
expect(result?.applied).toBe(1);
|
||||
expect(result?.current).toBe(LATEST_VERSION);
|
||||
expect(out).toContain(`Schema version ${LATEST_VERSION - 1} → `);
|
||||
expect(out).toContain('✓');
|
||||
expect(out).not.toContain('Setting up brain schema');
|
||||
});
|
||||
|
||||
test('no-pending run applies nothing and emits no setup/migration lines', async () => {
|
||||
let result: { applied: number; current: number } | undefined;
|
||||
const out = await captureStderr(async () => {
|
||||
result = await runMigrations(engineA);
|
||||
});
|
||||
|
||||
expect(result?.applied).toBe(0);
|
||||
expect(result?.current).toBe(LATEST_VERSION);
|
||||
expect(out).not.toContain('Setting up brain schema');
|
||||
expect(out).not.toContain('Schema version');
|
||||
expect(out).not.toContain('✓');
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { currentRecommendedSet } from '../src/core/advisor/recommended-set.ts';
|
||||
|
||||
describe('plugin membership curation (skills = plugin ∪ exclusions, disjoint)', () => {
|
||||
const root = join(import.meta.dir, '..');
|
||||
@@ -56,6 +57,18 @@ describe('plugin membership curation (skills = plugin ∪ exclusions, disjoint)'
|
||||
it('plugin skills has no duplicates', () => {
|
||||
expect(new Set(plugin.skills).size).toBe(plugin.skills.length);
|
||||
});
|
||||
|
||||
it('every RECOMMENDED skill is bundled (recommended-but-unscaffoldable is a broken funnel)', () => {
|
||||
// The post-install advisory + verify hand-off tell users to install these
|
||||
// by slug; `gbrain skillpack scaffold <slug>` resolves against the plugin
|
||||
// bundle. A recommendation the scaffold can't fulfill is a dead-end CTA —
|
||||
// the exact drift that kept cold-start (the day-one "now what?" skill)
|
||||
// unreachable for paste-in bootstrap users until v0.45.11.0.
|
||||
const unscaffoldable = currentRecommendedSet()
|
||||
.map((s) => s.slug)
|
||||
.filter((slug) => !bundled.includes(slug));
|
||||
expect(unscaffoldable).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bundled-skill reference closure (nothing bundled points at a non-bundled skill)', () => {
|
||||
|
||||
@@ -25,7 +25,9 @@ import { tmpdir } from 'os';
|
||||
import {
|
||||
buildAdvisory,
|
||||
detectInstalledSlugs,
|
||||
printAdvisoryIfRecommended,
|
||||
} from '../src/core/skillpack/post-install-advisory.ts';
|
||||
import { currentRecommendedSet } from '../src/core/advisor/recommended-set.ts';
|
||||
|
||||
const cleanup: string[] = [];
|
||||
|
||||
@@ -90,10 +92,20 @@ describe('detectInstalledSlugs', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('recommended set — OOBE invariants', () => {
|
||||
it('cold-start is the FIRST recommendation (the day-one "now what?" answer)', () => {
|
||||
// The compact init advisory previews the first slugs and `gbrain advisor`
|
||||
// ranks by list order — cold-start leads because every other recommended
|
||||
// skill only becomes magical once the brain holds the user's real life.
|
||||
expect(currentRecommendedSet()[0]!.slug).toBe('cold-start');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildAdvisory — partial-install path', () => {
|
||||
it('lists ONLY missing skills when most are already installed', () => {
|
||||
const { workspace, skillsDir } = scratchWorkspace([
|
||||
'brain-ops',
|
||||
'cold-start',
|
||||
'article-enrichment',
|
||||
'strategic-reading',
|
||||
'concept-synthesis',
|
||||
@@ -135,6 +147,7 @@ describe('buildAdvisory — partial-install path', () => {
|
||||
describe('buildAdvisory — all-installed → null (no nag)', () => {
|
||||
it('returns null when every recommended skill is already installed', () => {
|
||||
const allRecommended = [
|
||||
'cold-start',
|
||||
'book-mirror',
|
||||
'article-enrichment',
|
||||
'strategic-reading',
|
||||
@@ -218,6 +231,72 @@ describe('buildAdvisory — agent-readable framing', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('printAdvisoryIfRecommended — compact init pointer vs full upgrade banner', () => {
|
||||
function captureStderr(fn: () => void): string {
|
||||
const orig = process.stderr.write;
|
||||
let out = '';
|
||||
process.stderr.write = ((chunk: unknown) => {
|
||||
out += String(chunk);
|
||||
return true;
|
||||
}) as typeof process.stderr.write;
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
process.stderr.write = orig;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
it('context init with all skills missing prints the compact human-voiced pointer', () => {
|
||||
const { workspace, skillsDir } = scratchWorkspace([]);
|
||||
const out = captureStderr(() =>
|
||||
printAdvisoryIfRecommended({
|
||||
version: '0.25.1',
|
||||
context: 'init',
|
||||
targetWorkspace: workspace,
|
||||
targetSkillsDir: skillsDir,
|
||||
}),
|
||||
);
|
||||
const names = currentRecommendedSet().map((s) => s.slug);
|
||||
expect(out).toContain('recommended skill(s) not installed yet');
|
||||
// Preview truncates at 4 slugs + ellipsis; the 5th slug never appears
|
||||
// (the scaffold command is --all when everything is missing).
|
||||
expect(out).toContain(`(${names.slice(0, 4).join(', ')}, …)`);
|
||||
expect(out).not.toContain(names[4]);
|
||||
expect(out).toContain('gbrain advisor');
|
||||
// The compact init pointer is human-voiced — no agent stage directions.
|
||||
expect(out).not.toContain('ACTION FOR THE AGENT');
|
||||
expect(out).not.toContain('[AGENT]');
|
||||
});
|
||||
|
||||
it('context init with everything installed prints NOTHING', () => {
|
||||
const allSlugs = currentRecommendedSet().map((s) => s.slug);
|
||||
const { workspace, skillsDir } = scratchWorkspace(allSlugs);
|
||||
const out = captureStderr(() =>
|
||||
printAdvisoryIfRecommended({
|
||||
version: '0.25.1',
|
||||
context: 'init',
|
||||
targetWorkspace: workspace,
|
||||
targetSkillsDir: skillsDir,
|
||||
}),
|
||||
);
|
||||
expect(out).toBe('');
|
||||
});
|
||||
|
||||
it('context upgrade with missing skills keeps the full agent-addressed banner', () => {
|
||||
const { workspace, skillsDir } = scratchWorkspace([]);
|
||||
const out = captureStderr(() =>
|
||||
printAdvisoryIfRecommended({
|
||||
version: '0.25.1',
|
||||
context: 'upgrade',
|
||||
targetWorkspace: workspace,
|
||||
targetSkillsDir: skillsDir,
|
||||
}),
|
||||
);
|
||||
expect(out).toContain('ACTION FOR THE AGENT');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildAdvisory — no workspace detected', () => {
|
||||
it('still renders an advisory with a workspace-detection note', () => {
|
||||
const advisory = buildAdvisory({
|
||||
|
||||
@@ -99,6 +99,76 @@ describe('loadScenario', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadScenario — oracle validation', () => {
|
||||
const base = (oracle: string) =>
|
||||
`{"kind":"fresh-install","expected_phases":[],"oracle":${oracle}}`;
|
||||
|
||||
test('happy path: query defaults minResults to 1; min_results overrides; files_exist parsed', () => {
|
||||
scaffoldScenario('o-happy', base('{"query":"alice"}'));
|
||||
expect(loadScenario('o-happy').oracle).toEqual({ query: 'alice', minResults: 1 });
|
||||
|
||||
scaffoldScenario('o-min', base('{"query":"alice","min_results":0,"files_exist":["skills/query/SKILL.md"]}'));
|
||||
const cfg = loadScenario('o-min');
|
||||
expect(cfg.oracle?.minResults).toBe(0);
|
||||
expect(cfg.oracle?.filesExist).toEqual(['skills/query/SKILL.md']);
|
||||
});
|
||||
|
||||
test('oracle must be a JSON object', () => {
|
||||
scaffoldScenario('o-arr', base('["x"]'));
|
||||
expect(() => loadScenario('o-arr')).toThrow(/oracle must be a JSON object/);
|
||||
scaffoldScenario('o-str', base('"x"'));
|
||||
expect(() => loadScenario('o-str')).toThrow(/oracle must be a JSON object/);
|
||||
});
|
||||
|
||||
test('oracle.query must be a non-empty string and must not start with a dash', () => {
|
||||
scaffoldScenario('o-empty', base('{"query":" "}'));
|
||||
expect(() => loadScenario('o-empty')).toThrow(/oracle\.query must be a non-empty string/);
|
||||
scaffoldScenario('o-num', base('{"query":7}'));
|
||||
expect(() => loadScenario('o-num')).toThrow(/oracle\.query must be a non-empty string/);
|
||||
// The query lands in gbrain argv — a dash-leading value would parse as a flag.
|
||||
scaffoldScenario('o-dash', base('{"query":"--json"}'));
|
||||
expect(() => loadScenario('o-dash')).toThrow(/must not start with a dash/);
|
||||
});
|
||||
|
||||
test('oracle.min_results requires query and must be a finite number >= 0', () => {
|
||||
scaffoldScenario('o-orphan', base('{"min_results":1}'));
|
||||
expect(() => loadScenario('o-orphan')).toThrow(/min_results requires oracle\.query/);
|
||||
scaffoldScenario('o-neg', base('{"query":"alice","min_results":-1}'));
|
||||
expect(() => loadScenario('o-neg')).toThrow(/min_results must be a number >= 0/);
|
||||
scaffoldScenario('o-nan', base('{"query":"alice","min_results":"5"}'));
|
||||
expect(() => loadScenario('o-nan')).toThrow(/min_results must be a number >= 0/);
|
||||
});
|
||||
|
||||
test('oracle.files_exist must be workspace-relative strings', () => {
|
||||
scaffoldScenario('o-files-str', base('{"files_exist":"skills"}'));
|
||||
expect(() => loadScenario('o-files-str')).toThrow(/files_exist must be a string\[\]/);
|
||||
scaffoldScenario('o-files-abs', base('{"files_exist":["/etc/passwd"]}'));
|
||||
expect(() => loadScenario('o-files-abs')).toThrow(/workspace-relative/);
|
||||
scaffoldScenario('o-files-dots', base('{"files_exist":["../outside.md"]}'));
|
||||
expect(() => loadScenario('o-files-dots')).toThrow(/workspace-relative/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadScenario — containment (adversarial-gate pins)', () => {
|
||||
// Scenario packs load from arbitrary dirs via the scenarios-dir env
|
||||
// override, and their content flows to an external agent — traversal in the
|
||||
// name or the declared paths would expose arbitrary operator files.
|
||||
test('traversal-shaped scenario names are rejected', () => {
|
||||
expect(() => loadScenario('../outside')).toThrow(/not found/);
|
||||
expect(() => loadScenario('a/b')).toThrow(/not found/);
|
||||
expect(() => loadScenario('..')).toThrow(/not found/);
|
||||
});
|
||||
|
||||
test('brief, brain, and seed paths escaping the scenario dir are rejected', () => {
|
||||
scaffoldScenario('esc-brief', '{"kind":"fresh-install","expected_phases":[],"brief":"../outside.md"}');
|
||||
expect(() => loadScenario('esc-brief')).toThrow(/inside the scenario dir/);
|
||||
scaffoldScenario('esc-brain', '{"kind":"fresh-install","expected_phases":[],"brain":"../../brains"}');
|
||||
expect(() => loadScenario('esc-brain')).toThrow(/inside the scenario dir/);
|
||||
scaffoldScenario('esc-seed', '{"kind":"upgrade","expected_phases":[],"seed":"/etc"}');
|
||||
expect(() => loadScenario('esc-seed')).toThrow(/inside the scenario dir/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('readBrief', () => {
|
||||
test('returns BRIEF.md content', () => {
|
||||
scaffoldScenario('reads-brief', '{"kind":"fresh-install","expected_phases":[]}', '# Hello world');
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* Unit tests for `pendingUpgradeVersion` — the ONE shared "is an upgrade
|
||||
* actually pending for THIS binary?" predicate (src/core/self-upgrade.ts).
|
||||
*
|
||||
* Every upgrade-nag surface (CLI startup marker, doctor, advisor,
|
||||
* get_brain_identity) routes through it, so this file pins the suppression
|
||||
* rule centrally: a stale or foreign cache (latest <= running version) must
|
||||
* return null. The cache records the version of whatever binary WROTE it —
|
||||
* an older gbrain on PATH can write `UPGRADE_AVAILABLE 0.0.1 X` — so the
|
||||
* comparison must be against the RUNNING version, never `marker.current`.
|
||||
*
|
||||
* Also covers the advisor consumer (collect-version.ts): version_drift fires
|
||||
* only when the shared predicate says an upgrade is pending.
|
||||
*/
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import { mkdtempSync, rmSync, utimesSync, writeFileSync, mkdirSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { withEnv } from './helpers/with-env.ts';
|
||||
import {
|
||||
CACHE_TTL_UPGRADE_AVAILABLE_MS,
|
||||
pendingUpgradeVersion,
|
||||
updateCachePath,
|
||||
writeUpdateCache,
|
||||
} from '../src/core/self-upgrade.ts';
|
||||
import { collectVersion } from '../src/core/advisor/collect-version.ts';
|
||||
import type { AdvisorContext } from '../src/core/advisor/types.ts';
|
||||
|
||||
/** Run `fn` with GBRAIN_HOME pointed at a fresh temp dir (env restored after). */
|
||||
async function withHome<T>(fn: (home: string) => T | Promise<T>): Promise<T> {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'gbrain-pending-'));
|
||||
try {
|
||||
return await withEnv({ GBRAIN_HOME: dir }, () => fn(dir));
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
/** Backdate the cache file's mtime so `isCacheFresh` sees it as expired. */
|
||||
function backdateCache(byMs: number): void {
|
||||
const then = new Date(Date.now() - byMs);
|
||||
utimesSync(updateCachePath(), then, then);
|
||||
}
|
||||
|
||||
describe('pendingUpgradeVersion', () => {
|
||||
test('fresh cache, latest > running → returns latest', async () => {
|
||||
await withHome(() => {
|
||||
writeUpdateCache({ kind: 'upgrade_available', current: '0.42.0', latest: '0.99.0' });
|
||||
expect(pendingUpgradeVersion('0.42.0')).toBe('0.99.0');
|
||||
});
|
||||
});
|
||||
|
||||
test('fresh cache, latest == running → null (already current; suppress the nag)', async () => {
|
||||
await withHome(() => {
|
||||
writeUpdateCache({ kind: 'upgrade_available', current: '0.42.0', latest: '0.42.0' });
|
||||
expect(pendingUpgradeVersion('0.42.0')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
test('fresh cache, latest < running → null (downgrade/yanked never nags)', async () => {
|
||||
await withHome(() => {
|
||||
writeUpdateCache({ kind: 'upgrade_available', current: '0.0.1', latest: '0.42.0' });
|
||||
expect(pendingUpgradeVersion('0.99.0')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
test('foreign-writer cache: comparison is against the RUNNING version, not marker.current', async () => {
|
||||
await withHome(() => {
|
||||
// An old 0.0.1 binary on PATH wrote the cache. The running binary must
|
||||
// compare ITS version to latest — marker.current is untrusted.
|
||||
writeUpdateCache({ kind: 'upgrade_available', current: '0.0.1', latest: '0.99.0' });
|
||||
expect(pendingUpgradeVersion('0.99.0')).toBeNull(); // already on latest → suppressed
|
||||
expect(pendingUpgradeVersion('0.42.0')).toBe('0.99.0'); // genuinely behind → nag
|
||||
});
|
||||
});
|
||||
|
||||
test('stale cache (mtime beyond upgrade_available TTL) → null', async () => {
|
||||
await withHome(() => {
|
||||
writeUpdateCache({ kind: 'upgrade_available', current: '0.42.0', latest: '0.99.0' });
|
||||
backdateCache(CACHE_TTL_UPGRADE_AVAILABLE_MS + 60_000);
|
||||
expect(pendingUpgradeVersion('0.42.0')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
test('missing cache → null', async () => {
|
||||
await withHome(() => {
|
||||
expect(pendingUpgradeVersion('0.42.0')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
test('up_to_date marker kind → null', async () => {
|
||||
await withHome(() => {
|
||||
writeUpdateCache({ kind: 'up_to_date', current: '0.42.0' });
|
||||
expect(pendingUpgradeVersion('0.42.0')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
test('corrupt cache content → null (never throws)', async () => {
|
||||
await withHome((home) => {
|
||||
mkdirSync(join(home, '.gbrain'), { recursive: true });
|
||||
writeFileSync(updateCachePath(), 'UPGRADE_AVAILABLE not-a-version; rm -rf /\n');
|
||||
expect(pendingUpgradeVersion('0.42.0')).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('advisor collectVersion (consumer of the shared predicate)', () => {
|
||||
const ctx = (version: string) => ({ version } as unknown as AdvisorContext);
|
||||
|
||||
test('fresh cache with newer latest → one version_drift finding', async () => {
|
||||
await withHome(async () => {
|
||||
writeUpdateCache({ kind: 'upgrade_available', current: '0.42.0', latest: '0.99.0' });
|
||||
const findings = await collectVersion.collect(ctx('0.42.0'));
|
||||
expect(findings).toHaveLength(1);
|
||||
expect(findings[0].id).toBe('version_drift');
|
||||
expect(findings[0].title).toContain('0.99.0');
|
||||
expect(findings[0].title).toContain('0.42.0');
|
||||
});
|
||||
});
|
||||
|
||||
test('latest == ctx.version → no findings (stale/foreign cache suppressed)', async () => {
|
||||
await withHome(async () => {
|
||||
writeUpdateCache({ kind: 'upgrade_available', current: '0.42.0', latest: '0.99.0' });
|
||||
const findings = await collectVersion.collect(ctx('0.99.0'));
|
||||
expect(findings).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,241 @@
|
||||
/**
|
||||
* Unit suite for test/helpers/tty-harness.ts — the PTY DX harness.
|
||||
*
|
||||
* Pure helpers (stripAnsi, computeStalls, renderStallsReport,
|
||||
* parseDriveCommand, buildClaudeTuiSeed, saveTranscript) are exercised with
|
||||
* ZERO subprocesses. Two live smokes spawn `sh` under a real PTY (cheap,
|
||||
* no network, no API) and skip cleanly on a Bun without `terminal:` support
|
||||
* — the same fail-SKIP posture as the agent-harness door tests.
|
||||
*/
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { mkdtempSync, readFileSync, rmSync, existsSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
import {
|
||||
stripAnsi,
|
||||
computeStalls,
|
||||
renderStallsReport,
|
||||
parseDriveCommand,
|
||||
buildClaudeTuiSeed,
|
||||
saveTranscript,
|
||||
launchTty,
|
||||
ptySupported,
|
||||
KEY_MAP,
|
||||
type PtyFrame,
|
||||
} from './helpers/tty-harness.ts';
|
||||
|
||||
describe('stripAnsi', () => {
|
||||
test('removes CSI color + cursor sequences', () => {
|
||||
expect(stripAnsi('\x1b[1;32mgreen\x1b[0m plain \x1b[2K\x1b[1Gline')).toBe('green plain line');
|
||||
});
|
||||
|
||||
test('removes OSC title sequences (BEL and ST terminated)', () => {
|
||||
expect(stripAnsi('\x1b]0;title\x07text')).toBe('text');
|
||||
expect(stripAnsi('\x1b]8;;http://x\x1b\\link')).toBe('link');
|
||||
});
|
||||
|
||||
test('removes charset selection and keypad modes', () => {
|
||||
expect(stripAnsi('\x1b(Bhello\x1b=world\x1b>')).toBe('helloworld');
|
||||
});
|
||||
|
||||
test('removes private-mode CSI (cursor hide/show)', () => {
|
||||
expect(stripAnsi('\x1b[?25lhidden\x1b[?25h')).toBe('hidden');
|
||||
});
|
||||
});
|
||||
|
||||
describe('computeStalls', () => {
|
||||
const frames: PtyFrame[] = [
|
||||
{ tMs: 100, data: 'boot\n' },
|
||||
{ tMs: 300, data: 'fast\n' },
|
||||
{ tMs: 5300, data: 'after long silence\n' },
|
||||
{ tMs: 5400, data: 'tail\n' },
|
||||
];
|
||||
|
||||
test('finds mid-run gaps over the threshold with the pre-gap screen as context', () => {
|
||||
const stalls = computeStalls(frames, { thresholdMs: 2000 });
|
||||
expect(stalls.length).toBe(1);
|
||||
expect(stalls[0]!.startMs).toBe(300);
|
||||
expect(stalls[0]!.durationMs).toBe(5000);
|
||||
expect(stalls[0]!.context).toContain('fast');
|
||||
});
|
||||
|
||||
test('counts startup silence (spawn → first byte)', () => {
|
||||
const late: PtyFrame[] = [{ tMs: 4000, data: 'finally\n' }];
|
||||
const stalls = computeStalls(late, { thresholdMs: 2000 });
|
||||
expect(stalls.length).toBe(1);
|
||||
expect(stalls[0]!.startMs).toBe(0);
|
||||
expect(stalls[0]!.context).toBe('(no output yet)');
|
||||
});
|
||||
|
||||
test('counts trailing silence when endMs is supplied', () => {
|
||||
const stalls = computeStalls(frames, { thresholdMs: 2000, endMs: 12_000 });
|
||||
expect(stalls.length).toBe(2);
|
||||
expect(stalls[1]!.startMs).toBe(5400);
|
||||
expect(stalls[1]!.durationMs).toBe(6600);
|
||||
expect(stalls[1]!.context).toContain('tail');
|
||||
});
|
||||
|
||||
test('below-threshold gaps are ignored', () => {
|
||||
expect(computeStalls(frames, { thresholdMs: 6000 })).toEqual([]);
|
||||
});
|
||||
|
||||
test('empty frames + endMs = one all-silence stall', () => {
|
||||
const stalls = computeStalls([], { thresholdMs: 2000, endMs: 3000 });
|
||||
expect(stalls.length).toBe(1);
|
||||
expect(stalls[0]!.durationMs).toBe(3000);
|
||||
});
|
||||
|
||||
test('empty frames without endMs = no stalls', () => {
|
||||
expect(computeStalls([], { thresholdMs: 2000 })).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderStallsReport', () => {
|
||||
test('renders duration + context per stall', () => {
|
||||
const md = renderStallsReport(
|
||||
[{ startMs: 300, durationMs: 5000, context: 'Loading brain…' }],
|
||||
10_000,
|
||||
);
|
||||
expect(md).toContain('5.0s at t+0.3s');
|
||||
expect(md).toContain('Loading brain…');
|
||||
expect(md).toContain('1 silence window');
|
||||
});
|
||||
|
||||
test('clean report when no stalls', () => {
|
||||
expect(renderStallsReport([], 4000)).toContain('No stalls at threshold');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseDriveCommand', () => {
|
||||
test('parses line / send / key / note / stop', () => {
|
||||
expect(parseDriveCommand('{"line":"hello"}')).toEqual({ kind: 'send', data: 'hello\r' });
|
||||
expect(parseDriveCommand('{"send":"hello\\r"}')).toEqual({ kind: 'send', data: 'hello\r' });
|
||||
expect(parseDriveCommand('{"key":"Enter"}')).toEqual({ kind: 'key', key: 'Enter' });
|
||||
expect(parseDriveCommand('{"note":"confusing picker"}')).toEqual({
|
||||
kind: 'note',
|
||||
text: 'confusing picker',
|
||||
});
|
||||
expect(parseDriveCommand('{"stop":true}')).toEqual({ kind: 'stop' });
|
||||
});
|
||||
|
||||
test('line strips its own trailing newline before appending Enter', () => {
|
||||
expect(parseDriveCommand('{"line":"hello\\n"}')).toEqual({ kind: 'send', data: 'hello\r' });
|
||||
});
|
||||
|
||||
test('re-escapes raw control bytes that zsh echo produces from \\r', () => {
|
||||
// A literal CR byte inside the JSON string (what `echo '{"send":"x\r"}'`
|
||||
// yields under zsh) must parse instead of being dropped.
|
||||
expect(parseDriveCommand('{"send":"x\r"}')).toEqual({ kind: 'send', data: 'x\r' });
|
||||
expect(parseDriveCommand('{"line":"y\r"}')).toEqual({ kind: 'send', data: 'y\r' });
|
||||
});
|
||||
|
||||
test('rejects malformed JSON, unknown keys, unknown key names', () => {
|
||||
expect(parseDriveCommand('not json')).toBeNull();
|
||||
expect(parseDriveCommand('{"frobnicate":1}')).toBeNull();
|
||||
expect(parseDriveCommand('{"key":"HyperMeta"}')).toBeNull();
|
||||
expect(parseDriveCommand('{"stop":false}')).toBeNull();
|
||||
expect(parseDriveCommand('null')).toBeNull();
|
||||
expect(parseDriveCommand('"str"')).toBeNull();
|
||||
});
|
||||
|
||||
test('every KEY_MAP name round-trips through the key command', () => {
|
||||
for (const name of Object.keys(KEY_MAP)) {
|
||||
expect(parseDriveCommand(JSON.stringify({ key: name }))).toEqual({ kind: 'key', key: name });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildClaudeTuiSeed', () => {
|
||||
test('marks onboarding complete and pre-trusts dirs', () => {
|
||||
const seed = buildClaudeTuiSeed({ trustedDirs: ['/tmp/ws-a', '/tmp/ws-b'] });
|
||||
expect(seed.hasCompletedOnboarding).toBe(true);
|
||||
const projects = seed.projects as Record<string, { hasTrustDialogAccepted: boolean }>;
|
||||
expect(projects['/tmp/ws-a']!.hasTrustDialogAccepted).toBe(true);
|
||||
expect(projects['/tmp/ws-b']!.hasTrustDialogAccepted).toBe(true);
|
||||
expect(seed.customApiKeyResponses).toBeUndefined();
|
||||
});
|
||||
|
||||
test('approves the last 20 chars of a provided API key', () => {
|
||||
const key = 'sk-ant-' + 'x'.repeat(40);
|
||||
const seed = buildClaudeTuiSeed({ apiKey: key, trustedDirs: [] });
|
||||
expect(seed.customApiKeyResponses).toEqual({ approved: [key.slice(-20)] });
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveTranscript', () => {
|
||||
test('writes the full bundle (meta, raw, visible, frames, stalls)', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'gb-tty-unit-'));
|
||||
try {
|
||||
const frames: PtyFrame[] = [
|
||||
{ tMs: 50, data: '\x1b[32mready\x1b[0m\n' },
|
||||
{ tMs: 4050, data: 'done\n' },
|
||||
];
|
||||
saveTranscript(dir, {
|
||||
frames,
|
||||
raw: frames.map((f) => f.data).join(''),
|
||||
meta: {
|
||||
scenario: 'unit',
|
||||
argv: ['sh', '-c', 'x'],
|
||||
startedAtIso: '2026-08-12T00:00:00.000Z',
|
||||
exitCode: 0,
|
||||
durationMs: 4100,
|
||||
},
|
||||
});
|
||||
expect(JSON.parse(readFileSync(join(dir, 'meta.json'), 'utf8')).scenario).toBe('unit');
|
||||
expect(readFileSync(join(dir, 'visible.txt'), 'utf8')).toBe('ready\ndone\n');
|
||||
expect(readFileSync(join(dir, 'raw.txt'), 'utf8')).toContain('\x1b[32m');
|
||||
const frameLines = readFileSync(join(dir, 'frames.jsonl'), 'utf8').trim().split('\n');
|
||||
expect(frameLines.length).toBe(2);
|
||||
expect(readFileSync(join(dir, 'stalls.md'), 'utf8')).toContain('4.0s at t+0.1s');
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!ptySupported())('launchTty (live PTY smoke)', () => {
|
||||
test('captures timestamped frames from a real PTY child', async () => {
|
||||
const session = launchTty(['sh', '-c', 'printf one; sleep 0.6; printf two'], {
|
||||
timeoutMs: 15_000,
|
||||
});
|
||||
const code = await session.waitForExit(10_000);
|
||||
await session.close();
|
||||
expect(code).toBe(0);
|
||||
expect(session.visible()).toContain('one');
|
||||
expect(session.visible()).toContain('two');
|
||||
const frames = session.frames();
|
||||
expect(frames.length).toBeGreaterThanOrEqual(2);
|
||||
// The 600ms sleep shows up as a measurable gap (loose bound: >= 300ms).
|
||||
// Match the specific stall rather than index 0 — a slow spawn on a loaded
|
||||
// CI box can prepend a startup stall ('(no output yet)') before it.
|
||||
const stalls = computeStalls(frames, { thresholdMs: 300 });
|
||||
expect(stalls.length).toBeGreaterThanOrEqual(1);
|
||||
expect(stalls.some((s) => s.context.includes('one'))).toBe(true);
|
||||
}, 20_000);
|
||||
|
||||
test('send + waitFor drive an interactive child; child sees a real TTY', async () => {
|
||||
// Assert TTY-ness via `[ -t 0 ]` + a sentinel, NOT by matching the tty(1)
|
||||
// device path — macOS PTYs are /dev/ttysNNN but Linux CI PTYs are
|
||||
// /dev/pts/N, so a /dev\/tty pattern is a portability trap (bit us on CI).
|
||||
const session = launchTty(['sh', '-c', '[ -t 0 ] && echo IS_TTY || echo NOT_TTY; read line; echo "got:$line"'], {
|
||||
timeoutMs: 15_000,
|
||||
});
|
||||
await session.waitFor('IS_TTY', { timeoutMs: 8000 });
|
||||
session.send('ping\r');
|
||||
await session.waitFor('got:ping', { timeoutMs: 8000 });
|
||||
await session.waitForExit(5000);
|
||||
await session.close();
|
||||
expect(session.exited()).toBe(true);
|
||||
expect(session.visible()).not.toContain('NOT_TTY');
|
||||
}, 20_000);
|
||||
|
||||
test('waitForQuiet settles after output stops and reports exit as quiet', async () => {
|
||||
const session = launchTty(['sh', '-c', 'printf a; sleep 0.2; printf b'], {
|
||||
timeoutMs: 15_000,
|
||||
});
|
||||
const quiet = await session.waitForQuiet({ quietMs: 500, timeoutMs: 10_000 });
|
||||
expect(quiet).toBe(true);
|
||||
await session.close();
|
||||
}, 20_000);
|
||||
});
|
||||
@@ -7,6 +7,11 @@
|
||||
* in the module under test).
|
||||
*/
|
||||
import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
|
||||
import {
|
||||
resetGateway,
|
||||
__setChatTransportForTests,
|
||||
__setEmbedTransportForTests,
|
||||
} from '../src/core/ai/gateway.ts';
|
||||
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
|
||||
import {
|
||||
assembleTurnContext,
|
||||
@@ -33,6 +38,15 @@ import {
|
||||
let engine: PGLiteEngine;
|
||||
|
||||
beforeAll(async () => {
|
||||
// This file's corpus writes fire embed attempts iff the module-global
|
||||
// gateway looks configured AND keyed. A shard-mate can leave it configured
|
||||
// with a fake test key — the preload's beforeEach only restores when the
|
||||
// gateway is UNCONFIGURED, so that state persists and put_page 401s against
|
||||
// real OpenAI (the shard-8 flake). Reset back to the preload baseline
|
||||
// (real process.env → keyless degrade on CI) regardless of shard-mates.
|
||||
resetGateway();
|
||||
__setChatTransportForTests(null);
|
||||
__setEmbedTransportForTests(null);
|
||||
engine = new PGLiteEngine();
|
||||
await engine.connect({});
|
||||
await engine.initSchema();
|
||||
|
||||
Reference in New Issue
Block a user