Bulk, repeat, fan-out, or data-heavy work: write and run swarm scripts (inline `script-run`, named `script-upsert`, durable `launch-script-run`). Covers the script-vs-tool rubric, the authoring contract (`args` first, `ctx` second), the seed catalog, connections and secrets, files and bash over the agent-fs drive (`ctx.fs`, `ctx.sh`), `db_query`, and exposing a script as an API.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Swarm Scripts?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/desplega-ai-swarm-scripts)
---
name: swarm-scripts
description: "Bulk, repeat, fan-out, or data-heavy work: write and run swarm scripts (inline `script-run`, named `script-upsert`, durable `launch-script-run`). Covers the script-vs-tool rubric, the authoring contract (`args` first, `ctx` second), the seed catalog, connections and secrets, files and bash over the agent-fs drive (`ctx.fs`, `ctx.sh`), `db_query`, and exposing a script as an API."
---
# Swarm Scripts
A swarm script is TypeScript that runs out of process with a typed Swarm SDK. Only its return value enters your context. Use one when direct tool calls would repeat, flood your context, or need deterministic processing over many records.
## When to script
| Situation | Approach |
|---|---|
| 1 to 9 SDK calls, result fits in context | Direct tool calls. |
| 10 or more similar calls, bulk or fan-out | Script. Inline `script-run` for a one-off, a named script for reuse. |
| Fetch, parse, and transform heavy data | Script. |
| One large web fetch | Script with `ctx.stdlib.fetch`. Return only the part you need. |
| A schedule, workflow, or API script that reads or writes drive files | Script with `ctx.fs` and `ctx.sh`. |
| Multi-agent fan-out, parallel work, deterministic pipeline | Workflow. See `workflow-iterate`. |
| Work that repeats on a clock | Schedule. See `scheduling`. |
Named script only when the logic will run two or more times, by you, another agent, a schedule, or a workflow. A one-off goes inline, so the catalog does not fill with scratch saves.
Reference point: a workflow triage that took about 26 tool calls returns as one result of about 4k tokens in about 13 seconds.
Pick the run mode before you author. A short inline `script-run` is for small lookups only. A job that scans a whole population or may run past the inline cap of about 30 s starts as a durable `launch-script-run` from the start. A single `ctx.step.swarmScript` step has the same cap, so collect in bounded steps: each step takes a cursor, collects one slice in well under 30 s, and returns its rows plus the next cursor. Label each step with its slice (`collect-${page}`) so the journal checkpoints it, then compute from the collected slices in a final step. Restarting a long inline run throws away the work it already did.
Size both ends of an inline run. The `source` travels inside the tool call, so a long source (a big pasted `IN` list, an inlined dataset) overflows the call: keep such lists to the narrowest keys, query them inside the script, or move the logic to a named script (see "Upserting a large source"). A result over about 10 KB comes back truncated, and the full value spills to your own `mcp:overflow` KV namespace for 24 hours, which other agents cannot read. When the return value may be large, write it to the agent-fs drive with `ctx.fs` inside the script and return only the path and summary counts.
## Loading the tools
The script tools are deferred. Load them with your harness tool search before the first call: `script-search`, `script-run`, `script-upsert`, `script-query-types`, `script-delete`, and for durable runs `launch-script-run`, `get-script-run`, `list-script-runs`.
Run `script-query-types` before non-trivial work. It returns the live `swarm-sdk.d.ts` and stdlib declarations, including generated per-app types.
## Seed catalog
The swarm ships named scripts at global scope. Each one replaces a multi-step tool chain. `script-search` with a plain-language description finds them. Call one with `script-run` and `name` plus `args`.
| Script | Args | Use |
|---|---|---|
| `task-context-gathering` | `{ taskId, queries: [...] }` | the task plus a deduplicated multi-query memory recall |
| `smart-recall` | `{ queries: [...] }` | multi-query memory recall without the task |
| `memory-dedup-check` | `{ text, threshold? }` | near-duplicates before you store a memory |
| `delegate` | `{ agentName, task, routingReason, routingNote, parentTaskId? }` | a subtask for an agent by name, returns `{ taskId }` |
| `wait-for-task` | `{ taskId }` | waits up to about 25 s for a terminal state, returns `{ done, status, output }`; call again while `done` is false |
| `get-child-outputs` | `{ parentTaskId }` | every child with status and output |
| `complete-task` | `{ taskId, output }` | finish a task from inside a script |
| `report-progress` | `{ taskId, note }` | a progress note from inside a script |
| `swarm-overview` | `{}` | agents and task counts |
| `schedule-health`, `task-failure-audit`, `boot-triage` | see `heartbeat-runbook` | the lead's heartbeat data gathering |
## Authoring contract
The entry point takes `args` first and `ctx` second. A one-parameter `function (ctx)` receives `args` at runtime, so every `ctx.*` access throws. This is the most common cause of a failed run.
```ts
import type { ScriptContext } from "swarm-sdk";
import * as z from "zod";
export const argsSchema = z.object({ taskId: z.string(), limit: z.number().optional() });
export default async function (args: z.infer<typeof argsSchema>, ctx: ScriptContext) {
const res = await ctx.swarm.task_get({ taskId: args.taskId });
const task = ((res as { data?: unknown }).data ?? res) as { title?: string };
return { title: task?.title };
}
```
- `args` can be `undefined` when a caller passes none. Guard with `argsSchema.safeParse(args ?? {})` or optional chaining.
- Export a Zod `argsSchema` from every named script. `script-upsert` converts it to JSON Schema, so callers, schedules, and workflows see the input contract.
- Both `zod` and `zod/mini` imports work. Use `import * as z from "zod/mini"` for smaller QuickJS bundles. Mini uses functional forms: `z.optional(z.string())`, `z._default(z.number(), 10)`, and `z.string().check(z.minLength(1))`; its schemas still have `.parse()` and `.safeParse()`.
- Inline source through `script-run` runs without a typecheck. `script-upsert` typechecks before it saves. Import `ScriptContext` from `"swarm-sdk"` in inline code too, so promotion to a named script works. A worker's `script-upsert` saves at agent scope; a global-scope upsert is rejected unless you are the lead. To share a script, send the lead its name and `contentHash` to promote. When an acceptance check needs a lead-only tool, a worker cannot pass it: the worker proves the checks it can run and names the lead-gated ones in that handoff, and the lead verifies those before promotion.
- SDK methods return `Promise<unknown>`. Responses are usually wrapped: read `res?.data ?? res`. Exception: `app_query` with a literal `appId` and `query` returns rows typed from the app's columns.
- To fail a run, throw an `Error` whose message starts with a stable reason code (for example `JOB_FAILED: ...`) and carries a JSON payload. A returned `{ ok: false }` still ends the run as `completed`, so a schedule never surfaces the failure; never return `ok: false` when the run must fail.
- When a script returns an array, return a scalar count beside it (for example `{ count, rows }`). A bounded or elided reply can show an empty array as a clean zero; the count is the only reliable signal that rows existed.
- `agentId` is propagated through the `X-Agent-ID` header, so SDK calls run as you. `taskId` is not ambient: pass it in `args` when the script calls `task_storeProgress`.
- A script invoked from a workflow node may run under a workflow identity. The node may also supply arguments the script needs: when you run that script directly, pass the same arguments the node maps. A script without a strict `argsSchema` then fails deeper in, often as a not-found or resource error instead of a validation error.
- Return compact structured data. Raw logs, full HTML, big JSON arrays, and file contents stay inside the script.
- Limits: about 30 s wall clock (up to 5 minutes where the tool exposes it), 1 MB stdout. Never sleep or loop past about 25 s. Chain `wait-for-task` calls instead. The cap also applies to a script called through `ctx.step.swarmScript`; the step's `timeoutMs` does not extend it. A long LLM call, such as a 2,000-word draft, goes in a `ctx.step.rawLlm` step inside `launch-script-run`, never in a called script. A `targetType: "script"` schedule gets 60 s; a manual `script-run` gets 30 s, so a candidate that finishes well under 30 s by hand has margin on the schedule. A nested `ctx.swarm.script_run` gets its own 30 s but runs inside its caller's remaining time: run slow children as sibling calls from the outer script, not deeper in the chain. The run ends when your default export resolves. A call you did not await gets about 2 s more and is then stopped (`quickjs` aborts it at once), so await every write that must land. That makes a `Promise.race` deadline bound the run; a nested `script_run` that lost the race keeps running on the server under its own 30 s.
- Identity: under the default `native` executor, an API server that runs as root starts the script as `nobody` (uid 65534). The script's own `$TMPDIR` is writable. A raw `Bun.write` or `node:fs` write anywhere else on the API host fails with `EACCES` unless that directory is world-writable. Persist output through `/afs` or KV instead, and put any file write after the one-shot side effect so a failed write cannot block it.
### Upserting a large source
A large source pasted inline into a `script-upsert` tool call overflows the call. Write the source to a local file, then send it from bash through the same route, `POST $MCP_BASE_URL/api/scripts/upsert`. The body is JSON (`name`, `source`, optional `description`, `intent`, `scope`), so wrap the file instead of sending it raw:
```bash
jq -n --arg name my-script --rawfile source ./my-script.ts '{name: $name, source: $source}' \
| curl -sS -X POST "$MCP_BASE_URL/api/scripts/upsert" \
-H "Authorization: Bearer $API_KEY" -H "X-Agent-ID: $AGENT_ID" \
-H "Content-Type: application/json" --data-binary @-
```
The route runs the same typecheck and scope rules as the tool.
### What `ctx` holds
- `ctx.swarm.*`: the swarm SDK. `task_get`, `task_send`, `task_storeProgress`, `task_action`, `task_list`, `slack_reply`, `memory_search`, `memory_store`, `kv_get`, `kv_getOrNull`, `kv_set`, `kv_delete`, `kv_incr`, `kv_list`, `swarm_get`, `agent_info`, `db_query`, and more. `kv_getOrNull` returns the entry, `null` on a missing key, and throws on other errors.
- A failed call that throws (an unknown or excluded `ctx.swarm` method, a missing argument, `kv_getOrNull`, `script_run`, `ctx.<namespace>.*`, `ctx.db.query`) throws a `SwarmToolError` from `"swarm-sdk"` on both executors. Branch on `err.code` (`unknown_tool`, `not_exposed`, `invalid_args`, `permission`, `not_found`, `rate_limited`, `timeout`, `refused`, `upstream`), `err.tool` and `err.status`, not on message text. Use `SwarmToolError.is(err)` or `instanceof`. An `unknown_tool` error lists the closest method names in `err.suggestions`. Calls that return `{ success, status, data }` still do not throw.
- `ctx.swarm.<tool>(args, { idempotencyKey })` runs a write at most once per key: a later call of the same tool with the same key (from any run of the same agent, for 7 days) returns the first result and does nothing, whatever its args. A failed or refused call does not use up the key. Only tools that go through the tool bridge take it (`task_send`, `slack_post`, `memory_store`, ...); a REST-routed method such as `kv_*`, `task_get`, `schedule_*`, `workflow_*` or `script_run` throws `invalid_args`. A run started with an `idempotencyKey` (`script-run` tool or API) keys every call from it the same way, so re-running it under the same key replays its bridge writes and `workflow_trigger` calls and hands a derived key to nested `script_run` calls; `kv_*` and `schedule_*` writes run again. Other one-off runs send no key unless you pass one.
- `ctx.swarm.task_send` requires `routingReason` and `routingNote` whenever `agentId` is present. The note must explain the worker choice in at least 10 characters after trimming whitespace (maximum 200 characters). Use `human_pinned` for an explicit/configured choice, `skill` for a role or specialization match, `continuity` for the same worker/session, `reroute_fault` for a fault handoff, or `overflow` for capacity/pool escalation. Omit both `agentId` and routing fields for the pool.
- `ctx.swarm.config`: `apiKey`, `agentId`, `mcpBaseUrl`, and `ctx.swarm.config.get("KEY")` for swarm config values, resolved for the agent running the script (agent scope over global, the same rows `get-config` returns for that agent). A key that is not set returns `undefined`, so `config.get("KEY") ?? fallback` works. All are `Redacted` wrappers that stringify to `<redacted>`. Never unwrap one into a return value, a log line, or a request body you build by hand.
- `ctx.api.<slug>` and `ctx.mcp.<slug>`: typed clients for registered connections. They exist only for registered connections. Introspect with `Object.keys(ctx.api ?? {})` and `Object.keys(ctx.mcp ?? {})`.
- `ctx.fs` and `ctx.sh`: files and bash. See "Files and shell" below.
- `ctx.db`: the swarm database. `ctx.db.dialect` is `"sqlite"` or `"postgres"`, set when the run starts. `ctx.db.query(sql, params?)` runs read-only SQL and returns the rows as objects. See "SQL in scripts" below. Script workflows do not have `ctx.db`.
- `ctx.stdlib`: `fetch`, `fetchJson` (retries, 30 s timeout), `grep`, `glob`, `table`, `retry`, `Redacted`.
- Retries: `ctx.swarm` calls retry a 429 on any call, and a 502/503/504 or network error on reads and on writes that send an idempotency key (such as `workflow_trigger` with `idempotencyKey`), within about 5 s. `ctx.stdlib.fetch` retries a 429 on any method and a 502/503/504 or network error on GET, HEAD, OPTIONS, PUT and DELETE. A POST or PATCH retries those only with an `Idempotency-Key` header: pass `retry: { idempotencyKey: true }` to send one UUID on every attempt. The key prevents a duplicate only when the server dedupes by it. `retry: false` makes one attempt. Wrap anything else, sync or async, in `ctx.stdlib.retry(fn, { retries, minDelayMs, maxDelayMs, budgetMs, retryOn })`; it rethrows the last error or returns the last result when attempts run out.
- `ctx.logger`: `log`, `warn`, `error`. Keep logs short.
- `ctx.caller`: who started this run. `{ kind: "agent", agentId }` for a direct call, `{ kind: "script", agentId }` for a call from another script. Frozen.
### Files and shell
```ts
await ctx.fs.writeFile("/afs/reports/today.md", "# Today\n"); // your agent-fs drive
await ctx.sh("echo '- shipped' >> reports/today.md"); // cwd is /afs
const { stdout, exitCode } = await ctx.sh("grep -rn TODO /afs/notes | sort | head -20");
```
- `/afs/...` is your default agent-fs drive, with your own permissions. It persists. Every other path (`/tmp`, `/work`) is memory that ends with the run. Relative paths resolve against `/afs`.
- `ctx.fs`: `readFile`, `writeFile`, `appendFile`, `readdir`, `stat`, `exists`, `mkdir`, `rm`, `cp`, `mv`. UTF-8 text only.
- `ctx.sh(command, { cwd, env, stdin })` returns `{ stdout, stderr, exitCode }` and does not throw on a failed command. Check `exitCode`.
- `ctx.sh` is just-bash, a bash interpreter in TypeScript: pipes, globs, `grep`, `sed`, `awk`, `jq`, `sort`, `wc`, `find`, `xargs`. No `git`, `curl`, `node`, `python`, or network. Files stay across calls in one run; `cd`, `env`, and variables do not.
- Limits per run: 64 MiB of scratch files, about 30,000 scratch entries (8 MiB of names and entries), 64 MiB per command, and 64 MiB per `/afs` file read (`EFBIG`). Scratch names have at most 255 characters.
- Without agent-fs for your agent, an `/afs` path fails with `agent-fs is not configured for this agent`, in `ctx.fs` and in shell redirects. Scratch and `ctx.sh` still work.
- Use `/afs` paths directly. A scratch symlink into `/afs` fails (`EXDEV` when the drive is mounted).
- A change that another agent makes to the drive can take up to 5 seconds to show in `ls` or `stat` during a run. Your own writes show at once.
### Durable workflow scripts
`launch-script-run` runs a script as a durable, journaled run with a different `ctx`: `ctx.run` (`id`, `agentId`, `args`) and `ctx.step.rawLlm(label, config)`, `ctx.step.agentTask(label, config)`, `ctx.step.swarmScript(label, config)`, plus `ctx.swarm.*`, `ctx.stdlib`, `ctx.logger`. Durable runs have no `ctx.api`, no `ctx.mcp`, no `ctx.fs`, no `ctx.sh`, and no `ctx.swarm.config`. A configured `ctx.step.agentTask` `agentId` records `human_pinned` unless `routingReason` is supplied. Call a connection or the drive from an inner script through `ctx.step.swarmScript`. See the `script-workflows` skill.
- Each `ctx.step.agentTask` step is a normal worker task (`taskType: "script-run-step"`). When it finishes, the lead gets the usual "Worker task completed — review needed" follow-up, even while the run is still awaiting that step. The step config has no `followUpConfig` field, so a value passed there is dropped. Let the script's own judge step be the gate and close those follow-ups with a one-line accept.
- A respawned run (API restart, crash) re-executes the script from the top: steps replay from the journal, and each `ctx.swarm.*` call replays from its idempotency record, keyed by the run id, the tool and the call's ordinal among that tool's calls in the run. Keep the order of `ctx.swarm` writes deterministic; a write whose ordinal shifts runs again. Read-only tools are never replayed. Pass `{ idempotencyKey }` to use your own key instead.
- When a crash supersedes an in-flight step and resumes it as a new task, the run does not follow the new task id: it can stay `failed` (for example `write EPIPE`) while the resumed task completes. Check the resumed task's output before you re-launch the run.
### Durable objects
A durable object is a named instance of a global script class. Each instance keeps its own JSON state between calls. Only one call runs on an instance at a time, so you do not need your own KV locks.
```ts
import { DurableObject } from "swarm-sdk";
export default class Counter extends DurableObject<{ n: number }> {
initialState() { return { n: 0 }; }
async add(args: { by: number }) { this.state.n += args.by; return this.state.n; }
}
```
Save it with `script_upsert` in `scope: "global"`. The API refuses a DurableObject class in agent scope and in an inline `script_run`. Call one instance by id:
```ts
script_run({ name: "counter", object: { id: "team-a", method: "add" }, args: { by: 2 } })
// → result: 2, object: { id: "team-a", stateVersion: 1, destroyed: false }
```
- **Methods**: each callable method takes `(args, ctx)` and returns a JSON value. You cannot call `constructor`, `initialState`, `migrate`, `destroy`, a name that starts with `_`, a static method, a getter or an arrow-function field. The API refuses such a call before it runs any code.
- **First call**: an id with no state starts from `initialState()`.
- **State rules**:
- `this.state` must be JSON, at most 2 MiB.
- The API saves it only when the method returns without an error. A thrown error keeps the old state.
- The API saves `this.state` as it is when the method returns. A change made later, by work the method did not await, is lost.
- **Versions**: every call runs the latest saved version of the script. When the stored state comes from an older version, `migrate(state, fromVersion)` runs once before the method, if you define it.
- **Destroy**: `this.destroy()` deletes the state when the method returns. The next call on that id starts again from `initialState()`.
- **Idle expiry**: `static idleTtlSec = 3600` deletes the state after 1 hour with no call. Without it, state is kept until `destroy()`.
- **Read the state**: `kv_get({ namespace: "durable:<scriptId>", key: "state/<id>" })` returns `{ codeVersion, stateVersion, state }`, with secrets scrubbed. KV writes on a `durable:` namespace are refused. Change state only through a method.
- **Errors**:
- `object busy` (409): another call held the instance for 30 s (5 s for a call from a script). Retry the call later. Do not loop.
- `already in this call chain` (409): a method called its own instance, directly or through other scripts. Restructure the call. Do not retry.
- `call depth exceeds 16` (400): a chain of nested script runs is too deep.
| Cloudflare Durable Objects | Swarm durable objects |
|---|---|
| `this.ctx.storage` key-value API | One JSON `this.state` snapshot per instance |
| `fetch()` and RPC from a Worker | `script_run` with `object: { id, method }` |
| `env` bindings | `ctx` (the same `ctx` as other scripts), passed to each method |
| Alarms | Not available yet |
| Methods take any arguments | Methods take `(args, ctx)` |
## Inline script pattern
```typescript
import type { ScriptContext } from "swarm-sdk";
export default async function main(args: { status?: string; limit?: number } | undefined, ctx: ScriptContext) {
const res: any = await ctx.swarm.task_list({ status: args?.status, limit: args?.limit ?? 50 });
const tasks: any[] = res?.data?.tasks ?? res?.tasks ?? [];
ctx.logger.log(`Fetched ${tasks.length} tasks`);
return {
total: tasks.length,
tasks: tasks.map((task: any) => ({ id: task.id, status: task.status, title: task.task?.slice(0, 120) })),
};
}
```
## Named script pattern
`script-upsert` with a searchable name, a concrete description, and an intent that says when to choose it. Good named scripts: aggregate failures by agent, schedule, or error family; fetch and normalize a third-party API response; fan out over tasks, memories, repos, or schedules; turn noisy JSON or HTML into a compact summary.
## Secrets
A config row reaches your shell env only when its `exportToEnv` flag is on. If `$KEY` is empty in your shell but `get-config` or `list-config` shows the row marked `not in agent env`, the row stays server-side on purpose: do not copy the value into a file, an env var, or a command. Use it from a script instead, through one of the paths below. Harness, git, and agent-fs credentials stay exported. Keys only the API server reads (Slack app token and signing secret, GitHub App private key, webhook and OAuth client secrets) can never be exported. If a recipe genuinely needs the variable in a shell, ask the lead to turn `exportToEnv` on for that row (`set-config` with `exportToEnv: true`); it applies to your next task.
Order of preference:
1. A registered connection: `ctx.api.<slug>.<operationId>({ path, query, header, body })` (OpenAPI), `ctx.api.<slug>.graphql(query, variables)` (GraphQL), `ctx.mcp.<slug>.<toolName>(args)` (MCP, proxied server-side, returns the raw MCP envelope: read `res.structuredContent ?? res.content?.[0]?.text`). Credentials attach at egress. The script never sees them. OAuth tokens refresh on their own.
2. A credential binding placeholder in a hand-written request: `Authorization: Bearer [REDACTED:GITHUB_TOKEN]`. The runtime substitutes the value at egress, only for the binding's allowed hosts.
3. `ctx.swarm.config.get("KEY")` for a config value you pass to `ctx.stdlib.fetch` as a header value. Unwrap it with `ctx.stdlib.Redacted.value(...)` inside the header itself (a wrapped value sends the literal `<redacted>`), and never log it. Secret values are scrubbed from run output. Not available in `launch-script-run` (durable runs have no `ctx.swarm.config`): collect there without secrets, then pass the run's output to a short `script-run` that makes the authenticated calls.
A raw secret, or the output of `get-config` with `includeSecrets: true`, must not appear in script source, script `args`, a schedule's `taskTemplate` or `scriptArgs`, or a task description. Those are stored as plain text and replayed on every run.
Registration is lead-only: `script-connections` and `credential-bindings`. A worker that needs a missing connection puts the request in its task output: `slug`, `baseUrl` or spec URL, `allowedHosts`, and the auth (config key or OAuth provider). When a secret has no credential binding yet, step 3 is a stopgap: say so in the task output and ask the lead to register a binding for that config key, so later runs use it. Leads: `upsert-openapi` with a spec URL keeps operations refreshable; `credential-bindings` `oauth-app-upsert` then `oauth-authorize-url` sets up an OAuth provider (`tokenAuthStyle` and `tokenBodyFormat` cover Notion-style token endpoints).
Full reference: the "Script connections" guide on the docs site.
## SQL in scripts
Direct SQL beats fetching lists into the script. Use `ctx.db.query(sql, params?)`. It returns the rows as objects keyed by column name, and it throws when the query fails (bad SQL, timeout, 429). It goes through `ctx.swarm.db_query`, so it has the same permissions, row cap and errors.
```typescript
const counts = await ctx.db.query<{ status: string; cnt: number }>(
"SELECT status, count(*) AS cnt FROM agent_tasks GROUP BY status",
);
```
The swarm database is SQLite or PostgreSQL. `ctx.db.dialect` tells which one, with no probe query. When a statement has no portable form, give one text for each dialect. `ctx.db.query` runs the text of the active dialect:
```typescript
const durations = await ctx.db.query(
{
sqlite: `SELECT id, (julianday("finishedAt") - julianday("startedAt")) * 86400 AS seconds FROM script_runs WHERE "scriptName" = ? LIMIT 100`,
postgres: `SELECT id, extract(epoch FROM "finishedAt" - "startedAt") AS seconds FROM script_runs WHERE "scriptName" = $1 LIMIT 100`,
},
[args.scriptName],
);
```
Give both texts. An object without both strings throws a `TypeError`. The db-query-guidance skill has the dialect notes.
`ctx.swarm.db_query({ sql, params })` is the raw call. It does not throw on a failed query: it returns `{ success, status, data }`. On success, `data` holds positional rows, `{ rows: unknown[][], columns: string[], dialect }`. Zip them:
```typescript
function rowsToObjects(res: any): any[] {
const p = res?.data ?? res;
const cols: string[] = p?.columns ?? [];
return (p?.rows ?? []).map((r: any) =>
Array.isArray(r) ? Object.fromEntries(cols.map((c, i) => [c, r[i]])) : r,
);
}
```
### Concurrency limit
`ctx.swarm.db_query` throttles at about three concurrent calls. A wider `Promise.all` fan-out can return 429 responses with no `rows` key, so `res?.data?.rows ?? []` misreads throttling as a clean zero. `ctx.db.query` throws on a 429. With the raw call, check the response status and `data.error` before reading `rows`. Serialize queries or cap concurrency at three.
### Response traps
- Every `ctx.swarm.*` response passes the secret scrubber. Text shaped like a secret-looking `key=value` assignment comes back as `[REDACTED:<key>]`, so code, HTML or profile text read through `kv_get`, `db_query` or a page read can differ from the stored bytes. Never write such a read back verbatim. Read stored text as `hex(...)` through `db_query`, decode it in the script, and refuse to publish or write when the result contains `[REDACTED:`.
- `db_query` rejects any SQL containing `;`, even inside a string literal. Pass such literals through `params`.
- Pass a JSON list as a string param. SQLite reads it with `json_each(?)`. On PostgreSQL, cast it with `$1::text::jsonb`, or pass a real array with `$1::jsonb`. A string param in a `json` or `jsonb` position must be JSON text: `'abc'` fails.
- `db_query` caps rows without an error (1000 in scripts). Aggregate in SQL and confirm totals with `COUNT(*)`.
- `kv_get` returns the entry at `data`: read `res.data.value`. The MCP tool shape `entry.value` reads `undefined` in a script.
- `script_run` returns an envelope (`result`, `truncated`, `durationMs`, `stdout`, `stderr`, `exitCode`). The callee payload is at `.result`. Take key names from a real response: a guessed key that is absent reads as a false zero.
- An `argsSchema` validation error fires before the body runs and repeats on every retry. Fix the args instead of retrying.
- The skill methods disagree on the id field: `skill_get({ id })`, but `skill_update({ skillId, content })`. `skill_update({ id, content })` returns `success: false` with `data.message` "skillId is required." and writes nothing. Gate a write on `res.success`, then re-read: the version must increase and the content must match the draft.
- Write a dedup or idempotency marker only after the step succeeds. A marker written after a failure blocks the next real attempt.
- `kv_set` returns `success: false` instead of throwing when a key has a character outside `[a-zA-Z0-9._:/-]` (for example `#` or `|` from a git ref). Check `success` after every write.
- `script_run` without `scope` runs an agent-scoped row of the same name before the global one, so a stale private copy silently wins. Pass `scope: "global"` for a shared script.
- A nested `script_run` does not overlap with the caller's own `ctx.stdlib.fetch` calls: started alongside them it added about 8 s to one measured run. Start it after the caller's own queries.
Common tables: `agent_tasks`, `session_logs`, `agent_memory`, `scheduled_tasks`, `agents`. `session_logs` has no `tool_name` column. Tool names sit inside the `content` JSON column. Extract them with `instr` and `substr` in SQL, or parse the JSON in the script.
## Progress from a script
```typescript
export default async function main(args: { taskId: string; items: string[] }, ctx: ScriptContext) {
await ctx.swarm.task_storeProgress({ taskId: args.taskId, progress: `Processing ${args.items.length} items` });
return { processed: args.items.length };
}
```
## Exposing a script as an API
A named script can serve `POST /api/x/script/<id>` for callers outside the swarm. Manage endpoints from the script's API tab in the dashboard, or with the `script-apis` tool (`list`, `create`, `update`, `rotate`, `delete`). `list` masks bearer tokens; `includeSecrets: true` reveals them. `create` and `rotate` return the plaintext token once.