Skip to content

feat(takeover): take control in the native viewer, and let agents ask for help - #372

Merged
ElbertePlinio merged 17 commits into
mainfrom
feat/341-takeover
Oct 28, 2026
Merged

ElbertePlinio merged 17 commits into
mainfrom
feat/341-takeover

Conversation

@ElbertePlinio

Copy link
Copy Markdown
Member

Taking control of a lab session needs pickforge-lab watch --control and an external VNC client. An agent cannot ask for help in a way the user notices, and it learns about a takeover only from busy errors.

The native viewer from #340 now has Take control and Return control. Its host holds the existing human lease and grants input to the viewer only while the lease is live, and the viewer sends pointer, scroll and keyboard input to the session's Xvfb with XTEST. A new MCP tool, request_takeover, grows the viewer, shows the agent's message with a highlighted Take control button, and sends a desktop notification without taking focus. A second tool, takeover_wait, waits for the human to return control. No writable network server is started; the x11vnc path stays as the fallback for watch --control without a native viewer.

Closes #341

Requirement Verified by Scenario Result Evidence
Take control acquires the human lease with the same permit draining, heartbeat and watchdog as watch --control TS host tests, Rust Xvfb tests, manual QA Take from the bar and from the banner; renew on each heartbeat; first failed renewal revokes; no revoke ack within 1 s kills the viewer; a capture or wait sample in flight drains before the lease; a wait capture in flight stops within about 1 s, even when it ignores SIGTERM; stale lease at watch --control startup; a second watch --control during live control Pass; the second start is refused with "already under human control", and the first control and the open viewer stay native-viewer-host.test.ts, native-viewer.test.ts, control_xvfb.rs, wait.test.ts, QA item 1, F2 and F3
Pointer, scroll and keyboard reach the session; text arrives in any host layout; input stops when the lease ends Rust Xvfb tests, manual QA Composed text olá ção ÁÉ ñ ß €, chords, named keys, held modifiers, lock keys dropped; buttons 1 to 9; scroll capped at 32 steps per axis with the grant checked before each step; grant expiry mid-burst; revoke; stdin end of file; ABNT2 dead keys on a br/abnt2 host typing into a US session Pass; áãêàüç, ÁÕ and Ç arrive with no stray dead key inject_xvfb.rs, control_xvfb.rs, QA J1, item 1, F4
Return control and closing the window end the lease; a crash is recovered; every key, Esc included, goes to the session Rust and TS tests, manual QA Return with Shift and button 1 held; close while controlling; kill -9 of the host; host killed with the viewer frozen; Esc during control Pass; close gives returned; no held input after any end; the watchdog recovers in about 5 s QA items 1, 3 and 4, J7, F1, stuck-input.test.ts
desktop_screenshot and desktop_wait fail closed with busy during control MCP, CLI and security tests, manual QA Screenshot and wait during control, after return, and with a sample in flight when control starts Pass takeover.test.ts, security-takeover-observations.test.ts, QA item 1
request_takeover grows the viewer, shows the message and notifies without focus, and returns on return, timeout or cancel with a fresh screenshot MCP tests, manual QA Return; timeout; MCP cancellation with no response sent; viewer closed; window expanded by the user stays expanded, a thumbnail grows and collapses back; message with markup and an emoji at the cut; 500 characters; control, bidirectional, format and line separator characters Pass request-takeover.test.ts, QA items 1, 2 and 5, J3 to J5
With viewer.mode: "auto", MCP opens a missing viewer; otherwise it returns the CLI command MCP tests, manual QA Auto mode with a matching binary; manual mode with the binary on PATH; no graphical session Pass; manual mode returns pickforge-lab watch --session <id> and launches nothing request-takeover.test.ts, QA J2
takeover_wait waits until no human lease is active MCP tests Return, timeout, cancellation, no lease at start Pass takeover-wait.test.ts
request_user_input points to request_takeover for secrets MCP tests Secret guidance text Pass user.test.ts
No writable network server Security tests Static scan of crates/ for listeners; the x11vnc argv recorder never sees a writable start on the viewer path; no socket op grants control or injects input Pass security-native-control.test.ts, security-viewer-takeover.test.ts, native-viewer.test.ts
Tests and docs Repo checks Rust fmt, clippy for Linux and Windows, tests on Rust 1.99; typecheck, lint, coverage with the viewer on PATH, build, PickCheck; README, SECURITY.md, PROTOCOL.md, UNRELEASED.md Pass: 3,343 TypeScript tests, lines 90.26%; 260 Rust tests; PickCheck found nothing new or worse in 940 functions TypeScript at 26599e4, and 76d5557 changes only docs and a comment; Rust at bee23ab, unchanged since

QA pass: I drove the built CLI, the MCP server and the release viewer on nested Xvfb displays, with a fake notify-send that records its arguments. The first pass found four defects, all fixed here: closing the window while controlling reported viewer_closed, a cut emoji in the notification, bidirectional characters in the message, and a held time counted from the request instead of from Take control. The rerun found that a second watch --control took over a live control, and that a refused watch --control gave a vague error and closed the open viewer. One check fixes both, with tests that fail without it. The final pass confirmed both fixes, close while controlling, and Brazilian ABNT2 dead keys on the viewer's display. A last pass on 26599e4 started control three ways while a long wait ran. The wait failed with busy within 0.21 s of the lease, and control was granted within 0.8 s. Waits after return reported changed and stable with samples counted. Not covered: take control on a Wayland desktop with an input method such as fcitx5. Every QA run used the viewer's X11 backend on Xvfb, without an input method.

Declined:

  • viewer.mode: "auto" from a project config can let MCP open the viewer. That is the documented behavior from before this change, and no input is granted until the human presses Take control. Filed as viewer: a repository config can turn on automatic viewer launch #370 for an owner decision.
  • After a hard deadline on a live host, for example after suspend and resume, the watchdog stops the host group instead of letting it end cleanly. The result still fails closed.
  • Destroying a session while controlling closes the viewer without a "Session ended" frame. Destroy stops the viewer by design since watch: native Rust picture-in-picture viewer #340.
  • Help messages refuse emoji joined with U+200D and soft hyphens, because the rule refuses every format character.

Follow-ups: #369 (watch --control without a VNC client suggests a tunnel to a read-only server), #370 (viewer.mode trust), #371 (a version-mismatched watch stops a host that holds live control), and #317, which already covers the .takeover-identity and .input-closed files that destroyed sessions leave.

Rollback: revert this PR. The native viewer goes back to watch only, watch --control uses the external VNC client again, and the two MCP tools disappear. No stored data changes; a lease file left by a running host is recovered as stale by the existing watchdog.

Council review, 3 rounds:

Delegate Round 1 Round 2 Round 3
GPT-6 Astra 6/10 7/10 7/10
Fable 5.1 7/10 8/10 8/10
Kimi K3.1 Pro 4/10 10/10 not run

Astra's round 3 asked for changes. It showed that the 3 s cap on each wait sample timed out slow but healthy captures, and that a capture ignoring SIGTERM still held its permit past the lease drain. Fable raised the same timeout. A wait capture now stops when a takeover starts, and keeps the whole wait budget otherwise. After round 3, a fresh Fable review rechecked these fixes and the D6 fix from QA, and approved at 9/10. Its two nits: the wait cleanup docs, now fixed, and #371.

Port the keymap parsers, level lookup and spare keycode planner from
desktop-linux to Rust, with the legacy keysym table. The injector sends
pointer, scroll, named keys, chords and composed text only while a
control grant is valid, reconciles modifiers per character, and releases
every held key and button on revoke, expiry and drop.

Refs #341
Add the #341 stdio control messages: attention, clearAttention and
control commands, and takeRequested, returnRequested and controlAck
events. A control state machine owns the XTEST injector. It releases
every held key and button before it reports a revoke or a return, and
on expiry, focus loss, close, session end, shutdown and stdin end of
file.

The watch bar offers Take control. While controlling, the bar shows
Return control and Fullscreen, every key goes to the session, and
pointer input maps through the scale and letterbox. The attention
banner wraps the host's message inside the window and highlights
Take control.

Tests cover protocol parsing, the state machine, banner layout, and
on Xvfb the absence of input and held keys after each end reason.
A second control start stopped the live host and took its lease over.
The launcher now checks the lease under the session lock before it stops
the open host, and fails with HumanLeaseHeldError.
…ing each sample

A 3 s sample cap timed out slow but healthy captures, and a capture that
ignored SIGTERM still held its permit past the lease drain timeout. Each
wait capture now keeps the full wait budget, watches for a published lease,
and cancels its commands with a short kill grace. The digest runs outside
the permit because it reads a file, not the screen.
@ElbertePlinio

Copy link
Copy Markdown
Member Author

Live check after merge, on main at 4c6e18e, on the owner's Hyprland desktop with fcitx5 set as the input method. The PiP opened pinned in the corner without focus. request_takeover grew it and showed the banner. Take control, typing and Return control worked. The session received ação, à and vovó with correct accents. Backspace arrived as the X BackSpace key; the raw test terminal showed it as ^H, the same as for an agent's own desktop key BackSpace.

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.

takeover: take control from the native viewer, and agent-requested takeover

1 participant