iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / core / lib / src / lib.tsmain at aacbc74

// lib.ts — the pure helpers every layer shares, one platform-neutral file (it rides the SDK bundle
// and the Node unit tests, so no cloudflare:workers here):
//   errors  — `codedError` / `errorCode` / `reportIssue` / `forwardIssues`: THE machine-readable
//             error channel
//   release — `releaseRpcSessions`: dispose the Workers-RPC values a round trip reached
//   patch   — `diff` / `applyPatch` / `jsonEqual`: the live-state delta (an RFC 6902 subset)
//   timeout — `withTimeout`: a promise raced against a deadline (code TIMEOUT)
//   bytes   — `bytesToBase64`: bytes to base64 without overflowing the call stack
//   origin  — `isSameOriginBrowserRequest`: may a request spend the cookies it carries;
//             `cookieValueOf`: one cookie out of a `Cookie` header;
//             `sameOriginPath`, `isLocalOrigin`, `resolveContextPath`: origins and paths
//   environment — `deploymentEnvironment`, `environmentTitle`, `environmentFaviconHref`: which
//             deployment (a PR's preview, local dev, production) a page is on, in its browser tab

// ── errors ── THE machine-readable error channel, after cloudflare-os
// (workshop-shared/src/api.ts: plain Error + a `code` own-property via Object.assign, read with
// `"code" in error`). Why this shape and no other: capnweb coerces custom error NAMES to a
// builtin whitelist and drops subclass identity, but preserves ALL own enumerable properties
// across the wire (verified at runtime against @iterate-com/capnweb 0.10.0) — and with
// enhanced_error_serialization (our compat date) own props usually survive native Workers-RPC hops
// too (contexts call each other with `invokeSafe`: core/itx/src/iterate-context-durable-object.ts says why).
// So: never classify by `name`, `instanceof`, or message regex across a hop; check the code.
// Human messages stay verbatim and greppable — the code rides beside them, never instead.
// workerd's own stamped flags (`.retryable`, `.overloaded`, `.durableObjectReset`) ride the same
// own-property channel. What a failure is, and whether it is asked again, is one model
// (docs/engineering-invariants.md#failures-and-retries): a code is an expected outcome, never
// repeated; the platform's own failure crosses a hop as UNAVAILABLE; and "never retry this" is a
// code (PERMANENT_FAILURE, or the refusal's own), never an invented `retryable: false`.

