auditor-lambda 0.6.9 → 0.6.10

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/dist/cli.js CHANGED
@@ -1,6 +1,6 @@
1
- import { mkdir, readFile, readdir, rename, rm, unlink, writeFile } from "node:fs/promises";
1
+ import { mkdir, readFile, readdir, rm, unlink, writeFile } from "node:fs/promises";
2
2
  import { homedir } from "node:os";
3
- import { basename, dirname, join, resolve } from "node:path";
3
+ import { dirname, join, resolve } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { buildRepoManifest } from "./extractors/fileInventory.js";
6
6
  import { buildFileDisposition } from "./extractors/disposition.js";
@@ -11,7 +11,7 @@ import { buildFlowCoverage } from "./orchestrator/flowCoverage.js";
11
11
  import { buildRuntimeValidationTasks, } from "./orchestrator/runtimeValidation.js";
12
12
  import { initializeCoverageFromPlan } from "./orchestrator/planning.js";
13
13
  import { loadArtifactBundle, writeCoreArtifacts, promoteFinalAuditReport, AUDIT_REPORT_FILENAME, } from "./io/artifacts.js";
14
- import { isFileMissingError, readJsonFile, writeJsonFile, prefixValidationIssues, RunLogger } from "@audit-tools/shared";
14
+ import { isFileMissingError, readJsonFile, writeJsonFile, prefixValidationIssues } from "@audit-tools/shared";
15
15
  import { buildQuotaSource } from "@audit-tools/shared/quota/compositeQuotaSource";
16
16
  import { validateArtifactBundle } from "./validation/artifacts.js";
17
17
  import { validateAuditResults, formatAuditResultIssues, } from "./validation/auditResults.js";
