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.
- package/CHANGELOG.md +600 -0
- package/dist/assets/prompts/consolidate-system.md +23 -0
- package/dist/assets/prompts/contradiction-judge.md +33 -0
- package/dist/assets/prompts/distill-knowledge-system.md +22 -0
- package/dist/assets/prompts/distill-lesson-system.md +36 -0
- package/dist/assets/prompts/extract-session.md +5 -1
- package/dist/assets/prompts/graph-extract-system.md +1 -0
- package/dist/assets/prompts/memory-infer-system.md +1 -0
- package/dist/assets/prompts/memory-infer-user.md +5 -0
- package/dist/assets/prompts/metadata-enhance-system.md +1 -0
- package/dist/assets/prompts/procedural-system.md +44 -0
- package/dist/assets/prompts/recombine-system.md +40 -0
- package/dist/assets/prompts/staleness-detect-system.md +6 -0
- package/dist/assets/prompts/validate-summary-judge.md +1 -0
- package/dist/assets/templates/html/health.html +281 -111
- package/dist/cli.js +14 -3
- package/dist/commands/agent/contribute-cli.js +16 -3
- package/dist/commands/feedback-cli.js +15 -6
- package/dist/commands/graph/graph.js +75 -71
- package/dist/commands/health/checks.js +48 -0
- package/dist/commands/health/html-report.js +422 -80
- package/dist/commands/health.js +381 -9
- package/dist/commands/improve/calibration.js +161 -0
- package/dist/commands/improve/consolidate.js +634 -111
- package/dist/commands/improve/dedup.js +482 -0
- package/dist/commands/improve/distill.js +145 -69
- package/dist/commands/improve/encoding-salience.js +205 -0
- package/dist/commands/improve/extract-cli.js +115 -1
- package/dist/commands/improve/extract-prompt.js +33 -2
- package/dist/commands/improve/extract-watch.js +140 -0
- package/dist/commands/improve/extract.js +244 -35
- package/dist/commands/improve/feedback-valence.js +54 -0
- package/dist/commands/improve/homeostatic.js +467 -0
- package/dist/commands/improve/improve-auto-accept.js +113 -6
- package/dist/commands/improve/improve-profiles.js +12 -0
- package/dist/commands/improve/improve.js +1974 -614
- package/dist/commands/improve/memory/memory-contradiction-detect.js +23 -28
- package/dist/commands/improve/outcome-loop.js +256 -0
- package/dist/commands/improve/proactive-maintenance.js +87 -0
- package/dist/commands/improve/procedural.js +409 -0
- package/dist/commands/improve/recombine.js +528 -0
- package/dist/commands/improve/reflect.js +26 -1
- package/dist/commands/improve/related-sessions.js +120 -0
- package/dist/commands/improve/salience.js +386 -0
- package/dist/commands/improve/triage.js +95 -0
- package/dist/commands/lint/agent-linter.js +19 -24
- package/dist/commands/lint/base-linter.js +173 -60
- package/dist/commands/lint/command-linter.js +19 -24
- package/dist/commands/lint/env-key-rules.js +34 -1
- package/dist/commands/lint/fact-linter.js +39 -0
- package/dist/commands/lint/index.js +31 -13
- package/dist/commands/lint/memory-linter.js +1 -1
- package/dist/commands/lint/registry.js +7 -2
- package/dist/commands/lint/task-linter.js +3 -3
- package/dist/commands/lint/workflow-linter.js +26 -1
- package/dist/commands/proposal/proposal.js +5 -0
- package/dist/commands/proposal/validators/proposals.js +71 -54
- package/dist/commands/read/curate.js +344 -80
- package/dist/commands/read/search-cli.js +7 -0
- package/dist/commands/read/search.js +1 -0
- package/dist/commands/read/show.js +67 -2
- package/dist/commands/sources/installed-stashes.js +5 -1
- package/dist/commands/sources/stash-cli.js +10 -2
- package/dist/core/asset/asset-registry.js +2 -0
- package/dist/core/asset/asset-spec.js +14 -0
- package/dist/core/asset/frontmatter.js +166 -167
- package/dist/core/asset/markdown.js +8 -0
- package/dist/core/config/config-schema.js +259 -2
- package/dist/core/config/config.js +2 -2
- package/dist/core/logs-db.js +4 -3
- package/dist/core/paths.js +3 -0
- package/dist/core/state-db.js +649 -30
- package/dist/indexer/db/db.js +364 -38
- package/dist/indexer/db/graph-db.js +129 -86
- package/dist/indexer/ensure-index.js +152 -17
- package/dist/indexer/graph/graph-boost.js +51 -41
- package/dist/indexer/graph/graph-extraction.js +203 -3
- package/dist/indexer/index-writer-lock.js +99 -0
- package/dist/indexer/indexer.js +114 -111
- package/dist/indexer/passes/memory-inference.js +10 -3
- package/dist/indexer/passes/staleness-detect.js +2 -5
- package/dist/indexer/search/db-search.js +15 -4
- package/dist/indexer/search/ranking-contributors.js +22 -0
- package/dist/indexer/search/ranking.js +4 -0
- package/dist/indexer/walk/matchers.js +9 -0
- package/dist/integrations/agent/prompts.js +1 -0
- package/dist/integrations/harnesses/claude/session-log.js +11 -1
- package/dist/integrations/harnesses/opencode/session-log.js +9 -0
- package/dist/integrations/session-logs/index.js +16 -0
- package/dist/llm/client.js +23 -4
- package/dist/llm/embedder.js +27 -3
- package/dist/llm/embedders/local.js +66 -2
- package/dist/llm/graph-extract.js +2 -1
- package/dist/llm/memory-infer.js +4 -8
- package/dist/llm/metadata-enhance.js +9 -1
- package/dist/output/renderers.js +73 -1
- package/dist/output/shapes/curate.js +14 -2
- package/dist/output/text/helpers.js +9 -0
- package/dist/runtime.js +25 -1
- package/dist/scripts/migrate-storage.js +1242 -594
- package/dist/scripts/migrations/import-fs-improve-runs-to-db.js +473 -270
- package/dist/sources/providers/tar-utils.js +16 -8
- package/dist/storage/sqlite-pragmas.js +146 -0
- package/dist/workflows/db.js +3 -4
- package/dist/workflows/validate-summary.js +2 -7
- package/docs/data-and-telemetry.md +1 -0
- package/package.json +9 -6
|
@@ -8,16 +8,23 @@
|
|
|
8
8
|
* high-signal set of stash + registry hits and enrich each with the data
|
|
9
9
|
* needed to act (ref, run, parameters, follow-up command).
|
|
10
10
|
*
|
|
11
|
-
* The exported `akmCurate()` API is the single entry point. Internal
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* `
|
|
11
|
+
* The exported `akmCurate()` API is the single entry point. Internal helpers
|
|
12
|
+
* stay private. Tests can drive the public API or call the smaller pure
|
|
13
|
+
* helpers (`curateSearchResults`, `deriveCurateFallbackQueries`,
|
|
14
|
+
* `mergeCurateSearchResponses`) by importing them directly.
|
|
15
15
|
*/
|
|
16
|
+
import fs from "node:fs";
|
|
17
|
+
import { parseAssetRef } from "../../core/asset/asset-ref.js";
|
|
18
|
+
import { parseFrontmatter } from "../../core/asset/frontmatter.js";
|
|
19
|
+
import { getIndexPassConfig, loadConfig } from "../../core/config/config.js";
|
|
16
20
|
import { rethrowIfTestIsolationError, UsageError } from "../../core/errors.js";
|
|
17
21
|
import { appendEvent } from "../../core/events.js";
|
|
18
|
-
import { closeDatabase, openExistingDatabase } from "../../indexer/db/db.js";
|
|
22
|
+
import { closeDatabase, computeBodyHash, openExistingDatabase } from "../../indexer/db/db.js";
|
|
23
|
+
import { enqueueGraphExtraction, hasGraphData } from "../../indexer/db/graph-db.js";
|
|
24
|
+
import { findSourceForPath, resolveSourceEntries } from "../../indexer/search/search-source.js";
|
|
19
25
|
import { insertUsageEvent } from "../../indexer/usage/usage-events.js";
|
|
20
26
|
import { truncateDescription } from "../../output/shapes.js";
|
|
27
|
+
import { withIndexDb } from "../../storage/repositories/index-db.js";
|
|
21
28
|
import { akmSearch, parseSearchSource } from "./search.js";
|
|
22
29
|
import { akmShowUnified } from "./show.js";
|
|
23
30
|
const CURATE_FALLBACK_FILTER_WORDS = new Set([
|
|
@@ -34,13 +41,19 @@ const CURATE_FALLBACK_FILTER_WORDS = new Set([
|
|
|
34
41
|
"to",
|
|
35
42
|
"with",
|
|
36
43
|
]);
|
|
37
|
-
const
|
|
38
|
-
const CURATED_TYPE_FALLBACK_INDEX = new Map(CURATED_TYPE_FALLBACK_ORDER.map((type, index) => [type, index]));
|
|
44
|
+
const CURATE_SHORT_FALLBACK_TOKENS = new Set(["ai", "ci", "cd", "go", "js", "ts"]);
|
|
39
45
|
const MIN_CURATE_FALLBACK_TOKEN_LENGTH = 3;
|
|
40
46
|
const MAX_CURATE_FALLBACK_KEYWORDS = 6;
|
|
41
47
|
export const CURATE_SEARCH_LIMIT_MULTIPLIER = 4;
|
|
42
48
|
export const MIN_CURATE_SEARCH_LIMIT = 12;
|
|
43
49
|
const DEFAULT_CURATE_LIMIT = 4;
|
|
50
|
+
const CURATE_CLOSE_SCORE_BAND = 0.12;
|
|
51
|
+
const CURATE_TAIL_SCORE_FLOOR = 0.35;
|
|
52
|
+
const CURATE_RELATIVE_SCORE_FLOOR = 0.7;
|
|
53
|
+
const CURATE_FALLBACK_TOP_SCORE_THRESHOLD = 0.8;
|
|
54
|
+
const CURATE_FALLBACK_STRONG_SCORE_FLOOR = 0.35;
|
|
55
|
+
const MAX_CURATE_SUPPORT_REFS = 2;
|
|
56
|
+
const CURATE_REFERENCE_QUERY_RE = /\b(?:reference|docs?|guide|how|explain|learn|readme|why)\b/;
|
|
44
57
|
/**
|
|
45
58
|
* Fire-and-forget: log a curate event to the usage_events table and events.jsonl.
|
|
46
59
|
* Never blocks the caller; errors are silently ignored.
|
|
@@ -63,6 +76,16 @@ function logCurateEvent(query, result) {
|
|
|
63
76
|
}),
|
|
64
77
|
source: "user",
|
|
65
78
|
});
|
|
79
|
+
for (const item of result.items) {
|
|
80
|
+
if (!("ref" in item) || typeof item.ref !== "string")
|
|
81
|
+
continue;
|
|
82
|
+
insertUsageEvent(db, {
|
|
83
|
+
event_type: "curate",
|
|
84
|
+
query,
|
|
85
|
+
entry_ref: item.ref,
|
|
86
|
+
source: "user",
|
|
87
|
+
});
|
|
88
|
+
}
|
|
66
89
|
}
|
|
67
90
|
finally {
|
|
68
91
|
closeDatabase(db);
|
|
@@ -70,13 +93,8 @@ function logCurateEvent(query, result) {
|
|
|
70
93
|
}
|
|
71
94
|
catch (err) {
|
|
72
95
|
rethrowIfTestIsolationError(err);
|
|
73
|
-
/* ignore logging failures */
|
|
74
96
|
}
|
|
75
97
|
}
|
|
76
|
-
/**
|
|
77
|
-
* Public curate entry point. Performs the search itself when
|
|
78
|
-
* `options.searchResponse` is not supplied.
|
|
79
|
-
*/
|
|
80
98
|
export async function akmCurate(options) {
|
|
81
99
|
const trimmedQuery = options.query.trim();
|
|
82
100
|
if (!trimmedQuery) {
|
|
@@ -88,8 +106,6 @@ export async function akmCurate(options) {
|
|
|
88
106
|
(await searchForCuration({
|
|
89
107
|
query: options.query,
|
|
90
108
|
type: options.type,
|
|
91
|
-
// Search deeper than the final curated count so we can pick one strong
|
|
92
|
-
// match per type and still have room for fallback retries.
|
|
93
109
|
limit: Math.max(limit * CURATE_SEARCH_LIMIT_MULTIPLIER, MIN_CURATE_SEARCH_LIMIT),
|
|
94
110
|
source,
|
|
95
111
|
}));
|
|
@@ -101,23 +117,22 @@ export async function curateSearchResults(query, result, limit, selectedType) {
|
|
|
101
117
|
const stashHits = result.hits.filter((hit) => hit.type !== "registry");
|
|
102
118
|
const registryHits = result.registryHits ?? [];
|
|
103
119
|
let selectedStashHits;
|
|
120
|
+
let supportRefsByRef = new Map();
|
|
104
121
|
if (selectedType && selectedType !== "any") {
|
|
105
122
|
selectedStashHits = stashHits.slice(0, limit);
|
|
106
123
|
}
|
|
107
124
|
else {
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
const orderedTypes = orderCuratedTypes(query, Array.from(bestByType.keys()));
|
|
114
|
-
selectedStashHits = orderedTypes
|
|
115
|
-
.map((type) => bestByType.get(type))
|
|
116
|
-
.filter((hit) => Boolean(hit));
|
|
125
|
+
const selected = selectCuratedStashHits(query, stashHits, limit);
|
|
126
|
+
const preferred = preferBroadRootRepresentative(query, selected.selected, stashHits, selected.supportRefsByRef);
|
|
127
|
+
selectedStashHits = preferred.selected;
|
|
128
|
+
supportRefsByRef = preferred.supportRefsByRef;
|
|
117
129
|
}
|
|
118
130
|
const selectedRegistryHits = selectedStashHits.length >= limit ? [] : registryHits.slice(0, Math.min(2, limit - selectedStashHits.length));
|
|
131
|
+
const selectedRefs = new Set(selectedStashHits.map((hit) => hit.ref));
|
|
119
132
|
const items = [
|
|
120
|
-
...(await Promise.all(selectedStashHits
|
|
133
|
+
...(await Promise.all(selectedStashHits
|
|
134
|
+
.slice(0, limit)
|
|
135
|
+
.map((hit) => enrichCuratedStashHit(query, hit, supportRefsByRef.get(hit.ref) ?? [], selectedRefs)))),
|
|
121
136
|
...selectedRegistryHits.map((hit) => buildCuratedRegistryItem(query, hit)),
|
|
122
137
|
].slice(0, limit);
|
|
123
138
|
return {
|
|
@@ -128,37 +143,7 @@ export async function curateSearchResults(query, result, limit, selectedType) {
|
|
|
128
143
|
...(result.tip ? { tip: result.tip } : {}),
|
|
129
144
|
};
|
|
130
145
|
}
|
|
131
|
-
|
|
132
|
-
const lower = query.toLowerCase();
|
|
133
|
-
const boosts = new Map();
|
|
134
|
-
const addBoost = (type, amount) => boosts.set(type, (boosts.get(type) ?? 0) + amount);
|
|
135
|
-
if (/(run|script|bash|shell|cli|execute|automation|deploy|build|test|lint)/.test(lower)) {
|
|
136
|
-
addBoost("script", 6);
|
|
137
|
-
addBoost("command", 4);
|
|
138
|
-
}
|
|
139
|
-
if (/(guide|docs?|readme|reference|how|explain|learn|why)/.test(lower)) {
|
|
140
|
-
addBoost("knowledge", 6);
|
|
141
|
-
addBoost("skill", 4);
|
|
142
|
-
}
|
|
143
|
-
if (/(agent|assistant|planner|review|analy[sz]e|architect|prompt)/.test(lower)) {
|
|
144
|
-
addBoost("agent", 6);
|
|
145
|
-
addBoost("skill", 3);
|
|
146
|
-
}
|
|
147
|
-
if (/(config|template|release|generate|command)/.test(lower)) {
|
|
148
|
-
addBoost("command", 5);
|
|
149
|
-
}
|
|
150
|
-
if (/(memory|context|recall|remember)/.test(lower)) {
|
|
151
|
-
addBoost("memory", 6);
|
|
152
|
-
}
|
|
153
|
-
return [...types].sort((a, b) => {
|
|
154
|
-
const boostDiff = (boosts.get(b) ?? 0) - (boosts.get(a) ?? 0);
|
|
155
|
-
if (boostDiff !== 0)
|
|
156
|
-
return boostDiff;
|
|
157
|
-
return ((CURATED_TYPE_FALLBACK_INDEX.get(a) ?? Number.MAX_SAFE_INTEGER) -
|
|
158
|
-
(CURATED_TYPE_FALLBACK_INDEX.get(b) ?? Number.MAX_SAFE_INTEGER));
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
async function enrichCuratedStashHit(query, hit) {
|
|
146
|
+
async function enrichCuratedStashHit(query, hit, supportRefs, selectedRefs) {
|
|
162
147
|
let shown;
|
|
163
148
|
try {
|
|
164
149
|
shown = await akmShowUnified({ ref: hit.ref });
|
|
@@ -166,8 +151,14 @@ async function enrichCuratedStashHit(query, hit) {
|
|
|
166
151
|
catch {
|
|
167
152
|
shown = undefined;
|
|
168
153
|
}
|
|
154
|
+
// #624-P3: when lazy graph extraction is opted in, enqueue an ungraphed
|
|
155
|
+
// asset for a later pass to extract. Fire-and-forget, non-blocking, NO inline
|
|
156
|
+
// extraction and NO LLM call here. Default-off (flag unset) = byte-identical.
|
|
157
|
+
if (shown?.path)
|
|
158
|
+
maybeEnqueueLazyGraph(shown.path);
|
|
169
159
|
const description = shown?.description ?? hit.description;
|
|
170
160
|
const preview = buildCuratedPreview(shown, hit);
|
|
161
|
+
const mergedSupportRefs = mergeCurateSupportRefs(supportRefs, shown?.related?.hits, selectedRefs, hit.ref);
|
|
171
162
|
return {
|
|
172
163
|
source: "stash",
|
|
173
164
|
type: shown?.type ?? hit.type,
|
|
@@ -178,11 +169,50 @@ async function enrichCuratedStashHit(query, hit) {
|
|
|
178
169
|
...(shown?.keys?.length ? { keys: shown.keys } : {}),
|
|
179
170
|
...(shown?.parameters?.length ? { parameters: shown.parameters } : {}),
|
|
180
171
|
...(shown?.run ? { run: shown.run } : {}),
|
|
172
|
+
...(mergedSupportRefs.length > 0 ? { supportRefs: mergedSupportRefs } : {}),
|
|
181
173
|
followUp: `akm show ${hit.ref}`,
|
|
182
174
|
reason: buildCuratedReason(query, shown?.type ?? hit.type),
|
|
183
175
|
...(hit.score !== undefined ? { score: hit.score } : {}),
|
|
184
176
|
};
|
|
185
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* #624-P3 — enqueue an ungraphed asset for lazy graph extraction when the
|
|
180
|
+
* `index.graph.lazyGraphExtraction` flag is on. Pure side-effect, fully
|
|
181
|
+
* best-effort: any failure (config, fs, db) is swallowed so curate never fails
|
|
182
|
+
* on it. NO LLM call and NO inline extraction — only a cheap queue insert.
|
|
183
|
+
* Default-off (flag unset) returns immediately = byte-identical behavior.
|
|
184
|
+
*/
|
|
185
|
+
function maybeEnqueueLazyGraph(assetPath) {
|
|
186
|
+
try {
|
|
187
|
+
const config = loadConfig();
|
|
188
|
+
if (getIndexPassConfig(config.index, "graph")?.lazyGraphExtraction !== true)
|
|
189
|
+
return;
|
|
190
|
+
const sources = resolveSourceEntries();
|
|
191
|
+
const source = findSourceForPath(assetPath, sources);
|
|
192
|
+
const stashRoot = source?.path;
|
|
193
|
+
if (!stashRoot)
|
|
194
|
+
return;
|
|
195
|
+
let raw;
|
|
196
|
+
try {
|
|
197
|
+
raw = fs.readFileSync(assetPath, "utf8");
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const body = parseFrontmatter(raw).content.trim();
|
|
203
|
+
if (!body)
|
|
204
|
+
return;
|
|
205
|
+
const bodyHash = computeBodyHash(body);
|
|
206
|
+
withIndexDb((db) => {
|
|
207
|
+
if (!hasGraphData(db, stashRoot, assetPath)) {
|
|
208
|
+
enqueueGraphExtraction(db, stashRoot, assetPath, bodyHash, 0);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
catch (err) {
|
|
213
|
+
rethrowIfTestIsolationError(err);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
186
216
|
function buildCuratedRegistryItem(query, hit) {
|
|
187
217
|
return {
|
|
188
218
|
source: "registry",
|
|
@@ -209,19 +239,19 @@ function buildCuratedPreview(shown, hit) {
|
|
|
209
239
|
function buildCuratedReason(query, type) {
|
|
210
240
|
switch (type) {
|
|
211
241
|
case "script":
|
|
212
|
-
return `
|
|
242
|
+
return `Strong runnable script match for "${query}".`;
|
|
213
243
|
case "command":
|
|
214
|
-
return `
|
|
244
|
+
return `Strong reusable command/template match for "${query}".`;
|
|
215
245
|
case "knowledge":
|
|
216
|
-
return `
|
|
246
|
+
return `Strong reference document match for "${query}".`;
|
|
217
247
|
case "skill":
|
|
218
|
-
return `
|
|
248
|
+
return `Strong instructions/workflow match for "${query}".`;
|
|
219
249
|
case "agent":
|
|
220
|
-
return `
|
|
250
|
+
return `Strong specialized agent prompt match for "${query}".`;
|
|
221
251
|
case "memory":
|
|
222
|
-
return `
|
|
252
|
+
return `Strong saved context match for "${query}".`;
|
|
223
253
|
default:
|
|
224
|
-
return `
|
|
254
|
+
return `Strong ${type} match for "${query}".`;
|
|
225
255
|
}
|
|
226
256
|
}
|
|
227
257
|
function buildCurateSummary(query, items) {
|
|
@@ -229,27 +259,23 @@ function buildCurateSummary(query, items) {
|
|
|
229
259
|
return `No curated assets were selected for "${query}".`;
|
|
230
260
|
}
|
|
231
261
|
const labels = items.map((item) => `${item.type}:${item.name}`);
|
|
232
|
-
return `Selected ${items.length}
|
|
233
|
-
}
|
|
234
|
-
function hasSearchResults(result) {
|
|
235
|
-
return result.hits.length > 0 || (result.registryHits?.length ?? 0) > 0;
|
|
262
|
+
return `Selected ${items.length} curated result${items.length === 1 ? "" : "s"}: ${labels.join(", ")}.`;
|
|
236
263
|
}
|
|
237
|
-
/**
|
|
238
|
-
* Extract a small set of fallback keywords when a prompt-style curate query
|
|
239
|
-
* returns no hits as a whole phrase.
|
|
240
|
-
*
|
|
241
|
-
* We keep up to MAX_CURATE_FALLBACK_KEYWORDS distinct keywords and drop short
|
|
242
|
-
* or common filler words so follow-up searches stay inexpensive while focusing
|
|
243
|
-
* on higher-signal terms.
|
|
244
|
-
*/
|
|
245
264
|
export function deriveCurateFallbackQueries(query) {
|
|
246
|
-
|
|
265
|
+
const normalizedWhole = query
|
|
266
|
+
.toLowerCase()
|
|
267
|
+
.replace(/[^a-z0-9]+/g, " ")
|
|
268
|
+
.trim();
|
|
269
|
+
const tokens = Array.from(new Set(query
|
|
247
270
|
.toLowerCase()
|
|
248
271
|
.split(/[^a-z0-9]+/)
|
|
249
272
|
.map((token) => token.trim())
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
273
|
+
.filter((token) => token.length > 0 &&
|
|
274
|
+
!CURATE_FALLBACK_FILTER_WORDS.has(token) &&
|
|
275
|
+
(token.length >= MIN_CURATE_FALLBACK_TOKEN_LENGTH || CURATE_SHORT_FALLBACK_TOKENS.has(token))))).slice(0, MAX_CURATE_FALLBACK_KEYWORDS);
|
|
276
|
+
if (tokens.length === 1 && tokens[0] === normalizedWhole)
|
|
277
|
+
return [];
|
|
278
|
+
return tokens;
|
|
253
279
|
}
|
|
254
280
|
export function mergeCurateSearchResponses(base, extras) {
|
|
255
281
|
const hitsByRef = new Map();
|
|
@@ -289,10 +315,10 @@ export function mergeCurateSearchResponses(base, extras) {
|
|
|
289
315
|
}
|
|
290
316
|
export async function searchForCuration(input) {
|
|
291
317
|
const initial = await akmSearch({ ...input, skipLogging: true });
|
|
292
|
-
if (
|
|
318
|
+
if (!shouldRunCurateFallback(initial, input.limit))
|
|
293
319
|
return initial;
|
|
294
320
|
const fallbackQueries = deriveCurateFallbackQueries(input.query);
|
|
295
|
-
if (fallbackQueries.length
|
|
321
|
+
if (fallbackQueries.length === 0)
|
|
296
322
|
return initial;
|
|
297
323
|
const fallbackResults = await Promise.all(fallbackQueries.map((token) => akmSearch({
|
|
298
324
|
query: token,
|
|
@@ -303,3 +329,241 @@ export async function searchForCuration(input) {
|
|
|
303
329
|
})));
|
|
304
330
|
return mergeCurateSearchResponses(initial, fallbackResults);
|
|
305
331
|
}
|
|
332
|
+
function parseCurateIntent(query) {
|
|
333
|
+
const lower = query.toLowerCase();
|
|
334
|
+
return {
|
|
335
|
+
executionHeavy: /(run|script|bash|shell|cli|execute|automation|deploy|build|test|lint)/.test(lower),
|
|
336
|
+
multiStep: /(plan|workflow|steps?|procedure|rollout|review|migration|release|checklist)/.test(lower),
|
|
337
|
+
delegation: /(agent|assistant|planner|reviewer|architect|prompt)/.test(lower),
|
|
338
|
+
recall: /(memory|context|recall|remember)/.test(lower),
|
|
339
|
+
reference: CURATE_REFERENCE_QUERY_RE.test(lower),
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
function computeCurateTypeNudge(type, intent) {
|
|
343
|
+
let nudge = 0;
|
|
344
|
+
if (intent.executionHeavy) {
|
|
345
|
+
if (type === "script")
|
|
346
|
+
nudge += 0.06;
|
|
347
|
+
else if (type === "command")
|
|
348
|
+
nudge += 0.04;
|
|
349
|
+
else if (type === "memory")
|
|
350
|
+
nudge -= 0.04;
|
|
351
|
+
}
|
|
352
|
+
if (intent.multiStep) {
|
|
353
|
+
if (type === "workflow")
|
|
354
|
+
nudge += 0.06;
|
|
355
|
+
else if (type === "skill")
|
|
356
|
+
nudge += 0.04;
|
|
357
|
+
else if (type === "knowledge")
|
|
358
|
+
nudge -= 0.02;
|
|
359
|
+
}
|
|
360
|
+
if (intent.delegation && type === "agent")
|
|
361
|
+
nudge += 0.06;
|
|
362
|
+
if (intent.recall && type === "memory")
|
|
363
|
+
nudge += 0.08;
|
|
364
|
+
if (intent.reference) {
|
|
365
|
+
if (type === "knowledge")
|
|
366
|
+
nudge += 0.05;
|
|
367
|
+
else if (type === "skill")
|
|
368
|
+
nudge += 0.02;
|
|
369
|
+
}
|
|
370
|
+
return nudge;
|
|
371
|
+
}
|
|
372
|
+
function getCurateFamily(ref) {
|
|
373
|
+
try {
|
|
374
|
+
const parsed = parseAssetRef(ref);
|
|
375
|
+
if (parsed.type === "skill") {
|
|
376
|
+
return { key: parsed.name, role: "root" };
|
|
377
|
+
}
|
|
378
|
+
if (parsed.type !== "knowledge")
|
|
379
|
+
return undefined;
|
|
380
|
+
const match = /^skills\/(.+?)\/references\/(.+)$/.exec(parsed.name);
|
|
381
|
+
if (!match)
|
|
382
|
+
return undefined;
|
|
383
|
+
return {
|
|
384
|
+
key: match[1],
|
|
385
|
+
role: "reference",
|
|
386
|
+
topicTokens: match[2]
|
|
387
|
+
.split(/[^a-z0-9]+/i)
|
|
388
|
+
.map((token) => token.trim().toLowerCase())
|
|
389
|
+
.filter(Boolean),
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
catch {
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function annotateCurateHit(query, hit, index, intent) {
|
|
397
|
+
const rawScore = hit.score ?? 0;
|
|
398
|
+
const family = getCurateFamily(hit.ref);
|
|
399
|
+
let adjustedScore = rawScore + computeCurateTypeNudge(hit.type, intent);
|
|
400
|
+
if (family?.role === "root" && !isNarrowReferenceFamilyQuery(query, family))
|
|
401
|
+
adjustedScore += 0.07;
|
|
402
|
+
if (family?.role === "reference" && isNarrowReferenceFamilyQuery(query, family))
|
|
403
|
+
adjustedScore += 0.07;
|
|
404
|
+
return {
|
|
405
|
+
hit,
|
|
406
|
+
rawScore,
|
|
407
|
+
adjustedScore,
|
|
408
|
+
originalIndex: index,
|
|
409
|
+
family,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
function compareCurateHits(a, b) {
|
|
413
|
+
const rawDiff = b.rawScore - a.rawScore;
|
|
414
|
+
if (Math.abs(rawDiff) > CURATE_CLOSE_SCORE_BAND)
|
|
415
|
+
return rawDiff;
|
|
416
|
+
const adjustedDiff = b.adjustedScore - a.adjustedScore;
|
|
417
|
+
if (adjustedDiff !== 0)
|
|
418
|
+
return adjustedDiff;
|
|
419
|
+
if (rawDiff !== 0)
|
|
420
|
+
return rawDiff;
|
|
421
|
+
return a.originalIndex - b.originalIndex;
|
|
422
|
+
}
|
|
423
|
+
function passesCurateScoreFloor(hit, leaderScore) {
|
|
424
|
+
if (leaderScore === undefined)
|
|
425
|
+
return true;
|
|
426
|
+
return hit.rawScore >= Math.max(CURATE_TAIL_SCORE_FLOOR, leaderScore * CURATE_RELATIVE_SCORE_FLOOR);
|
|
427
|
+
}
|
|
428
|
+
function isNarrowReferenceFamilyQuery(query, family) {
|
|
429
|
+
if (!family || family.role !== "reference")
|
|
430
|
+
return false;
|
|
431
|
+
const lower = query.toLowerCase();
|
|
432
|
+
if (CURATE_REFERENCE_QUERY_RE.test(lower))
|
|
433
|
+
return true;
|
|
434
|
+
return family.topicTokens.some((token) => token.length >= 3 && lower.includes(token));
|
|
435
|
+
}
|
|
436
|
+
function appendCurateSupportRef(supportRefsByRef, ownerRef, supportRef) {
|
|
437
|
+
const existing = supportRefsByRef.get(ownerRef) ?? [];
|
|
438
|
+
if (existing.some((entry) => entry.ref === supportRef.ref))
|
|
439
|
+
return;
|
|
440
|
+
supportRefsByRef.set(ownerRef, [...existing, supportRef]);
|
|
441
|
+
}
|
|
442
|
+
function selectCuratedStashHits(query, hits, limit) {
|
|
443
|
+
const intent = parseCurateIntent(query);
|
|
444
|
+
const collapsed = collapseCurateFamilies(query, hits);
|
|
445
|
+
const ranked = collapsed.hits
|
|
446
|
+
.map(({ hit, originalIndex }) => annotateCurateHit(query, hit, originalIndex, intent))
|
|
447
|
+
.sort(compareCurateHits);
|
|
448
|
+
const selected = [];
|
|
449
|
+
const supportRefsByRef = collapsed.supportRefsByRef;
|
|
450
|
+
let leaderScore;
|
|
451
|
+
for (const candidate of ranked) {
|
|
452
|
+
if (!passesCurateScoreFloor(candidate, leaderScore))
|
|
453
|
+
continue;
|
|
454
|
+
selected.push(candidate);
|
|
455
|
+
if (leaderScore === undefined)
|
|
456
|
+
leaderScore = candidate.rawScore;
|
|
457
|
+
if (selected.length >= limit)
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
return { selected: selected.map((entry) => entry.hit), supportRefsByRef };
|
|
461
|
+
}
|
|
462
|
+
function collapseCurateFamilies(query, hits) {
|
|
463
|
+
const passthrough = [];
|
|
464
|
+
const supportRefsByRef = new Map();
|
|
465
|
+
const groups = new Map();
|
|
466
|
+
for (const [index, hit] of hits.entries()) {
|
|
467
|
+
const family = getCurateFamily(hit.ref);
|
|
468
|
+
if (!family) {
|
|
469
|
+
passthrough.push({ hit, originalIndex: index });
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
const group = groups.get(family.key) ?? { references: [] };
|
|
473
|
+
if (family.role === "root") {
|
|
474
|
+
if (!group.root)
|
|
475
|
+
group.root = { hit, originalIndex: index };
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
group.references.push({ hit, originalIndex: index });
|
|
479
|
+
}
|
|
480
|
+
groups.set(family.key, group);
|
|
481
|
+
}
|
|
482
|
+
const collapsedFamilies = [];
|
|
483
|
+
for (const group of groups.values()) {
|
|
484
|
+
const bestReference = group.references[0];
|
|
485
|
+
const representative = group.root && !isNarrowReferenceFamilyQuery(query, getCurateFamily(bestReference?.hit.ref ?? group.root.hit.ref))
|
|
486
|
+
? group.root
|
|
487
|
+
: (bestReference ?? group.root);
|
|
488
|
+
if (!representative)
|
|
489
|
+
continue;
|
|
490
|
+
collapsedFamilies.push(representative);
|
|
491
|
+
const supportCandidates = [group.root, ...group.references].filter((entry) => {
|
|
492
|
+
return entry !== undefined && entry.hit.ref !== representative.hit.ref;
|
|
493
|
+
});
|
|
494
|
+
for (const support of supportCandidates) {
|
|
495
|
+
appendCurateSupportRef(supportRefsByRef, representative.hit.ref, {
|
|
496
|
+
ref: support.hit.ref,
|
|
497
|
+
type: support.hit.type,
|
|
498
|
+
reason: "Related family asset to inspect next.",
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return {
|
|
503
|
+
hits: [...passthrough, ...collapsedFamilies].sort((a, b) => a.originalIndex - b.originalIndex),
|
|
504
|
+
supportRefsByRef,
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
function preferBroadRootRepresentative(query, selected, allHits, supportRefsByRef) {
|
|
508
|
+
const first = selected[0];
|
|
509
|
+
if (!first)
|
|
510
|
+
return { selected, supportRefsByRef };
|
|
511
|
+
const match = /^knowledge:skills\/(.+?)\/references\/(.+)$/.exec(first.ref);
|
|
512
|
+
if (!match)
|
|
513
|
+
return { selected, supportRefsByRef };
|
|
514
|
+
const lower = query.toLowerCase();
|
|
515
|
+
const topicTokens = match[2].split(/[^a-z0-9]+/i).filter(Boolean);
|
|
516
|
+
const wantsReference = CURATE_REFERENCE_QUERY_RE.test(lower) ||
|
|
517
|
+
topicTokens.some((token) => token.length >= 3 && lower.includes(token.toLowerCase()));
|
|
518
|
+
if (wantsReference)
|
|
519
|
+
return { selected, supportRefsByRef };
|
|
520
|
+
const rootRef = `skill:${match[1]}`;
|
|
521
|
+
const rootHit = allHits.find((hit) => hit.ref === rootRef);
|
|
522
|
+
if (!rootHit)
|
|
523
|
+
return { selected, supportRefsByRef };
|
|
524
|
+
const next = [rootHit, ...selected.filter((hit) => hit.ref !== first.ref && hit.ref !== rootRef)];
|
|
525
|
+
const merged = new Map(supportRefsByRef);
|
|
526
|
+
const priorSupport = merged.get(first.ref) ?? [];
|
|
527
|
+
for (const entry of priorSupport)
|
|
528
|
+
appendCurateSupportRef(merged, rootRef, entry);
|
|
529
|
+
appendCurateSupportRef(merged, rootRef, {
|
|
530
|
+
ref: first.ref,
|
|
531
|
+
type: first.type,
|
|
532
|
+
reason: "Related family asset to inspect next.",
|
|
533
|
+
});
|
|
534
|
+
merged.delete(first.ref);
|
|
535
|
+
return { selected: next, supportRefsByRef: merged };
|
|
536
|
+
}
|
|
537
|
+
function mergeCurateSupportRefs(seeded, relatedHits, selectedRefs, ownerRef) {
|
|
538
|
+
const merged = [];
|
|
539
|
+
for (const entry of seeded) {
|
|
540
|
+
if (entry.ref === ownerRef || selectedRefs.has(entry.ref))
|
|
541
|
+
continue;
|
|
542
|
+
if (merged.some((existing) => existing.ref === entry.ref))
|
|
543
|
+
continue;
|
|
544
|
+
merged.push(entry);
|
|
545
|
+
if (merged.length >= MAX_CURATE_SUPPORT_REFS)
|
|
546
|
+
return merged;
|
|
547
|
+
}
|
|
548
|
+
if (!Array.isArray(relatedHits))
|
|
549
|
+
return merged;
|
|
550
|
+
for (const hit of relatedHits) {
|
|
551
|
+
if (!hit.ref || hit.ref === ownerRef || selectedRefs.has(hit.ref))
|
|
552
|
+
continue;
|
|
553
|
+
if (merged.some((existing) => existing.ref === hit.ref))
|
|
554
|
+
continue;
|
|
555
|
+
merged.push({ ref: hit.ref, type: hit.type, reason: "Related asset via shared entities." });
|
|
556
|
+
if (merged.length >= MAX_CURATE_SUPPORT_REFS)
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
return merged;
|
|
560
|
+
}
|
|
561
|
+
function shouldRunCurateFallback(initial, desiredCount) {
|
|
562
|
+
const stashHits = initial.hits.filter((hit) => hit.type !== "registry");
|
|
563
|
+
if (stashHits.length === 0)
|
|
564
|
+
return true;
|
|
565
|
+
const topScore = stashHits[0]?.score ?? 0;
|
|
566
|
+
const strongFloor = Math.max(CURATE_FALLBACK_STRONG_SCORE_FLOOR, topScore * CURATE_RELATIVE_SCORE_FLOOR);
|
|
567
|
+
const strongCount = stashHits.filter((hit) => (hit.score ?? 0) >= strongFloor).length;
|
|
568
|
+
return !(topScore >= CURATE_FALLBACK_TOP_SCORE_THRESHOLD && strongCount >= Math.min(2, desiredCount));
|
|
569
|
+
}
|
|
@@ -63,6 +63,11 @@ export const searchCommand = defineJsonCommand({
|
|
|
63
63
|
description: "Disable the automatic project-context ranking boost (also disabled by AKM_DISABLE_PROJECT_CONTEXT=1).",
|
|
64
64
|
default: false,
|
|
65
65
|
},
|
|
66
|
+
"include-sessions": {
|
|
67
|
+
type: "boolean",
|
|
68
|
+
description: "Include session assets (excluded from default search results via config.search.defaultExcludeTypes).",
|
|
69
|
+
default: false,
|
|
70
|
+
},
|
|
66
71
|
},
|
|
67
72
|
async run({ args }) {
|
|
68
73
|
const query = (args.query ?? "").trim();
|
|
@@ -79,6 +84,7 @@ export const searchCommand = defineJsonCommand({
|
|
|
79
84
|
const includeProposed = args["include-proposed"] === true;
|
|
80
85
|
const belief = parseBeliefFilterMode(typeof args.belief === "string" ? args.belief : undefined);
|
|
81
86
|
const noProjectContext = getHyphenatedBoolean(args, "no-project-context");
|
|
87
|
+
const includeSessions = getHyphenatedBoolean(args, "include-sessions");
|
|
82
88
|
// --no-project-context sets env so searchDatabase picks it up without
|
|
83
89
|
// threading the flag through the entire call stack.
|
|
84
90
|
if (noProjectContext)
|
|
@@ -91,6 +97,7 @@ export const searchCommand = defineJsonCommand({
|
|
|
91
97
|
filters,
|
|
92
98
|
includeProposed,
|
|
93
99
|
belief,
|
|
100
|
+
includeSessions,
|
|
94
101
|
eventSource: resolveEventSource(),
|
|
95
102
|
});
|
|
96
103
|
output("search", result);
|
|
@@ -108,6 +108,7 @@ export async function akmSearch(input) {
|
|
|
108
108
|
// Without this, the index (which spans every configured source)
|
|
109
109
|
// would leak hits from sources the caller did not request.
|
|
110
110
|
restrictToSources: namedSourceName !== undefined,
|
|
111
|
+
includeExcludedTypes: input.includeSessions === true,
|
|
111
112
|
});
|
|
112
113
|
const registryResult = source === "stash" ? undefined : await searchRegistry(query, { limit, registries: config.registries });
|
|
113
114
|
if (source === "stash") {
|