@sixb/agent 0.0.0-stage → 0.2.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 (240) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -2
  3. package/dist/abort.d.ts +6 -0
  4. package/dist/abort.d.ts.map +1 -0
  5. package/dist/agent-prompt.d.ts +126 -0
  6. package/dist/agent-prompt.d.ts.map +1 -0
  7. package/dist/agent-runtime/errors.d.ts +13 -0
  8. package/dist/agent-runtime/errors.d.ts.map +1 -0
  9. package/dist/agent-runtime/preflight.d.ts +18 -0
  10. package/dist/agent-runtime/preflight.d.ts.map +1 -0
  11. package/dist/agent-runtime/profile.d.ts +14 -0
  12. package/dist/agent-runtime/profile.d.ts.map +1 -0
  13. package/dist/artifacts/layout-check/layout-check.d.ts +87 -0
  14. package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -0
  15. package/dist/artifacts/materialize.d.ts +115 -0
  16. package/dist/artifacts/materialize.d.ts.map +1 -0
  17. package/dist/artifacts/publish-tool.d.ts +86 -0
  18. package/dist/artifacts/publish-tool.d.ts.map +1 -0
  19. package/dist/ask-parent-tool.d.ts +36 -0
  20. package/dist/ask-parent-tool.d.ts.map +1 -0
  21. package/dist/attachments.d.ts +109 -0
  22. package/dist/attachments.d.ts.map +1 -0
  23. package/dist/child-files.d.ts +91 -0
  24. package/dist/child-files.d.ts.map +1 -0
  25. package/dist/child-questions.d.ts +24 -0
  26. package/dist/child-questions.d.ts.map +1 -0
  27. package/dist/computer-runner.d.ts +74 -0
  28. package/dist/computer-runner.d.ts.map +1 -0
  29. package/dist/context-budget.d.ts +34 -0
  30. package/dist/context-budget.d.ts.map +1 -0
  31. package/dist/context-compaction.d.ts +148 -0
  32. package/dist/context-compaction.d.ts.map +1 -0
  33. package/dist/context-guard.d.ts +69 -0
  34. package/dist/context-guard.d.ts.map +1 -0
  35. package/dist/conversation-memory.d.ts +21 -0
  36. package/dist/conversation-memory.d.ts.map +1 -0
  37. package/dist/conversation-messages.d.ts +33 -0
  38. package/dist/conversation-messages.d.ts.map +1 -0
  39. package/dist/errors.d.ts +46 -0
  40. package/dist/errors.d.ts.map +1 -0
  41. package/dist/execution-plan.d.ts +38 -0
  42. package/dist/execution-plan.d.ts.map +1 -0
  43. package/dist/failure.d.ts +14 -0
  44. package/dist/failure.d.ts.map +1 -0
  45. package/dist/family-mailbox.d.ts +60 -0
  46. package/dist/family-mailbox.d.ts.map +1 -0
  47. package/dist/file-ref.d.ts +6 -0
  48. package/dist/file-ref.d.ts.map +1 -0
  49. package/dist/image-attachments.d.ts +34 -0
  50. package/dist/image-attachments.d.ts.map +1 -0
  51. package/dist/index.d.ts +35 -0
  52. package/dist/index.d.ts.map +1 -0
  53. package/dist/index.js +14387 -0
  54. package/dist/index.js.map +72 -0
  55. package/dist/inline-images.d.ts +28 -0
  56. package/dist/inline-images.d.ts.map +1 -0
  57. package/dist/message-authors.d.ts +34 -0
  58. package/dist/message-authors.d.ts.map +1 -0
  59. package/dist/message-times.d.ts +9 -0
  60. package/dist/message-times.d.ts.map +1 -0
  61. package/dist/model-adapters.d.ts +59 -0
  62. package/dist/model-adapters.d.ts.map +1 -0
  63. package/dist/model-lines.d.ts +16 -0
  64. package/dist/model-lines.d.ts.map +1 -0
  65. package/dist/prompt-cache.d.ts +28 -0
  66. package/dist/prompt-cache.d.ts.map +1 -0
  67. package/dist/requester.d.ts +15 -0
  68. package/dist/requester.d.ts.map +1 -0
  69. package/dist/run-agent-turn.d.ts +93 -0
  70. package/dist/run-agent-turn.d.ts.map +1 -0
  71. package/dist/run-child.d.ts +77 -0
  72. package/dist/run-child.d.ts.map +1 -0
  73. package/dist/run-conversation-turn.d.ts +42 -0
  74. package/dist/run-conversation-turn.d.ts.map +1 -0
  75. package/dist/run-credential.d.ts +22 -0
  76. package/dist/run-credential.d.ts.map +1 -0
  77. package/dist/run-environment.d.ts +145 -0
  78. package/dist/run-environment.d.ts.map +1 -0
  79. package/dist/run-fate.d.ts +15 -0
  80. package/dist/run-fate.d.ts.map +1 -0
  81. package/dist/run-files-lock.d.ts +16 -0
  82. package/dist/run-files-lock.d.ts.map +1 -0
  83. package/dist/run-sandbox.d.ts +60 -0
  84. package/dist/run-sandbox.d.ts.map +1 -0
  85. package/dist/run-subagent.d.ts +65 -0
  86. package/dist/run-subagent.d.ts.map +1 -0
  87. package/dist/run-workflow-agent-node.d.ts +46 -0
  88. package/dist/run-workflow-agent-node.d.ts.map +1 -0
  89. package/dist/sandbox-api-context.d.ts +34 -0
  90. package/dist/sandbox-api-context.d.ts.map +1 -0
  91. package/dist/sandbox-file-registry.d.ts +9 -0
  92. package/dist/sandbox-file-registry.d.ts.map +1 -0
  93. package/dist/sandbox-files.d.ts +71 -0
  94. package/dist/sandbox-files.d.ts.map +1 -0
  95. package/dist/sandbox-handle.d.ts +15 -0
  96. package/dist/sandbox-handle.d.ts.map +1 -0
  97. package/dist/sandbox-readiness.d.ts +8 -0
  98. package/dist/sandbox-readiness.d.ts.map +1 -0
  99. package/dist/skills/built-in.d.ts +15 -0
  100. package/dist/skills/built-in.d.ts.map +1 -0
  101. package/dist/subagent-context.d.ts +50 -0
  102. package/dist/subagent-context.d.ts.map +1 -0
  103. package/dist/subagent-continuation.d.ts +33 -0
  104. package/dist/subagent-continuation.d.ts.map +1 -0
  105. package/dist/subagent-inbox.d.ts +53 -0
  106. package/dist/subagent-inbox.d.ts.map +1 -0
  107. package/dist/subagent-tools.d.ts +293 -0
  108. package/dist/subagent-tools.d.ts.map +1 -0
  109. package/dist/thread-context.d.ts +17 -0
  110. package/dist/thread-context.d.ts.map +1 -0
  111. package/dist/tools/attachments.d.ts +11 -0
  112. package/dist/tools/attachments.d.ts.map +1 -0
  113. package/dist/tools/bash.d.ts +68 -0
  114. package/dist/tools/bash.d.ts.map +1 -0
  115. package/dist/tools/model-spec.d.ts +9 -0
  116. package/dist/tools/model-spec.d.ts.map +1 -0
  117. package/dist/tools/read.d.ts +43 -0
  118. package/dist/tools/read.d.ts.map +1 -0
  119. package/dist/tools/result-media.d.ts +65 -0
  120. package/dist/tools/result-media.d.ts.map +1 -0
  121. package/dist/tools/result-output.d.ts +18 -0
  122. package/dist/tools/result-output.d.ts.map +1 -0
  123. package/dist/tools/tool-files.d.ts +59 -0
  124. package/dist/tools/tool-files.d.ts.map +1 -0
  125. package/dist/tools/tool-outputs.d.ts +63 -0
  126. package/dist/tools/tool-outputs.d.ts.map +1 -0
  127. package/dist/tools/view-file.d.ts +40 -0
  128. package/dist/tools/view-file.d.ts.map +1 -0
  129. package/dist/tsconfig.json +5 -0
  130. package/dist/turn-runtime.d.ts +19 -0
  131. package/dist/turn-runtime.d.ts.map +1 -0
  132. package/dist/types.d.ts +83 -0
  133. package/dist/types.d.ts.map +1 -0
  134. package/dist/workflow-input-attachments.d.ts +18 -0
  135. package/dist/workflow-input-attachments.d.ts.map +1 -0
  136. package/package.json +59 -4
  137. package/src/abort.ts +40 -0
  138. package/src/agent-prompt.ts +860 -0
  139. package/src/agent-runtime/errors.ts +44 -0
  140. package/src/agent-runtime/preflight.ts +314 -0
  141. package/src/agent-runtime/profile.ts +23 -0
  142. package/src/artifacts/layout-check/check.cjs +478 -0
  143. package/src/artifacts/layout-check/layout-check.ts +460 -0
  144. package/src/artifacts/materialize.ts +380 -0
  145. package/src/artifacts/publish-tool.ts +700 -0
  146. package/src/ask-parent-tool.ts +100 -0
  147. package/src/attachments.ts +1041 -0
  148. package/src/child-files.ts +535 -0
  149. package/src/child-questions.ts +42 -0
  150. package/src/computer-runner.ts +336 -0
  151. package/src/context-budget.ts +177 -0
  152. package/src/context-compaction.ts +1349 -0
  153. package/src/context-guard.ts +826 -0
  154. package/src/conversation-memory.ts +45 -0
  155. package/src/conversation-messages.ts +86 -0
  156. package/src/errors.ts +85 -0
  157. package/src/execution-plan.ts +70 -0
  158. package/src/failure.ts +82 -0
  159. package/src/family-mailbox.ts +189 -0
  160. package/src/file-ref.ts +18 -0
  161. package/src/image-attachments.ts +322 -0
  162. package/src/index.ts +54 -0
  163. package/src/inline-images.ts +69 -0
  164. package/src/message-authors.ts +77 -0
  165. package/src/message-times.ts +24 -0
  166. package/src/model-adapters.ts +273 -0
  167. package/src/model-lines.ts +28 -0
  168. package/src/prompt-cache.ts +40 -0
  169. package/src/requester.ts +21 -0
  170. package/src/run-agent-turn.ts +509 -0
  171. package/src/run-child.ts +344 -0
  172. package/src/run-conversation-turn.ts +272 -0
  173. package/src/run-credential.ts +55 -0
  174. package/src/run-environment.ts +1216 -0
  175. package/src/run-fate.ts +51 -0
  176. package/src/run-files-lock.ts +118 -0
  177. package/src/run-sandbox.ts +63 -0
  178. package/src/run-subagent.ts +433 -0
  179. package/src/run-workflow-agent-node.ts +344 -0
  180. package/src/sandbox-api-context.ts +237 -0
  181. package/src/sandbox-file-registry.ts +26 -0
  182. package/src/sandbox-files.ts +326 -0
  183. package/src/sandbox-handle.ts +19 -0
  184. package/src/sandbox-readiness.ts +22 -0
  185. package/src/skills/built-in.ts +183 -0
  186. package/src/skills/sixb-artifacts/SKILL.md +95 -0
  187. package/src/skills/sixb-artifacts/canvas.md +104 -0
  188. package/src/skills/sixb-artifacts/document.md +63 -0
  189. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/first-job.html +42 -0
  190. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/region.html +29 -0
  191. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/shared.css +35 -0
  192. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/welcome.html +31 -0
  193. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/canvas.json +107 -0
  194. package/src/skills/sixb-artifacts/examples/canvas/retro/canvas.json +158 -0
  195. package/src/skills/sixb-artifacts/examples/document/incident-report/document.md +35 -0
  196. package/src/skills/sixb-artifacts/examples/document/operations-review/document.md +41 -0
  197. package/src/skills/sixb-artifacts/examples/document/operations-review/figures/restorations.svg +1 -0
  198. package/src/skills/sixb-artifacts/examples/page/collections-desk/index.html +169 -0
  199. package/src/skills/sixb-artifacts/examples/page/collections-desk/queries/overdue.json +13 -0
  200. package/src/skills/sixb-artifacts/examples/page/dispatch-board/data/dispatch.json +234 -0
  201. package/src/skills/sixb-artifacts/examples/page/dispatch-board/index.html +245 -0
  202. package/src/skills/sixb-artifacts/examples/page/fleet-planner/data/plan.json +53 -0
  203. package/src/skills/sixb-artifacts/examples/page/fleet-planner/index.html +462 -0
  204. package/src/skills/sixb-artifacts/examples/page/service-explorer/data/service.json +276 -0
  205. package/src/skills/sixb-artifacts/examples/page/service-explorer/index.html +549 -0
  206. package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/queries/overdue.json +13 -0
  207. package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/workbook.json +30 -0
  208. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Inventory.csv +13 -0
  209. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Spend.csv +6 -0
  210. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/workbook.json +47 -0
  211. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Quarters.csv +5 -0
  212. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Regions.csv +5 -0
  213. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/workbook.json +78 -0
  214. package/src/skills/sixb-artifacts/examples/slides/launch-plan/index.html +63 -0
  215. package/src/skills/sixb-artifacts/examples/slides/q3-review/index.html +133 -0
  216. package/src/skills/sixb-artifacts/file.md +17 -0
  217. package/src/skills/sixb-artifacts/page.md +215 -0
  218. package/src/skills/sixb-artifacts/queries.md +66 -0
  219. package/src/skills/sixb-artifacts/references.md +68 -0
  220. package/src/skills/sixb-artifacts/sheet.md +223 -0
  221. package/src/skills/sixb-artifacts/slides.md +137 -0
  222. package/src/skills/sixb-memory/SKILL.md +68 -0
  223. package/src/skills/text-assets.d.ts +25 -0
  224. package/src/subagent-context.ts +182 -0
  225. package/src/subagent-continuation.ts +56 -0
  226. package/src/subagent-inbox.ts +101 -0
  227. package/src/subagent-tools.ts +1671 -0
  228. package/src/thread-context.ts +31 -0
  229. package/src/tools/attachments.ts +28 -0
  230. package/src/tools/bash.ts +233 -0
  231. package/src/tools/model-spec.ts +27 -0
  232. package/src/tools/read.ts +262 -0
  233. package/src/tools/result-media.ts +351 -0
  234. package/src/tools/result-output.ts +46 -0
  235. package/src/tools/tool-files.ts +204 -0
  236. package/src/tools/tool-outputs.ts +464 -0
  237. package/src/tools/view-file.ts +155 -0
  238. package/src/turn-runtime.ts +33 -0
  239. package/src/types.ts +91 -0
  240. package/src/workflow-input-attachments.ts +140 -0
