Files
gstack/test/timeline-stop-hook-slug.test.ts
T
Garry Tanandcapy-ai[bot] 914e7b6fb3 Write PR titles, commit messages and branch names in plain English (GST-1825) (#4146)
* Add a check that keeps tracker codes and internal jargon out of PR titles and branch names

gstack-plain-title checks the readable front of a title, branch name, PR opening or changelog entry against the repository's .gstack/plain-titles.json; one trailing issue code stays allowed. A Plain title workflow runs it on every new pull request, and /ship's push refuses a never-pushed branch whose name fails it.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Move the PR body's machine-read lines into a closing Agent notes section

The ship receipt, its runs block and the Plan line now live in one collapsed section at the end of the body, and every reader looks there first while still accepting the old receipt-at-top layout. gstack-post refuses to post a title the plain-title check refuses, and land uses the checked PR title as main's commit title.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Teach /ship to write plain titles, PR openings, commit messages and changelog entries

The README now lists what gstack is for, so a PR opening can name the goal it serves.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Keep tests, size budgets and generated files in step with plain titles and Agent notes

Copy lists that install result-code modules now include the plain-title codes, the ship goldens and size ledger are refreshed, the new workflow is classified, and the pinned prose tests read the new title and body layout.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Record the test-profile list change as non-behavioral for the shared test harness

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Keep text people add below the PR notes, and fix three smaller review findings

The notes section now ends at its own closing tag, so a collapsible added after it survives the next post. The opening check splits the body the same way the notes reader does. Wave PR titles no longer name the area, which the plain-title check could refuse. The Capy line in the PR-body steps now puts Approvals in the notes.

Review-Finding: p1/specialist-batched#1 reader=testing
Review-Finding: p1/red-team#1 reader=red-team
Review-Finding: p1/red-team#2 reader=red-team
Review-Finding: p1/red-team#3 reader=red-team
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Mask credentials in refused-title lines and keep older PRs' plan lines readable

A refused title's printed match is masked by the secret scanner first. The Plan line reader falls back to the whole body when the Agent notes section has none, so a body written before this change keeps its plan when its first receipt lands. The notes section also ends at its own closing tag when its opener is split or marked open, or when a collapsible inside it is left unclosed.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Print a fixed placeholder when a credential in a refused title cannot be masked

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Keep a plain-title policy edit from scheduling every paid eval

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Record the paid-eval list change as non-behavioral for the shared test harness

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Recheck the PR title on every edit so a base change cannot turn a refused title green

Review-Finding: s11/targeted#1 reader=targeted
Review-Finding: s11/adversarial-native#1 reader=adversarial-native
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-11-08 00:59:38 +00:00

176 lines
6.2 KiB
TypeScript

/**
* Timeline Stop hook slug resolution (#3103).
*
* Through Git Bash on Windows, gstack-slug took 2.2-2.8 s, past the hook's 2 s
* budget, so the synchronous spawn timed out on every Stop and nothing was
* repaired. The hook now reads gstack-slug's versioned cache in-process and,
* on a miss, hands the slow resolve to a detached child under a TTL lock with
* a per-cwd negative cache.
*
* Fixture: a copy of the hook and its imports in a temp tree whose
* bin/gstack-slug is a stub (runBin resolves bin/ from the hook's own
* location). The stub logs each call, sleeps, then prints a slug or fails.
* The hook runs through Bun argv, never as a shebang.
*/
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { slugCacheFile } from '../lib/bin-context';
const ROOT = path.resolve(import.meta.dir, '..');
const HOOK_FILES = [
'hosts/claude/hooks/timeline-stop-hook.ts',
'hosts/claude/hooks/spawn-bin.ts',
'hosts/claude/hooks/hook-log.ts',
'lib/state-root.ts',
'lib/bin-context.ts',
'lib/remote-identity.ts',
'lib/script-invocation.ts',
'lib/bun-isolation.ts',
'lib/result-codes.ts',
'lib/result-codes-release.ts',
'lib/result-codes-capy.ts',
'lib/result-codes-budget.ts',
'lib/result-codes-eval.ts',
'lib/result-codes-plan.ts',
'lib/result-codes-autoplan.ts',
'lib/result-codes-browse.ts',
'lib/result-codes-clean.ts',
'lib/result-codes-plain.ts',
];
const SLUG = 'stop-hook-slug-test';
const BUDGET_MS = 2000;
let tmp: string;
let tree: string;
let stateRoot: string;
let projectDir: string;
let callLog: string;
function writeStub(body: string): void {
const stub = path.join(tree, 'bin', 'gstack-slug');
fs.mkdirSync(path.dirname(stub), { recursive: true });
fs.writeFileSync(stub, `#!/usr/bin/env bash\necho "$PWD" >> ${JSON.stringify(callLog)}\n${body}\n`, { mode: 0o755 });
}
function seedTimeline(): string {
const file = path.join(stateRoot, 'projects', SLUG, 'timeline.jsonl');
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify({ skill: 'review', event: 'started', session: '11-1' }) + '\n');
return file;
}
function repairs(file: string): number {
return fs.readFileSync(file, 'utf-8').split('\n').filter((l) => l.includes('"source":"stop-hook"')).length;
}
function runHook(): { status: number | null; ms: number } {
const env: Record<string, string | undefined> = { ...process.env, GSTACK_STATE_ROOT: stateRoot };
delete env.GSTACK_PROJECT_SLUG;
const t0 = Date.now();
const r = spawnSync(process.execPath, [path.join(tree, 'hosts', 'claude', 'hooks', 'timeline-stop-hook.ts')], {
input: JSON.stringify({ hook_event_name: 'Stop', cwd: projectDir }),
env,
timeout: 15_000,
});
return { status: r.status, ms: Date.now() - t0 };
}
function calls(): number {
return fs.existsSync(callLog) ? fs.readFileSync(callLog, 'utf-8').split('\n').filter(Boolean).length : 0;
}
const lockFile = () => path.join(stateRoot, 'timeline-stop-hook', 'repair.lock');
async function until(cond: () => boolean, timeoutMs = 20_000): Promise<void> {
const end = Date.now() + timeoutMs;
while (!cond()) {
if (Date.now() > end) throw new Error('condition not met before timeout');
await Bun.sleep(100);
}
}
beforeEach(() => {
tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-stop-slug-'));
tree = path.join(tmp, 'tree');
stateRoot = path.join(tmp, 'state');
projectDir = path.join(tmp, 'project');
callLog = path.join(tmp, 'slug-calls.log');
fs.mkdirSync(stateRoot);
fs.mkdirSync(projectDir);
for (const rel of HOOK_FILES) {
fs.mkdirSync(path.dirname(path.join(tree, rel)), { recursive: true });
fs.copyFileSync(path.join(ROOT, rel), path.join(tree, rel));
}
});
afterEach(async () => {
await until(() => !fs.existsSync(lockFile())).catch(() => {});
fs.rmSync(tmp, { recursive: true, force: true });
});
describe('timeline-stop-hook slug resolution (#3103)', () => {
test('cache hit: repairs in the foreground without spawning gstack-slug', () => {
writeStub('exit 1');
const timeline = seedTimeline();
const cache = slugCacheFile(stateRoot, projectDir);
fs.mkdirSync(path.dirname(cache), { recursive: true });
fs.writeFileSync(cache, `v2:${SLUG}`);
expect(runHook().status).toBe(0);
expect(repairs(timeline)).toBe(1);
expect(calls()).toBe(0);
expect(fs.existsSync(lockFile())).toBe(false);
});
test('cache miss with a slow gstack-slug: the hook returns within its budget and a background child repairs', async () => {
writeStub(`sleep 3\necho "SLUG=${SLUG}"\necho "BRANCH=main"`);
const timeline = seedTimeline();
const r = runHook();
expect(r.status).toBe(0);
expect(r.ms).toBeLessThan(BUDGET_MS);
expect(repairs(timeline)).toBe(0);
await until(() => repairs(timeline) === 1);
await until(() => !fs.existsSync(lockFile()));
expect(calls()).toBe(1);
expect(repairs(timeline)).toBe(1);
}, 30_000);
test('negative cache: a directory gstack-slug cannot resolve spawns one child, not one per turn', async () => {
writeStub('exit 1');
seedTimeline();
expect(runHook().status).toBe(0);
await until(() => calls() === 1 && !fs.existsSync(lockFile()));
expect(fs.readFileSync(path.join(stateRoot, 'hook-errors.log'), 'utf-8')).toContain('could not resolve project slug');
expect(runHook().status).toBe(0);
expect(runHook().status).toBe(0);
await Bun.sleep(1000);
expect(calls()).toBe(1);
}, 30_000);
test('a fresh lock defers the repair; a lock past its TTL is taken over', () => {
writeStub('exit 1');
const timeline = seedTimeline();
const cache = slugCacheFile(stateRoot, projectDir);
fs.mkdirSync(path.dirname(cache), { recursive: true });
fs.writeFileSync(cache, `v2:${SLUG}`);
fs.mkdirSync(path.dirname(lockFile()), { recursive: true });
fs.writeFileSync(lockFile(), '999999\n');
expect(runHook().status).toBe(0);
expect(repairs(timeline)).toBe(0);
const old = new Date(Date.now() - 120_000);
fs.utimesSync(lockFile(), old, old);
expect(runHook().status).toBe(0);
expect(repairs(timeline)).toBe(1);
expect(fs.existsSync(lockFile())).toBe(false);
});
});