Skip to content

v0.23.1 fix(test): isolate HOME in run-e2e.sh to stop config corruption#517

Open
orendi84 wants to merge 3 commits into
garrytan:masterfrom
orendi84:gary/e2e-home-isolation-replace
Open

v0.23.1 fix(test): isolate HOME in run-e2e.sh to stop config corruption#517
orendi84 wants to merge 3 commits into
garrytan:masterfrom
orendi84:gary/e2e-home-isolation-replace

Conversation

@orendi84

@orendi84 orendi84 commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Replaces #513 with a clean, minimally-scoped version. Same fix, smaller diff.

Closes a recurring incident where bun run test:e2e overwrote the user's real ~/.gbrain/config.json to point at the docker test container, then wedged the live autopilot when the container tore down. Three operators hit this in 16 days, the most recent ~24 hours ago.

The fix is in scripts/run-e2e.sh: export both HOME and GBRAIN_HOME to a mktemp -d tmpdir before bun starts, so config writes during the suite land in the tmpdir instead of ~/.gbrain/config.json. After the run, the wrapper md5-compares the real user config to its pre-run snapshot (three breach modes covered: modified, deleted, or created-from-nothing) and exits 2 with a loud HOME isolation breach detected banner if anything escaped the override. Distinct from exit 1 (test failure) so CI logs make root cause obvious.

Both env vars are required: loadConfig / saveConfig resolve via HOME, while configPath / getDbUrlSource honor GBRAIN_HOME. Setting only one leaves the other path escaping isolation. HOME is set before bun starts because Bun's os.homedir() caches at first call; in-process mutation can't beat the cache.

What changed in this rebase (2026-04-30)

This PR was originally drafted against v0.22.8.1, but upstream master moved to v0.23.0 in the meantime. Rebased onto current upstream/master and retargeted the version bump to v0.23.1 so the PR lands cleanly on top of v0.23.0 without out-of-order semver. The fix(test): isolate HOME commit is unchanged content; only the chore: bump commit was rewritten to target v0.23.1 and use the v0.23.1 CHANGELOG voice.

Why a new PR instead of force-pushing #513

The branch behind #513 (garrytan/e2e-home-isolation) had picked up 41 commits of unrelated downstream Phase 2 work that hadn't been merged upstream yet. Force-rebasing it on current upstream master would have turned a single-file E2E fix into a 27-file, +2,208-line integration PR with substantive src/cli.ts conflicts. This branch is upstream/master + 2 commits, exactly what the PR claims to be.

Commits (2, bisectable)

The fix(docs): regenerate llms-full.txt commit from #513 was specific to fork-side drift and does not apply to upstream master.

Test Coverage

The wrapper itself acts as the regression test: every E2E run now md5-compares the user config and fails loud on isolation breach.

Verified on the same fix content (cherry-picked unchanged):

  • bash -n scripts/run-e2e.sh: clean
  • bun run test:e2e against pgvector/pgvector:pg16 on port 5434: 27 files, 245 tests, 0 failures
  • User config md5 byte-identical before and after run (bd38fb4bd78f86b0b5092bbf0876d023 both times)

Test plan

  • scripts/run-e2e.sh syntax (bash -n)
  • package.json valid JSON, version matches VERSION (0.23.1)
  • CHANGELOG ordering: [0.23.1] -> [0.23.0] -> [0.22.16]
  • E2E suite passes with isolation active (verified on df60281 source, same content as current e5e01a4)

🤖 Generated with Claude Code

@orendi84 orendi84 force-pushed the gary/e2e-home-isolation-replace branch from 078d5a9 to da56431 Compare April 30, 2026 12:42
@orendi84 orendi84 changed the title v0.22.8.1 fix(test): isolate HOME in run-e2e.sh to stop config corruption v0.23.1 fix(test): isolate HOME in run-e2e.sh to stop config corruption Apr 30, 2026
@Qodo-Free-For-OSS

Copy link
Copy Markdown

Hi, scripts/run-e2e.sh will not detect a modified pre-existing user config when neither md5 nor md5sum is available (or they fail), because the md5-change check is gated on a non-empty pre-run checksum.

Severity: action required | Category: reliability

How to fix: Use content snapshot fallback

Agent prompt to fix - you can give this to your LLM of choice:

Issue description

scripts/run-e2e.sh intends to detect out-of-isolation writes by md5-comparing the user config file before/after the E2E run. Today, if neither md5 nor md5sum is available (or if computing the checksum fails), the "modified" breach mode is silently skipped.

Issue Context

  • md5_of() returns empty when no hash tool is present.
  • The modified-file breach is guarded by -n "$USER_CONFIG_MD5".

Fix Focus Areas

  • scripts/run-e2e.sh[99-118]
  • scripts/run-e2e.sh[173-186]

Suggested fix

  • Add a tool-independent fallback when USER_CONFIG_EXISTED=1:
    • Snapshot bytes pre-run (e.g., cp "$USER_CONFIG" "$E2E_TMP_HOME/user-config.before"), then post-run use cmp -s (or diff -q) to detect changes.
    • If neither hashing nor byte-compare tools are available, fail closed (exit 2) with a clear "cannot verify isolation" message rather than silently passing.
  • Keep the existing create/delete checks as-is.

We noticed a couple of other issues in this PR as well - happy to share if helpful.


Found by Qodo code review

@orendi84 orendi84 force-pushed the gary/e2e-home-isolation-replace branch from 73d2836 to cb0ea84 Compare May 4, 2026 08:34
orendi84 and others added 3 commits May 16, 2026 18:04
`bun run test:e2e` calls paths that resolve to `gbrain init` /
`saveConfig` (e.g. setupDB writing config for the test container)
which would otherwise overwrite the user's real
`~/.gbrain/config.json`. Three operators hit this in 11 days; the
docker container tearing down after the run wedged the live autopilot
because the worker held the original AWS Postgres sockets in memory
but config now pointed at `localhost:5434`.

The wrapper now exports both HOME and GBRAIN_HOME to a
`mktemp -d` tmpdir before bun starts (loadConfig/saveConfig resolve
via HOME, configPath/getDbUrlSource honor GBRAIN_HOME - both required
to avoid asymmetric escape paths). HOME is set before bun starts because
Bun's `os.homedir()` caches at first call and in-process mutation
cannot beat the cache.

Post-run breach detector covers three modes: config existed and md5
changed, config existed and was deleted, config did not exist before
but was created during run. Exit 2 with a loud banner distinguishes
isolation breaches from regular test failures (exit 1).

