settlekit/packages/agent/test/agent.test.ts
caelum0x ff11f7754e feat(lepton): nanopayment modules + production settlement spine
Lepton Agents Hackathon (Canteen × Circle × Arc): agent-native nanopayments
on Arc, built on the existing SettleKit platform.

Hackathon modules (demo-ready, RFB-aligned):
- @settlekit/agent: autonomous paying agent (Claude tool-use loop) — RFB 1/3
- @settlekit/citation-toll: per-access content + recursive royalty splits — RFB 6
- @settlekit/streaming: per-second continuous-authorization settlement — RFB 4
- @settlekit/x402-client: x402 pay-and-retry + pluggable settler (local/Circle)
- @settlekit/agent-economy: closed-loop traction harness + CLI
- apps/api /v1/lepton: public live demo surface

Production settlement spine (Phase 1):
- @settlekit/settlement-core: configurable Gateway/Circle/local providers,
  idempotency, batch accumulator, on-chain reconciliation, nonce + receipt stores
- @settlekit/wallet-fleet: registry, spending caps + kill-switch, balances,
  Circle provisioning
- x402-client: provider-settler adapter + Arc-indexer on-chain verifier
- lepton DB schema (12 tables) + persistence Pg stores (settlements, nonces,
  sources, royalty legs, wallets, streams, payees)
- apps/worker jobs: lepton-settlement-reconcile, lepton-payout-sweep,
  lepton-stream-refund (no-op until the settlement spine is wired)

First real-world traction sidecar (Phase 2):
- @settlekit/payee-registry: identity→wallet payout rule (in-memory + Pg)
- services/sidecars/rsshub-citation-toll: ingest RSS items as priced citeable
  sources, x402-gated, settle per-citation royalties via settlement-core
- shared sweepPendingRoyalties used by the worker job and the sidecar
- services/sidecars/* wired as a TS workspace area (pnpm + vitest)

Docs: LEPTON.md, LEPTON_PRODUCTION_PLAN.md. All green: full typecheck + 870 tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 14:37:38 +03:00

115 lines
3.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { compareMoney, isOk, money } from "@settlekit/common";
import { InMemoryAgentReputationStore } from "@settlekit/agent-services";
import { createLocalSettlement } from "@settlekit/x402-client";
import {
InMemorySourceRegistry,
createCitationTollRouter,
createSource,
toAgentServiceListing,
} from "@settlekit/citation-toll";
import { PayingAgent } from "../src/agent.js";
import { HeuristicDecisionEngine } from "../src/heuristic-engine.js";
const BASE = "https://toll.test";
function buildMarket() {
const registry = new InMemorySourceRegistry();
const specs = [
{ title: "Cheap byte", wallet: "0xauthor1", price: "0.001" },
{ title: "Mid byte", wallet: "0xauthor2", price: "0.003" },
{ title: "Pricey byte", wallet: "0xauthor3", price: "0.02" }, // above per-call cap
];
for (const s of specs) {
const created = createSource({
organizationId: "org_lepton",
title: s.title,
authorWallet: s.wallet,
priceUsdc: s.price,
body: `content of ${s.title}`,
});
if (!isOk(created)) throw new Error("createSource failed");
registry.add(created.value);
}
const listings = registry.all().map((src) => toAgentServiceListing(src, { baseUrl: BASE }));
const settlement = createLocalSettlement();
const router = createCitationTollRouter(registry, {
verify: settlement.verify,
distributor: () => {},
});
return { registry, listings, settlement, router };
}
describe("PayingAgent (heuristic engine, closed loop)", () => {
it("autonomously discovers and pays for services within budget", async () => {
const { listings, settlement, router } = buildMarket();
const reputation = new InMemoryAgentReputationStore();
const agent = new PayingAgent({
services: listings,
reputation,
fetcher: router,
settler: settlement.settler,
from: "0xagent",
policy: {
totalBudgetUsdc: "0.01",
maxPriceUsdc: "0.005",
maxPurchases: 5,
},
});
const result = await agent.run(
{ objective: "Gather as much cited content as the budget allows." },
new HeuristicDecisionEngine(),
);
expect(result.engine).toBe("heuristic");
expect(result.purchases.length).toBeGreaterThanOrEqual(1);
// Never exceeds the total budget.
expect(compareMoney(result.totalSpent, money("0.01"))).toBeLessThanOrEqual(0);
// Every purchase respects the per-call cap (the 0.02 source is never bought).
for (const p of result.purchases) {
expect(compareMoney(p.amount, money("0.005"))).toBeLessThanOrEqual(0);
}
const boughtPrices = result.purchases.map((p) => p.amount.amount);
expect(boughtPrices).not.toContain("0.02");
// Real settlement volume matches what the agent spent.
expect(compareMoney(settlement.ledger.totalVolume(), result.totalSpent)).toBe(0);
// It rated what it used.
for (const p of result.purchases) {
const rep = await reputation.get(p.serviceId);
expect(rep.ratingCount).toBeGreaterThanOrEqual(1);
}
});
it("stops when the budget is exhausted", async () => {
const { listings, settlement, router } = buildMarket();
const reputation = new InMemoryAgentReputationStore();
const agent = new PayingAgent({
services: listings,
reputation,
fetcher: router,
settler: settlement.settler,
from: "0xagent",
// Only enough for the single cheapest call (0.001).
policy: { totalBudgetUsdc: "0.001", maxPriceUsdc: "0.005" },
});
const result = await agent.run(
{ objective: "Spend carefully." },
new HeuristicDecisionEngine(),
);
expect(result.purchases).toHaveLength(1);
expect(result.purchases[0]?.amount.amount).toBe("0.001");
expect(compareMoney(result.remaining, money("0"))).toBeGreaterThanOrEqual(0);
});
});