akm-cli 0.9.0-beta.3 → 0.9.0-beta.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/CHANGELOG.md +600 -0
  2. package/dist/assets/prompts/consolidate-system.md +23 -0
  3. package/dist/assets/prompts/contradiction-judge.md +33 -0
  4. package/dist/assets/prompts/distill-knowledge-system.md +22 -0
  5. package/dist/assets/prompts/distill-lesson-system.md +36 -0
  6. package/dist/assets/prompts/extract-session.md +5 -1
  7. package/dist/assets/prompts/graph-extract-system.md +1 -0
  8. package/dist/assets/prompts/memory-infer-system.md +1 -0
  9. package/dist/assets/prompts/memory-infer-user.md +5 -0
  10. package/dist/assets/prompts/metadata-enhance-system.md +1 -0
  11. package/dist/assets/prompts/procedural-system.md +44 -0
  12. package/dist/assets/prompts/recombine-system.md +40 -0
  13. package/dist/assets/prompts/staleness-detect-system.md +6 -0
  14. package/dist/assets/prompts/validate-summary-judge.md +1 -0
  15. package/dist/assets/templates/html/health.html +281 -111
  16. package/dist/cli.js +14 -3
  17. package/dist/commands/agent/contribute-cli.js +16 -3
  18. package/dist/commands/feedback-cli.js +15 -6
  19. package/dist/commands/graph/graph.js +75 -71
  20. package/dist/commands/health/checks.js +48 -0
  21. package/dist/commands/health/html-report.js +422 -80
  22. package/dist/commands/health.js +381 -9
  23. package/dist/commands/improve/calibration.js +161 -0
  24. package/dist/commands/improve/consolidate.js +634 -111
  25. package/dist/commands/improve/dedup.js +482 -0
  26. package/dist/commands/improve/distill.js +145 -69
  27. package/dist/commands/improve/encoding-salience.js +205 -0
  28. package/dist/commands/improve/extract-cli.js +115 -1
  29. package/dist/commands/improve/extract-prompt.js +33 -2
  30. package/dist/commands/improve/extract-watch.js +140 -0
  31. package/dist/commands/improve/extract.js +244 -35
  32. package/dist/commands/improve/feedback-valence.js +54 -0
  33. package/dist/commands/improve/homeostatic.js +467 -0
  34. package/dist/commands/improve/improve-auto-accept.js +113 -6
  35. package/dist/commands/improve/improve-profiles.js +12 -0
  36. package/dist/commands/improve/improve.js +1974 -614
  37. package/dist/commands/improve/memory/memory-contradiction-detect.js +23 -28
  38. package/dist/commands/improve/outcome-loop.js +256 -0
  39. package/dist/commands/improve/proactive-maintenance.js +87 -0
  40. package/dist/commands/improve/procedural.js +409 -0
  41. package/dist/commands/improve/recombine.js +528 -0
  42. package/dist/commands/improve/reflect.js +26 -1
  43. package/dist/commands/improve/related-sessions.js +120 -0
  44. package/dist/commands/improve/salience.js +386 -0
  45. package/dist/commands/improve/triage.js +95 -0
  46. package/dist/commands/lint/agent-linter.js +19 -24
  47. package/dist/commands/lint/base-linter.js +173 -60
  48. package/dist/commands/lint/command-linter.js +19 -24
  49. package/dist/commands/lint/env-key-rules.js +34 -1
  50. package/dist/commands/lint/fact-linter.js +39 -0
  51. package/dist/commands/lint/index.js +31 -13
  52. package/dist/commands/lint/memory-linter.js +1 -1
  53. package/dist/commands/lint/registry.js +7 -2
  54. package/dist/commands/lint/task-linter.js +3 -3
  55. package/dist/commands/lint/workflow-linter.js +26 -1
  56. package/dist/commands/proposal/proposal.js +5 -0
  57. package/dist/commands/proposal/validators/proposals.js +71 -54
  58. package/dist/commands/read/curate.js +344 -80
  59. package/dist/commands/read/search-cli.js +7 -0
  60. package/dist/commands/read/search.js +1 -0
  61. package/dist/commands/read/show.js +67 -2
  62. package/dist/commands/sources/installed-stashes.js +5 -1
  63. package/dist/commands/sources/stash-cli.js +10 -2
  64. package/dist/core/asset/asset-registry.js +2 -0
  65. package/dist/core/asset/asset-spec.js +14 -0
  66. package/dist/core/asset/frontmatter.js +166 -167
  67. package/dist/core/asset/markdown.js +8 -0
  68. package/dist/core/config/config-schema.js +259 -2
  69. package/dist/core/config/config.js +2 -2
  70. package/dist/core/logs-db.js +4 -3
  71. package/dist/core/paths.js +3 -0
  72. package/dist/core/state-db.js +649 -30
  73. package/dist/indexer/db/db.js +364 -38
  74. package/dist/indexer/db/graph-db.js +129 -86
  75. package/dist/indexer/ensure-index.js +152 -17
  76. package/dist/indexer/graph/graph-boost.js +51 -41
  77. package/dist/indexer/graph/graph-extraction.js +203 -3
  78. package/dist/indexer/index-writer-lock.js +99 -0
  79. package/dist/indexer/indexer.js +114 -111
  80. package/dist/indexer/passes/memory-inference.js +10 -3
  81. package/dist/indexer/passes/staleness-detect.js +2 -5
  82. package/dist/indexer/search/db-search.js +15 -4
  83. package/dist/indexer/search/ranking-contributors.js +22 -0
  84. package/dist/indexer/search/ranking.js +4 -0
  85. package/dist/indexer/walk/matchers.js +9 -0
  86. package/dist/integrations/agent/prompts.js +1 -0
  87. package/dist/integrations/harnesses/claude/session-log.js +11 -1
  88. package/dist/integrations/harnesses/opencode/session-log.js +9 -0
  89. package/dist/integrations/session-logs/index.js +16 -0
  90. package/dist/llm/client.js +23 -4
  91. package/dist/llm/embedder.js +27 -3
  92. package/dist/llm/embedders/local.js +66 -2
  93. package/dist/llm/graph-extract.js +2 -1
  94. package/dist/llm/memory-infer.js +4 -8
  95. package/dist/llm/metadata-enhance.js +9 -1
  96. package/dist/output/renderers.js +73 -1
  97. package/dist/output/shapes/curate.js +14 -2
  98. package/dist/output/text/helpers.js +9 -0
  99. package/dist/runtime.js +25 -1
  100. package/dist/scripts/migrate-storage.js +1242 -594
  101. package/dist/scripts/migrations/import-fs-improve-runs-to-db.js +473 -270
  102. package/dist/sources/providers/tar-utils.js +16 -8
  103. package/dist/storage/sqlite-pragmas.js +146 -0
  104. package/dist/workflows/db.js +3 -4
  105. package/dist/workflows/validate-summary.js +2 -7
  106. package/docs/data-and-telemetry.md +1 -0
  107. package/package.json +9 -6
@@ -491,349 +491,6 @@ var init_env = __esm(() => {
491
491
  ASSIGN_RE = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/;
492
492
  });
493
493
 
