akm-cli 0.7.5 → 0.8.0-rc1

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 (151) hide show
  1. package/.github/CHANGELOG.md +1 -1
  2. package/dist/cli/parse-args.js +43 -0
  3. package/dist/cli.js +804 -461
  4. package/dist/commands/agent-dispatch.js +102 -0
  5. package/dist/commands/agent-support.js +62 -0
  6. package/dist/commands/config-cli.js +68 -84
  7. package/dist/commands/consolidate.js +823 -0
  8. package/dist/commands/distill-promotion-policy.js +658 -0
  9. package/dist/commands/distill.js +244 -52
  10. package/dist/commands/eval-cases.js +40 -0
  11. package/dist/commands/events.js +2 -23
  12. package/dist/commands/graph.js +222 -0
  13. package/dist/commands/health.js +376 -0
  14. package/dist/commands/help/help-accept.md +9 -0
  15. package/dist/commands/help/help-improve.md +53 -0
  16. package/dist/commands/help/help-proposals.md +15 -0
  17. package/dist/commands/help/help-propose.md +17 -0
  18. package/dist/commands/help/help-reject.md +8 -0
  19. package/dist/commands/history.js +3 -30
  20. package/dist/commands/improve.js +1170 -0
  21. package/dist/commands/info.js +2 -2
  22. package/dist/commands/init.js +2 -2
  23. package/dist/commands/install-audit.js +5 -1
  24. package/dist/commands/installed-stashes.js +118 -138
  25. package/dist/commands/knowledge.js +133 -0
  26. package/dist/commands/lint/agent-linter.js +46 -0
  27. package/dist/commands/lint/base-linter.js +251 -0
  28. package/dist/commands/lint/command-linter.js +46 -0
  29. package/dist/commands/lint/default-linter.js +13 -0
  30. package/dist/commands/lint/index.js +107 -0
  31. package/dist/commands/lint/knowledge-linter.js +13 -0
  32. package/dist/commands/lint/memory-linter.js +58 -0
  33. package/dist/commands/lint/registry.js +33 -0
  34. package/dist/commands/lint/skill-linter.js +42 -0
  35. package/dist/commands/lint/task-linter.js +47 -0
  36. package/dist/commands/lint/types.js +1 -0
  37. package/dist/commands/lint/workflow-linter.js +53 -0
  38. package/dist/commands/lint.js +1 -0
  39. package/dist/commands/proposal.js +8 -7
  40. package/dist/commands/propose.js +78 -28
  41. package/dist/commands/reflect.js +143 -35
  42. package/dist/commands/registry-search.js +2 -2
  43. package/dist/commands/remember.js +54 -0
  44. package/dist/commands/schema-repair.js +130 -0
  45. package/dist/commands/search.js +21 -5
  46. package/dist/commands/show.js +121 -17
  47. package/dist/commands/source-add.js +10 -10
  48. package/dist/commands/source-manage.js +11 -19
  49. package/dist/commands/tasks.js +385 -0
  50. package/dist/commands/url-checker.js +39 -0
  51. package/dist/commands/vault.js +2 -23
  52. package/dist/core/action-contributors.js +25 -0
  53. package/dist/core/asset-registry.js +4 -16
  54. package/dist/core/asset-spec.js +10 -0
  55. package/dist/core/common.js +94 -0
  56. package/dist/core/concurrent.js +22 -0
  57. package/dist/core/config.js +222 -128
  58. package/dist/core/events.js +73 -126
  59. package/dist/core/frontmatter.js +3 -1
  60. package/dist/core/markdown.js +17 -0
  61. package/dist/core/memory-improve.js +678 -0
  62. package/dist/core/parse.js +155 -0
  63. package/dist/core/paths.js +101 -3
  64. package/dist/core/proposal-validators.js +61 -0
  65. package/dist/core/proposals.js +49 -38
  66. package/dist/core/state-db.js +775 -0
  67. package/dist/core/time.js +51 -0
  68. package/dist/core/warn.js +59 -1
  69. package/dist/indexer/db-search.js +52 -238
  70. package/dist/indexer/db.js +377 -1
  71. package/dist/indexer/ensure-index.js +61 -0
  72. package/dist/indexer/graph-boost.js +247 -94
  73. package/dist/indexer/graph-db.js +201 -0
  74. package/dist/indexer/graph-dedup.js +99 -0
  75. package/dist/indexer/graph-extraction.js +409 -76
  76. package/dist/indexer/index-context.js +10 -0
  77. package/dist/indexer/indexer.js +442 -290
  78. package/dist/indexer/llm-cache.js +47 -0
  79. package/dist/indexer/match-contributors.js +141 -0
  80. package/dist/indexer/matchers.js +24 -190
  81. package/dist/indexer/memory-inference.js +63 -29
  82. package/dist/indexer/metadata-contributors.js +26 -0
  83. package/dist/indexer/metadata.js +188 -175
  84. package/dist/indexer/path-resolver.js +89 -0
  85. package/dist/indexer/ranking-contributors.js +204 -0
  86. package/dist/indexer/ranking.js +74 -0
  87. package/dist/indexer/search-hit-enrichers.js +22 -0
  88. package/dist/indexer/search-source.js +24 -9
  89. package/dist/indexer/semantic-status.js +2 -16
  90. package/dist/indexer/walker.js +25 -0
  91. package/dist/integrations/agent/config.js +175 -3
  92. package/dist/integrations/agent/index.js +3 -1
  93. package/dist/integrations/agent/pipeline.js +39 -0
  94. package/dist/integrations/agent/profiles.js +67 -5
  95. package/dist/integrations/agent/prompts.js +77 -72
  96. package/dist/integrations/agent/runners.js +31 -0
  97. package/dist/integrations/agent/sdk-runner.js +120 -0
  98. package/dist/integrations/agent/spawn.js +71 -16
  99. package/dist/integrations/lockfile.js +10 -18
  100. package/dist/integrations/session-logs/index.js +65 -0
  101. package/dist/integrations/session-logs/providers/claude-code.js +56 -0
  102. package/dist/integrations/session-logs/providers/opencode.js +52 -0
  103. package/dist/integrations/session-logs/types.js +1 -0
  104. package/dist/llm/call-ai.js +74 -0
  105. package/dist/llm/client.js +61 -122
  106. package/dist/llm/feature-gate.js +27 -16
  107. package/dist/llm/graph-extract.js +297 -62
  108. package/dist/llm/memory-infer.js +49 -71
  109. package/dist/llm/metadata-enhance.js +39 -22
  110. package/dist/llm/prompts/graph-extract-user-prompt.md +12 -0
  111. package/dist/output/cli-hints-full.md +277 -0
  112. package/dist/output/cli-hints-short.md +65 -0
  113. package/dist/output/cli-hints.js +2 -318
  114. package/dist/output/renderers.js +190 -123
  115. package/dist/output/shapes.js +33 -0
  116. package/dist/output/text.js +239 -2
  117. package/dist/registry/providers/skills-sh.js +61 -49
  118. package/dist/registry/providers/static-index.js +44 -48
  119. package/dist/setup/setup.js +510 -11
  120. package/dist/sources/provider-factory.js +2 -1
  121. package/dist/sources/providers/git.js +2 -2
  122. package/dist/sources/website-ingest.js +4 -0
  123. package/dist/tasks/backends/cron.js +200 -0
  124. package/dist/tasks/backends/exec-utils.js +25 -0
  125. package/dist/tasks/backends/index.js +32 -0
  126. package/dist/tasks/backends/launchd-template.xml +19 -0
  127. package/dist/tasks/backends/launchd.js +184 -0
  128. package/dist/tasks/backends/schtasks-template.xml +29 -0
  129. package/dist/tasks/backends/schtasks.js +212 -0
  130. package/dist/tasks/parser.js +198 -0
  131. package/dist/tasks/resolveAkmBin.js +84 -0
  132. package/dist/tasks/runner.js +432 -0
  133. package/dist/tasks/schedule.js +208 -0
  134. package/dist/tasks/schema.js +13 -0
  135. package/dist/tasks/validator.js +59 -0
  136. package/dist/wiki/index-template.md +12 -0
  137. package/dist/wiki/ingest-workflow-template.md +54 -0
  138. package/dist/wiki/log-template.md +8 -0
  139. package/dist/wiki/schema-template.md +61 -0
  140. package/dist/wiki/wiki-templates.js +12 -0
  141. package/dist/wiki/wiki.js +10 -61
  142. package/dist/workflows/authoring.js +5 -25
  143. package/dist/workflows/renderer.js +8 -3
  144. package/dist/workflows/runs.js +59 -91
  145. package/dist/workflows/validator.js +1 -1
  146. package/dist/workflows/workflow-template.md +24 -0
  147. package/docs/README.md +3 -0
  148. package/docs/migration/release-notes/0.7.0.md +1 -1
  149. package/docs/migration/release-notes/0.8.0.md +43 -0
  150. package/package.json +3 -2
  151. package/dist/templates/wiki-templates.js +0 -100
