Skip to content

fly lexicon: flyApply — direct flaps REST applier (endpoint-overridable, owned-only prune, leases) #739

Closed

Description

@lex00

Epic: #736 · Phase: 1 (spine) · Depends on: #737, #738 · Blocks: #744 · Local target: #740

Reviewed 2026-07-11. The local target is live: flyApply is developed and
tested against mudflaps v0.3.0 (ghcr.io/intentius/mudflaps, :4280),
booted by the #740 wrapper. It exercises the tricky paths (lease-conflict
retry, wait-on-new-version) with real fidelity, so those are testable today.

Context

The meat of the epic: flyApply, a native applier that speaks flaps REST directly. Peer of
gcpApply (#706) — read lexicons/gcp/src/op/activities/gcp-apply.ts first; this mirrors its
shape (a direct-REST applier that does its own diff + owned-only prune). See #736 for the
decisions log.

Decisions that constrain this (from #736)

  • D1 — direct REST, no flyctl shell-out, no state file.
  • D2 — owned-only prune via the managed-by: chant machine metadata; app-scope for
    metadata-less types.
  • D3 — endpoint override via FLY_FLAPS_BASE_URL (default https://api.machines.dev,
    /v1 prefix). Same mechanism points the applier at real Fly or at mudflaps (:4280) with
    no code change.

What to build

  1. Consume chant's Fly resources (the generated types from fly lexicon: scaffold chant-lexicon-fly (chant init lexicon + OpenAPI generate) #737 + fly lexicon: App + Machine typed resources + serializer (machine-config / fly.toml) #738) and the lifecycle
    change-set (packages/core/src/lifecycle/change-set.ts — create/update/delete/adopt already
    classified) in dependency order (app → volumes → machines).
  2. Drive flaps per resource:
    • Apps: GET/POST /v1/apps, GET/DELETE /v1/apps/{app}.
    • Machines: POST /v1/apps/{app}/machines (create), POST .../{id} (update),
      DELETE .../{id} (destroy), GET .../{id} + GET /v1/apps/{app}/machines (list).
    • After each mutation, poll GET .../{id}/wait?state=started&version=<new instance_id>
      (fly-go sends version; client self-caps at 60s → re-poll) until settled.
  3. Owned-only prune (D2): list machines, filter by managed-by: chant; never touch an
    unmarked machine; destroy a declared-then-removed owned machine.
  4. Leases: acquire (POST .../{id}/lease) before mutating; send the nonce in the
    fly-machine-lease-nonce header on update/stop/destroy; release after; on a stale/lost nonce
    (409 with the holder's lease), re-acquire + retry.
  5. Endpoint override (D3): honor FLY_FLAPS_BASE_URL or an explicit endpoint option.

Where it lives

lexicons/fly/src/op/activities/fly-apply.ts (+ fly-apply.test.ts, index.ts) — mirror the
gcp op/activities/ files.

References

  • Pattern: lexicons/gcp/src/op/activities/gcp-apply.ts (diff, LRO wait, prune).
  • Behavioral oracle: superfly/fly-go flaps/flaps.go (base URL + FLY_FLAPS_BASE_URL override),
    flaps_machines.go (NonceHeader, update/lease semantics), flaps_machines_wait.go
    (version/repeatable state, 60s cap). Endpoints: openapi3.json.
  • Local target: mudflaps (fly lexicon: chant-side flaps wrapper (boot the mudflaps image for local tests) #740) — supports the full loop incl. the version filter and
    lease-conflict envelope, so every acceptance criterion below is reproducible offline.

The catch (be honest)

No server-side declarative apply — this loop is hand-rolled (the gcp model). The genuinely
tricky paths are lease-conflict retry and waiting on the correct version (new instance_id)
after an update; both are exercised by mudflaps.

Acceptance criteria

  • Applies App + Machine to a flaps endpoint: create → /wait started; re-apply is a no-op;
    image change → update to a new version; removing the owned machine prunes it.
  • Owned-only prune proven: an unmarked machine survives an apply that would otherwise delete it.
  • Endpoint override proven against mudflaps and (smoke) real Fly with a token.
  • Lease-conflict path exercised against mudflaps (stale nonce → 409 → re-acquire → succeed).

Out of scope

Volumes/IPs/certs/secrets applying (#741); the deploy Op + example (#744).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions