Skip to content

fix(server): device_close with shutdown after a plain close turns the device off - #48437

Open
juliusmarminge wants to merge 2 commits into
mainfrom
factory/issue-48424
Open

juliusmarminge wants to merge 2 commits into
mainfrom
factory/issue-48424

Conversation

@juliusmarminge

Copy link
Copy Markdown
Member

If an agent closed a device without shutting it down and then called device_close again with shutdown: true, the call reported success but did nothing. The device stayed booted and kept its Simlock lease until the lease expired. Now that second close turns the device off. If another thread still has the device open, the call returns an error.

  • DeviceService.close: when shutdown is set, a deviceId is named and the thread has no session left for it, the service shuts down that device if it is booted on the host. If another thread still has a session on the device, it fails with DeviceHostUnavailableError and does not turn it off.
  • MCP device_close: when the thread has no session for the device, the handler finds the device's host from the device list with locateDevice, so a Simlock or SSH device is shut down on its own host and not looked up on the local one.

Fixes #48424

Reproduction on main

Commit: origin/main 9ba2879. Real server: T3CODE_DEV_SEED=0 node apps/server/src/bin.ts --base-dir <fresh dir> --port <free> --no-browser, driven over its WebSocket RPC with a scratch script (sign-in as in scripts/scenarios/t3.ts rpcClient). Steps: (1) server.updateSettings with simlockDeviceHostsEnabled: true and one Simlock host (the real shared gateway, maxLeases: 1, maxLeaseMinutes: 30); device.configure {enabled: true}. (2) device.open a Pixel 9 (android) on that host for thread A: it returned a session and the device showed booted: true. (3) device.close {threadId A, deviceId, shutdown: false}: success, the session was removed and the device was still booted (this part is intended). (4) device.close {threadId A, deviceId, shutdown: true}: success with no error, but the device was still booted: true and no lease release reached Simlock. (5) device.open of another Android device for thread B failed with DeviceHostUnavailableError "Pool is limited to 1 device at once. Close one in the Device panel first." Cleanup: device.shutdown (the Device panel's power-off) released the lease (booted list empty), then the server was stopped. The MCP device_close handler is a thin wrapper over the same DeviceService.close, so the agent tool behaves the same way. A failing unit test in DeviceService.test.ts (open → close(shutdown:false) → close(shutdown:true) → expects a /shutdown request and booted:false) fails on main: no /shutdown request is sent.

Evidence

Being captured: before and after in the real app. This section is filled in before the pull request can merge.

Tests

  • vp test run src/device/DeviceService.test.ts -t "close with shutdown follows" without the fix: 1 failed (repro confirmed)
  • vp test run src/device/DeviceService.test.ts src/mcp/McpDeviceToolkit.test.ts (apps/server) with the fix: 110 passed
  • vp test run src/device/DeviceService.test.ts -t close after adding the error-path test: 20 passed
  • vp run check:changed: format, lint, knip, graphql, typecheck (3 packages) and tests (3 files, 98 tests) all pass

Not in this change

  • The MCP device_close handler still returns {} when shutdown: true names a device that is not booted on any host; it reports no 'already off' state.
  • close with shutdown: true and no deviceId (close every device for the thread) still does nothing when the thread has no sessions; without a device id there is nothing safe to shut down.

Who did the work

  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
  • Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5

Run by the t3code factory in T3 Code.

juliusmarminge and others added 2 commits December 3, 2026 20:28
…e device on (#48424)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… device off

A close without shutdown retires the thread's session, so a later close with
shutdown matched nothing and reported success while the device stayed booted
and kept its Simlock lease. Close now shuts down the booted device it names
when the thread has no session left, and fails when another thread still has
that device open. The MCP device_close tool finds the device's host the same way.

Fixes #48424

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Dec 3, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at d275eff

Macroscope's review found this PR approvable — A shutdown request now turns off a named device even after its session was already closed. Existing host-disambiguation checks and a guard against shutting down another thread’s device limit the exposure.

You can add or adjust custom eligibility rules. Learn more.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

Provider Metric Main baseline This PR Impact PR ceiling
Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅
Codex Live turn messages 1 1 0 (0.0%) 8 ✅
Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅
Claude Live turn messages 1 1 0 (0.0%) 8 ✅

Other scenarios

All 64 scenario measurements match main.

All 64 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling
Codex short turn resume WebSocket decoded 10.5 KiB 10.5 KiB 0 B (0.0%) 12.7 KiB ✅
Codex short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn running, cold open HTTP decoded 281.7 KiB 281.7 KiB 0 B (0.0%) 302.7 KiB ✅
Codex long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Codex long turn running, history page HTTP decoded 92.5 KiB 92.5 KiB 0 B (0.0%) 107.4 KiB ✅
Codex long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Codex long turn finished, cold open HTTP decoded 381.5 KiB 381.5 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, socket cold open WebSocket decoded 381.6 KiB 381.6 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, work page HTTP decoded 78.3 KiB 78.3 KiB 0 B (0.0%) 200.2 KiB ✅
Codex long turn finished, work page rows 75 75 0 (0.0%) 150 ✅
Codex idle thread resume WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell snapshot HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Codex fleet shell subscribe WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Codex fleet shell resume check rows 0 0 0 0 ✅
Codex stale shell reload HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex stale shell reload rows 27 27 0 (0.0%) 27 ✅
Codex stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Codex stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex stale todo list cold open HTTP decoded 72.4 KiB 72.4 KiB 0 B (0.0%) 83.0 KiB ✅
Codex stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Codex stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅
Claude short turn resume WebSocket decoded 10.6 KiB 10.6 KiB 0 B (0.0%) 12.7 KiB ✅
Claude short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn running, cold open HTTP decoded 283.3 KiB 283.3 KiB 0 B (0.0%) 302.7 KiB ✅
Claude long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Claude long turn running, history page HTTP decoded 93.4 KiB 93.4 KiB 0 B (0.0%) 107.4 KiB ✅
Claude long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Claude long turn finished, cold open HTTP decoded 384.2 KiB 384.2 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, socket cold open WebSocket decoded 384.3 KiB 384.3 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, work page HTTP decoded 79.2 KiB 79.2 KiB 0 B (0.0%) 200.2 KiB ✅
Claude long turn finished, work page rows 75 75 0 (0.0%) 150 ✅
Claude idle thread resume WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell snapshot HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Claude fleet shell subscribe WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Claude fleet shell resume check rows 0 0 0 0 ✅
Claude stale shell reload HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude stale shell reload rows 27 27 0 (0.0%) 27 ✅
Claude stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Claude stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude stale todo list cold open HTTP decoded 73.4 KiB 73.4 KiB 0 B (0.0%) 83.0 KiB ✅
Claude stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Claude stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅

Baseline: e915e31 · PR result: d275eff · Source CI: success

Scenario and decoded snapshot size

10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

  • Codex decoded thread snapshot: 108.5 KiB
  • Claude decoded thread snapshot: 108.8 KiB

Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The device close flow now supports shutting down an identified device after the requesting thread has closed its session without shutdown. It rejects shutdown if another thread still holds a session for that device. Regression tests cover both outcomes.

Changes: Device shutdown

Layer / File(s) Summary
Resolve and process device shutdown
apps/server/src/mcp/toolkits/device/handlers.ts, apps/server/src/device/DeviceService.ts, apps/server/src/device/DeviceService.test.ts
When no session is open and shutdown is requested for a deviceId, the handler locates the device and supplies its host ID. DeviceService.close includes an eligible booted device in shutdown processing, rejects shutdown if another thread has a session for it, and returns the combined shutdown set. Tests cover successful shutdown after a non-shutdown close and rejection when another thread holds the device.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant AgentThread
  participant device_close
  participant DeviceService
  participant DeviceHost
  AgentThread->>device_close: Request shutdown for deviceId
  device_close->>DeviceService: Close with located hostId
  DeviceService->>DeviceHost: Send shutdown request when no other thread holds the device
Loading

Suggested reviewers: gabrielelpidio

Merge Risk: Minimal · up to d275e

This fixes a no-op where a later close with shutdown did not power off the device or release its lease. A narrow concurrent-open race remains theoretically possible, but its impact is small and the change looks safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix and its scope: device_close with shutdown after a plain close turns the device off. The PR objectives and description describe the same change: a second close with shutdown: tr…
Description check Passed The description follows the template's required structure. It gives the problem (a no-op second close that leaves the device booted), the change (DeviceService.close and the MCP device_close handler),…
Linked Issues check Passed [#48424] Requirement: a second device_close with shutdown: true, after a shutdown: false close removed the thread's session, must release the Simlock lease, or return an error. The diff at head implem…
Out of Scope Changes check Passed All changed code maps to #48424. The DeviceService.ts change adds the unclaimed-device shutdown path and the other-thread error. The return value changes from closing to shuttingDown so the shutdown l…
Full details: Title check

Explanation

The title names the fix and its scope: device_close with shutdown after a plain close turns the device off. The PR objectives and description describe the same change: a second close with shutdown: true now powers off a booted device and releases its Simlock lease. The title follows the conventional commit format the template recommends. It omits the error case for a device that another thread still holds, but that is a secondary detail.

Full details: Description check

Explanation

The description follows the template's required structure. It gives the problem (a no-op second close that leaves the device booted), the change (DeviceService.close and the MCP device_close handler), and a linked issue (#48424). It includes a detailed reproduction and tests. Two template sections are weak. The Scope section does not state why the change is a bug fix, though it links the issue. The Evidence section is a placeholder that says it is being captured and must be filled before merge. Because the template asks for before and after evidence for behavior changes, that gap must close before merge.

Resolution

Fill in the Evidence section with before and after proof from the real app, or with a clear reason it is not needed. Optionally, state in Scope why this fix needs no separate issue beyond #48424.

Full details: Linked Issues check

Explanation

[#48424] Requirement: a second device_close with shutdown: true, after a shutdown: false close removed the thread's session, must release the Simlock lease, or return an error. The diff at head implements both branches in DeviceService.close (apps/server/src/device/DeviceService.ts). When shutdown is set, a deviceId is given, and no session from this thread is closing, the code selects the matching booted device on the host (unclaimed). If any other session exists on that device, it returns DeviceHostUnavailableError. Otherwise it adds the device to shuttingDown, and the existing readiness check and shutdown loop run over shuttingDown. The MCP device_close handler (apps/server/src/mcp/toolkits/device/handlers.ts) calls devices.locateDevice when no session is open and shutdown is true, so the close uses the device's own host. The PR description reports a failing unit test before the fix and passing tests after it. I did not re-run the tests. The repro evidence section is still pending, which does not affect the code-level verdict. Issue scope excludes closing on thread settle and the 'already off' case, and the PR states it does not address them.

Full details: Out of Scope Changes check

Explanation

All changed code maps to #48424. The DeviceService.ts change adds the unclaimed-device shutdown path and the other-thread error. The return value changes from closing to shuttingDown so the shutdown loop covers the new device. The handlers.ts change selects the host through locateDevice for the same case. The DeviceService.test.ts change adds regression tests for the repro and the error path. No unrelated files or behavior changes appear in the reviewed diff for these two source files and the test file. The raw summary lists only these files. The diff of the test file was not inspected directly, so its content rests on the summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between e915e31 and d275eff.

Files selected for processing: 3

  • apps/server/src/device/DeviceService.test.ts
  • apps/server/src/device/DeviceService.ts
  • apps/server/src/mcp/toolkits/device/handlers.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

@juliusmarminge

Copy link
Copy Markdown
Member Author

The automated pipeline stopped on this pull request and will not merge it:

the fix is written and reviewed, but proving it needs something no worker has:

  • Permanent blocker: this issue's required reproduction depends on a real Android device and the shared Simlock gateway; the scenario runner cannot cover Android, and the manual real-app capture was not completed in this run.

It stays open for a person to finish or close.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: device_close with shutdown: true after an earlier shutdown: false close does not release the Simlock lease

1 participant