@r_masseater/oxlint-plugin-decision 0.0.0-stage → 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Robert Soriano
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,287 @@
1
- # Temporary Holding Version
1
+ # @r_masseater/oxlint-plugin-decision
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > [!WARNING]
4
+ > This package is experimental. Use at your own risk.
5
+
6
+ [Oxlint](https://oxc.rs/docs/guide/usage/linter.html) rules written in plain English, answered by decision models on [OpenRouter](https://openrouter.ai).
7
+
8
+ A rule is a question about a function, a call, a JSX element, or a whole file. Each match is sent to one or more decision models with the question, and the plugin reports an error when the answer the rule cares about clears its cutoff.
9
+
10
+ Decision models do not write text. They return a probability for a yes/no question, or pick one of the options you list. That makes them fast and cheap enough to run over a codebase, and it is why every rule here is a question with a fixed set of answers.
11
+
12
+ For ready-made rules, see [`@r_masseater/oxlint-plugin-decision-presets`](https://www.npmjs.com/package/@r_masseater/oxlint-plugin-decision-presets).
13
+
14
+ This is a fork of [oxlint-plugin-jev](https://github.com/wobsoriano/oxlint-plugin-jev) by [Robert Soriano](https://github.com/wobsoriano), rebuilt on the OpenRouter Decisions API.
15
+
16
+ ## Install
17
+
18
+ ```sh
19
+ npm i -D oxlint @r_masseater/oxlint-plugin-decision
20
+ export OPENROUTER_API_KEY="..." # https://openrouter.ai/settings/keys
21
+ ```
22
+
23
+ The plugin reads the key from the `OPENROUTER_API_KEY` environment variable. Without it, the plugin warns once and reports nothing.
24
+
25
+ In CI, set `ci: "fail"` so a run that could not reach a model fails instead of passing quietly.
26
+
27
+ ## Config
28
+
29
+ Add the plugin and its one rule, `decision/ask`, to `.oxlintrc.json`. Your English rules go in the options.
30
+
31
+ ```json
32
+ {
33
+ "jsPlugins": ["@r_masseater/oxlint-plugin-decision"],
34
+ "rules": {
35
+ "decision/ask": [
36
+ "error",
37
+ {
38
+ "model": "openrouter/~typesafe/jev-latest",
39
+ "rules": [
40
+ {
41
+ "id": "no-pii-in-logs",
42
+ "target": "call",
43
+ "question": "Does this call write personal data, such as an email or phone number, to a log or console?",
44
+ "cutoff": 0.8
45
+ },
46
+ {
47
+ "id": "name-matches-behavior",
48
+ "target": "function",
49
+ "question": "Does this function's name describe only reading or fetching data (for example get, find, load or fetch), while its body also writes, logs or sends something?",
50
+ "cutoff": 0.6,
51
+ "context": { "scope": "enclosing", "kind": "class" }
52
+ }
53
+ ]
54
+ }
55
+ ]
56
+ }
57
+ }
58
+ ```
59
+
60
+ ### Rules
61
+
62
+ | Field | What it is |
63
+ | ---------- | ------------------------------------------------------------------------------------------------------- |
64
+ | `id` | Shown in the error message. Unique in the list. |
65
+ | `target` | `"function"`, `"call"`, `"jsx"`, or `"file"`. |
66
+ | `question` | The question, in English. With the default answer kind, "yes" means "report this". |
67
+ | `cutoff` | 0 to 1. The probability the reported answer needs, at or above. |
68
+ | `answer` | Optional. What kind of answer to ask for. See [Answers](#answers). Defaults to `{ "kind": "boolean" }`. |
69
+ | `context` | Optional. Extra source the model reads around the match. See [Context](#context). |
70
+ | `model` | Optional. Overrides the top-level `model` for this rule. |
71
+ | `location` | Optional for file rules: a source-location `question` and a `cutoff` greater than 0.5 and at most 1. |
72
+
73
+ `target` decides what the model gets to read. There is no selector syntax and no other target.
74
+
75
+ | Target | The model sees | The error underlines |
76
+ | ------------ | ------------------------------------------------------------------------------------------------------------ | -------------------- |
77
+ | `"function"` | The whole function. An arrow or method includes its name, so `const getUser = () => ...` reads as `getUser`. | The signature line |
78
+ | `"call"` | The whole call expression. | The whole call |
79
+ | `"jsx"` | The whole element, children included. | The opening tag |
80
+ | `"file"` | The whole file. | The first line |
81
+
82
+ The wording of the question is the rule, so be precise about what counts. "Does this send personal data" also fires on a legitimate `mailer.send(user.email, ...)`. "To a log or console" does not.
83
+
84
+ ### Answers
85
+
86
+ Pick a kind. You never write a schema.
87
+
88
+ | Kind | The model is asked | Reported when |
89
+ | ------------------- | -------------------------------------------------- | ---------------------------------------------------------------------------- |
90
+ | `boolean` (default) | The probability that the answer is yes. | That probability is at or above `cutoff`. |
91
+ | `classify` | To pick one of the `labels` you list. | The chosen label is in `report` and its probability is at or above `cutoff`. |
92
+ | `severity` | To rate the problem as none, info, warn, or error. | The rating is at or above `minimum` (default `"warn"`) and clears `cutoff`. |
93
+
94
+ ```json
95
+ {
96
+ "id": "stale-comment",
97
+ "target": "function",
98
+ "question": "How does the comment right above this function relate to what the function does?",
99
+ "cutoff": 0.7,
100
+ "context": { "scope": "lines", "before": 5, "after": 0 },
101
+ "answer": {
102
+ "kind": "classify",
103
+ "labels": {
104
+ "accurate": "The comment describes what the code does",
105
+ "stale": "The comment describes behavior the code no longer has",
106
+ "redundant": "The comment repeats the code without adding anything"
107
+ },
108
+ "report": ["stale"]
109
+ }
110
+ }
111
+ ```
112
+
113
+ ```json
114
+ {
115
+ "id": "security-smell",
116
+ "target": "function",
117
+ "question": "Does this function handle untrusted input in a way that could be exploited?",
118
+ "cutoff": 0.6,
119
+ "answer": { "kind": "severity", "minimum": "warn" }
120
+ }
121
+ ```
122
+
123
+ The message names the answer: `[stale-comment] openrouter/... answered stale (0.91 >= 0.70): ...`.
124
+
125
+ ### Context
126
+
127
+ By default the model reads only the match. `context` sends more source with it, marked as background so the verdict still targets the match.
128
+
129
+ | Context | Sent with the match |
130
+ | ----------------------------------------------- | ---------------------------------------------------------- |
131
+ | `{ "scope": "node" }` (default) | Nothing. |
132
+ | `{ "scope": "lines", "before": 5, "after": 5 }` | Numbered lines around the match. Both counts default to 5. |
133
+ | `{ "scope": "enclosing", "kind": "function" }` | The nearest enclosing `function`, `class`, or `block`. |
134
+ | `{ "scope": "file" }` | The whole file. |
135
+
136
+ More context costs more tokens and is cached together with the match, so an edit anywhere in the context re-asks that file.
137
+
138
+ ### Models
139
+
140
+ A model id is `<provider>/<model>`. The built-in provider is `openrouter`, so `openrouter/inception/mercury-decide` asks `inception/mercury-decide` on OpenRouter. Browse decision models at [openrouter.ai/models](https://openrouter.ai/models?output_modalities=decisions).
141
+
142
+ `model` takes one id or a list. With a list, every model answers every match, each model's request is separate, and each model's yes is its own diagnostic, so you can compare models on the same code.
143
+
144
+ ```json
145
+ { "model": ["openrouter/~typesafe/jev-latest", "openrouter/respan/span-01-lite"] }
146
+ ```
147
+
148
+ Respan models, such as `openrouter/respan/span-01-lite`, answer only yes/no questions: use them with `boolean` rules, not `classify`, `severity` or file-rule `location`. They are also asked about one match per request, so they send more requests than other models on files with many matches. The default `openrouter/~typesafe/jev-latest` supports every answer kind.
149
+
150
+ Probabilities are not calibrated the same way across models, so a cutoff tuned for one model does not carry over to another. Pin a versioned id once your cutoffs are tuned. Each diagnostic names the model that answered.
151
+
152
+ A key is required only for providers your config references. `OPENROUTER_BASE_URL` points the plugin at another OpenRouter-compatible host.
153
+
154
+ ### File locations
155
+
156
+ File rules can point editor diagnostics and GitHub annotations to the suspected violation:
157
+
158
+ ```json
159
+ {
160
+ "id": "prefer-boolean-state-helper",
161
+ "target": "file",
162
+ "question": "Does any boolean React state have a memoized callback that only sets it to true or false?",
163
+ "cutoff": 0.9,
164
+ "location": {
165
+ "question": "Select the declaration of the state used by the offending callback.",
166
+ "cutoff": 0.75
167
+ }
168
+ }
169
+ ```
170
+
171
+ The violation and location have separate cutoffs. An uncertain or invalid location keeps the finding on the first line. Files truncated by `maxSnippetChars` also keep the first-line diagnostic, since location questions only use complete source.
172
+
173
+ ### Settings
174
+
175
+ | Setting | Default | Meaning |
176
+ | ------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
177
+ | `ci` | `"skip"` | What happens when a model can't be asked and `CI` is set. `"skip"` warns once and reports nothing. `"fail"` fails the run. Outside CI it always skips. |
178
+ | `timeoutMs` | `10000` | Per-file budget shared by every model's request, retries and location refinements. |
179
+ | `maxMatchesPerFile` | `25` | Matches sent per file across all rules. Extra matches are dropped in source order and the file is named on stderr. |
180
+ | `maxSnippetChars` | `4000` | Longer snippets are cut and end with `/* ...truncated */`. |
181
+ | `model` | `"openrouter/~typesafe/jev-latest"` | The default model id or list of ids. |
182
+
183
+ ## Rules and providers in code
184
+
185
+ `createDecisionPlugin` builds a plugin with rules and providers defined in JavaScript. Export it from a local file and list that file in `jsPlugins`.
186
+
187
+ ```js
188
+ // decision-plugin.mjs
189
+ import {
190
+ createDecisionPlugin,
191
+ defineDecisionRule,
192
+ defineProvider,
193
+ } from "@r_masseater/oxlint-plugin-decision";
194
+
195
+ export default createDecisionPlugin({
196
+ name: "team",
197
+ rules: [
198
+ defineDecisionRule({
199
+ id: "no-swallowed-catch",
200
+ target: "function",
201
+ question: "Does this function catch an error and neither handle nor rethrow it?",
202
+ cutoff: 0.8,
203
+ context: { scope: "lines", before: 3, after: 3 },
204
+ }),
205
+ ],
206
+ providers: [
207
+ defineProvider({
208
+ name: "internal",
209
+ requiredEnv: ["INTERNAL_DECIDER_KEY"],
210
+ decide: ({ request, timeoutMs }) => ({
211
+ ok: true,
212
+ response: callYourDecider(request, timeoutMs),
213
+ }),
214
+ }),
215
+ ],
216
+ });
217
+ ```
218
+
219
+ ```json
220
+ {
221
+ "jsPlugins": ["./decision-plugin.mjs"],
222
+ "rules": { "team/ask": ["error", { "model": "internal/v2" }] }
223
+ }
224
+ ```
225
+
226
+ Rules from code come first, then rules from the config. Oxlint checks config rules against the plugin's JSON schema; rules from code are checked with the same constraints when linting starts.
227
+
228
+ A provider's `decide` is synchronous, because oxlint rules are. It gets an OpenRouter Decisions request (`model`, `state`, `questions`) and returns `{ ok: true, response }` with a Decisions response, or `{ ok: false, reason }`. The built-in `openRouter` provider does its network call on a worker thread through [synckit](https://github.com/un-ts/synckit); a custom provider that calls a network service should do the same.
229
+
230
+ Types for all of this are exported: `DecisionRule`, `DecisionOptions`, `RuleAnswer`, `RuleContext`, `DecisionProvider`, `DecisionRequest`, `DecideResult`, and more.
231
+
232
+ ## How it works
233
+
234
+ Each file gets one request per model, with every match for that model in it. Respan models get one request per match instead. A file rule with `location` adds numbered source and a choice question. Above 254 nonblank lines the model selects a range and refines it in follow-up requests with the same context. All requests share the per-file `timeoutMs` budget.
235
+
236
+ Answers are cached under `node_modules/.cache/oxlint-plugin-decision`, keyed by the endpoint and the request. Changing a snippet, its context, a question or an answer kind re-asks that file. Changing a cutoff or an `id` does not. Malformed answers are never cached, and malformed entries already on disk are asked again.
237
+
238
+ Requests go through the official [`@openrouter/sdk`](https://www.npmjs.com/package/@openrouter/sdk) client, which retries rate limits and server errors within `timeoutMs`.
239
+
240
+ If a model can't be asked, because the key is missing, the request times out, or the API errors, the plugin prints one warning and reports nothing for that model on that file. Set `ci: "fail"` to fail the run instead.
241
+
242
+ If only a location refinement fails, confirmed violations are still reported at the file level. Under CI with `ci: "fail"`, the refinement failure produces a separate diagnostic; otherwise it warns.
243
+
244
+ ## Cost and caching
245
+
246
+ Every match is a paid model request unless its answer is already cached, so keep an eye on what your rules match.
247
+
248
+ - A broad target, such as `"call"` on a large codebase, sends many matches. `maxMatchesPerFile` caps how many are sent per file.
249
+ - Wider `context` and longer snippets cost more tokens. `maxSnippetChars` caps each snippet.
250
+ - A list of models multiplies the requests: each model answers every match.
251
+ - Answers are cached on disk under `node_modules/.cache/oxlint-plugin-decision`. Unchanged code is not asked again, so a second run over the same files is free. Editing a snippet, its context, a question or an answer kind re-asks that file; changing a cutoff or an `id` does not.
252
+ - In CI, persist `node_modules/.cache/oxlint-plugin-decision` between runs to keep the cache warm.
253
+ - Free models such as `openrouter/respan/span-01-lite` are a cheap way to try rules before switching to a paid model.
254
+
255
+ ## In the editor
256
+
257
+ The oxlint VS Code extension lints as you type, and every keystroke inside a match is a paid request that blocks the language server until the model answers.
258
+
259
+ Keep `decision/ask` out of the config your editor reads, and put it in an overlay for CI and pre-push. Leave `jsPlugins` in the base config, since the overlay inherits it.
260
+
261
+ ```json
262
+ {
263
+ "extends": [".oxlintrc.json"],
264
+ "rules": {
265
+ "decision/ask": ["error", { "rules": [ ... ] }]
266
+ }
267
+ }
268
+ ```
269
+
270
+ ```sh
271
+ oxlint # editor and local runs, no models
272
+ oxlint -c .oxlintrc.ci.json # CI and pre-push, models included
273
+ ```
274
+
275
+ ## Example
276
+
277
+ Three rules that a pattern-based linter can't express, and code each one fails and passes on. The rules are the ones in the [Config](#config) section plus a prompt-injection check.
278
+
279
+ | Rule | Fails on | Passes on |
280
+ | ----------------------- | ------------------------------------------------------- | ------------------------------------------- |
281
+ | `no-pii-in-logs` | `console.log("loaded", user.email, user.phone)` | `console.log("notified", { userId: id })` |
282
+ | `name-matches-behavior` | `getUser` that also sends an email | The same body named `notifySignIn` |
283
+ | `no-prompt-injection` | A customer message pasted into the system prompt string | The message passed as a separate user field |
284
+
285
+ ## License
286
+
287
+ MIT
@@ -0,0 +1,112 @@
1
+ //#region src/types.d.ts
2
+ type Target = "function" | "call" | "jsx" | "file";
3
+ type CiBehavior = "skip" | "fail";
4
+ type ModelSpec = string | readonly string[];
5
+ interface RuleLocation {
6
+ readonly question: string;
7
+ readonly cutoff: number;
8
+ }
9
+ type EnclosingKind = "function" | "class" | "block";
10
+ type RuleContext = {
11
+ readonly scope: "node";
12
+ } | {
13
+ readonly scope: "lines";
14
+ readonly before?: number;
15
+ readonly after?: number;
16
+ } | {
17
+ readonly scope: "enclosing";
18
+ readonly kind: EnclosingKind;
19
+ } | {
20
+ readonly scope: "file";
21
+ };
22
+ type Severity = "info" | "warn" | "error";
23
+ type RuleAnswer = {
24
+ readonly kind: "boolean";
25
+ } | {
26
+ readonly kind: "classify";
27
+ readonly labels: Readonly<Record<string, string>>;
28
+ readonly report: readonly [string, ...string[]];
29
+ } | {
30
+ readonly kind: "severity";
31
+ readonly minimum?: Severity;
32
+ };
33
+ interface DecisionRule {
34
+ readonly id: string;
35
+ readonly target: Target;
36
+ readonly question: string;
37
+ readonly cutoff: number;
38
+ readonly location?: RuleLocation;
39
+ readonly model?: ModelSpec;
40
+ readonly context?: RuleContext;
41
+ readonly answer?: RuleAnswer;
42
+ }
43
+ interface DecisionOptions {
44
+ readonly ci?: CiBehavior;
45
+ readonly timeoutMs?: number;
46
+ readonly maxMatchesPerFile?: number;
47
+ readonly maxSnippetChars?: number;
48
+ readonly model?: ModelSpec;
49
+ readonly rules?: readonly DecisionRule[];
50
+ }
51
+ interface NoulQuestion {
52
+ readonly type: "noul";
53
+ readonly instructions: string;
54
+ }
55
+ interface ChoiceQuestion {
56
+ readonly type: "choice";
57
+ readonly instructions: string;
58
+ readonly criteria: Readonly<Record<string, string | null>>;
59
+ }
60
+ type Question = NoulQuestion | ChoiceQuestion;
61
+ interface DecisionRequest {
62
+ readonly model: string;
63
+ readonly state: {
64
+ readonly snippets: Readonly<Record<string, string>>;
65
+ readonly contexts?: Readonly<Record<string, string>>;
66
+ };
67
+ readonly questions: Readonly<Record<string, Question>>;
68
+ }
69
+ type DecideResult = {
70
+ readonly ok: true;
71
+ readonly response: unknown;
72
+ } | {
73
+ readonly ok: false;
74
+ readonly reason: string;
75
+ };
76
+ interface DecideInput {
77
+ readonly request: DecisionRequest;
78
+ readonly timeoutMs: number;
79
+ }
80
+ interface DecisionProvider {
81
+ readonly name: string;
82
+ readonly requiredEnv: readonly string[];
83
+ readonly decide: (input: DecideInput) => DecideResult;
84
+ }
85
+ interface DecisionPluginSetup {
86
+ readonly name?: string;
87
+ readonly rules?: readonly DecisionRule[];
88
+ readonly providers?: readonly DecisionProvider[];
89
+ }
90
+ interface DecisionPlugin {
91
+ readonly meta: {
92
+ readonly name: string;
93
+ };
94
+ readonly rules: {
95
+ readonly ask: object;
96
+ };
97
+ }
98
+ //#endregion
99
+ //#region src/define.d.ts
100
+ declare const defineDecisionRule: <const Rule extends DecisionRule>(rule: Rule) => Rule;
101
+ declare const defineProvider: <const Provider extends DecisionProvider>(provider: Provider) => Provider;
102
+ //#endregion
103
+ //#region src/openrouter.d.ts
104
+ declare const openRouter: DecisionProvider;
105
+ //#endregion
106
+ //#region src/plugin.d.ts
107
+ declare const createDecisionPlugin: (setup?: DecisionPluginSetup) => DecisionPlugin;
108
+ //#endregion
109
+ //#region src/index.d.ts
110
+ declare const plugin: DecisionPlugin;
111
+ //#endregion
112
+ export { type ChoiceQuestion, type CiBehavior, type DecideInput, type DecideResult, type DecisionOptions, type DecisionPlugin, type DecisionPluginSetup, type DecisionProvider, type DecisionRequest, type DecisionRule, type EnclosingKind, type ModelSpec, type NoulQuestion, type Question, type RuleAnswer, type RuleContext, type RuleLocation, type Severity, type Target, createDecisionPlugin, plugin as default, defineDecisionRule, defineProvider, openRouter };