feat: append cli_context param to altimate auth URL for PostHog session correlation

Merged
AL
3mo ago
+588-35

Summary

Appends a cli_context query param to the browser auth URL opened by the CLI during sign-in.

  • buildCliContext() encodes { v: 1, machine_id, cli_version } as a base64url JSON blob
  • machine_id is read from ~/.altimate/machine-id (random UUID, non-PII) — the same value already sent to Azure App Insights telemetry
  • On the frontend, cli_context is decoded and passed to posthog.register() + posthog.alias() to link the CLI device to the authenticated user

Companion PR: AltimateAI/altimate-frontend#3106

Requested by @saravmajestic via harness


Summary by cubic

Adds a base64url-encoded cli_context to the CLI auth URL (now in the URL fragment) so the web app can link the browser session to the CLI device in PostHog. Uses a shared, race‑safe machine_id helper with UUID v4 validation and an enforced 512‑byte cap; honors both env and config telemetry opt‑out.

  • Refactors

    • Extracted getOrCreateMachineId() to altimate/util/machine-id.ts (exclusive-create, UUID v4 check, rejects symlinks/non-regular files; bounded descriptor read enforces the 512‑byte limit, including on race re-reads; returns "" and logs on errors).
    • Updated telemetry, altimate plugin, and cli/welcome to use it; buildCliContext() respects ALTIMATE_TELEMETRY_DISABLED=true and config.telemetry.disabled, fails closed (and logs) if config is unreadable; cli_context appended via buildAuthorizeUrl() in the URL fragment; docs updated to clarify the device/installation ID and PostHog vs App Insights; added tests.
  • Bug Fixes

    • Fixed fresh‑install detection in cli/welcome.ts: probe file existence only (no minting), avoiding ID creation for config‑opt‑out users.
    • Prevented auth failures on read‑only $HOME by handling directory creation errors and returning an empty ID instead of throwing.