Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions agent/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,9 @@ 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>();
// Messages running now, per IP and in total.
const dryRunRunning = new Map<string, number>();
let dryRunRunningTotal = 0;
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) => {
Expand All @@ -692,8 +694,11 @@ export async function buildApp(deps: AppDeps): Promise<FastifyInstance> {
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);
const mine = dryRunRunning.get(client) ?? 0;
if (mine >= LIMITS.dryRunsAtOncePerIp) return reply.code(409).send({ error: "busy" });
if (dryRunRunningTotal >= LIMITS.dryRunsAtOnceTotal) return reply.code(503).send({ error: "dry_run_full" });
dryRunRunning.set(client, mine + 1);
dryRunRunningTotal++;
try {
const allowed = dryRunQuota.take(client);
if (allowed !== "ok") {
Expand Down Expand Up @@ -725,7 +730,10 @@ export async function buildApp(deps: AppDeps): Promise<FastifyInstance> {
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);
const left = (dryRunRunning.get(client) ?? 1) - 1;
if (left > 0) dryRunRunning.set(client, left);
else dryRunRunning.delete(client);
dryRunRunningTotal--;
}
});

Expand Down
6 changes: 5 additions & 1 deletion agent/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ export const LIMITS = {
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,
dryRunsPerIpPerDay: 100,
dryRunsPerDayTotal: 300,
/** Dry-run messages running at once from one IP: judges in one room share a network. */
dryRunsAtOncePerIp: 3,
/** Dry-run messages running at once for everyone: bounds the model calls in flight. */
dryRunsAtOnceTotal: 10,
/** 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. */
Expand Down
35 changes: 31 additions & 4 deletions agent/server/dryRun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 { Brain } from "../core/types.js";
import type { AgentEvent } from "../core/tools.js";
import { ASSETS } from "../policy/assets.js";
import { HELD_ASSETS } from "../policy/heldAssets.js";
Expand All @@ -11,6 +12,7 @@ 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 { LIMITS } from "./config.js";
import { DailyQuota } from "./quota.js";

const AGENT_ID = "e".repeat(64);
Expand Down Expand Up @@ -122,10 +124,10 @@ describe("POST /api/dry-run/chat", () => {
() => ({ 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) =>
const server = (quota?: DailyQuota, brain: Brain = buyThenReport()) =>
buildApp({
verify: () => Promise.reject(new Error("no sign-in here")),
brain: buyThenReport(),
brain,
tokens: () => Promise.resolve([...fixture.tokens, ...TOKENS]),
agents: {} as AgentAccounts,
grants: {} as GrantStore,
Expand All @@ -138,8 +140,8 @@ describe("POST /api/dry-run/chat", () => {
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) });
const send = (app: Awaited<ReturnType<typeof server>>, payload: unknown, remoteAddress = "127.0.0.1") =>
app.inject({ method: "POST", url: "/api/dry-run/chat", remoteAddress, 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" });
Expand All @@ -160,6 +162,31 @@ describe("POST /api/dry-run/chat", () => {
expect(second.json()).toEqual({ error: "user_limit" });
});

it("runs a few messages at once per network and caps how many run in total", async () => {
const releases: (() => void)[] = [];
const waiting: Brain = {
name: "waiting",
step: () => new Promise((r) => releases.push(() => r({ role: "assistant", text: "done", toolCalls: [], stop: "end" }))),
};
const app = await server(undefined, waiting);
const ips = ["10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4"];
const running = Array.from({ length: LIMITS.dryRunsAtOnceTotal }, (_, i) => send(app, { message: "hi" }, ips[Math.floor(i / LIMITS.dryRunsAtOncePerIp)]));
await new Promise((r) => setTimeout(r, 10));
const sameNetwork = await send(app, { message: "hi" }, "10.0.0.1");
expect(sameNetwork.statusCode).toBe(409);
expect(sameNetwork.json()).toEqual({ error: "busy" });
const full = await send(app, { message: "hi" }, "10.0.0.9");
expect(full.statusCode).toBe(503);
expect(full.json()).toEqual({ error: "dry_run_full" });
releases.forEach((release) => release());
expect((await Promise.all(running)).map((r) => r.statusCode)).toEqual(Array(LIMITS.dryRunsAtOnceTotal).fill(200));
releases.length = 0;
const after = send(app, { message: "hi" }, "10.0.0.1");
await new Promise((r) => setTimeout(r, 10));
releases.forEach((release) => release());
expect((await after).statusCode).toBe(200);
});

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);
Expand Down
3 changes: 2 additions & 1 deletion web/src/dryrun/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const MESSAGES: Record<string, string> = {
user_limit: "You reached today's limit of dry-run messages. Open the app to keep going.",
server_limit: "The dry run is very busy today. Try again tomorrow, or open the app.",
rate_limited: "Too many messages in a minute. Wait a moment and retry.",
busy: "I'm still working on your last message.",
busy: "A few dry-run messages from your network are still running. Wait for one to finish.",
dry_run_full: "Many people are trying the dry run right now. Try again in a minute.",
agent_paused: "Inti is paused for maintenance. Try again later.",
agent_unavailable: "I couldn't reach my tools right now. Try again in a minute.",
};
Expand Down
Loading