@clear-capabilities/agentic-security-scanner 0.184.0 → 0.186.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.
Files changed (228) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/bin/agentic-security.js +111 -1
  3. package/dist/1128.index.js +912 -0
  4. package/dist/1665.index.js +828 -0
  5. package/dist/2584.index.js +1 -1
  6. package/dist/4171.index.js +2 -2
  7. package/dist/5752.index.js +466 -0
  8. package/dist/7405.index.js +1 -1
  9. package/dist/8158.index.js +1713 -0
  10. package/dist/8798.index.js +1 -1
  11. package/dist/9382.index.js +2 -2
  12. package/dist/9579.index.js +1 -1
  13. package/dist/agentic-security.mjs +29 -29
  14. package/dist/agentic-security.mjs.sha256 +1 -1
  15. package/dist/{dad58d1113a12c23a142.json → ca62ee93079be4c24eee.json} +29 -12
  16. package/dist/frontend/src/shell.js +12 -2
  17. package/package.json +29 -12
  18. package/src/dataflow/CLAUDE.md +46 -1
  19. package/src/dataflow/access-paths.js +12 -2
  20. package/src/dataflow/backward.js +42 -7
  21. package/src/dataflow/cross-service-taint.js +83 -1
  22. package/src/dataflow/demand-query.js +1168 -0
  23. package/src/dataflow/dep-fingerprint.js +718 -0
  24. package/src/dataflow/engine.js +48 -6
  25. package/src/dataflow/exploit-prover.js +6 -2
  26. package/src/dataflow/ifds-precise.js +22 -0
  27. package/src/dataflow/incremental-replay.js +118 -0
  28. package/src/dataflow/incremental-session.js +257 -0
  29. package/src/dataflow/incremental.js +237 -35
  30. package/src/dataflow/index.js +25 -94
  31. package/src/dataflow/ir-cache.js +268 -0
  32. package/src/dataflow/privacy-taint.js +7 -0
  33. package/src/dataflow/summaries.js +72 -6
  34. package/src/dataflow/tabulation.js +80 -24
  35. package/src/engine.js +113 -8
  36. package/src/graph/CLAUDE.md +31 -0
  37. package/src/graph/adapters/artifact.js +81 -0
  38. package/src/graph/adapters/crossrepo.js +75 -0
  39. package/src/graph/adapters/dataflow.js +76 -0
  40. package/src/graph/adapters/provenance.js +92 -0
  41. package/src/graph/adapters/semantic.js +89 -0
  42. package/src/graph/crosswalk-builders.js +152 -0
  43. package/src/graph/crosswalk.js +146 -0
  44. package/src/graph/index.js +15 -0
  45. package/src/graph/observation.js +57 -0
  46. package/src/graph/refs.js +105 -0
  47. package/src/graph/registry.js +129 -0
  48. package/src/graph/relations.js +81 -0
  49. package/src/graph/scc.js +241 -0
  50. package/src/graph/view.js +254 -0
  51. package/src/ir/CLAUDE.md +4 -0
  52. package/src/ir/callgraph.js +36 -8
  53. package/src/ir/index.js +18 -4
  54. package/src/ir/parser-js.js +10 -0
  55. package/src/language/CLAUDE.md +13 -0
  56. package/src/language/nix-option-types.js +170 -0
  57. package/src/language/nix-script-taint.js +46 -3
  58. package/src/language/nix-secrets.js +20 -2
  59. package/src/lineage/CLAUDE.md +72 -8
  60. package/src/lineage/DESIGN_GRAPH_BUILDER.md +1 -1
  61. package/src/lineage/DESIGN_PATH_PROVENANCE.md +6 -0
  62. package/src/lineage/boundary-contract.js +273 -0
  63. package/src/lineage/completion.js +205 -0
  64. package/src/lineage/continuation.js +140 -0
  65. package/src/lineage/control-credit.js +313 -0
  66. package/src/lineage/correlation.js +199 -0
  67. package/src/lineage/coverage.js +8 -1
  68. package/src/lineage/cross-repo-link.js +22 -0
  69. package/src/lineage/cut-planner.js +548 -0
  70. package/src/lineage/driver.js +219 -66
  71. package/src/lineage/engine.js +166 -21
  72. package/src/lineage/export-json.js +5 -0
  73. package/src/lineage/graph-builder.js +84 -7
  74. package/src/lineage/haskell-view.js +59 -0
  75. package/src/lineage/index.js +3 -0
  76. package/src/lineage/nix-view.js +35 -0
  77. package/src/lineage/owned-build.js +77 -0
  78. package/src/lineage/owned-worker-entry.js +22 -0
  79. package/src/lineage/owned-worker.js +95 -0
  80. package/src/lineage/path-evidence.js +123 -0
  81. package/src/lineage/path-predicates.js +529 -0
  82. package/src/lineage/path-query.js +189 -20
  83. package/src/lineage/path-store.js +191 -17
  84. package/src/lineage/scc-solver.js +186 -0
  85. package/src/lineage/smt-orchestrator.js +174 -0
  86. package/src/lineage/smt-z3-backend.js +294 -0
  87. package/src/lineage/source-seeding.js +36 -2
  88. package/src/lineage/summaries.js +66 -4
  89. package/src/lineage/value-versions.js +130 -0
  90. package/src/lineage/witness-selection.js +95 -0
  91. package/src/pipeline/scan-health.js +18 -0
  92. package/src/posture/CLAUDE.md +17 -0
  93. package/src/posture/business-logic.js +16 -7
  94. package/src/posture/dag-commands.js +442 -0
  95. package/src/posture/evidence-bundle.js +1 -1
  96. package/src/posture/finding-defaults.js +19 -0
  97. package/src/posture/git-history.js +91 -1
  98. package/src/posture/witness-bundle.js +573 -0
  99. package/src/posture/witness-runtime.js +276 -0
  100. package/src/report/index.js +2 -0
  101. package/src/sast/CLAUDE.md +2 -0
  102. package/src/sast/_call-args.js +70 -0
  103. package/src/sast/init-scope.js +87 -0
  104. package/src/sast/listen-bind-address.js +49 -0
  105. package/src/sast/redirect-target.js +106 -0
  106. package/src/sast/reflected-input.js +76 -0
  107. package/src/sast/send-file-root.js +27 -0
  108. package/src/sast/sensitive-data-logging.js +38 -1
  109. package/dist/119.index.js +0 -197
  110. package/dist/128.index.js +0 -129
  111. package/dist/1293.index.js +0 -5689
  112. package/dist/1336.index.js +0 -16182
  113. package/dist/1351.index.js +0 -133
  114. package/dist/1387.index.js +0 -271
  115. package/dist/1437.index.js +0 -23
  116. package/dist/1445.index.js +0 -110
  117. package/dist/1505.index.js +0 -151
  118. package/dist/1557.index.js +0 -293
  119. package/dist/1621.index.js +0 -311
  120. package/dist/1751.index.js +0 -160
  121. package/dist/1765.index.js +0 -727
  122. package/dist/1861.index.js +0 -690
  123. package/dist/1906.index.js +0 -433
  124. package/dist/208.index.js +0 -1631
  125. package/dist/2223.index.js +0 -159
  126. package/dist/2238.index.js +0 -295
  127. package/dist/2530.index.js +0 -152
  128. package/dist/2565.index.js +0 -116
  129. package/dist/2574.index.js +0 -127
  130. package/dist/2620.index.js +0 -345
  131. package/dist/2773.index.js +0 -98
  132. package/dist/2775.index.js +0 -32
  133. package/dist/2966.index.js +0 -171
  134. package/dist/2972.index.js +0 -87
  135. package/dist/2991.index.js +0 -131
  136. package/dist/3063.index.js +0 -833
  137. package/dist/3075.index.js +0 -227
  138. package/dist/3121.index.js +0 -297
  139. package/dist/3314.index.js +0 -263
  140. package/dist/3480.index.js +0 -1822
  141. package/dist/3481.index.js +0 -24
  142. package/dist/3506.index.js +0 -268
  143. package/dist/3533.index.js +0 -591
  144. package/dist/3653.index.js +0 -450
  145. package/dist/3740.index.js +0 -722
  146. package/dist/3822.index.js +0 -131
  147. package/dist/3868.index.js +0 -292
  148. package/dist/3918.index.js +0 -192
  149. package/dist/4090.index.js +0 -288
  150. package/dist/4113.index.js +0 -1286
  151. package/dist/4121.index.js +0 -1190
  152. package/dist/4142.index.js +0 -690
  153. package/dist/4168.index.js +0 -509
  154. package/dist/4281.index.js +0 -993
  155. package/dist/4336.index.js +0 -436
  156. package/dist/4387.index.js +0 -532
  157. package/dist/4413.index.js +0 -302
  158. package/dist/4476.index.js +0 -4510
  159. package/dist/4491.index.js +0 -133
  160. package/dist/4538.index.js +0 -185
  161. package/dist/4684.index.js +0 -319
  162. package/dist/4740.index.js +0 -165
  163. package/dist/4780.index.js +0 -209
  164. package/dist/4802.index.js +0 -225
  165. package/dist/4812.index.js +0 -379
  166. package/dist/4835.index.js +0 -126
  167. package/dist/494.index.js +0 -226
  168. package/dist/4991.index.js +0 -549
  169. package/dist/5199.index.js +0 -305
  170. package/dist/5216.index.js +0 -109
  171. package/dist/5439.index.js +0 -301
  172. package/dist/5443.index.js +0 -97
  173. package/dist/5496.index.js +0 -377
  174. package/dist/5738.index.js +0 -240
  175. package/dist/5801.index.js +0 -588
  176. package/dist/5817.index.js +0 -714
  177. package/dist/5858.index.js +0 -223
  178. package/dist/5933.index.js +0 -143
  179. package/dist/601.index.js +0 -23
  180. package/dist/6173.index.js +0 -273
  181. package/dist/6291.index.js +0 -1871
  182. package/dist/6312.index.js +0 -666
  183. package/dist/634.index.js +0 -16182
  184. package/dist/644.index.js +0 -87
  185. package/dist/6508.index.js +0 -283
  186. package/dist/6560.index.js +0 -645
  187. package/dist/6576.index.js +0 -422
  188. package/dist/6579.index.js +0 -271
  189. package/dist/6655.index.js +0 -3221
  190. package/dist/6745.index.js +0 -305
  191. package/dist/6818.index.js +0 -21642
  192. package/dist/6932.index.js +0 -1190
  193. package/dist/6972.index.js +0 -167
  194. package/dist/6995.index.js +0 -163
  195. package/dist/7249.index.js +0 -191
  196. package/dist/7372.index.js +0 -1197
  197. package/dist/7546.index.js +0 -541
  198. package/dist/76.index.js +0 -448
  199. package/dist/7879.index.js +0 -163
  200. package/dist/8025.index.js +0 -130
  201. package/dist/8054.index.js +0 -248
  202. package/dist/808.index.js +0 -1330
  203. package/dist/8097.index.js +0 -201
  204. package/dist/8126.index.js +0 -191
  205. package/dist/8178.index.js +0 -78
  206. package/dist/8382.index.js +0 -93
  207. package/dist/8408.index.js +0 -261
  208. package/dist/8540.index.js +0 -174
  209. package/dist/8635.index.js +0 -802
  210. package/dist/8688.index.js +0 -410
  211. package/dist/8699.index.js +0 -779
  212. package/dist/8891.index.js +0 -457
  213. package/dist/8916.index.js +0 -885
  214. package/dist/9146.index.js +0 -170
  215. package/dist/9168.index.js +0 -841
  216. package/dist/9292.index.js +0 -369
  217. package/dist/9422.index.js +0 -866
  218. package/dist/9440.index.js +0 -453
  219. package/dist/9479.index.js +0 -152
  220. package/dist/9481.index.js +0 -220
  221. package/dist/9539.index.js +0 -100
  222. package/dist/9547.index.js +0 -103
  223. package/dist/9692.index.js +0 -90
  224. package/dist/9800.index.js +0 -462
  225. package/dist/9899.index.js +0 -138
  226. package/dist/9981.index.js +0 -206
  227. package/dist/999.index.js +0 -117
  228. package/dist/9999.index.js +0 -1698
