stopbar 0.0.0-stage → 1.0.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/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +164 -2
- package/SECURITY.md +111 -0
- package/bin/stopbar.mjs +6 -0
- package/claude-hooks.example.json +27 -0
- package/dist/attest.js +95 -0
- package/dist/build-info.json +6 -0
- package/dist/classify.js +183 -0
- package/dist/cli.js +78 -0
- package/dist/commands.js +164 -0
- package/dist/config.js +85 -0
- package/dist/doctor.js +133 -0
- package/dist/engine.js +51 -0
- package/dist/glob.js +68 -0
- package/dist/hook.js +215 -0
- package/dist/kernel.gen.js +63 -0
- package/dist/kernel.js +162 -0
- package/dist/pkg.js +13 -0
- package/dist/probe/index.js +119 -0
- package/dist/probe/wrapper.js +311 -0
- package/dist/repo.js +177 -0
- package/dist/store.js +144 -0
- package/dist/verify.js +154 -0
- package/package.json +45 -4
- package/src/kernel.ts +161 -0
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Built from src/doctor.ts by scripts/build.ts. Do not edit.
|
|
2
|
+
// `stopbar doctor`: environment, configuration, state, hooks, and a dry run of the real hook pipeline on a scratch repo.
|
|
3
|
+
import {execFileSync} from 'node:child_process'
|
|
4
|
+
import {createHash} from 'node:crypto'
|
|
5
|
+
import {existsSync, mkdirSync, readFileSync, rmSync, writeFileSync} from 'node:fs'
|
|
6
|
+
import {join} from 'node:path'
|
|
7
|
+
import {hasChain} from './classify.js'
|
|
8
|
+
import {fresh, HOOK_MATCHERS} from './commands.js'
|
|
9
|
+
import {makeConfig} from './config.js'
|
|
10
|
+
import {decode, MACHINE} from './engine.js'
|
|
11
|
+
import {runHook} from './hook.js'
|
|
12
|
+
import {PKG_ROOT} from './pkg.js'
|
|
13
|
+
import {FailClosed, findRepo, } from './repo.js'
|
|
14
|
+
import {paths, readConfig, readEvents, readState, VERSION, writeConfig, writeState} from './store.js'
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const sha = (f ) => createHash('sha256').update(readFileSync(f)).digest('hex')
|
|
18
|
+
|
|
19
|
+
function hooksIn(file ) {
|
|
20
|
+
if (!existsSync(file)) return null
|
|
21
|
+
try { return JSON.parse(readFileSync(file, 'utf8')).hooks ?? {} } catch { return {__invalid: []} }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function checkHooks(root , add ) {
|
|
25
|
+
const files = ['settings.json', 'settings.local.json'].map((n) => join(root, '.claude', n))
|
|
26
|
+
const merged = {}
|
|
27
|
+
for (const f of files) {
|
|
28
|
+
const h = hooksIn(f)
|
|
29
|
+
if (!h) continue
|
|
30
|
+
if ('__invalid' in h) { add('FAIL', 'hooks', `${f} is not valid JSON: Claude Code will not load the hooks`); continue }
|
|
31
|
+
for (const [ev, entries] of Object.entries(h)) {
|
|
32
|
+
for (const e of Array.isArray(entries) ? entries : []) {
|
|
33
|
+
for (const x of Array.isArray(e?.hooks) ? e.hooks : []) {
|
|
34
|
+
if (typeof x?.command === 'string' && /stopbar(\.mjs)?"?\s+hook\b/.test(x.command)) (merged[ev] ??= []).push({matcher: e.matcher, command: x.command, file: f})
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const covers = (m , tools ) => m === undefined || m === '*' || m === '' || tools.every((t) => new RegExp(`^(?:${m})$`).test(t))
|
|
40
|
+
const need = [['PreToolUse', ['Bash', 'Edit', 'Write', 'Read', 'WebFetch']], ['PostToolUse', HOOK_MATCHERS.post.split('|')],
|
|
41
|
+
['PostToolUseFailure', ['Bash', 'PowerShell']], ['Stop', []]]
|
|
42
|
+
for (const [ev, tools] of need) {
|
|
43
|
+
const hs = merged[ev] ?? []
|
|
44
|
+
if (!hs.length) { add('FAIL', `hook ${ev}`, `no Stopbar hook in ${files.join(' or ')}`); continue }
|
|
45
|
+
if (!hs.some((h) => covers(h.matcher, tools))) { add('FAIL', `hook ${ev}`, `matcher ${JSON.stringify(hs[0].matcher)} misses some of ${tools.join(', ')}`); continue }
|
|
46
|
+
const script = /node\s+"?([^"]+?stopbar\.mjs)"?\s+hook/.exec(hs[0].command)?.[1]
|
|
47
|
+
if (script && !existsSync(script)) { add('FAIL', `hook ${ev}`, `script not found: ${script}`); continue }
|
|
48
|
+
add('OK', `hook ${ev}`, `${hs[0].command} (${hs[0].file.replace(/\\/g, '/').split('/').slice(-2).join('/')})`)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The real hook pipeline (in process) on a scratch repository inside <git-dir>/stopbar/doctor-run. */
|
|
53
|
+
function dryRun(base , add ) {
|
|
54
|
+
const dir = join(base, 'doctor-run')
|
|
55
|
+
rmSync(dir, {recursive: true, force: true})
|
|
56
|
+
mkdirSync(join(dir, 'src'), {recursive: true}); mkdirSync(join(dir, 'tests'), {recursive: true})
|
|
57
|
+
const g = (...a ) => execFileSync('git', ['-c', 'user.name=stopbar-doctor', '-c', 'user.email=doctor@stopbar.invalid', '-c', 'commit.gpgsign=false', ...a], {cwd: dir, stdio: 'ignore'})
|
|
58
|
+
try {
|
|
59
|
+
writeFileSync(join(dir, 'src', 'a.mjs'), 'export const a = 1\n')
|
|
60
|
+
writeFileSync(join(dir, 'tests', 'a.test.mjs'), 'export {}\n')
|
|
61
|
+
g('init', '-q'); g('add', '-A'); g('commit', '-q', '-m', 'base')
|
|
62
|
+
const repo = findRepo(dir)
|
|
63
|
+
const cfg = makeConfig({buildCmd: 'node build.mjs', fullTestCmd: 'node --test'})
|
|
64
|
+
writeConfig(repo, cfg); writeState(repo, fresh(repo, cfg))
|
|
65
|
+
const hook = (ev , tool , ti , extra = {}) => runHook(JSON.stringify({hook_event_name: ev, tool_name: tool, tool_input: ti, cwd: dir, ...extra}))
|
|
66
|
+
const steps = [
|
|
67
|
+
['edit code is allowed', () => { const p = hook('PreToolUse', 'Write', {file_path: join(dir, 'src', 'a.mjs')}).code === 0; writeFileSync(join(dir, 'src', 'a.mjs'), 'export const a = 2\n'); return p && hook('PostToolUse', 'Write', {file_path: join(dir, 'src', 'a.mjs')}).code === 0 }],
|
|
68
|
+
['commit without build and tests is blocked (exit 2)', () => hook('PreToolUse', 'Bash', {command: 'git commit -m "x"'}).code === 2],
|
|
69
|
+
['passing build and full suite are recorded', () => hook('PostToolUse', 'Bash', {command: 'node build.mjs'}, {tool_response: {stdout: '', stderr: '', interrupted: false}}).code === 0 &&
|
|
70
|
+
hook('PostToolUse', 'Bash', {command: 'node --test'}, {tool_response: {stdout: '', stderr: '', interrupted: false}}).code === 0],
|
|
71
|
+
["safe heredoc commit git commit -m \"$(cat <<'EOF' ...)\" is allowed", () => hook('PreToolUse', 'Bash', {command: "git commit -m \"$(cat <<'EOF'\nFix a\n\nReviewed-by: Jane Doe <jane@example.invalid>\nEOF\n)\""}).code === 0],
|
|
72
|
+
['command substitution in a commit is blocked', () => hook('PreToolUse', 'Bash', {command: 'git commit -m "$(rm -rf /)"'}).code === 2],
|
|
73
|
+
['editing an existing test taints the session', () => { writeFileSync(join(dir, 'tests', 'a.test.mjs'), 'export {} // weakened\n'); return hook('PostToolUse', 'Edit', {file_path: join(dir, 'tests', 'a.test.mjs')}).code === 2 && decode(readState(repo).kernel).testsTainted === true }],
|
|
74
|
+
['writing .claude/settings.local.json is blocked', () => hook('PreToolUse', 'Write', {file_path: join(dir, '.claude', 'settings.local.json')}).code === 2],
|
|
75
|
+
['writing .git/hooks/pre-commit is blocked', () => hook('PreToolUse', 'Write', {file_path: join(dir, '.git', 'hooks', 'pre-commit')}).code === 2],
|
|
76
|
+
['a damaged state.json fails closed (exit 2)', () => { writeFileSync(paths(repo).state, '{"version": 2, "kern'); const r = hook('PreToolUse', 'Read', {file_path: join(dir, 'src', 'a.mjs')}); return r.code === 2 && /FAIL-CLOSED/.test(r.msg ?? '') }],
|
|
77
|
+
]
|
|
78
|
+
for (const [name, f] of steps) {
|
|
79
|
+
let ok = false
|
|
80
|
+
try { ok = f() } catch { ok = false }
|
|
81
|
+
add(ok ? 'OK' : 'FAIL', 'dry run', name)
|
|
82
|
+
}
|
|
83
|
+
} finally {
|
|
84
|
+
rmSync(dir, {recursive: true, force: true})
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function doctor(o ) {
|
|
89
|
+
const checks = []
|
|
90
|
+
const add = (status , name , detail ) => { checks.push({status, name, detail}) }
|
|
91
|
+
const [maj, min] = process.versions.node.split('.').map(Number)
|
|
92
|
+
add(maj > 22 || (maj === 22 && min >= 18) ? 'OK' : 'FAIL', 'node', `v${process.versions.node} (needs >= 22.18)`)
|
|
93
|
+
try { add('OK', 'git', execFileSync('git', ['--version'], {encoding: 'utf8'}).trim()) } catch { add('FAIL', 'git', 'git not found on PATH') }
|
|
94
|
+
add(MACHINE.name === 'stopbar_kernel_none' ? 'OK' : 'FAIL', 'kernel', `${MACHINE.name}, kernel.ts sha256 ${MACHINE.kernelSha256.slice(0, 16)}, machine sha256 ${MACHINE.machineSha256.slice(0, 16)}`)
|
|
95
|
+
if (existsSync(join(PKG_ROOT, 'node_modules', 'gdp-wired')) && existsSync(join(PKG_ROOT, 'dist', 'kernel.js'))) {
|
|
96
|
+
try {
|
|
97
|
+
const {checkGenerated} = await import('./verify.js')
|
|
98
|
+
const g = await checkGenerated(500)
|
|
99
|
+
add(g.ok ? 'OK' : 'FAIL', 'kernel link', `compileJs(kernel.ts) == dist/kernel.gen.js: ${g.sameMachine}; kernel.ts hash: ${g.sameSource}; ${(g.differential ).steps} steps vs interpreter, ${(g.differential ).mismatches} mismatches`)
|
|
100
|
+
} catch (e) { add('WARN', 'kernel link', `not checked: ${(e ).message}`) }
|
|
101
|
+
} else add('WARN', 'kernel link', 'gdp-wired not installed next to this copy (pinned runtime): run `stopbar prove` in the package')
|
|
102
|
+
|
|
103
|
+
let repo = null
|
|
104
|
+
try { repo = findRepo(o.repo ?? process.cwd()); add('OK', 'repository', `${repo.root} (state in ${repo.stateDir})`) } catch (e) { add('FAIL', 'repository', (e ).message) }
|
|
105
|
+
if (repo) {
|
|
106
|
+
let cfgOk = false
|
|
107
|
+
try {
|
|
108
|
+
const cfg = readConfig(repo); cfgOk = true
|
|
109
|
+
add('OK', 'config.json', `profile ${cfg.profile}; build \`${cfg.buildCmd}\`; full tests \`${cfg.fullTestCmd}\`; maxRetries ${cfg.maxRetries}; cap ${cfg.cap}; ` +
|
|
110
|
+
`${cfg.paths.tests.length}/${cfg.paths.testConfig.length}/${cfg.paths.secrets.length} test/config/secret patterns`)
|
|
111
|
+
for (const [k, v] of [['buildCmd', cfg.buildCmd], ['fullTestCmd', cfg.fullTestCmd]]) if (hasChain(v)) add('FAIL', 'config.json', `${k} has shell operators: it can never pass as a gate`)
|
|
112
|
+
try {
|
|
113
|
+
const s = readState(repo), v = decode(s.kernel)
|
|
114
|
+
add(v.halted ? 'WARN' : 'OK', 'state.json', `revision ${v.treeRev}; ${v.halted ? 'HALTED' : 'running'}; spent ${v.spent}/${v.capNow}; tainted ${v.testsTainted}`)
|
|
115
|
+
if (s.params.maxRetries !== cfg.maxRetries || s.params.cap !== cfg.cap) add('WARN', 'state.json', `state params ${JSON.stringify(s.params)} differ from config; a human runs \`stopbar reset\` to apply the config`)
|
|
116
|
+
} catch (e) { add('FAIL', 'state.json', e instanceof FailClosed ? `${e.message}. Fix: ${e.fix}` : String(e)) }
|
|
117
|
+
} catch (e) { add('FAIL', 'config.json', e instanceof FailClosed ? `${e.message}. Fix: ${e.fix}` : String(e)) }
|
|
118
|
+
const ev = readEvents(repo)
|
|
119
|
+
add(ev.torn.length ? 'WARN' : 'OK', 'events.jsonl', `${ev.events.length} entries${ev.torn.length ? `; ${ev.torn.length} torn line(s) (crash mid-write) at line ${ev.torn.map((t) => t.line).join(', ')}: kept, later entries are intact` : ''}`)
|
|
120
|
+
const rt = join(repo.stateDir, 'runtime', 'dist', 'kernel.gen.js')
|
|
121
|
+
if (existsSync(rt)) {
|
|
122
|
+
const same = existsSync(join(PKG_ROOT, 'dist', 'kernel.gen.js')) && sha(rt) === sha(join(PKG_ROOT, 'dist', 'kernel.gen.js'))
|
|
123
|
+
add(same ? 'OK' : 'WARN', 'pinned runtime', `${join(repo.stateDir, 'runtime')}${same ? ' (same kernel as this package)' : ' differs from this package: a human runs `stopbar init --force ...` to re-pin'}`)
|
|
124
|
+
} else if (cfgOk) add('WARN', 'pinned runtime', 'none: hooks run an unpinned copy (init --no-pin)')
|
|
125
|
+
checkHooks(repo.root, add)
|
|
126
|
+
try { dryRun(repo.stateDir, add) } catch (e) { add('FAIL', 'dry run', String(e)) }
|
|
127
|
+
}
|
|
128
|
+
const fails = checks.filter((c) => c.status === 'FAIL').length, warns = checks.filter((c) => c.status === 'WARN').length
|
|
129
|
+
const msg = o.json ? JSON.stringify({version: VERSION, ok: fails === 0, fails, warns, checks}, null, 1)
|
|
130
|
+
: [`stopbar doctor (Stopbar ${VERSION})`, ...checks.map((c) => `[${c.status.padEnd(4)}] ${c.name.padEnd(18)} ${c.detail}`),
|
|
131
|
+
`RESULT: ${fails ? `FAIL (${fails} problem(s), ${warns} warning(s))` : `OK (${warns} warning(s))`}`].join('\n')
|
|
132
|
+
return {code: fails ? 1 : 0, msg}
|
|
133
|
+
}
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Built from src/engine.ts by scripts/build.ts. Do not edit.
|
|
2
|
+
// One step of the proven rule. The step function is generated by gdp-wired's compileJs from src/kernel.ts at build
|
|
3
|
+
// time (dist/kernel.gen.js): the same machine the prover checked, without loading the prover in a hook.
|
|
4
|
+
// `stopbar verify` and the test suite regenerate it and compare it byte for byte, and replay random runs
|
|
5
|
+
// against gdp-wired's interpreter. There is no hand-written copy of step() anywhere.
|
|
6
|
+
import * as G from './kernel.gen.js'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
const DEF = {op: 'EDIT_CODE', cost: 0, passed: false, fullSuite: false, suspicious: false, risky: false, treeChanged: false, approveRev: 0, extra: 0}
|
|
15
|
+
|
|
16
|
+
export const MACHINE = {name: G.MACHINE_NAME, kernelSha256: G.KERNEL_SOURCE_SHA256, machineSha256: G.MACHINE_SHA256}
|
|
17
|
+
if (G.MACHINE_NAME !== 'stopbar_kernel_none') throw new Error(`kernel.gen.js holds ${G.MACHINE_NAME}, not the unmutated kernel`)
|
|
18
|
+
|
|
19
|
+
export function encodeInput(r ) {
|
|
20
|
+
const req = {...DEF, ...r}
|
|
21
|
+
const out = {}
|
|
22
|
+
for (const f of G.CODEC.inputs) {
|
|
23
|
+
const v = req[f.name]
|
|
24
|
+
if (f.kind === 'oneOf') {
|
|
25
|
+
const i = f.values .indexOf(v )
|
|
26
|
+
if (i < 0) throw new Error(`unknown ${f.name} ${JSON.stringify(v)}`)
|
|
27
|
+
out[f.name] = i
|
|
28
|
+
} else if (f.kind === 'bool') out[f.name] = v ? 1 : 0
|
|
29
|
+
else {
|
|
30
|
+
if (typeof v !== 'number' || !Number.isSafeInteger(v) || v < 0) throw new Error(`${f.name} must be a safe non-negative integer, got ${v}`)
|
|
31
|
+
out[f.name] = v
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return out
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function decode(st ) {
|
|
38
|
+
const out = {}
|
|
39
|
+
for (const f of G.CODEC.state) {
|
|
40
|
+
const v = st.scalars[f.name]
|
|
41
|
+
out[f.name] = f.kind === 'bool' ? v === 1 : f.kind === 'oneOf' ? f.values [v] : v
|
|
42
|
+
}
|
|
43
|
+
return out
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function initKernel(p ) { return G.init(p) }
|
|
47
|
+
|
|
48
|
+
/** Pure step. Throws on an input outside the rule's assumptions (fail closed). */
|
|
49
|
+
export function kstep(p , st , r ) {
|
|
50
|
+
return G.step(p, G.fork(st), encodeInput(r))
|
|
51
|
+
}
|
package/dist/glob.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Built from src/glob.ts by scripts/build.ts. Do not edit.
|
|
2
|
+
// Minimal glob matcher for path patterns in config.json. No dependencies.
|
|
3
|
+
// * any characters except '/'
|
|
4
|
+
// ** any number of path segments (also none), only as a whole segment: '**/x', 'a/**', 'a/**/b'
|
|
5
|
+
// ? one character except '/'
|
|
6
|
+
// {a,b} alternatives (no nesting)
|
|
7
|
+
// A pattern without '/' matches the base name of the path (like .gitignore). Matching is case-insensitive:
|
|
8
|
+
// more matches mean more gates, which is the safe direction on case-insensitive file systems.
|
|
9
|
+
|
|
10
|
+
const cache = new Map ()
|
|
11
|
+
|
|
12
|
+
function esc(c ) { return /[\\^$.*+?()[\]{}|/]/.test(c) ? '\\' + c : c }
|
|
13
|
+
|
|
14
|
+
function body(p ) {
|
|
15
|
+
let out = ''
|
|
16
|
+
for (let i = 0; i < p.length; i++) {
|
|
17
|
+
const c = p[i]
|
|
18
|
+
if (c === '*') {
|
|
19
|
+
if (p[i + 1] === '*') {
|
|
20
|
+
const atStart = i === 0 || p[i - 1] === '/'
|
|
21
|
+
const atEnd = i + 2 === p.length || p[i + 2] === '/'
|
|
22
|
+
if (!atStart || !atEnd) throw new Error(`glob ${JSON.stringify(p)}: '**' must be a whole path segment`)
|
|
23
|
+
if (i + 2 === p.length) { out += '.*'; i += 1 } else { out += '(?:[^/]*/)*'; i += 2 }
|
|
24
|
+
continue
|
|
25
|
+
}
|
|
26
|
+
out += '[^/]*'
|
|
27
|
+
} else if (c === '?') {
|
|
28
|
+
out += '[^/]'
|
|
29
|
+
} else if (c === '{') {
|
|
30
|
+
const end = p.indexOf('}', i)
|
|
31
|
+
if (end < 0) throw new Error(`glob ${JSON.stringify(p)}: unclosed '{'`)
|
|
32
|
+
const alts = p.slice(i + 1, end).split(',')
|
|
33
|
+
if (alts.some((a) => a.includes('{'))) throw new Error(`glob ${JSON.stringify(p)}: nested '{' is not supported`)
|
|
34
|
+
out += '(?:' + alts.map(body).join('|') + ')'
|
|
35
|
+
i = end
|
|
36
|
+
} else if (c === '}' ) {
|
|
37
|
+
throw new Error(`glob ${JSON.stringify(p)}: unmatched '}'`)
|
|
38
|
+
} else {
|
|
39
|
+
out += esc(c)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return out
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function globToRegExp(pattern ) {
|
|
46
|
+
let re = cache.get(pattern)
|
|
47
|
+
if (re) return re
|
|
48
|
+
if (typeof pattern !== 'string' || pattern.length === 0) throw new Error('glob pattern must be a non-empty string')
|
|
49
|
+
const p = pattern.replace(/\\/g, '/').replace(/^\.\//, '')
|
|
50
|
+
re = p.includes('/') ? new RegExp('^' + body(p.replace(/^\//, '')) + '$', 'i') : new RegExp('(?:^|/)' + body(p) + '$', 'i')
|
|
51
|
+
cache.set(pattern, re)
|
|
52
|
+
return re
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Path relative to the repository (or absolute), '/' or '\\' separated. */
|
|
56
|
+
export function matchAny(path , patterns ) {
|
|
57
|
+
const s = path.replace(/\\/g, '/').replace(/^\.\//, '')
|
|
58
|
+
for (const p of patterns) if (globToRegExp(p).test(s)) return true
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Same, but a pattern also matches when it matches any suffix of the path (used for absolute paths). */
|
|
63
|
+
export function matchAnySuffix(path , patterns ) {
|
|
64
|
+
const s = path.replace(/\\/g, '/')
|
|
65
|
+
const parts = s.split('/')
|
|
66
|
+
for (let i = 0; i < parts.length; i++) if (matchAny(parts.slice(i).join('/'), patterns)) return true
|
|
67
|
+
return false
|
|
68
|
+
}
|
package/dist/hook.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// Built from src/hook.ts by scripts/build.ts. Do not edit.
|
|
2
|
+
// Claude Code hooks: PreToolUse, PostToolUse, PostToolUseFailure, Stop. JSON on stdin; exit 0 = allow,
|
|
3
|
+
// exit 2 = block (stderr goes to the agent). Any wrapper error is exit 2 (fail closed).
|
|
4
|
+
import {resolve} from 'node:path'
|
|
5
|
+
import * as K from './classify.js'
|
|
6
|
+
|
|
7
|
+
import {decode, kstep, } from './engine.js'
|
|
8
|
+
import {changesSinceHead, countedEntries, diffEntries, differsFromHead, FailClosed, findRepo, gitCalls, inHead, markCounted, snapshot} from './repo.js'
|
|
9
|
+
import {appendEvent, readConfig, readState, withLock, writeState} from './store.js'
|
|
10
|
+
import {codeDirs} from './pkg.js'
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit'])
|
|
16
|
+
const READ_TOOLS = new Set(['Read', 'Grep', 'Glob', 'LS'])
|
|
17
|
+
const SHELL_TOOLS = new Set(['Bash', 'PowerShell'])
|
|
18
|
+
const OUTBOUND = (t ) => t === 'WebFetch' || t === 'WebSearch' || t.startsWith('mcp__')
|
|
19
|
+
|
|
20
|
+
function step(ctx , req , source ) {
|
|
21
|
+
const before = decode(ctx.s.kernel)
|
|
22
|
+
ctx.s.kernel = kstep(ctx.s.params, ctx.s.kernel, req)
|
|
23
|
+
ctx.dirty = true
|
|
24
|
+
const after = decode(ctx.s.kernel)
|
|
25
|
+
appendEvent(ctx.repo, {source, req, decision: after.decision, halted: after.halted, testsTainted: after.testsTainted,
|
|
26
|
+
secretTainted: after.secretTainted, shellToken: after.shellToken, treeRev: after.treeRev, buildOkRev: after.buildOkRev,
|
|
27
|
+
testsOkRev: after.testsOkRev, spent: after.spent, capNow: after.capNow, testFails: after.testFails, buildFails: after.buildFails,
|
|
28
|
+
commitExecuted: after.commitExecuted})
|
|
29
|
+
return {before, after}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function whyBlocked(v , cfg ) {
|
|
33
|
+
const r = []
|
|
34
|
+
if (v.buildOkRev !== v.treeRev) r.push(`no passing build at revision ${v.treeRev} (run \`${cfg.buildCmd}\` exactly, alone)`)
|
|
35
|
+
if (v.testsOkRev !== v.treeRev) r.push(`no passing FULL test suite at revision ${v.treeRev} (run \`${cfg.fullTestCmd}\` exactly, alone)`)
|
|
36
|
+
if (v.testsTainted) r.push(`existing tests or test config changed: a human must review and run \`stopbar approve-tests --rev ${v.treeRev}\``)
|
|
37
|
+
return r.join('; ') || 'kernel decision'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Counts a working-tree change the kernel has not seen yet. */
|
|
41
|
+
function syncTree(ctx , cost , source ) {
|
|
42
|
+
const {repo, s, cfg} = ctx
|
|
43
|
+
const snap = snapshot(repo)
|
|
44
|
+
ctx.now = snap
|
|
45
|
+
if (snap.id === s.snapshot) return null
|
|
46
|
+
const base = countedEntries(repo, s.snapshot)
|
|
47
|
+
let changes
|
|
48
|
+
if (base) changes = diffEntries(base, snap.entries)
|
|
49
|
+
else {
|
|
50
|
+
// the copy of the last counted snapshot is gone or damaged: compare with HEAD instead (more changes, never fewer)
|
|
51
|
+
changes = changesSinceHead(repo, snap.entries)
|
|
52
|
+
appendEvent(repo, {source: `${source}:fallback-diff`, note: 'counted snapshot copy missing; diff against HEAD'})
|
|
53
|
+
}
|
|
54
|
+
const testPaths = changes.map((c) => c.path).filter((p) => K.isTestPath(p, cfg.paths) && !K.isTestConfigPath(p, cfg.paths))
|
|
55
|
+
const head = testPaths.length ? inHead(repo.root, testPaths) : new Set ()
|
|
56
|
+
const cls = K.classifyTreeChange(changes, (p) => head.has(p), cfg.paths)
|
|
57
|
+
const {before, after} = step(ctx, {op: cls.op, suspicious: cls.suspicious, cost}, `${source}:tree ${cls.why.join(', ') || `${changes.length} file(s)`}`)
|
|
58
|
+
// not executed (halted or over budget): the change stays pending and is counted later
|
|
59
|
+
if (after.treeRev > before.treeRev) { s.snapshot = snap.id; markCounted(repo); ctx.dirty = true }
|
|
60
|
+
return {cls, after}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const block = (reason ) => ({code: 2, msg: `BLOCK (stopbar): ${reason}`})
|
|
64
|
+
|
|
65
|
+
// ---------------------------------------------------------------- PreToolUse
|
|
66
|
+
function pre(ctx , input ) {
|
|
67
|
+
const tool = String(input.tool_name ?? ''), ti = input.tool_input ?? {}
|
|
68
|
+
const {repo, cfg} = ctx
|
|
69
|
+
// self-protection: unconditional, before the kernel, also when halted
|
|
70
|
+
if (EDIT_TOOLS.has(tool)) {
|
|
71
|
+
for (const p of [ti.file_path, ti.notebook_path, ti.path].filter((x) => typeof x === 'string' && x)) {
|
|
72
|
+
const why = K.protectedPath(resolve(repo.root, p), repo.root, ctx.protect)
|
|
73
|
+
if (why) { appendEvent(repo, {source: 'pre', tool, wrapper: 'BLOCK', reason: why, path: p}); return block(`writing ${p} is not allowed: ${why}. Ask the human.`) }
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (SHELL_TOOLS.has(tool)) {
|
|
77
|
+
const cmd = String(ti.command ?? '')
|
|
78
|
+
const hd = tool === 'Bash' ? K.parseSafeHeredocCommit(cmd.trim()) : null
|
|
79
|
+
const why = K.protectedCommand(hd ? hd.prefix : cmd, ctx.protect)
|
|
80
|
+
if (why) { appendEvent(repo, {source: 'pre', tool, wrapper: 'BLOCK', reason: why}); return block(`${why}. Ask the human.`) }
|
|
81
|
+
}
|
|
82
|
+
const v = decode(ctx.s.kernel)
|
|
83
|
+
if (v.halted) {
|
|
84
|
+
appendEvent(repo, {source: 'pre', tool, wrapper: 'PAUSE', reason: 'halted'})
|
|
85
|
+
return {code: 2, msg: `PAUSE (stopbar): the session is halted (testFails=${v.testFails}, buildFails=${v.buildFails}, spent=${v.spent}/${v.capNow}). A human must run: stopbar resume --extra <n>`}
|
|
86
|
+
}
|
|
87
|
+
const cost = cfg.costPerCall
|
|
88
|
+
if (READ_TOOLS.has(tool)) {
|
|
89
|
+
const ps = [ti.file_path, ti.path, ti.glob, ti.pattern].filter((x) => typeof x === 'string' && x)
|
|
90
|
+
if (ps.some((p ) => K.isSecretPath(p, cfg.paths))) step(ctx, {op: 'READ_SECRET', cost}, `pre:${tool}`)
|
|
91
|
+
return {code: 0}
|
|
92
|
+
}
|
|
93
|
+
if (EDIT_TOOLS.has(tool)) return {code: 0} // cost and tree change are counted in PostToolUse
|
|
94
|
+
if (SHELL_TOOLS.has(tool)) {
|
|
95
|
+
const cmd = String(ti.command ?? '')
|
|
96
|
+
const k = K.classifyBash(cmd, cfg, tool === 'PowerShell' ? 'powershell' : 'bash')
|
|
97
|
+
if (k.kind === 'commit-chained') {
|
|
98
|
+
appendEvent(repo, {source: 'pre', tool, wrapper: 'BLOCK', reason: `chained commit (${k.reason})`})
|
|
99
|
+
return block(`run \`git commit\` / \`git push\` as a single command (${k.reason}): no &&, ;, |, redirections or $(...). ` +
|
|
100
|
+
`For a multi-line message use -m "..." or exactly: git commit -m "$(cat <<'EOF'\\n<message>\\nEOF\\n)" with the quoted 'EOF' delimiter and nothing else in the command.`)
|
|
101
|
+
}
|
|
102
|
+
if (k.kind === 'gate-chained') {
|
|
103
|
+
appendEvent(repo, {source: 'pre', tool, wrapper: 'BLOCK', reason: `chained gate command (${k.reason})`})
|
|
104
|
+
return block(`run \`${k.gate}\` exactly as configured, alone: no ;, &&, ||, |, redirections or $(...). Chained operators hide its exit status.`)
|
|
105
|
+
}
|
|
106
|
+
if (k.kind === 'build' || k.kind === 'fulltest') return {code: 0} // the result is recorded in PostToolUse
|
|
107
|
+
if (k.kind === 'commit') {
|
|
108
|
+
syncTree(ctx, 0, 'pre:commit-sync') // a change nobody reported (background job, human edit) is counted first
|
|
109
|
+
const {after} = step(ctx, {op: 'COMMIT', cost}, `pre:${tool}:commit${k.heredoc ? ':heredoc' : ''}`)
|
|
110
|
+
if (after.decision !== 'ALLOW') return {code: 2, msg: `${after.decision} (stopbar): commit refused: ${after.halted ? 'session halted' : whyBlocked(after, cfg)}`}
|
|
111
|
+
if (k.push) {
|
|
112
|
+
const r = step(ctx, {op: 'SHELL', risky: true}, `pre:${tool}:push`)
|
|
113
|
+
if (r.after.decision === 'PAUSE') return {code: 2, msg: 'PAUSE (stopbar): git push after a secret was read; a human must run `stopbar approve-shell`'}
|
|
114
|
+
}
|
|
115
|
+
return {code: 0}
|
|
116
|
+
}
|
|
117
|
+
if (k.secret) step(ctx, {op: 'READ_SECRET'}, `pre:${tool}:secret-mention`)
|
|
118
|
+
const {after} = step(ctx, {op: 'SHELL', risky: k.risky, cost}, `pre:${tool}`)
|
|
119
|
+
if (after.decision === 'PAUSE') {
|
|
120
|
+
return {code: 2, msg: after.halted ? 'PAUSE (stopbar): budget reached; a human must run `stopbar resume --extra <n>`'
|
|
121
|
+
: 'PAUSE (stopbar): risky or outbound command after a secret was read; a human must run `stopbar approve-shell` (one call)'}
|
|
122
|
+
}
|
|
123
|
+
return {code: 0}
|
|
124
|
+
}
|
|
125
|
+
if (OUTBOUND(tool)) {
|
|
126
|
+
const {after} = step(ctx, {op: 'SHELL', risky: true, cost}, `pre:${tool}`)
|
|
127
|
+
if (after.decision === 'PAUSE') return {code: 2, msg: `PAUSE (stopbar): ${tool} is outbound and a secret was read (or the budget is spent); a human runs \`stopbar approve-shell\` or \`stopbar resume\``}
|
|
128
|
+
}
|
|
129
|
+
return {code: 0}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ---------------------------------------------------------------- PostToolUse / PostToolUseFailure
|
|
133
|
+
// Claude Code sends PostToolUse after success and PostToolUseFailure after an error (non-zero exit of Bash).
|
|
134
|
+
// PostToolUse carries no exit code: success unless is_error / interrupted. Failure is the event type itself.
|
|
135
|
+
function exitCodeOf(tr ) {
|
|
136
|
+
if (!tr || typeof tr !== 'object') return undefined
|
|
137
|
+
for (const k of ['exitCode', 'exit_code', 'returnCode', 'code']) if (typeof tr[k] === 'number') return tr[k]
|
|
138
|
+
return undefined
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function post(ctx , input , failure ) {
|
|
142
|
+
const tool = String(input.tool_name ?? ''), ti = input.tool_input ?? {}
|
|
143
|
+
const {repo, cfg} = ctx
|
|
144
|
+
const msgs = []
|
|
145
|
+
if (SHELL_TOOLS.has(tool)) {
|
|
146
|
+
const k = K.classifyBash(String(ti.command ?? ''), cfg, tool === 'PowerShell' ? 'powershell' : 'bash')
|
|
147
|
+
const tr = input.tool_response
|
|
148
|
+
appendEvent(repo, {source: `payload:${input.hook_event_name}`, tool, inputKeys: Object.keys(input),
|
|
149
|
+
toolResponseKeys: tr && typeof tr === 'object' ? Object.keys(tr) : typeof tr})
|
|
150
|
+
const code = exitCodeOf(tr)
|
|
151
|
+
const flagged = input.is_error === true || tr?.is_error === true || tr?.interrupted === true
|
|
152
|
+
const passed = failure ? false : code !== undefined ? code === 0 : !flagged
|
|
153
|
+
const note = failure ? ' (PostToolUseFailure)' : code !== undefined ? ` (exit ${code})` : ` (no exit code; is_error/interrupted=${flagged})`
|
|
154
|
+
if (failure && input.is_interrupt === true) {
|
|
155
|
+
// Ctrl+C by the user: not a failed gate and not a retry; the tree is still checked below
|
|
156
|
+
appendEvent(repo, {source: 'post:user-interrupt', tool, kind: k.kind})
|
|
157
|
+
} else if (k.kind === 'build') {
|
|
158
|
+
step(ctx, {op: 'RUN_BUILD', passed, cost: cfg.costPerCall}, `post:build${note}`)
|
|
159
|
+
} else if (k.kind === 'fulltest') {
|
|
160
|
+
step(ctx, {op: 'RUN_TESTS', passed, fullSuite: true, cost: cfg.costPerCall}, `post:fulltest${note}`)
|
|
161
|
+
} else if (k.kind === 'shell' && k.partialTests) {
|
|
162
|
+
step(ctx, {op: 'RUN_TESTS', passed, fullSuite: false}, `post:partial-tests${note}`)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (SHELL_TOOLS.has(tool) || EDIT_TOOLS.has(tool)) {
|
|
166
|
+
// after SHELL the cost was paid in PreToolUse; an edit pays here
|
|
167
|
+
const r = syncTree(ctx, EDIT_TOOLS.has(tool) ? cfg.costPerCall : 0, `post:${tool}`)
|
|
168
|
+
if (r?.cls.suspicious) msgs.push(`PAUSE (stopbar): existing tests or test config changed (${r.cls.why.join(', ')}). Commit is blocked until a human reviews the change and runs \`stopbar approve-tests\`.`)
|
|
169
|
+
}
|
|
170
|
+
const v = decode(ctx.s.kernel)
|
|
171
|
+
if (v.halted) msgs.push(`PAUSE (stopbar): session halted (testFails=${v.testFails}, buildFails=${v.buildFails}, spent=${v.spent}/${v.capNow}). A human must run \`stopbar resume --extra <n>\`.`)
|
|
172
|
+
return msgs.length ? {code: 2, msg: msgs.join('\n')} : {code: 0}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---------------------------------------------------------------- Stop: "I am done" = the COMMIT gate when the tree differs from HEAD
|
|
176
|
+
function stop(ctx , input ) {
|
|
177
|
+
if (input.stop_hook_active) return {code: 0}
|
|
178
|
+
syncTree(ctx, 0, 'stop:sync')
|
|
179
|
+
if (!differsFromHead(ctx.repo, ctx.now .entries)) return {code: 0}
|
|
180
|
+
const {after} = step(ctx, {op: 'COMMIT'}, 'stop:declare-done')
|
|
181
|
+
return after.decision === 'ALLOW' ? {code: 0}
|
|
182
|
+
: {code: 2, msg: `Not done (stopbar): uncommitted changes and ${after.halted ? 'the session is halted' : whyBlocked(after, ctx.cfg)}.`}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ---------------------------------------------------------------- entry
|
|
186
|
+
export function failMsg(e ) {
|
|
187
|
+
if (e instanceof FailClosed) return `STOPBAR FAIL-CLOSED: ${e.message}${e.fix ? `\nFix: ${e.fix}` : ''}`
|
|
188
|
+
return `STOPBAR FAIL-CLOSED: ${e instanceof Error ? e.message : String(e)}`
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** One hook call. `raw` is the JSON Claude Code writes on stdin. Never throws. */
|
|
192
|
+
export function runHook(raw , t0 = performance.now()) {
|
|
193
|
+
let input
|
|
194
|
+
try { input = JSON.parse(raw) } catch (e) { return {code: 2, msg: `STOPBAR FAIL-CLOSED: hook input is not JSON (${(e ).message})`} }
|
|
195
|
+
if (!input || typeof input !== 'object') return {code: 2, msg: 'STOPBAR FAIL-CLOSED: hook input must be a JSON object'}
|
|
196
|
+
try {
|
|
197
|
+
const repo = findRepo(typeof input.cwd === 'string' && input.cwd ? input.cwd : process.cwd())
|
|
198
|
+
const g0 = gitCalls
|
|
199
|
+
return withLock(repo, () => {
|
|
200
|
+
const cfg = readConfig(repo)
|
|
201
|
+
const ctx = {repo, cfg, s: readState(repo), protect: {gitDir: repo.gitDir, stateDir: repo.stateDir, codeDirs: codeDirs()}, dirty: false}
|
|
202
|
+
const ev = input.hook_event_name
|
|
203
|
+
const res = ev === 'PreToolUse' ? pre(ctx, input)
|
|
204
|
+
: ev === 'PostToolUse' ? post(ctx, input, false)
|
|
205
|
+
: ev === 'PostToolUseFailure' ? post(ctx, input, true)
|
|
206
|
+
: ev === 'Stop' ? stop(ctx, input) : {code: 0}
|
|
207
|
+
if (ctx.dirty) writeState(repo, ctx.s)
|
|
208
|
+
appendEvent(repo, {source: 'hook-timing', event: ev, tool: input.tool_name, exit: res.code, gitCalls: gitCalls - g0,
|
|
209
|
+
handlerMs: Math.round(performance.now() - t0), processMs: Math.round(process.uptime() * 1000)})
|
|
210
|
+
return res
|
|
211
|
+
})
|
|
212
|
+
} catch (e) {
|
|
213
|
+
return {code: 2, msg: failMsg(e)}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// GENERATED by scripts/build.ts from src/kernel.ts with gdp-wired 0.60.0 compileJs. Do not edit.
|
|
2
|
+
// stopbar verify and the tests regenerate it and compare: KERNEL_SOURCE_SHA256 = sha256(src/kernel.ts),
|
|
3
|
+
// MACHINE_SHA256 = sha256(compileJs output below).
|
|
4
|
+
export const MACHINE_NAME = "stopbar_kernel_none"
|
|
5
|
+
export const KERNEL_SOURCE_SHA256 = "c602fc4bcf8ffb2e312bf838986e6d4b006af6d0e6c7447c8c61d5888adc5ef8"
|
|
6
|
+
export const MACHINE_SHA256 = "b397085e0cb13e2e50aa655c54de0744d08f2ebf500bf32af8ff3e8a11ba7321"
|
|
7
|
+
export const CODEC = {"inputs":[{"name":"op","kind":"oneOf","values":["EDIT_CODE","EDIT_TEST","RUN_BUILD","RUN_TESTS","READ_SECRET","SHELL","APPROVE_TESTS","APPROVE_SHELL","RESUME","COMMIT"]},{"name":"cost","kind":"nat"},{"name":"passed","kind":"bool"},{"name":"fullSuite","kind":"bool"},{"name":"suspicious","kind":"bool"},{"name":"risky","kind":"bool"},{"name":"treeChanged","kind":"bool"},{"name":"approveRev","kind":"nat"},{"name":"extra","kind":"nat"}],"state":[{"name":"treeRev","kind":"nat"},{"name":"buildOkRev","kind":"nat"},{"name":"testsOkRev","kind":"nat"},{"name":"testsTainted","kind":"bool"},{"name":"secretTainted","kind":"bool"},{"name":"shellToken","kind":"bool"},{"name":"buildFails","kind":"nat"},{"name":"testFails","kind":"nat"},{"name":"spent","kind":"nat"},{"name":"capNow","kind":"nat"},{"name":"halted","kind":"bool"},{"name":"decision","kind":"oneOf","values":["ALLOW","BLOCK","PAUSE"]},{"name":"commitExecuted","kind":"bool"},{"name":"gRev","kind":"nat"},{"name":"gBuildOk","kind":"bool"},{"name":"gTestsOk","kind":"bool"},{"name":"gMaxSusp","kind":"nat"},{"name":"gMaxAppr","kind":"nat"},{"name":"gBuildFails","kind":"nat"},{"name":"gTestFails","kind":"nat"}]}
|
|
8
|
+
// ---- compileJs output (unchanged) ----
|
|
9
|
+
// Generated from the machine stopbar_kernel_none. step consumes its state: keep the one it returns (fork to branch).
|
|
10
|
+
function $n(v, what) { if (typeof v !== 'number' || !Number.isSafeInteger(v) || v < 0) throw new RangeError(what + ' must be a natural below 2^53, not ' + v); return v; }
|
|
11
|
+
function $x(v) { if (v > 9007199254740991) throw new RangeError('a value passed 2^53 - 1, past which the proofs over the naturals no longer describe the run'); return v; }
|
|
12
|
+
const $wordDivisionValue = function wordDivisionValue(width, part, index, values) {
|
|
13
|
+
if (width !== 8 && width !== 16 && width !== 32 && width !== 64)
|
|
14
|
+
throw new Error('invalid word division width');
|
|
15
|
+
const n = width === 8 ? 1 : width / 16;
|
|
16
|
+
if (part !== 'q' && part !== 'r' && part !== 'c')
|
|
17
|
+
throw new Error('invalid word division part');
|
|
18
|
+
if (!Number.isSafeInteger(index) || (part === 'c' ? index < 1 || index > 2 * n : index < 0 || index >= n))
|
|
19
|
+
throw new Error('invalid word division index');
|
|
20
|
+
if (!Array.isArray(values) || values.length !== 2 * n)
|
|
21
|
+
throw new Error('invalid word division arity');
|
|
22
|
+
const base = width === 8 ? 256n : 65536n;
|
|
23
|
+
const digits = [];
|
|
24
|
+
for (const value of values) {
|
|
25
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
26
|
+
throw new Error('invalid word division operand');
|
|
27
|
+
digits.push(BigInt(value) % base);
|
|
28
|
+
}
|
|
29
|
+
let a = 0n, b = 0n, power = 1n;
|
|
30
|
+
for (let i = 0; i < n; i++) {
|
|
31
|
+
a += digits[i] * power;
|
|
32
|
+
b += digits[n + i] * power;
|
|
33
|
+
power *= base;
|
|
34
|
+
}
|
|
35
|
+
const q = b === 0n ? 0n : a / b;
|
|
36
|
+
const r = b === 0n ? 0n : a % b;
|
|
37
|
+
if (part === 'q' || part === 'r')
|
|
38
|
+
return Number(((part === 'q' ? q : r) / (base ** BigInt(index))) % base);
|
|
39
|
+
let carry = 0n;
|
|
40
|
+
for (let col = 0; col < index; col++) {
|
|
41
|
+
let total = carry + (col < n ? (r / (base ** BigInt(col))) % base : 0n);
|
|
42
|
+
for (let j = 0; j < n; j++) {
|
|
43
|
+
const k = col - j;
|
|
44
|
+
if (k >= 0 && k < n)
|
|
45
|
+
total += ((q / (base ** BigInt(j))) % base) * digits[n + k];
|
|
46
|
+
}
|
|
47
|
+
carry = total / base;
|
|
48
|
+
}
|
|
49
|
+
return Number(carry);
|
|
50
|
+
};
|
|
51
|
+
export function init(params) {
|
|
52
|
+
const env = {"maxRetries": $n(params["maxRetries"], "param maxRetries"), "cap": $n(params["cap"], "param cap")}, s = {};
|
|
53
|
+
return {scalars: {"treeRev": 1, "buildOkRev": 0, "testsOkRev": 0, "testsTainted": 0, "secretTainted": 0, "shellToken": 0, "buildFails": 0, "testFails": 0, "spent": 0, "capNow": env["cap"], "halted": 0, "decision": 0, "commitExecuted": 0, "gRev": 1, "gBuildOk": 0, "gTestsOk": 0, "gMaxSusp": 0, "gMaxAppr": 0, "gBuildFails": 0, "gTestFails": 0}, maps: {}};
|
|
54
|
+
}
|
|
55
|
+
export function fork(state) {
|
|
56
|
+
return {scalars: {...state.scalars}, maps: Object.fromEntries(Object.entries(state.maps).map(([name, cells]) => [name, new Map(cells)])), ...(state.defaults ? {defaults: {...state.defaults}} : {})};
|
|
57
|
+
}
|
|
58
|
+
export function step(params, state, input) {
|
|
59
|
+
const env = {"maxRetries": $n(params["maxRetries"], "param maxRetries"), "cap": $n(params["cap"], "param cap"), "op": $n(input["op"], "input op"), "cost": $n(input["cost"], "input cost"), "passed": $n(input["passed"], "input passed"), "fullSuite": $n(input["fullSuite"], "input fullSuite"), "suspicious": $n(input["suspicious"], "input suspicious"), "risky": $n(input["risky"], "input risky"), "treeChanged": $n(input["treeChanged"], "input treeChanged"), "approveRev": $n(input["approveRev"], "input approveRev"), "extra": $n(input["extra"], "input extra")}, s = state.scalars, dft = {...state.defaults}, maps = state.maps;
|
|
60
|
+
if (!((env["op"] < 10) && (env["passed"] <= 1) && (env["fullSuite"] <= 1) && (env["suspicious"] <= 1) && (env["risky"] <= 1) && (env["treeChanged"] <= 1) && ((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) || (env["cost"] === 0)))) throw new RangeError("stopbar_kernel_none: the request fails the rule's assumption, so no proof covers the step it would take");
|
|
61
|
+
const scalars = {...s, "treeRev": ((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 0) || (env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1)))) ? $x(s["treeRev"] + 1) : s["treeRev"]), "buildOkRev": (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (env["passed"] === 1)) ? s["treeRev"] : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (!(env["passed"] === 1))) ? 0 : s["buildOkRev"])), "testsOkRev": (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (env["passed"] === 1) && (env["fullSuite"] === 1)) ? s["treeRev"] : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1))) ? 0 : s["testsOkRev"])), "buildFails": ((env["op"] === 8) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (env["passed"] === 1)) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (!(env["passed"] === 1))) ? $x(s["buildFails"] + 1) : s["buildFails"]))), "testFails": ((env["op"] === 8) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (env["passed"] === 1) && (env["fullSuite"] === 1)) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1))) ? $x(s["testFails"] + 1) : s["testFails"]))), "spent": (((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1))) ? $x(s["spent"] + env["cost"]) : s["spent"]), "capNow": ((env["op"] === 8) ? $x(s["capNow"] + env["extra"]) : s["capNow"]), "gRev": ((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 0) || (env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1)))) ? $x(s["gRev"] + 1) : s["gRev"]), "gMaxSusp": ((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1))) && (env["suspicious"] === 1)) ? Math.max(s["gMaxSusp"], $x(s["gRev"] + 1)) : s["gMaxSusp"]), "gMaxAppr": (((env["op"] === 6) && (env["approveRev"] <= s["gRev"]) && (!(s["halted"] === 1))) ? Math.max(s["gMaxAppr"], env["approveRev"]) : s["gMaxAppr"]), "gBuildFails": ((env["op"] === 8) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (env["passed"] === 1)) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (!(env["passed"] === 1))) ? $x(s["gBuildFails"] + 1) : s["gBuildFails"]))), "gTestFails": ((env["op"] === 8) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (env["passed"] === 1) && (env["fullSuite"] === 1)) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1))) ? $x(s["gTestFails"] + 1) : s["gTestFails"]))), "testsTainted": ((((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1))) && (env["suspicious"] === 1)) && (0 <= 0)) || ((!(((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1))) && (env["suspicious"] === 1))) && ((((env["op"] === 6) && (env["approveRev"] === s["treeRev"]) && (!(s["halted"] === 1))) && (0 < 0)) || ((!((env["op"] === 6) && (env["approveRev"] === s["treeRev"]) && (!(s["halted"] === 1)))) && (s["testsTainted"] === 1))))) ? 1 : 0), "secretTainted": (((s["secretTainted"] === 1) || (((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 4))) ? 1 : 0), "shellToken": (((((env["op"] === 7) && (!(s["halted"] === 1))) && (0 <= 0)) || ((!((env["op"] === 7) && (!(s["halted"] === 1)))) && ((((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 5) && (env["risky"] === 1)) && (s["secretTainted"] === 1)) && (0 < 0)) || ((!((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 5) && (env["risky"] === 1)) && (s["secretTainted"] === 1))) && (s["shellToken"] === 1))))) ? 1 : 0), "halted": ((((env["op"] === 8) && (0 < 0)) || ((!(env["op"] === 8)) && ((s["halted"] === 1) || ((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))) || ((((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1))) && (env["maxRetries"] <= ((env["op"] === 8) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (env["passed"] === 1) && (env["fullSuite"] === 1)) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1))) ? $x(s["testFails"] + 1) : s["testFails"]))))) || (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (!(env["passed"] === 1))) && (env["maxRetries"] <= ((env["op"] === 8) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (env["passed"] === 1)) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (!(env["passed"] === 1))) ? $x(s["buildFails"] + 1) : s["buildFails"]))))))))) ? 1 : 0), "decision": ((env["op"] === 8) ? 0 : ((s["halted"] === 1) ? 2 : ((env["op"] === 6) ? (((env["op"] === 6) && (env["approveRev"] === s["treeRev"]) && (!(s["halted"] === 1))) ? 0 : 1) : ((env["op"] === 7) ? 0 : ((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))) || ((((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1))) && (env["maxRetries"] <= ((env["op"] === 8) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (env["passed"] === 1) && (env["fullSuite"] === 1)) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1))) ? $x(s["testFails"] + 1) : s["testFails"]))))) || (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (!(env["passed"] === 1))) && (env["maxRetries"] <= ((env["op"] === 8) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (env["passed"] === 1)) ? 0 : (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (!(env["passed"] === 1))) ? $x(s["buildFails"] + 1) : s["buildFails"])))))) || (((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1))) && (env["suspicious"] === 1)) || ((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 5) && (env["risky"] === 1)) && (s["secretTainted"] === 1) && (!(s["shellToken"] === 1)))) ? 2 : ((env["op"] === 9) ? ((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 9) && (s["buildOkRev"] === s["treeRev"]) && (s["testsOkRev"] === s["treeRev"]) && (!(s["testsTainted"] === 1))) ? 0 : 1) : 0)))))), "commitExecuted": ((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 9) && (s["buildOkRev"] === s["treeRev"]) && (s["testsOkRev"] === s["treeRev"]) && (!(s["testsTainted"] === 1))) ? 1 : 0), "gBuildOk": ((((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 0) || (env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1)))) && (0 < 0)) || ((!(((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 0) || (env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1))))) && (((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2)) && (env["passed"] === 1)) || ((!(((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 2))) && (s["gBuildOk"] === 1))))) ? 1 : 0), "gTestsOk": ((((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 0) || (env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1)))) && (0 < 0)) || ((!(((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && ((env["op"] === 0) || (env["op"] === 1) || ((env["op"] === 5) && (env["treeChanged"] === 1))))) && ((((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (env["passed"] === 1) && (env["fullSuite"] === 1)) && (0 <= 0)) || ((!((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (env["passed"] === 1) && (env["fullSuite"] === 1))) && ((((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1))) && (0 < 0)) || ((!((((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (!(s["halted"] === 1)) && (!((!((env["op"] === 6) || (env["op"] === 7) || (env["op"] === 8))) && (s["capNow"] <= $x(s["spent"] + env["cost"]))))) && (env["op"] === 3)) && (!(env["passed"] === 1)))) && (s["gTestsOk"] === 1))))))) ? 1 : 0)};
|
|
62
|
+
return Object.keys(dft).length ? {scalars, maps: state.maps, defaults: dft} : {scalars, maps: state.maps};
|
|
63
|
+
}
|