Files
PaperClipAI/server/src/__tests__/browser-use-connection.test.ts
T
Nicky LeachandClaude Opus 5.5 11ac90e31f fix(build): typecheck server/src/__tests__ and fix every type error (#18739)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The server has about 1,100 test and helper files in
`server/src/__tests__`
> - `server/tsconfig.json` excludes that directory, Vitest does not
check types, and no script runs the tsconfig in that directory
> - So a test can call a function with the wrong arguments and still
pass, and no tool reports it
> - This gap hid 2,168 real type errors, and some of them hid tests that
did not test what they claim
> - This pull request fixes all of these errors and adds the directory
to the server layers from paperclipai/paperclip#18737
> - The benefit is that a type error in a server test now fails the
typecheck, with no baseline and no backlog

## Linked Issues or Issue Description

Refs paperclipai/paperclip#18737, which is merged and added the server
layers. Refs paperclipai/paperclip#18738, which is merged and checks the
test projects outside `pnpm -r`, so it is not a duplicate. Refs
paperclipai/paperclip#18137, which records that this directory has no
check.

**What happened?**

No tool checked the types of 1,074 of the 1,101 files in
`server/src/__tests__`. I searched `package.json`, the workflows, and
all tsconfig files. 27 files are in the program of another tsconfig, and
these 27 have 0 errors. 11 of the 27 are in
`helpers/maintenance/tsconfig.json`. No script ran that project when I
measured this. paperclipai/paperclip#18738 now runs it.

**Expected behavior**

The server typecheck checks these files, and a type error fails the
check.

**Steps to reproduce**

1. On the base commit, run `tsc -p server/src/__tests__/tsconfig.json
--noEmit`. It reports 6,536 errors and needs 12.3 GiB.
2. Add a type error to any file in `server/src/__tests__`.
3. Run `pnpm --filter @paperclipai/server typecheck`. It passes.

**Paperclip version or commit**

I measured the error breakdown below on commit `8dfd632452`, with
TypeScript 7.0.2. The branch now includes master up to
paperclipai/paperclip#18865.

**Deployment mode**

Not applicable. This issue applies to development and CI. No production
source file in the server changes.

### Error breakdown

Each row adds one change to the row above.

| Step | Total errors | Change | Cause |
| --- | --- | --- | --- |
| Config as committed | 6,536 | | |
| Add `src/types/*.d.ts` | 4,447 | -2,089 | Config. The Express request
types were missing. |
| Set `rootDir` to the repository root | 2,436 | -2,011 | Config. |
| Set `allowImportingTsExtensions` | 2,181 | -255 | Config. Vitest
accepts `.ts` imports. |
| Add `ES2024.Promise` to `lib` | 2,138 | -43 | Config. Node 24 has
`Promise.withResolvers`. |
| Set `allowJs` for `.mjs` imports | 2,148 | +10 | Config. It removes 8
`TS7016` errors and shows new real errors. |
| Set `declaration` to false | 2,142 | -6 | Config. Tests emit nothing.
|
| Count only files in `server/src` | 2,058 | -84 | Other packages: 53
Daytona, 29 Runner test, 2 UI. |

So 4,478 of the 6,536 errors (69%) came from config or scope, and 2,058
are real type errors in 314 files. The 29 Runner test errors are also
real, which gives 2,087 errors. Tests that master added after this
measurement had 81 more errors, so this pull request fixes 2,168 errors.

This table classifies the first 2,058 errors.

| Kind of real error | Errors |
| --- | --- |
| Wrong argument or assignment type | 1,023 |
| Stale or partial fixture objects | 525 |
| Property that the type does not have | 317 |
| Missing narrowing of null or unknown | 101 |
| Implicit any | 60 |
| Missing name or module | 16 |
| Other | 16 |

### What the type errors hid

Most errors were stale fixtures and loose mocks. These errors hid real
test problems, and this pull request fixes them:

- **A test that never ran.** `instrumentation.test.ts` required
`sdk-trace-base`, which is not installed. So 3 span tests were skipped.
They now run and pass.
- **Assertions that could not fail.**
- `issue-thread-interactions-service` used a company ID as an issue ID.
- `status-card-evaluation-work` compared `[]` with `[]` in a rollback
check.
- `slack-conversation-lifecycle` read `item.issueId`, which does not
exist.
  - `email-channels` used a permission key that does not exist.
- `heartbeat-project-env` imported `LOW_TRUST_REVIEW_PRESET`, which is
not exported. The value was undefined.
- **Postgres processes that did not stop.** 11 test files called `stop`
on the test database helper. That method does not exist, and the helper
method is `cleanup`.
- **Arguments that the service ignored.**
- `methodKey` is now `connectionMethodKey` in the Daytona, Unikraft,
1Password, and Railway tests.
- 108 calls passed `"owner-user"` to a parameter that expects an options
object.
- 9 chat wizard fixtures passed `managementContext` as `source`, so they
ran `existing_app` and not `manifest`.
  - 2 tests passed a `{ userId }` actor that the function ignores.
- Inserts into `environments` passed a `companyId` column that the table
does not have.
- **Problems in tests that master added during this work.**
- `sandbox-creation-operations` read `database.connectionString` when an
external test database was set. In that mode `database` is undefined, so
the fixture would stop with a TypeError. It now uses the external URL.
- `idle-completed-history` set a Dot operation to `dispatched`. That is
a Muse status, and the Dot type does not have it. The fixture now uses
`admitted`, which is a real in-flight Dot status.

### Possible product problems, not changed

These may be real problems in production code or types. I did not change
them, because this pull request does not change production behavior:

- `decisions-service` `service.create` does not validate the lenient
`cancel_issue_tree` input.
- `createConnection` ignores `input.connectionPurpose`.
- The `createConnection` route schema default stores `{}` for
`transportConfig`.
- `prepareManagedAiRuntime` accepted the invalid value `mode:
"connection"`.
- `createBinding` accepts invalid `projectionClass` strings outside
class 3.
- `ensureManagedProjectWorkspace` forwards an untyped `repoRef` by
spread.
- `routineVariableSchema.label` is optional, but `RoutineVariable.label`
is required.
- `ToolRunContext.projectId` may be too strict.
- `withdrawInteraction` reads `issue.status`, which its type omits.
- `issueService.list` and `reconcileNativeFinalizations` omit fields
that they return at runtime.
- `createGitHubExternalObjectProvider` handles `tokenProvider: null`,
but its type forbids this value.

## What Changed

- Fixed the 2,168 type errors. The diff changes 322 test and helper
files.
- Corrected `server/src/__tests__/tsconfig.json`. Editors and the
typecheck now read the same options and report the same errors.
- Added three `test-tree` layers to
`server/scripts/typecheck-layers.mjs`. They read the server layers as
declarations, and any error fails them. A hash of the path selects the
shard, so a new file does not move other files.
- Added `server/scripts/typecheck-stubs/daytona-sdk.d.ts`. Server tests
reach Daytona plugin source, and the root install does not have
`@daytonaio/sdk`.
- Changed two imports in `ui/src/lib/issue-execution-policy.ts` to use
`.js` extensions, because server tests import this file.
- Made `declarationsConfig` and `--declarations-only` skip the
`test-tree` layers, because these layers emit nothing. Other projects
read only the source layers. Added a test for this.
- Updated `doc/DEVELOPING.md`.

## Verification

Run these commands:

```sh
pnpm --filter @paperclipai/server typecheck
pnpm exec vitest run server/scripts/typecheck-layers.test.mjs
```

Results on macOS (arm64), Node 24, 16 cores. Each check ran after the
merge of paperclipai/paperclip#18738 unless the row says otherwise:

| Check | Commit | Result |
| --- | --- | --- |
| Server typecheck, `PAPERCLIP_TYPECHECK_LOW_MEMORY=1`, all 7 layers |
`6e45a08573` | Exit 0, 0 errors, 62 seconds, peak 3.52 GiB |
| `node scripts/typecheck-test-projects.mjs --low-memory` | `a3306bd13e`
| Exit 0, 0 errors, 42 seconds, peak 3.25 GiB |
| The same script with `--reuse-declarations` | `1b878aabc0` | Exit 0, 0
errors |
| `typecheck-layers.test.mjs` and `live-recovery-work.test.ts` |
`01db5634e4` | 90 passed. These include real compiler runs for a pass, a
type error, and an invalid tsconfig. |
| `tsconfig.declarations.json` after a full server typecheck |
`0fb1e4fb0a` | It references only `below`, `cycle`, and `above`. No
generated config names a `tsconfig.layers.json` file. |
| Owned runtime test in `idle-completed-history` | `e8f2f448ac` |
Passed, with the fix that master later replaced |
| `idle-incoming-wake`, `plugin-idle-manager`, and the changed
`chat-channels` test | `6e45a08573` | 30 passed |
| Runner `native-execution.test.ts` | `eab4dcc1f4` | 114 passed |
| The 19 server test files that changed since `eab4dcc1f4`, and
`typecheck-layers.test.mjs` | `259737b700` | 4,275 of 4,277 passed. The
2 failures are in `chat-channels` and are in the list of 7 below. |

The peak is the largest single process, from `/usr/bin/time -l`. Peaks
for the server typecheck were between 3.42 GiB and 3.56 GiB across 8
runs. Peaks for the test projects were 3.25 GiB and 3.37 GiB across 2
runs. I did not measure a cgroup.

**Runtime check of the fixes.** Types do not prove that a test still
works. So I ran all 309 changed server test files, 11,929 tests, before
the rebase onto paperclipai/paperclip#18737:

| Result | Tests | Detail |
| --- | --- | --- |
| Passed | 11,913 | Includes the 2 tests and the 54 `dot-runner` tests
that passed in a later run, as described below. |
| Failed | 7 | The same 7 tests also fail on the commit before the
fixes. |
| Skipped | 9 | These tests have their own skip conditions: `sentry` 5,
and 1 each in `bundled-plugin-registration`, `customer-success-cloud`,
`onepassword-connection`, and `workspace-runtime-https-live-exercise`. |

The 7 tests that fail with and without this change, on my machine:

- 3 tests in `chat-channels` and `email-channels` stop with `ENOENT` on
a `SKILL.md` file. The path is in the parent checkout, above the git
worktree that I used, so I expect that the worktree is the cause. I did
not prove this.
- `workspace-instance-cleanup` compares `/private/var/...` with
`/var/...`. This is a macOS symlink difference.
- 2 tests in `git-reproducible-managed-checkouts` get
`unknown_base_git_files` and expect `unpushed_managed_checkout`. I did
not find the cause.
- 1 more test in `chat-channels`, `materializes direct external-chat
finals once`, gets `undefined`. I did not find the cause.

CI is the check for these 7 tests. `ci / verify` passed on `eab4dcc1f4`.

Failures during this work:

- My fix to `agent-runtime-connector-access` changed an invalid
permission key in the setup, but not in the assertion. The runtime check
found this. I corrected the assertion, and the file passes.
- `heartbeat-plugin-environment` failed one wait in the full run. It
passed three times alone, so I think that load caused the failure. It
can fail again under load.
- `dot-runner` skipped all 54 tests in the full run because `cargo` was
not on my `PATH`. It then passed 54 of 54 with the toolchain on the
`PATH`.
- My first `lib` value removed the default DOM types, and the error
count went up by 375. I added the DOM libraries.
- My Daytona stub with bare `any` left 48 errors, because generic
helpers then inferred `unknown`. I added shapes that return promises.
- One fix to `agent-secrets-routes` used `class_3_static_lease`, and the
test failed. That class allows only specific paths. I reverted to
`unclassified`.
- `ci / General tests (server (8/12))` failed on `259737b700` because of
the flaky port in `idle-completed-history`. paperclipai/paperclip#18853
fixed this test on master, so this pull request keeps that fix.

**Audit of the diff.** I counted these patterns in added and removed
lines, to find fixes that only hide an error:

| Pattern | Added | Removed |
| --- | --- | --- |
| `@ts-ignore` | 0 | 0 |
| `@ts-expect-error` | 0 | 1 |
| `.skip` | 0 | 0 |
| `as any` | 9 | 9 |
| `: any` | 11 | 2 |
| `as never` | 8 | 2 |
| `as unknown as` | 35 | 4 |
| `Promise<unknown>` | 16 | 5 |
| `expect(` | 128 | 113 |

No hunk removes more `expect(` calls than it adds. Three removals of
test content were intentional, and I checked each one:

- `muse-control` removed `signalToken`. Line 1012 already asserts that
it is absent.
- `merged-pr-confirmation-sweep` removed legacy aliases. Other tests
cover them.
- `heartbeat-workspace-branch-containment` removed a `finalize` branch
that no test row can reach.

Some partial fixtures stay unchecked through `as unknown as` and
`Promise<unknown>`, mostly in `issue-agent-mutation-ownership-routes`
and `issue-comment-reopen-routes`.

**Accuracy of the check.** One program over the tree and the three
shards reported the same errors at the same positions. Memory, as the
peak of the largest process on one thread, was 3.93 GiB for one program
and 2.43 GiB for the largest shard. I measured these two values before
the fixes.

Not run or not proven:

- I did not run the full `pnpm test:run`, or a root `pnpm build`. I
expect CI to run the full suites.
- I did not run the 4 GiB container check, or the full
`scripts/typecheck-low-memory.mjs`. The peaks above come from macOS, so
they are not a Linux cgroup measurement.
- I did not run the UI typecheck after the merges.
- `workspace-runtime-https-live-exercise` is skipped, so its `cleanup`
fix did not run.

## Risks

- **A large diff in test files.** 328 files change. A pull request that
edits the same tests can get merge conflicts. New tests on master can
also add type errors before this merges. Any such error fails this check
and needs a fix here.
- **A higher cost for changes to source types.** A change to a type can
add errors in test files that the author did not touch. The author must
then fix them. This is the purpose of the check, but it is new work.
- **Changed test behavior.** Some tests now run paths that they did not
run before, such as the 9 chat wizard fixtures and the 3 span tests.
They pass here, but they can be more sensitive to timing or to the
environment.
- **The Daytona stub can go stale.** The stub is written by hand. If the
plugin uses a new SDK member, the typecheck reports an error until
someone updates the stub.
- **The release path.** `release-verify.yml` and `scripts/release.sh`
run `pnpm -r typecheck`, so they now run this check.
- **The tsconfig change.** The test transform reads
`server/src/__tests__/tsconfig.json`. The options that I changed are for
type checks, and the runs above passed.
- **A gap that remains.** Tsconfigs in `packages/paperclip-runner` and
in most sandbox provider plugins exclude `*.test.ts`. This check does
not read most of those test files, so they can still drift. That work is
separate from this pull request.

> 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

Anthropic Claude Opus 5.5 (`claude-opus-5-5`) through Claude Code, with
reasoning, tool use, code execution, and subagents. The runtime does not
show the context window size.

## 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
- [ ] I have run tests locally and they pass (7 tests fail locally, with
and without this change. See Verification.)
- [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-03 15:27:38 -08:00

1728 lines
79 KiB
TypeScript

import { budgetService, computeObservedSpend } from "../services/budgets.js";
import { buildPaperclipRuntimeMcpServers } from "../services/heartbeat.js";
import { resolveNativeRuntimeMcpSnapshot } from "../services/native-runtime/runtime-context.js";
import express from "express";
import http from "supertest";
import { browserUseRoutes } from "../routes/browser-use.js";
import { errorHandler } from "../middleware/index.js";
import { randomUUID } from "node:crypto";
import { readFile } from "node:fs/promises";
import { and, eq, sql } from "drizzle-orm";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import {
agents,
approvals,
toolActionRequests,
toolInvocations,
toolPolicies,
companies,
companyMemberships,
connectionGrants,
createDb,
heartbeatRuns,
issues,
projects,
toolConnections,
toolApplications,
toolCatalogEntries,
toolConnectionInstalls,
toolProfiles,
toolProfileBindings,
toolProfileEntries,
browserUseSessions,
browserUseRuns,
browserUseBrowsers,
costEvents,
financeEvents,
budgetPolicies,
budgetReservations,
} from "@paperclipai/db";
import { toolAccessService } from "../services/tool-access.js";
import { createToolGatewayService } from "../services/tool-gateway.js";
import { browserUseViewports } from "../services/browser-use-viewport.js";
import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js";
import { registerBrowserUseCleanup } from "../services/browser-use-work.js";
import { browserUseService } from "../services/browser-use.js";
import { applyConnectorSkills, prepareConnectorSkillDelivery, resolveConnectorAssignments } from "../services/connector-runtime.js";
import { registerAssignedMcpGateway } from "../services/native-runtime/assigned-mcp-tools.js";
import { listPaperclipSkillEntries } from "@paperclipai/adapter-utils/server-utils";
import { fileURLToPath } from "node:url";
import path from "node:path";
import {
getEmbeddedPostgresTestSupport,
startEmbeddedPostgresTestDatabase,
} from "./helpers/embedded-postgres.js";
const support = await getEmbeddedPostgresTestSupport();
const actor = { actorType: "user" as const, actorId: "browser-reviewer" };
(support.supported ? describe : describe.skip)(
"Browser Use connection and lifecycle",
() => {
let db: ReturnType<typeof createDb>;
let temp: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
const originalApiUrl = process.env.PAPERCLIP_API_URL;
beforeAll(async () => {
process.env.PAPERCLIP_API_URL = "http://127.0.0.1:3100";
temp = await startEmbeddedPostgresTestDatabase("paperclip-browser-use-");
db = createDb(temp.connectionString);
}, 30000);
afterAll(async () => {
await temp?.cleanup();
if (originalApiUrl === undefined) delete process.env.PAPERCLIP_API_URL;
else process.env.PAPERCLIP_API_URL = originalApiUrl;
});
async function fixture(personal = false) {
const [company] = await db
.insert(companies)
.values({
name: "Browser fixture",
issuePrefix: `BU${randomUUID().slice(0, 6)}`,
})
.returning();
await db.insert(companyMemberships).values({
companyId: company.id,
principalType: "user",
principalId: actor.actorId,
membershipRole: "admin",
status: "active",
});
const providerSession = randomUUID(),
providerBrowser = randomUUID();
let providerRun = randomUUID();
let acceptedTask = "", loseReply = false, rejectCode = 0, recoveryPages = 0;
let createGate: (() => Promise<void>) | undefined;
let totalCostUsd = "0.15";
let status = "running",
stopped = false,
browserReady = false,
invalidKey = false,
failCreate = false,
paginatedEvents = false;
const request = vi.fn(async (url: string, init: RequestInit) => {
expect(new Headers(init.headers).get("X-Browser-Use-API-Key")).toBe(
"bu_fixture_secret",
);
if (invalidKey)
return new Response("bu_fixture_secret private-error", {
status: 401,
});
const path = new URL(url).pathname.replace("/api/v4", "");
if (path === "/profiles")
return Response.json({ items: [], totalItems: 0 });
if (path === "/runs" && init.method === "GET") {
const cursor = Number(new URL(url).searchParams.get("cursor") ?? 0);
return Response.json(cursor < recoveryPages
? { runs: [], hasMore: true, nextCursor: String(cursor + 1) }
: { runs: acceptedTask ? [{ id: providerRun, sessionId: providerSession, task: acceptedTask, status }] : [], hasMore: false });
}
if (path === "/runs" && init.method === "POST" && rejectCode)
return new Response("Rejected", { status: rejectCode, headers: { "Retry-After": "1" } });
if (path === "/runs" && init.method === "POST" && failCreate)
throw new Error("lost create response");
if (path === "/runs" && init.method === "POST") {
await createGate?.();
providerRun = randomUUID();
status = "running";
acceptedTask = JSON.parse(String(init.body)).task;
if (loseReply) { browserReady = true; throw new Error("lost accepted create response"); }
return Response.json({
id: providerRun,
sessionId: providerSession,
status,
model: "fixture",
});
}
if (path.endsWith("/cancel")) {
status = "cancelled";
return Response.json({});
}
if (path.endsWith("/status")) return Response.json({ status });
if (path.endsWith("/events") && paginatedEvents) {
const next = Number(new URL(url).searchParams.get("after")) + 1;
return Response.json({
events: [{ id: next, type: "agent.step", data: {} }],
nextAfter: next,
hasMore: next < 9,
});
}
if (path.endsWith("/events"))
return Response.json({
events: browserReady
? [
{
id: 1,
type: "browser.ready",
data: {
live_view_url: "https://live.browser-use.com/canary",
},
},
]
: [],
nextAfter: browserReady ? 1 : 0,
hasMore: false,
});
if (path === `/runs/${providerRun}`)
return Response.json({
status,
result: "Finished https://live.browser-use.com/canary",
totalCostUsd,
model: "fixture",
});
if (path === "/browsers")
return Response.json({
items: browserReady
? [
{
id: providerBrowser,
status: stopped ? "stopped" : "active",
},
]
: [],
totalItems: browserReady ? 1 : 0,
});
if (path === `/browsers/${providerBrowser}`) {
if (init.method === "PATCH") stopped = true;
return Response.json({
id: providerBrowser,
agentSessionId: providerSession,
status: stopped ? "stopped" : "active",
liveUrl: "https://live.browser-use.com/canary",
cdpUrl: `https://${providerBrowser}.cdp.browser-use.com`,
timeoutAt: new Date(Date.now() + 3600000).toISOString(),
});
}
throw new Error(`Unexpected fixture path ${path}`);
});
const access = toolAccessService(db, {
remoteHttpRequest: request,
remoteHttpEndpointLookup: async () => [
{ address: "8.8.8.8", family: 4 },
],
});
const connection = await access.connectGalleryApp(
company.id,
{
galleryKey: "browser-use-cloud",
...(personal ? { grantKind: "user" as const } : {}),
connectionMethodKey: "cloud-v4",
credentialValues: { "credentials.apiKey": "bu_fixture_secret" },
},
actor,
);
const [grant] = await db
.select()
.from(connectionGrants)
.where(eq(connectionGrants.connectionId, connection.connectionId));
const [agent] = await db
.insert(agents)
.values({
companyId: company.id,
name: "Browser agent",
role: "engineer",
adapterType: "process",
adapterConfig: {},
})
.returning();
await access.finishGalleryAppConnection(
company.id,
connection.connectionId,
{
enabledCatalogEntryIds: connection.catalog.map((t) => t.id),
askFirstCatalogEntryIds: [],
access: { agentIds: [agent.id] },
},
actor,
);
const [issue] = await db
.insert(issues)
.values({
companyId: company.id,
title: "Browser task",
assigneeAgentId: agent.id,
})
.returning();
const [run] = await db
.insert(heartbeatRuns)
.values({
companyId: company.id,
agentId: agent.id,
invocationSource: "on_demand",
status: "running",
responsibleUserId: actor.actorId,
contextSnapshot: { issueId: issue.id },
})
.returning();
const [profile] = await db
.insert(toolProfiles)
.values({
companyId: company.id,
name: "Browser tools",
profileKey: randomUUID(),
defaultAction: "allow",
})
.returning();
await db.insert(toolProfileBindings).values({
companyId: company.id,
profileId: profile.id,
targetType: "agent",
targetId: agent.id,
});
const gateway = createToolGatewayService(db, {
remoteHttpRequest: request,
toolActionSigningSecret: "browser-fixture-signing-secret",
});
registerAssignedMcpGateway(db, gateway);
const gatewaySession = await gateway.createSession({
companyId: company.id,
agentId: agent.id,
runId: run.id,
});
const service = browserUseService(db, request);
const binding = {
companyId: company.id,
agentId: agent.id,
issueId: issue.id,
runId: run.id,
};
const tick = async () => {
await db
.update(browserUseSessions)
.set({ nextPollAt: new Date(0) })
.where(eq(browserUseSessions.companyId, company.id));
await service.sweep(connection.connectionId);
};
return {
company,
connection,
access,
grant,
agent,
issue,
run,
service,
binding,
gateway,
gatewaySession,
request,
tick,
ready: () => {
browserReady = true;
},
complete: () => {
status = "completed";
},
invalidate: () => {
invalidKey = true;
},
failCreate: () => {
failCreate = true;
},
loseReply: () => { loseReply = true; },
reject: (code: number) => { rejectCode = code; },
recoveryPages: (count: number) => { recoveryPages = count; },
delayCreate: (gate: () => Promise<void>) => { createGate = gate; },
usage: (value: string) => { totalCostUsd = value; },
paginate: () => {
paginatedEvents = true;
},
};
}
it("wakes the app worker from request-service commits and restores durable poll and lease deadlines", async () => {
const f = await fixture();
const background = browserUseService(db, f.request);
const sweep = vi.spyOn(background, "sweep");
const onError = vi.fn();
const coordinator = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, onError });
try {
await registerBrowserUseCleanup(coordinator, background, () => true).ready;
expect(coordinator.nextWakeAt()).toBeNull();
expect(await background.nextSweepAt()).toBeNull();
const started = await f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Read example.com" }) as { sessionId: string };
await vi.waitFor(() => expect(sweep.mock.calls.length).toBeGreaterThan(1));
await coordinator.stop();
expect(onError).not.toHaveBeenCalled();
const poll = new Date(Date.now() + 60_000);
const lease = new Date(Date.now() + 180_000);
await db.update(browserUseSessions).set({ nextPollAt: poll, leaseUntil: lease })
.where(eq(browserUseSessions.id, started.sessionId));
expect(await background.nextSweepAt()).toBe(lease.getTime());
const restarted = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, onError });
try {
await registerBrowserUseCleanup(restarted, browserUseService(db, f.request), () => true).ready;
expect(restarted.nextWakeAt()).toBe(lease.getTime());
} finally { await restarted.stop(); }
await db.update(browserUseSessions).set({ leaseUntil: new Date(0) })
.where(eq(browserUseSessions.id, started.sessionId));
expect(await background.nextSweepAt()).toBe(poll.getTime());
await db.update(browserUseSessions).set({ leaseUntil: null })
.where(eq(browserUseSessions.id, started.sessionId));
expect(await background.nextSweepAt()).toBe(poll.getTime());
const [session] = await db.select().from(browserUseSessions).where(eq(browserUseSessions.id, started.sessionId));
await f.service.control(session, "end");
expect(await background.nextSweepAt()).toBeLessThan(poll.getTime());
await db.update(browserUseSessions).set({ status: "closed" }).where(eq(browserUseSessions.id, started.sessionId));
expect(await background.nextSweepAt()).toBeNull();
} finally {
await coordinator.stop();
await db.update(browserUseSessions).set({ status: "closed" }).where(eq(browserUseSessions.companyId, f.company.id));
}
});
it("keeps Cloud instructions out of universal skills and unassigned runtime overlays", async () => {
const skills = await listPaperclipSkillEntries(fileURLToPath(new URL("../", import.meta.url)), [fileURLToPath(new URL("../../../skills", import.meta.url))]);
expect(skills.some(skill => skill.runtimeName === "paperclip")).toBe(true);
expect(skills.some(skill => ["browser-use", "browser-use-cloud"].includes(skill.runtimeName))).toBe(false);
const base = { paperclipSkillSync: { desiredSkills: ["paperclipai/paperclip/browser-use", "paperclipai/paperclip/browser-use-cloud"] } };
const unassigned = await applyConnectorSkills(base, [
{ key: "paperclipai/paperclip/browser-use", runtimeName: "browser-use", source: "/retired-browser-skill" },
{ key: "paperclipai/paperclip/browser-use-cloud", runtimeName: "browser-use-cloud", source: "/unassigned-cloud-skill" },
{ key: "custom/browser-use", runtimeName: "browser-use", source: "/another-browser-skill" },
], []);
expect(unassigned.paperclipRuntimeSkills).toEqual([{ key: "custom/browser-use", runtimeName: "browser-use", source: "/another-browser-skill" }]);
expect(unassigned.paperclipConnectorSkillDigest).toBeNull();
expect(base.paperclipSkillSync.desiredSkills).toHaveLength(2);
});
it("delivers the Cloud skill only with authorized connection tools in a task run", async () => {
const f = await fixture();
const [unassignedAgent] = await db.insert(agents).values({
companyId: f.company.id,
name: "Agent without the Cloud connection",
role: "engineer",
adapterType: "process",
adapterConfig: {},
}).returning();
expect(await f.gateway.browserUseResources({ companyId: f.company.id, agentId: f.agent.id })).toEqual([]);
expect(await resolveConnectorAssignments(db, { ...f.binding, agentId: unassignedAgent.id })).toEqual([]);
expect(await resolveConnectorAssignments(db, { ...f.binding, companyId: randomUUID() })).toEqual([]);
const assignments = await resolveConnectorAssignments(db, f.binding);
expect(assignments).toMatchObject([{ key: "browser-use-cloud", skillKey: "paperclipai/paperclip/browser-use-cloud", resources: [{ connectionId: f.connection.connectionId }] }]);
const config = await applyConnectorSkills({}, [], assignments);
const skill = config.paperclipRuntimeSkills[0];
expect(skill.runtimeName).toBe("browser-use-cloud");
const markdown = await readFile(path.join(skill.source, "SKILL.md"), "utf8");
expect(markdown).toContain("name: browser-use-cloud");
expect(markdown).toContain("browser_start");
expect(markdown).toContain(f.connection.connectionId);
expect(markdown).not.toContain("bu_fixture_secret");
for (const adapterType of ["paperclip_runner", "codex_local", "claude_local", "kimi_local"]) {
const delivery = await prepareConnectorSkillDelivery({ ...config, engine: "cli" }, adapterType);
expect(delivery.config.paperclipRuntimeSkills).toEqual([skill]);
}
const sharedHome = await prepareConnectorSkillDelivery(config, "cursor_local");
expect(sharedHome.config.paperclipRuntimeSkills).toEqual([]);
expect(sharedHome.instructions).toContain("name: browser-use-cloud");
expect(sharedHome.config.paperclipConnectorSkillDigest).toBe(config.paperclipConnectorSkillDigest);
await db.update(connectionGrants).set({ status: "revoked" }).where(eq(connectionGrants.id, f.grant.id));
expect(await resolveConnectorAssignments(db, f.binding)).toEqual([]);
const revoked = await applyConnectorSkills(config, config.paperclipRuntimeSkills, []);
expect(revoked.paperclipRuntimeSkills).toEqual([]);
expect(revoked.paperclipConnectorSkillDigest).toBeNull();
expect((await prepareConnectorSkillDelivery(revoked, "cursor_local")).instructions).toBe("");
});
it.each([false, true])("skips tool discovery without an active company Browser Use connection (foreign: %s)", async foreign => {
const f = await fixture();
await db.update(toolConnections).set({ enabled: false }).where(eq(toolConnections.id, f.connection.connectionId));
if (foreign) await fixture();
f.request.mockClear();
const discovery = vi.spyOn(db, "transaction");
try {
expect(await f.gateway.browserUseResources(f.binding)).toEqual([]);
expect(discovery).not.toHaveBeenCalled();
expect(f.request).not.toHaveBeenCalled();
} finally { discovery.mockRestore(); }
});
it("resolves only the Browser Use catalog even while full discovery is saturated", async () => {
const f = await fixture();
const [application] = await db.insert(toolApplications).values({
companyId: f.company.id, name: "Unrelated MCP", type: "mcp_http", status: "active",
}).returning();
const [connection] = await db.insert(toolConnections).values({
companyId: f.company.id, applicationId: application.id, name: "Unrelated MCP",
uid: randomUUID(), transport: "mcp_remote", status: "active", enabled: true, healthStatus: "ok",
}).returning();
await db.insert(toolCatalogEntries).values(Array.from({ length: 500 }, (_, i) => ({
companyId: f.company.id, applicationId: application.id, connectionId: connection.id,
entryKind: "tool" as const, name: `unrelated_${i}`, toolName: `unrelated_${i}`,
inputSchema: { type: "object" }, riskLevel: "read" as const, status: "active" as const,
versionHash: "fixture", schemaHash: "fixture",
})));
const { recordingDb } = await import("./helpers/tool-gateway-listing-fixture.js");
const { toolDiscoveryScheduler } = await import("../services/tool-discovery-scheduler.js");
const releases: Array<() => void> = [];
const blockers = [0, 1].map(() => toolDiscoveryScheduler.run(() => new Promise<void>(resolve => releases.push(resolve))));
await vi.waitFor(() => expect(releases).toHaveLength(2));
const recorded = recordingDb(db);
const gateway = createToolGatewayService(recorded.db);
const pending = gateway.browserUseResources(f.binding);
let timeout: ReturnType<typeof setTimeout> | undefined;
try {
const resources = await Promise.race([pending, new Promise<never>((_, reject) => {
timeout = setTimeout(() => reject(new Error("Assignment waited for unrelated discovery")), 5_000);
})]);
expect(resources).toEqual([{ id: f.connection.connectionId, connectionId: f.connection.connectionId, label: "Browser Use Cloud" }]);
const catalogReads = recorded.statements.flatMap((query, index) =>
query.includes('from "tool_catalog_entries"') && query.includes('inner join "tool_connections"') ? [recorded.statementParams[index]] : []);
expect(catalogReads).toHaveLength(1);
expect(catalogReads[0]).toContain(f.connection.connectionId);
expect(catalogReads[0]).not.toContain(connection.id);
expect(recorded.statements.some(query => query.includes('from "chat_endpoints"'))).toBe(false);
} finally {
clearTimeout(timeout);
releases.forEach(release => release());
await Promise.all(blockers);
await pending;
}
});
it.each(["policy", "catalog", "grant"])("rechecks Browser Use %s revocation without a cached assignment", async kind => {
const f = await fixture();
expect(await f.gateway.browserUseResources(f.binding)).toHaveLength(1);
if (kind === "policy") await db.insert(toolPolicies).values({
companyId: f.company.id, name: "Revoke Browser Use", policyType: "block",
selectors: { connectionId: f.connection.connectionId }, priority: 100,
});
if (kind === "catalog") await db.update(toolCatalogEntries).set({ quarantinedAt: new Date() }).where(eq(toolCatalogEntries.connectionId, f.connection.connectionId));
if (kind === "grant") await db.update(connectionGrants).set({ status: "revoked" }).where(eq(connectionGrants.id, f.grant.id));
expect(await f.gateway.browserUseResources(f.binding)).toEqual([]);
});
it("withholds Cloud instructions when connection tools are disabled", async () => {
const f = await fixture();
await db.update(toolConnections).set({ enabled: false }).where(eq(toolConnections.id, f.connection.connectionId));
expect(await resolveConnectorAssignments(db, f.binding)).toEqual([]);
});
it("reapplies the renumbered migration without losing existing browser work", async () => {
const f = await fixture();
await f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", {
task: "Read example.com",
maxCostUsd: 0.5,
});
f.ready(); f.complete(); await f.tick();
const previous = await db.select().from(browserUseRuns)
.where(eq(browserUseRuns.companyId, f.company.id));
expect(previous).toHaveLength(1);
const [original] = await db.select().from(toolConnections).where(eq(toolConnections.id, f.connection.connectionId));
await db.update(toolConnections).set({
config: { ...original.config, sourceTemplateKey: "browser-use" },
transportConfig: { ...original.transportConfig, sourceTemplateKey: "browser-use" },
}).where(eq(toolConnections.id, original.id));
await db.update(toolApplications).set({
applicationKey: `app-gallery:browser-use:${original.id}`,
metadata: { sourceTemplateKey: "browser-use", galleryKey: "browser-use" },
}).where(eq(toolApplications.id, original.applicationId));
const legacyCost = { provider: "browser-use", biller: "browser-use", billingCode: `browser-use:${previous[0].id}` };
await db.update(costEvents).set(legacyCost).where(eq(costEvents.companyId, f.company.id));
await db.update(financeEvents).set(legacyCost).where(eq(financeEvents.companyId, f.company.id));
const migration = await readFile(new URL(
"../../../packages/db/src/migrations/0290_browser_use_cloud.sql", import.meta.url,
), "utf8");
for (let attempt = 0; attempt < 2; attempt++) {
for (const statement of migration.split("--> statement-breakpoint")) {
if (statement.trim()) await db.execute(sql.raw(statement));
}
}
expect(await db.select().from(browserUseRuns)
.where(eq(browserUseRuns.companyId, f.company.id))).toEqual(previous);
const [migrated] = await db.select().from(toolConnections).where(eq(toolConnections.id, original.id));
expect(migrated).toMatchObject({ config: { sourceTemplateKey: "browser-use-cloud" }, transportConfig: { sourceTemplateKey: "browser-use-cloud" }, credentialSecretRefs: original.credentialSecretRefs });
expect(await db.select().from(toolApplications).where(eq(toolApplications.id, original.applicationId))).toMatchObject([{ applicationKey: `app-gallery:browser-use-cloud:${original.id}`, metadata: { sourceTemplateKey: "browser-use-cloud", galleryKey: "browser-use-cloud" } }]);
expect(await resolveConnectorAssignments(db, f.binding)).toMatchObject([{ key: "browser-use-cloud" }]);
await f.tick();
expect(await db.select().from(costEvents).where(eq(costEvents.companyId, f.company.id))).toMatchObject([{ provider: "browser-use-cloud", biller: "browser-use-cloud", billingCode: `browser-use-cloud:${previous[0].id}`, costCents: 15 }]);
expect(await db.select().from(financeEvents).where(eq(financeEvents.companyId, f.company.id))).toMatchObject([{ provider: "browser-use-cloud", amountCents: 15 }]);
});
it("delivers Browser Use through the pinned native and CLI runtime gateway", async () => {
const f = await fixture();
await db.insert(toolConnectionInstalls).values({
companyId: f.company.id,
connectionId: f.connection.connectionId,
targetType: "agent",
targetId: f.agent.id,
});
const snapshot = await resolveNativeRuntimeMcpSnapshot({
db,
agent: f.agent,
runId: f.run.id,
});
expect(snapshot.bindingId).not.toBeNull();
const servers = await buildPaperclipRuntimeMcpServers({
db,
agent: f.agent,
runId: f.run.id,
expectedAssignmentDigest: snapshot.digest,
});
expect(servers).toHaveLength(1);
const gatewayPublicId = new URL(servers[0]!.url).pathname
.split("/")
.at(-1)!;
const tools = await f.gateway.listToolsForNamedGateway({
gatewayPublicId,
bearerToken: servers[0]!.token!,
});
expect(tools).toHaveLength(7);
expect(tools.some((tool) => tool.name.endsWith(":browser-start"))).toBe(
true,
);
});
it("verifies credentials without paid work, exposes REST tools and rejects invalid keys", async () => {
const f = await fixture();
expect(f.connection.catalog).toHaveLength(7);
expect(
f.request.mock.calls.every(([url]) => url.includes("/profiles")),
).toBe(true);
expect(JSON.stringify(f.connection)).not.toContain("bu_fixture_secret");
const tools = await f.gateway.listToolsForSession(f.gatewaySession.token);
expect(
tools.find((t) => t.upstreamToolName === "browser_start"),
).toMatchObject({ providerType: "provider_rest", risk: "destructive" });
const start = tools.find((t) => t.upstreamToolName === "browser_start")!;
const idempotencyKey = randomUUID();
const result = await f.gateway.executeTool({
sessionToken: f.gatewaySession.token,
tool: start.name,
parameters: { task: "Read example.com" },
idempotencyKey,
});
expect(result.status).toBe("completed");
expect(JSON.stringify(result)).not.toContain("bu_fixture_secret");
await f.gateway.executeTool({
sessionToken: f.gatewaySession.token,
tool: start.name,
parameters: { task: "Read example.com" },
idempotencyKey,
});
expect(
f.request.mock.calls.filter(
([url, init]) => url.endsWith("/runs") && init.method === "POST",
),
).toHaveLength(1);
f.invalidate();
await expect(
f.access.refreshCatalog(f.connection.connectionId, actor),
).rejects.toThrow();
});
it("observes delayed browser.ready, hides viewer credentials, accounts once and cleans up", async () => {
const f = await fixture();
const invocation = randomUUID();
const started = (await f.service.execute(
f.binding,
f.grant,
invocation,
"browser_start",
{ task: "Read example.com", maxCostUsd: 0.5 },
)) as { sessionId: string };
expect(started.sessionId).toBeTruthy();
const createCall = f.request.mock.calls.find(
([url, init]) => url.endsWith("/runs") && init.method === "POST",
);
expect(
JSON.parse(String(createCall?.[1].body)).browserSettings.allowResizing,
).toBe(true);
await f.tick();
expect(
await db
.select()
.from(browserUseBrowsers)
.where(eq(browserUseBrowsers.companyId, f.company.id)),
).toHaveLength(0);
f.ready();
await f.tick();
const list = await f.service.list(
f.company.id,
f.issue.id,
actor.actorId,
);
expect(list).toHaveLength(1);
expect(JSON.stringify(list)).not.toContain("canary");
expect(
await f.service.viewer(
f.company.id,
f.issue.id,
list[0].id,
actor.actorId,
),
).toEqual({
url: "https://live.browser-use.com/canary",
viewport: "fit",
viewportState: { preset: "fit" },
});
await expect(
f.service.viewer(f.company.id, randomUUID(), list[0].id, actor.actorId),
).rejects.toThrow("not found");
f.complete();
await f.tick();
await f.tick();
const [s] = await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, started.sessionId));
expect(s.status).toBe("idle");
expect(s.idleDeadline).toBeTruthy();
expect(
await db
.select()
.from(costEvents)
.where(eq(costEvents.companyId, f.company.id)),
).toHaveLength(1);
const ledger = await db
.select()
.from(financeEvents)
.where(eq(financeEvents.companyId, f.company.id));
const [cost] = await db
.select()
.from(costEvents)
.where(eq(costEvents.companyId, f.company.id));
expect(ledger).toHaveLength(1);
expect(ledger[0]).toMatchObject({
issueId: f.issue.id,
agentId: f.agent.id,
costEventId: cost.id,
amountCents: 15,
biller: "browser-use-cloud",
direction: "debit",
currency: "USD",
});
expect(
JSON.stringify(
await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_status",
{ sessionId: s.id },
),
),
).not.toContain("canary");
await f.service.control(s, "end");
await f.tick();
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, s.id))
)[0].status,
).toBe("closed");
expect(
(
await db
.select()
.from(browserUseBrowsers)
.where(eq(browserUseBrowsers.sessionId, s.id))
)[0].status,
).toBe("stopped");
});
it("keeps a viewed idle browser alive but still honors expiry, stop and authorization", async () => {
const f = await fixture();
const started = (await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_start",
{ task: "Read" },
)) as { sessionId: string };
f.ready();
f.complete();
await f.tick();
const [browser] = await f.service.list(
f.company.id,
f.issue.id,
actor.actorId,
);
const read = async () =>
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, started.sessionId))
)[0];
const original = new Date(Date.now() + 1_000);
await db
.update(browserUseSessions)
.set({ idleDeadline: original })
.where(eq(browserUseSessions.id, started.sessionId));
await expect(
f.service.presence(
f.company.id,
randomUUID(),
browser.id,
actor.actorId,
),
).rejects.toThrow();
expect((await read()).idleDeadline?.getTime()).toBe(original.getTime());
expect(
await f.service.presence(
f.company.id,
f.issue.id,
browser.id,
actor.actorId,
),
).toEqual({ accepted: true });
expect((await read()).idleDeadline!.getTime()).toBeGreaterThan(
Date.now() + 9 * 60_000,
);
await f.tick();
expect((await read()).status).toBe("idle");
// Provider lifetime wins over viewer presence.
const expiry = new Date(Date.now() + 3 * 60_000);
await db
.update(browserUseBrowsers)
.set({ expiresAt: expiry })
.where(eq(browserUseBrowsers.id, browser.id));
await db
.update(browserUseSessions)
.set({ idleDeadline: original })
.where(eq(browserUseSessions.id, started.sessionId));
await f.service.presence(
f.company.id,
f.issue.id,
browser.id,
actor.actorId,
);
expect((await read()).idleDeadline?.getTime()).toBe(expiry.getTime());
await f.service.control(await read(), "end");
await expect(
f.service.presence(f.company.id, f.issue.id, browser.id, actor.actorId),
).rejects.toThrow();
await f.tick();
expect((await read()).status).toBe("closed");
});
it("executes an approved start once after the original turn ends and attaches the resumed run", async () => {
const f = await fixture();
await db.insert(toolPolicies).values({
companyId: f.company.id,
name: "Review browser writes",
policyType: "require_approval",
selectors: { connectionId: f.connection.connectionId },
priority: 10,
});
const start = (
await f.gateway.listToolsForSession(f.gatewaySession.token)
).find((t) => t.upstreamToolName === "browser_start")!;
await expect(
f.gateway.executeTool({
sessionToken: f.gatewaySession.token,
tool: start.name,
parameters: { task: "Read example.com" },
idempotencyKey: randomUUID(),
}),
).rejects.toMatchObject({ reasonCode: "approval_required" });
const [pending] = await db
.select()
.from(toolActionRequests)
.where(eq(toolActionRequests.companyId, f.company.id));
await db
.update(heartbeatRuns)
.set({ status: "succeeded" })
.where(eq(heartbeatRuns.id, f.run.id));
if (pending.approvalId)
await db
.update(approvals)
.set({
status: "approved",
decidedByUserId: actor.actorId,
decidedAt: new Date(),
})
.where(eq(approvals.id, pending.approvalId));
await f.gateway.approveActionRequest({
companyId: f.company.id,
actionRequestId: pending.id,
actor: { userId: actor.actorId },
});
const [record] = await db
.select()
.from(browserUseRuns)
.where(eq(browserUseRuns.companyId, f.company.id));
expect(record?.providerRunId).toBeTruthy();
expect(record.detachedUntil).toBeTruthy();
f.ready();
await f.tick();
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, record.sessionId))
)[0].status,
).toBe("running");
const [resumed] = await db
.insert(heartbeatRuns)
.values({
companyId: f.company.id,
agentId: f.agent.id,
invocationSource: "on_demand",
status: "running",
responsibleUserId: actor.actorId,
contextSnapshot: { issueId: f.issue.id },
})
.returning();
await f.service.execute(
{ ...f.binding, runId: resumed.id },
f.grant,
randomUUID(),
"browser_status",
{ sessionId: record.sessionId },
);
const [attached] = await db
.select()
.from(browserUseRuns)
.where(eq(browserUseRuns.id, record.id));
expect(attached.detachedUntil).toBeNull();
expect(attached.heartbeatRunId).toBe(resumed.id);
await f.gateway.approveActionRequest({
companyId: f.company.id,
actionRequestId: pending.id,
actor: { userId: actor.actorId },
});
expect(
f.request.mock.calls.filter(
([url, init]) => url.endsWith("/runs") && init.method === "POST",
),
).toHaveLength(1);
});
it("enforces task, agent, grant and profile ownership and never replays a create", async () => {
const f = await fixture();
const invocation = randomUUID();
await expect(
f.service.execute(f.binding, f.grant, invocation, "browser_start", {
task: "Read",
profileId: randomUUID(),
}),
).rejects.toThrow("profile");
const start = (await f.service.execute(
f.binding,
f.grant,
invocation,
"browser_start",
{ task: "Read" },
)) as { sessionId: string };
await f.service.execute(f.binding, f.grant, invocation, "browser_start", {
task: "Read",
});
expect(
f.request.mock.calls.filter(
([url, init]) => url.endsWith("/runs") && init.method === "POST",
),
).toHaveLength(1);
await expect(
f.service.execute(
{ ...f.binding, issueId: randomUUID() },
f.grant,
randomUUID(),
"browser_status",
{ sessionId: start.sessionId },
),
).rejects.toThrow();
await expect(
f.service.execute(
f.binding,
{ ...f.grant, id: randomUUID() },
randomUUID(),
"browser_status",
{ sessionId: start.sessionId },
),
).rejects.toThrow();
await expect(
f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_continue",
{ sessionId: start.sessionId, task: "Again" },
),
).rejects.toThrow("busy");
f.ready();
await f.tick();
await db
.update(connectionGrants)
.set({ status: "revoked" })
.where(eq(connectionGrants.id, f.grant.id));
expect(
await f.service.list(f.company.id, f.issue.id, actor.actorId),
).toEqual([]);
await f.tick();
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, start.sessionId))
)[0].status,
).toBe("closed");
});
it("does not replay an uncertain paid create", async () => {
const f = await fixture();
f.failCreate();
const invocationId = randomUUID();
await expect(
f.service.execute(f.binding, f.grant, invocationId, "browser_start", {
task: "Read",
}),
).rejects.toThrow("could not be confirmed");
const result = await f.service.execute(
f.binding,
f.grant,
invocationId,
"browser_start",
{ task: "Read" },
);
expect(result).toMatchObject({ status: "stopping", runStatus: "unknown" });
expect(
f.request.mock.calls.filter(
([url, init]) => url.endsWith("/runs") && init.method === "POST",
),
).toHaveLength(1);
});
it.each([409, 429])("keeps a browser visible and closable after a rejected continuation (%s)", async (code) => {
const f = await fixture();
const start = await f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Read" }) as { sessionId: string };
f.ready(); f.complete(); await f.tick();
f.reject(code);
await expect(f.service.execute(f.binding, f.grant, randomUUID(), "browser_continue", { task: "Read again", sessionId: start.sessionId })).rejects.toThrow();
expect(await f.service.list(f.company.id, f.issue.id, actor.actorId)).toMatchObject([{ status: "idle" }]);
const [session] = await db.select().from(browserUseSessions).where(eq(browserUseSessions.id, start.sessionId));
await f.service.control(session, "end", actor.actorId);
// The 429 response delays the whole credential's provider requests.
if (code === 429) await new Promise(resolve => setTimeout(resolve, 1050));
await f.tick();
expect(await f.service.list(f.company.id, f.issue.id, actor.actorId)).toMatchObject([{ status: "closed" }]);
});
it("rolls back an interrupted rejection and closes the existing browser after restart", async () => {
const f = await fixture();
const start = await f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Read" }) as { sessionId: string };
f.ready(); f.complete(); await f.tick();
f.reject(409);
const invocationId = randomUUID();
const transaction = db.transaction.bind(db);
const interrupted = vi.spyOn(db, "transaction").mockImplementation((callback, config) =>
transaction(async tx => {
const result = await callback(tx);
const [run] = await tx.select().from(browserUseRuns).where(eq(browserUseRuns.invocationId, invocationId));
if (run?.status === "failed") throw new Error("process interrupted before commit");
return result;
}, config));
try {
await expect(f.service.execute(f.binding, f.grant, invocationId, "browser_continue", { task: "Read again", sessionId: start.sessionId })).rejects.toThrow("process interrupted");
} finally {
interrupted.mockRestore();
}
const [pending] = await db.select().from(browserUseRuns).where(eq(browserUseRuns.invocationId, invocationId));
expect(pending).toMatchObject({ status: "creating", eventsDrained: 0, providerRunId: null });
await db.update(browserUseRuns).set({ createdAt: new Date(Date.now() - 61_000) }).where(eq(browserUseRuns.id, pending.id));
await db.update(browserUseSessions).set({ nextPollAt: new Date(0) }).where(eq(browserUseSessions.id, start.sessionId));
await browserUseService(db, f.request).sweep();
expect(await db.select().from(browserUseBrowsers).where(eq(browserUseBrowsers.sessionId, start.sessionId))).toMatchObject([{ status: "stopped" }]);
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(2);
});
it("recovers an accepted start after a lost response and restart, then stops and accounts without replay", async () => {
const f = await fixture();
f.loseReply(); f.recoveryPages(6);
await expect(f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Read" })).rejects.toThrow("could not be confirmed");
await f.tick();
const [pending] = await db.select().from(browserUseRuns).where(eq(browserUseRuns.companyId, f.company.id));
expect(pending.recoveryCursor).toBe("5");
await db.update(browserUseSessions).set({ nextPollAt: new Date(0) }).where(eq(browserUseSessions.companyId, f.company.id));
await browserUseService(db, f.request).sweep();
expect(await f.service.list(f.company.id, f.issue.id, actor.actorId)).toMatchObject([{ status: "closed" }]);
expect(await db.select().from(financeEvents).where(eq(financeEvents.companyId, f.company.id))).toMatchObject([{ amountCents: 15 }]);
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(1);
});
it("recovers a continuation's lost reply within its existing provider session", async () => {
const f = await fixture();
const start = await f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Read" }) as { sessionId: string };
f.ready(); f.complete(); await f.tick();
f.loseReply();
await expect(f.service.execute(f.binding, f.grant, randomUUID(), "browser_continue", { task: "Read again", sessionId: start.sessionId })).rejects.toThrow("could not be confirmed");
await f.tick();
expect(await f.service.list(f.company.id, f.issue.id, actor.actorId)).toMatchObject([{ status: "closed" }]);
expect(f.request.mock.calls.some(([url]) => new URL(url).searchParams.has("sessionId"))).toBe(true);
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(2);
});
it("recovers a process crash before provider identifiers were saved", async () => {
const f = await fixture();
const start = await f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Read" }) as { sessionId: string };
f.ready();
await db.update(browserUseRuns).set({ providerRunId: null, status: "creating", createdAt: new Date(Date.now() - 61_000) }).where(eq(browserUseRuns.sessionId, start.sessionId));
await db.update(browserUseSessions).set({ providerSessionId: null, status: "starting" }).where(eq(browserUseSessions.id, start.sessionId));
await f.tick();
expect(await f.service.list(f.company.id, f.issue.id, actor.actorId)).toMatchObject([{ status: "closed" }]);
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(1);
});
it("drains bounded event pages after restart before closing and accounting", async () => {
const f = await fixture();
const start = (await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_start",
{ task: "Read" },
)) as { sessionId: string };
f.ready();
await f.tick();
f.paginate();
f.complete();
const [session] = await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, start.sessionId));
await f.service.control(session, "end");
await f.tick();
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, start.sessionId))
)[0].status,
).toBe("stopping");
expect(
await db
.select()
.from(costEvents)
.where(eq(costEvents.companyId, f.company.id)),
).toHaveLength(0);
await db
.update(browserUseSessions)
.set({ nextPollAt: new Date(0) })
.where(eq(browserUseSessions.id, start.sessionId));
await browserUseService(db, f.request).sweep();
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, start.sessionId))
)[0].status,
).toBe("closed");
expect(
(
await db
.select()
.from(browserUseRuns)
.where(eq(browserUseRuns.sessionId, start.sessionId))
)[0],
).toMatchObject({ eventCursor: 9, eventsDrained: 1, accountedCents: 15 });
});
it.each(["task", "saved chat"])("protects %s browser routes from agent, other-company and read-only access", async (kind) => {
const f = await fixture();
if (kind === "saved chat") {
await db.update(issues).set({
conversationAgentId: f.agent.id,
conversationUserId: actor.actorId,
conversationState: "active",
}).where(eq(issues.id, f.issue.id));
}
await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_start",
{ task: "Read" },
);
f.ready();
await f.tick();
const [browser] = await f.service.list(
f.company.id,
f.issue.id,
actor.actorId,
);
let mode = "board";
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
req.actor =
mode === "agent"
? {
type: "agent",
companyId: f.company.id,
agentId: f.agent.id,
source: "agent_key",
}
: {
type: "board",
userId: actor.actorId,
source: "session",
companyIds: mode === "other-company" ? [] : [f.company.id],
};
next();
});
app.use(browserUseRoutes(db, f.service));
app.use(errorHandler);
const listPath = `/issues/${f.issue.id}/browsers`;
const listed = await http(app).get(listPath);
expect(listed.status).toBe(200);
expect(listed.body).toMatchObject([{ id: browser.id, issueId: f.issue.id }]);
const missing = await http(app).get(`/issues/${randomUUID()}/browsers`);
expect(missing.status).toBe(404);
expect(missing.body).toEqual({ error: "Task not found" });
const path = `/issues/${f.issue.id}/browsers/${browser.id}/viewer`;
const ok = await http(app).get(path);
expect(ok.status).toBe(200);
expect(ok.headers["cache-control"]).toBe("no-store");
expect(ok.headers["referrer-policy"]).toBe("no-referrer");
const controlPath = path.replace("/viewer", "/control");
const presencePath = path.replace("/viewer", "/presence");
expect((await http(app).post(presencePath)).status).toBe(200);
const resizePath = path.replace("/viewer", "/viewport");
const resizeSpy = vi
.spyOn(browserUseViewports, "resize")
.mockResolvedValue({ preset: "phone", applied: true });
expect(
(await http(app).post(resizePath).send({ preset: "phone" })).body,
).toEqual({ preset: "phone", applied: true });
expect(resizeSpy).toHaveBeenCalledTimes(1);
const viewerId = randomUUID();
resizeSpy.mockResolvedValueOnce({
preset: "fit",
width: 531,
height: 712,
applied: true,
});
expect(
(
await http(app)
.post(resizePath)
.send({ preset: "fit", width: 531, height: 712, viewerId })
).body,
).toMatchObject({ preset: "fit", width: 531 });
expect(resizeSpy).toHaveBeenLastCalledWith(
expect.any(String),
expect.any(String),
{ preset: "fit", width: 531, height: 712, viewerId },
expect.any(String),
`${actor.actorId}:${viewerId}`,
);
expect(
(await http(app).post(`${resizePath}/release`).send({ viewerId }))
.status,
).toBe(200);
expect(
(await http(app).post(resizePath).send({ preset: "custom", width: 1 }))
.status,
).toBe(400);
expect((await http(app).post(controlPath).send({ action: "cancel" })).status).toBe(200);
mode = "agent";
expect((await http(app).get(listPath)).status).toBe(403);
expect((await http(app).post(controlPath).send({ action: "keep_open" })).status).toBe(403);
expect((await http(app).post(presencePath)).status).toBe(403);
expect(
(await http(app).post(`${resizePath}/release`).send({ viewerId }))
.status,
).toBe(403);
expect(
(await http(app).post(resizePath).send({ preset: "phone" })).status,
).toBe(403);
expect((await http(app).get(path)).status).toBe(403);
mode = "other-company";
const inaccessible = await http(app).get(listPath);
expect(inaccessible.status).toBe(missing.status);
expect(inaccessible.body).toEqual(missing.body);
expect((await http(app).post(controlPath).send({ action: "keep_open" })).status).toBe(404);
expect((await http(app).post(presencePath)).status).toBe(404);
expect(
(await http(app).post(`${resizePath}/release`).send({ viewerId }))
.status,
).toBe(404);
expect(
(await http(app).post(resizePath).send({ preset: "phone" })).status,
).toBe(404);
expect((await http(app).get(path)).status).toBe(404);
mode = "board";
await db
.update(companyMemberships)
.set({ membershipRole: "viewer" })
.where(eq(companyMemberships.companyId, f.company.id));
expect((await http(app).get(path)).status).toBe(403);
expect((await http(app).get(listPath)).body).toEqual([]);
expect((await http(app).post(controlPath).send({ action: "keep_open" })).status).toBe(403);
expect((await http(app).post(presencePath)).status).toBe(403);
expect(
(await http(app).post(resizePath).send({ preset: "phone" })).status,
).toBe(403);
expect(resizeSpy).toHaveBeenCalledTimes(2);
resizeSpy.mockRestore();
});
it("rechecks current gateway access and closes browsers when it is removed", async () => {
const f = await fixture();
const start = (
await f.gateway.listToolsForSession(f.gatewaySession.token)
).find((t) => t.upstreamToolName === "browser_start")!;
await f.gateway.executeTool({
sessionToken: f.gatewaySession.token,
tool: start.name,
parameters: { task: "Read" },
idempotencyKey: randomUUID(),
});
f.ready();
await f.tick();
const [run] = await db
.select()
.from(browserUseRuns)
.where(eq(browserUseRuns.companyId, f.company.id));
const scope = {
...f.binding,
runId: run.heartbeatRunId,
connectionId: f.grant.connectionId,
grantId: f.grant.id,
invocationId: run.invocationId,
};
expect(await f.gateway.browserUseSessionAuthorized(scope)).toBe(true);
await db.insert(toolPolicies).values({
companyId: f.company.id,
name: "Disable browser access",
policyType: "block",
selectors: { connectionId: f.connection.connectionId },
priority: 100,
});
expect(await f.gateway.browserUseSessionAuthorized(scope)).toBe(false);
await db
.update(browserUseSessions)
.set({ nextPollAt: new Date(0) })
.where(eq(browserUseSessions.companyId, f.company.id));
await browserUseService(db, f.request, {}, () =>
f.gateway.browserUseSessionAuthorized(scope),
).sweep();
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, run.sessionId))
)[0].status,
).toBe("closed");
});
it("resolves only the selected personal credential and denies another member's viewer", async () => {
const f = await fixture(true);
expect(f.grant.kind).toBe("user");
const tool = (
await f.gateway.listToolsForSession(f.gatewaySession.token)
).find((t) => t.upstreamToolName === "browser_start")!;
await f.gateway.executeTool({
sessionToken: f.gatewaySession.token,
tool: tool.name,
parameters: { task: "Read" },
idempotencyKey: randomUUID(),
});
f.ready();
await f.tick();
const [browser] = await f.service.list(
f.company.id,
f.issue.id,
actor.actorId,
);
expect(
await f.service.viewer(
f.company.id,
f.issue.id,
browser.id,
actor.actorId,
),
).toHaveProperty("url");
await db.insert(companyMemberships).values({
companyId: f.company.id,
principalType: "user",
principalId: "other-reviewer",
membershipRole: "admin",
status: "active",
});
await expect(
f.service.viewer(
f.company.id,
f.issue.id,
browser.id,
"other-reviewer",
),
).rejects.toThrow("another credential");
await f.access.archiveConnection(
f.connection.connectionId,
f.company.id,
actor,
);
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.companyId, f.company.id))
)[0].status,
).toBe("closed");
});
it("continues an idle conversation and bounds Keep open with idle cleanup", async () => {
const f = await fixture();
const start = (await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_start",
{ task: "Read" },
)) as { sessionId: string };
f.ready();
f.complete();
await f.tick();
const [idle] = await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, start.sessionId));
await f.service.control(idle, "keep_open");
await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_continue",
{ sessionId: start.sessionId, task: "Follow up" },
);
expect(
await db
.select()
.from(browserUseRuns)
.where(eq(browserUseRuns.sessionId, start.sessionId)),
).toHaveLength(2);
const calls = f.request.mock.calls.filter(
([url, init]) => url.endsWith("/runs") && init.method === "POST",
);
expect(JSON.parse(String(calls[1][1].body)).sessionId).toBe(
idle.providerSessionId,
);
f.complete();
await f.tick();
await db
.update(browserUseSessions)
.set({ idleDeadline: new Date(0) })
.where(eq(browserUseSessions.id, start.sessionId));
await f.tick();
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.id, start.sessionId))
)[0].status,
).toBe("closed");
});
it("serializes browser holds with parent reservations while provider work is delayed", async () => {
const f = await fixture();
const policy = await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "company", scopeId: f.company.id, amount: 100, hardStopEnabled: true }, actor.actorId);
await db.insert(budgetReservations).values({ companyId: f.company.id, runId: f.run.id, agentId: f.agent.id, amountCents: "40" });
let release!: () => void, started!: () => void;
const providerEntered = new Promise<void>(resolve => { started = resolve; });
const providerRelease = new Promise<void>(resolve => { release = resolve; });
f.delayCreate(async () => { started(); await providerRelease; });
const first = f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Read", maxCostUsd: 0.8 });
// Attach the rejection handler before waiting for the controlled POST.
await Promise.race([providerEntered, first.then(() => undefined)]);
try {
await expect(f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Concurrent" })).rejects.toThrow("reserved");
const [row] = await db.select().from(budgetPolicies).where(eq(budgetPolicies.id, policy.policyId));
expect(await computeObservedSpend(db, row)).toMatchObject({ ledgerCents: "0.0000000", effectiveHeldCents: "100.0000000" });
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(1);
const call = f.request.mock.calls.find(([url, init]) => url.endsWith("/runs") && init.method === "POST")!;
expect(JSON.parse(String(call[1].body)).maxCostUsd).toBe(0.6);
// Accounting lock is not held while the hosted browser is waiting.
expect(await db.select({ id: companies.id }).from(companies).where(eq(companies.id, f.company.id))).toHaveLength(1);
} finally { release(); await first; }
});
it.each(["reduction", "new_policy", "config", "charge", "cancel", "session_end", "policy_deny"])("rechecks %s at the actual provider handoff", async race => {
const f = await fixture();
if (race === "reduction" || race === "charge") await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "company", scopeId: f.company.id, amount: 100, hardStopEnabled: true }, actor.actorId);
const invocationId = randomUUID();
const transaction = db.transaction.bind(db);
let injected = false;
const barrier = vi.spyOn(db, "transaction").mockImplementation(async (callback, config) => {
const result = await transaction(callback, config);
const [run] = await db.select().from(browserUseRuns).where(eq(browserUseRuns.invocationId, invocationId));
if (!injected && run?.status === "creating" && !(run.result as { paperclipBudgetHold?: { dispatchCommittedAt?: string } })?.paperclipBudgetHold?.dispatchCommittedAt) {
injected = true;
if (race === "reduction" || race === "new_policy") await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "company", scopeId: f.company.id, amount: race === "new_policy" ? 100 : 10, hardStopEnabled: true }, actor.actorId);
else if (race === "config") await f.service.saveSettings(f.company.id, f.grant.id, actor.actorId, { allowedProfileIds: [], maxCostUsd: 0.1 });
else if (race === "charge") {
const { costService } = await import("../services/costs.js");
await costService(db).createEvent(f.company.id, { agentId: f.agent.id, provider: "fixture", model: "fixture", costCents: 20, occurredAt: new Date() });
} else if (race === "policy_deny") await db.insert(toolPolicies).values({ companyId: f.company.id,
name: "Deny before browser handoff", policyType: "block", selectors: { connectionId: f.connection.connectionId }, priority: 0 });
else if (race === "session_end") await db.update(browserUseSessions).set({ status: "stopping", stopRequested: "end" }).where(eq(browserUseSessions.id, run.sessionId));
else await db.update(heartbeatRuns).set({ resultJson: { startupCancellation: { requestedAt: new Date().toISOString() } } }).where(eq(heartbeatRuns.id, f.run.id));
}
return result;
});
try {
await expect(f.service.execute(f.binding, f.grant, invocationId, "browser_start", { task: "Must not start" })).rejects.toThrow();
} finally { barrier.mockRestore(); }
expect(injected).toBe(true);
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(0);
const [run] = await db.select().from(browserUseRuns).where(eq(browserUseRuns.invocationId, invocationId));
expect(run).toMatchObject({ status: "failed", eventsDrained: 1, result: { paperclipBudgetHold: { state: "released" } } });
});
it.each(["default_allow", "tool_name"])("retains valid %s profile authorization at browser admission", async selector => {
const f = await fixture();
await db.update(toolProfiles).set({ status: "disabled" }).where(eq(toolProfiles.companyId, f.company.id));
const [profile] = await db.insert(toolProfiles).values({ companyId: f.company.id, name: "Scoped browser access",
profileKey: randomUUID(), defaultAction: selector === "default_allow" ? "allow" : "deny" }).returning();
await db.insert(toolProfileBindings).values({ companyId: f.company.id, profileId: profile.id, targetType: "agent", targetId: f.agent.id });
if (selector === "tool_name") await db.insert(toolProfileEntries).values({ companyId: f.company.id, profileId: profile.id,
selectorType: "tool_name", toolName: "browser_start", effect: "include" });
await expect(f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Allowed by profile" })).resolves.toMatchObject({ status: "running" });
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(1);
});
it("checks the gateway invocation's namespaced tool identity for explicit denial", async () => {
const f = await fixture();
const invocationId = randomUUID(), toolName = "app.browser-fixture:browser-start";
const [entry] = await db.select().from(toolCatalogEntries).where(and(eq(toolCatalogEntries.connectionId, f.connection.connectionId), eq(toolCatalogEntries.toolName, "browser_start")));
await db.insert(toolInvocations).values({ id: invocationId, companyId: f.company.id, agentId: f.agent.id,
runId: f.run.id, issueId: f.issue.id, actorType: "agent", actorId: f.agent.id,
connectionId: f.connection.connectionId, applicationId: entry.applicationId, catalogEntryId: entry.id,
toolName, upstreamToolName: "browser_start", policyDecision: "allow", status: "executing" });
await db.insert(toolPolicies).values({ companyId: f.company.id, name: "Block namespaced invocation",
policyType: "block", selectors: { toolName }, priority: 0 });
await expect(f.service.execute(f.binding, f.grant, invocationId, "browser_start", { task: "Denied" })).rejects.toThrow("blocked");
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(0);
});
it("releases a crashed reservation that never reached provider dispatch authority", async () => {
const f = await fixture();
const policy = await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "agent", scopeId: f.agent.id, amount: 100, hardStopEnabled: true }, actor.actorId);
const [session] = await db.insert(browserUseSessions).values({ companyId: f.company.id, issueId: f.issue.id,
agentId: f.agent.id, connectionId: f.connection.connectionId, grantId: f.grant.id, status: "starting" }).returning();
const invocationId = randomUUID();
const [run] = await db.insert(browserUseRuns).values({ companyId: f.company.id, sessionId: session.id,
heartbeatRunId: f.run.id, invocationId, createdAt: new Date(Date.now() - 61_000),
result: { paperclipBudgetHold: { invocationId, amountCents: "50.0000000", state: "held", projectId: null } } }).returning();
f.request.mockClear();
await f.tick();
const [policyRow] = await db.select().from(budgetPolicies).where(eq(budgetPolicies.id, policy.policyId));
expect((await computeObservedSpend(db, policyRow)).effectiveHeldCents).toBe("0.0000000");
expect((await db.select().from(browserUseRuns).where(eq(browserUseRuns.id, run.id)))[0]).toMatchObject({ status: "failed", eventsDrained: 1, result: { paperclipBudgetHold: { state: "released" } } });
expect((await db.select().from(browserUseSessions).where(eq(browserUseSessions.id, session.id)))[0].status).toBe("failed");
expect(f.request.mock.calls.some(([url]) => new URL(url).pathname === "/api/v4/runs")).toBe(false);
});
it("releases a definitively rejected browser hold without inventing a charge", async () => {
const f = await fixture();
const policy = await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "agent", scopeId: f.agent.id, amount: 100, hardStopEnabled: true }, actor.actorId);
f.reject(409);
await expect(f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Rejected", maxCostUsd: 0.5 })).rejects.toThrow("busy");
const [row] = await db.select().from(budgetPolicies).where(eq(budgetPolicies.id, policy.policyId));
expect(await computeObservedSpend(db, row)).toMatchObject({ ledgerCents: "0.0000000", effectiveHeldCents: "0.0000000" });
expect(await db.select().from(costEvents).where(eq(costEvents.companyId, f.company.id))).toHaveLength(0);
await db.update(browserUseRuns).set({ result: null }).where(eq(browserUseRuns.companyId, f.company.id));
expect((await computeObservedSpend(db, row)).unknownBrowserRunCount).toBe(0);
});
it.each(["company", "agent", "project"] as const)("treats a running uncapped browser as unknown when a %s budget is introduced", async scopeType => {
for (const legacy of [false, true]) {
const f = await fixture();
const [project] = await db.insert(projects).values({ companyId: f.company.id, name: "Browser project" }).returning();
const [otherProject] = await db.insert(projects).values({ companyId: f.company.id, name: "Unrelated project" }).returning();
await db.update(issues).set({ projectId: project.id }).where(eq(issues.id, f.issue.id));
const invocation = randomUUID();
await expect(f.service.execute(f.binding, f.grant, invocation, "browser_start", { task: "Uncapped running work" })).resolves.toMatchObject({ status: "running" });
const [run] = await db.select().from(browserUseRuns).where(eq(browserUseRuns.invocationId, invocation));
expect(run.result).toMatchObject({ paperclipBudgetHold: { amountCents: "0.0000000", uncapped: true } });
expect(JSON.parse(String(f.request.mock.calls.find(([url, init]) => url.endsWith("/runs") && init.method === "POST")![1].body))).not.toHaveProperty("maxCostUsd");
if (legacy) await db.update(browserUseRuns).set({ result: null }).where(eq(browserUseRuns.id, run.id));
const scopeId = scopeType === "company" ? f.company.id : scopeType === "agent" ? f.agent.id : project.id;
const policy = await budgetService(db).upsertPolicy(f.company.id,
{ scopeType, scopeId, amount: 100, hardStopEnabled: true }, actor.actorId);
const [row] = await db.select().from(budgetPolicies).where(eq(budgetPolicies.id, policy.policyId));
expect(await computeObservedSpend(db, row)).toMatchObject({ ledgerCents: "0.0000000", effectiveHeldCents: "0.0000000", unknownBrowserRunCount: 1 });
expect((await computeObservedSpend(db, { ...row, scopeType: "project", scopeId: otherProject.id })).unknownBrowserRunCount).toBe(0);
expect(await budgetService(db).getInvocationBlock(f.company.id, f.agent.id, { projectId: project.id })).not.toBeNull();
f.request.mockClear();
await expect(f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Must wait for priced work" })).rejects.toThrow();
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(0);
await budgetService(db).upsertPolicy(f.company.id,
{ scopeType, scopeId, amount: 100, hardStopEnabled: true, unpricedUsagePolicy: "allow" }, actor.actorId);
expect(await budgetService(db).getInvocationBlock(f.company.id, f.agent.id, { projectId: project.id })).toBeNull();
f.complete(); await f.tick();
expect(await computeObservedSpend(db, row)).toMatchObject({ ledgerCents: "15.0000000", effectiveHeldCents: "0.0000000", unknownBrowserRunCount: 0 });
}
});
it.each([
{ label: "explicit capped zero", uncapped: false, state: "held", dispatched: true, status: "running", expected: 0 },
{ label: "legacy zero cap without a flag", uncapped: undefined, state: "held", dispatched: true, status: "running", expected: 1 },
{ label: "uncapped before dispatch", uncapped: true, state: "held", dispatched: false, status: "creating", expected: 0 },
{ label: "failed before dispatch", uncapped: true, state: "released", dispatched: false, status: "failed", expected: 0 },
])("distinguishes $label from unfinished uncapped spending", async item => {
const f = await fixture();
const [session] = await db.insert(browserUseSessions).values({ companyId: f.company.id, issueId: f.issue.id,
agentId: f.agent.id, connectionId: f.connection.connectionId, grantId: f.grant.id, status: "running" }).returning();
const invocationId = randomUUID();
await db.insert(browserUseRuns).values({ companyId: f.company.id, sessionId: session.id, heartbeatRunId: f.run.id,
invocationId, status: item.status, providerRunId: item.dispatched ? randomUUID() : null, eventsDrained: item.status === "failed" ? 1 : 0,
result: { paperclipBudgetHold: { invocationId, amountCents: "0.0000000", projectId: null, state: item.state,
...(item.uncapped === undefined ? {} : { uncapped: item.uncapped }),
...(item.dispatched ? { dispatchCommittedAt: new Date().toISOString() } : {}) } } });
const policy = await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "company", scopeId: f.company.id, amount: 100, hardStopEnabled: true }, actor.actorId);
const [row] = await db.select().from(budgetPolicies).where(eq(budgetPolicies.id, policy.policyId));
expect(await computeObservedSpend(db, row)).toMatchObject({ ledgerCents: "0.0000000", effectiveHeldCents: "0.0000000", unknownBrowserRunCount: item.expected });
const block = await budgetService(db).getInvocationBlock(f.company.id, f.agent.id);
expect(Boolean(block)).toBe(item.expected > 0);
});
it("blocks new budgeted work after an uncapped browser POST becomes uncertain", async () => {
const f = await fixture();
f.loseReply();
const uncertain = randomUUID();
await expect(f.service.execute(f.binding, f.grant, uncertain, "browser_start", { task: "Uncapped" })).rejects.toThrow("could not be confirmed");
const policy = await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "company", scopeId: f.company.id, amount: 100, hardStopEnabled: true }, actor.actorId);
const [row] = await db.select().from(budgetPolicies).where(eq(budgetPolicies.id, policy.policyId));
expect(await computeObservedSpend(db, row)).toMatchObject({ ledgerCents: "0.0000000", effectiveHeldCents: "0.0000000", unknownBrowserRunCount: 1 });
f.request.mockClear();
await expect(f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", { task: "Must wait" })).rejects.toThrow();
expect(f.request.mock.calls.filter(([url, init]) => url.endsWith("/runs") && init.method === "POST")).toHaveLength(0);
// The existing unpriced-usage policy remains the explicit operator override.
await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "company", scopeId: f.company.id, amount: 100, hardStopEnabled: true, unpricedUsagePolicy: "allow" }, actor.actorId);
expect(await budgetService(db).getInvocationBlock(f.company.id, f.agent.id)).toBeNull();
f.usage("0.15");
await f.tick();
expect(await computeObservedSpend(db, row)).toMatchObject({ ledgerCents: "15.0000000", unknownBrowserRunCount: 0 });
});
it("retains unknown browser holds and settles exact terminal usage without exposing internal metadata", async () => {
const f = await fixture();
const policy = await budgetService(db).upsertPolicy(f.company.id,
{ scopeType: "company", scopeId: f.company.id, amount: 100, hardStopEnabled: true }, actor.actorId);
f.loseReply();
const invocation = randomUUID();
await expect(f.service.execute(f.binding, f.grant, invocation, "browser_start", { task: "Read", maxCostUsd: 0.5 })).rejects.toThrow("could not be confirmed");
const [row] = await db.select().from(budgetPolicies).where(eq(budgetPolicies.id, policy.policyId));
expect((await computeObservedSpend(db, row)).effectiveHeldCents).toBe("50.0000000");
f.usage("0.150000001");
await f.tick();
expect(await computeObservedSpend(db, row)).toMatchObject({ ledgerCents: "15.0000001", effectiveHeldCents: "0.0000000" });
const [run] = await db.select().from(browserUseRuns).where(eq(browserUseRuns.invocationId, invocation));
expect(run.result).toMatchObject({ paperclipBudgetHold: { state: "settled", accountedCentsExact: "15.0000001" } });
const replay = await f.service.execute(f.binding, f.grant, invocation, "browser_start", { task: "Read" });
expect(JSON.stringify(replay)).not.toContain("paperclipBudgetHold");
expect(await db.select().from(costEvents).where(eq(costEvents.companyId, f.company.id))).toHaveLength(1);
});
it("retains exact cumulative accounting when an older browser row has no hold projection", async () => {
const f = await fixture();
const invocation = randomUUID();
await f.service.execute(f.binding, f.grant, invocation, "browser_start", { task: "Legacy result" });
const [run] = await db.select().from(browserUseRuns).where(eq(browserUseRuns.invocationId, invocation));
await db.update(browserUseRuns).set({ result: null }).where(eq(browserUseRuns.id, run.id));
f.usage("0.150000001"); f.complete(); await f.tick();
await db.update(browserUseRuns).set({ eventsDrained: 0 }).where(eq(browserUseRuns.id, run.id));
f.usage("0.150000002"); await f.tick();
const [total] = await db.select({ cents: sql<string>`sum(${costEvents.costCents})::text` }).from(costEvents).where(eq(costEvents.companyId, f.company.id));
expect(total.cents).toBe("15.0000002");
expect((await db.select().from(browserUseRuns).where(eq(browserUseRuns.id, run.id)))[0].result)
.toMatchObject({ paperclipBudgetHold: { state: "settled", accountedCentsExact: "15.0000002" } });
});
it("caps paid work at the remaining hard budget and enforces reported spend", async () => {
const f = await fixture();
await budgetService(db).upsertPolicy(
f.company.id,
{
scopeType: "agent",
scopeId: f.agent.id,
amount: 15,
hardStopEnabled: true,
},
actor.actorId,
);
await f.service.saveSettings(f.company.id, f.grant.id, actor.actorId, {
allowedProfileIds: [],
maxCostUsd: 0.5,
});
await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_start",
{ task: "Read", maxCostUsd: 1 },
);
const call = f.request.mock.calls.find(
([url, init]) => url.endsWith("/runs") && init.method === "POST",
)!;
expect(JSON.parse(String(call[1].body))).toMatchObject({
maxCostUsd: 0.15,
browserSettings: { record: false },
});
f.ready();
f.complete();
await f.tick();
await f.tick();
expect(
(await db.select().from(agents).where(eq(agents.id, f.agent.id)))[0],
).toMatchObject({ status: "paused", spentMonthlyCents: 15 });
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.companyId, f.company.id))
)[0].status,
).toBe("closed");
await expect(
f.service.execute(f.binding, f.grant, randomUUID(), "browser_start", {
task: "Read again",
}),
).rejects.toThrow();
});
it("preserves the idle grace period when the task and parent finish before the last poll", async () => {
const f = await fixture();
await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_start",
{ task: "Read" },
);
f.ready();
f.complete();
await db
.update(heartbeatRuns)
.set({ status: "succeeded" })
.where(eq(heartbeatRuns.id, f.run.id));
await db
.update(issues)
.set({ status: "done" })
.where(eq(issues.id, f.issue.id));
await f.tick();
expect(
(
await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.companyId, f.company.id))
)[0].status,
).toBe("idle");
expect(
f.request.mock.calls.some(([url]) => url.endsWith("/cancel")),
).toBe(false);
});
it("confirms hosted shutdown before removing the connection credential", async () => {
const f = await fixture();
await f.service.execute(
f.binding,
f.grant,
randomUUID(),
"browser_start",
{ task: "Read" },
);
f.ready();
await f.tick();
await f.access.archiveConnection(
f.connection.connectionId,
f.company.id,
actor,
);
const [session] = await db
.select()
.from(browserUseSessions)
.where(eq(browserUseSessions.companyId, f.company.id));
expect(session.status).toBe("closed");
const [grant] = await db
.select()
.from(connectionGrants)
.where(eq(connectionGrants.id, f.grant.id));
expect(grant.status).toBe("revoked");
expect(grant.credentialSecretRefs).toEqual([]);
});
},
);