@r_masseater/oxlint-plugin-decision 0.3.0 → 0.5.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.
@@ -2,7 +2,7 @@
2
2
  name: write-decision-rules
3
3
  description: >
4
4
  Use when writing, wiring up, or debugging lint rules for @r_masseater/oxlint-plugin-decision: rules
5
- phrased as plain-English questions that OpenRouter decision models answer about functions, calls,
5
+ phrased as plain-English questions that models on Cloudflare AI Gateway or OpenRouter answer about functions, calls,
6
6
  JSX elements, or whole files in an Oxlint run. Covers defineDecisionRule, createDecisionPlugin,
7
7
  enabling the rules in oxlint.config.ts, select, answer kinds, context, per-rule models, settings,
8
8
  explain mode, and keeping request cost under control. Also use when someone wants a lint check
@@ -26,20 +26,21 @@ sources:
26
26
 
27
27
  # Write decision rules
28
28
 
29
- The package ships no rules. You write each rule as a yes/no (or pick-one) question with `defineDecisionRule`, bundle the rules with `createDecisionPlugin` in a plugin file of your own, load that file through `jsPlugins` in `oxlint.config.ts`, and turn each rule on by name. Decision models return probabilities, never text, so a rule cannot produce an explanation or a fix.
29
+ The package ships no rules. You write each rule as a yes/no (or pick-one) question with `defineDecisionRule`. You bundle the rules with `createDecisionPlugin` in a plugin file of your own. Then you load that file through `jsPlugins` in `oxlint.config.ts` and turn each rule on by name. Decision models return probabilities, never text. So a rule cannot produce an explanation or a fix.
30
30
 
31
31
  ## Setup
32
32
 
33
- 1. Install the plugin next to Oxlint (`oxlint` >= 1.87.0 is a peer dependency):
33
+ 1. Install the plugin next to Oxlint (`oxlint` >= 1.87.0 is a peer dependency).
34
34
 
35
35
  ```sh
36
36
  npm i -D oxlint @r_masseater/oxlint-plugin-decision
37
- export OPENROUTER_API_KEY="..."
37
+ export CLOUDFLARE_ACCOUNT_ID="..."
38
+ export CLOUDFLARE_API_TOKEN="..." # account API token with Workers AI Read
38
39
  ```
39
40
 
40
- Result: `OPENROUTER_API_KEY` is set in the shell that runs oxlint. Without it the plugin warns once and reports nothing outside CI, and fails the run in CI.
41
+ Result: `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN` are set in the shell that runs oxlint. The default model runs on Workers AI through the AI Gateway `default` (`CLOUDFLARE_AI_GATEWAY_ID` picks another). `openrouter/...` models read `OPENROUTER_API_KEY` instead. Without it the plugin warns once and reports nothing outside CI, and fails the run in CI.
41
42
 
42
- 2. Write a plugin file that default-exports `createDecisionPlugin`:
43
+ 2. Write a plugin file that default-exports `createDecisionPlugin`.
43
44
 
44
45
  ```js
45
46
  // decision-plugin.mjs
@@ -59,7 +60,7 @@ The package ships no rules. You write each rule as a yes/no (or pick-one) questi
59
60
  });
60
61
  ```
61
62
 
62
- 3. Load the file and turn each rule on as `<plugin name>/<rule id>`:
63
+ 3. Load the file and turn each rule on as `<plugin name>/<rule id>`.
63
64
 
