# Edit your Robutler app

You're an AI coding agent helping someone **edit their own Robutler app** — a small, self-contained web app (an HTML/JS/CSS bundle) that runs sandboxed in an iframe on the Robutler canvas. Unlike a remix, there is **no fork**: you edit the owner's existing bundle in place and re-publish the update. This guide gets you connected and productive. (You fetched this file from a Robutler host; everywhere below, **"this host"** means that same origin.)

## 1. Connect the Robutler MCP server

The portal exposes a Model Context Protocol server with the tools to read, edit, and publish apps.

- **URL:** `/mcp` on this host (e.g. `https://robutler.ai/mcp`) — Streamable HTTP transport.
- **Auth:** OAuth. Your environment opens a browser to authorize; approve as the app's owner (it's your app). There is **no API key to paste** — the OAuth flow handles it.

How to add it, per environment:

| Environment | How to connect |
|---|---|
| **Claude Code** | `claude mcp add robutler --transport http https://<this-host>/mcp` then run `/mcp` to authorize |
| **Cursor** | Settings → MCP → Add → URL `https://<this-host>/mcp`, then authorize (or use a one-click MCP install deeplink) |
| **Codex** | Add `[mcp_servers.robutler]` (URL transport) to `~/.codex/config.toml`, then authorize |
| **Antigravity** | Settings → MCP servers → Add `https://<this-host>/mcp`, then authorize |

## 2. Tools you have

Once connected you'll see these (call `tools/list` if unsure):

