cua icon indicating copy to clipboard operation
cua copied to clipboard

[Feature]: Add a mockable TypeSafe Jev decision-loop example for Cua Driver

Open f-trycua opened this issue 2 months ago • 6 comments

Primary area

MCP or agent integration

User problem

Developers can connect general-purpose agents to Cua Driver, but there is no small, runnable example showing how a fast typed decision model can choose among snapshot-bound Cua actions. Existing experiments either own their platform capture and input stack or require live provider access, which makes the integration difficult to evaluate and reproduce.

Desired outcome

Add a self-contained Python and TypeScript example that:

  • observes a deterministic local fixture through Cua Driver;
  • constructs executable action candidates from the current state;
  • asks a mockable Jev-compatible chooser for one action;
  • executes through Cua Driver and verifies the result independently;
  • supports dry-run and replay without provider credentials; and
  • can switch to the official TypeSafe SDK when an API key is available.

Acceptance evidence is a credential-free end-to-end test in both languages plus focused unit and type checks. The example must preserve Driver lifecycle, action-result, and postcondition-verification semantics.

Current workaround

Developers currently assemble their own capture, accessibility, provider, action, and verification loop, or adapt third-party experiments that bypass Cua Driver's runtime and permission model.

Affected components and platforms

This is an example and documentation change under Cua Driver. The deterministic fixture is platform-independent; live desktop verification will initially be recorded on macOS. It does not change the public SDK, CLI, MCP, permission, or wire contract.

Alternatives and prior art

  • https://github.com/awlevin/typesafe-computer-use demonstrates the decision-loop shape with macOS-specific capture and actions.
  • #3914 proposes an optional Jev policy tool inside the Driver. This request is complementary: it keeps the policy loop outside the Driver, works without a live Jev key through a mock provider, and demonstrates both Python and TypeScript integration over existing contracts.

Implementation ideas

Create libs/cua-driver/examples/typesafe-jev/ with a shared fixture and equivalent Python and TypeScript runners. Keep a narrow chooser interface with deterministic mock and live TypeSafe implementations. Prefer persistent MCP for the standalone route, document the native SDK embedding route, and keep OCR/OmniParser as an optional follow-up so the base example stays credential-free and lightweight.

Submission checks

  • [x] I searched for existing issues, RFCs, and active pull requests.
  • [x] I reviewed the RFC criteria and used the RFC form instead when this requires a public contract or architectural decision.
  • [x] This public request contains no credentials, private data, sensitive screenshots, or vulnerability details.

f-trycua avatar Sep 16 '26 22:09 f-trycua

Following the maintainer’s request to make the Jev setup reproducible by an agent with no conversation context, I am contributing to the existing draft #3916 (same branch), not starting a competing implementation. Scope: turn the public /docs guide into an executable macOS-first walkthrough, make human credential/permission prerequisites explicit, and run an isolated fresh-agent rehearsal using only that guide. Acceptance: independent mock and live fixture postconditions, focused tests, docs hygiene/links/build, and a recorded distinction between pre-provisioned host prerequisites and steps actually reproduced. Existing example authorship and acknowledgments will be preserved.

injaneity avatar Sep 17 '26 00:09 injaneity

One small follow-up looks useful before changing the loop further: make the example's existing timing phase-correct.

On current main, both runners start decision_ms before:

  1. get_browser_state;
  2. optional get_window_state + parse_visual_regions;
  3. build_candidates; and
  4. the Jev/mock provider call.

So decision_ms currently measures “observe + ground + build + decide”, which makes it hard to tell whether a faster/slower run came from Driver observation, perception, local deterministic work, or the decision backend.

I kept the implementation additive: legacy decision_ms remains unchanged, while step events gain semantic_observe_ms, visual_observe_ms, candidate_build_ms, provider_decision_ms, and total_step_ms; existing action_ms stays unchanged. No new Driver/provider calls are introduced.

I deliberately left verify_ms out of this first slice because submit verification happens after the current step event; folding it in would change log/history ownership instead of merely timing an existing boundary.

Draft implementation: #4052. Acceptance still includes Python/TypeScript field parity, unchanged mock outcome, and a warm run showing the named decision spans explain >90% of decision_ms with any residual stated explicitly.

kvnloo avatar Sep 20 '26 08:09 kvnloo

Working the AX side of this rather than the DOM path the landed example takes — a small Cua Driver + Jev loop over get_window_state structured elements on macOS. One result looks worth reporting here before #3961's goal_achieved / needs_reobserve gates settle, because it is a property of how the observation is built, not of the model.

Deriving the model's view from the action space hides the evidence those gates depend on.

It is natural to build the element list you send to Jev from the same projection that builds the candidate table — both want "the interactive elements". But a result is usually displayed on something you cannot press, and a sheet announces itself with a container that exposes no action. Filtering to actionable elements removes exactly what a goal_achieved gate needs to read.

Measured on a Calculator-shaped window whose tree contains AXStaticText "42". jev-latest, mean of 3 runs:

element list sent to Jev recent actions supplied goal_satisfied
actionable elements only 4 clicks 0.31
+ non-actionable labelled text none 0.54
+ non-actionable labelled text 4 clicks 0.88

Same omission, second symptom. A window whose tree contains an AXSheet with two buttons inside it, asked whether a modal is blocking:

element list sent to Jev modal_blocking
actionable elements only 0.55
+ non-actionable labelled text 0.94

The AXSheet row is not actionable, so it never reached the model, and the two buttons inside it are ambiguous on their own. The signal was fine; the view was not.

