Personal AI, On Personal Devices
Python
Make analytics opt-in across setup and the compact sharing modal (#1143)
Elliot Slusky74694cdb4558
diff --git a/.github/workflows/bash-tests.yml b/.github/workflows/bash-tests.ymlindex afb578a0..b7910097 100644--- a/.github/workflows/bash-tests.yml+++ b/.github/workflows/bash-tests.yml@@ -21,7 +21,9 @@ jobs:- name: Install bats-corerun: |sudo apt-get update- sudo apt-get install -y bats+ sudo apt-get install -y bats python3-tomlkit- name: Run bats tests+ env:+ OPENJARVIS_TEST_PYTHON: /usr/bin/python3run: bats tests/install/bash/diff --git a/docs/assets/analytics-opt-in/desktop.jpg b/docs/assets/analytics-opt-in/desktop.jpgnew file mode 100644index 00000000..14a9a10dBinary files /dev/null and b/docs/assets/analytics-opt-in/desktop.jpg differdiff --git a/docs/getting-started/configuration.md b/docs/getting-started/configuration.mdindex 3026ad03..c2e28942 100644--- a/docs/getting-started/configuration.md+++ b/docs/getting-started/configuration.md@@ -465,6 +465,23 @@ jarvis serve --host 127.0.0.1 --port 9000 --model qwen3:8b --agent simple---+### `[analytics]` — Optional External Usage Analytics++External analytics default to off. Interactive Bash installation and `jarvis+init` ask whether to share: **Press Enter or Y to consent, or N to decline**.+Unattended runs do not consent automatically; `--analytics` explicitly enables+sharing and `--no-analytics` declines it.++```toml+[analytics]+enabled = false+```++Use `jarvis config set analytics.enabled true` to enable sharing or `false` to+disable it, then restart the server and reload the frontend. This setting is+independent of local telemetry and leaderboard participation. See+[Telemetry](../telemetry.md) for the event catalog and sharing details.+### `[telemetry]` — Telemetry PersistenceControls whether inference telemetry is recorded and where it is stored.diff --git a/docs/getting-started/install.md b/docs/getting-started/install.mdindex c2326d23..a274bc8c 100644--- a/docs/getting-started/install.md+++ b/docs/getting-started/install.md@@ -31,6 +31,11 @@ model. **You don't need to install uv or any other prerequisite first.**About 3 minutes on a typical broadband connection. Type `jarvis` to start chatting.+Before sending any installation analytics, the Bash installer displays a+dedicated optional-sharing prompt: **Press Enter or Y to consent, or N to+decline**. The choice also controls runtime analytics. No terminal, EOF, or+cancelled input leaves sharing off. See [Telemetry](../telemetry.md) for details.+## What the installer does| Phase | Step | Where |@@ -78,6 +83,8 @@ Local-first remains the default when no key is in env. Precedence is OpenRouter| `--minimal` | Skip the foreground model pull. First chat will need to wait for the bg pull to finish. || `--no-bg-orchestrator` | Don't detach the background work pipeline. (Mostly for testing.) || `--force` | Re-run all steps even if `install-state.json` says they're done. |+| `--analytics` | Consent to installation and runtime usage analytics without prompting. |+| `--no-analytics` | Decline analytics without prompting. |## Environment overridesdiff --git a/docs/telemetry.md b/docs/telemetry.mdindex 455f4ee9..b79d6379 100644--- a/docs/telemetry.md+++ b/docs/telemetry.md@@ -1,18 +1,70 @@# Telemetry-OpenJarvis ships **anonymous usage telemetry** by default so the team can+OpenJarvis offers **optional external usage analytics** so the team cansee where the product breaks, what features people actually use, andhow to make it better. This page documents exactly what is and isn't-collected, where the data goes, and how to opt out.+collected, where the data goes, and how to enable or disable it.## TL;DR-- **On by default**, anonymous, no chat content.-- **Anonymous** — one random UUID per install, no email, no name, no IP.+- **Off until you choose to share.** In an interactive Bash installation or+ `jarvis init`, the dedicated prompt says: **Press Enter or Y to consent,+ or N to decline**. Enter at that prompt enables sharing; EOF, cancellation,+ or an unattended run does not.+- **Installation ID** — one random UUID links install and usage events.+ A random identifier is not a guarantee of legal anonymity.- **No chat content, ever.** Only counts, timings, and feature names.-- **Self-hosted backend** on the OpenJarvis team's PostHog instance —- data is not sold or shared with third parties.-- **365-day retention**, after which events are deleted automatically.+- **Destination** — the configured PostHog endpoint; see below.+- **Documented retention policy: 365 days.** Enforcement is configured on+ the receiving service, rather than in the client.++## Choosing whether to share++The Bash installer asks before its first analytics beacon. The answer is+saved as `[analytics] enabled` in `config.toml`, so the backend and frontend+use the same choice. Installer reruns save the latest answer even when+completed installation steps are skipped. No IP-based country lookup is used;+the same prompt applies everywhere.++For unattended installs, consent explicitly with `--analytics` or decline with+`--no-analytics`:++```bash+curl -fsSL https://open-jarvis.github.io/OpenJarvis/install.sh | bash -s -- --analytics+# Or: ... | bash -s -- --no-analytics+jarvis init --analytics+# Or: jarvis init --no-analytics+```++To change your choice later:++```bash+jarvis config set analytics.enabled false+# To enable: jarvis config set analytics.enabled true+```++Restart the server and reload the frontend after changing the setting.+An existing, explicitly configured `enabled = true` remains enabled until+changed. Configurations without an analytics setting now default to off.+Local performance telemetry and Savings Leaderboard participation remain+separate settings.++In the browser and desktop apps, the existing **Share Your Savings** dialog+offers explicit buttons without a checkbox. Displaying or closing the dialog+does not enable analytics. Confirm with **Enable analytics only** or+**Enable analytics & enter giveaway**, or use **Continue without sharing**+to turn analytics off. A saved UI choice takes effect immediately in both+the frontend and the running backend.++Joining the leaderboard requires a display name and contact email submitted+with savings to the configured Supabase leaderboard. Analytics preferences+send only the on/off choice to the local server; names and emails are not+attached to PostHog usage events by this form. The existing Mac mini+promotion concerns leaderboard participation, not analytics consent.+Use **Enter giveaway only** to join with analytics off; submitting the+name/email form with Enter also uses that option. Stopping leaderboard+updates and disabling usage analytics are separate actions. Stopping updates+does not delete previously submitted entries from Supabase.## What we collect@@ -49,15 +101,18 @@ type validator lives inThat file is the only place new events can be added — PR review isthe gate.-## What we never collect+## Backend safeguards-Hard guardrails, enforced by code:+Backend events use a property allowlist and value-level redaction:- **Chat content** — prompts, model outputs, system messages, tool args.- **File paths** — anything matching `~/`, `$HOME`, `/Users/<name>`, `/home/<name>`, `file://`.-- **Emails, names, phone numbers, addresses.**-- **IP addresses** (IPv4 + IPv6). PostHog's IP geo lookup is disabled server-side too.-- **MAC addresses, hardware serials, drive UUIDs.**+- **Emails** matching the redaction patterns.+- **IP addresses** (IPv4 + IPv6) in property values. IP geolocation is+ disabled in the backend SDK; the receiving infrastructure can still+ observe connection IPs.+- **MAC addresses** matching the redaction patterns. Event properties do+ not include hardware serials or drive UUIDs.- **Stack traces** — only error class enums.- **API keys, OAuth tokens, JWTs, bearer tokens, password assignments** —matched and dropped at value level.@@ -65,7 +120,7 @@ Hard guardrails, enforced by code:- **Lists, dicts, sets** — composite values are never sent so PII can'tsmuggle through inside containers.-Two independent filters run before every event leaves the machine:+Two independent filters run before backend event properties are sent:1. [`src/openjarvis/analytics/redaction.py`](../src/openjarvis/analytics/redaction.py) — value-level pattern matching (20+ regexes for PII).2. [`src/openjarvis/analytics/events.py`](../src/openjarvis/analytics/events.py) — structural allowlist (event name + property name + type validator).@@ -73,33 +128,39 @@ Two independent filters run before every event leaves the machine:Any failure at either layer → the event or property is silentlydropped. Tests covering the patterns: [`tests/analytics/test_redaction.py`](../tests/analytics/test_redaction.py).+The frontend sends directly through `posthog-js`. Its wrapper restricts event+names but does not use these backend property filters. The SDK can add browser+and page metadata, including the current URL. Autocapture, pageviews, and session+recording are disabled. Installer beacons use fixed fields assembled separately.+## Where the data goes-- **Today** (alpha): PostHog Cloud (US region) free tier. Disclosed- here for transparency.-- **Production target**: A self-hosted PostHog instance at- `analytics.openjarvis.ai`, Hetzner US-East. Single-tenant, operated- by the OpenJarvis team.+- The default runtime and Bash installer endpoint is+ `https://34.231.106.201.sslip.io`, configured for the OpenJarvis team's+ PostHog project.+- Runtime deployments can override `[analytics] host`; installer deployments+ can override `OPENJARVIS_ANALYTICS_HOST`. A configured endpoint alone does+ not establish its hosting provider or server logging settings.- **Never** sold, shared with advertisers, or used for anything otherthan improving OpenJarvis.## Retention-- Default retention: **365 days**, then events are deleted by PostHog- automatically.-- `jarvis analytics reset-id` lets you orphan all of your past events- by generating a fresh anonymous ID for future events.+- The documented retention policy is **365 days**. The receiving service+ must be configured to enforce it; client code does not delete remote events.+- Replacing the local installation ID changes the identifier used for future+ events. It does not delete previously collected events.## How identity works-A single UUID v4 is generated on first install and stored at+A single UUID v4 is generated when sharing is first enabled and stored at`~/.openjarvis/anon_id`. The install script, backend, and frontend all-read the same file so events across the full lifecycle tie to one-person — without us ever knowing who that person is.+read the same file so events across the lifecycle can be linked to the+same installation.Delete the file (`rm ~/.openjarvis/anon_id`) and a fresh UUID will be-generated next time the app runs. The previous UUID and its events-are then orphaned.+generated next time analytics are enabled. Events associated with the+previous UUID remain on the receiving service until deleted there.## For researchers and contributorsdiff --git a/frontend/src/App.tsx b/frontend/src/App.tsxindex 021cc59d..caf7f556 100644--- a/frontend/src/App.tsx+++ b/frontend/src/App.tsx@@ -16,6 +16,7 @@ import { fetchModels, fetchServerInfo, fetchSavings, submitSavings, isTauri } frimport { OptInModal } from './components/OptInModal';import { UpdateChecker } from './components/Desktop/UpdateChecker';import { track, hashId } from './lib/analytics';+import { offerAnalyticsChoice } from './lib/analytics-invitation';export default function App() {const [setupDone, setSetupDone] = useState(!isTauri());@@ -115,15 +116,15 @@ export default function App() {refresh();const interval = setInterval(refresh, 30000);return () => clearInterval(interval);- }, [optInEnabled, optInDisplayName, optInAnonId]); // eslint-disable-line react-hooks/exhaustive-deps+ }, [optInEnabled, optInDisplayName, optInEmail, optInAnonId]); // eslint-disable-line react-hooks/exhaustive-deps- // Show opt-in modal on first visit- useEffect(() => {- if (!optInModalSeen) {- setOptInModalOpen(true);- markOptInModalSeen();- }- }, []); // eslint-disable-line react-hooks/exhaustive-deps+ // Wait for desktop setup before fetching identity or marking the dialog seen.+ useEffect(() => offerAnalyticsChoice({+ ready: setupDone,+ sharingModalSeen: optInModalSeen,+ open: () => setOptInModalOpen(true),+ markSharingModalSeen: markOptInModalSeen,+ }), [setupDone, optInModalSeen, setOptInModalOpen, markOptInModalSeen]);// Fire model_changed when the user switches models. First mount is// not a "change" — only emit when both prev and current are real anddiff --git a/frontend/src/components/OptInModal.test.tsx b/frontend/src/components/OptInModal.test.tsxnew file mode 100644index 00000000..9510ff1f--- /dev/null+++ b/frontend/src/components/OptInModal.test.tsx@@ -0,0 +1,51 @@+import { renderToStaticMarkup } from 'react-dom/server';+import { beforeEach, describe, expect, it, vi } from 'vitest';++const mocks = vi.hoisted(() => ({+ joined: false,+ fetchIdentity: vi.fn(),+ savePreference: vi.fn(),+ setOptIn: vi.fn(),+}));+vi.mock('../lib/store', () => ({+ useAppStore: (selector: (state: Record<string, unknown>) => unknown) => selector({+ setOptIn: mocks.setOptIn,+ optInDisplayName: '',+ optInEmail: '',+ optInEnabled: mocks.joined,+ }),+}));+vi.mock('../lib/analytics', () => ({+ fetchAnalyticsIdentity: mocks.fetchIdentity,+ saveAnalyticsPreference: mocks.savePreference,+}));++import { OptInModal } from './OptInModal';++beforeEach(() => {+ mocks.joined = false;+ vi.clearAllMocks();+});++describe('existing sharing modal', () => {+ it('offers explicit analytics actions without a checkbox or side effects on render', () => {+ const html = renderToStaticMarkup(<OptInModal onClose={vi.fn()} />);+ expect(html).not.toContain('type="checkbox"');+ expect(html).toContain('Enable analytics & enter giveaway');+ expect(html).toContain('Enter giveaway only');+ expect(html).toContain('Enable analytics only');+ expect(html).toContain('Continue without sharing');+ expect(html).toContain('Win a Mac mini');+ expect(html).toContain('Your name and savings are public');+ expect(mocks.savePreference).not.toHaveBeenCalled();+ expect(mocks.setOptIn).not.toHaveBeenCalled();+ });++ it('allows existing leaderboard members to change analytics independently', () => {+ mocks.joined = true;+ const html = renderToStaticMarkup(<OptInModal onClose={vi.fn()} />);+ expect(html).toContain('Enable analytics');+ expect(html).toContain('Stop leaderboard updates');+ expect(mocks.savePreference).not.toHaveBeenCalled();+ });+});diff --git a/frontend/src/components/OptInModal.tsx b/frontend/src/components/OptInModal.tsxindex 25f61529..b1f4b638 100644--- a/frontend/src/components/OptInModal.tsx+++ b/frontend/src/components/OptInModal.tsx@@ -1,7 +1,8 @@-import { useState } from 'react';-import { Lock, Zap, DollarSign, Cpu, Trophy, X } from 'lucide-react';+import { useEffect, useRef, useState } from 'react';+import { Trophy, X } from 'lucide-react';import { useAppStore } from '../lib/store';import { isProfane } from '../lib/profanity';+import { fetchAnalyticsIdentity, saveAnalyticsPreference } from '../lib/analytics';interface OptInModalProps {onClose: () => void;@@ -10,253 +11,255 @@ interface OptInModalProps {export function OptInModal({ onClose }: OptInModalProps) {const setOptIn = useAppStore((s) => s.setOptIn);const optInDisplayName = useAppStore((s) => s.optInDisplayName);- const optInEnabled = useAppStore((s) => s.optInEnabled);-const optInEmail = useAppStore((s) => s.optInEmail);-+ const optInEnabled = useAppStore((s) => s.optInEnabled);const [name, setName] = useState(optInDisplayName);const [email, setEmail] = useState(optInEmail);- const [error, setError] = useState('');const [joined, setJoined] = useState(optInEnabled);+ const [analyticsEnabled, setAnalyticsEnabled] = useState(false);+ const [analyticsLoading, setAnalyticsLoading] = useState(true);+ const [saving, setSaving] = useState(false);+ const [error, setError] = useState('');+ const [status, setStatus] = useState('');+ const dialogRef = useRef<HTMLDivElement>(null);++ useEffect(() => {+ let active = true;+ fetchAnalyticsIdentity()+ .then((identity) => { if (active) setAnalyticsEnabled(identity.enabled); })+ .catch(() => {+ if (active) setError('Could not load analytics. You can retry saving your choice.');+ })+ .finally(() => { if (active) setAnalyticsLoading(false); });+ return () => { active = false; };+ }, []);- const handleJoin = () => {+ const handleJoin = async (enableAnalytics: boolean) => {+ if (saving || analyticsLoading) return;const trimmed = name.trim();const trimmedEmail = email.trim();- if (!trimmed) {- setError('Please enter a display name');- return;- }if (trimmed.length < 2 || trimmed.length > 30) {- setError('Name must be 2-30 characters');+ setError('Please enter a display name with 2–30 characters.');return;}if (isProfane(trimmed)) {- setError('Please choose a different name');+ setError('Please choose a different name.');return;}- if (!trimmedEmail || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmedEmail)) {- setError('Please enter a valid email address');+ if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmedEmail)) {+ setError('Please enter a valid email address.');return;}setError('');- setOptIn(true, trimmed, trimmedEmail);- setJoined(true);+ setSaving(true);+ try {+ await saveAnalyticsPreference(enableAnalytics);+ setAnalyticsEnabled(enableAnalytics);+ setOptIn(true, trimmed, trimmedEmail);+ setJoined(true);+ setStatus(enableAnalytics ? 'Usage analytics enabled.' : 'Usage analytics are off.');+ } catch (cause) {+ setError(cause instanceof Error ? cause.message : 'Could not save your sharing choice.');+ } finally {+ setSaving(false);+ }};- const handleOptOut = () => {- setOptIn(false, '', '');- setJoined(false);- onClose();+ const handleAnalytics = async (enabled: boolean, close: boolean) => {+ if (saving || analyticsLoading) return;+ setError('');+ setSaving(true);+ try {+ await saveAnalyticsPreference(enabled);+ setAnalyticsEnabled(enabled);+ setStatus(enabled ? 'Usage analytics enabled.' : 'Usage analytics are off.');+ if (close) onClose();+ } catch (cause) {+ setError(cause instanceof Error ? cause.message : 'Could not save your analytics choice.');+ } finally {+ setSaving(false);+ }};- const features = [- { icon: Lock, label: 'Private — no IP or hardware info shared publicly' },- { icon: Zap, label: 'Track energy savings from local inference' },- { icon: DollarSign, label: 'See how much you save vs cloud providers' },- { icon: Cpu, label: 'Measure FLOPs and request efficiency' },- ];+ const busy = saving || analyticsLoading;+ const primaryStyle = {+ background: 'var(--color-accent)',+ color: 'var(--color-on-accent)',+ };+ const inputStyle = {+ background: 'var(--color-bg-secondary)',+ border: '1px solid var(--color-border)',+ color: 'var(--color-text)',+ };+ const buttonClass = 'min-h-10 rounded-xl px-3 py-2 text-sm font-medium cursor-pointer disabled:opacity-50';return (<divclassName="fixed inset-0 z-50 flex items-center justify-center"style={{ backdropFilter: 'blur(8px)', background: 'rgba(0,0,0,0.4)' }}- onClick={(e) => {- if (e.target === e.currentTarget) onClose();- }}+ onClick={(event) => { if (event.target === event.currentTarget && !saving) onClose(); }}><div- className="relative w-full max-w-md mx-4 overflow-hidden"+ ref={dialogRef}+ role="dialog"+ aria-modal="true"+ aria-labelledby="sharing-modal-title"+ className="relative w-full max-w-md mx-4 max-h-[90dvh] overflow-y-auto rounded-2xl p-4 sm:p-6"style={{background: 'var(--color-bg)',- borderRadius: 16,- boxShadow: '0 24px 80px rgba(0,0,0,0.25)',border: '1px solid var(--color-border)',+ boxShadow: '0 24px 80px rgba(0,0,0,0.25)',+ }}+ onKeyDown={(event) => {+ if (event.key === 'Escape' && !saving) onClose();+ if (event.key !== 'Tab') return;+ const controls = dialogRef.current?.querySelectorAll<HTMLElement>(+ 'button:not([disabled]), input:not([disabled]), a[href]',+ );+ if (!controls?.length) return;+ const first = controls[0];+ const last = controls[controls.length - 1];+ if (event.shiftKey && document.activeElement === first) {+ event.preventDefault();+ last.focus();+ } else if (!event.shiftKey && document.activeElement === last) {+ event.preventDefault();+ first.focus();+ }}}>- {/* Close button */}<button+ type="button"+ aria-label="Close sharing choices"onClick={onClose}- className="absolute top-3 right-3 p-1.5 rounded-lg transition-colors cursor-pointer"+ disabled={saving}+ className="absolute right-3 top-3 p-1.5 rounded-lg cursor-pointer"style={{ color: 'var(--color-text-tertiary)' }}- onMouseEnter={(e) => (e.currentTarget.style.background = 'var(--color-bg-secondary)')}- onMouseLeave={(e) => (e.currentTarget.style.background = 'transparent')}- >- <X size={16} />- </button>+ ><X size={16} /></button>- <div className="px-8 pt-8 pb-6">- {/* Trophy icon */}+ <div className="flex items-center gap-3 pr-6 mb-2"><div- className="flex items-center justify-center w-14 h-14 rounded-2xl mb-5 mx-auto"- style={{- background: 'linear-gradient(135deg, var(--color-accent), var(--color-accent-purple))',- }}- >- <Trophy size={28} color="white" />- </div>-- {/* Heading */}- <h2- className="text-xl font-semibold text-center mb-2"- style={{ color: 'var(--color-text)' }}- >- Share Your Savings+ className="flex items-center justify-center w-9 h-9 rounded-xl shrink-0"+ style={{ background: 'var(--color-accent-subtle)', color: 'var(--color-accent)' }}+ ><Trophy size={20} /></div>+ <h2 id="sharing-modal-title" className="text-xl font-semibold" style={{ color: 'var(--color-text)' }}>+ Win a Mac mini</h2>- <p- className="text-sm text-center mb-6 leading-relaxed"- style={{ color: 'var(--color-text-secondary)' }}- >- Opt in to privately share your energy, dollar, FLOPs, and request- savings for the chance to win a Mac Mini!- </p>+ </div>+ <p className="text-sm leading-relaxed mb-3" style={{ color: 'var(--color-text-secondary)' }}>+ Share your savings on the leaderboard for a chance to win a Mac mini.+ </p>- {/* Feature list */}- <div className="flex flex-col gap-3 mb-6">- {features.map(({ icon: Icon, label }) => (- <div key={label} className="flex items-center gap-3">- <div- className="flex items-center justify-center w-8 h-8 rounded-full shrink-0"- style={{ background: 'var(--color-bg-secondary)' }}- >- <Icon size={14} style={{ color: 'var(--color-accent)' }} />- </div>- <span- className="text-sm"- style={{ color: 'var(--color-text-secondary)' }}- >- {label}- </span>- </div>- ))}- </div>+ {error && <p role="alert" className="text-xs mb-3" style={{ color: 'var(--color-error)' }}>{error}</p>}+ {status && <p role="status" className="text-xs mb-3" style={{ color: 'var(--color-text-secondary)' }}>{status}</p>}- {/* Success state */}- {joined ? (- <div className="text-center">- <div- className="inline-flex items-center gap-2 px-4 py-2 rounded-full mb-4"- style={{- background: 'var(--color-accent-subtle)',- color: 'var(--color-accent)',- }}- >- <Trophy size={14} />- <span className="text-sm font-medium">- You're on the leaderboard!- </span>- </div>- <div className="flex gap-2 justify-center">- <button- onClick={onClose}- className="px-5 py-2.5 rounded-xl text-sm font-medium transition-colors cursor-pointer"- style={{- background: 'var(--color-accent)',- color: 'var(--color-on-accent)',- }}- >- Done- </button>- <button- onClick={handleOptOut}- className="px-5 py-2.5 rounded-xl text-sm transition-colors cursor-pointer"- style={{ color: 'var(--color-text-tertiary)' }}- >- Opt Out- </button>- </div>+ {!joined && (+ <form+ id="sharing-entry-form"+ onSubmit={(event) => { event.preventDefault(); void handleJoin(false); }}+ className="grid grid-cols-1 sm:grid-cols-2 gap-3 mb-3"+ >+ <div>+ <label htmlFor="sharing-display-name" className="block text-xs mb-1.5" style={{ color: 'var(--color-text-secondary)' }}>+ Display name (public)+ </label>+ <input+ id="sharing-display-name"+ type="text"+ value={name}+ disabled={saving}+ maxLength={30}+ onChange={(event) => { setName(event.target.value); setError(''); }}+ placeholder="Your leaderboard name"+ className="w-full rounded-lg px-3 py-2 text-base sm:text-sm"+ style={inputStyle}+ autoFocus+ />+ </div>+ <div>+ <label htmlFor="sharing-contact-email" className="block text-xs mb-1.5" style={{ color: 'var(--color-text-secondary)' }}>+ Email (not displayed)+ </label>+ <input+ id="sharing-contact-email"+ type="email"+ value={email}+ disabled={saving}+ onChange={(event) => { setEmail(event.target.value); setError(''); }}+ placeholder="[email protected]"+ className="w-full rounded-lg px-3 py-2 text-base sm:text-sm"+ style={inputStyle}+ /></div>- ) : (- <>- {/* Name input */}- <div className="mb-3">- <label- className="block text-xs font-medium mb-1.5"- style={{ color: 'var(--color-text-secondary)' }}- >- Display Name- </label>- <input- type="text"- value={name}- onChange={(e) => {- setName(e.target.value);- if (error) setError('');- }}- onKeyDown={(e) => {- if (e.key === 'Enter') handleJoin();- }}- placeholder="Choose a name for the leaderboard"- maxLength={30}- className="w-full px-3 py-2.5 rounded-xl text-sm outline-none transition-colors"- style={{- background: 'var(--color-bg-secondary)',- border: '1.5px solid var(--color-border)',- color: 'var(--color-text)',- }}- autoFocus- />- </div>+ </form>+ )}- {/* Email input */}- <div className="mb-4">- <label- className="block text-xs font-medium mb-1.5"- style={{ color: 'var(--color-text-secondary)' }}- >- Email <span style={{ color: 'var(--color-error)' }}>*</span>- <span className="font-normal ml-1" style={{ color: 'var(--color-text-tertiary)' }}>(never shown publicly)</span>- </label>- <input- type="email"- value={email}- onChange={(e) => {- setEmail(e.target.value);- if (error) setError('');- }}- onKeyDown={(e) => {- if (e.key === 'Enter') handleJoin();- }}- placeholder="[email protected]"- className="w-full px-3 py-2.5 rounded-xl text-sm outline-none transition-colors"- style={{- background: 'var(--color-bg-secondary)',- border: '1.5px solid var(--color-border)',- color: 'var(--color-text)',- }}- />- {error && (- <p- className="text-xs mt-1"- style={{ color: 'var(--color-error)' }}- >- {error}- </p>- )}- </div>+ <div className="text-xs leading-relaxed mb-3" style={{ color: 'var(--color-text-secondary)' }}>+ <p className="mb-1">+ Optional analytics sends usage counts, timings, errors, environment metadata,+ and a random ID to PostHog.+ </p>+ <p>+ Leaderboard entries send your name, email, and savings to Supabase.+ Your name and savings are public; email is not displayed.+ </p>+ <a+ href="https://open-jarvis.github.io/OpenJarvis/telemetry/"+ target="_blank"+ rel="noreferrer"+ className="inline-block underline mt-1"+ >Privacy details</a>+ </div>- {/* Buttons */}+ {joined ? (+ <div className="space-y-2">+ <p className="text-sm mb-3" style={{ color: 'var(--color-text)' }}>+ Leaderboard sharing enabled · Analytics {analyticsEnabled ? 'on' : 'off'}+ </p>+ <button+ type="button"+ onClick={() => void handleAnalytics(!analyticsEnabled, false)}+ disabled={busy}+ className={buttonClass + ' w-full'}+ style={primaryStyle}+ >{saving ? 'Saving…' : analyticsEnabled ? 'Disable analytics' : 'Enable analytics'}</button>+ <div className="grid grid-cols-2 gap-2">+ <button type="button" onClick={onClose} disabled={saving} className={buttonClass} style={{ color: 'var(--color-text)' }}>Done</button><button- onClick={handleJoin}- className="w-full py-2.5 rounded-xl text-sm font-medium transition-all cursor-pointer"- style={{- background: 'var(--color-accent)',- color: 'var(--color-on-accent)',- }}- onMouseEnter={(e) => (e.currentTarget.style.opacity = '0.9')}- onMouseLeave={(e) => (e.currentTarget.style.opacity = '1')}- >- Join Leaderboard+ type="button"+ onClick={() => { setOptIn(false, '', ''); setJoined(false); onClose(); }}+ disabled={saving}+ className={buttonClass}+ style={{ color: 'var(--color-text-secondary)' }}+ >Stop leaderboard updates</button>+ </div>+ </div>+ ) : (+ <div className="space-y-2">+ <button+ type="button"+ onClick={() => void handleJoin(true)}+ disabled={busy}+ className={buttonClass + ' w-full'}+ style={primaryStyle}+ >{saving ? 'Saving…' : 'Enable analytics & enter giveaway'}</button>+ <div className="grid grid-cols-2 gap-2">+ <button type="submit" form="sharing-entry-form" disabled={busy} className={buttonClass} style={{ color: 'var(--color-text)' }}>+ Enter giveaway only</button>- <button- onClick={onClose}- className="w-full py-2 text-sm transition-colors cursor-pointer mt-2"- style={{ color: 'var(--color-text-tertiary)' }}- >- No Thanks+ <button type="button" onClick={() => void handleAnalytics(true, true)} disabled={busy} className={buttonClass} style={{ color: 'var(--color-text)' }}>+ Enable analytics only</button>- </>- )}- </div>+ </div>+ <button+ type="button"+ onClick={() => void handleAnalytics(false, true)}+ disabled={busy}+ className={buttonClass + ' w-full border'}+ style={{ color: 'var(--color-text)', borderColor: 'var(--color-border)' }}+ >Continue without sharing</button>+ </div>+ )}</div></div>);diff --git a/frontend/src/lib/analytics-invitation.test.ts b/frontend/src/lib/analytics-invitation.test.tsnew file mode 100644index 00000000..385c115e--- /dev/null+++ b/frontend/src/lib/analytics-invitation.test.ts@@ -0,0 +1,87 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++const mocks = vi.hoisted(() => ({ fetchIdentity: vi.fn() }));+vi.mock('./analytics', () => ({ fetchAnalyticsIdentity: mocks.fetchIdentity }));+import { offerAnalyticsChoice } from './analytics-invitation';++const seenKey = 'openjarvis-analytics-choice-seen';+let stored: Map<string, string>;++beforeEach(() => {+ vi.resetAllMocks();+ stored = new Map();+ vi.stubGlobal('localStorage', {+ getItem: (key: string) => stored.get(key) ?? null,+ setItem: (key: string, value: string) => stored.set(key, value),+ });+});+afterEach(() => vi.unstubAllGlobals());++const invitation = (sharingModalSeen: boolean) => ({+ ready: false,+ sharingModalSeen,+ open: vi.fn(),+ markSharingModalSeen: vi.fn(),+});++describe('analytics invitation after setup', () => {+ it('does not consume a fresh user invitation while setup is incomplete', () => {+ const request = invitation(false);+ offerAnalyticsChoice(request);+ expect(request.open).not.toHaveBeenCalled();+ expect(request.markSharingModalSeen).not.toHaveBeenCalled();+ expect(stored.has(seenKey)).toBe(false);+ expect(mocks.fetchIdentity).not.toHaveBeenCalled();++ offerAnalyticsChoice({ ...request, ready: true });+ expect(request.open).toHaveBeenCalledOnce();+ expect(request.markSharingModalSeen).toHaveBeenCalledOnce();+ expect(stored.get(seenKey)).toBe('true');+ });++ it('waits for the desktop backend before checking an existing user choice', async () => {+ const request = invitation(true);+ offerAnalyticsChoice(request);+ expect(mocks.fetchIdentity).not.toHaveBeenCalled();+ expect(stored.has(seenKey)).toBe(false);++ mocks.fetchIdentity.mockResolvedValue({ choice_recorded: false });+ offerAnalyticsChoice({ ...request, ready: true });+ await vi.waitFor(() => expect(request.open).toHaveBeenCalledOnce());+ expect(stored.get(seenKey)).toBe('true');+ });++ it('does not reopen for an existing user who already recorded a choice', async () => {+ const request = invitation(true);+ mocks.fetchIdentity.mockResolvedValue({ choice_recorded: true });+ offerAnalyticsChoice({ ...request, ready: true });+ await vi.waitFor(() => expect(stored.get(seenKey)).toBe('true'));+ expect(request.open).not.toHaveBeenCalled();+ });++ it('does not consume the invitation when its effect is cancelled', async () => {+ const request = invitation(true);+ let finish: (identity: { choice_recorded: boolean }) => void = () => {};+ mocks.fetchIdentity.mockReturnValue(new Promise((resolve) => { finish = resolve; }));+ const cancel = offerAnalyticsChoice({ ...request, ready: true });+ cancel?.();+ finish({ choice_recorded: false });+ await Promise.resolve();+ expect(request.open).not.toHaveBeenCalled();+ expect(stored.has(seenKey)).toBe(false);+ });++ it('keeps a failed lookup eligible for the next ready launch', async () => {+ const request = invitation(true);+ mocks.fetchIdentity.mockRejectedValueOnce(new Error('Server unavailable'));+ offerAnalyticsChoice({ ...request, ready: true });+ await Promise.resolve();+ await Promise.resolve();+ expect(stored.has(seenKey)).toBe(false);++ mocks.fetchIdentity.mockResolvedValue({ choice_recorded: false });+ offerAnalyticsChoice({ ...request, ready: true });+ await vi.waitFor(() => expect(request.open).toHaveBeenCalledOnce());+ expect(stored.get(seenKey)).toBe('true');+ });+});diff --git a/frontend/src/lib/analytics-invitation.ts b/frontend/src/lib/analytics-invitation.tsnew file mode 100644index 00000000..75ccee14--- /dev/null+++ b/frontend/src/lib/analytics-invitation.ts@@ -0,0 +1,33 @@+import { fetchAnalyticsIdentity } from './analytics';++const ANALYTICS_SEEN_KEY = 'openjarvis-analytics-choice-seen';++interface AnalyticsInvitation {+ ready: boolean;+ sharingModalSeen: boolean;+ open: () => void;+ markSharingModalSeen: () => void;+}++/** Offer the sharing dialog only after the app can actually display it. */+export function offerAnalyticsChoice({+ ready, sharingModalSeen, open, markSharingModalSeen,+}: AnalyticsInvitation): (() => void) | undefined {+ if (!ready) return;+ if (!sharingModalSeen) {+ open();+ markSharingModalSeen();+ localStorage.setItem(ANALYTICS_SEEN_KEY, 'true');+ return;+ }+ if (localStorage.getItem(ANALYTICS_SEEN_KEY)) return;++ // Existing users may have seen the older leaderboard-only dialog.+ let active = true;+ fetchAnalyticsIdentity().then((identity) => {+ if (!active) return;+ if (!identity.choice_recorded) open();+ localStorage.setItem(ANALYTICS_SEEN_KEY, 'true');+ }).catch(() => { /* Retry next launch if the server was unavailable. */ });+ return () => { active = false; };+}diff --git a/frontend/src/lib/analytics.test.ts b/frontend/src/lib/analytics.test.tsnew file mode 100644index 00000000..9444bbc9--- /dev/null+++ b/frontend/src/lib/analytics.test.ts@@ -0,0 +1,154 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++const mocks = vi.hoisted(() => ({+ apiFetch: vi.fn(),+ init: vi.fn(),+ capture: vi.fn(),+ identify: vi.fn(),+ register: vi.fn(),+ opt_in_capturing: vi.fn(),+ opt_out_capturing: vi.fn(),+}));+vi.mock('./api', () => ({ apiFetch: mocks.apiFetch }));+vi.mock('posthog-js', () => ({+ default: {+ init: mocks.init,+ capture: mocks.capture,+ identify: mocks.identify,+ register: mocks.register,+ opt_in_capturing: mocks.opt_in_capturing,+ opt_out_capturing: mocks.opt_out_capturing,+ },+}));++const identity = (enabled: boolean) => ({+ enabled, key: enabled ? 'test-key' : '', anon_id: enabled ? 'test-id' : '',+ host: 'https://analytics.example.org', choice_recorded: true,+});+const response = (enabled: boolean) => ({+ ok: true, json: async () => identity(enabled),+});++beforeEach(() => {+ vi.resetModules();+ vi.clearAllMocks();+ let sdkInitialized = false;+ mocks.init.mockImplementation((_key, config) => {+ // Match PostHog: later init() calls do not call loaded again.+ if (sdkInitialized) return;+ sdkInitialized = true;+ config.loaded(mocks);+ });+});++describe('analytics sharing choice', () => {+ it('does not initialize or send when the backend reports sharing off', async () => {+ mocks.apiFetch.mockResolvedValue(response(false));+ const analytics = await import('./analytics');+ await analytics.initAnalytics();+ analytics.track('app_opened');+ expect(mocks.init).not.toHaveBeenCalled();+ expect(mocks.capture).not.toHaveBeenCalled();+ });++ it('saves only the boolean choice and enables both initialization and capture', async () => {+ mocks.apiFetch.mockResolvedValue(response(true));+ const analytics = await import('./analytics');+ await analytics.saveAnalyticsPreference(true);+ expect(mocks.apiFetch).toHaveBeenCalledWith('/v1/analytics/preference', {+ method: 'POST', headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ enabled: true }),+ });+ expect(mocks.opt_in_capturing).toHaveBeenCalledWith({ captureEventName: false });+ analytics.track('app_opened');+ expect(mocks.capture).toHaveBeenCalledWith('app_opened', {});+ });++ it('can enable after initial startup was disabled, then stop capture immediately', async () => {+ mocks.apiFetch.mockResolvedValue(response(false));+ const analytics = await import('./analytics');+ await analytics.initAnalytics();+ mocks.apiFetch.mockResolvedValue(response(true));+ await analytics.saveAnalyticsPreference(true);+ expect(analytics.isAnalyticsEnabled()).toBe(true);+ mocks.apiFetch.mockResolvedValue(response(false));+ await analytics.saveAnalyticsPreference(false);+ expect(analytics.isAnalyticsEnabled()).toBe(false);+ expect(analytics.getAnonId()).toBe('');+ analytics.track('app_opened');+ expect(mocks.capture).not.toHaveBeenCalled();+ expect(mocks.opt_out_capturing).toHaveBeenCalledOnce();+ });++ it('resumes browser capture when enabling again after a decline', async () => {+ mocks.apiFetch.mockResolvedValue(response(true));+ const analytics = await import('./analytics');+ await analytics.saveAnalyticsPreference(true);+ mocks.apiFetch.mockResolvedValue(response(false));+ await analytics.saveAnalyticsPreference(false);+ mocks.apiFetch.mockResolvedValue(response(true));+ await analytics.saveAnalyticsPreference(true);++ expect(mocks.init).toHaveBeenCalledOnce();+ expect(mocks.opt_in_capturing).toHaveBeenCalledTimes(2);+ expect(analytics.isAnalyticsEnabled()).toBe(true);+ expect(analytics.getAnonId()).toBe('test-id');+ analytics.track('app_opened');+ expect(mocks.capture).toHaveBeenCalledWith('app_opened', {});+ });++ it('does not resume on a delayed SDK load after decline, but allows a later opt-in', async () => {+ let finishLoading: () => void = () => {};+ mocks.init.mockImplementationOnce((_key, config) => { finishLoading = config.loaded; });+ mocks.apiFetch.mockResolvedValue(response(true));+ const analytics = await import('./analytics');+ const startup = analytics.initAnalytics();+ await vi.waitFor(() => expect(mocks.init).toHaveBeenCalledOnce());+ mocks.apiFetch.mockResolvedValue(response(false));+ await analytics.saveAnalyticsPreference(false);+ finishLoading();+ await startup;+ expect(analytics.isAnalyticsEnabled()).toBe(false);+ expect(mocks.opt_in_capturing).not.toHaveBeenCalled();++ mocks.apiFetch.mockResolvedValue(response(true));+ await analytics.saveAnalyticsPreference(true);+ expect(mocks.init).toHaveBeenCalledOnce();+ expect(analytics.isAnalyticsEnabled()).toBe(true);+ });++ it('does not enable capture when saving the choice fails', async () => {+ mocks.apiFetch.mockResolvedValue({ ok: false });+ const analytics = await import('./analytics');+ await expect(analytics.saveAnalyticsPreference(true)).rejects.toThrow('Could not save');+ expect(mocks.init).not.toHaveBeenCalled();+ expect(analytics.isAnalyticsEnabled()).toBe(false);+ });++ it('stops browser capture while a decline is still being saved', async () => {+ mocks.apiFetch.mockResolvedValue(response(true));+ const analytics = await import('./analytics');+ await analytics.initAnalytics();+ let finishSave: (value: ReturnType<typeof response>) => void = () => {};+ mocks.apiFetch.mockImplementationOnce(() => new Promise((resolve) => { finishSave = resolve; }));+ const saving = analytics.saveAnalyticsPreference(false);+ analytics.track('app_opened');+ expect(analytics.isAnalyticsEnabled()).toBe(false);+ expect(mocks.capture).not.toHaveBeenCalled();+ finishSave(response(false));+ await saving;+ });++ it('ignores a stale startup response after an explicit decline', async () => {+ let finishStartup: (value: ReturnType<typeof response>) => void = () => {};+ mocks.apiFetch.mockImplementationOnce(() => new Promise((resolve) => { finishStartup = resolve; }));+ const analytics = await import('./analytics');+ const startup = analytics.initAnalytics();+ mocks.apiFetch.mockResolvedValue(response(false));+ await analytics.saveAnalyticsPreference(false);+ finishStartup(response(true));+ await startup;+ expect(mocks.init).not.toHaveBeenCalled();+ expect(analytics.isAnalyticsEnabled()).toBe(false);+ });+});diff --git a/frontend/src/lib/analytics.ts b/frontend/src/lib/analytics.tsindex 9eeab7ca..0cbaa9cd 100644--- a/frontend/src/lib/analytics.ts+++ b/frontend/src/lib/analytics.ts@@ -18,7 +18,7 @@*/import posthog from 'posthog-js';-import { getBase } from './api';+import { apiFetch } from './api';/*** Mirror of the Python event catalog (src/openjarvis/analytics/events.py@@ -51,58 +51,92 @@ const KNOWN_EVENTS = new Set<string>([// TODO: wire to Vite define() so this comes from package.json at build time.const APP_VERSION = '0.1.0';-interface AnalyticsIdentity {+export interface AnalyticsIdentity {enabled: boolean;anon_id: string;host: string;key: string;+ choice_recorded?: boolean;}let initialized = false;let enabledState = false;let cachedAnonId = '';+let generation = 0;+let sdkReady: Promise<void> | null = null;++export async function fetchAnalyticsIdentity(): Promise<AnalyticsIdentity> {+ const response = await apiFetch('/v1/analytics/identity');+ if (!response.ok) throw new Error('Could not load the analytics setting.');+ return response.json();+}++export async function saveAnalyticsPreference(enabled: boolean): Promise<void> {+ if (!enabled) {+ // Stop the browser sender before waiting for the persistence request.+ generation += 1;+ initialized = false;+ enabledState = false;+ cachedAnonId = '';+ try { posthog.opt_out_capturing(); } catch { /* never break the UI */ }+ }+ const response = await apiFetch('/v1/analytics/preference', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ enabled }),+ });+ if (!response.ok) throw new Error('Could not save your analytics choice. Please try again.');+ const identity: AnalyticsIdentity = await response.json();+ if (identity.enabled !== enabled) throw new Error('Analytics choice was not saved.');+ if (enabled) {+ await initAnalytics(true);+ }+}/*** Fetch identity from the backend and initialize the SDK.* Idempotent — safe to call multiple times.*/-export async function initAnalytics(): Promise<void> {- if (initialized) return;+export async function initAnalytics(force = false): Promise<void> {+ if (initialized && !force) return;+ const currentGeneration = ++generation;initialized = true; // claim the slot even on failure pathstry {- const base = getBase();- if (!base) return;-- const resp = await fetch(`${base}/v1/analytics/identity`);- if (!resp.ok) return;-- const identity: AnalyticsIdentity = await resp.json();+ const identity = await fetchAnalyticsIdentity();+ if (currentGeneration !== generation) return;if (!identity.enabled || !identity.key || !identity.anon_id) {return;}cachedAnonId = identity.anon_id;- posthog.init(identity.key, {- api_host: identity.host,- bootstrap: { distinctID: identity.anon_id },- // No surprise data collection — only events we call out by name.- autocapture: false,- capture_pageview: false,- capture_pageleave: false,- disable_session_recording: true,- // No /decide call for feature flags (saves a request, we don't use them yet).- advanced_disable_decide: true,- // No PostHog person profiles — we're not making accounts, just sending events.- person_profiles: 'never',- // Don't try to load IP geolocation; backend disables this too.- ip: false,- // Best-effort sending only.- loaded: () => {- enabledState = true;- },- });+ // PostHog ignores repeated init() calls. Share the first initialization,+ // then explicitly resume capture for every later opt-in.+ if (!sdkReady) {+ sdkReady = new Promise<void>((resolve) => {+ posthog.init(identity.key, {+ api_host: identity.host,+ bootstrap: { distinctID: identity.anon_id },+ // No surprise data collection — only events we call out by name.+ autocapture: false,+ capture_pageview: false,+ capture_pageleave: false,+ disable_session_recording: true,+ // No /decide call for feature flags (saves a request, we don't use them yet).+ advanced_disable_decide: true,+ // No PostHog person profiles — we're not making accounts, just sending events.+ person_profiles: 'never',+ // Don't try to load IP geolocation; backend disables this too.+ ip: false,+ loaded: () => resolve(),+ });+ });+ }+ await sdkReady;+ if (currentGeneration !== generation) return;+ posthog.opt_in_capturing({ captureEventName: false });+ enabledState = true;// Set distinct_id explicitly in case bootstrap raced.posthog.identify(identity.anon_id);diff --git a/scripts/install/install.sh b/scripts/install/install.shindex e20d9105..33e6acd7 100755--- a/scripts/install/install.sh+++ b/scripts/install/install.sh@@ -8,6 +8,8 @@# --no-bg-orchestrator Skip the detached background orchestrator# --minimal Skip foreground model pull (no `qwen3.5:2b`)# --force Re-run all steps even if state file says done+# --analytics Consent to install and runtime usage analytics+# --no-analytics Decline analytics without an interactive prompt## Environment overrides:# OPENJARVIS_HOME Install dir (default: $HOME/.openjarvis)@@ -20,11 +22,20 @@ set -euo pipefailSKIP_BG=0MINIMAL=0FORCE=0+ANALYTICS_ENABLED=0+ANALYTICS_CHOICE=""for arg in "$@"; docase "$arg" in--no-bg-orchestrator) SKIP_BG=1 ;;--minimal) MINIMAL=1 ;;--force) FORCE=1 ;;+ --analytics|--no-analytics)+ if [[ -n "$ANALYTICS_CHOICE" && "$ANALYTICS_CHOICE" != "$arg" ]]; then+ echo "install.sh: --analytics and --no-analytics cannot be combined" >&2+ exit 2+ fi+ ANALYTICS_CHOICE="$arg"+ ;;*) echo "install.sh: unknown arg: $arg" >&2; exit 2 ;;esacdone@@ -236,12 +247,12 @@ elif [[ -f /proc/sys/kernel/osrelease ]] && grep -qi "microsoft" /proc/sys/kerneWSL=1fi-# ---- analytics beacon (anonymized install funnel) ----+# ---- analytics consent and install beacons ----## Posts a small JSON event to PostHog at each install stage so the# OpenJarvis team can see where users drop off during install.-# No content, no IPs (handled by PostHog disable_geoip on server),-# no hardware identifiers — just OS, arch, elapsed time, and stage name.+# Install payloads contain OS, arch, elapsed time, stage name, and a+# random installation ID. Beacons are disabled until consent is given.#ANALYTICS_HOST="${OPENJARVIS_ANALYTICS_HOST:-https://34.231.106.201.sslip.io}"ANALYTICS_KEY="${OPENJARVIS_ANALYTICS_KEY:-phc_ysKu72QaxzYNmDpHFcesD2ZZAe68zkdWJEKoYYkc5e3n}"@@ -250,7 +261,90 @@ INSTALL_START_EPOCH="$(date +%s)"CURRENT_STAGE=""analytics_enabled() {- return 0+ [[ "$ANALYTICS_ENABLED" -eq 1 ]]+}++choose_analytics() {+ # The usual curl | bash entry point occupies stdin. Read a real+ # controlling terminal instead; EOF/unattended runs never consent.+ case "$ANALYTICS_CHOICE" in+ --analytics) ANALYTICS_ENABLED=1; return ;;+ --no-analytics) return ;;+ esac+ if [[ ! -t 2 ]] || ! { exec 3<> /dev/tty; } 2>/dev/null; then+ echo "Analytics are off (no interactive terminal). Use --analytics to opt in." >&2+ return+ fi++ cat >&3 <<'EOF'++Help improve OpenJarvis by sharing optional install and usage statistics.+Data includes installation and feature usage, timings, token counts, error+categories, environment metadata, and a random installation ID.+Recipient: the OpenJarvis team's PostHog analytics endpoint.+Privacy details: https://open-jarvis.github.io/OpenJarvis/telemetry/+You can disable sharing later: jarvis config set analytics.enabled false+(then restart the server and reload the app).+Declining does not change OpenJarvis functionality.+EOF+ printf 'Analytics destination: %s\n' "$ANALYTICS_HOST" >&3+ local answer+ while true; do+ printf 'Press Enter or Y to consent, or N to decline: ' >&3+ if ! IFS= read -r answer <&3; then+ printf '\nAnalytics are off (no answer received).\n' >&3+ break+ fi+ case "$answer" in+ ""|y|Y|yes|YES|Yes)+ ANALYTICS_ENABLED=1+ ANALYTICS_CHOICE="--analytics"+ break+ ;;+ n|N|no|NO|No) ANALYTICS_CHOICE="--no-analytics"; break ;;+ *) printf 'Please press Enter, type Y, or type N.\n' >&3 ;;+ esac+ done+ exec 3>&-+}++persist_analytics_choice() {+ # Always run, including when the installer skips write_config on a+ # rerun. Use the installed TOML editor directly: an older jarvis CLI+ # rejects the new choice_recorded key before it can save the choice.+ local enabled=false+ if analytics_enabled; then+ enabled=true+ fi+ "$VENV_DIR/bin/python" - "$enabled" "$ANALYTICS_CHOICE" <<'PYEOF'+import os+import sys+import tempfile+from pathlib import Path++import tomlkit++from openjarvis.core.config import DEFAULT_CONFIG_PATH++path = Path(os.environ.get("OPENJARVIS_CONFIG") or DEFAULT_CONFIG_PATH).expanduser().resolve()+path.parent.mkdir(parents=True, exist_ok=True)+doc = tomlkit.parse(path.read_text(encoding="utf-8")) if path.exists() else tomlkit.document()+if "analytics" not in doc:+ doc.add("analytics", tomlkit.table())+doc["analytics"]["enabled"] = sys.argv[1] == "true"+if sys.argv[2]:+ doc["analytics"]["choice_recorded"] = True++temporary = None+try:+ with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=path.parent, delete=False) as handle:+ temporary = Path(handle.name)+ handle.write(tomlkit.dumps(doc))+ temporary.replace(path)+finally:+ if temporary is not None:+ temporary.unlink(missing_ok=True)+PYEOF}detect_os() {@@ -676,6 +770,7 @@ echo " install dir: $OPENJARVIS_HOME"echo " WSL2: $WSL"echo+choose_analyticsbeacon "install_started"step install_uv "Install uv" install_uv@@ -687,6 +782,7 @@ step install_ollama "Install Ollama" install_ollamastep start_ollama "Start Ollama daemon" start_ollamastep pull_default_model "Pull qwen3.5:2b" pull_default_modelstep write_config "Write config.toml" write_config+persist_analytics_choicestep install_symlinks "Install symlinks" install_symlinksstep ensure_path "Ensure PATH" ensure_pathstep detach_bg_orchestrator "Detach background work" detach_bg_orchestratordiff --git a/src/openjarvis/analytics/client.py b/src/openjarvis/analytics/client.pyindex 0189262d..bea5fa71 100644--- a/src/openjarvis/analytics/client.py+++ b/src/openjarvis/analytics/client.py@@ -37,10 +37,14 @@ class AnalyticsClient:def __init__(self, config: AnalyticsConfig, anon_id: str | None = None) -> None:self.config = config- self.anon_id = anon_id or get_or_create_anon_id(config.anon_id_path)+ self._enabled = is_analytics_enabled(config)+ self.anon_id = (+ anon_id or get_or_create_anon_id(config.anon_id_path)+ if self._enabled+ else ""+ )self._lock = threading.Lock()self._posthog: Any = None- self._enabled = is_analytics_enabled(config)if self._enabled:self._init_sdk()@@ -110,18 +114,33 @@ class AnalyticsClient:except Exception:pass- def shutdown(self) -> None:- """Flush and close the SDK. Call once on process exit."""+ def disable(self) -> None:+ """Stop accepting events without waiting for network delivery."""with self._lock:- if self._posthog is None:- return+ self._enabled = False++ def shutdown(self, *, wait: bool = True) -> None:+ """Close the SDK, optionally moving network cleanup off the caller."""+ with self._lock:+ sdk = self._posthog+ self._posthog = None+ self._enabled = False+ if sdk is None:+ return++ def close_sdk() -> None:try:- self._posthog.flush()- self._posthog.shutdown()+ sdk.flush()+ sdk.shutdown()except Exception:pass- self._posthog = None- self._enabled = False++ if wait:+ close_sdk()+ else:+ threading.Thread(+ target=close_sdk, name="analytics-shutdown", daemon=True+ ).start()__all__ = ["AnalyticsClient"]diff --git a/src/openjarvis/cli/_analytics_consent.py b/src/openjarvis/cli/_analytics_consent.pynew file mode 100644index 00000000..1a7eb73a--- /dev/null+++ b/src/openjarvis/cli/_analytics_consent.py@@ -0,0 +1,60 @@+"""Dedicated terminal consent for optional external usage analytics."""++from __future__ import annotations++import click+import tomlkit++from openjarvis.core.config import AnalyticsConfig+++def ask_analytics_consent(host: str | None = None) -> bool:+ """Ask once per init; a deliberate Enter/Y consents, EOF/cancellation does not."""+ stream = click.get_text_stream("stdin")+ if not stream.isatty():+ return False+ click.echo(+ "\nHelp improve OpenJarvis by sharing optional usage statistics.\n"+ "Data includes feature usage, timings, token counts, error categories,\n"+ "environment metadata, and a random installation ID.\n"+ "Recipient: the OpenJarvis team's PostHog analytics endpoint.\n"+ "Privacy details: https://open-jarvis.github.io/OpenJarvis/telemetry/\n"+ "You can disable sharing later: jarvis config set analytics.enabled false\n"+ "(then restart the server and reload the app).\n"+ "Declining does not change OpenJarvis functionality.",+ err=True,+ )+ click.echo(f"Analytics destination: {host or AnalyticsConfig().host}", err=True)+ while True:+ click.echo("Press Enter or Y to consent, or N to decline: ", nl=False, err=True)+ try:+ answer = stream.readline()+ except (EOFError, KeyboardInterrupt):+ click.echo(err=True)+ return False+ if not answer:+ return False+ answer = answer.strip().lower()+ if answer in ("", "y", "yes"):+ return True+ if answer in ("n", "no"):+ return False+ click.echo("Please press Enter, type Y, or type N.", err=True)+++def apply_analytics_choice(content: str, choice: bool | None, *, prompt: bool) -> str:+ """Persist an explicit flag or terminal choice in generated/copied TOML."""+ if choice is None:+ if not prompt or not click.get_text_stream("stdin").isatty():+ # Defaults are off. Preserve an explicitly supplied config in+ # unattended init rather than rewriting unrelated input files.+ return content+ doc = tomlkit.parse(content)+ host = doc.get("analytics", {}).get("host", AnalyticsConfig().host)+ choice = ask_analytics_consent(str(host))+ doc = tomlkit.parse(content)+ if "analytics" not in doc:+ doc.add("analytics", tomlkit.table())+ doc["analytics"]["enabled"] = choice+ doc["analytics"]["choice_recorded"] = True+ return tomlkit.dumps(doc)diff --git a/src/openjarvis/cli/init_cmd.py b/src/openjarvis/cli/init_cmd.pyindex affc1083..e2523593 100644--- a/src/openjarvis/cli/init_cmd.py+++ b/src/openjarvis/cli/init_cmd.py@@ -11,6 +11,7 @@ from rich.console import Consolefrom rich.markup import escapefrom rich.panel import Panel+from openjarvis.cli._analytics_consent import apply_analytics_choicefrom openjarvis.cli._banner import print_bannerfrom openjarvis.cli._bootstrap import detect_cloud_keysfrom openjarvis.cli.model import find_model_spec, hf_download, ollama_pull@@ -225,6 +226,11 @@ def _do_download(engine: str, model: str, spec, console: Console) -> None:@click.command()+ "--analytics/--no-analytics",+ default=None,+ help="Consent to or decline external usage analytics without prompting.",+)@click.option("--force", is_flag=True, help="Overwrite existing config without prompting.")@@ -304,6 +310,7 @@ def init(enable_digest: bool = False,preset: Optional[str] = None,from_bare_jarvis: bool = False,+ analytics: bool | None = None,) -> None:"""Detect hardware and generate ~/.openjarvis/config.toml."""print_banner(quiet=(ctx.obj or {}).get("quiet", False))@@ -351,7 +358,12 @@ def init(raise SystemExit(1)DEFAULT_CONFIG_DIR.mkdir(parents=True, exist_ok=True)DEFAULT_CONFIG_PATH.write_text(- preset_path.read_text(encoding="utf-8"), encoding="utf-8"+ apply_analytics_choice(+ preset_path.read_text(encoding="utf-8"),+ analytics,+ prompt=not from_bare_jarvis,+ ),+ encoding="utf-8",)console.print(f"[green]Preset '{preset}' installed to {DEFAULT_CONFIG_PATH}[/green]"@@ -462,6 +474,9 @@ def init(# ``--config`` selects an existing file to install; it is not an alternate# output path. Always activate the selected/generated configuration at# the canonical location that subsequent ``jarvis`` commands load.+ toml_content = apply_analytics_choice(+ toml_content, analytics, prompt=not from_bare_jarvis+ )DEFAULT_CONFIG_PATH.write_text(toml_content, encoding="utf-8")console.print()diff --git a/src/openjarvis/core/config.py b/src/openjarvis/core/config.pyindex 31064215..7c131608 100644--- a/src/openjarvis/core/config.py+++ b/src/openjarvis/core/config.py@@ -1216,11 +1216,13 @@ class AnalyticsConfig:usage events sent to the OpenJarvis team's PostHog instance tomeasure setup success, retention, feature usage, and churn.- No chat content, prompts, model outputs, file paths, emails, IPs,- or hardware identifiers are ever sent. See ``docs/telemetry.md``.+ Backend properties are limited to declared usage metadata; external+ SDKs may add environment metadata. See ``docs/telemetry.md`` for the+ safeguards, destination, and sharing controls."""- enabled: bool = True+ enabled: bool = False+ choice_recorded: bool = Falsehost: str = "https://34.231.106.201.sslip.io"key: str = "phc_ysKu72QaxzYNmDpHFcesD2ZZAe68zkdWJEKoYYkc5e3n"anon_id_path: str = field(default_factory=lambda: str(get_config_dir() / "anon_id"))@@ -1809,6 +1811,20 @@ class JarvisConfig:def _config_dir(self, value: Path) -> None:self.__dict__["_config_source_dir"] = Path(value)+ @property+ def _config_path(self) -> Path:+ """Loaded config file, or the configured default for new instances."""+ source = self.__dict__.get("_config_source_path")+ if source is not None:+ return source+ if custom_path := os.environ.get("OPENJARVIS_CONFIG"):+ return Path(custom_path).expanduser().resolve()+ return get_config_path()++ @_config_path.setter+ def _config_path(self, value: Path) -> None:+ self.__dict__["_config_source_path"] = Path(value)+@propertydef memory(self) -> StorageConfig:"""Backward-compatible accessor — canonical location is tools.storage."""@@ -2075,6 +2091,7 @@ def load_config(path: Optional[Path] = None) -> JarvisConfig:config_path = Path(os.environ["OPENJARVIS_CONFIG"]).expanduser().resolve()else:config_path = get_config_path()+ cfg._config_path = config_pathcfg._config_dir = config_path.parentif config_path.exists():with open(config_path, "rb") as fh:diff --git a/src/openjarvis/server/analytics_routes.py b/src/openjarvis/server/analytics_routes.pyindex 751508bf..b6064a06 100644--- a/src/openjarvis/server/analytics_routes.py+++ b/src/openjarvis/server/analytics_routes.py@@ -6,21 +6,31 @@ This endpoint returns that ID (generating it on first call) plus thepublic PostHog project key and host so the frontend can initialise``posthog-js`` against the same project.-Public API — no auth required because the data returned is exactly-what the frontend would ship to PostHog anyway. The project key is-a public token, not a secret.+Uses the server's normal API-key authentication when configured.+The returned project key is a public PostHog token, not a secret."""from __future__ import annotations-from fastapi import APIRouter-from pydantic import BaseModel+import logging+import tempfile+import threading+from pathlib import Path+import tomlkit+from fastapi import APIRouter, FastAPI, HTTPException, Request+from pydantic import BaseModel, ConfigDict, StrictBool++from openjarvis.analytics.bridge import EventBridge+from openjarvis.analytics.client import AnalyticsClientfrom openjarvis.analytics.identity import (get_or_create_anon_id,is_analytics_enabled,)-from openjarvis.core.config import load_config+from openjarvis.core.config import JarvisConfig, load_config++logger = logging.getLogger(__name__)+_preference_lock = threading.RLock()class AnalyticsIdentity(BaseModel):@@ -28,20 +38,28 @@ class AnalyticsIdentity(BaseModel):anon_id: strhost: strkey: str+ choice_recorded: bool = False+++class AnalyticsPreference(BaseModel):+ """Only the analytics choice belongs here; contest details go elsewhere."""++ model_config = ConfigDict(extra="forbid")+ enabled: StrictBoolrouter = APIRouter(prefix="/v1/analytics", tags=["analytics"])@router.get("/identity", response_model=AnalyticsIdentity)-def get_identity() -> AnalyticsIdentity:+def get_identity(request: Request) -> AnalyticsIdentity:"""Return the analytics identity for the current install.If analytics is disabled in config, still return astructurally valid response with ``enabled=False`` so the frontendcan decide what to do; never throw."""- cfg = load_config()+ cfg = getattr(request.app.state, "config", None) or load_config()enabled = is_analytics_enabled(cfg.analytics)anon_id = get_or_create_anon_id(cfg.analytics.anon_id_path) if enabled else ""return AnalyticsIdentity(@@ -49,7 +67,85 @@ def get_identity() -> AnalyticsIdentity:anon_id=anon_id,host=cfg.analytics.host,key=cfg.analytics.key if enabled else "",+ choice_recorded=cfg.analytics.choice_recorded,)-__all__ = ["router"]+def stop_analytics(app: FastAPI, *, wait: bool = True) -> None:+ """Detach the event bridge and close the current analytics client."""+ bridge = getattr(app.state, "analytics_bridge", None)+ client = getattr(app.state, "analytics_client", None)+ app.state.analytics_bridge = None+ app.state.analytics_client = None+ if client is not None and not wait:+ client.disable()+ if bridge is not None:+ try:+ bridge.stop()+ except Exception:+ logger.debug("Analytics bridge shutdown failed", exc_info=True)+ if client is not None:+ client.shutdown(wait=wait)+++def configure_analytics(app: FastAPI, cfg: JarvisConfig) -> None:+ """Apply a sharing choice immediately to this running server."""+ stop_analytics(app, wait=False)+ app.state.config = cfg+ if not is_analytics_enabled(cfg.analytics):+ return+ client = AnalyticsClient(cfg.analytics)+ app.state.analytics_client = client+ bus = getattr(app.state, "bus", None)+ if bus is not None and client.enabled:+ bridge = EventBridge(bus, client)+ app.state.analytics_bridge = bridge+ bridge.start()++[email protected]("/preference", response_model=AnalyticsIdentity)+def set_preference(choice: AnalyticsPreference, request: Request) -> AnalyticsIdentity:+ """Persist an explicit UI choice and update analytics without restarting."""+ with _preference_lock:+ cfg = getattr(request.app.state, "config", None) or load_config()+ path = cfg._config_path+ temporary: Path | None = None+ try:+ path.parent.mkdir(parents=True, exist_ok=True)+ doc = (+ tomlkit.parse(path.read_text(encoding="utf-8"))+ if path.exists()+ else tomlkit.document()+ )+ if "analytics" not in doc:+ doc.add("analytics", tomlkit.table())+ doc["analytics"]["enabled"] = choice.enabled+ doc["analytics"]["choice_recorded"] = True+ with tempfile.NamedTemporaryFile(+ mode="w", encoding="utf-8", dir=path.parent, delete=False+ ) as handle:+ temporary = Path(handle.name)+ handle.write(tomlkit.dumps(doc))+ temporary.replace(path)+ except (OSError, ValueError, TypeError) as exc:+ logger.warning("Could not save analytics preference: %s", exc)+ raise HTTPException(+ status_code=500, detail="Could not save your analytics choice."+ ) from exc+ finally:+ if temporary is not None:+ temporary.unlink(missing_ok=True)+ cfg.analytics.enabled = choice.enabled+ cfg.analytics.choice_recorded = True+ load_config.cache_clear()+ client = getattr(request.app.state, "analytics_client", None)+ if (choice.enabled and (client is None or not client.enabled)) or (+ not choice.enabled and client is not None+ ):+ configure_analytics(request.app, cfg)+ else:+ request.app.state.config = cfg+ return get_identity(request)+++__all__ = ["configure_analytics", "router", "stop_analytics"]diff --git a/src/openjarvis/server/app.py b/src/openjarvis/server/app.pyindex dba9d53c..7736760e 100644--- a/src/openjarvis/server/app.py+++ b/src/openjarvis/server/app.py@@ -12,7 +12,13 @@ from fastapi import FastAPIfrom fastapi.responses import FileResponsefrom fastapi.staticfiles import StaticFiles-from openjarvis.server.analytics_routes import router as analytics_router+from openjarvis.server.analytics_routes import (+ configure_analytics,+ stop_analytics,+)+from openjarvis.server.analytics_routes import (+ router as analytics_router,+)from openjarvis.server.api_routes import include_all_routesfrom openjarvis.server.comparison import comparison_routerfrom openjarvis.server.connectors_router import create_connectors_router@@ -445,40 +451,17 @@ def create_app(app.state.analytics_client = Noneapp.state.analytics_bridge = Nonetry:- from openjarvis.analytics import (- AnalyticsClient,- EventBridge,- is_analytics_enabled,- )from openjarvis.core.config import load_config_cfg = config if config is not None else load_config()- if is_analytics_enabled(_cfg.analytics):- _client = AnalyticsClient(_cfg.analytics)- app.state.analytics_client = _client- _bus_ref = getattr(app.state, "bus", None)- if _bus_ref is not None:- _bridge = EventBridge(_bus_ref, _client)- _bridge.start()- app.state.analytics_bridge = _bridge-- @app.on_event("shutdown")- async def _shutdown_analytics() -> None:- bridge = getattr(app.state, "analytics_bridge", None)- if bridge is not None:- try:- bridge.stop()- except Exception:- pass- client = getattr(app.state, "analytics_client", None)- if client is not None:- try:- client.shutdown()- except Exception:- pass+ configure_analytics(app, _cfg)except Exception as _exc:logger.debug("Analytics init skipped: %s", _exc)+ @app.on_event("shutdown")+ async def _shutdown_analytics() -> None:+ stop_analytics(app)+# Stop the background memory service cleanly when the server shuts down.if memory_service is not None:diff --git a/tests/analytics/test_opt_in.py b/tests/analytics/test_opt_in.pynew file mode 100644index 00000000..e5ae77cc--- /dev/null+++ b/tests/analytics/test_opt_in.py@@ -0,0 +1,53 @@+"""Default-off analytics must not create an identity or initialise sending."""++from __future__ import annotations++from pathlib import Path+from unittest.mock import MagicMock++import pytest+from fastapi import FastAPI+from fastapi.testclient import TestClient++from openjarvis.analytics.client import AnalyticsClient+from openjarvis.core.config import AnalyticsConfig, JarvisConfig, load_config+from openjarvis.server import analytics_routes+++def test_missing_analytics_section_defaults_off(tmp_path: Path) -> None:+ path = tmp_path / "config.toml"+ path.write_text('[engine]\ndefault = "ollama"\n')+ assert load_config(path).analytics.enabled is False+++def test_disabled_client_never_creates_id_or_initialises_sdk(+ tmp_path, monkeypatch+) -> None:+ init_sdk = MagicMock()+ monkeypatch.setattr(AnalyticsClient, "_init_sdk", init_sdk)+ cfg = AnalyticsConfig(anon_id_path=str(tmp_path / "anon_id"))+ client = AnalyticsClient(cfg)+ client.capture("app_opened", {"platform": "cli"})+ assert not client.enabled+ assert client.anon_id == ""+ assert not Path(cfg.anon_id_path).exists()+ init_sdk.assert_not_called()++[email protected]("enabled", [False, True])+def test_identity_gates_frontend_and_id_creation(+ tmp_path, monkeypatch, enabled+) -> None:+ cfg = JarvisConfig()+ cfg.analytics.enabled = enabled+ cfg.analytics.anon_id_path = str(tmp_path / "anon_id")+ monkeypatch.setattr(analytics_routes, "load_config", lambda: cfg)+ app = FastAPI()+ app.include_router(analytics_routes.router)+ response = TestClient(app).get("/v1/analytics/identity")+ assert response.status_code == 200+ data = response.json()+ assert data["enabled"] is enabled+ assert bool(data["key"]) is enabled+ assert bool(data["anon_id"]) is enabled+ assert Path(cfg.analytics.anon_id_path).exists() is enableddiff --git a/tests/analytics/test_preferences.py b/tests/analytics/test_preferences.pynew file mode 100644index 00000000..e98d0b4e--- /dev/null+++ b/tests/analytics/test_preferences.py@@ -0,0 +1,201 @@+"""An explicit UI choice must persist and immediately gate both senders."""++from __future__ import annotations++import threading+from concurrent.futures import ThreadPoolExecutor+from pathlib import Path+from unittest.mock import MagicMock++import pytest+import tomllib+from fastapi import FastAPI+from fastapi.testclient import TestClient++from openjarvis.analytics.client import AnalyticsClient+from openjarvis.core.config import JarvisConfig, load_config+from openjarvis.core.events import EventBus, EventType+from openjarvis.server import analytics_routes+from openjarvis.server.auth_middleware import AuthMiddleware+++def preference_app(tmp_path: Path, monkeypatch):+ path = tmp_path / "custom-config.toml"+ path.write_text('# Keep this comment\n[engine]\ndefault = "ollama"\n')+ cfg = JarvisConfig()+ cfg._config_path = path+ cfg.analytics.anon_id_path = str(tmp_path / "anon_id")++ def fake_sdk(client):+ client._posthog = MagicMock()++ monkeypatch.setattr(AnalyticsClient, "_init_sdk", fake_sdk)+ app = FastAPI()+ app.state.config = cfg+ app.state.bus = EventBus()+ app.include_router(analytics_routes.router)+ yield app, path, cfg+ analytics_routes.stop_analytics(app)+++def test_enable_and_disable_apply_immediately_and_survive_reload(preference_app):+ app, path, cfg = preference_app+ http = TestClient(app)+ assert http.get("/v1/analytics/identity").json()["enabled"] is False+ assert not Path(cfg.analytics.anon_id_path).exists()++ response = http.post("/v1/analytics/preference", json={"enabled": True})+ assert response.status_code == 200+ assert response.json()["enabled"] is True+ assert response.json()["choice_recorded"] is True+ assert response.json()["key"]+ sdk = app.state.analytics_client._posthog+ closed = threading.Event()+ sdk.shutdown.side_effect = closed.set+ # Re-saving an enabled choice must not replace a working sender.+ assert (+ http.post("/v1/analytics/preference", json={"enabled": True}).status_code == 200+ )+ assert app.state.analytics_client._posthog is sdk+ app.state.bus.publish(EventType.FEEDBACK_RECEIVED, {"rating": 1})+ assert sdk.capture.call_count == 1++ response = http.post("/v1/analytics/preference", json={"enabled": False})+ assert response.status_code == 200+ assert response.json()["enabled"] is False+ assert response.json()["key"] == ""+ assert response.json()["anon_id"] == ""+ assert app.state.analytics_client is None+ assert app.state.analytics_bridge is None+ assert closed.wait(timeout=2)+ sdk.shutdown.assert_called_once()+ app.state.bus.publish(EventType.FEEDBACK_RECEIVED, {"rating": 2})+ assert sdk.capture.call_count == 1+ loaded = load_config(path)+ assert loaded.analytics.enabled is False+ assert loaded.analytics.choice_recorded is True+ assert loaded.engine.default == "ollama"+ assert "# Keep this comment" in path.read_text()+++ "config_source", ["default", "environment", "explicit", "programmatic"]+)[email protected]("enabled", [False, True])+def test_preference_persists_to_active_config(+ preference_app, tmp_path, monkeypatch, config_source, enabled+):+ app, _, _ = preference_app+ home = tmp_path / "home"+ home.mkdir()+ monkeypatch.setenv("OPENJARVIS_HOME", str(home))+ monkeypatch.delenv("OPENJARVIS_CONFIG", raising=False)+ default_path = home / "config.toml"+ path = default_path+ if config_source in ("environment", "explicit"):+ path = tmp_path / "deployment.toml"+ default_path.write_text("# Leave the default configuration alone\n")+ if config_source == "environment":+ monkeypatch.setenv("OPENJARVIS_CONFIG", str(path))+ elif config_source == "explicit":+ # An explicit load path takes precedence over the environment too.+ monkeypatch.setenv("OPENJARVIS_CONFIG", str(tmp_path / "unused.toml"))+ path.write_text(f"[analytics]\nenabled = {str(not enabled).lower()}\n")+ load_config.cache_clear()+ try:+ if config_source == "programmatic":+ cfg = JarvisConfig()+ cfg.analytics.enabled = not enabled+ else:+ cfg = load_config(path if config_source == "explicit" else None)+ analytics_routes.configure_analytics(app, cfg)++ response = TestClient(app).post(+ "/v1/analytics/preference", json={"enabled": enabled}+ )++ assert response.status_code == 200+ assert response.json()["enabled"] is enabled+ load_config.cache_clear()+ reloaded = load_config(path if config_source == "explicit" else None)+ assert reloaded.analytics.enabled is enabled+ assert reloaded.analytics.choice_recorded is True+ if path != default_path:+ assert (+ default_path.read_text() == "# Leave the default configuration alone\n"+ )+ assert not (tmp_path / "unused.toml").exists()+ finally:+ load_config.cache_clear()+++def test_decline_does_not_wait_for_an_unreachable_analytics_server(preference_app):+ app, path, cfg = preference_app+ http = TestClient(app)+ assert (+ http.post("/v1/analytics/preference", json={"enabled": True}).status_code == 200+ )+ sdk = app.state.analytics_client._posthog+ release = threading.Event()+ sdk.flush.side_effect = lambda: release.wait(5)+ try:+ with ThreadPoolExecutor(max_workers=1) as executor:+ response = executor.submit(+ http.post, "/v1/analytics/preference", json={"enabled": False}+ ).result(timeout=2)+ assert response.status_code == 200+ assert cfg.analytics.enabled is False+ assert app.state.analytics_client is None+ finally:+ release.set()+++ "body",+ [+ {"enabled": "false"},+ {"enabled": 1},+ {"enabled": True, "email": "[email protected]"},+ ],+)+def test_preference_accepts_only_a_boolean_and_no_contest_details(preference_app, body):+ app, path, cfg = preference_app+ original = path.read_text()+ response = TestClient(app).post("/v1/analytics/preference", json=body)+ assert response.status_code == 422+ assert path.read_text() == original+ assert cfg.analytics.enabled is False+ assert not Path(cfg.analytics.anon_id_path).exists()+++def test_failed_persistence_does_not_enable_sending(preference_app, monkeypatch):+ app, path, cfg = preference_app+ original = path.read_text()+ monkeypatch.setattr(+ analytics_routes.tempfile,+ "NamedTemporaryFile",+ MagicMock(side_effect=PermissionError("read-only config")),+ )+ response = TestClient(app).post("/v1/analytics/preference", json={"enabled": True})+ assert response.status_code == 500+ assert path.read_text() == original+ assert cfg.analytics.enabled is False+ assert not Path(cfg.analytics.anon_id_path).exists()+++def test_preference_uses_existing_api_authentication(preference_app):+ app, path, cfg = preference_app+ app.add_middleware(AuthMiddleware, api_key="test-api-key")+ http = TestClient(app)+ assert (+ http.post("/v1/analytics/preference", json={"enabled": True}).status_code == 401+ )+ assert cfg.analytics.enabled is False+ response = http.post(+ "/v1/analytics/preference",+ json={"enabled": False},+ headers={"Authorization": "Bearer test-api-key"},+ )+ assert response.status_code == 200+ assert tomllib.loads(path.read_text())["analytics"]["choice_recorded"] is Truediff --git a/tests/install/bash/run_installer_pty.py b/tests/install/bash/run_installer_pty.pynew file mode 100644index 00000000..b22afc01--- /dev/null+++ b/tests/install/bash/run_installer_pty.py@@ -0,0 +1,74 @@+"""Run the real curl-pipe installer under a terminal with scripted answers."""++from __future__ import annotations++import fcntl+import json+import os+import pty+import select+import subprocess+import sys+import termios+import time+++def main() -> int:+ script = sys.argv[1]+ answers = json.loads(sys.argv[2])+ master, slave = pty.openpty()++ def attach_terminal() -> None:+ os.setsid()+ fcntl.ioctl(slave, termios.TIOCSCTTY, 0)++ process = subprocess.Popen(+ [+ "bash",+ "-c",+ 'cat "$1" | bash -s -- --no-bg-orchestrator --minimal',+ "_",+ script,+ ],+ stdin=slave,+ stdout=slave,+ stderr=slave,+ preexec_fn=attach_terminal,+ )+ os.close(slave)+ output = bytearray()+ answered = 0+ deadline = time.monotonic() + 20+ try:+ while time.monotonic() < deadline:+ readable, _, _ = select.select([master], [], [], 0.1)+ if readable:+ try:+ chunk = os.read(master, 65536)+ except OSError:+ break+ if not chunk:+ break+ output.extend(chunk)+ prompts = output.count(+ b"Press Enter or Y to consent, or N to decline: "+ )+ if prompts > answered and answered < len(answers):+ os.write(master, answers[answered].encode())+ answered += 1+ elif process.poll() is not None:+ break+ else:+ process.kill()+ raise TimeoutError("Installer terminal test timed out")+ return process.wait(timeout=5)+ finally:+ if process.poll() is None:+ process.kill()+ process.wait()+ os.close(master)+ sys.stdout.buffer.write(output)+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/tests/install/bash/test_install.bats b/tests/install/bash/test_install.batsindex 09f49cb7..d11197d9 100755--- a/tests/install/bash/test_install.bats+++ b/tests/install/bash/test_install.bats@@ -1,6 +1,12 @@#!/usr/bin/env bats+# Requires Python with tomlkit, as does the installed application. Set+# OPENJARVIS_TEST_PYTHON to an existing project venv when running locally.+setup() {+ export OPENJARVIS_TEST_PYTHON="${OPENJARVIS_TEST_PYTHON:-$(command -v python3)}"+ export PYTHONPATH="$BATS_TEST_DIRNAME/../../../src"+ unset OPENJARVIS_CONFIG XDG_DATA_HOMETEST_TMPDIR=$(mktemp -d)export FAKE_HOME="$TEST_TMPDIR/home"mkdir -p "$FAKE_HOME"@@ -16,6 +22,7 @@ setup() {export OLLAMA_STUB_LOG="$TEST_TMPDIR/ollama.log"export UV_STUB_LOG="$TEST_TMPDIR/uv.log"export CURL_STUB_LOG="$TEST_TMPDIR/curl.log"+ export JARVIS_STUB_LOG="$TEST_TMPDIR/jarvis.log": > "$GIT_STUB_LOG": > "$OLLAMA_STUB_LOG": > "$UV_STUB_LOG"@@ -46,14 +53,20 @@ case "\$1" incat > "\$venv_path/bin/jarvis" <<'EOJ'#!/usr/bin/env bash# fake jarvis for tests+echo "\$@" >> "\$JARVIS_STUB_LOG"+# Model the pre-consent CLI, which cannot validate the new schema key.+if [[ "\$*" == 'config set analytics.choice_recorded true' ]]; then+ echo "Error: Unknown config key: 'analytics.choice_recorded'" >&2+ exit 1+fiecho "fake jarvis: \$@"exit 0EOJchmod +x "\$venv_path/bin/jarvis"cat > "\$venv_path/bin/python" <<'EOJ'#!/usr/bin/env bash-# fake python that prints empty for the inline embedded scripts (recommend_model, etc.)-exit 0+# Run the real persistence code using an interpreter with app dependencies.+exec "\$OPENJARVIS_TEST_PYTHON" "\$@"EOJchmod +x "\$venv_path/bin/python"fi@@ -76,6 +89,131 @@ teardown() {[[ -n "${TEST_TMPDIR:-}" ]] && rm -rf "$TEST_TMPDIR"}+assert_analytics_preference() {+ "$OPENJARVIS_TEST_PYTHON" - "$1" "$2" "$3" <<'PY'+import sys+from pathlib import Path++import tomlkit++analytics = tomlkit.parse(Path(sys.argv[1]).read_text())["analytics"]+assert bool(analytics["enabled"]) is (sys.argv[2] == "true")+assert bool(analytics.get("choice_recorded", False)) is (sys.argv[3] == "true")+PY+}++@test "unattended install sends no analytics and persists disabled" {+ run bash "$SCRIPT" --no-bg-orchestrator --minimal+ [ "$status" -eq 0 ]+ ! grep -q '/i/v0/e/' "$CURL_STUB_LOG"+ [ ! -f "$OPENJARVIS_HOME/anon_id" ]+ assert_analytics_preference "$OPENJARVIS_HOME/config.toml" false false+}++@test "explicit analytics flag sends beacons and persists enabled" {+ run bash "$SCRIPT" --no-bg-orchestrator --minimal --analytics+ [ "$status" -eq 0 ]+ grep -q '/i/v0/e/' "$CURL_STUB_LOG"+ [ -s "$OPENJARVIS_HOME/anon_id" ]+ assert_analytics_preference "$OPENJARVIS_HOME/config.toml" true true+}++@test "Enter at dedicated terminal prompt consents before the first beacon" {+ run python3 "$BATS_TEST_DIRNAME/run_installer_pty.py" "$SCRIPT" '["\n"]'+ [ "$status" -eq 0 ]+ [[ "$output" == *"Press Enter or Y to consent, or N to decline"* ]]+ grep -q '"event":"install_started"' "$CURL_STUB_LOG"+ assert_analytics_preference "$OPENJARVIS_HOME/config.toml" true true+}++@test "N at terminal prompt declines every installer beacon" {+ run python3 "$BATS_TEST_DIRNAME/run_installer_pty.py" "$SCRIPT" '["N\n"]'+ [ "$status" -eq 0 ]+ ! grep -q '/i/v0/e/' "$CURL_STUB_LOG"+ [ ! -f "$OPENJARVIS_HOME/anon_id" ]+ assert_analytics_preference "$OPENJARVIS_HOME/config.toml" false true+}++@test "invalid answer retries without silently consenting" {+ run python3 "$BATS_TEST_DIRNAME/run_installer_pty.py" "$SCRIPT" '["invalid\n", "n\n"]'+ [ "$status" -eq 0 ]+ [[ "$output" == *"Please press Enter, type Y, or type N."* ]]+ ! grep -q '/i/v0/e/' "$CURL_STUB_LOG"+}++@test "terminal EOF is different from pressing Enter" {+ run python3 "$BATS_TEST_DIRNAME/run_installer_pty.py" "$SCRIPT" '["\u0004"]'+ [ "$status" -eq 0 ]+ ! grep -q '/i/v0/e/' "$CURL_STUB_LOG"+ [ ! -f "$OPENJARVIS_HOME/anon_id" ]+ assert_analytics_preference "$OPENJARVIS_HOME/config.toml" false false+}++@test "declining on a rerun persists despite skipped config creation" {+ run bash "$SCRIPT" --no-bg-orchestrator --minimal --analytics+ [ "$status" -eq 0 ]+ : > "$JARVIS_STUB_LOG"+ : > "$CURL_STUB_LOG"+ run bash "$SCRIPT" --no-bg-orchestrator --minimal --no-analytics+ [ "$status" -eq 0 ]+ ! grep -q '/i/v0/e/' "$CURL_STUB_LOG"+ ! grep -q '_bootstrap' "$JARVIS_STUB_LOG"+ assert_analytics_preference "$OPENJARVIS_HOME/config.toml" false true+}++@test "rerun saves consent with an older CLI and preserves existing TOML" {+ run bash "$SCRIPT" --no-bg-orchestrator --minimal+ [ "$status" -eq 0 ]+ cat > "$OPENJARVIS_HOME/config.toml" <<'TOML'+# Preserve user settings and comments.+[engine]+default = "ollama"+[analytics]+enabled = false # Previous choice.+flush_at_size = 7+TOML+ run "$OPENJARVIS_HOME/.venv/bin/jarvis" config set analytics.choice_recorded true+ [ "$status" -eq 1 ]+ : > "$JARVIS_STUB_LOG"+ : > "$UV_STUB_LOG"+ run bash "$SCRIPT" --no-bg-orchestrator --minimal --analytics+ [ "$status" -eq 0 ]+ [ ! -s "$UV_STUB_LOG" ]+ [ ! -s "$JARVIS_STUB_LOG" ]+ assert_analytics_preference "$OPENJARVIS_HOME/config.toml" true true+ grep -q '# Preserve user settings and comments.' "$OPENJARVIS_HOME/config.toml"+ grep -q '# Previous choice.' "$OPENJARVIS_HOME/config.toml"+ grep -q 'default = "ollama"' "$OPENJARVIS_HOME/config.toml"+ grep -q 'flush_at_size = 7' "$OPENJARVIS_HOME/config.toml"+}++@test "consent respects OPENJARVIS_CONFIG instead of changing the default config" {+ run bash "$SCRIPT" --no-bg-orchestrator --minimal --no-analytics+ [ "$status" -eq 0 ]+ export OPENJARVIS_CONFIG="$TEST_TMPDIR/custom config.toml"+ printf '# Custom config\n[engine]\ndefault = "ollama"\n' > "$OPENJARVIS_CONFIG"+ run bash "$SCRIPT" --no-bg-orchestrator --minimal --analytics+ [ "$status" -eq 0 ]+ assert_analytics_preference "$OPENJARVIS_CONFIG" true true+ assert_analytics_preference "$OPENJARVIS_HOME/config.toml" false true+ grep -q '# Custom config' "$OPENJARVIS_CONFIG"+}++@test "consent follows the runtime XDG path when OPENJARVIS_HOME is unset" {+ unset OPENJARVIS_HOME+ export XDG_DATA_HOME="$TEST_TMPDIR/xdg data"+ run bash "$SCRIPT" --no-bg-orchestrator --minimal --analytics+ [ "$status" -eq 0 ]+ assert_analytics_preference "$XDG_DATA_HOME/openjarvis/config.toml" true true+ [ ! -f "$HOME/.openjarvis/config.toml" ]+}++@test "contradictory analytics flags are rejected" {+ run bash "$SCRIPT" --analytics --no-analytics+ [ "$status" -eq 2 ]+ ! grep -q '/i/v0/e/' "$CURL_STUB_LOG"+}+@test "refuses to run as root" {# EUID is readonly in bash, so we stub `id` to return uid 0 instead.local root_stubs="$TEST_TMPDIR/root_stubs"diff --git a/tests/install/test_analytics_consent.py b/tests/install/test_analytics_consent.pynew file mode 100644index 00000000..5827d00e--- /dev/null+++ b/tests/install/test_analytics_consent.py@@ -0,0 +1,122 @@+"""Terminal answers, unattended init, and persisted runtime analytics choices."""++from __future__ import annotations++import io+from pathlib import Path++import pytest+import tomllib+from click.testing import CliRunner++from openjarvis.cli import _analytics_consent+from openjarvis.cli.init_cmd import init+from openjarvis.core.config import load_config+++class TerminalInput(io.StringIO):+ def isatty(self) -> bool:+ return True+++def use_input(monkeypatch, stream: io.StringIO) -> None:+ original = _analytics_consent.click.get_text_stream+ monkeypatch.setattr(+ _analytics_consent.click,+ "get_text_stream",+ lambda name, *args, **kwargs: (+ stream if name == "stdin" else original(name, *args, **kwargs)+ ),+ )+++ ("answer", "enabled"),+ [+ ("\n", True),+ ("y\n", True),+ ("YES\n", True),+ ("n\n", False),+ ("no\n", False),+ ("", False),+ ("invalid\nn\n", False),+ ],+)+def test_terminal_choice(monkeypatch, capsys, answer: str, enabled: bool) -> None:+ stream = TerminalInput(answer)+ use_input(monkeypatch, stream)+ assert _analytics_consent.ask_analytics_consent() is enabled+ assert "Press Enter or Y to consent, or N to decline" in capsys.readouterr().err+++def test_redirected_input_never_consents(monkeypatch) -> None:+ stream = io.StringIO("\n")+ use_input(monkeypatch, stream)+ assert _analytics_consent.ask_analytics_consent() is False+ assert stream.tell() == 0+++def test_cancelled_prompt_never_consents(monkeypatch) -> None:+ class CancelledInput(TerminalInput):+ def readline(self, *args) -> str:+ raise KeyboardInterrupt++ use_input(monkeypatch, CancelledInput())+ assert _analytics_consent.ask_analytics_consent() is False++[email protected]("flag", [None, "--analytics", "--no-analytics"])+def test_init_persists_flag_or_default(+ tmp_openjarvis_home: Path, flag: str | None+) -> None:+ args = ["--engine", "ollama", "--no-download", "--no-scan"]+ if flag:+ args.append(flag)+ result = CliRunner().invoke(init, args)+ assert result.exit_code == 0, result.output+ cfg = load_config(tmp_openjarvis_home / "config.toml")+ assert cfg.analytics.enabled is (flag == "--analytics")+ assert not (tmp_openjarvis_home / "anon_id").exists()++[email protected](("answer", "enabled"), [("\n", True), ("N\n", False)])+def test_init_persists_interactive_choice(+ tmp_openjarvis_home: Path, monkeypatch, answer: str, enabled: bool+) -> None:+ stream = TerminalInput(answer)+ use_input(monkeypatch, stream)+ result = CliRunner().invoke(+ init, ["--engine", "ollama", "--no-download", "--no-scan"]+ )+ assert result.exit_code == 0, result.output+ data = tomllib.loads((tmp_openjarvis_home / "config.toml").read_text())+ assert data["analytics"]["enabled"] is enabled+ assert load_config(tmp_openjarvis_home / "config.toml").analytics.enabled is enabled+++def test_supplied_config_is_preserved_without_a_new_choice(monkeypatch) -> None:+ content = '# Keep this comment\n[engine]\ndefault = "ollama"\n'+ use_input(monkeypatch, io.StringIO())+ assert (+ _analytics_consent.apply_analytics_choice(content, None, prompt=True) == content+ )+++def test_decline_replaces_existing_enabled_value_without_losing_settings() -> None:+ content = "# Keep this comment\n[analytics]\nenabled = true\nflush_at_size = 7\n"+ result = _analytics_consent.apply_analytics_choice(content, False, prompt=False)+ data = tomllib.loads(result)+ assert data["analytics"] == {+ "enabled": False,+ "flush_at_size": 7,+ "choice_recorded": True,+ }+ assert "# Keep this comment" in result+++def test_prompt_discloses_configured_destination(monkeypatch, capsys) -> None:+ use_input(monkeypatch, TerminalInput("n\n"))+ content = '[analytics]\nhost = "https://analytics.example.org"\n'+ result = _analytics_consent.apply_analytics_choice(content, None, prompt=True)+ assert "https://analytics.example.org" in capsys.readouterr().err+ assert tomllib.loads(result)["analytics"]["enabled"] is False