akm-cli 0.7.4 → 0.8.0-rc.3

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 (162) hide show
  1. package/{CHANGELOG.md → .github/CHANGELOG.md} +34 -1
  2. package/.github/LICENSE +374 -0
  3. package/dist/cli/parse-args.js +86 -0
  4. package/dist/cli.js +1223 -650
  5. package/dist/commands/agent-dispatch.js +107 -0
  6. package/dist/commands/agent-support.js +62 -0
  7. package/dist/commands/config-cli.js +68 -84
  8. package/dist/commands/consolidate.js +812 -0
  9. package/dist/commands/curate.js +1 -0
  10. package/dist/commands/distill-promotion-policy.js +658 -0
  11. package/dist/commands/distill.js +224 -39
  12. package/dist/commands/eval-cases.js +40 -0
  13. package/dist/commands/events.js +12 -24
  14. package/dist/commands/graph.js +222 -0
  15. package/dist/commands/health.js +376 -0
  16. package/dist/commands/help/help-accept.md +9 -0
  17. package/dist/commands/help/help-improve.md +53 -0
  18. package/dist/commands/help/help-proposals.md +15 -0
  19. package/dist/commands/help/help-propose.md +17 -0
  20. package/dist/commands/help/help-reject.md +8 -0
  21. package/dist/commands/history.js +3 -30
  22. package/dist/commands/improve.js +1161 -0
  23. package/dist/commands/info.js +2 -2
  24. package/dist/commands/init.js +2 -2
  25. package/dist/commands/install-audit.js +5 -1
  26. package/dist/commands/installed-stashes.js +118 -138
  27. package/dist/commands/knowledge.js +133 -0
  28. package/dist/commands/lint/agent-linter.js +46 -0
  29. package/dist/commands/lint/base-linter.js +291 -0
  30. package/dist/commands/lint/command-linter.js +46 -0
  31. package/dist/commands/lint/default-linter.js +13 -0
  32. package/dist/commands/lint/index.js +145 -0
  33. package/dist/commands/lint/knowledge-linter.js +13 -0
  34. package/dist/commands/lint/memory-linter.js +58 -0
  35. package/dist/commands/lint/registry.js +33 -0
  36. package/dist/commands/lint/skill-linter.js +42 -0
  37. package/dist/commands/lint/task-linter.js +47 -0
  38. package/dist/commands/lint/types.js +1 -0
  39. package/dist/commands/lint/vault-key-rules.js +67 -0
  40. package/dist/commands/lint/workflow-linter.js +53 -0
  41. package/dist/commands/lint.js +1 -0
  42. package/dist/commands/migration-help.js +2 -2
  43. package/dist/commands/proposal.js +8 -7
  44. package/dist/commands/propose.js +106 -43
  45. package/dist/commands/reflect.js +167 -41
  46. package/dist/commands/registry-search.js +2 -2
  47. package/dist/commands/remember.js +55 -1
  48. package/dist/commands/schema-repair.js +130 -0
  49. package/dist/commands/search.js +21 -5
  50. package/dist/commands/show.js +135 -55
  51. package/dist/commands/source-add.js +10 -10
  52. package/dist/commands/source-manage.js +11 -19
  53. package/dist/commands/tasks.js +385 -0
  54. package/dist/commands/url-checker.js +39 -0
  55. package/dist/commands/vault.js +173 -87
  56. package/dist/core/action-contributors.js +25 -0
  57. package/dist/core/asset-ref.js +4 -0
  58. package/dist/core/asset-registry.js +5 -17
  59. package/dist/core/asset-spec.js +11 -1
  60. package/dist/core/common.js +100 -0
  61. package/dist/core/concurrent.js +22 -0
  62. package/dist/core/config.js +240 -127
  63. package/dist/core/events.js +87 -123
  64. package/dist/core/frontmatter.js +0 -6
  65. package/dist/core/markdown.js +17 -0
  66. package/dist/core/memory-improve.js +678 -0
  67. package/dist/core/parse.js +155 -0
  68. package/dist/core/paths.js +101 -3
  69. package/dist/core/proposal-validators.js +61 -0
  70. package/dist/core/proposals.js +49 -38
  71. package/dist/core/state-db.js +731 -0
  72. package/dist/core/time.js +51 -0
  73. package/dist/core/warn.js +59 -1
  74. package/dist/indexer/db-search.js +86 -472
  75. package/dist/indexer/db.js +418 -59
  76. package/dist/indexer/ensure-index.js +133 -0
  77. package/dist/indexer/graph-boost.js +247 -94
  78. package/dist/indexer/graph-db.js +201 -0
  79. package/dist/indexer/graph-dedup.js +99 -0
  80. package/dist/indexer/graph-extraction.js +417 -74
  81. package/dist/indexer/index-context.js +10 -0
  82. package/dist/indexer/indexer.js +480 -298
  83. package/dist/indexer/llm-cache.js +47 -0
  84. package/dist/indexer/matchers.js +124 -160
  85. package/dist/indexer/memory-inference.js +63 -29
  86. package/dist/indexer/metadata-contributors.js +26 -0
  87. package/dist/indexer/metadata.js +196 -197
  88. package/dist/indexer/path-resolver.js +89 -0
  89. package/dist/indexer/ranking-contributors.js +204 -0
  90. package/dist/indexer/ranking.js +74 -0
  91. package/dist/indexer/search-hit-enrichers.js +22 -0
  92. package/dist/indexer/search-source.js +24 -9
  93. package/dist/indexer/semantic-status.js +2 -16
  94. package/dist/indexer/walker.js +25 -0
  95. package/dist/integrations/agent/builders.js +109 -0
  96. package/dist/integrations/agent/config.js +203 -3
  97. package/dist/integrations/agent/index.js +5 -2
  98. package/dist/integrations/agent/model-aliases.js +63 -0
  99. package/dist/integrations/agent/profiles.js +67 -5
  100. package/dist/integrations/agent/prompts.js +114 -29
  101. package/dist/integrations/agent/sdk-runner.js +120 -0
  102. package/dist/integrations/agent/spawn.js +158 -34
  103. package/dist/integrations/lockfile.js +10 -18
  104. package/dist/integrations/session-logs/index.js +65 -0
  105. package/dist/integrations/session-logs/providers/claude-code.js +56 -0
  106. package/dist/integrations/session-logs/providers/opencode.js +52 -0
  107. package/dist/integrations/session-logs/types.js +1 -0
  108. package/dist/llm/call-ai.js +74 -0
  109. package/dist/llm/client.js +63 -86
  110. package/dist/llm/feature-gate.js +27 -16
  111. package/dist/llm/graph-extract.js +297 -64
  112. package/dist/llm/memory-infer.js +52 -71
  113. package/dist/llm/metadata-enhance.js +39 -22
  114. package/dist/llm/prompts/graph-extract-user-prompt.md +12 -0
  115. package/dist/output/cli-hints-full.md +277 -0
  116. package/dist/output/cli-hints-short.md +65 -0
  117. package/dist/output/cli-hints.js +2 -309
  118. package/dist/output/renderers.js +226 -257
  119. package/dist/output/shapes.js +109 -96
  120. package/dist/output/text.js +274 -36
  121. package/dist/registry/providers/skills-sh.js +61 -49
  122. package/dist/registry/providers/static-index.js +44 -48
  123. package/dist/registry/resolve.js +8 -16
  124. package/dist/setup/setup.js +510 -11
  125. package/dist/sources/provider-factory.js +2 -1
  126. package/dist/sources/providers/filesystem.js +16 -23
  127. package/dist/sources/providers/git.js +45 -4
  128. package/dist/sources/providers/website.js +15 -22
  129. package/dist/sources/website-ingest.js +4 -0
  130. package/dist/tasks/backends/cron.js +200 -0
  131. package/dist/tasks/backends/exec-utils.js +25 -0
  132. package/dist/tasks/backends/index.js +32 -0
  133. package/dist/tasks/backends/launchd-template.xml +19 -0
  134. package/dist/tasks/backends/launchd.js +184 -0
  135. package/dist/tasks/backends/schtasks-template.xml +29 -0
  136. package/dist/tasks/backends/schtasks.js +212 -0
  137. package/dist/tasks/parser.js +198 -0
  138. package/dist/tasks/resolveAkmBin.js +84 -0
  139. package/dist/tasks/runner.js +432 -0
  140. package/dist/tasks/schedule.js +208 -0
  141. package/dist/tasks/schema.js +13 -0
  142. package/dist/tasks/validator.js +59 -0
  143. package/dist/wiki/index-template.md +12 -0
  144. package/dist/wiki/ingest-workflow-template.md +54 -0
  145. package/dist/wiki/log-template.md +8 -0
  146. package/dist/wiki/schema-template.md +61 -0
  147. package/dist/wiki/wiki-templates.js +12 -0
  148. package/dist/wiki/wiki.js +10 -61
  149. package/dist/workflows/authoring.js +5 -25
  150. package/dist/workflows/db.js +9 -0
  151. package/dist/workflows/renderer.js +8 -3
  152. package/dist/workflows/runs.js +73 -88
  153. package/dist/workflows/scope-key.js +76 -0
  154. package/dist/workflows/validator.js +1 -1
  155. package/dist/workflows/workflow-template.md +24 -0
  156. package/docs/README.md +5 -2
  157. package/docs/migration/release-notes/0.7.0.md +1 -1
  158. package/docs/migration/release-notes/0.7.4.md +1 -1
  159. package/docs/migration/release-notes/0.7.5.md +20 -0
  160. package/docs/migration/release-notes/0.8.0.md +43 -0
  161. package/package.json +4 -3
  162. 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";
