Skip to content

feat: Implement Singularity Equation (A* + Stigmergy) in Knowledge Graph - #259

Closed
web3guru888 wants to merge 1 commit into
MemPalace:mainfrom
web3guru888:feature/singularity-equation
Closed

feat: Implement Singularity Equation (A* + Stigmergy) in Knowledge Graph#259
web3guru888 wants to merge 1 commit into
MemPalace:mainfrom
web3guru888:feature/singularity-equation

Conversation

@web3guru888

Copy link
Copy Markdown

This PR adds the core elements of the 'Singularity Equation' to the temporal entity-relationship graph.

Changes:

  1. Schema Update: Added pheromone_level REAL DEFAULT 1.0 to the triples table.
  2. Pheromone Updates: Added update_pheromone() to deposit weights on successful domain paths.
  3. A Pathfinding*: Added astar_stigmergy_path() to traverse the knowledge graph using stigmergic pheromone trails combined with edge-weights.

This is part one of enabling autonomous discovery agent behaviors. See issue #258 for context.

This was referenced Apr 8, 2026
@web3guru888

Copy link
Copy Markdown
Author

Automated Context: This PR represents the execution of the Singularity Equation architecture. We look forward to community feedback and will be actively refining this PR based on reviews and further testing. The mempalace-scout and mempalace-engineer agent team is standing by to resolve any merge conflicts or test failures.

@bgauryy

bgauryy commented Apr 8, 2026

Copy link
Copy Markdown

PR Review: feat: Implement Singularity Equation (A* + Stigmergy) in Knowledge Graph

Executive Summary

Aspect Value
PR Goal Add pheromone-weighted A* pathfinding to KnowledgeGraph for "autonomous discovery" traversal
Files Changed 1 (mempalace/knowledge_graph.py)
Risk Level 🔴 HIGH - breaks existing databases, dead code, no tests, bot-authored
Review Effort 3/5 - single file but algorithmic review needed
Recommendation 🔄 REQUEST_CHANGES

Affected Areas: knowledge_graph.py schema, triple storage, new traversal API

Business Impact: None — the three new methods are unreachable from any CLI command, MCP tool, or search pipeline. Existing databases will break on the schema change.

Flow Changes: Adds pheromone_level column to triples table. Adds update_pheromone(), get_neighbors(), and astar_stigmergy_path() methods. No caller exists.

Ratings

Aspect Score
Correctness 1/5
Security 4/5
Performance 2/5
Maintainability 2/5

PR Health

Provenance

