-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstore.ts
More file actions
270 lines (237 loc) · 14.5 KB
/
Copy pathstore.ts
File metadata and controls
270 lines (237 loc) · 14.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
/**
* 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 (requires ZC_PG_URL)
*
* 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:/Users/Amit/AI_projects/RevClear").
* 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
// ─────────────────────────────────────────────────────────────────────────────
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 ──────────────────────────────────────────────────────────
remember(projectPath: string, key: string, value: string, importance: number, agentId: string, epi?: EpistemicOpts): Promise<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 }): Promise<MemoryFact[]>;
archiveSummary(projectPath: string, summary: string): Promise<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>;
// ── 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
// ─────────────────────────────────────────────────────────────────────────────
/** Derive the 16-hex-char project discriminator from the raw path. */
export function projectHash(projectPath: string): string {
return createHash("sha256").update(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.
*/
export async function createStore(): Promise<Store> {
const backend = process.env["ZC_STORE"] ?? "sqlite";
if (backend === "postgres") {
const pgUrl = process.env["ZC_PG_URL"];
if (!pgUrl) {
throw new Error(
"ZC_STORE=postgres requires ZC_PG_URL to be set.\n" +
"Example: ZC_PG_URL=postgresql://postgres:password@localhost:5432/securecontext"
);
}
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();
}