omakit 0.4.2 → 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.
package/README.md CHANGED
@@ -13,7 +13,7 @@ npm i -g omakit && omakit setup
13
13
  npx skills add mtolhuys/omakit
14
14
  ```
15
15
 
16
- Requirements: [Node >=22](package.json); Omarchy Quattro.
16
+ Runs where [Node 22+](package.json) and Git run; `weigh` and `audit` need a running Omarchy shell.
17
17
 
18
18
  Licence: [MIT](LICENSE).
19
19
 
@@ -35,6 +35,12 @@ Checks your submission commits and names the action that re-runs stale validatio
35
35
 
36
36
  Compares your installed plugin commits with the marketplace's validated commits. The GIF shows drift rows first: on the author's desktop, 9 of 18 audited plugins ran commits the marketplace never validated.
37
37
 
38
+ ## `omakit inspect <plugin-dir>`
39
+
40
+ ![inspect showing a size score and the two review classes one fixture shows](https://raw.githubusercontent.com/mtolhuys/omakit/main/docs/media/inspect.gif)
41
+
42
+ Reads a plugin's tree and prints what needs attention, biggest first: a size score (the share of its function lines that sit in functions over the measured size, placed among the 50 listed trees' shares, so a tree with no long function scores 10.00, [M12](docs/MEASUREMENTS.md#m12-how-long-a-plugins-functions-are-in-listed-trees)), the functions over what 90 of 100 listed functions stay under, then each review class the tree shows with the class's measured share of review findings ([M11](docs/MEASUREMENTS.md#m11-what-the-human-review-raises-by-class)) and up to five sites. No verdict, nothing run from the tree; `--full` is every site, `--json` the document. Over 18 listed plugins read at their validated commits, the extraction counted 515 process sites (57 QML `Process` blocks, 458 shell lines), 17 hosts, 63 writes and 40 timers, left 15 rows it could not resolve, and printed 73 pattern rows across 17 of the 18 ([record](docs/evidence/inspect/2026-09-15-listed-sample.json)).
43
+
38
44
  ## `omakit weigh <plugin>`
39
45
 
40
46
  ![completed three-run desktop weighing with baseline samples and the noise floor](https://raw.githubusercontent.com/mtolhuys/omakit/main/docs/media/weigh.gif)
@@ -48,13 +54,13 @@ Measures the shell with and without your plugin, reading Pss and CPU. The GIF sh
48
54
  | Baseline parity | 30/30 identical results, [recorded corpus](docs/evidence/parity/2026-09-12-local-vs-github-2.json), 2026-09-12 |
49
55
  | Stale validated commit | 326/519 readable comparisons stale (62.8%); 64/583 unknown, [2026-09-15 data](docs/evidence/staleness/2026-09-15.json) |
50
56
  | Registry churn | 4,201/4,293 registry-only commits in 30 days, 2026-09-13, [M7](docs/MEASUREMENTS.md#m7-the-registry-moves-by-the-hour-the-code-and-the-rules-move-by-the-week) |
51
- | GIFs are recorded output | 5 GIFs with [captures and scenes](docs/media/README.md) |
57
+ | GIFs are recorded output | 6 GIFs with [captures and scenes](docs/media/README.md) |
52
58
  | Posts nothing | 0 marketplace writes, [M10](docs/MEASUREMENTS.md#m10-readme-evidence-and-command-captures) |
53
59
  | Zero dependencies | 0 runtime and 0 development dependencies, counted in [package.json](package.json) |
54
60
 
55
61
  ## Documentation
56
62
 
57
63
  - Using: [install](docs/INSTALL.md), [commands](docs/COMMANDS.md), [audience](docs/MARKETPLACE.md).
58
- - Checks and measurements: [submit](docs/SUBMIT.md), [watch](docs/VALIDATION_WATCH.md), [audit](docs/AUDIT.md), [evidence](docs/MEASUREMENTS.md).
64
+ - Checks and measurements: [submit](docs/SUBMIT.md), [inspect](docs/INSPECT.md), [watch](docs/VALIDATION_WATCH.md), [audit](docs/AUDIT.md), [evidence](docs/MEASUREMENTS.md).
59
65
  - Method docs: [how](docs/HOW.md), [weigh](docs/WEIGH.md), [upstream contract](docs/UPSTREAM_CONTRACT.md), [palette](docs/PALETTE.md), [terminal](docs/TUI.md).
60
66
  - Contributing: [repository rules](AGENTS.md), [releasing](docs/RELEASING.md), [media](docs/media/README.md).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "omakit",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "description": "The safe place to find out: everything knowable about an Omarchy Quattro plugin submission before you post it, on your own machine. Agent-first, read-only against the marketplace, posts nothing, zero dependencies.",
5
5
  "license": "MIT",
6
6
  "author": "Maarten Tolhuijs",
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: omarchy-plugin-check
3
- description: Check an Omarchy Quattro plugin while building or changing it, before committing or pushing. Use whenever you create, edit, refactor or test an Omarchy plugin, when asked whether a plugin is marketplace-ready, or before any push to its default branch. Runs the marketplace's own security baseline and submission checks locally, read-only, and reports what the marketplace would refuse.
3
+ description: Check an Omarchy Quattro plugin while building or changing it, before committing or pushing. Use whenever you create, edit, refactor or test an Omarchy plugin, when asked whether a plugin is marketplace-ready, or before any push to its default branch. Runs the marketplace's own security baseline and submission checks locally, read-only, reports what the marketplace would refuse, and lists what the tree does (processes, hosts, writes, timers) as observations beside the review classes a human reviewer raises most.
4
4
  ---
5
5
 
6
6
  # Checking a plugin while you build it
@@ -31,6 +31,68 @@ editorial choice about where the plugin belongs.
31
31
  needs the network and reads the listed ids from the pin. For the real
32
32
  submission, drop it, and use `skills/omarchy-plugin-submit/SKILL.md`.
33
33
 
34
+ ## Read what the tree does before a human does
35
+
36
+ After the two commands above pass, and again whenever you add or change a
37
+ `Process`, a `curl`, a `FileView`, a shell script or a `Timer`, run:
38
+
39
+ ```bash
40
+ omakit inspect <path-to-the-plugin-repo> --json
41
+ ```
42
+
43
+ `inspect` lists what the tree shows, in the order a reviewer reads it: every
44
+ process with its argv and whether a deadline is observed for it, every host
45
+ with its timeout and size-cap flags, every write with whether its path falls
46
+ under a directory the plugin controls, every timer with its interval, and the
47
+ baseline's capabilities. Below the facts, `patterns` holds one entry per
48
+ review class the marketplace's human review has raised, only where the tree
49
+ shows the class's precondition (a process with no deadline, a collector with
50
+ no cap, a write under `/tmp`, `curl` without `-q`), with the class's measured
51
+ share of review findings (`share`, from M11 of `docs/MEASUREMENTS.md`).
52
+ `lookedFor` names the classes whose precondition was not observed.
53
+ `size.over` lists the functions longer, more branched or deeper than 90 of
54
+ 100 functions in listed trees (the thresholds are in `size.thresholds`,
55
+ from M12), longest first, each with its `percentile` among them, and
56
+ `size.heavyShare` is the share of the tree's function lines that sit in
57
+ those functions, and `size.score` is 10 minus the share of listed trees
58
+ with a strictly smaller `heavyShare`, divided by 10: 10.00 when no
59
+ function is over a threshold, 0.00 when the tree is heavier than every
60
+ listed one (`size.sample.heavyShares` holds the listed trees' shares). It
61
+ is line-weighted, so splitting a long function into short ones raises it
62
+ and adding small functions beside a long one barely moves it. When the
63
+ owner asks for simpler code, start with the top of `size.over`, and read
64
+ the score before and after as the measure of the change; it is a position
65
+ among listed plugins, not a grade, so never tell the owner a score is good
66
+ or bad, only that it moved. Read
67
+ the document, not the report: the report a person sees lists at most five
68
+ sites per class and drops classes under five percent, `--full` prints every
69
+ site, and `--json` carries all of it either way.
70
+
71
+ How to read "observed". Every row is what regular expressions found in the
72
+ text at `file:line`, never a runtime fact and never a verdict: `deadline
73
+ observed` means a killing `Timer`, a `timeout` in argv or a destruction
74
+ handler is in the file; `no cap observed` means the argv shows none of
75
+ `head -c`, `--max-filesize` or `timeout`; `observed nothing of this kind`
76
+ means the extraction found nothing, not that the tree is clean. A `▒ ?` row
77
+ (`argvForm: "computed"`, `argv: null`) is a command the text does not show
78
+ as a literal; do not guess it for the owner, read the file. `notVisible`
79
+ names what the method cannot see (commands built at run time, values from
80
+ variables or config, components outside the tree), and a tree that uses
81
+ those has facts `inspect` did not list.
82
+
83
+ What to do with a pattern row. It is not a marketplace rule and not a
84
+ finding: the marketplace's automated baseline blocks, a maintainer reviews,
85
+ and `inspect` only reports that the tree shows something reviewers have
86
+ raised in about N of every 100 findings. Show the owner the row and the
87
+ requirement in the reviewer's own terms from the M11 table (an absolute
88
+ deadline, producer-side bounds, a private 0700 directory, `curl -q`, no
89
+ secret in argv), and let the owner decide; never describe the row as a
90
+ requirement the marketplace enforces, and never say "safe" or "clean" about
91
+ a tree with no rows. The `supply-chain` row is the baseline's own finding
92
+ restated with its evidence sites; the baseline result is what decides there.
93
+ Exit status is 0 whenever a report was produced, so do not read the exit
94
+ code as pass or fail; 2 means the target could not be read.
95
+
34
96
  ## If omakit is not installed
35
97
 
36
98
  ```bash
