Skip to content

P1 (v0.5): Hierarchical + dialectical memory — full implementation#69

Merged
devlux76 merged 9 commits intomainfrom
copilot/p1-subtask-issues
Mar 14, 2026
Merged

P1 (v0.5): Hierarchical + dialectical memory — full implementation#69
devlux76 merged 9 commits intomainfrom
copilot/p1-subtask-issues

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

  • P0-X: Fix architectural naming drift (MetroidNeighbor → SemanticNeighbor, MetroidSubgraph → SemanticNeighborSubgraph, related method renames, DB store neighbor_graph, DB_VERSION=3)
  • P1-A: Implement HierarchyBuilder.ts (Page→Book→Volume→Shelf clustering with hotpath admission) + upgrade Ingest.ts + tests
    • Fix: add prev/next SemanticNeighbor edges between consecutive pages within each book slice (document-order adjacency edges, bypass cosine cutoff, respect maxDegree=16)
    • Fix: replace O(n²) pageIds.indexOf() with O(1) Map lookups; update DESIGN.md
  • Fix FastNeighborInsert.ts: filter missing allPageIds (b), throw for missing newPageIds (a)
  • Fix MetroidBuilder.ts: m2 medoid selection uses free-dim slices (not full embeddings)
  • Fix Query.ts: remove unused vectorBackend from QueryOptions
  • Fix Ingest.ts: ONE Book per ingestText call (all pages + single medoid) — volumes/shelves are Daydreamer responsibilities
  • Update IndexedDbMetadataStore.ts: align STORE constant name (neighborGraph), restructure applyUpgrade to match main's clean v3 section
  • Update DESIGN.md: remove stale MetroidNeighbor naming-error notes (already fixed by P0-X on main); keep P1 additions (ONE Book per ingest, fast/lightweight ingest strategy, DB upgrade rationale)
  • Rebase onto main: align TODO.md, PLAN.md, README.md, docs/api.md to main's updated versions (P0-X tasks done, American spelling); delete ARCHITECTURE-REVIEW.md and close-legacy-issues.yml
Original prompt

This section details on the original issue you should resolve

<issue_title>P1</issue_title>
<issue_description>All P1 tasks, issues and sub issues belong here, either directly or by reference. Please subtask every issue with P1 in its name for ease of tracking.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 4 commits March 13, 2026 22:28
…d upgrade Ingest

- HierarchyBuilder: cluster pages→books→volumes→shelves using cosine-distance
  medoid selection; persist each tier; run promotion sweeps after each level
- FastNeighborInsert: build sparse SemanticNeighbor graph (NOT Hebbian);
  forward + symmetric reverse edges; degree-bounded with eviction; marks
  affected volumes dirty; runs promotion sweep for new pages
- Ingest: replace manual single-book construction with buildHierarchy;
  add insertSemanticNeighbors call; extend IngestResult with volumes/shelves
- Tests: 11 HierarchyBuilder tests + 8 FastNeighborInsert tests (230 total pass)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- cortex/OpenTSPSolver.ts: greedy nearest-neighbor open-path TSP heuristic
  with deterministic start (lex smallest node) and tiebreaker
- cortex/MetroidBuilder.ts: dialectical probe builder selecting m1 (thesis)
  and m2 (antithesis medoid) with Matryoshka dimensional unwinding across
  standard tiers [32,64,128,256,512,768,1024,2048]; computes frozen synthesis
  centroid c
- cortex/KnowledgeGapDetector.ts: detectKnowledgeGap + buildCuriosityProbe
  with deterministic SHA-256 probeId
- cortex/QueryResult.ts: add coherencePath, metroid, knowledgeGap fields
- cortex/Query.ts: populate new QueryResult fields with flat-query defaults
- 32 new tests across OpenTSPSolver, MetroidBuilder, KnowledgeGapDetector

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add cortex/Ranking.ts: rankPages/rankBooks/rankVolumes/rankShelves
  use cosine similarity against entity representative vectors;
  spillToWarm('page') scores the full corpus as fallback

- Rewrite cortex/Query.ts (P1-E1): full dialectical orchestrator
  - HOT path: rankPages() over resident hotpath entries
  - Warm spill: spillToWarm('page') when hot path is insufficient
  - buildMetroid() with book medoid + page candidates
  - detectKnowledgeGap() from metroid result
  - getInducedNeighborSubgraph() + solveOpenTSP() for coherence path
  - queryHitCount increment + runPromotionSweep()
  - QueryResult now fully populated (coherencePath, metroid, knowledgeGap)

- Add tests/cortex/Ranking.test.ts: 12 tests covering empty inputs,
  cosine scoring, descending order, topK, spillToWarm all tiers

- Update tests/cortex/Query.test.ts: add assertions for new fields
  (coherencePath, metroid, knowledgeGap); preserve all existing
  behavioral checks; update describe label to reflect new orchestrator

All 274 tests pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tor, P1-B Ranking, P1-E Query rewrite, P1-F integration tests

