TypeScript SDK

The @floodwall/client package: options, guard, submit, wait, report, errors, signing, failing closed or open.

@floodwall/client asks a Floodwall Cloud plane before your agent changes production, and tells it how the change went. It has no dependencies and runs on Node 20+, Deno, Bun, Workers and current browsers. The source is in sdk/typescript.

sh
npm install @floodwall/client
js
import { Floodwall, NotAdmitted } from "@floodwall/client";

const fw = new Floodwall({
  apiKey: process.env.FLOODWALL_KEY, // an agent key from the console
  plane: process.env.FLOODWALL_PLANE,
  agent: "deployer",
});

// Runs the callback only if the plane admits the change, then reports
// "succeeded", or "failed" with the error if the callback throws.
await fw.guard(
  { action: { kind: "scale", resource: "web", replicas: 5 }, priority: "normal", blastRadius: "service" },
  () => kubectl("scale deploy/web --replicas=5"),
);

Options#

new Floodwall(options) is a connection to one plane, as one agent.

OptionTypeDefaultWhat it does
apiKeystringrequiredAn agent (or operator) key, fw_live_… or fw_test_…. Not enumerable, so logging the client doesn't print it.
planestringrequiredThe plane's id.
agentstringrequiredThe agent's name, as the plane's rules and ledger know it. An agent-bound key only works with its own name.
baseUrlstringhttps://api.floodwall.aiMust be https://, except for localhost, 127.0.0.1 and [::1].
whenUnreachable"closed" | "open""closed"What guard does when the plane can't be reached: refuse the change, or apply it unchecked.
onFailOpen(intent, error) => voidconsole.warnTold about each change applied unchecked. The default logs agent, kind, resource and error, never the manifest.
signingKeyCryptoKey | Uint8ArraynoneThe agent's Ed25519 private key, or its 32-byte seed, for planes whose keyring requires signed intents.
onReportError(error, key) => voidconsole.warnTold when reporting a successful change failed; guard still returns the result.
fetchtypeof fetchglobalThis.fetchFor tests, or a fetch with its own proxy or timeout.
sleep(ms) => Promise<void>setTimeoutFor tests: how wait pauses between polls.

Changes#

Every method that takes a change takes this shape (or an intent already in the plane's form):

ts
type Change = {
  action:
    | { kind: "apply"; resource: string; manifest: string }
    | { kind: "scale"; resource: string; replicas: number }
    | { kind: "destroy"; resource: string };
  priority?: "bulk" | "normal" | "urgent" | "pager";   // default "normal"
  blastRadius?: "cell" | "service" | "region" | "global"; // default "service"
  id?: number | string;                                  // default: a new id above every id this client used
};

An invalid change (an unknown kind, priority or blast radius, replicas that aren't a whole number from 0 to 4,294,967,295) throws a TypeError before anything is sent. See Intents for what each field means.

Methods#

guard(change, apply, options?)#

guard<T>(change, apply: () => T | Promise<T>, { timeoutSeconds = 300 } = {}): Promise<T>

Submits the change; waits while it is queued or held for a person (up to timeoutSeconds); runs apply only if it is admitted; then reports succeeded, or failed with the error's message if apply throws, and rethrows it. Throws NotAdmitted if the change is rejected, expires or lapses, or still waits when time runs out. Returns what apply returns.

submit(change)#

Offers a change and resolves to the plane's first answer, a Decision: admitted (status: "admit"), held, rejected, or queued.

wait(key, options?)#

wait(key, { timeoutSeconds = 300 } = {}): Promise<Decision>. Long-polls until the intent leaves the queue and the hold queue (admitted, rejected, expired or lapsed) or time runs out, and resolves to its latest status either way. A timeout leaves the intent held on the plane. Polls are at least a second apart, whatever the server does.

report(key, outcome, reason?)#

Reports how an admitted change went: "succeeded" or "failed", with an optional reason (whitespace collapsed, at most 1,024 characters). This frees the resource for the next change. Reporting an intent that isn't in flight is a FloodwallError with status 409.

prepare(change), intent(change), nextId()#

intent turns a change into the plane's form; prepare also validates and signs it; nextId returns an id above every id this client has used: the time in microseconds, or one more than the last, so a restarted agent needs no saved state.

submit, wait and report are the steps guard takes. Use them when the change is applied somewhere else, such as a CI job that asks in one step and deploys in another:

js
const first = await fw.submit({ action: { kind: "apply", resource: "api", manifest: "v42" } });
const decision = first.status === "queued" || first.status === "held" ? await fw.wait(first.key) : first;
if (decision.status !== "admit" && decision.status !== "in_flight") throw new NotAdmitted(decision);
try {
  await deploy();
  await fw.report(first.key, "succeeded");
} catch (error) {
  await fw.report(first.key, "failed", error.message);
  throw error;
}

Decisions#

ts
interface Decision {
  key: { agent: string; id: string };
  // "admit" or "in_flight" once admitted; "queued" and "held" while waiting;
  // else "rejected", "expired", "lapsed" or "done".
  status: string;
  verdict: "admit" | "defer" | "reject" | null;
  reason: string | null;
  policies: { policy: string; verdict: string; reason: string | null }[];
}

Errors#

ErrorWhenWhat to do
NotAdmittedThe change was rejected, expired or lapsed, or still waited when time ran out. error.decision says which.Don't apply the change. Log the reason.
FloodwallErrorThe plane answered with an error: error.status (401, 402, 403, 404, 409…) and error.body.Fix the key, plane or request; don't retry blindly.
TypeErrorAn invalid change or option, or a network failure from fetch.Fix the call; a network failure is what whenUnreachable decides on.

If apply throws and reporting the failure fails too, your error is still the one thrown, with the report's error on its reportError property. If apply succeeds but the report fails, guard returns the result and calls onReportError(error, key).

Failing closed or open#

By default (whenUnreachable: "closed") a change the plane can't be asked about is refused: guard throws. With "open", guard applies it unchecked and calls onFailOpen(intent, error). Only these count as unreachable: fetch's network TypeError, aborts and timeouts, and 5xx answers. Invalid changes, signing errors, 4xx answers and rejections always throw.

js
const fw = new Floodwall({
  apiKey, plane, agent: "autoscaler",
  whenUnreachable: "open", // scaling up during an outage beats waiting for the plane
  onFailOpen: (intent, error) => metrics.increment("floodwall.fail_open", { resource: intent.action.resource }),
});

Retries and rate limits#

A 429 (the agent's rate limit, or a full queue) is waited out as its Retry-After says, a minute at most, up to five times. Redirects are an error, so the key never follows a response elsewhere. Submitting the same intent again (same agent, id and content) answers with its current status, so a retry after a lost answer is safe: pass the same id.

Signing#

For a plane with a keyring, give the client the agent's Ed25519 key and every intent is signed. The keyring holds the matching public key, in hex (see the console guide).

js
// A 32-byte seed kept in your secret store, or a CryptoKey.
const seed = Uint8Array.from(Buffer.from(process.env.AGENT_SEED_HEX, "hex"));
const fw = new Floodwall({ apiKey, plane, agent: "deployer", signingKey: seed });

The package also exports intentBytes(intent), intentDigest(intent) and sign(intent, key), the encoding the plane verifies. The signed agent example generates a key and prints the public key for the keyring.

Examples#

The examples include a deploy agent, a Kubernetes scaler, a CI gate, an LLM agent's tool calls and a signed agent, all in TypeScript.