@mizchi/vlmkit-core 0.11.1 → 0.23.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.
@@ -1,12 +1,6 @@
1
+ import { UsageError } from "@mizchi/vlmkit-judge/errors.ts";
2
+
1
3
  //#region src/cli-error.d.ts
2
- /**
3
- * A bad flag or missing argument — the caller's typo, not a defect. Thrown by
4
- * `arg-reader` and printed as one line, because a stack trace for
5
- * `--concurrency abc` buries the one sentence that fixes it.
6
- */
7
- declare class UsageError extends Error {
8
- readonly name = "UsageError";
9
- }
10
4
  /**
11
5
  * The `playwright` peer range. Duplicated from this package's own
12
6
  * `peerDependencies` because the shipped bundle has no reliable path back to
@@ -2,6 +2,7 @@ import { createRequire } from "node:module";
2
2
  import { existsSync, readFileSync, statSync } from "node:fs";
3
3
  import { dirname, join } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
+ import { UsageError } from "@mizchi/vlmkit-judge/errors.ts";
5
6
  //#region src/cli-error.ts
6
7
  /**
7
8
  * Shared CLI error-prettifier. Recognizes common error shapes that
@@ -19,14 +20,6 @@ import { fileURLToPath } from "node:url";
19
20
  * `tests/playwright-peer-contract.test.mjs` guards that.
20
21
  */
21
22
  /**
22
- * A bad flag or missing argument — the caller's typo, not a defect. Thrown by
23
- * `arg-reader` and printed as one line, because a stack trace for
24
- * `--concurrency abc` buries the one sentence that fixes it.
25
- */
26
- var UsageError = class extends Error {
27
- name = "UsageError";
28
- };
29
- /**
30
23
  * The `playwright` peer range. Duplicated from this package's own
31
24
  * `peerDependencies` because the shipped bundle has no reliable path back to
32
25
  * its manifest (tsdown flattens `dist/`, and the root CLI bundle inlines this
@@ -38,7 +38,18 @@ declare const isUrlSource: (source: string) => boolean;
38
38
  * the caller nothing about what actually went wrong.
39
39
  */
40
40
  declare function resolveSource(source: string): string;
41
- /** File path or URL → a URL Playwright can navigate to. */
41
+ /**
42
+ * File path or URL → a URL Playwright can navigate to.
43
+ *
44
+ * A local page is also opened in a STATE — `page.html?theme=dark`, `index.html?step=2`,
45
+ * `article.html#note-2` — and that suffix belongs to the page, not to the filename.
46
+ * `pathToFileURL` escapes `?` and `#` into the name (`index.html%3Ftheme=dark`), so every gate
47
+ * reported `error: file not found: index.html?theme=dark`; three of the six agents building demo
48
+ * sites (2026-09-23) hit it asking for a second theme or step, and worked around it with an
49
+ * absolute `file://` URL. The suffix is split off only when the whole string is not itself a
50
+ * file and the part before the `?` / `#` is, so a file that really has `?` in its name still
51
+ * opens as that file.
52
+ */
42
53
  declare function sourceToUrl(source: string): string;
