Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix: make the SDK sandbox E2E prove the provider it claims to exercise
Alfredo Lopezda5bdb016a2c
diff --git a/Makefile b/Makefileindex 1a7625624..e4b9efa91 100644--- a/Makefile+++ b/Makefile@@ -1136,11 +1136,34 @@ test-e2e-plugin-workload-sdk-desktop: ## Run the explicit opt-in Electron/WebContest -n "$${CONTROL_API_BASE_URL:-}" || { echo "CONTROL_API_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \test -n "$${EXTERNAL_REST_API_BASE_URL:-}" || { echo "EXTERNAL_REST_API_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \test -n "$${RPC_PROXY_BASE_URL:-}" || { echo "RPC_PROXY_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \+ test -n "$${E2E_PLUGIN_SDK_EXPECT_PROVIDER:-}" || { echo "Refusing Desktop Plugin Workload SDK E2E: set E2E_PLUGIN_SDK_EXPECT_PROVIDER to the provider this run must exercise (openai | claude | codex-subscription)" >&2; exit 1; }; \. scripts/e2e/e2e-lib.sh; \require_branch_profile_urls "$$ctx" "$${CLERUM_PROFILE_PORTS_ENV:-$${E2E_PROFILE_PORTS_ENV:-}}"; \- echo "[E2E-GUARD] Desktop Plugin Workload SDK gate: context=$$ctx; URLs supplied by the branch profile"; \+ echo "[E2E-GUARD] Desktop Plugin Workload SDK gate: context=$$ctx; expected provider=$${E2E_PLUGIN_SDK_EXPECT_PROVIDER}; URLs supplied by the branch profile"; \cd desktop-app && KUBECONTEXT="$$ctx" E2E_K8S_CONTEXT="$$ctx" E2E_PLUGIN_SDK_DESKTOP=1 npm run build && \- KUBECONTEXT="$$ctx" E2E_K8S_CONTEXT="$$ctx" E2E_PLUGIN_SDK_DESKTOP=1 ./node_modules/.bin/playwright test --config test/e2e-playwright/playwright.config.ts plugin-workload-sdk-sandbox-ui.spec.ts+ KUBECONTEXT="$$ctx" E2E_K8S_CONTEXT="$$ctx" E2E_PLUGIN_SDK_DESKTOP=1 E2E_PLUGIN_SDK_EXPECT_PROVIDER="$$E2E_PLUGIN_SDK_EXPECT_PROVIDER" ./node_modules/.bin/playwright test --config test/e2e-playwright/playwright.config.ts plugin-workload-sdk-sandbox-ui.spec.ts++.PHONY: test-e2e-plugin-workload-sdk-desktop-no-grant+test-e2e-plugin-workload-sdk-desktop-no-grant: ## Run the no-grant Codex guard (#533 regression) against its own ungranted SDK-only recipe+ @set -eu; \+ ctx="$${KUBECONTEXT:-$(E2E_KUBECONTEXT)}"; \+ test -n "$$ctx" || { echo "Refusing Desktop Plugin Workload SDK no-grant guard: explicit Kubernetes context is required" >&2; exit 1; }; \+ test "$${E2E_PLUGIN_SDK_WRITE_CONFIRM:-}" = 1 || { echo "Refusing Desktop Plugin Workload SDK no-grant guard: set E2E_PLUGIN_SDK_WRITE_CONFIRM=1" >&2; exit 1; }; \+ test -n "$${CONTROL_UI_BASE_URL:-}" || { echo "CONTROL_UI_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \+ test -n "$${CONTROL_API_BASE_URL:-}" || { echo "CONTROL_API_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \+ test -n "$${EXTERNAL_REST_API_BASE_URL:-}" || { echo "EXTERNAL_REST_API_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \+ test -n "$${RPC_PROXY_BASE_URL:-}" || { echo "RPC_PROXY_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \+ test -n "$${E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAME:-}" || { echo "Refusing Desktop Plugin Workload SDK no-grant guard: set E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAME to a Codex SDK-only recipe whose execution binding is missing (never the granted happy-path recipe)" >&2; exit 1; }; \+ test -n "$${E2E_PLUGIN_SDK_NO_GRANT_APP_TITLE:-}" || { echo "Refusing Desktop Plugin Workload SDK no-grant guard: set E2E_PLUGIN_SDK_NO_GRANT_APP_TITLE to the ungranted app's Desktop catalog title" >&2; exit 1; }; \+ . scripts/e2e/e2e-lib.sh; \+ require_branch_profile_urls "$$ctx" "$${CLERUM_PROFILE_PORTS_ENV:-$${E2E_PROFILE_PORTS_ENV:-}}"; \+ echo "[E2E-GUARD] Desktop Plugin Workload SDK no-grant guard: context=$$ctx; recipe=$${E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAMESPACE:-sandbox-recipes}/$${E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAME}; URLs supplied by the branch profile"; \+ cd desktop-app && KUBECONTEXT="$$ctx" E2E_K8S_CONTEXT="$$ctx" E2E_PLUGIN_SDK_DESKTOP=1 E2E_PLUGIN_SDK_NO_GRANT=1 npm run build && \+ KUBECONTEXT="$$ctx" E2E_K8S_CONTEXT="$$ctx" E2E_PLUGIN_SDK_DESKTOP=1 E2E_PLUGIN_SDK_NO_GRANT=1 \+ E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAME="$$E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAME" \+ E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAMESPACE="$${E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAMESPACE:-sandbox-recipes}" \+ E2E_PLUGIN_SDK_NO_GRANT_APP_TITLE="$$E2E_PLUGIN_SDK_NO_GRANT_APP_TITLE" \+ ./node_modules/.bin/playwright test --config test/e2e-playwright/playwright.config.ts plugin-workload-sdk-no-grant-guard.spec.ts.PHONY: test-e2e-cron-tab-validationtest-e2e-cron-tab-validation: ## Run recipe cron tab E2E (set E2E_CRON_TAB_FIX_REQUIRED=1 when the fix must be present)diff --git a/desktop-app/test/e2e-playwright/plugin-workload-sdk-no-grant-guard.spec.ts b/desktop-app/test/e2e-playwright/plugin-workload-sdk-no-grant-guard.spec.tsnew file mode 100644index 000000000..ef1a68bb0--- /dev/null+++ b/desktop-app/test/e2e-playwright/plugin-workload-sdk-no-grant-guard.spec.ts@@ -0,0 +1,177 @@+import { type ElectronApplication, type Page, expect, test } from '@playwright/test'+import {+ type EmbeddedContents,+ type SandboxUiFixture,+ activateEmbedded,+ assertSandboxUiFixture,+ assertSteplessSdkRecipePrecondition,+ embeddedText,+ findSandboxUiContents,+ latestSdkInvocationStatus,+ promptBridgeLedgerForRun,+ readPluginWorkloadSdkStatus,+ sandboxUiViewUrlPrefix,+ sdkInvocationCount,+ typeEmbedded,+} from './pluginWorkloadSdkSandboxUi'+import {+ EXTERNAL_REST_API_BASE_URL,+ RPC_PROXY_BASE_URL,+ assertAllowedTarget,+ desktopCredentials,+ finalizeRecording,+ launchDesktopApp,+ login,+ requireRecorderConfirm,+} from './qa-recorder-helpers'++/*+ * E2E_GUARDIAN_IPC_FLOW: Desktop Apps opens the Plugin Workload SDK sandbox UI+ * through the main-process WebContentsView and EmbeddedContents bridge. The+ * renderer has no HTTP response to await for that transition; the recipe+ * status, native view title, and the absence of a Codex provider attempt in+ * the persisted ledger are the visible/business signals.+ *+ * Regression guard for issue #533: an SDK-only Codex recipe whose execution+ * binding is missing must refuse the prompt in the embedded UI and must never+ * reach the Codex provider or record a spend.+ *+ * This journey REQUIRES `status.pluginWorkloadSdk.state === 'awaiting_policy'`,+ * the exact opposite of the granted happy path in+ * plugin-workload-sdk-sandbox-ui.spec.ts. The two therefore run against+ * separate recipes, from separate Make targets, and this file refuses to start+ * if it is pointed at the granted fixture.+ */+const RUN_ENABLED =+ process.env.E2E_PLUGIN_SDK_DESKTOP === '1' && process.env.E2E_PLUGIN_SDK_NO_GRANT === '1'++test.skip(+ !RUN_ENABLED,+ 'Set E2E_PLUGIN_SDK_DESKTOP=1 and E2E_PLUGIN_SDK_NO_GRANT=1 for the no-grant Codex guard.'+)++/** Recipe identity of the granted happy path, used only to reject a collision. */+function grantedRecipeIdentity(): string {+ const name = process.env.E2E_PLUGIN_SDK_RECIPE_NAME || 'evenfire-prompt-notify-app'+ const namespace = process.env.E2E_PLUGIN_SDK_RECIPE_NAMESPACE || 'sandbox-recipes'+ return `${namespace}/${name}`+}++/**+ * Resolve the ungranted fixture from its own variables. There is deliberately+ * no default recipe: the guard needs a recipe that is missing its Codex+ * execution binding, and silently borrowing the granted recipe would turn this+ * regression test into a guaranteed failure (or, worse, into a pass that+ * proves nothing about the no-grant path).+ *+ * Built inside the test rather than at module scope so a misconfigured value+ * fails this journey loudly instead of aborting collection of the whole+ * Desktop Playwright suite.+ */+function ungrantedFixture(): SandboxUiFixture {+ const recipeName = (process.env.E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAME ?? '').trim()+ if (!recipeName) {+ throw new Error(+ 'E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAME is required: install a Codex SDK-only recipe whose ' ++ 'execution binding is missing. This guard must never share the granted happy-path recipe.'+ )+ }+ const appTitle = (process.env.E2E_PLUGIN_SDK_NO_GRANT_APP_TITLE ?? '').trim()+ if (!appTitle) {+ throw new Error(+ 'E2E_PLUGIN_SDK_NO_GRANT_APP_TITLE is required: the ungranted app must be distinguishable ' ++ 'from the granted app in the Desktop Apps catalog.'+ )+ }+ const fixture = assertSandboxUiFixture(+ {+ recipeName,+ recipeNamespace: process.env.E2E_PLUGIN_SDK_NO_GRANT_RECIPE_NAMESPACE || 'sandbox-recipes',+ appTitle,+ },+ 'E2E_PLUGIN_SDK_NO_GRANT'+ )+ const identity = `${fixture.recipeNamespace}/${fixture.recipeName}`+ if (identity === grantedRecipeIdentity()) {+ throw new Error(+ `Refusing to run the no-grant guard against the granted happy-path recipe ${identity}. ` ++ 'The two journeys require opposite pluginWorkloadSdk states on the same object.'+ )+ }+ return fixture+}++test('Desktop Apps refuses a Codex prompt when the execution binding is missing', async ({}, testInfo) => {+ requireRecorderConfirm(+ 'E2E_PLUGIN_SDK_WRITE_CONFIRM',+ 'This journey submits one promptBridge request that must be refused before any provider dispatch.'+ )+ const fixture = ungrantedFixture()+ // Hard precondition, not a skip: this guard exists only for Codex, so a+ // non-Codex fixture means the regression was never exercised. A skipped+ // guard is indistinguishable from a passing one in the lane summary.+ assertSteplessSdkRecipePrecondition(fixture, 'codex-subscription')+ const sdkStatus = readPluginWorkloadSdkStatus(fixture)+ expect(sdkStatus.state).not.toBe('validated')+ expect(sdkStatus.state).toBe('awaiting_policy')+ expect(sdkStatus.message).toMatch(+ /codex_execution_binding_missing|grant_missing|policy is not ready/i+ )+ await assertAllowedTarget('EXTERNAL_REST_API_BASE_URL', EXTERNAL_REST_API_BASE_URL)+ await assertAllowedTarget('RPC_PROXY_BASE_URL', RPC_PROXY_BASE_URL)++ let app: ElectronApplication | undefined+ let page: Page | undefined+ try {+ const credentials = desktopCredentials()+ const launched = await launchDesktopApp(testInfo)+ app = launched.app+ page = launched.page+ await login(page, credentials)+ await page.getByTestId('nav-sandbox-ui').click()+ await expect(page.getByRole('heading', { name: 'Apps', exact: true })).toBeVisible()+ const appCard = page+ .getByRole('main')+ .getByRole('button', { name: `Open ${fixture.appTitle}`, exact: true })+ // Exactly one card: a title shared with the granted app would make this+ // locator ambiguous and the guard could open the wrong sandbox UI.+ await expect(appCard).toHaveCount(1)+ await expect(appCard).toBeVisible({ timeout: 30_000 })+ await appCard.click()+ await expect(page.getByRole('button', { name: 'Back to apps' })).toBeVisible({+ timeout: 30_000,+ })+ let embedded: EmbeddedContents | null = null+ await expect+ .poll(async () => {+ embedded = await findSandboxUiContents(app!, fixture)+ return embedded?.url ?? ''+ })+ .toContain(sandboxUiViewUrlPrefix(fixture))+ const webContentsId = embedded!.id+ const runStartedAt = new Date().toISOString()+ const before = sdkInvocationCount(fixture, 'promptBridge')+ await typeEmbedded(app, webContentsId, '#prompt', 'This prompt must not dispatch Codex.')+ await activateEmbedded(app, webContentsId, '#run')+ await expect+ .poll(() => embeddedText(app!, webContentsId, '#prompt-out'), { timeout: 60_000 })+ .toMatch(+ /provider_unavailable|codex_execution_binding_missing|policy is not ready|not authorized/i+ )+ const promptResult = await embeddedText(app, webContentsId, '#prompt-out')+ expect(promptResult).not.toMatch(/Running…/)+ const after = sdkInvocationCount(fixture, 'promptBridge')+ const ledger = promptBridgeLedgerForRun(fixture, runStartedAt)+ expect(ledger.codexAttemptId).toBe('')+ expect(ledger.spendOutcome).not.toBe('exact')+ expect(after).toBeGreaterThanOrEqual(before)+ expect(after).toBeLessThanOrEqual(before + 1)+ if (after > before) {+ expect(latestSdkInvocationStatus(fixture, 'promptBridge')).not.toBe('complete')+ }+ await page.getByRole('button', { name: 'Back to apps' }).click()+ await expect(page.getByRole('heading', { name: 'Apps', exact: true })).toBeVisible()+ } finally {+ await finalizeRecording(app, page)+ }+})diff --git a/desktop-app/test/e2e-playwright/plugin-workload-sdk-sandbox-ui.spec.ts b/desktop-app/test/e2e-playwright/plugin-workload-sdk-sandbox-ui.spec.tsindex ac10f62fa..fb740c5d1 100644--- a/desktop-app/test/e2e-playwright/plugin-workload-sdk-sandbox-ui.spec.ts+++ b/desktop-app/test/e2e-playwright/plugin-workload-sdk-sandbox-ui.spec.ts@@ -1,6 +1,28 @@-import { type ElectronApplication, type Page, type TestInfo, expect, test } from '@playwright/test'-import { execFileSync } from 'node:child_process'-import { writeFileSync } from 'node:fs'+import { type ElectronApplication, type Page, expect, test } from '@playwright/test'+import {+ type EmbeddedContents,+ type SandboxUiFixture,+ activateEmbedded,+ assertSandboxUiFixture,+ assertSteplessSdkRecipePrecondition,+ attachVisualLayout,+ captureDesktopWindow,+ captureEmbeddedView,+ embeddedOptions,+ embeddedRect,+ embeddedText,+ embeddedValue,+ findSandboxUiContents,+ latestSdkInvocationStatus,+ notificationDeliverySignal,+ promptBridgeLedgerForRun,+ readPluginWorkloadSdkStatus,+ requireExpectedSdkProvider,+ sandboxUiViewUrlPrefix,+ sdkInvocationCount,+ selectEmbeddedRecipient,+ typeEmbedded,+} from './pluginWorkloadSdkSandboxUi'import {EXTERNAL_REST_API_BASE_URL,RPC_PROXY_BASE_URL,@@ -12,10 +34,6 @@ import {openSettings,requireRecorderConfirm,} from './qa-recorder-helpers'-import {- profilesSql,- sqlLiteral,-} from './third-party-authn-first-party-mcphost/workflowApprovalJourney'/** E2E_GUARDIAN_IPC_FLOW: Desktop Apps opens the Plugin Workload SDK sandbox UI@@ -23,11 +41,13 @@ import {* renderer has no HTTP response to await for that transition; the recipe* precondition, native view title, and persisted promptBridge ledger are the* visible/business signals.+ *+ * This file owns ONLY the validated (granted) happy path. The no-grant Codex+ * guard lives in plugin-workload-sdk-no-grant-guard.spec.ts with its own+ * recipe: the two journeys assert mutually exclusive cluster state on+ * `status.pluginWorkloadSdk.state`, so they must never share a fixture.*/const RUN_ENABLED = process.env.E2E_PLUGIN_SDK_DESKTOP === '1'-const APP_TITLE = process.env.E2E_PLUGIN_SDK_APP_TITLE || 'Prompt & Notify'-const RECIPE_NAME = process.env.E2E_PLUGIN_SDK_RECIPE_NAME || 'evenfire-prompt-notify-app'-const RECIPE_NAMESPACE = process.env.E2E_PLUGIN_SDK_RECIPE_NAMESPACE || 'sandbox-recipes'const CAPTURE_VISUALS = process.env.E2E_PLUGIN_SDK_CAPTURE === '1'test.skip(@@ -35,503 +55,20 @@ test.skip('Set E2E_PLUGIN_SDK_DESKTOP=1 for the branch-owned Electron/Apps/WebContentsView gate.')-type EmbeddedContents = { id: number; url: string }-type Rect = { x: number; y: number; width: number; height: number }-type EmbeddedOption = { value: string; label: string }-type NativeLayout = {- windowBounds: Rect- viewBounds: Rect | null- displayScaleFactor: number- mainRendererDpr: number- embeddedRendererDpr: number | null- embeddedCaptureSize: { width: number; height: number } | null-}--function assertSteplessSdkRecipePrecondition(): { provider: string; model: string } {- const context =- process.env.E2E_K8S_CONTEXT || process.env.KUBECONTEXT || process.env.K8S_CONTEXT || ''- if (!context) throw new Error('E2E_K8S_CONTEXT is required for the stepless recipe precondition.')- if (!/^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/.test(RECIPE_NAME)) {- throw new Error(`Unsafe E2E_PLUGIN_SDK_RECIPE_NAME: ${RECIPE_NAME}`)- }- if (!/^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/.test(RECIPE_NAMESPACE)) {- throw new Error(`Unsafe E2E_PLUGIN_SDK_RECIPE_NAMESPACE: ${RECIPE_NAMESPACE}`)- }- const raw = execFileSync(- 'kubectl',- [- '--context',- context,- '-n',- RECIPE_NAMESPACE,- 'get',- 'workflowrecipe',- RECIPE_NAME,- '-o',- 'json',- ],- { encoding: 'utf8', timeout: 30_000 }- )- const recipe = JSON.parse(raw) as {- spec?: {- steps?: unknown- triggers?: unknown- agent?: { provider?: unknown; model?: unknown }- workloads?: unknown- pluginWorkloadSdk?: { promptBridge?: Record<string, unknown> }- }- }- const spec = recipe.spec ?? {}- if (spec.steps !== undefined || spec.triggers !== undefined) {- throw new Error(- 'Desktop candidate must be SDK-only: spec.steps and spec.triggers must be absent.'- )- }- if (- typeof spec.agent?.provider !== 'string' ||- !spec.agent.provider ||- typeof spec.agent.model !== 'string' ||- !spec.agent.model- ) {- throw new Error('Desktop candidate must declare a resolvable spec.agent bootstrap.')- }- if (!['openai', 'claude', 'codex-subscription'].includes(spec.agent.provider.toLowerCase())) {- throw new Error(- `Desktop T3 candidate must use OpenAI, Claude, or Codex; received provider ${spec.agent.provider}.`- )- }- if (!Array.isArray(spec.workloads) || spec.workloads.length === 0) {- throw new Error('Desktop candidate must declare at least one plugin workload.')- }- if (- typeof spec.pluginWorkloadSdk?.promptBridge !== 'object' ||- spec.pluginWorkloadSdk.promptBridge === null- ) {- throw new Error('Desktop candidate must declare the pluginWorkloadSdk.promptBridge object.')- }- return { provider: spec.agent.provider, model: spec.agent.model }-}--type PluginWorkloadSdkStatus = {- state: string- bootstrapContractVersion: number | null- message: string-}--function readPluginWorkloadSdkStatus(): PluginWorkloadSdkStatus {- const context =- process.env.E2E_K8S_CONTEXT || process.env.KUBECONTEXT || process.env.K8S_CONTEXT || ''- if (!context) throw new Error('E2E_K8S_CONTEXT is required for the Plugin Workload SDK status.')- const raw = execFileSync(- 'kubectl',- [- '--context',- context,- '-n',- RECIPE_NAMESPACE,- 'get',- 'workflowrecipe',- RECIPE_NAME,- '-o',- 'json',- ],- { encoding: 'utf8', timeout: 30_000 }- )- const recipe = JSON.parse(raw) as {- status?: {- pluginWorkloadSdk?: {- state?: unknown- bootstrapContractVersion?: unknown- message?: unknown- }- }- }- const sdk = recipe.status?.pluginWorkloadSdk ?? {}- return {- state: typeof sdk.state === 'string' ? sdk.state : '',- bootstrapContractVersion:- sdk.bootstrapContractVersion === 2 || sdk.bootstrapContractVersion === 3- ? sdk.bootstrapContractVersion- : null,- message: typeof sdk.message === 'string' ? sdk.message : '',- }-}--async function findPromptNotifyContents(- app: ElectronApplication-): Promise<EmbeddedContents | null> {- return app.evaluate(async ({ webContents }, expectedTitle) => {- for (const contents of webContents.getAllWebContents()) {- if (contents.isDestroyed()) continue- const url = contents.getURL()- if (!url.includes('/api/v1/sandbox-ui/') || !url.includes('/view/')) continue- const title = await contents.executeJavaScript('document.title').catch(() => '')- if (title === expectedTitle) return { id: contents.id, url }- }- return null- }, APP_TITLE)-}--async function embeddedRect(- app: ElectronApplication,- webContentsId: number,- selector: string-): Promise<Rect | null> {- return app.evaluate(- async ({ webContents }, args) => {- const contents = webContents.fromId(args.webContentsId)- if (!contents || contents.isDestroyed()) return null- const script = `(() => {- const el = document.querySelector(${JSON.stringify(args.selector)});- if (!el) return null;- const r = el.getBoundingClientRect();- const style = getComputedStyle(el);- if (r.width <= 0 || r.height <= 0 || style.visibility === 'hidden' || style.display === 'none') return null;- return { x: r.x, y: r.y, width: r.width, height: r.height };- })()`- return contents.executeJavaScript(script)- },- { webContentsId, selector }- )-}--async function embeddedText(- app: ElectronApplication,- webContentsId: number,- selector: string-): Promise<string> {- return app.evaluate(- async ({ webContents }, args) => {- const contents = webContents.fromId(args.webContentsId)- if (!contents || contents.isDestroyed()) return ''- const script = `document.querySelector(${JSON.stringify(args.selector)})?.textContent ?? ''`- return String(await contents.executeJavaScript(script))- },- { webContentsId, selector }- )-}--async function embeddedValue(- app: ElectronApplication,- webContentsId: number,- selector: string-): Promise<string> {- return app.evaluate(- async ({ webContents }, args) => {- const contents = webContents.fromId(args.webContentsId)- if (!contents || contents.isDestroyed()) return ''- const script = `String(document.querySelector(${JSON.stringify(args.selector)})?.value ?? '')`- return String(await contents.executeJavaScript(script))- },- { webContentsId, selector }- )-}--async function embeddedOptions(- app: ElectronApplication,- webContentsId: number,- selector: string-): Promise<EmbeddedOption[]> {- return app.evaluate(- async ({ webContents }, args) => {- const contents = webContents.fromId(args.webContentsId)- if (!contents || contents.isDestroyed()) return []- const script = `Array.from(document.querySelector(${JSON.stringify(args.selector)})?.options ?? []).map((option) => ({ value: option.value, label: option.textContent ?? '' }))`- return (await contents.executeJavaScript(script)) as EmbeddedOption[]- },- { webContentsId, selector }- )-}--async function embeddedActiveControl(- app: ElectronApplication,- webContentsId: number,- selector: string-): Promise<boolean> {- return app.evaluate(- async ({ webContents }, args) => {- const contents = webContents.fromId(args.webContentsId)- if (!contents || contents.isDestroyed()) return false- const script = `document.activeElement?.matches(${JSON.stringify(args.selector)}) === true`- return Boolean(await contents.executeJavaScript(script))- },- { webContentsId, selector }- )-}--async function embeddedActiveSignature(- app: ElectronApplication,- webContentsId: number-): Promise<string> {- return app.evaluate(async ({ webContents }, webContentsId) => {- const contents = webContents.fromId(webContentsId)- if (!contents || contents.isDestroyed()) return 'destroyed'- return String(- await contents.executeJavaScript(`(() => {- const el = document.activeElement;- if (!el) return 'none';- return [el.tagName, el.id || '', el.getAttribute('name') || ''].join('#');- })()`)- )- }, webContentsId)-}-/**- * Capture the actual native WebContentsView, which is composited above the- * renderer and therefore is not guaranteed to appear in page.screenshot().- * The data URL crosses the Electron boundary without exposing browser state- * or using a DOM shortcut; the resulting PNG is a Playwright test artifact.+ * Resolve the granted fixture. Built inside the test rather than at module+ * scope so a misconfigured value fails this journey loudly instead of aborting+ * collection of the whole Desktop Playwright suite.*/-async function captureEmbeddedView(- app: ElectronApplication,- webContentsId: number,- outputPath: string-): Promise<void> {- const dataUrl = await app.evaluate(async ({ webContents }, id) => {- const contents = webContents.fromId(id)- if (!contents || contents.isDestroyed()) throw new Error('Embedded WebContentsView closed')- const image = await contents.capturePage()- return image.toDataURL()- }, webContentsId)- const encoded = dataUrl.match(/^data:image\/png;base64,(.+)$/)?.[1]- if (!encoded) throw new Error('Electron returned a non-PNG sandbox UI capture')- writeFileSync(outputPath, Buffer.from(encoded, 'base64'))-}--/** Capture the BrowserWindow renderer surface for comparison with the native child capture. */-async function captureDesktopWindow(app: ElectronApplication, outputPath: string): Promise<void> {- const dataUrl = await app.evaluate(async ({ BrowserWindow }) => {- const window = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0]- if (!window || window.isDestroyed()) throw new Error('Desktop BrowserWindow closed')- const image = await window.capturePage()- return image.toDataURL()- })- const encoded = dataUrl.match(/^data:image\/png;base64,(.+)$/)?.[1]- if (!encoded) throw new Error('Electron returned a non-PNG desktop window capture')- writeFileSync(outputPath, Buffer.from(encoded, 'base64'))-}--/** Read the native child-view bounds for a geometry comparison artifact. */-async function nativeEmbeddedLayout(- app: ElectronApplication,- webContentsId: number-): Promise<NativeLayout> {- return app.evaluate(async ({ BrowserWindow, screen }, id) => {- const window = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0]- if (!window || window.isDestroyed()) throw new Error('Desktop BrowserWindow closed')- const child = window.contentView.children.find(view => view.webContents?.id === id)- const windowBounds = window.getBounds()- const display = screen.getDisplayMatching(windowBounds)- const mainRendererDpr = Number(- await window.webContents.executeJavaScript('window.devicePixelRatio')- )- const embeddedRendererDpr = child- ? Number(await child.webContents.executeJavaScript('window.devicePixelRatio'))- : null- const embeddedCaptureSize = child ? (await child.webContents.capturePage()).getSize() : null- return {- windowBounds,- viewBounds: child?.getBounds() ?? null,- displayScaleFactor: display.scaleFactor,- mainRendererDpr,- embeddedRendererDpr,- embeddedCaptureSize,- }- }, webContentsId)-}--async function attachVisualLayout(- page: Page,- app: ElectronApplication,- webContentsId: number,- testInfo: TestInfo,- stage: string-): Promise<void> {- const rendererRect = await page.locator('.sandbox-ui-embed-slot').boundingBox()- const nativeLayout = await nativeEmbeddedLayout(app, webContentsId)- await testInfo.attach(`sandbox-ui-layout-${stage}`, {- body: JSON.stringify({ stage, rendererRect, nativeLayout }, null, 2),- contentType: 'application/json',- })- console.log(`[sandbox-ui-visual] ${JSON.stringify({ stage, rendererRect, nativeLayout })}`)-}--async function sendEmbeddedKey(- app: ElectronApplication,- webContentsId: number,- keyCode: string-): Promise<void> {- await app.evaluate(- ({ webContents }, args) => {- const contents = webContents.fromId(args.webContentsId)- if (!contents || contents.isDestroyed()) throw new Error('Embedded WebContentsView closed')- contents.focus()- contents.sendInputEvent({ type: 'keyDown', keyCode: args.keyCode })- if (args.keyCode === 'Enter') {- contents.sendInputEvent({ type: 'char', keyCode: '\r' })- }- contents.sendInputEvent({ type: 'keyUp', keyCode: args.keyCode })- },- { webContentsId, keyCode }- )-}--async function focusEmbeddedControl(- app: ElectronApplication,- webContentsId: number,- selector: string-): Promise<void> {- // WebContentsView controls below the fold cannot be clicked with coordinates- // returned by getBoundingClientRect(): Chromium drops pointer events whose- // target point is outside the view. Tab traversal is the native keyboard- // path a user takes and scrolls each focused control into view without DOM- // focus()/scrollIntoView() shortcuts.- for (let attempt = 0; attempt < 16; attempt += 1) {- if (await embeddedActiveControl(app, webContentsId, selector)) return- const activeBefore = await embeddedActiveSignature(app, webContentsId)- await sendEmbeddedKey(app, webContentsId, 'Tab')- await expect- .poll(() => embeddedActiveSignature(app, webContentsId), {- timeout: 2_000,- intervals: [10, 25, 50],- })- .not.toBe(activeBefore)- }- throw new Error(`Embedded control ${selector} did not receive native keyboard focus.`)-}--async function typeEmbedded(- app: ElectronApplication,- webContentsId: number,- selector: string,- value: string-): Promise<void> {- await focusEmbeddedControl(app, webContentsId, selector)- await app.evaluate(- ({ webContents }, args) => {- const contents = webContents.fromId(args.webContentsId)- if (!contents || contents.isDestroyed()) throw new Error('Embedded WebContentsView closed')- contents.focus()- for (const character of args.value) {- contents.sendInputEvent({ type: 'char', keyCode: character })- }+function grantedFixture(): SandboxUiFixture {+ return assertSandboxUiFixture(+ {+ recipeName: process.env.E2E_PLUGIN_SDK_RECIPE_NAME || 'evenfire-prompt-notify-app',+ recipeNamespace: process.env.E2E_PLUGIN_SDK_RECIPE_NAMESPACE || 'sandbox-recipes',+ appTitle: process.env.E2E_PLUGIN_SDK_APP_TITLE || 'Prompt & Notify',},- { webContentsId, value }+ 'E2E_PLUGIN_SDK')- await expect.poll(() => embeddedValue(app, webContentsId, selector)).toBe(value)-}--async function activateEmbedded(- app: ElectronApplication,- webContentsId: number,- selector: string-): Promise<void> {- await focusEmbeddedControl(app, webContentsId, selector)- await sendEmbeddedKey(app, webContentsId, 'Enter')-}--async function selectEmbeddedRecipient(- app: ElectronApplication,- webContentsId: number,- selector: string,- expectedEmail: string-): Promise<EmbeddedOption> {- const options = await embeddedOptions(app, webContentsId, selector)- const normalizedExpectedEmail = expectedEmail.trim().toLowerCase()- const recipient = options.find(- option => option.value && option.label.trim().toLowerCase() === normalizedExpectedEmail- )- if (!recipient) {- throw new Error(- `Embedded recipient picker ${selector} has no option for authenticated user ${expectedEmail}.`- )- }- const recipientIndex = options.findIndex(option => option.value === recipient.value)- if (recipientIndex < 0) {- throw new Error(`Embedded recipient picker ${selector} lost the authenticated-user option.`)- }-- // This is the native select path a user takes: focus the control, move from- // the first option to the authenticated user's granted recipient, and commit- // with Enter. The option order is not part of the contract, so the number of- // ArrowDown events is derived from the live option list rather than assumed.- // We inspect the option label only to verify the UI displays a human handle;- // the value is never injected into the DOM or sent directly to the backend.- await focusEmbeddedControl(app, webContentsId, selector)- await sendEmbeddedKey(app, webContentsId, 'Home')- for (let index = 0; index < recipientIndex; index += 1) {- await sendEmbeddedKey(app, webContentsId, 'ArrowDown')- }- await sendEmbeddedKey(app, webContentsId, 'Enter')- await expect.poll(() => embeddedValue(app, webContentsId, selector)).toBe(recipient.value)- expect(recipient.label.trim().toLowerCase()).toBe(normalizedExpectedEmail)- expect(recipient.label).not.toMatch(/^[0-9a-f-]{36}$/i)- return recipient-}--function sdkInvocationCount(method: 'promptBridge' | 'clientNotifications'): number {- const raw = profilesSql(`- SELECT count(*)::int- FROM plugin_workload_sdk_invocations- WHERE recipe_namespace = ${sqlLiteral(RECIPE_NAMESPACE)}- AND recipe_name = ${sqlLiteral(RECIPE_NAME)}- AND method = ${sqlLiteral(method)};- `)- return Number.parseInt(raw, 10) || 0-}--function promptBridgeLedgerForRun(startedAt: string): {- invocationId: string- sdkAttemptId: string- codexAttemptId: string- spendOutcome: string-} {- const raw = profilesSql(`- SELECT inv.id::text || '|' || sdk.id::text || '|' ||- coalesce(codex.id::text, '') || '|' || coalesce(spend.outcome, '')- FROM plugin_workload_sdk_invocations inv- JOIN plugin_workload_sdk_provider_attempts sdk- ON sdk.invocation_id = inv.id- LEFT JOIN llm_provider_attempts codex- ON codex.plugin_workload_sdk_provider_attempt_id = sdk.id- LEFT JOIN plugin_workload_sdk_spend_outcomes spend- ON spend.provider_attempt_id = sdk.id- WHERE inv.recipe_namespace = ${sqlLiteral(RECIPE_NAMESPACE)}- AND inv.recipe_name = ${sqlLiteral(RECIPE_NAME)}- AND inv.method = 'promptBridge'- AND inv.created_at >= ${sqlLiteral(startedAt)}::timestamptz- ORDER BY inv.created_at ASC, sdk.attempt_index ASC- LIMIT 1;- `)- const [invocationId, sdkAttemptId, codexAttemptId, spendOutcome] = raw.trim().split('|')- return {- invocationId: invocationId ?? '',- sdkAttemptId: sdkAttemptId ?? '',- codexAttemptId: codexAttemptId ?? '',- spendOutcome: spendOutcome ?? '',- }-}--function latestSdkInvocationStatus(method: 'promptBridge' | 'clientNotifications'): string {- return profilesSql(`- SELECT status- FROM plugin_workload_sdk_invocations- WHERE recipe_namespace = ${sqlLiteral(RECIPE_NAMESPACE)}- AND recipe_name = ${sqlLiteral(RECIPE_NAME)}- AND method = ${sqlLiteral(method)}- ORDER BY created_at DESC- LIMIT 1;- `)-}--function notificationDeliverySignal(notificationId: string): string {- return profilesSql(`- SELECT event_type || '|' || status- FROM notification_deliveries- WHERE event_type = 'plugin_workload_sdk.notification'- AND payload->>'notificationId' = ${sqlLiteral(notificationId)}- LIMIT 1;- `)}test('Desktop Apps executes promptBridge and clientNotifications inside the real WebContentsView', async ({}, testInfo) => {@@ -539,16 +76,23 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the real'E2E_PLUGIN_SDK_WRITE_CONFIRM','This journey performs one paid promptBridge call and sends one test notification.')+ const fixture = grantedFixture()+ // The provider under test is declared by the operator and then asserted+ // against the live recipe. Without it a Codex lane could report green while+ // an OpenAI recipe served every prompt, because both providers satisfy the+ // generic SDK-only precondition.+ const expectedProvider = requireExpectedSdkProvider('E2E_PLUGIN_SDK_EXPECT_PROVIDER')await assertAllowedTarget('EXTERNAL_REST_API_BASE_URL', EXTERNAL_REST_API_BASE_URL)await assertAllowedTarget('RPC_PROXY_BASE_URL', RPC_PROXY_BASE_URL)// Read-only setup assertion. Every functional action that follows is through// the real Desktop UI and its native WebContentsView.- const recipeAgent = assertSteplessSdkRecipePrecondition()+ assertSteplessSdkRecipePrecondition(fixture, expectedProvider)+ const requiresCodex = expectedProvider === 'codex-subscription'await expect- .poll(() => readPluginWorkloadSdkStatus().state, { timeout: 180_000 })+ .poll(() => readPluginWorkloadSdkStatus(fixture).state, { timeout: 180_000 }).toBe('validated')- if (recipeAgent.provider.toLowerCase() === 'codex-subscription') {- expect(readPluginWorkloadSdkStatus().bootstrapContractVersion).toBe(3)+ if (requiresCodex) {+ expect(readPluginWorkloadSdkStatus(fixture).bootstrapContractVersion).toBe(3)}let app: ElectronApplication | undefined@@ -588,7 +132,7 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the real// ambiguous global locator.const appCard = page.getByRole('main')- .getByRole('button', { name: `Open ${APP_TITLE}`, exact: true })+ .getByRole('button', { name: `Open ${fixture.appTitle}`, exact: true })await expect(appCard).toHaveCount(1)await expect(appCard).toBeVisible({ timeout: 30_000 })await appCard.click()@@ -599,12 +143,10 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the reallet embedded: EmbeddedContents | null = nullawait expect.poll(async () => {- embedded = await findPromptNotifyContents(app!)+ embedded = await findSandboxUiContents(app!, fixture)return embedded?.url ?? ''})- .toContain(- `/api/v1/sandbox-ui/${encodeURIComponent(RECIPE_NAMESPACE)}/${encodeURIComponent(RECIPE_NAME)}/view/`- )+ .toContain(sandboxUiViewUrlPrefix(fixture))const webContentsId = embedded!.idawait expect.poll(() => embeddedRect(app!, webContentsId, '#run')).not.toBeNull()@@ -626,12 +168,12 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the realconst runStartedAt = new Date().toISOString()await test.step('reject an empty prompt without creating an invocation', async () => {- const before = sdkInvocationCount('promptBridge')+ const before = sdkInvocationCount(fixture, 'promptBridge')await activateEmbedded(app!, webContentsId, '#run')await expect.poll(() => embeddedText(app!, webContentsId, '#prompt-out')).toContain('Enter a prompt first.')- expect(sdkInvocationCount('promptBridge')).toBe(before)+ expect(sdkInvocationCount(fixture, 'promptBridge')).toBe(before)})await test.step('run the real prompt through the embedded UI', async () => {@@ -641,7 +183,7 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the real'#prompt',`Reply with a short confirmation for test marker ${marker}.`)- const before = sdkInvocationCount('promptBridge')+ const before = sdkInvocationCount(fixture, 'promptBridge')await activateEmbedded(app!, webContentsId, '#run')await expect.poll(() => embeddedText(app!, webContentsId, '#prompt-out'), {@@ -653,19 +195,22 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the real.poll(() => embeddedText(app!, webContentsId, '#prompt-out'), { timeout: 120_000 }).not.toMatch(/^(?:|Running…|Enter a prompt first\.)$/)await expect- .poll(() => sdkInvocationCount('promptBridge'), { timeout: 30_000 })+ .poll(() => sdkInvocationCount(fixture, 'promptBridge'), { timeout: 30_000 }).toBe(before + 1)await expect- .poll(() => latestSdkInvocationStatus('promptBridge'), { timeout: 30_000 })+ .poll(() => latestSdkInvocationStatus(fixture, 'promptBridge'), { timeout: 30_000 }).toBe('complete')})const promptResult = await embeddedText(app, webContentsId, '#prompt-out')expect(promptResult).not.toMatch(/"error"|provider_unavailable|requires a resolvable agent/i)- const ledger = promptBridgeLedgerForRun(runStartedAt)+ const ledger = promptBridgeLedgerForRun(fixture, runStartedAt)expect(ledger.invocationId).toMatch(/^[0-9a-f-]{36}$/i)expect(ledger.sdkAttemptId).toMatch(/^[0-9a-f-]{36}$/i)expect(ledger.sdkAttemptId).not.toBe(ledger.invocationId)- if (recipeAgent.provider.toLowerCase() === 'codex-subscription') {+ if (requiresCodex) {+ // Unconditional for a declared Codex run: a linked llm_provider_attempts+ // row and an exact spend outcome are the only proof the prompt was+ // actually dispatched to Codex rather than to another granted provider.expect(ledger.codexAttemptId).toMatch(/^[0-9a-f-]{36}$/i)expect(ledger.codexAttemptId).not.toBe(ledger.sdkAttemptId)expect(ledger.spendOutcome).toBe('exact')@@ -691,12 +236,12 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the realawait selectEmbeddedRecipient(app, webContentsId, '#userRef', credentials.email)await typeEmbedded(app, webContentsId, '#title', `Evenfire E2E ${marker}`)await test.step('reject a notification missing its body before the SDK call', async () => {- const before = sdkInvocationCount('clientNotifications')+ const before = sdkInvocationCount(fixture, 'clientNotifications')await activateEmbedded(app!, webContentsId, '#notify')await expect.poll(() => embeddedText(app!, webContentsId, '#notify-out')).toContain('Title and body are required.')- expect(sdkInvocationCount('clientNotifications')).toBe(before)+ expect(sdkInvocationCount(fixture, 'clientNotifications')).toBe(before)})await typeEmbedded(app,@@ -720,7 +265,7 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the real)?.[1]expect(notificationId).toBeTruthy()await expect- .poll(() => latestSdkInvocationStatus('clientNotifications'), { timeout: 30_000 })+ .poll(() => latestSdkInvocationStatus(fixture, 'clientNotifications'), { timeout: 30_000 }).toMatch(/^(accepted|delivered)$/)await expect.poll(() => notificationDeliverySignal(notificationId!), { timeout: 30_000 })@@ -765,81 +310,7 @@ test('Desktop Apps executes promptBridge and clientNotifications inside the real// Renderer chrome + native view both remain alive after the two backend// operations, proving the Desktop Apps path rather than a direct proxy call.await expect(page.getByRole('button', { name: 'Back to apps' })).toBeVisible()- expect(await findPromptNotifyContents(app)).toMatchObject({ id: webContentsId })- await page.getByRole('button', { name: 'Back to apps' }).click()- await expect(page.getByRole('heading', { name: 'Apps', exact: true })).toBeVisible()- } finally {- await finalizeRecording(app, page)- }-})--test('Desktop Apps refuses a Codex prompt when the execution binding is missing', async ({}, testInfo) => {- test.skip(- process.env.E2E_PLUGIN_SDK_NO_GRANT !== '1',- 'Set E2E_PLUGIN_SDK_NO_GRANT=1 to exercise the no-grant Codex guard.'- )- const recipeAgent = assertSteplessSdkRecipePrecondition()- test.skip(- recipeAgent.provider.toLowerCase() !== 'codex-subscription',- 'No-grant guard requires a Codex SDK-only recipe.'- )- const sdkStatus = readPluginWorkloadSdkStatus()- expect(sdkStatus.state).not.toBe('validated')- expect(sdkStatus.state).toBe('awaiting_policy')- expect(sdkStatus.message).toMatch(- /codex_execution_binding_missing|grant_missing|policy is not ready/i- )- await assertAllowedTarget('EXTERNAL_REST_API_BASE_URL', EXTERNAL_REST_API_BASE_URL)- await assertAllowedTarget('RPC_PROXY_BASE_URL', RPC_PROXY_BASE_URL)-- let app: ElectronApplication | undefined- let page: Page | undefined- try {- const credentials = desktopCredentials()- const launched = await launchDesktopApp(testInfo)- app = launched.app- page = launched.page- await login(page, credentials)- await page.getByTestId('nav-sandbox-ui').click()- await expect(page.getByRole('heading', { name: 'Apps', exact: true })).toBeVisible()- const appCard = page- .getByRole('main')- .getByRole('button', { name: `Open ${APP_TITLE}`, exact: true })- await expect(appCard).toBeVisible({ timeout: 30_000 })- await appCard.click()- await expect(page.getByRole('button', { name: 'Back to apps' })).toBeVisible({- timeout: 30_000,- })- let embedded: EmbeddedContents | null = null- await expect- .poll(async () => {- embedded = await findPromptNotifyContents(app!)- return embedded?.url ?? ''- })- .toContain(- `/api/v1/sandbox-ui/${encodeURIComponent(RECIPE_NAMESPACE)}/${encodeURIComponent(RECIPE_NAME)}/view/`- )- const webContentsId = embedded!.id- const runStartedAt = new Date().toISOString()- const before = sdkInvocationCount('promptBridge')- await typeEmbedded(app, webContentsId, '#prompt', 'This prompt must not dispatch Codex.')- await activateEmbedded(app, webContentsId, '#run')- await expect- .poll(() => embeddedText(app!, webContentsId, '#prompt-out'), { timeout: 60_000 })- .toMatch(- /provider_unavailable|codex_execution_binding_missing|policy is not ready|not authorized/i- )- const promptResult = await embeddedText(app, webContentsId, '#prompt-out')- expect(promptResult).not.toMatch(/Running…/)- const after = sdkInvocationCount('promptBridge')- const ledger = promptBridgeLedgerForRun(runStartedAt)- expect(ledger.codexAttemptId).toBe('')- expect(ledger.spendOutcome).not.toBe('exact')- expect(after).toBeGreaterThanOrEqual(before)- expect(after).toBeLessThanOrEqual(before + 1)- if (after > before) {- expect(latestSdkInvocationStatus('promptBridge')).not.toBe('complete')- }+ expect(await findSandboxUiContents(app, fixture)).toMatchObject({ id: webContentsId })await page.getByRole('button', { name: 'Back to apps' }).click()await expect(page.getByRole('heading', { name: 'Apps', exact: true })).toBeVisible()} finally {diff --git a/desktop-app/test/e2e-playwright/pluginWorkloadSdkSandboxUi.ts b/desktop-app/test/e2e-playwright/pluginWorkloadSdkSandboxUi.tsnew file mode 100644index 000000000..bf51358c0--- /dev/null+++ b/desktop-app/test/e2e-playwright/pluginWorkloadSdkSandboxUi.ts@@ -0,0 +1,598 @@+import { type ElectronApplication, type Page, type TestInfo, expect } from '@playwright/test'+import { execFileSync } from 'node:child_process'+import { writeFileSync } from 'node:fs'+import {+ profilesSql,+ sqlLiteral,+} from './third-party-authn-first-party-mcphost/workflowApprovalJourney'++/*+ * Shared Plugin Workload SDK sandbox-UI journey helpers.+ *+ * Both Desktop SDK specs (the paid happy path and the no-grant Codex guard)+ * drive the same Electron/WebContentsView surface, but they REQUIRE opposite+ * cluster state: `pluginWorkloadSdk.state === 'validated'` for one and+ * `awaiting_policy` for the other. Keeping the recipe identity in module-level+ * constants made that impossible to express — a single process could only ever+ * point at one recipe, so enabling both tests guaranteed one failure.+ *+ * Every helper here therefore takes an explicit `SandboxUiFixture` instead of+ * reading module constants, and each spec owns its own fixture (and its own+ * Make target). No default recipe is baked in on the no-grant side: an unset+ * fixture must fail loudly, never silently borrow the happy-path recipe.+ */++/** Recipe identity plus the Desktop Apps catalog title of one sandbox-UI app. */+export type SandboxUiFixture = {+ recipeName: string+ recipeNamespace: string+ appTitle: string+}++export type EmbeddedContents = { id: number; url: string }+export type Rect = { x: number; y: number; width: number; height: number }+export type EmbeddedOption = { value: string; label: string }+export type NativeLayout = {+ windowBounds: Rect+ viewBounds: Rect | null+ displayScaleFactor: number+ mainRendererDpr: number+ embeddedRendererDpr: number | null+ embeddedCaptureSize: { width: number; height: number } | null+}++export type PluginWorkloadSdkStatus = {+ state: string+ bootstrapContractVersion: number | null+ message: string+}++/**+ * Providers a Desktop SDK candidate recipe may declare. The lane is shared:+ * `scripts/e2e/seed-e2e-data.sh` provisions OpenAI/Claude demo grants by+ * default, so restricting the whole spec to Codex would break the seeded local+ * fixture. The run's expected provider is declared by the operator instead+ * (see `requireExpectedSdkProvider`) and asserted against the live recipe.+ */+export const SUPPORTED_SDK_PROVIDERS = ['openai', 'claude', 'codex-subscription'] as const+export type SupportedSdkProvider = (typeof SUPPORTED_SDK_PROVIDERS)[number]++const DNS_1123_LABEL = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/++export function k8sContext(): string {+ const context =+ process.env.E2E_K8S_CONTEXT || process.env.KUBECONTEXT || process.env.K8S_CONTEXT || ''+ if (!context) throw new Error('E2E_K8S_CONTEXT is required for the Plugin Workload SDK lane.')+ return context+}++/**+ * Validate a fixture supplied by the operator. `envPrefix` names the variable+ * family the values came from so a rejected value points at the exact knob.+ */+export function assertSandboxUiFixture(+ fixture: SandboxUiFixture,+ envPrefix: string+): SandboxUiFixture {+ if (!DNS_1123_LABEL.test(fixture.recipeName)) {+ throw new Error(`Unsafe ${envPrefix}_RECIPE_NAME: ${fixture.recipeName}`)+ }+ if (!DNS_1123_LABEL.test(fixture.recipeNamespace)) {+ throw new Error(`Unsafe ${envPrefix}_RECIPE_NAMESPACE: ${fixture.recipeNamespace}`)+ }+ if (!fixture.appTitle.trim()) {+ throw new Error(`${envPrefix}_APP_TITLE is required to locate the Desktop Apps catalog card.`)+ }+ return fixture+}++/**+ * Read the provider this run claims to prove. Required, with no default: a run+ * that does not state which provider it exercises cannot be presented as+ * evidence for any of them.+ */+export function requireExpectedSdkProvider(envKey: string): SupportedSdkProvider {+ const raw = (process.env[envKey] ?? '').trim().toLowerCase()+ if (!raw) {+ throw new Error(+ `${envKey} is required; declare which provider this Desktop SDK run must exercise ` ++ `(${SUPPORTED_SDK_PROVIDERS.join(' | ')}).`+ )+ }+ const provider = SUPPORTED_SDK_PROVIDERS.find(candidate => candidate === raw)+ if (!provider) {+ throw new Error(+ `Unsupported ${envKey}: ${raw}. Expected one of ${SUPPORTED_SDK_PROVIDERS.join(', ')}.`+ )+ }+ return provider+}++function readRecipe(fixture: SandboxUiFixture): {+ spec?: {+ steps?: unknown+ triggers?: unknown+ agent?: { provider?: unknown; model?: unknown }+ workloads?: unknown+ pluginWorkloadSdk?: { promptBridge?: Record<string, unknown> }+ }+ status?: {+ pluginWorkloadSdk?: {+ state?: unknown+ bootstrapContractVersion?: unknown+ message?: unknown+ }+ }+} {+ const raw = execFileSync(+ 'kubectl',+ [+ '--context',+ k8sContext(),+ '-n',+ fixture.recipeNamespace,+ 'get',+ 'workflowrecipe',+ fixture.recipeName,+ '-o',+ 'json',+ ],+ { encoding: 'utf8', timeout: 30_000 }+ )+ return JSON.parse(raw)+}++/**+ * Read-only precondition for an SDK-only Desktop candidate recipe.+ *+ * `expectedProvider` is asserted against the live recipe rather than merely+ * accepted from an allowlist: a recipe that silently lost its Codex binding+ * would otherwise keep this journey green while it exercised a different+ * provider entirely.+ */+export function assertSteplessSdkRecipePrecondition(+ fixture: SandboxUiFixture,+ expectedProvider: SupportedSdkProvider+): { provider: string; model: string } {+ const recipe = readRecipe(fixture)+ const spec = recipe.spec ?? {}+ if (spec.steps !== undefined || spec.triggers !== undefined) {+ throw new Error(+ 'Desktop candidate must be SDK-only: spec.steps and spec.triggers must be absent.'+ )+ }+ if (+ typeof spec.agent?.provider !== 'string' ||+ !spec.agent.provider ||+ typeof spec.agent.model !== 'string' ||+ !spec.agent.model+ ) {+ throw new Error('Desktop candidate must declare a resolvable spec.agent bootstrap.')+ }+ const provider = spec.agent.provider.toLowerCase()+ if (provider !== expectedProvider) {+ throw new Error(+ `Desktop SDK run declared provider ${expectedProvider} but recipe ` ++ `${fixture.recipeNamespace}/${fixture.recipeName} declares ${spec.agent.provider}. ` ++ 'Refusing to report this run as evidence for a provider it does not exercise.'+ )+ }+ if (!Array.isArray(spec.workloads) || spec.workloads.length === 0) {+ throw new Error('Desktop candidate must declare at least one plugin workload.')+ }+ if (+ typeof spec.pluginWorkloadSdk?.promptBridge !== 'object' ||+ spec.pluginWorkloadSdk.promptBridge === null+ ) {+ throw new Error('Desktop candidate must declare the pluginWorkloadSdk.promptBridge object.')+ }+ return { provider: spec.agent.provider, model: spec.agent.model }+}++export function readPluginWorkloadSdkStatus(fixture: SandboxUiFixture): PluginWorkloadSdkStatus {+ const sdk = readRecipe(fixture).status?.pluginWorkloadSdk ?? {}+ return {+ state: typeof sdk.state === 'string' ? sdk.state : '',+ bootstrapContractVersion:+ sdk.bootstrapContractVersion === 2 || sdk.bootstrapContractVersion === 3+ ? sdk.bootstrapContractVersion+ : null,+ message: typeof sdk.message === 'string' ? sdk.message : '',+ }+}++export function sandboxUiViewUrlPrefix(fixture: SandboxUiFixture): string {+ return `/api/v1/sandbox-ui/${encodeURIComponent(fixture.recipeNamespace)}/${encodeURIComponent(+ fixture.recipeName+ )}/view/`+}++export async function findSandboxUiContents(+ app: ElectronApplication,+ fixture: SandboxUiFixture+): Promise<EmbeddedContents | null> {+ return app.evaluate(async ({ webContents }, expectedTitle) => {+ for (const contents of webContents.getAllWebContents()) {+ if (contents.isDestroyed()) continue+ const url = contents.getURL()+ if (!url.includes('/api/v1/sandbox-ui/') || !url.includes('/view/')) continue+ const title = await contents.executeJavaScript('document.title').catch(() => '')+ if (title === expectedTitle) return { id: contents.id, url }+ }+ return null+ }, fixture.appTitle)+}++export async function embeddedRect(+ app: ElectronApplication,+ webContentsId: number,+ selector: string+): Promise<Rect | null> {+ return app.evaluate(+ async ({ webContents }, args) => {+ const contents = webContents.fromId(args.webContentsId)+ if (!contents || contents.isDestroyed()) return null+ const script = `(() => {+ const el = document.querySelector(${JSON.stringify(args.selector)});+ if (!el) return null;+ const r = el.getBoundingClientRect();+ const style = getComputedStyle(el);+ if (r.width <= 0 || r.height <= 0 || style.visibility === 'hidden' || style.display === 'none') return null;+ return { x: r.x, y: r.y, width: r.width, height: r.height };+ })()`+ return contents.executeJavaScript(script)+ },+ { webContentsId, selector }+ )+}++export async function embeddedText(+ app: ElectronApplication,+ webContentsId: number,+ selector: string+): Promise<string> {+ return app.evaluate(+ async ({ webContents }, args) => {+ const contents = webContents.fromId(args.webContentsId)+ if (!contents || contents.isDestroyed()) return ''+ const script = `document.querySelector(${JSON.stringify(args.selector)})?.textContent ?? ''`+ return String(await contents.executeJavaScript(script))+ },+ { webContentsId, selector }+ )+}++export async function embeddedValue(+ app: ElectronApplication,+ webContentsId: number,+ selector: string+): Promise<string> {+ return app.evaluate(+ async ({ webContents }, args) => {+ const contents = webContents.fromId(args.webContentsId)+ if (!contents || contents.isDestroyed()) return ''+ const script = `String(document.querySelector(${JSON.stringify(args.selector)})?.value ?? '')`+ return String(await contents.executeJavaScript(script))+ },+ { webContentsId, selector }+ )+}++export async function embeddedOptions(+ app: ElectronApplication,+ webContentsId: number,+ selector: string+): Promise<EmbeddedOption[]> {+ return app.evaluate(+ async ({ webContents }, args) => {+ const contents = webContents.fromId(args.webContentsId)+ if (!contents || contents.isDestroyed()) return []+ const script = `Array.from(document.querySelector(${JSON.stringify(args.selector)})?.options ?? []).map((option) => ({ value: option.value, label: option.textContent ?? '' }))`+ return (await contents.executeJavaScript(script)) as EmbeddedOption[]+ },+ { webContentsId, selector }+ )+}++export async function embeddedActiveControl(+ app: ElectronApplication,+ webContentsId: number,+ selector: string+): Promise<boolean> {+ return app.evaluate(+ async ({ webContents }, args) => {+ const contents = webContents.fromId(args.webContentsId)+ if (!contents || contents.isDestroyed()) return false+ const script = `document.activeElement?.matches(${JSON.stringify(args.selector)}) === true`+ return Boolean(await contents.executeJavaScript(script))+ },+ { webContentsId, selector }+ )+}++export async function embeddedActiveSignature(+ app: ElectronApplication,+ webContentsId: number+): Promise<string> {+ return app.evaluate(async ({ webContents }, webContentsId) => {+ const contents = webContents.fromId(webContentsId)+ if (!contents || contents.isDestroyed()) return 'destroyed'+ return String(+ await contents.executeJavaScript(`(() => {+ const el = document.activeElement;+ if (!el) return 'none';+ return [el.tagName, el.id || '', el.getAttribute('name') || ''].join('#');+ })()`)+ )+ }, webContentsId)+}++/**+ * Capture the actual native WebContentsView, which is composited above the+ * renderer and therefore is not guaranteed to appear in page.screenshot().+ * The data URL crosses the Electron boundary without exposing browser state+ * or using a DOM shortcut; the resulting PNG is a Playwright test artifact.+ */+export async function captureEmbeddedView(+ app: ElectronApplication,+ webContentsId: number,+ outputPath: string+): Promise<void> {+ const dataUrl = await app.evaluate(async ({ webContents }, id) => {+ const contents = webContents.fromId(id)+ if (!contents || contents.isDestroyed()) throw new Error('Embedded WebContentsView closed')+ const image = await contents.capturePage()+ return image.toDataURL()+ }, webContentsId)+ const encoded = dataUrl.match(/^data:image\/png;base64,(.+)$/)?.[1]+ if (!encoded) throw new Error('Electron returned a non-PNG sandbox UI capture')+ writeFileSync(outputPath, Buffer.from(encoded, 'base64'))+}++/** Capture the BrowserWindow renderer surface for comparison with the native child capture. */+export async function captureDesktopWindow(+ app: ElectronApplication,+ outputPath: string+): Promise<void> {+ const dataUrl = await app.evaluate(async ({ BrowserWindow }) => {+ const window = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0]+ if (!window || window.isDestroyed()) throw new Error('Desktop BrowserWindow closed')+ const image = await window.capturePage()+ return image.toDataURL()+ })+ const encoded = dataUrl.match(/^data:image\/png;base64,(.+)$/)?.[1]+ if (!encoded) throw new Error('Electron returned a non-PNG desktop window capture')+ writeFileSync(outputPath, Buffer.from(encoded, 'base64'))+}++/** Read the native child-view bounds for a geometry comparison artifact. */+export async function nativeEmbeddedLayout(+ app: ElectronApplication,+ webContentsId: number+): Promise<NativeLayout> {+ return app.evaluate(async ({ BrowserWindow, screen }, id) => {+ const window = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0]+ if (!window || window.isDestroyed()) throw new Error('Desktop BrowserWindow closed')+ const child = window.contentView.children.find(view => view.webContents?.id === id)+ const windowBounds = window.getBounds()+ const display = screen.getDisplayMatching(windowBounds)+ const mainRendererDpr = Number(+ await window.webContents.executeJavaScript('window.devicePixelRatio')+ )+ const embeddedRendererDpr = child+ ? Number(await child.webContents.executeJavaScript('window.devicePixelRatio'))+ : null+ const embeddedCaptureSize = child ? (await child.webContents.capturePage()).getSize() : null+ return {+ windowBounds,+ viewBounds: child?.getBounds() ?? null,+ displayScaleFactor: display.scaleFactor,+ mainRendererDpr,+ embeddedRendererDpr,+ embeddedCaptureSize,+ }+ }, webContentsId)+}++export async function attachVisualLayout(+ page: Page,+ app: ElectronApplication,+ webContentsId: number,+ testInfo: TestInfo,+ stage: string+): Promise<void> {+ const rendererRect = await page.locator('.sandbox-ui-embed-slot').boundingBox()+ const nativeLayout = await nativeEmbeddedLayout(app, webContentsId)+ await testInfo.attach(`sandbox-ui-layout-${stage}`, {+ body: JSON.stringify({ stage, rendererRect, nativeLayout }, null, 2),+ contentType: 'application/json',+ })+ console.log(`[sandbox-ui-visual] ${JSON.stringify({ stage, rendererRect, nativeLayout })}`)+}++export async function sendEmbeddedKey(+ app: ElectronApplication,+ webContentsId: number,+ keyCode: string+): Promise<void> {+ await app.evaluate(+ ({ webContents }, args) => {+ const contents = webContents.fromId(args.webContentsId)+ if (!contents || contents.isDestroyed()) throw new Error('Embedded WebContentsView closed')+ contents.focus()+ contents.sendInputEvent({ type: 'keyDown', keyCode: args.keyCode })+ if (args.keyCode === 'Enter') {+ contents.sendInputEvent({ type: 'char', keyCode: '\r' })+ }+ contents.sendInputEvent({ type: 'keyUp', keyCode: args.keyCode })+ },+ { webContentsId, keyCode }+ )+}++export async function focusEmbeddedControl(+ app: ElectronApplication,+ webContentsId: number,+ selector: string+): Promise<void> {+ // WebContentsView controls below the fold cannot be clicked with coordinates+ // returned by getBoundingClientRect(): Chromium drops pointer events whose+ // target point is outside the view. Tab traversal is the native keyboard+ // path a user takes and scrolls each focused control into view without DOM+ // focus()/scrollIntoView() shortcuts.+ for (let attempt = 0; attempt < 16; attempt += 1) {+ if (await embeddedActiveControl(app, webContentsId, selector)) return+ const activeBefore = await embeddedActiveSignature(app, webContentsId)+ await sendEmbeddedKey(app, webContentsId, 'Tab')+ await expect+ .poll(() => embeddedActiveSignature(app, webContentsId), {+ timeout: 2_000,+ intervals: [10, 25, 50],+ })+ .not.toBe(activeBefore)+ }+ throw new Error(`Embedded control ${selector} did not receive native keyboard focus.`)+}++export async function typeEmbedded(+ app: ElectronApplication,+ webContentsId: number,+ selector: string,+ value: string+): Promise<void> {+ await focusEmbeddedControl(app, webContentsId, selector)+ await app.evaluate(+ ({ webContents }, args) => {+ const contents = webContents.fromId(args.webContentsId)+ if (!contents || contents.isDestroyed()) throw new Error('Embedded WebContentsView closed')+ contents.focus()+ for (const character of args.value) {+ contents.sendInputEvent({ type: 'char', keyCode: character })+ }+ },+ { webContentsId, value }+ )+ await expect.poll(() => embeddedValue(app, webContentsId, selector)).toBe(value)+}++export async function activateEmbedded(+ app: ElectronApplication,+ webContentsId: number,+ selector: string+): Promise<void> {+ await focusEmbeddedControl(app, webContentsId, selector)+ await sendEmbeddedKey(app, webContentsId, 'Enter')+}++export async function selectEmbeddedRecipient(+ app: ElectronApplication,+ webContentsId: number,+ selector: string,+ expectedEmail: string+): Promise<EmbeddedOption> {+ const options = await embeddedOptions(app, webContentsId, selector)+ const normalizedExpectedEmail = expectedEmail.trim().toLowerCase()+ const recipient = options.find(+ option => option.value && option.label.trim().toLowerCase() === normalizedExpectedEmail+ )+ if (!recipient) {+ throw new Error(+ `Embedded recipient picker ${selector} has no option for authenticated user ${expectedEmail}.`+ )+ }+ const recipientIndex = options.findIndex(option => option.value === recipient.value)+ if (recipientIndex < 0) {+ throw new Error(`Embedded recipient picker ${selector} lost the authenticated-user option.`)+ }++ // This is the native select path a user takes: focus the control, move from+ // the first option to the authenticated user's granted recipient, and commit+ // with Enter. The option order is not part of the contract, so the number of+ // ArrowDown events is derived from the live option list rather than assumed.+ // We inspect the option label only to verify the UI displays a human handle;+ // the value is never injected into the DOM or sent directly to the backend.+ await focusEmbeddedControl(app, webContentsId, selector)+ await sendEmbeddedKey(app, webContentsId, 'Home')+ for (let index = 0; index < recipientIndex; index += 1) {+ await sendEmbeddedKey(app, webContentsId, 'ArrowDown')+ }+ await sendEmbeddedKey(app, webContentsId, 'Enter')+ await expect.poll(() => embeddedValue(app, webContentsId, selector)).toBe(recipient.value)+ expect(recipient.label.trim().toLowerCase()).toBe(normalizedExpectedEmail)+ expect(recipient.label).not.toMatch(/^[0-9a-f-]{36}$/i)+ return recipient+}++export function sdkInvocationCount(+ fixture: SandboxUiFixture,+ method: 'promptBridge' | 'clientNotifications'+): number {+ const raw = profilesSql(`+ SELECT count(*)::int+ FROM plugin_workload_sdk_invocations+ WHERE recipe_namespace = ${sqlLiteral(fixture.recipeNamespace)}+ AND recipe_name = ${sqlLiteral(fixture.recipeName)}+ AND method = ${sqlLiteral(method)};+ `)+ return Number.parseInt(raw, 10) || 0+}++export function promptBridgeLedgerForRun(+ fixture: SandboxUiFixture,+ startedAt: string+): {+ invocationId: string+ sdkAttemptId: string+ codexAttemptId: string+ spendOutcome: string+} {+ const raw = profilesSql(`+ SELECT inv.id::text || '|' || sdk.id::text || '|' ||+ coalesce(codex.id::text, '') || '|' || coalesce(spend.outcome, '')+ FROM plugin_workload_sdk_invocations inv+ JOIN plugin_workload_sdk_provider_attempts sdk+ ON sdk.invocation_id = inv.id+ LEFT JOIN llm_provider_attempts codex+ ON codex.plugin_workload_sdk_provider_attempt_id = sdk.id+ LEFT JOIN plugin_workload_sdk_spend_outcomes spend+ ON spend.provider_attempt_id = sdk.id+ WHERE inv.recipe_namespace = ${sqlLiteral(fixture.recipeNamespace)}+ AND inv.recipe_name = ${sqlLiteral(fixture.recipeName)}+ AND inv.method = 'promptBridge'+ AND inv.created_at >= ${sqlLiteral(startedAt)}::timestamptz+ ORDER BY inv.created_at ASC, sdk.attempt_index ASC+ LIMIT 1;+ `)+ const [invocationId, sdkAttemptId, codexAttemptId, spendOutcome] = raw.trim().split('|')+ return {+ invocationId: invocationId ?? '',+ sdkAttemptId: sdkAttemptId ?? '',+ codexAttemptId: codexAttemptId ?? '',+ spendOutcome: spendOutcome ?? '',+ }+}++export function latestSdkInvocationStatus(+ fixture: SandboxUiFixture,+ method: 'promptBridge' | 'clientNotifications'+): string {+ return profilesSql(`+ SELECT status+ FROM plugin_workload_sdk_invocations+ WHERE recipe_namespace = ${sqlLiteral(fixture.recipeNamespace)}+ AND recipe_name = ${sqlLiteral(fixture.recipeName)}+ AND method = ${sqlLiteral(method)}+ ORDER BY created_at DESC+ LIMIT 1;+ `)+}++export function notificationDeliverySignal(notificationId: string): string {+ return profilesSql(`+ SELECT event_type || '|' || status+ FROM notification_deliveries+ WHERE event_type = 'plugin_workload_sdk.notification'+ AND payload->>'notificationId' = ${sqlLiteral(notificationId)}+ LIMIT 1;+ `)+}