@@ -0,0 +1,442 @@
1
+ // dag-commands.js: explicit, opt-in CLI entry points for the DAG-PRD capabilities that used to be library-only.
2
+ //
3
+ // dataflow view typed graph view over the signed DataFlowGraph (DAG-06) plus SCC condensation (DAG-01)
4
+ // dataflow contracts boundary contracts bound to parser-resolved sites, consumer seeding (DAG-08)
5
+ // dataflow sink-query demand-driven reverse taint for one sink site (DAG-02)
6
+ // dataflow plan-cuts its completion contract (the command itself lives in bin/agentic-security.js) (DAG-09)
7
+ // witness build|verify signed, replayable path witness bundles (DAG-10)
8
+ //
9
+ // Rules every command here follows:
10
+ // * Nothing runs unless the user types the subcommand. A default scan never imports this module, so a default scan's
11
+ // output is unchanged.
12
+ // * Every report carries a completion record with the shared vocabulary complete | partial | cancelled | unavailable |
13
+ // invalid. "No path found" is never reported as "no vulnerability": `negativeClaimAllowed` is false unless the
14
+ // completion envelope proves an exhausted query over a complete model, and even then it is scoped to the modeled graph.
15
+ // * Exit codes: 0 a report was produced (read its completion status), 1 nothing usable could be produced (unavailable or
16
+ // invalid input data, a failed verification), 2 a usage error.
17
+ //
18
+ // Everything is read-only with respect to scan state. The only writes are the files the user names with --output / --out.
19
+
20
+ import * as fs from 'node:fs';
21
+ import * as fsp from 'node:fs/promises';
22
+ import * as path from 'node:path';
23
+ import { makeCompletion, completionOfGraph } from '../lineage/completion.js';
24
+
25
+ const err = (m) => process.stderr.write(`${m}\n`);
26
+
27
+ /** Write a JSON report to --output, or stdout. Returns an exit code on failure, null on success. */
28
+ async function emit(who, args, text) {
29
+ const out = args.flags.output;
30
+ if (out !== undefined && (typeof out !== 'string' || !out)) { err(`${who}: --output needs a path.`); return 2; }
31
+ if (!out) { process.stdout.write(text); return null; }
32
+ try {
33
+ await fsp.mkdir(path.dirname(path.resolve(out)), { recursive: true });
34
+ await fsp.writeFile(path.resolve(out), text);
35
+ } catch (e) { err(`${who}: could not write --output: ${e.message}`); return 2; }
36
+ return null;
37
+ }
38
+
39
+ function describe(who, completion) {
40
+ if (completion.status === 'complete') return;
41
+ err(`${who}: completion is ${completion.status}${completion.reasons.length ? ` (${completion.reasons.join(', ')})` : ''}. This is a statement about the modeled scope, not about the software.`);
42
+ }
43
+
44
+ const asInt = (v) => (v === undefined ? undefined : (/^\d+$/.test(String(v)) ? Number(v) : NaN));
45
+
46
+ // ---------------------------------------------------------------------------------------------------------------------
47
+ // plan-cuts completion contract (DAG-09)
48
+ // ---------------------------------------------------------------------------------------------------------------------
49
+
50
+ /**
51
+ * Completion contract for a cut plan. A plan is `complete` only when a finite cut exists AND the graph it was computed over is
52
+ * known complete. `no-flow` is `partial`: no modeled path between the selected sources and sinks is a statement about the
53
+ * model, never evidence of safety. A placement is hypothetical, so a negative claim is never allowed here.
54
+ */
55
+ export function planContract(plan) {
56
+ const reasons = [];
57
+ if (plan.status !== 'complete-cut') reasons.push(`plan-${plan.status}`);
58
+ if (plan.status === 'no-flow') reasons.push('no-modeled-path');
59
+ for (const r of plan.completeness?.reasons ?? []) reasons.push(r.replace(/[^\w ,.:()-]/g, '').slice(0, 120));
60
+ const complete = plan.status === 'complete-cut' && plan.completeness?.complete === true && reasons.length === 0;
61
+ const c = makeCompletion({ status: complete ? 'complete' : 'partial', modelCoverage: plan.completeness?.complete ? 'complete' : 'partial', reasons, unit: 'cut-plan' });
62
+ return { ...c, negativeClaimAllowed: false, hypothetical: true, verified: false };
63
+ }
64
+
65
+ // ---------------------------------------------------------------------------------------------------------------------
66
+ // shared project loading (source -> IR -> call graph) for the commands that analyse code
67
+ // ---------------------------------------------------------------------------------------------------------------------
68
+
69
+ async function loadProgram(root) {
70
+ const { readTree } = await import('../runScan.js');
71
+ const { buildProjectIR } = await import('../ir/index.js');
72
+ const tree = await readTree(root);
73
+ const ir = buildProjectIR(tree.fileContents);
74
+ return { fileContents: tree.fileContents, oversize: tree.oversize || [], perFile: ir.perFile, callGraph: ir.callGraph, cha: ir.cha };
75
+ }
76
+
77
+ // ---------------------------------------------------------------------------------------------------------------------
78
+ // dataflow view
79
+ // ---------------------------------------------------------------------------------------------------------------------
80
+
81
+ /**
82
+ * agentic-security dataflow view [path] [--node <id> [--direction in|out|both] [--relations a,b] [--origins a,b]] [--output <file>]
83
+ * Read-only over the signed lineage graph. Without --node it reports the view identity, counts, completion and the SCC
84
+ * condensation of the data-flow relation. With --node it lists the neighbours the filter allows.
85
+ */
86
+ export async function cmdDataflowView(args) {
87
+ const who = 'agentic-security dataflow view';
88
+ const root = path.resolve(args._[2] || '.');
89
+ const f = args.flags;
90
+ for (const k of ['node', 'direction', 'relations', 'origins']) {
91
+ if (f[k] !== undefined && (typeof f[k] !== 'string' || !f[k])) { err(`${who}: --${k} needs a value.`); return 2; }
92
+ }
93
+ const direction = f.direction || 'both';
94
+ if (!['in', 'out', 'both'].includes(direction)) { err(`${who}: --direction must be in, out or both.`); return 2; }
95
+ if (f.node === undefined && (f.direction || f.relations || f.origins)) { err(`${who}: --direction, --relations and --origins apply to --node.`); return 2; }
96
+
97
+ const { loadSignedGraph } = await import('../server/graph-loader.js');
98
+ const loaded = loadSignedGraph(root);
99
+ if (!loaded.ok) { err(`${who}: ${loaded.message}`); return 1; }
100
+ const { dataflowView, GraphViewError } = await import('../graph/index.js');
101
+ const { computeScc, verifyScc } = await import('../graph/scc.js');
102
+
103
+ let view;
104
+ try { view = dataflowView(loaded.graph); } catch (e) { err(`${who}: the graph could not be viewed: ${e.message}`); return 1; }
105
+
106
+ const report = {
107
+ schema: 'agentic-security/graph-view-report@1',
108
+ maturity: 'preview',
109
+ identity: view.identity,
110
+ metadata: view.metadata(),
111
+ viewCompletion: view.completion(),
112
+ note: 'A view is a read-only lens over the signed graph. An absent edge means the model has no such edge, not that the software has none.',
113
+ };
114
+ const completion = completionOfGraph(loaded.graph);
115
+
116
+ if (f.node === undefined) {
117
+ const nodeIds = view.entities('node').map((e) => e.localId);
118
+ const edges = view.edges().filter((e) => e.relation === 'data_flow' && e.origin !== 'observed' && e.origin !== 'declared');
119
+ const pairs = edges.map((e) => [e.from.localId, e.to.localId]);
120
+ const known = new Set(nodeIds);
121
+ const scc = computeScc({ nodes: nodeIds, edges: pairs.filter(([a, b]) => known.has(a) && known.has(b)) });
122
+ const verify = verifyScc({ nodes: nodeIds, edges: pairs.filter(([a, b]) => known.has(a) && known.has(b)) }, scc);
123
+ const recursive = scc.components.filter((c) => c.recursive);
124
+ report.condensation = {
125
+ relation: 'data_flow',
126
+ origins: ['code', 'derived', 'schema'],
127
+ nodeCount: nodeIds.length,
128
+ componentCount: scc.components.length,
129
+ cyclicComponentCount: recursive.length,
130
+ cyclicComponents: recursive.slice(0, 20).map((c) => c.members),
131
+ isAcyclic: recursive.length === 0,
132
+ condensationEdgeCount: scc.condensationEdges.length,
133
+ verified: verify.ok === true,
134
+ verificationProblems: verify.problems.slice(0, 5),
135
+ note: 'The raw graph is preserved. The condensation is a derived view, acyclic by construction; a cyclic component is reported, never removed.',
136
+ };
137
+ } else {
138
+ const filter = { relations: f.relations ? f.relations.split(',').map((s) => s.trim()).filter(Boolean) : undefined, origins: f.origins ? f.origins.split(',').map((s) => s.trim()).filter(Boolean) : undefined };
139
+ try {
140
+ const ref = view.ref('node', f.node);
141
+ const lite = (e) => ({ id: e.id, relation: e.relation, origin: e.origin, from: e.from.localId, to: e.to.localId, evidence: e.evidence, unknowns: e.unknowns });
142
+ report.query = {
143
+ node: view.node(ref).summary ?? null, nodeId: f.node, direction, filter,
144
+ incoming: direction === 'out' ? undefined : view.incoming(ref, filter).map(lite),
145
+ outgoing: direction === 'in' ? undefined : view.outgoing(ref, filter).map(lite),
146
+ };
147
+ } catch (e) {
148
+ if (e instanceof GraphViewError) {
149
+ report.completion = makeCompletion({ status: 'invalid', reasons: [`graph-view-${String(e.code).toLowerCase()}`], unit: 'graph-view' });
150
+ const code = await emit(who, args, JSON.stringify(report, null, 2) + '\n');
151
+ err(`${who}: ${e.message}`);
152
+ return code ?? 1;
153
+ }
154
+ throw e;
155
+ }
156
+ }
157
+ report.completion = { ...completion, negativeClaimAllowed: false };
158
+ const code = await emit(who, args, JSON.stringify(report, null, 2) + '\n');
159
+ if (code !== null) return code;
160
+ describe(who, completion);
161
+ return 0;
162
+ }
163
+
164
+ // ---------------------------------------------------------------------------------------------------------------------
165
+ // dataflow contracts
166
+ // ---------------------------------------------------------------------------------------------------------------------
167
+
168
+ /**
169
+ * agentic-security dataflow contracts [path] [--contracts <file>] [--output <file>]
170
+ * Binds boundary contracts to parser-resolved sites and runs the taint engine with the consumer fields seeded. A declaration
171
+ * is a MODELED input: every attribution carries modeledInput:true, observed:false, exploited:false. A contract that does not
172
+ * bind becomes a coverage gap and never a finding.
173
+ */
174
+ export async function cmdDataflowContracts(args) {
175
+ const who = 'agentic-security dataflow contracts';
176
+ const root = path.resolve(args._[2] || '.');
177
+ const f = args.flags;
178
+ if (f.contracts !== undefined && (typeof f.contracts !== 'string' || !f.contracts)) { err(`${who}: --contracts needs a path.`); return 2; }
179
+ const { BOUNDARY_CONTRACTS_FILENAME, validateBoundaryContract, analyzeServiceBoundaries } = await import('../lineage/boundary-contract.js');
180
+ const { statePath } = await import('./state-dir.js');
181
+
182
+ const file = f.contracts ? path.resolve(f.contracts) : statePath(root, BOUNDARY_CONTRACTS_FILENAME);
183
+ let text;
184
+ try { text = fs.readFileSync(file, 'utf8'); } catch {
185
+ const completion = makeCompletion({ status: 'unavailable', reasons: ['no-contracts-file'], unit: 'boundary-contracts' });
186
+ const code = await emit(who, args, JSON.stringify({ schema: 'agentic-security/boundary-contracts-report@1', maturity: 'preview', contractsFile: file, completion: { ...completion, negativeClaimAllowed: false }, findings: [], note: 'No contracts were read, so nothing was analysed. This says nothing about the software.' }, null, 2) + '\n');
187
+ err(`${who}: no contracts file at ${file}. Pass --contracts <file> or write ${BOUNDARY_CONTRACTS_FILENAME} under the project state directory.`);
188
+ return code ?? 1;
189
+ }
190
+ let contracts;
191
+ try {
192
+ const doc = JSON.parse(text);
193
+ contracts = Array.isArray(doc) ? doc : Array.isArray(doc && doc.contracts) ? doc.contracts : null;
194
+ if (!contracts) throw new Error('expected a JSON array of contracts or { "contracts": [...] }');
195
+ } catch (e) { err(`${who}: ${file} is not a usable contracts document: ${e.message}`); return 2; }
196
+
197
+ const program = await loadProgram(root);
198
+ const { runBoundaryTaint } = await import('../dataflow/cross-service-taint.js');
199
+ const res = runBoundaryTaint(program.perFile, program.callGraph, contracts, { scanRoot: root, engine: { _cha: program.cha || undefined } });
200
+
201
+ const reasons = [];
202
+ if (res.rejected.length) reasons.push('rejected-contracts');
203
+ if (res.gaps.length) reasons.push('unbound-boundary');
204
+ if (res.coverage.truncated) reasons.push('engine-run-budget');
205
+ if (program.oversize.length) reasons.push('oversize-source-skipped');
206
+ if (contracts.length === 0) reasons.push('no-contracts');
207
+ const status = contracts.length === 0 || (res.rejected.length === contracts.length) ? 'unavailable' : reasons.length ? 'partial' : 'complete';
208
+ const completion = { ...makeCompletion({ status, reasons, unit: 'boundary-contracts' }), negativeClaimAllowed: false };
209
+
210
+ const valid = contracts.filter((c) => validateBoundaryContract(c).valid);
211
+ const report = {
212
+ schema: 'agentic-security/boundary-contracts-report@1',
213
+ maturity: 'preview',
214
+ contractsFile: file,
215
+ completion,
216
+ coverage: res.coverage,
217
+ bindings: res.bindings.map((b) => ({ contractId: b.contractId, kind: b.kind, persistence: b.persistence, channel: b.channel, file: b.file, line: b.line, param: b.param, seeds: b.seeds, mappedFields: b.mappedFields, evidence: b.evidence })),
218
+ gaps: res.gaps,
219
+ rejected: res.rejected,
220
+ findings: res.findings.map((x) => ({ id: x.id, severity: x.severity, file: x.file, line: x.line, vuln: x.vuln, cwe: x.cwe, boundary: x.boundary })),
221
+ services: analyzeServiceBoundaries(valid),
222
+ notes: [
223
+ 'A contract declaration establishes a modeled untrusted input at the consumer. It does not establish that any value was observed or that the sink is exploited.',
224
+ 'No finding does not mean the consumer is safe: only the declared fields were seeded, only the modeled sinks were checked.',
225
+ ],
226
+ };
227
+ const code = await emit(who, args, JSON.stringify(report, null, 2) + '\n');
228
+ if (code !== null) return code;
229
+ describe(who, completion);
230
+ return status === 'unavailable' ? 1 : 0;
231
+ }
232
+
233
+ // ---------------------------------------------------------------------------------------------------------------------
234
+ // dataflow sink-query
235
+ // ---------------------------------------------------------------------------------------------------------------------
236
+
237
+ /**
238
+ * agentic-security dataflow sink-query [path] (--list | --file <rel> --line <n>) [--mode demand|full|compare]
239
+ * [--arg-index <n>] [--sink-id <id>] [--access-path <p>] [--rule-family <cwe|family>] [--max-work <n>] [--max-caller-depth <n>]
240
+ * [--on-limit full|partial] [--output <file>]
241
+ * Answers "can attacker data reach this sink?" by selecting the backward demand region and running the ordinary forward taint
242
+ * engine over it. The only witness is a finding the forward engine produced. Verdicts: flow-found, no-flow-in-modeled-scope
243
+ * (only when the completion envelope allows a negative claim) and unknown. There is no "safe".
244
+ */
245
+ export async function cmdDataflowSinkQuery(args) {
246
+ const who = 'agentic-security dataflow sink-query';
247
+ const root = path.resolve(args._[2] || '.');
248
+ const f = args.flags;
249
+ const mode = f.mode || 'demand';
250
+ if (!['demand', 'full', 'compare'].includes(mode)) { err(`${who}: --mode must be demand, full or compare.`); return 2; }
251
+ for (const k of ['file', 'sink-id', 'access-path', 'rule-family']) {
252
+ if (f[k] !== undefined && (typeof f[k] !== 'string' || !f[k])) { err(`${who}: --${k} needs a value.`); return 2; }
253
+ }
254
+ const nums = { line: asInt(f.line), argIndex: asInt(f['arg-index']), maxWork: asInt(f['max-work']), maxCallerDepth: asInt(f['max-caller-depth']) };
255
+ for (const [k, v] of Object.entries(nums)) if (Number.isNaN(v)) { err(`${who}: --${k.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`)} must be a non-negative integer.`); return 2; }
256
+ const onLimit = f['on-limit'] || 'full';
257
+ if (!['full', 'partial'].includes(onLimit)) { err(`${who}: --on-limit must be full or partial.`); return 2; }
258
+ if (!f.list && (!f.file || nums.line === undefined)) { err(`Usage: ${who} [path] (--list | --file <rel> --line <n>) [--mode demand|full|compare] [--arg-index <n>] [--sink-id <id>] [--access-path <p>] [--rule-family <f>] [--max-work <n>] [--max-caller-depth <n>] [--on-limit full|partial] [--output <file>]`); return 2; }
259
+
260
+ const program = await loadProgram(root);
261
+ const dq = await import('../dataflow/demand-query.js');
262
+ if (!program.callGraph || !program.callGraph.functions || program.callGraph.functions.size === 0) {
263
+ const completion = { ...makeCompletion({ status: 'unavailable', reasons: ['no-call-graph'], unit: 'demand-query' }), negativeClaimAllowed: false };
264
+ const code = await emit(who, args, JSON.stringify({ schema: 'agentic-security/sink-query-report@1', maturity: 'preview', completion, verdict: 'unknown', note: 'No analyzable functions were found, so nothing was queried.' }, null, 2) + '\n');
265
+ err(`${who}: no analyzable functions under ${root}.`);
266
+ return code ?? 1;
267
+ }
268
+ if (f.list) {
269
+ const sites = dq.enumerateSinkQueries(program.callGraph);
270
+ const code = await emit(who, args, JSON.stringify({ schema: 'agentic-security/sink-query-report@1', maturity: 'preview', sites, count: sites.length, note: 'Sink sites the call graph and catalog recognise. A site that is not listed may still be a sink the catalog does not model.' }, null, 2) + '\n');
271
+ return code ?? 0;
272
+ }
273
+ const query = { file: f.file.replace(/^\.\//, ''), line: nums.line, argIndex: nums.argIndex ?? null, sinkId: f['sink-id'] ?? null, accessPath: f['access-path'] ?? null, ruleFamily: f['rule-family'] ?? null };
274
+ const opts = { cache: false, onLimit, engineOpts: { _cha: program.cha || undefined } };
275
+ if (nums.maxWork !== undefined) opts.maxWork = nums.maxWork;
276
+ if (nums.maxCallerDepth !== undefined) opts.maxCallerDepth = nums.maxCallerDepth;
277
+
278
+ const result = {};
279
+ if (mode === 'demand' || mode === 'compare') result.demand = dq.runDemandSinkQuery(program.perFile, program.callGraph, query, opts);
280
+ if (mode === 'full' || mode === 'compare') result.full = dq.runFullSinkQuery(program.perFile, program.callGraph, query, opts);
281
+ if (mode === 'compare') result.comparison = dq.compareToFull(result.demand, result.full);
282
+ const primary = result.demand || result.full;
283
+ // The full-set run has no frontier, so its completion is the engine's own incomplete list.
284
+ const completion = result.demand
285
+ ? { ...result.demand.completion, negativeClaimAllowed: result.demand.negativeClaimAllowed === true }
286
+ : { ...makeCompletion({ status: primary.sites.length === 0 ? 'unavailable' : (primary.engineIncomplete || []).length ? 'partial' : 'complete', reasons: primary.sites.length === 0 ? ['sink-site-not-found'] : (primary.engineIncomplete || []).map((r) => `engine:${r}`), unit: 'full-sink-query' }), negativeClaimAllowed: false };
287
+ const report = {
288
+ schema: 'agentic-security/sink-query-report@1',
289
+ maturity: 'preview',
290
+ optIn: 'explicit subcommand; a default scan never runs this',
291
+ mode,
292
+ query,
293
+ completion,
294
+ verdict: result.demand ? result.demand.verdict : (primary.findings.length ? 'flow-found' : 'unknown'),
295
+ ...result,
296
+ notes: [
297
+ 'A backward and forward intersection is a candidate filter only. The witness is a finding the forward engine produced over the selected region.',
298
+ 'no-flow-in-modeled-scope is scoped to the modeled call graph. It is never a statement that the sink is safe.',
299
+ ],
300
+ };
301
+ const code = await emit(who, args, JSON.stringify(report, null, 2) + '\n');
302
+ if (code !== null) return code;
303
+ describe(who, completion);
304
+ return completion.status === 'unavailable' ? 1 : 0;
305
+ }
306
+
307
+ // ---------------------------------------------------------------------------------------------------------------------
308
+ // witness build | verify
309
+ // ---------------------------------------------------------------------------------------------------------------------
310
+
311
+ function insideRoot(root, rel) {
312
+ const abs = path.resolve(root, rel);
313
+ return abs === root || abs.startsWith(root + path.sep) ? abs : null;
314
+ }
315
+
316
+ /**
317
+ * agentic-security witness build [path] --out <dir> [--sink <pnodeId>] [--list-sinks]
318
+ * Re-runs the field-identity analysis over the project (the hop stream is not persisted by a normal scan), reconstructs a
319
+ * bounded path, and writes <dir>/witness.json (signed with the install's Ed25519 attestation key) plus the artifacts replay
320
+ * needs: <dir>/artifacts/graph.json and <dir>/artifacts/provenance.json. Source blobs are bound by digest and re-read from
321
+ * the project at verify time; they are not copied.
322
+ */
323
+ async function witnessBuild(args) {
324
+ const who = 'agentic-security witness build';
325
+ const root = path.resolve(args._[2] || '.');
326
+ const f = args.flags;
327
+ for (const k of ['out', 'sink']) if (f[k] !== undefined && (typeof f[k] !== 'string' || !f[k])) { err(`${who}: --${k} needs a value.`); return 2; }
328
+ if (!f.out && !f['list-sinks']) { err(`Usage: ${who} [path] --out <dir> [--sink <pnodeId>] | --list-sinks`); return 2; }
329
+
330
+ const program = await loadProgram(root);
331
+ const { buildDataFlowGraph } = await import('../lineage/graph-builder.js');
332
+ const { sinkCandidates, reconstructPaths } = await import('../lineage/path-query.js');
333
+ const { buildWitnessBundle, signWitnessBundle, exportProvenanceArtifact } = await import('./witness-bundle.js');
334
+ if (!program.callGraph || !program.callGraph.functions || program.callGraph.functions.size === 0) {
335
+ err(`${who}: unavailable: no analyzable functions under ${root}. Nothing was analysed, which says nothing about the software.`);
336
+ return 1;
337
+ }
338
+ const built = buildDataFlowGraph(program.callGraph, { repository: path.basename(root) || 'repo' });
339
+ const { graph, store, hops } = built;
340
+ const incomplete = built.analysisCompletion?.incomplete || [];
341
+ const provenance = exportProvenanceArtifact({ hops, incomplete });
342
+ const sinks = sinkCandidates(store).filter((n) => n.kind === 'escape');
343
+
344
+ if (f['list-sinks']) {
345
+ const code = await emit(who, args, JSON.stringify({ schema: 'agentic-security/witness-sinks@1', maturity: 'preview', sinks: sinks.map((n) => ({ id: n.id, kind: n.kind, scope: n.scope, path: n.path ?? null })) }, null, 2) + '\n');
346
+ return code ?? 0;
347
+ }
348
+ const pool = f.sink ? sinks.filter((n) => n.id === f.sink) : sinks;
349
+ if (f.sink && pool.length === 0) { err(`${who}: invalid: ${f.sink} is not a sink candidate (use --list-sinks).`); return 2; }
350
+
351
+ let chosen = null;
352
+ for (const s of pool) {
353
+ const result = reconstructPaths(store, s.id, {});
354
+ const withEdges = (result.paths || []).filter((p) => (p.edgeIds || []).length > 0);
355
+ const path0 = withEdges.find((p) => p.crossScopeCount > 0) || withEdges[0];
356
+ if (path0) { chosen = { result, path: path0, sink: s }; break; }
357
+ }
358
+ if (!chosen) {
359
+ err(`${who}: unavailable: the analysis found no modeled path to witness (${sinks.length} sink candidate(s)). This is a statement about the model, not evidence that the software is safe.`);
360
+ return 1;
361
+ }
362
+ const sourceBlobs = {};
363
+ for (const [file, content] of Object.entries(program.fileContents)) sourceBlobs[file] = content;
364
+ const b = buildWitnessBundle({ result: chosen.result, path: chosen.path, store, graph, provenance, sourceBlobs });
365
+ if (!b.ok) { err(`${who}: invalid: ${b.reason}`); return 1; }
366
+
367
+ const { ensureKeyPair } = await import('./evidence-bundle.js');
368
+ const kp = ensureKeyPair();
369
+ if (kp.created) err(`Generated a new signing key at ${kp.privateKey} (public: ${kp.publicKey}).`);
370
+ const signed = signWitnessBundle(b.bundle, kp.privateKeyPem);
371
+
372
+ const outDir = path.resolve(f.out);
373
+ try {
374
+ fs.mkdirSync(path.join(outDir, 'artifacts'), { recursive: true });
375
+ fs.writeFileSync(path.join(outDir, 'witness.json'), JSON.stringify(signed, null, 2) + '\n');
376
+ fs.writeFileSync(path.join(outDir, 'artifacts', 'graph.json'), JSON.stringify(graph));
377
+ fs.writeFileSync(path.join(outDir, 'artifacts', 'provenance.json'), JSON.stringify(provenance));
378
+ } catch (e) { err(`${who}: could not write --out: ${e.message}`); return 2; }
379
+
380
+ const overall = signed.completion?.overall || 'partial';
381
+ console.log(`Wrote ${path.join(outDir, 'witness.json')} (completion: ${overall}).`);
382
+ console.log(`Public key (share this with whoever verifies): ${kp.publicKey}`);
383
+ if (overall !== 'complete') err(`${who}: completion is ${overall}. A partial witness never supports a "no vulnerability" claim.`);
384
+ console.log('A valid signature proves the bundle is unmodified. It does not prove the finding is real or exploitable.');
385
+ return 0;
386
+ }
387
+
388
+ /**
389
+ * agentic-security witness verify <bundle.json> [--public-key <path>] [--artifacts <dir>] [--root <project>] [--json]
390
+ * Integrity check with a public key; with --artifacts also an offline replay of every edge against the recorded artifacts.
391
+ * Source blobs are read from --root (default: none, which fails replay honestly when the bundle binds sources).
392
+ */
393
+ async function witnessVerify(args) {
394
+ const who = 'agentic-security witness verify';
395
+ const f = args.flags;
396
+ const file = f.bundle || args._[2];
397
+ if (!file || typeof file !== 'string') { err(`Usage: ${who} <bundle.json> [--public-key <path>] [--artifacts <dir>] [--root <project>] [--json]`); return 2; }
398
+ const { parseWitnessBundle, verifyWitness } = await import('./witness-bundle.js');
399
+ const { keyPaths } = await import('./evidence-bundle.js');
400
+ let text;
401
+ try { text = fs.readFileSync(path.resolve(file), 'utf8'); } catch (e) { err(`${who}: could not read bundle: ${e.message}`); return 2; }
402
+ const parsed = parseWitnessBundle(text);
403
+ if (!parsed.ok) { err(`${who}: invalid: ${parsed.reason}`); return 1; }
404
+ const keyFile = f['public-key'] || keyPaths().publicKey;
405
+ let pub;
406
+ try { pub = fs.readFileSync(path.resolve(keyFile), 'utf8'); } catch { err(`${who}: could not read public key at ${keyFile}. Pass --public-key <path>.`); return 2; }
407
+
408
+ let artifacts = null;
409
+ if (f.artifacts !== undefined) {
410
+ if (typeof f.artifacts !== 'string' || !f.artifacts) { err(`${who}: --artifacts needs a directory.`); return 2; }
411
+ const dir = path.resolve(f.artifacts);
412
+ try {
413
+ artifacts = { graph: JSON.parse(fs.readFileSync(path.join(dir, 'graph.json'), 'utf8')), provenance: JSON.parse(fs.readFileSync(path.join(dir, 'provenance.json'), 'utf8')), sources: {} };
414
+ } catch (e) { err(`${who}: could not read artifacts in ${dir}: ${e.message}`); return 2; }
415
+ if (f.root) {
416
+ const root = path.resolve(String(f.root));
417
+ for (const s of parsed.bundle.sources || []) {
418
+ const abs = typeof s.file === 'string' ? insideRoot(root, s.file) : null;
419
+ if (!abs) continue;
420
+ try { if (fs.lstatSync(abs).isFile()) artifacts.sources[s.file] = fs.readFileSync(abs); } catch { /* a missing blob fails replay by name */ }
421
+ }
422
+ }
423
+ }
424
+ const res = verifyWitness(parsed.bundle, pub, artifacts);
425
+ if (f.json) process.stdout.write(JSON.stringify({ schema: 'agentic-security/witness-verification@1', verdict: res.verdict, ok: res.ok, signature: res.signature, replay: res.replay, completion: parsed.bundle.completion?.overall ?? null, evidence: parsed.bundle.evidence, statement: res.statement }, null, 2) + '\n');
426
+ else {
427
+ console.log(res.ok ? `VALID (${res.verdict})` : `INVALID (${res.verdict}): ${res.signature.ok ? res.replay.failures.map((x) => `${x.code}: ${x.detail}`).join('; ') : res.signature.reason}`);
428
+ console.log(` bundle completion: ${parsed.bundle.completion?.overall}`);
429
+ for (const [k, v] of Object.entries(parsed.bundle.evidence || {})) console.log(` ${k}: ${v.status}`);
430
+ if (res.verdict === 'integrity-only') console.log(' Replay was not performed. Pass --artifacts <dir> --root <project> to re-derive the path.');
431
+ console.log(` ${res.statement.doesNotProve}`);
432
+ }
433
+ return res.ok ? 0 : 1;
434
+ }
435
+
436
+ export async function cmdWitness(args) {
437
+ const sub = args._[1];
438
+ if (sub === 'build') return witnessBuild(args);
439
+ if (sub === 'verify') return witnessVerify(args);
440
+ err(`agentic-security witness: unrecognized sub-command "${sub}"; must be "build" or "verify".`);
441
+ return 2;
442
+ }
@@ -77,7 +77,7 @@ export function keyFingerprint(publicKeyPem) {
77
77
  }
78
78
 
79
79
  /** Issuance block for a locally generated key. */
80
- function buildLocalIssuance(publicKeyPem) {
80
+ export function buildLocalIssuance(publicKeyPem) {
81
81
  const fp = keyFingerprint(publicKeyPem);
82
82
  return {
83
83
  issuer: { id: `local-install:${fp.slice(0, 16)}`, kind: 'local-install', keyFingerprint: fp },
@@ -120,10 +120,29 @@ function _inferParser(f) {
120
120
  return 'REGEX';
121
121
  }
122
122
 
123
+ // Flow-pair and structural detectors record WHERE they fired on `sink.line`
124
+ // (and `source.line`) but never on the finding itself, so the schema's `line`
125
+ // was absent. A finding with no integer `line` cannot be matched to a site,
126
+ // cannot be suppressed by a line pragma (see `_applyIgnorePragmas`), and is
127
+ // credited to every same-class site in its file by any file-level matcher.
128
+ // The sink line is the place the finding is anchored: backfill it, never
129
+ // overwrite a detector-set line, and never invent one (no positive integer
130
+ // on the sink or source means the line stays absent).
131
+ function _inferLine(f) {
132
+ const ok = (n) => Number.isInteger(n) && n > 0;
133
+ if (ok(f.sink && f.sink.line)) return f.sink.line;
134
+ if (ok(f.source && f.source.line)) return f.source.line;
135
+ return null;
136
+ }
137
+
123
138
  export function backfillFindingDefaults(findings) {
124
139
  if (!Array.isArray(findings)) return;
125
140
  for (const f of findings) {
126
141
  if (!f || typeof f !== 'object') continue;
142
+ if (!Number.isInteger(f.line)) {
143
+ const ln = _inferLine(f);
144
+ if (ln !== null) f.line = ln;
145
+ }
127
146
  if (!f.parser) f.parser = _inferParser(f);
128
147
  if (!f.family) {
129
148
  const fam = _inferFamily(f);
@@ -53,6 +53,57 @@ function _blame(scanRoot, file, line) {
53
53
  } catch { return null; }
54
54
  }
55
55
 
56
+ // One `git blame` per file for every wanted line, instead of one per line.
57
+ // Porcelain output repeats the full metadata only the first time a commit
58
+ // appears, so metadata is kept per sha and each line's block is resolved
59
+ // against it. Returns Map(line -> meta), or null on any failure so the caller
60
+ // falls back to the per-line path (a line past the end of the file fails the
61
+ // whole call, and that must not lose the other lines).
62
+ function _blameFile(scanRoot, file, lines) {
63
+ const rel = path.isAbsolute(file) ? path.relative(scanRoot, file) : file;
64
+ if (rel.startsWith('..')) return null;
65
+ const wanted = [...new Set(lines)].filter((n) => Number.isInteger(n) && n >= 1).sort((a, b) => a - b);
66
+ if (!wanted.length) return null;
67
+ const args = ['blame', '--porcelain', '--no-textconv'];
68
+ for (const n of wanted) args.push('-L', `${n},${n}`);
69
+ args.push('--', rel);
70
+ try {
71
+ const out = cp.execFileSync('git', hardenGitArgs(args), {
72
+ cwd: scanRoot, encoding: 'utf8', timeout: SUBPROC_TIMEOUT_MS * 4, maxBuffer: 64 * 1024 * 1024,
73
+ stdio: ['ignore', 'pipe', 'ignore'], env: hardenGitEnv(),
74
+ });
75
+ const bySha = new Map();
76
+ const result = new Map();
77
+ let cur = null;
78
+ let curLine = null;
79
+ const flush = () => {
80
+ if (cur && curLine != null) result.set(curLine, cur);
81
+ cur = null; curLine = null;
82
+ };
83
+ for (const ln of out.split('\n')) {
84
+ const head = /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/.exec(ln);
85
+ if (head) {
86
+ flush();
87
+ if (!bySha.has(head[1])) bySha.set(head[1], { raw: [], sha40: head[1] });
88
+ cur = bySha.get(head[1]);
89
+ curLine = parseInt(head[3], 10);
90
+ continue;
91
+ }
92
+ if (!cur) continue;
93
+ if (ln.charCodeAt(0) === 9) cur.rawDone = true;
94
+ else if (!cur.rawDone) cur.raw.push(ln);
95
+ }
96
+ flush();
97
+ const parsed = new Map();
98
+ const resolved = new Map();
99
+ for (const [n, c] of result) {
100
+ if (!parsed.has(c.sha40)) parsed.set(c.sha40, _parsePorcelain(`${c.sha40} 1 1 1\n${c.raw.join('\n')}`));
101
+ resolved.set(n, parsed.get(c.sha40));
102
+ }
103
+ return resolved;
104
+ } catch { return null; }
105
+ }
106
+
56
107
  function _parsePorcelain(out) {
57
108
  if (!out) return null;
58
109
  const lines = out.split('\n');
@@ -80,6 +131,29 @@ function _fullMessage(scanRoot, sha) {
80
131
  } catch { return ''; }
81
132
  }
82
133
 
134
+ // Every distinct commit message in one `git log --no-walk` call instead of one
135
+ // `git show` per commit. Returns Map(sha prefix as given -> body); a sha missing
136
+ // from the result is looked up individually by the caller.
137
+ function _fullMessages(scanRoot, shas) {
138
+ const out = new Map();
139
+ const list = [...new Set(shas)].filter((x) => /^[0-9a-f]{7,40}$/.test(x));
140
+ if (!list.length) return out;
141
+ try {
142
+ const raw = cp.execFileSync(
143
+ 'git', hardenGitArgs(['log', '--no-walk=unsorted', '--no-textconv', '--format=%H%x1f%B%x1e', ...list, '--']),
144
+ { cwd: scanRoot, encoding: 'utf8', timeout: SUBPROC_TIMEOUT_MS * 4, maxBuffer: 64 * 1024 * 1024, stdio: ['ignore', 'pipe', 'ignore'], env: hardenGitEnv() },
145
+ );
146
+ for (const rec of raw.split('\x1e')) {
147
+ const i = rec.indexOf('\x1f');
148
+ if (i < 0) continue;
149
+ const full = rec.slice(0, i).replace(/^\s+/, '');
150
+ const body = rec.slice(i + 1).replace(/^\n+/, '');
151
+ for (const sha of list) if (full.startsWith(sha)) out.set(sha, body);
152
+ }
153
+ } catch { /* the caller falls back per commit */ }
154
+ return out;
155
+ }
156
+
83
157
  function _extractPrompt(body) {
84
158
  if (!body || !PROMPT_MARKER_RE.test(body)) return null;
85
159
  // Take the line(s) that follow a "Prompt:" or "User asked:" marker, up to
@@ -101,10 +175,26 @@ export function annotateGitHistory(scanRoot, findings) {
101
175
  const messageCache = new Map(); // sha → body
102
176
  let annotated = 0, skipped = 0;
103
177
 
178
+ // Batch the blame per file; a file whose batch fails is blamed line by line.
179
+ const wantedByFile = new Map();
180
+ for (let i = 0; i < findings.length && i < MAX_BLAME_PER_SCAN; i++) {
181
+ const f = findings[i];
182
+ if (!f || !f.file || !f.line) continue;
183
+ if (!wantedByFile.has(f.file)) wantedByFile.set(f.file, []);
184
+ wantedByFile.get(f.file).push(f.line);
185
+ }
186
+ const blameByFile = new Map();
187
+ for (const [file, lines] of wantedByFile) blameByFile.set(file, _blameFile(scanRoot, file, lines));
188
+
189
+ const wantedShas = [];
190
+ for (const m of blameByFile.values()) if (m) for (const meta of m.values()) if (meta && meta.sha) wantedShas.push(meta.sha);
191
+ for (const [sha, body] of _fullMessages(scanRoot, wantedShas)) messageCache.set(sha, body);
192
+
104
193
  for (let i = 0; i < findings.length && i < MAX_BLAME_PER_SCAN; i++) {
105
194
  const f = findings[i];
106
195
  if (!f || !f.file || !f.line) { skipped++; continue; }
107
- const blame = _blame(scanRoot, f.file, f.line);
196
+ const batch = blameByFile.get(f.file);
197
+ const blame = batch && batch.has(f.line) ? batch.get(f.line) : _blame(scanRoot, f.file, f.line);
108
198
  if (!blame) { skipped++; continue; }
109
199
 
110
200
  f.introducedBy = blame.author || null;