@agentguard-run/burn 0.3.2 → 0.3.4
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 +13 -0
- package/README.md +103 -2
- package/dist/src/bench-import.d.ts +74 -0
- package/dist/src/bench-import.js +232 -0
- package/dist/src/bench-panel.d.ts +42 -0
- package/dist/src/bench-panel.js +119 -0
- package/dist/src/cli.js +71 -2
- package/dist/src/frames.d.ts +4 -0
- package/dist/src/frames.js +3 -0
- package/dist/src/guard-candidates-ranker.d.ts +27 -0
- package/dist/src/guard-candidates-ranker.js +134 -0
- package/dist/src/guard-candidates.d.ts +34 -0
- package/dist/src/guard-candidates.js +226 -0
- package/dist/src/insights/history.d.ts +34 -0
- package/dist/src/insights/history.js +122 -0
- package/dist/src/insights/limits.d.ts +50 -0
- package/dist/src/insights/limits.js +116 -0
- package/dist/src/insights/price-shadow.d.ts +84 -0
- package/dist/src/insights/price-shadow.js +240 -0
- package/dist/src/recording.js +4 -1
- package/docs/assets/burn-bench-fixture.evidence.json +18 -0
- package/docs/assets/burn-bench-fixture.gif +0 -0
- package/docs/assets/burn-bench-fixture.mp4 +0 -0
- package/docs/assets/burn-bench-fixture.mp4.provenance.json +70 -0
- package/docs/assets/burn-bench-fixture.png +0 -0
- package/docs/assets/burn-bench-fixture.recording.jsonl +8 -0
- package/fixtures/bench-panel-104x35.txt +35 -0
- package/fixtures/bench-panel-80x24.txt +24 -0
- package/fixtures/bench-run/burn-postrun.json +1 -0
- package/fixtures/bench-run/fixture-note.txt +1 -0
- package/fixtures/bench-run/grader.json +1 -0
- package/fixtures/bench-run/ledger.ndjson +8 -0
- package/fixtures/bench-run/manifest.json +1 -0
- package/fixtures/bench-run/measurement-key.json +1 -0
- package/fixtures/bench-run/patch.diff +0 -0
- package/fixtures/bench-run/policy.json +1 -0
- package/fixtures/bench-run/pricing.json +1 -0
- package/fixtures/bench-run/run.json +1 -0
- package/fixtures/bench-run/sensitivity-policy.json +1 -0
- package/fixtures/guard-candidates-ledger.ndjson +11 -0
- package/package.json +7 -4
- package/vendor/disciplinebench/LICENSE +202 -0
- package/vendor/disciplinebench/PROVENANCE.json +17 -0
- package/vendor/disciplinebench/harness/reducer.cjs +168 -0
- package/vendor/disciplinebench/harness/schema.cjs +14 -0
- package/vendor/disciplinebench/harness/util.cjs +23 -0
- package/vendor/disciplinebench/harness/verify.cjs +111 -0
- package/vendor/disciplinebench/schemas/aggregate.json +51 -0
- package/vendor/disciplinebench/schemas/event.json +62 -0
- package/vendor/disciplinebench/schemas/grader.json +38 -0
- package/vendor/disciplinebench/schemas/run.json +197 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { canonicalJson, digest } = require('./util.cjs');
|
|
4
|
+
const { validate } = require('./schema.cjs');
|
|
5
|
+
const IDENTITY_FIELDS = ['suite_version', 'host', 'host_version', 'requested_model', 'reported_model', 'agentguard_version', 'policy_hash', 'run_window'];
|
|
6
|
+
const CATEGORIES = ['input', 'cache_read', 'cache_write_5m', 'cache_write_1h', 'cache_write_unknown', 'output', 'reasoning_output', 'replay_input', 'context_input'];
|
|
7
|
+
const INPUTS = CATEGORIES.slice(0, 5);
|
|
8
|
+
const TERMINAL = ['completed_pass', 'completed_fail', 'timeout', 'agent_error', 'provider_error', 'invalid_infra', 'harness_error'];
|
|
9
|
+
const unknown = reason => ({ value: null, reason });
|
|
10
|
+
const known = value => ({ value, reason: null });
|
|
11
|
+
function identityKey(identity) {
|
|
12
|
+
if (!identity || IDENTITY_FIELDS.some(key => typeof identity[key] !== 'string' || !identity[key])) throw new Error('Incomplete system identity');
|
|
13
|
+
if (/agentguard/i.test(identity.host) || /agentguard/i.test(identity.requested_model)) throw new Error('AgentGuard is an instrument, not a system');
|
|
14
|
+
return canonicalJson(Object.fromEntries(IDENTITY_FIELDS.map(key => [key, identity[key]])));
|
|
15
|
+
}
|
|
16
|
+
function count(value) { return Number.isFinite(value) && value >= 0 ? value : null; }
|
|
17
|
+
function sumKnown(values) { return values.every(value => value !== null) ? values.reduce((a, b) => a + b, 0) : null; }
|
|
18
|
+
function priceUsage(usage, model, pricing) {
|
|
19
|
+
const rates = pricing.models[model];
|
|
20
|
+
if (!rates) return unknown('model_rate_unavailable');
|
|
21
|
+
const context = count(usage.context_input);
|
|
22
|
+
if (rates.long_context && context === null) return unknown('request_context_unknown');
|
|
23
|
+
const long = rates.long_context && context > rates.long_context.above_input_tokens;
|
|
24
|
+
const rateSet = long ? { ...rates, ...rates.long_context.rates } : rates;
|
|
25
|
+
let dollars = 0;
|
|
26
|
+
for (const category of [...INPUTS, 'output']) {
|
|
27
|
+
const tokens = count(usage[category]);
|
|
28
|
+
if (tokens === null) return unknown(`missing_usage_${category}`);
|
|
29
|
+
if (tokens === 0) continue;
|
|
30
|
+
const rate = category === 'cache_write_unknown'
|
|
31
|
+
? rateSet.cache_write_5m === rateSet.cache_write_1h ? rateSet.cache_write_5m : null
|
|
32
|
+
: rateSet[category];
|
|
33
|
+
if (!Number.isFinite(rate)) return unknown(`unknown_rate_${category}`);
|
|
34
|
+
dollars += tokens * rate / 1000000;
|
|
35
|
+
}
|
|
36
|
+
return known(dollars);
|
|
37
|
+
}
|
|
38
|
+
function reduce(events, grader, pricing, options = {}) {
|
|
39
|
+
if (!Array.isArray(events) || events.length === 0) throw new Error('Empty ledger');
|
|
40
|
+
events.forEach(event => validate('event', event));
|
|
41
|
+
validate('grader', grader);
|
|
42
|
+
const ids = new Set();
|
|
43
|
+
for (const event of events) {
|
|
44
|
+
if (ids.has(event.event_id)) throw new Error('Duplicate event id');
|
|
45
|
+
ids.add(event.event_id);
|
|
46
|
+
if (!Number.isFinite(Date.parse(event.timestamp))) throw new Error('Invalid event timestamp');
|
|
47
|
+
}
|
|
48
|
+
const starts = events.filter(e => e.type === 'session_start');
|
|
49
|
+
const ends = events.filter(e => e.type === 'session_end');
|
|
50
|
+
if (starts.length !== 1 || ends.length !== 1) throw new Error('Exactly one start and terminal event required');
|
|
51
|
+
const start = starts[0], end = ends[0], runId = start.run_id;
|
|
52
|
+
if (events.some(event => event.run_id !== runId)) throw new Error('Mixed runs');
|
|
53
|
+
if (start.parent_agent_id !== null || start.spawn_depth !== 0) throw new Error('Invalid root identity');
|
|
54
|
+
const identity = start.data.identity;
|
|
55
|
+
const systemKey = identityKey(identity);
|
|
56
|
+
const policy = options.policy || start.data.policy;
|
|
57
|
+
if (!policy || digest(policy) !== identity.policy_hash) throw new Error('Pinned policy content is required');
|
|
58
|
+
const policyRules = new Map(policy.rules.map(rule => [rule.id, rule.action]));
|
|
59
|
+
const status = end.data.status;
|
|
60
|
+
if (!TERMINAL.includes(status)) throw new Error('Unknown terminal status');
|
|
61
|
+
if (grader.passed && grader.exit_code !== 0) throw new Error('Passing grader requires zero exit code');
|
|
62
|
+
if (status === 'completed_pass' && !grader.passed || status === 'completed_fail' && grader.passed) throw new Error('Completion contradicts grader');
|
|
63
|
+
if (grader.run_id !== runId) throw new Error('Grader run mismatch');
|
|
64
|
+
const begin = Date.parse(start.timestamp), finish = Date.parse(end.timestamp);
|
|
65
|
+
if (finish < begin || events.some(e => Date.parse(e.timestamp) < begin || Date.parse(e.timestamp) > finish)) throw new Error('Event outside run window');
|
|
66
|
+
const agents = new Map([[start.agent_id, { agent_id: start.agent_id, parent_agent_id: null, spawn_depth: 0 }]]);
|
|
67
|
+
for (const event of events.filter(e => e.type === 'subagent_spawn')) {
|
|
68
|
+
const node = { agent_id: event.agent_id, parent_agent_id: event.parent_agent_id, spawn_depth: event.spawn_depth };
|
|
69
|
+
if (!node.parent_agent_id || node.agent_id === start.agent_id) throw new Error('Invalid child identity');
|
|
70
|
+
if (agents.has(node.agent_id) && canonicalJson(agents.get(node.agent_id)) !== canonicalJson(node)) throw new Error('Conflicting spawn identity');
|
|
71
|
+
agents.set(node.agent_id, node);
|
|
72
|
+
}
|
|
73
|
+
for (const node of agents.values()) {
|
|
74
|
+
if (node.parent_agent_id === null) continue;
|
|
75
|
+
const visited = new Set([node.agent_id]);
|
|
76
|
+
let current = node, depth = 0;
|
|
77
|
+
while (current.parent_agent_id !== null) {
|
|
78
|
+
current = agents.get(current.parent_agent_id);
|
|
79
|
+
if (!current || visited.has(current.agent_id)) throw new Error('Missing parent or cyclic lineage');
|
|
80
|
+
visited.add(current.agent_id); depth++;
|
|
81
|
+
}
|
|
82
|
+
if (depth !== node.spawn_depth) throw new Error('Inconsistent spawn depth');
|
|
83
|
+
}
|
|
84
|
+
const lineageKnown = start.data.coverage?.lineage === true;
|
|
85
|
+
if (lineageKnown && events.some(e => !agents.has(e.agent_id))) throw new Error('Event with undeclared agent');
|
|
86
|
+
for (const event of events) {
|
|
87
|
+
const node = agents.get(event.agent_id);
|
|
88
|
+
if (node && (node.parent_agent_id !== event.parent_agent_id || node.spawn_depth !== event.spawn_depth)) throw new Error('Event lineage contradicts declared agent');
|
|
89
|
+
}
|
|
90
|
+
const messages = new Map();
|
|
91
|
+
for (const event of events.filter(e => e.type === 'model_usage')) {
|
|
92
|
+
const data = event.data;
|
|
93
|
+
if (!data.message_id || !data.host_usage) throw new Error('Usage requires native message id and host usage');
|
|
94
|
+
const record = { ...data, model: data.model || identity.reported_model };
|
|
95
|
+
if (messages.has(data.message_id)) {
|
|
96
|
+
if (canonicalJson(messages.get(data.message_id)) !== canonicalJson(record)) throw new Error('Conflicting repeated usage');
|
|
97
|
+
} else messages.set(data.message_id, record);
|
|
98
|
+
}
|
|
99
|
+
const usageRows = [...messages.values()];
|
|
100
|
+
const categories = Object.fromEntries(CATEGORIES.map(key => [key, usageRows.length ? sumKnown(usageRows.map(row => count(row.host_usage[key]))) : null]));
|
|
101
|
+
function flattenNumbers(value, prefix = '', result = {}) {
|
|
102
|
+
for (const [key, item] of Object.entries(value || {})) {
|
|
103
|
+
const name = prefix ? `${prefix}.${key}` : key;
|
|
104
|
+
if (item && typeof item === 'object' && !Array.isArray(item)) flattenNumbers(item, name, result);
|
|
105
|
+
else if (typeof item === 'number' || item === null) result[name] = count(item);
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
const nativeRows = usageRows.map(row => flattenNumbers(row.native_usage || row.raw || {}));
|
|
110
|
+
const nativeCategories = [...new Set(nativeRows.flatMap(row => Object.keys(row)))].sort();
|
|
111
|
+
const nativeTokens = Object.fromEntries(nativeCategories.map(key => [key, sumKnown(nativeRows.map(row => count(row[key])))]));
|
|
112
|
+
const totalInput = sumKnown(INPUTS.map(key => categories[key]));
|
|
113
|
+
const replay = totalInput === null || categories.replay_input === null ? unknown('input_or_replay_unknown')
|
|
114
|
+
: totalInput === 0 ? unknown('zero_input_tokens') : known(categories.replay_input / totalInput);
|
|
115
|
+
if (replay.value !== null && replay.value > 1) throw new Error('Replay exceeds input');
|
|
116
|
+
const charges = usageRows.map(row => priceUsage(row.host_usage, row.model, pricing));
|
|
117
|
+
const exposure = !charges.length ? unknown('usage_not_exposed') : charges.some(c => c.value === null)
|
|
118
|
+
? unknown(charges.find(c => c.value === null).reason) : known(charges.reduce((sum, c) => sum + c.value, 0));
|
|
119
|
+
const reconciliation = usageRows.map(row => ({ message_id: row.message_id,
|
|
120
|
+
host_usage: row.host_usage, agentguard_usage: row.agentguard_usage ?? null,
|
|
121
|
+
delta: row.agentguard_usage ? Object.fromEntries(CATEGORIES.map(key => [key,
|
|
122
|
+
count(row.host_usage[key]) === null || count(row.agentguard_usage[key]) === null ? null : row.agentguard_usage[key] - row.host_usage[key]])) : null,
|
|
123
|
+
reason: row.agentguard_usage ? null : 'instrument_usage_not_exposed', raw: row.raw || {}, native_usage: row.native_usage || null }));
|
|
124
|
+
const decisions = new Map(), requests = new Map();
|
|
125
|
+
for (const event of events.filter(e => e.type === 'tool_request')) {
|
|
126
|
+
if (!event.data.tool_call_id) throw new Error('Tool request has no identity');
|
|
127
|
+
if (requests.has(event.data.tool_call_id)) throw new Error('Duplicate tool request');
|
|
128
|
+
requests.set(event.data.tool_call_id, event.data);
|
|
129
|
+
}
|
|
130
|
+
for (const event of events.filter(e => e.type === 'policy_decision')) {
|
|
131
|
+
const data = event.data;
|
|
132
|
+
if (data.enforced !== false || data.policy_hash !== identity.policy_hash) throw new Error('Policy mismatch or enforced benchmark decision');
|
|
133
|
+
if (!Array.isArray(data.rule_ids) || !['CLEAN', 'WARN', 'STOP'].includes(data.classification)) throw new Error('Invalid policy decision');
|
|
134
|
+
if (data.rule_ids.some(id => !policyRules.has(id))) throw new Error('Unknown policy rule id');
|
|
135
|
+
const actions = data.rule_ids.map(id => policyRules.get(id));
|
|
136
|
+
const expected = actions.includes('stop') ? 'STOP' : actions.includes('warn') ? 'WARN' : 'CLEAN';
|
|
137
|
+
if (data.classification !== expected && !data.uncertainty) throw new Error('Classification contradicts pinned policy');
|
|
138
|
+
if (decisions.has(data.tool_call_id)) throw new Error('Duplicate policy decision');
|
|
139
|
+
if (!requests.has(data.tool_call_id)) throw new Error('Decision without tool request');
|
|
140
|
+
decisions.set(data.tool_call_id, data);
|
|
141
|
+
}
|
|
142
|
+
const policyComplete = start.data.coverage?.policy !== false && requests.size === decisions.size && [...decisions.values()].every(d => !d.uncertainty);
|
|
143
|
+
const byRule = {};
|
|
144
|
+
for (const decision of decisions.values()) for (const id of new Set(decision.rule_ids)) byRule[id] = (byRule[id] || 0) + 1;
|
|
145
|
+
const highIds = new Set((options.highConfidencePolicy?.rules || []).filter(rule => rule.action === 'stop').map(rule => rule.id));
|
|
146
|
+
const ordered = [...decisions.values()].map(data => ({ ...data, tool_name: requests.get(data.tool_call_id).tool_name }));
|
|
147
|
+
const times = [...new Set(events.map(e => Date.parse(e.timestamp)))].sort((a, b) => a - b);
|
|
148
|
+
const active = times.slice(1).reduce((sum, value, i) => sum + Math.min(value - times[i], 60000), 0) / 60000;
|
|
149
|
+
const valid = !['invalid_infra', 'harness_error'].includes(status);
|
|
150
|
+
return {
|
|
151
|
+
schema_version: 1, run_id: runId, identity, system_key: systemKey, task_id: start.data.task_id,
|
|
152
|
+
replicate: start.data.replicate, fixture: start.data.fixture === true, status, valid,
|
|
153
|
+
completion: status === 'completed_pass' && grader.passed ? 1 : 0,
|
|
154
|
+
metrics: { spawn_count: lineageKnown ? known(agents.size - 1) : unknown('lineage_not_exposed'),
|
|
155
|
+
max_spawn_depth: lineageKnown ? known(Math.max(...[...agents.values()].map(a => a.spawn_depth))) : unknown('lineage_not_exposed'),
|
|
156
|
+
tokens: categories, native_tokens: nativeTokens, context_replay_share: replay, dangerous_command_matches: byRule,
|
|
157
|
+
would_stop: policyComplete ? known(ordered.filter(d => d.classification === 'STOP').length) : unknown('policy_coverage_incomplete'),
|
|
158
|
+
high_confidence_would_stop: !options.highConfidencePolicy ? unknown('sensitivity_policy_missing')
|
|
159
|
+
: policyComplete ? known(ordered.filter(d => d.rule_ids.some(id => highIds.has(id))).length) : unknown('policy_coverage_incomplete'),
|
|
160
|
+
active_minutes: active, wall_seconds: (finish - begin) / 1000, list_price_exposure: exposure },
|
|
161
|
+
coverage: { lineage: lineageKnown, usage: usageRows.length > 0, policy: policyComplete },
|
|
162
|
+
spawn_tree: [...agents.values()], policy_decisions: ordered, usage_reconciliation: reconciliation,
|
|
163
|
+
evidence: { events_sha256: digest(events), grader_sha256: digest(grader), pricing_sha256: digest(pricing),
|
|
164
|
+
sensitivity_sha256: options.highConfidencePolicy ? digest(options.highConfidencePolicy) : null },
|
|
165
|
+
grader
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
module.exports = { reduce, priceUsage, identityKey, IDENTITY_FIELDS, CATEGORIES, unknown, known };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const Ajv = require('ajv');
|
|
5
|
+
const { readJson } = require('./util.cjs');
|
|
6
|
+
const ajv = new Ajv({ allErrors: true, strict: false });
|
|
7
|
+
const validators = new Map();
|
|
8
|
+
function validate(kind, value) {
|
|
9
|
+
if (!validators.has(kind)) validators.set(kind, ajv.compile(readJson(path.join(__dirname, '..', 'schemas', `${kind}.json`))));
|
|
10
|
+
const check = validators.get(kind);
|
|
11
|
+
if (!check(value)) throw new Error(`${kind}: ${ajv.errorsText(check.errors)}`);
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
module.exports = { validate };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('node:crypto');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { canonicalJson } = require('@agentguard-run/spend/decision-log');
|
|
7
|
+
|
|
8
|
+
const sha256 = value => crypto.createHash('sha256').update(value).digest('hex');
|
|
9
|
+
const digest = value => sha256(canonicalJson(value));
|
|
10
|
+
const readJson = file => JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
11
|
+
function writeJson(file, value) {
|
|
12
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
13
|
+
fs.writeFileSync(file, canonicalJson(value) + '\n');
|
|
14
|
+
}
|
|
15
|
+
function safePath(root, relative) {
|
|
16
|
+
if (typeof relative !== 'string' || path.isAbsolute(relative) || relative.split(/[\\/]/).includes('..')) throw new Error('Unsafe artifact path');
|
|
17
|
+
const candidate = path.resolve(root, relative);
|
|
18
|
+
if (!candidate.startsWith(path.resolve(root) + path.sep)) throw new Error('Artifact outside bundle');
|
|
19
|
+
const real = fs.realpathSync(candidate);
|
|
20
|
+
if (!real.startsWith(fs.realpathSync(root) + path.sep)) throw new Error('Artifact symlink outside bundle');
|
|
21
|
+
return candidate;
|
|
22
|
+
}
|
|
23
|
+
module.exports = { canonicalJson, sha256, digest, readJson, writeJson, safePath };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('node:crypto');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const sdk = require('@agentguard-run/spend/decision-log');
|
|
7
|
+
const { canonicalJson, digest, sha256, readJson, writeJson, safePath } = require('./util.cjs');
|
|
8
|
+
const { reduce } = require('./reducer.cjs');
|
|
9
|
+
|
|
10
|
+
function createKey() {
|
|
11
|
+
const { privateKey, publicKey } = crypto.generateKeyPairSync('ed25519');
|
|
12
|
+
const seed = privateKey.export({ format: 'der', type: 'pkcs8' }).subarray(-32);
|
|
13
|
+
const raw = publicKey.export({ format: 'der', type: 'spki' }).subarray(-32);
|
|
14
|
+
return { privateKey, publicKey, seed, raw, id: sdk.computeSignerFingerprint(raw) };
|
|
15
|
+
}
|
|
16
|
+
function publicObject(hex) {
|
|
17
|
+
if (!/^[0-9a-f]{64}$/.test(hex)) throw new Error('Invalid public key');
|
|
18
|
+
return crypto.createPublicKey({ key: Buffer.concat([Buffer.from('302a300506032b6570032100', 'hex'), Buffer.from(hex, 'hex')]), format: 'der', type: 'spki' });
|
|
19
|
+
}
|
|
20
|
+
async function signEvents(events, key) {
|
|
21
|
+
const entries = []; let previousHash = sdk.GENESIS_PREVIOUS_HASH;
|
|
22
|
+
for (const event of events) {
|
|
23
|
+
const identity = events[0].data.identity;
|
|
24
|
+
const decision = { decisionId: event.event_id, timestamp: event.timestamp, action: 'allow', triggeredCap: null,
|
|
25
|
+
triggeredScopeKey: null, projectedCents: 0, windowSpendBefore: 0, windowSpendAfter: 0,
|
|
26
|
+
provider: 'other', modelRequested: identity.requested_model, modelResolved: identity.reported_model,
|
|
27
|
+
policyId: identity.policy_hash, policyVersion: 1, enforcementMode: 'shadow', reasons: ['benchmark_telemetry_only'],
|
|
28
|
+
entryType: 'governance', governanceReceipt: event };
|
|
29
|
+
const entry = await sdk.signDecision({ sequence: entries.length, decision, previousHash, privateKey: key.seed, publicKey: key.raw });
|
|
30
|
+
entries.push(entry); previousHash = entry.entryHash;
|
|
31
|
+
}
|
|
32
|
+
return entries;
|
|
33
|
+
}
|
|
34
|
+
function signManifest(manifest, publisher) {
|
|
35
|
+
if (manifest.measurement_key_id === publisher.id) throw new Error('Publisher and measurement keys must differ');
|
|
36
|
+
const { signature: previousSignature, ...unsigned } = manifest;
|
|
37
|
+
const payload = { ...unsigned, publisher_key_id: publisher.id, publisher_public_key: publisher.raw.toString('hex') };
|
|
38
|
+
return { ...payload, signature: crypto.sign(null, Buffer.from(canonicalJson(payload)), publisher.privateKey).toString('hex') };
|
|
39
|
+
}
|
|
40
|
+
function verifyManifest(manifest) {
|
|
41
|
+
const { signature, ...payload } = manifest;
|
|
42
|
+
if (!/^[a-f0-9]{128}$/.test(signature || '')) throw new Error('Missing publisher signature');
|
|
43
|
+
const fingerprint = sdk.computeSignerFingerprint(Buffer.from(payload.publisher_public_key, 'hex'));
|
|
44
|
+
if (fingerprint !== payload.publisher_key_id || payload.publisher_key_id === payload.measurement_key_id) throw new Error('Publisher identity mismatch');
|
|
45
|
+
if (!crypto.verify(null, Buffer.from(canonicalJson(payload)), publicObject(payload.publisher_public_key), Buffer.from(signature, 'hex'))) throw new Error('Manifest signature invalid');
|
|
46
|
+
return payload;
|
|
47
|
+
}
|
|
48
|
+
async function writeBundle(directory, { events, grader, pricing, policy, highConfidencePolicy, measurement, publisher, patch = '', extraArtifacts = {}, roundId = 'fixture-smoke-v1' }) {
|
|
49
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
50
|
+
const entries = await signEvents(events, measurement);
|
|
51
|
+
fs.writeFileSync(path.join(directory, 'ledger.ndjson'), entries.map(canonicalJson).join('\n') + '\n');
|
|
52
|
+
writeJson(path.join(directory, 'measurement-key.json'), { key_id: measurement.id, public_key: measurement.raw.toString('hex') });
|
|
53
|
+
writeJson(path.join(directory, 'grader.json'), grader);
|
|
54
|
+
writeJson(path.join(directory, 'pricing.json'), pricing);
|
|
55
|
+
writeJson(path.join(directory, 'policy.json'), policy);
|
|
56
|
+
writeJson(path.join(directory, 'sensitivity-policy.json'), highConfidencePolicy);
|
|
57
|
+
fs.writeFileSync(path.join(directory, 'patch.diff'), patch);
|
|
58
|
+
const run = reduce(events, grader, pricing, { highConfidencePolicy, policy });
|
|
59
|
+
if (digest(policy) !== run.identity.policy_hash) throw new Error('Policy content differs from run identity');
|
|
60
|
+
writeJson(path.join(directory, 'run.json'), run);
|
|
61
|
+
const burn = { status: 'unsupported_signed_benchmark_ledger', scoring_dependency: false,
|
|
62
|
+
reason: 'The installed Burn CLI prices native transcripts and cannot ingest this signed ledger. No unsupported flags were invoked.',
|
|
63
|
+
supported_interfaces: ['agentguard-burn pricing', 'agentguard-burn why TRANSCRIPT json', 'agentguard-burn render FRAMES --mp4 OUTPUT'] };
|
|
64
|
+
writeJson(path.join(directory, 'burn-postrun.json'), burn);
|
|
65
|
+
for (const [name, value] of Object.entries(extraArtifacts)) {
|
|
66
|
+
if (name.includes('/') || name.includes('..') || fs.existsSync(path.join(directory, name))) throw new Error('Invalid extra artifact name');
|
|
67
|
+
fs.writeFileSync(path.join(directory, name), typeof value === 'string' ? value : canonicalJson(value) + '\n');
|
|
68
|
+
}
|
|
69
|
+
const files = fs.readdirSync(directory).filter(file => file !== 'manifest.json').sort();
|
|
70
|
+
const artifacts = Object.fromEntries(files.map(file => [file, sha256(fs.readFileSync(path.join(directory, file)))]));
|
|
71
|
+
const manifest = signManifest({ schema_version: 1, round_id: roundId, run_id: run.run_id,
|
|
72
|
+
fixture: run.fixture, measurement_key_id: measurement.id, artifacts,
|
|
73
|
+
trust_note: 'Embedded keys prove integrity, not an external publisher identity. Pin the key id through a trusted channel.' }, publisher);
|
|
74
|
+
writeJson(path.join(directory, 'manifest.json'), manifest);
|
|
75
|
+
return { run, manifest };
|
|
76
|
+
}
|
|
77
|
+
async function verifyBundle(directory, { expectedPublisherKeyId } = {}) {
|
|
78
|
+
const manifest = readJson(path.join(directory, 'manifest.json'));
|
|
79
|
+
verifyManifest(manifest);
|
|
80
|
+
if (expectedPublisherKeyId && manifest.publisher_key_id !== expectedPublisherKeyId) throw new Error('Untrusted publisher key');
|
|
81
|
+
for (const [name, hash] of Object.entries(manifest.artifacts)) if (sha256(fs.readFileSync(safePath(directory, name))) !== hash) throw new Error(`Artifact hash mismatch: ${name}`);
|
|
82
|
+
const required = ['ledger.ndjson', 'measurement-key.json', 'grader.json', 'pricing.json', 'policy.json', 'sensitivity-policy.json', 'run.json', 'patch.diff', 'burn-postrun.json'];
|
|
83
|
+
if (required.some(name => !manifest.artifacts[name])) throw new Error('Manifest omits required artifact');
|
|
84
|
+
const key = readJson(path.join(directory, 'measurement-key.json'));
|
|
85
|
+
if (key.key_id !== manifest.measurement_key_id || sdk.computeSignerFingerprint(Buffer.from(key.public_key, 'hex')) !== key.key_id) throw new Error('Measurement key mismatch');
|
|
86
|
+
publicObject(key.public_key);
|
|
87
|
+
const entries = fs.readFileSync(path.join(directory, 'ledger.ndjson'), 'utf8').trim().split('\n').map(JSON.parse);
|
|
88
|
+
const chain = await sdk.verifyChain(entries, Buffer.from(key.public_key, 'hex'));
|
|
89
|
+
if (!chain.ok) throw new Error(`Ledger verification failed at ${chain.sequence}: ${chain.reason}`);
|
|
90
|
+
const events = entries.map(entry => entry.decision.governanceReceipt);
|
|
91
|
+
const policy = readJson(path.join(directory, 'policy.json'));
|
|
92
|
+
const run = reduce(events, readJson(path.join(directory, 'grader.json')), readJson(path.join(directory, 'pricing.json')),
|
|
93
|
+
{ highConfidencePolicy: readJson(path.join(directory, 'sensitivity-policy.json')), policy });
|
|
94
|
+
if (digest(policy) !== run.identity.policy_hash) throw new Error('Policy hash mismatch');
|
|
95
|
+
if (canonicalJson(run) + '\n' !== fs.readFileSync(path.join(directory, 'run.json'), 'utf8')) throw new Error('Recomputed run.json mismatch');
|
|
96
|
+
if (run.run_id !== manifest.run_id || run.fixture !== manifest.fixture) throw new Error('Run identity mismatch');
|
|
97
|
+
return { ok: true, run_id: run.run_id, ledger_sha256: manifest.artifacts['ledger.ndjson'],
|
|
98
|
+
run_sha256: manifest.artifacts['run.json'], publisher_key_id: manifest.publisher_key_id,
|
|
99
|
+
measurement_key_id: manifest.measurement_key_id, publisher_pinned: Boolean(expectedPublisherKeyId) };
|
|
100
|
+
}
|
|
101
|
+
function lockRound(round, files, publisher) {
|
|
102
|
+
return signManifest({ ...round, locked_files: Object.fromEntries(Object.entries(files).map(([name, bytes]) => [name, sha256(bytes)])) }, publisher);
|
|
103
|
+
}
|
|
104
|
+
function checkRoundLock(previous, current, root) {
|
|
105
|
+
verifyManifest(previous); verifyManifest(current);
|
|
106
|
+
if (previous.round_id !== current.round_id) return true;
|
|
107
|
+
if (canonicalJson(previous) !== canonicalJson(current)) throw new Error('Signed round changed without a new round id');
|
|
108
|
+
for (const [name, hash] of Object.entries(current.locked_files || {})) if (sha256(fs.readFileSync(safePath(root, name))) !== hash) throw new Error(`Locked round input changed: ${name}`);
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
module.exports = { createKey, signEvents, signManifest, verifyManifest, writeBundle, verifyBundle, lockRound, checkRoundLock };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"required": [
|
|
4
|
+
"schema_version",
|
|
5
|
+
"fixture",
|
|
6
|
+
"disclosure",
|
|
7
|
+
"systems",
|
|
8
|
+
"paired_differences",
|
|
9
|
+
"round_id"
|
|
10
|
+
],
|
|
11
|
+
"properties": {
|
|
12
|
+
"schema_version": {
|
|
13
|
+
"const": 1
|
|
14
|
+
},
|
|
15
|
+
"round_id": {
|
|
16
|
+
"type": "string",
|
|
17
|
+
"minLength": 1
|
|
18
|
+
},
|
|
19
|
+
"fixture": {
|
|
20
|
+
"type": "boolean"
|
|
21
|
+
},
|
|
22
|
+
"disclosure": {
|
|
23
|
+
"type": "object",
|
|
24
|
+
"required": [
|
|
25
|
+
"publisher",
|
|
26
|
+
"instrument_vendor",
|
|
27
|
+
"independent_rerun",
|
|
28
|
+
"reviewers"
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
"systems": {
|
|
32
|
+
"type": "array",
|
|
33
|
+
"items": {
|
|
34
|
+
"type": "object",
|
|
35
|
+
"required": [
|
|
36
|
+
"identity",
|
|
37
|
+
"run_ids",
|
|
38
|
+
"counts",
|
|
39
|
+
"headline",
|
|
40
|
+
"diagnostics",
|
|
41
|
+
"runs"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"paired_differences": {
|
|
46
|
+
"type": "array"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"additionalProperties": false,
|
|
50
|
+
"$schema": "http://json-schema.org/draft-07/schema#"
|
|
51
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"required": [
|
|
4
|
+
"schema_version",
|
|
5
|
+
"event_id",
|
|
6
|
+
"type",
|
|
7
|
+
"timestamp",
|
|
8
|
+
"run_id",
|
|
9
|
+
"agent_id",
|
|
10
|
+
"parent_agent_id",
|
|
11
|
+
"spawn_depth",
|
|
12
|
+
"data"
|
|
13
|
+
],
|
|
14
|
+
"additionalProperties": false,
|
|
15
|
+
"properties": {
|
|
16
|
+
"schema_version": {
|
|
17
|
+
"const": 1
|
|
18
|
+
},
|
|
19
|
+
"event_id": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"minLength": 1
|
|
22
|
+
},
|
|
23
|
+
"type": {
|
|
24
|
+
"enum": [
|
|
25
|
+
"session_start",
|
|
26
|
+
"model_usage",
|
|
27
|
+
"tool_request",
|
|
28
|
+
"policy_decision",
|
|
29
|
+
"tool_result",
|
|
30
|
+
"subagent_spawn",
|
|
31
|
+
"subagent_end",
|
|
32
|
+
"session_end"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
"timestamp": {
|
|
36
|
+
"type": "string",
|
|
37
|
+
"pattern": "^\\d{4}-\\d{2}-\\d{2}T.*Z$"
|
|
38
|
+
},
|
|
39
|
+
"run_id": {
|
|
40
|
+
"type": "string",
|
|
41
|
+
"minLength": 1
|
|
42
|
+
},
|
|
43
|
+
"agent_id": {
|
|
44
|
+
"type": "string",
|
|
45
|
+
"minLength": 1
|
|
46
|
+
},
|
|
47
|
+
"parent_agent_id": {
|
|
48
|
+
"type": [
|
|
49
|
+
"string",
|
|
50
|
+
"null"
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
"spawn_depth": {
|
|
54
|
+
"type": "integer",
|
|
55
|
+
"minimum": 0
|
|
56
|
+
},
|
|
57
|
+
"data": {
|
|
58
|
+
"type": "object"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"$schema": "http://json-schema.org/draft-07/schema#"
|
|
62
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"required": [
|
|
4
|
+
"run_id",
|
|
5
|
+
"passed",
|
|
6
|
+
"exit_code",
|
|
7
|
+
"checks"
|
|
8
|
+
],
|
|
9
|
+
"properties": {
|
|
10
|
+
"run_id": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"minLength": 1
|
|
13
|
+
},
|
|
14
|
+
"passed": {
|
|
15
|
+
"type": "boolean"
|
|
16
|
+
},
|
|
17
|
+
"exit_code": {
|
|
18
|
+
"type": [
|
|
19
|
+
"integer",
|
|
20
|
+
"null"
|
|
21
|
+
]
|
|
22
|
+
},
|
|
23
|
+
"checks": {
|
|
24
|
+
"type": "array"
|
|
25
|
+
},
|
|
26
|
+
"reason": {
|
|
27
|
+
"type": [
|
|
28
|
+
"string",
|
|
29
|
+
"null"
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
"environment": {
|
|
33
|
+
"type": "string"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"additionalProperties": false,
|
|
37
|
+
"$schema": "http://json-schema.org/draft-07/schema#"
|
|
38
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"required": [
|
|
4
|
+
"schema_version",
|
|
5
|
+
"run_id",
|
|
6
|
+
"identity",
|
|
7
|
+
"system_key",
|
|
8
|
+
"task_id",
|
|
9
|
+
"replicate",
|
|
10
|
+
"fixture",
|
|
11
|
+
"status",
|
|
12
|
+
"valid",
|
|
13
|
+
"completion",
|
|
14
|
+
"metrics",
|
|
15
|
+
"coverage",
|
|
16
|
+
"spawn_tree",
|
|
17
|
+
"policy_decisions",
|
|
18
|
+
"usage_reconciliation",
|
|
19
|
+
"evidence",
|
|
20
|
+
"grader"
|
|
21
|
+
],
|
|
22
|
+
"properties": {
|
|
23
|
+
"schema_version": {
|
|
24
|
+
"const": 1
|
|
25
|
+
},
|
|
26
|
+
"run_id": {
|
|
27
|
+
"type": "string",
|
|
28
|
+
"minLength": 1
|
|
29
|
+
},
|
|
30
|
+
"identity": {
|
|
31
|
+
"type": "object",
|
|
32
|
+
"required": [
|
|
33
|
+
"suite_version",
|
|
34
|
+
"host",
|
|
35
|
+
"host_version",
|
|
36
|
+
"requested_model",
|
|
37
|
+
"reported_model",
|
|
38
|
+
"agentguard_version",
|
|
39
|
+
"policy_hash",
|
|
40
|
+
"run_window"
|
|
41
|
+
],
|
|
42
|
+
"properties": {
|
|
43
|
+
"suite_version": {
|
|
44
|
+
"type": "string",
|
|
45
|
+
"minLength": 1
|
|
46
|
+
},
|
|
47
|
+
"host": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"minLength": 1
|
|
50
|
+
},
|
|
51
|
+
"host_version": {
|
|
52
|
+
"type": "string",
|
|
53
|
+
"minLength": 1
|
|
54
|
+
},
|
|
55
|
+
"requested_model": {
|
|
56
|
+
"type": "string",
|
|
57
|
+
"minLength": 1
|
|
58
|
+
},
|
|
59
|
+
"reported_model": {
|
|
60
|
+
"type": "string",
|
|
61
|
+
"minLength": 1
|
|
62
|
+
},
|
|
63
|
+
"agentguard_version": {
|
|
64
|
+
"type": "string",
|
|
65
|
+
"minLength": 1
|
|
66
|
+
},
|
|
67
|
+
"policy_hash": {
|
|
68
|
+
"type": "string",
|
|
69
|
+
"minLength": 1
|
|
70
|
+
},
|
|
71
|
+
"run_window": {
|
|
72
|
+
"type": "string",
|
|
73
|
+
"minLength": 1
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"additionalProperties": false
|
|
77
|
+
},
|
|
78
|
+
"system_key": {
|
|
79
|
+
"type": "string",
|
|
80
|
+
"minLength": 1
|
|
81
|
+
},
|
|
82
|
+
"task_id": {
|
|
83
|
+
"type": "string",
|
|
84
|
+
"minLength": 1
|
|
85
|
+
},
|
|
86
|
+
"replicate": {
|
|
87
|
+
"type": "integer",
|
|
88
|
+
"minimum": 0
|
|
89
|
+
},
|
|
90
|
+
"fixture": {
|
|
91
|
+
"type": "boolean"
|
|
92
|
+
},
|
|
93
|
+
"status": {
|
|
94
|
+
"enum": [
|
|
95
|
+
"completed_pass",
|
|
96
|
+
"completed_fail",
|
|
97
|
+
"timeout",
|
|
98
|
+
"agent_error",
|
|
99
|
+
"provider_error",
|
|
100
|
+
"invalid_infra",
|
|
101
|
+
"harness_error"
|
|
102
|
+
]
|
|
103
|
+
},
|
|
104
|
+
"valid": {
|
|
105
|
+
"type": "boolean"
|
|
106
|
+
},
|
|
107
|
+
"completion": {
|
|
108
|
+
"enum": [
|
|
109
|
+
0,
|
|
110
|
+
1
|
|
111
|
+
]
|
|
112
|
+
},
|
|
113
|
+
"metrics": {
|
|
114
|
+
"type": "object",
|
|
115
|
+
"required": [
|
|
116
|
+
"spawn_count",
|
|
117
|
+
"max_spawn_depth",
|
|
118
|
+
"tokens",
|
|
119
|
+
"context_replay_share",
|
|
120
|
+
"dangerous_command_matches",
|
|
121
|
+
"would_stop",
|
|
122
|
+
"high_confidence_would_stop",
|
|
123
|
+
"active_minutes",
|
|
124
|
+
"wall_seconds",
|
|
125
|
+
"list_price_exposure"
|
|
126
|
+
]
|
|
127
|
+
},
|
|
128
|
+
"coverage": {
|
|
129
|
+
"type": "object"
|
|
130
|
+
},
|
|
131
|
+
"spawn_tree": {
|
|
132
|
+
"type": "array"
|
|
133
|
+
},
|
|
134
|
+
"policy_decisions": {
|
|
135
|
+
"type": "array"
|
|
136
|
+
},
|
|
137
|
+
"usage_reconciliation": {
|
|
138
|
+
"type": "array"
|
|
139
|
+
},
|
|
140
|
+
"evidence": {
|
|
141
|
+
"type": "object"
|
|
142
|
+
},
|
|
143
|
+
"grader": {
|
|
144
|
+
"type": "object",
|
|
145
|
+
"required": [
|
|
146
|
+
"run_id",
|
|
147
|
+
"passed",
|
|
148
|
+
"exit_code",
|
|
149
|
+
"checks"
|
|
150
|
+
],
|
|
151
|
+
"properties": {
|
|
152
|
+
"run_id": {
|
|
153
|
+
"type": "string",
|
|
154
|
+
"minLength": 1
|
|
155
|
+
},
|
|
156
|
+
"passed": {
|
|
157
|
+
"type": "boolean"
|
|
158
|
+
},
|
|
159
|
+
"exit_code": {
|
|
160
|
+
"type": [
|
|
161
|
+
"integer",
|
|
162
|
+
"null"
|
|
163
|
+
]
|
|
164
|
+
},
|
|
165
|
+
"checks": {
|
|
166
|
+
"type": "array"
|
|
167
|
+
},
|
|
168
|
+
"reason": {
|
|
169
|
+
"type": [
|
|
170
|
+
"string",
|
|
171
|
+
"null"
|
|
172
|
+
]
|
|
173
|
+
},
|
|
174
|
+
"environment": {
|
|
175
|
+
"type": "string"
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
"additionalProperties": false,
|
|
179
|
+
"$schema": "http://json-schema.org/draft-07/schema#"
|
|
180
|
+
},
|
|
181
|
+
"bundle": {
|
|
182
|
+
"type": "object",
|
|
183
|
+
"required": [
|
|
184
|
+
"ledger_sha256"
|
|
185
|
+
],
|
|
186
|
+
"properties": {
|
|
187
|
+
"ledger_sha256": {
|
|
188
|
+
"type": "string",
|
|
189
|
+
"pattern": "^[0-9a-f]{64}$"
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
"additionalProperties": false
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
"additionalProperties": false,
|
|
196
|
+
"$schema": "http://json-schema.org/draft-07/schema#"
|
|
197
|
+
}
|