@@ -1,20 +1,21 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { deriveCanonicalAssetName, deriveCanonicalAssetNameFromStashRoot, isRelevantAssetFile, } from "../core/asset-spec";
4
- import { isAssetType } from "../core/common";
4
+ import { isAssetType, writeFileAtomic } from "../core/common";
5
5
  import { parseFrontmatter, toStringOrUndefined } from "../core/frontmatter";
6
6
  import { isVerbose, warn } from "../core/warn";
7
7
  import { buildFileContext, buildRenderContext, getRenderer, runMatchers } from "./file-context";
8
+ import { applyMetadataContributors } from "./metadata-contributors";
8
9
  export const SCOPE_KEYS = ["user", "agent", "run", "channel"];
9
10
  // ── Load / Write ────────────────────────────────────────────────────────────
10
11
  const STASH_FILENAME = ".stash.json";
11
12
  // ── Quality semantics (v1 spec §4.2) ────────────────────────────────────────
12
13
  /**
13
- * Well-known quality values. `generated` and `curated` are included in
14
+ * Well-known quality values. `generated`, `curated`, and `enriched` are included in
14
15
  * default search; `proposed` is excluded by default and opt-in via
15
16
  * `--include-proposed`. Unknown values warn once and remain searchable.
16
17
  */
