# Cubbies

> The SQLite databases an Agent Service's workflows and code agents share — what a cubby is, how a workflow's cubby steps and a code agent's ctx.cubby use it, and when to use the Memory Bank instead.

Source: https://developers.manykind.ai/agents/building-blocks/cubbies/

A **cubby** is a SQLite database your Agent Service declares. Every workflow and
code agent the service publishes shares it by alias, and it lives in each vault
that connects them. It is where shared working state goes: pipeline status,
intermediate results, rows a widget renders.

## Shape

| Property | What it means |
|---|---|
| **Owned by the service** | Every agent the service publishes reads and writes the same cubby by alias. An agent of a different service cannot reach it. |
| **One per vault** | A service's cubby is global within a vault: one database per vault, service, and alias, whichever scope triggered the run. A run in vault A never sees vault B's rows. |
| **SQLite** | Real tables, indexes, and `CHECK` constraints, declared by migrations. The `sqlite-vec` extension is available for vector search. |
| **Durable** | Survives every run and every redeploy. Disconnecting an agent keeps the data. |

## Declare one

A cubby is an alias and a directory of numbered `.sql` migrations. Keep one
directory per cubby under `cubbies/`; the directory name is the alias.

```
ticket-triage/
├── manykind.config.ts
└── cubbies/
    └── history/
        ├── 001-init.sql
        └── 002-add-customer.sql
```

```sql
-- cubbies/history/001-init.sql
CREATE TABLE IF NOT EXISTS history_messages (
  id   TEXT PRIMARY KEY,
  text TEXT NOT NULL,
  ts   INTEGER NOT NULL
);
```

Publish the declarations to your Agent Service's bucket:

```bash
mk cubby push --bucket <bucketId>
```

| Option | What it does |
|---|---|
| `--bucket <id>` | Required. The Agent Service's bucket, the same one you push agents to. |
| `--dir <path>` | Directory holding one subdirectory per cubby. Default `./cubbies`. |
| `--alias <name>` | Push only this cubby. |
| `--cubby-version <semver>` | Version to publish each declaration under. Default `0.1.0`. |

Credentials are the same as for `mk push`: `$MANYKIND_DDC_ACCESS_TOKEN` or
`--access-token`. See [CLI reference](https://developers.manykind.ai/reference/cli/).

You can also declare cubbies in ROC: the Agent Service's **Cubbies** page has
**New cubby** and, per cubby, **Rename**, **Add migration**, and **Delete**
(withdraws the declaration; existing vault copies stay). Each cubby shows its
schema version and published version.

<!-- shot: service-cubbies | /agent-services/:id/cubbies | the Cubbies page listing aliases with their tables -->

An agent or workflow can also declare a cubby itself, with
`cubbies: [{ alias, migrations }]` in its config. `mk push` then declares any
alias the bucket does not have yet, but never changes an existing declaration:
schema changes always go through `mk cubby push`.

`mk cubby push` creates no database: a vault's copy is created, and its
migrations applied, the first time an agent or step touches the cubby (see
[When migrations run](https://developers.manykind.ai/agents/building-blocks/cubby-schema/#when-migrations-run)).
A cubby belongs to
the Agent Service, not to one workflow, so prefix table names with the workflow
or feature that owns them (`triage_tickets`, `triage_escalations`), or give each
workflow its own alias. Migrations are forward-only; the rules, reserved
aliases, and when migrations run are on
[Cubby schema and migrations](https://developers.manykind.ai/agents/building-blocks/cubby-schema/).

## Use it from a workflow

Two step kinds reach a cubby: `cubbyQuery` runs a `SELECT` and puts the rows on
the carried item; `cubbyExec` runs a write. Values go in `args` with `?`
placeholders, never in the SQL:

```ts
{
  id: "save",
  kind: "cubbyExec",
  params: {
    alias: "history",
    sql: "INSERT OR IGNORE INTO history_messages (id, text, ts) VALUES (?, ?, ?)",
    args: ["={{ $runId }}", "={{ $json.text }}", "={{ $json.ts }}"],
  },
}
```

Parameters and the `{{ $runId }}` convention are on
[Steps: Cubby query and Cubby exec](https://developers.manykind.ai/agents/workflows/steps/#cubby-query-and-cubby-exec).

## Use it from a code agent

`ctx.cubby(alias).query(sql, params)` returns rows; `.exec(sql, params)` returns
`{ changes, lastInsertRowid }`:

```ts
await ctx.cubby("history").exec(
  "INSERT INTO history_messages (id, text, ts) VALUES (?, ?, ?) ON CONFLICT(id) DO NOTHING",
  [event.eventId, text, Date.now()],
);
const recent = await ctx.cubby("history").query<{ text: string }>(
  "SELECT text FROM history_messages ORDER BY ts DESC LIMIT 20",
);
```

See [Write an agent](https://developers.manykind.ai/agents/code-agents/overview/#3-use-ctx).

## Other readers

| From | How |
|---|---|
| A widget | Named `sql` queries in the widget declaration. See [Widgets](https://developers.manykind.ai/agents/building-blocks/widgets/). |
| Your app | `vault.service(agentServicePubkey).cubby(alias).query(sql, params)` with the Vault SDK. |
| ROC | **Cubbies**: select a cubby to see its **Tables**, a **SQL runner** for queries, and its **Migrations**. In the Workflow Builder, the canvas side rail's **Cubbies** button opens the same view beside the workflow. |

## Write idempotently

A Task can run more than once and an event can be delivered twice, in a
workflow and in a code agent alike. Key rows with `PRIMARY KEY` or `UNIQUE`
(the run id, the event id, a natural key) and write with `ON CONFLICT` or
`INSERT OR IGNORE`.

## Cubby or Memory Bank

| | Cubby | [Memory Bank](https://developers.manykind.ai/vaults/memory-bank/) |
|---|---|---|
| What goes in | Working state: status, intermediate results, rows a widget shows, and the raw rows a curator learns from | Durable conclusions the organization should keep, proposed by a curator and accepted by a person |
| Who writes it | Your workflows and agents | Curator connections (harvester or analyzer role) propose; a person accepts in ROC |
| Who owns the schema | Your Agent Service | The platform: records and relations |
| Who can read it | Your service's agents and widgets; the vault's members through ROC | Anyone the vault's grants and privacy classes allow |
| Classification | None | Every record and relation carries a privacy class |

Workflows write cubbies; curators propose; people accept. A workflow never writes the Memory Bank directly: it writes rows here, keyed by a stable subject key and stamped with its `run_id`. A curator reads them and proposes records, and a person accepts them. Readers of the bank never join back to a cubby. To leave rows for a curator in another Agent Service, write the scope's shared cubby with `ctx.workspace(alias)`. See [Build on the Memory Bank](https://developers.manykind.ai/vaults/build-on-the-memory-bank/#write-cubby-rows-a-curator-can-use).

## Limits

| Limit | Value | When you hit it |
|---|---|---|
| Size of one cubby | 10 GB | Writes are refused: `413 QUOTA_EXCEEDED`, "storage quota exceeded". Delete or archive old rows; keep large content as objects. |

## Related

- [Next: Cubby schema and migrations](https://developers.manykind.ai/agents/building-blocks/cubby-schema/)
- [Steps: Cubby query and Cubby exec](https://developers.manykind.ai/agents/workflows/steps/#cubby-query-and-cubby-exec)
- [Memory Bank](https://developers.manykind.ai/vaults/memory-bank/)
- [Your Agent Service](https://developers.manykind.ai/get-started/agent-service/)
