# WO-37: Preserve browser ownership and process outcome truth

**Status:** complete in repository; verified for user publication
**Priority:** P1
**Date:** 2026-09-05

## Reproduced failures

- Separate browser tool instances inherit a module-global session; HTTP 500
  responses with positive JSON become successful actions. Session/action HTTP
  calls lack deadlines, and close can terminate another client's service.
- A video worker timing out can exit zero and be accepted, or ignore TERM and
  leave the operation pending forever. Output buffers are unbounded.
- A Carbonyl worker exiting nonzero is accepted when its last stdout line says
  `ok: true`; cancellation does not reliably drain child processes.

## Repair and acceptance

- Own browser session state per tool instance, close only that session, and
  validate bounded HTTP responses across session and action calls.
- Connect browser cancellation to outstanding HTTP requests.
- Reuse the shared process runner for deadlines, bounded output, process-group
  termination, and exit status; preserve progress reporting and diagnostics.
- Test ownership, transport contradictions, timeouts, cancellation, and worker
  exit contradictions using hermetic fixtures without live models or services.

## Verification

- Browser session state is instance-owned. Every POST carries its session id,
  including close; closing one session no longer kills the shared service.
  Failed closure retains the handle for truthful recovery.
- Browser HTTP deadlines cover response bodies, not only headers. JSON shape,
  positive action receipts, and HTTP status are checked independently, and
  active requests honor the tool's cancellation hook.
- Video and Carbonyl workers use `runProcessBuffer` with bounded output and
  process-group termination. Timeout/abort/overflow errors stay failures even
  if a worker's shutdown handler emits exit zero or positive JSON.
- `pnpm exec vitest run --maxWorkers=4 --minWorkers=1 tests/browser-action-lifecycle.test.ts tests/browser-action-screenshot.test.ts tests/media-worker-lifecycle.test.ts tests/video-generate.test.ts`
  from `packages/execution`: **36 passed**.
- `pnpm exec tsc --noEmit` from `packages/execution`: passed.
- Worker tests use in-memory event emitters and mocked process leases; browser
  tests use mock fetch with live subprocess setup forbidden. No model loading,
  external requests, live browser sessions, or service changes occurred.
- Parent owns integration and push; publication is excluded.

## Ownership review follow-up

Review found a remaining launch path that killed every listener on the configured browser port after a capability mismatch. Port occupancy is not proof of process ownership. The launcher now returns a clear incompatible-service failure without invoking shell cleanup or spawning a replacement. A focused regression asserts the existing service is untouched; all 10 browser lifecycle tests passed in /tmp/omnius-wo37-ownership.log.

## Nested Playwright ownership and Stop follow-up

Carbonyl's text fallback constructs `PlaywrightBrowserTool`. Its prior module-global browser/page/context and diagnostic buffers meant a cancellation hook could close another tool instance's flow. These handles now belong to each tool instance; stateful helpers use that instance's asynchronous scope, and event listeners capture their owning session explicitly. Events from an old closed page cannot enter the replacement page's diagnostics. Same-instance overlapping calls are rejected.

`cancel()` closes only the owned page/context/browser, waits for asynchronous cleanup, and prevents a late launch or completed request from becoming successful after Stop. Every layer receives cleanup even if another layer fails. Failed closure retains the affected handle for retry and returns failure. Playwright package/browser installation now uses the shared asynchronous argv process runner with the invocation signal and process-group cleanup. The visual-click lookup receives the same signal and checks cancellation before any DOM fallback or subsequent action; its backend cancellation is implemented in the coordinated vision repair.

Review also confirmed two synchronous screenshot conversion shell commands, including interpolation of caller-controlled output paths. Both use one bounded asynchronous Python argv/stdin helper now. Image bytes never enter shell or Python program text; cancellation cannot become a successful image fallback, and empty/non-JPEG conversion output preserves the original PNG instead of being labeled JPEG.

Focused verification: **40 tests passed across six suites** (`audio-generation-cancellation`, `audio-generate`, `playwright-browser-cancellation`, `playwright-browser-contract`, `playwright-browser-install`, `playwright-image-conversion`). Execution `tsc --noEmit` and scoped `git diff --check` passed. Browser tests use synthetic objects and conversion stubs; no browser, inference, network, or capture operation ran. Parent owns aggregate review/push. Nested Carbonyl signal wiring is a separate coordinated commit.

## Invocation and service-startup cancellation follow-up

Independent review found that cancelling startup returned without draining the newly spawned server, asynchronous spawn errors had no handler, repeated readiness probes could extend a nominal 60-second wait to roughly seven minutes, and cleanup addressed a mutable global child without establishing a private process group.

- A shared owned-service primitive now retains an immutable child handle, creates a private process group, handles asynchronous spawn failures, and enforces one absolute readiness deadline, including stalled probes. TERM escalates to KILL; an undrained process is reported explicitly. Any service-parent close also terminates the remaining owned group, including unexpected exit and synchronous TERM-close. Parent signal handlers are shared and removed after the final owned service closes.
- Browser startup has one shared launch promise with separate waiting owners. Cancelling one waiter preserves another waiter's startup; cancelling the final waiter drains only the child created by that launch. Ready shared services and incompatible external listeners are not terminated by an individual tool action.
- Browser and Carbonyl instances reject overlapping execution. Cancelled health probes retain the known browser session. Carbonyl cancellation reaches setup, Python workers, and its nested Playwright fallback. Browser vision-click passes the same owner signal through point discovery and does not dispatch a click after cancellation.
- Five browser/service suites passed **30 tests** after the visual-point regression; the broader combined follow-up run passed **87 tests across 13 suites**. Tests use mocked services/processes and synthetic inputs; explicit regressions cover both remaining-group cleanup cases requested in independent review. Execution typecheck and scoped diff checks passed.

Carbonyl nested cleanup receipt follow-up: a failed Playwright close now makes the fallback fail and retains the browser on the owning Carbonyl instance. Before admitting another action, that instance retries the retained cleanup; it clears the handle only after a successful close receipt. Cancellation preserves any cleanup error in the returned failure. The focused Carbonyl suite passed **3 tests**, including a failed close, blocked new work during retry failure, and successful ownership release.

## Repository closure — September 5

All confirmed findings and independent review follow-ups in this order are resolved. The [program verification ledger](TOOL-QUALITY-2026-09-05.md#verification-ledger) records the final full suites and clean build. Earlier pending integration notes above are historical and superseded by this closure.

Scoped repair commits delivered to origin/main: 0d6dcc66, fe3c6487, 52cb2623, 484938c1, 72382afa, 4a072473. Publication and subsequent live acceptance remain with the user.

Source and integration locations:

- [packages/execution/src/tools/browser-action.ts](../../../packages/execution/src/tools/browser-action.ts)
- [packages/execution/src/tools/carbonyl-browser.ts](../../../packages/execution/src/tools/carbonyl-browser.ts)
- [packages/execution/src/tools/playwright-browser.ts](../../../packages/execution/src/tools/playwright-browser.ts)
- [packages/execution/src/tools/vision.ts](../../../packages/execution/src/tools/vision.ts)
- [packages/execution/src/owned-service-process.ts](../../../packages/execution/src/owned-service-process.ts)
- [packages/execution/src/process-async.ts](../../../packages/execution/src/process-async.ts)

The [point-localization cancellation follow-up](WO-37-point-localization-cancellation.md) closes the nested vision boundary under this same work order.
