pi-mega-compact 0.7.7 → 0.7.9
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/README.md +11 -12
- package/dist/extensions/dashboard-server/helpers.js +37 -0
- package/dist/extensions/dashboard-server/html/all-repos-tab.js +26 -0
- package/dist/extensions/dashboard-server/html/body-open.js +23 -0
- package/dist/extensions/dashboard-server/html/current-repo-tab.js +130 -0
- package/dist/extensions/dashboard-server/html/head-open.js +16 -0
- package/dist/extensions/dashboard-server/html/high-score-tab.js +25 -0
- package/dist/extensions/dashboard-server/html/repo-detail-modal.js +26 -0
- package/dist/extensions/dashboard-server/html/script.js +259 -0
- package/dist/extensions/dashboard-server/html/styles.js +103 -0
- package/dist/extensions/dashboard-server/html/summary-tab.js +19 -0
- package/dist/extensions/dashboard-server/html-template.js +41 -0
- package/dist/extensions/dashboard-server/html.js +756 -0
- package/dist/extensions/dashboard-server/index-reader.js +133 -0
- package/dist/extensions/dashboard-server/server.js +370 -0
- package/dist/extensions/dashboard-server/snapshot.js +43 -0
- package/dist/extensions/dashboard-server/state.js +30 -0
- package/dist/extensions/dashboard-server/types.js +5 -0
- package/dist/extensions/dashboard-server.js +7 -1315
- package/dist/extensions/mega-commands.js +162 -134
- package/dist/extensions/mega-compact.test.js +292 -24
- package/dist/extensions/mega-config.js +10 -0
- package/dist/extensions/mega-conflict-cmds.js +5 -1
- package/dist/extensions/mega-dashboard-cmds.js +29 -22
- package/dist/extensions/mega-db-cmds.js +11 -2
- package/dist/extensions/mega-events/agent-handlers.js +173 -0
- package/dist/extensions/mega-events/compact-handlers.js +133 -0
- package/dist/extensions/mega-events/context-handler.js +249 -0
- package/dist/extensions/mega-events/register.js +21 -0
- package/dist/extensions/mega-events/session-handlers.js +142 -0
- package/dist/extensions/mega-events.js +15 -652
- package/dist/extensions/mega-pipeline/compact.js +324 -0
- package/dist/extensions/mega-pipeline/memory-review.js +38 -0
- package/dist/extensions/mega-pipeline/recall.js +147 -0
- package/dist/extensions/mega-pipeline.js +9 -480
- package/dist/extensions/mega-runtime/helpers.js +40 -0
- package/dist/extensions/mega-runtime/query.js +29 -0
- package/dist/extensions/mega-runtime/state.js +711 -0
- package/dist/extensions/mega-runtime/widget.js +197 -0
- package/dist/extensions/mega-runtime.js +15 -932
- package/dist/src/store/sqlite/checkpoints.js +145 -0
- package/dist/src/store/sqlite/connection.js +35 -0
- package/dist/src/store/sqlite/dedup-mirror.js +64 -0
- package/dist/src/store/sqlite/foundation.js +38 -0
- package/dist/src/store/sqlite/global-index.js +224 -0
- package/dist/src/store/sqlite/index-store.js +167 -0
- package/dist/src/store/sqlite/maintenance.js +235 -0
- package/dist/src/store/sqlite/memories.js +164 -0
- package/dist/src/store/sqlite/memory.js +54 -0
- package/dist/src/store/sqlite/meta.js +82 -0
- package/dist/src/store/sqlite/minhash-lsh.js +47 -0
- package/dist/src/store/sqlite/model-snapshots.js +47 -0
- package/dist/src/store/sqlite/raptor.js +57 -0
- package/dist/src/store/sqlite/raw-transcript.js +134 -0
- package/dist/src/store/sqlite/schema.js +250 -0
- package/dist/src/store/sqlite/session-state.js +28 -0
- package/dist/src/store/sqlite/sessions.js +39 -0
- package/dist/src/store/sqlite/stats.js +66 -0
- package/dist/src/store/sqlite/transaction.js +19 -0
- package/dist/src/store/sqlite/utils.js +120 -0
- package/dist/src/store/sqlite.js +20 -1607
- package/dist/src/vectorStore/add.js +260 -0
- package/dist/src/vectorStore/dedup.js +52 -0
- package/dist/src/vectorStore/index.js +10 -0
- package/dist/src/vectorStore/queries.js +83 -0
- package/dist/src/vectorStore/search.js +95 -0
- package/dist/src/vectorStore/session.js +19 -0
- package/dist/src/vectorStore/store.js +105 -0
- package/dist/src/vectorStore/types.js +6 -0
- package/dist/src/vectorStore/utils.js +23 -0
- package/extensions/dashboard-server/html.ts +758 -0
- package/extensions/dashboard-server/index-reader.ts +130 -0
- package/extensions/dashboard-server/server.ts +358 -0
- package/extensions/dashboard-server/snapshot.ts +44 -0
- package/extensions/dashboard-server/state.ts +33 -0
- package/extensions/dashboard-server/types.ts +134 -0
- package/extensions/dashboard-server.ts +7 -1431
- package/extensions/mega-commands.ts +33 -10
- package/extensions/mega-compact.test.ts +453 -37
- package/extensions/mega-config.ts +22 -0
- package/extensions/mega-conflict-cmds.ts +6 -2
- package/extensions/mega-dashboard-cmds.ts +30 -23
- package/extensions/mega-db-cmds.ts +11 -3
- package/extensions/mega-events/agent-handlers.ts +214 -0
- package/extensions/mega-events/compact-handlers.ts +164 -0
- package/extensions/mega-events/context-handler.ts +290 -0
- package/extensions/mega-events/register.ts +37 -0
- package/extensions/mega-events/session-handlers.ts +165 -0
- package/extensions/mega-events.ts +15 -732
- package/extensions/mega-pipeline/compact.ts +366 -0
- package/extensions/mega-pipeline/memory-review.ts +46 -0
- package/extensions/mega-pipeline/recall.ts +165 -0
- package/extensions/mega-pipeline.ts +9 -537
- package/extensions/mega-runtime/helpers.ts +68 -0
- package/extensions/mega-runtime/query.ts +29 -0
- package/extensions/mega-runtime/state.ts +797 -0
- package/extensions/mega-runtime/widget.ts +258 -0
- package/extensions/mega-runtime.ts +15 -1076
- package/package.json +4 -3
- package/src/store/sqlite/checkpoints.ts +204 -0
- package/src/store/sqlite/dedup-mirror.ts +114 -0
- package/src/store/sqlite/foundation.ts +63 -0
- package/src/store/sqlite/global-index.ts +305 -0
- package/src/store/sqlite/maintenance.ts +294 -0
- package/src/store/sqlite/memories.ts +217 -0
- package/src/store/sqlite/meta.ts +108 -0
- package/src/store/sqlite/model-snapshots.ts +83 -0
- package/src/store/sqlite/raptor.ts +107 -0
- package/src/store/sqlite/raw-transcript.ts +221 -0
- package/src/store/sqlite/schema.ts +258 -0
- package/src/store/sqlite/session-state.ts +38 -0
- package/src/store/sqlite/stats.ts +127 -0
- package/src/store/sqlite/utils.ts +125 -0
- package/src/store/sqlite.ts +20 -2204
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* utils.ts — shared helpers for the SQLite store submodules.
|
|
3
|
+
*
|
|
4
|
+
* These are the low-level primitives (embed encode/decode, JSON guards,
|
|
5
|
+
* connection cache, `openStore`, `withTx`, row mappers) that every other
|
|
6
|
+
* submodule imports. Keeping them here avoids circular dependencies:
|
|
7
|
+
* submodules depend on `utils` + `schema`, never on each other (except via
|
|
8
|
+
* the barrel).
|
|
9
|
+
*/
|
|
10
|
+
import { DatabaseSync } from "node:sqlite";
|
|
11
|
+
import { existsSync, mkdirSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { getStateDir } from "../../store.js";
|
|
14
|
+
import type { StoredCheckpoint } from "../../store.js";
|
|
15
|
+
import { initSchema } from "./schema.js";
|
|
16
|
+
|
|
17
|
+
// NOTE: normalizeSessionId is imported from ../store.js by each submodule that
|
|
18
|
+
// needs it — it is NOT re-exported here (the original sqlite.ts did not export
|
|
19
|
+
// it, and we must not add new barrel exports).
|
|
20
|
+
|
|
21
|
+
/** Encode a float vector as a little-endian Float32 BLOB for cosine scanning. */
|
|
22
|
+
export function encodeEmbedding(v: number[]): Buffer {
|
|
23
|
+
const buf = Buffer.allocUnsafe(v.length * 4);
|
|
24
|
+
for (let i = 0; i < v.length; i++) buf.writeFloatLE(v[i] ?? 0, i * 4);
|
|
25
|
+
return buf;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Decode a Float32 BLOB back to a number[]. node:sqlite returns BLOBs as
|
|
29
|
+
* Uint8Array, so decode via DataView (Buffer is a Uint8Array subclass — both
|
|
30
|
+
* work). */
|
|
31
|
+
export function decodeEmbedding(buf: Uint8Array | null | undefined): number[] {
|
|
32
|
+
if (!buf || buf.length === 0) return [];
|
|
33
|
+
const dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
34
|
+
const n = buf.length / 4;
|
|
35
|
+
const out = new Array<number>(n);
|
|
36
|
+
for (let i = 0; i < n; i++) out[i] = dv.getFloat32(i * 4, true);
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function jsonText(v: unknown): string {
|
|
41
|
+
return JSON.stringify(v ?? []);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// In-process cache so the same stateDir reuses one connection (and so a fresh
|
|
45
|
+
// VectorStore over the same dir shares the open DB). Cross-process durability
|
|
46
|
+
// comes from reopening the same file path — proven by the integration test.
|
|
47
|
+
const cache = new Map<string, DatabaseSync>();
|
|
48
|
+
|
|
49
|
+
/** Open (or reuse) the SQLite store for a state dir. */
|
|
50
|
+
export function openStore(stateDir: string = getStateDir()): DatabaseSync {
|
|
51
|
+
const existing = cache.get(stateDir);
|
|
52
|
+
if (existing) {
|
|
53
|
+
// A closed handle in the cache (e.g. a test calling db.close() directly
|
|
54
|
+
// instead of closeStore) would surface as "database is not open" on the
|
|
55
|
+
// next reuse. Detect and evict so callers never see a dead handle.
|
|
56
|
+
try {
|
|
57
|
+
existing.prepare("SELECT 1");
|
|
58
|
+
return existing;
|
|
59
|
+
} catch {
|
|
60
|
+
cache.delete(stateDir);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!existsSync(stateDir)) mkdirSync(stateDir, { recursive: true });
|
|
65
|
+
const db = new DatabaseSync(join(stateDir, "sqlite.db"));
|
|
66
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
67
|
+
db.exec("PRAGMA foreign_keys = ON");
|
|
68
|
+
initSchema(db);
|
|
69
|
+
cache.set(stateDir, db);
|
|
70
|
+
return db;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Close and evict a cached connection (test teardown only). */
|
|
74
|
+
export function closeStore(stateDir: string): void {
|
|
75
|
+
const db = cache.get(stateDir);
|
|
76
|
+
if (db) {
|
|
77
|
+
db.close();
|
|
78
|
+
cache.delete(stateDir);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Run `fn` atomically. Uses SAVEPOINT so it nests safely under an outer
|
|
84
|
+
* transaction (unlike `BEGIN`, which SQLite rejects when one is already open).
|
|
85
|
+
* Mirrors better-sqlite3's `db.transaction(fn)` semantics — callers that wrap a
|
|
86
|
+
* batch in withTx (e.g. backfill) can still call helpers that also use withTx.
|
|
87
|
+
*/
|
|
88
|
+
export function withTx(db: DatabaseSync, fn: () => void): void {
|
|
89
|
+
db.exec("SAVEPOINT mc_tx");
|
|
90
|
+
try {
|
|
91
|
+
fn();
|
|
92
|
+
db.exec("RELEASE mc_tx");
|
|
93
|
+
} catch (e) {
|
|
94
|
+
db.exec("ROLLBACK TO mc_tx");
|
|
95
|
+
db.exec("RELEASE mc_tx");
|
|
96
|
+
throw e;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Map a DB row to the public StoredCheckpoint shape. */
|
|
101
|
+
export function rowToCheckpoint(row: any): StoredCheckpoint {
|
|
102
|
+
return {
|
|
103
|
+
checkpointId: row.id,
|
|
104
|
+
sessionId: row.session_id,
|
|
105
|
+
summary: row.summary ?? "",
|
|
106
|
+
topicSummary: row.topic_summary ?? undefined,
|
|
107
|
+
summaryHash: row.summary_hash ?? undefined,
|
|
108
|
+
keyDecisions: row.key_decisions ? JSON.parse(row.key_decisions) : [],
|
|
109
|
+
nextSteps: row.next_steps ? JSON.parse(row.next_steps) : [],
|
|
110
|
+
filesModified: row.files_modified ? JSON.parse(row.files_modified) : [],
|
|
111
|
+
tokenEstimate: row.token_estimate ?? 0,
|
|
112
|
+
originalTokenEstimate: row.original_token_estimate ?? undefined,
|
|
113
|
+
regionHash: row.region_hash ?? "",
|
|
114
|
+
contentHash: row.content_hash ?? undefined,
|
|
115
|
+
contentHash2: row.content_hash2 ?? undefined,
|
|
116
|
+
contentHashVersion: row.content_hash_version ?? undefined,
|
|
117
|
+
normalizedText: row.normalized_text ?? undefined,
|
|
118
|
+
// node:sqlite returns BLOBs as Uint8Array; normalize to Buffer so callers
|
|
119
|
+
// (e.g. decompressSmart → Buffer.toString) behave as under better-sqlite3.
|
|
120
|
+
compressedOriginal: row.compressed_original ? Buffer.from(row.compressed_original) : undefined,
|
|
121
|
+
embedding: decodeEmbedding(row.embedding_blob),
|
|
122
|
+
timestamp: Number(row.timestamp ?? 0),
|
|
123
|
+
dedupStatus: row.dedup_status ?? undefined,
|
|
124
|
+
};
|
|
125
|
+
}
|