17
- export const KNOWN_QUALITY_VALUES = new Set(["generated", "curated", "proposed"]);
18
+ export const KNOWN_QUALITY_VALUES = new Set(["generated", "curated", "enriched", "proposed"]);
18
19
  /** Tracks unknown quality values we've already warned about (one warn per value per process). */
19
20
  const warnedUnknownQualityValues = new Set();
20
21
  /**
@@ -80,20 +81,7 @@ export function loadStashFile(dirPath, options) {
80
81
  }
81
82
  export function writeStashFile(dirPath, stash) {
82
83
  const filePath = stashFilePath(dirPath);
83
- const tmpPath = `${filePath}.tmp.${process.pid}.${Math.random().toString(36).slice(2)}`;
84
- try {
85
- fs.writeFileSync(tmpPath, `${JSON.stringify(stash, null, 2)}\n`, "utf8");
86
- fs.renameSync(tmpPath, filePath);
87
- }
88
- catch (err) {
89
- try {
90
- fs.unlinkSync(tmpPath);
91
- }
92
- catch {
93
- /* ignore cleanup failure */
94
- }
95
- throw err;
96
- }
84
+ writeFileAtomic(filePath, `${JSON.stringify(stash, null, 2)}\n`);
97
85
  }
98
86
  /**
99
87
  * Validate and normalize a raw object into a `StashEntry`.
@@ -199,6 +187,30 @@ export function validateStashEntry(entry) {
199
187
  if (filtered.length > 0)
200
188
  result.sources = filtered;
201
189
  }
190
+ if (typeof e.beliefState === "string" && e.beliefState.trim().length > 0) {
191
+ result.beliefState = e.beliefState.trim();
192
+ }
193
+ if (Array.isArray(e.supersededBy)) {
194
+ const filtered = e.supersededBy
195
+ .filter((s) => typeof s === "string" && s.trim().length > 0)
196
+ .map((s) => s.trim());
197
+ if (filtered.length > 0)
198
+ result.supersededBy = filtered;
199
+ }
200
+ if (Array.isArray(e.contradictedBy)) {
201
+ const filtered = e.contradictedBy
202
+ .filter((s) => typeof s === "string" && s.trim().length > 0)
203
+ .map((s) => s.trim());
204
+ if (filtered.length > 0)
205
+ result.contradictedBy = filtered;
206
+ }
207
+ if (Array.isArray(e.currentBeliefRefs)) {
208
+ const filtered = e.currentBeliefRefs
209
+ .filter((s) => typeof s === "string" && s.trim().length > 0)
210
+ .map((s) => s.trim());
211
+ if (filtered.length > 0)
212
+ result.currentBeliefRefs = filtered;
213
+ }
202
214
  if (typeof e.scope === "object" && e.scope !== null && !Array.isArray(e.scope)) {
203
215
  const scope = normalizeScopeObject(e.scope);
204
216
  if (scope)
@@ -323,6 +335,18 @@ export function applyCuratedFrontmatter(entry, fmData) {
323
335
  const quality = toStringOrUndefined(fmData.quality);
324
336
  if (quality)
325
337
  entry.quality = normalizeQuality(quality);
338
+ const beliefState = toStringOrUndefined(fmData.beliefState);
339
+ if (beliefState)
340
+ entry.beliefState = beliefState;
341
+ const supersededBy = normalizeStringListOrUndefined(fmData.supersededBy);
342
+ if (supersededBy)
343
+ entry.supersededBy = supersededBy;
344
+ const contradictedBy = normalizeStringListOrUndefined(fmData.contradictedBy);
345
+ if (contradictedBy)
346
+ entry.contradictedBy = contradictedBy;
347
+ const currentBeliefRefs = normalizeStringListOrUndefined(fmData.currentBeliefRefs);
348
+ if (currentBeliefRefs)
349
+ entry.currentBeliefRefs = currentBeliefRefs;
326
350
  const intent = normalizeIntent(fmData.intent);
327
351
  if (intent)
328
352
  entry.intent = intent;
@@ -681,7 +705,137 @@ function mergeAliases(existing, generated) {
681
705
  const merged = normalizeTerms([...(existing ?? []), ...generated]);
682
706
  return merged.length > 0 ? merged : undefined;
683
707
  }
708
+ // ── Enrichment Completeness ─────────────────────────────────────────────────
709
+ /**
710
+ * Returns `true` when a stash entry already has enough LLM-quality metadata
711
+ * that calling the LLM would produce no meaningful improvement.
712
+ *
713
+ * An entry is considered complete when ALL of the following hold:
714
+ * - `description` is a non-empty string
715
+ * - `tags` is a non-empty array
716
+ * - `searchHints` is a non-empty array
717
+ *
718
+ * This predicate is used by `enhanceDirsWithLlm` to skip the LLM call for
719
+ * entries that were previously enriched and already carry all three fields.
720
+ * Pass `reEnrich = true` in the caller to bypass this check.
721
+ */
722
+ export function isEnrichmentComplete(entry) {
723
+ const hasDescription = typeof entry.description === "string" && entry.description.trim().length > 0;
724
+ const hasTags = Array.isArray(entry.tags) && entry.tags.length > 0;
725
+ const hasSearchHints = Array.isArray(entry.searchHints) && entry.searchHints.length > 0;
726
+ return hasDescription && hasTags && hasSearchHints;
727
+ }
684
728
  // ── Metadata Generation ─────────────────────────────────────────────────────