64
65
  ```ts
65
66
  // oxlint.config.ts
@@ -75,7 +76,7 @@ The package ships no rules. You write each rule as a yes/no (or pick-one) questi
75
76
 
76
77
  Result: `npx oxlint` lints with the rule. A rule that is defined but not listed under `rules` never sends a request. The plugin name defaults to `decision` when `name` is omitted.
77
78
 
78
- `createDecisionPlugin` validates every rule and setting when the plugin loads and throws on a bad value, so a typo fails the oxlint run instead of being ignored.
79
+ `createDecisionPlugin` validates every rule and setting when the plugin loads and throws on a bad value. A typo fails the oxlint run instead of being ignored.
79
80
 
80
81
  ## Core Patterns
81
82
 
@@ -88,13 +89,13 @@ The package ships no rules. You write each rule as a yes/no (or pick-one) questi
88
89
  | `"jsx"` | The whole element, children included. | The opening tag |
89
90
  | `"file"` | The whole file. | The first line |
90
91
 
91
- There is no other target and no selector syntax. With the default `boolean` answer, "yes" means "report this", so phrase the question so that yes is the violation. The wording is the rule: "Does this send personal data" also fires on a legitimate `mailer.send(user.email, ...)`, while "to a log or console" does not. Questions and messages are English sentences. `message` replaces the question in the diagnostic with a short sentence.
92
+ There is no other target and no selector syntax. With the default `boolean` answer, "yes" means "report this". Phrase the question so that yes is the violation. The wording is the rule. "Does this send personal data" also fires on a legitimate `mailer.send(user.email, ...)`. Adding "to a log or console" stops that. Questions and messages are English sentences. `message` replaces the question in the diagnostic with a short sentence.
92
93
 
93
- Rule fields: `id` (lowercase words joined by hyphens, unique in the plugin), `target`, `question`, `cutoff` (0.8 to 1, inclusive), and optional `message`, `select`, `answer`, `context`, `model`, and `location` (file rules only).
94
+ Every rule has an `id`, a `target`, a `question` and a `cutoff`. The `id` is lowercase words joined by hyphens, unique in the plugin. The `cutoff` is from 0.8 to 1, inclusive. The optional fields are `message`, `select`, `answer`, `context` and `model`. File rules can also set `location`.
94
95
 
95
96
  ### Narrow matches with `select` before any request
96
97
 
97
- `select(node, { name, context })` gets the ESTree node oxlint parsed and returns `true` to ask about it. Rejected matches cost nothing. `name` is the function's own or binding name, the callee as written (`console.error`, `it.only`), or the JSX tag name; it is `undefined` for file rules and for anonymous matches.
98
+ `select(node, { name, context })` gets the ESTree node oxlint parsed and returns `true` to ask about it. Rejected matches cost nothing. `name` is the function's own or binding name, the callee as written (`console.error`, `it.only`) or the JSX tag name. It is `undefined` for file rules and for anonymous matches.
98
99
 
99
100
  ```js
