Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
test(api): pin the crawl scan split, the refund guard and the payment-gate 402 contract
nirholasd5aafee3c3ed
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 72cec9ca1..f68ca5550 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -40,6 +40,7 @@ Public history for [three.ws](https://three.ws), newest first. New pages come fr- **The public profile sitemap now lists people, not the seed crons**: The sitemap Google and Bing crawl is split into one file per kind of page, and the profiles file was handing search engines every account row with a username. That was 29,600 machine accounts minted by the avatar and forge seed crons against 42 real people, growing by about one a minute, so the profiles that belong to actual members were buried under auto-generated ones whose content (the avatars and agents they own) was already listed in its own sitemap. Machine accounts are now excluded from both the profiles sitemap and the last-updated stamp the sitemap index publishes for it, so that stamp changes when a person updates their profile rather than every time a cron runs. Separately, a handful of URLs under the sitemap API answered scanners with a server error instead of a plain not-found, which also filed a false internal alert each time; those now return a normal not-found listing the sitemap types that do exist. `[fix, infra]`- **The sniper trade journal starts recording, and three sniper readouts stop reporting the wrong number**: The trade journal is the surface that answers why an agent bought and why it sold, and it had never recorded a single line. Two halves of the same mistake: the table expected a plain number where the platform stores an identifier, so every write failed and was silently discarded on purpose (a journal write must never be allowed to break a trade), and the page that reads it asked the database to compare the same two mismatched kinds of value, which failed outright and returned an error rather than an empty journal. Both are fixed and the surface now records and reads for real, across the 323 closed positions the fleet had already traded. Three readouts were also stating numbers that were not true. The Exit Lab corpus, asked without an explicit page size, answered with exactly one trade instead of the full 400, so the replay console looked like a fleet that had traded once; it now returns the whole window. The live pre-launch radar tape re-sent every precursor it had already shown, about forty times a minute, so the feed filled with copies of the same event; each precursor is now sent once. And the radar dashboard reported 100 watched wallets, the size of the table it was showing rather than the number it was watching, while the status line beside it reported the real figure; it now counts the wallets and the table says which slice of them it is showing. (`/radar`) `[fix, improvement]`- **The sniper's laddered-exit settings can finally be saved**: An armed sniper strategy has two knobs that decide how a winner is taken off the table: the multiple of your entry at which it sells enough to recover what you put in, and the minimum slice of the position it always keeps riding afterwards. Both were documented and both were read by the trading worker, but the arm API quietly threw them away on the way in, so every strategy kept the fleet default no matter what you sent. They now save, an explicit null on the multiple switches the strategy back to a single-shot exit, and reading your strategies back reports both values plus whether the treasury is allowed to refill that agent's wallet. Sending the treasury-refill consent flag now returns a clear error instead of a success that changed nothing, because that permission is granted by the platform, not by the arm call. (`/dashboard/sniper`) `[fix, improvement]`+- **The Solana agent directory is live again, and complete**: Discover and the deployments view list agents registered on the Metaplex Agent Registry, refreshed by a crawl that runs every half hour. The crawl had been reading nothing since 6 August: it asked one fixed Solana node for the registry, that node started refusing the request outright, and because it never tried any of the other nodes it fell back to, every run reported success while writing nothing. It now works its way down the same list of providers the rest of the platform uses, so one provider turning us away costs a second, not ten days. Fixing that surfaced a second fault underneath it: the registry holds two generations of agent records, and the crawl was reading both with the newer generation's layout, so older records came back as nonsense and the newer pass gave up entirely at the first old record it met. Each generation is now read on its own terms. The directory went from 1,558 stale entries to all 1,571 currently on chain, and new registrations show up within the half hour again. Separately, the treasury top-up that keeps the platform's wallets funded no longer fails outright when a Solana provider answers with something it cannot read: it treats that the same as a provider being busy and retries on the next pass, instead of skipping a funding cycle. (`/discover`) `[fix, infra]`- **The wallet balance API stops calling a rejected upload a validation error**: Apps and agents that read wallet balances through three.ws get honest answers when a request cannot be read. Posting a body that was too large, or forgetting the JSON content-type header, used to come back as "your fields are invalid", sending integrators hunting through a chain and address that were never the problem; those now answer with the right status for what actually went wrong. When the fields genuinely are wrong, the reply is one readable line naming the offending field plus a machine-readable list of issues, instead of the raw multi-line dump the endpoint used to return. `[fix, improvement]`- **Token sentiment reads real pump.fun commentary again**: The sentiment pulse for a Solana token was reading a pump.fun route that no longer exists, so it scored an empty set and reported every coin as perfectly neutral with full confidence. It now reads the coin's live callouts, the commentary feed the pump.fun coin page renders, and when that source is unreachable and you supplied no text of your own it says so with an error instead of returning a neutral-looking score. The scorer itself was also matching lexicon terms inside longer words, which read 'partnered' as bearish; it now matches whole words and knows the common variants (scammer, rugged, crashing, bought). (`/tutorials/emotion-from-data`) `[fix, improvement]`- **Token-gated scene shares survive a slow RPC, and a gate that could never open is refused up front**: Three repairs to the holder-only chat scene shares. When the blockchain read behind a gate failed (a provider down, a missing provider key), visitors were told they did not hold enough tokens, which was untrue and also handed the provider's raw error text to anyone who asked; a failed read now says plainly that verification is unavailable and invites a retry, while a genuine shortfall still says exactly how much is missing. The Solana read itself now rotates through the same pool of providers the rest of the platform uses instead of leaning on one endpoint, NFT collection gates use the indexer that can actually answer them, and gates on Ethereum work without a paid provider key. Finally, a gate whose chain and asset type did not match (a Solana scene gated on an Ethereum token) used to be accepted and then locked everyone out forever; that combination, and an address in the wrong format for its chain, are now refused when the share link is created. (`/chat`) `[fix, improvement]`diff --git a/data/changelog.json b/data/changelog.jsonindex 00bf2e7b1..03083e006 100644--- a/data/changelog.json+++ b/data/changelog.json@@ -4,12 +4,12 @@{"date": "2026-08-16","title": "The Solana agent directory is live again, and complete",- "summary": "Explore and the deployments view list agents registered on the Metaplex Agent Registry, refreshed by a crawl that runs every half hour. The crawl had been reading nothing since 6 August: it asked one fixed Solana node for the registry, that node started refusing the request outright, and because it never tried any of the other nodes it fell back to, every run reported success while writing nothing. It now works its way down the same list of providers the rest of the platform uses, so one provider turning us away costs a second, not ten days. Fixing that surfaced a second fault underneath it: the registry holds two generations of agent records, and the crawl was reading both with the newer generation's layout, so older records came back as nonsense and the newer pass gave up entirely at the first old record it met. Each generation is now read on its own terms. The directory went from 1,558 stale entries to all 1,571 currently on chain, and new registrations show up within the half hour again. Separately, the treasury top-up that keeps the platform's wallets funded no longer fails outright when a Solana provider answers with something it cannot read: it treats that the same as a provider being busy and retries on the next pass, instead of skipping a funding cycle.",+ "summary": "Discover and the deployments view list agents registered on the Metaplex Agent Registry, refreshed by a crawl that runs every half hour. The crawl had been reading nothing since 6 August: it asked one fixed Solana node for the registry, that node started refusing the request outright, and because it never tried any of the other nodes it fell back to, every run reported success while writing nothing. It now works its way down the same list of providers the rest of the platform uses, so one provider turning us away costs a second, not ten days. Fixing that surfaced a second fault underneath it: the registry holds two generations of agent records, and the crawl was reading both with the newer generation's layout, so older records came back as nonsense and the newer pass gave up entirely at the first old record it met. Each generation is now read on its own terms. The directory went from 1,558 stale entries to all 1,571 currently on chain, and new registrations show up within the half hour again. Separately, the treasury top-up that keeps the platform's wallets funded no longer fails outright when a Solana provider answers with something it cannot read: it treats that the same as a provider being busy and retries on the next pass, instead of skipping a funding cycle.","tags": ["fix","infra"],- "link": "/explore"+ "link": "/discover"},{"date": "2026-08-16",diff --git a/docs/github-memory-seeding.md b/docs/github-memory-seeding.mdindex 564b17495..0a2d35bb0 100644--- a/docs/github-memory-seeding.md+++ b/docs/github-memory-seeding.md@@ -191,7 +191,8 @@ curl -s https://three.ws/api/agents/$AGENT_ID/memory/seed/github --cookie "$JAR"```Rate-limited to one seed per agent per 6 hours, matching the X and Farcaster-lanes.+lanes. A run that stores nothing does not spend the window: see the two+refunding errors below.### `DELETE /api/agents/:id/memory/seed/github`@@ -217,8 +218,8 @@ with the connection row itself, and then asks GitHub to revoke the OAuth grant| `not_connected` | 412 | Connect GitHub first; the response carries `connect_url` || `invalid_selection` | 400 | A selected key is not in the catalog you were shown; `rejected` names each one || `empty_selection` | 400 | Pick your profile or at least one repository |-| `distill_error` | 502 | The selected material yielded no usable facts; add a README or another repository |-| `distill_unavailable` | 503 | Every model provider was busy, so nothing was read into memory and the existing memories are untouched. Carries `retry_at` (when this agent's next seed is allowed) and `providers_tried` |+| `distill_error` | 502 | The selected material yielded no usable facts; add a README or another repository. Carries `window_refunded` |+| `distill_unavailable` | 503 | Every model provider was busy, so nothing was read into memory and the existing memories are untouched. Carries `window_refunded`, `providers_tried`, and `retry_at` (null when the window was refunded) || rate limited | 429 | One seed per agent per 6 hours |Only `distill_error` and `distill_unavailable` are reachable after the seed@@ -227,6 +228,16 @@ immediately before the README reads and the distilling pass. Every otherrefusal above happens before that point and leaves your window intact, so amistyped or stale pick costs you nothing but the retry.+Both of those two also hand the window back. Neither one read anything into+memory or changed a single stored fact, and in the provider-outage case the+failure is the platform's, not yours, so the charge is reversed and the next+attempt is allowed immediately (`window_refunded: true`). Refunding is only+safe because this is a single-use window: see `refundLimit` in+[api/_lib/rate-limit.js](../api/_lib/rate-limit.js), which refuses any bucket+with a ceiling above one. If the refund itself cannot reach the limiter store,+the reply says so (`window_refunded: false`) and carries the real `retry_at`+rather than inviting a retry that would only earn a 429.+## Source| Piece | File |diff --git a/public/changelog.json b/public/changelog.jsonindex 187d5c478..0e9c68b7f 100644--- a/public/changelog.json+++ b/public/changelog.json@@ -1,5 +1,5 @@{- "generated_at": "2026-08-16T16:23:14.437Z",+ "generated_at": "2026-08-16T16:25:03.451Z","generated_by": "scripts/build-page-index.mjs from data/pages.json + data/changelog.json","site": {"name": "three.ws",@@ -366,6 +366,17 @@"improvement"]},+ {+ "date": "2026-08-16",+ "type": "update",+ "title": "The Solana agent directory is live again, and complete",+ "summary": "Discover and the deployments view list agents registered on the Metaplex Agent Registry, refreshed by a crawl that runs every half hour. The crawl had been reading nothing since 6 August: it asked one fixed Solana node for the registry, that node started refusing the request outright, and because it never tried any of the other nodes it fell back to, every run reported success while writing nothing. It now works its way down the same list of providers the rest of the platform uses, so one provider turning us away costs a second, not ten days. Fixing that surfaced a second fault underneath it: the registry holds two generations of agent records, and the crawl was reading both with the newer generation's layout, so older records came back as nonsense and the newer pass gave up entirely at the first old record it met. Each generation is now read on its own terms. The directory went from 1,558 stale entries to all 1,571 currently on chain, and new registrations show up within the half hour again. Separately, the treasury top-up that keeps the platform's wallets funded no longer fails outright when a Solana provider answers with something it cannot read: it treats that the same as a provider being busy and retries on the next pass, instead of skipping a funding cycle.",+ "link": "/discover",+ "tags": [+ "fix",+ "infra"+ ]+ },{"date": "2026-08-16","type": "update",diff --git a/public/changelog.xml b/public/changelog.xmlindex d095d3ef2..2efe349e6 100644--- a/public/changelog.xml+++ b/public/changelog.xml@@ -278,6 +278,14 @@<category>fix</category><description>An armed sniper strategy has two knobs that decide how a winner is taken off the table: the multiple of your entry at which it sells enough to recover what you put in, and the minimum slice of the position it always keeps riding afterwards. Both were documented and both were read by the trading worker, but the arm API quietly threw them away on the way in, so every strategy kept the fleet default no matter what you sent. They now save, an explicit null on the multiple switches the strategy back to a single-shot exit, and reading your strategies back reports both values plus whether the treasury is allowed to refill that agent's wallet. Sending the treasury-refill consent flag now returns a clear error instead of a success that changed nothing, because that permission is granted by the platform, not by the arm call.</description></item>+ <item>+ <title>The Solana agent directory is live again, and complete</title>+ <link>https://three.ws/discover</link>+ <guid isPermaLink="false">2026-08-16:The Solana agent directory is live again, and complete</guid>+ <pubDate>Sun, 16 Aug 2026 12:00:00 GMT</pubDate>+ <category>fix</category>+ <description>Discover and the deployments view list agents registered on the Metaplex Agent Registry, refreshed by a crawl that runs every half hour. The crawl had been reading nothing since 6 August: it asked one fixed Solana node for the registry, that node started refusing the request outright, and because it never tried any of the other nodes it fell back to, every run reported success while writing nothing. It now works its way down the same list of providers the rest of the platform uses, so one provider turning us away costs a second, not ten days. Fixing that surfaced a second fault underneath it: the registry holds two generations of agent records, and the crawl was reading both with the newer generation's layout, so older records came back as nonsense and the newer pass gave up entirely at the first old record it met. Each generation is now read on its own terms. The directory went from 1,558 stale entries to all 1,571 currently on chain, and new registrations show up within the half hour again. Separately, the treasury top-up that keeps the platform's wallets funded no longer fails outright when a Solana provider answers with something it cannot read: it treats that the same as a provider being busy and retries on the next pass, instead of skipping a funding cycle.</description>+ </item><item><title>The wallet balance API stops calling a rejected upload a validation error</title><link>https://three.ws/changelog</link>diff --git a/public/features.json b/public/features.jsonindex c763b895c..07a54e0c3 100644--- a/public/features.json+++ b/public/features.json@@ -1,5 +1,5 @@{- "generated_at": "2026-08-16T16:06:49.795Z",+ "generated_at": "2026-08-16T16:25:03.434Z","generated_by": "scripts/build-page-index.mjs from data/pages.json","site": {"name": "three.ws",diff --git a/tests/forge-consumption-payment.test.js b/tests/forge-consumption-payment.test.jsindex 4edf6baef..691a012e3 100644--- a/tests/forge-consumption-payment.test.js+++ b/tests/forge-consumption-payment.test.js@@ -33,10 +33,15 @@ const ACTION = 'forge.gameready';const GR_USD = Number(priceForAction(ACTION).usd); // 0.10 from the catalogconst HIGH_USD = Number(priceForAction('forge.high').usd); // 0.50const REF_ID = 'forge-gameready-abc123';+// token_payments.id is a `uuid` column (2026-06-01-token-payments.sql), so every+// real payment id is one. The fixtures use real uuids so the module is exercised+// against ids of the shape production actually hands it.+const PAY_ID = '3f1c8e9a-4d21-4b6e-9f07-2a5c8d13b7e4';+const UNKNOWN_PAY_ID = '9b2d7c14-8e35-4a90-b1f6-0c47ae2d5938';function paymentRow(over = {}) {return {- id: 'pay-1',+ id: PAY_ID,purpose: 'consumption',usd: GR_USD.toFixed(6),ref_type: 'forge',@@ -51,7 +56,7 @@ function paymentRow(over = {}) {// redemption-existence check.function queueAssert({ payment = paymentRow(), redeemed = false } = {}) {queue.push(payment ? [payment] : []); // lookupPayment- queue.push(redeemed ? [{ payment_id: 'pay-1' }] : []); // isRedeemed+ queue.push(redeemed ? [{ payment_id: PAY_ID }] : []); // isRedeemed}beforeEach(() => {@@ -62,14 +67,14 @@ beforeEach(() => {describe('assertForgePurchase', () => {it('accepts a settled, ref-bound, correctly-priced, recent, unredeemed payment', async () => {queueAssert();- const r = await assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID });+ const r = await assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID });expect(r.ok).toBe(true);- expect(r.payment).toMatchObject({ id: 'pay-1', usd: GR_USD });+ expect(r.payment).toMatchObject({ id: PAY_ID, usd: GR_USD });expect(r.payment.settledAt).toBeTruthy();});it('requires action, payment_id and ref_id', async () => {- await expect(assertForgePurchase({ action: '', paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: '', paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({status: 400,code: 'bad_request',});@@ -77,7 +82,7 @@ describe('assertForgePurchase', () => {status: 400,code: 'bad_request',});- await expect(assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: '' })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: '' })).rejects.toMatchObject({status: 400,code: 'bad_request',});@@ -85,27 +90,53 @@ describe('assertForgePurchase', () => {it('rejects an unknown payment as payment_invalid (402)', async () => {queue.push([]); // lookupPayment → none- await expect(assertForgePurchase({ action: ACTION, paymentId: 'nope', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: UNKNOWN_PAY_ID, refId: REF_ID })).rejects.toMatchObject({status: 402,code: 'payment_invalid',});});+ // Regression: a malformed payment_id used to reach the `where id = $1` lookup+ // against a uuid column, so Postgres raised 22P02 and the driver error escaped+ // this module carrying the SQLSTATE as `code` and the raw cast message+ // ("invalid input syntax for type uuid: ...") as `message`. The Game-Ready gate+ // echoes both to an unauthenticated caller, so a 402 leaked the storage engine+ // and the column type. It must be answered as a plain payment_invalid, decided+ // before any query runs.+ it('rejects a malformed payment_id as payment_invalid without querying the database', async () => {+ await expect(assertForgePurchase({ action: ACTION, paymentId: 'deadbeef', refId: REF_ID })).rejects.toMatchObject({+ status: 402,+ code: 'payment_invalid',+ });+ expect(sql).not.toHaveBeenCalled();+ });++ // The malformed and the merely-unknown id must be indistinguishable, so the+ // endpoint is never an oracle for which payment ids exist.+ it('answers a malformed and an unknown payment id identically', async () => {+ const malformed = await assertForgePurchase({ action: ACTION, paymentId: 'deadbeef', refId: REF_ID }).catch((e) => e);+ queue.push([]); // lookupPayment → none+ const unknown = await assertForgePurchase({ action: ACTION, paymentId: UNKNOWN_PAY_ID, refId: REF_ID }).catch((e) => e);+ expect(malformed.code).toBe(unknown.code);+ expect(malformed.status).toBe(unknown.status);+ expect(malformed.message).toBe(unknown.message);+ });+it('rejects a non-consumption / non-forge payment', async () => {queueAssert({ payment: paymentRow({ purpose: 'spin' }) });- await expect(assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});queueAssert({ payment: paymentRow({ ref_type: 'voice' }) });- await expect(assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});});it('rejects a payment bound to a different ref_id', async () => {queueAssert({ payment: paymentRow({ ref_id: 'someone-elses-nonce' }) });- await expect(assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});});@@ -115,7 +146,7 @@ describe('assertForgePurchase', () => {// check is what keeps same-ref_type Forge actions apart.expect(HIGH_USD).not.toBe(GR_USD);queueAssert({ payment: paymentRow({ usd: HIGH_USD.toFixed(6) }) });- await expect(assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});});@@ -123,7 +154,7 @@ describe('assertForgePurchase', () => {it('rejects a payment older than the redemption window (payment_expired)', async () => {const old = new Date(Date.now() - 48 * 60 * 60 * 1000).toISOString();queueAssert({ payment: paymentRow({ confirmed_at: old, created_at: old }) });- await expect(assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({status: 402,code: 'payment_expired',});@@ -131,7 +162,7 @@ describe('assertForgePurchase', () => {it('rejects an already-redeemed payment (409 payment_already_used)', async () => {queueAssert({ redeemed: true });- await expect(assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({status: 409,code: 'payment_already_used',});@@ -140,11 +171,11 @@ describe('assertForgePurchase', () => {it('applies the holder discount to the expected price when a discount rides along', async () => {const discounted = Number(priceForAction(ACTION, { discountBps: 500 }).usd); // 5% offqueueAssert({ payment: paymentRow({ usd: discounted.toFixed(6) }) });- const r = await assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID, discountBps: 500 });+ const r = await assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID, discountBps: 500 });expect(r.ok).toBe(true);// The same discounted payment fails at full price (no discount presented).queueAssert({ payment: paymentRow({ usd: discounted.toFixed(6) }) });- await expect(assertForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});});@@ -152,15 +183,15 @@ describe('assertForgePurchase', () => {describe('redeemForgePurchase', () => {it('claims a payment when the insert wins (returns a row)', async () => {- queue.push([{ payment_id: 'pay-1' }]);- const r = await redeemForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID });+ queue.push([{ payment_id: PAY_ID }]);+ const r = await redeemForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID });expect(r.redeemed).toBe(true);expect(sql).toHaveBeenCalledOnce();});it('does not claim when the row already exists (ON CONFLICT DO NOTHING → no row)', async () => {queue.push([]); // conflict → nothing returned- const r = await redeemForgePurchase({ action: ACTION, paymentId: 'pay-1', refId: REF_ID });+ const r = await redeemForgePurchase({ action: ACTION, paymentId: PAY_ID, refId: REF_ID });expect(r.redeemed).toBe(false);});});@@ -168,8 +199,8 @@ describe('redeemForgePurchase', () => {describe('releaseForgePurchase', () => {it('deletes the claim so the payment is reusable on retry', async () => {queue.push([]);- await releaseForgePurchase({ paymentId: 'pay-1' });+ await releaseForgePurchase({ paymentId: PAY_ID });expect(sql).toHaveBeenCalledOnce();- expect(sql.mock.calls[0].slice(1).map(String)).toContain('pay-1');+ expect(sql.mock.calls[0].slice(1).map(String)).toContain(PAY_ID);});});diff --git a/tests/forge-high-payment.test.js b/tests/forge-high-payment.test.jsindex d60180a85..c76049699 100644--- a/tests/forge-high-payment.test.js+++ b/tests/forge-high-payment.test.js@@ -28,12 +28,17 @@ import { priceForAction } from '../api/_lib/pricing/catalog.js';const HIGH_USD = Number(priceForAction('forge.high').usd); // 0.50 from the catalogconst REF_ID = 'forge-high-abc123';+// token_payments.id is a `uuid` column (2026-06-01-token-payments.sql), so every+// real payment id is one. The fixtures use real uuids so the module is exercised+// against ids of the shape production actually hands it.+const PAY_ID = '7c4e1b93-2a68-4f0d-8b52-1e93af6c04d7';+const UNKNOWN_PAY_ID = '05a9d6f2-b731-4c8e-9a14-6d2f83be5710';// A settled consumption payment row as token_payments returns it (numeric usd// comes back as a string from the driver). `confirmed_at` defaults to now.function paymentRow(over = {}) {return {- id: 'pay-1',+ id: PAY_ID,purpose: 'consumption',usd: HIGH_USD.toFixed(6),ref_type: 'forge',@@ -48,7 +53,7 @@ function paymentRow(over = {}) {// redemption-existence check.function queueAssert({ payment = paymentRow(), redeemed = false } = {}) {queue.push(payment ? [payment] : []); // lookupPayment- queue.push(redeemed ? [{ payment_id: 'pay-1' }] : []); // isRedeemed+ queue.push(redeemed ? [{ payment_id: PAY_ID }] : []); // isRedeemed}beforeEach(() => {@@ -59,9 +64,9 @@ beforeEach(() => {describe('assertForgePayment', () => {it('accepts a settled, ref-bound, correctly-priced, recent, unredeemed payment', async () => {queueAssert();- const r = await assertForgePayment({ paymentId: 'pay-1', refId: REF_ID });+ const r = await assertForgePayment({ paymentId: PAY_ID, refId: REF_ID });expect(r.ok).toBe(true);- expect(r.payment).toMatchObject({ id: 'pay-1', usd: HIGH_USD });+ expect(r.payment).toMatchObject({ id: PAY_ID, usd: HIGH_USD });expect(r.payment.settledAt).toBeTruthy();});@@ -70,7 +75,7 @@ describe('assertForgePayment', () => {status: 400,code: 'bad_request',});- await expect(assertForgePayment({ paymentId: 'pay-1', refId: '' })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: PAY_ID, refId: '' })).rejects.toMatchObject({status: 400,code: 'bad_request',});@@ -78,7 +83,7 @@ describe('assertForgePayment', () => {it('rejects an unknown payment as payment_invalid (402)', async () => {queue.push([]); // lookupPayment → none- await expect(assertForgePayment({ paymentId: 'nope', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: UNKNOWN_PAY_ID, refId: REF_ID })).rejects.toMatchObject({status: 402,code: 'payment_invalid',});@@ -86,26 +91,26 @@ describe('assertForgePayment', () => {it('rejects a non-consumption / non-forge payment (a different action) ', async () => {queueAssert({ payment: paymentRow({ purpose: 'spin' }) });- await expect(assertForgePayment({ paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});queueAssert({ payment: paymentRow({ ref_type: 'voice' }) });- await expect(assertForgePayment({ paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});});it('rejects a payment bound to a different ref_id', async () => {queueAssert({ payment: paymentRow({ ref_id: 'someone-elses-nonce' }) });- await expect(assertForgePayment({ paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});});it('rejects an underpaid payment (price must cover forge.high)', async () => {queueAssert({ payment: paymentRow({ usd: '0.100000' }) });- await expect(assertForgePayment({ paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});});@@ -113,7 +118,7 @@ describe('assertForgePayment', () => {it('rejects a payment older than the redemption window (payment_expired)', async () => {const old = new Date(Date.now() - 48 * 60 * 60 * 1000).toISOString();queueAssert({ payment: paymentRow({ confirmed_at: old, created_at: old }) });- await expect(assertForgePayment({ paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({status: 402,code: 'payment_expired',});@@ -121,7 +126,7 @@ describe('assertForgePayment', () => {it('rejects an already-redeemed payment (409 payment_already_used)', async () => {queueAssert({ redeemed: true });- await expect(assertForgePayment({ paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({status: 409,code: 'payment_already_used',});@@ -132,11 +137,11 @@ describe('assertForgePayment', () => {expect(discounted).toBeLessThan(HIGH_USD);// A payment for the discounted amount clears with the discount applied…queueAssert({ payment: paymentRow({ usd: discounted.toFixed(6) }) });- const r = await assertForgePayment({ paymentId: 'pay-1', refId: REF_ID, discountBps: 500 });+ const r = await assertForgePayment({ paymentId: PAY_ID, refId: REF_ID, discountBps: 500 });expect(r.ok).toBe(true);// …but the same discounted payment fails at full price (no discount presented).queueAssert({ payment: paymentRow({ usd: discounted.toFixed(6) }) });- await expect(assertForgePayment({ paymentId: 'pay-1', refId: REF_ID })).rejects.toMatchObject({+ await expect(assertForgePayment({ paymentId: PAY_ID, refId: REF_ID })).rejects.toMatchObject({code: 'payment_invalid',});});@@ -144,15 +149,15 @@ describe('assertForgePayment', () => {describe('redeemForgePayment', () => {it('claims a payment when the insert wins (returns a row)', async () => {- queue.push([{ payment_id: 'pay-1' }]);- const r = await redeemForgePayment({ paymentId: 'pay-1', refId: REF_ID });+ queue.push([{ payment_id: PAY_ID }]);+ const r = await redeemForgePayment({ paymentId: PAY_ID, refId: REF_ID });expect(r.redeemed).toBe(true);expect(sql).toHaveBeenCalledOnce();});it('does not claim when the row already exists (ON CONFLICT DO NOTHING → no row)', async () => {queue.push([]); // conflict → nothing returned- const r = await redeemForgePayment({ paymentId: 'pay-1', refId: REF_ID });+ const r = await redeemForgePayment({ paymentId: PAY_ID, refId: REF_ID });expect(r.redeemed).toBe(false);});});@@ -160,9 +165,9 @@ describe('redeemForgePayment', () => {describe('releaseForgePayment', () => {it('deletes the claim so the payment is reusable on retry', async () => {queue.push([]);- await releaseForgePayment({ paymentId: 'pay-1' });+ await releaseForgePayment({ paymentId: PAY_ID });expect(sql).toHaveBeenCalledOnce();// The interpolated payment id is passed as the query's 2nd arg.- expect(sql.mock.calls[0].slice(1).map(String)).toContain('pay-1');+ expect(sql.mock.calls[0].slice(1).map(String)).toContain(PAY_ID);});});diff --git a/tests/github-memory-seed-endpoint.test.js b/tests/github-memory-seed-endpoint.test.jsindex 9c165835c..500caa876 100644--- a/tests/github-memory-seed-endpoint.test.js+++ b/tests/github-memory-seed-endpoint.test.js@@ -34,8 +34,12 @@ vi.mock('../api/_lib/env.js', () => ({}));const githubSeedLimit = vi.fn();+const githubSeedRefund = vi.fn();vi.mock('../api/_lib/rate-limit.js', () => ({- limits: { githubSeed: (...a) => githubSeedLimit(...a) },+ limits: {+ githubSeed: (...a) => githubSeedLimit(...a),+ githubSeedRefund: (...a) => githubSeedRefund(...a),+ },clientIp: () => '127.0.0.1',}));@@ -113,6 +117,7 @@ beforeEach(() => {extractBearerMock.mockReset().mockReturnValue(null);requireCsrfMock.mockReset().mockResolvedValue(true);githubSeedLimit.mockReset().mockResolvedValue({ success: true, limit: 1, remaining: 0, reset: Date.now() + 21_600_000 });+ githubSeedRefund.mockReset().mockResolvedValue(true);llmCompleteMock.mockReset().mockResolvedValue({ text: '["A fact about the owner."]' });fetchProfileMock.mockReset().mockResolvedValue({ login: 'dev', name: 'Dev', public_repos: 2, followers: 3 });fetchPinnedReposMock.mockReset().mockResolvedValue([@@ -224,19 +229,51 @@ describe('when every model provider is busy', () => {expect(body.error).toBe('distill_unavailable');expect(body.error_description).toMatch(/nothing was seeded/);expect(body.providers_tried).toEqual(['ovh', 'pollinations']);- expect(typeof body.retry_at).toBe('string');expect(sqlMock.transaction).not.toHaveBeenCalled();+ // The outage is ours, so the window goes back: the owner retries when a+ // provider frees up instead of waiting out six hours for a run that read+ // nothing and wrote nothing.+ expect(githubSeedRefund).toHaveBeenCalledWith(AGENT);+ expect(body.window_refunded).toBe(true);+ expect(body.retry_at).toBeNull();+ expect(body.error_description).toMatch(/did not use up your six-hour window/);+ });++ it('keeps the retry deadline in the reply when the refund itself fails', async () => {+ connected();+ llmCompleteMock.mockRejectedValue(Object.assign(new Error('chain exhausted'), { attempts: [] }));+ githubSeedRefund.mockRejectedValue(new Error('redis unreachable'));+ const res = mkRes();+ await handler(post({ include_profile: true, repos: ['dev/kit'], readmes: [] }), res);+ expect(res.statusCode).toBe(503);+ const body = parse(res);+ expect(body.window_refunded).toBe(false);+ expect(typeof body.retry_at).toBe('string');+ expect(body.error_description).toMatch(/can be seeded again in about/);});- it('still answers distill_error when the material yields no facts', async () => {+ it('still answers distill_error when the material yields no facts, and refunds', async () => {connected();llmCompleteMock.mockResolvedValue({ text: '[]' });const res = mkRes();await handler(post({ include_profile: true, repos: ['dev/kit'], readmes: [] }), res);expect(res.statusCode).toBe(502);- expect(parse(res).error).toBe('distill_error');+ const body = parse(res);+ expect(body.error).toBe('distill_error');+ // The advice to pick more material is worthless behind a six-hour wait.+ expect(githubSeedRefund).toHaveBeenCalledWith(AGENT);+ expect(body.window_refunded).toBe(true);expect(sqlMock.transaction).not.toHaveBeenCalled();});++ it('does not refund a run that actually wrote memories', async () => {+ connected();+ const res = mkRes();+ await handler(post({ include_profile: true, repos: ['dev/kit'], readmes: [] }), res);+ expect(res.statusCode).toBe(200);+ expect(sqlMock.transaction).toHaveBeenCalled();+ expect(githubSeedRefund).not.toHaveBeenCalled();+ });});describe('the README narrowing survives the route', () => {diff --git a/tests/rate-limit-refund.test.js b/tests/rate-limit-refund.test.jsnew file mode 100644index 000000000..44ce38fca--- /dev/null+++ b/tests/rate-limit-refund.test.js@@ -0,0 +1,129 @@+// Tests for refunding a consumed single-use rate-limit window.+//+// The case this exists for: a caller is charged a 6-hour window, then the run+// produces nothing at all because a dependency the platform owns was down (every+// LLM provider busy at once, in the GitHub memory-seeding lane). Charging that+// to the caller means our outage costs them six hours on a run that read nothing+// and wrote nothing, so the handler hands the window back.+//+// A refund is only meaningful on a single-use window: Upstash cannot return one+// token, so the Redis path resets the identifier, which is equivalent only when+// the ceiling is 1. refundLimit refuses anything else rather than silently+// clearing a bucket it was never allowed to clear.++import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';++// In-memory stand-in for rate_limit_counters, with the UPDATE the refund issues.+function makeSqlStub() {+ const rows = new Map();+ const sql = (strings, ...values) => {+ const text = strings.join('?');+ if (/DELETE FROM rate_limit_counters/i.test(text)) return Promise.resolve([]);+ if (/INSERT INTO rate_limit_counters/i.test(text)) {+ const [bucket, windowStart] = values;+ const key = `${bucket}\u0000${windowStart}`;+ const hits = (rows.get(key) || 0) + 1;+ rows.set(key, hits);+ return Promise.resolve([{ hits }]);+ }+ if (/UPDATE rate_limit_counters/i.test(text)) {+ const [bucket, windowStart] = values;+ const key = `${bucket}\u0000${windowStart}`;+ if (!rows.has(key)) return Promise.resolve([]);+ const hits = Math.max(0, rows.get(key) - 1);+ rows.set(key, hits);+ return Promise.resolve([{ hits }]);+ }+ return Promise.resolve([]);+ };+ return { sql, rows };+}++async function loadRateLimit({ redis = null, sqlStub, production = false } = {}) {+ vi.resetModules();+ vi.doMock('../api/_lib/db.js', () => ({ sql: sqlStub.sql }));+ vi.doMock('../api/_lib/redis.js', () => ({ getRedis: () => redis, isRedisAuthError: () => false }));+ vi.doMock('../api/_lib/env.js', () => ({+ env: {+ NODE_ENV: production ? 'production' : 'test',+ VERCEL_ENV: production ? 'production' : 'development',+ DATABASE_URL: 'postgres://stub/db',+ CACHE_REDIS_CMD_TIMEOUT_MS: 3000,+ },+ }));+ return import('../api/_lib/rate-limit.js');+}++beforeEach(() => {+ vi.stubEnv('VITEST', '1');+});+afterEach(() => {+ vi.unstubAllEnvs();+ vi.doUnmock('../api/_lib/db.js');+ vi.doUnmock('../api/_lib/redis.js');+ vi.doUnmock('../api/_lib/env.js');+});++describe('the GitHub seeding window', () => {+ it('blocks a second run, and lets it through once the window is refunded', async () => {+ const { limits } = await loadRateLimit({ sqlStub: makeSqlStub() });++ expect((await limits.githubSeed('agent-1')).success).toBe(true);+ expect((await limits.githubSeed('agent-1')).success).toBe(false);++ expect(await limits.githubSeedRefund('agent-1')).toBe(true);+ expect((await limits.githubSeed('agent-1')).success).toBe(true);+ // The refund returned one hit, not the whole bucket.+ expect((await limits.githubSeed('agent-1')).success).toBe(false);+ });++ it('refunds only the agent that failed', async () => {+ const { limits } = await loadRateLimit({ sqlStub: makeSqlStub() });++ await limits.githubSeed('agent-1');+ await limits.githubSeed('agent-2');+ await limits.githubSeedRefund('agent-1');++ expect((await limits.githubSeed('agent-1')).success).toBe(true);+ expect((await limits.githubSeed('agent-2')).success).toBe(false);+ });++ it('reports nothing to refund when the window was never spent', async () => {+ const { limits } = await loadRateLimit({ sqlStub: makeSqlStub() });+ expect(await limits.githubSeedRefund('never-seeded')).toBe(false);+ });+});++describe('refundLimit', () => {+ it('refuses a bucket whose ceiling is not one', async () => {+ const { refundLimit } = await loadRateLimit({ sqlStub: makeSqlStub() });+ await expect(+ refundLimit('withdrawal:user', { limit: 5, window: '1 d', critical: true }, 'user-1'),+ ).rejects.toThrow(/not a single-use window/);+ });++ it('decrements the durable Postgres counter for a critical bucket', async () => {+ const stub = makeSqlStub();+ const { refundLimit } = await loadRateLimit({ sqlStub: stub, production: true });++ // A critical bucket with no Redis counts in Postgres, so its refund has to+ // find the row the counter wrote: same bucket key, same window start.+ const ms = 3600_000;+ const windowStart = Math.floor(Date.now() / ms) * ms;+ const key = `test:single\u0000user-1\u0000${windowStart}`;+ stub.rows.set(key, 1);++ expect(+ await refundLimit('test:single', { limit: 1, window: '1 h', critical: true }, 'user-1'),+ ).toBe(true);+ expect(stub.rows.get(key)).toBe(0);+ });++ it('reports no refund when the durable counter holds no row for the caller', async () => {+ const stub = makeSqlStub();+ const { refundLimit } = await loadRateLimit({ sqlStub: stub, production: true });+ expect(+ await refundLimit('test:single', { limit: 1, window: '1 h', critical: true }, 'nobody'),+ ).toBe(false);+ });+});diff --git a/tests/solana-agents-crawl-scan.test.js b/tests/solana-agents-crawl-scan.test.jsnew file mode 100644index 000000000..ffa74a896--- /dev/null+++ b/tests/solana-agents-crawl-scan.test.js@@ -0,0 +1,99 @@+// The Metaplex identity scan has two failure modes that both look like success+// from outside: the cron returns HTTP 200, Cloud Scheduler discards the body, and+// nothing reaches solana_agents_index. Production ran in both states at once, and+// the only symptom was a directory that stopped growing (last upsert 2026-08-06,+// discovered 2026-08-16 with the cron firing cleanly every 30 minutes since).+//+// 1. Lane pinning. Enumeration is getProgramAccounts, the method free lanes+// refuse first, and the scan used solanaRpcEndpoints()[0] raw with no+// failover. Lane 0 is SOLANA_RPC_URL, which is rpc.magicblock.app, which+// answers gPA with 403 "Your IP or provider is blocked from this endpoint".+// 2. Discriminator. Both GPA builders register `key` at offset 0 and neither+// filtered on it, so one version's deserializer was handed every account in+// the program. Measured against mainnet: 1503 v2 + 68 v1 = 1571 total. The+// v2 scan threw on the first v1 account; the v1 scan silently accepted all+// 1571 and read v2 bytes through the v1 layout.+//+// These pin both: the scan builds its umi on a rotating Connection, and each+// builder is filtered to its own key.++import { describe, expect, it, vi, beforeEach } from 'vitest';++const rotatingConnection = { tag: 'rotating-connection' };+const createUmiCalls = [];+const whereFieldCalls = [];++vi.mock('../api/_lib/db.js', () => ({+ sql: Object.assign(async () => [], { unsafe: async () => [] }),+}));++vi.mock('../api/_lib/solana/connection.js', () => ({+ // Lane 0 is the refusing one, exactly as in production. A scan that reaches+ // for this URL instead of the rotating connection is the bug being pinned.+ solanaRpcEndpoints: () => ['https://rpc.magicblock.app/mainnet', 'https://api.mainnet-beta.solana.com'],+ solanaConnection: vi.fn(() => rotatingConnection),+}));++function fakeGpaBuilder(label) {+ const builder = {+ whereField(field, value) {+ whereFieldCalls.push({ label, field, value });+ return builder;+ },+ getDeserialized: async () => [],+ };+ return builder;+}++vi.mock('@metaplex-foundation/umi-bundle-defaults', () => ({+ createUmi: (endpointOrConnection) => {+ createUmiCalls.push(endpointOrConnection);+ return { programs: { add: () => {} } };+ },+}));++vi.mock('@metaplex-foundation/umi', () => ({ publicKey: (v) => v }));++vi.mock('@metaplex-foundation/mpl-agent-registry', () => ({+ getAgentIdentityV2GpaBuilder: () => fakeGpaBuilder('v2'),+ getAgentIdentityV1GpaBuilder: () => fakeGpaBuilder('v1'),+}));++const { crawlMetaplexAgents } = await import('../api/_lib/solana-agents-crawl.js');+const { solanaConnection } = await import('../api/_lib/solana/connection.js');++describe('crawlMetaplexAgents enumerates over the rotating lane chain', () => {+ beforeEach(() => {+ createUmiCalls.length = 0;+ whereFieldCalls.length = 0;+ solanaConnection.mockClear();+ });++ it('builds umi on a rotating Connection, never on the raw lane-0 URL', async () => {+ await crawlMetaplexAgents({ deadline: Date.now() + 5_000 });++ expect(solanaConnection).toHaveBeenCalledWith({ network: 'mainnet', commitment: 'confirmed' });+ expect(createUmiCalls).toEqual([rotatingConnection]);+ // The precise regression: handing createUmi a string pins one lane and+ // loses every failover signal the chain already knows how to act on.+ expect(createUmiCalls.some((arg) => typeof arg === 'string')).toBe(false);+ });++ it('filters each version to its own key discriminator', async () => {+ await crawlMetaplexAgents({ deadline: Date.now() + 5_000 });++ expect(whereFieldCalls).toEqual([+ { label: 'v2', field: 'key', value: 2 },+ { label: 'v1', field: 'key', value: 1 },+ ]);+ });++ it('gives the two versions different keys, so the scans stay disjoint', async () => {+ await crawlMetaplexAgents({ deadline: Date.now() + 5_000 });++ const keys = whereFieldCalls.map((c) => c.value);+ expect(new Set(keys).size).toBe(keys.length);+ // 0 is Uninitialized on chain: filtering on it would scan nothing.+ expect(keys).not.toContain(0);+ });+});