Mint a per-session agent token with auth token --session-id

Merged
JE
1mo ago
+772-9

Why

An agent profile's shared token is cached per profile and served to every process on the host. Run a dozen sandboxed boxes and the connector on one computer, and Basecamp sees one token: every request reads as the agent, with nothing saying which launch made it.

What

basecamp auth token --stored --session-id <id> [--session-label <label>] mints a fresh self-token for that one session. The id and label ride the client_credentials request as launch_id and launch_label. bc3 stores them on the token and logs the id beside the agent. bc3 can't use "session" for this, because there it means web sign-in. The CLI keeps "session", the word its callers use.

  • Validated locally, before anything is sent, against the server's rules. The id is 32 lowercase hex characters (128 random bits). The label is optional: at most 100 characters, not blank, with no control, format (bidi), private-use, surrogate or line/paragraph-separator characters (\p{Cc}\p{Cf}\p{Co}\p{Cs}\p{Zl}\p{Zp}, the same set bc3 refuses). Unassigned code points are accepted on both sides, because a host's Unicode tables can be newer than the CLI's or the server's, and a refused label costs the launch its whole token. A label without an id, or an id without --stored, is a usage error. A session token never comes from BASECAMP_TOKEN.
  • Agent profiles only. A person's login has no session to attribute, and it isn't handed out in place of one.
  • Without the flags, nothing changes. It's the same path, output and JSON shape. With them, --json adds session_id and session_label beside token.

Decisions worth a look

Not cached. Each call mints. The shared cached token is neither served nor written. The caller is the per-launch cache: the sandbox's nono cmd:// capture holds each token for 240 s inside the launch's own proxy, which dies with the launch. Caching here would mean one keyring entry per launch that nothing ever cleans up. It would also make every launch contend on the profile's one credential lock for a token only that launch may use. A capture every 240 s is about 15 mints an hour per box, well inside the token endpoint's per-client budget.

Fail closed: a token bound to the session, or none.

  • Refused (any 4xx): the command exits non-zero, and the message names the session and the OAuth error code. Following this package's existing rule (oauthErrorCodes), it doesn't repeat error_description, because the request carried the client secret.
  • Ignored: a server that predates session attribution answers 200 with an ordinary token and doesn't echo launch_id (or echoes different values). The command exits non-zero with "this Basecamp predates agent session attribution", and the minted token is discarded: not printed, not cached.
  • It never retries without the session.
  • This is the contract the later steps depend on. Once Basecamp can stop a session, a token not bound to it is one that stopping it wouldn't reach. And a silently unattributed token is exactly the failure that reads as working.
  • The cost falls only on callers that pass the flags. Today that's the sandbox, which ships after bc3.

Holds. A session mint presents the same client secret the shared mint does. So it honors a stored hold before sending anything, and it remembers a refusal of the secret (invalid_client, invalid_grant, a bare 401/403, a 429), so a dozen launches don't keep presenting a dead secret. It takes the credential key's lock only to write a hold; nothing is written on success.

A refusal of the request itself, such as invalid_request for a malformed launch parameter, holds nothing, so one bad launch can't stop the others. Per-(credential, session) holds, for a future "session stopped" refusal, belong with that step and aren't here.

Tests

  • internal/auth/agent_launch_test.go, red then green. It covers:
    • fresh mint each call, with the params sent and the stored credential untouched;
    • an id alone, with no empty label sent;
    • an ignoring server (no echo, another id, another label, a missing label), where the token is discarded and nothing is held;
    • an invalid_request refusal, which names the session and holds nothing;
    • an invalid_client refusal, which is held, so the next session mint sends nothing;
    • a non-agent profile, which is a usage error;
    • local validation, including the bidi-override, line-separator and invalid UTF-8 refusals, and acceptance of a character newer than the build's Unicode tables (U+1FAE9).
  • internal/commands/auth_token_session_test.go: the command's JSON shape with and without a session, and the flag-combination usage errors.
  • .surface updated for the two flags.
  • An empty --session-id "" (a launcher's unset variable) is a usage error, not a fall-back to the shared token. Session mode is decided by whether the flag was given, not by its value. Red then green in TestAuthTokenSessionFlagUsage.
  • bin/ci: green.

Review round

Copilot couldn't review (quota). My own adversarial pass found one real issue, fixed in the second commit: --session-id "$ID" with ID unset fell back to the shared token. It also turned up two points I'm leaving as they are:

  • The label echo is matched exactly, so bc3 has to store and echo the label verbatim. It does (validated, never normalized). A future server that normalizes labels would fail closed, not open.
  • A token minted by a server that ignores the session is discarded here but stays valid server-side for its hour. Nothing can revoke it without client-authenticated revocation, and it is no wider than the shared token the same host already holds.

Overlap with open PRs

  • git merge-tree is clean against #850, #851, #852, #854 and #855.
  • #854 renames the hold API (MintHold โ†’ RenewalHold, rememberMintHold โ†’ rememberRenewalHold). Merged together, the tree doesn't compile until this PR's one call site and its tests take the new names. That's a mechanical fix for whichever lands second, so this is based on main rather than stacked.
  • #852 merged with this one builds and its tests pass.

Related

Step 1 of the multi-session proposal; draft for discussion with the agents team.