@botiverse/porthole 0.10.0 → 0.12.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 (48) hide show
  1. package/dist/annotate/types.d.ts +18 -6
  2. package/dist/annotate/validate.d.ts +11 -1
  3. package/dist/annotate/validate.js +18 -1
  4. package/dist/check/files.d.ts +2 -0
  5. package/dist/check/files.js +67 -50
  6. package/dist/check/lint.d.ts +8 -1
  7. package/dist/check/lint.js +18 -0
  8. package/dist/check/markup.d.ts +7 -0
  9. package/dist/check/markup.js +49 -0
  10. package/dist/check/page-source.js +1 -1
  11. package/dist/check/scroll.d.ts +100 -0
  12. package/dist/check/scroll.js +1429 -0
  13. package/dist/conformance/harness-source.js +1 -1
  14. package/dist/conformance/runtime.js +6 -0
  15. package/dist/guide/guide.js +1 -1
  16. package/dist/guide/runtime.js +2 -2
  17. package/dist/host/create-host.js +3 -3
  18. package/dist/index.d.ts +1 -1
  19. package/dist/react/frame.d.ts +2 -1
  20. package/dist/runtime/requests.d.ts +5 -0
  21. package/dist/runtime/requests.js +16 -1
  22. package/dist/runtime/types.generated.js +1 -1
  23. package/dist/shell/annotate.d.ts +2 -1
  24. package/dist/shell/annotate.js +29 -12
  25. package/dist/shell/use.js +9 -1
  26. package/docs/ADOPTING.md +25 -3
  27. package/docs/ARCHITECTURE.md +2 -1
  28. package/docs/DECISIONS.md +391 -2
  29. package/docs/DESIGN.md +31 -9
  30. package/docs/THREAT_MODEL.md +42 -7
  31. package/package.json +1 -1
  32. package/src/annotate/types.ts +19 -6
  33. package/src/annotate/validate.ts +20 -1
  34. package/src/check/files.ts +68 -54
  35. package/src/check/lint.ts +24 -0
  36. package/src/check/markup.ts +53 -0
  37. package/src/check/scroll.ts +1757 -0
  38. package/src/conformance/runtime.ts +9 -0
  39. package/src/guide/guide.ts +1 -0
  40. package/src/guide/runtime.ts +2 -2
  41. package/src/host/create-host.ts +9 -3
  42. package/src/index.ts +1 -0
  43. package/src/react/frame.tsx +2 -1
  44. package/src/runtime/requests.ts +23 -1
  45. package/src/runtime/types.generated.ts +1 -1
  46. package/src/shell/annotate.ts +29 -12
  47. package/src/shell/use.ts +9 -1
  48. package/types/runtime/v1/comments.d.ts +14 -4
@@ -73,10 +73,21 @@ export interface AnnotationMark {
73
73
  * A marker (P104): with a count, the shell also draws a small bubble with this number (0 to
74
74
  * `ANNOTATION_LIMITS.count`; above 99 it reads "99+", 0 draws the bubble alone) at the end of a
75
75
  * text anchor and the top right of an element. The person can click it: the frame reports
76
- * `activated`. The content can read the number; give one only where the page may know it.
76
+ * `activated`. The content can read the number; give one only where the page may know it. A
77
+ * draft's is dropped (P107): its marker has no number.
77
78
  */
78
79
  readonly count?: number;
80
+ /**
81
+ * What the mark is (P107): `"posted"` (the default, as absent) or `"draft"`, a comment its
82
+ * author hasn't submitted yet. A draft is drawn apart (a dashed line and a hollow marker with no
83
+ * number) and has a marker whether or not it has a count; its count is dropped before the mark
84
+ * goes to the frame. The content can read that a mark is a draft: send drafts only to their
85
+ * author's own frame.
86
+ */
87
+ readonly kind?: AnnotationMarkKind;
79
88
  }
89
+ /** A posted comment's mark, or an unsubmitted draft's (P107). */
90
+ export type AnnotationMarkKind = "posted" | "draft";
80
91
  /** Whether a mark's anchor was found in the content as it is now. */
81
92
  export type AnchorState = "attached" | "orphaned";
82
93
  export interface AnchorResult {
@@ -103,7 +114,8 @@ export type AnnotationReport =
103
114
  }
104
115
  /**
105
116
  * Where the marked anchors are now: every mark after `show`, the focused one after `focus`,
106
- * every mark when the content changed one's state, and, while any mark has a `count`, once a
117
+ * every mark when the content changed one's state, and, while any mark has a marker (a `count`,
118
+ * or a draft, P107), once a
107
119
  * scroll, a resize or a change of the content has settled, only the marks that moved since the
108
120
  * host was last told, at most once a second (P104).
109
121
  */
