plan: agent login by link (5 tasks)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+161
−0
docs/superpowers/plans/2026-11-07-agent-login-link.md+161 −0
CI passed — run details
Intent
Human
Go ahead
Attributed to this commit: 34.5 g CO₂e (4,138,986 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-agent-login-link, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/docs/superpowers/plans/2026-11-07-agent-login-link.md b/docs/superpowers/plans/2026-11-07-agent-login-link.md| new file mode 100644 |
| index 000000000..08a32805d |
| --- /dev/null |
| +++ b/docs/superpowers/plans/2026-11-07-agent-login-link.md |
| @@ -0,0 +1,161 @@ |
| +# Agent Login by Link Implementation Plan |
| + |
| +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. |
| + |
| +**Goal:** An agent session (e.g. Claude Code in the cloud, driven from a phone) logs in to a CARLOS console by showing the person one link that they tap and approve; no typed code. |
| + |
| +**Architecture:** Console side: `GET /auth/cli?code=X` resolves the code behind a pre-checked lookup budget and renders the description + Approve/Deny; decisions keep their own pre-checked budget behind CSRF; new copy says "agent or terminal". CLI side: `carlos auth login` prints `verification_uri_complete`, labels agent sessions, and gains `--no-wait` / `--wait` with a 0600 pending-login file. |
| + |
| +**Tech Stack:** Go (net/http, html/template), the console's SQLite store, the module-root `carlos` CLI. |
| + |
| +**Spec:** `docs/superpowers/specs/2026-11-07-agent-login-link-design.md` (rev 3.1, approved by Paul 2026-11-07). Read it first. |
| + |
| +## Global Constraints |
| + |
| +- R2 is WITHDRAWN: email sign-in is unchanged. Do not touch `internal/console/emailsignin*.go` behaviour. |
| +- Approve stays a POST with the session CSRF token and `requireFreshAuth`; Deny stays ungated; `notifyCLIIssued` unchanged; request TTL `CLIAuthTTL` (10 min) unchanged. |
| +- No codes or tokens in logs; the only code in a URL is the user code in `verification_uri_complete`. |
| +- Every page works with JavaScript off. |
| +- All new or changed user-facing strings are DRAFTS until copy review; put them in `internal/console/signincopy.go` (console) or as consts in `auth.go` (CLI), never inline in several places. Copy rules: no em or en dashes, no customer or tenant names. |
| +- Locally run only the touched packages' tests with `-run`; CI runs the full suite. Leave GOCACHE at its default (sandbox off for go commands). |
| +- Census: `auth.go` is in the module-root package (census scope). Run `make census-card` at the end; the envelope re-pin needs a cross-family approval recorded on #280. |
| +- Land with `amadan branch merge carlos/platform <branch> -squash -expect <full sha>`. |
| + |
| +## Review Focus |
| + |
| +1. A signed-in browser forced (top-level GET) to many bad `/auth/cli?code=` links must still be able to Approve a real code afterwards. (Task 1 test) |
| +2. Past the lookup budget, a VALID code on GET must get the same 429 as a bad one: no oracle. Same for POST decisions. (Task 1 tests) |
| +3. A code that is approved, denied or expired, followed from the link, must not render an Approve button. (Task 2 test) |
| +4. An old `--wait` finishing after a newer `--no-wait` must leave the newer pending file in place. (Task 4 test) |
| +5. `--wait` run with a different `-console` or default must still poll the console that started the login. (Task 4 test) |
| + |
| +--- |
| + |
| +### Task 1: Pre-checked guess budgets for CLI code lookups |
| + |
| +**Files:** |
| +- Modify: `internal/console/authcli.go` (cliLimits, newCLILimits, handleCLIApprove) |
| +- Test: `internal/console/authcli_test.go` |
| + |
| +**Interfaces:** |
| +- Produces: `cliLimits.lookAddr, lookIP *rateLimiter` (GET lookup budget, burst `cliGuessBurst`, window `cliRateWindow`); `func (s *Server) cliBudgetOpen(addrL, ipL *rateLimiter, addr, ip string, now time.Time) bool` (true when neither key is tripped; read-only, uses `rateLimiter.tripped`); `func (s *Server) cliBudgetSpend(addrL, ipL *rateLimiter, addr, ip string, now time.Time)` (calls `allow` on both). Existing `guessAddr/guessIP` become the DECISION budget. |
| + |
| +- [ ] **Step 1: Write the failing tests** |
| + |
| +`TestCLIDecisionBudgetRefusesValidCodeOnceSpent`: sign up, spend the decision budget with `cliGuessBurst` wrong `continue` POSTs, start a real login, POST `continue` with the VALID code → want 429 and body NOT containing the label. Same for `approve` (via `postGated`). |
| + |
| +- [ ] **Step 2: Run to verify failure** |
| + |
| +Run: `go test ./internal/console -run 'TestCLIDecisionBudget' -count=1` |
| +Expected: FAIL (today a valid code still resolves after the budget is spent). |
| + |
| +- [ ] **Step 3: Implement** |
| + |
| +In `handleCLIApprove`, before any `CLIAuthByUserCode` / `ApproveCLIAuth` / `DenyCLIAuth`, refuse with the existing "Too many attempts" 429 when `!s.cliBudgetOpen(s.cli.guessAddr, s.cli.guessIP, addr, ip, now)`. After a lookup or decision that finds nothing, `s.cliBudgetSpend(...)` (replacing the current `badCode` closure). Concurrency: `rateLimiter` is already mutex-guarded; the check-then-spend gap may let a burst exceed the budget by the number of concurrent requests, which is acceptable (note it in a comment). |
| + |
| +- [ ] **Step 4: Run tests** |
| + |
| +Run: `go test ./internal/console -run 'TestCLI' -count=1` |
| +Expected: PASS. |
| + |
| +- [ ] **Step 5: Commit** `console: check the CLI code budget before resolving a code` |
| + |
| +### Task 2: The approval page acts on the code in the link |
| + |
| +**Files:** |
| +- Modify: `internal/console/authcli.go` (handleCLIApprovePage, cliPage), `internal/console/templates/cli_approve.html`, `internal/console/signincopy.go` |
| +- Create: `internal/console/cliplace.go` (location line) |
| +- Test: `internal/console/authcli_test.go` (replace `TestCLIApprovalPageNeverApprovesFromALink`) |
| + |
| +**Interfaces:** |
| +- Consumes: Task 1's `cliBudgetOpen/cliBudgetSpend`, `lookAddr/lookIP`. |
| +- Produces: `type cliPlace struct{ Text, Attribution, AttributionURL string }`; `func (s *Server) cliPlaceFor(remoteAddr string) cliPlace` (one `s.cfg.GeoIPReader.Lookup` snapshot via `geoip.FromSnapshot`; Text = "Subdivision, Country" or "Country"; attribution from `geoip.AttributionFor(a.Source)`; empty when reader nil, StateOff or StateMiss); `cliPage.Place cliPlace`, `cliPage.Started time.Duration` (age of the request). |
| + |
| +- [ ] **Step 1: Write the failing tests** |
| + |
| +`TestCLIApprovalPageResolvesTheLinkCode`: sign up; start with label "Claude Code on box"; GET `/auth/cli?code=<code>` → 200, body contains the code, "Claude Code on box", `value="approve"`, `value="deny"`; poll still `pending` (GET changed nothing). |
| +`TestCLIApprovalPageStaleCodeShowsTheBox`: approve a code, then GET its link → body has the typed-code input and NO `value="approve"`; same for a denied code and an unknown code. |
| +`TestCLIApprovalPageLookupBudget`: GET bad codes until 429; then GET a VALID code → 429 and no label in body. |
| +`TestCLIForcedLookupsLeaveDecisionsWorking` (from Task 1): forced bad GETs past the lookup budget, then `postGated` approve of a real code → "Approved". |
| +`TestCLIPlaceAttribution`: with a fixture GeoIP reader (reuse apigeo tests' fixture) whose source is `DBIP-...`, `cliPlaceFor` returns non-empty Attribution "IP Geolocation by DB-IP"; with a nil reader returns zero `cliPlace`. |
| + |
| +- [ ] **Step 2: Run to verify failure** |
| + |
| +Run: `go test ./internal/console -run 'TestCLIApprovalPage|TestCLIForcedLookups|TestCLIPlace' -count=1` |
| +Expected: FAIL. |
| + |
| +- [ ] **Step 3: Implement** |
| + |
| +`handleCLIApprovePage` (signed in): normalise `?code=`; if code-shaped and `cliBudgetOpen(lookAddr, lookIP)` is false → 429 "Too many attempts"; else `CLIAuthByUserCode`; found → `p.Request`, `p.Place = s.cliPlaceFor(req.RemoteAddr)`, `p.Started = now.Sub(req.CreatedAt)`; not found → `cliBudgetSpend(lookAddr, lookIP)` and render the typed-code form with `p.Error = copyText("cli.link_unknown")`. Signed-out path unchanged except the banner copy key. Delete the "IT DELIBERATELY DOES NOT ACT ON ?code=" comment; replace with R1's reason and the controls that remain (session, CSRF on POST, step-up on Approve, issuance notice, deny, TTL, budgets). Template: when `.Request`, show code large, label + self-reported caveat, "From" IP + `.Place.Text` + attribution link when set, started-ago, expiry, the check question, Approve / That wasn't me. Remove "Nothing here is ever approved by following a link" text. |
| + |
| +Draft copy keys (signincopy.go; copy review later): `cli.title` "Approve a sign-in"; `cli.lede` "{label} is asking to use Carloku as you." (the console's brand name, from the same config the header brand uses (CARLOS_CONSOLE_NAME), never a literal "Carloku"); `cli.check` "Does your agent show this code?"; `cli.not_me` "If you didn't just ask an agent or terminal to log in, press That wasn't me."; `cli.link_unknown` "That link's code has expired or was already used. Ask your agent to start the login again."; `home.cli_prompt` "Sign in to connect your agent or terminal." |
| + |
| +- [ ] **Step 4: Run tests** |
| + |
| +Run: `go test ./internal/console -run 'TestCLI|TestClaimConsoleMutationInvariant' -count=1` |
| +Expected: PASS (update any probe in `claim_invariant_probes_*` that asserted the old page text). |
| + |
| +- [ ] **Step 5: Commit** `console: approve a CLI login from the code in its link` |
| + |
| +### Task 3: After approval, no dead end |
| + |
| +**Files:** |
| +- Modify: `internal/console/templates/cli_approve.html` (cli_decided), `internal/console/authcli.go` (handleCLIApprove approve branch), `internal/console/signincopy.go` |
| +- Test: `internal/console/authcli_test.go` |
| + |
| +**Interfaces:** |
| +- Consumes: Task 2's `cliPage`. |
| +- Produces: `cliPage.Label string`. The apps link is `href="/"`: the front page lands a signed-in person in their current account (handleHome), minting one if needed. |
| + |
| +- [ ] **Step 1: Write the failing test** |
| + |
| +`TestCLIApprovedPagePointsBack`: approve a login labelled "Claude Code on box" → body contains "Claude Code on box" and `href="/"`, and does not contain "terminal". |
| + |
| +- [ ] **Step 2: Run** `go test ./internal/console -run TestCLIApprovedPagePointsBack -count=1` → FAIL. |
| + |
| +- [ ] **Step 3: Implement** Read the request's label before approving (CLIAuthByUserCode) so the decided page can name it. Draft copy: `cli.approved` "Connected. Go back to {label}; it carries on by itself."; `cli.open_apps` "Open your apps"; `cli.denied` "Nothing was issued. That sign-in can't be approved now; if it wasn't yours, whoever started it got nothing."; settings link text `cli.settings_link` "agents and terminals". |
| + |
| +- [ ] **Step 4: Run** `go test ./internal/console -run 'TestCLI' -count=1` → PASS. |
| + |
| +- [ ] **Step 5: Commit** `console: approved sign-in says go back to the agent` |
| + |
| +### Task 4: CLI prints the link, labels agents, and can relay |
| + |
| +**Files:** |
| +- Modify: `auth.go` (authLogin, defaultLabel, the print block ~725-747) |
| +- Create: `authpending.go` (pending-login file) |
| +- Test: `auth_test.go`, create `authpending_test.go` |
| + |
| +**Interfaces:** |
| +- Produces: `type pendingLogin struct{ Console, DeviceCode, UserCode, Label string; ExpiresAt time.Time; Interval int }`; `func pendingLoginPath() string` (beside `credentialsPath()`: same dir, name `login-pending`); `func savePendingLogin(p pendingLogin) error` (atomic 0600, same pattern as `credentialFile.save`); `func loadPendingLogin() (*pendingLogin, error)` (missing file → nil, nil); `func clearPendingLoginIf(deviceCode string) error` (delete only when the file still holds deviceCode); `func agentLabel(env func(string) string, fallback string) string` ("Claude Code on <host>" when `CLAUDECODE=1`, else fallback); `func pollLogin(ctx, c *consoleClient, base, deviceCode, label string, interval int, deadline time.Time, out io.Writer) error` (today's poll loop extracted; stores the credential on approval). |
| + |
| +- [ ] **Step 1: Write the failing tests** |
| + |
| +Update `TestAuthLoginStoresACredential`: output now CONTAINS `fc.ts.URL + "/auth/cli?code=WXYZ-4823"` and "WXYZ-4823", and no longer "type this code" (remove the "never ?code=" assertion and its comment). |
| +`TestAuthLoginNoWaitThenWait`: `authLogin(-console X, --no-wait)` returns nil immediately with the link printed; `loadPendingLogin` has Console X; then `authLogin(--wait)` with a DIFFERENT `-console` stores a credential for X and removes the pending file. |
| +`TestAuthLoginWaitNoPending`: `--wait` with no file → error mentioning "no login waiting". |
| +`TestPendingLoginNewerSurvivesOlderWait`: save A; save B; `clearPendingLoginIf(A.DeviceCode)`; file still holds B. |
| +`TestPendingLoginFileMode`: saved file mode is 0600. |
| +`TestAuthLoginWaitTerminalStatesClear` (denied/unknown/expired via the fake console): file removed; transient poll error keeps it. |
| +`TestAgentLabel`: `CLAUDECODE=1` → "Claude Code on <host>"; unset → fallback; `-label` still wins (via authLogin). |
| + |
| +- [ ] **Step 2: Run** `go test . -run 'TestAuthLogin|TestPendingLogin|TestAgentLabel' -count=1` → FAIL. |
| + |
| +- [ ] **Step 3: Implement** Flags `--no-wait` and `--wait` (mutually exclusive; error if both). `--no-wait`: start, print, `savePendingLogin`, return. `--wait`: load (nil → error "no login waiting: run carlos auth login --no-wait first"), `pollLogin` against `p.Console` (ignore `-console`), then `clearPendingLoginIf(p.DeviceCode)` on approved/denied/unknown/expired; keep the file on transient errors. Plain login: unchanged flow, never touches the pending file. Replace the print block: draft CLI copy consts `loginOpenLink` "Open this link to approve this login:" and `loginCheckCode` "The page will show the code %s. Approve only if it matches."; replace the phishing comment with R1's reason and the remaining controls. `--help` text for `--no-wait`: "print the approval link and exit; finish with --wait (for agents that only see output when a command ends)". |
| + |
| +- [ ] **Step 4: Run** `go test . -run 'TestAuth|TestPendingLogin|TestAgentLabel' -count=1` → PASS. Then `GOOS=darwin go build ./... && GOOS=windows go build ./...`. |
| + |
| +- [ ] **Step 5: Commit** `carlos auth login: print the approval link; --no-wait and --wait` |
| + |
| +### Task 5: Docs, copy review, census, branch review |
| + |
| +**Files:** |
| +- Modify: `docs/site/cli.md` (auth login section: link flow, agents use `--no-wait` then `--wait`) |
| +- Create: `copy-review/strings.json` on the branch (every draft string from Tasks 2-4 and the docs paragraph) |
| +- Census overlay via `make census-propose` |
| + |
| +- [ ] **Step 1:** Docs paragraph (draft): how an agent logs in (`--no-wait`, show the link, `--wait`). |
| +- [ ] **Step 2:** Copy review of all drafts (copy-review skill; Paul edits). Apply approved text verbatim to signincopy.go, auth.go consts, templates, docs; rerun Tasks 2-4 tests. |
| +- [ ] **Step 3:** `make census-card`; write CLAIMS; `make census-propose NAME=agent-login-link CLAIMS=<file>`; `go test ./internal/provision -run TestPlan14RootReadCensus -count=1` → PASS. |
| +- [ ] **Step 4:** Whole-branch review (Astra, cross-family, brief from `docs/superpowers/reviews/REVIEW-BRIEF-TEMPLATE.md`, includes the census verdict). Fix per stopping rules. |
| +- [ ] **Step 5:** CI green on the tip; ask Paul to merge; release via the agent manager (8a3d302) like v0.34.21 (console + CLI: publish, tap, apt; dev console then flagship console in a #421 lock). |