> ## Documentation Index
> Fetch the complete documentation index at: https://docs.alakazam.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# Edit the graph from natural language

> Run the kernel agent server-side: describe the change in prose and the agent authors it, then the SAME fail-closed kernel gate every deterministic write passes must bless the result before it persists. This is real LLM spend — one unit is reserved against your daily `edit` quota BEFORE the agent runs, and refunded on a validation reject, agent error, or concurrency conflict. RESULT-ONLY: the response is exactly `world`, `diagnostics`, and `reply` — the agent's system prompt, op grammar, lexicon, model id, and raw op trace are never exposed. If the agent needs clarification it returns the question in `reply` and leaves the world unchanged. Pass an `Idempotency-Key` so a retry doesn't double-charge.




## OpenAPI

````yaml /alakazam-v1.yaml post /v1/worlds/{id}/edit
openapi: 3.1.0
info:
  title: Alakazam — Programmable Worlds API
  version: '1.0'
  description: >
    **Alakazam is the programmable worlds API.** Generate playable, AI-rendered
    worlds from a prompt or an image, program their logic, and embed them in
    your own products and games.


    A world is *programmable*: a live graph of states and events you generate,
    then read, edit, fork, drive, and react to through the API. Two surfaces: a
    **Creation API** (generate, read, manage, fork worlds) and a **Runtime/Embed
    API** (mint short-lived session tokens your end-users' browsers use to boot
    an embedded world).


    **Authentication.** Two schemes. Management endpoints (`/v1/apps*`) use your
    Supabase user session. Data endpoints use an **API key** issued per app:
    `pk_…` (publishable, browser-safe, read + embed) and `sk_…` (secret,
    server-only, create worlds + mint sessions). Never ship a secret key to a
    browser.


    **Usage & quota.** Generations and session mints are metered per app and
    reserved before any GPU spend; exceeding the daily quota returns `402`.
servers:
  - url: https://api.alakazam.gg
    description: Production (placeholder — set to your conjure-service host)
security: []
tags:
  - name: Apps & Keys
    description: >-
      Create apps (tenants) and manage API keys. Authenticated with your user
      session.
  - name: Worlds
    description: >-
      Create, read, manage, and fork SMWorld games. Authenticated with an API
      key.
  - name: Graph editing
    description: >
      Read and program a world's graph — states (nodes), events (edges), and the
      entrance — with deterministic CRUD, a batch op vocabulary, a
      natural-language kernel-agent edit, and the kernel validate/lint gate.
      Every write is validated fail-closed before it persists. Authenticated
      with an API key.
  - name: Versions
    description: >
      Snapshot, branch, check out, and diff a world's graph. Versions form a
      branching tree of full snapshots; a HEAD pointer tracks the working graph.
      Authenticated with an API key.
  - name: Characters
    description: >
      Create, manage, and talk to characters — a SMWorld subtype that pairs a
      stance-graph with a "brain" (persona, lore, voice). CRUD is authenticated
      with an API key; the live talk turn (/say) and voice (/tts) are called
      from the browser with a short-lived session token.
  - name: Sessions
    description: Mint short-lived runtime tokens for embedding a world.
  - name: Usage
    description: Per-app usage and quota.
  - name: Webhooks
    description: >-
      Register HTTPS endpoints to receive signed server-side event
      notifications. Managed with your user session.
paths:
  /v1/worlds/{id}/edit:
    post:
      tags:
        - Graph editing
      summary: Edit the graph from natural language
      description: >
        Run the kernel agent server-side: describe the change in prose and the
        agent authors it, then the SAME fail-closed kernel gate every
        deterministic write passes must bless the result before it persists.
        This is real LLM spend — one unit is reserved against your daily `edit`
        quota BEFORE the agent runs, and refunded on a validation reject, agent
        error, or concurrency conflict. RESULT-ONLY: the response is exactly
        `world`, `diagnostics`, and `reply` — the agent's system prompt, op
        grammar, lexicon, model id, and raw op trace are never exposed. If the
        agent needs clarification it returns the question in `reply` and leaves
        the world unchanged. Pass an `Idempotency-Key` so a retry doesn't
        double-charge.
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
            format: uuid
        - name: If-Match
          in: header
          required: false
          schema:
            type: string
          description: The `rev` you last read. Mismatch returns 409.
        - name: Idempotency-Key
          in: header
          required: false
          schema:
            type: string
          description: >-
            A retried edit with the same key returns the original result without
            a second LLM spend.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EditRequest'
      responses:
        '200':
          description: >-
            Edit applied (or a clarifying question in `reply` with no change).
            The new `rev` is the `ETag`.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EditResult'
        '400':
          description: Missing instruction
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Authentication required
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '402':
          description: Daily edit quota exceeded
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          description: World not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '409':
          description: Concurrent modification — reload and retry (reservation refunded)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '422':
          description: >-
            The agent's world failed the kernel gate (not persisted; reservation
            refunded)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GraphValidationError'
        '502':
          description: Authoring agent failed (reservation refunded)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '503':
          description: Authoring or quota backend unavailable (fail-closed)
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      security:
        - ApiKeyAuth: []
