Operating your app

Once an app is live you need to look inside it and fix things: correct a row, answer a question the pages don't, see why a run failed, rotate a key, undo a mistake. Every operation works on the dev or the production database, from your coding agent or the console, and every production write is snapshotted first and recorded.

When to use it

  • A customer asks you to fix their booking, or a row was entered wrong.
  • You want an answer the screens don't give ("which classes are always full?").
  • A workflow failed and you want to see why, then run it again.
  • A request was refused and you want to know which rule said no.

The tools

Each takes env: "dev" (default) or "prod". Ask in plain words ("on prod, set the capacity of Sunrise Flow to 16") and your agent picks them.

ToolWhat it does
query_dataRead rows, as the admin or as a role (what that user would see), with filters, counts and group-by aggregates. Answers the SQL it ran
sqlAny SQL as the database admin, several statements in one transaction. A script that only reads runs rolled back; as a role runs under its policies; dry_run rolls a write back; explain answers the plan
edit_rowsInsert, update, delete or move a lifecycle through the app's own data API, so policies, triggers and workflows behave as for a person. as a role to act as one
describe_schemaThe real schema: tables, columns, keys, indexes, triggers and policies, each marked as managed by db/, the platform, or made by hand
call_appOne request to the running app as a visitor, a role or the server key: an API path, an action, a page's HTML
explainWhy: a role's access to a table (about: "access"), a logged request by trace id, an error code, a verify check
logs, runWorkflow runs with their log lines; one run's steps; run a workflow now; dead letters and replay
tailRecent change events and topic messages, requests, errors, and the email outbox
statsRequests, error rate and latency, workflow runs, database size and slow statements
snapshots, deploys, migrationsBackups and restore; releases and rollback; the schema journal and its rollback
secrets, app_usersConfig values and secrets per environment; the app's accounts
run_scriptMany of the above in one TypeScript script, with loops and Promise.all; production writes only for tools you name in allow

A production edit and its undo

edit_rows { "project_id": "…", "env": "prod", "table": "class_session", "op": "update",
            "id": "…", "patch": { "capacity": 16 } }
// { "succeeded": 1, "snapshotId": "…" }: production was snapshotted first

tail { "project_id": "…", "kind": "events", "env": "prod", "since": "15m" }      // the change, as an event

snapshots { "project_id": "…", "action": "restore", "env": "prod", "id": "<that snapshotId>" }
// { "restored": "…", "undoSnapshotId": "…" }: the restore took its own snapshot first

A refused request

tail { "project_id": "…", "kind": "errors", "env": "prod", "since": "1h" }     // each with its trace_id and role
explain { "project_id": "…", "about": "request", "trace_id": "…", "env": "prod" }
// the request, which layer refused it and why, the error's meaning, and the workflow runs it started

A test plan in one call

run_script { "project_id": "…", "dry_run": true, "code": "
  const as = (role) => ({ kind: 'user', role });
  const [member, guest] = await Promise.all([
    tools.call_app({ path: '/__appbricx/api/v1/booking', as: as('member') }),
    tools.call_app({ path: '/__appbricx/api/v1/booking' }),
  ]);
  return { member: member.status, guest: guest.status };
" }

script_api answers the typed declarations of every tool a script can call.

Your own SQL outside db/

Prefer putting every table, index, view, function and trigger in db/: then it is versioned, checked, diffed and carried to production with the rest. Something made by hand with the sql tool shows in describe_schema as hand-made and is never dropped by an apply, but it isn't part of the project: copy it into a db/ file to keep it. A table made by hand still gets row-level security with no policies (admin only) until a policy in db/access opens it.

Secrets and config

app.config.ts declares names and rules (config, secrets with their hosts, required: ["prod"]); the values are set per environment and never enter the code, a version or the model:

secrets { "project_id": "…", "action": "set", "name": "PAYMENTS_KEY", "value": "…", "env": "prod" }
secrets { "project_id": "…", "action": "list" }      // names, env and updated-at, never values; and which declared names are missing

Server code reads config as ctx.config.NAME and pages read the public ones with useConfig(); a secret goes into an HTTP request by reference (ctx.secrets.ref), only to its hosts. Setting and deleting needs a workspace owner or admin. An apply refuses while a value required in dev is missing, a publish while one required in prod is. See server actions.

Gotchas

  • Prefer edit_rows to SQL for data. SQL as the admin goes past your policies; edit_rows behaves like the app.
  • Change the schema in db/, not with alter table: the next apply plans from your files.
  • Scopes matter. Reads need read; dev writes and secrets need apply; production writes, restores and rollbacks need deploy. Choose them when you authorize an agent.
  • Every agent write is audited with who made it, which tool and which environment, never a secret value.
  • Don't touch _appbricx_* tables or the ctx schema: they are the platform's.

Related