/** The stable machine-readable codes — SCREAMING_SNAKE, defined once, both ends import this. */
type ErrorCode =
  | "SCHEDULE_LIMIT" // bounded pending/failed scheduled batches per context
  | "WATCH_LIMIT" // itx.contexts.watch: the session holds its 64 watches, or the mind's catalog its 512 different live queries (core/itx/src/context/reactive-catalog.ts `CATALOG_LIMITS`): dispose a watch first
  | "PERSON_CALL_LIMIT" // one person's calls of person methods on one context, over the count for a minute (core/itx/src/context/facet-public-methods.ts): ask again in the next minute
  | "INVALID_INPUT"
  | "IDENTITY_CONFLICT" // verified login cannot replace another linked identity
  | "GRANT_NOT_FOUND" // a caller may revoke only a grant in their own inventory
  | "SECRET_NOT_SET" // itx.secrets.delete of a path that holds no secret: already gone, or never set; or a set that a delete came in during, whose value is not kept
  | "NOT_CREATED" // a verb or delete on an agent, repo, workspace or sandbox whose path has no creation certificate (never created, or its create is still running or failed): create it first; `data` is { path }; never retried
  | "NO_ITX_EXPRESSION_MATCH" // no rewrite rule matches the call (default-deny)
  | "IDEMPOTENCY_CONFLICT"
  | "OFFSET_CONFLICT" // an input's expected `offset` is not the offset it would land at
  | "EVENT_TOO_LARGE" // one event's serialized body is over the append ceiling (stream.ts EVENT_BODY_MAX_CHARS)
  | "REDUCE_CHECKPOINT_TOO_LARGE" // a reduce's state would not fit one storage cell (stream/processor.ts)
  | "EVENT_UNREADABLE" // a stored row's body is not JSON — `data.offset` names it (stream.ts read)
  | "STREAM_PAUSED"
  | "INVALID_CONTEXT" // a context name / mind id the codec refuses (core/itx/src/context/paths.ts `DurableObjectNameCodec`) — coded, so it survives the hop
  | "EXPRESSION_TOO_LONG" // a STRING itx expression over ITX_EXPRESSION_STRING_MAX_CHARS — pass the parsed form
  | "FACET_SOURCE_TOO_LARGE" // a facet's literal source, or a rule's target, over 1 MiB (worker-loader.ts FACET_SOURCE_MAX_CHARS) — refused on entry
  | "INVALID_CREDENTIALS" // authenticate(): the admin secret did not match, or the credentials named no known kind
  | "UNAUTHENTICATED" // authenticate({ type: "from-server-cookie" }): no session cookie on the request, or a cross-origin browser's request
  | "FORBIDDEN" // minds.get(mind): outside the session's reach (a grant narrowed to other minds, or the user is no member of its org); create on a narrowed grant; grants/consent on a session that carries none; organizations.get outside the session's memberships; `cd` in the global namespace (a global context is reached by identity, never by path); a first-party facet off the context the platform hosts it on, or loaded code in the global namespace (core/itx/src/first-party-facets.ts `assertFacetPlacement`)
  | "MIND_NAME_TAKEN" // minds.create({ mind }): a mind of that slug exists in another org; minds.rename(mind, { slug }): another mind holds the slug
  | "RPC_STUB_OFFLINE" // the rpc stub a row names is neither borrowed nor pager-backed right now — or its lend ended mid-call (recalled, returned, broken; the relay re-codes)
  | "NOT_A_METHOD" // the dotted path's terminal segment is not callable on the target
  | "NO_SERVICE" // itx.services.<name>: the stack's config binds no service of that name (core/alchemy config `services`); `provideEgressGateway` on a stack that names no egress gateway
  | "NO_SANDBOXES_YET" // a sandbox's start, command, file or container call on a stack with no container (core/alchemy config `mind.containers: false`, for an account without Workers Paid): never retried
  | "NO_FACET" // no facet of that name has been loaded into this context
  | "FACET_ABORTED" // the facet instance this call ran on was reset by `itx.facets.abort` (core/itx/src/context/facet-host.ts) — its next call starts it fresh
  | "FACET_RESTARTED" // the facet instance this call ran on was restarted by the platform under it — its source or loader identity changed, or another call on it timed out (core/itx/src/context/facet-host.ts) — its next call runs on the new instance
  | "FACET_NO_UPGRADE" // a WebSocket upgrade aimed at a facet: a facet answers RPC and plain HTTP, never a socket — sockets terminate at the edge (core/itx/src/context/facet-host.ts)
  | "WAIT_TIMEOUT" // waitForEvent expired with no matching event committed
  | "NOT_FAST_FORWARD" // a repo's pull or push without `force` where neither main contains the other (core/mind-worker/src/repo/durable-object.ts) — `data` is { ours, theirs }
  | "TIMEOUT" // lib.ts withTimeout: the call did not answer within its deadline
  | "GONE" // a target that says it is gone for good (an HTTP webhook's 410): the delivery halts its row, which an operator's resume reopens
  | "CALL_SESSION_ENDED" // a call's signalling step (core/configs/default/call/rules.ts): the SFU answered 410 for the page's own session, which is gone for good; the page makes its connection and its session anew, by its bounded repair (the kit's call engine)
  | "LOOP_LIMIT" // code reacting to code too many hand-offs deep (core/itx/src/cause.ts): the act is refused for good, never retried
  | "UNAVAILABLE" // the platform failed the call, not the caller: `data` is { kind, retryAfterMs } — a deploy's reset ("deploy-reset"), a lost connection ("disconnected") or an overload ("overloaded"); an idempotent call may be asked again after retryAfterMs, and an HTTP edge answers it 503 with that Retry-After
  | "NOT_FORWARDABLE" // a received mail's forward (core/boundary-worker/src/mail.ts): Cloudflare forwards no mail whose sender it did not prove
  | "FORWARDED_BEFORE" // a received mail's forward: this mail went to that address before, or its X-Forwarded-To names it
  | "REFUSED" // a received mail's forward: Cloudflare refused it for another reason, its words in the message (an address that is no verified destination)
  | "EXPIRED" // a received mail's forward after its delivery ended: the original is no longer there to forward
  | "PERMANENT_FAILURE"; // a failure no repeat can change (a subscriber's poison event): a delivery halts on it at once instead of climbing its retry ladder (core/itx/src/stream/subscription-delivery.ts); a processor's work in flight that died with its host five times, whose revive is refused and recorded as `itx/work-failed` (stream/processor.ts)
// (There is no separate boundary-validation library: the append method's own runtime guards
// throw plain Errors; a client is JUST capnweb, so malformed args surface as ordinary errors.)

/** A plain Error carrying `code` (+ optional `data`) as own enumerable properties. */
export function codedError(code: ErrorCode, message: string, data?: unknown): Error {
  return Object.assign(new Error(message), data === undefined ? { code } : { code, data });
}

/** The code of an error that crossed any number of hops — undefined for uncoded errors. */
export function errorCode(error: unknown): ErrorCode | undefined {
  return typeof error === "object" && error && "code" in error
    ? ((error as { code: unknown }).code as ErrorCode)
    : undefined;
}

/** THE HOP COUNT OF MAIL BETWEEN MINDS. `itx.email.send` writes it on every mail beside
 *  `Auto-Submitted`, and the boundary reads it on a received mail whose sender is proven: so a
 *  mind's agent can answer another mind's agent, and the two stop at a limit (the mind's config, iterate/email `nextMailHops`). */
