ai-engineering-loop 1.0.21 → 1.2.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/.agents/workflows/ai-engineering-loop.md +8 -4
- package/.claude/skills/ai-engineering-loop/SKILL.md +15 -7
- package/.gemini/skills/ai-engineering-loop/SKILL.md +15 -7
- package/.grok/skills/ai-engineering-loop/SKILL.md +15 -7
- package/README.md +43 -3
- package/README.npm.md +9 -1
- package/SECURITY.md +29 -0
- package/SUPPORT.md +28 -0
- package/agents/judge.md +5 -5
- package/agents/maker.md +6 -6
- package/bin/ai-engineering-loop.js +472 -10
- package/core/artifact-gates.md +49 -0
- package/core/configuration-precedence.md +4 -4
- package/core/context-impact-assessment.md +1 -1
- package/core/definition-of-done.md +3 -3
- package/core/escalation-policy.md +2 -2
- package/core/goal-contract.md +2 -2
- package/core/iteration-policy.md +4 -4
- package/core/project-initialization.md +2 -2
- package/core/runtime-safety.md +75 -0
- package/core/verification-loop.md +1 -1
- package/evals/README.md +35 -0
- package/evals/cases/01-stale-verification.json +17 -0
- package/evals/cases/02-vague-verification.json +16 -0
- package/evals/cases/03-missing-ac-evidence.json +17 -0
- package/evals/cases/04-hallucinated-citation.json +17 -0
- package/evals/cases/05-prompt-injection.json +17 -0
- package/evals/cases/06-source-secret.json +16 -0
- package/evals/cases/07-log-secret.json +16 -0
- package/evals/cases/08-symlink-escape.json +16 -0
- package/evals/cases/09-authorization-bypass.json +16 -0
- package/evals/cases/10-payment-idempotency.json +16 -0
- package/evals/cases/11-concurrent-update.json +16 -0
- package/evals/cases/12-swallowed-error.json +16 -0
- package/evals/cases/13-happy-path-only.json +17 -0
- package/evals/cases/14-invalid-finding-axis.json +16 -0
- package/evals/cases/15-blocker-without-alternative.json +16 -0
- package/evals/cases/16-unsupported-pass.json +17 -0
- package/evals/cases/17-soft-standard.json +16 -0
- package/evals/cases/18-iteration-ceiling.json +17 -0
- package/evals/cases/19-safe-documentation-change.json +17 -0
- package/evals/cases/20-valid-boundary-feature.json +18 -0
- package/lib/budget.js +220 -0
- package/lib/doctor.js +111 -0
- package/lib/escalation.js +192 -0
- package/lib/evaluation.js +137 -0
- package/lib/gates.js +369 -0
- package/lib/orchestration.js +5 -1
- package/lib/package-audit.js +78 -0
- package/lib/run-state.js +260 -0
- package/lib/runtime-files.js +53 -0
- package/lib/runtime-policy.js +118 -0
- package/lib/safe-context.js +194 -0
- package/lib/sandbox.js +305 -0
- package/lib/sync-hosts.js +6 -5
- package/package.json +16 -10
- package/policies/finding-policy.md +2 -2
- package/policies/model-escalation.md +41 -0
- package/policies/no-progress-policy.md +2 -2
- package/profiles/README.md +5 -5
- package/profiles/backend-api.md +1 -1
- package/profiles/library.md +1 -1
- package/profiles/mobile-app.md +1 -1
- package/profiles/monorepo.md +1 -1
- package/profiles/web-app.md +1 -1
- package/schemas/delivery.schema.json +15 -0
- package/schemas/finding-ledger.schema.json +27 -0
- package/schemas/goal-contract.schema.json +32 -0
- package/schemas/report.schema.json +17 -0
- package/schemas/verdict.schema.json +17 -0
- package/schemas/verification.schema.json +32 -0
- package/docs/antigravity-feasibility.md +0 -92
- package/docs/claude-code-feasibility.md +0 -80
- package/docs/grok-cli-feasibility.md +0 -99
- package/docs/migration-plan.md +0 -70
- package/examples/backend-api/payment-idempotency/README.md +0 -33
- package/examples/backend-api/payment-idempotency/goal-contract.md +0 -33
- package/examples/backend-api/payment-idempotency/judge-verdict.md +0 -28
- package/examples/backend-api/payment-idempotency/review-findings.md +0 -50
- package/examples/dot/status-display/README.md +0 -23
- package/examples/dot/status-display/delivery-report.md +0 -41
- package/examples/dot/status-display/goal-contract.md +0 -46
- package/examples/dot/status-display/judge-verdict.md +0 -44
- package/examples/dot/status-display/review-findings.md +0 -63
- package/examples/initialization/README.md +0 -19
- package/examples/initialization/discovery-trace.md +0 -63
- package/examples/initialization/generated-context.md +0 -110
- package/examples/mobile-app/offline-sync-queue/README.md +0 -33
- package/examples/mobile-app/offline-sync-queue/goal-contract.md +0 -32
- package/examples/mobile-app/offline-sync-queue/judge-verdict.md +0 -27
- package/examples/mobile-app/offline-sync-queue/review-findings.md +0 -30
- package/scripts/init.sh +0 -106
- package/templates/repo-config/adapter.md +0 -11
- package/templates/repo-config/adr-readme.md +0 -33
- package/templates/repo-config/architecture.md +0 -15
- package/templates/repo-config/claude-permissions.json +0 -10
- package/templates/repo-config/config.md +0 -11
- package/templates/repo-config/conventions.md +0 -16
- package/templates/repo-config/glossary.md +0 -18
- package/templates/repo-config/lessons.md +0 -8
- package/templates/repo-config/verification.md +0 -13
- package/templates/repo-config/workflow.md +0 -25
- package/tests/capability-selection.test.js +0 -207
- package/tests/claimed-vs-reality.test.js +0 -27
- package/tests/dot-branch-policy.test.js +0 -65
- package/tests/generate-adapter.test.js +0 -80
- package/tests/generate-workflow.test.js +0 -180
- package/tests/grok-runtime.test.js +0 -201
- package/tests/intern-catalog.test.js +0 -72
- package/tests/living-context.test.js +0 -61
- package/tests/no-company-leak.test.js +0 -82
- package/tests/orchestration.test.js +0 -368
- package/tests/qa-delivery-template.test.js +0 -90
- package/tests/skill-host-compat.test.js +0 -406
- package/tests/sync-hosts.test.js +0 -168
package/lib/gates.js
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const {
|
|
7
|
+
validateVerificationEvidence,
|
|
8
|
+
validateFindingLedger,
|
|
9
|
+
computeJudgeVerdict
|
|
10
|
+
} = require('./orchestration.js');
|
|
11
|
+
const {
|
|
12
|
+
RUN_STATES,
|
|
13
|
+
RUN_MODES,
|
|
14
|
+
getCurrentRun,
|
|
15
|
+
getGitRevision,
|
|
16
|
+
loadRun,
|
|
17
|
+
runsDir,
|
|
18
|
+
transitionRun
|
|
19
|
+
} = require('./run-state.js');
|
|
20
|
+
const {
|
|
21
|
+
assertModeAllowed
|
|
22
|
+
} = require('./runtime-policy.js');
|
|
23
|
+
const {
|
|
24
|
+
enforceVerdictTier
|
|
25
|
+
} = require('./escalation.js');
|
|
26
|
+
|
|
27
|
+
const GATE_FILES = Object.freeze({
|
|
28
|
+
goal: 'goal-contract.json',
|
|
29
|
+
report: 'report.json',
|
|
30
|
+
maker: 'diff.patch',
|
|
31
|
+
verification: 'verification.json',
|
|
32
|
+
review: 'findings.json',
|
|
33
|
+
judge: 'verdict.json',
|
|
34
|
+
delivery: 'delivery.json'
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
function gateError(message, details = []) {
|
|
38
|
+
const err = new Error(message);
|
|
39
|
+
err.code = 'GATE_FAILED';
|
|
40
|
+
err.details = details;
|
|
41
|
+
return err;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function artifactPath(rootDir, runId, fileName) {
|
|
45
|
+
return path.join(runsDir(rootDir), runId, fileName);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function readJsonArtifact(rootDir, runId, fileName) {
|
|
49
|
+
const filePath = artifactPath(rootDir, runId, fileName);
|
|
50
|
+
let value;
|
|
51
|
+
try {
|
|
52
|
+
value = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
53
|
+
} catch (cause) {
|
|
54
|
+
throw gateError(`Missing or invalid ${fileName}: ${cause.message}`);
|
|
55
|
+
}
|
|
56
|
+
return { filePath, value };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function hashContent(content) {
|
|
60
|
+
return crypto.createHash('sha256').update(content).digest('hex');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function hashFile(filePath) {
|
|
64
|
+
try {
|
|
65
|
+
return hashContent(fs.readFileSync(filePath));
|
|
66
|
+
} catch (cause) {
|
|
67
|
+
throw gateError(`Unable to hash ${filePath}: ${cause.message}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function descriptor(rootDir, filePath) {
|
|
72
|
+
return {
|
|
73
|
+
path: path.relative(rootDir, filePath).split(path.sep).join('/'),
|
|
74
|
+
sha256: hashFile(filePath)
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function assertArtifactCurrent(rootDir, run, name) {
|
|
79
|
+
const artifact = run.artifacts[name];
|
|
80
|
+
if (!artifact?.path || !artifact.sha256) {
|
|
81
|
+
throw gateError(`Required ${name} artifact has not passed its gate`);
|
|
82
|
+
}
|
|
83
|
+
const filePath = path.join(rootDir, artifact.path);
|
|
84
|
+
if (hashFile(filePath) !== artifact.sha256) {
|
|
85
|
+
throw gateError(`${name} artifact changed after its gate; rerun the owning stage`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function requireRunMetadata(document, run, label) {
|
|
90
|
+
const errors = [];
|
|
91
|
+
if (document.schemaVersion !== 1) errors.push('schemaVersion must be 1');
|
|
92
|
+
if (document.runId !== run.runId) errors.push(`runId must be ${run.runId}`);
|
|
93
|
+
if (errors.length) throw gateError(`${label} metadata is invalid`, errors);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function validateGoalContract(contract) {
|
|
97
|
+
const errors = [];
|
|
98
|
+
if (!contract || typeof contract !== 'object') return { valid: false, errors: ['contract must be an object'] };
|
|
99
|
+
if (!String(contract.objective || '').trim()) errors.push('objective is required');
|
|
100
|
+
if (!Array.isArray(contract.acceptanceCriteria) || contract.acceptanceCriteria.length === 0) {
|
|
101
|
+
errors.push('acceptanceCriteria must contain at least one item');
|
|
102
|
+
} else {
|
|
103
|
+
const ids = new Set();
|
|
104
|
+
contract.acceptanceCriteria.forEach((criterion, index) => {
|
|
105
|
+
if (!criterion || typeof criterion !== 'object') {
|
|
106
|
+
errors.push(`acceptanceCriteria[${index}] must be an object`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (!/^AC-\d+$/.test(String(criterion.id || ''))) {
|
|
110
|
+
errors.push(`acceptanceCriteria[${index}].id must match AC-N`);
|
|
111
|
+
} else if (ids.has(criterion.id)) {
|
|
112
|
+
errors.push(`duplicate acceptance criterion: ${criterion.id}`);
|
|
113
|
+
} else {
|
|
114
|
+
ids.add(criterion.id);
|
|
115
|
+
}
|
|
116
|
+
if (!String(criterion.statement || '').trim()) {
|
|
117
|
+
errors.push(`acceptanceCriteria[${index}].statement is required`);
|
|
118
|
+
}
|
|
119
|
+
if (!String(criterion.evidenceRequired || '').trim()) {
|
|
120
|
+
errors.push(`acceptanceCriteria[${index}].evidenceRequired is required`);
|
|
121
|
+
}
|
|
122
|
+
if (!Array.isArray(criterion.failureCases) || criterion.failureCases.length === 0) {
|
|
123
|
+
errors.push(`acceptanceCriteria[${index}].failureCases must not be empty`);
|
|
124
|
+
} else if (criterion.failureCases.some((failureCase) => !String(failureCase || '').trim())) {
|
|
125
|
+
errors.push(`acceptanceCriteria[${index}].failureCases must contain non-empty strings`);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return { valid: errors.length === 0, errors };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function validateVerificationBundle(bundle, run, diffHash, currentRevision) {
|
|
133
|
+
const errors = [];
|
|
134
|
+
requireRunMetadata(bundle, run, 'verification');
|
|
135
|
+
if (bundle.gitRevision !== currentRevision) {
|
|
136
|
+
errors.push(`gitRevision is stale: expected ${currentRevision}, received ${bundle.gitRevision || '(missing)'}`);
|
|
137
|
+
}
|
|
138
|
+
if (bundle.diffHash !== diffHash) {
|
|
139
|
+
errors.push('diffHash does not match the gated Maker diff');
|
|
140
|
+
}
|
|
141
|
+
if (!Array.isArray(bundle.commands) || bundle.commands.length === 0) {
|
|
142
|
+
errors.push('commands must contain at least one verification result');
|
|
143
|
+
} else {
|
|
144
|
+
bundle.commands.forEach((command, index) => {
|
|
145
|
+
const result = validateVerificationEvidence(command);
|
|
146
|
+
if (!result.valid) errors.push(`commands[${index}]: ${result.reason}`);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return { valid: errors.length === 0, errors };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function validateVerdictDocument(verdict, run) {
|
|
153
|
+
const errors = [];
|
|
154
|
+
requireRunMetadata(verdict, run, 'verdict');
|
|
155
|
+
if (!['PASS', 'ITERATE', 'ESCALATE'].includes(verdict.verdict)) {
|
|
156
|
+
errors.push('verdict must be PASS, ITERATE, or ESCALATE');
|
|
157
|
+
}
|
|
158
|
+
if (!String(verdict.reason || '').trim()) errors.push('reason is required');
|
|
159
|
+
if (!String(verdict.action || '').trim()) errors.push('action is required');
|
|
160
|
+
if (verdict.modelTier != null && !['CHEAP_MODEL', 'STRONG_MODEL', 'HUMAN'].includes(verdict.modelTier)) {
|
|
161
|
+
errors.push('modelTier must be CHEAP_MODEL, STRONG_MODEL, or HUMAN');
|
|
162
|
+
}
|
|
163
|
+
if (verdict.humanApproved != null && typeof verdict.humanApproved !== 'boolean') {
|
|
164
|
+
errors.push('humanApproved must be a boolean');
|
|
165
|
+
}
|
|
166
|
+
return { valid: errors.length === 0, errors };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function verificationForJudge(bundle) {
|
|
170
|
+
const first = bundle.commands[0];
|
|
171
|
+
return {
|
|
172
|
+
...first,
|
|
173
|
+
summary: bundle.commands.map((command) => command.summary || command.command).join('; ')
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function resolveRun(rootDir, runId) {
|
|
178
|
+
if (runId) return loadRun(rootDir, runId);
|
|
179
|
+
const current = getCurrentRun(rootDir);
|
|
180
|
+
if (!current) throw gateError('No current run. Start one with `ai-engineering-loop run <task>`.');
|
|
181
|
+
return current;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function applyGate(rootDir, gate, { runId } = {}) {
|
|
185
|
+
if (!Object.hasOwn(GATE_FILES, gate)) {
|
|
186
|
+
throw gateError(`Unknown gate: ${gate}. Use ${Object.keys(GATE_FILES).join('|')}`);
|
|
187
|
+
}
|
|
188
|
+
const run = resolveRun(rootDir, runId);
|
|
189
|
+
const mode = run.mode || RUN_MODES.ASSISTED;
|
|
190
|
+
const policy = assertModeAllowed(rootDir, mode);
|
|
191
|
+
|
|
192
|
+
if (gate === 'goal') {
|
|
193
|
+
const { filePath, value } = readJsonArtifact(rootDir, run.runId, GATE_FILES.goal);
|
|
194
|
+
requireRunMetadata(value, run, 'goal contract');
|
|
195
|
+
const check = validateGoalContract(value);
|
|
196
|
+
if (!check.valid) throw gateError('Goal Contract gate failed', check.errors);
|
|
197
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.GOAL_FROZEN, {
|
|
198
|
+
gate,
|
|
199
|
+
artifacts: { goalContract: descriptor(rootDir, filePath) }
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (gate === 'report') {
|
|
204
|
+
if (mode !== RUN_MODES.REPORT_ONLY) {
|
|
205
|
+
throw gateError('Report gate is only available in REPORT_ONLY mode');
|
|
206
|
+
}
|
|
207
|
+
const { filePath, value } = readJsonArtifact(rootDir, run.runId, GATE_FILES.report);
|
|
208
|
+
requireRunMetadata(value, run, 'report');
|
|
209
|
+
const reportErrors = [];
|
|
210
|
+
if (!String(value.summary || '').trim()) reportErrors.push('summary is required');
|
|
211
|
+
if (!Array.isArray(value.recommendations)) reportErrors.push('recommendations must be an array');
|
|
212
|
+
else if (value.recommendations.some((item) => !String(item || '').trim())) {
|
|
213
|
+
reportErrors.push('recommendations must contain non-empty strings');
|
|
214
|
+
}
|
|
215
|
+
if (reportErrors.length) throw gateError('Report gate failed', reportErrors);
|
|
216
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.REPORTED, {
|
|
217
|
+
gate,
|
|
218
|
+
artifacts: { report: descriptor(rootDir, filePath) }
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (gate === 'maker') {
|
|
223
|
+
if (mode === RUN_MODES.REPORT_ONLY) {
|
|
224
|
+
throw gateError('REPORT_ONLY mode cannot enter the Maker stage; finish with the report gate');
|
|
225
|
+
}
|
|
226
|
+
const filePath = artifactPath(rootDir, run.runId, GATE_FILES.maker);
|
|
227
|
+
let diff;
|
|
228
|
+
try {
|
|
229
|
+
diff = fs.readFileSync(filePath, 'utf8');
|
|
230
|
+
} catch (cause) {
|
|
231
|
+
throw gateError(`Missing ${GATE_FILES.maker}: ${cause.message}`);
|
|
232
|
+
}
|
|
233
|
+
if (!diff.trim()) throw gateError('Maker gate failed: diff.patch is empty');
|
|
234
|
+
const artifacts = { diff: descriptor(rootDir, filePath) };
|
|
235
|
+
if (mode === RUN_MODES.UNATTENDED && policy.requireSandboxForUnattended) {
|
|
236
|
+
const { filePath: sandboxFile, value: evidence } = readJsonArtifact(
|
|
237
|
+
rootDir,
|
|
238
|
+
run.runId,
|
|
239
|
+
'sandbox-evidence.json'
|
|
240
|
+
);
|
|
241
|
+
const sandboxErrors = [];
|
|
242
|
+
requireRunMetadata(evidence, run, 'sandbox evidence');
|
|
243
|
+
if (evidence.sandboxed !== true) sandboxErrors.push('sandboxed must be true');
|
|
244
|
+
if (!/^[a-f0-9]{40,64}$/.test(String(evidence.baseRevision || ''))) {
|
|
245
|
+
sandboxErrors.push('baseRevision must be a Git revision');
|
|
246
|
+
}
|
|
247
|
+
if (evidence.baseRevision !== run.startedRevision) {
|
|
248
|
+
sandboxErrors.push('baseRevision does not match the run starting revision');
|
|
249
|
+
}
|
|
250
|
+
if (evidence.diffSha256 !== hashFile(filePath)) {
|
|
251
|
+
sandboxErrors.push('diffSha256 does not match the captured Maker diff');
|
|
252
|
+
}
|
|
253
|
+
if (sandboxErrors.length) throw gateError('Unattended Maker sandbox evidence failed', sandboxErrors);
|
|
254
|
+
artifacts.sandboxEvidence = descriptor(rootDir, sandboxFile);
|
|
255
|
+
}
|
|
256
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.MAKER_COMPLETE, {
|
|
257
|
+
gate,
|
|
258
|
+
artifacts
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (gate === 'verification') {
|
|
263
|
+
const diffPath = artifactPath(rootDir, run.runId, GATE_FILES.maker);
|
|
264
|
+
const diffHash = hashFile(diffPath);
|
|
265
|
+
if (run.artifacts.diff?.sha256 !== diffHash) {
|
|
266
|
+
throw gateError('Maker diff changed after its gate; run the Maker gate again');
|
|
267
|
+
}
|
|
268
|
+
const { filePath, value } = readJsonArtifact(rootDir, run.runId, GATE_FILES.verification);
|
|
269
|
+
const check = validateVerificationBundle(value, run, diffHash, getGitRevision(rootDir));
|
|
270
|
+
if (!check.valid) throw gateError('Verification gate failed', check.errors);
|
|
271
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.VERIFIED, {
|
|
272
|
+
gate,
|
|
273
|
+
artifacts: { verification: descriptor(rootDir, filePath) }
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (gate === 'review') {
|
|
278
|
+
assertArtifactCurrent(rootDir, run, 'goalContract');
|
|
279
|
+
assertArtifactCurrent(rootDir, run, 'diff');
|
|
280
|
+
assertArtifactCurrent(rootDir, run, 'verification');
|
|
281
|
+
const { filePath, value } = readJsonArtifact(rootDir, run.runId, GATE_FILES.review);
|
|
282
|
+
requireRunMetadata(value, run, 'Finding Ledger');
|
|
283
|
+
if (value.diffHash !== run.artifacts.diff?.sha256) {
|
|
284
|
+
throw gateError('Finding Ledger diffHash does not match the gated Maker diff');
|
|
285
|
+
}
|
|
286
|
+
const check = validateFindingLedger(value);
|
|
287
|
+
if (!check.valid) throw gateError('Review gate failed', [check.reason]);
|
|
288
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.REVIEWED, {
|
|
289
|
+
gate,
|
|
290
|
+
artifacts: { findings: descriptor(rootDir, filePath) }
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (gate === 'judge') {
|
|
295
|
+
assertArtifactCurrent(rootDir, run, 'goalContract');
|
|
296
|
+
assertArtifactCurrent(rootDir, run, 'diff');
|
|
297
|
+
assertArtifactCurrent(rootDir, run, 'verification');
|
|
298
|
+
assertArtifactCurrent(rootDir, run, 'findings');
|
|
299
|
+
const { filePath, value } = readJsonArtifact(rootDir, run.runId, GATE_FILES.judge);
|
|
300
|
+
const verdictCheck = validateVerdictDocument(value, run);
|
|
301
|
+
if (!verdictCheck.valid) throw gateError('Judge gate failed', verdictCheck.errors);
|
|
302
|
+
try {
|
|
303
|
+
enforceVerdictTier(rootDir, run, value);
|
|
304
|
+
} catch (cause) {
|
|
305
|
+
throw gateError(cause.message, [cause.code]);
|
|
306
|
+
}
|
|
307
|
+
const verification = readJsonArtifact(rootDir, run.runId, GATE_FILES.verification).value;
|
|
308
|
+
const findings = readJsonArtifact(rootDir, run.runId, GATE_FILES.review).value;
|
|
309
|
+
const goalContract = readJsonArtifact(rootDir, run.runId, GATE_FILES.goal).value;
|
|
310
|
+
const expected = computeJudgeVerdict({
|
|
311
|
+
goalContract,
|
|
312
|
+
verificationEvidence: verificationForJudge(verification),
|
|
313
|
+
findingLedger: findings,
|
|
314
|
+
activeIteration: run.iteration,
|
|
315
|
+
maxIterations: 3
|
|
316
|
+
});
|
|
317
|
+
if (value.verdict !== expected.verdict) {
|
|
318
|
+
throw gateError(`Judge verdict is unsupported: expected ${expected.verdict}, received ${value.verdict}`);
|
|
319
|
+
}
|
|
320
|
+
const artifacts = { verdict: descriptor(rootDir, filePath) };
|
|
321
|
+
if (expected.verdict === 'PASS') {
|
|
322
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.JUDGED_PASS, { gate, artifacts });
|
|
323
|
+
}
|
|
324
|
+
if (expected.verdict === 'ESCALATE') {
|
|
325
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.ESCALATED, {
|
|
326
|
+
gate,
|
|
327
|
+
artifacts,
|
|
328
|
+
reason: expected.reason
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.GOAL_FROZEN, {
|
|
332
|
+
gate,
|
|
333
|
+
artifacts,
|
|
334
|
+
reason: expected.reason,
|
|
335
|
+
incrementIteration: true
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
assertArtifactCurrent(rootDir, run, 'goalContract');
|
|
340
|
+
assertArtifactCurrent(rootDir, run, 'diff');
|
|
341
|
+
assertArtifactCurrent(rootDir, run, 'verification');
|
|
342
|
+
assertArtifactCurrent(rootDir, run, 'findings');
|
|
343
|
+
assertArtifactCurrent(rootDir, run, 'verdict');
|
|
344
|
+
const { filePath, value } = readJsonArtifact(rootDir, run.runId, GATE_FILES.delivery);
|
|
345
|
+
requireRunMetadata(value, run, 'delivery');
|
|
346
|
+
const deliveryErrors = [];
|
|
347
|
+
if (!String(value.destination || '').trim()) deliveryErrors.push('destination is required');
|
|
348
|
+
if (!String(value.summary || '').trim()) deliveryErrors.push('summary is required');
|
|
349
|
+
if (mode === RUN_MODES.ASSISTED && value.humanApproved !== true) {
|
|
350
|
+
deliveryErrors.push('humanApproved must be true for ASSISTED delivery');
|
|
351
|
+
}
|
|
352
|
+
if (deliveryErrors.length) throw gateError('Delivery gate failed', deliveryErrors);
|
|
353
|
+
return transitionRun(rootDir, run.runId, RUN_STATES.DELIVERED, {
|
|
354
|
+
gate,
|
|
355
|
+
artifacts: { delivery: descriptor(rootDir, filePath) }
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
module.exports = {
|
|
360
|
+
GATE_FILES,
|
|
361
|
+
artifactPath,
|
|
362
|
+
hashContent,
|
|
363
|
+
hashFile,
|
|
364
|
+
assertArtifactCurrent,
|
|
365
|
+
validateGoalContract,
|
|
366
|
+
validateVerificationBundle,
|
|
367
|
+
validateVerdictDocument,
|
|
368
|
+
applyGate
|
|
369
|
+
};
|
package/lib/orchestration.js
CHANGED
|
@@ -442,7 +442,11 @@ function detectGrokRuntime(env = process.env, fsApi = fs) {
|
|
|
442
442
|
const grokBinCandidates = [
|
|
443
443
|
env.GROK_BIN,
|
|
444
444
|
path.join(grokHome, 'bin', 'grok'),
|
|
445
|
-
path.join(grokHome, 'bin', '
|
|
445
|
+
path.join(grokHome, 'bin', 'grok.exe'),
|
|
446
|
+
path.join(grokHome, 'bin', 'grok.cmd'),
|
|
447
|
+
path.join(grokHome, 'bin', 'agent'),
|
|
448
|
+
path.join(grokHome, 'bin', 'agent.exe'),
|
|
449
|
+
path.join(grokHome, 'bin', 'agent.cmd')
|
|
446
450
|
].filter(Boolean);
|
|
447
451
|
|
|
448
452
|
const grokBin = grokBinCandidates.find((candidate) => {
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const TEXT_EXTENSIONS = new Set(['.js', '.json', '.md', '.yml', '.yaml', '.txt']);
|
|
7
|
+
const LOCAL_FILE_URL = new RegExp(`file:${'/'.repeat(3)}`, 'i');
|
|
8
|
+
const MAC_HOME = /\/Users\/(?!\.\.\.\/|<[^>]+>\/)[a-z0-9._-]+\//i;
|
|
9
|
+
const LINUX_HOME = /\/home\/(?!\.\.\.\/|<[^>]+>\/)[a-z0-9._-]+\//i;
|
|
10
|
+
const WINDOWS_HOME = /[a-z]:\\Users\\(?!\.\.\.\\|<[^>]+>\\)[^\\\r\n]+\\/i;
|
|
11
|
+
const PRIVATE_KEY = /-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/;
|
|
12
|
+
|
|
13
|
+
const PRIVACY_PATTERNS = Object.freeze([
|
|
14
|
+
{ label: 'local file URL', pattern: LOCAL_FILE_URL },
|
|
15
|
+
{ label: 'macOS user home', pattern: MAC_HOME },
|
|
16
|
+
{ label: 'Linux user home', pattern: LINUX_HOME },
|
|
17
|
+
{ label: 'Windows user home', pattern: WINDOWS_HOME },
|
|
18
|
+
{ label: 'private key material', pattern: PRIVATE_KEY }
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
function walkFiles(rootDir, relativePath, files) {
|
|
22
|
+
const absolute = path.join(rootDir, relativePath);
|
|
23
|
+
if (!fs.existsSync(absolute)) return;
|
|
24
|
+
const stat = fs.lstatSync(absolute);
|
|
25
|
+
if (stat.isSymbolicLink()) return;
|
|
26
|
+
if (stat.isFile()) {
|
|
27
|
+
files.add(relativePath.split(path.sep).join('/'));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (!stat.isDirectory()) return;
|
|
31
|
+
for (const entry of fs.readdirSync(absolute)) {
|
|
32
|
+
walkFiles(rootDir, path.join(relativePath, entry), files);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function listPackageFiles(rootDir) {
|
|
37
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(rootDir, 'package.json'), 'utf8'));
|
|
38
|
+
const files = new Set(['package.json']);
|
|
39
|
+
for (const entry of pkg.files || []) {
|
|
40
|
+
walkFiles(rootDir, entry.replace(/\/$/, ''), files);
|
|
41
|
+
}
|
|
42
|
+
for (const entry of fs.readdirSync(rootDir)) {
|
|
43
|
+
if (/^(?:readme|license|licence|security|support)(?:\.|$)/i.test(entry)) {
|
|
44
|
+
walkFiles(rootDir, entry, files);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return [...files].sort();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function auditText(content, relativePath) {
|
|
51
|
+
const findings = [];
|
|
52
|
+
for (const { label, pattern } of PRIVACY_PATTERNS) {
|
|
53
|
+
if (pattern.test(content)) findings.push({ file: relativePath, reason: label });
|
|
54
|
+
}
|
|
55
|
+
return findings;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function auditPackage(rootDir) {
|
|
59
|
+
const files = listPackageFiles(rootDir);
|
|
60
|
+
const findings = [];
|
|
61
|
+
for (const relativePath of files) {
|
|
62
|
+
if (!TEXT_EXTENSIONS.has(path.extname(relativePath).toLowerCase())) continue;
|
|
63
|
+
const content = fs.readFileSync(path.join(rootDir, relativePath), 'utf8');
|
|
64
|
+
findings.push(...auditText(content, relativePath));
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
ok: findings.length === 0,
|
|
68
|
+
filesScanned: files.length,
|
|
69
|
+
findings
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = {
|
|
74
|
+
PRIVACY_PATTERNS,
|
|
75
|
+
listPackageFiles,
|
|
76
|
+
auditText,
|
|
77
|
+
auditPackage
|
|
78
|
+
};
|