Skip to content

Local-first 🧠 memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.

TypeScript

fix: keep generated cluster digests out of community detection (#240)

Serg Eroshef3d7185ef430
diff --git a/src/core/brain/link-graph/communities.ts b/src/core/brain/link-graph/communities.ts
index 91fba5fd..adb69821 100644
--- a/src/core/brain/link-graph/communities.ts
+++ b/src/core/brain/link-graph/communities.ts
@@ -111,10 +111,22 @@ function detectCommunitiesRun(
// call, so only the option-independent graph is shared.
const snapshot = getGraphSnapshot(store);
const pathById = snapshot.pathById;
- const adjacency = snapshot.adjacency;
+
+ // Generated cluster digests are outputs of this pass, not inputs (#233):
+ // once indexed, a digest links to every member and would join its own
+ // community, rename it (the id follows the leading member) and keep a
+ // link to the predecessor the stale sweep deletes. The shared snapshot
+ // stays whole (search and graph stats still see the digests); only
+ // detection drops them, edges included.
+ const adjacency = new Map<number, ReadonlySet<number>>();
+ for (const [node, neighbours] of snapshot.adjacency) {
+ if (isClusterOutput(pathById.get(node)!)) continue;
+ const kept = [...neighbours].filter((n) => !isClusterOutput(pathById.get(n)!));
+ if (kept.length > 0) adjacency.set(node, new Set(kept));
+ }
// Synchronous sweeps in sorted-id order; lowest label wins ties.
- const nodes = snapshot.nodesSorted;
+ const nodes = snapshot.nodesSorted.filter((n) => adjacency.has(n));
const labels = new Map<number, number>(nodes.map((n) => [n, n]));
for (let iteration = 0; iteration < maxIterations; iteration++) {
// Cooperative deadline: abort between sweeps (read-only pass).
@@ -191,6 +203,14 @@ function detectCommunitiesRun(
return communities.toSorted((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
}
+/** Vault-relative prefix of the materialized cluster notes. */
+const CLUSTERS_DIR_PREFIX = "Brain/clusters/";
+
+/** True for a note under the cluster-notes output directory. */
+function isClusterOutput(relPath: string): boolean {
+ return relPath.startsWith(CLUSTERS_DIR_PREFIX);
+}
+
/** Collision-free id: vault-relative path, `/` -> `-`, no `.md`. */
function communityId(relPath: string): string {
return relPath.replace(/\.md$/u, "").split("/").join("-");
diff --git a/tests/core/brain/communities.test.ts b/tests/core/brain/communities.test.ts
index bbe5065a..ca912240 100644
--- a/tests/core/brain/communities.test.ts
+++ b/tests/core/brain/communities.test.ts
@@ -182,6 +182,38 @@ describe("materializeClusterNotes", () => {
expect(second.removed).toEqual([]);
expect(existsSync(join(vault, "Brain", "clusters", "hand-written.md"))).toBe(true);
});
+
+ test("generated digests never feed back into detection across reindex cycles (#233)", async () => {
+ writeTwoCommunities();
+ const cycles: Array<{ ids: string[]; members: string[][]; removed: readonly string[] }> = [];
+ for (let cycle = 0; cycle < 4; cycle++) {
+ await indexVault(config);
+ const communities = await withStore((store) => detectCommunities(store, { minSize: 4 }));
+ const result = await withStore((store) =>
+ materializeClusterNotes(vault, communities, { store, now: NOW }),
+ );
+ cycles.push({
+ ids: communities.map((c) => c.id),
+ members: communities.map((c) => c.members.map((m) => m.path).toSorted()),
+ removed: result.removed,
+ });
+ // Every member a generated digest links to must still exist after
+ // the same pass's stale sweep.
+ const dir = join(vault, "Brain", "clusters");
+ for (const file of readdirSync(dir)) {
+ const [fm] = parseFrontmatter(join(dir, file));
+ for (const member of fm["members"] as string[]) {
+ expect(existsSync(join(vault, member))).toBe(true);
+ }
+ }
+ }
+ for (const cycle of cycles) {
+ expect(cycle.ids).toEqual(cycles[0]!.ids);
+ expect(cycle.members).toEqual(cycles[0]!.members);
+ expect(cycle.removed).toEqual([]);
+ }
+ expect(cycles[0]!.members.flat().some((p) => p.startsWith("Brain/clusters/"))).toBe(false);
+ });
});
describe("materializeClusterNotes batching (t_a286135c)", () => {