export const MAIL_HOPS_HEADER = "X-Iterate-Mail-Hops";

/** The header's value as a hop count: a positive whole number in digits, else null. */
export function mailHopsOf(value: string | undefined): number | null {
  const digits = value?.trim() ?? "";
  return /^[1-9]\d{0,8}$/.test(digits) ? Number(digits) : null;
}

/** OUR MARK (core/itx/src/cause.ts): what the platform sends — a request, a mail — carries the cause of
 *  the code that sent it here, as JSON; only the mind's own request to the host that its fetch
 *  named resumes its chain with it (`causeOfOwnRequest` says which redirect is such a fetch). */
export const ITERATE_CAUSE_HEADER = "X-Iterate-Cause";
/** THE MIND'S OWN REQUEST, as the boundary says it to the mind Worker: a request that the mind's
 *  code sent to a host of its own zones, which never left the stack. Only the boundary writes it,
 *  after it strips every `x-itx-*` header and every mark; with it, our mark for the host that the
 *  code's fetch named resumes its chain. */
export const ITX_OWN_HEADER = "x-itx-own";
/** OUR MARK AS IT LEAVES THE STACK, in a request to a host that is not the stack's own, or in a
 *  mail: the counts and when the chain began. No stack reads it at its edge today (a request from
 *  outside begins a chain): it leaves for the day one does. What began the chain and the event it
 *  runs for name the stack's own contexts (`<path>@<offset>`), and stay behind, as does the host
 *  that egress names for the mind's own request (`to`). Null for no mark, and for one that the
 *  kernel did not write. */
export function causeLeavingTheStack(mark: string | null | undefined): string | null {
  if (!mark) return null;
  let cause: unknown;
  try {
    cause = JSON.parse(mark);
  } catch {
    return null;
  }
  if (typeof cause !== "object" || !cause) return null;
  const { chain, depth, at, hops } = cause as Record<string, unknown>;
  const count = (value: unknown) =>
    typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
  if (count(depth) === undefined) return null;
  // the kernel's chains are `<time> with <what began it> ~<nonce>` (core/itx/src/cause.ts `newChain`)
  const [, began, nonce] =
    (typeof chain === "string" && /^(\S+) with .* (~\S+)$/s.exec(chain)) || [];
  return JSON.stringify({
    chain: began ? `${began} with another stack ${nonce}` : "another stack",
    depth,
    at: count(at),
    hops: count(hops),
  });
}

/** `request` as it leaves the stack: with our mark as `causeLeavingTheStack` gives it. */
export function leavingTheStack(request: Request): Request {
  const mark = request.headers.get(ITERATE_CAUSE_HEADER);
  if (!mark) return request;
  const headers = new Headers(request.headers);
  const outside = causeLeavingTheStack(mark);
  if (outside) headers.set(ITERATE_CAUSE_HEADER, outside);
  else headers.delete(ITERATE_CAUSE_HEADER);
  return new Request(request, { headers });
}

/** A STACK'S EGRESS GATEWAY: a Worker of the owner's that gets each request as it leaves the
 *  stack, WITH ITS SECRET IN IT. The stack's deploy binds it to the vault and the boundary as
 *  `EGRESS_GATEWAY` (core/alchemy/src/config.ts `egressGatewayWorker`, which says what goes
 *  through it and what does not), so it is trusted as the vault is: only a deploy binds one, and
 *  nothing in a mind can.
 *
 *  What a gateway must do: send exactly the request that it gets, over `https:` alone, and follow
 *  no redirect. Fail closed: a request that it did not carry is a 502 before the answer's head,
 *  and an error in the body's stream after it; it never sends another way and never sends twice.
 *  Answer as the platform's fetch does: the head first, a streamed body, a WebSocket upgrade as a
 *  101 with its socket. */
export type EgressGateway = { fetch(request: Request, init?: RequestInit): Promise<Response> };

/** What the last hop reads of a Worker's variables: the gateway's service binding, which the
 *  vault and the boundary of a stack have when its deploy names a gateway, and not otherwise. */
export type EgressGatewayEnv = { EGRESS_GATEWAY?: EgressGateway };

/** THE LAST HOP of a request that leaves the stack, after every check: the stack's egress gateway
 *  and nothing else, or for a stack with no gateway the platform's fetch with the caller's
 *  `init`. A gateway that throws, or that answers its own error, is the request's failure:
 *  nothing sends the request another way. A gateway follows no redirect. */
export function fetchLeavingTheStack(
  env: EgressGatewayEnv,
  request: Request,
  init?: RequestInit,
): Promise<Response> {
  const leaving = leavingTheStack(request);
  return env.EGRESS_GATEWAY
    ? env.EGRESS_GATEWAY.fetch(leaving, { redirect: "manual" })
    : fetch(leaving, init);
}