components:
  schemas:
    EditRequest:
      type: object
      description: >
        A natural-language edit instruction for the server-side kernel agent.
        Either `instruction` or `message` is required.
      properties:
        instruction:
          type: string
          description: >-
            What to change, in prose — e.g. "add a locked vault behind the
            cellar and wire a key to open it".
        message:
          type: string
          description: Alias for `instruction` (accepted for convenience).
        verifiedObjects:
          type: array
          description: Optional grounding hints (named objects the agent may reference).
          items:
            type: object
            additionalProperties: true
        expectedRev:
          type: string
          description: >-
            Optional optimistic-concurrency token; alternative to the `If-Match`
            header.
    EditResult:
      type: object
      description: >
        The RESULT-ONLY response from a kernel-agent edit. It carries the
        validated `world`, advisory `diagnostics`, and the agent's
        natural-language `reply`, and NOTHING else — the agent's system prompt,
        op grammar, lexicon, model id, and raw op trace are never exposed. When
        the agent needs clarification it returns its question in `reply` and
        leaves the world unchanged.
      required:
        - world
        - diagnostics
        - reply
      properties:
        world:
          $ref: '#/components/schemas/SMWorld'
        diagnostics:
          type: array
          description: >-
            The full validateWorld set (advisory warning/info findings) for the
            persisted world.
          items:
            $ref: '#/components/schemas/KernelDiagnostic'
        reply:
          type: string
          description: >-
            The agent's natural-language summary of what it did, or a clarifying
            question when no change was made.
    Error:
      type: object
      properties:
        detail:
          type: string
          description: Human-readable error message.
        errors:
          type: array
          items:
            type: string
          description: Field-level validation errors (e.g. on 422 from POST /v1/worlds).
        schemaVersion:
          type: string
      required:
        - detail
    GraphValidationError:
      type: object
      description: >-
        A 422 from a graph write or validate — the world failed the kernel gate.
        Nothing was persisted.
      required:
        - detail
        - diagnostics
      properties:
        detail:
          type: string
        diagnostics:
          type: array
          items:
            $ref: '#/components/schemas/KernelDiagnostic'
        schemaVersion:
          type: string
          example: '1.0'
    SMWorld:
      type: object
      description: >
        A playable world. The full contract is published as JSON Schema at
        https://alakazam.gg/schema/smworld-1.0.json (see
        schema/COMPATIBILITY.md). Additional optional properties are allowed
        (forward-compatible).
      additionalProperties: true
      required:
        - id
        - name
        - entrance
        - scene
      properties:
        id:
          type: string
        name:
          type: string
        description:
          type: string
        cover:
          type: string
        entrance:
          type: object
          required:
            - image
            - state
          properties:
            image:
              type: object
              required:
                - src
              properties:
                label:
                  type: string
                src:
                  type: string
                  description: Seed image for the boot.
            state:
              type: string
              description: Initial state id.
        scene:
          type: object
          required:
            - states
            - events
          properties:
            states:
              type: object
              additionalProperties: true
            events:
              type: array
              items:
                type: object
                additionalProperties: true
    KernelDiagnostic:
      type: object
      description: >
        A single finding from the kernel grammar + doctrine lint suite.
        STRUCTURAL lints (e.g. `dangling-ref`, `transition-needs-to`,
        `kernel-cycle`, `slot-*`) are always fatal `error`s and block the write
        (422). Doctrine/budget lints (e.g. `negation`, `whiteout`,
        `lethal-override`, `budget`) are advisory and surface as
        `warning`/`info`.
      required:
        - lint
        - severity
        - path
        - message
      properties:
        lint:
          type: string
          description: The lint id (e.g. `dangling-ref`, `negation`, `budget`).
        severity:
          type: string
          enum:
            - error
            - warning
            - info
        path:
          type: string
          description: Human-readable location, e.g. `event[Step outside].movement.static`.
        message:
          type: string
          description: What is wrong and, where possible, how to fix it.
        loc:
          type: object
          description: Structured location for editor/narrator UIs.
          properties:
            field:
              type: string
            template:
              type: string
            slot:
              type: string
            fill:
              type: string
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      description: >-
        An app API key, e.g. `Authorization: Bearer sk_live_…`. Secret (`sk_`)
        for writes/sessions; publishable (`pk_`) for read/embed.

````