Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
Add pruning/decay and a significance gate to the watcher
anirudhkumar-nanonets43dcab2ad37e
diff --git a/src/ai/providers.ts b/src/ai/providers.tsindex f51a8b96..bf639562 100644--- a/src/ai/providers.ts+++ b/src/ai/providers.ts@@ -61,6 +61,16 @@ export interface EngineConfig {*/mergeThreshold?: number;+ /**+ * When a changed file is re-ingested, prune the observations contributed by+ * its now-superseded prior version: facts that survived into the new version+ * are re-observed and stay, while facts that disappeared decay out (and orphan+ * nodes/edges are removed). This is what keeps the graph fresh instead of+ * accumulating stale facts forever. Env: CONTEXT_GRAPH_PRUNE (set to 0 to+ * disable). Default: true.+ */+ pruneSuperseded?: boolean;+// --- advanced: bring your own components ---/** Override the storage backend (defaults to a SQLite store at dbPath). */store?: GraphStore;@@ -87,6 +97,7 @@ export interface ResolvedConfig {chunkSize: number;chunkOverlap: number;mergeThreshold: number;+ pruneSuperseded: boolean;store?: GraphStore;embedder?: Embedder;extractor?: Extractor;@@ -137,6 +148,9 @@ export function resolveConfig(config: EngineConfig = {}): ResolvedConfig {chunkSize: config.chunkSize ?? DEFAULTS.chunkSize,chunkOverlap: config.chunkOverlap ?? DEFAULTS.chunkOverlap,mergeThreshold: config.mergeThreshold ?? DEFAULTS.mergeThreshold,+ pruneSuperseded:+ config.pruneSuperseded ??+ !["0", "false", "no"].includes((env.CONTEXT_GRAPH_PRUNE ?? "").toLowerCase()),store: config.store,embedder: config.embedder,extractor: config.extractor,diff --git a/src/cli.ts b/src/cli.tsindex 8889cc67..a9dfff93 100644--- a/src/cli.ts+++ b/src/cli.ts@@ -157,9 +157,14 @@ function logWatchEvent(event: WatchEvent): void {: `✓ ${event.result.title} — ${event.result.chunks} chunks, +${event.result.nodesCreated} nodes, +${event.result.edgesCreated} edges`,);break;- case "deleted":- console.log(`• ${event.file} — deleted (knowledge kept, graph is append-only)`);+ case "deleted": {+ const p = event.pruned;+ const detail = p+ ? ` (pruned ${p.nodesRemoved} entities, ${p.edgesRemoved} relationships; ${p.nodesDecayed} decayed)`+ : "";+ console.log(`• ${event.file} — deleted${detail}`);break;+ }case "error":console.error(`✗ ${event.file}: ${event.error}`);break;diff --git a/src/engine.ts b/src/engine.tsindex 73b7ce87..7a636dfd 100644--- a/src/engine.ts+++ b/src/engine.ts@@ -13,6 +13,7 @@ import { LocalEmbedder, OllamaExtractor } from "./ai/local.js";import { OllamaSummarizer, OpenRouterSummarizer, type Summarizer } from "./ai/summarize.js";import { chunkText } from "./ingest/chunker.js";import { mergeExtraction } from "./graph/merge.js";+import { confidenceFromObservations, totalObservations } from "./graph/crdt.js";import { serializeGraph, importGraph, type ImportResult } from "./graph/serialize.js";import { retrieve, type RetrieveOptions } from "./retrieval/retriever.js";import type { ContextBundle, Chunk, Extraction, GraphStats } from "./graph/types.js";@@ -52,6 +53,18 @@ export interface ContributeResult {edgesUpdated: number;}+/** Outcome of pruning stale observations (see {@link ContextGraphEngine.pruneDocuments}). */+export interface PruneResult {+ /** Nodes deleted because their last supporting observation was removed. */+ nodesRemoved: number;+ /** Nodes that lost an observation but retained others (confidence re-derived). */+ nodesDecayed: number;+ /** Edges deleted because their last supporting observation was removed. */+ edgesRemoved: number;+ /** Edges that lost an observation but retained others. */+ edgesDecayed: number;+}+/** Extensions treated as source code by {@link ContextGraphEngine.ingestRepo}. */export const CODE_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs",@@ -215,9 +228,12 @@ export class ContextGraphEngine {// Same source, new content: the file changed, so replace its stale// document(s) instead of piling up superseded chunks. "inline" is the// catch-all source for pasted text and is exempt — many unrelated- // documents legitimately share it.+ // documents legitimately share it. The prior ids are remembered so their+ // observations can be pruned after the new content merges in (below).+ const supersededIds: string[] = [];if (source !== "inline") {for (const prior of await this.store.documentsBySource(source)) {+ supersededIds.push(prior.id);await this.store.deleteDocument(prior.id);}}@@ -264,6 +280,15 @@ export class ContextGraphEngine {totals.edgesUpdated += r.edgesUpdated;}+ // Now that the new version has re-observed everything it still asserts,+ // retire the superseded version's observations. Facts present in both+ // versions keep the fresh observation; facts that vanished lose their last+ // support and are pruned. Runs after the merge so a fact never briefly+ // drops to zero observations mid-update.+ if (this.cfg.pruneSuperseded && supersededIds.length > 0) {+ await this.pruneDocuments(supersededIds);+ }+return { documentId, title, skipped: false, chunks: pieces.length, ...totals };}@@ -570,6 +595,101 @@ export class ContextGraphEngine {};}+ /**+ * Prune the observations contributed by one or more documents.+ *+ * For every node and edge, the given documents' observation keys are removed+ * from the grow-only CRDT counter and provenance set, and `observations` /+ * `confidence` are re-derived from what remains. A node or edge left with no+ * supporting observation is deleted entirely (deleting a node cascades to its+ * incident edges). Observations from *other* documents and from agent+ * contributions are untouched — so a fact seen in several places survives the+ * loss of any one of them.+ *+ * This is the counterpart to the grow-only merge: the deliberate, local+ * write path that lets stale knowledge leave the graph.+ */+ async pruneDocuments(documentIds: string[]): Promise<PruneResult> {+ const keys = new Set(documentIds.map((id) => `doc:${id}`));+ const result: PruneResult = { nodesRemoved: 0, nodesDecayed: 0, edgesRemoved: 0, edgesDecayed: 0 };+ if (keys.size === 0) return result;++ // Nodes first. An orphaned node is removed, which cascades to its incident+ // edges via the store's foreign keys — so we record those edge ids up front+ // to count them, then let the cascade do the deletion.+ const orphanedNodeIds: string[] = [];+ for (const node of await this.store.allNodes()) {+ let touched = false;+ for (const key of keys) {+ if (key in node.observationSources) {+ delete node.observationSources[key];+ touched = true;+ }+ }+ if (!touched) continue;+ node.provenance = node.provenance.filter((p) => !keys.has(p));+ const total = totalObservations(node.observationSources);+ if (total <= 0) {+ orphanedNodeIds.push(node.id);+ } else {+ node.observations = total;+ node.confidence = confidenceFromObservations(total);+ await this.store.upsertNode(node);+ result.nodesDecayed += 1;+ }+ }++ // Edges that will vanish with their endpoints, counted before the cascade.+ const cascadedEdgeIds = new Set(+ orphanedNodeIds.length > 0+ ? (await this.store.edgesForNodes(orphanedNodeIds)).map((e) => e.id)+ : [],+ );+ for (const id of orphanedNodeIds) await this.store.deleteNode(id);+ result.nodesRemoved += orphanedNodeIds.length;+ result.edgesRemoved += cascadedEdgeIds.size;++ // Surviving edges that themselves lost their last observation. (Edges that+ // already cascaded above are gone from the store and won't reappear here.)+ for (const edge of await this.store.allEdges()) {+ let touched = false;+ for (const key of keys) {+ if (key in edge.observationSources) {+ delete edge.observationSources[key];+ touched = true;+ }+ }+ if (!touched) continue;+ edge.provenance = edge.provenance.filter((p) => !keys.has(p));+ const total = totalObservations(edge.observationSources);+ if (total <= 0) {+ await this.store.deleteEdge(edge.id);+ result.edgesRemoved += 1;+ } else {+ edge.observations = total;+ edge.confidence = confidenceFromObservations(total);+ await this.store.upsertEdge(edge);+ result.edgesDecayed += 1;+ }+ }++ return result;+ }++ /**+ * Forget a source that no longer exists (e.g. a watched file was deleted):+ * delete its document(s) and prune the observations they contributed. Returns+ * the prune result plus how many documents were removed. A source with no+ * documents is a no-op.+ */+ async forgetSource(source: string): Promise<PruneResult & { documentsRemoved: number }> {+ const docs = await this.store.documentsBySource(source);+ const ids = docs.map((d) => d.id);+ for (const id of ids) await this.store.deleteDocument(id);+ const pruned = await this.pruneDocuments(ids);+ return { ...pruned, documentsRemoved: ids.length };+ }+/** Current graph statistics. */async stats(): Promise<GraphStats> {return this.store.stats();diff --git a/src/graph/sqlite-store.ts b/src/graph/sqlite-store.tsindex 94a14b13..f1e090a8 100644--- a/src/graph/sqlite-store.ts+++ b/src/graph/sqlite-store.ts@@ -336,6 +336,12 @@ export class SqliteStore implements GraphStore {});}+ async deleteNode(id: string): Promise<void> {+ // Incident edges cascade via the source_id/target_id foreign keys+ // (foreign_keys = ON, set in the constructor).+ this.db.prepare(`DELETE FROM nodes WHERE id = ?`).run(id);+ }+private rowToNode(row: NodeRow): GraphNode {const observationSources = parseCounter(row.observation_sources, row.observations);return {@@ -404,6 +410,10 @@ export class SqliteStore implements GraphStore {});}+ async deleteEdge(id: string): Promise<void> {+ this.db.prepare(`DELETE FROM edges WHERE id = ?`).run(id);+ }+async edgesForNodes(nodeIds: string[]): Promise<GraphEdge[]> {if (nodeIds.length === 0) return [];const placeholders = nodeIds.map(() => "?").join(",");diff --git a/src/graph/store.ts b/src/graph/store.tsindex 666f8da5..fb6d0093 100644--- a/src/graph/store.ts+++ b/src/graph/store.ts@@ -52,11 +52,18 @@ export interface GraphStore {/** Every node in the graph (for export/visualization). */allNodes(): Promise<GraphNode[]>;upsertNode(node: GraphNode): Promise<void>;+ /**+ * Delete a node and (via the edge foreign keys) every edge incident to it.+ * Used by the pruning path when a node's last supporting source disappears.+ */+ deleteNode(id: string): Promise<void>;// --- edges ---/** Existing edge with the same (source, target, relation), if any. */findEdge(sourceId: string, targetId: string, relation: string): Promise<GraphEdge | undefined>;upsertEdge(edge: GraphEdge): Promise<void>;+ /** Delete a single edge by id (used when its last supporting source disappears). */+ deleteEdge(id: string): Promise<void>;/** All edges incident to any of the given node ids. */edgesForNodes(nodeIds: string[]): Promise<GraphEdge[]>;/** Every edge in the graph (for export/visualization). */diff --git a/src/index.ts b/src/index.tsindex 0892216d..a208dfcc 100644--- a/src/index.ts+++ b/src/index.ts@@ -22,6 +22,7 @@ export type {IngestResult,ContributeOptions,ContributeResult,+ PruneResult,RepoIngestOptions,RepoIngestResult,WatchedDir,diff --git a/src/mcp.ts b/src/mcp.tsindex 28a37dda..ed21974e 100644--- a/src/mcp.ts+++ b/src/mcp.ts@@ -241,7 +241,8 @@ server.registerTool("Connect a folder to the graph and keep it evolving: existing documents are ingested now, " +"and any file added or edited later is re-ingested automatically for as long as this session " +"lives. The folder is registered, so `context-graph watch` (or CONTEXT_GRAPH_AUTOWATCH=1) " +- "resumes it in future sessions. Append-only: deleting a file never removes learned knowledge.",+ "resumes it in future sessions. Deleting a file forgets what it taught: its facts are pruned " ++ "unless still supported by another source.",inputSchema: {dir: z.string().describe("Absolute path to the folder to watch."),extensions: zdiff --git a/src/watch.ts b/src/watch.tsindex 2db7ec0d..c2613cf9 100644--- a/src/watch.ts+++ b/src/watch.ts@@ -1,21 +1,32 @@/*** Auto-watch: keep the graph evolving as connected folders change.*- * A {@link GraphWatcher} watches document folders and re-ingests files as they- * are added or edited, so the graph tracks the folder without manual re-runs- * of `ingest-dir`. Deliberately append-only: deleting a file never removes- * knowledge already learned from it (the grow-only observation model is- * untouched); the deletion is surfaced as an event and nothing more.+ * A {@link GraphWatcher} watches document folders and keeps the graph tracking+ * them without manual re-runs of `ingest-dir`. It is *smart* about change in+ * two ways:*- * Rapid saves are absorbed by a per-file debounce, and files are ingested by a+ * - **Significance gate.** A save that only reformats a file (whitespace,+ * blank lines, line endings) leaves its normalized content unchanged, so it+ * is skipped without an LLM call. Only meaningful edits are re-ingested.+ * - **Pruning on delete.** Deleting a watched file forgets what it taught: its+ * documents are removed and the observations they contributed are pruned, so+ * facts that lose their last source decay out of the graph (see+ * {@link ContextGraphEngine.forgetSource}). Facts still supported elsewhere+ * survive.+ *+ * Rapid saves are absorbed by a per-file debounce, and files are processed by a* single serial worker — each ingest already fans out to concurrent LLM* extraction internally, so one document at a time keeps spend predictable.+ * Deletes flow through the same worker, so a delete can never race an in-flight+ * ingest of the same file.*/import { watch, type FSWatcher } from "chokidar";-import type { Stats } from "node:fs";-import { resolve, sep } from "node:path";-import type { ContextGraphEngine, IngestResult } from "./engine.js";+import { readFileSync, type Stats } from "node:fs";+import { basename, resolve, sep } from "node:path";+import type { ContextGraphEngine, IngestResult, PruneResult } from "./engine.js";import { DOC_EXTENSIONS, isIngestablePath, shouldIgnorePath, walkDir } from "./ingest/fs.js";+import { isPdfPath } from "./ingest/pdf.js";+import { contentHash } from "./util/id.js";export interface WatchOptions {/** Extensions to ingest. Default: {@link DOC_EXTENSIONS}. */@@ -32,7 +43,7 @@ export type WatchEvent =| { type: "watching"; dir: string; files: number }| { type: "queued"; file: string; reason: "add" | "change" | "initial" }| { type: "ingested"; file: string; result: IngestResult }- | { type: "deleted"; file: string }+ | { type: "deleted"; file: string; pruned?: PruneResult & { documentsRemoved: number } }| { type: "error"; file: string; error: string };/**@@ -45,6 +56,10 @@ export class GraphWatcher {private watchers = new Map<string, FSWatcher>();private debounces = new Map<string, NodeJS.Timeout>();private pending = new Set<string>();+ /** Deleted files awaiting a prune pass, drained by the same serial worker. */+ private pendingDeletes = new Set<string>();+ /** Per-path normalized-content hash of the last ingested version (significance gate). */+ private contentHashes = new Map<string, string>();private inFlight?: string;private working = false;private closed = false;@@ -88,11 +103,18 @@ export class GraphWatcher {watcher.on("add", onFile("add"));watcher.on("change", onFile("change"));watcher.on("unlink", (path: string) => {- // Append-only by design: the file is gone but what it taught us stays.+ if (this.closed) return;+ // Only files we would have ingested are worth forgetting. (unlink has no+ // stats to hand shouldIgnorePath, so match on extension + ignore rules.)+ if (!this.extensions.some((e) => path.toLowerCase().endsWith(e))) return;+ if (shouldIgnorePath(path, root)) return;+ // The file is gone: cancel any queued (re-)ingest and queue a prune.+ const timer = this.debounces.get(path);+ if (timer) clearTimeout(timer);this.debounces.delete(path);this.pending.delete(path);- this.emit({ type: "deleted", file: path });- this.checkIdle();+ this.pendingDeletes.add(path);+ this.kick();});watcher.on("error", (err) => {this.emit({ type: "error", file: root, error: err instanceof Error ? err.message : String(err) });@@ -133,6 +155,9 @@ export class GraphWatcher {for (const path of this.pending) {if (under(path)) this.pending.delete(path);}+ for (const path of this.pendingDeletes) {+ if (under(path)) this.pendingDeletes.delete(path);+ }this.checkIdle();}@@ -141,9 +166,14 @@ export class GraphWatcher {return [...this.watchers.keys()];}- /** Files queued or being ingested right now. */+ /** Files queued (ingest or delete) or being processed right now. */pendingCount(): number {- return this.pending.size + this.debounces.size + (this.inFlight ? 1 : 0);+ return (+ this.pending.size ++ this.pendingDeletes.size ++ this.debounces.size ++ (this.inFlight ? 1 : 0)+ );}/** Resolves once every queued and debouncing file has been ingested. */@@ -160,6 +190,7 @@ export class GraphWatcher {for (const timer of this.debounces.values()) clearTimeout(timer);this.debounces.clear();this.pending.clear();+ this.pendingDeletes.clear();await this.idle();}@@ -172,6 +203,9 @@ export class GraphWatcher {setTimeout(() => {this.debounces.delete(path);if (this.closed) return this.checkIdle();+ // A fresh add/change wins over a pending delete for the same path+ // (e.g. an editor's delete-then-rename save).+ this.pendingDeletes.delete(path);this.pending.add(path);this.kick();}, this.debounceMs),@@ -195,15 +229,36 @@ export class GraphWatcher {}private async work(): Promise<void> {- for (let next = this.takeNext(); next; next = this.takeNext()) {+ for (;;) {+ // Drain deletes ahead of ingests so a removed file's knowledge is+ // forgotten promptly, before we spend effort re-reading anything.+ const del = this.takeNextDelete();+ if (del) {+ this.inFlight = del;+ try {+ const pruned = await this.engine.forgetSource(del);+ this.contentHashes.delete(del);+ this.emit({ type: "deleted", file: del, pruned });+ } catch (err) {+ this.emit({ type: "error", file: del, error: err instanceof Error ? err.message : String(err) });+ } finally {+ this.inFlight = undefined;+ }+ continue;+ }++ const next = this.takeNext();+ if (!next) break;this.inFlight = next;try {- const result = await this.engine.ingestFile(next);+ const result = await this.ingestWithSignificance(next);this.emit({ type: "ingested", file: next, result });} catch (err) {// The file vanished between the event and the ingest — that's a delete.if ((err as NodeJS.ErrnoException).code === "ENOENT") {- this.emit({ type: "deleted", file: next });+ const pruned = await this.engine.forgetSource(next).catch(() => undefined);+ this.contentHashes.delete(next);+ this.emit({ type: "deleted", file: next, pruned });} else {this.emit({ type: "error", file: next, error: err instanceof Error ? err.message : String(err) });}@@ -213,9 +268,73 @@ export class GraphWatcher {}}+ /**+ * Ingest a file, skipping the LLM pass when the change is insignificant.+ *+ * For text files we compare the *normalized* content (whitespace/line-ending+ * collapsed) against the last version ingested in this session; an unchanged+ * normalization means the save only reformatted the file, so we report it as+ * skipped without re-extracting. Meaningful edits go through the normal ingest+ * path (which itself dedups on exact content and prunes superseded facts).+ * PDFs skip the gate — extracting their text to hash it costs as much as+ * ingesting — and rely on the engine's exact-content dedup.+ */+ private async ingestWithSignificance(path: string): Promise<IngestResult> {+ if (isPdfPath(path)) return this.engine.ingestFile(path);++ let text: string;+ try {+ text = readFileSync(path, "utf8");+ } catch {+ // Let the normal path surface the real error (or ENOENT → delete).+ return this.engine.ingestFile(path);+ }++ const nhash = contentHash(normalizeForSignificance(text));+ if (this.contentHashes.get(path) === nhash) {+ return {+ documentId: "",+ title: basename(path),+ skipped: true,+ chunks: 0,+ nodesCreated: 0,+ nodesUpdated: 0,+ edgesCreated: 0,+ edgesUpdated: 0,+ };+ }++ const result = await this.engine.ingest(text, { title: basename(path), source: path });+ this.contentHashes.set(path, nhash);+ return result;+ }+private takeNext(): string | undefined {const next = this.pending.values().next().value as string | undefined;if (next !== undefined) this.pending.delete(next);return next;}++ private takeNextDelete(): string | undefined {+ const next = this.pendingDeletes.values().next().value as string | undefined;+ if (next !== undefined) this.pendingDeletes.delete(next);+ return next;+ }+}++/**+ * Collapse formatting-only differences so a reformat hashes identically to the+ * original: normalize line endings, strip trailing whitespace, collapse runs of+ * blank lines and inner whitespace, and trim. Deliberately conservative — it+ * only ignores layout, never content.+ */+function normalizeForSignificance(text: string): string {+ return text+ .replace(/\r\n?/g, "\n")+ .split("\n")+ .map((line) => line.replace(/[ \t]+$/g, ""))+ .join("\n")+ .replace(/\n{3,}/g, "\n\n")+ .replace(/[ \t]+/g, " ")+ .trim();}diff --git a/src/web.ts b/src/web.tsindex 91f7edeb..deca28a5 100644--- a/src/web.ts+++ b/src/web.ts@@ -400,7 +400,9 @@ function ensureWebWatcher(): GraphWatcher {if (event.type === "ingested" && !event.result.skipped) {console.log(`watch: ✓ ${event.result.title} (+${event.result.nodesCreated} entities)`);} else if (event.type === "deleted") {- console.log(`watch: • ${event.file} deleted (knowledge kept)`);+ const p = event.pruned;+ const detail = p ? ` (pruned ${p.nodesRemoved} entities, ${p.edgesRemoved} relationships)` : "";+ console.log(`watch: • ${event.file} deleted${detail}`);} else if (event.type === "error") {console.error(`watch: ✗ ${event.file}: ${event.error}`);}diff --git a/test/watch.test.ts b/test/watch.test.tsindex 3db97491..fe55eb9d 100644--- a/test/watch.test.ts+++ b/test/watch.test.ts@@ -134,7 +134,7 @@ test("editing a file replaces its document instead of piling up copies", async (rmSync(dir, { recursive: true, force: true });});-test("deleting a file keeps its knowledge (append-only)", async () => {+test("deleting a file prunes the facts only it supported", async () => {const dir = tempDir();const e = engine();const c = collector();@@ -143,20 +143,116 @@ test("deleting a file keeps its knowledge (append-only)", async () => {writeFileSync(file, "[[Legacy System]] ==replaced_by==> [[New System]]");await w.add(dir);await w.idle();- const statsBefore = await e.stats();+ assert.equal((await e.store.findNodesByName("legacy system")).length, 1);rmSync(file);await waitFor(() => c.of("deleted").length === 1, "the deletion to be observed");await w.idle();- assert.deepEqual(await e.stats(), statsBefore, "nothing is pruned on delete");+ // The file was the only source for both entities and the relationship, so+ // all of it decays out of the graph.+ assert.equal((await e.store.findNodesByName("legacy system")).length, 0, "entity pruned");+ assert.equal((await e.store.findNodesByName("new system")).length, 0, "entity pruned");+ assert.deepEqual(await e.stats(), { documents: 0, nodes: 0, edges: 0, chunks: 0 });assert.equal(c.of("ingested").length, 1, "the delete triggered no ingest");+ const deleted = c.of("deleted")[0];+ assert.ok(deleted.type === "deleted" && deleted.pruned, "delete reports a prune result");+ assert.equal(deleted.pruned.documentsRemoved, 1);+ assert.equal(deleted.pruned.nodesRemoved, 2);+ assert.equal(deleted.pruned.edgesRemoved, 1);+await w.close();await e.close();rmSync(dir, { recursive: true, force: true });});+test("a shared fact survives when one of its sources is deleted", async () => {+ const dir = tempDir();+ const e = engine();+ const c = collector();+ const w = new GraphWatcher(e, { debounceMs: 50, onEvent: c.onEvent });+ writeFileSync(join(dir, "a.md"), "[[Shared Service]] ==owns==> [[Cache]]");+ writeFileSync(join(dir, "b.md"), "[[Shared Service]] ==owns==> [[Queue]]");+ await w.add(dir);+ await w.idle();+ const shared = (await e.store.findNodesByName("shared service"))[0];+ assert.equal(shared.observations, 2, "seen in both files");++ rmSync(join(dir, "a.md"));+ await waitFor(() => c.of("deleted").length === 1, "the deletion to be observed");+ await w.idle();++ // Shared Service and Queue live on (b.md still asserts them); Cache is gone.+ assert.equal((await e.store.findNodesByName("shared service")).length, 1, "shared entity kept");+ assert.equal((await e.store.findNodesByName("queue")).length, 1);+ assert.equal((await e.store.findNodesByName("cache")).length, 0, "orphaned entity pruned");+ const after = (await e.store.findNodesByName("shared service"))[0];+ assert.equal(after.observations, 1, "confidence decayed to the remaining source");++ await w.close();+ await e.close();+ rmSync(dir, { recursive: true, force: true });+});++test("a formatting-only save is skipped without re-extraction", async () => {+ const dir = tempDir();+ const e = engine();+ const c = collector();+ const w = new GraphWatcher(e, { debounceMs: 50, onEvent: c.onEvent });+ const file = join(dir, "notes.md");+ writeFileSync(file, "[[Payments]] ==charges==> [[Card]]");+ await w.add(dir);+ await w.idle();+ assert.equal(c.of("ingested").filter((ev) => ev.type === "ingested" && !ev.result.skipped).length, 1);++ // Reformat only: extra spaces, trailing whitespace, blank lines — same content.+ writeFileSync(file, " [[Payments]] ==charges==> [[Card]] \n\n\n");+ await waitFor(() => c.of("ingested").length === 2, "the reformat to be processed");+ await w.idle();++ const second = c.of("ingested")[1];+ assert.ok(second.type === "ingested" && second.result.skipped, "reformat skipped, no LLM pass");+ assert.equal((await e.stats()).documents, 1, "no new document");+ assert.equal((await e.store.findNodesByName("payments"))[0].observations, 1, "not re-observed");++ await w.close();+ await e.close();+ rmSync(dir, { recursive: true, force: true });+});++test("a meaningful edit decays facts dropped from the new version", async () => {+ const e = engine();+ await e.ingest("[[Alpha]] ==uses==> [[Beta]]", { source: "spec.md" });+ await e.ingest("[[Alpha]] ==needs==> [[Gamma]]", { source: "spec.md" });++ // Alpha appears in both versions and survives; Beta was dropped and decays;+ // Gamma is new.+ assert.equal((await e.store.findNodesByName("alpha")).length, 1, "kept across versions");+ assert.equal((await e.store.findNodesByName("beta")).length, 0, "dropped fact pruned");+ assert.equal((await e.store.findNodesByName("gamma")).length, 1, "new fact present");+ assert.equal((await e.store.findNodesByName("alpha"))[0].observations, 1, "decayed to the live version");++ const alphaId = (await e.store.findNodesByName("alpha"))[0].id;+ const gammaId = (await e.store.findNodesByName("gamma"))[0].id;+ assert.ok(await e.store.findEdge(alphaId, gammaId, "needs"), "new relationship exists");+ assert.equal((await e.stats()).documents, 1, "still one document for the source");++ await e.close();+});++test("pruning can be disabled, restoring grow-only behavior", async () => {+ const e = new ContextGraphEngine({ dbPath: ":memory:", pruneSuperseded: false, ...fakeProviders() });+ await e.ingest("[[One]] ==links==> [[Two]]", { source: "doc.md" });+ await e.ingest("[[One]] ==links==> [[Three]]", { source: "doc.md" });++ // With pruning off, the superseded "Two" lingers as before.+ assert.equal((await e.store.findNodesByName("two")).length, 1, "stale fact retained when pruning is off");+ assert.equal((await e.store.findNodesByName("three")).length, 1);++ await e.close();+});+test("wrong extensions, oversized files, and skip-dirs are ignored", async () => {const dir = tempDir();const e = engine();