/** What marks a 508 as an act refused past the loop limit (core/itx/src/unavailable.ts). */
export const LOOP_LIMIT_HEADER = "iterate-loop-limit";

/** The loop guard counts speed, not age (core/itx/src/cause.ts): a chain loses a step every this long. */
export const STEP_FORGIVEN_EVERY_MS = 2 * 60_000;

/** THE ONE +1: `cause` one hand-off deeper, less a step for every STEP_FORGIVEN_EVERY_MS since its
 *  `at`, whose remainder it keeps. Work that waits keeps pace with the clock and never goes deep. */
export function oneStepDeeper<Cause extends { depth: number; at?: number }>(cause: Cause) {
  const now = Date.now();
  const since = cause.at ?? now;
  // never below 0: a cause minted on another machine can carry a clock a moment ahead of this one
  const forgiven = Math.max(0, Math.floor((now - since) / STEP_FORGIVEN_EVERY_MS));
  const depth = Math.max(0, cause.depth - forgiven);
  return {
    ...cause,
    depth: depth + 1,
    at: depth === 0 ? now : since + forgiven * STEP_FORGIVEN_EVERY_MS,
  };
}

/** An answer refused past the loop limit — a 508 marked so — as the LOOP_LIMIT refusal it is,
 *  already recorded where it was met; none for any other answer. */
export async function loopLimitOf(answer: Response): Promise<Error | undefined> {
  if (answer.status !== 508 || !answer.headers.has(LOOP_LIMIT_HEADER)) return undefined;
  return codedError("LOOP_LIMIT", (await answer.text()).trim(), { recorded: true });
}

// reportIssue — the ONE exit for unexpected failures (cloudflare-os error-reporting.ts, minus
// its private Reporter Worker): one bounded console.error line; query event="issue" in Workers
// Logs. A host forwards issues elsewhere too by `forwardIssues(forward)` (to an error tracker,
// say) — capture sites never change. Deliberately NO cloudflare:workers
// import: this file rides the platform-neutral SDK bundle. Reporting must never disturb the
// caller — armored end to end; worst case it prints nothing.

// Bounds verbatim from cloudflare-os: hostile strings get clipped, never explode a log line.
const MAX = { message: 1024, stack: 16_384, string: 256, attributeKeys: 32 } as const;
type Scalar = string | number | boolean | null; // attribute values stay queryable scalars

/** One reported issue as `reportIssue` prints it, and the value that was caught. */
export type Issue = {
  failureSite: string;
  caught: unknown;
  attributes: Record<string, Scalar>;
};
let forwardIssue: ((issue: Issue) => void) | undefined;

/** Also hand every issue to `forward` (one per isolate; the last call wins). It runs inside
 *  `reportIssue`'s armor: a throw is swallowed. */
export function forwardIssues(forward: (issue: Issue) => void): void {
  forwardIssue = forward;
}

/** Print ONE bounded console.error line for an unexpected failure; never throws. */
export function reportIssue(
  failureSite: string,
  caught: unknown,
  attributes?: Record<string, Scalar | undefined>,
): void {
  try {
    const bounded: Record<string, Scalar> = {};
    for (const [key, value] of Object.entries(attributes || {}).slice(0, MAX.attributeKeys)) {
      if (value === undefined) continue;
      bounded[key.slice(0, MAX.string)] =
        typeof value === "string" ? value.slice(0, MAX.string) : value;
    }
    const code = errorCode(caught);
    // The thrown value, bounded — name/message/stack read off an Error; an arbitrary object is
    // never walked.
    const error =
      caught instanceof Error
        ? {
            type: (caught.name || "Error").slice(0, MAX.string),
            message: caught.message.slice(0, MAX.message),
            ...(caught.stack && { stack: caught.stack.slice(0, MAX.stack) }),
          }
        : typeof caught === "object" && caught
          ? { type: "ObjectThrown" }
          : { type: `${typeof caught}Thrown`, message: String(caught).slice(0, MAX.message) };
    console.error({
      ...bounded, // fixed keys spread last so an attribute can never shadow them
      event: "issue",
      failureSite: failureSite.slice(0, MAX.string),
      code,
      error,
    });
    forwardIssue?.({ failureSite, caught, attributes: bounded });
  } catch {
    // Reporting must never disturb the caller — swallow and move on.
  }
}

// ── release ── a Workers-RPC value (a stub, a call's promise) keeps its session, and the actor at
// its far end, open until disposed.

/** Release each of `rpcSessions`, the last first. The answer they served is already in, so a release
 *  that throws is reported, never made the call's failure. */
export function releaseRpcSessions(rpcSessions: readonly unknown[]): void {
  for (const rpcSession of [...rpcSessions].reverse())
    try {
      // A session-brand value or an RPC result object carries a disposer; a void call's undefined
      // answer, or a plain value, has nothing to release.
      (rpcSession as Partial<Disposable> | undefined)?.[Symbol.dispose]?.();
    } catch (error) {
      reportIssue("itx-expression.release-rpc-session", error);
    }
}