494
- // src/core/asset/frontmatter.ts
495
- function parseFrontmatter(raw) {
496
- const parsedBlock = parseFrontmatterBlock(raw);
497
- if (!parsedBlock) {
498
- return { data: {}, content: raw, frontmatter: null, bodyStartLine: 1 };
499
- }
500
- const data = {};
501
- let currentKey = null;
502
- let mode = "scalar";
503
- let nested = null;
504
- let currentList = null;
505
- let blockLines = null;
506
- let blockChomping = "clip";
507
- const flushPending = () => {
508
- if (mode === "pending" && currentKey !== null) {
509
- data[currentKey] = "";
510
- }
511
- };
512
- const flushBlock = () => {
513
- if (mode !== "block" || currentKey === null || blockLines === null)
514
- return;
515
- const deindented = blockLines.map((l) => l.startsWith(" ") ? l.slice(2) : l);
516
- if (blockChomping === "keep") {
517
- data[currentKey] = deindented.join(`
518
- `);
519
- } else if (blockChomping === "strip") {
520
- data[currentKey] = deindented.join(`
521
- `).replace(/\n+$/, "");
522
- } else {
523
- data[currentKey] = `${deindented.join(`
524
- `).replace(/\n+$/, "")}
525
- `;
526
- }
527
- };
528
- for (const line of parsedBlock.frontmatter.split(/\r?\n/)) {
529
- if (mode === "block") {
530
- if (line.startsWith(" ") || line === "") {
531
- blockLines.push(line);
532
- continue;
533
- }
534
- flushBlock();
535
- mode = "scalar";
536
- blockLines = null;
537
- }
538
- const seqItem = line.match(/^(?: {2})?- (.*)$/);
539
- if (seqItem && currentKey !== null && (mode === "list" || mode === "pending")) {
540
- if (mode === "pending") {
541
- currentList = [];
542
- data[currentKey] = currentList;
543
- mode = "list";
544
- }
545
- currentList.push(parseYamlScalar(seqItem[1].trim()));
546
- continue;
547
- }
548
- if (mode === "scalar" && currentKey !== null && /^ {2}\S/.test(line)) {
549
- data[currentKey] = `${String(data[currentKey])} ${line.trim()}`;
550
- continue;
551
- }
552
- const indented = line.match(/^ {2}(\w[\w-]*):\s*(.+)$/);
553
- if (indented && currentKey !== null && (mode === "object" || mode === "pending")) {
554
- if (mode === "pending") {
555
- nested = {};
556
- data[currentKey] = nested;
557
- mode = "object";
558
- }
559
- nested[indented[1]] = parseYamlScalar(indented[2].trim());
560
- continue;
561
- }
562
- const top = line.match(/^(\w[\w-]*):\s*(.*)$/);
563
- if (!top) {
564
- continue;
565
- }
566
- flushPending();
567
- currentKey = top[1];
568
- const value = top[2].trim();
569
- if (value === "|" || value === "|-" || value === "|+") {
570
- mode = "block";
571
- blockLines = [];
572
- blockChomping = value === "|-" ? "strip" : value === "|+" ? "keep" : "clip";
573
- nested = null;
574
- currentList = null;
575
- } else if (value === "") {
576
- mode = "pending";
577
- nested = null;
578
- currentList = null;
579
- } else if (value.startsWith("[") && value.endsWith("]")) {
580
- mode = "list";
581
- nested = null;
582
- currentList = null;
583
- currentList = parseFlowArray(value);
584
- data[currentKey] = currentList;
585
- } else {
586
- mode = "scalar";
587
- nested = null;
588
- currentList = null;
589
- data[currentKey] = parseYamlScalar(value);
590
- }
591
- }
592
- if (mode === "block") {
593
- flushBlock();
594
- }
595
- flushPending();
596
- return {
597
- data,
598
- content: parsedBlock.content,
599
- frontmatter: parsedBlock.frontmatter,
600
- bodyStartLine: parsedBlock.bodyStartLine
601
- };
602
- }
603
- function parseFlowArray(value) {
604
- const inner = value.slice(1, -1).trim();
605
- if (!inner)
606
- return [];
607
- return inner.split(",").map((item) => parseYamlScalar(item.trim()));
608
- }
609
- function parseFrontmatterBlock(raw) {
610
- const match = raw.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r\n|\r|\n|$)([\s\S]*)$/);
611
- if (!match)
612
- return null;
613
- const frontmatter = match[1].replace(/\r/g, "");
614
- const content = match[2];
615
- return {
616
- frontmatter,
617
- content,
618
- bodyStartLine: countLines(raw.slice(0, match[0].length - match[2].length)) + 1
619
- };
620
- }
621
- function countLines(text) {
622
- if (text.length === 0)
623
- return 0;
624
- return text.split(/\r?\n/).length - 1;
625
- }
626
- function parseYamlScalar(value) {
627
- if (value === "")
628
- return "";
629
- if (value === "true")
630
- return true;
631
- if (value === "false")
632
- return false;
633
- const asNumber = Number(value);
634
- if (!Number.isNaN(asNumber))
635
- return asNumber;
636
- if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
637
- return value.slice(1, -1);
638
- }
639
- return value;
640
- }
641
-
642
- // src/core/asset/markdown.ts
643
- function parseMarkdownToc(content) {
644
- const lines = content.split(/\r?\n/);
645
- const headings = [];
646
- const parsed = parseFrontmatter(content);
647
- const start = parsed.frontmatter ? parsed.bodyStartLine - 1 : 0;
648
- for (let i = start;i < lines.length; i++) {
649
- const match = lines[i].match(/^(#{1,6})\s+(.+)$/);
650
- if (match) {
651
- headings.push({
652
- level: match[1].length,
653
- text: match[2].replace(/\s+#+\s*$/, "").trim(),
654
- line: i + 1
655
- });
656
- }
657
- }
658
- return { headings, totalLines: lines.length };
659
- }
660
- var init_markdown = () => {};
661
-
662
- // src/core/warn.ts
663
- import fs2 from "fs";
664
- function isVerbose() {
665
- const env = process.env.AKM_VERBOSE?.trim().toLowerCase();
666
- if (env === "1" || env === "true" || env === "yes" || env === "on")
667
- return true;
668
- if (env === "0" || env === "false" || env === "no" || env === "off")
669
- return false;
670
- return verbose;
671
- }
672
- function appendToLogFile(level, args) {
673
- if (!logFilePath)
674
- return;
675
- const ts = new Date().toISOString();
676
- const msg = args.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ");
677
- try {
678
- fs2.appendFileSync(logFilePath, `[${ts}] [${level}] ${msg}
679
- `);
680
- } catch (e) {
681
- process.stderr.write(`[akm:warn] log-file write failed (${logFilePath}): ${e}
682
- `);
683
- process.stderr.write(`[${ts}] [${level}] ${msg}
684
- `);
685
- }
686
- }
687
- function warn(...args) {
688
- appendToLogFile("WARN", args);
689
- if (!quiet) {
690
- console.warn(...args);
691
- }
692
- }
693
- function error(...args) {
694
- appendToLogFile("ERROR", args);
695
- if (!quiet) {
696
- console.error(...args);
697
- }
698
- }
699
- function warnVerbose(...args) {
700
- if (isVerbose()) {
701
- warn(...args);
702
- }
703
- }
704
- var quiet = false, verbose = false, logFilePath;
705
- var init_warn = () => {};
706
-
707
- // src/indexer/walk/file-context.ts
708
- var renderers;
709
- var init_file_context = __esm(() => {
710
- init_common();
711
- renderers = new Map;
712
- });
713
-
714
- // src/indexer/passes/metadata-contributors.ts
715
- function registerMetadataContributor(contributor) {
716
- contributors.push(contributor);
717
- }
718
- var contributors;
719
- var init_metadata_contributors = __esm(() => {
720
- contributors = [];
721
- });
722
-
723
- // src/indexer/passes/metadata.ts
724
- import fs3 from "fs";
725
- function extractDescriptionFromComments(filePath) {
726
- let content;
727
- try {
728
- content = fs3.readFileSync(filePath, "utf8");
729
- } catch {
730
- return null;
731
- }
732
- const lines = content.split(/\r?\n/).slice(0, 50);
733
- const blockStart = lines.findIndex((l) => /^\s*\/\*\*/.test(l));
734
- if (blockStart >= 0) {
735
- const desc = [];
736
- for (let i = blockStart;i < lines.length; i++) {
737
- const line = lines[i];
738
- if (i > blockStart && /\*\//.test(line))
739
- break;
740
- const cleaned = line.replace(/^\s*\/?\*\*?\s?/, "").replace(/\*\/\s*$/, "").trim();
741
- if (cleaned)
742
- desc.push(cleaned);
743
- }
744
- if (desc.length > 0)
745
- return desc.join(" ");
746
- }
747
- let start = 0;
748
- if (lines[0]?.startsWith("#!"))
749
- start = 1;
750
- const hashLines = [];
751
- for (let i = start;i < lines.length; i++) {
752
- const line = lines[i].trim();
753
- if (line.startsWith("#") && !line.startsWith("#!")) {
754
- hashLines.push(line.replace(/^#+\s*/, "").trim());
755
- } else if (line === "") {} else {
756
- break;
757
- }
758
- }
759
- if (hashLines.length > 0)
760
- return hashLines.join(" ");
761
- return null;
762
- }
763
- var KNOWN_QUALITY_VALUES, warnedUnknownQualityValues, WIKI_INFRA_FILES;
764
- var init_metadata = __esm(() => {
765
- init_asset_spec();
766
- init_common();
767
- init_warn();
768
- init_file_context();
769
- init_metadata_contributors();
770
- KNOWN_QUALITY_VALUES = new Set(["generated", "curated", "enriched", "proposed"]);
771
- warnedUnknownQualityValues = new Set;
772
- WIKI_INFRA_FILES = new Set(["schema.md", "index.md", "log.md"]);
773
- });
774
-
775
- // src/core/asset/asset-ref.ts
776
- import path from "path";
777
- function parseAssetRef(ref) {
778
- const trimmed = ref.trim();
779
- if (!trimmed)
780
- throw new UsageError("Empty ref.", "MISSING_REQUIRED_ARGUMENT");
781
- let origin;
782
- let body = trimmed;
783
- const boundary = trimmed.indexOf("//");
784
- if (boundary >= 0) {
785
- origin = trimmed.slice(0, boundary);
786
- body = trimmed.slice(boundary + 2);
787
- if (!origin)
788
- throw new UsageError("Empty origin in ref.", "MISSING_REQUIRED_ARGUMENT");
789
- }
790
- const colon = body.indexOf(":");
791
- if (colon <= 0) {
792
- throw new UsageError(`Invalid ref "${trimmed}". Expected [origin//]type:name, e.g. skill:deploy or knowledge:guide.md`, "MISSING_REQUIRED_ARGUMENT");
793
- }
794
- const rawType = body.slice(0, colon);
795
- const rawName = body.slice(colon + 1);
796
- if (rawType === "vault") {
797
- throw new UsageError("The `vault` asset type was removed in 0.9.0 \u2014 use `env:` (whole .env config) or `secret:` (a single value).", "MISSING_REQUIRED_ARGUMENT");
798
- }
799
- const resolvedType = TYPE_ALIASES[rawType] ?? rawType;
800
- if (!isAssetType(resolvedType)) {
801
- throw new UsageError(`Invalid asset type: "${rawType}".`, "MISSING_REQUIRED_ARGUMENT");
802
- }
803
- validateName(rawName);
804
- const name = normalizeName(rawName);
805
- return { type: resolvedType, name, origin: origin || undefined };
806
- }
807
- function validateName(name) {
808
- if (!name)
809
- throw new UsageError("Empty asset name.", "MISSING_REQUIRED_ARGUMENT");
810
- if (name.includes("\x00"))
811
- throw new UsageError("Null byte in asset name.", "MISSING_REQUIRED_ARGUMENT");
812
- if (/^[A-Za-z]:/.test(name))
813
- throw new UsageError("Windows drive path in asset name.", "MISSING_REQUIRED_ARGUMENT");
814
- const normalized = path.posix.normalize(name.replace(/\\/g, "/"));
815
- if (path.posix.isAbsolute(normalized))
816
- throw new UsageError("Absolute path in asset name.", "MISSING_REQUIRED_ARGUMENT");
817
- if (normalized === ".." || normalized.startsWith("../")) {
818
- throw new UsageError("Path traversal in asset name.", "MISSING_REQUIRED_ARGUMENT");
819
- }
820
- const segments = normalized.split("/");
821
- if (segments.some((seg) => seg === "." || seg === "..")) {
822
- throw new UsageError("Asset name cannot contain relative path segments.", "MISSING_REQUIRED_ARGUMENT");
823
- }
824
- }
825
- function normalizeName(name) {
826
- return path.posix.normalize(name.replace(/\\/g, "/"));
827
- }
828
- var TYPE_ALIASES;
829
- var init_asset_ref = __esm(() => {
830
- init_common();
831
- init_errors();
832
- TYPE_ALIASES = {
833
- environment: "env"
834
- };
835
- });
836
-
837
494
  // node_modules/yaml/dist/nodes/identity.js
838
495
  var require_identity = __commonJS((exports) => {
839
496
  var ALIAS = Symbol.for("yaml.alias");
@@ -906,17 +563,17 @@ var require_visit = __commonJS((exports) => {
906
563
  visit.BREAK = BREAK;
907
564
  visit.SKIP = SKIP;
908
565
  visit.REMOVE = REMOVE;
909
- function visit_(key, node, visitor, path2) {
910
- const ctrl = callVisitor(key, node, visitor, path2);
566
+ function visit_(key, node, visitor, path) {
567
+ const ctrl = callVisitor(key, node, visitor, path);
911
568
  if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
912
- replaceNode(key, path2, ctrl);
913
- return visit_(key, ctrl, visitor, path2);
569
+ replaceNode(key, path, ctrl);
570
+ return visit_(key, ctrl, visitor, path);
914
571
  }
915
572
  if (typeof ctrl !== "symbol") {
916
573
  if (identity.isCollection(node)) {
917
- path2 = Object.freeze(path2.concat(node));
574
+ path = Object.freeze(path.concat(node));
918
575
  for (let i = 0;i < node.items.length; ++i) {
919
- const ci = visit_(i, node.items[i], visitor, path2);
576
+ const ci = visit_(i, node.items[i], visitor, path);
920
577
  if (typeof ci === "number")
921
578
  i = ci - 1;
922
579
  else if (ci === BREAK)
@@ -927,13 +584,13 @@ var require_visit = __commonJS((exports) => {
927
584
  }
928
585
  }
929
586
  } else if (identity.isPair(node)) {
930
- path2 = Object.freeze(path2.concat(node));
931
- const ck = visit_("key", node.key, visitor, path2);
587
+ path = Object.freeze(path.concat(node));
588
+ const ck = visit_("key", node.key, visitor, path);
932
589
  if (ck === BREAK)
933
590
  return BREAK;
934
591
  else if (ck === REMOVE)
935
592
  node.key = null;
936
- const cv = visit_("value", node.value, visitor, path2);
593
+ const cv = visit_("value", node.value, visitor, path);
937
594
  if (cv === BREAK)
938
595
  return BREAK;
939
596
  else if (cv === REMOVE)
@@ -954,17 +611,17 @@ var require_visit = __commonJS((exports) => {
954
611
  visitAsync.BREAK = BREAK;
955
612
  visitAsync.SKIP = SKIP;
956
613
  visitAsync.REMOVE = REMOVE;
957
- async function visitAsync_(key, node, visitor, path2) {
958
- const ctrl = await callVisitor(key, node, visitor, path2);
614
+ async function visitAsync_(key, node, visitor, path) {
615
+ const ctrl = await callVisitor(key, node, visitor, path);
959
616
  if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
960
- replaceNode(key, path2, ctrl);
961
- return visitAsync_(key, ctrl, visitor, path2);
617
+ replaceNode(key, path, ctrl);
618
+ return visitAsync_(key, ctrl, visitor, path);
962
619
  }
963
620
  if (typeof ctrl !== "symbol") {
964
621
  if (identity.isCollection(node)) {
965
- path2 = Object.freeze(path2.concat(node));
622
+ path = Object.freeze(path.concat(node));
966
623
  for (let i = 0;i < node.items.length; ++i) {
967
- const ci = await visitAsync_(i, node.items[i], visitor, path2);
624
+ const ci = await visitAsync_(i, node.items[i], visitor, path);
968
625
  if (typeof ci === "number")
969
626
  i = ci - 1;
970
627
  else if (ci === BREAK)
@@ -975,13 +632,13 @@ var require_visit = __commonJS((exports) => {
975
632
  }
976
633
  }
977
634
  } else if (identity.isPair(node)) {
978
- path2 = Object.freeze(path2.concat(node));
979
- const ck = await visitAsync_("key", node.key, visitor, path2);
635
+ path = Object.freeze(path.concat(node));
636
+ const ck = await visitAsync_("key", node.key, visitor, path);
980
637
  if (ck === BREAK)
981
638
  return BREAK;
982
639
  else if (ck === REMOVE)
983
640
  node.key = null;
984
- const cv = await visitAsync_("value", node.value, visitor, path2);
641
+ const cv = await visitAsync_("value", node.value, visitor, path);
985
642
  if (cv === BREAK)
986
643
  return BREAK;
987
644
  else if (cv === REMOVE)
@@ -1008,23 +665,23 @@ var require_visit = __commonJS((exports) => {
1008
665
  }
1009
666
  return visitor;
1010
667
  }
1011
- function callVisitor(key, node, visitor, path2) {
668
+ function callVisitor(key, node, visitor, path) {
1012
669
  if (typeof visitor === "function")
1013
- return visitor(key, node, path2);
670
+ return visitor(key, node, path);
1014
671
  if (identity.isMap(node))
1015
- return visitor.Map?.(key, node, path2);
672
+ return visitor.Map?.(key, node, path);
1016
673
  if (identity.isSeq(node))
1017
- return visitor.Seq?.(key, node, path2);
674
+ return visitor.Seq?.(key, node, path);
1018
675
  if (identity.isPair(node))
1019
- return visitor.Pair?.(key, node, path2);
676
+ return visitor.Pair?.(key, node, path);
1020
677
  if (identity.isScalar(node))
1021
- return visitor.Scalar?.(key, node, path2);
678
+ return visitor.Scalar?.(key, node, path);
1022
679
  if (identity.isAlias(node))
1023
- return visitor.Alias?.(key, node, path2);
680
+ return visitor.Alias?.(key, node, path);
1024
681
  return;
1025
682
  }
1026
- function replaceNode(key, path2, node) {
1027
- const parent = path2[path2.length - 1];
683
+ function replaceNode(key, path, node) {
684
+ const parent = path[path.length - 1];
1028
685
  if (identity.isCollection(parent)) {
1029
686
  parent.items[key] = node;
1030
687
  } else if (identity.isPair(parent)) {
@@ -1150,8 +807,8 @@ var require_directives = __commonJS((exports) => {
1150
807
  if (prefix) {
1151
808
  try {
1152
809
  return prefix + decodeURIComponent(suffix);
1153
- } catch (error2) {
1154
- onError(String(error2));
810
+ } catch (error) {
811
+ onError(String(error));
1155
812
  return null;
1156
813
  }
1157
814
  }
@@ -1242,9 +899,9 @@ var require_anchors = __commonJS((exports) => {
1242
899
  if (typeof ref === "object" && ref.anchor && (identity.isScalar(ref.node) || identity.isCollection(ref.node))) {
1243
900
  ref.node.anchor = ref.anchor;
1244
901
  } else {
1245
- const error2 = new Error("Failed to resolve repeated object (this should not happen)");
1246
- error2.source = source;
1247
- throw error2;
902
+ const error = new Error("Failed to resolve repeated object (this should not happen)");
903
+ error.source = source;
904
+ throw error;
1248
905
  }
1249
906
  }
1250
907
  },
@@ -1583,10 +1240,10 @@ var require_Collection = __commonJS((exports) => {
1583
1240
  var createNode = require_createNode();
1584
1241
  var identity = require_identity();
1585
1242
  var Node = require_Node();
1586
- function collectionFromPath(schema, path2, value) {
1243
+ function collectionFromPath(schema, path, value) {
1587
1244
  let v = value;
1588
- for (let i = path2.length - 1;i >= 0; --i) {
1589
- const k = path2[i];
1245
+ for (let i = path.length - 1;i >= 0; --i) {
1246
+ const k = path[i];
1590
1247
  if (typeof k === "number" && Number.isInteger(k) && k >= 0) {
1591
1248
  const a = [];
1592
1249
  a[k] = v;
@@ -1605,7 +1262,7 @@ var require_Collection = __commonJS((exports) => {
1605
1262
  sourceObjects: new Map
1606
1263
  });
1607
1264
  }
1608
- var isEmptyPath = (path2) => path2 == null || typeof path2 === "object" && !!path2[Symbol.iterator]().next().done;
1265
+ var isEmptyPath = (path) => path == null || typeof path === "object" && !!path[Symbol.iterator]().next().done;
1609
1266
 
1610
1267
  class Collection extends Node.NodeBase {
1611
1268
  constructor(type, schema) {
@@ -1626,11 +1283,11 @@ var require_Collection = __commonJS((exports) => {
1626
1283
  copy.range = this.range.slice();
1627
1284
  return copy;
1628
1285
  }
1629
- addIn(path2, value) {
1630
- if (isEmptyPath(path2))
1286
+ addIn(path, value) {
1287
+ if (isEmptyPath(path))
1631
1288
  this.add(value);
1632
1289
  else {
1633
- const [key, ...rest] = path2;
1290
+ const [key, ...rest] = path;
1634
1291
  const node = this.get(key, true);
1635
1292
  if (identity.isCollection(node))
1636
1293
  node.addIn(rest, value);
@@ -1640,8 +1297,8 @@ var require_Collection = __commonJS((exports) => {
1640
1297
  throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
1641
1298
  }
1642
1299
  }
1643
- deleteIn(path2) {
1644
- const [key, ...rest] = path2;
1300
+ deleteIn(path) {
1301
+ const [key, ...rest] = path;
1645
1302
  if (rest.length === 0)
1646
1303
  return this.delete(key);
1647
1304
  const node = this.get(key, true);
@@ -1650,8 +1307,8 @@ var require_Collection = __commonJS((exports) => {
1650
1307
  else
1651
1308
  throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
1652
1309
  }
1653
- getIn(path2, keepScalar) {
1654
- const [key, ...rest] = path2;
1310
+ getIn(path, keepScalar) {
1311
+ const [key, ...rest] = path;
1655
1312
  const node = this.get(key, true);
1656
1313
  if (rest.length === 0)
1657
1314
  return !keepScalar && identity.isScalar(node) ? node.value : node;
@@ -1666,15 +1323,15 @@ var require_Collection = __commonJS((exports) => {
1666
1323
  return n == null || allowScalar && identity.isScalar(n) && n.value == null && !n.commentBefore && !n.comment && !n.tag;
1667
1324
  });
1668
1325
  }
1669
- hasIn(path2) {
1670
- const [key, ...rest] = path2;
1326
+ hasIn(path) {
1327
+ const [key, ...rest] = path;
1671
1328
  if (rest.length === 0)
1672
1329
  return this.has(key);
1673
1330
  const node = this.get(key, true);
1674
1331
  return identity.isCollection(node) ? node.hasIn(rest) : false;
1675
1332
  }
1676
- setIn(path2, value) {
1677
- const [key, ...rest] = path2;
1333
+ setIn(path, value) {
1334
+ const [key, ...rest] = path;
1678
1335
  if (rest.length === 0) {
1679
1336
  this.set(key, value);
1680
1337
  } else {
@@ -2409,7 +2066,7 @@ var require_log = __commonJS((exports) => {
2409
2066
  if (logLevel === "debug")
2410
2067
  console.log(...messages);
2411
2068
  }
2412
- function warn2(logLevel, warning) {
2069
+ function warn(logLevel, warning) {
2413
2070
  if (logLevel === "debug" || logLevel === "warn") {
2414
2071
  if (typeof node_process.emitWarning === "function")
2415
2072
  node_process.emitWarning(warning);
@@ -2418,7 +2075,7 @@ var require_log = __commonJS((exports) => {
2418
2075
  }
2419
2076
  }
2420
2077
  exports.debug = debug;
2421
- exports.warn = warn2;
2078
+ exports.warn = warn;
2422
2079
  });
2423
2080
 
2424
2081
  // node_modules/yaml/dist/schema/yaml-1.1/merge.js
@@ -4067,9 +3724,9 @@ var require_Document = __commonJS((exports) => {
4067
3724
  if (assertCollection(this.contents))
4068
3725
  this.contents.add(value);
4069
3726
  }
4070
- addIn(path2, value) {
3727
+ addIn(path, value) {
4071
3728
  if (assertCollection(this.contents))
4072
- this.contents.addIn(path2, value);
3729
+ this.contents.addIn(path, value);
4073
3730
  }
4074
3731
  createAlias(node, name) {
4075
3732
  if (!node.anchor) {
@@ -4118,30 +3775,30 @@ var require_Document = __commonJS((exports) => {
4118
3775
  delete(key) {
4119
3776
  return assertCollection(this.contents) ? this.contents.delete(key) : false;
4120
3777
  }
4121
- deleteIn(path2) {
4122
- if (Collection.isEmptyPath(path2)) {
3778
+ deleteIn(path) {
3779
+ if (Collection.isEmptyPath(path)) {
4123
3780
  if (this.contents == null)
4124
3781
  return false;
4125
3782
  this.contents = null;
4126
3783
  return true;
4127
3784
  }
4128
- return assertCollection(this.contents) ? this.contents.deleteIn(path2) : false;
3785
+ return assertCollection(this.contents) ? this.contents.deleteIn(path) : false;
4129
3786
  }
4130
3787
  get(key, keepScalar) {
4131
3788
  return identity.isCollection(this.contents) ? this.contents.get(key, keepScalar) : undefined;
4132
3789
  }
4133
- getIn(path2, keepScalar) {
4134
- if (Collection.isEmptyPath(path2))
3790
+ getIn(path, keepScalar) {
3791
+ if (Collection.isEmptyPath(path))
4135
3792
  return !keepScalar && identity.isScalar(this.contents) ? this.contents.value : this.contents;
4136
- return identity.isCollection(this.contents) ? this.contents.getIn(path2, keepScalar) : undefined;
3793
+ return identity.isCollection(this.contents) ? this.contents.getIn(path, keepScalar) : undefined;
4137
3794
  }
4138
3795
  has(key) {
4139
3796
  return identity.isCollection(this.contents) ? this.contents.has(key) : false;
4140
3797
  }
4141
- hasIn(path2) {
4142
- if (Collection.isEmptyPath(path2))
3798
+ hasIn(path) {
3799
+ if (Collection.isEmptyPath(path))
4143
3800
  return this.contents !== undefined;
4144
- return identity.isCollection(this.contents) ? this.contents.hasIn(path2) : false;
3801
+ return identity.isCollection(this.contents) ? this.contents.hasIn(path) : false;
4145
3802
  }
4146
3803
  set(key, value) {
4147
3804
  if (this.contents == null) {
@@ -4150,13 +3807,13 @@ var require_Document = __commonJS((exports) => {
4150
3807
  this.contents.set(key, value);
4151
3808
  }
4152
3809
  }
4153
- setIn(path2, value) {
4154
- if (Collection.isEmptyPath(path2)) {
3810
+ setIn(path, value) {
3811
+ if (Collection.isEmptyPath(path)) {
4155
3812
  this.contents = value;
4156
3813
  } else if (this.contents == null) {
4157
- this.contents = Collection.collectionFromPath(this.schema, Array.from(path2), value);
3814
+ this.contents = Collection.collectionFromPath(this.schema, Array.from(path), value);
4158
3815
  } else if (assertCollection(this.contents)) {
4159
- this.contents.setIn(path2, value);
3816
+ this.contents.setIn(path, value);
4160
3817
  }
4161
3818
  }
4162
3819
  setSchema(version, options = {}) {
@@ -4255,12 +3912,12 @@ var require_errors = __commonJS((exports) => {
4255
3912
  super("YAMLWarning", pos, code, message);
4256
3913
  }
4257
3914
  }
4258
- var prettifyError = (src, lc) => (error2) => {
4259
- if (error2.pos[0] === -1)
3915
+ var prettifyError = (src, lc) => (error) => {
3916
+ if (error.pos[0] === -1)
4260
3917
  return;
4261
- error2.linePos = error2.pos.map((pos) => lc.linePos(pos));
4262
- const { line, col } = error2.linePos[0];
4263
- error2.message += ` at line ${line}, column ${col}`;
3918
+ error.linePos = error.pos.map((pos) => lc.linePos(pos));
3919
+ const { line, col } = error.linePos[0];
3920
+ error.message += ` at line ${line}, column ${col}`;
4264
3921
  let ci = col - 1;
4265
3922
  let lineStr = src.substring(lc.lineStarts[line - 1], lc.lineStarts[line]).replace(/[\n\r]+$/, "");
4266
3923
  if (ci >= 60 && lineStr.length > 80) {
@@ -4279,12 +3936,12 @@ var require_errors = __commonJS((exports) => {
4279
3936
  }
4280
3937
  if (/[^ ]/.test(lineStr)) {
4281
3938
  let count = 1;
4282
- const end = error2.linePos[1];
3939
+ const end = error.linePos[1];
4283
3940
  if (end?.line === line && end.col > col) {
4284
3941
  count = Math.max(1, Math.min(end.col - col, 80 - ci));
4285
3942
  }
4286
3943
  const pointer = " ".repeat(ci) + "^".repeat(count);
4287
- error2.message += `:
3944
+ error.message += `:
4288
3945
 
4289
3946
  ${lineStr}
4290
3947
  ${pointer}
@@ -5074,7 +4731,7 @@ var require_resolve_block_scalar = __commonJS((exports) => {
5074
4731
  const mode = source[0];
5075
4732
  let indent = 0;
5076
4733
  let chomp = "";
5077
- let error2 = -1;
4734
+ let error = -1;
5078
4735
  for (let i = 1;i < source.length; ++i) {
5079
4736
  const ch = source[i];
5080
4737
  if (!chomp && (ch === "-" || ch === "+"))
@@ -5083,12 +4740,12 @@ var require_resolve_block_scalar = __commonJS((exports) => {
5083
4740
  const n = Number(ch);
5084
4741
  if (!indent && n)
5085
4742
  indent = n;
5086
- else if (error2 === -1)
5087
- error2 = offset + i;
4743
+ else if (error === -1)
4744
+ error = offset + i;
5088
4745
  }
5089
4746
  }
5090
- if (error2 !== -1)
5091
- onError(error2, "UNEXPECTED_TOKEN", `Block scalar header includes extra characters: ${source}`);
4747
+ if (error !== -1)
4748
+ onError(error, "UNEXPECTED_TOKEN", `Block scalar header includes extra characters: ${source}`);
5092
4749
  let hasSpace = false;
5093
4750
  let comment = "";
5094
4751
  let length = source.length;
@@ -5375,8 +5032,8 @@ var require_compose_scalar = __commonJS((exports) => {
5375
5032
  try {
5376
5033
  const res = tag.resolve(value, (msg) => onError(tagToken ?? token, "TAG_RESOLVE_FAILED", msg), ctx.options);
5377
5034
  scalar = identity.isScalar(res) ? res : new Scalar.Scalar(res);
5378
- } catch (error2) {
5379
- const msg = error2 instanceof Error ? error2.message : String(error2);
5035
+ } catch (error) {
5036
+ const msg = error instanceof Error ? error.message : String(error);
5380
5037
  onError(tagToken ?? token, "TAG_RESOLVE_FAILED", msg);
5381
5038
  scalar = new Scalar.Scalar(value);
5382
5039
  }
@@ -5493,8 +5150,8 @@ var require_compose_node = __commonJS((exports) => {
5493
5150
  node = composeCollection.composeCollection(CN, ctx, token, props, onError);
5494
5151
  if (anchor)
5495
5152
  node.anchor = anchor.source.substring(1);
5496
- } catch (error2) {
5497
- const message = error2 instanceof Error ? error2.message : String(error2);
5153
+ } catch (error) {
5154
+ const message = error instanceof Error ? error.message : String(error);
5498
5155
  onError(token, "RESOURCE_EXHAUSTION", message);
5499
5156
  }
5500
5157
  break;
@@ -5743,11 +5400,11 @@ ${cb}` : comment;
5743
5400
  break;
5744
5401
  case "error": {
5745
5402
  const msg = token.source ? `${token.message}: ${JSON.stringify(token.source)}` : token.message;
5746
- const error2 = new errors.YAMLParseError(getErrorPos(token), "UNEXPECTED_TOKEN", msg);
5403
+ const error = new errors.YAMLParseError(getErrorPos(token), "UNEXPECTED_TOKEN", msg);
5747
5404
  if (this.atDirectives || !this.doc)
5748
- this.errors.push(error2);
5405
+ this.errors.push(error);
5749
5406
  else
5750
- this.doc.errors.push(error2);
5407
+ this.doc.errors.push(error);
5751
5408
  break;
5752
5409
  }
5753
5410
  case "doc-end": {
@@ -6051,9 +5708,9 @@ var require_cst_visit = __commonJS((exports) => {
6051
5708
  visit.BREAK = BREAK;
6052
5709
  visit.SKIP = SKIP;
6053
5710
  visit.REMOVE = REMOVE;
6054
- visit.itemAtPath = (cst, path2) => {
5711
+ visit.itemAtPath = (cst, path) => {
6055
5712
  let item = cst;
6056
- for (const [field, index] of path2) {
5713
+ for (const [field, index] of path) {
6057
5714
  const tok = item?.[field];
6058
5715
  if (tok && "items" in tok) {
6059
5716
  item = tok.items[index];
@@ -6062,23 +5719,23 @@ var require_cst_visit = __commonJS((exports) => {
6062
5719
  }
6063
5720
  return item;
6064
5721
  };
6065
- visit.parentCollection = (cst, path2) => {
6066
- const parent = visit.itemAtPath(cst, path2.slice(0, -1));
6067
- const field = path2[path2.length - 1][0];
5722
+ visit.parentCollection = (cst, path) => {
5723
+ const parent = visit.itemAtPath(cst, path.slice(0, -1));
5724
+ const field = path[path.length - 1][0];
6068
5725
  const coll = parent?.[field];
6069
5726
  if (coll && "items" in coll)
6070
5727
  return coll;
6071
5728
  throw new Error("Parent collection not found");
6072
5729
  };
6073
- function _visit(path2, item, visitor) {
6074
- let ctrl = visitor(item, path2);
5730
+ function _visit(path, item, visitor) {
5731
+ let ctrl = visitor(item, path);
6075
5732
  if (typeof ctrl === "symbol")
6076
5733
  return ctrl;
6077
5734
  for (const field of ["key", "value"]) {
6078
5735
  const token = item[field];
6079
5736
  if (token && "items" in token) {
6080
5737
  for (let i = 0;i < token.items.length; ++i) {
6081
- const ci = _visit(Object.freeze(path2.concat([[field, i]])), token.items[i], visitor);
5738
+ const ci = _visit(Object.freeze(path.concat([[field, i]])), token.items[i], visitor);
6082
5739
  if (typeof ci === "number")
6083
5740
  i = ci - 1;
6084
5741
  else if (ci === BREAK)
@@ -6089,10 +5746,10 @@ var require_cst_visit = __commonJS((exports) => {
6089
5746
  }
6090
5747
  }
6091
5748
  if (typeof ctrl === "function" && field === "key")
6092
- ctrl = ctrl(item, path2);
5749
+ ctrl = ctrl(item, path);
6093
5750
  }
6094
5751
  }
6095
- return typeof ctrl === "function" ? ctrl(item, path2) : ctrl;
5752
+ return typeof ctrl === "function" ? ctrl(item, path) : ctrl;
6096
5753
  }
6097
5754
  exports.visit = visit;
6098
5755
  });
@@ -7020,8 +6677,8 @@ var require_parser = __commonJS((exports) => {
7020
6677
  peek(n) {
7021
6678
  return this.stack[this.stack.length - n];
7022
6679
  }
7023
- *pop(error2) {
7024
- const token = error2 ?? this.stack.pop();
6680
+ *pop(error) {
6681
+ const token = error ?? this.stack.pop();
7025
6682
  if (!token) {
7026
6683
  const message = "Tried to pop an empty stack";
7027
6684
  yield { type: "error", offset: this.offset, source: "", message };
@@ -7361,14 +7018,14 @@ var require_parser = __commonJS((exports) => {
7361
7018
  case "scalar":
7362
7019
  case "single-quoted-scalar":
7363
7020
  case "double-quoted-scalar": {
7364
- const fs4 = this.flowScalar(this.type);
7021
+ const fs2 = this.flowScalar(this.type);
7365
7022
  if (atNextItem || it.value) {
7366
- map.items.push({ start, key: fs4, sep: [] });
7023
+ map.items.push({ start, key: fs2, sep: [] });
7367
7024
  this.onKeyLine = true;
7368
7025
  } else if (it.sep) {
7369
- this.stack.push(fs4);
7026
+ this.stack.push(fs2);
7370
7027
  } else {
7371
- Object.assign(it, { key: fs4, sep: [] });
7028
+ Object.assign(it, { key: fs2, sep: [] });
7372
7029
  this.onKeyLine = true;
7373
7030
  }
7374
7031
  return;
@@ -7496,13 +7153,13 @@ var require_parser = __commonJS((exports) => {
7496
7153
  case "scalar":
7497
7154
  case "single-quoted-scalar":
7498
7155
  case "double-quoted-scalar": {
7499
- const fs4 = this.flowScalar(this.type);
7156
+ const fs2 = this.flowScalar(this.type);
7500
7157
  if (!it || it.value)
7501
- fc.items.push({ start: [], key: fs4, sep: [] });
7158
+ fc.items.push({ start: [], key: fs2, sep: [] });
7502
7159
  else if (it.sep)
7503
- this.stack.push(fs4);
7160
+ this.stack.push(fs2);
7504
7161
  else
7505
- Object.assign(it, { key: fs4, sep: [] });
7162
+ Object.assign(it, { key: fs2, sep: [] });
7506
7163
  return;
7507
7164
  }
7508
7165
  case "flow-map-end":
@@ -7804,7 +7461,301 @@ var init_dist = __esm(() => {
7804
7461
  $visitAsync = visit.visitAsync;
7805
7462
  });
7806
7463
 
7807
- // src/workflows/schema.ts
7464
+ // src/core/asset/frontmatter.ts
7465
+ function parseFrontmatter(raw) {
7466
+ const parsedBlock = parseFrontmatterBlock(raw);
7467
+ if (!parsedBlock) {
7468
+ return { data: {}, content: raw, frontmatter: null, bodyStartLine: 1 };
7469
+ }
7470
+ let data = {};
7471
+ if (parsedBlock.frontmatter.trim()) {
7472
+ try {
7473
+ const parsed = $parse(parsedBlock.frontmatter);
7474
+ if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) {
7475
+ data = normalizeYamlValues(parsed);
7476
+ }
7477
+ } catch {
7478
+ data = parseFrontmatterLenient(parsedBlock.frontmatter);
7479
+ }
7480
+ }
7481
+ return {
7482
+ data,
7483
+ content: parsedBlock.content,
7484
+ frontmatter: parsedBlock.frontmatter,
7485
+ bodyStartLine: parsedBlock.bodyStartLine
7486
+ };
7487
+ }
7488
+ function normalizeYamlValues(value) {
7489
+ if (value instanceof Date) {
7490
+ const y = value.getUTCFullYear();
7491
+ const m = String(value.getUTCMonth() + 1).padStart(2, "0");
7492
+ const d = String(value.getUTCDate()).padStart(2, "0");
7493
+ return `${y}-${m}-${d}`;
7494
+ }
7495
+ if (value === null)
7496
+ return "";
7497
+ if (Array.isArray(value))
7498
+ return value.map(normalizeYamlValues);
7499
+ if (typeof value === "object") {
7500
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, normalizeYamlValues(v)]));
7501
+ }
7502
+ return value;
7503
+ }
7504
+ function parseFrontmatterLenient(frontmatter) {
7505
+ const data = {};
7506
+ for (const line of frontmatter.split(/\r?\n/)) {
7507
+ const m = line.match(/^([\w][\w-]*):\s*(.*)$/);
7508
+ if (!m)
7509
+ continue;
7510
+ const key = m[1];
7511
+ const rawValue = (m[2] ?? "").trim();
7512
+ try {
7513
+ const singleEntry = $parse(`k: ${rawValue}`);
7514
+ if (singleEntry !== null && typeof singleEntry === "object" && !Array.isArray(singleEntry)) {
7515
+ const v = singleEntry.k;
7516
+ data[key] = v === null || v === undefined ? "" : v;
7517
+ } else {
7518
+ data[key] = rawValue;
7519
+ }
7520
+ } catch {
7521
+ data[key] = rawValue;
7522
+ }
7523
+ }
7524
+ return data;
7525
+ }
7526
+ function parseFrontmatterBlock(raw) {
7527
+ const match = raw.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r\n|\r|\n|$)([\s\S]*)$/);
7528
+ if (match) {
7529
+ const frontmatter = match[1].replace(/\r/g, "");
7530
+ const content = match[2];
7531
+ return {
7532
+ frontmatter,
7533
+ content,
7534
+ bodyStartLine: countLines(raw.slice(0, match[0].length - match[2].length)) + 1
7535
+ };
7536
+ }
7537
+ const emptyMatch = raw.match(/^---\r?\n---(?:\r\n|\r|\n)([\s\S]*)$/);
7538
+ if (emptyMatch) {
7539
+ return { frontmatter: "", content: emptyMatch[1], bodyStartLine: 3 };
7540
+ }
7541
+ return null;
7542
+ }
7543
+ function countLines(text) {
7544
+ if (text.length === 0)
7545
+ return 0;
7546
+ return text.split(/\r?\n/).length - 1;
7547
+ }
7548
+ var init_frontmatter = __esm(() => {
7549
+ init_dist();
7550
+ });
7551
+
7552
+ // src/core/asset/markdown.ts
7553
+ function parseMarkdownToc(content) {
7554
+ const lines = content.split(/\r?\n/);
7555
+ const headings = [];
7556
+ const parsed = parseFrontmatter(content);
7557
+ const start = parsed.frontmatter ? parsed.bodyStartLine - 1 : 0;
7558
+ let inFence = false;
7559
+ for (let i = start;i < lines.length; i++) {
7560
+ if (/^\s*(`{3,}|~{3,})/.test(lines[i])) {
7561
+ inFence = !inFence;
7562
+ continue;
7563
+ }
7564
+ if (inFence)
7565
+ continue;
7566
+ const match = lines[i].match(/^(#{1,6})\s+(.+)$/);
7567
+ if (match) {
7568
+ headings.push({
7569
+ level: match[1].length,
7570
+ text: match[2].replace(/\s+#+\s*$/, "").trim(),
7571
+ line: i + 1
7572
+ });
7573
+ }
7574
+ }
7575
+ return { headings, totalLines: lines.length };
7576
+ }
7577
+ var init_markdown = __esm(() => {
7578
+ init_frontmatter();
7579
+ });
7580
+
7581
+ // src/core/warn.ts
7582
+ import fs2 from "fs";
7583
+ function isVerbose() {
7584
+ const env = process.env.AKM_VERBOSE?.trim().toLowerCase();
7585
+ if (env === "1" || env === "true" || env === "yes" || env === "on")
7586
+ return true;
7587
+ if (env === "0" || env === "false" || env === "no" || env === "off")
7588
+ return false;
7589
+ return verbose;
7590
+ }
7591
+ function appendToLogFile(level, args) {
7592
+ if (!logFilePath)
7593
+ return;
7594
+ const ts = new Date().toISOString();
7595
+ const msg = args.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ");
7596
+ try {
7597
+ fs2.appendFileSync(logFilePath, `[${ts}] [${level}] ${msg}
7598
+ `);
7599
+ } catch (e) {
7600
+ process.stderr.write(`[akm:warn] log-file write failed (${logFilePath}): ${e}
7601
+ `);
7602
+ process.stderr.write(`[${ts}] [${level}] ${msg}
7603
+ `);
7604
+ }
7605
+ }
7606
+ function warn(...args) {
7607
+ appendToLogFile("WARN", args);
7608
+ if (!quiet) {
7609
+ console.warn(...args);
7610
+ }
7611
+ }
7612
+ function error(...args) {
7613
+ appendToLogFile("ERROR", args);
7614
+ if (!quiet) {
7615
+ console.error(...args);
7616
+ }
7617
+ }
7618
+ function warnVerbose(...args) {
7619
+ if (isVerbose()) {
7620
+ warn(...args);
7621
+ }
7622
+ }
7623
+ var quiet = false, verbose = false, logFilePath;
7624
+ var init_warn = () => {};
7625
+
7626
+ // src/indexer/walk/file-context.ts
7627
+ var renderers;
7628
+ var init_file_context = __esm(() => {
7629
+ init_frontmatter();
7630
+ init_common();
7631
+ renderers = new Map;
7632
+ });
7633
+
7634
+ // src/indexer/passes/metadata-contributors.ts
7635
+ function registerMetadataContributor(contributor) {
7636
+ contributors.push(contributor);
7637
+ }
7638
+ var contributors;
7639
+ var init_metadata_contributors = __esm(() => {
7640
+ contributors = [];
7641
+ });
7642
+
7643
+ // src/indexer/passes/metadata.ts
7644
+ import fs3 from "fs";
7645
+ function extractDescriptionFromComments(filePath) {
7646
+ let content;
7647
+ try {
7648
+ content = fs3.readFileSync(filePath, "utf8");
7649
+ } catch {
7650
+ return null;
7651
+ }
7652
+ const lines = content.split(/\r?\n/).slice(0, 50);
7653
+ const blockStart = lines.findIndex((l) => /^\s*\/\*\*/.test(l));
7654
+ if (blockStart >= 0) {
7655
+ const desc = [];
7656
+ for (let i = blockStart;i < lines.length; i++) {
7657
+ const line = lines[i];
7658
+ if (i > blockStart && /\*\//.test(line))
7659
+ break;
7660
+ const cleaned = line.replace(/^\s*\/?\*\*?\s?/, "").replace(/\*\/\s*$/, "").trim();
7661
+ if (cleaned)
7662
+ desc.push(cleaned);
7663
+ }
7664
+ if (desc.length > 0)
7665
+ return desc.join(" ");
7666
+ }
7667
+ let start = 0;
7668
+ if (lines[0]?.startsWith("#!"))
7669
+ start = 1;
7670
+ const hashLines = [];
7671
+ for (let i = start;i < lines.length; i++) {
7672
+ const line = lines[i].trim();
7673
+ if (line.startsWith("#") && !line.startsWith("#!")) {
7674
+ hashLines.push(line.replace(/^#+\s*/, "").trim());
7675
+ } else if (line === "") {} else {
7676
+ break;
7677
+ }
7678
+ }
7679
+ if (hashLines.length > 0)
7680
+ return hashLines.join(" ");
7681
+ return null;
7682
+ }
7683
+ var KNOWN_QUALITY_VALUES, warnedUnknownQualityValues, WIKI_INFRA_FILES;
7684
+ var init_metadata = __esm(() => {
7685
+ init_asset_spec();
7686
+ init_frontmatter();
7687
+ init_common();
7688
+ init_warn();
7689
+ init_file_context();
7690
+ init_metadata_contributors();
7691
+ KNOWN_QUALITY_VALUES = new Set(["generated", "curated", "enriched", "proposed"]);
7692
+ warnedUnknownQualityValues = new Set;
7693
+ WIKI_INFRA_FILES = new Set(["schema.md", "index.md", "log.md"]);
7694
+ });
7695
+
7696
+ // src/core/asset/asset-ref.ts
7697
+ import path from "path";
7698
+ function parseAssetRef(ref) {
7699
+ const trimmed = ref.trim();
7700
+ if (!trimmed)
7701
+ throw new UsageError("Empty ref.", "MISSING_REQUIRED_ARGUMENT");
7702
+ let origin;
7703
+ let body = trimmed;
7704
+ const boundary = trimmed.indexOf("//");
7705
+ if (boundary >= 0) {
7706
+ origin = trimmed.slice(0, boundary);
7707
+ body = trimmed.slice(boundary + 2);
7708
+ if (!origin)
7709
+ throw new UsageError("Empty origin in ref.", "MISSING_REQUIRED_ARGUMENT");
7710
+ }
7711
+ const colon = body.indexOf(":");
7712
+ if (colon <= 0) {
7713
+ throw new UsageError(`Invalid ref "${trimmed}". Expected [origin//]type:name, e.g. skill:deploy or knowledge:guide.md`, "MISSING_REQUIRED_ARGUMENT");
7714
+ }
7715
+ const rawType = body.slice(0, colon);
7716
+ const rawName = body.slice(colon + 1);
7717
+ if (rawType === "vault") {
7718
+ throw new UsageError("The `vault` asset type was removed in 0.9.0 \u2014 use `env:` (whole .env config) or `secret:` (a single value).", "MISSING_REQUIRED_ARGUMENT");
7719
+ }
7720
+ const resolvedType = TYPE_ALIASES[rawType] ?? rawType;
7721
+ if (!isAssetType(resolvedType)) {
7722
+ throw new UsageError(`Invalid asset type: "${rawType}".`, "MISSING_REQUIRED_ARGUMENT");
7723
+ }
7724
+ validateName(rawName);
7725
+ const name = normalizeName(rawName);
7726
+ return { type: resolvedType, name, origin: origin || undefined };
7727
+ }
7728
+ function validateName(name) {
7729
+ if (!name)
7730
+ throw new UsageError("Empty asset name.", "MISSING_REQUIRED_ARGUMENT");
7731
+ if (name.includes("\x00"))
7732
+ throw new UsageError("Null byte in asset name.", "MISSING_REQUIRED_ARGUMENT");
7733
+ if (/^[A-Za-z]:/.test(name))
7734
+ throw new UsageError("Windows drive path in asset name.", "MISSING_REQUIRED_ARGUMENT");
7735
+ const normalized = path.posix.normalize(name.replace(/\\/g, "/"));
7736
+ if (path.posix.isAbsolute(normalized))
7737
+ throw new UsageError("Absolute path in asset name.", "MISSING_REQUIRED_ARGUMENT");
7738
+ if (normalized === ".." || normalized.startsWith("../")) {
7739
+ throw new UsageError("Path traversal in asset name.", "MISSING_REQUIRED_ARGUMENT");
7740
+ }
7741
+ const segments = normalized.split("/");
7742
+ if (segments.some((seg) => seg === "." || seg === "..")) {
7743
+ throw new UsageError("Asset name cannot contain relative path segments.", "MISSING_REQUIRED_ARGUMENT");
7744
+ }
7745
+ }
7746
+ function normalizeName(name) {
7747
+ return path.posix.normalize(name.replace(/\\/g, "/"));
7748
+ }
7749
+ var TYPE_ALIASES;
7750
+ var init_asset_ref = __esm(() => {
7751
+ init_common();
7752
+ init_errors();
7753
+ TYPE_ALIASES = {
7754
+ environment: "env"
7755
+ };
7756
+ });
7757
+
7758
+ // src/workflows/schema.ts
7808
7759
  var WORKFLOW_SCHEMA_VERSION = 1;
7809
7760
 
7810
7761
  // src/workflows/validator.ts
@@ -8198,6 +8149,7 @@ function sortErrors(errors2) {
8198
8149
  var WORKFLOW_TITLE_PREFIX = "Workflow:", STEP_PREFIX = "Step:", STEP_ID_LINE, BULLET_LINE, SUBSECTION_INSTRUCTIONS = "Instructions", SUBSECTION_COMPLETION_CRITERIA = "Completion Criteria";
8199
8150
  var init_parser = __esm(() => {
8200
8151
  init_dist();
8152
+ init_frontmatter();
8201
8153
  init_markdown();
8202
8154
  init_validator();
8203
8155
  STEP_ID_LINE = /^Step ID:\s+(.+?)\s*$/;
@@ -8288,6 +8240,25 @@ function applyTocMetadata(entry, ctx) {
8288
8240
  entry.toc = toc.headings;
8289
8241
  } catch {}
8290
8242
  }
8243
+ function applyFactMetadata(entry, ctx) {
8244
+ try {
8245
+ const fm = applyFrontmatterDescriptionAndTags(entry, ctx);
8246
+ const tags = new Set([...entry.tags ?? [], "fact"]);
8247
+ const hints = new Set(entry.searchHints ?? []);
8248
+ const category = asNonEmptyString(fm.category);
8249
+ if (category) {
8250
+ tags.add(category);
8251
+ hints.add(`category:${category}`);
8252
+ }
8253
+ if (fm.pinned === true) {
8254
+ tags.add("pinned");
8255
+ hints.add("pinned");
8256
+ }
8257
+ entry.tags = Array.from(tags).filter(Boolean);
8258
+ if (hints.size > 0)
8259
+ entry.searchHints = Array.from(hints).filter(Boolean);
8260
+ } catch {}
8261
+ }
8291
8262
  function applyFrontmatterDescriptionAndTags(entry, ctx) {
8292
8263
  const parsed = parseFrontmatter(ctx.content());
8293
8264
  const fm = parsed.data;
@@ -8387,6 +8358,7 @@ function applyTaskMetadata(entry, ctx) {
8387
8358
  }
8388
8359
  var init_renderers = __esm(() => {
8389
8360
  init_env();
8361
+ init_frontmatter();
8390
8362
  init_markdown();
8391
8363
  init_common();
8392
8364
  init_metadata();
@@ -8433,6 +8405,11 @@ var init_renderers = __esm(() => {
8433
8405
  appliesTo: ({ rendererName }) => rendererName === "session-md",
8434
8406
  contribute: (entry, ctx) => applySessionMetadata(entry, ctx.renderContext)
8435
8407
  });
8408
+ registerMetadataContributor({
8409
+ name: "fact-md-metadata",
8410
+ appliesTo: ({ rendererName }) => rendererName === "fact-md",
8411
+ contribute: (entry, ctx) => applyFactMetadata(entry, ctx.renderContext)
8412
+ });
8436
8413
  });
8437
8414
 
8438
8415
  // src/core/asset/asset-registry.ts
@@ -8567,6 +8544,12 @@ var init_asset_spec = __esm(() => {
8567
8544
  ...markdownSpec,
8568
8545
  rendererName: "session-md",
8569
8546
  actionBuilder: (ref) => `akm show ${ref} -> read the session summary; follow the \`access\` frontmatter to open the raw log at \`log_path\``
8547
+ },
8548
+ fact: {
8549
+ stashDir: "facts",
8550
+ ...markdownSpec,
8551
+ rendererName: "fact-md",
8552
+ actionBuilder: (ref) => `akm show ${ref} -> read the stash fact and apply it as durable context`
8570
8553
  }
8571
8554
  };
8572
8555
  TYPE_DIRS = Object.fromEntries(Object.entries(ASSET_SPECS_INTERNAL).map(([type, spec]) => [type, spec.stashDir]));
@@ -8825,20 +8808,117 @@ function ensureMigrationsTable(db) {
8825
8808
  );
8826
8809
  `);
8827
8810
  }
8828
- function runMigrations(db, migrations, opts) {
8829
- ensureMigrationsTable(db);
8830
- opts?.bootstrap?.(db);
8831
- const appliedRows = db.prepare("SELECT id FROM schema_migrations").all();
8832
- const applied = new Set(appliedRows.map((r) => r.id));
8833
- for (const migration of migrations) {
8834
- if (applied.has(migration.id))
8835
- continue;
8836
- db.transaction(() => {
8837
- db.exec(migration.up);
8838
- db.prepare("INSERT INTO schema_migrations (id) VALUES (?)").run(migration.id);
8839
- })();
8811
+ function runMigrations(db, migrations, opts) {
8812
+ ensureMigrationsTable(db);
8813
+ opts?.bootstrap?.(db);
8814
+ const appliedRows = db.prepare("SELECT id FROM schema_migrations").all();
8815
+ const applied = new Set(appliedRows.map((r) => r.id));
8816
+ for (const migration of migrations) {
8817
+ if (applied.has(migration.id))
8818
+ continue;
8819
+ db.transaction(() => {
8820
+ db.exec(migration.up);
8821
+ db.prepare("INSERT INTO schema_migrations (id) VALUES (?)").run(migration.id);
8822
+ })();
8823
+ }
8824
+ }
8825
+
8826
+ // src/runtime.ts
8827
+ import { createHash } from "crypto";
8828
+ import { createWriteStream, statfsSync } from "fs";
8829
+ import { createRequire as createRequire2 } from "module";
8830
+ function sha256Hex(data) {
8831
+ return createHash("sha256").update(data).digest("hex");
8832
+ }
8833
+ function statfsType(path5) {
8834
+ try {
8835
+ return statfsSync(path5).type;
8836
+ } catch {
8837
+ return;
8838
+ }
8839
+ }
8840
+ function sleepSync(ms) {
8841
+ if (isBun2) {
8842
+ bunGlobal().sleepSync(ms);
8843
+ return;
8844
+ }
8845
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
8846
+ }
8847
+ function bunGlobal() {
8848
+ return globalThis.Bun;
8849
+ }
8850
+ var isBun2, nodeRequire2, mainPath;
8851
+ var init_runtime = __esm(() => {
8852
+ isBun2 = !!process.versions?.bun;
8853
+ nodeRequire2 = createRequire2(import.meta.url);
8854
+ mainPath = isBun2 ? bunGlobal().main : process.argv[1];
8855
+ });
8856
+
8857
+ // src/storage/sqlite-pragmas.ts
8858
+ function resolveJournalMode(raw) {
8859
+ if (raw === undefined)
8860
+ return "WAL";
8861
+ const normalized = raw.trim().toUpperCase();
8862
+ if (normalized === "")
8863
+ return "WAL";
8864
+ if (VALID_MODES.has(normalized)) {
8865
+ return normalized;
8866
+ }
8867
+ warnInvalidJournalModeOnce(raw);
8868
+ return "WAL";
8869
+ }
8870
+ function resolveConfiguredJournalMode() {
8871
+ return resolveJournalMode(process.env.AKM_SQLITE_JOURNAL_MODE);
8872
+ }
8873
+ function warnInvalidJournalModeOnce(raw) {
8874
+ if (warnedInvalid)
8875
+ return;
8876
+ warnedInvalid = true;
8877
+ warn(`[akm] invalid AKM_SQLITE_JOURNAL_MODE=${JSON.stringify(raw)} \u2014 using WAL (valid: WAL, DELETE, TRUNCATE)`);
8878
+ }
8879
+ function isNetworkFilesystem(fsType) {
8880
+ if (fsType === undefined)
8881
+ return false;
8882
+ return NETWORK_FS_MAGICS.has(fsType);
8883
+ }
8884
+ function applyStandardPragmas(db, opts = {}) {
8885
+ let mode = resolveConfiguredJournalMode();
8886
+ if (mode === "WAL" && opts.dataDir) {
8887
+ const probe = opts.fsTypeProbe ?? statfsType;
8888
+ if (isNetworkFilesystem(probe(opts.dataDir))) {
8889
+ mode = "DELETE";
8890
+ warnNetworkFallbackOnce(opts.dataDir);
8891
+ }
8892
+ }
8893
+ db.exec("PRAGMA busy_timeout = 30000");
8894
+ db.exec(`PRAGMA journal_mode = ${mode}`);
8895
+ if (opts.foreignKeys !== false) {
8896
+ db.exec("PRAGMA foreign_keys = ON");
8897
+ }
8898
+ if (mode !== "WAL") {
8899
+ db.exec("PRAGMA synchronous = FULL");
8840
8900
  }
8901
+ return mode;
8841
8902
  }
8903
+ function warnNetworkFallbackOnce(dataDir) {
8904
+ if (warnedNetworkFallback)
8905
+ return;
8906
+ warnedNetworkFallback = true;
8907
+ warn(`[akm] network filesystem detected at ${dataDir} \u2014 WAL unsupported, using DELETE journal mode`);
8908
+ }
8909
+ var VALID_MODES, warnedInvalid = false, warnedNetworkFallback = false, FS_MAGIC_NFS = 26985, FS_MAGIC_SMB = 20859, FS_MAGIC_CIFS = 4283649346, FS_MAGIC_SMB2 = 4266872130, FS_MAGIC_FUSE = 1702057286, NETWORK_FS_MAGICS;
8910
+ var init_sqlite_pragmas = __esm(() => {
8911
+ init_warn();
8912
+ init_runtime();
8913
+ VALID_MODES = new Set(["WAL", "DELETE", "TRUNCATE"]);
8914
+ NETWORK_FS_MAGICS = new Set([
8915
+ FS_MAGIC_NFS,
8916
+ FS_MAGIC_SMB,
8917
+ FS_MAGIC_CIFS,
8918
+ FS_MAGIC_SMB2,
8919
+ FS_MAGIC_FUSE
8920
+ ]);
8921
+ });
8842
8922
 
8843
8923
  // src/core/assert.ts
8844
8924
  function assertNever(x, context) {
@@ -8882,11 +8962,15 @@ var init_improve_types = () => {};
8882
8962
  // src/core/state-db.ts
8883
8963
  var exports_state_db = {};
8884
8964
  __export(exports_state_db, {
8965
+ withImmediateTransaction: () => withImmediateTransaction,
8885
8966
  upsertTaskHistory: () => upsertTaskHistory,
8886
8967
  upsertProposal: () => upsertProposal,
8887
8968
  upsertExtractedSession: () => upsertExtractedSession,
8969
+ upsertConsolidationJudged: () => upsertConsolidationJudged,
8970
+ upsertBodyEmbeddings: () => upsertBodyEmbeddings,
8888
8971
  shouldSkipAlreadyExtractedSession: () => shouldSkipAlreadyExtractedSession,
8889
8972
  runMigrations: () => runMigrations2,
8973
+ recordRecombineInduction: () => recordRecombineInduction,
8890
8974
  recordImproveRun: () => recordImproveRun,
8891
8975
  recordFsProposalsImport: () => recordFsProposalsImport,
8892
8976
  readStateEvents: () => readStateEvents,
@@ -8897,9 +8981,12 @@ __export(exports_state_db, {
8897
8981
  purgeOldEvents: () => purgeOldEvents,
8898
8982
  proposalToRowValues: () => proposalToRowValues,
8899
8983
  proposalRowToProposal: () => proposalRowToProposal,
8984
+ persistPhaseThreshold: () => persistPhaseThreshold,
8900
8985
  openStateDatabase: () => openStateDatabase,
8986
+ markRecombineHypothesisPromoted: () => markRecombineHypothesisPromoted,
8901
8987
  listStateProposals: () => listStateProposals,
8902
8988
  listStateProposalIdsByPrefix: () => listStateProposalIdsByPrefix,
8989
+ listProposalGateDecisions: () => listProposalGateDecisions,
8903
8990
  listExistingTableNames: () => listExistingTableNames,
8904
8991
  insertProposalIfAbsent: () => insertProposalIfAbsent,
8905
8992
  insertEvent: () => insertEvent,
@@ -8909,10 +8996,18 @@ __export(exports_state_db, {
8909
8996
  getTaskHistory: () => getTaskHistory,
8910
8997
  getStateProposal: () => getStateProposal,
8911
8998
  getStateDbPath: () => getStateDbPath,
8999
+ getRecombineHypothesis: () => getRecombineHypothesis,
9000
+ getPhaseThreshold: () => getPhaseThreshold,
8912
9001
  getExtractedSessionsMap: () => getExtractedSessionsMap,
8913
9002
  getExtractedSession: () => getExtractedSession,
9003
+ getConsolidationJudgedMap: () => getConsolidationJudgedMap,
9004
+ getBodyEmbeddings: () => getBodyEmbeddings,
9005
+ findMatchingRecombineHypothesis: () => findMatchingRecombineHypothesis,
8914
9006
  eventRowToEnvelope: () => eventRowToEnvelope,
9007
+ embeddingToBlob: () => embeddingToBlob,
9008
+ decayUnseenRecombineHypotheses: () => decayUnseenRecombineHypotheses,
8915
9009
  computeImproveRunMetrics: () => computeImproveRunMetrics,
9010
+ blobToEmbedding: () => blobToEmbedding,
8916
9011
  REGISTRY_INDEX_CACHE_DDL: () => REGISTRY_INDEX_CACHE_DDL
8917
9012
  });
8918
9013
  import fs5 from "fs";
@@ -8927,9 +9022,7 @@ function openStateDatabase(dbPath) {
8927
9022
  fs5.mkdirSync(dir, { recursive: true });
8928
9023
  }
8929
9024
  const db = openDatabase(resolvedPath);
8930
- db.exec("PRAGMA journal_mode = WAL");
8931
- db.exec("PRAGMA foreign_keys = ON");
8932
- db.exec("PRAGMA busy_timeout = 30000");
9025
+ applyStandardPragmas(db, { dataDir: dir });
8933
9026
  runMigrations2(db);
8934
9027
  return db;
8935
9028
  }
@@ -8979,7 +9072,8 @@ function proposalRowToProposal(row) {
8979
9072
  ...meta.review !== undefined ? { review: meta.review } : {},
8980
9073
  ...typeof meta.confidence === "number" ? { confidence: meta.confidence } : {},
8981
9074
  ...meta.gateDecision !== undefined ? { gateDecision: meta.gateDecision } : {},
8982
- ...typeof meta.backupContent === "string" ? { backupContent: meta.backupContent } : {}
9075
+ ...typeof meta.backupContent === "string" ? { backupContent: meta.backupContent } : {},
9076
+ ...typeof meta.eligibilitySource === "string" ? { eligibilitySource: meta.eligibilitySource } : {}
8983
9077
  };
8984
9078
  }
8985
9079
  function proposalToRowValues(proposal, stashDir) {
@@ -8994,6 +9088,8 @@ function proposalToRowValues(proposal, stashDir) {
8994
9088
  metaObj.gateDecision = proposal.gateDecision;
8995
9089
  if (proposal.backupContent !== undefined)
8996
9090
  metaObj.backupContent = proposal.backupContent;
9091
+ if (proposal.eligibilitySource !== undefined)
9092
+ metaObj.eligibilitySource = proposal.eligibilitySource;
8997
9093
  return {
8998
9094
  id: proposal.id,
8999
9095
  stash_dir: stashDir,
@@ -9090,6 +9186,32 @@ function listStateProposals(db, options = {}) {
9090
9186
  FROM proposals ${where} ORDER BY created_at ASC, rowid ASC`).all(...params);
9091
9187
  return rows.map(proposalRowToProposal);
9092
9188
  }
9189
+ function listProposalGateDecisions(db) {
9190
+ const rows = db.prepare("SELECT metadata_json FROM proposals ORDER BY created_at ASC, rowid ASC").all();
9191
+ const decisions = [];
9192
+ for (const row of rows) {
9193
+ let meta;
9194
+ try {
9195
+ meta = JSON.parse(row.metadata_json);
9196
+ } catch {
9197
+ continue;
9198
+ }
9199
+ const decision = meta.gateDecision;
9200
+ if (decision && typeof decision === "object" && typeof decision.outcome === "string") {
9201
+ decisions.push(decision);
9202
+ }
9203
+ }
9204
+ decisions.sort((a, b) => new Date(a.decidedAt).getTime() - new Date(b.decidedAt).getTime());
9205
+ return decisions;
9206
+ }
9207
+ function getPhaseThreshold(db, phase) {
9208
+ const row = db.prepare("SELECT threshold FROM improve_gate_thresholds WHERE phase = ?").get(phase);
9209
+ return row?.threshold;
9210
+ }
9211
+ function persistPhaseThreshold(db, phase, threshold) {
9212
+ db.prepare(`INSERT OR REPLACE INTO improve_gate_thresholds (phase, threshold, updated_at)
9213
+ VALUES (?, ?, ?)`).run(phase, Math.round(threshold), Date.now());
9214
+ }
9093
9215
  function getStateProposal(db, id, stashDir) {
9094
9216
  const sql = `SELECT id, stash_dir, ref, status, source, created_at, updated_at,
9095
9217
  content, frontmatter_json, metadata_json
@@ -9120,6 +9242,19 @@ function insertProposalIfAbsent(db, proposal, stashDir) {
9120
9242
  const changes = result.changes ?? 0;
9121
9243
  return Number(changes) > 0;
9122
9244
  }
9245
+ function withImmediateTransaction(db, fn) {
9246
+ db.exec("BEGIN IMMEDIATE");
9247
+ try {
9248
+ const result = fn();
9249
+ db.exec("COMMIT");
9250
+ return result;
9251
+ } catch (err) {
9252
+ try {
9253
+ db.exec("ROLLBACK");
9254
+ } catch {}
9255
+ throw err;
9256
+ }
9257
+ }
9123
9258
  function upsertTaskHistory(db, row) {
9124
9259
  db.prepare(`
9125
9260
  INSERT OR IGNORE INTO task_history
@@ -9278,9 +9413,10 @@ function upsertExtractedSession(db, input) {
9278
9413
  db.prepare(`
9279
9414
  INSERT OR REPLACE INTO extract_sessions_seen
9280
9415
  (harness, session_id, processed_at, session_ended_at, outcome,
9281
- candidate_count, proposal_count, rationale, source_run, metadata_json)
9282
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
9283
- `).run(input.harness, input.sessionId, input.processedAt, endedAtIso, input.outcome, input.candidateCount, input.proposalCount, input.rationale ?? null, input.sourceRun ?? null, JSON.stringify(input.metadata ?? {}));
9416
+ candidate_count, proposal_count, rationale, source_run, metadata_json,
9417
+ content_hash)
9418
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
9419
+ `).run(input.harness, input.sessionId, input.processedAt, endedAtIso, input.outcome, input.candidateCount, input.proposalCount, input.rationale ?? null, input.sourceRun ?? null, JSON.stringify(input.metadata ?? {}), input.contentHash);
9284
9420
  }
9285
9421
  function getExtractedSession(db, harness, sessionId) {
9286
9422
  const row = db.prepare("SELECT * FROM extract_sessions_seen WHERE harness = ? AND session_id = ?").get(harness, sessionId);
@@ -9301,16 +9437,139 @@ function getExtractedSessionsMap(db, harness, sessionIds) {
9301
9437
  }
9302
9438
  return out;
9303
9439
  }
9304
- function shouldSkipAlreadyExtractedSession(prior, liveSessionEndedAtMs) {
9440
+ function shouldSkipAlreadyExtractedSession(prior, currentContentHash) {
9305
9441
  if (!prior)
9306
9442
  return false;
9307
- if (typeof liveSessionEndedAtMs !== "number" || !Number.isFinite(liveSessionEndedAtMs)) {
9308
- return true;
9309
- }
9310
- const priorMs = prior.session_ended_at ? Date.parse(prior.session_ended_at) : Number.NaN;
9311
- if (!Number.isFinite(priorMs))
9443
+ if (prior.content_hash == null)
9312
9444
  return false;
9313
- return liveSessionEndedAtMs <= priorMs;
9445
+ return prior.content_hash === currentContentHash;
9446
+ }
9447
+ function getConsolidationJudgedMap(db, entryKeys) {
9448
+ const out = new Map;
9449
+ if (entryKeys.length === 0)
9450
+ return out;
9451
+ const CHUNK = 500;
9452
+ for (let i = 0;i < entryKeys.length; i += CHUNK) {
9453
+ const chunk = entryKeys.slice(i, i + CHUNK);
9454
+ const placeholders = chunk.map(() => "?").join(",");
9455
+ const rows = db.prepare(`SELECT * FROM consolidation_judged WHERE entry_key IN (${placeholders})`).all(...chunk);
9456
+ for (const row of rows)
9457
+ out.set(row.entry_key, row);
9458
+ }
9459
+ return out;
9460
+ }
9461
+ function upsertConsolidationJudged(db, input) {
9462
+ db.prepare(`
9463
+ INSERT OR REPLACE INTO consolidation_judged
9464
+ (entry_key, content_hash, judged_at, outcome)
9465
+ VALUES (?, ?, ?, ?)
9466
+ `).run(input.entryKey, input.contentHash, input.judgedAt, input.outcome);
9467
+ }
9468
+ function recordRecombineInduction(db, input) {
9469
+ const row = db.prepare(`
9470
+ INSERT INTO recombine_hypotheses
9471
+ (hypothesis_ref, signature, member_key, consecutive_count, first_seen_at, last_seen_at, last_run)
9472
+ VALUES (?, ?, ?, 1, ?, ?, ?)
9473
+ ON CONFLICT(hypothesis_ref) DO UPDATE SET
9474
+ consecutive_count = consecutive_count + (CASE WHEN last_run IS excluded.last_run THEN 0 ELSE 1 END),
9475
+ last_seen_at = excluded.last_seen_at,
9476
+ last_run = excluded.last_run,
9477
+ signature = excluded.signature,
9478
+ member_key = excluded.member_key
9479
+ RETURNING consecutive_count
9480
+ `).get(input.hypothesisRef, input.signature, input.memberKey, input.seenAt, input.seenAt, input.run);
9481
+ return row?.consecutive_count ?? 0;
9482
+ }
9483
+ function findMatchingRecombineHypothesis(db, input) {
9484
+ const candidateMembers = new Set(input.memberKey.split("|").filter((m) => m.length > 0));
9485
+ if (candidateMembers.size === 0)
9486
+ return;
9487
+ const rows = db.prepare("SELECT * FROM recombine_hypotheses WHERE signature = ? AND promoted_at IS NULL ORDER BY last_seen_at DESC").all(input.signature);
9488
+ let best;
9489
+ let bestOverlap = -1;
9490
+ for (const row of rows) {
9491
+ const rowMembers = row.member_key.split("|").filter((m) => m.length > 0);
9492
+ if (rowMembers.length === 0)
9493
+ continue;
9494
+ let intersection = 0;
9495
+ for (const m of rowMembers) {
9496
+ if (candidateMembers.has(m))
9497
+ intersection += 1;
9498
+ }
9499
+ const union = candidateMembers.size + rowMembers.length - intersection;
9500
+ const overlap = union === 0 ? 0 : intersection / union;
9501
+ if (overlap >= input.minOverlap && overlap > bestOverlap) {
9502
+ best = row;
9503
+ bestOverlap = overlap;
9504
+ }
9505
+ }
9506
+ return best;
9507
+ }
9508
+ function getRecombineHypothesis(db, hypothesisRef) {
9509
+ const row = db.prepare("SELECT * FROM recombine_hypotheses WHERE hypothesis_ref = ?").get(hypothesisRef);
9510
+ return row ?? undefined;
9511
+ }
9512
+ function markRecombineHypothesisPromoted(db, hypothesisRef, promotedAt) {
9513
+ db.prepare("UPDATE recombine_hypotheses SET promoted_at = ?, consecutive_count = 0 WHERE hypothesis_ref = ?").run(promotedAt, hypothesisRef);
9514
+ }
9515
+ function decayUnseenRecombineHypotheses(db, currentRun, seenRefs) {
9516
+ if (seenRefs.length === 0) {
9517
+ const res2 = db.prepare("UPDATE recombine_hypotheses SET consecutive_count = 0 WHERE promoted_at IS NULL AND (last_run IS NULL OR last_run != ?) AND consecutive_count != 0").run(currentRun);
9518
+ return Number(res2.changes);
9519
+ }
9520
+ if (seenRefs.length > 900) {
9521
+ const res2 = db.prepare("UPDATE recombine_hypotheses SET consecutive_count = 0 WHERE promoted_at IS NULL AND (last_run IS NULL OR last_run != ?) AND consecutive_count != 0").run(currentRun);
9522
+ return Number(res2.changes);
9523
+ }
9524
+ const placeholders = seenRefs.map(() => "?").join(",");
9525
+ const res = db.prepare(`UPDATE recombine_hypotheses SET consecutive_count = 0
9526
+ WHERE promoted_at IS NULL
9527
+ AND (last_run IS NULL OR last_run != ?)
9528
+ AND consecutive_count != 0
9529
+ AND hypothesis_ref NOT IN (${placeholders})`).run(currentRun, ...seenRefs);
9530
+ return Number(res.changes);
9531
+ }
9532
+ function embeddingToBlob(vec) {
9533
+ const f32 = new Float32Array(vec);
9534
+ return new Uint8Array(f32.buffer);
9535
+ }
9536
+ function blobToEmbedding(blob) {
9537
+ const f32 = new Float32Array(blob.buffer, blob.byteOffset, blob.byteLength / 4);
9538
+ return Array.from(f32);
9539
+ }
9540
+ function getBodyEmbeddings(db, contentHashes, expectedModelId) {
9541
+ const out = new Map;
9542
+ if (contentHashes.length === 0)
9543
+ return out;
9544
+ const firstRow = db.prepare("SELECT model_id FROM body_embeddings LIMIT 1").get();
9545
+ if (firstRow && firstRow.model_id !== expectedModelId) {
9546
+ db.exec("DELETE FROM body_embeddings");
9547
+ return out;
9548
+ }
9549
+ const CHUNK = 500;
9550
+ for (let i = 0;i < contentHashes.length; i += CHUNK) {
9551
+ const chunk = contentHashes.slice(i, i + CHUNK);
9552
+ const placeholders = chunk.map(() => "?").join(",");
9553
+ const rows = db.prepare(`SELECT content_hash, embedding FROM body_embeddings WHERE content_hash IN (${placeholders})`).all(...chunk);
9554
+ for (const row of rows) {
9555
+ out.set(row.content_hash, blobToEmbedding(row.embedding));
9556
+ }
9557
+ }
9558
+ return out;
9559
+ }
9560
+ function upsertBodyEmbeddings(db, entries) {
9561
+ if (entries.length === 0)
9562
+ return;
9563
+ const now = Date.now();
9564
+ const stmt = db.prepare(`
9565
+ INSERT OR REPLACE INTO body_embeddings (content_hash, embedding, model_id, created_at)
9566
+ VALUES (?, ?, ?, ?)
9567
+ `);
9568
+ db.transaction(() => {
9569
+ for (const { contentHash, embedding, modelId } of entries) {
9570
+ stmt.run(contentHash, embeddingToBlob(embedding), modelId, now);
9571
+ }
9572
+ })();
9314
9573
  }
9315
9574
  var MIGRATIONS, REGISTRY_INDEX_CACHE_DDL = `
9316
9575
  CREATE TABLE IF NOT EXISTS registry_index_cache (
@@ -9326,6 +9585,7 @@ var MIGRATIONS, REGISTRY_INDEX_CACHE_DDL = `
9326
9585
  `;
9327
9586
  var init_state_db = __esm(() => {
9328
9587
  init_database();
9588
+ init_sqlite_pragmas();
9329
9589
  init_improve_types();
9330
9590
  init_paths();
9331
9591
  init_warn();
@@ -9402,7 +9662,7 @@ var init_state_db = __esm(() => {
9402
9662
  -- metadata_json TEXT \u2014 JSON object for future proposal fields.
9403
9663
  -- Current fields stored here: sourceRun,
9404
9664
  -- review, confidence, gateDecision (#577),
9405
- -- backupContent.
9665
+ -- backupContent, eligibilitySource.
9406
9666
  --
9407
9667
  -- ADD COLUMN extension points (future migrations):
9408
9668
  -- ALTER TABLE proposals ADD COLUMN source_run TEXT DEFAULT NULL;
@@ -9599,6 +9859,117 @@ var init_state_db = __esm(() => {
9599
9859
  imported_count INTEGER NOT NULL DEFAULT 0
9600
9860
  );
9601
9861
  `
9862
+ },
9863
+ {
9864
+ id: "006-proposals-pending-ref-source",
9865
+ up: `
9866
+ CREATE INDEX IF NOT EXISTS idx_proposals_stash_status_ref_source
9867
+ ON proposals(stash_dir, status, ref, source);
9868
+ `
9869
+ },
9870
+ {
9871
+ id: "007-consolidation-judged",
9872
+ up: `
9873
+ CREATE TABLE IF NOT EXISTS consolidation_judged (
9874
+ entry_key TEXT PRIMARY KEY,
9875
+ content_hash TEXT NOT NULL,
9876
+ judged_at TEXT NOT NULL,
9877
+ outcome TEXT NOT NULL
9878
+ );
9879
+ `
9880
+ },
9881
+ {
9882
+ id: "008-body-embeddings",
9883
+ up: `
9884
+ CREATE TABLE IF NOT EXISTS body_embeddings (
9885
+ content_hash TEXT PRIMARY KEY,
9886
+ embedding BLOB NOT NULL,
9887
+ model_id TEXT NOT NULL,
9888
+ created_at INTEGER NOT NULL
9889
+ );
9890
+ `
9891
+ },
9892
+ {
9893
+ id: "009-asset-salience",
9894
+ up: `
9895
+ CREATE TABLE IF NOT EXISTS asset_salience (
9896
+ asset_ref TEXT PRIMARY KEY,
9897
+ encoding_salience REAL NOT NULL DEFAULT 0.5,
9898
+ outcome_salience REAL NOT NULL DEFAULT 0.0,
9899
+ retrieval_salience REAL NOT NULL DEFAULT 0.0,
9900
+ rank_score REAL NOT NULL DEFAULT 0.0,
9901
+ consecutive_no_ops INTEGER NOT NULL DEFAULT 0,
9902
+ updated_at INTEGER NOT NULL DEFAULT 0
9903
+ );
9904
+
9905
+ -- Hot path: sort / filter by rank_score for selector queries.
9906
+ CREATE INDEX IF NOT EXISTS idx_asset_salience_rank
9907
+ ON asset_salience(rank_score DESC);
9908
+ `
9909
+ },
9910
+ {
9911
+ id: "010-asset-outcome",
9912
+ up: `
9913
+ CREATE TABLE IF NOT EXISTS asset_outcome (
9914
+ asset_ref TEXT PRIMARY KEY,
9915
+ last_retrieved_at INTEGER NOT NULL DEFAULT 0,
9916
+ retrieval_count INTEGER NOT NULL DEFAULT 0,
9917
+ expected_retrieval_rate REAL NOT NULL DEFAULT 0.0,
9918
+ negative_feedback_count INTEGER NOT NULL DEFAULT 0,
9919
+ accepted_change_count INTEGER NOT NULL DEFAULT 0,
9920
+ review_pressure INTEGER NOT NULL DEFAULT 0,
9921
+ outcome_score REAL NOT NULL DEFAULT 0.0,
9922
+ updated_at INTEGER NOT NULL DEFAULT 0
9923
+ );
9924
+
9925
+ -- Hot path: sort assets by review_pressure DESC for #613 admission.
9926
+ CREATE INDEX IF NOT EXISTS idx_asset_outcome_review_pressure
9927
+ ON asset_outcome(review_pressure DESC);
9928
+
9929
+ -- Secondary: sort by outcome_score DESC for outcomeSalience reads.
9930
+ CREATE INDEX IF NOT EXISTS idx_asset_outcome_score
9931
+ ON asset_outcome(outcome_score DESC);
9932
+ `
9933
+ },
9934
+ {
9935
+ id: "011-asset-salience-homeostatic-demoted-at",
9936
+ up: `
9937
+ ALTER TABLE asset_salience ADD COLUMN homeostatic_demoted_at INTEGER DEFAULT NULL;
9938
+ `
9939
+ },
9940
+ {
9941
+ id: "012-improve-gate-thresholds",
9942
+ up: `
9943
+ CREATE TABLE IF NOT EXISTS improve_gate_thresholds (
9944
+ phase TEXT NOT NULL PRIMARY KEY,
9945
+ threshold INTEGER NOT NULL,
9946
+ updated_at INTEGER NOT NULL
9947
+ );
9948
+ `
9949
+ },
9950
+ {
9951
+ id: "013-extract-sessions-content-hash",
9952
+ up: `
9953
+ ALTER TABLE extract_sessions_seen ADD COLUMN content_hash TEXT DEFAULT NULL;
9954
+ `
9955
+ },
9956
+ {
9957
+ id: "014-recombine-hypotheses",
9958
+ up: `
9959
+ CREATE TABLE IF NOT EXISTS recombine_hypotheses (
9960
+ hypothesis_ref TEXT PRIMARY KEY,
9961
+ signature TEXT NOT NULL,
9962
+ member_key TEXT NOT NULL,
9963
+ consecutive_count INTEGER NOT NULL DEFAULT 0,
9964
+ first_seen_at TEXT NOT NULL,
9965
+ last_seen_at TEXT NOT NULL,
9966
+ last_run TEXT,
9967
+ promoted_at TEXT,
9968
+ metadata_json TEXT NOT NULL DEFAULT '{}'
9969
+ );
9970
+ CREATE INDEX IF NOT EXISTS idx_recombine_hypotheses_last_seen
9971
+ ON recombine_hypotheses(last_seen_at);
9972
+ `
9602
9973
  }
9603
9974
  ];
9604
9975
  });
@@ -9642,29 +10013,6 @@ var init_types = __esm(() => {
9642
10013
  init_warn();
9643
10014
  });
9644
10015
 
9645
- // src/runtime.ts
9646
- import { createHash } from "crypto";
9647
- import { createRequire as createRequire2 } from "module";
9648
- function sha256Hex(data) {
9649
- return createHash("sha256").update(data).digest("hex");
9650
- }
9651
- function sleepSync(ms) {
9652
- if (isBun2) {
9653
- bunGlobal().sleepSync(ms);
9654
- return;
9655
- }
9656
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
9657
- }
9658
- function bunGlobal() {
9659
- return globalThis.Bun;
9660
- }
9661
- var isBun2, nodeRequire2, mainPath;
9662
- var init_runtime = __esm(() => {
9663
- isBun2 = !!process.versions?.bun;
9664
- nodeRequire2 = createRequire2(import.meta.url);
9665
- mainPath = isBun2 ? bunGlobal().main : process.argv[1];
9666
- });
9667
-
9668
10016
  // src/indexer/search/search-fields.ts
9669
10017
  function buildSearchFields(entry) {
9670
10018
  const name = entry.name.replace(/[-_]/g, " ").toLowerCase();
@@ -10348,6 +10696,10 @@ class ClaudeCodeProvider {
10348
10696
  isAvailable() {
10349
10697
  return fs9.existsSync(claudeProjectsDir());
10350
10698
  }
10699
+ watchRoots() {
10700
+ const dir = claudeProjectsDir();
10701
+ return fs9.existsSync(dir) ? [dir] : [];
10702
+ }
10351
10703
  *readEvents(input) {
10352
10704
  try {
10353
10705
  for (const jsonlPath of this.#walkJsonl(claudeProjectsDir())) {
@@ -10512,7 +10864,7 @@ class ClaudeCodeProvider {
10512
10864
  const full = path8.join(dir, entry.name);
10513
10865
  if (entry.isDirectory())
10514
10866
  yield* this.#walkJsonl(full);
10515
- else if (entry.name.endsWith(".jsonl"))
10867
+ else if (entry.name.endsWith(".jsonl") && entry.name !== "journal.jsonl")
10516
10868
  yield full;
10517
10869
  }
10518
10870
  } catch {}
@@ -10582,6 +10934,10 @@ class OpenCodeProvider {
10582
10934
  isAvailable() {
10583
10935
  return fs10.existsSync(this.#baseDir);
10584
10936
  }
10937
+ watchRoots() {
10938
+ const sessionRoot = path9.join(this.#baseDir, "storage", "session");
10939
+ return fs10.existsSync(sessionRoot) ? [sessionRoot] : [];
10940
+ }
10585
10941
  *readEvents(input) {
10586
10942
  const candidates = [this.#baseDir, path9.join(this.#baseDir, "log")];
10587
10943
  for (const dir of candidates) {
@@ -15442,7 +15798,7 @@ var init_config_types = __esm(() => {
15442
15798
  });
15443
15799
 
15444
15800
  // src/core/config/config-schema.ts
15445
- var positiveInt, nonNegativeNumber, nonEmptyString, httpUrl, LlmCapabilitiesSchema, LlmConnectionConfigSchema, LlmProfileConfigSchema, EmbeddingOllamaOptionsSchema, EmbeddingConnectionConfigSchema, AgentPlatformSchema, AgentProfileConfigSchema, ImproveProcessConfigSchema, ImproveProfileProcessesSchema, ImproveProfileConfigSchema, ProfilesSchema, DefaultsSchema, SourceConfigEntryOptionsSchema, SourceConfigEntrySchema, RegistryConfigEntrySchema, KitSourceSchema, InstalledStashEntrySchema, OutputConfigSchema, SearchGraphBoostSchema, SearchConfigSchema, FeedbackConfigSchema, ImproveUtilityDecaySchema, ImproveConfigSchema, GRAPH_EXTRACTION_INCLUDE_TYPES_ALLOWED, INDEX_PASS_PROVIDER_KEYS, INDEX_PASS_KNOWN_KEYS, IndexPassConfigSchema, MetadataEnhanceSchema, StalenessDetectionSchema, IndexConfigSchema, SetupTaskSchedulesSchema, SetupConfigSchema, AkmConfigShape, AkmConfigBaseSchema, AkmConfigSchema;
15801
+ var positiveInt, nonNegativeNumber, nonEmptyString, httpUrl, LlmCapabilitiesSchema, LlmConnectionConfigSchema, LlmProfileConfigSchema, EmbeddingOllamaOptionsSchema, EmbeddingConnectionConfigSchema, AgentPlatformSchema, AgentProfileConfigSchema, ImproveProcessConfigSchema, ImproveProfileProcessesSchema, ImproveProfileConfigSchema, ProfilesSchema, DefaultsSchema, SourceConfigEntryOptionsSchema, SourceConfigEntrySchema, RegistryConfigEntrySchema, KitSourceSchema, InstalledStashEntrySchema, OutputConfigSchema, SearchGraphBoostSchema, SearchConfigSchema, FeedbackConfigSchema, ImproveUtilityDecaySchema, ImproveCalibrationSchema, ImproveExplorationSchema, ImproveSalienceSchema, ImproveConfigSchema, GRAPH_EXTRACTION_INCLUDE_TYPES_ALLOWED, INDEX_PASS_PROVIDER_KEYS, INDEX_PASS_KNOWN_KEYS, IndexPassConfigSchema, MetadataEnhanceSchema, StalenessDetectionSchema, IndexConfigSchema, SetupTaskSchedulesSchema, SetupConfigSchema, AkmConfigShape, AkmConfigBaseSchema, AkmConfigSchema;
15446
15802
  var init_config_schema = __esm(() => {
15447
15803
  init_zod();
15448
15804
  init_config_types();
@@ -15504,15 +15860,66 @@ var init_config_schema = __esm(() => {
15504
15860
  timeoutMs: exports_external.union([positiveInt, exports_external.null()]).optional(),
15505
15861
  allowedTypes: exports_external.array(exports_external.string().min(1)).optional(),
15506
15862
  minPoolSize: exports_external.number().int().min(0).optional(),
15863
+ dedup: exports_external.object({
15864
+ enabled: exports_external.boolean().optional(),
15865
+ cosineThreshold: exports_external.number().min(0).max(1).optional(),
15866
+ cosineCandidateLimit: exports_external.number().int().positive().optional()
15867
+ }).strict().optional(),
15868
+ judgedCache: exports_external.object({ enabled: exports_external.boolean().optional() }).strict().optional(),
15507
15869
  qualityGate: exports_external.object({ enabled: exports_external.boolean().optional() }).strict().optional(),
15508
15870
  contradictionDetection: exports_external.object({ enabled: exports_external.boolean().optional() }).strict().optional(),
15509
15871
  defaultSince: exports_external.string().min(1).optional(),
15510
15872
  maxTotalChars: positiveInt.optional(),
15511
15873
  minContentChars: exports_external.number().int().min(0).optional(),
15512
15874
  maxChunkSize: exports_external.number().int().min(1).max(50).optional(),
15875
+ incrementalSince: exports_external.string().optional(),
15876
+ limit: positiveInt.optional(),
15877
+ neighborsPerChanged: exports_external.number().int().min(1).optional(),
15878
+ requirePlannedRefs: exports_external.boolean().optional(),
15879
+ dueDays: exports_external.number().int().min(0).optional(),
15880
+ maxPerRun: positiveInt.optional(),
15881
+ topN: positiveInt.optional(),
15882
+ minPendingCount: exports_external.number().int().min(0).optional(),
15513
15883
  minNewSessions: exports_external.number().int().min(0).optional(),
15884
+ maxSessionsPerRun: exports_external.number().int().min(0).optional(),
15514
15885
  indexSessions: exports_external.boolean().optional(),
15515
15886
  minSessionDuration: exports_external.number().min(0).optional(),
15887
+ p90ChunkSecondsDefault: exports_external.number().finite().positive().optional(),
15888
+ homeostaticDemotion: exports_external.object({
15889
+ enabled: exports_external.boolean().optional(),
15890
+ staleDays: exports_external.number().int().min(0).optional(),
15891
+ demotionFactor: exports_external.number().min(0).max(1).optional()
15892
+ }).strict().optional(),
15893
+ schemaSimilarity: exports_external.object({
15894
+ enabled: exports_external.boolean().optional(),
15895
+ epsilon: exports_external.number().min(0).max(1).optional(),
15896
+ confidencePenalty: exports_external.number().min(0).max(1).optional()
15897
+ }).strict().optional(),
15898
+ hotProbation: exports_external.object({
15899
+ enabled: exports_external.boolean().optional()
15900
+ }).strict().optional(),
15901
+ antiCollapse: exports_external.object({
15902
+ enabled: exports_external.boolean().optional(),
15903
+ maxGeneration: exports_external.number().int().min(1).optional(),
15904
+ lexicalDiversityCheck: exports_external.boolean().optional(),
15905
+ randomClusterFraction: exports_external.number().min(0).max(1).optional()
15906
+ }).strict().optional(),
15907
+ cls: exports_external.object({
15908
+ enabled: exports_external.boolean().optional(),
15909
+ adjacentCount: exports_external.number().int().min(1).optional()
15910
+ }).strict().optional(),
15911
+ fidelityCheck: exports_external.object({
15912
+ enabled: exports_external.boolean().optional()
15913
+ }).strict().optional(),
15914
+ minClusterSize: exports_external.number().int().min(2).optional(),
15915
+ maxClustersPerRun: positiveInt.optional(),
15916
+ maxClusterSize: positiveInt.optional(),
15917
+ excludeTags: exports_external.array(exports_external.string().min(1)).optional(),
15918
+ relatednessSource: exports_external.enum(["tags", "graph", "both"]).optional(),
15919
+ confirmThreshold: exports_external.number().int().min(1).optional(),
15920
+ minRecurrence: exports_external.number().int().min(2).optional(),
15921
+ maxProposalsPerRun: positiveInt.optional(),
15922
+ emitAs: exports_external.enum(["workflow", "skill"]).optional(),
15516
15923
  applyMode: exports_external.enum(["queue", "promote"]).optional(),
15517
15924
  policy: exports_external.string().min(1).optional(),
15518
15925
  maxAcceptsPerRun: positiveInt.optional(),
@@ -15531,7 +15938,10 @@ var init_config_schema = __esm(() => {
15531
15938
  memoryInference: ImproveProcessConfigSchema.optional(),
15532
15939
  graphExtraction: ImproveProcessConfigSchema.optional(),
15533
15940
  validation: ImproveProcessConfigSchema.optional(),
15534
- triage: ImproveProcessConfigSchema.optional()
15941
+ triage: ImproveProcessConfigSchema.optional(),
15942
+ proactiveMaintenance: ImproveProcessConfigSchema.optional(),
15943
+ recombine: ImproveProcessConfigSchema.optional(),
15944
+ procedural: ImproveProcessConfigSchema.optional()
15535
15945
  }).passthrough().superRefine((val, ctx) => {
15536
15946
  const raw = val;
15537
15947
  if ("feedbackDistillation" in raw) {
@@ -15549,7 +15959,10 @@ var init_config_schema = __esm(() => {
15549
15959
  "graphExtraction",
15550
15960
  "validation",
15551
15961
  "extract",
15552
- "triage"
15962
+ "triage",
15963
+ "proactiveMaintenance",
15964
+ "recombine",
15965
+ "procedural"
15553
15966
  ]);
15554
15967
  for (const k of Object.keys(raw)) {
15555
15968
  if (!allowed.has(k)) {
@@ -15566,6 +15979,8 @@ var init_config_schema = __esm(() => {
15566
15979
  processes: ImproveProfileProcessesSchema.optional(),
15567
15980
  autoAccept: nonNegativeNumber.optional(),
15568
15981
  limit: positiveInt.optional(),
15982
+ maxCycles: positiveInt.optional(),
15983
+ symmetricValence: exports_external.boolean().optional(),
15569
15984
  sync: exports_external.object({
15570
15985
  enabled: exports_external.boolean().optional(),
15571
15986
  push: exports_external.boolean().optional(),
@@ -15646,6 +16061,7 @@ var init_config_schema = __esm(() => {
15646
16061
  }).strict();
15647
16062
  SearchConfigSchema = exports_external.object({
15648
16063
  minScore: nonNegativeNumber.optional(),
16064
+ defaultExcludeTypes: exports_external.array(nonEmptyString).optional(),
15649
16065
  curateRerank: exports_external.object({ enabled: exports_external.boolean().optional() }).strict().optional(),
15650
16066
  graphBoost: SearchGraphBoostSchema.optional()
15651
16067
  }).strict();
@@ -15657,9 +16073,29 @@ var init_config_schema = __esm(() => {
15657
16073
  halfLifeDays: exports_external.number().finite().min(0.1).optional(),
15658
16074
  feedbackStabilityBoost: exports_external.number().finite().min(1).optional()
15659
16075
  }).strict();
16076
+ ImproveCalibrationSchema = exports_external.object({
16077
+ autoTune: exports_external.boolean().optional(),
16078
+ minThreshold: exports_external.number().int().min(0).max(100).optional(),
16079
+ maxThreshold: exports_external.number().int().min(0).max(100).optional(),
16080
+ maxStep: positiveInt.optional(),
16081
+ minSamples: nonNegativeNumber.optional(),
16082
+ targetAcceptRate: exports_external.number().finite().min(0).max(1).optional()
16083
+ }).strict();
16084
+ ImproveExplorationSchema = exports_external.object({
16085
+ enabled: exports_external.boolean().optional(),
16086
+ budgetFraction: exports_external.number().finite().min(0).max(1).optional()
16087
+ }).strict();
16088
+ ImproveSalienceSchema = exports_external.object({
16089
+ outcomeWeightEnabled: exports_external.boolean().optional(),
16090
+ salienceThreshold: exports_external.number().min(0).max(1).optional(),
16091
+ replayBudget: exports_external.number().int().min(0).optional()
16092
+ }).strict();
15660
16093
  ImproveConfigSchema = exports_external.object({
15661
16094
  utilityDecay: ImproveUtilityDecaySchema.optional(),
15662
- eventRetentionDays: nonNegativeNumber.optional()
16095
+ eventRetentionDays: nonNegativeNumber.optional(),
16096
+ calibration: ImproveCalibrationSchema.optional(),
16097
+ exploration: ImproveExplorationSchema.optional(),
16098
+ salience: ImproveSalienceSchema.optional()
15663
16099
  }).strict();
15664
16100
  GRAPH_EXTRACTION_INCLUDE_TYPES_ALLOWED = [
15665
16101
  "memory",
@@ -15670,7 +16106,8 @@ var init_config_schema = __esm(() => {
15670
16106
  "workflow",
15671
16107
  "lesson",
15672
16108
  "task",
15673
- "wiki"
16109
+ "wiki",
16110
+ "fact"
15674
16111
  ];
15675
16112
  INDEX_PASS_PROVIDER_KEYS = new Set([
15676
16113
  "endpoint",
@@ -15686,7 +16123,8 @@ var init_config_schema = __esm(() => {
15686
16123
  "llm",
15687
16124
  "graphExtractionBatchSize",
15688
16125
  "graphExtractionIncludeTypes",
15689
- "memoryInferenceBatchSize"
16126
+ "memoryInferenceBatchSize",
16127
+ "lazyGraphExtraction"
15690
16128
  ]);
15691
16129
  IndexPassConfigSchema = exports_external.preprocess((raw, ctx) => {
15692
16130
  if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
@@ -15704,7 +16142,7 @@ var init_config_schema = __esm(() => {
15704
16142
  if (!INDEX_PASS_KNOWN_KEYS.has(key)) {
15705
16143
  ctx.addIssue({
15706
16144
  code: exports_external.ZodIssueCode.custom,
15707
- message: `Unknown key \`${[...ctx.path ?? [], key].join(".")}\`. Per-pass entries support \`llm\` ` + "(boolean opt-out), `graphExtractionBatchSize`, `graphExtractionIncludeTypes`, and " + "`memoryInferenceBatchSize`."
16145
+ message: `Unknown key \`${[...ctx.path ?? [], key].join(".")}\`. Per-pass entries support \`llm\` ` + "(boolean opt-out), `graphExtractionBatchSize`, `graphExtractionIncludeTypes`, " + "`memoryInferenceBatchSize`, and `lazyGraphExtraction`."
15708
16146
  });
15709
16147
  return raw;
15710
16148
  }
@@ -15721,7 +16159,8 @@ var init_config_schema = __esm(() => {
15721
16159
  llm: exports_external.boolean().optional(),
15722
16160
  graphExtractionBatchSize: positiveInt.optional(),
15723
16161
  graphExtractionIncludeTypes: exports_external.array(exports_external.enum(GRAPH_EXTRACTION_INCLUDE_TYPES_ALLOWED)).nonempty().optional(),
15724
- memoryInferenceBatchSize: positiveInt.optional()
16162
+ memoryInferenceBatchSize: positiveInt.optional(),
16163
+ lazyGraphExtraction: exports_external.boolean().optional()
15725
16164
  }).passthrough());
15726
16165
  MetadataEnhanceSchema = exports_external.object({ enabled: exports_external.boolean().optional() }).strict();
15727
16166
  StalenessDetectionSchema = exports_external.object({
@@ -16042,9 +16481,9 @@ function maybeAutoMigrateConfigFile(configPath, text) {
16042
16481
  "\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501"
16043
16482
  ].join(`
16044
16483
  `);
16045
- process.stderr.write(`${banner}
16484
+ process.stderr?.write?.(`${banner}
16046
16485
  `);
16047
- process.stdout.write(`${banner}
16486
+ process.stdout?.write?.(`${banner}
16048
16487
  `);
16049
16488
  } catch (err) {
16050
16489
  throw new ConfigError(`Failed to write migrated config to ${configPath}: ${err instanceof Error ? err.message : String(err)}`, "INVALID_CONFIG_FILE", "Check filesystem permissions, free space, and disk health. To skip auto-migration, set AKM_NO_AUTO_MIGRATE=1.");
@@ -16288,6 +16727,7 @@ __export(exports_db, {
16288
16727
  getMeta: () => getMeta,
16289
16728
  getLlmCacheEntry: () => getLlmCacheEntry,
16290
16729
  getLlmCacheEntriesByRefs: () => getLlmCacheEntriesByRefs,
16730
+ getIndexedFilePaths: () => getIndexedFilePaths,
16291
16731
  getIndexDirState: () => getIndexDirState,
16292
16732
  getEntryRefRowsForStashRoot: () => getEntryRefRowsForStashRoot,
16293
16733
  getEntryIdByFilePath: () => getEntryIdByFilePath,
@@ -16296,6 +16736,7 @@ __export(exports_db, {
16296
16736
  getEntryByRef: () => getEntryByRef,
16297
16737
  getEntryById: () => getEntryById,
16298
16738
  getEntriesByDir: () => getEntriesByDir,
16739
+ getEntitiesByEntryIds: () => getEntitiesByEntryIds,
16299
16740
  getEmbeddingCount: () => getEmbeddingCount,
16300
16741
  getEmbeddableEntryCount: () => getEmbeddableEntryCount,
16301
16742
  getDerivedForParent: () => getDerivedForParent,
@@ -16330,9 +16771,7 @@ function openDatabase2(dbPath, options) {
16330
16771
  fs12.mkdirSync(dir, { recursive: true });
16331
16772
  }
16332
16773
  const db = openDatabase(resolvedPath);
16333
- db.exec("PRAGMA journal_mode = WAL");
16334
- db.exec("PRAGMA busy_timeout = 30000");
16335
- db.exec("PRAGMA foreign_keys = ON");
16774
+ applyStandardPragmas(db, { dataDir: dir });
16336
16775
  loadVecExtension(db);
16337
16776
  const resolvedDim = options?.embeddingDim ?? resolveConfiguredEmbeddingDim();
16338
16777
  ensureSchema(db, resolvedDim, { dataDir: dir });
@@ -16353,10 +16792,9 @@ function resolveConfiguredEmbeddingDim() {
16353
16792
  }
16354
16793
  function openExistingDatabase(dbPath) {
16355
16794
  const resolvedPath = dbPath ?? getDbPath();
16795
+ const dir = path11.dirname(resolvedPath);
16356
16796
  const db = openDatabase(resolvedPath);
16357
- db.exec("PRAGMA journal_mode = WAL");
16358
- db.exec("PRAGMA busy_timeout = 30000");
16359
- db.exec("PRAGMA foreign_keys = ON");
16797
+ applyStandardPragmas(db, { dataDir: dir });
16360
16798
  loadVecExtension(db);
16361
16799
  return db;
16362
16800
  }
@@ -16521,6 +16959,7 @@ function ensureSchema(db, embeddingDim, options) {
16521
16959
  CREATE INDEX IF NOT EXISTS idx_llm_cache_updated
16522
16960
  ON llm_enrichment_cache(updated_at);
16523
16961
  `);
16962
+ migrateGraphFilesSchema(db);
16524
16963
  db.exec(`
16525
16964
  CREATE TABLE IF NOT EXISTS graph_meta (
16526
16965
  stash_root TEXT PRIMARY KEY,
@@ -16544,7 +16983,6 @@ function ensureSchema(db, embeddingDim, options) {
16544
16983
  );
16545
16984
 
16546
16985
  CREATE TABLE IF NOT EXISTS graph_files (
16547
- entry_id INTEGER PRIMARY KEY REFERENCES entries(id) ON DELETE CASCADE,
16548
16986
  stash_root TEXT NOT NULL,
16549
16987
  file_path TEXT NOT NULL,
16550
16988
  file_order INTEGER NOT NULL,
@@ -16554,26 +16992,34 @@ function ensureSchema(db, embeddingDim, options) {
16554
16992
  status TEXT NOT NULL DEFAULT 'extracted',
16555
16993
  reason TEXT,
16556
16994
  extraction_run_id TEXT,
16557
- UNIQUE(stash_root, file_path)
16995
+ PRIMARY KEY (stash_root, file_path, body_hash)
16558
16996
  );
16559
16997
 
16998
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_graph_files_path
16999
+ ON graph_files(stash_root, file_path);
17000
+
16560
17001
  CREATE INDEX IF NOT EXISTS idx_graph_files_stash_order
16561
17002
  ON graph_files(stash_root, file_order);
16562
17003
 
16563
17004
  CREATE TABLE IF NOT EXISTS graph_file_entities (
16564
- entry_id INTEGER NOT NULL REFERENCES graph_files(entry_id) ON DELETE CASCADE,
16565
- entity_order INTEGER NOT NULL,
16566
17005
  stash_root TEXT NOT NULL,
17006
+ file_path TEXT NOT NULL,
17007
+ body_hash TEXT NOT NULL,
17008
+ entity_order INTEGER NOT NULL,
16567
17009
  entity_norm TEXT NOT NULL,
16568
17010
  entity TEXT NOT NULL,
16569
- PRIMARY KEY (entry_id, entity_order)
17011
+ PRIMARY KEY (stash_root, file_path, body_hash, entity_order),
17012
+ FOREIGN KEY (stash_root, file_path, body_hash)
17013
+ REFERENCES graph_files(stash_root, file_path, body_hash) ON DELETE CASCADE
16570
17014
  );
16571
17015
 
16572
17016
  CREATE INDEX IF NOT EXISTS idx_graph_file_entities_entity_norm
16573
17017
  ON graph_file_entities(stash_root, entity_norm);
16574
17018
 
16575
17019
  CREATE TABLE IF NOT EXISTS graph_file_relations (
16576
- entry_id INTEGER NOT NULL REFERENCES graph_files(entry_id) ON DELETE CASCADE,
17020
+ stash_root TEXT NOT NULL,
17021
+ file_path TEXT NOT NULL,
17022
+ body_hash TEXT NOT NULL,
16577
17023
  relation_order INTEGER NOT NULL,
16578
17024
  from_entity_norm TEXT NOT NULL,
16579
17025
  from_entity TEXT NOT NULL,
@@ -16581,9 +17027,28 @@ function ensureSchema(db, embeddingDim, options) {
16581
17027
  to_entity TEXT NOT NULL,
16582
17028
  relation_type TEXT,
16583
17029
  confidence REAL,
16584
- PRIMARY KEY (entry_id, relation_order)
17030
+ PRIMARY KEY (stash_root, file_path, body_hash, relation_order),
17031
+ FOREIGN KEY (stash_root, file_path, body_hash)
17032
+ REFERENCES graph_files(stash_root, file_path, body_hash) ON DELETE CASCADE
17033
+ );
17034
+
17035
+ -- #624-P3: lazy graph-extraction queue. Standalone table (NO FK to
17036
+ -- graph_files \u2014 a queued file by definition has no graph row yet).
17037
+ -- Idempotent on (stash_root, file_path); drained highest-priority-first.
17038
+ -- CREATE TABLE IF NOT EXISTS is the forward migration (no DB_VERSION bump).
17039
+ CREATE TABLE IF NOT EXISTS graph_extraction_queue (
17040
+ stash_root TEXT NOT NULL,
17041
+ file_path TEXT NOT NULL,
17042
+ body_hash TEXT NOT NULL,
17043
+ queued_at TEXT NOT NULL DEFAULT (datetime('now')),
17044
+ priority INTEGER NOT NULL DEFAULT 0,
17045
+ PRIMARY KEY (stash_root, file_path)
16585
17046
  );
17047
+
17048
+ CREATE INDEX IF NOT EXISTS idx_graph_extraction_queue_drain
17049
+ ON graph_extraction_queue(stash_root, priority DESC, queued_at);
16586
17050
  `);
17051
+ migrateGraphDataFromLegacy(db);
16587
17052
  db.exec(`
16588
17053
  CREATE TABLE IF NOT EXISTS entries_fts_dirty (
16589
17054
  entry_id INTEGER PRIMARY KEY
@@ -16649,6 +17114,7 @@ function handleVersionUpgrade(db) {
16649
17114
  db.exec("DROP TABLE IF EXISTS index_dir_state");
16650
17115
  db.exec("DROP TABLE IF EXISTS llm_enrichment_cache");
16651
17116
  db.exec("DROP INDEX IF EXISTS idx_llm_cache_updated");
17117
+ db.exec("DROP TABLE IF EXISTS graph_extraction_queue");
16652
17118
  db.exec("DROP TABLE IF EXISTS graph_file_relations");
16653
17119
  db.exec("DROP TABLE IF EXISTS graph_file_entities");
16654
17120
  db.exec("DROP TABLE IF EXISTS graph_files");
@@ -16799,6 +17265,67 @@ function ensureDerivedFromColumn(db) {
16799
17265
  db.exec("CREATE INDEX IF NOT EXISTS idx_entries_derived_from ON entries(derived_from)");
16800
17266
  }, "entries table may not exist on a brand-new DB before CREATE \u2014 caller is responsible");
16801
17267
  }
17268
+ function tableExists(db, name) {
17269
+ const row = db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name=? LIMIT 1").get(name);
17270
+ return row !== undefined && row !== null;
17271
+ }
17272
+ function migrateGraphFilesSchema(db) {
17273
+ bestEffort(() => {
17274
+ const cols = db.prepare("PRAGMA table_info(graph_files)").all();
17275
+ const isLegacyShape = cols.some((c) => c.name === "entry_id");
17276
+ if (!isLegacyShape)
17277
+ return;
17278
+ db.exec("DROP TABLE IF EXISTS graph_file_relations_legacy");
17279
+ db.exec("DROP TABLE IF EXISTS graph_file_entities_legacy");
17280
+ db.exec("DROP TABLE IF EXISTS graph_files_legacy");
17281
+ db.exec("ALTER TABLE graph_files RENAME TO graph_files_legacy");
17282
+ if (tableExists(db, "graph_file_entities")) {
17283
+ db.exec("ALTER TABLE graph_file_entities RENAME TO graph_file_entities_legacy");
17284
+ }
17285
+ if (tableExists(db, "graph_file_relations")) {
17286
+ db.exec("ALTER TABLE graph_file_relations RENAME TO graph_file_relations_legacy");
17287
+ }
17288
+ }, "graph_files may not exist on a brand-new DB before CREATE \u2014 caller is responsible");
17289
+ }
17290
+ function migrateGraphDataFromLegacy(db) {
17291
+ if (!tableExists(db, "graph_files_legacy"))
17292
+ return;
17293
+ let migratedFiles = 0;
17294
+ bestEffort(() => {
17295
+ db.transaction(() => {
17296
+ const res = db.prepare(`INSERT OR IGNORE INTO graph_files
17297
+ (stash_root, file_path, body_hash, file_order, file_type, confidence, status, reason, extraction_run_id)
17298
+ SELECT stash_root, file_path, body_hash, file_order, file_type, confidence, status, reason, extraction_run_id
17299
+ FROM graph_files_legacy
17300
+ WHERE body_hash IS NOT NULL AND body_hash != ''`).run();
17301
+ migratedFiles = Number(res.changes);
17302
+ if (tableExists(db, "graph_file_entities_legacy")) {
17303
+ db.exec(`INSERT OR IGNORE INTO graph_file_entities
17304
+ (stash_root, file_path, body_hash, entity_order, entity_norm, entity)
17305
+ SELECT gf.stash_root, gf.file_path, gf.body_hash, e.entity_order, e.entity_norm, e.entity
17306
+ FROM graph_file_entities_legacy e
17307
+ JOIN graph_files_legacy gf ON gf.entry_id = e.entry_id
17308
+ WHERE gf.body_hash IS NOT NULL AND gf.body_hash != ''`);
17309
+ }
17310
+ if (tableExists(db, "graph_file_relations_legacy")) {
17311
+ db.exec(`INSERT OR IGNORE INTO graph_file_relations
17312
+ (stash_root, file_path, body_hash, relation_order, from_entity_norm, from_entity, to_entity_norm, to_entity, relation_type, confidence)
17313
+ SELECT gf.stash_root, gf.file_path, gf.body_hash, r.relation_order, r.from_entity_norm, r.from_entity, r.to_entity_norm, r.to_entity, r.relation_type, r.confidence
17314
+ FROM graph_file_relations_legacy r
17315
+ JOIN graph_files_legacy gf ON gf.entry_id = r.entry_id
17316
+ WHERE gf.body_hash IS NOT NULL AND gf.body_hash != ''`);
17317
+ }
17318
+ })();
17319
+ }, "graph data migration is best-effort; legacy tables are dropped regardless below");
17320
+ bestEffort(() => {
17321
+ db.exec("DROP TABLE IF EXISTS graph_file_relations_legacy");
17322
+ db.exec("DROP TABLE IF EXISTS graph_file_entities_legacy");
17323
+ db.exec("DROP TABLE IF EXISTS graph_files_legacy");
17324
+ }, "drop legacy graph tables after migration");
17325
+ if (migratedFiles > 0) {
17326
+ warn(`[akm] graph index re-keyed (#624): migrated ${migratedFiles} extracted file(s) to the new schema \u2014 no re-extraction needed. Index + embeddings untouched.`);
17327
+ }
17328
+ }
16802
17329
  function getDerivedForParent(db, parentRef) {
16803
17330
  if (!parentRef)
16804
17331
  return null;
@@ -16888,6 +17415,25 @@ function deleteRelatedRows(db, ids) {
16888
17415
  bestEffort(() => db.prepare(`DELETE FROM utility_scores_scoped WHERE entry_id IN (${placeholders})`).run(...chunk), "delete utility_scores_scoped for entries");
16889
17416
  bestEffort(() => db.prepare(`DELETE FROM usage_events WHERE entry_id IN (${placeholders})`).run(...chunk), "delete usage_events for entries");
16890
17417
  }
17418
+ const affectedStashRoots = new Set;
17419
+ for (let i = 0;i < numericIds.length; i += SQLITE_CHUNK_SIZE) {
17420
+ const chunk = numericIds.slice(i, i + SQLITE_CHUNK_SIZE);
17421
+ const placeholders = chunk.map(() => "?").join(",");
17422
+ bestEffort(() => {
17423
+ const rows = db.prepare(`SELECT DISTINCT stash_dir FROM entries WHERE id IN (${placeholders})`).all(...chunk);
17424
+ for (const row of rows) {
17425
+ if (row.stash_dir)
17426
+ affectedStashRoots.add(row.stash_dir);
17427
+ }
17428
+ }, "resolve stash roots for graph_meta recompute");
17429
+ }
17430
+ for (const stashRoot of affectedStashRoots) {
17431
+ bestEffort(() => db.prepare(`UPDATE graph_meta
17432
+ SET extracted_files = (SELECT COUNT(*) FROM graph_files WHERE stash_root = ?),
17433
+ entity_count = (SELECT COUNT(*) FROM graph_file_entities WHERE stash_root = ?),
17434
+ relation_count = (SELECT COUNT(*) FROM graph_file_relations WHERE stash_root = ?)
17435
+ WHERE stash_root = ?`).run(stashRoot, stashRoot, stashRoot, stashRoot), "sync graph_meta counts after entries delete");
17436
+ }
16891
17437
  }
16892
17438
  function deleteEntriesByIds(db, ids) {
16893
17439
  if (ids.length === 0)
@@ -17017,17 +17563,17 @@ function searchBlobVec(db, queryEmbedding, k) {
17017
17563
  return [];
17018
17564
  }
17019
17565
  }
17020
- function searchFts(db, query, limit, entryType) {
17566
+ function searchFts(db, query, limit, entryType, excludeTypes) {
17021
17567
  const ftsQuery = sanitizeFtsQuery(query);
17022
17568
  if (!ftsQuery)
17023
17569
  return [];
17024
- const exactResults = runFtsQuery(db, ftsQuery, limit, entryType);
17570
+ const exactResults = runFtsQuery(db, ftsQuery, limit, entryType, excludeTypes);
17025
17571
  if (exactResults.length > 0)
17026
17572
  return exactResults;
17027
17573
  const prefixQuery = buildPrefixQuery(ftsQuery);
17028
17574
  if (!prefixQuery)
17029
17575
  return [];
17030
- return runFtsQuery(db, prefixQuery, limit, entryType);
17576
+ return runFtsQuery(db, prefixQuery, limit, entryType, excludeTypes);
17031
17577
  }
17032
17578
  function buildPrefixQuery(ftsQuery) {
17033
17579
  const tokens = ftsQuery.split(/\s+/).filter(Boolean);
@@ -17043,9 +17589,10 @@ function buildPrefixQuery(ftsQuery) {
17043
17589
  return null;
17044
17590
  return prefixTokens.join(" ");
17045
17591
  }
17046
- function runFtsQuery(db, ftsQuery, limit, entryType) {
17592
+ function runFtsQuery(db, ftsQuery, limit, entryType, excludeTypes) {
17047
17593
  let sql;
17048
17594
  let params;
17595
+ const excludes = excludeTypes && excludeTypes.length > 0 ? excludeTypes : [];
17049
17596
  if (entryType && entryType !== "any") {
17050
17597
  sql = `
17051
17598
  SELECT e.id, e.file_path AS filePath, e.entry_json, e.search_text AS searchText,
@@ -17059,16 +17606,18 @@ function runFtsQuery(db, ftsQuery, limit, entryType) {
17059
17606
  `;
17060
17607
  params = [ftsQuery, entryType, limit];
17061
17608
  } else {
17609
+ const excludeClause = excludes.length > 0 ? `AND e.entry_type NOT IN (${excludes.map(() => "?").join(", ")})` : "";
17062
17610
  sql = `
17063
17611
  SELECT e.id, e.file_path AS filePath, e.entry_json, e.search_text AS searchText,
17064
17612
  bm25(entries_fts, 0, 10.0, 5.0, 3.0, 2.0, 1.0) AS bm25Score
17065
17613
  FROM entries_fts f
17066
17614
  JOIN entries e ON e.id = f.entry_id
17067
17615
  WHERE entries_fts MATCH ?
17616
+ ${excludeClause}
17068
17617
  ORDER BY bm25Score, e.id ASC
17069
17618
  LIMIT ?
17070
17619
  `;
17071
- params = [ftsQuery, limit];
17620
+ params = [ftsQuery, ...excludes, limit];
17072
17621
  }
17073
17622
  try {
17074
17623
  const rows = db.prepare(sql).all(...params);
@@ -17124,12 +17673,16 @@ function parseEntryRows(rows, context) {
17124
17673
  }
17125
17674
  return entries;
17126
17675
  }
17127
- function getAllEntries(db, entryType) {
17676
+ function getAllEntries(db, entryType, excludeTypes) {
17128
17677
  let sql;
17129
17678
  let params;
17679
+ const excludes = excludeTypes && excludeTypes.length > 0 ? excludeTypes : [];
17130
17680
  if (entryType && entryType !== "any") {
17131
17681
  sql = "SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries WHERE entry_type = ?";
17132
17682
  params = [entryType];
17683
+ } else if (excludes.length > 0) {
17684
+ sql = `SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries WHERE entry_type NOT IN (${excludes.map(() => "?").join(", ")})`;
17685
+ params = [...excludes];
17133
17686
  } else {
17134
17687
  sql = "SELECT id, entry_key, dir_path, file_path, stash_dir, entry_json, search_text FROM entries";
17135
17688
  params = [];
@@ -17137,6 +17690,33 @@ function getAllEntries(db, entryType) {
17137
17690
  const rows = db.prepare(sql).all(...params);
17138
17691
  return parseEntryRows(rows, "getAllEntries");
17139
17692
  }
17693
+ function getEntitiesByEntryIds(db, entryIds) {
17694
+ const result = new Map;
17695
+ if (entryIds.length === 0)
17696
+ return result;
17697
+ for (let i = 0;i < entryIds.length; i += SQLITE_CHUNK_SIZE) {
17698
+ const chunk = entryIds.slice(i, i + SQLITE_CHUNK_SIZE);
17699
+ const placeholders = chunk.map(() => "?").join(", ");
17700
+ const rows = db.prepare(`SELECT e.id AS entry_id, gfe.entity_norm AS entity_norm
17701
+ FROM entries e
17702
+ JOIN graph_files gf
17703
+ ON gf.stash_root = e.stash_dir AND gf.file_path = e.file_path
17704
+ JOIN graph_file_entities gfe
17705
+ ON gfe.stash_root = gf.stash_root
17706
+ AND gfe.file_path = gf.file_path
17707
+ AND gfe.body_hash = gf.body_hash
17708
+ WHERE e.id IN (${placeholders})
17709
+ ORDER BY e.id, gfe.entity_order`).all(...chunk);
17710
+ for (const row of rows) {
17711
+ const list = result.get(row.entry_id);
17712
+ if (list)
17713
+ list.push(row.entity_norm);
17714
+ else
17715
+ result.set(row.entry_id, [row.entity_norm]);
17716
+ }
17717
+ }
17718
+ return result;
17719
+ }
17140
17720
  function findEntryIdByRef(db, ref) {
17141
17721
  const parsed = parseAssetRef(ref);
17142
17722
  const nameVariants = [parsed.name];
@@ -17187,6 +17767,10 @@ function getEntryIdByFilePath(db, filePath) {
17187
17767
  const row = db.prepare("SELECT id FROM entries WHERE file_path = ? LIMIT 1").get(filePath);
17188
17768
  return row?.id;
17189
17769
  }
17770
+ function getIndexedFilePaths(db) {
17771
+ const rows = db.prepare("SELECT DISTINCT file_path FROM entries WHERE file_path IS NOT NULL AND file_path <> ''").all();
17772
+ return new Set(rows.map((r) => r.file_path));
17773
+ }
17190
17774
  function getEntryFilePathById(db, id) {
17191
17775
  const row = db.prepare("SELECT file_path FROM entries WHERE id = ?").get(id);
17192
17776
  return row?.file_path;
@@ -17314,18 +17898,56 @@ function clearStaleCacheEntries(db) {
17314
17898
  function computeBodyHash(body) {
17315
17899
  return sha256Hex(body);
17316
17900
  }
17901
+ function bareRef(ref) {
17902
+ try {
17903
+ const parsed = parseAssetRef(ref);
17904
+ return `${parsed.type}:${parsed.name}`;
17905
+ } catch {
17906
+ return ref;
17907
+ }
17908
+ }
17317
17909
  function getRetrievalCounts(db, refs) {
17318
17910
  if (refs.length === 0)
17319
17911
  return new Map;
17320
- const result = new Map;
17321
- for (let i = 0;i < refs.length; i += SQLITE_CHUNK_SIZE) {
17322
- const chunk = refs.slice(i, i + SQLITE_CHUNK_SIZE);
17912
+ const bareToInputs = new Map;
17913
+ for (const ref of refs) {
17914
+ const bare = bareRef(ref);
17915
+ const existing = bareToInputs.get(bare);
17916
+ if (existing)
17917
+ existing.push(ref);
17918
+ else
17919
+ bareToInputs.set(bare, [ref]);
17920
+ }
17921
+ const bareForms = [...bareToInputs.keys()];
17922
+ const countsByBare = new Map;
17923
+ for (let i = 0;i < bareForms.length; i += SQLITE_CHUNK_SIZE) {
17924
+ const chunk = bareForms.slice(i, i + SQLITE_CHUNK_SIZE);
17323
17925
  const placeholders = chunk.map(() => "?").join(", ");
17324
- const rows = db.prepare(`SELECT entry_ref, COUNT(*) AS cnt FROM usage_events
17325
- WHERE event_type IN ('search','show') AND entry_ref IN (${placeholders})
17326
- GROUP BY entry_ref`).all(...chunk);
17327
- for (const r of rows)
17328
- result.set(r.entry_ref, r.cnt);
17926
+ const rows = db.prepare(`SELECT
17927
+ CASE
17928
+ WHEN instr(entry_ref, '//') > 0
17929
+ THEN substr(entry_ref, instr(entry_ref, '//') + 2)
17930
+ ELSE entry_ref
17931
+ END AS bare_ref,
17932
+ COUNT(*) AS cnt
17933
+ FROM usage_events
17934
+ WHERE event_type IN ('search','show','curate')
17935
+ AND entry_ref IS NOT NULL
17936
+ AND CASE
17937
+ WHEN instr(entry_ref, '//') > 0
17938
+ THEN substr(entry_ref, instr(entry_ref, '//') + 2)
17939
+ ELSE entry_ref
17940
+ END IN (${placeholders})
17941
+ GROUP BY bare_ref`).all(...chunk);
17942
+ for (const r of rows) {
17943
+ countsByBare.set(r.bare_ref, (countsByBare.get(r.bare_ref) ?? 0) + r.cnt);
17944
+ }
17945
+ }
17946
+ const result = new Map;
17947
+ for (const [bare, count] of countsByBare) {
17948
+ for (const input of bareToInputs.get(bare) ?? []) {
17949
+ result.set(input, count);
17950
+ }
17329
17951
  }
17330
17952
  return result;
17331
17953
  }
@@ -17489,7 +18111,7 @@ function collectTagSetFromEntries(db, entryType) {
17489
18111
  }
17490
18112
  return tags;
17491
18113
  }
17492
- var DB_VERSION = 17, EMBEDDING_DIM = 384, GRAPH_SCHEMA_VERSION = 3, vecStatus, VEC_DOCS_URL = "https://github.com/itlackey/akm/blob/main/docs/configuration.md#sqlite-vec-extension", VEC_FALLBACK_THRESHOLD = 1e4, vecInitWarnedDbs, SQLITE_CHUNK_SIZE = 500, upsertStmtsByDb, FEEDBACK_LR = 0.1, FEEDBACK_REWARD_POSITIVE = 1, FEEDBACK_REWARD_NEGATIVE = 0, MAX_NEG_DELTA_PER_CALL = 0.15, UTILITY_REVIEW_THRESHOLD = 0.5, HIGH_UTILITY_THRESHOLD = 0.5;
18114
+ var DB_VERSION = 17, EMBEDDING_DIM = 384, GRAPH_SCHEMA_VERSION = 4, vecStatus, VEC_DOCS_URL = "https://github.com/itlackey/akm/blob/main/docs/configuration.md#sqlite-vec-extension", VEC_FALLBACK_THRESHOLD = 1e4, vecInitWarnedDbs, SQLITE_CHUNK_SIZE = 500, upsertStmtsByDb, FEEDBACK_LR = 0.1, FEEDBACK_REWARD_POSITIVE = 1, FEEDBACK_REWARD_NEGATIVE = 0, MAX_NEG_DELTA_PER_CALL = 0.15, UTILITY_REVIEW_THRESHOLD = 0.5, HIGH_UTILITY_THRESHOLD = 0.5;
17493
18115
  var init_db = __esm(() => {
17494
18116
  init_asset_ref();
17495
18117
  init_best_effort();
@@ -17499,6 +18121,7 @@ var init_db = __esm(() => {
17499
18121
  init_types();
17500
18122
  init_runtime();
17501
18123
  init_database();
18124
+ init_sqlite_pragmas();
17502
18125
  init_db_backup();
17503
18126
  vecStatus = new WeakMap;
17504
18127
  vecInitWarnedDbs = new WeakSet;
@@ -17508,13 +18131,15 @@ var init_db = __esm(() => {
17508
18131
  // src/indexer/db/graph-db.ts
17509
18132
  var exports_graph_db = {};
17510
18133
  __export(exports_graph_db, {
17511
- resolveEntryIdForPath: () => resolveEntryIdForPath,
17512
18134
  replaceStoredGraph: () => replaceStoredGraph,
17513
18135
  loadStoredGraphSnapshot: () => loadStoredGraphSnapshot,
17514
18136
  loadStoredGraphMeta: () => loadStoredGraphMeta,
17515
18137
  loadGraphMetaOnly: () => loadGraphMetaOnly,
17516
18138
  loadGraphFilesOnly: () => loadGraphFilesOnly,
17517
- loadGraphEntitiesByEntry: () => loadGraphEntitiesByEntry,
18139
+ loadGraphEntitiesByPath: () => loadGraphEntitiesByPath,
18140
+ hasGraphData: () => hasGraphData,
18141
+ enqueueGraphExtraction: () => enqueueGraphExtraction,
18142
+ drainExtractionQueue: () => drainExtractionQueue,
17518
18143
  deleteStoredGraph: () => deleteStoredGraph
17519
18144
  });
17520
18145
  import fs13 from "fs";
@@ -17537,17 +18162,6 @@ function uniqueSorted(values) {
17537
18162
  function normalizeEntity(value) {
17538
18163
  return value.trim().toLowerCase();
17539
18164
  }
17540
- function resolveEntryIdForPath(db, stashRoot, filePath) {
17541
- try {
17542
- const row = db.prepare("SELECT id FROM entries WHERE stash_dir = ? AND file_path = ? LIMIT 1").get(stashRoot, filePath);
17543
- if (row)
17544
- return row.id;
17545
- const fallback = db.prepare("SELECT id FROM entries WHERE file_path = ? LIMIT 1").get(filePath);
17546
- return fallback?.id ?? null;
17547
- } catch {
17548
- return null;
17549
- }
17550
- }
17551
18165
  function replaceStoredGraph(db, graph) {
17552
18166
  const upsertMeta = db.prepare(`INSERT INTO graph_meta (
17553
18167
  stash_root,
@@ -17587,21 +18201,21 @@ function replaceStoredGraph(db, graph) {
17587
18201
  cache_misses = excluded.cache_misses,
17588
18202
  truncation_count = excluded.truncation_count,
17589
18203
  failure_count = excluded.failure_count`);
17590
- const selectExisting = db.prepare("SELECT entry_id, file_path, body_hash FROM graph_files WHERE stash_root = ?");
17591
- const deleteFile = db.prepare("DELETE FROM graph_files WHERE entry_id = ?");
17592
- const deleteEntities = db.prepare("DELETE FROM graph_file_entities WHERE entry_id = ?");
17593
- const deleteRelations = db.prepare("DELETE FROM graph_file_relations WHERE entry_id = ?");
18204
+ const selectExisting = db.prepare("SELECT file_path, body_hash, file_order FROM graph_files WHERE stash_root = ?");
18205
+ const deleteFile = db.prepare("DELETE FROM graph_files WHERE stash_root = ? AND file_path = ? AND body_hash = ?");
18206
+ const deleteEntities = db.prepare("DELETE FROM graph_file_entities WHERE stash_root = ? AND file_path = ? AND body_hash = ?");
18207
+ const deleteRelations = db.prepare("DELETE FROM graph_file_relations WHERE stash_root = ? AND file_path = ? AND body_hash = ?");
17594
18208
  const insertFile = db.prepare(`INSERT INTO graph_files (
17595
- entry_id, stash_root, file_path, file_order, file_type, body_hash, confidence, status, reason, extraction_run_id
17596
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
18209
+ stash_root, file_path, file_order, file_type, body_hash, confidence, status, reason, extraction_run_id
18210
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`);
17597
18211
  const updateFileMeta = db.prepare(`UPDATE graph_files
17598
18212
  SET file_order = ?, file_type = ?, confidence = ?, status = ?, reason = ?, extraction_run_id = ?
17599
- WHERE entry_id = ?`);
17600
- const insertEntity = db.prepare(`INSERT INTO graph_file_entities (entry_id, entity_order, stash_root, entity_norm, entity)
17601
- VALUES (?, ?, ?, ?, ?)`);
18213
+ WHERE stash_root = ? AND file_path = ? AND body_hash = ?`);
18214
+ const insertEntity = db.prepare(`INSERT INTO graph_file_entities (stash_root, file_path, body_hash, entity_order, entity_norm, entity)
18215
+ VALUES (?, ?, ?, ?, ?, ?)`);
17602
18216
  const insertRelation = db.prepare(`INSERT INTO graph_file_relations (
17603
- entry_id, relation_order, from_entity_norm, from_entity, to_entity_norm, to_entity, relation_type, confidence
17604
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
18217
+ stash_root, file_path, body_hash, relation_order, from_entity_norm, from_entity, to_entity_norm, to_entity, relation_type, confidence
18218
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
17605
18219
  const quality = graph.quality;
17606
18220
  const telemetry = graph.telemetry;
17607
18221
  db.transaction(() => {
@@ -17610,44 +18224,35 @@ function replaceStoredGraph(db, graph) {
17610
18224
  const existingByPath = new Map;
17611
18225
  for (const row of existingRows)
17612
18226
  existingByPath.set(row.file_path, row);
17613
- let orphanCount = 0;
17614
- const presentEntryIds = new Set;
18227
+ const presentPaths = new Set;
17615
18228
  for (const [fileOrder, node] of graph.files.entries()) {
17616
18229
  const bodyHash = node.bodyHash && node.bodyHash.length > 0 ? node.bodyHash : "";
17617
- const entryId = resolveEntryIdForPath(db, graph.stashRoot, node.path);
17618
- if (entryId == null) {
17619
- orphanCount += 1;
17620
- continue;
17621
- }
17622
- presentEntryIds.add(entryId);
18230
+ presentPaths.add(node.path);
17623
18231
  const existing = existingByPath.get(node.path);
17624
- if (existing && existing.entry_id === entryId && existing.body_hash === bodyHash) {
17625
- updateFileMeta.run(fileOrder, node.type, node.confidence ?? null, node.status ?? (node.entities.length > 0 ? "extracted" : "empty"), node.reason ?? (node.entities.length > 0 ? "none" : "no_graph_content"), node.extractionRunId ?? telemetry?.extractionRunId ?? null, entryId);
18232
+ if (existing && existing.body_hash === bodyHash) {
18233
+ updateFileMeta.run(fileOrder, node.type, node.confidence ?? null, node.status ?? (node.entities.length > 0 ? "extracted" : "empty"), node.reason ?? (node.entities.length > 0 ? "none" : "no_graph_content"), node.extractionRunId ?? telemetry?.extractionRunId ?? null, graph.stashRoot, node.path, bodyHash);
17626
18234
  continue;
17627
18235
  }
17628
18236
  if (existing) {
17629
- deleteEntities.run(existing.entry_id);
17630
- deleteRelations.run(existing.entry_id);
17631
- deleteFile.run(existing.entry_id);
18237
+ deleteEntities.run(graph.stashRoot, existing.file_path, existing.body_hash);
18238
+ deleteRelations.run(graph.stashRoot, existing.file_path, existing.body_hash);
18239
+ deleteFile.run(graph.stashRoot, existing.file_path, existing.body_hash);
17632
18240
  }
17633
- insertFile.run(entryId, graph.stashRoot, node.path, fileOrder, node.type, bodyHash, node.confidence ?? null, node.status ?? (node.entities.length > 0 ? "extracted" : "empty"), node.reason ?? (node.entities.length > 0 ? "none" : "no_graph_content"), node.extractionRunId ?? telemetry?.extractionRunId ?? null);
18241
+ insertFile.run(graph.stashRoot, node.path, fileOrder, node.type, bodyHash, node.confidence ?? null, node.status ?? (node.entities.length > 0 ? "extracted" : "empty"), node.reason ?? (node.entities.length > 0 ? "none" : "no_graph_content"), node.extractionRunId ?? telemetry?.extractionRunId ?? null);
17634
18242
  for (const [entityOrder, entity] of node.entities.entries()) {
17635
- insertEntity.run(entryId, entityOrder, graph.stashRoot, normalizeEntity(entity), entity);
18243
+ insertEntity.run(graph.stashRoot, node.path, bodyHash, entityOrder, normalizeEntity(entity), entity);
17636
18244
  }
17637
18245
  for (const [relationOrder, relation] of node.relations.entries()) {
17638
- insertRelation.run(entryId, relationOrder, normalizeEntity(relation.from), relation.from, normalizeEntity(relation.to), relation.to, relation.type ?? null, relation.confidence ?? null);
18246
+ insertRelation.run(graph.stashRoot, node.path, bodyHash, relationOrder, normalizeEntity(relation.from), relation.from, normalizeEntity(relation.to), relation.to, relation.type ?? null, relation.confidence ?? null);
17639
18247
  }
17640
18248
  }
17641
18249
  for (const row of existingRows) {
17642
- if (!presentEntryIds.has(row.entry_id)) {
17643
- deleteEntities.run(row.entry_id);
17644
- deleteRelations.run(row.entry_id);
17645
- deleteFile.run(row.entry_id);
18250
+ if (!presentPaths.has(row.file_path)) {
18251
+ deleteEntities.run(graph.stashRoot, row.file_path, row.body_hash);
18252
+ deleteRelations.run(graph.stashRoot, row.file_path, row.body_hash);
18253
+ deleteFile.run(graph.stashRoot, row.file_path, row.body_hash);
17646
18254
  }
17647
18255
  }
17648
- if (orphanCount > 0) {
17649
- warn(`[graph] replaceStoredGraph: skipped ${orphanCount} file(s) with no resolvable entry under ${graph.stashRoot}.`);
17650
- }
17651
18256
  })();
17652
18257
  }
17653
18258
  function deleteStoredGraph(db, stashPath) {
@@ -17656,6 +18261,44 @@ function deleteStoredGraph(db, stashPath) {
17656
18261
  db.prepare("DELETE FROM graph_meta WHERE stash_root = ?").run(stashPath);
17657
18262
  })();
17658
18263
  }
18264
+ function hasGraphData(db, stashRoot, filePath) {
18265
+ try {
18266
+ const row = db.prepare("SELECT 1 AS present FROM graph_files WHERE stash_root = ? AND file_path = ? LIMIT 1").get(stashRoot, filePath);
18267
+ return row !== undefined;
18268
+ } catch {
18269
+ return false;
18270
+ }
18271
+ }
18272
+ function enqueueGraphExtraction(db, stashRoot, filePath, bodyHash, priority = 0) {
18273
+ try {
18274
+ db.prepare(`INSERT INTO graph_extraction_queue (stash_root, file_path, body_hash, priority)
18275
+ VALUES (?, ?, ?, ?)
18276
+ ON CONFLICT(stash_root, file_path) DO UPDATE SET
18277
+ body_hash = excluded.body_hash,
18278
+ priority = MAX(graph_extraction_queue.priority, excluded.priority),
18279
+ queued_at = datetime('now')`).run(stashRoot, filePath, bodyHash, priority);
18280
+ } catch (err) {
18281
+ rethrowIfTestIsolationError(err);
18282
+ }
18283
+ }
18284
+ function drainExtractionQueue(db, stashRoot, limit) {
18285
+ try {
18286
+ return db.transaction(() => {
18287
+ const rows = db.prepare(`SELECT file_path, body_hash, priority
18288
+ FROM graph_extraction_queue
18289
+ WHERE stash_root = ?
18290
+ ORDER BY priority DESC, queued_at ASC
18291
+ LIMIT ?`).all(stashRoot, limit);
18292
+ const del = db.prepare("DELETE FROM graph_extraction_queue WHERE stash_root = ? AND file_path = ?");
18293
+ for (const row of rows)
18294
+ del.run(stashRoot, row.file_path);
18295
+ return rows.map((row) => ({ filePath: row.file_path, bodyHash: row.body_hash, priority: row.priority }));
18296
+ })();
18297
+ } catch (err) {
18298
+ rethrowIfTestIsolationError(err);
18299
+ return [];
18300
+ }
18301
+ }
17659
18302
  function loadGraphMetaOnly(stashPath, db) {
17660
18303
  return loadStoredGraphMeta(stashPath, db);
17661
18304
  }
@@ -17663,12 +18306,11 @@ function loadGraphFilesOnly(stashPath, db) {
17663
18306
  try {
17664
18307
  return withReadableGraphDb(db, (readDb) => {
17665
18308
  try {
17666
- const rows = readDb.prepare(`SELECT entry_id, file_path, file_type, body_hash, confidence, status, reason
18309
+ const rows = readDb.prepare(`SELECT file_path, file_type, body_hash, confidence, status, reason
17667
18310
  FROM graph_files
17668
18311
  WHERE stash_root = ?
17669
18312
  ORDER BY file_order`).all(stashPath);
17670
18313
  return rows.map((row) => ({
17671
- entryId: row.entry_id,
17672
18314
  path: row.file_path,
17673
18315
  type: row.file_type,
17674
18316
  bodyHash: row.body_hash,
@@ -17685,9 +18327,11 @@ function loadGraphFilesOnly(stashPath, db) {
17685
18327
  return [];
17686
18328
  }
17687
18329
  }
17688
- function loadGraphEntitiesByEntry(db, entryId) {
18330
+ function loadGraphEntitiesByPath(db, stashRoot, filePath, bodyHash) {
17689
18331
  try {
17690
- const rows = db.prepare("SELECT entity FROM graph_file_entities WHERE entry_id = ? ORDER BY entity_order").all(entryId);
18332
+ const rows = db.prepare(`SELECT entity FROM graph_file_entities
18333
+ WHERE stash_root = ? AND file_path = ? AND body_hash = ?
18334
+ ORDER BY entity_order`).all(stashRoot, filePath, bodyHash);
17691
18335
  return rows.map((r) => r.entity);
17692
18336
  } catch {
17693
18337
  return [];
@@ -17762,23 +18406,28 @@ function loadStoredGraphSnapshot(stashPath, db) {
17762
18406
  if (!meta)
17763
18407
  return null;
17764
18408
  try {
17765
- const fileRows = readDb.prepare(`SELECT entry_id, file_path, file_type, body_hash, confidence, status, reason, extraction_run_id
18409
+ const fileRows = readDb.prepare(`SELECT file_path, file_type, body_hash, confidence, status, reason, extraction_run_id
17766
18410
  FROM graph_files
17767
18411
  WHERE stash_root = ?
17768
18412
  ORDER BY file_order`).all(stashPath);
17769
- const entityRows = readDb.prepare(`SELECT gfe.entry_id AS entry_id, gf.file_path AS file_path, gfe.entity AS entity
18413
+ const entityRows = readDb.prepare(`SELECT gf.file_path AS file_path, gfe.entity AS entity
17770
18414
  FROM graph_file_entities gfe
17771
- JOIN graph_files gf ON gf.entry_id = gfe.entry_id
18415
+ JOIN graph_files gf
18416
+ ON gf.stash_root = gfe.stash_root
18417
+ AND gf.file_path = gfe.file_path
18418
+ AND gf.body_hash = gfe.body_hash
17772
18419
  WHERE gf.stash_root = ?
17773
18420
  ORDER BY gf.file_order, gfe.entity_order`).all(stashPath);
17774
- const relationRows = readDb.prepare(`SELECT gfr.entry_id AS entry_id,
17775
- gf.file_path AS file_path,
18421
+ const relationRows = readDb.prepare(`SELECT gf.file_path AS file_path,
17776
18422
  gfr.from_entity AS from_entity,
17777
18423
  gfr.to_entity AS to_entity,
17778
18424
  gfr.relation_type AS relation_type,
17779
18425
  gfr.confidence AS confidence
17780
18426
  FROM graph_file_relations gfr
17781
- JOIN graph_files gf ON gf.entry_id = gfr.entry_id
18427
+ JOIN graph_files gf
18428
+ ON gf.stash_root = gfr.stash_root
18429
+ AND gf.file_path = gfr.file_path
18430
+ AND gf.body_hash = gfr.body_hash
17782
18431
  WHERE gf.stash_root = ?
17783
18432
  ORDER BY gf.file_order, gfr.relation_order`).all(stashPath);
17784
18433
  const entitiesByPath = new Map;
@@ -17837,7 +18486,6 @@ function loadStoredGraphSnapshot(stashPath, db) {
17837
18486
  var init_graph_db = __esm(() => {
17838
18487
  init_errors();
17839
18488
  init_paths();
17840
- init_warn();
17841
18489
  init_db();
17842
18490
  });
17843
18491