@@ -0,0 +1,460 @@
1
+ import { posix } from "node:path"
2
+ import type { Sandbox } from "@sixb/core"
3
+ import type { ArtifactTheme } from "@sixb/core/artifacts"
4
+ import {
5
+ FRAME_ASSETS,
6
+ FRAME_DEFAULT_THEMES,
7
+ type FrameLayout,
8
+ injectFrameAssets,
9
+ type LayoutProblem,
10
+ layoutProbe,
11
+ rewriteFrameReferences,
12
+ } from "@sixb/core/internal/artifacts/frames"
13
+ import { type AgentSandboxHandle, contextPath } from "../../sandbox-handle"
14
+ import checkScript from "./check.cjs" with { type: "text" }
15
+
16
+ /** The check's library and each check's files, in the run's context directory. */
17
+ const LIBRARY = "lib/layout-check"
18
+ const CHECKS = "checks"
19
+ /** Chromium's start, a deck's fonts, and two passes over up to 100 slides, or a page at two widths. */
20
+ const CHECK_TIMEOUT_MS = 120_000
21
+ /**
22
+ * Sandboxes the check's library is written in. The context directory lasts the run, and each run
23
+ * has its own sandbox object, a lasting sandbox's included, so the library is written once per run.
24
+ */
25
+ const installed = new WeakSet<Sandbox>()
26
+
27
+ /** What the layout check found, for the publish tool to report beside the version. */
28
+ export type LayoutCheck =
29
+ | {
30
+ readonly status: "checked"
31
+ readonly warnings: readonly LayoutWarning[]
32
+ /** One per slide, or a page's at each width, as the agent names them. */
33
+ readonly screenshots: readonly string[]
34
+ }
35
+ | { readonly status: "skipped"; readonly reason: string }
36
+
37
+ export interface LayoutWarning {
38
+ readonly code: string
39
+ readonly message: string
40
+ /** The slide's number, or the width a page was checked at: `desktop` or `phone`. */
41
+ readonly part?: string
42
+ }
43
+
44
+ /** What the check lays out: a deck, a page, or a canvas's boards, each a page at its own size. */
45
+ export type CheckLayout = FrameLayout | "canvas"
46
+
47
+ /** A canvas's board, as the check opens it. */
48
+ export interface CheckedBoard {
49
+ readonly id: string
50
+ /** Its HTML file, relative to the version's directory. */
51
+ readonly file: string
52
+ readonly w: number
53
+ readonly h: number
54
+ readonly html: Uint8Array
55
+ }
56
+
57
+ interface CheckReport {
58
+ /** The layout the check laid the entry out in, which says it ran to the end of its setup. */
59
+ readonly layout?: CheckLayout
60
+ readonly slides?: number
61
+ readonly problems?: readonly CheckedProblem[]
62
+ readonly errors?: readonly string[]
63
+ readonly screenshots?: readonly string[]
64
+ /** Why the check stopped before it finished; what it found until then is above. */
65
+ readonly stopped?: string
66
+ readonly skipped?: string
67
+ readonly detail?: string
68
+ readonly failed?: string
69
+ }
70
+
71
+ /**
72
+ * What a page's `sixb.query(name)` reads in the check: the query route's JSON as it answers the
73
+ * requester, or why the query failed for them.
74
+ */
75
+ export type CheckedQuery = { readonly result: unknown } | { readonly error: string }
76
+
77
+ /** A width a page is checked at, as a laptop and a phone show it. */
78
+ export type PageView = "desktop" | "phone"
79
+
80
+ /**
81
+ * Lays a published deck, page, or canvas out in the sandbox's headless Chromium, as the app's
82
+ * frame does, and reports what a reader would find wrong: on each slide of a deck, on a page at a
83
+ * laptop's width and a phone's, or on each of a canvas's boards at its own size, with a screenshot
84
+ * of each for the Agent to look at. A sandbox without the agent image's browser skips it; so does
85
+ * a check that fails, which never costs the Agent its publish.
86
+ */
87
+ export async function checkLayout(
88
+ input: {
89
+ readonly handle: AgentSandboxHandle
90
+ /** The version's directory in the sandbox. */
91
+ readonly directory: string
92
+ /** The paths the version publishes, relative to the directory: the check serves no others. */
93
+ readonly files: readonly string[]
94
+ /** Names the check's directory, so each version's screenshots stay apart. */
95
+ readonly name: string
96
+ /** The version's `theme.json`, which the reader's frame applies over the app's theme. */
97
+ readonly theme?: ArtifactTheme
98
+ /** The version's queries by name, served where the runtime reads them opened on its own. */
99
+ readonly queries?: Readonly<Record<string, CheckedQuery>>
100
+ readonly signal: AbortSignal
101
+ /** Test seam; production allows 120 seconds. */
102
+ readonly timeoutMs?: number
103
+ } & (
104
+ | { readonly layout: FrameLayout; readonly entry: Uint8Array }
105
+ | { readonly layout: "canvas"; readonly boards: readonly CheckedBoard[] }
106
+ )
107
+ ): Promise<LayoutCheck> {
108
+ const { sandbox } = input.handle
109
+ const timeoutMs = input.timeoutMs ?? CHECK_TIMEOUT_MS
110
+ const library = contextPath(input.handle, LIBRARY)
111
+ const output = contextPath(input.handle, CHECKS, input.name)
112
+ // Each page to open, with the kit and the runtime as its frame serves them. The sandbox has no
113
+ // way to the API: what the version shows from other artifacts is a placeholder, and what its
114
+ // queries return is read beforehand.
115
+ const pages =
116
+ input.layout === "canvas"
117
+ ? input.boards.map((board) => ({
118
+ path: board.file,
119
+ layout: "page" as const,
120
+ html: board.html,
121
+ }))
122
+ : [{ path: "index.html", layout: input.layout, html: input.entry }]
123
+ const entries = pages.map((page) => ({
124
+ path: page.path,
125
+ contents: injectFrameAssets(
126
+ rewriteFrameReferences(new TextDecoder().decode(page.html), [], page.path),
127
+ page.layout,
128
+ page.path
129
+ ),
130
+ }))
131
+ await sandbox.writeFiles([
132
+ ...(installed.has(sandbox)
133
+ ? []
134
+ : [
135
+ { path: posix.join(library, "check.cjs"), contents: checkScript },
136
+ { path: posix.join(library, "deck.css"), contents: asset("__sixb/deck.css") },
137
+ { path: posix.join(library, "page.css"), contents: asset("__sixb/page.css") },
138
+ { path: posix.join(library, "runtime.js"), contents: asset("__sixb/runtime.js") },
139
+ { path: posix.join(library, "probe.js"), contents: `(${layoutProbe.toString()})` },
140
+ ]),
141
+ ...entries.map((entry) => ({
142
+ path: posix.join(output, "entries", entry.path),
143
+ contents: entry.contents,
144
+ })),
145
+ {
146
+ path: posix.join(output, "entries.json"),
147
+ contents: JSON.stringify(
148
+ Object.fromEntries(entries.map((entry) => [entry.path, posix.join("entries", entry.path)]))
149
+ ),
150
+ },
151
+ ...(input.layout === "canvas"
152
+ ? [
153
+ {
154
+ path: posix.join(output, "boards.json"),
155
+ contents: JSON.stringify(
156
+ input.boards.map(({ id, file, w, h }) => ({ id, file, w, h }))
157
+ ),
158
+ },
159
+ ]
160
+ : []),
161
+ { path: posix.join(output, "themes.json"), contents: JSON.stringify(deckThemes(input.theme)) },
162
+ { path: posix.join(output, "files.json"), contents: JSON.stringify(input.files) },
163
+ ...queryFiles(output, input.queries ?? {}),
164
+ ])
165
+ installed.add(sandbox)
166
+ // The check stops itself first, so it reports what it found and closes its browser, which a
167
+ // node killed at the timeout leaves running.
168
+ const deadlineMs = Math.floor((timeoutMs * 5) / 6)
169
+ const result = await sandbox.runCommand(
170
+ "bash",
171
+ [
172
+ "-lc",
173
+ 'command -v node >/dev/null 2>&1 || { echo \'{"skipped":"no_node"}\'; exit 0; }; exec node "$SIXB_CHECK/check.cjs" "$SIXB_CHECK_DECK" "$SIXB_CHECK_OUT" "$SIXB_CHECK" "$SIXB_CHECK_DEADLINE_MS" "$SIXB_CHECK_LAYOUT"',
174
+ ],
175
+ {
176
+ env: {
177
+ ...input.handle.env,
178
+ SIXB_CHECK: library,
179
+ SIXB_CHECK_DECK: input.directory,
180
+ SIXB_CHECK_OUT: output,
181
+ SIXB_CHECK_DEADLINE_MS: String(deadlineMs),
182
+ SIXB_CHECK_LAYOUT: input.layout,
183
+ },
184
+ timeout: timeoutMs,
185
+ signal: input.signal,
186
+ }
187
+ )
188
+ input.signal.throwIfAborted()
189
+ // A report counts however the command ended: a check that printed one and then hung on its way
190
+ // out still found what it found.
191
+ const report = parseReport(result.stdout)
192
+ if (report?.skipped) {
193
+ return {
194
+ status: "skipped",
195
+ reason:
196
+ report.skipped === "no_node"
197
+ ? "the sandbox has no Node.js"
198
+ : "the sandbox has no headless browser (the agent image has one)",
199
+ }
200
+ }
201
+ if (!report || report.failed || report.layout !== input.layout) {
202
+ if (result.timedOut) {
203
+ return { status: "skipped", reason: `it took longer than ${timeoutMs / 1000} seconds` }
204
+ }
205
+ const detail = report?.failed ?? result.stderr.trim()
206
+ console.warn(`[SixbAgent] The layout check failed: ${detail.slice(0, 500)}`)
207
+ return { status: "skipped", reason: "the check failed in the sandbox" }
208
+ }
209
+ const noun = { deck: "deck", page: "page", canvas: "canvas" }[input.layout]
210
+ const reached = { deck: "Slides", page: "Widths", canvas: "Boards" }[input.layout]
211
+ const checks = posix.join(input.handle.contextDirectory, CHECKS, input.name)
212
+ const screenshots = (report.screenshots ?? []).map((path) =>
213
+ posix.join(checks, posix.relative(output, path))
214
+ )
215
+ return {
216
+ status: "checked",
217
+ screenshots,
218
+ warnings: [
219
+ ...layoutWarnings(report.problems ?? []),
220
+ ...(report.errors ?? []).map((error) => ({
221
+ code: "layout_load_error",
222
+ message: `While the ${noun} loaded: ${error.slice(0, 200)}.`,
223
+ })),
224
+ ...(report.stopped === undefined
225
+ ? []
226
+ : [
227
+ {
228
+ code: "layout_incomplete",
229
+ message: `The check stopped early: ${report.stopped.slice(0, 200)}. ${reached} it did not reach were not checked.`,
230
+ },
231
+ ]),
232
+ ],
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Each query's answer as `queries/<name>.json`, and `queries.json`, the status each is served
238
+ * with: a failed query answers with its error, which the runtime rejects the page's promise with.
239
+ */
240
+ function queryFiles(
241
+ output: string,
242
+ queries: Readonly<Record<string, CheckedQuery>>
243
+ ): { path: string; contents: string }[] {
244
+ const entries = Object.entries(queries)
245
+ return [
246
+ ...entries.map(([name, query]) => ({
247
+ path: posix.join(output, "queries", `${name}.json`),
248
+ contents: JSON.stringify("error" in query ? { error: query.error } : query.result),
249
+ })),
250
+ {
251
+ path: posix.join(output, "queries.json"),
252
+ contents: JSON.stringify(
253
+ Object.fromEntries(entries.map(([name, query]) => [name, "error" in query ? 409 : 200]))
254
+ ),
255
+ },
256
+ ]
257
+ }
258
+
259
+ /**
260
+ * The themes a reader sees the deck in: Sixb's light and dark, or only the mode its `theme.json`
261
+ * pins, each with the deck's tokens over Sixb's.
262
+ */
263
+ function deckThemes(theme: ArtifactTheme | undefined) {
264
+ const modes = theme?.mode ? [theme.mode] : (["light", "dark"] as const)
265
+ return Object.fromEntries(
266
+ modes.map((mode) => {
267
+ const base = FRAME_DEFAULT_THEMES[mode]
268
+ return [mode, { ...base, tokens: { ...base.tokens, ...theme?.tokens } }]
269
+ })
270
+ )
271
+ }
272
+
273
+ function asset(path: string): string {
274
+ const file = FRAME_ASSETS[path]
275
+ if (!file) throw new Error(`[SixbAgent] The frame has no '${path}'.`)
276
+ return file.body
277
+ }
278
+
279
+ function parseReport(stdout: string): CheckReport | null {
280
+ const line = stdout.trim().split("\n").at(-1)
281
+ if (!line) return null
282
+ try {
283
+ const value: unknown = JSON.parse(line)
284
+ return typeof value === "object" && value !== null ? (value as CheckReport) : null
285
+ } catch {
286
+ return null
287
+ }
288
+ }
289
+
290
+ /** The most a check reports; the screenshots show the rest. */
291
+ const MAX_WARNINGS = 12
292
+ const SEVERITY: readonly LayoutProblem["code"][] = [
293
+ "broken_image",
294
+ "outside",
295
+ "wide",
296
+ "footer",
297
+ "overlap",
298
+ "clipped",
299
+ "contrast",
300
+ "small_target",
301
+ "small_text",
302
+ ]
303
+ const VIEWS: readonly PageView[] = ["desktop", "phone"]
304
+
305
+ /** A problem where the check found it: on a deck's slide, or on a page at one width. */
306
+ type CheckedProblem = LayoutProblem & {
307
+ readonly theme: "light" | "dark"
308
+ readonly slide?: number
309
+ readonly view?: PageView
310
+ /** The canvas's board it is on. */
311
+ readonly board?: string
312
+ }
313
+
314
+ /**
315
+ * The problems as warnings, worst first. A layout break is one warning per element; small text,
316
+ * a small control, and low contrast with one cause, the same element at the same size or in the
317
+ * same colors, is one warning naming every slide or width it is at, so a deck's eyebrow at 17px
318
+ * is one line, not nine. Contrast says which theme it fails in when it fails in one.
319
+ */
320
+ export function layoutWarnings(problems: readonly CheckedProblem[]): LayoutWarning[] {
321
+ const groups = new Map<
322
+ string,
323
+ {
324
+ problem: CheckedProblem
325
+ slides: Set<number>
326
+ views: Set<PageView>
327
+ boards: Set<string>
328
+ texts: Set<string>
329
+ themes: Set<string>
330
+ }
331
+ >()
332
+ for (const problem of problems) {
333
+ const at = problem.slide ?? problem.view ?? problem.board
334
+ const cause =
335
+ problem.code === "small_text" || problem.code === "small_target"
336
+ ? `${problem.selector}|${problem.size}`
337
+ : problem.code === "contrast"
338
+ ? `${problem.selector}|${problem.colors?.join("|")}`
339
+ : `${at}|${problem.selector}|${problem.text}|${problem.rect.x},${problem.rect.y}`
340
+ const key = `${problem.code}|${cause}`
341
+ const group = groups.get(key) ?? {
342
+ problem,
343
+ slides: new Set(),
344
+ views: new Set(),
345
+ boards: new Set(),
346
+ texts: new Set(),
347
+ themes: new Set(),
348
+ }
349
+ if (problem.slide !== undefined) group.slides.add(problem.slide)
350
+ if (problem.view) group.views.add(problem.view)
351
+ if (problem.board) group.boards.add(problem.board)
352
+ group.texts.add(problem.text)
353
+ group.themes.add(problem.theme)
354
+ groups.set(key, group)
355
+ }
356
+ // Boards in the order the check met them, as the canvas lists them.
357
+ const boardOrder = [
358
+ ...new Set(problems.flatMap((problem) => (problem.board ? [problem.board] : []))),
359
+ ]
360
+ const first = (group: { slides: Set<number>; views: Set<PageView>; boards: Set<string> }) =>
361
+ group.slides.size > 0
362
+ ? Math.min(...group.slides)
363
+ : group.boards.size > 0
364
+ ? Math.min(...[...group.boards].map((board) => boardOrder.indexOf(board)))
365
+ : Math.min(...[...group.views].map((view) => VIEWS.indexOf(view)))
366
+ const ordered = [...groups.values()].sort(
367
+ (a, b) =>
368
+ SEVERITY.indexOf(a.problem.code) - SEVERITY.indexOf(b.problem.code) || first(a) - first(b)
369
+ )
370
+ const warnings: LayoutWarning[] = ordered
371
+ .slice(0, MAX_WARNINGS)
372
+ .map(({ problem, slides, views, boards, texts, themes }) => {
373
+ const theme =
374
+ problem.code === "contrast" && themes.size === 1 ? ` in ${[...themes][0]} mode` : ""
375
+ const page = slides.size === 0
376
+ const onBoards = boardOrder.filter((board) => boards.has(board))
377
+ const where =
378
+ onBoards.length > 0
379
+ ? boardList(onBoards)
380
+ : page
381
+ ? viewList([...views])
382
+ : slideList([...slides])
383
+ return {
384
+ code: `layout_${problem.code}`,
385
+ part:
386
+ onBoards[0] ??
387
+ (page
388
+ ? (VIEWS.find((view) => views.has(view)) ?? "desktop")
389
+ : String(Math.min(...slides))),
390
+ message: `${where}: ${describeProblem(problem, [...texts], page)}${theme}.`,
391
+ }
392
+ })
393
+ const more = ordered.length - warnings.length
394
+ if (more > 0) {
395
+ warnings.push({
396
+ code: "layout_more",
397
+ message: `${more} more layout problem${more === 1 ? "" : "s"}; the screenshots show them.`,
398
+ })
399
+ }
400
+ return warnings
401
+ }
402
+
403
+ /** `Board 'region'`, `Boards 'welcome' and 'region'`. */
404
+ function boardList(boards: readonly string[]): string {
405
+ const named = boards.map((board) => `'${board}'`)
406
+ if (named.length === 1) return `Board ${named[0]}`
407
+ return `Boards ${named.slice(0, -1).join(", ")} and ${named.at(-1)}`
408
+ }
409
+
410
+ /** `Slide 3`, `Slides 1–4 and 7`. */
411
+ function slideList(slides: number[]): string {
412
+ const sorted = [...slides].sort((a, b) => a - b)
413
+ const runs: string[] = []
414
+ for (let index = 0; index < sorted.length; ) {
415
+ let end = index
416
+ while (end + 1 < sorted.length && sorted[end + 1] === (sorted[end] ?? 0) + 1) end++
417
+ runs.push(end > index ? `${sorted[index]}–${sorted[end]}` : String(sorted[index]))
418
+ index = end + 1
419
+ }
420
+ const label = runs.length > 1 ? `${runs.slice(0, -1).join(", ")} and ${runs.at(-1)}` : runs[0]
421
+ return `${sorted.length === 1 ? "Slide" : "Slides"} ${label}`
422
+ }
423
+
424
+ /** `On a phone (390px)`, `At 1280px and on a phone`. */
425
+ function viewList(views: PageView[]): string {
426
+ if (views.length > 1) return "At 1280px and on a phone (390px)"
427
+ return views[0] === "phone" ? "On a phone (390px)" : "At 1280px"
428
+ }
429
+
430
+ function describeProblem(problem: LayoutProblem, texts: readonly string[], page: boolean): string {
431
+ const { x, y, width, height } = problem.rect
432
+ const where = `at ${x},${y} (${width}×${height})`
433
+ const quoted = texts
434
+ .slice(0, 2)
435
+ .map((text) => `“${text}”`)
436
+ .join(", ")
437
+ const what = `${problem.selector} ${quoted}${texts.length > 2 ? ` and ${texts.length - 2} more` : ""}`
438
+ switch (problem.code) {
439
+ case "outside":
440
+ return `${what} runs past the slide's edge ${where}, which cuts it off`
441
+ case "footer":
442
+ return `${what} runs into the footer ${where}; keep content above the source line`
443
+ case "wide":
444
+ return `${what} runs past the screen's right edge ${where}, so the whole page scrolls sideways; let it wrap or shrink, or put it in a box that scrolls (.table-scroll)`
445
+ case "overlap":
446
+ return `${what} is drawn over “${problem.other}” ${where}`
447
+ case "clipped":
448
+ return `${what} is cut off by its own box ${where}`
449
+ case "small_text":
450
+ return page
451
+ ? `${what} ${texts.length === 1 ? "is" : "are"} ${problem.size}px; nothing on a page may be under 12px`
452
+ : `${what} ${texts.length === 1 ? "is" : "are"} ${problem.size}px; nothing on a slide may be under 18px`
453
+ case "small_target":
454
+ return `${what} ${texts.length === 1 ? "is" : "are"} ${problem.size}px across; make each control at least 24px each way`
455
+ case "contrast":
456
+ return `${what} ${texts.length === 1 ? "has" : "have"} a contrast of ${problem.ratio}:1 (${problem.colors?.join(" on ")}), under WCAG AA`
457
+ case "broken_image":
458
+ return `the image ${quoted} did not load ${where}`
459
+ }
460
+ }