# enu3379/kibitzer architecture

> Architecture diagram of the GitHub repository enu3379/kibitzer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/enu3379/kibitzer
- Repository: https://github.com/enu3379/kibitzer

## Overview

#### Purpose and execution surface
Kibitzer is a Manifest-based Chrome extension that observes non-incognito browsing during a user-defined focus session, estimates distraction accumulation, and displays non-blocking coaching plus a session summary. The extension package and build boundary are `apps/extension/manifest.json`, `apps/extension/build.mjs`, and `apps/extension/src/background.ts`. User-facing entry points are the popup, options, onboarding, replay viewer, and local-PDF prompt pages.

#### Extension orchestration and browser observation
The background service worker is the control-plane owner: it receives Chrome lifecycle/navigation/activity signals, maintains session state, schedules dwell processing, updates UI/badge state, and coordinates persistence and judgments. Page-context collection is intentionally separated into content scripts: `apps/extension/src/content/pageExcerpt.ts` extracts bounded page text only when needed, while `apps/extension/src/content/toastOverlay.ts` renders coaching on the current page. `apps/extension/src/offscreen.ts` provides an offscreen runtime boundary for work unsuitable for the service worker.

#### Session, event, and durable state
A session records the goal, browsing events, judgment outcomes, intervention state, and aggregate reporting data. Core state is persisted locally in browser storage/IndexedDB through `apps/extension/src/lib/db.ts`; event definitions and history/session reconstruction live in `apps/extension/src/lib/events.ts`, `apps/extension/src/lib/session.ts`, and `apps/extension/src/lib/sessionRestore.ts`. This local-first boundary supports service-worker restarts and the replay capability; no application backend is present.

#### Attention measurement and intervention state machine
Browsing dwell and presence signals are converted into attention evidence by `apps/extension/src/lib/dwell.ts`, `apps/extension/src/lib/dwellScheduler.ts`, and `apps/extension/src/lib/presence.ts`. The focus “gauge” is a distinct deterministic reducer/state-machine layer in `apps/extension/src/core/gauge/reducer.ts`, with its contract/types/config in `apps/extension/src/core/gauge/types.ts` and `apps/extension/src/core/gauge/config.ts`. Gauge tiers, cooldown/snooze/refund behavior, and runtime transitions determine whether the extension stays quiet, requests judgment, or emits a nag.

#### Tiered relevance judgment
Judgment is staged to minimize data sharing and latency. A local Tier 0 semantic classifier uses the bundled Korean/English ONNX embedding model via `apps/extension/src/providers/tier0Wasm.ts` and `apps/extension/assets/models/koen-e5-tiny/model-manifest.json`; relevance policy/orchestration resides in `apps/extension/src/lib/relevance.ts` and `apps/extension/src/lib/tier0.ts`. Higher tiers can use an opted-in LLM for fast or detailed assessment; detailed requests add a page excerpt capped by product policy. Sensitive and user-excluded domains are filtered before observation through `apps/extension/src/lib/domainFilter.ts` and `configs/sensitive_domains.json`.

#### AI-provider and persona boundary
Provider-neutral request formation, prompting, parsing, and failure handling are concentrated in `apps/extension/src/providers/chatTransport.ts`, `apps/extension/src/providers/payloads.ts`, and `apps/extension/src/providers/judgeParsing.ts`. Implementations include OpenAI-compatible/OpenAI, Claude, and Ollama paths in `apps/extension/src/providers/openaiChat.ts`, `apps/extension/src/providers/claudeChat.ts`, and `apps/extension/src/providers/ollamaChat.ts`; configured services include Ollama Cloud, OpenRouter, Gemini, Deepseek, and Kimi. Persona configuration drives coaching voice and fallback copy via `apps/extension/src/lib/personas.ts`, `configs/personas.yaml`, and `configs/personas/`.

#### UI, reporting, and operational tooling
The popup starts/controls sessions and exposes immediate status (`apps/extension/src/popup/popup.ts`); settings cover AI, privacy, and local data (`apps/extension/src/options/options.ts`); onboarding initializes user choices (`apps/extension/src/onboarding/onboarding.ts`). End-of-session aggregation and generated commentary are handled by `apps/extension/src/lib/sessionSummary.ts`, `apps/extension/src/lib/sessionReport.ts`, and `apps/extension/src/lib/reportCards.ts`. Recorded events can be replayed through `apps/extension/src/replay/replay.ts` and the CLI `apps/extension/tools/replay.ts`, creating a deterministic debugging/audit boundary around the core loop.

