@olegkoval/agent-skills 1.80.0 → 1.82.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/adapters/claude/olko-github-pr/skills/lekker-review/scripts/codex-usage.mjs +32 -11
- package/adapters/claude/olko-github-pr/skills/lekker-review/scripts/defect-policy.mjs +58 -0
- package/adapters/claude/olko-github-pr/skills/lekker-review/scripts/native-results.mjs +49 -0
- package/package.json +1 -1
- package/plugins/olko-apple-kit/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-creative/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-garmin-kit/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-git-tools/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-github-pr/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-github-pr/skills/lekker-review/scripts/codex-usage.mjs +32 -11
- package/plugins/olko-github-pr/skills/lekker-review/scripts/defect-policy.mjs +58 -0
- package/plugins/olko-github-pr/skills/lekker-review/scripts/native-results.mjs +49 -0
- package/plugins/olko-obsidian/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-product/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-reflection/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-release/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-skill-meta/.claude-plugin/plugin.json +1 -1
- package/plugins/olko-web-ops/.claude-plugin/plugin.json +1 -1
|
@@ -7,14 +7,17 @@ const FIELDS = ['input_tokens', 'cached_input_tokens', 'cache_write_input_tokens
|
|
|
7
7
|
const record = v => v && typeof v === 'object' && !Array.isArray(v);
|
|
8
8
|
const unavailable = (agentPath, reason) => ({ agentPath, status: 'unavailable', reason });
|
|
9
9
|
|
|
10
|
-
async function inspectSession(sessionFile, parentThreadId, agentPath) {
|
|
11
|
-
let sessionId = null, usage = null, complete = false, models = [];
|
|
10
|
+
async function inspectSession(sessionFile, parentThreadId, agentPath, requireUsage) {
|
|
11
|
+
let sessionId = null, usage = null, complete = false, models = [], terminalReason = null, turnId = null;
|
|
12
12
|
try {
|
|
13
13
|
const input = createReadStream(sessionFile);
|
|
14
14
|
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
|
|
15
15
|
if (!line.trim()) continue;
|
|
16
16
|
let row;
|
|
17
17
|
try { row = JSON.parse(line); } catch { return unavailable(agentPath, 'malformed JSONL'); }
|
|
18
|
+
if (!requireUsage && !record(row)) return unavailable(agentPath, 'malformed JSONL');
|
|
19
|
+
if (!requireUsage && row.type === 'event_msg' && ['task_complete', 'turn_aborted'].includes(row.payload?.type)
|
|
20
|
+
&& (!turnId || row.payload.turn_id !== turnId)) return unavailable(agentPath, 'task turn identity mismatch');
|
|
18
21
|
if (row.type === 'session_meta') {
|
|
19
22
|
const spawn = row.payload?.source?.subagent?.thread_spawn;
|
|
20
23
|
if (sessionId || typeof row.payload?.id !== 'string' || !row.payload.id) return unavailable(agentPath, 'session metadata missing or duplicated');
|
|
@@ -22,9 +25,16 @@ async function inspectSession(sessionFile, parentThreadId, agentPath) {
|
|
|
22
25
|
sessionId = row.payload.id;
|
|
23
26
|
} else if (row.type === 'turn_context' && typeof row.payload?.model === 'string' && row.payload.model && !models.includes(row.payload.model)) {
|
|
24
27
|
models = [...models, row.payload.model];
|
|
25
|
-
} else if (row.type === 'event_msg' && row.payload?.type === 'task_started')
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
} else if (row.type === 'event_msg' && row.payload?.type === 'task_started') {
|
|
29
|
+
if (!requireUsage && (typeof row.payload.turn_id !== 'string' || !row.payload.turn_id.trim())) return unavailable(agentPath, 'task turn identity unavailable');
|
|
30
|
+
if (!requireUsage) turnId = row.payload.turn_id;
|
|
31
|
+
complete = false;
|
|
32
|
+
terminalReason = null;
|
|
33
|
+
} else if (!requireUsage && row.type === 'event_msg' && row.payload?.type === 'turn_aborted') {
|
|
34
|
+
complete = false;
|
|
35
|
+
terminalReason = 'task aborted';
|
|
36
|
+
} else if (row.type === 'event_msg' && row.payload?.type === 'task_complete') complete = terminalReason === null;
|
|
37
|
+
else if (requireUsage && row.type === 'event_msg' && row.payload?.type === 'token_count') {
|
|
28
38
|
const raw = row.payload.info?.total_token_usage;
|
|
29
39
|
if (!record(raw) || FIELDS.some(k => !Number.isSafeInteger(raw[k]) || raw[k] < 0)) return unavailable(agentPath, 'token counters are malformed');
|
|
30
40
|
const next = Object.fromEntries(FIELDS.map(k => [k, raw[k]]));
|
|
@@ -34,15 +44,15 @@ async function inspectSession(sessionFile, parentThreadId, agentPath) {
|
|
|
34
44
|
}
|
|
35
45
|
} catch { return unavailable(agentPath, 'session file could not be read'); }
|
|
36
46
|
if (!sessionId) return unavailable(agentPath, 'session metadata unavailable');
|
|
37
|
-
if (!complete) return unavailable(agentPath, 'task completion unavailable');
|
|
38
|
-
if (!usage) return unavailable(agentPath, 'token counters unavailable');
|
|
39
|
-
return { agentPath, status: 'available', sessionId, models, usage };
|
|
47
|
+
if (!complete) return unavailable(agentPath, terminalReason || 'task completion unavailable');
|
|
48
|
+
if (requireUsage && !usage) return unavailable(agentPath, 'token counters unavailable');
|
|
49
|
+
return { agentPath, status: 'available', sessionId, models, ...(requireUsage ? { usage } : { turnId }) };
|
|
40
50
|
}
|
|
41
51
|
|
|
42
|
-
|
|
52
|
+
async function collectSessions(manifest, requireUsage) {
|
|
43
53
|
if (!record(manifest) || typeof manifest.parentThreadId !== 'string' || !manifest.parentThreadId
|
|
44
54
|
|| !Array.isArray(manifest.agents) || !manifest.agents.length
|
|
45
|
-
|| manifest.agents.some(a => !record(a) || typeof a.agentPath !== 'string' || !a.agentPath || typeof a.sessionFile !== 'string' || !a.sessionFile)) {
|
|
55
|
+
|| Array.from(manifest.agents).some(a => !record(a) || typeof a.agentPath !== 'string' || !a.agentPath || typeof a.sessionFile !== 'string' || !a.sessionFile)) {
|
|
46
56
|
return { available: false, agents: [], totals: null, reason: 'manifest needs parentThreadId and explicit agentPath/sessionFile entries' };
|
|
47
57
|
}
|
|
48
58
|
const paths = manifest.agents.map(a => a.agentPath), files = manifest.agents.map(a => a.sessionFile);
|
|
@@ -50,13 +60,24 @@ export async function collectUsage(manifest) {
|
|
|
50
60
|
return { available: false, agents: [], totals: null, reason: 'manifest repeats an agent path or session file' };
|
|
51
61
|
}
|
|
52
62
|
let agents = [];
|
|
53
|
-
for (const agent of manifest.agents) agents = [...agents, await inspectSession(agent.sessionFile, manifest.parentThreadId, agent.agentPath)];
|
|
63
|
+
for (const agent of manifest.agents) agents = [...agents, await inspectSession(agent.sessionFile, manifest.parentThreadId, agent.agentPath, requireUsage)];
|
|
54
64
|
const available = agents.every(a => a.status !== 'unavailable');
|
|
65
|
+
if (!requireUsage) return { available, agents };
|
|
55
66
|
const totals = available ? Object.fromEntries(FIELDS.map(k => [k, agents.reduce((n, a) => n + a.usage[k], 0)])) : null;
|
|
56
67
|
const safe = totals && FIELDS.every(k => Number.isSafeInteger(totals[k]));
|
|
57
68
|
return { available: Boolean(safe), agents, totals: safe ? totals : null, ...(available && !safe ? { reason: 'aggregate token counters exceed safe integer range' } : {}) };
|
|
58
69
|
}
|
|
59
70
|
|
|
71
|
+
export async function collectUsage(manifest) {
|
|
72
|
+
return collectSessions(manifest, true);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Host terminal evidence only; does not validate assigned tasks or result artifacts. */
|
|
76
|
+
export async function collectCompletion(manifest) {
|
|
77
|
+
const { totals, ...completion } = await collectSessions(manifest, false);
|
|
78
|
+
return completion;
|
|
79
|
+
}
|
|
80
|
+
|
|
60
81
|
async function main(argv) {
|
|
61
82
|
const i = argv.indexOf('--manifest'), path = i < 0 ? null : argv[i + 1];
|
|
62
83
|
if (!path || path.startsWith('--')) { console.error('usage: codex-usage.mjs --manifest <explicit-sessions.json>'); process.exitCode = 2; return; }
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Pure validation for frozen defect cases and adjudication records.
|
|
2
|
+
const FULL_SHA = /^[a-f0-9]{40}$/i
|
|
3
|
+
const SHA256 = /^[a-f0-9]{64}$/i
|
|
4
|
+
const RUN_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/
|
|
5
|
+
const VERDICTS = new Set(['confirmed', 'false-alarm', 'suggestion', 'unresolved'])
|
|
6
|
+
const SEVERITIES = new Set(['critical', 'important', 'observation', 'idiomatic'])
|
|
7
|
+
|
|
8
|
+
const requireValue = (condition, message) => {
|
|
9
|
+
if (!condition) throw new Error(message)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function validateDefectManifest(manifest) {
|
|
13
|
+
requireValue(manifest?.schema === 1 && Array.isArray(manifest.cases) && manifest.cases.length > 0
|
|
14
|
+
&& Array.isArray(manifest.defects), 'defect manifest needs schema 1, cases, and defects')
|
|
15
|
+
requireValue(manifest.defects.every(defect => defect && typeof defect === 'object'), 'defects must be objects')
|
|
16
|
+
const defectIds = manifest.defects.map(defect => defect.id)
|
|
17
|
+
requireValue(defectIds.every(id => typeof id === 'string' && id) && new Set(defectIds).size === defectIds.length,
|
|
18
|
+
'defect ids must be unique non-empty strings')
|
|
19
|
+
requireValue(manifest.defects.every(defect => typeof defect.contract === 'string' && defect.contract.trim()
|
|
20
|
+
&& typeof defect.reproductionEvidence === 'string' && defect.reproductionEvidence.trim()),
|
|
21
|
+
'every defect needs a reproduction contract and evidence')
|
|
22
|
+
requireValue(manifest.cases.every(item => item && typeof item === 'object'), 'cases must be objects')
|
|
23
|
+
const caseIds = manifest.cases.map(item => item.id)
|
|
24
|
+
requireValue(caseIds.every(id => typeof id === 'string' && id) && new Set(caseIds).size === caseIds.length,
|
|
25
|
+
'case ids must be unique non-empty strings')
|
|
26
|
+
requireValue(manifest.cases.every(item => typeof item.headSha === 'string' && FULL_SHA.test(item.headSha)
|
|
27
|
+
&& Number.isInteger(item.expectedRuns) && item.expectedRuns > 0 && Array.isArray(item.expectedDefects)
|
|
28
|
+
&& item.expectedDefects.every(id => defectIds.includes(id))
|
|
29
|
+
&& new Set(item.expectedDefects).size === item.expectedDefects.length), 'case identity or expected defects are malformed')
|
|
30
|
+
return { caseIds, defectIds }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function validateDefectEvaluation(manifest, evaluation, findingsSha256, previousRunIds = []) {
|
|
34
|
+
const registry = validateDefectManifest(manifest)
|
|
35
|
+
const run = evaluation?.run
|
|
36
|
+
const adjudication = evaluation?.adjudication
|
|
37
|
+
const unavailable = reason => ({ valid: false, reason, caseId: run?.caseId })
|
|
38
|
+
if (!run || !adjudication || !registry.caseIds.includes(run.caseId)) return unavailable('run or registered case unavailable')
|
|
39
|
+
const testCase = manifest.cases.find(item => item.id === run.caseId)
|
|
40
|
+
if (run.status !== 'completed') return unavailable('run is not completed')
|
|
41
|
+
if (typeof run.runId !== 'string' || !RUN_ID.test(run.runId) || previousRunIds.includes(run.runId)) return unavailable('unsafe or duplicate runId')
|
|
42
|
+
if (run.headSha !== testCase.headSha || adjudication.headSha !== run.headSha || adjudication.runId !== run.runId) return unavailable('run identity mismatch')
|
|
43
|
+
// The edge hashes exact findings bytes; this pure check only binds that supplied hash.
|
|
44
|
+
if (!SHA256.test(findingsSha256 || '') || adjudication.findingsSha256 !== findingsSha256) return unavailable('finding bytes hash mismatch')
|
|
45
|
+
if (!Array.isArray(run.findings) || run.findings.some(finding => !finding || typeof finding.file !== 'string' || !finding.file.trim()
|
|
46
|
+
|| !(finding.line === null || (Number.isInteger(finding.line) && finding.line > 0)) || !SEVERITIES.has(finding.severity))) return unavailable('malformed findings')
|
|
47
|
+
const decisions = adjudication.decisions
|
|
48
|
+
if (!Array.isArray(decisions) || decisions.length !== run.findings.length || decisions.some(item => !item || typeof item !== 'object')) {
|
|
49
|
+
return unavailable('adjudication coverage incomplete or malformed')
|
|
50
|
+
}
|
|
51
|
+
const indexes = decisions.map(item => item.findingIndex)
|
|
52
|
+
const valid = decisions.every((item, index) => Number.isInteger(item.findingIndex) && item.findingIndex >= 0
|
|
53
|
+
&& item.findingIndex < run.findings.length && indexes.indexOf(item.findingIndex) === index && VERDICTS.has(item.verdict)
|
|
54
|
+
&& typeof item.rationale === 'string' && item.rationale.trim() && typeof item.evidence === 'string' && item.evidence.trim()
|
|
55
|
+
&& (item.verdict !== 'confirmed' || (typeof item.defectId === 'string' && item.defectId)))
|
|
56
|
+
if (!valid) return unavailable('finding decisions are malformed or duplicated')
|
|
57
|
+
return { valid: true, caseId: run.caseId, runId: run.runId, testCase, decisions }
|
|
58
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const record = value => value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
2
|
+
const text = value => typeof value === 'string' && value.trim().length > 0
|
|
3
|
+
const owns = (value, key) => Object.hasOwn(value, key)
|
|
4
|
+
const strings = (value, keys) => keys.every(key => typeof value[key] === 'string')
|
|
5
|
+
const optionalStrings = (value, keys) => keys.every(key => !owns(value, key) || typeof value[key] === 'string')
|
|
6
|
+
const instances = /^(?!\/)(?!.*\\)(?![A-Za-z]:)(?!(?:.*\/)?\.\.(?:\/|:)).+:[1-9][0-9]*$/
|
|
7
|
+
const ruleException = /^(PROJECT:[a-z0-9]+(-[a-z0-9]+)*|weakens-project-rule)$/
|
|
8
|
+
const dimensions = ['deps', 'triage-quality', 'triage-logic', 'quality', 'implementation', 'simplification', 'conventions', 'test-quality', 'project-rules']
|
|
9
|
+
|
|
10
|
+
function validBoundary(value) {
|
|
11
|
+
return record(value) && text(value.entryPoint) && text(value.consumer)
|
|
12
|
+
&& optionalStrings(value, ['decisionPoint', 'realizationPoint', 'stateOwner', 'evidenceClass'])
|
|
13
|
+
&& ['identityDimensions', 'transitions'].every(key => !owns(value, key)
|
|
14
|
+
|| (Array.isArray(value[key]) && Array.from(value[key]).every(item => typeof item === 'string')))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function validFinding(value) {
|
|
18
|
+
if (!record(value) || !strings(value, ['file', 'severity', 'title', 'description', 'badCode', 'fix', 'bugClass'])
|
|
19
|
+
|| !Number.isInteger(value.line) || !['critical', 'important', 'observation', 'idiomatic'].includes(value.severity)
|
|
20
|
+
|| !/^[a-z0-9]+(-[a-z0-9]+)*$/.test(value.bugClass)
|
|
21
|
+
|| !Array.isArray(value.otherInstances) || !Array.from(value.otherInstances).every(item => typeof item === 'string' && instances.test(item))
|
|
22
|
+
|| !optionalStrings(value, ['precedent', 'ruleQuote'])
|
|
23
|
+
|| (owns(value, 'rule') && !text(value.rule))
|
|
24
|
+
|| (owns(value, 'boundary') && !validBoundary(value.boundary))) return false
|
|
25
|
+
if (value.severity === 'idiomatic' && !text(value.precedent)) return false
|
|
26
|
+
if (['critical', 'important'].includes(value.severity)) {
|
|
27
|
+
return text(value.badCode) && text(value.fix)
|
|
28
|
+
&& (owns(value, 'boundary') || (typeof value.rule === 'string' && ruleException.test(value.rule)))
|
|
29
|
+
}
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Validate a parsed native reviewer document without changing its semantic output. */
|
|
34
|
+
export function validateNativeResult(dimension, document) {
|
|
35
|
+
const unavailable = reason => ({ available: false, reason })
|
|
36
|
+
if (!dimensions.includes(dimension)) return unavailable('unknown review dimension')
|
|
37
|
+
if (!record(document) || !Array.isArray(document.findings)) return unavailable('result must contain a findings array')
|
|
38
|
+
if (!Array.from(document.findings).every(validFinding)) return unavailable('finding violates the shared result contract')
|
|
39
|
+
const required = dimension === 'implementation' ? ['acCoverage']
|
|
40
|
+
: dimension === 'test-quality' ? ['coverageVerdict', 'mutationSlip'] : []
|
|
41
|
+
if (!strings(document, required)) return unavailable('required reviewer metadata is missing or malformed')
|
|
42
|
+
if (!optionalStrings(document, ['acCoverage', 'coverageVerdict', 'mutationSlip'])) return unavailable('reviewer metadata must be strings')
|
|
43
|
+
if ((dimension === 'test-quality' || owns(document, 'mockSmells'))
|
|
44
|
+
&& (!Array.isArray(document.mockSmells) || !Array.from(document.mockSmells).every(value => record(value)
|
|
45
|
+
&& strings(value, ['file', 'description', 'fix']) && Number.isInteger(value.line)))) {
|
|
46
|
+
return unavailable('mockSmells must contain valid file, line, description and fix records')
|
|
47
|
+
}
|
|
48
|
+
return { available: true, reason: null }
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -7,14 +7,17 @@ const FIELDS = ['input_tokens', 'cached_input_tokens', 'cache_write_input_tokens
|
|
|
7
7
|
const record = v => v && typeof v === 'object' && !Array.isArray(v);
|
|
8
8
|
const unavailable = (agentPath, reason) => ({ agentPath, status: 'unavailable', reason });
|
|
9
9
|
|
|
10
|
-
async function inspectSession(sessionFile, parentThreadId, agentPath) {
|
|
11
|
-
let sessionId = null, usage = null, complete = false, models = [];
|
|
10
|
+
async function inspectSession(sessionFile, parentThreadId, agentPath, requireUsage) {
|
|
11
|
+
let sessionId = null, usage = null, complete = false, models = [], terminalReason = null, turnId = null;
|
|
12
12
|
try {
|
|
13
13
|
const input = createReadStream(sessionFile);
|
|
14
14
|
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
|
|
15
15
|
if (!line.trim()) continue;
|
|
16
16
|
let row;
|
|
17
17
|
try { row = JSON.parse(line); } catch { return unavailable(agentPath, 'malformed JSONL'); }
|
|
18
|
+
if (!requireUsage && !record(row)) return unavailable(agentPath, 'malformed JSONL');
|
|
19
|
+
if (!requireUsage && row.type === 'event_msg' && ['task_complete', 'turn_aborted'].includes(row.payload?.type)
|
|
20
|
+
&& (!turnId || row.payload.turn_id !== turnId)) return unavailable(agentPath, 'task turn identity mismatch');
|
|
18
21
|
if (row.type === 'session_meta') {
|
|
19
22
|
const spawn = row.payload?.source?.subagent?.thread_spawn;
|
|
20
23
|
if (sessionId || typeof row.payload?.id !== 'string' || !row.payload.id) return unavailable(agentPath, 'session metadata missing or duplicated');
|
|
@@ -22,9 +25,16 @@ async function inspectSession(sessionFile, parentThreadId, agentPath) {
|
|
|
22
25
|
sessionId = row.payload.id;
|
|
23
26
|
} else if (row.type === 'turn_context' && typeof row.payload?.model === 'string' && row.payload.model && !models.includes(row.payload.model)) {
|
|
24
27
|
models = [...models, row.payload.model];
|
|
25
|
-
} else if (row.type === 'event_msg' && row.payload?.type === 'task_started')
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
} else if (row.type === 'event_msg' && row.payload?.type === 'task_started') {
|
|
29
|
+
if (!requireUsage && (typeof row.payload.turn_id !== 'string' || !row.payload.turn_id.trim())) return unavailable(agentPath, 'task turn identity unavailable');
|
|
30
|
+
if (!requireUsage) turnId = row.payload.turn_id;
|
|
31
|
+
complete = false;
|
|
32
|
+
terminalReason = null;
|
|
33
|
+
} else if (!requireUsage && row.type === 'event_msg' && row.payload?.type === 'turn_aborted') {
|
|
34
|
+
complete = false;
|
|
35
|
+
terminalReason = 'task aborted';
|
|
36
|
+
} else if (row.type === 'event_msg' && row.payload?.type === 'task_complete') complete = terminalReason === null;
|
|
37
|
+
else if (requireUsage && row.type === 'event_msg' && row.payload?.type === 'token_count') {
|
|
28
38
|
const raw = row.payload.info?.total_token_usage;
|
|
29
39
|
if (!record(raw) || FIELDS.some(k => !Number.isSafeInteger(raw[k]) || raw[k] < 0)) return unavailable(agentPath, 'token counters are malformed');
|
|
30
40
|
const next = Object.fromEntries(FIELDS.map(k => [k, raw[k]]));
|
|
@@ -34,15 +44,15 @@ async function inspectSession(sessionFile, parentThreadId, agentPath) {
|
|
|
34
44
|
}
|
|
35
45
|
} catch { return unavailable(agentPath, 'session file could not be read'); }
|
|
36
46
|
if (!sessionId) return unavailable(agentPath, 'session metadata unavailable');
|
|
37
|
-
if (!complete) return unavailable(agentPath, 'task completion unavailable');
|
|
38
|
-
if (!usage) return unavailable(agentPath, 'token counters unavailable');
|
|
39
|
-
return { agentPath, status: 'available', sessionId, models, usage };
|
|
47
|
+
if (!complete) return unavailable(agentPath, terminalReason || 'task completion unavailable');
|
|
48
|
+
if (requireUsage && !usage) return unavailable(agentPath, 'token counters unavailable');
|
|
49
|
+
return { agentPath, status: 'available', sessionId, models, ...(requireUsage ? { usage } : { turnId }) };
|
|
40
50
|
}
|
|
41
51
|
|
|
42
|
-
|
|
52
|
+
async function collectSessions(manifest, requireUsage) {
|
|
43
53
|
if (!record(manifest) || typeof manifest.parentThreadId !== 'string' || !manifest.parentThreadId
|
|
44
54
|
|| !Array.isArray(manifest.agents) || !manifest.agents.length
|
|
45
|
-
|| manifest.agents.some(a => !record(a) || typeof a.agentPath !== 'string' || !a.agentPath || typeof a.sessionFile !== 'string' || !a.sessionFile)) {
|
|
55
|
+
|| Array.from(manifest.agents).some(a => !record(a) || typeof a.agentPath !== 'string' || !a.agentPath || typeof a.sessionFile !== 'string' || !a.sessionFile)) {
|
|
46
56
|
return { available: false, agents: [], totals: null, reason: 'manifest needs parentThreadId and explicit agentPath/sessionFile entries' };
|
|
47
57
|
}
|
|
48
58
|
const paths = manifest.agents.map(a => a.agentPath), files = manifest.agents.map(a => a.sessionFile);
|
|
@@ -50,13 +60,24 @@ export async function collectUsage(manifest) {
|
|
|
50
60
|
return { available: false, agents: [], totals: null, reason: 'manifest repeats an agent path or session file' };
|
|
51
61
|
}
|
|
52
62
|
let agents = [];
|
|
53
|
-
for (const agent of manifest.agents) agents = [...agents, await inspectSession(agent.sessionFile, manifest.parentThreadId, agent.agentPath)];
|
|
63
|
+
for (const agent of manifest.agents) agents = [...agents, await inspectSession(agent.sessionFile, manifest.parentThreadId, agent.agentPath, requireUsage)];
|
|
54
64
|
const available = agents.every(a => a.status !== 'unavailable');
|
|
65
|
+
if (!requireUsage) return { available, agents };
|
|
55
66
|
const totals = available ? Object.fromEntries(FIELDS.map(k => [k, agents.reduce((n, a) => n + a.usage[k], 0)])) : null;
|
|
56
67
|
const safe = totals && FIELDS.every(k => Number.isSafeInteger(totals[k]));
|
|
57
68
|
return { available: Boolean(safe), agents, totals: safe ? totals : null, ...(available && !safe ? { reason: 'aggregate token counters exceed safe integer range' } : {}) };
|
|
58
69
|
}
|
|
59
70
|
|
|
71
|
+
export async function collectUsage(manifest) {
|
|
72
|
+
return collectSessions(manifest, true);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Host terminal evidence only; does not validate assigned tasks or result artifacts. */
|
|
76
|
+
export async function collectCompletion(manifest) {
|
|
77
|
+
const { totals, ...completion } = await collectSessions(manifest, false);
|
|
78
|
+
return completion;
|
|
79
|
+
}
|
|
80
|
+
|
|
60
81
|
async function main(argv) {
|
|
61
82
|
const i = argv.indexOf('--manifest'), path = i < 0 ? null : argv[i + 1];
|
|
62
83
|
if (!path || path.startsWith('--')) { console.error('usage: codex-usage.mjs --manifest <explicit-sessions.json>'); process.exitCode = 2; return; }
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Pure validation for frozen defect cases and adjudication records.
|
|
2
|
+
const FULL_SHA = /^[a-f0-9]{40}$/i
|
|
3
|
+
const SHA256 = /^[a-f0-9]{64}$/i
|
|
4
|
+
const RUN_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/
|
|
5
|
+
const VERDICTS = new Set(['confirmed', 'false-alarm', 'suggestion', 'unresolved'])
|
|
6
|
+
const SEVERITIES = new Set(['critical', 'important', 'observation', 'idiomatic'])
|
|
7
|
+
|
|
8
|
+
const requireValue = (condition, message) => {
|
|
9
|
+
if (!condition) throw new Error(message)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function validateDefectManifest(manifest) {
|
|
13
|
+
requireValue(manifest?.schema === 1 && Array.isArray(manifest.cases) && manifest.cases.length > 0
|
|
14
|
+
&& Array.isArray(manifest.defects), 'defect manifest needs schema 1, cases, and defects')
|
|
15
|
+
requireValue(manifest.defects.every(defect => defect && typeof defect === 'object'), 'defects must be objects')
|
|
16
|
+
const defectIds = manifest.defects.map(defect => defect.id)
|
|
17
|
+
requireValue(defectIds.every(id => typeof id === 'string' && id) && new Set(defectIds).size === defectIds.length,
|
|
18
|
+
'defect ids must be unique non-empty strings')
|
|
19
|
+
requireValue(manifest.defects.every(defect => typeof defect.contract === 'string' && defect.contract.trim()
|
|
20
|
+
&& typeof defect.reproductionEvidence === 'string' && defect.reproductionEvidence.trim()),
|
|
21
|
+
'every defect needs a reproduction contract and evidence')
|
|
22
|
+
requireValue(manifest.cases.every(item => item && typeof item === 'object'), 'cases must be objects')
|
|
23
|
+
const caseIds = manifest.cases.map(item => item.id)
|
|
24
|
+
requireValue(caseIds.every(id => typeof id === 'string' && id) && new Set(caseIds).size === caseIds.length,
|
|
25
|
+
'case ids must be unique non-empty strings')
|
|
26
|
+
requireValue(manifest.cases.every(item => typeof item.headSha === 'string' && FULL_SHA.test(item.headSha)
|
|
27
|
+
&& Number.isInteger(item.expectedRuns) && item.expectedRuns > 0 && Array.isArray(item.expectedDefects)
|
|
28
|
+
&& item.expectedDefects.every(id => defectIds.includes(id))
|
|
29
|
+
&& new Set(item.expectedDefects).size === item.expectedDefects.length), 'case identity or expected defects are malformed')
|
|
30
|
+
return { caseIds, defectIds }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function validateDefectEvaluation(manifest, evaluation, findingsSha256, previousRunIds = []) {
|
|
34
|
+
const registry = validateDefectManifest(manifest)
|
|
35
|
+
const run = evaluation?.run
|
|
36
|
+
const adjudication = evaluation?.adjudication
|
|
37
|
+
const unavailable = reason => ({ valid: false, reason, caseId: run?.caseId })
|
|
38
|
+
if (!run || !adjudication || !registry.caseIds.includes(run.caseId)) return unavailable('run or registered case unavailable')
|
|
39
|
+
const testCase = manifest.cases.find(item => item.id === run.caseId)
|
|
40
|
+
if (run.status !== 'completed') return unavailable('run is not completed')
|
|
41
|
+
if (typeof run.runId !== 'string' || !RUN_ID.test(run.runId) || previousRunIds.includes(run.runId)) return unavailable('unsafe or duplicate runId')
|
|
42
|
+
if (run.headSha !== testCase.headSha || adjudication.headSha !== run.headSha || adjudication.runId !== run.runId) return unavailable('run identity mismatch')
|
|
43
|
+
// The edge hashes exact findings bytes; this pure check only binds that supplied hash.
|
|
44
|
+
if (!SHA256.test(findingsSha256 || '') || adjudication.findingsSha256 !== findingsSha256) return unavailable('finding bytes hash mismatch')
|
|
45
|
+
if (!Array.isArray(run.findings) || run.findings.some(finding => !finding || typeof finding.file !== 'string' || !finding.file.trim()
|
|
46
|
+
|| !(finding.line === null || (Number.isInteger(finding.line) && finding.line > 0)) || !SEVERITIES.has(finding.severity))) return unavailable('malformed findings')
|
|
47
|
+
const decisions = adjudication.decisions
|
|
48
|
+
if (!Array.isArray(decisions) || decisions.length !== run.findings.length || decisions.some(item => !item || typeof item !== 'object')) {
|
|
49
|
+
return unavailable('adjudication coverage incomplete or malformed')
|
|
50
|
+
}
|
|
51
|
+
const indexes = decisions.map(item => item.findingIndex)
|
|
52
|
+
const valid = decisions.every((item, index) => Number.isInteger(item.findingIndex) && item.findingIndex >= 0
|
|
53
|
+
&& item.findingIndex < run.findings.length && indexes.indexOf(item.findingIndex) === index && VERDICTS.has(item.verdict)
|
|
54
|
+
&& typeof item.rationale === 'string' && item.rationale.trim() && typeof item.evidence === 'string' && item.evidence.trim()
|
|
55
|
+
&& (item.verdict !== 'confirmed' || (typeof item.defectId === 'string' && item.defectId)))
|
|
56
|
+
if (!valid) return unavailable('finding decisions are malformed or duplicated')
|
|
57
|
+
return { valid: true, caseId: run.caseId, runId: run.runId, testCase, decisions }
|
|
58
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const record = value => value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
2
|
+
const text = value => typeof value === 'string' && value.trim().length > 0
|
|
3
|
+
const owns = (value, key) => Object.hasOwn(value, key)
|
|
4
|
+
const strings = (value, keys) => keys.every(key => typeof value[key] === 'string')
|
|
5
|
+
const optionalStrings = (value, keys) => keys.every(key => !owns(value, key) || typeof value[key] === 'string')
|
|
6
|
+
const instances = /^(?!\/)(?!.*\\)(?![A-Za-z]:)(?!(?:.*\/)?\.\.(?:\/|:)).+:[1-9][0-9]*$/
|
|
7
|
+
const ruleException = /^(PROJECT:[a-z0-9]+(-[a-z0-9]+)*|weakens-project-rule)$/
|
|
8
|
+
const dimensions = ['deps', 'triage-quality', 'triage-logic', 'quality', 'implementation', 'simplification', 'conventions', 'test-quality', 'project-rules']
|
|
9
|
+
|
|
10
|
+
function validBoundary(value) {
|
|
11
|
+
return record(value) && text(value.entryPoint) && text(value.consumer)
|
|
12
|
+
&& optionalStrings(value, ['decisionPoint', 'realizationPoint', 'stateOwner', 'evidenceClass'])
|
|
13
|
+
&& ['identityDimensions', 'transitions'].every(key => !owns(value, key)
|
|
14
|
+
|| (Array.isArray(value[key]) && Array.from(value[key]).every(item => typeof item === 'string')))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function validFinding(value) {
|
|
18
|
+
if (!record(value) || !strings(value, ['file', 'severity', 'title', 'description', 'badCode', 'fix', 'bugClass'])
|
|
19
|
+
|| !Number.isInteger(value.line) || !['critical', 'important', 'observation', 'idiomatic'].includes(value.severity)
|
|
20
|
+
|| !/^[a-z0-9]+(-[a-z0-9]+)*$/.test(value.bugClass)
|
|
21
|
+
|| !Array.isArray(value.otherInstances) || !Array.from(value.otherInstances).every(item => typeof item === 'string' && instances.test(item))
|
|
22
|
+
|| !optionalStrings(value, ['precedent', 'ruleQuote'])
|
|
23
|
+
|| (owns(value, 'rule') && !text(value.rule))
|
|
24
|
+
|| (owns(value, 'boundary') && !validBoundary(value.boundary))) return false
|
|
25
|
+
if (value.severity === 'idiomatic' && !text(value.precedent)) return false
|
|
26
|
+
if (['critical', 'important'].includes(value.severity)) {
|
|
27
|
+
return text(value.badCode) && text(value.fix)
|
|
28
|
+
&& (owns(value, 'boundary') || (typeof value.rule === 'string' && ruleException.test(value.rule)))
|
|
29
|
+
}
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Validate a parsed native reviewer document without changing its semantic output. */
|
|
34
|
+
export function validateNativeResult(dimension, document) {
|
|
35
|
+
const unavailable = reason => ({ available: false, reason })
|
|
36
|
+
if (!dimensions.includes(dimension)) return unavailable('unknown review dimension')
|
|
37
|
+
if (!record(document) || !Array.isArray(document.findings)) return unavailable('result must contain a findings array')
|
|
38
|
+
if (!Array.from(document.findings).every(validFinding)) return unavailable('finding violates the shared result contract')
|
|
39
|
+
const required = dimension === 'implementation' ? ['acCoverage']
|
|
40
|
+
: dimension === 'test-quality' ? ['coverageVerdict', 'mutationSlip'] : []
|
|
41
|
+
if (!strings(document, required)) return unavailable('required reviewer metadata is missing or malformed')
|
|
42
|
+
if (!optionalStrings(document, ['acCoverage', 'coverageVerdict', 'mutationSlip'])) return unavailable('reviewer metadata must be strings')
|
|
43
|
+
if ((dimension === 'test-quality' || owns(document, 'mockSmells'))
|
|
44
|
+
&& (!Array.isArray(document.mockSmells) || !Array.from(document.mockSmells).every(value => record(value)
|
|
45
|
+
&& strings(value, ['file', 'description', 'fix']) && Number.isInteger(value.line)))) {
|
|
46
|
+
return unavailable('mockSmells must contain valid file, line, description and fix records')
|
|
47
|
+
}
|
|
48
|
+
return { available: true, reason: null }
|
|
49
|
+
}
|