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

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/package.json CHANGED
@@ -1,6 +1,70 @@
1
1
  {
2
2
  "name": "@r_masseater/oxlint-plugin-decision",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.3",
4
+ "description": "Oxlint JS plugin that lints code with plain-English rules answered by decision models on OpenRouter.",
5
+ "keywords": [
6
+ "decision-model",
7
+ "lint",
8
+ "openrouter",
9
+ "oxlint",
10
+ "oxlint-plugin",
11
+ "tanstack-intent"
12
+ ],
13
+ "homepage": "https://github.com/masseater/oxlint-plugin-decision#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/masseater/oxlint-plugin-decision/issues"
16
+ },
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/masseater/oxlint-plugin-decision.git",
21
+ "directory": "libs/oxlint-plugin-decision"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "skills/write-decision-rules"
26
+ ],
27
+ "type": "module",
28
+ "imports": {
29
+ "#src/*": "./src/*"
30
+ },
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/index.d.mts",
34
+ "default": "./dist/index.mjs"
35
+ },
36
+ "./package.json": "./package.json"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "provenance": true
41
+ },
42
+ "dependencies": {
43
+ "@effect/platform-node": "4.0.0",
44
+ "@openrouter/sdk": "1.4.25",
45
+ "effect": "4.0.0",
46
+ "synckit": "0.12.1"
47
+ },
48
+ "devDependencies": {
49
+ "@effect/tsgo": "0.50.0",
50
+ "@effect/vitest": "4.0.0",
51
+ "@oxlint/plugins": "1.87.0",
52
+ "@types/node": "26.6.5",
53
+ "@vitest/coverage-v8": "5.0.3",
54
+ "oxlint": "1.87.0",
55
+ "typescript": "7.0.2",
56
+ "vite-plus": "1.1.0"
57
+ },
58
+ "peerDependencies": {
59
+ "oxlint": ">=1.87.0"
60
+ },
61
+ "engines": {
62
+ "node": ">=20"
63
+ },
64
+ "scripts": {
65
+ "build": "vp pack",
66
+ "dev": "vp pack --watch",
67
+ "test": "vp test --project=mock --coverage",
68
+ "example": "vp run build && ./node_modules/oxlint/bin/oxlint -c example/oxlint.config.ts example/"
69
+ }
6
70
  }
