flagdoctor 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +69 -147
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,181 +1,103 @@
1
1
  # FlagDoctor
2
2
 
3
- **FlagDoctor** is a developer tool for diagnosing inconsistencies between feature-flag references in source code and the configuration available in Flagsmith.
3
+ Check that your code agrees with your [Flagsmith](https://flagsmith.com) feature-flag configuration — and see exactly where it doesn't.
4
4
 
5
- It scans repositories, obtains a normalized Flagsmith configuration snapshot, runs deterministic diagnostics, and presents findings through a CLI, CI output and a dashboard. Its companion visualization module, **FlagRadar**, shows the relationship between repositories, files, flags and environments.
5
+ FlagDoctor scans your source code (statically, read-only; it never runs your code), compares the flags it finds with a Flagsmith project and environment, and reports problems with file, line and column. It also maps which files, classes and functions each flag affects.
6
6
 
7
- ## Product question
7
+ ## Install
8
8
 
9
- > Does my code agree with the feature-flag configuration in this environment, and can I prove where it doesn't?
10
-
11
- ## Planned capabilities
12
-
13
- - Find code references to Flagsmith feature flags and remote configuration values.
14
- - Report references missing from the selected Flagsmith project/environment.
15
- - Detect references to archived flags and statically identifiable type mismatches.
16
- - Identify unresolved/dynamic references without guessing.
17
- - Compare flag presence and values across explicitly mapped environments.
18
- - Identify possible stale flags, with clear warnings about incomplete repository coverage.
19
- - Produce terminal, JSON and SARIF reports.
20
- - Visualize repository → file → flag → environment relationships.
21
- - Later integrate with GitHub/GitLab and CI pull-request checks.
22
-
23
- ## Architecture at a glance
24
-
25
- - `packages/core` — framework-independent scanning, Flagsmith adapter, snapshot normalization, diagnostic rules and reporters.
26
- - `packages/shared-types` — shared Zod schemas and TypeScript contracts.
27
- - `packages/cli` — local developer CLI built with Commander and Ink (`init` and `scan`).
28
- - `packages/api` — Fastify server, persistence and background scan orchestration (planned).
29
- - `packages/web` — React + TypeScript dashboard (planned).
30
-
31
- The CLI, dashboard and CI must share the same core engine. See `docs/ARCHITECTURE.md`.
32
-
33
- ## Development status
34
-
35
- Phase 0 (Project Foundation) is complete: the npm workspace, strict shared TypeScript configuration, thin package scaffolds, foundational shared contracts, quality tooling and safe environment conventions are in place. See `docs/ROADMAP.md` and `docs/phases/PHASE_0_FOUNDATION.md`.
36
-
37
- Phase 1A (local source provider + Flagsmith snapshot foundation) is complete: `@flagdoctor/core` provides a read-only `LocalProvider` and a Flagsmith Management API adapter that returns normalized snapshots.
38
-
39
- Phase 1B (static scanner) is complete: `@flagdoctor/core` exposes `scanSources()`, a keyword prefilter plus Tree-sitter AST extractors for JavaScript/TypeScript and Java, producing canonical `FlagReference` records.
40
-
41
- Phase 1C (Diagnostic rules, CLI and reports) is complete: `flagdoctor init` and `flagdoctor scan` are available; FD001, FD002, FD004 and FD006 are evaluated deterministically in core; terminal and JSON reporters consume the same `Finding[]` data. Dashboard, server, persistence and later-phase rules remain out of scope.
42
-
43
- Every scan also builds a flag dependency map — the files, classes and functions each flag affects — independent of whether a finding was raised. `flagdoctor graph` visualizes it in the terminal (or as JSON); the web dashboard's **Flag Dependencies** page (`/dependencies`) visualizes the same data from an imported report. `flagdoctor analyze` compiles the dependency map and findings into a compact context and sends it to an LLM via Groq to look for misconfigurations a deterministic rule cannot catch; its findings come back tagged `FDLLM` and merge into the same report.
44
-
45
- ## Prerequisites
46
-
47
- - Node.js 22.12 or newer (validated on Node.js 24).
48
- - npm 10 or newer (validated on npm 11). npm ships with Node.js; no global package-manager tooling is required.
49
-
50
- ## Local development
51
-
52
- Run these commands from the repository root:
9
+ Requires Node.js 22.12 or newer.
53
10
 
54
11
  ```bash
55
- npm install # install all workspace dependencies
56
- npm run build # build every package via TypeScript project references
57
- npm run typecheck # typecheck every package without emitting files
58
- npm run lint # lint TypeScript sources with ESLint
59
- npm run test # run all Vitest smoke tests
60
- npm run format:check # check formatting with Prettier
61
- npm run format # apply Prettier formatting
62
- npm run clean # remove build outputs
12
+ npm install -g flagdoctor
13
+ flagdoctor --help
63
14
  ```
64
15
 
65
- Use the CLI from the repository root after `npm run build`:
66
-
67
- ```bash
68
- npm run flagdoctor -- init
69
- npm run flagdoctor -- scan --env development
70
- npm run flagdoctor -- scan --env production
71
- npm run flagdoctor -- scan --env production --format json
72
-
73
- # Which files, classes and functions each flag affects (ASCII tree, or --format json)
74
- npm run flagdoctor -- graph --env production
75
- npm run flagdoctor -- graph --env production --flag some_flag_name
76
-
77
- # Ask an LLM (Groq) to review the dependency map + findings for misconfigurations
78
- # Requires GROQ_API_KEY in the environment or .env; see .env.example.
79
- npm run flagdoctor -- analyze --env production
80
- ```
16
+ Or run it without installing: `npx flagdoctor scan --env production`.
81
17
 
82
- The `FLAGSMITH_TOKEN` environment variable is required for `scan`; `init` never writes it to `flagdoctor.yaml`. Exit codes: `0` clean completed scan with warning/info only, `1` confirmed errors in a completed scan, `2` configuration/credential/API failure or incomplete (`partial`) source analysis. See `docs/MANUAL_SMOKE_TEST.md` for an optional real-account check.
83
-
84
- ### CLI, API and dashboard stay in sync
85
-
86
- Run the API (`npm run api:migrate`, then `npm run api:dev`) and the web app (`npm run web:dev`), then:
18
+ ## Quick start
87
19
 
88
20
  ```bash
89
- npm run flagdoctor -- login # approve this device in the browser (project-scoped credential)
90
- npm run flagdoctor -- init # prefills flagdoctor.yaml from the project and saves its environment aliases
91
- npm run flagdoctor -- scan --env production # report is uploaded; the dashboard shows it
92
- npm run flagdoctor -- graph --env production # flag dependency map is uploaded too
93
- npm run flagdoctor -- analyze --env production
94
- npm run flagdoctor -- scan --env production --no-sync # skip the upload
95
- ```
21
+ # 1. Sign in to your FlagDoctor dashboard (opens the browser; approve the code shown)
22
+ flagdoctor login --api-url https://your-flagdoctor.example.com
96
23
 
97
- - **Sync is best effort.** A failed upload is reported on stderr and never changes the exit code or stdout (so `--format json` pipes stay valid). Without `flagdoctor login` nothing is uploaded.
98
- - **Terminal UI.** In an interactive terminal the CLI renders with Ink: spinners, coloured finding and dependency-tree screens, the login code screen. In CI, pipes, `--ci`, `--format json` and tests it prints the same plain text as before. The guided `init` prompts are still line-based. `login`, `init` and a bare `flagdoctor` open with a short animated welcome screen (set `FLAGDOCTOR_NO_SPLASH=1` to turn it off); it never plays in CI, pipes or `--format json`.
99
- - **AI analysis (Qwen on Groq).** Set `GROQ_API_KEY` (and optionally `GROQ_MODEL`, default `qwen/qwen3.8-27b`) in `.env`. `flagdoctor analyze --env <name>` sends the scan's findings and flag dependency map (no source code, snippets or tokens) to the model and prints its review; the web app's **Analyze with AI** button (scan results and FlagRadar pages) does the same from the server, which is the only place the key is read. Either way the result is stored as a new `analyze` scan with a summary and `FDLLM` findings. AI findings are low-confidence hints: they are capped at `warning` and can never change the exit code.
100
- - **What is stored.** Each upload is one row in `scans` (summary columns plus the full report as JSONB: findings, diagnostics and the flag dependency map). Tokens and source snippets are never included.
101
- - **Web pages read the API.** Dashboard, Scan History, Scan Results (with delete), FlagRadar (the flag dependency graph and list; `/dependencies` redirects there), Environment Comparison, Repositories and Settings load from the active project. New Scan can import a report file or run a scan on the server against a saved local codebase (`FLAGDOCTOR_SCAN_ROOT` bounds which directories the API may read; the Flagsmith token stays server-side).
24
+ # 2. In your repository: create flagdoctor.yaml (guided; picks your Flagsmith project and environments)
25
+ cd my-app
26
+ flagdoctor init
102
27
 
103
- | Who | Endpoint |
104
- | --- | --- |
105
- | CLI (bearer) | `POST /api/v1/cli/scans`, `GET`/`PUT /api/v1/cli/project-config` |
106
- | Web (session + CSRF) | `GET /api/v1/workspaces/:w/projects/:p/scans`, `GET`/`DELETE .../scans/:id`, `POST .../scans/import`, `POST .../scans/run`, `GET .../dependencies`, `DELETE .../codebases/:id` |
28
+ # 3. Provide your Flagsmith Organisation API token (never written to flagdoctor.yaml)
29
+ export FLAGSMITH_TOKEN=... # PowerShell: $env:FLAGSMITH_TOKEN = "..."
107
30
 
108
- Optional variables: `FLAGDOCTOR_API_URL` (default `http://localhost:3001`) and `FLAGDOCTOR_WEB_URL` (default `http://localhost:5173`, used for the dashboard link printed after a sync).
109
-
110
- ### Guided setup (Windows)
31
+ # 4. Scan
32
+ flagdoctor scan --env production
33
+ ```
111
34
 
112
- `setup.bat` installs all workspace dependencies, creates a blank `.env` from `.env.example` (if absent), then runs the guided `init` wizard. `run.bat` builds every package and starts the CLI; pass arguments through, e.g. `run.bat scan --env development`.
35
+ The report is uploaded to your dashboard automatically after `login`. Without `login`, FlagDoctor works fully offline from the dashboard: it just prints the report.
113
36
 
114
- In an interactive terminal, `run.bat init` asks for:
37
+ ## Commands
115
38
 
116
- - repository source — local path, or GitHub/GitLab (base URL and optional personal access token, defaulting to `https://github.com` / `https://gitlab.com`); a remote repository is cloned into a local folder with `git clone --depth 1`, then scanned locally. No remote source provider is implemented and no repository code is executed.
117
- - Flagsmith Management API base URL and Organisation API token — the token is masked while typing and saved only to the local, gitignored `.env`, never to `flagdoctor.yaml`; a git PAT is used only in memory for the import.
118
- - optional live sync — reads projects and environments through read-only Admin API GET requests so you can pick a project and map environment aliases; if sync fails, you can enter the project and mapping manually.
119
- - include/exclude globs.
39
+ | Command | What it does |
40
+ | ------------------------------------------------ | ------------------------------------------------------------------------------------------------- |
41
+ | `flagdoctor login [--api-url <url>]` | Authorize this machine through the browser. Stores a project-scoped credential outside your repo. |
42
+ | `flagdoctor whoami` | Show the signed-in project. |
43
+ | `flagdoctor init [--ci] [--force]` | Create `flagdoctor.yaml`. `--ci` writes a starter file without prompts. |
44
+ | `flagdoctor scan --env <alias>` | Scan the repository and report findings. |
45
+ | `flagdoctor graph --env <alias> [--flag <name>]` | Show which files, classes and functions each flag affects. |
46
+ | `flagdoctor analyze --env <alias>` | AI review of the findings and dependency map (needs `GROQ_API_KEY`). |
120
47
 
121
- For CI or scripted use, `run.bat init --ci` (or a non-TTY session) writes the safe starter config without prompts. `scan` also loads `.env` from the workspace so a saved token is available on Node 22 as well as Node 24.
48
+ Common options: `--format json` (machine-readable report), `--root <path>` (scan another directory), `-c, --config <path>`, `--ci` (plain output), `--no-sync` (don't upload).
122
49
 
123
- Individual packages also expose `build` and `typecheck` scripts, for example `npm run build --workspace @flagdoctor/core`. All agents must also follow `AGENTS.md`, which documents the mandatory Git commit and push workflow.
50
+ ## What it reports
124
51
 
125
- The cinematic homepage lives in `packages/web` (React + Vite). Run `npm run web:dev` for local development or `npm run web:build` to produce the static site in `packages/web/site-dist/` (ignored by Git). The existing `@flagdoctor/web` library entry point and its smoke test are unchanged.
52
+ | Rule | Meaning |
53
+ | ----- | ------------------------------------------------------------------------------- |
54
+ | FD001 | Code references a flag that doesn't exist in the Flagsmith environment (error). |
55
+ | FD002 | Code references an archived flag (error). |
56
+ | FD004 | A flag's value type doesn't match how the code reads it (warning). |
57
+ | FD006 | A flag name can't be resolved statically — reported, never guessed (info). |
58
+ | FDLLM | AI-suggested hints from `analyze`; low confidence, never fail a build. |
126
59
 
127
- There is nothing to configure before running the checks. Copy `.env.example` to `.env` only if you need to supply future local credentials; never commit `.env` or put real tokens in source files.
60
+ A disabled flag is not an error. JavaScript/TypeScript and Java are supported (Flagsmith JS and Java SDKs).
128
61
 
129
- ## Workspace layout
62
+ ## Use in CI
130
63
 
131
- ```text
132
- packages/
133
- ├── core/ # @flagdoctor/core — scanner, adapter, rules and reporters
134
- ├── shared-types/ # @flagdoctor/shared-types — canonical schemas/types
135
- ├── cli/ # @flagdoctor/cli — Commander + Ink CLI
136
- ├── api/ # @flagdoctor/api — placeholder (Phase 2)
137
- └── web/ # @flagdoctor/web — placeholder (Phase 2)
64
+ ```bash
65
+ flagdoctor scan --env production --ci --format json > flagdoctor-report.json
138
66
  ```
139
67
 
140
- All packages retain their Phase 0 smoke tests. `api` and `web` remain placeholders; no dashboard or server features are implemented.
68
+ Exit codes: `0` clean (warnings/info only), `1` confirmed errors, `2` configuration, credential or API failure, or an incomplete scan.
141
69
 
142
- ## Phase 1A foundation
70
+ ## Configuration
143
71
 
144
- - `LocalProvider` lists stable, POSIX-relative source files; it respects root and nested `.gitignore` files, include/exclude patterns, file-size/file-count limits and never follows symlinks or executes repository code.
145
- - `FlagsmithAdapter` reads only Management API GET routes (`/projects/`, `/environments/`, `/projects/{id}/features/`, `/environments/{api_key}/featurestates/`) and normalizes them into `FlagSnapshot`.
146
- - `flagdoctor.yaml` accepts only `repository.root/include/exclude` and `flagsmith.baseUrl/project` plus an explicit `flagsmith.environments` alias map (or a single `environment`). Secret-looking keys are rejected; supply the Organisation API token through `FLAGSMITH_TOKEN` in the environment only.
147
- - Raw API DTOs stay private to the adapter. Errors are typed and token values are redacted from messages and serialized errors.
72
+ `flagdoctor.yaml` (created by `init`) holds only non-secret settings:
148
73
 
149
- ## Phase 1B scanner
150
-
151
- - `scanSources({ provider })` lists files through a `SourceProvider`, skips irrelevant files with a text prefilter, then uses Tree-sitter AST extractors as the only source of flag references.
152
- - Supported calls: Flagsmith JS `hasFeature`/`getValue` and Java `Flags.isFeatureEnabled`/`getFeatureValue`/`getFlag`. Only calls on verified SDK receivers are accepted; wrappers can be registered explicitly.
153
- - Literal and same-file scope-safe constants resolve to names; anything else becomes `flagName: null`, `resolution: 'dynamic'`.
154
- - `FlagReference` is the canonical shared contract in `@flagdoctor/shared-types`. Parse/read errors become per-file `ScanDiagnostic` entries and never abort the rest of the scan.
155
-
156
- ## Phase 1C diagnostics and CLI
74
+ ```yaml
75
+ version: 1
76
+ repository:
77
+ root: .
78
+ include: []
79
+ exclude: []
80
+ flagsmith:
81
+ baseUrl: https://api.flagsmith.com/api/v1
82
+ project: 12345
83
+ environments:
84
+ production: <environment-api-key>
85
+ development: <environment-api-key>
86
+ ```
157
87
 
158
- - `evaluateFindings()` applies FD001 (missing flag, high-confidence error with close-name suggestion), FD002 (archived reference, error), FD004 (demonstrably incompatible value type, warning) and FD006 (dynamic reference, info). Disabled flags are never errors.
159
- - FD004 is SDK-aware: it is evaluated only for the verified pairs `flagsmith-js-client + getValue` and `flagsmith-java-client + getFeatureValue`. Enabled-state checks (`hasFeature`, `isFeatureEnabled`), object-returning `getFlag`, registered wrappers and unknown SDK/method pairs never produce type-mismatch findings; wrapper configs cannot claim a reserved built-in SDK identity.
160
- - A failed, partial or malformed snapshot produces exit code `2` with **no** rule findings, so missing-flag findings cannot be inferred from absent data.
161
- - Source parse/read errors mark the report `status: "partial"` (preserving diagnostics) and return exit code `2`; a clean completed scan is required for exit code `0`.
162
- - `--env` selects a configured environment alias and resolves it through `flagsmith.environments`; branch names are never assumed to equal Flagsmith environments.
163
- - `formatJsonReport()` and `formatTerminalReport()` both consume the same schema-validated `ScanReport`/`Finding[]`; JSON is versioned (`schemaVersion: 1`) and includes stable finding IDs.
164
- - `--ci`, JSON output and non-TTY execution never prompt; Ink is used only for interactive TTY terminal output.
88
+ Secrets come from the environment only:
165
89
 
166
- ## Security note
90
+ | Variable | Used for |
91
+ | ------------------------ | -------------------------------------------------------------------------------------------- |
92
+ | `FLAGSMITH_TOKEN` | Flagsmith Organisation API token (required for `scan`, `graph`, `analyze`). |
93
+ | `GROQ_API_KEY` | Optional, enables `analyze`. Only findings and the dependency map are sent — no source code. |
94
+ | `FLAGDOCTOR_API_URL` | Dashboard server to sync with (alternative to `login --api-url`). |
95
+ | `FLAGDOCTOR_NO_SPLASH=1` | Turn off the welcome animation. |
167
96
 
168
- Flagsmith Organisation API tokens are secrets. They must remain server-side/local-only, must never be committed or logged, and must never be exposed to browser code. FlagDoctor starts read-only and must never modify remote Flagsmith configuration as part of a scan. The client-side environment key is a different credential from the Organisation API token.
97
+ A `.env` file in the working directory is also read.
169
98
 
170
- ## Documentation
99
+ ## Privacy and safety
171
100
 
172
- - `AGENTS.md` — persistent rules for all coding agents, including the mandatory Git workflow.
173
- - `CLAUDE.md` — persistent instructions for Claude Code.
174
- - `docs/PRODUCT_SPEC.md` — product and diagnostic requirements.
175
- - `docs/ARCHITECTURE.md` — architecture and package responsibilities.
176
- - `docs/ROADMAP.md` — delivery phases.
177
- - `docs/DECISIONS.md` — important engineering decisions.
178
- - `docs/phases/PHASE_0_FOUNDATION.md` — Phase 0 acceptance criteria.
179
- - `docs/MANUAL_SMOKE_TEST.md` — optional real-Flagsmith-account smoke test (no tokens included).
180
- - `docs/phases/PHASE_1_CLI_MVP.md` — Phase 1 CLI MVP specification.
181
- - `prompts/` — phase implementation prompts (1A complete; 1B and 1C planned).
101
+ - Scans are static and read-only; repository code is never executed.
102
+ - Tokens are never printed, logged, or written to `flagdoctor.yaml`.
103
+ - Nothing in Flagsmith is modified.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagdoctor",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Compare feature-flag usage in your code with a Flagsmith environment",
5
5
  "type": "module",
6
6
  "bin": {