harnery 0.5.0 → 0.6.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.
Files changed (68) hide show
  1. package/dist/commander.d.ts +10 -0
  2. package/dist/commander.d.ts.map +1 -1
  3. package/dist/commander.js +2 -0
  4. package/dist/commands/agents.d.ts.map +1 -1
  5. package/dist/commands/agents.js +43 -29
  6. package/dist/commands/browse-ai.js +1 -1
  7. package/dist/commands/browse.d.ts.map +1 -1
  8. package/dist/commands/browse.js +41 -9
  9. package/dist/commands/cookies.js +1 -1
  10. package/dist/commands/decision.d.ts +4 -0
  11. package/dist/commands/decision.d.ts.map +1 -0
  12. package/dist/commands/decision.js +354 -0
  13. package/dist/commands/docs.d.ts.map +1 -1
  14. package/dist/commands/docs.js +5 -1
  15. package/dist/commands/fetch.js +1 -1
  16. package/dist/core/agents/events/consume.d.ts +25 -2
  17. package/dist/core/agents/events/consume.d.ts.map +1 -1
  18. package/dist/core/agents/events/consume.js +55 -7
  19. package/dist/core/agents/events/emit.d.ts +2 -1
  20. package/dist/core/agents/events/emit.d.ts.map +1 -1
  21. package/dist/core/agents/events/emit.js +2 -1
  22. package/dist/core/agents/state/scratch.d.ts +1 -1
  23. package/dist/core/agents/state/scratch.js +2 -2
  24. package/dist/core/hooks/effects/index.d.ts.map +1 -1
  25. package/dist/core/hooks/effects/index.js +2 -1
  26. package/dist/lib/agent-browser/client.js +1 -1
  27. package/dist/lib/browser/client.d.ts +14 -0
  28. package/dist/lib/browser/client.d.ts.map +1 -1
  29. package/dist/lib/browser/client.js +20 -0
  30. package/dist/lib/browser/index.d.ts +1 -0
  31. package/dist/lib/browser/index.d.ts.map +1 -1
  32. package/dist/lib/browser/runts.d.ts +44 -0
  33. package/dist/lib/browser/runts.d.ts.map +1 -0
  34. package/dist/lib/browser/runts.js +193 -0
  35. package/dist/lib/completion/walk.js +1 -1
  36. package/dist/lib/cookies/client.d.ts +1 -1
  37. package/dist/lib/cookies/client.d.ts.map +1 -1
  38. package/dist/lib/cookies/client.js +1 -1
  39. package/dist/lib/decision/index.d.ts +212 -0
  40. package/dist/lib/decision/index.d.ts.map +1 -0
  41. package/dist/lib/decision/index.js +523 -0
  42. package/dist/lib/docs-lint.d.ts +1 -0
  43. package/dist/lib/docs-lint.d.ts.map +1 -1
  44. package/dist/lib/docs-lint.js +49 -0
  45. package/dist/lib/tunnel/gate.js +1 -1
  46. package/package.json +3 -1
  47. package/src/commander.ts +12 -0
  48. package/src/commands/agents.ts +47 -26
  49. package/src/commands/browse-ai.ts +1 -1
  50. package/src/commands/browse.ts +63 -8
  51. package/src/commands/cookies.ts +1 -1
  52. package/src/commands/decision.ts +438 -0
  53. package/src/commands/docs.ts +5 -1
  54. package/src/commands/fetch.ts +1 -1
  55. package/src/core/agents/events/consume.ts +65 -7
  56. package/src/core/agents/events/emit.ts +2 -1
  57. package/src/core/agents/state/scratch.ts +2 -2
  58. package/src/core/config.ts +1 -1
  59. package/src/core/hooks/effects/index.ts +2 -1
  60. package/src/lib/agent-browser/client.ts +1 -1
  61. package/src/lib/browser/client.ts +28 -0
  62. package/src/lib/browser/index.ts +4 -0
  63. package/src/lib/browser/runts.ts +218 -0
  64. package/src/lib/completion/walk.ts +1 -1
  65. package/src/lib/cookies/client.ts +2 -2
  66. package/src/lib/decision/index.ts +685 -0
  67. package/src/lib/docs-lint.ts +44 -0
  68. package/src/lib/tunnel/gate.ts +1 -1