/** AN ANSWER FROM ANOTHER WORKER, COPIED AND RELEASED. An answer that crosses Workers RPC holds its
 *  call's session open until its disposer runs, and behind that session every session the callee
 *  opened to answer. Left alone, each stays open for as long as the code that holds the answer
 *  lives: until the Worker is replaced, or for a socket's whole life. Enough of them fill the
 *  callee's memory, and a Worker over its memory limit ends every call that waits in it. For
 *  plain data the caller keeps a copy and lets the session go.
 *
 *  A CALL THAT IS REFUSED HOLDS ITS SESSION TOO, and there is no answer to release: the call's own
 *  promise carries the disposer then. So `call` is the call as it was made, not a promise made
 *  from it: a wrapper (`Promise.resolve(call)`, an `async` function's return) has no disposer. */
export async function releasedAnswer<T>(call: PromiseLike<T>): Promise<T> {
  let held: T;
  try {
    held = await call;
  } catch (error) {
    // workerd puts the disposer on the promise of a call across Workers; a plain promise has none
    (call as Partial<Disposable>)[Symbol.dispose]?.();
    throw error;
  }
  if (typeof held !== "object" || held === null) return held;
  let copy: T;
  try {
    copy = structuredClone(held);
  } catch {
    return held; // not plain data: whoever reads it releases it
  }
  // workerd puts the disposer on what a call across Workers answers; the answer's own type, plain
  // data, does not name it
  (held as Partial<Disposable>)[Symbol.dispose]?.();
  return copy;
}

// ── patch ── the LiveView-style delta that rides every live-state change event. `diff` runs at
// the PRODUCER (the processor's reduce, the mini-app helper's set) where old and new value are
// both in hand; the stream forwards the ops verbatim; only CLIENTS apply them. The format is an
// RFC 6902 subset (add / replace / remove, JSON-Pointer paths) so any off-the-shelf json-patch
// library applies our patch payloads — we invent no wire format, we just never emit the exotic ops.

export type PatchOp =
  | { op: "add"; path: string; value: unknown }
  | { op: "replace"; path: string; value: unknown }
  | { op: "remove"; path: string };

const isRecord = (v: unknown): v is Record<string, unknown> =>
  typeof v === "object" && !!v && !Array.isArray(v);

const escape = (seg: string | number): string =>
  String(seg).replaceAll("~", "~0").replaceAll("/", "~1");

/** Structural deep-equal over plain JSON values — order-insensitive, unbudgeted (JSON is acyclic).
 *  THE one deep-equal: the live-state diff's "don't emit" test AND the idempotency-body compare
 *  (re-exported through stream/processor.ts). The `Object.hasOwn(b, k)` guard is load-bearing — without
 *  it, two objects with the same key COUNT but different key SETS compare equal. */
export function jsonEqual(a: unknown, b: unknown): boolean {
  if (Object.is(a, b)) return true;
  if (Array.isArray(a) && Array.isArray(b))
    return a.length === b.length && a.every((v, i) => jsonEqual(v, b[i]));
  if (isRecord(a) && isRecord(b)) {
    const ka = Object.keys(a);
    return (
      ka.length === Object.keys(b).length &&
      ka.every((k) => Object.hasOwn(b, k) && jsonEqual(a[k], b[k]))
    );
  }
  return false;
}

/** Structural diff `a → b`, or undefined when deep-equal (the producer's "don't emit" signal).
 *  Objects recurse per key; arrays get the chat-log fast paths (pure append → `add …/-` ops,
 *  pure tail-truncate → `remove` ops) and are replaced wholesale on any middle divergence —
 *  the LiveView trade: optimize the growing log, full-render the rewrite.
 *  Both sides are JSON-normalized first — the wire is JSON, so the diff must see exactly what
 *  the wire will carry: undefined-valued keys vanish, Dates become their ISO strings, array
 *  holes become null. Diffing what you didn't normalize is how a Date change goes silent. */
export function diff(a: unknown, b: unknown): PatchOp[] | undefined {
  // stringify can itself return undefined (a bare function, toJSON() → undefined) — parse
  // would then throw SyntaxError('"undefined" is not valid JSON'); treat those as absent.
  const json = (v: unknown) => {
    const s = JSON.stringify(v);
    return s ? (JSON.parse(s) as unknown) : undefined;
  };
  return walk(json(a), json(b), "");
}

