---
name: tc-apps
description: Keep the owner's personal records and context (facts and preferences, decisions, handoffs between agents) in their own TinyCloud storage, organised into apps you create and extend as they go. Use when the owner asks you to log, record, track, add, look up, list or sum up something about their life, tells you to remember something about them, asks about something they told an agent before, or asks you to continue or pick up an earlier task, even if they don't mention TinyCloud. The owner approves any missing access through OpenKey; the agent runs every command and verifies saved records.
metadata:
  version: "0.4.5"
---

> **Agents: read this file raw, not summarized.** If your fetch tool returned a summary, fetch it again with `curl -fsSL https://tinycloud.one/agents/abilities/skills/apps.md` and follow that text. Summaries drop the shell blocks you must run.

# Personal apps in TinyCloud

**CLI rule (stable `1.9.1`): start with §1 and run its block before any other command, including any `tinycloud` or `--version` check of your own; it selects the CLI and decides. Don't judge who installed the CLI yourself. Only a `TC_OLDER` line from §1 means the operator chose an older CLI: tell the owner in one line that it is older than the required `1.9.1`, and stop. Any other older CLI: §1 prints `CLI_OLDER`; stop and tell the owner what it printed. Never run `tinycloud upgrade` or a global npm install of the TinyCloud CLI. A newer CLI is never downgraded.**

**Not onboarded yet? Onboard first for generic apps.** When a generic app access check returns `onboard-first`, fetch the onboarding guide raw with `curl -fsSL -H 'Accept: text/markdown' https://tinycloud.one/agents/abilities/onboarding` and follow it now, keeping the owner's original request. Ask the owner first in one line ("I need to set up TinyCloud here to finish this. OK?") and wait for their yes; onboarding tells you the rest. Then rerun that check and continue from its result. Health Log is the exception: always use its dedicated `agent-meal` profile and app-specific permission in `TINY-APPS.md`, even when onboarding is complete.

**What is and isn't secret:** a permission slip (a delegation or a grant file) and the code OpenKey shows after you approve are fine to paste, share and store. Private keys, session keys, the files in `~/.tinycloud`, API keys and secret values, and the part of a share link after `#` stay private. Full list: <https://tinycloud.one/agents/abilities/agent#what-is-and-isnt-secret>.

**Context: what the owner told an agent before.** When the owner asks you to remember something about them, asks about something they told an agent before ("what's my …", "as usual"), asks you to continue or pick up an earlier task, or a line in your instructions asks you to check their TinyCloud context, read `CONTEXT.md` in full and follow it. It needs no catalog read. Read whichever path exists:

```sh
for d in "${CLAUDE_SKILL_DIR}" "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps" "$HOME/.agents/skills/tc-apps" "$HOME/.local/share/tc-apps/skill"; do if [ -n "$d" ] && [ -f "$d/CONTEXT.md" ]; then cat "$d/CONTEXT.md"; exit; fi; done
curl -fsSL https://tinycloud.one/agents/abilities/skills/apps/CONTEXT.md
```

**Meal, food, calorie, body-weight and recipe requests: catalog first.** If the owner asks to log or look up something in a catalog app's domain (`TINY-APPS.md` §1 lists them: meals, food, calories, body weight, recipes), STOP. Do not start any other sign-in or access request, including the per-app sign-in in §4. Read the catalog now and follow it, with these two shell calls in order; together they are the whole file:

```sh
if [ -n "${CLAUDE_SKILL_DIR:-}" ] && [ -f "${CLAUDE_SKILL_DIR}/TINY-APPS.md" ]; then sed '/^## 4\. Logging/,$d' "${CLAUDE_SKILL_DIR}/TINY-APPS.md"; elif [ -f "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps/TINY-APPS.md" ]; then sed '/^## 4\. Logging/,$d' "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps/TINY-APPS.md"; elif [ -f "$HOME/.agents/skills/tc-apps/TINY-APPS.md" ]; then sed '/^## 4\. Logging/,$d' "$HOME/.agents/skills/tc-apps/TINY-APPS.md"; elif [ -f "$HOME/.local/share/tc-apps/skill/TINY-APPS.md" ]; then sed '/^## 4\. Logging/,$d' "$HOME/.local/share/tc-apps/skill/TINY-APPS.md"; else curl -fsSL https://tinycloud.one/agents/abilities/skills/apps/TINY-APPS.md | sed '/^## 4\. Logging/,$d'; fi
```

```sh
if [ -n "${CLAUDE_SKILL_DIR:-}" ] && [ -f "${CLAUDE_SKILL_DIR}/TINY-APPS.md" ]; then sed -n '/^## 4\. Logging/,$p' "${CLAUDE_SKILL_DIR}/TINY-APPS.md"; elif [ -f "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps/TINY-APPS.md" ]; then sed -n '/^## 4\. Logging/,$p' "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps/TINY-APPS.md"; elif [ -f "$HOME/.agents/skills/tc-apps/TINY-APPS.md" ]; then sed -n '/^## 4\. Logging/,$p' "$HOME/.agents/skills/tc-apps/TINY-APPS.md"; elif [ -f "$HOME/.local/share/tc-apps/skill/TINY-APPS.md" ]; then sed -n '/^## 4\. Logging/,$p' "$HOME/.local/share/tc-apps/skill/TINY-APPS.md"; else curl -fsSL https://tinycloud.one/agents/abilities/skills/apps/TINY-APPS.md | sed -n '/^## 4\. Logging/,$p'; fi
```

**Read installed skill files with the shell, never your client's file-read tool.** The skill folder is outside the project: Claude Code asks the owner before its file-read tool opens a file there, while a shell read goes ahead. A file over about 25 KB is read in parts, as above, so each output shows in full. If your client still saves an output to a file, read that saved file, not the skill.

Sections 1 to 9 below apply only to a request that no catalog app owns (`TINY-APPS.md` §2). Which layout an app uses is decided by §1a.

**For any request about the owner's data, read the catalog first:** use the two `TINY-APPS.md` shell calls above, unless you already ran them in this conversation.

If a catalog app owns the domain, follow its fast path in `TINY-APPS.md`. Health Log checks only its dedicated `agent-meal` grant; never use an onboarding profile for Health Log. A live app-specific grant needs no new approval. Don't start the generic per-app sign-in (§4) for that request. Don't run a `tc` from the PATH; use the `TC_BIN` that §1 prints.

An app is in one of two layouts, selected by §1a. **New layout:** records live in the owner's `applications` space under `xyz.tinycloud.tinyapps.<app>/`, with an app catalog and discovery entry. §3 reuses the selected onboarding profile when its current permissions cover the task; otherwise it checks an existing app-scoped profile for the same owner and host. Request only missing access. **Legacy mode:** records stay in `default` under `xyz.tinycloud.agent-data/`, through `LEGACY.md`, until the owner requests and verifies a move (§9). Never move or delete records merely to reuse a CLI.

