This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Run all tests
python -m pytest tests/ -q
# Run a single test file
python -m pytest tests/test_cso_store.py -v
# Run a single test by name
python -m pytest tests/test_cso_store.py::test_write_and_query -v
# Start MCP server (stdio transport, used by Cursor/Claude/Windsurf)
python -m hcr.product.integrations.mcp_server_stdio
# CLI
hcr init # initialize .hcr/ in current project
hcr status # show cognitive state summary
hcr resume # show resume contextHCR is a persistent developer memory layer exposed as an MCP server. The key insight: AI tools are stateless, so HCR provides the stateful substrate they lack.
hcr/engine/cso/ is the core data layer. A CSO (cso_model.py) is a typed, causally-linked record — types include DECISION, OBSERVATION, CONSTRAINT, RISK, TASK, OUTCOME, CLAIM, INTENT, ROLLBACK, TRIGGER. Each CSO has explicit causal_in/causal_out edge lists forming a directed causal graph. CSOStore (cso_store.py) persists them in SQLite + WAL at .hcr/cso.db. Indexes on type, created_at, and scope.
The Cognitive State Fabric (CSF) — the intelligence layer over raw CSO storage:
centrality.py—CausalCentralityScorer: BFS transitive reachability on causal edges. CSOs that caused the most downstream effects score highest (0–1).projection.py—CognitiveProjection: replaces naivefacts[-10:]with centrality-ranked, decay-filtered live state. Called on everyhcr_get_stateandhcr_preflight.prefetch.py—ProjectionPrefetcher: background thread triggered on file edit events. CachesCognitiveProjectionso the next tool call hits cache (zero compute latency).embedding_store.py—EmbeddingStore: sqlite-vec ANN store at.hcr/embeddings.db. Embeds qualifying CSO tiers (commit/task/decision/constraint/risk/edited) viasentence-transformers(all-MiniLM-L6-v2, 384-dim), falling back to Ollamanomic-embed-text. Only CSOs in those tiers are embedded. Wired intoCognitiveProjectionas 3rd RRF retrieval channel.implicit_graph.py—generate_soft_links: called in_handle_file_editafter embedding; auto-detects semantic soft causal edges (similarity > 0.55 threshold via1/(1+distance)) and back-writes them to the CSO.fusion.py—reciprocal_rank_fusion(RRF, accepts optional learnedweightstuple) andmmr_select(Maximum Marginal Relevance): fuse semantic + causal ranked lists; select diverse results. RRF called byCognitiveProjection(3-way: decay + BM25 + embedding) andcso_impact. MMR called after cross-encoder reranking in projection.prospective.py—get_triggered_csos: returnsTRIGGERCSOs matching the active file pattern; injected at rank-0 byCognitiveProjection.feedback.py—FeedbackStore: SQLite store recording preflight retrievals and session outcomes. AfterTRAINING_THRESHOLD=50labelled samples, trains learned RRF weights via least-squares.HCREngine._feedback_storeowns the instance.sync.py—EmbeddingSync: fire-and-forget Supabase pgvector push via daemon threads. Only(org_id, project_id, cso_id, embedding)pushed — no raw content. Wired intoEmbeddingStore._syncwhenSUPABASE_URL/SUPABASE_KEYconfigured.cpap.py—CausalPrefixAlignmentProtocol: three-layer freeze-gated context serialization achieving ~98% prompt cache hit rate. Layers 1+2 are byte-identical between git commits; Layer 3 is the per-call delta.reranker.py—CrossEncoderReranker: second-pass joint (query, candidate) relevance scoring viacross-encoder/ms-marco-MiniLM-L-6-v2(22 MB). Called after RRF fusion inCognitiveProjection. Graceful identity-order fallback on import error.episode_store.py— Removed. Episode detection (BOCPD) deprecated; functionality moved toevent_store.py.commit_extractor.py—ExtractedSignal: regex-based extraction of decisions, constraints, and risks from git commit messages. Scans trailers (Decided-to:,Constraint:) and body patterns; returns typed CSOs with confidence scores.merger.py—StateMerger: cross-project CSO state merger with dedup (exact-ID, path+text for observations) and conflict resolution (confidence then timestamp for decisions/tasks). Emits conflict OBSERVATION CSOs.
Static and runtime causal analysis over the codebase dependency graph:
ast_extractor.py—DependencyExtractor: AST-based extraction of imports and function calls from Python source files to build the causal graph.dependency_graph.py—DependencyGraph: in-memory directed graph of file/module dependencies (forward + reverse edges) with support for latent links discovered by neural inference.impact_analyzer.py—ImpactAnalyzer: BFS traversal of reverse dependencies to predict the ripple effect of a file change up to configurable depth.event_store.py—EventStore: append-only JSONL event log (causal_events.jsonl) for temporal reasoning and time-travel over cognitive/file state changes.cso_impact.py—query_cso_impact: CSO-graph-based impact analysis (v2.0). Semantic attention BFS overcausal_inedges with optional embedding similarity gating and RRF fusion with ANN search.metrics.py—MetricsAnalyzer: calculates fragility (AST complexity, incoming dependencies) and centrality scores for files in the causal graph.
hco_engine.py was removed. Legacy operator stubs (BaseOperator, CompositeOperator, PolicySelector) are deprecated. Core engine logic now lives entirely in engine_api.py.
llm_provider.py — unified synchronous interface (LLMResponse) for all LLM providers (Groq, Google, Ollama). Handles markdown code-fence stripping and JSON parsing of responses.
HCREngine is the central object. Key responsibilities:
- Owns
_cso_store,_prefetcher,_embedding_store,_feedback_store _handle_file_edit()is the hot path: writes OBSERVATION CSO → symbolic verifier → prefetcher → embeds CSO → generates soft-links (back-written to CSO causal_in)_extract_task()and_calculate_progress()filteredited:facts to paths that exist underproject_path— prevents cross-project state bleed from stalecausal_events.jsonl- Parallel legacy state in
CognitiveState(symbolic facts, causal graph) is kept for backwards compatibility but CSOs are the v2.0 source of truth
SymbolicVerifier (verifier.py) evaluates DEFAULT_RULES against a newly written CSO and emits RISK CSOs when rules fire. Rules live in rules.py. Rule evaluation failures are logged at DEBUG level (not silently swallowed).
Tier-based fact retention. Each fact prefix (commit:, task:, edited:, error:, cmd:, mcp_tool:, pattern:, observation:) has a half-life (7 days → 15 min). CognitiveProjection uses this to compute effective half-life adjusted by centrality: effective_hl = base / max(1 - centrality*0.8, 0.2) — high-centrality CSOs decay slower.
HCRMCPResponder in mcp_server.py dispatches all MCP tool calls to modular BaseMCPTool subclasses in tools/. Each tool class is ~one file. 32 tools total. Key tools:
hcr_get_state/hcr_preflight— main context-handoff tools for agentshcr_preflight/hcr_postflight— agent lifecycle: preflight records retrieval for learned fusion; postflight records outcome signal; preflight hintshcr_set_triggerwhen no triggers activehcr_record_file_edit,hcr_remember,hcr_fail,hcr_resolve— write-path toolshcr_analyze_impact— causal BFS + RRF semantic fusion;file_path=nullreturns causal graph summaryhcr_set_trigger— creates TRIGGER CSOs for prospective memory (file-pattern → reminder injection)hcr_read_decisions— JSONL decision log;source=csoorsource=allalso queries CSO storehcr_get_recommendations— current task + next action + AI recommendations (consolidated from 3 tools)
All imports into mcp_server.py must be at module level (not inside functions/conditionals).
.hcr/
cso.db # SQLite CSO store (WAL mode, indexes: type/created_at/scope)
embeddings.db # sqlite-vec embedding store (WAL, synchronous=NORMAL)
feedback.db # learned fusion weights (FeedbackStore)
state.json # legacy CognitiveState (required for init check)
decisions/ # legacy JSONL decision log (migrated to CSOs on init)
The docs/ directory is the primary project memory. Always consult it when you need context about architecture, goals, history, or current work. Key files:
docs/architecture.md— system design, component relationshipsdocs/NORTHSTAR.md— project vision, long-term directiondocs/product_roadmap.md— roadmap, priorities, milestonesdocs/dev_log.md— development history, past decisionsdocs/tasks.md— current work itemsdocs/strategic_vision.md— business strategy
Rule: When uncertain about project context, read the relevant docs/ file before proceeding. After making significant changes, update the corresponding doc to keep it current.
tests/conftest.py has collect_ignore for non-pytest scripts. Tests are fully synchronous where possible; async tests use pytest-asyncio with asyncio_mode = "auto". Mock _get_embedding on EmbeddingStore to avoid needing Ollama running. The full suite runs in ~310 seconds (452 tests: 446 passed, 6 skipped — skipped tests require a live LLM endpoint).