@@ -112,10 +124,10 @@ export type AnnotationReport =
112
124
  readonly results: readonly AnchorResult[];
113
125
  }
114
126
  /**
115
- * The person clicked the marker of a mark the host shows with a `count` (P104); `rect` is the
116
- * marker's box, to open the host's own UI beside it. The content can fake it for any mark the
117
- * host shows with a count, and can tell a real one happened: open the host's own view of that
118
- * mark, never act on it.
127
+ * The person clicked the marker of a mark the host shows with a `count` (P104), or of a draft
128
+ * (P107); `rect` is the marker's box, to open the host's own UI beside it. The content can fake
129
+ * it for any mark the host shows with a marker, and can tell a real one happened: open the
130
+ * host's own view of that mark, never act on it.
119
131
  */
120
132
  | {
121
133
  readonly type: "activated";
@@ -22,5 +22,15 @@ export declare function clampRect(rect: AnnotationRect, viewport: {
22
22
  export declare function parseReport(value: unknown): AnnotationReport | null;
23
23
  /** What the host may start with: elements, text, or both (default both). */
24
24
  export declare function annotateMode(mode: AnnotateMode | undefined): Required<AnnotateMode>;
25
- /** The host's marks, checked before they go to the frame; throws on the first bad one. */
25
+ /**
26
+ * Whether a mark gets a marker, the one thing a person can click (P104): one with a `count`, and
27
+ * every draft (P107).
28
+ */
29
+ export declare const hasMarker: (mark: AnnotationMark) => boolean;
30
+ /**
31
+ * The host's marks, checked before they go to the frame; throws on the first bad one. Each is
32
+ * rebuilt as the frame gets it: a posted mark as 0.10.0 sent it (no `kind`), a draft with
33
+ * `kind: "draft"` and without its count (P107: a draft's marker shows no number, so the page isn't
34
+ * told one).
35
+ */
26
36
  export declare function checkMarks(marks: readonly AnnotationMark[]): AnnotationMark[];
@@ -166,7 +166,17 @@ export function annotateMode(mode) {
166
166
  }
167
167
  return { elements, text: words };
168
168
  }
169
- /** The host's marks, checked before they go to the frame; throws on the first bad one. */
169
+ /**
170
+ * Whether a mark gets a marker, the one thing a person can click (P104): one with a `count`, and
171
+ * every draft (P107).
172
+ */
173
+ export const hasMarker = (mark) => mark.count !== undefined || mark.kind === "draft";
174
+ /**
175
+ * The host's marks, checked before they go to the frame; throws on the first bad one. Each is
176
+ * rebuilt as the frame gets it: a posted mark as 0.10.0 sent it (no `kind`), a draft with
177
+ * `kind: "draft"` and without its count (P107: a draft's marker shows no number, so the page isn't
178
+ * told one).
179
+ */
170
180
  export function checkMarks(marks) {
171
181
  if (!Array.isArray(marks) || marks.length > LIMITS.marks) {
172
182
  throw new TypeError(`annotations.show: at most ${LIMITS.marks} marks`);
@@ -180,7 +190,14 @@ export function checkMarks(marks) {
180
190
  if (mark.count !== undefined && !count(mark.count, LIMITS.count)) {
181
191
  throw new TypeError(`annotations.show: mark ${index}'s count is a whole number from 0 to ${LIMITS.count}`);
182
192
  }
193
+ const kind = mark.kind;
194
+ if (kind !== undefined && kind !== "posted" && kind !== "draft") {
195
+ throw new TypeError(`annotations.show: mark ${index}'s kind is "posted" or "draft"`);
196
+ }
183
197
  seen.add(mark.id);
198
+ if (kind === "draft") {
199
+ return { id: mark.id, anchor, kind };
200
+ }
184
201
  return { id: mark.id, anchor, ...(mark.count === undefined ? {} : { count: mark.count }) };
185
202
  });
186
203
  }
@@ -13,6 +13,8 @@ export interface FileFinding extends Omit<Finding, "rule"> {
13
13
  }
14
14
  /** A set's files, by path: text, or bytes. */
15
15
  export type FileContents = Readonly<Record<string, string | Uint8Array>>;
16
+ /** The most pages the scroll-lock rule reads in a version: its entry, and 7 more (P106). */
17
+ export declare const SCROLL_PAGES = 8;
16
18
  /**
17
19
  * Lints a page that comes with files (P081): the set (paths, limits, its entry), each page as
18
20
  * `lintContent` lints a page, each relative reference looked up in the set (a leading `/`, which
@@ -1,7 +1,9 @@
1
1
  import { checkFileSet, DEFAULT_ENTRY, fileLimits, lookupFile, servesFiles, } from "../files/paths.js";
2
2
  import { fileTypeOf, knownType } from "../files/types.js";
3
3
  import { sizeWords } from "../guide/limits.js";
4
- import { lintContent } from "./lint.js";
4
+ import { lintPage } from "./lint.js";
5
+ import { attributesOf } from "./markup.js";
6
+ import { scrollBudget, scrollLock } from "./scroll.js";
5
7
  /** A text's line numbers by offset: its line starts found once, each line then a binary search. */
6
8
  function linesOf(text) {
7
9
  const starts = [0];
@@ -45,54 +47,6 @@ const REFERENCE_ATTRIBUTES = {
45
47
  };
46
48
  /** A tag: its name and its attributes' text. Bounded, and never across another `<`: linear. */
47
49
  const TAG = /<([a-z][a-z0-9-]{0,30})\b([^<>]{0,8192})>/giu;
48
- const isSpace = (code) => code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
49
- /** A tag's attributes, read once from left to right (linear): each name, value and its offset. */
50
- function attributesOf(text) {
51
- const out = [];
52
- let at = 0;
53
- while (at < text.length) {
54
- while (at < text.length && (isSpace(text.charCodeAt(at)) || text[at] === "/")) {
55
- at += 1;
56
- }
57
- const nameStart = at;
58
- while (at < text.length &&
59
- !isSpace(text.charCodeAt(at)) &&
60
- text[at] !== "=" &&
61
- text[at] !== "/") {
62
- at += 1;
63
- }
64
- const name = text.slice(nameStart, at).toLowerCase();
65
- if (name === "") {
66
- at += 1;
67
- continue;
68
- }
69
- while (at < text.length && isSpace(text.charCodeAt(at))) {
70
- at += 1;
71
- }
72
- if (text[at] !== "=") {
73
- continue;
74
- }
75
- at += 1;
76
- while (at < text.length && isSpace(text.charCodeAt(at))) {
77
- at += 1;
78
- }
79
- const quote = text[at];
80
- if (quote === '"' || quote === "'") {
81
- const close = text.indexOf(quote, at + 1);
82
- const end = close < 0 ? text.length : close;
83
- out.push({ name, value: text.slice(at + 1, end), at: at + 1 });
84
- at = end + 1;
85
- }
86
- else {
87
- const start = at;
88
- while (at < text.length && !isSpace(text.charCodeAt(at))) {
89
- at += 1;
90
- }
91
- out.push({ name, value: text.slice(start, at), at: start });
92
- }
93
- }
94
- return out;
95
- }
96
50
  /** What a page's markup loads or links: each element's `src`, `href` or `poster`. */
97
51
  function markupReferences(text) {
98
52
  const found = [];
@@ -179,6 +133,8 @@ function resolve(directory, reference) {
179
133
  return out.join("/");
180
134
  }
181
135
  const directoryOf = (path) => path.slice(0, path.lastIndexOf("/") + 1);
136
+ /** The most pages the scroll-lock rule reads in a version: its entry, and 7 more (P106). */
137
+ export const SCROLL_PAGES = 8;
182
138
  /**
183
139
  * Lints a page that comes with files (P081): the set (paths, limits, its entry), each page as
184
140
  * `lintContent` lints a page, each relative reference looked up in the set (a leading `/`, which
@@ -255,7 +211,30 @@ export function lintFiles(config, files, options = {}) {
255
211
  }
256
212
  }
257
213
  };
214
+ // A stylesheet a page links or a stylesheet imports, found in the set by its path (P106).
215
+ const decoded = new Map();
216
+ const stylesheet = (href, from) => {
217
+ if (href === "" || elsewhere(href) || href.startsWith("/")) {
218
+ return null;
219
+ }
220
+ const target = resolve(directoryOf(from), href);
221
+ const sheet = target === null || !has(target) ? undefined : files[target];
222
+ if (sheet === undefined || target === null) {
223
+ return null;
224
+ }
225
+ // Decoded once for the whole set, however many pages link it.
226
+ let text = decoded.get(target);
227
+ if (text === undefined) {
228
+ text = textOf(sheet).slice(0, SCANNED);
229
+ decoded.set(target, text);
230
+ }
231
+ return { path: target, text };
232
+ };
233
+ const it = config.mode === "mcp-app" ? "view" : config.mode === "inline" ? "widget" : "page";
258
234
  const maxPage = config.limits.maxBytes;
235
+ // The version's pages, read, for the scroll-lock rule after the rest (P106). A page over its
236
+ // limit isn't read at all: its error says so.
237
+ const pages = new Set();
259
238
  for (const [path, body] of Object.entries(files)) {
260
239
  const { role } = fileTypeOf(path);
261
240
  if (!knownType(path)) {
@@ -280,9 +259,12 @@ export function lintFiles(config, files, options = {}) {
280
259
  continue;
281
260
  }
282
261
  const text = textOf(body);
283
- for (const finding of lintContent(config, text)) {
262
+ // The scroll-lock rule apart, with the stylesheets the page links (P106).
263
+ for (const finding of lintPage(config, text, { scrollLock: false })) {
284
264
  findings.push({ ...finding, path });
285
265
  }
266
+ // Its path only: the few the rule reads are decoded again there, one at a time.
267
+ pages.add(path);
286
268
  const lines = linesOf(text);
287
269
  check(path, lines, markupReferences(text), directory, "error");
288
270
  check(path, lines, patternReferences(text, CSS_REFERENCES), directory, "warning");
@@ -301,6 +283,41 @@ export function lintFiles(config, files, options = {}) {
301
283
  check(path, lines, patternReferences(text, PAGE_REFERENCES), directoryOf(entry), "warning", directory);
302
284
  }
303
285
  }
286
+ // Its own styles holding a page to one screen (P106): the entry first, then the others in path
287
+ // order, at most SCROLL_PAGES, within one budget for them all. A warning: past either, the pages
288
+ // left aren't checked, and it says how many.
289
+ const budget = scrollBudget();
290
+ const order = [
291
+ ...(pages.has(entry) ? [entry] : []),
292
+ ...[...pages].filter((path) => path !== entry).sort(),
293
+ ];
294
+ let checked = 0;
295
+ for (const path of order.slice(0, SCROLL_PAGES)) {
296
+ const body = files[path];
297
+ const text = body === undefined ? "" : textOf(body);
298
+ const held = scrollLock(text, { noun: it, path, sheet: stylesheet, budget });
299
+ if (budget.spent) {
300
+ break;
301
+ }
302
+ checked += 1;
303
+ if (held !== null) {
304
+ findings.push({
305
+ rule: "scroll-lock",
306
+ severity: "warning",
307
+ message: held.message,
308
+ path: held.path ?? path,
309
+ line: held.line,
310
+ });
311
+ }
312
+ }
313
+ const unchecked = pages.size - checked;
314
+ if (unchecked > 0) {
315
+ findings.push({
316
+ rule: "scroll-lock",
317
+ severity: "warning",
318
+ message: `${unchecked} page${unchecked === 1 ? "" : "s"} not checked for scroll lock: the check reads the entry and at most ${SCROLL_PAGES - 1} other pages, within one budget. If one of them holds html, body or its root to one screen (overflow: hidden with height: 100% or 100vh, or position: fixed), let it scroll as a document.`,
319
+ });
320
+ }
304
321
  return findings;
305
322
  }
306
323
  /** Where a finding is, for the agent: `about.html line 3: `. */
@@ -4,7 +4,7 @@ import type { PortholeConfig } from "../config/schema.ts";
4
4
  * the agent fixes it before anyone sees it. Pure: any runtime. Errors are only what certainly
5
5
  * fails or is cut (`refuseOn: "error"` refuses those); the rest are warnings.
6
6
  */
7
- export type LintRule = "size" | "document" | "nested-frame" | "document-write" | "clipboard" | "dialog" | "form" | "egress" | "unpinned" | "position-fixed" | "new-window";
7
+ export type LintRule = "size" | "document" | "nested-frame" | "document-write" | "clipboard" | "dialog" | "form" | "egress" | "unpinned" | "position-fixed" | "scroll-lock" | "new-window";
8
8
  export interface Finding {
9
9
  readonly rule: LintRule;
10
10
  readonly severity: "error" | "warning";
@@ -19,3 +19,10 @@ export interface Finding {
19
19
  */
20
20
  export declare function fragmentOf(content: string): string | null;
21
21
  export declare function lintContent(config: PortholeConfig, content: string): Finding[];
22
+ /**
23
+ * `lintContent`, with or without the scroll-lock rule: `lintFiles` reads that one itself, once,
24
+ * with the stylesheets the page links.
25
+ */
26
+ export declare function lintPage(config: PortholeConfig, content: string, options: {
27
+ readonly scrollLock: boolean;
28
+ }): Finding[];
@@ -1,6 +1,7 @@
1
1
  import { anyHttps, declaredCapabilities, egressOrigins, librarySources, } from "../compile/policy.js";
2
2
  import { sizeWords } from "../guide/limits.js";
3
3
  import { originMatchesSource } from "../isolation/served.js";
4
+ import { scrollLock } from "./scroll.js";
4
5
  const lineOf = (text, index) => text.slice(0, index).split("\n").length;
5
6
  const noun = (config) => config.mode === "artifact" ? "page" : config.mode === "mcp-app" ? "view" : "widget";
6
7
  /** A whole document's markers: what a fragment must not have. */
@@ -71,6 +72,13 @@ const OWN_CDN = [
71
72
  { name: "Plotly", host: /^https:\/\/cdn\.plot\.ly\/plotly/u, pinned: /\d+\.\d+\.\d+/u },
72
73
  ];
73
74
  export function lintContent(config, content) {
75
+ return lintPage(config, content, { scrollLock: true });
76
+ }
77
+ /**
78
+ * `lintContent`, with or without the scroll-lock rule: `lintFiles` reads that one itself, once,
79
+ * with the stylesheets the page links.
80
+ */
81
+ export function lintPage(config, content, options) {
74
82
  if (config.mode === "url") {
75
83
  return [];
76
84
  }
@@ -159,6 +167,16 @@ export function lintContent(config, content) {
159
167
  add("position-fixed", "warning", `No position: fixed: the frame grows with the ${it}'s content, so a fixed element covers it. Use position: sticky or normal flow.`, fixed.index);
160
168
  }
161
169
  }
170
+ // Its own styles holding it to one screen (P106): a warning, as a one-screen app means it.
171
+ const held = options.scrollLock ? scrollLock(content, { noun: it }) : null;
172
+ if (held !== null) {
173
+ findings.push({
174
+ rule: "scroll-lock",
175
+ severity: "warning",
176
+ message: held.message,
177
+ line: held.line,
178
+ });
179
+ }
162
180
  const opens = /target\s*=\s*["']?_blank|\bwindow\s*\.\s*open\s*\(/u.exec(content);
163
181
  if (opens !== null && !declared.has("openLink")) {
164
182
  add("new-window", "warning", "Nothing opens a new window here (window.open returns null, target=_blank does nothing): show it inline.", opens.index);
@@ -0,0 +1,7 @@
1
+ /** Reading markup without parsing it: what the checks share. Linear, however it was written. */
2
+ /** A tag's attributes, read once from left to right (linear): each name, value and its offset. */
3
+ export declare function attributesOf(text: string): {
4
+ name: string;
5
+ value: string;
6
+ at: number;
7
+ }[];
@@ -0,0 +1,49 @@
1
+ /** Reading markup without parsing it: what the checks share. Linear, however it was written. */
2
+ const isSpace = (code) => code === 32 || code === 9 || code === 10 || code === 12 || code === 13;
3
+ /** A tag's attributes, read once from left to right (linear): each name, value and its offset. */
4
+ export function attributesOf(text) {
5
+ const out = [];
6
+ let at = 0;
7
+ while (at < text.length) {
8
+ while (at < text.length && (isSpace(text.charCodeAt(at)) || text[at] === "/")) {
9
+ at += 1;
10
+ }
11
+ const nameStart = at;
12
+ while (at < text.length &&
13
+ !isSpace(text.charCodeAt(at)) &&
14
+ text[at] !== "=" &&
15
+ text[at] !== "/") {
16
+ at += 1;
17
+ }
18
+ const name = text.slice(nameStart, at).toLowerCase();
19
+ if (name === "") {
20
+ at += 1;
21
+ continue;
22
+ }
23
+ while (at < text.length && isSpace(text.charCodeAt(at))) {
24
+ at += 1;
25
+ }
26
+ if (text[at] !== "=") {
27
+ continue;
28
+ }
29
+ at += 1;
30
+ while (at < text.length && isSpace(text.charCodeAt(at))) {
31
+ at += 1;
32
+ }
33
+ const quote = text[at];
34
+ if (quote === '"' || quote === "'") {
35
+ const close = text.indexOf(quote, at + 1);
36
+ const end = close < 0 ? text.length : close;
37
+ out.push({ name, value: text.slice(at + 1, end), at: at + 1 });
38
+ at = end + 1;
39
+ }
40
+ else {
41
+ const start = at;
42
+ while (at < text.length && !isSpace(text.charCodeAt(at))) {
43
+ at += 1;
44
+ }
45
+ out.push({ name, value: text.slice(start, at), at: start });
46
+ }
47
+ }
48
+ return out;
49
+ }