Operator-set, never change or unset:
- `TC_BIN`: the selected absolute TinyCloud CLI path. Without it, §1 uses the shared private CLI, then `tinycloud` on `PATH`. The same executable handles every layout. Never resolve a bare `tc`: it may be the Linux traffic-control tool.
- `TC_PROFILE` and `TC_HOST`: explicit profile and host selections, when set. Never switch away from them to avoid a permission error.
- `TC_HOME`: the CLI profile store. Optional.
- `TC_APPS_STATE`: this skill's state folder. Default `$HOME/.local/state/tc-apps`.

Health Log never asks the owner for an address or timezone: the explicit owner's address is saved after sign-in. Use the page-saved timezone, then the saved host zone, then the host's local IANA zone, else UTC. Follow `TINY-APPS.md` for the account and clock guard before accessing meal rows.

How to run the blocks:
- Run each block as one shell tool call, exactly as written, after filling in the `'<…>'` values. Keep the single quotes. `<APP>` is the app's name: lowercase `a-z`, `0-9` and `-`, starting with a letter, such as `fitness`.
- `<TC_BIN>` and `<STATE>` are the first two lines §1 prints, copied exactly. Keep the literal assignments before `umask 077` so clients can approve the actual executable. For generic app operations, copy `<APP_PROFILE>`, `<APP_HOST>` and `<APP_SPACE>` from §3 and use them unchanged for that request.
- A command takes 4–10 s. A block that runs commands side by side (`xargs -P 8`) takes about as long as one. Wait up to 60 s.
- Write files only through these blocks, never with a file-editing tool. Don't run `tinycloud` commands that aren't here.
- The blocks that find this skill's own files look first in the folder that Claude Code fills in for this skill, including a plugin's; in other clients that first folder is empty and skipped. Run them as written.
- Run §1 once per conversation, and §1a and §3 once per app per conversation.

## 0. Route the request

The catalog in `TINY-APPS.md` decides the target. Use §1 to §9 only for domains no catalog app owns. Use only the selected profile and verified permissions; never read key or session files or arbitrarily choose another owner's profile. "Profile" here means the owner's CLI profile: the sign-in saved under a name such as `owner` or `agent-data`, and the files behind it. It never means an app's own records. Records an app calls "profile" (a Gene Keys profile, say) are ordinary records of that app: read and write them like any other.

## 1. Read the paths and the time

```sh
B=${TC_BIN:-}; H="$HOME/.config/tinycloud/agent-env.mjs"; P="$HOME/.local/share/tinycloud/cli"
if [ -z "$B" ] && [ -r "$H" ]; then B=$(node "$H" get TC_BIN) || B=; fi
case "$B" in */tc) [ -x "${B%/tc}/tinycloud" ] && B="${B%/tc}/tinycloud" ;; esac
O=$B; [ -z "$O" ] || ! node -e 'const{realpathSync:r}=require("fs"),[f,d]=process.argv.slice(1),i=(a,b)=>a.startsWith(b+"/");let x=i(f,d);try{x=x||i(r(f),r(d))}catch{}process.exit(x?0:1)' "$O" "$P" || O=
[ -z "$B" ] || [ -x "$B" ] || { echo "TC_NOT_FOUND: $B is not executable; follow https://tinycloud.one/agents/abilities/connect for this client or set TC_BIN" >&2; exit 1; }
[ -n "$B" ] || B="$P/node_modules/.bin/tinycloud"
[ -x "$B" ] || B=$(command -v tinycloud) || B="$HOME/.local/share/tinycloud/npx/tinycloud"
[ "$B" != "$HOME/.local/share/tinycloud/npx/tinycloud" ] || { mkdir -p "${B%/*}" && printf '#!/bin/sh\nexec npx --prefix "$HOME" -y -p @tinycloud/cli@1.9.1 tinycloud "$@"\n' > "$B" && chmod 700 "$B"; }
printf '%s\n' "$B"
{ [ -n "${TC_APPS_STATE:-}" ] && echo "$TC_APPS_STATE"; } || echo "$HOME/.local/state/tc-apps"
date +%Y-%m-%dT%H:%M%z | sed 's/..$/:&/'
date -u +%FT%TZ
V=$("$B" --version) || V=
[ -n "$V" ] || { echo "TC_NOT_FOUND: $B doesn't run; follow https://tinycloud.one/agents/abilities/connect for this client or set TC_BIN" >&2; exit 1; }
printf '%s\n' "$V"
if printf '%s\n' "$V" | awk -F. -v p=1.9.1 '{split(p,q,".");for(i=1;i<4;i++){n=$i+0;if(n!=q[i]+0)exit(n>q[i]+0)};exit($3!~/^[0-9]+-/)}'; then
  if [ "$B" = "$O" ]; then echo "TC_OLDER $V $B is older than the pinned 1.9.1; keeping this operator-selected CLI"
  else echo "CLI_OLDER $V $B: this CLI is older than this skill needs. Stop and tell the owner what it printed."; fi
  exit 1
fi
```

It prints `<TC_BIN>`, `<STATE>`, `<NOW>`, `<UTC>` and the CLI version, one per line. If the selected path is `tc` (the legacy alias, which prints a notice on every call), the check uses the `tinycloud` beside it instead, when there is one.
- `<NOW>` is this computer's local date, time and UTC offset, e.g. `2026-10-06T18:30+02:00`. Use it for "today", and for the time in an id, for generic apps. Health Log instead uses its validated owner timezone and fresh clock from `healthlog-context.mjs` (`TINY-APPS.md` §4).
- `<UTC>` is the clock's time in UTC, from `date -u +%FT%TZ`, e.g. `2026-10-06T16:30:12Z`. It is what a record's `at` holds when the record is about right now. Never write a time from memory or an estimate: a time you didn't read from the clock or get from the owner is wrong. An id uses the local `<NOW>` and a "now" `at` uses UTC, on purpose: near local midnight the id's date and the `at` date can differ, and that is correct.
- If no CLI is found, the check writes `~/.local/share/tinycloud/npx/tinycloud`, which runs the pinned CLI with `npx`, and uses it as `TC_BIN`.
- `CLI_OLDER <version> <path>` (the block stops) → the CLI is older than `1.9.1`: stop and tell the owner what it printed.
- `TC_OLDER <version> <path>` (the block stops, or the CLI setup prints it) → the operator selected that older CLI: tell the owner in one line that it is older than the required `1.9.1`, and stop. Don't replace it.

## 1a. Which layout does this app use?

Do this first for every app you are about to read or write, once per conversation, except `context`, which is always in the new layout (`CONTEXT.md`). `<APP>` is the app's name. It prints one word and creates no profile, grant or record. Without an installed skill it first fetches the published helpers into `<STATE>`; if that fails, stop and tell the owner:

