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/src/kernel.ts ADDED
@@ -0,0 +1,161 @@
1
+ // Stopbar kernel: the decision rule of the hooks, deterministic, in the gdp-wired 0.60.0 DSL.
2
+ //
3
+ // An action is one request with an `op` field. The wrapper supplies the classes deterministically:
4
+ // suspicious - EDIT_TEST / SHELL that changes the tree: a change of an existing test or of test config
5
+ // risky - SHELL: risky or outbound (true) or safe and local (false)
6
+ // treeChanged - SHELL: the wrapper sees a different git tree hash after the call (e.g. `sed -i`)
7
+ // approveRev - APPROVE_TESTS: the tree revision whose accumulated test changes a human reviewed
8
+ //
9
+ // The g* fields are ghost variables: the SPECIFICATION keeps them, not the implementation. Mutants change only the
10
+ // implementation, so the laws over g* do not repeat the COMMIT condition (no tautology).
11
+ import {bool, max, nat, no, oneOf, rule, when, yes} from 'gdp-wired'
12
+
13
+ // APPROVE_TESTS and APPROVE_SHELL are a full no-op in the halted state (step law L4.4, mutant M7).
14
+ export const MUTANTS = ['none', 'M1a', 'M1b', 'M2', 'M3', 'M3b', 'M4', 'M4b', 'M5', 'M6', 'M7'] as const
15
+ export type Mutant = (typeof MUTANTS)[number]
16
+
17
+ const OPS = ['EDIT_CODE', 'EDIT_TEST', 'RUN_BUILD', 'RUN_TESTS', 'READ_SECRET', 'SHELL',
18
+ 'APPROVE_TESTS', 'APPROVE_SHELL', 'RESUME', 'COMMIT'] as const
19
+
20
+ export function kernel(m: Mutant = 'none') {
21
+ return rule(`stopbar_kernel_${m}`)
22
+ .params({maxRetries: nat, cap: nat})
23
+ .pre(({maxRetries, cap}) => maxRetries.atLeast(1).and(cap.atLeast(1)))
24
+ .inputs({op: oneOf(...OPS), cost: nat, passed: bool, fullSuite: bool, suspicious: bool, risky: bool,
25
+ treeChanged: bool, approveRev: nat, extra: nat})
26
+ // human actions cost nothing (only agent actions count against the budget)
27
+ .assume(({op, cost}) => op.is('APPROVE_TESTS').or(op.is('APPROVE_SHELL'), op.is('RESUME')).implies(cost.eq(0)))
28
+ .state({
29
+ // implementation
30
+ treeRev: 1, buildOkRev: 0, testsOkRev: 0,
31
+ testsTainted: bool.from(false), secretTainted: bool.from(false), shellToken: bool.from(false),
32
+ buildFails: 0, testFails: 0, spent: 0, capNow: ({cap}) => cap,
33
+ halted: bool.from(false), decision: oneOf('ALLOW', 'BLOCK', 'PAUSE').from('ALLOW'), commitExecuted: bool.from(false),
34
+ // ghost (specification)
35
+ gRev: 1, gBuildOk: bool.from(false), gTestsOk: bool.from(false), gMaxSusp: 0, gMaxAppr: 0,
36
+ gBuildFails: 0, gTestFails: 0,
37
+ })
38
+ .step((e) => {
39
+ const {op, cost, passed, fullSuite, suspicious, risky, treeChanged, approveRev, extra, maxRetries} = e
40
+ const human = op.is('APPROVE_TESTS').or(op.is('APPROVE_SHELL'), op.is('RESUME'))
41
+ const agent = human.not()
42
+ const overBudget = agent.and(e.capNow.atMost(e.spent.plus(cost)))
43
+ const exec = agent.and(e.halted.not(), overBudget.not())
44
+
45
+ // --- specification: what changes the tree and what is a gate run (never mutated)
46
+ const specTreeEdit = exec.and(op.is('EDIT_CODE').or(op.is('EDIT_TEST'), op.is('SHELL').and(treeChanged)))
47
+ const specSuspEdit = exec.and(op.is('EDIT_TEST').or(op.is('SHELL').and(treeChanged)), suspicious)
48
+ const build = exec.and(op.is('RUN_BUILD'))
49
+ const tests = exec.and(op.is('RUN_TESTS'))
50
+ const specFullPass = tests.and(passed, fullSuite)
51
+
52
+ // --- implementation (the mutants act here)
53
+ const treeEdit = m === 'M1a' ? exec.and(op.is('EDIT_CODE').or(op.is('EDIT_TEST'))) : specTreeEdit
54
+ const fullPass = m === 'M1b' ? tests.and(passed) : specFullPass
55
+ const testFail = tests.and(passed.not())
56
+ const buildPass = build.and(passed)
57
+ const buildFail = build.and(passed.not())
58
+ const awake = e.halted.not() // human approvals act only outside the halted state
59
+ const approveT = m === 'M6' ? op.is('APPROVE_TESTS').and(awake) : op.is('APPROVE_TESTS').and(approveRev.eq(e.treeRev), awake)
60
+ const grantShell = m === 'M7' ? op.is('APPROVE_SHELL') : op.is('APPROVE_SHELL').and(awake) // M7: token also when halted
61
+ const riskyShell = exec.and(op.is('SHELL'), risky)
62
+ const needPause = m === 'M3b' ? no : riskyShell.and(e.secretTainted, e.shellToken.not())
63
+ const commitOk = exec.and(op.is('COMMIT'), e.buildOkRev.eq(e.treeRev), e.testsOkRev.eq(e.treeRev), e.testsTainted.not())
64
+
65
+ const testReset = m === 'M4' ? fullPass.or(buildPass) : m === 'M4b' ? fullPass.or(tests.and(passed)) : fullPass
66
+ const testFailsNext = when(op.is('RESUME'), 0, when(testReset, 0, when(testFail, e.testFails.plus(1), e.testFails)))
67
+ const buildFailsNext = when(op.is('RESUME'), 0, when(buildPass, 0, when(buildFail, e.buildFails.plus(1), e.buildFails)))
68
+ const loopHalt = testFail.and(maxRetries.atMost(testFailsNext)).or(buildFail.and(maxRetries.atMost(buildFailsNext)))
69
+ const haltedNext = when(op.is('RESUME'), no, e.halted.or(overBudget, loopHalt))
70
+
71
+ const taintClear = m === 'M2' ? approveT.or(fullPass) : approveT
72
+ const secretNext = m === 'M3' ? when(exec.and(op.is('EDIT_CODE')), no, e.secretTainted.or(exec.and(op.is('READ_SECRET'))))
73
+ : e.secretTainted.or(exec.and(op.is('READ_SECRET')))
74
+ const approveVisible = m === 'M5' ? no : e.halted // M5: a test approval lifts PAUSE in the halted state
75
+
76
+ const ALLOW = e.decision.of('ALLOW'), BLOCK = e.decision.of('BLOCK'), PAUSE = e.decision.of('PAUSE')
77
+ const decision = when(op.is('RESUME'), ALLOW,
78
+ when(approveVisible, PAUSE,
79
+ when(op.is('APPROVE_TESTS'), when(approveT, ALLOW, BLOCK),
80
+ when(op.is('APPROVE_SHELL'), ALLOW,
81
+ when(overBudget.or(loopHalt, specSuspEdit, needPause), PAUSE,
82
+ when(op.is('COMMIT'), when(commitOk, ALLOW, BLOCK), ALLOW))))))
83
+
84
+ return {
85
+ treeRev: when(treeEdit, e.treeRev.plus(1), e.treeRev),
86
+ buildOkRev: when(buildPass, e.treeRev, when(buildFail, 0, e.buildOkRev)),
87
+ testsOkRev: when(fullPass, e.treeRev, when(testFail, 0, e.testsOkRev)),
88
+ testsTainted: when(specSuspEdit, yes, when(taintClear, no, e.testsTainted)),
89
+ secretTainted: secretNext,
90
+ shellToken: when(grantShell, yes, when(riskyShell.and(e.secretTainted), no, e.shellToken)),
91
+ buildFails: buildFailsNext,
92
+ testFails: testFailsNext,
93
+ spent: when(agent.and(e.halted.not()), e.spent.plus(cost), e.spent),
94
+ capNow: when(op.is('RESUME'), e.capNow.plus(extra), e.capNow),
95
+ halted: haltedNext,
96
+ decision,
97
+ commitExecuted: commitOk,
98
+ // ghost
99
+ gRev: when(specTreeEdit, e.gRev.plus(1), e.gRev),
100
+ gBuildOk: when(specTreeEdit, no, when(build, passed, e.gBuildOk)),
101
+ gTestsOk: when(specTreeEdit, no, when(specFullPass, yes, when(tests.and(passed.not()), no, e.gTestsOk))),
102
+ gMaxSusp: when(specSuspEdit, max(e.gMaxSusp, e.gRev.plus(1)), e.gMaxSusp),
103
+ gMaxAppr: when(op.is('APPROVE_TESTS').and(approveRev.atMost(e.gRev), e.halted.not()), max(e.gMaxAppr, approveRev), e.gMaxAppr),
104
+ gBuildFails: when(op.is('RESUME'), 0, when(build.and(passed), 0, when(build.and(passed.not()), e.gBuildFails.plus(1), e.gBuildFails))),
105
+ gTestFails: when(op.is('RESUME'), 0, when(specFullPass, 0, when(tests.and(passed.not()), e.gTestFails.plus(1), e.gTestFails))),
106
+ }
107
+ })
108
+ }
109
+
110
+ type K = ReturnType<typeof kernel>
111
+
112
+ // Inductive lemmas: the simulation relation between the implementation and the ghosts. They add no requirement;
113
+ // they strengthen the induction (the prover does not find them on its own).
114
+ export function lemmas(r: K) {
115
+ return [
116
+ r.law('I1 tree revision starts at 1 and the ghost follows it', ({treeRev, gRev}) => treeRev.atLeast(1).and(gRev.eq(treeRev))),
117
+ r.law('I2 a build verified at the current revision means the spec saw a passing build since the last tree change',
118
+ ({buildOkRev, treeRev, gBuildOk}) => buildOkRev.atMost(treeRev).and(buildOkRev.eq(treeRev).implies(gBuildOk))),
119
+ r.law('I3 a suite verified at the current revision means the spec saw a full passing suite since the last tree change',
120
+ ({testsOkRev, treeRev, gTestsOk}) => testsOkRev.atMost(treeRev).and(testsOkRev.eq(treeRev).implies(gTestsOk))),
121
+ r.law('I4 suspicious edits never exceed the current revision; clean tests mean they are all approved',
122
+ ({gMaxSusp, gMaxAppr, gRev, testsTainted}) => gMaxSusp.atMost(gRev).and(gMaxAppr.atMost(gRev), testsTainted.not().implies(gMaxSusp.atMost(gMaxAppr)))),
123
+ r.law('I5 failure counters equal their ghosts', ({testFails, gTestFails, buildFails, gBuildFails}) =>
124
+ testFails.eq(gTestFails).and(buildFails.eq(gBuildFails))),
125
+ ]
126
+ }
127
+
128
+ export function laws(r: K) {
129
+ return {
130
+ laws: [
131
+ r.law('L1 commit only after build ok and full suite ok since last tree change',
132
+ ({commitExecuted, gBuildOk, gTestsOk}) => commitExecuted.implies(gBuildOk.and(gTestsOk))),
133
+ r.law('L2.1 commit only when every suspicious test edit is covered by a human approval',
134
+ ({commitExecuted, gMaxSusp, gMaxAppr}) => commitExecuted.implies(gMaxSusp.atMost(gMaxAppr))),
135
+ r.law('L4.3a maxRetries consecutive failed test runs without a full pass halt the session',
136
+ ({gTestFails, maxRetries, halted}) => maxRetries.atMost(gTestFails).implies(halted)),
137
+ r.law('L4.3b maxRetries consecutive failed builds halt the session',
138
+ ({gBuildFails, maxRetries, halted}) => maxRetries.atMost(gBuildFails).implies(halted)),
139
+ ],
140
+ stepLaws: [
141
+ r.stepLaw('L2.2 taint survives every action except a human test approval (RUN_TESTS full pass included)',
142
+ (b, a) => b.testsTainted.and(b.op.is('APPROVE_TESTS').not()).implies(a.testsTainted)),
143
+ r.stepLaw('L3.1 secretTainted is monotone',
144
+ (b, a) => b.secretTainted.implies(a.secretTainted)),
145
+ r.stepLaw('L3.2 exfiltration gate: risky shell after a secret read pauses unless a one-time human approval is held',
146
+ (b, a) => b.secretTainted.and(b.op.is('SHELL'), b.risky, b.shellToken.not()).implies(a.decision.is('PAUSE'))),
147
+ r.stepLaw('L3.3 the one-time shell approval is used up by the risky call it allows',
148
+ (b, a) => b.secretTainted.and(b.op.is('SHELL'), b.risky, b.halted.not(), b.spent.plus(b.cost).lt(b.capNow)).implies(a.shellToken.not())),
149
+ r.stepLaw('L4.1 halted: only RESUME unblocks; no ALLOW/BLOCK, no commit',
150
+ (b, a) => b.halted.and(b.op.is('RESUME').not()).implies(a.halted.and(a.decision.is('PAUSE'), a.commitExecuted.not()))),
151
+ r.stepLaw('L4.4 halted: no action but RESUME changes the key state fields',
152
+ (b, a) => b.halted.and(b.op.is('RESUME').not()).implies(
153
+ a.testsTainted.iff(b.testsTainted).and(a.shellToken.iff(b.shellToken), a.treeRev.eq(b.treeRev),
154
+ a.buildOkRev.eq(b.buildOkRev), a.testsOkRev.eq(b.testsOkRev), a.buildFails.eq(b.buildFails),
155
+ a.testFails.eq(b.testFails), a.spent.eq(b.spent), a.capNow.eq(b.capNow)))),
156
+ r.stepLaw('L4.2 reaching the budget cap halts at once',
157
+ (b, a) => b.op.is('APPROVE_TESTS').or(b.op.is('APPROVE_SHELL'), b.op.is('RESUME')).not()
158
+ .and(b.capNow.atMost(b.spent.plus(b.cost))).implies(a.halted)),
159
+ ],
160
+ }
161
+ }