-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstore.ts
More file actions
379 lines (339 loc) · 19.5 KB
/
Copy pathstore.ts
File metadata and controls
379 lines (339 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* SecureContext Storage Abstraction Layer
*
* PURPOSE:
* Decouples the MCP plugin and API server from any specific database backend.
* Two implementations ship out of the box:
*
* SqliteStore — local SQLite file per project (default, single-developer)
* PostgresStore — shared PostgreSQL + pgvector (production, multi-agent, multi-machine)
*
* SELECTION:
* Controlled by the ZC_STORE environment variable:
* ZC_STORE=sqlite (or unset) → SqliteStore
* ZC_STORE=postgres → PostgresStore, configured by ZC_POSTGRES_URL
* or the ZC_POSTGRES_* parts (see
* resolvePgConnectionString below)
*
* DESIGN PRINCIPLES:
* - All methods are async (Promise-returning).
* SqliteStore wraps synchronous DatabaseSync calls in Promise.resolve().
* PostgresStore uses pg.Pool with native async/await.
* - No method throws to the caller for expected errors (token-not-found,
* key-not-found, etc.) — those return null/false/empty array.
* - Security enforcement (RBAC, hash chain, rate limits) lives in the Store
* implementation, not in the caller.
* - projectPath is always the raw filesystem path (e.g. "C:/repos/my-service").
* Implementations derive projectHash = SHA256(projectPath).slice(0,16) internally.
* Callers never need to know about hashing.
*/
import { createHash } from "node:crypto";
import type { MemoryFact, BroadcastType, BroadcastMessage, BroadcastResult, ComplexityProfile, EpistemicOpts } from "./memory.js";
import type { KnowledgeEntry, CrossProjectEntry, RetentionTier } from "./knowledge.js";
import type { AgentRole } from "./access-control.js";
// ─────────────────────────────────────────────────────────────────────────────
// Re-exported shared types (callers import from store.ts, not from sub-modules)
// ─────────────────────────────────────────────────────────────────────────────
export type {
MemoryFact,
BroadcastType,
BroadcastMessage,
BroadcastResult,
ComplexityProfile,
KnowledgeEntry,
CrossProjectEntry,
RetentionTier,
AgentRole,
};
// ─────────────────────────────────────────────────────────────────────────────
// Store-specific types
// ─────────────────────────────────────────────────────────────────────────────
/**
* One call target and everything that reaches it.
*
* `callers` and `sites` are different questions on purpose: callers is "how many
* places must I check", sites is "how many edits might be needed". `ambiguous`
* is what name-based resolution could not attribute confidently and is
* deliberately NOT folded into callers.
*/
export interface CallImpactTarget {
symbol: string;
declaredIn: string;
callers: number;
sites: number;
files: string[];
ambiguous: number;
}
export interface CallImpactResult {
targets: CallImpactTarget[];
/** Call sites in the queried file that could not be named. Coverage, not noise. */
dynamicSites: number;
/** False when the call layer has never been built. Never render this as zero impact. */
built: boolean;
}
export interface MemoryStats {
count: number;
max: number;
evictTo: number;
criticalCount: number;
complexity: ComplexityProfile | null;
}
export interface MemoryLimits {
max: number;
evictTo: number;
profile: ComplexityProfile | null;
}
export interface KbStats {
totalEntries: number;
externalEntries: number;
summaryEntries: number;
embeddingsCached: number;
dbSizeBytes: number;
}
export interface SearchOptions {
limit?: number;
agentId?: string;
depth?: "L0" | "L1" | "L2";
/** TR-2 internal: set on sub-searches spawned by temporal-question
* decomposition so they don't decompose recursively. */
_noDecompose?: boolean;
/** TKG-T2 (v0.47.0) — point-in-time KB view: only entries first seen at or
* before this ISO timestamp ("what did the KB contain on date X"). */
asOf?: string;
}
export interface ExplainEntry {
source: string;
bm25Score: number;
vectorScore: number;
hybridScore: number;
tier: string;
snippet: string;
}
export interface ExplainResult {
query: string;
depth: string;
results: ExplainEntry[];
model: string;
searchMode: string;
}
export interface BroadcastOptions {
task?: string;
files?: string[];
state?: string;
summary?: string;
depends_on?: string[];
reason?: string;
importance?: number;
channel_key?: string;
session_token?: string;
}
export interface RecallOptions {
limit?: number;
sinceId?: number;
type?: BroadcastType;
agentId?: string;
}
export interface ChainStatus {
ok: boolean;
totalRows: number;
brokenAt?: number;
}
export interface TokenPayload {
tokenId: string;
agentId: string;
role: AgentRole;
iat: number;
exp: number;
}
export interface FetchStats {
used: number;
remaining: number;
}
// ─────────────────────────────────────────────────────────────────────────────
// Store interface
// ─────────────────────────────────────────────────────────────────────────────
export interface Store {
// ── Working Memory ──────────────────────────────────────────────────────────
/** Returns an effect-verification result when a write did not round-trip as requested (v0.52.0). */
remember(projectPath: string, key: string, value: string, importance: number, agentId: string, epi?: EpistemicOpts): Promise<import("./effect_verify.js").VerifyResult | void>;
forget(projectPath: string, key: string, agentId: string): Promise<boolean>;
// M1 (v0.41.0): optional focus re-ranks live facts by blended relevance to the
// agent's current task; without it, ordering is unchanged (backward-compatible).
// M3 (v0.41.0): from/to = temporal-window bonus; asOf = point-in-time reconstruction.
recall(projectPath: string, agentId: string, opts?: { focus?: string; from?: Date; to?: Date; asOf?: Date; role?: string }): Promise<MemoryFact[]>;
archiveSummary(projectPath: string, summary: string): Promise<{ submitted: number; stored: number; dropped: number } | void>;
getMemoryStats(projectPath: string, agentId: string): Promise<MemoryStats>;
getWorkingMemoryLimits(projectPath: string, forceRecompute?: boolean): Promise<MemoryLimits>;
// R8c (v0.43.0): live ★5 count in a namespace — importance-inflation soft-quota nudge.
countImportance5(projectPath: string, agentId: string): Promise<number>;
// ── Knowledge Base ──────────────────────────────────────────────────────────
index(projectPath: string, content: string, source: string, sourceType?: "internal" | "external", retentionTier?: RetentionTier): Promise<void>;
search(projectPath: string, queries: string[], opts?: SearchOptions): Promise<KnowledgeEntry[]>;
/** D2 (v0.46.1): projectFilter narrows to projects whose label contains the
* string (case-insensitive) or whose hash starts with it — the cross-repo
* reference lookup ("how did SecureContext implement replay?"). */
searchGlobal(queries: string[], limit?: number, projectFilter?: string): Promise<CrossProjectEntry[]>;
getKbStats(projectPath: string): Promise<KbStats>;
explain(projectPath: string, query: string, depth?: string): Promise<ExplainResult>;
// ── Knowledge graph + backlinks (Tier-1 A) ───────────────────────────────────
rebuildBacklinks(projectPath: string): Promise<{ edges: number; nodes: number; topHub: { source: string; weightedIn: number } | null }>;
/** Optional boot heal: rebuild backlinks for every project with knowledge but an empty graph.
* PG-native (one DB, many project_hashes); the SQLite path is healed by indexProject's flush. */
backfillBacklinks?(): Promise<{ projects: number; edges: number }>;
/** Optional Tier-2 #6 enrichment cycle (run by the cron): re-scan contradictions for every
* active (project,agent) pair + backfill empty backlink graphs. PG-native. */
runEnrichment?(): Promise<{ projects: number; flagged: number; backfilledProjects: number; ollamaDown: boolean; entities?: number }>;
graphData(projectPath: string): Promise<{ nodes: Array<{ id: string; inDegree: number; weightedIn: number }>; edges: Array<{ from: string; to: string; relation: string; weight: number }> }>;
backlinksFor(projectPath: string, source: string, limit: number): Promise<{ inDegree: number; weightedIn: number; inbound: Array<{ from: string; relation: string; weight: number }> } | null>;
/**
* Function-level impact: who calls what is declared in `file`, or who calls
* `symbol` anywhere. Exactly one of file/symbol is used.
*
* `built: false` means the call layer has never been built for this project —
* NOT that nothing depends on the target. Callers must render those
* differently, which is the entire point of the feature.
*/
callImpactFor(
projectPath: string,
query: { file?: string; symbol?: string },
): Promise<CallImpactResult>;
// ── Community query mode (v0.37.0) ───────────────────────────────────────────
/** Corpus-level Q&A over pre-computed Louvain community summaries (+ DRIFT-lite follow-ups). */
globalSearch(projectPath: string, question: string): Promise<{ answer: string; followups: string[]; communities: Array<{ community_id: number; size: number; sample_sources: string; summary: string }> } | null>;
// ── Temporal fact retirement (v0.37.0) ───────────────────────────────────────
/** Retire a fact (valid_to close-out + KB archival, non-destructive). Returns false if no live fact. */
retireFact(projectPath: string, key: string, agentId: string, supersededBy: string | null, reason: string): Promise<boolean>;
/** Undo a retirement (clears valid_to). Returns false if the fact wasn't retired. */
reviveFact(projectPath: string, key: string, agentId: string): Promise<boolean>;
// ── Memory contradictions (Tier-1 B) ─────────────────────────────────────────
// R8 (v0.43.0): `skipped` = facts whose embedding transiently failed (embedder busy) — the
// scan continued without them, so a clean result with skipped>0 is INCOMPLETE, not clean.
scanContradictions(projectPath: string, agentId: string): Promise<{ scanned: number; flagged: number; ollamaAvailable: boolean; skipped?: number }>;
listContradictions(projectPath: string, agentId: string): Promise<Array<{ key_a: string; key_b: string; similarity: number; reason: string; detail: string }>>;
reviewContradiction(projectPath: string, agentId: string, keyA: string, keyB: string, status: "dismissed" | "acknowledged" | "resolved", mode?: string): Promise<number>;
// ── Broadcasts ──────────────────────────────────────────────────────────────
broadcast(projectPath: string, type: BroadcastType, agentId: string, opts: BroadcastOptions): Promise<BroadcastMessage>;
recallBroadcasts(projectPath: string, opts: RecallOptions): Promise<BroadcastResult[]>;
replay(projectPath: string, fromId?: number): Promise<BroadcastResult[]>;
ack(projectPath: string, id: number): Promise<void>;
chainStatus(projectPath: string): Promise<ChainStatus>;
setChannelKey(projectPath: string, key: string): Promise<void>;
isChannelKeyConfigured(projectPath: string): Promise<boolean>;
// ── RBAC & Tokens ──────────────────────────────────────────────────────────
issueToken(projectPath: string, agentId: string, role: AgentRole): Promise<string>;
revokeTokens(projectPath: string, agentId: string): Promise<void>;
verifyToken(projectPath: string, token: string): Promise<TokenPayload | null>;
countActiveSessions(projectPath: string): Promise<number>;
// ── Rate Limiting ──────────────────────────────────────────────────────────
getFetchStats(projectPath: string): Promise<FetchStats>;
incrementFetch(projectPath: string): Promise<FetchStats>;
// ── Lifecycle ──────────────────────────────────────────────────────────────
/** Called once on shutdown — close connection pools, flush caches. */
close(): Promise<void>;
}
// ─────────────────────────────────────────────────────────────────────────────
// Utility helpers used by both implementations
// ─────────────────────────────────────────────────────────────────────────────
/**
* Canonical form of a project path, so that spellings of the SAME directory
* produce the SAME hash.
*
* This is not hypothetical tidying. Measured on the author's machine before the
* fix: RevClear had TWO databases (6160 KB under the backslash form, 380 KB
* under the forward-slash form) and Test_Agent_Coordination had two more
* (1336 KB / 352 KB). Whatever an agent wrote through one spelling was
* invisible to every component using the other — a silent partition of memory,
* with no error at any layer.
*
* Deliberately conservative about WHAT is normalised:
* - separators and a trailing separator, which caused the observed split;
* - NOT case. Lower-casing would change the hash of every project that
* already exists and strand every database on disk. Windows is
* case-insensitive, so that is a real remaining gap — but a migration, not
* a one-line change, and it has not bitten anything yet.
*/
export function normalizeProjectPath(projectPath: string): string {
let s = String(projectPath ?? "");
const isWindows = /^[a-zA-Z]:/.test(s);
if (isWindows) s = s.replace(/\//g, "\\");
// A trailing separator does not make it a different project. Keep the root
// separator itself ("C:\" / "/") so the path stays meaningful.
const stripped = s.replace(/[\\/]+$/, "");
return stripped === "" || /^[a-zA-Z]:$/.test(stripped) ? s.slice(0, stripped.length + 1) : stripped;
}
/**
* Derive the 16-hex-char project discriminator.
*
* THE single definition. Everything that needs a project hash — stores, hooks,
* scripts, the MCP tool handlers — must route through here rather than
* re-deriving sha256(...).slice(0,16) inline, because a second copy is free to
* disagree about the input and nothing will report it.
*/
export function projectHash(projectPath: string): string {
return createHash("sha256").update(normalizeProjectPath(projectPath)).digest("hex").slice(0, 16);
}
/** Current UTC date string in YYYY-MM-DD format (for rate limit buckets). */
export function todayUtc(): string {
return new Date().toISOString().slice(0, 10);
}
// ─────────────────────────────────────────────────────────────────────────────
// Factory
// ─────────────────────────────────────────────────────────────────────────────
/**
* Create the appropriate Store based on the ZC_STORE environment variable.
*
* ZC_STORE=sqlite (or unset) → SqliteStore — no extra config needed
* ZC_STORE=postgres → PostgresStore — requires ZC_PG_URL
*
* The factory is async because PostgresStore needs to verify the connection
* and run schema migrations before first use.
*/
/**
* Connection string for the Postgres store.
*
* Accepts the SAME configuration pg_pool.ts already uses. Before this,
* `createStore` read only `ZC_PG_URL` — a name referenced nowhere else in the
* codebase — while every PG-native path (telemetry, kb_edges_pg, task queue)
* read `ZC_POSTGRES_URL` / `ZC_POSTGRES_*`. A machine fully configured for
* Postgres would therefore throw "requires ZC_PG_URL" the moment you set
* ZC_STORE=postgres: the documented switch did not work with the documented
* configuration.
*
* Returns null when nothing is configured, so the caller can say what is missing
* rather than attempting a connection to a default that was never intended.
*/
export function resolvePgConnectionString(): string | null {
const direct = process.env["ZC_POSTGRES_URL"] || process.env["ZC_PG_URL"];
if (direct) return direct;
const host = process.env["ZC_POSTGRES_HOST"];
const password = process.env["ZC_POSTGRES_PASSWORD"];
// Host alone is not enough to be sure Postgres was actually intended; requiring
// one credential avoids silently connecting to a stray localhost server.
if (!host && !password) return null;
const user = process.env["ZC_POSTGRES_USER"] || "scuser";
const port = process.env["ZC_POSTGRES_PORT"] || "5432";
const db = process.env["ZC_POSTGRES_DB"] || "securecontext";
const auth = `${encodeURIComponent(user)}${password ? ":" + encodeURIComponent(password) : ""}`;
return `postgresql://${auth}@${host || "localhost"}:${port}/${db}`;
}
export async function createStore(): Promise<Store> {
const backend = process.env["ZC_STORE"] ?? "sqlite";
if (backend === "postgres") {
const pgUrl = resolvePgConnectionString();
if (!pgUrl) {
throw new Error(
"ZC_STORE=postgres needs connection details. Any of these works:\n" +
" ZC_POSTGRES_URL=postgresql://user:pass@host:5432/securecontext (same var pg_pool.ts uses)\n" +
" ZC_POSTGRES_HOST / _PORT / _USER / _PASSWORD / _DB (parts)\n" +
" ZC_PG_URL=... (legacy alias)"
);
}
const { PostgresStore } = await import("./store-postgres.js");
const store = new PostgresStore(pgUrl);
await store.init();
return store;
}
// Default: SQLite
const { SqliteStore } = await import("./store-sqlite.js");
return new SqliteStore();
}