100
101
  defineDecisionRule({
@@ -154,30 +155,30 @@ The kinds are `boolean` (default), `classify`, and `severity`. You never write a
154
155
  | `{ scope: "file" }` | The whole file. |
155
156
  | `{ scope: "comment" }` | The match starting at the comments directly above it. |
156
157
 
157
- With `{ scope: "comment" }`, a match without a comment directly above it (or above the `export` or declaration holding it) is not asked about at all. Wider context costs more tokens, and an edit anywhere in the context re-asks that file.
158
+ With `{ scope: "comment" }`, a match needs a comment directly above it. The comment may also sit above the `export` or declaration holding the match. Without one, the match is not asked about at all. Wider context costs more tokens, and an edit anywhere in the context re-asks that file.
158
159
 
159
160
  ### Point a file rule at a line
160
161
 
161
- A `"file"` rule can add `location: { question, cutoff }` to move the diagnostic from the first line to the suspected violation. The location `cutoff` must be from 0.8 to 1. An uncertain location, or a file cut by `maxSnippetChars`, keeps the first-line diagnostic.
162
+ A `"file"` rule can add `location: { question, cutoff }`. It moves the diagnostic from the first line to the suspected violation. The location `cutoff` must be from 0.8 to 1. An uncertain location keeps the first-line diagnostic. So does a file cut by `maxSnippetChars`.
162
163
 
163
164
  ### Set models and plugin settings
164
165
 
165
- Settings apply to the whole plugin: pass them to `createDecisionPlugin` or put them under `settings.<plugin name>` in `oxlint.config.ts`; the config file wins.
166
+ Settings apply to the whole plugin. Pass them to `createDecisionPlugin` or put them under `settings.<plugin name>` in `oxlint.config.ts`. The config file wins.
166
167
 
167
- | Setting | Default | Meaning |
168
- | ----------------- | ----------------------------------- | ---------------------------------------------------------------------------------------- |
169
- | `model` | `"openrouter/~typesafe/jev-latest"` | Model id `<provider>/<model>`, or a list; every listed model answers every match. |
170
- | `ci` | `"fail"` | When `CI` is set and a model can't be asked: `"fail"` fails the run, `"skip"` warns. |
171
- | `timeoutMs` | `10000` | Per-file budget shared by every request, retry, and location refinement. |
172
- | `maxSnippetChars` | `4000` | Longer snippets are cut and end with `/* ...truncated */`. |
173
- | `env` | `{}` | Renames variables providers read, e.g. `{ OPENROUTER_API_KEY: "TEAM_KEY" }`. Names only. |
174
- | `explain` | `false` | `true`/`"text"` or `"json"` prints why each match was or wasn't reported, to stderr. |
168
+ | Setting | Default | Meaning |
169
+ | ----------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------------- |
170
+ | `model` | `"cloudflare/@cf/qwen/qwen3.8-27b"` | Model id `<provider>/<model>`, or a list; every listed model answers every match. |
171
+ | `ci` | `"fail"` | When `CI` is set and a model can't be asked: `"fail"` fails the run, `"skip"` warns. A spent budget only warns. |
172
+ | `timeoutMs` | `10000` | Per-file budget shared by every request, retry, and location refinement. |
173
+ | `maxSnippetChars` | `4000` | Longer snippets are cut and end with `/* ...truncated */`. |
174
+ | `env` | `{}` | Renames variables providers read, e.g. `{ CLOUDFLARE_API_TOKEN: "TEAM_KEY" }`. Names only. |
175
+ | `explain` | `false` | `true`/`"text"` or `"json"` prints why each match was or wasn't reported, to stderr. |
175
176
 
176
- A rule's model comes from the first of: the rule's options in `oxlint.config.ts` (`["warn", { model: "openrouter/respan/span-01-lite" }]`), the rule's own `model`, `settings.<plugin name>.model`, the `model` passed to `createDecisionPlugin`, then the default.
177
+ A rule's model comes from the first source that sets one. The rule's options in `oxlint.config.ts` come first, such as `["warn", { model: "openrouter/respan/span-01-lite" }]`. Next comes the rule's own `model`, and then `settings.<plugin name>.model`. After that comes the `model` passed to `createDecisionPlugin`. The default comes last.
177
178
 
178
179
  When tuning a question or cutoff, or switching models, read [tuning rules](references/tuning.md) first.
179
180
 
180
- When the rules must ask a decision service other than OpenRouter, read [custom providers](references/providers.md) before writing one.
181
+ To ask a service other than Cloudflare and OpenRouter, read [custom providers](references/providers.md) before writing one.
181
182
 
182
183
  ## Common Mistakes
183
184
 
@@ -187,19 +188,23 @@ When the rules must ask a decision service other than OpenRouter, read [custom p
187
188
  // Wrong: there is no apiKey setting; createDecisionPlugin rejects unknown keys
188
189
  createDecisionPlugin({ name: "team", apiKey: process.env.KEY, rules });
189
190
 
190
- // Right: export OPENROUTER_API_KEY, or rename the variable the provider reads
191
- createDecisionPlugin({ name: "team", env: { OPENROUTER_API_KEY: "TEAM_OPENROUTER_KEY" }, rules });
191
+ // Right: export CLOUDFLARE_API_TOKEN, or rename the variable the provider reads
192
+ createDecisionPlugin({
193
+ name: "team",
194
+ env: { CLOUDFLARE_API_TOKEN: "TEAM_CLOUDFLARE_TOKEN" },
195
+ rules,
196
+ });
192
197
  ```
193
198
 
194
199
  Keys are read only from environment variables; `env` values are variable names, not keys (`src/options.ts`, `src/runtime.ts`).
195
200
 
196
201
  ### HIGH: a broad target on a whole codebase
197
202
 
198
- Every match is a paid request unless cached, and the plugin has no per-file cap, budget, include/exclude, or changed-files option. A `"call"` rule without `select` asks about every call in every linted file. Narrow with `select`, scope files and rules with oxlint `overrides`, and lint only changed files by passing their paths to oxlint. A list of models multiplies the requests.
203
+ Every match is a paid request unless cached. The plugin has no per-file cap, budget, include/exclude or changed-files option. A `"call"` rule without `select` asks about every call in every linted file. Narrow with `select`, and scope files and rules with oxlint `overrides`. Lint only changed files by passing their paths to oxlint. A list of models multiplies the requests.
199
204
 
200
205
  ### HIGH: decision rules on in the editor config
201
206
 
202
- The oxlint editor extension lints on every keystroke, and each edit inside a match is a request that blocks the language server. Keep decision rules off in the config the editor reads and enable them in an overlay used for CI and pre-push:
207
+ The oxlint editor extension lints on every keystroke. Each edit inside a match is a request that blocks the language server. Keep decision rules off in the config the editor reads. Enable them in an overlay used for CI and pre-push, as below.
203
208
 
204
209
  ```ts
205
210
  // oxlint.ci.config.ts
@@ -217,14 +222,14 @@ Run it with `oxlint -c oxlint.ci.config.ts`. Keep `jsPlugins` in the base config
217
222
 
218
223
  ### MEDIUM: a question where "yes" means "fine"
219
224
 
220
- "Is this function named well?" reports the well-named functions. With the `boolean` answer, ask about the violation, or use `classify` with `report` listing only the bad labels.
225
+ Asking whether a function is named well reports the well-named functions. With the `boolean` answer, ask about the violation. Or use `classify` with `report` listing only the bad labels.
221
226
 
222
227
  ### MEDIUM: rule ids that are not lowercase-hyphenated or are repeated
223
228
 
224
- `id` must match `^[a-z0-9]+(?:-[a-z0-9]+)*$` and be unique within the plugin, and `rules` must list at least one rule; otherwise loading the plugin throws (`src/options.ts`).
229
+ `id` must match `^[a-z0-9]+(?:-[a-z0-9]+)*$` and be unique within the plugin. `rules` must list at least one rule. Otherwise loading the plugin throws (`src/options.ts`).
225
230
 
226
231
  ## Completion
227
232
 
228
- 1. Run `npx oxlint` (or the CI overlay) on a file that should fail and one that should pass. Result: the failing file gets a diagnostic like `[no-pii-in-logs] openrouter/... answered yes (0.93 >= 0.80): ...`; the passing file gets none.
229
- 2. If nothing is reported, run once with `OXLINT_DECISION_EXPLAIN=1` and check stderr: a missing match means the target, `select`, or `context: { scope: "comment" }` excluded it; an `error` line means the key, timeout, or API failed; a low probability means the question or cutoff needs tuning (see [tuning rules](references/tuning.md)).
233
+ 1. Run `npx oxlint` (or the CI overlay) on a file that should fail and one that should pass. Result: the failing file gets a diagnostic like `[no-pii-in-logs] openrouter/... answered yes (0.93 >= 0.80): ...`. The passing file gets none.
234
+ 2. If nothing is reported, run once with `OXLINT_DECISION_EXPLAIN=1` and check stderr. A missing match means the target, `select` or `context: { scope: "comment" }` excluded it. An `error` line means the key, timeout or API failed. A low probability means the question or cutoff needs tuning (see [tuning rules](references/tuning.md)).
230
235
  3. If the plugin fails to load, fix the rule or setting the thrown error names; do not remove validation.
@@ -1,6 +1,6 @@
1
1
  # Custom providers
2
2
 
3
- Read this when rules must ask a decision service other than OpenRouter. The built-in provider is `openrouter`, so `openrouter/<model>` needs no provider code.
3
+ Read this when rules must ask a service other than Cloudflare and OpenRouter. `cloudflare/<model>` and `openrouter/<model>` are built in and need no provider code.
4
4
 
5
5
  ## Define and register a provider
6
6
 
@@ -36,16 +36,20 @@ export default createDecisionPlugin({
36
36
  });
37
37
  ```
38
38
 
39
- - A model id `internal/v2` routes to the provider named `internal` and sends `model: "v2"` in the request. An id whose provider is not registered makes the plugin throw at load time.
40
- - `requiredEnv` lists the variables the provider needs. The plugin checks them, after applying the `env` renames, before calling `decide`; when one is missing it warns once and reports nothing for that model (or fails the run in CI with `ci: "fail"`).
41
- - `env` in the `decide` input is the rename map from settings, not the values. Read `process.env[env[name] ?? name]` yourself.
39
+ A model id `internal/v2` routes to the provider named `internal` and sends `model: "v2"` in the request. An id whose provider is not registered makes the plugin throw at load time.
40
+
41
+ `requiredEnv` lists the variables the provider needs. The plugin checks them before calling `decide`, after applying the `env` renames. When one is missing, it warns once and reports nothing for that model. In CI with `ci: "fail"`, it fails the run instead.
42
+
43
+ `env` in the `decide` input is the rename map from settings, not the values. Read `process.env[env[name] ?? name]` yourself.
42
44
 
43
45
  ## The `decide` contract
44
46
 
45
- - `decide` is synchronous because oxlint rules are. A provider that calls a network service must block on a worker thread, as the built-in provider does with [synckit](https://github.com/un-ts/synckit); returning a Promise is not supported.
46
- - Input: `{ request, timeoutMs, env }`. `request` is an OpenRouter Decisions request: `model`, `state` (`snippets` keyed by match id, optional `contexts`), and `questions`, each `{ type: "noul", instructions }` or `{ type: "choice", instructions, criteria }`.
47
- - Output: `{ ok: true, response }` with a Decisions response body, or `{ ok: false, reason }`. A failure is a warning (or a CI failure), never an exception; do not throw from `decide`.
48
- - Respect `timeoutMs`; it is the remaining per-file budget.
49
- - Optional `decideAll(inputs)` gets every model of one file for this provider at once and returns one result per input, in the same order. Send them concurrently there; without it the plugin calls `decide` once per model, one after another. A result count that differs from the input count fails every input of that call.
47
+ `decide` is synchronous because oxlint rules are. A provider that calls a network service must block on a worker thread. The built-in provider does that with [synckit](https://github.com/un-ts/synckit). Returning a Promise is not supported.
48
+
49
+ The input is `{ request, timeoutMs, env }`. `request` is an OpenRouter Decisions request with `model`, `state` and `questions`. `state` holds `snippets` keyed by match id and optional `contexts`. Each question is `{ type: "noul", instructions }` or `{ type: "choice", instructions, criteria }`.
50
+
51
+ The output is `{ ok: true, response }` with a Decisions response body, or `{ ok: false, reason }`. A failure is a warning (or a CI failure), never an exception. Do not throw from `decide`. Respect `timeoutMs`, which is the remaining per-file budget.
52
+
53
+ Optional `decideAll(inputs)` gets every model of one file for this provider at once. It returns one result per input, in the same order. Send them concurrently there. Without it, the plugin calls `decide` once per model, one after another. A result count that differs from the input count fails every input of that call.
50
54
 
51
- Check: run oxlint with `OXLINT_DECISION_EXPLAIN=1`. Each match should show your model id and a probability, not an `error` line.
55
+ To check the provider, run oxlint with `OXLINT_DECISION_EXPLAIN=1`. Each match should show your model id and a probability, not an `error` line.
@@ -9,21 +9,21 @@ OXLINT_DECISION_EXPLAIN=1 npx oxlint # readable text on stderr
9
9
  OXLINT_DECISION_EXPLAIN=json npx oxlint # one JSON object per match
10
10
  ```
11
11
 
12
- oxlint does not pass flags to plugins, so explain mode is an environment variable (or `explain: true` / `"json"` in settings; the variable wins, and `OXLINT_DECISION_EXPLAIN=0` turns it off). Each record shows the file and position, rule, requested and served model, whether the answer was cached, the start of the source sent, every question exactly as sent with its probability, the verdict, and the cutoff. A failed request shows its reason instead. Explain mode only prints; it changes nothing that is asked, cached, or reported.
12
+ oxlint does not pass flags to plugins, so explain mode is an environment variable. Settings also take `explain: true` or `"json"`, and the variable wins. `OXLINT_DECISION_EXPLAIN=0` turns it off. Each record shows the file and position, the rule, and the requested and served model. It also shows whether the answer was cached and the start of the source sent. Then come every question exactly as sent with its probability, the verdict and the cutoff. A failed request shows its reason instead. Explain mode only prints. It changes nothing that is asked, cached or reported.
13
13
 
14
14
  ## Adjust the question before the cutoff
15
15
 
16
- 1. Write one file that must fail and one that must pass, close to each other (the same body with a different name, for example).
17
- 2. Run explain mode on both. If the yes probabilities overlap, the question is ambiguous: reword it to name exactly what counts and what doesn't. Changing only the cutoff cannot separate overlapping cases.
18
- 3. When the two separate, set `cutoff` between them. Result: the failing file is reported and the passing file is not.
16
+ Write one file that must fail and one that must pass, close to each other. For example, use the same body with a different name. Run explain mode on both. If the yes probabilities overlap, the question is ambiguous. Reword it to name exactly what counts and what doesn't. Changing only the cutoff cannot separate overlapping cases. When the two separate, set `cutoff` between them. Then the failing file is reported and the passing file is not.
19
17
 
20
18
  A changed question, snippet, context, or answer kind re-asks that file. A changed `cutoff` or `id` reuses the cache, so cutoff tuning is free after the first run.
21
19
 
22
20
  ## Switching or mixing models
23
21
 
24
- - Probabilities are not calibrated the same across models; re-tune cutoffs after switching, and pin a versioned model id once tuned.
25
- - `openrouter/respan/span-01-lite` is free and a cheap way to try rules. Respan models answer yes/no only: the plugin splits `classify` into one yes/no per label (highest yes wins), `severity` into "at least info/warn/error" (the most serious level at 0.8 or above wins), and asks about one match per request.
26
- - Override one rule's model in `oxlint.config.ts` without touching the plugin file: `"team/stale-comment": ["warn", { model: "openrouter/~typesafe/jev-latest" }]`. Rules on different models are asked in separate requests and cached separately.
22
+ Probabilities are not calibrated the same across models. Re-tune cutoffs after switching, and pin a versioned model id once tuned.
23
+
24
+ Cloudflare models and Respan models such as `openrouter/respan/span-01-lite` answer yes/no only. The plugin splits `classify` into one yes/no per label, and the highest yes wins. It splits `severity` into "at least info/warn/error", and the most serious level at 0.8 or above wins. It also asks one question per request.
25
+
26
+ You can override one rule's model in `oxlint.config.ts` without touching the plugin file. For example, write `"team/stale-comment": ["warn", { model: "openrouter/~typesafe/jev-latest" }]`. Rules on different models are asked in separate requests and cached separately.
27
27
 
28
28
  ## Cache
29
29