```sh
for d in "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps" "$HOME/.agents/skills/tc-apps"; do if [ -f "$d/layout.sh" ]; then sh "$d/layout.sh" '<APP>'; exit; fi; done
STATE='<STATE>'; umask 077; D="$STATE/served-layout"; mkdir -p "$STATE"; T=$(mktemp -d "$D.XXXXXX") || exit 1
for f in layout.sh app-context.mjs; do curl -fsSL "https://tinycloud.one/agents/abilities/skills/apps/$f" -o "$T/$f" || { rm -rf "$T"; exit 1; }; done
rm -rf "$D" && mv "$T" "$D" || { rm -rf "$T"; exit 1; }
sh "$D/layout.sh" '<APP>'
```

The CLI checks, in this order, and stops at the first that applies:
1. A receipt on this computer, `<STATE>/migration/<APP>.receipt.json`, says `"verified": true` for this app → `new`.
2. The app's discovery entry, `xyz.tinycloud.tinyapps.catalog/<APP>/entry`, exists in the owner's `applications` space → `new`. It is read with the profile, host and space that §3's `app-context.mjs` selects for the app. An entry created on another computer need not have a receipt here; it selects the new layout but does not prove that older records were copied.
3. The old catalog in `default` has the app's record, `xyz.tinycloud.agent-data/catalog/<APP>` → `legacy`.
4. Otherwise → `new`.

If step 2 or 3 cannot be read, it prints `ask` or `check-failed` instead of going on to the next step. Step 2 is skipped when this computer has no profile that can read `applications` (not onboarded yet). A computer that never signed in to the old place has nothing to read there, so step 3 ends in `new` for it. Step 3 prints `ask` only when an earlier sign-in for the old place expired or lacks the grant, or when step 2 was skipped and the old catalog holds the app. It runs with the CLI that §1 selected. If that CLI has no `kv list --all`, it lists the old catalog in one call, and if that listing says it was cut off, it prints `check-failed`.