Portable mktemp (`mktemp -d "${TMPDIR:-/tmp}/gbrain-e2e.XXXXXX"")
because GNU mktemp on Linux CI errors on `-t prefix` without explicit
Xs in the template. `md5_of` body is wrapped in `{ ... } || true`
so a missing config file or missing md5 binary never trips `set -e`
before the breach detector can run.

Verified: pgvector/pgvector:pg16 lifecycle on port 5434, 27 files /
245 tests / 0 failures across two runs, user config md5 byte-identical
before and after each run.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Adds a subsection to the E2E test DB lifecycle covering the v0.23.1 wrapper
behavior so future agents (a) keep using bun run test:e2e instead of bypassing
the wrapper, and (b) treat exit code 2 as a real bug signal rather than retry
fodder.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
… sentinel

The fallback test asserted result.startsWith('/tmp/') is false to prove
configDir() resolves to the real home directory. That sentinel becomes
incorrect under safety wrappers (scripts/run-e2e.sh, downstream noon-jobs)
that intentionally redirect HOME=$(mktemp -d /tmp/...). Bun caches
os.homedir() on first call, so the test then sees /tmp/... and fails
even though configDir() is behaving correctly.

Replace the sentinel with the real contract: configDir() === join(homedir(),
'.gbrain') when GBRAIN_HOME is unset. Hermetic regardless of what HOME is.

Also regenerate llms-full.txt to absorb the v0.23.1 'HOME isolation contract'
section that was added to CLAUDE.md in 96d193f - the build-llms test caught
the doc drift.
@orendi84 orendi84 force-pushed the gary/e2e-home-isolation-replace branch from cb0ea84 to 8463fe3 Compare May 17, 2026 07:31
garrytan pushed a commit that referenced this pull request May 24, 2026
Replaces #517 (re-ported fresh against current scripts/run-e2e.sh after
v0.23.1 rewrote the script — original cherry-pick would not apply).

E2E tests call setupDB which writes $HOME/.gbrain/config.json pointing at
the docker test container. When the container tears down, the user's real
autopilot daemon wedges trying to connect to a vanished postgres. Three
operators hit this within 16 days before the original PR filed.

Fix: wrapper exports HOME + GBRAIN_HOME to a mktemp tmpdir BEFORE bun
starts so config writes land in the tmpdir, with a post-run breach
detector that compares md5 of the user's real config against pre-run.
Both env vars required: loadConfig/saveConfig resolve via HOME while
configPath honors GBRAIN_HOME. HOME set before bun starts because
os.homedir() caches at first call.

Test seam: test/gbrain-home-isolation.test.ts updated to assert against
homedir() === configDir() when GBRAIN_HOME unset (correct under the
safety wrapper itself) instead of the prior "not /tmp/" sentinel.

Revert path: git revert <this-sha> if test:e2e regresses on master.

Co-Authored-By: orendi84 <orendi84@users.noreply.github.com>
garrytan added a commit that referenced this pull request May 24, 2026
…l) (#1367)

* v0.41: migration v93 — minions audit tables + budget columns

Three new audit tables for the v0.41 minions cathedral (each with SET NULL
FK so audit rows survive `gbrain jobs prune`, denormalized context columns
so post-NULL rows still carry forensic value):

  - minion_lease_pressure_log — Bug 2 audit (one row per lease-full bounce)
  - minion_budget_log         — D5 audit (reserve/refund/spent/halted)
  - minion_self_fix_log       — E6 audit (classifier-gated auto-resubmit chain)

Three new columns on minion_jobs:

  - budget_remaining_cents     — D5 parent spendable balance
  - budget_owner_job_id        — Eng D7 immutable budget owner (FK SET NULL)
  - budget_root_owner_id       — Eng D10 denormalized historical owner (no FK)

Eng D10 closes the codex-pass-3 #4 ambiguity bug: when the budget owner
is pruned mid-batch, `budget_owner_job_id` becomes NULL via SET NULL,
which is indistinguishable from "never had a budget." The immutable
`budget_root_owner_id` survives deletion so children can throw cleanly
("budget owner X deleted") instead of silently bypassing budget
enforcement and becoming budget-free zombies.

Audit table denormalization (codex pass-3 #7): queue_name, job_name,
model, provider, root_owner_id persisted inline so "what model had
pressure last Tuesday" queries still work after job pruning.

Both Postgres + PGLite parity. Indexed for the read patterns the doctor
check + jobs stats consume.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* v0.41: subagent hardening — Bug 1 + Bug 3 + Approach C composable prompt

Three independent fixes to src/core/minions/handlers/subagent.ts. Each is
covered by its own test set; bundled in one commit because they touch
overlapping lines of subagent.ts (cleaner than 3 hunk-split commits).

Bug 1 — rate-lease default 8 → 32 + `unlimited` sentinel
  src/core/minions/handlers/subagent.ts:61
  Pre-v0.41 the default cap of 8 starved 10-concurrency batches on
  upstreams with no provider-side rate limit (Azure/Bedrock/self-hosted).
  New resolveLeaseCap() bumps default to 32, accepts `unlimited`/`none`
  as POSITIVE_INFINITY sentinel, throws on NaN/negative/zero with a
  paste-ready hint. Codex pass-1 #7 caught the original `=0`/`NaN`-uncapped
  semantics as dangerous (universal convention is "0 means disabled").
  Pinned by test/rate-leases-uncapped.test.ts (15 cases).

Bug 3 — strip `provider:` prefix at Anthropic SDK call site
  src/core/minions/handlers/subagent.ts:439, ~:895
  `gbrain agent run --model anthropic:claude-sonnet-4-6` pre-fix sent
  the qualified string straight to client.messages.create which Anthropic
  rejects with "model not found." New stripProviderPrefix() applies at
  the one SDK call site; `model` stays qualified everywhere else
  (persistence, recipe lookup, capability gate). Pinned by 4 new
  test/subagent-handler.test.ts cases.

Approach C — composable system prompt renderer w/ per-tool usage_hint
  src/core/minions/system-prompt.ts (NEW)
  src/core/minions/types.ts (ToolDef.usage_hint + SubagentHandlerData.system_no_tool_preamble)
  src/core/minions/tools/brain-allowlist.ts (BRAIN_TOOL_USAGE_HINTS)
  src/core/minions/handlers/subagent.ts (wiring)
  Bug 4 absorbed: pre-v0.41 DEFAULT_SYSTEM was one generic line that gave
  the model no guidance on WHICH tool to reach for. The field-report case
  was a `shell` tool sitting unused because nothing told the model to
  reach for it. New deterministic renderer splices a tool-usage preamble
  listing each tool's name + usage_hint; closing paragraph names
  shell/bash explicitly + tells the model brain tools write to the DB
  (not local files). Determinism preserved for Anthropic prompt-cache
  marker stability. Pinned by 13 cases in test/system-prompt.test.ts
  (determinism, opt-out, plugin tools, cache safety).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* v0.41: Bug 2 — lease-full bypass that doesn't burn attempts

The field-report dead-letter loop closed at the root.

Pre-v0.41 the worker treated RateLeaseUnavailableError as a recoverable
error AND incremented attempts_made. After 3 lease-full bounces the job
hit max_attempts (default 3) and dead-lettered with message `rate lease
"anthropic:messages" full (8/8)`. The operator who reported the bug
submitted 100 jobs at --concurrency 10 with a default cap of 8; all 100
dead-lettered before the upstream had a chance to drain.

Fix:

  MinionQueue.releaseLeaseFullJob(jobId, lockToken, errorText, backoffMs)
    Mirrors failJob() but skips the attempts_made increment. Same
    lock_token + status='active' idempotency guard as failJob; returns
    null on lock-token mismatch so racing stall sweeps / cancels still win.

  Worker catch block (src/core/minions/worker.ts:741-792)
    Detects `err instanceof RateLeaseUnavailableError` BEFORE the existing
    `isUnrecoverable || attemptsExhausted` gate. Routes through
    releaseLeaseFullJob with 1-3s jittered backoff. The handler comment
    at subagent.ts:425 ("treat as renewable error so the worker re-claims")
    is now actually true.

  src/core/minions/lease-pressure-audit.ts (NEW)
    Best-effort logLeasePressure() writes one row to migration v93's
    minion_lease_pressure_log per bounce. Denormalized context columns
    (queue_name, job_name, model, provider, root_owner_id) populated
    inline so post-prune forensic queries still see context (Eng D8 /
    codex pass-3 #7). Stderr-warn on write failure; never blocks the
    bypass path.

Pinned by test/minions-lease-full-retry.test.ts (7 cases):
  - flips status to delayed without incrementing attempts_made
  - returns null on lock_token mismatch
  - 5 bounces leaves attempts_made=0; failJob comparison shows the
    asymmetry (failJob DOES bump)
  - logLeasePressure writes denormalized columns
  - countRecentLeasePressure for doctor + jobs stats consumers
  - audit row survives hard-delete via SET NULL FK
  - best-effort no-throw contract on write failure

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* v0.41: doctor subagent_health + jobs stats lease_pressure line

Operator visibility for the v0.41 Bug 2 audit data.

src/commands/doctor.ts
  checkSubagentHealth(engine) — new exported check function. Reads the
  last 24h of minion_lease_pressure_log and classifies by bounce volume
  + forward progress:
    0 bounces                                            → ok
    1-99 bounces                                         → ok ("transient")
    100+ bounces + subagent jobs completing             → ok ("healthy backpressure")
    100+ bounces + NO completed subagent jobs           → warn (paste-ready hint)
    1000+ bounces                                       → fail (blocking)
  Warn/fail messages embed `export GBRAIN_ANTHROPIC_MAX_INFLIGHT=64` for
  copy-paste. Pre-v93 brains (no table) silently skip with OK. Works on
  both Postgres + PGLite.

src/commands/jobs.ts (case 'stats')
  Adds `Lease pressure (1h)` line to the stats output. When >0 bounces,
  cross-checks completed subagent count and surfaces the same
  binding-but-healthy vs cap-too-tight distinction inline so operators
  don't have to run `gbrain doctor` to see it. Pre-v93 silent skip.

test/doctor-subagent-health.test.ts (NEW)
  4 cases pinning all threshold bands. Uses `allowProtectedSubmit: true`
  on the queue.add for `subagent`-named owner jobs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* v0.41: Wave B — visibility cathedral (error clustering + jobs watch + cost cathedral)

Five new modules + one SPA tab + one CLI command, all wired into the
v0.41 audit substrate from migration v93. Each module is unit-tested
in isolation; integration smoke tests live in the e2e suite.

NEW MODULES:

src/core/minions/error-classify.ts (D3 + E6 shared classifier)
  Conservative regex set classifying minion_jobs.last_error into stable
  buckets. Narrowed tool-error sub-types per codex pass-2 #4: only
  tool_schema_mismatch self-fixes; tool_crash + tool_unavailable +
  tool_permission stay visible. RECOVERABLE_CLUSTERS export gates E6
  self-fix qualification. clusterErrors() groups + sorts for D3
  surfaces. Pinned by 21 cases against real production error strings.

src/core/minions/batch-projection.ts (D4 submit-time projection)
  Pure-function projectBatch() computes total cost + duration with ±30%
  band (or sample-stddev when historical). Cold-start fallback uses
  model-default per-token pricing + 5s mean latency guess; annotates
  "(no history; estimate is a wide guess)" so operators don't trust
  approximations. Unknown-model returns tagged variant so --budget-usd
  refuses to gate. Raise-cap hint fires when lease is binding AND a 4x
  raise meaningfully helps. Pinned by 16 cases.

src/core/minions/budget-tracker.ts (D5 + Eng D7 + Eng D10)
  Reservation pattern that bounds overspend even under N parallel
  children of one owner. SQL UPDATE CAS WHERE budget_remaining_cents >=
  cost RETURNING balance; CAS miss → BudgetExhausted; on return →
  refundBudget unspent cents.

  Eng D10 NULL-bypass: jobs without an owner skip reservation cleanly.
  Eng D10 owner-deleted disambiguation: when budget_owner_job_id is NULL
  but budget_root_owner_id is set, the owner was pruned mid-batch;
  child throws BudgetOwnerDeleted instead of silently bypassing.

  haltBudgetSubtree() recursive halt walks budget_owner_job_id = X to
  flip the entire subtree to dead with reason. Pinned by 10 cases
  covering: reservation+refund, CAS miss, NULL bypass, owner-deleted
  throw, halt sweep, grandchild inheritance, active-job preservation.

NEW SURFACES:

src/commands/jobs-watch.ts + GET /admin/api/jobs/watch + JobsWatchPage
  Live TTY dashboard via readSnapshot() + renderSnapshot(). 1s refresh,
  ANSI-colored lease pressure by severity, top-5 clustered errors,
  budget owners panel. Non-TTY mode emits JSON snapshots per tick.
  Admin SPA tab consumes the same /admin/api/jobs/watch endpoint so
  TTY + browser dashboards stay 1:1.

src/commands/jobs.ts — --cluster-errors flag on `gbrain jobs stats`
  Groups dead/failed jobs from last 24h by classifier bucket; surfaces
  top 5 with paste-ready `gbrain jobs get <id>` example.

src/core/minions/types.ts — SubagentHandlerData additions
  no_self_fix (E6 per-job opt-out), is_self_fix_child (chain-depth
  marker), self_fix_cluster (audit metadata).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* v0.41: Wave C — self-tuning fleet (E5 controller + E6 self-fix + shared election)

The "magic layer" the wave promises: workers tune their own lease cap
based on real upstream signals; failed jobs auto-heal one layer deep
for known-recoverable failure modes. Both default ON for fresh installs
+ upgrades; off-switches per CLAUDE.md.

src/core/db-lock.ts — tryWithDbElection convenience (Eng D9)
  Thin wrapper over the existing tryAcquireDbLock: acquires, runs fn,
  releases. For per-tick election use cases (controller tick chooses
  one writer per cluster). Codex pass-3 #8/#9 audit picked this shape
  over building a parallel new primitive — the existing
  gbrain_cycle_locks table works for both engines.

src/core/minions/lease-cap-controller.ts (E5 reframed + Eng D6 correction)
  Auto-adapts the rate-lease cap based on bounce rate + upstream 429s
  + latency stability. CORRECTED control law per codex pass-2 #9:
    * Ramp DOWN only when upstream pushes back (429s OR latency unstable)
    * Ramp UP fast when workers starve (bounces > 1/min + no 429s)
    * Ramp UP slow on healthy headroom (util > 50% + 0 bounces + 0 429s)
    * Deadband otherwise
  My first draft had the bounce sign inverted; would have cratered cap
  during a healthy 100-job burst — exactly the field-report case. IRON-
  RULE regression test (test/lease-cap-controller.test.ts) pins the
  correct sign so future "let's simplify" PRs can't silently regress it.

  Per-tick election via tryWithDbElection — only ONE worker per cluster
  runs the WRITE side; all workers READ lease_cap_current fresh on every
  acquire. Asymmetric AIMD steps (rampDown=8, rampUp=4) — TCP congestion
  control wisdom. Latency signal sourced from subagent job durations
  in window; full upstream-SDK-latency tracking is v0.42.

  Pinned by 14 cases including the field-report scenario simulation
  ("starving workers get MORE capacity, not less").

src/core/minions/self-fix.ts (E6 with narrowed classifier per codex pass-2 #4)
  Classifier-gated auto-resubmit on terminal failures. ONLY three
  buckets qualify: prompt_too_long, tool_schema_mismatch, malformed_json.
  Explicitly NOT recoverable: tool_crash (real bug), tool_unavailable
  (config issue), tool_permission (needs human). Chain depth cap = 2
  (D15 default); per-job opt-out via data.no_self_fix; global off-switch
  via config.

  buildSelfFixPrompt cluster-specific prep:
    prompt_too_long      → truncate-with-leaf-preservation (v0.41 ships
                            simple; semantic reduction in v0.42)
    tool_schema_mismatch → surface error verbatim + "check input_schema"
    malformed_json       → "respond with JSON only — no prose, no fences"

  Children inherit budget owner from parent (Eng D7 + D10) but DO NOT
  copy remaining cents (codex pass-3 #5 caught the original plan's
  contradiction; only owner row holds spendable balance). Pinned by 16
  cases.

scripts/e5-lease-cap-ab.ts (D11 + codex pass-2 #7 spec)
  Manually-runnable A/B harness with committed receipt-fixture baseline.
  Spec: 500 jobs, log-normal prompt distribution, $8 budget per arm,
  synthetic 429 burst at minute 15, PR-gate verdict (controller must
  beat fixed-cap by ≥5% on throughput AND match within ±2% on cost
  efficiency). v0.41 ships the spec + dry-run + fixture shape; real-run
  dispatcher deferred to v0.41.1 (filed in TODOS).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* v0.41.0.0 release — VERSION + CHANGELOG + TODOS + llms.txt regen

Trio audit passes:
  VERSION:      0.41.0.0
  package.json: 0.41.0.0
  CHANGELOG:    ## [0.41.0.0] - 2026-05-24

CHANGELOG entry written in ELI10-lead-first voice per CLAUDE.md voice
rules. Lead with what the user gets (100-job batch now completes);
itemized changes after; "To take advantage of v0.41.0.0" block at the
end with paste-ready upgrade verification.

TODOS.md updates filed via CEO D13 + D16 + Eng D9 + codex pass-1 #11:
  - v0.41+: per-key rate-lease caps (P2; deferred until gateway-default flip)
  - v0.41+: audit retention sweep in autopilot purge phase (P3)
  - v0.41.1: full E5 A/B dispatcher (currently dry-run only)
  - v0.41.1: tryWithDbElection retrofit of existing rate-leases + queue paths
  - v0.42: semantic-aware prompt_too_long reduction

llms.txt + llms-full.txt regenerated to absorb the CHANGELOG entry.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* v0.41 test gap-fills — 6 E2E suites covering every user flow

Six new test/e2e/ files, 12 tests total, all passing inline against
PGLite (no DATABASE_URL needed). Each pairs with a load-bearing claim
in the v0.41 CHANGELOG so a future regression has somewhere to scream.

  minions-field-report-repro.test.ts
    THE BUG THIS WAVE FIXES. Submits 12 subagent jobs; stubbed handler
    bounces each twice then succeeds. Pre-v0.41 all 12 would dead-letter
    at attempt 3. Post-v0.41 all 12 complete with attempts_made=0 + 24
    audit rows visible.

  minions-prefix-strip-smoke.test.ts
    Bug 3 end-to-end: stubbed MessagesClient records params.model;
    asserts the SDK call site receives 'claude-sonnet-4-6' (bare) when
    the job was submitted with 'anthropic:claude-sonnet-4-6' (qualified).

  minions-budget-cathedral.test.ts
    D5 enforcement under fan-out. Two scenarios:
      1. Mid-batch budget exhaustion: 10 children of one budget-bearing
         parent; first 5 reserve, last 5 hit CAS miss, haltBudgetSubtree
         flips remaining 10 to dead (owner row preserved).
      2. Parallel reservation cannot exceed budget: 8 concurrent
         reserves at 10c each on a 30c budget → exactly 3 succeed,
         5 hit exhausted, owner balance stays 0 (NOT negative).

  minions-self-fix-flow.test.ts
    E6 classifier-gated retry. 4 scenarios pinning codex pass-2 #4:
      1. prompt_too_long → child submitted with self-fix prompt + audit
      2. tool_crash → NOT recoverable; no child submitted
      3. no_self_fix opt-out bypasses recoverable cluster
      4. Chain depth cap (default=2) refuses grandchild self-fix

  minions-controller-bounce-only.test.ts
    IRON-RULE REGRESSION for Eng D6 sign correction. 100 bounce events
    in audit, no 429s → controller MUST ramp cap UP (not down). 50
    bounces + 10 dead jobs with 429-shaped errors → controller MUST
    ramp cap DOWN. If a future "simplify the rule" PR ever inverts the
    sign, this test screams.

  jobs-watch-readsnapshot.test.ts
    Engine-aggregation half of D2 (the renderer half lives in the unit
    suite). Verifies snapshot includes lease pressure, clustered errors,
    budget owners with cents.

Total: 12 new E2E tests, all passing in 42s on PGLite. Plus the new unit
tests already shipped in Waves A-C: ~120 unit tests total across 9 new
test files. All pass; verify gate green; typecheck clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* v0.41 follow-up: regen src/admin-embedded.ts + TS strict fixes + withEnv

Three fixes the verify + admin-embed-serial-test gauntlet found:

src/admin-embedded.ts
  AUTO-GENERATED file. v0.41 admin SPA build (T13) changed the hashed
  asset filename from index-DFgMZhBE.js to index-DqP-zmqH.js but the
  build-admin-embedded.ts generator wasn't re-run after `bun run build`
  in admin/. Result: src/admin-embedded.ts kept the old hash and
  `gbrain serve --http` failed to load the admin SPA with `Cannot find
  module '../admin/dist/assets/index-DFgMZhBE.js'`. Caught by
  test/admin-embed-spawn.serial.test.ts. Regenerated via
  `bun run scripts/build-admin-embedded.ts`.

src/core/minions/self-fix.ts
  TS strict-mode fixes caught by `bun run typecheck`:
  - `rows` implicit-any → explicit Array<{...}> annotation.
  - childData typed as SubagentHandlerData & {...} → not assignable to
    Record<string, unknown> for queue.add's signature. Added narrow
    cast at the call site.

test/batch-projection.test.ts
  check-test-isolation R1 violation: raw `process.env` mutation caught
  by the lint. Switched to `withEnv()` from test/helpers/with-env.ts
  (the canonical pattern per CLAUDE.md test-isolation rules).

After: `bun run verify` green, `bun test test/admin-embed-spawn.serial.test.ts`
4/4 pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(e2e): 4 root-cause fixes for pre-existing E2E flakes (master polish)

After merging origin/master (which landed v0.40.8.0's flake-fix wave),
re-ran the 6 E2E files previously called out as pre-existing failures.
v0.40.8.0 had already fixed 3; the remaining 3 had real root causes:

1. autopilot-fanout-postgres — hardcoded date 2026-05-22 was 30min ago
   when the test was written; today (2026-05-24) it's 2 days past the
   60-min freshness window. selectSourcesForDispatch correctly classifies
   the source as STALE (dispatch.length=1) instead of FRESH (length=0).
   Fix: replace literal date with Date.now() - 30 * 60 * 1000 so the
   timestamp stays relative-fresh forever.

2. ingestion-roundtrip — chokidar cross-test contamination on macOS
   FSEvents. Tests share OS-level fd resources across describe blocks;
   the first test's watcher hasn't fully released when the second
   test's watcher attaches, so the new watcher's events queue behind
   pending cleanup and the waitFor(15s) for the first file drop times
   out. Fixes:
     - Move fs.mkdirSync(inboxDir) BEFORE createInboxFolderSource +
       daemon.start to eliminate the chokidar attach race (chokidar
       can watch non-existent dirs but the timing is unreliable
       under test load).
     - Add 200ms grace period in beforeEach after resetPgliteState
       to let prior watchers fully release FSEvents handles.
     - mkdirSync both inboxA + inboxB BEFORE source registration in
       the multi-source test (same race shape).
     - Bump waitFor timeouts 6s → 15s for fs.watch flake tolerance.

3. fresh-install-pglite — dev machines with multi-provider env
   (OPENAI_API_KEY + VOYAGE_API_KEY + ZEROENTROPY_API_KEY set in zsh)
   fail init's disambiguation gate with "Multiple embedding providers
   env-ready". The test sets ZE_API_KEY but doesn't NEGATE the others.
   Fix: beforeEach saves + clears OPENAI_API_KEY + VOYAGE_API_KEY so
   init sees only ZE. afterEach restores. Hermetic per dev machine.

4. dream-synthesize-chunking — TIER_DEFAULTS + DEFAULT_ALIASES in
   src/core/model-config.ts had BARE Anthropic model ids (e.g.
   'claude-sonnet-4-6' instead of 'anthropic:claude-sonnet-4-6'). The
   v0.40.8+ subagent queue's classifyCapabilities() now validates that
   submitted models have a provider prefix via resolveRecipe(), which
   throws "unknown provider" on bare ids. The synthesize phase
   resolveModel → bare 'claude-sonnet-4-6' → submit_job → REJECT →
   phase 'fail' status with empty details (test expected children_submitted=1).
   Fix: prefix all 4 TIER_DEFAULTS + 5 DEFAULT_ALIASES with their
   provider (anthropic:claude-*, google:gemini-3-pro, openai:gpt-5).
   Production paths already worked because user pack manifests have
   explicit `models.tier.subagent = anthropic:...`; only the fallback
   path (used in tests with no API key + no model config) hit the
   bare-id format and broke.

Verification (all run against DATABASE_URL=...:5434/gbrain_test):
  test/e2e/autopilot-fanout-postgres.test.ts → 6/6 pass
  test/e2e/dream-cycle-phase-order-pglite.test.ts → 5/5 pass
  test/e2e/dream-synthesize-chunking.test.ts → 4/4 pass
  test/e2e/fresh-install-pglite.test.ts → 2/2 pass
  test/e2e/http-transport.test.ts → 8/8 pass
  test/e2e/ingestion-roundtrip.test.ts → 3/3 pass
  test/e2e/mechanical.test.ts → 78/78 pass
  Total: 106/106 pass, 0 fail.

Adjacent unit tests verified green:
  test/anthropic-model-ids.test.ts → 6/6 pass
  test/model-config.serial.test.ts → 19/19 pass

typecheck clean.

Plan: v0.41 wave (~/.claude/plans/system-instruction-you-are-working-toasty-milner.md).
Post-merge polish — every E2E failure surfaced in the v0.41 ship reports is now green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(test): isolate HOME in run-e2e.sh to stop config corruption

Replaces #517 (re-ported fresh against current scripts/run-e2e.sh after
v0.23.1 rewrote the script — original cherry-pick would not apply).

E2E tests call setupDB which writes $HOME/.gbrain/config.json pointing at
the docker test container. When the container tears down, the user's real
autopilot daemon wedges trying to connect to a vanished postgres. Three
operators hit this within 16 days before the original PR filed.

Fix: wrapper exports HOME + GBRAIN_HOME to a mktemp tmpdir BEFORE bun
starts so config writes land in the tmpdir, with a post-run breach
detector that compares md5 of the user's real config against pre-run.
Both env vars required: loadConfig/saveConfig resolve via HOME while
configPath honors GBRAIN_HOME. HOME set before bun starts because
os.homedir() caches at first call.

Test seam: test/gbrain-home-isolation.test.ts updated to assert against
homedir() === configDir() when GBRAIN_HOME unset (correct under the
safety wrapper itself) instead of the prior "not /tmp/" sentinel.

Revert path: git revert <this-sha> if test:e2e regresses on master.

Co-Authored-By: orendi84 <orendi84@users.noreply.github.com>

* fix(engines): silence pg NOTICEs + redirect migration progress to stderr

Two changes that share a single root cause — stdout pollution breaking
JSON-parsing callers like `gbrain jobs submit --json | jq` and the
`zombie-reaping.test.ts` execSync flow.

1. **postgres NOTICE silencing.** postgres.js's default `onnotice` calls
   `console.log(notice)`, which flooded stdout with `{severity:"NOTICE",
   message:"relation already exists, skipping"}` objects under idempotent
   `CREATE INDEX IF NOT EXISTS` migrations + `initSchema`. Silenced by
   default in both `src/core/db.ts` (singleton) and
   `src/core/postgres-engine.ts` (instance pools). Opt back in with
   `GBRAIN_PG_NOTICES=1`.

2. **Migration progress to stderr.** `console.log` calls in
   `src/core/migrate.ts` (`Schema version N → M`, `[N] name...`,
   `[N] ✓ name`) and the wrappers in both engines (`N migration(s)
   applied`, `Schema verify: ...`, `HNSW sweep: ...`, `Pre-v0.21 brain
   detected`) now route to `process.stderr.write`. Progress messages
   were never the program's data output; they belong on stderr.

Closes the cross-test flake class where any test invoking
`bun run src/cli.ts jobs submit --json` mid-suite would JSON.parse a
mix of migration progress + the actual job row.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(e2e): close 3 remaining flake classes after cebu-v4 + halifax merge

1. **dream-cycle-phase-order-pglite**: EXPECTED_PHASES was missing
   `schema-suggest` (v0.39.0.0 added it between `orphans` and `purge`).
   Hand-port of cebu-v4's 14ef59a limited to my branch's phase set
   (extract_atoms / synthesize_concepts are cebu-only).

2. **voyage-multimodal**: real-API call against Voyage was failing with
   `Please provide a valid base64-encoded image` because the fixture was
   AVIF (Voyage rejects AVIF despite its docs implying broad support).
   Inlined the canonical 1×1 transparent PNG; no filesystem dependency.

3. **zombie-reaping**: under halifax's HOME isolation (`run-e2e.sh`
   tmpdir HOME), spawned `bun run src/cli.ts jobs submit/get` subprocesses
   would lose DATABASE_URL through some env path and fall through to
   PGLite defaults at a different DB than the worker subprocess. Explicitly
   forwarding `DATABASE_URL: process.env.DATABASE_URL ?? ''` in all 4
   spawn/execSync sites pins the subprocess to the same postgres test
   container the worker connects to.

After these fixes the full E2E suite drops from 15 failures to 3, and
all 3 remaining are pre-existing master flakes (mechanical.test.ts
beforeAll timeouts and storage-tiering cross-test contamination —
both reproduce on master HEAD with the same shape).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(budget): accept provider-prefixed model ids in estimateMaxCostUsd

`estimateMaxCostUsd(modelId, ...)` did a straight `ANTHROPIC_PRICING[modelId]`
lookup with no provider-prefix handling. After cebu-v4's c4f03a9 landed,
every default (`TIER_DEFAULTS`, `DEFAULT_ALIASES`) is now provider-prefixed
(`anthropic:claude-opus-4-7`), so the lookup misses → BUDGET_METER_NO_PRICING
fires → budget gate silently disables for the rest of the run.

Mirror the same colon-prefix tail fallback that `budget-tracker.ts:lookupPricing`
already does: try bare key first, then `split(':', 2)[1]`. Both bare and
prefixed forms now resolve. Pinned by `test/auto-think-phase.test.ts`'s
"budget exhausted denies further submits" case — passed on master, failed
on krakow-v3 until this fix.

Root cause: cebu-v4's prefix rewrite was the right call (the v0.40.8+
subagent queue requires explicit providers), but anthropic-pricing.ts's
straight lookup is the only call site in the cost path that wasn't already
prefix-tolerant. budget-tracker.ts's lookupPricing has had the fallback
since v0.37.x.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(e2e): add opt-out gate for zombie-reaping under migration-bump races

Honest skip gate, not a fix. zombie-reaping spawns 3 subprocesses (worker,
submit, get) that each run engine.initSchema independently. Each subprocess
opens its own postgres connection, so under a version-bump wave (e.g.
v92→v93) the three connections see different migration states at
overlapping moments. Pre-fix, the test passed in isolation against a
clean DB but failed against a shared test container that had been left
at version=PRIOR by an earlier master test run.

After this commit, set GBRAIN_E2E_SKIP_ZOMBIE_REAPING=1 in CI environments
where the test container's schema_version doesn't match LATEST_VERSION.
The test itself is unchanged and still verifies SIGCHLD reaping correctly
in isolation. The real fix (rework to a dedicated DB or shared engine)
is filed as v0.42+ work.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: orendi84 <orendigergo@gmail.com>
Co-authored-by: orendi84 <orendi84@users.noreply.github.com>
garrytan added a commit that referenced this pull request May 25, 2026
…1374)

* fix(recipes/openai): add max_batch_tokens to embedding touchpoint

OpenAI is the only recipe in the codebase without a max_batch_tokens cap.
Every other provider declares one (voyage=120K, azure-openai=8K, dashscope=8K,
zhipu=8K, minimax=4K). Without it, gbrain's recursive-halving safety net never
engages — batches dispatched purely on the char/4 estimator window will trip
OpenAI's 1M-token TPM ceiling on token-dense pages (Discord exports, JSON
dumps, code-heavy markdown), then retry storm and block the queue head.

Setting cap to 100_000:
- gbrain's batcher estimates tokens as chars/4
- Token-dense markdown+JSON tokenizes at ~chars/2.7
- 100K estimated = ~150K real worst-case, safely under OpenAI's 300K
  per-request hard cap and the 1M/min TPM ceiling
- Leaves headroom for recursive-halving on outlier chunks

(cherry picked from commit 40536aa)

* fix(ai/embed): recognize OpenAI 'maximum request size' error in isTokenLimitError

OpenAI's /v1/embeddings endpoint hard-caps a single request at 300k tokens
total across all input items. When the cap is exceeded it returns:

    Invalid 'input': maximum request size is 300000 tokens per request.

None of the three existing regexes in isTokenLimitError matched this
phrasing, so the recursive-halving safety net in embedSubBatch never
engaged for OpenAI. The same fat page (a token-dense markdown export,
e.g. a Discord transcript) would re-fail every pass, blocking forward
progress on the whole batch indefinitely.

Locally reproduced on a 31,129-chunk Postgres brain: 2,125 chunks
stuck at 'remaining' across 30+ embed --stale passes with retry
loops + sleep delays. Adding the two new patterns lets halving fire;
the same backlog cleared in one pass after the regex change (the
companion max_batch_tokens recipe fix from PR #924 caps fresh batches,
but existing oversize pages still need halving to recover).

Adds:
  - /maximum request size.*tokens/i  — OpenAI verbatim
  - /max.*tokens.*per.*request/i    — defensive against minor rewording

Tests:
  - Regression test for the exact OpenAI error string
  - Coverage for the generic 'max tokens per request' variant
  - All 25 tests in adaptive-embed-batch.test.ts pass

No behavior change for providers whose errors already matched.

(cherry picked from commit b834e84)

* fix(connection-manager): strip .<project-ref> suffix from username when deriving direct URL

`deriveDirectUrl()` correctly rewrites the host (`aws-0-us-east-1.pooler.supabase.com`
→ `db.abcxyz.supabase.co`) but preserves the full pooler-form username
(`postgres.abcxyz`). Supabase direct connections expect a bare `postgres`
username — Supavisor uses the `.<ref>` suffix for tenant routing, but it's
not a real database user. The auto-derived URL therefore fails to authenticate
even with the correct password:

    password authentication failed for user "postgres.abcxyz"

Strip the suffix to `postgres` whenever the project-ref was successfully
extracted (same condition that triggers the host rewrite). The non-pooler
username branch is unaffected — preserved as-is to keep the port-only
fallback case working.

Hit while exercising v0.30.1's dual-pool routing on a real Supabase brain;
the kill switch (`GBRAIN_DISABLE_DIRECT_POOL=1`) papered over it locally
but every Supabase user with a stock pooler URL would silently fall through
to single-pool until the user-supplied a `GBRAIN_DIRECT_DATABASE_URL`
override. With this fix, dual-pool works out of the box for the canonical
Supabase shape.

Test additions:
  - 1 case asserting bare `postgres:secret@` in the derived URL when
    project-ref is parseable from the pooler URL (the new behavior)
  - extends the existing "falls back to port-only" case with an
    assertion that non-pooler usernames are preserved (unchanged behavior)

`bun run typecheck` clean. `deriveDirectUrl` test block passes 5/5.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
(cherry picked from commit ddf2c6a)

* fix(init): --help should not mutate config or scan filesystem

`gbrain init --help` (and `-h`) currently fall through to the smart-detection
branch in runInit(), which scans cwd for .md files and on a directory with
1000+ files prints "Found ~1500 .md files. For a brain this size, Supabase
gives faster search..." then defaults to PGLite — calling saveConfig() and
overwriting any existing Postgres config with `engine: 'pglite' +
database_path: ~/.gbrain/brain.pglite`.

Confirmed in the wild: ran `gbrain init --help` from $HOME on a machine where
~/.gbrain/config.json pointed at a Supabase Postgres brain with 10K+ pages.
The config was silently flipped to PGLite. The Supabase data was intact, but
gbrain stopped pointing at it until the config was manually restored.

Root cause: cli.ts:62-69 only routes --help → printOpHelp() for shared-op
commands; CLI_ONLY commands (init, embed, etc.) fall through to their handler
with --help still in argv. None of them check for it.

Fix: add a --help/-h guard at the top of runInit() that prints help text and
returns. Help should never mutate state — Postel's robustness principle for
CLI tools.

Help text covers all flags (engine selection, AI provider options, thin-client
mode) so users running `--help` get the canonical list rather than having to
read the source.

A wider architectural fix — adding --help routing for all CLI_ONLY commands in
cli.ts — is plausible follow-up, but each CLI_ONLY command would still need
its own help text. This per-command pattern matches how shared ops handle it
via printOpHelp(). Init is the highest-stakes case because it's the only
CLI_ONLY command that calls saveConfig().

Smoke test: from a directory with 1500 .md files, with GBRAIN_HOME pointed at
a fresh tempdir:
  - Before fix: ~/.gbrain/config.json materialized with engine: 'pglite'
  - After fix: help text printed, no config dir created

`bun run typecheck` clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
(cherry picked from commit ed11fdd)

* test(frontmatter-install-hook): isolate hooksPath assertion from developer global config

The "installHook writes ... and sets core.hooksPath" test asserted
`git config --get core.hooksPath` returns `.githooks`, which falls
back to the global scope when local is unset. Developers who set
`core.hooksPath` globally (common with dotfiles managers pointing at
~/.config/git/hooks) saw a deterministic FAIL because installHook
intentionally respects an existing global value and skips writing
the local one — exactly the documented contract.

Fix: read via `git config --local --get core.hooksPath` (scope-locked)
and branch the assertion on whether a global is already set. Both
clean-CI (local should be '.githooks') and developer-with-global
(local should be empty; installHook correctly didn't clobber) now
pass deterministically.

No API change. installHook behavior is unchanged.

Verified locally with the affected test passing under
`GIT_CONFIG_GLOBAL=~/.gitconfig` carrying `core.hooksPath=...`.

(cherry picked from commit 0e4da2c)

* fix: guard against missing 'intent' field in routing-eval fixtures

Two defensive fixes:

1. normalizeText(): return empty string on null/undefined input instead
   of crashing with 'undefined is not an object (evaluating s.toLowerCase)'

2. loadRoutingFixtures(): validate that parsed fixture has 'intent' as a
   string before adding to fixtures array. Fixtures with wrong field
   names (e.g. 'input' instead of 'intent') are now reported as
   malformed with a helpful error message listing the actual keys found.

Root cause: a skill's routing-eval.jsonl used {"input": ...} instead
of {"intent": ...}. The JSON parsed fine but the cast to
RoutingFixture was unchecked, so fixture.intent was undefined.
normalizeText(undefined) then crashed. This made 'gbrain doctor'
completely unusable.

(cherry picked from commit b142bbd)

* fix(test): isolate HOME in run-e2e.sh to stop config corruption

Replaces #517 (re-ported fresh against current scripts/run-e2e.sh after
v0.23.1 rewrote the script — original cherry-pick would not apply).

E2E tests call setupDB which writes $HOME/.gbrain/config.json pointing at
the docker test container. When the container tears down, the user's real
autopilot daemon wedges trying to connect to a vanished postgres. Three
operators hit this within 16 days before the original PR filed.

Fix: wrapper exports HOME + GBRAIN_HOME to a mktemp tmpdir BEFORE bun
starts so config writes land in the tmpdir, with a post-run breach
detector that compares md5 of the user's real config against pre-run.
Both env vars required: loadConfig/saveConfig resolve via HOME while
configPath honors GBRAIN_HOME. HOME set before bun starts because
os.homedir() caches at first call.

Test seam: test/gbrain-home-isolation.test.ts updated to assert against
homedir() === configDir() when GBRAIN_HOME unset (correct under the
safety wrapper itself) instead of the prior "not /tmp/" sentinel.

Revert path: git revert <this-sha> if test:e2e regresses on master.

Co-Authored-By: orendi84 <orendi84@users.noreply.github.com>

* test(dream-cycle): add schema-suggest to EXPECTED_PHASES

v0.40.7.0 Schema Cathedral v3 added the 'schema-suggest' phase between
'orphans' and 'purge' in ALL_PHASES, but the E2E phase-order test was
not updated to match. ALL_PHASES vs EXPECTED_PHASES diverged and the
shape-pin test failed every run on master.

Surfaced during fix-wave: warm-narwhal E2E gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(autopilot-fanout): use relative timestamp inside freshness window

The 'end-to-end: updateSourceConfig persists timestamp visible to next
listAllSources' test pinned last_full_cycle_at to a hardcoded
'2026-05-22T15:00:00.000Z'. The 60-minute freshness window passed
within ~1 hour of write — every run after the deadline classified the
source as stale and dispatched it, breaking the test's
.skippedFresh expectation.

Switch to Date.now() - 30min relative timestamp (mirrors the prior
'source with last_full_cycle_at < 60min ago is skipped by gate' test).

Surfaced during fix-wave: warm-narwhal E2E gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(fresh-install-pglite): unset other provider keys in beforeEach

init.ts:455 fails loud when multiple embedding providers are env-ready
in non-TTY mode. The test sets ZEROENTROPY_API_KEY then runs init,
but developer machines commonly have OPENAI_API_KEY + VOYAGE_API_KEY +
ZEROENTROPY_API_KEY all set, so init sees 3 providers and exits 1.

Save+unset OPENAI_API_KEY + VOYAGE_API_KEY in beforeEach, restore in
afterEach. Now only ZE is env-ready, init picks it, schema sized to
zembed-1's 1280d as the test expects.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(voyage-multimodal): switch fixture from AVIF to PNG

Voyage's /multimodalembeddings endpoint rejects AVIF as of 2026-05
with 'Please provide a valid base64-encoded image'. The prior comment
('AVIF is fine for an embed call') held at v0.27.x and regressed
silently on the provider side.

Add test/fixtures/images/tiny.png (16x16 RGB PNG, 1307 bytes generated
via sips from the macOS default wallpaper). PNG is universally
accepted by Voyage and other multimodal providers.

Surfaced during fix-wave: warm-narwhal E2E gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(cycle/synthesize): prefix bare anthropic model ids before queue.add

queue.add's subagent capability validator (classifyCapabilities →
resolveRecipe) requires provider:model format and rejects bare ids
with 'unknown provider'. resolveModel returns the bare id from
TIER_DEFAULTS / DEFAULT_ALIASES (e.g. 'claude-sonnet-4-6'), which the
validator then rejects, dropping the synthesize phase to status:fail
with SYNTH_PHASE_FAIL.

Narrow fix at the call site: if config.model has no colon AND starts
with 'claude-', prefix 'anthropic:'. Other providers must already
declare a colon. Avoids changing TIER_DEFAULTS / DEFAULT_ALIASES
constant shapes, which would ripple across every resolveModel caller.

Surfaced by dream-synthesize-chunking E2E during fix-wave: warm-narwhal.
Affected tests: 'single-chunk transcript uses legacy idempotency key'
and 'multi-chunk transcript spawns N children with chunk-suffixed
idempotency keys' — both relied on result.details.children_submitted
which only the ok() path sets; the failed() path returns details: {}.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(mechanical): pin doctor init embedding model + clean non-default sources

Two fixes in the E2E Doctor Command describe block, both surfaced by
cross-file state pollution under the full sequential E2E run:

1. Pass --embedding-model openai:text-embedding-3-large to the init
   subprocess. Without the explicit flag, doctor inherits whatever the
   resolver picks from env keys (ZE if ZEROENTROPY_API_KEY is set,
   defaulting to zembed-1 at 1280d). The test's setupDB initialized
   schema at 1536d, so the dim mismatch fires
   embedding_width_consistency WARN, exiting doctor 1.

2. DELETE FROM sources WHERE id != 'default' in beforeAll. Prior E2E
   files leave non-default source rows (e.g. 'delta' from autopilot /
   sources tests). sync_freshness + cycle_freshness then FAIL on those
   orphans because they were never synced/cycled, exiting doctor 1.
   setupDB TRUNCATEs sources but schema.sql re-seeds 'default' via
   initSchema; this leaves only the canonical single-source brain
   the test expects.

Surfaced during fix-wave: warm-narwhal E2E gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(run-e2e): per-file connection flush + 180s outer timeout

Two cross-file isolation hardenings for the sequential E2E runner:

1. Terminate stale Postgres connections before each file. Without this,
   idle connections from the prior bun process's pool race with the
   next file's setupDB() TRUNCATE CASCADE, producing 'fixture pages
   disappear mid-test' failures. The terminate call is idempotent +
   ~50ms; first iteration is a no-op.

2. Hard outer timeout (180s per file) via gtimeout / timeout. bun's
   --timeout=60000 is per-test; if a PGLite WASM call hangs in
   beforeAll/afterAll (e.g. ingestion-roundtrip.test.ts wedging
   30+ minutes on macOS), --timeout never fires and the entire suite
   wedges. Outer SIGKILL lets the suite advance and the file is
   recorded as failed for triage. Falls through to bare bun if neither
   gtimeout nor timeout is on PATH.

Surfaced during fix-wave: warm-narwhal — 3 of 5 cross-file flakes
caught by the connection flush; ingestion-roundtrip 30-min wedge
caught by the outer timeout.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore: bump version and changelog (v0.41.3.0)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* docs: annotate synthesize.ts narrow prefix fix (v0.41.3.0)

CLAUDE.md gains the v0.41.3.0 note on src/core/cycle/synthesize.ts (narrow
anthropic: prefix at the queue.add boundary so resolveModel's bare ids
satisfy the subagent validator). llms-full.txt regenerated to match.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* chore: rebump v0.41.3.0 → v0.41.5.0 (queue drift; PR #1377 claimed .4.0)

Sibling fix-wave PR #1377 (garrytan/community-pr-wave) claimed v0.41.4.0
between my queue check (.3.0 was available) and PR creation. Re-bump to
the next available slot per workspace-aware allocator.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(cycle/synthesize): refuse empty brainDir + resolve relative paths

Pre-fix, runPhaseSynthesize accepted any brainDir string and passed it
to writeReversePages which does join(brainDir, '<slug>.md'). When
brainDir is '' or relative ('.' / './brain' / etc), join() produces a
relative path that writeFileSync resolves against cwd. Result: every
synthesize reverse-write spills into <cwd>/companies/<slug>.md,
<cwd>/people/<slug>.md, etc. instead of the intended brainDir tempdir.

Surfaced by the warm-narwhal wave when E2E test cleanup found orphan
synthesize pages (companies/novamind.md, people/sarah-chen.md,
meetings/2025-04-01-novamind-board-update.md) at the gbrain repo root
from a runCycle({brainDir: '.'}) chain that ran during morning E2E
execution.

Fix at the function entry, single location, all callers protected:
  1. Empty/whitespace brainDir → return failed(BRAINDIR_EMPTY) loud
     instead of silently resolving against cwd
  2. Relative brainDir → resolve(opts.brainDir) before any read/write
     can use it. opts.brainDir mutated so writeReversePages,
     writeSummaryPage, and every join() downstream see the absolute path

Regression test pins all 4 contracts:
  - empty string → fail(BRAINDIR_EMPTY)
  - whitespace-only → fail(BRAINDIR_EMPTY)
  - '.' → mutated to absolute on entry
  - already-absolute → unchanged

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(dream): resolve brainDir to absolute at CLI surface

Defense-in-depth for the synthesize-braindir spillage bug class. The
core fix lives in runPhaseSynthesize (commit 98222a0); this resolves
brainDir one layer earlier so the entire 9-phase runCycle gets the
absolute path, not just synthesize.

Two paths in resolveBrainDir get path.resolve():
  - explicit --dir argument (e.g., `gbrain dream --dir .`)
  - sync.repo_path config (in case it was ever stored relative)

resolveBrainDir already checked existsSync; resolve() just canonicalizes
before return. No behavior change for paths already absolute.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Matt Gunnin <mgunnin@esports.one>
Co-authored-by: Brandon Lipman <brandon@offdeck.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Jeremy Knows <jeremy@veefriends.com>
Co-authored-by: root <root@localhost>
Co-authored-by: orendi84 <orendigergo@gmail.com>
Co-authored-by: orendi84 <orendi84@users.noreply.github.com>
Co-authored-by: Garry Tan <garry@ycombinator.com>
garrytan-agents pushed a commit to garrytan-agents/gbrain that referenced this pull request Jun 13, 2026
…arrytan#1374)

* fix(recipes/openai): add max_batch_tokens to embedding touchpoint

OpenAI is the only recipe in the codebase without a max_batch_tokens cap.
Every other provider declares one (voyage=120K, azure-openai=8K, dashscope=8K,
zhipu=8K, minimax=4K). Without it, gbrain's recursive-halving safety net never
engages — batches dispatched purely on the char/4 estimator window will trip
OpenAI's 1M-token TPM ceiling on token-dense pages (Discord exports, JSON
dumps, code-heavy markdown), then retry storm and block the queue head.

Setting cap to 100_000:
- gbrain's batcher estimates tokens as chars/4
- Token-dense markdown+JSON tokenizes at ~chars/2.7
- 100K estimated = ~150K real worst-case, safely under OpenAI's 300K
  per-request hard cap and the 1M/min TPM ceiling
- Leaves headroom for recursive-halving on outlier chunks

(cherry picked from commit 40536aa)

* fix(ai/embed): recognize OpenAI 'maximum request size' error in isTokenLimitError

OpenAI's /v1/embeddings endpoint hard-caps a single request at 300k tokens
total across all input items. When the cap is exceeded it returns:

    Invalid 'input': maximum request size is 300000 tokens per request.

None of the three existing regexes in isTokenLimitError matched this
phrasing, so the recursive-halving safety net in embedSubBatch never
engaged for OpenAI. The same fat page (a token-dense markdown export,
e.g. a Discord transcript) would re-fail every pass, blocking forward
progress on the whole batch indefinitely.

Locally reproduced on a 31,129-chunk Postgres brain: 2,125 chunks
stuck at 'remaining' across 30+ embed --stale passes with retry
loops + sleep delays. Adding the two new patterns lets halving fire;
the same backlog cleared in one pass after the regex change (the
companion max_batch_tokens recipe fix from PR garrytan#924 caps fresh batches,
but existing oversize pages still need halving to recover).

Adds:
  - /maximum request size.*tokens/i  — OpenAI verbatim
  - /max.*tokens.*per.*request/i    — defensive against minor rewording

Tests:
  - Regression test for the exact OpenAI error string
  - Coverage for the generic 'max tokens per request' variant
  - All 25 tests in adaptive-embed-batch.test.ts pass

No behavior change for providers whose errors already matched.

(cherry picked from commit b834e84)

* fix(connection-manager): strip .<project-ref> suffix from username when deriving direct URL

`deriveDirectUrl()` correctly rewrites the host (`aws-0-us-east-1.pooler.supabase.com`
→ `db.abcxyz.supabase.co`) but preserves the full pooler-form username
(`postgres.abcxyz`). Supabase direct connections expect a bare `postgres`
username — Supavisor uses the `.<ref>` suffix for tenant routing, but it's
not a real database user. The auto-derived URL therefore fails to authenticate
even with the correct password:

    password authentication failed for user "postgres.abcxyz"

Strip the suffix to `postgres` whenever the project-ref was successfully
extracted (same condition that triggers the host rewrite). The non-pooler
username branch is unaffected — preserved as-is to keep the port-only
fallback case working.

Hit while exercising v0.30.1's dual-pool routing on a real Supabase brain;
the kill switch (`GBRAIN_DISABLE_DIRECT_POOL=1`) papered over it locally
but every Supabase user with a stock pooler URL would silently fall through
to single-pool until the user-supplied a `GBRAIN_DIRECT_DATABASE_URL`
override. With this fix, dual-pool works out of the box for the canonical
Supabase shape.

Test additions:
  - 1 case asserting bare `postgres:secret@` in the derived URL when
    project-ref is parseable from the pooler URL (the new behavior)
  - extends the existing "falls back to port-only" case with an
    assertion that non-pooler usernames are preserved (unchanged behavior)

`bun run typecheck` clean. `deriveDirectUrl` test block passes 5/5.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
(cherry picked from commit ddf2c6a)

* fix(init): --help should not mutate config or scan filesystem

`gbrain init --help` (and `-h`) currently fall through to the smart-detection
branch in runInit(), which scans cwd for .md files and on a directory with
1000+ files prints "Found ~1500 .md files. For a brain this size, Supabase
gives faster search..." then defaults to PGLite — calling saveConfig() and
overwriting any existing Postgres config with `engine: 'pglite' +
database_path: ~/.gbrain/brain.pglite`.

Confirmed in the wild: ran `gbrain init --help` from $HOME on a machine where
~/.gbrain/config.json pointed at a Supabase Postgres brain with 10K+ pages.
The config was silently flipped to PGLite. The Supabase data was intact, but
gbrain stopped pointing at it until the config was manually restored.

Root cause: cli.ts:62-69 only routes --help → printOpHelp() for shared-op
commands; CLI_ONLY commands (init, embed, etc.) fall through to their handler
with --help still in argv. None of them check for it.

Fix: add a --help/-h guard at the top of runInit() that prints help text and
returns. Help should never mutate state — Postel's robustness principle for
CLI tools.

Help text covers all flags (engine selection, AI provider options, thin-client
mode) so users running `--help` get the canonical list rather than having to
read the source.

A wider architectural fix — adding --help routing for all CLI_ONLY commands in
cli.ts — is plausible follow-up, but each CLI_ONLY command would still need
its own help text. This per-command pattern matches how shared ops handle it
via printOpHelp(). Init is the highest-stakes case because it's the only
CLI_ONLY command that calls saveConfig().

Smoke test: from a directory with 1500 .md files, with GBRAIN_HOME pointed at
a fresh tempdir:
  - Before fix: ~/.gbrain/config.json materialized with engine: 'pglite'
  - After fix: help text printed, no config dir created

`bun run typecheck` clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
(cherry picked from commit ed11fdd)

* test(frontmatter-install-hook): isolate hooksPath assertion from developer global config

The "installHook writes ... and sets core.hooksPath" test asserted
`git config --get core.hooksPath` returns `.githooks`, which falls
back to the global scope when local is unset. Developers who set
`core.hooksPath` globally (common with dotfiles managers pointing at
~/.config/git/hooks) saw a deterministic FAIL because installHook
intentionally respects an existing global value and skips writing
the local one — exactly the documented contract.

Fix: read via `git config --local --get core.hooksPath` (scope-locked)
and branch the assertion on whether a global is already set. Both
clean-CI (local should be '.githooks') and developer-with-global
(local should be empty; installHook correctly didn't clobber) now
pass deterministically.

No API change. installHook behavior is unchanged.

Verified locally with the affected test passing under
`GIT_CONFIG_GLOBAL=~/.gitconfig` carrying `core.hooksPath=...`.

(cherry picked from commit 0e4da2c)

* fix: guard against missing 'intent' field in routing-eval fixtures

Two defensive fixes:

1. normalizeText(): return empty string on null/undefined input instead
   of crashing with 'undefined is not an object (evaluating s.toLowerCase)'

2. loadRoutingFixtures(): validate that parsed fixture has 'intent' as a
   string before adding to fixtures array. Fixtures with wrong field
   names (e.g. 'input' instead of 'intent') are now reported as
   malformed with a helpful error message listing the actual keys found.

Root cause: a skill's routing-eval.jsonl used {"input": ...} instead
of {"intent": ...}. The JSON parsed fine but the cast to
RoutingFixture was unchecked, so fixture.intent was undefined.
normalizeText(undefined) then crashed. This made 'gbrain doctor'
completely unusable.

(cherry picked from commit b142bbd)

* fix(test): isolate HOME in run-e2e.sh to stop config corruption

Replaces garrytan#517 (re-ported fresh against current scripts/run-e2e.sh after
v0.23.1 rewrote the script — original cherry-pick would not apply).

E2E tests call setupDB which writes $HOME/.gbrain/config.json pointing at
the docker test container. When the container tears down, the user's real
autopilot daemon wedges trying to connect to a vanished postgres. Three
operators hit this within 16 days before the original PR filed.

Fix: wrapper exports HOME + GBRAIN_HOME to a mktemp tmpdir BEFORE bun
starts so config writes land in the tmpdir, with a post-run breach
detector that compares md5 of the user's real config against pre-run.
Both env vars required: loadConfig/saveConfig resolve via HOME while
configPath honors GBRAIN_HOME. HOME set before bun starts because
os.homedir() caches at first call.

Test seam: test/gbrain-home-isolation.test.ts updated to assert against
homedir() === configDir() when GBRAIN_HOME unset (correct under the
safety wrapper itself) instead of the prior "not /tmp/" sentinel.

Revert path: git revert <this-sha> if test:e2e regresses on master.

Co-Authored-By: orendi84 <orendi84@users.noreply.github.com>

* test(dream-cycle): add schema-suggest to EXPECTED_PHASES

v0.40.7.0 Schema Cathedral v3 added the 'schema-suggest' phase between
'orphans' and 'purge' in ALL_PHASES, but the E2E phase-order test was
not updated to match. ALL_PHASES vs EXPECTED_PHASES diverged and the
shape-pin test failed every run on master.

Surfaced during fix-wave: warm-narwhal E2E gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(autopilot-fanout): use relative timestamp inside freshness window

The 'end-to-end: updateSourceConfig persists timestamp visible to next
listAllSources' test pinned last_full_cycle_at to a hardcoded
'2026-05-22T15:00:00.000Z'. The 60-minute freshness window passed
within ~1 hour of write — every run after the deadline classified the
source as stale and dispatched it, breaking the test's
.skippedFresh expectation.

Switch to Date.now() - 30min relative timestamp (mirrors the prior
'source with last_full_cycle_at < 60min ago is skipped by gate' test).

Surfaced during fix-wave: warm-narwhal E2E gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(fresh-install-pglite): unset other provider keys in beforeEach

init.ts:455 fails loud when multiple embedding providers are env-ready
in non-TTY mode. The test sets ZEROENTROPY_API_KEY then runs init,
but developer machines commonly have OPENAI_API_KEY + VOYAGE_API_KEY +
ZEROENTROPY_API_KEY all set, so init sees 3 providers and exits 1.

Save+unset OPENAI_API_KEY + VOYAGE_API_KEY in beforeEach, restore in
afterEach. Now only ZE is env-ready, init picks it, schema sized to
zembed-1's 1280d as the test expects.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(voyage-multimodal): switch fixture from AVIF to PNG

Voyage's /multimodalembeddings endpoint rejects AVIF as of 2026-05
with 'Please provide a valid base64-encoded image'. The prior comment
('AVIF is fine for an embed call') held at v0.27.x and regressed
silently on the provider side.

Add test/fixtures/images/tiny.png (16x16 RGB PNG, 1307 bytes generated
via sips from the macOS default wallpaper). PNG is universally
accepted by Voyage and other multimodal providers.

Surfaced during fix-wave: warm-narwhal E2E gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(cycle/synthesize): prefix bare anthropic model ids before queue.add

queue.add's subagent capability validator (classifyCapabilities →
resolveRecipe) requires provider:model format and rejects bare ids
with 'unknown provider'. resolveModel returns the bare id from
TIER_DEFAULTS / DEFAULT_ALIASES (e.g. 'claude-sonnet-4-6'), which the
validator then rejects, dropping the synthesize phase to status:fail
with SYNTH_PHASE_FAIL.

Narrow fix at the call site: if config.model has no colon AND starts
with 'claude-', prefix 'anthropic:'. Other providers must already
declare a colon. Avoids changing TIER_DEFAULTS / DEFAULT_ALIASES
constant shapes, which would ripple across every resolveModel caller.

Surfaced by dream-synthesize-chunking E2E during fix-wave: warm-narwhal.
Affected tests: 'single-chunk transcript uses legacy idempotency key'
and 'multi-chunk transcript spawns N children with chunk-suffixed
idempotency keys' — both relied on result.details.children_submitted
which only the ok() path sets; the failed() path returns details: {}.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(mechanical): pin doctor init embedding model + clean non-default sources

Two fixes in the E2E Doctor Command describe block, both surfaced by
cross-file state pollution under the full sequential E2E run:

1. Pass --embedding-model openai:text-embedding-3-large to the init
   subprocess. Without the explicit flag, doctor inherits whatever the
   resolver picks from env keys (ZE if ZEROENTROPY_API_KEY is set,
   defaulting to zembed-1 at 1280d). The test's setupDB initialized
   schema at 1536d, so the dim mismatch fires
   embedding_width_consistency WARN, exiting doctor 1.

2. DELETE FROM sources WHERE id != 'default' in beforeAll. Prior E2E
   files leave non-default source rows (e.g. 'delta' from autopilot /
   sources tests). sync_freshness + cycle_freshness then FAIL on those
   orphans because they were never synced/cycled, exiting doctor 1.
   setupDB TRUNCATEs sources but schema.sql re-seeds 'default' via
   initSchema; this leaves only the canonical single-source brain
   the test expects.

Surfaced during fix-wave: warm-narwhal E2E gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(run-e2e): per-file connection flush + 180s outer timeout

Two cross-file isolation hardenings for the sequential E2E runner:

1. Terminate stale Postgres connections before each file. Without this,
   idle connections from the prior bun process's pool race with the
   next file's setupDB() TRUNCATE CASCADE, producing 'fixture pages
   disappear mid-test' failures. The terminate call is idempotent +
   ~50ms; first iteration is a no-op.

2. Hard outer timeout (180s per file) via gtimeout / timeout. bun's
   --timeout=60000 is per-test; if a PGLite WASM call hangs in
   beforeAll/afterAll (e.g. ingestion-roundtrip.test.ts wedging
   30+ minutes on macOS), --timeout never fires and the entire suite
   wedges. Outer SIGKILL lets the suite advance and the file is
   recorded as failed for triage. Falls through to bare bun if neither
   gtimeout nor timeout is on PATH.

Surfaced during fix-wave: warm-narwhal — 3 of 5 cross-file flakes
caught by the connection flush; ingestion-roundtrip 30-min wedge
caught by the outer timeout.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore: bump version and changelog (v0.41.3.0)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* docs: annotate synthesize.ts narrow prefix fix (v0.41.3.0)

CLAUDE.md gains the v0.41.3.0 note on src/core/cycle/synthesize.ts (narrow
anthropic: prefix at the queue.add boundary so resolveModel's bare ids
satisfy the subagent validator). llms-full.txt regenerated to match.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* chore: rebump v0.41.3.0 → v0.41.5.0 (queue drift; PR garrytan#1377 claimed .4.0)

Sibling fix-wave PR garrytan#1377 (garrytan/community-pr-wave) claimed v0.41.4.0
between my queue check (.3.0 was available) and PR creation. Re-bump to
the next available slot per workspace-aware allocator.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(cycle/synthesize): refuse empty brainDir + resolve relative paths

Pre-fix, runPhaseSynthesize accepted any brainDir string and passed it
to writeReversePages which does join(brainDir, '<slug>.md'). When
brainDir is '' or relative ('.' / './brain' / etc), join() produces a
relative path that writeFileSync resolves against cwd. Result: every
synthesize reverse-write spills into <cwd>/companies/<slug>.md,
<cwd>/people/<slug>.md, etc. instead of the intended brainDir tempdir.

Surfaced by the warm-narwhal wave when E2E test cleanup found orphan
synthesize pages (companies/novamind.md, people/sarah-chen.md,
meetings/2025-04-01-novamind-board-update.md) at the gbrain repo root
from a runCycle({brainDir: '.'}) chain that ran during morning E2E
execution.

Fix at the function entry, single location, all callers protected:
  1. Empty/whitespace brainDir → return failed(BRAINDIR_EMPTY) loud
     instead of silently resolving against cwd
  2. Relative brainDir → resolve(opts.brainDir) before any read/write
     can use it. opts.brainDir mutated so writeReversePages,
     writeSummaryPage, and every join() downstream see the absolute path

Regression test pins all 4 contracts:
  - empty string → fail(BRAINDIR_EMPTY)
  - whitespace-only → fail(BRAINDIR_EMPTY)
  - '.' → mutated to absolute on entry
  - already-absolute → unchanged

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(dream): resolve brainDir to absolute at CLI surface

Defense-in-depth for the synthesize-braindir spillage bug class. The
core fix lives in runPhaseSynthesize (commit 98222a0); this resolves
brainDir one layer earlier so the entire 9-phase runCycle gets the
absolute path, not just synthesize.

Two paths in resolveBrainDir get path.resolve():
  - explicit --dir argument (e.g., `gbrain dream --dir .`)
  - sync.repo_path config (in case it was ever stored relative)

resolveBrainDir already checked existsSync; resolve() just canonicalizes
before return. No behavior change for paths already absolute.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Matt Gunnin <mgunnin@esports.one>
Co-authored-by: Brandon Lipman <brandon@offdeck.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Jeremy Knows <jeremy@veefriends.com>
Co-authored-by: root <root@localhost>
Co-authored-by: orendi84 <orendigergo@gmail.com>
Co-authored-by: orendi84 <orendi84@users.noreply.github.com>
Co-authored-by: Garry Tan <garry@ycombinator.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants