@opencraw/studio 0.0.5 → 0.0.6

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.
Files changed (30) hide show
  1. package/dist/index.esm.js +888 -10
  2. package/dist/src/index.d.ts +2 -2
  3. package/dist/src/page-snapshot/hidden-marks.algorithm.d.ts +23 -0
  4. package/dist/src/page-snapshot/index.d.ts +7 -0
  5. package/dist/src/page-snapshot/rewrite-document.mapper.d.ts +27 -0
  6. package/dist/src/page-snapshot/snapshot.store.d.ts +14 -0
  7. package/dist/src/page-snapshot/take-snapshot.use-case.d.ts +34 -0
  8. package/dist/src/sample-run/run-sample.use-case.d.ts +3 -1
  9. package/dist/src/selector-inference/class-token.policy.d.ts +35 -0
  10. package/dist/src/selector-inference/dom-path.model.d.ts +73 -0
  11. package/dist/src/selector-inference/index.d.ts +12 -0
  12. package/dist/src/selector-inference/list-inference.algorithm.d.ts +43 -0
  13. package/dist/src/selector-inference/selector-candidates.algorithm.d.ts +22 -0
  14. package/dist/src/selector-inference/selector-ranking.policy.d.ts +27 -0
  15. package/dist/src/selector-inference/shadow-dom.error.d.ts +11 -0
  16. package/dist/src/selector-inference/take-kind.policy.d.ts +12 -0
  17. package/dist/src/studio-api/command.contract.d.ts +44 -0
  18. package/dist/src/studio-api/index.d.ts +8 -2
  19. package/dist/src/studio-api/infer-selector-view.contract.d.ts +68 -0
  20. package/dist/src/studio-api/snapshot-view.contract.d.ts +9 -0
  21. package/dist/src/studio-api/verify-selector-view.contract.d.ts +18 -0
  22. package/dist/src/studio-server/infer-selector.handler.d.ts +18 -0
  23. package/dist/src/studio-server/studio-http.use-case.d.ts +3 -0
  24. package/dist/src/studio-server/take-snapshot.handler.d.ts +16 -0
  25. package/dist/src/studio-server/verify-selector.handler.d.ts +18 -0
  26. package/dist/src/studio-server/workspace.store.d.ts +7 -2
  27. package/dist/ui/assets/index-BfbrsfCv.js +13 -0
  28. package/dist/ui/index.html +1 -1
  29. package/package.json +5 -3
  30. package/dist/ui/assets/index-DYb-tdmr.js +0 -12
package/dist/index.esm.js CHANGED
@@ -3,7 +3,8 @@ import { createServer } from 'node:http';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { z } from 'zod';
5
5
  import { randomBytes, createHash } from 'node:crypto';
6
- import { BrowserClient, HttpClient, readRecipeSource, parseInputRecipe, parseOutputRecipe, bindRecipeSet, memorySink, createCrawler, traceLine, RecipeBindingError, RecipeValidationError } from '@opencraw/core';
6
+ import { BrowserClient, HttpClient, NODE_ID_ATTRIBUTE, readRecipeSource, parseInputRecipe, parseOutputRecipe, bindRecipeSet, memorySink, createCrawler, traceLine, countMatches, RecipeBindingError, RecipeValidationError } from '@opencraw/core';
7
+ import { load } from 'cheerio';
7
8
  import { writeFile } from 'node:fs/promises';
8
9
  import { existsSync, createReadStream } from 'node:fs';
9
10
 
@@ -82,7 +83,101 @@ const saveOutlineCommandSchema = z.object({
82
83
  path: z.string().min(1),
83
84
  outline: outlineViewSchema
84
85
  });
85
- const studioCommandSchema = z.discriminatedUnion('type', [openWorkspaceCommandSchema, runSampleCommandSchema, stopRunCommandSchema, saveRecipeCommandSchema, fetchStartPageCommandSchema, saveOutlineCommandSchema]);
86
+ /**
87
+ * Captures the snapshot the content pane shows for a step (`page-snapshot`'s
88
+ * `take-snapshot.use-case.ts`), caching it by recipe and step path so
89
+ * switching the selected step does not always re-run the recipe.
90
+ */
91
+ const takeSnapshotCommandSchema = z.object({
92
+ type: z.literal('take-snapshot'),
93
+ recipeId: z.string().min(1),
94
+ path: z.string().min(1)
95
+ });
96
+ /**
97
+ * Verifies a candidate selector against the cached snapshot and, when
98
+ * reachable, the live page — the studio's own selection (`@opencraw/core`'s
99
+ * `countMatches`), never the browser's or a guess (issue #91).
100
+ */
101
+ const verifySelectorCommandSchema = z.object({
102
+ type: z.literal('verify-selector'),
103
+ recipeId: z.string().min(1),
104
+ path: z.string().min(1),
105
+ selector: z.string().min(1)
106
+ });
107
+ /**
108
+ * Turns one or two picked nodes (their `data-oc-node` ids, off the cached
109
+ * snapshot) into a verified selector: one id for a `Read` card's field, two
110
+ * for the safe item+field list shape `selector-inference`'s `inferList`
111
+ * builds by construction (issue #91, studio plan §3.2).
112
+ */
113
+ const pickedNodeIdSchema = z.string().min(1);
114
+ const onePickedNodeSchema = z.tuple([pickedNodeIdSchema]);
115
+ const twoPickedNodesSchema = z.tuple([pickedNodeIdSchema, pickedNodeIdSchema]);
116
+ const inferSelectorCommandSchema = z.object({
117
+ type: z.literal('infer-selector'),
118
+ recipeId: z.string().min(1),
119
+ path: z.string().min(1),
120
+ nodeIds: z.union([onePickedNodeSchema, twoPickedNodesSchema])
121
+ });
122
+ const studioCommandSchema = z.discriminatedUnion('type', [openWorkspaceCommandSchema, runSampleCommandSchema, stopRunCommandSchema, saveRecipeCommandSchema, fetchStartPageCommandSchema, saveOutlineCommandSchema, takeSnapshotCommandSchema, verifySelectorCommandSchema, inferSelectorCommandSchema]);
123
+
124
+ /** What `take-snapshot` answers with: the rewritten document ready for the content pane's sandboxed iframe (studio plan §3.1, issue #91). */
125
+ const snapshotViewSchema = z.object({
126
+ html: z.string(),
127
+ nodeCount: z.number(),
128
+ baseUrl: z.string()
129
+ });
130
+
131
+ /**
132
+ * What `verify-selector` answers with: the match count against the snapshot
133
+ * and, when the live page could be reached, against it too (issue #91's
134
+ * "flag a selector that matches the snapshot but not the live page").
135
+ * `liveChecked` is `false` when the live page could not be fetched (offline,
136
+ * blocked by `allowedHosts`, a transient error) — the card shows the
137
+ * snapshot count alone rather than a misleading "0 on the live page".
138
+ */
139
+ const verifySelectorViewSchema = z.object({
140
+ selector: z.string(),
141
+ snapshotMatches: z.number(),
142
+ liveChecked: z.boolean(),
143
+ liveMatches: z.number().optional(),
144
+ liveError: z.string().optional()
145
+ });
146
+
147
+ /** The candidate tiers `selector-inference`'s ranking works through, best first — see `selector-candidates.algorithm.ts`. */
148
+ const candidateTierSchema = z.enum(['id', 'data-attr', 'class', 'structure', 'position']);
149
+ /** One clicked node's field: the best selector found for it, verified against the snapshot. */
150
+ const fieldPickSchema = z.object({
151
+ selector: z.string(),
152
+ tier: candidateTierSchema,
153
+ take: z.string(),
154
+ matches: z.number()
155
+ });
156
+ /** A list's item shape: the best selector that matches every item, verified against the snapshot. */
157
+ const itemPickSchema = z.object({
158
+ selector: z.string(),
159
+ tier: candidateTierSchema,
160
+ matches: z.number()
161
+ });
162
+ /**
163
+ * What `infer-selector` answers with (issue #91, studio plan §3.2):
164
+ * `"field"` for one clicked node — a `Read` card's shape; `"list"` for two
165
+ * similar nodes — the safe `extract`(items)+`forEach`(field) shape, item and
166
+ * field each verified separately; `"unsupported"` when the two picks are not
167
+ * a coherent list (different branches, the same node twice, or shadow DOM),
168
+ * with a message the card can show as-is.
169
+ */
170
+ const inferSelectorViewSchema = z.discriminatedUnion('kind', [z.object({
171
+ kind: z.literal('field'),
172
+ field: fieldPickSchema
173
+ }), z.object({
174
+ kind: z.literal('list'),
175
+ item: itemPickSchema,
176
+ field: fieldPickSchema
177
+ }), z.object({
178
+ kind: z.literal('unsupported'),
179
+ reason: z.string()
180
+ })]);
86
181
 
87
182
  /** Why a sample run stopped before finishing on its own; mirrors `@opencraw/core`'s `RecipeReport.stoppedBy`. */
88
183
  const stoppedBySchema = z.enum(['sample-maxRecords', 'sample-maxPages', 'sample-maxMs']);
@@ -245,19 +340,224 @@ async function fetchApi(url) {
245
340
  const response = await client.send({
246
341
  url
247
342
  });
248
- return textOf(response.body);
343
+ return textOf$1(response.body);
249
344
  } finally {
250
345
  await client.dispose();
251
346
  }
252
347
  }
253
348
  /** Every body kind the studio can show as text today; others get a placeholder rather than a crash. */
254
- function textOf(body) {
349
+ function textOf$1(body) {
255
350
  if (body.kind === 'html') return body.html;
256
351
  if (body.kind === 'text') return body.text;
257
352
  if (body.kind === 'json') return JSON.stringify(body.data, null, 2);
258
353
  return `[${body.kind} document: the content pane shows HTML and JSON responses for now]`;
259
354
  }
260
355
 
356
+ /** The attribute `hiddenMarksScript` stamps on an element hidden on the live page. */
357
+ const HIDDEN_ATTRIBUTE = 'data-oc-hidden';
358
+ /**
359
+ * The in-page script `take-snapshot.use-case.ts` runs with `page.evaluate`
360
+ * *before* `page.content()`, so a `display: none`/`visibility: hidden`/
361
+ * zero-size element carries `data-oc-hidden="1"` into the captured markup
362
+ * (studio plan §3.1, issue #91) — the only way to know an element was
363
+ * hidden on the live page, since a static parse of the HTML afterwards has
364
+ * no computed style to ask.
365
+ *
366
+ * Kept as a plain string, not a TypeScript function passed to
367
+ * `page.evaluate`, because it references `window`/`document` as globals: `AGENTS.md`'s
368
+ * "things that bit before" calls out that in-page functions must not do
369
+ * that (the repo's `isolated-functions` lint rule), and every existing
370
+ * `page.evaluate` call with real DOM logic in this codebase (see
371
+ * `packages/core/src/captcha/resolve-captcha.use-case.ts`) already uses
372
+ * this same string-template shape.
373
+ *
374
+ * @param attribute - The attribute to stamp; defaults to `HIDDEN_ATTRIBUTE` (parameterised for tests and for a future caller that wants a different name).
375
+ * @returns A self-invoking script, ready for `page.evaluate(hiddenMarksScript())`.
376
+ */
377
+ function hiddenMarksScript(attribute = HIDDEN_ATTRIBUTE) {
378
+ return `(() => {
379
+ var attribute = ${JSON.stringify(attribute)}
380
+ var elements = document.querySelectorAll('*')
381
+ for (var i = 0; i < elements.length; i++) {
382
+ var element = elements[i]
383
+ var style = window.getComputedStyle(element)
384
+ var rect = element.getBoundingClientRect()
385
+ var hidden = style.display === 'none' || style.visibility === 'hidden' || (rect.width === 0 && rect.height === 0)
386
+ if (hidden) element.setAttribute(attribute, '1')
387
+ }
388
+ })()`;
389
+ }
390
+
391
+ /** Attributes that carry a URL and are rewritten to absolute; `srcset` is handled separately (it carries several). */
392
+ const URL_ATTRIBUTES = ['href', 'src', 'action', 'poster'];
393
+ /** Schemes left untouched: they are not a location `new URL(value, base)` should resolve, or already carry their own meaning. */
394
+ const SKIP_SCHEME = /^(?:javascript|mailto|tel|data|about):/i;
395
+ /**
396
+ * Rewrites captured HTML into what the content pane's sandboxed iframe shows
397
+ * (studio plan §3.1, issue #91): every URL made absolute against `baseUrl`,
398
+ * a `<base>` injected, every `<script>` removed, forms disabled (`onsubmit`
399
+ * stripped, fields made read-only), `<meta http-equiv="refresh">` stripped
400
+ * (it would otherwise navigate the sandboxed frame on its own), and every
401
+ * element given a stable `data-oc-node` id, in document order, for the
402
+ * picker overlay and `selector-inference` to address specific nodes by.
403
+ *
404
+ * Purely a markup transform: it does not know whether an element is hidden
405
+ * on the live page (`display: none`, `visibility: hidden`, zero size) — in
406
+ * web mode that is `hidden-marks.algorithm.ts`'s job, run against the live
407
+ * page *before* this function ever sees the markup, so a `data-oc-hidden`
408
+ * attribute it wrote just survives serialisation untouched here.
409
+ *
410
+ * @param html - The captured document (`page.content()` in web mode, the fetched body in api mode).
411
+ * @param baseUrl - The page's own URL, for resolving relative links and as the `<base>`.
412
+ * @returns The rewritten document and how many elements it stamped.
413
+ */
414
+ function rewriteDocument(html, baseUrl) {
415
+ const $ = load(html);
416
+ if ($('head').length === 0) $('html').prepend('<head></head>');
417
+ const head = $('head').first();
418
+ head.find('base').remove();
419
+ head.prepend(`<base href="${escapeAttribute(baseUrl)}">`);
420
+ $('meta').each((_index, element) => {
421
+ if (($(element).attr('http-equiv') ?? '').toLowerCase() === 'refresh') $(element).remove();
422
+ });
423
+ $('script').remove();
424
+ $('form').removeAttr('onsubmit');
425
+ $('input, textarea').attr('readonly', 'readonly');
426
+ $('select, button, input[type="submit"], input[type="button"]').attr('disabled', 'disabled');
427
+ $('*').each((_index, element) => {
428
+ const node = $(element);
429
+ for (const attribute of URL_ATTRIBUTES) {
430
+ const value = node.attr(attribute);
431
+ if (value !== undefined) node.attr(attribute, resolveUrl(value, baseUrl) ?? value);
432
+ }
433
+ const srcset = node.attr('srcset');
434
+ if (srcset !== undefined) node.attr('srcset', rewriteSrcset(srcset, baseUrl));
435
+ });
436
+ let nodeCount = 0;
437
+ $('*').each((_index, element) => {
438
+ $(element).attr(NODE_ID_ATTRIBUTE, `n${nodeCount}`);
439
+ nodeCount += 1;
440
+ });
441
+ return {
442
+ html: $.html(),
443
+ nodeCount
444
+ };
445
+ }
446
+ /** Resolves one URL against `baseUrl`; a scheme that is not a location (`javascript:`, `data:`…), or a value `new URL` cannot parse, is left as it was. */
447
+ function resolveUrl(value, baseUrl) {
448
+ const trimmed = value.trim();
449
+ if (trimmed === '' || SKIP_SCHEME.test(trimmed)) return undefined;
450
+ try {
451
+ return new URL(trimmed, baseUrl).href;
452
+ } catch {
453
+ return undefined;
454
+ }
455
+ }
456
+ /** `srcset`'s own micro-syntax: comma-separated `url descriptor?` pairs. Malformed entries are passed through unresolved rather than dropped. */
457
+ function rewriteSrcset(value, baseUrl) {
458
+ return value.split(',').map(candidate => {
459
+ const trimmed = candidate.trim();
460
+ const spaceAt = trimmed.indexOf(' ');
461
+ const url = spaceAt === -1 ? trimmed : trimmed.slice(0, spaceAt);
462
+ const descriptor = spaceAt === -1 ? '' : trimmed.slice(spaceAt);
463
+ return `${resolveUrl(url, baseUrl) ?? url}${descriptor}`;
464
+ }).join(', ');
465
+ }
466
+ function escapeAttribute(value) {
467
+ return value.replaceAll('&', '&amp;').replaceAll('"', '&quot;');
468
+ }
469
+
470
+ /**
471
+ * Captures the snapshot the content pane shows (studio plan §3.1, issue
472
+ * #91): runs the recipe up to `stepPath`, serialises the document, marks
473
+ * hidden elements in web mode, and hands it to `rewrite-document.mapper.ts`
474
+ * for display.
475
+ *
476
+ * `stepPath` is accepted (and cached against, by `snapshot.store.ts`) for
477
+ * the API the UI needs — one snapshot per selected step — but v1 only
478
+ * actually *captures* `"start"`: the first `start` point, before any step
479
+ * of the recipe has run, the same reach `fetch-start-page.use-case.ts` (phase
480
+ * 0) already had. Snapshotting mid-recipe (after an arbitrary later step)
481
+ * needs a "stop the crawl after step X and hand back the live page/document"
482
+ * hook in `crawl-execution` that does not exist yet; building one was out of
483
+ * this phase's budget (see this phase's final report for the tradeoff this
484
+ * was weighed against). Any other `stepPath` throws, clearly, rather than
485
+ * silently returning the start page under a misleading label.
486
+ *
487
+ * @param input - The input recipe; only `mode` and the first `start` point are used.
488
+ * @param stepPath - The step to snapshot after, e.g. `"start"` or `"steps.2"` (`scope-outline`'s path format).
489
+ * @param browser - Browser launch settings for web mode.
490
+ * @returns The rewritten snapshot.
491
+ * @throws Error when the recipe has no start point, or `stepPath` is not `"start"`.
492
+ */
493
+ async function takeSnapshot(input, stepPath, browser) {
494
+ if (stepPath !== 'start') throw new Error(`takeSnapshot: "${stepPath}" is not supported yet — only "start" (the first start point, before any step runs) is; see this function's doc comment`);
495
+ const point = input.start[0];
496
+ if (point === undefined) throw new Error(`recipe "${input.id}" has no start point`);
497
+ return input.mode === 'web' ? snapshotWeb(point.url, browser) : snapshotApi(point.url);
498
+ }
499
+ async function snapshotWeb(url, browser) {
500
+ const client = await BrowserClient.launch(browser);
501
+ try {
502
+ const session = await client.newSession();
503
+ try {
504
+ await session.page.goto(url);
505
+ await session.page.evaluate(hiddenMarksScript(HIDDEN_ATTRIBUTE));
506
+ const html = await session.page.content();
507
+ const baseUrl = session.page.url();
508
+ const rewritten = rewriteDocument(html, baseUrl);
509
+ return {
510
+ ...rewritten,
511
+ baseUrl
512
+ };
513
+ } finally {
514
+ await session.close();
515
+ }
516
+ } finally {
517
+ await client.close();
518
+ }
519
+ }
520
+ async function snapshotApi(url) {
521
+ const client = await HttpClient.open({});
522
+ try {
523
+ const response = await client.send({
524
+ url
525
+ });
526
+ const rewritten = rewriteDocument(textOf(response.body), url);
527
+ return {
528
+ ...rewritten,
529
+ baseUrl: url
530
+ };
531
+ } finally {
532
+ await client.dispose();
533
+ }
534
+ }
535
+ /** Every body kind the studio can show as text today; others get a placeholder rather than a crash (mirrors `fetch-start-page.use-case.ts`'s own `textOf`). */
536
+ function textOf(body) {
537
+ if (body.kind === 'html') return body.html;
538
+ if (body.kind === 'text') return body.text;
539
+ if (body.kind === 'json') return `<pre>${JSON.stringify(body.data, null, 2)}</pre>`;
540
+ return `<p>[${body.kind} document: the content pane shows HTML and JSON responses for now]</p>`;
541
+ }
542
+
543
+ /** @returns An empty cache, for a freshly opened workspace. */
544
+ function createSnapshotCache() {
545
+ return {
546
+ entries: new Map()
547
+ };
548
+ }
549
+ /** @returns The cached snapshot for `recipeId`/`path`, or `undefined` when none was taken yet. */
550
+ function cachedSnapshot(cache, recipeId, path) {
551
+ return cache.entries.get(keyOf(recipeId, path));
552
+ }
553
+ /** Records a freshly taken snapshot, replacing whatever was cached for the same recipe and step path. */
554
+ function putSnapshot(cache, recipeId, path, snapshot) {
555
+ cache.entries.set(keyOf(recipeId, path), snapshot);
556
+ }
557
+ function keyOf(recipeId, path) {
558
+ return `${recipeId}:${path}`;
559
+ }
560
+
261
561
  /**
262
562
  * Finds and parses one input recipe of a workspace folder, and the output
263
563
  * recipe it names.
@@ -299,10 +599,11 @@ function idOf(content) {
299
599
  * @param recipeId - The input recipe to run.
300
600
  * @param budget - The sample budget; unset runs the recipe to completion (still capped by its own `limits`).
301
601
  * @param callbacks - Where trace lines and records go as the run proceeds.
602
+ * @param browser - Browser launch settings for a web-mode recipe (an executable path override, headless…); the server-wide setting `studio-http.use-case.ts`'s `StudioServerOptions.browser` carries in, e.g. `OPENCRAW_CHROMIUM` in a sandbox with no full Playwright install.
302
603
  * @returns A handle: `stop` closes the crawler cleanly, `result` resolves when the run ends.
303
604
  * @throws RecipeValidationError, RecipeBindingError, Error when the recipe or its output cannot be loaded or bound.
304
605
  */
305
- async function runSample(folder, recipeId, budget, callbacks) {
606
+ async function runSample(folder, recipeId, budget, callbacks, browser) {
306
607
  const {
307
608
  input,
308
609
  output
@@ -313,6 +614,7 @@ async function runSample(folder, recipeId, budget, callbacks) {
313
614
  sink,
314
615
  debug: true,
315
616
  sample: budget,
617
+ browser,
316
618
  onEvent: event => {
317
619
  const line = traceLine(event);
318
620
  if (line !== undefined) callbacks.onTraceLine(line);
@@ -365,12 +667,504 @@ async function handleFetchStartPage(state, command) {
365
667
  const {
366
668
  input
367
669
  } = await loadRecipePair(state.folder, command.recipeId);
368
- const html = await fetchStartPage(input);
670
+ const html = await fetchStartPage(input, state.browser);
369
671
  return {
370
672
  html
371
673
  };
372
674
  }
373
675
 
676
+ const GENERATED_ID = /^(?:react-|radix-|:r|ember|css-|mui-|headlessui-)/i;
677
+ /**
678
+ * The path from the document root to `node`, one level per ancestor. Two
679
+ * elements that share an ancestor produce identical levels up to that
680
+ * ancestor and diverge exactly where their parents stop being the same node
681
+ * — `list-inference.algorithm.ts` leans on that to find the nearest pair of
682
+ * siblings two picks belong to, without ever touching cheerio again.
683
+ *
684
+ * @param $ - The parsed document.
685
+ * @param node - The element to describe.
686
+ * @returns The path, root first.
687
+ */
688
+ function domPathOf($, node) {
689
+ const chain = [];
690
+ let current = node;
691
+ while (current !== undefined && current.length > 0 && isElement(current.get(0))) {
692
+ chain.unshift(current);
693
+ const parent = current.parent();
694
+ current = parent.length > 0 ? parent : undefined;
695
+ }
696
+ return chain.map(element => levelOf($, element));
697
+ }
698
+ function levelOf($, element) {
699
+ const node = element.get(0);
700
+ const tag = node.tagName.toLowerCase();
701
+ const attrs = {};
702
+ const entries = Object.entries(node.attribs ?? {});
703
+ for (const [name, value] of entries) attrs[name] = value;
704
+ const classes = (attrs.class ?? '').split(/\s+/).filter(token => token.length > 0);
705
+ const siblings = element.parent().length > 0 ? element.parent().children(tag) : $.root().children(tag);
706
+ const index = siblings.toArray().indexOf(node) + 1;
707
+ return {
708
+ tag,
709
+ id: attrs.id,
710
+ classes,
711
+ attrs,
712
+ index: Math.max(index, 1),
713
+ siblingCount: Math.max(siblings.length, 1)
714
+ };
715
+ }
716
+ function isElement(node) {
717
+ return node !== undefined && node.type === 'tag';
718
+ }
719
+ /**
720
+ * Whether `id` looks handed by a framework (`react-`, a CSS-module or a
721
+ * `radix-`/`mui-` autogenerated one) rather than by the site's own markup —
722
+ * `class-token.policy.ts` has the equivalent check for class tokens, kept
723
+ * separate because ids and classes are ranked at different tiers.
724
+ */
725
+ function isStableId(id) {
726
+ return id.length > 0 && !GENERATED_ID.test(id);
727
+ }
728
+ /**
729
+ * Whether `path` failed to reach the real document root. `domPathOf` walks
730
+ * up through cheerio's own `.parent()`, which (matching real DOM semantics)
731
+ * never crosses from a `<template>`'s content into the light DOM outside
732
+ * it — a `<template shadowrootmode>` is exactly declarative shadow DOM, the
733
+ * only shadow-DOM shape that can show up in static, server-captured markup
734
+ * at all (imperative `attachShadow()` content never serialises into
735
+ * `page.content()`/a fetched response in the first place, so that case
736
+ * cannot reach this slice and needs no separate detection). A path whose
737
+ * first level is not `html` was cut short at exactly this kind of boundary:
738
+ * v1 does not pierce it, so callers report a clear "shadow DOM is not
739
+ * supported" error instead of building ancestor-based candidates over a
740
+ * chain that stops partway up.
741
+ */
742
+ function crossesShadowRoot(path) {
743
+ return path.length === 0 || path[0].tag !== 'html';
744
+ }
745
+ /** Parses `html` and returns cheerio's API, for callers that need more than one path from the same document. */
746
+ function parseDocument$1(html) {
747
+ return load(html);
748
+ }
749
+ /**
750
+ * The path to the element the snapshot stamped with `nodeId` (its
751
+ * `data-oc-node` attribute — `@opencraw/core`'s `NODE_ID_ATTRIBUTE`,
752
+ * `packages/studio/src/page-snapshot/rewrite-document.mapper.ts`). This is
753
+ * how a click in the content pane's iframe — which only ever needs to read
754
+ * that one attribute off the element the user clicked — turns into
755
+ * something `selector-inference`'s other algorithms can work with, without
756
+ * the browser-side picker script needing to know anything about ancestry,
757
+ * classes or candidate ranking itself.
758
+ *
759
+ * @param html - The snapshot document (already stamped with node ids).
760
+ * @param nodeId - The `data-oc-node` value to find.
761
+ * @returns The node's path, or `undefined` when no element carries that id.
762
+ */
763
+ function pathToNode(html, nodeId) {
764
+ const $ = parseDocument$1(html);
765
+ const found = $(`[${NODE_ID_ATTRIBUTE}="${cssAttributeValue(nodeId)}"]`);
766
+ return found.length === 0 ? undefined : domPathOf($, found.first());
767
+ }
768
+ function cssAttributeValue(value) {
769
+ return value.replaceAll('\\', String.raw`\\`).replaceAll('"', String.raw`\"`);
770
+ }
771
+
772
+ /** Attributes worth reading instead of text, in preference order, when the clicked element has one. */
773
+ const PREFERRED_ATTRS = {
774
+ a: ['href'],
775
+ img: ['src', 'alt']
776
+ };
777
+ /**
778
+ * What a `Read` card's `take` should be for the element a pick landed on
779
+ * (issue #91, studio plan §3.2): `attr:href` for an `<a>`, `attr:src` for an
780
+ * `<img>`, `attr:<name>` for the first `data-*` attribute otherwise, `json`
781
+ * for a `<script type="application/json">`, else the default, `text`.
782
+ *
783
+ * @param level - The clicked element's own level (the last entry of its `DomPath`).
784
+ * @returns The `take` value to put on the `extract` step.
785
+ */
786
+ function takeKindFor(level) {
787
+ if (level.tag === 'script' && (level.attrs.type ?? '').toLowerCase() === 'application/json') return 'json';
788
+ const preferred = PREFERRED_ATTRS[level.tag];
789
+ if (preferred !== undefined) {
790
+ const attribute = preferred.find(name => level.attrs[name] !== undefined);
791
+ if (attribute !== undefined) return `attr:${attribute}`;
792
+ }
793
+ const dataAttribute = Object.keys(level.attrs).find(name => name.startsWith('data-') && name !== 'data-oc-node' && name !== 'data-oc-hidden');
794
+ if (dataAttribute !== undefined) return `attr:${dataAttribute}`;
795
+ return 'text';
796
+ }
797
+
798
+ /**
799
+ * Which class tokens are worth building a selector on. A hand-authored class
800
+ * (`product_pod`, `price_color`) survives a rebuild; a generated one
801
+ * (a content hash, a CSS-module suffix, a styled-components token) does not,
802
+ * so a candidate built on it looks fine on the snapshot it was picked from
803
+ * and breaks on the very next deploy. This is the "generated/hashed class
804
+ * names" tradeoff issue #91 calls out explicitly: the heuristics below are a
805
+ * reasoned judgement call, not a guarantee — see the doc comments on each
806
+ * pattern for what real-world tool it targets.
807
+ */
808
+ const GENERATED_TOKEN_PATTERNS = [/** styled-components / emotion: `sc-bdVaJa`, `css-1x2y3z4` */
809
+ /^(?:sc|css)-[a-z0-9]+$/i, /** CSS Modules, most bundlers' default hash suffix: `wrapper__3fA9k`, `Button-module_root__aB3dK` */
810
+ /__[a-z0-9]{4,10}$/i, /** JSS / MUI: `jss123`, `MuiButton-root-145` */
811
+ /^(?:jss|mui[a-z]*-)[a-z0-9-]*\d{2}$/i, /** A token that is only digits: not a class a human would type. */
812
+ /^\d+$/, /** A bare hex hash: `a3f9c21`, `1a2b3c4d` (6+ hex chars, nothing else). */
813
+ /^[0-9a-f]{6,}$/i, /** Tailwind/atomic-CSS-in-JS runtime classes some bundlers emit: `_1a2b3c` */
814
+ /^_[0-9a-z]{5,}$/i];
815
+ /**
816
+ * Whether a class token looks hand-authored and safe to build a selector on.
817
+ *
818
+ * @param token - One class, no surrounding whitespace.
819
+ * @returns `false` for anything matching a known generated-class shape.
820
+ */
821
+ function isStableClassToken(token) {
822
+ return token.length > 0 && GENERATED_TOKEN_PATTERNS.every(pattern => !pattern.test(token));
823
+ }
824
+ /**
825
+ * The subset of `tokens` worth ranking a selector candidate on, order kept.
826
+ *
827
+ * @param tokens - An element's class list.
828
+ * @returns Only the stable tokens.
829
+ */
830
+ function stableClasses(tokens) {
831
+ return tokens.filter(token => isStableClassToken(token));
832
+ }
833
+ /**
834
+ * Whether two class lists describe "the same kind of element" for list
835
+ * inference: same stable classes, generated tokens (which often differ
836
+ * per-instance, e.g. a React `key`-derived class) ignored on both sides.
837
+ *
838
+ * @param a - One element's classes.
839
+ * @param b - Another element's classes.
840
+ * @returns Whether their stable class sets are equal, order ignored.
841
+ */
842
+ function sameClassSignature(a, b) {
843
+ const left = new Set(stableClasses(a));
844
+ const right = new Set(stableClasses(b));
845
+ if (left.size !== right.size) return false;
846
+ return [...left].every(token => right.has(token));
847
+ }
848
+
849
+ /**
850
+ * Thrown when a pick or a list-inference walk crosses a declarative shadow
851
+ * root (`<template shadowrootmode>`). v1 does not pierce shadow DOM: rather
852
+ * than infer a selector that silently matches the wrong thing (or nothing,
853
+ * once the page hydrates), the studio surfaces this and stops.
854
+ */
855
+ class ShadowDomUnsupportedError extends Error {
856
+ path;
857
+ constructor(path) {
858
+ super(`"${path}" is inside a shadow root: shadow DOM is not supported for picking (v1)`);
859
+ this.path = path;
860
+ this.name = 'ShadowDomUnsupportedError';
861
+ }
862
+ }
863
+
864
+ /** Attributes ranked just under `id`: named explicitly, not "any `data-*`", because a generated `data-reactid` is no more stable than a generated class. */
865
+ const STABLE_ATTRS = ['data-testid', 'data-id', 'itemprop'];
866
+ const TIER_ORDER$1 = {
867
+ 'id': 0,
868
+ 'data-attr': 1,
869
+ 'class': 2,
870
+ 'structure': 3,
871
+ 'position': 4
872
+ };
873
+ /**
874
+ * Every selector candidate worth trying for the last node of `path`, ranked
875
+ * best-first: `id` > stable `data-*`/`itemprop` attributes > stable class
876
+ * tokens > ancestor structure (`parent > tag`) > position (`nth-of-type`) as
877
+ * a last resort (issue #91's ranking, studio plan §3.2). Structure and
878
+ * position candidates only look as far up as `path` itself goes — pass a
879
+ * sub-path (e.g. from an item's root down) to keep a field's selector
880
+ * relative to its item instead of absolute to the document.
881
+ *
882
+ * @param path - The node's ancestry, root (or scope root) first, itself last.
883
+ * @param includePosition - `false` for an item selector: a position candidate would only ever match one of the items, which is never what an item selector should do.
884
+ * @returns Candidates, best tier first; ties keep this function's own order.
885
+ */
886
+ function candidatesFor(path, includePosition = true) {
887
+ const node = path.at(-1);
888
+ if (node === undefined) return [];
889
+ const candidates = [];
890
+ if (node.id !== undefined && isStableId(node.id)) candidates.push({
891
+ selector: `#${cssEscape(node.id)}`,
892
+ tier: 'id'
893
+ });
894
+ for (const name of STABLE_ATTRS) {
895
+ const value = node.attrs[name];
896
+ if (value !== undefined && value.length > 0) candidates.push({
897
+ selector: `${node.tag}[${name}="${cssValue(value)}"]`,
898
+ tier: 'data-attr'
899
+ });
900
+ }
901
+ const classes = stableClasses(node.classes);
902
+ if (classes.length > 0) candidates.push({
903
+ selector: `${node.tag}.${classes.map(token => cssEscape(token)).join('.')}`,
904
+ tier: 'class'
905
+ });
906
+ candidates.push(...structureCandidates(path));
907
+ if (includePosition) candidates.push(...positionCandidates(path));
908
+ return sortByTier(candidates);
909
+ }
910
+ /** `ancestor > ... > tag`, growing from 2 to 4 levels (or to the top of `path`), each ancestor named by its own best simple form. */
911
+ function structureCandidates(path) {
912
+ const node = path.at(-1);
913
+ if (node === undefined) return [];
914
+ const candidates = [];
915
+ const maxDepth = Math.min(path.length - 1, 3);
916
+ for (let depth = 1; depth <= maxDepth; depth++) {
917
+ const ancestor = path.at(-1 - depth);
918
+ if (ancestor === undefined) break;
919
+ const between = Array.from({
920
+ length: depth - 1
921
+ }, (_, index) => path.at(-depth + index)?.tag ?? '');
922
+ const parts = [simpleSelector(ancestor), ...between, node.tag];
923
+ candidates.push({
924
+ selector: parts.join(' > '),
925
+ tier: 'structure'
926
+ });
927
+ }
928
+ return candidates;
929
+ }
930
+ /** `parent > tag:nth-of-type(n)`, the position-only fallback; never `nth-child`, which counts every sibling tag, not just this element's own. */
931
+ function positionCandidates(path) {
932
+ const node = path.at(-1);
933
+ if (node === undefined) return [];
934
+ const own = node.siblingCount > 1 ? `${node.tag}:nth-of-type(${node.index})` : node.tag;
935
+ const parent = path.at(-2);
936
+ if (parent === undefined) return [{
937
+ selector: own,
938
+ tier: 'position'
939
+ }];
940
+ return [{
941
+ selector: `${simpleSelector(parent)} > ${own}`,
942
+ tier: 'position'
943
+ }];
944
+ }
945
+ /** A level's own selector fragment, no combinators: id, else stable classes, else the bare tag. */
946
+ function simpleSelector(level) {
947
+ if (level.id !== undefined && isStableId(level.id)) return `#${cssEscape(level.id)}`;
948
+ const classes = stableClasses(level.classes);
949
+ return classes.length > 0 ? `${level.tag}.${classes.map(token => cssEscape(token)).join('.')}` : level.tag;
950
+ }
951
+ function sortByTier(candidates) {
952
+ return [...candidates].sort((a, b) => TIER_ORDER$1[a.tier] - TIER_ORDER$1[b.tier]);
953
+ }
954
+ /** Escapes a CSS identifier segment (an id or a class token) well enough for the shapes real sites use; not a full CSS.escape polyfill. */
955
+ function cssEscape(token) {
956
+ return token.replaceAll(/([^\w-])/gi, String.raw`\$1`);
957
+ }
958
+ /** Escapes a value inside `[name="…"]`. */
959
+ function cssValue(value) {
960
+ return value.replaceAll(/(["\\])/g, String.raw`\$1`);
961
+ }
962
+
963
+ const TIER_ORDER = {
964
+ 'id': 0,
965
+ 'data-attr': 1,
966
+ 'class': 2,
967
+ 'structure': 3,
968
+ 'position': 4
969
+ };
970
+ /**
971
+ * Verifies and orders selector candidates against real markup, by running
972
+ * each one through the engine's own CSS matching (`@opencraw/core`'s
973
+ * `countMatches`/`matchesOf`) rather than reimplementing it — issue #91's
974
+ * "every candidate is verified" requirement.
975
+ *
976
+ * Ordering: candidates that do not match `targetNodeId` (when given) sort
977
+ * last; among the rest, best tier first, and within a tier the fewest
978
+ * matches first (the most specific selector that still hits the target).
979
+ * A candidate matching nothing is dropped entirely: it is not a real option.
980
+ *
981
+ * @param html - The document to verify against (the snapshot, or the live page for the second, cross-check pass).
982
+ * @param candidates - Unranked candidates, e.g. from `candidatesFor`.
983
+ * @param targetNodeId - The `data-oc-node` id the pick landed on, when the document carries those stamps (the snapshot does; a live page fetched separately does not).
984
+ * @returns Every candidate that matched at least one element, ranked best first.
985
+ */
986
+ function rankCandidates(html, candidates, targetNodeId) {
987
+ const ranked = candidates.map(candidate => {
988
+ const matches = countMatches(html, candidate.selector);
989
+ const hitsTarget = targetNodeId === undefined ;
990
+ return {
991
+ ...candidate,
992
+ matches,
993
+ hitsTarget
994
+ };
995
+ });
996
+ return ranked.filter(candidate => candidate.matches > 0).sort((a, b) => Number(b.hitsTarget) - Number(a.hitsTarget) || TIER_ORDER[a.tier] - TIER_ORDER[b.tier] || a.matches - b.matches);
997
+ }
998
+ /** The single best candidate, or `undefined` when none of them matched anything. */
999
+ function bestCandidate(html, candidates, targetNodeId) {
1000
+ return rankCandidates(html, candidates, targetNodeId)[0];
1001
+ }
1002
+
1003
+ /**
1004
+ * Infers "these two picks are the same field on two items of a list" from
1005
+ * two node paths, by walking up from both to the nearest ancestors that are
1006
+ * real siblings (the same parent) with the same tag and stable-class
1007
+ * signature — studio plan §3.2, issue #91. That ancestor is the item; the
1008
+ * remainder of each path is the field, relative to it.
1009
+ *
1010
+ * Because a `DomPath` is built root-down (`dom-path.model.ts`), two paths
1011
+ * from the same document are byte-for-byte identical up to their real
1012
+ * common ancestor and diverge exactly at its differing children — so the
1013
+ * *first* level where they differ is always the nearest-sibling test point;
1014
+ * there is no need to look further down. This also makes the "wrapper trap"
1015
+ * (an extra single-child element around each item) resolve itself: a
1016
+ * wrapper's own instances are never one another's siblings, only the
1017
+ * genuine repeating element is, so the walk naturally lands there instead of
1018
+ * on the wrapper (see the wrapper-trap fixture in this slice's tests).
1019
+ *
1020
+ * @param pathA - The first click's path.
1021
+ * @param pathB - The second click's path.
1022
+ * @returns The item and field shapes (generalised) plus each pick's raw path, or `null` when the two picks are not on a coherent list (different branches entirely, the same node twice, or one path nested inside the other).
1023
+ * @throws ShadowDomUnsupportedError when either path crosses a declarative shadow root.
1024
+ */
1025
+ function inferList(pathA, pathB) {
1026
+ if (crossesShadowRoot(pathA)) throw new ShadowDomUnsupportedError(pathToString(pathA));
1027
+ if (crossesShadowRoot(pathB)) throw new ShadowDomUnsupportedError(pathToString(pathB));
1028
+ let divergedAt = -1;
1029
+ const limit = Math.min(pathA.length, pathB.length);
1030
+ for (let index = 0; index < limit; index++) {
1031
+ if (!sameLevel(pathA[index], pathB[index])) {
1032
+ divergedAt = index;
1033
+ break;
1034
+ }
1035
+ }
1036
+ // Identical paths, or one a strict prefix of the other (the same node picked twice, or one element nested inside the other): not a list.
1037
+ if (divergedAt <= 0) return null;
1038
+ const branchA = pathA[divergedAt];
1039
+ const branchB = pathB[divergedAt];
1040
+ if (branchA.tag !== branchB.tag || !sameClassSignature(branchA.classes, branchB.classes)) return null;
1041
+ if (branchA.index === branchB.index) return null; // Same position: not two different items.
1042
+ const ancestors = pathA.slice(0, divergedAt);
1043
+ const fieldPathA = pathA.slice(divergedAt);
1044
+ const fieldPathB = pathB.slice(divergedAt);
1045
+ const fieldPath = mergeRelativePaths(fieldPathA, fieldPathB);
1046
+ return {
1047
+ itemPath: [...ancestors, fieldPath[0]],
1048
+ fieldPath,
1049
+ fieldPathA,
1050
+ fieldPathB
1051
+ };
1052
+ }
1053
+ function sameLevel(a, b) {
1054
+ return a.tag === b.tag && a.id === b.id && a.index === b.index && a.siblingCount === b.siblingCount && sameClassSignature(a.classes, b.classes);
1055
+ }
1056
+ /**
1057
+ * Zips two item-relative paths (item first) level by level into one that
1058
+ * only keeps what both share: the tag (must already match, checked by the
1059
+ * caller for level 0), the intersection of stable classes, and attributes
1060
+ * present with the same value on both sides. Shorter than the other when
1061
+ * the two picks' local structure differs in depth (a real possibility once
1062
+ * a field is missing on one item; `list-inference.algorithm.test.ts`'s
1063
+ * shifted-field fixture is exactly this) — the merge just stops there,
1064
+ * which is fine: a field selector only needs to reach as deep as the
1065
+ * shallower of the two picks.
1066
+ */
1067
+ function mergeRelativePaths(a, b) {
1068
+ const length = Math.min(a.length, b.length);
1069
+ const merged = [];
1070
+ for (let index = 0; index < length; index++) {
1071
+ const left = a[index];
1072
+ const right = b[index];
1073
+ if (left.tag !== right.tag) break;
1074
+ merged.push(mergeLevel(left, right));
1075
+ }
1076
+ return merged.length > 0 ? merged : [a[0]];
1077
+ }
1078
+ function mergeLevel(a, b) {
1079
+ const classes = stableClasses(a.classes).filter(token => b.classes.includes(token));
1080
+ const attrs = {};
1081
+ for (const [name, value] of Object.entries(a.attrs)) {
1082
+ if (name !== 'class' && name !== 'id' && b.attrs[name] === value) attrs[name] = value;
1083
+ }
1084
+ return {
1085
+ tag: a.tag,
1086
+ id: a.id !== undefined && a.id === b.id ? a.id : undefined,
1087
+ classes,
1088
+ attrs,
1089
+ index: 1,
1090
+ siblingCount: Math.max(a.siblingCount, b.siblingCount, 1)
1091
+ };
1092
+ }
1093
+ function pathToString(path) {
1094
+ return path.map(level => level.tag + (level.id === undefined ? '' : `#${level.id}`)).join(' > ');
1095
+ }
1096
+
1097
+ /**
1098
+ * Handles `infer-selector`: turns one or two picked nodes (their
1099
+ * `data-oc-node` ids, off the cached snapshot) into a verified selector —
1100
+ * one id for a `Read` card's field, two for the safe item+field list shape
1101
+ * `selector-inference`'s `inferList` builds by construction (issue #91,
1102
+ * studio plan §3.2). All the actual candidate/ranking/list-inference work
1103
+ * is `selector-inference`'s (pure); this handler only locates the clicked
1104
+ * nodes in the cached document and calls into it.
1105
+ *
1106
+ * @param state - The server state.
1107
+ * @param command - The command; `command.path` must already have a cached snapshot (call `take-snapshot` first).
1108
+ * @returns A field pick, a list pick, or `"unsupported"` with why.
1109
+ * @throws Error when no workspace is open, or `command.path` has no cached snapshot yet.
1110
+ */
1111
+ function handleInferSelector(state, command) {
1112
+ const snapshot = cachedSnapshot(state.snapshots, command.recipeId, command.path);
1113
+ if (snapshot === undefined) throw new Error(`no cached snapshot for "${command.recipeId}" at "${command.path}": call take-snapshot first`);
1114
+ const html = snapshot.html;
1115
+ const paths = command.nodeIds.map(nodeId => pathToNode(html, nodeId));
1116
+ if (paths.includes(undefined)) return {
1117
+ kind: 'unsupported',
1118
+ reason: 'one of the picked nodes is no longer in the snapshot; take a fresh snapshot and try again'
1119
+ };
1120
+ const [pathA, pathB] = paths;
1121
+ if (pathB === undefined) return {
1122
+ kind: 'field',
1123
+ field: fieldPick(html, pathA)
1124
+ };
1125
+ try {
1126
+ const result = inferList(pathA, pathB);
1127
+ if (result === null) return {
1128
+ kind: 'unsupported',
1129
+ reason: 'these two picks do not belong to the same list: pick the same field on two similar items'
1130
+ };
1131
+ const item = bestCandidate(html, candidatesFor(result.itemPath, false));
1132
+ if (item === undefined) return {
1133
+ kind: 'unsupported',
1134
+ reason: 'no selector matches every item of this list; try picking two items closer to each other in the markup'
1135
+ };
1136
+ const field = fieldPick(html, result.fieldPathA, result.fieldPath);
1137
+ return {
1138
+ kind: 'list',
1139
+ item: {
1140
+ selector: item.selector,
1141
+ tier: item.tier,
1142
+ matches: item.matches
1143
+ },
1144
+ field
1145
+ };
1146
+ } catch (error) {
1147
+ if (error instanceof ShadowDomUnsupportedError) return {
1148
+ kind: 'unsupported',
1149
+ reason: error.message
1150
+ };
1151
+ throw error;
1152
+ }
1153
+ }
1154
+ /** The best selector for one clicked node, `take` decided from the concrete clicked element; `rankSource` (a generalised path) is used for candidate generation when given, so a per-item-unique attribute of the one node picked cannot end up in the selector. */
1155
+ function fieldPick(html, clicked, rankSource = clicked) {
1156
+ const leaf = clicked.at(-1);
1157
+ const take = leaf === undefined ? 'text' : takeKindFor(leaf);
1158
+ const best = bestCandidate(html, candidatesFor(rankSource));
1159
+ if (best === undefined) throw new Error('no selector matches the picked node — this should not happen, since the node came from this same document');
1160
+ return {
1161
+ selector: best.selector,
1162
+ tier: best.tier,
1163
+ take,
1164
+ matches: best.matches
1165
+ };
1166
+ }
1167
+
374
1168
  /**
375
1169
  * The outline model: an `InputRecipe`'s `steps` (and only `steps`; the rest
376
1170
  * of the recipe passes through untouched, see `recipe-to-outline.mapper.ts`)
@@ -752,10 +1546,11 @@ async function saveRecipe(path, recipe) {
752
1546
  await writeFile(path, `${JSON.stringify(recipe, null, 2)}\n`, 'utf8');
753
1547
  }
754
1548
 
755
- /** @returns Fresh state for a new server instance: no workspace open, no run, no sockets. */
1549
+ /** @returns Fresh state for a new server instance: no workspace open, no run, no sockets, an empty snapshot cache, Playwright's own default browser. */
756
1550
  function createStudioState() {
757
1551
  return {
758
- sockets: new Set()
1552
+ sockets: new Set(),
1553
+ snapshots: createSnapshotCache()
759
1554
  };
760
1555
  }
761
1556
  /**
@@ -824,7 +1619,7 @@ async function handleRunSample(state, command) {
824
1619
  data: record.data
825
1620
  });
826
1621
  }
827
- });
1622
+ }, state.browser);
828
1623
  state.activeRun = handle;
829
1624
  void handle.result.then(result => {
830
1625
  if (state.activeRun === handle) state.activeRun = undefined;
@@ -943,6 +1738,76 @@ function serveStatic(root, pathname, response) {
943
1738
  createReadStream(file).pipe(response);
944
1739
  }
945
1740
 
1741
+ /**
1742
+ * Handles `take-snapshot`: loads the named input recipe, captures its
1743
+ * snapshot at `command.path` (only `"start"` is captured for now — see
1744
+ * `page-snapshot`'s `take-snapshot.use-case.ts`), and caches it so a later
1745
+ * `verify-selector`/`infer-selector` against the same recipe and path, or
1746
+ * the UI reselecting the same step, does not recapture it.
1747
+ *
1748
+ * @param state - The server state: `folder` must already be set.
1749
+ * @param command - The command.
1750
+ * @returns The rewritten snapshot.
1751
+ * @throws Error when no workspace is open, or the recipe cannot be found or loaded.
1752
+ */
1753
+ async function handleTakeSnapshot(state, command) {
1754
+ if (state.folder === undefined) throw new Error('open a workspace first');
1755
+ const cached = cachedSnapshot(state.snapshots, command.recipeId, command.path);
1756
+ if (cached !== undefined) return cached;
1757
+ const {
1758
+ input
1759
+ } = await loadRecipePair(state.folder, command.recipeId);
1760
+ const snapshot = await takeSnapshot(input, command.path, state.browser);
1761
+ putSnapshot(state.snapshots, command.recipeId, command.path, snapshot);
1762
+ return snapshot;
1763
+ }
1764
+
1765
+ /**
1766
+ * Handles `verify-selector`: runs a candidate through the engine's own
1767
+ * selection (`@opencraw/core`'s `countMatches`) against the cached
1768
+ * snapshot, and, best-effort, against the live page too — issue #91's "flag
1769
+ * a selector that matches the snapshot but not the live page". The live
1770
+ * page is fetched raw (`page-snapshot`'s `fetchStartPage`, the same reach
1771
+ * `take-snapshot` has: only `"start"` today), not through the rewritten
1772
+ * snapshot, so its match count is the site's own, not the studio's.
1773
+ *
1774
+ * @param state - The server state.
1775
+ * @param command - The command; `command.path` must already have a cached snapshot (call `take-snapshot` first).
1776
+ * @returns The match counts.
1777
+ * @throws Error when no workspace is open, the recipe cannot be found, or `command.path` has no cached snapshot yet.
1778
+ */
1779
+ async function handleVerifySelector(state, command) {
1780
+ if (state.folder === undefined) throw new Error('open a workspace first');
1781
+ const snapshot = cachedSnapshot(state.snapshots, command.recipeId, command.path);
1782
+ if (snapshot === undefined) throw new Error(`no cached snapshot for "${command.recipeId}" at "${command.path}": call take-snapshot first`);
1783
+ const snapshotMatches = countMatches(snapshot.html, command.selector);
1784
+ if (command.path !== 'start') return {
1785
+ selector: command.selector,
1786
+ snapshotMatches,
1787
+ liveChecked: false,
1788
+ liveError: 'only the start page can be checked against the live site for now'
1789
+ };
1790
+ try {
1791
+ const {
1792
+ input
1793
+ } = await loadRecipePair(state.folder, command.recipeId);
1794
+ const live = await fetchStartPage(input, state.browser);
1795
+ return {
1796
+ selector: command.selector,
1797
+ snapshotMatches,
1798
+ liveChecked: true,
1799
+ liveMatches: countMatches(live, command.selector)
1800
+ };
1801
+ } catch (error) {
1802
+ return {
1803
+ selector: command.selector,
1804
+ snapshotMatches,
1805
+ liveChecked: false,
1806
+ liveError: error instanceof Error ? error.message : String(error)
1807
+ };
1808
+ }
1809
+ }
1810
+
946
1811
  /** RFC 6455's fixed handshake suffix. */
947
1812
  const WS_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
948
1813
  /**
@@ -1059,6 +1924,7 @@ async function startStudioServer(options = {}) {
1059
1924
  const uiRoot = options.uiRoot ?? defaultUiRoot();
1060
1925
  const state = createStudioState();
1061
1926
  if (options.initialFolder !== undefined) state.folder = options.initialFolder;
1927
+ state.browser = options.browser;
1062
1928
  const server = createServer((request, response) => {
1063
1929
  void handleRequest(request, response, token, state, uiRoot);
1064
1930
  });
@@ -1155,6 +2021,18 @@ function dispatch(command, state) {
1155
2021
  {
1156
2022
  return handleSaveOutline(state, command);
1157
2023
  }
2024
+ case 'take-snapshot':
2025
+ {
2026
+ return handleTakeSnapshot(state, command);
2027
+ }
2028
+ case 'verify-selector':
2029
+ {
2030
+ return handleVerifySelector(state, command);
2031
+ }
2032
+ case 'infer-selector':
2033
+ {
2034
+ return Promise.resolve(handleInferSelector(state, command));
2035
+ }
1158
2036
  }
1159
2037
  }
1160
2038
  function respondJson(response, status, body) {
@@ -1211,5 +2089,5 @@ async function main(argv) {
1211
2089
  process.on('SIGTERM', shutdown);
1212
2090
  }
1213
2091
 
1214
- export { fetchStartPage, fetchStartPageCommandSchema, loadRecipePair, main, openWorkspace, openWorkspaceCommandSchema, outlineBracketSchema, outlineCardSchema, outlineNodeSchema, outlineViewSchema, recipeIssueSchema, recipeListingSchema, recordEventSchema, runFinishedEventSchema, runSample, runSampleCommandSchema, sampleBudgetSchema, sampleRunRecordSchema, sampleRunViewSchema, saveOutlineCommandSchema, saveRecipe, saveRecipeCommandSchema, sentencePartSchema, startPageViewSchema, startStudioServer, stopRunCommandSchema, stoppedBySchema, studioCommandSchema, studioEventSchema, traceLineEventSchema, workspaceChangedEventSchema, workspaceViewSchema };
2092
+ export { fetchStartPage, fetchStartPageCommandSchema, fieldPickSchema, inferSelectorCommandSchema, inferSelectorViewSchema, loadRecipePair, main, openWorkspace, openWorkspaceCommandSchema, outlineBracketSchema, outlineCardSchema, outlineNodeSchema, outlineViewSchema, recipeIssueSchema, recipeListingSchema, recordEventSchema, runFinishedEventSchema, runSample, runSampleCommandSchema, sampleBudgetSchema, sampleRunRecordSchema, sampleRunViewSchema, saveOutlineCommandSchema, saveRecipe, saveRecipeCommandSchema, sentencePartSchema, snapshotViewSchema, startPageViewSchema, startStudioServer, stopRunCommandSchema, stoppedBySchema, studioCommandSchema, studioEventSchema, takeSnapshotCommandSchema, traceLineEventSchema, verifySelectorCommandSchema, verifySelectorViewSchema, workspaceChangedEventSchema, workspaceViewSchema };
1215
2093
  //# sourceMappingURL=index.esm.js.map