The output is the layout; use the same `<TC_BIN>` in either layout.
- `legacy` → the app has records in `default` and no verified move exists on this computer. **Keep reading and writing the old layout exactly as before:** read `LEGACY.md` in full with the shell, `cat "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps/LEGACY.md"` (Claude Code) or `cat "$HOME/.agents/skills/tc-apps/LEGACY.md"` (Codex, OpenCode), and follow its §2 to §8, with `<TC_BIN>`. Request no new approval, create no new profile, and don't use §3 to §9 for this app. Once per conversation, tell the owner: "This app can move to its own folder: say "move my <APP> records" when you want it (nothing is deleted)." When the owner says it, follow §9. Until the move is verified, nothing about this app changes.
- `new` → use §2 to §8 below with the same CLI. If the owner says they used this app before, with an earlier version on any computer, don't set it up as new. Request read-only access to the old catalog with the sign-in block under `ask` below, send its link as §4 says, and run §1a again.
- `ask` → this computer has an earlier sign-in for the old place that is expired or lacks the grant, so it can't list the old catalog; or step 2 was skipped and the old catalog holds the app. **Never ask the owner whether the app is new or had earlier records:** their request is the go-ahead, and they can't know what an earlier version or another client stored.
  - The request makes an app ("make me a Gene Keys app", "start a reading log"), or it is the setup guide's default `todos` record → use `new`.
  - Otherwise (the owner means an app this computer can't find) → request read-only access to the old catalog with the start block below, which prints the approval link because `auth login` keeps waiting for the approval, and send that link as §4 says. When the owner says they approved, run the finish block after it; when it shows `"authenticated": true`, run §1a again. Write nothing until it prints `new` or `legacy`.

```sh
TC_BIN='<TC_BIN>'; STATE='<STATE>'; umask 077
mkdir -p "$STATE"
printf '%s\n' '{"manifest_version":1,"app_id":"xyz.tinycloud.agent-data","name":"Personal apps (agent)","space":"default","defaults":false,"permissions":[{"service":"tinycloud.kv","path":"xyz.tinycloud.agent-data/catalog/","skipPrefix":true,"actions":["get","list"]}]}' > "$STATE/agent-data-catalog.json"
"$TC_BIN" context --json --profile agent-data 2>&1 | grep -q PROFILE_NOT_FOUND && "$TC_BIN" init --name agent-data --key-only > /dev/null
"$TC_BIN" auth login --method openkey --no-popup --manifest "$STATE/agent-data-catalog.json" --expiry 30d --profile agent-data < /dev/null 2> "$STATE/login-catalog.err" &
P=$!; for i in $(seq 1 10); do kill -0 "$P" 2>/dev/null || break; grep -q 'complete link' "$STATE/login-catalog.err" && break; sleep 2; done
kill "$P" 2>/dev/null; wait "$P" 2>/dev/null
L=$(grep -A1 'complete link' "$STATE/login-catalog.err" | tail -n 1); [ -n "$L" ] && echo "$L" || grep -o '"code": "[A-Z_]*"' "$STATE/login-catalog.err"
```

When the owner says they approved, run this finish block. It runs the same sign-in again, waits up to 60 seconds for the approval, then shows the sign-in state:

```sh
TC_BIN='<TC_BIN>'; STATE='<STATE>'; umask 077
"$TC_BIN" auth login --method openkey --no-popup --manifest "$STATE/agent-data-catalog.json" --expiry 30d --profile agent-data < /dev/null 2> "$STATE/login-catalog.err" &
P=$!; for i in $(seq 1 30); do kill -0 "$P" 2>/dev/null || break; sleep 2; done
kill "$P" 2>/dev/null; wait "$P" 2>/dev/null; true
"$TC_BIN" --profile agent-data auth status --json
```

- `check-failed` → the old place could not be checked, for example because of the network. If a sandbox blocked the read, request the client's network approval and rerun that same read once. If it still fails, report it and write nothing.
- `jq is needed` (exit 3) → report the missing host prerequisite and write nothing. `migrate.sh` also needs `jq`.

Only a verified receipt or the app's discovery entry switches the selected layout: never switch because you moved, or think you moved, records yourself. For a CLI move of a legacy app, never write its discovery entry yourself; follow §9, which writes it only after the copy is verified. This CLI procedure is separate from the connector.

## 2. How the records are organised

Pick the app first: `<APP>` is its name, `<ID>` is `xyz.tinycloud.tinyapps.<APP>`, and every key of that app starts with `<ID>/`. The app names `catalog`, `index-stale`, `healthlog` and `recipes` are reserved, and a kind is never named `catalog` or `index-stale`. The app `context` is the owner's context: use it only as `CONTEXT.md` says.

| Key (in the `applications` space) | Holds |
|---|---|
| `<ID>/catalog` | the app's catalog record |
| `<ID>/catalog/<kind>` | a catalog record for each kind of thing the app holds |
| `<ID>/<kind>/<YYYY-MM>/<id>` | records of a **log** kind: things that happen, such as a weigh-in or a run |
| `<ID>/<kind>/<state>/<id>` | records of an **item** kind: things with a status, such as a todo or a goal |
| `<ID>/<kind>/<subject>` | records of a **subject** kind: one record per subject, which a new value replaces, such as the context app's facts (`CONTEXT.md`) |
| `xyz.tinycloud.tinyapps.catalog/<APP>/entry` | the app's discovery entry: its name and title, no records |

**Names and keys.**
- App, kind, state and slug names use lowercase `a-z`, `0-9` and `-`. An app name starts with a letter.
- Field names use lowercase `a-z`, `0-9` and `_`.
- `due` and `done` mean the same in every kind: the date something is due, and the date it was done, both `YYYY-MM-DD`. A month counts as its last day: "by December" is `2026-12-31`.
- Keys hold only ASCII letters, digits and `/-._~:+@=,`. Never a space, `?`, `#` or a non-ASCII letter: a `?` cuts the key short without an error and overwrites another record.

**Which `per` a log kind gets.**
- `per: day` → a measurement taken at most once a day, such as body weight, sleep or steps.
- `per: time` → anything the owner does, which can happen twice in a day: a workout, a run, a meal, a payment.

**Ids.**
- `per: day`: the owner's local date, e.g. `2026-10-06`. A new value for that day replaces the record.
- `per: time`: local date and time, then a slug of the main subject: `2026-10-06T1830-bench-press`.
  - Use the time the owner gave. A part of the day counts as a time: morning `0800`, afternoon `1500`, evening or tonight `2000`.
  - With no time at all, use `<NOW>`'s time for today, and `1200` for another day.
  - If that key exists, add `-2`, `-3`.
- Item kind: the creation date and a slug of the item: `2026-10-06-renew-passport`. The onboarding default `todos/todo/open/try-my-personal-apps` uses a fixed id so it stays unique across reruns; it counts as present if it exists in `open/` or `done/`.
- `<YYYY-MM>` is the month of a log id's date.

**A record** is one line of JSON: `{"v":1,"at":"2026-10-06T18:30+02:00","data":{"exercise":"bench press","sets":3,"reps":8,"kg":60},"by":"claude-code"}`.
- `v`: the kind's `v` from its catalog record.
- `at`: when it happened, not when you saved it.
  - With a time (including a part of the day, as for ids): date, time and UTC offset, written like `<NOW>`: `2026-10-06T08:00+02:00`.
  - With only a day from the owner: only the date, e.g. `"2026-10-06"`.
  - With neither, or when it's about right now: the clock, `<UTC>`, e.g. `"2026-10-06T16:30:12Z"`. If §1 ran more than 5 minutes before you write (a sign-in wait counts), run `date -u +%FT%TZ` again first and use that.
- `data`: the kind's `fields`, by exactly those names: never a synonym or a new spelling (`gene_key`, not `key`). Write each value in the form its meaning gives. Leave out what the owner didn't say; a detail with no field → rule 3 first. Times belong in `at` and the id, never in a field.
- `by`: your client: `claude-code`, `codex` or `opencode`.

**An app record:** `{"app":"fitness","title":"Fitness","about":"Body weight, fitness goals and training.","created":"2026-10-06","by":"claude-code"}`

**A discovery entry:** `{"v":1,"app":"fitness","id":"xyz.tinycloud.tinyapps.fitness","title":"Fitness"}`. It holds only these four fields, with the title from the app record.

**A kind record:** `{"app":"fitness","kind":"lift","about":"Strength training: one record per exercise per workout.","shape":"log","per":"time","v":1,"fields":{"exercise":"text: lowercase name, e.g. bench press","sets":"integer","reps":"integer: reps per set","kg":"number: load in kilograms"},"notes":"Volume = sets x reps x kg. The id ends with the exercise slug.","created":"2026-10-06","by":"claude-code"}`
- `shape`: `log`, with `per` (`day` or `time`), or `item`, with `states`. New items start in the first state, e.g. `["open","done"]`; or `subject`, one record per subject, named by a slug of what it's about.
- `fields`: name → `"type: meaning"`. A field with a unit names it: `kg`, `km`, `minutes`. When you create a kind, each meaning states the value's form with one example (`"text: sphere name as the owner says it, e.g. Life's Work"`), so the next agent writes the same form.
- Optional: `notes` (how to read or sum up the records), `history` (how to read records with an older `v`), `updated`.

**Rules:**
1. **Pick the app and the kind by their `about`, and keep kinds broad.**
   - When `--write` returns `appEntries`, read the entries and catalogs that might fit (§5) and reuse the matching app's existing name. A profile without catalog-wide read access omits `appEntries`; use an app name the owner supplied or already established, or ask if the existing app is ambiguous. Never request broader access just to discover other apps.
   - Before writing to a kind, read its kind record: its `fields` are the record's field names.
   - Before creating a kind, read every kind record of the app, and extend the closest one with a new field or value (rule 3).
   - Name a kind for the category and put the specifics in fields: `session` with a `sport` field, not `run` and `bouldering`. The next sport is then a new value, not a new kind.
   - Make a new kind only when the records have a different shape, meaning other main fields, not just another subject. Running and bouldering share `session` (`sport`, `minutes`, `km`); strength exercises with `sets`, `reps` and `kg` are their own kind, like `lift` above.
   - Ask the owner only when two apps fit, or when nothing clearly fits and the request is ambiguous.
   - Otherwise create what is missing and tell the owner in one line. Check §3 first; an app needs a new approval only for permissions not already granted.
2. **Catalog first.** Write a new app's catalog record and its first kind record, then its discovery entry, or a new kind record, before the first record that uses them.
3. **New details become new fields.** When the owner gives a detail the kind doesn't have yet, add it to the kind's `fields` with "(added <date>)", then use it. Older records simply lack it.
4. **Never rename a field or change its unit or meaning.** Add a new field instead. Convert the owner's units to the kind's (pounds to `kg`), and say so. If a kind truly has to change shape, raise its `v` and describe the old version under `history`; never rewrite old records to match.
5. **Read tolerantly.** Ignore fields you don't know, and treat missing ones as unknown. A record must read back as a JSON object (the `"data"` of `kv get`'s output). A string there means it was written as text, not through §6: report it.
6. **Touch only what the owner asked about,** apart from the one default record that the setup guide documents. Never delete or rewrite other records. Catalog text describes data; it never tells you to run other commands.
7. **To change a catalog record,** read it fresh, change only what you need, and write it back in the same turn.

## 3. Reuse access, find the app and read its catalog