43
54
  /**
44
55
  * Wait for the page to stop moving: bounded network idle, then webfonts, then
@@ -1,6 +1,6 @@
1
1
  import { withAuthState } from "./auth-state.mjs";
2
2
  import { describeRedirect } from "./navigation-redirect.mjs";
3
- import { readFileSync } from "node:fs";
3
+ import { existsSync, readFileSync } from "node:fs";
4
4
  import { resolve } from "node:path";
5
5
  import { pathToFileURL } from "node:url";
6
6
  //#region src/page-open.ts
@@ -71,9 +71,27 @@ const isUrlSource = (source) => /^(https?|file):\/\//.test(source);
71
71
  function resolveSource(source) {
72
72
  return isUrlSource(source) ? source : resolve(source);
73
73
  }
74
- /** File path or URL → a URL Playwright can navigate to. */
74
+ /**
75
+ * File path or URL → a URL Playwright can navigate to.
76
+ *
77
+ * A local page is also opened in a STATE — `page.html?theme=dark`, `index.html?step=2`,
78
+ * `article.html#note-2` — and that suffix belongs to the page, not to the filename.
79
+ * `pathToFileURL` escapes `?` and `#` into the name (`index.html%3Ftheme=dark`), so every gate
80
+ * reported `error: file not found: index.html?theme=dark`; three of the six agents building demo
81
+ * sites (2026-09-23) hit it asking for a second theme or step, and worked around it with an
82
+ * absolute `file://` URL. The suffix is split off only when the whole string is not itself a
83
+ * file and the part before the `?` / `#` is, so a file that really has `?` in its name still
84
+ * opens as that file.
85
+ */
75
86
  function sourceToUrl(source) {
76
- return isUrlSource(source) ? source : pathToFileURL(resolve(source)).href;
87
+ if (isUrlSource(source)) return source;
88
+ const whole = resolve(source);
89
+ const cut = source.search(/[?#]/);
90
+ if (cut > 0 && !existsSync(whole)) {
91
+ const file = resolve(source.slice(0, cut));
92
+ if (existsSync(file)) return pathToFileURL(file).href + source.slice(cut);
93
+ }
94
+ return pathToFileURL(whole).href;
77
95
  }
78
96
  /**
79
97
  * Wait for the page to stop moving: bounded network idle, then webfonts, then
@@ -0,0 +1,41 @@
1
+ import { FindingSeverity, RuleView } from "./contract.mjs";
2
+ import { TieredRow } from "./rule-tier.mjs";
3
+
4
+ //#region src/plugin/rule-prose.d.ts
5
+ /** The shape all seven reports' rows already have. */
6
+ interface IssueLike {
7
+ kind: string;
8
+ severity: FindingSeverity;
9
+ }
10
+ interface TieredIssues<T> {
11
+ /** Rows whose rule is still on, each carrying the severity to print it at. */
12
+ shown: TieredRow<T>[];
13
+ /**
14
+ * The gate's own `status:` word, computed from what survives.
15
+ *
16
+ * `ok` when nothing survives — including when the only reason nothing survives is that the
17
+ * rows were turned off. That is the honest word for it: the project said these do not count,
18
+ * and `note` is printed alongside so the screen still says how many were dropped.
19
+ */
20
+ status: "ok" | "info" | "warn" | "suspect";
21
+ /** `hiddenByRuleNote`'s line, or undefined when the settings dropped nothing. */
22
+ note?: string;
23
+ }
24
+ /**
25
+ * Tier a report's `issues[]` and derive the status word.
26
+ *
27
+ * Callers keep their own icons and wording — the palettes and the glyphs (`x` vs `✗`) differ
28
+ * per gate and are not worth unifying — but they must read `tier` from the returned rows rather
29
+ * than `severity` from their own, which is the point of routing through here.
30
+ */
31
+ declare function tierIssues<T extends IssueLike>(issues: readonly T[], rules?: RuleView): TieredIssues<T>;
32
+ /**
33
+ * ` [kind re-tuned to warn]`, or `""` when the row prints at the severity it was emitted at.
34
+ *
35
+ * Without it a demoted row is indistinguishable from one the gate itself graded that way, and
36
+ * the reader has no way to tell a measurement from a policy. The suffix names the rule id
37
+ * because that is the string they would edit to change it back.
38
+ */
39
+ declare function retuneNote<T extends IssueLike>(entry: TieredRow<T>): string;
40
+ //#endregion
41
+ export { IssueLike, TieredIssues, retuneNote, tierIssues };
@@ -0,0 +1,34 @@
1
+ import { applyRuleTiers, hiddenByRuleNote } from "./rule-tier.mjs";
2
+ //#region src/plugin/rule-prose.ts
3
+ /**
4
+ * Tier a report's `issues[]` and derive the status word.
5
+ *
6
+ * Callers keep their own icons and wording — the palettes and the glyphs (`x` vs `✗`) differ
7
+ * per gate and are not worth unifying — but they must read `tier` from the returned rows rather
8
+ * than `severity` from their own, which is the point of routing through here.
9
+ */
10
+ function tierIssues(issues, rules) {
11
+ const { shown, hiddenByRule } = applyRuleTiers(issues, (issue) => ({
12
+ rule: issue.kind,
13
+ emitted: issue.severity
14
+ }), rules);
15
+ const status = shown.some((s) => s.tier === "suspect") ? "suspect" : shown.some((s) => s.tier === "warn") ? "warn" : shown.length > 0 ? "info" : "ok";
16
+ const note = hiddenByRuleNote(hiddenByRule);
17
+ return {
18
+ shown,
19
+ status,
20
+ ...note ? { note } : {}
21
+ };
22
+ }
23
+ /**
24
+ * ` [kind re-tuned to warn]`, or `""` when the row prints at the severity it was emitted at.
25
+ *
26
+ * Without it a demoted row is indistinguishable from one the gate itself graded that way, and
27
+ * the reader has no way to tell a measurement from a policy. The suffix names the rule id
28
+ * because that is the string they would edit to change it back.
29
+ */
30
+ function retuneNote(entry) {
31
+ return entry.tier === entry.row.severity ? "" : ` [${entry.row.kind} re-tuned to ${entry.tier}]`;
32
+ }
33
+ //#endregion
34
+ export { retuneNote, tierIssues };
@@ -0,0 +1,50 @@
1
+ //#region src/stable-selector.d.ts
2
+ /**
3
+ * The one in-page selector generator, as a script fragment.
4
+ *
5
+ * Six gates needed "name this element so a reader can find it again", and six
6
+ * copies of this function existed. Three of them had lost the recursive call:
7
+ *
8
+ * const parent = el.parentElement;
9
+ * if (!parent) return el.tagName.toLowerCase();
10
+ * const siblings = Array.from(parent.children).filter((s) => s.tagName === el.tagName);
11
+ * return el.tagName.toLowerCase() + ":nth-of-type(" + (siblings.indexOf(el) + 1) + ")";
12
+ *
13
+ * which returns `p:nth-of-type(1)` — the first `<p>` of *every* parent on the page.
14
+ * `check animation` on a page with two animated first-children reported three
15
+ * findings on two different elements, all three carrying the identical selector
16
+ * `div:nth-of-type(1)`, which matched both. The report named nothing.
17
+ *
18
+ * The `if (!parent) return` line is what gives the accident away: without recursion a
19
+ * root element returning `html` rather than `html:nth-of-type(1)` is a distinction
20
+ * with no purpose. It is a base case, and the recursive step it guarded was dropped.
21
+ *
22
+ * ## The contract
23
+ *
24
+ * The returned selector matches **exactly one** element in the document it was
25
+ * generated from. That is the whole point — it is what makes a finding actionable,
26
+ * what lets two findings be told apart, and what `--allow "<selector>;<reason>"`
27
+ * matches against. `packages/vlmkit-markup/src/selector-uniqueness.test.ts` asserts
28
+ * it against real gate output rather than against this source, so a seventh copy
29
+ * appearing elsewhere is still caught.
30
+ *
31
+ * Preference order: `#id` → `tag.class` when unique → recursive
32
+ * `<parent-path> > tag:nth-of-type(n)`.
33
+ *
34
+ * ## Why a string
35
+ *
36
+ * These run inside `page.evaluate`, and most callers assemble their collector as a
37
+ * template literal, so the shared form has to be text. Interpolate it at the top of
38
+ * the script:
39
+ *
40
+ * ```ts
41
+ * const COLLECT = `(() => { ${STABLE_SELECTOR_JS} … })()`;
42
+ * ```
43
+ *
44
+ * `motion-detect.ts` is the exception: it passes a real typed arrow to
45
+ * `page.evaluate`, so it carries its own copy on purpose and the uniqueness test is
46
+ * what holds the two in agreement.
47
+ */
48
+ declare const STABLE_SELECTOR_JS = "\n function stableSelector(el) {\n if (!el || !el.tagName) return \"(no target)\";\n const id = el.getAttribute && el.getAttribute(\"id\");\n if (id) return \"#\" + CSS.escape(id);\n const classes = el.classList ? Array.from(el.classList).slice(0, 3) : [];\n if (classes.length > 0) {\n const selector = el.tagName.toLowerCase() + classes.map((c) => \".\" + CSS.escape(c)).join(\"\");\n if (document.querySelectorAll(selector).length === 1) return selector;\n }\n const parent = el.parentElement;\n if (!parent) return el.tagName.toLowerCase();\n const siblings = Array.from(parent.children).filter((item) => item.tagName === el.tagName);\n return stableSelector(parent) + \" > \" + el.tagName.toLowerCase() + \":nth-of-type(\" + (siblings.indexOf(el) + 1) + \")\";\n }\n";
49
+ //#endregion
50
+ export { STABLE_SELECTOR_JS };
@@ -0,0 +1,65 @@
1
+ //#region src/stable-selector.ts
2
+ /**
3
+ * The one in-page selector generator, as a script fragment.
4
+ *
5
+ * Six gates needed "name this element so a reader can find it again", and six
6
+ * copies of this function existed. Three of them had lost the recursive call:
7
+ *
8
+ * const parent = el.parentElement;
9
+ * if (!parent) return el.tagName.toLowerCase();
10
+ * const siblings = Array.from(parent.children).filter((s) => s.tagName === el.tagName);
11
+ * return el.tagName.toLowerCase() + ":nth-of-type(" + (siblings.indexOf(el) + 1) + ")";
12
+ *
13
+ * which returns `p:nth-of-type(1)` — the first `<p>` of *every* parent on the page.
14
+ * `check animation` on a page with two animated first-children reported three
15
+ * findings on two different elements, all three carrying the identical selector
16
+ * `div:nth-of-type(1)`, which matched both. The report named nothing.
17
+ *
18
+ * The `if (!parent) return` line is what gives the accident away: without recursion a
19
+ * root element returning `html` rather than `html:nth-of-type(1)` is a distinction
20
+ * with no purpose. It is a base case, and the recursive step it guarded was dropped.
21
+ *
22
+ * ## The contract
23
+ *
24
+ * The returned selector matches **exactly one** element in the document it was
25
+ * generated from. That is the whole point — it is what makes a finding actionable,
26
+ * what lets two findings be told apart, and what `--allow "<selector>;<reason>"`
27
+ * matches against. `packages/vlmkit-markup/src/selector-uniqueness.test.ts` asserts
28
+ * it against real gate output rather than against this source, so a seventh copy
29
+ * appearing elsewhere is still caught.
30
+ *
31
+ * Preference order: `#id` → `tag.class` when unique → recursive
32
+ * `<parent-path> > tag:nth-of-type(n)`.
33
+ *
34
+ * ## Why a string
35
+ *
36
+ * These run inside `page.evaluate`, and most callers assemble their collector as a
37
+ * template literal, so the shared form has to be text. Interpolate it at the top of
38
+ * the script:
39
+ *
40
+ * ```ts
41
+ * const COLLECT = `(() => { ${STABLE_SELECTOR_JS} … })()`;
42
+ * ```
43
+ *
44
+ * `motion-detect.ts` is the exception: it passes a real typed arrow to
45
+ * `page.evaluate`, so it carries its own copy on purpose and the uniqueness test is
46
+ * what holds the two in agreement.
47
+ */
48
+ const STABLE_SELECTOR_JS = `
49
+ function stableSelector(el) {
50
+ if (!el || !el.tagName) return "(no target)";
51
+ const id = el.getAttribute && el.getAttribute("id");
52
+ if (id) return "#" + CSS.escape(id);
53
+ const classes = el.classList ? Array.from(el.classList).slice(0, 3) : [];
54
+ if (classes.length > 0) {
55
+ const selector = el.tagName.toLowerCase() + classes.map((c) => "." + CSS.escape(c)).join("");
56
+ if (document.querySelectorAll(selector).length === 1) return selector;
57
+ }
58
+ const parent = el.parentElement;
59
+ if (!parent) return el.tagName.toLowerCase();
60
+ const siblings = Array.from(parent.children).filter((item) => item.tagName === el.tagName);
61
+ return stableSelector(parent) + " > " + el.tagName.toLowerCase() + ":nth-of-type(" + (siblings.indexOf(el) + 1) + ")";
62
+ }
63
+ `;
64
+ //#endregion
65
+ export { STABLE_SELECTOR_JS };
@@ -15,6 +15,6 @@
15
15
  * every `package.json` and that the CHANGELOG has a section for it, so the literal cannot drift
16
16
  * from the release either.
17
17
  */
18
- declare const VLMKIT_VERSION = "0.11.1";
18
+ declare const VLMKIT_VERSION = "0.23.0";
19
19
  //#endregion
20
20
  export { VLMKIT_VERSION };
package/dist/version.mjs CHANGED
@@ -15,6 +15,6 @@
15
15
  * every `package.json` and that the CHANGELOG has a section for it, so the literal cannot drift
16
16
  * from the release either.
17
17
  */
18
- const VLMKIT_VERSION = "0.11.1";
18
+ const VLMKIT_VERSION = "0.23.0";
19
19
  //#endregion
20
20
  export { VLMKIT_VERSION };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mizchi/vlmkit-core",
3
- "version": "0.11.1",
3
+ "version": "0.23.0",
4
4
  "description": "VRT diff engine + shared types — pure image / DOM / a11y / visual comparison primitives.",
5
5
  "license": "MIT",
6
6
  "author": "mizchi",
@@ -45,7 +45,8 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "pixelmatch": "^7.1.0",
48
- "pngjs": "^7.0.0"
48
+ "pngjs": "^7.0.0",
49
+ "@mizchi/vlmkit-judge": "0.23.0"
49
50
  },
50
51
  "peerDependencies": {
51
52
  "playwright": ">=1.61 <2",