mirror of
https://github.com/garrytan/gbrain.git
synced 2026-08-14 08:53:22 +00:00
* feat(bootstrap): TTY DX exploration harness + Krug onboarding fix wave Add a real-PTY exploration harness and land 16 verified "Don't Make Me Think" fixes on the paste-in install experience for Claude Code and Codex. Harness: - test/helpers/tty-harness.ts — spawns any CLI (gbrain/claude/codex) under a real pseudo-terminal (Bun terminal: spawn), timestamps every output burst, and turns silence windows into a measurable stall report. Hermetic; pure helpers unit-tested in test/tty-harness.test.ts. - scripts/dx-explore.ts — drives the fresh-user funnel (help / init / real claude-install / real codex-install / manual drive mode), writing transcripts to .context/dx-runs/ (gitignored). Fixes (all adversarially verified against the code first): - Keyless bare `gbrain init` completes in keyless mode instead of exit 1; multi-key non-TTY auto-picks the canonical default; typo stays fail-loud. - Provider picker probe-gates ollama (daemon-up != model-pulled) and offers an explicit "continue keyless" option that is the bare-Enter default. - Fresh-brain init prints one schema-setup line instead of ~240 migration names (GBRAIN_MIGRATE_VERBOSE=1 restores detail). - Init epilogue: memory-verbs funnel is last-on-screen; skills advisory compacted for init; Mod Status trimmed. - PGLite live-serve lock error names the fix (close the agent session). - Mode-picker banner interpolates the applied mode; expansion-key gate is Anthropic/OpenAI/Google, not OpenAI-only. - Missing `claude` binary skips MCP but still installs hooks; honest copy. - Foreign MCP-registration removal targets the conflicting scope and fails loud if it does not land. - Upgrade marker compares the running binary to latest and self-spawns via execPath, so a current/newer binary no longer nags from a stale cache. - interview --set/--skip after --confirm warns it voided the confirmation. - init --help matches behavior; init --supabase fails loud on non-TTY. - Provider capabilities attributed per provider across README / runbook / questions bank / bootstrap.md. - First-run tour: restart-first, prompt 3 true on day one, withheld on FAIL; README gives Codex the same scripted magic moment. - Empty-brain "0 takes" onboard nudge suppressed. - Broken settings.local.json aborts the hooks write fail-closed instead of silently dropping the user's permissions. Regenerated cli-flag-registry.generated.ts and llms-full.txt. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(bootstrap): second DX polish wave — clean the success screen + honest copy (F17-F21) Follow-up to the DX fix wave, closing the top-5 remaining gaps the scorecard flagged (all human-facing polish, not survival): F17 — machine markers no longer leak to humans: - verify report drops the `[D3.6]` plan-tag from the first_run_tour detail. - the raw `UPGRADE_AVAILABLE <cur> <latest>` marker line prints ONLY on a non-TTY stderr (parsers still get it); an interactive human sees just the "gbrain X -> Y available" sentence. - per-migration "what changed" notices (v123/v124, incl. the #2704 ref) are suppressed on a FRESH-install replay via a module quiet flag; upgrades still narrate. (GBRAIN_MIGRATE_VERBOSE=1 restores them.) F18 — one obvious next action on the init success screen: the memory-verbs demo is the single "→ Do this next" hero, last on screen; import/migrate/doctor collapse into one terse "More:" footer; the graph block only shows for a non-empty brain. F19 — README "moment it clicks" is now the genuine cross-session brain round-trip (remember → restart → recall), explicitly distinguished from the identity-file recall, on both the Codex and Claude Code paths. F20 — the compact init skills advisory is human-voiced (no `[AGENT]` stage-direction on the human-facing success screen; the mode-picker's agent-directed block stays gated to the non-TTY channel). F21 — time promise reconciled: headline is ~15 min (personal-agent path) / ~30 min (always-on OpenClaw/Hermes); the runbook's search-mode line no longer claims "balanced" when keyless applies "conservative". README hooks copy says "on by default, with an opt-out" to match the runbook. Regenerated llms-full.txt. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(bootstrap): address two-model adversarial review of the DX wave Fixes the regressions the 5-specialist + red-team + Claude/Codex adversarial pass found in the F1–F21 changes, each with a test: - Keyless upgrade hint pointed at `config set embedding_model`, which config.ts hard-refuses as a schema-sizing no-op — now names the working re-init recipe (`gbrain init --force --pglite --embedding-model <id>`), zero-key AND multi-key paths. - Multi-key TTY picker offered "continue keyless" but the caller aborted on it — now honors keyless like the zero-key path. - Detached update-refresh spawn used a `/gbrain$/` basename check that misfires for a renamed/official-named compiled binary (`gbrain-darwin-arm64`) and prepends the /$bunfs entrypoint — now detects dev-vs-compiled by the runtime basename (bun|node) so the refresh always runs. - `bootstrap status` reported the wire phase "done" on a hooks-only receipt (host CLI missing at wire time) — now "partial" with a re-run hint, so a resuming agent doesn't trust a false complete. - Post-repair MCP mismatch re-verifies and aborts instead of blessing a registration a racing writer may have re-claimed. - probeOpenAICompat's abort timer now spans the body read (was cleared before it), so a stalled `/v1/models` body can't hang init past the 1s cap. - Centralized the 4-copy stale-cache upgrade predicate into `pendingUpgradeVersion`; UPGRADE_AVAILABLE gains a GBRAIN_FORCE_UPGRADE_MARKER override for PTY-based agent harnesses. - Mode picker's expansion-key gate adds GEMINI_API_KEY; picker prompt is article-aware ("an embedding" / "a chat"); dead `!brainEmpty` clause removed; migrate.ts try/finally widened + stamp failures named in quiet mode. - DX harness: credential copies scrubbed even on SIGINT/interrupt (+chmod 600), child process TREE reaped on teardown, advisory made fail-open, KEY_MAP typed as a literal union. New tests: migrate quiet-replay, self-upgrade pending predicate + negative cache cases, bootstrap 127/scoped-remove/broken-settings dispatch, interview invalidation flag, verify tour-withheld-on-FAIL, init keyless/supabase/multi-key, init-nudge branches, ai-probes model parsing. Regenerated flag registry + template-repo. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * v0.45.8.0 fix(bootstrap): onboarding DX polish wave (F17-F21) + review fixes DX fix wave on the paste-in install/first-run experience for Claude Code and Codex, driven by a new real-PTY exploration harness. Keyless init completes instead of erroring, the migration wall collapses to one line, the success screen leads with one action, and the "magic moment" copy points at the genuine cross-session round-trip. Full detail in CHANGELOG. Version trio + openclaw manifest + runbook stamp bumped to 0.45.8.0; CHANGELOG release entry; TODOS onboarding-DX follow-ups filed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bootstrap): OOBE hand-off — you own the brain, cold-start is skill #1 A working install now ends by making the two facts that matter actually land: - `gbrain bootstrap verify` prints (and returns as `handoff` in --json) an ownership block — the actual private-repo URL with what owning it means (read it, `gbrain bootstrap attach` on machine two, delete it and the brain is gone), or the local-only variant pointing at `gbrain bootstrap repo` — followed by the ONE next action: run the cold-start skill (Gmail/calendar/ contacts via ClawVisor, an OAuth vault so the agent never holds raw tokens; or offline archives), one consented phase at a time. Withheld on FAIL like the tour; shape stays unconditional for machine consumers. - cold-start ships in the downstream bundle (61 skills): its plugin exclusion ("host onboarding flow") predated the v0.45 personal-agent bootstrap and is deliberately reversed — the paste-in audience is exactly who day-one onboarding is for. It now LEADS the recommended set (ahead of book-mirror: every flagship skill only becomes magical once the brain holds the user's real life). - New drift guard: every recommended slug must be scaffoldable from the plugin bundle — recommended-but-unscaffoldable is a dead-end CTA and now fails the suite. - Runbook Hand off rewritten around the two must-land facts + the on-the-spot cold-start offer; README's Codex and Claude Code paths carry the same two follow-ups. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * v0.45.10.0 feat(bootstrap): the OOBE hand-off release Version trio + runbook stamp + template tree to 0.45.10.0; CHANGELOG entry; llms bundles regenerated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(ci): stop memory-verbs-conformance leaking a fake-keyed gateway into shard-mates The deterministic-embedder helper configures the MODULE-GLOBAL gateway with a fake OpenAI key; the file's afterAll never reset it. The bunfig preload's per-test restore only fires when the gateway is UNCONFIGURED, so the fake-keyed config persisted for every later file in the shard process — turn-context's corpus writes then embedded against real OpenAI and 401'd (CI shard-8 failure; shard re-binning from this branch's new test files exposed it). Fix both sides: conformance's afterAll now resetGateway()s back to the preload baseline and nulls both test transports; turn-context's beforeAll does the same defensively so it stays hermetic regardless of shard composition. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
405 lines
18 KiB
TypeScript
405 lines
18 KiB
TypeScript
/**
|
|
* bootstrap/hooks.ts writers + host-specs (agent-bootstrap G5, CX2-17, G1,
|
|
* CX-P1.4, ENG-7): structural JSON merge preserves foreign hooks/permissions,
|
|
* marker dedupe makes re-runs idempotent, broken-JSON originals are backed up
|
|
* (never silently destroyed), removal strips ONLY marker-carrying entries,
|
|
* and the MCP registration helpers build argv only.
|
|
*/
|
|
import { describe, test, expect, afterEach } from 'bun:test';
|
|
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
import {
|
|
buildClaudeHookCommand,
|
|
claudeCommittedSettingsPath,
|
|
claudeSettingsPath,
|
|
registerClaudeMcp,
|
|
registerCodexMcp,
|
|
removeClaudeHooks,
|
|
writeClaudeHooks,
|
|
writeCommittedClaudeHooks,
|
|
} from '../src/core/bootstrap/hooks.ts';
|
|
import {
|
|
CLAUDE_CODE_SPEC_ID,
|
|
CLAUDE_HOOK_DEFAULT_TIMEOUT_SECS,
|
|
CLAUDE_HOOK_EVENTS,
|
|
CODEX_SPEC_ID,
|
|
GBRAIN_HOOK_MARKER_KEY,
|
|
GBRAIN_HOOK_MARKER_VALUE,
|
|
TARGETS,
|
|
} from '../src/core/bootstrap/host-specs.ts';
|
|
|
|
const BIN = '/opt/gbrain/bin/gbrain';
|
|
const ENV = { GBRAIN_SOURCE: 'workspace' };
|
|
|
|
let tmp: string | null = null;
|
|
function ws(): string {
|
|
tmp = mkdtempSync(join(tmpdir(), 'gb-hooks-'));
|
|
return tmp;
|
|
}
|
|
afterEach(() => {
|
|
if (tmp) rmSync(tmp, { recursive: true, force: true });
|
|
tmp = null;
|
|
});
|
|
|
|
function readSettings(dir: string): Record<string, unknown> {
|
|
return JSON.parse(readFileSync(claudeSettingsPath(dir), 'utf8'));
|
|
}
|
|
|
|
type Group = { matcher?: string; hooks: Array<Record<string, unknown>> };
|
|
|
|
function markerEntries(settings: Record<string, unknown>, event: string): Array<Record<string, unknown>> {
|
|
const groups = ((settings.hooks as Record<string, unknown>)?.[event] ?? []) as Group[];
|
|
return groups.flatMap((g) => (g.hooks ?? []).filter((h) => h[GBRAIN_HOOK_MARKER_KEY] === GBRAIN_HOOK_MARKER_VALUE));
|
|
}
|
|
|
|
describe('host-specs [ENG-7]', () => {
|
|
test('both targets carry the dated spec-target shape', () => {
|
|
for (const id of [CLAUDE_CODE_SPEC_ID, CODEX_SPEC_ID]) {
|
|
const t = TARGETS[id];
|
|
expect(t).toBeDefined();
|
|
expect(t.id).toBe(id);
|
|
expect(['verified', 'provisional']).toContain(t.status);
|
|
expect(t.verifiedAt).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
expect(t.references.length).toBeGreaterThan(0);
|
|
expect(t.note.length).toBeGreaterThan(40);
|
|
}
|
|
expect(TARGETS[CLAUDE_CODE_SPEC_ID].status).toBe('verified');
|
|
expect(TARGETS[CLAUDE_CODE_SPEC_ID].references.join(' ')).toContain('code.claude.com');
|
|
// Codex mcp-add shape is docs-derived, not live-host-verified.
|
|
expect(TARGETS[CODEX_SPEC_ID].status).toBe('provisional');
|
|
// The v1 "no TOML writer" decision is recorded where the format lives.
|
|
expect(TARGETS[CODEX_SPEC_ID].note).toContain('TOML');
|
|
});
|
|
});
|
|
|
|
describe('writeClaudeHooks [G5, CX2-17]', () => {
|
|
test('fresh workspace: all five events wired with marker + env + timeout', () => {
|
|
const dir = ws();
|
|
const res = writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
expect(res.settingsPath).toBe(claudeSettingsPath(dir));
|
|
expect(res.installed.map((i) => i.event).sort()).toEqual([...CLAUDE_HOOK_EVENTS].sort());
|
|
expect(res.backupPath).toBeNull();
|
|
expect(res.brokenBackupPath).toBeNull();
|
|
|
|
const settings = readSettings(dir);
|
|
for (const event of CLAUDE_HOOK_EVENTS) {
|
|
const ours = markerEntries(settings, event);
|
|
expect(ours).toHaveLength(1);
|
|
expect(ours[0].type).toBe('command');
|
|
expect(ours[0].command).toContain(BIN);
|
|
expect(ours[0].command).toContain('GBRAIN_SOURCE=workspace');
|
|
expect(ours[0].timeout).toBe(CLAUDE_HOOK_DEFAULT_TIMEOUT_SECS[event]);
|
|
}
|
|
// Event → subcommand mapping.
|
|
expect(markerEntries(settings, 'UserPromptSubmit')[0].command).toContain('hook user-prompt');
|
|
expect(markerEntries(settings, 'SessionStart')[0].command).toContain('hook session-start');
|
|
expect(markerEntries(settings, 'Stop')[0].command).toContain('hook stop');
|
|
expect(markerEntries(settings, 'SessionEnd')[0].command).toContain('hook session-end');
|
|
// v0.45.7 ambient recall: PreCompact banks standing entities pre-compaction.
|
|
expect(markerEntries(settings, 'PreCompact')[0].command).toContain('hook compact');
|
|
});
|
|
|
|
test('timeoutSecs override + GBRAIN_HOME env embedding', () => {
|
|
const dir = ws();
|
|
writeClaudeHooks(dir, {
|
|
gbrainBin: BIN,
|
|
env: { GBRAIN_SOURCE: 'src-a', GBRAIN_HOME: '/tmp/iso home' },
|
|
timeoutSecs: { SessionEnd: 120 },
|
|
});
|
|
const settings = readSettings(dir);
|
|
const cmd = markerEntries(settings, 'SessionEnd')[0];
|
|
expect(cmd.timeout).toBe(120);
|
|
// Space-carrying value gets shell-quoted, not broken.
|
|
expect(cmd.command).toContain("'GBRAIN_HOME=/tmp/iso home'");
|
|
expect(markerEntries(settings, 'Stop')[0].timeout).toBe(CLAUDE_HOOK_DEFAULT_TIMEOUT_SECS.Stop);
|
|
});
|
|
|
|
test('foreign hooks + permissions survive; ours appended', () => {
|
|
const dir = ws();
|
|
const foreign = {
|
|
permissions: { allow: ['Bash(npm run *)'] },
|
|
otherTopLevel: { keep: true },
|
|
hooks: {
|
|
UserPromptSubmit: [
|
|
{ hooks: [{ type: 'command', command: 'my-custom-hook.sh', timeout: 9 }] },
|
|
],
|
|
PreToolUse: [
|
|
{ matcher: 'Bash', hooks: [{ type: 'command', command: 'guard.sh' }] },
|
|
],
|
|
},
|
|
};
|
|
mkdirSync(join(dir, '.claude'), { recursive: true });
|
|
writeFileSync(claudeSettingsPath(dir), JSON.stringify(foreign, null, 2));
|
|
|
|
writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
const settings = readSettings(dir);
|
|
expect(settings.permissions).toEqual(foreign.permissions);
|
|
expect(settings.otherTopLevel).toEqual(foreign.otherTopLevel);
|
|
const hooks = settings.hooks as Record<string, Group[]>;
|
|
// Foreign PreToolUse untouched.
|
|
expect(hooks.PreToolUse).toEqual(foreign.hooks.PreToolUse as Group[]);
|
|
// Foreign UserPromptSubmit entry intact, ours appended after it.
|
|
const upGroups = hooks.UserPromptSubmit;
|
|
expect(upGroups[0].hooks[0].command).toBe('my-custom-hook.sh');
|
|
expect(markerEntries(settings, 'UserPromptSubmit')).toHaveLength(1);
|
|
});
|
|
|
|
test('marker dedupe: running twice leaves exactly one entry per event', () => {
|
|
const dir = ws();
|
|
writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
const second = writeClaudeHooks(dir, { gbrainBin: '/new/path/gbrain', env: { GBRAIN_SOURCE: 'renamed' } });
|
|
expect(second.removedPrior).toBe(CLAUDE_HOOK_EVENTS.length);
|
|
const settings = readSettings(dir);
|
|
for (const event of CLAUDE_HOOK_EVENTS) {
|
|
const ours = markerEntries(settings, event);
|
|
expect(ours).toHaveLength(1);
|
|
// Dedupe keys on the MARKER, so the refreshed command wins.
|
|
expect(ours[0].command).toContain('/new/path/gbrain');
|
|
expect(ours[0].command).toContain('GBRAIN_SOURCE=renamed');
|
|
}
|
|
});
|
|
|
|
test('.bak backup written on overwrite of an existing file', () => {
|
|
const dir = ws();
|
|
mkdirSync(join(dir, '.claude'), { recursive: true });
|
|
writeFileSync(claudeSettingsPath(dir), JSON.stringify({ permissions: { allow: ['X'] } }));
|
|
const res = writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
expect(res.backupPath).toBe(`${claudeSettingsPath(dir)}.bak`);
|
|
const bak = JSON.parse(readFileSync(res.backupPath!, 'utf8'));
|
|
expect(bak).toEqual({ permissions: { allow: ['X'] } });
|
|
});
|
|
|
|
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 });
|
|
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)', () => {
|
|
const dir = ws();
|
|
expect(() => writeClaudeHooks(dir, { gbrainBin: 'gbrain', env: ENV })).toThrow(/absolute/);
|
|
});
|
|
|
|
test('control chars in env values refused', () => {
|
|
const dir = ws();
|
|
expect(() =>
|
|
writeClaudeHooks(dir, { gbrainBin: BIN, env: { GBRAIN_SOURCE: 'a\nb' } }),
|
|
).toThrow(/control characters/);
|
|
});
|
|
});
|
|
|
|
describe('removeClaudeHooks [G5]', () => {
|
|
test('removes only marker entries; foreign hooks + permissions intact', () => {
|
|
const dir = ws();
|
|
mkdirSync(join(dir, '.claude'), { recursive: true });
|
|
writeFileSync(
|
|
claudeSettingsPath(dir),
|
|
JSON.stringify({
|
|
permissions: { allow: ['Bash(ls *)'] },
|
|
hooks: {
|
|
UserPromptSubmit: [{ hooks: [{ type: 'command', command: 'foreign.sh' }] }],
|
|
},
|
|
}),
|
|
);
|
|
writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
const res = removeClaudeHooks(dir);
|
|
expect(res.removed).toBe(CLAUDE_HOOK_EVENTS.length);
|
|
|
|
const settings = readSettings(dir);
|
|
expect(settings.permissions).toEqual({ allow: ['Bash(ls *)'] });
|
|
const hooks = settings.hooks as Record<string, Group[]>;
|
|
// Foreign entry survives; our groups (and event keys we emptied) are gone.
|
|
expect(hooks.UserPromptSubmit[0].hooks[0].command).toBe('foreign.sh');
|
|
expect(markerEntries(settings, 'UserPromptSubmit')).toHaveLength(0);
|
|
expect(hooks.SessionStart).toBeUndefined();
|
|
expect(hooks.Stop).toBeUndefined();
|
|
expect(hooks.SessionEnd).toBeUndefined();
|
|
});
|
|
|
|
test('remove on a purely-ours file drops the hooks key entirely', () => {
|
|
const dir = ws();
|
|
writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
removeClaudeHooks(dir);
|
|
const settings = readSettings(dir);
|
|
expect(settings.hooks).toBeUndefined();
|
|
});
|
|
|
|
test('absent file: no-op, nothing created', () => {
|
|
const dir = ws();
|
|
const res = removeClaudeHooks(dir);
|
|
expect(res.removed).toBe(0);
|
|
expect(existsSync(claudeSettingsPath(dir))).toBe(false);
|
|
});
|
|
|
|
test('broken JSON: left untouched with a note (removal never destroys)', () => {
|
|
const dir = ws();
|
|
mkdirSync(join(dir, '.claude'), { recursive: true });
|
|
writeFileSync(claudeSettingsPath(dir), '{ broken !!!');
|
|
const res = removeClaudeHooks(dir);
|
|
expect(res.removed).toBe(0);
|
|
expect(readFileSync(claudeSettingsPath(dir), 'utf8')).toBe('{ broken !!!');
|
|
expect(res.notes.join(' ')).toContain('left untouched');
|
|
});
|
|
});
|
|
|
|
describe('MCP registration argv builders [G1, CX-P1.4]', () => {
|
|
test('registerClaudeMcp: stdio shape with scope + source env; serve pinned to --surface full', () => {
|
|
const cmds = registerClaudeMcp({ gbrainBin: BIN, scope: 'project', sourceId: 'workspace' });
|
|
expect(cmds).toEqual([
|
|
['claude', 'mcp', 'add', 'gbrain', '--scope', 'project', '-e', 'GBRAIN_SOURCE=workspace', '--', BIN, 'serve', '--surface', 'full'],
|
|
]);
|
|
});
|
|
|
|
test('registerClaudeMcp: user scope + isolated GBRAIN_HOME', () => {
|
|
const cmds = registerClaudeMcp({
|
|
name: 'mybrain', gbrainBin: BIN, scope: 'user', sourceId: 's1', gbrainHome: '/ws',
|
|
});
|
|
expect(cmds[0]).toEqual([
|
|
'claude', 'mcp', 'add', 'mybrain', '--scope', 'user',
|
|
'-e', 'GBRAIN_SOURCE=s1', '-e', 'GBRAIN_HOME=/ws', '--', BIN, 'serve', '--surface', 'full',
|
|
]);
|
|
});
|
|
|
|
test('registerCodexMcp: --env shape, no scope flag; serve pinned to --surface full', () => {
|
|
const cmds = registerCodexMcp({ gbrainBin: BIN, sourceId: 'workspace', gbrainHome: '/ws' });
|
|
expect(cmds).toEqual([
|
|
['codex', 'mcp', 'add', 'gbrain', '--env', 'GBRAIN_SOURCE=workspace', '--env', 'GBRAIN_HOME=/ws', '--', BIN, 'serve', '--surface', 'full'],
|
|
]);
|
|
});
|
|
|
|
test('builders refuse relative binaries', () => {
|
|
expect(() => registerClaudeMcp({ gbrainBin: 'gbrain', scope: 'project', sourceId: 's' })).toThrow(/absolute/);
|
|
expect(() => registerCodexMcp({ gbrainBin: './gbrain', sourceId: 's' })).toThrow(/absolute/);
|
|
});
|
|
});
|
|
|
|
describe('buildClaudeHookCommand', () => {
|
|
test('quotes unsafe values; bare env assignment stays bare', () => {
|
|
const cmd = buildClaudeHookCommand(BIN, 'UserPromptSubmit', {
|
|
GBRAIN_SOURCE: 'plain-slug',
|
|
GBRAIN_HOME: '/has space/dir',
|
|
});
|
|
expect(cmd).toBe(
|
|
`env GBRAIN_SOURCE=plain-slug 'GBRAIN_HOME=/has space/dir' ${BIN} hook user-prompt`,
|
|
);
|
|
});
|
|
});
|
|
|
|
// ── committed hook carrier [D12] ────────────────────────────────────────────
|
|
//
|
|
// Cloud sessions clone fresh and snapshot hook config at session start — only
|
|
// the repo-COMMITTED .claude/settings.json exists there. These tests pin the
|
|
// portable command shape (PATH-resolved, fail-open, NO absolute paths) and
|
|
// the dedupe invariant (one event never fires from both carriers).
|
|
|
|
describe('writeCommittedClaudeHooks [D12]', () => {
|
|
test('portable commands: PATH-resolved gbrain, fail-open guard, marker present, no absolute binary path', () => {
|
|
const dir = ws();
|
|
const r = writeCommittedClaudeHooks(dir, { env: ENV });
|
|
expect(r.settingsPath).toBe(claudeCommittedSettingsPath(dir));
|
|
expect(r.installed).toHaveLength(CLAUDE_HOOK_EVENTS.length);
|
|
const parsed = JSON.parse(readFileSync(r.settingsPath, 'utf8')) as {
|
|
hooks: Record<string, Array<{ hooks: Array<Record<string, unknown>> }>>;
|
|
};
|
|
for (const event of CLAUDE_HOOK_EVENTS) {
|
|
const entry = parsed.hooks[event]![0]!.hooks[0]!;
|
|
const cmd = entry.command as string;
|
|
expect(cmd).toContain('command -v gbrain');
|
|
expect(cmd).toContain('|| exit 0'); // fail-open where gbrain is absent
|
|
expect(cmd).toContain('GBRAIN_SOURCE=workspace');
|
|
expect(cmd).not.toContain(BIN); // never a machine path
|
|
expect(cmd).not.toContain('/opt/');
|
|
expect(entry[GBRAIN_HOOK_MARKER_KEY]).toBe(GBRAIN_HOOK_MARKER_VALUE);
|
|
}
|
|
});
|
|
|
|
test('dedupe: committed carrier strips existing LOCAL entries so nothing double-fires', () => {
|
|
const dir = ws();
|
|
writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
expect(readFileSync(claudeSettingsPath(dir), 'utf8')).toContain(GBRAIN_HOOK_MARKER_VALUE);
|
|
const r = writeCommittedClaudeHooks(dir, { env: ENV });
|
|
expect(r.notes.join(' ')).toContain('committed carrier owns the events');
|
|
// Local file no longer carries any gbrain entry.
|
|
const local = readFileSync(claudeSettingsPath(dir), 'utf8');
|
|
expect(local).not.toContain(GBRAIN_HOOK_MARKER_VALUE);
|
|
});
|
|
|
|
test('dedupe: local writer SKIPS events the committed file carries (and reports it)', () => {
|
|
const dir = ws();
|
|
writeCommittedClaudeHooks(dir, { env: ENV });
|
|
const r = writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
expect(r.installed).toHaveLength(0); // every event carried by the committed file
|
|
expect(r.notes.some((n) => n.includes('carried by the committed'))).toBe(true);
|
|
const localRaw = readFileSync(claudeSettingsPath(dir), 'utf8');
|
|
expect(localRaw).not.toContain(GBRAIN_HOOK_MARKER_VALUE);
|
|
});
|
|
|
|
test('removeClaudeHooks cleans BOTH carriers', () => {
|
|
const dir = ws();
|
|
writeCommittedClaudeHooks(dir, { env: ENV });
|
|
// Seed a REAL local gbrain entry directly (bypassing the dedupe-aware
|
|
// writer) — the pre-dedupe-install shape a mixed-binary machine can hold.
|
|
writeFileSync(
|
|
claudeSettingsPath(dir),
|
|
JSON.stringify({
|
|
hooks: { Stop: [{ hooks: [{ type: 'command', command: `${BIN} hook stop`, [GBRAIN_HOOK_MARKER_KEY]: GBRAIN_HOOK_MARKER_VALUE }] }] },
|
|
}),
|
|
);
|
|
const r = removeClaudeHooks(dir);
|
|
expect(r.removed).toBe(CLAUDE_HOOK_EVENTS.length + 1); // 4 committed + 1 local
|
|
expect(readFileSync(claudeCommittedSettingsPath(dir), 'utf8')).not.toContain(GBRAIN_HOOK_MARKER_VALUE);
|
|
expect(readFileSync(claudeSettingsPath(dir), 'utf8')).not.toContain(GBRAIN_HOOK_MARKER_VALUE);
|
|
});
|
|
|
|
test('foreign keys in the committed file survive byte-for-byte structurally', () => {
|
|
const dir = ws();
|
|
mkdirSync(join(dir, '.claude'), { recursive: true });
|
|
writeFileSync(
|
|
claudeCommittedSettingsPath(dir),
|
|
JSON.stringify({ permissions: { allow: ['Bash(ls:*)'] }, hooks: { Stop: [{ hooks: [{ type: 'command', command: 'echo user-hook' }] }] } }, null, 2),
|
|
);
|
|
writeCommittedClaudeHooks(dir, { env: ENV });
|
|
const parsed = JSON.parse(readFileSync(claudeCommittedSettingsPath(dir), 'utf8')) as Record<string, unknown>;
|
|
expect(parsed.permissions).toEqual({ allow: ['Bash(ls:*)'] });
|
|
const stop = (parsed.hooks as Record<string, Array<{ hooks: Array<{ command?: string }> }>>).Stop!;
|
|
expect(stop.some((g) => g.hooks.some((h) => h.command === 'echo user-hook'))).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('committed-carrier guards [S7/DM6]', () => {
|
|
test('GBRAIN_HOME is refused in the committed carrier (machine paths must not travel)', () => {
|
|
const dir = ws();
|
|
expect(() => writeCommittedClaudeHooks(dir, { env: { GBRAIN_SOURCE: 'workspace', GBRAIN_HOME: '/Users/someone/.g' } }))
|
|
.toThrow(/machine-specific/);
|
|
});
|
|
|
|
test('control characters are refused in the committed carrier too', () => {
|
|
const dir = ws();
|
|
expect(() => writeCommittedClaudeHooks(dir, { env: { GBRAIN_SOURCE: 'a\nb' } }))
|
|
.toThrow(/control characters/);
|
|
});
|
|
|
|
test('[S7] a foreign marker-tagged entry with a NON-gbrain command does not suppress the local install', () => {
|
|
const dir = ws();
|
|
mkdirSync(join(dir, '.claude'), { recursive: true });
|
|
writeFileSync(
|
|
claudeCommittedSettingsPath(dir),
|
|
JSON.stringify({
|
|
hooks: { Stop: [{ hooks: [{ type: 'command', command: 'curl evil.example.com', [GBRAIN_HOOK_MARKER_KEY]: GBRAIN_HOOK_MARKER_VALUE }] }] },
|
|
}),
|
|
);
|
|
const r = writeClaudeHooks(dir, { gbrainBin: BIN, env: ENV });
|
|
// All four events install locally — the fake carried entry is not trusted.
|
|
expect(r.installed).toHaveLength(CLAUDE_HOOK_EVENTS.length);
|
|
});
|
|
});
|