# autoloops/greplica architecture

> Architecture diagram of the GitHub repository autoloops/greplica, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/autoloops/greplica
- Repository: https://github.com/autoloops/greplica

## Overview

#### Purpose and Entry Points
Greplica is a Node.js (22–26) CLI that gives coding agents persistent, searchable repository memory. It stores repo structure, decisions, workflows, and session-derived claims locally, then retrieves a focused Markdown context packet for later tasks.

Primary entry point: `apps/cli/main.ts`. Repository context discovery is in `apps/cli/repo-context.ts`. The published CLI drives installation, graph operations, proposal validation/application, transcript bundling, and diagnostics.

#### Installation and Agent Integration
Installation prepares per-repo state and wires Greplica into supported coding-agent platforms. `libs/install/install.ts` coordinates platform-specific skills, hooks, and paths; adapters live under `libs/install/platforms/`, including `libs/install/platforms/codex.ts`, `libs/install/platforms/cursor.ts`, and `libs/install/platforms/openhands.ts`.

This is a boundary between Greplica’s local graph/store and external agent environments. Hook placement varies: some platforms use user-home configuration, while OpenHands is repo-local.

#### Session Hooks and Memory Maintenance
Hooks inject retrieval guidance when a task starts, track session state, and can launch background working-memory updates after sessions. The core hook worker is `libs/hooks/worker.ts`, with protocol/state in `libs/hooks/hook-input.ts` and `libs/hooks/session-state.ts`.

Skills define the agent-facing workflows for initial repository bootstrap, fast transcript bootstrap, and incremental updates: `skills/greplica-bootstrap/SKILL.md`, `skills/greplica-fast-session-bootstrap/SKILL.md`, and `skills/greplica-update-working-memory/SKILL.md`.

#### Knowledge Graph Domain and Proposal Boundary
The durable memory model consists of components, flows, claims, source commits/scopes, and typed edges. Its core schema/domain modules are `libs/knowledge-graph/schema.ts`, `libs/knowledge-graph/claim.ts`, `libs/knowledge-graph/edge.ts`, and `libs/knowledge-graph/service.ts`.

Agents or memory-building processes produce proposal JSON. `libs/knowledge-graph/validate-proposal.ts` validates it before `libs/knowledge-graph/proposal.ts` applies it, creating an explicit integrity boundary between generated content and persisted graph state. Similar-claim detection is handled by `libs/knowledge-graph/dedupe/find-similar-claims.ts`.

#### Local Persistence and Source Integrity
SQLite is the primary local persistence layer, implemented by `libs/storage/sqlite/db.ts`, `libs/storage/sqlite/schema.ts`, and `libs/storage/sqlite/repository.ts`; migrations are managed in `libs/storage/sqlite/migrate.ts`.

Claims can cite code anchors. Anchor fingerprints, resolution, and drift audits are separated in `libs/knowledge-graph/code-anchors/fingerprint.ts`, `libs/knowledge-graph/code-anchors/resolver.ts`, and `libs/knowledge-graph/code-anchors/audit.ts`. This permits stored knowledge to be checked against an evolving working tree.

#### Retrieval and Graph Consumption
`greplica graph context` converts a natural-language query into an actionable Markdown context packet. The retrieval pipeline in `libs/knowledge-graph/graph-context/` combines document construction, lexical BM25, vector similarity, graph ranking, packet ranking, and rendering: `context-builder.ts`, `bm25.ts`, `vector.ts`, `graph-rank.ts`, and `render.ts`.

Embeddings are a replaceable boundary: local models are supported through `local-embedder.ts` and model locking, while OpenAI is supported through `openai-embedder.ts`. The graph can also be rendered/exported via `libs/knowledge-graph/graph-view/build-graph-view.ts` and `libs/knowledge-graph/folder-export.ts`.