729
+ /**
730
+ * Shared pipeline (steps 2-6) for building a single StashEntry from a file.
731
+ *
732
+ * Both `generateMetadata` and `generateMetadataFlat` perform identical work
733
+ * once the initial `entry` object has been seeded with type and canonical name.
734
+ * This helper encapsulates that shared pipeline so the two callers only differ
735
+ * in how they determine the asset type and canonical name (step 1):
736
+ *
737
+ * - `generateMetadata` — explicit `assetType` arg + `deriveCanonicalAssetName`
738
+ * - `generateMetadataFlat` — type from `runMatchers()` + `deriveCanonicalAssetNameFromStashRoot`
739
+ *
740
+ * @param file Absolute path to the file being processed.
741
+ * @param assetType Resolved asset type string (already validated by caller).
742
+ * @param canonicalName Resolved canonical name (already computed by caller).
743
+ * @param dirPath Directory containing the file (used for tag fallback).
744
+ * @param pkgMeta Pre-loaded package.json metadata for this directory (may be null/undefined).
745
+ * @param stashRoot Stash root used for renderer search hints context.
746
+ * @param ctx FileContext for the file (may be pre-built by the caller).
747
+ * @param match Pre-resolved MatchResult when available (from `generateMetadataFlat`).
748
+ * @returns The populated entry, or `{ skip: true, warning: string }` when the
749
+ * renderer throws and the file should be dropped.
750
+ */
751
+ async function buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, stashRoot, ctx, match) {
752
+ const ext = path.extname(file).toLowerCase();
753
+ const baseName = path.basename(file, ext);
754
+ const entry = {
755
+ name: canonicalName,
756
+ type: assetType,
757
+ quality: "generated",
758
+ confidence: 0.55,
759
+ source: "filename",
760
+ };
761
+ // Priority 1: Package.json metadata
762
+ if (pkgMeta) {
763
+ if (pkgMeta.description && !entry.description) {
764
+ entry.description = pkgMeta.description;
765
+ entry.source = "package";
766
+ entry.confidence = 0.8;
767
+ }
768
+ if (pkgMeta.keywords && pkgMeta.keywords.length > 0)
769
+ entry.tags = normalizeTerms(pkgMeta.keywords);
770
+ }
771
+ // Priority 2: Frontmatter (for .md files -- overrides package.json description)
772
+ if (ext === ".md") {
773
+ const content = ctx.content();
774
+ const parsed = parseFrontmatter(content);
775
+ applyCuratedFrontmatter(entry, parsed.data);
776
+ // Extract parameters from frontmatter params: key
777
+ const fmParams = extractFrontmatterParameters(parsed.data);
778
+ if (fmParams)
779
+ entry.parameters = fmParams;
780
+ // Pass wiki-pattern frontmatter through onto the entry
781
+ applyWikiFrontmatter(entry, parsed.data);
782
+ // Extract parameters from template placeholders ($1, $ARGUMENTS, {{named}})
783
+ if (entry.type === "command") {
784
+ const cmdParams = extractCommandParameters(parsed.content);
785
+ if (cmdParams) {
786
+ entry.parameters = mergeParameters(entry.parameters, cmdParams);
787
+ }
788
+ }
789
+ }
790
+ // Extract @param from script files.
791
+ // Vault files (.env) are deliberately excluded — their contents are secrets
792
+ // and must never be parsed for @param or any other metadata that could
793
+ // embed a value into the entry.
794
+ if (ext !== ".md" && assetType !== "vault") {
795
+ const content = ctx.content();
796
+ const scriptParams = extractScriptParameters(file, content);
797
+ if (scriptParams)
798
+ entry.parameters = scriptParams;
799
+ applyCommentMetadata(entry, extractCommentMetadata(file, content));
800
+ }
801
+ // Priority 3: Renderer metadata extraction
802
+ // When no pre-resolved match is available (generateMetadata path), run
803
+ // matchers now so the renderer can extract type-specific metadata.
804
+ const resolvedMatch = match ?? (await runMatchers(ctx));
805
+ if (resolvedMatch) {
806
+ const renderer = await getRenderer(resolvedMatch.renderer);
807
+ if (renderer) {
808
+ const renderCtx = buildRenderContext(ctx, resolvedMatch, [stashRoot]);
809
+ try {
810
+ await applyMetadataContributors(entry, {
811
+ rendererName: renderer.name,
812
+ renderContext: renderCtx,
813
+ });
814
+ }
815
+ catch (error) {
816
+ return {
817
+ skip: true,
818
+ warning: buildMetadataSkipWarning(file, assetType, error),
819
+ };
820
+ }
821
+ }
822
+ }
823
+ // Priority 4: Filename heuristics (fallback)
824
+ if (!entry.description) {
825
+ entry.description = fileNameToDescription(baseName);
826
+ entry.source = "filename";
827
+ entry.confidence = Math.min(entry.confidence ?? 0.55, 0.55);
828
+ }
829
+ if (!entry.tags || entry.tags.length === 0) {
830
+ entry.tags = extractTagsFromPath(file, dirPath);
831
+ }
832
+ entry.tags = normalizeTerms(entry.tags ?? []);
833
+ entry.aliases = mergeAliases(entry.aliases, buildAliases(canonicalName, entry.tags));
834
+ // Search hints are only generated when LLM is configured (via enhanceStashWithLlm)
835
+ // Heuristic search hints are too noisy to be useful for search quality
836
+ entry.filename = path.basename(file);
837
+ return entry;
838
+ }
685
839
  export async function generateMetadata(dirPath, assetType, files, typeRoot = dirPath) {
686
840
  const entries = [];
687
841
  const warnings = [];
@@ -694,84 +848,16 @@ export async function generateMetadata(dirPath, assetType, files, typeRoot = dir
694
848
  if (!isRelevantAssetFile(assetType, fileName))
695
849
  continue;
696
850
  const canonicalName = deriveCanonicalAssetName(assetType, typeRoot, file) ?? baseName;
697
- const entry = {
698
- name: canonicalName,
699
- type: assetType,
700
- quality: "generated",
701
- confidence: 0.55,
702
- source: "filename",
703
- };
704
- // Priority 1: Package.json metadata
705
- if (pkgMeta) {
706
- if (pkgMeta.description && !entry.description) {
707
- entry.description = pkgMeta.description;
708
- entry.source = "package";
709
- entry.confidence = 0.8;
710
- }
711
- if (pkgMeta.keywords && pkgMeta.keywords.length > 0)
712
- entry.tags = normalizeTerms(pkgMeta.keywords);
713
- }
714
- // Priority 2: Frontmatter (for .md files -- overrides package.json description)
715
- if (ext === ".md") {
716
- const content = fs.readFileSync(file, "utf8");
717
- const parsed = parseFrontmatter(content);
718
- applyCuratedFrontmatter(entry, parsed.data);
719
- // Extract parameters from frontmatter params: key
720
- const fmParams = extractFrontmatterParameters(parsed.data);
721
- if (fmParams)
722
- entry.parameters = fmParams;
723
- // Pass wiki-pattern frontmatter through onto the entry
724
- applyWikiFrontmatter(entry, parsed.data);
725
- // Extract parameters from template placeholders ($1, $ARGUMENTS, {{named}})
726
- if (entry.type === "command") {
727
- const cmdParams = extractCommandParameters(parsed.content);
728
- if (cmdParams) {
729
- entry.parameters = mergeParameters(entry.parameters, cmdParams);
730
- }
731
- }
732
- }
733
- // Extract @param from script files.
734
- // Vault files (.env) are deliberately excluded — their contents are secrets
735
- // and must never be parsed for @param or any other metadata that could
736
- // embed a value into the entry.
737
- if (ext !== ".md" && assetType !== "vault") {
738
- const content = fs.readFileSync(file, "utf8");
739
- const scriptParams = extractScriptParameters(file, content);
740
- if (scriptParams)
741
- entry.parameters = scriptParams;
742
- applyCommentMetadata(entry, extractCommentMetadata(file, content));
743
- }
744
- // Priority 3: Type-specific metadata extraction (e.g. TOC for knowledge, comments for scripts)
851
+ // Build file context with typeRoot as the stash root so renderer context
852
+ // and search hints are scoped to the type directory.
745
853
  const fileCtx = buildFileContext(typeRoot, file);
746
- const match = await runMatchers(fileCtx);
747
- if (match) {
748
- const renderer = await getRenderer(match.renderer);
749
- if (renderer?.extractMetadata) {
750
- const renderCtx = buildRenderContext(fileCtx, match, [typeRoot]);
751
- try {
752
- renderer.extractMetadata(entry, renderCtx);
753
- }
754
- catch (error) {
755
- warnings.push(buildMetadataSkipWarning(file, assetType, error));
756
- continue;
757
- }
758
- }
759
- }
760
- // Priority 4: Filename heuristics (fallback)
761
- if (!entry.description) {
762
- entry.description = fileNameToDescription(baseName);
763
- entry.source = "filename";
764
- entry.confidence = Math.min(entry.confidence ?? 0.55, 0.55);
765
- }
766
- if (!entry.tags || entry.tags.length === 0) {
767
- entry.tags = extractTagsFromPath(file, dirPath);
854
+ // Step 1: type is explicit; delegate steps 2-6 to the shared pipeline.
855
+ const result = await buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, typeRoot, fileCtx, null);
856
+ if ("skip" in result) {
857
+ warnings.push(result.warning);
858
+ continue;
768
859
  }
769
- entry.tags = normalizeTerms(entry.tags ?? []);
770
- entry.aliases = mergeAliases(entry.aliases, buildAliases(canonicalName, entry.tags));
771
- // Search hints are only generated when LLM is configured (via enhanceStashWithLlm)
772
- // Heuristic search hints are too noisy to be useful for search quality
773
- entry.filename = path.basename(file);
774
- entries.push(entry);
860
+ entries.push(result);
775
861
  }
776
862
  return warnings.length > 0 ? { entries, warnings } : { entries };
777
863
  }
