|
1 | 1 | # CLAUDE.md |
2 | 2 |
|
3 | | -This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
4 | | - |
5 | | -## What This Repo Is |
6 | | - |
7 | | -A **Solana × CoralOS starter kit for agents that earn** — a forkable hackathon template for autonomous |
8 | | -services that get paid on-chain. The full devnet loop is already wired — |
9 | | -**WANT → BID → AWARD → DEPOSITED → DELIVERED → RELEASED**: an LLM agent sells a service, buyers reason |
10 | | -about value, sellers compete on price/quality, funds lock in a Solana escrow, delivery triggers release, |
11 | | -no-shows refund. A builder's job is **one function** — replace `deliverService()` |
12 | | -([`examples/txodds/agent/service.ts`](examples/txodds/agent/service.ts)) with something an agent can |
13 | | -sell — plus the seller persona and the buyer's value criteria. |
14 | | - |
15 | | -The **World Cup oracle is the default demo, not the product**: it sells a verified, de-margined odds |
16 | | -read to prove the rails end-to-end. Frame work around outcomes (a freelancer / research / broker / |
17 | | -oracle / reseller agent), not the sports data. |
18 | | - |
19 | | -The stack is pure TypeScript end-to-end; the **only Rust is the escrow + arbiter Anchor programs**, the |
20 | | -**settlement spine** (not optional, already deployed to devnet). Two ways to see it: `npm run dev` brings |
21 | | -up the single-agent demo (data/escrow proxy + React UI, **no Docker**); the full market — |
22 | | -`docker compose up -d coral` + `bash build-agents.sh` + `cd examples/marketplace && npm start` — runs |
23 | | -competing buyer/seller agents over coral-server (MCP), settling via the escrow on devnet. The kit's LLM |
24 | | -is **Venice AI** (free credits; `LLM_PROVIDER` also accepts `openai`/`anthropic` — see `LLM.md`). |
25 | | - |
26 | | -## Repo Layout |
27 | | - |
28 | | -| Directory | Purpose | |
29 | | -|-----------|---------| |
30 | | -| `examples/txodds/` | The World Cup Oracle (the headline example). `agent/` (`edge.ts` — the verified-odds→LLM-call transform; `service.ts` — the `deliverService` fork point; `escrow.ts` — the buyer-side escrow client), `server/` (`mint.ts`, `proxy.ts` — the live data + escrow proxy), `web/` (the no-build React app), `research/` (`watcher.ts` + `detect.ts` — the odds-move event watcher feeding the research round; `npm run watch`), `escrow/` (the Anchor escrow + arbiter programs — the settlement spine). | |
31 | | -| `examples/agent-economy/` | **Three front doors** on CoralOS (needs Docker): `autonomous/` (agent→agent purchase), `bridge/` (HTTP bridge + React checkout — the human front door), `quickstart/` (bare 402 pay-per-call, no Docker/CoralOS), `web/` (3-tab dashboard), `config/coral.toml`. Escrow references point at `examples/txodds/escrow/`. | |
32 | | -| `examples/txodds-agent-desk/` | The **operator console**: a Tauri v2 desktop shell (`src-tauri/`, ~10 lines of Rust, no IPC/keys) around a no-build static UI (`ui/`) that consumes the txodds proxy (:8801 — runs, proof receipts, settle, Pay.sh procurement), the marketplace feed (:4000, optional), and the watcher (:4600, optional). Browser mode: `npm run desk` (serves `ui/` on :3030, no Rust toolchain needed); desktop: `npm run desk:app` (needs Rust + WebView2). | |
33 | | -| `examples/marketplace/` | **Competitive bidding market, three rounds** (needs Docker): `start.ts` (classic buyer + persona sellers), `freelancer.ts` (**verifier-gated**: heterogeneous harnesses — `seller-scribe` node-llm vs `seller-claude` Claude Code — bid on a brief; `verifier-agent` gates the release), `research.ts` (**event-driven**: the buyer polls the txodds watcher via `WANT_FEED_URL` — quiet board, no spend; needs the txodds proxy + `npm run watch` there); `feed/` (folds session state into rounds AND persists each round to the **run ledger** `runs/`; serves `/api/runs` + `/api/reputation`; replays a session from disk when coral is down), `web/` (React visualizer + Playwright tests). Settles via the escrow. | |
34 | | -| `packages/agent-runtime/` | The runtime, one folder each under `src/`: the LLM provider shim (`llm/`), Solana Pay + devnet guard (`solana/`), a CoralOS MCP client (`coral/`), the market protocol incl. VERIFY/VERIFIED (`market/`), the **run ledger + reputation** (`ledger/`), and the **policy choke point** (`policy/` — spend caps, payout/award-price binding, verifier gate). Root `src/index.ts` re-exports all of them. | |
35 | | -| `packages/harness-runtime/` | The **harness adapter SDK**: one `HarnessAdapter` interface (`quote`/`run`, hash-bound deliveries, streamed events) so a seller can be a prompt (`node-llm`), headless Claude Code (`claude-code`, Coral MCP config injection), or any CLI (`HARNESS=cli HARNESS_CMD='hermes {prompt}'`). Harness processes never hold keys. Build after agent-runtime. | |
36 | | -| `packages/payment-runtime/` | The **payment rail router**: one `PaymentRail` interface + `PaymentRailRouter` + allowance/merchant/procurement policies. **Honest split** (status table in its README): Solana Pay + escrow are live devnet rails; Pay.sh, x402, USDC, allowance, embedded-wallet, payout are typed scaffolds. First consumers: the txodds Pay.sh procurement demo (`/api/pay-sh-edge`) and the seller-agent's upstream procurement. Build after agent-runtime. | |
37 | | -| `coral-agents/` | The agents coral-server launches per session: `buyer-agent` (policy-enforced, verifier-gated, event-mode capable), `seller-agent` (harness-adapter seller; personas `seller-worldcup`/`seller-scribe`/`seller-claude`/`seller-moves`/`seller-stats` reuse its image), `verifier-agent` (independent delivery checks — release gate), plus `broker/` (swarm — buys upstream, resells at a markup, escrow both legs), `echo-agent/` (minimal test agent), `user_proxy/` (the human's puppet for the bridge). | |
38 | | -| `scripts/` | `txodds.js` (the `npm run dev` launcher — proxy + web + browser) and `setup.js` (devnet wallet generation → `.env`). | |
39 | | - |
40 | | -## Commands |
41 | | - |
42 | | -### Run the demo |
| 3 | +This file gives Claude Code repository context for local development. |
| 4 | + |
| 5 | +## Repository Purpose |
| 6 | + |
| 7 | +The repository implements a devnet agent-commerce reference system. Agents coordinate through CoralOS, exchange typed market messages, and settle through Solana payment rails. The default service reads TxODDS TxLINE football data, but the runtime and protocol are service-agnostic. |
| 8 | + |
| 9 | +Primary lifecycle: |
| 10 | + |
| 11 | +```text |
| 12 | +WANT -> BID -> AWARD -> ESCROW_REQUIRED -> DEPOSITED -> DELIVERED -> VERIFIED -> RELEASED |
| 13 | +``` |
| 14 | + |
| 15 | +The Rust surface is limited to `examples/txodds/escrow`, which contains the escrow and arbiter programs. Most repository code is TypeScript. |
| 16 | + |
| 17 | +## Layout |
| 18 | + |
| 19 | +| Path | Purpose | |
| 20 | +|---|---| |
| 21 | +| `packages/agent-runtime/` | LLM shim, Coral MCP client, Solana guard/helpers, market protocol, run ledger, reputation, and policy. | |
| 22 | +| `packages/harness-runtime/` | Seller execution adapter SDK for `node-llm`, `claude-code`, and arbitrary CLI harnesses. | |
| 23 | +| `packages/payment-runtime/` | Rail interface/router, working devnet Solana Pay and escrow rails, scaffold rails, and proof receipts. | |
| 24 | +| `packages/solana-agent-tools/` | Read-only Solana context tools and optional Solana Agent Kit adapter. | |
| 25 | +| `examples/txodds/` | TxODDS proxy, web UI, service implementation, research watcher, and escrow workspace. | |
| 26 | +| `examples/marketplace/` | CoralOS market launchers, feed server, React visualizer, and ledger persistence. | |
| 27 | +| `examples/agent-economy/` | Autonomous purchase, checkout bridge, HTTP 402 quickstart, and dashboard. | |
| 28 | +| `examples/txodds-agent-desk/` | Browser/Tauri operator UI over proxy, ledger, receipts, reputation, and watcher data. | |
| 29 | +| `coral-agents/` | Buyer, seller, verifier, broker, echo, and user-proxy agent containers. | |
| 30 | +| `scripts/` | Setup, wallet provisioning, example runner, and readiness e2e scripts. | |
| 31 | + |
| 32 | +## Common Commands |
43 | 33 |
|
44 | 34 | ```sh |
45 | | -npm run dev # = node scripts/txodds.js — proxy (:8801) + Oracle UI (:3020) + browser |
46 | | -node scripts/setup.js # generate devnet wallets → .env (fund the buyer at faucet.solana.com) |
| 35 | +npm run setup |
| 36 | +npm run dev |
| 37 | +npm run readiness:e2e |
47 | 38 | ``` |
48 | 39 |
|
49 | | -By hand (what `npm run dev` automates), from `examples/txodds/`: |
| 40 | +Runtime package checks: |
50 | 41 |
|
51 | 42 | ```sh |
52 | | -npm run proxy # tsx server/proxy.ts — live TxODDS data + escrow settle on :8801 |
53 | | -npm run web # serve web -l 3020 — the Oracle UI |
54 | | -npm run mint # mint a fresh TxLINE free-tier token into .env (optional) |
| 43 | +cd packages/agent-runtime && npm install && npm run typecheck && npm test && npm run build |
| 44 | +cd packages/harness-runtime && npm install && npm run typecheck && npm test && npm run build |
| 45 | +cd packages/payment-runtime && npm install && npm run typecheck && npm test && npm run build |
55 | 46 | ``` |
56 | 47 |
|
57 | | -### packages/agent-runtime (the runtime) |
| 48 | +TxODDS checks: |
58 | 49 |
|
59 | 50 | ```sh |
60 | | -cd packages/agent-runtime && npm install |
61 | | -cd packages/agent-runtime && npm run typecheck |
62 | | -cd packages/agent-runtime && npm test |
63 | | -cd packages/agent-runtime && npm run build # dependents (examples/txodds, harness-runtime, agents) need its dist |
64 | | -cd packages/harness-runtime && npm run build # after agent-runtime; the seller needs its dist |
| 51 | +cd examples/txodds && npm install && npm run typecheck && npm test |
65 | 52 | ``` |
66 | 53 |
|
67 | | -### Typecheck / test the example |
| 54 | +Marketplace feed/web checks: |
68 | 55 |
|
69 | 56 | ```sh |
70 | | -cd examples/txodds && npm install && npm run typecheck && npm test # incl. edge.test.ts |
| 57 | +cd examples/marketplace/feed && npm install && npm test |
| 58 | +cd examples/marketplace/web && npm install && npm test && npm run e2e |
71 | 59 | ``` |
72 | 60 |
|
73 | | -## Architecture |
74 | | - |
75 | | -### packages/agent-runtime — the runtime |
76 | | - |
77 | | -- **LLM** (`llm/`) — `complete.ts` (`complete()` — SDK-free `fetch` shim; **Venice AI** is the kit's LLM, |
78 | | - `LLM_PROVIDER` also accepts `openai`/`anthropic`, no code change) + `parseJsonReply` for model output. |
79 | | -- **Solana** (`solana/`) — `connection.ts` (`solanaConnection`/`assertDevnet` guard) + `pay.ts` |
80 | | - (`generatePaymentUrl`/`verifyPayment`/`signTransfer`/`loadKeypairB58`, reference-bound). |
81 | | -- **CoralOS** (`coral/`) + **market** (`market/`) — an MCP client and the WANT/BID/AWARD protocol. Not |
82 | | - used by the single-agent web oracle; they power the **CoralOS round** (`coral-agents/` + |
83 | | - `examples/txodds/coral/`). |
84 | | - |
85 | | -### examples/txodds — the World Cup Oracle |
86 | | - |
87 | | -- `agent/edge.ts` — `analyzeEdge()`: verified de-margined odds → an LLM one-line call + confidence, |
88 | | - with a deterministic fallback (so it renders with no LLM key). Shared by the proxy and the agent. |
89 | | -- `agent/escrow.ts` — the buyer-side escrow client (`makeProgram`/`deposit`/`release`/`escrowPda`). It |
90 | | - fetches the program IDL **on-chain**, so only the deployed devnet program is needed, not a local build. |
91 | | -- `agent/arbiter.ts` — client for the deployed **arbiter** wrapper (bundled IDL `arbiter_idl.json`). |
92 | | -- `server/proxy.ts` — subscribes the buyer wallet to the free World Cup tier on devnet, then serves: |
93 | | - `/api/board` (only fixtures with verified live 1X2 odds, inlined), `/api/edge` (the agent's read), |
94 | | - `/api/settle` (settles via the **arbiter** wrapper, falling back to the direct escrow; the escrow |
95 | | - `reference` is bound to the read as `sha256(...)`). `/api/fixtures` + `/api/odds` are raw passthroughs. |
96 | | -- `web/app.js` — the React app. Loads `/api/board`, renders the board + the agent's read, and on |
97 | | - delivery auto-settles (no button), showing the 3-party arbiter settlement + Explorer links. |
98 | | - |
99 | | -### examples/txodds/escrow — the settlement spine (+ the arbiter) |
100 | | - |
101 | | -A Cargo workspace with **two** deployed devnet programs: `programs/escrow` (the spine — buyer deposits |
102 | | -into a per-order PDA seeded by `(buyer, reference)`, releases on delivery / refunds after a deadline) |
103 | | -and `programs/arbiter` (the trustless wrapper — a neutral 3rd signer gates release/refund via the |
104 | | -vault-as-buyer CPI pattern, so the buyer can't take delivery and refund). The demo settles through the |
105 | | -arbiter. Build with `anchor build`; the demo runs against the deployed ids. See its `README.md`. |
106 | | - |
107 | | -## Key Constraints |
108 | | - |
109 | | -- **Web demo: no Docker** (proxy + web). **CoralOS round: needs Docker** — `docker-compose.yml` runs |
110 | | - coral-server, which launches `coral-agents/` (buyer + seller) per session; `examples/txodds/coral/round.ts` |
111 | | - (`npm run coral`) is the launcher. The round settles via the base escrow (the web view adds the arbiter). |
112 | | -- **Devnet only** — payment + escrow code build their `Connection` via `solanaConnection()` |
113 | | - (`@pay/agent-runtime`), which throws on a mainnet RPC unless `ALLOW_MAINNET=1`; it defaults to |
114 | | - `https://api.devnet.solana.com`. Never put a funded mainnet keypair in `.env`. |
115 | | -- **`examples/txodds` depends on `@pay/agent-runtime` via a `file:` dep** — run `npm run build` in |
116 | | - `packages/agent-runtime` first so the dist exists. |
117 | | -- **Secrets live in `.env`** (gitignored). `server/proxy.ts` loads the repo-root `.env`; the proxy needs |
118 | | - `BUYER_KEYPAIR_B58` (from `setup.js`, funded) and `VENICE_API_KEY` (or another provider's key — see `LLM.md`). |
| 61 | +## TxODDS Example |
| 62 | + |
| 63 | +Key files: |
| 64 | + |
| 65 | +| File | Role | |
| 66 | +|---|---| |
| 67 | +| `examples/txodds/agent/txline.ts` | TxLINE client. | |
| 68 | +| `examples/txodds/agent/edge.ts` | Verified odds to fair-line analysis. | |
| 69 | +| `examples/txodds/agent/service.ts` | `deliverService()` for paid delivery. | |
| 70 | +| `examples/txodds/server/proxy.ts` | Proxy, settlement endpoints, run persistence, grading. | |
| 71 | +| `examples/txodds/research/watcher.ts` | Odds-move event queue. | |
| 72 | +| `examples/txodds/escrow/` | Escrow and arbiter Anchor programs. | |
| 73 | + |
| 74 | +`npm run dev` starts the proxy on `:8801` and the static UI on `:3020`. |
| 75 | + |
| 76 | +## CoralOS Usage |
| 77 | + |
| 78 | +The single-agent TxODDS web flow does not require CoralOS. Multi-agent flows use `docker-compose.yml` to run a pinned `coral-server` container and launch agents from `coral-agents/`. |
| 79 | + |
| 80 | +CoralOS provides: |
| 81 | + |
| 82 | +- per-session agent launching; |
| 83 | +- thread messages and mentions; |
| 84 | +- blocking wait primitives; |
| 85 | +- extended session state for feed/UI replay; |
| 86 | +- puppet API for the checkout bridge. |
| 87 | + |
| 88 | +The market protocol is owned by `packages/agent-runtime/src/market/protocol.ts`; Coral transports opaque strings. |
| 89 | + |
| 90 | +## Payment and Policy |
| 91 | + |
| 92 | +Solana value movement is devnet by default. Runtime helpers reject mainnet RPC URLs unless `ALLOW_MAINNET=1` is set. |
| 93 | + |
| 94 | +Policy checks are centralized in `packages/agent-runtime/src/policy` and cover spend caps, service allowlists, payout binding, award-price binding, rate limiting, and verifier gating. |
| 95 | + |
| 96 | +Harness processes should not receive signing keys. Agent processes hold wallet authority and call policy before deposits/releases. |
| 97 | + |
| 98 | +## Environment |
| 99 | + |
| 100 | +Common variables: |
| 101 | + |
| 102 | +| Variable | Purpose | |
| 103 | +|---|---| |
| 104 | +| `BUYER_KEYPAIR_B58` | Buyer funding keypair for devnet transactions. | |
| 105 | +| `ARBITER_KEYPAIR_B58` | Arbiter release/refund keypair. | |
| 106 | +| `WALLET` / `SELLER_WALLET` | Seller payout addresses. | |
| 107 | +| `SOLANA_RPC_URL` | Defaults to devnet if unset. | |
| 108 | +| `LLM_PROVIDER` | `venice`, `openai`, or `anthropic`. | |
| 109 | +| `VENICE_API_KEY` / `OPENAI_API_KEY` / `ANTHROPIC_API_KEY` | Provider keys. | |
| 110 | +| `TXLINE_API_KEY` | TxLINE token for TxODDS examples. | |
| 111 | + |
| 112 | +Never commit `.env`, private keys, provider keys, seed phrases, or generated wallet secrets. |
0 commit comments