Co-authored-by: devlux76 <86517969+devlux76@users.noreply.github.com>
Copilot AI changed the title [WIP] [P1] Subtask all P1 tasks and issues for tracking P1 (v0.5): Hierarchical + dialectical memory — full implementation Mar 13, 2026
Copilot AI requested a review from devlux76 March 13, 2026 22:49
@devlux76 devlux76 marked this pull request as ready for review March 13, 2026 22:51
Copilot AI review requested due to automatic review settings March 13, 2026 22:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements the v0.5 (P1) “hierarchical + dialectical memory” slice by adding hierarchy construction (Pages→Books→Volumes→Shelves), semantic neighbor graph storage/retrieval, dialectical probing (Metroid + knowledge gap), coherence path derivation (open TSP), and updated query orchestration, alongside substantial new unit/integration test coverage.

Changes:

  • Adds hierarchy builder + semantic neighbor insertion to ingest, with IndexedDB schema updates for the neighbor graph.
  • Introduces cortex modules for ranking, dialectical Metroid construction, knowledge gap detection, and coherence path solving; rewrites cortex/query.
  • Adds/updates tests to cover hierarchy persistence, neighbor graph behavior, ranking/query behavior, and knowledge gap signaling.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/integration/IngestQuery.test.ts Adds v0.5 integration tests for hierarchy, neighbors, Williams bound, knowledge-gap signaling.
tests/hippocampus/HierarchyBuilder.test.ts New unit tests for hierarchy building + persistence + hotpath admission.
tests/hippocampus/FastNeighborInsert.test.ts New unit tests for semantic neighbor insertion behavior and constraints.
tests/cortex/Ranking.test.ts New unit tests for ranking and warm spill behavior.
tests/cortex/Query.test.ts Updates query tests for new QueryResult fields and dialectical behavior.
tests/cortex/OpenTSPSolver.test.ts New unit tests for deterministic greedy open-path TSP.
tests/cortex/MetroidBuilder.test.ts New unit tests for Metroid construction + determinism + Matryoshka behavior.
tests/cortex/KnowledgeGapDetector.test.ts New unit tests for knowledge gap detection and curiosity probe creation.
tests/SalienceEngine.test.ts Updates mock MetadataStore to renamed neighbor-graph APIs.
tests/Persistence.test.ts Updates persistence tests to renamed neighbor-graph types/APIs.
storage/IndexedDbMetadataStore.ts Bumps DB version; renames neighbor store to neighbor_graph; renames APIs.
hippocampus/Ingest.ts Integrates hierarchy building + semantic neighbor insertion into ingest pipeline.
hippocampus/HierarchyBuilder.ts New hierarchy builder (books/volumes/shelves) + prototype vector creation.
hippocampus/FastNeighborInsert.ts New semantic neighbor insertion implementation + dirty-volume recalc flagging.
cortex/Ranking.ts New ranking functions for pages/books/volumes/shelves + warm spill for pages.
cortex/QueryResult.ts Extends query result schema with coherencePath/metroid/knowledgeGap fields.
cortex/Query.ts Rewrites query orchestration to use ranking→metroid→gap→subgraph→TSP pipeline.
cortex/OpenTSPSolver.ts New deterministic greedy open-path TSP heuristic over neighbor subgraph.
cortex/MetroidBuilder.ts New Metroid builder with Matryoshka unwinding and centroid freezing.
cortex/KnowledgeGapDetector.ts New knowledge gap detection + curiosity probe builder.
core/types.ts Renames Metroid neighbor graph types/APIs to SemanticNeighbor equivalents.

Comment on lines +105 to +116
const pageChunks = chunkArray(pageIds, PAGES_PER_BOOK);
const books: Book[] = [];

for (const chunk of pageChunks) {
const sortedChunk = [...chunk].sort();
const bookId = await hashText(sortedChunk.join("|"));

const chunkVectors = chunk.map((id) => {
const idx = pageIds.indexOf(id);
return pageVectors[idx];
});

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Also verify prev/next edges are added to nodes that follow one another directly from slicing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in de785cd. HierarchyBuilder.buildHierarchy now inserts SemanticNeighbor edges between every consecutive pair of pages within each book slice — both the forward (A→B) and reverse (B→A) edges — using the actual cosine similarity. These document-order adjacency edges bypass the cosine-distance cutoff (adjacent text chunks of the same source are always related) but still respect a per-direction degree cap (16). A Map<Hash, Float32Array> was also introduced to replace the O(n²) indexOf calls. The FastNeighborInsert test fixture was updated to build pages directly without going through ingestText/buildHierarchy, keeping those tests isolated from the adjacency-edge insertion.

Comment on lines +112 to +140
const chunkVectors = chunk.map((id) => {
const idx = pageIds.indexOf(id);
return pageVectors[idx];
});

const medoidIdx = selectMedoidIndex(chunkVectors);
const medoidPageId = chunk[medoidIdx];

const book: Book = { bookId, pageIds: chunk, medoidPageId, meta: {} };
await metadataStore.putBook(book);
books.push(book);
}

