Files
PaperClipAI/tests/runner-e2e/chat-catalog-domains/external-private-browser-worker.ts
T
DottaandClaude Opus 5.5 d16becac04 test(evals): fix cohort-10 secrets entry, steering graders, private-task settle and queue brief (#21296)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The chat eval catalog checks real chat journeys against saved
product state, not reply wording
> - Full cohort 10 failed six cases where the product worked but the
eval was out of date or raced
> - #21225 redesigned Settings → Secrets, and #21243 changed when a
queued comment counts as delivered
> - The private secret steps, two steering graders, one final run check
and one fixture did not follow
> - This pull request updates those eval layers and keeps each one able
to fail a broken product
> - The benefit is that these six cases measure the product again
instead of the stale harness

## Linked Issues or Issue Description

Refs #21225
Refs #21243

**What happened?**
In full cohort 10 (OpenCode, local), six cases failed for eval reasons:

- `secrets.configure-company-secret` and `secrets.personal-secret`: the
private browser step waited for the old "Create secret" dialog and its
Provider field, and for the old personal form text. #21225 renamed the
dialog to "New secret", removed Provider, moved Key under "More
options", and moved the definition name into the "Set your value for …"
heading.
- `steering.steer-active` and `steering.discard-queued-message`:
Morgan's running turn read the queued comments through task history and
cited them in its accepted result. Under #21243 that is delivery, so no
continuation ran. The graders still required one more run and timed out.
- `tasks.private-task`: the final run check read the ledger while the
CEO's `chat_task_completed` follow-up was still running.
- `steering.queue-follow-up`: the brief was only a reference string, so
the continuation asked a question instead of building.

**Expected behavior**
Each case grades the product's current contract on saved state, and
still fails a broken product.

**Steps to reproduce**
Run the six cases at `origin/master` with the OpenCode local profile.
See the cohort-10 evidence for each case.

## What Changed

- `external-private-browser-worker.ts`: the company step opens "New
secret", finds the dialog by its "Who provides the value" radiogroup,
selects Organization, opens "More options" for a requested key, and
picks Paperclip-encrypted storage only when the "Stored in" picker is
shown. The saved secret must now have provider `local_encrypted`. The
personal step binds the form by the heading "Set your value for Personal
publishing credential". Credential handling is unchanged.
`manual-private-*.ts` is unchanged.
- `external-company-secret-create-dialog.test.ts`: the canary page uses
the new dialog. It adds a several-vaults variant (valid), a
wrong-storage result and the retired "Create secret" form (both
rejected). Source anchors follow `Secrets.tsx`.
- `external-personal-secret-entry-dialog.test.ts`: checks that the
worker heading matches what `SetMyUserSecretDialog.tsx` renders, and
rejects another definition and the "Replace your value" form.
- New `runtime-cited-delivery.ts`: `owedAfterTurn` (queued comments the
turn did not read at the current revision and cite) and
`continuationCarriesOwed` (one continuation carries each owed comment
once, with its text and the earlier-read warning, and none that the turn
handled).
- `runtime-active-steering.ts` (oracle `steer-active.v9`): records the
queued comments (person and any CEO relay) and the worker's receipts.
Delivery passes if every queued comment was handled in turn with no
continuation, or one continuation carries exactly the owed ones. Run
count is 2 plus one when anything is owed. The website the worker
registered last must still render blue.
- `runtime-discard.ts` (oracle `runtime-discard.v7`, `minimumRunCount:
4`): the same rule for the font note and the later contact request. The
run that handled the contact request must save its labelled section. The
discarded request must never be cited. The gate no longer says "Leave
saved follow-ups for their ordinary continuation", because that
contradicts the #21243 contract.
- `internal-private-delegation.ts` (oracle `private-task.v3`):
`verifyResult` waits for the delegated work and its completion follow-up
to settle, in the pattern of `identity-plan-review.ts`. A failed or
cancelled run still stops the wait. A worker still running at the
deadline stays bounded active work.
- `runtime-queue.ts` (oracle `runtime-queue.v4`): the gate gives a
complete website brief (product, page, hero copy, features, pricing,
company details). It does not contain the word "contact", so the brief
does not complete the queued requirement.
- Oracle bumps for the secrets cases:
`company-secret-controller-baseline.v22`, `personal-secret.v21`.
`chat-catalog-descriptors.json` follows all bumps and source files. No
scenario YAML changed, so the TestOps manifest is unchanged.

## Why each new check still catches a broken product

- Secrets: the old check matched removed UI text, so it failed a working
product. The new check uses roles and labels, and still fails a missing
form, a failed POST, wrong saved metadata and storage outside Paperclip.
- Steering: the old check required a continuation, which #21243 removes
when the turn cites the comment. The new check still fails an uncited
read with no continuation (the lost-comment bug), a citation without a
current-revision read, a cited comment that was not applied (no blue
page or no saved label), a replay of a handled comment, and any extra
run.
- Private task: the old check read the ledger at a random point after
acceptance. The case still fails if the follow-up fails or is cancelled.
- Queue brief: the grader is unchanged. The brief test fails if the
brief leaks the contact requirement.

## Verification

- `npx vitest run --config tests/runner-e2e/vitest.config.ts
tests/runner-e2e/chat-catalog tests/runner-e2e/chat-catalog-domains`:
285 files, 3948 tests pass.
- Calibration uses the retained cohort-10 receipts. The steer-active
worker read and cited both queued comments. The discard worker read and
cited the font note and the later contact request. Both now pass.
Reworded-valid variants (target handled in turn, relay or font note owed
to one continuation) pass. The broken variants listed above fail.
- `PAPERCLIP_CHAT_EVAL_SECRET_ROUTE_CANARY=1` secrets canaries: 3 of 3
pass. RED: the old worker against the new dialog page fails
(`locator.fill` timeout).
- `pnpm test:e2e:runner:typecheck`: pass. `node --test
tests/runner-e2e/chat-full-catalog-testops-manifest.test.mjs`: 10 of 10.
`node tests/runner-e2e/source-sdk-public-inputs.mjs
e0b618b02a --check`: `changed: false`.
- Paid run: Runner Full-Stack E2E 43368487002 on throwaway branch
`chat-eval/full10-paid` (the six cases). The result follows in a
comment.

## Risks

- Steering: a run that applies a comment but does not cite it still
needs one continuation. This matches the product contract.
- The steer-active gate still asks the worker to keep queued
requirements for normal delivery. Both paths are accepted, so the gate
cannot cause a false pass.
- The private-task settle adds up to 120 seconds inside the original
observation budget.
- Low risk overall: eval-only changes. No product code changed.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, through Claude Code
with tool use and code execution.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 03:38:12 -06:00

319 lines
39 KiB
TypeScript

import {canonicalCompanySecretKey,validCompanySecretMetadata} from "./external-company-secret-confirmation.js";
import { personalSecretEntryDialog } from "./external-personal-secret-entry-dialog.js";
import { validDevicePrivateInput, type DevicePrivateInput } from "./external-device-private-contract.js";
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { chromium, test, type Cookie } from "@playwright/test";
import { recordPrivateChildFailure } from "./private-child-failure.js";
import type { PrivateBrowserStep } from "./external-private-browser-steps.js";
import { validGrokRoutinePrivateInput, matchesConfiguredGrokRoutine, type GrokRoutinePrivateInput } from "./external-grok-private-contract.js";
import { validSlackIdentityInput, validSlackWizardIdentityInput, matchingSlackIdentityPreview, type SlackIdentityPrivateInput } from "./external-slack-identity-contract.js";
import { validSlackSetupPrivate, type SlackSetupPrivateInput } from "./external-slack-setup-private-contract.js";
interface Request { mode: "secure-key" | "canary" | "gateway-token-rotation" | "company-secret-create" | "personal-secret-create" | "grok-routine-configure" | "slack-identity-confirm" | "slack-endpoint-configure" | "slack-wizard-confirm" | "slack-wizard-resume" | "slack-endpoint-reconnect" | "device-approve" | "device-expired"; device?: DevicePrivateInput; slackSetup?:SlackSetupPrivateInput; slackIdentity?:SlackIdentityPrivateInput; grok?:GrokRoutinePrivateInput; companyId?:string; companyMetadata?:{name?:string;key?:string}; personal?:{definitionId:string;userId:string;viaMySecrets?:boolean}; gateway?: { id: string; oldTokenId: string; otherToken: string; endpointPath: string; readName: string; pendingTokenId?: string }; baseURL: string; setupURL: string; providerURL: string; credential: string; cookies: Cookie[]; executablePath?: string; channel?: string }
/** Separate Node process: no Playwright Test worker, API-step listener or artifact
* recorder exists. This module never enables tracing, video or screenshots. */
let step: PrivateBrowserStep = "request";
async function main() {
let attached = false; try { test.info(); attached = true; } catch { /* expected outside Test */ }
if (attached) throw new Error("Private browser worker cannot run inside Playwright Test");
const input = JSON.parse(await readFile(process.argv[2]!, "utf8")) as Request;
if (!input.credential || input.credential.length > (input.mode === "grok-routine-configure" ? 16384 : 8192) || !["secure-key", "canary", "gateway-token-rotation", "company-secret-create", "personal-secret-create", "grok-routine-configure", "slack-identity-confirm", "slack-endpoint-configure", "slack-wizard-confirm", "slack-wizard-resume", "slack-endpoint-reconnect", "device-approve", "device-expired"].includes(input.mode)) throw new Error("Invalid private browser request");
const base = new URL(input.baseURL);
if (base.hostname !== "127.0.0.1" || base.protocol !== "http:" || new URL(input.setupURL).origin !== base.origin) throw new Error("Private browser target is outside the disposable loopback instance");
step = "launch"; const browser = await chromium.launch({ headless: true, ...(input.executablePath ? { executablePath: input.executablePath } : {}), ...(input.channel ? { channel: input.channel } : {}) });
try {
const context = await browser.newContext({ baseURL: base.origin });
await context.addCookies(input.cookies); const page = await context.newPage(); page.setDefaultTimeout(30_000); step = input.mode;
if (input.mode === "device-approve" || input.mode === "device-expired") {
if (!validDevicePrivateInput(input.mode, input.baseURL, input.setupURL, input.companyId, input.device, input.credential) || !input.device) throw new Error("Invalid finite device verification request");
step = "device-session"; const session = await context.request.get("/api/auth/get-session");
if (!session.ok() || (await session.json()).user?.id !== input.device.userId) throw new Error("Device UI requires the same signed-in fixture person");
const description = page.waitForResponse(response => {
const url = new URL(response.url());
return url.origin === base.origin && url.pathname === "/api/mcp/device" && url.searchParams.get("user_code") === input.credential && response.request().method() === "GET";
});
step = "device-describe"; await page.goto(input.setupURL, { waitUntil: "domcontentloaded" });
const described = await description;
if (input.mode === "device-expired") {
const body = await described.json();
if (described.status() !== 404 || body.error !== "invalid_request") throw new Error("Expired device UI did not receive the actual issuer rejection");
step = "device-expired-page"; await page.getByText(/expired|invalid/i).first().waitFor();
if (await page.getByRole("button", { name: "Connect organization", exact: true }).isVisible()) throw new Error("Expired device still offers approval");
return { processIsolation: true, testRunnerAttached: false, tracingEnabled: false, videoEnabled: false, apiStepRecorder: false, deviceSessionVerified: true, expiredPageVisible: true };
}
if (!described.ok()) throw new Error("Device consent description unavailable");
const request = await described.json();
if (request.clientName !== "Paperclip disposable CLI" || request.requestedCompanyId !== input.companyId || request.requiresSignIn !== false || request.requestedWrite !== false || request.requestedConfigure !== false || request.agentConnection === true
|| !request.companies?.some((company: { id: string; name: string }) => company.id === input.companyId && company.name === input.device!.companyName)) throw new Error("Device consent does not match the exact owned read-only request");
step = "device-consent-page"; await page.getByRole("heading", { name: "Connect Paperclip disposable CLI to Paperclip", exact: true }).waitFor();
await page.getByText(input.credential.toUpperCase(), { exact: true }).waitFor();
await page.getByText(input.device.companyName, { exact: true }).waitFor();
await page.getByText("Read all of your Paperclip data", { exact: true }).waitFor();
if (await page.getByText("Write all of your Paperclip data", { exact: true }).isVisible()) throw new Error("Device read-only request unexpectedly offers write authority");
const approval = page.waitForResponse(response => new URL(response.url()).origin === base.origin && new URL(response.url()).pathname === "/api/mcp/device/consent" && response.request().method() === "POST");
step = "device-approve"; await page.getByRole("button", { name: "Connect organization", exact: true }).click();
const approved = await approval, submitted = approved.request().postDataJSON();
if (!approved.ok() || (await approved.json()).status !== "approved" || submitted.userCode !== input.credential || submitted.companyId !== input.companyId || submitted.decision !== "approve" || submitted.allowWrites !== false || submitted.allowConfiguration !== false) throw new Error("Actual device UI did not approve exactly the selected read-only company/code");
await page.getByRole("heading", { name: "Access approved", exact: true }).waitFor();
return { processIsolation: true, testRunnerAttached: false, tracingEnabled: false, videoEnabled: false, apiStepRecorder: false, deviceSessionVerified: true, currentCodeVisible: true, companyConsentVisible: true, readScopeVisible: true, humanApproved: true };
}
if (input.mode === "canary") {
await page.setContent('<input type="password" aria-label="App key" autocomplete="off">');
const field = page.getByLabel("App key"); await field.fill(input.credential);
if (await field.inputValue() !== input.credential) throw new Error("Canary entry failed");
return { processIsolation: true, testRunnerAttached: false, credentialEntered: true, tracingEnabled: false, videoEnabled: false, apiStepRecorder: false };
}
if(input.mode === "slack-wizard-confirm" || input.mode === "slack-wizard-resume"){
const identity=input.slackIdentity;
if(!validSlackWizardIdentityInput(input.companyId,identity,input.setupURL)||!identity)throw new Error("Invalid finite normal Slack wizard identity operation");
const session=await context.request.get("/api/auth/get-session");if(!session.ok()||(await session.json()).user?.id!==identity.userId)throw new Error("Slack wizard requires the exact same signed-in person");
const endpointPath=`/api/chat-endpoints/${identity.endpointId}`;
const before=await context.request.get(endpointPath);if(!before.ok())throw new Error("Slack wizard endpoint missing");const endpoint=await before.json();if(endpoint.companyId!==input.companyId||endpoint.provider!=="slack"||endpoint.status!=="verifying"||endpoint.setup?.step!=="test")throw new Error("Normal Slack wizard is not waiting at actual verified identity/test boundary");
const principals=await context.request.get(`${endpointPath}/principals`);if(!principals.ok())throw new Error("Slack command discovery unavailable");
const rows=await principals.json() as Array<{principalId:string;externalDetail:string;lastConnectAt?:string;status:string;paperclipUserId?:string}>;const candidate=rows.filter(row=>row.principalId===identity.principalId&&row.externalDetail===identity.externalDetail&&row.lastConnectAt&&Date.parse(row.lastConnectAt)>=Date.parse(endpoint.setup.testStartedAt));if(candidate.length!==1||(input.mode==="slack-wizard-resume"?(candidate[0]!.status!=="linked"||candidate[0]!.paperclipUserId!==identity.userId):candidate[0]!.status==="linked"))throw new Error("Genuine current Slack connect receipt is required before wizard ownership confirmation");
await page.goto(input.setupURL,{waitUntil:"domcontentloaded"});
if(input.mode==="slack-wizard-confirm"){
const intentResponse=page.waitForResponse(response=>new URL(response.url()).pathname===`${endpointPath}/principals/${identity.principalId}/link-intent`&&response.request().method()==="POST"&&response.status()===201);
const confirmed=page.waitForResponse(response=>new URL(response.url()).pathname==="/api/chat-identity-links/confirm"&&response.request().method()==="POST"&&response.ok());
await page.getByRole("button",{name:`Link ${identity.externalLabel} to my Paperclip account`,exact:true}).click();
const intent=await(await intentResponse).json(),token=new URL(intent.confirmationUrl,base.origin).searchParams.get("token"),confirmation=await confirmed;
if(!token||confirmation.request().postDataJSON()?.token!==token||(await confirmation.json()).endpointId!==identity.endpointId)throw new Error("Actual Slack ownership button did not confirm its own generated scoped link");
}
await page.getByText("Linked to you",{exact:true}).waitFor();
const linkedResponse=await context.request.get(`${endpointPath}/principals`);if(!linkedResponse.ok()||!(await linkedResponse.json()).some((row:{principalId:string;status:string;paperclipUserId?:string})=>row.principalId===identity.principalId&&row.status==="linked"&&row.paperclipUserId===identity.userId))throw new Error("Wizard ownership did not persist as the exact same person");
await page.getByRole("button",{name:"Continue to message test",exact:true}).click();
const activation=page.waitForResponse(response=>new URL(response.url()).pathname===`${endpointPath}/finish`&&response.request().method()==="POST"&&response.ok());
await page.getByRole("button",{name:"Done",exact:true}).click();const activated=await(await activation).json();
const afterResponse=await context.request.get(endpointPath);if(!afterResponse.ok())throw new Error("Activated Slack endpoint unavailable");const after=await afterResponse.json();
if(activated.id!==identity.endpointId||activated.status!=="active"||after.status!=="active"||after.companyId!==input.companyId||after.setup?.step!=="complete")throw new Error("Actual wizard Done did not independently save activation");
return {processIsolation:true,testRunnerAttached:false,tracingEnabled:false,videoEnabled:false,apiStepRecorder:false,endpointId:identity.endpointId,principalId:identity.principalId,paperclipUserId:identity.userId,identityConfirmed:true,identityUiVisible:true,humanSubmittedSecurely:true,activationUiVisible:true,endpointActive:true,identityWasAlreadyLinked:input.mode==="slack-wizard-resume"};
}
if(input.mode === "slack-endpoint-configure" || input.mode === "slack-endpoint-reconnect"){
const slack=input.slackSetup;
if(!validSlackSetupPrivate(input.companyId,slack,input.setupURL,input.mode==="slack-endpoint-reconnect")||!slack||!input.companyId)throw new Error("Invalid finite existing Slack credential form");
const endpointPath=`/api/chat-endpoints/${slack.endpointId}`,beforeResponse=await context.request.get(endpointPath);
if(!beforeResponse.ok())throw new Error("Owned Slack draft unavailable");
const before=await beforeResponse.json();
if(before.companyId!==input.companyId||before.provider!=="slack"||before.assignedAgentId!==slack.agentId||before.setup?.slackSetupMethod!=="existing"||(input.mode==="slack-endpoint-reconnect"?before.providerAccountId!==slack.workspaceId:!!before.providerAccountId))throw new Error("Slack credential form must target the exact fresh existing-app draft");
await page.goto(input.setupURL,{waitUntil:"domcontentloaded"});
const bot=page.getByLabel("Bot User OAuth Token",{exact:true}),secret=page.getByLabel("Signing Secret",{exact:true});
await bot.waitFor();if(await bot.getAttribute("type")!=="password"||await secret.getAttribute("type")!=="password")throw new Error("Slack credential fields are not protected");
await bot.fill(input.credential);await secret.fill(slack.signingSecret);
if(await page.getByText("Your Bot User OAuth Token must start with",{exact:false}).isVisible()){
return {processIsolation:true,testRunnerAttached:false,tracingEnabled:false,videoEnabled:false,apiStepRecorder:false,endpointId:slack.endpointId,slackCredentialUiVisible:true,humanSubmittedSecurely:false,credentialUiRejected:true};
}
const saved=page.waitForResponse(response=>new URL(response.url()).pathname===`${endpointPath}/setup`&&response.request().method()==="POST"&&response.ok());
await page.getByRole("button",{name:input.mode==="slack-endpoint-reconnect"?"Reconnect Slack app":"Connect Slack app",exact:true}).click();const response=await saved,submitted=response.request().postDataJSON();
if(submitted.action!==(input.mode==="slack-endpoint-reconnect"?"reconnect":"configure")||submitted.credentials?.botToken!==input.credential||submitted.credentials?.signingSecret!==slack.signingSecret)throw new Error("Slack UI submitted another private setup operation");
const result=await response.json(),afterResponse=await context.request.get(endpointPath);if(!afterResponse.ok())throw new Error("Saved Slack endpoint unavailable");const after=await afterResponse.json();
if(result.id!==slack.endpointId||after.id!==slack.endpointId||after.companyId!==input.companyId||after.assignedAgentId!==slack.agentId||after.providerAccountId!==slack.workspaceId||!after.setup?.webhookSecretConfigured)throw new Error("Slack UI credential save did not persist the intended real workspace/agent");
await bot.waitFor({state:"hidden"});const storageClean=await page.evaluate(values=>!values.some(value=>JSON.stringify([Object.entries(localStorage),Object.entries(sessionStorage),document.body.innerText,location.href]).includes(value)),[input.credential,slack.signingSecret]);
return {processIsolation:true,testRunnerAttached:false,tracingEnabled:false,videoEnabled:false,apiStepRecorder:false,endpointId:slack.endpointId,slackCredentialUiVisible:true,humanSubmittedSecurely:true,storageClean};
}
if (input.mode === "slack-identity-confirm") {
const identity = input.slackIdentity;
if (!validSlackIdentityInput(input.companyId, identity, input.setupURL) || !identity || !input.companyId) throw new Error("Invalid finite Slack confirmation operation");
const session = await context.request.get("/api/auth/get-session");
if (!session.ok() || (await session.json()).user?.id !== identity.userId) throw new Error("Slack confirmation must use the same actual signed-in fixture human");
const principalsPath = `/api/chat-endpoints/${identity.endpointId}/principals`;
async function principal() {
const response = await context.request.get(principalsPath);
if (!response.ok()) throw new Error("Slack principal cannot be independently read");
const rows = await response.json() as Array<{ principalId:string; status:string; paperclipUserId?:string; externalDetail?:string }>;
const matches = rows.filter(row => row.principalId === identity!.principalId && row.externalDetail === identity!.externalDetail);
if (matches.length !== 1) throw new Error("Slack principal identity is ambiguous");
return matches[0]!;
}
if ((await principal()).status === "linked") throw new Error("Slack identity was already linked before human confirmation");
await page.goto(input.setupURL, {waitUntil:"domcontentloaded"});
const row = page.getByRole("list", {name:"People",exact:true}).getByRole("listitem").filter({has:page.getByText(identity.externalLabel,{exact:true})});
if(await row.count()!==1) throw new Error("Exact unlinked Slack identity is not uniquely visible in Access");
const issued=page.waitForResponse(response=>new URL(response.url()).pathname===`${principalsPath}/${identity.principalId}/link-intent`&&response.request().method()==="POST"&&response.status()===201);
await row.getByRole("button", {name:"Create confirmation link",exact:true}).click();
const intent=await(await issued).json() as {confirmationUrl?:string};
const confirmation=new URL(intent.confirmationUrl??"",base.origin),token=confirmation.searchParams.get("token");
if(confirmation.origin!==base.origin||confirmation.pathname!=="/chat-identity/confirm"||!token||token.length<32||token.length>8192||confirmation.hash||confirmation.searchParams.size!==1) throw new Error("Visible identity form returned another confirmation surface");
const previewResponse = await context.request.get(`/api/chat-identity-links/preview?token=${encodeURIComponent(token)}`);
if (!previewResponse.ok() || !matchingSlackIdentityPreview(await previewResponse.json(), input.companyId, identity)) throw new Error("Slack link preview targets another company, endpoint or external human");
await page.goto(confirmation.href, {waitUntil:"domcontentloaded"});
await page.getByRole("heading", {name:"Link your external identity",exact:true}).waitFor();
await page.getByText(identity.externalDetail, {exact:false}).waitFor();
const confirmed = page.waitForResponse(response => new URL(response.url()).pathname === "/api/chat-identity-links/confirm" && response.request().method() === "POST" && response.ok());
await page.getByRole("button", {name:"Confirm identity",exact:true}).click();
const response = await confirmed;
if (response.request().postDataJSON()?.token !== token) throw new Error("Slack confirmation submitted a different one-use token");
const result = await response.json() as {ok?:boolean;endpointId?:string};
if (result.ok !== true || result.endpointId !== identity.endpointId) throw new Error("Slack confirmation returned another endpoint");
await page.getByRole("heading", {name:"Identity linked",exact:true}).waitFor();
const after = await principal();
const afterSession = await context.request.get("/api/auth/get-session");
if (after.status !== "linked" || after.paperclipUserId !== identity.userId || !afterSession.ok() || (await afterSession.json()).user?.id !== identity.userId) throw new Error("Confirmed Slack authority does not bind the same current signed-in person");
return {processIsolation:true,testRunnerAttached:false,tracingEnabled:false,videoEnabled:false,apiStepRecorder:false,identityConfirmed:true,identityUiVisible:true,principalId:identity.principalId,paperclipUserId:identity.userId,humanSubmittedSecurely:true};
}
if (input.mode === "grok-routine-configure") {
const grok = input.grok;
if (!validGrokRoutinePrivateInput(input.companyId, grok, input.setupURL) || !grok) {
throw new Error("Invalid finite Grok routine operation");
}
const endpoint = `/api/companies/${input.companyId}/agents/${grok.agentId}/grok-binding`;
const beforeResponse = await context.request.get(endpoint);
if (!beforeResponse.ok()) throw new Error("Existing Grok binding is unavailable");
const before = await beforeResponse.json() as { enabled?: boolean; canConfigureConnection?: boolean; binding?: { id?: string; connected?: boolean; routine?: { revision?: number; configured?: boolean } } };
if (!before.enabled || !before.canConfigureConnection || before.binding?.id !== grok.bindingId ||
before.binding.connected !== true || (before.binding.routine?.revision ?? 0) !== grok.expectedRevision) {
throw new Error("Genuine connected Grok binding and exact revision are required");
}
await page.goto(input.setupURL, { waitUntil: "domcontentloaded" });
const section = page.getByRole("region", { name: "Grok Bot connection", exact: true });
await section.waitFor();
if (before.binding.routine?.configured) {
await section.getByRole("button", { name: "Replace routine credentials", exact: true }).click();
}
const urlField = section.getByLabel("Webhook URL", { exact: true });
const keyField = section.getByLabel("Webhook key", { exact: true });
if (await urlField.getAttribute("type") !== "password" || await keyField.getAttribute("type") !== "password") {
throw new Error("Grok credential fields are not protected");
}
await section.getByLabel("Bot name in Grok", { exact: true }).fill(grok.botName);
await urlField.fill(grok.webhookUrl);
await keyField.fill(input.credential);
const savedResponse = page.waitForResponse(response => new URL(response.url()).pathname === `${endpoint}/routine` &&
response.request().method() === "PUT" && response.ok());
await section.getByRole("button", { name: "Save routine", exact: true }).click();
const response = await savedResponse;
const submitted = response.request().postDataJSON() as { bindingId?: string; expectedRevision?: number; botName?: string; webhookUrl?: string; webhookKey?: string };
if (submitted.bindingId !== grok.bindingId || submitted.expectedRevision !== grok.expectedRevision ||
submitted.botName !== grok.botName || submitted.webhookUrl !== grok.webhookUrl || submitted.webhookKey !== input.credential) {
throw new Error("Grok form did not submit the intended scoped private configuration");
}
const saved = await response.json() as { bindingId?: string; configured?: boolean; verified?: boolean; revision?: number };
if (!matchesConfiguredGrokRoutine(saved, grok.bindingId, grok.expectedRevision)) {
throw new Error("Grok routine response does not match the configured unverified binding");
}
await section.getByRole("button", { name: "Verify connection", exact: true }).waitFor();
await keyField.waitFor({ state: "detached" });
const afterResponse = await context.request.get(endpoint);
if (!afterResponse.ok()) throw new Error("Saved Grok binding could not be independently read");
const after = await afterResponse.json() as { binding?: { id?: string; connected?: boolean; routine?: { configured?: boolean; revision?: number } } };
if (after.binding?.id !== grok.bindingId || after.binding.connected !== true ||
after.binding.routine?.configured !== true || after.binding.routine.revision !== saved.revision) {
throw new Error("Saved Grok configuration did not persist on the same connected binding");
}
const storageClean = await page.evaluate(values => !values.some(value =>
JSON.stringify([Object.entries(localStorage), Object.entries(sessionStorage), document.body.innerText, location.href]).includes(value)),
[input.credential, grok.webhookUrl]);
return { processIsolation: true, testRunnerAttached: false, tracingEnabled: false, videoEnabled: false, apiStepRecorder: false,
bindingId: grok.bindingId, routineConfigured: true, routineUiVisible: true, humanSubmittedSecurely: true, storageClean };
}
if(input.mode === "personal-secret-create"){
const personal=input.personal,setup=new URL(input.setupURL);
if (personal?.viaMySecrets !== undefined && typeof personal.viaMySecrets !== 'boolean') throw new Error('Invalid finite personal secret navigation');
if(!input.companyId||!/^[a-f0-9-]{36}$/i.test(input.companyId)||!personal||!/^[a-f0-9-]{36}$/i.test(personal.definitionId)||!personal.userId||personal.userId.length>160||!setup.pathname.endsWith('/company/settings/secrets')||(personal.viaMySecrets ? setup.search!=='' : setup.searchParams.get('definition')!==personal.definitionId))throw new Error('Invalid finite personal secret operation');
if (!personal.viaMySecrets) await page.goto(input.setupURL,{waitUntil:'domcontentloaded'});
const session=await context.request.get('/api/auth/get-session');if(!session.ok()||(await session.json()).user?.id!==personal.userId)throw new Error('Personal secret human identity mismatch');
const companyResponse = await context.request.get(`/api/companies/${input.companyId}`);
const company = companyResponse.ok() ? await companyResponse.json() as { id: string; issuePrefix: string } : null;
if (company?.id !== input.companyId || setup.pathname !== `/${company.issuePrefix}/company/settings/secrets`) throw new Error('Personal secret company navigation mismatch');
const own=await context.request.get(`/api/companies/${input.companyId}/me/user-secrets`);if(!own.ok())throw new Error('Personal definition not readable');
const entries=await own.json() as Array<{definition:{id:string;key:string;name:string};secret:unknown}>;const entry=entries.find(row=>row.definition.id===personal.definitionId);if(!entry||entry.definition.key!=='PERSONAL_PUBLISHING_KEY'||entry.secret)throw new Error('Personal definition already configured or mismatched');
if (personal.viaMySecrets) {
const settings = new URL(setup); settings.pathname = settings.pathname.replace(/\/secrets$/, '');
await page.goto(settings.href,{waitUntil:'domcontentloaded'});
await page.getByRole('link',{name:'Secrets',exact:true}).filter({visible:true}).click();
await page.getByRole('tab',{name:'My secrets',exact:true}).click();
await page.getByRole('listitem').filter({hasText:entry.definition.key}).getByRole('button',{name:'Set value',exact:true}).click();
} else await page.getByRole('button',{name:'Set my value',exact:true}).click();
const dialog=personalSecretEntryDialog(page);await dialog.getByRole('heading',{name:/^Set your value/}).waitFor();
if(!await dialog.getByRole('heading',{name:'Set your value for Personal publishing credential',exact:true}).isVisible())throw new Error('Wrong personal credential form');
await dialog.getByRole('textbox').fill(input.credential);
const created=page.waitForResponse(response=>new URL(response.url()).pathname===`/api/companies/${input.companyId}/me/user-secrets`&&response.request().method()==='POST'&&response.status()===201);
await dialog.getByRole('button',{name:'Save value',exact:true}).click();const secret=await(await created).json() as {id:string;ownerUserId:string;userSecretDefinitionId:string};
if(!/^[a-f0-9-]{36}$/i.test(secret.id)||secret.ownerUserId!==personal.userId||secret.userSecretDefinitionId!==personal.definitionId)throw new Error('Personal value owner/definition mismatch');await dialog.waitFor({state:'hidden'});
const storageClean=await page.evaluate(value=>!JSON.stringify([Object.entries(localStorage),Object.entries(sessionStorage),document.body.innerText,location.href]).includes(value),input.credential);
return {processIsolation:true,testRunnerAttached:false,tracingEnabled:false,videoEnabled:false,apiStepRecorder:false,secretId:secret.id,secretUiVisible:true,humanSubmittedSecurely:true,storageClean};
}
if(input.mode === "company-secret-create"){
if(!validCompanySecretMetadata(input.companyMetadata))throw new Error("Invalid requested company secret metadata");
const requestedName=input.companyMetadata?.name??"Deployment API key",requestedKey=input.companyMetadata?.key;
if(!input.companyId||!/^[a-f0-9-]{36}$/i.test(input.companyId)||!new URL(input.setupURL).pathname.endsWith("/company/settings/secrets"))throw new Error("Invalid finite company secret operation");
await page.goto(input.setupURL,{waitUntil:"domcontentloaded"});await page.getByRole("button",{name:"New secret",exact:true}).first().click();
const dialog=page.getByRole("dialog").filter({has:page.getByRole("radiogroup",{name:"Who provides the value",exact:true})});await dialog.getByLabel("Name",{exact:true}).fill(requestedName);
await dialog.getByRole("radiogroup",{name:"Who provides the value",exact:true}).getByRole("radio",{name:/^Organization\b/}).click();
if(requestedKey){const more=dialog.getByRole("button",{name:"More options",exact:true});if(await more.getAttribute("aria-expanded")!=="true")await more.click();await dialog.getByLabel("Key",{exact:true}).fill(requestedKey);}
const storedIn=dialog.getByLabel("Stored in",{exact:true});if(await storedIn.count()){const local=await storedIn.locator("option").evaluateAll(options=>options.map(option=>(option as HTMLOptionElement).value).filter(value=>value.startsWith("local_encrypted:")));if(local.length!==1)throw new Error("Paperclip-encrypted storage is not a single choice");await storedIn.selectOption(local[0]!);}
await dialog.getByLabel("Value",{exact:true}).fill(input.credential);
const created=page.waitForResponse(response=>new URL(response.url()).pathname===`/api/companies/${input.companyId}/secrets`&&response.request().method()==="POST"&&response.status()===201);
await dialog.getByRole("button",{name:"Create secret",exact:true}).click();const response=await created,posted=response.request().postDataJSON() as {name?:string;key?:string};
if(posted.name!==requestedName||requestedKey&&posted.key!==requestedKey)throw new Error("Outgoing company secret metadata differs from the actual request");
const submittedKey=requestedKey??posted.key??requestedName,secret=await response.json() as {id:string;companyId:string;name:string;key:string;scope:string;status:string;provider:string};
if(secret.companyId!==input.companyId||secret.provider!=="local_encrypted"||secret.scope!=="company"||secret.status!=="active"||secret.name!==requestedName||secret.key!==canonicalCompanySecretKey(submittedKey))throw new Error("Saved company secret metadata differs from the actual request");
if(!/^[a-f0-9-]{36}$/i.test(secret.id))throw new Error("Invalid created secret identity");await dialog.waitFor({state:"hidden"});
await page.getByText(requestedName,{exact:true}).first().waitFor();
const storageClean=await page.evaluate(secret=>!JSON.stringify([Object.entries(localStorage),Object.entries(sessionStorage),document.body.innerText,location.href]).includes(secret),input.credential);
return {processIsolation:true,testRunnerAttached:false,tracingEnabled:false,videoEnabled:false,apiStepRecorder:false,secretId:secret.id,secretUiVisible:true,humanSubmittedSecurely:true,storageClean,requestedCompanyName:requestedName,requestedCompanyKey:submittedKey,savedCompanyName:secret.name,savedCompanyKey:secret.key};
}
if (input.mode === "gateway-token-rotation") {
const g = input.gateway;
if (!g || !/^[a-f0-9-]{36}$/i.test(g.id) || !/^[a-f0-9-]{36}$/i.test(g.oldTokenId) || (g.pendingTokenId !== undefined && !/^[a-f0-9-]{36}$/i.test(g.pendingTokenId)) || !g.otherToken || g.otherToken.length > 8192 || !g.readName || g.readName.length > 240) throw new Error("Invalid finite gateway rotation request");
const setup = new URL(input.setupURL), endpoint = new URL(g.endpointPath, base.origin);
if (!setup.pathname.endsWith(`/apps/gateways/${g.id}/tokens`) || endpoint.origin !== base.origin || !/^\/mcp\/gateways\/[^/]+$|^\/api\/tool-gateway\/gateways\/[^/]+\/mcp$/.test(endpoint.pathname)) throw new Error("Invalid gateway UI/protocol boundary");
async function discover(token: string) {
const response = await fetch(endpoint, { method: "POST", redirect: "error", signal: AbortSignal.timeout(15_000), headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify({jsonrpc:"2.0",id:1,method:"tools/list",params:{}}) });
const status = response.status; if (!response.ok) { await response.body?.cancel(); return { status, ok: false }; }
const body = await response.json() as {result?: {tools?: Array<{name: string}>}}; return {status, ok: body.result?.tools?.some(tool => tool.name === g!.readName) === true};
}
await page.goto(input.setupURL, {waitUntil:"domcontentloaded"});
let replacement: {id: string; token: string};
if (g.pendingTokenId) {
// The agent created the replacement; the person reveals its value once.
const waiting = page.getByRole("region", {name:"Tokens waiting to be revealed"});
const revealed = page.waitForResponse(response => new URL(response.url()).pathname === `/api/tool-gateway/gateway-tokens/${g.pendingTokenId}/reveal` && response.request().method() === "POST" && response.status() === 200);
await waiting.getByRole("button", {name:/^Reveal token /}).click();
replacement = await (await revealed).json() as {id: string; token: string};
if (replacement.id !== g.pendingTokenId) throw new Error("Revealed a different token than the agent created");
} else {
await page.getByRole("button", {name:"Issue token",exact:true}).click();
await page.getByRole("textbox", {name:/^Token name/}).fill("Replacement test client");
const issued = page.waitForResponse(response => new URL(response.url()).pathname === `/api/tool-gateway/gateways/${g.id}/tokens` && response.request().method() === "POST" && response.status() === 201);
await page.locator("form").getByRole("button", {name:"Issue token",exact:true}).click();
replacement = await (await issued).json() as {id: string; token: string};
}
if (!/^[a-f0-9-]{36}$/i.test(replacement.id) || typeof replacement.token !== "string" || !replacement.token || replacement.token.length > 8192) throw new Error("Invalid one-time token");
await page.getByText("New token — copy now", {exact:true}).waitFor();
if (!await page.locator("code").filter({hasText:replacement.token}).count()) throw new Error("One-time token was not visibly delivered");
const newWorkedBeforeRevocation = (await discover(replacement.token)).ok, oldWorkedUntilRevocation = (await discover(input.credential)).ok;
if (!newWorkedBeforeRevocation || !oldWorkedUntilRevocation) throw new Error("Replacement must work before retiring old credential");
await page.getByRole("button", {name:"Dismiss new token",exact:true}).click();
// Identify the old token by its saved prefix: an agent may give the replacement the same name.
const oldRow = page.getByRole("row").filter({has:page.getByText(`pcgw_${g.oldTokenId.slice(0, 8)}•••`, {exact:true})});
if (!await oldRow.isVisible()) await page.getByRole("button", {name:/Token history/}).click();
await oldRow.getByRole("button", {name:"Revoke",exact:true}).click();
const dialog = page.getByRole("dialog"); await dialog.getByLabel("Type the token name to confirm", {exact:true}).fill("Old test client");
const revoked = page.waitForResponse(response => new URL(response.url()).pathname === `/api/tool-gateway/gateway-tokens/${g.oldTokenId}/revoke` && response.request().method() === "POST" && response.ok());
await dialog.getByRole("button", {name:"Revoke token",exact:true}).click(); await revoked; await dialog.waitFor({state:"hidden"});
const old = await discover(input.credential), fresh = await discover(replacement.token), other = await discover(g.otherToken);
await writeFile(path.join(path.dirname(process.argv[2]!), "token.json"), JSON.stringify({id:replacement.id,token:replacement.token}), {mode:0o600});
return {processIsolation:true,testRunnerAttached:false,tracingEnabled:false,videoEnabled:false,apiStepRecorder:false,newTokenId:replacement.id,tokenUiVisible:true,newWorkedBeforeRevocation,oldWorkedUntilRevocation,revealedPending:!!g.pendingTokenId,oldRevoked:true,newWorksAfter:fresh.ok,oldStatusAfter:old.status,otherWorksAfter:other.ok};
}
step = "secure-key-address"; await page.goto(input.setupURL, { waitUntil: "domcontentloaded" });
await page.getByRole("textbox", { name: "MCP server URL", exact: true }).fill(input.providerURL); await page.getByRole("button", { name: "Continue", exact: true }).click();
await page.getByRole("button", { name: "Check link", exact: true }).click();
step = "secure-key-field"; const key = page.getByLabel("App key", { exact: true }); await key.waitFor({ state: "visible" });
const passwordInput = await key.getAttribute("type") === "password", autocompleteOff = await key.getAttribute("autocomplete") === "off";
step = "secure-key-connect"; await key.fill(input.credential);
const created = page.waitForResponse(response => new URL(response.url()).pathname.endsWith("/tools/apps/connect") && response.request().method() === "POST" && response.status() === 201);
await page.getByRole("button", { name: "Check link", exact: true }).click();
const connectionId = (await (await created).json() as { connectionId: string }).connectionId;
if (!/^[a-f0-9-]{36}$/i.test(connectionId)) throw new Error("Secure setup did not return a bounded connection identity");
step = "secure-key-ready"; await page.getByRole("heading", { name: /is ready\./i }).waitFor({ state: "visible" });
const storageClean = await page.evaluate(secret => !JSON.stringify([Object.entries(localStorage), Object.entries(sessionStorage), location.href]).includes(secret), input.credential);
await page.getByRole("button", { name: "View connection", exact: true }).click(); await key.waitFor({ state: "detached" });
return { processIsolation: true, testRunnerAttached: false, tracingEnabled: false, videoEnabled: false, apiStepRecorder: false, connectionId, challengeVisible: true, passwordInput, autocompleteOff, humanSubmittedSecurely: true, visibleReady: true, storageClean };
} finally { await browser.close(); }
}
main().then(receipt => process.stdout.write(JSON.stringify(receipt))).catch(async (error: unknown) => {
await recordPrivateChildFailure(path.dirname(process.argv[2]!), step, error);
process.stderr.write("Private browser operation failed; diagnostics withheld"); process.exitCode = 1;
});