@ziprack/sdk 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,11 @@
8
8
  - [Permissions](#permissions)
9
9
  - [Quotas](#quotas)
10
10
  - [Seeding](#seeding)
11
+ - [Data Across Versions](#data-across-versions)
12
+ - [Data Migrations](#data-migrations)
13
+ - [Snapshots](#snapshots)
11
14
  - [How It Is Stored](#how-it-is-stored)
15
+ - [Rack Side](#rack-side)
12
16
 
13
17
  <a name="introduction"></a>
14
18
  ## Introduction
@@ -67,11 +71,19 @@ what happens to it when an attempt ends.
67
71
  | `attempt` | One attempt | **Frozen when the attempt completes**, alongside `state` | `attempt@1` |
68
72
  | `user` | One participant | Outlives any attempt | — |
69
73
  | `room` | Everyone in the room | The room | `broadcast@1` |
70
- | `applet` | Everyone on this applet version | The version. Read-mostly: seed data, shared catalogs | — |
74
+ | `applet` | Everyone using the applet, on every version | The applet. Carries over every promotion. Seed data, shared catalogs | — |
71
75
 
72
76
  A session that has no attempt or no room cannot touch a collection scoped to one; the call is
73
77
  refused with `forbidden` rather than silently falling back to something else.
74
78
 
79
+ `user`, `room` and `applet` records belong to the applet, not to a version, so they are still there
80
+ after you promote a new one — see [Data Across Versions](#data-across-versions). `attempt` records
81
+ stay with their attempt, and so with the version it ran on.
82
+
83
+ A session reads and writes a collection through the definition of the version it launched on. An
84
+ attempt pinned to an earlier version keeps that version's schema, indexes and quota until it
85
+ completes, even after a later version changed them.
86
+
75
87
  <a name="reading-and-writing"></a>
76
88
  ## Reading and Writing
77
89
 
@@ -183,33 +195,230 @@ The seed file is a JSON array of records, each one a document exactly as `create
183
195
  ]
184
196
  ```
185
197
 
186
- The installer loads it when the version is installed, inside the same transaction, and holds every
187
- record to the rules a write would face: it must be an object, it must match the schema, it must fit
198
+ The seed is **checked at install and loaded at promotion**. At install the Rack holds every record
199
+ to the rules a write would face: it must be an object, it must match the schema, it must fit
188
200
  `max_bytes`, and the file must fit `max_records`. One bad record refuses the whole package, and the
189
201
  error names the record's index and the failing path. A package with a bad seed never installs.
190
202
 
203
+ When the version is promoted, the seed loads into the collection **only if its live dataset is
204
+ empty**. A seed is the starting data, never a reset: once the applet has records, a later version's
205
+ seed file is not loaded, even if it changed. The promotion plan says so. To change records that
206
+ already exist, ship a [data migration](#data-migrations).
207
+
191
208
  Things to know:
192
209
 
193
210
  - **Only `applet` scope.** No room, participant or attempt exists at install, so nothing else has
194
211
  a scope to seed into.
195
- - **Every version gets its own copy.** `applet` records belong to the version, so each version
196
- loads its own seed file. Records written to the previous version stay with that version.
212
+ - **Loaded once, then the data is yours.** Records carry over every promotion. If the collection is
213
+ emptied (preview records do not count), the next promotion loads the seed again.
197
214
  - **Seeded records have no owner.** `owner_id` is `null`, so under the default `owner` permission
198
215
  participants can read them but cannot change them. Declare `members` or `facilitator` if they
199
216
  should be editable.
200
- - **No `RecordChanged` events.** Nobody is listening to a version that has not launched yet.
217
+ - **No `RecordChanged` events.** The seed loads inside the promotion, before any session runs the
218
+ new version.
201
219
  - **The quota still applies.** A seed is capped at 5000 records (`max_records_per_scope`), and so
202
220
  is everything later written to that scope.
203
221
  - **Not in the mock Rack.** The SDK's `MockRack` does not load seed files. In local development,
204
222
  create the records from your dev harness.
205
223
 
224
+ <a name="data-across-versions"></a>
225
+ ## Data Across Versions
226
+
227
+ Promoting a version carries the applet's data over to it. `user`, `room` and `applet` records are
228
+ kept and checked against the new version's collections; `attempt` records are left alone, in the
229
+ shape their attempt wrote them. Nothing about your data changes at install: a new version's
230
+ collections are checked, and names the applet has never had are registered empty, but the live
231
+ version keeps reading what it always read until promotion.
232
+
233
+ At promotion the Rack plans the cutover. For each declared collection it works out whether it is
234
+ `added`, `removed`, `restored`, `changed` or `unchanged`, describes the structural diff (a new
235
+ required property, a narrowed enum, a changed quota), and — when no migration runs — validates
236
+ every existing record against the new schema and quota. A promotion that would leave records the
237
+ new version rejects, strand a collection that holds records, or roll migrations back is not done
238
+ silently: it is refused until someone confirms, and the refusal carries the plan. See
239
+ [Promotion](publishing.md#promotion) for how that looks from the admin API, the CLI and MCP.
240
+
241
+ | The new version… | What happens at promotion |
242
+ |---|---|
243
+ | Changes nothing about its collections | Promotes. Records are untouched; no snapshot is taken. |
244
+ | Makes a compatible change: a new optional property, a wider enum, a new index, a new collection | Promotes. Existing records validate, so they carry over as they are. A snapshot is taken first when a collection holding records changed. New collections start empty, or with their seed. |
245
+ | Ships a [migration](#data-migrations) that has not run on this applet | Must be promoted from the Rack's UI, which runs the migrations in the browser, checks the result, then commits it. The CLI and MCP refuse it (`migration_required`). |
246
+ | Makes an incompatible change with no migration | Refused with `data_loss`, naming how many records the new schema rejects, with samples. Once confirmed, rejected records are deleted (`invalid: discard`, the default) or kept as they are (`keep`). A kept record fails validation on its next update until it is fixed. |
247
+ | Drops a collection that holds records | Refused with `data_loss`. Once confirmed, the collection is set aside with its records; `ziprack:db:prune` deletes them. A later version that declares it again gets them back. |
248
+ | Changes a collection's `scope` | Refused **at install**: `Collection [x] cannot change scope from [room] to [applet]; use a new collection name.` A migration cannot move records between scopes either. |
249
+ | Lowers `max_records` below what a scope holds, or `max_bytes` below a record's size | Refused with `data_loss`. Once confirmed, oversize records count as rejected; a scope over its new record quota keeps its records but refuses `create()` with `quota_exceeded` until it is back under. |
250
+ | Ships a changed seed file for a collection that has records | Promotes. The seed is not loaded, and the plan notes it. Use a migration. |
251
+ | Is an earlier version that lacks migrations already run (a rollback) | Those migrations run `down`, newest first, from the source the Rack stored when they ran up — in the browser, like any migration, and flagged as a rollback. A migration with no `down` stops it; restore the [snapshot](#snapshots) taken before it ran instead. An earlier version with no migrations involved is treated like any other change. |
252
+ | Is the applet's first | Nothing to carry over. Its migrations are recorded as applied without running, and the seed loads. |
253
+
254
+ Uploading without activating changes nothing about the data; the plan is made when someone
255
+ promotes. An upload *with* activate tries the promotion, and if the cutover needs a decision the
256
+ version stays installed but not current.
257
+
258
+ Two more rules apply to every cutover:
259
+
260
+ - **Preview records are dropped where shapes change.** A cutover that runs migrations deletes every
261
+ [preview](attempts.md#preview-runs) record of the applet; one that does not deletes the preview
262
+ records of the collections whose definition changed. Migrations never see preview records.
263
+ - **Pinned attempts keep the old shape.** An in-progress attempt on the outgoing version reads and
264
+ writes `user`, `room` and `applet` records through that version's definition until it completes.
265
+ After a cutover that reshapes them, it can still write records in the old shape. If your applet is
266
+ pinned, write code that tolerates both, or reshape only what in-progress attempts do not write.
267
+
268
+ <a name="data-migrations"></a>
269
+ ## Data Migrations
270
+
271
+ When a new version needs existing records in a new shape — a renamed property, a split collection,
272
+ a value back-filled from another — ship a data migration. A migration is an ES module in a
273
+ `migrations/` directory at the zip root:
274
+
275
+ ```
276
+ my-applet-2.0.0.zip
277
+ ├── manifest.json
278
+ ├── migrations/
279
+ │ ├── 2026_09_01_090000_add_difficulty.js
280
+ │ └── 2026_10_09_120000_split_archived_ideas.js
281
+ └── …
282
+ ```
283
+
284
+ | Rule | |
285
+ |---|---|
286
+ | Name | `YYYY_MM_DD_HHMMSS_name.js` (or `.mjs`), lowercase, directly inside `migrations/`. Anything else refuses the package at install. |
287
+ | Order | Name order. The timestamp prefix is the order. |
288
+ | Cumulative | **Keep every migration you have ever shipped.** The Rack runs the ones this applet has not run yet, so any earlier version can be brought forward. |
289
+ | Self-contained | Each file is imported on its own, with no network. Do not import other files or packages. The starter copies `migrations/` into the zip as written, unbundled. |
290
+ | Exports | `up(db, ctx)`, and `down(db, ctx)` if the change can be undone. Either as named exports or on the default export. |
291
+
292
+ ```js
293
+ // migrations/2026_09_01_090000_add_difficulty.js
294
+ export function up(db, ctx) {
295
+ for (const question of db.questions) {
296
+ question.data.difficulty ??= 'medium';
297
+ }
298
+ ctx.log(`set a difficulty on ${db.questions.length} questions`);
299
+ }
300
+
301
+ export function down(db) {
302
+ for (const question of db.questions) {
303
+ delete question.data.difficulty;
304
+ }
305
+ }
306
+ ```
307
+
308
+ `db` holds the applet's carried records, by collection name: every `user`, `room` and `applet`
309
+ collection, including ones the outgoing version no longer declares. A record is:
310
+
311
+ ```ts
312
+ interface MigrationRecord {
313
+ id: string;
314
+ scope_id: number; // the room or participant; 0 for applet scope
315
+ owner_id: number | null;
316
+ data: object; // the document, as create() took it
317
+ created_at: string;
318
+ updated_at: string;
319
+ }
320
+ ```
321
+
322
+ Mutate it in place, or return `{ collection: records[] }` for the collections you replace. A
323
+ collection you return replaces that collection; one you leave out is kept unchanged, so its records
324
+ must already fit the target version.
325
+
326
+ ```js
327
+ // migrations/2026_10_09_120000_split_archived_ideas.js
328
+ export default {
329
+ up(db) {
330
+ return {
331
+ ideas: db.ideas.filter((idea) => idea.data.status !== 'archived'),
332
+ archived_ideas: db.ideas.filter((idea) => idea.data.status === 'archived'),
333
+ };
334
+ },
335
+
336
+ down(db) {
337
+ return { ideas: [...db.ideas, ...db.archived_ideas] };
338
+ },
339
+ };
340
+ ```
341
+
342
+ A record that appears under another collection's name moves there, keeping its id. To add a record,
343
+ leave out `id`; it is given a new one. A collection the target version does not declare cannot be
344
+ written to: returning it is ignored.
345
+
346
+ `ctx` is `{ direction, applet, from, to, scopes, log }`: `'up'` or `'down'`, the applet slug, the
347
+ outgoing and target version strings (`from` is `null` when nothing was current), each collection's
348
+ scope by name, and `log(...)` for lines kept with the snapshot. `console.log`, `info`, `warn` and
349
+ `error` are captured the same way.
350
+
351
+ **The Rack checks what comes back.** Every record the cutover leaves in place, returned or kept,
352
+ must match the target version's schema and fit its quota. A record cannot change its `scope_id` or `owner_id`, and can move only to
353
+ a collection with the same scope. A new record in a `user` or `room` collection needs the
354
+ `scope_id` of a participant or room that already has records, and its `owner_id` must be `null` or
355
+ someone who already owns one. Records missing from a returned collection are deleted. If any check
356
+ fails, nothing is written and the errors name the collection, the record and the problem. These are
357
+ checks on structure, not on which participant's values end up in which record: migrations see every
358
+ scope's data and are [privileged code](security.md#the-migration-runner).
359
+
360
+ Migrations run in the browser of whoever promotes, in a sandboxed frame with no network access
361
+ (see [Security](security.md#the-migration-runner)), one after another, 30 seconds each by default.
362
+ A migration that throws, times out, lacks the function it needs, or returns anything other than
363
+ `{ collection: records[] }` stops the run, and nothing is written. If the applet's data changes
364
+ while they run, the promotion is refused with `data_changed` and they run again.
365
+
366
+ The Rack keeps a ledger of which migrations have run on each applet, with each one's source. Going
367
+ forward, pending `up`s run in name order. Promoting a version that lacks some applied migrations
368
+ runs their `down`s in reverse, from the stored source, so a rollback works even after a later
369
+ package dropped the file. A migration with no `down` cannot be rolled back; the Rack says so, and
370
+ you restore the snapshot taken before it ran.
371
+
372
+ `attempt` records are never passed in: they stay in the shape their attempt wrote them. Preview
373
+ records are not passed in either.
374
+
375
+ Migrations are plain functions over plain arrays, so test them like any other: import the module,
376
+ call `up` on a fixture, assert on the result. The mock Rack does not run them.
377
+
378
+ <a name="snapshots"></a>
379
+ ## Snapshots
380
+
381
+ Before every cutover that changes data, the Rack copies the applet's entire dataset: every record
382
+ in every collection and scope, preview and set-aside ones included, the collection registry, the
383
+ migration ledger and the applet's [role](auth.md#roles) assignments. The copy is written before the
384
+ promotion's transaction opens and marked applied if the cutover commits, so a migration that
385
+ wrecks the data can always be undone.
386
+
387
+ The transaction then locks the applet's collections, which holds record writes off until it
388
+ commits, and checks the data is still what the cutover was planned and snapshotted from. A write
389
+ that landed in between refuses the promotion with `data_changed`, before anything is deleted, so a
390
+ cutover never removes a record its snapshot does not hold. Likewise, a promotion refuses with
391
+ `version_changed` if another one committed while it was being prepared.
392
+
393
+ Snapshots are also taken before every restore, so a restore can itself be undone, and on request:
394
+
395
+ ```sh
396
+ php artisan ziprack:db:snapshot my-applet
397
+ php artisan ziprack:db:snapshots my-applet # list them, with ids
398
+ php artisan ziprack:db:restore my-applet 42 --force
399
+ ```
400
+
401
+ A restore replaces all of the applet's records, its collection registry, its ledger and its role
402
+ assignments with the snapshot's, then makes the version that was current when it was taken current
403
+ again, if that version is still `ready`. The file's sha256 is checked first; a snapshot that does
404
+ not match is not restored.
405
+
406
+ The file is gzipped NDJSON: a header line (`format: ziprack-snapshot/1`, with the registry, the
407
+ ledger, role assignments and record counts), then one line per record. It is stored at
408
+ `ziprack/snapshots/{slug}/…ndjson.gz` on `ziprack-db.snapshots.disk`, which defaults to
409
+ `ziprack.disk`. **That disk must be persistent** — S3 in production, like packages.
410
+
411
+ The newest `ziprack-db.snapshots.keep` (10) are kept per applet, and never at the expense of the
412
+ newest one taken before a cutover.
413
+
206
414
  <a name="how-it-is-stored"></a>
207
415
  ## How It Is Stored
208
416
 
209
417
  Worth knowing, because it explains the constraints above.
210
418
 
211
419
  Every collection's records live in one shared `ziprack_records` table, created by a migration and
212
- keyed by collection and scope. The document lives in a JSON column; filters and ordering on an
420
+ keyed by collection and scope — the attempt, participant or room id, or `0` for `applet` scope.
421
+ The document lives in a JSON column; filters and ordering on an
213
422
  `indexed` path read that path out of the JSON, typed from the declared schema so numbers compare as
214
423
  numbers. Every query is narrowed by an index on `(collection_id, scope_id)` first, and the quota caps
215
424
  a scope's records, so that read is always over a small, bounded set. The declared schema is still
@@ -221,10 +430,44 @@ with `is_preview` set when it is created. The flag never changes after that, so
221
430
  the flag of whoever wrote each card. Applets never see it; it exists for the admin, which leaves
222
431
  preview records out unless asked.
223
432
 
224
- **Installing a version never changes the database schema.** Adding a collection or an indexed path
225
- only writes rows, so the install stays one transaction on every database. Collections that no
226
- current manifest declares any more keep their records until you run:
433
+ **Installing or promoting a version never changes the database schema.** Adding a collection or an
434
+ indexed path only writes rows, so both stay one transaction on every database. The collection
435
+ registry follows the current version: promotion updates each definition and sets aside the
436
+ collections the new version no longer declares. They keep their records until you run:
227
437
 
228
438
  ```sh
229
439
  php artisan ziprack:db:prune
440
+ php artisan ziprack:db:prune --stranded # also the rows described below
441
+ ```
442
+
443
+ **Upgrading from a Rack before v1.8.** Applet-scoped records used to belong to the version that
444
+ wrote them, so every upgrade started from an empty dataset. A migration re-keys the current
445
+ version's rows to the applet; earlier versions' rows are left where they were, unreachable. Bring
446
+ one version's rows back into the live dataset, or delete them all:
447
+
448
+ ```sh
449
+ php artisan ziprack:db:recover my-applet 1.2.0 [collection] [--replace] [--force]
450
+ php artisan ziprack:db:prune --stranded
230
451
  ```
452
+
453
+ `recover` adds to the live records (`--replace` deletes those first), reports how many recovered
454
+ records the current schema rejects, and takes a snapshot before it changes anything.
455
+
456
+ <a name="rack-side"></a>
457
+ ## Rack Side
458
+
459
+ | Command | Does |
460
+ |---|---|
461
+ | `ziprack:db:prune [--stranded] [--force]` | Delete set-aside collections and their records; with `--stranded`, pre-v1.8 applet rows too. |
462
+ | `ziprack:db:recover {slug} {version} [collection] [--replace] [--force]` | Bring an earlier version's stranded applet-scoped records back. |
463
+ | `ziprack:db:snapshot {slug}` · `ziprack:db:snapshots {slug}` | Take a snapshot; list them. |
464
+ | `ziprack:db:restore {slug} {snapshot} [--force]` | Replace the applet's data with a snapshot. |
465
+
466
+ The admin API routes — the record browser, the cutover plan and check, snapshots — are listed in
467
+ [Reporting](reporting.md#the-admin-api).
468
+
469
+ A Rack that lets its operators edit records uses `ZipRack\Db\RecordAdmin`: `create()` (into an
470
+ `applet` collection only, since every other scope belongs to a participant, room or attempt),
471
+ `update()`, `setField()` across many records, all or none, and `delete()`. Every write is checked
472
+ against the schema and quota exactly as an applet's write is, and fires the same `RecordChanged`
473
+ event, with a `null` participant, so a live room sees the edit.
@@ -7,6 +7,8 @@
7
7
  - [Message Handlers](#message-handlers)
8
8
  - [Launch Payload](#launch-payload)
9
9
  - [Install Hooks](#install-hooks)
10
+ - [Promotion Hooks](#promotion-hooks)
11
+ - [Rack Roles](#rack-roles)
10
12
  - [Conformance and Status](#conformance-and-status)
11
13
  - [The SDK Side](#the-sdk-side)
12
14
 
@@ -60,7 +62,8 @@ class SurveyCapability extends AbstractCapability
60
62
  | `validateManifest()` | Cross-field checks; add errors to the validator. |
61
63
  | `messageHandlers()` | `type => handler class`. |
62
64
  | `launchPayload()` | What to merge into `rack:launch` under this capability's id. |
63
- | `onInstall()` / `onPromote()` / `onRetire()` | Version lifecycle hooks. `onInstall()` runs inside the install transaction, so row changes only. |
65
+ | `onInstall()` / `onPromote()` / `onRetire()` | Version lifecycle hooks. `onInstall()` runs inside the install transaction, after extraction, so row changes only. `onPromote()` runs after the promotion commits. |
66
+ | `preparePromotion()` | Only with `ParticipatesInPromotion`: carry state over when the current version changes, or refuse. See [Promotion Hooks](#promotion-hooks). |
64
67
  | `adminRoutes()` | Headless admin routes, registered only when `ziprack/admin` is installed. |
65
68
  | `conformanceTests()` | Checks contributed to the conformance suite. |
66
69
  | `statusChecks()` | Warnings for `ziprack:status` and `/ziprack/health`. |
@@ -208,15 +211,78 @@ public function onInstall(AppletVersion $version, ?AppletVersion $previous): voi
208
211
  }
209
212
  ```
210
213
 
211
- `onInstall()` runs inside the install transaction, with the previous current version for comparison
212
- — which is what lets `db@1` register, update and orphan collections as they change between
213
- versions. If it throws, the whole installation rolls back and extracted files are removed.
214
+ `onInstall()` runs inside the install transaction, after the package is extracted, with the
215
+ previous current version for comparison. It can read the package's files from
216
+ `$version->storage_path` on `ziprack.disk` — which is how `db@1` checks seed files and migration
217
+ names. Keep it to checking and to rows the live version cannot see: the version is not current yet,
218
+ so anything that changes what running sessions read belongs in [promotion](#promotion-hooks). If it
219
+ throws, the whole installation rolls back and extracted files are removed; throw a
220
+ `PackageInstallException` to give the uploader a message.
214
221
 
215
222
  Keep it to row changes. `CREATE`, `ALTER` and `DROP` commit any open transaction on MySQL and
216
223
  MariaDB, which breaks the install and lets the conformance suite's data escape its rollback. If a
217
224
  capability needs tables, ship them as migrations; the conformance suite fails any check that commits
218
225
  its transaction.
219
226
 
227
+ <a name="promotion-hooks"></a>
228
+ ## Promotion Hooks
229
+
230
+ A capability that holds per-applet state which has to follow the current version — `db@1` and its
231
+ records — implements `ZipRack\Core\Contracts\ParticipatesInPromotion`:
232
+
233
+ ```php
234
+ use ZipRack\Core\Contracts\ParticipatesInPromotion;
235
+
236
+ class SurveyCapability extends AbstractCapability implements ParticipatesInPromotion
237
+ {
238
+ public function preparePromotion(AppletVersion $version, ?AppletVersion $outgoing, array $input): ?Closure
239
+ {
240
+ $plan = $this->planner->plan($version, $outgoing);
241
+
242
+ if ($plan->losesAnswers() && ! ($input['accept_loss'] ?? false)) {
243
+ throw new PromotionBlockedException('Version '.$version->version.' drops surveys that have answers.');
244
+ }
245
+
246
+ return fn () => $this->planner->apply($plan); // inside the promotion transaction
247
+ }
248
+ }
249
+ ```
250
+
251
+ `PromoteVersion::handle($version, $force, $input)` calls it on every capability the *target*
252
+ version declares, whenever the current version actually changes, forwards or back:
253
+
254
+ 1. `preparePromotion()` runs **before** the transaction opens, so slow or file-writing work — `db@1`
255
+ writes its snapshot here — stays out of it. Throw a `PromotionBlockedException` to refuse.
256
+ 2. The closures it returned run **inside** the promotion transaction, before `current_version_id`
257
+ moves. Row changes only, for the same DDL reason as `onInstall()`.
258
+ 3. After the commit, `onPromote()` runs.
259
+
260
+ `$input` is this capability's share of the promotion input, keyed by its id: the admin API's
261
+ promote body passes `db: {…}` to `db@1`. A refusal that carries something the caller should show
262
+ extends `PromotionBlockedException`; `db@1` throws `ZipRack\Core\Installer\DataCutoverRequired`,
263
+ with a `reason` and the plan as `details()`, which the admin API turns into a `409`.
264
+
265
+ <a name="rack-roles"></a>
266
+ ## Rack Roles
267
+
268
+ An applet's declared [roles](auth.md#roles) can be assigned to everyone holding one of the Rack's
269
+ own roles. zipRACK asks the Rack what those are through `ZipRack\Core\Contracts\RackRoles`:
270
+
271
+ ```php
272
+ interface RackRoles
273
+ {
274
+ /** @return array<string, string> key => label */
275
+ public function all(): array;
276
+
277
+ /** @return list<string> keys */
278
+ public function of(Authenticatable $user): array;
279
+ }
280
+ ```
281
+
282
+ The default, `GateRackRoles`, has one role, `administrator`, from the `ziprack.admin` gate. A Rack
283
+ with authors, facilitators or departments binds its own, the same way it binds
284
+ [`AppletAccess`](reporting.md#who-may-do-what).
285
+
220
286
  <a name="conformance-and-status"></a>
221
287
  ## Conformance and Status
222
288
 
@@ -84,6 +84,7 @@ ones you will actually touch.
84
84
  | `allowed_extensions` | static list | Anything else rejects the whole upload. |
85
85
  | `asset_token_ttl_hours` | `6` | Lifetime of the signed package-asset token. |
86
86
  | `session_ttl_hours` | `6` | Lifetime of a launch session. |
87
+ | `session_cookie` | `ziprack_launch` | HttpOnly cookie that binds launch sessions to the browser that launched them. |
87
88
  | `csp.img_hosts`, `csp.media_hosts` | `[]` | Per-deployment allowlist. A bare scheme is rejected. |
88
89
  | `player.frame_allow` | `[]` | Features added to the applet iframe's `allow` list, e.g. `microphone`. Bare names from a fixed set only. |
89
90
  | `media.providers` | `['vimeo','youtube']` | Drives `frame-src` and manifest validation. |
@@ -99,7 +100,7 @@ ones you will actually touch.
99
100
  |---|---|
100
101
  | `ziprack-auth.php` | `releasable` — the single place a deployment answers "what can applets see about our people"; `claim_map` |
101
102
  | `ziprack-attempt.php` | `max_state_bytes` (64 KB), `max_segment_keys` (20) |
102
- | `ziprack-db.php` | `max_records_per_scope` (5000), `max_record_bytes` (64 KB), `max_indexed_paths` (8), `list_limit` (100) |
103
+ | `ziprack-db.php` | `max_records_per_scope` (5000), `max_record_bytes` (64 KB), `max_indexed_paths` (8), `list_limit` (100), `snapshots.disk` (`ziprack.disk`; must be persistent), `snapshots.keep` (10) |
103
104
  | `ziprack-storage.php` | `disk`, `max_bytes_cap` (50 MB), `url_ttl_minutes` (10), `scanner` (`null` \| `clamav`) |
104
105
  | `ziprack-ai.php` | `provider`, `models.analysis`, `models.chat`, `allowed_models`, the whole `budget` block |
105
106
  | `ziprack-broadcast.php` | `join_prefix`, `facilitator_grace_seconds` (60), `presence_window_seconds` (45), `max_event_bytes` |
@@ -122,11 +123,14 @@ validation. Defaults apply when a manifest declares nothing.
122
123
  | `GET /ziprack/f/{file}` | Signed, short-lived download of a stored file (`storage`). |
123
124
  | `POST /ziprack/s/{session}/broadcast/auth` | Rack-side WebSocket channel authentication (`broadcast`). |
124
125
  | `GET /ziprack/health` | Fleet visibility: format, core version, capabilities, warnings. |
126
+ | `GET /ziprack/migration-runner` | The no-network page a Rack UI frames, sandboxed, to run [data migrations](database.md#data-migrations) at promotion. |
125
127
  | `GET /r/{code}` | Join a room by code (`broadcast`). |
126
128
  | `*` `/ziprack/admin/*` | The headless admin API (`ziprack/admin`). |
127
129
 
128
- Session routes are deliberately stateless: token in the path, no session cookie, no CSRF, rate
129
- limited by `ziprack.routes.session_rate_limit`.
130
+ Session routes run without the `web` group: no Laravel session, no CSRF, rate limited by
131
+ `ziprack.routes.session_rate_limit`. Each request needs the token in the path **and** the
132
+ `ziprack_launch` cookie the player set when it issued that token, so a token copied out of the page
133
+ or an access log is refused from any other browser.
130
134
 
131
135
  <a name="participants-and-guests"></a>
132
136
  ## Participants and Guests
@@ -172,8 +176,8 @@ the deployment owns. A Rack can add an approved image host; it cannot reintroduc
172
176
 
173
177
  | Command | Does |
174
178
  |---|---|
175
- | `ziprack:install {zip} [--applet=] [--activate]` | Install a package as a new version. |
176
- | `ziprack:promote {slug} {version} [--force]` | Make a version current. `--force` also approves a pending capability review. |
179
+ | `ziprack:install {zip} [--slug=] [--activate] [--force]` | Install a package as a new version; `--slug` installs into that existing applet. `--activate` promotes it, `--force` past a pending capability review. |
180
+ | `ziprack:promote {slug} {version} [--force] [--accept-data-loss] [--keep-invalid]` | Make a version current. `--force` also approves a pending capability review; `--accept-data-loss` confirms a [data cutover](publishing.md#the-data-cutover) that rejects or drops records, and `--keep-invalid` keeps rejected records instead of deleting them. |
177
181
  | `ziprack:retire {slug} {version}` | Kill switch: the version can no longer be launched. |
178
182
  | `ziprack:status [--json]` | Format, core version, installed capabilities, warnings. |
179
183
  | `ziprack:conformance [--json]` | Verify this Rack honours `ziprack/1`. |
@@ -182,5 +186,9 @@ the deployment owns. A Rack can add an approved image host; it cannot reintroduc
182
186
  | `ziprack:roster:import {csv}` | Import profile attributes by email. |
183
187
  | `ziprack:room:create {slug} [--name=] [--mode=] [--roster=]` | Create a room and print its join code. |
184
188
  | `ziprack:rooms:tick` | Vacate and reassign dropped facilitators. **Schedule every minute.** |
185
- | `ziprack:db:prune [--force]` | Drop tables of collections no current manifest declares. |
189
+ | `ziprack:db:prune [--stranded] [--force]` | Delete collections no current manifest declares, and their records. `--stranded` also deletes applet-scoped rows earlier versions left behind before v1.8. |
190
+ | `ziprack:db:recover {slug} {version} [collection] [--replace] [--force]` | Bring those stranded rows back into the live dataset. |
191
+ | `ziprack:db:snapshot {slug}` · `ziprack:db:snapshots {slug}` | Snapshot an applet's data; list its snapshots. |
192
+ | `ziprack:db:restore {slug} {snapshot} [--force]` | Replace an applet's data with a snapshot and make that version current again. |
193
+ | `ziprack:mcp:token {email} [--name=]` · `ziprack:mcp:revoke {email} [token] [--all]` | Mint and revoke [MCP](mcp.md) tokens. |
186
194
  | `ziprack:response-analysis [--applet=] [--force]` | Regenerate admin AI summaries. **Schedule every few hours.** |
@@ -40,7 +40,7 @@ The iframe is rendered with:
40
40
 
41
41
  ```html
42
42
  <iframe sandbox="allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox"
43
- allow="autoplay; fullscreen; picture-in-picture; encrypted-media">
43
+ allow="autoplay; fullscreen; picture-in-picture; encrypted-media; clipboard-write">
44
44
  ```
45
45
 
46
46
  Note the absence of `allow-same-origin`. The applet document has an **opaque origin**: no cookies,
@@ -72,11 +72,12 @@ ever cross the boundary.
72
72
  | **Applet** | One experience or app, identified by an immutable `slug`. Has many versions. | core |
73
73
  | **Version** | One uploaded zip. Immutable. Exactly one is current. | core |
74
74
  | **Participant** | The person in front of the applet, resolved from the Rack's own users (or a guest cookie). | core |
75
+ | **Role** | A name the participant holds in an applet: built in (`member`, `guest`, `applet-author`, `rack-administrator`) or declared by the manifest and assigned by the Rack. Sent at launch. | core |
75
76
  | **Attempt** | One participant's run. Holds `status`, `state`, `segment`. Pinned to the version it started on. | `attempt@1` |
76
77
  | **Statement** | Append-only record: actor did verb on activity with result. The audit trail and the uniform export. | `activity@1` |
77
- | **Record** | A mutable row in a declared collection, at a declared scope. Working state. | `db@1` |
78
+ | **Record** | A mutable row in a declared collection, at a declared scope. Working state that carries over to new versions. | `db@1` |
78
79
  | **Stored file** | A participant upload, addressable only once scanned clean. | `storage@1` |
79
- | **Room** | A shared session: an applet version plus its members, each `participant` or `facilitator`. | `broadcast@1` |
80
+ | **Room** | A shared session: an applet plus its members, each `participant` or `facilitator`. | `broadcast@1` |
80
81
  | **Profile attribute** | A Rack-owned fact about the participant, released only if requested and permitted. | `auth@1` |
81
82
 
82
83
  Statements and records coexist because they answer different questions. A statement is *what
@@ -103,8 +104,9 @@ any call the manifest did not declare.
103
104
  Capability versions are independent of the format string and of the Composer package version. See
104
105
  [Versioning](versioning.md).
105
106
 
106
- Two things need no capability: [declared media](media.md) (`Rack.media.embedUrl(id)`) and
107
- `Rack.eject()`, which asks the Rack to leave the applet.
107
+ Three things need no capability: [declared media](media.md) (`Rack.media.embedUrl(id)`),
108
+ [roles](auth.md#roles) (`Rack.auth.hasRole(name)`), and `Rack.eject()`, which asks the Rack to
109
+ leave the applet.
108
110
 
109
111
  <a name="what-ziprack-is-not"></a>
110
112
  ## What zipRACK Is Not
@@ -3,6 +3,7 @@
3
3
  - [Introduction](#introduction)
4
4
  - [Core Fields](#core-fields)
5
5
  - [The Requires Block](#the-requires-block)
6
+ - [Roles](#roles)
6
7
  - [Capability Sections](#capability-sections)
7
8
  - [A Complete Example](#a-complete-example)
8
9
  - [Validation Errors](#validation-errors)
@@ -42,6 +43,7 @@ upload meaningful: the manifest is a contract a reviewer can read.
42
43
  | `entry` | Optional, default `index.html`. Relative, no `..`, ends in `.html`. |
43
44
  | `requires` | Required, even if empty. See below. |
44
45
  | `media` | Optional. See [Media](media.md). |
46
+ | `roles` | Optional. The applet's own roles. See [Roles](#roles). |
45
47
 
46
48
  Everything else at the top level belongs to a capability. A section whose owning capability is not
47
49
  declared is a validation error, and so is a section no installed capability owns.
@@ -64,6 +66,28 @@ declared is a validation error, and so is a section no installed capability owns
64
66
  - Declaring `attempt@1` has a side effect: it pins every participant to the version they started on.
65
67
  See [Versioning](versioning.md#version-pinning).
66
68
 
69
+ <a name="roles"></a>
70
+ ## Roles
71
+
72
+ ```json
73
+ "roles": [
74
+ { "name": "dungeon-master", "label": "Dungeon Master", "description": "Runs the session." },
75
+ { "name": "spectator", "grant": "guests" }
76
+ ]
77
+ ```
78
+
79
+ A core key, so it needs no capability. Every launch tells the applet which roles the participant
80
+ holds; the Rack decides who holds them. See [Roles](auth.md#roles) for how they resolve.
81
+
82
+ | Field | Rules |
83
+ |---|---|
84
+ | `name` | Required. Lowercase words joined by hyphens, `^[a-z][a-z0-9]*(-[a-z0-9]+)*$`, max 40, unique. Not one of the built-in roles: `rack-administrator`, `applet-author`, `member`, `guest`. |
85
+ | `label` | Optional, max 60. Shown to whoever assigns the role. Defaults to `name`. |
86
+ | `description` | Optional, max 255. |
87
+ | `grant` | Optional. Who holds it before anyone is assigned: `everyone`, `members`, `guests` or `assigned` (the default — nobody). |
88
+
89
+ At most 20 roles.
90
+
67
91
  <a name="capability-sections"></a>
68
92
  ## Capability Sections
69
93
 
@@ -74,7 +98,8 @@ declared is a validation error, and so is a section no installed capability owns
74
98
  | `reporting.metrics[]` | `activity@1` (+ `attempt@1`) | `{key, label, pre, post}` — pairs two declared activities |
75
99
  | `collections[]` | `db@1` | `{name, scope, schema, indexed?, quota?, permissions?, seed?}` |
76
100
  | `uploads[]` | `storage@1` | `{name, scope, mime[], max_bytes?, max_files?}` |
77
- | `prompts[]` | `ai@1` | `{id, mode, template, vars?, output_schema?, budget?, model?}` |
101
+ | `prompts[]` | `ai@1` | `{id, mode, template, vars?, output_schema?, budget?, model?, knowledge?}` |
102
+ | `knowledge[]` | `ai@1` | `{id, source?, label?, fields?}` |
78
103
  | `broadcast` | `broadcast@1` | `{channels?, facilitator_events?}` |
79
104
 
80
105
  Each is specified in full on its capability's page. Two cross-cutting rules are worth stating here:
@@ -111,6 +136,10 @@ An applet exercising every capability at once. Most applets need three or four.
111
136
  { "id": "debrief.clip", "provider": "youtube", "key": "dQw4w9WgXcQ" }
112
137
  ],
113
138
 
139
+ "roles": [
140
+ { "name": "judge", "label": "Judge", "description": "Scores the final pitches." }
141
+ ],
142
+
114
143
  "segments": [
115
144
  {
116
145
  "key": "track",
@@ -205,6 +234,10 @@ one message. The checks that catch people out:
205
234
  | `A room-scoped collection requires the [broadcast] capability` | Scope implies capability. |
206
235
  | `Indexed path [votes] must be a string, integer, number or boolean property declared in the schema` | You can only index what you declared, and only scalars. |
207
236
  | `Only applet-scoped collections can be seeded` | A seed is shared reference data; see [Seeding](database.md#seeding). |
237
+ | `Role names are lowercase words joined by hyphens, like "dungeon-master".` | `roles[].name` has capitals, underscores or spaces. |
238
+ | `Role [guest] is one every Rack provides …; it cannot be declared.` | `rack-administrator`, `applet-author`, `member` and `guest` are built in. |
239
+ | `Duplicate role [judge].` | Role names are unique. |
240
+ | `A role's grant is one of everyone, members, guests or assigned.` | Any other `grant`. |
208
241
  | `Placeholder {{pitch}} is not declared in vars.properties` | Prompt templates and their `vars` schema must agree. |
209
242
  | `Analysis prompts need an output_schema of type object` | `mode: "analysis"` is structured output by definition. |
210
243
  | `The $room channel is reserved for the Rack` | Pick another channel name. |
package/dist/docs/mcp.md CHANGED
@@ -100,13 +100,16 @@ user is no longer an admin.
100
100
  | `rack-status` | mcp | Format, implemented capabilities, health notes. Needs `ViewRack` |
101
101
  | `list-applets`, `get-applet`, `get-applet-version` | mcp | Applets, versions, review state, manifest |
102
102
  | `create-upload` | mcp | A signed upload URL (see below) |
103
- | `promote-version` | mcp | Make a version current. It cannot approve, so it cannot force |
103
+ | `promote-version` | mcp | Make a version current. It cannot approve, so it cannot force. See below |
104
104
  | `get-scaffold-skill` | mcp | The applet scaffold skill's files |
105
105
  | `list-attempts`, `get-attempt`, `attempt-overview` | attempt | |
106
106
  | `list-statements`, `applet-metrics` | activity | |
107
107
  | `list-collections`, `list-records` | db | |
108
108
  | `list-files` | storage | |
109
109
  | `ai-spend` | ai | |
110
+ | `list-knowledge`, `search-knowledge` | ai | Knowledge slots, documents, label counts; try retrieval as a prompt would |
111
+ | `get-knowledge-prep-skill` | ai | The agent skill that prepares decks, PDFs and transcripts as tagged JSONL locally, validates them and loads them |
112
+ | `create-knowledge-upload`, `delete-knowledge-document` | ai | Load a document into a slot via a signed URL; delete one. Need `ziprack.ai.load-knowledge` |
110
113
  | `list-rooms`, `get-room` | broadcast | |
111
114
 
112
115
  Capability tools are only present when their capability package is installed.
@@ -118,6 +121,17 @@ Capability tools are only present when their capability package is installed.
118
121
  - **Audit log.** Writes (`mcp:upload`, `mcp:promote`) go to the audit log with the admin and the
119
122
  OAuth client or token name they came through.
120
123
 
124
+ `promote-version` takes `slug`, `version`, and for the [data cutover](publishing.md#the-data-cutover):
125
+
126
+ | Argument | |
127
+ |---|---|
128
+ | `accept_data_loss` | Promote even though the new version's collections reject or drop existing records. Pass it only after the user has seen what would be lost. |
129
+ | `keep_invalid` | With `accept_data_loss`, keep the records the new schema rejects instead of deleting them. |
130
+
131
+ A refused cutover comes back as a validation error carrying the message and the plan's notes, so
132
+ the agent can tell the user what would change. A version with data migrations is always refused:
133
+ they run in the browser, so it must be promoted from the applet's page in the Rack.
134
+
121
135
  <a name="uploading-a-package"></a>
122
136
  ## Uploading a Package
123
137
 
@@ -133,8 +147,9 @@ The rules in [Publishing](publishing.md) are unchanged.
133
147
 
134
148
  - **Default.** A later version installs as not current.
135
149
  - **With `activate: true`.** The version is promoted, unless it asks for capabilities that no admin
136
- has approved yet. In that case the response carries a `warning` and the version waits in the
137
- review queue.
150
+ has approved yet, or its data cutover needs a decision. In either case the response carries a
151
+ `warning` and the version stays installed but not current: approve it in the review queue, or
152
+ promote it with `promote-version` (or from the Rack, when it has migrations).
138
153
  - **Reuse.** A used or expired URL answers `410`. Call `create-upload` again.
139
154
 
140
155
  <a name="the-scaffold-skill"></a>