Skip to content

feat(android): support observe-only snapshots - #4647

Merged
thymikee merged 4 commits into
callstack:0.22from
hannojg:hannojg/android-observe-only-snapshot
Nov 23, 2026
Merged

thymikee merged 4 commits into
callstack:0.22from
hannojg:hannojg/android-observe-only-snapshot

Conversation

@hannojg

@hannojg hannojg commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

An IDE panel's Android snapshot can stop the agent's helper. Separate uiautomator dump calls compete for Android's sole UiAutomation connection.

Android ADB --observe-only returns geometry and preserves the agent's refs, baseline, journal and idle timer. The dispatcher supplies lifetime-scoped authority and exact claim coverage. All captures release only helpers they start.

This uses 0.22's ADR 0040 interfaces. #4729 already landed the Java disconnect fix. Fixes #4646.

Before / after

agent-device snapshot --platform android --serial emulator-5556 --observe-only --json

Base 58c76a620 returns UNSUPPORTED_OPERATION. Head 4afee81b1 returns 37 nodes and a 1080×2400 viewport without refs. The standalone read leaves an empty session list and no helper process.

Validation

Tested 4afee81b1 on Android 16/API 36 with helper 0.22.0-dev/API 2.

Observation preserved PID 7021, and the original pinned @e11~s758323 resolved exactly and opened Network & internet.

Cancelling the CLI after the helper request flushed, before any response bytes, preserved that PID. The next agent snapshot returned 29 nodes with helperSessionReused=true.

pnpm check:affected --run --base 58c76a620 --head 4afee81b1 is blocked by three help-size failures reproduced on base (22,789 tests passed). All 319 provider tests, smoke and documentation checks pass. CI pending.

Selected verification output
[
  {
    "message": "standalone",
    "nodes": 37,
    "viewport": {
      "width": 1080,
      "height": 2400
    },
    "sessions": [],
    "helperPid": null
  },
  {
    "message": "active",
    "pidBefore": "7021",
    "pidAfter": "7021",
    "pinnedRef": "@e11~s758323",
    "pressResolution": {
      "source": "ref",
      "phase": "pre-action",
      "kind": "exact"
    }
  },
  {
    "message": "cancelled",
    "responseBytes": 0,
    "socketClosed": true,
    "pidBeforeAbort": "7021",
    "pidAfterAbort": "7021"
  }
]

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

View guided diff

@thymikee

Copy link
Copy Markdown
Member

This is not ready yet: the live Android evidence for f8d58eb is missing. This change touches the helper Java, helper session borrowing and daemon routing, so it needs device output. The body describes a live Android 14 run on a506b7f in prose only, and that commit is not in this branch, so it is unclear whether the delta to f8d58eb is help-only.

Please attach output from f8d58eb, or a diff that shows a506b7f..f8d58eb is help-only, that covers three routes. First, with an agent session holding the helper, show the helper PID (for example adb shell pidof com.callstack.agentdevice.snapshothelper) and a pinned @ref before and after snapshot --platform android --serial <s> --observe-only --json, and show that the ref still resolves through an action afterwards. Second, cancel the same read mid-capture and show the helper PID unchanged, then show the next agent snapshot reporting helperSessionReused true. Third, show a standalone read with no session that returns nodes and viewport, an empty session list, and no helper process left afterwards.

Repo Guards & Compatibility fails at "Check for production-unused exports" because the re-export of captureAndroidSnapshotWithHelper in snapshot-helper.ts:2 is now unused. Moving captureAndroidUiHierarchyFromHelper into snapshot-helper-runtime.ts removed its last production consumer, so this failure comes from this PR. There are no conflicts.

Not blocking, and you can take or leave these: the cancellation test at snapshot.test.ts:436 uses a fake helper that never exits on client hang-up, so the Java fix has no automated coverage and the live run should back that claim, and it would help to say which new tests fail on base; the three daemon sites that classify an Android observe read use different predicates (request-execution-scope.ts:505, session-routing.ts and snapshot-runtime-binding.ts), so one typed passive-read check would keep them aligned; snapshot --serial <androidSerial> --observe-only without --platform can route to an occupied default session and get refused, so routing on the serial alone (or sessionless for an unowned serial) would avoid that at session-routing.ts:70; the observeAndroid bypass of withSessionlessRunnerCleanup at snapshot-command-runtime.ts:136 is a no-op for Android and forces a large re-indent; a borrowed capture failure at snapshot.ts:530 rethrows the raw transport error, and the UNSUPPORTED_OPERATION refusal in snapshot-runtime-binding.ts has no details.reason like its INVALID_ARGS sibling.

