iterate.com / kernel

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

deploy: the command that goes on keeps the person's flags; a kept token from before the card's scope is used

The reviewer's two findings on #20:
- The printed command to go on was built anew and dropped --owner, so a
  second run could give a new mind another owner. It is now the person's
  own command with --account <id> in place of --account and --new-account.
- A valid token in the config folder from the release before was refused
  for not having asked aig.read, and forced a new sign-in. A kept token
  that asked for every needed scope is used (keptTokenServes).

Via: /agents/claude/installer-cloudflare-card-prompt-mwynk7y7-d3de92
Session: claude 3eb581c8-1b51-4686-8d1d-b642d0822a42 on beelink
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014UJeUkb5YT4HuZzZEVabHj

Jonas Templestein, 2026-11-27 07:29 UTC · 84b930565e983ba75616e0a2e29198bec66286f1 · parent 247c6d7

4 files changed

M core/lib/src/cli/cloudflare-access.test.ts
@@ -3,7 +3,9 @@
 import { join } from "node:path";
 import { expect, test } from "vitest";
 import {
+  CARD_SCOPE,
   DEPLOY_SCOPES,
+  keptTokenServes,
   describeNetworkError,
   dropKeptAccess,
   KeptSignIn,
@@ -212,4 +214,18 @@
   } finally {
     rmSync(folder, { recursive: true, force: true });
   }
+});
+
+test("a token in the config folder from before the card's scope still serves a deploy; one that lacks a needed scope, or ends soon, does not", () => {
+  const at = 1_800_000_000_000;
+  const before = { scopes: DEPLOY_SCOPES.join(" "), expiresAt: at + 40 * 60_000 };
+  // the deploy asks a new sign-in for CARD_SCOPE too, and needs it for nothing
+  expect(keptTokenServes(before, DEPLOY_SCOPES, at)).toBe(true);
+  const withCard = { ...before, scopes: [...DEPLOY_SCOPES, CARD_SCOPE].join(" ") };
+  expect(keptTokenServes(withCard, DEPLOY_SCOPES, at)).toBe(true);
+  expect(DEPLOY_SCOPES).not.toContain(CARD_SCOPE);
+
+  const lacking = { ...before, scopes: DEPLOY_SCOPES.filter((s) => s !== "d1.write").join(" ") };
+  expect(keptTokenServes(lacking, DEPLOY_SCOPES, at)).toBe(false);
+  expect(keptTokenServes({ ...before, expiresAt: at + 19 * 60_000 }, DEPLOY_SCOPES, at)).toBe(false);
 });
M core/lib/src/cli/cloudflare-access.ts
@@ -355,6 +355,19 @@
   throw new Error("Nobody approved the sign-in at Cloudflare in time. Run the command again.");
 }
 
