iterate.com / kernel

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

CLI: iterate login takes --device before the address (#16)

Card: tasks/accept-the-device-flag-before-the-address-in-iterate-login.md of /repos/task-board
(asked by /agents/claude/cli-device-qr-size-mwy42a8w-a5b868 on 26 Nov 2026).

`--device` takes an optional `true` or `false` (`--device [boolean]`). So the parser read the word
after it as its value: `iterate login --device iterate.com` lost its address and printed "missing
required argument 'address'". `iterate login iterate.com --device` worked.

Now `runCli` spells a `--device` of `login` that no `true` or `false` follows as `--device=true`
(`loginArgvOf`), before the parser reads it. The next word stays what it was. No other command
and no other flag is touched.

- Both orders hand `login` the address and `device: true`.
- `--device true`, `--device false` and `--device=false` do what they did, in either order.
- `iterate login --device` with no address still says "missing required argument 'address'".
- The help is not changed and is right: `--device [boolean]`.

The sign-in itself is not touched: `login`, `loginFlowFor` and the stored session are as they
were. The input's schema moved out of the router as `LoginInput`, so that the tests parse with the
real one.

Proof:

- `cli.test.ts` parses each argv with trpc-cli, `LoginInput` and a `login` handler that signs in
  nowhere: 8 forms that reach the handler, 3 that miss the address, the help line, and that
  another command's argv is left alone.
- `bin/iterate.js` with an empty config home and an address that does not resolve:
  `login --device nomind.invalid`, `login nomind.invalid --device` and
  `login --device false nomind.invalid` all reach discovery ("iterate.nomind.invalid could not be
  reached"); `login --device` says the address is missing.

npm: this is source alone. No release was made, and none is asked for here.

Session: claude 565e456e-5522-4bf4-8083-7194fc7b8066 on beelink

Tests: core/lib 1114 pass (13 new in `cli.test.ts`). core/lib type-checks.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Je5kJV8v5dbNuDKb3stDyz
Iterate-Run: /@777895

jonas, 2026-11-26 23:07 UTC · 567769dfeeb6e529dad6b5eb13c6ba40c26e65ef · parent ca5d72a

2 files changed

M core/lib/src/cli/cli.test.ts
@@ -1,9 +1,95 @@
 import { spawnSync } from "node:child_process";
 import { readFileSync } from "node:fs";
 import { fileURLToPath } from "node:url";
+import { createCli, os } from "trpc-cli";
 import { expect, test } from "vitest";
 import { z } from "zod";
-import { packageVersionOf } from "./cli.ts";
+import { LoginInput, loginArgvOf, packageVersionOf } from "./cli.ts";
+
+/** `iterate <argv>` as the CLI parses it, with a `login` that signs in nowhere: what the handler
+ *  got, the exit code and what was printed. */
+const parsedLogin = async (argv: string[]) => {
+  const inputs: z.infer<typeof LoginInput>[] = [];
+  const printed: string[] = [];
+  let exitCode: number | undefined;
+  const cli = createCli({
+    name: "iterate",
+    router: {
+      login: os.input(LoginInput).handler(({ input }) => {
+        inputs.push(input);
+        return "signed in";
+      }),
+    },
+  });
+  await cli
+    .run({
+      argv: loginArgvOf(argv),
+      prompts: false,
+      logger: {
+        info: (line) => printed.push(String(line)),
+        error: (line) => printed.push(String(line)),
+      },
+      process: {
+        exit: (code) => {
+          exitCode ??= code;
+          return undefined as never;
+        },
+      },
+    })
+    .catch(() => {});
+  return { inputs, exitCode, printed: printed.join("\n") };
+};
+
+test.for<{ argv: string[]; input: z.infer<typeof LoginInput> }>([
+  { argv: ["login", "--device", "example.com"], input: { address: "example.com", device: true } },
+  { argv: ["login", "example.com", "--device"], input: { address: "example.com", device: true } },
+  { argv: ["login", "example.com"], input: { address: "example.com" } },
+  {
+    argv: ["login", "--device", "true", "example.com"],
+    input: { address: "example.com", device: true },
+  },
+  {
+    argv: ["login", "--device", "false", "example.com"],
+    input: { address: "example.com", device: false },
+  },
+  {
+    argv: ["login", "example.com", "--device", "false"],
+    input: { address: "example.com", device: false },
+  },
+  {
+    argv: ["login", "--device=false", "example.com"],
+    input: { address: "example.com", device: false },
+  },
+  {
+    argv: ["login", "--device", "--expect-user", "a@example.com", "example.com"],
+    input: { address: "example.com", device: true, expectUser: "a@example.com" },
+  },
+])("iterate $argv hands login $input", async ({ argv, input }) => {
+  const { inputs, exitCode } = await parsedLogin(argv);
+  expect({ inputs, exitCode }).toEqual({ inputs: [input], exitCode: 0 });
+});
+
+test.for([["login", "--device"], ["login", "--device", "false"], ["login"]])(
+  "iterate %s names the missing address and signs in nowhere",
+  async (...[argv]) => {
+    const { inputs, exitCode, printed } = await parsedLogin(argv);
+    expect({ inputs, exitCode }).toEqual({ inputs: [], exitCode: 1 });
+    expect(printed).toContain("missing required argument 'address'");
+  },
+);
+
+test("only login's --device is spelled anew", () => {
+  expect(loginArgvOf(["run", "--device", "x"])).toEqual(["run", "--device", "x"]);
+  expect(loginArgvOf(["login", "--device", "example.com"])).toEqual([
+    "login",
+    "--device=true",
+    "example.com",
+  ]);
+});
+
+test("iterate login --help shows the flag as it is taken", async () => {
+  expect((await parsedLogin(["login", "--help"])).printed).toContain("--device [boolean]");
+});
 
 test("iterate --version and -v print the package version and exit 0", () => {
   const pkg = z
M core/lib/src/cli/cli.ts
@@ -195,6 +195,30 @@
   });
 }
 
+/** What `iterate login` takes. */
+export const LoginInput = z.object({
+  address: z.string().meta({ positional: true }).describe("The mind's address, e.g. example.com"),
+  device: z
+    .boolean()
+    .optional()
+    .describe(
+      "Approve from a browser on any device, when the mind's issuer offers it: the default over SSH, on Linux with no display, and under a coding agent",
+    ),
+  expectUser: z.string().optional().describe("Refuse and end a session for anyone but this email"),
+});
+
+/** `--device` takes an optional `true` or `false`, so the parser reads the word after it as its
+ *  value: `iterate login --device example.com` lost its address. Here a `--device` of `login` that
+ *  no `true` or `false` follows is spelled `--device=true`, and the next word stays what it was. */
+export const loginArgvOf = (argv: string[]): string[] =>
+  argv[0] === "login"
+    ? argv.map((arg, index) =>
+        arg === "--device" && !["true", "false"].includes(argv[index + 1] ?? "")
+          ? "--device=true"
+          : arg,
+      )
+    : argv;
+
 /** `iterate login`: sign in to the mind at `address` (as `addressOf` read it from what a person
  *  typed), check who that is, and make it the profile's mind. `chatgpt connect` signs in by this
  *  too. */
@@ -679,24 +703,7 @@
       }),
   },
   login: os
-    .input(
-      z.object({
-        address: z
-          .string()
-          .meta({ positional: true })
-          .describe("The mind's address, e.g. example.com"),
-        device: z
-          .boolean()
-          .optional()
-          .describe(
-            "Approve from a browser on any device, when the mind's issuer offers it: the default over SSH, on Linux with no display, and under a coding agent",
-          ),
-        expectUser: z
-          .string()
-          .optional()
-          .describe("Refuse and end a session for anyone but this email"),
-      }),
-    )
+    .input(LoginInput)
     .meta({ description: "Sign in to a mind and make it the profile's mind" })
     .handler(async ({ input }) => login({ ...input, address: addressOf(input.address) })),
   logout: os
@@ -1068,6 +1075,7 @@
     return;
   }
   configFlagOverride = consumeCliStringFlag("--config");
+  process.argv.splice(2, Infinity, ...loginArgvOf(process.argv.slice(2)));
   if (process.argv.length === 2) process.argv.push("--help");
   if (process.argv.length === 3 && process.argv[2] === "-v") process.argv[2] = "--version";
   const cli = createCli({