Two things follow that may be worth building into the example:

  1. Keep two projections of one snapshot — a read view for the model, an action space for the candidate table. Narrowing the candidate table is a safety property and should stay narrow. Narrowing what the model can see is an accuracy regression with no safety benefit, since Jev returns an ID from the candidate table either way.

  2. A goal_achieved gate wants the action history alongside the state. 0.54 against 0.88 on an identical view is the difference between "the screen shows 42" and "the screen shows 42 and these four clicks produced it". A gate reading current state alone cannot separate a fresh result from a stale one. The 0.54 row is also the least stable of the three (0.41–0.63 across runs) — which is the behaviour you would want, since it is the genuinely ambiguous case.

Caveat on the evidence: these are hand-written snapshots in get_window_state's structured shape, not live captures. This machine's TCC grant is still pending, so the numbers isolate the observation-construction effect and are not end-to-end timings. Happy to re-run against live macOS captures, or against the example's own fixture shape if that is more useful.

HuntercodeT avatar Sep 21 '26 01:09 HuntercodeT

Follow-up with live macOS evidence, since my earlier numbers came from hand-written fixtures. TCC is granted here now. Two things: the loop works end to end, and my Calculator example was wrong in a way that turns out to be more interesting than the original point.

Environment: macOS 26.5.1 (25F80), Apple silicon, cua-driver 0.28.2, daemon started via open -n -g -a CuaDriver --args serve, permission mode standard.

The loop works, and background delivery is real

Driving 计算器 (Calculator) through get_window_state → click(element_token=…) for 6, ×, 7, =: all four landed, the screenshot shows 6×7 and 42, and the app never came to the foreground. Each click returned {"delivery": {"mode": "background"}, "effect": "unverifiable", "route": "accessibility"}. invoke_menu correctly reported "mode": "foreground" for the menu path. No complaints — this part behaved exactly as documented.

The correction: structuredContent.elements drops the text that tree_markdown keeps

My fixture assumed the result would arrive as an AXStaticText element. On real macOS Calculator it does not arrive at all — but not because the app hides it. One unfiltered snapshot of that exact window, right after computing, with the history sidebar open:

surface AXStaticText carrying the result
tree_markdown 6 lines: AXStaticText = "‎6‎×‎7" and AXStaticText = "‎42", three pairs (display + two history rows)
structuredContent.elements 0

Also in the same snapshot: 0 of the 148 structured entries carry a non-null value, and the markdown has exactly 148 lines bearing an [N] index — the same count as the structured array.

So the rule appears to be that the structured array is precisely the indexed (actionable) rows, while unindexed leaf text is rendered only into the markdown. A query: "42" projection makes this visible: it returns the containing AXRow and AXCell, both with no label and no value, while the markdown for those same rows shows the AXStaticText = "‎42" child underneath them.

This matters because the tool description names structuredContent.elements the preferred consumer surface and says the markdown only "stays available … for existing text-parsing callers". A goal_achieved gate written against the documented preferred surface is blind to the one thing that would satisfy it, and the fallback is to parse markdown — which is the surface the docs steer new consumers away from.

(Minor parsing note for anyone who does fall back: those values are wrapped in U+200E left-to-right marks — "‎42", not "42".)

What this changes about my earlier comment

The mechanism I described stands — an actionable-only projection drops the evidence a gate needs — but I had located it in my own code. On macOS at least part of it happens inside the snapshot, before any caller projects anything. Two consequences for #3961's gates:

  1. A read view cannot be recovered from elements alone on surfaces like this. Either the structured side needs to carry unindexed text (a text field on the containing row, or unindexed entries with element_index: null), or the recipe has to document that goal_achieved reads tree_markdown.

  2. Until then a gate needs "no evidence available" as an outcome distinct from "not satisfied". Reading the structured side here yields a confident negative forever, and the loop spends its whole budget re-pressing =. click already tells you as much with "effect": "unverifiable".

This also seems like direct support for #3931 — on this surface the accessibility rung genuinely cannot verify the goal, and the screenshot is the only place 42 exists for a caller following the documented path.

Happy to open this as a separate Driver bug if the structured/markdown asymmetry is better tracked on its own rather than under this issue. Raw snapshots and the screenshot are reproducible with the steps above.

HuntercodeT avatar Sep 21 '26 01:09 HuntercodeT

Correction to my comment above: the structured/markdown asymmetry I described is already tracked, and has been since well before I hit it — #2958 (display-only AXStaticText is invisible to structuredContent.elements and verify_state), with the design decision in #3903 / draft RFC #3904. The elements_complete: false observation I made in passing is #3815. I have moved my live evidence to #2958 and am not proposing anything separate here.

What stays relevant to this issue: whichever way #2958 resolves, the recipe's goal_achieved gate needs an outcome for "the observation cannot answer this" that is distinct from "not satisfied". On a display-only surface the preferred structured path yields a confident negative indefinitely, and click already reports "effect": "unverifiable" for the very action that succeeded. Worth pinning down in #3961's gate semantics rather than leaving it to each caller.

Apologies for not linking #2958 in the first place — my duplicate search came after the report rather than before it.

HuntercodeT avatar Sep 21 '26 02:09 HuntercodeT

Shipped. #3916 added the mockable jev-use example with Python and TypeScript agents, a deterministic fixture, a mock chooser, and live TypeSafe Jev. RFC 4268 (#4270, #4288, #4290, #4295, #4298, #4301) extended it to native accessibility candidates and a Cua-S1 provider. Guides: Use jev-use with Cua Driver and Use jev-use with native apps. Follow-ups are tracked in #4312, #4313, and #4314.

f-trycua avatar Sep 28 '26 23:09 f-trycua