@agentskit/doc-bridge 1.4.3 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  // src/cli/program.ts
2
- import { existsSync as existsSync17, mkdirSync as mkdirSync5, readFileSync as readFileSync17, writeFileSync as writeFileSync4 } from "fs";
3
- import { dirname as dirname8, resolve as resolve13 } from "path";
2
+ import { existsSync as existsSync21, mkdirSync as mkdirSync8, readFileSync as readFileSync21, writeFileSync as writeFileSync8 } from "fs";
3
+ import { dirname as dirname10, resolve as resolve18 } from "path";
4
4
  import { createInterface } from "readline/promises";
5
5
 
6
6
  // src/config/load-config.ts
@@ -302,6 +302,14 @@ var applyConfigDefaults = (config) => {
302
302
  preset: "minimal",
303
303
  ...config.gates
304
304
  },
305
+ rules: {
306
+ mode: "default",
307
+ ...config.rules
308
+ },
309
+ safety: {
310
+ redactSecrets: true,
311
+ ...config.safety
312
+ },
305
313
  surfaces: {
306
314
  cli: {
307
315
  bin: "ak-docs",
@@ -310,7 +318,22 @@ var applyConfigDefaults = (config) => {
310
318
  },
311
319
  mcp: {
312
320
  enabled: true,
313
- tools: ["handoff.resolve", "doc.search", "doc.get", "gate.status"],
321
+ tools: [
322
+ "handoff.resolve",
323
+ "doc.search",
324
+ "doc.get",
325
+ "gate.status",
326
+ "retriever.query",
327
+ "memory.classify",
328
+ "memory.promoteDraft",
329
+ "registry.topology",
330
+ "docbridge.snapshot",
331
+ "docbridge.report",
332
+ "docbridge.diagnostics",
333
+ "docbridge.relations",
334
+ "docbridge.run",
335
+ "docbridge.proposals"
336
+ ],
314
337
  transport: "stdio",
315
338
  ...config.surfaces?.mcp
316
339
  },
@@ -428,6 +451,39 @@ var GatesConfigSchema = z.object({
428
451
  ).max(16).optional(),
429
452
  options: z.record(z.string(), z.unknown()).optional()
430
453
  }).strict();
454
+ var RuleIdSchema = z.enum([
455
+ "documentation-quality",
456
+ "graph-undocumented-relation",
457
+ "declared-unobserved-relation",
458
+ "unresolved-reference",
459
+ "conflicting-declaration",
460
+ "not-analyzed-coverage",
461
+ "stale-documentation",
462
+ "centrality-risk",
463
+ "critical-path-risk",
464
+ "freshness",
465
+ "ownership"
466
+ ]);
467
+ var RuleSeveritySchema = z.enum(["off", "info", "warn", "error"]);
468
+ var RulesConfigSchema = z.object({
469
+ mode: z.enum(["default", "recommended", "strict"]).optional(),
470
+ severity: z.record(RuleIdSchema, RuleSeveritySchema).optional(),
471
+ ignore: z.array(RuleIdSchema).max(128).optional(),
472
+ criticalEntities: z.array(z.string().min(1).max(256)).max(128).optional(),
473
+ criticalPaths: z.array(z.string().min(1).max(512)).max(128).optional(),
474
+ warningThresholds: z.record(RuleIdSchema, z.number().int().min(1).max(1e5)).optional()
475
+ }).strict();
476
+ var WorkflowConfigSchema = z.object({
477
+ stateDir: z.string().min(1).max(512).optional()
478
+ }).strict();
479
+ var RepositorySafetyConfigSchema = z.object({
480
+ exclude: z.array(z.string().min(1).max(512)).max(128).optional(),
481
+ maxFiles: z.number().int().positive().max(1e6).optional(),
482
+ maxBytes: z.number().int().positive().max(1e10).optional(),
483
+ maxTimeMs: z.number().int().positive().max(864e5).optional(),
484
+ maxMemoryMb: z.number().int().positive().max(1048576).optional(),
485
+ redactSecrets: z.boolean().optional()
486
+ }).strict();
431
487
  var SurfacesConfigSchema = z.object({
432
488
  cli: z.object({
433
489
  bin: z.string().min(1).max(64).optional(),
@@ -445,7 +501,13 @@ var SurfacesConfigSchema = z.object({
445
501
  "retriever.query",
446
502
  "memory.classify",
447
503
  "memory.promoteDraft",
448
- "registry.topology"
504
+ "registry.topology",
505
+ "docbridge.snapshot",
506
+ "docbridge.report",
507
+ "docbridge.diagnostics",
508
+ "docbridge.relations",
509
+ "docbridge.run",
510
+ "docbridge.proposals"
449
511
  ])
450
512
  ).max(16).optional(),
451
513
  transport: z.enum(["stdio", "http"]).optional(),
@@ -488,7 +550,13 @@ var IntelligenceConfigSchema = z.object({
488
550
  }).strict().optional()
489
551
  }).strict().optional(),
490
552
  runtime: z.enum(["agentskit", "custom"]).optional(),
491
- runtimeModule: z.string().min(1).max(512).optional()
553
+ runtimeModule: z.string().min(1).max(512).optional(),
554
+ registry: z.object({
555
+ enabled: z.boolean().optional(),
556
+ agentId: z.string().min(1).max(256).optional(),
557
+ agentRoot: z.string().min(1).max(512).optional(),
558
+ runnerModule: z.string().min(1).max(512).optional()
559
+ }).strict().optional()
492
560
  }).strict();
