Spaces:
Configuration error
Configuration error
Raplh 2 in process
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .scratch/arch-improvements/issues/01-appcontext-expand.mdDeleted-16
- .scratch/arch-improvements/issues/02-privacy-module.mdDeleted-17
- .scratch/arch-improvements/issues/03-auth-middleware.mdDeleted-16
- .scratch/arch-improvements/issues/04-mcp-tool-registry.mdDeleted-17
- .scratch/arch-improvements/issues/05-split-observations-memories.mdDeleted-23
- .scratch/arch-improvements/issues/06-split-indexing-retrieval.mdDeleted-24
- .scratch/arch-improvements/issues/07-split-consolidation-slots-lessons.mdDeleted-25
- .scratch/arch-improvements/issues/08-delete-functions-shim.mdDeleted-23
- .scratch/arch-improvements/issues/09-typed-observations-table-expand.mdDeleted-19
- .scratch/arch-improvements/issues/10-typed-observations-reads-contract.mdDeleted-18
- .scratch/arch-improvements/issues/11-basevectorindex-protocol.mdDeleted-17
- src/agentcache.egg-info/PKG-INFO+2-484
- src/agentcache.egg-info/SOURCES.txt+6-1
- src/agentcache/__init__.py+6-9
- src/agentcache/app.py+36-22
- src/agentcache/cache/__init__.py+3-9
- src/agentcache/cache/context.py+1-1
- src/agentcache/cache/graph.py+1-1
- src/agentcache/cache/health.py+1-1
- src/agentcache/cache/observe.py+1-1
- src/agentcache/cache/remember.py+1-1
- src/agentcache/cache/timeline.py+1-1
- src/agentcache/cli.py+4-3
- src/agentcache/core/__init__.py+7-2
- src/agentcache/core/kv_scopes.py-2
- src/agentcache/core/observation_store.py+18-15
- src/agentcache/core/search_service.py+17-6
- src/agentcache/functions.pyDeleted
- src/agentcache/legacy.py+12-13
- src/agentcache/replay_import.py+4-24
- src/agentcache/routes/__init__.py+7-12
- src/agentcache/routes/graph.py+1-1
- src/agentcache/routes/health.py+1-1
- src/agentcache/routes/mcp.py+10-5
- src/agentcache/routes/memories.py+1-4
- src/agentcache/routes/migration.py+1-1
- src/agentcache/routes/observations.py+251-267
- src/agentcache/routes/search.py+34-8
- src/agentcache/search.py+1
- src/agentcache/storage/scopes.py+3-72
- src/agentcache/workers.py+39-14
- tests/__init__.py-2
- tests/conftest.py+1
- tests/test_api.pyDeleted-294
- tests/test_auth.py+20-7
- tests/test_auto_forget.pyDeleted-139
- tests/test_cli_context.pyDeleted-70
- tests/test_context.pyDeleted-204
- tests/test_debounce.pyDeleted-166
- tests/test_folder_graph_build.pyDeleted-432
12345678910111213141516# 01 — Introduce `AppContext` dataclass (Expand)
**Blocked by:** None — can start immediately**Status:** ready-for-agent
## What to build
Introduce a single `AppContext` dataclass that packages all runtime state currently scattered as module-level globals in `functions.py` (`_bm25_index`, `_vector_index`, `_embedding_provider`, `_hybrid_search`, `_stream_broadcaster`, and the `kv` reference). Construct one `AppContext` instance inside `create_app()` and thread it through to every function that needs it. The existing globals stay in place as a compatibility shim — no callers are changed in this ticket. Zero behaviour change; this is the expand step that makes the god-module split (tickets 05–08) possible.
## Acceptance criteria
- [ ] `AppContext` dataclass exists in a new `context.py` module (or equivalent) with typed fields for `kv`, `bm25`, `vector`, `embedder`, and `broadcast`- [ ] `create_app()` constructs one `AppContext` and stores it on the Flask app (e.g. `app.ctx`)- [ ] The five existing `set_*` functions in `functions.py` remain working — globals are still set alongside the new `AppContext`- [ ] All existing tests pass with no changes- [ ] No route handler or worker is changed in this ticket1234567891011121314151617# 02 — Extract `privacy.py` deep module
**Blocked by:** None — can start immediately**Status:** ready-for-agent
## What to build
Extract `strip_private_data()` from `functions.py` into a dedicated `privacy.py` module. The new module exposes a single `scrub(text: str, patterns=DEFAULT_PATTERNS) -> str` interface. The regex pattern list is promoted to a public, documented constant (`DEFAULT_PATTERNS`) and is extensible via an env var (`AGENTCACHE_REDACT_PATTERNS`). `functions.py` calls `privacy.scrub()` internally — all existing callers continue working without change. First unit tests for Privacy Scrub are added in this ticket, targeting `scrub()` directly.
## Acceptance criteria
- [ ] `privacy.py` exists with a `scrub(text, patterns=DEFAULT_PATTERNS)` function- [ ] `DEFAULT_PATTERNS` is a documented list of regex strings — visible and auditable- [ ] An env var `AGENTCACHE_REDACT_PATTERNS` (comma-separated regex strings) appends additional patterns at startup- [ ] `functions.py` no longer contains the scrubbing regex list — it imports and calls `privacy.scrub()`- [ ] Unit tests cover: API key pattern, bearer token pattern, custom pattern via argument, no false positives on safe text- [ ] All existing tests pass12345678910111213141516# 03 — Centralise auth middleware
**Blocked by:** None — can start immediately**Status:** ready-for-agent
## What to build
Replace the copy-pasted `_check_auth()` function that currently lives independently in every route blueprint (`mcp.py`, `observations.py`, `memories.py`, `health.py`, and others) with a single `require_auth` decorator defined in a new `auth.py` module. Every route that previously called `_check_auth()` at the top of its handler switches to the `@require_auth` decorator. The auth logic (timing-safe `hmac.compare_digest` Bearer token check) is identical — this is purely a deduplication. RBAC can be added in one place in a future ticket.
## Acceptance criteria
- [ ] `auth.py` exists with a `require_auth(f)` decorator that performs the timing-safe Bearer token check- [ ] Every route blueprint imports and uses `@require_auth` — no blueprint defines its own `_check_auth`- [ ] A request with no secret configured passes through (existing behaviour preserved)- [ ] A request with a wrong token still gets a `401` response- [ ] All existing tests pass1234567891011121314151617# 04 — MCP tool registry (replace elif chain)
**Blocked by:** 03 — centralise auth middleware**Status:** ready-for-agent
## What to build
Replace the 300-line `if/elif` dispatch chain in `routes/mcp.py` with a `@register("tool_name")` decorator registry. Each of the 30+ MCP tool handlers becomes a standalone function decorated with `@register`. The dispatcher becomes a two-liner: look up the tool name in the registry dict and call the handler. Auth is handled by the shared `@require_auth` decorator from ticket 03. The tool schema list (`get_mcp_tools_schemas`) stays as-is. No change to the MCP wire format or agent behaviour.
## Acceptance criteria
- [ ] A `_tools: dict[str, Callable]` registry exists and a `@register(name)` decorator populates it- [ ] Every MCP tool handler is a standalone function — not an inline block inside a giant if/elif- [ ] The POST `/mcp/tools` handler body is ≤ 20 lines (lookup + call + error handling)- [ ] Each tool handler function is independently importable and callable in a test without starting Flask- [ ] Adding a new tool requires only adding a new decorated function — no editing of existing dispatch code- [ ] All existing MCP tool calls produce the same responses as before1234567891011121314151617181920212223# 05 — Split god module batch 1: `observations.py` + `memories.py`
**Blocked by:** 01 — AppContext dataclass (Expand)**Status:** ready-for-agent
## What to build
Move the first two major domain areas out of `functions.py` into focused modules, using the domain vocabulary from `UBIQUITOUS_LANGUAGE.md`.
**`observations.py`** receives: `folder_observe()`, `observe()` (legacy), `dedup_folder_observations()`, `build_synthetic_compression()`, `infer_type()`, `extract_files()`, `extract_image()`, image store helpers (`save_image_to_disk`, `delete_image`, `touch_image`, `is_managed_image_path`), and `normalize_folder_path()` / `validate_agent_id()`.
**`memories.py`** receives: `remember()`, `forget()`, memory versioning logic, and `jaccard_similarity()`.
`functions.py` re-exports everything from these two modules so all existing callers continue to work unchanged. CI must stay green. This is the first batch of the expand phase of the god-module split.
## Acceptance criteria
- [ ] `observations.py` and `memories.py` exist as standalone modules- [ ] All moved functions accept `AppContext` (from ticket 01) instead of reaching into module-level globals, while also accepting the legacy `kv: StateKV` signature for backward compat- [ ] `functions.py` re-exports every moved symbol — no call site outside `functions.py` needs to change- [ ] Existing route blueprints and workers continue to import from `functions` without modification- [ ] All existing tests pass- [ ] No logic is changed — this is a pure relocation123456789101112131415161718192021222324# 06 — Split god module batch 2: `indexing.py` + `retrieval.py`
**Blocked by:** 05 — split batch 1 (observations + memories)**Status:** ready-for-agent
## What to build
Move the second major domain area out of `functions.py`.
**`indexing.py`** receives: `IndexPersistence`, `rebuild_index()`, `backfill_obs_lookup_if_needed()`, `vector_index_add_guarded()`, `clip_embed_input()`, and all index-related helpers. This module owns the Index Rebuild lifecycle (see `UBIQUITOUS_LANGUAGE.md`).
**`retrieval.py`** receives: `folder_search()`, `folder_timeline()`, `compile_context()`, and `export_data()`. This module owns the Recall, Smart Search, Folder Search, Timeline, and Context Compilation operations.
`functions.py` continues to re-export everything. CI stays green.
## Acceptance criteria
- [ ] `indexing.py` and `retrieval.py` exist as standalone modules- [ ] All moved functions use `AppContext` from ticket 01 rather than reaching into globals directly- [ ] `functions.py` re-exports every moved symbol unchanged- [ ] `IndexPersistence` is importable from `indexing` in `workers.py` without changing any other worker code- [ ] `folder_search()` and `compile_context()` are callable from a test by constructing an `AppContext` with an in-memory KV and a fresh `SearchIndex` — no Flask app required- [ ] All existing tests pass- [ ] No logic is changed12345678910111213141516171819202122232425# 07 — Split god module batch 3: `consolidation.py` + `slots.py` + `lessons.py`
**Blocked by:** 06 — split batch 2 (indexing + retrieval)**Status:** ready-for-agent
## What to build
Move the final major domain areas out of `functions.py`.
**`consolidation.py`** receives: `consolidate()`, `auto_forget()`, `folder_graph_build()`, and all LLM-call helpers used exclusively by the Consolidation pipeline. This module owns the Consolidation lifecycle (Working Memory → Episodic → Semantic → Procedural).
**`slots.py`** receives: all Slot CRUD functions (`get_slots`, `set_slot`, `delete_slot`, `reflect_slot`, `append_slot`, etc.).
**`lessons.py`** receives: all Lesson CRUD functions (`get_lessons`, `save_lesson`, `search_lessons`, `strengthen_lesson`, lesson decay logic).
`functions.py` re-exports everything. After this ticket, `functions.py` is a pure shim — no domain logic remains in it.
## Acceptance criteria
- [ ] `consolidation.py`, `slots.py`, and `lessons.py` exist as standalone modules- [ ] `functions.py` contains no domain logic — only re-export statements- [ ] All moved functions use `AppContext` from ticket 01- [ ] `auto_forget()` is callable from a test without importing `functions` directly- [ ] All existing tests pass- [ ] No logic is changed1234567891011121314151617181920212223# 08 — Contract: delete `functions.py` shim, migrate all callers to direct imports
**Blocked by:** 05, 06, 07 — all three split batches must be complete**Status:** ready-for-agent
## What to build
Delete `functions.py` entirely. Update every import across `routes/`, `workers.py`, `app.py`, and the test suite to point directly to the focused module that owns the symbol. This is the contract step of the expand–contract sequence. After this ticket the domain vocabulary from `UBIQUITOUS_LANGUAGE.md` is directly reflected in the module names — there is no god module.
Import mapping (non-exhaustive):- `from .functions import folder_observe` → `from .observations import folder_observe`- `from .functions import remember, forget` → `from .memories import remember, forget`- `from .functions import folder_search, compile_context` → `from .retrieval import folder_search, compile_context`- `from .functions import IndexPersistence, rebuild_index` → `from .indexing import IndexPersistence, rebuild_index`- `from .functions import consolidate, auto_forget` → `from .consolidation import consolidate, auto_forget`
## Acceptance criteria
- [ ] `functions.py` does not exist in the repository- [ ] `rg "from .functions import\|from agentcache.functions import\|import functions"` returns zero results in `src/`- [ ] All existing tests pass with imports updated- [ ] No test imports from `functions` — each test imports from the specific module it exercises- [ ] CI is green12345678910111213141516171819# 09 — Add typed `observations` table to SQLite (Expand)
**Blocked by:** 05 — observations module must exist to own this migration**Status:** ready-for-agent
## What to build
Add a typed `observations` table to the SQLite schema with columns for `folder`, `agent`, `timestamp`, `type`, `importance`, and `text`. When a new Observation is ingested via `folder_observe()`, write it to both the new typed table and the existing `kv_store` scope (dual-write). Reads still use `kv_store` — zero behaviour change for any query path. This is the expand step that makes SQL-level filtering possible without breaking anything.
The new table enables: `WHERE folder = ? AND agent = ? AND timestamp > ?` and `ORDER BY importance DESC LIMIT N` entirely at the SQLite layer — no Python-side filtering needed.
## Acceptance criteria
- [ ] `observations(id TEXT PK, folder TEXT, agent TEXT, timestamp TEXT, type TEXT, importance INTEGER, text TEXT)` table exists in the DB schema with indexes on `(folder, agent, timestamp)` and `(importance)`- [ ] `folder_observe()` dual-writes: one row to `observations`, one entry to `kv_store` (existing path)- [ ] The DB migration runs automatically on startup if the table does not exist (no manual step)- [ ] All reads continue to use `kv_store` — no query is changed in this ticket- [ ] All existing tests pass- [ ] A DB integrity test verifies that for each `kv_store` Observation entry written, a matching row exists in `observations`123456789101112131415161718# 10 — Migrate Observation reads to typed table (Contract)
**Blocked by:** 09 — typed observations table (Expand) must be complete**Status:** ready-for-agent
## What to build
Switch `folder_search`, `folder_timeline`, `compile_context`, and `auto_forget` to read Observations from the typed `observations` table using SQL-level filtering, replacing the current pattern of `kv.list(scope)` + Python-side filtering. Stop dual-writing new Observations to `kv_store` — the typed table is now the single source of truth. A one-time migration backfills any existing `kv_store` Observation entries into the typed table for users upgrading from a previous version.
## Acceptance criteria
- [ ] `folder_timeline()` issues a single `SELECT … WHERE folder=? AND agent=? ORDER BY timestamp DESC LIMIT ?` — no Python-side filtering loop- [ ] `folder_search()` hydrates candidates from the typed table rather than `kv.list(scope)` for the Observation load step- [ ] `compile_context()` fetches recent Observations using `ORDER BY importance DESC, timestamp DESC LIMIT ?` at the SQL layer- [ ] New `folder_observe()` calls write only to the typed table (no `kv_store` Observation scope write)- [ ] A backfill function runs once on startup to migrate existing `kv_store` Observations into the typed table- [ ] A benchmark test (or manual note in the PR) shows query latency improvement at ≥ 10k Observations- [ ] All existing tests pass1234567891011121314151617# 11 — `BaseVectorIndex` protocol + `InMemoryVectorIndex` adapter
**Blocked by:** 06 — indexing module must exist before its interface is formalised**Status:** ready-for-agent
## What to build
Define a `BaseVectorIndex` Protocol (or ABC) in `indexing.py` with three methods: `add(obs_id, session_id, embedding)`, `remove(obs_id)`, and `search(query_embedding, limit) -> list`. Rename the current flat-list implementation to `InMemoryVectorIndex` and have it implement `BaseVectorIndex`. Update `AppContext.vector` to be typed as `BaseVectorIndex`. Behaviour is completely unchanged — this ticket only cuts the adapter seam so future backends (HNSW, Qdrant, Chroma) can be swapped in without touching `retrieval.py` or `consolidation.py`.
## Acceptance criteria
- [ ] `BaseVectorIndex` Protocol exists in `indexing.py` with `add`, `remove`, and `search` as the only required methods- [ ] `InMemoryVectorIndex` implements `BaseVectorIndex` and passes a `isinstance(idx, BaseVectorIndex)` check- [ ] `AppContext.vector` is typed as `BaseVectorIndex | None`- [ ] A test constructs a minimal stub that implements `BaseVectorIndex` and passes it to `folder_search()` via `AppContext` — confirming the seam is real and injectable- [ ] `VectorIndex` (old name) remains as an alias for `InMemoryVectorIndex` for one release to avoid breaking any external imports- [ ] All existing tests pass with no behaviour change123430 unmodified lines3536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520Metadata-Version: 2.1Name: agentcacheVersion: 0.9.8Summary: A Python REST + WebSocket + MCP cache server for AI agents, backed by SQLite30 unmodified linesRequires-Dist: twine>=5.0.0; extra == "dev"Provides-Extra: local-embeddingsRequires-Dist: sentence-transformers>=2.7.0; extra == "local-embeddings"---title: AgentCache Pythonemoji: 🧠colorFrom: bluecolorTo: indigosdk: dockerpinned: false---<h1 align="center">agentcache-python</h1><p align="center"><strong>Persistent memory for AI coding agents — pure Python, zero external databases.</strong><br/>Works with Claude Code, Cursor, Cline, Windsurf, Gemini CLI, and any MCP client.</p><p align="center"><img src="https://img.shields.io/badge/Python-3.10%2B-3776AB?style=for-the-badge&logo=python&logoColor=white" alt="Python 3.10+" /><img src="https://img.shields.io/badge/SQLite-WAL-003B57?style=for-the-badge&logo=sqlite&logoColor=white" alt="SQLite WAL" /><img src="https://img.shields.io/badge/Flask-3.0-000000?style=for-the-badge&logo=flask&logoColor=white" alt="Flask 3.0" /><img src="https://img.shields.io/badge/MCP-Compatible-6B21A8?style=for-the-badge" alt="MCP Compatible" /><img src="https://img.shields.io/badge/HuggingFace-Space-FF9D00?style=for-the-badge&logo=huggingface&logoColor=white" alt="HuggingFace Space" /></p><p align="center"><a href="#quick-start">Quick Start</a> •<a href="#features">Features</a> •<a href="#mcp-integration">MCP</a> •<a href="#api-reference">API</a> •<a href="#configuration">Config</a> •<a href="#deploy-to-huggingface">Deploy</a> •<a href="#viewer">Viewer</a> •<a href="#architecture">Architecture</a></p>---## What Is This?**agentcache-python** is a Python reimplementation of the [agentcache](https://github.com/rohitg00/agentcache) persistent memory server. It exposes a REST API, WebSocket stream, and MCP tools endpoint that AI coding agents use to store and retrieve session observations, long-term memories, lessons, and pinned memory slots.Key differences from the Node.js original:- **No Node.js or iii-engine** — runs with plain `python src/app.py`- **SQLite instead of Dolt** — single file, WAL mode, instant startup- **HuggingFace Space ready** — deploys in one click, data synced to an HF dataset repo- **Same REST + MCP wire format** — drop-in for any agent already wired to agentcacheYour agent captures every tool call, stores them as observations, compresses them into searchable memory, and injects the right context at the start of every new session — automatically.---## Quick Start### Run locally```bash# Clonegit clone https://github.com/Yashwant00CR7/agentcache.gitcd agentcache# Install dependencies (no build step)pip install -r requirements.txt# Start the serverpython src/app.py```Server starts on **http://localhost:3111**. Open the viewer at http://localhost:3111/viewer.### Verify it works```bash# Health checkcurl http://localhost:3111/agentcache/livez# {"status": "ok"}# Save a memorycurl -X POST http://localhost:3111/agentcache/remember \-H "Content-Type: application/json" \-d '{"content": "JWT auth uses jose middleware in src/middleware/auth.ts", "concepts": ["auth", "jwt"]}'# Recall itcurl -X POST http://localhost:3111/agentcache/search \-H "Content-Type: application/json" \-d '{"query": "authentication middleware", "limit": 5}'```---## Features| Feature | Status | Notes ||---------|--------|-------|| REST API — sessions, memories, observations | ✅ | Full surface || WebSocket live stream | ✅ | `/stream/mem-live/viewer` || MCP tools endpoint | ✅ | 31 tools || Built-in HTML viewer | ✅ | Real-time dashboard at `/viewer` || BM25 keyword search | ✅ | Always on, no API key needed || Hybrid BM25 + vector search | ✅ | Requires `GEMINI_API_KEY` || 4-tier memory consolidation | ⚙️ | `CONSOLIDATION_ENABLED=true` + LLM key || Knowledge graph extraction | ⚙️ | `GRAPH_EXTRACTION_ENABLED=true` + LLM key || LLM observation compression | ⚙️ | `AGENTCACHE_AUTO_COMPRESS=true` + LLM key || Lessons with confidence decay | ✅ | Fingerprinted, auto-strengthen on repeat || Memory slots (pinned context) | ✅ | CRUD + auto-reflect || Session replay | ✅ | Full timeline in viewer || Audit log | ✅ | Tracks every write with agent_id + timestamp || HuggingFace Space deploy | ✅ | One-click, data synced to dataset repo || Privacy filtering | ✅ | Strips API keys, tokens before storage |### 4-Tier Memory ModelInspired by how human memory works — raw experience → compressed episodes → extracted facts → learned patterns.| Tier | What | When ||------|------|------|| **Working** | Raw observations from tool use | Every tool call || **Episodic** | Compressed session summaries | Session end || **Semantic** | Extracted facts and patterns | Consolidation || **Procedural** | Workflows and decision patterns | Consolidation |---## MCP IntegrationWire agentcache-python into your agent's MCP config. It speaks the same MCP protocol as the Node.js original.### Most agents (Cursor, Claude Desktop, Cline, Windsurf)```json{"mcpServers": {"agentcache": {"command": "npx","args": ["-y", "@agentcache/mcp"],"env": {"AGENTCACHE_URL": "http://localhost:3111"}}}}```### Claude CodePaste this prompt and your agent will wire everything:```Start agentcache-python: run `python src/app.py` from the agentcache-python directory.Then add this MCP server to ~/.claude.json under mcpServers:{"agentcache": {"command": "npx","args": ["-y", "@agentcache/mcp"],"env": { "AGENTCACHE_URL": "http://localhost:3111" }}}Verify with: curl http://localhost:3111/agentcache/livezOpen the viewer at: http://localhost:3111/viewer```### Available MCP Tools (31)| Tool | Description ||------|-------------|| `memory_save` | Save a long-term insight, decision, or pattern || `memory_recall` | Search past observations by keyword || `memory_smart_search` | Hybrid BM25 + vector semantic search || `memory_sessions` | List recent sessions || `memory_sessions_list` | Retrieve all memory sessions || `memory_timeline` | Chronological observations for a session || `memory_observations` | Observations for a session || `memory_profile` | Per-project concept + file profile || `memory_lessons` | List active lessons with confidence scores || `memory_lesson_save` | Save a lesson (duplicate saves strengthen it) || `memory_lesson_recall` | Search lessons by query || `memory_lesson_search` | Search lessons by keywords || `memory_consolidate` | Run 4-tier memory consolidation || `memory_reflect` | Reflect on session, update context || `memory_diagnose` | Health check across all subsystems || `memory_forget` | Delete memory, session, or observations || `memory_export` | Export all memory data as JSON || `agent_observe` | Log agent execution observation || `agent_remember` | Save agent cache to long-term storage || `memory_antigravity_sync` | Sync Antigravity transcripts to memory || `memory_antigravity_sync_all` | Master sync: transcript + crystallize + reflect || `memory_slot_list` | List all pinned memory slots || `memory_slot_get` | Retrieve a specific pinned memory slot || `memory_slot_create` | Create/overwrite a pinned memory slot || `memory_slot_append` | Append text content to a pinned memory slot || `memory_slot_replace` | Replace pinned memory slot content || `memory_slot_delete` | Delete a pinned memory slot || `memory_action_create` | Create a new work item / action || `memory_action_update` | Update fields of an existing action || `memory_frontier` | Get active and pending actions sorted by priority || `memory_crystallize` | Crystallize/summarize observations in a session |---## API ReferenceBase URL: `http://localhost:3111/agentcache`### Health| Method | Path | Description ||--------|------|-------------|| `GET` | `/livez` | Liveness probe — no auth required |### Sessions| Method | Path | Description ||--------|------|-------------|| `POST` | `/session/start` | Start a new session || `POST` | `/session/end` | End a session || `POST` | `/session/commit` | Commit session with summary || `GET` | `/sessions` | List all sessions |### Observations| Method | Path | Description ||--------|------|-------------|| `POST` | `/observe` | Ingest a hook event observation || `POST` | `/agent/observe` | Simplified observe for direct agent use || `GET` | `/observations` | List observations (`?session_id=`) || `POST` | `/timeline` | Chronological observation window |### Memories| Method | Path | Description ||--------|------|-------------|| `POST` | `/remember` | Save long-term memory || `POST` | `/agent/remember` | Simplified remember || `POST` | `/forget` | Delete memory / session / observations || `POST` | `/search` | BM25 + vector search || `POST` | `/context` | Compile context for a session + project || `GET` | `/memories` | List memories (`?latest=true&limit=N`) || `POST` | `/evolve` | Create a new memory version |### Lessons| Method | Path | Description ||--------|------|-------------|| `GET` | `/lessons` | List lessons || `POST` | `/lessons` | Create lesson || `POST` | `/lessons/search` | Search lessons || `POST` | `/lessons/strengthen` | Reinforce an existing lesson |### Slots| Method | Path | Description ||--------|------|-------------|| `GET` | `/slots` | List all pinned slots || `POST` | `/slot` | Create or update a slot || `GET` | `/slot` | Get slot by name || `DELETE` | `/slot` | Delete a slot || `POST` | `/slot/reflect` | Auto-populate from session observations |### Graph + Profile| Method | Path | Description ||--------|------|-------------|| `GET` | `/relations` | Knowledge graph edges || `POST` | `/relations` | Add a relation || `GET` | `/profile` | Project profile (top concepts, files) |### Actions| Method | Path | Description ||--------|------|-------------|| `GET` | `/actions` | List actions || `POST` | `/actions` | Create an action || `PATCH` | `/actions/<id>` | Update action status / fields || `GET` | `/frontier` | Pending actions sorted by priority || `GET` | `/insights` | List insights |### Replay| Method | Path | Description ||--------|------|-------------|| `GET` | `/replay/sessions` | Sessions list for replay tab || `GET` | `/replay/load` | Full session + observations (`?sessionId=`) |### MCP| Method | Path | Description ||--------|------|-------------|| `GET` | `/mcp/tools` | MCP tool schema list || `POST` | `/mcp/tools` | MCP tool call dispatch |---## ConfigurationCreate `~/.agentcache/.env` (no `export` prefix needed):```env# Server portIII_REST_PORT=3111# Vector search — enables Gemini 768-dim embeddingsGEMINI_API_KEY=your-gemini-key# LLM for compression / consolidation / graph extraction# Any one of these enables LLM features:ANTHROPIC_API_KEY=your-anthropic-key# OPENAI_API_KEY=your-openai-key# GEMINI_API_KEY=your-key (same key as above works for both)# LLM-powered features (disabled by default — spend tokens)CONSOLIDATION_ENABLED=trueGRAPH_EXTRACTION_ENABLED=trueAGENTCACHE_AUTO_COMPRESS=true# Context injection limitsTOKEN_BUDGET=2000MAX_OBS_PER_SESSION=500# Auth — set to require Bearer token on all endpointsAGENTCACHE_SECRET=your-secret# Agent scope isolationAGENT_ID=my-agentAGENTCACHE_AGENT_SCOPE=isolated # only see this agent's data# HuggingFace syncHF_TOKEN=your-hf-tokenAGENTCACHE_DATASET_REPO=username/agentcache-data```### Full Variable Reference| Variable | Default | Purpose ||----------|---------|---------|| `III_REST_PORT` / `PORT` | `3111` | API server port || `GEMINI_API_KEY` / `GOOGLE_API_KEY` | — | Enables 768-dim vector search || `AGENTCACHE_SECRET` | — | Bearer token auth on all endpoints || `AGENT_ID` | — | Default agent ID for scope isolation || `AGENTCACHE_AGENT_SCOPE=isolated` | — | Filters data to current `AGENT_ID` || `MAX_OBS_PER_SESSION` | `500` | Hard cap on observations per session || `TOKEN_BUDGET` | `2000` | Max tokens in compiled context || `GRAPH_EXTRACTION_ENABLED` | `false` | Knowledge graph (needs LLM) || `CONSOLIDATION_ENABLED` | `false` | Memory consolidation (needs LLM) || `AGENTCACHE_AUTO_COMPRESS` | `false` | LLM observation compression |---## ViewerBuilt-in dashboard at **http://localhost:3111/viewer**.| Tab | What You See ||-----|-------------|| **Dashboard** | Session stats, memory counts, recent activity || **Sessions** | Browse sessions, inspect observations || **Memories** | Search, filter, and read long-term memories || **Graph** | Project folder visualization — nodes = folders, edges = shared concepts or parent path || **Timeline** | Per-session chronological observation view || **Lessons** | Confidence-scored lessons with decay tracking || **Slots** | Pinned memory slots editor || **Replay** | Scrub through past sessions frame by frame |---## Deploy to HuggingFaceThis project is designed to run as a HuggingFace Space. Data is stored in an HF dataset repo and restored on every boot — so no persistent disk is needed.### Setup1. Fork this repo as a HuggingFace Space (SDK: Docker)2. Create a dataset repo (e.g. `your-username/agentcache-data`)3. Add Space secrets in the HF dashboard:| Secret | Value ||--------|-------|| `HF_TOKEN` | Your HF write token || `AGENTCACHE_DATASET_REPO` | `your-username/agentcache-data` || `AGENTCACHE_SECRET` | A random secret (optional but recommended) || `GEMINI_API_KEY` | Gemini key (optional, enables vector search) |4. The Space boots, restores `agentcache.db` from the dataset repo, and starts the server### How sync works`sync.py` uses mtime fingerprinting — it only uploads when the database actually changed, so there are no unnecessary uploads during idle periods.```bash# Manual backuppython sync.py# Environment for syncHF_TOKEN=...AGENTCACHE_DATASET_REPO=username/agentcache-data```---## Architecture```agentcache-python/├── src/│ ├── app.py Flask server — all endpoints, WebSocket broadcaster│ ├── db.py SQLite StateKV — WAL mode, audit_log table│ ├── functions.py Core logic — observe, remember, search, context│ ├── search.py BM25 + Gemini vector index + HybridSearch (RRF)│ └── viewer/│ └── index.html Single-file HTML dashboard (no build step)├── sync.py HuggingFace dataset backup/restore├── Dockerfile HF Space container├── start.sh Boot script (restore → start server → start sync)└── requirements.txt 6 Python dependencies, no external DB required```### Database layoutTwo SQLite tables in `~/.agentcache/agentcache.db`:```sql-- All data lives here, namespaced by scopekv_store (scope TEXT NOT NULL, -- e.g. "mem:sessions", "mem:obs:{session_id}"key TEXT NOT NULL,value TEXT NOT NULL, -- JSON-serializedPRIMARY KEY (scope, key))-- Audit trail replaces Dolt git versioningaudit_log (id INTEGER PRIMARY KEY AUTOINCREMENT,ts INTEGER NOT NULL, -- unix millisagent_id TEXT NOT NULL,message TEXT NOT NULL)```### Search pipeline```Query→ BM25 (always) — Porter-stemmed keyword matching→ Vector (if Gemini key) — 768-dim cosine similarity→ RRF fusion — Reciprocal Rank Fusion (k=60)→ Session diversify — max 3 results per session→ Return top-K```---## vs Original agentcache| | agentcache (Node.js) | agentcache-python ||---|---|---|| Runtime | Node.js 20+ | Python 3.10+ || Storage | Dolt SQL (git-versioned MySQL) | SQLite WAL (single file) || Engine dependency | iii-engine (separate binary) | None — just Flask || Embeddings | 6 providers + local `@xenova/transformers` | Gemini 768-dim || MCP tools | 53 | 31 || REST endpoints | 128 | ~50 || Deploy | npm, Docker, fly.io, Railway, Render | Docker, HuggingFace Spaces || Cold boot | ~7s (iii engine warm-up) | <2s || Database size | ~232MB (417 Dolt chunk files) | ~20MB (single `.db` file) || Setup | `npm install -g @agentcache/agentcache` | `pip install -r requirements.txt` |Choose the Python version for: simpler setup, HF Space deployment, single-file database, no Node.js, or Python ecosystem integration.Choose the Node.js version for: the full 53-tool MCP surface, iii-engine observability, production multi-agent deployments, or the full auto-hook suite.---## ContributingSee [CONTRIBUTING.md](CONTRIBUTING.md). Issues and PRs welcome.Priority areas: test coverage, additional embedding providers, more agent hook scripts.---## LicenseApache-2.0 — see [LICENSE](LICENSE).123430 unmodified lines35363738Metadata-Version: 2.4Name: agentcacheVersion: 0.9.8Summary: A Python REST + WebSocket + MCP cache server for AI agents, backed by SQLite30 unmodified linesRequires-Dist: twine>=5.0.0; extra == "dev"Provides-Extra: local-embeddingsRequires-Dist: sentence-transformers>=2.7.0; extra == "local-embeddings"Dynamic: license-file
1234520 unmodified lines26272829303110 unmodified lines4243444546478 unmodified lines56575859LICENSEREADME.mdpyproject.tomlsrc/agentcache/__init__.pysrc/agentcache/app.py20 unmodified linessrc/agentcache/cache/observe.pysrc/agentcache/cache/remember.pysrc/agentcache/cache/timeline.pysrc/agentcache/routes/__init__.pysrc/agentcache/routes/graph.pysrc/agentcache/routes/health.py10 unmodified linessrc/agentcache/viewer/index.htmltests/test_api.pytests/test_auth.pytests/test_context.pytests/test_debounce.pytests/test_folder_graph_build.py8 unmodified linestests/test_remember.pytests/test_route_regressions.pytests/test_search.pytests/test_timeline.py123420 unmodified lines25262728293031323310 unmodified lines44454647484950518 unmodified lines6061626364LICENSEpyproject.tomlsrc/agentcache/__init__.pysrc/agentcache/app.py20 unmodified linessrc/agentcache/cache/observe.pysrc/agentcache/cache/remember.pysrc/agentcache/cache/timeline.pysrc/agentcache/core/__init__.pysrc/agentcache/core/kv_scopes.pysrc/agentcache/core/search_service.pysrc/agentcache/routes/__init__.pysrc/agentcache/routes/graph.pysrc/agentcache/routes/health.py10 unmodified linessrc/agentcache/viewer/index.htmltests/test_api.pytests/test_auth.pytests/test_auto_forget.pytests/test_cli_context.pytests/test_context.pytests/test_debounce.pytests/test_folder_graph_build.py8 unmodified linestests/test_remember.pytests/test_route_regressions.pytests/test_search.pytests/test_security.pytests/test_timeline.py
5 unmodified lines67891011121314151617183 unmodified lines22232425262728293031325 unmodified linesfrom .app import create_appfrom .connect import run_connectfrom .db import StateKVfrom .functions import (folder_graph_build,folder_observe,folder_search,folder_timeline,forget,health_check,remember,)3 unmodified lines"create_app","StateKV","run_connect","folder_observe","folder_search","folder_timeline","folder_graph_build","remember","forget","health_check",]5 unmodified lines67891011121314153 unmodified lines19202122232425262728295 unmodified linesfrom .app import create_appfrom .connect import run_connectfrom .core import KV, ObservationEvents, ObservationStore, SearchServicefrom .db import StateKVfrom .legacy import (folder_graph_build,health_check,remember,)3 unmodified lines"create_app","StateKV","run_connect","KV","ObservationStore","ObservationEvents","SearchService","folder_graph_build","remember","health_check",]
12 unmodified lines1314151617181921 unmodified lines41424344454647484950515253545556575859606162636465664 unmodified lines717273747576772 unmodified lines808182838485862 unmodified lines899091929394954 unmodified lines10010110210310410510610710810911011111211311411511611711811912012114 unmodified lines1361371381391401411 unmodified line14314414514614714814926 unmodified lines17617717817918018118212 unmodified linesfrom flask import Flask, request, send_from_directoryfrom flask_sock import Sockfrom . import functions# Prevent double-import of app when run directly as __main__if __name__ == "__main__":21 unmodified lines_load_env()# Module-level globals — set once by create_app(), read by blueprints via `import app`kv = Noneembedding_provider = Nonepersistence = Nonedef init_services() -> tuple:"""Initialise database, embedding provider, and index persistence."""global kv, embedding_provider, persistenceif kv is not None:return kv, embedding_provider, persistencefrom . import functionsfrom . import search as search_modfrom .db import StateKV# 1. DBkv = StateKV()# 2. Embedding provider — auto-select by priority (D5.3):# GEMINI_API_KEY → OPENAI_API_KEY → AGENTCACHE_LOCAL_EMBEDDING_MODEL → BM25-onlyapi_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")openai_key = os.getenv("OPENAI_API_KEY")4 unmodified linesif api_key:try:embedding_provider = search_mod.GeminiEmbeddingProvider(api_key)functions.set_embedding_provider(embedding_provider)print(f"[search] Embedding provider active: gemini ({embedding_provider.dimensions} dims)")2 unmodified lineselif openai_key:try:embedding_provider = search_mod.OpenAIEmbeddingProvider(openai_key)functions.set_embedding_provider(embedding_provider)print(f"[search] Embedding provider active: openai ({embedding_provider.dimensions} dims)")2 unmodified lineselif local_model:try:embedding_provider = search_mod.SentenceTransformerProvider(local_model)functions.set_embedding_provider(embedding_provider)print(f"[search] Embedding provider active: sentence-transformers/{local_model} ({embedding_provider.dimensions} dims)")4 unmodified lineselse:print("[search] No embedding API key found — running in BM25-only mode.")# 3. Index persistence — use embedding_provider variable set abovehas_vector = embedding_provider is not Nonepersistence = functions.IndexPersistence(kv,functions._bm25_index,functions._vector_index if has_vector else None,)functions.set_index_persistence(persistence)loaded = persistence.load()print(f"[persistence] Load results: BM25={loaded['bm25']}, Vector={loaded['vector']}")# Backfill coordinate lookup index if missing/incompletetry:functions.backfill_obs_lookup_if_needed(kv)except Exception as e:print(f"[db] Warning backfilling obs_lookup: {e}")14 unmodified lines# 4. Flask app + blueprintsflask_app = Flask(__name__)from werkzeug.middleware.proxy_fix import ProxyFixflask_app.wsgi_app = ProxyFix(1 unmodified line)from .routes import register_blueprintsregister_blueprints(flask_app)# 5. WebSocket broadcastersock = Sock(flask_app)26 unmodified linesexcept Exception:_ws_clients.discard(ws)functions.set_stream_broadcaster(_broadcast)# 6. Viewer static routesfrom importlib.resources import files12 unmodified lines1314151617181921 unmodified lines4142434445464748495051525354555657585960616263646566676869704 unmodified lines7576777879802 unmodified lines8384858687882 unmodified lines9192939495964 unmodified lines10110210310410510610710810911011111211311411511611711811912012112212312412512612714 unmodified lines1421431441451461471481491501 unmodified line15215315415515615715815916026 unmodified lines18718818919019119219319419519612 unmodified linesfrom flask import Flask, request, send_from_directoryfrom flask_sock import Sockfrom . import legacy# Prevent double-import of app when run directly as __main__if __name__ == "__main__":21 unmodified lines_load_env()# Module-level singletons — set once by init_services(), read by blueprints and workers.kv = Noneembedding_provider = Nonepersistence = None # kept for backward compat with workers; use search_service insteadsearch_service = None # SearchService instanceobservation_store = None # ObservationStore instancedef init_services() -> tuple:"""Initialise database, SearchService, ObservationStore, and legacy persistence shim."""global kv, embedding_provider, persistence, search_service, observation_storeif kv is not None:return kv, embedding_provider, persistencefrom . import search as search_modfrom .core.observation_store import ObservationEvents, ObservationStorefrom .core.search_service import SearchServicefrom .db import StateKVfrom .search import SearchIndex, VectorIndex# 1. DBkv = StateKV()# 2. Embedding provider — auto-select by priority:# GEMINI_API_KEY → OPENAI_API_KEY → AGENTCACHE_LOCAL_EMBEDDING_MODEL → BM25-onlyapi_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")openai_key = os.getenv("OPENAI_API_KEY")4 unmodified linesif api_key:try:embedding_provider = search_mod.GeminiEmbeddingProvider(api_key)print(f"[search] Embedding provider active: gemini ({embedding_provider.dimensions} dims)")2 unmodified lineselif openai_key:try:embedding_provider = search_mod.OpenAIEmbeddingProvider(openai_key)print(f"[search] Embedding provider active: openai ({embedding_provider.dimensions} dims)")2 unmodified lineselif local_model:try:embedding_provider = search_mod.SentenceTransformerProvider(local_model)print(f"[search] Embedding provider active: sentence-transformers/{local_model} ({embedding_provider.dimensions} dims)")4 unmodified lineselse:print("[search] No embedding API key found — running in BM25-only mode.")# 3. Construct SearchService and ObservationStore with injected dependencies.bm25 = SearchIndex()vector = VectorIndex() if embedding_provider is not None else Nonesearch_service = SearchService(bm25, vector, embedding_provider, kv)observation_store = ObservationStore(kv, search_service=search_service, events=ObservationEvents())# Load persisted indexes.loaded = search_service.load_persisted()print(f"[persistence] Load results: BM25={loaded['bm25']}, Vector={loaded['vector']}")# Keep persistence reference for workers backward compat.persistence = search_service._persistence# Backfill coordinate lookup index if missing/incomplete.try:if observation_store is not None:observation_store.backfill_lookup()except Exception as e:print(f"[db] Warning backfilling obs_lookup: {e}")14 unmodified lines# 4. Flask app + blueprintsflask_app = Flask(__name__)flask_app.extensions["observation_store"] = observation_storeflask_app.extensions["search_service"] = search_servicefrom werkzeug.middleware.proxy_fix import ProxyFixflask_app.wsgi_app = ProxyFix(1 unmodified line)from .routes import register_blueprintsregister_blueprints(flask_app, observation_store=observation_store, search_service=search_service)# 5. WebSocket broadcastersock = Sock(flask_app)26 unmodified linesexcept Exception:_ws_clients.discard(ws)legacy.set_stream_broadcaster(_broadcast)if observation_store and observation_store.events:observation_store.events.on_added.append(_broadcast)# 6. Viewer static routesfrom importlib.resources import files
17 unmodified lines181920212223246 unmodified lines313233343536373839404142434427 unmodified lines72737475767778798081828384858617 unmodified lines# Each name is imported lazily via a try/except so missing items don't break# the package import on partially-initialised environments.# ---------------------------------------------------------------------------from .. import functions as _fn # noqa: E402from .context import context, export_data, rebuild_indexfrom .graph import folder_graph_buildfrom .health import auto_forget, health_check6 unmodified linesfrom .remember import forget, jaccard_similarity, rememberfrom .timeline import folder_search, folder_timelineKV = _fn.KVgenerate_id = _fn.generate_idfingerprint_id = _fn.fingerprint_idnormalize_folder_path = _fn.normalize_folder_pathvalidate_agent_id = _fn.validate_agent_idIndexPersistence = _fn.IndexPersistenceset_embedding_provider = _fn.set_embedding_providerset_index_persistence = _fn.set_index_persistenceset_stream_broadcaster = _fn.set_stream_broadcasterget_agent_id = _fn.get_agent_idrecord_audit = _fn.record_audit27 unmodified lines# health.py"health_check","auto_forget",# functions.py shims (A2.2)"KV","generate_id","fingerprint_id","normalize_folder_path","validate_agent_id","IndexPersistence","set_embedding_provider","set_index_persistence","set_stream_broadcaster","get_agent_id","record_audit",17 unmodified lines18192021222324256 unmodified lines3233343536373839404127 unmodified lines69707172737475767778798017 unmodified lines# Each name is imported lazily via a try/except so missing items don't break# the package import on partially-initialised environments.# ---------------------------------------------------------------------------from .. import legacy as _fn # noqa: E402from ..core import KVfrom .context import context, export_data, rebuild_indexfrom .graph import folder_graph_buildfrom .health import auto_forget, health_check6 unmodified linesfrom .remember import forget, jaccard_similarity, rememberfrom .timeline import folder_search, folder_timelinegenerate_id = _fn.generate_idfingerprint_id = _fn.fingerprint_idnormalize_folder_path = _fn.normalize_folder_pathvalidate_agent_id = _fn.validate_agent_idset_stream_broadcaster = _fn.set_stream_broadcasterget_agent_id = _fn.get_agent_idrecord_audit = _fn.record_audit27 unmodified lines# health.py"health_check","auto_forget",# legacy.py shims"KV","generate_id","fingerprint_id","normalize_folder_path","validate_agent_id","set_stream_broadcaster","get_agent_id","record_audit",
10 unmodified lines1112131415161710 unmodified linesfrom typing import Any, Dictfrom .. import functions as _fnfrom ..db import StateKV10 unmodified lines1112131415161710 unmodified linesfrom typing import Any, Dictfrom .. import legacy as _fnfrom ..db import StateKV
8 unmodified lines91011121314158 unmodified linesfrom typing import Any, Dictfrom .. import functions as _fnfrom ..db import StateKV8 unmodified lines91011121314158 unmodified linesfrom typing import Any, Dictfrom .. import legacy as _fnfrom ..db import StateKV
9 unmodified lines101112131415169 unmodified linesfrom typing import Any, Dictfrom .. import functions as _fnfrom ..db import StateKV9 unmodified lines101112131415169 unmodified linesfrom typing import Any, Dictfrom .. import legacy as _fnfrom ..db import StateKV
11 unmodified lines1213141516171811 unmodified linesfrom typing import Any, Dictfrom .. import functions as _fn # access module-level globals (_bm25_index, etc.)from ..db import StateKV# Re-export for backward compatibility11 unmodified lines1213141516171811 unmodified linesfrom typing import Any, Dictfrom .. import legacy as _fn # access module-level globals (_bm25_index, etc.)from ..db import StateKV# Re-export for backward compatibility
10 unmodified lines1112131415161710 unmodified linesfrom typing import Any, Dictfrom .. import functions as _fnfrom ..db import StateKV10 unmodified lines1112131415161710 unmodified linesfrom typing import Any, Dictfrom .. import legacy as _fnfrom ..db import StateKV
9 unmodified lines101112131415169 unmodified linesfrom typing import Any, Dict, List, Optionalfrom .. import functions as _fnfrom ..db import StateKV9 unmodified lines101112131415169 unmodified linesfrom typing import Any, Dict, List, Optionalfrom .. import legacy as _fnfrom ..db import StateKV
27 unmodified lines2829303132333419 unmodified lines5455565758596052 unmodified lines11311411511611711811927 unmodified linesdef cmd_migrate(args) -> None:"""Run session → folder migration."""from .db import StateKVfrom .functions import migrate_sessions_to_folderskv = StateKV()result = migrate_sessions_to_folders(kv, dry_run=args.dry_run)19 unmodified linesdef cmd_export(args) -> None:"""Export all data as JSON."""from .db import StateKVfrom .functions import export_datakv = StateKV()data = export_data(kv, {})52 unmodified linesimport timefrom .app import init_servicesfrom .functions import KV, normalize_folder_path# 1. Resolve folder and agentcwd = os.getcwd()27 unmodified lines2829303132333419 unmodified lines5455565758596052 unmodified lines11311411511611711811912027 unmodified linesdef cmd_migrate(args) -> None:"""Run session → folder migration."""from .db import StateKVfrom .legacy import migrate_sessions_to_folderskv = StateKV()result = migrate_sessions_to_folders(kv, dry_run=args.dry_run)19 unmodified linesdef cmd_export(args) -> None:"""Export all data as JSON."""from .db import StateKVfrom .legacy import export_datakv = StateKV()data = export_data(kv, {})52 unmodified linesimport timefrom .app import init_servicesfrom .core import KVfrom .core.observation_store import normalize_folder_path# 1. Resolve folder and agentcwd = os.getcwd()
9 unmodified lines10111213149 unmodified linesfrom .observation_store import ObservationEvents, ObservationStorefrom .search_service import IndexPersistence, SearchService__all__ = ["KV", "SearchService", "IndexPersistence", "ObservationStore", "ObservationEvents"]9 unmodified lines101112131415161718199 unmodified linesfrom .observation_store import ObservationEvents, ObservationStorefrom .search_service import IndexPersistence, SearchService__all__ = ["KV","SearchService","IndexPersistence","ObservationStore","ObservationEvents",]
4 unmodified lines567891011124 unmodified linesImport this module wherever a KV scope key is needed — routes, stores, workers."""from typing import Optionalclass KV:# ---- Folder memory scopes ----4 unmodified lines56789104 unmodified linesImport this module wherever a KV scope key is needed — routes, stores, workers."""class KV:# ---- Folder memory scopes ----
103 unmodified lines1041051061071081091101114 unmodified lines116117118119120121122144 unmodified lines26726826927027127227318 unmodified lines2922932942952962972986 unmodified lines30530630730830931031111 unmodified lines32332432532632732832921 unmodified lines35135235335435535635735826 unmodified lines38538638738838939039119 unmodified lines41141241341441541641736 unmodified lines45445545645745845946013 unmodified lines47447547647747847948087 unmodified lines568569570571572103 unmodified linesfolder_path = normalize_folder_path(folder_path_raw)agent_id = validate_agent_id(agent_id_raw)from ..legacy import strip_private_data, infer_type, extract_filessafe_text = strip_private_data(text_raw)[:4000]4 unmodified linesdedup_lock = self._get_dedup_lock(folder_path, agent_id)with dedup_lock:existing_dedup = self.kv.get(KV.obs_dedup(folder_path, agent_id), dedup_fp)if existing_dedup and isinstance(existing_dedup, dict) and existing_dedup.get("obsId"):return {"observationId": existing_dedup["obsId"], "deduplicated": True}max_obs = int(os.getenv("MAX_OBS_PER_FOLDER", "2000"))144 unmodified linestotal_kept = 0for pair in pairs:fp = pair["folderPath"]aid = pair["agentId"]all_obs = self.kv.list(KV.folder_obs(fp, aid))18 unmodified linesduplicates.append(obs["id"])if duplicates:self.forget({"folderPath": fp, "agentId": aid, "observationIds": duplicates})total_removed += len(duplicates)total_kept += len(fingerprint_map)6 unmodified lines{"obsId": obs["id"], "timestamp": obs.get("timestamp", "")},)return {"success": True,"deduplicated": total_removed,11 unmodified linesdeleted = 0deleted_mem_ids: List[str] = []deleted_obs_ids: List[str] = []deleted_session = Falseif memory_id:mem = self.kv.get(KV.memories, memory_id)21 unmodified linesif "observationIds" in data and data["observationIds"] is not None:partial_deleted = 0for oid in obs_ids:obs = self.kv.get(obs_scope, oid)existed = self.kv.delete(obs_scope, oid)if existed:26 unmodified linestry:cb(deleted_obs_ids)except Exception as ex:print(f"[observation_store] Error in on_deleted callback: {ex}")else:all_obs = self.kv.list(obs_scope)for obs in all_obs:19 unmodified linestry:cb(fp, aid)except Exception as ex:print(f"[observation_store] Error in on_folder_deleted callback: {ex}")if session_id and obs_ids:for oid in obs_ids:36 unmodified linesdeleted += 1self.kv.delete(KV.sessions, session_id)self.kv.delete(KV.summaries, session_id)deleted_session = Truedeleted += 2if deleted > 0 and self.search_service:13 unmodified linesindex_entries = self.kv.list(KV.folders)if folder_path is not None:index_entries = [e for e in index_entries if e.get("folderPath") == folder_path]if agent_id is not None:index_entries = [e for e in index_entries if e.get("agentId") == agent_id]87 unmodified linesself.search_service.schedule_persist()return total_indexed103 unmodified lines1041051061071081091104 unmodified lines115116117118119120121122123124125144 unmodified lines27027127227327427518 unmodified lines2942952962972982993003013026 unmodified lines30931031131231331411 unmodified lines32632732832933033121 unmodified lines35335435535635735826 unmodified lines38538638738838939039139239319 unmodified lines41341441541641741841942042136 unmodified lines45845946046146246313 unmodified lines47747847948048148248348448587 unmodified lines573574575103 unmodified linesfolder_path = normalize_folder_path(folder_path_raw)agent_id = validate_agent_id(agent_id_raw)from ..legacy import extract_files, infer_type, strip_private_datasafe_text = strip_private_data(text_raw)[:4000]4 unmodified linesdedup_lock = self._get_dedup_lock(folder_path, agent_id)with dedup_lock:existing_dedup = self.kv.get(KV.obs_dedup(folder_path, agent_id), dedup_fp)if (existing_dedupand isinstance(existing_dedup, dict)and existing_dedup.get("obsId")):return {"observationId": existing_dedup["obsId"], "deduplicated": True}max_obs = int(os.getenv("MAX_OBS_PER_FOLDER", "2000"))144 unmodified linestotal_kept = 0for pair in pairs:fp = pair["folderPath"]aid = pair["agentId"]all_obs = self.kv.list(KV.folder_obs(fp, aid))18 unmodified linesduplicates.append(obs["id"])if duplicates:self.forget({"folderPath": fp, "agentId": aid, "observationIds": duplicates})total_removed += len(duplicates)total_kept += len(fingerprint_map)6 unmodified lines{"obsId": obs["id"], "timestamp": obs.get("timestamp", "")},)return {"success": True,"deduplicated": total_removed,11 unmodified linesdeleted = 0deleted_mem_ids: List[str] = []deleted_obs_ids: List[str] = []if memory_id:mem = self.kv.get(KV.memories, memory_id)21 unmodified linesif "observationIds" in data and data["observationIds"] is not None:partial_deleted = 0for oid in obs_ids:obs = self.kv.get(obs_scope, oid)existed = self.kv.delete(obs_scope, oid)if existed:26 unmodified linestry:cb(deleted_obs_ids)except Exception as ex:print(f"[observation_store] Error in on_deleted callback: {ex}")else:all_obs = self.kv.list(obs_scope)for obs in all_obs:19 unmodified linestry:cb(fp, aid)except Exception as ex:print(f"[observation_store] Error in on_folder_deleted callback: {ex}")if session_id and obs_ids:for oid in obs_ids:36 unmodified linesdeleted += 1self.kv.delete(KV.sessions, session_id)self.kv.delete(KV.summaries, session_id)deleted += 2if deleted > 0 and self.search_service:13 unmodified linesindex_entries = self.kv.list(KV.folders)if folder_path is not None:index_entries = [e for e in index_entries if e.get("folderPath") == folder_path]if agent_id is not None:index_entries = [e for e in index_entries if e.get("agentId") == agent_id]87 unmodified linesself.search_service.schedule_persist()return total_indexed
7 unmodified lines8910111213149 unmodified lines24252627282930118 unmodified lines149150151152153154155156157158159160161162209 unmodified lines3723733743753763773787 unmodified linesfrom __future__ import annotationsimport jsonimport mathimport sqlite3import threadingimport time9 unmodified linesDEBOUNCE_SECONDS: float = 5.0def __init__(self, kv: Any, bm25: SearchIndex, vector: Optional[VectorIndex] = None):self.kv = kvself.bm25 = bm25self.vector = vector118 unmodified linescursor.close()except sqlite3.OperationalError as ex:err_msg = str(ex).lower()if ("locked" in err_msg or "busy" in err_msg) and attempt < max_retries - 1:time.sleep(delay)delay *= 2continueprint(f"[index persistence] error cleaning up obsolete shards: {ex}")breakexcept Exception as ex:print(f"[index persistence] error cleaning up obsolete shards: {ex}")breakif (209 unmodified linesresults.append(result)seen_ids.add(obs_id)# Lazy backfill lookupactive_kv.set(KV.obs_lookup, obs_id, {"folderPath": fp, "agentId": aid})found = Truebreakif found:7 unmodified lines89101112139 unmodified lines232425262728293031118 unmodified lines150151152153154155156157158159160161162163164165166167168169209 unmodified lines3793803813823833843853863873883897 unmodified linesfrom __future__ import annotationsimport jsonimport sqlite3import threadingimport time9 unmodified linesDEBOUNCE_SECONDS: float = 5.0def __init__(self, kv: Any, bm25: SearchIndex, vector: Optional[VectorIndex] = None):self.kv = kvself.bm25 = bm25self.vector = vector118 unmodified linescursor.close()except sqlite3.OperationalError as ex:err_msg = str(ex).lower()if ("locked" in err_msg or "busy" in err_msg) and attempt < max_retries - 1:time.sleep(delay)delay *= 2continueprint(f"[index persistence] error cleaning up obsolete shards: {ex}")breakexcept Exception as ex:print(f"[index persistence] error cleaning up obsolete shards: {ex}")breakif (209 unmodified linesresults.append(result)seen_ids.add(obs_id)# Lazy backfill lookupactive_kv.set(KV.obs_lookup,obs_id,{"folderPath": fp, "agentId": aid},)found = Truebreakif found:
1 unmodified line234567891011122 unmodified lines15161718192021789 unmodified lines81181281381481581681736 unmodified lines85485585685785885986086186213 unmodified lines87687787887988088188235 unmodified lines918919920921922923924127 unmodified lines1052105310541055105610571058899 unmodified lines1958195919601961196219631964575 unmodified lines254025412542254325442545254631 unmodified lines2578257925802581258225832584144 unmodified lines2729273027312732273327342735807 unmodified lines35433544354535463547354835491 unmodified line35513552355335543555355620 unmodified lines35773578357935801 unmodified lineimport datetimeimport hashlibimport jsonimport osimport reimport sqlite3import threadingimport timeimport uuid2 unmodified linesfrom .core.kv_scopes import KV # noqa: F401 re-exported for backward compatfrom .core.search_service import IndexPersistence # noqa: F401from .db import StateKVfrom .search import HybridSearch, SearchIndex, VectorIndex# =====================================================================# Global Variables / Module State789 unmodified linesif k in raw_for_synthetic:synthetic[k] = raw_for_synthetic[k]kv.set(KV.observations(session_id), obs_id, synthetic)if _search_service: _search_service.bm25.add(synthetic)comb_text = synthetic["title"] + " " + (synthetic.get("narrative") or "")vector_index_add_guarded(36 unmodified linesdef _get_observation_store(kv: StateKV):from . import app as app_moduleif getattr(app_module, "observation_store", None) is not None:return app_module.observation_storefrom .core.observation_store import ObservationStorereturn ObservationStore(kv, search_service=_search_service)13 unmodified linesreturn store.dedup(folder_path_raw, agent_id_raw)# =====================================================================# Folder-Based Search (folder_search)# =====================================================================35 unmodified lines)# =====================================================================# Memory System (Remember, Forget, Evolve)# =====================================================================127 unmodified linesreturn store.forget(data)# =====================================================================# Prompt Context Compilation System# =====================================================================899 unmodified linesreturn store.rebuild_index()# =====================================================================# Advanced Function Stubs / CRUD Operations# =====================================================================575 unmodified linesrefs = kv.get(KV.imageRefs, ref) or 0if refs > 0:kv.set(KV.imageRefs, ref, refs - 1)if _search_service: _search_service.remove(mem_id)# Commit evictions for session-based observationsfor sid, obs_id in evicted_observations:31 unmodified lines).hexdigest()kv.delete(KV.obs_dedup(fp, aid), dedup_fp)if _search_service: _search_service.remove(obs_id)pair_key = (fp, aid)folder_deletes[pair_key] = folder_deletes.get(pair_key, 0) + 1144 unmodified lines}def strip_xml_wrappers(raw: str) -> str:if not raw:return ""807 unmodified linesstore.backfill_lookup()def verify_index_sync_on_boot(kv: StateKV, search_service: Optional[Any] = None) -> bool:"""Check if the search index size matches the database counts.Returns True if in sync, False if a rebuild is needed."""1 unmodified linesvc = search_service or _search_serviceif svc is None:from . import app as app_modulesvc = getattr(app_module, "search_service", None)# 1. Total folder obs count20 unmodified linesexcept Exception as e:print(f"[persistence] verify_index_sync_on_boot failed: {e}")return False1 unmodified line23456789102 unmodified lines131415161718789 unmodified lines80880981081181281381481536 unmodified lines85285385485585685785885986086186213 unmodified lines87687787887988088135 unmodified lines917918919920921922127 unmodified lines105010511052105310541055899 unmodified lines195519561957195819591960575 unmodified lines2536253725382539254025412542254331 unmodified lines25752576257725782579258025812582144 unmodified lines272727282729273027312732807 unmodified lines3540354135423543354435453546354735481 unmodified line355035513552355335543555355620 unmodified lines3577357835791 unmodified lineimport datetimeimport hashlibimport jsonimport osimport reimport threadingimport timeimport uuid2 unmodified linesfrom .core.kv_scopes import KV # noqa: F401 re-exported for backward compatfrom .core.search_service import IndexPersistence # noqa: F401from .db import StateKV# =====================================================================# Global Variables / Module State789 unmodified linesif k in raw_for_synthetic:synthetic[k] = raw_for_synthetic[k]kv.set(KV.observations(session_id), obs_id, synthetic)if _search_service:_search_service.bm25.add(synthetic)comb_text = synthetic["title"] + " " + (synthetic.get("narrative") or "")vector_index_add_guarded(36 unmodified linesdef _get_observation_store(kv: StateKV):from . import app as app_moduleif getattr(app_module, "observation_store", None) is not None:return app_module.observation_storefrom .core.observation_store import ObservationStorereturn ObservationStore(kv, search_service=_search_service)13 unmodified linesreturn store.dedup(folder_path_raw, agent_id_raw)# =====================================================================# Folder-Based Search (folder_search)# =====================================================================35 unmodified lines)# =====================================================================# Memory System (Remember, Forget, Evolve)# =====================================================================127 unmodified linesreturn store.forget(data)# =====================================================================# Prompt Context Compilation System# =====================================================================899 unmodified linesreturn store.rebuild_index()# =====================================================================# Advanced Function Stubs / CRUD Operations# =====================================================================575 unmodified linesrefs = kv.get(KV.imageRefs, ref) or 0if refs > 0:kv.set(KV.imageRefs, ref, refs - 1)if _search_service:_search_service.remove(mem_id)# Commit evictions for session-based observationsfor sid, obs_id in evicted_observations:31 unmodified lines).hexdigest()kv.delete(KV.obs_dedup(fp, aid), dedup_fp)if _search_service:_search_service.remove(obs_id)pair_key = (fp, aid)folder_deletes[pair_key] = folder_deletes.get(pair_key, 0) + 1144 unmodified lines}def strip_xml_wrappers(raw: str) -> str:if not raw:return ""807 unmodified linesstore.backfill_lookup()def verify_index_sync_on_boot(kv: StateKV, search_service: Optional[Any] = None) -> bool:"""Check if the search index size matches the database counts.Returns True if in sync, False if a rebuild is needed."""1 unmodified linesvc = search_service or _search_serviceif svc is None:from . import app as app_modulesvc = getattr(app_module, "search_service", None)# 1. Total folder obs count20 unmodified linesexcept Exception as e:print(f"[persistence] verify_index_sync_on_boot failed: {e}")return False
218 unmodified lines219220221222223224225142 unmodified lines368369370371372373374106 unmodified lines4814824834844854864874884894904914924934944954964974984995005015025035046 unmodified lines511512513514515516517518519520521522523524525526527528218 unmodified linescompressed: List[Dict[str, Any]],first_prompt: str = None,) -> None:from .functions import KVif not raw_obs:return142 unmodified linesdef import_jsonl_data(kv, path: str = None, max_files: int = None) -> Dict[str, Any]:from .functions import KV, _bm25_index, build_synthetic_compressiondefault_root = os.path.expanduser(os.path.join("~", ".claude", "projects"))raw_path = path or default_root106 unmodified lines}kv.set(KV.sessions, session["id"], session)from .functions import vector_index_add_guardedcompressed = []for obs in parsed["observations"]:synthetic = build_synthetic_compression(obs)compressed.append(synthetic)kv.set(KV.observations(parsed["sessionId"]), obs["id"], synthetic)# Index_bm25_index.add(synthetic)comb_text = synthetic["title"] + " " + (synthetic.get("narrative") or "")vector_index_add_guarded(synthetic["id"],synthetic["sessionId"],comb_text,{"kind": "synthetic", "logId": synthetic["id"]},)observation_count += len(parsed["observations"])session_ids.append(parsed["sessionId"])6 unmodified linesfirst_prompt,)# Save the updated persistence statefrom . import functionsif functions._index_persistence:try:functions._index_persistence.save()except Exception as e:print(f"[import-jsonl] Warning saving index persistence: {e}")# Audit trailtry:from .functions import log_auditlog_audit(kv,218 unmodified lines219220221222223224225142 unmodified lines368369370371372373374375106 unmodified lines4824834844854864874884894904914924936 unmodified lines500501502503504505506507508218 unmodified linescompressed: List[Dict[str, Any]],first_prompt: str = None,) -> None:from .core import KVif not raw_obs:return142 unmodified linesdef import_jsonl_data(kv, path: str = None, max_files: int = None) -> Dict[str, Any]:from .core import KVfrom .legacy import build_synthetic_compressiondefault_root = os.path.expanduser(os.path.join("~", ".claude", "projects"))raw_path = path or default_root106 unmodified lines}kv.set(KV.sessions, session["id"], session)compressed = []for obs in parsed["observations"]:synthetic = build_synthetic_compression(obs)compressed.append(synthetic)kv.set(KV.observations(parsed["sessionId"]), obs["id"], synthetic)observation_count += len(parsed["observations"])session_ids.append(parsed["sessionId"])6 unmodified linesfirst_prompt,)# Audit trailtry:from .legacy import log_auditlog_audit(kv,
3 unmodified lines456789101112131415161718192021222324252627282930313233343 unmodified linesImport and register all blueprints via register_blueprints(app)."""from .graph import create_graph_bpfrom .health import create_health_bpfrom .mcp import mcp_bpfrom .memories import create_memories_bpfrom .migration import migration_bpfrom .observations import create_observations_bp, observations_bpfrom .search import search_bpdef register_blueprints(app, observation_store=None, search_service=None, kv=None):"""Register all route blueprints on a Flask application instance."""obs_bp = (create_observations_bp(observation_store)if observation_storeelse observations_bp)if kv is None and observation_store is not None:kv = observation_store.kvapp.register_blueprint(obs_bp)app.register_blueprint(create_memories_bp(kv))app.register_blueprint(search_bp)app.register_blueprint(create_graph_bp(kv))app.register_blueprint(create_health_bp(kv))app.register_blueprint(mcp_bp)app.register_blueprint(migration_bp)3 unmodified lines45678910111213141516171819202122232425262728293 unmodified linesImport and register all blueprints via register_blueprints(app)."""from .graph import graph_bpfrom .health import health_bpfrom .mcp import mcp_bpfrom .memories import memories_bpfrom .migration import migration_bpfrom .observations import create_observations_bp, observations_bpfrom .search import search_bpdef register_blueprints(app, observation_store=None, search_service=None):"""Register all route blueprints on a Flask application instance."""obs_bp = (create_observations_bp(observation_store)if observation_storeelse observations_bp)app.register_blueprint(obs_bp)app.register_blueprint(memories_bp)app.register_blueprint(search_bp)app.register_blueprint(graph_bp)app.register_blueprint(health_bp)app.register_blueprint(mcp_bp)app.register_blueprint(migration_bp)
21 unmodified lines22232425262753 unmodified lines818283848521 unmodified linesif kv is not None:return kvfrom .. import app as app_modulereturn app_module.kv# ------------------------------------------------------------------53 unmodified linesreturn bpgraph_bp = create_graph_bp(None)21 unmodified lines2223242526272853 unmodified lines8283848521 unmodified linesif kv is not None:return kvfrom .. import app as app_modulereturn app_module.kv# ------------------------------------------------------------------53 unmodified linesreturn bpgraph_bp = create_graph_bp(None)
24 unmodified lines25262728293059 unmodified lines9091929394959624 unmodified linesif kv is not None:return kvfrom .. import app as app_modulereturn app_module.kv# ------------------------------------------------------------------59 unmodified linesres = query_audit(_get_kv(), {"operation": op, "limit": limit})return jsonify({"entries": res, "success": True}), 200# ------------------------------------------------------------------# GET /agentcache/config/flags# ------------------------------------------------------------------24 unmodified lines2526272829303159 unmodified lines91929394959624 unmodified linesif kv is not None:return kvfrom .. import app as app_modulereturn app_module.kv# ------------------------------------------------------------------59 unmodified linesres = query_audit(_get_kv(), {"operation": op, "limit": limit})return jsonify({"entries": res, "success": True}), 200# ------------------------------------------------------------------# GET /agentcache/config/flags# ------------------------------------------------------------------
14 unmodified lines1516171819202130 unmodified lines52535455565758342 unmodified lines40140240340440540640732 unmodified lines44044144244344444544628 unmodified lines47547647747847948048114 unmodified linesfrom .. import legacyfrom ..core import KVmcp_bp = Blueprint("mcp", __name__)30 unmodified linesreturn app_module.observation_storedef _datetime_now_iso() -> str:return (datetime.datetime.now(datetime.timezone.utc).isoformat().replace("+00:00", "Z")342 unmodified linessearch_svc = _get_search_service()if search_svc is not None:res = search_svc.search(query=q, limit=limit, folder_path=folder_path, agent_id=agent_id, kv=kv)else:res = []32 unmodified linessearch_svc = _get_search_service()if search_svc is not None:res = search_svc.search(query=q, limit=limit, folder_path=folder_path, agent_id=agent_id, kv=kv)else:res = []28 unmodified linesres = {"success": False, "deleted": 0}text_out = json.dumps(res, indent=2)elif name in ("cache_export", "memory_export"):res = legacy.export_data(kv, {})text_out = json.dumps(res, indent=2)14 unmodified lines15161718192030 unmodified lines515253545556342 unmodified lines39940040140240340440540640740840932 unmodified lines44244344444544644744844945045145228 unmodified lines48148248348448548614 unmodified linesfrom .. import legacyfrom ..core import KVmcp_bp = Blueprint("mcp", __name__)30 unmodified linesreturn app_module.observation_storedef _datetime_now_iso() -> str:return (datetime.datetime.now(datetime.timezone.utc).isoformat().replace("+00:00", "Z")342 unmodified linessearch_svc = _get_search_service()if search_svc is not None:res = search_svc.search(query=q,limit=limit,folder_path=folder_path,agent_id=agent_id,kv=kv,)else:res = []32 unmodified linessearch_svc = _get_search_service()if search_svc is not None:res = search_svc.search(query=q,limit=limit,folder_path=folder_path,agent_id=agent_id,kv=kv,)else:res = []28 unmodified linesres = {"success": False, "deleted": 0}text_out = json.dumps(res, indent=2)elif name in ("cache_export", "memory_export"):res = legacy.export_data(kv, {})text_out = json.dumps(res, indent=2)
22 unmodified lines23242526272811 unmodified lines40414243444546474864 unmodified lines11311411511611711811922 unmodified linesif kv is not None:return kvfrom .. import app as app_modulereturn app_module.kv# ------------------------------------------------------------------11 unmodified linesexcept Exception as e:return jsonify({"error": str(e)}), 400# ------------------------------------------------------------------# POST /agentmemory/agent/remember# ------------------------------------------------------------------64 unmodified linesres = functions.forget(_get_kv(), body)return jsonify(res), 200except Exception as e:return jsonify({"error": str(e)}), 400return bp22 unmodified lines2324252627282911 unmodified lines41424344454664 unmodified lines11111211311411511622 unmodified linesif kv is not None:return kvfrom .. import app as app_modulereturn app_module.kv# ------------------------------------------------------------------11 unmodified linesexcept Exception as e:return jsonify({"error": str(e)}), 400# ------------------------------------------------------------------# POST /agentmemory/agent/remember# ------------------------------------------------------------------64 unmodified linesres = functions.forget(_get_kv(), body)return jsonify(res), 200except Exception as e:return jsonify({"error": str(e)}), 400return bp
8 unmodified lines91011121314158 unmodified linesfrom flask import Blueprint, jsonify, requestfrom .. import functionsmigration_bp = Blueprint("migration", __name__)8 unmodified lines91011121314158 unmodified linesfrom flask import Blueprint, jsonify, requestfrom .. import legacy as functionsmigration_bp = Blueprint("migration", __name__)
1 unmodified line234567891011121314151617181920212218 unmodified lines41424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103111 unmodified lineObservation routes blueprint.Handles:POST /agentmemory/observePOST /agentmemory/agent/observeGET /agentmemory/folder/observationsGET /agentmemory/folders"""import datetimeimport osfrom flask import Blueprint, jsonify, requestfrom .. import functionsfrom ..functions import KVobservations_bp = Blueprint("observations", __name__)def _datetime_now_iso() -> str:18 unmodified linesreturn Nonedef _get_kv():"""Retrieve the shared kv instance from the app module."""from .. import app as app_modulereturn app_module.kv# ---------------------------------------------------------------------------# POST /agentmemory/observe (legacy raw hook endpoint + auto-compat shim)# ---------------------------------------------------------------------------@observations_bp.route("/agentcache/observe", methods=["POST"])@observations_bp.route("/agentmemory/observe", methods=["POST"])def api_observe():auth_err = _check_auth()if auth_err:return auth_errbody = {}try:body = request.get_json(force=True) or {}folder_path = body.get("folderPath")agent_id = body.get("agentId")text = body.get("text") or body.get("content") or ""if not folder_path or not agent_id or not text:return jsonify({"error": "folderPath, agentId, and text are required"}), 400payload = {"folderPath": folder_path,"agentId": agent_id,"text": text,"timestamp": body.get("timestamp") or _datetime_now_iso(),"type": body.get("type"),"title": body.get("title"),"concepts": body.get("concepts"),"files": body.get("files"),"importance": body.get("importance"),}res = functions.folder_observe(_get_kv(), payload)return jsonify(res), 201except Exception as e:import tracebacktb = traceback.format_exc()print(f"[observe] 400 — keys={list(body.keys())} {type(e).__name__}: {e}\n{tb}")return jsonify({"error": str(e),"detail": type(e).__name__,"keys": list(body.keys()),"tb": tb,}), 400# ---------------------------------------------------------------------------# POST /agentmemory/agent/observe# ---------------------------------------------------------------------------@observations_bp.route("/agentcache/agent/observe", methods=["POST"])@observations_bp.route("/agentmemory/agent/observe", methods=["POST"])def api_agent_observe():auth_err = _check_auth()if auth_err:return auth_errtry:body = request.get_json(force=True) or {}folder_path = body.get("folderPath")agent_id = body.get("agentId")text = body.get("text") or body.get("content") or ""if not folder_path or not agent_id or not text:return jsonify({"error": "folderPath, agentId, and text are required"}), 400# sessionId accepted but ignored (folder-based model)timestamp = body.get("timestamp") or _datetime_now_iso()payload = {"folderPath": folder_path,"agentId": agent_id,"text": text,"timestamp": timestamp,"type": body.get("type"),"title": body.get("title"),"concepts": body.get("concepts"),"files": body.get("files"),"importance": body.get("importance"),}res = functions.folder_observe(_get_kv(), payload)return jsonify(res), 201except ValueError as e:import tracebackprint(f"[agent_observe] 400 ValueError — body keys: {list(body.keys())} — {e}")return jsonify({"error": str(e)}), 400except Exception as e:import tracebackprint(f"[agent_observe] 400 error — body keys: {list(body.keys())} — {type(e).__name__}: {e}")print(traceback.format_exc())return jsonify({"error": str(e), "detail": type(e).__name__}), 400# ---------------------------------------------------------------------------# GET /agentmemory/folders# ---------------------------------------------------------------------------@observations_bp.route("/agentcache/folders", methods=["GET"])@observations_bp.route("/agentmemory/folders", methods=["GET"])def api_folders():auth_err = _check_auth()if auth_err:return auth_errfolders = sorted(_get_kv().list(KV.folders),key=lambda x: x.get("lastUpdated", ""),reverse=True,)if functions.is_agent_scope_isolated():aid = functions.get_agent_id()if aid:folders = [f for f in folders if f.get("agentId") == aid]return jsonify({"folders": folders}), 200# ---------------------------------------------------------------------------# GET /agentmemory/folder/observations# ---------------------------------------------------------------------------@observations_bp.route("/agentcache/folder/observations", methods=["GET"])@observations_bp.route("/agentmemory/folder/observations", methods=["GET"])def api_folder_observations():auth_err = _check_auth()if auth_err:return auth_errfp = request.args.get("folderPath")aid = request.args.get("agentId")if not fp or not aid:return jsonify({"error": "folderPath and agentId are required"}), 400if functions.is_agent_scope_isolated():current_aid = functions.get_agent_id()if current_aid and aid != current_aid:return jsonify({"error": "Unauthorized: Agent scope is isolated to another agent"}), 403observations = sorted(_get_kv().list(KV.folder_obs(fp, aid)),key=lambda x: x.get("timestamp", ""),reverse=True,)return jsonify({"observations": observations, "folderPath": fp, "agentId": aid}), 200# ---------------------------------------------------------------------------# POST /agentmemory/session/start (legacy compat shim → 200 no-op)# ---------------------------------------------------------------------------@observations_bp.route("/agentcache/session/start", methods=["POST"])@observations_bp.route("/agentmemory/session/start", methods=["POST"])def api_session_start():"""Legacy session/start — clients in the wild still call this.Return a synthetic session ID so callers don't error out."""auth_err = _check_auth()if auth_err:return auth_errimport uuidbody = request.get_json(force=True) or {}session_id = body.get("sessionId") or f"compat_{uuid.uuid4().hex[:16]}"return jsonify({"sessionId": session_id,"status": "active","message": "Session model migrated to folder-based. Use /agentmemory/agent/observe.",}), 200# ---------------------------------------------------------------------------# POST /agentmemory/session/end (legacy compat shim → 200 no-op)# ---------------------------------------------------------------------------@observations_bp.route("/agentcache/session/end", methods=["POST"])@observations_bp.route("/agentmemory/session/end", methods=["POST"])def api_session_end():auth_err = _check_auth()if auth_err:return auth_errreturn jsonify({"success": True, "message": "Session model is now folder-based."}), 200# ---------------------------------------------------------------------------# GET /agentmemory/observations (legacy compat shim)# ---------------------------------------------------------------------------@observations_bp.route("/agentcache/folder/dedup", methods=["POST"])@observations_bp.route("/agentmemory/folder/dedup", methods=["POST"])def api_folder_dedup():"""POST /agentmemory/folder/dedup — remove duplicate observations.Body (both optional):folderPath: str — deduplicate only this folder pairagentId: str — deduplicate only this agentIf both are omitted all folder pairs are processed.Returns: {"success": bool, "deduplicated": int, "pairs_processed": int, "kept": int}"""auth_err = _check_auth()if auth_err:return auth_errtry:body = request.get_json(force=True) or {}folder_path = body.get("folderPath") or Noneagent_id = body.get("agentId") or Noneres = functions.dedup_folder_observations(_get_kv(), folder_path, agent_id)return jsonify(res), 200except Exception as e:return jsonify({"error": str(e)}), 400# ---------------------------------------------------------------------------# GET /agentmemory/observations (legacy compat shim)# ---------------------------------------------------------------------------@observations_bp.route("/agentcache/observations", methods=["GET"])@observations_bp.route("/agentmemory/observations", methods=["GET"])def api_observations_legacy():"""Legacy /observations?sessionId=... shim.Reads from legacy KV scope if data exists, otherwise returns empty list."""auth_err = _check_auth()if auth_err:return auth_errsession_id = request.args.get("sessionId", "")if not session_id:return jsonify({"observations": [], "sessionId": ""}), 200try:obs = sorted(_get_kv().list(functions.KV.observations(session_id)),key=lambda x: x.get("timestamp", ""),reverse=True,)return jsonify({"observations": obs, "sessionId": session_id}), 200except Exception as e:return jsonify({"observations": [], "sessionId": session_id, "error": str(e)}), 2001 unmodified line234567891011121314151617181920212218 unmodified lines41424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942951 unmodified lineObservation routes blueprint.Handles:POST /agentcache/observePOST /agentcache/agent/observeGET /agentcache/folder/observationsGET /agentcache/foldersPOST /agentcache/folder/dedup"""import datetimeimport osfrom typing import Optionalfrom flask import Blueprint, jsonify, requestfrom ..core.kv_scopes import KVfrom ..core.observation_store import ObservationStoredef _datetime_now_iso() -> str:18 unmodified linesreturn Nonedef create_observations_bp(observation_store: Optional[ObservationStore] = None,) -> Blueprint:bp = Blueprint("observations", __name__)def get_store() -> ObservationStore:if observation_store is not None:return observation_storefrom flask import current_appstore = current_app.extensions.get("observation_store")if store is None:from .. import app as app_modulestore = getattr(app_module, "observation_store", None)if store is None:raise RuntimeError("ObservationStore is not initialized")return storedef get_kv():return get_store().kv@bp.route("/agentcache/observe", methods=["POST"])@bp.route("/agentmemory/observe", methods=["POST"])def api_observe():auth_err = _check_auth()if auth_err:return auth_errbody = {}try:body = request.get_json(force=True) or {}folder_path = body.get("folderPath")agent_id = body.get("agentId")text = body.get("text") or body.get("content") or ""if not folder_path or not agent_id or not text:return (jsonify({"error": "folderPath, agentId, and text are required"}),400,)payload = {"folderPath": folder_path,"agentId": agent_id,"text": text,"timestamp": body.get("timestamp") or _datetime_now_iso(),"type": body.get("type"),"title": body.get("title"),"concepts": body.get("concepts"),"files": body.get("files"),"importance": body.get("importance"),}res = get_store().ingest(payload)return jsonify(res), 201except Exception as e:import tracebacktb = traceback.format_exc()print(f"[observe] 400 — keys={list(body.keys())} {type(e).__name__}: {e}\n{tb}")return (jsonify({"error": str(e),"detail": type(e).__name__,"keys": list(body.keys()),"tb": tb,}),400,)@bp.route("/agentcache/agent/observe", methods=["POST"])@bp.route("/agentmemory/agent/observe", methods=["POST"])def api_agent_observe():auth_err = _check_auth()if auth_err:return auth_errtry:body = request.get_json(force=True) or {}folder_path = body.get("folderPath")agent_id = body.get("agentId")text = body.get("text") or body.get("content") or ""if not folder_path or not agent_id or not text:return (jsonify({"error": "folderPath, agentId, and text are required"}),400,)timestamp = body.get("timestamp") or _datetime_now_iso()payload = {"folderPath": folder_path,"agentId": agent_id,"text": text,"timestamp": timestamp,"type": body.get("type"),"title": body.get("title"),"concepts": body.get("concepts"),"files": body.get("files"),"importance": body.get("importance"),}res = get_store().ingest(payload)return jsonify(res), 201except ValueError as e:print(f"[agent_observe] 400 ValueError — body keys: {list(body.keys())} — {e}")return jsonify({"error": str(e)}), 400except Exception as e:import tracebackprint(f"[agent_observe] 400 error — body keys: {list(body.keys())} — {type(e).__name__}: {e}")print(traceback.format_exc())return jsonify({"error": str(e), "detail": type(e).__name__}), 400@bp.route("/agentcache/folders", methods=["GET"])@bp.route("/agentmemory/folders", methods=["GET"])def api_folders():auth_err = _check_auth()if auth_err:return auth_errfrom .. import legacyfolders = sorted(get_kv().list(KV.folders),key=lambda x: x.get("lastUpdated", ""),reverse=True,)if legacy.is_agent_scope_isolated():aid = legacy.get_agent_id()if aid:folders = [f for f in folders if f.get("agentId") == aid]return jsonify({"folders": folders}), 200@bp.route("/agentcache/folder/observations", methods=["GET"])@bp.route("/agentmemory/folder/observations", methods=["GET"])def api_folder_observations():auth_err = _check_auth()if auth_err:return auth_errfp = request.args.get("folderPath")aid = request.args.get("agentId")if not fp or not aid:return jsonify({"error": "folderPath and agentId are required"}), 400from .. import legacyif legacy.is_agent_scope_isolated():current_aid = legacy.get_agent_id()if current_aid and aid != current_aid:return (jsonify({"error": "Unauthorized: Agent scope is isolated to another agent"}),403,)observations = sorted(get_kv().list(KV.folder_obs(fp, aid)),key=lambda x: x.get("timestamp", ""),reverse=True,)return (jsonify({"observations": observations, "folderPath": fp, "agentId": aid}),200,)@bp.route("/agentcache/session/start", methods=["POST"])@bp.route("/agentmemory/session/start", methods=["POST"])def api_session_start():auth_err = _check_auth()if auth_err:return auth_errimport uuidbody = request.get_json(force=True) or {}session_id = body.get("sessionId") or f"compat_{uuid.uuid4().hex[:16]}"return (jsonify({"sessionId": session_id,"status": "active","message": "Session model migrated to folder-based. Use /agentmemory/agent/observe.",}),200,)@bp.route("/agentcache/session/end", methods=["POST"])@bp.route("/agentmemory/session/end", methods=["POST"])def api_session_end():auth_err = _check_auth()if auth_err:return auth_errreturn (jsonify({"success": True, "message": "Session model is now folder-based."}),200,)@bp.route("/agentcache/folder/dedup", methods=["POST"])@bp.route("/agentmemory/folder/dedup", methods=["POST"])def api_folder_dedup():auth_err = _check_auth()if auth_err:return auth_errtry:body = request.get_json(force=True) or {}folder_path = body.get("folderPath") or Noneagent_id = body.get("agentId") or Noneres = get_store().dedup(folder_path, agent_id)return jsonify(res), 200except Exception as e:return jsonify({"error": str(e)}), 400@bp.route("/agentcache/observations", methods=["GET"])@bp.route("/agentmemory/observations", methods=["GET"])def api_observations_legacy():auth_err = _check_auth()if auth_err:return auth_errsession_id = request.args.get("sessionId", "")if not session_id:return jsonify({"observations": [], "sessionId": ""}), 200try:obs = sorted(get_kv().list(KV.observations(session_id)),key=lambda x: x.get("timestamp", ""),reverse=True,)return jsonify({"observations": obs, "sessionId": session_id}), 200except Exception as e:return (jsonify({"observations": [], "sessionId": session_id, "error": str(e)}),200,)return bpobservations_bp = create_observations_bp()
9 unmodified lines101112131415161718 unmodified lines36373839404115 unmodified lines57585960616263646518 unmodified lines8485868788899091929 unmodified linesfrom flask import Blueprint, jsonify, requestfrom .. import functionssearch_bp = Blueprint("search", __name__)18 unmodified linesreturn app_module.kv# ---------------------------------------------------------------------------# POST /agentcache/search# ---------------------------------------------------------------------------15 unmodified linesfolder_path = body.get("folderPath")agent_id = body.get("agentId")res = functions.folder_search(_get_kv(), query, limit, folder_path=folder_path, agent_id=agent_id)return jsonify(res), 200except Exception as e:return jsonify({"error": str(e)}), 40018 unmodified lineslimit = body.get("limit") or 100before = body.get("before")after = body.get("after")result = functions.folder_timeline(_get_kv(), limit, folder_path, agent_id, before, after)return jsonify({"observations": result}), 200except Exception as e:return jsonify({"error": str(e)}), 4009 unmodified lines10111213141518 unmodified lines34353637383940414243444546474849505115 unmodified lines676869707172737475767778798081828318 unmodified lines1021031041051061071081091101111121131141151161171189 unmodified linesfrom flask import Blueprint, jsonify, requestsearch_bp = Blueprint("search", __name__)18 unmodified linesreturn app_module.kvdef _get_search_service():from .. import app as app_modulereturn app_module.search_servicedef _get_observation_store():from .. import app as app_modulereturn app_module.observation_store# ---------------------------------------------------------------------------# POST /agentcache/search# ---------------------------------------------------------------------------15 unmodified linesfolder_path = body.get("folderPath")agent_id = body.get("agentId")search_svc = _get_search_service()if search_svc is not None:res = search_svc.search(query=query,limit=limit,folder_path=folder_path,agent_id=agent_id,kv=_get_kv(),)else:res = []return jsonify(res), 200except Exception as e:return jsonify({"error": str(e)}), 40018 unmodified lineslimit = body.get("limit") or 100before = body.get("before")after = body.get("after")obs_store = _get_observation_store()if obs_store is not None:result = obs_store.timeline(limit=limit,folder_path=folder_path,agent_id=agent_id,before=before,after=after,)else:result = []return jsonify({"observations": result}), 200except Exception as e:return jsonify({"error": str(e)}), 400
521 unmodified lines522523524525526527521 unmodified linesparts = [obs.get("title", ""),obs.get("subtitle", "") or "",obs.get("narrative", "") or ""," ".join(obs.get("facts", []) or [])," ".join(obs.get("concepts", []) or []),521 unmodified lines522523524525526527528521 unmodified linesparts = [obs.get("title", ""),obs.get("subtitle", "") or "",obs.get("text", "") or "",obs.get("narrative", "") or ""," ".join(obs.get("facts", []) or [])," ".join(obs.get("concepts", []) or []),
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778"""src/storage/scopes.py — KV scope registry (A2.3).Copied from src/functions.py — do NOT delete the original (backward compat).The KV class defines all storage scope keys used across agentcache-python."""class KV:# ---- Folder memory scopes (new) ----# Global index of all (folder_path, agent_id) pairs known to the system.# Key = "{safe_folder_path}:{agent_id}", value = FolderIndexEntry dict.folders = "mem:folders"@staticmethoddef folder_obs(folder_path: str, agent_id: str) -> str:"""Per-(folder, agent) observations scope.Key = obs_id, value = FolderObservation dict."""safe_path = folder_path.replace("\\", "/").strip("/")safe_agent = agent_id.strip()return f"mem:folder:{safe_path}:{safe_agent}"@staticmethoddef folder_meta(folder_path: str, agent_id: str) -> str:"""Per-(folder, agent) metadata scope.Key = "meta", value = FolderMeta dict (obsCount, lastUpdated, summary)."""safe_path = folder_path.replace("\\", "/").strip("/")safe_agent = agent_id.strip()return f"mem:foldermeta:{safe_path}:{safe_agent}"@staticmethoddef obs_dedup(folder_path: str, agent_id: str) -> str:"""Deduplication index scope for (folder, agent) pairs.Key = SHA-256 fingerprint hex of normalized text.Value = {"obsId": str, "timestamp": str}"""safe_path = folder_path.replace("\\", "/").strip("/")safe_agent = agent_id.strip()return f"mem:obs_dedup:{safe_path}:{safe_agent}"# ---- Global / shared scopes (kept) ----# Long-term memories — unchanged from previous implementation.memories = "mem:memories"# BM25 index shards — unchanged.bm25Index = "mem:index:bm25"# Audit log — unchanged.audit = "mem:audit"# Graph edges — repurposed for folder graph edges.relations = "mem:relations"# ---- Legacy scopes (read-only; kept for migration and backward compat) ----# Legacy session store — read by migrate_sessions_to_folders() and legacy observe().sessions = "mem:sessions"@staticmethoddef observations(session_id: str) -> str:"""Legacy per-session observations scope.Key = obs_id, value = raw/synthetic observation dict.Read by migrate_sessions_to_folders() and legacy observe()."""return f"mem:obs:{session_id}"# Legacy summary / profile / slot / image-ref scopes retained for legacy code paths.summaries = "mem:summaries"profiles = "mem:profiles"slots = "mem:slots"imageRefs = "mem:image-refs"# Global (cross-project) pinned slots.globalSlots = "mem:global-slots"123456789"""src/storage/scopes.py — KV scope registry (A2.3).Re-exports KV from core.kv_scopes for backward compatibility."""from ..core.kv_scopes import KV__all__ = ["KV"]
13 unmodified lines14151617181920212223242526272 unmodified lines303132333435362 unmodified lines39404142434445464741 unmodified lines8990919293949511 unmodified lines10710810911011111211313 unmodified lines12712812913013113213313413513613713813914014114214314414514614713 unmodified linesimport threadingimport timefrom . import functions# Module-level shutdown flag — set by signal handlers_shutting_down = threading.Event()# Reference to the persistence object (set by start_background_workers)_persistence_ref = None# Reference to the kv object (set by start_background_workers)_kv_ref = None2 unmodified linesSteps:1. Set the global _shutting_down flag to stop background loops.2. Flush the debounce timer and save the index synchronously.3. Run a WAL checkpoint via StateKV.teardown().4. Exit cleanly with code 0."""2 unmodified lines_shutting_down.set()# Flush in-flight persistence debounce timer and save immediatelyglobal _persistence_refif _persistence_ref is not None:try:print("[workers] Flushing index persistence...")_persistence_ref.flush()41 unmodified linesif kv.acquire_lock("auto_forget", lease_seconds=300):try:print("[scheduler] Running auto_forget sweep...")res = functions.auto_forget(kv, dry_run=False)print(f"[scheduler] auto_forget sweep completed: {res}")finally:kv.release_lock("auto_forget")11 unmodified linestry:if kv.acquire_lock("index_rebuild", lease_seconds=600):try:count = functions.rebuild_index(kv)print(f"[persistence] Rebuild completed: indexed {count} items.")finally:kv.release_lock("index_rebuild")13 unmodified lineskv: Initialised StateKV instance.tasks: Optional list of tasks to run ("index", "forget"). Defaults to running both."""global _kv_ref, _persistence_ref_kv_ref = kv# Capture the persistence reference for the shutdown handler_persistence_ref = functions._index_persistence# Register graceful shutdown signal handlers (C5.1)_register_signal_handlers()if tasks is None or "index" in tasks:# Rebuild search index if empty or out of sync (Step 5)index_empty = functions._bm25_index.size == 0index_in_sync = Trueif not index_empty:index_in_sync = functions.verify_index_sync_on_boot(kv)if index_empty or not index_in_sync:reason = "empty" if index_empty else "out of sync"13 unmodified lines14151617181920212223242526272 unmodified lines303132333435362 unmodified lines39404142434445464748495051525341 unmodified lines959697989910010111 unmodified lines11311411511611711811912012112212312412513 unmodified lines13914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117213 unmodified linesimport threadingimport timefrom . import legacy# Module-level shutdown flag — set by signal handlers_shutting_down = threading.Event()_persistence_ref = None_search_svc_ref = None_obs_store_ref = None_kv_ref = None2 unmodified linesSteps:1. Set the global _shutting_down flag to stop background loops.2. Flush the debounce timer and save the index synchronously via SearchService.flush_persist().3. Run a WAL checkpoint via StateKV.teardown().4. Exit cleanly with code 0."""2 unmodified lines_shutting_down.set()global _search_svc_ref, _persistence_refif _search_svc_ref is not None:try:print("[workers] Flushing SearchService persistence...")_search_svc_ref.flush_persist()print("[workers] SearchService persistence flushed.")except Exception as e:print(f"[workers] Error flushing SearchService: {e}")elif _persistence_ref is not None:try:print("[workers] Flushing index persistence...")_persistence_ref.flush()41 unmodified linesif kv.acquire_lock("auto_forget", lease_seconds=300):try:print("[scheduler] Running auto_forget sweep...")res = legacy.auto_forget(kv, dry_run=False)print(f"[scheduler] auto_forget sweep completed: {res}")finally:kv.release_lock("auto_forget")11 unmodified linestry:if kv.acquire_lock("index_rebuild", lease_seconds=600):try:from . import app as app_moduleobs_store = getattr(app_module, "observation_store", None)if obs_store is not None:count = obs_store.rebuild_index()else:count = 0print(f"[persistence] Rebuild completed: indexed {count} items.")finally:kv.release_lock("index_rebuild")13 unmodified lineskv: Initialised StateKV instance.tasks: Optional list of tasks to run ("index", "forget"). Defaults to running both."""global _kv_ref, _persistence_ref, _search_svc_ref, _obs_store_ref_kv_ref = kvfrom . import app as app_modulesearch_svc = getattr(app_module, "search_service", None)obs_store = getattr(app_module, "observation_store", None)_search_svc_ref = search_svc_obs_store_ref = obs_storeif search_svc is not None:_persistence_ref = search_svc._persistenceelse:_persistence_ref = None# Register graceful shutdown signal handlers (C5.1)_register_signal_handlers()if tasks is None or "index" in tasks:# Rebuild search index if empty or out of sync (Step 5)bm25_size = search_svc.bm25_size if search_svc is not None else 0index_empty = bm25_size == 0index_in_sync = Trueif not index_empty:index_in_sync = legacy.verify_index_sync_on_boot(kv, search_service=search_svc)if index_empty or not index_in_sync:reason = "empty" if index_empty else "out of sync"
12# tests package
2 unmodified lines3456782 unmodified lines"""import pytestimport agentcache.app as app_modfrom agentcache.app import create_appfrom agentcache.db import StateKV2 unmodified lines34567892 unmodified lines"""import pytestimport agentcache.app as app_modfrom agentcache.app import create_appfrom agentcache.db import StateKV
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294"""tests/test_api.py — C3.1
Integration tests for REST endpoints using the Flask test client."""
import datetimeimport jsonimport os
import pytest
# ---------------------------------------------------------------------------# Fixtures# ---------------------------------------------------------------------------
@pytest.fixture(scope="module")def flask_app(tmp_path_factory): tmp_dir = tmp_path_factory.mktemp("api_test_db") db_path = str(tmp_dir / "test.db") os.environ.pop("AGENTCACHE_SECRET", None) os.environ.pop("AGENTMEMORY_SECRET", None)
from agentcache.db import StateKV
original_init = StateKV.__init__
def patched_init(self, db_path_arg=None, **kwargs): original_init(self, db_path=db_path, **kwargs)
StateKV.__init__ = patched_init import agentcache.app as app_module
os.environ.pop("AGENTCACHE_SECRET", None) os.environ.pop("AGENTMEMORY_SECRET", None) flask_application = app_module.create_app() StateKV.__init__ = original_init flask_application.config["TESTING"] = True return flask_application
@pytest.fixture(scope="module")def client(flask_app): return flask_app.test_client()
def _now(): return ( datetime.datetime.now(datetime.timezone.utc).isoformat().replace("+00:00", "Z") )
def _post(client, url, payload): return client.post(url, data=json.dumps(payload), content_type="application/json")
# ---------------------------------------------------------------------------# POST /agentcache/agent/observe# ---------------------------------------------------------------------------
class TestAgentObserve: def test_valid_payload_returns_201(self, client): resp = _post( client, "/agentcache/agent/observe", { "folderPath": "/home/user/test-project", "agentId": "kiro", "text": "Implemented new authentication middleware", "timestamp": _now(), }, ) assert resp.status_code == 201 data = resp.get_json() assert "observationId" in data assert data["observationId"].startswith("fobs_")
def test_missing_folder_path_returns_400(self, client): resp = _post( client, "/agentcache/agent/observe", { "agentId": "kiro", "text": "Some work", "timestamp": _now(), }, ) assert resp.status_code == 400
def test_missing_agent_id_returns_400(self, client): resp = _post( client, "/agentcache/agent/observe", { "folderPath": "/home/user/proj", "text": "Some work", "timestamp": _now(), }, ) assert resp.status_code == 400
def test_missing_text_returns_400(self, client): resp = _post( client, "/agentcache/agent/observe", { "folderPath": "/home/user/proj", "agentId": "kiro", "timestamp": _now(), }, ) assert resp.status_code == 400
# ---------------------------------------------------------------------------# POST /agentcache/search# ---------------------------------------------------------------------------
class TestSearch: def test_search_with_query_returns_200(self, client): # Seed data first _post( client, "/agentcache/agent/observe", { "folderPath": "/home/user/search-proj", "agentId": "kiro", "text": "Refactored the authentication system", "timestamp": _now(), }, ) resp = _post(client, "/agentcache/search", {"query": "authentication"}) assert resp.status_code == 200 data = resp.get_json() assert isinstance(data, list) or isinstance(data, dict)
def test_search_missing_query_returns_400(self, client): resp = _post(client, "/agentcache/search", {}) assert resp.status_code == 400
def test_search_empty_query_returns_400(self, client): resp = _post(client, "/agentcache/search", {"query": " "}) assert resp.status_code == 400
# ---------------------------------------------------------------------------# GET /agentcache/folders# ---------------------------------------------------------------------------
class TestFolders: def test_get_folders_returns_200(self, client): # Ensure at least one folder exists from earlier tests _post( client, "/agentcache/agent/observe", { "folderPath": "/home/user/folders-check", "agentId": "kiro", "text": "Check folders endpoint", "timestamp": _now(), }, ) resp = client.get("/agentcache/folders") assert resp.status_code == 200 data = resp.get_json() assert "folders" in data assert isinstance(data["folders"], list)
# ---------------------------------------------------------------------------# GET /agentcache/health# ---------------------------------------------------------------------------
class TestHealth: def test_health_returns_200(self, client): resp = client.get("/agentcache/health") assert resp.status_code == 200 data = resp.get_json() assert "folderCount" in data assert "observationCount" in data assert "memoryCount" in data
def test_health_status_ok(self, client): resp = client.get("/agentcache/health") data = resp.get_json() assert data.get("status") in ("ok", "degraded")
# ---------------------------------------------------------------------------# GET /agentcache/livez# ---------------------------------------------------------------------------
class TestLivez: def test_livez_returns_200_no_auth(self, client): resp = client.get("/agentcache/livez") assert resp.status_code == 200 data = resp.get_json() assert data["status"] == "ok"
def test_livez_open_with_secret_set(self, client): os.environ["AGENTCACHE_SECRET"] = "test-secret-123" try: resp = client.get("/agentcache/livez") assert resp.status_code == 200 finally: del os.environ["AGENTCACHE_SECRET"]
# ---------------------------------------------------------------------------# Authentication# ---------------------------------------------------------------------------
class TestAuthentication: def test_protected_endpoint_returns_401_with_wrong_token(self, client): os.environ["AGENTCACHE_SECRET"] = "correct-secret" try: resp = client.get( "/agentcache/audit", headers={"Authorization": "Bearer wrong-token"}, ) assert resp.status_code == 401 finally: del os.environ["AGENTCACHE_SECRET"]
def test_protected_endpoint_passes_with_correct_token(self, client): secret = "my-test-secret-xyz" os.environ["AGENTCACHE_SECRET"] = secret try: resp = client.get( "/agentcache/audit", headers={"Authorization": f"Bearer {secret}"}, ) assert resp.status_code == 200 finally: del os.environ["AGENTCACHE_SECRET"]
def test_livez_always_open_regardless_of_secret(self, client): os.environ["AGENTCACHE_SECRET"] = "any-secret" try: resp = client.get("/agentcache/livez") assert resp.status_code == 200 finally: del os.environ["AGENTCACHE_SECRET"]
# ---------------------------------------------------------------------------# Additional endpoint smoke tests# ---------------------------------------------------------------------------
class TestMemoriesEndpoint: def test_memories_list_returns_200(self, client): resp = client.get("/agentcache/memories") assert resp.status_code == 200 data = resp.get_json() assert "memories" in data
def test_remember_valid_payload_returns_201(self, client): resp = _post( client, "/agentcache/remember", { "content": "API test memory content", "type": "fact", }, ) assert resp.status_code == 201
def test_forget_nonexistent_id(self, client): resp = _post(client, "/agentcache/forget", {"memoryId": "mem_nonexistent"}) assert resp.status_code == 200
def test_graph_endpoint_returns_200(self, client): resp = client.get("/agentcache/graph") assert resp.status_code == 200 data = resp.get_json() assert "nodes" in data assert "edges" in data
def test_mcp_tools_list_returns_200(self, client): resp = client.get("/agentcache/mcp/tools") assert resp.status_code == 200 data = resp.get_json() assert "tools" in data tool_names = {t["name"] for t in data["tools"]} assert "agent_observe" in tool_names assert "cache_recall" in tool_names2 unmodified lines34567891074 unmodified lines8586878889909112 unmodified lines1041051061071081091101118 unmodified lines1201211221231241251262 unmodified lines1291301311321331341353 unmodified lines1391401411421431441452 unmodified lines"""from flask import Flask, jsonifyfrom agentcache.routes.auth import require_auth, verify_token# ------------------------------------------------------------------------------# Unit Tests — verify_token & require_auth74 unmodified linesheaders = {"Authorization": f"Bearer {secret}"}protected_endpoints = [("POST", "/agentcache/observe", {"folderPath": "src/test", "agentId": "a1", "text": "test"}),("POST", "/agentcache/remember", {"content": "test memory"}),("POST", "/agentcache/search", {"query": "test"}),("POST", "/agentcache/timeline", {}),12 unmodified linesres_unauth = client.get(path)res_auth = client.get(path, headers=headers)assert res_unauth.status_code == 401, f"{method} {path} should require auth (got {res_unauth.status_code})"assert res_auth.status_code in (200, 201), f"{method} {path} failed with valid auth (got {res_auth.status_code})"def test_unprotected_routes_accessible_without_auth(authed_client):8 unmodified linesfor path in unprotected_paths:res = client.get(path)assert res.status_code == 200, f"Unprotected route {path} failed (got {res.status_code})"def test_wrong_token_on_any_blueprint_returns_401(authed_client):2 unmodified linesbad_headers = {"Authorization": "Bearer wrong-token-value"}protected_endpoints = [("POST", "/agentcache/observe", {"folderPath": "src/test", "agentId": "a1", "text": "test"}),("POST", "/agentcache/remember", {"content": "test memory"}),("POST", "/agentcache/search", {"query": "test"}),("POST", "/agentcache/timeline", {}),3 unmodified lines("POST", "/agentcache/migrate", {}),]for method, path, payload in protected_endpoints:if method == "POST":res = client.post(path, json=payload or {}, headers=bad_headers)2 unmodified lines34567891074 unmodified lines858687888990919293949512 unmodified lines1081091101111121131141151161171181198 unmodified lines1281291301311321331341351362 unmodified lines1391401411421431441451461471481493 unmodified lines1531541551561571582 unmodified lines"""from flask import Flask, jsonifyfrom agentcache.routes.auth import require_auth, verify_token# ------------------------------------------------------------------------------# Unit Tests — verify_token & require_auth74 unmodified linesheaders = {"Authorization": f"Bearer {secret}"}protected_endpoints = [("POST","/agentcache/observe",{"folderPath": "src/test", "agentId": "a1", "text": "test"},),("POST", "/agentcache/remember", {"content": "test memory"}),("POST", "/agentcache/search", {"query": "test"}),("POST", "/agentcache/timeline", {}),12 unmodified linesres_unauth = client.get(path)res_auth = client.get(path, headers=headers)assert res_unauth.status_code == 401, (f"{method} {path} should require auth (got {res_unauth.status_code})")assert res_auth.status_code in (200, 201), (f"{method} {path} failed with valid auth (got {res_auth.status_code})")def test_unprotected_routes_accessible_without_auth(authed_client):8 unmodified linesfor path in unprotected_paths:res = client.get(path)assert res.status_code == 200, (f"Unprotected route {path} failed (got {res.status_code})")def test_wrong_token_on_any_blueprint_returns_401(authed_client):2 unmodified linesbad_headers = {"Authorization": "Bearer wrong-token-value"}protected_endpoints = [("POST","/agentcache/observe",{"folderPath": "src/test", "agentId": "a1", "text": "test"},),("POST", "/agentcache/remember", {"content": "test memory"}),("POST", "/agentcache/search", {"query": "test"}),("POST", "/agentcache/timeline", {}),3 unmodified lines("POST", "/agentcache/migrate", {}),]for method, path, payload in protected_endpoints:if method == "POST":res = client.post(path, json=payload or {}, headers=bad_headers)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139"""Unit tests for auto_forget() folder-based and memory-based eviction."""
import datetimeimport os
from agentcache.db import StateKVfrom agentcache.functions import KV, auto_forget, folder_observe, remember
def make_kv(tmp_path): db_path = os.path.join(str(tmp_path), "test.db") return StateKV(db_path=db_path)
def test_auto_forget_memories(tmp_path): kv = make_kv(tmp_path)
# 1. Create a memory that expires in the past past_time = ( (datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=1)) .isoformat() .replace("+00:00", "Z") ) res1 = remember(kv, {"content": "Stale memory", "forgetAfter": past_time}) mem1_id = res1["memory"]["id"]
# 2. Create a memory that expires in the future future_time = ( (datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=5)) .isoformat() .replace("+00:00", "Z") ) res2 = remember(kv, {"content": "Fresh memory", "forgetAfter": future_time}) mem2_id = res2["memory"]["id"]
# Run auto_forget results = auto_forget(kv, dry_run=False) assert len(results["evictedMemories"]) == 1
# Verify mem1 is deleted, mem2 exists assert kv.get(KV.memories, mem1_id) is None assert kv.get(KV.memories, mem2_id) is not None
def test_auto_forget_expired_folder_observations(tmp_path): kv = make_kv(tmp_path) folder = "/home/user/myproject" agent = "kiro"
# 1. Create expired folder observation past_time = ( (datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=1)) .isoformat() .replace("+00:00", "Z") ) res1 = folder_observe( kv, { "folderPath": folder, "agentId": agent, "text": "Stale observation", "timestamp": past_time, "forgetAfter": past_time, }, ) obs1_id = res1["observationId"]
# 2. Create fresh folder observation future_time = ( (datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=1)) .isoformat() .replace("+00:00", "Z") ) res2 = folder_observe( kv, { "folderPath": folder, "agentId": agent, "text": "Fresh observation", "timestamp": past_time, "forgetAfter": future_time, }, ) obs2_id = res2["observationId"]
# Run auto_forget results = auto_forget(kv, dry_run=False) assert len(results["evictedObservations"]) == 1
# Verify eviction fp = "home/user/myproject" assert kv.get(KV.folder_obs(fp, agent), obs1_id) is None assert kv.get(KV.folder_obs(fp, agent), obs2_id) is not None
def test_auto_forget_low_importance_stale_observations(tmp_path): kv = make_kv(tmp_path) folder = "/home/user/myproject" agent = "kiro"
# 1. Create old low-importance folder observation (importance = 1, 200 days old) old_time = ( (datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=200)) .isoformat() .replace("+00:00", "Z") ) res1 = folder_observe( kv, { "folderPath": folder, "agentId": agent, "text": "Stale low value observation", "timestamp": old_time, "importance": 1, }, ) obs1_id = res1["observationId"]
# 2. Create old high-importance folder observation (importance = 8, 200 days old) res2 = folder_observe( kv, { "folderPath": folder, "agentId": agent, "text": "Stale high value observation", "timestamp": old_time, "importance": 8, }, ) obs2_id = res2["observationId"]
# Run auto_forget results = auto_forget(kv, dry_run=False) assert len(results["evictedObservations"]) == 1
# Verify eviction fp = "home/user/myproject" assert kv.get(KV.folder_obs(fp, agent), obs1_id) is None assert kv.get(KV.folder_obs(fp, agent), obs2_id) is not None12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970"""Unit tests for the agentcache context CLI command."""
import argparseimport osfrom unittest.mock import patch
from agentcache.cli import cmd_contextfrom agentcache.db import StateKVfrom agentcache.functions import folder_observe, remember
def make_kv(tmp_path): db_path = os.path.join(str(tmp_path), "test.db") return StateKV(db_path=db_path)
def test_cli_context_generation(tmp_path): kv = make_kv(tmp_path)
# 1. Add observations and memories folder = "/home/user/myproject" agent = "test-agent"
folder_observe( kv, { "folderPath": folder, "agentId": agent, "text": "First observation", "timestamp": "2026-07-15T10:00:00Z", }, )
remember( kv, {"content": "A crucial project rule", "type": "fact", "agentId": agent} )
remember( kv, { "content": "A project-wide memory", "type": "architecture", "project": "myproject", "agentId": "some-other-agent", }, )
output_file = os.path.join(str(tmp_path), "context.md") args = argparse.Namespace(agent=agent, output=output_file, watch=False)
# Mock os.getcwd to match the project path and init_services to return our test db with ( patch("os.getcwd", return_value=folder), patch("agentcache.app.init_services", return_value=(kv, None, None)), ): cmd_context(args)
# 2. Verify file output assert os.path.exists(output_file) with open(output_file, "r", encoding="utf-8") as f: content = f.read()
# Check that metadata and values are written assert "Agent Cache Context" in content assert "Project Metadata" in content assert "myproject" in content assert "test-agent" in content assert "First observation" in content assert "A crucial project rule" in content assert "A project-wide memory" in content123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204"""tests/test_context.py — C1.4
Tests for context(), export_data(), and token budget enforcement."""
import datetimeimport os
import pytest
# ---------------------------------------------------------------------------# Helpers# ---------------------------------------------------------------------------
def _make_kv(tmp_path): from agentcache.db import StateKV
os.environ.pop("AGENTCACHE_SECRET", None) return StateKV(db_path=str(tmp_path / "test.db"))
def _now(): return ( datetime.datetime.now(datetime.timezone.utc).isoformat().replace("+00:00", "Z") )
# ---------------------------------------------------------------------------# context() tests# ---------------------------------------------------------------------------
class TestContext: def test_empty_db_returns_minimal_context(self, tmp_path): """Empty DB should return a well-formed but empty context.""" from agentcache.functions import context
kv = _make_kv(tmp_path) result = context( kv, { "sessionId": "sess_test_empty", "project": "/home/user/my-project", "budget": 2000, }, ) assert isinstance(result, dict) assert "context" in result assert "blocks" in result assert "tokens" in result assert result["blocks"] == 0 assert result["tokens"] == 0
def test_raises_on_missing_session_id(self, tmp_path): from agentcache.functions import context
kv = _make_kv(tmp_path) with pytest.raises(ValueError): context(kv, {"project": "/home/user/proj"})
def test_raises_on_missing_project(self, tmp_path): from agentcache.functions import context
kv = _make_kv(tmp_path) with pytest.raises(ValueError): context(kv, {"sessionId": "sess_x"})
def test_respects_token_budget(self, tmp_path): """Context output tokens should not exceed the requested budget.""" from agentcache.functions import context, lesson_save
kv = _make_kv(tmp_path) project = "/home/user/budget-test"
# Add many lessons to push towards the budget for i in range(20): lesson_save( kv, { "content": f"Lesson {i}: " + ("x " * 100), "project": project, "confidence": 0.9, }, )
budget = 500 result = context( kv, { "sessionId": "sess_budget", "project": project, "budget": budget, }, ) # Token estimate is len/3 — check that total tokens respects budget assert result["tokens"] <= budget + 50 # small headroom for header/footer
def test_context_includes_xml_wrapper(self, tmp_path): """Non-empty context should be wrapped in <agentcache-context>.""" from agentcache.functions import context, lesson_save
kv = _make_kv(tmp_path) project = "/home/user/xml-test"
lesson_save( kv, { "content": "Always validate user input before processing", "project": project, "confidence": 0.8, }, )
result = context( kv, { "sessionId": "sess_xml", "project": project, "budget": 2000, }, )
if result["blocks"] > 0: assert "<agentcache-context" in result["context"] assert "</agentcache-context>" in result["context"]
def test_token_budget_env_var_respected(self, tmp_path, monkeypatch): """TOKEN_BUDGET env var should be used when no budget param given.""" from agentcache.functions import context, lesson_save
kv = _make_kv(tmp_path) project = "/home/user/env-budget" monkeypatch.setenv("TOKEN_BUDGET", "100")
for i in range(10): lesson_save( kv, { "content": f"Important lesson {i}: " + ("word " * 50), "project": project, "confidence": 0.9, }, )
result = context(kv, {"sessionId": "sess_env_budget", "project": project}) # Should use TOKEN_BUDGET=100 from env assert result["tokens"] <= 150 # with some headroom for XML wrapper
# ---------------------------------------------------------------------------# export_data() tests# ---------------------------------------------------------------------------
class TestExportData: def test_export_returns_folders_and_memories(self, tmp_path): from agentcache.functions import export_data, folder_observe, remember
kv = _make_kv(tmp_path)
folder_observe( kv, { "folderPath": "/home/user/export-test", "agentId": "kiro", "text": "Working on export feature", "timestamp": _now(), }, ) remember(kv, {"content": "Export data uses v2 format"})
result = export_data(kv, {}) assert isinstance(result, dict) assert "folders" in result or "observations" in result or "memories" in result
def test_export_empty_db(self, tmp_path): from agentcache.functions import export_data
kv = _make_kv(tmp_path) result = export_data(kv, {}) assert isinstance(result, dict) # Should not crash on empty DB
# ---------------------------------------------------------------------------# estimate_tokens()# ---------------------------------------------------------------------------
class TestEstimateTokens: def test_empty_string(self): from agentcache.functions import estimate_tokens
assert estimate_tokens("") == 0
def test_typical_text(self): from agentcache.functions import estimate_tokens
text = "hello world this is a test" * 10 tokens = estimate_tokens(text) # Should be approximately len/3 assert tokens == len(text) // 3123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166"""A4.3 — Unit tests for IndexPersistence debounce behavior.
Tests:- 100 rapid schedule_save() calls result in exactly 1 save() call.- flush() triggers immediate save without waiting for debounce timer."""
import timeimport unittest.mock as mock
from agentcache.db import StateKVfrom agentcache.functions import IndexPersistencefrom agentcache.search import SearchIndex, VectorIndex
# Speed up debounce for testsFAST_DEBOUNCE = 0.05
def make_kv(tmp_path): return StateKV(db_path=str(tmp_path / "test_debounce.db"))
class TestDebounce: def test_100_rapid_calls_result_in_1_save(self, tmp_path): """100 rapid schedule_save() calls must fire exactly 1 save().""" kv = make_kv(tmp_path) bm25 = SearchIndex() vector = VectorIndex()
persistence = IndexPersistence(kv, bm25, vector) persistence.DEBOUNCE_SECONDS = FAST_DEBOUNCE
save_call_count = [0]
original_save = persistence.save
def counting_save(): save_call_count[0] += 1 original_save()
with mock.patch.object(persistence, "save", side_effect=counting_save): for _ in range(100): persistence.schedule_save() # Wait for the debounce timer to fire (2× debounce window is plenty) time.sleep(FAST_DEBOUNCE * 4)
assert save_call_count[0] == 1, ( f"Expected exactly 1 save() call; got {save_call_count[0]}" )
def test_rapid_calls_with_dirty_bm25(self, tmp_path): """schedule_save() fires exactly once even when BM25 is dirty.""" kv = make_kv(tmp_path) bm25 = SearchIndex() vector = VectorIndex()
# Add a doc so the index is dirty bm25.add( { "id": "obs_test1", "sessionId": "sess1", "title": "hello world", "facts": [], "concepts": [], "files": [], "type": "other", } )
persistence = IndexPersistence(kv, bm25, vector) persistence.DEBOUNCE_SECONDS = FAST_DEBOUNCE
save_call_count = [0] original_save = persistence.save
def counting_save(): save_call_count[0] += 1 original_save()
with mock.patch.object(persistence, "save", side_effect=counting_save): for _ in range(100): persistence.schedule_save() time.sleep(FAST_DEBOUNCE * 4)
assert save_call_count[0] == 1
def test_flush_triggers_immediate_save(self, tmp_path): """flush() must call save() immediately without waiting for the debounce timer.""" kv = make_kv(tmp_path) bm25 = SearchIndex() vector = VectorIndex()
persistence = IndexPersistence(kv, bm25, vector) persistence.DEBOUNCE_SECONDS = 60.0 # very long timer — flush must bypass it
save_call_count = [0] original_save = persistence.save
def counting_save(): save_call_count[0] += 1 original_save()
with mock.patch.object(persistence, "save", side_effect=counting_save): persistence.schedule_save() # Timer is set but hasn't fired yet (60s window) assert save_call_count[0] == 0, "save() should not have been called yet"
# flush() must cancel the timer and call save() synchronously persistence.flush()
assert save_call_count[0] == 1, ( f"flush() should trigger exactly 1 save(); got {save_call_count[0]}" )
def test_flush_after_no_pending_save_is_safe(self, tmp_path): """flush() with no pending timer should still call save() once.""" kv = make_kv(tmp_path) bm25 = SearchIndex() vector = VectorIndex()
persistence = IndexPersistence(kv, bm25, vector)
save_call_count = [0] original_save = persistence.save
def counting_save(): save_call_count[0] += 1 original_save()
with mock.patch.object(persistence, "save", side_effect=counting_save): persistence.flush()
assert save_call_count[0] == 1
def test_subsequent_schedule_after_fire_starts_new_timer(self, tmp_path): """Two bursts of saves separated by more than DEBOUNCE_SECONDS should fire 2 saves.""" kv = make_kv(tmp_path) bm25 = SearchIndex() vector = VectorIndex()
persistence = IndexPersistence(kv, bm25, vector) persistence.DEBOUNCE_SECONDS = FAST_DEBOUNCE
save_call_count = [0] original_save = persistence.save
def counting_save(): save_call_count[0] += 1 original_save()
with mock.patch.object(persistence, "save", side_effect=counting_save): # First burst for _ in range(10): persistence.schedule_save() # Wait for first timer to fire time.sleep(FAST_DEBOUNCE * 4)
# Second burst for _ in range(10): persistence.schedule_save() # Wait for second timer to fire time.sleep(FAST_DEBOUNCE * 4)
assert save_call_count[0] == 2, ( f"Expected 2 save() calls for two separate bursts; got {save_call_count[0]}" )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432"""Unit tests for folderColor() and folder_graph_build() — REQ-023–REQ-028."""
import pytest
from agentcache.db import StateKVfrom agentcache.functions import KV, folder_graph_buildfrom agentcache.functions import folder_color as folderColor
# ---------------------------------------------------------------------------# Fixtures# ---------------------------------------------------------------------------
@pytest.fixture()def kv(tmp_path): """Return a fresh in-file StateKV backed by a temp SQLite database.""" db_file = str(tmp_path / "test.db") return StateKV(db_path=db_file)
def _write_pair( kv: StateKV, folder_path: str, agent_id: str, obs_texts: list = None, obs_count: int = None,) -> None: """Insert a (folder_path, agent_id) entry into KV.folders and optionally write observations.""" obs_texts = obs_texts or [] count = obs_count if obs_count is not None else len(obs_texts)
# Write folders index entry index_key = f"{folder_path}:{agent_id}" kv.set( KV.folders, index_key, { "folderPath": folder_path, "agentId": agent_id, "obsCount": count, "lastUpdated": "2025-01-15T12:00:00.000Z", }, )
# Write observation objects if text supplied for i, text in enumerate(obs_texts): obs_id = f"obs_{folder_path.replace('/', '_')}_{agent_id}_{i}" obs = { "id": obs_id, "folderPath": folder_path, "agentId": agent_id, "timestamp": "2025-01-15T12:00:00.000Z", "text": text, "type": "other", "title": f"title {i}", "concepts": [], "files": [], "importance": 5, } kv.set(KV.folder_obs(folder_path, agent_id), obs_id, obs)
# ---------------------------------------------------------------------------# Tests — folderColor helper# ---------------------------------------------------------------------------
def test_folder_color_returns_hsl_string(): """folderColor should return a string matching hsl(...) format.""" color = folderColor("projects/alpha") assert color.startswith("hsl(") assert color.endswith(")")
def test_folder_color_deterministic(): """Same path always returns the same color.""" assert folderColor("projects/alpha") == folderColor("projects/alpha")
def test_folder_color_different_paths_produce_different_colors(): """Different paths should (almost always) produce different colors.""" # Use very distinct paths to ensure hash difference assert folderColor("projects/alpha") != folderColor( "projects/omega-completely-different" )
def test_folder_color_hsl_values_in_range(): """HSL values should be within expected ranges.""" color = folderColor("some/path") # Strip "hsl(" and ")" then parse inner = color[4:-1] # e.g. "200, 70%, 55%" parts = [p.strip().rstrip("%") for p in inner.split(",")] hue, sat, lig = int(parts[0]), int(parts[1]), int(parts[2]) assert 0 <= hue < 360 assert 55 <= sat <= 79 # 55 + (h % 25) assert 38 <= lig <= 51 # 38 + (h % 14)
def test_folder_color_empty_string(): """folderColor on empty string should not raise.""" color = folderColor("") assert color.startswith("hsl(")
# ---------------------------------------------------------------------------# Tests — empty KV returns empty graph (REQ-023)# ---------------------------------------------------------------------------
def test_empty_kv_returns_empty_graph(kv): """Empty KV returns {nodes: [], edges: []}.""" result = folder_graph_build(kv) assert result == {"nodes": [], "edges": []}
# ---------------------------------------------------------------------------# Tests — node construction (REQ-023, REQ-024)# ---------------------------------------------------------------------------
def test_one_node_per_unique_folder_path(kv): """Two agents in the same folder produce a single node (REQ-023).""" _write_pair(kv, "projects/alpha", "kiro", obs_count=3) _write_pair(kv, "projects/alpha", "claude", obs_count=2)
result = folder_graph_build(kv) assert len(result["nodes"]) == 1 node = result["nodes"][0] assert node["folderPath"] == "projects/alpha"
def test_multiple_folders_produce_multiple_nodes(kv): """Each distinct folder_path produces exactly one node.""" _write_pair(kv, "projects/alpha", "kiro") _write_pair(kv, "projects/beta", "kiro") _write_pair(kv, "projects/gamma", "claude")
result = folder_graph_build(kv) folder_paths = {n["folderPath"] for n in result["nodes"]} assert folder_paths == {"projects/alpha", "projects/beta", "projects/gamma"}
def test_node_fields_present(kv): """Each node contains all required fields (REQ-024).""" _write_pair(kv, "projects/alpha", "kiro", obs_count=5)
result = folder_graph_build(kv) node = result["nodes"][0] assert "id" in node assert "label" in node assert "folderPath" in node assert "agentIds" in node assert "obsCount" in node assert "color" in node
def test_node_id_equals_folder_path(kv): """Node id is the folderPath string.""" _write_pair(kv, "projects/alpha", "kiro")
result = folder_graph_build(kv) node = result["nodes"][0] assert node["id"] == "projects/alpha" assert node["folderPath"] == "projects/alpha"
def test_node_label_is_basename(kv): """Node label is the last path component.""" _write_pair(kv, "home/user/projects/myapp", "kiro")
result = folder_graph_build(kv) node = result["nodes"][0] assert node["label"] == "myapp"
def test_node_agent_ids_aggregated_and_sorted(kv): """agentIds is the sorted union of all agents for that folder.""" _write_pair(kv, "projects/alpha", "zorro", obs_count=1) _write_pair(kv, "projects/alpha", "alice", obs_count=1) _write_pair(kv, "projects/alpha", "bob", obs_count=1)
result = folder_graph_build(kv) node = result["nodes"][0] assert node["agentIds"] == ["alice", "bob", "zorro"]
def test_node_obs_count_summed_across_agents(kv): """obsCount is the sum across all agents for that folder.""" _write_pair(kv, "projects/alpha", "kiro", obs_count=4) _write_pair(kv, "projects/alpha", "claude", obs_count=6)
result = folder_graph_build(kv) node = result["nodes"][0] assert node["obsCount"] == 10
def test_node_color_is_hsl(kv): """Node color comes from folderColor and is an HSL string.""" _write_pair(kv, "projects/alpha", "kiro")
result = folder_graph_build(kv) node = result["nodes"][0] assert node["color"].startswith("hsl(") # Must match folderColor directly assert node["color"] == folderColor("projects/alpha")
# ---------------------------------------------------------------------------# Tests — same-parent edges (REQ-025)# ---------------------------------------------------------------------------
def test_same_parent_edge_created(kv): """Two folders with the same parent get a same-parent edge.""" _write_pair(kv, "projects/alpha", "kiro") _write_pair(kv, "projects/beta", "kiro") # both under "projects"
result = folder_graph_build(kv) same_parent_edges = [e for e in result["edges"] if e["type"] == "same-parent"] assert len(same_parent_edges) == 1 edge = same_parent_edges[0] assert set([edge["source"], edge["target"]]) == {"projects/alpha", "projects/beta"}
def test_no_same_parent_edge_for_different_parents(kv): """Folders with different parents do not get a same-parent edge.""" _write_pair(kv, "projects/alpha", "kiro") _write_pair(kv, "work/beta", "kiro")
result = folder_graph_build(kv) same_parent_edges = [e for e in result["edges"] if e["type"] == "same-parent"] assert same_parent_edges == []
def test_same_parent_edge_only_for_sharing_pairs(kv): """Only pairs sharing a parent get same-parent edges; non-sharing pairs do not.""" _write_pair(kv, "a/x", "kiro") _write_pair(kv, "a/y", "kiro") # shares parent "a" with a/x _write_pair(kv, "b/z", "kiro") # different parent "b"
result = folder_graph_build(kv) same_parent_edges = [e for e in result["edges"] if e["type"] == "same-parent"] assert len(same_parent_edges) == 1 edge = same_parent_edges[0] assert set([edge["source"], edge["target"]]) == {"a/x", "a/y"}
# ---------------------------------------------------------------------------# Tests — cross-reference edges (REQ-026)# ---------------------------------------------------------------------------
def test_cross_ref_edge_when_obs_mentions_other_folder(kv): """A cross-ref edge is created when folder A's obs text mentions folder B's path.""" _write_pair( kv, "projects/alpha", "kiro", obs_texts=["I worked on projects/beta today"] ) _write_pair(kv, "projects/beta", "kiro", obs_texts=["nothing"])
result = folder_graph_build(kv) cross_edges = [e for e in result["edges"] if e["type"] == "cross-ref"] assert len(cross_edges) >= 1 sources_targets = {(e["source"], e["target"]) for e in cross_edges} assert ("projects/alpha", "projects/beta") in sources_targets
def test_no_cross_ref_edge_when_no_mention(kv): """No cross-ref edge when obs texts don't mention another folder path.""" _write_pair(kv, "projects/alpha", "kiro", obs_texts=["Just some work here"]) _write_pair(kv, "projects/beta", "kiro", obs_texts=["Unrelated content"])
result = folder_graph_build(kv) cross_edges = [e for e in result["edges"] if e["type"] == "cross-ref"] assert cross_edges == []
def test_cross_ref_edge_from_title_mention(kv): """Cross-ref edges are also detected via obs titles.""" _write_pair(kv, "projects/alpha", "kiro", obs_texts=["some text"]) # Manually insert obs with a title that mentions the other folder obs = { "id": "obs_special", "folderPath": "projects/alpha", "agentId": "kiro", "timestamp": "2025-01-15T12:00:00.000Z", "text": "normal text", "type": "other", "title": "work on projects/beta", "concepts": [], "files": [], "importance": 5, } kv.set(KV.folder_obs("projects/alpha", "kiro"), "obs_special", obs) _write_pair(kv, "projects/beta", "kiro", obs_texts=["nothing"])
result = folder_graph_build(kv) cross_edges = [e for e in result["edges"] if e["type"] == "cross-ref"] sources = {e["source"] for e in cross_edges} assert "projects/alpha" in sources
# ---------------------------------------------------------------------------# Tests — agent-shared edges (REQ-027)# ---------------------------------------------------------------------------
def test_agent_shared_edge_created(kv): """Two folders with a common agent get an agent-shared edge.""" _write_pair(kv, "projects/alpha", "kiro") _write_pair(kv, "projects/beta", "kiro") # same agent "kiro"
result = folder_graph_build(kv) agent_edges = [e for e in result["edges"] if e["type"] == "agent-shared"] assert len(agent_edges) >= 1 edge = agent_edges[0] assert set([edge["source"], edge["target"]]) == {"projects/alpha", "projects/beta"}
def test_no_agent_shared_edge_when_no_common_agent(kv): """Folders with no common agents do not get an agent-shared edge.""" _write_pair(kv, "projects/alpha", "kiro") _write_pair(kv, "projects/beta", "claude") # different agents
result = folder_graph_build(kv) agent_edges = [e for e in result["edges"] if e["type"] == "agent-shared"] assert agent_edges == []
def test_agent_shared_edge_with_partial_overlap(kv): """Two folders with one common agent among several agents still get an edge.""" _write_pair(kv, "projects/alpha", "kiro") _write_pair(kv, "projects/alpha", "claude") _write_pair(kv, "projects/beta", "claude") _write_pair(kv, "projects/beta", "cursor")
result = folder_graph_build(kv) agent_edges = [e for e in result["edges"] if e["type"] == "agent-shared"] endpoints = {frozenset([e["source"], e["target"]]) for e in agent_edges} assert frozenset({"projects/alpha", "projects/beta"}) in endpoints
# ---------------------------------------------------------------------------# Tests — edge deduplication (REQ-028)# ---------------------------------------------------------------------------
def test_no_duplicate_edges(kv): """No two edges share the same (source, target, type) pair.""" _write_pair(kv, "projects/alpha", "kiro", obs_texts=["mentions projects/beta"]) _write_pair(kv, "projects/beta", "kiro", obs_texts=["mentions projects/alpha"])
result = folder_graph_build(kv) seen = set() for edge in result["edges"]: key = (frozenset([edge["source"], edge["target"]]), edge["type"]) assert key not in seen, f"Duplicate edge: {edge}" seen.add(key)
def test_ab_and_ba_treated_as_same_edge(kv): """(a, b, type) and (b, a, type) are considered the same edge.""" # Both folders reference each other — should produce only one cross-ref edge _write_pair( kv, "projects/alpha", "kiro", obs_texts=["See also projects/beta for details"] ) _write_pair( kv, "projects/beta", "kiro", obs_texts=["Related to projects/alpha work"] )
result = folder_graph_build(kv) cross_edges = [e for e in result["edges"] if e["type"] == "cross-ref"] # Should be exactly 1 cross-ref edge (not 2) assert len(cross_edges) == 1
def test_same_parent_and_agent_shared_are_separate_edge_types(kv): """same-parent and agent-shared edges between the same pair are both kept.""" # Both folders share parent "projects" AND share agent "kiro" _write_pair(kv, "projects/alpha", "kiro") _write_pair(kv, "projects/beta", "kiro")
result = folder_graph_build(kv) edge_types = {e["type"] for e in result["edges"]} # We expect both types to appear assert "same-parent" in edge_types assert "agent-shared" in edge_types
# ---------------------------------------------------------------------------# Tests — return structure# ---------------------------------------------------------------------------
def test_return_has_nodes_and_edges_keys(kv): """Result always has 'nodes' and 'edges' keys.""" _write_pair(kv, "projects/alpha", "kiro") result = folder_graph_build(kv) assert "nodes" in result assert "edges" in result
def test_edge_has_required_fields(kv): """Each edge has source, target, and type fields.""" _write_pair(kv, "projects/alpha", "kiro") _write_pair(kv, "projects/beta", "kiro")
result = folder_graph_build(kv) for edge in result["edges"]: assert "source" in edge assert "target" in edge assert "type" in edge
def test_single_folder_produces_no_edges(kv): """A graph with only one folder produces no edges.""" _write_pair(kv, "projects/alpha", "kiro")
result = folder_graph_build(kv) assert len(result["nodes"]) == 1 assert result["edges"] == []
def test_edge_types_are_valid(kv): """All edge types are one of the three valid values.""" _write_pair(kv, "projects/alpha", "kiro", obs_texts=["mentions projects/beta"]) _write_pair(kv, "projects/beta", "kiro")
result = folder_graph_build(kv) valid_types = {"same-parent", "cross-ref", "agent-shared"} for edge in result["edges"]: assert edge["type"] in valid_types.scratch/arch-improvements/issues/01-appcontext-expand.mdDeleted-16
.scratch/arch-improvements/issues/02-privacy-module.mdDeleted-17
.scratch/arch-improvements/issues/03-auth-middleware.mdDeleted-16
.scratch/arch-improvements/issues/04-mcp-tool-registry.mdDeleted-17
.scratch/arch-improvements/issues/05-split-observations-memories.mdDeleted-23
.scratch/arch-improvements/issues/06-split-indexing-retrieval.mdDeleted-24
.scratch/arch-improvements/issues/07-split-consolidation-slots-lessons.mdDeleted-25
.scratch/arch-improvements/issues/08-delete-functions-shim.mdDeleted-23
.scratch/arch-improvements/issues/09-typed-observations-table-expand.mdDeleted-19
.scratch/arch-improvements/issues/10-typed-observations-reads-contract.mdDeleted-18
.scratch/arch-improvements/issues/11-basevectorindex-protocol.mdDeleted-17
src/agentcache.egg-info/PKG-INFO+2-484
src/agentcache.egg-info/SOURCES.txt+6-1
src/agentcache/__init__.py+6-9
src/agentcache/app.py+36-22
src/agentcache/cache/__init__.py+3-9
src/agentcache/cache/context.py+1-1
src/agentcache/cache/graph.py+1-1
src/agentcache/cache/health.py+1-1
src/agentcache/cache/observe.py+1-1
src/agentcache/cache/remember.py+1-1
src/agentcache/cache/timeline.py+1-1
src/agentcache/cli.py+4-3
src/agentcache/core/__init__.py+7-2
src/agentcache/core/kv_scopes.py-2
src/agentcache/core/observation_store.py+18-15
src/agentcache/core/search_service.py+17-6
src/agentcache/functions.pyDeleted
src/agentcache/legacy.py+12-13
src/agentcache/replay_import.py+4-24
src/agentcache/routes/__init__.py+7-12
src/agentcache/routes/graph.py+1-1
src/agentcache/routes/health.py+1-1
src/agentcache/routes/mcp.py+10-5
src/agentcache/routes/memories.py+1-4
src/agentcache/routes/migration.py+1-1
src/agentcache/routes/observations.py+251-267
src/agentcache/routes/search.py+34-8
src/agentcache/search.py+1
src/agentcache/storage/scopes.py+3-72
src/agentcache/workers.py+39-14
tests/__init__.py-2
tests/conftest.py+1
tests/test_api.pyDeleted-294
tests/test_auth.py+20-7
tests/test_auto_forget.pyDeleted-139
tests/test_cli_context.pyDeleted-70
tests/test_context.pyDeleted-204
tests/test_debounce.pyDeleted-166
tests/test_folder_graph_build.pyDeleted-432