# CLI and agent skills

The `kokumi` binary outputs strict JSON envelopes on stdout, isolates visitor-written strings behind an untrusted marker, and generates its own `read` and `onboarding` skills.

## First-timer setup and READ_TOKEN generation

Kokumi tokens are bearer secrets stored on your Worker, not Cloudflare API keys. Generate one with `openssl rand -hex 32`, save it to your Worker secret, and initialize a chmod 600 CLI config.

```sh
cargo build --manifest-path cli/Cargo.toml --release
openssl rand -hex 32
wrangler secret put READ_TOKEN
./cli/target/release/kokumi init --site main --url https://example.com --token "<READ_TOKEN>"
./cli/target/release/kokumi health
```

> Scope: MUTATE | Vars: <READ_TOKEN>, https://example.com | Expect: exit 0 (health.status ok and CF limits snapshot)

## Self-describing registry and dual skills

Run `kokumi describe --format skill` to give a daily heartbeat assistant read-only instructions, or `kokumi describe --format onboarding-skill` for guided setup ending in `handover` (which drops any write token and verifies HTTP 403 least-privilege).

## Live commands (20)

Every command is listed in kokumi describe. Click any row to inspect flags, agent directives, and linked exit recovery.

| Code | Symbol | Trigger | Meaning | Next move |
| --- | --- | --- | --- | --- |
| READ | kokumi describe | --format json\|skill\|onboarding-skill | Print command schema or self-contained read and onboarding skills. | Feed skill output to your agent before any setup. Refs: #exit-0, #cmd-handover |
| READ | kokumi health | --site <name> | Check edge beacons, classifier canary, and Cloudflare quota limits. | Run after deploy or when exit 3 flags alert. Refs: #exit-3, #exit-5 |
| READ | kokumi moved | --since yesterday\|7d\|date --raw --budget 2k | Report pages that moved relative to baseline median views. | Keep --budget capped before pasting into agent context. Refs: #cmd-post, #exit-4 |
| READ | kokumi post <slug> | --site <name> | Show Day 1, 7, and 30 post scorecard against baseline median. | Inspect pending Day 30 windows on newly published URLs. Refs: #cmd-moved |
| READ | kokumi agents | --cited [--by-page] --since 7d | Compare verified Web Bot Auth crawlers against claimed user agents, or join per-page citations. | Separate verified signatures from claimed substrings. Refs: #cmd-citations, #cmd-health |
| READ | kokumi citations | --since 14d --state <state> --meta --budget 2k --format json\|markdown | Report per-page verified retrieval/training reads alongside confirmed and likely AI referrals. | Filter zero-click-absorption pages or export sponsor Markdown with --format markdown. Refs: #cmd-agents, #exit-4 |
| READ | kokumi campaigns | --since 14d --channel <ch> --campaign <c> --budget 2k --format json\|markdown | Report /go/ edge clicks, preview bots, and 5-field UTMs. | Filter by --channel or export Markdown. Refs: #cmd-link-list, #exit-4 |
| READ | kokumi privacy | --table --format json\|ts\|md | Fetch stored vs omitted fields from live Worker config. | Confirm raw_ip_opt_in matches your compliance stance. Refs: #exit-0 |
| READ | kokumi snippet | --platform html\|astro\|shopify\|... [--external] | Print CMS script tag pointing to your Worker /visit.js route. | Paste into base layout head then run verify. Refs: #cmd-verify |
| SETUP | kokumi onboard | --site <name> --format json\|human | Check onboarding stage, wrangler readiness, and next step. | Start here or resume onboarding at any time. Refs: #cmd-init, #exit-0 |
| SETUP | kokumi init | --url --token [--write-token] [--dry-run] | Write chmod 600 site config binding your Worker URL and Bearer token. | Follow with health check to verify auth and connectivity. Refs: #cmd-health, #exit-4 |
| WRITE | kokumi deploy | --config <file> [--dry-run] | Wrap wrangler authentication and push Worker bundle to Cloudflare. | Run --dry-run first to inspect target wrangler config. Refs: #cmd-verify, #exit-1 |
| WRITE | kokumi verify | --path / [--dry-run] | Send synthetic beacon hit and confirm views increment cleanly. | Re-run after adding snippet tag to confirm ingestion. Refs: #exit-3, #cmd-handover |
| WRITE | kokumi import | <umami\|plausible> --file <path> [--dry-run] | Validate and upsert Umami or Plausible CSV/JSON daily aggregates. | Run with --dry-run first; re-runs use Idempotency-Key. Refs: #exit-2 |
| WRITE | kokumi goal add | <name> --kind <kind> --target <val> [--dry-run] | Create or update conversion goal definition in D1 goals table. | Check --dry-run payload before writing production goals. Refs: #exit-2, #exit-4 |
| WRITE | kokumi digest on | --schedule daily\|weekly | Render ready-to-install cron and systemd schedule units. | Inspect generated unit before enabling unattended heartbeat. Refs: #cmd-handover |
| SETUP | kokumi handover | [--dry-run] [--idempotency-key <key>] | Drop write_token locally and verify HTTP 403 least-privilege probe. | Required final step ending any onboarding session. Refs: #exit-0, #exit-4 |
| WRITE | kokumi link add | <channel>/<slug> --target <path> [--utm-*] [--dry-run] | Create or update a first-party /go/<channel>/<slug> campaign link. | Run --dry-run first and verify target_path. Refs: #cmd-link-list, #exit-4 |
| READ | kokumi link list | [--channel <ch>] [--format json\|markdown] | List registered active and archived /go/<channel>/<slug> links. | Check slugs before minting new links. Refs: #cmd-campaigns |
| WRITE | kokumi link archive | <channel>/<slug> [--dry-run] [--idempotency-key <key>] | Archive a /go/<channel>/<slug> link while preserving historical metrics. | Run --dry-run first to inspect the archive request. Refs: #cmd-link-list, #exit-4 |

