@cerefox/memory 0.4.3 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,235 @@
1
+ # Cerefox Access Paths
2
+
3
+ Cerefox is built in three distinct layers. Understanding them tells you which credentials to
4
+ configure, what can reach the database, and which path is right for your integration.
5
+
6
+ > **A note on Supabase keys (2026):** Cerefox needs two API keys for two different transport
7
+ > layers. Layer 1 (Edge Functions) uses the **legacy anon JWT** as a Bearer token — the new
8
+ > `sb_publishable_…` key is rejected by the Edge Function gateway. Layer 2 (Python REST)
9
+ > uses the new **secret key** (`sb_secret_…`) or the legacy `service_role` JWT — either
10
+ > works. See [`setup-supabase.md` → Supabase API keys (2026)](setup-supabase.md#supabase-api-keys-2026)
11
+ > for the full picture and why this asymmetry exists.
12
+
13
+ ---
14
+
15
+ ## Layer 1 — AI Agents via Edge Functions (HTTPS)
16
+
17
+ This is the primary integration layer for AI clients. Six Supabase Edge Functions are
18
+ deployed on the Supabase platform and are reachable over HTTPS with nothing more than the
19
+ **legacy anon JWT** (a public-facing JWT, `eyJ…`). The Supabase gateway validates the key
20
+ before any request reaches a function; individual functions then use the service-role key
21
+ internally to call Postgres RPCs. Your anon key is never elevated to database-level access.
22
+
23
+ > ⚠️ Use the **legacy anon JWT** here, not the new `sb_publishable_…` key. The Edge
24
+ > Function gateway rejects non-JWT keys with `UNAUTHORIZED_INVALID_JWT_FORMAT`. See
25
+ > [`setup-supabase.md` → Supabase API keys (2026)](setup-supabase.md#supabase-api-keys-2026)
26
+ > for why.
27
+
28
+ ### The six Edge Functions
29
+
30
+ | Edge Function | Role |
31
+ |---|---|
32
+ | `cerefox-search` | Hybrid FTS + semantic search; handles server-side embedding |
33
+ | `cerefox-ingest` | Ingest a document — chunks, embeds, versions, stores |
34
+ | `cerefox-metadata` | List metadata keys with document counts and example values |
35
+ | `cerefox-get-document` | Retrieve full document content (current or archived version) |
36
+ | `cerefox-list-versions` | List the archived version history for a document |
37
+ | `cerefox-mcp` | Streamable HTTP MCP adapter — delegates to all five above |
38
+
39
+ ### How clients connect
40
+
41
+ **MCP clients** (Claude Code, Cursor, Claude Desktop) connect to `cerefox-mcp`. It speaks
42
+ the MCP Streamable HTTP protocol and fans out each tool call to the appropriate primitive
43
+ Edge Function via an internal `fetch()`. The client only ever talks to one URL.
44
+
45
+ ```
46
+ MCP client (anon key)
47
+
48
+
49
+ cerefox-mcp ──▶ cerefox-search
50
+ ──▶ cerefox-ingest
51
+ ──▶ cerefox-metadata
52
+ ──▶ cerefox-get-document
53
+ ──▶ cerefox-list-versions
54
+
55
+ ▼ (service-role key, internal)
56
+ Postgres RPCs
57
+ ```
58
+
59
+ **ChatGPT Custom GPT Actions** call the five primitive Edge Functions directly over HTTPS
60
+ using an OpenAPI schema. `cerefox-mcp` is not involved (ChatGPT does not support the
61
+ Streamable HTTP MCP protocol).
62
+
63
+ **curl / scripts / custom HTTP clients** can also call the primitives directly using the
64
+ same anon key as a Bearer token.
65
+
66
+ ### Credentials needed
67
+
68
+ - `CEREFOX_SUPABASE_URL` — your Supabase project URL
69
+ - **Legacy anon JWT** — found in your Supabase dashboard under **Project Settings → API Keys → Legacy → anon**. (Do not use the new `sb_publishable_…` key — gateway constraint.)
70
+
71
+ See `docs/guides/connect-agents.md` for step-by-step setup per client.
72
+
73
+ ---
74
+
75
+ ## Layer 2 — Python Web App and CLI via Supabase REST
76
+
77
+ The FastAPI web app and all `cerefox` CLI commands (`ingest`, `search`, `reindex`,
78
+ `backup`, etc.) use `CerefoxClient` (`src/cerefox/db/client.py`), a thin wrapper around
79
+ `supabase-py`. This library talks to Supabase over its REST API (PostgREST), but
80
+ authenticates with a **service-role-equivalent key** rather than the anon key — either
81
+ the new **secret key** (`sb_secret_…`) or the legacy `service_role` JWT. Both are
82
+ accepted by the Data API gateway.
83
+
84
+ The service-role key bypasses Supabase Row Level Security (RLS) policies and grants
85
+ unrestricted read and write access. This is intentional — the CLI and web app are trusted,
86
+ local tools that need to insert, update, and delete freely. Keep this key out of any
87
+ public-facing configuration.
88
+
89
+ > **Local coding agents (Claude Code, Codex CLI, opencode, OpenClaw, Hermes, …) also reach
90
+ > Cerefox through this layer**, when the user authorises the agent to invoke `uv run cerefox …`
91
+ > via its Bash tool. This is "Path C" in `connect-agents.md`. The agent runs with the same
92
+ > service-role privileges as the user — same trust assumption as letting the agent edit
93
+ > source code in your repo. See `docs/guides/connect-agents.md` → "Path C — Shell CLI for
94
+ > local coding agents" for the setup and caveats.
95
+
96
+ ```
97
+ Python web app / CLI (service-role key)
98
+
99
+
100
+ Supabase REST API (PostgREST)
101
+
102
+
103
+ Postgres RPCs (same cerefox_* functions called by Edge Functions)
104
+ ```
105
+
106
+ The Python layer calls the same Postgres RPCs as the Edge Functions — the business logic
107
+ lives in one place (Postgres) and is shared across all callers.
108
+
109
+ ### Credentials needed
110
+
111
+ - `CEREFOX_SUPABASE_URL` — your Supabase project URL
112
+ - `CEREFOX_SUPABASE_KEY` — the new **secret key** (`sb_secret_…`) from **Project Settings → API Keys → Secret key**, or the legacy `service_role` JWT from the "Legacy" section of the same panel. **Not** the anon / publishable key.
113
+
114
+ ---
115
+
116
+ ## Layer 3 — Direct Postgres (Deployment Scripts Only)
117
+
118
+ The deployment and migration scripts (`scripts/db_deploy.py`, `scripts/db_migrate.py`,
119
+ `scripts/backup_restore.py`) connect directly to Postgres over TCP using **psycopg2** and
120
+ the database connection string. This is the only path that can run DDL statements (`CREATE
121
+ TABLE`, `CREATE FUNCTION`) — the REST API does not support them.
122
+
123
+ ```
124
+ scripts/db_deploy.py (DB password via DATABASE_URL)
125
+
126
+
127
+ Postgres (direct TCP connection)
128
+ ```
129
+
130
+ No application code — not the web app, not the CLI — uses this path at runtime. It is
131
+ exclusively for schema deployment and data restore operations.
132
+
133
+ ### Credentials needed
134
+
135
+ - `CEREFOX_DATABASE_URL` — the direct Postgres connection string. **Use the Session
136
+ Pooler** (port `5432`) from **Project Settings → Database → Connection pooling**, with
137
+ username `postgres.<project-ref>` and `?sslmode=require` appended. Do not use the
138
+ Transaction Pooler (`6543`) — it does not support DDL. See [`setup-supabase.md` →
139
+ Connection pooling (2026)](setup-supabase.md#connection-pooling-2026) for the full
140
+ reference.
141
+
142
+ ---
143
+
144
+ ## Summary
145
+
146
+ | Caller | Transport | Auth credential | Typical use |
147
+ |---|---|---|---|
148
+ | Claude Code / Cursor | HTTPS → `cerefox-mcp` | Legacy anon JWT | Daily AI assistant access |
149
+ | Claude Desktop | HTTPS → `cerefox-mcp` (via `supergateway`) | Legacy anon JWT | Daily AI assistant access |
150
+ | ChatGPT Custom GPT | HTTPS → primitive Edge Functions | Legacy anon JWT | AI assistant via GPT Actions |
151
+ | curl / HTTP scripts | HTTPS → primitive Edge Functions | Legacy anon JWT | Ad-hoc queries, automation |
152
+ | Python web app | Supabase REST API | Secret key (or legacy service_role) | Web UI backend |
153
+ | `cerefox` CLI (human) | Supabase REST API | Secret key (or legacy service_role) | Ingestion, search, reindex, backup |
154
+ | Local coding agent via `cerefox` CLI | Supabase REST API | Secret key (or legacy service_role) | User-authorised agent (Claude Code, Codex CLI, opencode, OpenClaw, Hermes, …) acting on user's behalf via Bash tool |
155
+ | Deployment scripts | Direct TCP (psycopg2) | DB password | Schema deploy, data restore |
156
+
157
+ ### Key security principle
158
+
159
+ The (legacy) anon JWT is safe to share with AI agents and client applications — it can
160
+ only call the operations exposed by the Edge Functions, and the Supabase gateway
161
+ rate-limits and validates it. The secret key / `service_role` JWT and the database
162
+ password must never be embedded in client-facing configuration or committed to the
163
+ repository.
164
+
165
+ ---
166
+
167
+ ## Destructive operations and the trust model
168
+
169
+ Cerefox classifies write operations into three tiers based on how irreversible they are.
170
+ The access surface for each tier is **not** the same — this asymmetry is a deliberate
171
+ architectural property, not an oversight. Future contributors should read this section
172
+ before "completing" the parity table by adding purge or restore to agent-facing access
173
+ paths.
174
+
175
+ ### The three tiers
176
+
177
+ | Tier | Operations | Reversible? | Where exposed |
178
+ |---|---|---|---|
179
+ | 1. Reads + soft mutations | search, get, list-*, ingest (create/update), metadata-search, get-audit-log | n/a (reads) / yes (versioned) | All paths — MCP, Edge Functions, CLI, web UI |
180
+ | 2. Soft-destructive | `delete_document` (soft delete to trash), `set_review_status` | yes — restorable via web UI | All paths (CLI: `cerefox delete-doc`; web UI; Python; **not** MCP or Edge Functions today) |
181
+ | 3. **Hard-destructive** | `purge_document` (permanent), `restore_document` (un-trash), `set_version_archived` (toggle version retention) | no (purge) / yes (restore, but recovers from a destructive action) | **Web UI only** |
182
+
183
+ ### Why purge / restore are web-UI-only
184
+
185
+ The recovery story behind Cerefox depends on a **human-in-the-loop confirmation step
186
+ before irreversible action.** Soft-delete on its own is not enough — an agent that
187
+ mistakenly soft-deletes a document needs to be unable to silently restore the same
188
+ document later (covering its tracks), and certainly unable to escalate from soft-delete
189
+ to permanent purge.
190
+
191
+ So the access model is:
192
+
193
+ 1. **An agent (via MCP, Edge Function, or CLI) can write or soft-delete freely.** Every
194
+ such operation is recorded in `cerefox_audit_log` with `author`, `author_type`, and
195
+ `created_at`. Soft-deleted documents land in trash and are excluded from search.
196
+ 2. **A human reviews the trash through the Cerefox web UI.** They see the audit history
197
+ for each document, decide whether the agent's action was correct, and either restore
198
+ the document or — only after seeing what they're about to destroy — purge it.
199
+ 3. **Purge is the only operation that frees database storage.** Soft delete keeps every
200
+ version, every chunk, every audit entry. The "I made a mistake; recover this" workflow
201
+ is therefore always possible until the human explicitly chooses purge.
202
+
203
+ A `cerefox purge-doc` CLI command, a `cerefox_purge_document` MCP tool, or a
204
+ `/documents/{id}/purge` HTTP endpoint accessible via the anon JWT would each break this
205
+ property. **Do not add them without a governance design that replaces the human-in-the-
206
+ loop step with an equivalent guard** (e.g. a "purge approval queue" the web UI must
207
+ clear before the operation actually runs).
208
+
209
+ ### What this means for agent operations
210
+
211
+ If you're building tooling that uses the CLI (Path C) or any MCP/Edge Function path:
212
+
213
+ - **Use `cerefox delete-doc` freely** to soft-delete agent-authored content. Pair it
214
+ with `--author <name> --author-type agent` so the audit trail is correct.
215
+ - **Surface the soft-delete to the user.** When your agent decides to delete something,
216
+ tell the user explicitly: "I soft-deleted X (recoverable from the Cerefox trash in
217
+ the web UI)." This gives them the visibility to review and either restore or commit.
218
+ - **Do not attempt to purge or restore from agent code.** There is intentionally no
219
+ programmatic path. If your workflow needs purge / restore, that workflow needs human
220
+ intervention — the design is correct, not incomplete.
221
+
222
+ ### CLI delete-doc — interactive vs scripted
223
+
224
+ `cerefox delete-doc` prompts for confirmation by default (since `click.confirm` requires
225
+ a TTY, an agent's Bash tool will get an abort instead of accidentally deleting). Agents
226
+ that legitimately need to soft-delete must pass `--yes` *and* set `--author` /
227
+ `--author-type` so the audit log captures who acted:
228
+
229
+ ```bash
230
+ cerefox delete-doc <doc-id> --yes \
231
+ --author "claude-code" --author-type "agent"
232
+ ```
233
+
234
+ The success message echoes the resolved values back so the agent can include them in
235
+ its response to the user.
@@ -0,0 +1,163 @@
1
+ # Multi-Agent Coordination via Cerefox
2
+
3
+ This guide describes how to use Cerefox as the shared memory layer for coordinating multiple AI agents, including agents running on different machines, using different models, and managed by different runtimes.
4
+
5
+ > **Status**: this guide describes both patterns that work today and conventions that are proposed but not yet implemented. Proposed conventions are marked accordingly.
6
+ >
7
+ > For the broader vision of Cerefox as an asynchronous coordination layer, see [`docs/research/vision.md`](../../docs/research/vision.md).
8
+
9
+ ---
10
+
11
+ ## The Problem
12
+
13
+ Modern AI workflows increasingly involve multiple agents collaborating on a task. Common scenarios include:
14
+
15
+ - **Cross-machine collaboration**: Claude Code agents on one machine and OpenAI Codex agents on another, both working on related codebases
16
+ - **Cross-vendor workflows**: a research agent using one model produces findings that a coding agent using a different model needs to act on
17
+ - **Sequential sessions**: an agent writes context in session A that a different agent needs in session B, hours or days later
18
+ - **Specialized agent teams**: planning agents, coding agents, writing agents, and review agents each handling a phase of a larger workflow
19
+
20
+ Within a single runtime (e.g., Claude Code's agent teams feature, or a LangGraph pipeline), agents can coordinate through in-memory state and direct message passing. But **cross-runtime, cross-machine coordination** has no standard solution.
21
+
22
+ ## How Cerefox Helps
23
+
24
+ Cerefox sits in a unique position: it is vendor-neutral, protocol-native (MCP + REST), and designed for persistent storage. Any agent that can make an HTTP call can read and write to Cerefox.
25
+
26
+ The coordination model is **asynchronous and knowledge-based**:
27
+
28
+ 1. **Agent A writes** a finding, decision, or task breakdown to Cerefox. It does not need to know which agent will consume it.
29
+ 2. **Agent B, starting a new session** (possibly hours later, on a different machine), searches Cerefox and discovers the relevant context. It does not need to know which agent produced it.
30
+ 3. **The human monitors** the knowledge base and intervenes when needed, correcting errors or resolving conflicts.
31
+
32
+ This is not real-time orchestration. It is persistent, searchable shared memory.
33
+
34
+ ---
35
+
36
+ ## Coordination Patterns
37
+
38
+ ### Pattern 1: Implicit Coordination (works today)
39
+
40
+ Agents influence each other through the shared knowledge base without any explicit signaling.
41
+
42
+ **Example**: A research agent writes a document summarizing API design patterns. Weeks later, a coding agent is asked to design a new API. It searches Cerefox, finds the research summary, and uses it to inform its design.
43
+
44
+ **How it works**: No special metadata or conventions needed. This is the default behavior of any agent that searches Cerefox for context before starting work.
45
+
46
+ **Best for**: Organic knowledge sharing, building institutional memory, serendipitous discovery.
47
+
48
+ ### Pattern 2: Decision Logs (works today)
49
+
50
+ A living document where agents record decisions, experiment outcomes, and lessons learned. Future sessions load the log and benefit from accumulated institutional memory.
51
+
52
+ **Example**: A coding agent working on a project records "Chose PostgreSQL RPC approach over application-level logic because..." in a decision log document. Next week, a different agent working on a related feature searches Cerefox, finds the decision log, and understands the rationale without re-deriving it.
53
+
54
+ **How it works**: Create a document with a structured format (date, context, decision, outcome). Use a consistent title or project tag so agents can find it. Use `update_if_exists: true` to append new entries.
55
+
56
+ **Best for**: Project-level institutional memory, avoiding repeated decisions, onboarding new agent sessions.
57
+
58
+ ### Pattern 3: Session Handoffs (convention proposed)
59
+
60
+ When one agent session ends and another needs to continue the work, a structured handoff document captures the current state.
61
+
62
+ **Suggested handoff document structure**:
63
+
64
+ ```markdown
65
+ # Session Handoff: [Project/Task Name]
66
+
67
+ ## Date
68
+ YYYY-MM-DD
69
+
70
+ ## State of Play
71
+ [What has been accomplished so far]
72
+
73
+ ## Outstanding Tasks
74
+ - [ ] Task 1
75
+ - [ ] Task 2
76
+
77
+ ## Decisions Made
78
+ - Decision 1: [rationale]
79
+ - Decision 2: [rationale]
80
+
81
+ ## Open Questions
82
+ - Question 1
83
+ - Question 2
84
+
85
+ ## Key Files / References
86
+ - [list of relevant files, documents, or links]
87
+ ```
88
+
89
+ **How it works**: The ending session writes a handoff document to Cerefox. The next session (same or different agent) searches for recent handoff documents in the relevant project.
90
+
91
+ **Best for**: Continuing work across sessions, transferring context between different agents or models.
92
+
93
+ ### Pattern 4: Structured Metadata for Coordination (convention proposed)
94
+
95
+ Using metadata fields to signal document status and intended audience, so agents can filter for relevant coordination artifacts.
96
+
97
+ **Proposed metadata conventions**:
98
+
99
+ | Field | Values | Purpose |
100
+ |-------|--------|---------|
101
+ | `coordination_status` | `draft`, `active`, `superseded` | Lifecycle of coordination documents |
102
+ | `intended_audience` | `coding-agents`, `research-agents`, `all` | Who should pick this up |
103
+ | `handoff_from` | agent name/model | Which agent produced this |
104
+ | `handoff_to` | agent name/model or `any` | Which agent should consume this |
105
+
106
+ **How it works**: Agents write documents with these metadata fields. Other agents use metadata-filtered search to find relevant coordination artifacts (e.g., "show me all active documents intended for coding-agents").
107
+
108
+ **Best for**: Larger workflows with multiple specialized agents, explicit task delegation.
109
+
110
+ ### Pattern 5: Temporal Catch-Up (capability proposed)
111
+
112
+ An agent starting a new session queries for everything that changed since its last session.
113
+
114
+ **Proposed query pattern**: "Show me all documents created or updated since timestamp X, optionally filtered by project."
115
+
116
+ **How it works**: The agent records its session start timestamp. At the beginning of the next session, it queries for documents modified after that timestamp. This gives it a complete picture of what other agents have done in the interim.
117
+
118
+ **Best for**: Agents that work on a shared project intermittently, catching up after gaps.
119
+
120
+ ---
121
+
122
+ ## Example: Cross-Machine Agent Teams
123
+
124
+ A real-world setup where Cerefox coordinates agents across machines:
125
+
126
+ **Machine A** runs Claude Code agents working on a Python backend. They use Cerefox (via MCP) to:
127
+ - Store architectural decisions in a decision log
128
+ - Write task completion summaries
129
+ - Record API contracts and interface definitions
130
+
131
+ **Machine B** runs OpenAI Codex agents working on a TypeScript frontend. They use Cerefox (via REST API) to:
132
+ - Search for the latest API contracts written by the backend agents
133
+ - Find architectural decisions that affect the frontend
134
+ - Write their own implementation notes and decisions
135
+
136
+ **The human** periodically reviews the knowledge base through the web UI, resolving any conflicts and validating that the two agent groups are aligned.
137
+
138
+ No direct communication channel exists between the agent groups. Cerefox is the shared memory that ties them together.
139
+
140
+ ---
141
+
142
+ ## Tips for Effective Multi-Agent Coordination
143
+
144
+ 1. **Use projects to scope coordination**: assign all documents related to a shared workflow to the same Cerefox project. This makes project-filtered search the natural way for agents to find relevant context.
145
+
146
+ 2. **Write for discovery, not for a specific recipient**: when an agent writes to Cerefox, it should assume the reader has no prior context. Include enough background that any agent (or human) can understand the document without knowing who wrote it or when.
147
+
148
+ 3. **Use descriptive titles**: agents discover documents through search. A title like "API Contract: User Authentication Endpoints v2" is far more discoverable than "Notes from session 47."
149
+
150
+ 4. **Timestamp your entries**: especially in decision logs and handoff documents, include dates so readers can understand the chronological order of events.
151
+
152
+ 5. **Let metadata carry the signals**: use metadata fields for status, audience, and coordination signals rather than embedding them in document content. This enables filtered search.
153
+
154
+ ---
155
+
156
+ ## What's Next
157
+
158
+ This guide will be updated as coordination conventions are formalized and implemented. Planned additions:
159
+
160
+ - Temporal query API (search by modification date range)
161
+ - Recommended metadata schema for coordination
162
+ - Handoff document template as a first-class Cerefox feature
163
+ - Best practices refined from real-world multi-agent workflows