akm-cli 0.9.0-beta.53 → 0.9.0-beta.55
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/dist/cli/clack.js +56 -0
- package/dist/cli/confirm.js +1 -1
- package/dist/cli.js +5 -3
- package/dist/commands/agent/contribute-cli.js +2 -3
- package/dist/commands/env/env-cli.js +187 -202
- package/dist/commands/env/secret-cli.js +109 -121
- package/dist/commands/feedback-cli.js +152 -155
- package/dist/commands/health/advisories.js +151 -0
- package/dist/commands/health/html-report.js +33 -10
- package/dist/commands/health/improve-metrics.js +754 -0
- package/dist/commands/health/llm-usage.js +65 -0
- package/dist/commands/health/md-report.js +103 -0
- package/dist/commands/health/metrics.js +278 -0
- package/dist/commands/health/task-runs.js +135 -0
- package/dist/commands/health/types.js +18 -0
- package/dist/commands/health/windows.js +196 -0
- package/dist/commands/health.js +15 -1492
- package/dist/commands/improve/anti-collapse.js +170 -0
- package/dist/commands/improve/collapse-detector.js +3 -2
- package/dist/commands/improve/consolidate.js +636 -633
- package/dist/commands/improve/dedup.js +1 -1
- package/dist/commands/improve/distill/content-repair.js +202 -0
- package/dist/commands/improve/distill/promote-memory.js +228 -0
- package/dist/commands/improve/distill/quality-gate.js +233 -0
- package/dist/commands/improve/distill-guards.js +127 -0
- package/dist/commands/improve/distill.js +49 -575
- package/dist/commands/improve/extract-cli.js +74 -76
- package/dist/commands/improve/extract.js +6 -4
- package/dist/commands/improve/hot-probation.js +45 -0
- package/dist/commands/improve/improve-auto-accept.js +3 -2
- package/dist/commands/improve/improve-cli.js +14 -13
- package/dist/commands/improve/improve-result-file.js +2 -1
- package/dist/commands/improve/improve.js +6 -5
- package/dist/commands/improve/loop-stages.js +19 -21
- package/dist/commands/improve/outcome-loop.js +18 -16
- package/dist/commands/improve/preparation.js +23 -5
- package/dist/commands/improve/procedural.js +10 -31
- package/dist/commands/improve/recombine.js +19 -43
- package/dist/commands/improve/reflect.js +1 -1
- package/dist/commands/improve/schema-similarity-gate.js +168 -0
- package/dist/commands/improve/shared.js +48 -0
- package/dist/commands/observability-cli.js +4 -4
- package/dist/commands/proposal/drain-policies.js +2 -2
- package/dist/commands/proposal/drain.js +1 -1
- package/dist/commands/proposal/legacy-import.js +115 -0
- package/dist/commands/proposal/proposal-cli.js +3 -3
- package/dist/commands/proposal/proposal.js +2 -1
- package/dist/commands/proposal/propose.js +1 -1
- package/dist/commands/proposal/repository.js +829 -0
- package/dist/commands/proposal/validators/proposals.js +5 -920
- package/dist/commands/read/curate.js +4 -4
- package/dist/commands/read/remember-cli.js +132 -137
- package/dist/commands/read/search-cli.js +7 -5
- package/dist/commands/read/search.js +7 -3
- package/dist/commands/read/show.js +3 -5
- package/dist/commands/registry-cli.js +76 -87
- package/dist/commands/sources/add-cli.js +91 -95
- package/dist/commands/sources/history.js +1 -1
- package/dist/commands/sources/init.js +12 -0
- package/dist/commands/sources/schema-repair.js +1 -1
- package/dist/commands/sources/sources-cli.js +3 -3
- package/dist/commands/sources/stash-cli.js +2 -2
- package/dist/commands/tasks/default-tasks.js +12 -0
- package/dist/commands/tasks/tasks-cli.js +1 -2
- package/dist/commands/wiki-cli.js +2 -3
- package/dist/core/common.js +3 -3
- package/dist/core/config/config-schema.js +6 -0
- package/dist/core/config/config.js +12 -0
- package/dist/core/deep-merge.js +38 -0
- package/dist/core/events.js +2 -1
- package/dist/core/logs-db.js +8 -13
- package/dist/core/paths.js +14 -14
- package/dist/core/state-db.js +13 -1140
- package/dist/core/warn.js +21 -0
- package/dist/indexer/db/db.js +72 -709
- package/dist/indexer/db/entry-mapper.js +41 -0
- package/dist/indexer/db/schema.js +516 -0
- package/dist/indexer/ensure-index.js +3 -2
- package/dist/indexer/feedback/utility-policy.js +85 -0
- package/dist/indexer/graph/graph-extraction.js +2 -1
- package/dist/indexer/index-writer-lock.js +18 -0
- package/dist/indexer/indexer.js +94 -27
- package/dist/indexer/read-preflight.js +23 -0
- package/dist/indexer/search/fts-query.js +51 -0
- package/dist/indexer/walk/walker.js +21 -13
- package/dist/integrations/agent/detect.js +9 -0
- package/dist/integrations/agent/index.js +1 -1
- package/dist/integrations/agent/spawn.js +15 -66
- package/dist/llm/client.js +12 -0
- package/dist/llm/embedder.js +26 -2
- package/dist/llm/embedders/local.js +7 -1
- package/dist/output/text/helpers.js +13 -0
- package/dist/scripts/migrate-storage.js +6903 -7424
- package/dist/scripts/migrations/import-fs-improve-runs-to-db.js +49 -44
- package/dist/setup/detect.js +9 -0
- package/dist/setup/legacy-config.js +106 -0
- package/dist/setup/prompt.js +57 -0
- package/dist/setup/providers.js +14 -0
- package/dist/setup/registry-stash-loader.js +12 -0
- package/dist/setup/semantic-assets.js +124 -0
- package/dist/setup/setup.js +25 -1608
- package/dist/setup/steps/connection.js +734 -0
- package/dist/setup/steps/output.js +31 -0
- package/dist/setup/steps/platforms.js +124 -0
- package/dist/setup/steps/semantic.js +27 -0
- package/dist/setup/steps/sources.js +222 -0
- package/dist/setup/steps/stashdir.js +42 -0
- package/dist/setup/steps/tasks.js +152 -0
- package/dist/storage/repositories/canaries-repository.js +107 -0
- package/dist/storage/repositories/consolidation-repository.js +38 -0
- package/dist/storage/repositories/embeddings-repository.js +72 -0
- package/dist/storage/repositories/events-repository.js +187 -0
- package/dist/storage/repositories/extract-sessions-repository.js +96 -0
- package/dist/storage/repositories/improve-runs-repository.js +130 -0
- package/dist/storage/repositories/index-db.js +4 -7
- package/dist/storage/repositories/proposals-repository.js +220 -0
- package/dist/storage/repositories/recombine-repository.js +213 -0
- package/dist/storage/repositories/task-history-repository.js +93 -0
- package/dist/storage/sqlite-pragmas.js +3 -3
- package/dist/tasks/backends/index.js +9 -0
- package/dist/tasks/runner.js +11 -1
- package/package.json +2 -2
- package/dist/commands/improve/homeostatic.js +0 -497
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
/**
|
|
5
|
+
* WS-3b Step 8 — Anti-collapse merge guards.
|
|
6
|
+
*
|
|
7
|
+
* (a) Generation counter: merged.generation = max(sources)+1; refuse merge
|
|
8
|
+
* of two assets both above generation N (default 2); merges cite sources.
|
|
9
|
+
* (b) Lexical-diversity check: low n-gram diversity ⇒ raise merge threshold.
|
|
10
|
+
* (c) Merge-information floor (R5 §4.2): provenance union must not shrink and
|
|
11
|
+
* the merged body must retain a minimum fraction of the source tokens.
|
|
12
|
+
* (d) Occasional random non-similar cluster in the pool.
|
|
13
|
+
*
|
|
14
|
+
* @module anti-collapse
|
|
15
|
+
*/
|
|
16
|
+
/** Default max generation depth before merge is refused. */
|
|
17
|
+
export const DEFAULT_MAX_GENERATION = 2;
|
|
18
|
+
/** Default fraction of pool to fill with random (non-similar) clusters. */
|
|
19
|
+
export const DEFAULT_RANDOM_CLUSTER_FRACTION = 0.05;
|
|
20
|
+
/**
|
|
21
|
+
* Read the `generation` field from an asset's frontmatter.
|
|
22
|
+
* Returns 0 when absent (no generation metadata = original asset).
|
|
23
|
+
*/
|
|
24
|
+
export function readAssetGeneration(frontmatterData) {
|
|
25
|
+
const gen = frontmatterData.generation;
|
|
26
|
+
if (typeof gen === "number" && Number.isFinite(gen) && gen >= 0) {
|
|
27
|
+
return Math.floor(gen);
|
|
28
|
+
}
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Compute the new generation for a merged asset.
|
|
33
|
+
* Rule: `merged.generation = max(source generations) + 1`.
|
|
34
|
+
*/
|
|
35
|
+
export function computeMergedGeneration(sourceGenerations) {
|
|
36
|
+
if (sourceGenerations.length === 0)
|
|
37
|
+
return 1;
|
|
38
|
+
return Math.max(...sourceGenerations) + 1;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Check whether a merge of the given assets should be refused due to the
|
|
42
|
+
* anti-collapse generation guard.
|
|
43
|
+
*
|
|
44
|
+
* Returns `{ refused: true, reason }` when BOTH assets have generation > maxGeneration.
|
|
45
|
+
* Returns `{ refused: false }` when the merge is allowed.
|
|
46
|
+
*
|
|
47
|
+
* @param sourceGenerations - Generation values for all merge participants.
|
|
48
|
+
* @param config - Anti-collapse config.
|
|
49
|
+
*/
|
|
50
|
+
export function checkGenerationGuard(sourceGenerations, config) {
|
|
51
|
+
// R5: default ON — only an explicit opt-out disables the guard.
|
|
52
|
+
if (config.enabled === false)
|
|
53
|
+
return { refused: false };
|
|
54
|
+
const maxGen = config.maxGeneration ?? DEFAULT_MAX_GENERATION;
|
|
55
|
+
const highGenCount = sourceGenerations.filter((g) => g > maxGen).length;
|
|
56
|
+
if (highGenCount >= 2) {
|
|
57
|
+
return {
|
|
58
|
+
refused: true,
|
|
59
|
+
reason: `Anti-collapse: ${highGenCount} merge participants have generation > ${maxGen} (${sourceGenerations.join(", ")}); refusing to merge over-consolidated assets.`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return { refused: false };
|
|
63
|
+
}
|
|
64
|
+
/** Distinct-token retention floor default (R5 §4.2). */
|
|
65
|
+
export const DEFAULT_MIN_SPECIFICITY_RETENTION = 0.6;
|
|
66
|
+
function distinctTokens(text) {
|
|
67
|
+
// Same lowercase whitespace tokenization computeBigramDiversity uses.
|
|
68
|
+
return new Set(text
|
|
69
|
+
.toLowerCase()
|
|
70
|
+
.split(/\s+/)
|
|
71
|
+
.filter((w) => w.length > 0));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A merge must strictly increase information (R5 §4.2):
|
|
75
|
+
* 1. Provenance: the merged asset's `source_refs` must be a superset of the
|
|
76
|
+
* union of all participants' `source_refs` plus the participant refs
|
|
77
|
+
* themselves — provenance never shrinks through a merge.
|
|
78
|
+
* 2. Specificity: distinctTokens(mergedBody) ≥ minSpecificityRetention ×
|
|
79
|
+
* |union(distinctTokens(participant bodies))| — a merge that only
|
|
80
|
+
* shortens/genericizes fails.
|
|
81
|
+
*
|
|
82
|
+
* Pure and deterministic; ADVISORY in v1 (the caller counts violations, it
|
|
83
|
+
* does not refuse the merge). Returns `passed: true` immediately when the
|
|
84
|
+
* anti-collapse suite or the floor itself is opted out.
|
|
85
|
+
*/
|
|
86
|
+
export function checkMergeInformationFloor(mergedBody, mergedSourceRefs, participants, config) {
|
|
87
|
+
if (config.enabled === false || config.mergeInformationFloor === false || participants.length === 0) {
|
|
88
|
+
return { passed: true, provenanceBefore: 0, provenanceAfter: 0, specificityRetention: 1 };
|
|
89
|
+
}
|
|
90
|
+
// 1. Provenance union: participants + everything they already cited.
|
|
91
|
+
const required = new Set();
|
|
92
|
+
for (const p of participants) {
|
|
93
|
+
required.add(p.ref);
|
|
94
|
+
for (const sr of p.sourceRefs)
|
|
95
|
+
required.add(sr);
|
|
96
|
+
}
|
|
97
|
+
const after = new Set(mergedSourceRefs);
|
|
98
|
+
const missing = [...required].filter((r) => !after.has(r));
|
|
99
|
+
// 2. Specificity retention over the union of source tokens.
|
|
100
|
+
const sourceTokens = new Set();
|
|
101
|
+
for (const p of participants) {
|
|
102
|
+
for (const t of distinctTokens(p.body))
|
|
103
|
+
sourceTokens.add(t);
|
|
104
|
+
}
|
|
105
|
+
const mergedTokens = distinctTokens(mergedBody);
|
|
106
|
+
// Clamped at computation so the pass/fail decision, the reason string, and
|
|
107
|
+
// the reported field all describe the same value.
|
|
108
|
+
const specificityRetention = Math.min(1, sourceTokens.size === 0 ? 1 : mergedTokens.size / sourceTokens.size);
|
|
109
|
+
const minRetention = config.minSpecificityRetention ?? DEFAULT_MIN_SPECIFICITY_RETENTION;
|
|
110
|
+
const provenanceOk = missing.length === 0;
|
|
111
|
+
const specificityOk = specificityRetention >= minRetention;
|
|
112
|
+
const reasons = [];
|
|
113
|
+
if (!provenanceOk) {
|
|
114
|
+
reasons.push(`provenance shrank: merged source_refs missing ${missing.length} ref(s) (e.g. ${missing[0]})`);
|
|
115
|
+
}
|
|
116
|
+
if (!specificityOk) {
|
|
117
|
+
reasons.push(`specificity retention ${specificityRetention.toFixed(2)} < ${minRetention} (merge genericized/shortened)`);
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
passed: provenanceOk && specificityOk,
|
|
121
|
+
provenanceBefore: required.size,
|
|
122
|
+
provenanceAfter: after.size,
|
|
123
|
+
specificityRetention,
|
|
124
|
+
...(reasons.length > 0 ? { reason: reasons.join("; ") } : {}),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Compute the bigram n-gram diversity of a text string.
|
|
129
|
+
* Returns a value in [0, 1] where 0 = all identical bigrams, 1 = all unique.
|
|
130
|
+
* Used by the lexical-diversity check to detect correlated-extraction artifacts.
|
|
131
|
+
*/
|
|
132
|
+
export function computeBigramDiversity(text) {
|
|
133
|
+
const words = text
|
|
134
|
+
.toLowerCase()
|
|
135
|
+
.split(/\s+/)
|
|
136
|
+
.filter((w) => w.length > 0);
|
|
137
|
+
if (words.length < 2)
|
|
138
|
+
return 1; // too short to have bigrams; treat as diverse
|
|
139
|
+
const total = words.length - 1;
|
|
140
|
+
const unique = new Set();
|
|
141
|
+
for (let i = 0; i < total; i++) {
|
|
142
|
+
unique.add(`${words[i]}\t${words[i + 1]}`);
|
|
143
|
+
}
|
|
144
|
+
return unique.size / total;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Check whether a cluster of memories exhibits suspiciously low lexical diversity.
|
|
148
|
+
* When true, the cluster is likely a correlated-extraction artifact; the merge
|
|
149
|
+
* threshold should be raised.
|
|
150
|
+
*
|
|
151
|
+
* @param bodies - The stripped body texts of the cluster members.
|
|
152
|
+
* @param config - Anti-collapse config.
|
|
153
|
+
* @returns `{ lowDiversity: true, diversity }` when the cluster diversity is
|
|
154
|
+
* below the 0.3 threshold; `{ lowDiversity: false }` otherwise.
|
|
155
|
+
*/
|
|
156
|
+
export function checkLexicalDiversity(bodies, config) {
|
|
157
|
+
// R5: default ON — only an explicit opt-out disables the check.
|
|
158
|
+
if (config.enabled === false || config.lexicalDiversityCheck === false) {
|
|
159
|
+
return { lowDiversity: false };
|
|
160
|
+
}
|
|
161
|
+
if (bodies.length === 0)
|
|
162
|
+
return { lowDiversity: false };
|
|
163
|
+
// Average bigram diversity across all bodies in the cluster.
|
|
164
|
+
const avg = bodies.reduce((sum, b) => sum + computeBigramDiversity(b), 0) / bodies.length;
|
|
165
|
+
const DIVERSITY_FLOOR = 0.3;
|
|
166
|
+
if (avg < DIVERSITY_FLOOR) {
|
|
167
|
+
return { lowDiversity: true, diversity: avg };
|
|
168
|
+
}
|
|
169
|
+
return { lowDiversity: false };
|
|
170
|
+
}
|
|
@@ -27,10 +27,11 @@
|
|
|
27
27
|
import { randomBytes } from "node:crypto";
|
|
28
28
|
import { makeAssetRef } from "../../core/asset/asset-ref.js";
|
|
29
29
|
import { appendEvent } from "../../core/events.js";
|
|
30
|
-
import {
|
|
30
|
+
import { withStateDb } from "../../core/state-db.js";
|
|
31
31
|
import { warn } from "../../core/warn.js";
|
|
32
32
|
import { closeDatabase, getAllEntries, openExistingDatabase, searchFts, } from "../../indexer/db/db.js";
|
|
33
|
-
import {
|
|
33
|
+
import { deactivateCanarySet, getActiveCanaries, getCanariesBySetId, insertCanaries, insertCycleMetrics, listActiveCanarySetIds, queryRecentCycleMetrics, } from "../../storage/repositories/canaries-repository.js";
|
|
34
|
+
import { computeBigramDiversity, DEFAULT_MAX_GENERATION } from "./anti-collapse.js";
|
|
34
35
|
import { getAllRankScores } from "./salience.js";
|
|
35
36
|
// ── Defaults (mirrored in config-schema.ts ImproveCollapseDetectorSchema) ────
|
|
36
37
|
export const DEFAULT_CANARY_COUNT = 40; // owner-approved 30–50 range
|