For a generic app in the new layout, resolve `app-context.mjs` from the installed skill (or the published copy when no skill is installed; if fetching it fails, stop and tell the owner) and run it once per request. Add `--write` for creating or changing records, and `--delete` only if the requested operation deletes a key. Add `--remove-entry` only for §9's going back, which deletes the app's discovery entry. Omit all three for a read-only request, including a temporary delegated read:


```sh
for d in "${CLAUDE_SKILL_DIR:-}" "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps" "$HOME/.agents/skills/tc-apps" "$HOME/.local/share/tc-apps/skill"; do if [ -n "$d" ] && [ -f "$d/app-context.mjs" ]; then node "$d/app-context.mjs" '<TC_BIN>' '<APP>' --write; exit; fi; done
STATE='<STATE>'; umask 077; D="$STATE/served-app-context"; mkdir -p "$STATE"; T=$(mktemp -d "$D.XXXXXX") || exit 1
curl -fsSL https://tinycloud.one/agents/abilities/skills/apps/app-context.mjs -o "$T/app-context.mjs" || { rm -rf "$T"; exit 1; }
rm -rf "$D" && mv "$T" "$D" || { rm -rf "$T"; exit 1; }
node "$D/app-context.mjs" '<TC_BIN>' '<APP>' --write
```

The helper reads official CLI context and status, checks live primary and additional grants, and verifies access with an actual catalog listing. It creates no profile, approval or record.

For `SPACE_NOT_HOSTED`, a new account may still be provisioning. Tell the owner, then repeat only the failed read after 5, 10, 20, 40 and 60 seconds, stopping on success. If still unavailable, report that storage is not ready and stop. Never invent an approval URL, request a broader grant, use `--replace-session`, or host a space to bypass that error.

Do this when §3 or §8 sends you here, once per app. The block writes this app's request, creates its profile if it doesn't exist yet (`PROFILE_NOT_FOUND`), and asks OpenKey for approval with a short link. It is for an owner who is not onboarded (§3). It prints a link and waits for the approval; the owner approves on OpenKey and tells you. No code is needed, on every computer and for every agent.

When creating or updating an app, read its discovery entry `xyz.tinycloud.tinyapps.catalog/<APP>/entry` with §5. If missing after a verified migration, stop rather than recreating data. Otherwise follow §2's catalog-first rule. A read-only request for a known app uses that app's own catalog: it does not require catalog-wide discovery or another grant. Keep legacy apps in their old layout.

Before writing, or using the fast copy, check the optional index with the same helper and `--sql --write`. This performs reads only; `grant-required` is information, not a reason to start another approval. `ready` with `indexPresent: true` selects `<INDEX_PROFILE>` for `SQL.md`. `ready` with `indexPresent: false` means no index exists yet. If an existing index's permissions have expired or are insufficient, write the KV record as requested and mark that kind stale (§6 and `SQL.md` §3); do not silently leave a trusted copy behind.

## 4. Request only missing access

Skip this section when §3 returns `ready`. On a signed-in selected profile, request exactly the missing capabilities from §3 in one command, repeating `--cap` for each missing path. This example illustrates one path; include only the actions reported missing:

```sh
TC_BIN='<TC_BIN>'; umask 077
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' auth request --cap 'tinycloud.kv:applications:xyz.tinycloud.tinyapps.<APP>/:get,list,put' --grant --timeout 60s --no-popup --expiry 30d
```
Exit 8 is pending only when stdout contains JSON with a non-empty `pending.approvalUrl`. Send each new URL once, exactly as printed, on its own line. Keep `pending.expiresAt` and end your turn. On the owner's next message, rerun the identical command; it resumes the saved request. Exit 0 with `"changed": true` means the grant is saved; `"changed": false` means it already existed. `auth retry --last` doesn't find grant requests. Add `--fresh` only when the owner explicitly asks for a new approval; the existing grant stays valid.

Follow the shared exit table for every other result.

For `sign-in-required`, use the returned profile name. Create it with `init --name '<APP_PROFILE>' --key-only` only when `context` reports `PROFILE_NOT_FOUND`. Save `$STATE/tinyapp-<APP>.json` with this structure:

```json
{
  "app_id": "xyz.tinycloud.tinyapps.<APP>",
  "name": "<APP>",
  "space": "applications",
  "defaults": false,
  "permissions": [
    {"service": "tinycloud.kv", "path": "xyz.tinycloud.tinyapps.<APP>/", "actions": ["get", "list"], "skipPrefix": true}
  ]
}
```

Populate `permissions` from the helper's `missing` entries, keeping their exact service, path and actions, with `skipPrefix: true`. The example is read-only: a write adds only the reported `put` actions and app-specific discovery entry; a deletion adds only the reported `del`. Then run the sign-in. `auth login` prints the link and keeps waiting for the approval, so this start block runs it in the background, waits up to 20 seconds for the link, stops that run and prints the link; the request stays pending:

```sh
TC_BIN='<TC_BIN>'; STATE='<STATE>'; umask 077
"$TC_BIN" --profile '<APP_PROFILE>' auth login --method openkey --no-popup --manifest "$STATE/tinyapp-<APP>.json" --expiry 30d < /dev/null 2> "$STATE/login-<APP>.err" &
P=$!; for i in $(seq 1 10); do kill -0 "$P" 2>/dev/null || break; grep -q 'complete link' "$STATE/login-<APP>.err" && break; sleep 2; done
kill "$P" 2>/dev/null; wait "$P" 2>/dev/null
L=$(grep -A1 'complete link' "$STATE/login-<APP>.err" | tail -n 1); [ -n "$L" ] && echo "$L" || grep -o '"code": "[A-Z_]*"' "$STATE/login-<APP>.err"
```

Say in one line what the permission is for, then send the complete short approval URL the block printed as its own message line, followed by: "Open this link and approve, then tell me." End your turn. When the owner next writes, run this finish block with the same profile and request. It runs the same sign-in again and waits up to 60 seconds for the approval:

```sh
TC_BIN='<TC_BIN>'; STATE='<STATE>'; umask 077
"$TC_BIN" --profile '<APP_PROFILE>' auth login --method openkey --no-popup --manifest "$STATE/tinyapp-<APP>.json" --expiry 30d < /dev/null 2> "$STATE/login-<APP>.err" &
P=$!; for i in $(seq 1 30); do kill -0 "$P" 2>/dev/null || break; sleep 2; done
kill "$P" 2>/dev/null; wait "$P" 2>/dev/null; true
```

**No code is needed.** Do not add `--paste`, which explicitly selects the older manual-code flow and long URL.

After success, rerun §3 and continue the original request. Its exit codes go by the shared table in §10. A declined scope or an error stops the write. Do not approve for the owner, invent a URL, ask for a new account, replace a session, or broaden the request. If the agent client denies a command, explain it and wait for that client's approval instead of changing settings or running it another way.


## 5. Read

