Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions lexicons/fly/src/op/activities/fly-apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@

import { readFileSync } from "node:fs";
import { safeHeartbeat, sleep } from "@intentius/chant/op";
import { hasOwnershipMarker } from "@intentius/chant/ownership";
import { FLY_METADATA_OWNERSHIP_KEYS } from "../../ownership";

/** Default flaps host when neither an `endpoint` arg nor `FLY_FLAPS_BASE_URL` is set. */
export const DEFAULT_FLAPS_BASE_URL = "https://api.machines.dev";

/** Header carrying a lease nonce on a mutating request, matching fly-go/flaps. */
export const LEASE_NONCE_HEADER = "fly-machine-lease-nonce";

/** Machine `config.metadata` key that marks chant ownership (D2). */
const OWNERSHIP_KEY = "managed-by";
const OWNERSHIP_VALUE = "chant";

/** One flaps REST call as emitted by the #738 serializer. */
export interface FlapsRequest {
endpoint: string;
Expand Down Expand Up @@ -152,9 +150,15 @@ export function appNameFromRequest(req: FlapsRequest): string {
return name;
}

/** True when a machine's live metadata carries chant's ownership marker (D2). Pure. */
/**
* True when a machine's live metadata carries chant's ownership marker (D2).
* Pure. Reads the key convention the fly lexicon declares via the #686 seam
* (`FLY_METADATA_OWNERSHIP_KEYS`) through core's `hasOwnershipMarker`, so the
* prune filter and the serializer's stamp can never drift apart — the marker
* key lives in exactly one place.
*/
export function isChantOwned(metadata: Record<string, string> | null | undefined): boolean {
return metadata?.[OWNERSHIP_KEY] === OWNERSHIP_VALUE;
return hasOwnershipMarker(metadata ?? undefined, FLY_METADATA_OWNERSHIP_KEYS);
}

/** Structural equality over two config values, order-insensitive. Pure. */
Expand Down Expand Up @@ -535,13 +539,29 @@ export async function pruneMachines(
return pruned;
}

// ── App-scoped, metadata-less resources (#741, D2) ───────────────────────────
// ── App-scoped, metadata-less resources (#741, #743, D2) ─────────────────────
//
// The fly ownership convention (#743) is asymmetric, and the asymmetry is
// deliberate:
//
// - Machines carry `config.metadata`, so they get the primary marker
// (`managed-by: chant`, FLY_METADATA_OWNERSHIP_KEYS). `pruneMachines`
// filters on it, so a foreign machine in the same app is never touched.
// - Volumes, IPs, certificates, and secrets carry no arbitrary metadata, so
// they have no marker channel. Their ownership boundary is the app itself
// (like a CloudFormation stack): everything under a chant-managed app is
// treated as chant's, and prune is app-scoped — anything live that the plan
// no longer declares is removed.
//
// The limitation: because these types have no marker, a resource created
// out-of-band inside a chant-managed app is indistinguishable from a chant one
// and CAN be pruned. That is the price of app-boundary ownership; the
// safeguard is that the app boundary is itself only ever chant-managed when the
// app carries the marker via its machines. Never widen app-scoped prune beyond
// a single chant-declared app.
//
// Volumes, IPs, certificates, and secrets carry no ownership marker, so they are
// owned at the app boundary (D2): everything under a chant-managed app is chant's
// and prune is app-scoped, not metadata-filtered like machines. Each resource
// creates if absent (idempotent by its natural key) and prunes anything live
// that the plan no longer declares. Secrets are apply-only (D7): always POSTed,
// Each resource creates if absent (idempotent by its natural key) and prunes
// what the plan no longer declares. Secrets are apply-only (D7): always POSTed,
// never read back for a diff.

/** A live volume as flaps lists it. */
Expand Down
29 changes: 22 additions & 7 deletions lexicons/fly/src/ownership.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
/**
* The fly lexicon's ownership marker convention. Fly machines carry ownership
* in their `config.metadata` map (a flat `Record<string, string>`), not in
* cloud tags or k8s labels. Decision D2 fixes the managed-by key as the bare
* `managed-by`, so chant's marker uses that key with the stack/env keys
* namespaced alongside it. Core owns the generic stamp/detect logic
* (@intentius/chant/ownership); this is the fly-specific key naming it stamps.
* The fly lexicon's ownership convention (#743, decision D2), declared here so
* core carries no fly-specific branch — the #686 seam: each lexicon owns its key
* names; core keeps only the generic stamp/detect logic
* (@intentius/chant/ownership), the same way aws declares `AWS_TAG_OWNERSHIP_KEYS`
* and azure `AZURE_TAG_OWNERSHIP_KEYS`.
*
* The convention is two-tier, because flaps resources differ in what they can
* carry:
*
* 1. Primary marker — machine `config.metadata`. Machines carry a flat
* `Record<string, string>`, so chant stamps `managed-by: chant` (+ stack/env)
* there. This is the k8s-label / AWS-tag analogue and the basis for
* owned-only machine prune (a metadata filter — a foreign machine survives).
* The serializer stamps it (see serializer.ts); the applier reads it via
* `isChantOwned` → core `hasOwnershipMarker(..., FLY_METADATA_OWNERSHIP_KEYS)`.
*
* 2. Secondary boundary — the app. Volumes, secrets, certs, and IPs carry no
* arbitrary metadata, so for them the Fly app is the ownership boundary
* (like a CloudFormation stack): chant owns the app's declared-type
* resources and prunes owned orphans within app scope. See the app-scoped
* prune block in op/activities/fly-apply.ts for the limitation this leaves.
*/

import type { ChannelKeys } from "@intentius/chant/ownership";

/** Machine `config.metadata` keys for chant's ownership markers (D2). */
/** Machine `config.metadata` keys for chant's ownership markers (tier 1, D2). */
export const FLY_METADATA_OWNERSHIP_KEYS: ChannelKeys = {
managedBy: "managed-by",
stack: "chant-stack",
Expand Down