mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-16 18:05:31 +02:00
* feat: add a restricted and supervised Claude Code runner Preserve configured authentication and models while enforcing tool access, strict completion JSON, bounded output and process cleanup. Cover argv, failure handling, session metadata and Windows process containment. * feat: route outside reviews by harness and migrate wrapper installs Use Claude Code from Codex and Codex from other supported hosts, with shared invocation rendering, positive gate validation and per-phase provenance. Rename /claude to /claude-code, repair managed shared and copied installations safely, and generate native Kiro skills. Add installed-workflow, failure-injection and live cross-harness regression coverage. * test: recognize CEO mode labels without terminal spacing The paid workflow rendered SCOPEEXPANSION at option 4, but its driver required a literal space. Match the leading mode title without cursor-spacing artifacts and ignore adjacent preview text. Preserve missing-target failures and downstream posture assertions. * test: isolate plan-count fixtures before starting review workflows Seed the complete test plan in a private git repository before launching Claude, so a bare slash command cannot review the live workspace while a delayed fixture message remains queued. Preserve count thresholds, parsers and budgets. Add initial-context and installed-discovery tests, and retain startup/terminal diagnostics on failed evaluations. * test: stabilize review fixtures and Claude eval startup Preserve source boundaries in workflow judge inputs, isolate CEO mode plans, and wait for interactive trust input readiness. Keep startup failure evidence and retain existing models, budgets, and assertions. Co-Authored-By: OpenAI Codex <noreply@openai.com> * test: classify collapsed review modes and isolate seeded findings Keep review questions out of the setup count when terminal cursor positioning removes spaces. State existing webhook safeguards so the five-finding control measures its seeded defects without accidental extra security and concurrency gaps. Preserve question bands and the paired control. Co-Authored-By: OpenAI Codex <noreply@openai.com> * test: isolate browser daemon state across free shards Co-Authored-By: OpenAI Codex <noreply@openai.com> * test: stabilize native review counting and interactive navigation Co-Authored-By: OpenAI Codex <noreply@openai.com> * chore: prepare v1.82.0.0 release Co-Authored-By: OpenAI Codex <noreply@openai.com> * fix: eliminate browser and process-cleanup test flakes Pin every CI surface to Bun 1.4.0 to avoid extra-stdio finalizers closing reused live sockets. Add an isolated GC/listener regression that fails on Bun 1.3.13, and prevent coordinated rollback to an affected CI runtime. Check renderer cleanup against the render's own staging directory so concurrent renders cannot invalidate the assertion. Make the no-pgrep process-tree walk tolerate disappearing /proc entries, and synchronize its test fixture through child readiness and pipe EOF instead of sleeps. Validation: 9,157 passed, 31 skipped, zero failures across 556 files with retries disabled. Build, all-host generation freshness, and skill checks passed. All three races have failing-before/passing-after regressions. * fix: count completed native review questions in evals * fix: drive review navigation from confirmed native choices * fix: require complete section-loading eval reports * test: isolate telemetry HTTP transport from local assertions * fix: keep review input on the active native question * test: let tunnel revocation daemon choose an available port * test: allocate available ports for pairing and watchdog fixtures * fix: stabilize planning eval navigation and phase reporting * test: isolate installed runtime paths in planning evals * test: stabilize review evidence and concurrent refresh fixtures * fix: resolve design findings before editing the plan * fix: honor and persist disabled outside plan reviews * fix: preserve planning decisions and terminal evidence Load installed host reviews at autoplan phase entry and wait for completed reviewers and saved artifacts. Reuse approved remedies while preserving individual finding decisions. Drive interactive evals from the current terminal viewport, bind native questions across scrolling, and require complete native report evidence. Cover captured stale menus, permission lifecycles, setup classification, and disabled-review tool availability with deterministic regressions. Advance release metadata and the upgrade migration to the unclaimed 1.83.0.0 slot. * fix: drive native review questions and preserve current plans Use the native single-choice keyboard protocol and current terminal viewport, with per-question navigation inside packets and completed-call coverage. Keep permissions, multi-select menus, and Submit controls distinct. Send Autoplan reviewers the amended implementation plan, keep its review record separate, and supply retained application contracts in the chain fixture. Clarify individual DevEx decisions and complete CEO fix options; use one active plan destination for the section-loading report. * fix: preserve complete plan-review decisions * fix: recognize native plan dialogs and reviewer controls * fix: preserve review decisions and phase completion * fix: recognize completed reviews without losing findings * fix: preserve review continuity and native eval completion * test: fix native review completion and eval retry isolation * test: handle native review menus and complete eval fixtures * test: fix native review setup, completion, and isolation failures * test: limit native skill discovery to runtime assets * fix: bind Autoplan reviews to full ordered phase inputs * test: fix planning eval routing, counting, and timeout handling * chore: advance queued release to v1.84.0.0 * fix: preserve complete review inputs and planning decisions * fix: reconcile review approvals and preserve phase obligations * fix: preserve review obligations and unblock eval permissions Carry recorded Autoplan requirements into blind phase inputs, require Eng review approvals before exit, and exercise combined asynchronous flows in CEO reviews. Correct native finding and handoff classification and unblock repeated report edits using scoped request identities. * fix: retain plan requirements and complete native review dialogs * fix: complete native review prompts and retain plan references * fix: preserve review inputs and classify native eval evidence * fix: check competing completion orders in CEO reviews * fix: recognize review decisions and require phase methodology Require the current phase methodology before Autoplan snapshots. Correct substantive decision, closed handoff, and cache-finding classification, and honor the recommended implementation approach in native review dialogs. Add captured-transcript regressions without changing review thresholds, provider models, retries, or deadlines. * test: bind native review decisions and close completed handoffs * fix: complete review dialogs and verify methodology delivery * fix: preserve review evidence and unblock native eval prompts * fix: handle native review question completions * fix: recognize native review narration and controls * fix: count native review decisions and isolate eval fixtures * test: verify seeded review coverage and current artifact permissions * test: isolate model and brain-aware skill renders * fix: repair native workflow evaluation and clarify review steps * fix: stabilize workflow eval evidence and review guidance * test: repair native workflow observation and fixture isolation * fix: recognize completed workflow evidence and owned skill reads * test: repair seeded workflow delivery and completion evidence * test: recognize current review evidence across native forms * test: handle native review variants and permission redraws * fix: honor review preferences and recognize native eval evidence * test: recognize completed review decisions and queued permissions * test: match current review contracts and partial-line edits * test: recognize completed workflow evidence and bounded human waits * fix: preserve review entry gates and native eval interactions * fix: recognize native workflow evidence and preserve review gates * test: recognize current review evidence and preconfigure workflow fixtures * test: recognize completed review findings and scoped artifact permissions * fix: stabilize native workflow review and permission evidence * fix: recognize current review evidence and scoped edit confirmations Clarify Design and engineering review entry instructions and Design scoring. Recognize required legacy coverage and public Autoplan completion recaps. Bind the pending Edit confirmation to its exact file, ordered digest, and one-request approval when a preceding command display remains visible. Keep reviews within their existing size limits and preserve scope gates when extracting workflow fixtures from either supported preamble header. Keep failure outcomes, review thresholds, provider choices, and eval budgets. * fix: recover review workflow progress and eval evidence * fix: recognize valid review evidence and scope selection * test: fix review evidence parsing and repeated artifact prompts * test: recognize valid review decisions and pending native cards * fix(plan-eng-review): keep final navigation consistent with approved tasks * test: recognize valid review evidence and bind legacy diff requests * fix: stabilize review eval evidence and harness repair guidance * docs: update project documentation for v1.85.0.0 Co-Authored-By: OpenAI Codex <noreply@openai.com> * test: fix Windows CI fixtures and credential scan Rebase captured JSON values and filesystem evidence using the appropriate path convention. Compile native fake CLIs on Windows and synchronize pipe holder readiness, with cleanup retained when assertions fail. Assemble synthetic credential fixtures at runtime so the added-line scan keeps enforcing the same gate without flagging its own rejection controls. Discover generated skills directly for the empty-find regression check, avoiding a recursive scan through saved evaluation artifacts and dependencies. * fix: preserve source renders on Windows Compare canonical generator paths using native separators so an output sidecar pointing at the source cannot overwrite its skill or metadata. Keep the regression fixture isolated from the real checkout and expose freshness diagnostics before asserting subprocess status. Detach Windows drain-test pipe holders from the fake provider's automatic child cleanup while preserving the enclosing runner job and its assertions. * fix: clarify outside review fallback and CEO decisions Render one applicable own-harness fallback path and retain native review, disabled policy, and missing-coverage semantics. Align report field names and mode labels, and make the existing per-cut scope approval explicit. Regenerate skill outputs and keep the workflow judge's model, thresholds, and retry policy unchanged. * chore: move release to free version slot (v1.86.0.0) PR #2852 now claims v1.85.0.0. Align the release metadata and rename migration so upgrades from that version still receive it. Co-Authored-By: OpenAI Codex <noreply@openai.com> * fix: include engineering review prerequisites and restore branch context * fix: recognize coverage diagrams and clarify design review instructions * fix: preserve file identities and join Windows test processes --------- Co-authored-by: OpenAI Codex <noreply@openai.com>
436 lines
20 KiB
TypeScript
436 lines
20 KiB
TypeScript
/**
|
|
* Hermetic child environment for E2E test runners.
|
|
*
|
|
* Local E2E runs spawn `claude` (and codex/gemini/SDK) children that, until
|
|
* this module, inherited the operator's full session context: ~/.claude
|
|
* (user CLAUDE.md, .claude.json MCP servers incl. gbrain + Conductor,
|
|
* skills), ~/.gstack decision logs, and CONDUCTOR_-/CLAUDECODE-style env vars.
|
|
* CI was hermetic only by accident (fresh Docker /home/runner). This module
|
|
* makes local children see a CI-equivalent clean room by default.
|
|
*
|
|
* operator shell (contaminated) hermetic child env
|
|
* ┌─────────────────────────────┐ buildHermeticEnv()
|
|
* │ PATH, HOME, TMPDIR, ... │── allowlist ─────────► kept
|
|
* │ HTTP(S)_PROXY, SSL_CERT_* │── allowlist ─────────► kept (network)
|
|
* │ ANTHROPIC_API_KEY/BASE_URL/ │── named list ────────► kept (auth)
|
|
* │ AUTH_TOKEN │
|
|
* │ GSTACK_ANTHROPIC_API_KEY │── promotedEnv() ─────► ANTHROPIC_API_KEY
|
|
* │ CONDUCTOR_*, CLAUDECODE, │
|
|
* │ CLAUDE_*, GSTACK_*, MCP_*, │── dropped ───────────► ∅
|
|
* │ GBRAIN_*, GH_TOKEN, ... │
|
|
* └─────────────────────────────┘
|
|
* + per-runner extraAllow (codex: OpenAI vars; gemini: Google vars)
|
|
* + CLAUDE_CONFIG_DIR=<runRoot>/.claude GSTACK_HOME=<runRoot>/gstack-home
|
|
* + per-test overrides spread LAST
|
|
*
|
|
* Escape hatch: EVALS_HERMETIC=0 restores the legacy contaminated env
|
|
* byte-identically (runners must also gate --strict-mcp-config on
|
|
* isHermeticEnabled() so the escape hatch restores args too).
|
|
*
|
|
* isHermeticEnabled() is evaluated at CALL time, never at module load —
|
|
* ESM hoists imports above any in-file `process.env.EVALS_HERMETIC = '0'`
|
|
* assignment, so a module-load-time read would silently ignore test pins.
|
|
*/
|
|
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as os from 'os';
|
|
import { promotedEnv } from '../../lib/conductor-env-shim';
|
|
import { isProcessAlive, safeUnlink } from '../../lib/error-handling';
|
|
import { skillCensus, frontmatterName } from './skill-census';
|
|
|
|
/** Exact env names a hermetic child keeps. Everything not listed (or matched
|
|
* by a prefix rule below) is dropped. */
|
|
const ALLOW_EXACT = new Set([
|
|
// Process basics
|
|
'PATH', 'HOME', 'TMPDIR', 'TERM', 'COLORTERM', 'LANG', 'LC_ALL', 'SHELL',
|
|
'USER', 'LOGNAME', 'TZ', 'NODE_ENV', 'CI',
|
|
// Browser/runtime caches the child legitimately shares with the operator
|
|
'PLAYWRIGHT_BROWSERS_PATH',
|
|
// Network reachability — without these, children on proxied networks can't
|
|
// reach the Anthropic API at all
|
|
'HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY',
|
|
'http_proxy', 'https_proxy', 'no_proxy',
|
|
'SSL_CERT_FILE', 'SSL_CERT_DIR', 'NODE_EXTRA_CA_CERTS',
|
|
// Auth — named, NOT the broad ANTHROPIC_* prefix: a prefix rule would
|
|
// smuggle model/beta/debug knobs that change eval behavior
|
|
'ANTHROPIC_API_KEY', // the auth credential evals require
|
|
'ANTHROPIC_BASE_URL', // API endpoint override (corp proxies)
|
|
'ANTHROPIC_AUTH_TOKEN', // bearer-token auth variant
|
|
]);
|
|
|
|
/** Prefix rules: eval-harness knobs + CI metadata. Deliberately NOT here:
|
|
* CONDUCTOR_* / CLAUDE_* (incl. CLAUDECODE, CLAUDE_CODE_ENTRYPOINT) /
|
|
* GSTACK_* / MCP_* / GBRAIN_* — session-context contamination; and operator
|
|
* credentials (GH_TOKEN, SSH_AUTH_SOCK, GIT_*, OPENAI_API_KEY,
|
|
* VOYAGE_API_KEY) — CI doesn't have them and eval children have no business
|
|
* using them. A test that legitimately needs one opts in via its own env
|
|
* override; a provider runner (codex/gemini) re-admits its auth vars via
|
|
* opts.extraAllow. */
|
|
const ALLOW_PREFIXES = ['EVALS_', 'GITHUB_'];
|
|
|
|
export interface HermeticEnvOpts {
|
|
/** Per-runner additional allowed names (exact match) or prefixes (entries
|
|
* ending in '*'). Example: codex runner passes ['OPENAI_API_KEY', 'CODEX_*']. */
|
|
extraAllow?: string[];
|
|
}
|
|
|
|
/** EVALS_HERMETIC !== '0'. Read at call time (see module doc — ESM hoist). */
|
|
export function isHermeticEnabled(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
return env.EVALS_HERMETIC !== '0';
|
|
}
|
|
|
|
/**
|
|
* Pure allowlist scrub. No I/O. Overrides spread LAST so per-test env
|
|
* (GSTACK_HOME, CONDUCTOR_WORKSPACE_PATH, GSTACK_HEADLESS opt-out) always
|
|
* wins over the scrub — that is the documented re-contamination escape and
|
|
* the wiring tripwire forbids passing raw process.env through it.
|
|
*/
|
|
export function buildHermeticEnv(
|
|
base: NodeJS.ProcessEnv,
|
|
hermeticVars: Record<string, string>,
|
|
overrides?: Record<string, string | undefined>,
|
|
opts?: HermeticEnvOpts,
|
|
): Record<string, string> {
|
|
if (!isHermeticEnabled(base)) {
|
|
// Escape hatch: byte-identical to the legacy spread.
|
|
const legacy: Record<string, string> = {};
|
|
for (const [k, v] of Object.entries(base)) if (v !== undefined) legacy[k] = v;
|
|
for (const [k, v] of Object.entries(overrides ?? {})) if (v !== undefined) legacy[k] = v;
|
|
return legacy;
|
|
}
|
|
|
|
const promoted = promotedEnv(base);
|
|
const extraExact = new Set<string>();
|
|
const extraPrefixes: string[] = [];
|
|
for (const entry of opts?.extraAllow ?? []) {
|
|
if (entry.endsWith('*')) extraPrefixes.push(entry.slice(0, -1));
|
|
else extraExact.add(entry);
|
|
}
|
|
|
|
const out: Record<string, string> = {};
|
|
for (const [k, v] of Object.entries(promoted)) {
|
|
if (v === undefined) continue;
|
|
const allowed =
|
|
ALLOW_EXACT.has(k) ||
|
|
extraExact.has(k) ||
|
|
ALLOW_PREFIXES.some((p) => k.startsWith(p)) ||
|
|
extraPrefixes.some((p) => k.startsWith(p));
|
|
if (allowed) out[k] = v;
|
|
}
|
|
if (!out.TERM) out.TERM = 'xterm-256color';
|
|
Object.assign(out, hermeticVars);
|
|
for (const [k, v] of Object.entries(overrides ?? {})) if (v !== undefined) out[k] = v;
|
|
return out;
|
|
}
|
|
|
|
export interface SeedConfigOpts {
|
|
/** When undefined (operator has no key exported), customApiKeyResponses is
|
|
* omitted — the child fails auth exactly as it would today, no throw here. */
|
|
apiKey: string | undefined;
|
|
trustedDirs: string[];
|
|
}
|
|
|
|
/**
|
|
* Minimal $CLAUDE_CONFIG_DIR/.claude.json for fresh-config children.
|
|
*
|
|
* Empirically verified 2026-06-12 on claude 2.1.175: PRINT MODE (`claude -p`)
|
|
* with ANTHROPIC_API_KEY needs NO seed at all — a fresh empty config dir ran
|
|
* non-interactively (exit 0, real cost billed to the key). The seed exists
|
|
* for the PTY path, where first-run TUI prompts DO appear:
|
|
* - hasCompletedOnboarding: suppresses the onboarding flow
|
|
* - customApiKeyResponses.approved: suppresses the "use this API key?"
|
|
* prompt; entries are the key's LAST 20 CHARS (shape verified against a
|
|
* real ~/.claude.json)
|
|
* - projects[dir].hasTrustDialogAccepted: pre-trusts repo-cwd PTY sessions
|
|
* (the pty-runner's 15s trust-watcher remains as fallback for temp cwds)
|
|
* bypassPermissionsModeAccepted was considered and dropped: absent from a
|
|
* real config even though --dangerously-skip-permissions is in daily use.
|
|
*/
|
|
export function buildSeedConfig(opts: SeedConfigOpts): Record<string, unknown> {
|
|
const seed: Record<string, unknown> = {
|
|
hasCompletedOnboarding: true,
|
|
projects: Object.fromEntries(
|
|
opts.trustedDirs.map((dir) => [
|
|
dir,
|
|
{ hasTrustDialogAccepted: true, hasCompletedProjectOnboarding: true },
|
|
]),
|
|
),
|
|
};
|
|
if (opts.apiKey) {
|
|
seed.customApiKeyResponses = { approved: [opts.apiKey.slice(-20)] };
|
|
}
|
|
return seed;
|
|
}
|
|
|
|
export interface HermeticDirs {
|
|
/** Ends in `/.claude` — load-bearing: extractPlanFilePath in
|
|
* claude-pty-runner.ts:191 anchors plan-file paths on `.claude/plans/`
|
|
* under a /var|/tmp prefix. Renaming this segment breaks PTY plan tests. */
|
|
configDir: string;
|
|
gstackHome: string;
|
|
runRoot: string;
|
|
}
|
|
|
|
const DIR_PREFIX = 'gstack-hermetic-';
|
|
|
|
let cachedDirs: HermeticDirs | null = null;
|
|
|
|
/** Repo root for the trusted-dir seed: test files live in <root>/test/helpers. */
|
|
function repoRoot(): string {
|
|
return path.resolve(__dirname, '..', '..');
|
|
}
|
|
|
|
/**
|
|
* Sync memoized per-process singleton — intentionally NO async gap between
|
|
* the cache check and create+seed, so concurrent first calls under
|
|
* `bun test --concurrent` cannot double-create or observe a half-seeded dir.
|
|
* Shared across all tests in the process: that matches CI's within-job
|
|
* shared /home/runner (operator isolation, not per-test isolation).
|
|
*/
|
|
export function getHermeticDirs(): HermeticDirs {
|
|
if (cachedDirs) return cachedDirs;
|
|
|
|
gcStaleHermeticDirs();
|
|
|
|
// Embed our pid so the GC of future processes can check liveness.
|
|
const runRoot = fs.mkdtempSync(path.join(os.tmpdir(), `${DIR_PREFIX}${process.pid}-`));
|
|
const configDir = path.join(runRoot, '.claude');
|
|
const gstackHome = path.join(runRoot, 'gstack-home');
|
|
|
|
// A half-seeded config dir means children hang on first-run prompts until
|
|
// the test timeout — far worse than failing loudly here. So we throw on
|
|
// failure, but tear down the partial dir first: an unseeded runRoot named
|
|
// with our (alive) pid would be skipped by this process's GC and leak until
|
|
// process exit, so remove it before rethrowing.
|
|
try {
|
|
fs.mkdirSync(configDir, { recursive: true });
|
|
fs.mkdirSync(gstackHome, { recursive: true });
|
|
const seed = buildSeedConfig({
|
|
apiKey: process.env.ANTHROPIC_API_KEY ?? process.env.GSTACK_ANTHROPIC_API_KEY,
|
|
trustedDirs: [repoRoot()],
|
|
});
|
|
fs.writeFileSync(path.join(configDir, '.claude.json'), JSON.stringify(seed, null, 2));
|
|
// Seed one-time onboarding markers into the CHILD's GSTACK_HOME.
|
|
// bin/gstack-skill-start reads ${GSTACK_HOME:-$HOME/.gstack} (EOV7), so
|
|
// the operator-HOME seeding in e2e-helpers.ts no longer reaches hermetic
|
|
// children — without these, the emission layer fires lake-intro/telemetry
|
|
// prompts that burn turns and can stall PTY tests waiting on an answer.
|
|
// Tests that exercise onboarding itself override GSTACK_HOME per-test.
|
|
for (const f of [
|
|
'.activated',
|
|
'.completeness-intro-seen',
|
|
'.telemetry-prompted',
|
|
'.proactive-prompted',
|
|
'.first-loop-tip-shown',
|
|
'.feature-prompted-continuous-checkpoint',
|
|
'.feature-prompted-model-overlay',
|
|
]) {
|
|
fs.writeFileSync(path.join(gstackHome, f), '');
|
|
}
|
|
// The privacy stop-gate is config-keyed, not marker-keyed: on machines
|
|
// with gbrain installed it fires whenever artifacts_sync_mode is off and
|
|
// the consent prompt is unrecorded — same PTY-stall class as the markers.
|
|
fs.writeFileSync(path.join(gstackHome, 'config.yaml'), 'artifacts_sync_mode_prompted: true\n');
|
|
} catch (err) {
|
|
try { fs.rmSync(runRoot, { recursive: true, force: true }); } catch { /* best-effort */ }
|
|
throw err;
|
|
}
|
|
|
|
process.on('exit', () => {
|
|
// Exit handlers cannot await: sync best-effort removal only. Anything
|
|
// left behind is reclaimed by the next process's pid-aware GC.
|
|
try { fs.rmSync(runRoot, { recursive: true, force: true }); } catch { /* GC reclaims */ }
|
|
});
|
|
|
|
cachedDirs = { configDir, gstackHome, runRoot };
|
|
return cachedDirs;
|
|
}
|
|
|
|
let cachedSkillsConfigDir: string | null = null;
|
|
|
|
/**
|
|
* Canonical paths without exposing the checkout to Claude's recursive markdown
|
|
* file index. Directory links would also expose .context, dependencies, and
|
|
* generated host registries. Expand owned runtime directories and link files
|
|
* individually, preserving their source realpaths and executable bits.
|
|
*/
|
|
export function seedHermeticRuntimeView(root: string, destination: string): void {
|
|
const sourceRoot = fs.realpathSync(root);
|
|
const excluded = new Set(['node_modules', 'test', 'tests']);
|
|
const roots = new Set([
|
|
'SKILL.md', 'ETHOS.md', 'VERSION', 'package.json', 'bin', 'lib', 'scripts',
|
|
'browse', 'browser-skills', 'design', 'extension', 'model-overlays', 'agents',
|
|
// On-demand protocol/reference files explicitly read by shipped skills.
|
|
'docs/askuserquestion-split.md', 'docs/askuserquestion-cjk.md',
|
|
'docs/designs/PLAN_TUNING_V0.md', 'docs/designs/PLAN_TUNING_V1.md',
|
|
...skillCensus(root).physicalSkillFiles.filter(file => file !== 'SKILL.md').map(file => path.dirname(file)),
|
|
]);
|
|
const allowed = [...roots].filter(name => fs.existsSync(path.join(root, name))).map(name => ({
|
|
path: fs.realpathSync(path.join(root, name)), directory: fs.statSync(path.join(root, name)).isDirectory(),
|
|
}));
|
|
function link(source: string, target: string, ancestors: Set<string>): void {
|
|
const real = fs.realpathSync(source);
|
|
if (real !== sourceRoot && !real.startsWith(sourceRoot + path.sep))
|
|
throw new Error(`Runtime asset leaves the owned checkout: ${source}`);
|
|
if (path.relative(sourceRoot, real).split(path.sep).some(name =>
|
|
name.startsWith('.') || excluded.has(name) || name.endsWith('.tmpl')))
|
|
throw new Error(`Runtime asset resolves into an excluded tree: ${source}`);
|
|
if (real !== sourceRoot && !allowed.some(asset => real === asset.path ||
|
|
asset.directory && real.startsWith(asset.path + path.sep)))
|
|
throw new Error(`Runtime asset resolves into an excluded tree: ${source}`);
|
|
const stat = fs.statSync(source);
|
|
if (stat.isDirectory()) {
|
|
if (ancestors.has(real)) throw new Error(`Circular runtime asset: ${source}`);
|
|
fs.mkdirSync(target);
|
|
const next = new Set([...ancestors, real]);
|
|
for (const name of fs.readdirSync(source)) {
|
|
if (name.startsWith('.') || excluded.has(name) || name.endsWith('.tmpl')) continue;
|
|
link(path.join(source, name), path.join(target, name), next);
|
|
}
|
|
} else if (stat.isFile()) {
|
|
fs.symlinkSync(source, target, 'file');
|
|
}
|
|
}
|
|
fs.mkdirSync(destination);
|
|
try {
|
|
for (const name of roots) {
|
|
const source = path.join(root, name);
|
|
if (fs.existsSync(source)) {
|
|
const target = path.join(destination, name);
|
|
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
link(source, target, new Set([sourceRoot]));
|
|
}
|
|
}
|
|
} catch (error) {
|
|
fs.rmSync(destination, { recursive: true, force: true });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A hermetic CLAUDE_CONFIG_DIR with the repo's shipped skills REGISTERED in
|
|
* user scope, mirroring ./setup's registration exactly: each discovered skill
|
|
* gets a REAL directory `<configDir>/skills/<registryName>/` containing a
|
|
* SYMLINK to that skill's SKILL.md (absolute path), plus symlinks to its
|
|
* runtime assets using setup's exclusions. An owned runtime view also
|
|
* lives at `<configDir>/skills/gstack`, so canonical
|
|
* lazy-section paths work alongside flattened discovery. registryName is the frontmatter `name:`
|
|
* (dir-name fallback), NO gstack- prefix; the root SKILL.md router registers
|
|
* as `_gstack-command`. skillCensus().registryEntries is the authoritative
|
|
* set of what must appear here.
|
|
*
|
|
* The default hermetic dir deliberately seeds no skills — correct for
|
|
* children that install their own or probe setup behavior — but a PTY test
|
|
* that TYPES `/office-hours` needs the slash command to exist, or claude
|
|
* rejects it as Unknown command before any model turn and the gate measures
|
|
* nothing. Separate dir, same runRoot: opt-in per session, never contaminates
|
|
* the default-config children, and the existing exit teardown + pid-aware GC
|
|
* cover it. Ends in `/.claude` for the same plan-path anchoring reason as
|
|
* HermeticDirs.configDir.
|
|
*
|
|
* Seeding reads the live repo tree: the skills are the subject under test.
|
|
* For default seeded PTY sessions, launchClaudePty also supplies an owned HOME
|
|
* via hermetic-skill-runtime so literal runtime and lazy-section paths reach
|
|
* this same checkout. Explicit HOME/config overrides remain caller-owned;
|
|
* this registration helper itself does not change their environment.
|
|
*/
|
|
export function hermeticSkillsConfigDir(): string {
|
|
if (cachedSkillsConfigDir) return cachedSkillsConfigDir;
|
|
const { runRoot } = getHermeticDirs();
|
|
const configDir = path.join(runRoot, 'with-skills', '.claude');
|
|
const skillsDir = path.join(configDir, 'skills');
|
|
fs.mkdirSync(skillsDir, { recursive: true });
|
|
fs.writeFileSync(
|
|
path.join(configDir, '.claude.json'),
|
|
JSON.stringify(buildSeedConfig({
|
|
apiKey: process.env.ANTHROPIC_API_KEY ?? process.env.GSTACK_ANTHROPIC_API_KEY,
|
|
trustedDirs: [repoRoot()],
|
|
}), null, 2),
|
|
);
|
|
const root = repoRoot();
|
|
for (const rel of skillCensus(root).physicalSkillFiles) {
|
|
const skillMd = path.join(root, rel);
|
|
const skillDir = path.dirname(rel);
|
|
const registryName = rel === 'SKILL.md'
|
|
? '_gstack-command'
|
|
: frontmatterName(skillMd) || skillDir;
|
|
const target = path.join(skillsDir, registryName);
|
|
// Idempotent overwrite mirrors setup's re-link: connect-chrome (a dir
|
|
// symlink to open-gstack-browser) shares its target's frontmatter name,
|
|
// so the two walk entries collapse to one registry dir.
|
|
fs.mkdirSync(target, { recursive: true });
|
|
safeUnlink(path.join(target, 'SKILL.md'));
|
|
fs.symlinkSync(skillMd, path.join(target, 'SKILL.md'));
|
|
if (rel !== 'SKILL.md') {
|
|
// Mirror setup's _link_skill_runtime_assets, including references and
|
|
// helpers beside sections. Missing assets can send a live agent looking
|
|
// outside its installed fixture and into the operator's stale checkout.
|
|
const source = path.join(root, skillDir);
|
|
for (const name of fs.readdirSync(source)) {
|
|
if (name.startsWith('.') || ['SKILL.md', 'node_modules', 'dist', 'test'].includes(name) || name.endsWith('.tmpl')) continue;
|
|
const asset = path.join(source, name);
|
|
if (!fs.existsSync(asset)) continue;
|
|
const destination = path.join(target, name);
|
|
safeUnlink(destination);
|
|
fs.symlinkSync(asset, destination, fs.statSync(asset).isDirectory() ? 'dir' : 'file');
|
|
}
|
|
}
|
|
}
|
|
// Canonical lazy paths remain available without letting native file-index
|
|
// discovery recursively read the source checkout's historical artifacts.
|
|
seedHermeticRuntimeView(root, path.join(skillsDir, 'gstack'));
|
|
cachedSkillsConfigDir = configDir;
|
|
return configDir;
|
|
}
|
|
|
|
/** A dir younger than this is never GC'd even if its pid looks dead — guards
|
|
* against PID reuse deleting a freshly-created dir whose original pid exited
|
|
* and was recycled to an unrelated live process between create and GC. */
|
|
const GC_MIN_AGE_MS = 60 * 60 * 1000; // 1h
|
|
|
|
/**
|
|
* Reclaim leftovers from crashed runs. Two signals, both required: the
|
|
* embedded pid is dead AND the dir is older than GC_MIN_AGE_MS. Pid-alone
|
|
* would risk PID-reuse false-deletes of live dirs; age-alone would delete a
|
|
* live >24h eval run's config out from under it. Exported for tests.
|
|
*/
|
|
export function gcStaleHermeticDirs(tmpDir: string = os.tmpdir()): void {
|
|
let entries: string[];
|
|
try { entries = fs.readdirSync(tmpDir); } catch { return; }
|
|
const now = Date.now();
|
|
for (const name of entries) {
|
|
if (!name.startsWith(DIR_PREFIX)) continue;
|
|
const pidStr = name.slice(DIR_PREFIX.length).split('-')[0];
|
|
const pid = Number(pidStr);
|
|
if (!Number.isInteger(pid) || pid <= 0) continue;
|
|
if (pid === process.pid || isProcessAlive(pid)) continue;
|
|
const full = path.join(tmpDir, name);
|
|
try {
|
|
if (now - fs.statSync(full).mtimeMs < GC_MIN_AGE_MS) continue; // too fresh
|
|
} catch { continue; } // vanished or unreadable — leave it
|
|
try { fs.rmSync(full, { recursive: true, force: true }); } catch { /* best-effort */ }
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The composition runners use: scrub process.env, point the child at the
|
|
* singleton hermetic dirs, apply per-test overrides last. Returns the legacy
|
|
* env untouched when EVALS_HERMETIC=0 (and skips dir creation entirely).
|
|
*/
|
|
export function hermeticChildEnv(
|
|
overrides?: Record<string, string | undefined>,
|
|
opts?: HermeticEnvOpts,
|
|
): Record<string, string> {
|
|
if (!isHermeticEnabled()) {
|
|
return buildHermeticEnv(process.env, {}, overrides, opts);
|
|
}
|
|
const dirs = getHermeticDirs();
|
|
return buildHermeticEnv(
|
|
process.env,
|
|
{ CLAUDE_CONFIG_DIR: dirs.configDir, GSTACK_HOME: dirs.gstackHome },
|
|
overrides,
|
|
opts,
|
|
);
|
|
}
|