@ggui-ai/ui-gen 0.10.0 → 0.11.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.
@@ -45,6 +45,26 @@ interface RunRenderCheckInput {
45
45
  readonly mockupProps: JsonObject;
46
46
  readonly contract?: DataContract;
47
47
  }
48
+ /**
49
+ * Run the in-loop runtime render check.
50
+ *
51
+ * Isolation dispatcher (#592): when the CALLING environment already
52
+ * owns a DOM (`window` + `document` on `globalThis` — e.g. a vitest
53
+ * `happy-dom` environment, where the environment owner installed the
54
+ * globals deliberately), the check runs in-process exactly as before.
55
+ * In a plain Node process it runs in an ISOLATED SUBPROCESS
56
+ * (`render-check-host.ts` → `render-check-worker.ts`), because the
57
+ * in-process path installs happy-dom globals on `globalThis`
58
+ * process-wide — a TOCTOU hazard for anything that sniffs `window`
59
+ * concurrently (provider SDKs throw "browser-like environment" at
60
+ * client construction; two overlapping checks corrupt each other's
61
+ * teardown). See #592 for the measured incident (4 bench cells hard-
62
+ * failed on the 2026-08-19 run).
63
+ *
64
+ * The host is loaded lazily so the worker bundle (which imports this
65
+ * module for `runRenderCheckInProcess`) never drags in the sandbox
66
+ * spawn machinery.
67
+ */
48
68
  declare function runRenderCheck(input: RunRenderCheckInput): Promise<RenderCheckResult>;
49
69
 
50
70
  export { type RenderCheckIssue as R, type RenderCheckResult as a, runRenderCheck as r };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ggui-ai/ui-gen",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "UI generation harness for ggui — the LLM system prompt, multi-turn coding loop, and evaluation engine that turn a DataContract into a React component. Implements the UiGenerator contract from @ggui-ai/mcp-server-core; consumed by @ggui-ai/mcp-server.",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -188,9 +188,9 @@
188
188
  "puppeteer-core": "^24.40.0 || ^25.0.0",
189
189
  "react": "^18.0.0 || ^19.0.0",
190
190
  "react-dom": "^18.0.0 || ^19.0.0",
191
- "@ggui-ai/mcp-server-core": "0.10.0",
192
- "@ggui-ai/project-config": "0.10.0",
193
- "@ggui-ai/protocol": "0.10.0"
191
+ "@ggui-ai/project-config": "0.11.0",
192
+ "@ggui-ai/mcp-server-core": "0.11.0",
193
+ "@ggui-ai/protocol": "0.11.0"
194
194
  },
