thumbgate 1.37.3 → 1.39.0
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/.agents/skills/aws-strands-harness-not-clone/SKILL.md +53 -0
- package/.agents/skills/bolt-forge-opt-in-not-clone/SKILL.md +72 -0
- package/.agents/skills/decisive-step-evaluator/SKILL.md +47 -0
- package/.agents/skills/fiddler-control-plane-honesty/SKILL.md +61 -0
- package/.agents/skills/halo-trace-optimize-not-clone/SKILL.md +60 -0
- package/.agents/skills/harness-evolution-ceiling/SKILL.md +52 -0
- package/.agents/skills/infoq-architect-honesty-not-clone/SKILL.md +37 -0
- package/.agents/skills/meko-datapack-honesty-not-clone/SKILL.md +45 -0
- package/.agents/skills/mintlify-knowledge-honesty-not-clone/SKILL.md +49 -0
- package/.agents/skills/model-distress-governance/SKILL.md +56 -0
- package/.agents/skills/omp-coordination-honesty-not-clone/SKILL.md +94 -0
- package/.agents/skills/saas-moat-honesty-not-clone/SKILL.md +22 -0
- package/.agents/skills/salt-code-policy-honesty-not-clone/SKILL.md +76 -0
- package/.agents/skills/siren-mcp-governance/SKILL.md +50 -0
- package/.agents/skills/vs-coverage-gap-not-clone/SKILL.md +69 -0
- package/.agents/skills/wisdom-context-artifact-not-clone/SKILL.md +67 -0
- package/.claude-plugin/plugin.json +1 -1
- package/.well-known/mcp/server-card.json +1 -1
- package/adapters/claude/.mcp.json +2 -2
- package/adapters/forge/forge.yaml +3 -3
- package/adapters/future-agi/.mcp.json +1 -1
- package/adapters/future-agi/config.toml +1 -1
- package/adapters/future-agi/opencode.json +1 -1
- package/adapters/herdr/herdr-plugin.toml +2 -2
- package/adapters/hotdata/hotdata-vector-store.js +575 -0
- package/adapters/mcp/server-stdio.js +1 -1
- package/adapters/opencode/opencode.json +1 -1
- package/adapters/strands/STRANDS.md +107 -0
- package/adapters/strands/strands-box-diode.js +344 -0
- package/adapters/strands/strands-decider-adapter.js +445 -0
- package/adapters/strands/strands-middleware.js +192 -0
- package/adapters/tailscale/.mcp.json +15 -0
- package/adapters/tailscale/TAILSCALE.md +117 -0
- package/adapters/tailscale/config.toml +16 -0
- package/bin/cli.js +602 -21
- package/config/gate-templates.json +109 -0
- package/config/gates/default.json +13 -0
- package/config/schemas/task-outcome-receipt.schema.json +12 -0
- package/package.json +150 -21
- package/public/blog/2026-09-28-pretool-secret-deny.html +83 -0
- package/public/blog/2026-10-01-aws-strands-harness-token-firewall.html +77 -0
- package/public/blog/2026-10-01-model-distress-relief-seeking-firewall.html +71 -0
- package/public/blog/2026-10-02-salt-code-honest-steal-40-guardrails.html +106 -0
- package/public/blog/2026-10-05-why-3-days-of-triage-wont-save-ai-speed-code.html +119 -0
- package/public/blog/2026-10-09-your-code-got-a-third-author-governance.html +141 -0
- package/public/connectors.html +329 -0
- package/public/enterprise-governance.html +311 -0
- package/public/gaming-playbook.html +732 -0
- package/public/index.html +77 -51
- package/public/install.html +7 -7
- package/public/learn.html +14 -0
- package/public/numbers.html +2 -2
- package/public/onboarding.html +794 -0
- package/public/operator-framework.html +385 -0
- package/public/pricing.html +14 -10
- package/scripts/activesaddler-curriculum-doctor.js +357 -0
- package/scripts/agent-context-artifact.js +325 -0
- package/scripts/agent-egress-policy.js +2 -2
- package/scripts/agent-operations-planner.js +1 -1
- package/scripts/agent-outcome-eval.js +1 -1
- package/scripts/agent-reliability-client.js +148 -0
- package/scripts/apple-minions-idle-compute-steal.js +428 -0
- package/scripts/autoresearch-runner.js +20 -1
- package/scripts/billing.js +0 -1
- package/scripts/cli-demo.js +505 -1
- package/scripts/cli-schema.js +36 -0
- package/scripts/colab-compute-honesty.js +93 -0
- package/scripts/corporate-email-isolation-diode.js +299 -0
- package/scripts/coverage-gap.js +396 -0
- package/scripts/dashboard-chat.js +3 -3
- package/scripts/dashboard.js +2 -2
- package/scripts/decisive-step-doctor.js +256 -0
- package/scripts/decisive-step-evaluator.js +390 -0
- package/scripts/entitlement.js +18 -7
- package/scripts/experiment-tracker.js +5 -3
- package/scripts/feedback-loop.js +26 -3
- package/scripts/fiddler-control-plane-honesty.js +303 -0
- package/scripts/financial-control-plane.js +10 -2
- package/scripts/gates-engine.js +3 -1
- package/scripts/github-license-compliance-diode.js +378 -0
- package/scripts/halo-trace-optimizer.js +631 -0
- package/scripts/harness-ceiling-doctor.js +326 -0
- package/scripts/hf-papers.js +1 -1
- package/scripts/human-escalation.js +7 -1
- package/scripts/hybrid-feedback-context.js +53 -7
- package/scripts/infoq-architect-honesty.js +362 -0
- package/scripts/lesson-graph.js +785 -0
- package/scripts/lesson-inference.js +1 -1
- package/scripts/lesson-retrieval.js +26 -18
- package/scripts/license.js +122 -21
- package/scripts/llm-behavior-monitor.js +1 -1
- package/scripts/llm-obs-honesty.js +52 -0
- package/scripts/local-model-profile.js +11 -7
- package/scripts/mailer/resend-mailer.js +6 -3
- package/scripts/mcp-transport-strategy.js +1 -1
- package/scripts/meko-datapack-honesty.js +414 -0
- package/scripts/migrate-lesson-graph.js +442 -0
- package/scripts/mintlify-knowledge-honesty.js +647 -0
- package/scripts/model-distress-detector.js +207 -0
- package/scripts/model-distress-doctor.js +368 -0
- package/scripts/multi-harness-invariants.js +305 -0
- package/scripts/omp-coordination-honesty.js +488 -0
- package/scripts/pro-local-dashboard.js +20 -3
- package/scripts/prompt-eval.js +1 -1
- package/scripts/prove-herdr-adapter.js +367 -3
- package/scripts/rag-embedding-identity.js +1 -1
- package/scripts/rrsi-regularizer.js +531 -0
- package/scripts/saas-moat-honesty.js +443 -0
- package/scripts/salt-code-policy-honesty.js +1204 -0
- package/scripts/secret-redaction.js +58 -0
- package/scripts/session-export-gate.js +368 -0
- package/scripts/siren-mcp-governance.js +539 -0
- package/scripts/strands-box-doctor.js +130 -0
- package/scripts/strands-harness-doctor.js +390 -0
- package/scripts/stripe-revenue-catalog-audit.js +12 -0
- package/scripts/stripe-revenue-catalog.js +40 -0
- package/scripts/switchyard-router.js +153 -0
- package/scripts/tailscale-p2p-doctor.js +252 -0
- package/scripts/task-outcomes.js +148 -3
- package/scripts/thumbgate-board-loop.js +4 -2
- package/scripts/thumbgate-daily-discoveries-publish.js +86 -38
- package/scripts/triage-savings-calculator.js +181 -0
- package/scripts/vector-store.js +41 -2
- package/scripts/vulnerability-pre-action-diode.js +319 -0
- package/scripts/windbg-evidence-diagnostics.js +241 -0
- package/scripts/workflow-intake-queue.js +1 -1
- package/scripts/workspace-agent-routines.js +534 -6
- package/scripts/workspace-evolver.js +143 -13
- package/server.json +2 -2
- package/skills/aws-strands-harness-not-clone/SKILL.md +53 -0
- package/skills/siren-mcp-governance/SKILL.md +50 -0
- package/src/api/server.js +191 -5
- package/src/mcp-writeguard.js +1 -1
- package/src/session-interactivity.js +284 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// Rebuild the optional graph from JSONL without modifying source records.
|
|
5
|
+
// Existing SQLite state is backed up with VACUUM INTO before the atomic rebuild.
|
|
6
|
+
// Only complete, equal scopes can cluster or acquire lineage/contradiction edges.
|
|
7
|
+
|
|
8
|
+
const fs = require('node:fs');
|
|
9
|
+
const path = require('node:path');
|
|
10
|
+
const { readTextTail } = require('./fs-utils');
|
|
11
|
+
const { canonicalHash, canonicalizeText } = require('./lesson-canonical');
|
|
12
|
+
const {
|
|
13
|
+
initGraphDB,
|
|
14
|
+
upsertNode,
|
|
15
|
+
addEdge,
|
|
16
|
+
scopeKey, sameScope, sameScopeIdentity,
|
|
17
|
+
extractCorrectionTarget,
|
|
18
|
+
lessonSignal,
|
|
19
|
+
lessonText,
|
|
20
|
+
DUPLICATE_SIMILARITY_THRESHOLD,
|
|
21
|
+
CONTRADICTION_SIMILARITY_THRESHOLD,
|
|
22
|
+
} = require('./lesson-graph');
|
|
23
|
+
|
|
24
|
+
// Bound the O(n^2) fuzzy passes. At the observed store sizes (hundreds to a
|
|
25
|
+
// few thousand rows) this completes in seconds; beyond the cap we keep the
|
|
26
|
+
// exact-hash clustering (which is O(n)) and skip only the fuzzy pass.
|
|
27
|
+
const MAX_FUZZY_RECORDS = 5000;
|
|
28
|
+
|
|
29
|
+
function parseArgs(argv) {
|
|
30
|
+
const args = { dryRun: false, json: false, feedbackDir: null };
|
|
31
|
+
for (let i = 0; i < argv.length; i++) {
|
|
32
|
+
const arg = argv[i];
|
|
33
|
+
if (arg === '--dry-run') args.dryRun = true;
|
|
34
|
+
else if (arg === '--json') args.json = true;
|
|
35
|
+
else if (arg === '--feedback-dir') args.feedbackDir = argv[++i];
|
|
36
|
+
else if (arg.startsWith('--feedback-dir=')) args.feedbackDir = arg.split('=')[1];
|
|
37
|
+
else if (arg === '--help' || arg === '-h') args.help = true;
|
|
38
|
+
}
|
|
39
|
+
return args;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function bigramSet(text) {
|
|
43
|
+
const normalized = String(text || '')
|
|
44
|
+
.toLowerCase()
|
|
45
|
+
.replace(/[^a-z0-9\s]/g, ' ')
|
|
46
|
+
.replace(/\s+/g, ' ')
|
|
47
|
+
.trim();
|
|
48
|
+
const set = new Set();
|
|
49
|
+
for (let i = 0; i < normalized.length - 1; i++) set.add(normalized.slice(i, i + 2));
|
|
50
|
+
return set;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function bigramJaccard(a, b) {
|
|
54
|
+
if (a.size === 0 && b.size === 0) return 1;
|
|
55
|
+
if (a.size === 0 || b.size === 0) return 0;
|
|
56
|
+
let intersection = 0;
|
|
57
|
+
for (const item of a) if (b.has(item)) intersection++;
|
|
58
|
+
const union = a.size + b.size - intersection;
|
|
59
|
+
return union === 0 ? 0 : intersection / union;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Normalize one store record (memory record OR feedback event) into the
|
|
64
|
+
* planning shape. Feedback events have no title; their context is the text.
|
|
65
|
+
*/
|
|
66
|
+
function toPlanRecord(record, source) {
|
|
67
|
+
if (!record || !record.id) return null;
|
|
68
|
+
const text = lessonText(record);
|
|
69
|
+
return {
|
|
70
|
+
id: record.id,
|
|
71
|
+
...require('./memory-scope-readiness').normalizeScope(record),
|
|
72
|
+
source, // 'memory' | 'feedback'
|
|
73
|
+
sourceFeedbackId: record.sourceFeedbackId || null,
|
|
74
|
+
title: record.title || record.context || null,
|
|
75
|
+
signal: lessonSignal(record),
|
|
76
|
+
text,
|
|
77
|
+
canonicalText: canonicalizeText(text),
|
|
78
|
+
hash: canonicalHash(record) || null,
|
|
79
|
+
timestamp: record.timestamp || record.receivedAt || null,
|
|
80
|
+
occurrences: Number.isFinite(record.occurrences) && record.occurrences > 0
|
|
81
|
+
? Math.floor(record.occurrences)
|
|
82
|
+
: 1,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Cluster records into duplicate groups: union by canonical hash first, then
|
|
88
|
+
* merge near-identical canonical texts (>= DUPLICATE_SIMILARITY_THRESHOLD)
|
|
89
|
+
* within the same signal. Canonical member = earliest timestamp (the store's
|
|
90
|
+
* first sighting), matching the existing merge semantics in feedback-loop.
|
|
91
|
+
*/
|
|
92
|
+
function clusterDuplicates(records) {
|
|
93
|
+
const parent = new Map();
|
|
94
|
+
const find = (x) => {
|
|
95
|
+
let root = x;
|
|
96
|
+
while (parent.get(root) !== root) root = parent.get(root);
|
|
97
|
+
let cursor = x;
|
|
98
|
+
while (parent.get(cursor) !== cursor) {
|
|
99
|
+
const next = parent.get(cursor);
|
|
100
|
+
parent.set(cursor, root);
|
|
101
|
+
cursor = next;
|
|
102
|
+
}
|
|
103
|
+
return root;
|
|
104
|
+
};
|
|
105
|
+
const union = (a, b) => {
|
|
106
|
+
const ra = find(a);
|
|
107
|
+
const rb = find(b);
|
|
108
|
+
if (ra !== rb) parent.set(ra, rb);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
for (const rec of records) parent.set(rec.id, rec.id);
|
|
112
|
+
|
|
113
|
+
// Pass 1 — exact canonical-hash groups (per signal).
|
|
114
|
+
const byHash = new Map();
|
|
115
|
+
for (const rec of records) {
|
|
116
|
+
if (!rec.hash) continue;
|
|
117
|
+
if (!scopeKey(rec)) continue;
|
|
118
|
+
const key = scopeKey(rec) + ':' + rec.signal + ':' + rec.hash;
|
|
119
|
+
if (byHash.has(key)) union(rec.id, byHash.get(key));
|
|
120
|
+
else byHash.set(key, rec.id);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Pass 1b — exact canonical-text groups (hash can differ across schemas).
|
|
124
|
+
const byText = new Map();
|
|
125
|
+
for (const rec of records) {
|
|
126
|
+
if (!rec.canonicalText) continue;
|
|
127
|
+
if (!scopeKey(rec)) continue;
|
|
128
|
+
const key = scopeKey(rec) + ':' + rec.signal + ':' + rec.canonicalText;
|
|
129
|
+
if (byText.has(key)) union(rec.id, byText.get(key));
|
|
130
|
+
else byText.set(key, rec.id);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Pass 2 — fuzzy near-duplicates within the same signal (bounded).
|
|
134
|
+
if (records.length <= MAX_FUZZY_RECORDS) {
|
|
135
|
+
const withGrams = records
|
|
136
|
+
.filter((r) => r.canonicalText && r.canonicalText.length >= 8)
|
|
137
|
+
.map((r) => ({ rec: r, grams: bigramSet(r.canonicalText) }));
|
|
138
|
+
for (let i = 0; i < withGrams.length; i++) {
|
|
139
|
+
for (let j = i + 1; j < withGrams.length; j++) {
|
|
140
|
+
const a = withGrams[i];
|
|
141
|
+
const b = withGrams[j];
|
|
142
|
+
if (a.rec.signal !== b.rec.signal || !sameScope(a.rec, b.rec)) continue;
|
|
143
|
+
if (find(a.rec.id) === find(b.rec.id)) continue;
|
|
144
|
+
if (bigramJaccard(a.grams, b.grams) >= DUPLICATE_SIMILARITY_THRESHOLD) {
|
|
145
|
+
union(a.rec.id, b.rec.id);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const byId = new Map(records.map((r) => [r.id, r]));
|
|
152
|
+
const clusters = new Map();
|
|
153
|
+
for (const rec of records) {
|
|
154
|
+
const root = find(rec.id);
|
|
155
|
+
if (!clusters.has(root)) clusters.set(root, []);
|
|
156
|
+
clusters.get(root).push(rec);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const result = [];
|
|
160
|
+
for (const members of clusters.values()) {
|
|
161
|
+
members.sort((a, b) => {
|
|
162
|
+
const ta = a.timestamp ? new Date(a.timestamp).getTime() : Infinity;
|
|
163
|
+
const tb = b.timestamp ? new Date(b.timestamp).getTime() : Infinity;
|
|
164
|
+
return ta - tb;
|
|
165
|
+
});
|
|
166
|
+
// Prefer a memory record as canonical when the cluster mixes sources —
|
|
167
|
+
// memory records carry the promoted content retrieval actually uses.
|
|
168
|
+
const canonical = members.find((m) => m.source === 'memory') || members[0];
|
|
169
|
+
result.push({ canonical, members });
|
|
170
|
+
}
|
|
171
|
+
return { clusters: result, byId };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function readCompleteJsonl(filePath) {
|
|
175
|
+
let source;
|
|
176
|
+
try {
|
|
177
|
+
source = readTextTail(filePath, 0);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error.code === 'ENOENT') return [];
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
if (source.truncated) throw new Error('lesson-graph: complete JSONL read required for ' + filePath);
|
|
183
|
+
return source.text.split('\n').filter(line => line.trim()).map(line => JSON.parse(line));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Build the full migration plan (pure — no writes).
|
|
188
|
+
*/
|
|
189
|
+
function buildPlan(feedbackDir) {
|
|
190
|
+
const memoryLogPath = path.join(feedbackDir, 'memory-log.jsonl');
|
|
191
|
+
const feedbackLogPath = path.join(feedbackDir, 'feedback-log.jsonl');
|
|
192
|
+
|
|
193
|
+
const memoryRecords = readCompleteJsonl(memoryLogPath)
|
|
194
|
+
.map((r) => toPlanRecord(r, 'memory'))
|
|
195
|
+
.filter(Boolean);
|
|
196
|
+
const memoryFeedbackIds = new Set(
|
|
197
|
+
memoryRecords.filter(r => r.sourceFeedbackId && scopeKey(r)).map(r => scopeKey(r) + ':' + r.sourceFeedbackId),
|
|
198
|
+
);
|
|
199
|
+
// Feedback events already promoted into a memory record are represented by
|
|
200
|
+
// that record; only unpromoted events need their own nodes for clustering.
|
|
201
|
+
const feedbackRecords = readCompleteJsonl(feedbackLogPath)
|
|
202
|
+
.filter((r) => r && r.id && !memoryFeedbackIds.has(scopeKey(r) + ':' + r.id))
|
|
203
|
+
.map((r) => toPlanRecord(r, 'feedback'))
|
|
204
|
+
.filter(Boolean);
|
|
205
|
+
|
|
206
|
+
const byIdentity = new Map();
|
|
207
|
+
for (const record of [...memoryRecords, ...feedbackRecords]) {
|
|
208
|
+
const existing = byIdentity.get(record.id);
|
|
209
|
+
if (existing && !sameScopeIdentity(existing, record)) throw new Error('lesson-graph: duplicate id across scopes');
|
|
210
|
+
if (!existing) byIdentity.set(record.id, record);
|
|
211
|
+
}
|
|
212
|
+
const all = [...byIdentity.values()];
|
|
213
|
+
const { clusters, byId } = clusterDuplicates(all);
|
|
214
|
+
|
|
215
|
+
const duplicateEdges = [];
|
|
216
|
+
const canonicalById = new Map();
|
|
217
|
+
let collapsedRecords = 0;
|
|
218
|
+
let largestCluster = { size: 0, title: null };
|
|
219
|
+
for (const { canonical, members } of clusters) {
|
|
220
|
+
const clusterCount = members.reduce((sum, m) => sum + m.occurrences, 0);
|
|
221
|
+
canonical.duplicateCount = clusterCount;
|
|
222
|
+
if (clusterCount > largestCluster.size) {
|
|
223
|
+
largestCluster = {
|
|
224
|
+
size: clusterCount,
|
|
225
|
+
title: (canonical.title || canonical.text || '').slice(0, 80),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
for (const member of members) {
|
|
229
|
+
canonicalById.set(member.id, canonical.id);
|
|
230
|
+
if (member.id === canonical.id) continue;
|
|
231
|
+
collapsedRecords += 1;
|
|
232
|
+
duplicateEdges.push({
|
|
233
|
+
src: member.id,
|
|
234
|
+
dst: canonical.id,
|
|
235
|
+
type: 'duplicate_of',
|
|
236
|
+
validFrom: member.timestamp || canonical.timestamp || new Date().toISOString(),
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Supersession chains from CORRECTION/REFINEMENT markers.
|
|
242
|
+
const lineageEdges = [];
|
|
243
|
+
const unresolvedRefs = [];
|
|
244
|
+
const resolveRef = (ref, selfId) => {
|
|
245
|
+
if (byId.has(ref) && ref !== selfId && sameScope(byId.get(ref), byId.get(selfId))) return ref;
|
|
246
|
+
const matches = [];
|
|
247
|
+
for (const rec of all) {
|
|
248
|
+
if (rec.id === selfId || !sameScope(rec, byId.get(selfId))) continue;
|
|
249
|
+
if (rec.id.startsWith(ref) || (rec.sourceFeedbackId && rec.sourceFeedbackId.startsWith(ref))) {
|
|
250
|
+
matches.push(rec.id);
|
|
251
|
+
if (matches.length > 1) break;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return matches.length === 1 ? matches[0] : null;
|
|
255
|
+
};
|
|
256
|
+
for (const rec of all) {
|
|
257
|
+
const correction = extractCorrectionTarget(rec.text);
|
|
258
|
+
if (!correction) continue;
|
|
259
|
+
const targetId = canonicalById.get(resolveRef(correction.targetRef, rec.id));
|
|
260
|
+
if (!targetId) {
|
|
261
|
+
unresolvedRefs.push({ id: rec.id, ref: correction.targetRef });
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
lineageEdges.push({
|
|
265
|
+
src: rec.id,
|
|
266
|
+
dst: targetId,
|
|
267
|
+
type: correction.edgeType,
|
|
268
|
+
validFrom: rec.timestamp || new Date().toISOString(),
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Contradiction candidates among cluster canonicals: same topic, opposite
|
|
273
|
+
// signal, not already related through lineage or duplication.
|
|
274
|
+
const related = new Set();
|
|
275
|
+
for (const e of [...duplicateEdges, ...lineageEdges]) {
|
|
276
|
+
related.add(e.src + '|' + e.dst);
|
|
277
|
+
related.add(e.dst + '|' + e.src);
|
|
278
|
+
}
|
|
279
|
+
const contradictionEdges = [];
|
|
280
|
+
const canonicals = clusters
|
|
281
|
+
.map((c) => c.canonical)
|
|
282
|
+
.filter((r) => r.canonicalText && r.canonicalText.length >= 8);
|
|
283
|
+
if (canonicals.length <= MAX_FUZZY_RECORDS) {
|
|
284
|
+
const withGrams = canonicals.map((r) => ({ rec: r, grams: bigramSet(r.canonicalText) }));
|
|
285
|
+
for (let i = 0; i < withGrams.length; i++) {
|
|
286
|
+
for (let j = i + 1; j < withGrams.length; j++) {
|
|
287
|
+
const a = withGrams[i].rec;
|
|
288
|
+
const b = withGrams[j].rec;
|
|
289
|
+
if (a.signal === b.signal || !sameScope(a, b)) continue;
|
|
290
|
+
if (related.has(a.id + '|' + b.id)) continue;
|
|
291
|
+
const sim = bigramJaccard(withGrams[i].grams, withGrams[j].grams);
|
|
292
|
+
if (sim < CONTRADICTION_SIMILARITY_THRESHOLD) continue;
|
|
293
|
+
const aTime = a.timestamp ? new Date(a.timestamp).getTime() : -Infinity;
|
|
294
|
+
const bTime = b.timestamp ? new Date(b.timestamp).getTime() : -Infinity;
|
|
295
|
+
const newer = aTime >= bTime ? a : b;
|
|
296
|
+
const older = aTime >= bTime ? b : a;
|
|
297
|
+
contradictionEdges.push({
|
|
298
|
+
src: newer.id,
|
|
299
|
+
dst: older.id,
|
|
300
|
+
type: 'contradicts',
|
|
301
|
+
validFrom: newer.timestamp || new Date().toISOString(),
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return {
|
|
308
|
+
feedbackDir,
|
|
309
|
+
memoryLogPath,
|
|
310
|
+
feedbackLogPath,
|
|
311
|
+
records: all,
|
|
312
|
+
clusters,
|
|
313
|
+
stats: {
|
|
314
|
+
memoryRecords: memoryRecords.length,
|
|
315
|
+
feedbackEvents: feedbackRecords.length,
|
|
316
|
+
totalRecords: all.length,
|
|
317
|
+
clusters: clusters.length,
|
|
318
|
+
duplicateClustersCollapsed: clusters.filter((c) => c.members.length > 1).length,
|
|
319
|
+
recordsCollapsedIntoCanonicals: collapsedRecords,
|
|
320
|
+
largestCluster,
|
|
321
|
+
supersessionEdges: lineageEdges.filter((e) => e.type === 'supersedes').length,
|
|
322
|
+
refinementEdges: lineageEdges.filter((e) => e.type === 'refines').length,
|
|
323
|
+
contradictionEdges: contradictionEdges.length,
|
|
324
|
+
unresolvedCorrectionRefs: unresolvedRefs.length,
|
|
325
|
+
},
|
|
326
|
+
duplicateEdges,
|
|
327
|
+
lineageEdges,
|
|
328
|
+
contradictionEdges,
|
|
329
|
+
unresolvedRefs,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function backupExistingGraph(graphDbPath) {
|
|
334
|
+
if (!fs.existsSync(graphDbPath)) return null;
|
|
335
|
+
const backupPath = graphDbPath + '.bak-' + require('node:crypto').randomUUID();
|
|
336
|
+
const Database = require('better-sqlite3');
|
|
337
|
+
const db = new Database(graphDbPath, { fileMustExist: true });
|
|
338
|
+
try {
|
|
339
|
+
db.prepare('VACUUM INTO ?').run(backupPath);
|
|
340
|
+
} finally {
|
|
341
|
+
db.close();
|
|
342
|
+
}
|
|
343
|
+
return backupPath;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function applyPlan(plan, graphDbPath) {
|
|
347
|
+
const backupPath = backupExistingGraph(graphDbPath);
|
|
348
|
+
const db = initGraphDB(graphDbPath);
|
|
349
|
+
const ingestedAt = new Date().toISOString();
|
|
350
|
+
|
|
351
|
+
const canonicalIds = new Map();
|
|
352
|
+
for (const { canonical, members } of plan.clusters) {
|
|
353
|
+
for (const member of members) {
|
|
354
|
+
if (member.id !== canonical.id) canonicalIds.set(member.id, canonical.id);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const run = db.transaction(() => {
|
|
359
|
+
db.exec('DELETE FROM lesson_edges; DELETE FROM lesson_nodes;');
|
|
360
|
+
for (const rec of plan.records) {
|
|
361
|
+
upsertNode(db, {
|
|
362
|
+
id: rec.id,
|
|
363
|
+
...require('./memory-scope-readiness').normalizeScope(rec),
|
|
364
|
+
sourceFeedbackId: rec.sourceFeedbackId,
|
|
365
|
+
canonicalId: canonicalIds.get(rec.id) || null,
|
|
366
|
+
title: rec.title || (rec.text || '').slice(0, 200),
|
|
367
|
+
signal: rec.signal,
|
|
368
|
+
canonicalHash: rec.hash,
|
|
369
|
+
canonicalText: rec.canonicalText,
|
|
370
|
+
duplicateCount: rec.duplicateCount || 1,
|
|
371
|
+
validFrom: rec.timestamp || ingestedAt,
|
|
372
|
+
ingestedAt,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
for (const edge of plan.duplicateEdges) addEdge(db, { ...edge, ingestedAt });
|
|
376
|
+
for (const edge of plan.lineageEdges) addEdge(db, { ...edge, ingestedAt });
|
|
377
|
+
for (const edge of plan.contradictionEdges) addEdge(db, { ...edge, ingestedAt });
|
|
378
|
+
});
|
|
379
|
+
run();
|
|
380
|
+
|
|
381
|
+
const nodeCount = db.prepare('SELECT COUNT(*) AS c FROM lesson_nodes').get().c;
|
|
382
|
+
const edgeCounts = db.prepare(
|
|
383
|
+
'SELECT type, COUNT(*) AS c FROM lesson_edges GROUP BY type ORDER BY type',
|
|
384
|
+
).all();
|
|
385
|
+
db.close();
|
|
386
|
+
|
|
387
|
+
return { backupPath, graphDbPath, nodeCount, edgeCounts };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function main() {
|
|
391
|
+
const args = parseArgs(process.argv.slice(2));
|
|
392
|
+
if (args.help) {
|
|
393
|
+
console.log('Usage: node scripts/migrate-lesson-graph.js [--dry-run] [--feedback-dir <dir>] [--json]');
|
|
394
|
+
return 0;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const { resolveFeedbackDir } = require('./feedback-paths');
|
|
398
|
+
const feedbackDir = args.feedbackDir || resolveFeedbackDir({});
|
|
399
|
+
if (!fs.existsSync(path.join(feedbackDir, 'memory-log.jsonl'))
|
|
400
|
+
&& !fs.existsSync(path.join(feedbackDir, 'feedback-log.jsonl'))) {
|
|
401
|
+
console.error('migrate-lesson-graph: no lesson store found in ' + feedbackDir);
|
|
402
|
+
return 1;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const plan = buildPlan(feedbackDir);
|
|
406
|
+
const graphDbPath = path.join(feedbackDir, 'lesson-graph.sqlite');
|
|
407
|
+
|
|
408
|
+
if (args.dryRun) {
|
|
409
|
+
const output = {
|
|
410
|
+
mode: 'dry-run',
|
|
411
|
+
feedbackDir,
|
|
412
|
+
graphDbPath,
|
|
413
|
+
wouldWrite: [graphDbPath],
|
|
414
|
+
stats: plan.stats,
|
|
415
|
+
sampleChains: plan.lineageEdges.slice(0, 5),
|
|
416
|
+
sampleContradictions: plan.contradictionEdges.slice(0, 5),
|
|
417
|
+
};
|
|
418
|
+
console.log(args.json ? JSON.stringify(output) : JSON.stringify(output, null, 2));
|
|
419
|
+
return 0;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const applied = applyPlan(plan, graphDbPath);
|
|
423
|
+
const output = {
|
|
424
|
+
mode: 'apply',
|
|
425
|
+
feedbackDir,
|
|
426
|
+
graphDbPath: applied.graphDbPath,
|
|
427
|
+
backupPath: applied.backupPath,
|
|
428
|
+
stats: plan.stats,
|
|
429
|
+
graph: {
|
|
430
|
+
nodes: applied.nodeCount,
|
|
431
|
+
edges: applied.edgeCounts,
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
console.log(args.json ? JSON.stringify(output) : JSON.stringify(output, null, 2));
|
|
435
|
+
return 0;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (path.resolve(process.argv[1] || '') === path.resolve(__filename)) {
|
|
439
|
+
process.exitCode = main();
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
module.exports = { buildPlan, applyPlan, clusterDuplicates, toPlanRecord, parseArgs };
|