@@ -18,6 +18,12 @@ import {
18
18
  type OverflowResult,
19
19
  type WidthResult,
20
20
  } from "./layout.js";
21
+ import {
22
+ buildClearRuntsAnnotationsScript,
23
+ buildRuntsAnnotateScript,
24
+ buildRuntsCheck,
25
+ type RuntsResult,
26
+ } from "./runts.js";
21
27
  import {
22
28
  buildAnnotateScript,
23
29
  buildClearAnnotationsScript,
@@ -357,6 +363,28 @@ export class Browser {
357
363
  });
358
364
  }
359
365
 
366
+ /**
367
+ * Scan text blocks for runts — a single word alone on a block's last
368
+ * visual line. Word-count per line via per-word Range rects (the width
369
+ * of the last line is deliberately NOT the signal; see runts.ts).
370
+ */
371
+ async checkRunts(opts: { scope?: string | null; minChars?: number } = {}): Promise<RuntsResult> {
372
+ return await this.currentPage.evaluate(buildRuntsCheck(), {
373
+ scope: opts.scope ?? null,
374
+ minChars: opts.minChars ?? 40,
375
+ });
376
+ }
377
+
378
+ /** Inject annotation overlays for runt hits. Used before screenshot. */
379
+ async annotateRunts(result: RuntsResult): Promise<void> {
380
+ await this.currentPage.evaluate(buildRuntsAnnotateScript(), { runts: result.runts });
381
+ }
382
+
383
+ /** Remove runt annotation overlays. */
384
+ async clearRuntsAnnotations(): Promise<void> {
385
+ await this.currentPage.evaluate(buildClearRuntsAnnotationsScript());
386
+ }
387
+
360
388
  /** Inject annotation overlays for width + overflow results. Used before screenshot. */