Main entry points: [`apps/extension/src/background.ts`](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/background.ts) (Background control plane), [`apps/extension/src/lib/relevance.ts`](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/relevance.ts) (Relevance orchestration), [`apps/extension/src/lib/session.ts`](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/session.ts) (Session history), [`apps/extension/src/core/gauge/reducer.ts`](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/core/gauge/reducer.ts) (Focus gauge reducer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_extension["Extension runtime"]
  node_background{{"Background control plane<br/>service worker<br/>[background.ts]"}}
  node_db[("Local durable store<br/>browser storage / IndexedDB<br/>[db.ts]")]
  node_restore["Session restoration<br/>state recovery<br/>[sessionRestore.ts]"]
  node_page_excerpt["Page excerpt collector<br/>content script<br/>[pageExcerpt.ts]"]
  node_toast["Coaching overlay<br/>content script<br/>[toastOverlay.ts]"]
  node_offscreen["Offscreen runtime<br/>extension runtime<br/>[offscreen.ts]"]
end

subgraph group_attention["Attention &amp; session core"]
  node_attention_evidence["Dwell &amp; presence evidence<br/>attention measurement<br/>[dwellScheduler.ts]"]
  node_gauge{{"Focus gauge reducer<br/>deterministic state machine<br/>[reducer.ts]"}}
  node_session["Session history<br/>event and session model<br/>[session.ts]"]
end

subgraph group_judgment["Relevance &amp; AI"]
  node_domain_filter["Domain privacy filter<br/>observation policy<br/>[domainFilter.ts]"]
  node_relevance{{"Relevance orchestration<br/>tiered judgment policy<br/>[relevance.ts]"}}
  node_tier0["Local semantic classifier<br/>ONNX/WASM inference<br/>[tier0Wasm.ts]"]
  node_model["Bundled embedding model<br/>ONNX model asset"]
  node_transport["Provider-neutral AI transport<br/>AI request boundary<br/>[chatTransport.ts]"]
  node_llm(("Opted-in LLM providers<br/>external AI services<br/>[openaiChat.ts]"))
  node_personas["Persona configuration<br/>coaching policy<br/>[personas.ts]"]
end

subgraph group_ui["User surfaces &amp; reporting"]
  node_popup["Popup controls<br/>extension UI<br/>[popup.ts]"]
  node_settings["Options &amp; onboarding<br/>extension setup UI<br/>[options.ts]"]
  node_reporting["Session reports<br/>aggregation and commentary<br/>[sessionReport.ts]"]
  node_replay["Replay viewer &amp; CLI<br/>deterministic audit tool<br/>[replay.ts]"]
end

node_chrome(("Chrome signals<br/>browser API"))

node_chrome -->|"browser events"| node_background
node_background -->|"filter before observe"| node_domain_filter
node_background -->|"schedule dwell"| node_attention_evidence
node_attention_evidence -->|"attention evidence"| node_gauge
node_background -->|"request judgment"| node_relevance
node_relevance -->|"Tier 0"| node_tier0
node_model -->|"embeddings"| node_tier0
node_relevance -.->|"detailed-only excerpt"| node_page_excerpt
node_relevance -->|"higher tiers"| node_transport
node_transport -->|"provider request"| node_llm
node_relevance -->|"judgment outcome"| node_gauge
node_personas -.->|"coaching voice"| node_toast
node_gauge -->|"emit coaching"| node_toast
node_background -->|"record state"| node_session
node_session -->|"persist locally"| node_db
node_db -->|"reload after restart"| node_restore
node_restore -->|"restore session"| node_background
node_popup -->|"session controls"| node_background
node_settings -->|"preferences"| node_background
node_session -->|"aggregate session"| node_reporting
node_session -->|"recorded events"| node_replay
node_background -.->|"delegate runtime work"| node_offscreen

click node_background "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/background.ts"
click node_db "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/db.ts"
click node_restore "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/sessionRestore.ts"
click node_page_excerpt "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/content/pageExcerpt.ts"
click node_toast "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/content/toastOverlay.ts"
click node_offscreen "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/offscreen.ts"
click node_attention_evidence "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/dwellScheduler.ts"
click node_gauge "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/core/gauge/reducer.ts"
click node_session "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/session.ts"
click node_domain_filter "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/domainFilter.ts"
click node_relevance "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/relevance.ts"
click node_tier0 "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/providers/tier0Wasm.ts"
click node_model "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/assets/models/koen-e5-tiny/model-manifest.json"
click node_transport "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/providers/chatTransport.ts"
click node_llm "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/providers/openaiChat.ts"
click node_personas "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/personas.ts"
click node_popup "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/popup/popup.ts"
click node_settings "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/options/options.ts"
click node_reporting "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/sessionReport.ts"
click node_replay "https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/replay/replay.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_background,node_db,node_restore,node_page_excerpt,node_toast,node_offscreen toneBlue
class node_attention_evidence,node_gauge,node_session toneAmber
class node_domain_filter,node_relevance,node_tier0,node_model,node_transport,node_llm,node_personas toneMint
class node_popup,node_settings,node_reporting,node_replay toneRose
class node_chrome toneNeutral
```

## Components

### Extension runtime

- [Background control plane](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/background.ts): service worker · Owns session orchestration, badge state, and runtime coordination. · `apps/extension/src/background.ts`
- [Local durable store](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/db.ts): browser storage / IndexedDB · Local-first session, event, and judgment persistence. · `apps/extension/src/lib/db.ts`
- [Session restoration](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/sessionRestore.ts): state recovery · `apps/extension/src/lib/sessionRestore.ts`
- [Page excerpt collector](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/content/pageExcerpt.ts): content script · Extracts bounded text only for detailed assessment. · `apps/extension/src/content/pageExcerpt.ts`
- [Coaching overlay](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/content/toastOverlay.ts): content script · Renders non-blocking coaching in the current page. · `apps/extension/src/content/toastOverlay.ts`
- [Offscreen runtime](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/offscreen.ts): extension runtime · Hosts work unsuitable for the service worker. · `apps/extension/src/offscreen.ts`

### Attention & session core

- [Dwell & presence evidence](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/dwellScheduler.ts): attention measurement · `apps/extension/src/lib/dwellScheduler.ts`
- [Focus gauge reducer](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/core/gauge/reducer.ts): deterministic state machine · Applies tiers, cooldowns, snoozes, refunds, and intervention transitions. · `apps/extension/src/core/gauge/reducer.ts`
- [Session history](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/session.ts): event and session model · Reconstructs goals, events, judgments, and aggregates. · `apps/extension/src/lib/session.ts`

### Relevance & AI

- [Domain privacy filter](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/domainFilter.ts): observation policy · Excludes sensitive and user-filtered domains before observation. · `apps/extension/src/lib/domainFilter.ts`
- [Relevance orchestration](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/relevance.ts): tiered judgment policy · Selects local or opted-in higher-tier relevance assessment. · `apps/extension/src/lib/relevance.ts`
- [Local semantic classifier](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/providers/tier0Wasm.ts): ONNX/WASM inference · Korean/English Tier 0 relevance classification. · `apps/extension/src/providers/tier0Wasm.ts`
- [Bundled embedding model](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/assets/models/koen-e5-tiny/model-manifest.json): ONNX model asset · `apps/extension/assets/models/koen-e5-tiny/model-manifest.json`
- [Provider-neutral AI transport](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/providers/chatTransport.ts): AI request boundary · Forms prompts, parses responses, and handles provider failures. · `apps/extension/src/providers/chatTransport.ts`
- [Opted-in LLM providers](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/providers/openaiChat.ts): external AI services · OpenAI-compatible, Claude, and Ollama provider paths. · `apps/extension/src/providers/openaiChat.ts`
- [Persona configuration](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/personas.ts): coaching policy · Controls coaching voice and fallback copy. · `apps/extension/src/lib/personas.ts`

### User surfaces & reporting

- [Popup controls](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/popup/popup.ts): extension UI · Starts and controls focus sessions. · `apps/extension/src/popup/popup.ts`
- [Options & onboarding](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/options/options.ts): extension setup UI · Configures AI, privacy, local data, and initial choices. · `apps/extension/src/options/options.ts`
- [Session reports](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/lib/sessionReport.ts): aggregation and commentary · Builds end-of-session summaries and report cards. · `apps/extension/src/lib/sessionReport.ts`
- [Replay viewer & CLI](https://github.com/enu3379/kibitzer/blob/dev/apps/extension/src/replay/replay.ts): deterministic audit tool · Replays recorded events for debugging and audit. · `apps/extension/src/replay/replay.ts`

### Other components

- **Chrome signals**: browser API · Lifecycle, navigation, and activity signals.

## Connections

- Chrome signals → Background control plane: browser events
- Background control plane → Domain privacy filter: filter before observe
- Background control plane → Dwell & presence evidence: schedule dwell
- Dwell & presence evidence → Focus gauge reducer: attention evidence
- Background control plane → Relevance orchestration: request judgment
- Relevance orchestration → Local semantic classifier: Tier 0
- Bundled embedding model → Local semantic classifier: embeddings
- Relevance orchestration → Page excerpt collector: detailed-only excerpt
- Relevance orchestration → Provider-neutral AI transport: higher tiers
- Provider-neutral AI transport → Opted-in LLM providers: provider request
- Relevance orchestration → Focus gauge reducer: judgment outcome
- Persona configuration → Coaching overlay: coaching voice
- Focus gauge reducer → Coaching overlay: emit coaching
- Background control plane → Session history: record state
- Session history → Local durable store: persist locally
- Local durable store → Session restoration: reload after restart
- Session restoration → Background control plane: restore session
- Popup controls → Background control plane: session controls
- Options & onboarding → Background control plane: preferences
- Session history → Session reports: aggregate session
- Session history → Replay viewer & CLI: recorded events
- Background control plane → Offscreen runtime: delegate runtime work