195
195
  "peerDependenciesMeta": {
196
196
  "@google/adk": {
@@ -229,11 +229,11 @@
229
229
  "openai": "^6.29.0",
230
230
  "typescript": "^5.0.0",
231
231
  "zod": "^4.3.6",
232
- "@ggui-ai/gadgets": "0.10.0",
233
- "@ggui-ai/design": "0.10.0",
234
- "@ggui-ai/sandbox": "0.10.0",
235
- "@ggui-ai/ui-visual-tester": "0.10.0",
236
- "@ggui-ai/wire": "0.10.0"
232
+ "@ggui-ai/gadgets": "0.11.0",
233
+ "@ggui-ai/sandbox": "0.11.0",
234
+ "@ggui-ai/ui-visual-tester": "0.11.0",
235
+ "@ggui-ai/wire": "0.11.0",
236
+ "@ggui-ai/design": "0.11.0"
237
237
  },
238
238
  "devDependencies": {
239
239
  "@google/adk": "^0.5.0",
@@ -248,9 +248,9 @@
248
248
  "react-dom": "^19.0.0",
249
249
  "tsup": "^8.5.0",
250
250
  "vitest": "^3.2.6",
251
- "@ggui-ai/mcp-server-core": "0.10.0",
252
- "@ggui-ai/project-config": "0.10.0",
253
- "@ggui-ai/protocol": "0.10.0"
251
+ "@ggui-ai/mcp-server-core": "0.11.0",
252
+ "@ggui-ai/project-config": "0.11.0",
253
+ "@ggui-ai/protocol": "0.11.0"
254
254
  },
255
255
  "engines": {
256
256
  "node": ">=20.0.0"
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Host side of the isolated runtime render check (#592).
3
+ *
4
+ * Spawns `render-check-worker.ts` in a sandboxed subprocess and maps
5
+ * every sandbox outcome onto the check's own `RenderCheckResult`
6
+ * vocabulary, so callers see one result shape whether the check ran
7
+ * in-process (DOM-owning environments) or isolated (plain Node).
8
+ *
9
+ * Outcome mapping policy (mirrors the adapter's escaped-error
10
+ * doctrine — component faults are `failed`, harness faults are
11
+ * `unverified`, and nothing throws):
12
+ *
13
+ * - worker exit 0 + parseable verdict → the verdict, verbatim.
14
+ * - timeout → `failed` (runaway component: the
15
+ * in-process check has no comparable wall-clock bound, the
16
+ * subprocess finally gives us one).
17
+ * - spawn-error / overflow / non-zero exit / unparseable stdout
18
+ * → `unverified` (the harness could not
19
+ * run the check; never blame the component for our plumbing).
20
+ *
21
+ * Spawn resolution copies the proven `src/tools/render-check.ts`
22
+ * pattern: prefer the built `dist/.../render-check-worker.js`; in
23
+ * dev/test (vitest imports `src/`), fall back to `node --import tsx`
24
+ * on the `.ts` source. tsup inlines THIS module into several dist
25
+ * entries (`dist/index.js`, `dist/harness/index.js`,
26
+ * `dist/check/index.js`, `dist/advanced/index.js`, …), so the
27
+ * candidate list walks from each of those locations to the one real
28
+ * `dist/harness/check/runtime-render/render-check-worker.js`.
29
+ */
30
+ import { existsSync } from 'node:fs';
31
+ import { createRequire } from 'node:module';
32
+ import { fileURLToPath, pathToFileURL } from 'node:url';
33
+ import { runSandboxed } from '@ggui-ai/sandbox';
34
+ import type { SandboxResult } from '@ggui-ai/sandbox';
35
+ import type { RenderCheckResult, RunRenderCheckInput } from './render-check.js';
36
+
37
+ /**
38
+ * Wall-clock bound for one isolated check. The rich probe renders,
39
+ * clicks every declared action, and re-renders streams — slower than
40
+ * the smoke worker's 12s budget; 30s bounds runaway components while
41
+ * staying far under any generation-turn budget.
42
+ */
43
+ const CHECK_TIMEOUT_MS = 30_000;
44
+
45
+ /**
46
+ * stdout cap. The verdict JSON carries issue lists with diagnostics
47
+ * arrays — bigger than the smoke worker's one-liner, still tiny in
48
+ * absolute terms. 2 MiB is far above any legitimate verdict.
49
+ */
50
+ const CHECK_STDOUT_CAP = 2 * 1024 * 1024;
51
+
52
+ /**
53
+ * V8 heap cap. The worker realm loads react-dom, happy-dom and
54
+ * testing-library on top of the component itself; 512 MB leaves
55
+ * headroom while still bounding pathological allocation.
56
+ */
57
+ const CHECK_NODE_HEAP_MB = 512;
58
+
59
+ const WORKER_BASENAME = 'render-check-worker';
60
+
61
+ function resolveWorkerSpawn(): { command: string; args: string[] } {
62
+ const jsCandidates = [
63
+ // Sibling — when import.meta.url is already .../harness/check/runtime-render/.
64
+ new URL(`./${WORKER_BASENAME}.js`, import.meta.url),
65
+ // From dist/index.js.
66
+ new URL(`./harness/check/runtime-render/${WORKER_BASENAME}.js`, import.meta.url),
67
+ // From dist/harness/index.js.
68
+ new URL(`./check/runtime-render/${WORKER_BASENAME}.js`, import.meta.url),
69
+ // From dist/check/index.js, dist/advanced/index.js, dist/<flat>.js.
70
+ new URL(`../harness/check/runtime-render/${WORKER_BASENAME}.js`, import.meta.url),
71
+ ];
72
+ for (const jsUrl of jsCandidates) {
73
+ const jsPath = fileURLToPath(jsUrl);
74
+ if (existsSync(jsPath)) {
75
+ return { command: process.execPath, args: [jsPath] };
76
+ }
77
+ }
78
+ // Dev / vitest: only the .ts source exists — run it through tsx.
79
+ const tsCandidates = [
80
+ new URL(`./${WORKER_BASENAME}.ts`, import.meta.url),
81
+ new URL(`../harness/check/runtime-render/${WORKER_BASENAME}.ts`, import.meta.url),
82
+ ];
83
+ for (const tsUrl of tsCandidates) {
84
+ const tsPath = fileURLToPath(tsUrl);
85
+ if (existsSync(tsPath)) {
86
+ const require_ = createRequire(import.meta.url);
87
+ const tsxLoader = pathToFileURL(require_.resolve('tsx')).href;
88
+ return { command: process.execPath, args: ['--import', tsxLoader, tsPath] };
89
+ }
90
+ }
91
+ const tried = [...jsCandidates, ...tsCandidates]
92
+ .map((u) => fileURLToPath(u))
93
+ .join(', ');
94
+ throw new Error(
95
+ `render-check: isolated worker not found at any of: ${tried}. Did \`pnpm build\` run?`,
96
+ );
97
+ }
98
+
99
+ function failed(reason: string, t0: number): RenderCheckResult {
100
+ return {
101
+ ok: false,
102
+ issues: [{ check: 'render-no-throw', outcome: 'failed', reason }],
103
+ stats: { actionsChecked: 0, streamsChecked: 0, renderMs: Date.now() - t0 },
104
+ };
105
+ }
106
+
107
+ function unverified(reason: string, t0: number): RenderCheckResult {
108
+ return {
109
+ ok: false,
110
+ issues: [{ check: 'render-no-throw', outcome: 'unverified', reason }],
111
+ stats: { actionsChecked: 0, streamsChecked: 0, renderMs: Date.now() - t0 },
112
+ };
113
+ }
114
+
115
+ /**
116
+ * Map one sandbox result onto a `RenderCheckResult`. Exported for the
117
+ * unit pins — the spawn itself is exercised by the integration test.
118
+ */
119
+ export function mapSandboxResultToCheckResult(
120
+ result: SandboxResult,
121
+ t0: number,
122
+ ): RenderCheckResult {
123
+ if (result.outcome === 'timeout') {
124
+ return failed(
125
+ `render check timed out after ${CHECK_TIMEOUT_MS}ms in the isolated worker (likely infinite loop or runaway effect in the component).`,
126
+ t0,
127
+ );
128
+ }
129
+ if (result.outcome === 'overflow-stdout' || result.outcome === 'overflow-stderr') {
130
+ return failed(
131
+ `render check worker produced excessive output (${result.outcome}) — the component is likely in a pathological logging or error loop.`,
132
+ t0,
133
+ );
134
+ }
135
+ if (result.outcome === 'spawn-error') {
136
+ return unverified(
137
+ `render check worker failed to start — ${result.errorMessage}`,
138
+ t0,
139
+ );
140
+ }
141
+ if (result.outcome === 'canceled') {
142
+ return unverified('render check worker was canceled before completing.', t0);
143
+ }
144
+ if (result.outcome !== 'exit') {
145
+ return unverified(
146
+ `render check worker ended with unexpected sandbox outcome '${result.outcome}'.`,
147
+ t0,
148
+ );
149
+ }
150
+ if (result.exitCode !== 0) {
151
+ const tail = result.stderr.trim() || result.stdout.trim();
152
+ return unverified(
153
+ `render check worker exited ${result.exitCode}${tail ? ` — ${tail.slice(0, 600)}` : ''}`,
154
+ t0,
155
+ );
156
+ }
157
+ const stdout = result.stdout.trim();
158
+ if (stdout.length === 0) {
159
+ return unverified('render check worker exited without producing a verdict.', t0);
160
+ }
161
+ try {
162
+ return JSON.parse(stdout) as RenderCheckResult;
163
+ } catch (err) {
164
+ return unverified(
165
+ `render check worker verdict was not valid JSON — ${
166
+ err instanceof Error ? err.message : String(err)
167
+ }`,
168
+ t0,
169
+ );
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Run the render check in an isolated subprocess. See module doc.
175
+ */
176
+ export async function runRenderCheckViaWorker(
177
+ input: RunRenderCheckInput,
178
+ ): Promise<RenderCheckResult> {
179
+ const t0 = Date.now();
180
+ let spawn: { command: string; args: string[] };
181
+ try {
182
+ spawn = resolveWorkerSpawn();
183
+ } catch (err) {
184
+ return unverified(err instanceof Error ? err.message : String(err), t0);
185
+ }
186
+
187
+ const result = await runSandboxed({
188
+ command: spawn.command,
189
+ args: spawn.args,
190
+ timeoutMs: CHECK_TIMEOUT_MS,
191
+ maxStdoutBytes: CHECK_STDOUT_CAP,
192
+ nodeHeapMb: CHECK_NODE_HEAP_MB,
193
+ stdin: JSON.stringify(input),
194
+ // NODE_ENV steers React's production vs development build — keep
195
+ // parity with the caller; everything else stays on the sandbox's
196
+ // default allowlist.
197
+ env: { NODE_ENV: process.env.NODE_ENV ?? 'production' },
198
+ });
199
+
200
+ return mapSandboxResultToCheckResult(result, t0);
201
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Isolated worker for the in-loop runtime render check (#592).
3
+ *
4
+ * Runs `runRenderCheckInProcess` in a SUBPROCESS so the check's
5
+ * happy-dom global install (`window`, `document`, `navigator`, … onto
6
+ * `globalThis`) lands on THIS process's globals, never the caller's.
7
+ *
8
+ * Why this exists — the process-global install is a TOCTOU hazard in
9
+ * any concurrent host: provider SDKs (OpenAI and Anthropic alike)
10
+ * sniff `typeof window !== 'undefined' && window.document && navigator`
11
+ * at client construction and refuse with "browser-like environment"
12
+ * when another cell's check-window happens to be open (#592, the
13
+ * 2026-08-19 bench run's gpt-5.6-sol hard failures). Two overlapping
14
+ * in-process checks also corrupt each other's install/teardown. A
15
+ * subprocess gives one private global scope per check plus real kill
16
+ * semantics via `@ggui-ai/sandbox` on the host side.
17
+ *
18
+ * Wire protocol (single JSON document each way):
19
+ *
20
+ * stdin: RunRenderCheckInput ({ sourceCode, mockupProps, contract? })
21
+ * stdout: RenderCheckResult ({ ok, issues, stats })
22
+ * exit: 0 on any verdict (issues are data, not status); non-zero
23
+ * only when the worker itself is broken (malformed input,
24
+ * harness crash) — the host maps that to an `unverified`
25
+ * issue, mirroring the adapter's escaped-error semantics.
26
+ *
27
+ * Same file-path distribution model as `src/tools/render-check-worker.ts`
28
+ * (the render SMOKE worker): reached by spawn path, never by import
29
+ * specifier; registered as a tsup entry so `dist/harness/check/
30
+ * runtime-render/render-check-worker.js` ships.
31
+ */
32
+ import { runRenderCheckInProcess } from './render-check.js';
33
+ import type { RunRenderCheckInput } from './render-check.js';
34
+
35
+ async function readAllStdin(): Promise<string> {
36
+ const chunks: Buffer[] = [];
37
+ for await (const chunk of process.stdin) {
38
+ chunks.push(chunk as Buffer);
39
+ }
40
+ return Buffer.concat(chunks).toString('utf8');
41
+ }
42
+
43
+ function emit(value: unknown): void {
44
+ process.stdout.write(`${JSON.stringify(value)}\n`);
45
+ }
46
+
47
+ async function main(): Promise<void> {
48
+ const raw = await readAllStdin();
49
+ let input: RunRenderCheckInput;
50
+ try {
51
+ input = JSON.parse(raw) as RunRenderCheckInput;
52
+ } catch (err) {
53
+ process.stderr.write(
54
+ `render-check worker: malformed input JSON — ${
55
+ err instanceof Error ? err.message : String(err)
56
+ }\n`,
57
+ );
58
+ process.exitCode = 1;
59
+ return;
60
+ }
61
+ if (typeof input.sourceCode !== 'string' || input.sourceCode.length === 0) {
62
+ process.stderr.write(
63
+ 'render-check worker: input.sourceCode must be a non-empty string\n',
64
+ );
65
+ process.exitCode = 1;
66
+ return;
67
+ }
68
+
69
+ // The in-process check never throws for component failures (they are
70
+ // reported as issues); an escape here is a harness defect — let it
71
+ // crash the worker so the host maps it to `unverified` with the
72
+ // stderr tail as the reason.
73
+ const result = await runRenderCheckInProcess(input);
74
+ emit(result);
75
+ // The check's async-infra grace period (React scheduler drain) holds
76
+ // handlers for ~100ms; exit through the event loop naturally so that
77
+ // drain completes inside the worker instead of leaking crashes.
78
+ }
79
+
80
+ void main();
@@ -114,8 +114,48 @@ export interface RunRenderCheckInput {
114
114
  // Public entry
115
115
  // ─────────────────────────────────────────────────────────────────────────────
116
116
 
117
+ /**
118
+ * Run the in-loop runtime render check.
119
+ *
120
+ * Isolation dispatcher (#592): when the CALLING environment already
121
+ * owns a DOM (`window` + `document` on `globalThis` — e.g. a vitest
122
+ * `happy-dom` environment, where the environment owner installed the
123
+ * globals deliberately), the check runs in-process exactly as before.
124
+ * In a plain Node process it runs in an ISOLATED SUBPROCESS
125
+ * (`render-check-host.ts` → `render-check-worker.ts`), because the
126
+ * in-process path installs happy-dom globals on `globalThis`
127
+ * process-wide — a TOCTOU hazard for anything that sniffs `window`
128
+ * concurrently (provider SDKs throw "browser-like environment" at
129
+ * client construction; two overlapping checks corrupt each other's
130
+ * teardown). See #592 for the measured incident (4 bench cells hard-
131
+ * failed on the 2026-08-19 run).
132
+ *
133
+ * The host is loaded lazily so the worker bundle (which imports this
134
+ * module for `runRenderCheckInProcess`) never drags in the sandbox
135
+ * spawn machinery.
136
+ */
117
137
  export async function runRenderCheck(
118
138
  input: RunRenderCheckInput,
139
+ ): Promise<RenderCheckResult> {
140
+ const g = hostGlobals();
141
+ if ("window" in g && "document" in g) {
142
+ return runRenderCheckInProcess(input);
143
+ }
144
+ const { runRenderCheckViaWorker } = await import("./render-check-host.js");
145
+ return runRenderCheckViaWorker(input);
146
+ }
147
+
148
+ /**
149
+ * The actual check, executed IN THE CURRENT PROCESS — installs
150
+ * happy-dom globals on `globalThis` for the duration when the
151
+ * environment doesn't already provide a DOM. Call sites outside this
152
+ * module should use {@link runRenderCheck}; this export exists for the
153
+ * isolation worker (which IS the private process the globals may own)
154
+ * and for DOM-owning test environments that want to bypass the
155
+ * dispatcher explicitly.
156
+ */
157
+ export async function runRenderCheckInProcess(
158
+ input: RunRenderCheckInput,
119
159
  ): Promise<RenderCheckResult> {
120
160
  const t0 = Date.now();
121
161
  const issues: RenderCheckIssue[] = [];