Skip to content
4 changes: 3 additions & 1 deletion agent/core/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export interface CycleOptions {
/** A set of tools the cycle can offer instead of the simulated ones. */
export interface Toolset {
specs: readonly ToolSpec[];
/** The instructions for these tools; without them, the prompt for real money. */
prompt?: string;
/** Runs one call and returns JSON for the model; throws ToolInputError on bad input. */
run(name: string, input: unknown, events: AgentEvent[]): Promise<string>;
}
Expand Down Expand Up @@ -119,7 +121,7 @@ export async function runCycle(options: CycleOptions): Promise<CycleResult> {

for (let step = 0; step < maxSteps; step++) {
if (options.deadline !== undefined && ctx.now() >= options.deadline) return { history, events, report: "", stopReason: "time_limit" };
const turn = await brain.step(options.tools ? REAL_SYSTEM_PROMPT : SYSTEM_PROMPT, history, options.tools?.specs ?? toolSpecs);
const turn = await brain.step(options.tools ? (options.tools.prompt ?? REAL_SYSTEM_PROMPT) : SYSTEM_PROMPT, history, options.tools?.specs ?? toolSpecs);
history.push(turn);
if (turn.stop === "end" || turn.toolCalls.length === 0) {
return { history, events, report: turn.text, stopReason: "done" };
Expand Down
61 changes: 61 additions & 0 deletions agent/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { Dashboard } from "./dashboard.js";
import { LIMITS } from "./config.js";
import { GRANT_DAY_CHOICES, GRANT_DAYS, newGrantRequest, usableGrant, type GrantRecord, type GrantStore } from "./grants.js";
import type { ChainPort } from "./orders.js";
import { dryRunTools, practiceDashboard, type DryRunDeps, type DryRunRecord } from "./dryRun.js";
import { DailyQuota } from "./quota.js";
import { realTools } from "./realTools.js";
import type { Trading } from "./trading.js";
Expand Down Expand Up @@ -50,6 +51,9 @@ export interface AppDeps {
agentQuota?: DailyQuota;
verifyQuota?: DailyQuota;
heldSaleQuota?: DailyQuota;
/** Where dry-run quotes come from; without it, the dry run prices with the live token list. */
dryRun?: Omit<DryRunDeps, "tokens">;
dryRunQuota?: DailyQuota;
logger?: boolean;
}

Expand Down Expand Up @@ -668,6 +672,63 @@ export async function buildApp(deps: AppDeps): Promise<FastifyInstance> {
}
});

// Dry run: open to anyone, no sign-in, no wallet. The tools can only read prices and quotes, so
// nothing here can move money; the caps per IP and per day bound what it costs in model calls.
const dryRunQuota = deps.dryRunQuota ?? new DailyQuota(LIMITS.dryRunsPerIpPerDay, LIMITS.dryRunsPerDayTotal);
const dryRunBusy = new Set<string>();
const dryRunSource = deps.dryRun ?? { quoteAgent: async () => null, agent: { quote: async () => null } };
// The practice wallet's dashboard: live prices and the rules every agent signs. No model call.
app.get("/api/dry-run/dashboard", async (req, reply) => {
try {
return practiceDashboard(await deps.tokens(), now());
} catch (error) {
req.log.error({ err: error instanceof Error ? error.message : "unknown" }, "dry_run_dashboard_failed");
return reply.code(502).send({ error: "agent_unavailable" });
}
});

app.post("/api/dry-run/chat", { config: { rateLimit: { max: LIMITS.dryRunsPerIpPerMinute, timeWindow: "1 minute" } } }, async (req, reply) => {
const body = chatBody.safeParse(req.body);
if (!body.success) return reply.code(400).send({ error: "bad_request" });
if (!deps.agentEnabled) return reply.code(503).send({ error: "agent_paused" });
const client = req.ip;
if (dryRunBusy.has(client)) return reply.code(409).send({ error: "busy" });
dryRunBusy.add(client);
try {
const allowed = dryRunQuota.take(client);
if (allowed !== "ok") {
req.log.warn({ cap: allowed }, "dry_run_quota_refused");
return reply.code(429).send({ error: allowed });
}
const tokens = await deps.tokens();
const record: DryRunRecord = { log: [], steps: [] };
const result = await runCycle({
brain: deps.brain,
goal: body.data.message,
policy: deps.policy(),
portfolio: deps.newPortfolio(tokens),
mode: "execute",
tools: dryRunTools({ ...dryRunSource, tokens: deps.tokens }, record),
maxSteps: LIMITS.dryRunSteps,
deadline: now() + LIMITS.dryRunTimeLimitMs,
});
req.log.info({ stopReason: result.stopReason, tools: record.log.length }, "dry_run");
return {
dryRun: true,
report: result.report,
stopReason: result.stopReason,
events: result.events.map(publicEvent),
log: record.log,
steps: record.steps,
};
} catch (error) {
req.log.error({ err: error instanceof Error ? error.message : "unknown" }, "dry_run_failed");
return reply.code(502).send({ error: "agent_unavailable" });
} finally {
dryRunBusy.delete(client);
}
});

// What the signed-in user's message is doing: its steps while it runs, then the reply POST
// /api/chat used to wait for. Only the user's own job is found; it has its own rate limit, since
// the app asks every few seconds.
Expand Down
8 changes: 8 additions & 0 deletions agent/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ export const LIMITS = {
chatJobKeepMs: 15 * 60_000,
/** Progress reads per IP per minute: the app asks every few seconds while a message runs. */
chatPollsPerIpPerMinute: 120,
/** Dry-run messages, open to anyone without signing in: per IP per minute and per day, and in total per day. */
dryRunsPerIpPerMinute: 5,
dryRunsPerIpPerDay: 20,
dryRunsPerDayTotal: 300,
/** Model steps one dry-run message may take: the portfolio, a quote or two, and the reply. */
dryRunSteps: 6,
/** After this long no new step of a dry-run message starts. */
dryRunTimeLimitMs: 90_000,
} as const;

const envSchema = z.object({
Expand Down
178 changes: 178 additions & 0 deletions agent/server/dryRun.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { ScriptedBrain } from "../brains/scripted.js";
import { demoPolicy, demoPortfolio } from "../core/demo.js";
import type { PricedToken } from "../core/portfolio.js";
import type { AgentEvent } from "../core/tools.js";
import { ASSETS } from "../policy/assets.js";
import { HELD_ASSETS } from "../policy/heldAssets.js";
import type { AgentAccounts } from "./agents.js";
import { buildApp } from "./app.js";
import { dryRunTools, LOG_LINES, partnerQuoteAgent, QUOTES_PER_MESSAGE, type DryRunRecord } from "./dryRun.js";
import type { GrantStore } from "./grants.js";
import { FUNDING_ASSET } from "./orders.js";
import { DailyQuota } from "./quota.js";

const AGENT_ID = "e".repeat(64);
const TOKENS: PricedToken[] = [
{ asset_id: ASSETS.bitcoin.assetId, symbol: "BTC", decimals: 8, blockchain: "btc", price: 100_000 },
{ asset_id: HELD_ASSETS.gold.assetId, symbol: "XAUT", decimals: 6, blockchain: "eth", price: 4000 },
{ asset_id: FUNDING_ASSET, symbol: "wNEAR", decimals: 24, blockchain: "near", price: 2.5 },
];

function setup(agentId: string | null = AGENT_ID) {
const quotes: { agentId: string; from: string; to: string; amount: bigint }[] = [];
const record: DryRunRecord = { log: [], steps: [] };
const tools = dryRunTools(
{
tokens: () => Promise.resolve(TOKENS),
quoteAgent: () => Promise.resolve(agentId),
agent: {
quote: (id, from, to, amount) => {
quotes.push({ agentId: id, from, to, amount });
return Promise.resolve({ amountOut: 4945n, minAmountOut: 4895n, costUsd: "0.0065", costPercent: "0.19", seconds: 12 });
},
},
},
record,
);
return { tools, record, quotes };
}

describe("dry-run tools", () => {
it("offers no tool that moves money", () => {
const { tools } = setup();
expect(tools.specs.map((s) => s.name)).toEqual(["get_portfolio", "get_quote", "buy_now", "ask_user"]);
});

it("refuses the real money tools by name", async () => {
const { tools, quotes } = setup();
for (const name of ["sell_now", "buy_at_price", "send_near_to_wallet", "transfer"]) {
await expect(tools.run(name, {}, [])).rejects.toThrow("unknown tool");
}
expect(quotes).toEqual([]);
});

it("a buy asks NEAR Intents for a dry quote and shows where the money would go, buying nothing", async () => {
const { tools, record, quotes } = setup();
const events: AgentEvent[] = [];
const out = JSON.parse(await tools.run("buy_now", { asset: "bitcoin", usd: "5", reason: "asked" }, events)) as Record<string, unknown>;
expect(out).toMatchObject({ status: "dry_run", nothing_bought: true, receive: "0.00004945", ends_in: "wallet" });
// 5 USD at 2.5 USD per NEAR is 2 NEAR.
expect(quotes).toEqual([{ agentId: AGENT_ID, from: FUNDING_ASSET, to: ASSETS.bitcoin.assetId, amount: 2n * 10n ** 24n }]);
expect(record.log[0]?.request?.body).toMatchObject({ dry: "true", amount: (2n * 10n ** 24n).toString() });
expect(record.steps.map((s) => s.place)).toEqual(["wallet", "agent", "intents", "wallet"]);
expect(events.map((e) => e.type)).toEqual(["swap_proposed"]);
});

it("gold ends with the agent, as in the real app", async () => {
const { tools, record } = setup();
await tools.run("buy_now", { asset: "gold", usd: "5", reason: "asked" }, []);
expect(record.steps.at(-1)?.place).toBe("agent");
});

it("a buy above the practice limit per order is refused before any quote", async () => {
const { tools, quotes } = setup();
const out = JSON.parse(await tools.run("buy_now", { asset: "bitcoin", usd: "26", reason: "asked" }, [])) as { status: string };
expect(out.status).toBe("refused");
expect(quotes).toEqual([]);
});

it("asks the trading service for at most a few quotes per message", async () => {
const { tools, quotes } = setup();
for (let i = 0; i < QUOTES_PER_MESSAGE; i++) await tools.run("get_quote", { asset: "bitcoin", usd: "5" }, []);
await expect(tools.run("get_quote", { asset: "bitcoin", usd: "5" }, [])).rejects.toThrow("at most");
expect(quotes).toHaveLength(QUOTES_PER_MESSAGE);
});

it("stops running tools once the log of one message is full", async () => {
const { tools, record } = setup();
for (let i = 0; i < LOG_LINES; i++) await tools.run("get_portfolio", {}, []);
await expect(tools.run("get_portfolio", {}, [])).rejects.toThrow("too many steps");
expect(record.log).toHaveLength(LOG_LINES);
});

it("without an agent to ask, it prices with the live token list and says so in the log", async () => {
const { tools, record, quotes } = setup(null);
const out = JSON.parse(await tools.run("get_quote", { asset: "bitcoin", usd: "5" }, [])) as { receive: string };
expect(out.receive).toBe("0.00005");
expect(quotes).toEqual([]);
expect(record.log[0]).toMatchObject({ request: null, answer: { source: "live_prices" } });
});
});

describe("partner quote agent", () => {
it("picks an active agent and never sends the key anywhere but the header", async () => {
const seen: { url: string; key: string | null }[] = [];
const fetcher = ((url: URL, init: RequestInit) => {
seen.push({ url: url.toString(), key: new Headers(init.headers).get("X-API-Key") });
return Promise.resolve(new Response(JSON.stringify({ data: [{ id: "a".repeat(64), status: "PENDING" }, { id: AGENT_ID, status: "ACTIVE" }] })));
}) as unknown as typeof fetch;
const find = partnerQuoteAgent("partner-key", "https://agents.example", fetcher);
expect(await find()).toBe(AGENT_ID);
expect(await find()).toBe(AGENT_ID);
expect(seen).toEqual([{ url: "https://agents.example/v1/agents", key: "partner-key" }]);
});
});

describe("POST /api/dry-run/chat", () => {
const fixture = JSON.parse(readFileSync(new URL("../core/fixtures/priced-tokens.json", import.meta.url), "utf8")) as { tokens: PricedToken[] };
const buyThenReport = () =>
new ScriptedBrain([
() => ({ text: "", stop: "tool_use", toolCalls: [{ id: "1", name: "buy_now", input: { asset: "bitcoin", usd: "5", reason: "asked" } }] }),
() => ({ text: "Dry run: nothing was bought.", stop: "end", toolCalls: [] }),
]);
const server = (quota?: DailyQuota) =>
buildApp({
verify: () => Promise.reject(new Error("no sign-in here")),
brain: buyThenReport(),
tokens: () => Promise.resolve([...fixture.tokens, ...TOKENS]),
agents: {} as AgentAccounts,
grants: {} as GrantStore,
policy: demoPolicy,
newPortfolio: demoPortfolio,
allowedOrigins: ["https://example.com"],
agentEnabled: true,
trustedProxy: null,
logger: false,
dryRun: { quoteAgent: () => Promise.resolve(AGENT_ID), agent: { quote: () => Promise.resolve({ amountOut: 4945n, minAmountOut: 4895n, costUsd: null, costPercent: null, seconds: null }) } },
...(quota ? { dryRunQuota: quota } : {}),
});
const send = (app: Awaited<ReturnType<typeof server>>, payload: unknown) =>
app.inject({ method: "POST", url: "/api/dry-run/chat", headers: { "content-type": "application/json" }, payload: JSON.stringify(payload) });

it("answers without signing in, with the log and the steps of the money", async () => {
const res = await send(await server(), { message: "buy 5 dollars of bitcoin" });
expect(res.statusCode).toBe(200);
const body = res.json<{ dryRun: boolean; report: string; log: unknown[]; steps: unknown[] }>();
expect(body.dryRun).toBe(true);
expect(body.report).toBe("Dry run: nothing was bought.");
expect(body.log).toHaveLength(1);
expect(body.steps).toHaveLength(4);
expect(res.headers["cache-control"]).toBe("no-store");
});

it("refuses a message past the per-client daily cap", async () => {
const app = await server(new DailyQuota(1, 100));
expect((await send(app, { message: "hi" })).statusCode).toBe(200);
const second = await send(app, { message: "hi" });
expect(second.statusCode).toBe(429);
expect(second.json()).toEqual({ error: "user_limit" });
});

it("shows the practice dashboard without signing in: live prices and the rules every agent signs", async () => {
const res = await (await server()).inject({ method: "GET", url: "/api/dry-run/dashboard" });
expect(res.statusCode).toBe(200);
const body = res.json<{ prices: Record<string, number | null>; agent: { account: string; policy: { sendsOnlyToOwner: boolean; canBuyNow: boolean; destinations: string[] }; held: unknown[] } }>();
expect(body.prices.bitcoin).toBe(100_000);
expect(body.agent.account).toBe("practice-agent");
expect(body.agent.policy).toMatchObject({ sendsOnlyToOwner: true, canBuyNow: true, destinations: ["0".repeat(64)] });
expect(body.agent.held).toEqual([]);
});

it("refuses unknown fields and messages that are too long", async () => {
const app = await server();
expect((await send(app, { message: "hi", tools: ["sell_now"] })).statusCode).toBe(400);
expect((await send(app, { message: "x".repeat(1001) })).statusCode).toBe(400);
});
});
Loading
Loading