- **`widget_download(folderId)`** — list + read the files in the app's bundle folder (returns each file's `id`). Text comes back inline up to a byte budget (default 200 KB); files past it are listed with `omitted: true`, so call again with `paths: [...]` for the ones you need. **Start here.**
- **`widget_put_files(folderId, files[])`** — write text files in the bundle (the EDIT step). Full-replace (`{ path, text }`) or patch in place (`{ path, edits: [{ oldText, newText }] }` — prefer `edits` for large files; they work at any size). UTF-8 text, 32 MiB per file. File edits are live in the bundle: reload the running instance to see them (`__reload` below).
- `widget_instantiate(refId, …)` — mount the app on a canvas and get a live URL to preview. Pass the **widget id** (an owned bundle `folderId` is auto-resolved); the result's `mountKind` should say `"widget"`.
- `widget_publish(folderId)` — re-deploy the bundle as the app. Needed when `widget.json` or the declared command surface changed (those are snapshotted and refresh on republish); plain HTML/CSS/JS edits are live on reload without it. Appears on accounts with publishing enabled.
- `widget_snapshot(workingWidgetId, …)` — cut an immutable, versioned release (do this when the change is solid, so a new version lands in the app's marketplace page). Same availability as publish.
- `workspace_list_open` / `workspace_widgets_list` / `workspace_widgets_invoke` / `workspace_widgets_logs` — drive and debug the LIVE instance in the owner's open tab: `__reload` after edits (bump `?v=` tokens on assets you changed), `__screenshot`, console logs + load state.
- `widget_dev_token(…)` — mint a short-lived bearer so a locally-running copy can reach the portal.
- `widget_fn_logs(agentId, …)` — read the app's server-side function logs when debugging.
- A server function or release hook that uses `ctx.kv` declares it in its `widget.json` tool entry: `"permissions": { "kv": { "self": "rw", "agent_scope": true } }`, or every read and write is refused. The release hook format (`lifecycle.dataVersion`, `lifecycle.migrate`) is at `/develop/release-hooks`.
- `app_status(appId)`: the draft, the current release, whether the app is public, and what the draft would let the app reach that the release does not.
- `app_release_request(appId)`: ASKS the owner to release the draft. It releases nothing by itself; give the owner the `approvalUrl` it returns.
- `app_use_version_request(appId)`: for a private app, ASKS the owner to start using the draft (so new server functions answer) without publishing it.
- `widget_put_files(folderId, files[], branch: "people/<your user id>")` writes to your own branch instead of the draft; `casfs_branch_diff` shows what it changes and `casfs_branch_merge` brings it in (the Apply step).
- `app_rollback_request(appId, root?)`: ASKS the owner to restore an earlier release; without `root` it lists them (so does `app_status`).
- `app_unpublish(appId, mode)` (`unlist`: out of Discover, the link still works; `unpublish`: the listing goes) and `app_delete(appId)` (to Trash for everyone, restorable for 30 days) ASK the owner: each answers with what would happen and sends the owner a link to where they do it themself. Neither changes anything; show the person the effects and the `approvalUrl`.

From a terminal or a coding agent, the `robutler` command line does the same with a folder on disk: `npm install -g robutler`, `robutler login`, then `robutler app create`, `push`, `status` and `release` (see `/develop/app-cli`).

## 3. The edit workflow

1. **Read** the bundle: `widget_download(<folderId>)`. Look for **`AGENT.md`** or `README.md` inside it — most apps ship a guide with the architecture and "how to add X" recipes. Read it before changing anything.
2. **Orient**: the entry point is usually `index.html` plus a main JS module; `widget.json` is the manifest (name, permissions, declared tools).
3. **Confirm the change** with the owner, then **edit in place** with `widget_put_files(folderId, [{ path, text }])`. This is the owner's live app — keep it working; don't introduce a build step and keep the bundle self-contained (bundle or CDN-load deps).

   The app talks to the platform through the global `host` object (the SDK at `/widgets/sdk.v2.js`). Core surface: `host.ready()` (await before use), `host.kv` (per-INSTANCE key/value — small JSON, NOT shared between two instances of the same document; see §4), `host.fn` (call the app's own server functions declared in `widget.json`), `host.content` (PROJECT-folder file storage — binaries shared across the whole document; see §4), `host.collab` (Yjs multi-user rooms — the shared document body), `host.commands.handle(name, fn)` (the agent-control commands declared in the `<meta name="robutler:widget">` tag), plus `host.live`, `host.user`, `host.discover`. The authoritative surface is the TypeScript typedefs at **`/widgets/sdk.v2.d.ts`**; prose reference at **`/develop/sdk-v2`** and per-namespace guides under `/develop/` (`host-kv`, `host-content`, `host-fn`, `host-collab`, `host-live`, `host-user`).
4. **See it live and verify.** File edits are served from the bundle immediately: `workspace_widgets_invoke(<itemId>, "__reload")` on the open instance picks them up (bump any `?v=` asset tokens you touched), `__screenshot` shows the result, and `workspace_widgets_logs` should read `loadState: "ready"` with a clean console. To mount a fresh preview, `widget_instantiate(refId=<widget id>)` and check `mountKind: "widget"`.
5. **Republish when the manifest changed.** `widget_publish(<folderId>)` re-stamps `widget.json` and the command surface (needed for those; not for plain file edits); cut a release with `widget_snapshot` when it's solid so the new version shows on the app's page. Both are available on publishing-enabled accounts.

6. **A change to stored data goes with a release hook.** If the new version keeps data in a different shape (a renamed field, a new key layout in the app's function storage, `ctx.kv`), add the hook in the same change: raise `lifecycle.dataVersion` in `widget.json` by one and point `lifecycle.migrate.up`, `down` and `post` at exported functions of the app that convert the stored data in batches and are safe to run twice (`/develop/release-hooks`). `up` runs when the app is released, before the new version goes live, where release hooks are switched on. A hook reaches only function storage: data kept in files or per person is converted by the code that reads it. Never edit stored data by hand to fit new code.

   An agent working on the app inside Robutler writes to its own branch, and the owner applies its changes into the draft; the release, and its hooks, follow from the draft whichever way it was changed.

**Safari gotcha: load the entry module by absolute URL.** The bundle runs in a sandboxed `srcdoc` iframe with an injected `<base href>`. Chrome honors that base when resolving relative module specifiers; Safari/WebKit does not, so an inline boot script that does `import('./app.js')` works in Chrome but fails in Safari (*"Module name, './app.js' does not resolve to a valid URL"*). Resolve the entry against `document.baseURI`:

    const app = await import(new URL('./app.js', document.baseURI).href);

This only matters for the FIRST hop from the inline `<script type="module">` in `index.html`. Static `import`s inside `app.js` resolve against its own (now absolute) URL and are fine.

## 4. Where shared content lives (read before storing ANYTHING)

A document is often open in **two instances at once** (a second tab, a collaborator, a split view). They all join the same collab room, so for every byte you persist ask: *will the other instance see it?* Store shared user-content in the **project folder**, never in per-instance scratch:

- **The document body / structured state** → the **collab room** (Yjs via `host.collab`). The shared CRDT; every instance sees edits live (text, shapes, slides, rows, state).
- **Binary media** (images, figures, audio, exports) → **`host.content.put(file, { name })`**. Writes a name-addressed file into the open PROJECT FOLDER (ADR-0022), shared across every instance and collaborator. Reference as `content://<name>`, resolve with `host.content.get(name)`.
- **Binding**: `host.content` is project-scoped once bound; the shared collab-kit calls `host.documents.bind(projectFolderId)` on open. A bespoke open flow must call `host.documents.bind(<folderId>)` itself, or media 404s in the second instance.

**Anti-patterns that break sharing:**
- ❌ `localStorage` for user content — browser-local; never reaches the server or a collaborator.
- ❌ `host.kv` for anything shared — it's **per-instance** (small JSON only, never binaries). Use it only for that instance's private UI prefs.
- ❌ Large images as `data:` URLs in the Yjs doc — it bloats the CRDT; use `host.content`.

## Who can call it

Each server function in `widget.json` says who may call it with `httpAuth`: `session` is the app's owner only, `visitor_session` any signed-in person, and `public` anyone. A missing `httpAuth` means `public`. A function with `expose: ["tool"]` is also a tool on the app's agent with no scope of its own, so any caller that agent's run lets in can trigger it.

- Never widen an existing `httpAuth` or `permissions` entry unless the owner asks for exactly that, and say what it opens.
- A function you add that reads or changes the owner's private data, or uses the owner's keys, secrets or connected accounts, is `session`. `public` is for data anyone may see and for webhooks that verify their own signature.

## Attribution markers (keep credit fair)

Robutler shares app revenue with the people whose code gets used, by `@handle` marks across the code paths.

    host.attribution.mark('@yourhandle/export_pdf');                       // a discrete action
    const s = host.attribution.span('@yourhandle/render'); /* ... */ s.end();  // time spent

- **Keep existing marks intact.** This app may already carry `@contributor` marks from past remixes/backmixes. **Never remove, relabel, or reassign another author's marks to the owner or yourself** — that's a Terms violation and unfair (it strips their earnings). New code *you* write on the owner's behalf gets the owner's own mark (a mark with no `@handle`), or the owner's handle.
- Be fair and honest: the code is source-available and cross-checkable; disputes are settled in public discussion, not by the platform.

## Licensing and terms (important)

By editing and publishing here you and the code stay bound by the Robutler Terms of Service, in particular the app-licensing terms (Section 7):

- **It stays on Robutler.** Apps are source-available on the Service but not licensed for use off it. Do not deploy, host, redistribute, or export the app (or anything it incorporates) to another platform (7.3-7.4). The download/export tools are for the owner's own backup.
- **Keep attribution and lineage intact** (7.5) — see above.
- **You are responsible for third-party libraries you add** (7.6) — prefer permissive licenses (MIT, Apache, BSD, ISC).
- **Content rules apply** (Section 6) — no illegal, infringing, or abusive content.

Full terms: `/docs/terms-of-service`.

## 5. Your task

The owner sent you here to edit their own app. The app's **bundle folder id** (and **name**) are in the message that opened this session — it is the owner's live bundle, edited in place (no copy).

Read it first (`widget_download`, then any `AGENT.md` / `README.md`), then ask the owner what they'd like to change. After editing, `__reload` the live instance and verify; republish (`widget_publish`) when the manifest or command surface changed, and `widget_snapshot` to cut a version that shows on the app's marketplace page.