Simplicity question: the transient reader mode already means "a reader that does not own the helper" (snapshot.ts:316), so could borrowedSession fold into one helper-ownership policy derived once from observeOnly or transient, which decides install, release, retire-on-content and retire-on-session-failure? On the daemon side, could the passive Android read be classified once on the resolved snapshot route, with an unowned-serial read going sessionless instead of using a synthetic session address? Nothing else needs to change first.

To move forward, remove the unused re-export so Repo Guards passes, then attach the f8d58eb live output for the borrow, cancel and standalone routes.

@hannojg
hannojg marked this pull request as ready for review November 17, 2026 18:56
@thymikee

Copy link
Copy Markdown
Member

The code at 9706eee looks good, and I have no new findings. The unused re-export in snapshot-helper.ts that failed Repo Guards at f8d58eb is now removed. The live evidence in the PR body covers all three requested routes on a build of this commit and matches the fields the code reports. Raw command or JSON output would be easier to audit, but this is enough. CI is still running on this commit and needs to finish green before merge. There are no conflicts. The earlier non-blocking notes and the simplicity question are still yours to decide.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 17, 2026
@thymikee

Copy link
Copy Markdown
Member

Thanks again for this. A follow-up pass at 9706eee asked whether each added line is needed for the outcome in #4646. The problem is real. An IDE panel cannot read the screen while an agent holds the snapshot helper, and a second sessionless read stops the agent's helper. Most of that outcome is available with much less code, so I removed the ready-for-human label until the shape is smaller. My estimate is about 45 production lines instead of about 400.

  1. Make helper ownership the default, not a mode. Today only the transient read keeps a helper session it found and releases one it started (snapshot.ts:398-406). If every read that does not own the helper follows the same rule (about 3 lines), no read ever stops a helper that belongs to someone else. This fixes the second-reader bug for every caller, not only observe-only, and breaks nothing: a helper that is already running in this daemon always belongs to something else. The borrowed-session capture path then becomes unnecessary: the borrowedSession plumbing in snapshot.ts, the borrowed branch in snapshot-helper-runtime.ts, captureBorrowedAndroidSnapshotHelperSession, and the observeOnly pass-through into the platform layer.
  2. Keep --observe-only only as the opt-out of session publication and refs. That part cannot be a default, because a plain sessionless snapshot must create a session so press @eN works without open. Decide it once on the routed snapshot, in place of the three separate checks (snapshot-runtime-binding.ts:168, request-execution-scope.ts:505, session-routing.ts:50). When it applies, skip the session record, the refs and refsGeneration, and the activity stamp. MCP then skips ref-pin merging when there is no refsGeneration.
  3. Run an unowned serial sessionless. Do not route it into the agent's session or invent an <address>:snapshot:<hash> name (session-routing.ts:50-93). The reroute is what creates the need to protect the agent's baseline, refs, idle timer and MCP pins. Without it, those guards are not needed.
  4. Drop the skipped session-less cleanup step in snapshot-command-runtime.ts. That cleanup does nothing on Android, so skipping it only adds a 70-line re-indent.
  5. Land the helper Java fix on its own. The change that survives a client hanging up mid-capture is a real fix, because before it any cancelled snapshot killed the shared helper. It is small enough to go in as its own PR.

One open question: --observe-only would then mean "no foreground repair, refs still issued" on iOS and "no refs, no session" on Android. Please either make the two meanings match, or state both clearly in the help and in commands.md.

