openpencil/scripts/ab-corpus/inspect-shape.ts
Fini 5a841f011f fix(ai): contrast detector skips effectively-transparent wrapper fills
Codex stop-hook review caught: the detectTextBgContrast ancestor walk
treated any wrapper with a solid `fill` entry as the bg color, even
when the fill was effectively invisible. The classic miss case:

  page { fill: cream }
    └─ wrapper { fill: [{ type: 'solid', color: '#FFFFFF', opacity: 0 }] }
        └─ text { fill: cream }

Without the guard, the detector picked the wrapper's white fill as bg
and reported a healthy contrast ratio against the cream text — masking
the real cream-on-cream failure that lives one level up.

firstSolidColor() now skips fills with `opacity === 0` and 8-hex colors
whose alpha byte is `00` (e.g. `#FFFFFF00`). Both produce no visible
color, so the ancestor walk continues past them to the real bg.

Semi-transparent fills (opacity 0.5, 8-hex alpha 80, etc.) are out of
scope — the detector still treats them as opaque rather than trying to
math the layered composite. Tests pin both: opacity=0.5 + alpha=80
stay treated as bg.

4 new test cases cover the fix plus the boundary (opacity=0.5, alpha=80
should NOT be skipped). Full corpus replay shows 14 hits unchanged on
the 470-row corpus — no false-positive regression introduced.
2026-05-10 14:55:00 +08:00

77 lines
2.6 KiB
TypeScript

/**
* Dump the doc shape (root width / height / layout / direct-children
* roles) for every applied row in a category. Used 2026-05-10 to figure
* out why detectEdgeSectionPadding scored 0 hits on the 220-row mobile
* subset of `2026-05-03-ab-v8-v1-default` — needed to see whether the
* model is producing page-shaped roots at all or just fragments that
* don't look like a mobile page.
*/
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { parseModelOutput } from '@zseven-w/pen-ai-skills';
import { applyToFreshDoc } from './apply';
interface JsonlRow {
promptId: string;
category: string;
difficulty: string;
variant: string;
rawOutput: string;
}
async function main(): Promise<void> {
const runId = process.argv[2];
const wantCategory = process.argv[3] ?? 'mobile';
if (!runId) {
console.error('usage: bun run scripts/ab-corpus/inspect-shape.ts <run-id> [category]');
process.exit(1);
}
const path = join(import.meta.dir, 'runs', runId, 'scores.jsonl');
const rows: JsonlRow[] = readFileSync(path, 'utf-8')
.split('\n')
.filter(Boolean)
.map((l) => JSON.parse(l));
const buckets = new Map<string, number>();
for (const r of rows) {
if (r.category !== wantCategory) continue;
const parsed = parseModelOutput(r.rawOutput);
if (parsed.kind === 'garbage') continue;
const result = await applyToFreshDoc(parsed);
if (!result.ok || !result.doc) continue;
const root = result.doc.children?.[0] as
| (Record<string, unknown> & { width?: unknown; height?: unknown; children?: unknown[] })
| undefined;
if (!root) continue;
const w = root.width;
const h = root.height;
const childCount = Array.isArray(root.children) ? root.children.length : 0;
const wKey =
typeof w === 'number'
? w >= 320 && w <= 480
? 'mobile-w'
: w > 480 && w <= 800
? 'tablet-w'
: w > 800
? 'desktop-w'
: 'tiny-w'
: `non-numeric:${typeof w}`;
const hKey = typeof h === 'number' ? (h >= 568 ? 'tall' : 'short') : `non-numeric:${typeof h}`;
const ratioKey =
typeof w === 'number' && typeof h === 'number' && w > 0 && h / w >= 1.5
? 'aspect-ok'
: 'aspect-low';
const key = `${wKey} / ${hKey} / ${ratioKey} / children=${childCount}`;
buckets.set(key, (buckets.get(key) ?? 0) + 1);
}
console.log(`shapes for category=${wantCategory}:`);
const sorted = Array.from(buckets.entries()).sort((a, b) => b[1] - a[1]);
for (const [k, v] of sorted) console.log(` ${String(v).padStart(4)} ${k}`);
}
main().catch((e) => {
console.error(e);
process.exit(1);
});