## Output safety and exit codes

All visitor-controlled paths and referrers are NFKC-normalized, stripped of control and bidi characters, capped at 120 chars, and wrapped as {"untrusted": "..."}. Bearer tokens never print to stdout or stderr.

| Code | Symbol | Trigger | Meaning | Next move |
| --- | --- | --- | --- | --- |
| 0 | ok | Valid command, valid scope, healthy upstream JSON envelope | Command succeeded and emitted deterministic stdout envelope. | Parse JSON payload and continue workflow. Refs: #cmd-describe, #cmd-handover |
| 1 | error | Network timeout, TLS failure, or unexpected Worker error | Unexpected operational failure before contract completion. | Check Worker URL reachability and wrangler tail logs. Refs: #cmd-deploy, #cmd-health |
| 2 | bad_usage | Unknown subcommand, missing flag, or invalid argument value | Invalid command or flags rejected before network call. | Re-read live schema via kokumi describe --format json. Refs: #cmd-describe |
| 3 | health_alert | Zero recent beacons, D1 write drop, or canary drift detected | Beacon quiet, write drop, or classifier canary drift. | Send synthetic hit with kokumi verify --path /. Refs: #cmd-verify, #cmd-health |
| 4 | auth | Missing config token, HTTP 401 unauthorized, or HTTP 403 scope error | Missing, invalid, expired, or wrong-scope token. | Re-run kokumi init with valid token or confirm handover 403. Refs: #cmd-init, #cmd-handover |
| 5 | limit | Worker API_LIMIT 429 or Cloudflare free-plan quota threshold | Rate limit or Cloudflare free-plan quota reached. | Pause polling and check health quota usage before retrying. Refs: #cmd-health |

## Related

- [Self-hosting guide](/docs/self-hosting)
- [Worker API reference](/docs/api)
- [Data and privacy ledger](/docs/data-and-privacy)