auditor-lambda 0.6.9 → 0.6.11

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.
@@ -0,0 +1,166 @@
1
+ import { join } from "node:path";
2
+ import { isFileMissingError, readJsonFile, writeJsonFile } from "@audit-tools/shared";
3
+ import { loadArtifactBundle, writeCoreArtifacts, } from "../io/artifacts.js";
4
+ import { deriveAuditState } from "../orchestrator/state.js";
5
+ import { buildRunId, getRunPaths, writeWorkerTaskFiles, } from "../io/runArtifacts.js";
6
+ import { renderWorkerPrompt } from "../prompts/renderWorkerPrompt.js";
7
+ import { buildAuditCodeHandoff, writeAuditCodeHandoffArtifacts, } from "../supervisor/operatorHandoff.js";
8
+ import { LOCAL_SUBPROCESS_PROVIDER_NAME } from "../providers/constants.js";
9
+ import { addFileLineCountHints } from "./lineIndex.js";
10
+ import { buildPendingAuditTasks } from "./dispatch.js";
11
+ import { buildBlockedAuditState, buildManualReviewBlocker } from "./envelope.js";
12
+ export function activeReviewRunFromTask(artifactsDir, task) {
13
+ if (task.preferred_executor !== "agent" || !task.audit_results_path) {
14
+ return null;
15
+ }
16
+ const paths = getRunPaths(artifactsDir, task.run_id);
17
+ return {
18
+ run_id: task.run_id,
19
+ task_path: paths.taskPath,
20
+ prompt_path: paths.promptPath,
21
+ pending_audit_tasks_path: task.pending_audit_tasks_path,
22
+ audit_results_path: task.audit_results_path,
23
+ worker_command: task.worker_command,
24
+ };
25
+ }
26
+ export async function loadCurrentActiveReviewRun(artifactsDir) {
27
+ try {
28
+ const task = await readJsonFile(join(artifactsDir, "dispatch", "current-task.json"));
29
+ return activeReviewRunFromTask(artifactsDir, task);
30
+ }
31
+ catch (error) {
32
+ if (isFileMissingError(error)) {
33
+ return null;
34
+ }
35
+ throw error;
36
+ }
37
+ }
38
+ export async function writeHandoffOnly(params) {
39
+ const handoff = buildAuditCodeHandoff({
40
+ root: params.root,
41
+ artifactsDir: params.artifactsDir,
42
+ state: params.audit_state,
43
+ bundle: params.bundle,
44
+ providerName: params.providerName,
45
+ progressSummary: params.progress_summary,
46
+ isConfigError: params.isConfigError,
47
+ activeReviewRun: params.activeReviewRun,
48
+ });
49
+ await writeAuditCodeHandoffArtifacts(handoff);
50
+ }
51
+ export async function ensureSemanticReviewRun(params) {
52
+ const existingRun = await loadCurrentActiveReviewRun(params.artifactsDir);
53
+ if (existingRun) {
54
+ const blockedState = params.bundle.audit_state?.status === "blocked"
55
+ ? params.bundle.audit_state
56
+ : buildBlockedAuditState({
57
+ state: params.state,
58
+ obligationId: params.obligationId,
59
+ executor: "agent",
60
+ blocker: buildManualReviewBlocker(LOCAL_SUBPROCESS_PROVIDER_NAME),
61
+ });
62
+ const blockedBundle = { ...params.bundle, audit_state: blockedState };
63
+ await writeCoreArtifacts(params.artifactsDir, blockedBundle);
64
+ await writeHandoffOnly({
65
+ root: params.root,
66
+ artifactsDir: params.artifactsDir,
67
+ bundle: blockedBundle,
68
+ audit_state: blockedState,
69
+ progress_summary: buildManualReviewBlocker(LOCAL_SUBPROCESS_PROVIDER_NAME),
70
+ providerName: LOCAL_SUBPROCESS_PROVIDER_NAME,
71
+ activeReviewRun: existingRun,
72
+ });
73
+ return {
74
+ state: blockedState,
75
+ bundle: blockedBundle,
76
+ activeReviewRun: existingRun,
77
+ };
78
+ }
79
+ const blockedState = buildBlockedAuditState({
80
+ state: params.state,
81
+ obligationId: params.obligationId,
82
+ executor: "agent",
83
+ blocker: buildManualReviewBlocker(LOCAL_SUBPROCESS_PROVIDER_NAME),
84
+ });
85
+ await writeCoreArtifacts(params.artifactsDir, {
86
+ ...params.bundle,
87
+ audit_state: blockedState,
88
+ });
89
+ const runId = buildRunId(params.obligationId, 1);
90
+ const paths = getRunPaths(params.artifactsDir, runId);
91
+ const pendingTasks = await addFileLineCountHints(params.root, buildPendingAuditTasks(params.bundle));
92
+ const pendingTasksPath = join(paths.runDir, "pending-audit-tasks.json");
93
+ const auditResultsPath = join(paths.runDir, "audit-results.json");
94
+ const taskReadPaths = new Set();
95
+ for (const pt of pendingTasks) {
96
+ for (const fp of pt.file_paths)
97
+ taskReadPaths.add(fp);
98
+ }
99
+ const task = {
100
+ contract_version: "audit-code-worker/v1alpha1",
101
+ run_id: runId,
102
+ repo_root: params.root,
103
+ artifacts_dir: params.artifactsDir,
104
+ obligation_id: params.obligationId,
105
+ preferred_executor: "agent",
106
+ result_path: paths.resultPath,
107
+ worker_command: [
108
+ process.execPath,
109
+ params.selfCliPath,
110
+ "worker-run",
111
+ "--task",
112
+ paths.taskPath,
113
+ ],
114
+ audit_results_path: auditResultsPath,
115
+ pending_audit_tasks_path: pendingTasksPath,
116
+ timeout_ms: params.timeoutMs,
117
+ max_retries: 0,
118
+ access: {
119
+ read_paths: [...taskReadPaths],
120
+ write_paths: [auditResultsPath, paths.resultPath],
121
+ },
122
+ };
123
+ const prompt = renderWorkerPrompt(task);
124
+ await writeWorkerTaskFiles(task, prompt, paths, params.artifactsDir, pendingTasks);
125
+ await writeJsonFile(pendingTasksPath, pendingTasks);
126
+ const activeReviewRun = activeReviewRunFromTask(params.artifactsDir, task);
127
+ if (!activeReviewRun) {
128
+ throw new Error("Internal error: failed to materialize active review run.");
129
+ }
130
+ const blockedBundle = {
131
+ ...params.bundle,
132
+ audit_state: blockedState,
133
+ };
134
+ await writeHandoffOnly({
135
+ root: params.root,
136
+ artifactsDir: params.artifactsDir,
137
+ bundle: blockedBundle,
138
+ audit_state: blockedState,
139
+ progress_summary: buildManualReviewBlocker(LOCAL_SUBPROCESS_PROVIDER_NAME),
140
+ providerName: LOCAL_SUBPROCESS_PROVIDER_NAME,
141
+ activeReviewRun,
142
+ });
143
+ return { state: blockedState, bundle: blockedBundle, activeReviewRun };
144
+ }
145
+ export async function persistConfigErrorHandoff(params) {
146
+ const bundle = await loadArtifactBundle(params.artifactsDir);
147
+ const blockedState = buildBlockedAuditState({
148
+ state: bundle.audit_state ?? deriveAuditState(bundle),
149
+ obligationId: null,
150
+ executor: null,
151
+ blocker: params.progressSummary,
152
+ });
153
+ await writeCoreArtifacts(params.artifactsDir, {
154
+ ...bundle,
155
+ audit_state: blockedState,
156
+ });
157
+ const handoff = buildAuditCodeHandoff({
158
+ root: params.root,
159
+ artifactsDir: params.artifactsDir,
160
+ state: blockedState,
161
+ bundle: { ...bundle, audit_state: blockedState },
162
+ progressSummary: params.progressSummary,
163
+ isConfigError: true,
164
+ });
165
+ await writeAuditCodeHandoffArtifacts(handoff);
166
+ }
@@ -0,0 +1 @@
1
+ export declare function cmdRunToCompletion(argv: string[]): Promise<void>;