pi-mega-compact 0.20.6 → 0.20.8
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/config/vector-cortex.js +12 -0
- package/dist/config.js +1 -1
- package/dist/extensions/dashboard-server/api-contracts/vector-cortex-heal.js +11 -0
- package/dist/extensions/dashboard-server/route-dispatch.js +102 -0
- package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +3 -32
- package/dist/extensions/dashboard-server/routes-rag-settings-types.js +11 -0
- package/dist/extensions/dashboard-server/routes-rag-settings-vector-cortex.js +44 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-heal.js +47 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex.js +1 -0
- package/dist/extensions/dashboard-server/routes.js +1 -1
- package/dist/extensions/dashboard-server/server.js +3 -82
- package/dist/src/config/dedup.js +1 -0
- package/dist/src/config/vector-cortex.js +12 -0
- package/dist/src/config.js +1 -1
- package/dist/src/dedup-audit.test-helpers.js +43 -0
- package/dist/src/monitoring.js +9 -0
- package/dist/src/vector-cortex/heal/_acceptance-fixture.js +56 -0
- package/dist/src/vector-cortex/heal/closure-metrics.js +66 -0
- package/dist/src/vector-cortex/heal/closure-opt.js +242 -0
- package/dist/src/vector-cortex/heal/emit.js +69 -0
- package/dist/src/vector-cortex/heal/proof.js +210 -0
- package/dist/src/vector-cortex/heal/types.js +42 -0
- package/dist/src/vectorStore/add-l0.js +108 -0
- package/dist/src/vectorStore/add-l1.js +35 -0
- package/dist/src/vectorStore/add.js +229 -0
- package/dist/src/vectorStore/class.js +15 -291
- package/dist/src/vectorStore/dedup-audit.js +104 -0
- package/dist/src/vectorStore.js +1 -0
- package/dist/vector-cortex/heal/_acceptance-fixture.js +56 -0
- package/dist/vector-cortex/heal/closure-metrics.js +66 -0
- package/dist/vector-cortex/heal/closure-opt.js +242 -0
- package/dist/vector-cortex/heal/emit.js +69 -0
- package/dist/vector-cortex/heal/proof.js +210 -0
- package/dist/vector-cortex/heal/types.js +42 -0
- package/extensions/dashboard-client/dist/assets/{AreaChart-DqTbaSRA.js → AreaChart-5yxaI8IO.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AreaChart-DqTbaSRA.js.map → AreaChart-5yxaI8IO.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{BarChart-BfNBeOvF.js → BarChart-BoHhCqH-.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{BarChart-BfNBeOvF.js.map → BarChart-BoHhCqH-.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-C9SOMYB5.js → CacheTab-Di6L3CsF.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-C9SOMYB5.js.map → CacheTab-Di6L3CsF.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-GsMKP10_.js → EventsTab-Cdff3h6s.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-GsMKP10_.js.map → EventsTab-Cdff3h6s.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-DAOCXwRP.js → HealthTab-MthYYVLw.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-DAOCXwRP.js.map → HealthTab-MthYYVLw.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-BtBeMkB5.js → MaintenanceTab-D9G4ntsG.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-BtBeMkB5.js.map → MaintenanceTab-D9G4ntsG.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-DzndtgkZ.js → MemoryMapTab-CN8Lr5Qf.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-DzndtgkZ.js.map → MemoryMapTab-CN8Lr5Qf.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-DyxJ4eb8.js → MetricsTab-CgO32kSo.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-DyxJ4eb8.js.map → MetricsTab-CgO32kSo.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-DCiEdVWR.js → OverviewTab-Cb4kR0h1.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-DCiEdVWR.js.map → OverviewTab-Cb4kR0h1.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-B5SIqVHA.js → ReposTab-Dt1FGnMR.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-B5SIqVHA.js.map → ReposTab-Dt1FGnMR.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-DLWFJ9xI.js → SessionsTab-D7D2RW6U.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-DLWFJ9xI.js.map → SessionsTab-D7D2RW6U.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SetupTab-CvGXZyej.js → SetupTab-CgV5xCzz.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SetupTab-CvGXZyej.js.map → SetupTab-CgV5xCzz.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-wZuQGxLS.js → TimeSavedCard-CCNuJjyO.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-wZuQGxLS.js.map → TimeSavedCard-CCNuJjyO.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-B7aOejch.js → TurnsTab-Cv1OCttJ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-B7aOejch.js.map → TurnsTab-Cv1OCttJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DTabQo6h.js +2 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DTabQo6h.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{WikiTab-DhWHjAtt.js → WikiTab-B48QnQrK.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{WikiTab-DhWHjAtt.js.map → WikiTab-B48QnQrK.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{button-BrC-W87h.js → button-BBQl2MiF.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{button-BrC-W87h.js.map → button-BBQl2MiF.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{card-0rgfRgr3.js → card-BYbefUur.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{card-0rgfRgr3.js.map → card-BYbefUur.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-DRresOQF.js → generateCategoricalChart-swm0YZe0.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-DRresOQF.js.map → generateCategoricalChart-swm0YZe0.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{index-DohmtZWL.js → index-DeIky7xm.js} +3 -3
- package/extensions/dashboard-client/dist/assets/{index-DohmtZWL.js.map → index-DeIky7xm.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{switch-Bs8g_zB5.js → switch-CctU78ww.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{switch-Bs8g_zB5.js.map → switch-CctU78ww.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{toggle-DVVauIo8.js → toggle-BRCNCoiI.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{toggle-DVVauIo8.js.map → toggle-BRCNCoiI.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-DxH1PTAR.js → useSSE-WfOiWDH_.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-DxH1PTAR.js.map → useSSE-WfOiWDH_.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +1 -1
- package/extensions/dashboard-client/src/api/vector-cortex.ts +9 -0
- package/extensions/dashboard-client/src/tabs/VectorCortexClosureCard.tsx +46 -0
- package/extensions/dashboard-client/src/tabs/VectorCortexLedgerCard.tsx +80 -0
- package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +12 -164
- package/extensions/dashboard-client/src/tabs/VectorCortexTopologyCard.tsx +131 -0
- package/extensions/dashboard-client/src/types/vector-cortex.ts +14 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex-heal.ts +43 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex.ts +2 -0
- package/extensions/dashboard-server/route-dispatch.ts +112 -0
- package/extensions/dashboard-server/routes-rag-settings-helpers.ts +11 -135
- package/extensions/dashboard-server/routes-rag-settings-types.ts +34 -0
- package/extensions/dashboard-server/routes-rag-settings-vector-cortex.ts +148 -0
- package/extensions/dashboard-server/routes-vector-cortex-heal.ts +56 -0
- package/extensions/dashboard-server/routes-vector-cortex.ts +1 -0
- package/extensions/dashboard-server/routes.ts +1 -0
- package/extensions/dashboard-server/server.ts +3 -86
- package/package.json +1 -1
- package/src/config/dedup.ts +9 -0
- package/src/config/vector-cortex.ts +13 -0
- package/src/config.ts +1 -0
- package/src/dedup-audit.test-helpers.ts +49 -0
- package/src/monitoring.ts +10 -0
- package/src/vector-cortex/heal/_acceptance-fixture.ts +108 -0
- package/src/vector-cortex/heal/closure-metrics.ts +73 -0
- package/src/vector-cortex/heal/closure-opt.ts +266 -0
- package/src/vector-cortex/heal/emit.ts +93 -0
- package/src/vector-cortex/heal/proof.ts +234 -0
- package/src/vector-cortex/heal/types.ts +226 -0
- package/src/vectorStore/add-l0.ts +152 -0
- package/src/vectorStore/add-l1.ts +57 -0
- package/src/vectorStore/add.ts +269 -0
- package/src/vectorStore/class.ts +17 -327
- package/src/vectorStore/dedup-audit.ts +199 -0
- package/src/vectorStore.ts +1 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-eHVt9yDx.js +0 -2
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-eHVt9yDx.js.map +0 -1
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* add.ts — the dedup cascade behind VectorStore.add() (extracted from class.ts).
|
|
3
|
+
*
|
|
4
|
+
* Free function taking the store as its first argument, matching the sibling
|
|
5
|
+
* pattern used by vector-read.ts / vector-search.ts / vector-dedup.ts: the
|
|
6
|
+
* store's fields are package-public by design (see the comment in class.ts) so
|
|
7
|
+
* the helpers can read them without casts, and class.ts stays a thin shell.
|
|
8
|
+
*
|
|
9
|
+
* Cascade order (unchanged by the extraction):
|
|
10
|
+
* 1. L0 contentHash exact match (bloom-accelerated)
|
|
11
|
+
* 2. L0 legacy regionHash exact match (backward-compat)
|
|
12
|
+
* 3. L0 summaryHash exact match (same-topic incremental compactions)
|
|
13
|
+
* 4. L1 MinHash/LSH near-duplicate
|
|
14
|
+
* 5. L2 cosine similarity >= threshold
|
|
15
|
+
* 6. None matched → create a new checkpoint
|
|
16
|
+
*
|
|
17
|
+
* Sprint-14 monitoring (`store.record`) and the live `onTier` UI hook are
|
|
18
|
+
* untouched. The dedup audit trail (external-audit item #2) is layered on top as
|
|
19
|
+
* PURE instrumentation: every emit is best-effort and flag-gated, so a flag-off
|
|
20
|
+
* or a failing write leaves the dedup outcome byte-identical.
|
|
21
|
+
*/
|
|
22
|
+
import { cosineSimilarity } from "../embedder.js";
|
|
23
|
+
import type { StoredCheckpoint } from "../store.js";
|
|
24
|
+
import { normalizeSessionId, compressSmart } from "../store.js";
|
|
25
|
+
import { computeContentDigest } from "../dedup/digest.js";
|
|
26
|
+
import { minhashSignature, SIGNATURE_VERSION } from "../dedup/l1-minhash.js";
|
|
27
|
+
import { lshBands } from "../dedup/l1-lsh.js";
|
|
28
|
+
import { openBloom, saveBloom } from "../store/bloom.js";
|
|
29
|
+
import {
|
|
30
|
+
listCheckpoints,
|
|
31
|
+
nextCheckpointId,
|
|
32
|
+
upsertCheckpoint,
|
|
33
|
+
loadSessionState,
|
|
34
|
+
saveSessionState,
|
|
35
|
+
upsertMinhashSignature,
|
|
36
|
+
insertLshBuckets,
|
|
37
|
+
addTokensSaved,
|
|
38
|
+
bumpDedupStats,
|
|
39
|
+
} from "../store/sqlite.js";
|
|
40
|
+
import { computeRegionHash } from "./hash.js";
|
|
41
|
+
import { runL0Tier, computeSummaryHash } from "./add-l0.js";
|
|
42
|
+
import { findL1Duplicate } from "./add-l1.js";
|
|
43
|
+
import { dedupAuditRecorder } from "./dedup-audit.js";
|
|
44
|
+
import type { VectorStore } from "./class.js";
|
|
45
|
+
import type { AddInput, AddResult } from "./types.js";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Add a checkpoint, running the full dedup cascade.
|
|
49
|
+
*
|
|
50
|
+
* Returns the matched checkpoint with `deduped: true` when a tier collapsed the
|
|
51
|
+
* region, otherwise the newly created checkpoint with `deduped: false`.
|
|
52
|
+
*/
|
|
53
|
+
export function addCheckpoint(store: VectorStore, input: AddInput): AddResult {
|
|
54
|
+
const t0 = Date.now();
|
|
55
|
+
const sessionId = normalizeSessionId(input.sessionId);
|
|
56
|
+
const regionHash = computeRegionHash(input.regionText);
|
|
57
|
+
const all = listCheckpoints(sessionId, store.stateDir);
|
|
58
|
+
// Honest "tokens saved" base for this region. For a deduped add the whole
|
|
59
|
+
// original region is discarded (nothing new stored); for a new checkpoint
|
|
60
|
+
// we persist (orig − stored). Falls back to stored when orig is unknown.
|
|
61
|
+
const origTokens = input.originalTokenEstimate ?? input.tokenEstimate ?? 0;
|
|
62
|
+
const cfg = store.cfg;
|
|
63
|
+
// Live per-tier progress hook (Phase 1). Sync + optional; fired at each tier
|
|
64
|
+
// so the UI can paint "L0 ✓ → L1 ✓ → L2 0.91 → stored" during a compaction.
|
|
65
|
+
const onTier = input.onTier;
|
|
66
|
+
// Durable decision trail (external-audit item #2). Bound to this one cascade
|
|
67
|
+
// so each call site is a single line; a no-op when cfg.DEDUP_AUDIT is off.
|
|
68
|
+
const audit = dedupAuditRecorder(
|
|
69
|
+
{
|
|
70
|
+
stateDir: store.stateDir,
|
|
71
|
+
eventsPath: store.eventsPath,
|
|
72
|
+
auditEnabled: cfg.DEDUP_AUDIT,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
sessionId,
|
|
76
|
+
originalTokenEstimate: input.originalTokenEstimate,
|
|
77
|
+
tokenEstimate: input.tokenEstimate,
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
// Tracks whether a tier matched while in MARK_ONLY (record-but-don't-collapse),
|
|
81
|
+
// and which tier.
|
|
82
|
+
let markOnly: "L0" | "L1" | "L2" | null = null;
|
|
83
|
+
|
|
84
|
+
// L0 exact-match tier (contentHash / regionHash / summaryHash) — see add-l0.ts.
|
|
85
|
+
const digest = computeContentDigest(input.regionText);
|
|
86
|
+
const bloom = openBloom(store.stateDir);
|
|
87
|
+
const summaryHash = computeSummaryHash(input.topicSummary);
|
|
88
|
+
const l0 = runL0Tier({
|
|
89
|
+
store,
|
|
90
|
+
input,
|
|
91
|
+
all,
|
|
92
|
+
digest,
|
|
93
|
+
regionHash,
|
|
94
|
+
summaryHash,
|
|
95
|
+
origTokens,
|
|
96
|
+
bloom,
|
|
97
|
+
t0,
|
|
98
|
+
audit,
|
|
99
|
+
});
|
|
100
|
+
if (l0.result) return l0.result;
|
|
101
|
+
if (l0.markOnly) markOnly = l0.markOnly;
|
|
102
|
+
|
|
103
|
+
// 2b. L1 MinHash/LSH near-duplicate dedup (Sprint 11) — catches one-word
|
|
104
|
+
// edits / rewordings that L0's exact hash misses. Cheap LSH bucket
|
|
105
|
+
// retrieval → trigram verification (pg_trgm-equivalent) as the final gate.
|
|
106
|
+
// Gated by L1_ENABLED (Sprint 14); MARK_ONLY_L1 records but doesn't collapse.
|
|
107
|
+
onTier?.({ tier: "L1", status: "scanning" });
|
|
108
|
+
if (cfg.L1_ENABLED) {
|
|
109
|
+
const l1 = findL1Duplicate(store, sessionId, input.regionText, all);
|
|
110
|
+
if (l1 && !cfg.MARK_ONLY_L1) {
|
|
111
|
+
l1.timestamp = input.timestamp;
|
|
112
|
+
upsertCheckpoint(l1, store.stateDir);
|
|
113
|
+
bumpDedupStats(true, store.stateDir);
|
|
114
|
+
const r = { checkpoint: l1, deduped: true, reason: "l1MinHash" };
|
|
115
|
+
store.record("L1", "deduped", "l1MinHash", Date.now() - t0, 1, l1.checkpointId);
|
|
116
|
+
// Verify tier: the trigram gate is a boolean, so no score is invented.
|
|
117
|
+
audit.deduped("L1", l1.checkpointId, "l1MinHash");
|
|
118
|
+
onTier?.({ tier: "L1", status: "deduped", detail: "l1MinHash" });
|
|
119
|
+
return r;
|
|
120
|
+
}
|
|
121
|
+
if (l1 && cfg.MARK_ONLY_L1) markOnly = "L1";
|
|
122
|
+
}
|
|
123
|
+
onTier?.({ tier: "L1", status: "passed" });
|
|
124
|
+
|
|
125
|
+
// 3. L2 semantic dedup — catches near-identical / semantically-similar regions
|
|
126
|
+
// via cosine over the embedding. topicSummary is used for summaryHash dedup
|
|
127
|
+
// (tier 2); the vector index is keyed on the original region for backward-
|
|
128
|
+
// compat search semantics. Threshold from cfg (L2_COSINE trigram honest
|
|
129
|
+
// firing point). QA #13 timeout guard: if the O(n) scan exceeds the budget,
|
|
130
|
+
// degrade to "store without dedup this pass" so we never lose a checkpoint.
|
|
131
|
+
// Gated by L2_ENABLED (Sprint 14); MARK_ONLY_L2 records but doesn't collapse.
|
|
132
|
+
const SIMILARITY_BUDGET_MS = cfg.SIMILARITY_BUDGET_MS;
|
|
133
|
+
const simThreshold = store.l2Threshold; // from cfg.L2_COSINE (default 0.85 trigram)
|
|
134
|
+
const embedding = store.embedder.embed(input.regionText);
|
|
135
|
+
onTier?.({ tier: "L2", status: "scanning" });
|
|
136
|
+
if (cfg.L2_ENABLED && all.length > 0) {
|
|
137
|
+
const start = Date.now();
|
|
138
|
+
let timedOut = false;
|
|
139
|
+
const nearest = all.reduce(
|
|
140
|
+
(best, cp) => {
|
|
141
|
+
if (!timedOut && Date.now() - start > SIMILARITY_BUDGET_MS)
|
|
142
|
+
timedOut = true;
|
|
143
|
+
if (timedOut) return best;
|
|
144
|
+
const sim = cosineSimilarity(embedding, cp.embedding);
|
|
145
|
+
return sim > best.sim ? { checkpoint: cp, sim } : best;
|
|
146
|
+
},
|
|
147
|
+
{ checkpoint: all[0], sim: -1 },
|
|
148
|
+
);
|
|
149
|
+
if (!timedOut && nearest.sim >= simThreshold) {
|
|
150
|
+
if (!cfg.MARK_ONLY_L2) {
|
|
151
|
+
// Near-identical — update timestamp on existing checkpoint
|
|
152
|
+
nearest.checkpoint.timestamp = input.timestamp;
|
|
153
|
+
upsertCheckpoint(nearest.checkpoint, store.stateDir);
|
|
154
|
+
bumpDedupStats(true, store.stateDir);
|
|
155
|
+
// Deduped: whole original region discarded, nothing new stored.
|
|
156
|
+
addTokensSaved(origTokens, store.stateDir);
|
|
157
|
+
const r = {
|
|
158
|
+
checkpoint: nearest.checkpoint,
|
|
159
|
+
deduped: true,
|
|
160
|
+
reason: "contentSimilarity",
|
|
161
|
+
};
|
|
162
|
+
store.record("L2", "deduped", "contentSimilarity", Date.now() - t0, nearest.sim, nearest.checkpoint.checkpointId);
|
|
163
|
+
// The real cosine that cleared the threshold — the tuning datum.
|
|
164
|
+
audit.deduped(
|
|
165
|
+
"L2",
|
|
166
|
+
nearest.checkpoint.checkpointId,
|
|
167
|
+
"contentSimilarity",
|
|
168
|
+
nearest.sim,
|
|
169
|
+
);
|
|
170
|
+
onTier?.({
|
|
171
|
+
tier: "L2",
|
|
172
|
+
status: "deduped",
|
|
173
|
+
detail: nearest.sim.toFixed(2),
|
|
174
|
+
});
|
|
175
|
+
return r;
|
|
176
|
+
}
|
|
177
|
+
markOnly = "L2";
|
|
178
|
+
}
|
|
179
|
+
onTier?.({
|
|
180
|
+
tier: "L2",
|
|
181
|
+
status: "passed",
|
|
182
|
+
detail: `best ${nearest.sim.toFixed(2)}`,
|
|
183
|
+
});
|
|
184
|
+
// Near-miss: how close did we come to collapsing? Only emitted when the
|
|
185
|
+
// scan actually completed and scored a candidate — a timed-out scan has no
|
|
186
|
+
// honest best to report.
|
|
187
|
+
if (!timedOut && nearest.sim >= 0) {
|
|
188
|
+
audit.passed("L2", nearest.checkpoint.checkpointId, nearest.sim);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 4. Genuinely new — create checkpoint
|
|
193
|
+
const checkpointId = nextCheckpointId(sessionId, store.stateDir);
|
|
194
|
+
const checkpoint: StoredCheckpoint = {
|
|
195
|
+
checkpointId,
|
|
196
|
+
sessionId,
|
|
197
|
+
repoId: store.repoId,
|
|
198
|
+
summary: input.summary,
|
|
199
|
+
topicSummary: input.topicSummary,
|
|
200
|
+
summaryHash,
|
|
201
|
+
keyDecisions: input.keyDecisions ?? [],
|
|
202
|
+
nextSteps: input.nextSteps ?? [],
|
|
203
|
+
filesModified: input.filesModified ?? [],
|
|
204
|
+
tokenEstimate: input.tokenEstimate ?? 0,
|
|
205
|
+
originalTokenEstimate: input.originalTokenEstimate,
|
|
206
|
+
regionHash,
|
|
207
|
+
contentHash: digest.contentHash,
|
|
208
|
+
contentHash2: digest.contentHash2,
|
|
209
|
+
contentHashVersion: digest.contentHashVersion,
|
|
210
|
+
normalizedText: digest.normalizedText,
|
|
211
|
+
compressedOriginal: compressSmart(
|
|
212
|
+
Buffer.from(input.regionText, "utf-8"),
|
|
213
|
+
input.compressionPressure,
|
|
214
|
+
),
|
|
215
|
+
embedding,
|
|
216
|
+
timestamp: input.timestamp,
|
|
217
|
+
};
|
|
218
|
+
// Persistence is SQLite (store/sqlite.ts). upsertCheckpoint keeps the
|
|
219
|
+
// idempotent-by-id semantics the old JSON append implied.
|
|
220
|
+
upsertCheckpoint(checkpoint, store.stateDir);
|
|
221
|
+
// Cumulative "tokens saved" counter (per-repo SQLite meta). For a NEW
|
|
222
|
+
// checkpoint the saved amount is (original − stored); for a deduped add the
|
|
223
|
+
// whole original region is discarded (handled in the deduped return paths
|
|
224
|
+
// above). Survives sessions and travels with the repo.
|
|
225
|
+
const stored = input.tokenEstimate ?? 0;
|
|
226
|
+
addTokensSaved(Math.max(0, origTokens - stored), store.stateDir);
|
|
227
|
+
// L1: persist this checkpoint's MinHash signature + LSH buckets so future
|
|
228
|
+
// near-duplicate inserts can find it. Deterministic given the seed.
|
|
229
|
+
const sig = minhashSignature(input.regionText);
|
|
230
|
+
upsertMinhashSignature(
|
|
231
|
+
checkpointId,
|
|
232
|
+
sessionId,
|
|
233
|
+
SIGNATURE_VERSION,
|
|
234
|
+
sig,
|
|
235
|
+
store.stateDir,
|
|
236
|
+
);
|
|
237
|
+
insertLshBuckets(
|
|
238
|
+
checkpointId,
|
|
239
|
+
sessionId,
|
|
240
|
+
SIGNATURE_VERSION,
|
|
241
|
+
lshBands(sig, sessionId, SIGNATURE_VERSION),
|
|
242
|
+
store.stateDir,
|
|
243
|
+
);
|
|
244
|
+
// Bloom accelerator: record the new content_hash so a future add() can short-
|
|
245
|
+
// circuit the scan on a hit (still confirmed by the SELECT-based `all` above).
|
|
246
|
+
bloom.add(digest.contentHash);
|
|
247
|
+
saveBloom(store.stateDir);
|
|
248
|
+
|
|
249
|
+
// Track the region hash in session state for fast sentinel checks.
|
|
250
|
+
const state = loadSessionState(sessionId, store.stateDir);
|
|
251
|
+
if (!state.storedRegionHashes.includes(regionHash)) {
|
|
252
|
+
state.storedRegionHashes.push(regionHash);
|
|
253
|
+
saveSessionState(sessionId, state, store.stateDir);
|
|
254
|
+
}
|
|
255
|
+
// A new checkpoint. If a tier matched while MARK_ONLY, record that (the
|
|
256
|
+
// decision fired but we intentionally did not collapse).
|
|
257
|
+
if (markOnly) {
|
|
258
|
+
store.record(markOnly, "mark_only", "mark_only", Date.now() - t0);
|
|
259
|
+
} else {
|
|
260
|
+
store.record("L0", "new", undefined, Date.now() - t0);
|
|
261
|
+
}
|
|
262
|
+
// Final outcome. `mark_only` distinguishes "a tier matched but policy said
|
|
263
|
+
// store anyway" from a genuinely novel region.
|
|
264
|
+
audit.stored(checkpointId, markOnly ? "mark_only" : "new", stored);
|
|
265
|
+
// Cumulative store-wide dedup accounting (attempt, not collapsed).
|
|
266
|
+
bumpDedupStats(false, store.stateDir);
|
|
267
|
+
onTier?.({ tier: "new", status: "stored" });
|
|
268
|
+
return { checkpoint, deduped: false };
|
|
269
|
+
}
|
package/src/vectorStore/class.ts
CHANGED
|
@@ -7,9 +7,8 @@
|
|
|
7
7
|
* Backed by the gzipped on-disk checkpoint files (store.ts). Similarity is a
|
|
8
8
|
* linear cosine scan — checkpoint counts are small, so no ANN index is needed.
|
|
9
9
|
*/
|
|
10
|
-
import { createHash } from "node:crypto";
|
|
11
10
|
import type { Embedder } from "../embedder.js";
|
|
12
|
-
import {
|
|
11
|
+
import { defaultEmbedder } from "../embedder.js";
|
|
13
12
|
import {
|
|
14
13
|
loadDedupConfig,
|
|
15
14
|
type DedupConfigShape,
|
|
@@ -17,47 +16,28 @@ import {
|
|
|
17
16
|
} from "../config/dedup.js";
|
|
18
17
|
import { logDecision } from "../monitoring.js";
|
|
19
18
|
import { repoKey } from "../store/repoKey.js";
|
|
20
|
-
import
|
|
21
|
-
import { getStateDir, normalizeSessionId, compressSmart } from "../store.js";
|
|
22
|
-
import { computeContentDigest } from "../dedup/digest.js";
|
|
19
|
+
import { getStateDir } from "../store.js";
|
|
23
20
|
import type { RaptorTree } from "../dedup/raptor/tree.js";
|
|
24
|
-
import {
|
|
25
|
-
minhashSignature,
|
|
26
|
-
SIGNATURE_VERSION,
|
|
27
|
-
NUM_HASHES,
|
|
28
|
-
} from "../dedup/l1-minhash.js";
|
|
29
|
-
import { lshBands } from "../dedup/l1-lsh.js";
|
|
30
|
-
import { isNearDuplicate } from "../dedup/l1-verify.js";
|
|
31
|
-
import { openBloom, saveBloom } from "../store/bloom.js";
|
|
32
|
-
import {
|
|
33
|
-
listCheckpoints,
|
|
34
|
-
nextCheckpointId,
|
|
35
|
-
upsertCheckpoint,
|
|
36
|
-
loadSessionState,
|
|
37
|
-
saveSessionState,
|
|
38
|
-
upsertMinhashSignature,
|
|
39
|
-
insertLshBuckets,
|
|
40
|
-
lshCandidateChunks,
|
|
41
|
-
addTokensSaved,
|
|
42
|
-
bumpDedupStats,
|
|
43
|
-
} from "../store/sqlite.js";
|
|
21
|
+
import { openBloom } from "../store/bloom.js";
|
|
44
22
|
import { migrateJsonToSqlite } from "../store/migrate.js";
|
|
45
|
-
import {
|
|
23
|
+
import { addCheckpoint } from "./add.js";
|
|
46
24
|
import type { AddInput, AddResult } from "./types.js";
|
|
47
25
|
|
|
48
26
|
export class VectorStore {
|
|
49
27
|
// These fields are `readonly` (set once in the constructor) but NOT private:
|
|
50
|
-
// the read/search/dedup helpers split into
|
|
51
|
-
// and vector-dedup.ts access them directly. Marking them
|
|
52
|
-
// ugly `as unknown as` casts in those modules; keeping
|
|
53
|
-
// makes VectorStore a thin barrel whose helpers live in
|
|
28
|
+
// the add/read/search/dedup helpers split into add.ts, vector-read.ts,
|
|
29
|
+
// vector-search.ts, and vector-dedup.ts access them directly. Marking them
|
|
30
|
+
// private would force ugly `as unknown as` casts in those modules; keeping
|
|
31
|
+
// them package-public makes VectorStore a thin barrel whose helpers live in
|
|
32
|
+
// sibling files.
|
|
54
33
|
readonly embedder: Embedder;
|
|
55
34
|
readonly stateDir: string;
|
|
56
|
-
|
|
35
|
+
/** L2 semantic firing point (cfg.L2_COSINE, or a direct test override). */
|
|
36
|
+
readonly l2Threshold: number;
|
|
57
37
|
/** Single source of truth for tier flags + thresholds (Sprint 14). */
|
|
58
38
|
readonly cfg: DedupConfigShape;
|
|
59
39
|
/** Optional monitoring target (Sprint 14). Undefined → no monitoring. */
|
|
60
|
-
|
|
40
|
+
readonly eventsPath?: string;
|
|
61
41
|
/**
|
|
62
42
|
* Repo key for the async PGlite vector index (Slice 2). We use the stateDir
|
|
63
43
|
* itself as the repo id — it is already unique per repo and available here
|
|
@@ -132,302 +112,12 @@ export class VectorStore {
|
|
|
132
112
|
}
|
|
133
113
|
|
|
134
114
|
/**
|
|
135
|
-
* Add a checkpoint
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* 4. If none match → create new checkpoint
|
|
115
|
+
* Add a checkpoint, deduping it against the session's existing checkpoints.
|
|
116
|
+
*
|
|
117
|
+
* The cascade itself lives in ./add.ts (sibling-helper pattern) so this class
|
|
118
|
+
* stays a thin shell over the fields its helpers read.
|
|
140
119
|
*/
|
|
141
120
|
add(input: AddInput): AddResult {
|
|
142
|
-
|
|
143
|
-
const sessionId = normalizeSessionId(input.sessionId);
|
|
144
|
-
const regionHash = computeRegionHash(input.regionText);
|
|
145
|
-
const all = listCheckpoints(sessionId, this.stateDir);
|
|
146
|
-
// Honest "tokens saved" base for this region. For a deduped add the whole
|
|
147
|
-
// original region is discarded (nothing new stored); for a new checkpoint
|
|
148
|
-
// we persist (orig − stored). Falls back to stored when orig is unknown.
|
|
149
|
-
const origTokens = input.originalTokenEstimate ?? input.tokenEstimate ?? 0;
|
|
150
|
-
const cfg = this.cfg;
|
|
151
|
-
// Live per-tier progress hook (Phase 1). Sync + optional; fired at each tier
|
|
152
|
-
// so the UI can paint "L0 ✓ → L1 ✓ → L2 0.91 → stored" during a compaction.
|
|
153
|
-
const onTier = input.onTier;
|
|
154
|
-
// Tracks whether a tier matched while in MARK_ONLY (record-but-don't-collapse),
|
|
155
|
-
// and which tier.
|
|
156
|
-
let markOnly: DedupTier | null = null;
|
|
157
|
-
|
|
158
|
-
// 0. L0 content-hash dedup (Sprint 9) — catches identical content arriving
|
|
159
|
-
// under different regionText. Normalization handles case/whitespace/ANSI so
|
|
160
|
-
// variants collapse to one row. Dual-hash guards a single-hash collision.
|
|
161
|
-
// Sprint 10: bloom is the accelerator — a miss means "definitely new" and
|
|
162
|
-
// skips the scan; a hit is only a candidate, confirmed against `all` below.
|
|
163
|
-
// Gated by L0_ENABLED (Sprint 14). MARK_ONLY_L0 records the decision but
|
|
164
|
-
// does not collapse — the new region is still stored.
|
|
165
|
-
onTier?.({ tier: "L0", status: "scanning" });
|
|
166
|
-
const digest = computeContentDigest(input.regionText);
|
|
167
|
-
const bloom = openBloom(this.stateDir);
|
|
168
|
-
if (cfg.L0_ENABLED && bloom.maybeHas(digest.contentHash)) {
|
|
169
|
-
const contentMatch = all.find(
|
|
170
|
-
(cp) =>
|
|
171
|
-
cp.contentHash === digest.contentHash &&
|
|
172
|
-
cp.contentHash2 === digest.contentHash2,
|
|
173
|
-
);
|
|
174
|
-
if (contentMatch) {
|
|
175
|
-
if (cfg.MARK_ONLY_L0) {
|
|
176
|
-
markOnly = "L0"; // Record-but-don't-collapse: fall through.
|
|
177
|
-
} else {
|
|
178
|
-
contentMatch.timestamp = input.timestamp;
|
|
179
|
-
upsertCheckpoint(contentMatch, this.stateDir);
|
|
180
|
-
bumpDedupStats(true, this.stateDir);
|
|
181
|
-
// Deduped: whole original region discarded, nothing new stored.
|
|
182
|
-
addTokensSaved(origTokens, this.stateDir);
|
|
183
|
-
const r = {
|
|
184
|
-
checkpoint: contentMatch,
|
|
185
|
-
deduped: true,
|
|
186
|
-
reason: "contentHash",
|
|
187
|
-
};
|
|
188
|
-
this.record("L0", "deduped", "contentHash", Date.now() - t0, 1, contentMatch.checkpointId);
|
|
189
|
-
onTier?.({ tier: "L0", status: "deduped", detail: "contentHash" });
|
|
190
|
-
return r;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// 1. Legacy regionHash dedup (backward-compat) — part of L0 tier gating.
|
|
196
|
-
if (cfg.L0_ENABLED) {
|
|
197
|
-
const regionMatch = all.find((cp) => cp.regionHash === regionHash);
|
|
198
|
-
if (regionMatch) {
|
|
199
|
-
if (cfg.MARK_ONLY_L0) {
|
|
200
|
-
markOnly = "L0"; // fall through
|
|
201
|
-
} else {
|
|
202
|
-
bumpDedupStats(true, this.stateDir);
|
|
203
|
-
// Deduped: whole original region discarded, nothing new stored.
|
|
204
|
-
addTokensSaved(origTokens, this.stateDir);
|
|
205
|
-
const r = {
|
|
206
|
-
checkpoint: regionMatch,
|
|
207
|
-
deduped: true,
|
|
208
|
-
reason: "regionHash",
|
|
209
|
-
};
|
|
210
|
-
this.record("L0", "deduped", "regionHash", Date.now() - t0, 1, regionMatch.checkpointId);
|
|
211
|
-
onTier?.({ tier: "L0", status: "deduped", detail: "regionHash" });
|
|
212
|
-
return r;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// 2. SummaryHash dedup — catches same-topic incremental compactions.
|
|
218
|
-
// Full 64-hex SHA-256 (was 16-hex in Sprint 8 — collision-prone).
|
|
219
|
-
const summaryHash = input.topicSummary
|
|
220
|
-
? createHash("sha256").update(input.topicSummary).digest("hex")
|
|
221
|
-
: undefined;
|
|
222
|
-
if (summaryHash && cfg.L0_ENABLED) {
|
|
223
|
-
const summaryMatch = all.find((cp) => cp.summaryHash === summaryHash);
|
|
224
|
-
if (summaryMatch) {
|
|
225
|
-
if (cfg.MARK_ONLY_L0) {
|
|
226
|
-
markOnly = "L0"; // fall through
|
|
227
|
-
} else {
|
|
228
|
-
summaryMatch.timestamp = input.timestamp;
|
|
229
|
-
upsertCheckpoint(summaryMatch, this.stateDir);
|
|
230
|
-
bumpDedupStats(true, this.stateDir);
|
|
231
|
-
// Deduped: whole original region discarded, nothing new stored.
|
|
232
|
-
addTokensSaved(origTokens, this.stateDir);
|
|
233
|
-
const r = {
|
|
234
|
-
checkpoint: summaryMatch,
|
|
235
|
-
deduped: true,
|
|
236
|
-
reason: "summaryHash",
|
|
237
|
-
};
|
|
238
|
-
this.record("L0", "deduped", "summaryHash", Date.now() - t0, 1, summaryMatch.checkpointId);
|
|
239
|
-
onTier?.({ tier: "L0", status: "deduped", detail: "summaryHash" });
|
|
240
|
-
return r;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
// L0 did not collapse this region.
|
|
245
|
-
onTier?.({ tier: "L0", status: "passed" });
|
|
246
|
-
|
|
247
|
-
// 2b. L1 MinHash/LSH near-duplicate dedup (Sprint 11) — catches one-word
|
|
248
|
-
// edits / rewordings that L0's exact hash misses. Cheap LSH bucket
|
|
249
|
-
// retrieval → trigram verification (pg_trgm-equivalent) as the final gate.
|
|
250
|
-
// Gated by L1_ENABLED (Sprint 14); MARK_ONLY_L1 records but doesn't collapse.
|
|
251
|
-
onTier?.({ tier: "L1", status: "scanning" });
|
|
252
|
-
if (cfg.L1_ENABLED) {
|
|
253
|
-
const l1 = this.findL1Duplicate(sessionId, input.regionText, all);
|
|
254
|
-
if (l1 && !cfg.MARK_ONLY_L1) {
|
|
255
|
-
l1.timestamp = input.timestamp;
|
|
256
|
-
upsertCheckpoint(l1, this.stateDir);
|
|
257
|
-
bumpDedupStats(true, this.stateDir);
|
|
258
|
-
const r = { checkpoint: l1, deduped: true, reason: "l1MinHash" };
|
|
259
|
-
this.record("L1", "deduped", "l1MinHash", Date.now() - t0, 1, l1.checkpointId);
|
|
260
|
-
onTier?.({ tier: "L1", status: "deduped", detail: "l1MinHash" });
|
|
261
|
-
return r;
|
|
262
|
-
}
|
|
263
|
-
if (l1 && cfg.MARK_ONLY_L1) markOnly = "L1";
|
|
264
|
-
}
|
|
265
|
-
onTier?.({ tier: "L1", status: "passed" });
|
|
266
|
-
|
|
267
|
-
// 3. L2 semantic dedup — catches near-identical / semantically-similar regions
|
|
268
|
-
// via cosine over the embedding. topicSummary is used for summaryHash dedup
|
|
269
|
-
// (tier 2); the vector index is keyed on the original region for backward-
|
|
270
|
-
// compat search semantics. Threshold from cfg (L2_COSINE trigram honest
|
|
271
|
-
// firing point). QA #13 timeout guard: if the O(n) scan exceeds the budget,
|
|
272
|
-
// degrade to "store without dedup this pass" so we never lose a checkpoint.
|
|
273
|
-
// Gated by L2_ENABLED (Sprint 14); MARK_ONLY_L2 records but doesn't collapse.
|
|
274
|
-
const SIMILARITY_BUDGET_MS = cfg.SIMILARITY_BUDGET_MS;
|
|
275
|
-
const simThreshold = this.l2Threshold; // from cfg.L2_COSINE (default 0.85 trigram)
|
|
276
|
-
const embedding = this.embedder.embed(input.regionText);
|
|
277
|
-
onTier?.({ tier: "L2", status: "scanning" });
|
|
278
|
-
if (cfg.L2_ENABLED && all.length > 0) {
|
|
279
|
-
const start = Date.now();
|
|
280
|
-
let timedOut = false;
|
|
281
|
-
const nearest = all.reduce(
|
|
282
|
-
(best, cp) => {
|
|
283
|
-
if (!timedOut && Date.now() - start > SIMILARITY_BUDGET_MS)
|
|
284
|
-
timedOut = true;
|
|
285
|
-
if (timedOut) return best;
|
|
286
|
-
const sim = cosineSimilarity(embedding, cp.embedding);
|
|
287
|
-
return sim > best.sim ? { checkpoint: cp, sim } : best;
|
|
288
|
-
},
|
|
289
|
-
{ checkpoint: all[0], sim: -1 },
|
|
290
|
-
);
|
|
291
|
-
if (!timedOut && nearest.sim >= simThreshold) {
|
|
292
|
-
if (!cfg.MARK_ONLY_L2) {
|
|
293
|
-
// Near-identical — update timestamp on existing checkpoint
|
|
294
|
-
nearest.checkpoint.timestamp = input.timestamp;
|
|
295
|
-
upsertCheckpoint(nearest.checkpoint, this.stateDir);
|
|
296
|
-
bumpDedupStats(true, this.stateDir);
|
|
297
|
-
// Deduped: whole original region discarded, nothing new stored.
|
|
298
|
-
addTokensSaved(origTokens, this.stateDir);
|
|
299
|
-
const r = {
|
|
300
|
-
checkpoint: nearest.checkpoint,
|
|
301
|
-
deduped: true,
|
|
302
|
-
reason: "contentSimilarity",
|
|
303
|
-
};
|
|
304
|
-
this.record("L2", "deduped", "contentSimilarity", Date.now() - t0, nearest.sim, nearest.checkpoint.checkpointId);
|
|
305
|
-
onTier?.({
|
|
306
|
-
tier: "L2",
|
|
307
|
-
status: "deduped",
|
|
308
|
-
detail: nearest.sim.toFixed(2),
|
|
309
|
-
});
|
|
310
|
-
return r;
|
|
311
|
-
}
|
|
312
|
-
markOnly = "L2";
|
|
313
|
-
}
|
|
314
|
-
onTier?.({
|
|
315
|
-
tier: "L2",
|
|
316
|
-
status: "passed",
|
|
317
|
-
detail: `best ${nearest.sim.toFixed(2)}`,
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// 4. Genuinely new — create checkpoint
|
|
322
|
-
const checkpointId = nextCheckpointId(sessionId, this.stateDir);
|
|
323
|
-
const checkpoint: StoredCheckpoint = {
|
|
324
|
-
checkpointId,
|
|
325
|
-
sessionId,
|
|
326
|
-
repoId: this.repoId,
|
|
327
|
-
summary: input.summary,
|
|
328
|
-
topicSummary: input.topicSummary,
|
|
329
|
-
summaryHash,
|
|
330
|
-
keyDecisions: input.keyDecisions ?? [],
|
|
331
|
-
nextSteps: input.nextSteps ?? [],
|
|
332
|
-
filesModified: input.filesModified ?? [],
|
|
333
|
-
tokenEstimate: input.tokenEstimate ?? 0,
|
|
334
|
-
originalTokenEstimate: input.originalTokenEstimate,
|
|
335
|
-
regionHash,
|
|
336
|
-
contentHash: digest.contentHash,
|
|
337
|
-
contentHash2: digest.contentHash2,
|
|
338
|
-
contentHashVersion: digest.contentHashVersion,
|
|
339
|
-
normalizedText: digest.normalizedText,
|
|
340
|
-
compressedOriginal: compressSmart(
|
|
341
|
-
Buffer.from(input.regionText, "utf-8"),
|
|
342
|
-
input.compressionPressure,
|
|
343
|
-
),
|
|
344
|
-
embedding,
|
|
345
|
-
timestamp: input.timestamp,
|
|
346
|
-
};
|
|
347
|
-
// Persistence is SQLite (store/sqlite.ts). upsertCheckpoint keeps the
|
|
348
|
-
// idempotent-by-id semantics the old JSON append implied.
|
|
349
|
-
upsertCheckpoint(checkpoint, this.stateDir);
|
|
350
|
-
// Cumulative "tokens saved" counter (per-repo SQLite meta). For a NEW
|
|
351
|
-
// checkpoint the saved amount is (original − stored); for a deduped add the
|
|
352
|
-
// whole original region is discarded (handled in the deduped return paths
|
|
353
|
-
// below). Survives sessions and travels with the repo.
|
|
354
|
-
const stored = input.tokenEstimate ?? 0;
|
|
355
|
-
addTokensSaved(Math.max(0, origTokens - stored), this.stateDir);
|
|
356
|
-
// L1: persist this checkpoint's MinHash signature + LSH buckets so future
|
|
357
|
-
// near-duplicate inserts can find it. Deterministic given the seed.
|
|
358
|
-
const sig = minhashSignature(input.regionText);
|
|
359
|
-
upsertMinhashSignature(
|
|
360
|
-
checkpointId,
|
|
361
|
-
sessionId,
|
|
362
|
-
SIGNATURE_VERSION,
|
|
363
|
-
sig,
|
|
364
|
-
this.stateDir,
|
|
365
|
-
);
|
|
366
|
-
insertLshBuckets(
|
|
367
|
-
checkpointId,
|
|
368
|
-
sessionId,
|
|
369
|
-
SIGNATURE_VERSION,
|
|
370
|
-
lshBands(sig, sessionId, SIGNATURE_VERSION),
|
|
371
|
-
this.stateDir,
|
|
372
|
-
);
|
|
373
|
-
// Bloom accelerator: record the new content_hash so a future add() can short-
|
|
374
|
-
// circuit the scan on a hit (still confirmed by the SELECT-based `all` above).
|
|
375
|
-
bloom.add(digest.contentHash);
|
|
376
|
-
saveBloom(this.stateDir);
|
|
377
|
-
|
|
378
|
-
// Track the region hash in session state for fast sentinel checks.
|
|
379
|
-
const state = loadSessionState(sessionId, this.stateDir);
|
|
380
|
-
if (!state.storedRegionHashes.includes(regionHash)) {
|
|
381
|
-
state.storedRegionHashes.push(regionHash);
|
|
382
|
-
saveSessionState(sessionId, state, this.stateDir);
|
|
383
|
-
}
|
|
384
|
-
// A new checkpoint. If a tier matched while MARK_ONLY, record that (the
|
|
385
|
-
// decision fired but we intentionally did not collapse).
|
|
386
|
-
if (markOnly) {
|
|
387
|
-
this.record(markOnly, "mark_only", "mark_only", Date.now() - t0);
|
|
388
|
-
} else {
|
|
389
|
-
this.record("L0", "new", undefined, Date.now() - t0);
|
|
390
|
-
}
|
|
391
|
-
// Cumulative store-wide dedup accounting (attempt, not collapsed).
|
|
392
|
-
bumpDedupStats(false, this.stateDir);
|
|
393
|
-
onTier?.({ tier: "new", status: "stored" });
|
|
394
|
-
return { checkpoint, deduped: false };
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* L1 near-duplicate lookup: MinHash → LSH candidate retrieval → trigram verify.
|
|
399
|
-
* Returns the matching checkpoint or undefined. Bounded by a 100-candidate cap
|
|
400
|
-
* and a 20ms verify budget (QA #7/#15) so it never hangs a large session.
|
|
401
|
-
*/
|
|
402
|
-
private findL1Duplicate(
|
|
403
|
-
sessionId: string,
|
|
404
|
-
regionText: string,
|
|
405
|
-
all: StoredCheckpoint[],
|
|
406
|
-
): StoredCheckpoint | undefined {
|
|
407
|
-
if (all.length === 0) return undefined;
|
|
408
|
-
const sig = minhashSignature(regionText);
|
|
409
|
-
if (sig.length !== NUM_HASHES) return undefined;
|
|
410
|
-
const bands = lshBands(sig, sessionId, SIGNATURE_VERSION);
|
|
411
|
-
// Cheap candidate retrieval (single query, capped). Exclude nothing yet —
|
|
412
|
-
// the new checkpoint has no id, so pass a sentinel that never matches.
|
|
413
|
-
const candidateIds = lshCandidateChunks(
|
|
414
|
-
bands,
|
|
415
|
-
sessionId,
|
|
416
|
-
"__new__",
|
|
417
|
-
this.stateDir,
|
|
418
|
-
100,
|
|
419
|
-
);
|
|
420
|
-
if (candidateIds.length === 0) return undefined;
|
|
421
|
-
const byId = new Map(all.map((cp) => [cp.checkpointId, cp]));
|
|
422
|
-
const VERIFY_BUDGET_MS = 20;
|
|
423
|
-
const start = Date.now();
|
|
424
|
-
for (const id of candidateIds) {
|
|
425
|
-
if (Date.now() - start > VERIFY_BUDGET_MS) break; // QA #15: abort → "not dup"
|
|
426
|
-
const cand = byId.get(id);
|
|
427
|
-
if (!cand) continue;
|
|
428
|
-
const candText = cand.normalizedText ?? cand.summary ?? "";
|
|
429
|
-
if (isNearDuplicate(regionText, candText)) return cand;
|
|
430
|
-
}
|
|
431
|
-
return undefined;
|
|
121
|
+
return addCheckpoint(this, input);
|
|
432
122
|
}
|
|
433
123
|
}
|