function walk(a: unknown, b: unknown, path: string): PatchOp[] | undefined {
  if (jsonEqual(a, b)) return undefined;
  if (Array.isArray(a) && Array.isArray(b)) {
    const shared = Math.min(a.length, b.length);
    let p = 0;
    while (p < shared && jsonEqual(a[p], b[p])) p++;
    if (p === a.length) return b.slice(p).map((value) => ({ op: "add", path: `${path}/-`, value }));
    if (p === b.length)
      return a
        .slice(p)
        .map((_, i) => ({ op: "remove", path: `${path}/${a.length - 1 - i}` }) as const);
    return [{ op: "replace", path, value: b }];
  }
  if (isRecord(a) && isRecord(b)) {
    const ops: PatchOp[] = [];
    for (const k of Object.keys(a))
      if (!Object.hasOwn(b, k)) ops.push({ op: "remove", path: `${path}/${escape(k)}` });
    for (const k of Object.keys(b)) {
      if (!Object.hasOwn(a, k)) ops.push({ op: "add", path: `${path}/${escape(k)}`, value: b[k] });
      else ops.push(...(walk(a[k], b[k], `${path}/${escape(k)}`) ?? []));
    }
    return ops.length ? ops : undefined;
  }
  return [{ op: "replace", path, value: b }];
}

/** Apply a patch non-mutatingly (clone-then-mutate). The client half of `diff` — exported
 *  through the SDK so subscribers need no third-party json-patch dependency. */
export function applyPatch<T>(doc: T, ops: PatchOp[]): T {
  let root: unknown = structuredClone(doc);
  for (const op of ops) {
    if (op.path === "") {
      if (op.op === "remove") throw new Error("applyPatch: cannot remove the document root");
      root = op.value;
      continue;
    }
    const segs = op.path
      .slice(1)
      .split("/")
      .map((s) => s.replaceAll("~1", "/").replaceAll("~0", "~"));
    // Patches arrive over the wire. "__proto__" is refused outright (assigning it invokes the
    // prototype setter, not a property write); other prototype members ("constructor", …) are
    // ordinary keys — traversal below is own-property-only, so the chain is never walked.
    for (const s of segs)
      if (s === "__proto__") throw new Error(`applyPatch: refusing __proto__ in path ${op.path}`);
    const last = segs.pop()!;
    let parent = root;
    for (const s of segs) {
      parent = Array.isArray(parent)
        ? parent[Number(s)]
        : isRecord(parent) && Object.hasOwn(parent, s)
          ? parent[s]
          : undefined;
      if (parent === undefined) throw new Error(`applyPatch: missing path ${op.path}`);
    }
    if (Array.isArray(parent)) {
      if (op.op === "add") {
        if (last === "-") parent.push(op.value);
        else parent.splice(Number(last), 0, op.value);
      } else if (op.op === "replace") parent[Number(last)] = op.value;
      else parent.splice(Number(last), 1);
    } else if (isRecord(parent)) {
      if (op.op === "remove") delete parent[last];
      else parent[last] = op.value;
    } else throw new Error(`applyPatch: path ${op.path} traverses a non-container`);
  }
  return root as T;
}

// ── timeout ── race a promise against a deadline. The timer is CLEARED on every exit: a leaked
// timer per call would pin a Durable Object awake. A loss rejects with code TIMEOUT so a caller can
// tell "took too long" from the call's own failure (the DO's facet watchdog aborts the facet on it).
//
// `what` may be a THUNK: a label whose construction is expensive (the facet watchdog prints the whole
// pushed batch) is built ONLY when the deadline is actually lost, off the per-call hot path.

export async function withTimeout<T>(
  promise: Promise<T>,
  ms: number,
  what: string | (() => string),
): Promise<T> {
  let timer: ReturnType<typeof setTimeout> | undefined;
  try {
    return await Promise.race([
      promise,
      new Promise<never>((_, reject) => {
        timer = setTimeout(
          () =>
            reject(
              codedError(
                "TIMEOUT",
                `${typeof what === "function" ? what() : what}: no answer in ${ms / 1000}s`,
              ),
            ),
          ms,
        );
      }),
    ]);
  } finally {
    if (timer) clearTimeout(timer);
  }
}

// ── bytes ──

/** Bytes to base64, chunked so a long buffer cannot overflow the call stack's argument list. */
export function bytesToBase64(bytes: Uint8Array) {
  let binary = "";
  for (let index = 0; index < bytes.length; index += 0x8000) {
    binary += String.fromCharCode(...bytes.subarray(index, index + 0x8000));
  }
  return btoa(binary);
}

// ── origin ── the one check that makes an ambient cookie safe to honour (app-server.ts, and the
// boundary Worker's cookie sessions, core/boundary-worker/src/host-session.ts).

/** Whether `request` may spend the cookies it carries: its `Origin` header is this origin, or absent
 *  (a non-browser client — curl, a script). A browser stamps the page's origin on every WebSocket
 *  handshake, every cross-site fetch and every form POST, so a foreign origin means a foreign site
 *  drove the request with the visitor's cookie riding along. A malformed `Origin` (the literal
 *  `null` of a sandboxed document included) is foreign. */
