Ai brief
Build shared-agent-memory in JavaScript ESM on Node >=22. Type-check with tsc --noEmit --checkJs. Use sqlite-vec as the only runtime dependency and TypeScript as the only devDependency. Ship reviewed source with no build step.
Profiles choose documented files. Read them without modifying them. Fail closed on unreadable sources. Keep keys in the environment. Screen every path and chunk before embedding. Use SQLite transactions, per-device inventories and acknowledged fingerprints. Filter privacy before cosine ranking. Frame hits as stored data. Expose memory_search over MCP stdio. Setup owns only files in its manifest. Uninstall keeps edited files.
Step contracts
- Acquire the collector lock. Rules: Only one collector owns the state; never steal a live lock. Failure: Stop without changing state. Source: src/collector/01-lock.js.
- Load settings and keys from the environment. Rules: Never persist or print keys; reject matching keys. Failure: Stop with a safe configuration error. Source: src/collector/02-load-config.js.
- Discover memory through source profiles. Rules: Overrides win; transcripts default off. Failure: Stop on invalid profiles or unreadable source roots. Source: src/collector/03-discover.js.
- Read source files without modifying them. Rules: Skip binaries and symlinks; abort on unreadable inputs. Failure: Preserve state and do not send an inventory. Source: src/collector/04-read.js.
- Normalize encoding and frontmatter. Rules: Decode UTF-8 or BOM-marked UTF-16; remove only leading frontmatter. Failure: Reject invalid encoding. Source: src/collector/05-normalize.js.
- Split text at headings or paragraphs. Rules: Target 1500 characters; preserve all non-frontmatter text. Failure: Stop on invalid text. Source: src/collector/06-chunk.js.
- Fingerprint each source file. Rules: Hash original bytes; distinguish path provenance. Failure: Stop on unreadable bytes. Source: src/collector/07-fingerprint.js.
- Find files changed since acknowledgment. Rules: Do not advance fingerprints before acknowledgment. Failure: Treat missing state as a first run. Source: src/collector/08-diff.js.
- Build the full inventory for every AI. Rules: Include unchanged files; never infer deletion from a read failure. Failure: Refuse an incomplete inventory. Source: src/collector/09-inventory.js.
- Rebuild a source missing from the store. Rules: Resend all files for a remembered source whose store is empty. Failure: Stop if server status cannot be checked. Source: src/collector/10-reconcile.js.
- Pack batches within the byte and chunk caps. Rules: Keep each file atomic; every batch carries full inventory. Failure: Reject a file or inventory too large for a batch. Source: src/collector/11-pack.js.
- Send batches with the write key. Rules: Use bearer headers; never put a key in a URL; timeout requests. Failure: Keep unacknowledged fingerprints unchanged. Source: src/collector/12-send.js.
- Save only acknowledged fingerprints. Rules: A rejected file must retry next time. Failure: Preserve the previous state on write failure. Source: src/collector/13-save.js.
- Log counts only. Rules: Never include memory text, source paths or keys. Failure: Print a safe error code. Source: src/collector/14-log.js.
- Check the write key. Rules: Use timing-safe comparison; require distinct read and write keys. Failure: Return unauthorized. Source: src/server/15-authorize-write.js.
- Validate the batch schema and limits. Rules: Reject unknown fields, oversized payloads and mismatched inventory. Failure: Refuse the whole batch. Source: src/server/16-validate.js.
- Scan every chunk and path for secrets. Rules: Unavailable scanner refuses all writes; a finding refuses the whole batch. Failure: Return a safe scanner or secret error. Source: src/server/17-scan.js.
- Guard empty inventories and mass deletion. Rules: Refuse empty inventory; refuse deletion of more than half and more than 20 rows. Failure: Keep all stored rows. Source: src/server/18-guard.js.
- Dedupe exact chunk copies. Rules: Use exact SHA-256 identity. Failure: Abort before transaction. Source: src/server/19-dedupe.js.
- Embed new chunks. Rules: Require finite normalized vectors with 768 dimensions. Failure: Write nothing on embedder failure. Source: src/server/20-embed.js.
- Link copies held by different AIs. Rules: Keep device, AI, path and chunk ordinal. Failure: Abort the transaction on invalid links. Source: src/server/21-link.js.
- Apply privacy tags. Rules: Every matched tag must allow the querying AI. Failure: Reject invalid privacy configuration. Source: src/server/22-privacy.js.
- Write the batch in one transaction. Rules: Rollback every write if any statement fails. Failure: Keep the previous store intact. Source: src/server/23-write.js.
- Remove files missing from the inventory. Rules: Touch only the current device and selected AI inventories. Failure: Rollback on failure. Source: src/server/24-remove.js.
- Reply with acknowledged files. Rules: Acknowledge only after commit. Failure: Client retries safely. Source: src/server/25-ack.js.
- Check the read key. Rules: Use timing-safe comparison. Failure: Return unauthorized. Source: src/search/26-authorize-read.js.
- Embed the search query. Rules: Use the same embedder identity as stored vectors. Failure: Return a safe embedding error. Source: src/search/27-embed-query.js.
- Filter by scope and privacy tags. Rules: Default to own scope; every tag must permit the querying AI. Failure: Return no hidden candidates. Source: src/search/28-filter.js.
- Rank by cosine distance. Rules: Filter before ranking; bound k. Failure: Return an empty result for an empty store. Source: src/search/29-rank.js.
- Frame each hit as stored data. Rules: Mark text as untrusted stored data, never instructions. Failure: Return a safe response error. Source: src/search/30-frame.js.
- Measure freshness for each device. Rules: Use committed acknowledgments only. Failure: Report unknown devices as stale. Source: src/ops/31-freshness.js.
- Report stale or unavailable collection. Rules: Never print source content or keys. Failure: Exit with a failing health result. Source: src/ops/32-alert.js.
- Rebuild the full store safely. Rules: Preview counts; preserve the old store until the replacement is complete. Failure: Keep the previous store on failure. Source: src/ops/33-rebuild.js.
Verification
Run npm run typecheck, npm test and npm run docs:check. Use synthetic homes, the hash embedder and the builtin scanner. Prove secret refusal, deletion guards and edited-file preservation with RED then GREEN tests.