Skip to content
Source
Sign inSign up
samwillis/neon-glow-demo
Open History to see the last commit

STATE_PROTOCOL.md

127 lines7.5 KB

Persistent gameplay state protocol

Status: proposed API/storage contract, not implemented or live-validated. It supports in-place app updates, older loaded clients, multiple devices, and idempotent gameplay writes without resetting progress or allocating branches.

Records, schemas, and capabilities

Every state record has an app/gameplay scope, record key, player identity where private, canonical schema version, monotonically increasing record revision, and value. Preview uses a distinct private test scope with the same protocol. Public state lives on the app branch; private player state lives behind App's player API. Each mutation targets one authoritative database, not a distributed transaction spanning public and private state.

An app version declares the state schema(s) it reads/writes and its supported mutation types. The trusted runtime registers validated schemas and compatibility adapters before admitting that version. The signed play capability binds app, scope, player, loaded app version, and its allowed state contract. A request cannot claim a different schema or player merely by supplying another field.

Use a fixed, bounded platform envelope around declared JSON state. The first implementation permits additive/defaulted schema changes and known platform operations, not arbitrary model-authored migration scripts running with database credentials. Reject undeclared fields, unsupported paths, oversized values, and unregistered schema versions before committing a mutation.

Read and mutation contract

A state read returns the view appropriate to the loaded app version plus an opaque record revision and canonical schema identity. The record revision is concurrency metadata; it is distinct from the app's source/version identity.

Mutations carry a stable client operation ID, expected record revision, loaded state-contract version, capability-bound mutation epoch, and a bounded intent. An epoch has a fixed server-side retry deadline; renewing a play capability does not extend that deadline or change an already queued operation's epoch. Supported intents include atomic score/counter changes and typed field patches. The SDK does not send a stale whole-state object as an unconditional replacement.

  1. Verify the capability and match its loaded app version to registered schemas and allowed operations. Resolve app/player/scope from trusted context.
  2. In one transaction, check for a previously committed operation receipt on this actor/record/epoch. The same operation/intent returns its original result; reusing its ID for a different intent is an idempotency conflict.
  3. Check the expected record revision. A mismatch returns state_conflict with a fresh authorized read and no mutation/receipt. Do not overwrite newer data.
  4. Adapt the intent into the canonical schema, apply it to the current record, validate the complete result, and increment the record revision.
  5. Commit the new value and operation receipt atomically in that record's database. A lost HTTP response can then be retried without applying the mutation twice.

The successful receipt binds the canonical intent and loaded schema; its lookup does not treat a refetched expected revision as a different intent. A rejected revision conflict writes no successful receipt, so the SDK can safely retry the same declared intent against the freshly read revision.

For a missing record, use an explicit create-if-absent precondition and unique scope/key identity. Concurrent first writes cannot initialize two records or replace another client's just-created state. Retain receipts through their epoch's fixed retry deadline plus transport grace. Reject all mutations in a retired epoch before looking up a receipt, so deleting old receipts cannot turn a replay into a new write. The SDK must not move an uncertain queued operation into a new epoch; it refreshes state and resolves the old outcome or waits for fresh user intent.

The SDK refetches on conflict. It may reapply a declared safe intent, such as a counter increment or field patch, with the same operation ID and a fresh revision if no receipt exists. It must not retry a stale serialized object or ambiguous read/modify/write calculation. Conflicts do not become additional AI generation requests; ordinary synchronization is a runtime/SDK responsibility. An idempotent receipt may describe an older record revision than another device's later write. It confirms the operation's outcome but must not replace a newer local snapshot; merge monotonically by record revision or refetch current state.

Old clients and in-place updates

Keep records in the newest canonical schema while supporting registered older read/write views. An old client sees its declared fields. Its mutation changes only fields understood by that contract; newer fields remain untouched.

Whole-object or container replacement across schema versions is rejected unless an explicit adapter preserves fields the old writer cannot understand. Prefer leaf-field patches and atomic intents. Array/container transformations that lack a safe adapter return update_required before any write.

Before promoting app version B over A, validate deterministic old-to-new defaults/transforms, both versions' supported reads/writes against the canonical state, and existing progress fixtures. Register compatibility support before live-head promotion. An additive field alone is insufficient proof if an old client can still replace the record. Do not destructively migrate away fields needed by an active version.

An unsupported loaded version receives update_required without mutating state. The player fetches the current app head and offers/loads the update at a safe point. Continue an older session only while its contract remains supported. Capability renewal respects that policy. Retire adapters only after supported sessions/capabilities and their retry windows have drained, or after explicitly ending those versions' writes. Never leave an old token authorized to overwrite state after its adapter is removed.

Undo changes source/live-head selection, not canonical state history. If app A is restored after B added fields, retain the canonical newer schema and its safe adapter for A. Undo must not delete B's retained data to make A appear compatible. An incompatible rollback stays unpromoted until a safe adapter exists.

Example and acceptance cases

Version A stores pet hunger. B adds energy with a default and changes animation. A phone still running A and a desktop running B can both update the same pet: B's energy write is preserved when A subsequently patches hunger. A stale record revision causes a refetch/rebase of that intent, not a full-state overwrite.

Required focused cases:

  • Old/new clients write concurrently: new fields and both valid intents survive.
  • A stale full-object/container replacement is rejected before mutation.
  • Duplicate delivery/lost responses return the same operation result once.
  • Concurrent creation of an absent record produces one initialized record.
  • The same operation ID with a different intent is rejected.
  • A mutation replay after receipt expiry is rejected by its retired epoch; token renewal cannot resurrect the operation or extend its receipt retention window.
  • Unsupported or expired loaded versions cannot write or silently reset progress.
  • Preview, another player, and a fork cannot select the original's private scope.
  • Undo from B to A retains state and uses a compatible adapter; no branch is added.

These cases supplement app-code compatibility checks. They are enforced by the runtime/storage protocol even when a generated client behaves incorrectly.