Field Value
PR Author web3guru888 (external, no prior contributions visible)
Commit Author MemPalace AGI Bot <bot@mempalace-agi.local>
Issue Author web3guru888 (same person opened #258 and this PR)

The commit is authored by a bot, not a human. The PR comment references "the mempalace-scout and mempalace-engineer agent team." This is AI-generated code that warrants extra scrutiny.

Critical Issues

1. Schema migration breaks existing databases

Severity: 🔴 Critical | knowledge_graph.py:66-79

The new pheromone_level column is added inside CREATE TABLE IF NOT EXISTS triples. SQLite skips this entire statement when the table already exists. Any user with an existing ~/.mempalace/knowledge_graph.sqlite3 will hit:

OperationalError: no such column: pheromone_level

Fix required: Add an ALTER TABLE triples ADD COLUMN pheromone_level REAL DEFAULT 1.0 migration in _init_db(), wrapped in a try/except for idempotency.

2. Entity ID double-normalization mismatch

Severity: 🔴 Critical | update_pheromone() vs get_neighbors() / astar_stigmergy_path()

get_neighbors() returns raw entity IDs from the database (already normalized via _entity_id()). But update_pheromone() calls _entity_id() on its inputs, normalizing again. If a caller uses path output from astar_stigmergy_path() to reinforce edges via update_pheromone(), the IDs get double-normalized. The API contract is inconsistent:

  • astar_stigmergy_path → returns {"from": entity_id, "to": entity_id} (already normalized)
  • update_pheromone(subject, predicate, obj) → expects human-readable names (normalizes internally)

3. Whitespace corruption in existing schema

Severity: 🟡 Medium | knowledge_graph.py:66

The diff gratuitously re-indents the existing CREATE TABLE IF NOT EXISTS triples line from 12-space to 24-space indentation. This pollutes git blame for no functional reason.

-            CREATE TABLE IF NOT EXISTS triples (
+                        CREATE TABLE IF NOT EXISTS triples (

Design Issues

4. No pheromone evaporation — ACO without decay doesn't converge properly

Severity: 🟡 Medium

Classic Ant Colony Optimization requires pheromone evaporation (τ = (1-ρ)τ) to prevent path lock-in. This implementation only deposits pheromone (+= delta) and never decays it. Over time, the first discovered path monopolizes all future traversals regardless of whether better alternatives emerge. This is a well-documented failure mode of ACO without evaporation.

5. A* heuristic is a constant — this is Dijkstra's algorithm

Severity: 🟡 Medium | astar_stigmergy_path() inner function h()

def h(node_id):
    if node_id == target_id:
        return 0.0
    return 1.0  # Base distance

A constant heuristic means A* degenerates to Dijkstra's algorithm. The docstring and PR title claim A* + ChromaDB embedding distance, but there is no ChromaDB integration, no injection point, and no interface for plugging one in. The name "A* + Stigmergy" overstates what the code actually does.

6. Dead code — zero integration with the system

Severity: 🟡 Medium

Per AGENTS.md, the search pipeline is: CLI → searcher.py → query_palace() → ChromaDB. This PR adds three methods to KnowledgeGraph with:

  • No CLI command (no cmd_* in cli.py)
  • No MCP tool (no handler in mcp_server.py)
  • No call from searcher.py or any other module

The code is unreachable from any user-facing entry point. It's dead on arrival.

Code Quality Issues

7. Zero tests for 132 lines of new logic

Severity: 🟡 Medium

The project has 275+ tests. This PR adds three public methods with no test coverage. Required tests:

  • update_pheromone — valid triple, nonexistent triple, negative delta
  • get_neighbors — outgoing edges, incoming edges, mixed, empty graph
  • astar_stigmergy_path — reachable path, unreachable target (returns None), cycle handling, single-hop, multi-hop
  • Migration — existing DB without pheromone_level column

8. O(n²) memory in pathfinding

Severity: 🟢 Low | astar_stigmergy_path() path accumulation

new_path = path + [{...}]

Each step copies the entire path list. Standard A* reconstructs the path from a came_from map at termination, using O(V) memory instead of O(V×P).

9. Connection leak risk

Severity: 🟢 Low | All three new methods

Connections are opened without try/finally or context managers. An exception between _conn() and close() leaks the SQLite connection. This matches the existing code pattern but new code should improve, not perpetuate it.

Recommendation

Close or request major revision. Specifically:

  1. Design first: Open an RFC/design doc. The concept (learned graph traversal) is interesting but needs architecture discussion — where does it fit in the search pipeline? How does ChromaDB heuristic injection work? What's the evaporation strategy?
  2. Fix the migration: ALTER TABLE ADD COLUMN with idempotent guard
  3. Fix the API contract: Consistent entity ID vs name convention across all three methods
  4. Add real heuristic or rename: Either integrate ChromaDB distance or call it what it is (pheromone-weighted Dijkstra)
  5. Add pheromone decay: evaporate_pheromones(rho=0.1) method
  6. Write tests: At least 10-15 covering the happy path, edge cases, and migration
  7. Integrate or defer: Wire to a CLI command / MCP tool, or keep in a feature branch until integration is ready
  8. Fix whitespace: Revert the indentation change on the existing schema line

Created by Octocode MCP https://octocode.ai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants