graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Ship nanocua as graff's computer use on macOS
Rach Pradhan4f9031c0bc37
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex ce55d274..d3274e1b 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -254,6 +254,27 @@ jobs:# Windows build + smoke — the harness has a Win32 console backend, so verify# it compiles, passes tests, and the binary loads + runs on a real Windows# runner (the macOS/Linux jobs above can't exercise the console backend).+ nanocua:+ name: nanocua (macOS)+ runs-on: macos-latest+ timeout-minutes: 30+ steps:+ - uses: actions/checkout@v7+ - uses: ./.github/actions/setup-codegraff-zig+ - name: zig fmt --check+ working-directory: apps/nanocua+ run: zig fmt --check build.zig src+ - name: zig build test+ working-directory: apps/nanocua+ run: zig build test --summary all+ - name: Linux and Windows still build+ working-directory: apps/nanocua+ run: |+ zig build -Dtarget=aarch64-linux-gnu --prefix zig-out/linux+ zig build -Dtarget=x86_64-windows-gnu --prefix zig-out/windows+ - name: The app packages+ run: sh apps/nanocua/macos/package.sh aarch64-macos "$RUNNER_TEMP"+windows:if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.forkruns-on: windows-latestdiff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 577078cc..6b50cc15 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -113,9 +113,18 @@ jobs:- uses: actions/checkout@v7if: steps.signing.outputs.enabled == 'true'with:- sparse-checkout: scripts/release-sign-macos.sh+ sparse-checkout: |+ scripts/release-sign-macos.sh+ scripts/install-zig-ci.py+ .github/actions/setup-codegraff-zig+ apps/nanocuasparse-checkout-cone-mode: false+ # nanocua.app is built here: it needs the macOS SDK.+ - name: Install Zig+ if: steps.signing.outputs.enabled == 'true'+ uses: ./.github/actions/setup-codegraff-zig+- uses: actions/download-artifact@v7if: steps.signing.outputs.enabled == 'true'with:@@ -179,7 +188,7 @@ jobs:for target in aarch64-macos x86_64-macos; doname="graff-$target"# Same member layout as the Linux-built tarball, no AppleDouble files.- members="$(tar -tzf "signed/$name.tar.gz" | sed 's:/$::' | LC_ALL=C sort | tr '\n' ' ')"+ members="$(tar -tzf "signed/$name.tar.gz" | grep -v '/nanocua\.app/' | sed 's:/$::' | LC_ALL=C sort | tr '\n' ' ')"[ "$members" = "$name $name/LICENSE $name/README.md $name/graff " ] \|| { echo "$name: unexpected tarball members: $members" >&2; exit 1; }tar -xzf "signed/$name.tar.gz" -C "$check"@@ -187,6 +196,10 @@ jobs:info="$(codesign -dv --verbose=2 "$check/$name/graff" 2>&1)"grep -q 'Authority=Developer ID Application' <<<"$info"grep -q 'flags=.*runtime' <<<"$info"+ app="$check/$name/nanocua.app"+ codesign --verify --deep --strict "$app"+ codesign -dv --verbose=2 "$app" 2>&1 | grep -q 'Authority=Developer ID Application'+ xcrun stapler validate "$app"doneshasum -a 256 signed/*.tar.gzdiff --git a/AGENTS.md b/AGENTS.mdindex 19faa787..c8871c46 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -131,6 +131,10 @@ change. The workspace the agent runs in is not chrome.- While you work, run only what you touched: `zig build unit -Dtest-filter="<part of a test name>"` builds and runs just the unit tests (repeat the flag for several names).- `zig build test` also runs the subprocess integration scripts and takes several minutes. Run it once before you push; the pre-push hook does.+## nanocua: this repo holds the original++`apps/nanocua` is graff's computer use on macOS (ADR 0314), its own package with its own `zig build test`, pinned to a nanoui release. graff links none of it. nanoui's `apps/nanocua` is generated from this one by `scripts/vendor-nanocua.sh` and keeps this repository's license; change it here and copy it there, never the reverse.+## Tests must be reachable- `zig build test` only runs the tests in files the test root pulls in. A new module's `test {}` blocks compile to nothing until something references it, and the suite still reports green.diff --git a/apps/nanocua/README.md b/apps/nanocua/README.mdnew file mode 100644index 00000000..03a8e792--- /dev/null+++ b/apps/nanocua/README.md@@ -0,0 +1,136 @@+# nanocua++Lets AI agents see and use apps on your Mac, as far as you allow, without+taking over your screen. A small app built on+[nanoui](https://github.com/justrach/nanoui): a control panel, and an+[MCP](https://modelcontextprotocol.io) server any agent can add (Claude Code,+Codex, Cursor…). It is [codegraff](https://github.com/justrach/codegraff)'s+computer use: `graff` ships with it and offers it by default on macOS.++macOS first. Linux (X11 with XTest and AT-SPI) and Windows (UI Automation+and SendInput) come next; there the app builds and every tool says it isn't+supported yet.++## How it works++```+agent ──stdio──▶ nanocua mcp ──unix socket──▶ nanocua (the app)+ (a pipe, nothing more) the panel, the policy, the log,+ accessibility, input, screenshots+```++The app does all the work, and the agent's `nanocua mcp` only carries lines.+That matters on macOS: permissions (Accessibility, Screen Recording) belong+to the app that asks, so you grant them to nanocua once, not to each terminal+or agent that happens to start it, and you can take them back in one place.+`nanocua mcp` answers handshakes and the tool list itself, so adding it to an+agent starts nothing; the first real call opens the app, in the background.++Agents work from the **accessibility tree**, not from pixels: `get_ui_tree`+gives one line per element worth naming, with a ref to act on it, and+leaves out containers that say nothing. That's exact (no misclicks from a+guessed coordinate) and cheap (text, not images):++```+app "Notes" pid 812+[1] Window "Groceries" @0,25 1200x800+ [2] Button desc="New Note" @310,38 28x28+ [3] SearchField value="" @960,36 220x28+ [4] TextArea value="eggs\nmilk" @320,80 860x700 focused+```++Agents work **inside one app, in the background**: the app they last read or+named. `click` with a ref presses the element through accessibility when it can;+otherwise, and for keys, text and scrolling, events are posted to that app alone,+so your pointer stays where it is and your keyboard stays yours. Screenshots+capture that app's window, even behind others, with the mapping from image pixels+back to screen points. This isn't a sandbox: it's your real Mac, your apps and+your logins. And some apps ignore events they get in the background (Chromium's+web content is the likely one); refs still work there.++## Tools++| tool | does | needs |+| --- | --- | --- |+| `list_apps` | running apps with windows, which is frontmost | |+| `get_ui_tree` | an app's elements, with refs | Accessibility |+| `screenshot` | a JPEG of an app's window, scaled | Screen Recording |+| `click` | by ref (pressed) or at x,y; right, double | input on |+| `type_text` | types Unicode text, into a ref if given | input on |+| `press_key` | `cmd+s`, `shift+tab`, `enter`, `f5`… | input on |+| `scroll` | at x,y or over a ref, by lines | input on |+| `move_mouse` | hovers at x,y | input on |+| `set_value` | sets a field's value through accessibility | input on |+| `open_app` | opens an app by name, behind your work | input on |++With `whole_screen: true`, once you've allowed it, `screenshot` takes the main+display or a region of it, input uses the real pointer and keyboard, and `open_app`+brings the app forward.++## What you control++- **Stop** (or **⌃⌥⌘.** from anywhere) refuses every call, even looking,+ including calls already waiting, and stops text being typed, until you press+ Resume. An agent may be moving the pointer, which is why the keyboard shortcut+ exists.+- **Allow input** is off at launch: agents can look but not click, type or+ open apps until you turn it on.+- **Allow whole screen** is off at launch too: agents stay inside one app.+- nanocua's own window is off limits: agents don't see it in trees and can't+ click, type or set values in it, so an agent can't press its own Resume.+- **Activity** lists every call: the tool, its arguments, how long it took,+ and whether it was done, failed or refused.++Calls run one at a time on a queue of their own, so a hung app never+freezes the panel (accessibility calls give up after two seconds).++## Adding it to an agent++With `graff` there's nothing to add: on macOS its installer puts nanocua in+`~/Applications`, and `graff mcp disable nanocua` turns it off. Elsewhere, copy+the command from the panel. With Claude Code:++```sh+claude mcp add nanocua -- ~/Applications/nanocua.app/Contents/MacOS/nanocua mcp+```++Or as an MCP server over stdio:++```json+{ "mcpServers": { "nanocua": { "command": "/Users/you/Applications/nanocua.app/Contents/MacOS/nanocua", "args": ["mcp"] } } }+```++The socket is `~/Library/Application Support/nanocua/mcp.sock` (macOS) or+`$XDG_RUNTIME_DIR/nanocua.sock` (Linux), only usable by your user;+`NANOCUA_SOCKET` overrides it.++## Building++```sh+zig build # zig-out/bin/nanocua+zig build test # the tests, the panel through ui.Tester+zig build -Dtarget=aarch64-linux-gnu --prefix zig-out/linux+zig build -Dtarget=x86_64-windows-gnu --prefix zig-out/windows+sh macos/package.sh aarch64-macos /tmp # /tmp/nanocua.app+```++Nothing in the tests moves the pointer, types or captures the screen: the policy,+where calls go, MCP sessions, keys, the tree's text, the socket (over a socket+pair) and the panel are tested; the macOS backend is compiled, not run.++macOS keeps permissions across updates only for an app signed by the same+identity. Set `CODESIGN_IDENTITY` for `package.sh`; an ad hoc build is asked again+each time.++## License++codegraff's: see its `LICENSE`. nanoui's copy of this folder is generated from+codegraff's (`scripts/vendor-nanocua.sh`), and is not under nanoui's MIT license.++## Next++- A virtual Mac as a second mode: agents in a separate macOS guest, for real+ isolation, without your apps and logins.+- An icon, and a menu bar item while the window is closed.+- Asking before each action, per app or per tool, in the panel.+- Linux (X11: XTest, AT-SPI over D-Bus) and Windows (UI Automation, SendInput).diff --git a/apps/nanocua/build.zig b/apps/nanocua/build.zignew file mode 100644index 00000000..bdfdab91--- /dev/null+++ b/apps/nanocua/build.zig@@ -0,0 +1,35 @@+const std = @import("std");++pub fn build(b: *std.Build) void {+ const target = b.standardTargetOptions(.{});+ const optimize = b.standardOptimizeOption(.{});+ const nanoui = b.dependency("nanoui", .{ .target = target, .optimize = optimize }).module("nanoui");+ const mac = target.result.os.tag == .macos;++ const exe = b.addExecutable(.{ .name = "nanocua", .root_module = module(b, "src/main.zig", target, optimize, nanoui, mac) });+ b.installArtifact(exe);+ const run = b.addRunArtifact(exe);+ run.addPassthruArgs();+ b.step("run", "Run nanocua (the control panel and the agents' server)").dependOn(&run.step);++ const tests = b.addTest(.{ .root_module = module(b, "src/tests.zig", target, optimize, nanoui, mac) });+ b.step("test", "Run nanocua tests").dependOn(&b.addRunArtifact(tests).step);+ // The tests as a program, for another machine (Linux in a container).+ b.step("test-bin", "Install the tests as zig-out/bin/nanocua-tests").dependOn(&b.addInstallArtifact(tests, .{ .dest_sub_path = "nanocua-tests" }).step);+}++fn module(b: *std.Build, root: []const u8, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, nanoui: *std.Build.Module, mac: bool) *std.Build.Module {+ const mod = b.createModule(.{+ .root_source_file = b.path(root),+ .target = target,+ .optimize = optimize,+ .imports = &.{.{ .name = "nanoui", .module = nanoui }},+ });+ if (mac) {+ // The accessibility tree and events (ApplicationServices), pictures+ // of the screen (ScreenCaptureKit) and their encoding (ImageIO),+ // the global hot key that stops everything (Carbon).+ for ([_][]const u8{ "ApplicationServices", "CoreGraphics", "ImageIO", "ScreenCaptureKit", "Carbon", "AppKit" }) |f| mod.linkFramework(f, .{});+ }+ return mod;+}diff --git a/apps/nanocua/build.zig.zon b/apps/nanocua/build.zig.zonnew file mode 100644index 00000000..94cf06ed--- /dev/null+++ b/apps/nanocua/build.zig.zon@@ -0,0 +1,13 @@+.{+ .name = .nanocua,+ .version = "0.1.0",+ .fingerprint = 0xe266b3446ce40614,+ .minimum_zig_version = "0.17.0",+ .dependencies = .{+ .nanoui = .{+ .url = "git+https://github.com/justrach/nanoui#8e2c690acf363f7fbd0393ef84a6231ecfc12dfb",+ .hash = "nanoui-0.8.0-we0KMefsGwAnjNVY0I55quWIL4o3W1ykGfzMKDGtMu0k",+ },+ },+ .paths = .{ "build.zig", "build.zig.zon", "src" },+}diff --git a/apps/nanocua/macos/package.sh b/apps/nanocua/macos/package.shnew file mode 100755index 00000000..b3b07676--- /dev/null+++ b/apps/nanocua/macos/package.sh@@ -0,0 +1,61 @@+#!/bin/sh+# Builds nanocua.app into a folder.+#+# VERSION=0.1.0 sh apps/nanocua/macos/package.sh <aarch64-macos|x86_64-macos> <out-dir>+#+# Signs with $CODESIGN_IDENTITY (a "Developer ID Application" identity in the+# keychain) when it is set, else ad hoc. macOS keeps Accessibility and Screen+# Recording grants across updates only for the stable identity; an ad hoc build+# has to be granted again each time. Notarizing is the caller's: release builds+# notarize the app with the graff beside it (scripts/release-sign-macos.sh).+set -eu+target="$1"+out="$2"+cd "$(dirname "$0")/.."++app=nanocua+version="${VERSION:-0.1.0}"+version="${version#v}"+min_macos=13.0+case "$target" in+ aarch64-macos) cpu=apple_m1 ;;+ x86_64-macos) cpu=baseline ;;+ *) echo "unknown target: $target" >&2; exit 2 ;;+esac+prefix="zig-out/$target"+# Older than the machine building it, or it won't run on older Macs.+zig build -Doptimize=ReleaseFast "-Dtarget=$target.$min_macos" "-Dcpu=$cpu" --prefix "$prefix"+if ! vtool -show-build "$prefix/bin/$app" | grep -q "minos $min_macos\$"; then+ echo "$prefix/bin/$app is not built for macOS $min_macos" >&2+ exit 1+fi+bundle="$out/$app.app"+rm -rf "$bundle"+mkdir -p "$bundle/Contents/MacOS"+cp "$prefix/bin/$app" "$bundle/Contents/MacOS/$app"+cat > "$bundle/Contents/Info.plist" <<PLIST+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">+<plist version="1.0">+<dict>+ <key>CFBundleName</key><string>$app</string>+ <key>CFBundleDisplayName</key><string>$app</string>+ <key>CFBundleIdentifier</key><string>com.justrach.$app</string>+ <key>CFBundleExecutable</key><string>$app</string>+ <key>CFBundlePackageType</key><string>APPL</string>+ <key>CFBundleShortVersionString</key><string>$version</string>+ <key>CFBundleVersion</key><string>$version</string>+ <key>LSApplicationCategoryType</key><string>public.app-category.utilities</string>+ <key>LSMinimumSystemVersion</key><string>$min_macos</string>+ <key>NSHighResolutionCapable</key><true/>+ <key>NSPrincipalClass</key><string>NSApplication</string>+</dict>+</plist>+PLIST+xattr -cr "$bundle"+if [ -n "${CODESIGN_IDENTITY:-}" ]; then+ codesign --force --timestamp --options runtime --sign "$CODESIGN_IDENTITY" "$bundle"+else+ codesign --force --sign - "$bundle"+fi+codesign --verify --deep --strict "$bundle"diff --git a/apps/nanocua/src/app.zig b/apps/nanocua/src/app.zignew file mode 100644index 00000000..1d4f1cdb--- /dev/null+++ b/apps/nanocua/src/app.zig@@ -0,0 +1,160 @@+//! nanocua's state: the hub agents talk to, the socket it listens on, the+//! backend that runs the tools, the kill switch and its hot key, and what+//! the panel shows (permissions, the command for agents, the log).+const std = @import("std");+const builtin = @import("builtin");+const nanoui = @import("nanoui");+const mcp = @import("mcp.zig");+const server_mod = @import("server.zig");++pub const mac = builtin.os.tag == .macos;+const backend_mod = if (mac) @import("mac/backend.zig") else @import("backend_none.zig");+const HotKey = if (mac) @import("mac/hotkey.zig").HotKey else void;++pub const Perms = struct {+ accessibility: bool = false,+ screen: bool = false,+};++pub const App = struct {+ gpa: std.mem.Allocator,+ hub: mcp.Hub,+ server: server_mod.Server,+ /// The kill switch where the backend's queue reads it.+ stop_flag: ?*std.atomic.Value(bool) = null,+ /// The whole-screen switch where the backend's queue reads it.+ whole_flag: ?*std.atomic.Value(bool) = null,+ hotkey: ?HotKey = null,+ window: ?*nanoui.Window = null,+ /// Why the socket isn't open, if it isn't.+ listen_error: ?[]const u8 = null,+ socket_buf: [512]u8 = undefined,+ socket_path: []const u8 = "",+ /// What an agent's MCP settings run.+ command_buf: [1024]u8 = undefined,+ command: []const u8 = "nanocua mcp",+ /// The permissions as last read; how to read them (none in tests).+ perms: Perms = .{},+ read_perms: ?*const fn () Perms = null,+ ask: ?*const fn (which: Ask) void = null,+ /// "Copied" shows for a while after copying the command.+ copied_until: f64 = 0,+ /// Light or dark, fixed (tests); else as the system is.+ dark: ?bool = null,++ pub const Ask = enum { accessibility, screen };++ /// Sets `a` up in place: the hub writes through `a.server`.+ pub fn init(a: *App, gpa: std.mem.Allocator, backend: mcp.Backend) void {+ a.* = .{+ .gpa = gpa,+ .server = .{ .gpa = gpa, .hub = &a.hub },+ .hub = .{ .gpa = gpa, .sink = undefined, .backend = backend, .clock = nanoui.ui.clock.now },+ };+ a.hub.sink = a.server.sink();+ a.hub.changed = .{ .ptr = a, .func = changed };+ }++ pub fn deinit(a: *App) void {+ if (comptime mac) if (a.hotkey) |*h| h.deinit();+ a.server.deinit();+ }++ /// Opens the socket at `path`; on failure the panel says why.+ pub fn listen(a: *App, path: [:0]const u8) void {+ a.socket_path = std.fmt.bufPrint(&a.socket_buf, "{s}", .{path}) catch "";+ if (!server_mod.supported) {+ a.listen_error = "Agents can't connect on this system yet.";+ return;+ }+ @import("sockpath.zig").makeDir(path);+ a.server.listen(path) catch |e| {+ a.listen_error = switch (e) {+ error.AlreadyRunning => "Another nanocua is already serving agents.",+ else => "Couldn't open the socket for agents.",+ };+ };+ }++ /// The command agents run, from this program's path.+ pub fn setCommand(a: *App, exe: []const u8) void {+ a.command = std.fmt.bufPrint(&a.command_buf, "{s} mcp", .{exe}) catch a.command;+ }++ /// The kill switch, also on ⌃⌥⌘. (macOS).+ pub fn stopKey(a: *App) void {+ if (comptime !mac) return;+ a.hotkey = .{ .ctx = a, .func = stopPressed };+ if (!a.hotkey.?.register()) a.hotkey = null;+ }++ fn stopPressed(ctx: *anyopaque) void {+ const a: *App = @ptrCast(@alignCast(ctx));+ a.setStopped(true);+ }++ pub fn setStopped(a: *App, stopped: bool) void {+ a.hub.policy.stopped = stopped;+ if (a.stop_flag) |f| f.store(stopped, .release);+ a.redraw();+ }++ pub fn setInput(a: *App, on: bool) void {+ a.hub.policy.allow_input = on;+ a.redraw();+ }++ pub fn setWholeScreen(a: *App, on: bool) void {+ a.hub.policy.allow_whole_screen = on;+ if (a.whole_flag) |f| f.store(on, .release);+ a.redraw();+ }++ pub fn refreshPerms(a: *App) void {+ if (a.read_perms) |f| a.perms = f();+ }++ pub fn request(a: *App, which: Ask) void {+ if (a.ask) |f| f(which);+ }++ fn changed(ctx: *anyopaque) void {+ const a: *App = @ptrCast(@alignCast(ctx));+ a.redraw();+ }++ fn redraw(a: *App) void {+ if (a.window) |w| w.redraw();+ }+};++/// The platform's backend and permissions, for main.+pub const Platform = struct {+ impl: if (mac) backend_mod.Mac else backend_mod.None,++ pub fn init(gpa: std.mem.Allocator) Platform {+ return .{ .impl = if (mac) backend_mod.Mac.init(gpa) else .{} };+ }++ /// Wires the backend into `a` (after `a.init`).+ pub fn attach(p: *Platform, a: *App) void {+ a.hub.backend = p.impl.backend();+ if (comptime mac) {+ a.stop_flag = &p.impl.stopped;+ a.whole_flag = &p.impl.whole_screen;+ a.read_perms = readPerms;+ a.ask = ask;+ }+ }++ fn readPerms() Perms {+ return .{ .accessibility = backend_mod.accessibility(), .screen = backend_mod.screenRecording() };+ }++ fn ask(which: App.Ask) void {+ backend_mod.request(switch (which) {+ .accessibility => .accessibility,+ .screen => .screen,+ });+ }+};diff --git a/apps/nanocua/src/backend_none.zig b/apps/nanocua/src/backend_none.zignew file mode 100644index 00000000..0d633c8e--- /dev/null+++ b/apps/nanocua/src/backend_none.zig@@ -0,0 +1,13 @@+//! Where nanocua can't act yet (Linux and Windows, next): every tool says so.+const mcp = @import("mcp.zig");+const tools = @import("tools.zig");++pub const None = struct {+ pub fn backend(n: *None) mcp.Backend {+ return .{ .ptr = n, .call = call };+ }++ fn call(_: *anyopaque, _: tools.Tool, _: tools.Args, reply: *mcp.Reply) void {+ reply.fail("nanocua controls macOS so far; Linux (X11) and Windows are next.");+ }+};diff --git a/apps/nanocua/src/keys.zig b/apps/nanocua/src/keys.zignew file mode 100644index 00000000..44fe5d10--- /dev/null+++ b/apps/nanocua/src/keys.zig@@ -0,0 +1,126 @@+//! Keys as agents write them, "cmd+shift+t", "enter", "escape": modifiers+//! and one key, and that key's virtual key code on the Mac (ANSI layout).+const std = @import("std");++pub const Mods = packed struct(u8) {+ cmd: bool = false,+ shift: bool = false,+ alt: bool = false,+ ctrl: bool = false,+ @"fn": bool = false,+ _: u3 = 0,+};++pub const Combo = struct {+ mods: Mods = .{},+ /// The key's name, lowercased ("enter", "a", "f5", "[").+ key: []const u8,+};++pub const ParseError = error{ Empty, UnknownKey, UnknownModifier };++/// Splits "cmd+shift+t" into modifiers and a key. "+" alone, or last as in+/// "cmd++", is the plus key.+pub fn parse(buf: []u8, s_in: []const u8) ParseError!Combo {+ const s = std.mem.trim(u8, s_in, " ");+ if (s.len == 0) return error.Empty;+ var mods: Mods = .{};+ var rest = s;+ while (std.mem.indexOfScalar(u8, rest, '+')) |i| {+ if (i == rest.len - 1 and i == 0) break; // "+"+ if (i == 0) return error.UnknownKey;+ const m = rest[0..i];+ if (!setMod(&mods, m)) return error.UnknownModifier;+ rest = rest[i + 1 ..];+ if (rest.len == 0) return error.Empty;+ }+ if (rest.len > buf.len) return error.UnknownKey;+ const key = std.ascii.lowerString(buf, rest);+ const named = alias(key);+ if (macCode(named) == null) return error.UnknownKey;+ return .{ .mods = mods, .key = named };+}++fn setMod(m: *Mods, name_in: []const u8) bool {+ var b: [16]u8 = undefined;+ if (name_in.len > b.len) return false;+ const name = std.ascii.lowerString(&b, name_in);+ const eq = struct {+ fn f(a: []const u8, list: []const []const u8) bool {+ for (list) |x| if (std.mem.eql(u8, a, x)) return true;+ return false;+ }+ }.f;+ if (eq(name, &.{ "cmd", "command", "meta", "super", "win", "⌘" })) m.cmd = true else if (eq(name, &.{ "shift", "⇧" })) m.shift = true else if (eq(name, &.{ "alt", "opt", "option", "⌥" })) m.alt = true else if (eq(name, &.{ "ctrl", "control", "⌃" })) m.ctrl = true else if (eq(name, &.{"fn"})) m.@"fn" = true else return false;+ return true;+}++/// Other names for the same key.+fn alias(k: []const u8) []const u8 {+ const pairs = [_][2][]const u8{+ .{ "return", "enter" }, .{ "esc", "escape" }, .{ "backspace", "delete" },+ .{ "del", "forwarddelete" }, .{ "pgup", "pageup" }, .{ "pgdn", "pagedown" },+ .{ "arrowup", "up" }, .{ "arrowdown", "down" }, .{ "arrowleft", "left" },+ .{ "arrowright", "right" }, .{ "spacebar", "space" }, .{ " ", "space" },+ };+ for (pairs) |p| if (std.mem.eql(u8, k, p[0])) return p[1];+ return k;+}++/// The Mac's virtual key code for a key name (Carbon's kVK_*).+pub fn macCode(k: []const u8) ?u16 {+ if (k.len == 1) {+ const chars = "asdfhgzxcv\x00bqweryt123465=97-80]ou[ip\x00lj'k;\\,/nm.";+ if (std.mem.indexOfScalar(u8, chars, k[0])) |i| if (k[0] != 0) return @intCast(i);+ if (k[0] == '`') return 50;+ if (k[0] == '+') return 24; // with shift on US keyboards; "=" key+ return null;+ }+ const named = [_]struct { []const u8, u16 }{+ .{ "enter", 36 }, .{ "tab", 48 }, .{ "space", 49 }, .{ "delete", 51 },+ .{ "escape", 53 }, .{ "forwarddelete", 117 }, .{ "home", 115 }, .{ "end", 119 },+ .{ "pageup", 116 }, .{ "pagedown", 121 }, .{ "left", 123 }, .{ "right", 124 },+ .{ "down", 125 }, .{ "up", 126 }, .{ "f1", 122 }, .{ "f2", 120 },+ .{ "f3", 99 }, .{ "f4", 118 }, .{ "f5", 96 }, .{ "f6", 97 },+ .{ "f7", 98 }, .{ "f8", 100 }, .{ "f9", 101 }, .{ "f10", 109 },+ .{ "f11", 103 }, .{ "f12", 111 },+ };+ for (named) |n| if (std.mem.eql(u8, k, n[0])) return n[1];+ return null;+}++/// CGEventFlags for the modifiers.+pub fn macFlags(m: Mods) u64 {+ var f: u64 = 0;+ if (m.shift) f |= 0x20000;+ if (m.ctrl) f |= 0x40000;+ if (m.alt) f |= 0x80000;+ if (m.cmd) f |= 0x100000;+ if (m.@"fn") f |= 0x800000;+ return f;+}++test "combos parse: modifiers, aliases, the plus key" {+ var b: [32]u8 = undefined;+ const c = try parse(&b, "Cmd+Shift+T");+ try std.testing.expect(c.mods.cmd and c.mods.shift and !c.mods.alt);+ try std.testing.expectEqualStrings("t", c.key);+ try std.testing.expectEqual(@as(?u16, 17), macCode(c.key));+ try std.testing.expectEqualStrings("enter", (try parse(&b, "return")).key);+ try std.testing.expectEqualStrings("escape", (try parse(&b, "ctrl+esc")).key);+ try std.testing.expectEqualStrings("+", (try parse(&b, "cmd++")).key);+ try std.testing.expectEqualStrings("+", (try parse(&b, "+")).key);+ try std.testing.expectError(error.UnknownModifier, parse(&b, "hyper+a"));+ try std.testing.expectError(error.UnknownKey, parse(&b, "cmd+banana"));+ try std.testing.expectError(error.Empty, parse(&b, " "));+ try std.testing.expectEqual(@as(u64, 0x120000), macFlags((try parse(&b, "shift+cmd+left")).mods));+}++test "the Mac's key codes" {+ const expect = [_]struct { []const u8, u16 }{+ .{ "a", 0 }, .{ "s", 1 }, .{ "b", 11 }, .{ "q", 12 }, .{ "1", 18 }, .{ "5", 23 }, .{ "=", 24 },+ .{ "0", 29 }, .{ "o", 31 }, .{ "l", 37 }, .{ "'", 39 }, .{ "\\", 42 }, .{ "/", 44 }, .{ "m", 46 },+ .{ ".", 47 }, .{ "`", 50 }, .{ "up", 126 },+ };+ for (expect) |e| try std.testing.expectEqual(@as(?u16, e[1]), macCode(e[0]));+}diff --git a/apps/nanocua/src/mac/apps.zig b/apps/nanocua/src/mac/apps.zignew file mode 100644index 00000000..9d1bc51b--- /dev/null+++ b/apps/nanocua/src/mac/apps.zig@@ -0,0 +1,88 @@+//! Running apps through NSWorkspace: the ones with windows (regular+//! activation policy), finding one by name, and opening one, behind what the+//! user is doing or brought forward. Callers hold an autorelease pool.+const std = @import("std");+const objc = @import("nanoui").objc;++pub const App = struct {+ pid: i32,+ name: []const u8,+ bundle: []const u8,+ active: bool,+};++fn running() objc.Object {+ const ws = objc.msg(objc.Object, objc.class("NSWorkspace"), "sharedWorkspace", .{});+ return objc.msg(objc.Object, ws, "runningApplications", .{});+}++/// Calls `f(ctx, app)` for every regular app; strings live until the pool pops.+pub fn each(ctx: anytype, comptime f: fn (@TypeOf(ctx), App) void) void {+ const list = running();+ const n = objc.msg(usize, list, "count", .{});+ for (0..n) |i| {+ const a = objc.msg(objc.Object, list, "objectAtIndex:", .{i});+ if (objc.msg(isize, a, "activationPolicy", .{}) != 0) continue;+ f(ctx, .{+ .pid = objc.msg(i32, a, "processIdentifier", .{}),+ .name = objc.fromNSString(objc.msg(?objc.Object, a, "localizedName", .{})),+ .bundle = objc.fromNSString(objc.msg(?objc.Object, a, "bundleIdentifier", .{})),+ .active = objc.msg(bool, a, "isActive", .{}),+ });+ }+}++fn find(name: []const u8) ?objc.Object {+ const list = running();+ const n = objc.msg(usize, list, "count", .{});+ for (0..n) |i| {+ const a = objc.msg(objc.Object, list, "objectAtIndex:", .{i});+ const an = objc.fromNSString(objc.msg(?objc.Object, a, "localizedName", .{}));+ const ab = objc.fromNSString(objc.msg(?objc.Object, a, "bundleIdentifier", .{}));+ if (std.ascii.eqlIgnoreCase(an, name) or std.ascii.eqlIgnoreCase(ab, name)) return a;+ }+ return null;+}++/// The pid of the running app named `name` (or with that bundle id).+pub fn pidNamed(name: []const u8) ?i32 {+ const a = find(name) orelse return null;+ return objc.msg(i32, a, "processIdentifier", .{});+}++pub const OpenResult = enum { running, activated, launched, not_found };++/// Launches `name` from the usual folders (or by bundle id) if it isn't+/// running. Only with `forward` does it come in front of the user's work.+pub fn open(name: []const u8, forward: bool) OpenResult {+ if (find(name)) |a| {+ if (!forward) return .running;+ // NSApplicationActivateAllWindows+ _ = objc.msg(bool, a, "activateWithOptions:", .{@as(usize, 1)});+ return .activated;+ }+ const ws = objc.msg(objc.Object, objc.class("NSWorkspace"), "sharedWorkspace", .{});+ var url: ?objc.Object = null;+ if (std.mem.indexOfScalar(u8, name, '.') != null)+ url = objc.msg(?objc.Object, ws, "URLForApplicationWithBundleIdentifier:", .{objc.nsString(name)});+ if (url == null) url = byPath(name);+ const u = url orelse return .not_found;+ const config = objc.msg(objc.Object, objc.class("NSWorkspaceOpenConfiguration"), "configuration", .{});+ objc.msg(void, config, "setActivates:", .{forward});+ objc.msg(void, ws, "openApplicationAtURL:configuration:completionHandler:", .{ u, config, @as(?*anyopaque, null) });+ return .launched;+}++fn byPath(name: []const u8) ?objc.Object {+ if (name.len == 0 or name.len > 200 or std.mem.indexOfScalar(u8, name, '/') != null) return null;+ const dirs = [_][]const u8{ "/Applications/", "/System/Applications/", "/System/Applications/Utilities/", "/Applications/Utilities/" };+ const fm = objc.msg(objc.Object, objc.class("NSFileManager"), "defaultManager", .{});+ var buf: [300]u8 = undefined;+ for (dirs) |d| {+ const path = std.fmt.bufPrint(&buf, "{s}{s}.app", .{ d, name }) catch continue;+ const ns = objc.nsString(path);+ if (!objc.msg(bool, fm, "fileExistsAtPath:", .{ns})) continue;+ return objc.msg(objc.Object, objc.class("NSURL"), "fileURLWithPath:", .{ns});+ }+ return null;+}diff --git a/apps/nanocua/src/mac/ax.zig b/apps/nanocua/src/mac/ax.zignew file mode 100644index 00000000..a3e3de78--- /dev/null+++ b/apps/nanocua/src/mac/ax.zig@@ -0,0 +1,222 @@+//! An app's accessibility tree, walked with one round trip per element+//! (AXUIElementCopyMultipleAttributeValues), written as text with refs;+//! the elements behind the refs, kept until the next walk; and what can+//! be done to one: press it, focus it, set its value, find its frame.+//! Used from the backend's queue only.+const std = @import("std");+const cf = @import("cf.zig");+const tree = @import("../tree.zig");++extern "c" fn usleep(us: c_uint) c_int;++/// Elements named by the last tree: ref n is list[n - 1], retained.+pub const Refs = struct {+ gpa: std.mem.Allocator,+ list: std.ArrayList(cf.AXUIElementRef) = .empty,++ pub fn clear(r: *Refs) void {+ for (r.list.items) |e| cf.CFRelease(e);+ r.list.clearRetainingCapacity();+ }++ pub fn deinit(r: *Refs) void {+ r.clear();+ r.list.deinit(r.gpa);+ }++ fn add(r: *Refs, el: cf.AXUIElementRef) !u32 {+ try r.list.append(r.gpa, el);+ _ = cf.CFRetain(el);+ return @intCast(r.list.items.len);+ }++ pub fn get(r: *Refs, ref: i64) ?cf.AXUIElementRef {+ if (ref < 1 or ref > r.list.items.len) return null;+ return r.list.items[@intCast(ref - 1)];+ }+};++const names = [_][]const u8{ "AXRole", "AXSubrole", "AXTitle", "AXDescription", "AXValue", "AXPosition", "AXSize", "AXChildren", "AXEnabled", "AXFocused", "AXSelected" };+const Attr = enum { role, subrole, title, description, value, position, size, children, enabled, focused, selected };++pub const Options = struct {+ max_nodes: usize = 400,+ /// The menu bar's items (long; keys reach menus anyway).+ menus: bool = false,+};++const Walk = struct {+ refs: *Refs,+ w: *std.Io.Writer,+ opts: Options,+ names: cf.CFArrayRef,+ shown: usize = 0,+ full: bool = false,+};++/// Writes app `pid`'s tree to `w`, replacing the refs.+pub fn writeTree(refs: *Refs, w: *std.Io.Writer, pid: i32, opts: Options) !void {+ refs.clear();+ var name_refs: [names.len]?*const anyopaque = undefined;+ for (names, 0..) |n, i| name_refs[i] = cf.str(n);+ defer for (name_refs) |n| cf.CFRelease(@constCast(n.?));+ const arr = cf.CFArrayCreate(null, &name_refs, names.len, &cf.kCFTypeArrayCallBacks) orelse return error.OutOfMemory;+ defer cf.CFRelease(arr);+ const app = cf.AXUIElementCreateApplication(pid);+ defer cf.CFRelease(app);+ // Chromium and Electron build their tree only for assistive technology+ // that says it's there; everything else ignores this. Until it's built+ // they show no elements, so an empty first read is worth another look.+ _ = set(app, "AXManualAccessibility", cf.kCFBooleanTrue);+ var walk: Walk = .{ .refs = refs, .w = w, .opts = opts, .names = arr };+ var title_buf: [128]u8 = undefined;+ try w.print("app \"{s}\" pid {d}\n", .{ copyText(&title_buf, app, "AXTitle"), pid });+ const before = w.end;+ try visit(&walk, app, 0, 0);+ var tries: usize = 0;+ while (w.end == before and tries < 3) : (tries += 1) {+ _ = usleep(300_000);+ try visit(&walk, app, 0, 0);+ }+ if (w.end == before) try w.writeAll("(no elements: the app shows no windows, or it doesn't support accessibility)\n");+ if (walk.full) try w.print("(stopped at {d} elements: ask for a larger max_nodes, up to 2000)\n", .{walk.shown});+}++fn visit(walk: *Walk, el: cf.AXUIElementRef, depth: usize, level: usize) !void {+ if (walk.full or level > 64) return;+ var out: ?cf.CFArrayRef = null;+ if (cf.AXUIElementCopyMultipleAttributeValues(el, walk.names, 0, &out) != 0) return;+ const vals = out orelse return;+ defer cf.CFRelease(vals);+ var v: [names.len]?*anyopaque = undefined;+ for (0..names.len) |i| {+ const x = cf.CFArrayGetValueAtIndex(vals, @intCast(i));+ // Missing attributes come back as AXValues holding an error.+ v[i] = if (x) |p| (if (cf.CFGetTypeID(p) == cf.AXValueGetTypeID() and cf.AXValueGetType(@ptrCast(p)) == 5) null else p) else null;+ }+ var bufs: [5][256]u8 = undefined;+ const at = struct {+ fn f(vs: *[names.len]?*anyopaque, a: Attr) ?*anyopaque {+ return vs[@backingInt(a)];+ }+ }.f;+ var node: tree.Node = .{+ .role = cf.text(&bufs[0], at(&v, .role)),+ .subrole = cf.text(&bufs[1], at(&v, .subrole)),+ .title = cf.text(&bufs[2], at(&v, .title)),+ .description = cf.text(&bufs[3], at(&v, .description)),+ .value = cf.text(&bufs[4], at(&v, .value)),+ .enabled = cf.boolean(at(&v, .enabled)) orelse true,+ .focused = cf.boolean(at(&v, .focused)) orelse false,+ .selected = cf.boolean(at(&v, .selected)) orelse false,+ };+ if (cf.point(at(&v, .position))) |p| if (cf.size(at(&v, .size))) |s| {+ node.frame = .{ p.x, p.y, s.width, s.height };+ };+ if (std.mem.eql(u8, node.role, "AXMenuBar") and !walk.opts.menus) return;+ var child_depth = depth;+ // The application itself is the header, not a line.+ if (level > 0 and tree.shown(node)) {+ if (walk.shown >= walk.opts.max_nodes) {+ walk.full = true;+ return;+ }+ const ref = try walk.refs.add(el);+ try tree.writeLine(walk.w, node, ref, depth);+ walk.shown += 1;+ child_depth += 1;+ }+ const kids = cf.array(at(&v, .children)) orelse return;+ const n: usize = @intCast(cf.CFArrayGetCount(kids));+ for (0..n) |i| {+ const k = cf.CFArrayGetValueAtIndex(kids, @intCast(i)) orelse continue;+ try visit(walk, @ptrCast(k), child_depth, level + 1);+ }+}++/// An attribute as text (a title, a role).+pub fn copyText(buf: []u8, el: cf.AXUIElementRef, attr: []const u8) []const u8 {+ const v = copy(el, attr) orelse return "";+ defer cf.CFRelease(v);+ return cf.text(buf, v);+}++/// An attribute's value, owned: release it.+pub fn copy(el: cf.AXUIElementRef, attr: []const u8) ?*anyopaque {+ const name = cf.str(attr);+ defer cf.CFRelease(name);+ var out: ?cf.CFTypeRef = null;+ if (cf.AXUIElementCopyAttributeValue(el, name, &out) != 0) return null;+ return out;+}++/// x, y, width, height in screen points.+pub fn frame(el: cf.AXUIElementRef) ?[4]f64 {+ const p = copy(el, "AXPosition") orelse return null;+ defer cf.CFRelease(p);+ const s = copy(el, "AXSize") orelse return null;+ defer cf.CFRelease(s);+ const pt = cf.point(p) orelse return null;+ const sz = cf.size(s) orelse return null;+ return .{ pt.x, pt.y, sz.width, sz.height };+}++pub fn center(el: cf.AXUIElementRef) ?[2]f64 {+ const f = frame(el) orelse return null;+ return .{ f[0] + f[2] / 2, f[1] + f[3] / 2 };+}++pub fn pidOf(el: cf.AXUIElementRef) ?i32 {+ var pid: i32 = 0;+ return if (cf.AXUIElementGetPid(el, &pid) == 0) pid else null;+}++pub fn perform(el: cf.AXUIElementRef, action: []const u8) bool {+ const a = cf.str(action);+ defer cf.CFRelease(a);+ return cf.AXUIElementPerformAction(el, a) == 0;+}++pub fn set(el: cf.AXUIElementRef, attr: []const u8, value: *anyopaque) cf.AXError {+ const a = cf.str(attr);+ defer cf.CFRelease(a);+ return cf.AXUIElementSetAttributeValue(el, a, value);+}++pub fn setText(el: cf.AXUIElementRef, s: []const u8) cf.AXError {+ const v = cf.str(s);+ defer cf.CFRelease(v);+ return set(el, "AXValue", v);+}++pub fn focus(el: cf.AXUIElementRef) bool {+ return set(el, "AXFocused", cf.kCFBooleanTrue) == 0;+}++/// The app owning whatever is at x, y.+pub fn pidAt(x: f64, y: f64) ?i32 {+ const sys = cf.AXUIElementCreateSystemWide();+ defer cf.CFRelease(sys);+ var el: ?cf.AXUIElementRef = null;+ if (cf.AXUIElementCopyElementAtPosition(sys, @floatCast(x), @floatCast(y), &el) != 0) return null;+ const e = el orelse return null;+ defer cf.CFRelease(e);+ return pidOf(e);+}++/// The app the keyboard goes to.+pub fn focusedPid() ?i32 {+ const sys = cf.AXUIElementCreateSystemWide();+ defer cf.CFRelease(sys);+ const app = copy(sys, "AXFocusedApplication") orelse return null;+ defer cf.CFRelease(app);+ return pidOf(@ptrCast(app));+}++/// How long a call waits for an app that doesn't answer (a hung app+/// mustn't hold every agent for the default six seconds).+pub fn setTimeout(seconds: f32) void {+ const sys = cf.AXUIElementCreateSystemWide();+ defer cf.CFRelease(sys);+ _ = cf.AXUIElementSetMessagingTimeout(sys, seconds);+}diff --git a/apps/nanocua/src/mac/backend.zig b/apps/nanocua/src/mac/backend.zignew file mode 100644index 00000000..57d96564--- /dev/null+++ b/apps/nanocua/src/mac/backend.zig@@ -0,0 +1,403 @@+//! The tools on macOS. Calls run one at a time on a serial queue of their+//! own, so a slow app's accessibility (or a screenshot) never holds the+//! panel; each answers on the main thread. Calls work inside one app in the+//! background (target.zig) unless they ask for the whole screen and the user+//! allows it. Acting re-checks both switches on the queue (Stop wins over calls+//! already waiting) and refuses to touch nanocua's own windows.+const std = @import("std");+const nanoui = @import("nanoui");+const objc = nanoui.objc;+const mcp = @import("../mcp.zig");+const tools = @import("../tools.zig");+const keys = @import("../keys.zig");+const cf = @import("cf.zig");+const ax = @import("ax.zig");+const input = @import("input.zig");+const capture = @import("capture.zig");+const apps = @import("apps.zig");+const windows = @import("windows.zig");+const target = @import("../target.zig");++extern "c" fn dispatch_queue_create(label: [*:0]const u8, attr: ?*anyopaque) objc.dispatch_queue_t;+extern "c" fn dispatch_async_f(queue: objc.dispatch_queue_t, ctx: ?*anyopaque, work: *const fn (?*anyopaque) callconv(.c) void) void;+extern "c" fn getpid() c_int;+extern "c" fn usleep(us: c_uint) c_int;++pub const Mac = struct {+ gpa: std.mem.Allocator,+ queue: objc.dispatch_queue_t,+ /// The last tree's elements: the queue's alone.+ refs: ax.Refs,+ /// The kill switch as the queue sees it.+ stopped: std.atomic.Value(bool) = .init(false),+ /// The whole-screen switch as the queue sees it.+ whole_screen: std.atomic.Value(bool) = .init(false),+ /// The app the agent last read or opened: the queue's alone.+ last: ?i32 = null,+ self_pid: i32,++ pub fn init(gpa: std.mem.Allocator) Mac {+ ax.setTimeout(2.0);+ return .{ .gpa = gpa, .queue = dispatch_queue_create("nanocua.tools", null), .refs = .{ .gpa = gpa }, .self_pid = getpid() };+ }++ pub fn backend(m: *Mac) mcp.Backend {+ return .{ .ptr = m, .call = call };+ }++ fn call(ptr: *anyopaque, tool: tools.Tool, args: tools.Args, reply: *mcp.Reply) void {+ const m: *Mac = @ptrCast(@alignCast(ptr));+ const job = Job.create(m, tool, args, reply) catch return reply.fail("nanocua is out of memory");+ dispatch_async_f(m.queue, job, Job.run);+ }+};++/// Permissions, as the panel shows them.+pub fn accessibility() bool {+ return cf.trusted(false);+}++pub fn screenRecording() bool {+ return capture.allowed();+}++/// Asks for a permission: the system's prompt, then its Settings pane.+pub fn request(which: enum { accessibility, screen }) void {+ switch (which) {+ .accessibility => if (!cf.trusted(true)) openSettings("Privacy_Accessibility"),+ .screen => if (!cf.CGRequestScreenCaptureAccess()) openSettings("Privacy_ScreenCapture"),+ }+}++fn openSettings(pane: []const u8) void {+ var buf: [128]u8 = undefined;+ const url = std.fmt.bufPrint(&buf, "x-apple.systempreferences:com.apple.preference.security?{s}", .{pane}) catch return;+ _ = nanoui.shell.openUrl(url);+}++const Job = struct {+ mac: *Mac,+ tool: tools.Tool,+ reply: *mcp.Reply,+ arena: std.heap.ArenaAllocator,+ // The arguments, copied off the request.+ x: ?f64,+ y: ?f64,+ width: ?f64,+ height: ?f64,+ dx: ?f64,+ dy: ?f64,+ ref: ?i64,+ pid: ?i64,+ count: ?i64,+ max_nodes: ?i64,+ max_size: ?i64,+ whole_screen: bool,+ text: ?[]const u8,+ name: ?[]const u8,+ app: ?[]const u8,+ button: ?[]const u8,+ // The answer.+ out: std.Io.Writer.Allocating,+ shot: ?capture.Shot = null,+ failed: bool = false,++ fn create(m: *Mac, tool: tools.Tool, a: tools.Args, reply: *mcp.Reply) !*Job {+ const j = try m.gpa.create(Job);+ j.* = .{+ .mac = m,+ .tool = tool,+ .reply = reply,+ .arena = .init(m.gpa),+ .out = .init(m.gpa),+ .x = a.number("x"),+ .y = a.number("y"),+ .width = a.number("width"),+ .height = a.number("height"),+ .dx = a.number("dx"),+ .dy = a.number("dy"),+ .ref = a.int("ref"),+ .pid = a.int("pid"),+ .count = a.int("count"),+ .max_nodes = a.int("max_nodes"),+ .max_size = a.int("max_size"),+ .whole_screen = a.boolean("whole_screen") orelse false,+ .text = null,+ .name = null,+ .app = null,+ .button = null,+ };+ const al = j.arena.allocator();+ // Whichever string the tool takes, under one name.+ const t = a.string("text") orelse a.string("keys") orelse a.string("value");+ if (t) |s| j.text = try al.dupe(u8, s);+ if (a.string("name")) |s| j.name = try al.dupe(u8, s);+ if (a.string("app")) |s| j.app = try al.dupe(u8, s);+ if (a.string("button")) |s| j.button = try al.dupe(u8, s);+ return j;+ }++ fn run(ctx: ?*anyopaque) callconv(.c) void {+ const j: *Job = @ptrCast(@alignCast(ctx));+ const pool = objc.Pool.push();+ j.perform() catch |e| j.fail("{s}", .{@errorName(e)});+ pool.pop();+ objc.dispatchMain(j, deliver);+ }++ fn deliver(ctx: ?*anyopaque) callconv(.c) void {+ const j: *Job = @ptrCast(@alignCast(ctx));+ const gpa = j.mac.gpa;+ defer {+ if (j.shot) |s| gpa.free(s.data);+ j.out.deinit();+ j.arena.deinit();+ gpa.destroy(j);+ }+ const text = j.out.written();+ if (j.shot) |s| {+ j.reply.finish(&.{ .{ .image = .{ .data = s.data, .mime = "image/jpeg" } }, .{ .text = text } }, false, .ok);+ } else j.reply.finish(&.{.{ .text = if (text.len > 0) text else "done" }}, j.failed, if (j.failed) .failed else .ok);+ }++ fn fail(j: *Job, comptime fmt: []const u8, args: anytype) void {+ j.failed = true;+ j.out.clearRetainingCapacity();+ j.out.writer.print(fmt, args) catch {};+ }++ fn perform(j: *Job) !void {+ const w = &j.out.writer;+ const m = j.mac;+ if (j.tool.kind() == .act) {+ if (m.stopped.load(.acquire)) return j.fail("nanocua is stopped: the user pressed Stop.", .{});+ if (!accessibility()) return j.fail("nanocua has no Accessibility permission, which input needs: ask the user to grant it in nanocua's window.", .{});+ }+ switch (j.tool) {+ .list_apps => apps.each(j, writeApp),+ .get_ui_tree => {+ if (!accessibility()) return j.fail("nanocua has no Accessibility permission: ask the user to grant it in nanocua's window.", .{});+ const named = j.namedPid() orelse return;+ const pid = named orelse m.last orelse+ ax.focusedPid() orelse return j.fail("No app has the keyboard focus: pass pid or app.", .{});+ if (pid == m.self_pid) return j.fail("That is nanocua itself, which agents don't see. Pass pid or app (see list_apps).", .{});+ const max: usize = @intCast(std.math.clamp(j.max_nodes orelse 400, 1, 2000));+ try ax.writeTree(&m.refs, w, pid, .{ .max_nodes = max });+ m.last = pid;+ },+ .screenshot => try j.screenshot(),+ .click => {+ const button: input.Button = if (j.button != null and std.mem.eql(u8, j.button.?, "right")) .right else .left;+ const count: u32 = @intCast(std.math.clamp(j.count orelse 1, 1, 3));+ if (j.ref) |r| {+ const el = j.element(r) orelse return;+ const t = j.events(ax.pidOf(el)) orelse return;+ if (button == .left and count == 1 and ax.perform(el, "AXPress")) return w.print("Pressed [{d}].", .{r});+ const c = ax.center(el) orelse return j.fail("[{d}] has no frame to click.", .{r});+ input.click(t, c[0], c[1], button, count);+ return w.print("Clicked [{d}] at {d:.0},{d:.0}.", .{ r, c[0], c[1] });+ }+ const t = j.events(null) orelse return;+ const p = j.point(t) orelse return;+ input.click(t, p[0], p[1], button, count);+ try w.print("Clicked at {d:.0},{d:.0}.", .{ p[0], p[1] });+ },+ .type_text => {+ const text = j.text orelse return j.fail("type_text needs text.", .{});+ var el_pid: ?i32 = null;+ const el = if (j.ref) |r| blk: {+ const e = j.element(r) orelse return;+ el_pid = ax.pidOf(e);+ break :blk e;+ } else null;+ const t = j.events(el_pid) orelse return;+ if (el) |e| {+ if (!ax.focus(e)) if (ax.center(e)) |c| input.click(t, c[0], c[1], .left, 1);+ _ = usleep(60_000);+ }+ if (!j.keyboardIsOthers(t)) return;+ const typed = input.typeText(t, text, &m.stopped);+ try w.print("Typed {d} characters.", .{typed});+ },+ .press_key => {+ var buf: [32]u8 = undefined;+ const combo = keys.parse(&buf, j.text orelse "") catch+ return j.fail("Write keys like \"cmd+s\", \"shift+tab\", \"enter\", \"escape\", \"left\", \"f5\".", .{});+ const t = j.events(null) orelse return;+ if (!j.keyboardIsOthers(t)) return;+ if (!input.press(t, combo)) return j.fail("No key code for that key.", .{});+ try w.print("Pressed {s}.", .{j.text.?});+ },+ .scroll => {+ var el_pid: ?i32 = null;+ const center = if (j.ref) |r| blk: {+ const el = j.element(r) orelse return;+ el_pid = ax.pidOf(el);+ break :blk ax.center(el) orelse return j.fail("[{d}] has no frame.", .{r});+ } else null;+ const t = j.events(el_pid) orelse return;+ const p = center orelse j.point(t) orelse return;+ const dx = j.dx orelse 0;+ const dy: f64 = j.dy orelse if (dx == 0) 3 else 0;+ input.scroll(t, p[0], p[1], dx, dy);+ try w.print("Scrolled {d} down, {d} right at {d:.0},{d:.0}.", .{ dy, dx, p[0], p[1] });+ },+ .move_mouse => {+ const t = j.events(null) orelse return;+ const p = j.point(t) orelse return;+ input.move(t, p[0], p[1]);+ try w.print("Moved to {d:.0},{d:.0}.", .{ p[0], p[1] });+ },+ .set_value => {+ const r = j.ref orelse return j.fail("set_value needs ref.", .{});+ const el = j.element(r) orelse return;+ const err = ax.setText(el, j.text orelse "");+ if (err != 0) return j.fail("[{d}] refused the value (AXError {d}): it may not be editable; try type_text.", .{ r, err });+ try w.print("Set [{d}].", .{r});+ },+ .open_app => {+ const n = j.name orelse return j.fail("open_app needs name.", .{});+ if (j.whole_screen and !m.whole_screen.load(.acquire)) return j.fail("The whole screen is off in nanocua: open it behind the user's work (leave out whole_screen).", .{});+ switch (apps.open(n, j.whole_screen)) {+ .running => try w.print("{s} is running, behind what the user is doing. Read it with get_ui_tree.", .{n}),+ .activated => try w.print("Brought {s} forward.", .{n}),+ .launched => try w.print("Opening {s} in the background. Read it with get_ui_tree once it has started.", .{n}),+ .not_found => return j.fail("Found no app named \"{s}\".", .{n}),+ }+ // A later get_ui_tree without app reads this one. (One just+ // launching isn't in the running list yet.)+ if (apps.pidNamed(n)) |pid| if (pid != m.self_pid) {+ m.last = pid;+ };+ },+ }+ // Let what was done show before the agent looks again.+ if (j.tool.kind() == .act) _ = usleep(120_000);+ }++ fn writeApp(j: *Job, a: apps.App) void {+ const w = &j.out.writer;+ w.print("{s} — {s} pid {d}", .{ a.name, a.bundle, a.pid }) catch return;+ if (a.active) w.writeAll(" (frontmost)") catch return;+ if (a.pid == j.mac.self_pid) w.writeAll(" (nanocua: off limits)") catch return;+ w.writeByte('\n') catch return;+ }++ /// The element behind ref `r`, unless it is missing or nanocua's.+ fn element(j: *Job, r: i64) ?cf.AXUIElementRef {+ const el = j.mac.refs.get(r) orelse {+ j.fail("No element [{d}]: refs come from the last get_ui_tree.", .{r});+ return null;+ };+ if (ax.pidOf(el)) |pid| if (pid == j.mac.self_pid) {+ j.fail("[{d}] is nanocua's own: agents can't act on it.", .{r});+ return null;+ };+ return el;+ }++ /// The pid the call names, which may be none. Null if it names an app+ /// that isn't running (it says so).+ fn namedPid(j: *Job) ??i32 {+ if (j.pid) |p| return @as(?i32, @intCast(std.math.clamp(p, 0, std.math.maxInt(i32))));+ const n = j.app orelse return @as(?i32, null);+ if (apps.pidNamed(n)) |pid| return @as(?i32, pid);+ j.fail("No running app is named \"{s}\" (see list_apps).", .{n});+ return null;+ }++ /// Where this call's events go, or null if it may not send any (it says+ /// why). `el_pid`: the app of the element it acts on.+ fn events(j: *Job, el_pid: ?i32) ?input.Target {+ const m = j.mac;+ const named = j.namedPid() orelse return null;+ const r = target.route(.{+ .whole_screen = j.whole_screen,+ .allowed = m.whole_screen.load(.acquire),+ .pid = named orelse el_pid,+ .last = m.last,+ .self = m.self_pid,+ });+ switch (r) {+ .refused => |why| {+ j.fail("{s}", .{why});+ return null;+ },+ .whole_screen => return .hid,+ .app => |pid| {+ m.last = pid;+ return .{ .app = .{ .pid = pid, .window = if (windows.main(pid)) |w| w.id else 0 } };+ },+ }+ }++ /// x and y. On the real pointer, not over nanocua's own windows.+ fn point(j: *Job, t: input.Target) ?[2]f64 {+ const x = j.x orelse {+ j.fail("{s} needs ref, or x and y.", .{@tagName(j.tool)});+ return null;+ };+ const y = j.y orelse {+ j.fail("{s} needs y too.", .{@tagName(j.tool)});+ return null;+ };+ if (t == .hid) if (ax.pidAt(x, y)) |pid| if (pid == j.mac.self_pid) {+ j.fail("{d:.0},{d:.0} is over nanocua's own window: agents can't act on it.", .{ x, y });+ return null;+ };+ return .{ x, y };+ }++ /// Whether keys can go where `t` sends them: into an app they always can.+ fn keyboardIsOthers(j: *Job, t: input.Target) bool {+ if (t != .hid) return true;+ if (ax.focusedPid()) |pid| if (pid == j.mac.self_pid) {+ j.fail("nanocua has the keyboard: keys would go to its own window. Bring another app forward first (open_app).", .{});+ return false;+ };+ return true;+ }++ fn screenshot(j: *Job) !void {+ const m = j.mac;+ const named = j.namedPid() orelse return;+ const route = target.route(.{+ .whole_screen = j.whole_screen,+ .allowed = m.whole_screen.load(.acquire),+ .pid = named,+ .last = m.last,+ .self = m.self_pid,+ });+ const max: u32 = @intCast(std.math.clamp(j.max_size orelse 1280, 64, 2560));+ var rect = capture.screen();+ const result: capture.Error!capture.Shot = switch (route) {+ .refused => |why| return j.fail("{s}", .{why}),+ .whole_screen => blk: {+ if (j.x != null or j.y != null or j.width != null or j.height != null) {+ const x = j.x orelse 0;+ const y = j.y orelse 0;+ rect = .{ .origin = .{ .x = x, .y = y }, .size = .{+ .width = j.width orelse rect.size.width - x,+ .height = j.height orelse rect.size.height - y,+ } };+ if (rect.size.width < 1 or rect.size.height < 1) return j.fail("The region is empty.", .{});+ }+ break :blk capture.capture(m.gpa, rect, max);+ },+ .app => |pid| blk: {+ const w = windows.main(pid) orelse return j.fail("App {d} has no window to capture.", .{pid});+ rect = w.frame;+ m.last = pid;+ break :blk capture.window(m.gpa, w.id, w.frame, max);+ },+ };+ const shot = result catch |e| return switch (e) {+ error.NoPermission => j.fail("nanocua has no Screen Recording permission: ask the user to grant it in nanocua's window, or use get_ui_tree.", .{}),+ else => j.fail("The screenshot failed ({s}).", .{@errorName(e)}),+ };+ j.shot = shot;+ const sx = rect.size.width / @as(f64, @floatFromInt(shot.width));+ const sy = rect.size.height / @as(f64, @floatFromInt(shot.height));+ try j.out.writer.print("The image is {d}x{d} px of the region at {d:.0},{d:.0} sized {d:.0}x{d:.0} points: pixel (px, py) is the point ({d:.0} + px*{d:.4}, {d:.0} + py*{d:.4}).", .{ shot.width, shot.height, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, rect.origin.x, sx, rect.origin.y, sy });+ }+};diff --git a/apps/nanocua/src/mac/capture.zig b/apps/nanocua/src/mac/capture.zignew file mode 100644index 00000000..45bff006--- /dev/null+++ b/apps/nanocua/src/mac/capture.zig@@ -0,0 +1,192 @@+//! Pictures: one window, even behind others (ScreenCaptureKit's window filter,+//! macOS 14 and later), or a rectangle of the screen (macOS 15.2 and later;+//! CGWindowListCreateImage before either), encoded as JPEG by ImageIO at most+//! `max_size` pixels on the longer side. Runs on the backend's queue, waiting+//! there for ScreenCaptureKit's answer.+const std = @import("std");+const objc = @import("nanoui").objc;+const cf = @import("cf.zig");++const CGImageRef = *opaque {};+const Destination = *opaque {};+extern "c" fn CGImageGetWidth(img: CGImageRef) usize;+extern "c" fn CGImageGetHeight(img: CGImageRef) usize;+extern "c" fn CGImageRelease(img: CGImageRef) void;+extern "c" fn CGWindowListCreateImage(rect: cf.CGRect, list: u32, window: u32, options: u32) ?CGImageRef;+extern "c" fn CGImageDestinationCreateWithData(data: cf.CFMutableDataRef, kind: cf.CFStringRef, count: usize, options: ?cf.CFDictionaryRef) ?Destination;+extern "c" fn CGImageDestinationAddImage(dest: Destination, img: CGImageRef, props: ?cf.CFDictionaryRef) void;+extern "c" fn CGImageDestinationFinalize(dest: Destination) bool;+extern "c" const kCGImageDestinationLossyCompressionQuality: cf.CFStringRef;+extern "c" const kCGImageDestinationImageMaxPixelSize: cf.CFStringRef;+extern "c" fn objc_getClass(name: [*:0]const u8) ?objc.Class;+extern "c" const CGRectNull: cf.CGRect;++const list_including_window: u32 = 8;+const image_bounds_ignore_framing: u32 = 1;++pub const Shot = struct {+ /// Base64 of the JPEG.+ data: []u8,+ /// The image's size in pixels, after scaling.+ width: usize,+ height: usize,+};++pub const Error = error{ NoPermission, CaptureFailed, EncodeFailed, OutOfMemory };++/// The main display's frame in points.+pub fn screen() cf.CGRect {+ return cf.CGDisplayBounds(cf.CGMainDisplayID());+}++pub fn allowed() bool {+ return cf.CGPreflightScreenCaptureAccess();+}++/// A rectangle of the screen.+pub fn capture(gpa: std.mem.Allocator, rect: cf.CGRect, max_size: u32) Error!Shot {+ // Asking would put up the system's prompt for an agent: only the user+ // asks, from the panel.+ if (!allowed()) return error.NoPermission;+ const img = try grab(gpa, rect) orelse return error.CaptureFailed;+ defer CGImageRelease(img);+ return encode(gpa, img, max_size);+}++/// One window, whatever is in front of it. `frame` is its size in points.+pub fn window(gpa: std.mem.Allocator, id: u32, frame: cf.CGRect, max_size: u32) Error!Shot {+ if (!allowed()) return error.NoPermission;+ const img = try grabWindow(gpa, id, frame, max_size) orelse return error.CaptureFailed;+ defer CGImageRelease(img);+ return encode(gpa, img, max_size);+}++fn encode(gpa: std.mem.Allocator, img: CGImageRef, max_size: u32) Error!Shot {+ const w = CGImageGetWidth(img);+ const h = CGImageGetHeight(img);+ const longer = @max(w, h, 1);+ const scale = @min(1.0, @as(f64, @floatFromInt(max_size)) / @as(f64, @floatFromInt(longer)));+ const out_w: usize = @max(1, @as(usize, @intFromFloat(@round(@as(f64, @floatFromInt(w)) * scale))));+ const out_h: usize = @max(1, @as(usize, @intFromFloat(@round(@as(f64, @floatFromInt(h)) * scale))));++ const data = cf.CFDataCreateMutable(null, 0) orelse return error.OutOfMemory;+ defer cf.CFRelease(data);+ const kind = cf.str("public.jpeg");+ defer cf.CFRelease(kind);+ const dest = CGImageDestinationCreateWithData(data, kind, 1, null) orelse return error.EncodeFailed;+ defer cf.CFRelease(dest);+ const quality: f64 = 0.75;+ const max_px: f64 = @floatFromInt(@max(out_w, out_h));+ const q = cf.CFNumberCreate(null, cf.number_double, &quality) orelse return error.OutOfMemory;+ defer cf.CFRelease(q);+ const m = cf.CFNumberCreate(null, cf.number_double, &max_px) orelse return error.OutOfMemory;+ defer cf.CFRelease(m);+ const keys = [_]?*const anyopaque{ kCGImageDestinationLossyCompressionQuality, kCGImageDestinationImageMaxPixelSize };+ const values = [_]?*const anyopaque{ q, m };+ const props = cf.CFDictionaryCreate(null, &keys, &values, 2, &cf.kCFTypeDictionaryKeyCallBacks, &cf.kCFTypeDictionaryValueCallBacks) orelse return error.OutOfMemory;+ defer cf.CFRelease(props);+ CGImageDestinationAddImage(dest, img, props);+ if (!CGImageDestinationFinalize(dest)) return error.EncodeFailed;+ const bytes = cf.CFDataGetBytePtr(data)[0..@intCast(cf.CFDataGetLength(data))];+ const enc = std.base64.standard.Encoder;+ const b64 = try gpa.alloc(u8, enc.calcSize(bytes.len));+ _ = enc.encode(b64, bytes);+ return .{ .data = b64, .width = out_w, .height = out_h };+}++const Wait = struct {+ sem: objc.dispatch_semaphore_t,+ image: ?CGImageRef = null,++ fn done(w: *Wait, img: ?*anyopaque, err: ?objc.Object) void {+ _ = err;+ if (img) |i| w.image = @ptrCast(cf.CFRetain(i));+ _ = objc.dispatch_semaphore_signal(w.sem);+ }+};++/// The window server's windows, for finding one to capture.+const Shareable = struct {+ sem: objc.dispatch_semaphore_t,+ content: ?objc.Object = null,++ fn done(w: *Shareable, content: ?objc.Object, err: ?objc.Object) void {+ _ = err;+ if (content) |c| w.content = objc.retain(c);+ _ = objc.dispatch_semaphore_signal(w.sem);+ }+};++const answer_timeout: u64 = 10 * std.time.ns_per_s;++/// ScreenCaptureKit's window with number `id`, retained.+fn scWindow(gpa: std.mem.Allocator, cls: objc.Class, id: u32) !?objc.Object {+ const wait = try gpa.create(Shareable);+ wait.* = .{ .sem = objc.dispatch_semaphore_create(0) };+ var block = objc.Block(Shareable, &.{ ?objc.Object, ?objc.Object }).init(wait, Shareable.done);+ objc.msg(void, cls, "getShareableContentWithCompletionHandler:", .{@as(*anyopaque, @ptrCast(&block))});+ // As with a picture: a late answer still has somewhere to go.+ if (objc.dispatch_semaphore_wait(wait.sem, dispatchTime(answer_timeout)) != 0) return null;+ defer gpa.destroy(wait);+ defer objc.dispatch_release(@ptrCast(wait.sem));+ const content = wait.content orelse return null;+ defer objc.release(content);+ const list = objc.msg(objc.Object, content, "windows", .{});+ for (0..objc.msg(usize, list, "count", .{})) |i| {+ const w = objc.msg(objc.Object, list, "objectAtIndex:", .{i});+ if (objc.msg(u32, w, "windowID", .{}) == id) return objc.retain(w);+ }+ return null;+}++fn grabWindow(gpa: std.mem.Allocator, id: u32, frame: cf.CGRect, max_size: u32) !?CGImageRef {+ const manager = objc_getClass("SCScreenshotManager");+ const shareable = objc_getClass("SCShareableContent");+ const modern = if (manager) |c| objc.msg(bool, c, "respondsToSelector:", .{objc.sel("captureImageWithFilter:configuration:completionHandler:")}) else false;+ if (!modern or shareable == null) return CGWindowListCreateImage(CGRectNull, list_including_window, id, image_bounds_ignore_framing);+ const sc = try scWindow(gpa, shareable.?, id) orelse return null;+ defer objc.release(sc);+ const filter = objc.msg(objc.Object, objc.alloc("SCContentFilter"), "initWithDesktopIndependentWindow:", .{sc});+ defer objc.release(filter);+ const config = objc.new("SCStreamConfiguration");+ defer objc.release(config);+ // Retina's pixels, unless that's more than the picture will keep.+ const longer = @max(frame.size.width, frame.size.height, 1);+ const scale = @min(2.0, @as(f64, @floatFromInt(max_size)) / longer);+ objc.msg(void, config, "setWidth:", .{@as(usize, @intFromFloat(@max(1, @round(frame.size.width * scale))))});+ objc.msg(void, config, "setHeight:", .{@as(usize, @intFromFloat(@max(1, @round(frame.size.height * scale))))});+ objc.msg(void, config, "setShowsCursor:", .{false});+ const wait = try gpa.create(Wait);+ wait.* = .{ .sem = objc.dispatch_semaphore_create(0) };+ var block = objc.Block(Wait, &.{ ?*anyopaque, ?objc.Object }).init(wait, Wait.done);+ objc.msg(void, manager.?, "captureImageWithFilter:configuration:completionHandler:", .{ filter, config, @as(*anyopaque, @ptrCast(&block)) });+ if (objc.dispatch_semaphore_wait(wait.sem, dispatchTime(answer_timeout)) != 0) return null;+ defer gpa.destroy(wait);+ defer objc.dispatch_release(@ptrCast(wait.sem));+ return wait.image;+}++fn grab(gpa: std.mem.Allocator, rect: cf.CGRect) !?CGImageRef {+ const r: objc.CGRect = .{ .origin = .{ .x = rect.origin.x, .y = rect.origin.y }, .size = .{ .width = rect.size.width, .height = rect.size.height } };+ const cls = objc_getClass("SCScreenshotManager");+ const modern = if (cls) |c| objc.msg(bool, c, "respondsToSelector:", .{objc.sel("captureImageInRect:completionHandler:")}) else false;+ if (!modern) {+ // Below macOS 15.2: on screen windows, at the display's resolution.+ return CGWindowListCreateImage(rect, 1, 0, 0);+ }+ // On the heap: if ScreenCaptureKit never answers, its late answer+ // still has somewhere to go (left allocated, rare and small).+ const wait = try gpa.create(Wait);+ wait.* = .{ .sem = objc.dispatch_semaphore_create(0) };+ var block = objc.Block(Wait, &.{ ?*anyopaque, ?objc.Object }).init(wait, Wait.done);+ objc.msg(void, cls.?, "captureImageInRect:completionHandler:", .{ r, @as(*anyopaque, @ptrCast(&block)) });+ if (objc.dispatch_semaphore_wait(wait.sem, dispatchTime(answer_timeout)) != 0) return null;+ defer gpa.destroy(wait);+ defer objc.dispatch_release(@ptrCast(wait.sem));+ return wait.image;+}++extern "c" fn dispatch_time(when: u64, delta: i64) u64;+fn dispatchTime(ns: u64) u64 {+ return dispatch_time(0, @intCast(ns));+}diff --git a/apps/nanocua/src/mac/cf.zig b/apps/nanocua/src/mac/cf.zignew file mode 100644index 00000000..fcd6e509--- /dev/null+++ b/apps/nanocua/src/mac/cf.zig@@ -0,0 +1,153 @@+//! CoreFoundation, the Accessibility API (HIServices) and the screen+//! capture permission, declared directly; and reading CF values as Zig.+const std = @import("std");++pub const CFTypeRef = *opaque {};+pub const CFStringRef = *opaque {};+pub const CFArrayRef = *opaque {};+pub const CFDictionaryRef = *opaque {};+pub const CFMutableDataRef = *opaque {};+pub const CFNumberRef = *opaque {};+pub const AXUIElementRef = *opaque {};+pub const CFIndex = isize;+pub const CFRange = extern struct { location: CFIndex, length: CFIndex };+pub const CGPoint = extern struct { x: f64 = 0, y: f64 = 0 };+pub const CGSize = extern struct { width: f64 = 0, height: f64 = 0 };+pub const CGRect = extern struct { origin: CGPoint = .{}, size: CGSize = .{} };++const utf8: u32 = 0x08000100;++pub extern "c" const kCFTypeArrayCallBacks: opaque {};+pub extern "c" const kCFTypeDictionaryKeyCallBacks: opaque {};+pub extern "c" const kCFTypeDictionaryValueCallBacks: opaque {};+pub extern "c" const kCFBooleanTrue: CFTypeRef;+pub extern "c" const kCFBooleanFalse: CFTypeRef;+pub extern "c" fn CFRelease(ref: *anyopaque) void;+pub extern "c" fn CFRetain(ref: *anyopaque) *anyopaque;+pub extern "c" fn CFGetTypeID(ref: *anyopaque) usize;+pub extern "c" fn CFStringGetTypeID() usize;+pub extern "c" fn CFArrayGetTypeID() usize;+pub extern "c" fn CFBooleanGetTypeID() usize;+pub extern "c" fn CFNumberGetTypeID() usize;+pub extern "c" fn CFStringCreateWithBytes(alloc: ?*anyopaque, bytes: [*]const u8, len: CFIndex, encoding: u32, external: bool) ?CFStringRef;+pub extern "c" fn CFStringGetLength(s: CFStringRef) CFIndex;+pub extern "c" fn CFStringGetBytes(s: CFStringRef, range: CFRange, encoding: u32, loss: u8, external: bool, buf: ?[*]u8, max: CFIndex, used: ?*CFIndex) CFIndex;+pub extern "c" fn CFArrayCreate(alloc: ?*anyopaque, values: [*]const ?*const anyopaque, n: CFIndex, cb: *const anyopaque) ?CFArrayRef;+pub extern "c" fn CFArrayGetCount(a: CFArrayRef) CFIndex;+pub extern "c" fn CFArrayGetValueAtIndex(a: CFArrayRef, i: CFIndex) ?*anyopaque;+pub extern "c" fn CFBooleanGetValue(b: CFTypeRef) bool;+pub extern "c" fn CFNumberGetValue(n: CFTypeRef, kind: CFIndex, out: *anyopaque) bool;+pub extern "c" fn CFNumberCreate(alloc: ?*anyopaque, kind: CFIndex, value: *const anyopaque) ?CFNumberRef;+pub extern "c" fn CFDictionaryCreate(alloc: ?*anyopaque, keys: [*]const ?*const anyopaque, values: [*]const ?*const anyopaque, n: CFIndex, key_cb: *const anyopaque, value_cb: *const anyopaque) ?CFDictionaryRef;+pub extern "c" fn CFDataCreateMutable(alloc: ?*anyopaque, capacity: CFIndex) ?CFMutableDataRef;+pub extern "c" fn CFDataGetLength(d: CFMutableDataRef) CFIndex;+pub extern "c" fn CFDataGetBytePtr(d: CFMutableDataRef) [*]const u8;+pub const number_double: CFIndex = 13;++// Accessibility (HIServices).+pub const AXError = i32;+pub extern "c" fn AXIsProcessTrusted() bool;+pub extern "c" fn AXIsProcessTrustedWithOptions(options: ?CFDictionaryRef) bool;+pub extern "c" const kAXTrustedCheckOptionPrompt: CFStringRef;+pub extern "c" fn AXUIElementCreateSystemWide() AXUIElementRef;+pub extern "c" fn AXUIElementCreateApplication(pid: i32) AXUIElementRef;+pub extern "c" fn AXUIElementCopyAttributeValue(el: AXUIElementRef, attr: CFStringRef, out: *?CFTypeRef) AXError;+pub extern "c" fn AXUIElementCopyMultipleAttributeValues(el: AXUIElementRef, attrs: CFArrayRef, options: u32, out: *?CFArrayRef) AXError;+pub extern "c" fn AXUIElementSetAttributeValue(el: AXUIElementRef, attr: CFStringRef, value: *anyopaque) AXError;+pub extern "c" fn AXUIElementPerformAction(el: AXUIElementRef, action: CFStringRef) AXError;+pub extern "c" fn AXUIElementSetMessagingTimeout(el: AXUIElementRef, seconds: f32) AXError;+pub extern "c" fn AXUIElementCopyElementAtPosition(app: AXUIElementRef, x: f32, y: f32, out: *?AXUIElementRef) AXError;+pub extern "c" fn AXUIElementGetPid(el: AXUIElementRef, pid: *i32) AXError;+pub extern "c" fn AXValueGetTypeID() usize;+pub extern "c" fn AXValueGetType(v: CFTypeRef) u32;+pub extern "c" fn AXValueGetValue(v: CFTypeRef, kind: u32, out: *anyopaque) bool;+pub const ax_point: u32 = 1;+pub const ax_size: u32 = 2;++// The screen: its bounds and the permission to capture it.+pub extern "c" fn CGMainDisplayID() u32;+pub extern "c" fn CGDisplayBounds(display: u32) CGRect;+pub extern "c" fn CGPreflightScreenCaptureAccess() bool;+pub extern "c" fn CGRequestScreenCaptureAccess() bool;++/// A CFString of `s`; release it.+pub fn str(s: []const u8) CFStringRef {+ return CFStringCreateWithBytes(null, s.ptr, @intCast(s.len), utf8, false) orelse+ CFStringCreateWithBytes(null, "?", 1, utf8, false).?;+}++pub fn isString(v: *anyopaque) bool {+ return CFGetTypeID(v) == CFStringGetTypeID();+}++/// A CF string's UTF-8 into `buf` (cut if it doesn't fit).+pub fn utf8Into(buf: []u8, s: CFStringRef) []const u8 {+ var used: CFIndex = 0;+ const len = CFStringGetLength(s);+ _ = CFStringGetBytes(s, .{ .location = 0, .length = len }, utf8, '?', false, buf.ptr, @intCast(buf.len), &used);+ return buf[0..@intCast(used)];+}++/// A value as text: strings as they are, numbers and booleans written.+pub fn text(buf: []u8, v: ?*anyopaque) []const u8 {+ const x = v orelse return "";+ const t = CFGetTypeID(x);+ if (t == CFStringGetTypeID()) return utf8Into(buf, @ptrCast(x));+ if (t == CFNumberGetTypeID()) {+ var d: f64 = 0;+ if (!CFNumberGetValue(@ptrCast(x), number_double, &d)) return "";+ return std.fmt.bufPrint(buf, "{d}", .{d}) catch "";+ }+ if (t == CFBooleanGetTypeID()) return if (CFBooleanGetValue(@ptrCast(x))) "true" else "false";+ return "";+}++pub fn boolean(v: ?*anyopaque) ?bool {+ const x = v orelse return null;+ if (CFGetTypeID(x) != CFBooleanGetTypeID()) return null;+ return CFBooleanGetValue(@ptrCast(x));+}++pub fn point(v: ?*anyopaque) ?CGPoint {+ const x = v orelse return null;+ if (CFGetTypeID(x) != AXValueGetTypeID() or AXValueGetType(@ptrCast(x)) != ax_point) return null;+ var p: CGPoint = .{};+ return if (AXValueGetValue(@ptrCast(x), ax_point, &p)) p else null;+}++pub fn size(v: ?*anyopaque) ?CGSize {+ const x = v orelse return null;+ if (CFGetTypeID(x) != AXValueGetTypeID() or AXValueGetType(@ptrCast(x)) != ax_size) return null;+ var s: CGSize = .{};+ return if (AXValueGetValue(@ptrCast(x), ax_size, &s)) s else null;+}++pub fn array(v: ?*anyopaque) ?CFArrayRef {+ const x = v orelse return null;+ return if (CFGetTypeID(x) == CFArrayGetTypeID()) @ptrCast(x) else null;+}++/// Whether this process may use accessibility; with `prompt`, the system+/// asks the user (once) to allow it.+pub fn trusted(prompt: bool) bool {+ if (!prompt) return AXIsProcessTrusted();+ const keys = [_]?*const anyopaque{kAXTrustedCheckOptionPrompt};+ const values = [_]?*const anyopaque{kCFBooleanTrue};+ const opts = CFDictionaryCreate(null, &keys, &values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);+ defer if (opts) |o| CFRelease(o);+ return AXIsProcessTrustedWithOptions(opts);+}++test "CF strings and values read back" {+ const s = str("héllo");+ defer CFRelease(s);+ var buf: [16]u8 = undefined;+ try std.testing.expectEqualStrings("héllo", text(&buf, s));+ try std.testing.expectEqualStrings("hé", utf8Into(buf[0..3], s));+ const d: f64 = 2.5;+ const n = CFNumberCreate(null, number_double, &d).?;+ defer CFRelease(n);+ try std.testing.expectEqualStrings("2.5", text(&buf, n));+ try std.testing.expectEqual(@as(?bool, true), boolean(kCFBooleanTrue));+ try std.testing.expectEqual(@as(?bool, null), boolean(s));+}diff --git a/apps/nanocua/src/mac/hotkey.zig b/apps/nanocua/src/mac/hotkey.zignew file mode 100644index 00000000..973e450d--- /dev/null+++ b/apps/nanocua/src/mac/hotkey.zig@@ -0,0 +1,47 @@+//! The kill switch's key, ⌃⌥⌘. (period), heard wherever the focus is:+//! a Carbon hot key, which needs no permission and arrives on the main+//! thread. Stopping by keyboard matters because an agent may be moving+//! the pointer.+const std = @import("std");++const EventTypeSpec = extern struct { class: u32, kind: u32 };+const HotKeyID = extern struct { signature: u32, id: u32 };+const Handler = *const fn (next: ?*anyopaque, event: ?*anyopaque, user: ?*anyopaque) callconv(.c) i32;+extern "c" fn GetApplicationEventTarget() *anyopaque;+extern "c" fn InstallEventHandler(target: *anyopaque, handler: Handler, n: u32, list: [*]const EventTypeSpec, user: ?*anyopaque, out: ?*?*anyopaque) i32;+extern "c" fn RegisterEventHotKey(code: u32, modifiers: u32, id: HotKeyID, target: *anyopaque, options: u32, out: *?*anyopaque) i32;+extern "c" fn UnregisterEventHotKey(ref: *anyopaque) i32;++const keyboard_class: u32 = 0x6b657962; // 'keyb'+const hot_key_pressed: u32 = 5;+const cmd: u32 = 0x100;+const option: u32 = 0x800;+const control: u32 = 0x1000;+const period: u32 = 47;++pub const label = "⌃⌥⌘.";++pub const HotKey = struct {+ ref: ?*anyopaque = null,+ ctx: *anyopaque,+ func: *const fn (ctx: *anyopaque) void,++ /// Registers the key; `hk` must stay where it is.+ pub fn register(hk: *HotKey) bool {+ const target = GetApplicationEventTarget();+ const spec = [_]EventTypeSpec{.{ .class = keyboard_class, .kind = hot_key_pressed }};+ if (InstallEventHandler(target, pressed, 1, &spec, hk, null) != 0) return false;+ return RegisterEventHotKey(period, cmd | option | control, .{ .signature = 0x6e637561, .id = 1 }, target, 0, &hk.ref) == 0;+ }++ pub fn deinit(hk: *HotKey) void {+ if (hk.ref) |r| _ = UnregisterEventHotKey(r);+ hk.ref = null;+ }++ fn pressed(_: ?*anyopaque, _: ?*anyopaque, user: ?*anyopaque) callconv(.c) i32 {+ const hk: *HotKey = @ptrCast(@alignCast(user orelse return 0));+ hk.func(hk.ctx);+ return 0;+ }+};diff --git a/apps/nanocua/src/mac/input.zig b/apps/nanocua/src/mac/input.zignew file mode 100644index 00000000..c51f113e--- /dev/null+++ b/apps/nanocua/src/mac/input.zig@@ -0,0 +1,158 @@+//! Input as the hardware would make it, posted either into one app, which+//! leaves the user's pointer and keyboard alone, or to the HID event tap, the+//! real thing: clicks (single, double, right), keys with modifiers, text typed+//! as Unicode, scrolling by lines, and moving the pointer.+const std = @import("std");+const keys = @import("../keys.zig");+const cf = @import("cf.zig");++const CGEventRef = *opaque {};+extern "c" fn CGEventCreateMouseEvent(source: ?*anyopaque, kind: u32, at: cf.CGPoint, button: u32) ?CGEventRef;+extern "c" fn CGEventCreateKeyboardEvent(source: ?*anyopaque, code: u16, down: bool) ?CGEventRef;+extern "c" fn CGEventCreateScrollWheelEvent2(source: ?*anyopaque, units: u32, count: u32, wheel1: i32, wheel2: i32, wheel3: i32) ?CGEventRef;+extern "c" fn CGEventKeyboardSetUnicodeString(ev: CGEventRef, len: c_ulong, chars: [*]const u16) void;+extern "c" fn CGEventSetFlags(ev: CGEventRef, flags: u64) void;+extern "c" fn CGEventSetLocation(ev: CGEventRef, at: cf.CGPoint) void;+extern "c" fn CGEventSetIntegerValueField(ev: CGEventRef, field: u32, value: i64) void;+extern "c" fn CGEventPost(tap: u32, ev: CGEventRef) void;+extern "c" fn CGEventPostToPid(pid: i32, ev: CGEventRef) void;+extern "c" fn usleep(us: c_uint) c_int;++const hid_tap: u32 = 0;+const field_click_state: u32 = 1;+const field_window_under_pointer: u32 = 91;+const field_window_handling: u32 = 92;+const mouse_moved: u32 = 5;++pub const Button = enum { left, right };++/// Where events go.+pub const Target = union(enum) {+ /// The real pointer and keyboard.+ hid,+ /// Into one app. `window` is what a mouse event says it is over (0: unknown).+ app: struct { pid: i32, window: u32 = 0 },++ fn post(t: Target, ev: ?CGEventRef) void {+ const e = ev orelse return;+ switch (t) {+ .hid => CGEventPost(hid_tap, e),+ .app => |a| CGEventPostToPid(a.pid, e),+ }+ cf.CFRelease(e);+ }++ /// A mouse event at x, y, saying which window it is over: an app in the+ /// background otherwise goes by where the real pointer is.+ fn mouse(t: Target, kind: u32, x: f64, y: f64, button: u32) ?CGEventRef {+ const ev = CGEventCreateMouseEvent(null, kind, .{ .x = x, .y = y }, button) orelse return null;+ if (t == .app and t.app.window != 0) {+ CGEventSetIntegerValueField(ev, field_window_under_pointer, t.app.window);+ CGEventSetIntegerValueField(ev, field_window_handling, t.app.window);+ }+ return ev;+ }+};++pub fn move(t: Target, x: f64, y: f64) void {+ t.post(t.mouse(mouse_moved, x, y, 0));+}++/// Clicks `count` times at x, y (a double click is two, the second with+/// click state 2, as the system makes it).+pub fn click(t: Target, x: f64, y: f64, button: Button, count: u32) void {+ const down: u32, const up: u32, const b: u32 = switch (button) {+ .left => .{ 1, 2, 0 },+ .right => .{ 3, 4, 1 },+ };+ move(t, x, y);+ _ = usleep(15_000);+ for (1..@min(count, 3) + 1) |n| {+ for ([_]u32{ down, up }) |kind| {+ const ev = t.mouse(kind, x, y, b) orelse continue;+ CGEventSetIntegerValueField(ev, field_click_state, @intCast(n));+ t.post(ev);+ _ = usleep(8_000);+ }+ }+}++/// Presses and releases a combo; false if its key has no code.+pub fn press(t: Target, combo: keys.Combo) bool {+ const code = keys.macCode(combo.key) orelse return false;+ const flags = keys.macFlags(combo.mods);+ for ([_]bool{ true, false }) |down| {+ const ev = CGEventCreateKeyboardEvent(null, code, down) orelse return false;+ CGEventSetFlags(ev, flags);+ t.post(ev);+ _ = usleep(10_000);+ }+ return true;+}++/// Types `s` as Unicode one character per event, whatever the layout, with no+/// modifiers held: apps that read keys one by one (games, terminals) see each.+/// Stops as soon as `stopped` is set; returns the characters typed.+pub fn typeText(t: Target, s: []const u8, stopped: *const std.atomic.Value(bool)) usize {+ var typed: usize = 0;+ var units: [2]u16 = undefined;+ var it = std.unicode.Utf8View.initUnchecked(if (std.unicode.utf8ValidateSlice(s)) s else "").iterator();+ while (it.nextCodepoint()) |cp| {+ if (stopped.load(.acquire)) break;+ typed += 1;+ if (cp == '\n') {+ _ = press(t, .{ .key = "enter" });+ continue;+ }+ unicode(t, units[0..utf16(&units, cp)]);+ }+ return typed;+}++/// `cp` in UTF-16; the units it took.+fn utf16(units: *[2]u16, cp: u21) usize {+ if (cp < 0x10000) {+ units[0] = @intCast(cp);+ return 1;+ }+ const v = cp - 0x10000;+ units[0] = @intCast(0xD800 + (v >> 10));+ units[1] = @intCast(0xDC00 + (v & 0x3ff));+ return 2;+}++fn unicode(t: Target, units: []const u16) void {+ for ([_]bool{ true, false }) |down| {+ const ev = CGEventCreateKeyboardEvent(null, 0, down) orelse continue;+ CGEventSetFlags(ev, 0);+ CGEventKeyboardSetUnicodeString(ev, @intCast(units.len), units.ptr);+ t.post(ev);+ }+ _ = usleep(4_000);+}++/// Scrolls at x, y: `dy` lines down, `dx` lines right.+pub fn scroll(t: Target, x: f64, y: f64, dx: f64, dy: f64) void {+ if (t == .hid) {+ move(t, x, y);+ _ = usleep(15_000);+ }+ const clamp = struct {+ fn f(v: f64) i32 {+ return @intFromFloat(std.math.clamp(@round(v), -200, 200));+ }+ }.f;+ // Wheel 1 is vertical, positive up; wheel 2 horizontal, positive left.+ const ev = CGEventCreateScrollWheelEvent2(null, 1, 2, -clamp(dy), -clamp(dx), 0) orelse return;+ CGEventSetLocation(ev, .{ .x = x, .y = y });+ t.post(ev);+}++test "characters in UTF-16" {+ var u: [2]u16 = undefined;+ try std.testing.expectEqual(@as(usize, 1), utf16(&u, 'é'));+ try std.testing.expectEqual(@as(u16, 0xe9), u[0]);+ try std.testing.expectEqual(@as(usize, 2), utf16(&u, 0x1F600));+ try std.testing.expectEqual(@as(u16, 0xD83D), u[0]);+ try std.testing.expectEqual(@as(u16, 0xDE00), u[1]);+}diff --git a/apps/nanocua/src/mac/windows.zig b/apps/nanocua/src/mac/windows.zignew file mode 100644index 00000000..3bacd7cb--- /dev/null+++ b/apps/nanocua/src/mac/windows.zig@@ -0,0 +1,68 @@+//! An app's main window from the window server's list, which needs no+//! permission for the numbers and frames used here: what a window screenshot+//! captures, and what a background click says it is over.+const std = @import("std");+const cf = @import("cf.zig");++extern "c" fn CGWindowListCopyWindowInfo(option: u32, relative_to: u32) ?cf.CFArrayRef;+extern "c" fn CGRectMakeWithDictionaryRepresentation(dict: cf.CFDictionaryRef, rect: *cf.CGRect) bool;+extern "c" fn CFDictionaryGetValue(dict: cf.CFDictionaryRef, key: *const anyopaque) ?*anyopaque;+extern "c" const kCGWindowNumber: cf.CFStringRef;+extern "c" const kCGWindowOwnerPID: cf.CFStringRef;+extern "c" const kCGWindowLayer: cf.CFStringRef;+extern "c" const kCGWindowBounds: cf.CFStringRef;+extern "c" const kCGWindowIsOnscreen: cf.CFStringRef;++const exclude_desktop: u32 = 16;+const number_sint64: cf.CFIndex = 4;++pub const Window = struct {+ id: u32,+ frame: cf.CGRect,+ onscreen: bool = true,+};++/// Which of two windows is the app's main one: what is on screen before what+/// isn't, then the largest (browsers keep toolbars in slim windows of their own).+pub fn better(a: Window, b: Window) bool {+ if (a.onscreen != b.onscreen) return a.onscreen;+ return a.frame.size.width * a.frame.size.height > b.frame.size.width * b.frame.size.height;+}++fn int(v: ?*anyopaque) ?i64 {+ const x = v orelse return null;+ if (cf.CFGetTypeID(x) != cf.CFNumberGetTypeID()) return null;+ var n: i64 = 0;+ return if (cf.CFNumberGetValue(@ptrCast(x), number_sint64, &n)) n else null;+}++/// App `pid`'s main window, if it has one.+pub fn main(pid: i32) ?Window {+ const list = CGWindowListCopyWindowInfo(exclude_desktop, 0) orelse return null;+ defer cf.CFRelease(list);+ var best: ?Window = null;+ const n: usize = @intCast(cf.CFArrayGetCount(list));+ for (0..n) |i| {+ const d: cf.CFDictionaryRef = @ptrCast(cf.CFArrayGetValueAtIndex(list, @intCast(i)) orelse continue);+ if (int(CFDictionaryGetValue(d, kCGWindowOwnerPID)) != pid) continue;+ if (int(CFDictionaryGetValue(d, kCGWindowLayer)) != 0) continue;+ var w: Window = .{ .id = @intCast(int(CFDictionaryGetValue(d, kCGWindowNumber)) orelse continue), .frame = .{} };+ const bounds = CFDictionaryGetValue(d, kCGWindowBounds) orelse continue;+ if (!CGRectMakeWithDictionaryRepresentation(@ptrCast(bounds), &w.frame)) continue;+ if (w.frame.size.width < 2 or w.frame.size.height < 2) continue;+ w.onscreen = cf.boolean(CFDictionaryGetValue(d, kCGWindowIsOnscreen)) orelse false;+ if (best == null or better(w, best.?)) best = w;+ }+ return best;+}++test "the main window: on screen first, then the largest" {+ const at = struct {+ fn f(w: f64, h: f64, on: bool) Window {+ return .{ .id = 1, .frame = .{ .size = .{ .width = w, .height = h } }, .onscreen = on };+ }+ }.f;+ try std.testing.expect(better(at(1352, 802, true), at(1352, 47, true)));+ try std.testing.expect(!better(at(1352, 47, true), at(1352, 802, true)));+ try std.testing.expect(better(at(10, 10, true), at(1352, 849, false)));+}diff --git a/apps/nanocua/src/main.zig b/apps/nanocua/src/main.zignew file mode 100644index 00000000..238ecaf1--- /dev/null+++ b/apps/nanocua/src/main.zig@@ -0,0 +1,40 @@+//! nanocua: lets AI agents see and use the computer, as far as its user+//! allows. `nanocua` is the app: the control panel, and the server agents+//! reach over a Unix socket. `nanocua mcp` is what an agent runs: an MCP+//! server on stdio that carries its lines to the app.+const std = @import("std");+const nanoui = @import("nanoui");+const app_mod = @import("app.zig");+const theme = @import("theme.zig");++pub fn main(init: std.process.Init) !u8 {+ const args = try init.minimal.args.toSlice(init.arena.allocator());+ if (args.len > 1 and std.mem.eql(u8, args[1], "mcp"))+ return if (comptime @import("server.zig").supported) @import("proxy.zig").run(init) else 1;++ const app = try nanoui.App.create(init, .{ .name = "nanocua" });+ defer app.destroy();+ var plat: app_mod.Platform = .init(init.gpa);+ var a: app_mod.App = undefined;+ a.init(init.gpa, undefined);+ plat.attach(&a);+ defer a.deinit();+ var path_buf: [512]u8 = undefined;+ if (@import("sockpath.zig").path(&path_buf, init.environ_map)) |path| a.listen(path) else |_| {+ a.listen_error = "No folder for the socket (HOME is unset).";+ }+ var exe_buf: [1024]u8 = undefined;+ if (std.process.executablePath(init.io, &exe_buf)) |n| a.setCommand(exe_buf[0..n]) else |_| {}+ a.stopKey();+ a.window = try app.newWindow(.{+ .title = "nanocua",+ .width = 560,+ .height = 720,+ .min_width = 460,+ .min_height = 520,+ .theme = theme.ui(nanoui.isDark()),+ .content = .{ .view = .init(&a, @import("view.zig").view) },+ });+ try app.run();+ return 0;+}diff --git a/apps/nanocua/src/mcp.zig b/apps/nanocua/src/mcp.zignew file mode 100644index 00000000..545f9b92--- /dev/null+++ b/apps/nanocua/src/mcp.zig@@ -0,0 +1,292 @@+//! MCP over newline-delimited JSON-RPC: initialize, ping, tools/list and+//! tools/call. The hub checks each call against the policy, logs it, and+//! hands it to the backend with a Reply, which the backend completes now+//! or later (a screenshot finishes on another queue), on the main thread.+//! Connections are numbers: a reply to one that closed is dropped.+const std = @import("std");+const tools = @import("tools.zig");+const policy = @import("policy.zig");++pub const protocol_version = "2025-06-18";+pub const version = "0.1.0";++/// Where replies go: the server's connection `conn`, one line each.+pub const Sink = struct {+ ptr: *anyopaque,+ send: *const fn (ptr: *anyopaque, conn: u64, line: []const u8) void,+};++/// What runs the tools. `call` copies what it needs from `args` before+/// returning; it completes `reply` exactly once, now or later.+pub const Backend = struct {+ ptr: *anyopaque,+ call: *const fn (ptr: *anyopaque, tool: tools.Tool, args: tools.Args, reply: *Reply) void,+};++pub const Content = union(enum) {+ text: []const u8,+ /// Base64 already.+ image: struct { data: []const u8, mime: []const u8 },+};++pub const Callback = struct {+ ptr: *anyopaque,+ func: *const fn (ptr: *anyopaque) void,+};++pub const Hub = struct {+ gpa: std.mem.Allocator,+ sink: Sink,+ backend: Backend,+ policy: policy.Policy = .{},+ log: policy.Log = .{},+ /// Seconds, for the log.+ clock: *const fn () f64,+ /// Something changed for the panel (a call came or finished).+ changed: ?Callback = null,+ /// Calls not yet answered.+ pending: usize = 0,++ /// Handles one line from connection `conn`.+ pub fn handle(h: *Hub, conn: u64, line: []const u8) void {+ if (std.mem.trim(u8, line, " \t\r\n").len == 0) return;+ var arena: std.heap.ArenaAllocator = .init(h.gpa);+ defer arena.deinit();+ const a = arena.allocator();+ const msg = std.json.parseFromSliceLeaky(std.json.Value, a, line, .{}) catch+ return h.sendError(conn, "null", -32700, "Parse error");+ if (msg != .object) return h.sendError(conn, "null", -32600, "Invalid request");+ const id_v = msg.object.get("id");+ const method_v = msg.object.get("method") orelse return; // a response: nothing asked+ if (method_v != .string) return h.sendError(conn, "null", -32600, "Invalid request");+ // Notifications (initialized, cancelled) need no answer.+ const id_val = id_v orelse return;+ const id = std.json.Stringify.valueAlloc(a, id_val, .{}) catch return;+ const method = method_v.string;+ const params = msg.object.get("params");+ if (std.mem.eql(u8, method, "initialize")) {+ h.sendResult(conn, id, initResult(a, params) catch return);+ } else if (std.mem.eql(u8, method, "ping")) {+ h.sendResult(conn, id, "{}");+ } else if (std.mem.eql(u8, method, "tools/list")) {+ var aw: std.Io.Writer.Allocating = .init(a);+ tools.writeList(&aw.writer) catch return;+ h.sendResult(conn, id, aw.written());+ } else if (std.mem.eql(u8, method, "tools/call")) {+ h.call(conn, id, params);+ } else h.sendError(conn, id, -32601, "Method not found");+ }++ fn call(h: *Hub, conn: u64, id: []const u8, params: ?std.json.Value) void {+ const p = tools.Args.of(params);+ const name = p.string("name") orelse return h.sendError(conn, id, -32602, "tools/call needs a name");+ const tool = tools.Tool.parse(name) orelse return h.sendError(conn, id, -32602, "Unknown tool");+ const args_v: ?std.json.Value = if (p.obj) |o| o.get("arguments") else null;+ var sum_buf: [256]u8 = undefined;+ const sum = summarize(&sum_buf, args_v);+ const seq = h.log.add(tool, sum, h.clock());+ const reply = h.gpa.create(Reply) catch return;+ reply.* = .{ .hub = h, .conn = conn, .seq = seq, .id = h.gpa.dupe(u8, id) catch {+ h.gpa.destroy(reply);+ return;+ } };+ h.pending += 1;+ h.notify();+ const whole_screen = tools.Args.of(args_v).boolean("whole_screen") orelse false;+ if (h.policy.denial(tool, whole_screen)) |why| return reply.finish(&.{.{ .text = why }}, true, .denied);+ h.backend.call(h.backend.ptr, tool, tools.Args.of(args_v), reply);+ }++ fn notify(h: *Hub) void {+ if (h.changed) |c| c.func(c.ptr);+ }++ fn sendResult(h: *Hub, conn: u64, id: []const u8, result: []const u8) void {+ var aw: std.Io.Writer.Allocating = .init(h.gpa);+ defer aw.deinit();+ aw.writer.print("{{\"jsonrpc\":\"2.0\",\"id\":{s},\"result\":{s}}}", .{ id, result }) catch return;+ h.sink.send(h.sink.ptr, conn, aw.written());+ }++ fn sendError(h: *Hub, conn: u64, id: []const u8, code: i32, message: []const u8) void {+ var buf: [256]u8 = undefined;+ const line = std.fmt.bufPrint(&buf, "{{\"jsonrpc\":\"2.0\",\"id\":{s},\"error\":{{\"code\":{d},\"message\":\"{s}\"}}}}", .{ id, code, message }) catch return;+ h.sink.send(h.sink.ptr, conn, line);+ }+};++/// An unanswered tools/call; frees itself once finished.+pub const Reply = struct {+ hub: *Hub,+ conn: u64,+ id: []u8,+ seq: u64,++ pub fn text(r: *Reply, s: []const u8) void {+ r.finish(&.{.{ .text = s }}, false, .ok);+ }++ /// A failure the model should read (no permission, no such element).+ pub fn fail(r: *Reply, s: []const u8) void {+ r.finish(&.{.{ .text = s }}, true, .failed);+ }++ pub fn failf(r: *Reply, comptime fmt: []const u8, args: anytype) void {+ var buf: [512]u8 = undefined;+ r.fail(std.fmt.bufPrint(&buf, fmt, args) catch fmt);+ }++ pub fn textf(r: *Reply, comptime fmt: []const u8, args: anytype) void {+ var buf: [512]u8 = undefined;+ r.text(std.fmt.bufPrint(&buf, fmt, args) catch fmt);+ }++ pub fn finish(r: *Reply, content: []const Content, is_error: bool, status: policy.Status) void {+ const h = r.hub;+ defer {+ h.pending -= 1;+ h.gpa.free(r.id);+ h.gpa.destroy(r);+ h.notify();+ }+ var detail: []const u8 = "";+ for (content) |c| if (c == .text) {+ detail = c.text;+ break;+ } else {+ detail = "image";+ };+ h.log.finish(r.seq, status, detail, h.clock());+ var aw: std.Io.Writer.Allocating = .init(h.gpa);+ defer aw.deinit();+ writeResult(&aw.writer, r.id, content, is_error) catch return;+ h.sink.send(h.sink.ptr, r.conn, aw.written());+ }+};++/// One tools/call result as a JSON-RPC line, without a Reply.+pub fn writeResult(w: *std.Io.Writer, id: []const u8, content: []const Content, is_error: bool) !void {+ try w.print("{{\"jsonrpc\":\"2.0\",\"id\":{s},\"result\":{{\"content\":[", .{id});+ for (content, 0..) |c, i| {+ if (i > 0) try w.writeByte(',');+ switch (c) {+ .text => |t| {+ try w.writeAll("{\"type\":\"text\",\"text\":");+ try std.json.Stringify.encodeJsonString(t, .{}, w);+ try w.writeByte('}');+ },+ .image => |img| try w.print("{{\"type\":\"image\",\"data\":\"{s}\",\"mimeType\":\"{s}\"}}", .{ img.data, img.mime }),+ }+ }+ try w.print("],\"isError\":{}}}}}", .{is_error});+}++fn initResult(a: std.mem.Allocator, params: ?std.json.Value) ![]const u8 {+ // Answer in the client's version when it asks for one we know.+ const known = [_][]const u8{ "2024-11-05", "2025-03-26", "2025-06-18" };+ var ver: []const u8 = protocol_version;+ if (tools.Args.of(params).string("protocolVersion")) |v| for (known) |k| {+ if (std.mem.eql(u8, v, k)) ver = k;+ };+ var aw: std.Io.Writer.Allocating = .init(a);+ try aw.writer.print("{{\"protocolVersion\":\"{s}\",\"capabilities\":{{\"tools\":{{\"listChanged\":false}}}},\"serverInfo\":{{\"name\":\"nanocua\",\"version\":\"{s}\"}},\"instructions\":", .{ ver, version });+ try std.json.Stringify.encodeJsonString(instructions, .{}, &aw.writer);+ try aw.writer.writeByte('}');+ return aw.written();+}++pub const instructions =+ \\nanocua lets you use apps on this computer, within what its user allows in the nanocua window.+ \\Use it for another app's real UI; to show or test your own work, prefer the project's own commands.+ \\You work inside one app in the background: the user's pointer and keyboard stay theirs. Look first:+ \\get_ui_tree gives exact elements with refs and makes that app the one you act in; act on refs (click,+ \\type_text, set_value). Use screenshot when the tree is not enough. Coordinates are screen points from+ \\the top-left. If a call says nanocua is stopped, or input or the whole screen is off, tell the user+ \\once and carry on without it; do not retry in a loop, and permissions are the user's to give.+;++/// The arguments as one short line for the log.+fn summarize(buf: []u8, args: ?std.json.Value) []const u8 {+ const v = args orelse return "";+ var w: std.Io.Writer = .fixed(buf);+ std.json.Stringify.value(v, .{}, &w) catch {};+ const s = w.buffered();+ if (std.mem.eql(u8, s, "{}")) return "";+ return s;+}++const testing = std.testing;++/// Lines sent, for the tests.+const Lines = struct {+ gpa: std.mem.Allocator,+ list: std.ArrayList(u8) = .empty,+ fn send(ptr: *anyopaque, conn: u64, line: []const u8) void {+ const l: *Lines = @ptrCast(@alignCast(ptr));+ l.list.print(l.gpa, "{d}:{s}\n", .{ conn, line }) catch {};+ }+};++/// Answers clicks at once and keeps screenshots waiting, as the real one.+const Fake = struct {+ held: ?*Reply = null,+ fn call(ptr: *anyopaque, tool: tools.Tool, args: tools.Args, reply: *Reply) void {+ const f: *Fake = @ptrCast(@alignCast(ptr));+ switch (tool) {+ .screenshot => f.held = reply,+ .click => if (args.point()) |p| reply.textf("clicked {d},{d}", .{ p[0], p[1] }) else reply.fail("no target"),+ else => reply.text("ok"),+ }+ }+};++fn zero() f64 {+ return 0;+}++test "a session: initialize, list, calls answered now and later, the policy" {+ var lines: Lines = .{ .gpa = testing.allocator };+ defer lines.list.deinit(testing.allocator);+ var fake: Fake = .{};+ var h: Hub = .{ .gpa = testing.allocator, .clock = zero, .sink = .{ .ptr = &lines, .send = Lines.send }, .backend = .{ .ptr = &fake, .call = Fake.call } };+ h.handle(1, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2025-03-26\"}}");+ h.handle(1, "{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}");+ h.handle(1, "{\"jsonrpc\":\"2.0\",\"id\":\"two\",\"method\":\"tools/list\"}");+ try testing.expect(std.mem.indexOf(u8, lines.list.items, "\"protocolVersion\":\"2025-03-26\"") != null);+ try testing.expect(std.mem.indexOf(u8, lines.list.items, "1:{\"jsonrpc\":\"2.0\",\"id\":\"two\",\"result\":{\"tools\":[") != null);+ lines.list.clearRetainingCapacity();++ // Input is off: the click is refused, and logged as such.+ h.handle(2, "{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"tools/call\",\"params\":{\"name\":\"click\",\"arguments\":{\"x\":5,\"y\":6}}}");+ try testing.expect(std.mem.indexOf(u8, lines.list.items, "\"isError\":true") != null);+ try testing.expectEqual(policy.Status.denied, h.log.newest(0).?.status);+ try testing.expectEqualStrings("{\"x\":5,\"y\":6}", h.log.newest(0).?.summary());+ h.policy.allow_input = true;+ lines.list.clearRetainingCapacity();+ h.handle(2, "{\"jsonrpc\":\"2.0\",\"id\":4,\"method\":\"tools/call\",\"params\":{\"name\":\"click\",\"arguments\":{\"x\":5,\"y\":6}}}");+ try testing.expectEqualStrings("2:{\"jsonrpc\":\"2.0\",\"id\":4,\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"clicked 5,6\"}],\"isError\":false}}\n", lines.list.items);++ // A screenshot answers later; the hub counts it until then.+ lines.list.clearRetainingCapacity();+ h.handle(2, "{\"jsonrpc\":\"2.0\",\"id\":5,\"method\":\"tools/call\",\"params\":{\"name\":\"screenshot\"}}");+ try testing.expectEqual(@as(usize, 1), h.pending);+ try testing.expectEqual(@as(usize, 0), lines.list.items.len);+ fake.held.?.finish(&.{ .{ .image = .{ .data = "QUJD", .mime = "image/jpeg" } }, .{ .text = "1:1" } }, false, .ok);+ try testing.expectEqual(@as(usize, 0), h.pending);+ try testing.expect(std.mem.indexOf(u8, lines.list.items, "{\"type\":\"image\",\"data\":\"QUJD\",\"mimeType\":\"image/jpeg\"}") != null);++ // Errors: bad JSON, unknown methods and tools.+ lines.list.clearRetainingCapacity();+ h.handle(3, "{nope");+ h.handle(3, "{\"jsonrpc\":\"2.0\",\"id\":6,\"method\":\"resources/list\"}");+ h.handle(3, "{\"jsonrpc\":\"2.0\",\"id\":7,\"method\":\"tools/call\",\"params\":{\"name\":\"rm_rf\"}}");+ try testing.expect(std.mem.indexOf(u8, lines.list.items, "-32700") != null);+ try testing.expect(std.mem.indexOf(u8, lines.list.items, "-32601") != null);+ try testing.expect(std.mem.indexOf(u8, lines.list.items, "-32602") != null);++ // Stopped: even looking is refused.+ h.policy.stopped = true;+ lines.list.clearRetainingCapacity();+ h.handle(3, "{\"jsonrpc\":\"2.0\",\"id\":8,\"method\":\"tools/call\",\"params\":{\"name\":\"list_apps\"}}");+ try testing.expect(std.mem.indexOf(u8, lines.list.items, "stopped") != null);+}diff --git a/apps/nanocua/src/policy.zig b/apps/nanocua/src/policy.zignew file mode 100644index 00000000..02a57d18--- /dev/null+++ b/apps/nanocua/src/policy.zig@@ -0,0 +1,169 @@+//! What the user allows, and what agents did. Stopped refuses everything;+//! input (clicks, keys, launching) is refused until the user turns it on;+//! the whole screen (the real pointer and keyboard, pictures of the display)+//! is refused until they turn that on too, so by default agents stay inside+//! one app; nanocua's own windows are never acted on (an agent can't press+//! its own Resume). Every call is logged: what, with what, and how it went.+const std = @import("std");+const tools = @import("tools.zig");++pub const Policy = struct {+ /// The kill switch: nothing runs until the user resumes.+ stopped: bool = false,+ /// Clicks, keys, scrolling, launching apps.+ allow_input: bool = false,+ /// The real pointer and keyboard, and pictures of the whole display.+ allow_whole_screen: bool = false,++ /// Why `tool` may not run now, or null if it may. `whole_screen`: the+ /// call asked for more than one app.+ pub fn denial(p: Policy, tool: tools.Tool, whole_screen: bool) ?[]const u8 {+ if (p.stopped) return "nanocua is stopped: the user pressed Stop. Wait for them to resume it.";+ if (tool.kind() == .act and !p.allow_input)+ return "Input is off in nanocua: you may look (list_apps, get_ui_tree, screenshot) but not act. Ask the user to turn on \"Allow input\".";+ if (whole_screen and !p.allow_whole_screen and tool != .list_apps and tool != .get_ui_tree)+ return "The whole screen is off in nanocua: work inside one app (leave out whole_screen). If that can't work, ask the user to turn on \"Allow whole screen\".";+ return null;+ }+};++pub const Status = enum { running, ok, failed, denied };++pub const Entry = struct {+ seq: u64,+ tool: tools.Tool,+ status: Status = .running,+ /// When it started, by the hub's clock; how long it took.+ at: f64,+ ms: f32 = 0,+ summary_buf: [96]u8 = undefined,+ summary_len: u8 = 0,+ detail_buf: [120]u8 = undefined,+ detail_len: u8 = 0,++ pub fn summary(e: *const Entry) []const u8 {+ return e.summary_buf[0..e.summary_len];+ }++ pub fn detail(e: *const Entry) []const u8 {+ return e.detail_buf[0..e.detail_len];+ }+};++/// The last `capacity` calls, newest last.+pub const Log = struct {+ pub const capacity = 200;+ entries: [capacity]Entry = undefined,+ len: usize = 0,+ /// Where the oldest entry is once the ring is full.+ head: usize = 0,+ next_seq: u64 = 1,+ /// Counts since launch.+ calls: u64 = 0,+ denied: u64 = 0,++ pub fn add(l: *Log, tool: tools.Tool, summary: []const u8, at: f64) u64 {+ const e: Entry = .{ .seq = l.next_seq, .tool = tool, .at = at };+ l.next_seq += 1;+ l.calls += 1;+ const slot = if (l.len < capacity) blk: {+ l.len += 1;+ break :blk l.len - 1;+ } else blk: {+ const s = l.head;+ l.head = (l.head + 1) % capacity;+ break :blk s;+ };+ l.entries[slot] = e;+ const n = clip(&l.entries[slot].summary_buf, summary);+ l.entries[slot].summary_len = @intCast(n);+ return e.seq;+ }++ pub fn finish(l: *Log, seq: u64, status: Status, detail: []const u8, now: f64) void {+ const e = l.find(seq) orelse return;+ e.status = status;+ if (status == .denied) l.denied += 1;+ e.ms = @floatCast(@max(0, (now - e.at) * 1000));+ e.detail_len = @intCast(clip(&e.detail_buf, detail));+ }++ pub fn find(l: *Log, seq: u64) ?*Entry {+ for (l.entries[0..l.len]) |*e| if (e.seq == seq) return e;+ return null;+ }++ /// The `i`th newest entry (0 is the newest).+ pub fn newest(l: *Log, i: usize) ?*const Entry {+ if (i >= l.len) return null;+ const oldest_first = l.len - 1 - i;+ return &l.entries[(l.head + oldest_first) % l.len];+ }++ pub fn clear(l: *Log) void {+ l.len = 0;+ l.head = 0;+ }+};++/// Copies `s` into `buf`, cut at a character's start with "…" when long,+/// on one line.+fn clip(buf: []u8, s: []const u8) usize {+ const ell = "…";+ var n = s.len;+ var cut = false;+ if (n > buf.len) {+ n = buf.len - ell.len;+ while (n > 0 and (s[n] & 0xc0) == 0x80) n -= 1;+ cut = true;+ }+ for (s[0..n], 0..) |ch, i| buf[i] = if (ch == '\n' or ch == '\r' or ch == '\t') ' ' else ch;+ if (cut) {+ @memcpy(buf[n..][0..ell.len], ell);+ n += ell.len;+ }+ return n;+}++test "stopped refuses everything; input waits for the user" {+ var p: Policy = .{};+ try std.testing.expect(p.denial(.get_ui_tree, false) == null);+ try std.testing.expect(p.denial(.click, false) != null);+ p.allow_input = true;+ try std.testing.expect(p.denial(.click, false) == null);+ p.stopped = true;+ try std.testing.expect(p.denial(.screenshot, false) != null);+ try std.testing.expect(p.denial(.click, false) != null);+}++test "the whole screen waits for the user, one app doesn't" {+ var p: Policy = .{ .allow_input = true };+ try std.testing.expect(p.denial(.screenshot, false) == null);+ try std.testing.expect(p.denial(.click, false) == null);+ try std.testing.expect(p.denial(.screenshot, true) != null);+ try std.testing.expect(p.denial(.click, true) != null);+ try std.testing.expect(p.denial(.press_key, true) != null);+ try std.testing.expect(p.denial(.get_ui_tree, true) == null); // reading an app is one app anyway+ p.allow_whole_screen = true;+ try std.testing.expect(p.denial(.screenshot, true) == null);+ try std.testing.expect(p.denial(.click, true) == null);+ p.allow_input = false;+ try std.testing.expect(p.denial(.click, true) != null); // input stays its own switch+}++test "the log keeps the newest calls, clipped to a line" {+ var l: Log = .{};+ const first = l.add(.click, "a\nb", 1);+ l.finish(first, .ok, "pressed", 1.25);+ try std.testing.expectEqualStrings("a b", l.newest(0).?.summary());+ try std.testing.expectEqual(@as(f32, 250), l.newest(0).?.ms);+ const long: [200]u8 = @splat('x');+ for (0..Log.capacity + 5) |i| _ = l.add(.scroll, &long, @floatFromInt(i));+ try std.testing.expectEqual(@as(usize, Log.capacity), l.len);+ try std.testing.expect(l.find(first) == null);+ const e = l.newest(0).?;+ try std.testing.expectEqual(l.next_seq - 1, e.seq);+ try std.testing.expect(std.mem.endsWith(u8, e.summary(), "…"));+ try std.testing.expect(e.summary().len <= 96);+ try std.testing.expectEqual(l.next_seq - Log.capacity, l.newest(Log.capacity - 1).?.seq);+}diff --git a/apps/nanocua/src/proxy.zig b/apps/nanocua/src/proxy.zignew file mode 100644index 00000000..fc1e7907--- /dev/null+++ b/apps/nanocua/src/proxy.zig@@ -0,0 +1,198 @@+//! `nanocua mcp`: what an agent runs as its MCP server. Handshakes and the+//! tool list are answered here, so adding nanocua to an agent starts nothing+//! and shows nothing. The first tools/call connects to the app, opening it (in+//! the background, through Launch Services) if it isn't running; from then on+//! calls go to its socket and its answers come back. The app does the work, so+//! macOS asks once for nanocua's permissions, not for each terminal or agent.+const std = @import("std");+const builtin = @import("builtin");+const nanoui = @import("nanoui");+const sockpath = @import("sockpath.zig");+const server = @import("server.zig");+const mcp = @import("mcp.zig");+const tools = @import("tools.zig");++extern "c" fn socket(domain: c_int, kind: c_int, protocol: c_int) c_int;+extern "c" fn close(fd: c_int) c_int;+extern "c" fn usleep(us: c_uint) c_int;+extern "c" fn _NSGetExecutablePath(buf: [*]u8, size: *u32) c_int;++const stdout = 1;++pub fn run(init: std.process.Init) !u8 {+ var buf: [512]u8 = undefined;+ var p: Proxy = .{+ .gpa = init.gpa,+ .path = try sockpath.path(&buf, init.environ_map),+ .hub = .{+ .gpa = init.gpa,+ .sink = .{ .ptr = undefined, .send = toStdout },+ .backend = .{ .ptr = undefined, .call = unreachableCall },+ .clock = zero,+ },+ };+ defer p.deinit();+ p.pump();+ return 0;+}++fn zero() f64 {+ return 0;+}++fn toStdout(_: *anyopaque, _: u64, line: []const u8) void {+ _ = server.writeAll(stdout, line);+ _ = server.writeAll(stdout, "\n");+}++/// Every tools/call goes to the app (Proxy.line): the hub here never runs one.+fn unreachableCall(_: *anyopaque, _: tools.Tool, _: tools.Args, reply: *mcp.Reply) void {+ reply.fail("nanocua mcp can't run tools itself");+}++/// Whether a line needs the app: only a tools/call does.+pub fn needsApp(gpa: std.mem.Allocator, line: []const u8) bool {+ var arena: std.heap.ArenaAllocator = .init(gpa);+ defer arena.deinit();+ const msg = std.json.parseFromSliceLeaky(std.json.Value, arena.allocator(), line, .{}) catch return false;+ if (msg != .object) return false;+ const method = msg.object.get("method") orelse return false;+ return method == .string and std.mem.eql(u8, method.string, "tools/call");+}++/// The answer to a tools/call when the app can't be reached.+pub fn unreachableLine(a: std.mem.Allocator, line: []const u8, why: []const u8) ![]const u8 {+ const msg = try std.json.parseFromSliceLeaky(std.json.Value, a, line, .{});+ const id = try std.json.Stringify.valueAlloc(a, msg.object.get("id") orelse .null, .{});+ var aw: std.Io.Writer.Allocating = .init(a);+ try mcp.writeResult(&aw.writer, id, &.{.{ .text = why }}, true);+ return aw.written();+}++const not_running = "nanocua isn't running and couldn't be opened. Ask the user to open nanocua.app.";+const not_listening = "nanocua didn't start listening. Ask the user to open nanocua.app and check its window.";++const Proxy = struct {+ gpa: std.mem.Allocator,+ path: [:0]const u8,+ hub: mcp.Hub,+ /// The app's socket, once a call has needed it.+ fd: ?c_int = null,+ lines: server.Lines = .{},+ /// stdin ended or the app went away.+ done: bool = false,++ fn deinit(p: *Proxy) void {+ p.lines.deinit(p.gpa);+ if (p.fd) |fd| _ = close(fd);+ }++ /// stdin to the hub or the socket, the socket to stdout, until either ends.+ fn pump(p: *Proxy) void {+ var buf: [64 * 1024]u8 = undefined;+ while (!p.done) {+ var fds = [_]std.posix.pollfd{+ .{ .fd = 0, .events = std.posix.POLL.IN, .revents = 0 },+ // A negative fd is ignored.+ .{ .fd = p.fd orelse -1, .events = std.posix.POLL.IN, .revents = 0 },+ };+ _ = std.posix.poll(&fds, -1) catch return;+ if (fds[1].revents != 0) {+ const n = std.c.read(fds[1].fd, &buf, buf.len);+ // The app quit. Exiting closes the agent's end, so it can+ // start this again, rather than wait for answers forever.+ if (n <= 0 or !server.writeAll(stdout, buf[0..@intCast(n)])) return;+ }+ if (fds[0].revents != 0) {+ const n = std.c.read(0, &buf, buf.len);+ if (n <= 0 or !p.lines.feed(p.gpa, buf[0..@intCast(n)], p, line)) return;+ }+ }+ }++ fn line(p: *Proxy, l: []const u8) void {+ if (!needsApp(p.gpa, l)) return p.hub.handle(0, l);+ const fd = p.fd orelse p.open(l) orelse return;+ if (!server.writeAll(fd, l) or !server.writeAll(fd, "\n")) p.done = true;+ }++ /// Connects to the app, opening it if need be. On failure answers the+ /// call that wanted it and returns null: the next call tries again.+ fn open(p: *Proxy, l: []const u8) ?c_int {+ if (connect(p.path)) |fd| return p.keep(fd);+ if (!launch()) {+ p.answer(l, not_running);+ return null;+ }+ // The app starts in well under this; its first launch may ask the+ // user something first.+ for (0..100) |_| {+ _ = usleep(100_000);+ if (connect(p.path)) |fd| return p.keep(fd);+ }+ p.answer(l, not_listening);+ return null;+ }++ fn keep(p: *Proxy, fd: c_int) c_int {+ p.fd = fd;+ return fd;+ }++ fn answer(p: *Proxy, l: []const u8, why: []const u8) void {+ var arena: std.heap.ArenaAllocator = .init(p.gpa);+ defer arena.deinit();+ const reply = unreachableLine(arena.allocator(), l, why) catch return;+ toStdout(undefined, 0, reply);+ }+};++fn connect(path: [:0]const u8) ?c_int {+ var addr: std.c.sockaddr.un = .{ .path = @splat(0) };+ if (path.len >= addr.path.len) return null;+ @memcpy(addr.path[0..path.len], path);+ const fd = socket(1, 1, 0);+ if (fd < 0) return null;+ if (std.c.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) {+ _ = close(fd);+ return null;+ }+ return fd;+}++/// Opens nanocua.app if this program is inside it.+fn launch() bool {+ if (builtin.os.tag != .macos) return false;+ var buf: [1024]u8 = undefined;+ var size: u32 = buf.len;+ if (_NSGetExecutablePath(&buf, &size) != 0) return false;+ const exe = std.mem.sliceTo(&buf, 0);+ const end = std.mem.indexOf(u8, exe, ".app/Contents/MacOS/") orelse return false;+ const objc = nanoui.objc;+ const pool = objc.Pool.push();+ defer pool.pop();+ const url = objc.msg(objc.Object, objc.class("NSURL"), "fileURLWithPath:", .{objc.nsString(exe[0 .. end + 4])});+ const config = objc.msg(objc.Object, objc.class("NSWorkspaceOpenConfiguration"), "configuration", .{});+ // Not in front of what the user is doing.+ objc.msg(void, config, "setActivates:", .{false});+ const ws = objc.msg(objc.Object, objc.class("NSWorkspace"), "sharedWorkspace", .{});+ objc.msg(void, ws, "openApplicationAtURL:configuration:completionHandler:", .{ url, config, @as(?*anyopaque, null) });+ return true;+}++test "only a tools/call needs the app" {+ const gpa = std.testing.allocator;+ try std.testing.expect(needsApp(gpa, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"click\"}}"));+ try std.testing.expect(!needsApp(gpa, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\"}"));+ try std.testing.expect(!needsApp(gpa, "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/list\"}"));+ try std.testing.expect(!needsApp(gpa, "{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}"));+ try std.testing.expect(!needsApp(gpa, "{nope"));+}++test "a call the app can't take is answered as the tool's error, with its id" {+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);+ defer arena.deinit();+ const out = try unreachableLine(arena.allocator(), "{\"id\":\"a\",\"method\":\"tools/call\"}", "open it");+ try std.testing.expect(std.mem.indexOf(u8, out, "\"id\":\"a\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, out, "\"isError\":true") != null);+}diff --git a/apps/nanocua/src/server.zig b/apps/nanocua/src/server.zignew file mode 100644index 00000000..9db4f941--- /dev/null+++ b/apps/nanocua/src/server.zig@@ -0,0 +1,193 @@+//! The app's end of the socket: accepts agents' connections on the main+//! thread (a dispatch source on macOS, the GTK loop on Linux; nothing+//! polls), splits what they send into lines for the hub, and writes the+//! hub's answers back. A connection is a number to the hub, so an answer+//! for one that closed is dropped. Not on Windows yet.+const std = @import("std");+const builtin = @import("builtin");+const nanoui = @import("nanoui");+const mcp = @import("mcp.zig");++pub const supported = builtin.os.tag == .macos or (builtin.os.tag == .linux and !builtin.abi.isAndroid());+const fdwatch = nanoui.fdwatch;++extern "c" fn socket(domain: c_int, kind: c_int, protocol: c_int) c_int;+extern "c" fn close(fd: c_int) c_int;+extern "c" fn chmod(path: [*:0]const u8, mode: c_uint) c_int;+extern "c" fn signal(sig: c_int, handler: usize) usize;+const af_unix = 1;+const sock_stream = 1;+const sigpipe = 13;++/// Lines out of a stream of bytes; a line longer than `max` closes it.+pub const Lines = struct {+ pub const max = 32 << 20;+ buf: std.ArrayList(u8) = .empty,++ /// Calls `f(ctx, line)` for each whole line in what came; false if a+ /// line grew too long.+ pub fn feed(l: *Lines, gpa: std.mem.Allocator, bytes: []const u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), []const u8) void) bool {+ l.buf.appendSlice(gpa, bytes) catch return false;+ var start: usize = 0;+ while (std.mem.indexOfScalarPos(u8, l.buf.items, start, '\n')) |nl| {+ f(ctx, std.mem.trimEnd(u8, l.buf.items[start..nl], "\r"));+ start = nl + 1;+ }+ const rest = l.buf.items.len - start;+ std.mem.copyForwards(u8, l.buf.items[0..rest], l.buf.items[start..]);+ l.buf.shrinkRetainingCapacity(rest);+ return rest <= max;+ }++ pub fn deinit(l: *Lines, gpa: std.mem.Allocator) void {+ l.buf.deinit(gpa);+ }+};++const Conn = struct {+ server: *Server,+ id: u64,+ fd: c_int,+ watch: ?fdwatch.Watch = null,+ lines: Lines = .{},++ fn readable(ctx: ?*anyopaque) callconv(.c) void {+ if (comptime !supported) return;+ const c: *Conn = @ptrCast(@alignCast(ctx));+ var buf: [64 * 1024]u8 = undefined;+ const n = std.c.read(c.fd, &buf, buf.len);+ if (n <= 0) return c.server.drop(c.id);+ if (!c.lines.feed(c.server.gpa, buf[0..@intCast(n)], c, line)) c.server.drop(c.id);+ }++ fn line(c: *Conn, l: []const u8) void {+ c.server.hub.handle(c.id, l);+ }+};++pub const Server = struct {+ gpa: std.mem.Allocator,+ hub: *mcp.Hub,+ fd: c_int = -1,+ watch: ?fdwatch.Watch = null,+ conns: std.AutoArrayHashMapUnmanaged(u64, *Conn) = .empty,+ next_id: u64 = 1,+ path: ?[:0]const u8 = null,++ pub fn sink(s: *Server) mcp.Sink {+ return .{ .ptr = s, .send = send };+ }++ /// Listens at `path`; error.AlreadyRunning if another nanocua answers there.+ pub fn listen(s: *Server, path: [:0]const u8) !void {+ if (!supported) return error.Unsupported;+ _ = signal(sigpipe, 1); // SIG_IGN: a closed agent is a failed write+ var addr: std.c.sockaddr.un = .{ .path = @splat(0) };+ if (path.len >= addr.path.len) return error.NameTooLong;+ @memcpy(addr.path[0..path.len], path);+ const probe = socket(af_unix, sock_stream, 0);+ if (probe >= 0) {+ defer _ = close(probe);+ if (std.c.connect(probe, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) == 0) return error.AlreadyRunning;+ }+ _ = std.c.unlink(path);+ const fd = socket(af_unix, sock_stream, 0);+ if (fd < 0) return error.Socket;+ errdefer _ = close(fd);+ if (std.c.bind(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) return error.Bind;+ _ = chmod(path, 0o600);+ if (std.c.listen(fd, 8) != 0) return error.Listen;+ s.watch = try fdwatch.readable(fd, s, accepted);+ s.fd = fd;+ s.path = try s.gpa.dupeSentinel(u8, path, 0);+ }++ fn accepted(ctx: ?*anyopaque) callconv(.c) void {+ const s: *Server = @ptrCast(@alignCast(ctx));+ const fd = std.c.accept(s.fd, null, null);+ if (fd < 0) return;+ _ = s.adopt(fd) catch {+ _ = close(fd);+ };+ }++ /// Serves an open stream (an accepted socket; a socket pair in tests).+ pub fn adopt(s: *Server, fd: c_int) !u64 {+ const c = try s.gpa.create(Conn);+ errdefer s.gpa.destroy(c);+ c.* = .{ .server = s, .id = s.next_id, .fd = fd };+ try s.conns.put(s.gpa, c.id, c);+ errdefer _ = s.conns.swapRemove(c.id);+ c.watch = try fdwatch.readable(fd, c, Conn.readable);+ s.next_id += 1;+ s.changed();+ return c.id;+ }++ pub fn clients(s: *const Server) usize {+ return s.conns.count();+ }++ fn changed(s: *Server) void {+ if (s.hub.changed) |c| c.func(c.ptr);+ }++ fn send(ptr: *anyopaque, id: u64, line: []const u8) void {+ const s: *Server = @ptrCast(@alignCast(ptr));+ const c = s.conns.get(id) orelse return;+ if (!writeAll(c.fd, line) or !writeAll(c.fd, "\n")) s.drop(id);+ }++ fn drop(s: *Server, id: u64) void {+ const kv = s.conns.fetchSwapRemove(id) orelse return;+ const c = kv.value;+ if (c.watch) |w| w.cancel();+ _ = close(c.fd);+ c.lines.deinit(s.gpa);+ s.gpa.destroy(c);+ s.changed();+ }++ pub fn deinit(s: *Server) void {+ while (s.conns.count() > 0) s.drop(s.conns.keys()[0]);+ s.conns.deinit(s.gpa);+ if (s.watch) |w| w.cancel();+ if (s.fd >= 0) _ = close(s.fd);+ if (s.path) |p| {+ _ = std.c.unlink(p);+ s.gpa.free(p);+ }+ }+};++/// Writes all of `bytes`, waiting for a slow reader; false once it's gone.+pub fn writeAll(fd: c_int, bytes: []const u8) bool {+ if (comptime !supported) return false;+ var rest = bytes;+ while (rest.len > 0) {+ const n = std.c.write(fd, rest.ptr, rest.len);+ if (n < 0 and std.c._errno().* == @backingInt(std.c.E.INTR)) continue;+ if (n <= 0) return false;+ rest = rest[@intCast(n)..];+ }+ return true;+}++test "lines split across reads, with CRLF, and too long" {+ const gpa = std.testing.allocator;+ var l: Lines = .{};+ defer l.deinit(gpa);+ var got: std.ArrayList(u8) = .empty;+ defer got.deinit(gpa);+ const Ctx = struct {+ got: *std.ArrayList(u8),+ fn f(c: @This(), s: []const u8) void {+ c.got.print(std.testing.allocator, "<{s}>", .{s}) catch {};+ }+ };+ const ctx: Ctx = .{ .got = &got };+ try std.testing.expect(l.feed(gpa, "{\"a\":1}\n{\"b\"", ctx, Ctx.f));+ try std.testing.expect(l.feed(gpa, ":2}\r\n\n", ctx, Ctx.f));+ try std.testing.expectEqualStrings("<{\"a\":1}><{\"b\":2}><>", got.items);+ try std.testing.expectEqual(@as(usize, 0), l.buf.items.len);+}diff --git a/apps/nanocua/src/sockpath.zig b/apps/nanocua/src/sockpath.zignew file mode 100644index 00000000..88ef9202--- /dev/null+++ b/apps/nanocua/src/sockpath.zig@@ -0,0 +1,42 @@+//! Where the app listens and `nanocua mcp` connects: a Unix socket only+//! this user can open, in nanocua's Application Support folder on macOS,+//! the user's runtime folder on Linux; NANOCUA_SOCKET overrides it.+const std = @import("std");+const builtin = @import("builtin");++extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;+extern "c" fn getuid() c_uint;++pub fn path(buf: []u8, env: *const std.process.Environ.Map) ![:0]const u8 {+ if (env.get("NANOCUA_SOCKET")) |p| return std.fmt.bufPrintSentinel(buf, "{s}", .{p}, 0);+ if (builtin.os.tag == .macos) {+ const home = env.get("HOME") orelse return error.NoHome;+ return std.fmt.bufPrintSentinel(buf, "{s}/Library/Application Support/nanocua/mcp.sock", .{home}, 0);+ }+ if (env.get("XDG_RUNTIME_DIR")) |d| return std.fmt.bufPrintSentinel(buf, "{s}/nanocua.sock", .{d}, 0);+ if (builtin.os.tag == .windows) return error.Unsupported;+ return std.fmt.bufPrintSentinel(buf, "/tmp/nanocua-{d}.sock", .{getuid()}, 0);+}++/// Makes the socket's folder (readable by this user only) if it is missing.+pub fn makeDir(p: []const u8) void {+ const dir = std.fs.path.dirname(p) orelse return;+ var buf: [512]u8 = undefined;+ const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{dir}, 0) catch return;+ _ = mkdir(z, 0o700);+}++test "the path: overridden, or the platform's" {+ var env: std.process.Environ.Map = .init(std.testing.allocator);+ defer env.deinit();+ var buf: [256]u8 = undefined;+ try env.put("HOME", "/Users/ada");+ try env.put("XDG_RUNTIME_DIR", "/run/user/501");+ const p = try path(&buf, &env);+ if (builtin.os.tag == .macos)+ try std.testing.expectEqualStrings("/Users/ada/Library/Application Support/nanocua/mcp.sock", p)+ else+ try std.testing.expectEqualStrings("/run/user/501/nanocua.sock", p);+ try env.put("NANOCUA_SOCKET", "/tmp/x.sock");+ try std.testing.expectEqualStrings("/tmp/x.sock", try path(&buf, &env));+}diff --git a/apps/nanocua/src/target.zig b/apps/nanocua/src/target.zignew file mode 100644index 00000000..3549d5eb--- /dev/null+++ b/apps/nanocua/src/target.zig@@ -0,0 +1,47 @@+//! Where a call goes. By default into one app: the one it names, else the+//! one the agent last read, and never nanocua itself. The whole screen (the+//! real pointer and keyboard) only when the call asks and the user allows.+const std = @import("std");++pub const Ask = struct {+ /// The call passed whole_screen.+ whole_screen: bool = false,+ /// The user's switch, as it is when the call runs.+ allowed: bool = false,+ /// The app the call names, by pid or name, or the one its ref is in.+ pid: ?i32 = null,+ /// The app the agent last read or opened.+ last: ?i32 = null,+ self: i32,+};++pub const Route = union(enum) {+ whole_screen,+ app: i32,+ refused: []const u8,+};++pub fn route(a: Ask) Route {+ if (a.whole_screen) {+ if (!a.allowed) return .{ .refused = "The whole screen is off in nanocua: work inside one app (leave out whole_screen), or ask the user to turn it on." };+ return .whole_screen;+ }+ const pid = a.pid orelse a.last orelse return .{ .refused = "Which app? Pass app or pid, or read one first with get_ui_tree (see list_apps)." };+ if (pid == a.self) return .{ .refused = "That is nanocua itself: agents can't act on it." };+ return .{ .app = pid };+}++test "a call goes into the app it names, else the last one read" {+ try std.testing.expectEqual(Route{ .app = 7 }, route(.{ .pid = 7, .last = 9, .self = 1 }));+ try std.testing.expectEqual(Route{ .app = 9 }, route(.{ .last = 9, .self = 1 }));+ try std.testing.expect(route(.{ .self = 1 }) == .refused);+}++test "never nanocua, and the whole screen only when allowed" {+ try std.testing.expect(route(.{ .pid = 1, .self = 1 }) == .refused);+ try std.testing.expect(route(.{ .last = 1, .self = 1 }) == .refused);+ try std.testing.expect(route(.{ .whole_screen = true, .pid = 7, .self = 1 }) == .refused);+ try std.testing.expect(route(.{ .whole_screen = true, .allowed = true, .self = 1 }) == .whole_screen);+ // Allowed isn't asked for: agents stay in the app unless they say otherwise.+ try std.testing.expectEqual(Route{ .app = 7 }, route(.{ .allowed = true, .pid = 7, .self = 1 }));+}diff --git a/apps/nanocua/src/tests.zig b/apps/nanocua/src/tests.zignew file mode 100644index 00000000..9e7a251d--- /dev/null+++ b/apps/nanocua/src/tests.zig@@ -0,0 +1,31 @@+//! nanocua's tests: the tools' catalog, the policy and log, MCP sessions,+//! keys, the tree's text, the socket's framing and path, the panel through+//! the Tester, and the socket end to end. Nothing here moves the pointer,+//! types, or captures the screen; the macOS backend is compiled (every+//! declaration analyzed) but not run.+const std = @import("std");+const builtin = @import("builtin");++test {+ _ = @import("tools.zig");+ _ = @import("policy.zig");+ _ = @import("mcp.zig");+ _ = @import("keys.zig");+ _ = @import("tree.zig");+ _ = @import("target.zig");+ _ = @import("server.zig");+ _ = @import("sockpath.zig");+ _ = @import("tests_app.zig");+ if (comptime @import("server.zig").supported) _ = @import("proxy.zig");+ std.testing.refAllDecls(@import("server.zig").Server);+ std.testing.refAllDecls(@import("app.zig").App);+ std.testing.refAllDecls(@import("app.zig").Platform);+ if (builtin.os.tag == .macos) {+ _ = @import("mac/cf.zig");+ _ = @import("mac/windows.zig");+ _ = @import("mac/input.zig");+ inline for (.{ @import("mac/backend.zig"), @import("mac/ax.zig"), @import("mac/input.zig"), @import("mac/capture.zig"), @import("mac/apps.zig"), @import("mac/windows.zig"), @import("mac/hotkey.zig"), @import("proxy.zig") }) |m| std.testing.refAllDecls(m);+ std.testing.refAllDecls(@import("mac/backend.zig").Mac);+ std.testing.refAllDecls(@import("mac/hotkey.zig").HotKey);+ }+}diff --git a/apps/nanocua/src/tests_app.zig b/apps/nanocua/src/tests_app.zignew file mode 100644index 00000000..1d8fa12a--- /dev/null+++ b/apps/nanocua/src/tests_app.zig@@ -0,0 +1,140 @@+//! The panel through the Tester (Stop and Resume, input, the log, the+//! command's Copy) and the socket end to end over a socket pair, with a+//! backend that only records: nothing here touches the real screen.+const std = @import("std");+const builtin = @import("builtin");+const nanoui = @import("nanoui");+const ui = nanoui.ui;+const mcp = @import("mcp.zig");+const tools = @import("tools.zig");+const App = @import("app.zig").App;+const view = @import("view.zig");+const theme = @import("theme.zig");+const server = @import("server.zig");++const Recorder = struct {+ calls: usize = 0,+ fn call(ptr: *anyopaque, tool: tools.Tool, _: tools.Args, reply: *mcp.Reply) void {+ const r: *Recorder = @ptrCast(@alignCast(ptr));+ r.calls += 1;+ if (tool == .get_ui_tree) reply.text("app \"Notes\" pid 42\n[1] Window \"Notes\"") else reply.fail("No element [9]");+ }+};++fn call(a: *App, id: u32, name: []const u8, args: []const u8) void {+ var buf: [256]u8 = undefined;+ a.hub.handle(1, std.fmt.bufPrint(&buf, "{{\"jsonrpc\":\"2.0\",\"id\":{d},\"method\":\"tools/call\",\"params\":{{\"name\":\"{s}\",\"arguments\":{s}}}}}", .{ id, name, args }) catch unreachable);+}++fn perms() @import("app.zig").Perms {+ return .{ .accessibility = true, .screen = false };+}++test "the panel: stop and resume, input, the log, copying the command" {+ const gpa = std.testing.allocator;+ var rec: Recorder = .{};+ var a: App = undefined;+ a.init(gpa, .{ .ptr = &rec, .call = Recorder.call });+ defer a.deinit();+ a.dark = false;+ a.read_perms = perms;+ a.setCommand("/Applications/nanocua.app/Contents/MacOS/nanocua");+ var t: ui.Tester = undefined;+ try t.init(gpa, .init(&a, view.view), .{ .width = 560, .height = 720, .theme = theme.ui(false) });+ defer t.deinit();++ try std.testing.expect(t.hasText("Looking only"));+ try std.testing.expect(t.hasText("No agent connected"));+ try std.testing.expect(t.hasText("Nothing yet. Every call an agent makes shows here."));+ if (comptime builtin.os.tag == .macos) {+ try std.testing.expect(t.hasText("Allowed"));+ try std.testing.expect(t.hasText("Allow…"));+ }++ // Looking works; acting is refused until input is on.+ call(&a, 1, "get_ui_tree", "{\"app\":\"Notes\"}");+ call(&a, 2, "click", "{\"ref\":9}");+ t.settle(0.5);+ try std.testing.expectEqual(@as(usize, 1), rec.calls);+ try std.testing.expect(t.hasText("refused"));+ try std.testing.expect(t.hasText("done"));+ try std.testing.expect(t.hasText("2 calls, 1 refused"));+ try t.screenshot("/tmp/nanocua-panel.png");++ try t.clickText("Allow input: clicks, typing, keys, opening apps");+ t.settle(0.5);+ try std.testing.expect(a.hub.policy.allow_input);+ try std.testing.expect(t.hasText("Acting"));+ call(&a, 3, "click", "{\"ref\":9}");+ t.settle(0.5);+ try std.testing.expectEqual(@as(usize, 2), rec.calls);+ try std.testing.expect(t.hasText("failed"));+ try std.testing.expect(t.hasText("No element [9]"));++ // The whole screen has its own switch, off until the user turns it on.+ call(&a, 30, "screenshot", "{\"whole_screen\":true}");+ t.settle(0.5);+ try std.testing.expectEqual(@as(usize, 2), rec.calls);+ try t.clickText("Allow whole screen: your pointer and keyboard, pictures of everything");+ t.settle(0.5);+ try std.testing.expect(a.hub.policy.allow_whole_screen);+ try std.testing.expect(t.hasText("Whole screen"));+ call(&a, 31, "screenshot", "{\"whole_screen\":true}");+ t.settle(0.5);+ try std.testing.expectEqual(@as(usize, 3), rec.calls);++ // Stop refuses everything, even looking; Resume undoes it.+ try t.clickText("Stop");+ t.settle(0.5);+ try std.testing.expect(a.hub.policy.stopped);+ try std.testing.expect(t.hasText("Stopped"));+ call(&a, 4, "get_ui_tree", "{}");+ try std.testing.expectEqual(@as(usize, 3), rec.calls);+ try t.clickText("Resume");+ t.settle(0.5);+ try std.testing.expect(!a.hub.policy.stopped);++ try t.clickText("Copy");+ t.settle(0.2);+ try std.testing.expectEqualStrings("/Applications/nanocua.app/Contents/MacOS/nanocua mcp", t.clipboard.items);+ try std.testing.expect(t.hasText("Copied"));++ try t.clickText("Clear");+ t.settle(0.5);+ try std.testing.expect(t.hasText("Nothing yet. Every call an agent makes shows here."));+}++extern "c" fn socketpair(domain: c_int, kind: c_int, protocol: c_int, fds: *[2]c_int) c_int;+extern "c" fn close(fd: c_int) c_int;++test "the socket: a request in, its answer out, a closed agent dropped" {+ if (builtin.os.tag != .macos) return error.SkipZigTest;+ const gpa = std.testing.allocator;+ var rec: Recorder = .{};+ var a: App = undefined;+ a.init(gpa, .{ .ptr = &rec, .call = Recorder.call });+ defer a.deinit();+ var fds: [2]c_int = undefined;+ try std.testing.expectEqual(@as(c_int, 0), socketpair(1, 1, 0, &fds));+ _ = try a.server.adopt(fds[0]);+ try std.testing.expectEqual(@as(usize, 1), a.server.clients());+ const req = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"ping\"}\n{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\",\"params\":{\"name\":\"get_ui_tree\"}}\n";+ try std.testing.expect(server.writeAll(fds[1], req));+ var got: std.ArrayList(u8) = .empty;+ defer got.deinit(gpa);+ var tries: usize = 0;+ while (std.mem.count(u8, got.items, "\n") < 2 and tries < 50) : (tries += 1) {+ nanoui.runFor(0.02);+ var buf: [4096]u8 = undefined;+ var fds_poll = [_]std.posix.pollfd{.{ .fd = fds[1], .events = std.posix.POLL.IN, .revents = 0 }};+ if ((std.posix.poll(&fds_poll, 0) catch 0) == 0) continue;+ const n = std.c.read(fds[1], &buf, buf.len);+ if (n > 0) try got.appendSlice(gpa, buf[0..@intCast(n)]);+ }+ try std.testing.expect(std.mem.startsWith(u8, got.items, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{}}\n"));+ try std.testing.expect(std.mem.indexOf(u8, got.items, "[1] Window \\\"Notes\\\"") != null);+ _ = close(fds[1]);+ tries = 0;+ while (a.server.clients() > 0 and tries < 50) : (tries += 1) nanoui.runFor(0.02);+ try std.testing.expectEqual(@as(usize, 0), a.server.clients());+}diff --git a/apps/nanocua/src/theme.zig b/apps/nanocua/src/theme.zignew file mode 100644index 00000000..3262273e--- /dev/null+++ b/apps/nanocua/src/theme.zig@@ -0,0 +1,66 @@+//! nanocua's colors: the family's (nanoterm, nanomon), warm charcoal or+//! cream with amber; rust for Stop, sage for what is allowed.+const nanoui = @import("nanoui");+const Color = nanoui.ui.Color;++pub const Colors = struct {+ page: Color,+ card: Color,+ border: Color,+ text: Color,+ secondary: Color,+ accent: Color,+ red: Color,+ green: Color,+ yellow: Color,+ well: Color,+};++const white: Color = .{ .r = 1, .g = 1, .b = 1 };+const black: Color = .{};++pub fn colors(dark: bool) Colors {+ if (dark) {+ const bg = Color.hex(0x16140f);+ return .{+ .page = bg,+ .card = .hex(0x1f1c16),+ .border = bg.mix(white, 0.09),+ .text = .hex(0xedeae2),+ .secondary = .hex(0x9a9384),+ .accent = .hex(0xe8a33d),+ .red = .hex(0xd2674f),+ .green = .hex(0x7fa86b),+ .yellow = .hex(0xd9a441),+ .well = .hex(0x191712),+ };+ }+ const bg = Color.hex(0xfaf8f3);+ return .{+ .page = bg,+ .card = .hex(0xf0ece3),+ .border = bg.mix(black, 0.09),+ .text = .hex(0x1a1813),+ .secondary = .hex(0x6b6557),+ .accent = .hex(0xc77d20),+ .red = .hex(0xb14228),+ .green = .hex(0x5c7e47),+ .yellow = .hex(0x9a6e1b),+ .well = .hex(0xe9e3d6),+ };+}++pub fn ui(dark: bool) nanoui.ui.Theme {+ const k = colors(dark);+ var t = if (dark) nanoui.ui.Theme.dark_theme else nanoui.ui.Theme.light;+ t.background = k.page;+ t.text = k.text;+ t.muted = k.secondary;+ t.border = k.border;+ t.accent = k.accent;+ t.accent_hover = k.accent.mix(if (dark) white else black, 0.14);+ t.accent_text = if (dark) k.page else Color.hex(0xfffdf8);+ t.danger = k.red;+ t.success = k.green;+ return t;+}diff --git a/apps/nanocua/src/tools.zig b/apps/nanocua/src/tools.zignew file mode 100644index 00000000..58e5f955--- /dev/null+++ b/apps/nanocua/src/tools.zig@@ -0,0 +1,199 @@+//! What agents can do: each tool's name, what it is for (the model reads+//! it), whether it only looks or acts, and its arguments' JSON Schema, as+//! MCP's tools/list gives them.+const std = @import("std");++pub const Kind = enum {+ /// Reads the screen: allowed unless everything is stopped.+ observe,+ /// Moves the pointer, clicks, types, launches: allowed only when the+ /// user has turned input on.+ act,+};++pub const Tool = enum {+ list_apps,+ get_ui_tree,+ screenshot,+ click,+ type_text,+ press_key,+ scroll,+ move_mouse,+ set_value,+ open_app,++ pub fn kind(t: Tool) Kind {+ return switch (t) {+ .list_apps, .get_ui_tree, .screenshot => .observe,+ else => .act,+ };+ }++ pub fn parse(name: []const u8) ?Tool {+ return std.meta.stringToEnum(Tool, name);+ }++ pub fn description(t: Tool) []const u8 {+ return switch (t) {+ .list_apps =>+ \\Lists the running apps with windows: name, bundle id, pid, and which one is frontmost.+ ,+ .get_ui_tree =>+ \\The accessibility tree of an app, in the background (pid or app; else the app you last read, else the+ \\frontmost): one line per element with a ref like [12], its role, title, value and frame in screen points+ \\(top-left origin). The app becomes the one you act in. Use refs with click, type_text, scroll and+ \\set_value; they stay valid until the next get_ui_tree. Prefer this over screenshots: it is exact and cheap.+ ,+ .screenshot =>+ \\A JPEG of an app's main window, even behind other windows (pid or app; else the app you last read).+ \\With whole_screen, if the user allows it: the main display, or a region of it (x, y, width, height).+ \\The reply says how image pixels map back to screen points. Use it when the tree does not show what you+ \\need (canvases, images).+ ,+ .click =>+ \\Clicks in the app you act in, without moving the user's pointer: an element by ref (pressing it through+ \\accessibility when it can, else at its center) or x,y in screen points. button: left (default) or right;+ \\count: 2 for a double click. Some apps ignore background clicks: then use refs, or ask for whole_screen.+ ,+ .type_text =>+ \\Types text into the app you act in, without taking the user's keyboard: into its focused element, or+ \\into the element ref after focusing it.+ ,+ .press_key =>+ \\Presses a key with modifiers in the app you act in, written like "cmd+s", "shift+tab", "enter", "escape",+ \\"cmd+shift+left".+ ,+ .scroll =>+ \\Scrolls in the app you act in, at x,y (or over element ref), by dy lines (positive scrolls down) and dx+ \\lines (positive scrolls right).+ ,+ .move_mouse =>+ \\Hovers at x,y in screen points in the app you act in. The user's pointer stays put unless whole_screen.+ ,+ .set_value =>+ \\Sets an element's value through accessibility (a text field's text, a slider's value): exact, no typing.+ ,+ .open_app =>+ \\Opens an app by name, like "Safari" or "Notes", behind what the user is doing, and makes it the one you+ \\act in. With whole_screen, if the user allows it: brings it forward.+ ,+ };+ }++ /// The arguments' JSON Schema.+ pub fn schema(t: Tool) []const u8 {+ const point =+ \\"x":{"type":"number","description":"Screen points from the left"},"y":{"type":"number","description":"Screen points from the top"}+ ;+ const ref =+ \\"ref":{"type":"integer","description":"An element's ref from get_ui_tree"}+ ;+ const scope =+ \\"app":{"type":"string","description":"The app to act in, by name (else the last one you read)"},"pid":{"type":"integer"},"whole_screen":{"type":"boolean","description":"Use the real pointer and keyboard, if the user allows it"}+ ;+ return switch (t) {+ .list_apps => "{\"type\":\"object\",\"properties\":{}}",+ .get_ui_tree =>+ \\{"type":"object","properties":{"pid":{"type":"integer"},"app":{"type":"string","description":"An app's name"},"max_nodes":{"type":"integer","description":"At most this many elements (default 400)"}}}+ ,+ .screenshot =>+ \\{"type":"object","properties":{"app":{"type":"string","description":"An app's name"},"pid":{"type":"integer"},"whole_screen":{"type":"boolean","description":"The display, not one app's window, if the user allows it"},"x":{"type":"number"},"y":{"type":"number"},"width":{"type":"number"},"height":{"type":"number"},"max_size":{"type":"integer","description":"The image's longer side, in pixels (default 1280)"}}}+ ,+ .click => "{\"type\":\"object\",\"properties\":{" ++ ref ++ "," ++ point ++ "," ++ scope +++ \\,"button":{"type":"string","enum":["left","right"]},"count":{"type":"integer","minimum":1,"maximum":3}}}+ ,+ .type_text => "{\"type\":\"object\",\"properties\":{\"text\":{\"type\":\"string\"}," ++ ref ++ "," ++ scope ++ "},\"required\":[\"text\"]}",+ .press_key => "{\"type\":\"object\",\"properties\":{\"keys\":{\"type\":\"string\",\"description\":\"Like cmd+s or enter\"}," ++ scope ++ "},\"required\":[\"keys\"]}",+ .scroll => "{\"type\":\"object\",\"properties\":{" ++ ref ++ "," ++ point ++ "," ++ scope +++ \\,"dx":{"type":"number"},"dy":{"type":"number"}}}+ ,+ .move_mouse => "{\"type\":\"object\",\"properties\":{" ++ point ++ "," ++ scope ++ "},\"required\":[\"x\",\"y\"]}",+ .set_value => "{\"type\":\"object\",\"properties\":{" ++ ref ++ ",\"value\":{\"type\":\"string\"}},\"required\":[\"ref\",\"value\"]}",+ .open_app =>+ \\{"type":"object","properties":{"name":{"type":"string"},"whole_screen":{"type":"boolean","description":"Bring it forward, if the user allows it"}},"required":["name"]}+ ,+ };+ }+};++/// MCP's tools/list result: every tool, its description and schema.+pub fn writeList(w: *std.Io.Writer) !void {+ try w.writeAll("{\"tools\":[");+ inline for (comptime std.enums.values(Tool), 0..) |t, i| {+ if (i > 0) try w.writeByte(',');+ try w.print("{{\"name\":\"{s}\",\"description\":", .{@tagName(t)});+ try std.json.Stringify.encodeJsonString(t.description(), .{}, w);+ try w.print(",\"inputSchema\":{s}", .{t.schema()});+ try w.print(",\"annotations\":{{\"readOnlyHint\":{}}}}}", .{t.kind() == .observe});+ }+ try w.writeAll("]}");+}++/// Arguments, read leniently from the call's JSON object.+pub const Args = struct {+ obj: ?std.json.ObjectMap,++ pub fn of(v: ?std.json.Value) Args {+ if (v) |val| if (val == .object) return .{ .obj = val.object };+ return .{ .obj = null };+ }++ pub fn number(a: Args, key: []const u8) ?f64 {+ const v = (a.obj orelse return null).get(key) orelse return null;+ return switch (v) {+ .integer => |i| @floatFromInt(i),+ .float => |f| f,+ .number_string => |s| std.fmt.parseFloat(f64, s) catch null,+ else => null,+ };+ }++ pub fn int(a: Args, key: []const u8) ?i64 {+ const n = a.number(key) orelse return null;+ if (!std.math.isFinite(n) or @abs(n) > 1e15) return null;+ return @intFromFloat(n);+ }++ pub fn string(a: Args, key: []const u8) ?[]const u8 {+ const v = (a.obj orelse return null).get(key) orelse return null;+ return if (v == .string) v.string else null;+ }++ pub fn boolean(a: Args, key: []const u8) ?bool {+ const v = (a.obj orelse return null).get(key) orelse return null;+ return if (v == .bool) v.bool else null;+ }++ /// x and y, both or neither.+ pub fn point(a: Args) ?[2]f64 {+ return .{ a.number("x") orelse return null, a.number("y") orelse return null };+ }+};++test "every schema is JSON, and the list parses" {+ const gpa = std.testing.allocator;+ var aw: std.Io.Writer.Allocating = .init(gpa);+ defer aw.deinit();+ try writeList(&aw.writer);+ const parsed = try std.json.parseFromSlice(std.json.Value, gpa, aw.written(), .{});+ defer parsed.deinit();+ const tools = parsed.value.object.get("tools").?.array.items;+ try std.testing.expectEqual(std.enums.values(Tool).len, tools.len);+ for (tools) |t| {+ try std.testing.expect(Tool.parse(t.object.get("name").?.string) != null);+ try std.testing.expectEqualStrings("object", t.object.get("inputSchema").?.object.get("type").?.string);+ }+}++test "arguments read leniently" {+ const gpa = std.testing.allocator;+ const parsed = try std.json.parseFromSlice(std.json.Value, gpa, "{\"x\":10,\"y\":2.5,\"ref\":\"no\",\"text\":\"hi\"}", .{});+ defer parsed.deinit();+ const a = Args.of(parsed.value);+ try std.testing.expectEqual(@as(?[2]f64, .{ 10, 2.5 }), a.point());+ try std.testing.expectEqual(@as(?i64, null), a.int("ref"));+ try std.testing.expectEqualStrings("hi", a.string("text").?);+ try std.testing.expectEqual(@as(?bool, null), a.boolean("text"));+ try std.testing.expectEqual(@as(?[]const u8, null), Args.of(null).string("text"));+}diff --git a/apps/nanocua/src/tree.zig b/apps/nanocua/src/tree.zignew file mode 100644index 00000000..8a2fff36--- /dev/null+++ b/apps/nanocua/src/tree.zig@@ -0,0 +1,101 @@+//! The accessibility tree as text for a model: one line per element worth+//! naming, indented by depth, with a ref to act on it. Containers with+//! nothing to say (a group with no title) are left out and their children+//! moved up, which keeps deep web pages and toolbars short.+const std = @import("std");++pub const Node = struct {+ /// "AXButton", as the platform names it; written without "AX".+ role: []const u8,+ subrole: []const u8 = "",+ title: []const u8 = "",+ description: []const u8 = "",+ value: []const u8 = "",+ /// x, y, width, height in screen points.+ frame: ?[4]f64 = null,+ enabled: bool = true,+ focused: bool = false,+ selected: bool = false,+};++/// Roles a model may act on, named even when they say nothing.+const actionable = [_][]const u8{+ "Button", "CheckBox", "RadioButton", "TextField", "TextArea", "SecureTextField", "ComboBox", "PopUpButton",+ "MenuItem", "MenuButton", "MenuBarItem", "Slider", "Link", "Tab", "Cell", "Row",+ "Incrementor", "DisclosureTriangle", "SearchField", "Window", "Sheet", "Dialog", "Image", "ColorWell",+};++/// Roles that are only containers.+const plumbing = [_][]const u8{ "Group", "Unknown", "SplitGroup", "ScrollArea", "LayoutArea", "LayoutItem", "Splitter", "Matte", "Column" };++fn bare(role: []const u8) []const u8 {+ return if (std.mem.startsWith(u8, role, "AX")) role[2..] else role;+}++fn in(s: []const u8, list: []const []const u8) bool {+ for (list) |x| if (std.mem.eql(u8, s, x)) return true;+ return false;+}++/// Whether the node gets a line (and a ref).+pub fn shown(n: Node) bool {+ const r = bare(n.role);+ if (in(r, &actionable)) return true;+ if (n.title.len > 0 or n.description.len > 0) return true;+ if (n.value.len > 0 and !in(r, &plumbing)) return true;+ if (in(r, &plumbing)) return false;+ // Static text with nothing in it, separators, and the like.+ return !std.mem.eql(u8, r, "StaticText") and !std.mem.eql(u8, r, "Splitter");+}++/// Writes `n`'s line: ` [12] Button "Save" (desc) value="…" @x,y wxh`.+pub fn writeLine(w: *std.Io.Writer, n: Node, ref: u32, depth: usize) !void {+ try w.splatByteAll(' ', @min(depth, 40) * 2);+ try w.print("[{d}] {s}", .{ ref, bare(n.role) });+ if (n.subrole.len > 0 and !std.mem.eql(u8, bare(n.subrole), bare(n.role))) try w.print("/{s}", .{bare(n.subrole)});+ if (n.title.len > 0) try quoted(w, " ", n.title, 120);+ if (n.description.len > 0 and !std.mem.eql(u8, n.description, n.title)) try quoted(w, " desc=", n.description, 120);+ if (n.value.len > 0 and !std.mem.eql(u8, n.value, n.title)) try quoted(w, " value=", n.value, 200);+ if (n.frame) |f| try w.print(" @{d:.0},{d:.0} {d:.0}x{d:.0}", .{ f[0], f[1], f[2], f[3] });+ if (!n.enabled) try w.writeAll(" disabled");+ if (n.focused) try w.writeAll(" focused");+ if (n.selected) try w.writeAll(" selected");+ try w.writeByte('\n');+}++/// `s` in quotes on one line, cut after `max` bytes at a character's start.+fn quoted(w: *std.Io.Writer, prefix: []const u8, s: []const u8, max: usize) !void {+ var end = @min(s.len, max);+ while (end < s.len and end > 0 and (s[end] & 0xc0) == 0x80) end -= 1;+ try w.writeAll(prefix);+ try w.writeByte('"');+ for (s[0..end]) |ch| switch (ch) {+ '\n' => try w.writeAll("\\n"),+ '"' => try w.writeAll("\\\""),+ '\r', '\t' => try w.writeByte(' '),+ else => try w.writeByte(ch),+ };+ if (end < s.len) try w.writeAll("…");+ try w.writeByte('"');+}++test "lines name what can be acted on and skip plumbing" {+ var buf: [512]u8 = undefined;+ var w: std.Io.Writer = .fixed(&buf);+ try std.testing.expect(!shown(.{ .role = "AXGroup" }));+ try std.testing.expect(shown(.{ .role = "AXGroup", .title = "Sidebar" }));+ try std.testing.expect(shown(.{ .role = "AXButton" }));+ try std.testing.expect(!shown(.{ .role = "AXStaticText" }));+ try std.testing.expect(shown(.{ .role = "AXStaticText", .value = "Hello" }));+ try writeLine(&w, .{ .role = "AXButton", .title = "Save", .description = "Save", .frame = .{ 10, 20.4, 80, 24 }, .enabled = false }, 3, 1);+ try writeLine(&w, .{ .role = "AXTextField", .subrole = "AXSearchField", .value = "a \"b\"\nc", .focused = true }, 4, 0);+ try std.testing.expectEqualStrings(+ \\ [3] Button "Save" @10,20 80x24 disabled+ \\[4] TextField/SearchField value="a \"b\"\nc" focused+ \\+ , w.buffered());+ w = .fixed(&buf);+ const long: [300]u8 = @splat('x');+ try writeLine(&w, .{ .role = "AXStaticText", .value = &long }, 5, 0);+ try std.testing.expect(std.mem.endsWith(u8, w.buffered(), "x…\"\n"));+}diff --git a/apps/nanocua/src/view.zig b/apps/nanocua/src/view.zignew file mode 100644index 00000000..cca14b88--- /dev/null+++ b/apps/nanocua/src/view.zig@@ -0,0 +1,160 @@+//! The control panel: whether agents may act, in one app or on the whole+//! screen, and the switch that stops them, the permissions macOS needs, the command that adds nanocua to an+//! agent, and every call agents made, newest first.+const std = @import("std");+const nanoui = @import("nanoui");+const ui = nanoui.ui;+const theme = @import("theme.zig");+const policy = @import("policy.zig");+const App = @import("app.zig").App;+const mac = @import("app.zig").mac;++pub fn view(a: *App, c: *ui.Context) void {+ const k = theme.colors(a.dark orelse nanoui.isDark());+ a.refreshPerms();+ const page = ui.div(c).column().sizeFull().p(20).gap(14).bg(k.page);+ defer page.end();+ header(c, a, k);+ permissions(c, a, k);+ agents(c, a, k);+ activity(c, a, k);+}++fn card(c: *ui.Context, k: theme.Colors) ui.Div {+ return ui.div(c).column().wFull().p(14).gap(10).bg(k.card).border(k.border).rounded(12);+}++fn title(c: *ui.Context, k: theme.Colors, s: []const u8) void {+ ui.text(c, s, .{ .size = 13, .weight = .semibold, .color = k.secondary });+}++fn header(c: *ui.Context, a: *App, k: theme.Colors) void {+ const p = a.hub.policy;+ const row = ui.div(c).row().wFull().itemsCenter().gap(10);+ defer row.end();+ {+ const col = ui.div(c).column().gap(3).grow();+ defer col.end();+ {+ const r = ui.div(c).row().itemsCenter().gap(8);+ defer r.end();+ ui.text(c, "nanocua", .{ .size = 20, .weight = .bold, .color = k.text });+ if (p.stopped) ui.badge(c, "Stopped", k.red) else if (p.allow_input) ui.badge(c, "Acting", k.green) else ui.badge(c, "Looking only", k.yellow);+ if (p.allow_whole_screen and !p.stopped) ui.badge(c, "Whole screen", k.red);+ }+ ui.text(c, "Lets AI agents see and use this computer, as far as you allow here.", .{ .size = 12, .color = k.secondary, .wrap = true });+ }+ if (p.stopped) {+ if (ui.button(c, "Resume", .{ .variant = .primary }).clicked) a.setStopped(false);+ } else if (ui.button(c, "Stop", .{ .variant = .danger }).clicked) a.setStopped(true);+}++fn permissions(c: *ui.Context, a: *App, k: theme.Colors) void {+ if (comptime !mac) return;+ const d = card(c, k);+ defer d.end();+ title(c, k, "Permissions");+ permission(c, a, k, "Accessibility", "To read apps' controls, click and type", a.perms.accessibility, .accessibility);+ permission(c, a, k, "Screen Recording", "For screenshots", a.perms.screen, .screen);+}++fn permission(c: *ui.Context, a: *App, k: theme.Colors, name: []const u8, what: []const u8, on: bool, which: App.Ask) void {+ const row = ui.div(c).row().wFull().itemsCenter().gap(10);+ defer row.end();+ {+ const col = ui.div(c).column().gap(2).grow();+ defer col.end();+ ui.text(c, name, .{ .size = 14, .weight = .medium, .color = k.text });+ ui.text(c, what, .{ .size = 12, .color = k.secondary });+ }+ if (on) ui.badge(c, "Allowed", k.green) else {+ if (ui.button(c, "Allow…", .{ .size = 13 }).clicked) a.request(which);+ }+}++fn agents(c: *ui.Context, a: *App, k: theme.Colors) void {+ const d = card(c, k);+ defer d.end();+ title(c, k, "Agents");+ var input = a.hub.policy.allow_input;+ if (ui.toggle(c, "Allow input: clicks, typing, keys, opening apps", &input)) a.setInput(input);+ var whole = a.hub.policy.allow_whole_screen;+ if (ui.toggle(c, "Allow whole screen: your pointer and keyboard, pictures of everything", &whole)) a.setWholeScreen(whole);+ ui.text(c, "Off, agents work inside one app at a time, in the background.", .{ .size = 12, .color = k.secondary, .wrap = true });+ if (a.listen_error) |e| {+ ui.text(c, e, .{ .size = 12, .color = k.red, .wrap = true });+ } else {+ const n = a.server.clients();+ const waiting = a.hub.pending;+ ui.text(c, if (n == 0) "No agent connected" else c.print("{d} agent{s} connected{s}", .{+ n, if (n == 1) "" else "s", if (waiting > 0) c.print(", {d} call{s} running", .{ waiting, if (waiting == 1) "" else "s" }) else "",+ }), .{ .size = 13, .color = k.text });+ }+ ui.text(c, "Add nanocua to an agent as an MCP server (stdio) that runs:", .{ .size = 12, .color = k.secondary, .wrap = true });+ {+ const r = ui.div(c).row().wFull().itemsCenter().gap(8);+ defer r.end();+ {+ const well = ui.div(c).row().grow().px(10).py(7).bg(k.well).rounded(8);+ defer well.end();+ ui.text(c, a.command, .{ .size = 12, .mono = true, .color = k.text, .truncate = .ellipsis });+ }+ const now = a.hub.clock();+ if (ui.button(c, if (now < a.copied_until) "Copied" else "Copy", .{ .size = 13 }).clicked) {+ c.clipboard.write(c.clipboard.ctx, a.command);+ a.copied_until = now + 2;+ }+ }+ if (comptime mac) {+ const r = ui.div(c).row().itemsCenter().gap(6);+ defer r.end();+ ui.text(c, "Stop everything from anywhere:", .{ .size = 12, .color = k.secondary });+ ui.kbd(c, @import("mac/hotkey.zig").label);+ }+}++fn activity(c: *ui.Context, a: *App, k: theme.Colors) void {+ const log = &a.hub.log;+ const d = card(c, k).grow();+ defer d.end();+ {+ const r = ui.div(c).row().wFull().itemsCenter().gap(8);+ defer r.end();+ title(c, k, "Activity");+ if (log.calls > 0) ui.text(c, c.print("{d} call{s}, {d} refused", .{ log.calls, if (log.calls == 1) "" else "s", log.denied }), .{ .size = 12, .color = k.secondary });+ ui.spacer(c);+ if (log.len > 0 and ui.button(c, "Clear", .{ .variant = .ghost, .size = 12 }).clicked) log.clear();+ }+ if (log.len == 0) {+ ui.text(c, "Nothing yet. Every call an agent makes shows here.", .{ .size = 12, .color = k.secondary });+ return;+ }+ const list = ui.div(c).column().wFull().grow().scrollY().gap(2);+ defer list.end();+ var i: usize = 0;+ while (log.newest(i)) |e| : (i += 1) {+ ui.key(c, c.print("{d}", .{e.seq}));+ entry(c, k, e);+ }+}++fn entry(c: *ui.Context, k: theme.Colors, e: *const policy.Entry) void {+ const col = ui.div(c).column().wFull().gap(2).py(5);+ defer col.end();+ {+ const r = ui.div(c).row().wFull().itemsCenter().gap(8);+ defer r.end();+ ui.text(c, @tagName(e.tool), .{ .size = 13, .weight = .medium, .mono = true, .color = k.text });+ ui.text(c, e.summary(), .{ .size = 12, .mono = true, .color = k.secondary, .truncate = .ellipsis });+ ui.spacer(c);+ if (e.status != .running) ui.text(c, c.print("{d:.0} ms", .{e.ms}), .{ .size = 11, .color = k.secondary });+ switch (e.status) {+ .running => ui.badge(c, "running", k.accent),+ .ok => ui.badge(c, "done", k.green),+ .failed => ui.badge(c, "failed", k.red),+ .denied => ui.badge(c, "refused", k.yellow),+ }+ }+ if (e.detail().len > 0 and e.status != .ok)+ ui.text(c, e.detail(), .{ .size = 11, .color = k.secondary, .truncate = .ellipsis });+}diff --git a/docs/adr/0209-desktop-app-has-no-computer-use.md b/docs/adr/0209-desktop-app-has-no-computer-use.mdindex 10ff3b33..299be8e8 100644--- a/docs/adr/0209-desktop-app-has-no-computer-use.md+++ b/docs/adr/0209-desktop-app-has-no-computer-use.md@@ -25,7 +25,8 @@ clicks and keystrokes to Codegraff's own window. Nothing in the app or the MCPserver can reach it, and it never requests permissions.graff's bridge for the Codex Computer Use plugin (ADR 0036, 0119) is separate-and unchanged. (Since removed: ADR 0313.)+and unchanged. (Since removed: ADR 0313. Computer use returned outside the app,+as an MCP server every surface sees: ADR 0314.)## Consequencesdiff --git a/docs/adr/0313-graff-does-not-load-codex-computer-use.md b/docs/adr/0313-graff-does-not-load-codex-computer-use.mdindex 4d9ea1dc..d2fb3f34 100644--- a/docs/adr/0313-graff-does-not-load-codex-computer-use.md+++ b/docs/adr/0313-graff-does-not-load-codex-computer-use.md@@ -1,6 +1,7 @@# 0313. graff does not load Codex's computer-use pluginsStatus: accepted 2026-11-04. Supersedes [0036](0036-computer-use-keeps-the-signed-codex-bridge.md) and [0119](0119-computer-tools-receive-caller-context.md).+Computer use came back as nanocua: [0314](0314-nanocua-is-grafts-computer-use.md).## Contextdiff --git a/docs/adr/0314-nanocua-is-grafts-computer-use.md b/docs/adr/0314-nanocua-is-grafts-computer-use.mdnew file mode 100644index 00000000..17dd521b--- /dev/null+++ b/docs/adr/0314-nanocua-is-grafts-computer-use.md@@ -0,0 +1,57 @@+# 0314. nanocua is graff's computer use on macOS++Status: accepted 2026-11-04. Replaces the "no computer use" half of [0313](0313-graff-does-not-load-codex-computer-use.md).++## Context++ADR 0313 removed the last computer-use path, which depended on another+product's install layout and private service. Agents still sometimes need+another app's real UI. Two forces shape how graff provides it. macOS grants+Accessibility and Screen Recording to the app responsible for a process, so a+library inside `graff` would hand both to whichever terminal started it, and to+everything else running in that terminal. And ADR 0209 removed the desktop app's+`computer` tool because agents reached for it to show a local build, then turned+a missing permission into a setup task for the user.++## Decision++Computer use is nanocua: `apps/nanocua`, a signed macOS app with its own+permissions, a control panel and a kill switch. graff links none of it. Release+builds put `nanocua.app` beside `graff` in the macOS tarballs; `install.sh` and+`graff update` place it in `~/Applications`. When the app is there, graff adds+one MCP server, `nanocua mcp`, below every config file, so a server the user+named `nanocua` wins and `graff mcp disable nanocua` works.++That entry skips the startup consent gate, because it runs what graff shipped+and starting it does nothing: `nanocua mcp` answers handshakes and the tool list+itself, and only the first `tools/call` opens the app. What it may do is the+app's decision, not graff's or the agent's:++- input is off until the user turns it on in the panel, each launch;+- by default a call works inside one app in the background, with the user's+ pointer and keyboard untouched, and screenshots capture that app's window;+- the real pointer and keyboard and pictures of the whole display need a second+ switch; Stop refuses everything; agents cannot reach nanocua's own window.++Its instructions tell agents to prefer the project's own commands for showing+their work and not to chase permissions, which is ADR 0209's failure.++codegraff holds the original. nanoui's `apps/nanocua` is generated from it by+`scripts/vendor-nanocua.sh` and stays under codegraff's license, not nanoui's+MIT. nanoui does not import it, so nanoui itself stays MIT. Linking the other way+is fine: `apps/nanocua` depends on nanoui, pinned, like any MIT library.++## Consequences++- The desktop app runs `graff acp`, so it offers nanocua too once the app is in+ `~/Applications`, as every engine feature reaches every surface. ADR 0209's+ `computer` tool stays gone; this differs in living outside the desktop app,+ behind nanocua's own switches.+- Other platforms get nothing yet: the server is macOS-only, and the app there+ answers that each tool is unsupported.+- A release built without the signing secrets has no app, so that install has no+ computer use. An ad hoc app would lose its permissions on every update.+- Background input is delivered per process. Apps may ignore it, Chromium's web+ content most likely; agents fall back to refs or ask for the whole screen.+- Two agents share one app, one target and one set of refs, calls one at a time.+- Beta releases are not signed, so they carry no app.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex 677b10c7..14728301 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -337,7 +337,8 @@ record only when you need the evidence or the edge cases.| [0310](0310-auth-failures-name-the-rejected-credential.md) | A terminal auth rejection names the seat that failed: sign-in text keeps "sign in again"; key and promoted-key texts say "API key was rejected" and never the signed-out phrases. Key seats carry `{"kind":"api_key_rejected","provider":…}` (v1 `-32603` data, v2 `_meta["graff/recovery"]`); `reauth_required` is unchanged. || [0311](0311-tool-refresh-and-addressed-wakes.md) | Claude Agent refreshes a changed tool list with MCP `tools/list_changed` (served before anything else; fresh session if not re-read in 15 s) instead of removing and re-adding its server, which failed held calls. `attempt_completion` settles a task like a final answer: room mail waits for the next human prompt, addressed mail can still start a turn; `yield_turn` stays strict. A peer wake preloads `peer_message`. || [0312](0312-openai-12h-prompt-cache-is-opt-in.md) | OpenAI Responses GPT-5.6+ can put a 12h explicit breakpoint on the stable prefix (`GRAFF_OPENAI_CACHE_TTL=12h` or `openai_cache_ttl`); the default stays 30m (ADR 0009 bytes) until a live probe confirms OpenAI accepts the field and keeps the entry. Usage parses cache writes by TTL. |-| [0313](0313-graff-does-not-load-codex-computer-use.md) | Graff has no computer use and does not load Codex's `computer-use` or `unified-computer-use` plugins; every MCP elicitation is declined. Supersedes 0036 and 0119. |+| [0313](0313-graff-does-not-load-codex-computer-use.md) | Graff does not load Codex's `computer-use` or `unified-computer-use` plugins; every MCP elicitation is declined. Supersedes 0036 and 0119. |+| [0314](0314-nanocua-is-grafts-computer-use.md) | Computer use on macOS is nanocua, a signed app shipped beside graff and offered as one MCP server; it works inside one app in the background, and the app's panel, not graff, says what it may do. nanoui's copy is generated from codegraff's and keeps codegraff's license. || [0302](0302-sse-idle-watchdogs-follow-body-byte-progress.md) | SSE idle watchdogs observe partial body-byte progress, while separate unfinished-event time and size bounds preserve recovery. |diff --git a/docs/notarization.md b/docs/notarization.mdindex 10dd3cd6..c70f2e9f 100644--- a/docs/notarization.md+++ b/docs/notarization.md@@ -14,6 +14,11 @@ half are ordering rules that are easy to get subtly wrong.| Linux / Windows tarballs | nobody | CI (`release.yml`) builds and uploads them || macOS CLI tarballs | Developer ID Application, then Apple notarized | CI `macos-sign` job when the secrets exist; otherwise **locally**, after CI's draft exists |+The macOS tarballs also carry `nanocua.app` (ADR 0314), built, signed and+notarized with the `graff` beside it by `scripts/release-sign-macos.sh`, and+stapled. The manual steps below cover `graff` alone: a tarball they produce has no+app unless you run `apps/nanocua/macos/package.sh` before notarizing.+## The CI path`release.yml` runs `build` (cross-compile on Linux) → `macos-sign` (macOSdiff --git a/install.sh b/install.shindex a4aa1e84..1a6891b2 100644--- a/install.sh+++ b/install.sh@@ -82,6 +82,19 @@ darwin_sign() {codesign -s - --force --identifier "$identifier" "$bin" >/dev/null 2>&1 || true}+# macOS release tarballs carry nanocua.app, graff's computer use (ADR 0314).+# ~/Applications needs no administrator, and Launch Services finds it there. A+# tarball without it changes nothing. It runs nothing until an agent needs it.+place_nanocua() {+ [ "$(uname -s)" = Darwin ] || return 0+ local app+ app="$(find "$1" -maxdepth 2 -type d -name nanocua.app 2>/dev/null | head -1)"+ [ -n "$app" ] || return 0+ mkdir -p "$HOME/Applications"+ rm -rf "$HOME/Applications/nanocua.app"+ cp -R "$app" "$HOME/Applications/nanocua.app" || return 0+}+# Drop the binary onto a *fresh inode*: overwriting a running macOS binary in# place gets the replacement SIGKILLed by the kernel's signature cache.place_bin() {@@ -155,6 +168,7 @@ fetch_release() {fi[ -n "$binpath" ] && [ -f "$binpath" ] || { rm -rf "$tmpd"; return 1; }place_bin "$binpath"+ place_nanocua "$tmpd"rm -rf "$tmpd"# Fail closed: if the binary didn't actually land, return non-zero so the# caller falls back to a source build instead of falsely reporting success.diff --git a/scripts/release-sign-macos.sh b/scripts/release-sign-macos.shindex 01232f79..b277dd34 100755--- a/scripts/release-sign-macos.sh+++ b/scripts/release-sign-macos.sh@@ -4,7 +4,7 @@## scripts/release-sign-macos.sh <aarch64-macos|x86_64-macos>#-# Needs CODESIGN_IDENTITY, AC_API_KEY_ID, AC_API_ISSUER_ID, the notary key at+# Needs Zig, a checkout with apps/nanocua, CODESIGN_IDENTITY, AC_API_KEY_ID, AC_API_ISSUER_ID, the notary key at# $RUNNER_TEMP/ac_api_key.p8, and dist/graff-<target>.tar.gz. Writes# signed/graff-<target>.tar.gz. docs/notarization.md is the manual version.set -euo pipefail@@ -31,6 +31,10 @@ xattr -cr "$work/$name"codesign --force --options runtime --timestamp --sign "$CODESIGN_IDENTITY" "$bin"codesign --verify --strict --verbose=2 "$bin"+# nanocua ships beside graff (ADR 0314), signed by the same identity and in the+# same notarization, so macOS keeps its permissions across updates.+VERSION="${GITHUB_REF_NAME:-}" sh apps/nanocua/macos/package.sh "$target" "$work/$name"+# ditto, not zip: keep the signed bytes and metadata exact.ditto -c -k --keepParent "$work/$name" "$work/$name.notarize.zip"# notarytool exits 0 even when the submission is Invalid, so the status field@@ -48,4 +52,6 @@ if [ "$status" != Accepted ]; thenfi# Bare CLIs cannot be stapled (stapler error 73); the ticket is bound to the# cdhash and checked online. Ship the accepted bytes.+# An app can be, so it opens offline too.+xcrun stapler staple "$work/$name/nanocua.app"(cd "$work" && tar -czf "$GITHUB_WORKSPACE/signed/$name.tar.gz" "$name")diff --git a/scripts/vendor-nanocua.sh b/scripts/vendor-nanocua.shnew file mode 100755index 00000000..19317c84--- /dev/null+++ b/scripts/vendor-nanocua.sh@@ -0,0 +1,50 @@+#!/usr/bin/env bash+# Copies apps/nanocua into a nanoui checkout. codegraff holds the original;+# nanoui's copy is generated from it and not edited there.+#+# scripts/vendor-nanocua.sh /path/to/nanoui+#+# The copy builds against the nanoui it sits in rather than the pinned one, and+# stays under codegraff's license, not nanoui's MIT: the license goes verbatim+# in third_party/licenses/, where nanoui's line limit exempts it, and VENDORED.md+# says what the folder is and which commit it came from.+set -euo pipefail++dest_repo="${1:?usage: scripts/vendor-nanocua.sh /path/to/nanoui}"+repo="$(git -C "$(dirname "${BASH_SOURCE[0]}")/.." rev-parse --show-toplevel)"+[ -f "$dest_repo/src/nanoui.zig" ] || { echo "$dest_repo is not a nanoui checkout" >&2; exit 1; }+src="$repo/apps/nanocua"+dest="$dest_repo/apps/nanocua"+mkdir -p "$dest" "$dest_repo/third_party/licenses"++# Builds in the copy are its own; what the original no longer has goes.+rsync -a --delete --exclude zig-out --exclude .zig-cache --exclude zig-pkg --exclude VENDORED.md "$src/" "$dest/"++python3 -I - "$dest/build.zig.zon" <<'PY'+import re, sys+path = sys.argv[1]+text = open(path).read()+text, n = re.subn(r"\.nanoui = \.\{.*?\n \},", '.nanoui = .{ .path = "../.." },', text, flags=re.S)+if n != 1:+ sys.exit(f"{path}: expected one nanoui dependency, found {n}")+open(path, "w").write(text)+PY++cp "$repo/LICENSE" "$dest_repo/third_party/licenses/codegraff-LICENSE.txt"++commit="$(git -C "$repo" rev-parse HEAD)"+git -C "$repo" diff --quiet HEAD -- apps/nanocua LICENSE || commit="$commit (with uncommitted changes)"+cat > "$dest/VENDORED.md" <<MD+# Vendored from codegraff++This folder is generated from \`apps/nanocua\` in+[codegraff](https://github.com/justrach/codegraff) at \`$commit\`, by that+repository's \`scripts/vendor-nanocua.sh\`. Change it there, not here: the next+copy overwrites this one.++**It is not under nanoui's MIT license.** It keeps codegraff's license, in+[\`third_party/licenses/codegraff-LICENSE.txt\`](../../third_party/licenses/codegraff-LICENSE.txt).+Only the dependency in \`build.zig.zon\` differs from the original: here it builds+against the nanoui around it.+MD+echo "vendored apps/nanocua at $commit into $dest"diff --git a/src/mcp_cli.zig b/src/mcp_cli.zigindex f910a40e..4adeba42 100644--- a/src/mcp_cli.zig+++ b/src/mcp_cli.zig@@ -54,6 +54,7 @@ pub fn countMcpServers(merged: mcp_config.Merged) usize {var n: usize = 0;var it = merged.servers.iterator();while (it.next()) |entry| {+ if (merged.shipped_nanocua and std.mem.eql(u8, entry.key_ptr.*, @import("nanocua_default.zig").server_name)) continue;if (!trustedMcpEntry(entry.key_ptr.*, entry.value_ptr.*)) n += 1;}return n;diff --git a/src/mcp_config.zig b/src/mcp_config.zigindex ca2101ad..1f2a16e5 100644--- a/src/mcp_config.zig+++ b/src/mcp_config.zig@@ -64,6 +64,8 @@ pub const Merged = struct {/// (a client switched a server off) or be `{}` (an imported name).global_off: std.json.ObjectMap = .empty,project_off: std.json.ObjectMap = .empty,+ /// `nanocua` in `servers` is the entry graff added for the app it shipped.+ shipped_nanocua: bool = false,/// What the off-lists took out of `servers`, with the config each had.disabled: std.json.ObjectMap = .empty,@@ -194,6 +196,9 @@ pub fn load(io: Io, arena: Allocator, dir: Io.Dir, project_path: []const u8, glo// Plugin / Claude / Cursor / Grok configs fill names still missing. They// never beat a graff file. `home` empty keeps tests off the real $HOME.plugins.mergeMcp(io, arena, home, dir, &merged.servers, &merged.found);+ // Computer use on macOS, when the app is there (ADR 0314). Below everything+ // the user wrote; above the off-lists, so they can switch it off.+ merged.shipped_nanocua = @import("nanocua_default.zig").merge(io, arena, home, &merged.servers, &merged.found);// Off-lists last, so they reach imported names too. A project defining a// server itself keeps it even if the global file switched that name off.var global_off_it = merged.global_off.iterator();diff --git a/src/mcp_status.zig b/src/mcp_status.zigindex 56f77512..df8ae3cd 100644--- a/src/mcp_status.zig+++ b/src/mcp_status.zig@@ -40,7 +40,7 @@ pub const ToolRow = struct { name: []const u8, description: []const u8 };pub const Row = struct {name: []const u8,- /// project | global | imported | session+ /// project | global | shipped | imported | sessionsource: []const u8,/// stdio | httptransport: []const u8,@@ -62,6 +62,7 @@ pub const Row = struct {pub fn sourceOf(merged: mcp_config.Merged, name: []const u8) []const u8 {if (merged.project.get(name) != null) return "project";if (merged.global.get(name) != null) return "global";+ if (merged.shipped_nanocua and std.mem.eql(u8, name, @import("nanocua_default.zig").server_name)) return "shipped";if (merged.servers.get(name) != null) return "imported";return "session";}diff --git a/src/nanocua_default.zig b/src/nanocua_default.zignew file mode 100644index 00000000..077fe14a--- /dev/null+++ b/src/nanocua_default.zig@@ -0,0 +1,151 @@+//! Computer use on macOS is nanocua (ADR 0314), an app of its own that release+//! builds ship beside graff. graff links none of it: when the app is there, it+//! is one more MCP server, `nanocua mcp`, which answers handshakes without+//! starting the app. A server the user named `nanocua` themselves wins, and+//! `graff mcp disable nanocua` switches it off like any other.++const std = @import("std");+const builtin = @import("builtin");+const Io = std.Io;+const Allocator = std.mem.Allocator;++pub const server_name = "nanocua";+const executable = "nanocua.app/Contents/MacOS/nanocua";++/// Where install.sh and `graff update` put the app.+pub fn installRoot(arena: Allocator, home: []const u8) ?[]const u8 {+ if (home.len == 0) return null;+ return std.fmt.allocPrint(arena, "{s}/Applications", .{home}) catch null;+}++fn inRoot(io: Io, arena: Allocator, root: []const u8) ?[]const u8 {+ const path = std.fmt.allocPrint(arena, "{s}/{s}", .{ root, executable }) catch return null;+ _ = Io.Dir.cwd().statFile(io, path, .{}) catch return null;+ return path;+}++/// The shipped app's program. Tests look only under their own home, so an app+/// installed on the machine running them doesn't change what they see.+pub fn find(io: Io, arena: Allocator, home: []const u8) ?[]const u8 {+ if (installRoot(arena, home)) |root| if (inRoot(io, arena, root)) |p| return p;+ if (builtin.is_test) return null;+ if (inRoot(io, arena, "/Applications")) |p| return p;+ // An unpacked release tarball, which has it beside graff.+ const exe = std.process.executablePathAlloc(io, arena) catch return null;+ return inRoot(io, arena, std.fs.path.dirname(exe) orelse return null);+}++fn entry(arena: Allocator, command: []const u8) !std.json.Value {+ var args = std.json.Array.init(arena);+ try args.append(.{ .string = "mcp" });+ var cfg: std.json.ObjectMap = .empty;+ try cfg.put(arena, "command", .{ .string = command });+ try cfg.put(arena, "args", .{ .array = args });+ return .{ .object = cfg };+}++/// Fills `nanocua` when nothing else defined that name, on macOS, with the app+/// there. True means this entry is graff's own, which needs no consent: it runs+/// the app graff shipped, and that app asks the user itself before anything acts.+pub fn merge(io: Io, arena: Allocator, home: []const u8, servers: *std.json.ObjectMap, found: *bool) bool {+ if (builtin.os.tag != .macos) return false;+ return mergeFound(io, arena, home, servers, found);+}++fn mergeFound(io: Io, arena: Allocator, home: []const u8, servers: *std.json.ObjectMap, found: *bool) bool {+ if (home.len == 0 or servers.get(server_name) != null) return false;+ const command = find(io, arena, home) orelse return false;+ const cfg = entry(arena, command) catch return false;+ servers.put(arena, server_name, cfg) catch return false;+ found.* = true;+ return true;+}++const testing = std.testing;++fn tmpHome(io: Io, arena: Allocator, tmp: *testing.TmpDir, with_app: bool) ![]const u8 {+ var buf: [std.fs.max_path_bytes]u8 = undefined;+ const n = try tmp.dir.realPath(io, &buf);+ const home = try arena.dupe(u8, buf[0..n]);+ if (with_app) {+ const path = try std.fmt.allocPrint(arena, "{s}/Applications/{s}", .{ home, executable });+ try Io.Dir.cwd().createDirPath(io, std.fs.path.dirname(path).?);+ try Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = "" });+ }+ return home;+}++test "the shipped app becomes the nanocua server" {+ var tmp = testing.tmpDir(.{});+ defer tmp.cleanup();+ var state = std.heap.ArenaAllocator.init(testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const home = try tmpHome(testing.io, a, &tmp, true);+ var servers: std.json.ObjectMap = .empty;+ var found = false;+ try testing.expect(mergeFound(testing.io, a, home, &servers, &found));+ try testing.expect(found);+ const cfg = servers.get(server_name).?.object;+ try testing.expect(std.mem.endsWith(u8, cfg.get("command").?.string, "/Applications/nanocua.app/Contents/MacOS/nanocua"));+ try testing.expectEqualStrings("mcp", cfg.get("args").?.array.items[0].string);+}++test "no app, no home or the user's own nanocua: nothing is added" {+ var tmp = testing.tmpDir(.{});+ defer tmp.cleanup();+ var state = std.heap.ArenaAllocator.init(testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ var servers: std.json.ObjectMap = .empty;+ var found = false;+ try testing.expect(!mergeFound(testing.io, a, try tmpHome(testing.io, a, &tmp, false), &servers, &found));+ try testing.expect(!mergeFound(testing.io, a, "", &servers, &found));+ try testing.expect(!found);+ const home = try tmpHome(testing.io, a, &tmp, true);+ try servers.put(a, server_name, .{ .string = "theirs" });+ try testing.expect(!mergeFound(testing.io, a, home, &servers, &found));+ try testing.expectEqualStrings("theirs", servers.get(server_name).?.string);+}++test "only macOS offers it" {+ var tmp = testing.tmpDir(.{});+ defer tmp.cleanup();+ var state = std.heap.ArenaAllocator.init(testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const home = try tmpHome(testing.io, a, &tmp, true);+ var servers: std.json.ObjectMap = .empty;+ var found = false;+ try testing.expectEqual(builtin.os.tag == .macos, merge(testing.io, a, home, &servers, &found));+}++test "through the config: shipped and consent-free, the user's files win and can switch it off" {+ if (builtin.os.tag != .macos) return error.SkipZigTest;+ const mcp_config = @import("mcp_config.zig");+ const mcp_status = @import("mcp_status.zig");+ const io = testing.io;+ var tmp = testing.tmpDir(.{});+ defer tmp.cleanup();+ var state = std.heap.ArenaAllocator.init(testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const home = try tmpHome(io, a, &tmp, true);+ const global = try std.fmt.allocPrint(a, "{s}/global.json", .{home});++ const plain = mcp_config.load(io, a, tmp.dir, ".mcp.json", global, home, false);+ try testing.expect(plain.shipped_nanocua and plain.found);+ try testing.expect(plain.servers.get(server_name) != null);+ try testing.expectEqualStrings("shipped", mcp_status.sourceOf(plain, server_name));++ try Io.Dir.cwd().writeFile(io, .{ .sub_path = global, .data = "{\"mcpServers\":{},\"disabledMcpServers\":{\"nanocua\":{}}}" });+ const off = mcp_config.load(io, a, tmp.dir, ".mcp.json", global, home, false);+ try testing.expect(off.servers.get(server_name) == null);+ try testing.expect(off.disabled.get(server_name) != null);++ try Io.Dir.cwd().writeFile(io, .{ .sub_path = global, .data = "{\"mcpServers\":{\"nanocua\":{\"command\":\"theirs\"}}}" });+ const theirs = mcp_config.load(io, a, tmp.dir, ".mcp.json", global, home, false);+ try testing.expect(!theirs.shipped_nanocua);+ try testing.expectEqualStrings("theirs", theirs.servers.get(server_name).?.object.get("command").?.string);+ try testing.expectEqualStrings("global", mcp_status.sourceOf(theirs, server_name));+}diff --git a/src/nanocua_install.zig b/src/nanocua_install.zignew file mode 100644index 00000000..965a6b78--- /dev/null+++ b/src/nanocua_install.zig@@ -0,0 +1,128 @@+//! `graff update`'s half of shipping nanocua (ADR 0314): a macOS release+//! tarball carries nanocua.app beside graff, and this puts it in+//! ~/Applications. The archive is already verified against SHA256SUMS. A tarball+//! without the app (one CI didn't sign) changes nothing, so a signed app already+//! installed is never swapped for no app.++const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;+const archive = @import("update_archive.zig");+const nanocua_default = @import("nanocua_default.zig");++const bundle = "nanocua.app";++/// `name`'s path inside the bundle, if it is in one. Nothing may climb out.+pub fn memberPath(name: []const u8) ?[]const u8 {+ const marker = bundle ++ "/";+ const at = if (std.mem.startsWith(u8, name, marker))+ 0+ else if (std.mem.indexOf(u8, name, "/" ++ marker)) |i| i + 1 else return null;+ const rel = name[at + marker.len ..];+ if (rel.len == 0 or rel[0] == '/') return null;+ var parts = std.mem.splitScalar(u8, rel, '/');+ while (parts.next()) |p| if (std.mem.eql(u8, p, "..")) return null;+ return rel;+}++fn modeFor(rel: []const u8) Io.File.Permissions {+ const executable = std.mem.startsWith(u8, rel, "Contents/MacOS/");+ return .fromMode(if (executable) 0o755 else 0o644);+}++/// Installs the app from a gzipped release tarball. True if it held one.+pub fn fromArchive(io: Io, gpa: Allocator, home: []const u8, gz: []const u8) !bool {+ if (home.len == 0) return false;+ const tar = try archive.gunzip(gpa, gz);+ defer gpa.free(tar);+ return fromTar(io, gpa, home, tar);+}++pub fn fromTar(io: Io, gpa: Allocator, home: []const u8, tar: []const u8) !bool {+ var state = std.heap.ArenaAllocator.init(gpa);+ defer state.deinit();+ const a = state.allocator();+ const root = nanocua_default.installRoot(a, home) orelse return false;+ const dest = try std.fmt.allocPrint(a, "{s}/{s}", .{ root, bundle });+ const stage = try std.fmt.allocPrint(a, "{s}/.{s}.new", .{ root, bundle });+ const old = try std.fmt.allocPrint(a, "{s}/.{s}.old", .{ root, bundle });+ const cwd = Io.Dir.cwd();+ cwd.deleteTree(io, stage) catch {};+ var any = false;+ errdefer cwd.deleteTree(io, stage) catch {};+ var members = archive.Members.init(tar);+ while (try members.next()) |m| {+ const rel = memberPath(m.name) orelse continue;+ const path = try std.fmt.allocPrint(a, "{s}/{s}", .{ stage, rel });+ try cwd.createDirPath(io, std.fs.path.dirname(path).?);+ try cwd.writeFile(io, .{ .sub_path = path, .data = m.payload, .flags = .{ .permissions = modeFor(rel) } });+ any = true;+ }+ if (!any) return false;+ // Aside, in, then away: a failure between leaves the old app to put back+ // by hand, never half of a new one.+ cwd.deleteTree(io, old) catch {};+ const had = if (cwd.rename(dest, cwd, old, io)) true else |_| false;+ try cwd.rename(stage, cwd, dest, io);+ if (had) cwd.deleteTree(io, old) catch {};+ return true;+}++const testing = std.testing;++test "members inside the bundle, and nothing that climbs out" {+ try testing.expectEqualStrings("Contents/Info.plist", memberPath("graff-aarch64-macos/nanocua.app/Contents/Info.plist").?);+ try testing.expectEqualStrings("Contents/MacOS/nanocua", memberPath("nanocua.app/Contents/MacOS/nanocua").?);+ try testing.expect(memberPath("graff-aarch64-macos/graff") == null);+ try testing.expect(memberPath("graff-aarch64-macos/nanocua.app/") == null);+ try testing.expect(memberPath("x/nanocua.app/../../evil") == null);+ try testing.expect(memberPath("x/notnanocua.app/Contents/Info.plist") == null);+}++fn appendMember(gpa: Allocator, out: *std.ArrayList(u8), name: []const u8, data: []const u8, kind: u8) !void {+ var hdr: [512]u8 = @splat(0);+ @memcpy(hdr[0..name.len], name);+ _ = try std.fmt.bufPrint(hdr[124..136], "{o:0>11}", .{data.len});+ hdr[156] = kind;+ try out.appendSlice(gpa, &hdr);+ try out.appendSlice(gpa, data);+ try out.appendNTimes(gpa, 0, std.mem.alignForward(usize, data.len, 512) - data.len);+}++test "the app lands in ~/Applications, replacing the old one; none in the tarball changes nothing" {+ const gpa = testing.allocator;+ const io = testing.io;+ var tmp = testing.tmpDir(.{});+ defer tmp.cleanup();+ var buf: [std.fs.max_path_bytes]u8 = undefined;+ const n = try tmp.dir.realPath(io, &buf);+ const home = buf[0..n];+ var state = std.heap.ArenaAllocator.init(gpa);+ defer state.deinit();+ const a = state.allocator();+ const stale = try std.fmt.allocPrint(a, "{s}/Applications/nanocua.app/Contents/stale", .{home});+ try Io.Dir.cwd().createDirPath(io, std.fs.path.dirname(stale).?);+ try Io.Dir.cwd().writeFile(io, .{ .sub_path = stale, .data = "old" });++ var none: std.ArrayList(u8) = .empty;+ defer none.deinit(gpa);+ try appendMember(gpa, &none, "graff-aarch64-macos/graff", "bin", '0');+ try none.appendNTimes(gpa, 0, 1024);+ try testing.expect(!try fromTar(io, gpa, home, none.items));+ _ = try Io.Dir.cwd().statFile(io, stale, .{}); // still there++ var with: std.ArrayList(u8) = .empty;+ defer with.deinit(gpa);+ try appendMember(gpa, &with, "graff-aarch64-macos/graff", "bin", '0');+ try appendMember(gpa, &with, "graff-aarch64-macos/nanocua.app/", "", '5');+ try appendMember(gpa, &with, "graff-aarch64-macos/nanocua.app/Contents/MacOS/nanocua", "app", '0');+ try appendMember(gpa, &with, "graff-aarch64-macos/nanocua.app/Contents/Info.plist", "plist", '0');+ try with.appendNTimes(gpa, 0, 1024);+ try testing.expect(try fromTar(io, gpa, home, with.items));+ const exe = try std.fmt.allocPrint(a, "{s}/Applications/nanocua.app/Contents/MacOS/nanocua", .{home});+ const data = try Io.Dir.cwd().readFileAlloc(io, exe, a, .limited(16));+ try testing.expectEqualStrings("app", data);+ const stat = try Io.Dir.cwd().statFile(io, exe, .{});+ try testing.expect(stat.permissions.toMode() & 0o111 != 0);+ try testing.expectError(error.FileNotFound, Io.Dir.cwd().statFile(io, stale, .{}));+}diff --git a/src/test_hooks.zig b/src/test_hooks.zigindex f2d3a4e9..15ce902c 100644--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@ -403,6 +403,9 @@ test {_ = @import("update_tests.zig");_ = @import("update_windows.zig");_ = @import("mcp_elicitation.zig");+ _ = @import("nanocua_default.zig");+ // POSIX file modes; only macOS installs it, Linux runs its tests too.+ if (@import("builtin").os.tag != .windows) _ = @import("nanocua_install.zig");_ = @import("edit_worktree_tests.zig"); // #747: edit lands on the selected worktree_ = @import("mcp.zig");_ = @import("mcp_rpc.zig");diff --git a/src/update_archive.zig b/src/update_archive.zigindex 35cca139..24176d90 100644--- a/src/update_archive.zig+++ b/src/update_archive.zig@@ -80,6 +80,32 @@ fn isGraffMember(name: []const u8) bool {return std.mem.eql(u8, base, "graff") or std.mem.eql(u8, base, "graff.exe");}+/// A tarball's regular files, in order.+pub const Members = struct {+ tar: []const u8,+ off: usize = 0,++ pub const Member = struct { name: []const u8, payload: []const u8 };++ pub fn init(tar: []const u8) Members {+ return .{ .tar = tar };+ }++ pub fn next(m: *Members) Error!?Member {+ while (m.off + 512 <= m.tar.len) {+ const hdr = m.tar[m.off .. m.off + 512];+ if (isZeroBlock(hdr)) return null;+ const size = tarSize(hdr) orelse return error.Malformed;+ m.off += 512;+ if (m.off + size > m.tar.len) return error.Malformed;+ const payload = m.tar[m.off .. m.off + @as(usize, @intCast(size))];+ m.off += std.mem.alignForward(usize, @as(usize, @intCast(size)), 512);+ if (hdr[156] == 0 or hdr[156] == '0') return .{ .name = tarName(hdr), .payload = payload };+ }+ return null;+ }+};+pub fn extractGraff(gpa: Allocator, tar_bytes: []const u8) Error![]u8 {if (tar_bytes.len < 512) return error.Malformed;var off: usize = 0;diff --git a/src/update_cmd.zig b/src/update_cmd.zigindex 3a24a9f7..6bd395a7 100644--- a/src/update_cmd.zig+++ b/src/update_cmd.zig@@ -134,6 +134,7 @@ fn makeOpts(root: *Agent, target: []const u8, fetch: update_service.Fetch, asset.asset_name = asset,.fetch = fetch,.cancel = test_cancel,+ .home = root.home,};}diff --git a/src/update_service.zig b/src/update_service.zigindex 810dfa2d..717ebfab 100644--- a/src/update_service.zig+++ b/src/update_service.zig@@ -86,6 +86,8 @@ pub const Opts = struct {fetch: Fetch,channel: enum { stable, beta } = .stable,cancel: ?*const std.atomic.Value(bool) = null,+ /// Where nanocua.app goes on macOS (empty: it isn't installed).+ home: []const u8 = "",};var install_busy = std.atomic.Value(bool).init(false);@@ -304,6 +306,10 @@ pub fn install(opts: Opts, consent: Consent) Report {defer opts.gpa.free(bin);if (cancelled(opts)) return snapshot(opts, .cancelled, tag_copy, viewed.installed(), "update cancelled");+ // The app that ships with graff (ADR 0314). It has its own uses, so failing+ // to place it doesn't stop graff updating.+ if (builtin.os.tag == .macos) _ = @import("nanocua_install.zig").fromArchive(opts.io, opts.gpa, opts.home, tarball) catch {};+if (existingMatches(opts.io, opts.gpa, opts.target_path, bin)) {writeInstalledVersion(opts.io, opts.target_path, version_status.stripV(tag_copy));return snapshot(opts, .already_installed, tag_copy, version_status.stripV(tag_copy), "the install target already has these bytes");