export function isSameOriginBrowserRequest(request: Pick<Request, "url" | "headers">): boolean {
  const origin = request.headers.get("origin");
  // oxlint-disable-next-line iterate/simple-truthiness-check -- security: only a truly absent Origin (non-browser client) is trusted; an empty "" Origin header must stay foreign, which a truthiness check would wrongly accept
  if (origin === null) return true;
  try {
    return new URL(origin).origin === new URL(request.url).origin;
  } catch {
    return false;
  }
}

/** A cookie pair or name without the space and tab that a `Cookie` header allows around it, and
 *  nothing else: `trim()` also takes a non-breaking or other Unicode space, which makes another
 *  name on the wire read as a protected one. */
export const cookieTrimmed = (text: string) => text.replace(/^[ \t]+|[ \t]+$/g, "");

/** The value of the cookie `name` in a `Cookie` header, or null. */
export function cookieValueOf(cookieHeader: string | null, name: string): string | null {
  for (const part of (cookieHeader || "").split(";")) {
    const separator = part.indexOf("=");
    if (separator < 0) continue;
    if (cookieTrimmed(part.slice(0, separator)) === name)
      return cookieTrimmed(part.slice(separator + 1));
  }
  return null;
}

/** `next` as a path on `origin`, else "/" — a redirect never leaves the host: `//evil.example`,
 *  `/\evil.example` and an absolute URL all resolve to a foreign origin and fall back to "/". So
 *  does a same-origin path that parses to one starting `//` (`/.//evil.example`, `/..//…`,
 *  `/%2e//…`, `/a/..//…`), which a browser reads in a `Location` as another host. The auth Worker's
 *  sign-in redirects use it too (core/auth-worker/src/email-sign-in.ts, passkey-sign-in.ts). */
export function sameOriginPath(next: string, origin: string): string {
  try {
    const url = new URL(next, origin);
    return url.origin === origin && !url.pathname.startsWith("//")
      ? url.pathname + url.search
      : "/";
  } catch {
    return "/";
  }
}

/** Only plain HTTP loopback origins use development login and client registration. */
export function isLocalOrigin(origin: string) {
  const url = new URL(origin);
  return (
    url.protocol === "http:" &&
    (url.hostname === "localhost" ||
      url.hostname.endsWith(".localhost") ||
      url.hostname === "127.0.0.1")
  );
}

/** Resolve a `cd` target against a context's own path — the one resolver every `cd` (the edge
 *  method, the built-in root, the library's relative handles) shares. Absolute ("/agents/x") stands alone; relative
 *  ("agents/x", "../inbox", ".") joins onto `base`. `.` and `..` resolve; the root cannot be
 *  escaped ("/.." is "/"). The result is canonical: leading slash, no trailing slash but for "/". */
export function resolveContextPath(basePath: string, contextPath: string): string {
  const segments: string[] = [];
  for (const seg of `${contextPath.startsWith("/") ? "" : basePath}/${contextPath}`.split("/")) {
    if (seg === "" || seg === ".") continue;
    if (seg === "..") segments.pop();
    else segments.push(seg);
  }
  return `/${segments.join("/")}`;
}

// ── environment ──
/**
 * Which deployment a page is on, told apart in the browser tab: a per-PR preview gets a purple icon
 * with its PR number and a `[pr<N>]` title prefix, local dev a teal icon and `[dev]`, and
 * production keeps the app's own plain logo and its titles untouched, in every client.
 *
 * Read from the page's own hostname, the one fact the Worker, the server render and the browser
 * all agree on, so no env var or config carries it (envs.ts names the hosts):
 * - `pr<N>-<sha7>-<app>.<subdomain>.workers.dev`: a PR's per-commit deployment
 * - `localhost`, `*.localhost`, `127.0.0.1`: `pnpm dev`
 * - anything else: production (dash.iterate.com, agents.iterate.com, …)
 *
 * Rendered by environment-head-content.tsx in every client's root (packages/ui).
 */
export function deploymentEnvironment(hostname: string) {
  const preview = /^(pr(\d+)(?:-[^.]+)?)-[^.-]+\.[^.]+\.workers\.dev$/.exec(hostname);
  // `deployment`: the name every worker of the set shares, the host's first label without its app
  // (`pr2990-a1b2c3d`), which the sign-in page shows so a reader knows which commit they are on
  if (preview) return { kind: "preview" as const, pr: Number(preview[2]), deployment: preview[1]! };
  if (hostname === "localhost" || hostname.endsWith(".localhost") || hostname === "127.0.0.1")
    return { kind: "dev" as const };
  return { kind: "production" as const };
}

export type DeploymentEnvironment = ReturnType<typeof deploymentEnvironment>;

/** `Dash` → `[pr2990] Dash` on a preview, `[dev] Dash` locally, `Dash` in production. */
export function environmentTitle(environment: DeploymentEnvironment, title: string) {
  if (environment.kind === "production") return title;
  return `[${environment.kind === "preview" ? `pr${environment.pr}` : "dev"}] ${title}`;
}

