bastion-gate 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +111 -2
- package/action.yml +10 -0
- package/dist/action.js +11 -0
- package/dist/cli.js +184 -0
- package/dist/commands/audit.js +63 -0
- package/dist/commands/clean.js +143 -0
- package/dist/commands/gate.js +108 -0
- package/dist/commands/init.js +143 -0
- package/dist/commands/lock.js +146 -0
- package/dist/commands/mutate.js +154 -0
- package/dist/commands/plan.js +163 -0
- package/dist/commands/run.js +305 -0
- package/dist/config.js +130 -0
- package/dist/git.js +75 -0
- package/dist/orchestrator/dag.js +53 -0
- package/dist/orchestrator/worktree.js +36 -0
- package/dist/runner.js +136 -0
- package/dist/test-names.js +36 -0
- package/dist/version.js +25 -0
- package/package.json +50 -4
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { CONFIG_REL_PATH, createDefaultConfig, writeConfigToDisk, } from '../config.js';
|
|
4
|
+
import { execGit, getRepoRoot } from '../git.js';
|
|
5
|
+
const AGENTS_SNIPPET = `
|
|
6
|
+
<!-- bastion-gate:start -->
|
|
7
|
+
## Test Gate (Bastion)
|
|
8
|
+
|
|
9
|
+
- Tests and test-related configs/fixtures are locked by Bastion (\`.bastion/lock.json\`).
|
|
10
|
+
- **Do not** modify locked test files, test configs, fixtures, or \`.bastion/\` to make checks pass.
|
|
11
|
+
- If a locked test itself has a defect, stop and report it as \`test_defect\` for human confirmation.
|
|
12
|
+
- Before claiming work is done, run \`npx bastion-gate gate\` (or \`bastion gate\`).
|
|
13
|
+
<!-- bastion-gate:end -->
|
|
14
|
+
`;
|
|
15
|
+
const SKILL_MD = `---
|
|
16
|
+
name: bastion-gate
|
|
17
|
+
description: Enforce red-first test locking and immutable test gates with Bastion. Use before implementing features or claiming completion.
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
# Bastion Gate Workflow
|
|
21
|
+
|
|
22
|
+
1. **Red-first lock**: Write failing acceptance tests first, commit them to the baseline branch, and run \`bastion lock\` (must fail on baseline to be accepted).
|
|
23
|
+
2. **Implement without touching tests**: Write production code only. Never edit locked tests, test configs, fixtures, or \`.bastion/\`.
|
|
24
|
+
3. **Verify through the gate**: Run \`bastion gate\` before finishing. CI will re-run \`bastion gate\` against the baseline branch; local passes alone do not merge.
|
|
25
|
+
4. **Test defects**: If a locked test is genuinely wrong, stop and ask a human to confirm \`bastion lock --amend --reason "..."\`.
|
|
26
|
+
`;
|
|
27
|
+
function buildWorkflowYaml(repoRoot, baseBranch, setupCommand) {
|
|
28
|
+
const setupSteps = [];
|
|
29
|
+
if (fs.existsSync(path.join(repoRoot, 'package.json'))) {
|
|
30
|
+
setupSteps.push(` - uses: actions/setup-node@v4`, ` with:`, ` node-version: '20'`, ` - name: Install dependencies`, ` run: ${setupCommand ?? (fs.existsSync(path.join(repoRoot, 'package-lock.json')) ? 'npm ci' : 'npm install')}`);
|
|
31
|
+
}
|
|
32
|
+
else if (fs.existsSync(path.join(repoRoot, 'pyproject.toml')) ||
|
|
33
|
+
fs.existsSync(path.join(repoRoot, 'requirements.txt'))) {
|
|
34
|
+
const pyInstall = setupCommand ?? (fs.existsSync(path.join(repoRoot, 'requirements.txt')) ? 'pip install -r requirements.txt' : 'pip install -e .');
|
|
35
|
+
setupSteps.push(` - uses: actions/setup-python@v5`, ` with:`, ` python-version: '3.12'`, ` - name: Install dependencies`, ` run: ${pyInstall}`);
|
|
36
|
+
}
|
|
37
|
+
else if (setupCommand) {
|
|
38
|
+
setupSteps.push(` - name: Setup`, ` run: ${setupCommand}`);
|
|
39
|
+
}
|
|
40
|
+
const setupBlock = setupSteps.length > 0 ? setupSteps.join('\n') + '\n' : '';
|
|
41
|
+
return `name: Bastion Gate
|
|
42
|
+
|
|
43
|
+
on:
|
|
44
|
+
pull_request:
|
|
45
|
+
branches: [ ${baseBranch} ]
|
|
46
|
+
|
|
47
|
+
jobs:
|
|
48
|
+
bastion-gate:
|
|
49
|
+
runs-on: ubuntu-latest
|
|
50
|
+
steps:
|
|
51
|
+
- uses: actions/checkout@v4
|
|
52
|
+
with:
|
|
53
|
+
fetch-depth: 0
|
|
54
|
+
${setupBlock} - name: Run Bastion Gate
|
|
55
|
+
uses: maomuxz/bastion-gate@v0
|
|
56
|
+
with:
|
|
57
|
+
base: "origin/\${{ github.base_ref }}"
|
|
58
|
+
`;
|
|
59
|
+
}
|
|
60
|
+
function detectTestCommand(repoRoot) {
|
|
61
|
+
const probes = [
|
|
62
|
+
['pyproject.toml', 'pytest'],
|
|
63
|
+
['pytest.ini', 'pytest'],
|
|
64
|
+
['go.mod', 'go test ./...'],
|
|
65
|
+
['Cargo.toml', 'cargo test'],
|
|
66
|
+
['pubspec.yaml', 'flutter test'],
|
|
67
|
+
];
|
|
68
|
+
for (const [marker, cmd] of probes) {
|
|
69
|
+
if (fs.existsSync(path.join(repoRoot, marker)))
|
|
70
|
+
return cmd;
|
|
71
|
+
}
|
|
72
|
+
return 'npm test';
|
|
73
|
+
}
|
|
74
|
+
function detectBaseBranch(repoRoot) {
|
|
75
|
+
const res = execGit(['symbolic-ref', '--short', 'HEAD'], repoRoot);
|
|
76
|
+
if (res.ok && res.stdout.trim())
|
|
77
|
+
return res.stdout.trim();
|
|
78
|
+
return 'main';
|
|
79
|
+
}
|
|
80
|
+
function ensureGitignoreEntries(repoRoot) {
|
|
81
|
+
const gitignorePath = path.join(repoRoot, '.gitignore');
|
|
82
|
+
const entries = ['.bastion/worktrees/', '.bastion/logs/', '.bastion/receipts/', '.bastion/run-state.json', '.bastion/run.pid'];
|
|
83
|
+
const existing = fs.existsSync(gitignorePath) ? fs.readFileSync(gitignorePath, 'utf8') : '';
|
|
84
|
+
const missing = entries.filter((e) => !existing.includes(e));
|
|
85
|
+
if (missing.length === 0)
|
|
86
|
+
return;
|
|
87
|
+
const prefix = existing && !existing.endsWith('\n') ? '\n' : '';
|
|
88
|
+
fs.writeFileSync(gitignorePath, existing + prefix + missing.join('\n') + '\n', 'utf8');
|
|
89
|
+
console.log(`[bastion] Updated .gitignore (${missing.join(', ')})`);
|
|
90
|
+
}
|
|
91
|
+
export function runInit(opts) {
|
|
92
|
+
const repoRoot = getRepoRoot(opts.cwd) ?? path.resolve(opts.cwd);
|
|
93
|
+
const configPath = path.join(repoRoot, CONFIG_REL_PATH);
|
|
94
|
+
const resolvedBase = opts.baseBranch ?? detectBaseBranch(repoRoot);
|
|
95
|
+
const resolvedTestCmd = opts.testCommand ?? detectTestCommand(repoRoot);
|
|
96
|
+
if (fs.existsSync(configPath) && !opts.force) {
|
|
97
|
+
console.log(`[bastion] ${CONFIG_REL_PATH} already exists (use --force to overwrite).`);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const config = createDefaultConfig({
|
|
101
|
+
testCommand: resolvedTestCmd,
|
|
102
|
+
baseBranch: resolvedBase,
|
|
103
|
+
});
|
|
104
|
+
writeConfigToDisk(repoRoot, config);
|
|
105
|
+
console.log(`[bastion] Created ${CONFIG_REL_PATH}`);
|
|
106
|
+
}
|
|
107
|
+
ensureGitignoreEntries(repoRoot);
|
|
108
|
+
// Write or append AGENTS.md
|
|
109
|
+
const agentsPath = path.join(repoRoot, 'AGENTS.md');
|
|
110
|
+
if (!fs.existsSync(agentsPath)) {
|
|
111
|
+
fs.writeFileSync(agentsPath, AGENTS_SNIPPET.trimStart(), 'utf8');
|
|
112
|
+
console.log('[bastion] Created AGENTS.md');
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const existing = fs.readFileSync(agentsPath, 'utf8');
|
|
116
|
+
if (!existing.includes('<!-- bastion-gate:start -->')) {
|
|
117
|
+
fs.writeFileSync(agentsPath, existing.trimEnd() + '\n\n' + AGENTS_SNIPPET.trimStart(), 'utf8');
|
|
118
|
+
console.log('[bastion] Appended Bastion section to AGENTS.md');
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
console.log('[bastion] AGENTS.md already contains Bastion section.');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Write universal SKILL.md
|
|
125
|
+
const skillPath = path.join(repoRoot, '.agents', 'skills', 'bastion-gate', 'SKILL.md');
|
|
126
|
+
if (!fs.existsSync(skillPath) || opts.force) {
|
|
127
|
+
fs.mkdirSync(path.dirname(skillPath), { recursive: true });
|
|
128
|
+
fs.writeFileSync(skillPath, SKILL_MD, 'utf8');
|
|
129
|
+
console.log('[bastion] Created .agents/skills/bastion-gate/SKILL.md');
|
|
130
|
+
}
|
|
131
|
+
// Write GitHub Action workflow template
|
|
132
|
+
const workflowPath = path.join(repoRoot, '.github', 'workflows', 'bastion.yml');
|
|
133
|
+
if (!fs.existsSync(workflowPath) || opts.force) {
|
|
134
|
+
fs.mkdirSync(path.dirname(workflowPath), { recursive: true });
|
|
135
|
+
fs.writeFileSync(workflowPath, buildWorkflowYaml(repoRoot, resolvedBase), 'utf8');
|
|
136
|
+
console.log('[bastion] Created .github/workflows/bastion.yml');
|
|
137
|
+
}
|
|
138
|
+
console.log('\nNext steps:');
|
|
139
|
+
console.log(' 1. Commit .bastion/config.json, tests, and .github/workflows/bastion.yml to your baseline branch.');
|
|
140
|
+
console.log(' 2. Run "bastion lock" while tests are RED on the baseline.');
|
|
141
|
+
console.log(' 3. Commit .bastion/lock.json and enable branch protection requiring the Bastion Gate CI check.');
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { CONFIG_REL_PATH, LOCK_REL_PATH, readConfigFromDisk, readLockFromDisk, writeLockToDisk, } from '../config.js';
|
|
4
|
+
import { getDirtyFiles, getRepoRoot, resolveRef } from '../git.js';
|
|
5
|
+
import { matchesAnyGlob, runTestCommand, sha256File, walkRepoFiles } from '../runner.js';
|
|
6
|
+
import { extractTestNames } from '../test-names.js';
|
|
7
|
+
const FAKE_RED_PATTERN = /(?:SyntaxError:|IndentationError:|ImportError:|ModuleNotFoundError:|ERR_MODULE_NOT_FOUND|Cannot find module ['"]|Cannot find package ['"])/;
|
|
8
|
+
export function runLock(opts) {
|
|
9
|
+
const repoRoot = getRepoRoot(opts.cwd);
|
|
10
|
+
if (!repoRoot) {
|
|
11
|
+
console.error('[bastion] Error: not inside a git repository.');
|
|
12
|
+
return 1;
|
|
13
|
+
}
|
|
14
|
+
const config = readConfigFromDisk(repoRoot);
|
|
15
|
+
const baseRef = opts.baseRef ?? 'HEAD';
|
|
16
|
+
const baseCommit = resolveRef(baseRef, repoRoot);
|
|
17
|
+
if (!baseCommit) {
|
|
18
|
+
console.error(`[bastion] Error: cannot resolve baseline ref "${baseRef}". Commit your baseline first.`);
|
|
19
|
+
return 1;
|
|
20
|
+
}
|
|
21
|
+
// Check if amending an existing lock
|
|
22
|
+
let existingLock = null;
|
|
23
|
+
if (fs.existsSync(path.join(repoRoot, LOCK_REL_PATH))) {
|
|
24
|
+
existingLock = readLockFromDisk(repoRoot);
|
|
25
|
+
if (!opts.amend) {
|
|
26
|
+
console.error(`[bastion] Error: ${LOCK_REL_PATH} already exists.`);
|
|
27
|
+
console.error(' Modifying a lock is a human-confirmed test_defect action.');
|
|
28
|
+
console.error(' If the test itself is defective, run: bastion lock --amend --reason "<why>"');
|
|
29
|
+
return 1;
|
|
30
|
+
}
|
|
31
|
+
if (!opts.reason || !opts.reason.trim()) {
|
|
32
|
+
console.error('[bastion] Error: --amend requires a non-empty --reason "<why>".');
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// Collect all files matching lockedGlobs, extraLockedPaths, and explicit CLI args
|
|
37
|
+
const allRepoFiles = walkRepoFiles(repoRoot);
|
|
38
|
+
const explicitFiles = (opts.extraFiles ?? []).map((f) => f.replace(/\\/g, '/').replace(/^\.\//, ''));
|
|
39
|
+
const patterns = [...config.lockedGlobs, ...config.extraLockedPaths];
|
|
40
|
+
const lockedSet = new Set();
|
|
41
|
+
for (const rel of allRepoFiles) {
|
|
42
|
+
if (matchesAnyGlob(rel, patterns) || explicitFiles.includes(rel)) {
|
|
43
|
+
lockedSet.add(rel);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (const rel of explicitFiles) {
|
|
47
|
+
if (!fs.existsSync(path.join(repoRoot, rel))) {
|
|
48
|
+
console.error(`[bastion] Error: specified file does not exist: ${rel}`);
|
|
49
|
+
return 1;
|
|
50
|
+
}
|
|
51
|
+
lockedSet.add(rel);
|
|
52
|
+
}
|
|
53
|
+
const lockedPaths = [...lockedSet].sort();
|
|
54
|
+
if (lockedPaths.length === 0) {
|
|
55
|
+
console.error('[bastion] Error: no test or config files matched lockedGlobs / extraLockedPaths.');
|
|
56
|
+
return 1;
|
|
57
|
+
}
|
|
58
|
+
// Ensure config and locked files are committed cleanly to baseline (unless --allow-dirty)
|
|
59
|
+
if (!opts.allowDirty) {
|
|
60
|
+
const dirty = new Set(getDirtyFiles(repoRoot));
|
|
61
|
+
const dirtyLocked = [CONFIG_REL_PATH, ...lockedPaths].filter((f) => dirty.has(f));
|
|
62
|
+
if (dirtyLocked.length > 0) {
|
|
63
|
+
console.error('[bastion] Error: locked files or config have uncommitted changes:');
|
|
64
|
+
for (const f of dirtyLocked)
|
|
65
|
+
console.error(` - ${f}`);
|
|
66
|
+
console.error(' Commit them to the baseline branch first so "gate" can verify against git history.');
|
|
67
|
+
return 1;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// RED-FIRST VERIFICATION: tests MUST FAIL on the baseline before locking
|
|
71
|
+
console.log(`[bastion] Running red-first check: ${config.testCommand}`);
|
|
72
|
+
const run = runTestCommand(config.testCommand, repoRoot);
|
|
73
|
+
if (run.exitCode === 0) {
|
|
74
|
+
console.error('\n[REJECTED] Red-first check failed: tests PASSED (exit 0) on the baseline!');
|
|
75
|
+
console.error(' A test that already passes before implementation is either a tautology or not testing new work.');
|
|
76
|
+
console.error(' Bastion refuses to lock green tests. Write a failing test first, then run "bastion lock".');
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
const combinedOutput = `${run.stdout}\n${run.stderr}`;
|
|
80
|
+
if (!opts.allowImportError && FAKE_RED_PATTERN.test(combinedOutput)) {
|
|
81
|
+
console.error('\n[REJECTED] Fake-red detected: tests failed due to SyntaxError or missing module/import, not a failing assertion!');
|
|
82
|
+
console.error(' Create stub exports/signatures first so the test fails on an assertion, then run "bastion lock".');
|
|
83
|
+
return 1;
|
|
84
|
+
}
|
|
85
|
+
console.log(`[bastion] Red-first verified (exit ${run.exitCode} in ${run.durationMs}ms). Locking ${lockedPaths.length} file(s)...`);
|
|
86
|
+
const lockedFiles = lockedPaths.map((rel) => ({
|
|
87
|
+
path: rel,
|
|
88
|
+
sha256: sha256File(path.join(repoRoot, rel)),
|
|
89
|
+
}));
|
|
90
|
+
const testSummary = lockedPaths
|
|
91
|
+
.filter((rel) => matchesAnyGlob(rel, config.lockedGlobs))
|
|
92
|
+
.map((rel) => {
|
|
93
|
+
const content = fs.readFileSync(path.join(repoRoot, rel), 'utf8');
|
|
94
|
+
return { file: rel, names: extractTestNames(content) };
|
|
95
|
+
})
|
|
96
|
+
.filter((entry) => entry.names.length > 0);
|
|
97
|
+
const amendments = existingLock?.amendments ? [...existingLock.amendments] : [];
|
|
98
|
+
if (existingLock && opts.amend && opts.reason) {
|
|
99
|
+
amendments.push({
|
|
100
|
+
at: new Date().toISOString(),
|
|
101
|
+
reason: opts.reason.trim(),
|
|
102
|
+
previousBaseCommit: existingLock.baseCommit,
|
|
103
|
+
newBaseCommit: baseCommit,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const lock = {
|
|
107
|
+
version: 1,
|
|
108
|
+
lockedAt: new Date().toISOString(),
|
|
109
|
+
baseCommit,
|
|
110
|
+
testCommand: config.testCommand,
|
|
111
|
+
redVerified: {
|
|
112
|
+
exitCode: run.exitCode,
|
|
113
|
+
command: config.testCommand,
|
|
114
|
+
},
|
|
115
|
+
lockedFiles,
|
|
116
|
+
testSummary,
|
|
117
|
+
...(amendments.length > 0 ? { amendments } : {}),
|
|
118
|
+
};
|
|
119
|
+
writeLockToDisk(repoRoot, lock);
|
|
120
|
+
console.log(`[bastion] Wrote ${LOCK_REL_PATH} (baseCommit: ${baseCommit.slice(0, 8)}).`);
|
|
121
|
+
if (testSummary.length > 0) {
|
|
122
|
+
console.log('\nLocked test cases (plain-language scan):');
|
|
123
|
+
for (const entry of testSummary) {
|
|
124
|
+
console.log(` ${entry.file}:`);
|
|
125
|
+
for (const name of entry.names) {
|
|
126
|
+
console.log(` - ${name}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const acceptancePath = path.join(repoRoot, '.bastion', 'acceptance.md');
|
|
131
|
+
if (fs.existsSync(acceptancePath)) {
|
|
132
|
+
const accLines = fs
|
|
133
|
+
.readFileSync(acceptancePath, 'utf8')
|
|
134
|
+
.split(/\r?\n/)
|
|
135
|
+
.map((l) => l.trim())
|
|
136
|
+
.filter((l) => /^[-*\d.]+\s+/.test(l));
|
|
137
|
+
if (accLines.length > 0) {
|
|
138
|
+
const totalTests = testSummary.reduce((acc, s) => acc + s.names.length, 0);
|
|
139
|
+
console.log(`\nAcceptance checklist (.bastion/acceptance.md: ${accLines.length} item(s) vs ${totalTests} locked test(s)):`);
|
|
140
|
+
for (const item of accLines)
|
|
141
|
+
console.log(` ${item}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
console.log('\nRemember: commit .bastion/lock.json to your baseline branch before agents start coding.');
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { readConfigFromDisk } from '../config.js';
|
|
4
|
+
import { getDirtyFiles, getRepoRoot, resolveRef, toPosixPath } from '../git.js';
|
|
5
|
+
import { createNodeWorktree, removeNodeWorktree } from '../orchestrator/worktree.js';
|
|
6
|
+
import { matchesAnyGlob, runTestCommand, walkRepoFiles } from '../runner.js';
|
|
7
|
+
const SOURCE_EXTENSIONS = new Set(['.ts', '.js', '.mjs', '.cjs', '.py', '.go', '.rs', '.dart']);
|
|
8
|
+
const DEFAULT_MUTATE_EXCLUDE = [
|
|
9
|
+
'scripts/**',
|
|
10
|
+
'examples/**',
|
|
11
|
+
'demo/**',
|
|
12
|
+
'docs/**',
|
|
13
|
+
'dist/**',
|
|
14
|
+
'dist-test/**',
|
|
15
|
+
'build/**',
|
|
16
|
+
'vendor/**',
|
|
17
|
+
'.bastion/**',
|
|
18
|
+
'**/*.config.*',
|
|
19
|
+
];
|
|
20
|
+
const MUTATION_RULES = [
|
|
21
|
+
{ pattern: /===/, replace: '!==', label: '=== -> !==' },
|
|
22
|
+
{ pattern: /!==/, replace: '===', label: '!== -> ===' },
|
|
23
|
+
{ pattern: />=/, replace: '<', label: '>= -> <' },
|
|
24
|
+
{ pattern: /<=/, replace: '>', label: '<= -> >' },
|
|
25
|
+
{ pattern: /\btrue\b/, replace: 'false', label: 'true -> false' },
|
|
26
|
+
{ pattern: /\bfalse\b/, replace: 'true', label: 'false -> true' },
|
|
27
|
+
];
|
|
28
|
+
function collectMutantsForFile(relPath, content) {
|
|
29
|
+
const lines = content.split('\n');
|
|
30
|
+
const mutants = [];
|
|
31
|
+
for (let i = 0; i < lines.length; i++) {
|
|
32
|
+
const line = lines[i];
|
|
33
|
+
const trimmed = line.trim();
|
|
34
|
+
if (!trimmed || trimmed.startsWith('//') || trimmed.startsWith('#') || trimmed.startsWith('import ')) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
for (const rule of MUTATION_RULES) {
|
|
38
|
+
if (!rule.pattern.test(line))
|
|
39
|
+
continue;
|
|
40
|
+
const mutatedLine = line.replace(rule.pattern, rule.replace);
|
|
41
|
+
if (mutatedLine === line)
|
|
42
|
+
continue;
|
|
43
|
+
const copy = [...lines];
|
|
44
|
+
copy[i] = mutatedLine;
|
|
45
|
+
mutants.push({
|
|
46
|
+
file: relPath,
|
|
47
|
+
line: i + 1,
|
|
48
|
+
description: rule.label,
|
|
49
|
+
mutatedContent: copy.join('\n'),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return mutants;
|
|
54
|
+
}
|
|
55
|
+
/** Pick up to `max` mutants evenly across files (round-robin) so one file cannot starve others. */
|
|
56
|
+
function sampleMutantsRoundRobin(perFile, max) {
|
|
57
|
+
const selected = [];
|
|
58
|
+
const cursors = new Array(perFile.length).fill(0);
|
|
59
|
+
let progressed = true;
|
|
60
|
+
while (selected.length < max && progressed) {
|
|
61
|
+
progressed = false;
|
|
62
|
+
for (let i = 0; i < perFile.length && selected.length < max; i++) {
|
|
63
|
+
if (cursors[i] < perFile[i].length) {
|
|
64
|
+
selected.push(perFile[i][cursors[i]++]);
|
|
65
|
+
progressed = true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return selected;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Lightweight mutation spot-check: runs inside an isolated temporary worktree
|
|
73
|
+
* (when clean git HEAD exists) so user source files are never left dirty even on Ctrl+C.
|
|
74
|
+
*/
|
|
75
|
+
export function runMutate(opts) {
|
|
76
|
+
const repoRoot = getRepoRoot(opts.cwd) ?? path.resolve(opts.cwd);
|
|
77
|
+
const config = readConfigFromDisk(repoRoot);
|
|
78
|
+
const maxMutations = opts.maxMutations ?? 10;
|
|
79
|
+
const lockedPatterns = [...config.lockedGlobs, ...config.extraLockedPaths];
|
|
80
|
+
const excludePatterns = [...DEFAULT_MUTATE_EXCLUDE, ...(config.mutateExclude ?? [])];
|
|
81
|
+
const candidateFiles = opts.files && opts.files.length > 0
|
|
82
|
+
? opts.files.map(toPosixPath)
|
|
83
|
+
: walkRepoFiles(repoRoot).filter((rel) => {
|
|
84
|
+
const ext = path.extname(rel);
|
|
85
|
+
return (SOURCE_EXTENSIONS.has(ext) &&
|
|
86
|
+
!matchesAnyGlob(rel, excludePatterns) &&
|
|
87
|
+
!matchesAnyGlob(rel, lockedPatterns));
|
|
88
|
+
});
|
|
89
|
+
// Use an isolated worktree if git HEAD exists and working tree is clean
|
|
90
|
+
const head = resolveRef('HEAD', repoRoot);
|
|
91
|
+
const canUseWorktree = Boolean(head && getDirtyFiles(repoRoot).length === 0);
|
|
92
|
+
let workHandle = null;
|
|
93
|
+
let execRoot = repoRoot;
|
|
94
|
+
if (canUseWorktree && head) {
|
|
95
|
+
const id = `mutate-${Date.now().toString(36)}`;
|
|
96
|
+
workHandle = createNodeWorktree(repoRoot, head, id);
|
|
97
|
+
execRoot = workHandle.dir;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const baseline = runTestCommand(config.testCommand, execRoot);
|
|
101
|
+
if (baseline.exitCode !== 0) {
|
|
102
|
+
console.error(`[bastion] Mutation aborted: baseline testCommand already fails (exit ${baseline.exitCode}).`);
|
|
103
|
+
return 1;
|
|
104
|
+
}
|
|
105
|
+
const perFileMutants = [];
|
|
106
|
+
for (const rel of candidateFiles) {
|
|
107
|
+
const full = path.join(execRoot, rel);
|
|
108
|
+
if (!fs.existsSync(full))
|
|
109
|
+
continue;
|
|
110
|
+
const content = fs.readFileSync(full, 'utf8');
|
|
111
|
+
const m = collectMutantsForFile(rel, content);
|
|
112
|
+
if (m.length > 0)
|
|
113
|
+
perFileMutants.push(m);
|
|
114
|
+
}
|
|
115
|
+
const allMutants = sampleMutantsRoundRobin(perFileMutants, maxMutations);
|
|
116
|
+
if (allMutants.length === 0) {
|
|
117
|
+
console.log('[bastion] No mutation points found in target source files.');
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
console.log(`[bastion] Running ${allMutants.length} mutation spot-check(s)${workHandle ? ' (in isolated worktree)' : ''}...`);
|
|
121
|
+
let killed = 0;
|
|
122
|
+
const survived = [];
|
|
123
|
+
for (const mutant of allMutants) {
|
|
124
|
+
const full = path.join(execRoot, mutant.file);
|
|
125
|
+
const original = fs.readFileSync(full, 'utf8');
|
|
126
|
+
try {
|
|
127
|
+
fs.writeFileSync(full, mutant.mutatedContent, 'utf8');
|
|
128
|
+
const res = runTestCommand(config.testCommand, execRoot);
|
|
129
|
+
if (res.exitCode !== 0) {
|
|
130
|
+
killed++;
|
|
131
|
+
console.log(` [KILLED] ${mutant.file}:${mutant.line} (${mutant.description})`);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
survived.push(mutant);
|
|
135
|
+
console.error(` [SURVIVED] ${mutant.file}:${mutant.line} (${mutant.description}) — tests still passed!`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
fs.writeFileSync(full, original, 'utf8');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
console.log(`\n[bastion] Mutation score: ${killed}/${allMutants.length} killed.`);
|
|
143
|
+
if (survived.length > 0) {
|
|
144
|
+
console.error('[bastion] Some mutations survived — tests may have blind spots.');
|
|
145
|
+
return 1;
|
|
146
|
+
}
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
if (workHandle) {
|
|
151
|
+
removeNodeWorktree(repoRoot, workHandle);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { LOCK_REL_PATH, PLAN_REL_PATH, readConfigFromDisk, readLockFromDisk, readPlanFromDisk, writePlanToDisk, } from '../config.js';
|
|
4
|
+
import { getRepoRoot } from '../git.js';
|
|
5
|
+
import { buildExecutionWaves } from '../orchestrator/dag.js';
|
|
6
|
+
import { matchesAnyGlob } from '../runner.js';
|
|
7
|
+
const FALLBACK_PLAN = {
|
|
8
|
+
version: 1,
|
|
9
|
+
nodes: [
|
|
10
|
+
{
|
|
11
|
+
id: 'task-1',
|
|
12
|
+
title: 'Implement module 1',
|
|
13
|
+
prompt: 'Implement module 1 without modifying locked tests.',
|
|
14
|
+
tier: 1,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
id: 'task-2',
|
|
18
|
+
title: 'Implement module 2',
|
|
19
|
+
prompt: 'Implement module 2 without modifying locked tests.',
|
|
20
|
+
tier: 1,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: 'integrate',
|
|
24
|
+
title: 'Integrate modules',
|
|
25
|
+
dependsOn: ['task-1', 'task-2'],
|
|
26
|
+
prompt: 'Wire module 1 and module 2 together and pass all tests.',
|
|
27
|
+
tier: 2,
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
function resolveEffectiveTestCommand(repoRoot, rawTestCommand) {
|
|
32
|
+
const trimmed = rawTestCommand.trim();
|
|
33
|
+
if (/^npm\s+test\b/.test(trimmed)) {
|
|
34
|
+
const pkgPath = path.join(repoRoot, 'package.json');
|
|
35
|
+
if (fs.existsSync(pkgPath)) {
|
|
36
|
+
try {
|
|
37
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
38
|
+
if (pkg.scripts?.test)
|
|
39
|
+
return pkg.scripts.test.trim();
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// ignore
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return trimmed;
|
|
47
|
+
}
|
|
48
|
+
function deriveNodeTestCommand(repoRoot, globalTestCommand, testFile) {
|
|
49
|
+
const effective = resolveEffectiveTestCommand(repoRoot, globalTestCommand);
|
|
50
|
+
if (/\bvitest\b/.test(effective))
|
|
51
|
+
return `npx vitest run ${testFile}`;
|
|
52
|
+
if (/\bjest\b/.test(effective))
|
|
53
|
+
return `npx jest ${testFile}`;
|
|
54
|
+
if (/^node\s+--test\b/.test(effective))
|
|
55
|
+
return `node --test ${testFile}`;
|
|
56
|
+
if (/^(?:python\s+-m\s+)?pytest\b/.test(effective))
|
|
57
|
+
return `pytest ${testFile}`;
|
|
58
|
+
if (/^flutter\s+test\b/.test(effective))
|
|
59
|
+
return `flutter test ${testFile}`;
|
|
60
|
+
if (/^go\s+test\b/.test(effective)) {
|
|
61
|
+
const dir = path.posix.dirname(testFile);
|
|
62
|
+
return dir === '.' ? 'go test .' : `go test ./${dir}/`;
|
|
63
|
+
}
|
|
64
|
+
if (/^cargo\s+test\b/.test(effective) && testFile.startsWith('tests/')) {
|
|
65
|
+
const stem = path.basename(testFile, path.extname(testFile));
|
|
66
|
+
return `cargo test --test ${stem}`;
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
function isLikelyIntegrationTest(testFile) {
|
|
71
|
+
const base = path.basename(testFile).toLowerCase();
|
|
72
|
+
return /^(?:cli|main|app|e2e|integration)[._-]/.test(base);
|
|
73
|
+
}
|
|
74
|
+
function makeUniqueNodeId(testFile, used, index) {
|
|
75
|
+
const clean = (s) => s.replace(/[^a-zA-Z0-9_-]+/g, '-').replace(/^-+|-+$/g, '').toLowerCase();
|
|
76
|
+
const baseStem = path.basename(testFile).replace(/\.[^.]+$/, '');
|
|
77
|
+
let id = clean(baseStem) || `node-${index + 1}`;
|
|
78
|
+
if (used.has(id)) {
|
|
79
|
+
const parent = path.posix.dirname(testFile);
|
|
80
|
+
const parentClean = parent !== '.' ? clean(parent.replace(/\//g, '-')) : '';
|
|
81
|
+
const candidate = parentClean ? `${parentClean}-${id}` : `${id}-${index + 1}`;
|
|
82
|
+
id = used.has(candidate) ? `${candidate}-${index + 1}` : candidate;
|
|
83
|
+
}
|
|
84
|
+
used.add(id);
|
|
85
|
+
return id;
|
|
86
|
+
}
|
|
87
|
+
function buildDraftPlanFromLock(repoRoot) {
|
|
88
|
+
const lockPath = path.join(repoRoot, LOCK_REL_PATH);
|
|
89
|
+
if (!fs.existsSync(lockPath))
|
|
90
|
+
return null;
|
|
91
|
+
const config = readConfigFromDisk(repoRoot);
|
|
92
|
+
const lock = readLockFromDisk(repoRoot);
|
|
93
|
+
const testFiles = lock.lockedFiles
|
|
94
|
+
.map((f) => f.path)
|
|
95
|
+
.filter((p) => matchesAnyGlob(p, config.lockedGlobs));
|
|
96
|
+
if (testFiles.length === 0)
|
|
97
|
+
return null;
|
|
98
|
+
const usedIds = new Set();
|
|
99
|
+
const unitNodes = [];
|
|
100
|
+
const integrationSpecs = [];
|
|
101
|
+
testFiles.forEach((file, idx) => {
|
|
102
|
+
const id = makeUniqueNodeId(file, usedIds, idx);
|
|
103
|
+
if (isLikelyIntegrationTest(file) && testFiles.length > 1) {
|
|
104
|
+
integrationSpecs.push({ id, file });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const nodeCmd = deriveNodeTestCommand(repoRoot, config.testCommand, file);
|
|
108
|
+
if (!nodeCmd) {
|
|
109
|
+
console.log(`[bastion] Note: could not derive single-file command for "${file}"; node "${id}" will run the full testCommand.`);
|
|
110
|
+
}
|
|
111
|
+
unitNodes.push({
|
|
112
|
+
id,
|
|
113
|
+
title: `Pass ${file}`,
|
|
114
|
+
prompt: `Implement the code required to pass ${file} without modifying locked tests or .bastion/.`,
|
|
115
|
+
...(nodeCmd ? { testCommand: nodeCmd } : {}),
|
|
116
|
+
tier: 1,
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
const unitIds = unitNodes.map((n) => n.id);
|
|
120
|
+
const integrationNodes = integrationSpecs.map(({ id, file }) => ({
|
|
121
|
+
id,
|
|
122
|
+
title: `Integrate (${file})`,
|
|
123
|
+
...(unitIds.length > 0 ? { dependsOn: unitIds } : {}),
|
|
124
|
+
prompt: `Integrate modules and verify ${file} passes without modifying locked tests or .bastion/.`,
|
|
125
|
+
tier: 1,
|
|
126
|
+
}));
|
|
127
|
+
if (integrationNodes.length > 0) {
|
|
128
|
+
console.log(`[bastion] Placed ${integrationNodes.map((n) => n.id).join(', ')} in Wave 2 (verify dependencies before running).`);
|
|
129
|
+
}
|
|
130
|
+
return { version: 1, nodes: [...unitNodes, ...integrationNodes] };
|
|
131
|
+
}
|
|
132
|
+
export function runPlan(opts) {
|
|
133
|
+
const repoRoot = getRepoRoot(opts.cwd) ?? path.resolve(opts.cwd);
|
|
134
|
+
const planPath = path.join(repoRoot, PLAN_REL_PATH);
|
|
135
|
+
if (opts.init) {
|
|
136
|
+
if (fs.existsSync(planPath) && !opts.force) {
|
|
137
|
+
console.log(`[bastion] ${PLAN_REL_PATH} already exists (use --force to overwrite).`);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
const draft = buildDraftPlanFromLock(repoRoot) ?? FALLBACK_PLAN;
|
|
141
|
+
writePlanToDisk(repoRoot, draft);
|
|
142
|
+
console.log(`[bastion] Created ${PLAN_REL_PATH} (${draft.nodes.length} node(s)).`);
|
|
143
|
+
}
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
const plan = readPlanFromDisk(repoRoot);
|
|
147
|
+
const waves = buildExecutionWaves(plan.nodes);
|
|
148
|
+
console.log(`[bastion] Execution plan (${plan.nodes.length} node(s), ${waves.length} wave(s)):`);
|
|
149
|
+
waves.forEach((wave, idx) => {
|
|
150
|
+
console.log(`\n Wave ${idx + 1} (parallel):`);
|
|
151
|
+
for (const node of wave) {
|
|
152
|
+
const deps = node.dependsOn?.length ? ` [after: ${node.dependsOn.join(', ')}]` : '';
|
|
153
|
+
const tier = node.tier ? ` (tier ${node.tier}${node.tier === 3 && !node.approved ? ' - NEEDS APPROVAL' : ''})` : '';
|
|
154
|
+
console.log(` - ${node.id}: ${node.title}${deps}${tier}`);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
const unapprovedTier3 = plan.nodes.filter((n) => n.tier === 3 && !n.approved);
|
|
158
|
+
if (unapprovedTier3.length > 0) {
|
|
159
|
+
console.error('\n[bastion] Warning: tier-3 nodes require "approved": true before `bastion run` will execute them.');
|
|
160
|
+
return 1;
|
|
161
|
+
}
|
|
162
|
+
return 0;
|
|
163
|
+
}
|