C-ABI bindings over agent-desktop's PlatformAdapter. Consumers (Python ctypes, Swift, Node ffi-napi, Go cgo, C++, Ruby fiddle) link libagent_desktop_ffi.{dylib,so,dll} and call `ad_*` functions directly instead of spawning the CLI binary per call. The canonical observe-act workflow is: ad_init → ad_adapter_create[_with_session] → ad_snapshot → read qualified refs (@s8f3k2p9:e5) → ad_execute_by_ref → ad_free_string → ad_adapter_destroy.
---
name: agent-desktop-ffi
version: 0.11.1
tags: ffi, c-bindings, cdylib, python, swift, node, go, rust-ffi
requirements:
- agent-desktop-ffi
description: >
C-ABI bindings over agent-desktop's PlatformAdapter. Consumers
(Python ctypes, Swift, Node ffi-napi, Go cgo, C++, Ruby fiddle)
link libagent_desktop_ffi.{dylib,so,dll} and call `ad_*` functions
directly instead of spawning the CLI binary per call. The canonical
observe-act workflow is: ad_init → ad_adapter_create[_with_session]
→ ad_snapshot → read qualified refs (@s8f3k2p9:e5) → ad_execute_by_ref → ad_free_string
→ ad_adapter_destroy.
---
# agent-desktop-ffi
Direct C-ABI access to PlatformAdapter operations. Build the cdylib with the
`release-ffi` profile:
```sh
cargo build --profile release-ffi -p agent-desktop-ffi
```
The output is `target/release-ffi/libagent_desktop_ffi.dylib` (`.so` on Linux,
`agent_desktop_ffi.dll` on Windows) plus a committed C header at
`crates/ffi/include/agent_desktop.h`. The default `release` profile uses
`panic = "abort"`: a Rust panic inside an `extern "C"` function would `SIGABRT`
the host. `release-ffi` keeps `panic = "unwind"`, which the panic traps need.
Four reference topics, loaded as needed:
- [ownership.md](references/ownership.md): who allocates and who frees every
pointer the FFI hands back.
- [error-handling.md](references/error-handling.md): errno-style last-error
contract, error codes, enum validation, panic boundary.
- [threading.md](references/threading.md): host-thread contract, mutation
ordering across threads and processes, adapter-bound native handles,
AXIsProcessTrusted inheritance.
- [build-and-link.md](references/build-and-link.md): prebuilt archives, ABI
handshake, struct size validation, C and Python examples.
## Observe-act workflow (canonical path)
```
ad_init(AD_ABI_VERSION_MAJOR) // verify header ↔ dylib match
adapter = ad_adapter_create_with_session("s1") // or ad_adapter_create()
rc = ad_snapshot(adapter, "Finder", 0, 10, false, false, &json_out)
// parse json_out: locate snapshot-qualified refs in data.tree
ad_free_string(json_out)
// build action:
AdAction act = {0}; act.kind = AD_ACTION_KIND_CLICK;
rc = ad_execute_by_ref(adapter, "@s8f3k2p9:e5", &act, 0, &result_out)
ad_free_string(result_out)
ad_adapter_destroy(adapter)
```
`ad_snapshot` returns the CLI envelope `{version, ok, command, data}`.
`data.tree` carries snapshot-qualified refs for interactive elements. Read
`data.complete` before trusting a tree: `false` means the observation budget ran
out and the tree is partial, not an error. `ad_snapshot` always targets the
focused window of `app`; window targeting, skeleton mode and `--root` drill-down
are not exposed over the ABI. A bare `@eN` ref returns
`AD_RESULT_ERR_INVALID_ARGS`.
## Core constraints
- **ABI handshake.** Call `ad_init(AD_ABI_VERSION_MAJOR)` once after loading the
library. A mismatch returns `AD_RESULT_ERR_INVALID_ARGS`: abort. `ad_abi_version()`
returns the library's major for display. The major increments on a removed
symbol or a layout change; new `ad_*` symbols and error codes are additive.
Before 1.0, pin the exact `libagent_desktop_ffi` version you link against.
- **Session adapters.** `ad_adapter_create_with_session("session-id")` scopes
refmap persistence to a session namespace, like CLI `--session <id>`. IDs are 1
to 64 ASCII alphanumeric, `-`, or `_` characters. A null ID behaves like
`ad_adapter_create()`. An invalid ID returns null (read `ad_last_error_*`).
- **Ref actions.** `ad_execute_by_ref` is the primary action entrypoint. It loads
the RefStore, resolves the ref strictly (`STALE_REF` when the snapshot has no
such ref or the element changed, `AMBIGUOUS_TARGET` on several candidates), runs
the live actionability preflight (visible, stable, enabled, supported action,
policy, editable), then dispatches. A disabled or unsupported target fails
before any platform call; on `AD_RESULT_ERR_ACTION_FAILED` the check report is
JSON in `ad_last_error_details()`. It auto-waits 5000 ms before the preflight;
`ad_execute_by_ref_timeout` takes `timeout_ms` (`-1` = 5000 ms default, `0` =
single-shot). `PressKey` defaults to `focus_fallback`; every other action,
including `TypeText`, defaults to `headless`. Pass `AD_POLICY_KIND_FOCUS_FALLBACK`
(1) to allow focus without cursor movement, or `AD_POLICY_KIND_HEADED` (2) to allow
physical cursor and keyboard delivery. `ad_execute_ref_action_exact_with_policy`
runs the same preflight from an `AdExactRefEntry`.
- **Generation-safe direct APIs.** Every direct targeting function takes
process-generation evidence: `AdExactRefEntry`, `AdExactWindowInfo`,
`ad_list_windows_exact`, `ad_launch_app_exact`, and the other `*_exact`
symbols. `AdRefEntry`, `AdWindowInfo`, and `AdSurfaceInfo` exist
only as fields embedded in the exact structs. `ad_list_surfaces_exact` keeps the
surface `id`. From `AD_EXACT_SURFACE_INFO_VERSION` 2, `AdExactSurfaceInfo.unclassified`
names the surface kinds its probe could not read (for example `sheet,menu`), or
is null. A missing `sheet` or `menu` next to a listed kind is unknown, not
absent. Only the Windows adapter fills it; macOS and Linux return null.
- **Native handles.** `ad_resolve_element_exact` and `ad_find_exact` return an
`AdNativeHandle`. `ad_execute_action` (headless, no preflight) and
`ad_execute_action_with_policy` act on it directly, bypassing the ref-action
pipeline. Release every handle with `ad_free_handle(adapter, &handle)` on the
same adapter and thread, before `ad_adapter_destroy`, on every platform. It
zeroes `handle.ptr`, so a second call is a no-op.
- **Action results.** `AdActionResult.steps` mirrors the CLI `steps` array for
activation-chain actions. Each `AdActionStep` has `label` and `outcome` strings,
`mechanism` (`AD_STEP_MECHANISM_SEMANTIC_API` = 1 or
`AD_STEP_MECHANISM_PHYSICAL_SYNTHETIC` = 2, valid only when `has_mechanism`),
and `verified` (valid only when `has_verified`). `AdActionResult.disposition`
carries the same delivery/retry pair as `ad_last_error_delivery_semantics`.
Release the result with `ad_free_action_result(&out)`.
- **Display discovery.** Call `ad_list_displays` before using
`AdScreenshotTarget.screen_index`. List order is the screenshot index order.
Each `AdDisplayInfo` carries a stable display ID, bounds, primary flag, and scale.
Release the list with `ad_display_list_free`.
- **Errors.** A failing call sets thread-local last-error state; read it with the
`ad_last_error_*` accessors before the next failing call. `ad_last_error_details()`
returns structured JSON (the actionability report on `ACTION_FAILED`, candidate
summaries on `AMBIGUOUS_TARGET`) that can contain element names, values, and
window titles from the user's screen: keep it out of shared logs.
`ad_last_error_delivery_semantics` reports whether the failed action was
delivered and whether a retry is safe. Invalid enum discriminants return
`AD_RESULT_ERR_INVALID_ARGS`. See [error-handling.md](references/error-handling.md).
- **Threading.** Entrypoints may be called from any host thread. Native handles
stay on the adapter and thread that created them; pass qualified refs across
threads. Desktop mutations take the same cross-process interaction lease as the
CLI; reads take no lease and carry finite deadlines. See
[threading.md](references/threading.md) for the read/read, read/mutation, and
mutation/mutation matrix.
- **Tracing.** File tracing (same JSONL contract as CLI `--trace`; segments carry
`event`, `ts_ms`, `seq`, and redacted fields) is active only when the session has
a manifest with `trace: on`, created by `session start` (or an equivalent manifest
on disk) before the adapter is created. `ad_adapter_create_with_session` alone
writes nothing. Each OS process appends to one segment,
`~/.agent-desktop/sessions/<id>/trace/<pid>-<procTs>.jsonl`, and a long-lived host
reuses it. `ad_trace_show` (JSON envelope) and `ad_trace_export` (self-contained
HTML file) read the adapter session's merged timeline; `limit` `-1` = CLI default.
- **Log callback.** `ad_set_log_callback(cb)` registers one process-wide callback
for diagnostics regardless of session manifest. It receives, as JSON, the
`tracing` events emitted inside later `ad_*` calls on the same thread; the FFI
never installs or replaces the host process's global tracing subscriber. `cb`
receives an `int32_t` level (1=ERROR ... 5=TRACE) and a `const char *msg` valid
only for the duration of the call. Pass `NULL` to unregister. Sensitive field
values (password, token, text, ...) are replaced with `{"redacted":true}`. The
callback must not unwind across the C ABI: catch C++ exceptions and Rust panics
inside it, because a violation may abort the host. It may fire from several host
threads at once and briefly after a `NULL` unregister: keep the callback and
any data it captures valid for the process lifetime.
- **Wait.** `ad_wait(adapter, args, &out)` runs the CLI `wait` command. Set
`mode.pause` (`AdOptionalU64`, ms), `mode.element`, `mode.window` (waits for a
window with that title), `mode.text`, or one of `mode.surfaces.menu`,
`.menu_closed`, `.notification`; add the `predicate` group (`predicate`,
`value`, `action`, `count`) and `scope.timeout_ms` / `scope.app`. The CLI event
wait (`--event`, `--window-id`) is not exposed. Zero-initialize `AdWaitArgs` and
validate the struct size against `AD_WAIT_ARGS_SIZE` / `ad_wait_args_size()`
before calling. The output is a `{version, ok, command, data}` envelope freed with
`ad_free_string`. `ad_wait` blocks the calling thread up to `scope.timeout_ms` ms. A
concurrent `ad_adapter_destroy` does not invalidate an in-flight wait.
- **Text input privacy.** On macOS, focus-fallback or headed text insertion may
briefly use the clipboard for non-ASCII text. For sensitive text, prefer
`AD_ACTION_KIND_SET_VALUE` with `AD_POLICY_KIND_HEADLESS` when the target
supports settable values.
- **`ad_get_tree_exact` vs `ad_snapshot`.** `ad_get_tree_exact` returns a raw flat
breadth-first tree: no refs, no refmap persistence, no JSON envelope (free it
with `ad_free_tree`). Use it for custom traversal or UI inspection. For
observe-act agents that drive actions via `ad_execute_by_ref`, start with
`ad_snapshot`.
don't have the plugin yet? install it then click "run inline in claude" again.
restructured into 6 required components with explicit decision trees for session/stateless/tracing modes, added edge cases (panic profile, ABI mismatch, handle lifecycle, multi-threading), clarified all FFI-specific inputs and output memory contracts, preserved canonical observe-act workflow and all reference topics.
direct C-ABI access to every PlatformAdapter operation. build the cdylib with the workspace's release-ffi profile (panic = "unwind"), not the standard release profile. callers (Python ctypes, Swift, Node ffi-napi, Go cgo, C++, Ruby fiddle) link the dylib and call ad_* functions directly instead of spawning the CLI binary per invocation.
you use this skill when you need to embed agent-desktop operations into a long-lived host process (Python service, Swift desktop app, Node daemon, Go binary, C++ application, Ruby script) without forking. the FFI layer gives you the full observe-act workflow (snapshot, parse refs, execute actions) with subsecond startup latency and shared adapter state across calls. use this instead of shelling out to the agent-desktop CLI when you control the host process and can manage C memory semantics.
libagent_desktop_ffi library artifact
libagent_desktop_ffi.dylib (from cargo build --profile release-ffi -p agent-desktop-ffi)libagent_desktop_ffi.solibagent_desktop_ffi.dlltarget/release-ffi/libagent_desktop_ffi.*C header
crates/ffi/include/agent_desktop.h (committed to repo)ad_* function signatures, AdAction, AdActionResult, error codes, enum discriminants, ABI version constanthost language bindings
ctypes (standard library; see tests/ffi-python/smoke.py for worked example)ffi-napi or node-fficgo#include and linkfiddle (standard library)session context (optional)
- / _ad_snapshot calls, or want structured trace output to diskad_adapter_create_with_session("id") instead of ad_adapter_create()~/.agent-desktop/sessions/<id>/trace/<pid>-<procTs>.jsonl only if session has trace: on in its manifest (from CLI session start or on-disk setup)process and thread context
external connections
build the dylib
cargo build --profile release-ffi -p agent-desktop-ffitarget/release-ffi/libagent_desktop_ffi.{dylib,so,dll} plus include path crates/ffi/include/agent_desktop.h--release profile (it sets panic = "abort", will SIGABRT on any Rust panic inside extern "C" fn)load the dylib and bind functions in your host language
ctypes.CDLL(dylib_path) and wrap each ad_* signature@_silgen_name or equivalent FFI bridgeffi-napi, define struct and function signatures.go files, #cgo LDFLAGS: -L... -lagent_desktop_ffi#include "agent_desktop.h", link with -lagent_desktop_ffiFiddle.dlopen(dylib_path) and Fiddle::Function.new(...)handshake: call ad_init(AD_ABI_VERSION_MAJOR) once after loading
AD_ABI_VERSION_MAJOR constant from header (currently 2)ad_* callAD_RESULT_ERR_INVALID_ARGS, the dylib major version does not match; abort and rebuildad_abi_version() to read the raw dylib major for displayAD_RESULT_OK on success, AD_RESULT_ERR_INVALID_ARGS on version mismatchcreate an adapter
ad_adapter_create_with_session("session-id") if you need refmap persistence or tracingad_adapter_create() for a one-off stateless adapterad_last_error_code() / ad_last_error_message() to diagnose (invalid session ID format, internal setup failure)AdAdapter * handle (non-null on success)snapshot: call ad_snapshot(adapter, app_name, x, y, width, height, &json_out)
ad_snapshot(adapter, "Finder", 0, 0, 0, 0, &json_out) (0,0,0,0 means capture full screen)AD_RESULT_OK on success; other codes indicate capture or parsing failuread_last_error_*() functions, the JSON envelope in json_out may still be populated with partial data and error detailsjson_out pointer to JSON string in C-allocated memory; contains {version, ok, command, data} envelope with data.tree field carrying snapshot-qualified ref IDs like @s8f3k2p9:e5parse the snapshot JSON and locate interactive elements
json_out string from step 5data.tree array@ qualified ref (e.g. @s8f3k2p9:e5)@<snapshot-id>:<element-id>build an action struct
AdAction struct, zero-initialize it: AdAction act = {0};act.kind = AD_ACTION_KIND_CLICK; (or other kind)act.text or act.key; these default to focus_fallback policy (matching CLI behavior)headless; pass AD_POLICY_KIND_HEADED (2) to opt in to cursor-based fallbacksAdAction structexecute the action
ad_execute_by_ref(adapter, "@s8f3k2p9:e5", &act, 0, &result_out)AD_RESULT_OK on successAD_RESULT_ERR_ACTION_FAILED: call ad_last_error_details() to get structured JSON report of which preflight check failedresult_out pointer to JSON string in C-allocated memory; contains action result steps arrayrelease strings and handles
json_out from step 5, result_out from step 8, any handles from direct targeting calls (e.g. ad_resolve_element_exact)ad_free_string(json_out) to release snapshot JSONad_free_string(result_out) to release action result JSONad_free_handle(adapter, &handle) for any native handles before destroying the adapter (balances CFRetain on macOS; no-op on Windows/Linux but safe to issue)destroy the adapter
ad_adapter_destroy(adapter)AD_RESULT_ERR_INVALID_ARGSif you need refmap persistence across snapshots in the same session, call ad_adapter_create_with_session("id"); else call ad_adapter_create(). session-bound adapters preserve ref-to-element mappings across multiple snapshots, allowing you to cache qualified refs longer. stateless adapters start fresh each time.
if the session manifest has trace: on (from CLI session start or on-disk setup), structured JSONL trace files are written to ~/.agent-desktop/sessions/<id>/trace/<pid>-<procTs>.jsonl; else no trace files are written. plain session-id adapters do not create trace files on their own; you must pre-configure the session via CLI or on-disk manifest. for unstructured diagnostics regardless of session setup, call ad_set_log_callback(cb) to install a tracing layer (see below).
if you have a live AdNativeHandle from ad_resolve_element_exact or ad_find_exact, you can bypass the ref-action pipeline and call ad_execute_action or ad_execute_action_with_policy directly; else use ad_execute_by_ref for the full preflight. low-level action paths skip element re-id and actionability checks; use them only when you already hold a validated handle and want to skip preflight overhead.
if the host process is long-lived and you want unstructured diagnostic logs, call ad_set_log_callback(cb) once to install a tracing subscriber layer; else skip this. the callback receives level (1=ERROR … 5=TRACE) and JSON message for the duration of the call. if a foreign global subscriber already owns the process, the install fails with AD_RESULT_ERR_INTERNAL and no events are delivered. pass NULL to unregister, but the callback may fire briefly afterward so keep it valid for process lifetime.
if you need to inspect actionability on failure, call ad_last_error_details() after an AD_RESULT_ERR_ACTION_FAILED return; else just check ad_last_error_message() for a human-readable string. the details JSON contains the full preflight report (visibility, stability, enablement, policy, editability) and element summaries, treated as sensitive diagnostics.
if TypeText or PressKey action must avoid clipboard use (sensitive input), use AD_ACTION_KIND_SET_VALUE with AD_POLICY_KIND_HEADLESS; else the default focus_fallback policy on macOS may briefly use the clipboard for non-ASCII text.
if you need raw flat tree traversal for custom inspection, call ad_get_tree_exact instead of ad_snapshot; else use ad_snapshot for observe-act loops. ad_get_tree_exact returns a flat BFS tree without @e refs or JSON envelope, no refmap, no pagination; use it only for direct tree inspection, not for driving actions.
if a call returns NULL or an error code, always check ad_last_error_code() and ad_last_error_message() on the same thread. last-error pointers remain valid across any number of subsequent successful calls; only the next failing call rotates them. cache the pointer once, read it as many times as you need.
if the host is multi-threaded and you call adapter entrypoints from multiple threads, native handles remain bound to the adapter and creating thread. desktop mutations acquire the canonical cross-process interaction lease (same as CLI); reads have finite deadlines without the lock. do not destroy the adapter from another thread while a blocking call like ad_wait is running.
snapshot output (ad_snapshot)
{version, ok, command, data}data.tree field: array of elements, each with @<snapshot-id>:<element-id> qualified refad_free_string(json_out) by callerok fieldaction result output (ad_execute_by_ref, ad_execute_action*)
{version, ok, command, data}data.steps field: array of action result steps with label and outcome stringsad_free_action_result(&out) or ad_free_string(result_out) by callerad_last_error_details()error state (all failing calls)
ad_last_error_code() returns AD_RESULT_* enum; ad_last_error_message() returns human-readable string; ad_last_error_details() returns structured JSON report (actionability checks, ambiguous candidates, etc.)handle release
ad_resolve_element_exact, ad_find_exact, ad_list_windows_exact, ad_launch_app_exact, ad_list_displays, ad_get_tree_exact must be released with the corresponding free function (e.g. ad_free_handle(adapter, &handle), ad_display_list_free(handles))CFRetain; on Windows/Linux: safe no-opyou know the skill worked when:
dylib loads and handshake succeeds: ad_init(AD_ABI_VERSION_MAJOR) returns AD_RESULT_OK and does not abort with version mismatch.
adapter is created: ad_adapter_create_with_session("my-session") or ad_adapter_create() returns a non-null pointer without triggering ad_last_error_*.
snapshot captures elements: ad_snapshot(adapter, "Finder", 0, 0, 0, 0, &json_out) returns AD_RESULT_OK, json_out is non-null, and parsing the JSON yields a non-empty data.tree array with at least one element carrying a @ qualified ref.
action dispatches cleanly: ad_execute_by_ref(adapter, "@s8f3k2p9:e5", &act, 0, &result_out) returns AD_RESULT_OK, result_out is non-null, and the action reaches the target (e.g. window focus changes, text appears, button press registers).
memory is released correctly: all calls to ad_free_string, ad_free_handle, and ad_adapter_destroy complete without leaking or segfaulting; the host process remains stable across multiple observe-act loops.
error handling is robust: on any failure, ad_last_error_message() returns a descriptive string (e.g. "invalid session ID format", "element is not visible", "action not supported on target"), and the caller can log or retry intelligently.