feat(analytics): add auth status gate for metrics sync - #488
Conversation
Expired CodeMie SSO cookies made the metrics endpoint answer with HTTP 200 Keycloak login HTML instead of 401, so analytics auth failures were silent and sessions disappeared from metrics. - Add analytics-auth-status marker util (records credential rejections) - MetricsApiClient: flag 401/403 and HTML login-page responses as auth failures, write the marker, clear it on successful send - SSO login flow clears the marker on successful authentication - Unified hook: generic UserPromptSubmit gate blocks the prompt with re-login instructions when analytics auth is missing or rejected (all agents, only when analytics sync is configured) - AgentCLI: launch-time analytics auth check for non-SSO providers when analytics is enabled, with interactive re-auth prompt (non-fatal)
Automated review — critical findingsFocused on architecture, patterns, correctness, DRY, YAGNI. Minor issues intentionally omitted. 1.
|
Any add/remove/rename/rewrite of X-CodeMie-* attribution headers, the Responses API `user` body field, JWT sub/email overrides, or their derivation source is now called out as a strictly secured operation that requires security-review sign-off before merge. - security-practices.md: promote request header integrity to a Core Security Principle; add a top-priority "CRITICAL: Project & User Attribution Headers" section covering the in-scope surface (every X-CodeMie-* header injected by header-injection.plugin.ts plus the Responses `user` field and JWT claim overrides), five hard rules, Bad/Good examples, and a reviewer sub-checklist; extend the Common Security Pitfalls table and the Development checklist. - git-workflow.md: lead the Code Review Checklist with a highlighted CRITICAL callout for attribution-header changes and require the security-review sign-off; add a canonical-injector check pointing at header-injection.plugin.ts.
Summary
Expired CodeMie SSO cookies caused the metrics endpoint (fronted by Keycloak) to answer POSTs with HTTP 200 + HTML login page instead of 401. Analytics auth failures were completely silent — sessions vanished from the metrics dashboard while local JSONL deltas looked healthy. This adds a three-layer auth status gate so broken analytics auth is detected and surfaced immediately.
Changes
src/utils/analytics-auth-status.ts(new): file-based marker recording metrics-endpoint credential rejections (~/.codemie/analytics-auth-status.json); best-effort, never throwsmetrics-api-client.ts: classify HTTP 401/403 and non-JSON HTML responses (Keycloak login page) as auth failures; write marker on rejection, clear on successful sendsso.auth.ts: clear the marker on successfulcodemie profile loginhook.ts: genericUserPromptSubmitgate (all agents via unifiedcodemie hook) — blocks the prompt withcodemie profile logininstructions when analytics auth is missing or was server-rejected; active only when analytics sync is configuredAgentCLI.ts: launch-time analytics auth check for non-SSO providers when analytics is enabled (codeMieUrlset); reuses SSOvalidateAuth(stored creds + live API test) with interactive re-auth prompt; non-fatal on decline; clears stale marker on successImpact
Before: expired analytics credentials → silent metrics loss for days (observed 08-12 → 08-14 locally; data recovered only after manual re-login + session resumes).
After: launch-time warning with re-auth prompt; hard block at prompt level with exact re-login command if still unresolved; marker cleared automatically by login or any successful send.
Testing
Checklist
npm run ci)main