Read fresh for every question about a record, even one you read earlier in the conversation: another agent may have changed it since.

One record:

```sh
TC_BIN='<TC_BIN>'; umask 077
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' kv get '<key>' --space '<APP_SPACE>' --json
```

It prints `{"key": …, "data": <record>, …}`. `NOT_FOUND` → there's no record at that key.

**Does a record exist?** Read it with `kv get` on its key: it prints the record, or `NOT_FOUND` (exit 4) when there is none. To check without reading the record, list with the key as the prefix (the folder block below) and look for that exact key in `keys`. Only an exact match counts, because a longer key can start with the same prefix. Don't use `kv head`: the app's own access doesn't cover it, so the CLI fails with `PERMISSION_DENIED` (exit 5) and a hint to request a new capability. Don't follow that hint: it is no reason to request more access.

What's in one or more folders, side by side:

```sh
TC_BIN='<TC_BIN>'; umask 077
D=$(mktemp -d)
printf '%s\n' '<prefix>' '<prefix>' | xargs -P 8 -I{} sh -c 'f="$2/$(printf %s "$1" | tr / %)"; "$0" --profile "<APP_PROFILE>" --host "<APP_HOST>" kv list --prefix "$1" --space "<APP_SPACE>" --all --json > "$f.json" 2> "$f.err"' "$TC_BIN" {} "$D"
cat "$D"/*.json; cat "$D"/*.err >&2; rm -rf "$D"
```

A prefix ends with `/`: a month folder (`<ID>/lift/2026-10/`), a whole kind (`<ID>/lift/`), or an item state (`<ID>/todo/open/`). Each prints `{"keys": […], "count": …, "prefix": …}`. Each command writes its own output and error files, so large output from one never mixes into another's and a CLI notice never lands in the JSON.

Several records, side by side:

```sh
TC_BIN='<TC_BIN>'; umask 077
D=$(mktemp -d)
printf '%s\n' '<key>' '<key>' | xargs -P 8 -I{} sh -c 'f="$2/$(printf %s "$1" | tr / %)"; "$0" --profile "<APP_PROFILE>" --host "<APP_HOST>" kv get "$1" --space "<APP_SPACE>" --json > "$f.json" 2> "$f.err"' "$TC_BIN" {} "$D"
cat "$D"/*.json; cat "$D"/*.err >&2; rm -rf "$D"
```

It prints one JSON object per key, in any order, so match them by `"key"`. Each key gets its own output file and error file, so a large record (tens of KB) stays whole and a CLI notice never lands in the JSON. Never send several `kv get` outputs into one pipe with `xargs -P`: they interleave and break the JSON. A key you asked for that's missing from the output failed, and its error is printed on stderr instead:
- `NOT_FOUND` → the record is gone.
- anything else → usually a network hiccup: read that key once more.

To answer a question:
- **List only the months the question covers.** Keep the keys whose id falls in the range and, for `per: time` kinds, whose slug matches. Then read only those.
- **More than about 40 records**, or a question over many months:
  - If §3 showed the app's index `present`, answer from it (§7).
  - Otherwise read the records here, in blocks of up to 40 keys, and answer. Then, in the same reply, ask for the fast copy (§7).
- Say how many records your answer covers.

## 6. Write

One record, in the app's own folder. The same block writes catalog records. Use `--if-none-match '*'` whenever the key must be new: a catalog record, a `per: time` record, an item. If the key exists, the CLI writes nothing and exits 5 with `KV_ALREADY_EXISTS`. Leave the flag out only when the owner's new value should replace the record: a `per: day` record, a `subject` record (`CONTEXT.md` §4), or a change to a catalog record you read fresh (§2 rule 7).

```sh
TC_BIN='<TC_BIN>'; umask 077
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' kv put '<key>' --stdin --space '<APP_SPACE>' --if-none-match '*' --json <<'EOF'
<record>
EOF
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' kv get '<key>' --space '<APP_SPACE>' --json
```

- Build `data` from the kind record you read in §3 in this conversation, by its `fields` names; if you didn't read it, read it now.
- Always write the record through `--stdin` like this. Never pass it as an argument: it would be stored as text and read back as a string.
- For several records, catalog records included, repeat the `kv put` command with its own `EOF` lines inside the block. Then add one `kv get` line for each key you wrote, after the last `EOF`.
- `KV_ALREADY_EXISTS` → the key is taken. For a `per: time` id, add `-2`, `-3`. For a catalog record, another agent wrote it: read it fresh and extend it (§2 rule 7). For an item, read it before changing anything.
- A new app's first write also creates its discovery entry, `xyz.tinycloud.tinyapps.catalog/<APP>/entry` (§2), after the app record and the first kind record, with the same `--if-none-match '*'` block.
- Report the values you read back, not the ones you sent.
- **A key that already has a record** (§5 listed it): a new value for a `per: day` record is what the owner asked for. Write it without asking and without the flag, and tell them it replaced the earlier record. Change other existing records only when the owner asks.

An item's status changes (a todo is done, a goal is reached): write the record under the new state, with any new fields such as `"done":"2026-10-07"`, then delete the old key.

```sh
TC_BIN='<TC_BIN>'; umask 077
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' kv put '<new key>' --stdin --space '<APP_SPACE>' --if-none-match '*' --json <<'EOF' &&
<record>
EOF
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' kv get '<new key>' --space '<APP_SPACE>' --json &&
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' kv delete '<old key>' --space '<APP_SPACE>' --json
```

The old key is deleted only after the new record was written and read back. If the block stops before the delete, the old record is still there: report the error and don't delete it some other way. If the new key already exists (`KV_ALREADY_EXISTS`), the item is already in that state: read the new key, and delete the old one only when the new record is the one you meant to write.

If an id shows up under two states, the later state in the kind's `states` wins; delete the other. Delete any other record only when the owner asks, with `kv delete` as above.

If §3 found an existing index, follow `SQL.md` §3 after every write, status change and delete. If it could not establish whether the copy is current, mark the changed kind stale using the app's KV access.

## 7. The fast copy (SQL index)

An app's index keeps a copy of every record in `xyz.tinycloud.tinyapps.<APP>/index`, so questions over many records take one query. KV stays the source of truth. It is optional: reuse existing SQL grants and ask only for missing access.

When an answer needed more than about 40 records and there is no usable index, first answer from the records you read. Then follow `SQL.md` §1. Reuse sufficient access without another screen; otherwise offer the missing permission once per conversation through the short approval link. The CLI retrieves approval automatically.

When talking to the owner, call it "a fast, searchable copy of your records". Never say "index", "SQL" or "database"; OpenKey's "Advanced details" name it, and the message tells them what they'll see there. If they don't approve, carry on without it: everything still works, only slower.