5
- import { parseFrontmatter, toStringOrUndefined } from "../core/frontmatter";
4
+ import { asNonEmptyString, isAssetType, writeFileAtomic } from "../core/common";
5
+ import { parseFrontmatter } 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`.
@@ -185,20 +173,24 @@ export function validateStashEntry(entry) {
185
173
  if (typeof e.pageKind === "string" && e.pageKind.trim().length > 0) {
186
174
  result.pageKind = e.pageKind.trim();
187
175
  }
188
- if (Array.isArray(e.xrefs)) {
189
- const filtered = e.xrefs
190
- .filter((x) => typeof x === "string" && x.trim().length > 0)
191
- .map((x) => x.trim());
192
- if (filtered.length > 0)
193
- result.xrefs = filtered;
194
- }
195
- if (Array.isArray(e.sources)) {
196
- const filtered = e.sources
197
- .filter((s) => typeof s === "string" && s.trim().length > 0)
198
- .map((s) => s.trim());
199
- if (filtered.length > 0)
200
- result.sources = filtered;
176
+ const xrefs = normalizeNonEmptyStringList(e.xrefs);
177
+ if (xrefs)
178
+ result.xrefs = xrefs;
179
+ const sources = normalizeNonEmptyStringList(e.sources);
180
+ if (sources)
181
+ result.sources = sources;
182
+ if (typeof e.beliefState === "string" && e.beliefState.trim().length > 0) {
183
+ result.beliefState = e.beliefState.trim();
201
184
  }
185
+ const supersededBy = normalizeNonEmptyStringList(e.supersededBy);
186
+ if (supersededBy)
187
+ result.supersededBy = supersededBy;
188
+ const contradictedBy = normalizeNonEmptyStringList(e.contradictedBy);
189
+ if (contradictedBy)
190
+ result.contradictedBy = contradictedBy;
191
+ const currentBeliefRefs = normalizeNonEmptyStringList(e.currentBeliefRefs);
192
+ if (currentBeliefRefs)
193
+ result.currentBeliefRefs = currentBeliefRefs;
202
194
  if (typeof e.scope === "object" && e.scope !== null && !Array.isArray(e.scope)) {
203
195
  const scope = normalizeScopeObject(e.scope);
204
196
  if (scope)
@@ -275,9 +267,9 @@ function normalizeIntent(value) {
275
267
  return undefined;
276
268
  const raw = value;
277
269
  const intent = {};
278
- const when = toStringOrUndefined(raw.when);
279
- const input = toStringOrUndefined(raw.input);
280
- const output = toStringOrUndefined(raw.output);
270
+ const when = asNonEmptyString(raw.when);
271
+ const input = asNonEmptyString(raw.input);
272
+ const output = asNonEmptyString(raw.output);
281
273
  if (when)
282
274
  intent.when = when;
283
275
  if (input)
@@ -290,7 +282,7 @@ function normalizeStringListOrUndefined(value) {
290
282
  return normalizeNonEmptyStringList(value);
291
283
  }
292
284
  export function applyCuratedFrontmatter(entry, fmData) {
293
- const description = toStringOrUndefined(fmData.description);
285
+ const description = asNonEmptyString(fmData.description);
294
286
  if (description) {
295
287
  entry.description = description;
296
288
  entry.source = "frontmatter";
@@ -311,18 +303,30 @@ export function applyCuratedFrontmatter(entry, fmData) {
311
303
  const examples = normalizeStringListOrUndefined(fmData.examples);
312
304
  if (examples)
313
305
  entry.examples = examples;
314
- const run = toStringOrUndefined(fmData.run);
306
+ const run = asNonEmptyString(fmData.run);
315
307
  if (run)
316
308
  entry.run = run;
317
- const setup = toStringOrUndefined(fmData.setup);
309
+ const setup = asNonEmptyString(fmData.setup);
318
310
  if (setup)
319
311
  entry.setup = setup;
320
- const cwd = toStringOrUndefined(fmData.cwd);
312
+ const cwd = asNonEmptyString(fmData.cwd);
321
313
  if (cwd)
322
314
  entry.cwd = cwd;
323
- const quality = toStringOrUndefined(fmData.quality);
315
+ const quality = asNonEmptyString(fmData.quality);
324
316
  if (quality)
325
317
  entry.quality = normalizeQuality(quality);
318
+ const beliefState = asNonEmptyString(fmData.beliefState);
319
+ if (beliefState)
320
+ entry.beliefState = beliefState;
321
+ const supersededBy = normalizeStringListOrUndefined(fmData.supersededBy);
322
+ if (supersededBy)
323
+ entry.supersededBy = supersededBy;
324
+ const contradictedBy = normalizeStringListOrUndefined(fmData.contradictedBy);
325
+ if (contradictedBy)
326
+ entry.contradictedBy = contradictedBy;
327
+ const currentBeliefRefs = normalizeStringListOrUndefined(fmData.currentBeliefRefs);
328
+ if (currentBeliefRefs)
329
+ entry.currentBeliefRefs = currentBeliefRefs;
326
330
  const intent = normalizeIntent(fmData.intent);
327
331
  if (intent)
328
332
  entry.intent = intent;
@@ -416,6 +420,12 @@ export function shouldIndexStashFile(stashRoot, file, options) {
416
420
  const segments = relPath.split(/[\\/]+/).filter(Boolean);
417
421
  if (segments.length === 0)
418
422
  return true;
423
+ // Skip vault .env files that have a sibling .sensitive marker file.
424
+ if (segments[0] === "vaults" && (file.endsWith(".env") || path.basename(file) === ".env")) {
425
+ const markerPath = file.replace(/\.env$/, ".sensitive");
426
+ if (fs.existsSync(markerPath))
427
+ return false;
428
+ }
419
429
  if (options?.treatStashRootAsWikiRoot) {
420
430
  return !(segments.length === 1 && WIKI_INFRA_FILES.has(segments[0]));
421
431
  }
@@ -681,7 +691,137 @@ function mergeAliases(existing, generated) {
681
691
  const merged = normalizeTerms([...(existing ?? []), ...generated]);
682
692
  return merged.length > 0 ? merged : undefined;
683
693
  }
694
+ // ── Enrichment Completeness ─────────────────────────────────────────────────
695
+ /**
696
+ * Returns `true` when a stash entry already has enough LLM-quality metadata
697
+ * that calling the LLM would produce no meaningful improvement.
698
+ *
699
+ * An entry is considered complete when ALL of the following hold:
700
+ * - `description` is a non-empty string
701
+ * - `tags` is a non-empty array
702
+ * - `searchHints` is a non-empty array
703
+ *
704
+ * This predicate is used by `enhanceDirsWithLlm` to skip the LLM call for
705
+ * entries that were previously enriched and already carry all three fields.
706
+ * Pass `reEnrich = true` in the caller to bypass this check.
707
+ */
708
+ export function isEnrichmentComplete(entry) {
709
+ const hasDescription = typeof entry.description === "string" && entry.description.trim().length > 0;
710
+ const hasTags = Array.isArray(entry.tags) && entry.tags.length > 0;
711
+ const hasSearchHints = Array.isArray(entry.searchHints) && entry.searchHints.length > 0;
712
+ return hasDescription && hasTags && hasSearchHints;
713
+ }
684
714
  // ── Metadata Generation ─────────────────────────────────────────────────────
715
+ /**
716
+ * Shared pipeline (steps 2-6) for building a single StashEntry from a file.
717
+ *
718
+ * Both `generateMetadata` and `generateMetadataFlat` perform identical work
719
+ * once the initial `entry` object has been seeded with type and canonical name.
720
+ * This helper encapsulates that shared pipeline so the two callers only differ
721
+ * in how they determine the asset type and canonical name (step 1):
722
+ *
723
+ * - `generateMetadata` — explicit `assetType` arg + `deriveCanonicalAssetName`
724
+ * - `generateMetadataFlat` — type from `runMatchers()` + `deriveCanonicalAssetNameFromStashRoot`
725
+ *
726
+ * @param file Absolute path to the file being processed.
727
+ * @param assetType Resolved asset type string (already validated by caller).
728
+ * @param canonicalName Resolved canonical name (already computed by caller).
729
+ * @param dirPath Directory containing the file (used for tag fallback).
730
+ * @param pkgMeta Pre-loaded package.json metadata for this directory (may be null/undefined).
731
+ * @param stashRoot Stash root used for renderer search hints context.
732
+ * @param ctx FileContext for the file (may be pre-built by the caller).
733
+ * @param match Pre-resolved MatchResult when available (from `generateMetadataFlat`).
734
+ * @returns The populated entry, or `{ skip: true, warning: string }` when the
735
+ * renderer throws and the file should be dropped.
736
+ */
737
+ async function buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, stashRoot, ctx, match) {
738
+ const ext = path.extname(file).toLowerCase();
739
+ const baseName = path.basename(file, ext);
740
+ const entry = {
741
+ name: canonicalName,
742
+ type: assetType,
743
+ quality: "generated",
744
+ confidence: 0.55,
745
+ source: "filename",
746
+ };
747
+ // Priority 1: Package.json metadata
748
+ if (pkgMeta) {
749
+ if (pkgMeta.description && !entry.description) {
750
+ entry.description = pkgMeta.description;
751
+ entry.source = "package";
752
+ entry.confidence = 0.8;
753
+ }
754
+ if (pkgMeta.keywords && pkgMeta.keywords.length > 0)
755
+ entry.tags = normalizeTerms(pkgMeta.keywords);
756
+ }
757
+ // Priority 2: Frontmatter (for .md files -- overrides package.json description)
758
+ if (ext === ".md") {
759
+ const content = ctx.content();
760
+ const parsed = parseFrontmatter(content);
761
+ applyCuratedFrontmatter(entry, parsed.data);
762
+ // Extract parameters from frontmatter params: key
763
+ const fmParams = extractFrontmatterParameters(parsed.data);
764
+ if (fmParams)
765
+ entry.parameters = fmParams;
766
+ // Pass wiki-pattern frontmatter through onto the entry
767
+ applyWikiFrontmatter(entry, parsed.data);
768
+ // Extract parameters from template placeholders ($1, $ARGUMENTS, {{named}})
769
+ if (entry.type === "command") {
770
+ const cmdParams = extractCommandParameters(parsed.content);
771
+ if (cmdParams) {
772
+ entry.parameters = mergeParameters(entry.parameters, cmdParams);
773
+ }
774
+ }
775
+ }
776
+ // Extract @param from script files.
777
+ // Vault files (.env) are deliberately excluded — their contents are secrets
778
+ // and must never be parsed for @param or any other metadata that could
779
+ // embed a value into the entry.
780
+ if (ext !== ".md" && assetType !== "vault") {
781
+ const content = ctx.content();
782
+ const scriptParams = extractScriptParameters(file, content);
783
+ if (scriptParams)
784
+ entry.parameters = scriptParams;
785
+ applyCommentMetadata(entry, extractCommentMetadata(file, content));
786
+ }
787
+ // Priority 3: Renderer metadata extraction
788
+ // When no pre-resolved match is available (generateMetadata path), run
789
+ // matchers now so the renderer can extract type-specific metadata.
790
+ const resolvedMatch = match ?? (await runMatchers(ctx));
791
+ if (resolvedMatch) {
792
+ const renderer = await getRenderer(resolvedMatch.renderer);
793
+ if (renderer) {
794
+ const renderCtx = buildRenderContext(ctx, resolvedMatch, [stashRoot]);
795
+ try {
796
+ await applyMetadataContributors(entry, {
797
+ rendererName: renderer.name,
798
+ renderContext: renderCtx,
799
+ });
800
+ }
801
+ catch (error) {
802
+ return {
803
+ skip: true,
804
+ warning: buildMetadataSkipWarning(file, assetType, error),
805
+ };
806
+ }
807
+ }
808
+ }
809
+ // Priority 4: Filename heuristics (fallback)
810
+ if (!entry.description) {
811
+ entry.description = fileNameToDescription(baseName);
812
+ entry.source = "filename";
813
+ entry.confidence = Math.min(entry.confidence ?? 0.55, 0.55);
814
+ }
815
+ if (!entry.tags || entry.tags.length === 0) {
816
+ entry.tags = extractTagsFromPath(file, dirPath);
817
+ }
818
+ entry.tags = normalizeTerms(entry.tags ?? []);
819
+ entry.aliases = mergeAliases(entry.aliases, buildAliases(canonicalName, entry.tags));
820
+ // Search hints are only generated when LLM is configured (via enhanceStashWithLlm)
821
+ // Heuristic search hints are too noisy to be useful for search quality
822
+ entry.filename = path.basename(file);
823
+ return entry;
824
+ }
685
825
  export async function generateMetadata(dirPath, assetType, files, typeRoot = dirPath) {
686
826
  const entries = [];
687
827
  const warnings = [];
@@ -694,84 +834,16 @@ export async function generateMetadata(dirPath, assetType, files, typeRoot = dir
694
834
  if (!isRelevantAssetFile(assetType, fileName))
695
835
  continue;
696
836
  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)
837
+ // Build file context with typeRoot as the stash root so renderer context
838
+ // and search hints are scoped to the type directory.
745
839
  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);
840
+ // Step 1: type is explicit; delegate steps 2-6 to the shared pipeline.
841
+ const result = await buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, typeRoot, fileCtx, null);
842
+ if ("skip" in result) {
843
+ warnings.push(result.warning);
844
+ continue;
768
845
  }
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);
846
+ entries.push(result);
775
847
  }
776
848
  return warnings.length > 0 ? { entries, warnings } : { entries };
777
849
  }
@@ -789,6 +861,7 @@ export async function generateMetadataFlat(stashRoot, files) {
789
861
  for (const file of files) {
790
862
  if (!shouldIndexStashFile(stashRoot, file))
791
863
  continue;
864
+ // Step 1: determine type and canonical name via the matcher system.
792
865
  const ctx = buildFileContext(stashRoot, file);
793
866
  const match = await runMatchers(ctx);
794
867
  if (!match)
@@ -802,83 +875,20 @@ export async function generateMetadataFlat(stashRoot, files) {
802
875
  const ext = path.extname(file).toLowerCase();
803
876
  const baseName = path.basename(file, ext);
804
877
  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
878
+ // Resolve package.json metadata with a per-directory cache.
813
879
  const dirPath = path.dirname(file);
814
880
  if (!pkgMetaCache.has(dirPath)) {
815
881
  pkgMetaCache.set(dirPath, extractPackageMetadata(dirPath));
816
882
  }
817
883
  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);
884
+ // Steps 2-6: delegate to the shared pipeline; pass the pre-resolved match
885
+ // so we don't run matchers a second time.
886
+ const result = await buildEntryFromFile(file, assetType, canonicalName, dirPath, pkgMeta, stashRoot, ctx, match);
887
+ if ("skip" in result) {
888
+ warnings.push(result.warning);
889
+ continue;
877
890
  }
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);
891
+ entries.push(result);
882
892
  }
883
893
  return warnings.length > 0 ? { entries, warnings } : { entries };
884
894
  }
@@ -980,17 +990,6 @@ export function extractDescriptionFromComments(filePath) {
980
990
  return hashLines.join(" ");
981
991
  return null;
982
992
  }
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
993
  export function extractPackageMetadata(dirPath) {
995
994
  const pkgPath = path.join(dirPath, "package.json");
996
995
  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
+ }