Skip to content

feat(core): add browser session contracts - #24

Closed
ElbertePlinio wants to merge 6 commits into
mainfrom
feat/browser-session-contracts
Closed

ElbertePlinio wants to merge 6 commits into
mainfrom
feat/browser-session-contracts

Conversation

@ElbertePlinio

Copy link
Copy Markdown
Member

Changed

  • Add the browser session type and brow-* IDs.
  • Add the composite browser record contract and desktop width/height metadata.
  • Resolve sessions by capability, allowing future browser sessions to serve desktop tools.
  • Add Linux process identity helpers and verified process-group teardown that refuses reused PIDs and confirms surviving children are killed.
  • Wire browser identity-aware liveness and best-effort reaping into the session registry.

This is PR 2 of the computer-use roadmap. It settles shared contracts only; Chrome launch, profile lifecycle, and CLI/MCP browser creation remain in the later #18 slice.

Validated

  • bun run typecheck
  • bun run test — 579 passed, 1 skipped
  • bun run test:coverage — thresholds met
  • bun run build
  • Regression test for a stubborn child surviving its process-group leader
  • Regression test proving a reused browser PID is not signalled
  • Browser liveness requires both Xvfb and the recorded Chrome process identity

Not tested

  • Real Chrome lifecycle; not part of this contract-only slice.

Risk

  • Process-group identity uses Linux /proc, matching PickLab's current Linux desktop scope.

Refs #18
Refs #17

@ElbertePlinio

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 202b237728

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/core/src/proc.ts Outdated
Comment on lines +397 to +398
if (stat !== undefined && stat.pgrp === pgid) {
members.push(pid);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat zombie group members as terminated

When a killed child is reparented to a non-reaping PID 1 (common in containers), it remains in /proc with state Z and the same process group. This check counts that zombie as a live group member, so stopProcessGroupVerified() never observes an empty group and waits until returning survived even after SIGKILL; browser cleanup can then spend the full timeout or misreport a successful teardown as failed. Parse the stat state and exclude zombies from live group membership.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9e28b3e. /proc state is now parsed and zombie (Z) entries are excluded from live process-group membership, with a synthetic parser regression test. Group teardown now treats zombie-only groups as terminated instead of timing out or reporting survived.

@ElbertePlinio

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bdf0b0cc67

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +222 to +226
isPidAlive(xvfbPid) &&
processIdentityMatches({
pid: browser.browserPid,
startTicks: browser.browserStartTimeTicks,
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat surviving browser groups as alive

When the recorded browser process is only a launcher/group leader that exits after spawning the real browser (for example a distro chromium wrapper), the process group can still contain the usable browser while processIdentityMatches returns false. In that case reapDeadRunningSessions will classify the session as dead and then stopProcessGroupVerified will kill the still-running browser group; the liveness check should also consider verified process-group members for the recorded browser PGID before reaping.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not applying this suggestion because it conflicts with the PID-reuse safety boundary: the recorded leader identity is the authority, and a bare PGID with no matching leader cannot be proven to belong to PickLab. The #18 lifecycle implementation will keep a stable supervised group leader and fail startup if that identity disappears. This contract-only PR exposes no browser-session creator.

} from "./proc.js";

export type SessionType = "desktop" | "android" | "desktop+android";
export type SessionType = "desktop" | "android" | "desktop+android" | "browser";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle browser sessions in destroy-all paths

Adding browser to the persisted SessionType makes these records appear in listSessions(), but the CLI and MCP destroy helpers still switch only on desktop and android (packages/cli/src/commands/session.ts and packages/mcp-server/src/tools/session.ts). When an internal browser session exists, picklab session destroy --all or session_destroy(all=true) reports Cannot destroy ... type "browser" and leaves the browser/display processes and session record behind instead of using the new cleanup support.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct for the future lifecycle integration, but intentionally deferred to the existing #18 implementation slice: this PR adds internal record/process contracts only and exposes no CLI/MCP path that can create a browser record. The browser lifecycle branch adds create/status/destroy and destroy-all dispatch together, with regression coverage, before browser sessions become user-creatable.

Comment thread packages/core/src/proc.ts Outdated
if (leader !== undefined && leader.startTicks !== identity.startTicks) {
return { outcome: "reused", signaled: false };
}
if (leader === undefined && listProcessGroupMembers(identity.pid).length === 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat missing group leaders as reused

If the recorded browser PID has exited and that numeric PID/PGID is later reused by an unrelated process group whose leader has already exited but still has children, this branch falls through because listProcessGroupMembers(identity.pid) is non-empty and signalGroup then kills the unrelated group without ever verifying the new leader's start time. That breaks the PID-reuse protection this helper is meant to provide for stale browser session records; the initial cleanup path should fail closed when the leader cannot be identity-checked rather than signaling an unverifiable PGID.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f6eedbd. If the recorded leader is gone while the numeric PGID still has members, teardown now returns reused without signalling. A regression test captures the leader identity, lets it exit while a child remains, and proves the unverifiable group is not killed.

@ElbertePlinio

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e943c91e1a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/core/src/proc.ts Outdated

/** Read a process's start time in clock ticks, or undefined if it is gone. */
export function readProcessStartTicks(pid: number): number | undefined {
return readProcStat(pid)?.startTicks;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat zombie process identities as dead

When the recorded browser process exits but remains unreaped as a zombie while Xvfb is still running, /proc/<pid>/stat still exists with the same start ticks, so processIdentityMatches continues to return true and isSessionProcessAlive will keep the browser session marked running. Since parseProcStat already exposes state, this helper should ignore Z entries before returning a live identity/start time so the reaper can remove dead browser sessions.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 788fc81. readProcessStartTicks now returns undefined for zombie (Z) states, so zombie leaders no longer satisfy browser identity/liveness checks. A mocked /proc regression test covers both start-tick and identity reads.

@ElbertePlinio

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 205087dec3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ElbertePlinio

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3506392a7b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +274 to +278
const browser = record.browser;
if (browser?.browserPid !== undefined) {
try {
await stopProcessGroupVerified({
pid: browser.browserPid,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Stop the browser group before killing its display

When reaping a browser session whose Chrome is still alive, the loop just above first terminates desktop.xvfbPid; if losing the X server makes the browser process-group leader exit before this block runs, stopProcessGroupVerified treats the remaining group as unverifiable/reused and does not signal surviving renderer children. That leaves browser children running while the session record is deleted, so the verified browser group should be stopped before tearing down the desktop helpers.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. This is a new P2 on the fourth and final allowed review round, so PR #24 is left open and will not merge. The fix must stop the verified browser group before VNC/Xvfb helpers; it is tracked in #18 and will be applied in the browser lifecycle branch with regression coverage. No fifth Codex round will be requested.

@ElbertePlinio

Copy link
Copy Markdown
Member Author

Superseded by #26, which includes the final teardown-order fix, additional fail-closed hardening, full validation, and a clean review on the corrected HEAD.

@ElbertePlinio
ElbertePlinio deleted the feat/browser-session-contracts branch July 12, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant