flagdoctor 0.0.0-stage → 0.1.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.
- package/README.md +180 -2
- package/dist/index.js +4234 -0
- package/package.json +24 -4
package/README.md
CHANGED
|
@@ -1,3 +1,181 @@
|
|
|
1
|
-
#
|
|
1
|
+
# FlagDoctor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**FlagDoctor** is a developer tool for diagnosing inconsistencies between feature-flag references in source code and the configuration available in Flagsmith.
|
|
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.
|
|
6
|
+
|
|
7
|
+
## Product question
|
|
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:
|
|
53
|
+
|
|
54
|
+
```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
|
|
63
|
+
```
|
|
64
|
+
|
|
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
|
+
```
|
|
81
|
+
|
|
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:
|
|
87
|
+
|
|
88
|
+
```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
|
+
```
|
|
96
|
+
|
|
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).
|
|
102
|
+
|
|
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` |
|
|
107
|
+
|
|
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)
|
|
111
|
+
|
|
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`.
|
|
113
|
+
|
|
114
|
+
In an interactive terminal, `run.bat init` asks for:
|
|
115
|
+
|
|
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.
|
|
120
|
+
|
|
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.
|
|
122
|
+
|
|
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.
|
|
124
|
+
|
|
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.
|
|
126
|
+
|
|
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.
|
|
128
|
+
|
|
129
|
+
## Workspace layout
|
|
130
|
+
|
|
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)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
All packages retain their Phase 0 smoke tests. `api` and `web` remain placeholders; no dashboard or server features are implemented.
|
|
141
|
+
|
|
142
|
+
## Phase 1A foundation
|
|
143
|
+
|
|
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.
|
|
148
|
+
|
|
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
|
|
157
|
+
|
|
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.
|
|
165
|
+
|
|
166
|
+
## Security note
|
|
167
|
+
|
|
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.
|
|
169
|
+
|
|
170
|
+
## Documentation
|
|
171
|
+
|
|
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).
|