AI-native design editor. Open-source Figma alternative.
fix(automation): wait for Portless MCP readiness
Danila Poyarkov5951f45d64e9
diff --git a/src/app/automation/bridge/vite-plugin.ts b/src/app/automation/bridge/vite-plugin.tsindex 66756dec9..c6e18c7a1 100644--- a/src/app/automation/bridge/vite-plugin.ts+++ b/src/app/automation/bridge/vite-plugin.ts@@ -50,6 +50,8 @@ export function createAutomationEnvironment(const MAX_CONFIGURATION_BYTES = 70_000const CHILD_EXIT_TIMEOUT_MS = 2_000+const CHILD_HEALTH_ATTEMPTS = 40+const CHILD_HEALTH_DELAY_MS = 50type DevMCPConfigurationErrorStatus = 400 | 413@@ -98,6 +100,27 @@ export async function readDevMCPConfiguration(request: IncomingMessage): Promise}}+export async function waitForAutomationHealth(+ browserURL: string,+ fetcher: typeof fetch = fetch+): Promise<void> {+ const healthURL = `${browserURL.replace(/^ws/, 'http')}/health`+ for (let attempt = 0; attempt < CHILD_HEALTH_ATTEMPTS; attempt++) {+ try {+ const response = await fetcher(healthURL)+ if (response.ok) return+ } catch (error) {+ if (attempt === CHILD_HEALTH_ATTEMPTS - 1) {+ console.warn(`[MCP] Health check failed at ${healthURL}`, error)+ }+ }+ await new Promise<void>((resolve) => {+ setTimeout(resolve, CHILD_HEALTH_DELAY_MS)+ })+ }+ throw new Error(`MCP server did not become healthy at ${healthURL}`)+}+interface AutomationPluginOptions {browserURL: stringcorsOrigin: string@@ -195,6 +218,8 @@ export function automationPlugin(if (code && code !== 0) console.error(`[MCP] Server exited with code ${code}`)if (child === spawned) child = null})++ await waitForAutomationHealth(options.browserURL)}async function restartChild(nextConfiguration: DevMCPConfiguration): Promise<void> {diff --git a/tests/engine/app/automation/mcp-vite-plugin.test.ts b/tests/engine/app/automation/mcp-vite-plugin.test.tsindex ecc8998dd..261bc45f2 100644--- a/tests/engine/app/automation/mcp-vite-plugin.test.ts+++ b/tests/engine/app/automation/mcp-vite-plugin.test.ts@@ -6,7 +6,8 @@ import {DevMCPConfigurationTooLargeError,createAutomationEnvironment,devMCPConfigurationErrorStatus,- readDevMCPConfiguration+ readDevMCPConfiguration,+ waitForAutomationHealth} from '@/app/automation/bridge/vite-plugin'import { parseDevMCPConfiguration } from '@/app/automation/mcp/dev-control'@@ -71,6 +72,21 @@ describe('MCP Vite development server', () => {})})+ test('waits through transient Portless responses until MCP is healthy', async () => {+ const statuses = [404, 404, 200]+ const requests: string[] = []+ await waitForAutomationHealth('wss://feature.mcp.open-pencil.localhost', async (input) => {+ requests.push(String(input))+ return new Response(null, { status: statuses.shift() ?? 500 })+ })++ expect(requests).toEqual([+ 'https://feature.mcp.open-pencil.localhost/health',+ 'https://feature.mcp.open-pencil.localhost/health',+ 'https://feature.mcp.open-pencil.localhost/health'+ ])+ })+test('classifies malformed and oversized configuration requests', async () => {const malformed = Readable.from(['{'])const malformedError = await readDevMCPConfiguration(malformed as never).catch(diff --git a/tests/engine/mcp/server/security.test.ts b/tests/engine/mcp/server/security.test.tsindex 0544b5144..1bb4004d3 100644--- a/tests/engine/mcp/server/security.test.ts+++ b/tests/engine/mcp/server/security.test.ts@@ -32,6 +32,42 @@ function testSocketPath(): string | null {return join(SOCKET_DIR, `mcp-test-${process.pid}-${++testCounter}.sock`)}+describe('MCP server CORS', () => {+ test('accepts authenticated health preflight from the configured worktree origin', async () => {+ const origin = 'https://feature.open-pencil.localhost'+ const handle = await startServer({+ httpPort: 0,+ withTcp: true,+ socketPath: testSocketPath(),+ authToken: TEST_AUTH_TOKEN,+ corsOrigin: origin,+ enableEval: false,+ mcpRoot: null+ })+ const httpPort = handle.httpPort+ if (!httpPort) {+ await handle.close()+ throw new Error('withTcp: true did not produce an HTTP port')+ }++ try {+ const response = await fetch(`http://127.0.0.1:${httpPort}/health`, {+ method: 'OPTIONS',+ headers: {+ origin,+ 'access-control-request-method': 'GET',+ 'access-control-request-headers': 'authorization'+ }+ })+ expect(response.status).toBe(204)+ expect(response.headers.get('access-control-allow-origin')).toBe(origin)+ expect(response.headers.get('access-control-allow-headers')).toContain('Authorization')+ } finally {+ await handle.close()+ }+ })+})+// ---------------------------------------------------------------------------// Auto-generated auth token// ---------------------------------------------------------------------------