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
Programmable orders: halt terminal-block fills correctly; complete task
nirholas20f89ffb71b5
diff --git a/tasks/trading-frontier/02-programmable-orders-engine.md b/tasks/trading-frontier/02-programmable-orders-engine.mddeleted file mode 100644index 6b8ee3df9..000000000--- a/tasks/trading-frontier/02-programmable-orders-engine.md+++ /dev/null@@ -1,85 +0,0 @@-# Task 02 — Programmable Orders Engine (limit / stop / DCA / TWAP / conditional triggers)--> **Operating bar.** Senior engineer + product thinker building three.ws to beat the best.-> Genuinely innovative, not a clone. No mocks/fake data/placeholders/TODO/stubs/`setTimeout`-> fake-loading. Wire 100% with REAL APIs + on-chain data. Only coin is **$THREE**-> (`FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump`); runtime mints in generic plumbing are the only-> exception, never promoted.--## ⛓ Chain protocol — STEP 0, before building--Node in a self-extending chain (see `tasks/trading-frontier/README.md`). Before building:-read all `tasks/` + `BACKLOG.md`; invent **10 genuinely new, non-duplicate, real-codebase-grounded-feature ideas**; write each as a full prompt in `tasks/trading-frontier/next/<slug>.md` matching-this file's structure (including this Chain protocol section); append each to `BACKLOG.md` after a-dedup check. Only then build YOUR feature to the production-ready bar (epic README) and `git rm`-this file in the completion commit.--## The invention--Pump.fun has no native order types — you watch a chart and click frantically. We give every agent-wallet a real **programmable order layer**: limit buys/sells, stop-loss + trailing stops that-survive across sessions, **DCA** (recurring buys), **TWAP** (slice a large order over time to cut-impact), and **conditional triggers** ("buy when smart-money score > X", "sell if dev dumps",-"buy on graduation"). A worker watches the market and fires from the agent wallet automatically.-Set-and-forget institutional order tooling for memecoins.--## Context (real, verified)--- Execution + quotes: `api/agents/agent-trade.js`, `api/_lib/pump.js` (curve + AMM quotes),- `api/_lib/pump-swap-ix.js`; spend guards `api/_lib/agent-trade-guards.js`; firewall- `tasks/next-gen-trading/01`; MEV engine `tasks/next-gen-trading/02`.-- Worker loop + position lifecycle precedent: `workers/agent-sniper/index.js`,- `workers/agent-sniper/positions.js` (`runPositionSweep`, `decideExit`), per-agent lock pattern.-- Trigger inputs: live price re-quote (curve/AMM), intel (`pump_coin_intel`), smart-money- (`tasks/next-gen-trading/03`), graduation events (`api/_lib/pumpfun-ws-feed.js`).-- Custody audit: `agent_custody_events`.--## Goal--An `orders` model + a worker that evaluates triggers against live market data and executes-firewall+spend-guarded fills from the agent wallet, with a full order-management UI.--## What to build--1. **Order model** — `orders` table (id, agent_id, user_id, network, mint, type- [limit|stop|trailing|dca|twap|conditional], side, size, limit_price/stop_price/trail_pct,- schedule (for dca/twap: interval, slices, total), condition jsonb (for conditional triggers),- status [active|partial|filled|cancelled|expired], filled_amount, expires_at, created_at) +- `order_fills` audit. Dated migration.-2. **Evaluation worker** — extend `workers/agent-sniper/` (or sibling) to sweep active orders on a- cadence: re-quote each order's mint, evaluate its trigger/schedule, and on fire, execute through- the firewall + MEV engine + spend guards, recording fills. DCA/TWAP advance their schedule and- partial-fill; trailing stops track the high-water mark like `decideExit`. Idempotent; per-agent- serialized; honest on RPC failure (retry, don't double-fire).-3. **Condition language** — a safe, validated condition spec (no arbitrary code) for triggers over- real signals: price thresholds, smart-money score, dev-dump flag, graduation, market-cap bands.-4. **API + UI** — `/api/orders` (CRUD, cancel), `/api/orders/:id` (state + fills), SSE for live- order status. Build an **Orders** surface in the wallet hub: create order (per-type forms with- sane presets + a live preview of the fill condition + firewall verdict), an open-orders table- with cancel, and a fills history. All states designed; accessible; responsive.--## Constraints--- Every fill is real, firewall-gated, spend-guarded, audited; kill switch halts all orders; cancel- is instant. Orders can never exceed daily budget or per-trade caps.-- Triggers evaluate against **real live data** only — never simulated prices. Honest on data gaps.-- No arbitrary-code conditions; validated spec only. $THREE-only rule; runtime mints are data.--## Success criteria--- Each order type (limit/stop/trailing/DCA/TWAP/conditional) fires correctly from real market data- through the firewall + MEV engine, audited, with partials handled.-- Orders UI renders all states; cancel + kill instant. Production-ready bar met; chain extended.-- Build/typecheck/test clean. Changelog entry (tags: feature). Completionist passes.--<!-- AUTO:self-delete-on-complete -->-------## ✅ On completion — delete this file (after authoring + registering 10 chained prompts)--```bash-git rm "tasks/trading-frontier/02-programmable-orders-engine.md"-```-A file that still exists is unfinished work; a file that is gone has shipped. Do not delete early.diff --git a/workers/agent-orders/store.js b/workers/agent-orders/store.jsindex 24d8b4a13..796bacae0 100644--- a/workers/agent-orders/store.js+++ b/workers/agent-orders/store.js@@ -86,9 +86,11 @@ export async function seedReference(orderId, value) {* @param {object} o.order the order row* @param {object} o.fill { sliceIndex, triggerReason, triggerPrice, solAmount, tokenAmount, priceImpactPct, venue, signature, custodyEventId, status, detail, meta }* @param {boolean} o.terminal true → order is fully filled (price/conditional, or last slice)+ * @param {boolean} [o.terminalError] true → a FAILED fill that should halt the order to 'error'+ * (a block that won't clear by retrying), not return to active* @param {string|null} o.nextFireAt ISO for the next scheduled slice (dca/twap), or null*/-export async function recordFillAndAdvance({ order, fill, terminal, nextFireAt = null }) {+export async function recordFillAndAdvance({ order, fill, terminal, terminalError = false, nextFireAt = null }) {const confirmedSol = fill.status === 'failed' ? 0 : Number(fill.solAmount || 0);const confirmedTok = fill.status === 'failed' ? 0 : Number(fill.tokenAmount || 0);@@ -104,11 +106,13 @@ export async function recordFillAndAdvance({ order, fill, terminal, nextFireAt =${fill.detail || null}, ${fill.meta ? JSON.stringify(fill.meta) : null}::jsonb)`;- // A failed fill does not advance fills/budget and must not consume a slice —- // it returns the order to its prior state to retry next sweep.+ // A failed fill does not advance fills/budget and must not consume a slice. A+ // terminal failure (a block that won't clear by retrying) halts the order to+ // 'error'; an ordinary failure returns it to its prior state to retry next sweep.if (fill.status === 'failed') {+ const nextStatus = terminalError ? 'error' : (order.fill_count > 0 ? 'partial' : 'active');await sql`- UPDATE orders SET status = ${order.fill_count > 0 ? 'partial' : 'active'},+ UPDATE orders SET status = ${nextStatus},last_error = ${(fill.detail || 'fill_failed').slice(0, 280)}, updated_at = now()WHERE id = ${order.id} AND status = 'firing'`;diff --git a/workers/agent-orders/sweep.js b/workers/agent-orders/sweep.jsindex 42a51afda..9e41603fa 100644--- a/workers/agent-orders/sweep.js+++ b/workers/agent-orders/sweep.js@@ -42,7 +42,7 @@ function errCode(err) {// Blocks that won't clear by retrying — stop the order (status 'error') instead// of re-quoting it every sweep forever.-const TERMINAL_CODES = new Set(['firewall_block', 'graduated', 'zero_out', 'invalid_mint', 'quote_not_sol']);+const TERMINAL_CODES = new Set(['firewall_blocked', 'graduated', 'zero_out', 'invalid_mint', 'quote_not_sol']);/*** Decide whether (and how) an order fires this sweep. Returns a fire descriptor@@ -125,11 +125,12 @@ async function settle(order, fire, result, mode) {if (!result.ok) {const code = result.code || 'error';if (TERMINAL_CODES.has(code)) {+ // Record the failed attempt AND halt the order to 'error' atomically — a+ // rug verdict / graduated-buy / zero-out won't clear by retrying.await recordFillAndAdvance({- order, terminal: false,+ order, terminal: false, terminalError: true,fill: { sliceIndex: fire.sliceIndex, triggerReason: fire.reason, triggerPrice: fire.triggerPrice, status: 'failed', detail: `${code}: ${result.message || ''}`.slice(0, 280) },});- await releaseFire(order.id, 'error', `${code}: ${result.message || ''}`.slice(0, 280));log.warn('order halted', { order: order.id, type: order.type, code });return;}