@@ -789,6 +875,7 @@ export async function generateMetadataFlat(stashRoot, files) {
789
875
  for (const file of files) {
790
876
  if (!shouldIndexStashFile(stashRoot, file))
791
877
  continue;
878
+ // Step 1: determine type and canonical name via the matcher system.
792
879
  const ctx = buildFileContext(stashRoot, file);
793
880
  const match = await runMatchers(ctx);
794
881
  if (!match)
@@ -802,83 +889,20 @@ export async function generateMetadataFlat(stashRoot, files) {
802
889
  const ext = path.extname(file).toLowerCase();
803
890
  const baseName = path.basename(file, ext);
804
891
  const canonicalName = deriveCanonicalAssetNameFromStashRoot(assetType, stashRoot, file) ?? baseName;
805
- const entry = {
806
- name: canonicalName,
807
- type: assetType,
808
- quality: "generated",
809
- confidence: 0.55,
810
- source: "filename",
811
- };
812
- // Package.json metadata
892
+ // Resolve package.json metadata with a per-directory cache.
813
893
  const dirPath = path.dirname(file);
814
894
  if (!pkgMetaCache.has(dirPath)) {
815
895
  pkgMetaCache.set(dirPath, extractPackageMetadata(dirPath));
816
896
  }
817
897
  const pkgMeta = pkgMetaCache.get(dirPath);
818
- if (pkgMeta) {
819
- if (pkgMeta.description && !entry.description) {
820
- entry.description = pkgMeta.description;
821
- entry.source = "package";
822
- entry.confidence = 0.8;
823
- }
824
- if (pkgMeta.keywords?.length)
825
- entry.tags = normalizeTerms(pkgMeta.keywords);
826
- }
827
- // Frontmatter
828
- if (ext === ".md") {
829
- const content = ctx.content();
830
- const parsed = parseFrontmatter(content);
831
- applyCuratedFrontmatter(entry, parsed.data);
832
- // Extract parameters from frontmatter params: key
833
- const fmParams = extractFrontmatterParameters(parsed.data);
834
- if (fmParams)
835
- entry.parameters = fmParams;
836
- // Pass wiki-pattern frontmatter through onto the entry
837
- applyWikiFrontmatter(entry, parsed.data);
838
- // Extract parameters from template placeholders ($1, $ARGUMENTS, {{named}})
839
- if (entry.type === "command") {
840
- const cmdParams = extractCommandParameters(parsed.content);
841
- if (cmdParams) {
842
- entry.parameters = mergeParameters(entry.parameters, cmdParams);
843
- }
844
- }
845
- }
846
- // Extract @param from script files.
847
- // Vault files (.env) are deliberately excluded — their contents are secrets
848
- // and must never be parsed for @param or any other metadata that could
849
- // embed a value into the entry.
850
- if (ext !== ".md" && assetType !== "vault") {
851
- const content = ctx.content();
852
- const scriptParams = extractScriptParameters(file, content);
853
- if (scriptParams)
854
- entry.parameters = scriptParams;
855
- applyCommentMetadata(entry, extractCommentMetadata(file, content));
856
- }
857
- // Renderer metadata extraction
858
- const renderer = await getRenderer(match.renderer);
859
- if (renderer?.extractMetadata) {
860
- const renderCtx = buildRenderContext(ctx, match, [stashRoot]);
861
- try {
862
- renderer.extractMetadata(entry, renderCtx);
863
- }
864
- catch (error) {
865
- warnings.push(buildMetadataSkipWarning(file, assetType, error));
866
- continue;
867
- }
868
- }
869
- // Filename heuristics fallback
870
- if (!entry.description) {
871
- entry.description = fileNameToDescription(baseName);
872
- entry.source = "filename";
873
- entry.confidence = Math.min(entry.confidence ?? 0.55, 0.55);
874
- }
875
- if (!entry.tags || entry.tags.length === 0) {
876
- entry.tags = extractTagsFromPath(file, dirPath);
898
+ // Steps 2-6: delegate to the shared pipeline; pass the pre-resolved match
899
+ // so we don't run matchers a second time.
900
+ const result = await buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, stashRoot, ctx, match);
901
+ if ("skip" in result) {
902
+ warnings.push(result.warning);
903
+ continue;
877
904
  }
878
- entry.tags = normalizeTerms(entry.tags ?? []);
879
- entry.aliases = mergeAliases(entry.aliases, buildAliases(canonicalName, entry.tags));
880
- entry.filename = path.basename(file);
881
- entries.push(entry);
905
+ entries.push(result);
882
906
  }
883
907
  return warnings.length > 0 ? { entries, warnings } : { entries };
884
908
  }
@@ -980,17 +1004,6 @@ export function extractDescriptionFromComments(filePath) {
980
1004
  return hashLines.join(" ");
981
1005
  return null;
982
1006
  }
983
- export function extractFrontmatterDescription(filePath) {
984
- let content;
985
- try {
986
- content = fs.readFileSync(filePath, "utf8");
987
- }
988
- catch {
989
- return null;
990
- }
991
- const parsed = parseFrontmatter(content);
992
- return toStringOrUndefined(parsed.data.description) ?? null;
993
- }
994
1007
  export function extractPackageMetadata(dirPath) {
995
1008
  const pkgPath = path.join(dirPath, "package.json");
996
1009
  if (!fs.existsSync(pkgPath))
@@ -0,0 +1,89 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { parseAssetRef } from "../core/asset-ref";
4
+ import { resolveAssetPathFromName, TYPE_DIRS } from "../core/asset-spec";
5
+ import { isWithin } from "../core/common";
6
+ import { resolveSourcesForOrigin } from "../registry/origin-resolve";
7
+ import { lookup } from "./indexer";
8
+ import { resolveSourceEntries } from "./search-source";
9
+ function normalizeRef(ref) {
10
+ return typeof ref === "string" ? parseAssetRef(ref) : ref;
11
+ }
12
+ function buildDiskCandidates(sourcePath, ref, preserveDirectNameFallback) {
13
+ const typeDir = path.join(sourcePath, TYPE_DIRS[ref.type] ?? `${ref.type}s`);
14
+ const candidates = [
15
+ resolveAssetPathFromName(ref.type, typeDir, ref.name),
16
+ path.join(sourcePath, ref.type, `${ref.name}.md`),
17
+ path.join(sourcePath, ref.type, ref.name),
18
+ ];
19
+ if (preserveDirectNameFallback) {
20
+ candidates.push(path.join(sourcePath, `${ref.name}.md`), path.join(sourcePath, ref.name));
21
+ }
22
+ return candidates;
23
+ }
24
+ function resolveDirectoryEntry(filePath, directoryIndexNames) {
25
+ let stat;
26
+ try {
27
+ stat = fs.statSync(filePath);
28
+ }
29
+ catch {
30
+ return null;
31
+ }
32
+ if (stat.isFile())
33
+ return filePath;
34
+ if (!stat.isDirectory())
35
+ return null;
36
+ for (const indexName of directoryIndexNames) {
37
+ const candidate = path.join(filePath, indexName);
38
+ if (fs.existsSync(candidate) && fs.statSync(candidate).isFile())
39
+ return candidate;
40
+ }
41
+ return null;
42
+ }
43
+ async function resolveViaIndex(ref) {
44
+ try {
45
+ const entry = await lookup(ref);
46
+ return entry?.filePath ?? null;
47
+ }
48
+ catch {
49
+ return null;
50
+ }
51
+ }
52
+ function resolveViaDisk(ref, options) {
53
+ let sources = resolveSourceEntries(options.stashDir);
54
+ if (options.honorOrigin !== false) {
55
+ sources = resolveSourcesForOrigin(ref.origin, sources);
56
+ }
57
+ const directoryIndexNames = options.directoryIndexNames ?? ["SKILL.md"];
58
+ const preserveDirectNameFallback = options.preserveDirectNameFallback ?? true;
59
+ for (const source of sources) {
60
+ if (options.writableDirSet && !options.writableDirSet.has(path.resolve(source.path)))
61
+ continue;
62
+ const candidates = buildDiskCandidates(source.path, ref, preserveDirectNameFallback);
63
+ for (const candidate of candidates) {
64
+ if (!fs.existsSync(candidate))
65
+ continue;
66
+ const resolved = resolveDirectoryEntry(candidate, directoryIndexNames);
67
+ if (!resolved)
68
+ continue;
69
+ const resolvedRoot = fs.realpathSync(source.path);
70
+ const realTarget = fs.realpathSync(resolved);
71
+ if (!isWithin(realTarget, resolvedRoot))
72
+ continue;
73
+ return realTarget;
74
+ }
75
+ }
76
+ return null;
77
+ }
78
+ export async function resolveAssetPath(ref, options = {}) {
79
+ const parsed = normalizeRef(ref);
80
+ const mode = options.mode ?? "index-first";
81
+ if (mode !== "disk-only") {
82
+ const indexed = await resolveViaIndex(parsed);
83
+ if (indexed)
84
+ return indexed;
85
+ if (mode === "index-only")
86
+ return null;
87
+ }
88
+ return resolveViaDisk(parsed, options);
89
+ }