@firstdraft.com/cli 0.8.0 → 0.8.2

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 CHANGED
@@ -4,13 +4,13 @@ firstdraft is the command-line client shared by First Draft agents and automatio
4
4
  files, calls the versioned Service API, exposes reviewed analysis and GapSets, materializes verified Compilations,
5
5
  and coordinates private GitHub publication.
6
6
 
7
- Start with the [local development guide](https://gist.github.com/raghubetina/3d424a97a1eaa6de8c406e67f32a237e):
7
+ Start with the [local development guide](https://firstdraft.github.io/firstdraft/docs/guides/local-app.html):
8
8
  install the CLI and Skill, then compile into your current folder with `firstdraft plan compile --output .`. No Drawing Board
9
9
  clone or GitHub push is required. The [Drawing Board guide](https://github.com/firstdraft/drawing-board#build-an-app-with-first-draft)
10
10
  is the Codespaces fallback.
11
11
 
12
- CLI 0.4 and later make `--output .` the default. Keep the explicit flag with CLI 0.3, whose zero-flag command selects GitHub
13
- publication.
12
+ `--output .` is also the default, so `firstdraft plan compile` alone writes to the current folder. GitHub publication
13
+ requires `--github`.
14
14
 
15
15
  ## What this repository owns
16
16
 
@@ -18,7 +18,7 @@ publication.
18
18
  - conditional whole-document push and conflict reporting;
19
19
  - analysis polling and complete GapSet output;
20
20
  - direct Compile-and-materialize and private publish orchestration;
21
- - retained Compilation inspection and artifact download;
21
+ - retained Compilation inspection, cancellation, and artifact download;
22
22
  - terminal output, exit status, and recovery contracts;
23
23
  - the dependency-free npm package; and
24
24
  - package provenance and publication.
@@ -33,21 +33,22 @@ repository owns the exact command and transport behavior between them.
33
33
  | Change the CLI | [Agent instructions](https://github.com/firstdraft/cli/blob/main/AGENTS.md), then [documentation map](docs/README.md) |
34
34
  | Find a command or output contract | [Command reference](docs/commands.md) |
35
35
  | Interpret an error or recover safely | [Errors and recovery](docs/errors.md) |
36
- | Prepare or publish a package | [Release runbook](RELEASING.md) |
37
- | Inspect dated package observations | [Release history](docs/release-history.md) |
36
+ | Commit, review, or land a change | [Contributing guide](https://github.com/firstdraft/cli/blob/main/CONTRIBUTING.md) |
37
+ | Prepare or publish a package | [Release runbook](https://github.com/firstdraft/cli/blob/main/RELEASING.md) |
38
+ | See what changed in a version | [Changelog](https://github.com/firstdraft/cli/blob/main/CHANGELOG.md) |
38
39
  | Report a vulnerability | [Security policy](SECURITY.md) |
39
40
 
40
41
  Run firstdraft --help or a command group's --help for concise terminal syntax.
41
42
 
42
43
  ## Repository layout
43
44
 
44
- | Path | Responsibility |
45
- | -------- | ------------------------------------------------------------- |
46
- | bin/ | Published executable entrypoint |
47
- | src/ | Commands, API client, local Plan state, and output contracts |
48
- | test/ | Command, protocol, recovery, and package tests |
49
- | scripts/ | Test runner and package allowlist/smoke checks |
50
- | docs/ | Command, error, release-history, and maintainer documentation |
45
+ | Path | Responsibility |
46
+ | -------- | ------------------------------------------------------------ |
47
+ | bin/ | Published executable entrypoint |
48
+ | src/ | Commands, API client, local Plan state, and output contracts |
49
+ | test/ | Command, protocol, recovery, and package tests |
50
+ | scripts/ | Test runner, package checks, and version sync |
51
+ | docs/ | Documentation map and command and error references |
51
52
 
52
53
  ## Development
53
54
 
@@ -75,8 +76,8 @@ To exercise the checkout directly:
75
76
  node bin/firstdraft.js --help
76
77
  ```
77
78
 
78
- Remote commands default to production and read `FIRSTDRAFT_API_TOKEN`. Use `firstdraft --staging plan compile`
79
- and a separate `FIRSTDRAFT_STAGING_API_TOKEN` for staging. Existing Projects retain their saved origin. See
79
+ Remote commands default to production. Run `firstdraft login` (or `firstdraft --staging login`) once, or set
80
+ `FIRSTDRAFT_API_TOKEN` / `FIRSTDRAFT_STAGING_API_TOKEN`. Existing Projects retain their saved origin. See
80
81
  [environment selection](docs/commands.md#select-an-environment-and-authenticate) for custom URLs and credential
81
82
  isolation. Keep tokens out of arguments, shell history, fixtures, snapshots, and logs.
82
83
 
@@ -88,8 +89,8 @@ The published CLI supports Node.js 22 or newer. Direct automation callers can in
88
89
  npm install --global @firstdraft.com/cli
89
90
  ```
90
91
 
91
- Pin an exact compatible version when a repeatable installation matters; [RELEASING.md](RELEASING.md) owns channel
92
- and release meaning.
92
+ Pin an exact compatible version when a repeatable installation matters. The
93
+ [changelog](https://github.com/firstdraft/cli/blob/main/CHANGELOG.md) describes each version.
93
94
 
94
95
  The published package:
95
96
 
@@ -97,14 +98,9 @@ The published package:
97
98
  - runs reviewed JavaScript source directly;
98
99
  - has no runtime dependencies or install scripts;
99
100
  - performs no telemetry, update check, or network request unless the caller invokes an API command;
100
- - reads Bearer credentials only from the environment; and
101
+ - reads Bearer credentials from the environment or its private `firstdraft login` credentials file; and
101
102
  - carries npm provenance linking registry bytes to its GitHub workflow and commit.
102
103
 
103
- Inspect the packed file list whenever a source or documentation path moves. The public documentation graph,
104
- including the release runbook and dated release history, ships with the package. `AGENTS.md` and the source-only
105
- `release/compatibility.json` do not.
106
-
107
- ## Release boundary
108
-
109
- Merging source is not package publication. An approved coordinated release publishes directly to `latest`, reusing
110
- successful CI for the exact source. [RELEASING.md](RELEASING.md) owns the short release and recovery procedure.
104
+ The package includes this README, the documentation map, the command and error references, and the security
105
+ policy. Repository files for maintainers stay out of it: `AGENTS.md`, the release runbook, the changelog, and the
106
+ source-only `release/compatibility.json`. Inspect the packed file list whenever a source or documentation path moves.
package/docs/README.md CHANGED
@@ -5,36 +5,31 @@ evidence for implemented behavior; if they contradict a document, surface the co
5
5
 
6
6
  | Task | Read first |
7
7
  | -------------------------------- | ------------------------------------------------------------------------------------------------ |
8
- | Local app development | [Local guide](https://gist.github.com/raghubetina/3d424a97a1eaa6de8c406e67f32a237e) |
8
+ | Local app development | [Local guide](https://firstdraft.github.io/firstdraft/docs/guides/local-app.html) |
9
9
  | Codespaces fallback | [Drawing Board guide](https://github.com/firstdraft/drawing-board#build-an-app-with-first-draft) |
10
10
  | Installation or package contract | [Root README](../README.md) |
11
- | Commands, API, or output | [Command reference](commands.md) |
11
+ | Commands, Service API, or output | [Command reference](commands.md); [add a command](commands.md#add-a-command) |
12
12
  | Errors and recovery | [Errors and recovery](errors.md) |
13
- | Versioning and publication | [Release policy and runbook](../RELEASING.md) |
14
- | Dated release observations | [Release history](release-history.md) |
13
+ | Versioning and publication | [Release runbook](https://github.com/firstdraft/cli/blob/main/RELEASING.md) |
14
+ | Changes by version | [Changelog](https://github.com/firstdraft/cli/blob/main/CHANGELOG.md) |
15
15
  | Vulnerability reporting | [Security policy](../SECURITY.md) |
16
16
 
17
17
  ## Authority boundaries
18
18
 
19
19
  - [README.md](../README.md) owns repository orientation, direct installation, package boundaries, and routes.
20
- - [commands.md](commands.md) owns detailed command semantics. Built-in `--help`, runtime source, and tests own exact
21
- executable syntax and behavior.
20
+ - [commands.md](commands.md) owns detailed command semantics, Service endpoints, and the add-a-command checklist.
21
+ Built-in `--help`, runtime source, and tests own exact executable syntax and behavior.
22
22
  - [errors.md](errors.md) owns handled-error interpretation and recovery guidance.
23
- - [RELEASING.md](../RELEASING.md) owns living release policy and the operator runbook.
24
- - [release-history.md](release-history.md) preserves dated release observations. Recheck live tags, package versions,
25
- and dist-tags before relying on them operationally; publisher configuration is checked when it changes or fails.
23
+ - `RELEASING.md` owns publication mechanics. The service repository owns release policy.
24
+ - `CHANGELOG.md` gets each new version's entry in the pull request that sets the version.
26
25
  - The source repository's `AGENTS.md` routes agent work; it should stay compact rather than duplicate these documents.
26
+ Its `CLAUDE.md` only imports `AGENTS.md`, so Claude Code and Codex read the same instructions.
27
27
 
28
28
  ## Retrieval quality
29
29
 
30
30
  Start here, then load the one owning document for the task. Follow a cross-link only when the task crosses an
31
31
  authority boundary. Create another page only for a distinct audience, task, or authority.
32
32
 
33
- The documentation tests keep `AGENTS.md` at or below 2 KiB, the root README at or below 6 KiB, and this map at or
34
- below 4 KiB. They also require every public topic to remain reachable from this map or the root README and verify
35
- repository-local links and fragments. The package check separately verifies that every relative link in the
36
- packaged Markdown resolves inside that exact package.
37
-
38
33
  ## Work on the repository
39
34
 
40
35
  Development uses Node.js 24.18.0 and npm 11.16.0, pinned in `.tool-versions`. From a fresh checkout:
package/docs/commands.md CHANGED
@@ -3,64 +3,128 @@
3
3
  This page owns the detailed public semantics of the current command surface. Run `firstdraft --help` or a command
4
4
  group's `--help` for concise executable syntax. See [Errors and recovery](errors.md) before retrying a failed mutation.
5
5
 
6
- The current `0.8.x` source line contains the auditable command shell, local Foundation Plan initialization, local
6
+ The current `0.8.x` source line contains the auditable command shell, browser and device login, local Foundation Plan initialization, local
7
7
  application-key and UUID generation, conditional whole-document push, whole-graph analysis status polling, direct
8
- Compile-and-materialize and private publish orchestration, and retained-Compilation inspection. CLI `0.8.x`
9
- requires the service's `0.7.x` API contract. See the [release policy](../RELEASING.md) for versioning and channel
10
- semantics and [release history](release-history.md) for the transition from prereleases.
8
+ Compile-and-materialize and private publish orchestration, and retained-Compilation inspection and cancellation.
9
+ CLI `0.8.x` requires the service's `0.7.x` API contract. The
10
+ [changelog](https://github.com/firstdraft/cli/blob/main/CHANGELOG.md) describes each version.
11
11
 
12
12
  ## Command map
13
13
 
14
- | Command | Network | Purpose |
15
- | ------------------------------------- | ------- | ---------------------------------------------------------- |
16
- | `firstdraft plan init` | No | Create an empty local Foundation Plan and Project identity |
17
- | `firstdraft generate application-key` | No | Preview deterministic name-to-key derivation |
18
- | `firstdraft generate uuid` | No | Generate one or more Foundation Plan subject identities |
19
- | `firstdraft plan push` | Yes | Conditionally submit the exact whole Plan |
20
- | `firstdraft plan status` | Yes | Read or wait for the current whole-graph analysis |
21
- | `firstdraft plan compile` | Yes | Push and analyze, then materialize in the current folder |
22
- | `firstdraft plan compile --github` | Yes | Push and analyze, then publish to private GitHub |
23
- | `firstdraft compilation status` | Yes | Inspect a retained Compilation by ID |
24
- | `firstdraft compilation download` | Yes | Verify and materialize a successful retained Compilation |
14
+ | Command | Network | Purpose |
15
+ | ------------------------------------- | ------- | ----------------------------------------------------------- |
16
+ | `firstdraft plan init` | No | Create an empty local Foundation Plan and Project identity |
17
+ | `firstdraft generate application-key` | No | Preview deterministic name-to-key derivation |
18
+ | `firstdraft generate uuid` | No | Generate one or more Foundation Plan subject identities |
19
+ | `firstdraft login` | Yes | Approve the CLI in a browser and save a token for an origin |
20
+ | `firstdraft logout` | Yes | Revoke and remove the saved token for an origin |
21
+ | `firstdraft plan push` | Yes | Conditionally submit the exact whole Plan |
22
+ | `firstdraft plan status` | Yes | Read or wait for the current whole-graph analysis |
23
+ | `firstdraft plan compile` | Yes | Push and analyze, then materialize in the current folder |
24
+ | `firstdraft plan compile --github` | Yes | Push and analyze, then publish to private GitHub |
25
+ | `firstdraft compilation status` | Yes | Inspect a retained Compilation by ID |
26
+ | `firstdraft compilation download` | Yes | Verify and materialize a successful retained Compilation |
27
+ | `firstdraft compilation cancel` | Yes | Cancel a queued or running Compilation by ID |
25
28
 
26
29
  ## Select an environment and authenticate
27
30
 
28
- Production at `https://firstdraft.com` is the default. Create a token at
29
- [First Draft](https://firstdraft.com/api-tokens) and provide it through `FIRSTDRAFT_API_TOKEN` when running a network
30
- command. Keep token values out of shell history and command arguments.
31
+ Production at `https://firstdraft.com` is the default. Log in once per environment:
31
32
 
32
33
  ```sh
33
- firstdraft plan push
34
+ firstdraft login
34
35
  ```
35
36
 
36
- For staging, create a separate token at [First Draft staging](https://staging.firstdraft.com/api-tokens), provide it
37
- through `FIRSTDRAFT_STAGING_API_TOKEN`, and select staging on the first remote command:
37
+ The command prints an authorization URL on standard error. Open it in a browser on the same machine, sign in, and
38
+ approve **First Draft CLI**. The CLI then saves a token and prints `Logged in to <origin>` on standard output.
39
+
40
+ For staging, select it on the login and on the first remote command:
38
41
 
39
42
  ```sh
43
+ firstdraft --staging login
40
44
  firstdraft --staging plan push
41
45
  firstdraft plan compile --staging
42
46
  ```
43
47
 
44
- `--staging` may precede the command group or appear among a remote command's options. It selects
45
- `https://staging.firstdraft.com`. `plan init` and `generate` remain local; a global flag on a local command does not
46
- save an environment selection. The first successful push, including the push within `plan compile`, pins the API
47
- origin in `.firstdraft/state.json`.
48
+ ### How login works
49
+
50
+ The default login uses the OAuth 2.0 authorization code flow with PKCE (`S256`) and a loopback redirect
51
+ (RFC 8252). The CLI listens on `127.0.0.1` at an ephemeral port for one `GET /callback`, sends a random `state`
52
+ and code challenge, and waits up to five minutes. Only a callback with the exact `state` is accepted. Requests
53
+ for other paths, with a missing or different `state`, or with another `Host` get an error page and do not end
54
+ the wait, so a stray local request can neither inject a code nor cancel the login. The first matching callback
55
+ closes the listener and shows a page that says you can close the tab. The CLI then exchanges the single-use code
56
+ and its PKCE verifier at `POST /oauth/token`. The token never appears in a URL.
57
+
58
+ On a machine without a browser, such as an SSH session or a container, use the device flow (RFC 8628):
59
+
60
+ ```sh
61
+ firstdraft login --interactive # also -i or --device
62
+ ```
63
+
64
+ The CLI prints a verification URL, a short user code, and a URL that already includes the code. Open either URL on
65
+ any device, confirm the code, and approve. The CLI polls at the server's interval, adds five seconds when asked to
66
+ slow down, and stops when the code expires.
67
+
68
+ Both flows send this machine's hostname as the device name shown on the approval page and in the token's name on
69
+ `/api-tokens`. A denied approval exits with `authorization_denied`. A login that is not approved in time exits with
70
+ `authorization_expired`. Other failures exit with `login_failed`. No credential is saved unless the login succeeds.
71
+ See [Errors and recovery](errors.md#login-and-logout-errors).
72
+
73
+ ### Saved credentials
74
+
75
+ The token is saved in `$XDG_CONFIG_HOME/firstdraft/credentials.json`, or `~/.config/firstdraft/credentials.json`
76
+ when `XDG_CONFIG_HOME` is unset or not absolute. The directory is created with mode `0700`. The file is written
77
+ with mode `0600` through a temporary file and an atomic rename. Each update holds `credentials.json.lock` in the
78
+ same directory from its read to its rename, so concurrent commands cannot drop each other's entries. An update
79
+ waits up to 10 seconds for another command's lock, then fails with reason `credentials_locked`. The CLI never removes
80
+ a lock by itself: if no other `firstdraft` command is running, the lock was left by one that exited mid-update, and
81
+ you can delete it. The file stores one entry per exact origin, so production, staging, and local development logins
82
+ can exist side by side. Logging in again to the same origin replaces its
83
+ entry. Run `firstdraft logout` first if the old token should also be revoked. The CLI never prints a saved token or
84
+ writes it to `.firstdraft`.
85
+
86
+ `firstdraft logout` (or `firstdraft --staging logout`) asks First Draft to revoke the saved token for the selected
87
+ origin through `POST /oauth/revoke`. It then removes the local entry even when revocation cannot be confirmed; in
88
+ that case it says so on standard error, and you can revoke the token on `/api-tokens`. With nothing saved, it
89
+ reports that and exits 0. If a newer login saved a different token for the origin while revocation was pending,
90
+ logout keeps that token and says so on standard error. If the local update fails after revocation, logout exits with `logout_failed` and reports
91
+ whether revocation was confirmed; see [Errors and recovery](errors.md#login-and-logout-errors). Logout never changes
92
+ token environment variables.
93
+
94
+ ### Environment tokens
95
+
96
+ Tokens from the environment still work and take precedence over a saved login. Create one on
97
+ [First Draft](https://firstdraft.com/api-tokens) and provide it through `FIRSTDRAFT_API_TOKEN`. For staging, create a
98
+ separate token at [First Draft staging](https://staging.firstdraft.com/api-tokens) and provide it through
99
+ `FIRSTDRAFT_STAGING_API_TOKEN`. Keep token values out of shell history and command arguments. `login` and `logout`
100
+ note on standard error when an environment token will override the saved login.
101
+
102
+ ### Origins and projects
103
+
104
+ `--staging` may precede the command group or appear among a remote command's options, including `login` and
105
+ `logout`. It selects `https://staging.firstdraft.com`. `plan init` and `generate` remain local; a global flag on a
106
+ local command does not save an environment selection. The first successful push, including the push within
107
+ `plan compile`, pins the API origin in `.firstdraft/state.json`.
48
108
 
49
109
  Existing Projects keep their pinned origin with or without the flag. A CLI upgrade does not migrate a Project or its
50
110
  credentials. A staging flag that disagrees with a Project's pin stops before any request.
51
111
  To work with another environment, initialize a separate project directory and submit the Plan there; do not edit
52
112
  the existing Project's private state to redirect it.
53
113
 
54
- `FIRSTDRAFT_API_URL` remains available for an initial custom HTTPS origin or loopback HTTP development server.
55
- `--staging` together with a different URL is an error; the equivalent normalized staging URL is allowed. Later
56
- pushes and compilation reject an override that differs from the pin. Read-only status and retained download
57
- commands use the pin and ignore `FIRSTDRAFT_API_URL` unless checking its conflict with an explicit `--staging`.
58
-
59
- Every remote command selects credentials from its effective origin: the exact `https://staging.firstdraft.com`
60
- origin requires `FIRSTDRAFT_STAGING_API_TOKEN`; production and custom origins use `FIRSTDRAFT_API_TOKEN`. Neither
61
- token is a fallback for the other. This includes existing staging Projects and retained status or artifact reads,
62
- even when no flag is supplied. Upgrading from CLI `0.6.x` therefore requires moving the staging credential to
63
- `FIRSTDRAFT_STAGING_API_TOKEN`; production tokens stay in `FIRSTDRAFT_API_TOKEN`.
114
+ `FIRSTDRAFT_API_URL` remains available for an initial custom HTTPS origin or loopback HTTP development server, and
115
+ it also selects the origin for `login` and `logout`, for example `FIRSTDRAFT_API_URL=http://127.0.0.1:3000
116
+ firstdraft login`. `--staging` together with a different URL is an error; the equivalent normalized staging URL is
117
+ allowed. Later pushes and compilation reject an override that differs from the pin. Status, retained download, and
118
+ cancel commands use the pin and ignore `FIRSTDRAFT_API_URL` unless checking its conflict with an explicit
119
+ `--staging`.
120
+
121
+ Every remote command selects credentials from its effective origin. First it uses the environment: the exact
122
+ `https://staging.firstdraft.com` origin uses `FIRSTDRAFT_STAGING_API_TOKEN`; production and custom origins use
123
+ `FIRSTDRAFT_API_TOKEN`. When that variable is unset or empty, it uses the token saved by `firstdraft login` for that
124
+ exact origin. No token is a fallback for a different origin: neither environment variable substitutes for the other,
125
+ and a saved login for one origin never authenticates another. This includes existing staging Projects and retained
126
+ status or artifact reads, even when no flag is supplied. An unreadable or malformed credentials file counts as no
127
+ saved login.
64
128
 
65
129
  The CLI sends the selected token as a Bearer credential on every API request. It does not save it in `.firstdraft`,
66
130
  print it, or require it for local commands. Revoke a token in the environment that issued it if it is exposed. A
@@ -153,8 +217,8 @@ or `superseded`. Every validated analysis status is a successful read with exit
153
217
  `analysis.status` value and inspect `analysis.diagnostics` rather than treating a completed analysis with issues as
154
218
  a transport failure.
155
219
 
156
- The projection includes the exact Head digest, Analyzer and Compiler releases, selected target, and
157
- `analysis.gap_set` plus `analysis.gap_set_sha256`. A `valid` run always returns the complete parsed canonical
220
+ The projection includes the exact [Head](#head-and-its-etag) digest, Analyzer and Compiler releases, selected target,
221
+ and `analysis.gap_set` plus `analysis.gap_set_sha256`. A `valid` run always returns the complete parsed canonical
158
222
  `firstdraft.foundation-gaps/2` object, including every ordered gap record and an empty `gaps` array when nothing is
159
223
  missing. Both GapSet fields are `null` for every other status. The CLI validates the GapSet's Head, Project,
160
224
  generation, releases, target, canonical digest, and complete record shapes, then prints the records without
@@ -179,8 +243,7 @@ firstdraft plan compile
179
243
  This is equivalent to `firstdraft plan compile --output .`. Use `--output ./application` for another absent
180
244
  directory, or `--github` to publish to a private GitHub repository. `--github` and `--output` are mutually exclusive.
181
245
  No GitHub connection, repository clone, or push is required for local compilation. Compilation runs on the First
182
- Draft service; output and the application runtime are local. CLI `0.3.x` used GitHub Publication as its default;
183
- scripts that require that behavior must add `--github` when upgrading to `0.4.x` or later.
246
+ Draft service; output and the application runtime are local.
184
247
 
185
248
  Both `plan compile` modes first push the exact current bytes in
186
249
  `.firstdraft/foundation-plan.json`, even when those bytes are unchanged, and save the accepted ETag using the same
@@ -202,10 +265,6 @@ destination is checked again after analysis; root adoption instead holds its own
202
265
  pre-move identity recheck described below. Other existing destinations remain invalid, so
203
266
  `--output ./application` retains its absent-directory contract.
204
267
 
205
- The nested archive layout below is shared by CLI `0.3.x` and later.
206
- [Published CLI `0.2.2`](release-history.md#022-publication-and-registry-observation) archives at top-level `design/`;
207
- existing applications are not migrated automatically.
208
-
209
268
  The default `--output .` is the noninteractive root-adoption mode. `./`, an absolute spelling of the current directory, and
210
269
  another spelling that resolves to that same physical directory select the same mode. It works at any real current
211
270
  directory that meets the preconditions below and does not recognize Drawing Board or another repository layout
@@ -405,3 +464,138 @@ sibling directory, verifies the complete tree, and atomically renames it into th
405
464
  POSIX, directories use mode `0755` and files use artifact-declared `0644` or `0755`; Windows verifies structure,
406
465
  contents, and digests without claiming POSIX mode bits. The declared and streamed artifact envelope is bounded at
407
466
  128 MiB.
467
+
468
+ ## Cancel a stuck Compilation
469
+
470
+ A Project allows one active Compilation. While it is queued or running, First Draft refuses to replace the Plan or
471
+ start another Compilation with `409 compilation_active`, so `plan push` and `plan compile` fail. If that Compilation
472
+ will not finish, for example because its worker process died, cancel it from the Project's directory:
473
+
474
+ ```sh
475
+ firstdraft compilation cancel 01900000-0000-7000-8000-000000000001
476
+ ```
477
+
478
+ The `compilation_active` problem names the active Compilation's ID in its `response.detail`, and a `plan compile`
479
+ failure after a validated start carries it as `current.compilation.id`. Read the Compilation with
480
+ `compilation status` first. Cancelling one that is still making progress throws away its work, and the next
481
+ `plan compile` starts over.
482
+
483
+ The command validates the UUID before network access and makes one `POST` to the origin pinned for the Project, with
484
+ that origin's credential. It sends no Plan bytes or precondition header and changes no local state. Success prints
485
+ the validated cancelled Compilation in the same shape as `compilation status`, with `compilation.status` set to
486
+ `cancelled`. First Draft clears the Project's active Compilation, so the next `plan push` or `plan compile` can
487
+ proceed. A worker that finishes later cannot attach an artifact to a cancelled Compilation; stopping that worker is
488
+ best effort.
489
+
490
+ Cancel is idempotent for queued, running, and cancelled Compilations, so repeating it prints the same cancelled
491
+ Compilation. First Draft never changes a succeeded or failed Compilation: it answers
492
+ `409 compilation_not_cancellable`, which the CLI reports as `compilation_cancel_rejected`. Because a repeat is
493
+ safe, an outcome the CLI cannot confirm is `compilation_cancel_unavailable` rather than `request_outcome_unknown`.
494
+ See [stuck Compilation recovery](errors.md#stuck-compilation-recovery).
495
+
496
+ If the Compilation was started by `plan compile --github`, Cancel also cancels the Project's Publication. The
497
+ Publication is a Project singleton in this release, so `plan compile --github` cannot start another one for that
498
+ Project.
499
+
500
+ ## Service endpoints
501
+
502
+ The CLI calls these Service API routes. The `/v1` routes go to the origin pinned for the Project and send the
503
+ selected token as a Bearer credential. The `/oauth` routes go to the origin selected for `login` or `logout` and
504
+ send form-encoded OAuth parameters without a Bearer credential. Every request refuses redirects and has a bounded
505
+ timeout. Each request takes its method and path from one
506
+ `SERVICE_ROUTES` entry in `src/api-response.js`. `test/service-endpoints.test.js` fails when this table and
507
+ `SERVICE_ROUTES` differ, or when no file in `src/` uses a declared route.
508
+
509
+ | Method | Path | Purpose |
510
+ | ------ | ------------------------------------------------------------------ | ------------------------------------ |
511
+ | `PUT` | `/v1/projects/{project_id}/foundation-plan` | Create or replace the Head |
512
+ | `GET` | `/v1/projects/{project_id}/analysis` | Read the Head's current analysis |
513
+ | `POST` | `/v1/projects/{project_id}/compilations` | Start a Compilation of the Head |
514
+ | `GET` | `/v1/projects/{project_id}/compilations/{compilation_id}` | Read one retained Compilation |
515
+ | `POST` | `/v1/projects/{project_id}/compilations/{compilation_id}/cancel` | Cancel that Compilation |
516
+ | `GET` | `/v1/projects/{project_id}/compilations/{compilation_id}/artifact` | Download that Compilation's artifact |
517
+ | `PUT` | `/v1/projects/{project_id}/github-publication` | Start or rejoin the Publication |
518
+ | `GET` | `/v1/projects/{project_id}/github-publication` | Poll or reconcile the Publication |
519
+ | `POST` | `/oauth/token` | Exchange a login grant for a token |
520
+ | `POST` | `/oauth/device_authorization` | Start a device-flow login |
521
+ | `POST` | `/oauth/revoke` | Revoke a saved token |
522
+
523
+ `plan push` sends the Plan `PUT`, and `plan status` reads the analysis. `plan compile` does both. It then starts a
524
+ Compilation, polls it, and downloads its artifact. With `--github`, it starts and polls the Publication instead.
525
+ `compilation status` reads one retained Compilation. `compilation download` reads it and downloads its artifact.
526
+ `compilation cancel` cancels it.
527
+ `login` exchanges its authorization code at `/oauth/token`. With `--interactive`, it starts at
528
+ `/oauth/device_authorization` and then polls `/oauth/token`. `logout` calls `/oauth/revoke`. The browser, not the
529
+ CLI, opens `/oauth/authorize` and `/device`, so they have no rows.
530
+
531
+ The first push sends `If-None-Match: *`. Later pushes, the Compilation start `POST`, and the Publication `PUT` send
532
+ the saved Head ETag in `If-Match`. The cancel `POST` sends no precondition header.
533
+
534
+ The `/v1` routes belong to the API-contract range that `release/compatibility.json` accepts. The CLI does not read the
535
+ Service's `FirstDraft-API-Contract` response header. Instead, the Service's release compatibility check compares the
536
+ declared ranges before a release. The Service documents the routes in its Foundation Plan machine reference,
537
+ `docs/architecture/reference/README.md` in the private `firstdraft/firstdraft` repository.
538
+
539
+ ### Head and its ETag
540
+
541
+ The Head is the exact Foundation Plan bytes that First Draft holds for a Project. The latest accepted
542
+ `PUT /v1/projects/{project_id}/foundation-plan` sets it. The Service stores those bytes unchanged. A byte change that
543
+ keeps the Plan's meaning still makes a new Head, although the Project's `graph_version` stays the same.
544
+
545
+ A successful Plan `PUT` returns a strong `ETag` of the form `"sha256:<hex>"`, quotes included. `<hex>` is the
546
+ 64-character lowercase SHA-256 of the Head bytes. The CLI saves the complete header value in `.firstdraft/state.json`
547
+ and replays it in `If-Match`. `plan compile` also extracts `<hex>` to check that the local Plan still matches the
548
+ Head before it starts a Compilation or Publication. A saved ETag in any other form stops that check with
549
+ `invalid_configuration`.
550
+
551
+ The same digest appears as `head_source_sha256` in analysis and Compilation responses. To tell whether the local Plan
552
+ is the Head, compare the SHA-256 of `.firstdraft/foundation-plan.json` with `analysis.head_source_sha256` from
553
+ `plan status`. The artifact download expects an ETag of the same form over the artifact bytes.
554
+
555
+ The Service reference asks clients to replay the Plan ETag without interpreting it. This CLI parses it anyway. A
556
+ Service ETag in another form would make every `plan compile` stop with `invalid_configuration`.
557
+
558
+ ## Add a command
559
+
560
+ Use this checklist when a change adds a command or subcommand. Each step names the file to change.
561
+
562
+ 1. **Implement it.** Put the command's logic in a module under `src/commands/`, usually `<group>-<name>.js`. Accept
563
+ `fetchFunction`, file system functions, clocks, and request signals as options, as the existing commands do, so
564
+ tests can replace them. Throw a named error class for each failure the command handles. Build each Service
565
+ request with `serviceEndpoint` and a route declared in `SERVICE_ROUTES` in `src/api-response.js`.
566
+ 2. **Dispatch it in `src/cli.js`.**
567
+ - Add a `<GROUP>_<NAME>_HELP` string, and list the command in its group's help: `PLAN_HELP`,
568
+ `COMPILATION_HELP`, or `GENERATE_HELP`. A new group also needs a line in `ROOT_HELP` and a branch in `run`.
569
+ - Add the branch in `runPlan`, `runCompilation`, or `runGenerate`, and a `run<Group><Name>` function.
570
+ - Parse arguments with strict `parseArgs`. Invalid syntax writes `invalid_arguments` and exits 2.
571
+ - A command that calls the Service also accepts `--staging` and calls `authenticateApiCommand`. It maps each
572
+ error class to one `writeJson(stderr, …)` envelope and its exit status.
573
+ 3. **Test it.**
574
+ - Add `test/<group>-<name>.test.js`. Cover the help text, invalid arguments, the success output, and every
575
+ handled `error` value.
576
+ - Update the exact group help in the tests: `HELP` in `test/cli.test.js`, `PLAN_HELP` in `test/plan-init.test.js`,
577
+ or `GENERATE_HELP` in `test/generate-uuid.test.js`. No test asserts the whole `compilation` group help.
578
+ - Append a command that calls the Service to `REMOTE_COMMANDS` in `test/api-environments.test.js`. Some tests
579
+ there select entries by index, so add it at the end. The command's first request must reach the pinned origin
580
+ with that origin's token, and one `401` must produce `authentication_required`.
581
+ - Add packed-package cases to `scripts/smoke-package.js`, including at least an invalid-arguments case.
582
+ 4. **Package it.** Add each new `src/` file to the exact list in `scripts/check-pack.js`. `firstdraft/skills` keeps
583
+ a copy of that list as `packedFileAllowlist` in `script/cli-contract/config.mjs`. The copy must change when
584
+ Skills bundles this CLI version.
585
+ 5. **Document it.**
586
+ - In this page, add a row to the [command map](#command-map) and a section for the command. Add a row to
587
+ [Service endpoints](#service-endpoints) for each new route, and name the command in the paragraph below the
588
+ table.
589
+ - In [errors.md](errors.md#error-index), add an Error index row for each new `error` value. Add recovery guidance
590
+ when retrying the command is safe in a different way than for the existing commands.
591
+ - When the Skill will call the command, update the Skill's CLI references and contract checks in
592
+ `firstdraft/skills` with its CLI pin.
593
+ 6. **Review it.** New command names, flags, `error` values, and exit statuses need the independent review named in
594
+ `AGENTS.md`.
595
+ 7. **Version it.** Choose the next version with the
596
+ [version rule](https://github.com/firstdraft/cli/blob/main/RELEASING.md#versions), and apply it with the
597
+ [version pull request](https://github.com/firstdraft/cli/blob/main/RELEASING.md#prepare-the-version-pull-request)
598
+ steps. If the command needs a route or response that the accepted API range lacks, the Service ships it first
599
+ under a new API-contract version. Then raise `requires.api_contract` in `release/compatibility.json`, and align
600
+ the Skills CLI requirement. Because the CLI does not read the contract header, an older Service rejects the new
601
+ route as not found.