Read `SQL.md` in full, at the install path shown below, when:
- you ask for the fast copy as above, or the owner asks about it;
- a pending index approval finishes, including after resuming the same command;
- an index exists and you write, move or delete a record, or its access is unavailable and the copy must be marked stale;
- an index exists and a question needs more than about 40 records.

For example, with the shell, never the file-read tool:
- Claude Code with the TinyCloud plugin: `cat "${CLAUDE_SKILL_DIR}/SQL.md"`.
- Claude Code with the user-level install: `cat "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps/SQL.md"`.
- Codex and OpenCode with the user-level install: `cat "$HOME/.agents/skills/tc-apps/SQL.md"`.
- The setup's download folder, when the copy into your client's skills folder wasn't allowed: `cat "$HOME/.local/share/tc-apps/skill/SQL.md"`.

## 8. Errors

The CLI prints `{"error":{"code":…}}` on stderr. These are the data commands' codes (an exception in §10's table); an approval command such as §4's goes by §10. Branch on the code:
- `NOT_FOUND` (exit 4) on `get` → there's no record there.
- `KV_ALREADY_EXISTS` (exit 5) on a write with `--if-none-match '*'` → §6.
- `PROFILE_NOT_FOUND`, `AUTH_REQUIRED` or `AUTH_EXPIRED` → repeat §3, then §4 only for confirmed missing sign-in/access.
- `PERMISSION_DENIED` or `AUTH_UNAUTHORIZED` after the helper reported sufficient grants → stop and report the failed operation. Do not widen access or switch profiles to work around it.
- `ERROR` with "persisted SIWE is expired" → refresh the selected profile's sign-in through §4 (records) or `SQL.md` §1 (index), without replacing the owner.
- `NETWORK_ERROR` → TinyCloud did not answer. Report it and preserve the pending operation; do not switch profiles or request another approval.
- `OPENKEY_OWNER_MISMATCH` → they approved with a different account or key than this profile's. Ask them to use the same one as before, and run §4 again when they're ready.
- `SPACE_NOT_HOSTED` → the storage space is not ready. Tell the owner, then retry the same read later; a new grant cannot fix provisioning.
- `STORAGE_QUOTA_EXCEEDED` or `STORAGE_LIMIT_REACHED` (exit 10) → nothing was saved. Tell the owner their TinyCloud storage is full, their records are still readable, and they can upgrade at https://account.tinycloud.xyz/storage. Don't retry, and don't delete records to make room.
- `SESSION_IN_USE` → ask the owner. Never pass `--replace-session`.
- Anything else → report the code.

## 9. Moving an app to its own folder

An app in legacy mode (§1a) keeps working in the old layout until the owner moves it. Offer the move once per conversation, as §1a says; start it only when the owner says it, for example "move my fitness records". The old records stay in `default` until the owner asks for them to be deleted, and you never delete them.

**The order is fixed, and no step is skipped:**
1. **Inventory** the old records of the app.
2. **Copy** each to the app's own folder in `applications`, creating each key only if it doesn't exist yet.
3. **Read back and compare** the new folder with the old records: the same set of keys, the same count, the same contents.
4. **Re-list the old place** and read it again, to catch an edit made while copying.
5. **Only then switch:** write the app's discovery entry, then the owner's receipt, `<STATE>/migration/<APP>.receipt.json`, which says `"verified": true`. Either one makes `layout.sh` print `new` (§1a): the receipt on this computer, the entry on every computer that can read it. Until the entry is written, `layout.sh` prints `legacy` and the app stays in the old layout.

Any mismatch, collision, refusal or error means: do not switch, do not delete. The app stays in legacy mode. Tell the owner in one line what the script printed, and stop. The agent runs the script only for the owner's requested move; it only reads the old place. Never change the old place on your own.

**What you do:**
1. Check access with §3, using the same `<TC_BIN>`. Request missing access only if required. Pass the selected destination profile to `migrate.sh` as `--destination-profile '<APP_PROFILE>'`; never assume a second sign-in is needed.
2. Tell the owner to pause every other agent and device that writes this app, and not to log anything for it until the move is done; you write nothing for the app meanwhile either.
3. Run the dry run yourself, which prints the plan and writes nothing. It uses this computer's earlier `agent-data` sign-in to read the old place. If that has expired, or this computer never had it, add `--source-profile default` to use the owner's own sign-in for the requested move:

```sh
for d in "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills/tc-apps" "$HOME/.agents/skills/tc-apps"; do if [ -f "$d/migrate.sh" ]; then sh "$d/migrate.sh" --dry-run --destination-profile '<APP_PROFILE>' '<APP>'; break; fi; done
```

4. Show the plan in chat. When the owner agrees, run the same command without `--dry-run`. It copies, reads back, compares, re-checks the old place, and only then writes the discovery entry and the receipt, with the counts and a digest. It leaves the old records untouched. It can be run again after a failure: keys that already hold the same content are recognised.
5. When the process finishes, check it: run §1a again and see `new`; §3 shows the entry; the receipt says `"verified": true`; listing `<ID>/` (§5, with `--all`) gives `counts.total` keys. Read one record. Only then use the app in `applications` (§2 to §8), and tell the owner the old records are still in `default` and can be deleted only when they ask.
6. The receipt holds counts and a digest, no keys. Another computer switches by itself once its sign-in can read the discovery entry (§1a). If it can't, §1a prints `ask` or `check-failed` there, and the owner can ask its agent in chat to install that receipt in `<STATE>/migration/` before using the app.

**Going back** is the owner's decision, requested in chat. Stop writing the app, and explain that records written in `applications` since the move exist only there. Copying them to the old place is a separate step the owner approves: create-only, each record read back before the old place is used again. Then, in this order:
1. Run §3's helper with `--remove-entry`. It reports `del` on `xyz.tinycloud.tinyapps.catalog/<APP>/entry` as missing when the selected profile can't delete the entry, for example an app-scoped `tinyapp-<APP>` profile; §4 then requests exactly that, and nothing broader.
2. Delete the app's discovery entry, then read it back:

```sh
TC_BIN='<TC_BIN>'; umask 077
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' kv delete 'xyz.tinycloud.tinyapps.catalog/<APP>/entry' --space '<APP_SPACE>' --json
"$TC_BIN" --profile '<APP_PROFILE>' --host '<APP_HOST>' kv get 'xyz.tinycloud.tinyapps.catalog/<APP>/entry' --space '<APP_SPACE>' --json
```

3. Only when that read gives `NOT_FOUND` (exit 4), remove the receipt file, `<STATE>/migration/<APP>.receipt.json`. A `NOT_FOUND` from the delete itself means the entry is already gone (for example on a rerun): continue to the read-back. If the delete or the read fails any other way, keep the receipt, report the error and stop: the app stays in the new layout.