@@ -46,208 +46,16 @@ import { WORKER_RESULT_CONTRACT_VERSION, buildWorkerResult, persistWorkerRunArti
46
46
  import { DISPATCH_RESULT_MAP_FILENAME, ACTIVE_DISPATCH_FILENAME, resolveRunScopedArg, loadDispatchResultMap, entriesByTaskId, buildPendingAuditTasks, prepareDispatchArtifacts, } from "./cli/dispatch.js";
47
47
  import { readWaveManifest, writeWaveManifest, removeWaveManifest, buildWaveSlotEntry, } from "./cli/waveManifest.js";
48
48
  import { buildLineIndex, buildLineIndexForPaths, addFileLineCountHints, } from "./cli/lineIndex.js";
49
+ import { emitEnvelope, buildBlockedAuditState, buildManualReviewBlocker, shouldRunInlineExecutor, } from "./cli/envelope.js";
50
+ import { writeHandoffOnly, ensureSemanticReviewRun, } from "./cli/reviewRun.js";
51
+ import { runAuditStep, ingestBatchAuditResults, } from "./cli/auditStep.js";
49
52
  const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
50
- const ADVANCE_AUDIT_CONTRACT_VERSION = "audit-code/v1alpha1";
51
53
  const SAMPLE_REPO_FILES = [
52
54
  { path: "src/api/auth.ts", size_bytes: 1240, hash: "abc123" },
53
55
  { path: "src/lib/session.ts", size_bytes: 980, hash: "def456" },
54
56
  { path: "infra/deploy.yml", size_bytes: 420, hash: "ghi789" },
55
57
  { path: "docs/notes.md", size_bytes: 300, hash: "doc111" },
56
58
  ];
57
- function buildEnvelope(params) {
58
- return {
59
- contract_version: ADVANCE_AUDIT_CONTRACT_VERSION,
60
- audit_state: params.audit_state,
61
- selected_obligation: params.selected_obligation,
62
- selected_executor: params.selected_executor,
63
- progress_made: params.progress_made,
64
- artifacts_written: params.artifacts_written,
65
- progress_summary: params.progress_summary,
66
- next_likely_step: params.next_likely_step,
67
- handoff: params.handoff,
68
- };
69
- }
70
- async function emitEnvelope(params) {
71
- const handoff = buildAuditCodeHandoff({
72
- root: params.root,
73
- artifactsDir: params.artifactsDir,
74
- state: params.audit_state,
75
- bundle: params.bundle,
76
- providerName: params.providerName,
77
- progressSummary: params.progress_summary,
78
- isConfigError: params.isConfigError,
79
- activeReviewRun: params.activeReviewRun,
80
- });
81
- await writeAuditCodeHandoffArtifacts(handoff);
82
- console.log(JSON.stringify(buildEnvelope({
83
- audit_state: params.audit_state,
84
- selected_obligation: params.selected_obligation,
85
- selected_executor: params.selected_executor,
86
- progress_made: params.progress_made,
87
- artifacts_written: params.artifacts_written,
88
- progress_summary: params.progress_summary,
89
- next_likely_step: params.next_likely_step,
90
- handoff,
91
- }), null, 2));
92
- }
93
- function buildManualReviewBlocker(providerName) {
94
- return providerName === LOCAL_SUBPROCESS_PROVIDER_NAME
95
- ? "Ready for LLM semantic review. If the host exposes a callable subagent tool, prepare dispatch and fan out packets. " +
96
- "If not, use single-task fallback: review only the first pending task, write one AuditResult to the run audit-results path, execute worker_command, then stop."
97
- : "Audit blocked: waiting for manual audit results or interactive provider configuration.";
98
- }
99
- function shouldRunInlineExecutor(selectedExecutor) {
100
- return selectedExecutor !== null && selectedExecutor !== "agent";
101
- }
102
- function buildBlockedAuditState(params) {
103
- return {
104
- ...params.state,
105
- status: "blocked",
106
- last_executor: params.executor ?? params.state.last_executor,
107
- last_obligation: params.obligationId ?? params.state.last_obligation,
108
- blockers: [...new Set([...(params.state.blockers ?? []), params.blocker])],
109
- obligations: params.state.obligations.map((item) => item.id === params.obligationId
110
- ? {
111
- ...item,
112
- state: "blocked",
113
- reason: params.blocker,
114
- }
115
- : item),
116
- };
117
- }
118
- function activeReviewRunFromTask(artifactsDir, task) {
119
- if (task.preferred_executor !== "agent" || !task.audit_results_path) {
120
- return null;
121
- }
122
- const paths = getRunPaths(artifactsDir, task.run_id);
123
- return {
124
- run_id: task.run_id,
125
- task_path: paths.taskPath,
126
- prompt_path: paths.promptPath,
127
- pending_audit_tasks_path: task.pending_audit_tasks_path,
128
- audit_results_path: task.audit_results_path,
129
- worker_command: task.worker_command,
130
- };
131
- }
132
- async function loadCurrentActiveReviewRun(artifactsDir) {
133
- try {
134
- const task = await readJsonFile(join(artifactsDir, "dispatch", "current-task.json"));
135
- return activeReviewRunFromTask(artifactsDir, task);
136
- }
137
- catch (error) {
138
- if (isFileMissingError(error)) {
139
- return null;
140
- }
141
- throw error;
142
- }
143
- }
144
- async function writeHandoffOnly(params) {
145
- const handoff = buildAuditCodeHandoff({
146
- root: params.root,
147
- artifactsDir: params.artifactsDir,
148
- state: params.audit_state,
149
- bundle: params.bundle,
150
- providerName: params.providerName,
151
- progressSummary: params.progress_summary,
152
- isConfigError: params.isConfigError,
153
- activeReviewRun: params.activeReviewRun,
154
- });
155
- await writeAuditCodeHandoffArtifacts(handoff);
156
- }
157
- async function ensureSemanticReviewRun(params) {
158
- const existingRun = await loadCurrentActiveReviewRun(params.artifactsDir);
159
- if (existingRun) {
160
- const blockedState = params.bundle.audit_state?.status === "blocked"
161
- ? params.bundle.audit_state
162
- : buildBlockedAuditState({
163
- state: params.state,
164
- obligationId: params.obligationId,
165
- executor: "agent",
166
- blocker: buildManualReviewBlocker(LOCAL_SUBPROCESS_PROVIDER_NAME),
167
- });
168
- const blockedBundle = { ...params.bundle, audit_state: blockedState };
169
- await writeCoreArtifacts(params.artifactsDir, blockedBundle);
170
- await writeHandoffOnly({
171
- root: params.root,
172
- artifactsDir: params.artifactsDir,
173
- bundle: blockedBundle,
174
- audit_state: blockedState,
175
- progress_summary: buildManualReviewBlocker(LOCAL_SUBPROCESS_PROVIDER_NAME),
176
- providerName: LOCAL_SUBPROCESS_PROVIDER_NAME,
177
- activeReviewRun: existingRun,
178
- });
179
- return {
180
- state: blockedState,
181
- bundle: blockedBundle,
182
- activeReviewRun: existingRun,
183
- };
184
- }
185
- const blockedState = buildBlockedAuditState({
186
- state: params.state,
187
- obligationId: params.obligationId,
188
- executor: "agent",
189
- blocker: buildManualReviewBlocker(LOCAL_SUBPROCESS_PROVIDER_NAME),
190
- });
191
- await writeCoreArtifacts(params.artifactsDir, {
192
- ...params.bundle,
193
- audit_state: blockedState,
194
- });
195
- const runId = buildRunId(params.obligationId, 1);
196
- const paths = getRunPaths(params.artifactsDir, runId);
197
- const pendingTasks = await addFileLineCountHints(params.root, buildPendingAuditTasks(params.bundle));
198
- const pendingTasksPath = join(paths.runDir, "pending-audit-tasks.json");
199
- const auditResultsPath = join(paths.runDir, "audit-results.json");
200
- const taskReadPaths = new Set();
201
- for (const pt of pendingTasks) {
202
- for (const fp of pt.file_paths)
203
- taskReadPaths.add(fp);
204
- }
205
- const task = {
206
- contract_version: "audit-code-worker/v1alpha1",
207
- run_id: runId,
208
- repo_root: params.root,
209
- artifacts_dir: params.artifactsDir,
210
- obligation_id: params.obligationId,
211
- preferred_executor: "agent",
212
- result_path: paths.resultPath,
213
- worker_command: [
214
- process.execPath,
215
- params.selfCliPath,
216
- "worker-run",
217
- "--task",
218
- paths.taskPath,
219
- ],
220
- audit_results_path: auditResultsPath,
221
- pending_audit_tasks_path: pendingTasksPath,
222
- timeout_ms: params.timeoutMs,
223
- max_retries: 0,
224
- access: {
225
- read_paths: [...taskReadPaths],
226
- write_paths: [auditResultsPath, paths.resultPath],
227
- },
228
- };
229
- const prompt = renderWorkerPrompt(task);
230
- await writeWorkerTaskFiles(task, prompt, paths, params.artifactsDir, pendingTasks);
231
- await writeJsonFile(pendingTasksPath, pendingTasks);
232
- const activeReviewRun = activeReviewRunFromTask(params.artifactsDir, task);
233
- if (!activeReviewRun) {
234
- throw new Error("Internal error: failed to materialize active review run.");
235
- }
236
- const blockedBundle = {
237
- ...params.bundle,
238
- audit_state: blockedState,
239
- };
240
- await writeHandoffOnly({
241
- root: params.root,
242
- artifactsDir: params.artifactsDir,
243
- bundle: blockedBundle,
244
- audit_state: blockedState,
245
- progress_summary: buildManualReviewBlocker(LOCAL_SUBPROCESS_PROVIDER_NAME),
246
- providerName: LOCAL_SUBPROCESS_PROVIDER_NAME,
247
- activeReviewRun,
248
- });
249
- return { state: blockedState, bundle: blockedBundle, activeReviewRun };
250
- }
251
59
  export const cliTestUtils = {
252
60
  defaults: DIRECT_CLI_DEFAULTS,
253
61
  getFlag,
@@ -265,127 +73,6 @@ export const cliTestUtils = {
265
73
  countLines,
266
74
  warnIfNotGitRepo,
267
75
  };
268
- async function maybeArchiveLegacyPendingResults(auditResultsPath) {
269
- if (!auditResultsPath || basename(auditResultsPath) !== "worker_results_pending.json") {
270
- return undefined;
271
- }
272
- const archivedPath = join(dirname(auditResultsPath), `worker_results_submitted_${new Date().toISOString().replace(/[:.]/g, "-")}.json`);
273
- try {
274
- await rename(auditResultsPath, archivedPath);
275
- return archivedPath;
276
- }
277
- catch (error) {
278
- process.stderr.write(`[audit-results cleanup] failed to archive ${auditResultsPath}: ${error instanceof Error ? error.message : String(error)}\n`);
279
- return undefined;
280
- }
281
- }
282
- async function runAuditStep(options) {
283
- const bundle = await loadArtifactBundle(options.artifactsDir);
284
- const runLogger = new RunLogger(join(options.artifactsDir, "run.log.jsonl"), {
285
- enabled: options.runLog ?? true,
286
- });
287
- const lineIndex = bundle.repo_manifest
288
- ? await buildLineIndex(options.root, bundle.repo_manifest)
289
- : undefined;
290
- const sizeIndex = bundle.repo_manifest
291
- ? sizeIndexFromManifest(bundle.repo_manifest)
292
- : undefined;
293
- if (looksLikeCliFlag(options.auditResultsPath)) {
294
- throw new Error(`Invalid audit results path '${options.auditResultsPath}'. This looks like a CLI flag rather than a file path.`);
295
- }
296
- const auditResults = options.auditResultsPath
297
- ? await readJsonFile(options.auditResultsPath)
298
- : undefined;
299
- if (auditResults !== undefined) {
300
- const issues = validateAuditResults(auditResults, bundle.audit_tasks ?? [], {
301
- lineIndex,
302
- });
303
- const errors = issues.filter((issue) => issue.severity === "error");
304
- const warnings = issues.filter((issue) => issue.severity === "warning");
305
- if (warnings.length > 0) {
306
- process.stderr.write(`audit-results validation: ${warnings.length} warning(s):\n` +
307
- formatAuditResultIssues(warnings) +
308
- "\n");
309
- }
310
- if (errors.length > 0) {
311
- throw new Error(formatAuditResultValidationError(errors));
312
- }
313
- }
314
- const runtimeValidationUpdates = options.runtimeUpdatesPath
315
- ? await readJsonFile(options.runtimeUpdatesPath)
316
- : undefined;
317
- const externalAnalyzerResults = options.externalAnalyzerPath
318
- ? await readJsonFile(options.externalAnalyzerPath)
319
- : undefined;
320
- const narrativeResults = options.narrativeResultsPath
321
- ? await readJsonFile(options.narrativeResultsPath)
322
- : undefined;
323
- const edgeReasoningResults = options.edgeReasoningResultsPath
324
- ? await readJsonFile(options.edgeReasoningResultsPath)
325
- : undefined;
326
- const result = await advanceAudit(bundle, {
327
- root: options.root,
328
- lineIndex,
329
- sizeIndex,
330
- auditResults: auditResults,
331
- runtimeValidationUpdates,
332
- externalAnalyzerResults,
333
- narrativeResults,
334
- edgeReasoningResults,
335
- analyzers: options.analyzers,
336
- graphLlmEdgeReasoning: options.graphLlmEdgeReasoning,
337
- since: options.since,
338
- preferredExecutor: options.preferredExecutor,
339
- opentoken: options.opentoken,
340
- runLogger,
341
- });
342
- await writeCoreArtifacts(options.artifactsDir, result.updated_bundle);
343
- const archivedPendingResults = await maybeArchiveLegacyPendingResults(options.auditResultsPath);
344
- if (archivedPendingResults) {
345
- result.progress_summary +=
346
- ` Archived legacy staging file to ${archivedPendingResults}.`;
347
- }
348
- return result;
349
- }
350
- async function ingestBatchAuditResults(options) {
351
- const batchFiles = await listBatchResultFiles(options.batchDir);
352
- const artifactsWritten = new Set();
353
- const progressSummaries = [];
354
- let lastStep = null;
355
- let anyProgress = false;
356
- for (const batchFile of batchFiles) {
357
- const step = await runAuditStep({
358
- root: options.root,
359
- artifactsDir: options.artifactsDir,
360
- preferredExecutor: "result_ingestion_executor",
361
- auditResultsPath: batchFile,
362
- });
363
- lastStep = step;
364
- anyProgress ||= step.progress_made;
365
- for (const artifact of step.artifacts_written) {
366
- artifactsWritten.add(artifact);
367
- }
368
- progressSummaries.push(`${basename(batchFile)}: ${step.progress_summary}`);
369
- }
370
- const bundle = lastStep?.updated_bundle ??
371
- (await loadArtifactBundle(options.artifactsDir));
372
- const state = deriveAuditState(bundle);
373
- const decision = decideNextStep(bundle);
374
- return {
375
- batchFiles,
376
- bundle,
377
- audit_state: state,
378
- selected_obligation: lastStep?.selected_obligation ?? decision.selected_obligation,
379
- selected_executor: lastStep?.selected_executor ?? "result_ingestion_executor",
380
- progress_made: anyProgress,
381
- artifacts_written: Array.from(artifactsWritten),
382
- progress_summary: `Imported ${batchFiles.length} batch result file${batchFiles.length === 1 ? "" : "s"} from ${options.batchDir}.` +
383
- (progressSummaries.length > 0
384
- ? `\n${progressSummaries.join("\n")}`
385
- : ""),
386
- next_likely_step: state.status === "complete" ? null : decision.selected_obligation,
387
- };
388
- }
389
76
  async function persistConfigErrorHandoff(params) {
390
77
  const bundle = await loadArtifactBundle(params.artifactsDir);
391
78
  const blockedState = buildBlockedAuditState({