feat(proxy): authenticate option sets each caller's tenant

Merged
V3
1mo ago
+710-61

Summary

createDaemonProxy checks every client against one shared clientToken and never sets a tenant, so an embedder serving many callers had to rewrite requests itself, and upload tickets carried the shared token instead of the caller's credential.

New optional authenticate(request) (replaces clientToken; null → the usual 401). The proxy then:

  • stamps the returned tenant on /rpc (meta.tenantId + sessionIsolation: 'tenant', lease tenantId, overriding flags.tenant/flags.sessionIsolation) and the x-agent-device-tenant header elsewhere, overwriting what the client sent;
  • serves request diagnostics only for <tenant>: sessions (the daemon does not partition a declared tenant);
  • puts the caller's own authorization/x-agent-device-token on upload tickets.

Without authenticate, nothing changes. Docs: proxy README, remote-proxy.md. 5 files.

Validation

Commit b342c0b. pnpm check:affected --run: all runnable checks passed (fail-open full set, triggered by untracked local state files).

  • 7 new proxy unit tests; each fails when its stamp, ticket or diagnostics check is removed. 20/20 pass.
  • Built CLI + local daemon + built @agent-device/proxy with authenticate:
    • click @e1 as alice → session alice:default; as bob → bob:default; alice with --tenant bob --session-isolation none → alice:default.
    • bob GET alice's diagnostics → 401 Session is outside this tenant; alice → 200.
    • install … Fake.ipa as alice: PUT /upload/direct/… auth=Bearer alice-key -> alice.
    • unknown key → UNAUTHORIZED.
  • agent-device proxy (clientToken) still works: session list ok, wrong token → 401, session cwd:…:default (no tenant), upload via ticket reaches the install step (DEVICE_NOT_FOUND, empty private sim set).