mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-09 06:28:59 +02:00
* fix: pin the claude CLI to an exact version in the CI image + tripwire The image installed @anthropic-ai/claude-code UNPINNED and rebuilt weekly 'to pick up CLI updates' — while bun sat carefully pinned at 1.3.13 two RUN lines above. The PTY harness screen-scrapes this CLI's TUI, and that drift broke it three separate times (welcome-screen wedge on 2.1.233, skillify HOME discovery on 2.1.237, guard/freeze hooks on 2.1.162), each debugged as a flake first. Pin 2.1.251 (current latest), bump deliberately via a PR that runs the PTY gate, and enforce with test/ci-image-cli-pin.test.ts: any global npm install in Dockerfile.ci without an exact @X.Y.Z pin fails the free suite. The weekly ci-image cron stays as a cheap tag self-heal. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: stamp the claude CLI version into every eval-store run record Three harness breakages were traced to claude-CLI TUI drift only after long flake hunts, because no run record said which CLI it actually exercised. EvalCollector now stamps claude_cli_version (claude --version, cached once per process, 'unknown' when the binary is absent) into both partial and finalized records — schema-additive optional field, no SCHEMA_VERSION bump. Correlating a flake wave with a CLI release becomes a grep over ~/.gstack/projects/<slug>/evals/ instead of archaeology. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: give the spinning-shard kill test load headroom (30s -> 90s) The test spawns and group-kills three real children (one a busy-loop burning a full core) while five sibling shard processes compete for eight vCPUs. Under full-suite load it blew bun's default 30s per-test ceiling at 30,009ms — while passing in isolation in 1.4s — and red the only required lane. Every assertion in it is event-based (statuses, group-kill proof, heartbeat lines); the sole latency claim is the <30s kill-deadline sanity bound, which stays. Explicit 90s headroom, not a weakened oracle. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: green-by-skip census — skip counts in the classifier, all-skipped labeling in the paid runner bun's 'Ran N tests' line COUNTS skipped tests, so a codex/gemini shard whose every test self-skipped (binary absent on the runner — true of every CI runner today) exits 0, dodges the hollow-shard guard, and reads as coverage in the weekly census. The classifier now parses bun's ' N skip' / ' N pass' recap lines; ShardOutcome carries skippedTests; formatSummary and the fail-closed slices report label an all-skipped pass explicitly: 'all N tests SKIPPED — verified nothing'. Status stays 'passed' (external service availability is host state, not a repo regression) but the census can no longer mistake absence for coverage. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor: extract composite actions for eval-lane setup; surviving lanes gain the fail-fast registry verification 'Fix bun temp' x3, 'Restore deps' x5, 'Seed claude interactive config' x3, and 'Register gstack skills' x3 were byte-near-identical copies across the legacy matrix, the sliced lane, and the periodic lane — and only the MATRIX copy of register-skills carried the 19-line dangling-symlink + frontmatter fail-fast loop written after a silent 'Unknown command' + 35-min-timeout incident. Extract all four into .github/actions/ composites; the register composite carries the verification loop (generalized over the skill list), so the sliced and periodic lanes — the lanes that SURVIVE the matrix deletion — now inherit the check they had silently dropped. Matrix-job inline copies are left untouched: that job is deleted next. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: delete the legacy 17-row eval matrix — the sliced lane is the only paid lane Every PR paid twice: the hand-enumerated matrix (18 test files, 22.6 min, ~$21 API measured on run 33263204465) ran serialized AHEAD of the strictly superior sliced lane via 'needs: evals' — 35.5 min wall and ~2x paid spend for the same diff. 14 of 17 rows carried no tier:, so periodic Opus benchmarks leaked into every PR (the e2e-plan row alone: 12/12 tests, 21.7 min, $7.28 — the wall-clock bound of ALL of CI). Parity receipt (static, pre-deletion): the sliced lane's gate census (49 files, derived from the runner itself) strictly contains all 18 matrix test files, plus 31 files the matrix never ran. Pure deletion — one revert restores it. The PR comment moved into slices-report (same '## E2E Evals' upsert marker, now sourced from slice artifacts + carrying the fail-closed reconciliation verdict). plan-slices loses the needs edge; the dead workflow-level EVALS_TIER env goes with it. test/evals-workflow-matrix.test.ts (and its KNOWN_MATRIX_GAPS / KNOWN_TIER_UNSET burn-down ratchets — retired: the sliced census makes 'every gate file runs' true by construction) is rewritten as test/evals-workflow-wiring.test.ts: matrix stays deleted, planner/executor/ report tier + slice-count agreement, both surviving lanes on the shared register-skills composite with its fail-fast verification loop, PR comment survival. Expected: PR eval wall 35.5 -> ~13 min, per-PR paid spend ~halved. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: provider-runner timeouts kill the whole process GROUP; codex/gemini inherit the orphan-drain hardening All three provider runners (claude/codex/gemini) killed only the direct child on timeout: tool subprocesses the CLI spawned survived as orphans holding our pipes open and burning shared API rate (observed: a 600s timeout stretching past 1400s; a stalled run once burned a core for 15 hours). gstack-detach's watchdog had the same shape one level up — killpg SIGTERM, 5s grace, then a direct-child proc.kill() that orphaned grandchildren. Fix: spawn provider children via node:child_process with detached (own process group) and killProcessGroup(SIGKILL) in the timeout handler — runShardChild's proven pattern, EPERM/ESRCH fallbacks included. The codex and gemini copies also gain the reader.cancel() + stderr Promise.race hardening only the claude copy had (they still carried the blocked-drain hang it fixed). gstack-detach's watchdog now group-SIGKILLs after the grace. Regression net: test/session-runner-groupkill.test.ts drives the REAL runSkillTest against a fake claude shim (PATH override) that spawns a grandchild and wedges — the run must classify timeout within budget and leave neither shim nor grandchild alive — plus source pins on all three runners (detached + killProcessGroup, no bare timeout kill, no Bun.spawn reversion). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: skill-e2e-opus-47 renders SKILL.md fixtures into a mkdtemp — never the live tree mkEvalRoot ran gen-skill-docs with cwd=ROOT, regenerating every in-repo SKILL.md mid-run while concurrent paid shards copyFileSync those same files in their beforeAll (EVALS_JOBS>=4 locally, 2 per CI slice) — a sibling could capture a half-regenerated or opus-rendered SKILL.md, and a timeout before afterAll stranded the whole tree at the wrong model for every later shard. A cross-shard race that could flake ANY concurrent paid test. Render via the --out-dir flag gen-skill-docs grew for exactly this reason (mirrors the repo layout, which is all the fixture reads), read the skill heads from the render dir, delete it, and drop the afterAll restore-regen entirely. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: claude CLI version resolves in the runner parent, never on a test thread Eng-review finding: getClaudeCliVersion's fallback is a SYNCHRONOUS spawnSync on the same thread that polls concurrent PTY/session tests — the judgePtyState blocking class this overhaul kills elsewhere. The paid runner parent now resolves it once (cached) and stamps GSTACK_CLAUDE_CLI_VERSION into every shard's env; eval-store short-circuits on the env var, and the fallback spawn's budget tightens 10s -> 3s (bounded one-time stall, records 'unknown' on a slow CLI). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test: wire skippedTests end-to-end through runPaidShard The census unit tests hand-built outcomes and the classifier tests parsed strings; nothing proved a real child's ' N skip' recap flows into outcome.skippedTests and the formatSummary label. A commandFor fake now prints the recap shape and the test asserts the parsed counts, the all-skipped predicate, and the 'verified nothing' label. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: make the setup composites rerun-safe (codex diff-review hardenings) restore-deps: 'cp -r SRC node_modules' with an existing node_modules NESTS the copy and leaves stale deps active — rm first. register-gstack-skills: 'ln -snf' hard-errors under set -eu when a REAL directory occupies the gstack slot — clear a non-symlink leftover first. CI workspaces are fresh today; a reusable composite must survive dirty reruns. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: sweep — every sync spawn in the test trees carries a timeout (436 sites, 157 files) spawnSync/execSync/Bun.spawnSync BLOCK the main thread, so bun's in-process per-test timeout can never fire while one waits — a hung child (stdin read, network probe, dead daemon) wedges the whole shard until the runner's external wall-clock SIGKILL. This exact class reached main: free-tests run 33262077256, test/gstack-memory-ingest.test.ts (normally 2.3s) held shard 2 at the 360s wall while its five siblings finished in ~65s. Mechanical sweep in two waves (12 + 4 fan-out agents, every edit verified against its call site): default timeout: 30_000 (matches the free runner's per-test budget), 120_000 for genuinely slow ops (installs, builds, playwright, provider CLIs), helper wrappers fixed ONCE where call sites route through them. Sites that only LOOK like calls (string fixtures, grep needles, comments) were skipped with reasons — the enforcement commit that follows marks them exempt. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test: sync-spawn timeout tripwire — the wedge class stays extinct Free scanner over all test trees (test/, browse/test/, design/test/, make-pdf/test/, ios-qa, browser-skills): every spawnSync/execSync/ Bun.spawnSync call site must carry a timeout within a 30-line options window, or an explicit '// tripwire-exempt: <reason>' marker. Comment lines are skipped; exemptions are counted and ratcheted shrink-only (ceiling 6 = the 6 string-fixture/grep-needle sites where the pattern is CONTENT, not a call — marked in this commit). A scan-sanity test pins that the scanner still sees >100 real call sites so it can never rot to a vacuous green. Companion to the 436-site sweep in the previous commit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: paid-lane flake telemetry — record-level attempts, flaky_retries, report surfacing bun --retry leaves a retried pass INVISIBLE in its output: a fail-then-pass prints the error detail but no (fail) result line and recaps as a clean pass (probed live on 1.3.10). So attempts are recorded where they cannot lie: EvalCollector.addTest stamps a 1-based attempt on same-name re-records (a retried test runs its body again and re-records), finalized runs carry flaky_retries, printSummary warns loudly, and the fail-closed slices report lists every passed-only-on-retry test — recorded and ranked, never blocking and never silent. Cross-model confirmed (codex reached the same don't-parse -the-stream conclusion independently). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: free-lane flake ledger — retry ON in CI, flaky-passes recorded and uploaded The runner's attribution-gated flaky-retry pass (cap 5, truncation veto) was OFF in the required lane and its FLAKY-PASS evidence was console-only — so a single timing flake red the merge gate while repeat offenders stayed unenumerable. free-tests.yml now sets GSTACK_FREE_RETRY_FLAKY=1 and points GSTACK_FLAKE_LEDGER at runner.temp; every flaky-pass appends a JSONL entry (SINGLE writer: the parent runner — no concurrent-append hazard by construction; fail-open with a loud warning so a broken ledger can never red the lane) and the artifact uploads UNCONDITIONALLY — a flaky-pass run is green, which is exactly when the evidence matters. Wiring pinned by free-tests-workflow-wiring; ledger behavior unit-tested incl. the fail-open path. Matches 2026 industry practice (retry for data, quarantine out of merge-blocking but never out of logging) with the repo's own receipts. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: eval:flake-rank — the flake-telemetry dial Aggregates per-test series across every finalized eval-store run (shard dirs included) plus the free flake ledger: runs, fails, RETRIED PASSES (the flake signature), avg duration — ranked retries-first. This is the readable dial behind two policies: a flaky pass never blocks a merge but is always ranked here, and the WS16 required-check promotion needs weeks of clean flake-rank, not vibes. --json for machines, --dir for downloaded CI artifacts. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: two-phase session timeout — silent APIs die at the startup grace, named The single spawn-armed timer charged API queue latency to the work budget: the recurring '0 turns / $0.00 / x3 attempts' failure with four budget-bump receipts (180->300s, 240->360s, 300->420s, 90->300s). Split: startup phase (no NDJSON byte yet) kills EARLY at min(grace, timeout) with the distinct exitReason 'timeout_startup' — an availability verdict, not transcript archaeology — and the work phase arms on the first byte for the REMAINING budget, so total wall never exceeds the timeout (tier envelopes are margin-free: tests pass timeout: CAPTURE_MS and bun-budget the same tier). Local grace 90s (observed queue latency 60-90s), CI floor 300s (TODOS-filed; shared runners queue harder), both pinned by the new grace tests with fake -claude shims covering the late-first-byte and silent-API paths. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: census integrity — 17 phantom selection keys deleted, reverse invariant added, gitignored dep patterns replaced, local map forks derived The merge-blocking gate census counted tests that could not run. Deleted (critic-verified against both quoted-occurrence and dep-registration liveness): 7 *-prosons-format keys with no declaring test, ship-plan- completion/-verification, review-plan-completion, design-shotgun-path/ session/full, autoplan-core (dead ~10 months), e2e-harness-audit (its namesake is a FREE-suite file), plus 2 dead LLM-judge keys and 2 free-file keys (budget-regression-pty, global-discover) misplaced in the PAID maps. Census: 191 -> 174 keys, gate 86 -> 78 honest. The new reverse invariant in touchfiles.test.ts makes the class structurally impossible: every key must be quoted in a living paid test file OR registered to an existing paid test file via its dep list (the constructed- name binding the 2026-08 self-registration sweep established) — zero exceptions needed today, with a live-file check on any future exception. Also: '.agents/skills/**' dep patterns replaced with the generator (scripts/gen-skill-docs.ts) — .agents/ is gitignored, so those patterns could NEVER match a git diff and review-template edits silently stopped selecting codex/gemini tests; the codex/gemini local touchfile maps are now DERIVED from the canonical map (loud throw if a key vanishes) instead of hand-forked copies that had already drifted. ios-qa-e2e demoted gate -> periodic: its gate declaration was never executable in CI (hardware exclusion only applies at tier=periodic), so every Linux PR planned a hollow shard. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: routing journeys lose their answer key and end at the routing decision The journey tests exist to catch skill-DESCRIPTION regressions (touchfiles: */SKILL.md.tmpl), but the fixture CLAUDE.md shipped an explicit prompt->skill lookup table — with the answer key in context, a badly regressed frontmatter description still routed correctly, so the tests could not fail on the exact class they select for. The fixture now carries only the generic invoke-skills nudge; the frontmatter carries the routing load. Also capped all 10 journeys at maxTurns 2 / tools [Skill, Read]: only the FIRST Skill call is asserted, so 5 turns of Read/Bash/Glob/Grep was pure spend — roughly halves each journey's cost. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: retire decided A/B experiments; vendor the pre-cut fixture; ban raw-SHA fixtures Three one-shot decision experiments kept re-running weekly as N=1 stochastic comparisons — flaky by construction with near-zero remaining information: skill-e2e-auq-repetition-cut-ab (its own header: gate "passed pre-landing, approved 2026-08-25"), skill-e2e-preamble-script-ab ("demoted post-Phase-3"), and opus-47's fanout arm-vs-arm (parA >= parB across two SINGLE stochastic runs — a coin flip). Deleted, with their selection keys; the SDK overlay-harness stays as the maintained instrument for the next experiment, and opus-47 keeps its routing-precision cases. verboseSkill() now reads the VENDORED test/fixtures/auq-pre-cut-...-SKILL.md instead of `git show ab66193e^:...` — a branch-local ref that dies on branch prune and already failed on shallow clones. New free tripwire (test/git-ref-fixture-tripwire.test.ts) bans the raw-SHA fixture class outright: quoted SHA:path rev-specs and gitRef-style hex defaults in the test trees fail the suite with the vendor-instead instruction. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: demote plan-ceo-review-expansion-energy to periodic Opus generator + a subjective 2-axis >=4/5 LLM-judge threshold sat in the MERGE-BLOCKING gate — the exact class its sibling posture tests were demoted for, with a receipt (a +21-line preamble change once flipped the score). CLAUDE.md's own tiering rule: Opus model test -> periodic. The weekly lane keeps the regression signal; merges stop paying a judge- temperament tax. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: paid shards get per-shard TMPDIR + CHROMIUM_PROFILE isolation and a kill-path cleanup backstop The free runner treats this isolation as MANDATORY (two concurrent shards on one Chromium profile kill each other's browser; shared tmp cross-contaminates) — the paid lane had none of it. Doubly load-bearing here: a shard that hits its 30-min wall is group-SIGKILLed, so per-test afterAll cleanup never runs; the rmSync backstop is the only thing keeping wedged runs from accumulating full git-repo workspaces in the shared tmpdir forever. This is the DAG prerequisite for raising EVALS_JOBS (next commit) — more concurrency on shared state amplifies exactly the shared-tree race class opus-47 exhibited. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: paid-runner defaults 4x4 -> 8x2 — halve the local gate worst case 39 of 75 skill-e2e files hold exactly ONE test, so within-shard concurrency was dead weight for most shards: 4 jobs x 4 concurrency yielded only ~4-6 real in-flight sessions and a 13-wave local gate worst case (~6.5h). 8 jobs x 2 gives ~10-13 in-flight — under the documented-safe ~15 — and ~7 waves (~3.3h worst case). CI lanes keep their explicit EVALS_JOBS env (2 per slice; 4 for gate-census); this changes local defaults. Rollback trigger: sustained 429 storms in the WS1 telemetry across 2 PR cycles. test/eval-detach-timeout-floor.test.ts recomputed green (the raise LOWERS the worst-case floor). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: SHA-pin every action in the secrets-bearing eval lanes evals.yml and evals-periodic.yml execute PR-authored code with three provider API keys in env, yet rode mutable action tags (@v7/@v8/@v2/@v4) — while quality-gate.yml, osv-scanner.yml, and dependency-review.yml already model the SHA-pin pattern. All 30 uses sites across both lanes now pin the exact commit (tag noted in a trailing comment); dependabot's github-actions ecosystem keeps them fresh via PRs instead of silent tag moves. Pulled forward from the plan's endgame on the CEO-review + outside- voice agreement: supply-chain pins on secret lanes go first, not last. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: sweep wave 3 — the execFileSync family gets timeouts (90 sites, 17 files) The tripwire's regex covered spawnSync/execSync/Bun.spawnSync but not execFileSync — an entire blocking sync-spawn API family that could reintroduce the shard-wedge class undetected (ship review army). Same mechanical recipe as waves 1-2: timeout: 30_000 default, 120_000 for slow ops, shared wrappers fixed once, string-needle sites skipped with reasons. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test: review-army + adversarial test hardening - Tripwire scans execFileSync too (ceiling 8: two more grep-needle string exemptions); merge-introduced timeout-less spawnSync in question-preference-hook fixed — the tripwire caught a site that landed on main AFTER the sweep, on its first day. - gstack-detach gains TWO watchdog kill regression tests: TERM-immune grandchild (the killpg-after-grace escalation) and the leader-dies variant (the pgid-at-spawn fix — the case the first test cannot see). - eval-flake-rank gets its unit suite (final-attempt accounting, artifact exclusion, shard recursion, recency bound). - Groupkill/startup-grace shim markers are per-run unique (pid-suffixed sleep durations): sibling Conductor worktrees run free suites with no machine lock, and fixed markers let one run pgrep/pkill the other's shims — a cross-run flake inside the anti-flake tests. - flake-ledger test pins the project-scoped local default; stale empty section headers in touchfiles-data deleted (they invited entries under deliberately retired categories). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: adversarial-review runtime fixes across the telemetry + kill paths - session-runner: exit-labeling keys off 'exit', not 'close' — an orphan holding the pipes could relabel a REAL exit (auth failure) as 'timeout_startup' availability noise; the kill path still always group-kills and cancels the reader (labeling and unblocking are separate concerns). Work phase arms on a flag, not firstResponseMs===0 (a same-ms first byte left the startup timer live all run). The CI startup grace is now a real FLOOR (Math.max), matching its name and pinning test. - gstack-detach: pgid captured AT SPAWN (== child pid under start_new_session) — resolving it after the grace raised ESRCH once the leader died on SIGTERM, orphaning TERM-immune grandchildren forever. - test-free-shards: ledger entries carry branch + git_sha (rev-parse split: '--abbrev-ref HEAD HEAD' printed the branch twice and recorded it as the sha); local ledger default is per-PROJECT, not the machine-global tmpdir. - eval-flake-rank: per-LINE ledger parse (one torn JSONL line vanished the whole series), 60-day recency bound (transcript-bearing files are MBs), shared isFinalizedEvalResultFile predicate (the artifact-taxonomy rule lived in three places); eval-store exports the predicate and finalize stops computing flakyRetries twice; paid-shards cleanup uses async rm (a SIGKILLed shard's git-workspace teardown blocked every sibling's stream classification on the parent event loop). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: CI trust-boundary + fail-closed repairs (adversarial findings) - Token/exec separation restored: slices-report (runs PR-authored code: bun install + the reconcile runner) drops to contents:read; the PR comment moves to a NEW slices-comment job holding the write token with ZERO repo code — no checkout, no bun, only downloaded artifacts + jq/gh. $GITHUB_ENV/BASH_ENV persistence is job-scoped, so the split is the boundary. The matrix-era report job had this property; the consolidation had regressed it. Pinned by the wiring test. - Reconcile exit captured via PIPESTATUS[0] in BOTH lanes: GitHub's default run-step shell has no pipefail, so `$?` after `| tee` was tee's exit — the fail-closed gate was silently fail-open. Wiring test pins it. - PR comment: final-attempt accounting restored the dropped COST accumulation (the dial read $0 forever), flaky passes render as the warning they are (never as failures), and a malformed tests[] artifact skips that file instead of aborting the whole comment under bash -e. - Remaining mutable action tags pinned (free-tests upload-artifact, ci-image checkout/docker trio — the image publisher holds packages:write and feeds the secret-bearing lanes). restore-deps fallback installs --frozen-lockfile; register-gstack-skills validates skill names before its rm -rf. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore: bump version and changelog (v1.77.0.0) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs: update project documentation for v1.77.0.0 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs: cross-model doc-review fixes — flake-ledger env knobs, CI retry-on note, stale version comment Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs: correct CHANGELOG receipt numbers to measured values Gate census keys: 78 -> 77 (bun-imported E2E_TIERS count). Sweep receipt: 586 sites/176 files -> 499 sites/146 files, measured by running this branch's spawnsync-timeout-tripwire against origin/main (exit 1, 499 violations across 146 unique files; green on this branch). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: slices-comment creates the PR comment via REST — the write-token job has no git context The token/exec split gives slices-comment NO checkout by design, and gh's pr-comment subcommand resolves the repo FROM git — it died with 'not a git repository' on PR #2746's first run (the update-existing PATCH path was already explicit-repo REST and worked). Create now posts through gh api repos/.../issues/N/comments, and the wiring test pins that no git-context-requiring comment call can creep back into the job. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: startup-grace probes clear CI for local semantics; new probe pins the floor clamp The two shim probes pass explicit 2s/4s graces, but in CI the runner clamps any explicit grace up to the 300s floor (deliberate adversarial-review fix), so 'silent API killed at the grace' died at the 30s work cap instead of 2s — a deterministic red on every CI run, green locally. The probes now pin LOCAL semantics with CI cleared (same save/restore pattern as their PATH shim), and a fourth probe pins the clamp itself: CI=1 + 2s grace + 6s timeout must kill at the 6s cap, still in the startup phase — proof an explicit low grace cannot bypass the floor. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1124 lines
49 KiB
TypeScript
1124 lines
49 KiB
TypeScript
/**
|
|
* Tests for lib/code-intelligence — the OPTIONAL, repo-oriented provider contract
|
|
* with three REAL adapters (GBrain CLI, Graphify CLI, Sourcebot HTTP) and the
|
|
* selection store the `gstack-code-intelligence` CLI drives.
|
|
*
|
|
* The Graphify and Sourcebot expectations here are pinned to the REAL formats
|
|
* captured from live tools (graphify 0.9.23 NODE/EDGE query output; Sourcebot v5
|
|
* `/api/search` response + Bearer auth), not invented shapes.
|
|
*/
|
|
|
|
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
|
|
import { canRevokeReads } from "./helpers/fs-caps";
|
|
import * as fs from "fs";
|
|
import * as os from "os";
|
|
import * as path from "path";
|
|
import { execFileSync, spawnSync } from "child_process";
|
|
import { sha256Hex } from "../lib/egress-receipt.js";
|
|
import {
|
|
REQUIRED_CAPABILITIES,
|
|
detectAvailable,
|
|
GbrainProvider,
|
|
GraphifyProvider,
|
|
SourcebotProvider,
|
|
parseGbrainSearch,
|
|
parseGraphifyQuery,
|
|
parseSourcebotSearch,
|
|
readSelection,
|
|
setProvider,
|
|
setConsent,
|
|
hasConsent,
|
|
setRoot,
|
|
getRoot,
|
|
resolveSelectedProvider,
|
|
RECOMMENDED_ORDER,
|
|
shouldOfferIndexing,
|
|
trackedFileCount,
|
|
LARGE_REPO_FILE_THRESHOLD,
|
|
} from "../lib/code-intelligence";
|
|
|
|
describe("capability matrix", () => {
|
|
test("every provider advertises the four required capabilities", () => {
|
|
for (const p of [new GbrainProvider(), new SourcebotProvider(), new GraphifyProvider()]) {
|
|
for (const cap of REQUIRED_CAPABILITIES) expect(p.has(cap)).toBe(true);
|
|
}
|
|
});
|
|
|
|
test("only GBrain advertises the document ops; local flags are right", () => {
|
|
const g = new GbrainProvider();
|
|
expect(g.local).toBe(false);
|
|
for (const cap of ["add", "delete", "export"] as const) expect(g.has(cap)).toBe(true);
|
|
|
|
const s = new SourcebotProvider({ baseUrl: "http://localhost:3000" });
|
|
expect(s.local).toBe(true); // loopback → content stays on machine
|
|
expect(s.has("add")).toBe(false);
|
|
expect(new SourcebotProvider({ baseUrl: "https://sb.example.com" }).local).toBe(false);
|
|
|
|
const gf = new GraphifyProvider();
|
|
expect(gf.local).toBe(true);
|
|
expect(gf.has("export")).toBe(true);
|
|
expect(gf.has("add")).toBe(false);
|
|
});
|
|
|
|
test("RECOMMENDED_ORDER puts GBrain first", () => {
|
|
expect([...RECOMMENDED_ORDER]).toEqual(["gbrain", "sourcebot", "graphify"]);
|
|
});
|
|
});
|
|
|
|
describe("parsers (pinned to real tool output)", () => {
|
|
test("parseGbrainSearch (text surface)", () => {
|
|
const hits = parseGbrainSearch("[0.91] slug/a -- one\nbanner\n[0.05] slug/b -- low", 0.1, 10);
|
|
expect(hits).toEqual([{ ref: "slug/a", score: 0.91, snippet: "one", kind: "document" }]);
|
|
});
|
|
|
|
test("parseGraphifyQuery reads file:line from real NODE/EDGE lines", () => {
|
|
// Verbatim shape from graphify 0.9.23 `query ... --graph`.
|
|
const real = [
|
|
"Traversal: BFS depth=2 | Start: ['query()'] | Context: call (heuristic) | 4 nodes found",
|
|
"",
|
|
"NODE query() [src=db.py loc=L4 community=login]",
|
|
"EDGE query() --calls [EXTRACTED context=call]--> login() at=auth.py:L8",
|
|
].join("\n");
|
|
const hits = parseGraphifyQuery(real, 10);
|
|
expect(hits.map((h) => h.ref)).toEqual(["db.py:L4", "auth.py:L8"]); // NOT "graphify"
|
|
expect(hits.every((h) => h.kind === "graph-node")).toBe(true);
|
|
// The Traversal header must NOT become a bogus hit.
|
|
expect(hits.some((h) => h.snippet?.startsWith("Traversal:"))).toBe(false);
|
|
});
|
|
|
|
test("parseSourcebotSearch maps files to file:line hits (real v5 shape)", () => {
|
|
const real = {
|
|
files: [
|
|
{
|
|
fileName: { text: "src/checksum.ts", matchRanges: [] },
|
|
repository: "github.com/example/sb-sample",
|
|
chunks: [{ content: "export function computeChecksum(data: string): number {", matchRanges: [{ start: { byteOffset: 58, column: 17, lineNumber: 2 } }] }],
|
|
},
|
|
],
|
|
};
|
|
expect(parseSourcebotSearch(real, 10)).toEqual([
|
|
{ ref: "src/checksum.ts:2", snippet: "export function computeChecksum(data: string): number {", kind: "file" },
|
|
]);
|
|
expect(parseSourcebotSearch("nope", 10)).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("selection store + provider-OFF", () => {
|
|
let home: string;
|
|
let env: NodeJS.ProcessEnv;
|
|
beforeEach(() => {
|
|
home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-home-"));
|
|
env = { ...process.env, GSTACK_HOME: home };
|
|
});
|
|
afterEach(() => fs.rmSync(home, { recursive: true, force: true }));
|
|
|
|
test("no selection = provider-OFF (null)", () => {
|
|
expect(readSelection(env).provider).toBeNull();
|
|
expect(resolveSelectedProvider({ env })).toBeNull();
|
|
});
|
|
|
|
test("select persists and resolves the provider", () => {
|
|
setProvider("graphify", env);
|
|
expect(readSelection(env).provider).toBe("graphify");
|
|
expect(resolveSelectedProvider({ env })?.id).toBe("graphify");
|
|
});
|
|
|
|
test("consent is per-repo", () => {
|
|
const repo = path.join(home, "repoA");
|
|
expect(hasConsent(repo, env)).toBe(false);
|
|
setConsent(repo, true, env);
|
|
expect(hasConsent(repo, env)).toBe(true);
|
|
expect(hasConsent(path.join(home, "repoB"), env)).toBe(false);
|
|
});
|
|
|
|
test("indexed root persists per provider (so search reads the same graph)", () => {
|
|
expect(getRoot("graphify", env)).toBeUndefined();
|
|
setRoot("graphify", "/tmp/some/repo", env);
|
|
expect(getRoot("graphify", env)).toBe(path.resolve("/tmp/some/repo"));
|
|
});
|
|
|
|
test("select none records the decline; selecting a provider clears it", () => {
|
|
setProvider(null, env);
|
|
expect(readSelection(env).declined).toBe(true);
|
|
setProvider("graphify", env);
|
|
expect(readSelection(env).declined).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("session-start indexing offer (suggest)", () => {
|
|
let home: string;
|
|
let repo: string;
|
|
let env: NodeJS.ProcessEnv;
|
|
beforeEach(() => {
|
|
home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-home-"));
|
|
repo = fs.mkdtempSync(path.join(os.tmpdir(), "ci-repo-"));
|
|
env = { ...process.env, GSTACK_HOME: home };
|
|
Bun.spawnSync(["git", "init", "-q", repo], { timeout: 30_000 });
|
|
for (const name of ["a.ts", "b.ts", "c.ts"]) fs.writeFileSync(path.join(repo, name), "x\n");
|
|
Bun.spawnSync(["git", "-C", repo, "add", "-A"], { timeout: 30_000 });
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
fs.rmSync(repo, { recursive: true, force: true });
|
|
});
|
|
|
|
test("offers exactly once: large repo with no prior decision", () => {
|
|
const s = shouldOfferIndexing(repo, { env, threshold: 3 });
|
|
expect(s).toEqual({ offer: true, reason: "large-repo", fileCount: 3, threshold: 3 });
|
|
});
|
|
|
|
test("small repo → no offer", () => {
|
|
expect(shouldOfferIndexing(repo, { env, threshold: 4 }).reason).toBe("small-repo");
|
|
});
|
|
|
|
test("not a git repo → no offer", () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-plain-"));
|
|
try {
|
|
expect(shouldOfferIndexing(dir, { env, threshold: 0 }).reason).toBe("not-a-repo");
|
|
} finally {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("provider already selected → no offer", () => {
|
|
setProvider("graphify", env);
|
|
expect(shouldOfferIndexing(repo, { env, threshold: 3 }).reason).toBe("provider-selected");
|
|
});
|
|
|
|
test("explicit decline → never asked again", () => {
|
|
setProvider(null, env);
|
|
expect(shouldOfferIndexing(repo, { env, threshold: 3 }).reason).toBe("declined");
|
|
});
|
|
|
|
test("trackedFileCount counts git-tracked files only", () => {
|
|
fs.writeFileSync(path.join(repo, "untracked.ts"), "x\n");
|
|
expect(trackedFileCount(repo)).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe("egress consent gate", () => {
|
|
test("GBrain (non-local) registerSource without consent → PROVIDER_NOT_CONSENTED", async () => {
|
|
await expect(new GbrainProvider().registerSource({ id: "code", path: "/repo" })).rejects.toMatchObject({
|
|
code: "PROVIDER_NOT_CONSENTED",
|
|
});
|
|
});
|
|
|
|
test("Graphify (local) is exempt from the egress gate", async () => {
|
|
await expect(
|
|
new GraphifyProvider({ env: { PATH: "/nonexistent" } }).registerSource({ id: "r", path: os.tmpdir() }),
|
|
).rejects.toMatchObject({ code: "PROVIDER_UNAVAILABLE" });
|
|
});
|
|
});
|
|
|
|
describe("Graphify adapter (fake graphify shim, real NODE/EDGE format)", () => {
|
|
let binDir: string;
|
|
let repo: string;
|
|
function env(): NodeJS.ProcessEnv {
|
|
return { PATH: `${binDir}:${process.env.PATH}` };
|
|
}
|
|
beforeEach(() => {
|
|
binDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-gf-bin-"));
|
|
repo = fs.mkdtempSync(path.join(os.tmpdir(), "ci-gf-repo-"));
|
|
// Shim emulates real graphify 0.9.23: `update <path>` writes graph.json (no LLM);
|
|
// `query <q> --graph <path>` prints NODE/EDGE lines.
|
|
fs.writeFileSync(
|
|
path.join(binDir, "graphify"),
|
|
`#!/usr/bin/env bash
|
|
case "$1" in
|
|
--version) echo "graphify 0.9.23"; exit 0;;
|
|
update) mkdir -p "$2/graphify-out"; echo '{"nodes":[1,2,3,4,5],"edges":[]}' > "$2/graphify-out/graph.json"; echo "Rebuilt: 5 nodes, 8 edges"; exit 0;;
|
|
query)
|
|
echo "Traversal: BFS depth=2 | Start: ['query()'] | 4 nodes found"
|
|
echo ""
|
|
echo "NODE query() [src=db.py loc=L4 community=login]"
|
|
echo "EDGE query() --calls [EXTRACTED context=call]--> login() at=auth.py:L8"
|
|
exit 0;;
|
|
esac
|
|
exit 1
|
|
`,
|
|
{ mode: 0o755 },
|
|
);
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(binDir, { recursive: true, force: true });
|
|
fs.rmSync(repo, { recursive: true, force: true });
|
|
});
|
|
|
|
test("index builds a graph (via `graphify update`) and status counts nodes", async () => {
|
|
const gf = new GraphifyProvider({ root: repo, env: env() });
|
|
const reg = await gf.registerSource({ id: repo, path: repo });
|
|
expect(reg.state).toBe("ready");
|
|
expect(reg.itemCount).toBe(5);
|
|
expect(fs.existsSync(path.join(repo, "graphify-out", "graph.json"))).toBe(true);
|
|
});
|
|
|
|
test("search reads file:line refs from real query output", async () => {
|
|
const gf = new GraphifyProvider({ root: repo, env: env() });
|
|
await gf.registerSource({ id: repo, path: repo });
|
|
const hits = await gf.search("what calls db", { source: repo });
|
|
expect(hits.map((h) => h.ref)).toEqual(["db.py:L4", "auth.py:L8"]);
|
|
});
|
|
|
|
test("missing graphify CLI degrades to PROVIDER_UNAVAILABLE", async () => {
|
|
await expect(
|
|
new GraphifyProvider({ root: repo, env: { PATH: os.tmpdir() } }).search("q"),
|
|
).rejects.toMatchObject({ code: "PROVIDER_UNAVAILABLE" });
|
|
});
|
|
|
|
test("status skips parsing a huge graph.json (heap guard): size in detail, no itemCount", async () => {
|
|
const outDir = path.join(repo, "graphify-out");
|
|
fs.mkdirSync(outDir, { recursive: true });
|
|
// 6MB of spaces — over the 5MB parse threshold, so the content is never
|
|
// parsed (on real target repos graph.json can run to hundreds of MB).
|
|
fs.writeFileSync(path.join(outDir, "graph.json"), Buffer.alloc(6 * 1024 * 1024, 0x20));
|
|
const s = await new GraphifyProvider({ root: repo, env: env() }).status();
|
|
expect(s.state).toBe("ready");
|
|
expect(s.itemCount).toBeUndefined();
|
|
expect(s.detail).toContain("node count skipped");
|
|
});
|
|
});
|
|
|
|
describe("Sourcebot adapter (injected fetch, real v5 auth + shape)", () => {
|
|
test("registerSource writes a local git connection to config.json", async () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-sb-"));
|
|
const configPath = path.join(dir, "config.json");
|
|
await new SourcebotProvider({ baseUrl: "http://localhost:3000", configPath }).registerSource({ id: "myrepo", path: "/abs/repo" });
|
|
const written = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
|
expect(written.connections.myrepo).toEqual({ type: "git", url: "file:///abs/repo" });
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
test("search sends Bearer auth and maps the real v5 response to hits", async () => {
|
|
const seen: Array<{ url: string; auth: string | null }> = [];
|
|
const fetchStub = (async (url: string, init: RequestInit) => {
|
|
seen.push({ url: String(url), auth: (init.headers as Record<string, string>)?.Authorization ?? null });
|
|
return new Response(
|
|
JSON.stringify({ files: [{ fileName: { text: "a.ts" }, chunks: [{ content: "x", matchRanges: [{ start: { lineNumber: 3 } }] }] }] }),
|
|
{ status: 200, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}) as unknown as typeof fetch;
|
|
const sb = new SourcebotProvider({ baseUrl: "http://localhost:3000", apiKey: "sbk_test", fetch: fetchStub });
|
|
const hits = await sb.search("foo");
|
|
expect(seen[0].url).toBe("http://localhost:3000/api/search");
|
|
expect(seen[0].auth).toBe("Bearer sbk_test");
|
|
expect(hits).toEqual([{ ref: "a.ts:3", snippet: "x", kind: "file" }]);
|
|
});
|
|
|
|
test("401 (no API key) degrades to PROVIDER_UNAVAILABLE, not PROVIDER_ERROR", async () => {
|
|
const fetchStub = (async () =>
|
|
new Response(JSON.stringify({ errorCode: "NOT_AUTHENTICATED" }), { status: 401 })) as unknown as typeof fetch;
|
|
await expect(
|
|
new SourcebotProvider({ baseUrl: "http://localhost:3000", fetch: fetchStub }).search("q"),
|
|
).rejects.toMatchObject({ code: "PROVIDER_UNAVAILABLE" });
|
|
});
|
|
|
|
test("unreachable server degrades to PROVIDER_UNAVAILABLE", async () => {
|
|
const fetchStub = (async () => {
|
|
throw new Error("ECONNREFUSED");
|
|
}) as unknown as typeof fetch;
|
|
await expect(
|
|
new SourcebotProvider({ baseUrl: "http://localhost:3999", fetch: fetchStub }).search("q"),
|
|
).rejects.toMatchObject({ code: "PROVIDER_UNAVAILABLE" });
|
|
});
|
|
|
|
test("registerSource without SOURCEBOT_CONFIG → PROVIDER_UNAVAILABLE", async () => {
|
|
await expect(
|
|
new SourcebotProvider({ baseUrl: "http://localhost:3000", env: {} }).registerSource({ id: "r", path: "/x" }),
|
|
).rejects.toMatchObject({ code: "PROVIDER_UNAVAILABLE" });
|
|
});
|
|
});
|
|
|
|
describe("GBrain adapter (fake gbrain shim)", () => {
|
|
let binDir: string;
|
|
let homeDir: string;
|
|
function env(): NodeJS.ProcessEnv {
|
|
return { PATH: `${binDir}:${process.env.PATH}`, HOME: homeDir };
|
|
}
|
|
function writeShim(body: string): void {
|
|
fs.writeFileSync(path.join(binDir, "gbrain"), body, { mode: 0o755 });
|
|
}
|
|
beforeEach(() => {
|
|
binDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-gb-bin-"));
|
|
homeDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-gb-home-"));
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(binDir, { recursive: true, force: true });
|
|
fs.rmSync(homeDir, { recursive: true, force: true });
|
|
});
|
|
|
|
test("search parses hits (and sends no --source: gbrain search is global)", async () => {
|
|
writeShim(`#!/usr/bin/env bash
|
|
if [ "$1" = "search" ]; then
|
|
if printf '%s ' "$@" | grep -q -- "--source"; then echo "[0.0] ERR -- adapter sent phantom --source"; exit 0; fi
|
|
echo "[0.88] src/x.ts -- match"; exit 0
|
|
fi
|
|
exit 1
|
|
`);
|
|
const hits = await new GbrainProvider().search("where", { env: env(), consented: true });
|
|
expect(hits).toEqual([{ ref: "src/x.ts", score: 0.88, snippet: "match", kind: "document" }]);
|
|
});
|
|
|
|
test("refresh runs the code-indexing pass (`sync --strategy code --full`)", async () => {
|
|
// Real gbrain only indexes code when `sync --strategy code` runs; without it
|
|
// code-def stays not_built. Pin that the adapter issues that pass.
|
|
const marker = path.join(homeDir, "sync-calls.log");
|
|
writeShim(`#!/usr/bin/env bash
|
|
if [ "$1" = "sync" ]; then printf '%s\\n' "$*" >> "${marker}"; exit 0; fi
|
|
if [ "$1" = "sources" ]; then echo '{"sources":[{"id":"code","local_path":"/r","page_count":1}]}'; exit 0; fi
|
|
exit 1
|
|
`);
|
|
await new GbrainProvider().refresh({ id: "code" }, { env: env(), consented: true });
|
|
const log = fs.readFileSync(marker, "utf-8");
|
|
expect(log).toContain("--strategy code");
|
|
expect(log).toContain("--full");
|
|
});
|
|
|
|
test("engine-down (pglite WASM) degrades to PROVIDER_UNAVAILABLE, not PROVIDER_ERROR", async () => {
|
|
// Reproduces garrytan/gbrain#223: engine fails to init; must degrade cleanly.
|
|
writeShim(`#!/usr/bin/env bash
|
|
echo "PGLite failed to initialize its WASM runtime." >&2
|
|
echo " Original error: Aborted()." >&2
|
|
exit 1
|
|
`);
|
|
await expect(new GbrainProvider().search("q", { env: env(), consented: true })).rejects.toMatchObject({
|
|
code: "PROVIDER_UNAVAILABLE",
|
|
});
|
|
});
|
|
|
|
test("missing CLI degrades to PROVIDER_UNAVAILABLE", async () => {
|
|
await expect(
|
|
new GbrainProvider().search("q", { env: { PATH: binDir, HOME: homeDir }, consented: true }),
|
|
).rejects.toMatchObject({ code: "PROVIDER_UNAVAILABLE" });
|
|
});
|
|
});
|
|
|
|
// ── R1: the repo-policy deny tier vetoes recorded consent ───────────────────
|
|
// Two consent stores must never disagree about whether code may leave a repo:
|
|
// gstack-gbrain-repo-policy (per-remote trust tiers) is the single authority.
|
|
describe("consent unification — deny tier wins (R1)", () => {
|
|
function makeRepo(dir: string, url: string): string {
|
|
const repo = path.join(dir, "repo");
|
|
fs.mkdirSync(repo, { recursive: true });
|
|
const git = (...a: string[]) => execFileSync("git", a, { cwd: repo, timeout: 30_000 });
|
|
git("init", "-q", ".");
|
|
git("remote", "add", "origin", url);
|
|
return repo;
|
|
}
|
|
const POLICY_BIN = path.join(import.meta.dir, "..", "bin", "gstack-gbrain-repo-policy");
|
|
const URL = "https://github.com/acme/veto-widget.git";
|
|
|
|
test("recorded consent survives when no policy store exists", () => {
|
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-veto-"));
|
|
try {
|
|
const env = { ...process.env, GSTACK_HOME: home };
|
|
const repo = makeRepo(home, URL);
|
|
setConsent(repo, true, env);
|
|
expect(hasConsent(repo, env)).toBe(true);
|
|
} finally { fs.rmSync(home, { recursive: true, force: true }); }
|
|
});
|
|
|
|
test("deny tier vetoes recorded consent", () => {
|
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-veto-"));
|
|
try {
|
|
const env = { ...process.env, GSTACK_HOME: home };
|
|
const repo = makeRepo(home, URL);
|
|
setConsent(repo, true, env);
|
|
execFileSync(POLICY_BIN, ["set", URL, "deny"], { env, encoding: "utf-8", timeout: 30_000 });
|
|
expect(hasConsent(repo, env)).toBe(false);
|
|
// Flipping the tier back restores the recorded consent — the veto is
|
|
// live policy, not a destructive rewrite of the consent store.
|
|
execFileSync(POLICY_BIN, ["set", URL, "read-write"], { env, encoding: "utf-8", timeout: 30_000 });
|
|
expect(hasConsent(repo, env)).toBe(true);
|
|
} finally { fs.rmSync(home, { recursive: true, force: true }); }
|
|
});
|
|
|
|
test("unreadable policy store fails closed (consent vetoed) for BOTH op classes", () => {
|
|
if (!canRevokeReads()) return; // chmod is advisory here (win32, root, DAC-override containers)
|
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-veto-"));
|
|
try {
|
|
const env = { ...process.env, GSTACK_HOME: home };
|
|
const repo = makeRepo(home, URL);
|
|
setConsent(repo, true, env);
|
|
execFileSync(POLICY_BIN, ["set", URL, "read-write"], { env, encoding: "utf-8", timeout: 30_000 });
|
|
fs.chmodSync(path.join(home, "gbrain-repo-policy.json"), 0o000);
|
|
try {
|
|
expect(hasConsent(repo, env)).toBe(false);
|
|
expect(hasConsent(repo, env, "read")).toBe(false);
|
|
} finally {
|
|
fs.chmodSync(path.join(home, "gbrain-repo-policy.json"), 0o600);
|
|
}
|
|
} finally { fs.rmSync(home, { recursive: true, force: true }); }
|
|
});
|
|
|
|
// ── R2: read-only is a WRITE veto, not a total one ─────────────────────────
|
|
// gstack-gbrain-sync semantics: "search allowed, page writes never". The
|
|
// code-intelligence veto must match: index/register/refresh (write-class)
|
|
// are refused on read-only; search (read-class) still works.
|
|
test("read-only tier vetoes write-class consent but allows read-class (R2)", () => {
|
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-veto-"));
|
|
try {
|
|
const env = { ...process.env, GSTACK_HOME: home };
|
|
const repo = makeRepo(home, URL);
|
|
setConsent(repo, true, env);
|
|
execFileSync(POLICY_BIN, ["set", URL, "read-only"], { env, encoding: "utf-8", timeout: 30_000 });
|
|
// Default op class is write — a caller that doesn't say gets fail-closed.
|
|
expect(hasConsent(repo, env)).toBe(false);
|
|
expect(hasConsent(repo, env, "write")).toBe(false);
|
|
// Read-class (search/export/status) survives read-only.
|
|
expect(hasConsent(repo, env, "read")).toBe(true);
|
|
} finally { fs.rmSync(home, { recursive: true, force: true }); }
|
|
});
|
|
|
|
test("deny beats consent for BOTH op classes", () => {
|
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-veto-"));
|
|
try {
|
|
const env = { ...process.env, GSTACK_HOME: home };
|
|
const repo = makeRepo(home, URL);
|
|
setConsent(repo, true, env);
|
|
execFileSync(POLICY_BIN, ["set", URL, "deny"], { env, encoding: "utf-8", timeout: 30_000 });
|
|
expect(hasConsent(repo, env, "write")).toBe(false);
|
|
expect(hasConsent(repo, env, "read")).toBe(false);
|
|
} finally { fs.rmSync(home, { recursive: true, force: true }); }
|
|
});
|
|
});
|
|
|
|
// ── R2 at the CLI: `index` is write-class, so read-only refuses it ──────────
|
|
describe("read-only repo policy blocks write-class CLI index (R2)", () => {
|
|
const CLI = path.join(import.meta.dir, "..", "bin", "gstack-code-intelligence");
|
|
const POLICY_BIN = path.join(import.meta.dir, "..", "bin", "gstack-gbrain-repo-policy");
|
|
const URL = "https://github.com/acme/readonly-widget.git";
|
|
|
|
test("index refuses on read-only even with recorded consent (gbrain provider)", () => {
|
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-ro-"));
|
|
const shimDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-ro-bin-"));
|
|
try {
|
|
// Shim shadows any real gbrain on PATH: even if a regression lets the
|
|
// index proceed, this test can never touch a real brain.
|
|
fs.writeFileSync(path.join(shimDir, "gbrain"), "#!/usr/bin/env bash\nexit 1\n", { mode: 0o755 });
|
|
const env = { ...process.env, GSTACK_HOME: home, PATH: `${shimDir}:${process.env.PATH}` };
|
|
const repo = path.join(home, "repo");
|
|
fs.mkdirSync(repo, { recursive: true });
|
|
execFileSync("git", ["init", "-q", "."], { cwd: repo });
|
|
execFileSync("git", ["remote", "add", "origin", URL], { cwd: repo });
|
|
setProvider("gbrain", env);
|
|
setConsent(repo, true, env);
|
|
execFileSync(POLICY_BIN, ["set", URL, "read-only"], { env, encoding: "utf-8" });
|
|
const res = spawnSync("bun", [CLI, "index", repo], {
|
|
encoding: "utf-8",
|
|
timeout: 30_000,
|
|
env: env as Record<string, string>,
|
|
});
|
|
expect(res.status).not.toBe(0);
|
|
expect(res.stderr).toContain("repo trust policy");
|
|
} finally {
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
fs.rmSync(shimDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
// ── receipt truthfulness: the tamper-evident ledger must never claim a consent
|
|
// that was never checked (red-team finding 1). Non-loopback Sourcebot only —
|
|
// loopback sends nothing off-machine and writes no receipt at all.
|
|
describe("Sourcebot egress receipts record the TRUE consent state", () => {
|
|
let home: string;
|
|
beforeEach(() => {
|
|
home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-sb-egress-"));
|
|
});
|
|
afterEach(() => fs.rmSync(home, { recursive: true, force: true }));
|
|
|
|
function ledgerLines(): Array<Record<string, unknown>> {
|
|
const p = path.join(home, "security", "egress.jsonl");
|
|
if (!fs.existsSync(p)) return [];
|
|
return fs
|
|
.readFileSync(p, "utf-8")
|
|
.split("\n")
|
|
.filter(Boolean)
|
|
.map((l) => JSON.parse(l) as Record<string, unknown>);
|
|
}
|
|
|
|
const okFetch = (async () =>
|
|
new Response(JSON.stringify({ files: [] }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
})) as unknown as typeof fetch;
|
|
|
|
test("status probe: allowed without consent, receipt says consent=unchecked (never consented=true)", async () => {
|
|
const sb = new SourcebotProvider({ baseUrl: "http://sb.example.com:3000", fetch: okFetch });
|
|
const s = await sb.status(undefined, { env: { GSTACK_HOME: home } });
|
|
expect(s.state).toBe("ready");
|
|
const lines = ledgerLines();
|
|
expect(lines.length).toBe(1);
|
|
expect(lines[0].sink).toBe("sourcebot");
|
|
expect(String(lines[0].consent)).toContain("consent=unchecked");
|
|
expect(String(lines[0].consent)).not.toContain("consented=true");
|
|
expect(String(lines[0].payload_class)).toContain("liveness-probe");
|
|
});
|
|
|
|
test("non-loopback search without consent is refused BEFORE any bytes leave (fail-closed)", async () => {
|
|
let calls = 0;
|
|
const spyFetch = (async () => {
|
|
calls++;
|
|
return new Response("{}", { status: 200 });
|
|
}) as unknown as typeof fetch;
|
|
const sb = new SourcebotProvider({ baseUrl: "http://sb.example.com:3000", fetch: spyFetch });
|
|
await expect(sb.search("internalSecretFn", { env: { GSTACK_HOME: home } })).rejects.toMatchObject({
|
|
code: "PROVIDER_NOT_CONSENTED",
|
|
});
|
|
expect(calls).toBe(0); // the query never left the machine
|
|
expect(ledgerLines()).toEqual([]); // nothing sent → nothing receipted
|
|
});
|
|
|
|
test("non-loopback refresh without consent → PROVIDER_NOT_CONSENTED (write-class)", async () => {
|
|
const sb = new SourcebotProvider({ baseUrl: "http://sb.example.com:3000", fetch: okFetch });
|
|
await expect(sb.refresh({ id: "r" }, { env: { GSTACK_HOME: home } })).rejects.toMatchObject({
|
|
code: "PROVIDER_NOT_CONSENTED",
|
|
});
|
|
expect(ledgerLines()).toEqual([]);
|
|
});
|
|
|
|
test("consented non-loopback search sends, and the receipt truthfully records consented=true", async () => {
|
|
const sb = new SourcebotProvider({ baseUrl: "http://sb.example.com:3000", fetch: okFetch });
|
|
const hits = await sb.search("foo", { consented: true, env: { GSTACK_HOME: home } });
|
|
expect(hits).toEqual([]);
|
|
const lines = ledgerLines();
|
|
expect(lines.length).toBe(1);
|
|
expect(String(lines[0].consent)).toContain("consented=true");
|
|
expect(lines[0].payload_class).toBe("code-search-request");
|
|
});
|
|
|
|
test("loopback search stays consent-free and writes no receipt (no egress)", async () => {
|
|
const sb = new SourcebotProvider({ baseUrl: "http://localhost:3000", fetch: okFetch });
|
|
await sb.search("foo", { env: { GSTACK_HOME: home } });
|
|
expect(ledgerLines()).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// ── GBrain consent + receipts: gbrain federates into a possibly-remote
|
|
// DATABASE_URL and (unlike Sourcebot) has no cheap loopback check — the URL is
|
|
// resolved inside the gbrain CLI's own config — so EVERY content-bearing send
|
|
// is consent-gated and receipted: search's query text and the export request
|
|
// included, not just register/refresh/add.
|
|
describe("GBrain search/export consent gate + egress receipts", () => {
|
|
let binDir: string;
|
|
let home: string;
|
|
let marker: string;
|
|
function env(): NodeJS.ProcessEnv {
|
|
return { PATH: `${binDir}:${process.env.PATH}`, HOME: home, GSTACK_HOME: home };
|
|
}
|
|
function ledgerLines(): Array<Record<string, unknown>> {
|
|
const p = path.join(home, "security", "egress.jsonl");
|
|
if (!fs.existsSync(p)) return [];
|
|
return fs
|
|
.readFileSync(p, "utf-8")
|
|
.split("\n")
|
|
.filter(Boolean)
|
|
.map((l) => JSON.parse(l) as Record<string, unknown>);
|
|
}
|
|
beforeEach(() => {
|
|
binDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-gb-egress-bin-"));
|
|
home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-gb-egress-home-"));
|
|
marker = path.join(home, "gbrain-invocations.log");
|
|
// Shim logs every invocation, so "refused BEFORE the subprocess runs" is
|
|
// provable: an unconsented op must leave the marker file nonexistent.
|
|
fs.writeFileSync(
|
|
path.join(binDir, "gbrain"),
|
|
`#!/usr/bin/env bash
|
|
printf '%s\\n' "$*" >> "${marker}"
|
|
if [ "$1" = "search" ]; then echo "[0.88] src/x.ts -- match"; exit 0; fi
|
|
if [ "$1" = "export" ]; then echo "exported-brain-body"; exit 0; fi
|
|
exit 1
|
|
`,
|
|
{ mode: 0o755 },
|
|
);
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(binDir, { recursive: true, force: true });
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
});
|
|
|
|
test("unconsented search is refused BEFORE the subprocess runs (fail-closed) and writes no receipt", async () => {
|
|
await expect(new GbrainProvider().search("internalSecretFn", { env: env() })).rejects.toMatchObject({
|
|
code: "PROVIDER_NOT_CONSENTED",
|
|
});
|
|
expect(fs.existsSync(marker)).toBe(false); // the query never left the machine
|
|
expect(ledgerLines()).toEqual([]); // nothing sent → nothing receipted
|
|
});
|
|
|
|
test("consented search sends, and the receipt truthfully records consented=true + the query hash", async () => {
|
|
const hits = await new GbrainProvider().search("where is auth", { env: env(), consented: true });
|
|
expect(hits).toEqual([{ ref: "src/x.ts", score: 0.88, snippet: "match", kind: "document" }]);
|
|
const lines = ledgerLines();
|
|
expect(lines.length).toBe(1);
|
|
expect(lines[0].sink).toBe("gbrain");
|
|
expect(String(lines[0].consent)).toContain("consented=true");
|
|
expect(String(lines[0].payload_class)).toContain("code-search-query");
|
|
expect(lines[0].sha256).toBe(sha256Hex("where is auth"));
|
|
expect(lines[0].bytes).toBe(Buffer.byteLength("where is auth"));
|
|
});
|
|
|
|
test("unconsented export is refused (no loopback exemption exists for gbrain) and writes no receipt", async () => {
|
|
await expect(new GbrainProvider().export({ id: "code" }, { env: env() })).rejects.toMatchObject({
|
|
code: "PROVIDER_NOT_CONSENTED",
|
|
});
|
|
expect(fs.existsSync(marker)).toBe(false);
|
|
expect(ledgerLines()).toEqual([]);
|
|
});
|
|
|
|
test("consented export sends and writes a truthful receipt", async () => {
|
|
const body = await new GbrainProvider().export({ id: "code" }, { env: env(), consented: true });
|
|
expect(body).toContain("exported-brain-body");
|
|
const lines = ledgerLines();
|
|
expect(lines.length).toBe(1);
|
|
expect(lines[0].sink).toBe("gbrain");
|
|
expect(String(lines[0].consent)).toContain("consented=true");
|
|
expect(String(lines[0].payload_class)).toContain("brain-export-request");
|
|
});
|
|
});
|
|
|
|
// ── the CLI names WHY a gbrain search is refused (missing consent vs a deny
|
|
// repo trust policy) instead of surfacing a generic provider error, and the
|
|
// refusal happens before any subprocess spawn or receipt.
|
|
describe("CLI search consent gate (gbrain provider, honest refusal message)", () => {
|
|
const CLI = path.join(import.meta.dir, "..", "bin", "gstack-code-intelligence");
|
|
const POLICY_BIN = path.join(import.meta.dir, "..", "bin", "gstack-gbrain-repo-policy");
|
|
const URL = "https://github.com/acme/search-widget.git";
|
|
let home: string;
|
|
let shimDir: string;
|
|
let marker: string;
|
|
let repo: string;
|
|
let env: NodeJS.ProcessEnv;
|
|
|
|
beforeEach(() => {
|
|
home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-cli-search-"));
|
|
shimDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-cli-search-bin-"));
|
|
marker = path.join(home, "gbrain-invocations.log");
|
|
fs.writeFileSync(
|
|
path.join(shimDir, "gbrain"),
|
|
`#!/usr/bin/env bash\nprintf '%s\\n' "$*" >> "${marker}"\necho "[0.90] slug -- hit"; exit 0\n`,
|
|
{ mode: 0o755 },
|
|
);
|
|
repo = path.join(home, "repo");
|
|
fs.mkdirSync(repo, { recursive: true });
|
|
execFileSync("git", ["init", "-q", "."], { cwd: repo });
|
|
execFileSync("git", ["remote", "add", "origin", URL], { cwd: repo });
|
|
env = { ...process.env, GSTACK_HOME: home, PATH: `${shimDir}:${process.env.PATH}` };
|
|
setProvider("gbrain", env);
|
|
setRoot("gbrain", repo, env);
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
fs.rmSync(shimDir, { recursive: true, force: true });
|
|
});
|
|
|
|
function runSearch() {
|
|
return spawnSync("bun", [CLI, "search", "internalSecretFn"], {
|
|
encoding: "utf-8",
|
|
timeout: 30_000,
|
|
env: env as Record<string, string>,
|
|
});
|
|
}
|
|
|
|
test("no recorded consent → refused with the consent instruction; nothing spawned, nothing receipted", () => {
|
|
const res = runSearch();
|
|
expect(res.status).not.toBe(0);
|
|
expect(res.stderr).toContain("consent");
|
|
expect(fs.existsSync(marker)).toBe(false);
|
|
expect(fs.existsSync(path.join(home, "security", "egress.jsonl"))).toBe(false);
|
|
});
|
|
|
|
test("deny repo trust policy → refused with the trust-policy explanation even with recorded consent", () => {
|
|
setConsent(repo, true, env);
|
|
execFileSync(POLICY_BIN, ["set", URL, "deny"], { env, encoding: "utf-8" });
|
|
const res = runSearch();
|
|
expect(res.status).not.toBe(0);
|
|
expect(res.stderr).toContain("repo trust policy");
|
|
expect(fs.existsSync(marker)).toBe(false);
|
|
});
|
|
|
|
test("recorded consent + no deny → search runs against the provider", () => {
|
|
setConsent(repo, true, env);
|
|
const res = runSearch();
|
|
expect(res.status).toBe(0);
|
|
expect(res.stdout).toContain("slug");
|
|
expect(fs.existsSync(marker)).toBe(true);
|
|
});
|
|
});
|
|
|
|
// ── consent CLI polarity — a recorded "no" must persist DENIED, never granted ─
|
|
describe("consent CLI requires an explicit yes|no (never defaults to granted)", () => {
|
|
const CLI = path.join(import.meta.dir, "..", "bin", "gstack-code-intelligence");
|
|
let home: string;
|
|
let repo: string;
|
|
let env: NodeJS.ProcessEnv;
|
|
|
|
function runConsent(...args: string[]) {
|
|
const res = spawnSync("bun", [CLI, "consent", ...args], {
|
|
encoding: "utf-8",
|
|
timeout: 30_000,
|
|
env: env as Record<string, string>,
|
|
});
|
|
return { status: res.status ?? -1, stdout: res.stdout || "", stderr: res.stderr || "" };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-consent-home-"));
|
|
repo = fs.mkdtempSync(path.join(os.tmpdir(), "ci-consent-repo-"));
|
|
env = { ...process.env, GSTACK_HOME: home };
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
fs.rmSync(repo, { recursive: true, force: true });
|
|
});
|
|
|
|
test("consent <repo> no records FALSE, and the deny-check honors it", () => {
|
|
const r = runConsent(repo, "no");
|
|
expect(r.status).toBe(0);
|
|
expect(readSelection(env).consents[path.resolve(repo)]).toBe(false);
|
|
expect(hasConsent(repo, env)).toBe(false);
|
|
});
|
|
|
|
test("consent <repo> yes records true; a later no overrides it", () => {
|
|
expect(runConsent(repo, "yes").status).toBe(0);
|
|
expect(hasConsent(repo, env)).toBe(true);
|
|
expect(runConsent(repo, "no").status).toBe(0);
|
|
expect(readSelection(env).consents[path.resolve(repo)]).toBe(false);
|
|
expect(hasConsent(repo, env)).toBe(false);
|
|
});
|
|
|
|
test("true/false are accepted as aliases", () => {
|
|
expect(runConsent(repo, "false").status).toBe(0);
|
|
expect(readSelection(env).consents[path.resolve(repo)]).toBe(false);
|
|
expect(runConsent(repo, "true").status).toBe(0);
|
|
expect(readSelection(env).consents[path.resolve(repo)]).toBe(true);
|
|
});
|
|
|
|
test("missing value exits nonzero and records NOTHING (never defaults to yes)", () => {
|
|
const r = runConsent(repo);
|
|
expect(r.status).not.toBe(0);
|
|
expect(r.stderr).toContain("yes|no");
|
|
expect(readSelection(env).consents).toEqual({});
|
|
expect(hasConsent(repo, env)).toBe(false);
|
|
});
|
|
|
|
test("garbage value exits nonzero and records NOTHING", () => {
|
|
const r = runConsent(repo, "maybe");
|
|
expect(r.status).not.toBe(0);
|
|
expect(r.stderr).toContain("yes|no");
|
|
expect(readSelection(env).consents).toEqual({});
|
|
});
|
|
});
|
|
|
|
// ── GBrain document ops (add/delete/export) — behavioral pins over the same
|
|
// fake-gbrain shim harness. add() must pipe the body to `put <slug>` on stdin
|
|
// and receipt the body's sha256 BEFORE the send; delete() must survive a
|
|
// confirmation prompt via the stdin-EOF guard; export() must return the CLI's
|
|
// stdout verbatim; and each must degrade to PROVIDER_UNAVAILABLE (not a hard
|
|
// error) when the CLI is absent.
|
|
describe("GBrain document ops (add/delete/export) via fake gbrain shim", () => {
|
|
let binDir: string;
|
|
let home: string;
|
|
let argvLog: string;
|
|
let stdinLog: string;
|
|
function env(): NodeJS.ProcessEnv {
|
|
return { PATH: `${binDir}:${process.env.PATH}`, HOME: home, GSTACK_HOME: home };
|
|
}
|
|
function ledgerLines(): Array<Record<string, unknown>> {
|
|
const p = path.join(home, "security", "egress.jsonl");
|
|
if (!fs.existsSync(p)) return [];
|
|
return fs
|
|
.readFileSync(p, "utf-8")
|
|
.split("\n")
|
|
.filter(Boolean)
|
|
.map((l) => JSON.parse(l) as Record<string, unknown>);
|
|
}
|
|
beforeEach(() => {
|
|
binDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-gb-docops-bin-"));
|
|
home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-gb-docops-home-"));
|
|
argvLog = path.join(home, "gbrain-argv.log");
|
|
stdinLog = path.join(home, "gbrain-stdin.log");
|
|
// Shim logs every argv (so "refused BEFORE the subprocess" is provable),
|
|
// captures put's stdin byte-for-byte, and blocks delete on a confirmation
|
|
// read the way a real prompt would — the adapter's stdin-EOF guard is the
|
|
// only thing keeping that from hanging until the op timeout.
|
|
fs.writeFileSync(
|
|
path.join(binDir, "gbrain"),
|
|
`#!/usr/bin/env bash
|
|
printf '%s\\n' "$*" >> "${argvLog}"
|
|
case "$1" in
|
|
put) cat > "${stdinLog}"; echo "stored $2"; exit 0;;
|
|
delete) read -r _confirm; echo "deleted $2"; exit 0;;
|
|
export) printf 'line-one\\nline-two\\n'; exit 0;;
|
|
esac
|
|
exit 1
|
|
`,
|
|
{ mode: 0o755 },
|
|
);
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(binDir, { recursive: true, force: true });
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
});
|
|
|
|
test("add() pipes the document body to `put <slug>` on stdin and receipts the body's sha256", async () => {
|
|
const body = "# curated memory\nthe exact body bytes\n";
|
|
const s = await new GbrainProvider().add({ slug: "my-doc", body }, { env: env(), consented: true });
|
|
expect(s).toEqual({ id: "my-doc", state: "ready" });
|
|
expect(fs.readFileSync(argvLog, "utf-8").trim()).toBe("put my-doc");
|
|
// The body reached gbrain via stdin, byte-for-byte (not argv, not a temp file).
|
|
expect(fs.readFileSync(stdinLog, "utf-8")).toBe(body);
|
|
const lines = ledgerLines();
|
|
expect(lines.length).toBe(1);
|
|
expect(lines[0].sink).toBe("gbrain");
|
|
expect(String(lines[0].payload_class)).toContain("document-body");
|
|
expect(String(lines[0].consent)).toContain("consented=true");
|
|
expect(lines[0].sha256).toBe(sha256Hex(body));
|
|
expect(lines[0].bytes).toBe(Buffer.byteLength(body));
|
|
});
|
|
|
|
test("add() asserts consent: unconsented add is refused BEFORE the subprocess runs, no receipt", async () => {
|
|
await expect(new GbrainProvider().add({ slug: "s", body: "secret body" }, { env: env() })).rejects.toMatchObject({
|
|
code: "PROVIDER_NOT_CONSENTED",
|
|
});
|
|
expect(fs.existsSync(argvLog)).toBe(false); // the body never left the machine
|
|
expect(ledgerLines()).toEqual([]);
|
|
});
|
|
|
|
test("delete() survives a confirmation prompt via the stdin-EOF guard (never hangs)", async () => {
|
|
// Shim blocks on `read` — with the adapter's input:"" the read gets EOF
|
|
// instantly. A regression to inherited/open stdin fails here at the 5s
|
|
// op timeout (PROVIDER_TIMEOUT) instead of returning state=absent.
|
|
const s = await new GbrainProvider().delete("stale-doc", { env: env(), timeout: 5_000 });
|
|
expect(s).toEqual({ id: "stale-doc", state: "absent" });
|
|
expect(fs.readFileSync(argvLog, "utf-8").trim()).toBe("delete stale-doc");
|
|
});
|
|
|
|
test("export() returns the CLI's stdout verbatim (multi-line, unparsed)", async () => {
|
|
const body = await new GbrainProvider().export({ id: "code" }, { env: env(), consented: true });
|
|
expect(body).toBe("line-one\nline-two\n");
|
|
expect(fs.readFileSync(argvLog, "utf-8").trim()).toBe("export");
|
|
});
|
|
|
|
test("add/delete/export each degrade to PROVIDER_UNAVAILABLE when the CLI is absent", async () => {
|
|
const missing = { PATH: os.tmpdir(), HOME: home, GSTACK_HOME: home };
|
|
const g = new GbrainProvider();
|
|
await expect(g.add({ slug: "s", body: "b" }, { env: missing, consented: true })).rejects.toMatchObject({
|
|
code: "PROVIDER_UNAVAILABLE",
|
|
});
|
|
await expect(g.delete("s", { env: missing })).rejects.toMatchObject({ code: "PROVIDER_UNAVAILABLE" });
|
|
await expect(g.export({ id: "code" }, { env: missing, consented: true })).rejects.toMatchObject({
|
|
code: "PROVIDER_UNAVAILABLE",
|
|
});
|
|
});
|
|
});
|
|
|
|
// ── CLI rendering surfaces: options / status / suggest --json ───────────────
|
|
// The human/agent-facing renderers had zero reach: pin that `options` lists
|
|
// the three providers with availability rows (GBrain first), `status` renders
|
|
// the persisted selection + per-provider availability, and `suggest --json`
|
|
// emits the machine-readable offer/reason contract.
|
|
describe("CLI rendering (options / status / suggest --json)", () => {
|
|
const CLI = path.join(import.meta.dir, "..", "bin", "gstack-code-intelligence");
|
|
let home: string;
|
|
let shimDir: string;
|
|
let env: NodeJS.ProcessEnv;
|
|
|
|
function runCli(...args: string[]) {
|
|
return spawnSync("bun", [CLI, ...args], {
|
|
encoding: "utf-8",
|
|
timeout: 30_000,
|
|
env: env as Record<string, string>,
|
|
});
|
|
}
|
|
|
|
beforeEach(() => {
|
|
home = fs.mkdtempSync(path.join(os.tmpdir(), "ci-cli-render-"));
|
|
shimDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-cli-render-bin-"));
|
|
// Fake gbrain satisfies the FULL localEngineStatus probe chain (--version
|
|
// resolution + `sources list --json` liveness) so the GBrain row is
|
|
// deterministically available; config.json lives in the temp GBRAIN_HOME
|
|
// so the operator's real ~/.gbrain is never read.
|
|
fs.writeFileSync(
|
|
path.join(shimDir, "gbrain"),
|
|
`#!/usr/bin/env bash
|
|
if [ "$1" = "--version" ]; then echo "gbrain 0.42.0"; exit 0; fi
|
|
if [ "$1" = "sources" ]; then echo '{"sources":[]}'; exit 0; fi
|
|
exit 1
|
|
`,
|
|
{ mode: 0o755 },
|
|
);
|
|
const gbrainHome = path.join(home, ".gbrain");
|
|
fs.mkdirSync(gbrainHome, { recursive: true });
|
|
fs.writeFileSync(path.join(gbrainHome, "config.json"), JSON.stringify({ engine: "pglite" }));
|
|
env = {
|
|
...process.env,
|
|
GSTACK_HOME: home,
|
|
HOME: home,
|
|
// #2521: GBRAIN_HOME is the PARENT of .gbrain per gbrain's configDir()
|
|
// contract — pointing it at `home` resolves to home/.gbrain/config.json.
|
|
GBRAIN_HOME: home,
|
|
PATH: `${shimDir}:${process.env.PATH}`,
|
|
// Dead loopback port → the Sourcebot probe fails fast + deterministically
|
|
// (connection refused) instead of poking whatever operator dev server
|
|
// happens to sit on localhost:3000.
|
|
SOURCEBOT_URL: "http://127.0.0.1:1",
|
|
};
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(home, { recursive: true, force: true });
|
|
fs.rmSync(shimDir, { recursive: true, force: true });
|
|
});
|
|
|
|
function makeRepoWithFiles(count: number): string {
|
|
const repo = fs.mkdtempSync(path.join(os.tmpdir(), "ci-cli-suggest-"));
|
|
Bun.spawnSync(["git", "init", "-q", repo], { timeout: 30_000 });
|
|
for (let i = 0; i < count; i++) fs.writeFileSync(path.join(repo, `f${i}.ts`), "x\n");
|
|
Bun.spawnSync(["git", "-C", repo, "add", "-A"], { timeout: 30_000 });
|
|
return repo;
|
|
}
|
|
|
|
test("options lists the three providers with availability rows, GBrain first", () => {
|
|
const res = runCli("options");
|
|
expect(res.status).toBe(0);
|
|
expect(res.stdout).toContain("Code-intelligence providers");
|
|
// GBrain row: recommended marker + deterministically available via the shim.
|
|
expect(res.stdout).toMatch(/\* GBrain\s+\[available\]/);
|
|
expect(res.stdout).toContain("gbrain engine: ok");
|
|
// Sourcebot row: dead loopback port → not available, probe detail rendered.
|
|
expect(res.stdout).toMatch(/Sourcebot\s+\[not available\]/);
|
|
expect(res.stdout).toContain("unreachable at http://127.0.0.1:1");
|
|
// Graphify row renders an availability mark either way (the operator
|
|
// machine may or may not have graphify installed).
|
|
expect(res.stdout).toMatch(/Graphify\s+\[(available|not available)\]/);
|
|
// Rows come out in recommendation order (GBrain → Sourcebot → Graphify).
|
|
const at = ["GBrain", "Sourcebot", "Graphify"].map((s) => res.stdout.indexOf(s));
|
|
expect(at[0]).toBeGreaterThan(-1);
|
|
expect(at[0]).toBeLessThan(at[1]);
|
|
expect(at[1]).toBeLessThan(at[2]);
|
|
expect(res.stdout).toContain("select <provider>");
|
|
});
|
|
|
|
test("status renders the provider-OFF selection and per-provider availability", () => {
|
|
const res = runCli("status");
|
|
expect(res.status).toBe(0);
|
|
expect(res.stdout).toContain("selected: none (grep / file-only fallback)");
|
|
expect(res.stdout).toContain("GBrain: available (gbrain engine: ok)");
|
|
expect(res.stdout).toContain("Sourcebot: unavailable (unreachable at http://127.0.0.1:1)");
|
|
expect(res.stdout).toMatch(/Graphify: (available|unavailable)/);
|
|
});
|
|
|
|
test("status renders a persisted selection", () => {
|
|
setProvider("gbrain", env);
|
|
const res = runCli("status");
|
|
expect(res.status).toBe(0);
|
|
expect(res.stdout).toContain("selected: gbrain");
|
|
});
|
|
|
|
test("suggest --json: small repo → machine-readable offer:false / reason:small-repo", () => {
|
|
const repo = makeRepoWithFiles(3);
|
|
try {
|
|
const res = runCli("suggest", repo, "--json");
|
|
expect(res.status).toBe(0);
|
|
const parsed = JSON.parse(res.stdout) as Record<string, unknown>;
|
|
expect(parsed).toEqual({
|
|
offer: false,
|
|
reason: "small-repo",
|
|
fileCount: 3,
|
|
threshold: LARGE_REPO_FILE_THRESHOLD,
|
|
repoPath: repo,
|
|
});
|
|
} finally {
|
|
fs.rmSync(repo, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("suggest --json: large repo → offer:true with self-contained provider options", () => {
|
|
const repo = makeRepoWithFiles(LARGE_REPO_FILE_THRESHOLD);
|
|
try {
|
|
const res = runCli("suggest", repo, "--json");
|
|
expect(res.status).toBe(0);
|
|
const parsed = JSON.parse(res.stdout) as {
|
|
offer: boolean;
|
|
reason: string;
|
|
fileCount: number;
|
|
options: Array<{ id: string; label: string; reason: string; local: boolean; available: boolean; detail: string }>;
|
|
};
|
|
expect(parsed.offer).toBe(true);
|
|
expect(parsed.reason).toBe("large-repo");
|
|
expect(parsed.fileCount).toBe(LARGE_REPO_FILE_THRESHOLD);
|
|
// The offer is self-contained: every provider option carries the fields
|
|
// an agent needs to render the question without a second CLI call.
|
|
expect(parsed.options.map((o) => o.id)).toEqual(["gbrain", "sourcebot", "graphify"]);
|
|
for (const o of parsed.options) {
|
|
expect(typeof o.label).toBe("string");
|
|
expect(typeof o.reason).toBe("string");
|
|
expect(typeof o.local).toBe("boolean");
|
|
expect(typeof o.available).toBe("boolean");
|
|
expect(typeof o.detail).toBe("string");
|
|
}
|
|
const gbrain = parsed.options[0];
|
|
expect(gbrain.label).toBe("GBrain");
|
|
expect(gbrain.local).toBe(false);
|
|
expect(gbrain.available).toBe(true);
|
|
expect(gbrain.detail).toContain("gbrain engine:");
|
|
const sourcebot = parsed.options[1];
|
|
expect(sourcebot.local).toBe(true); // loopback SOURCEBOT_URL
|
|
expect(sourcebot.available).toBe(false);
|
|
} finally {
|
|
fs.rmSync(repo, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
// ── picker probes: concurrent, short-timeout, never a 30s CLI stall ─────────
|
|
describe("detectAvailable probes fast even when Sourcebot is a dead non-loopback host", () => {
|
|
test("rows come back in recommendation order, well under the old 30s stall", async () => {
|
|
const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-pick-bin-"));
|
|
const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-pick-home-"));
|
|
// localEngineStatus writes its probe cache via process.env.GSTACK_HOME —
|
|
// point it at the temp home for the duration so nothing touches ~/.gstack.
|
|
const prevGstackHome = process.env.GSTACK_HOME;
|
|
process.env.GSTACK_HOME = homeDir;
|
|
try {
|
|
// PATH-scoped fake gbrain; graphify deliberately absent from that PATH.
|
|
fs.writeFileSync(
|
|
path.join(binDir, "gbrain"),
|
|
`#!/usr/bin/env bash
|
|
if [ "$1" = "--version" ]; then echo "gbrain 0.42.0"; exit 0; fi
|
|
if [ "$1" = "sources" ]; then echo '{"sources":[]}'; exit 0; fi
|
|
exit 1
|
|
`,
|
|
{ mode: 0o755 },
|
|
);
|
|
const env: NodeJS.ProcessEnv = { PATH: binDir, HOME: homeDir, GSTACK_HOME: homeDir };
|
|
// A hanging fetch that only settles on abort — the 3s probe cap must cut
|
|
// it off; with the adapters' 30s default this test would blow its budget.
|
|
const hangingFetch = ((_url: string, init: RequestInit) =>
|
|
new Promise((_resolve, reject) => {
|
|
init.signal?.addEventListener("abort", () =>
|
|
reject(Object.assign(new Error("aborted"), { name: "AbortError" })),
|
|
);
|
|
})) as unknown as typeof fetch;
|
|
const t0 = Date.now();
|
|
const rows = await detectAvailable({
|
|
env,
|
|
sourcebot: { baseUrl: "http://sb.internal.example:3000", fetch: hangingFetch },
|
|
});
|
|
expect(Date.now() - t0).toBeLessThan(5_000);
|
|
expect(rows.map((r) => r.id)).toEqual(["gbrain", "sourcebot", "graphify"]);
|
|
const gbrain = rows.find((r) => r.id === "gbrain")!;
|
|
expect(gbrain.detail).toContain("gbrain engine:");
|
|
const sourcebot = rows.find((r) => r.id === "sourcebot")!;
|
|
expect(sourcebot.available).toBe(false);
|
|
const graphify = rows.find((r) => r.id === "graphify")!;
|
|
expect(graphify.available).toBe(false); // not on the scoped PATH
|
|
expect(graphify.detail).toContain("not installed");
|
|
} finally {
|
|
if (prevGstackHome === undefined) delete process.env.GSTACK_HOME;
|
|
else process.env.GSTACK_HOME = prevGstackHome;
|
|
fs.rmSync(binDir, { recursive: true, force: true });
|
|
fs.rmSync(homeDir, { recursive: true, force: true });
|
|
}
|
|
}, 20_000);
|
|
});
|