/** Production's icon is the app's own file (`productionHref`), byte for byte; preview and dev get
 *  an inline SVG, so no app ships or routes a second file. */
export function environmentFaviconHref(environment: DeploymentEnvironment, productionHref: string) {
  if (environment.kind === "production") return productionHref;
  return `data:image/svg+xml,${encodeURIComponent(environmentFaviconSvg(environment))}`;
}

/**
 * Preview: purple, the PR number in white as large as the square allows (PR numbers run to four
 * digits and more, too many for the corner badge iterate/iterate#2197 drew for single-digit preview slots).
 * Dev: teal, the white iterate mark (packages/ui's iterate-logo.svg's paths).
 */
export function environmentFaviconSvg(
  environment: Exclude<DeploymentEnvironment, { kind: "production" }>,
) {
  if (environment.kind === "dev")
    return `<svg width="500" height="500" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg"><rect width="500" height="500" fill="${FAVICON_BACKGROUNDS.dev}"/><g fill="white" transform="translate(20 20) scale(0.92)"><path d="M264.649 170.149H289.821L286.092 186.904L276.303 233.444L270.709 259.971L263.717 293.015L258.124 320.008L251.131 352.586L249.267 364.687V371.668L249.733 372.133H253.462L259.522 369.806L266.048 365.617L275.371 357.24L282.829 349.328L286.558 345.14L288.888 346.071L294.948 350.725L308 360.498L307.068 362.36L303.339 367.944L296.813 376.322L291.685 382.837L286.558 388.422L282.363 393.076L275.837 399.592L272.108 402.849L267.446 406.573L262.785 409.83L256.725 413.554L247.869 417.742L238.08 420.535L231.554 421H224.096L216.637 420.069L211.51 418.673L206.382 416.811L201.255 413.088L196.594 408.434L192.865 400.988L191.466 394.938L191 389.818V383.768L193.797 365.152L199.857 335.832L207.315 301.392L224.096 223.205L225.028 216.224V206.916L224.562 205.054L222.231 204.123L219.434 203.193L206.382 203.658L196.127 204.589H193.331V178.526L194.263 175.734L258.59 170.615L264.649 170.149Z"/><path d="M264.649 78H268.844L275.836 78.9308L282.362 80.7924L287.49 83.5848L292.151 87.7734L295.414 92.8928L297.278 96.616L299.143 105.924L299.609 113.836L299.143 118.49L298.677 122.213L296.812 128.729L293.549 134.779L290.286 138.502L286.091 141.76L282.362 143.621L278.167 145.018L274.438 145.948L267.912 146.414H260.92L254.394 145.483L249.267 144.087L244.139 141.294L239.944 138.037L236.681 133.383L233.884 127.332L232.486 121.282L232.02 117.559V108.716L232.952 101.735L234.816 95.6852L237.613 90.1004L240.41 86.3772L246.936 82.1886L252.529 79.8616L259.522 78.4654L264.649 78Z"/></g></svg>`;
  const digits = String(environment.pr);
  // A bold sans digit is about 0.58em wide: fill 460 of the 500 across, and no taller than 360.
  const fontSize = Math.min(360, Math.floor(460 / (0.58 * digits.length)));
  // Three digits and more are stretched upright (at most 1.6×) to stay legible at 16px.
  const stretch = Math.min(1.6, 360 / fontSize);
  // Centred: the baseline sits half a digit's height (≈ 0.72em) below the middle.
  const baseline = Math.round((250 + 0.36 * fontSize * stretch) / stretch);
  return `<svg width="500" height="500" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg"><rect width="500" height="500" fill="${FAVICON_BACKGROUNDS.preview}"/><text x="250" y="${baseline}" transform="scale(1 ${Math.round(stretch * 100) / 100})" text-anchor="middle" fill="white" font-family="Arial, Helvetica, sans-serif" font-size="${fontSize}" font-weight="800">${digits}</text></svg>`;
}

const FAVICON_BACKGROUNDS = { preview: "#7C3AED", dev: "#0F766E" };

/** The names a mind has, of what `whoami` answered (api.ts says which mind has which), for the
 *  metadata of a request to the AI Gateway. A name the mind does not have gets no key: the
 *  binding's metadata takes no undefined value, and a spend rule partitions on the keys it finds. */
export function mindNamesOf(who: { mindId?: string; mindSlug?: string }): {
  mindId?: string;
  mindSlug?: string;
} {
  return {
    // oxlint-disable-next-line iterate/simple-truthiness-check -- a missing name must be a missing key, as the docstring says
    ...(who.mindId && { mindId: who.mindId }),
    // oxlint-disable-next-line iterate/simple-truthiness-check -- as the line above
    ...(who.mindSlug && { mindSlug: who.mindSlug }),
  };
}