#39 feat: agent review and undo, security and usage limits, observability
Originally created by: mxchristian17
What changed
Three steps toward taking the modeling agent out of beta: reviewing and undoing what it does, keeping it safe and within usage limits, and making each turn observable.
Review and undo
- Preview in Ask before running: each batch is tried on a copy of the drawing and shown as a ghost in the view (green: what it would create or how a changed object ends up; red: what it would delete or move away). Run (Enter) runs it; Reject (Esc) leaves no trace (no undo step, no command line output, selection, saved state and variables untouched). Long batches, very large drawings and commands that act outside the drawing are shown only as text, with the reason.
- Turn card: what the turn created, changed and deleted (by type), the geometry warnings that remain, tokens and time. Show highlights what it touched; Undo turn undoes the whole turn in one step (disabled, with the reason, once the drawing changed afterwards).
- Conversations: rename, export (JSON without API keys, with a draft evaluation case) and delete. Server:
DELETE /agent/conversations/{id}(own conversations only; 404 otherwise). - The agent's text is rendered with minimal, safe formatting (never HTML). Autosave recovery only offers drawings with unsaved changes.
Safety and usage limits
- Sensitive commands: the agent can never run AGENT, COMPACT, PROMPT, SCRIPT, TRACEBACK, OPTIONS, LOGIN, CLOUD or OPENFOLDER (the command engine rejects them inside the agent's batches). Commands that act outside the drawing or cannot be undone (NEW, OPEN, SAVE, EXPORT, IMPORT, PLOT, PURGE, the clipboard…) always wait for the user's OK, also in Run automatically and for external agents. Aliases count; the preview now resolves them too.
- Text in the drawing is data: texts, dimension overrides and names reach the model marked as ⟦…⟧, with a rule not to follow instructions written there. New evaluation case (32 cases in total).
- Privacy notice in the panel (what goes to the AI provider), shown once and never blocking.
- Server limits: model calls per user and minute, an optional daily token budget per user and a cap on saved conversations, counted in the database so they hold with several API instances. Over a limit,
/agent/stepanswers 429 withX-Agent-Error: limitandRetry-After; the panel shows the wait and Continue. The web container's nginx also limits/api/agent/per IP address (zoneagent). - External agent bridge (
cota-mcp.mjs1.1.0, protocol 2): pairing with a token. The first time a tab connects, Cota asks the user to allow it and shows the session code the bridge printed; only that tab gets the bridge's token, and the bridge refuses everything else until it restarts.--no-pairis deprecated (connects without a token, still asking once per connection) and will be removed in the next version.
Observability
- Turn card adds the number of model calls («2 calls · 1.4 s · 1,160 tokens»; the tooltip splits input and output tokens).
- TRACEBACK records the agent's turns as
agentevents (turn start and end, the first line of each tool result, the error class, Undo turn; never API keys) and each batch as the new store actionrunAgentScript.tools/replay-traceback.mjsreplays a turn as one undo step without calling the model (typings intools/replay-traceback.d.mts). - Server log: one
agent_stepline per step on thecota.agentlogger (user, provider, model, latency, tokens, outcome or error class; never content, keys or base URL), also as a dict for a JSON formatter. GET /agent/usage: steps and tokens today and in the last 30 days (UTC) withrate_per_minuteanddaily_token_budget; the agent settings show it in server mode.
Self-hosting
New API variables (see docs/self-hosting.md):
| Variable | Default | |
|---|---|---|
AGENT_RATE_PER_MINUTE |
30 |
Agent model calls per user and minute; 0 turns it off |
AGENT_DAILY_TOKEN_BUDGET |
0 (off) |
Agent tokens per user and UTC day |
AGENT_MAX_CONVERSATIONS |
200 |
Saved agent conversations per user |
Migration cota_004 (after cota_003): table agent_usage and column agent_conversation.meta. Run alembic upgrade head (the Docker image does it on start). Observability needs no migration.
Docs: docs/guides/agent.md, docs/external-agents.md, docs/self-hosting.md, docs/architecture.md, docs/reference/help.md (TRACEBACK), services/web/README.md and CHANGELOG.md (Unreleased). main is merged into the branch.
How it was tested
- [x]
npm run typecheck(tsc --noEmit) andnpx eslint .: clean - [x]
npm test(vitest run): 212 files passed (1 skipped), 1679 tests passed (3 skipped), including the agent evaluation replay with 32 cases - [x]
npm run test:e2efortests/e2e/agent.spec.ts: 22/22 passed (2 workers) - [x]
pytestandruff check .: ruff clean; pytest 63 passed, 1 skipped on SQLite and 64 passed on PostgreSQL (COTA_TEST_PG_URL); migrationupgrade head→downgrade cota_003→upgrade headon PostgreSQL - [x] Generated files:
public/cota-mcp.mjsregenerated (UPDATE_DOCS=1 npx vitest run src/agent/external.test.ts); command reference unchanged
To try it:
cd services/api && alembic upgrade head && pytest
cd services/web && npm run typecheck && npm test && npx playwright test tests/e2e/agent.spec.ts
npm run dev # open the agent panel (AGENT), set a key, ask for a part in "Ask before running":
# check the ghost preview, Reject/Run, the turn card (calls, tokens, time), Undo turn,
# then TRACEBACK and: node tools/replay-traceback.mjs traceback.txt --headed
With the Docker stack and an account: the agent settings (⚙) show the usage on the server; with AGENT_RATE_PER_MINUTE=1 a second step in the same minute pauses with the wait and Continue; the API log shows one agent_step line per step.
Generated by Claude Code
Ticket changed by: mxchristian17
Ticket changed by: mxchristian17