+/** WHETHER THE TOKEN IN THE CONFIG FOLDER SERVES A RUN that needs `scopes`: its sign-in asked for
+ *  each of them, and it lives long enough still. What else that sign-in asked for, or did not,
+ *  decides nothing: a scope of `alsoAsk` is for a new sign-in alone and forces none, so a token
+ *  of a version before such a scope is used and not signed in for again. */
+export const keptTokenServes = (
+  kept: { scopes: string; expiresAt: number },
+  scopes: readonly string[],
+  now: number,
+) => {
+  const had = new Set(kept.scopes.split(" "));
+  return scopes.every((scope) => had.has(scope)) && kept.expiresAt - now > LEAST_LEFT_MS;
+};
+
 /** A token for Cloudflare's API: the environment's, the kept sign-in in the file `keptIn`, the
  *  kept one of a sign-in that asked for these `scopes`, or a new sign-in, for which `show` gets the
  *  page to open and the code the person checks there. `fresh` skips the token in the config
@@ -378,7 +391,7 @@
   }
   if (given) return { token: given, from: "environment", lacks: [] };
   const before = !fresh && existsSync(KEPT) ? parsed(Kept, readFileSync(KEPT, "utf8")) : undefined;
-  if (before && before.scopes === asked.join(" ") && before.expiresAt - Date.now() > LEAST_LEFT_MS)
+  if (before && keptTokenServes(before, scopes, Date.now()))
     return { token: before.token, from: "sign-in", lacks: before.lacks };
 
   const answer = await deviceSignIn(asked, show);
M core/lib/src/cli/deploy.test.ts
@@ -915,18 +915,38 @@
   expect(step!.words).toContain(`If it has stopped, run ${step!.resume}`);
   expect(step!.words).not.toContain("contact your administrator");
 
-  // a domain that a flag named is in the command that goes on; a new account gets the note
+  // the command that goes on is the person's own, every flag kept (the owner above all), with the
+  // account by its id in place of the flags that chose one; a new account gets the note
   const named = paymentCardStep({
     account: cardAccount,
     card: "none",
     paid: false,
     created: true,
-    domain: "ada-labs.com",
+    args: [
+      "deploy",
+      "--new-account",
+      "--domain",
+      "ada-labs.com",
+      "--owner",
+      "ada@example.org",
+      "--owner-email-patterns",
+      "*@ada's.example",
+    ],
   });
   expect(named!.resume).toBe(
-    `npx iterate@latest deploy --account ${cardAccount} --domain ada-labs.com`,
+    `npx iterate@latest deploy --domain ada-labs.com --owner ada@example.org --owner-email-patterns '*@ada'\\''s.example' --account ${cardAccount}`,
   );
   expect(named!.words).toContain("contact your administrator");
+  for (const chosen of [["--account", "Ada Labs"], ["--account=other"]])
+    expect(
+      paymentCardStep({
+        account: cardAccount,
+        card: "none",
+        paid: false,
+        created: false,
+        args: ["deploy", ...chosen, "--buy"],
+      })!.resume,
+    ).toBe(`npx iterate@latest deploy --buy --account ${cardAccount}`);
 });
 
 test("an account with a card is asked nothing, and a card that nobody could read is never read as no card", () => {
M core/lib/src/cli/deploy.ts
@@ -984,10 +984,22 @@
   return said.has_default_payment_method ? "there" : "none";
 }
 
-/** The command that goes on with this deploy: the account by its id, so that a second run asks for
- *  no account and makes no second new one, and the domain when a flag named it. */
-export const resumeCommandOf = (account: string, domain?: string) =>
-  `npx iterate@latest deploy --account ${account}${domain ? ` --domain ${domain}` : ""}`;
+/** The command that goes on with this deploy: THE PERSON'S OWN COMMAND, every flag of it kept
+ *  (`--owner` names a new mind's owner, and a command without it would give the mind another),
+ *  with the account by its id in place of `--account` and `--new-account`, so that a second run
+ *  asks for no account and makes no second new one. `args`: what followed the program's name. */
+export function resumeCommandOf(account: string, args: readonly string[] = ["deploy"]) {
+  const kept: string[] = [];
+  for (let index = 0; index < args.length; index++) {
+    const arg = args[index]!;
+    if (arg === "--new-account" || arg.startsWith("--account=")) continue;
+    if (arg === "--account") index++;
+    else kept.push(arg);
+  }
+  const quoted = (arg: string) =>
+    /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replaceAll("'", "'\\''")}'`;
+  return ["npx", "iterate@latest", ...kept, "--account", account].map(quoted).join(" ");
+}
 
 /** THE STEP FOR A PAYMENT CARD, which comes before every other step of the account: only for an
  *  account that Cloudflare says has no card (`paymentCardOf`) and that has no Workers Paid, so one
@@ -1001,10 +1013,11 @@
   card: PaymentCard;
   paid: boolean;
   created: boolean;
-  domain?: string;
+  /** the command's own arguments (`resumeCommandOf`) */
+  args?: readonly string[];
 }) {
   if (input.card !== "none" || input.paid) return undefined;
-  const resume = resumeCommandOf(input.account, input.domain);
+  const resume = resumeCommandOf(input.account, input.args);
   const lines = [
     "Add a payment card to your Cloudflare account. It is the one step that only you can do: without a card Cloudflare sells neither Workers Paid ($5 a month) nor a domain, and a mind needs both.",
     "Type the card on Cloudflare's page, never here. This command does not ask for a card, and this step buys nothing.",
@@ -2694,7 +2707,13 @@
     // before Workers Paid. Nothing was changed yet, but for an account that this run made.
     const cardStep = input.plan
       ? undefined
-      : paymentCardStep({ account: account.id, card, paid, created, domain: input.domain });
+      : paymentCardStep({
+          account: account.id,
+          card,
+          paid,
+          created,
+          args: process.argv.slice(2),
+        });
     if (cardStep) {
       if (!interactive())
         throw new Error(`${cardStep.words}\n${cardStep.page}\nNothing is deployed.`);