#### Memory-Build and Evaluation Workflows
Offline memory construction scripts collect repository snapshots and GitHub material, extract symbols, generate/apply proposals, audit anchors, and prepare task-specific context. The orchestrating stages are under `scripts/memory-build/`, notably `prepare-task.ts`, `collect-repo-snapshot.ts`, `generate-bootstrap-proposal.ts`, and `apply-proposals.ts`.

Agent runners abstract Codex, OpenCode, and OpenHands execution in `libs/agent-runner/`. Evaluation cases and ranking experiments under `evals/` exercise retrieval, proposal integrity, session backfill, and SWE-chat planning performance rather than serving runtime users.

Main entry points: [`apps/cli/main.ts`](https://github.com/autoloops/greplica/blob/main/apps/cli/main.ts) (Published CLI), [`libs/hooks/worker.ts`](https://github.com/autoloops/greplica/blob/main/libs/hooks/worker.ts) (Session Hook Worker), [`libs/knowledge-graph/graph-context/context-builder.ts`](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/context-builder.ts) (Context Builder), [`libs/install/platforms/codex.ts`](https://github.com/autoloops/greplica/blob/main/libs/install/platforms/codex.ts) (Platform Adapters)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI &amp; Agent Integration"]
  node_cli_main["Published CLI<br/>Node CLI<br/>[main.ts]"]
  node_repo_context["Repository Discovery<br/>context discovery<br/>[repo-context.ts]"]
  node_installer["Installer<br/>installation coordinator<br/>[install.ts]"]
  node_platform_adapters["Platform Adapters<br/>agent integration<br/>[codex.ts]"]
  node_skills["Agent Skills<br/>Markdown workflows<br/>[SKILL.md]"]
  node_hook_worker["Session Hook Worker<br/>[worker.ts]"]
  node_hook_protocol["Hook Session State<br/>hook protocol<br/>[hook-input.ts]"]
end

subgraph group_memory["Knowledge Memory"]
  node_graph_domain["Knowledge Graph Domain<br/>graph service<br/>[service.ts]"]
  node_proposal_validation{{"Proposal Validation<br/>integrity boundary"}}
  node_proposal_apply["Proposal Application<br/>graph mutation<br/>[proposal.ts]"]
  node_claim_dedupe["Similar-Claim Detection<br/>deduplication"]
  node_sqlite_repository[("SQLite Repository<br/>local persistence<br/>[repository.ts]")]
end

subgraph group_retrieval["Retrieval &amp; Integrity"]
  node_anchor_integrity["Code Anchor Audits<br/>source integrity<br/>[audit.ts]"]
  node_context_builder["Context Builder<br/>retrieval pipeline<br/>[context-builder.ts]"]
  node_hybrid_ranking["Hybrid Ranking<br/>[graph-rank.ts]"]
  node_embedding_providers["Embedding Providers<br/>replaceable embeddings<br/>[local-embedder.ts]"]
  node_context_render["Markdown Packet Renderer<br/>context output<br/>[render.ts]"]
  node_graph_export["Graph View &amp; Export<br/>graph output"]
end

subgraph group_automation["Build Workflows"]
  node_memory_build["Memory-Build Pipeline<br/>offline orchestration<br/>[prepare-task.ts]"]
  node_agent_runners["Agent Runners<br/>agent execution abstraction<br/>[codex.ts]"]
end

node_cli_main -->|"discovers repository"| node_repo_context
node_cli_main -->|"installs"| node_installer
node_cli_main -->|"graph context"| node_context_builder
node_cli_main -->|"validates proposals"| node_proposal_validation
node_installer -->|"configures"| node_platform_adapters
node_platform_adapters -->|"installs workflows"| node_skills
node_platform_adapters -->|"registers hooks"| node_hook_worker
node_hook_worker -->|"tracks sessions"| node_hook_protocol
node_hook_worker -->|"requests guidance"| node_context_builder
node_hook_worker -.->|"updates after session"| node_memory_build
node_memory_build -->|"runs agents"| node_agent_runners
node_memory_build -->|"generates proposals"| node_proposal_validation
node_proposal_validation -->|"accepted proposals"| node_proposal_apply
node_proposal_apply -->|"checks duplicates"| node_claim_dedupe
node_proposal_apply -->|"mutates"| node_graph_domain
node_graph_domain -->|"persists"| node_sqlite_repository
node_anchor_integrity -->|"audits stored anchors"| node_sqlite_repository
node_context_builder -->|"loads graph"| node_sqlite_repository
node_context_builder -->|"ranks evidence"| node_hybrid_ranking
node_embedding_providers -->|"vector similarity"| node_hybrid_ranking
node_hybrid_ranking -->|"selected context"| node_context_render
node_graph_domain -->|"renders graph"| node_graph_export

click node_cli_main "https://github.com/autoloops/greplica/blob/main/apps/cli/main.ts"
click node_repo_context "https://github.com/autoloops/greplica/blob/main/apps/cli/repo-context.ts"
click node_installer "https://github.com/autoloops/greplica/blob/main/libs/install/install.ts"
click node_platform_adapters "https://github.com/autoloops/greplica/blob/main/libs/install/platforms/codex.ts"
click node_skills "https://github.com/autoloops/greplica/blob/main/skills/greplica-bootstrap/SKILL.md"
click node_hook_worker "https://github.com/autoloops/greplica/blob/main/libs/hooks/worker.ts"
click node_hook_protocol "https://github.com/autoloops/greplica/blob/main/libs/hooks/hook-input.ts"
click node_graph_domain "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/service.ts"
click node_proposal_validation "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/validate-proposal.ts"
click node_proposal_apply "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/proposal.ts"
click node_claim_dedupe "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/dedupe/find-similar-claims.ts"
click node_sqlite_repository "https://github.com/autoloops/greplica/blob/main/libs/storage/sqlite/repository.ts"
click node_anchor_integrity "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/code-anchors/audit.ts"
click node_context_builder "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/context-builder.ts"
click node_hybrid_ranking "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/graph-rank.ts"
click node_embedding_providers "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/local-embedder.ts"
click node_context_render "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/render.ts"
click node_graph_export "https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-view/build-graph-view.ts"
click node_memory_build "https://github.com/autoloops/greplica/blob/main/scripts/memory-build/prepare-task.ts"
click node_agent_runners "https://github.com/autoloops/greplica/blob/main/libs/agent-runner/codex.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli_main,node_repo_context,node_installer,node_platform_adapters,node_skills,node_hook_worker,node_hook_protocol toneBlue
class node_graph_domain,node_proposal_validation,node_proposal_apply,node_claim_dedupe,node_sqlite_repository toneAmber
class node_anchor_integrity,node_context_builder,node_hybrid_ranking,node_embedding_providers,node_context_render,node_graph_export toneMint
class node_memory_build,node_agent_runners toneRose
```

## Components

### CLI & Agent Integration

- [Published CLI](https://github.com/autoloops/greplica/blob/main/apps/cli/main.ts): Node CLI · Drives installation, graph operations, proposals, bundles, and diagnostics. · `apps/cli/main.ts`
- [Repository Discovery](https://github.com/autoloops/greplica/blob/main/apps/cli/repo-context.ts): context discovery · `apps/cli/repo-context.ts`
- [Installer](https://github.com/autoloops/greplica/blob/main/libs/install/install.ts): installation coordinator · `libs/install/install.ts`
- [Platform Adapters](https://github.com/autoloops/greplica/blob/main/libs/install/platforms/codex.ts): agent integration · Installs skills, hooks, and platform-specific paths. · `libs/install/platforms/codex.ts`
- [Agent Skills](https://github.com/autoloops/greplica/blob/main/skills/greplica-bootstrap/SKILL.md): Markdown workflows · Bootstrap, fast-session bootstrap, and working-memory update guidance. · `skills/greplica-bootstrap/SKILL.md`
- [Session Hook Worker](https://github.com/autoloops/greplica/blob/main/libs/hooks/worker.ts): hook worker · Injects retrieval guidance and triggers background memory maintenance. · `libs/hooks/worker.ts`
- [Hook Session State](https://github.com/autoloops/greplica/blob/main/libs/hooks/hook-input.ts): hook protocol · `libs/hooks/hook-input.ts`

### Knowledge Memory

- [Knowledge Graph Domain](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/service.ts): graph service · Models components, flows, claims, sources, and typed edges. · `libs/knowledge-graph/service.ts`
- [Proposal Validation](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/validate-proposal.ts): integrity boundary · Validates generated proposal JSON before mutation. · `libs/knowledge-graph/validate-proposal.ts`
- [Proposal Application](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/proposal.ts): graph mutation · `libs/knowledge-graph/proposal.ts`
- [Similar-Claim Detection](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/dedupe/find-similar-claims.ts): deduplication · `libs/knowledge-graph/dedupe/find-similar-claims.ts`
- [SQLite Repository](https://github.com/autoloops/greplica/blob/main/libs/storage/sqlite/repository.ts): local persistence · Persists durable graph state and migrations. · `libs/storage/sqlite/repository.ts`

### Retrieval & Integrity

- [Code Anchor Audits](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/code-anchors/audit.ts): source integrity · Fingerprints, resolves, and audits claim anchors against the working tree. · `libs/knowledge-graph/code-anchors/audit.ts`
- [Context Builder](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/context-builder.ts): retrieval pipeline · Builds query documents and coordinates context retrieval. · `libs/knowledge-graph/graph-context/context-builder.ts`
- [Hybrid Ranking](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/graph-rank.ts): BM25, vector & graph rank · Combines lexical, embedding, graph, and packet ranking. · `libs/knowledge-graph/graph-context/graph-rank.ts`
- [Embedding Providers](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/local-embedder.ts): replaceable embeddings · Supports local models and OpenAI embeddings. · `libs/knowledge-graph/graph-context/local-embedder.ts`
- [Markdown Packet Renderer](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-context/render.ts): context output · `libs/knowledge-graph/graph-context/render.ts`
- [Graph View & Export](https://github.com/autoloops/greplica/blob/main/libs/knowledge-graph/graph-view/build-graph-view.ts): graph output · `libs/knowledge-graph/graph-view/build-graph-view.ts`

### Build Workflows

- [Memory-Build Pipeline](https://github.com/autoloops/greplica/blob/main/scripts/memory-build/prepare-task.ts): offline orchestration · Collects snapshots, generates proposals, applies them, and prepares task context. · `scripts/memory-build/prepare-task.ts`
- [Agent Runners](https://github.com/autoloops/greplica/blob/main/libs/agent-runner/codex.ts): agent execution abstraction · Abstracts Codex, OpenCode, and OpenHands execution. · `libs/agent-runner/codex.ts`

## Connections

- Published CLI → Repository Discovery: discovers repository
- Published CLI → Installer: installs
- Published CLI → Context Builder: graph context
- Published CLI → Proposal Validation: validates proposals
- Installer → Platform Adapters: configures
- Platform Adapters → Agent Skills: installs workflows
- Platform Adapters → Session Hook Worker: registers hooks
- Session Hook Worker → Hook Session State: tracks sessions
- Session Hook Worker → Context Builder: requests guidance
- Session Hook Worker → Memory-Build Pipeline: updates after session
- Memory-Build Pipeline → Agent Runners: runs agents
- Memory-Build Pipeline → Proposal Validation: generates proposals
- Proposal Validation → Proposal Application: accepted proposals
- Proposal Application → Similar-Claim Detection: checks duplicates
- Proposal Application → Knowledge Graph Domain: mutates
- Knowledge Graph Domain → SQLite Repository: persists
- Code Anchor Audits → SQLite Repository: audits stored anchors
- Context Builder → SQLite Repository: loads graph
- Context Builder → Hybrid Ranking: ranks evidence
- Embedding Providers → Hybrid Ranking: vector similarity
- Hybrid Ranking → Markdown Packet Renderer: selected context
- Knowledge Graph Domain → Graph View & Export: renders graph
