From 25277fc95f7d1cae173e7c4337e11b5fc5298740 Mon Sep 17 00:00:00 2001 From: JA VI <127710324+JaDi03@users.noreply.github.com> Date: Sat, 14 Nov 2026 09:23:31 -0600 Subject: [PATCH] feat(server): dry run keeps the buys of a message within the practice daily limit --- agent/server/dryRun.test.ts | 11 +++++++++++ agent/server/dryRun.ts | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/agent/server/dryRun.test.ts b/agent/server/dryRun.test.ts index 760316e..8e8673a 100644 --- a/agent/server/dryRun.test.ts +++ b/agent/server/dryRun.test.ts @@ -80,6 +80,17 @@ describe("dry-run tools", () => { expect(quotes).toEqual([]); }); + it("the buys of one message together stay within the practice limit per day", async () => { + const { tools, quotes, record } = setup(); + const buy = async (usd: string) => (JSON.parse(await tools.run("buy_now", { asset: "bitcoin", usd, reason: "asked" }, [])) as { status: string }).status; + expect(await buy("20")).toBe("dry_run"); + expect(await buy("20")).toBe("dry_run"); + expect(await buy("20")).toBe("refused"); + expect(quotes).toHaveLength(2); + expect(record.log.at(-1)?.answer).toEqual({ status: "refused", code: "over_day_limit" }); + expect(await buy("10")).toBe("dry_run"); + }); + 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" }, []); diff --git a/agent/server/dryRun.ts b/agent/server/dryRun.ts index d573c67..f42f897 100644 --- a/agent/server/dryRun.ts +++ b/agent/server/dryRun.ts @@ -118,6 +118,8 @@ const answerOf = (q: SwapQuote): Record => ({ /** The tools of one dry-run message; every call is written to `record` for the app to show. */ export function dryRunTools(deps: DryRunDeps, record: DryRunRecord): Toolset & { prompt: string } { + // Each message starts a fresh practice day: its buys together stay within the daily limit. + let spentMicro = 0n; const prices = async () => { const list = await deps.tokens(); const byId = new Map(list.map((t) => [t.asset_id, t])); @@ -194,10 +196,16 @@ export function dryRunTools(deps: DryRunDeps, record: DryRunRecord): Toolset & { events.push({ type: "swap_denied", from: "NEAR", amountIn: input.usd, code: "over_order_limit", reason: "above the practice limit per order" }); return JSON.stringify({ status: "refused", reason: `the amount is above the practice limit of ${PRACTICE_PER_ORDER_USD} USD per order` }); } + if (name === "buy_now" && spentMicro + usdStringToMicro(input.usd) > usdStringToMicro(PRACTICE_PER_DAY_USD)) { + record.log.push({ tool: name, asked, request: null, answer: { status: "refused", code: "over_day_limit" } }); + events.push({ type: "swap_denied", from: "NEAR", amountIn: input.usd, code: "over_day_limit", reason: "above the practice limit per day" }); + return JSON.stringify({ status: "refused", reason: `together with the earlier buys this is above the practice limit of ${PRACTICE_PER_DAY_USD} USD per day` }); + } const q = await quote(name, asked, a.assetId, wnear, token, nearMicro); if (!q) return JSON.stringify({ status: "refused", reason: "there is no way to trade this right now, often because the amount is too small for this asset's market" }); const view = quoteView(input.usd, wnear, q, token.decimals, priceToMicro(token.price ?? 0)); if (name === "get_quote") return JSON.stringify({ status: "quoted", asset: a.name, ...view }); + spentMicro += usdStringToMicro(input.usd); const kept = staysOnAgent(a.assetId); record.steps = [ { place: "wallet", text: `${view.pay_near} NEAR (${input.usd} USD) waits in your wallet, set aside for orders. Your Spending Limits contract caps each order and each day.` },