await runPromotionSweep(books.map((b) => b.bookId), metadataStore, policy);

// -------------------------------------------------------------------------
// Level 2: Books → Volumes
// -------------------------------------------------------------------------
const bookChunks = chunkArray(books, BOOKS_PER_VOLUME);
const volumes: Volume[] = [];

for (const bookChunk of bookChunks) {
const sortedBookIds = bookChunk.map((b) => b.bookId).sort();
const volumeId = await hashText(sortedBookIds.join("|"));

const medoidVectors = bookChunk.map((b) => {
const idx = pageIds.indexOf(b.medoidPageId);
return pageVectors[idx];
});
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Ok I agree with this, just make sure to fix it using your best judgement and update the DESIGN.md to reflect this design decision. Insertion needs to be fast and lightweight. Daydreaming exists to find connections we never noticed before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b2fd983. The Map<Hash, Float32Array> (O(1) lookups) was already landed in the previous commit. DESIGN.md now explicitly documents the fast/lightweight ingest strategy: only document-order adjacency edges are created at ingest time using O(1) precomputed maps, and the Daydreamer is responsible for discovering the semantic cross-connections we never noticed before via its idle-pass graph walks.

Comment thread storage/IndexedDbMetadataStore.ts Outdated
Comment thread cortex/Query.ts
Comment thread cortex/MetroidBuilder.ts Outdated

if (oppositeSet.length === 0) return null;

const medoidIdx = findMedoidIndex(oppositeSet.map((s) => s.candidate.embedding));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Use your best judgement to resolve the discrepancy please.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b2fd983. searchM2 now calls findMedoidIndex(oppositeSet.map((s) => s.candidate.embedding.slice(protectedDim))) — the medoid is selected over the free-dimension slices only, matching the scoring used to build the opposite set.

Comment thread hippocampus/Ingest.ts Outdated
Comment thread hippocampus/FastNeighborInsert.ts
…ice pages in HierarchyBuilder

Co-authored-by: devlux76 <86517969+devlux76@users.noreply.github.com>
@devlux76
Copy link
Copy Markdown
Owner

@copilot Apparently the PR touched on some P0 issues that were being worked on concurrently. As a result there has been drift on main and this PR has merge conflicts. Rebase to head main and then roll forward applying the changes that still make sense in light of the higher priority P0 issues that were recently closed.

Sorry about that!

…move vectorBackend, ONE Book per ingest, DESIGN.md update

Co-authored-by: devlux76 <86517969+devlux76@users.noreply.github.com>
…e ARCHITECTURE-REVIEW.md and close-legacy-issues.yml

Co-authored-by: devlux76 <86517969+devlux76@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 14, 2026

@copilot Apparently the PR touched on some P0 issues that were being worked on concurrently. As a result there has been drift on main and this PR has merge conflicts. Rebase to head main and then roll...

Done in dc04d6f. The branch is now aligned with main's HEAD (619374d):

  • ARCHITECTURE-REVIEW.md and close-legacy-issues.yml deleted (removed on main)
  • TODO.md / PLAN.md / README.md / docs/api.md taken from main (P0-X tasks marked done, American spelling)
  • DESIGN.md stale "MetroidNeighbor is a naming error" notes removed (P0-X is done; only our P1 additions remain)
  • storage/IndexedDbMetadataStore.ts STORE constant renamed metroidNeighborsneighborGraph; applyUpgrade restructured to match main's clean v3 section (neighbor_graph created after v2 stores)
  • core/types.ts "neighbour" → "neighbor" comment spelling fixed

All 280 tests pass, build and lint clean.

Copy link
Copy Markdown
Owner

@devlux76 devlux76 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work!

@github-actions github-actions Bot added layer: foundation Core types, model profiles, crypto layer: storage OPFS vector store and IndexedDB metadata store labels Mar 14, 2026
@github-actions github-actions Bot added layer: hippocampus Ingest orchestration (chunk → embed → persist) layer: cortex Retrieval orchestration (rank → expand → order) layer: testing Test coverage and integration tests layer: documentation API docs, developer guide, architecture diagrams labels Mar 14, 2026
@devlux76 devlux76 mentioned this pull request Mar 14, 2026
Closed
@devlux76 devlux76 merged commit e6fc325 into main Mar 14, 2026
3 checks passed
@devlux76 devlux76 deleted the copilot/p1-subtask-issues branch March 14, 2026 00:38
@devlux76 devlux76 mentioned this pull request Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

layer: cortex Retrieval orchestration (rank → expand → order) layer: documentation API docs, developer guide, architecture diagrams layer: foundation Core types, model profiles, crypto layer: hippocampus Ingest orchestration (chunk → embed → persist) layer: storage OPFS vector store and IndexedDB metadata store layer: testing Test coverage and integration tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P1

3 participants