493
561
  var FederationConfigSchema = z.object({
494
562
  enabled: z.boolean().optional(),
@@ -578,6 +646,9 @@ var DocBridgeConfigV1Schema = z.object({
578
646
  index: IndexConfigSchema.optional(),
579
647
  routing: RoutingConfigSchema.optional(),
580
648
  gates: GatesConfigSchema.optional(),
649
+ rules: RulesConfigSchema.optional(),
650
+ workflow: WorkflowConfigSchema.optional(),
651
+ safety: RepositorySafetyConfigSchema.optional(),
581
652
  surfaces: SurfacesConfigSchema.optional(),
582
653
  intelligence: IntelligenceConfigSchema.optional(),
583
654
  federation: FederationConfigSchema.optional(),
@@ -1368,7 +1439,7 @@ var buildLookup = (config, packages, corpus, indexOutFile, humanDocs = {}, root
1368
1439
  };
1369
1440
 
1370
1441
  // src/version.ts
1371
- var PACKAGE_VERSION = "1.4.3";
1442
+ var PACKAGE_VERSION = "1.5.0";
1372
1443
 
1373
1444
  // src/index-builder/capabilities.ts
1374
1445
  var renderCapabilitiesJson = (config, index, paths) => {
@@ -1413,23 +1484,28 @@ var sortValue = (value) => {
1413
1484
  }
1414
1485
  return value;
1415
1486
  };
1487
+ var canonicalJsonV1 = (payload) => JSON.stringify(sortValue(payload));
1416
1488
  var sha256NormalizedV1 = (payload) => {
1417
- const normalized = JSON.stringify(sortValue(payload));
1489
+ const normalized = canonicalJsonV1(payload);
1418
1490
  return createHash("sha256").update(normalized, "utf8").digest("hex");
1419
1491
  };
1492
+ var contentHashForArtifactV1 = (artifact2) => {
1493
+ const { contentHash: _contentHash, ...payload } = artifact2;
1494
+ return sha256NormalizedV1(payload);
1495
+ };
1420
1496
 
1421
1497
  // src/index-builder/llms-txt.ts
1422
1498
  var routePath = (path, pathPrefix) => {
1423
1499
  const normalizedPath = path.replaceAll("\\", "/").replace(/\.(?:md|mdx)$/, "");
1424
1500
  const normalizedPrefix = pathPrefix?.replaceAll("\\", "/").replace(/\/$/, "");
1425
- const relativePath2 = normalizedPrefix && normalizedPath.startsWith(`${normalizedPrefix}/`) ? normalizedPath.slice(normalizedPrefix.length + 1) : normalizedPath;
1426
- return relativePath2.replace(/^\/+/, "");
1501
+ const relativePath3 = normalizedPrefix && normalizedPath.startsWith(`${normalizedPrefix}/`) ? normalizedPath.slice(normalizedPrefix.length + 1) : normalizedPath;
1502
+ return relativePath3.replace(/^\/+/, "");
1427
1503
  };
1428
1504
  var knowledgeUrl = (path, options = {}) => {
1429
- const relativePath2 = routePath(path, options.pathPrefix);
1505
+ const relativePath3 = routePath(path, options.pathPrefix);
1430
1506
  if (!options.urlPrefix) return path;
1431
1507
  const base = options.urlPrefix.endsWith("/") ? options.urlPrefix : `${options.urlPrefix}/`;
1432
- return new URL(relativePath2, base).toString();
1508
+ return new URL(relativePath3, base).toString();
1433
1509
  };
1434
1510
  var renderLlmsTxt = (config, knowledge, projectName2) => {
1435
1511
  const preamble = config.index?.llmsTxt?.preamble ?? `# ${projectName2}
@@ -2185,26 +2261,26 @@ var llmsRule = (root, config, options) => {
2185
2261
  if (key !== llmsKey && !rawSources.has(key)) rawSources.set(key, path);
2186
2262
  }
2187
2263
  const paths = [llmsPath, ...rawSources.values()];
2188
- const evidence = paths.map((path) => fileEvidence(root, path));
2264
+ const evidence2 = paths.map((path) => fileEvidence(root, path));
2189
2265
  const generated = buildDocBridgeIndex({ root, config, write: false }).index;
2190
2266
  const expectedLlms = renderLlmsTxt(config, generated.knowledge, generated.project?.name ?? "project");
2191
- const llmsIsFresh = evidence[0]?.content === expectedLlms;
2192
- if (evidence[0]?.exists) {
2193
- evidence[0] = {
2194
- ...evidence[0],
2267
+ const llmsIsFresh = evidence2[0]?.content === expectedLlms;
2268
+ if (evidence2[0]?.exists) {
2269
+ evidence2[0] = {
2270
+ ...evidence2[0],
2195
2271
  evidence: {
2196
- ...evidence[0].evidence,
2272
+ ...evidence2[0].evidence,
2197
2273
  detail: llmsIsFresh ? "File matches the deterministic ak-docs output." : "File is stale or was not generated by the current ak-docs inputs."
2198
2274
  }
2199
2275
  };
2200
2276
  }
2201
- const passed = config.index?.llmsTxt?.enabled !== false && paths.length > 1 && llmsIsFresh && evidence.every((item) => item.exists);
2277
+ const passed = config.index?.llmsTxt?.enabled !== false && paths.length > 1 && llmsIsFresh && evidence2.every((item) => item.exists);
2202
2278
  return {
2203
2279
  id: "llms-and-raw-source",
2204
2280
  level: "required",
2205
2281
  passed,
2206
2282
  message: passed ? `Resolved llms.txt and ${paths.length - 1} raw source(s).` : "llms.txt must be enabled, current, and accompanied by at least one readable raw source.",
2207
- evidence: evidence.map((item) => item.evidence),
2283
+ evidence: evidence2.map((item) => item.evidence),
2208
2284
  remediation: {
2209
2285
  command: "ak-docs index",
2210
2286
  detail: "Generate llms.txt and configure conformance.documentationStandardV1.rawSources."
@@ -2223,8 +2299,8 @@ var ecosystemContract = (root, options) => {
2223
2299
  }
2224
2300
  const manifestFile = fileEvidence(root, declaration.manifest);
2225
2301
  const claimsFile = fileEvidence(root, declaration.claims);
2226
- const evidence = [manifestFile.evidence, claimsFile.evidence];
2227
- if (!manifestFile.exists || !claimsFile.exists) return { passed: false, urls: /* @__PURE__ */ new Set(), evidence };
2302
+ const evidence2 = [manifestFile.evidence, claimsFile.evidence];
2303
+ if (!manifestFile.exists || !claimsFile.exists) return { passed: false, urls: /* @__PURE__ */ new Set(), evidence: evidence2 };
2228
2304
  try {
2229
2305
  const manifest = JSON.parse(manifestFile.content);
2230
2306
  const claims = JSON.parse(claimsFile.content);
@@ -2242,15 +2318,15 @@ var ecosystemContract = (root, options) => {
2242
2318
  if (typeof value === "string" && /^https:\/\//.test(value)) urls.add(normalizedUrl(value));
2243
2319
  }
2244
2320
  }
2245
- evidence[0] = { path: declaration.manifest, detail: `Validated ${productIds.length} canonical product(s), including ${declaration.productId}.` };
2246
- evidence[1] = { path: declaration.claims, detail: `Validated claim-ledger identity for ${contract.claims.products.length} product(s).` };
2247
- return { passed: urls.size > 0, urls, evidence };
2321
+ evidence2[0] = { path: declaration.manifest, detail: `Validated ${productIds.length} canonical product(s), including ${declaration.productId}.` };
2322
+ evidence2[1] = { path: declaration.claims, detail: `Validated claim-ledger identity for ${contract.claims.products.length} product(s).` };
2323
+ return { passed: urls.size > 0, urls, evidence: evidence2 };
2248
2324
  } catch (error) {
2249
- evidence.push({
2325
+ evidence2.push({
2250
2326
  path: `${declaration.manifest}, ${declaration.claims}`,
2251
2327
  detail: error instanceof Error ? error.message : "Canonical ecosystem contract is invalid."
2252
2328
  });
2253
- return { passed: false, urls: /* @__PURE__ */ new Set(), evidence };
2329
+ return { passed: false, urls: /* @__PURE__ */ new Set(), evidence: evidence2 };
2254
2330
  }
2255
2331
  };
2256
2332
  var handoffsRule = (root, config) => {
@@ -2276,13 +2352,13 @@ var handoffsRule = (root, config) => {
2276
2352
  };
2277
2353
  var contributionRule = (root, options) => {
2278
2354
  const paths = options.contributionPaths?.length ? options.contributionPaths : ["CONTRIBUTING.md"];
2279
- const evidence = paths.map((path) => fileEvidence(root, path));
2355
+ const evidence2 = paths.map((path) => fileEvidence(root, path));
2280
2356
  return {
2281
2357
  id: "contribution",
2282
2358
  level: "required",
2283
- passed: evidence.some((item) => item.exists),
2284
- message: evidence.some((item) => item.exists) ? "Contribution guidance is available." : "Contribution guidance is missing.",
2285
- evidence: evidence.map((item) => item.evidence),
2359
+ passed: evidence2.some((item) => item.exists),
2360
+ message: evidence2.some((item) => item.exists) ? "Contribution guidance is available." : "Contribution guidance is missing.",
2361
+ evidence: evidence2.map((item) => item.evidence),
2286
2362
  remediation: {
2287
2363
  command: "edit CONTRIBUTING.md",
2288
2364
  detail: "Document setup, validation commands, and the pull-request workflow."
@@ -2291,13 +2367,13 @@ var contributionRule = (root, options) => {
2291
2367
  };
2292
2368
  var markersRule = (root, options, kind, level) => {
2293
2369
  const declarations = options[kind === "metadata" ? "metadata" : "diagrams"] ?? [];
2294
- const evidence = [];
2370
+ const evidence2 = [];
2295
2371
  let passed = declarations.length > 0;
2296
2372
  for (const declaration of declarations) {
2297
2373
  const file = fileEvidence(root, declaration.path);
2298
2374
  const missing = declaration.contains.filter((marker) => !file.content.includes(marker));
2299
2375
  if (!file.exists || missing.length > 0) passed = false;
2300
- evidence.push({
2376
+ evidence2.push({
2301
2377
  path: declaration.path,
2302
2378
  detail: !file.exists ? file.evidence.detail : missing.length ? `Missing marker(s): ${missing.join(", ")}` : `Found marker(s): ${declaration.contains.join(", ")}`
2303
2379
  });
@@ -2307,7 +2383,7 @@ var markersRule = (root, options, kind, level) => {
2307
2383
  level,
2308
2384
  passed,
2309
2385
  message: passed ? `${kind} evidence is complete.` : `${kind} evidence is incomplete.`,
2310
- evidence,
2386
+ evidence: evidence2,
2311
2387
  remediation: {
2312
2388
  command: "edit doc-bridge.config.json",
2313
2389
  detail: `Declare ${kind} evidence paths and markers that exist in the repository.`
@@ -2317,14 +2393,14 @@ var markersRule = (root, options, kind, level) => {
2317
2393
  var linksRule = (root, options) => {
2318
2394
  const links = options.links ?? [];
2319
2395
  const contract = ecosystemContract(root, options);
2320
- const evidence = [...contract.evidence];
2396
+ const evidence2 = [...contract.evidence];
2321
2397
  let passed = links.length > 0 && contract.passed;
2322
2398
  for (const link of links) {
2323
2399
  const sources = link.paths.map((path) => ({ path, file: fileEvidence(root, path) }));
2324
2400
  const matches = sources.filter(({ file }) => file.exists && file.content.includes(link.url));
2325
2401
  const canonical = contract.urls.has(normalizedUrl(link.url));
2326
2402
  if (matches.length === 0 || !canonical) passed = false;
2327
- evidence.push({
2403
+ evidence2.push({
2328
2404
  path: sources.map((source) => source.path).join(", "),
2329
2405
  detail: matches.length === 0 ? `Missing ${link.url}` : canonical ? `Found canonical ecosystem URL ${link.url}` : `Found ${link.url}, but it is absent from the canonical ecosystem manifest.`
2330
2406
  });
@@ -2334,7 +2410,7 @@ var linksRule = (root, options) => {
2334
2410
  level: "required",
2335
2411
  passed,
2336
2412
  message: passed ? `${links.length} required ecosystem link(s) resolve in source.` : "One or more required ecosystem links are missing from source.",
2337
- evidence,
2413
+ evidence: evidence2,
2338
2414
  remediation: {
2339
2415
  command: "edit README.md",
2340
2416
  detail: "Sync the canonical ecosystem snapshots and add each configured canonical URL to a declared documentation source."
@@ -2343,14 +2419,14 @@ var linksRule = (root, options) => {
2343
2419
  };
2344
2420
  var quickstartsRule = (root, options) => {
2345
2421
  const quickstarts = options.quickstarts ?? [];
2346
- const evidence = [];
2422
+ const evidence2 = [];
2347
2423
  let passed = quickstarts.length > 0;
2348
2424
  for (const quickstart of quickstarts) {
2349
2425
  const doc = fileEvidence(root, quickstart.doc);
2350
2426
  const test = fileEvidence(root, quickstart.test);
2351
2427
  const missingMarkers = quickstart.testContains.filter((marker) => !test.content.includes(marker));
2352
2428
  if (!doc.exists || !test.exists || missingMarkers.length > 0 || !quickstart.command.trim()) passed = false;
2353
- evidence.push(
2429
+ evidence2.push(
2354
2430
  { path: quickstart.doc, detail: `${quickstart.id}: ${doc.evidence.detail}` },
2355
2431
  {
2356
2432
  path: quickstart.test,
@@ -2363,7 +2439,7 @@ var quickstartsRule = (root, options) => {
2363
2439
  level: "required",
2364
2440
  passed,
2365
2441
  message: passed ? `${quickstarts.length} quickstart(s) have executable test evidence.` : "Quickstart test evidence is incomplete.",
2366
- evidence,
2442
+ evidence: evidence2,
2367
2443
  remediation: {
2368
2444
  command: "pnpm test",
2369
2445
  detail: "Map every quickstart to a documentation path, test file, identifying marker, and CI command."
@@ -2372,13 +2448,13 @@ var quickstartsRule = (root, options) => {
2372
2448
  };
2373
2449
  var visualsRule = (root, options) => {
2374
2450
  const visuals = options.visuals ?? [];
2375
- const evidence = visuals.map((path) => fileEvidence(root, path, { readContent: false }));
2451
+ const evidence2 = visuals.map((path) => fileEvidence(root, path, { readContent: false }));
2376
2452
  return {
2377
2453
  id: "visual-explanations",
2378
2454
  level: "recommended",
2379
- passed: visuals.length > 0 && evidence.every((item) => item.exists),
2380
- message: visuals.length > 0 && evidence.every((item) => item.exists) ? `${visuals.length} visual asset(s) found.` : "Visual explanation evidence is incomplete.",
2381
- evidence: evidence.map((item) => item.evidence),
2455
+ passed: visuals.length > 0 && evidence2.every((item) => item.exists),
2456
+ message: visuals.length > 0 && evidence2.every((item) => item.exists) ? `${visuals.length} visual asset(s) found.` : "Visual explanation evidence is incomplete.",
2457
+ evidence: evidence2.map((item) => item.evidence),
2382
2458
  remediation: {
2383
2459
  command: "edit doc-bridge.config.json",
2384
2460
  detail: "Declare the images or animations that explain the product workflow."
@@ -2427,14 +2503,941 @@ var formatDocumentationStandardText = (report) => [
2427
2503
  "",
2428
2504
  ...report.results.flatMap((result) => [
2429
2505
  `${result.status === "pass" ? "PASS" : result.status === "excepted" ? "EXCEPTED" : "FAIL"} [${result.level}] ${result.id}: ${result.message}`,
2430
- ...result.evidence.map((evidence) => ` evidence: ${evidence.path} \u2014 ${evidence.detail}`),
2506
+ ...result.evidence.map((evidence2) => ` evidence: ${evidence2.path} \u2014 ${evidence2.detail}`),
2431
2507
  ...result.exception ? [` exception: ${result.exception.reason} \u2014 ${result.exception.approvedBy} (${result.exception.trackingUrl})`] : result.ok ? [] : [` fix: ${result.remediation.command} \u2014 ${result.remediation.detail}`]
2432
2508
  ])
2433
2509
  ];
2434
2510
 
2435
- // src/federation/llms.ts
2511
+ // src/schemas/knowledge.ts
2512
+ import { z as z3 } from "zod";
2513
+ var KNOWLEDGE_SCHEMA_VERSION = 1;
2514
+ var KNOWLEDGE_CONTENT_HASH_ALGO = "sha256-normalized-v1";
2515
+ var hash = z3.string().regex(/^[a-f0-9]{64}$/);
2516
+ var boundedString = (max) => z3.string().min(1).max(max);
2517
+ var ProvenanceSchema = z3.enum(["observed", "declared", "proposed"]);
2518
+ var FindingStatusSchema = z3.enum([
2519
+ "confirmed",
2520
+ "undocumented",
2521
+ "stale-or-unverified",
2522
+ "conflict",
2523
+ "unresolved",
2524
+ "not-analyzed"
2525
+ ]);
2526
+ var DiagnosticSeveritySchema = z3.enum(["off", "info", "warn", "error"]);
2527
+ var EvidenceSourceSchema = z3.enum([
2528
+ "code",
2529
+ "configuration",
2530
+ "documentation",
2531
+ "agent",
2532
+ "derived"
2533
+ ]);
2534
+ var EvidenceSchema2 = z3.object({
2535
+ source: EvidenceSourceSchema,
2536
+ path: boundedString(512),
2537
+ lineStart: z3.number().int().positive().optional(),
2538
+ lineEnd: z3.number().int().positive().optional(),
2539
+ contentHash: hash.optional(),
2540
+ context: z3.string().max(1024).optional()
2541
+ }).strict().superRefine((value, context) => {
2542
+ if (value.lineStart !== void 0 && value.lineEnd !== void 0 && value.lineEnd < value.lineStart) {
2543
+ context.addIssue({
2544
+ code: z3.ZodIssueCode.custom,
2545
+ path: ["lineEnd"],
2546
+ message: "Must be greater than or equal to lineStart"
2547
+ });
2548
+ }
2549
+ });
2550
+ var CoverageStatusSchema = z3.enum(["complete", "partial", "not-analyzed"]);
2551
+ var CoverageSchema = z3.object({
2552
+ analyzer: boundedString(128),
2553
+ scope: boundedString(512),
2554
+ status: CoverageStatusSchema,
2555
+ reason: z3.string().max(1024).optional(),
2556
+ evidence: z3.array(EvidenceSchema2).max(32).optional()
2557
+ }).strict();
2558
+ var ProjectIdentitySchema = z3.object({
2559
+ name: boundedString(128),
2560
+ root: boundedString(512).optional()
2561
+ }).strict();
2562
+ var ArtifactMetadata = {
2563
+ schemaVersion: z3.literal(KNOWLEDGE_SCHEMA_VERSION),
2564
+ contentHash: hash,
2565
+ contentHashAlgo: z3.literal(KNOWLEDGE_CONTENT_HASH_ALGO),
2566
+ project: ProjectIdentitySchema,
2567
+ sourceRevision: boundedString(128),
2568
+ sourceRevisionKind: z3.enum(["git", "content"]),
2569
+ configurationHash: hash,
2570
+ pipelineVersion: boundedString(64),
2571
+ analyzerVersions: z3.record(boundedString(128), boundedString(64))
2572
+ };
2573
+ var EntitySchema = z3.object({
2574
+ id: boundedString(256),
2575
+ kind: boundedString(128),
2576
+ name: boundedString(256),
2577
+ path: boundedString(512).optional(),
2578
+ aliases: z3.array(boundedString(256)).max(32).optional(),
2579
+ provenance: ProvenanceSchema,
2580
+ evidence: z3.array(EvidenceSchema2).max(64),
2581
+ metadata: z3.record(z3.unknown()).optional()
2582
+ }).strict();
2583
+ var RelationSchema = z3.object({
2584
+ id: boundedString(256),
2585
+ kind: boundedString(128),
2586
+ from: boundedString(256),
2587
+ to: boundedString(256),
2588
+ discriminator: boundedString(256).optional(),
2589
+ provenance: ProvenanceSchema,
2590
+ evidence: z3.array(EvidenceSchema2).max(64),
2591
+ metadata: z3.record(z3.unknown()).optional()
2592
+ }).strict();
2593
+ var DiscoverySnapshotV1Schema = z3.object({
2594
+ type: z3.literal("discovery-snapshot"),
2595
+ ...ArtifactMetadata,
2596
+ entities: z3.array(EntitySchema).max(5e4),
2597
+ relations: z3.array(RelationSchema).max(1e5),
2598
+ coverage: z3.array(CoverageSchema).max(1e3)
2599
+ }).strict().superRefine((value, context) => {
2600
+ const entityIds = /* @__PURE__ */ new Set();
2601
+ for (const [index, entity] of value.entities.entries()) {
2602
+ if (entityIds.has(entity.id)) {
2603
+ context.addIssue({ code: z3.ZodIssueCode.custom, path: ["entities", index, "id"], message: `Duplicate entity id: ${entity.id}` });
2604
+ }
2605
+ entityIds.add(entity.id);
2606
+ }
2607
+ const relationIds = /* @__PURE__ */ new Set();
2608
+ for (const [index, relation] of value.relations.entries()) {
2609
+ if (relationIds.has(relation.id)) {
2610
+ context.addIssue({ code: z3.ZodIssueCode.custom, path: ["relations", index, "id"], message: `Duplicate relation id: ${relation.id}` });
2611
+ }
2612
+ relationIds.add(relation.id);
2613
+ }
2614
+ });
2615
+ var DiagnosticSchema = z3.object({
2616
+ id: boundedString(256),
2617
+ code: boundedString(128),
2618
+ status: FindingStatusSchema,
2619
+ severity: DiagnosticSeveritySchema,
2620
+ message: boundedString(2048),
2621
+ evidence: z3.array(EvidenceSchema2).max(64),
2622
+ entityIds: z3.array(boundedString(256)).max(64).optional(),
2623
+ relationIds: z3.array(boundedString(256)).max(64).optional(),
2624
+ remediation: z3.string().max(2048).optional()
2625
+ }).strict();
2626
+ var ReconciliationReportV1Schema = z3.object({
2627
+ type: z3.literal("reconciliation-report"),
2628
+ ...ArtifactMetadata,
2629
+ snapshotHash: hash,
2630
+ diagnostics: z3.array(DiagnosticSchema).max(1e5),
2631
+ summary: z3.object({
2632
+ entityCount: z3.number().int().nonnegative(),
2633
+ relationCount: z3.number().int().nonnegative(),
2634
+ diagnosticCount: z3.number().int().nonnegative()
2635
+ }).strict()
2636
+ }).strict();
2637
+ var WorkflowStateSchema = z3.enum([
2638
+ "created",
2639
+ "discovering",
2640
+ "analyzed",
2641
+ "compared",
2642
+ "awaiting-agent",
2643
+ "proposed",
2644
+ "awaiting-approval",
2645
+ "validating",
2646
+ "delivered",
2647
+ "failed",
2648
+ "cancelled",
2649
+ "stale",
2650
+ "superseded"
2651
+ ]);
2652
+ var WorkflowStepSchema = z3.object({
2653
+ name: boundedString(128),
2654
+ status: z3.enum(["pending", "running", "completed", "failed", "skipped"]),
2655
+ inputHash: hash,
2656
+ outputHash: hash.optional(),
2657
+ artifactRefs: z3.array(boundedString(512)).max(32).optional()
2658
+ }).strict();
2659
+ var WorkflowTransitionSchema = z3.object({
2660
+ from: WorkflowStateSchema.nullable(),
2661
+ to: WorkflowStateSchema,
2662
+ at: z3.string().datetime(),
2663
+ reason: z3.string().max(1024).optional()
2664
+ }).strict();
2665
+ var WorkflowRunV1Schema = z3.object({
2666
+ type: z3.literal("workflow-run"),
2667
+ ...ArtifactMetadata,
2668
+ runId: boundedString(128),
2669
+ state: WorkflowStateSchema,
2670
+ steps: z3.array(WorkflowStepSchema).max(32),
2671
+ transitions: z3.array(WorkflowTransitionSchema).max(1e3),
2672
+ artifactRefs: z3.array(boundedString(512)).max(128)
2673
+ }).strict();
2674
+ var ProposalOriginSchema = z3.object({
2675
+ kind: z3.enum(["registry-agent", "manual", "deterministic"]),
2676
+ id: boundedString(256).optional(),
2677
+ version: boundedString(64).optional(),
2678
+ provider: boundedString(128).optional(),
2679
+ model: boundedString(256).optional(),
2680
+ capabilities: z3.array(boundedString(128)).max(32).optional()
2681
+ }).strict().superRefine((value, context) => {
2682
+ if (value.kind === "registry-agent" && value.id === void 0) {
2683
+ context.addIssue({ code: z3.ZodIssueCode.custom, path: ["id"], message: "Registry agent origin requires an id" });
2684
+ }
2685
+ });
2686
+ var AgentProposalV1Schema = z3.object({
2687
+ type: z3.literal("agent-proposal"),
2688
+ ...ArtifactMetadata,
2689
+ proposalId: boundedString(128),
2690
+ baseSnapshotHash: hash,
2691
+ baseReportHash: hash,
2692
+ relatedDiagnosticIds: z3.array(boundedString(256)).max(64),
2693
+ rationale: boundedString(4e3),
2694
+ confidence: z3.number().min(0).max(1),
2695
+ evidence: z3.array(EvidenceSchema2).max(128),
2696
+ intendedChanges: z3.array(boundedString(4e3)).max(64),
2697
+ origin: ProposalOriginSchema,
2698
+ checks: z3.array(boundedString(512)).max(32)
2699
+ }).strict();
2700
+ var FixProposalStatusSchema = z3.enum(["proposed", "approved", "rejected", "stale", "applied", "failed"]);
2701
+ var AffectedFileSchema = z3.object({
2702
+ path: boundedString(512),
2703
+ contentHash: hash
2704
+ }).strict();
2705
+ var FixChangeSchema = z3.object({
2706
+ path: boundedString(512),
2707
+ before: z3.string().max(1e5),
2708
+ after: z3.string().max(1e5)
2709
+ }).strict();
2710
+ var FixProposalV1Schema = z3.object({
2711
+ type: z3.literal("fix-proposal"),
2712
+ ...ArtifactMetadata,
2713
+ proposalId: boundedString(128),
2714
+ baseRevision: boundedString(128),
2715
+ affectedFiles: z3.array(AffectedFileSchema).max(256),
2716
+ changes: z3.array(FixChangeSchema).max(256).optional(),
2717
+ preconditions: z3.array(boundedString(2048)).max(64),
2718
+ diff: boundedString(1e5),
2719
+ postconditions: z3.array(boundedString(2048)).max(64),
2720
+ approval: z3.object({
2721
+ proposalHash: hash,
2722
+ approvedAt: z3.string().datetime(),
2723
+ approvedBy: boundedString(256)
2724
+ }).strict().optional(),
2725
+ status: FixProposalStatusSchema
2726
+ }).strict().superRefine((value, context) => {
2727
+ if ((value.status === "approved" || value.status === "applied") && value.approval === void 0) {
2728
+ context.addIssue({
2729
+ code: z3.ZodIssueCode.custom,
2730
+ path: ["approval"],
2731
+ message: `Fix proposal status ${value.status} requires an approval record`
2732
+ });
2733
+ }
2734
+ });
2735
+
2736
+ // src/discovery/documentation.ts
2737
+ var detectionValues = /* @__PURE__ */ new Set(["static", "dynamic", "external"]);
2738
+ var evidence = (path, lineStart, lineEnd = lineStart) => ({
2739
+ source: "documentation",
2740
+ path,
2741
+ lineStart,
2742
+ lineEnd
2743
+ });
2744
+ var diagnostic = (path, code, message, lineStart, lineEnd = lineStart) => ({ code, message, path: "docbridge", evidence: evidence(path, lineStart, lineEnd) });
2745
+ var scalar = (value) => {
2746
+ const trimmed = value.trim();
2747
+ if (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'")) {
2748
+ return trimmed.slice(1, -1);
2749
+ }
2750
+ return trimmed;
2751
+ };
2752
+ var list = (value) => {
2753
+ const trimmed = value.trim();
2754
+ if (!trimmed.startsWith("[") || !trimmed.endsWith("]")) return void 0;
2755
+ const body = trimmed.slice(1, -1).trim();
2756
+ return body ? body.split(",").map(scalar).filter(Boolean) : [];
2757
+ };
2758
+ var findFrontmatter = (content) => {
2759
+ const lines = content.replace(/^\uFEFF/, "").split(/\r?\n/);
2760
+ if (lines[0] !== "---") return void 0;
2761
+ const end = lines.findIndex((line, index) => index > 0 && line === "---");
2762
+ return end < 0 ? void 0 : { lines, end };
2763
+ };
2764
+ var addDiagnostic = (diagnostics, input, code, message, line, endLine = line) => {
2765
+ diagnostics.push(diagnostic(input.path, code, message, line, endLine));
2766
+ };
2767
+ var resolveEntity = (reference, entities, input, lineStart, unresolved) => {
2768
+ const resolved = entities.find((entity2) => entity2.id === reference || entity2.aliases?.includes(reference));
2769
+ if (resolved) return resolved;
2770
+ const id = `unresolved:${reference}`;
2771
+ const existing = unresolved.get(id);
2772
+ if (existing) return existing;
2773
+ const entity = {
2774
+ id,
2775
+ kind: "unresolved-reference",
2776
+ name: reference,
2777
+ provenance: "declared",
2778
+ evidence: [evidence(input.path, lineStart)]
2779
+ };
2780
+ unresolved.set(id, entity);
2781
+ return entity;
2782
+ };
2783
+ var relationKey = (from, to, kind) => `${from}\0${to}\0${kind}`;
2784
+ var parseBlock = (input) => {
2785
+ const frontmatter = findFrontmatter(input.content);
2786
+ if (!frontmatter) {
2787
+ if (input.content.replace(/^\uFEFF/, "").startsWith("---")) {
2788
+ return {
2789
+ covers: [],
2790
+ relations: [],
2791
+ hasDocbridge: true,
2792
+ diagnostics: [diagnostic(input.path, "DOCBRIDGE_FRONTMATTER_MALFORMED", "Frontmatter must close with a line containing only ---.", 1)]
2793
+ };
2794
+ }
2795
+ return { covers: [], relations: [], diagnostics: [], hasDocbridge: false };
2796
+ }
2797
+ const { lines, end } = frontmatter;
2798
+ const docbridgeLine = lines.findIndex((line, index) => index > 0 && index < end && /^docbridge\s*:/.test(line));
2799
+ if (docbridgeLine < 0) return { covers: [], relations: [], diagnostics: [], hasDocbridge: false };
2800
+ const diagnostics = [];
2801
+ const covers = [];
2802
+ const relations = [];
2803
+ const inline = lines[docbridgeLine]?.slice("docbridge:".length).trim() ?? "";
2804
+ if (inline && inline !== "{}") addDiagnostic(diagnostics, input, "DOCBRIDGE_BLOCK_MALFORMED", "docbridge must be a nested frontmatter object.", docbridgeLine + 1);
2805
+ let section;
2806
+ let current;
2807
+ const finishRelation = () => {
2808
+ if (current) relations.push(current);
2809
+ current = void 0;
2810
+ };
2811
+ for (let index = docbridgeLine + 1; index < end; index += 1) {
2812
+ const raw = lines[index] ?? "";
2813
+ const trimmed = raw.trim();
2814
+ const line = index + 1;
2815
+ if (!trimmed || trimmed.startsWith("#")) continue;
2816
+ if (/\t/.test(raw)) {
2817
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_INDENTATION_INVALID", "docbridge indentation must use spaces.", line);
2818
+ continue;
2819
+ }
2820
+ if (!raw.startsWith(" ")) {
2821
+ finishRelation();
2822
+ section = void 0;
2823
+ continue;
2824
+ }
2825
+ if (/^ {2}[A-Za-z][A-Za-z0-9_-]*\s*:/.test(raw)) {
2826
+ finishRelation();
2827
+ const match = /^ {2}([A-Za-z][A-Za-z0-9_-]*)\s*:\s*(.*)$/.exec(raw);
2828
+ const key = match?.[1];
2829
+ const value = match?.[2] ?? "";
2830
+ if (key !== "covers" && key !== "relations") {
2831
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_FIELD_UNKNOWN", `Unknown docbridge field: ${key ?? "(missing)"}.`, line);
2832
+ section = void 0;
2833
+ } else if (key === "covers") {
2834
+ section = "covers";
2835
+ if (value) {
2836
+ const values = list(value);
2837
+ if (!values) addDiagnostic(diagnostics, input, "DOCBRIDGE_COVERS_INVALID", "covers must be a list of entity references.", line);
2838
+ else values.forEach((item) => covers.push({ value: item, line }));
2839
+ }
2840
+ } else {
2841
+ section = "relations";
2842
+ if (value) addDiagnostic(diagnostics, input, "DOCBRIDGE_RELATIONS_INVALID", "relations must be a list of relation objects.", line);
2843
+ }
2844
+ continue;
2845
+ }
2846
+ if (section === "covers" && /^ {4}-\s*/.test(raw)) {
2847
+ const value = scalar(raw.replace(/^ {4}-\s*/, ""));
2848
+ if (!value) addDiagnostic(diagnostics, input, "DOCBRIDGE_REFERENCE_MISSING", "covers entries must not be empty.", line);
2849
+ else covers.push({ value, line });
2850
+ continue;
2851
+ }
2852
+ if (section === "relations" && /^ {4}-\s*/.test(raw)) {
2853
+ finishRelation();
2854
+ const firstField = /^ {4}-\s*([A-Za-z][A-Za-z0-9_-]*)\s*:\s*(.*)$/.exec(raw);
2855
+ current = { startLine: line, endLine: line, fields: /* @__PURE__ */ new Set() };
2856
+ if (firstField?.[1]) {
2857
+ current.fields.add(firstField[1]);
2858
+ current[firstField[1]] = scalar(firstField[2] ?? "");
2859
+ } else if (raw.replace(/^ {4}-\s*/, "").trim()) {
2860
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_RELATION_INVALID", "Relation entries must be field mappings.", line);
2861
+ }
2862
+ continue;
2863
+ }
2864
+ if (section === "relations" && current && /^ {6}[A-Za-z][A-Za-z0-9_-]*\s*:/.test(raw)) {
2865
+ const field = /^ {6}([A-Za-z][A-Za-z0-9_-]*)\s*:\s*(.*)$/.exec(raw);
2866
+ const key = field?.[1];
2867
+ current.endLine = line;
2868
+ if (!key || !["from", "to", "kind", "detection"].includes(key)) {
2869
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_FIELD_UNKNOWN", `Unknown relation field: ${key ?? "(missing)"}.`, line);
2870
+ } else if (current.fields.has(key)) {
2871
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_FIELD_DUPLICATE", `Duplicate relation field: ${key}.`, line);
2872
+ } else {
2873
+ current.fields.add(key);
2874
+ current[key] = scalar(field?.[2] ?? "");
2875
+ }
2876
+ continue;
2877
+ }
2878
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_STRUCTURE_INVALID", `Invalid docbridge structure at line ${line}.`, line);
2879
+ }
2880
+ finishRelation();
2881
+ return { covers, relations, diagnostics, hasDocbridge: true };
2882
+ };
2883
+ var parseDocumentationDeclarations = (input, options) => {
2884
+ const parsed = parseBlock(input);
2885
+ if (!parsed.hasDocbridge) return { hasDocbridge: false, entities: [], relations: [], diagnostics: [] };
2886
+ const diagnostics = [...parsed.diagnostics];
2887
+ const unresolved = /* @__PURE__ */ new Map();
2888
+ const relations = [];
2889
+ const documentId = options.documentId ?? `document:${input.path}`;
2890
+ const relationClaims = /* @__PURE__ */ new Map();
2891
+ if (!parsed.covers.length && !parsed.relations.length) {
2892
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_CONTENT_MISSING", "docbridge must declare covers or relations.", 1);
2893
+ }
2894
+ for (const [index, cover] of parsed.covers.entries()) {
2895
+ const target = resolveEntity(cover.value, options.snapshot.entities, input, cover.line, unresolved);
2896
+ relations.push({
2897
+ id: `relation:declared:${input.path}:covers:${index}`,
2898
+ kind: "covers",
2899
+ from: documentId,
2900
+ to: target.id,
2901
+ provenance: "declared",
2902
+ evidence: [evidence(input.path, cover.line)]
2903
+ });
2904
+ }
2905
+ for (const [index, declaration] of parsed.relations.entries()) {
2906
+ const basePath = `relations[${index}]`;
2907
+ const missing = ["from", "to", "kind", "detection"].filter((field) => !declaration[field]);
2908
+ if (missing.length) {
2909
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_RELATION_FIELD_MISSING", `Relation is missing required field(s): ${missing.join(", ")}.`, declaration.startLine, declaration.endLine);
2910
+ continue;
2911
+ }
2912
+ const from = declaration.from;
2913
+ const to = declaration.to;
2914
+ const kind = declaration.kind;
2915
+ const detection = declaration.detection;
2916
+ if (!detectionValues.has(detection)) {
2917
+ addDiagnostic(diagnostics, input, "DOCBRIDGE_DETECTION_INVALID", `Invalid relation detection: ${detection}.`, declaration.startLine, declaration.endLine);
2918
+ continue;
2919
+ }
2920
+ const fromEntity = resolveEntity(from, options.snapshot.entities, input, declaration.startLine, unresolved);
2921
+ const toEntity = resolveEntity(to, options.snapshot.entities, input, declaration.startLine, unresolved);
2922
+ const key = relationKey(fromEntity.id, toEntity.id, kind);
2923
+ const previousDetection = relationClaims.get(key);
2924
+ if (previousDetection === detection) addDiagnostic(diagnostics, input, "DOCBRIDGE_DECLARATION_DUPLICATE", "Duplicate relation declaration.", declaration.startLine, declaration.endLine);
2925
+ if (previousDetection && previousDetection !== detection) addDiagnostic(diagnostics, input, "DOCBRIDGE_DECLARATION_CONFLICT", "Conflicting relation declarations use different detection values.", declaration.startLine, declaration.endLine);
2926
+ relationClaims.set(key, previousDetection ?? detection);
2927
+ relations.push({
2928
+ id: `relation:declared:${input.path}:${basePath}`,
2929
+ kind,
2930
+ from: fromEntity.id,
2931
+ to: toEntity.id,
2932
+ discriminator: detection,
2933
+ provenance: "declared",
2934
+ evidence: [evidence(input.path, declaration.startLine, declaration.endLine)],
2935
+ metadata: { detection }
2936
+ });
2937
+ }
2938
+ return {
2939
+ hasDocbridge: true,
2940
+ entities: [...unresolved.values()].sort((a, b) => a.id.localeCompare(b.id)),
2941
+ relations,
2942
+ diagnostics
2943
+ };
2944
+ };
2945
+ var applyDocumentationDeclarations = (snapshot, documents) => {
2946
+ const entities = new Map(snapshot.entities.map((entity) => [entity.id, entity]));
2947
+ const relations = new Map(snapshot.relations.map((relation) => [relation.id, relation]));
2948
+ const diagnostics = [];
2949
+ for (const document of documents) {
2950
+ const result = parseDocumentationDeclarations(document, { snapshot });
2951
+ diagnostics.push(...result.diagnostics);
2952
+ for (const entity of result.entities) entities.set(entity.id, entity);
2953
+ for (const relation of result.relations) relations.set(relation.id, relation);
2954
+ }
2955
+ const base = { ...snapshot, contentHash: "0".repeat(64), entities: [...entities.values()], relations: [...relations.values()] };
2956
+ return {
2957
+ snapshot: DiscoverySnapshotV1Schema.parse({ ...base, contentHash: contentHashForArtifactV1(base) }),
2958
+ diagnostics
2959
+ };
2960
+ };
2961
+
2962
+ // src/discovery/repository.ts
2963
+ import { execFileSync } from "child_process";
2436
2964
  import { existsSync as existsSync10, readFileSync as readFileSync9 } from "fs";
2437
- import { resolve as resolve7 } from "path";
2965
+ import { basename as basename4, dirname as dirname5, extname, join as join8, relative as relative6, resolve as resolve8, sep as sep7 } from "path";
2966
+ import * as ts from "typescript";
2967
+
2968
+ // src/safety/repository.ts
2969
+ import { lstatSync as lstatSync3, readdirSync as readdirSync3, realpathSync as realpathSync7, statSync as statSync4 } from "fs";
2970
+ import { isAbsolute as isAbsolute5, relative as relative5, resolve as resolve7, sep as sep6 } from "path";
2971
+ import { minimatch as minimatch4 } from "minimatch";
2972
+ var DEFAULT_SAFETY_EXCLUDES = ["**/.git/**", "**/node_modules/**", "**/dist/**", "**/build/**", "**/coverage/**", "**/.doc-bridge/**", "**/.env", "**/.env.*", "**/*secret*", "**/*credential*", "**/*.pem", "**/*.key"];
2973
+ var containedPath = (root, candidate) => {
2974
+ const projectRoot = realpathSync7.native(resolve7(root));
2975
+ const unresolved = resolve7(projectRoot, candidate);
2976
+ const unresolvedRelative = relative5(projectRoot, unresolved);
2977
+ if (isAbsolute5(unresolvedRelative) || unresolvedRelative === ".." || unresolvedRelative.startsWith(`..${sep6}`)) return void 0;
2978
+ try {
2979
+ const canonical = realpathSync7.native(unresolved);
2980
+ const canonicalRelative = relative5(projectRoot, canonical);
2981
+ return isAbsolute5(canonicalRelative) || canonicalRelative === ".." || canonicalRelative.startsWith(`..${sep6}`) ? void 0 : canonical;
2982
+ } catch {
2983
+ return unresolved;
2984
+ }
2985
+ };
2986
+ var safeWalkFiles = (root, options = {}) => {
2987
+ const projectRoot = resolve7(root);
2988
+ const extensions = options.extensions ?? [];
2989
+ const excludes = options.exclude ?? DEFAULT_SAFETY_EXCLUDES;
2990
+ const files = [];
2991
+ let bytes = 0;
2992
+ let reason;
2993
+ const started = Date.now();
2994
+ const matchesExclude = (path) => excludes.some((pattern) => minimatch4(path, pattern, { dot: true }));
2995
+ const visit = (directory) => {
2996
+ if (reason) return;
2997
+ if (options.maxTimeMs !== void 0 && Date.now() - started >= options.maxTimeMs) {
2998
+ reason = `Repository scan exceeded the ${options.maxTimeMs} ms time limit.`;
2999
+ return;
3000
+ }
3001
+ if (options.maxMemoryMb !== void 0 && process.memoryUsage().heapUsed > options.maxMemoryMb * 1024 * 1024) {
3002
+ reason = `Repository scan exceeded the ${options.maxMemoryMb} MiB memory limit.`;
3003
+ return;
3004
+ }
3005
+ let entries;
3006
+ try {
3007
+ entries = readdirSync3(directory);
3008
+ } catch {
3009
+ return;
3010
+ }
3011
+ for (const name of entries.sort()) {
3012
+ const absolute = resolve7(directory, name);
3013
+ const relativePath3 = relative5(projectRoot, absolute).split(sep6).join("/");
3014
+ if (matchesExclude(relativePath3) || name === ".git") continue;
3015
+ let stats;
3016
+ try {
3017
+ stats = lstatSync3(absolute);
3018
+ } catch {
3019
+ continue;
3020
+ }
3021
+ if (stats.isSymbolicLink()) continue;
3022
+ if (stats.isDirectory()) {
3023
+ visit(absolute);
3024
+ if (reason) return;
3025
+ continue;
3026
+ }
3027
+ if (!stats.isFile() || extensions.length > 0 && !extensions.some((extension) => name.endsWith(extension))) continue;
3028
+ if (files.length >= (options.maxFiles ?? 1e4)) {
3029
+ reason = `Repository scan exceeded the ${options.maxFiles ?? 1e4} file limit.`;
3030
+ return;
3031
+ }
3032
+ bytes += statSync4(absolute).size;
3033
+ if (options.maxBytes !== void 0 && bytes > options.maxBytes) {
3034
+ reason = `Repository scan exceeded the ${options.maxBytes} byte limit.`;
3035
+ return;
3036
+ }
3037
+ files.push(absolute);
3038
+ }
3039
+ };
3040
+ visit(projectRoot);
3041
+ return { files: files.sort(), incomplete: reason !== void 0, ...reason ? { reason } : {} };
3042
+ };
3043
+ var SECRET_PATTERNS = [
3044
+ /\b(?:sk|pk)[_-](?:live|test)[_-][A-Za-z0-9_-]{12,}\b/g,
3045
+ /\b(?:ghp|github_pat|xox[baprs])_[A-Za-z0-9_-]{12,}\b/g,
3046
+ /\bAKIA[0-9A-Z]{16}\b/g,
3047
+ /(?:password|passwd|secret|token|api[_-]?key)\s*[:=]\s*["']?[^\s,"']+/gi
3048
+ ];
3049
+ var redactSecrets = (value) => SECRET_PATTERNS.reduce((result, pattern) => result.replace(pattern, "[REDACTED]"), value);
3050
+ var redactValue = (value) => Array.isArray(value) ? value.map(redactValue) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).map(([key, item]) => [key, redactValue(item)])) : typeof value === "string" ? redactSecrets(value) : value;
3051
+
3052
+ // src/discovery/repository.ts
3053
+ var SOURCE_EXTENSIONS = [".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts"];
3054
+ var DOCUMENT_EXTENSIONS = [".md", ".mdx"];
3055
+ var DEFAULT_MAX_FILES2 = 1e4;
3056
+ var EMPTY_HASH = "0".repeat(64);
3057
+ var isRecord2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
3058
+ var readJson = (path) => {
3059
+ try {
3060
+ const value = JSON.parse(readFileSync9(path, "utf8"));
3061
+ return isRecord2(value) ? { value } : { error: "JSON root is not an object" };
3062
+ } catch (error) {
3063
+ return { error: error instanceof Error ? error.message : String(error) };
3064
+ }
3065
+ };
3066
+ var relativePath = (root, path) => toPosix(relative6(root, path)) || ".";
3067
+ var MAX_ID_LENGTH = 256;
3068
+ var ID_HASH_LENGTH = 32;
3069
+ var entityId = (kind, value) => {
3070
+ const fullId = `${kind}:${value}`;
3071
+ if (fullId.length <= MAX_ID_LENGTH) return fullId;
3072
+ const suffix = `:${sha256NormalizedV1(fullId).slice(0, ID_HASH_LENGTH)}`;
3073
+ return `${fullId.slice(0, MAX_ID_LENGTH - suffix.length)}${suffix}`;
3074
+ };
3075
+ var lineEvidence = (source, root, path, lineStart, lineEnd) => ({
3076
+ source,
3077
+ path: relativePath(root, path),
3078
+ ...lineStart !== void 0 ? { lineStart } : {},
3079
+ ...lineEnd !== void 0 ? { lineEnd } : {}
3080
+ });
3081
+ var firstLineContaining = (text, pattern) => {
3082
+ const line = text.split(/\r?\n/).findIndex((value) => value.includes(pattern));
3083
+ return line >= 0 ? line + 1 : void 0;
3084
+ };
3085
+ var packageName = (manifest, fallback) => typeof manifest.name === "string" && manifest.name.length > 0 ? manifest.name : fallback || void 0;
3086
+ var workspacePatterns = (root, rootManifest) => {
3087
+ const fromPackageJson = rootManifest?.workspaces;
3088
+ if (Array.isArray(fromPackageJson)) return fromPackageJson.filter((value) => typeof value === "string");
3089
+ if (isRecord2(fromPackageJson) && Array.isArray(fromPackageJson.packages)) {
3090
+ return fromPackageJson.packages.filter((value) => typeof value === "string");
3091
+ }
3092
+ const workspacePath = join8(root, "pnpm-workspace.yaml");
3093
+ if (!existsSync10(workspacePath)) return [];
3094
+ const patterns = [];
3095
+ let inPackages = false;
3096
+ for (const line of readFileSync9(workspacePath, "utf8").split(/\r?\n/)) {
3097
+ const trimmed = line.trim();
3098
+ if (trimmed === "packages:") {
3099
+ inPackages = true;
3100
+ continue;
3101
+ }
3102
+ if (!inPackages) continue;
3103
+ if (trimmed.startsWith("- ")) {
3104
+ patterns.push(trimmed.slice(2).trim().replace(/^['"]|['"]$/g, ""));
3105
+ continue;
3106
+ }
3107
+ if (trimmed && !trimmed.startsWith("#")) inPackages = false;
3108
+ }
3109
+ return patterns;
3110
+ };
3111
+ var discoverPackages = (root, rootManifest, config) => {
3112
+ const packages = [];
3113
+ const coverage = [];
3114
+ const rootManifestPath = join8(root, "package.json");
3115
+ if (rootManifest) {
3116
+ const name = packageName(rootManifest, "");
3117
+ packages.push({
3118
+ id: entityId("package", packageName(rootManifest, "root") ?? "root"),
3119
+ ...name ? { name } : {},
3120
+ path: ".",
3121
+ absPath: root,
3122
+ manifestPath: rootManifestPath,
3123
+ manifest: rootManifest
3124
+ });
3125
+ }
3126
+ const configuredPatterns = config?.routing?.options?.packages;
3127
+ const patterns = configuredPatterns?.length ? [...configuredPatterns] : workspacePatterns(root, rootManifest);
3128
+ if (!patterns.length) {
3129
+ coverage.push({ status: "complete" });
3130
+ return { packages, coverage };
3131
+ }
3132
+ const dirs = expandWorkspaceGlobs(root, patterns);
3133
+ for (const absPath of dirs) {
3134
+ const manifestPath = join8(absPath, "package.json");
3135
+ const parsed = readJson(manifestPath);
3136
+ if (!parsed.value) {
3137
+ coverage.push({ status: "partial", reason: `${relativePath(root, manifestPath)}: ${parsed.error ?? "invalid package.json"}` });
3138
+ continue;
3139
+ }
3140
+ const path = relativePath(root, absPath);
3141
+ const name = packageName(parsed.value, path);
3142
+ const id = entityId("package", name ?? path);
3143
+ const duplicate = packages.find((pkg) => pkg.id === id);
3144
+ if (duplicate && duplicate.absPath !== absPath) {
3145
+ throw new Error(`Package identity collision for "${id}": "${duplicate.path}" and "${path}".`);
3146
+ }
3147
+ if (!duplicate) packages.push({ id, ...name ? { name } : {}, path, absPath, manifestPath, manifest: parsed.value });
3148
+ }
3149
+ coverage.push({ status: "complete" });
3150
+ return { packages: packages.sort((a, b) => a.id.localeCompare(b.id)), coverage };
3151
+ };
3152
+ var packageForModule = (packages, absPath) => [...packages].filter((pkg) => absPath === pkg.absPath || absPath.startsWith(`${pkg.absPath}${sep7}`)).sort((a, b) => b.absPath.length - a.absPath.length)[0];
3153
+ var readCompilerOptions = (root) => {
3154
+ const configPath = ts.findConfigFile(root, ts.sys.fileExists, "tsconfig.json");
3155
+ if (!configPath) return { options: {} };
3156
+ const parsed = ts.readConfigFile(configPath, ts.sys.readFile);
3157
+ if (parsed.error) return { options: {}, error: ts.flattenDiagnosticMessageText(parsed.error.messageText, "\n") };
3158
+ const config = ts.parseJsonConfigFileContent(parsed.config, ts.sys, dirname5(configPath));
3159
+ if (config.errors.length) {
3160
+ return {
3161
+ options: config.options,
3162
+ error: ts.flattenDiagnosticMessageText(config.errors[0]?.messageText ?? "Invalid tsconfig", "\n")
3163
+ };
3164
+ }
3165
+ return { options: config.options };
3166
+ };
3167
+ var scriptKind = (path) => {
3168
+ switch (extname(path)) {
3169
+ case ".js":
3170
+ return ts.ScriptKind.JS;
3171
+ case ".jsx":
3172
+ return ts.ScriptKind.JSX;
3173
+ case ".mjs":
3174
+ return ts.ScriptKind.JS;
3175
+ case ".cjs":
3176
+ return ts.ScriptKind.JS;
3177
+ case ".ts":
3178
+ return ts.ScriptKind.TS;
3179
+ case ".tsx":
3180
+ return ts.ScriptKind.TSX;
3181
+ case ".mts":
3182
+ return ts.ScriptKind.TS;
3183
+ case ".cts":
3184
+ return ts.ScriptKind.TS;
3185
+ default:
3186
+ return ts.ScriptKind.Unknown;
3187
+ }
3188
+ };
3189
+ var nodeEvidence = (root, path, sourceFile, node) => {
3190
+ const start = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
3191
+ const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd()).line + 1;
3192
+ return lineEvidence("code", root, path, start, end);
3193
+ };
3194
+ var isExported = (node) => {
3195
+ const modifiers = ts.canHaveModifiers(node) ? ts.getModifiers(node) : void 0;
3196
+ return modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword) ?? false;
3197
+ };
3198
+ var exportedNames = (sourceFile) => {
3199
+ const names = /* @__PURE__ */ new Set();
3200
+ const addDeclarationName = (node) => {
3201
+ if (!isExported(node)) return;
3202
+ const name = ts.getNameOfDeclaration(node);
3203
+ if (name && ts.isIdentifier(name)) names.add(name.text);
3204
+ };
3205
+ const visit = (node) => {
3206
+ if (ts.isExportDeclaration(node)) {
3207
+ if (!node.exportClause) names.add("*");
3208
+ else if (ts.isNamedExports(node.exportClause)) {
3209
+ for (const element of node.exportClause.elements) names.add(element.name.text);
3210
+ }
3211
+ } else if (ts.isExportAssignment(node)) {
3212
+ names.add("default");
3213
+ } else if (ts.isClassDeclaration(node) || ts.isFunctionDeclaration(node) || ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isEnumDeclaration(node) || ts.isModuleDeclaration(node)) {
3214
+ addDeclarationName(node);
3215
+ } else if (ts.isVariableStatement(node) && isExported(node)) {
3216
+ for (const declaration of node.declarationList.declarations) {
3217
+ if (ts.isIdentifier(declaration.name)) names.add(declaration.name.text);
3218
+ }
3219
+ }
3220
+ ts.forEachChild(node, visit);
3221
+ };
3222
+ visit(sourceFile);
3223
+ return [...names].sort();
3224
+ };
3225
+ var moduleReferences = (root, path, sourceFile) => {
3226
+ const references = [];
3227
+ let hasDynamic = false;
3228
+ let hasRuntimeWiring = false;
3229
+ const addReference = (specifier, kind, node) => {
3230
+ references.push({ specifier: specifier.text, kind, evidence: nodeEvidence(root, path, sourceFile, node) });
3231
+ };
3232
+ const visit = (node) => {
3233
+ if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
3234
+ addReference(node.moduleSpecifier, "imports", node);
3235
+ } else if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
3236
+ addReference(node.moduleSpecifier, "re-exports", node);
3237
+ } else if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference) && ts.isStringLiteral(node.moduleReference.expression)) {
3238
+ addReference(node.moduleReference.expression, "imports", node);
3239
+ } else if (ts.isCallExpression(node)) {
3240
+ if (node.expression.kind === ts.SyntaxKind.ImportKeyword) {
3241
+ if (!node.arguments[0] || !ts.isStringLiteralLike(node.arguments[0])) hasDynamic = true;
3242
+ } else if (ts.isIdentifier(node.expression) && node.expression.text === "require") {
3243
+ const argument = node.arguments[0];
3244
+ if (argument && ts.isStringLiteralLike(argument)) addReference(argument, "imports", node);
3245
+ else hasDynamic = true;
3246
+ } else if (ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "register") {
3247
+ hasRuntimeWiring = true;
3248
+ }
3249
+ }
3250
+ ts.forEachChild(node, visit);
3251
+ };
3252
+ visit(sourceFile);
3253
+ return {
3254
+ references,
3255
+ exports: exportedNames(sourceFile),
3256
+ hasDynamic,
3257
+ hasRuntimeWiring
3258
+ };
3259
+ };
3260
+ var resolveRelativeModule = (specifier, containingFile, modulePaths) => {
3261
+ const base = resolve8(dirname5(containingFile), specifier);
3262
+ const extension = extname(base);
3263
+ const extensionlessBase = extension ? base.slice(0, -extension.length) : base;
3264
+ const candidates = [
3265
+ base,
3266
+ ...SOURCE_EXTENSIONS.map((extension2) => `${base}${extension2}`),
3267
+ ...SOURCE_EXTENSIONS.map((extension2) => join8(base, `index${extension2}`)),
3268
+ ...SOURCE_EXTENSIONS.map((extension2) => `${extensionlessBase}${extension2}`),
3269
+ ...SOURCE_EXTENSIONS.map((extension2) => join8(extensionlessBase, `index${extension2}`))
3270
+ ];
3271
+ return candidates.map((candidate) => modulePaths.get(resolve8(candidate))).find(Boolean);
3272
+ };
3273
+ var resolveReference = (reference, containingFile, modules, packages, compilerOptions) => {
3274
+ if (reference.specifier.startsWith(".") || reference.specifier.startsWith("/")) {
3275
+ const relativeTarget = resolveRelativeModule(reference.specifier, containingFile, modules);
3276
+ return relativeTarget ? { targetId: relativeTarget.entityId } : void 0;
3277
+ }
3278
+ const packageTarget = [...packages].filter((pkg) => pkg.name && (reference.specifier === pkg.name || reference.specifier.startsWith(`${pkg.name}/`))).sort((a, b) => (b.name?.length ?? 0) - (a.name?.length ?? 0))[0];
3279
+ if (packageTarget) return { targetId: packageTarget.id };
3280
+ const resolved = ts.resolveModuleName(reference.specifier, containingFile, compilerOptions, ts.sys).resolvedModule?.resolvedFileName;
3281
+ const resolvedTarget = resolved ? modules.get(resolve8(resolved)) : void 0;
3282
+ if (resolvedTarget) return { targetId: resolvedTarget.entityId };
3283
+ return { targetId: entityId("external", reference.specifier) };
3284
+ };
3285
+ var dependencyEntries = (manifest) => {
3286
+ const sections = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
3287
+ return sections.flatMap((type) => {
3288
+ const value = manifest[type];
3289
+ if (!isRecord2(value)) return [];
3290
+ return Object.keys(value).sort().map((name) => ({ name, type }));
3291
+ });
3292
+ };
3293
+ var sourceRevision = (root, files) => {
3294
+ const contentRevision = () => ({
3295
+ value: sha256NormalizedV1(
3296
+ files.map((path) => ({
3297
+ path: relativePath(root, path),
3298
+ contentHash: sha256NormalizedV1(readFileSync9(path, "utf8"))
3299
+ }))
3300
+ ),
3301
+ kind: "content"
3302
+ });
3303
+ try {
3304
+ const status = execFileSync("git", ["status", "--porcelain", "--untracked-files=all"], {
3305
+ cwd: root,
3306
+ encoding: "utf8",
3307
+ stdio: ["ignore", "pipe", "ignore"]
3308
+ }).trim();
3309
+ if (status) return contentRevision();
3310
+ const value = execFileSync("git", ["rev-parse", "HEAD"], { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
3311
+ if (value) return { value, kind: "git" };
3312
+ } catch {
3313
+ }
3314
+ return contentRevision();
3315
+ };
3316
+ var hasPackageManagerMetadata = (root, rootManifest) => Boolean(
3317
+ rootManifest?.packageManager || existsSync10(join8(root, "pnpm-lock.yaml")) || existsSync10(join8(root, "pnpm-workspace.yaml")) || existsSync10(join8(root, "yarn.lock")) || existsSync10(join8(root, "bun.lock")) || existsSync10(join8(root, "bun.lockb")) || existsSync10(join8(root, "package-lock.json"))
3318
+ );
3319
+ var artifact = (root, config, files, entities, relations, coverage) => {
3320
+ const revision = sourceRevision(root, files);
3321
+ const base = {
3322
+ type: "discovery-snapshot",
3323
+ schemaVersion: 1,
3324
+ contentHash: EMPTY_HASH,
3325
+ contentHashAlgo: "sha256-normalized-v1",
3326
+ project: { name: entities.find((entity) => entity.kind === "package" && entity.path === ".")?.name ?? basename4(root), root: "." },
3327
+ sourceRevision: revision.value,
3328
+ sourceRevisionKind: revision.kind,
3329
+ configurationHash: sha256NormalizedV1(config ?? {}),
3330
+ pipelineVersion: "1.0.0",
3331
+ analyzerVersions: { repository: "1.0.0", "js-ts": "1.0.0" },
3332
+ entities: [...entities].sort((a, b) => a.id.localeCompare(b.id)),
3333
+ relations: [...relations].sort((a, b) => a.id.localeCompare(b.id)),
3334
+ coverage: [...coverage]
3335
+ };
3336
+ return DiscoverySnapshotV1Schema.parse({ ...base, contentHash: contentHashForArtifactV1(base) });
3337
+ };
3338
+ var discoverRepository = (opts = {}) => {
3339
+ const root = resolve8(opts.root ?? process.cwd());
3340
+ const maxFiles = opts.maxFiles ?? DEFAULT_MAX_FILES2;
3341
+ const safety = opts.config?.safety;
3342
+ const maxBytes = opts.maxBytes ?? safety?.maxBytes;
3343
+ const safeOptions = {
3344
+ exclude: [...DEFAULT_SAFETY_EXCLUDES, ...safety?.exclude ?? []],
3345
+ maxFiles,
3346
+ ...maxBytes !== void 0 ? { maxBytes } : {},
3347
+ ...safety?.maxTimeMs !== void 0 ? { maxTimeMs: safety.maxTimeMs } : {},
3348
+ ...safety?.maxMemoryMb !== void 0 ? { maxMemoryMb: safety.maxMemoryMb } : {}
3349
+ };
3350
+ const rootManifestPath = join8(root, "package.json");
3351
+ const rootManifest = readJson(rootManifestPath).value;
3352
+ const packageResult = discoverPackages(root, rootManifest, opts.config);
3353
+ const sourceWalk = safeWalkFiles(root, { extensions: SOURCE_EXTENSIONS, ...safeOptions });
3354
+ const documentWalk = safeWalkFiles(root, { extensions: DOCUMENT_EXTENSIONS, ...safeOptions });
3355
+ const configWalk = safeWalkFiles(root, { extensions: [".json", ".yaml", ".yml", ".js", ".ts"], ...safeOptions });
3356
+ const sourcePaths = sourceWalk.files;
3357
+ const documentPaths = documentWalk.files;
3358
+ const configPaths = configWalk.files.filter((path) => /(?:^|\/)(?:tsconfig|jsconfig|vite\.config|webpack\.config|rollup\.config|next\.config|jest\.config|eslint\.config|vitest\.config)/.test(relativePath(root, path)));
3359
+ const allFiles = [...new Set([rootManifestPath, ...sourcePaths, ...documentPaths, ...configPaths].filter(existsSync10))].sort();
3360
+ const entities = /* @__PURE__ */ new Map();
3361
+ const relations = /* @__PURE__ */ new Map();
3362
+ const addEntity = (entity) => {
3363
+ const existing = entities.get(entity.id);
3364
+ if (existing && (existing.kind !== entity.kind || existing.path !== entity.path)) throw new Error(`Entity identity collision for "${entity.id}".`);
3365
+ entities.set(entity.id, existing ?? entity);
3366
+ };
3367
+ const addRelation = (relation) => {
3368
+ const existing = relations.get(relation.id);
3369
+ if (!existing) {
3370
+ relations.set(relation.id, relation);
3371
+ return;
3372
+ }
3373
+ const evidence2 = new Map(
3374
+ [...existing.evidence, ...relation.evidence].map((item) => [
3375
+ `${item.path}:${item.lineStart ?? ""}:${item.lineEnd ?? ""}:${item.source}`,
3376
+ item
3377
+ ])
3378
+ );
3379
+ relations.set(relation.id, { ...existing, evidence: [...evidence2.values()] });
3380
+ };
3381
+ for (const pkg of packageResult.packages) {
3382
+ const text = readFileSync9(pkg.manifestPath, "utf8");
3383
+ addEntity({ id: pkg.id, kind: "package", name: pkg.name ?? pkg.path, path: pkg.path, provenance: "observed", evidence: [lineEvidence("configuration", root, pkg.manifestPath, firstLineContaining(text, '"name"'))] });
3384
+ }
3385
+ const modules = /* @__PURE__ */ new Map();
3386
+ for (const absPath of sourcePaths) {
3387
+ const path = relativePath(root, absPath);
3388
+ const pkg = packageForModule(packageResult.packages, absPath);
3389
+ const id = entityId("module", path);
3390
+ const text = readFileSync9(absPath, "utf8");
3391
+ const sourceFile = ts.createSourceFile(absPath, text, ts.ScriptTarget.Latest, true, scriptKind(absPath));
3392
+ const exports = exportedNames(sourceFile);
3393
+ modules.set(resolve8(absPath), { absPath, path, entityId: id, ...pkg ? { packageId: pkg.id } : {} });
3394
+ addEntity({ id, kind: "module", name: basename4(absPath), path, provenance: "observed", evidence: [lineEvidence("code", root, absPath, 1, sourceFile.getLineAndCharacterOfPosition(sourceFile.getEnd()).line + 1)], ...exports.length ? { metadata: { exports, test: /(?:\.test|\.spec|__tests__)/.test(path) } } : {} });
3395
+ if (pkg) addRelation({ id: entityId("relation", `${pkg.id}:contains:${id}`), kind: "contains", from: pkg.id, to: id, provenance: "observed", evidence: [lineEvidence("code", root, absPath, 1)] });
3396
+ }
3397
+ for (const absPath of documentPaths) {
3398
+ const path = relativePath(root, absPath);
3399
+ addEntity({ id: entityId("document", path), kind: "document", name: basename4(absPath), path, provenance: "observed", evidence: [lineEvidence("documentation", root, absPath, 1)] });
3400
+ }
3401
+ const compiler = readCompilerOptions(root);
3402
+ const coverage = [
3403
+ ...[sourceWalk, documentWalk, configWalk].flatMap((walk, index) => walk.incomplete ? [{ analyzer: "repository", scope: `limits:${["source", "documentation", "configuration"][index]}`, status: "partial", reason: walk.reason }] : []),
3404
+ { analyzer: "repository", scope: "package-manager", status: hasPackageManagerMetadata(root, rootManifest) ? "complete" : "partial", ...!hasPackageManagerMetadata(root, rootManifest) ? { reason: `No package manager metadata found; default helper would fall back to ${detectPackageManager(root)}.` } : {} },
3405
+ { analyzer: "repository", scope: "workspace-packages", status: packageResult.coverage.some((item) => item.status === "partial") ? "partial" : "complete", ...packageResult.coverage.find((item) => item.reason)?.reason ? { reason: packageResult.coverage.find((item) => item.reason)?.reason } : {} },
3406
+ { analyzer: "js-ts", scope: "static-imports-and-exports", status: compiler.error ? "partial" : "complete", ...compiler.error ? { reason: compiler.error } : {} },
3407
+ { analyzer: "js-ts", scope: "dynamic-imports", status: "not-analyzed", reason: "Dynamic import expressions and non-literal require calls are not resolved." },
3408
+ { analyzer: "js-ts", scope: "runtime-wiring", status: "not-analyzed", reason: "Reflection, dependency injection and runtime wiring are not inferred." },
3409
+ { analyzer: "js-ts", scope: "generated-code", status: "not-analyzed", reason: "Generated code is not interpreted as source architecture." }
3410
+ ];
3411
+ for (const pkg of packageResult.packages) {
3412
+ const text = readFileSync9(pkg.manifestPath, "utf8");
3413
+ for (const dependency of dependencyEntries(pkg.manifest)) {
3414
+ const target = packageResult.packages.find((candidate) => candidate.name === dependency.name)?.id ?? entityId("external", dependency.name);
3415
+ if (!entities.has(target)) addEntity({ id: target, kind: "external", name: dependency.name, provenance: "observed", evidence: [lineEvidence("configuration", root, pkg.manifestPath, firstLineContaining(text, `"${dependency.name}"`))] });
3416
+ addRelation({ id: entityId("relation", `${pkg.id}:depends-on:${target}:${dependency.type}`), kind: "depends-on", from: pkg.id, to: target, provenance: "observed", evidence: [lineEvidence("configuration", root, pkg.manifestPath, firstLineContaining(text, `"${dependency.name}"`))], metadata: { dependencyType: dependency.type } });
3417
+ }
3418
+ }
3419
+ for (const module of modules.values()) {
3420
+ const text = readFileSync9(module.absPath, "utf8");
3421
+ const sourceFile = ts.createSourceFile(module.absPath, text, ts.ScriptTarget.Latest, true, scriptKind(module.absPath));
3422
+ const references = moduleReferences(root, module.absPath, sourceFile);
3423
+ for (const reference of references.references) {
3424
+ const target = resolveReference(reference, module.absPath, modules, packageResult.packages, compiler.options);
3425
+ if (!target) continue;
3426
+ if (!entities.has(target.targetId)) {
3427
+ const externalName = target.targetId.replace(/^external:/, "");
3428
+ addEntity({ id: target.targetId, kind: "external", name: externalName, provenance: "observed", evidence: [reference.evidence] });
3429
+ }
3430
+ addRelation({ id: entityId("relation", `${module.entityId}:${reference.kind}:${target.targetId}`), kind: reference.kind, from: module.entityId, to: target.targetId, provenance: "observed", evidence: [reference.evidence] });
3431
+ }
3432
+ if (references.hasDynamic) coverage.push({ analyzer: "js-ts", scope: `dynamic-imports:${module.path}`, status: "not-analyzed", reason: "A dynamic import or non-literal require was found.", evidence: [lineEvidence("code", root, module.absPath)] });
3433
+ if (references.hasRuntimeWiring) coverage.push({ analyzer: "js-ts", scope: `runtime-wiring:${module.path}`, status: "not-analyzed", reason: "A possible runtime registration/wiring call was found.", evidence: [lineEvidence("code", root, module.absPath)] });
3434
+ }
3435
+ return artifact(root, opts.config, allFiles, [...entities.values()], [...relations.values()], coverage);
3436
+ };
3437
+
3438
+ // src/federation/llms.ts
3439
+ import { existsSync as existsSync11, readFileSync as readFileSync10 } from "fs";
3440
+ import { resolve as resolve9 } from "path";
2438
3441
 
2439
3442
  // src/query/search.ts
2440
3443
  var tokenize = (value) => value.toLowerCase().split(/[^a-z0-9@/_-]+/).filter((t) => t.length >= 2);
@@ -2563,9 +3566,9 @@ var defaultFetchText = async (url) => {
2563
3566
  var sourceText = async (root, source, fetchText) => {
2564
3567
  try {
2565
3568
  if (/^https?:\/\//.test(source)) return await fetchText(source);
2566
- const path = resolve7(root, source);
2567
- if (!existsSync10(path)) return null;
2568
- return readFileSync9(path, "utf8");
3569
+ const path = resolve9(root, source);
3570
+ if (!existsSync11(path)) return null;
3571
+ return readFileSync10(path, "utf8");
2569
3572
  } catch {
2570
3573
  return null;
2571
3574
  }
@@ -2650,16 +3653,16 @@ var retrieveHybridChunks = async (root, config, index, query, options = {}) => {
2650
3653
  };
2651
3654
 
2652
3655
  // src/gates/run-gates.ts
2653
- import { readFileSync as readFileSync11 } from "fs";
3656
+ import { readFileSync as readFileSync12 } from "fs";
2654
3657
 
2655
3658
  // src/query/load-index.ts
2656
- import { existsSync as existsSync11, readFileSync as readFileSync10 } from "fs";
2657
- import { join as join8, resolve as resolve8 } from "path";
3659
+ import { existsSync as existsSync12, readFileSync as readFileSync11 } from "fs";
3660
+ import { join as join9, resolve as resolve10 } from "path";
2658
3661
 
2659
3662
  // src/schemas/agent-handoff.ts
2660
- import { z as z3 } from "zod";
3663
+ import { z as z4 } from "zod";
2661
3664
  var HANDOFF_SCHEMA_VERSION = 1;
2662
- var HandoffTargetTypeSchema = z3.enum([
3665
+ var HandoffTargetTypeSchema = z4.enum([
2663
3666
  "package",
2664
3667
  "module",
2665
3668
  "app",
@@ -2670,133 +3673,133 @@ var HandoffTargetTypeSchema = z3.enum([
2670
3673
  "change",
2671
3674
  "search"
2672
3675
  ]);
2673
- var HandoffTargetSchema = z3.object({
3676
+ var HandoffTargetSchema = z4.object({
2674
3677
  type: HandoffTargetTypeSchema,
2675
- id: z3.string().min(1).max(256),
2676
- path: z3.string().min(1).max(512).optional(),
2677
- group: z3.string().min(1).max(128).optional(),
2678
- layer: z3.string().min(1).max(32).optional()
3678
+ id: z4.string().min(1).max(256),
3679
+ path: z4.string().min(1).max(512).optional(),
3680
+ group: z4.string().min(1).max(128).optional(),
3681
+ layer: z4.string().min(1).max(32).optional()
2679
3682
  }).strict();
2680
- var HandoffBridgeSchema = z3.object({
2681
- humanDoc: z3.enum(["linked", "missing", "external"]),
2682
- action: z3.string().min(1).max(256).optional(),
2683
- bootstrap: z3.string().min(1).max(256).optional()
3683
+ var HandoffBridgeSchema = z4.object({
3684
+ humanDoc: z4.enum(["linked", "missing", "external"]),
3685
+ action: z4.string().min(1).max(256).optional(),
3686
+ bootstrap: z4.string().min(1).max(256).optional()
2684
3687
  }).strict();
2685
- var AgentHandoffV1Schema = z3.object({
2686
- type: z3.literal("agent-handoff"),
2687
- schemaVersion: z3.literal(HANDOFF_SCHEMA_VERSION).default(HANDOFF_SCHEMA_VERSION),
2688
- source: z3.string().min(1).max(512),
3688
+ var AgentHandoffV1Schema = z4.object({
3689
+ type: z4.literal("agent-handoff"),
3690
+ schemaVersion: z4.literal(HANDOFF_SCHEMA_VERSION).default(HANDOFF_SCHEMA_VERSION),
3691
+ source: z4.string().min(1).max(512),
2689
3692
  target: HandoffTargetSchema,
2690
- startHere: z3.string().min(1).max(512),
2691
- readBeforeEditing: z3.array(z3.string().min(1).max(512)).max(64),
2692
- editRoots: z3.array(z3.string().min(1).max(512)).max(32),
2693
- checks: z3.array(z3.string().min(1).max(256)).max(32),
2694
- humanDoc: z3.string().min(1).max(512).nullable().optional(),
3693
+ startHere: z4.string().min(1).max(512),
3694
+ readBeforeEditing: z4.array(z4.string().min(1).max(512)).max(64),
3695
+ editRoots: z4.array(z4.string().min(1).max(512)).max(32),
3696
+ checks: z4.array(z4.string().min(1).max(256)).max(32),
3697
+ humanDoc: z4.string().min(1).max(512).nullable().optional(),
2695
3698
  bridge: HandoffBridgeSchema.optional(),
2696
- playbookPatterns: z3.array(z3.string().url()).max(16).optional(),
2697
- notes: z3.array(z3.string().min(1).max(1024)).max(16)
3699
+ playbookPatterns: z4.array(z4.string().url()).max(16).optional(),
3700
+ notes: z4.array(z4.string().min(1).max(1024)).max(16)
2698
3701
  }).strict();
2699
3702
  var AgentHandoffLegacySchema = AgentHandoffV1Schema.omit({ schemaVersion: true }).extend({
2700
- schemaVersion: z3.literal(HANDOFF_SCHEMA_VERSION).optional()
3703
+ schemaVersion: z4.literal(HANDOFF_SCHEMA_VERSION).optional()
2701
3704
  });
2702
- var AgentSearchMatchSchema = z3.object({
2703
- type: z3.string().min(1).max(64),
2704
- id: z3.string().min(1).max(256),
2705
- path: z3.string().min(1).max(512),
2706
- summary: z3.string().max(2048).optional(),
2707
- score: z3.number().optional(),
2708
- refs: z3.number().int().nonnegative().optional()
3705
+ var AgentSearchMatchSchema = z4.object({
3706
+ type: z4.string().min(1).max(64),
3707
+ id: z4.string().min(1).max(256),
3708
+ path: z4.string().min(1).max(512),
3709
+ summary: z4.string().max(2048).optional(),
3710
+ score: z4.number().optional(),
3711
+ refs: z4.number().int().nonnegative().optional()
2709
3712
  }).strict();
2710
- var AgentSearchV1Schema = z3.object({
2711
- type: z3.literal("agent-search"),
2712
- schemaVersion: z3.literal(HANDOFF_SCHEMA_VERSION).default(HANDOFF_SCHEMA_VERSION),
2713
- source: z3.string().min(1).max(512),
2714
- term: z3.string().min(1).max(512),
2715
- count: z3.number().int().nonnegative(),
3713
+ var AgentSearchV1Schema = z4.object({
3714
+ type: z4.literal("agent-search"),
3715
+ schemaVersion: z4.literal(HANDOFF_SCHEMA_VERSION).default(HANDOFF_SCHEMA_VERSION),
3716
+ source: z4.string().min(1).max(512),
3717
+ term: z4.string().min(1).max(512),
3718
+ count: z4.number().int().nonnegative(),
2716
3719
  bestMatch: AgentSearchMatchSchema.nullable(),
2717
- matches: z3.array(AgentSearchMatchSchema).max(32),
2718
- nextCommands: z3.array(z3.string().min(1).max(512)).max(16)
3720
+ matches: z4.array(AgentSearchMatchSchema).max(32),
3721
+ nextCommands: z4.array(z4.string().min(1).max(512)).max(16)
2719
3722
  }).strict();
2720
3723
  var normalizeAgentHandoff = (input) => AgentHandoffV1Schema.parse(AgentHandoffLegacySchema.parse(input));
2721
3724
 
2722
3725
  // src/schemas/doc-bridge-index.ts
2723
- import { z as z4 } from "zod";
3726
+ import { z as z5 } from "zod";
2724
3727
  var INDEX_SCHEMA_VERSION = 1;
2725
- var ContentHashAlgoSchema = z4.literal("sha256-normalized-v1");
2726
- var EcosystemPropertySchema = z4.object({
2727
- id: z4.string().min(1).max(64),
2728
- name: z4.string().min(1).max(128),
2729
- url: z4.string().url().optional(),
2730
- llms: z4.string().url().optional()
3728
+ var ContentHashAlgoSchema = z5.literal("sha256-normalized-v1");
3729
+ var EcosystemPropertySchema = z5.object({
3730
+ id: z5.string().min(1).max(64),
3731
+ name: z5.string().min(1).max(128),
3732
+ url: z5.string().url().optional(),
3733
+ llms: z5.string().url().optional()
2731
3734
  }).strict();
2732
- var KnowledgeEntrySchema = z4.object({
2733
- id: z4.string().min(1).max(256),
2734
- type: z4.string().min(1).max(128),
2735
- title: z4.string().min(1).max(256),
2736
- path: z4.string().min(1).max(512),
2737
- description: z4.string().max(2048).optional(),
3735
+ var KnowledgeEntrySchema = z5.object({
3736
+ id: z5.string().min(1).max(256),
3737
+ type: z5.string().min(1).max(128),
3738
+ title: z5.string().min(1).max(256),
3739
+ path: z5.string().min(1).max(512),
3740
+ description: z5.string().max(2048).optional(),
2738
3741
  /** Flattened body excerpt for full-text search (not for display). */
2739
- body: z4.string().max(8e3).optional(),
2740
- links: z4.array(z4.string().min(1).max(512)).max(64).optional(),
2741
- tags: z4.array(z4.string().min(1).max(64)).max(32).optional()
3742
+ body: z5.string().max(8e3).optional(),
3743
+ links: z5.array(z5.string().min(1).max(512)).max(64).optional(),
3744
+ tags: z5.array(z5.string().min(1).max(64)).max(32).optional()
2742
3745
  }).strict();
2743
- var CapabilityRefSchema = z4.object({
2744
- id: z4.string().min(1).max(256),
2745
- kind: z4.string().min(1).max(64),
2746
- description: z4.string().max(512).optional()
3746
+ var CapabilityRefSchema = z5.object({
3747
+ id: z5.string().min(1).max(256),
3748
+ kind: z5.string().min(1).max(64),
3749
+ description: z5.string().max(512).optional()
2747
3750
  }).strict();
2748
- var OwnershipRecordSchema = z4.object({
2749
- id: z4.string().min(1).max(256),
2750
- path: z4.string().min(1).max(512),
2751
- group: z4.string().min(1).max(128).optional(),
2752
- layer: z4.string().min(1).max(32).optional(),
2753
- purpose: z4.string().max(1024).optional(),
2754
- checks: z4.array(z4.string().min(1).max(256)).max(32),
2755
- agentDoc: z4.string().min(1).max(512).optional(),
2756
- humanDoc: z4.string().min(1).max(512).optional(),
2757
- readme: z4.string().min(1).max(512).optional()
3751
+ var OwnershipRecordSchema = z5.object({
3752
+ id: z5.string().min(1).max(256),
3753
+ path: z5.string().min(1).max(512),
3754
+ group: z5.string().min(1).max(128).optional(),
3755
+ layer: z5.string().min(1).max(32).optional(),
3756
+ purpose: z5.string().max(1024).optional(),
3757
+ checks: z5.array(z5.string().min(1).max(256)).max(32),
3758
+ agentDoc: z5.string().min(1).max(512).optional(),
3759
+ humanDoc: z5.string().min(1).max(512).optional(),
3760
+ readme: z5.string().min(1).max(512).optional()
2758
3761
  }).strict();
2759
- var IndexLookupSchema = z4.object({
2760
- packages: z4.array(z4.string().min(1).max(256)).max(2e3),
2761
- ownership: z4.record(z4.string().min(1).max(256), OwnershipRecordSchema).optional(),
2762
- intents: z4.record(
2763
- z4.string().min(1).max(128),
2764
- z4.object({
2765
- id: z4.string().min(1).max(128),
2766
- title: z4.string().min(1).max(256),
2767
- paths: z4.array(z4.string().min(1).max(512)).max(32)
3762
+ var IndexLookupSchema = z5.object({
3763
+ packages: z5.array(z5.string().min(1).max(256)).max(2e3),
3764
+ ownership: z5.record(z5.string().min(1).max(256), OwnershipRecordSchema).optional(),
3765
+ intents: z5.record(
3766
+ z5.string().min(1).max(128),
3767
+ z5.object({
3768
+ id: z5.string().min(1).max(128),
3769
+ title: z5.string().min(1).max(256),
3770
+ paths: z5.array(z5.string().min(1).max(512)).max(32)
2768
3771
  }).strict()
2769
3772
  ).optional(),
2770
- changes: z4.record(
2771
- z4.string().min(1).max(128),
2772
- z4.object({
2773
- id: z4.string().min(1).max(128),
2774
- title: z4.string().min(1).max(256),
2775
- startHere: z4.string().min(1).max(512),
2776
- relatedPackages: z4.array(z4.string().min(1).max(256)).max(32).optional()
3773
+ changes: z5.record(
3774
+ z5.string().min(1).max(128),
3775
+ z5.object({
3776
+ id: z5.string().min(1).max(128),
3777
+ title: z5.string().min(1).max(256),
3778
+ startHere: z5.string().min(1).max(512),
3779
+ relatedPackages: z5.array(z5.string().min(1).max(256)).max(32).optional()
2777
3780
  }).strict()
2778
3781
  ).optional()
2779
3782
  }).strict();
2780
- var DocBridgeIndexV1Schema = z4.object({
2781
- schemaVersion: z4.literal(INDEX_SCHEMA_VERSION),
2782
- contentHash: z4.string().regex(/^[a-f0-9]{64}$/),
3783
+ var DocBridgeIndexV1Schema = z5.object({
3784
+ schemaVersion: z5.literal(INDEX_SCHEMA_VERSION),
3785
+ contentHash: z5.string().regex(/^[a-f0-9]{64}$/),
2783
3786
  contentHashAlgo: ContentHashAlgoSchema,
2784
- generatedAt: z4.string().datetime().optional(),
2785
- project: z4.object({
2786
- name: z4.string().min(1).max(128),
2787
- root: z4.string().min(1).max(512).optional()
3787
+ generatedAt: z5.string().datetime().optional(),
3788
+ project: z5.object({
3789
+ name: z5.string().min(1).max(128),
3790
+ root: z5.string().min(1).max(512).optional()
2788
3791
  }).strict().optional(),
2789
- properties: z4.array(EcosystemPropertySchema).max(16).optional(),
2790
- knowledge: z4.array(KnowledgeEntrySchema).max(1e4),
2791
- capabilities: z4.array(CapabilityRefSchema).max(5e3).optional(),
2792
- handoffs: z4.record(z4.string().min(1).max(256), AgentHandoffLegacySchema).optional(),
3792
+ properties: z5.array(EcosystemPropertySchema).max(16).optional(),
3793
+ knowledge: z5.array(KnowledgeEntrySchema).max(1e4),
3794
+ capabilities: z5.array(CapabilityRefSchema).max(5e3).optional(),
3795
+ handoffs: z5.record(z5.string().min(1).max(256), AgentHandoffLegacySchema).optional(),
2793
3796
  lookup: IndexLookupSchema.optional()
2794
3797
  }).strict();
2795
3798
 
2796
3799
  // src/schemas/memory-candidate.ts
2797
- import { z as z5 } from "zod";
3800
+ import { z as z6 } from "zod";
2798
3801
  var MEMORY_CANDIDATE_SCHEMA_VERSION = 1;
2799
- var MemorySourceSchema = z5.enum([
3802
+ var MemorySourceSchema = z6.enum([
2800
3803
  "cursor",
2801
3804
  "claude",
2802
3805
  "codex",
@@ -2804,24 +3807,24 @@ var MemorySourceSchema = z5.enum([
2804
3807
  "agent-memory",
2805
3808
  "manual"
2806
3809
  ]);
2807
- var MemorySuggestedTypeSchema = z5.enum([
3810
+ var MemorySuggestedTypeSchema = z6.enum([
2808
3811
  "user",
2809
3812
  "feedback",
2810
3813
  "project",
2811
3814
  "reference",
2812
3815
  "unknown"
2813
3816
  ]);
2814
- var MemoryCandidateV1Schema = z5.object({
2815
- schemaVersion: z5.literal(MEMORY_CANDIDATE_SCHEMA_VERSION),
2816
- id: z5.string().min(1).max(256),
3817
+ var MemoryCandidateV1Schema = z6.object({
3818
+ schemaVersion: z6.literal(MEMORY_CANDIDATE_SCHEMA_VERSION),
3819
+ id: z6.string().min(1).max(256),
2817
3820
  source: MemorySourceSchema,
2818
- rawPath: z5.string().min(1).max(512).optional(),
2819
- fact: z5.string().min(1).max(8e3),
2820
- why: z5.string().max(4e3).optional(),
2821
- howToApply: z5.string().max(4e3).optional(),
3821
+ rawPath: z6.string().min(1).max(512).optional(),
3822
+ fact: z6.string().min(1).max(8e3),
3823
+ why: z6.string().max(4e3).optional(),
3824
+ howToApply: z6.string().max(4e3).optional(),
2822
3825
  suggestedType: MemorySuggestedTypeSchema,
2823
- confidence: z5.number().min(0).max(1),
2824
- references: z5.array(z5.string().min(1).max(512)).max(64)
3826
+ confidence: z6.number().min(0).max(1),
3827
+ references: z6.array(z6.string().min(1).max(512)).max(64)
2825
3828
  }).strict();
2826
3829
 
2827
3830
  // src/validate.ts
@@ -2847,6 +3850,8 @@ ${result.issues.map((i) => ` - ${i.path}: ${i.message}`).join("\n")}`
2847
3850
  return result.value;
2848
3851
  };
2849
3852
  var parseDocBridgeIndex = (input) => DocBridgeIndexV1Schema.parse(input);
3853
+ var parseDiscoverySnapshot = (input) => DiscoverySnapshotV1Schema.parse(input);
3854
+ var parseReconciliationReport = (input) => ReconciliationReportV1Schema.parse(input);
2850
3855
  var parseDocBridgeConfig = (input) => {
2851
3856
  const result = DocBridgeConfigV1Schema.safeParse(input);
2852
3857
  if (!result.success) {
@@ -2867,11 +3872,11 @@ var IndexNotFoundError = class extends Error {
2867
3872
  }
2868
3873
  path;
2869
3874
  };
2870
- var indexFilePath = (root, config) => join8(root, config.index?.outFile ?? ".doc-bridge/index.json");
3875
+ var indexFilePath = (root, config) => join9(root, config.index?.outFile ?? ".doc-bridge/index.json");
2871
3876
  var loadDocBridgeIndex = (root, config) => {
2872
3877
  const path = indexFilePath(root, config);
2873
- if (!existsSync11(path)) throw new IndexNotFoundError(path);
2874
- const raw = JSON.parse(readFileSync10(path, "utf8"));
3878
+ if (!existsSync12(path)) throw new IndexNotFoundError(path);
3879
+ const raw = JSON.parse(readFileSync11(path, "utf8"));
2875
3880
  return parseDocBridgeIndex(raw);
2876
3881
  };
2877
3882
 
@@ -2946,7 +3951,7 @@ var runOkfTypeGate = (root, config) => {
2946
3951
  const required = config.corpus.agent.okf?.requireType ?? config.gates?.preset === "strict";
2947
3952
  if (!required) return { id: "okf-type", ok: true, message: "OKF type frontmatter not required" };
2948
3953
  const allowed = config.corpus.agent.okf?.allowedTypes;
2949
- const bad = scanAgentCorpus(root, config).filter((doc) => doc.path !== config.corpus.agent.index).map((doc) => ({ path: doc.path, type: frontmatterType(readFileSync11(doc.absPath, "utf8")) })).filter((doc) => !doc.type || allowed && !allowed.includes(doc.type));
3954
+ const bad = scanAgentCorpus(root, config).filter((doc) => doc.path !== config.corpus.agent.index).map((doc) => ({ path: doc.path, type: frontmatterType(readFileSync12(doc.absPath, "utf8")) })).filter((doc) => !doc.type || allowed && !allowed.includes(doc.type));
2950
3955
  if (bad.length) {
2951
3956
  return {
2952
3957
  id: "okf-type",
@@ -2973,10 +3978,10 @@ var DOCS_STYLE_RULES = {
2973
3978
  "playbook-okf-soft": ["title", "no-stale-wording"],
2974
3979
  "title-only": ["title"]
2975
3980
  };
2976
- var isRecord2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
3981
+ var isRecord3 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
2977
3982
  var docsStyleOptions = (config) => {
2978
3983
  const raw = config.gates?.options?.["docs-style"];
2979
- if (!isRecord2(raw)) {
3984
+ if (!isRecord3(raw)) {
2980
3985
  if (config.gates?.preset === "playbook") {
2981
3986
  return { profile: "playbook-okf-soft", required: DOCS_STYLE_RULES["playbook-okf-soft"] };
2982
3987
  }
@@ -3020,7 +4025,7 @@ var runDocsStyleGate = (root, config) => {
3020
4025
  }
3021
4026
  const bad = scanAgentCorpus(root, config).filter((doc) => doc.path !== config.corpus.agent.index).map((doc) => ({
3022
4027
  path: doc.path,
3023
- missing: missingStyleRules(readFileSync11(doc.absPath, "utf8"), required)
4028
+ missing: missingStyleRules(readFileSync12(doc.absPath, "utf8"), required)
3024
4029
  })).filter((doc) => doc.missing.length > 0);
3025
4030
  if (bad.length) {
3026
4031
  return {
@@ -3064,6 +4069,112 @@ var resolveGateIds = (config) => {
3064
4069
  return [...ids];
3065
4070
  };
3066
4071
 
4072
+ // src/rules/engine.ts
4073
+ import { minimatch as minimatch5 } from "minimatch";
4074
+ var diagnosticRules = {
4075
+ DOCUMENTATION_QUALITY: "documentation-quality",
4076
+ RELATION_UNDOCUMENTED: "graph-undocumented-relation",
4077
+ DECLARED_RELATION_STALE: "declared-unobserved-relation",
4078
+ UNRESOLVED_ENTITY_REFERENCE: "unresolved-reference",
4079
+ CONFLICTING_DECLARATIONS: "conflicting-declaration",
4080
+ RELATION_NOT_ANALYZED: "not-analyzed-coverage",
4081
+ STALE_DOCUMENTATION: "stale-documentation",
4082
+ FRESHNESS_FAILURE: "freshness",
4083
+ OWNERSHIP_GAP: "ownership",
4084
+ CENTRALITY_RISK: "centrality-risk",
4085
+ CRITICAL_PATH_RISK: "critical-path-risk"
4086
+ };
4087
+ var defaultSeverity = (mode, ruleId) => {
4088
+ if (mode === "default") return "info";
4089
+ if (mode === "strict") return ruleId === "not-analyzed-coverage" ? "warn" : "error";
4090
+ return ruleId === "not-analyzed-coverage" ? "info" : "warn";
4091
+ };
4092
+ var resolvedOptions = (options) => {
4093
+ const config = RulesConfigSchema.parse(options.config ?? {});
4094
+ const mode = options.preset ?? config.mode ?? "default";
4095
+ const severity = { ...config.severity, ...options.severity };
4096
+ const ignore = /* @__PURE__ */ new Set([...config.ignore ?? [], ...options.ignore ?? []]);
4097
+ return {
4098
+ mode,
4099
+ severity,
4100
+ ignore,
4101
+ criticalEntities: options.criticalEntities ?? config.criticalEntities ?? [],
4102
+ criticalPaths: options.criticalPaths ?? config.criticalPaths ?? [],
4103
+ warningThresholds: { ...config.warningThresholds, ...options.warningThresholds }
4104
+ };
4105
+ };
4106
+ var severityFor = (ruleId, mode, overrides) => overrides[ruleId] ?? defaultSeverity(mode, ruleId);
4107
+ var findingFromDiagnostic = (diagnostic2, ruleId, severity) => ({
4108
+ id: `${diagnostic2.id}:${ruleId}`,
4109
+ ruleId,
4110
+ code: ruleId,
4111
+ status: diagnostic2.status,
4112
+ severity,
4113
+ message: diagnostic2.message,
4114
+ evidence: diagnostic2.evidence,
4115
+ ...diagnostic2.entityIds ? { entityIds: diagnostic2.entityIds } : {},
4116
+ ...diagnostic2.relationIds ? { relationIds: diagnostic2.relationIds } : {},
4117
+ ...diagnostic2.remediation ? { remediation: diagnostic2.remediation } : {},
4118
+ sourceDiagnosticCode: diagnostic2.code
4119
+ });
4120
+ var criticalFinding = (finding, severity, target) => ({
4121
+ ...finding,
4122
+ id: `${finding.id}:critical:${target}`,
4123
+ ruleId: "critical-path-risk",
4124
+ code: "critical-path-risk",
4125
+ severity,
4126
+ message: `Critical path or entity is affected: ${target}. ${finding.message}`
4127
+ });
4128
+ var evaluateRules = (report, options = {}) => {
4129
+ const resolved = resolvedOptions(options);
4130
+ const findings = [];
4131
+ for (const diagnostic2 of [...report.diagnostics].sort((a, b) => a.id.localeCompare(b.id))) {
4132
+ const ruleId = diagnosticRules[diagnostic2.code];
4133
+ if (!ruleId || resolved.ignore.has(ruleId)) continue;
4134
+ const severity = severityFor(ruleId, resolved.mode, resolved.severity);
4135
+ if (severity !== "off") findings.push(findingFromDiagnostic(diagnostic2, ruleId, severity));
4136
+ }
4137
+ const criticalSeverity = severityFor("critical-path-risk", resolved.mode, resolved.severity);
4138
+ const criticalEntitySet = new Set(resolved.criticalEntities);
4139
+ for (const finding of [...findings]) {
4140
+ const matchingEntity = (finding.entityIds ?? []).find((id) => criticalEntitySet.has(id));
4141
+ if (matchingEntity && !resolved.ignore.has("critical-path-risk") && criticalSeverity !== "off") {
4142
+ findings.push(criticalFinding(finding, criticalSeverity, matchingEntity));
4143
+ }
4144
+ for (const path of resolved.criticalPaths) {
4145
+ if (finding.evidence.some((item) => minimatch5(item.path, path, { dot: true })) && !resolved.ignore.has("critical-path-risk") && criticalSeverity !== "off") {
4146
+ findings.push(criticalFinding(finding, criticalSeverity, path));
4147
+ }
4148
+ }
4149
+ }
4150
+ const centralityThreshold = resolved.warningThresholds["centrality-risk"] ?? 3;
4151
+ const centralitySeverity = severityFor("centrality-risk", resolved.mode, resolved.severity);
4152
+ if (!resolved.ignore.has("centrality-risk") && centralitySeverity !== "off") {
4153
+ const counts = /* @__PURE__ */ new Map();
4154
+ for (const finding of findings.filter((item) => item.ruleId === "graph-undocumented-relation")) {
4155
+ for (const entityId2 of finding.entityIds ?? []) counts.set(entityId2, (counts.get(entityId2) ?? 0) + 1);
4156
+ }
4157
+ for (const [entityId2, count2] of [...counts.entries()].sort(([a], [b]) => a.localeCompare(b))) {
4158
+ if (count2 < centralityThreshold || !criticalEntitySet.has(entityId2)) continue;
4159
+ findings.push({
4160
+ id: `centrality-risk:${entityId2}`,
4161
+ ruleId: "centrality-risk",
4162
+ code: "centrality-risk",
4163
+ status: "unresolved",
4164
+ severity: centralitySeverity,
4165
+ message: `Critical entity has ${count2} undocumented relation finding(s); static centrality is a review signal, not a runtime availability claim.`,
4166
+ evidence: findings.filter((item) => item.ruleId === "graph-undocumented-relation" && item.entityIds?.includes(entityId2)).flatMap((item) => item.evidence),
4167
+ entityIds: [entityId2],
4168
+ remediation: "Review ownership, dependency boundaries, and runtime availability before declaring an SPOF."
4169
+ });
4170
+ }
4171
+ }
4172
+ const sortedFindings = [...findings].sort((a, b) => a.id.localeCompare(b.id));
4173
+ return { mode: resolved.mode, findings: sortedFindings, exitCode: sortedFindings.some((finding) => finding.severity === "error") ? 1 : 0 };
4174
+ };
4175
+ var parseRuleId = (value) => RuleIdSchema.parse(value);
4176
+ var parseRuleSeverity = (value) => RuleSeveritySchema.parse(value);
4177
+
3067
4178
  // src/query/query.ts
3068
4179
  var handoffForPackage = (index, id, config) => {
3069
4180
  const fromIndex = index.handoffs?.[id];
@@ -3175,7 +4286,7 @@ var runQuery = (index, config, req) => {
3175
4286
  };
3176
4287
 
3177
4288
  // src/intelligence/adapter.ts
3178
- import { join as join9 } from "path";
4289
+ import { join as join10 } from "path";
3179
4290
 
3180
4291
  // src/intelligence/peers.ts
3181
4292
  var PeerMissingError = class extends Error {
@@ -3302,20 +4413,20 @@ var resolveIntelligenceRuntime = async (config) => {
3302
4413
  }
3303
4414
  return { adapter, embed, provider, ...model ? { model } : {} };
3304
4415
  };
3305
- var defaultVectorStorePath = (root) => join9(root, ".doc-bridge", "vectors");
4416
+ var defaultVectorStorePath = (root) => join10(root, ".doc-bridge", "vectors");
3306
4417
 
3307
4418
  // src/intelligence/rag.ts
3308
- import { readFileSync as readFileSync12 } from "fs";
3309
- import { join as join10 } from "path";
4419
+ import { readFileSync as readFileSync13 } from "fs";
4420
+ import { join as join11 } from "path";
3310
4421
  var loadDocuments = (root, index, sources) => {
3311
4422
  const includeAgent = sources.includes("agent") || sources.length === 0;
3312
4423
  const docs = [];
3313
4424
  if (includeAgent) {
3314
4425
  for (const entry of index.knowledge) {
3315
- const abs = join10(root, entry.path);
4426
+ const abs = join11(root, entry.path);
3316
4427
  let content = "";
3317
4428
  try {
3318
- content = readFileSync12(abs, "utf8");
4429
+ content = readFileSync13(abs, "utf8");
3319
4430
  } catch {
3320
4431
  content = [entry.title, entry.description].filter(Boolean).join("\n\n");
3321
4432
  }
@@ -3333,7 +4444,7 @@ var createDocBridgeRag = async (root, config, index) => {
3333
4444
  const { embed } = await resolveIntelligenceRuntime(config);
3334
4445
  const ragMod = await importPeer("@agentskit/rag");
3335
4446
  const memoryMod = await importPeer("@agentskit/memory");
3336
- const storePath = typeof config.intelligence?.retriever?.options?.storePath === "string" ? join10(root, config.intelligence.retriever.options.storePath) : defaultVectorStorePath(root);
4447
+ const storePath = typeof config.intelligence?.retriever?.options?.storePath === "string" ? join11(root, config.intelligence.retriever.options.storePath) : defaultVectorStorePath(root);
3337
4448
  const store = memoryMod.fileVectorMemory({ path: storePath });
3338
4449
  const rag = ragMod.createRAG({
3339
4450
  embed,
@@ -3468,15 +4579,15 @@ var startInkChat = async (root, config, index) => {
3468
4579
  };
3469
4580
 
3470
4581
  // src/memory/ingest.ts
3471
- import { existsSync as existsSync12, readFileSync as readFileSync13 } from "fs";
3472
- import { join as join11 } from "path";
4582
+ import { existsSync as existsSync13, readFileSync as readFileSync14 } from "fs";
4583
+ import { join as join12 } from "path";
3473
4584
  var memoryFact = (raw, id) => firstParagraph(raw) ?? firstHeading(raw) ?? id;
3474
- var relativePath = (root, abs) => toPosix(abs).replace(`${toPosix(root)}/`, "");
4585
+ var relativePath2 = (root, abs) => toPosix(abs).replace(`${toPosix(root)}/`, "");
3475
4586
  var ingestMarkdownDir = (root, dir, source, confidence) => {
3476
- if (!existsSync12(dir)) return [];
4587
+ if (!existsSync13(dir)) return [];
3477
4588
  return walkFiles(dir, { extensions: [".md", ".mdc"] }).map((abs) => {
3478
- const rel = relativePath(root, abs);
3479
- const raw = readFileSync13(abs, "utf8");
4589
+ const rel = relativePath2(root, abs);
4590
+ const raw = readFileSync14(abs, "utf8");
3480
4591
  const id = slugFromPath(rel);
3481
4592
  return {
3482
4593
  schemaVersion: 1,
@@ -3491,10 +4602,10 @@ var ingestMarkdownDir = (root, dir, source, confidence) => {
3491
4602
  });
3492
4603
  };
3493
4604
  var ingestCursorRules = (root) => {
3494
- const dir = join11(root, ".cursor", "rules");
4605
+ const dir = join12(root, ".cursor", "rules");
3495
4606
  return ingestMarkdownDir(root, dir, "cursor", 0.6);
3496
4607
  };
3497
- var ingestAgentMemory = (root) => ingestMarkdownDir(root, join11(root, ".agent-memory"), "agent-memory", 0.7);
4608
+ var ingestAgentMemory = (root) => ingestMarkdownDir(root, join12(root, ".agent-memory"), "agent-memory", 0.7);
3498
4609
  var ingestMemoryCandidates = (root) => [
3499
4610
  ...ingestAgentMemory(root),
3500
4611
  ...ingestCursorRules(root)
@@ -3580,9 +4691,9 @@ var draftMemoryPromotion = (classifications) => {
3580
4691
  };
3581
4692
 
3582
4693
  // src/memory/github-pr.ts
3583
- import { execFileSync, spawnSync } from "child_process";
3584
- import { existsSync as existsSync13, mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "fs";
3585
- import { join as join12 } from "path";
4694
+ import { execFileSync as execFileSync2, spawnSync } from "child_process";
4695
+ import { existsSync as existsSync14, mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "fs";
4696
+ import { join as join13 } from "path";
3586
4697
  var run = (cmd, args, cwd) => {
3587
4698
  const result = spawnSync(cmd, [...args], { cwd, encoding: "utf8" });
3588
4699
  const out = `${result.stdout ?? ""}${result.stderr ?? ""}`.trim();
@@ -3590,10 +4701,10 @@ var run = (cmd, args, cwd) => {
3590
4701
  };
3591
4702
  var hasGh = () => run("gh", ["--version"], process.cwd()).ok;
3592
4703
  var slug = () => (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").slice(0, 19);
3593
- var defaultPromotionDraftPath = (root) => join12(root, ".doc-bridge", "drafts", `memory-promotion-${slug()}.md`);
4704
+ var defaultPromotionDraftPath = (root) => join13(root, ".doc-bridge", "drafts", `memory-promotion-${slug()}.md`);
3594
4705
  var writePromotionDraft = (root, draft, path) => {
3595
4706
  const draftPath = path ?? defaultPromotionDraftPath(root);
3596
- mkdirSync2(join12(root, ".doc-bridge", "drafts"), { recursive: true });
4707
+ mkdirSync2(join13(root, ".doc-bridge", "drafts"), { recursive: true });
3597
4708
  writeFileSync2(draftPath, `${draft.body}
3598
4709
  `, "utf8");
3599
4710
  return draftPath;
@@ -3629,7 +4740,7 @@ var promoteMemoryToGithubPr = (root, draft, options = {}) => {
3629
4740
  message: `Wrote draft to ${relDraft}. Run the printed git/gh commands to open a draft PR.`
3630
4741
  };
3631
4742
  }
3632
- if (!existsSync13(join12(root, ".git"))) {
4743
+ if (!existsSync14(join13(root, ".git"))) {
3633
4744
  return {
3634
4745
  ok: false,
3635
4746
  dryRun: false,
@@ -3707,7 +4818,7 @@ ${auth.out}`
3707
4818
  ];
3708
4819
  let prUrl = "";
3709
4820
  try {
3710
- prUrl = execFileSync("gh", prArgs, { cwd: root, encoding: "utf8" }).trim();
4821
+ prUrl = execFileSync2("gh", prArgs, { cwd: root, encoding: "utf8" }).trim();
3711
4822
  } catch (error) {
3712
4823
  const message = error instanceof Error ? error.message : String(error);
3713
4824
  return {
@@ -3732,23 +4843,23 @@ ${auth.out}`
3732
4843
  };
3733
4844
 
3734
4845
  // src/index-builder/watch-index.ts
3735
- import { existsSync as existsSync14, watch } from "fs";
3736
- import { dirname as dirname5, resolve as resolve9 } from "path";
4846
+ import { existsSync as existsSync15, watch } from "fs";
4847
+ import { dirname as dirname6, resolve as resolve11 } from "path";
3737
4848
  var WATCH_PATTERN = /\.(md|mdx|json|ya?ml|mdc)$/i;
3738
4849
  var NX_MANIFEST_PATTERN = /(^|[/\\])(project|package)\.json$/i;
3739
4850
  var collectWatchRoots = (root, config, configPath) => {
3740
4851
  const roots = /* @__PURE__ */ new Set();
3741
- roots.add(resolve9(root, config.corpus.agent.root));
4852
+ roots.add(resolve11(root, config.corpus.agent.root));
3742
4853
  const humanSources = config.corpus.human ? Array.isArray(config.corpus.human) ? config.corpus.human : [config.corpus.human] : [];
3743
4854
  for (const source of humanSources) {
3744
4855
  const humanOpts = source.options ?? {};
3745
4856
  for (const key of ["contentDir", "docsDir", "root", "srcDir"]) {
3746
4857
  const value = humanOpts[key];
3747
- if (typeof value === "string" && value.length) roots.add(resolve9(root, value));
4858
+ if (typeof value === "string" && value.length) roots.add(resolve11(root, value));
3748
4859
  }
3749
4860
  }
3750
- if (configPath) roots.add(dirname5(resolve9(configPath)));
3751
- return [...roots].filter((dir) => existsSync14(dir));
4861
+ if (configPath) roots.add(dirname6(resolve11(configPath)));
4862
+ return [...roots].filter((dir) => existsSync15(dir));
3752
4863
  };
3753
4864
  var watchDocBridgeIndex = (opts) => {
3754
4865
  const debounceMs = opts.debounceMs ?? 350;
@@ -3789,15 +4900,15 @@ var watchDocBridgeIndex = (opts) => {
3789
4900
  rebuild();
3790
4901
  });
3791
4902
  }
3792
- const nxRoot = resolve9(opts.root);
3793
- if (opts.config.routing?.plugin === "nx" && existsSync14(nxRoot)) {
4903
+ const nxRoot = resolve11(opts.root);
4904
+ if (opts.config.routing?.plugin === "nx" && existsSync15(nxRoot)) {
3794
4905
  watch(nxRoot, { recursive: true }, (_event, filename) => {
3795
4906
  if (!filename || !NX_MANIFEST_PATTERN.test(filename)) return;
3796
4907
  rebuild();
3797
4908
  });
3798
4909
  }
3799
- const configDir = resolve9(opts.root);
3800
- if (existsSync14(configDir)) {
4910
+ const configDir = resolve11(opts.root);
4911
+ if (existsSync15(configDir)) {
3801
4912
  watch(configDir, (_event, filename) => {
3802
4913
  if (!filename || !/doc-bridge\.config/.test(filename)) return;
3803
4914
  rebuild();
@@ -3814,6 +4925,179 @@ var watchDocBridgeIndex = (opts) => {
3814
4925
  });
3815
4926
  };
3816
4927
 
4928
+ // src/workflow/engine.ts
4929
+ import { appendFileSync, existsSync as existsSync16, mkdirSync as mkdirSync3, readFileSync as readFileSync15, renameSync, rmSync, writeFileSync as writeFileSync3 } from "fs";
4930
+ import { join as join15, relative as relative7, resolve as resolve12 } from "path";
4931
+ var WORKFLOW_STAGES = ["collect", "normalize", "reconcile", "evaluate", "report"];
4932
+ var stageState = {
4933
+ collect: "discovering",
4934
+ normalize: "analyzed",
4935
+ reconcile: "compared",
4936
+ evaluate: "proposed",
4937
+ report: "delivered"
4938
+ };
4939
+ var defaultStateDir = (root) => join15(root, ".doc-bridge", "workflow");
4940
+ var atomicWrite = (path, value) => {
4941
+ const temp = `${path}.tmp-${process.pid}-${Date.now()}`;
4942
+ writeFileSync3(temp, `${JSON.stringify(value, null, 2)}
4943
+ `, "utf8");
4944
+ renameSync(temp, path);
4945
+ };
4946
+ var writeManifest = (stateDir, run2) => {
4947
+ atomicWrite(join15(stateDir, "manifest.json"), run2);
4948
+ };
4949
+ var appendTransition = (stateDir, transition2) => {
4950
+ appendFileSync(join15(stateDir, "transitions.jsonl"), `${JSON.stringify(transition2)}
4951
+ `, "utf8");
4952
+ };
4953
+ var transition = (run2, to, reason) => {
4954
+ const item = {
4955
+ from: run2.state,
4956
+ to,
4957
+ at: (/* @__PURE__ */ new Date()).toISOString(),
4958
+ ...reason ? { reason } : {}
4959
+ };
4960
+ return { ...run2, state: to, transitions: [...run2.transitions, item] };
4961
+ };
4962
+ var runId = () => `${Date.now()}-${process.pid}`;
4963
+ var stageInputHash = (options, stage, input) => sha256NormalizedV1({ stage, input, sourceRevision: options.sourceRevision, configurationHash: options.configurationHash, toolVersion: options.toolVersion ?? "1.0.0" });
4964
+ var stageArtifactPath = (stateDir, stage, inputHash) => join15(stateDir, "artifacts", `${stage}-${inputHash}.json`);
4965
+ var readArtifact = (path) => JSON.parse(readFileSync15(path, "utf8"));
4966
+ var stepArtifactPath = (stateDir, step) => resolve12(stateDir, step.artifactRefs?.[0] ?? "");
4967
+ var stepOutput = (stateDir, run2, stage) => {
4968
+ const step = run2.steps.find((item) => item.name === stage);
4969
+ if (!step || step.status !== "completed" || !step.artifactRefs?.[0]) return null;
4970
+ return readArtifact(stepArtifactPath(stateDir, step)).value;
4971
+ };
4972
+ var acquireLock = (stateDir) => {
4973
+ const lock = join15(stateDir, ".lock");
4974
+ try {
4975
+ mkdirSync3(lock);
4976
+ } catch {
4977
+ const ownerPath = join15(lock, "owner.json");
4978
+ try {
4979
+ const owner = JSON.parse(readFileSync15(ownerPath, "utf8"));
4980
+ if (typeof owner.pid === "number") process.kill(owner.pid, 0);
4981
+ throw new Error(`Workflow is already running (pid ${owner.pid ?? "unknown"}).`);
4982
+ } catch (error) {
4983
+ if (error instanceof Error && error.message.startsWith("Workflow is already running")) throw error;
4984
+ rmSync(lock, { recursive: true, force: true });
4985
+ mkdirSync3(lock);
4986
+ }
4987
+ }
4988
+ writeFileSync3(join15(lock, "owner.json"), JSON.stringify({ pid: process.pid, startedAt: (/* @__PURE__ */ new Date()).toISOString() }), "utf8");
4989
+ return () => rmSync(lock, { recursive: true, force: true });
4990
+ };
4991
+ var loadManifest = (stateDir) => {
4992
+ const path = join15(stateDir, "manifest.json");
4993
+ if (!existsSync16(path)) return void 0;
4994
+ return WorkflowRunV1Schema.parse(JSON.parse(readFileSync15(path, "utf8")));
4995
+ };
4996
+ var baseRun = (options, stateDir, supersedes) => {
4997
+ const inputHash = sha256NormalizedV1({ sourceRevision: options.sourceRevision, configurationHash: options.configurationHash, toolVersion: options.toolVersion ?? "1.0.0" });
4998
+ return WorkflowRunV1Schema.parse({
4999
+ type: "workflow-run",
5000
+ schemaVersion: 1,
5001
+ contentHash: "0".repeat(64),
5002
+ contentHashAlgo: "sha256-normalized-v1",
5003
+ project: { name: resolve12(options.root).split("/").pop() ?? "project", root: "." },
5004
+ sourceRevision: options.sourceRevision,
5005
+ sourceRevisionKind: "content",
5006
+ configurationHash: options.configurationHash,
5007
+ pipelineVersion: "1.0.0",
5008
+ analyzerVersions: { workflow: options.toolVersion ?? "1.0.0" },
5009
+ runId: options.runId ?? runId(),
5010
+ state: "created",
5011
+ steps: WORKFLOW_STAGES.map((name) => ({ name, status: "pending", inputHash })),
5012
+ transitions: [{ from: null, to: "created", at: (/* @__PURE__ */ new Date()).toISOString() }],
5013
+ artifactRefs: [relative7(resolve12(options.root), stateDir), ...supersedes ? [`supersedes:${supersedes}`] : []]
5014
+ });
5015
+ };
5016
+ var withHash = (run2) => WorkflowRunV1Schema.parse({ ...run2, contentHash: contentHashForArtifactV1(run2) });
5017
+ var sameInputs = (run2, options) => run2.sourceRevision === options.sourceRevision && run2.configurationHash === options.configurationHash && run2.analyzerVersions.workflow === (options.toolVersion ?? "1.0.0");
5018
+ var selectedStages = (stage) => stage && stage !== "all" ? [stage] : WORKFLOW_STAGES;
5019
+ var runWorkflow = (options) => {
5020
+ const root = resolve12(options.root);
5021
+ const stateDir = resolve12(root, options.stateDir ?? defaultStateDir(root));
5022
+ mkdirSync3(join15(stateDir, "artifacts"), { recursive: true });
5023
+ const release = acquireLock(stateDir);
5024
+ try {
5025
+ let run2 = loadManifest(stateDir);
5026
+ let supersedes;
5027
+ if (run2 && !sameInputs(run2, options)) {
5028
+ supersedes = run2.runId;
5029
+ run2 = withHash(transition(run2, "stale", "Source revision, configuration hash, or tool version changed."));
5030
+ appendTransition(stateDir, run2.transitions[run2.transitions.length - 1]);
5031
+ writeManifest(stateDir, run2);
5032
+ run2 = void 0;
5033
+ }
5034
+ if (!run2) {
5035
+ run2 = withHash(baseRun(options, stateDir, supersedes));
5036
+ appendTransition(stateDir, run2.transitions[0]);
5037
+ writeManifest(stateDir, run2);
5038
+ }
5039
+ if (!run2) throw new Error("Workflow manifest was not initialized.");
5040
+ const firstSelectedStage = selectedStages(options.stage)[0];
5041
+ const previousStageIndex = firstSelectedStage ? WORKFLOW_STAGES.indexOf(firstSelectedStage) - 1 : -1;
5042
+ let previousOutput = previousStageIndex >= 0 ? stepOutput(stateDir, run2, WORKFLOW_STAGES[previousStageIndex]) : null;
5043
+ const reusedStages = [];
5044
+ for (const stage of selectedStages(options.stage)) {
5045
+ const input = options.inputs?.[stage] ?? previousOutput;
5046
+ const inputHash = stageInputHash(options, stage, input);
5047
+ const existing = run2.steps.find((step) => step.name === stage);
5048
+ const artifactPath = existing?.artifactRefs?.[0] ? resolve12(stateDir, existing.artifactRefs[0]) : stageArtifactPath(stateDir, stage, inputHash);
5049
+ if (existing?.status === "completed" && existing.inputHash === inputHash && existing.outputHash && existsSync16(artifactPath)) {
5050
+ previousOutput = readArtifact(artifactPath).value;
5051
+ reusedStages.push(stage);
5052
+ continue;
5053
+ }
5054
+ const handler = options.handlers[stage];
5055
+ if (!handler) throw new Error(`No handler configured for workflow stage "${stage}".`);
5056
+ run2 = withHash(transition(run2, stageState[stage]));
5057
+ appendTransition(stateDir, run2.transitions[run2.transitions.length - 1]);
5058
+ const runningStep = { name: stage, status: "running", inputHash };
5059
+ run2 = withHash({ ...run2, steps: run2.steps.map((step) => step.name === stage ? runningStep : step) });
5060
+ writeManifest(stateDir, run2);
5061
+ try {
5062
+ const value = handler({ root, stage, input, previousOutput });
5063
+ const outputHash = sha256NormalizedV1(value);
5064
+ const artifact2 = { type: "workflow-step-artifact", stage, inputHash, outputHash, value };
5065
+ mkdirSync3(join15(stateDir, "artifacts"), { recursive: true });
5066
+ if (existsSync16(artifactPath)) {
5067
+ const existingArtifact = readArtifact(artifactPath);
5068
+ if (existingArtifact.outputHash !== outputHash) throw new Error(`Immutable workflow artifact collision for stage "${stage}".`);
5069
+ } else {
5070
+ atomicWrite(artifactPath, artifact2);
5071
+ }
5072
+ const ref = relative7(stateDir, artifactPath);
5073
+ const completedStep = { name: stage, status: "completed", inputHash, outputHash, artifactRefs: [ref] };
5074
+ run2 = withHash({ ...run2, steps: run2.steps.map((step) => step.name === stage ? completedStep : step) });
5075
+ writeManifest(stateDir, run2);
5076
+ previousOutput = value;
5077
+ } catch (error) {
5078
+ run2 = withHash(transition(run2, "failed", error instanceof Error ? error.message : String(error)));
5079
+ appendTransition(stateDir, run2.transitions[run2.transitions.length - 1]);
5080
+ writeManifest(stateDir, run2);
5081
+ throw error;
5082
+ }
5083
+ }
5084
+ if (selectedStages(options.stage).every((stage) => run2.steps.find((step) => step.name === stage)?.status === "completed")) {
5085
+ const complete = selectedStages(options.stage).includes("report") && run2.state !== "delivered" ? withHash(transition(run2, "delivered")) : run2;
5086
+ if (complete !== run2) {
5087
+ appendTransition(stateDir, complete.transitions[complete.transitions.length - 1]);
5088
+ run2 = complete;
5089
+ }
5090
+ writeManifest(stateDir, run2);
5091
+ if (run2.state === "delivered") atomicWrite(join15(stateDir, "last-known-good.json"), { runId: run2.runId, manifestHash: run2.contentHash, report: run2.steps.find((step) => step.name === "report")?.artifactRefs?.[0] });
5092
+ }
5093
+ return { run: run2, stateDir, reusedStages };
5094
+ } finally {
5095
+ release();
5096
+ }
5097
+ };
5098
+ var loadWorkflowManifest = (stateDir) => WorkflowRunV1Schema.parse(JSON.parse(readFileSync15(join15(resolve12(stateDir), "manifest.json"), "utf8")));
5099
+ var loadWorkflowStepOutput = (stateDir, stage) => stepOutput(resolve12(stateDir), loadWorkflowManifest(stateDir), stage);
5100
+
3817
5101
  // src/doctor/badge.ts
3818
5102
  var doctorBadgeMetrics = (report) => {
3819
5103
  const { packages } = report.coverage;
@@ -3973,43 +5257,43 @@ var docBridgePatternPayload = () => ({
3973
5257
  });
3974
5258
 
3975
5259
  // src/cli/demo.ts
3976
- import { cpSync, existsSync as existsSync16, mkdtempSync, readFileSync as readFileSync15, rmSync } from "fs";
5260
+ import { cpSync, existsSync as existsSync18, mkdtempSync, readFileSync as readFileSync17, rmSync as rmSync2 } from "fs";
3977
5261
  import { tmpdir } from "os";
3978
- import { dirname as dirname7, join as join15, resolve as resolve11 } from "path";
5262
+ import { dirname as dirname8, join as join17, resolve as resolve14 } from "path";
3979
5263
  import { fileURLToPath } from "url";
3980
5264
 
3981
5265
  // src/mcp/install.ts
3982
- import { existsSync as existsSync15, mkdirSync as mkdirSync3, readFileSync as readFileSync14, writeFileSync as writeFileSync3 } from "fs";
5266
+ import { existsSync as existsSync17, mkdirSync as mkdirSync4, readFileSync as readFileSync16, writeFileSync as writeFileSync4 } from "fs";
3983
5267
  import { homedir } from "os";
3984
- import { dirname as dirname6, join as join14, resolve as resolve10 } from "path";
5268
+ import { dirname as dirname7, join as join16, resolve as resolve13 } from "path";
3985
5269
  var SERVER_NAME = "ak-docs";
3986
5270
  var mcpServerEntry = (root) => ({
3987
5271
  command: "npx",
3988
5272
  args: ["ak-docs", "mcp"],
3989
5273
  cwd: root
3990
5274
  });
3991
- var readJson = (path) => {
3992
- if (!existsSync15(path)) return {};
5275
+ var readJson2 = (path) => {
5276
+ if (!existsSync17(path)) return {};
3993
5277
  try {
3994
- const parsed = JSON.parse(readFileSync14(path, "utf8"));
5278
+ const parsed = JSON.parse(readFileSync16(path, "utf8"));
3995
5279
  return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
3996
5280
  } catch {
3997
5281
  return {};
3998
5282
  }
3999
5283
  };
4000
5284
  var writeJson = (path, value) => {
4001
- mkdirSync3(dirname6(path), { recursive: true });
4002
- writeFileSync3(path, `${JSON.stringify(value, null, 2)}
5285
+ mkdirSync4(dirname7(path), { recursive: true });
5286
+ writeFileSync4(path, `${JSON.stringify(value, null, 2)}
4003
5287
  `, "utf8");
4004
5288
  };
4005
5289
  var resolveTargetPath = (target, root) => {
4006
- if (target === "cursor") return resolve10(root, ".cursor", "mcp.json");
4007
- return join14(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json");
5290
+ if (target === "cursor") return resolve13(root, ".cursor", "mcp.json");
5291
+ return join16(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json");
4008
5292
  };
4009
5293
  var installMcpConfig = (root, target) => {
4010
5294
  const configPath = resolveTargetPath(target, root);
4011
- const created = !existsSync15(configPath);
4012
- const existing = readJson(configPath);
5295
+ const created = !existsSync17(configPath);
5296
+ const existing = readJson2(configPath);
4013
5297
  const servers = existing.mcpServers && typeof existing.mcpServers === "object" && !Array.isArray(existing.mcpServers) ? { ...existing.mcpServers } : {};
4014
5298
  servers[SERVER_NAME] = mcpServerEntry(root);
4015
5299
  writeJson(configPath, { ...existing, mcpServers: servers });
@@ -4033,12 +5317,12 @@ var installMcpConfig = (root, target) => {
4033
5317
  var mcpSnippet = (root) => JSON.stringify({ mcpServers: { [SERVER_NAME]: mcpServerEntry(root) } }, null, 2);
4034
5318
 
4035
5319
  // src/cli/demo.ts
4036
- var packageRoot = resolve11(dirname7(fileURLToPath(import.meta.url)), "..", "..");
5320
+ var packageRoot = resolve14(dirname8(fileURLToPath(import.meta.url)), "..", "..");
4037
5321
  var fixturePath = (fixture) => {
4038
5322
  if (fixture === "monorepo") {
4039
- return join15(packageRoot, "examples", "demo-monorepo");
5323
+ return join17(packageRoot, "examples", "demo-monorepo");
4040
5324
  }
4041
- return join15(packageRoot, "examples", "demo-example");
5325
+ return join17(packageRoot, "examples", "demo-example");
4042
5326
  };
4043
5327
  var formatHandoffText = (handoff) => {
4044
5328
  const bridge = handoff.bridge?.humanDoc === "missing" ? `human guide: missing \u2192 ${handoff.bridge.action ?? "ak-docs bootstrap agent-docs"}` : handoff.humanDoc ? `human guide: ${handoff.humanDoc}` : "human guide: (none)";
@@ -4054,11 +5338,11 @@ var formatHandoffText = (handoff) => {
4054
5338
  var runDemo = (root, config, fixture = "example", options = {}) => {
4055
5339
  const targetPackage = fixture === "monorepo" ? "auth" : "example";
4056
5340
  const fixtureDir = fixturePath(fixture);
4057
- if (options.copyFixture && existsSync16(fixtureDir)) {
5341
+ if (options.copyFixture && existsSync18(fixtureDir)) {
4058
5342
  for (const rel of ["doc-bridge.config.json", "docs", "packages", "pnpm-workspace.yaml", "package.json"]) {
4059
- const src = join15(fixtureDir, rel);
4060
- if (!existsSync16(src)) continue;
4061
- const dest = join15(root, rel);
5343
+ const src = join17(fixtureDir, rel);
5344
+ if (!existsSync18(src)) continue;
5345
+ const dest = join17(root, rel);
4062
5346
  cpSync(src, dest, { recursive: true });
4063
5347
  }
4064
5348
  }
@@ -4113,17 +5397,17 @@ var formatDemoText = (result) => {
4113
5397
  return lines;
4114
5398
  };
4115
5399
  var withDemoWorkspace = (fixture, fn) => {
4116
- const dir = mkdtempSync(join15(tmpdir(), "ak-docs-demo-"));
5400
+ const dir = mkdtempSync(join17(tmpdir(), "ak-docs-demo-"));
4117
5401
  try {
4118
5402
  const fixtureDir = fixturePath(fixture);
4119
- if (!existsSync16(fixtureDir)) {
5403
+ if (!existsSync18(fixtureDir)) {
4120
5404
  throw new Error(`Demo fixture "${fixture}" not found at ${fixtureDir}`);
4121
5405
  }
4122
5406
  cpSync(fixtureDir, dir, { recursive: true });
4123
- const config = JSON.parse(readFileSync15(join15(dir, "doc-bridge.config.json"), "utf8"));
5407
+ const config = JSON.parse(readFileSync17(join17(dir, "doc-bridge.config.json"), "utf8"));
4124
5408
  return fn(dir, config);
4125
5409
  } finally {
4126
- rmSync(dir, { recursive: true, force: true });
5410
+ rmSync2(dir, { recursive: true, force: true });
4127
5411
  }
4128
5412
  };
4129
5413
 
@@ -4331,9 +5615,220 @@ var formatDoctorText = (report) => {
4331
5615
  };
4332
5616
 
4333
5617
  // src/mcp/server.ts
4334
- import { readFileSync as readFileSync16, realpathSync as realpathSync7 } from "fs";
4335
- import { relative as relative5, resolve as resolve12 } from "path";
4336
- import { z as z6, ZodError } from "zod";
5618
+ import { mkdirSync as mkdirSync7, readFileSync as readFileSync20, realpathSync as realpathSync9, writeFileSync as writeFileSync7 } from "fs";
5619
+ import { join as join20, relative as relative9, resolve as resolve17 } from "path";
5620
+ import { z as z8, ZodError } from "zod";
5621
+
5622
+ // src/fixes/proposals.ts
5623
+ import { existsSync as existsSync19, readdirSync as readdirSync4, readFileSync as readFileSync18, realpathSync as realpathSync8, renameSync as renameSync2, statSync as statSync5, unlinkSync, writeFileSync as writeFileSync5 } from "fs";
5624
+ import { basename as basename5, dirname as dirname9, extname as extname2, join as join18, relative as relative8, resolve as resolve15, sep as sep8 } from "path";
5625
+ var hash2 = (value) => sha256NormalizedV1(value);
5626
+ var artifactMetadata = (root, options) => ({
5627
+ schemaVersion: 1,
5628
+ contentHash: "0".repeat(64),
5629
+ contentHashAlgo: "sha256-normalized-v1",
5630
+ project: { name: options.projectName ?? basename5(resolve15(root)), root: "." },
5631
+ sourceRevision: options.baseRevision,
5632
+ sourceRevisionKind: options.baseRevision.length === 40 ? "git" : "content",
5633
+ configurationHash: options.configurationHash,
5634
+ pipelineVersion: "1.0.0",
5635
+ analyzerVersions: { fixes: options.toolVersion ?? "1.0.0" }
5636
+ });
5637
+ var unifiedDiff = (changes) => changes.map((change) => {
5638
+ const before = change.before.split("\n").map((line) => `-${line}`).join("\n");
5639
+ const after = change.after.split("\n").map((line) => `+${line}`).join("\n");
5640
+ return `--- a/${change.path}
5641
+ +++ b/${change.path}
5642
+ @@
5643
+ ${before}
5644
+ ${after}`;
5645
+ }).join("\n");
5646
+ var makeProposal = (root, options, changes, preconditions, postconditions) => {
5647
+ const draft = {
5648
+ ...artifactMetadata(root, options),
5649
+ type: "fix-proposal",
5650
+ proposalId: `fix-${hash2(changes).slice(0, 20)}`,
5651
+ baseRevision: options.baseRevision,
5652
+ affectedFiles: changes.map(({ path, before }) => ({ path, contentHash: sha256NormalizedV1(before) })),
5653
+ changes,
5654
+ preconditions,
5655
+ diff: unifiedDiff(changes),
5656
+ postconditions,
5657
+ status: "proposed"
5658
+ };
5659
+ return FixProposalV1Schema.parse({ ...draft, contentHash: contentHashForArtifactV1(draft) });
5660
+ };
5661
+ var walkMarkdown = (root, directory = root) => readdirSync4(directory, { withFileTypes: true }).flatMap((entry) => {
5662
+ if (entry.name === ".git" || entry.name === "node_modules" || entry.name === "dist" || entry.name === "build") return [];
5663
+ const path = join18(directory, entry.name);
5664
+ if (entry.isDirectory()) return walkMarkdown(root, path);
5665
+ return entry.isFile() && [".md", ".mdx"].includes(extname2(entry.name).toLowerCase()) ? [relative8(root, path).split(sep8).join("/")] : [];
5666
+ });
5667
+ var localLink = /(!?)\[([^\]]*)\]\(([^)\s]+)(?:\s+["'][^)]*["'])?\)/g;
5668
+ var sortJson = (value) => Array.isArray(value) ? value.map(sortJson) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, item]) => [key, sortJson(item)])) : value;
5669
+ var createMarkdownLinkFixProposal = (root, options) => {
5670
+ const projectRoot = realpathSync8.native(resolve15(root));
5671
+ const paths = walkMarkdown(projectRoot);
5672
+ const changes = [];
5673
+ for (const path of paths) {
5674
+ const content = readFileSync18(join18(projectRoot, path), "utf8");
5675
+ let next = content;
5676
+ for (const match of content.matchAll(localLink)) {
5677
+ const target = match[3];
5678
+ if (!target || match[1] === "!" || /^(?:[a-z]+:|\/|#)/i.test(target)) continue;
5679
+ const targetPath = target.split("#")[0]?.split("?")[0];
5680
+ if (!targetPath || existsSync19(resolve15(projectRoot, dirname9(path), targetPath))) continue;
5681
+ const targetStem = basename5(targetPath, extname2(targetPath)).toLowerCase();
5682
+ const labelStem = (match[2] ?? "").trim().toLowerCase().replace(/[^a-z0-9]+/g, "");
5683
+ const candidates = paths.filter((candidate) => basename5(candidate, extname2(candidate)).toLowerCase() === targetStem || basename5(candidate, extname2(candidate)).toLowerCase().replace(/[^a-z0-9]+/g, "") === labelStem);
5684
+ if (candidates.length !== 1) continue;
5685
+ let replacement = relative8(dirname9(path), candidates[0]).split(sep8).join("/");
5686
+ if (target.startsWith("./") && !replacement.startsWith(".")) replacement = `./${replacement}`;
5687
+ next = next.replace(match[0], match[0].replace(target, replacement));
5688
+ }
5689
+ if (next !== content) changes.push({ path, before: content, after: next });
5690
+ }
5691
+ return changes.length ? makeProposal(projectRoot, options, changes, ["Each replacement has exactly one Markdown target."], ["All corrected local Markdown links resolve."]) : void 0;
5692
+ };
5693
+ var createArtifactNormalizationProposal = (root, artifactPath, options) => {
5694
+ const projectRoot = realpathSync8.native(resolve15(root));
5695
+ const path = artifactPath.split(sep8).join("/");
5696
+ const absolute = containedPath(projectRoot, path);
5697
+ if (!absolute || !existsSync19(absolute) || !statSync5(absolute).isFile()) return void 0;
5698
+ const before = readFileSync18(absolute, "utf8");
5699
+ let after;
5700
+ try {
5701
+ after = `${JSON.stringify(sortJson(JSON.parse(before)), null, 2)}
5702
+ `;
5703
+ } catch {
5704
+ return void 0;
5705
+ }
5706
+ return after === before ? void 0 : makeProposal(projectRoot, options, [{ path: relative8(projectRoot, absolute).split(sep8).join("/"), before, after }], ["The artifact contains valid JSON."], ["The artifact is valid canonical JSON with one trailing newline."]);
5707
+ };
5708
+ var approveFixProposal = (proposalInput, approvedBy, approvedAt = (/* @__PURE__ */ new Date()).toISOString()) => {
5709
+ const proposal = FixProposalV1Schema.parse(proposalInput);
5710
+ if (proposal.status !== "proposed") throw new Error(`Only proposed fixes can be approved; received "${proposal.status}".`);
5711
+ const approved = { ...proposal, status: "approved", approval: { proposalHash: proposal.contentHash, approvedAt, approvedBy }, contentHash: "0".repeat(64) };
5712
+ return FixProposalV1Schema.parse({ ...approved, contentHash: contentHashForArtifactV1(approved) });
5713
+ };
5714
+ var bindingHash = (proposal) => {
5715
+ const { approval: _approval, contentHash: _contentHash, status: _status, ...rest } = proposal;
5716
+ return contentHashForArtifactV1({ ...rest, contentHash: "0".repeat(64), status: "proposed" });
5717
+ };
5718
+ var applyFixProposal = (root, proposalInput, options = {}) => {
5719
+ const proposal = FixProposalV1Schema.parse(proposalInput);
5720
+ if (proposal.status !== "approved" || !proposal.approval) throw new Error("Only an explicitly approved fix proposal can be applied.");
5721
+ if (proposal.approval.proposalHash !== bindingHash(proposal)) throw new Error("Approval is not bound to the exact proposal content.");
5722
+ if (options.currentRevision && options.currentRevision !== proposal.baseRevision) throw new Error("The repository revision changed since this proposal was created.");
5723
+ if (!proposal.changes?.length) throw new Error("This proposal has no executable changes.");
5724
+ const projectRoot = realpathSync8.native(resolve15(root));
5725
+ const originals = /* @__PURE__ */ new Map();
5726
+ const affected = new Map(proposal.affectedFiles.map((file) => [file.path, file]));
5727
+ if (proposal.changes.some((change) => !affected.has(change.path) || affected.get(change.path)?.contentHash !== sha256NormalizedV1(change.before)) || affected.size !== proposal.changes.length) {
5728
+ throw new Error("Proposal changes do not match its affected-file hashes.");
5729
+ }
5730
+ for (const file of proposal.affectedFiles) {
5731
+ const absolute = containedPath(projectRoot, file.path);
5732
+ if (!absolute || !existsSync19(absolute)) throw new Error(`Affected file is unavailable or escapes the repository root: ${file.path}`);
5733
+ const current = readFileSync18(absolute, "utf8");
5734
+ if (sha256NormalizedV1(current) !== file.contentHash) throw new Error(`Affected file changed since proposal creation: ${file.path}`);
5735
+ originals.set(absolute, current);
5736
+ }
5737
+ try {
5738
+ for (const change of proposal.changes) {
5739
+ const absolute = resolve15(projectRoot, change.path);
5740
+ writeFileSync5(`${absolute}.docbridge-${process.pid}.tmp`, change.after, "utf8");
5741
+ }
5742
+ for (const change of proposal.changes) {
5743
+ const absolute = resolve15(projectRoot, change.path);
5744
+ renameSync2(`${absolute}.docbridge-${process.pid}.tmp`, absolute);
5745
+ }
5746
+ for (const change of proposal.changes) {
5747
+ if (readFileSync18(resolve15(projectRoot, change.path), "utf8") !== change.after) throw new Error(`Postcondition failed for ${change.path}`);
5748
+ }
5749
+ options.verify?.(proposal.changes.map((change) => change.path));
5750
+ } catch (error) {
5751
+ for (const [absolute, content] of originals) writeFileSync5(absolute, content, "utf8");
5752
+ for (const change of proposal.changes) {
5753
+ const temp = `${resolve15(projectRoot, change.path)}.docbridge-${process.pid}.tmp`;
5754
+ if (existsSync19(temp)) unlinkSync(temp);
5755
+ }
5756
+ throw error;
5757
+ }
5758
+ const applied = { ...proposal, status: "applied", contentHash: "0".repeat(64) };
5759
+ return FixProposalV1Schema.parse({ ...applied, contentHash: contentHashForArtifactV1(applied) });
5760
+ };
5761
+
5762
+ // src/agents/registry-adapter.ts
5763
+ import { existsSync as existsSync20, readFileSync as readFileSync19, writeFileSync as writeFileSync6, mkdirSync as mkdirSync6 } from "fs";
5764
+ import { join as join19, resolve as resolve16 } from "path";
5765
+ import { pathToFileURL } from "url";
5766
+ import { z as z7 } from "zod";
5767
+ var DEFAULT_REGISTRY_AGENT_ID = "ecosystem-doc-bridge-corpus-scanner";
5768
+ var RegistryAgentMetadataSchema = z7.object({
5769
+ id: z7.string().min(1).max(256),
5770
+ version: z7.string().min(1).max(64),
5771
+ provider: z7.string().min(1).max(128).optional(),
5772
+ model: z7.string().min(1).max(256).optional(),
5773
+ capabilities: z7.array(z7.string().min(1).max(128)).max(32).default([])
5774
+ }).strict();
5775
+ var deepFreeze = (value) => {
5776
+ if (value && typeof value === "object" && !Object.isFrozen(value)) {
5777
+ Object.freeze(value);
5778
+ for (const child of Object.values(value)) deepFreeze(child);
5779
+ }
5780
+ return value;
5781
+ };
5782
+ var registryConfig = (config) => config.intelligence?.registry;
5783
+ var loadRegistryAgentRunner = async (root, config) => {
5784
+ const metadata = loadRegistryAgentMetadata(root, config);
5785
+ const configured = registryConfig(config)?.runnerModule;
5786
+ const modulePath = configured ? containedPath(root, configured) : containedPath(root, join19(metadata.root, "doc-bridge-adapter.js"));
5787
+ if (!modulePath || !existsSync20(modulePath)) throw new Error(`Registry agent "${metadata.id}" has no local runner module. Configure intelligence.registry.runnerModule or add doc-bridge-adapter.js to the installed agent.`);
5788
+ const loaded = await import(pathToFileURL(modulePath).href);
5789
+ const runner = typeof loaded.run === "function" ? loaded.run : typeof loaded.default === "function" ? loaded.default : loaded.default && typeof loaded.default === "object" && "run" in loaded.default && typeof loaded.default.run === "function" ? loaded.default.run : void 0;
5790
+ if (!runner) throw new Error(`Registry agent runner at ${modulePath} must export a function or { run }. `);
5791
+ return runner;
5792
+ };
5793
+ var loadRegistryAgentMetadata = (root, config) => {
5794
+ const settings = registryConfig(config);
5795
+ const id = settings?.agentId ?? DEFAULT_REGISTRY_AGENT_ID;
5796
+ const agentRoot = settings?.agentRoot ?? "agents";
5797
+ const agentPath = containedPath(root, join19(agentRoot, id));
5798
+ if (!agentPath || !existsSync20(agentPath)) throw new Error(`AgentsKit Registry agent "${id}" is not installed at ${join19(agentRoot, id)}. Install it with: npx agentskit add ${id}`);
5799
+ const metadataPath = [join19(agentPath, "agent.json"), join19(agentPath, "manifest.json")].find(existsSync20);
5800
+ if (!metadataPath) throw new Error(`Registry agent "${id}" is installed but has no agent.json or manifest.json metadata.`);
5801
+ const metadata = RegistryAgentMetadataSchema.parse(JSON.parse(readFileSync19(metadataPath, "utf8")));
5802
+ if (metadata.id !== id) throw new Error(`Installed Registry agent metadata id "${metadata.id}" does not match configured id "${id}".`);
5803
+ return { ...metadata, root: agentPath };
5804
+ };
5805
+ var createRegistryAgentAdapter = (root, config, runner) => {
5806
+ if (!registryConfig(config)?.enabled) throw new Error("Registry agents are disabled. Set intelligence.registry.enabled: true to run an assisted workflow.");
5807
+ const metadata = loadRegistryAgentMetadata(resolve16(root), config);
5808
+ return {
5809
+ metadata,
5810
+ run: async (snapshot, report, evidence2 = report.diagnostics.flatMap((diagnostic2) => diagnostic2.evidence).slice(0, 64)) => {
5811
+ const context = deepFreeze({ snapshot: redactValue(snapshot), report: redactValue(report), evidence: redactValue(evidence2), capabilities: ["snapshot.read", "evidence.read", "proposal.write"], network: false, shell: false });
5812
+ const proposal = AgentProposalV1Schema.parse(await runner(context));
5813
+ if (proposal.contentHash !== contentHashForArtifactV1(proposal)) throw new Error("Registry agent proposal contentHash does not match its canonical contents.");
5814
+ if (proposal.baseSnapshotHash !== snapshot.contentHash || proposal.baseReportHash !== report.contentHash) throw new Error("Registry agent proposal is not based on the supplied snapshot/report hashes.");
5815
+ if (proposal.origin.kind !== "registry-agent" || proposal.origin.id !== metadata.id) throw new Error(`Registry agent proposal origin must be ${metadata.id}.`);
5816
+ return proposal;
5817
+ }
5818
+ };
5819
+ };
5820
+ var persistRegistryAgentProposal = (stateDir, proposal) => {
5821
+ AgentProposalV1Schema.parse(proposal);
5822
+ if (proposal.contentHash !== contentHashForArtifactV1(proposal)) throw new Error("Cannot persist a Registry agent proposal with an invalid contentHash.");
5823
+ const safeHash = contentHashForArtifactV1(proposal);
5824
+ mkdirSync6(join19(resolve16(stateDir), "agents"), { recursive: true });
5825
+ const path = join19(resolve16(stateDir), "agents", `${proposal.origin.id}-${safeHash}.json`);
5826
+ writeFileSync6(path, `${JSON.stringify(proposal, null, 2)}
5827
+ `, "utf8");
5828
+ return path;
5829
+ };
5830
+
5831
+ // src/mcp/server.ts
4337
5832
  var MCP_TOOLS = [
4338
5833
  {
4339
5834
  name: "handoff.resolve",
@@ -4405,25 +5900,70 @@ var MCP_TOOLS = [
4405
5900
  description: "Return the static Doc Bridge curator and delegate topology.",
4406
5901
  annotations: { readOnlyHint: true },
4407
5902
  inputSchema: { type: "object", properties: {} }
5903
+ },
5904
+ {
5905
+ name: "docbridge.snapshot",
5906
+ title: "Read the latest discovery snapshot",
5907
+ description: "Read the bounded canonical repository snapshot from the latest workflow run.",
5908
+ annotations: { readOnlyHint: true },
5909
+ inputSchema: { type: "object", properties: { runId: { type: "string" } } }
5910
+ },
5911
+ {
5912
+ name: "docbridge.report",
5913
+ title: "Read the latest reconciliation report",
5914
+ description: "Read the canonical reconciliation report from the latest workflow run.",
5915
+ annotations: { readOnlyHint: true },
5916
+ inputSchema: { type: "object", properties: { runId: { type: "string" } } }
5917
+ },
5918
+ {
5919
+ name: "docbridge.diagnostics",
5920
+ title: "Read reconciliation diagnostics",
5921
+ description: "Read bounded diagnostics from the latest canonical reconciliation report.",
5922
+ annotations: { readOnlyHint: true },
5923
+ inputSchema: { type: "object", properties: { status: { type: "string" }, severity: { type: "string" } } }
5924
+ },
5925
+ {
5926
+ name: "docbridge.relations",
5927
+ title: "Read architecture relations",
5928
+ description: "Read bounded observed and declared relations from the latest canonical snapshot.",
5929
+ annotations: { readOnlyHint: true },
5930
+ inputSchema: { type: "object", properties: { kind: { type: "string" }, limit: { type: "number" } } }
5931
+ },
5932
+ {
5933
+ name: "docbridge.run",
5934
+ title: "Read workflow state",
5935
+ description: "Read the latest resumable workflow state and artifact references.",
5936
+ annotations: { readOnlyHint: true },
5937
+ inputSchema: { type: "object", properties: {} }
5938
+ },
5939
+ {
5940
+ name: "docbridge.proposals",
5941
+ title: "Read or approve proposals",
5942
+ description: "Create, inspect, approve and apply deterministic proposals through the shared human-gated workflow.",
5943
+ inputSchema: { type: "object", properties: { action: { type: "string", enum: ["list", "propose-links", "propose-normalize", "suggest", "approve", "apply"] }, proposalHash: { type: "string" }, artifactPath: { type: "string" }, approvedBy: { type: "string" }, proposal: { type: "object" } } }
4408
5944
  }
4409
5945
  ];
4410
5946
  var asRecord = (value) => value && typeof value === "object" ? value : {};
4411
- var HandoffResolveArgsSchema = z6.object({
4412
- id: z6.string().min(1),
4413
- kind: z6.enum(["package", "ownership"]).optional()
5947
+ var HandoffResolveArgsSchema = z8.object({
5948
+ id: z8.string().min(1),
5949
+ kind: z8.enum(["package", "ownership"]).optional()
4414
5950
  });
4415
- var DocSearchArgsSchema = z6.object({
4416
- term: z6.string().min(1),
4417
- limit: z6.number().int().positive().max(100).optional()
5951
+ var DocSearchArgsSchema = z8.object({
5952
+ term: z8.string().min(1),
5953
+ limit: z8.number().int().positive().max(100).optional()
4418
5954
  });
4419
- var RetrieverQueryArgsSchema = z6.object({
4420
- query: z6.string().min(1),
4421
- limit: z6.number().int().positive().max(100).optional()
5955
+ var RetrieverQueryArgsSchema = z8.object({
5956
+ query: z8.string().min(1),
5957
+ limit: z8.number().int().positive().max(100).optional()
4422
5958
  });
4423
- var DocGetArgsSchema = z6.object({
4424
- id: z6.string().min(1).optional(),
4425
- path: z6.string().min(1).optional()
5959
+ var DocGetArgsSchema = z8.object({
5960
+ id: z8.string().min(1).optional(),
5961
+ path: z8.string().min(1).optional()
4426
5962
  }).refine((args) => args.id || args.path, "doc.get requires id or path");
5963
+ var WorkflowRunArgsSchema = z8.object({ runId: z8.string().min(1).optional() });
5964
+ var DiagnosticsArgsSchema = z8.object({ status: z8.string().min(1).optional(), severity: z8.string().min(1).optional() });
5965
+ var RelationsArgsSchema = z8.object({ kind: z8.string().min(1).optional(), limit: z8.number().int().positive().max(500).optional() });
5966
+ var ProposalsArgsSchema = z8.object({ action: z8.enum(["list", "propose-links", "propose-normalize", "suggest", "approve", "apply"]).optional(), proposalHash: z8.string().min(1).optional(), artifactPath: z8.string().min(1).optional(), approvedBy: z8.string().min(1).optional(), proposal: z8.unknown().optional() });
4427
5967
  var parseToolArgs = (tool, schema, value) => {
4428
5968
  try {
4429
5969
  return schema.parse(value);
@@ -4454,15 +5994,54 @@ var findDocPath = (index, args) => {
4454
5994
  return doc.path;
4455
5995
  };
4456
5996
  var resolveDocPath = (root, relPath) => {
4457
- const rootAbs = realpathSync7.native(root);
4458
- const unresolved = resolve12(rootAbs, relPath);
4459
- const unresolvedRel = relative5(rootAbs, unresolved);
5997
+ const rootAbs = realpathSync9.native(root);
5998
+ const unresolved = resolve17(rootAbs, relPath);
5999
+ const unresolvedRel = relative9(rootAbs, unresolved);
4460
6000
  if (unresolvedRel.startsWith("..")) throw new Error("doc.get path escapes project root");
4461
- const abs = realpathSync7.native(unresolved);
4462
- const rel = relative5(rootAbs, abs);
6001
+ const abs = realpathSync9.native(unresolved);
6002
+ const rel = relative9(rootAbs, abs);
4463
6003
  if (rel.startsWith("..")) throw new Error("doc.get path escapes project root");
4464
6004
  return abs;
4465
6005
  };
6006
+ var workflowStateDir = (ctx) => resolve17(ctx.root, ctx.config.workflow?.stateDir ?? ".doc-bridge/workflow");
6007
+ var workflowRun = (ctx) => loadWorkflowManifest(workflowStateDir(ctx));
6008
+ var ensureLatestRun = (ctx, runId2) => {
6009
+ const run2 = workflowRun(ctx);
6010
+ if (runId2 && run2.runId !== runId2) throw new Error(`Unknown workflow run "${runId2}"`);
6011
+ if (run2.state === "stale" || run2.state === "failed") throw new Error(`Workflow run is ${run2.state}; resume or create a valid run before reading artifacts.`);
6012
+ return run2;
6013
+ };
6014
+ var workflowSnapshot = (ctx, runId2) => {
6015
+ ensureLatestRun(ctx, runId2);
6016
+ return parseDiscoverySnapshot(loadWorkflowStepOutput(workflowStateDir(ctx), "normalize"));
6017
+ };
6018
+ var workflowReport = (ctx, runId2) => {
6019
+ ensureLatestRun(ctx, runId2);
6020
+ return parseReconciliationReport(loadWorkflowStepOutput(workflowStateDir(ctx), "reconcile"));
6021
+ };
6022
+ var proposalPath = (ctx) => join20(ctx.root, ".doc-bridge", "proposal.json");
6023
+ var readSavedProposal = (ctx, input) => {
6024
+ if (input !== void 0) return FixProposalV1Schema.parse(input);
6025
+ try {
6026
+ return FixProposalV1Schema.parse(JSON.parse(readFileSync20(proposalPath(ctx), "utf8")));
6027
+ } catch {
6028
+ throw new Error(`No saved fix proposal at ${proposalPath(ctx)}.`);
6029
+ }
6030
+ };
6031
+ var saveProposal = (ctx, proposal) => {
6032
+ mkdirSync7(join20(ctx.root, ".doc-bridge"), { recursive: true });
6033
+ writeFileSync7(proposalPath(ctx), `${JSON.stringify(proposal, null, 2)}
6034
+ `, "utf8");
6035
+ };
6036
+ var enabledMcpTools = (ctx) => {
6037
+ const configured = ctx.config.surfaces?.mcp?.tools;
6038
+ if (!configured || configured.length === MCP_TOOLS.length && MCP_TOOLS.every((tool) => configured.includes(tool.name))) return MCP_TOOLS;
6039
+ return MCP_TOOLS.filter((tool) => configured.includes(tool.name));
6040
+ };
6041
+ var assertMcpToolEnabled = (ctx, name) => {
6042
+ if (!MCP_TOOLS.some((tool) => tool.name === name)) throw new Error(`Unknown tool "${name}"`);
6043
+ if (!enabledMcpTools(ctx).some((tool) => tool.name === name)) throw new Error(`MCP tool "${name}" is disabled by configuration.`);
6044
+ };
4466
6045
  var handleMcpRequest = (ctx, request) => {
4467
6046
  if (request.method === "initialize") {
4468
6047
  return {
@@ -4471,11 +6050,13 @@ var handleMcpRequest = (ctx, request) => {
4471
6050
  serverInfo: { name: "ak-docs", version: PACKAGE_VERSION }
4472
6051
  };
4473
6052
  }
4474
- if (request.method === "tools/list") return { tools: MCP_TOOLS };
6053
+ if (request.method === "tools/list") return { tools: enabledMcpTools(ctx) };
4475
6054
  if (request.method === "tools/call") {
4476
6055
  const params = asRecord(request.params);
4477
6056
  const name = params.name;
4478
6057
  const args = asRecord(params.arguments);
6058
+ if (typeof name !== "string") throw new Error("MCP tools/call requires a tool name.");
6059
+ assertMcpToolEnabled(ctx, name);
4479
6060
  const index = () => ctx.loadIndex?.() ?? loadDocBridgeIndex(ctx.root, ctx.config);
4480
6061
  if (name === "handoff.resolve") {
4481
6062
  const parsed = parseToolArgs("handoff.resolve", HandoffResolveArgsSchema, args);
@@ -4493,7 +6074,7 @@ var handleMcpRequest = (ctx, request) => {
4493
6074
  }
4494
6075
  if (name === "doc.get") {
4495
6076
  const relPath = findDocPath(index(), parseToolArgs("doc.get", DocGetArgsSchema, args));
4496
- return textResult(readFileSync16(resolveDocPath(ctx.root, relPath), "utf8"));
6077
+ return textResult(readFileSync20(resolveDocPath(ctx.root, relPath), "utf8"));
4497
6078
  }
4498
6079
  if (name === "gate.status") return textResult(runGates(ctx.root, ctx.config));
4499
6080
  if (name === "retriever.query") {
@@ -4515,6 +6096,81 @@ var handleMcpRequest = (ctx, request) => {
4515
6096
  mergePolicy: { autoMerge: false, requiresHuman: true }
4516
6097
  });
4517
6098
  }
6099
+ if (name === "docbridge.snapshot") {
6100
+ const parsed = parseToolArgs("docbridge.snapshot", WorkflowRunArgsSchema, args);
6101
+ return textResult(redactValue(workflowSnapshot(ctx, parsed.runId)));
6102
+ }
6103
+ if (name === "docbridge.report") {
6104
+ const parsed = parseToolArgs("docbridge.report", WorkflowRunArgsSchema, args);
6105
+ return textResult(redactValue(workflowReport(ctx, parsed.runId)));
6106
+ }
6107
+ if (name === "docbridge.diagnostics") {
6108
+ const parsed = parseToolArgs("docbridge.diagnostics", DiagnosticsArgsSchema, args);
6109
+ const diagnostics = workflowReport(ctx).diagnostics.filter(
6110
+ (diagnostic2) => (!parsed.status || diagnostic2.status === parsed.status) && (!parsed.severity || diagnostic2.severity === parsed.severity)
6111
+ );
6112
+ return textResult(redactValue({ reportHash: workflowReport(ctx).contentHash, diagnostics }));
6113
+ }
6114
+ if (name === "docbridge.relations") {
6115
+ const parsed = parseToolArgs("docbridge.relations", RelationsArgsSchema, args);
6116
+ const snapshot = workflowSnapshot(ctx);
6117
+ return textResult({ snapshotHash: snapshot.contentHash, relations: snapshot.relations.filter((relation) => !parsed.kind || relation.kind === parsed.kind).slice(0, parsed.limit ?? 100) });
6118
+ }
6119
+ if (name === "docbridge.run") {
6120
+ parseToolArgs("docbridge.run", z8.object({}), args);
6121
+ return textResult(workflowRun(ctx));
6122
+ }
6123
+ if (name === "docbridge.proposals") {
6124
+ const parsed = parseToolArgs("docbridge.proposals", ProposalsArgsSchema, args);
6125
+ const run2 = (() => {
6126
+ try {
6127
+ return workflowRun(ctx);
6128
+ } catch {
6129
+ return void 0;
6130
+ }
6131
+ })();
6132
+ if (!parsed.action || parsed.action === "list") {
6133
+ let proposal2;
6134
+ try {
6135
+ proposal2 = readSavedProposal(ctx, void 0);
6136
+ } catch {
6137
+ proposal2 = void 0;
6138
+ }
6139
+ return textResult(redactValue({ ...run2 ? { runId: run2.runId } : {}, proposals: proposal2 ? [proposal2] : [] }));
6140
+ }
6141
+ if (parsed.action === "suggest") {
6142
+ return loadRegistryAgentRunner(ctx.root, ctx.config).then(async (runner) => {
6143
+ const snapshot = workflowSnapshot(ctx);
6144
+ const report = workflowReport(ctx);
6145
+ const adapter = createRegistryAgentAdapter(ctx.root, ctx.config, runner);
6146
+ const proposal2 = await adapter.run(snapshot, report);
6147
+ const savedPath = persistRegistryAgentProposal(workflowStateDir(ctx), proposal2);
6148
+ return textResult(redactValue({ ...run2 ? { runId: run2.runId } : {}, proposal: proposal2, proposalPath: savedPath }));
6149
+ });
6150
+ }
6151
+ const discovered = discoverRepository({ root: ctx.root, config: ctx.config });
6152
+ const options = { baseRevision: discovered.sourceRevision, configurationHash: sha256NormalizedV1(ctx.config), ...ctx.config.project?.name ? { projectName: ctx.config.project.name } : {} };
6153
+ if (parsed.action === "propose-links") {
6154
+ const proposal2 = createMarkdownLinkFixProposal(ctx.root, options);
6155
+ if (proposal2) saveProposal(ctx, proposal2);
6156
+ return textResult(redactValue({ ...run2 ? { runId: run2.runId } : {}, proposal: proposal2 ?? null }));
6157
+ }
6158
+ if (parsed.action === "propose-normalize") {
6159
+ if (!parsed.artifactPath) throw new Error("docbridge.proposals propose-normalize requires artifactPath");
6160
+ const proposal2 = createArtifactNormalizationProposal(ctx.root, parsed.artifactPath, options);
6161
+ if (proposal2) saveProposal(ctx, proposal2);
6162
+ return textResult(redactValue({ ...run2 ? { runId: run2.runId } : {}, proposal: proposal2 ?? null }));
6163
+ }
6164
+ if (parsed.action === "approve") {
6165
+ const proposal2 = approveFixProposal(readSavedProposal(ctx, parsed.proposal), parsed.approvedBy ?? "human");
6166
+ if (parsed.proposalHash && proposal2.approval?.proposalHash !== parsed.proposalHash) throw new Error("proposalHash does not match the saved proposal");
6167
+ saveProposal(ctx, proposal2);
6168
+ return textResult(redactValue({ ...run2 ? { runId: run2.runId } : {}, proposal: proposal2 }));
6169
+ }
6170
+ const proposal = applyFixProposal(ctx.root, readSavedProposal(ctx, parsed.proposal), { currentRevision: discovered.sourceRevision });
6171
+ saveProposal(ctx, proposal);
6172
+ return textResult(redactValue({ ...run2 ? { runId: run2.runId } : {}, proposal }));
6173
+ }
4518
6174
  throw new Error(`Unknown tool "${String(name)}"`);
4519
6175
  }
4520
6176
  if (request.method?.startsWith("notifications/")) return void 0;
@@ -4527,16 +6183,16 @@ var writeFrame = (payload, framing) => {
4527
6183
  \r
4528
6184
  ${body}`);
4529
6185
  };
4530
- var respond = (ctx, request, framing) => {
6186
+ var respondMcpRequest = async (ctx, request, framing) => {
4531
6187
  if (request.id === void 0) {
4532
6188
  try {
4533
- handleMcpRequest(ctx, request);
6189
+ await handleMcpRequest(ctx, request);
4534
6190
  } catch {
4535
6191
  }
4536
6192
  return;
4537
6193
  }
4538
6194
  try {
4539
- const result = handleMcpRequest(ctx, request);
6195
+ const result = await handleMcpRequest(ctx, request);
4540
6196
  writeFrame({ jsonrpc: "2.0", id: request.id, result: result ?? {} }, framing);
4541
6197
  } catch (error) {
4542
6198
  writeFrame({
@@ -4548,6 +6204,10 @@ var respond = (ctx, request, framing) => {
4548
6204
  };
4549
6205
  var startMcpStdioServer = (ctx) => {
4550
6206
  let buffer = Buffer.alloc(0);
6207
+ let responseChain = Promise.resolve();
6208
+ const enqueueResponse = (request, framing) => {
6209
+ responseChain = responseChain.then(() => respondMcpRequest(ctx, request, framing));
6210
+ };
4551
6211
  process.stdin.on("data", (chunk) => {
4552
6212
  buffer = Buffer.concat([buffer, chunk]);
4553
6213
  while (true) {
@@ -4566,7 +6226,7 @@ var startMcpStdioServer = (ctx) => {
4566
6226
  if (buffer.length < bodyEnd) return;
4567
6227
  const raw2 = buffer.subarray(bodyStart, bodyEnd).toString("utf8");
4568
6228
  buffer = buffer.subarray(bodyEnd);
4569
- respond(ctx, JSON.parse(raw2), "content-length");
6229
+ enqueueResponse(JSON.parse(raw2), "content-length");
4570
6230
  continue;
4571
6231
  }
4572
6232
  const lineEnd = buffer.indexOf("\n");
@@ -4575,7 +6235,7 @@ var startMcpStdioServer = (ctx) => {
4575
6235
  buffer = buffer.subarray(lineEnd + 1);
4576
6236
  if (!raw) continue;
4577
6237
  try {
4578
- respond(ctx, JSON.parse(raw), "json-line");
6238
+ enqueueResponse(JSON.parse(raw), "json-line");
4579
6239
  } catch {
4580
6240
  writeFrame({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } }, "json-line");
4581
6241
  }
@@ -4584,6 +6244,237 @@ var startMcpStdioServer = (ctx) => {
4584
6244
  process.stdin.resume();
4585
6245
  };
4586
6246
 
6247
+ // src/reconciliation/reconcile.ts
6248
+ var ignoredDocumentationRelations = /* @__PURE__ */ new Set(["covers"]);
6249
+ var metadataDetection = (relation) => {
6250
+ const detection = relation.metadata?.detection;
6251
+ return typeof detection === "string" ? detection : void 0;
6252
+ };
6253
+ var relationDetection = (relation) => relation.discriminator ?? metadataDetection(relation) ?? "static";
6254
+ var entityResolver = (snapshots) => {
6255
+ const references = /* @__PURE__ */ new Map();
6256
+ const entities = snapshots.flatMap((snapshot) => snapshot.entities).sort((a, b) => a.id.localeCompare(b.id));
6257
+ for (const entity of entities) {
6258
+ references.set(entity.id, entity.id);
6259
+ for (const alias of entity.aliases ?? []) {
6260
+ if (!references.has(alias)) references.set(alias, entity.id);
6261
+ }
6262
+ }
6263
+ return (reference) => references.get(reference) ?? reference;
6264
+ };
6265
+ var relationBase = (relation, resolveEntity2) => `${resolveEntity2(relation.from)}\0${resolveEntity2(relation.to)}\0${relation.kind}`;
6266
+ var evidenceKey = (item) => `${item.source}:${item.path}:${item.lineStart ?? ""}:${item.lineEnd ?? ""}:${item.context ?? ""}`;
6267
+ var mergeEvidence = (...relations) => {
6268
+ const merged = /* @__PURE__ */ new Map();
6269
+ for (const relation of relations) {
6270
+ for (const item of relation.evidence) merged.set(evidenceKey(item), item);
6271
+ }
6272
+ return [...merged.values()].sort((a, b) => evidenceKey(a).localeCompare(evidenceKey(b)));
6273
+ };
6274
+ var diagnosticId = (code, value) => `reconciliation:${code}:${sha256NormalizedV1(value).slice(0, 32)}`;
6275
+ var coverageAvailable = (snapshot, relation) => {
6276
+ const status = snapshot.coverage.find(
6277
+ (entry) => entry.scope === "static-imports-and-exports" && (relation.kind === "imports" || relation.kind === "re-exports")
6278
+ )?.status;
6279
+ return status === void 0 || status === "complete";
6280
+ };
6281
+ var entityById = (snapshots) => {
6282
+ const entities = /* @__PURE__ */ new Map();
6283
+ for (const snapshot of snapshots) {
6284
+ for (const entity of snapshot.entities) if (!entities.has(entity.id)) entities.set(entity.id, entity);
6285
+ }
6286
+ return entities;
6287
+ };
6288
+ var isUnresolved = (id, entities) => id.startsWith("unresolved:") || entities.get(id)?.kind === "unresolved-reference";
6289
+ var relationMatches = (observed, declared, resolveEntity2) => {
6290
+ if (relationBase(observed, resolveEntity2) !== relationBase(declared, resolveEntity2)) return false;
6291
+ return relationDetection(declared) === relationDetection(observed);
6292
+ };
6293
+ var reportDiagnostic = (code, status, severity, message, evidence2, value, entityIds, relationIds, remediation) => ({
6294
+ id: diagnosticId(code, value),
6295
+ code,
6296
+ status,
6297
+ severity,
6298
+ message,
6299
+ evidence: [...evidence2],
6300
+ ...entityIds?.length ? { entityIds: [...entityIds] } : {},
6301
+ ...relationIds?.length ? { relationIds: [...relationIds] } : {},
6302
+ ...remediation ? { remediation } : {}
6303
+ });
6304
+ var reconcileKnowledge = (observed, declared) => {
6305
+ const resolveEntity2 = entityResolver([observed, declared]);
6306
+ const entities = entityById([observed, declared]);
6307
+ const observedRelations = observed.relations.filter((relation) => relation.provenance === "observed" && !ignoredDocumentationRelations.has(relation.kind));
6308
+ const declaredRelations = declared.relations.filter((relation) => relation.provenance === "declared" && !ignoredDocumentationRelations.has(relation.kind));
6309
+ const diagnostics = [];
6310
+ for (const entity of declared.entities.filter((item) => isUnresolved(item.id, entities)).sort((a, b) => a.id.localeCompare(b.id))) {
6311
+ diagnostics.push(reportDiagnostic(
6312
+ "UNRESOLVED_ENTITY_REFERENCE",
6313
+ "unresolved",
6314
+ "error",
6315
+ `Declared reference could not be resolved: ${entity.name}.`,
6316
+ entity.evidence,
6317
+ entity.id,
6318
+ [entity.id],
6319
+ void 0,
6320
+ "Resolve the reference to an observed entity ID or configured alias."
6321
+ ));
6322
+ }
6323
+ const declaredByBase = /* @__PURE__ */ new Map();
6324
+ for (const relation of declaredRelations) {
6325
+ const base2 = relationBase(relation, resolveEntity2);
6326
+ const group = declaredByBase.get(base2) ?? [];
6327
+ group.push(relation);
6328
+ declaredByBase.set(base2, group);
6329
+ }
6330
+ for (const group of declaredByBase.values()) {
6331
+ const detections = new Set(group.map(relationDetection));
6332
+ if (detections.size < 2) continue;
6333
+ diagnostics.push(reportDiagnostic(
6334
+ "CONFLICTING_DECLARATIONS",
6335
+ "conflict",
6336
+ "error",
6337
+ "Declarations for the same semantic relation disagree on detection.",
6338
+ mergeEvidence(...group),
6339
+ [relationBase(group[0], resolveEntity2), [...detections].sort()],
6340
+ void 0,
6341
+ group.map((relation) => relation.id),
6342
+ "Keep one detection value for this relation or document the intended distinction with a different relation kind."
6343
+ ));
6344
+ }
6345
+ for (const relation of observedRelations) {
6346
+ const candidates = declaredByBase.get(relationBase(relation, resolveEntity2)) ?? [];
6347
+ const match = candidates.find((candidate) => relationMatches(relation, candidate, resolveEntity2));
6348
+ if (match) {
6349
+ diagnostics.push(reportDiagnostic(
6350
+ "RELATION_CONFIRMED",
6351
+ "confirmed",
6352
+ "info",
6353
+ "Observed relation is covered by a matching declaration.",
6354
+ mergeEvidence(relation, match),
6355
+ relation.id,
6356
+ void 0,
6357
+ [relation.id, match.id]
6358
+ ));
6359
+ } else if (coverageAvailable(observed, relation)) {
6360
+ diagnostics.push(reportDiagnostic(
6361
+ "RELATION_UNDOCUMENTED",
6362
+ "undocumented",
6363
+ "warn",
6364
+ "Observed relation has no matching documentation declaration.",
6365
+ relation.evidence,
6366
+ relation.id,
6367
+ void 0,
6368
+ [relation.id],
6369
+ "Add a matching relation declaration or configure this relation kind as intentionally undocumented."
6370
+ ));
6371
+ }
6372
+ }
6373
+ for (const relation of declaredRelations) {
6374
+ const candidates = observedRelations.filter((candidate) => relationBase(candidate, resolveEntity2) === relationBase(relation, resolveEntity2));
6375
+ const detection = relationDetection(relation);
6376
+ if (candidates.some((candidate) => relationMatches(candidate, relation, resolveEntity2))) continue;
6377
+ if (isUnresolved(resolveEntity2(relation.from), entities) || isUnresolved(resolveEntity2(relation.to), entities)) continue;
6378
+ if (detection === "dynamic" || detection === "external") {
6379
+ diagnostics.push(reportDiagnostic(
6380
+ "RELATION_NOT_ANALYZED",
6381
+ "not-analyzed",
6382
+ "info",
6383
+ `Declared ${detection} relation cannot be verified by the current static analyzer.`,
6384
+ relation.evidence,
6385
+ relation.id,
6386
+ void 0,
6387
+ [relation.id],
6388
+ "Enable a compatible analyzer or provide explicit observed evidence before treating this relation as confirmed."
6389
+ ));
6390
+ } else if (coverageAvailable(observed, relation)) {
6391
+ diagnostics.push(reportDiagnostic(
6392
+ "DECLARED_RELATION_STALE",
6393
+ "stale-or-unverified",
6394
+ "warn",
6395
+ candidates.length ? "Declared relation has incompatible observed evidence." : "Declared static relation was not observed.",
6396
+ candidates.length ? mergeEvidence(relation, ...candidates) : relation.evidence,
6397
+ relation.id,
6398
+ void 0,
6399
+ [relation.id, ...candidates.map((candidate) => candidate.id)],
6400
+ "Update the declaration or the implementation so both graphs describe the same relation."
6401
+ ));
6402
+ }
6403
+ }
6404
+ const sortedDiagnostics = [...diagnostics].sort((a, b) => a.id.localeCompare(b.id));
6405
+ const base = {
6406
+ type: "reconciliation-report",
6407
+ schemaVersion: 1,
6408
+ contentHash: "0".repeat(64),
6409
+ contentHashAlgo: observed.contentHashAlgo,
6410
+ project: observed.project,
6411
+ sourceRevision: observed.sourceRevision,
6412
+ sourceRevisionKind: observed.sourceRevisionKind,
6413
+ configurationHash: observed.configurationHash,
6414
+ pipelineVersion: observed.pipelineVersion,
6415
+ analyzerVersions: observed.analyzerVersions,
6416
+ snapshotHash: observed.contentHash,
6417
+ diagnostics: sortedDiagnostics,
6418
+ summary: {
6419
+ entityCount: observed.entities.length,
6420
+ relationCount: observedRelations.length,
6421
+ diagnosticCount: sortedDiagnostics.length
6422
+ }
6423
+ };
6424
+ return ReconciliationReportV1Schema.parse({ ...base, contentHash: contentHashForArtifactV1(base) });
6425
+ };
6426
+
6427
+ // src/report/html.ts
6428
+ var escapeHtml = (value) => String(value).replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&#39;");
6429
+ var anchor = (prefix, value) => `${prefix}-${value.replace(/[^A-Za-z0-9_-]+/g, "-")}`;
6430
+ var evidenceText = (evidence2, includeSnippets) => {
6431
+ const location = `${evidence2.path}${evidence2.lineStart ? `:${evidence2.lineStart}${evidence2.lineEnd && evidence2.lineEnd !== evidence2.lineStart ? `-${evidence2.lineEnd}` : ""}` : ""}`;
6432
+ return `${location}${includeSnippets && evidence2.context ? ` \u2014 ${redactSecrets(evidence2.context)}` : ""}`;
6433
+ };
6434
+ var errorPage = (message) => `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Doc Bridge report error</title><style>body{font:16px system-ui;margin:3rem;color:#311}main{max-width:60rem;margin:auto;border:1px solid #d99;padding:2rem;border-radius:8px;background:#fff8f8}code{white-space:pre-wrap}</style></head><body><main><h1>Doc Bridge report unavailable</h1><p>The saved snapshot/report could not be rendered.</p><code>${escapeHtml(message)}</code><p>Run <code>ak-docs check</code> to regenerate valid artifacts.</p></main></body></html>`;
6435
+ var embeddedJson = (value) => JSON.stringify(value).replaceAll("<", "\\u003c");
6436
+ var controls = (statusOptions) => `<section id="controls"><label>Search <input id="search" type="search" placeholder="entity, relation, diagnostic"></label><label>Severity <select id="severity"><option value="">Any</option><option>error</option><option>warn</option><option>info</option></select></label><label>Provenance <select id="provenance"><option value="">Any</option><option>observed</option><option>declared</option><option>proposed</option></select></label><label>Status <select id="status"><option value="">Any</option>${statusOptions.map((value) => `<option>${value}</option>`).join("")}</select></label><label>Analyzer <input id="analyzer" type="search" placeholder="js-ts, report"></label><label>Entity <input id="entity" type="search" placeholder="entity id"></label><label>Relation <input id="relation" type="search" placeholder="relation id"></label><button id="reset">Reset filters</button></section>`;
6437
+ var renderCompact = (snapshot, report, entityAnchors, includeSnippets) => {
6438
+ const entities = snapshot.entities.map((entity) => ({ id: entity.id, name: entity.name, kind: entity.kind, anchor: entityAnchors.get(entity.id) ?? anchor("entity", entity.id) }));
6439
+ const entityIndexes = new Map(entities.map((entity, index) => [entity.id, index]));
6440
+ const relations = snapshot.relations.map((relation) => ({ id: relation.id, from: entityIndexes.get(relation.from), to: entityIndexes.get(relation.to), kind: relation.kind, provenance: relation.provenance }));
6441
+ const diagnostics = report.diagnostics.map((diagnostic2) => ({
6442
+ ...diagnostic2,
6443
+ evidence: diagnostic2.evidence.map(({ context, ...evidence2 }) => includeSnippets && context ? { ...evidence2, context: redactSecrets(context) } : evidence2)
6444
+ }));
6445
+ const data = embeddedJson({ entities, relations, diagnostics, coverage: snapshot.coverage });
6446
+ const script = `const data=${data};const esc=(v)=>String(v).replaceAll('&','&amp;').replaceAll('<','&lt;').replaceAll('>','&gt;').replaceAll('"','&quot;').replaceAll("'",'&#39;');const entityById=new Map(data.entities.map((e)=>[e.id,e]));const entityLink=(id)=>{const e=entityById.get(id);return '<a href="#'+esc(e?.anchor??'entity-'+id)+'">'+esc(e?.name??id)+'</a>'};const evidenceText=(e)=>esc(e.path+(e.lineStart?':'+e.lineStart+(e.lineEnd&&e.lineEnd!==e.lineStart?'-'+e.lineEnd:''):'')+(e.context?' \u2014 '+e.context:''));const render=()=>{document.querySelector('#map').innerHTML=data.entities.map((e)=>'<div class="node" id="'+esc(e.anchor)+'" data-node="'+esc(e.anchor)+'" data-search="'+esc([e.id,e.name,e.kind].join(' '))+'"><a href="#'+esc(e.anchor)+'">'+esc(e.name)+'</a><br><span class="muted">'+esc(e.kind)+'</span></div>').join('');document.querySelector('#relations').innerHTML=data.relations.map((r)=>{const from=data.entities[r.from],to=data.entities[r.to];return '<tr id="relation-'+esc(r.id)+'" data-relation="'+esc(r.id)+'" data-from="'+esc(from?.anchor??'')+'" data-to="'+esc(to?.anchor??'')+'" data-kind="'+esc(r.kind)+'" data-provenance="'+esc(r.provenance)+'" data-analyzer="snapshot" data-search="'+esc([r.id,from?.id,to?.id,r.kind,r.provenance].join(' '))+'"><td>'+esc(r.kind)+'</td><td>'+entityLink(from?.id??'')+'</td><td>'+entityLink(to?.id??'')+'</td><td>'+esc(r.provenance)+'</td></tr>'}).join('');document.querySelector('#diagnostics').innerHTML=data.diagnostics.length?data.diagnostics.map((d)=>'<article id="diagnostic-'+esc(d.id)+'" class="diagnostic" data-status="'+esc(d.status)+'" data-severity="'+esc(d.severity)+'" data-analyzer="report" data-entity="'+esc((d.entityIds??[]).join(' '))+'" data-relation="'+esc((d.relationIds??[]).join(' '))+'" data-search="'+esc([d.id,d.code,d.message,...(d.entityIds??[]),...(d.relationIds??[])].join(' '))+'"><h3><a href="#diagnostic-'+esc(d.id)+'">'+esc(d.code)+'</a> <span class="badge '+esc(d.severity)+'">'+esc(d.severity)+'</span></h3><p>'+esc(d.message)+'</p><p>Status: <b>'+esc(d.status)+'</b></p><ul>'+d.evidence.map((e)=>'<li>'+evidenceText(e)+'</li>').join('')+'</ul>'+(d.entityIds?.length?'<p>Entities: '+d.entityIds.map(entityLink).join(', ')+'</p>':'')+'</article>').join(''):'<p class="muted">No diagnostics.</p>';document.querySelector('#coverage').innerHTML=data.coverage.length?data.coverage.map((c)=>'<li data-status="'+esc(c.status)+'" data-analyzer="'+esc(c.analyzer)+'" data-search="'+esc([c.analyzer,c.scope,c.status,c.reason??''].join(' '))+'"><b>'+esc(c.analyzer)+'</b> / '+esc(c.scope)+': '+esc(c.status)+(c.reason?' \u2014 '+esc(c.reason):'')+'</li>').join(''):'<li class="muted">No coverage metadata.</li>';document.querySelectorAll('[data-node]').forEach((node)=>node.onclick=()=>{const id=node.dataset.node;document.querySelectorAll('[data-from],[data-to]').forEach((edge)=>edge.classList.toggle('hidden',edge.dataset.from!==id&&edge.dataset.to!==id))});};const apply=()=>{const q=document.querySelector('#search').value.toLowerCase(),severity=document.querySelector('#severity').value,provenance=document.querySelector('#provenance').value,status=document.querySelector('#status').value,analyzer=document.querySelector('#analyzer').value.toLowerCase(),entity=document.querySelector('#entity').value.toLowerCase(),relation=document.querySelector('#relation').value.toLowerCase();document.querySelectorAll('[data-search]').forEach((el)=>el.classList.toggle('hidden',Boolean(q&&!el.dataset.search.toLowerCase().includes(q)||severity&&el.dataset.severity!==severity||provenance&&el.dataset.provenance!==provenance||status&&el.dataset.status!==status||analyzer&&!el.dataset.analyzer?.toLowerCase().includes(analyzer)||entity&&!el.dataset.entity?.toLowerCase().includes(entity)||relation&&!el.dataset.relation?.toLowerCase().includes(relation))));};render();for(const id of ['search','analyzer','entity','relation'])document.querySelector('#'+id).oninput=apply;for(const id of ['severity','provenance','status'])document.querySelector('#'+id).onchange=apply;document.querySelector('#reset').onclick=()=>{for(const id of ['search','analyzer','entity','relation'])document.querySelector('#'+id).value='';for(const id of ['severity','provenance','status'])document.querySelector('#'+id).value='';apply()};`;
6447
+ return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Doc Bridge \u2014 ${escapeHtml(snapshot.project.name)}</title><style>body{font:14px system-ui;margin:0;color:#18202a;background:#f5f7fa}header,main{max-width:1200px;margin:auto;padding:1.25rem}header{background:#18202a;color:white;max-width:none;padding-left:calc((100% - 1200px)/2);padding-right:calc((100% - 1200px)/2)}main{background:white}section{margin:1.5rem 0;border-top:1px solid #d7dde5;padding-top:1rem}table{border-collapse:collapse;width:100%;margin-top:.75rem}td,th{border-bottom:1px solid #e5e9ef;text-align:left;padding:.45rem;vertical-align:top}input,select,button{padding:.45rem;margin:.15rem;border:1px solid #b9c3d0;border-radius:4px;background:white}.badge{border-radius:1rem;padding:.15rem .5rem;background:#dce4ee}.error{background:#ffd9d9}.warn{background:#fff0c2}.info{background:#dcecff}.diagnostic{border:1px solid #d7dde5;border-left:4px solid #9aa7b5;padding:.75rem;margin:.75rem 0}.diagnostic:target,tr:target{background:#fff8cf}.muted{color:#5d6a78}a{color:#0b5cad}#map{display:flex;gap:1rem;flex-wrap:wrap}.node{border:1px solid #9aa7b5;padding:.5rem;border-radius:4px}.hidden{display:none}</style></head><body><header><h1>Doc Bridge: ${escapeHtml(snapshot.project.name)}</h1><p>Offline architecture and documentation reconciliation report</p><p class="muted">Snapshot ${escapeHtml(snapshot.contentHash)} \xB7 Report ${escapeHtml(report.contentHash)} \xB7 Revision ${escapeHtml(snapshot.sourceRevision)}</p></header><main>${controls(["confirmed", "undocumented", "stale-or-unverified", "conflict", "unresolved", "not-analyzed"])}<section id="architecture"><h2>Architecture map</h2><p>Large snapshots are rendered from compact canonical data after load to keep the offline report bounded.</p><div id="map"></div><h3>Relations</h3><table><thead><tr><th>Kind</th><th>From</th><th>To</th><th>Provenance</th></tr></thead><tbody id="relations"></tbody></table></section><section id="diagnostic-lens"><h2>Diagnostic lens</h2><p>Findings: ${report.diagnostics.length}</p><div id="diagnostics"></div></section><section id="coverage"><h2>Coverage and unsupported areas</h2><ul></ul></section><section id="metadata"><h2>Run metadata</h2><dl><dt>Source revision</dt><dd>${escapeHtml(snapshot.sourceRevision)} (${escapeHtml(snapshot.sourceRevisionKind)})</dd><dt>Configuration hash</dt><dd>${escapeHtml(snapshot.configurationHash)}</dd><dt>Pipeline</dt><dd>${escapeHtml(snapshot.pipelineVersion)}</dd></dl></section></main><script>${script}</script></body></html>`;
6448
+ };
6449
+ var render = (input, options) => {
6450
+ const { snapshot, report } = input;
6451
+ const includeSnippets = options.includeSnippets === true;
6452
+ const entityAnchors = new Map(snapshot.entities.map((entity, index) => [entity.id, entity.id.length > 64 ? `entity-n${index}` : anchor("entity", entity.id)]));
6453
+ if (snapshot.entities.length > 500 || report.diagnostics.length > 1e3) return renderCompact(snapshot, report, entityAnchors, includeSnippets);
6454
+ const entityNames = new Map(snapshot.entities.map((entity) => [entity.id, entity.name]));
6455
+ const entityAnchor = (id) => entityAnchors.get(id) ?? anchor("entity", id);
6456
+ const entityLabel = (id) => entityNames.get(id) ?? id;
6457
+ const entityLink = (id) => `<a href="#${entityAnchor(id)}">${escapeHtml(entityLabel(id))}</a>`;
6458
+ const relations = snapshot.relations.map((relation) => `<tr id="${anchor("relation", relation.id)}" data-relation="${escapeHtml(relation.id)}" data-from="${entityAnchor(relation.from)}" data-to="${entityAnchor(relation.to)}" data-kind="${escapeHtml(relation.kind)}" data-provenance="${escapeHtml(relation.provenance)}" data-analyzer="snapshot" data-search="${escapeHtml(`${relation.id} ${relation.from} ${relation.to} ${relation.kind} ${relation.provenance}`)}"><td>${escapeHtml(relation.kind)}</td><td>${entityLink(relation.from)}</td><td>${entityLink(relation.to)}</td><td>${escapeHtml(relation.provenance)}</td></tr>`).join("");
6459
+ const diagnostics = report.diagnostics.map((diagnostic2) => `<article id="${anchor("diagnostic", diagnostic2.id)}" class="diagnostic" data-status="${escapeHtml(diagnostic2.status)}" data-severity="${escapeHtml(diagnostic2.severity)}" data-analyzer="report" data-entity="${escapeHtml(diagnostic2.entityIds?.join(" ") ?? "")}" data-relation="${escapeHtml(diagnostic2.relationIds?.join(" ") ?? "")}" data-search="${escapeHtml(`${diagnostic2.id} ${diagnostic2.code} ${diagnostic2.message} ${diagnostic2.entityIds?.join(" ") ?? ""} ${diagnostic2.relationIds?.join(" ") ?? ""}`)}"><h3><a href="#${anchor("diagnostic", diagnostic2.id)}">${escapeHtml(diagnostic2.code)}</a> <span class="badge ${escapeHtml(diagnostic2.severity)}">${escapeHtml(diagnostic2.severity)}</span></h3><p>${escapeHtml(diagnostic2.message)}</p><p>Status: <b>${escapeHtml(diagnostic2.status)}</b></p><ul>${diagnostic2.evidence.map((item) => `<li>${escapeHtml(evidenceText(item, includeSnippets))}</li>`).join("")}</ul>${diagnostic2.entityIds?.length ? `<p>Entities: ${diagnostic2.entityIds.map((id) => entityLink(id)).join(", ")}</p>` : ""}</article>`).join("");
6460
+ const coverage = snapshot.coverage.map((entry) => `<li data-status="${escapeHtml(entry.status)}" data-analyzer="${escapeHtml(entry.analyzer)}" data-search="${escapeHtml(`${entry.analyzer} ${entry.scope} ${entry.status} ${entry.reason ?? ""}`)}"><b>${escapeHtml(entry.analyzer)}</b> / ${escapeHtml(entry.scope)}: ${escapeHtml(entry.status)}${entry.reason ? ` \u2014 ${escapeHtml(entry.reason)}` : ""}</li>`).join("");
6461
+ const nodes = snapshot.entities.map((entity) => `<div class="node" id="${entityAnchor(entity.id)}" data-node="${entityAnchor(entity.id)}"><a href="#${entityAnchor(entity.id)}">${escapeHtml(entity.name)}</a><br><span class="muted">${escapeHtml(entity.kind)}</span></div>`).join("");
6462
+ const script = `const q=document.querySelector('#search'),severity=document.querySelector('#severity'),provenance=document.querySelector('#provenance'),status=document.querySelector('#status'),analyzer=document.querySelector('#analyzer'),entity=document.querySelector('#entity'),relation=document.querySelector('#relation');function apply(){const term=q.value.toLowerCase(),entityTerm=entity.value.toLowerCase(),relationTerm=relation.value.toLowerCase();document.querySelectorAll('[data-search]').forEach((el)=>{const match=(!term||el.dataset.search.toLowerCase().includes(term))&&(!severity.value||el.dataset.severity===severity.value)&&(!provenance.value||el.dataset.provenance===provenance.value)&&(!status.value||el.dataset.status===status.value)&&(!analyzer.value||el.dataset.analyzer?.toLowerCase().includes(analyzer.value.toLowerCase()))&&(!entityTerm||el.dataset.entity?.toLowerCase().includes(entityTerm))&&(!relationTerm||el.dataset.relation?.toLowerCase().includes(relationTerm));el.classList.toggle('hidden',!match)});}q.oninput=apply;severity.onchange=apply;provenance.onchange=apply;status.onchange=apply;analyzer.oninput=apply;entity.oninput=apply;relation.oninput=apply;document.querySelector('#reset').onclick=()=>{q.value='';severity.value='';provenance.value='';status.value='';analyzer.value='';entity.value='';relation.value='';apply()};document.querySelectorAll('[data-node]').forEach((node)=>node.onclick=()=>{const id=node.dataset.node;document.querySelectorAll('[data-from],[data-to]').forEach((edge)=>edge.classList.toggle('hidden',edge.dataset.from!==id&&edge.dataset.to!==id));});`;
6463
+ return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Doc Bridge \u2014 ${escapeHtml(snapshot.project.name)}</title><style>body{font:14px system-ui;margin:0;color:#18202a;background:#f5f7fa}header,main{max-width:1200px;margin:auto;padding:1.25rem}header{background:#18202a;color:white;max-width:none;padding-left:calc((100% - 1200px)/2);padding-right:calc((100% - 1200px)/2)}main{background:white}section{margin:1.5rem 0;border-top:1px solid #d7dde5;padding-top:1rem}table{border-collapse:collapse;width:100%;margin-top:.75rem}td,th{border-bottom:1px solid #e5e9ef;text-align:left;padding:.45rem;vertical-align:top}input,select,button{padding:.45rem;margin:.15rem;border:1px solid #b9c3d0;border-radius:4px;background:white}.badge{border-radius:1rem;padding:.15rem .5rem;background:#dce4ee}.error{background:#ffd9d9}.warn{background:#fff0c2}.info{background:#dcecff}.diagnostic{border:1px solid #d7dde5;border-left:4px solid #9aa7b5;padding:.75rem;margin:.75rem 0}.diagnostic:target,tr:target{background:#fff8cf}.muted{color:#5d6a78}a{color:#0b5cad}#map{display:flex;gap:1rem;flex-wrap:wrap}.node{border:1px solid #9aa7b5;padding:.5rem;border-radius:4px}.hidden{display:none}</style></head><body><header><h1>Doc Bridge: ${escapeHtml(snapshot.project.name)}</h1><p>Offline architecture and documentation reconciliation report</p><p class="muted">Snapshot ${escapeHtml(snapshot.contentHash)} \xB7 Report ${escapeHtml(report.contentHash)} \xB7 Revision ${escapeHtml(snapshot.sourceRevision)}</p></header><main><section id="controls"><label>Search <input id="search" type="search" placeholder="entity, relation, diagnostic"></label><label>Severity <select id="severity"><option value="">Any</option><option>error</option><option>warn</option><option>info</option></select></label><label>Provenance <select id="provenance"><option value="">Any</option><option>observed</option><option>declared</option><option>proposed</option></select></label><label>Status <select id="status"><option value="">Any</option>${["confirmed", "undocumented", "stale-or-unverified", "conflict", "unresolved", "not-analyzed"].map((value) => `<option>${value}</option>`).join("")}</select></label><label>Analyzer <input id="analyzer" type="search" placeholder="js-ts, report"></label><label>Entity <input id="entity" type="search" placeholder="entity id"></label><label>Relation <input id="relation" type="search" placeholder="relation id"></label><button id="reset">Reset filters</button></section><section id="architecture"><h2>Architecture map</h2><p>Observed and declared relations are rendered from the canonical snapshot; browser code does not infer edges.</p><div id="map">${nodes}</div><h3>Relations</h3><table><thead><tr><th>Kind</th><th>From</th><th>To</th><th>Provenance</th></tr></thead><tbody id="relations">${relations}</tbody></table></section><section id="diagnostic-lens"><h2>Diagnostic lens</h2><p>Findings: ${report.diagnostics.length}</p><div id="diagnostics">${diagnostics || '<p class="muted">No diagnostics.</p>'}</div></section><section id="coverage"><h2>Coverage and unsupported areas</h2><ul>${coverage || "<li>No coverage metadata.</li>"}</ul></section><section id="metadata"><h2>Run metadata</h2><dl><dt>Source revision</dt><dd>${escapeHtml(snapshot.sourceRevision)} (${escapeHtml(snapshot.sourceRevisionKind)})</dd><dt>Configuration hash</dt><dd>${escapeHtml(snapshot.configurationHash)}</dd><dt>Pipeline</dt><dd>${escapeHtml(snapshot.pipelineVersion)}</dd></dl></section></main><script>${script}</script></body></html>`;
6464
+ };
6465
+ var renderOfflineReport = (input, options = {}) => {
6466
+ try {
6467
+ if (!input || typeof input !== "object") throw new Error("Input must contain snapshot and report artifacts.");
6468
+ const value = input;
6469
+ const snapshot = DiscoverySnapshotV1Schema.parse(value.snapshot);
6470
+ const report = ReconciliationReportV1Schema.parse(value.report);
6471
+ if (report.snapshotHash !== snapshot.contentHash) throw new Error("Report snapshotHash does not match snapshot contentHash.");
6472
+ return render({ snapshot, report }, options);
6473
+ } catch (error) {
6474
+ return errorPage(error instanceof Error ? error.message : String(error));
6475
+ }
6476
+ };
6477
+
4587
6478
  // src/cli/program.ts
4588
6479
  var usage = `ak-docs \u2014 human\u2194agent documentation bridge (@agentskit/doc-bridge)
4589
6480
 
@@ -4592,11 +6483,17 @@ Core (no API key):
4592
6483
  ak-docs demo [--fixture example|monorepo] [--text] [--in-project]
4593
6484
  ak-docs doctor [--text] [--badge] [--write-badge]
4594
6485
  ak-docs index [--watch]
6486
+ ak-docs discover [--text|--json]
6487
+ ak-docs scan | reconcile | check | map [--text|--json] [--html]
6488
+ ak-docs fix propose links|normalize <artifact> [--output <file>]
6489
+ ak-docs fix approve|apply <proposal.json> [--by <name>]
6490
+ ak-docs suggest [--json|--text] run the configured local Registry agent
4595
6491
  ak-docs query [package|ownership|intent|change] <id> [--agent] [--text]
4596
6492
  ak-docs search <term> [--agent] [--text]
4597
6493
  ak-docs list <packages|intents|changes|knowledge> [--text]
4598
6494
  ak-docs ask [question] local consult (no LLM)
4599
6495
  ak-docs gate run [gate-id]
6496
+ ak-docs rules run <report.json> [--preset default|recommended|strict] [--severity rule=level] [--ignore rule]
4600
6497
  ak-docs conformance run documentation-standard-v1 [--text|--json]
4601
6498
  ak-docs mcp
4602
6499
  ak-docs mcp install --cursor | --claude
@@ -4656,8 +6553,16 @@ var parseArgs = (argv) => {
4656
6553
  else if (positional[0] === "memory") command = "memory";
4657
6554
  else if (positional[0] === "playbook") command = "playbook";
4658
6555
  else if (positional[0] === "registry") command = "registry";
6556
+ else if (positional[0] === "discover") command = "discover";
6557
+ else if (positional[0] === "scan") command = "scan";
6558
+ else if (positional[0] === "reconcile") command = "reconcile";
6559
+ else if (positional[0] === "check") command = "check";
6560
+ else if (positional[0] === "map") command = "map";
6561
+ else if (positional[0] === "fix") command = "fix";
6562
+ else if (positional[0] === "suggest") command = "suggest";
4659
6563
  else if (positional[0] === "index") command = "index";
4660
6564
  else if (positional[0] === "gate") command = "gate";
6565
+ else if (positional[0] === "rules") command = "rules";
4661
6566
  else if (positional[0] === "mcp") command = "mcp";
4662
6567
  else if (positional[0] === "doctor") command = "doctor";
4663
6568
  else if (positional[0] === "demo") command = "demo";
@@ -4671,6 +6576,28 @@ var parseArgs = (argv) => {
4671
6576
  else if (positional[0] === "conformance") command = "conformance";
4672
6577
  return { command, flags, configPath, positional };
4673
6578
  };
6579
+ var optionValues = (argv, name) => {
6580
+ const values = [];
6581
+ for (let index = 0; index < argv.length; index += 1) {
6582
+ const arg = argv[index];
6583
+ if (arg?.startsWith(`${name}=`)) values.push(arg.slice(name.length + 1));
6584
+ else if (arg === name && argv[index + 1] && !argv[index + 1]?.startsWith("-")) {
6585
+ values.push(argv[index + 1]);
6586
+ index += 1;
6587
+ }
6588
+ }
6589
+ return values;
6590
+ };
6591
+ var parseRuleAssignments = (values, parseValue) => {
6592
+ const result = {};
6593
+ for (const value of values) {
6594
+ const separator = value.indexOf("=");
6595
+ if (separator <= 0 || separator === value.length - 1) throw new Error(`Invalid rule assignment "${value}". Use rule=value.`);
6596
+ const key = value.slice(0, separator);
6597
+ result[key] = parseValue(value.slice(separator + 1));
6598
+ }
6599
+ return result;
6600
+ };
4674
6601
  var writeJson2 = (payload) => {
4675
6602
  process.stdout.write(`${JSON.stringify(payload, null, 2)}
4676
6603
  `);
@@ -4762,12 +6689,12 @@ var readIndexedDoc = (root, config, idOrPath) => {
4762
6689
  const index = loadDocBridgeIndex(root, config);
4763
6690
  const entry = index.knowledge.find((doc) => doc.id === idOrPath || doc.path === idOrPath);
4764
6691
  if (!entry) throw new Error(`Unknown indexed doc "${idOrPath}". Try: search ${idOrPath}`);
4765
- const abs = resolve13(root, entry.path);
4766
- const rootAbs = resolve13(root);
6692
+ const abs = resolve18(root, entry.path);
6693
+ const rootAbs = resolve18(root);
4767
6694
  if (abs !== rootAbs && !abs.startsWith(`${rootAbs}/`)) {
4768
6695
  throw new Error(`Indexed doc escapes project root: ${entry.path}`);
4769
6696
  }
4770
- return readFileSync17(abs, "utf8");
6697
+ return readFileSync21(abs, "utf8");
4771
6698
  };
4772
6699
  var runAskRepl = async (root, config) => {
4773
6700
  const index = loadDocBridgeIndex(root, config);
@@ -4842,10 +6769,197 @@ var diagnosticNextCommands = (config, result) => {
4842
6769
  "ak-docs index"
4843
6770
  ];
4844
6771
  };
6772
+ var workflowOptions = (root, config, sourceRevision2, stage, handlers) => ({
6773
+ root,
6774
+ ...config.workflow?.stateDir ? { stateDir: config.workflow.stateDir } : {},
6775
+ sourceRevision: sourceRevision2,
6776
+ configurationHash: sha256NormalizedV1(config),
6777
+ stage,
6778
+ handlers
6779
+ });
6780
+ var scanWorkflow = (root, config) => {
6781
+ const discovered = discoverRepository({ root, config });
6782
+ runWorkflow(workflowOptions(root, config, discovered.sourceRevision, "collect", { collect: () => discovered }));
6783
+ return runWorkflow(workflowOptions(root, config, discovered.sourceRevision, "normalize", { normalize: ({ input }) => input }));
6784
+ };
6785
+ var documentationInputs = (root, snapshot) => snapshot.entities.filter((entity) => entity.kind === "document" && entity.path).map((entity) => ({ path: entity.path, content: readFileSync21(resolve18(root, entity.path), "utf8") }));
6786
+ var reconcileWorkflow = (root, config) => {
6787
+ const scanned = scanWorkflow(root, config);
6788
+ const snapshot = parseDiscoverySnapshot(loadWorkflowStepOutput(scanned.stateDir, "normalize"));
6789
+ const declared = applyDocumentationDeclarations(snapshot, documentationInputs(root, snapshot)).snapshot;
6790
+ const report = reconcileKnowledge(snapshot, declared);
6791
+ return runWorkflow(workflowOptions(root, config, snapshot.sourceRevision, "reconcile", { reconcile: () => report }));
6792
+ };
6793
+ var checkWorkflow = (root, config) => {
6794
+ const reconciled = reconcileWorkflow(root, config);
6795
+ const report = parseReconciliationReport(loadWorkflowStepOutput(reconciled.stateDir, "reconcile"));
6796
+ const evaluated = runWorkflow(workflowOptions(root, config, report.sourceRevision, "evaluate", { evaluate: () => evaluateRules(report, { ...config.rules ? { config: config.rules } : {} }) }));
6797
+ return runWorkflow(workflowOptions(root, config, report.sourceRevision, "report", { report: ({ input }) => input }));
6798
+ };
6799
+ var workflowOutput = (result) => {
6800
+ const snapshot = (() => {
6801
+ try {
6802
+ return parseDiscoverySnapshot(loadWorkflowStepOutput(result.stateDir, "normalize"));
6803
+ } catch {
6804
+ return void 0;
6805
+ }
6806
+ })();
6807
+ const report = (() => {
6808
+ try {
6809
+ return parseReconciliationReport(loadWorkflowStepOutput(result.stateDir, "reconcile"));
6810
+ } catch {
6811
+ return void 0;
6812
+ }
6813
+ })();
6814
+ const rules = (() => {
6815
+ try {
6816
+ const value = loadWorkflowStepOutput(result.stateDir, "evaluate");
6817
+ return value && typeof value === "object" ? value : void 0;
6818
+ } catch {
6819
+ return void 0;
6820
+ }
6821
+ })();
6822
+ return {
6823
+ ok: result.run.state !== "failed" && result.run.state !== "stale",
6824
+ runId: result.run.runId,
6825
+ state: result.run.state,
6826
+ stateDir: result.stateDir,
6827
+ artifactRefs: result.run.artifactRefs,
6828
+ steps: result.run.steps,
6829
+ reusedStages: result.reusedStages,
6830
+ ...snapshot ? { snapshotHash: snapshot.contentHash, sourceRevision: snapshot.sourceRevision, configurationHash: snapshot.configurationHash, coverage: snapshot.coverage } : {},
6831
+ ...report ? { reportHash: report.contentHash, diagnostics: report.diagnostics } : {},
6832
+ ...rules ? { rules } : {}
6833
+ };
6834
+ };
6835
+ var runWorkflowCommand = (command, flags, configPath, argv) => {
6836
+ try {
6837
+ const { config, root } = loadProject(configPath);
6838
+ const result = command === "scan" ? scanWorkflow(root, config) : command === "reconcile" ? reconcileWorkflow(root, config) : checkWorkflow(root, config);
6839
+ const output = workflowOutput(result);
6840
+ if (command === "map") output.kind = "architecture-map";
6841
+ if (command === "map" && flags.has("--html")) {
6842
+ const snapshot = parseDiscoverySnapshot(loadWorkflowStepOutput(result.stateDir, "normalize"));
6843
+ const report = parseReconciliationReport(loadWorkflowStepOutput(result.stateDir, "reconcile"));
6844
+ const outputPath = optionValues(argv, "--output")[0] ?? ".doc-bridge/report.html";
6845
+ const htmlPath = resolve18(root, outputPath);
6846
+ mkdirSync8(dirname10(htmlPath), { recursive: true });
6847
+ writeFileSync8(htmlPath, renderOfflineReport({ snapshot, report }), "utf8");
6848
+ output.htmlPath = htmlPath;
6849
+ }
6850
+ if (wantsTextOutput(flags, config)) {
6851
+ writeLines([`Run: ${String(output.runId)}`, `State: ${String(output.state)}`, ...output.snapshotHash ? [`Snapshot: ${String(output.snapshotHash)}`] : [], ...output.reportHash ? [`Report: ${String(output.reportHash)}`] : [], `Artifacts: ${String(output.artifactRefs.length)}`]);
6852
+ } else writeJson2(output);
6853
+ const ruleExitCode = output.rules && typeof output.rules === "object" && "exitCode" in output.rules && output.rules.exitCode === 1 ? 1 : 0;
6854
+ return result.run.state === "failed" ? 1 : ruleExitCode;
6855
+ } catch (error) {
6856
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}
6857
+ `);
6858
+ return 2;
6859
+ }
6860
+ };
6861
+ var runRulesCommand = (argv, flags, positional, configPath) => {
6862
+ const action = positional[1];
6863
+ const reportPath = positional[2];
6864
+ if (action !== "run" || !reportPath) {
6865
+ process.stderr.write("Usage: ak-docs rules run <report.json> [--preset default|recommended|strict] [--severity rule=level] [--ignore rule]\n");
6866
+ return 2;
6867
+ }
6868
+ try {
6869
+ const { config } = loadProject(configPath);
6870
+ const report = parseReconciliationReport(JSON.parse(readFileSync21(resolve18(reportPath), "utf8")));
6871
+ const presetValue = optionValues(argv, "--preset")[0];
6872
+ const preset = presetValue === void 0 ? void 0 : ["default", "recommended", "strict"].includes(presetValue) ? presetValue : (() => {
6873
+ throw new Error(`Invalid rules preset "${presetValue}".`);
6874
+ })();
6875
+ const severity = {};
6876
+ for (const [rule, level] of Object.entries(parseRuleAssignments(optionValues(argv, "--severity"), parseRuleSeverity))) {
6877
+ severity[parseRuleId(rule)] = level;
6878
+ }
6879
+ const ignore = optionValues(argv, "--ignore").map(parseRuleId);
6880
+ const result = evaluateRules(report, {
6881
+ ...config.rules ? { config: config.rules } : {},
6882
+ ...preset ? { preset } : {},
6883
+ ...Object.keys(severity).length ? { severity } : {},
6884
+ ...ignore.length ? { ignore } : {},
6885
+ ...optionValues(argv, "--critical-entity").length ? { criticalEntities: optionValues(argv, "--critical-entity") } : {},
6886
+ ...optionValues(argv, "--critical-path").length ? { criticalPaths: optionValues(argv, "--critical-path") } : {}
6887
+ });
6888
+ if (wantsTextOutput(flags, config)) {
6889
+ writeLines([
6890
+ `Rules: ${result.mode}`,
6891
+ `Findings: ${result.findings.length}`,
6892
+ ...result.findings.length ? result.findings.map((finding) => ` [${finding.severity}] ${finding.code}: ${finding.message}`) : [" (none)"],
6893
+ `Exit code: ${result.exitCode}`
6894
+ ]);
6895
+ } else {
6896
+ writeJson2({ ok: result.exitCode === 0, reportHash: report.contentHash, ...result });
6897
+ }
6898
+ return result.exitCode;
6899
+ } catch (error) {
6900
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}
6901
+ `);
6902
+ return 2;
6903
+ }
6904
+ };
6905
+ var runFixCommand = (argv, positional, configPath) => {
6906
+ try {
6907
+ const { config, root } = loadProject(configPath);
6908
+ const action = positional[1];
6909
+ const proposalPath2 = positional[2];
6910
+ const sourceRevision2 = discoverRepository({ root, config }).sourceRevision;
6911
+ const fixOptions = { baseRevision: sourceRevision2, configurationHash: sha256NormalizedV1(config), ...config.project?.name ? { projectName: config.project.name } : {} };
6912
+ if (action === "propose") {
6913
+ const proposal2 = positional[2] === "links" ? createMarkdownLinkFixProposal(root, fixOptions) : positional[2] === "normalize" && positional[3] ? createArtifactNormalizationProposal(root, positional[3], fixOptions) : void 0;
6914
+ if (!proposal2) {
6915
+ writeJson2({ ok: true, proposal: null });
6916
+ return 0;
6917
+ }
6918
+ const outputPath = optionValues(argv, "--output")[0];
6919
+ if (outputPath) {
6920
+ mkdirSync8(dirname10(resolve18(root, outputPath)), { recursive: true });
6921
+ writeFileSync8(resolve18(root, outputPath), `${JSON.stringify(proposal2, null, 2)}
6922
+ `, "utf8");
6923
+ }
6924
+ writeJson2({ ok: true, proposal: proposal2, ...outputPath ? { proposalPath: resolve18(root, outputPath) } : {} });
6925
+ return 0;
6926
+ }
6927
+ if (!proposalPath2 || !["approve", "apply"].includes(action ?? "")) throw new Error("Usage: ak-docs fix propose links|normalize <artifact> [--output <file>] | fix approve|apply <proposal.json> [--by <name>]");
6928
+ const file = resolve18(root, proposalPath2);
6929
+ const proposal = JSON.parse(readFileSync21(file, "utf8"));
6930
+ const result = action === "approve" ? approveFixProposal(proposal, optionValues(argv, "--by")[0] ?? "human") : applyFixProposal(root, proposal, { currentRevision: sourceRevision2 });
6931
+ writeFileSync8(file, `${JSON.stringify(result, null, 2)}
6932
+ `, "utf8");
6933
+ writeJson2({ ok: true, proposal: result, proposalPath: file });
6934
+ return 0;
6935
+ } catch (error) {
6936
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}
6937
+ `);
6938
+ return 2;
6939
+ }
6940
+ };
6941
+ var runSuggestCommand = async (flags, configPath) => {
6942
+ try {
6943
+ const { config, root } = loadProject(configPath);
6944
+ const stateDir = resolve18(root, config.workflow?.stateDir ?? ".doc-bridge/workflow");
6945
+ const snapshot = parseDiscoverySnapshot(loadWorkflowStepOutput(stateDir, "normalize"));
6946
+ const report = parseReconciliationReport(loadWorkflowStepOutput(stateDir, "reconcile"));
6947
+ const adapter = createRegistryAgentAdapter(root, config, await loadRegistryAgentRunner(root, config));
6948
+ const proposal = await adapter.run(snapshot, report);
6949
+ const proposalPath2 = persistRegistryAgentProposal(stateDir, proposal);
6950
+ if (flags.has("--text")) writeLines([`Agent: ${adapter.metadata.id}`, `Proposal: ${proposal.proposalId}`, `Hash: ${proposal.contentHash}`, `Saved: ${proposalPath2}`]);
6951
+ else writeJson2({ ok: true, proposal, proposalPath: proposalPath2 });
6952
+ return 0;
6953
+ } catch (error) {
6954
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}
6955
+ `);
6956
+ return 2;
6957
+ }
6958
+ };
4845
6959
  var writeIfMissing = (path, contents) => {
4846
- if (existsSync17(path)) return false;
4847
- mkdirSync5(dirname8(path), { recursive: true });
4848
- writeFileSync4(path, contents, "utf8");
6960
+ if (existsSync21(path)) return false;
6961
+ mkdirSync8(dirname10(path), { recursive: true });
6962
+ writeFileSync8(path, contents, "utf8");
4849
6963
  return true;
4850
6964
  };
4851
6965
  var demoOwnership = {
@@ -4948,7 +7062,7 @@ var scaffoldWorkspaceDocs = (root, config) => {
4948
7062
  const created = [];
4949
7063
  const skipped = [];
4950
7064
  for (const pkg of discoverPnpmPackages(root, config)) {
4951
- const path = resolve13(root, config.corpus.agent.root, "packages", `${pkg.id}.md`);
7065
+ const path = resolve18(root, config.corpus.agent.root, "packages", `${pkg.id}.md`);
4952
7066
  if (writeIfMissing(path, workspaceDocDraft(pkg.id, pkg.path))) created.push(path);
4953
7067
  else skipped.push(path);
4954
7068
  }
@@ -4958,11 +7072,11 @@ var bootstrapAgentDocs = (root, config) => {
4958
7072
  const created = [];
4959
7073
  const skipped = [];
4960
7074
  for (const doc of scanHumanDocRecords(root, config)) {
4961
- const raw = readFileSync17(doc.path, "utf8");
7075
+ const raw = readFileSync21(doc.path, "utf8");
4962
7076
  const body = raw.replace(/^---\n[\s\S]*?\n---\n?/, "");
4963
7077
  const title = firstHeading(body) ?? doc.id;
4964
7078
  const description = firstParagraph(body);
4965
- const draftPath = resolve13(root, config.corpus.agent.root, "human", `${doc.id}.md`);
7079
+ const draftPath = resolve18(root, config.corpus.agent.root, "human", `${doc.id}.md`);
4966
7080
  const draft = [
4967
7081
  "---",
4968
7082
  "type: knowledge",
@@ -5026,8 +7140,8 @@ var runCli = (argv) => {
5026
7140
  return 1;
5027
7141
  }
5028
7142
  try {
5029
- const abs = resolve13(file);
5030
- const raw = readFileSync17(abs, "utf8");
7143
+ const abs = resolve18(file);
7144
+ const raw = readFileSync21(abs, "utf8");
5031
7145
  const handoff = parseAgentHandoff(JSON.parse(raw));
5032
7146
  writeJson2({ ok: true, handoff });
5033
7147
  return 0;
@@ -5037,13 +7151,42 @@ var runCli = (argv) => {
5037
7151
  return 1;
5038
7152
  }
5039
7153
  }
7154
+ if (command === "discover") {
7155
+ try {
7156
+ let config;
7157
+ let root = process.cwd();
7158
+ try {
7159
+ const loaded = loadProject(configPath);
7160
+ config = loaded.config;
7161
+ root = loaded.root;
7162
+ } catch (error) {
7163
+ if (!(error instanceof ConfigNotFoundError) || configPath) throw error;
7164
+ }
7165
+ const snapshot = discoverRepository({ root, ...config ? { config } : {} });
7166
+ if (wantsTextOutput(flags, config ?? { surfaces: { cli: { defaultFormat: "json" } } })) {
7167
+ writeLines([`Project: ${snapshot.project.name}`, `Entities: ${snapshot.entities.length}`, `Relations: ${snapshot.relations.length}`, `Source revision: ${snapshot.sourceRevision}`, ...config ? [] : ["No configuration found; discovery used safe defaults."]]);
7168
+ } else {
7169
+ writeJson2({ ok: true, snapshot, ...config ? {} : { proposedConfig: { schemaVersion: 1, corpus: { agent: { root: "docs/for-agents" } } } } });
7170
+ }
7171
+ return 0;
7172
+ } catch (error) {
7173
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}
7174
+ `);
7175
+ return 2;
7176
+ }
7177
+ }
7178
+ if (command === "scan" || command === "reconcile" || command === "check" || command === "map") {
7179
+ return runWorkflowCommand(command, flags, configPath, argv);
7180
+ }
7181
+ if (command === "fix") return runFixCommand(argv, positional, configPath);
7182
+ if (command === "suggest") return runSuggestCommand(flags, configPath);
5040
7183
  if (command === "init") {
5041
7184
  const root = process.cwd();
5042
7185
  const withDemo = flags.has("--demo") || !flags.has("--no-demo");
5043
- const configFile = resolve13(root, configPath ?? "doc-bridge.config.json");
5044
- const docsIndex = resolve13(root, "docs/for-agents/INDEX.md");
5045
- const exampleDoc = resolve13(root, "docs/for-agents/packages/example.md");
5046
- const agentsMd = resolve13(root, "AGENTS.md");
7186
+ const configFile = resolve18(root, configPath ?? "doc-bridge.config.json");
7187
+ const docsIndex = resolve18(root, "docs/for-agents/INDEX.md");
7188
+ const exampleDoc = resolve18(root, "docs/for-agents/packages/example.md");
7189
+ const agentsMd = resolve18(root, "AGENTS.md");
5047
7190
  const configWritten = writeIfMissing(configFile, initConfigContents(configFile, withDemo));
5048
7191
  const indexWritten = writeIfMissing(
5049
7192
  docsIndex,
@@ -5051,7 +7194,7 @@ var runCli = (argv) => {
5051
7194
  );
5052
7195
  const exampleWritten = withDemo ? writeIfMissing(exampleDoc, exampleAgentDoc) : false;
5053
7196
  const agentsWritten = writeIfMissing(agentsMd, agentsMdSnippet);
5054
- if (withDemo) writeIfMissing(resolve13(root, "src/.gitkeep"), "");
7197
+ if (withDemo) writeIfMissing(resolve18(root, "src/.gitkeep"), "");
5055
7198
  const scaffold = flags.has("--scaffold-workspaces") ? scaffoldWorkspaceDocs(root, loadProject(configFile).config) : void 0;
5056
7199
  writeJson2({
5057
7200
  ok: true,
@@ -5230,6 +7373,7 @@ var runCli = (argv) => {
5230
7373
  return 1;
5231
7374
  }
5232
7375
  }
7376
+ if (command === "rules") return runRulesCommand(argv, flags, positional, configPath);
5233
7377
  if (command === "conformance") {
5234
7378
  const action = positional[1];
5235
7379
  const profile = positional[2];
@@ -5294,9 +7438,9 @@ var runCli = (argv) => {
5294
7438
  const { config, root } = loadProject(configPath);
5295
7439
  const report = runDoctor(root, config);
5296
7440
  if (flags.has("--write-badge")) {
5297
- const badgePath = resolve13(root, ".doc-bridge", "coverage-badge.json");
5298
- mkdirSync5(dirname8(badgePath), { recursive: true });
5299
- writeFileSync4(badgePath, `${formatDoctorBadgeJson(report.badge)}
7441
+ const badgePath = resolve18(root, ".doc-bridge", "coverage-badge.json");
7442
+ mkdirSync8(dirname10(badgePath), { recursive: true });
7443
+ writeFileSync8(badgePath, `${formatDoctorBadgeJson(report.badge)}
5300
7444
  `, "utf8");
5301
7445
  }
5302
7446
  if (flags.has("--badge")) {