@@ -0,0 +1,230 @@
1
+ ---
2
+ name: write-decision-rules
3
+ description: >
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,
6
+ JSX elements, or whole files in an Oxlint run. Covers defineDecisionRule, createDecisionPlugin,
7
+ enabling the rules in oxlint.config.ts, select, answer kinds, context, per-rule models, settings,
8
+ explain mode, and keeping request cost under control. Also use when someone wants a lint check
9
+ that a pattern-based linter cannot express. Not for choosing or enabling the ready-made rules in
10
+ @r_masseater/oxlint-plugin-decision-presets.
11
+ metadata:
12
+ purpose: >
13
+ Teach an agent to author decision rules with @r_masseater/oxlint-plugin-decision, load them into
14
+ Oxlint, tune their questions and cutoffs, and avoid the mistakes that silently report nothing or
15
+ send far more paid requests than intended.
16
+ type: core
17
+ library: "@r_masseater/oxlint-plugin-decision"
18
+ library_version: 0.0.1
19
+ sources:
20
+ - src/**
21
+ - README.md
22
+ - example/**
23
+ - tests/skills/write-decision-rules/**
24
+ - tests/skill-write-decision-rules.test.ts
25
+ ---
26
+
27
+ # Write decision rules
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.
30
+
31
+ ## Setup
32
+
33
+ 1. Install the plugin next to Oxlint (`oxlint` >= 1.87.0 is a peer dependency):
34
+
35
+ ```sh
36
+ npm i -D oxlint @r_masseater/oxlint-plugin-decision
37
+ export OPENROUTER_API_KEY="..."
38
+ ```
39
+
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
+
42
+ 2. Write a plugin file that default-exports `createDecisionPlugin`:
43
+
44
+ ```js
45
+ // decision-plugin.mjs
46
+ import { createDecisionPlugin, defineDecisionRule } from "@r_masseater/oxlint-plugin-decision";
47
+
48
+ export default createDecisionPlugin({
49
+ name: "team",
50
+ rules: [
51
+ defineDecisionRule({
52
+ id: "no-pii-in-logs",
53
+ target: "call",
54
+ question:
55
+ "Does this call write personal data, such as an email or phone number, to a log or console?",
56
+ cutoff: 0.8,
57
+ }),
58
+ ],
59
+ });
60
+ ```
61
+
62
+ 3. Load the file and turn each rule on as `<plugin name>/<rule id>`:
63
+
64
+ ```ts
65
+ // oxlint.config.ts
66
+ import { defineConfig } from "oxlint";
67
+
68
+ export default defineConfig({
69
+ jsPlugins: ["./decision-plugin.mjs"],
70
+ rules: {
71
+ "team/no-pii-in-logs": "error",
72
+ },
73
+ });
74
+ ```
75
+
76
+ 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
+ `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
+
80
+ ## Core Patterns
81
+
82
+ ### Pick the target and phrase the question
83
+
84
+ | `target` | The model reads | The error underlines |
85
+ | ------------ | -------------------------------------------------------- | -------------------- |
86
+ | `"function"` | The whole function; arrows and methods include the name. | The signature line |
87
+ | `"call"` | The whole call expression. | The whole call |
88
+ | `"jsx"` | The whole element, children included. | The opening tag |
89
+ | `"file"` | The whole file. | The first line |
90
+
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
+
93
+ Rule fields: `id` (lowercase words joined by hyphens, unique in the plugin), `target`, `question`, `cutoff` (0 to 1, inclusive), and optional `message`, `select`, `answer`, `context`, `model`, and `location` (file rules only).
94
+
95
+ ### Narrow matches with `select` before any request
96
+
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
+
99
+ ```js
100
+ defineDecisionRule({
101
+ id: "test-proves-its-name",
102
+ target: "call",
103
+ select: (node) =>
104
+ node.type === "CallExpression" &&
105
+ node.callee.type === "Identifier" &&
106
+ ["it", "test"].includes(node.callee.name),
107
+ question: "Would this test still pass if the behaviour its name claims were broken?",
108
+ cutoff: 0.7,
109
+ });
110
+ ```
111
+
112
+ Use `select` for structure the AST already answers; leave judgement to the question.
113
+
114
+ ### Choose the answer kind
115
+
116
+ ```js
117
+ // classify: report when the picked label is in `report` and its probability clears cutoff
118
+ defineDecisionRule({
119
+ id: "stale-comment",
120
+ target: "function",
121
+ question: "How does the comment right above this function relate to what the function does?",
122
+ cutoff: 0.7,
123
+ context: { scope: "comment" },
124
+ answer: {
125
+ kind: "classify",
126
+ labels: {
127
+ accurate: "The comment describes what the code does",
128
+ stale: "The comment describes behavior the code no longer has",
129
+ redundant: "The comment repeats the code without adding anything",
130
+ },
131
+ report: ["stale"],
132
+ },
133
+ });
134
+
135
+ // severity: report when the rating is at least `minimum` (default "warn") and clears cutoff
136
+ defineDecisionRule({
137
+ id: "security-smell",
138
+ target: "function",
139
+ question: "Does this function handle untrusted input in a way that could be exploited?",
140
+ cutoff: 0.6,
141
+ answer: { kind: "severity", minimum: "warn" },
142
+ });
143
+ ```
144
+
145
+ The kinds are `boolean` (default), `classify`, and `severity`. You never write a schema.
146
+
147
+ ### Send surrounding source with `context`
148
+
149
+ | `context` | Sent with the match |
150
+ | ------------------------------------------ | ------------------------------------------------------ |
151
+ | `{ scope: "node" }` (default) | Nothing. |
152
+ | `{ scope: "lines", before: 5, after: 5 }` | Numbered lines around the match; both default to 5. |
153
+ | `{ scope: "enclosing", kind: "function" }` | The nearest enclosing `function`, `class`, or `block`. |
154
+ | `{ scope: "file" }` | The whole file. |
155
+ | `{ scope: "comment" }` | The match starting at the comments directly above it. |
156
+
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
+
159
+ ### Point a file rule at a line
160
+
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 greater than 0.5 and at most 1. An uncertain location, or a file cut by `maxSnippetChars`, keeps the first-line diagnostic.
162
+
163
+ ### Set models and plugin settings
164
+
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
+
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. |
175
+
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
+
178
+ When tuning a question or cutoff, or switching models, read [tuning rules](references/tuning.md) first.
179
+
180
+ When the rules must ask a decision service other than OpenRouter, read [custom providers](references/providers.md) before writing one.
181
+
182
+ ## Common Mistakes
183
+
184
+ ### HIGH: the API key is put in code or config
185
+
186
+ ```js
187
+ // Wrong: there is no apiKey setting; createDecisionPlugin rejects unknown keys
188
+ createDecisionPlugin({ name: "team", apiKey: process.env.KEY, rules });
189
+
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 });
192
+ ```
193
+
194
+ Keys are read only from environment variables; `env` values are variable names, not keys (`src/options.ts`, `src/runtime.ts`).
195
+
196
+ ### HIGH: a broad target on a whole codebase
197
+
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.
199
+
200
+ ### HIGH: decision rules on in the editor config
201
+
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:
203
+
204
+ ```ts
205
+ // oxlint.ci.config.ts
206
+ import { defineConfig } from "oxlint";
207
+
208
+ import base from "./oxlint.config.ts";
209
+
210
+ export default defineConfig({
211
+ extends: [base],
212
+ rules: { "team/no-pii-in-logs": "error" },
213
+ });
214
+ ```
215
+
216
+ Run it with `oxlint -c oxlint.ci.config.ts`. Keep `jsPlugins` in the base config so the overlay inherits it.
217
+
218
+ ### MEDIUM: a question where "yes" means "fine"
219
+
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.
221
+
222
+ ### MEDIUM: rule ids that are not lowercase-hyphenated or are repeated
223
+
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`).
225
+
226
+ ## Completion
227
+
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)).
230
+ 3. If the plugin fails to load, fix the rule or setting the thrown error names; do not remove validation.
@@ -0,0 +1,51 @@
1
+ # Custom providers
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.
4
+
5
+ ## Define and register a provider
6
+
7
+ ```js
8
+ // decision-plugin.mjs
9
+ import {
10
+ createDecisionPlugin,
11
+ defineDecisionRule,
12
+ defineProvider,
13
+ } from "@r_masseater/oxlint-plugin-decision";
14
+
15
+ export default createDecisionPlugin({
16
+ name: "team",
17
+ model: "internal/v2",
18
+ rules: [
19
+ defineDecisionRule({
20
+ id: "no-swallowed-catch",
21
+ target: "function",
22
+ question: "Does this function catch an error and neither handle nor rethrow it?",
23
+ cutoff: 0.8,
24
+ }),
25
+ ],
26
+ providers: [
27
+ defineProvider({
28
+ name: "internal",
29
+ requiredEnv: ["INTERNAL_DECIDER_KEY"],
30
+ decide: ({ request, timeoutMs, env }) => {
31
+ const key = process.env[env.INTERNAL_DECIDER_KEY ?? "INTERNAL_DECIDER_KEY"];
32
+ return { ok: true, response: callYourDecider(request, key, timeoutMs) };
33
+ },
34
+ }),
35
+ ],
36
+ });
37
+ ```
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.
42
+
43
+ ## The `decide` contract
44
+
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.
50
+
51
+ Check: run oxlint with `OXLINT_DECISION_EXPLAIN=1`. Each match should show your model id and a probability, not an `error` line.
@@ -0,0 +1,30 @@
1
+ # Tuning rules
2
+
3
+ Read this when a rule reports too much or too little, or before switching a rule to another model.
4
+
5
+ ## See why each match was or wasn't reported
6
+
7
+ ```sh
8
+ OXLINT_DECISION_EXPLAIN=1 npx oxlint # readable text on stderr
9
+ OXLINT_DECISION_EXPLAIN=json npx oxlint # one JSON object per match
10
+ ```
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.
13
+
14
+ ## Adjust the question before the cutoff
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.
19
+
20
+ 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
+
22
+ ## Switching or mixing models
23
+
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.5 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.
27
+
28
+ ## Cache
29
+
30
+ Answers are cached under `node_modules/.cache/oxlint-plugin-decision`, keyed by endpoint and request. Unchanged code is not asked again. Persist that directory between CI runs to keep it warm. Malformed answers are never cached.