@@ -0,0 +1,256 @@
1
+ // The JSON contract of docs/INSPECT.md, executable. `validateInspectDocument()`
2
+ // returns every way a document departs from it, as sentences, and an empty
3
+ // list when it does not. tests/unit/inspect.test.mjs holds every produced
4
+ // document to it, so the prose and the code cannot drift apart unnoticed.
5
+ //
6
+ // Run as a program: `node tools/inspect/contract.mjs <document.json>` prints
7
+ // the problems and exits 1 on any.
8
+
9
+ import { readFileSync } from "node:fs"
10
+ import { resolve } from "node:path"
11
+ import { pathToFileURL } from "node:url"
12
+
13
+ const ARGV_FORMS = new Set(["array", "string", "computed"])
14
+ const DEADLINE_VIA = new Set(["timer-kill", "timeout-argv", "destruction", null])
15
+ const COLLECTORS = new Set(["StdioCollector", "SplitParser", "none", "unknown"])
16
+ const CONTROLLED = new Set(["observed", "not-observed", "unknown"])
17
+ const DECLARED_IN = new Set(["qml", "shell"])
18
+ const FILE_KINDS = ["qml", "js", "shell", "python", "other"]
19
+
20
+ const isString = (value) => typeof value === "string"
21
+ const isBool = (value) => typeof value === "boolean"
22
+ const isInt = (value) => Number.isInteger(value)
23
+ const nullOr = (test) => (value) => value === null || test(value)
24
+
25
+ function site(value, at, problems) {
26
+ if (!value || typeof value !== "object") {
27
+ problems.push(`${at} is not a site`)
28
+ return
29
+ }
30
+ if (!isString(value.file)) problems.push(`${at}.file is not a string`)
31
+ if (!isInt(value.line) || value.line < 1) problems.push(`${at}.line is not a positive integer`)
32
+ }
33
+
34
+ function processRow(row, at, problems) {
35
+ site(row, at, problems)
36
+ if (!DECLARED_IN.has(row.declaredIn)) problems.push(`${at}.declaredIn is neither qml nor shell`)
37
+ if (!nullOr(isString)(row.id)) problems.push(`${at}.id is neither a string nor null`)
38
+ if (!ARGV_FORMS.has(row.argvForm)) problems.push(`${at}.argvForm is not array, string or computed`)
39
+ if (row.argvForm === "computed") {
40
+ if (row.argv !== null) problems.push(`${at}.argv is not null for a computed command`)
41
+ if (!isString(row.commandText)) problems.push(`${at}.commandText is not the expression text of a computed command`)
42
+ } else if (!Array.isArray(row.argv) || row.argv.some((word) => !isString(word))) problems.push(`${at}.argv is not a list of strings`)
43
+ if (!Array.isArray(row.expressions) || row.expressions.some((entry) => !isInt(entry?.index) || !isString(entry?.text))) problems.push(`${at}.expressions is not a list of { index, text }`)
44
+ if (row.argvForm === "array" && Array.isArray(row.argv)) {
45
+ for (const entry of row.expressions || []) if (row.argv[entry.index] !== entry.text) problems.push(`${at}.expressions[${entry.index}] does not name the argv element it stands for`)
46
+ }
47
+ for (const key of ["running", "detached", "shellWrapper"]) if (!isBool(row[key])) problems.push(`${at}.${key} is not a boolean`)
48
+ const deadline = row.deadline || {}
49
+ if (!isBool(deadline.observed)) problems.push(`${at}.deadline.observed is not a boolean`)
50
+ if (!DEADLINE_VIA.has(deadline.via)) problems.push(`${at}.deadline.via is not timer-kill, timeout-argv, destruction or null`)
51
+ if (deadline.observed !== (deadline.via !== null)) problems.push(`${at}.deadline.observed disagrees with deadline.via`)
52
+ if (!nullOr(isInt)(deadline.ms)) problems.push(`${at}.deadline.ms is neither an integer nor null`)
53
+ const output = row.output || {}
54
+ if (!COLLECTORS.has(output.collector)) problems.push(`${at}.output.collector is not StdioCollector, SplitParser, none or unknown`)
55
+ if (!isBool(output.capObserved)) problems.push(`${at}.output.capObserved is not a boolean`)
56
+ if (!nullOr(isString)(output.via)) problems.push(`${at}.output.via is neither a string nor null`)
57
+ if (output.capObserved !== (output.via !== null)) problems.push(`${at}.output.capObserved disagrees with output.via`)
58
+ if (row.pipedFrom !== null && (!isInt(row.pipedFrom?.line) || !isString(row.pipedFrom?.argv0))) problems.push(`${at}.pipedFrom is neither null nor { line, argv0 }`)
59
+ }
60
+
61
+ function host(row, at, problems) {
62
+ site(row, at, problems)
63
+ if (!isString(row.host) || !row.host) problems.push(`${at}.host is not a host name`)
64
+ if (row.scheme !== "http" && row.scheme !== "https") problems.push(`${at}.scheme is neither http nor https`)
65
+ if (!nullOr(isString)(row.tool)) problems.push(`${at}.tool is neither a string nor null`)
66
+ for (const key of ["timeout", "sizeCap"]) {
67
+ const flag = row[key] || {}
68
+ if (!isBool(flag.observed)) problems.push(`${at}.${key}.observed is not a boolean`)
69
+ if (!nullOr(isString)(flag.via)) problems.push(`${at}.${key}.via is neither a string nor null`)
70
+ if (flag.observed !== (flag.via !== null)) problems.push(`${at}.${key}.observed disagrees with ${key}.via`)
71
+ }
72
+ if (!Array.isArray(row.flags) || row.flags.some((flag) => !isString(flag))) problems.push(`${at}.flags is not a list of strings`)
73
+ if (!isBool(row.privateAddress)) problems.push(`${at}.privateAddress is not a boolean`)
74
+ }
75
+
76
+ function write(row, at, problems) {
77
+ site(row, at, problems)
78
+ if (!isString(row.path)) problems.push(`${at}.path is not a string`)
79
+ if (!nullOr(isString)(row.canonicalPath)) problems.push(`${at}.canonicalPath is neither a string nor null`)
80
+ if (!isString(row.via)) problems.push(`${at}.via is not a string`)
81
+ if (!CONTROLLED.has(row.controlledDirectory)) problems.push(`${at}.controlledDirectory is not observed, not-observed or unknown`)
82
+ if ((row.controlledDirectory === "observed") !== isString(row.controlledBy)) problems.push(`${at}.controlledBy names a directory exactly when controlledDirectory is observed`)
83
+ if (row.canonicalPath === null && row.controlledDirectory !== "unknown") problems.push(`${at}.controlledDirectory is decided for a path that could not be read`)
84
+ if (!isBool(row.temp)) problems.push(`${at}.temp is not a boolean`)
85
+ if (!nullOr(isString)(row.mode)) problems.push(`${at}.mode is neither a string nor null`)
86
+ }
87
+
88
+ function fn(row, at, problems) {
89
+ site(row, at, problems)
90
+ if (!isString(row.name) || !row.name) problems.push(`${at}.name is not a name`)
91
+ if (row.kind !== "function" && row.kind !== "handler") problems.push(`${at}.kind is neither function nor handler`)
92
+ for (const key of ["lines", "depth", "branches"]) if (!isInt(row[key]) || row[key] < 0) problems.push(`${at}.${key} is not a count`)
93
+ if (isInt(row.lines) && row.lines < 1) problems.push(`${at}.lines is under one`)
94
+ if (typeof row.percentile !== "number" || row.percentile < 0 || row.percentile > 100) problems.push(`${at}.percentile is not a rank between 0 and 100`)
95
+ }
96
+
97
+ function timer(row, at, problems) {
98
+ site(row, at, problems)
99
+ if (!nullOr(isString)(row.id)) problems.push(`${at}.id is neither a string nor null`)
100
+ if (!nullOr(isInt)(row.intervalMs)) problems.push(`${at}.intervalMs is neither an integer nor null`)
101
+ if (!nullOr(isString)(row.intervalText)) problems.push(`${at}.intervalText is neither a string nor null`)
102
+ if (row.intervalMs !== null && row.intervalText !== null) problems.push(`${at} has both an interval and an interval expression`)
103
+ for (const key of ["repeat", "running", "triggeredOnStart"]) if (!nullOr(isBool)(row[key])) problems.push(`${at}.${key} is neither a boolean nor null`)
104
+ if (!nullOr(isString)(row.startedBy)) problems.push(`${at}.startedBy is neither a string nor null`)
105
+ }
106
+
107
+ /**
108
+ * @param {object} document
109
+ * @param {{ patternIds?: string[], notVisible?: string[] }} [known] the ids
110
+ * patterns.mjs defines and the fixed blind-spot list, when the caller has
111
+ * them, so a pattern id the code does not define and a rewritten blind
112
+ * spot are both problems.
113
+ * @returns {string[]} problems; empty when the document follows the contract
114
+ */
115
+ export function validateInspectDocument(document, known = {}) {
116
+ const problems = []
117
+ if (!document || typeof document !== "object") return ["the document is not an object"]
118
+ if (!isString(document.omakit)) problems.push("omakit is not a string")
119
+ if (document.command !== "inspect") problems.push('command is not "inspect"')
120
+ if (!isString(document.method) || !/observed/.test(document.method)) problems.push("method is not the one sentence that says observed")
121
+ const subject = document.subject || {}
122
+ if (!isString(subject.dir)) problems.push("subject.dir is not a string")
123
+ if (!nullOr((value) => /^[0-9a-f]{40}$/.test(value))(subject.commit)) problems.push("subject.commit is neither a 40-character sha nor null")
124
+ if (!subject.repository || !nullOr(isString)(subject.repository.url)) problems.push("subject.repository.url is neither a string nor null")
125
+ const filesRead = subject.filesRead || {}
126
+ for (const kind of FILE_KINDS) if (!isInt(filesRead[kind]) || filesRead[kind] < 0) problems.push(`subject.filesRead.${kind} is not a count`)
127
+ for (const key of Object.keys(filesRead)) if (!FILE_KINDS.includes(key)) problems.push(`subject.filesRead.${key} is not a kind inspect reads`)
128
+ if (!isInt(subject.uncommittedFiles) || subject.uncommittedFiles < 0) problems.push("subject.uncommittedFiles is not a count")
129
+
130
+ const observed = document.observed || {}
131
+ for (const [key, check] of [["processes", processRow], ["hosts", host], ["writes", write], ["timers", timer], ["functions", fn]]) {
132
+ if (!Array.isArray(observed[key])) {
133
+ problems.push(`observed.${key} is not a list`)
134
+ continue
135
+ }
136
+ for (const [index, row] of observed[key].entries()) check(row, `observed.${key}[${index}]`, problems)
137
+ }
138
+ const counts = document.counts || {}
139
+ if (!counts.processes || typeof counts.processes !== "object") problems.push("counts.processes is not the qml and shell split")
140
+ else {
141
+ for (const key of ["total", "qml", "shell"]) if (!isInt(counts.processes[key]) || counts.processes[key] < 0) problems.push(`counts.processes.${key} is not a count`)
142
+ if (counts.processes.total !== counts.processes.qml + counts.processes.shell) problems.push("counts.processes.total is not qml plus shell")
143
+ if (Array.isArray(observed.processes)) {
144
+ if (counts.processes.total !== observed.processes.length) problems.push("counts.processes.total is not the number of process rows")
145
+ if (counts.processes.qml !== observed.processes.filter((row) => row.declaredIn === "qml").length) problems.push("counts.processes.qml is not the number of qml process rows")
146
+ }
147
+ }
148
+ for (const key of ["hosts", "writes", "timers", "functions"]) {
149
+ if (!isInt(counts[key]) || counts[key] < 0) problems.push(`counts.${key} is not a count`)
150
+ else if (Array.isArray(observed[key]) && counts[key] !== observed[key].length) problems.push(`counts.${key} is not the number of ${key} rows`)
151
+ }
152
+ if (!isInt(counts.notResolvable) || (Array.isArray(document.notResolvable) && counts.notResolvable !== document.notResolvable.length)) problems.push("counts.notResolvable is not the number of notResolvable rows")
153
+ if (!Array.isArray(document.notResolvable)) problems.push("notResolvable is not a list")
154
+ else for (const [index, row] of document.notResolvable.entries()) {
155
+ site(row, `notResolvable[${index}]`, problems)
156
+ if (!isString(row.kind) || !isString(row.text)) problems.push(`notResolvable[${index}] lacks kind and text`)
157
+ }
158
+ // Every computed command is also a site the extraction could not read.
159
+ for (const row of Array.isArray(observed.processes) ? observed.processes : []) {
160
+ if (row.argvForm === "computed" && !(document.notResolvable || []).some((entry) => entry.kind === "command" && entry.file === row.file && entry.line === row.line)) {
161
+ problems.push(`observed.processes at ${row.file}:${row.line} is computed but not listed under notResolvable`)
162
+ }
163
+ }
164
+
165
+ const size = document.size
166
+ if (!size || typeof size !== "object") problems.push("size is missing")
167
+ else {
168
+ if (!isString(size.measurement) || !/^M\d+$/.test(size.measurement)) problems.push("size.measurement is not a measurement id")
169
+ for (const key of ["lines", "branches", "depth"]) if (!isInt(size.thresholds?.[key]) || size.thresholds[key] < 1) problems.push(`size.thresholds.${key} is not a count`)
170
+ const shares = size.sample?.heavyShares
171
+ if (!size.sample || !isInt(size.sample.trees) || !isInt(size.sample.functions)) problems.push("size.sample is not { trees, functions, heavyShares }")
172
+ else if (!Array.isArray(shares) || shares.length !== size.sample.trees || !shares.every((share) => typeof share === "number" && share >= 0 && share <= 1)) problems.push("size.sample.heavyShares is not one share from 0 to 1 per listed tree")
173
+ const scorable = Array.isArray(observed.functions) && observed.functions.length > 0
174
+ if (typeof size.heavyShare !== "number" || size.heavyShare < 0 || size.heavyShare > 1) problems.push("size.heavyShare is not a share from 0 to 1")
175
+ else if (Array.isArray(observed.functions) && size.thresholds) {
176
+ const total = observed.functions.reduce((sum, row) => sum + row.lines, 0)
177
+ const heavy = observed.functions.filter((row) => row.lines > size.thresholds.lines || row.branches > size.thresholds.branches || row.depth > size.thresholds.depth).reduce((sum, row) => sum + row.lines, 0)
178
+ const expected = total ? heavy / total : 0
179
+ if (Math.abs(expected - size.heavyShare) > 0.00011) problems.push(`size.heavyShare is ${size.heavyShare}; the function lines over the thresholds say ${expected}`)
180
+ }
181
+ if (size.score === null) {
182
+ if (scorable) problems.push("size.score is null for a tree with functions")
183
+ } else if (typeof size.score !== "number" || size.score < 0 || size.score > 10 || Math.abs(Math.round(size.score * 100) - size.score * 100) > 1e-6) problems.push("size.score is not a number from 0 to 10 with two decimals")
184
+ else if (!scorable) problems.push("size.score is set for a tree with no function")
185
+ else if (Array.isArray(shares) && shares.length && typeof size.heavyShare === "number") {
186
+ const rank = (shares.filter((share) => share < size.heavyShare).length / shares.length) * 100
187
+ const expected = Math.round((10 - rank / 10) * 100) / 100
188
+ if (Math.abs(expected - size.score) > 0.011) problems.push(`size.score is ${size.score}; the tree's rank among the listed shares says ${expected}`)
189
+ }
190
+ if (!Array.isArray(size.over)) problems.push("size.over is not a list")
191
+ else {
192
+ for (const [index, row] of size.over.entries()) {
193
+ fn(row, `size.over[${index}]`, problems)
194
+ if (size.thresholds && !(row.lines > size.thresholds.lines || row.branches > size.thresholds.branches || row.depth > size.thresholds.depth)) problems.push(`size.over[${index}] is under every threshold`)
195
+ if (index && size.over[index - 1].lines < row.lines) problems.push(`size.over[${index}] is longer than the one before it; the list is longest first`)
196
+ }
197
+ if (Array.isArray(observed.functions)) {
198
+ const listed = new Set(size.over.map((row) => `${row.file}:${row.line}`))
199
+ for (const row of observed.functions) {
200
+ const over = row.lines > size.thresholds?.lines || row.branches > size.thresholds?.branches || row.depth > size.thresholds?.depth
201
+ if (over && !listed.has(`${row.file}:${row.line}`)) problems.push(`observed.functions at ${row.file}:${row.line} is over a threshold but not under size.over`)
202
+ }
203
+ }
204
+ }
205
+ }
206
+ if (!Array.isArray(document.patterns)) problems.push("patterns is not a list")
207
+ else for (const [index, row] of document.patterns.entries()) {
208
+ const at = `patterns[${index}]`
209
+ if (!isString(row.id)) problems.push(`${at}.id is not a string`)
210
+ else if (known.patternIds && !known.patternIds.includes(row.id)) problems.push(`${at}.id ${row.id} is not a pattern patterns.mjs defines`)
211
+ if (!isInt(row.observedCount) || row.observedCount < 1) problems.push(`${at}.observedCount is not a positive count: a pattern is listed only where its precondition was observed`)
212
+ if (!Array.isArray(row.sites) || row.sites.length === 0) problems.push(`${at}.sites is not a non-empty list`)
213
+ else for (const [n, entry] of row.sites.entries()) site(entry, `${at}.sites[${n}]`, problems)
214
+ if (!isString(row.measurement) || !/^M\d+$/.test(row.measurement)) problems.push(`${at}.measurement is not a measurement id`)
215
+ if (typeof row.share !== "number" || !(row.share > 0 && row.share < 1)) problems.push(`${at}.share is not a share between 0 and 1`)
216
+ if (!isString(row.observation) || !/^observed /.test(row.observation)) problems.push(`${at}.observation does not start with the word observed`)
217
+ if (!isString(row.summary) || !row.summary || /\w:\d+/.test(row.summary)) problems.push(`${at}.summary is not the observation without its sites`)
218
+ if (/\b(?:missing|should|fix)\b/i.test(String(row.observation))) problems.push(`${at}.observation reads as a verdict`)
219
+ }
220
+ if (!Array.isArray(document.lookedFor) || document.lookedFor.some((id) => !isString(id))) problems.push("lookedFor is not a list of pattern ids")
221
+ else {
222
+ const listed = new Set((document.patterns || []).map((row) => row.id))
223
+ for (const id of document.lookedFor) {
224
+ if (listed.has(id)) problems.push(`lookedFor names ${id}, which is also under patterns`)
225
+ if (known.patternIds && !known.patternIds.includes(id)) problems.push(`lookedFor names ${id}, which patterns.mjs does not define`)
226
+ }
227
+ if (known.patternIds) {
228
+ for (const id of known.patternIds) if (!listed.has(id) && !document.lookedFor.includes(id)) problems.push(`pattern ${id} is neither under patterns nor under lookedFor`)
229
+ }
230
+ }
231
+ if (!Array.isArray(document.notVisible) || document.notVisible.some((line) => !isString(line))) problems.push("notVisible is not a list of sentences")
232
+ else if (known.notVisible && JSON.stringify(document.notVisible) !== JSON.stringify(known.notVisible)) problems.push("notVisible is not the fixed blind-spot list, verbatim")
233
+
234
+ const baseline = document.marketplaceBaseline
235
+ if (!baseline || typeof baseline !== "object") problems.push("marketplaceBaseline is missing")
236
+ else if (baseline.skipped === true) {
237
+ if (!isString(baseline.reason)) problems.push("marketplaceBaseline.reason is not a string")
238
+ } else {
239
+ if (!baseline.pin || !isString(baseline.pin.commit)) problems.push("marketplaceBaseline.pin.commit is not a string")
240
+ if (!isString(baseline.transport)) problems.push("marketplaceBaseline.transport is not a string")
241
+ if (!isBool(baseline.invoked)) problems.push("marketplaceBaseline.invoked is not a boolean")
242
+ if (!isString(baseline.statement)) problems.push("marketplaceBaseline.statement is not a string")
243
+ if (baseline.invoked && (!baseline.official || typeof baseline.official !== "object")) problems.push("marketplaceBaseline.official is missing for an invoked baseline")
244
+ }
245
+ return problems
246
+ }
247
+
248
+ const invoked = process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href
249
+ if (invoked) {
250
+ const input = process.argv[2]
251
+ if (!input) throw new Error("usage: node tools/inspect/contract.mjs <document.json>")
252
+ const problems = validateInspectDocument(JSON.parse(readFileSync(resolve(input), "utf8")))
253
+ for (const problem of problems) process.stdout.write(`${problem}\n`)
254
+ process.stdout.write(problems.length ? `${problems.length} problem(s)\n` : "ok: the document follows the contract in docs/INSPECT.md\n")
255
+ process.exit(problems.length ? 1 : 0)
256
+ }
@@ -0,0 +1,171 @@
1
+ // Function sites: every named function (declared, assigned as an arrow, or
2
+ // a method), QML handler, shell function and Python def, with its length
3
+ // in lines, its deepest nesting and its branch count. Size is the one
4
+ // thing a person asks about a tree that regular expressions can answer
5
+ // without a parser: where does the logic pile up.
6
+ // The numbers are counts over the text; the threshold they are compared
7
+ // with is measured over listed trees (M12), never chosen.
8
+
9
+ import { blankComments, closingBracket, lineOf } from "./text.mjs"
10
+
11
+ const JS_BRANCH = /\b(?:if|else if|for|while|do|switch|case|catch)\b|&&|\|\||\?[^.:]/g
12
+ const SHELL_OPEN = /^\s*(?:if|for|while|until|case|select)\b/
13
+ const SHELL_CLOSE = /^\s*(?:fi|done|esac)\b/
14
+ const SHELL_BRANCH = /\b(?:if|elif|for|while|until|case)\b|\|\||&&|^\s*[^)]*\)\s*(?!\s*$)/
15
+ const PY_BRANCH = /^\s*(?:if|elif|for|while|except|with)\b|\band\b|\bor\b/
16
+
17
+ /**
18
+ * Deepest brace nesting inside a body, relative to the body itself. A `{`
19
+ * that opens a literal is not a level: an object or array literal spread
20
+ * over lines (`return {`, `foo({`, `x = {`, `[{`, `? {`, `a || {`) and an
21
+ * inline arrow body (`=> {`) are values, not control flow. The test is the
22
+ * last non-space text before the brace, so this is a regular-expression
23
+ * heuristic, not a parser: it aims at an object literal not counting as
24
+ * nesting, and a `{` after `)` or `else` or on a line of its own counts.
25
+ */
26
+ const LITERAL_BEFORE = /(?:[(,:=[?]|\breturn|=>|\|\||&&)\s*$/
27
+
28
+ function braceDepth(body) {
29
+ let depth = 0
30
+ let deepest = 0
31
+ let quote = null
32
+ const literal = []
33
+ for (let i = 0; i < body.length; i += 1) {
34
+ const ch = body[i]
35
+ if (quote) {
36
+ if (ch === "\\") i += 1
37
+ else if (ch === quote) quote = null
38
+ continue
39
+ }
40
+ if (ch === '"' || ch === "'" || ch === "`") quote = ch
41
+ else if (ch === "{") {
42
+ const isLiteral = LITERAL_BEFORE.test(body.slice(Math.max(0, i - 12), i))
43
+ literal.push(isLiteral)
44
+ if (isLiteral) continue
45
+ depth += 1
46
+ if (depth > deepest) deepest = depth
47
+ } else if (ch === "}") {
48
+ if (!literal.pop()) depth -= 1
49
+ }
50
+ }
51
+ return deepest
52
+ }
53
+
54
+ /** Words that stand before `(...) {` without naming a method. */
55
+ const NOT_A_METHOD = new Set(["if", "for", "while", "switch", "catch", "with", "function", "return", "else", "do", "try"])
56
+
57
+ function jsFunctions(file) {
58
+ const text = blankComments(file.text)
59
+ const rows = []
60
+ // Four shapes, each followed by a body block: `function name(`; in QML,
61
+ // `onSomething: {` handlers; a named arrow function, `const load = (rows)
62
+ // => {` or `this.load = rows => {` on a property; and method shorthand,
63
+ // `load(rows) {` at the start of a line inside an object literal or a
64
+ // class. An anonymous callback (`(x) => {`, `function (x) {`) has no name
65
+ // and is not counted; the method shape excludes the keywords that stand
66
+ // before `(...) {` (`if`, `for`, `while`, `switch`, `catch`).
67
+ const pattern = new RegExp([
68
+ /(?<![\w.])function\s+([A-Za-z_$][\w$]*)\s*\(/.source,
69
+ /(?<![\w.])(on[A-Z]\w*)\s*:\s*(?=\{)/.source,
70
+ /(?<![\w$])([A-Za-z_$][\w$]*)\s*=\s*(?:async\s*)?(?:\([^()\n]*\)|[A-Za-z_$][\w$]*)\s*=>[ \t]*(?=\{)/.source,
71
+ /^[ \t]*(?:(?:async|static)\s+)*([A-Za-z_$][\w$]*)[ \t]*\([^()\n]*\)[ \t]*(?=\{)/.source,
72
+ ].join("|"), "gm")
73
+ for (const match of text.matchAll(pattern)) {
74
+ if (match[4] && NOT_A_METHOD.has(match[4])) continue
75
+ const open = text.indexOf("{", match.index + match[0].length)
76
+ if (open < 0) continue
77
+ const end = closingBracket(text, open)
78
+ if (end < 0) continue
79
+ const body = text.slice(open + 1, end)
80
+ const start = lineOf(text, match.index + match[0].length - match[0].trimStart().length)
81
+ rows.push({
82
+ file: file.path,
83
+ line: start,
84
+ name: match[1] || match[2] || match[3] || match[4],
85
+ kind: match[2] ? "handler" : "function",
86
+ lines: lineOf(text, end) - start + 1,
87
+ depth: braceDepth(body),
88
+ branches: (body.match(JS_BRANCH) || []).length,
89
+ })
90
+ }
91
+ return rows
92
+ }
93
+
94
+ function shellFunctions(file) {
95
+ const lines = file.text.split("\n")
96
+ const rows = []
97
+ for (let index = 0; index < lines.length; index += 1) {
98
+ const head = lines[index].match(/^\s*(?:function\s+)?([A-Za-z_][\w-]*)\s*\(\)\s*\{?\s*$|^\s*function\s+([A-Za-z_][\w-]*)\s*\{?\s*$/)
99
+ if (!head) continue
100
+ const name = head[1] || head[2]
101
+ // The body runs to the line that is only `}` at the function's own indent.
102
+ const indent = lines[index].match(/^\s*/)[0]
103
+ let end = index
104
+ let depth = 0
105
+ let deepest = 0
106
+ let branches = 0
107
+ for (let at = index + 1; at < lines.length; at += 1) {
108
+ const line = lines[at]
109
+ if (line.trim() === "}" && line.startsWith(indent) && line.match(/^\s*/)[0].length === indent.length) {
110
+ end = at
111
+ break
112
+ }
113
+ if (SHELL_OPEN.test(line)) {
114
+ depth += 1
115
+ if (depth > deepest) deepest = depth
116
+ }
117
+ if (SHELL_CLOSE.test(line)) depth -= 1
118
+ if (SHELL_BRANCH.test(line.split("#")[0])) branches += 1
119
+ end = at
120
+ }
121
+ rows.push({ file: file.path, line: index + 1, name, kind: "function", lines: end - index + 1, depth: deepest, branches })
122
+ index = end
123
+ }
124
+ return rows
125
+ }
126
+
127
+ function pythonFunctions(file) {
128
+ const lines = file.text.split("\n")
129
+ const rows = []
130
+ for (let index = 0; index < lines.length; index += 1) {
131
+ const head = lines[index].match(/^(\s*)(?:async\s+)?def\s+([A-Za-z_]\w*)\s*\(/)
132
+ if (!head) continue
133
+ const base = head[1].length
134
+ let end = index
135
+ let deepest = 0
136
+ let branches = 0
137
+ // Depth is relative to the body, the way it is for a brace block: the
138
+ // body's own indent is depth 0 and one `if` is depth 1. The unit is
139
+ // one indentation step, read from the first body line (4 spaces by
140
+ // PEP 8, 2 in some trees); 4 is assumed when there is no body line.
141
+ let unit = 4
142
+ let first = true
143
+ for (let at = index + 1; at < lines.length; at += 1) {
144
+ const line = lines[at]
145
+ if (!line.trim()) continue
146
+ const indent = line.match(/^\s*/)[0].length
147
+ if (indent <= base) break
148
+ if (first) {
149
+ unit = indent - base
150
+ first = false
151
+ }
152
+ const level = Math.max(0, Math.floor((indent - base) / unit) - 1)
153
+ if (level > deepest) deepest = level
154
+ if (PY_BRANCH.test(line)) branches += 1
155
+ end = at
156
+ }
157
+ rows.push({ file: file.path, line: index + 1, name: head[2], kind: "function", lines: end - index + 1, depth: deepest, branches })
158
+ }
159
+ return rows
160
+ }
161
+
162
+ /**
163
+ * @param {{ path: string, kind: string, text: string }} file
164
+ * @returns {Array<{ file, line, name, kind, lines, depth, branches }>}
165
+ */
166
+ export function extractFunctions(file) {
167
+ if (file.kind === "qml" || file.kind === "js") return jsFunctions(file)
168
+ if (file.kind === "shell") return shellFunctions(file)
169
+ if (file.kind === "python") return pythonFunctions(file)
170
+ return []
171
+ }