mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-28 23:52:28 +02:00
* fix(memory-ingest): --scan-secrets scans the rendered page and fails closed --scan-secrets ran gitleaks on the raw transcript .jsonl, then imported a page rendered from it. gitleaks' assignment rules don't match across a JSON-escaped quote (KEY=\"v\" on disk), so a secret the rendered page shows as KEY="v" was imported unflagged. And the gate skipped a file only on scanner "gitleaks" with findings, so a scan that errored (non-zero exit, 16MB maxBuffer overflow on a file with many findings, unparseable report) or could not run (gitleaks missing, slow-probe cooldown) imported the file unscanned. Scan the rendered page body, the exact bytes writeStaged() writes, via a new secretScanText() helper, and skip the file whenever the scan did not complete. Skipped files stay out of the state file, so the next run retries them. Reword the helper warnings and setup-gbrain/memory.md, which described the fail-open as intended. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(test): reconcile Bun failure markers and footer counts * fix(sync-gbrain): verify source-scoped reads without mutation * fix(test): recognize grounded TTHW target choices structurally * fix(aside): make the readiness probe work under zsh and report why it failed The probe built its deadline into `_T` and expanded it unquoted, so `$_T aside repl …` only worked in a shell that word-splits. zsh does not: it looked for a command literally named "gtimeout 30", the probe answered ASIDE_NOT_RUNNING with Aside installed and ready, and every browsing skill fell back to the bundled Chromium in silence. zsh is the macOS default and Aside is macOS-only, so on a stock Mac the probe could never report READY. The deadline becomes a function, `_gs_d`. It receives the command as "$@", already split, so sh, bash and zsh all behave the same, and the gtimeout → timeout → perl alarm chain is unchanged. A 4th arm runs the call unbounded when none of the three is present, which is what the empty `_T` did before. Not `eval`: it re-parses the string, so the parens and `;` of the perl arm become syntax and that arm dies in bash *and* zsh — on a stock Mac, the arm that actually runs. On failure the probe now prints the CLI's reason after ASIDE_NOT_RUNNING:, the shape gstack-render already uses: the first line that starts with a capital letter, i.e. the CLI's own sentence or Node's `Error:` line below its loader frame. "Not running" covers states with different fixes — no window open for the profile, a NODE_OPTIONS preload that kills the CLI — and a bare verdict sent all of them to "open the Aside app". The BROWSER SETUP prose quotes that reason before asking the user to open the app. The text pin asserted the broken invocation verbatim, so it now pins the function and asserts neither `$_T aside repl` nor an eval form comes back. A second test executes the rendered probe in sh, bash and zsh on each of the four deadline arms with stubbed binaries on a narrowed PATH, plus two failing CLIs: one that prints its own sentence, one that crashes like Node with the useful line below the frame. The deadline function costs zero bytes against the lines it replaces; the reason costs 53 per copy of the probe (44 where the reworded BROWSER SETUP line gives 9 back). That moves four guards by the measured amount: plan-devex-review's skeleton cap to 68,550 (measured 68,544), plan-ceo-review's skeleton cap to 80,150 (measured 80,111) and union ratio to 1.081 (measured 1.0803), and plan-eng-review's union ratio to 1.151 (measured 1.1504). Fixes #2842, #2941. * Clarify engineering review startup and decision flow * Fix Windows readiness fixture PATH and command shim * fix(test): recognize grounded TTHW target choices structurally * Clarify engineering review startup and decision flow * fix(test): restrict QA-only fixture tools to its no-Edit contract * v1.90.0.0 fix(sync-gbrain): guard readiness verdicts and refresh metadata * fix(browse): validate canonical upload targets * fix(gbrain): classify structured PGLite busy response * fix(browse): preserve native extension runtime APIs * Fix displayless browser handoff ownership * Accept unique installed autoplan methodology aliases * fix(skills): preserve positional literals during installation * fix(browse): checksum installer contents through stdin * fix(test): normalize Windows checksum fixture paths * test: emulate unavailable shasum in Windows checksum fixture * fix(investigate): preserve owned freeze lifecycle * fix(review): preserve N+1 retry and Red Team completion * fix: bound Aside readiness and preserve safe fallback * test: exercise setup and Chromium on native ARM * fix: preserve install ownership and ARM browser selection * Fix gbrain ingest scan boundaries and seed observation * Refresh managed ship hooks and supervise expanded paid census * Reject resumed gbrain pages excluded by current policy * Recover zombie agent locks safely and enable CI Python venv * Repair paid actor declarations and Aside pitch assertions * Bump consolidated wave to next free minor release * Clarify CEO review admin choices and option tradeoffs * Preserve CEO mode handoff anchors in clarified workflow * Make Windows portability fixtures use shell-native paths * Restore ARM Bun alias and clarify ship review gates * Refresh ship workflow golden snapshots * Fix Windows DX documentation controls without piped stdin * Decode Codex child pipes without Bun's encoded-stream stall * Bound DX pre-review audit before product questions * Clarify trusted review-start read in paid revalidation * Bump consolidated wave to next free minor release * Clarify CEO review admin choices and option tradeoffs * Preserve CEO mode handoff anchors in clarified workflow * Make Windows portability fixtures use shell-native paths * Restore ARM Bun alias and clarify ship review gates * Refresh ship workflow golden snapshots * Fix Windows DX documentation controls without piped stdin * Decode Codex child pipes without Bun's encoded-stream stall * Bound DX pre-review audit before product questions * Clarify trusted review-start read in paid revalidation * Reconcile new main planning flow and paid judge census * fix: reconcile rebased planning and source-bound validation * test: pin cookie workflow judge to scored Sonnet model * fix: keep terminal agent boot out of module imports * fix: preserve pending-question uncertainty in engineering review * fix: stabilize Windows reliability-wave fixtures * fix: clarify design consultation research workflow * fix: preserve independent design consultation inputs * fix: resolve design taste scope and browser research guidance * fix: make consultation opt-in preflight unambiguous * test: await native Edge owner readiness or terminal result --------- Co-authored-by: Bruce Krysiak <brucek@alum.mit.edu> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Antonio Vitalic <antoninte99@gmail.com>
201 lines
15 KiB
TypeScript
201 lines
15 KiB
TypeScript
import * as fs from 'node:fs';
|
|
import * as os from 'node:os';
|
|
import * as path from 'node:path';
|
|
import { createHash, randomUUID } from 'node:crypto';
|
|
import { spawnSync } from 'node:child_process';
|
|
import type { CanUseTool, HookCallback, SDKMessage } from '@anthropic-ai/claude-agent-sdk';
|
|
import type { AgentSdkResult, QueryProvider } from './agent-sdk-runner';
|
|
import type { EvalTestEntry } from './eval-store';
|
|
import { CAPTURE_MS } from './eval-budgets';
|
|
import { readWorkflowExcerpt } from './workflow-excerpt';
|
|
|
|
export type ShipHookCase = 'ship-managed-hook-refresh' | 'ship-unmanaged-hook-consent' | 'ship-local-hook-preservation';
|
|
const ROOT = path.resolve(import.meta.dir, '../..');
|
|
const quote = (value: string) => `'${value.replaceAll("'", "'\"'\"'")}'`;
|
|
const read = (file: string) => fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '';
|
|
const oldHook = '#!/bin/sh\n# gstack-redact pre-push (managed)\n_input="$(cat)"\nprintf "%s" "$_input" | "$(git rev-parse --git-path hooks/pre-push.local)" "$@"\n';
|
|
const unmanagedHook = '#!/bin/sh\n# gstack-redact pre-push (managed) extra\nexit 42\n';
|
|
const localPolicy = '#!/bin/sh\ncat > "$HOME/received"\nexit 37\n';
|
|
const refs = 'refs/heads/a aaaa refs/heads/a bbbb\nrefs/heads/b cccc refs/heads/b dddd\n';
|
|
|
|
export function createShipHookFixture(id: ShipHookCase, root = fs.mkdtempSync(path.join(fs.realpathSync(os.tmpdir()), 'shook-'))) {
|
|
fs.mkdirSync(root, { recursive: true });
|
|
fs.chmodSync(root, 0o700);
|
|
const repo = path.join(root, 'project');
|
|
const home = path.join(root, 'home');
|
|
const state = path.join(root, 'state');
|
|
const installed = path.join(home, '.claude/skills/gstack/bin');
|
|
const receipts = path.join(root, 'receipts');
|
|
const protectedFiles = new Map<string, string>();
|
|
const write = (file: string, text: string, executable = false) => {
|
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
const target = fs.lstatSync(file, { throwIfNoEntry: false }) ? fs.realpathSync(file) : path.join(fs.realpathSync(path.dirname(file)), path.basename(file));
|
|
const relative = path.relative(fs.realpathSync(root), target);
|
|
if (relative.startsWith('..') || path.isAbsolute(relative)) throw new Error('fixture write escaped its temporary root');
|
|
fs.writeFileSync(file, text, { mode: executable ? 0o755 : 0o600 });
|
|
protectedFiles.set(file, text);
|
|
};
|
|
fs.mkdirSync(repo);
|
|
fs.mkdirSync(state);
|
|
const env = { HOME: home, GSTACK_HOME: state, GSTACK_STATE_ROOT: state, CLAUDE_PLUGIN_DATA: '',
|
|
CLAUDE_CONFIG_DIR: path.join(root, 'claude-config'), GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_SYSTEM: '/dev/null',
|
|
GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_COUNT: '0',
|
|
PATH: `${path.dirname(process.execPath)}:${process.env.PATH ?? '/usr/bin:/bin'}` };
|
|
const initialized = spawnSync('git', ['init', '-q', repo], { env, encoding: 'utf8', timeout: 10000 });
|
|
if (initialized.status !== 0) throw new Error(initialized.stderr);
|
|
const hook = path.join(repo, '.git/hooks/pre-push');
|
|
write(hook, id === 'ship-managed-hook-refresh' ? oldHook : unmanagedHook, true);
|
|
if (id !== 'ship-unmanaged-hook-consent') write(hook + '.local', localPolicy, true);
|
|
if (id === 'ship-managed-hook-refresh') protectedFiles.delete(hook);
|
|
write(path.join(state, 'config.yaml'), 'redact_prepush_hook: true\n');
|
|
write(path.join(installed, 'gstack-config'), `#!/bin/sh\nprintf 'CONFIG:%s\\n' "$*" >> ${quote(receipts)}\nexec bash ${quote(path.join(ROOT, 'bin/gstack-config'))} "$@"\n`, true);
|
|
write(path.join(installed, 'gstack-redact'), `#!/bin/sh\nprintf 'INSTALL:%s\\n' "$*" >> ${quote(receipts)}\nexec ${quote(process.execPath)} ${quote(path.join(ROOT, 'bin/gstack-redact'))} "$@"\n`, true);
|
|
const workflow = readWorkflowExcerpt('ship/SKILL.md', '**Credential pre-push guard', '**Idempotency check');
|
|
write(path.join(repo, 'workflow.md'), workflow);
|
|
const guard = workflow.match(/```bash\n([\s\S]*?)```/)![1].trim();
|
|
const readable = new Set([path.join(repo, 'workflow.md'), hook, hook + '.local']);
|
|
const commands = new Set([guard, 'git config --get core.hooksPath', 'git rev-parse --git-path hooks/pre-push', 'git rev-parse --git-path hooks/pre-push.local']);
|
|
const executions: Array<{ tool: string; input: Record<string, unknown>; allowed: boolean }> = [];
|
|
const interactions: Array<{ disposition: string; input: unknown }> = [];
|
|
const prompt = `Load gstack's /ship credential pre-push guard from workflow.md and run only that workflow excerpt. Inspect the actual repository, apply its rules, report the outcome, and stop before pushing.
|
|
|
|
Declared fixture interactions: Read workflow.md and the local hook files; Bash for the supplied guard block verbatim and these read-only commands: git config --get core.hooksPath, git rev-parse --git-path hooks/pre-push, git rev-parse --git-path hooks/pre-push.local. Run each listed inspection command in its own Bash call; do not chain commands or append an echo/status probe. A missing core.hooksPath setting may return exit 1 without requiring another command. One native AskUserQuestion about hook modification is supported if the workflow calls for it; the owner declines modifications. Use Read rather than shell commands to inspect files. No other command, file edit, direct receipt access, directory change, background command, commit, push, config change, installation phase or unrelated /ship phase is supported. HOME and GSTACK_HOME already belong to this fixture; do not change them.`;
|
|
const invalid = (tool: string, input: Record<string, unknown>) => {
|
|
if (tool === 'Read') return typeof input.file_path !== 'string' || !readable.has(path.resolve(repo, input.file_path)) ? 'Read outside declared fixture paths' : undefined;
|
|
if (tool === 'Bash') return typeof input.command !== 'string' || input.run_in_background || !commands.has(input.command.trim()) ? 'Bash outside declared fixture commands' : undefined;
|
|
if (tool === 'AskUserQuestion') return interactions.some(event => event.disposition === 'declined') ? 'Duplicate owner question' : undefined;
|
|
return 'Tool outside declared fixture interactions';
|
|
};
|
|
const preToolUse: HookCallback = async input => {
|
|
if (input.hook_event_name !== 'PreToolUse') throw new Error('unexpected native hook event');
|
|
const toolInput = input.tool_input as Record<string, unknown>;
|
|
const reason = invalid(input.tool_name, toolInput);
|
|
executions.push({ tool: input.tool_name, input: toolInput, allowed: !reason });
|
|
return { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: reason ? 'deny' : input.tool_name === 'AskUserQuestion' ? 'ask' : 'allow',
|
|
...(reason ? { permissionDecisionReason: reason } : {}),
|
|
...(input.tool_name === 'Bash' && !reason ? { updatedInput: { command: toolInput.command, timeout: 10000, run_in_background: false } } : {}) } };
|
|
};
|
|
const canUseTool: CanUseTool = async (tool, input) => {
|
|
const reason = invalid(tool, input);
|
|
if (reason) {
|
|
interactions.push({ disposition: 'unsupported', input });
|
|
return { behavior: 'deny', message: reason };
|
|
}
|
|
if (tool !== 'AskUserQuestion') return { behavior: 'allow', updatedInput: input };
|
|
const questions = input.questions as Array<{ question: string; multiSelect?: boolean; options: Array<{ label: string; description: string }> }>;
|
|
const question = questions?.[0];
|
|
const decline = question?.options?.find(option => /^(?:[A-D][).]\s*)?(no\b|decline\b|do not\b|skip\b|leave\b|keep\b)/i.test(option.label));
|
|
if (questions?.length !== 1 || question.multiSelect || !/hook|guard|chain|credential/i.test(question.question) || !decline) {
|
|
interactions.push({ disposition: 'unsupported', input });
|
|
return { behavior: 'deny', message: 'Only one hook modification question with a decline option is supported.' };
|
|
}
|
|
interactions.push({ disposition: 'declined', input });
|
|
return { behavior: 'allow', updatedInput: { ...input, answers: { [question.question]: decline.label } } };
|
|
};
|
|
const snapshot = () => ({ receipts: read(receipts), hook: read(hook), localHook: read(hook + '.local'), localExists: fs.existsSync(hook + '.local'),
|
|
executions, interactions, changedProtectedFiles: [...protectedFiles].filter(([file, bytes]) => read(file) !== bytes).map(([file]) => path.relative(root, file)),
|
|
workflowSha256: createHash('sha256').update(workflow).digest('hex') });
|
|
return { id, root, repo, home, env, hook, receipts, guard, prompt, preToolUse, canUseTool, snapshot };
|
|
}
|
|
|
|
export function shipHookFailures(fixture: ReturnType<typeof createShipHookFixture>, result: Pick<AgentSdkResult, 'exitReason' | 'output'>) {
|
|
const evidence = fixture.snapshot();
|
|
const failures: string[] = [];
|
|
const check = (ok: boolean, message: string) => { if (!ok) failures.push(message); };
|
|
check(result.exitReason === 'success', `actor ended: ${result.exitReason}`);
|
|
check(evidence.changedProtectedFiles.length === 0, 'protected policy or fixture files changed');
|
|
check(!evidence.executions.some(event => !event.allowed) && !evidence.interactions.some(event => event.disposition === 'unsupported'), 'undeclared interaction');
|
|
check(evidence.executions.some(event => event.tool === 'Read' && event.allowed && path.resolve(fixture.repo, event.input.file_path as string) === path.join(fixture.repo, 'workflow.md')), 'workflow was not read through native hook');
|
|
check(evidence.executions.some(event => event.tool === 'Bash' && event.allowed && (event.input.command as string).trim() === fixture.guard), 'guard was not executed through native hook');
|
|
check(evidence.receipts.includes('CONFIG:get redact_prepush_hook\n'), 'actual config helper was not called');
|
|
const installs = evidence.receipts.match(/^INSTALL:install-prepush-hook$/gm)?.length ?? 0;
|
|
let callback: { status: number | null; received: string } | undefined;
|
|
if (fixture.id === 'ship-managed-hook-refresh') {
|
|
check(installs === 1, 'expected exactly one actual installer call');
|
|
check(evidence.hook !== oldHook && evidence.hook.includes('_input="$(cat; printf x)"'), 'managed wrapper was not refreshed');
|
|
check(evidence.localHook === localPolicy, 'local policy changed');
|
|
check(evidence.interactions.length === 0, 'managed refresh unnecessarily requested consent');
|
|
const invoked = spawnSync('bash', [fixture.hook, 'origin', 'synthetic'], { cwd: fixture.repo, env: fixture.env, input: refs, encoding: 'utf8', timeout: 10000 });
|
|
callback = { status: invoked.status, received: read(path.join(fixture.home, 'received')) };
|
|
check(callback.status === 37 && callback.received === refs, 'refreshed wrapper lost local policy status or complete stdin');
|
|
check(/refresh|updat|install|current/i.test(result.output), 'refresh outcome was not acknowledged');
|
|
} else {
|
|
check(installs === 0, 'installer invoked without applicable consent');
|
|
check(evidence.hook === unmanagedHook, 'unmanaged policy changed');
|
|
if (fixture.id === 'ship-unmanaged-hook-consent') {
|
|
check(evidence.interactions.filter(event => event.disposition === 'declined').length === 1, 'unmanaged-hook consent was not requested');
|
|
check(!evidence.localExists, 'declined install created a local policy');
|
|
check(/declin|unchanged|not install|not modif|preserv|left|leave/i.test(result.output), 'declined modification was not acknowledged');
|
|
} else {
|
|
check(evidence.localHook === localPolicy, 'existing local policy changed');
|
|
check(evidence.interactions.length === 0, 'existing local policy requires manual integration, not consent to overwrite');
|
|
check(/manual/i.test(result.output), 'manual integration was not reported');
|
|
}
|
|
}
|
|
return { failures, evidence: { ...evidence, callback } };
|
|
}
|
|
|
|
export async function runShipHookActor(id: ShipHookCase, record: (entry: EvalTestEntry) => void, injectedQuery?: QueryProvider, artifactDirectory?: string) {
|
|
const artifacts = artifactDirectory ?? process.env.GSTACK_EVAL_DIR;
|
|
if (!artifacts) throw new Error('ship hook actor requires an explicit GSTACK_EVAL_DIR for durable evidence');
|
|
fs.mkdirSync(artifacts, { recursive: true, mode: 0o700 });
|
|
const { query } = await import('@anthropic-ai/claude-agent-sdk');
|
|
const { runAgentSdkTest, resolveClaudeBinary } = await import('./agent-sdk-runner');
|
|
let fixture = createShipHookFixture(id);
|
|
const started = Date.now();
|
|
const attempts: Array<{ events: SDKMessage[]; evidence?: ReturnType<typeof fixture.snapshot> }> = [];
|
|
let result: AgentSdkResult | undefined;
|
|
let finalEvidence: unknown;
|
|
let failure: unknown;
|
|
let passed = false;
|
|
const evidenceFile = path.join(artifacts, `${id}-${randomUUID()}.json`);
|
|
try {
|
|
const binary = injectedQuery ? undefined : resolveClaudeBinary();
|
|
if (!injectedQuery && !binary) throw new Error('native ship hook actor requires the pinned Claude CLI');
|
|
result = await runAgentSdkTest({ systemPrompt: { type: 'preset', preset: 'claude_code' }, userPrompt: fixture.prompt,
|
|
workingDirectory: fixture.repo, env: fixture.env, maxTurns: 12, signal: AbortSignal.timeout(CAPTURE_MS - 20000),
|
|
allowedTools: ['Read', 'Bash', 'AskUserQuestion'], settingSources: [], testName: id,
|
|
pathToClaudeCodeExecutable: binary ?? undefined,
|
|
canUseTool: (...args) => fixture.canUseTool(...args),
|
|
onRetry: () => {
|
|
attempts.at(-1)!.evidence = fixture.snapshot();
|
|
const root = fixture.root;
|
|
fs.rmSync(root, { recursive: true, force: true });
|
|
fixture = createShipHookFixture(id, root);
|
|
},
|
|
queryProvider: options => {
|
|
const attempt: typeof attempts[number] = { events: [] };
|
|
attempts.push(attempt);
|
|
const stream = (injectedQuery ?? query)({ ...options, options: { ...options.options, allowedTools: [],
|
|
hooks: { PreToolUse: [{ hooks: [(...args) => fixture.preToolUse(...args)] }] } } });
|
|
return new Proxy(stream, { get(target, property) {
|
|
if (property === Symbol.asyncIterator) return async function* () {
|
|
try { for await (const event of target) { attempt.events.push(event); yield event; } }
|
|
finally { attempt.evidence = fixture.snapshot(); }
|
|
};
|
|
const value = Reflect.get(target, property, target);
|
|
return typeof value === 'function' ? value.bind(target) : value;
|
|
} });
|
|
},
|
|
});
|
|
const verdict = shipHookFailures(fixture, result);
|
|
finalEvidence = verdict.evidence;
|
|
if (verdict.failures.length) throw new Error(verdict.failures.join('; '));
|
|
passed = true;
|
|
} catch (error) {
|
|
failure = error;
|
|
throw error;
|
|
} finally {
|
|
try {
|
|
const output = JSON.stringify({ assistant: result?.output, error: failure === undefined ? undefined : String(failure),
|
|
evidence: finalEvidence ?? fixture.snapshot(), attempts });
|
|
fs.writeFileSync(evidenceFile, output + '\n', { mode: 0o600 });
|
|
record({ name: id, suite: 'ship-hook-boundary', tier: 'e2e', passed, duration_ms: Date.now() - started,
|
|
cost_usd: result?.costUsd ?? 0, transcript: result?.events, prompt: fixture.prompt, turns_used: result?.turnsUsed,
|
|
model: result?.model, output, error: failure === undefined ? undefined : String(failure),
|
|
exit_reason: passed ? 'success' : result?.exitReason === 'success' ? 'assertion_failed' : result?.exitReason ?? 'runner_error' });
|
|
} finally { fs.rmSync(fixture.root, { recursive: true, force: true }); }
|
|
}
|
|
return evidenceFile;
|
|
}
|