361
389
  async annotateLayout(args: {
362
390
  widths: WidthResult[];
@@ -17,6 +17,10 @@ export type {
17
17
  OverflowResult,
18
18
  WidthResult,
19
19
  } from "./layout.js";
20
+ export type {
21
+ RuntHit,
22
+ RuntsResult,
23
+ } from "./runts.js";
20
24
  export type {
21
25
  CheckVisibilityOptions,
22
26
  VisibilityResult,
@@ -0,0 +1,218 @@
1
+ // Runt detection for `harn browse` — a runt (a.k.a. "widow word") is a single
2
+ // short word sitting alone on the LAST visual line of a text block. Not a
3
+ // page-break issue; it happens mid-page and just looks sloppy.
4
+ //
5
+ // Detection is by COUNTING WORDS on the last visual line (== 1 word), NOT by
6
+ // relative width: a width threshold misses runts in narrow columns (how-to
7
+ // steps, a right-aligned contact box) where one short word still fills a big
8
+ // slice of the skinny column. "Text block" = element with real text whose
9
+ // element children are all inline — a <p>-only pass silently skips
10
+ // pull-quotes, blurbs and captions that ship as <div>/<blockquote>.
11
+ //
12
+ // Atomic tokens (URLs, emails, phone numbers) are excluded — they can't be
13
+ // rebalanced and usually sit on their own line by design. The detector can't
14
+ // catch a runt that is itself one long word; eyeball as a final pass.
15
+ //
16
+ // The word-level line mapping (per-word Range rects grouped by line top) was
17
+ // first built for the paged.js print-book lint; this is the generalized
18
+ // live-page port.
19
+
20
+ export interface RuntHit {
21
+ /** Human label for the owning block: `tag#id` / `tag.class` / `tag`. */
22
+ block: string;
23
+ /** The lone word on the last visual line (trailing punctuation included). */
24
+ word: string;
25
+ /** First ~80 chars of the block's text, for locating it. */
26
+ snippet: string;
27
+ /** Document-relative rect of the runt word (scroll offsets applied). */
28
+ rect: { x: number; y: number; width: number; height: number };
29
+ /** Number of visual lines in the block. */
30
+ lines: number;
31
+ }
32
+
33
+ export interface RuntsResult {
34
+ /** Text blocks scanned (post length/inline-children filters). */
35
+ scannedBlocks: number;
36
+ /** True when the block sweep hit the internal cap (very large page). */
37
+ truncated: boolean;
38
+ runts: RuntHit[];
39
+ }
40
+
41
+ /**
42
+ * Build the JS function passed to `page.evaluate`. `scope` narrows the sweep
43
+ * to one container (null = whole body); `minChars` filters out tiny labels
44
+ * that can't meaningfully wrap (default 40).
45
+ */
46
+ export function buildRuntsCheck(): (args: {
47
+ scope: string | null;
48
+ minChars: number;
49
+ }) => RuntsResult {
50
+ return ({ scope, minChars }) => {
51
+ const INLINE = new Set([
52
+ "B",
53
+ "I",
54
+ "EM",
55
+ "STRONG",
56
+ "SPAN",
57
+ "A",
58
+ "BR",
59
+ "SMALL",
60
+ "SUP",
61
+ "SUB",
62
+ "U",
63
+ "MARK",
64
+ "CODE",
65
+ "ABBR",
66
+ "TIME",
67
+ "WBR",
68
+ ]);
69
+ const ATOMIC = /@|https?:|\.(com|org|net|io|co)\b|\d-\d{3}/;
70
+ const BLOCK_CAP = 5000;
71
+ const RUNT_CAP = 50;
72
+
73
+ const root = scope ? document.querySelector(scope) : document.body;
74
+ if (!root) return { scannedBlocks: 0, truncated: false, runts: [] };
75
+
76
+ const sx = window.scrollX;
77
+ const sy = window.scrollY;
78
+
79
+ const label = (el: Element): string => {
80
+ const tag = el.tagName.toLowerCase();
81
+ if (el.id) return `${tag}#${el.id}`;
82
+ const cls = typeof el.className === "string" ? el.className.trim() : "";
83
+ if (cls) return `${tag}.${cls.split(/\s+/).slice(0, 2).join(".")}`;
84
+ return tag;
85
+ };
86
+
87
+ const all = root.querySelectorAll("*");
88
+ const runts: RuntHit[] = [];
89
+ let scanned = 0;
90
+ let truncated = false;
91
+
92
+ for (let i = 0; i < all.length; i++) {
93
+ const el = all[i];
94
+ if (!el) continue;
95
+ const text = el.textContent ?? "";
96
+ if (text.trim().length < minChars) continue;
97
+ let allInline = true;
98
+ for (const c of el.children) {
99
+ if (!INLINE.has(c.tagName)) {
100
+ allInline = false;
101
+ break;
102
+ }
103
+ }
104
+ if (!allInline) continue;
105
+ const elRect = el.getBoundingClientRect();
106
+ if (elRect.width <= 0 || elRect.height <= 0) continue;
107
+
108
+ if (scanned >= BLOCK_CAP) {
109
+ truncated = true;
110
+ break;
111
+ }
112
+ scanned++;
113
+
114
+ // Map every word to its visual line by rounding the Range rect top.
115
+ const lines = new Map<number, { words: string[]; last: DOMRect }>();
116
+ const tw = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
117
+ for (let n = tw.nextNode(); n; n = tw.nextNode()) {
118
+ const value = n.nodeValue ?? "";
119
+ for (const m of value.matchAll(/\S+/g)) {
120
+ const r = document.createRange();
121
+ r.setStart(n, m.index);
122
+ r.setEnd(n, m.index + m[0].length);
123
+ const rect = r.getBoundingClientRect();
124
+ if (!rect.width) continue;
125
+ const top = Math.round(rect.top);
126
+ const entry = lines.get(top);
127
+ if (entry) {
128
+ entry.words.push(m[0]);
129
+ entry.last = rect;
130
+ } else {
131
+ lines.set(top, { words: [m[0]], last: rect });
132
+ }
133
+ }
134
+ }
135
+ const tops = [...lines.keys()].sort((a, b) => a - b);
136
+ if (tops.length < 2) continue; // single line, no runt risk
137
+ const lastTop = tops[tops.length - 1];
138
+ const last = lastTop === undefined ? undefined : lines.get(lastTop);
139
+ if (last?.words.length !== 1) continue;
140
+ const word = last.words[0] ?? "";
141
+ if (ATOMIC.test(word)) continue;
142
+
143
+ runts.push({
144
+ block: label(el),
145
+ word,
146
+ snippet: text.replace(/\s+/g, " ").trim().slice(0, 80),
147
+ rect: {
148
+ x: Math.round(last.last.x + sx),
149
+ y: Math.round(last.last.y + sy),
150
+ width: Math.round(last.last.width),
151
+ height: Math.round(last.last.height),
152
+ },
153
+ lines: tops.length,
154
+ });
155
+ if (runts.length >= RUNT_CAP) {
156
+ truncated = true;
157
+ break;
158
+ }
159
+ }
160
+
161
+ return { scannedBlocks: scanned, truncated, runts };
162
+ };
163
+ }
164
+
165
+ /**
166
+ * Annotate runt hits onto the live page before the screenshot. Uses a
167
+ * document-absolute root (not fixed-inset like the other check overlays)
168
+ * because runts are usually below the fold and the rects are document
169
+ * coordinates — this keeps boxes aligned on full-page captures.
170
+ */
171
+ export function buildRuntsAnnotateScript(): (args: { runts: RuntHit[] }) => void {
172
+ return ({ runts }) => {
173
+ const ROOT_ID = "__bp-check-runt-annotations__";
174
+ document.getElementById(ROOT_ID)?.remove();
175
+ const root = document.createElement("div");
176
+ root.id = ROOT_ID;
177
+ root.style.cssText =
178
+ "position: absolute; top: 0; left: 0; width: 100%; height: 0; overflow: visible; pointer-events: none; z-index: 2147483646;";
179
+ document.body.appendChild(root);
180
+
181
+ for (const hit of runts) {
182
+ const box = document.createElement("div");
183
+ box.style.cssText = `
184
+ position: absolute;
185
+ left: ${hit.rect.x - 3}px;
186
+ top: ${hit.rect.y - 3}px;
187
+ width: ${hit.rect.width + 6}px;
188
+ height: ${hit.rect.height + 6}px;
189
+ border: 2px solid #d946ef;
190
+ box-sizing: border-box;
191
+ background: #d946ef1a;
192
+ pointer-events: none;
193
+ `;
194
+ const tag = document.createElement("div");
195
+ tag.textContent = `runt: …${hit.word} (${hit.block})`;
196
+ tag.style.cssText = `
197
+ position: absolute;
198
+ left: 0;
199
+ top: -18px;
200
+ background: #d946ef;
201
+ color: #fff;
202
+ font: 12px/1.4 -apple-system, system-ui, sans-serif;
203
+ padding: 1px 6px;
204
+ border-radius: 3px;
205
+ white-space: nowrap;
206
+ pointer-events: none;
207
+ `;
208
+ box.appendChild(tag);
209
+ root.appendChild(box);
210
+ }
211
+ };
212
+ }
213
+
214
+ export function buildClearRuntsAnnotationsScript(): () => void {
215
+ return () => {
216
+ document.getElementById("__bp-check-runt-annotations__")?.remove();
217
+ };
218
+ }
@@ -84,7 +84,7 @@ function walkCommand(
84
84
  // The root program represents `harn` itself; its path stays empty so that
85
85
  // top-level subcommands have paths like "anthropic", not "harn anthropic".
86
86
  // The shell driver walks COMP_WORDS[1..], so it's already operating in
87
- // post-`harn` space and shouldn't see "bp" in its lookup keys.
87
+ // post-`harn` space and shouldn't see the host bin name in its lookup keys.
88
88
  const path = isRoot ? "" : parentPath ? `${parentPath} ${name}` : name;
89
89
 
90
90
  const subcommands: CommandSpec[] = cmd.commands
@@ -41,7 +41,7 @@ export interface CookieStore {
41
41
  export interface CookieJarOptions {
42
42
  /** Absolute path to the JSON store. Caller must provide. */
43
43
  path: string;
44
- /** `exportedFrom` tag stamped on every save. Defaults to `"bp-cookies"`. */
44
+ /** `exportedFrom` tag stamped on every save. Defaults to `"harn-cookies"`. */
45
45
  source?: string;
46
46
  }
47
47
 
@@ -64,7 +64,7 @@ export class CookieJar {
64
64
 
65
65
  constructor(opts: CookieJarOptions) {
66
66
  this.path = opts.path;
67
- this.source = opts.source ?? "bp-cookies";
67
+ this.source = opts.source ?? "harn-cookies";
68
68
  }
69
69
 
70
70
  exists(): boolean {