Skip to content

Persistent database — replace in memory stores#357

Open
clintjeff2 wants to merge 4 commits into
Bitcoindefi:mainfrom
clintjeff2:Persistent-database-—-replace-in-memory-stores

Hidden character warning

The head ref may contain hidden characters: "Persistent-database-\u2014-replace-in-memory-stores"
Open

Persistent database — replace in memory stores#357
clintjeff2 wants to merge 4 commits into
Bitcoindefi:mainfrom
clintjeff2:Persistent-database-—-replace-in-memory-stores

Conversation

@clintjeff2

Copy link
Copy Markdown
Contributor

Motivation

  • Replace volatile in-memory stores with optional persistent backends (Postgres + KV) so agent state, x402 receipts and reputation survive cold starts and can scale.
  • Keep existing file/ memory behavior as a fallback and avoid forcing DB/KV at test time or when env vars are not provided.

Description

  • Added a Prisma schema (prisma/schema.prisma) for Agent, X402Receipt, and ReputationEntry tables and added prisma/@prisma/client/Vercel client deps in package.json.
  • Introduced dynamic Postgres and KV adapters in lib/db/postgres.ts and lib/db/kv.ts which only enable when env vars are present and skip during tests; they dynamically import the Vercel SDKs to avoid hard failures when not configured.
  • Updated x402 storage to provide async Postgres-backed operations with a file-based fallback in lib/protocols/x402-receipt-store.ts and exposed save/get/list async variants; wired async flow into lib/protocols/x402.ts and the settle/receipts routes.
  • Added DB-backed reputation helpers in lib/reputation/reputation-store.ts (getReputationAsync, applyReputationActionAsync, listReputationsAsync) that write/read ReputationEntry rows when configured and kept in-memory snapshot behavior as fallback.
  • Added KV-backed async task queue helpers in lib/agent-runtime/task-queue.ts and wired async enqueue/get/list into app/api/tasks routes while preserving the in-memory queue fallback.
  • Added persistent agent state store and a new API endpoint at app/api/agents/state implemented in lib/agents/agent-state-store.ts to read/upsert agents via Postgres with seeded in-memory data when DB is not configured.

Testing

  • Ran npm test -- lib/protocols/x402.test.ts lib/reputation/reputation-store.test.ts and the targeted tests passed.
  • Ran npm test -- __tests__/api/tasks.test.ts __tests__/api/protocol/x402-receipts.test.ts and those API-level tests passed after wiring async helpers.
  • Ran the full test suite with npm test; some pre-existing failures remain (tests around task-drain max caps and a few unrelated expectations) and are not caused by the DB/KV changes.
  • Ran npx tsc --noEmit; TypeScript reported unrelated type errors in areas outside this change (task-drain typings and a missing connector type), not blocking the new adapters.
  • Attempted npm run build; build was blocked by environment network failures fetching Google Fonts for next/font in this environment (not related to DB/KV changes).

Closes #25

clintjeff2 and others added 4 commits June 27, 2026 08:18
…ercel-postgres

Add optional Postgres (Prisma) + Vercel KV persistence for agents, receipts, reputation and task queue
- Fix TypeScript error in drain route by adding required 'result' property to 'task.completed' event.
- Fix TypeScript errors in task-drain tests by correctly awaiting async 'drainAgentTasks' calls.
- Synchronize package-lock.json with package.json to fix 'npm ci' failures.
- Increase 'MAX_PENDING_PER_AGENT' to 300 to support existing test cases.

Co-authored-by: clintjeff2 <119521983+clintjeff2@users.noreply.github.com>
…e-9749460999197247886

Fix TypeScript errors and synchronize lockfile
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Persistent database — replace in-memory stores with Vercel Postgres + KV

1 participant