The live evidence for the outcome (helper PID unchanged, the agent's refs still valid, an empty session list) still applies. Re-running those three checks at the smaller head is enough.

@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 17, 2026
@thymikee

Copy link
Copy Markdown
Member

This now conflicts with main. Please rebase. The earlier review at 9706eee still stands for the code.

@thymikee

Copy link
Copy Markdown
Member

ADR 0040 alignment (ADR)

Decision: observe-only Android routing: Rebase the daemon slice on #4965 and remove the new raw lookups in request-execution-scope.ts and session-routing.ts from the handler path. Plan detached or admitted observation through the existing dispatcher and descriptor rule. Borrow a helper only after proof, and release only the helper the detached request started. Preserve the native Android groundwork, the ref and baseline controls, and the no-session-published control. Reconcile the scope hunk with #4945 and use the #4971 scoped port.

@hannojg
hannojg force-pushed the hannojg/android-observe-only-snapshot branch from 9706eee to 4afee81 Compare November 23, 2026 13:52
@hannojg
hannojg changed the base branch from main to 0.22 November 23, 2026 13:53
@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. The conflict from the earlier review is resolved, and the code at 4afee81 looks ready.

Not blocking, and you can take or leave these: a session-attached snapshot --observe-only returns before assertSessionSelectorMatches runs (https://github.com/callstack/agent-device/blob/4afee81/packages/daemon/src/server/request-execution-scope.ts#L460), so a mismatched --device or --platform reads the bound device instead of refusing with the INVALID_ARGS selector conflict; and skipping mergeCommandResult for snapshots without refsGeneration (https://github.com/callstack/agent-device/blob/4afee81/src/mcp/command-tools.ts#L254) leaves the clear branch in tool-ref-pins.ts unreachable for snapshot, so its comment is now stale.

Only 3 checks ran at this head, and Body rules was cancelled. That check validates the PR body and this diff touches no body-rule code, so the cancellation is likely unrelated. Unit, typecheck and help-size results do not exist at this head. Not verified here: the live Android evidence in the PR body, the new tests, and the reported help-size failures on base.

Before merge, CI needs to run green at 4afee81 on the 0.22 base.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@thymikee
thymikee force-pushed the hannojg/android-observe-only-snapshot branch from 4afee81 to 545c716 Compare November 23, 2026 14:45
@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@thymikee

Copy link
Copy Markdown
Member

Thanks for the rebase.

The code at 545c716 looks good, and no code findings remain from the earlier review at 4afee81 (#4647 (comment)). The rebase changed only daemon scheduling, not the device path, so the live Android evidence in the PR body still counts.

The Repo Guards check fails because the Fallow audit reports two complexity findings in functions this PR touches in request-execution-scope.ts. runAdmitted (cyclomatic 23) has the two new observation early returns. lockAndAdmit (cognitive 19) has the two nested detachedObservationDevice ternaries. A small fix is to move the detached plan and wait choice into one helper, for example planDetachedObservationLock, and to move runObservation out of runAdmitted.

The Coverage check also fails. Its log is cut off before the failing step, so the cause is unclear. Could a coverage threshold or CRAP regression on the touched daemon files explain it?

Not verified here: the new and rebased tests were not run locally at this head. Before merge, the Fallow audit must pass and the Coverage failure must be explained or fixed. No conflicts were reported.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@thymikee

Copy link
Copy Markdown
Member

Rebased onto 0.22 after #5000, with detached snapshots taking the existing session/device lock pair inside admission and skipping the addressed-session lookup, expiry settlement, and open-wait. The follow-up reuses lifetime authority, exact claim matching, the live helper, and snapshot output projection, and folds duplicate tests into request-path checks for helper survival, ref preservation, and cancellation (production +356/−97 → +350/−110, tests +816/−1 → +735/−1). All 321 targeted tests passed on 376fb28e5a; full local Coverage reproduced nine failures from the unchanged base, and a separate plugin packaging timeout passed on a focused rerun.

@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. The changes since the earlier review (545c716) look good at 376fb28: observations stay read-only, the helper stays bounded, and the review found no new problems. The 15 checks at this head all pass, and there are no conflicts. Not verified here: a live Android run at this head, since the run in the PR body is at 4afee81 and the delta is a behavior-preserving refactor by trace. Not verified here: the new tests were not run locally, and the new test that a refused detached observation never reads the public session address was not mutated to confirm it fails without the guard at request-router.ts:365. Nothing else is needed before merge.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@thymikee
thymikee merged commit 92e6ea5 into callstack:0.22 Nov 23, 2026
15 checks passed
thymikee added a commit that referenced this pull request Nov 24, 2026
* feat(android): admit detached snapshot observations

* fix(android): preserve host-global claims during observation

* chore(gates): declare observation error reasons and colocate tests

* refactor(android): reuse snapshot observation owners

---------

Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support observe-only snapshots on Android

2 participants