@clear-capabilities/agentic-security-scanner 0.162.0 → 0.163.1
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 +34 -0
- package/bin/agentic-security.js +87 -0
- package/dist/1340.index.js +1871 -0
- package/dist/3477.index.js +727 -0
- package/dist/3499.index.js +23 -3
- package/dist/4113.index.js +1 -1
- package/dist/5014.index.js +833 -0
- package/dist/5756.index.js +2 -2
- package/dist/6257.index.js +1 -1
- package/dist/6907.index.js +248 -0
- package/dist/6994.index.js +1 -1
- package/dist/7033.index.js +33 -5
- package/dist/7439.index.js +1822 -0
- package/dist/7838.index.js +1 -1
- package/dist/9560.index.js +2318 -23
- package/dist/957.index.js +1 -1
- package/dist/9999.index.js +43 -7
- package/dist/agentic-security.mjs +12 -12
- package/dist/agentic-security.mjs.sha256 +1 -1
- package/package.json +33 -6
- package/src/capabilities/CLAUDE.md +114 -0
- package/src/capabilities/address.js +73 -0
- package/src/capabilities/attack-coverage.js +93 -0
- package/src/capabilities/decide.js +256 -0
- package/src/capabilities/delegate.js +69 -0
- package/src/capabilities/egress.js +20 -0
- package/src/capabilities/hook-advice.js +50 -0
- package/src/capabilities/index.js +32 -0
- package/src/capabilities/manifest.js +344 -0
- package/src/capabilities/outbound.js +189 -0
- package/src/capabilities/paths.js +57 -0
- package/src/capabilities/probes.js +211 -0
- package/src/capabilities/proxy.js +182 -0
- package/src/capabilities/reasons.js +67 -0
- package/src/capabilities/receipts.js +356 -0
- package/src/capabilities/records.js +58 -0
- package/src/capabilities/recovery.js +367 -0
- package/src/capabilities/report.js +81 -0
- package/src/capabilities/runner.js +236 -0
- package/src/capabilities/secrets.js +26 -0
- package/src/capabilities/tool-gate.js +119 -0
- package/src/capabilities/tool-registry.js +60 -0
- package/src/dataflow/CLAUDE.md +30 -0
- package/src/dataflow/catalog.js +42 -1
- package/src/dataflow/engine.js +67 -2
- package/src/dataflow/guard-dominance.js +352 -0
- package/src/discovery/CLAUDE.md +9 -0
- package/src/discovery/index.js +15 -1
- package/src/discovery/lenses.js +18 -0
- package/src/engine.js +80 -15
- package/src/integrations/index.js +5 -2
- package/src/ir/CLAUDE.md +14 -0
- package/src/ir/implicit-handler-params.js +74 -0
- package/src/ir/parser-cs.js +20 -0
- package/src/ir/parser-go.js +30 -13
- package/src/ir/parser-java.js +11 -0
- package/src/language/nix-eval-isolation.js +33 -5
- package/src/lineage/CLAUDE.md +31 -0
- package/src/lineage/deployment/adapter-cloud.js +202 -0
- package/src/lineage/deployment/adapter-compose.js +90 -0
- package/src/lineage/deployment/adapter-kit.js +142 -0
- package/src/lineage/deployment/adapter-kubernetes.js +247 -0
- package/src/lineage/deployment/attack-paths.js +443 -0
- package/src/lineage/deployment/boundaries-run.js +223 -0
- package/src/lineage/deployment/boundary-graph.js +567 -0
- package/src/lineage/deployment/cross-repo.js +253 -0
- package/src/lineage/deployment/drift.js +383 -0
- package/src/lineage/deployment/ingest.js +221 -0
- package/src/lineage/deployment/projection.js +244 -0
- package/src/lineage/deployment/trace-correlation.js +541 -0
- package/src/lineage/source-registry.js +19 -0
- package/src/mcp/CLAUDE.md +5 -2
- package/src/mcp/invariant-tools.js +64 -0
- package/src/mcp/portfolio-tools.js +79 -0
- package/src/mcp/redact.js +15 -0
- package/src/mcp/server.js +24 -3
- package/src/mcp/tools.js +13 -1
- package/src/posture/CLAUDE.md +332 -1
- package/src/posture/accuracy-scorecard.js +95 -4
- package/src/posture/assurance/rollback.js +214 -0
- package/src/posture/assurance/rollout.js +200 -0
- package/src/posture/business-logic.js +26 -4
- package/src/posture/evaluation/bakeoff.js +148 -0
- package/src/posture/evaluation/custody.js +200 -0
- package/src/posture/evaluation/deployment-ablation.js +416 -0
- package/src/posture/evaluation/dev-cases.js +65 -0
- package/src/posture/evaluation/economics.js +85 -0
- package/src/posture/evaluation/gates.js +161 -0
- package/src/posture/evaluation/grouping.js +101 -0
- package/src/posture/evaluation/interval.js +132 -0
- package/src/posture/evaluation/invariant-ablation.js +325 -0
- package/src/posture/evaluation/labels.js +200 -0
- package/src/posture/evaluation/protocol.js +308 -0
- package/src/posture/evaluation/release-gate.js +209 -0
- package/src/posture/evaluation/report.js +158 -0
- package/src/posture/evaluation/runner.js +273 -0
- package/src/posture/evaluation/scan-child.js +80 -0
- package/src/posture/evaluation/score.js +174 -0
- package/src/posture/evaluation/synthetic.js +90 -0
- package/src/posture/evaluation/why-missed.js +219 -0
- package/src/posture/fleet.js +11 -2
- package/src/posture/invariants/cli.js +95 -0
- package/src/posture/invariants/coverage.js +164 -0
- package/src/posture/invariants/export.js +178 -0
- package/src/posture/invariants/expressions.js +46 -0
- package/src/posture/invariants/lifecycle.js +289 -0
- package/src/posture/invariants/project-input.js +55 -0
- package/src/posture/invariants/repair.js +340 -0
- package/src/posture/invariants/run.js +45 -0
- package/src/posture/invariants/scenarios.js +311 -0
- package/src/posture/invariants/schema.js +167 -0
- package/src/posture/invariants/shrink.js +407 -0
- package/src/posture/invariants/state-assertions.js +165 -0
- package/src/posture/logic-claims.js +12 -0
- package/src/posture/model-routing.js +40 -0
- package/src/posture/oracles/adapters.js +172 -1
- package/src/posture/oracles/conformance.js +3 -2
- package/src/posture/oracles/oracle.js +23 -3
- package/src/posture/oracles/registry.js +1 -1
- package/src/posture/portfolio/backend.js +279 -0
- package/src/posture/portfolio/bundle.js +314 -0
- package/src/posture/portfolio/cli.js +142 -0
- package/src/posture/portfolio/manifest.js +263 -0
- package/src/posture/portfolio/progress.js +267 -0
- package/src/posture/portfolio/resume.js +130 -0
- package/src/posture/portfolio/retention.js +207 -0
- package/src/posture/portfolio/scheduler.js +488 -0
- package/src/posture/portfolio/signing.js +155 -0
- package/src/posture/portfolio/wording.js +59 -0
- package/src/posture/portfolio/work-units.js +405 -0
- package/src/posture/replay/replay.js +1 -0
- package/src/posture/routing/adapter.js +140 -0
- package/src/posture/routing/calibration.js +278 -0
- package/src/posture/routing/canary.js +141 -0
- package/src/posture/routing/control.js +35 -0
- package/src/posture/routing/decide.js +211 -0
- package/src/posture/routing/drift.js +137 -0
- package/src/posture/routing/economics.js +185 -0
- package/src/posture/routing/feedback.js +207 -0
- package/src/posture/routing/index.js +31 -0
- package/src/posture/routing/outcomes.js +281 -0
- package/src/posture/routing/promotion.js +291 -0
- package/src/posture/routing/receipts.js +59 -0
- package/src/posture/routing/report.js +142 -0
- package/src/posture/routing/shadow.js +53 -0
- package/src/posture/specification-mining.js +26 -0
- package/src/posture/verification/patch-negative.js +16 -4
- package/src/posture/verification/patch-promotion.js +10 -2
- package/src/report/index.js +51 -1
- package/src/sandbox/CLAUDE.md +10 -0
- package/src/sandbox/backend-userspace.js +91 -13
- package/dist/5613.index.js +0 -500
- package/dist/6239.index.js +0 -469
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
// Verify a repair against invariants (X-406).
|
|
2
|
+
//
|
|
3
|
+
// A patch for a business-logic violation is `verified-fix` here only when ALL of this is shown by executed, receipted runs on a
|
|
4
|
+
// disposable fixture, each one pinned by a replay manifest (so by commit, fixture hash, patch hash, toolchain and oracle logic):
|
|
5
|
+
//
|
|
6
|
+
// 1. the violated contract is APPROVED (verified ledger, `lifecycle.js`). A model-proposed or code-inferred contract cannot be
|
|
7
|
+
// the ground truth a repair is judged against, so a repair of a candidate violation is never `verified-fix`;
|
|
8
|
+
// 2. the original revision reproduces the violation, and the patched revision no longer does (`verifyPatchNegative`, the same
|
|
9
|
+
// four-run discipline, promotion and repair ledger every other repair uses; this is that module with the business-state
|
|
10
|
+
// oracle as the declared behaviour check, not a second implementation of it);
|
|
11
|
+
// 3. the authorized workflows still work: the scenario's own control flow plus any workflows the requester declares are run as
|
|
12
|
+
// the behaviour check on the original (a baseline, so a wrong expectation is not blamed on the patch) and on the patched
|
|
13
|
+
// revision. A patch that blocks everyone "fixes" the violation and fails here, because the business-state oracle will not
|
|
14
|
+
// call a flow clean unless the application visibly changed durable state;
|
|
15
|
+
// 4. every OTHER approved contract on the same fixture is still not violated: its bounded scenarios run on both revisions. A
|
|
16
|
+
// violation that appears only on the patched revision is a regression and blocks; one already present on the original is
|
|
17
|
+
// reported as pre-existing and is not blamed on the patch. Proposed contracts are not required (they are advisory).
|
|
18
|
+
//
|
|
19
|
+
// Anything else blocks, with an explicit reason code from `REASON_CODES`: failed setup (a prerequisite, the harness, a rejected
|
|
20
|
+
// manifest), an unsupported or inconclusive check, a violation that is still reproduced, a broken authorized workflow, an
|
|
21
|
+
// unrelated invariant that now fails, an unapproved contract. A blocked result never reads as a pass.
|
|
22
|
+
//
|
|
23
|
+
// The regression artifact. A verified repair yields a self-contained document: the exact patch, the invariant revision and its
|
|
24
|
+
// approval evidence, the exploit scenario, and one replay manifest per leg with its expected outcome. `runRegressionArtifact`
|
|
25
|
+
// re-executes every leg from the artifact alone (no agent conversation, no in-process receipt: each leg runs again through the
|
|
26
|
+
// trust boundary) and passes only if every leg settles as recorded. It carries the fixture and patch source, so it refuses to be
|
|
27
|
+
// built if either holds a credential.
|
|
28
|
+
//
|
|
29
|
+
// Limits, stated: only the declared exploit scenario, the declared authorized workflows and the other contracts' bounded
|
|
30
|
+
// scenarios are exercised; the patch is not shown correct anywhere else. The approval evidence in the artifact is a digest and a
|
|
31
|
+
// transition id; verifying the HMAC needs the operator's key (symmetric: tamper-evidence, not third-party non-repudiation).
|
|
32
|
+
// Receipts are valid inside the verifier's process; the artifact is how a result crosses a process, by re-execution. Linux is
|
|
33
|
+
// not claimed.
|
|
34
|
+
import { digestOf, semanticId } from '../assurance/identity.js';
|
|
35
|
+
import { SCHEMA_VERSION, isPlainObject, isCommit } from '../assurance/schema-kit.js';
|
|
36
|
+
import { featureStatus, resolveAssuranceConfig } from '../assurance/config.js';
|
|
37
|
+
import { createReplayManifest, replayManifest } from '../replay/replay.js';
|
|
38
|
+
import { verifyPatchNegative } from '../verification/patch-negative.js';
|
|
39
|
+
import { appendRepairRecord, verificationRepairFor, withRepairStatus } from '../verification/repair-records.js';
|
|
40
|
+
import { validateInvariant } from './schema.js';
|
|
41
|
+
import { verifyLedger } from './lifecycle.js';
|
|
42
|
+
import { generateScenarios, FEATURE } from './scenarios.js';
|
|
43
|
+
import { findSecrets } from './export.js';
|
|
44
|
+
|
|
45
|
+
const ARTIFACT_SCHEMA = 'agentic-security/invariant-regression-artifact';
|
|
46
|
+
export const REASON_CODES = Object.freeze([
|
|
47
|
+
'feature-disabled', 'bad-request', 'invariant-not-approved', 'setup-failed', 'unsupported-check', 'original-not-reproduced',
|
|
48
|
+
'violation-not-eliminated', 'authorized-workflow-broken', 'workflow-regression', 'workflow-check-inconclusive', 'workflow-baseline-invalid',
|
|
49
|
+
'invariant-regression', 'preservation-inconclusive', 'preservation-unsupported', 'patched-check-inconclusive',
|
|
50
|
+
'oracle-changed', 'environment-mismatch', 'promotion-refused', 'revision-unbound', 'artifact-refused',
|
|
51
|
+
]);
|
|
52
|
+
const REQUIRED_FEATURES = Object.freeze([FEATURE, 'verification-oracles', 'patch-negative-verification']);
|
|
53
|
+
const SCENARIO_SCHEMA = 'agentic-security/invariant-scenario';
|
|
54
|
+
const MAX_WORKFLOW_ITEMS = 12;
|
|
55
|
+
const EXPECT = Object.freeze({ original: 'confirmed', patched: 'refuted', workflowBaseline: 'refuted', workflow: 'refuted' });
|
|
56
|
+
|
|
57
|
+
const copy = (v) => JSON.parse(JSON.stringify(v));
|
|
58
|
+
const blocker = (code, step, reason, extra = {}) => ({ code, step, reason: String(reason).slice(0, 400), ...extra });
|
|
59
|
+
|
|
60
|
+
// patch-negative's typed failure -> the reason code a reader of an invariant repair sees
|
|
61
|
+
const CODE_OF = Object.freeze({
|
|
62
|
+
'original-not-reproduced': 'original-not-reproduced',
|
|
63
|
+
'patched-still-exploitable': 'violation-not-eliminated',
|
|
64
|
+
'patched-build-broken': 'authorized-workflow-broken',
|
|
65
|
+
'patched-negative-inconclusive': 'patched-check-inconclusive',
|
|
66
|
+
'functional-inconclusive': 'workflow-check-inconclusive',
|
|
67
|
+
'functional-regression-detected': 'workflow-regression',
|
|
68
|
+
'functional-baseline-invalid': 'workflow-baseline-invalid',
|
|
69
|
+
'functional-omitted': 'unsupported-check',
|
|
70
|
+
'prerequisite-unmet': 'setup-failed', 'harness-error': 'setup-failed', 'manifest-rejected': 'setup-failed', 'content-hash-mismatch': 'setup-failed',
|
|
71
|
+
'oracle-changed': 'oracle-changed', 'environment-mismatch': 'environment-mismatch', 'promotion-refused': 'promotion-refused', 'revision-unbound': 'revision-unbound',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/** The reason code (from `REASON_CODES`) for a patch-negative failure code; unknown ones become `setup-failed`, never a pass. */
|
|
75
|
+
export function reasonCodeFor(failureCode) { return CODE_OF[failureCode] || 'setup-failed'; }
|
|
76
|
+
|
|
77
|
+
const validStep = (s, actors) => isPlainObject(s) && actors.has(s.actor) && typeof s.action === 'string' && /^[A-Za-z_$][\w$]{0,63}$/.test(s.action)
|
|
78
|
+
&& (s.args === undefined || (Array.isArray(s.args) && s.args.length <= 4));
|
|
79
|
+
|
|
80
|
+
/** The business-state inputs of the authorized-workflow check: the scenario's control flow plus declared workflows, as both phases. */
|
|
81
|
+
function workflowInputs(scenario, extra) {
|
|
82
|
+
const actors = new Set(scenario.inputs.actors.map((a) => a.id));
|
|
83
|
+
const steps = copy(scenario.inputs.control);
|
|
84
|
+
for (const wf of Array.isArray(extra) ? extra : []) {
|
|
85
|
+
if (!Array.isArray(wf) || !wf.every((s) => validStep(s, actors))) return { error: 'a declared workflow is a list of steps by declared actors, each with an action identifier' };
|
|
86
|
+
for (const s of wf) steps.push(copy(s));
|
|
87
|
+
}
|
|
88
|
+
if (steps.length > MAX_WORKFLOW_ITEMS) return { error: `the authorized workflows exceed ${MAX_WORKFLOW_ITEMS} steps` };
|
|
89
|
+
return { inputs: { ...copy(scenario.inputs), control: copy(steps), attack: steps } };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function approvalEvidence(ledger, verified, invariant) {
|
|
93
|
+
const rec = [...(ledger?.records || [])].reverse().find((r) => r.invariantId === invariant.id && r.action === 'approve');
|
|
94
|
+
const head = ledger?.records?.length ? ledger.records[ledger.records.length - 1].id : null;
|
|
95
|
+
return { state: verified.ok ? (verified.states[invariant.id] ?? 'unrecorded') : 'unverified', transitionId: rec?.id ?? null, reviewer: rec?.actor?.id ?? null, authority: rec?.policyId ?? null, ledgerHead: head, ledgerDigest: ledger ? digestOf(ledger.records) : null };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function runLeg(manifest, fixtureFiles, patchFiles, o) {
|
|
99
|
+
try {
|
|
100
|
+
return await replayManifest({ manifest, fixtureFiles, patchFiles }, { config: o.config, runOptions: o.runOptions, hostEnvironment: o.hostEnvironment });
|
|
101
|
+
} catch (e) {
|
|
102
|
+
return { status: 'rejected', executed: false, outcome: null, errors: [{ code: 'RUN_FAILED', message: String(e?.message || e).slice(0, 200) }] };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Run the bounded scenarios of one other approved contract on both revisions and judge preservation. */
|
|
107
|
+
async function preserveOther(other, ctx) {
|
|
108
|
+
const entry = { invariantId: other.id, key: other.key, revision: other.revision, status: 'preserved', legs: [], preexisting: [], blockers: [] };
|
|
109
|
+
const gen = generateScenarios(other, { fixture: ctx.fixture, config: ctx.config, bounds: ctx.bounds });
|
|
110
|
+
if (gen.status !== 'ok' || !gen.scenarios.length) {
|
|
111
|
+
entry.status = 'unsupported';
|
|
112
|
+
entry.blockers.push(blocker('preservation-unsupported', `preserve:${other.key}`, gen.reason || (gen.unsupported[0]?.reason ?? 'no scenario could be built for this contract on this fixture'), { invariant: other.id }));
|
|
113
|
+
return entry;
|
|
114
|
+
}
|
|
115
|
+
for (const s of gen.scenarios) {
|
|
116
|
+
const mk = (patchFiles) => createReplayManifest({
|
|
117
|
+
hypothesisId: s.id, commit: ctx.commit, fixtureFiles: ctx.fixture.files, patchFiles, oracleId: 'business-state', entry: s.entry, inputs: s.inputs, budgets: { timeoutMs: s.limits.timeBudgetMs },
|
|
118
|
+
});
|
|
119
|
+
const baseManifest = mk(null);
|
|
120
|
+
const patchManifest = mk(ctx.patchFiles);
|
|
121
|
+
const base = await runLeg(baseManifest, ctx.fixture.files, null, ctx);
|
|
122
|
+
const pat = await runLeg(patchManifest, ctx.fixture.files, ctx.patchFiles, ctx);
|
|
123
|
+
const where = `preserve:${other.key}:${s.kind}`;
|
|
124
|
+
const leg = { scenarioId: s.id, kind: s.kind, baseline: base.outcome ?? base.status, patched: pat.outcome ?? pat.status, baselineReceipt: base.receipt?.receiptDigest ?? null, patchedReceipt: pat.receipt?.receiptDigest ?? null };
|
|
125
|
+
entry.legs.push(leg);
|
|
126
|
+
if (base.status !== 'completed' || pat.status !== 'completed') {
|
|
127
|
+
const why = base.status !== 'completed' ? base : pat;
|
|
128
|
+
entry.blockers.push(blocker('setup-failed', where, (why.prerequisites || []).map((p) => `${p.kind} ${p.id} (${p.state})`).join('; ') || why.errors?.[0]?.message || 'the preservation run did not execute', { invariant: other.id }));
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (!['refuted', 'confirmed'].includes(base.outcome)) {
|
|
132
|
+
entry.blockers.push(blocker('preservation-inconclusive', where, `the original revision settled '${base.outcome}' for this contract, so preservation cannot be shown`, { invariant: other.id }));
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (base.outcome === 'confirmed') {
|
|
136
|
+
leg.note = 'pre-existing violation on the original revision; not blamed on the patch';
|
|
137
|
+
entry.preexisting.push({ scenarioId: s.id, kind: s.kind, patched: pat.outcome });
|
|
138
|
+
if (!['refuted', 'confirmed'].includes(pat.outcome)) entry.blockers.push(blocker('preservation-inconclusive', where, `the patched revision settled '${pat.outcome}'`, { invariant: other.id }));
|
|
139
|
+
ctx.legs.push({ role: `${where}:baseline`, patched: false, manifest: baseManifest, expected: base.outcome }, { role: `${where}:patched`, patched: true, manifest: patchManifest, expected: pat.outcome });
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (pat.outcome === 'confirmed') entry.blockers.push(blocker('invariant-regression', where, `the patch makes an approved contract fail that held on the original: ${String(pat.record?.reason || '').slice(0, 200)}`, { invariant: other.id }));
|
|
143
|
+
else if (pat.outcome !== 'refuted') entry.blockers.push(blocker('preservation-inconclusive', where, `the patched revision settled '${pat.outcome}': ${String(pat.record?.reason || '').slice(0, 200)}`, { invariant: other.id }));
|
|
144
|
+
ctx.legs.push({ role: `${where}:baseline`, patched: false, manifest: baseManifest, expected: 'refuted' }, { role: `${where}:patched`, patched: true, manifest: patchManifest, expected: 'refuted' });
|
|
145
|
+
}
|
|
146
|
+
if (entry.blockers.length) entry.status = 'blocked';
|
|
147
|
+
return entry;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Verify one repair against an approved invariant, the authorized workflows and every other approved invariant. Never throws.
|
|
152
|
+
*
|
|
153
|
+
* @param {object} req
|
|
154
|
+
* @param {object} req.invariant the violated contract (a valid invariant document)
|
|
155
|
+
* @param {object} req.ledger the approval ledger; the contract must be approved in it
|
|
156
|
+
* @param {object} req.scenario a scenario that reproduces the violation (generated, or the minimized one from `shrinkScenario`)
|
|
157
|
+
* @param {{ files: object }} req.fixture the disposable fixture the scenario was generated from
|
|
158
|
+
* @param {{ files: object }} req.patch the exact diff: the new content of each changed file
|
|
159
|
+
* @param {string} req.commit exact revision the patch is proposed against
|
|
160
|
+
* @param {object[][]} [req.workflows] extra authorized workflows (lists of steps by declared actors)
|
|
161
|
+
* @param {object[]} [req.others] other invariant documents on the same fixture; the approved ones must still hold
|
|
162
|
+
* @param {object[]} [req.repairRecords] an earlier repair ledger to extend
|
|
163
|
+
* @param {object} [req.shrinkRecordId] id of the shrink record when the scenario is a minimized one (carried into the artifact)
|
|
164
|
+
* @param {object} [o] { config, runOptions, signer, bounds }
|
|
165
|
+
*/
|
|
166
|
+
export async function verifyInvariantRepair(req, o = {}) {
|
|
167
|
+
const config = o.config || resolveAssuranceConfig({ env: process.env });
|
|
168
|
+
const base = { status: 'not-verified-fix', verifiedFix: false, blockers: [], patchNegative: null, preservation: [], notRequired: [], artifact: null, repairRecords: Array.isArray(req?.repairRecords) ? req.repairRecords : [], verificationRecord: null, summary: '' };
|
|
169
|
+
const done = (extra) => {
|
|
170
|
+
const r = { ...base, ...extra };
|
|
171
|
+
r.reasonCodes = [...new Set(r.blockers.map((b) => b.code))];
|
|
172
|
+
r.summary = r.verifiedFix
|
|
173
|
+
? 'verified-fix: the violation is gone on the patched revision, the authorized workflows and every other approved contract still hold. Only the declared scenario, workflows and the other contracts\' bounded scenarios were exercised.'
|
|
174
|
+
: `NOT verified-fix: ${r.blockers.map((b) => `${b.code} (${b.step})`).join('; ') || 'not reached'}`;
|
|
175
|
+
return r;
|
|
176
|
+
};
|
|
177
|
+
const stop = (status, ...blockers) => done({ status, blockers });
|
|
178
|
+
|
|
179
|
+
for (const f of REQUIRED_FEATURES) {
|
|
180
|
+
const gate = featureStatus(config, f);
|
|
181
|
+
if (gate.status !== 'ok') return stop('disabled', blocker('feature-disabled', 'feature-gate', `${f} is not available: ${gate.reason}`));
|
|
182
|
+
}
|
|
183
|
+
// ---- shape: nothing executes on a malformed request
|
|
184
|
+
const inv = req?.invariant;
|
|
185
|
+
const valid = validateInvariant(inv);
|
|
186
|
+
const scenario = req?.scenario;
|
|
187
|
+
const fixtureFiles = req?.fixture?.files;
|
|
188
|
+
const patchFiles = req?.patch?.files;
|
|
189
|
+
if (!valid.ok) return stop('rejected', blocker('bad-request', 'input-validation', 'the invariant is not valid'));
|
|
190
|
+
if (!isPlainObject(scenario) || scenario.schema !== SCENARIO_SCHEMA || scenario.invariant?.id !== inv.id) return stop('rejected', blocker('bad-request', 'input-validation', 'the scenario is missing or belongs to a different invariant'));
|
|
191
|
+
if (!isPlainObject(fixtureFiles) || digestOf(fixtureFiles) !== scenario.fixtureDigest) return stop('rejected', blocker('bad-request', 'input-validation', 'the fixture is missing or does not match the scenario\'s pinned digest'));
|
|
192
|
+
if (!isPlainObject(patchFiles) || !Object.keys(patchFiles).length) return stop('rejected', blocker('bad-request', 'input-validation', 'a repair needs a non-empty patch'));
|
|
193
|
+
if (!isCommit(req.commit)) return stop('rejected', blocker('revision-unbound', 'input-validation', 'no exact commit was given: a verdict cannot be tied to a revision'));
|
|
194
|
+
const wf = workflowInputs(scenario, req.workflows);
|
|
195
|
+
if (wf.error) return stop('rejected', blocker('bad-request', 'input-validation', wf.error));
|
|
196
|
+
|
|
197
|
+
// ---- the contract must be approved: a repair is judged against a requirement a reviewer owns, not against a guess
|
|
198
|
+
const verified = verifyLedger(req.ledger, { signer: o.signer });
|
|
199
|
+
const approval = approvalEvidence(req.ledger, verified, inv);
|
|
200
|
+
if (approval.state !== 'approved') {
|
|
201
|
+
return done({ status: 'not-verified-fix', approval, blockers: [blocker('invariant-not-approved', 'approval', approval.state === 'unverified'
|
|
202
|
+
? 'the approval ledger does not verify, so no contract counts as approved'
|
|
203
|
+
: `the contract is ${approval.state}: only a reviewer-approved contract can define a verified fix; a model- or code-derived contract does not establish its own ground truth`)] });
|
|
204
|
+
}
|
|
205
|
+
const others = (Array.isArray(req.others) ? req.others : []).filter((x) => isPlainObject(x) && x.id !== inv.id);
|
|
206
|
+
const requiredOthers = []; const notRequired = [];
|
|
207
|
+
for (const x of others) {
|
|
208
|
+
const state = verified.states[x.id];
|
|
209
|
+
if (state === 'approved' && validateInvariant(x).ok) requiredOthers.push(x);
|
|
210
|
+
else notRequired.push({ invariantId: x.id ?? null, key: x.key ?? null, state: state ?? 'unrecorded', reason: 'only approved contracts must be preserved; this one is advisory' });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ---- the four-run repair verification, with the authorized workflows as the declared behaviour check
|
|
214
|
+
const budgets = { timeoutMs: scenario.limits.timeBudgetMs };
|
|
215
|
+
const pn = await verifyPatchNegative({
|
|
216
|
+
hypothesisId: scenario.id, commit: req.commit, ledger: req.repairRecords,
|
|
217
|
+
original: { files: fixtureFiles, entry: scenario.entry, oracleId: 'business-state', inputs: scenario.inputs, budgets },
|
|
218
|
+
patch: { files: patchFiles },
|
|
219
|
+
functional: { oracleId: 'business-state', inputs: wf.inputs, budgets },
|
|
220
|
+
}, { config, runOptions: o.runOptions });
|
|
221
|
+
const common = { approval, patchNegative: pn, notRequired, repairRecords: pn.repairRecords, verificationRecord: pn.verificationRecord };
|
|
222
|
+
if (!pn.verifiedFix) {
|
|
223
|
+
return done({ ...common, status: pn.status === 'disabled' ? 'disabled' : 'not-verified-fix', blockers: [blocker(reasonCodeFor(pn.failureCode), pn.incompleteStep || 'verification', pn.reason || 'the repair verification did not complete', { underlying: pn.failureCode, resumable: pn.resumable === true })] });
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ---- every other approved contract must still hold
|
|
227
|
+
const ctx = { fixture: { files: fixtureFiles }, patchFiles, commit: req.commit, config, runOptions: o.runOptions, bounds: o.bounds, legs: [] };
|
|
228
|
+
const preservation = [];
|
|
229
|
+
for (const x of requiredOthers) preservation.push(await preserveOther(x, ctx));
|
|
230
|
+
const blockers = preservation.flatMap((p) => p.blockers);
|
|
231
|
+
if (blockers.length) {
|
|
232
|
+
const rejected = appendRepairRecord(pn.repairRecords, { kind: 'rejected', hypothesisId: scenario.id, revision: req.commit, diffDigest: pn.patchDigest, step: 'invariant-preservation', reason: `${blockers[0].code}: ${blockers[0].reason}`.slice(0, 300) });
|
|
233
|
+
const repairRecords = rejected.ok ? rejected.records : pn.repairRecords;
|
|
234
|
+
const downgraded = pn.records?.original ? withRepairStatus(pn.records.original, verificationRepairFor(repairRecords, scenario.id, pn.patchDigest)) : pn.verificationRecord;
|
|
235
|
+
return done({ ...common, status: 'not-verified-fix', preservation, blockers, repairRecords, verificationRecord: downgraded });
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const artifact = buildRegressionArtifact({
|
|
239
|
+
invariant: inv, approval, scenario, fixtureFiles, patchFiles, commit: req.commit, budgets, workflowInputs: wf.inputs, extraLegs: ctx.legs, pn, shrinkRecordId: req.shrinkRecordId ?? null, repairRecords: pn.repairRecords,
|
|
240
|
+
});
|
|
241
|
+
if (!artifact.ok) {
|
|
242
|
+
return done({ ...common, status: 'not-verified-fix', preservation, blockers: [blocker('artifact-refused', 'regression-artifact', artifact.reason)], artifactErrors: artifact.errors });
|
|
243
|
+
}
|
|
244
|
+
return done({ ...common, status: 'verified-fix', verifiedFix: true, preservation, artifact: artifact.artifact });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ---------------------------------------------------------------- the regression artifact
|
|
248
|
+
|
|
249
|
+
function leg(role, manifest, patched, expected) { return { role, patched, expected, manifest }; }
|
|
250
|
+
|
|
251
|
+
function buildRegressionArtifact({ invariant, approval, scenario, fixtureFiles, patchFiles, commit, budgets, workflowInputs: wfi, extraLegs, pn, shrinkRecordId, repairRecords }) {
|
|
252
|
+
const secrets = [...findSecrets(fixtureFiles).map((s) => ({ ...s, where: `fixture:${s.where}` })), ...findSecrets(patchFiles).map((s) => ({ ...s, where: `patch:${s.where}` }))];
|
|
253
|
+
if (secrets.length) return { ok: false, reason: 'the fixture or the patch holds secret-looking content; a self-contained artifact would embed it', errors: secrets };
|
|
254
|
+
const mk = (spec) => createReplayManifest({ hypothesisId: scenario.id, commit, fixtureFiles, patchFiles: spec.patched ? patchFiles : null, oracleId: 'business-state', entry: scenario.entry, inputs: spec.inputs, budgets, expected: { outcome: spec.expected } });
|
|
255
|
+
const legs = [
|
|
256
|
+
leg('original-positive', mk({ patched: false, inputs: scenario.inputs, expected: EXPECT.original }), false, EXPECT.original),
|
|
257
|
+
leg('patched-negative', mk({ patched: true, inputs: scenario.inputs, expected: EXPECT.patched }), true, EXPECT.patched),
|
|
258
|
+
leg('workflow-baseline', mk({ patched: false, inputs: wfi, expected: EXPECT.workflowBaseline }), false, EXPECT.workflowBaseline),
|
|
259
|
+
leg('workflow-preserved', mk({ patched: true, inputs: wfi, expected: EXPECT.workflow }), true, EXPECT.workflow),
|
|
260
|
+
...extraLegs,
|
|
261
|
+
];
|
|
262
|
+
// the manifests rebuilt here must be the ones the verification actually ran: compare what each receipt pinned
|
|
263
|
+
const pins = [['original', legs[0]], ['patched', legs[1]], ['functionalBaseline', legs[2]], ['functional', legs[3]]];
|
|
264
|
+
for (const [role, l] of pins) {
|
|
265
|
+
const r = pn.receipts?.[role];
|
|
266
|
+
if (!r || r.request.inputsDigest !== l.manifest.inputsDigest || r.request.entry !== l.manifest.entry) return { ok: false, reason: `the ${role} manifest does not match the run the verification executed`, errors: [] };
|
|
267
|
+
}
|
|
268
|
+
const body = {
|
|
269
|
+
schema: ARTIFACT_SCHEMA, schemaVersion: SCHEMA_VERSION, hypothesisId: scenario.id, commit,
|
|
270
|
+
patch: { digest: digestOf(patchFiles), files: copy(patchFiles) },
|
|
271
|
+
fixture: { digest: digestOf(fixtureFiles), files: copy(fixtureFiles) },
|
|
272
|
+
invariant: { id: invariant.id, key: invariant.key, revision: invariant.revision, class: invariant.class, document: copy(invariant), approval },
|
|
273
|
+
scenario: { id: scenario.id, digest: digestOf(scenario), document: copy(scenario), shrinkRecordId },
|
|
274
|
+
legs,
|
|
275
|
+
repair: { diffDigest: pn.patchDigest, ledger: copy(repairRecords) },
|
|
276
|
+
scope: 'the declared exploit scenario, the declared authorized workflows and the bounded scenarios of the other approved contracts, on one disposable fixture; the patch is not shown correct anywhere else',
|
|
277
|
+
};
|
|
278
|
+
body.id = semanticId('rart', { ...body, legDigests: legs.map((l) => l.manifest.id) }, ['hypothesisId', 'commit', 'patch', 'invariant', 'legDigests']);
|
|
279
|
+
body.artifactDigest = digestOf({ ...body });
|
|
280
|
+
return { ok: true, artifact: body };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Structural and integrity check of an artifact, without executing anything. Never throws. */
|
|
284
|
+
export function validateRegressionArtifact(a) {
|
|
285
|
+
const errors = [];
|
|
286
|
+
const fail = (code, message) => errors.push({ code, message });
|
|
287
|
+
try {
|
|
288
|
+
if (!isPlainObject(a) || a.schema !== ARTIFACT_SCHEMA) return { ok: false, errors: [{ code: 'malformed', message: 'not a regression artifact' }] };
|
|
289
|
+
const { artifactDigest, ...rest } = a;
|
|
290
|
+
if (digestOf(rest) !== artifactDigest) fail('artifact-tampered', 'the artifact does not match its digest');
|
|
291
|
+
if (digestOf(a.patch?.files) !== a.patch?.digest) fail('patch-tampered', 'the patch files do not match the recorded patch digest');
|
|
292
|
+
if (digestOf(a.fixture?.files) !== a.fixture?.digest) fail('fixture-tampered', 'the fixture files do not match the recorded fixture digest');
|
|
293
|
+
if (digestOf(a.scenario?.document) !== a.scenario?.digest) fail('scenario-tampered', 'the scenario does not match its digest');
|
|
294
|
+
if (a.scenario?.document?.fixtureDigest !== a.fixture?.digest) fail('fixture-pin-mismatch', 'the scenario pins a different fixture than the artifact carries');
|
|
295
|
+
if (a.invariant?.document?.id !== a.invariant?.id) fail('invariant-mismatch', 'the invariant document does not match the recorded id');
|
|
296
|
+
if (a.invariant?.approval?.state !== 'approved') fail('not-approved', 'the artifact records an invariant that was not approved');
|
|
297
|
+
if (!Array.isArray(a.legs) || a.legs.length < 4) fail('legs-missing', 'the four required legs are not all present');
|
|
298
|
+
for (const l of a.legs || []) {
|
|
299
|
+
if (l.manifest?.repository?.commit !== a.commit) fail('leg-commit', `leg ${l.role} is pinned to a different commit`);
|
|
300
|
+
if (l.manifest?.fixture?.digest !== a.fixture?.digest) fail('leg-fixture', `leg ${l.role} pins a different fixture`);
|
|
301
|
+
const wantPatch = l.patched ? a.patch?.digest : null;
|
|
302
|
+
if ((l.manifest?.patch?.digest ?? null) !== wantPatch) fail('leg-patch', `leg ${l.role} does not carry the artifact's exact patch`);
|
|
303
|
+
}
|
|
304
|
+
const roles = (a.legs || []).map((l) => l.role);
|
|
305
|
+
for (const r of ['original-positive', 'patched-negative', 'workflow-baseline', 'workflow-preserved']) if (!roles.includes(r)) fail('leg-missing', `required leg '${r}' is absent`);
|
|
306
|
+
} catch (e) {
|
|
307
|
+
fail('malformed', `the check could not complete: ${String(e?.message || e).slice(0, 120)}`);
|
|
308
|
+
}
|
|
309
|
+
return { ok: errors.length === 0, errors };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Re-execute a regression artifact from the artifact alone. Passes only if every leg settles as recorded. Never throws.
|
|
314
|
+
* @param {object} artifact
|
|
315
|
+
* @param {object} [o] { config, runOptions, hostEnvironment }
|
|
316
|
+
* @returns {Promise<{ status: 'passed'|'failed'|'invalid'|'prerequisite-unmet'|'disabled', legs: object[], reasonCodes: string[], summary: string }>}
|
|
317
|
+
*/
|
|
318
|
+
export async function runRegressionArtifact(artifact, o = {}) {
|
|
319
|
+
const check = validateRegressionArtifact(artifact);
|
|
320
|
+
if (!check.ok) return { status: 'invalid', legs: [], reasonCodes: [...new Set(check.errors.map((e) => e.code))], errors: check.errors, summary: `the artifact was not run: ${check.errors[0].message}` };
|
|
321
|
+
const config = o.config || resolveAssuranceConfig({ env: process.env });
|
|
322
|
+
for (const f of ['verification-oracles', FEATURE]) {
|
|
323
|
+
const gate = featureStatus(config, f);
|
|
324
|
+
if (gate.status !== 'ok') return { status: 'disabled', legs: [], reasonCodes: ['feature-disabled'], summary: `${f} is not available: ${gate.reason}` };
|
|
325
|
+
}
|
|
326
|
+
const legs = [];
|
|
327
|
+
let blocked = false;
|
|
328
|
+
for (const l of artifact.legs) {
|
|
329
|
+
const run = await runLeg(l.manifest, artifact.fixture.files, l.patched ? artifact.patch.files : null, { config, runOptions: o.runOptions, hostEnvironment: o.hostEnvironment });
|
|
330
|
+
const awaiting = run.status === 'awaiting-prerequisites';
|
|
331
|
+
if (awaiting) blocked = true;
|
|
332
|
+
legs.push({ role: l.role, expected: l.expected, outcome: run.outcome ?? null, status: run.status, matched: run.status === 'completed' && run.outcome === l.expected, recordId: run.record?.id ?? null, receiptDigest: run.receipt?.receiptDigest ?? null, prerequisites: awaiting ? (run.prerequisites || []).map((p) => `${p.kind}:${p.id}:${p.state}`) : [] });
|
|
333
|
+
}
|
|
334
|
+
const failed = legs.filter((l) => !l.matched);
|
|
335
|
+
const status = !failed.length ? 'passed' : blocked ? 'prerequisite-unmet' : 'failed';
|
|
336
|
+
return {
|
|
337
|
+
status, legs, reasonCodes: failed.map((l) => (l.status === 'completed' ? (l.role === 'original-positive' ? 'original-not-reproduced' : l.role === 'patched-negative' ? 'violation-not-eliminated' : l.role.startsWith('preserve:') ? 'invariant-regression' : 'workflow-regression') : 'setup-failed')),
|
|
338
|
+
summary: status === 'passed' ? `all ${legs.length} legs settled as recorded` : `${failed.length} of ${legs.length} legs did not settle as recorded: ${failed.map((l) => `${l.role} (expected ${l.expected}, got ${l.outcome ?? l.status})`).join('; ')}`,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Run an invariant's scenarios and classify what they found (X-403, X-404, X-402).
|
|
2
|
+
//
|
|
3
|
+
// The one orchestration over the pieces: generate the bounded scenarios (`scenarios.js`), replay each through the business-state
|
|
4
|
+
// oracle (`runScenario`, so every run is pinned by a replay manifest, executed only through the trust boundary, and receipted by
|
|
5
|
+
// the verifier domain), then classify each settled run against the signed approval ledger (`lifecycle.js`). A violation of an
|
|
6
|
+
// approved contract is the only thing that may gate; a violation of a proposed one is a candidate, reported separately.
|
|
7
|
+
//
|
|
8
|
+
// With the `invariant-scenarios` feature off, or no fixture, nothing is generated and nothing executes. Every non-ok status is
|
|
9
|
+
// returned with its reason; a scenario that could not be built is listed as unsupported, never counted as checked.
|
|
10
|
+
import { generateScenarios, runScenario, exerciseOf } from './scenarios.js';
|
|
11
|
+
import { classifyViolation, violationReport } from './lifecycle.js';
|
|
12
|
+
import { shrinkScenario } from './shrink.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {object} p
|
|
16
|
+
* @param {object} p.invariant a valid invariant document
|
|
17
|
+
* @param {object} p.fixture { files } of the disposable fixture
|
|
18
|
+
* @param {string} p.commit exact commit id the fixture belongs to
|
|
19
|
+
* @param {object} [p.ledger] the signed approval ledger (without one, every violation is a candidate)
|
|
20
|
+
* @param {object} [p.config] assurance config
|
|
21
|
+
* @param {number} [p.seed]
|
|
22
|
+
* @param {object} [p.bounds]
|
|
23
|
+
* @param {object} [p.signer] ledger signer (test seam)
|
|
24
|
+
* @param {object} [p.runOptions] oracle runner seams (test)
|
|
25
|
+
* @param {object|boolean} [p.shrink] shrink each confirmed violation within this budget (`true` for the default); additive, off by default
|
|
26
|
+
*/
|
|
27
|
+
export async function verifyInvariant(p) {
|
|
28
|
+
const gen = generateScenarios(p.invariant, { fixture: p.fixture, config: p.config, seed: p.seed, bounds: p.bounds });
|
|
29
|
+
if (gen.status !== 'ok') return { status: gen.status, reason: gen.reason, errors: gen.errors, results: [], unsupported: gen.unsupported, report: violationReport([]) };
|
|
30
|
+
const results = [];
|
|
31
|
+
for (const scenario of gen.scenarios) {
|
|
32
|
+
const run = await runScenario(scenario, { fixture: p.fixture, commit: p.commit, config: p.config, runOptions: p.runOptions });
|
|
33
|
+
const classification = classifyViolation({ invariant: p.invariant, ledger: p.ledger, run, signer: p.signer });
|
|
34
|
+
const shrunk = p.shrink && run.outcome === 'confirmed'
|
|
35
|
+
? await shrinkScenario(scenario, { fixture: p.fixture, commit: p.commit, config: p.config, budget: p.shrink === true ? undefined : p.shrink, runOptions: p.runOptions })
|
|
36
|
+
: null;
|
|
37
|
+
results.push({
|
|
38
|
+
scenarioId: scenario.id, kind: scenario.kind, limits: scenario.limits, determinism: scenario.determinism, exercise: exerciseOf(scenario),
|
|
39
|
+
status: run.status, outcome: run.outcome, reason: run.record?.reason ?? run.errors?.[0]?.message ?? null,
|
|
40
|
+
recordId: run.record?.id ?? null, receipt: run.receipt ?? null, manifestId: run.manifestId ?? null, report: run.report ?? null, classification,
|
|
41
|
+
...(shrunk ? { shrink: shrunk } : {}),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return { status: 'ok', results, unsupported: gen.unsupported, bounds: gen.bounds, report: violationReport(results) };
|
|
45
|
+
}
|