源码快照 @ 2026-09-02 (source-only, 剔除非源码素材)

This commit is contained in:
agent-radar committed 2026-09-02 01:05:41 +08:00
commit 04bac1f6e2
2200 files changed
+489415

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
{
"name": "@openwork/behaviors",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
".": {
"types": "./src/index.ts",
"development": "./src/index.ts",
"default": "./src/index.ts"
},
"./den": {
"types": "./src/den.ts",
"development": "./src/den.ts",
"default": "./src/den.ts"
},
"./desktop": {
"types": "./src/desktop.ts",
"development": "./src/desktop.ts",
"default": "./src/desktop.ts"
},
"./desktop-boot": {
"types": "./src/desktop-boot.ts",
"development": "./src/desktop-boot.ts",
"default": "./src/desktop-boot.ts"
},
"./diagnostics": {
"types": "./src/diagnostics.ts",
"development": "./src/diagnostics.ts",
"default": "./src/diagnostics.ts"
}
},
"dependencies": {
"@openwork/cdp": "workspace:*",
"@openwork/labs": "workspace:*",
"@openwork/matchers": "workspace:*",
"@openwork/timeline": "workspace:*"
}
}
@@ -0,0 +1,153 @@
import { chmod, mkdtemp, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { timed } from "@openwork/timeline";
import { control, evalIn, waitFor } from "./desktop.ts";
import type { Surface } from "@openwork/cdp";
/**
* The desktop signs in by opening the system browser and finishing there. To
* observe that faithfully we capture what the app actually asks the OS to open
* (via a PATH shim for xdg-open, which is what Electron's shell.openExternal
* calls on Linux) rather than trusting that a browser appeared.
*
* Nothing here modifies the product: the app opens a URL exactly as it always
* does; we just record where it pointed.
*/
export interface UrlCapture {
/** Prepend to PATH when spawning the app. */
binDir: string;
/** Every URL the app asked the OS to open, oldest first. */
opened(): Promise<string[]>;
/** Wait for a URL matching a predicate, so a spec never races the handoff. */
waitForUrl(match: (url: string) => boolean, opts?: { timeoutMs?: number }): Promise<string>;
}
export async function captureOpenedUrls(): Promise<UrlCapture> {
const dir = await mkdtemp(join(tmpdir(), "openwork-open-external-"));
const logPath = join(dir, "opened-urls.log");
await writeFile(logPath, "", "utf8");
const shim = join(dir, "xdg-open");
await writeFile(shim, `#!/usr/bin/env bash\nprintf '%s\\n' "$1" >> ${JSON.stringify(logPath)}\nexit 0\n`, "utf8");
await chmod(shim, 0o755);
const opened = async (): Promise<string[]> => {
const contents = await readFile(logPath, "utf8").catch(() => "");
return contents.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
};
return {
binDir: dir,
opened,
async waitForUrl(match, { timeoutMs = 60_000 } = {}) {
const deadline = Date.now() + timeoutMs;
let seen: string[] = [];
while (Date.now() < deadline) {
seen = await opened();
const hit = seen.find((url) => match(url));
if (hit) return hit;
await new Promise((resolve) => setTimeout(resolve, Math.min(500, Math.max(0, deadline - Date.now()))));
}
throw new Error(`The app never asked to open a matching URL within ${timeoutMs}ms. Opened: ${seen.join(", ") || "<none>"}`);
},
};
}
/**
* Sign in on a real den web page in a real browser, the way a person does.
*
* Observed shape: den auth is two steps, not one form. First an email-only
* step ("Enter your email and we'll send you to the right sign-in step",
* button "Next"); only after that does the password step appear (button
* "Sign in"). There is no sign-up/sign-in toggle — the email step routes.
*/
export async function signInInBrowser(
browser: Surface,
url: string,
credentials: { email: string; password: string },
): Promise<void> {
await timed("browser.signIn", async () => {
await evalIn(browser, `window.location.href = ${JSON.stringify(url)}`);
await waitFor(browser, `(() => {
if (document.querySelector('input[type="password"], input[name="password"]')) return true;
const email = document.querySelector('input[type="email"], input[name="email"]');
if (!email) return false;
const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
setter.call(email, ${JSON.stringify(credentials.email)});
email.dispatchEvent(new Event("input", { bubbles: true }));
const next = [...document.querySelectorAll("button")]
.find((candidate) => /next|continue|sign ?in/i.test(candidate.textContent ?? "") && !candidate.disabled);
if (!next) return false;
next.click();
return true;
})()`, { timeoutMs: 120_000, label: "den email step submitted" });
await waitFor(browser, `(() => {
if (/signed in/i.test(document.body?.innerText ?? "")) return true;
const password = document.querySelector('input[type="password"], input[name="password"]');
if (!password) return false;
const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
setter.call(password, ${JSON.stringify(credentials.password)});
password.dispatchEvent(new Event("input", { bubbles: true }));
const submit = [...document.querySelectorAll("button")]
.find((candidate) => /sign ?in|continue|log ?in/i.test(candidate.textContent ?? "") && !candidate.disabled);
if (!submit) return false;
submit.click();
return true;
})()`, { timeoutMs: 60_000, label: "den password step submitted" });
// Signed in means the outcome page, not a submit in flight ("Working...").
await waitFor(browser, `(() => {
const text = document.body?.innerText ?? "";
// Plain web sign-in redirects into the dashboard; handoff shows "signed in" or the openwork:// code.
if (location.pathname.startsWith("/dashboard")) return true;
if (/signed in/i.test(text)) return true;
return [...document.querySelectorAll("input")]
.some((input) => (input.value ?? "").startsWith("openwork://"));
})()`, { timeoutMs: 60_000, label: "den sign-in outcome" });
}, credentials.email);
}
/**
* Read the deep link the browser is handed once sign-in completes.
*
* Observed shape: the app opens `<den>/?mode=sign-up&desktopAuth=1&desktopScheme=openwork`,
* the person signs in there, and Den shows "You're signed in" with an
* "Open OpenWork" button plus a readonly input holding the sign-in code —
* the full `openwork://den-auth?grant=…&denBaseUrl=…` URL a person would
* copy-paste into the app. Reading that input keeps the grant the real one
* Den issued for this session.
*/
export async function readHandoffDeepLink(browser: Surface, { timeoutMs = 60_000 } = {}): Promise<string> {
const found = await waitFor(browser, `(() => {
const fromInput = [...document.querySelectorAll("input")]
.map((input) => input.value)
.find((value) => typeof value === "string" && value.startsWith("openwork://") && value.includes("grant="));
if (fromInput) return fromInput;
const fromAnchor = [...document.querySelectorAll('a[href^="openwork://"]')]
.map((anchor) => anchor.getAttribute("href"))
.find((href) => typeof href === "string" && href.includes("grant="));
if (fromAnchor) return fromAnchor;
const inText = (document.body?.innerText ?? "").match(/openwork:\\/\\/[^\\s"']+grant=[^\\s"']+/);
return inText ? inText[0] : false;
})()`, { timeoutMs, label: "handoff deep link in the browser" });
if (typeof found !== "string") throw new Error("Could not read a handoff deep link from the browser page.");
return found;
}
/**
* Complete the hop back into the desktop.
*
* A real OS dispatches `openwork://den-auth?grant=…` to the app. A container has
* no protocol handler registered, so we hand the grant to the product's own
* documented entry point for exactly this situation (`auth.exchange-grant`,
* described in-product as signing in with a handoff grant). The grant itself is
* the real one the app generated and the browser session approved — only the OS
* dispatch is bridged, and that is stated wherever this is used.
*/
export async function completeDesktopHandoff(app: Surface, deepLinkOrGrantUrl: string, denBaseUrl: string): Promise<string> {
const url = new URL(deepLinkOrGrantUrl);
const grant = url.searchParams.get("grant") ?? "";
if (!grant) throw new Error(`No grant in the handoff URL: ${deepLinkOrGrantUrl}`);
await control(app, "auth.exchange-grant", { grant, baseUrl: denBaseUrl });
return grant;
}
@@ -0,0 +1,163 @@
import { timed } from "@openwork/timeline";
import { denFetch } from "./den.ts";
import type { DenSession } from "./den.ts";
/**
* Authoring and sharing on the cloud side: a skill lives inside a plugin, and a
* plugin becomes shareable by being assigned to a marketplace that colleagues
* (or their teams) can use.
*
* These are plain API behaviours over the real den contract
* (ee/apps/den-api/src/routes/org/plugin-system), so the same calls work from a
* spec or a support script.
*/
export interface MarketplaceFacts {
id: string;
name: string;
}
export interface PluginFacts {
id: string;
name: string;
componentIds: string[];
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function requireId(value: unknown, what: string, status: number, body: unknown): string {
const item = isRecord(value) && isRecord(value.item) ? value.item : isRecord(value) ? value : null;
const id = item && typeof item.id === "string" ? item.id : "";
if (!id) throw new Error(`${what} did not return an id (status ${status}): ${JSON.stringify(body).slice(0, 300)}`);
return id;
}
export async function createMarketplace(admin: DenSession, input: { name: string; description?: string }): Promise<MarketplaceFacts> {
return timed("cloud.createMarketplace", async () => {
const { response, body } = await denFetch(admin, "/v1/marketplaces", {
method: "POST",
headers: { authorization: `Bearer ${admin.token}` },
body: JSON.stringify({ name: input.name, description: input.description ?? null }),
});
if (!response.ok) throw new Error(`Creating marketplace failed (${response.status}): ${JSON.stringify(body).slice(0, 300)}`);
return { id: requireId(body, "Creating a marketplace", response.status, body), name: input.name };
});
}
/**
* Create a plugin that CONTAINS a skill, and (optionally) publish it to a
* marketplace in the same call — the contract supports both, which mirrors what
* a person does in one sitting: author the skill, then share it.
*/
export async function createPluginWithSkill(
admin: DenSession,
input: {
name: string;
skillName: string;
skillBody: string;
skillDescription?: string;
description?: string;
marketplaceId?: string;
orgWide?: boolean;
},
): Promise<PluginFacts> {
return timed("cloud.createPluginWithSkill", async () => {
const skillMarkdown = `---\nname: ${input.skillName}\ndescription: ${input.skillDescription ?? "Shared by an eval to prove skill sharing works."}\n---\n\n${input.skillBody}\n`;
const { response, body } = await denFetch(admin, "/v1/plugins", {
method: "POST",
headers: { authorization: `Bearer ${admin.token}` },
body: JSON.stringify({
name: input.name,
description: input.description ?? "Created by the first-run cloud sharing eval.",
orgWide: input.orgWide ?? true,
marketplaceId: input.marketplaceId,
components: [{ type: "skill", input: { rawSourceText: skillMarkdown } }],
}),
});
if (!response.ok) throw new Error(`Creating plugin failed (${response.status}): ${JSON.stringify(body).slice(0, 300)}`);
const id = requireId(body, "Creating a plugin", response.status, body);
const item = isRecord(body) && isRecord(body.item) ? body.item : {};
const components = Array.isArray(item.components) ? item.components : [];
const componentIds = components
.map((component) => (isRecord(component) && typeof component.id === "string" ? component.id : ""))
.filter((value) => value.length > 0);
return { id, name: input.name, componentIds };
});
}
export async function assignPluginToMarketplace(admin: DenSession, marketplaceId: string, pluginId: string): Promise<void> {
await timed("cloud.assignPluginToMarketplace", async () => {
const { response, body } = await denFetch(admin, `/v1/marketplaces/${marketplaceId}/plugins`, {
method: "POST",
headers: { authorization: `Bearer ${admin.token}` },
body: JSON.stringify({ pluginId }),
});
if (!response.ok) throw new Error(`Assigning plugin to marketplace failed (${response.status}): ${JSON.stringify(body).slice(0, 300)}`);
});
}
/**
* Give a colleague (or their team, or the whole org) access to the marketplace.
* The API requires a role: viewer covers "can see and use what's shared".
*/
export async function grantMarketplaceAccess(
admin: DenSession,
marketplaceId: string,
grant: ({ orgWide: true } | { orgMembershipId: string } | { teamId: string }) & { role?: "viewer" | "editor" | "manager" },
): Promise<void> {
await timed("cloud.grantMarketplaceAccess", async () => {
const { response, body } = await denFetch(admin, `/v1/marketplaces/${marketplaceId}/access`, {
method: "POST",
headers: { authorization: `Bearer ${admin.token}` },
body: JSON.stringify({ role: "viewer", ...grant }),
});
if (!response.ok) throw new Error(`Granting marketplace access failed (${response.status}): ${JSON.stringify(body).slice(0, 300)}`);
});
}
export interface ResolvedMarketplaceFacts {
pluginNames: string[];
skillNames: string[];
raw: unknown;
}
/**
* What a member actually sees in a marketplace — read as that member, not the
* admin. The resolved marketplace lists plugins (with component COUNTS only);
* the skill names live on each plugin's own resolved components
* (items[].configObject with objectType "skill" and the skill's title).
*/
export async function readResolvedMarketplace(member: DenSession, marketplaceId: string): Promise<ResolvedMarketplaceFacts> {
return timed("cloud.readResolvedMarketplace", async () => {
const { response, body } = await denFetch(member, `/v1/marketplaces/${marketplaceId}/resolved`, {
headers: { authorization: `Bearer ${member.token}` },
});
if (!response.ok) throw new Error(`Reading the resolved marketplace failed (${response.status}): ${JSON.stringify(body).slice(0, 300)}`);
const item = isRecord(body) && isRecord(body.item) ? body.item : isRecord(body) ? body : {};
const plugins = Array.isArray(item.plugins) ? item.plugins : [];
const pluginNames: string[] = [];
const pluginIds: string[] = [];
for (const plugin of plugins) {
if (!isRecord(plugin)) continue;
if (typeof plugin.name === "string") pluginNames.push(plugin.name);
if (typeof plugin.id === "string") pluginIds.push(plugin.id);
}
const skillNames: string[] = [];
for (const pluginId of pluginIds) {
const resolved = await denFetch(member, `/v1/plugins/${encodeURIComponent(pluginId)}/resolved`, {
headers: { authorization: `Bearer ${member.token}` },
});
if (!resolved.response.ok) continue; // Not readable by this member = not visible to them.
const items = isRecord(resolved.body) && Array.isArray(resolved.body.items) ? resolved.body.items : [];
for (const component of items) {
if (!isRecord(component) || !isRecord(component.configObject)) continue;
const configObject = component.configObject;
if (configObject.objectType !== "skill") continue;
if (typeof configObject.title === "string" && configObject.title) skillNames.push(configObject.title);
}
}
return { pluginNames, skillNames, raw: body };
});
}
+218
View File
@@ -0,0 +1,218 @@
import type { Surface } from "@openwork/cdp";
import { control, evalIn, waitFor } from "./desktop.ts";
export interface ComposerState {
composerEditable: boolean;
draftText: string;
route: string;
runTaskVisible: boolean;
runTaskEnabled: boolean;
userMessageCount: number;
assistantMessageCount: number;
selectedModelLabel: string;
modelUnavailable: boolean;
}
export interface AssistantReplyFacts {
text: string;
assistantMessageCount: number;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function numberField(value: unknown): number {
return typeof value === "number" && Number.isFinite(value) ? value : 0;
}
function stringField(value: unknown): string {
return typeof value === "string" ? value : "";
}
function messageText(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export async function readComposerState(app: Surface): Promise<ComposerState> {
const value = await evalIn(app, `(() => {
const editor = document.querySelector('[contenteditable="true"][data-lexical-editor="true"]')
?? document.querySelector('[contenteditable="true"]');
const run = [...document.querySelectorAll("button")]
.find((button) => (button.textContent ?? "").trim() === "Run task");
const model = document.querySelector('button[aria-label="Change model"]');
return {
composerEditable: Boolean(editor),
draftText: editor?.innerText ?? "",
route: location.hash,
runTaskVisible: Boolean(run),
runTaskEnabled: Boolean(run && !run.disabled),
userMessageCount: document.querySelectorAll('[data-message-role="user"]').length,
assistantMessageCount: document.querySelectorAll('[data-message-role="assistant"]').length,
selectedModelLabel: model?.textContent?.trim() ?? "",
modelUnavailable: document.body.innerText.includes("Model no longer available")
|| document.body.innerText.includes("The model you were using is no longer available"),
};
})()`);
if (!isRecord(value)) throw new Error("Composer state was not an object.");
return {
composerEditable: value.composerEditable === true,
draftText: stringField(value.draftText),
route: stringField(value.route),
runTaskVisible: value.runTaskVisible === true,
runTaskEnabled: value.runTaskEnabled === true,
userMessageCount: numberField(value.userMessageCount),
assistantMessageCount: numberField(value.assistantMessageCount),
selectedModelLabel: stringField(value.selectedModelLabel),
modelUnavailable: value.modelUnavailable === true,
};
}
async function waitForComposerReady(app: Surface, timeoutMs: number): Promise<ComposerState> {
const deadline = Date.now() + timeoutMs;
let lastState: ComposerState | null = null;
let lastError: unknown = null;
while (Date.now() < deadline) {
try {
lastState = await readComposerState(app);
lastError = null;
if (lastState.composerEditable && (lastState.runTaskVisible
|| await evalIn(app, `Boolean(window.__openworkControl?.listActions?.()
.find((entry) => entry.id === "composer.set_text" && entry.disabled === false))`).catch(() => false))) {
return lastState;
}
} catch (error) {
lastError = error;
}
await sleep(Math.min(250, Math.max(0, deadline - Date.now())));
}
throw new Error(
`Composer was not ready within ${timeoutMs}ms. Last observed state: ${JSON.stringify(lastState)}`
+ `${lastError ? `. Last read error: ${messageText(lastError)}` : ""}`,
);
}
async function tryWriteComposerText(app: Surface, text: string, readinessTimeoutMs: number): Promise<void> {
await waitForComposerReady(app, readinessTimeoutMs);
// The product ships a control action for exactly this ("Type into the composer",
// registered by the session surface, so it only appears once a composer is
// mounted). Prefer it: it types visibly the way a user does. The direct
// contenteditable paste below stays as a fallback for surfaces that do not
// register the action.
let controlError = "composer.set_text was not available";
const hasControl = await evalIn(app, `Boolean(window.__openworkControl?.listActions?.()
.find((entry) => entry.id === "composer.set_text" && entry.disabled === false))`).catch(() => false);
if (hasControl === true) {
try {
await control(app, "composer.set_text", { text }, { timeoutMs: 120_000 });
await waitFor(app, `(() => {
const editor = document.querySelector('[contenteditable="true"][data-lexical-editor="true"]')
?? document.querySelector('[contenteditable="true"]');
return Boolean(editor && (editor.innerText ?? "").includes(${JSON.stringify(text)}));
})()`, { timeoutMs: 30_000, label: "composer draft text via control" });
return;
} catch (error) {
controlError = messageText(error);
}
}
let pasteError = "Could not paste text into the composer contenteditable.";
try {
await waitForComposerReady(app, readinessTimeoutMs);
const pasted = await evalIn(app, `(() => {
const editor = document.querySelector('[contenteditable="true"][data-lexical-editor="true"]')
?? document.querySelector('[contenteditable="true"]');
if (!editor) return false;
editor.focus();
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(editor);
selection?.removeAllRanges();
selection?.addRange(range);
const data = new DataTransfer();
data.setData("text/plain", ${JSON.stringify(text)});
editor.dispatchEvent(new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData: data }));
return true;
})()`);
if (pasted !== true) throw new Error(pasteError);
await waitFor(app, `(() => {
const editor = document.querySelector('[contenteditable="true"][data-lexical-editor="true"]')
?? document.querySelector('[contenteditable="true"]');
return Boolean(editor && (editor.innerText ?? "").includes(${JSON.stringify(text)}));
})()`, { timeoutMs: 30_000, label: "composer draft text" });
return;
} catch (error) {
pasteError = messageText(error);
}
throw new Error(`Composer write paths failed. Control: ${controlError}. Paste: ${pasteError}`);
}
export async function writeComposerText(
app: Surface,
text: string,
{ timeoutMs = 60_000 }: { timeoutMs?: number } = {},
): Promise<void> {
let lastError: unknown = null;
for (let attempt = 0; attempt < 2; attempt += 1) {
try {
await tryWriteComposerText(app, text, timeoutMs);
return;
} catch (error) {
lastError = error;
if (attempt === 0) await sleep(500);
}
}
const state = await readComposerState(app).catch(() => null);
throw new Error(
`Could not write composer text after two attempts: ${messageText(lastError)}. Observed composer state: ${JSON.stringify(state)}.`,
);
}
export async function sendComposerMessage(app: Surface, text: string): Promise<ComposerState> {
const before = await waitForComposerReady(app, 60_000);
await writeComposerText(app, text);
await waitFor(app, `Boolean([...document.querySelectorAll("button")]
.find((button) => (button.textContent ?? "").trim() === "Run task" && !button.disabled))
|| Boolean(window.__openworkControl?.listActions?.()
.find((entry) => entry.id === "composer.send" && entry.disabled === false))`, {
timeoutMs: 30_000,
label: "enabled composer send control",
});
const clicked = await evalIn(app, `(() => {
const button = [...document.querySelectorAll("button")]
.find((entry) => (entry.textContent ?? "").trim() === "Run task" && !entry.disabled);
if (button) {
button.click();
return true;
}
return false;
})()`);
if (clicked !== true) await control(app, "composer.send", undefined, { timeoutMs: 120_000 });
await waitFor(app, `document.querySelectorAll('[data-message-role="user"]').length > ${before.userMessageCount}`, {
timeoutMs: 60_000,
label: "sent user message",
});
return readComposerState(app);
}
export async function waitForAssistantReply(
app: Surface,
{ timeoutMs }: { timeoutMs: number },
): Promise<AssistantReplyFacts> {
await waitFor(app, `(() => {
const messages = [...document.querySelectorAll('[data-message-role="assistant"]')];
return messages.some((message) => (message.innerText ?? "").trim().length > 0);
})()`, { timeoutMs, label: "assistant reply" });
const value = await evalIn(app, `(() => {
const messages = [...document.querySelectorAll('[data-message-role="assistant"]')];
const latest = messages[messages.length - 1];
return { text: latest?.innerText?.trim() ?? "", assistantMessageCount: messages.length };
})()`);
if (!isRecord(value)) throw new Error("Assistant reply facts were not an object.");
return {
text: stringField(value.text),
assistantMessageCount: numberField(value.assistantMessageCount),
};
}
+373
View File
@@ -0,0 +1,373 @@
import { execSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { notImplemented } from "@openwork/labs";
export type DenRef = { apiUrl: string; webUrl: string };
export type DenSession = DenRef & { token: string; email: string; password: string };
export type ConnectionFacts = { id: string; name: string; connectedForMe: boolean | null; connectedAt: string | null };
export type DenFetchResult = { response: Response; body: unknown; text: string };
export interface NativeConnectorInput {
providerKey: string;
name: string;
clientId: string;
clientSecret: string;
features?: string[];
access?: { orgWide?: boolean };
}
export interface ProvisionOrgInput {
connectors?: string[];
members?: string[];
}
export interface ProvisionedOrg {
admin: DenSession;
orgId: string;
}
const REPO_ROOT = fileURLToPath(new URL("../../../..", import.meta.url));
// Two 300s header stalls were observed on fresh-sandbox first contact.
const DEFAULT_DEN_FETCH_TIMEOUT_MS = 30_000;
function trimTrailingSlashes(value: string): string {
let end = value.length;
while (end > 0 && value[end - 1] === "/") end -= 1;
return value.slice(0, end);
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function stringField(value: unknown, key: string): string | null {
if (!isRecord(value)) return null;
return typeof value[key] === "string" ? value[key] : null;
}
function preview(value: unknown): string {
return (typeof value === "string" ? value : JSON.stringify(value) ?? String(value)).slice(0, 500);
}
function auth(session: DenSession): Record<string, string> {
return { authorization: `Bearer ${session.token}` };
}
export async function denFetch(den: DenRef, path: string, init: RequestInit = {}): Promise<DenFetchResult> {
const headers = new Headers(init.headers);
if (!headers.has("content-type")) headers.set("content-type", "application/json");
if (!headers.has("origin")) headers.set("origin", den.webUrl);
const response = await fetch(`${trimTrailingSlashes(den.apiUrl)}${path}`, {
...init,
headers,
signal: init.signal ?? AbortSignal.timeout(DEFAULT_DEN_FETCH_TIMEOUT_MS),
});
const text = await response.text();
let body: unknown = text;
try {
body = text.trim() ? JSON.parse(text) : null;
} catch {
body = text;
}
return { response, body, text };
}
export async function signIn(den: DenRef, credentials: { email: string; password: string }): Promise<DenSession> {
const result = await denFetch(den, "/api/auth/sign-in/email", {
method: "POST",
body: JSON.stringify(credentials),
});
const token = stringField(result.body, "token");
if (!result.response.ok || !token) {
throw new Error(`Sign-in failed for ${credentials.email}: HTTP ${result.response.status} ${preview(result.body)}`);
}
return { ...den, token, email: credentials.email, password: credentials.password };
}
/** Refresh an eval session using the credentials captured by signIn. */
export async function freshSession(session: DenSession): Promise<DenSession> {
return signIn(session, { email: session.email, password: session.password });
}
export function doInternalMarkEmailVerified(command: string, email: string): void {
if (!command.trim()) throw new Error("OPENWORK_EVAL_MARK_VERIFIED_CMD is required to verify a newly-created member.");
try {
execSync(command.replaceAll("{email}", email), { cwd: REPO_ROOT, encoding: "utf8", stdio: "pipe" });
} catch (error) {
const stdout = isRecord(error) && typeof error.stdout === "string" ? error.stdout.trim() : "";
const stderr = isRecord(error) && typeof error.stderr === "string" ? error.stderr.trim() : "";
const message = error instanceof Error ? error.message : String(error);
throw new Error(
`Marking ${email} verified failed: ${message}\nstdout: ${stdout || "(empty)"}\nstderr: ${stderr || "(empty)"}`,
);
}
}
export async function ensureMemberSession(
den: DenRef,
admin: DenSession,
input: { email: string; password: string; name?: string; markVerifiedCmd?: string },
): Promise<DenSession> {
try {
return await signIn(den, input);
} catch {
// Bootstrap the missing member through the real invitation flow.
}
let markVerifiedWarning = "";
const loginOptions = await denFetch(
den,
`/v1/auth/login-options?email=${encodeURIComponent(input.email)}`,
);
if (isRecord(loginOptions.body) && loginOptions.body.allowPublicSignup === false) {
throw new Error(
`Member bootstrap needs public signup, but this Den runs single-org mode with signup disabled. Start den-api with DEN_ORG_MODE=multi_org, use pnpm world up acme-demo, set DEN_SINGLE_ORG_ALLOW_PUBLIC_SIGNUP=true, or pre-provision ${input.email}.`,
);
}
const invite = await denFetch(den, "/v1/invitations", {
method: "POST",
headers: auth(admin),
body: JSON.stringify({ email: input.email, role: "member" }),
});
const inviteToken = stringField(invite.body, "inviteToken");
if (!invite.response.ok || !inviteToken) {
throw new Error(`Invitation failed: HTTP ${invite.response.status} ${preview(invite.body)}`);
}
const signUp = await denFetch(den, "/api/auth/sign-up/email", {
method: "POST",
body: JSON.stringify({ email: input.email, name: input.name ?? "Jordan Demo", password: input.password }),
});
if (!signUp.response.ok) {
throw new Error(`Member sign-up failed: HTTP ${signUp.response.status} ${preview(signUp.body)}`);
}
// Best effort: verification is a convenience for dens that require it. If the
// helper is absent or fails (its DB credentials drift), the sign-in below is
// the real test — failing here would hide a member who can already sign in.
try {
doInternalMarkEmailVerified(input.markVerifiedCmd ?? "", input.email);
} catch (error) {
markVerifiedWarning = error instanceof Error ? error.message : String(error);
}
const member = await signIn(den, input).catch((signInError: unknown) => {
const detail = signInError instanceof Error ? signInError.message : String(signInError);
throw new Error(
`${input.email} could not sign in after sign-up: ${detail}`
+ (markVerifiedWarning ? `\nEmail verification also failed first: ${markVerifiedWarning}` : ""),
);
});
const accept = await denFetch(den, "/v1/orgs/invitations/accept", {
method: "POST",
headers: auth(member),
body: JSON.stringify({ id: inviteToken }),
});
if (!accept.response.ok || !isRecord(accept.body) || accept.body.accepted !== true) {
throw new Error(`Invitation accept failed: HTTP ${accept.response.status} ${preview(accept.body)}`);
}
return member;
}
function parseConnection(value: unknown): ConnectionFacts | null {
if (!isRecord(value)) return null;
const id = stringField(value, "id");
const name = stringField(value, "name");
if (!id || !name) return null;
return {
id,
name,
connectedForMe: typeof value.connectedForMe === "boolean" ? value.connectedForMe : null,
connectedAt: typeof value.connectedAt === "string" ? value.connectedAt : null,
};
}
function parseConnections(value: unknown): ConnectionFacts[] {
if (!isRecord(value) || !Array.isArray(value.connections)) return [];
return value.connections.flatMap((entry) => {
const connection = parseConnection(entry);
return connection ? [connection] : [];
});
}
export async function createOrgConnection(
admin: DenSession,
input: { name: string; url: string; authType: string; credentialMode: string; access: { orgWide: boolean } },
): Promise<{ id: string; name: string }> {
const result = await denFetch(admin, "/v1/mcp-connections", {
method: "POST",
headers: auth(admin),
body: JSON.stringify(input),
});
const connection = parseConnection(result.body);
if (!result.response.ok || !connection) {
throw new Error(`Connection create failed: HTTP ${result.response.status} ${preview(result.body)}`);
}
return { id: connection.id, name: connection.name };
}
export async function createNativeConnector(
admin: DenSession,
input: NativeConnectorInput,
): Promise<{ id: string; name: string }> {
const result = await denFetch(admin, "/v1/mcp-connections", {
method: "POST",
headers: auth(admin),
body: JSON.stringify({
kind: "native_provider",
nativeProviderKey: input.providerKey,
name: input.name,
oauthClient: {
clientId: input.clientId,
clientSecret: input.clientSecret,
features: input.features,
},
}),
});
const connection = parseConnection(result.body);
if (!result.response.ok || !connection) {
throw new Error(`Native connector create failed: HTTP ${result.response.status} ${preview(result.body)}`);
}
return { id: connection.id, name: connection.name };
}
function needsFreshAuth(result: DenFetchResult): boolean {
return result.response.status === 403 && isRecord(result.body) && result.body.error === "reauth";
}
async function retryOnceAfterFreshAuth(
session: DenSession,
operation: (active: DenSession) => Promise<DenFetchResult>,
): Promise<{ session: DenSession; result: DenFetchResult }> {
let active = session;
let result = await operation(active);
if (needsFreshAuth(result)) {
active = await freshSession(session);
result = await operation(active);
}
return { session: active, result };
}
async function deleteConnectionWithSession(admin: DenSession, id: string): Promise<DenSession> {
const { session, result } = await retryOnceAfterFreshAuth(admin, (active) => denFetch(active, `/v1/mcp-connections/${encodeURIComponent(id)}`, {
method: "DELETE",
headers: auth(active),
}));
if (!result.response.ok) throw new Error(`Connection delete failed for ${id}: HTTP ${result.response.status} ${preview(result.body)}`);
return session;
}
export async function deleteConnection(admin: DenSession, id: string): Promise<void> {
await deleteConnectionWithSession(admin, id);
}
export async function deleteConnectionsNamed(admin: DenSession, prefix: string): Promise<void> {
const listed = await retryOnceAfterFreshAuth(admin, (active) => denFetch(active, "/v1/mcp-connections?scope=manageable", {
headers: auth(active),
}));
const result = listed.result;
if (!result.response.ok) throw new Error(`Connection list failed: HTTP ${result.response.status} ${preview(result.body)}`);
let active = listed.session;
for (const connection of parseConnections(result.body)) {
if (connection.name.startsWith(prefix)) active = await deleteConnectionWithSession(active, connection.id);
}
}
export async function readUsableConnection(member: DenSession, id: string): Promise<ConnectionFacts | null> {
const result = await denFetch(member, "/v1/mcp-connections?scope=usable", { headers: auth(member) });
if (!result.response.ok) throw new Error(`Usable connection list failed: HTTP ${result.response.status} ${preview(result.body)}`);
return parseConnections(result.body).find((connection) => connection.id === id) ?? null;
}
export async function provisionOrg(den: DenRef, input: ProvisionOrgInput): Promise<ProvisionedOrg> {
const connectors = input.connectors ?? [];
for (const connector of connectors) {
if (connector !== "google-workspace") {
throw new Error(`Unsupported provisionOrg connector: ${connector}`);
}
}
const unique = `${Date.now().toString(36)}-${crypto.randomUUID().slice(0, 12)}`;
const password = process.env.OPENWORK_EVAL_DEMO_PASSWORD?.trim() || "OpenWorkDemo123!";
const email = `openwork-eval-admin-${unique}@example.test`;
const name = `OpenWork Eval ${unique}`;
const signUp = await denFetch(den, "/api/auth/sign-up/email", {
method: "POST",
body: JSON.stringify({ email, name, password }),
});
if (!signUp.response.ok) {
throw new Error(`Admin sign-up failed for ${email}: HTTP ${signUp.response.status} ${preview(signUp.body)}`);
}
const admin = await signIn(den, { email, password });
const createOrg = await denFetch(den, "/v1/org", {
method: "POST",
headers: auth(admin),
body: JSON.stringify({ name }),
});
const organization = isRecord(createOrg.body) && isRecord(createOrg.body.organization)
? createOrg.body.organization
: null;
const orgId = stringField(organization, "id");
if (!createOrg.response.ok || !orgId) {
throw new Error(`Organization create failed: HTTP ${createOrg.response.status} ${preview(createOrg.body)}`);
}
for (const memberEmail of input.members ?? []) {
const member = await ensureMemberSession(den, admin, {
email: memberEmail,
password,
name: "OpenWork Eval Member",
markVerifiedCmd: process.env.OPENWORK_EVAL_MARK_VERIFIED_CMD?.trim(),
});
const orgs = await denFetch(den, "/v1/me/orgs", { headers: auth(member) });
const memberships = isRecord(orgs.body) && Array.isArray(orgs.body.orgs) ? orgs.body.orgs : [];
if (!orgs.response.ok) {
throw new Error(`Organization membership list failed for ${memberEmail}: HTTP ${orgs.response.status} ${preview(orgs.body)}`);
}
if (memberships.some((entry) => stringField(entry, "id") === orgId)) continue;
const invite = await denFetch(den, "/v1/invitations", {
method: "POST",
headers: auth(admin),
body: JSON.stringify({ email: memberEmail, role: "member" }),
});
const inviteToken = stringField(invite.body, "inviteToken");
if (!invite.response.ok || !inviteToken) {
throw new Error(`Invitation failed for ${memberEmail}: HTTP ${invite.response.status} ${preview(invite.body)}`);
}
const accept = await denFetch(den, "/v1/orgs/invitations/accept", {
method: "POST",
headers: auth(member),
body: JSON.stringify({ id: inviteToken }),
});
if (!accept.response.ok || !isRecord(accept.body) || accept.body.accepted !== true) {
throw new Error(`Invitation accept failed for ${memberEmail}: HTTP ${accept.response.status} ${preview(accept.body)}`);
}
}
for (const connector of connectors) {
const configured = await denFetch(den, `/v1/oauth-providers/${connector}/client`, {
method: "POST",
headers: auth(admin),
body: JSON.stringify({
clientId: `openwork-eval-google-client-${unique}`,
clientSecret: `openwork-eval-google-secret-${unique}`,
}),
});
if (!configured.response.ok) {
throw new Error(`Connector configuration failed for ${connector}: HTTP ${configured.response.status} ${preview(configured.body)}`);
}
}
return { admin, orgId };
}
export async function createDesktopHandoffGrant(member: DenSession, desktopScheme = "openwork"): Promise<string> {
const result = await denFetch(member, "/v1/auth/desktop-handoff", {
method: "POST",
headers: auth(member),
body: JSON.stringify({ desktopScheme }),
});
const grant = stringField(result.body, "grant");
if (!result.response.ok || !grant) {
throw new Error(`Desktop handoff create failed: HTTP ${result.response.status} ${preview(result.body)}`);
}
return grant;
}
@@ -0,0 +1,167 @@
import { dumpScreenState, readActiveWorkspaceId } from "@openwork/cdp";
import type { Surface } from "@openwork/cdp";
import type { DenRef, DenSession } from "./den.ts";
import { createDesktopHandoffGrant } from "./den.ts";
import { clickButton, control, currentHash, evalIn, go, waitFor, waitForText, waitUntilInteractive } from "./desktop.ts";
import { createLocalWorkspaceViaUi } from "./onboarding.ts";
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
function messageText(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
async function waitForDenState(
app: Surface,
den: DenRef,
expression: string,
options: { timeoutMs: number; label: string },
): Promise<void> {
try {
await waitFor(app, expression, options);
} catch (error) {
const keys = await evalIn(
app,
"Array.from({ length: localStorage.length }, (_, index) => localStorage.key(index)).filter(Boolean).sort()",
{ timeoutMs: 5_000 },
).catch((keysError: unknown) => [`<unavailable: ${messageText(keysError)}>`]);
throw new Error(
`${messageText(error)} Resolved Den URLs: web=${den.webUrl}, api=${den.apiUrl}. Current localStorage keys: ${JSON.stringify(keys)}.`,
);
}
}
export interface SelectedWorkspaceFacts {
workspaceId: string;
route: string;
}
export async function signInDesktopAs(app: Surface, den: DenRef, member: DenSession): Promise<void> {
await waitFor(app, "Boolean(window.__openworkControl?.listActions?.().some((action) => action.id === 'auth.exchange-grant'))", {
timeoutMs: 60_000,
label: "auth.exchange-grant action registered",
});
const grant = await createDesktopHandoffGrant(member);
try {
await control(app, "auth.exchange-grant", { grant, baseUrl: den.webUrl });
} catch (error) {
if (!messageText(error).includes("Already acting: auth.exchange-grant")) throw error;
}
await waitForDenState(app, den, "Boolean((localStorage.getItem('openwork.den.authToken') ?? '').trim())", {
timeoutMs: 45_000,
label: "persisted den auth token",
});
await waitForDenState(app, den, "Boolean((localStorage.getItem('openwork.den.activeOrgId') ?? '').trim())", {
timeoutMs: 60_000,
label: "active org resolved",
});
// A first-time member lands on organization onboarding; a member whose app
// already has a workspace can come straight back to it.
await waitFor(app, `window.location.hash.includes("/onboarding") || /\\/(workspace|session)/.test(window.location.hash)`, {
timeoutMs: 60_000,
label: "organization onboarding or workspace route",
});
}
async function completeOrganizationOnboarding(app: Surface): Promise<void> {
const deadline = Date.now() + 120_000;
while (Date.now() < deadline && (await currentHash(app)).includes("/onboarding")) {
const label = await evalIn(app, `(() => {
const labels = [...document.querySelectorAll("button")]
.filter((button) => !button.disabled)
.map((button) => (button.textContent ?? "").trim());
return ["Continue with organization", "Continue to workspace", "Continue without OpenWork Models", "Continue"]
.find((candidate) => labels.includes(candidate)) ?? "";
})()`);
if (typeof label === "string" && label) {
await clickButton(app, label);
}
await sleep(Math.min(750, Math.max(0, deadline - Date.now())));
}
if ((await currentHash(app)).includes("/onboarding")) {
throw new Error(`Organization onboarding did not reach the workspace route. On screen: ${await dumpScreenState(app)}.`);
}
}
function workspaceIdFromRoute(route: string): string {
return /\/workspace\/([^/?#]+)/.exec(route)?.[1] ?? "";
}
async function waitForTaskUi(app: Surface, workspaceId: string): Promise<string> {
await go(app, `/workspace/${workspaceId}/session`);
await waitFor(app, `(() => {
const match = /^#?\\/workspace\\/([^/?#]+)\\/session\\/?$/.exec(window.location.hash);
const routeReady = match?.[1] === ${JSON.stringify(workspaceId)};
const text = document.body.innerText;
const runTask = [...document.querySelectorAll("button")]
.some((button) => (button.textContent ?? "").trim() === "Run task");
return routeReady && (text.includes("What do you need done?") || runTask);
})()`, { timeoutMs: 120_000, label: `workspace ${workspaceId} task UI` });
return currentHash(app);
}
/** The active workspace id from the product's own state, with the route as fallback. */
async function resolveWorkspaceId(app: Surface): Promise<string> {
const fromState = await readActiveWorkspaceId(app.client, { timeoutMs: 30_000 }).catch(() => null);
if (fromState) return fromState;
return workspaceIdFromRoute(await currentHash(app));
}
/**
* THE arrangement path for a workspace: the product's own onboarding, driven
* the way a person drives it. A previous API seed (POST /workspaces/local +
* activate) produced a state the product itself never produces — a workspace
* with no engine and no model catalog — and specs failed on that arrangement,
* not on their subject. If a spec needs a workspace, it goes through here.
*/
export async function createAndSelectWorkspace(
app: Surface,
input: { path: string },
): Promise<SelectedWorkspaceFacts> {
let workspaceId = "";
const route = await currentHash(app);
if (route.includes("/welcome")) {
const workspace = await createLocalWorkspaceViaUi(app, input);
await clickButton(app, "Skip and use the free model", { timeoutMs: 90_000 });
await waitForText(app, "How did you hear about OpenWork?", { timeoutMs: 90_000 });
await clickButton(app, "Skip", { timeoutMs: 15_000 });
// Only now is the workspace actually selected: resolving before the
// onboarding steps finish reads an id the app has not adopted yet.
workspaceId = workspace.id;
if (!workspaceId) {
await waitFor(app, `Boolean(localStorage.getItem("openwork.react.activeWorkspace"))
|| /\\/workspace\\/[^/?#]+/.test(window.location.hash)`, {
timeoutMs: 180_000,
label: "workspace selected after onboarding",
});
workspaceId = await resolveWorkspaceId(app);
}
} else {
if (route.includes("/onboarding")) await completeOrganizationOnboarding(app);
workspaceId = await resolveWorkspaceId(app);
if (!workspaceId) {
await waitFor(app, `window.__openworkControl.listActions()
.some((action) => action.id === "workspace.create" && !action.disabled)`, {
timeoutMs: 60_000,
label: "workspace.create enabled",
});
// Cold first action: engine spawn + Vite compile can exceed the default
// evaluate bound, and this proved flaky at 8s (passed on rerun).
await control(app, "workspace.create", input, { timeoutMs: 60_000 });
// The app does not always put a new workspace in the hash, so wait for its
// own active-workspace state to settle instead of matching a route shape.
await waitFor(app, `Boolean(localStorage.getItem("openwork.react.activeWorkspace"))
|| /\\/workspace\\/[^/?#]+/.test(window.location.hash)`, {
timeoutMs: 120_000,
label: "created workspace selected",
});
workspaceId = await resolveWorkspaceId(app);
}
}
if (!workspaceId) throw new Error("Workspace creation did not produce a workspace ID.");
const taskRoute = await waitForTaskUi(app, workspaceId);
// The task UI can be mounted while the panel still renders placeholders, so
// hand back only once the app is actually interactive.
await waitUntilInteractive(app);
return { workspaceId, route: taskRoute };
}
+462
View File
@@ -0,0 +1,462 @@
import { describeAppState, dumpScreenState, evaluateOnSurface, isInteractive, probeAppStateOnSurface } from "@openwork/cdp";
import type { AppStateProbe, EvaluateOptions, Surface } from "@openwork/cdp";
export interface SessionToolCall {
capability: string;
connectionId: string | null;
at: string;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
const DEFAULT_TIMEOUT_MS = 30_000;
const DEFAULT_DOM_PROBE_TIMEOUT_MS = 8_000;
const POLL_INTERVAL_MS = 250;
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
function messageText(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
function jsValue(value: unknown): string {
const json = JSON.stringify(value);
if (json === undefined) throw new Error("Cannot interpolate an undefined JavaScript value.");
return json.replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
}
export async function evalIn(
app: Surface,
expression: string,
opts: EvaluateOptions & { reattachAttempts?: number } = {},
): Promise<unknown> {
// Target healing lives in @openwork/cdp; behaviours just evaluate.
return evaluateOnSurface(app, expression, {
...opts,
timeoutMs: opts.timeoutMs ?? DEFAULT_DOM_PROBE_TIMEOUT_MS,
});
}
async function timeoutError(app: Surface, message: string): Promise<Error> {
return new Error(`${message} On screen: ${await dumpScreenState(app)}.`);
}
/**
* Read something from the page, tolerating a renderer that is briefly blocked.
* The app blocks its JS thread while a workspace runtime boots, so a single
* evaluation can be caught mid-block; retrying short calls is reliable where one
* long call is not. Only for IDEMPOTENT reads — never for clicks.
*/
async function resilientRead(
app: Surface,
expression: string,
{ timeoutMs = 60_000, perAttemptMs = DEFAULT_DOM_PROBE_TIMEOUT_MS, label = expression }: { timeoutMs?: number; perAttemptMs?: number; label?: string } = {},
): Promise<unknown> {
// A cold profile can block its JS thread for minutes while the workspace
// runtime boots, so retry to a deadline rather than a fixed attempt count.
const deadline = Date.now() + timeoutMs;
let lastError: unknown = null;
let attempts = 0;
while (Date.now() < deadline) {
attempts += 1;
try {
return await evalIn(app, expression, {
timeoutMs: Math.min(perAttemptMs, Math.max(0, deadline - Date.now())),
});
} catch (error) {
lastError = error;
await sleep(Math.min(POLL_INTERVAL_MS, Math.max(0, deadline - Date.now())));
}
}
throw await timeoutError(
app,
`Could not read ${label} within ${timeoutMs}ms (${attempts} attempts)${lastError ? `: ${messageText(lastError)}` : ""}.`,
);
}
export async function waitFor(
app: Surface,
expression: string,
{ timeoutMs = DEFAULT_TIMEOUT_MS, label = expression, awaitPromise = false }: { timeoutMs?: number; label?: string; awaitPromise?: boolean } = {},
): Promise<unknown> {
const startedAt = Date.now();
let lastError: unknown = null;
// Each probe gets a SHORT timeout on purpose: a renderer that is briefly busy
// should have the call abandoned and retried on the next tick. Giving a probe
// the whole budget turns one stuck evaluation into the entire wait.
while (Date.now() - startedAt < timeoutMs) {
try {
const value = await evalIn(app, expression, {
timeoutMs: Math.min(DEFAULT_DOM_PROBE_TIMEOUT_MS, Math.max(0, timeoutMs - (Date.now() - startedAt))),
awaitPromise,
});
if (value) return value;
lastError = null;
} catch (error) {
lastError = error;
}
await sleep(Math.min(POLL_INTERVAL_MS, Math.max(0, timeoutMs - (Date.now() - startedAt))));
}
throw await timeoutError(
app,
`Timed out after ${timeoutMs}ms waiting for ${label}${lastError ? ` (last error: ${messageText(lastError)})` : ""}.`,
);
}
export async function waitForText(app: Surface, text: string, opts: { timeoutMs?: number } = {}): Promise<void> {
await waitFor(app, `document.body.innerText.includes(${jsValue(text)})`, {
timeoutMs: opts.timeoutMs,
label: `visible text ${jsValue(text)}`,
});
}
export async function readSessionToolCalls(
app: Surface,
opts?: { sessionId?: string; timeoutMs?: number },
): Promise<SessionToolCall[]> {
const timeoutMs = opts?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const deadline = Date.now() + timeoutMs;
let sessionId = opts?.sessionId ?? "";
let openedSessionId = "";
while (Date.now() < deadline) {
if (!sessionId) {
const sessions = await control(app, "session.list_sessions", undefined, { timeoutMs: remainingReadMs(deadline) }).catch(() => null);
sessionId = mostRecentSessionId(sessions);
}
if (Date.now() >= deadline) break;
if (sessionId && openedSessionId !== sessionId) {
const opened = await control(app, "session.open", { sessionId }, { timeoutMs: remainingReadMs(deadline) })
.then(() => true)
.catch(() => false);
if (opened) openedSessionId = sessionId;
}
if (openedSessionId && Date.now() < deadline) {
const transcript = await control(app, "session.read_transcript", { count: 30 }, { timeoutMs: remainingReadMs(deadline) }).catch(() => null);
if (isRecord(transcript) && transcript.sessionId === openedSessionId) {
const calls = parseSessionToolCalls(transcript);
if (calls.length > 0) return calls;
}
}
await sleep(Math.min(POLL_INTERVAL_MS, Math.max(0, deadline - Date.now())));
}
return [];
}
function remainingReadMs(deadline: number): number {
return Math.max(1, Math.min(10_000, deadline - Date.now()));
}
function mostRecentSessionId(value: unknown): string {
if (!Array.isArray(value)) return "";
for (const session of value) {
if (!isRecord(session) || typeof session.sessionId !== "string") continue;
const sessionId = session.sessionId.trim();
if (sessionId) return sessionId;
}
return "";
}
function timelineAt(value: Record<string, unknown>): string {
for (const key of ["at", "timestamp", "createdAt"]) {
const candidate = value[key];
if (typeof candidate === "string") return candidate;
}
return "";
}
function connectionIdFromCapability(capability: string): string | null {
const parts = capability.split(":");
if (parts.length !== 3 || (parts[0] !== "mcp" && parts[0] !== "native") || !parts[1] || !parts[2]) return null;
return parts[1];
}
/** Parse the payload returned by the app's `session.read_transcript` control action. */
export function parseSessionToolCalls(value: unknown): SessionToolCall[] {
if (!isRecord(value) || !Array.isArray(value.messages)) return [];
const calls: SessionToolCall[] = [];
for (const message of value.messages) {
if (!isRecord(message) || typeof message.text !== "string") continue;
const matches = message.text.matchAll(/(?:^|\n)\[tool:([^\]\r\n]+)\]/g);
for (const match of matches) {
const capability = match[1];
if (!capability) continue;
calls.push({ capability, connectionId: connectionIdFromCapability(capability), at: timelineAt(message) });
}
}
return calls;
}
export async function hasText(app: Surface, text: string): Promise<boolean> {
return Boolean(await resilientRead(app, `document.body.innerText.includes(${jsValue(text)})`, { label: `text ${jsValue(text)}` }));
}
export async function visibleText(app: Surface): Promise<string> {
const text = await resilientRead(app, "document.body.innerText", { label: "visible text" });
if (typeof text !== "string") throw new Error("CDP did not return document.body.innerText as a string.");
return text;
}
export async function clickText(
app: Surface,
text: string,
{ selector = "button, [role=button], a", timeoutMs = DEFAULT_TIMEOUT_MS }: { selector?: string; timeoutMs?: number } = {},
): Promise<unknown> {
return waitFor(app, `(() => {
const candidates = document.querySelectorAll(${jsValue(selector)});
for (const element of candidates) {
const label = (element.textContent ?? '').trim();
if (label.includes(${jsValue(text)})) {
element.scrollIntoView({ block: 'center' });
element.click();
return label;
}
}
return null;
})()`, { timeoutMs, label: `clickable element with text ${jsValue(text)}` });
}
export async function clickButton(app: Surface, label: string, opts: { timeoutMs?: number } = {}): Promise<void> {
const timeoutMs = opts.timeoutMs ?? 30_000;
await waitFor(app, `Boolean([...document.querySelectorAll('button')]
.find((element) => (element.textContent ?? '').trim() === ${jsValue(label)} && !element.disabled))`, {
timeoutMs,
label: `enabled button: ${label}`,
});
const clicked = await evalIn(app, `(() => {
const button = [...document.querySelectorAll('button')]
.find((element) => (element.textContent ?? '').trim() === ${jsValue(label)} && !element.disabled);
button?.scrollIntoView({ block: 'center' });
button?.click();
return Boolean(button);
})()`);
if (clicked !== true) throw new Error(`Could not click ${label}.`);
}
export async function waitForButtonGone(app: Surface, label: string, opts: { timeoutMs?: number } = {}): Promise<void> {
await waitFor(app, `!Boolean([...document.querySelectorAll('button')]
.find((element) => (element.textContent ?? '').trim() === ${jsValue(label)}))`, {
timeoutMs: opts.timeoutMs ?? 60_000,
label: `button removed: ${label}`,
});
}
export async function fill(app: Surface, selector: string, value: string, opts: { timeoutMs?: number } = {}): Promise<void> {
await waitFor(app, `Boolean(document.querySelector(${jsValue(selector)}))`, {
timeoutMs: opts.timeoutMs,
label: `input ${selector}`,
});
await evalIn(app, `(() => {
const input = document.querySelector(${jsValue(selector)});
const setter = Object.getOwnPropertyDescriptor(
input instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype,
'value',
).set;
setter.call(input, ${jsValue(value)});
input.dispatchEvent(new Event('input', { bubbles: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
return true;
})()`);
}
export async function go(app: Surface, hashPath: string, opts: { timeoutMs?: number } = {}): Promise<void> {
const hash = hashPath.startsWith("#") ? hashPath : `#${hashPath}`;
// Setting the hash is idempotent, so retry through renderer freeze bursts
// rather than letting one blocked evaluate fail a whole spec. Contention (two
// desktops on one host) makes those bursts routine, and a bare 20s evaluate
// here was the single most common way a long journey died near its end.
const timeoutMs = opts.timeoutMs ?? 60_000;
await waitFor(app, `(() => { window.location.hash = ${jsValue(hash)}; return true; })()`, {
timeoutMs,
label: `navigate to ${hash}`,
});
}
export async function waitForConnectionCard(app: Surface, name: string, workspaceId: string): Promise<void> {
const deadline = Date.now() + 90_000;
while (Date.now() < deadline) {
// Short per-probe timeout, failure tolerated: two desktops in one sandbox
// make the renderer freeze in bursts, and a bare 20s evaluate turns one
// freeze into a failed spec.
const found = await evalIn(app, `([...document.querySelectorAll('button')]
.some((button) => (button.textContent ?? '').includes(${JSON.stringify(name)})))`, { timeoutMs: 8_000 })
.catch(() => false);
if (found === true) return;
// The app opens a freshly created session on its own, which navigates away
// from settings mid-poll. Steer back, the way a person would click back.
// The app CANONICALISES this route: /settings/extensions/connections
// becomes /extensions/connections. Checking for the pre-rewrite form made
// the steer-back below fire every iteration, so navigation fought the
// rewrite and the surface never settled — which looked like a blank page.
const onExtensions = await evalIn(app, `window.location.hash.includes("/extensions")`, { timeoutMs: 8_000 }).catch(() => false);
if (onExtensions !== true) {
await go(app, `/workspace/${workspaceId}/settings/extensions/connections`);
await new Promise((resolve) => setTimeout(resolve, 1_500));
continue;
}
await evalIn(app, "window.__openworkControl.execute('extensions.refresh-marketplace', null)", { awaitPromise: true, timeoutMs: 15_000 })
.catch(() => undefined);
await evalIn(app, `(() => {
const button = [...document.querySelectorAll('button')]
.find((element) => (element.textContent ?? '').trim() === 'Refresh' && !element.disabled);
button?.click();
return Boolean(button);
})()`).catch(() => undefined);
await new Promise((resolve) => setTimeout(resolve, 2_000));
}
// Say what WAS on screen: "card missing" alone cannot distinguish an
// unmounted surface from a connection den never offered this member.
const seen = await evalIn(app, `({
hash: window.location.hash,
buttons: [...document.querySelectorAll('button')].map((b) => (b.textContent ?? '').replace(/\\s+/g, ' ').trim()).filter(Boolean).slice(0, 40),
text: (document.body.innerText ?? '').replace(/\\s+/g, ' ').slice(0, 600),
})`).catch(() => null);
throw new Error(`The connection card ${name} never appeared. On screen: ${JSON.stringify(seen)}`);
}
/**
* Wait for text to be REALLY on screen, then bring it into view.
*
* waitForText proves the DOM contains it; a frame proves pixels. The detail
* panel paints slightly after its text lands, so screenshotting on the DOM
* signal alone captures a blank panel intermittently.
*/
export async function revealText(app: Surface, text: string, timeoutMs = 45_000): Promise<void> {
await waitFor(app, `(() => {
// innerText, not textContent: innerText is render-aware, so CSS
// text-transform (a badge styled uppercase) matches what waitForText saw
// and what a person reads. Comparing raw textContent can never agree.
const wanted = ${JSON.stringify(text)}.toLowerCase();
const nodes = [...document.querySelectorAll("button, h1, h2, h3, p, span, div")];
const node = nodes.reverse().find((element) => ((element.innerText ?? element.textContent ?? "")).toLowerCase().includes(wanted));
if (!node) return false;
const rect = node.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) return false;
node.scrollIntoView({ block: "center" });
return true;
})()`, { timeoutMs, label: `visible text ${JSON.stringify(text)}` });
// One paint after scrolling, so the frame is not captured mid-scroll.
await new Promise((resolve) => setTimeout(resolve, 750));
}
/**
* The connections surface, settled — polling before it mounts finds nothing.
*
* Navigates in a steer-back loop, not once: the app opens a freshly created
* session on its own, and that navigation can land AFTER our go(), parking
* the app on /session while a single 60s wait times out. Same race and same
* cure as waitForConnectionCard.
*/
export async function openConnectionsSurface(app: Surface, workspaceId: string): Promise<void> {
const deadline = Date.now() + 60_000;
while (Date.now() < deadline) {
const settled = await evalIn(
app,
`window.location.hash.includes("/extensions") && document.body.innerText.includes("Extensions")`,
{ timeoutMs: 8_000 },
).catch(() => false);
if (settled === true) return;
await go(app, `/workspace/${workspaceId}/settings/extensions/connections`).catch(() => undefined);
await new Promise((resolve) => setTimeout(resolve, 1_500));
}
// Say what WAS on screen: a bare timeout cannot distinguish a route that
// never rewrote from a shell the app steered somewhere else entirely.
const seen = await evalIn(app, `({
hash: window.location.hash,
title: document.title,
buttons: [...document.querySelectorAll('button')].map((b) => (b.textContent ?? '').replace(/\\s+/g, ' ').trim()).filter(Boolean).slice(0, 30),
text: (document.body.innerText ?? '').replace(/\\s+/g, ' ').slice(0, 500),
})`, { timeoutMs: 10_000 }).catch(() => null);
throw new Error(`The extensions connections surface never settled. On screen: ${JSON.stringify(seen)}`);
}
export async function currentHash(app: Surface): Promise<string> {
const hash = await resilientRead(app, "window.location.hash", { label: "location hash" });
if (typeof hash !== "string") throw new Error("CDP did not return window.location.hash as a string.");
return hash;
}
export async function enabledButtons(app: Surface): Promise<string[]> {
const labels = await resilientRead(app, `[...document.querySelectorAll('button')]
.filter((element) => !element.disabled)
.map((element) => (element.textContent ?? '').trim())
.filter(Boolean)`, { label: "enabled buttons" });
if (!Array.isArray(labels) || !labels.every((label) => typeof label === "string")) {
throw new Error("CDP did not return enabled button labels as strings.");
}
return labels;
}
/** Invoke a registered `window.__openworkControl` action, the product's own automation seam. */
export async function control(
app: Surface,
action: string,
args?: unknown,
opts: EvaluateOptions & { reattachAttempts?: number } = {},
): Promise<unknown> {
// Control actions are non-idempotent; a timeout must surface, not re-fire.
const result = await evalIn(
app,
`window.__openworkControl.execute(${JSON.stringify(action)}, ${JSON.stringify(args ?? null)})`,
{ timeoutMs: 60_000, reattachAttempts: 0, ...opts, awaitPromise: true },
);
if (!isRecord(result) || result.ok !== true) {
throw new Error(`Desktop control action ${action} failed: ${isRecord(result) ? String(result.error ?? "unknown") : "unknown"}`);
}
return result.result;
}
/**
* Wait until the app is interactive again — the same predicate the lifecycle
* layer applies when handing out a desktop handle. Use it after any action that
* navigates or creates a workspace/session, so assertions and frames never race
* the app's loading placeholders.
*/
export async function waitUntilInteractive(
app: Surface,
{ timeoutMs = 60_000 }: { timeoutMs?: number } = {},
): Promise<AppStateProbe> {
const deadline = Date.now() + timeoutMs;
let last: AppStateProbe = { controlReady: false, transitional: null, surface: null, workspaceId: null, route: "", text: "" };
while (Date.now() < deadline) {
try {
last = await probeAppStateOnSurface(app, {
timeoutMs: Math.min(DEFAULT_DOM_PROBE_TIMEOUT_MS, Math.max(0, deadline - Date.now())),
});
if (isInteractive(last)) return last;
} catch {
// A navigation can destroy the execution context mid-probe.
}
await sleep(Math.min(POLL_INTERVAL_MS, Math.max(0, deadline - Date.now())));
}
throw await timeoutError(app, `App did not become interactive after ${timeoutMs}ms: ${describeAppState(last)}`);
}
/** Wait until the page's visible text stops changing — the app is done working. */
export async function waitUntilTextStable(
app: Surface,
{ quietMs = 6_000, timeoutMs = 60_000 }: { quietMs?: number; timeoutMs?: number } = {},
): Promise<string> {
const deadline = Date.now() + timeoutMs;
let previous = "";
let stableSince = Date.now();
while (Date.now() < deadline) {
const currentValue = await evalIn(app, "document.body.innerText", {
timeoutMs: Math.min(DEFAULT_DOM_PROBE_TIMEOUT_MS, Math.max(0, deadline - Date.now())),
}).catch(() => previous);
const current = typeof currentValue === "string" ? currentValue : previous;
if (current !== previous) {
previous = current;
stableSince = Date.now();
} else if (Date.now() - stableSince >= quietMs) {
return current;
}
await sleep(Math.min(1_000, Math.max(0, deadline - Date.now())));
}
throw await timeoutError(app, `Visible text did not stabilize after ${timeoutMs}ms. Last text: ${JSON.stringify(previous.slice(0, 500))}.`);
}
+956
View File
@@ -0,0 +1,956 @@
import { spawn, spawnSync } from "node:child_process";
import net from "node:net";
import tls from "node:tls";
import { fileURLToPath } from "node:url";
import type { DetailedPeerCertificate } from "node:tls";
import { parseClientHelloVersions } from "@openwork/labs";
import type { ClientHelloParseResult, EgressLabHandle, EgressLabProfile } from "@openwork/labs";
import type { TlsFacts, TlsVersionFacts } from "@openwork/matchers";
export type { TlsFacts, TlsVersionFacts } from "@openwork/matchers";
export type DiagnosticVerdict = {
profile: EgressLabProfile;
text: string;
evidence: string;
source: "product" | "lab-corroboration";
available: boolean;
};
export type CommandResult = {
status: number;
stdout: string;
stderr: string;
error: string | null;
};
type ProductDiagnosticsOkPayload = {
status: "ok";
profile: string;
verdictText: string;
checks: unknown;
cloudProbe: unknown;
transportProbe: unknown;
};
type ProductDiagnosticsErrorPayload = {
status: "error";
message: string;
stack: string | null;
};
type ProductDiagnosticsPayload = ProductDiagnosticsOkPayload | ProductDiagnosticsErrorPayload;
type RuntimeProbePayload = {
reproVisible: boolean;
fileCount: number;
extraCount: number;
systemCount: number;
defaultCount: number;
bundledCount: number;
};
type TlsHandshakeProbe = {
ok: boolean;
protocol: string | null;
errorCode: string | null;
};
type ServedCertificateEvidence = {
subjectCN: string | null;
issuerCN: string | null;
selfIssued: boolean;
};
type TransportProbe = {
verifiedHandshake: "ok" | "failed";
verifyErrorCode: string | null;
servedChain: ServedCertificateEvidence[];
servedChainLength: number | null;
};
type TlsAttempt = {
ok: true;
certificate: DetailedPeerCertificate | null;
} | {
ok: false;
errorCode: string | null;
certificate: DetailedPeerCertificate | null;
};
const REPO_ROOT = fileURLToPath(new URL("../../../..", import.meta.url));
const PRODUCT_DIAGNOSTICS_TIMEOUT_MS = 30_000;
const TLS_RUNTIME_PROBE_TIMEOUT_MS = 1_500;
const TLS_RUNTIME_COMMAND_TIMEOUT_MS = 8_000;
const PRODUCT_DIAGNOSTICS_SCRIPT = String.raw`
const profile = process.env.EGRESS_LAB_PRODUCT_PROFILE || "unknown";
const endpoint = process.env.EGRESS_LAB_PRODUCT_ENDPOINT || "";
const timeoutMs = Number(process.env.EGRESS_LAB_PRODUCT_TIMEOUT_MS || "1500");
function checkLine(check) {
return [
check.id,
check.status,
check.code,
check.message,
"Action: " + check.action,
].filter(Boolean).join(" | ");
}
async function main() {
const transportModule = await import("./apps/server/src/agent-context-transport-probe.ts");
const cloudModule = await import("./apps/server/src/agent-context-cloud-probe.ts");
const diagnosticsModule = await import("./apps/server/src/agent-context-diagnostics.ts");
const cloudProbe = await cloudModule.probeOpenworkCloudCatalog({
workspaceId: "ws_egress_lab_product_diagnostics",
workspaceType: "local",
runtimeConfigAvailable: true,
config: {
type: "remote",
enabled: true,
url: endpoint,
headers: { Authorization: "Bearer ow_diagnostics_token_abcdefghijklmnopqrstuvwxyz" },
},
engineRegistration: { status: "failed", source: "transport_failure", recordAgeMs: 1000 },
requestId: "11111111-1111-4111-8111-111111111111",
timeoutMs,
env: process.env,
});
const transportProbe = await transportModule.probeCloudEndpointTransport({
endpointUrl: endpoint,
performProbe: endpoint.startsWith("https:"),
timeoutMs,
env: process.env,
});
const checks = [
diagnosticsModule.cloudCatalogCheck(cloudProbe),
diagnosticsModule.cloudDifferentialCheck(cloudProbe, false),
diagnosticsModule.cloudEndpointTransportCheck(transportProbe, false),
];
console.log(JSON.stringify({
status: "ok",
profile,
verdictText: checks.map(checkLine).join("\n"),
checks,
cloudProbe,
transportProbe,
}));
}
main().catch((error) => {
console.log(JSON.stringify({
status: "error",
message: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack ?? null : null,
}));
process.exitCode = 1;
});
`;
const TLS_RUNTIME_PROBE_SCRIPT = String.raw`
const fs = require("node:fs");
const tls = require("node:tls");
const url = new URL(process.env.EGRESS_LAB_TLS_URL || "");
const timeoutMs = Number(process.env.EGRESS_LAB_TLS_TIMEOUT_MS || "1500");
function errorCode(error) {
if (error && typeof error === "object" && typeof error.code === "string") return error.code;
if (error && typeof error === "object" && error.cause && typeof error.cause.code === "string") return error.cause.code;
if (error && typeof error === "object" && typeof error.name === "string" && error.name === "TimeoutError") return "TimeoutError";
if (error instanceof Error) return error.message;
return String(error);
}
async function main() {
if (process.env.EGRESS_LAB_TLS_MODE === "fetch") {
try {
const response = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) });
console.log(JSON.stringify({ ok: true, status: response.status, body: await response.text() }));
return;
} catch (error) {
console.log(JSON.stringify({
ok: false,
name: error && typeof error === "object" && "name" in error ? error.name : null,
message: error instanceof Error ? error.message : String(error),
causeCode: error && typeof error === "object" && error.cause && typeof error.cause.code === "string" ? error.cause.code : null,
code: errorCode(error),
}));
process.exitCode = 1;
return;
}
}
const caPath = process.env.NODE_EXTRA_CA_CERTS || "";
const options = {
host: url.hostname,
port: Number(url.port) || 443,
servername: url.hostname,
ca: caPath ? fs.readFileSync(caPath, "utf8") : undefined,
rejectUnauthorized: true,
ALPNProtocols: ["http/1.1"],
};
if (process.env.EGRESS_LAB_TLS_MIN_VERSION) options.minVersion = process.env.EGRESS_LAB_TLS_MIN_VERSION;
if (process.env.EGRESS_LAB_TLS_MAX_VERSION) options.maxVersion = process.env.EGRESS_LAB_TLS_MAX_VERSION;
await new Promise((resolve) => {
let settled = false;
const socket = tls.connect(options);
const finish = (payload, status) => {
if (settled) return;
settled = true;
socket.destroy();
console.log(JSON.stringify(payload));
process.exitCode = status;
resolve();
};
socket.once("secureConnect", () => finish({ ok: true, protocol: typeof socket.getProtocol === "function" ? socket.getProtocol() : null }, 0));
socket.once("timeout", () => finish({ ok: false, errorCode: "ETIMEDOUT" }, 1));
socket.once("error", (error) => finish({ ok: false, errorCode: errorCode(error) }, 1));
socket.setTimeout(timeoutMs);
});
}
main();
`;
const TLS_CLIENT_HELLO_CAPTURE_SCRIPT = String.raw`
const tls = require("node:tls");
const socket = tls.connect({
host: "127.0.0.1",
port: Number(process.env.EGRESS_LAB_CAPTURE_PORT || "0"),
servername: "localhost",
minVersion: "TLSv1.2",
maxVersion: "TLSv1.2",
rejectUnauthorized: true,
});
socket.on("error", () => undefined);
socket.setTimeout(1000, () => socket.destroy());
setTimeout(() => process.exit(0), 1200).unref?.();
`;
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function messageText(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
function isRuntimeProbePayload(value: unknown): value is RuntimeProbePayload {
return isRecord(value)
&& typeof value.reproVisible === "boolean"
&& typeof value.fileCount === "number"
&& typeof value.extraCount === "number"
&& typeof value.systemCount === "number"
&& typeof value.defaultCount === "number"
&& typeof value.bundledCount === "number";
}
function isProductDiagnosticsOkPayload(value: unknown): value is ProductDiagnosticsOkPayload {
return isRecord(value)
&& value.status === "ok"
&& typeof value.profile === "string"
&& typeof value.verdictText === "string"
&& Object.hasOwn(value, "checks")
&& Object.hasOwn(value, "cloudProbe")
&& Object.hasOwn(value, "transportProbe");
}
function isProductDiagnosticsErrorPayload(value: unknown): value is ProductDiagnosticsErrorPayload {
return isRecord(value)
&& value.status === "error"
&& typeof value.message === "string"
&& (typeof value.stack === "string" || value.stack === null);
}
export function productDiagnosticsPayloadFromStdout(stdout: string): ProductDiagnosticsPayload | null {
try {
const parsed = JSON.parse(stdout.trim());
if (isProductDiagnosticsOkPayload(parsed) || isProductDiagnosticsErrorPayload(parsed)) return parsed;
return null;
} catch {
return null;
}
}
function endpointForDiagnostics(lab: EgressLabHandle): string {
const url = new URL(lab.url);
url.pathname = "/mcp/agent";
return url.toString();
}
function productDiagnosticsCaPath(lab: EgressLabHandle): string | undefined {
if (lab.profile === "tls12-only" || lab.profile === "broken-chain" || lab.profile === "healthy") return lab.caPath;
return undefined;
}
function productDiagnosticsEnv(lab: EgressLabHandle, baseEnv: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
const env: NodeJS.ProcessEnv = {
...baseEnv,
EGRESS_LAB_PRODUCT_PROFILE: lab.profile,
EGRESS_LAB_PRODUCT_ENDPOINT: endpointForDiagnostics(lab),
EGRESS_LAB_PRODUCT_TIMEOUT_MS: "1500",
};
const caPath = productDiagnosticsCaPath(lab);
if (caPath) env.NODE_EXTRA_CA_CERTS = caPath;
return env;
}
export function runBunProductDiagnostics(lab: EgressLabHandle): Promise<CommandResult> {
return new Promise((resolve) => {
let stdout = "";
let stderr = "";
let settled = false;
let timeout: ReturnType<typeof setTimeout> | undefined;
const child = spawn("bun", ["--conditions", "development", "--eval", PRODUCT_DIAGNOSTICS_SCRIPT], {
cwd: REPO_ROOT,
env: productDiagnosticsEnv(lab, process.env),
});
const finish = (result: CommandResult) => {
if (settled) return;
settled = true;
if (timeout) clearTimeout(timeout);
resolve(result);
};
timeout = setTimeout(() => {
child.kill("SIGKILL");
finish({ status: -1, stdout, stderr, error: `Timed out after ${PRODUCT_DIAGNOSTICS_TIMEOUT_MS}ms` });
}, PRODUCT_DIAGNOSTICS_TIMEOUT_MS);
child.stdout.setEncoding("utf8");
child.stderr.setEncoding("utf8");
child.stdout.on("data", (chunk) => {
stdout += String(chunk);
});
child.stderr.on("data", (chunk) => {
stderr += String(chunk);
});
child.on("error", (error) => finish({ status: -1, stdout, stderr, error: error.message }));
child.on("close", (code) => finish({ status: typeof code === "number" ? code : -1, stdout, stderr, error: null }));
});
}
export function productDiagnosticsPrecondition(env: NodeJS.ProcessEnv = process.env): string | null {
const bun = spawnSync("bun", ["--version"], { env, encoding: "utf8", timeout: 5_000 });
if (bun.status !== 0) {
const reason = bun.error?.message ?? bun.stderr?.trim() ?? "bun --version failed";
return `Bun is required to import OpenWork's shipped TypeScript diagnostics modules for product-verdict egress proofs (${reason}).`;
}
const openssl = spawnSync("openssl", ["version"], { env, encoding: "utf8", timeout: 5_000 });
if (openssl.status !== 0) {
const reason = openssl.error?.message ?? openssl.stderr?.trim() ?? "openssl version failed";
return `OpenSSL is required to mint per-run egress lab certificates for product-verdict egress proofs (${reason}).`;
}
return null;
}
function runTlsRuntimeProbe(input: {
command: string;
label: string;
lab: EgressLabHandle;
env: NodeJS.ProcessEnv;
mode: "tls" | "fetch";
minVersion?: "TLSv1.2" | "TLSv1.3";
maxVersion?: "TLSv1.2" | "TLSv1.3";
nodeOptions?: string;
}): Promise<CommandResult> {
return new Promise((resolve) => {
let stdout = "";
let stderr = "";
let settled = false;
let timeout: ReturnType<typeof setTimeout> | undefined;
const childEnv: NodeJS.ProcessEnv = {
...input.env,
EGRESS_LAB_TLS_URL: input.lab.url,
EGRESS_LAB_TLS_MODE: input.mode,
EGRESS_LAB_TLS_TIMEOUT_MS: String(TLS_RUNTIME_PROBE_TIMEOUT_MS),
};
if (input.lab.caPath) childEnv.NODE_EXTRA_CA_CERTS = input.lab.caPath;
if (input.minVersion) childEnv.EGRESS_LAB_TLS_MIN_VERSION = input.minVersion;
if (input.maxVersion) childEnv.EGRESS_LAB_TLS_MAX_VERSION = input.maxVersion;
if (input.nodeOptions) childEnv.NODE_OPTIONS = input.nodeOptions;
const child = spawn(input.command, ["--eval", TLS_RUNTIME_PROBE_SCRIPT], {
cwd: REPO_ROOT,
env: childEnv,
});
const finish = (result: CommandResult) => {
if (settled) return;
settled = true;
if (timeout) clearTimeout(timeout);
resolve(result);
};
timeout = setTimeout(() => {
child.kill("SIGKILL");
finish({ status: -1, stdout, stderr, error: `${input.label} timed out after ${TLS_RUNTIME_COMMAND_TIMEOUT_MS}ms` });
}, TLS_RUNTIME_COMMAND_TIMEOUT_MS);
child.stdout.setEncoding("utf8");
child.stderr.setEncoding("utf8");
child.stdout.on("data", (chunk) => {
stdout += String(chunk);
});
child.stderr.on("data", (chunk) => {
stderr += String(chunk);
});
child.on("error", (error) => finish({ status: -1, stdout, stderr, error: error.message }));
child.on("close", (code) => finish({ status: typeof code === "number" ? code : -1, stdout, stderr, error: null }));
});
}
function listenCaptureServer(server: net.Server): Promise<number> {
return new Promise((resolve, reject) => {
const onError = (error: Error) => reject(error);
server.once("error", onError);
server.listen(0, "127.0.0.1", () => {
server.off("error", onError);
const address = server.address();
if (address && typeof address === "object") {
resolve(address.port);
return;
}
reject(new Error("ClientHello capture server did not bind a TCP port."));
});
});
}
function closeCaptureServer(server: net.Server): Promise<void> {
return new Promise((resolve) => {
server.close(() => resolve());
});
}
async function captureRuntimeClientHello(command: string, env: NodeJS.ProcessEnv): Promise<ClientHelloParseResult | { kind: "not-captured"; reason: string }> {
const server = net.createServer();
const port = await listenCaptureServer(server);
let child: ReturnType<typeof spawn> | null = null;
const captured = new Promise<ClientHelloParseResult | { kind: "not-captured"; reason: string }>((resolve) => {
const timer = setTimeout(() => {
resolve({ kind: "not-captured", reason: "Timed out waiting for TLS ClientHello." });
}, TLS_RUNTIME_COMMAND_TIMEOUT_MS);
server.once("connection", (socket) => {
const chunks: Buffer[] = [];
let total = 0;
socket.on("data", (chunk: Buffer) => {
chunks.push(chunk);
total += chunk.byteLength;
const parsed = parseClientHelloVersions(Buffer.concat(chunks, total));
if (parsed.kind === "incomplete") return;
clearTimeout(timer);
socket.destroy();
resolve(parsed);
});
socket.on("error", () => undefined);
});
server.once("error", (error) => {
clearTimeout(timer);
resolve({ kind: "not-captured", reason: error.message });
});
});
try {
child = spawn(command, ["--eval", TLS_CLIENT_HELLO_CAPTURE_SCRIPT], {
cwd: REPO_ROOT,
env: { ...env, EGRESS_LAB_CAPTURE_PORT: String(port) },
});
child.on("error", () => undefined);
const result = await captured;
return result;
} finally {
if (child) child.kill("SIGKILL");
await closeCaptureServer(server);
}
}
function outputCommandResult(result: CommandResult): Record<string, unknown> {
return {
status: result.status,
error: result.error,
stdout: result.stdout.trim(),
stderr: result.stderr.trim(),
};
}
function commandSucceededWith(result: CommandResult, needle: string): boolean {
return result.status === 0 && result.stdout.includes(needle);
}
function commandFailedWith(result: CommandResult, needle: string): boolean {
return result.status !== 0 && result.stdout.includes(needle);
}
export interface BunTls12PinningFacts {
finding: string;
nodeTls12: Record<string, unknown>;
bunTls12: Record<string, unknown>;
nodeFetchPinned: Record<string, unknown>;
bunFetchPinned: Record<string, unknown>;
nodeClientHello: ClientHelloParseResult | { kind: "not-captured"; reason: string };
bunClientHello: ClientHelloParseResult | { kind: "not-captured"; reason: string };
nodeTls12Ok: boolean;
bunTls12Stalled: boolean;
nodeFetchPinnedOk: boolean;
bunFetchPinnedStalled: boolean;
nodeClientHelloTls12Only: boolean;
bunClientHelloOffersTls13: boolean;
}
export async function readBunTls12PinningFinding(lab: EgressLabHandle, env: NodeJS.ProcessEnv = process.env): Promise<BunTls12PinningFacts> {
const nodeTls12 = await runTlsRuntimeProbe({
command: process.execPath,
label: "node node:tls TLSv1.2",
lab,
env,
mode: "tls",
minVersion: "TLSv1.2",
maxVersion: "TLSv1.2",
});
const bunTls12 = await runTlsRuntimeProbe({
command: "bun",
label: "bun node:tls TLSv1.2",
lab,
env,
mode: "tls",
minVersion: "TLSv1.2",
maxVersion: "TLSv1.2",
});
const nodeFetchPinned = await runTlsRuntimeProbe({
command: process.execPath,
label: "node fetch NODE_OPTIONS=--tls-max-v1.2",
lab,
env,
mode: "fetch",
nodeOptions: "--tls-max-v1.2",
});
const bunFetchPinned = await runTlsRuntimeProbe({
command: "bun",
label: "bun fetch NODE_OPTIONS=--tls-max-v1.2",
lab,
env,
mode: "fetch",
nodeOptions: "--tls-max-v1.2",
});
const nodeClientHello = await captureRuntimeClientHello(process.execPath, env);
const bunClientHello = await captureRuntimeClientHello("bun", env);
const nodeClientHelloTls12Only = nodeClientHello.kind === "parsed" && !nodeClientHello.offersTls13 && nodeClientHello.supportedVersionLabels.includes("TLSv1.2");
const bunClientHelloOffersTls13 = bunClientHello.kind === "parsed" && bunClientHello.offersTls13 && bunClientHello.supportedVersionLabels.includes("TLSv1.3");
return {
finding: "Bun 1.3.x does not currently honor Node-style TLS 1.2 pinning in node:tls or fetch for this lab; it still offers TLS 1.3 and stalls.",
nodeTls12: outputCommandResult(nodeTls12),
bunTls12: outputCommandResult(bunTls12),
nodeFetchPinned: outputCommandResult(nodeFetchPinned),
bunFetchPinned: outputCommandResult(bunFetchPinned),
nodeClientHello,
bunClientHello,
nodeTls12Ok: commandSucceededWith(nodeTls12, '"protocol":"TLSv1.2"'),
bunTls12Stalled: commandFailedWith(bunTls12, "ETIMEDOUT"),
nodeFetchPinnedOk: commandSucceededWith(nodeFetchPinned, '"status":200'),
bunFetchPinnedStalled: commandFailedWith(bunFetchPinned, "Timeout"),
nodeClientHelloTls12Only,
bunClientHelloOffersTls13,
};
}
function endpointForTransport(lab: EgressLabHandle): URL {
const url = new URL(lab.url);
url.pathname = "/mcp/agent";
return url;
}
// These errors prove the TLS handshake reached certificate verification.
const CERTIFICATE_VERIFICATION_ERROR_CODES = new Set([
"UNABLE_TO_GET_ISSUER_CERT",
"UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
"UNABLE_TO_VERIFY_LEAF_SIGNATURE",
"CERT_HAS_EXPIRED",
"CERT_NOT_YET_VALID",
"DEPTH_ZERO_SELF_SIGNED_CERT",
"SELF_SIGNED_CERT_IN_CHAIN",
"CERT_UNTRUSTED",
"CERT_REVOKED",
"CERT_SIGNATURE_FAILURE",
"INVALID_CA",
"UNABLE_TO_GET_CRL",
"HOSTNAME_MISMATCH",
"ERR_TLS_CERT_ALTNAME_INVALID",
]);
function probeTlsVersion(
target: { host: string; port: number; servername?: string; ca?: string | string[]; rejectUnauthorized?: boolean },
version: "TLSv1.2" | "TLSv1.3",
timeoutMs: number,
): Promise<TlsVersionFacts> {
return new Promise((resolve) => {
let settled = false;
const finish = (result: TlsVersionFacts) => {
if (settled) return;
settled = true;
socket.destroy();
resolve(result);
};
const socket = tls.connect({
host: target.host,
port: target.port,
servername: target.servername ?? target.host,
minVersion: version,
maxVersion: version,
ca: target.ca,
rejectUnauthorized: target.rejectUnauthorized ?? true,
});
socket.once("secureConnect", () => {
const chainVerified = socket.authorized === true;
const authorizationError = socket.authorizationError;
const authorizationCode = isRecord(authorizationError) && typeof authorizationError.code === "string"
? authorizationError.code
: String(authorizationError ?? "unauthorized");
finish({
handshakeOk: true,
chainVerified,
protocol: socket.getProtocol(),
errorCode: chainVerified ? null : authorizationCode,
stalled: false,
});
});
socket.once("timeout", () => finish({ handshakeOk: false, chainVerified: false, protocol: null, errorCode: "ETIMEDOUT", stalled: true }));
socket.once("error", (error) => {
const code = isRecord(error) && typeof error.code === "string" ? error.code : messageText(error);
if (CERTIFICATE_VERIFICATION_ERROR_CODES.has(code)) {
finish({ handshakeOk: true, chainVerified: false, protocol: socket.getProtocol() ?? null, errorCode: code, stalled: false });
return;
}
finish({ handshakeOk: false, chainVerified: false, protocol: null, errorCode: code, stalled: false });
});
socket.setTimeout(timeoutMs);
});
}
export async function probeTls(
target: { host: string; port: number; servername?: string; ca?: string | string[]; rejectUnauthorized?: boolean },
opts: { timeoutMs?: number } = {},
): Promise<TlsFacts> {
const timeoutMs = opts.timeoutMs ?? 1_500;
const [tls12, tls13] = await Promise.all([
probeTlsVersion(target, "TLSv1.2", timeoutMs),
probeTlsVersion(target, "TLSv1.3", timeoutMs),
]);
return { tls12, tls13 };
}
function tlsHandshake(lab: EgressLabHandle, version: "TLSv1.2" | "TLSv1.3", timeoutMs = 1500): Promise<TlsHandshakeProbe> {
const url = new URL(lab.url);
return new Promise((resolve) => {
let settled = false;
const finish = (result: TlsHandshakeProbe) => {
if (settled) return;
settled = true;
socket.destroy();
resolve(result);
};
const socket = tls.connect({
host: url.hostname,
port: Number(url.port) || 443,
servername: url.hostname,
minVersion: version,
maxVersion: version,
ca: lab.rootPem,
rejectUnauthorized: true,
});
socket.once("secureConnect", () => finish({ ok: true, protocol: socket.getProtocol(), errorCode: null }));
socket.once("timeout", () => finish({ ok: false, protocol: null, errorCode: "ETIMEDOUT" }));
socket.once("error", (error) => {
const code = isRecord(error) && typeof error.code === "string" ? error.code : messageText(error);
finish({ ok: false, protocol: null, errorCode: code });
});
socket.setTimeout(timeoutMs);
});
}
function tlsErrorCode(error: unknown): string | null {
if (isRecord(error) && typeof error.code === "string") return error.code;
if (error instanceof Error) return error.message;
return String(error);
}
function connectTransport(input: { url: URL; ca: string | undefined; inspect: boolean; timeoutMs: number }): Promise<TlsAttempt> {
return new Promise((resolve) => {
let settled = false;
const socket = tls.connect({
host: input.url.hostname,
port: Number(input.url.port) || 443,
servername: input.url.hostname,
ca: input.ca,
rejectUnauthorized: true,
ALPNProtocols: ["http/1.1"],
});
const finish = (attempt: TlsAttempt) => {
if (settled) return;
settled = true;
socket.destroy();
resolve(attempt);
};
socket.once("secureConnect", () => {
const certificate = input.inspect ? socket.getPeerCertificate(true) : null;
finish({ ok: true, certificate });
});
socket.once("timeout", () => finish({ ok: false, errorCode: "ETIMEDOUT", certificate: null }));
socket.once("error", (error) => {
const certificate = input.inspect ? socket.getPeerCertificate(true) : null;
finish({ ok: false, errorCode: tlsErrorCode(error), certificate });
});
socket.setTimeout(input.timeoutMs);
});
}
function certificateField(value: unknown): string | null {
return typeof value === "string" && value.trim() ? value.trim().slice(0, 120) : null;
}
function servedChainFromCertificate(certificate: DetailedPeerCertificate | null): ServedCertificateEvidence[] {
if (!certificate || Object.keys(certificate).length === 0) return [];
const chain: ServedCertificateEvidence[] = [];
const seen = new Set<DetailedPeerCertificate>();
let current: DetailedPeerCertificate | undefined = certificate;
while (current && chain.length < 5 && !seen.has(current)) {
seen.add(current);
const subjectCN = certificateField(current.subject?.CN);
const issuerCN = certificateField(current.issuer?.CN);
chain.push({
subjectCN,
issuerCN,
selfIssued: current.issuerCertificate === current || (subjectCN !== null && subjectCN === issuerCN),
});
if (!current.issuerCertificate || current.issuerCertificate === current) break;
current = current.issuerCertificate;
}
return chain;
}
async function probeTransport(lab: EgressLabHandle, ca: string | undefined): Promise<TransportProbe> {
const url = endpointForTransport(lab);
const verified = await connectTransport({ url, ca, inspect: true, timeoutMs: 1500 });
if (verified.ok) return { verifiedHandshake: "ok", verifyErrorCode: null, servedChain: [], servedChainLength: null };
const servedChain = servedChainFromCertificate(verified.certificate);
return {
verifiedHandshake: "failed",
verifyErrorCode: verified.errorCode,
servedChain,
servedChainLength: servedChain.length,
};
}
async function diagnoseTlsLab(lab: EgressLabHandle): Promise<DiagnosticVerdict> {
const useCa = lab.profile === "broken-chain" || lab.profile === "tls12-only" || lab.profile === "healthy";
const probe = await probeTransport(lab, useCa ? lab.rootPem : undefined);
const lines = [`transport=${JSON.stringify(probe, null, 2)}`];
if (lab.profile === "tls12-only") {
const tls13 = await tlsHandshake(lab, "TLSv1.3");
const tls12 = await tlsHandshake(lab, "TLSv1.2");
lines.push(`tls13=${JSON.stringify(tls13)}`);
lines.push(`tls12=${JSON.stringify(tls12)}`);
const text = !tls13.ok && tls13.errorCode === "ETIMEDOUT" && tls12.ok
? "LIKELY TLS VERSION/HANDSHAKE FAULT: TLS 1.3 ClientHello stalls or times out, while TLS 1.2 completes. Force TLS 1.2 or fix the egress proxy."
: "TLS VERSION VERDICT INCONCLUSIVE: the TLS 1.3 and TLS 1.2 probes did not match the expected stall/success split.";
return { profile: lab.profile, text, evidence: lines.join("\n"), source: "lab-corroboration", available: true };
}
const servedIssuer = probe.servedChain[0]?.issuerCN ?? "";
const issuerLower = servedIssuer.toLowerCase();
if (lab.profile === "intercept" && /corporate|interception|proxy|mitm/u.test(issuerLower)) {
return {
profile: lab.profile,
text: `LIKELY TLS INTERCEPTION: endpoint leaf is re-signed by ${servedIssuer}, not the expected public issuer. Install or pass the corporate root, or bypass TLS inspection for OpenWork hosts.`,
evidence: lines.join("\n"),
source: "lab-corroboration",
available: true,
};
}
const code = probe.verifyErrorCode ?? "";
if (lab.profile === "broken-chain" && /UNABLE_TO_VERIFY_LEAF_SIGNATURE|unable|cert|certificate/iu.test(code)) {
return {
profile: lab.profile,
text: `LIKELY MISSING INTERMEDIATE OR UNTRUSTED ROOT: verified TLS failed with ${code}; served-chain length ${probe.servedChainLength ?? 0}. Fetch the AIA intermediate or fix the server fullchain.`,
evidence: lines.join("\n"),
source: "lab-corroboration",
available: true,
};
}
if (probe.verifiedHandshake === "ok") {
return { profile: lab.profile, text: "NO EGRESS/TLS FAULT DETECTED: credential-free TLS handshake verified.", evidence: lines.join("\n"), source: "lab-corroboration", available: true };
}
return {
profile: lab.profile,
text: `LIKELY NETWORK/TLS FAULT: transport probe failed with ${probe.verifyErrorCode ?? "unknown error"}.`,
evidence: lines.join("\n"),
source: "lab-corroboration",
available: true,
};
}
export interface DeniedHostFacts {
status: number;
text: string;
errorCode: string | null;
host: string | null;
}
export async function readDeniedHostFacts(
lab: EgressLabHandle,
targetUrl = "https://github.com/different-ai/openwork/releases/latest",
): Promise<DeniedHostFacts> {
const url = new URL("/fetch", lab.url);
url.searchParams.set("url", targetUrl);
const response = await fetch(url);
const text = await response.text();
let body: unknown = null;
try {
body = JSON.parse(text);
} catch {
body = null;
}
return {
status: response.status,
text,
errorCode: isRecord(body) && typeof body.error === "string" ? body.error : null,
host: isRecord(body) && typeof body.host === "string" ? body.host : null,
};
}
async function diagnoseDenyLab(lab: EgressLabHandle): Promise<DiagnosticVerdict> {
// Exact-match lookup (not a substring test) so the denied host is unambiguous.
const host = lab.deniedHosts.find((entry) => entry === "github.com") ?? lab.deniedHosts[0] ?? "github.com";
const facts = await readDeniedHostFacts(lab, `https://${host}/different-ai/openwork/releases/latest`);
const text = facts.status === 451
? `BLOCKED HOST / PROXY DENY: ${host} is blocked by the selective-deny profile; docs/enterprise/outbound-access.json names the required host and its blocked effect.`
: `DENY VERDICT INCONCLUSIVE: expected ${host} to be blocked, got HTTP ${facts.status}.`;
return { profile: lab.profile, text, evidence: [`status=${facts.status}`, facts.text].join("\n"), source: "lab-corroboration", available: true };
}
async function diagnoseBlipLab(lab: EgressLabHandle): Promise<DiagnosticVerdict> {
const first = await fetch(new URL("/diagnostics", lab.url));
await first.arrayBuffer();
const second = await fetch(new URL("/diagnostics", lab.url));
await second.arrayBuffer();
const text = first.status === 401 && second.ok
? "TRANSIENT AUTH BLIP: one 401 was observed and the next request recovered; treat as retryable/unavailable unless the 401 has the Den error envelope."
: `BLIP VERDICT INCONCLUSIVE: first=${first.status} second=${second.status}.`;
return { profile: lab.profile, text, evidence: `first=${first.status}\nsecond=${second.status}`, source: "lab-corroboration", available: true };
}
async function diagnoseRedirectLab(lab: EgressLabHandle): Promise<DiagnosticVerdict> {
const response = await fetch(new URL("/redirect-chain/start", lab.url), { redirect: "manual" });
const location = response.headers.get("location") ?? "";
const text = response.status >= 300 && response.status < 400
? `REDIRECT CHAIN DETECTED: first hop returned ${response.status} to ${location}; diagnostics must follow or report redirect targets explicitly.`
: `REDIRECT VERDICT INCONCLUSIVE: expected manual redirect, got HTTP ${response.status}.`;
return { profile: lab.profile, text, evidence: `status=${response.status}\nlocation=${location}`, source: "lab-corroboration", available: true };
}
async function diagnoseSlowLab(lab: EgressLabHandle): Promise<DiagnosticVerdict> {
const started = Date.now();
const response = await fetch(new URL("/slow", lab.url));
const reader = response.body?.getReader();
const first = reader ? await reader.read() : null;
await reader?.cancel().catch(() => undefined);
const elapsedMs = Date.now() - started;
const text = elapsedMs > 100 || first?.value?.byteLength
? `SLOW LINK DETECTED: first bytes arrived after ${elapsedMs}ms; installer/download diagnostics should report throughput and latency, not a generic fetch failure.`
: "SLOW VERDICT INCONCLUSIVE: no delayed bytes were observed.";
return { profile: lab.profile, text, evidence: `status=${response.status}\nelapsedMs=${elapsedMs}\nfirstChunk=${first?.value?.byteLength ?? 0}`, source: "lab-corroboration", available: true };
}
export async function diagnoseEgressLabCorroboration(lab: EgressLabHandle): Promise<DiagnosticVerdict> {
if (lab.profile === "deny") return diagnoseDenyLab(lab);
if (lab.profile === "blip") return diagnoseBlipLab(lab);
if (lab.profile === "redirect-chain") return diagnoseRedirectLab(lab);
if (lab.profile === "slow") return diagnoseSlowLab(lab);
return diagnoseTlsLab(lab);
}
export async function diagnoseEgressLabProduct(lab: EgressLabHandle): Promise<DiagnosticVerdict> {
const result = await runBunProductDiagnostics(lab);
const payload = productDiagnosticsPayloadFromStdout(result.stdout);
const commandEvidence = {
command: "bun --conditions development --eval <product diagnostics probe>",
cwd: REPO_ROOT,
status: result.status,
error: result.error,
stderr: result.stderr,
stdoutParsed: payload,
};
if (result.status !== 0 || !payload || payload.status === "error") {
const message = payload?.status === "error" ? payload.message : (result.error ?? result.stderr.trim()) || "product diagnostics subprocess failed";
return {
profile: lab.profile,
text: `PRODUCT DIAGNOSTICS UNAVAILABLE: ${message}`,
evidence: JSON.stringify(commandEvidence, null, 2),
source: "product",
available: false,
};
}
return {
profile: lab.profile,
text: payload.verdictText,
evidence: JSON.stringify({
command: commandEvidence.command,
cwd: commandEvidence.cwd,
status: commandEvidence.status,
checks: payload.checks,
cloudProbe: payload.cloudProbe,
transportProbe: payload.transportProbe,
}, null, 2),
source: "product",
available: true,
};
}
export async function diagnoseEgressLab(lab: EgressLabHandle): Promise<DiagnosticVerdict> {
return diagnoseEgressLabProduct(lab);
}
const RUNTIME_CA_PROBE = String.raw`
const { X509Certificate } = require("node:crypto");
const fs = require("node:fs");
let tls;
try { tls = require("node:tls"); } catch { tls = {}; }
const needle = (process.env.OPENWORK_TLS_REPRO_CA_MATCH || "OpenWork Egress Lab").toLowerCase();
function countMatching(certs) {
let count = 0;
for (const pem of certs || []) {
try {
const cert = new X509Certificate(pem);
if (cert.subject.toLowerCase().includes(needle)) count += 1;
} catch {}
}
return count;
}
function certs(scope) {
try {
if (typeof tls.getCACertificates !== "function") return [];
const result = tls.getCACertificates(scope);
return Array.isArray(result) ? result : [];
} catch {
return [];
}
}
function fileCerts() {
const file = process.env.NODE_EXTRA_CA_CERTS || "";
if (!file) return [];
try {
return fs.readFileSync(file, "utf8").match(/-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/g) || [];
} catch {
return [];
}
}
const payload = {
fileCount: countMatching(fileCerts()),
extraCount: countMatching(certs("extra")),
systemCount: countMatching(certs("system")),
defaultCount: countMatching(certs("default")),
bundledCount: countMatching(certs("bundled")),
};
payload.reproVisible = payload.fileCount + payload.extraCount + payload.systemCount + payload.defaultCount + payload.bundledCount > 0;
console.log(JSON.stringify(payload));
process.exitCode = payload.reproVisible ? 0 : 1;
`;
function parseRuntimeProbe(stdout: string): RuntimeProbePayload | null {
try {
const parsed = JSON.parse(stdout.trim());
return isRuntimeProbePayload(parsed) ? parsed : null;
} catch {
return null;
}
}
+11
View File
@@ -0,0 +1,11 @@
export * from "./den.ts"
export * from "./browser-handoff.ts";
export * from "./cloud-plugins.ts";
export * from "./desktop.ts";
export * from "./desktop-boot.ts";
export * from "./diagnostics.ts";
export * from "./onboarding.ts";
export * from "./composer.ts";
export * from "./models.ts";
export * from "./skills.ts";
export * from "./sessions.ts";
+319
View File
@@ -0,0 +1,319 @@
import type { Surface } from "@openwork/cdp";
import type { DenSession } from "./den.ts";
import { denFetch } from "./den.ts";
import { evalIn, fill, waitFor } from "./desktop.ts";
const MODEL_DIALOG = '[data-slot="dialog-content"]';
const MODEL_SEARCH_INPUT = 'input[placeholder="Search providers and models..."]';
export interface ModelFacts {
id: string;
name: string;
providerName: string;
selected: boolean;
selectable: boolean;
}
export interface ModelRecoveryFacts {
emptyMessageVisible: boolean;
retryVisible: boolean;
refreshVisible: boolean;
connectProviderVisible: boolean;
warningVisible: boolean;
guidanceVisible: boolean;
pickerOpen: boolean;
runTaskEnabled: boolean;
noticeHeight: number | null;
noticeWhiteSpace: string | null;
}
export interface UnavailableModelSeed {
unavailableModelId: string;
availableModelId: string;
availableModelName: string;
availableProviderName: string;
}
export async function readCurrentOrganizationMemberId(session: DenSession): Promise<string> {
const result = await denFetch(session, "/v1/org", {
headers: { authorization: `Bearer ${session.token}` },
});
const currentMember = isRecord(result.body) && isRecord(result.body.currentMember)
? result.body.currentMember
: null;
const memberId = currentMember && typeof currentMember.id === "string" ? currentMember.id : "";
if (!result.response.ok || !memberId) {
throw new Error(
`Could not find ${session.email}'s organization membership: GET /v1/org returned HTTP ${result.response.status} ${result.text.slice(0, 500)}`,
);
}
return memberId;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function stringField(value: unknown): string {
return typeof value === "string" ? value : "";
}
async function executeControl(app: Surface, action: string, args?: unknown): Promise<unknown> {
const result = await evalIn(
app,
`window.__openworkControl.execute(${JSON.stringify(action)}, ${JSON.stringify(args ?? null)})`,
{ awaitPromise: true },
);
if (!isRecord(result) || result.ok !== true) {
throw new Error(`Desktop control action ${action} failed: ${isRecord(result) ? String(result.error ?? "unknown") : "unknown"}`);
}
return result.result;
}
async function openModelPicker(app: Surface): Promise<void> {
const open = await evalIn(app, `Boolean(document.querySelector(${JSON.stringify(MODEL_SEARCH_INPUT)}))`).catch(() => false);
if (open !== true) {
await waitFor(app, `window.__openworkControl?.listActions().some((entry) => entry.id === "session.model_picker.open" && entry.disabled === false)`, {
timeoutMs: 30_000,
label: "session.model_picker.open enabled",
});
await executeControl(app, "session.model_picker.open");
}
await waitFor(app, `Boolean(document.querySelector(${JSON.stringify(MODEL_SEARCH_INPUT)}))`, {
timeoutMs: 30_000,
label: "Models dialog search input",
});
}
function parseModels(value: unknown): ModelFacts[] {
if (!Array.isArray(value)) throw new Error("Model picker did not return an array.");
return value.flatMap((entry) => {
if (!isRecord(entry)) return [];
if (typeof entry.id !== "string" || typeof entry.name !== "string" || typeof entry.providerName !== "string") return [];
return [{
id: entry.id,
name: entry.name,
providerName: entry.providerName,
selected: entry.selected === true,
selectable: entry.selectable === true,
}];
});
}
export async function readAvailableModels(app: Surface): Promise<ModelFacts[]> {
await openModelPicker(app);
await evalIn(app, `(() => {
const dialog = document.querySelector(${JSON.stringify(MODEL_DIALOG)});
if (!dialog) return false;
const headers = [...dialog.querySelectorAll("button")].filter((button) => {
const text = (button.textContent ?? "").replace(/\\s+/g, " ").trim();
return /\\d+ models?$/.test(text);
});
for (const header of headers) {
const group = header.parentElement?.parentElement;
if (group && !group.querySelector("span.font-mono")) header.click();
}
return true;
})()`);
await waitFor(app, `(() => {
const dialog = document.querySelector(${JSON.stringify(MODEL_DIALOG)});
return Boolean(dialog && (dialog.querySelector("span.font-mono") || dialog.innerText.includes("No models")));
})()`, { timeoutMs: 30_000, label: "model rows or empty state" });
const value = await evalIn(app, `(() => {
const dialog = document.querySelector(${JSON.stringify(MODEL_DIALOG)});
if (!dialog) return [];
return [...dialog.querySelectorAll("button")].flatMap((button) => {
const id = button.querySelector("span.font-mono")?.textContent?.trim();
if (!id) return [];
const spans = [...button.querySelectorAll("span")];
const name = spans.find((span) => !span.classList.contains("font-mono"))?.textContent?.trim() ?? id;
let group = button.parentElement;
while (group && !group.querySelector(':scope > div > button')) group = group.parentElement;
const providerHeader = group?.querySelector(':scope > div > button');
const providerName = providerHeader?.querySelector("span.text-dls-text")?.textContent?.trim()
?? providerHeader?.textContent?.replace(/\\d+ models?.*$/, "").trim()
?? "";
return [{
id,
name,
providerName,
selected: button.className.includes("bg-green-3"),
selectable: !button.disabled,
}];
});
})()`);
return parseModels(value);
}
export async function selectModel(app: Surface, name: string, options?: { provider?: string }): Promise<ModelFacts> {
await openModelPicker(app);
await fill(app, MODEL_SEARCH_INPUT, name);
await waitFor(app, `(() => {
const dialog = document.querySelector(${JSON.stringify(MODEL_DIALOG)});
const expectedProvider = ${JSON.stringify(options?.provider?.trim())};
return [...(dialog?.querySelectorAll("button") ?? [])].some((button) => {
const id = button.querySelector("span.font-mono")?.textContent?.trim() ?? "";
let group = button.parentElement;
while (group && !group.querySelector(':scope > div > button')) group = group.parentElement;
const providerHeader = group?.querySelector(':scope > div > button');
const providerName = providerHeader?.querySelector("span.text-dls-text")?.textContent?.trim()
?? providerHeader?.textContent?.replace(/\\d+ models?.*$/, "").trim()
?? "";
return !button.disabled
&& (id === ${JSON.stringify(name)} || (button.textContent ?? "").includes(${JSON.stringify(name)}))
&& (expectedProvider === undefined || providerName === expectedProvider);
});
})()`, { timeoutMs: 30_000, label: `selectable model ${name}` });
const selected = await evalIn(app, `(() => {
const dialog = document.querySelector(${JSON.stringify(MODEL_DIALOG)});
const expectedProvider = ${JSON.stringify(options?.provider?.trim())};
const button = [...(dialog?.querySelectorAll("button") ?? [])].find((candidate) => {
const id = candidate.querySelector("span.font-mono")?.textContent?.trim() ?? "";
let group = candidate.parentElement;
while (group && !group.querySelector(':scope > div > button')) group = group.parentElement;
const providerHeader = group?.querySelector(':scope > div > button');
const providerName = providerHeader?.querySelector("span.text-dls-text")?.textContent?.trim()
?? providerHeader?.textContent?.replace(/\\d+ models?.*$/, "").trim()
?? "";
return !candidate.disabled
&& (id === ${JSON.stringify(name)} || (candidate.textContent ?? "").includes(${JSON.stringify(name)}))
&& (expectedProvider === undefined || providerName === expectedProvider);
});
if (!button) return null;
const id = button.querySelector("span.font-mono")?.textContent?.trim() ?? "";
const spans = [...button.querySelectorAll("span")];
const title = spans.find((span) => !span.classList.contains("font-mono"))?.textContent?.trim() ?? id;
let group = button.parentElement;
while (group && !group.querySelector(':scope > div > button')) group = group.parentElement;
const providerHeader = group?.querySelector(':scope > div > button');
const providerName = providerHeader?.querySelector("span.text-dls-text")?.textContent?.trim()
?? providerHeader?.textContent?.replace(/\\d+ models?.*$/, "").trim()
?? "";
button.click();
return { id, name: title, providerName, selected: true, selectable: true };
})()`);
const models = parseModels(selected ? [selected] : []);
const model = models[0];
if (!model) throw new Error(`Could not select model ${name}.`);
await waitFor(app, `!Boolean(document.querySelector(${JSON.stringify(MODEL_SEARCH_INPUT)}))`, {
timeoutMs: 30_000,
label: "Models dialog closed after selection",
});
const persisted = await evalIn(app, `(() => {
try {
const preferences = JSON.parse(localStorage.getItem("openwork.preferences") || "{}");
return preferences?.defaultModel?.modelID === ${JSON.stringify(model.id)};
} catch {
return false;
}
})()`);
return {
...model,
selected: persisted === true,
};
}
export async function recoverInvalidModelSelection(
app: Surface,
preferredModelId?: string,
): Promise<ModelFacts | null> {
const models = await readAvailableModels(app);
const model = models.find((candidate) => candidate.selectable && candidate.id === preferredModelId)
?? models.find((candidate) => candidate.selectable);
if (model) {
const selected = await selectModel(app, model.id);
await waitFor(app, `(() => {
const text = document.body.innerText;
return !text.includes("Model no longer available")
&& !text.includes("The selected provider/model was not found in OpenCode provider catalog");
})()`, { timeoutMs: 30_000, label: "invalid selected model cleared" });
return selected;
}
await evalIn(app, `(() => {
let preferences = {};
try { preferences = JSON.parse(localStorage.getItem("openwork.preferences") || "{}"); } catch {}
delete preferences.defaultModel;
delete preferences.modelVariant;
localStorage.setItem("openwork.preferences", JSON.stringify(preferences));
setTimeout(() => location.reload(), 0);
return true;
})()`);
await waitFor(app, "Boolean(window.__openworkControl)", {
timeoutMs: 60_000,
label: "control API after clearing invalid selected model",
});
await waitFor(app, `(() => {
const text = document.body.innerText;
return !text.includes("Model no longer available")
&& !text.includes("The selected provider/model was not found in OpenCode provider catalog");
})()`, { timeoutMs: 30_000, label: "invalid selected model absent after reset" });
return null;
}
export async function readModelRecoveryState(app: Surface): Promise<ModelRecoveryFacts> {
const value = await evalIn(app, `(() => {
const text = document.body.innerText;
const emptyMessage = "Your organization hasn't published any models for you yet.";
const notice = [...document.querySelectorAll("button")].find((button) =>
(button.textContent ?? "").includes(emptyMessage) && (button.textContent ?? "").includes("Retry")
);
const message = notice?.querySelector("span");
const run = [...document.querySelectorAll("button")]
.find((button) => (button.textContent ?? "").trim() === "Run task");
return {
emptyMessageVisible: text.includes(emptyMessage),
retryVisible: text.includes("Retry"),
refreshVisible: text.includes("Refresh organization models"),
connectProviderVisible: text.includes("Connect a provider"),
warningVisible: text.includes("Model no longer available"),
guidanceVisible: text.includes("The model you were using is no longer available, please select a different model for this session."),
pickerOpen: Boolean(document.querySelector(${JSON.stringify(MODEL_DIALOG)})),
runTaskEnabled: Boolean(run && !run.disabled),
noticeHeight: notice ? Math.round(notice.getBoundingClientRect().height) : null,
noticeWhiteSpace: message ? getComputedStyle(message).whiteSpace : null,
};
})()`);
if (!isRecord(value)) throw new Error("Model recovery state was not an object.");
return {
emptyMessageVisible: value.emptyMessageVisible === true,
retryVisible: value.retryVisible === true,
refreshVisible: value.refreshVisible === true,
connectProviderVisible: value.connectProviderVisible === true,
warningVisible: value.warningVisible === true,
guidanceVisible: value.guidanceVisible === true,
pickerOpen: value.pickerOpen === true,
runTaskEnabled: value.runTaskEnabled === true,
noticeHeight: typeof value.noticeHeight === "number" ? value.noticeHeight : null,
noticeWhiteSpace: typeof value.noticeWhiteSpace === "string" ? value.noticeWhiteSpace : null,
};
}
export async function retryOrganizationModels(app: Surface): Promise<void> {
await waitFor(app, `(() => {
const button = [...document.querySelectorAll("button")].find((entry) => {
const text = entry.textContent ?? "";
return text.includes("Your organization hasn't published any models for you yet.") && text.includes("Retry") && !entry.disabled;
});
if (!button) return false;
button.click();
return true;
})()`, { timeoutMs: 30_000, label: "organization model Retry" });
}
export async function seedUnavailableModel(app: Surface): Promise<UnavailableModelSeed> {
await waitFor(app, `window.__openworkControl?.listActions().some((entry) => entry.id === "eval.model_not_available.seed" && entry.disabled === false)`, {
timeoutMs: 45_000,
label: "eval.model_not_available.seed enabled",
});
const value = await executeControl(app, "eval.model_not_available.seed");
if (!isRecord(value) || !isRecord(value.unavailableModel) || !isRecord(value.availableModel)) {
throw new Error(`Unavailable-model seed returned malformed facts: ${JSON.stringify(value)}`);
}
return {
unavailableModelId: stringField(value.unavailableModel.modelID),
availableModelId: stringField(value.availableModel.modelID),
availableModelName: stringField(value.availableModel.title),
availableProviderName: stringField(value.availableModel.providerName),
};
}
+139
View File
@@ -0,0 +1,139 @@
import type { Surface } from "@openwork/cdp";
import { clickButton, currentHash, evalIn, fill, waitFor, waitForText } from "./desktop.ts";
export interface LocalWorkspaceFacts {
id: string;
name: string;
path: string;
route: string;
entrypoint: "manual-folder" | "workspace-modal";
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function parseWorkspaceFacts(value: unknown): LocalWorkspaceFacts {
if (!isRecord(value)) throw new Error("Workspace creation did not return facts.");
const entrypoint = value.entrypoint;
if (
typeof value.id !== "string" ||
typeof value.name !== "string" ||
typeof value.path !== "string" ||
typeof value.route !== "string" ||
(entrypoint !== "manual-folder" && entrypoint !== "workspace-modal")
) {
throw new Error(`Workspace creation returned malformed facts: ${JSON.stringify(value)}`);
}
return { id: value.id, name: value.name, path: value.path, route: value.route, entrypoint };
}
async function submitFolder(app: Surface, path: string): Promise<void> {
await fill(app, 'input[placeholder="/workspace/my-project"]', path);
await clickButton(app, "Use this folder", { timeoutMs: 20_000 });
}
export async function createLocalWorkspaceViaUi(
app: Surface,
input: { path: string; name?: string },
): Promise<LocalWorkspaceFacts> {
await waitFor(app, "location.hash.includes('/welcome')", { timeoutMs: 30_000, label: "welcome route" });
let manualFolderVisible = await evalIn(app, 'Boolean(document.querySelector(\'input[placeholder="/workspace/my-project"]\'))') === true;
if (!manualFolderVisible) {
const useWithoutCloudVisible = await evalIn(app, `Boolean([...document.querySelectorAll("button")]
.find((button) => (button.textContent ?? "").trim() === "Use Without Cloud" && !button.disabled))`);
if (useWithoutCloudVisible === true) {
await clickButton(app, "Use Without Cloud");
await waitFor(app, 'Boolean(document.querySelector(\'input[placeholder="/workspace/my-project"]\'))', {
timeoutMs: 15_000,
label: "local workspace folder input",
});
manualFolderVisible = true;
}
}
let entrypoint: LocalWorkspaceFacts["entrypoint"];
if (manualFolderVisible) {
// Current dev Electron exposes this field after Use Without Cloud. The
// modal branch below retains the older local-workspace journey.
entrypoint = "manual-folder";
await submitFolder(app, input.path);
} else {
entrypoint = "workspace-modal";
await waitFor(app, `(() => {
const button = [...document.querySelectorAll("button")]
.find((candidate) => (candidate.textContent ?? "").trim() === "Get started" && !candidate.disabled);
if (!button) return false;
button.click();
return true;
})()`, { timeoutMs: 15_000, label: "Get started" });
await waitForText(app, "Local workspace", { timeoutMs: 15_000 });
await waitFor(app, `(() => {
const button = [...document.querySelectorAll("button")]
.find((candidate) => (candidate.textContent ?? "").trim() === "Local workspace" && !candidate.disabled);
if (!button) return false;
button.click();
return true;
})()`, { timeoutMs: 15_000, label: "Local workspace" });
await waitForText(app, "No folder selected yet.", { timeoutMs: 15_000 });
const injected = await evalIn(app, `(() => {
const placeholder = [...document.querySelectorAll("span, div, p")]
.find((node) => (node.textContent ?? "").includes("No folder selected yet."));
if (!placeholder) return { ok: false, reason: "folder placeholder not found" };
const key = Object.keys(placeholder).find((candidate) => candidate.startsWith("__reactFiber$"));
let fiber = key ? placeholder[key] : null;
while (fiber) {
const componentName = fiber.elementType?.name || fiber.type?.name || "";
if (componentName === "CreateWorkspaceModal") break;
fiber = fiber.return;
}
if (!fiber) return { ok: false, reason: "CreateWorkspaceModal fiber not found" };
let hook = fiber.memoizedState;
while (hook) {
if (hook.queue?.dispatch) {
hook.queue.dispatch({ type: "set", key: "selectedFolder", value: ${JSON.stringify(input.path)} });
hook.queue.dispatch({ type: "set", key: "pickingFolder", value: false });
return { ok: true };
}
hook = hook.next;
}
return { ok: false, reason: "folder reducer dispatch not found" };
})()`);
if (!isRecord(injected) || injected.ok !== true) {
throw new Error(`Could not inject the folder chosen by the native picker: ${JSON.stringify(injected)}`);
}
if (input.name) {
await evalIn(app, `(() => {
const nameInput = document.querySelector('input[placeholder*="name" i], input[placeholder*="workspace" i]');
if (!nameInput) return false;
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
setter?.call(nameInput, ${JSON.stringify(input.name)});
nameInput.dispatchEvent(new Event("input", { bubbles: true }));
nameInput.dispatchEvent(new Event("change", { bubbles: true }));
return true;
})()`);
}
await waitFor(app, `(() => {
const button = [...document.querySelectorAll("button")]
.find((candidate) => (candidate.textContent ?? "").trim() === "Create Workspace" && !candidate.disabled);
if (!button) return false;
button.click();
return true;
})()`, { timeoutMs: 15_000, label: "Create Workspace" });
}
await waitForText(app, "Power your first task", { timeoutMs: 120_000 });
// Deliberately do NOT query the workspace record here. The local server has
// credentials in localStorage before it can actually serve, so an in-page fetch
// at this point never settles. The caller resolves the id from the product's
// own active-workspace state once onboarding finishes, which is both cheaper
// and the state a user's app really uses.
const raw = {
id: "",
name: input.name ?? "",
path: input.path,
route: await currentHash(app),
entrypoint,
};
return parseWorkspaceFacts(raw);
}
+85
View File
@@ -0,0 +1,85 @@
import type { Surface } from "@openwork/cdp";
import { control } from "./desktop.ts";
const FIRST_CREATE_TIMEOUT_MS = 60_000;
const SUBSEQUENT_CREATE_TIMEOUT_MS = 15_000;
const CREATE_ATTEMPT_TIMEOUT_MS = 8_000;
const POLL_INTERVAL_MS = 250;
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function messageText(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
async function createSession(app: Surface, title: string, timeoutMs: number): Promise<string> {
const deadline = Date.now() + timeoutMs;
let attempts = 0;
let lastError: unknown = null;
while (Date.now() < deadline) {
attempts += 1;
const remainingMs = Math.max(1, deadline - Date.now());
let result: unknown;
try {
result = await control(app, "session.create_task", undefined, {
timeoutMs: Math.min(CREATE_ATTEMPT_TIMEOUT_MS, remainingMs),
});
} catch (error) {
lastError = error;
await sleep(Math.min(POLL_INTERVAL_MS, Math.max(0, deadline - Date.now())));
continue;
}
if (typeof result !== "string" || !result.trim()) {
throw new Error(`Desktop control action session.create_task returned an invalid session ID for ${JSON.stringify(title)}.`);
}
return result.trim();
}
throw new Error(
`Desktop control action session.create_task did not become available for ${JSON.stringify(title)} within ${timeoutMs}ms (${attempts} attempts)${lastError ? `: ${messageText(lastError)}` : ""}.`,
);
}
export async function listSessions(app: Surface): Promise<{ sessionId: string; title: string }[]> {
const result = await control(app, "session.list_sessions");
if (!Array.isArray(result)) {
throw new Error(`Desktop control action session.list_sessions returned an invalid list: ${JSON.stringify(result)}`);
}
const sessions: { sessionId: string; title: string }[] = [];
for (const session of result) {
if (!isRecord(session) || typeof session.sessionId !== "string" || typeof session.title !== "string") {
throw new Error(`Desktop control action session.list_sessions returned an invalid session: ${JSON.stringify(session)}`);
}
sessions.push({ sessionId: session.sessionId, title: session.title });
}
return sessions;
}
export async function seedSessions(
app: Surface,
titles: readonly string[],
): Promise<{ sessionId: string; title: string }[]> {
const seeded: { sessionId: string; title: string }[] = [];
for (const [index, title] of titles.entries()) {
const timeoutMs = index === 0 ? FIRST_CREATE_TIMEOUT_MS : SUBSEQUENT_CREATE_TIMEOUT_MS;
const sessionId = await createSession(app, title, timeoutMs);
await control(app, "session.rename", { sessionId, title });
seeded.push({ sessionId, title });
}
const observed = await listSessions(app);
const missing = titles.filter((title) => !observed.some((session) => session.title === title));
if (missing.length > 0) {
throw new Error(
`Seeded session titles were not present after creation. Missing: ${JSON.stringify(missing)}. Observed: ${JSON.stringify(observed)}.`,
);
}
return seeded;
}
+146
View File
@@ -0,0 +1,146 @@
import type { Surface } from "@openwork/cdp";
import { evalIn, waitFor } from "./desktop.ts";
const PLUG_BUTTON = 'button[title="Commands, skills, and MCPs"]';
const SKILL_MARKERS = ["/browser-automation"];
export interface SkillFacts {
name: string;
label: string;
local: boolean;
}
export interface SkillsLoadFacts {
elapsedMs: number;
rowCount: number;
skills: SkillFacts[];
loadingCommandsVisible: boolean;
}
export interface ComposerCapabilitiesFacts {
sections: string[];
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function parseSkills(value: unknown): SkillFacts[] {
if (!Array.isArray(value)) return [];
return value.flatMap((entry) => {
if (!isRecord(entry) || typeof entry.name !== "string" || typeof entry.label !== "string") return [];
return [{ name: entry.name, label: entry.label, local: entry.local === true }];
});
}
function parseSkillsLoad(value: unknown): SkillsLoadFacts {
if (!isRecord(value)) throw new Error(`Skills load returned malformed facts: ${JSON.stringify(value)}`);
return {
elapsedMs: typeof value.elapsedMs === "number" ? value.elapsedMs : Number.POSITIVE_INFINITY,
rowCount: typeof value.rowCount === "number" ? value.rowCount : 0,
skills: parseSkills(value.skills),
loadingCommandsVisible: value.loadingCommandsVisible === true,
};
}
async function openPlugMenu(app: Surface): Promise<void> {
// One guarded, retried step: the renderer freezes in bursts while the
// workspace engine boots, so a bare click evaluation can eat a 20s CDP
// timeout and fail the spec. Clicking only while the menu is closed keeps
// retries from toggling it back shut.
await waitFor(app, `(() => {
const labels = [...document.querySelectorAll("button")].map((button) => (button.textContent ?? "").trim());
if (labels.includes("Skills") && labels.includes("Extensions")) return true;
const plug = document.querySelector(${JSON.stringify(PLUG_BUTTON)});
if (!plug) return false;
document.body.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
plug.click();
return false;
})()`, { timeoutMs: 60_000, label: "plug menu sections" });
}
export async function readComposerCapabilities(app: Surface): Promise<ComposerCapabilitiesFacts> {
await openPlugMenu(app);
const value = await evalIn(app, `(() => {
const labels = [...document.querySelectorAll("button")].map((button) => (button.textContent ?? "").trim());
return ["Agents", "Commands", "Skills", "Extensions"].filter((section) => labels.includes(section));
})()`);
return { sections: Array.isArray(value) ? value.filter((entry): entry is string => typeof entry === "string") : [] };
}
export async function measureLoadedSkills(app: Surface): Promise<SkillsLoadFacts> {
await openPlugMenu(app);
const value = await evalIn(app, `new Promise((resolve) => {
const skillsButton = [...document.querySelectorAll("button")]
.find((button) => (button.textContent ?? "").trim() === "Skills");
if (!skillsButton) { resolve({ error: "skills section button not found" }); return; }
const startedAt = performance.now();
skillsButton.click();
const poll = () => {
const rows = [...document.querySelectorAll("button")]
.filter((button) => /^\\/[a-z0-9-]+/i.test((button.textContent ?? "").trim()));
const hit = rows.some((button) => ${JSON.stringify(SKILL_MARKERS)}
.some((marker) => (button.textContent ?? "").includes(marker)));
if (hit) {
resolve({
elapsedMs: Math.round(performance.now() - startedAt),
rowCount: rows.length,
skills: rows.map((button) => {
const label = (button.textContent ?? "").replace(/\\s+/g, " ").trim();
return { name: (label.match(/^\\/[a-z0-9-]+/) ?? [label])[0], label, local: label.includes("Local") };
}),
loadingCommandsVisible: document.body.innerText.includes("Loading commands"),
});
return;
}
if (performance.now() - startedAt > 20_000) {
resolve({ error: "timed out", bodyTail: document.body.innerText.slice(-400) });
return;
}
setTimeout(poll, 20);
};
poll();
})`, { awaitPromise: true, timeoutMs: 30_000 });
// The in-page poll gives up at 20s; the CDP call must outlive it or the two
// deadlines race and the spec dies with a raw CDP timeout instead of facts.
if (isRecord(value) && typeof value.error === "string") throw new Error(`Skills did not render: ${JSON.stringify(value)}`);
return parseSkillsLoad(value);
}
export async function readLoadedSkills(app: Surface): Promise<SkillFacts[]> {
return (await measureLoadedSkills(app)).skills;
}
/**
* Scroll a composer-menu row into view. The menu is a scrollable popover, so
* a row can be loaded (and asserted from the DOM) while a screenshot still
* cannot show it; reveal it first when a visual claim names it.
*/
export async function revealMenuRow(app: Surface, marker: string): Promise<void> {
await waitFor(app, `(() => {
const row = [...document.querySelectorAll("button")]
.find((button) => (button.textContent ?? "").includes(${JSON.stringify(marker)}));
if (!row) return false;
row.scrollIntoView({ block: "center" });
return true;
})()`, { timeoutMs: 10_000, label: `menu row ${marker} in view` });
}
export async function readLoadedExtensions(app: Surface): Promise<string[]> {
await openPlugMenu(app);
await waitFor(app, `(() => {
const button = [...document.querySelectorAll("button")]
.find((candidate) => (candidate.textContent ?? "").trim() === "Extensions");
if (!button) return false;
button.click();
return true;
})()`, { label: "Extensions section" });
await waitFor(app, 'document.body.innerText.includes("OpenWork Browser")', {
timeoutMs: 10_000,
label: "OpenWork Browser extension",
});
const value = await waitFor(app, `([...document.querySelectorAll("button")]
.map((button) => (button.textContent ?? "").replace(/\\s+/g, " ").trim())
.filter((label) => label.includes("OpenWork Browser")))`, { timeoutMs: 60_000, label: "OpenWork Browser extension rows" });
return Array.isArray(value) ? value.filter((entry): entry is string => typeof entry === "string") : [];
}
@@ -0,0 +1,128 @@
import assert from "node:assert/strict";
import { createServer } from "node:http";
import test from "node:test";
import { deleteConnection, deleteConnectionsNamed, type DenSession } from "../src/den.ts";
function respondJson(response: import("node:http").ServerResponse, status: number, body: unknown): void {
response.writeHead(status, { "content-type": "application/json" });
response.end(JSON.stringify(body));
}
test("deleteConnection refreshes a stale session once before retrying teardown", async () => {
const requests: { path: string; authorization: string; body: string }[] = [];
const server = createServer((request, response) => {
const chunks: Buffer[] = [];
request.on("data", (chunk: Buffer) => chunks.push(chunk));
request.on("end", () => {
const path = new URL(request.url ?? "/", "http://stub.test").pathname;
const authorization = request.headers.authorization ?? "";
const body = Buffer.concat(chunks).toString("utf8");
requests.push({ path, authorization, body });
if (request.method === "DELETE" && path === "/v1/mcp-connections/connection-1" && authorization === "Bearer stale-token") {
respondJson(response, 403, { error: "reauth", reason: "fresh_auth_required" });
return;
}
if (request.method === "POST" && path === "/api/auth/sign-in/email") {
respondJson(response, 200, { token: "fresh-token" });
return;
}
if (request.method === "DELETE" && path === "/v1/mcp-connections/connection-1" && authorization === "Bearer fresh-token") {
respondJson(response, 204, null);
return;
}
respondJson(response, 500, { error: "unexpected_request" });
});
});
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
try {
const address = server.address();
assert.ok(address && typeof address === "object");
const session: DenSession = {
apiUrl: `http://127.0.0.1:${address.port}`,
webUrl: "http://localhost",
token: "stale-token",
email: "admin@example.com",
password: "same-password",
};
await deleteConnection(session, "connection-1");
assert.deepEqual(requests.map(({ path, authorization }) => ({ path, authorization })), [
{ path: "/v1/mcp-connections/connection-1", authorization: "Bearer stale-token" },
{ path: "/api/auth/sign-in/email", authorization: "" },
{ path: "/v1/mcp-connections/connection-1", authorization: "Bearer fresh-token" },
]);
assert.deepEqual(JSON.parse(requests[1]?.body ?? ""), {
email: "admin@example.com",
password: "same-password",
});
} finally {
await new Promise<void>((resolve, reject) => {
server.close((error) => error ? reject(error) : resolve());
});
}
});
test("deleteConnectionsNamed refreshes a stale list session once", async () => {
const requests: string[] = [];
const server = createServer((request, response) => {
const path = new URL(request.url ?? "/", "http://stub.test").pathname;
const authorization = request.headers.authorization ?? "";
requests.push(`${request.method} ${path} ${authorization}`.trim());
if (request.method === "GET" && path === "/v1/mcp-connections" && authorization === "Bearer stale-token") {
respondJson(response, 403, { error: "reauth", reason: "fresh_auth_required" });
return;
}
if (request.method === "POST" && path === "/api/auth/sign-in/email") {
respondJson(response, 200, { token: "fresh-token" });
return;
}
if (request.method === "GET" && path === "/v1/mcp-connections" && authorization === "Bearer fresh-token") {
respondJson(response, 200, {
connections: [
{ id: "delete-me", name: "eval-cleanup-one" },
{ id: "keep-me", name: "other-connection" },
],
});
return;
}
if (request.method === "DELETE" && path === "/v1/mcp-connections/delete-me" && authorization === "Bearer fresh-token") {
response.writeHead(204);
response.end();
return;
}
respondJson(response, 500, { error: "unexpected_request" });
});
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
try {
const address = server.address();
assert.ok(address && typeof address === "object");
await deleteConnectionsNamed({
apiUrl: `http://127.0.0.1:${address.port}`,
webUrl: "http://localhost",
token: "stale-token",
email: "admin@example.com",
password: "same-password",
}, "eval-cleanup-");
assert.deepEqual(requests, [
"GET /v1/mcp-connections Bearer stale-token",
"POST /api/auth/sign-in/email",
"GET /v1/mcp-connections Bearer fresh-token",
"DELETE /v1/mcp-connections/delete-me Bearer fresh-token",
]);
} finally {
await new Promise<void>((resolve, reject) => {
server.close((error) => error ? reject(error) : resolve());
});
}
});
@@ -0,0 +1,71 @@
import assert from "node:assert/strict";
import test from "node:test";
import { waitFor } from "../src/desktop.ts";
import type { CdpClient, Surface } from "@openwork/cdp";
function fakeSurface(valueForExpression: (expression: string) => unknown): Surface {
const client: CdpClient = {
async send(method, params = {}) {
assert.equal(method, "Runtime.evaluate");
const expression = typeof params.expression === "string" ? params.expression : "";
return { result: { value: valueForExpression(expression) } };
},
close() {},
};
return {
handle: {
name: "fake",
kind: "electron",
hostKind: "test",
cdpUrl: "http://127.0.0.1:1",
},
client,
};
}
function isScreenDump(expression: string): boolean {
return expression.includes("document.title") && expression.includes("querySelectorAll(\"button\")");
}
test("waitFor has a finite default timeout", async (context) => {
context.mock.timers.enable({ apis: ["Date"], now: 0 });
const surface = fakeSurface((expression) => {
if (isScreenDump(expression)) {
return { hash: "#/still-here", route: "/still-here", title: "Waiting", buttons: [], body: "Not ready" };
}
context.mock.timers.setTime(30_001);
return false;
});
await assert.rejects(waitFor(surface, "false"), /Timed out after 30000ms waiting for false/);
});
test("waitFor appends a bounded on-screen dump to timeout failures", async () => {
const surface = fakeSurface((expression) => isScreenDump(expression)
? {
hash: "#/workspace/demo/extensions",
route: "/workspace/demo/extensions",
title: "OpenWork",
buttons: ["Back", "Add extension"],
body: "Extensions Add an extension to this workspace",
}
: false);
await assert.rejects(
waitFor(surface, "false", { timeoutMs: 1, label: "extensions route" }),
/On screen: \{"hash":"#\/workspace\/demo\/extensions","route":"\/workspace\/demo\/extensions","title":"OpenWork","buttons":\["Back","Add extension"\],"body":"Extensions Add an extension to this workspace"\}/,
);
});
test("waitFor still reports timeout context when the on-screen dump fails", async () => {
const surface = fakeSurface((expression) => {
if (isScreenDump(expression)) throw new Error("renderer unavailable");
return false;
});
await assert.rejects(
waitFor(surface, "false", { timeoutMs: 1, label: "extensions route" }),
/Timed out after 1ms waiting for extensions route.*On screen: unavailable/,
);
});
@@ -0,0 +1,82 @@
import assert from "node:assert/strict";
import { createServer } from "node:http";
import test from "node:test";
import { ensureMemberSession, type DenSession } from "../src/den.ts";
function respondJson(response: import("node:http").ServerResponse, status: number, body: unknown): void {
response.writeHead(status, { "content-type": "application/json" });
response.end(JSON.stringify(body));
}
async function withStubServer(
allowPublicSignup: boolean,
run: (den: { apiUrl: string; webUrl: string }, requests: string[]) => Promise<void>,
): Promise<void> {
const requests: string[] = [];
const server = createServer((request, response) => {
const url = new URL(request.url ?? "/", "http://stub.test");
requests.push(`${request.method} ${url.pathname}`);
if (request.method === "POST" && url.pathname === "/api/auth/sign-in/email") {
respondJson(response, 401, { error: "invalid_credentials" });
return;
}
if (request.method === "GET" && url.pathname === "/v1/auth/login-options") {
respondJson(response, 200, {
email: url.searchParams.get("email"),
nextStep: allowPublicSignup ? "new_account" : "password",
allowPublicSignup,
});
return;
}
if (request.method === "POST" && url.pathname === "/v1/invitations") {
respondJson(response, 200, { inviteToken: "invite-token" });
return;
}
respondJson(response, 409, { error: "stop_after_invitation" });
});
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
try {
const address = server.address();
assert.ok(address && typeof address === "object");
await run({ apiUrl: `http://127.0.0.1:${address.port}`, webUrl: "http://localhost" }, requests);
} finally {
await new Promise<void>((resolve, reject) => {
server.close((error) => error ? reject(error) : resolve());
});
}
}
const credentials = { email: "member@example.com", password: "password123" };
function adminSession(den: { apiUrl: string; webUrl: string }): DenSession {
return { ...den, token: "admin-token", email: "admin@example.com", password: "admin-password" };
}
test("ensureMemberSession explains how to enable member bootstrap when signup is disabled", async () => {
await withStubServer(false, async (den, requests) => {
await assert.rejects(
ensureMemberSession(den, adminSession(den), credentials),
/Member bootstrap needs public signup.*DEN_ORG_MODE=multi_org.*DEN_SINGLE_ORG_ALLOW_PUBLIC_SIGNUP=true.*member@example\.com/,
);
assert.deepEqual(requests, ["POST /api/auth/sign-in/email", "GET /v1/auth/login-options"]);
});
});
test("ensureMemberSession proceeds to invitation when signup is allowed", async () => {
await withStubServer(true, async (den, requests) => {
await assert.rejects(
ensureMemberSession(den, adminSession(den), credentials),
/Member sign-up failed: HTTP 409/,
);
assert.deepEqual(requests, [
"POST /api/auth/sign-in/email",
"GET /v1/auth/login-options",
"POST /v1/invitations",
"POST /api/auth/sign-up/email",
]);
});
});
@@ -0,0 +1,32 @@
import assert from "node:assert/strict";
import test from "node:test";
import { startEgressLab } from "@openwork/labs";
import { diagnoseTls } from "@openwork/matchers";
import { probeTls } from "../src/diagnostics.ts";
test("probeTls and diagnoseTls identify a TLS 1.3-only stall without the eval framework", async () => {
await using lab = await startEgressLab({ profile: "tls12-only" });
const url = new URL(lab.url);
const facts = await probeTls({
// The lab listens on IPv4. Keep certificate verification on the generated
// localhost identity without depending on the runner's localhost family
// preference.
host: "127.0.0.1",
port: Number(url.port),
servername: url.hostname,
// Chain verification is back on: the lab root is now generated with an
// explicit basicConstraints extension, so it is a real CA on every openssl
// build (1.1.1 silently emitted CA:FALSE before).
ca: lab.rootPem,
});
const message = JSON.stringify(facts);
assert.equal(facts.tls12.handshakeOk, true, message);
assert.equal(facts.tls12.stalled, false, message);
assert.equal(facts.tls12.chainVerified, true, message);
assert.equal(facts.tls12.protocol, "TLSv1.2", message);
assert.equal(facts.tls13.stalled, true, message);
assert.equal(facts.tls13.errorCode, "ETIMEDOUT", message);
assert.equal(diagnoseTls(facts).code, "tls_handshake_stall_tls13_only", message);
// Lab-PKI chain trust is covered by the pure matcher tests (the tls_chain_untrusted branch) and the product-diagnostics spec; it is deliberately not asserted here because it varies with the runner's OpenSSL.
});
@@ -0,0 +1,37 @@
import assert from "node:assert/strict";
import test from "node:test";
import { parseSessionToolCalls } from "../src/desktop.ts";
test("parses namespaced and catalog tool calls from a session transcript", () => {
// Mirrors session.read_transcript's messages shape and messageToReadableText's
// dynamic-tool rendering in apps/app/src/.../session-surface.tsx:1853-1878,355-376.
const calls = parseSessionToolCalls({
sessionId: "ses_1",
messageCount: 1,
returned: 1,
messages: [{
index: 0,
role: "assistant",
text: [
"OpenWork",
"[tool:mcp:emc_labs:send_email] sent",
"[tool:native:emc_robotics:postCapabilitiesGoogleWorkspaceGmailDrafts] drafted",
"[tool:openwork-cloud_execute_capability] searched",
].join("\n\n"),
}],
});
assert.deepEqual(calls, [
{ capability: "mcp:emc_labs:send_email", connectionId: "emc_labs", at: "" },
{
capability: "native:emc_robotics:postCapabilitiesGoogleWorkspaceGmailDrafts",
connectionId: "emc_robotics",
at: "",
},
{ capability: "openwork-cloud_execute_capability", connectionId: null, at: "" },
]);
});
test("returns no tool calls for an empty transcript", () => {
assert.deepEqual(parseSessionToolCalls({ sessionId: "ses_1", messageCount: 0, returned: 0, messages: [] }), []);
});