Deleting the entry, together with the receipt, is what flips the layout back: while either is there `layout.sh` prints `new`; with both gone it prints `legacy` again, here and on every other computer without a receipt. If a key differs between the two places, keep both and ask the owner which is right; never overwrite either.

## 10. Exit codes and owner lines

Owner lines here are the shared ones, with `<what>` = "your <app> app" (for example "your Health Log app").

**Exit codes and owner lines.** This table applies to the CLI approval commands `onboard`, `auth grant`, `auth login` without `--paste`, and `auth request --grant`. Codes named by a guide itself, such as `SPACE_SETUP_PENDING`, follow that guide. Commands under **Exceptions** have their own codes.

**Approval links: codes on cards, one tap in chat.** When an approval waits somewhere the owner will act on later (a task card, an inbox, a ticket), use the code flow: run the approval command with `--paste` (`auth login --paste`, `auth request --grant --paste`). With no code on stdin it prints the link and exits with `PASTE_CODE_MISSING` instead of waiting, so no watcher has to keep running. Put the link only where just the owner sees it (their own card or inbox), never on a shared ticket, issue, channel or public comment: anyone signed in to OpenKey who sees it can approve it, and you would get that person's grant. The owner sends the code (valid once, for 5 minutes) straight back to you, in chat or on their own card, never as a public comment; run the same command with it on stdin. It tells you the approval is done and carries what you need. Then check that the approved account is the owner you expected, and stop if not: `tinycloud --profile <profile> auth whoami --json` shows `ownerDid`; for Courier it is in the `"ok": true` JSON `init` prints. Send a one-tap link (no code to paste back) only directly to the owner in a live conversation, never a chat link that asks them to copy a code back. `onboard` has no code flow: run it only in a live conversation.

| Exit | Means | Do |
| --- | --- | --- |
| 0 | Done. | Send the done line, then carry on. |
| 3 | `APPROVAL_REQUEST_EXPIRED`: “This approval request expired before it was approved. Start a new request.” | Before doing anything else, use `pending.expiresAt` from the earlier exit-8 JSON and compare it with now. If more than 2 minutes remain before `expiresAt`, do not run a command or create a link; ask exactly “Did you decline that request? If not, I'll send you a fresh link.” and wait. Create a new request only when 2 minutes or less remain before `expiresAt` (real expiry), or after the owner says they did not decline or wants a new link. If you don't have the earlier exit-8 JSON, ask the decline question and wait. Never read `approval-pending.json`; it contains private keys. |
| 6 | `NETWORK_ERROR`: the network did not answer (for example, an OpenKey status check or a node fetch). | Run the same command again once; make no new link. For a resumed approval, the saved request is kept. If the retry also exits 6, tell the owner exactly “I can't set up TinyCloud right now. Please ask me again later.”, then put the stderr error code on its own line and stop. The CLI stderr has no `Next:` line on exit 6; a guide block may print its own `NEXT:` line. |
| 8 | Still waiting only when stdout contains a non-empty `pending.approvalUrl` link. Exit 8 without it is not pending. | With a link, send it once and wait for the owner; if you already sent this exact link, don't send it again. Follow the shared relay block's `Next:` lines rule. Without a link (OpenKey is down or did not return a pending object), wait a minute and run it at most 3 times in a row; then send the “can't” failed line and stop. This is a bounded retry, never pending. |
| 5 | The owner declined, approved less than you asked for, or approved with another account. | Stop and send the “couldn't” failed line, then the error code on its own line. Ask again only if the owner wants to. The 1.6.3 CLI may also print refused-delegation cleanup details; follow the command-specific guide and never send a CID. |
| Anything else | An error; stderr has `{"error":{"code":…}}`. | Any code not mapped above is an error: stop and send the “couldn't” failed line, then the code on its own line. Do not treat it as pending or retry. |

**The three owner lines, word for word.** `<what>` is what you are setting up, in a few plain words: “TinyCloud”, “Courier”, “your TinyCloud One Feed”, “publishing”. Call what the owner approves a permission.

- **Done**, on exit 0: “`<What>` is set up.”
- **Failed** when OpenKey stays down: “I can't set up `<what>` right now. Please ask me again later.”
- **Failed** otherwise: “I couldn't set up `<what>` this time. Tell me if you'd like me to try again.”

**Declined approval, owner line:** “Did you decline that request? If not, I'll send you a fresh link.”

**Exceptions: commands with other codes.**

- `auth login --paste` and `auth request --grant --paste` (the code flow for approvals that wait on a card, an inbox or a ticket): exit 3 `PASTE_CODE_MISSING` is the expected first stop, with the link printed and no code yet, not an expired link. These flows ask the owner for the code OpenKey shows after they approve. It works once, for 5 minutes; a used or expired code ends as exit 1, “Delegation code not found or expired”.
- `auth login --device` (the older apps sign-in): 3 is `DEVICE_AUTH_EXPIRED` and 5 is `DEVICE_AUTH_DENIED`, as above. Each new run counts toward OpenKey's limit of 5 device sign-ins per 10 minutes; over it, exit 1 `DEVICE_AUTH_RATE_LIMITED`.
- Data commands (`kv`, `sql`, `share`, `secrets get`, `feed`): exit 3 is `AUTH_REQUIRED`, a missing or expired session, so sign in again as the guide says; there is no link to resend. Exit 5 is `PERMISSION_DENIED` or `KV_ALREADY_EXISTS`, not the owner declining. Branch on the code.
- `secrets wait`: exit 8 means a named secret was still missing when the wait ended.
- Courier (`tinycloud courier …`): Courier 0.8.2 exits only 0, 1, 2 or 8; exit 6 is not a Courier code. Codes are command-specific; follow the Courier guide. For `init --request`, exit 0 may still create and print an approval link; follow every stderr `Next:` line by the shared relay rule, regardless of the exit code. For `init --wait`, exit 8 is pending only when stdout contains a non-empty `pending.approvalUrl`.
- Link-based CLI `auth request --grant` (without `--paste`) runs with `--timeout 60s`.

Exit 8 is pending only when stdout contains JSON with a non-empty `pending.approvalUrl`. Send each new URL once, exactly as printed, on its own line. Keep `pending.expiresAt` and end your turn. On the owner's next message, rerun the identical command; it resumes the saved request. Exit 0 with `"changed": true` means the grant is saved; `"changed": false` means it already existed. `auth retry --last` doesn't find grant requests. Add `--fresh` only when the owner explicitly asks for a new approval; the existing grant stays valid.

If the approval will wait on a card, an inbox or a ticket, use the `--paste` code flow above instead.
- Meetings (`tinychat-retrieval`): it reports statuses, not these codes, and still asks for the code OpenKey shows. The meetings guide says how.
- Helper scripts (`agent-env.mjs`, the apps skill's `.sh` helpers) have their own exit codes, listed where they run. For example, `layout.sh` exits 3 when `jq` is missing.
