From 191a36c85bbe39be0505f3a14276acb62b6cbfff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Ortiz?= Date: Sun, 15 Nov 2026 08:38:44 +0100 Subject: [PATCH 1/4] Add benchup bridge and in-app agent connection (#55) Agents can now edit the project open in BenchUp, on the desktop and on the web, without a central server. BenchUp connects out to a local bridge, because a browser tab cannot listen on a port. benchup bridge (CLI): - Listens on 127.0.0.1 only, with challenge-response pairing (HMAC-SHA256; the token is never sent), an Origin allowlist, pre-authentication limits and call deadlines. One BenchUp window per bridge. - Serves MCP on stdio. When another bridge holds the port it forwards to it, and takes the port over if that bridge exits. - scene, get, call (--batch, --dry-run), undo, redo and render take --live. The first one starts a background bridge, which exits after 30 minutes without calls. - Batch references and reports move to benchup_tools::batch, so files, MCP and the app answer the same way. Agent connection (app): - Settings -> Agent connection: off by default; the setting and the port persist. The pairing token stays in this device's private storage, with copy buttons for the token and setup snippets. - Each changing call, and each batch, is one undo step "Agent: ...", recorded like the designer's own edits, so shared projects keep their read-only state and edit leases. - Reads always run. A changing call asks the designer to grant or deny access when they are mid-operation or have taken control; granting cancels their operation. The status bar offers Take control. - Calls run from App::logic, so a minimized window or hidden tab still answers. Renders run off the UI thread on the desktop. - eframe's clipboard feature is enabled so copied text reaches the system clipboard on the desktop. - All text is translated in every catalog; docs and the skill cover live mode. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 104 +- assets/i18n/en_US.json | 75 ++ assets/i18n/es_419.json | 75 ++ assets/i18n/it_IT.json | 75 ++ assets/i18n/pt_BR.json | 75 ++ assets/i18n/vi_VN.json | 75 ++ assets/i18n/zh_CN.json | 75 ++ crates/benchup-app/Cargo.toml | 6 +- crates/benchup-app/src/agent_link.rs | 1239 ++++++++++++++++++++ crates/benchup-app/src/agent_link_tests.rs | 595 ++++++++++ crates/benchup-app/src/agent_link_ui.rs | 183 +++ crates/benchup-app/src/app.rs | 55 +- crates/benchup-app/src/credentials.rs | 104 ++ crates/benchup-app/src/projects.rs | 1 + crates/benchup-app/src/realtime.rs | 30 +- crates/benchup-app/src/settings.rs | 13 +- crates/benchup-app/src/sharing.rs | 84 ++ crates/benchup-app/src/sidebar.rs | 24 + crates/benchup-cli/Cargo.toml | 2 + crates/benchup-cli/skill/benchup/SKILL.md | 2 +- crates/benchup-cli/src/bridge.rs | 956 +++++++++++++++ crates/benchup-cli/src/bridge_tests.rs | 256 ++++ crates/benchup-cli/src/main.rs | 156 ++- crates/benchup-cli/src/mcp.rs | 16 +- crates/benchup-cli/src/project.rs | 160 +-- crates/benchup-cli/tests/live.rs | 138 +++ crates/benchup-tools/Cargo.toml | 1 + crates/benchup-tools/src/batch.rs | 167 +++ crates/benchup-tools/src/lib.rs | 5 + crates/benchup-tools/src/pairing.rs | 157 +++ docs/agent-guide.md | 16 +- docs/cli.md | 50 +- 32 files changed, 4755 insertions(+), 215 deletions(-) create mode 100644 crates/benchup-app/src/agent_link.rs create mode 100644 crates/benchup-app/src/agent_link_tests.rs create mode 100644 crates/benchup-app/src/agent_link_ui.rs create mode 100644 crates/benchup-app/src/credentials.rs create mode 100644 crates/benchup-cli/src/bridge.rs create mode 100644 crates/benchup-cli/src/bridge_tests.rs create mode 100644 crates/benchup-cli/tests/live.rs create mode 100644 crates/benchup-tools/src/batch.rs create mode 100644 crates/benchup-tools/src/pairing.rs diff --git a/Cargo.lock b/Cargo.lock index 2a07621..3973699 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -445,6 +445,7 @@ dependencies = [ "tempfile", "tungstenite", "ureq", + "uuid", "velopack", "wasm-bindgen", "wasm-bindgen-futures", @@ -459,8 +460,10 @@ version = "0.7.2" dependencies = [ "benchup-core", "benchup-tools", + "getrandom 0.4.3", "serde_json", "tempfile", + "tungstenite", ] [[package]] @@ -502,6 +505,7 @@ dependencies = [ "flate2", "serde", "serde_json", + "sha2", "tempfile", ] @@ -636,18 +640,42 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "calloop" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b32313e937c8721c9d17ee9bf60699a6402b4b7c2cb6ac1bee0ffcaa4b4e5e" +dependencies = [ + "bitflags 2.13.2", + "polling", + "rustix 1.1.5", + "tracing", +] + [[package]] name = "calloop-wayland-source" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20" dependencies = [ - "calloop", + "calloop 0.13.0", "rustix 0.38.44", "wayland-backend", "wayland-client", ] +[[package]] +name = "calloop-wayland-source" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138efcf0940a02ebf0cc8d1eff41a1682a46b431630f4c52450d6265876021fa" +dependencies = [ + "calloop 0.14.5", + "rustix 1.1.5", + "wayland-backend", + "wayland-client", +] + [[package]] name = "cc" version = "1.10.0" @@ -1117,6 +1145,7 @@ dependencies = [ "profiling", "raw-window-handle", "serde", + "smithay-clipboard", "web-time", "webbrowser", "windows-sys 0.61.2", @@ -3518,8 +3547,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016" dependencies = [ "bitflags 2.13.2", - "calloop", - "calloop-wayland-source", + "calloop 0.13.0", + "calloop-wayland-source 0.3.0", "cursor-icon", "libc", "log", @@ -3536,6 +3565,44 @@ dependencies = [ "xkeysym", ] +[[package]] +name = "smithay-client-toolkit" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0512da38f5e2b31201a93524adb8d3136276fa4fe4aafab4e1f727a82b534cc0" +dependencies = [ + "bitflags 2.13.2", + "calloop 0.14.5", + "calloop-wayland-source 0.4.1", + "cursor-icon", + "libc", + "log", + "memmap2", + "rustix 1.1.5", + "thiserror 2.0.21", + "wayland-backend", + "wayland-client", + "wayland-csd-frame", + "wayland-cursor", + "wayland-protocols", + "wayland-protocols-experimental", + "wayland-protocols-misc", + "wayland-protocols-wlr", + "wayland-scanner", + "xkeysym", +] + +[[package]] +name = "smithay-clipboard" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71704c03f739f7745053bde45fa203a46c58d25bc5c4efba1d9a60e9dba81226" +dependencies = [ + "libc", + "smithay-client-toolkit 0.20.0", + "wayland-backend", +] + [[package]] name = "smol_str" version = "0.2.2" @@ -3759,6 +3826,7 @@ version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -4179,6 +4247,32 @@ dependencies = [ "wayland-scanner", ] +[[package]] +name = "wayland-protocols-experimental" +version = "20250721.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40a1f863128dcaaec790d7b4b396cc9b9a7a079e878e18c47e6c2d2c5a8dcbb1" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-misc" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e9567599ef23e09b8dad6e429e5738d4509dfc46b3b21f32841a304d16b29c8" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + [[package]] name = "wayland-protocols-plasma" version = "0.3.12" @@ -4684,7 +4778,7 @@ dependencies = [ "bitflags 2.13.2", "block2 0.5.1", "bytemuck", - "calloop", + "calloop 0.13.0", "cfg_aliases", "concurrent-queue", "core-foundation 0.9.4", @@ -4705,7 +4799,7 @@ dependencies = [ "raw-window-handle", "redox_syscall 0.4.1", "rustix 0.38.44", - "smithay-client-toolkit", + "smithay-client-toolkit 0.19.2", "smol_str", "tracing", "unicode-segmentation", diff --git a/assets/i18n/en_US.json b/assets/i18n/en_US.json index d259755..5597b07 100644 --- a/assets/i18n/en_US.json +++ b/assets/i18n/en_US.json @@ -41,6 +41,81 @@ "account.storage_unavailable": "Storage unavailable", "account.the_server_did_not_sign_out": "The server did not sign out.", "account.use_https_or_http_on_localhost_for_development": "Use HTTPS, or HTTP on localhost for development.", + "agent_link.allow": "Allow agent connection", + "agent_link.allow_help": "When on, BenchUp connects to benchup bridge and runs the calls an agent sends. Each change is one undo step.", + "agent_link.an_agent": "An agent", + "agent_link.chip.connected": "Agent connected", + "agent_link.chip.designer": "You have control", + "agent_link.chip.editing": "Agent is editing", + "agent_link.command_help": "Adds BenchUp to Claude Code as an MCP server that uses this token.", + "agent_link.copied": "Copied", + "agent_link.copy_command": "Copy Claude Code command", + "agent_link.copy_json": "Copy MCP configuration", + "agent_link.copy_shell": "Copy shell setup", + "agent_link.copy_token": "Copy token", + "agent_link.deny": "Deny access", + "agent_link.error.app_busy": "Another BenchUp window is already connected to this bridge", + "agent_link.error.arguments_not_object": "The arguments must be a JSON object", + "agent_link.error.bridge": "The bridge refused the connection: {message}", + "agent_link.error.busy": "BenchUp was busy for too long; the call was not run", + "agent_link.error.calls_list": "calls must be a non-empty list of tool calls, each with a tool name", + "agent_link.error.cannot_save_token": "The token could not be saved in this browser", + "agent_link.error.connect_timeout": "The bridge did not answer", + "agent_link.error.denied": "The designer denied access to the project", + "agent_link.error.denied_hint": "They are working in BenchUp. Wait, then ask them before trying again.", + "agent_link.error.disabled": "Agent connection was turned off in BenchUp", + "agent_link.error.document_changed": "The open project changed before this call ran", + "agent_link.error.document_changed_hint": "Call get_scene to see the project that is open now.", + "agent_link.error.handshake": "The bridge did not follow the pairing protocol", + "agent_link.error.missing_tool": "The call names no tool", + "agent_link.error.nested_session": "{tool} cannot run inside run_batch", + "agent_link.error.no_browser": "No browser window is available", + "agent_link.error.no_randomness": "This system has no secure source of random numbers", + "agent_link.error.nothing_to_redo": "Nothing to redo", + "agent_link.error.nothing_to_undo": "Nothing to undo", + "agent_link.error.protocol": "This bridge uses a different protocol version; update the benchup command", + "agent_link.error.queue_full": "Too many calls are waiting in BenchUp", + "agent_link.error.result_too_large": "The answer is larger than the bridge accepts ({size} MB)", + "agent_link.error.result_too_large_hint": "Ask for less at once: filter get_scene, or render a smaller view.", + "agent_link.error.storage_unavailable": "Browser storage is unavailable", + "agent_link.error.taken_control": "The designer took control of the project", + "agent_link.error.unproven": "The bridge does not know this pairing token", + "agent_link.generate": "Create token", + "agent_link.grant": "Grant access", + "agent_link.help": "Let an AI agent on this computer edit the open project through the benchup bridge command. BenchUp connects to the bridge on 127.0.0.1; nothing is sent to other computers.", + "agent_link.hide": "Hide", + "agent_link.history_label": "Agent: {summary}", + "agent_link.json_help": "An mcpServers entry for other MCP clients.", + "agent_link.permission.body": "{client} wants to change the project.", + "agent_link.permission.busy": "You are in the middle of an operation. Granting access cancels it and lets the agent continue.", + "agent_link.permission.designer": "You took control. Granting access lets the agent continue editing.", + "agent_link.permission.title": "Grant access to the agent?", + "agent_link.permission.waiting": "Changes waiting: {count}", + "agent_link.port": "Bridge port", + "agent_link.port_help": "The port benchup bridge listens on (BENCHUP_BRIDGE_PORT). The default is 47615.", + "agent_link.queued_status": "BenchUp is busy; the call is waiting", + "agent_link.regenerate": "New token", + "agent_link.regenerate_help": "Replace the token. Agents using the old one are disconnected.", + "agent_link.release_control": "Let the agent edit", + "agent_link.release_control_help": "The agent may change the project again without asking while you are not using a tool.", + "agent_link.setup": "Set up an agent", + "agent_link.setup_help": "Install the benchup command, then add it to your agent with one of these.", + "agent_link.shell_help": "For agents that run benchup commands: set the token, then use --live, for example benchup scene --live.", + "agent_link.show": "Show", + "agent_link.status.connected": "Connected: {client}", + "agent_link.status.connecting": "Looking for benchup bridge on this computer…", + "agent_link.status.error": "Not connected: {message}", + "agent_link.status.off": "Off", + "agent_link.status.waiting": "Connected to the bridge. No agent has called yet.", + "agent_link.take_control": "Take control", + "agent_link.take_control_help": "Pause the agent. Its next changes wait until you grant access.", + "agent_link.title": "Agent connection", + "agent_link.token": "Pairing token", + "agent_link.token_help": "Give this token to the agent as BENCHUP_AGENT_TOKEN. It stays on this device. Anyone who has it can edit your open project through the bridge.", + "agent_link.token_none": "No token yet", + "agent_link.token_save_failed": "The token could not be saved: {error}", + "agent_link.waiting_for_designer": "Waiting for the designer to grant access", + "agent_link.waiting_for_server": "Waiting for the sharing server to confirm the selection", "app.about_benchup": "About BenchUp", "app.account": "Account", "app.account_not_connected": "Account · Not connected", diff --git a/assets/i18n/es_419.json b/assets/i18n/es_419.json index 611ceb3..358ba2d 100644 --- a/assets/i18n/es_419.json +++ b/assets/i18n/es_419.json @@ -41,6 +41,81 @@ "account.storage_unavailable": "Almacenamiento no disponible", "account.the_server_did_not_sign_out": "El servidor no cerró la sesión.", "account.use_https_or_http_on_localhost_for_development": "Usa HTTPS, o HTTP en localhost para desarrollo.", + "agent_link.allow": "Permitir conexión de agentes", + "agent_link.allow_help": "Al activarla, BenchUp se conecta a benchup bridge y ejecuta las llamadas que envía un agente. Cada cambio es un paso de deshacer.", + "agent_link.an_agent": "Un agente", + "agent_link.chip.connected": "Agente conectado", + "agent_link.chip.designer": "Tienes el control", + "agent_link.chip.editing": "El agente está editando", + "agent_link.command_help": "Añade BenchUp a Claude Code como servidor MCP con este token.", + "agent_link.copied": "Copiado", + "agent_link.copy_command": "Copiar comando de Claude Code", + "agent_link.copy_json": "Copiar configuración MCP", + "agent_link.copy_shell": "Copiar configuración de terminal", + "agent_link.copy_token": "Copiar token", + "agent_link.deny": "Denegar acceso", + "agent_link.error.app_busy": "Otra ventana de BenchUp ya está conectada a este puente", + "agent_link.error.arguments_not_object": "Los argumentos deben ser un objeto JSON", + "agent_link.error.bridge": "El puente rechazó la conexión: {message}", + "agent_link.error.busy": "BenchUp estuvo ocupado demasiado tiempo; la llamada no se ejecutó", + "agent_link.error.calls_list": "calls debe ser una lista no vacía de llamadas, cada una con el nombre de una herramienta", + "agent_link.error.cannot_save_token": "No se pudo guardar el token en este navegador", + "agent_link.error.connect_timeout": "El puente no respondió", + "agent_link.error.denied": "El diseñador denegó el acceso al proyecto", + "agent_link.error.denied_hint": "Está trabajando en BenchUp. Espera y pregúntale antes de volver a intentarlo.", + "agent_link.error.disabled": "La conexión de agentes se desactivó en BenchUp", + "agent_link.error.document_changed": "El proyecto abierto cambió antes de ejecutar esta llamada", + "agent_link.error.document_changed_hint": "Llama a get_scene para ver el proyecto que está abierto ahora.", + "agent_link.error.handshake": "El puente no siguió el protocolo de emparejamiento", + "agent_link.error.missing_tool": "La llamada no indica ninguna herramienta", + "agent_link.error.nested_session": "{tool} no se puede ejecutar dentro de run_batch", + "agent_link.error.no_browser": "No hay ninguna ventana del navegador disponible", + "agent_link.error.no_randomness": "Este sistema no tiene una fuente segura de números aleatorios", + "agent_link.error.nothing_to_redo": "No hay nada que rehacer", + "agent_link.error.nothing_to_undo": "No hay nada que deshacer", + "agent_link.error.protocol": "Este puente usa otra versión del protocolo; actualiza el comando benchup", + "agent_link.error.queue_full": "Hay demasiadas llamadas esperando en BenchUp", + "agent_link.error.result_too_large": "La respuesta es más grande de lo que acepta el puente ({size} MB)", + "agent_link.error.result_too_large_hint": "Pide menos de una vez: filtra get_scene o renderiza una vista más pequeña.", + "agent_link.error.storage_unavailable": "El almacenamiento del navegador no está disponible", + "agent_link.error.taken_control": "El diseñador tomó el control del proyecto", + "agent_link.error.unproven": "El puente no conoce este token de emparejamiento", + "agent_link.generate": "Crear token", + "agent_link.grant": "Conceder acceso", + "agent_link.help": "Permite que un agente de IA de este equipo edite el proyecto abierto mediante el comando benchup bridge. BenchUp se conecta al puente en 127.0.0.1; no se envía nada a otros equipos.", + "agent_link.hide": "Ocultar", + "agent_link.history_label": "Agente: {summary}", + "agent_link.json_help": "Una entrada mcpServers para otros clientes MCP.", + "agent_link.permission.body": "{client} quiere cambiar el proyecto.", + "agent_link.permission.busy": "Estás en mitad de una operación. Conceder acceso la cancela y deja que el agente continúe.", + "agent_link.permission.designer": "Tomaste el control. Conceder acceso deja que el agente siga editando.", + "agent_link.permission.title": "¿Conceder acceso al agente?", + "agent_link.permission.waiting": "Cambios en espera: {count}", + "agent_link.port": "Puerto del puente", + "agent_link.port_help": "El puerto en el que escucha benchup bridge (BENCHUP_BRIDGE_PORT). El predeterminado es 47615.", + "agent_link.queued_status": "BenchUp está ocupado; la llamada espera", + "agent_link.regenerate": "Nuevo token", + "agent_link.regenerate_help": "Reemplaza el token. Los agentes que usen el anterior se desconectan.", + "agent_link.release_control": "Dejar editar al agente", + "agent_link.release_control_help": "El agente podrá volver a cambiar el proyecto sin preguntar mientras no uses una herramienta.", + "agent_link.setup": "Configurar un agente", + "agent_link.setup_help": "Instala el comando benchup y añádelo a tu agente con una de estas opciones.", + "agent_link.shell_help": "Para agentes que ejecutan comandos benchup: define el token y usa --live, por ejemplo benchup scene --live.", + "agent_link.show": "Mostrar", + "agent_link.status.connected": "Conectado: {client}", + "agent_link.status.connecting": "Buscando benchup bridge en este equipo…", + "agent_link.status.error": "Sin conexión: {message}", + "agent_link.status.off": "Desactivada", + "agent_link.status.waiting": "Conectado al puente. Ningún agente ha llamado todavía.", + "agent_link.take_control": "Tomar el control", + "agent_link.take_control_help": "Pausa al agente. Sus próximos cambios esperarán a que concedas acceso.", + "agent_link.title": "Conexión de agentes", + "agent_link.token": "Token de emparejamiento", + "agent_link.token_help": "Dale este token al agente como BENCHUP_AGENT_TOKEN. Se queda en este dispositivo. Quien lo tenga puede editar tu proyecto abierto a través del puente.", + "agent_link.token_none": "Todavía no hay token", + "agent_link.token_save_failed": "No se pudo guardar el token: {error}", + "agent_link.waiting_for_designer": "Esperando a que el diseñador conceda acceso", + "agent_link.waiting_for_server": "Esperando a que el servidor de colaboración confirme la selección", "app.about_benchup": "Acerca de BenchUp", "app.account": "Cuenta", "app.account_not_connected": "Cuenta · Sin conexión", diff --git a/assets/i18n/it_IT.json b/assets/i18n/it_IT.json index e87c40b..f15109f 100644 --- a/assets/i18n/it_IT.json +++ b/assets/i18n/it_IT.json @@ -41,6 +41,81 @@ "account.storage_unavailable": "Archiviazione non disponibile", "account.the_server_did_not_sign_out": "Il server non ha effettuato la disconnessione.", "account.use_https_or_http_on_localhost_for_development": "Usa HTTPS, oppure HTTP su localhost per lo sviluppo.", + "agent_link.allow": "Consenti connessione agenti", + "agent_link.allow_help": "Se attiva, BenchUp si collega a benchup bridge ed esegue le chiamate inviate da un agente. Ogni modifica è un passo di annullamento.", + "agent_link.an_agent": "Un agente", + "agent_link.chip.connected": "Agente collegato", + "agent_link.chip.designer": "Hai il controllo", + "agent_link.chip.editing": "L'agente sta modificando", + "agent_link.command_help": "Aggiunge BenchUp a Claude Code come server MCP con questo token.", + "agent_link.copied": "Copiato", + "agent_link.copy_command": "Copia comando Claude Code", + "agent_link.copy_json": "Copia configurazione MCP", + "agent_link.copy_shell": "Copia configurazione terminale", + "agent_link.copy_token": "Copia token", + "agent_link.deny": "Nega accesso", + "agent_link.error.app_busy": "Un'altra finestra di BenchUp è già collegata a questo bridge", + "agent_link.error.arguments_not_object": "Gli argomenti devono essere un oggetto JSON", + "agent_link.error.bridge": "Il bridge ha rifiutato la connessione: {message}", + "agent_link.error.busy": "BenchUp è rimasto occupato troppo a lungo; la chiamata non è stata eseguita", + "agent_link.error.calls_list": "calls deve essere un elenco non vuoto di chiamate, ognuna con il nome di uno strumento", + "agent_link.error.cannot_save_token": "Impossibile salvare il token in questo browser", + "agent_link.error.connect_timeout": "Il bridge non ha risposto", + "agent_link.error.denied": "Il progettista ha negato l'accesso al progetto", + "agent_link.error.denied_hint": "Sta lavorando in BenchUp. Attendi, poi chiediglielo prima di riprovare.", + "agent_link.error.disabled": "La connessione agenti è stata disattivata in BenchUp", + "agent_link.error.document_changed": "Il progetto aperto è cambiato prima dell'esecuzione di questa chiamata", + "agent_link.error.document_changed_hint": "Chiama get_scene per vedere il progetto aperto ora.", + "agent_link.error.handshake": "Il bridge non ha seguito il protocollo di abbinamento", + "agent_link.error.missing_tool": "La chiamata non indica alcuno strumento", + "agent_link.error.nested_session": "{tool} non può essere eseguito dentro run_batch", + "agent_link.error.no_browser": "Nessuna finestra del browser disponibile", + "agent_link.error.no_randomness": "Questo sistema non ha una fonte sicura di numeri casuali", + "agent_link.error.nothing_to_redo": "Niente da ripetere", + "agent_link.error.nothing_to_undo": "Niente da annullare", + "agent_link.error.protocol": "Questo bridge usa un'altra versione del protocollo; aggiorna il comando benchup", + "agent_link.error.queue_full": "Troppe chiamate in attesa in BenchUp", + "agent_link.error.result_too_large": "La risposta è più grande di quanto il bridge accetti ({size} MB)", + "agent_link.error.result_too_large_hint": "Chiedi meno dati alla volta: filtra get_scene o esegui il rendering di una vista più piccola.", + "agent_link.error.storage_unavailable": "L'archiviazione del browser non è disponibile", + "agent_link.error.taken_control": "Il progettista ha preso il controllo del progetto", + "agent_link.error.unproven": "Il bridge non conosce questo token di abbinamento", + "agent_link.generate": "Crea token", + "agent_link.grant": "Concedi accesso", + "agent_link.help": "Consente a un agente IA su questo computer di modificare il progetto aperto tramite il comando benchup bridge. BenchUp si collega al bridge su 127.0.0.1; nulla viene inviato ad altri computer.", + "agent_link.hide": "Nascondi", + "agent_link.history_label": "Agente: {summary}", + "agent_link.json_help": "Una voce mcpServers per altri client MCP.", + "agent_link.permission.body": "{client} vuole modificare il progetto.", + "agent_link.permission.busy": "Sei nel mezzo di un'operazione. Concedere l'accesso la annulla e lascia continuare l'agente.", + "agent_link.permission.designer": "Hai preso il controllo. Concedere l'accesso lascia continuare l'agente.", + "agent_link.permission.title": "Concedere l'accesso all'agente?", + "agent_link.permission.waiting": "Modifiche in attesa: {count}", + "agent_link.port": "Porta del bridge", + "agent_link.port_help": "La porta su cui ascolta benchup bridge (BENCHUP_BRIDGE_PORT). Il valore predefinito è 47615.", + "agent_link.queued_status": "BenchUp è occupato; la chiamata è in attesa", + "agent_link.regenerate": "Nuovo token", + "agent_link.regenerate_help": "Sostituisce il token. Gli agenti che usano quello vecchio vengono scollegati.", + "agent_link.release_control": "Lascia modificare l'agente", + "agent_link.release_control_help": "L'agente potrà di nuovo modificare il progetto senza chiedere mentre non usi uno strumento.", + "agent_link.setup": "Configura un agente", + "agent_link.setup_help": "Installa il comando benchup, poi aggiungilo al tuo agente con una di queste opzioni.", + "agent_link.shell_help": "Per agenti che eseguono comandi benchup: imposta il token, poi usa --live, ad esempio benchup scene --live.", + "agent_link.show": "Mostra", + "agent_link.status.connected": "Collegato: {client}", + "agent_link.status.connecting": "Ricerca di benchup bridge su questo computer…", + "agent_link.status.error": "Non collegato: {message}", + "agent_link.status.off": "Disattivata", + "agent_link.status.waiting": "Collegato al bridge. Nessun agente ha ancora chiamato.", + "agent_link.take_control": "Prendi il controllo", + "agent_link.take_control_help": "Mette in pausa l'agente. Le sue prossime modifiche attendono finché non concedi l'accesso.", + "agent_link.title": "Connessione agenti", + "agent_link.token": "Token di abbinamento", + "agent_link.token_help": "Fornisci questo token all'agente come BENCHUP_AGENT_TOKEN. Resta su questo dispositivo. Chiunque lo possieda può modificare il progetto aperto tramite il bridge.", + "agent_link.token_none": "Nessun token", + "agent_link.token_save_failed": "Impossibile salvare il token: {error}", + "agent_link.waiting_for_designer": "In attesa che il progettista conceda l'accesso", + "agent_link.waiting_for_server": "In attesa che il server di condivisione confermi la selezione", "app.about_benchup": "Informazioni su BenchUp", "app.account": "Account", "app.account_not_connected": "Account · Non connesso", diff --git a/assets/i18n/pt_BR.json b/assets/i18n/pt_BR.json index f86f027..f24a379 100644 --- a/assets/i18n/pt_BR.json +++ b/assets/i18n/pt_BR.json @@ -41,6 +41,81 @@ "account.storage_unavailable": "Armazenamento indisponível", "account.the_server_did_not_sign_out": "O servidor não encerrou a sessão.", "account.use_https_or_http_on_localhost_for_development": "Use HTTPS, ou HTTP no localhost para desenvolvimento.", + "agent_link.allow": "Permitir conexão de agentes", + "agent_link.allow_help": "Quando ativada, o BenchUp se conecta ao benchup bridge e executa as chamadas que um agente envia. Cada alteração é uma etapa de desfazer.", + "agent_link.an_agent": "Um agente", + "agent_link.chip.connected": "Agente conectado", + "agent_link.chip.designer": "Você tem o controle", + "agent_link.chip.editing": "O agente está editando", + "agent_link.command_help": "Adiciona o BenchUp ao Claude Code como servidor MCP com este token.", + "agent_link.copied": "Copiado", + "agent_link.copy_command": "Copiar comando do Claude Code", + "agent_link.copy_json": "Copiar configuração MCP", + "agent_link.copy_shell": "Copiar configuração do terminal", + "agent_link.copy_token": "Copiar token", + "agent_link.deny": "Negar acesso", + "agent_link.error.app_busy": "Outra janela do BenchUp já está conectada a esta ponte", + "agent_link.error.arguments_not_object": "Os argumentos devem ser um objeto JSON", + "agent_link.error.bridge": "A ponte recusou a conexão: {message}", + "agent_link.error.busy": "O BenchUp ficou ocupado por tempo demais; a chamada não foi executada", + "agent_link.error.calls_list": "calls deve ser uma lista não vazia de chamadas, cada uma com o nome de uma ferramenta", + "agent_link.error.cannot_save_token": "Não foi possível salvar o token neste navegador", + "agent_link.error.connect_timeout": "A ponte não respondeu", + "agent_link.error.denied": "O projetista negou o acesso ao projeto", + "agent_link.error.denied_hint": "Ele está trabalhando no BenchUp. Aguarde e pergunte antes de tentar novamente.", + "agent_link.error.disabled": "A conexão de agentes foi desativada no BenchUp", + "agent_link.error.document_changed": "O projeto aberto mudou antes de esta chamada ser executada", + "agent_link.error.document_changed_hint": "Chame get_scene para ver o projeto aberto agora.", + "agent_link.error.handshake": "A ponte não seguiu o protocolo de pareamento", + "agent_link.error.missing_tool": "A chamada não indica nenhuma ferramenta", + "agent_link.error.nested_session": "{tool} não pode ser executado dentro de run_batch", + "agent_link.error.no_browser": "Nenhuma janela do navegador está disponível", + "agent_link.error.no_randomness": "Este sistema não tem uma fonte segura de números aleatórios", + "agent_link.error.nothing_to_redo": "Nada para refazer", + "agent_link.error.nothing_to_undo": "Nada para desfazer", + "agent_link.error.protocol": "Esta ponte usa outra versão do protocolo; atualize o comando benchup", + "agent_link.error.queue_full": "Há chamadas demais aguardando no BenchUp", + "agent_link.error.result_too_large": "A resposta é maior do que a ponte aceita ({size} MB)", + "agent_link.error.result_too_large_hint": "Peça menos de uma vez: filtre get_scene ou renderize uma vista menor.", + "agent_link.error.storage_unavailable": "O armazenamento do navegador não está disponível", + "agent_link.error.taken_control": "O projetista assumiu o controle do projeto", + "agent_link.error.unproven": "A ponte não conhece este token de pareamento", + "agent_link.generate": "Criar token", + "agent_link.grant": "Conceder acesso", + "agent_link.help": "Permite que um agente de IA neste computador edite o projeto aberto pelo comando benchup bridge. O BenchUp se conecta à ponte em 127.0.0.1; nada é enviado a outros computadores.", + "agent_link.hide": "Ocultar", + "agent_link.history_label": "Agente: {summary}", + "agent_link.json_help": "Uma entrada mcpServers para outros clientes MCP.", + "agent_link.permission.body": "{client} quer alterar o projeto.", + "agent_link.permission.busy": "Você está no meio de uma operação. Conceder acesso a cancela e deixa o agente continuar.", + "agent_link.permission.designer": "Você assumiu o controle. Conceder acesso deixa o agente continuar editando.", + "agent_link.permission.title": "Conceder acesso ao agente?", + "agent_link.permission.waiting": "Alterações aguardando: {count}", + "agent_link.port": "Porta da ponte", + "agent_link.port_help": "A porta em que o benchup bridge escuta (BENCHUP_BRIDGE_PORT). O padrão é 47615.", + "agent_link.queued_status": "O BenchUp está ocupado; a chamada está aguardando", + "agent_link.regenerate": "Novo token", + "agent_link.regenerate_help": "Substitui o token. Agentes que usam o antigo são desconectados.", + "agent_link.release_control": "Deixar o agente editar", + "agent_link.release_control_help": "O agente poderá alterar o projeto novamente sem perguntar enquanto você não estiver usando uma ferramenta.", + "agent_link.setup": "Configurar um agente", + "agent_link.setup_help": "Instale o comando benchup e adicione-o ao seu agente com uma destas opções.", + "agent_link.shell_help": "Para agentes que executam comandos benchup: defina o token e use --live, por exemplo benchup scene --live.", + "agent_link.show": "Mostrar", + "agent_link.status.connected": "Conectado: {client}", + "agent_link.status.connecting": "Procurando o benchup bridge neste computador…", + "agent_link.status.error": "Sem conexão: {message}", + "agent_link.status.off": "Desativada", + "agent_link.status.waiting": "Conectado à ponte. Nenhum agente chamou ainda.", + "agent_link.take_control": "Assumir o controle", + "agent_link.take_control_help": "Pausa o agente. As próximas alterações dele esperam até você conceder acesso.", + "agent_link.title": "Conexão de agentes", + "agent_link.token": "Token de pareamento", + "agent_link.token_help": "Informe este token ao agente como BENCHUP_AGENT_TOKEN. Ele fica neste dispositivo. Quem o tiver pode editar seu projeto aberto pela ponte.", + "agent_link.token_none": "Ainda não há token", + "agent_link.token_save_failed": "Não foi possível salvar o token: {error}", + "agent_link.waiting_for_designer": "Aguardando o projetista conceder acesso", + "agent_link.waiting_for_server": "Aguardando o servidor de compartilhamento confirmar a seleção", "app.about_benchup": "Sobre o BenchUp", "app.account": "Conta", "app.account_not_connected": "Conta · Não conectada", diff --git a/assets/i18n/vi_VN.json b/assets/i18n/vi_VN.json index 1f5b0c5..8d75450 100644 --- a/assets/i18n/vi_VN.json +++ b/assets/i18n/vi_VN.json @@ -41,6 +41,81 @@ "account.storage_unavailable": "Không có bộ nhớ", "account.the_server_did_not_sign_out": "Máy chủ không đăng xuất.", "account.use_https_or_http_on_localhost_for_development": "Sử dụng HTTPS hoặc HTTP trên localhost để phát triển.", + "agent_link.allow": "Cho phép kết nối tác tử", + "agent_link.allow_help": "Khi bật, BenchUp kết nối tới benchup bridge và thực hiện các lệnh gọi do tác tử gửi. Mỗi thay đổi là một bước hoàn tác.", + "agent_link.an_agent": "Một tác tử", + "agent_link.chip.connected": "Tác tử đã kết nối", + "agent_link.chip.designer": "Bạn đang điều khiển", + "agent_link.chip.editing": "Tác tử đang chỉnh sửa", + "agent_link.command_help": "Thêm BenchUp vào Claude Code làm máy chủ MCP dùng mã này.", + "agent_link.copied": "Đã sao chép", + "agent_link.copy_command": "Sao chép lệnh Claude Code", + "agent_link.copy_json": "Sao chép cấu hình MCP", + "agent_link.copy_shell": "Sao chép thiết lập dòng lệnh", + "agent_link.copy_token": "Sao chép mã", + "agent_link.deny": "Từ chối", + "agent_link.error.app_busy": "Một cửa sổ BenchUp khác đã kết nối với cầu nối này", + "agent_link.error.arguments_not_object": "Đối số phải là một đối tượng JSON", + "agent_link.error.bridge": "Cầu nối đã từ chối kết nối: {message}", + "agent_link.error.busy": "BenchUp bận quá lâu; lệnh gọi không được thực hiện", + "agent_link.error.calls_list": "calls phải là danh sách lệnh gọi không rỗng, mỗi lệnh có tên công cụ", + "agent_link.error.cannot_save_token": "Không thể lưu mã trong trình duyệt này", + "agent_link.error.connect_timeout": "Cầu nối không phản hồi", + "agent_link.error.denied": "Nhà thiết kế đã từ chối quyền truy cập dự án", + "agent_link.error.denied_hint": "Họ đang làm việc trong BenchUp. Hãy chờ, rồi hỏi họ trước khi thử lại.", + "agent_link.error.disabled": "Kết nối tác tử đã bị tắt trong BenchUp", + "agent_link.error.document_changed": "Dự án đang mở đã thay đổi trước khi lệnh gọi này chạy", + "agent_link.error.document_changed_hint": "Gọi get_scene để xem dự án đang mở hiện tại.", + "agent_link.error.handshake": "Cầu nối không tuân theo giao thức ghép nối", + "agent_link.error.missing_tool": "Lệnh gọi không nêu công cụ nào", + "agent_link.error.nested_session": "Không thể chạy {tool} bên trong run_batch", + "agent_link.error.no_browser": "Không có cửa sổ trình duyệt nào", + "agent_link.error.no_randomness": "Hệ thống này không có nguồn số ngẫu nhiên an toàn", + "agent_link.error.nothing_to_redo": "Không có gì để làm lại", + "agent_link.error.nothing_to_undo": "Không có gì để hoàn tác", + "agent_link.error.protocol": "Cầu nối này dùng phiên bản giao thức khác; hãy cập nhật lệnh benchup", + "agent_link.error.queue_full": "Có quá nhiều lệnh gọi đang chờ trong BenchUp", + "agent_link.error.result_too_large": "Câu trả lời lớn hơn mức cầu nối chấp nhận ({size} MB)", + "agent_link.error.result_too_large_hint": "Hãy yêu cầu ít hơn mỗi lần: lọc get_scene hoặc kết xuất một khung nhìn nhỏ hơn.", + "agent_link.error.storage_unavailable": "Bộ nhớ trình duyệt không khả dụng", + "agent_link.error.taken_control": "Nhà thiết kế đã giành quyền điều khiển dự án", + "agent_link.error.unproven": "Cầu nối không biết mã ghép nối này", + "agent_link.generate": "Tạo mã", + "agent_link.grant": "Cấp quyền", + "agent_link.help": "Cho phép một tác tử AI trên máy tính này chỉnh sửa dự án đang mở thông qua lệnh benchup bridge. BenchUp kết nối tới cầu nối tại 127.0.0.1; không có gì được gửi tới máy tính khác.", + "agent_link.hide": "Ẩn", + "agent_link.history_label": "Tác tử: {summary}", + "agent_link.json_help": "Một mục mcpServers cho các ứng dụng MCP khác.", + "agent_link.permission.body": "{client} muốn thay đổi dự án.", + "agent_link.permission.busy": "Bạn đang thực hiện một thao tác. Cấp quyền sẽ hủy thao tác đó và để tác tử tiếp tục.", + "agent_link.permission.designer": "Bạn đã giành quyền điều khiển. Cấp quyền sẽ để tác tử tiếp tục chỉnh sửa.", + "agent_link.permission.title": "Cấp quyền cho tác tử?", + "agent_link.permission.waiting": "Thay đổi đang chờ: {count}", + "agent_link.port": "Cổng cầu nối", + "agent_link.port_help": "Cổng mà benchup bridge lắng nghe (BENCHUP_BRIDGE_PORT). Mặc định là 47615.", + "agent_link.queued_status": "BenchUp đang bận; lệnh gọi đang chờ", + "agent_link.regenerate": "Mã mới", + "agent_link.regenerate_help": "Thay mã. Các tác tử dùng mã cũ sẽ bị ngắt kết nối.", + "agent_link.release_control": "Để tác tử chỉnh sửa", + "agent_link.release_control_help": "Tác tử có thể lại thay đổi dự án mà không cần hỏi khi bạn không dùng công cụ nào.", + "agent_link.setup": "Thiết lập tác tử", + "agent_link.setup_help": "Cài đặt lệnh benchup, sau đó thêm nó vào tác tử bằng một trong các cách sau.", + "agent_link.shell_help": "Cho tác tử chạy lệnh benchup: đặt mã, rồi dùng --live, ví dụ benchup scene --live.", + "agent_link.show": "Hiện", + "agent_link.status.connected": "Đã kết nối: {client}", + "agent_link.status.connecting": "Đang tìm benchup bridge trên máy tính này…", + "agent_link.status.error": "Chưa kết nối: {message}", + "agent_link.status.off": "Tắt", + "agent_link.status.waiting": "Đã kết nối với cầu nối. Chưa có tác tử nào gọi.", + "agent_link.take_control": "Giành quyền điều khiển", + "agent_link.take_control_help": "Tạm dừng tác tử. Các thay đổi tiếp theo của nó sẽ chờ bạn cấp quyền.", + "agent_link.title": "Kết nối tác tử", + "agent_link.token": "Mã ghép nối", + "agent_link.token_help": "Cung cấp mã này cho tác tử dưới dạng BENCHUP_AGENT_TOKEN. Mã chỉ lưu trên thiết bị này. Bất kỳ ai có mã đều có thể chỉnh sửa dự án đang mở qua cầu nối.", + "agent_link.token_none": "Chưa có mã", + "agent_link.token_save_failed": "Không thể lưu mã: {error}", + "agent_link.waiting_for_designer": "Đang chờ nhà thiết kế cấp quyền", + "agent_link.waiting_for_server": "Đang chờ máy chủ chia sẻ xác nhận vùng chọn", "app.about_benchup": "Giới thiệu BenchUp", "app.account": "Tài khoản", "app.account_not_connected": "Tài khoản · Chưa kết nối", diff --git a/assets/i18n/zh_CN.json b/assets/i18n/zh_CN.json index 5201d54..8e49627 100644 --- a/assets/i18n/zh_CN.json +++ b/assets/i18n/zh_CN.json @@ -41,6 +41,81 @@ "account.storage_unavailable": "存储不可用", "account.the_server_did_not_sign_out": "服务器未执行退出登录。", "account.use_https_or_http_on_localhost_for_development": "开发时请使用 HTTPS,或在 localhost 上使用 HTTP。", + "agent_link.allow": "允许智能体连接", + "agent_link.allow_help": "开启后,BenchUp 会连接到 benchup bridge 并执行智能体发送的调用。每次更改都是一个撤销步骤。", + "agent_link.an_agent": "一个智能体", + "agent_link.chip.connected": "智能体已连接", + "agent_link.chip.designer": "由你控制", + "agent_link.chip.editing": "智能体正在编辑", + "agent_link.command_help": "将 BenchUp 作为使用此令牌的 MCP 服务器添加到 Claude Code。", + "agent_link.copied": "已复制", + "agent_link.copy_command": "复制 Claude Code 命令", + "agent_link.copy_json": "复制 MCP 配置", + "agent_link.copy_shell": "复制终端设置", + "agent_link.copy_token": "复制令牌", + "agent_link.deny": "拒绝访问", + "agent_link.error.app_busy": "另一个 BenchUp 窗口已连接到此桥接程序", + "agent_link.error.arguments_not_object": "参数必须是 JSON 对象", + "agent_link.error.bridge": "桥接程序拒绝了连接:{message}", + "agent_link.error.busy": "BenchUp 忙碌时间过长;调用未执行", + "agent_link.error.calls_list": "calls 必须是非空的调用列表,每个调用都需包含工具名称", + "agent_link.error.cannot_save_token": "无法在此浏览器中保存令牌", + "agent_link.error.connect_timeout": "桥接程序没有响应", + "agent_link.error.denied": "设计者拒绝了对项目的访问", + "agent_link.error.denied_hint": "对方正在 BenchUp 中工作。请等待,并在重试前先询问对方。", + "agent_link.error.disabled": "BenchUp 中的智能体连接已关闭", + "agent_link.error.document_changed": "在此调用执行前,打开的项目已更改", + "agent_link.error.document_changed_hint": "调用 get_scene 查看当前打开的项目。", + "agent_link.error.handshake": "桥接程序未遵循配对协议", + "agent_link.error.missing_tool": "调用未指定任何工具", + "agent_link.error.nested_session": "{tool} 不能在 run_batch 中运行", + "agent_link.error.no_browser": "没有可用的浏览器窗口", + "agent_link.error.no_randomness": "此系统没有安全的随机数来源", + "agent_link.error.nothing_to_redo": "没有可重做的操作", + "agent_link.error.nothing_to_undo": "没有可撤销的操作", + "agent_link.error.protocol": "此桥接程序使用不同的协议版本;请更新 benchup 命令", + "agent_link.error.queue_full": "BenchUp 中等待的调用过多", + "agent_link.error.result_too_large": "回复大于桥接程序可接受的大小({size} MB)", + "agent_link.error.result_too_large_hint": "一次请求更少的内容:筛选 get_scene,或渲染更小的视图。", + "agent_link.error.storage_unavailable": "浏览器存储不可用", + "agent_link.error.taken_control": "设计者已接管项目的控制", + "agent_link.error.unproven": "桥接程序不知道此配对令牌", + "agent_link.generate": "创建令牌", + "agent_link.grant": "授予访问权限", + "agent_link.help": "允许本机上的 AI 智能体通过 benchup bridge 命令编辑当前打开的项目。BenchUp 连接到 127.0.0.1 上的桥接程序,不会向其他计算机发送任何内容。", + "agent_link.hide": "隐藏", + "agent_link.history_label": "智能体:{summary}", + "agent_link.json_help": "供其他 MCP 客户端使用的 mcpServers 条目。", + "agent_link.permission.body": "{client} 想要更改项目。", + "agent_link.permission.busy": "你正在进行一项操作。授予访问权限会取消该操作并让智能体继续。", + "agent_link.permission.designer": "你已接管控制。授予访问权限会让智能体继续编辑。", + "agent_link.permission.title": "授予智能体访问权限?", + "agent_link.permission.waiting": "等待中的更改:{count}", + "agent_link.port": "桥接端口", + "agent_link.port_help": "benchup bridge 监听的端口(BENCHUP_BRIDGE_PORT)。默认为 47615。", + "agent_link.queued_status": "BenchUp 正忙;调用正在等待", + "agent_link.regenerate": "新令牌", + "agent_link.regenerate_help": "替换令牌。使用旧令牌的智能体将断开连接。", + "agent_link.release_control": "让智能体编辑", + "agent_link.release_control_help": "在你未使用工具时,智能体可以再次无需询问地更改项目。", + "agent_link.setup": "设置智能体", + "agent_link.setup_help": "安装 benchup 命令,然后用以下任一方式将其添加到你的智能体。", + "agent_link.shell_help": "适用于运行 benchup 命令的智能体:设置令牌,然后使用 --live,例如 benchup scene --live。", + "agent_link.show": "显示", + "agent_link.status.connected": "已连接:{client}", + "agent_link.status.connecting": "正在本机上查找 benchup bridge…", + "agent_link.status.error": "未连接:{message}", + "agent_link.status.off": "已关闭", + "agent_link.status.waiting": "已连接到桥接程序。尚无智能体调用。", + "agent_link.take_control": "接管控制", + "agent_link.take_control_help": "暂停智能体。它接下来的更改将等待你授予访问权限。", + "agent_link.title": "智能体连接", + "agent_link.token": "配对令牌", + "agent_link.token_help": "将此令牌作为 BENCHUP_AGENT_TOKEN 提供给智能体。它只保存在本设备上。持有它的任何人都可以通过桥接程序编辑你打开的项目。", + "agent_link.token_none": "尚无令牌", + "agent_link.token_save_failed": "无法保存令牌:{error}", + "agent_link.waiting_for_designer": "正在等待设计者授予访问权限", + "agent_link.waiting_for_server": "正在等待共享服务器确认所选内容", "app.about_benchup": "关于 BenchUp", "app.account": "账户", "app.account_not_connected": "账户 · 未连接", diff --git a/crates/benchup-app/Cargo.toml b/crates/benchup-app/Cargo.toml index e3b5d2e..f5a7df5 100644 --- a/crates/benchup-app/Cargo.toml +++ b/crates/benchup-app/Cargo.toml @@ -14,12 +14,16 @@ serde_json.workspace = true flate2 = { version = "1", default-features = false, features = ["rust_backend"] } skrifa = "0.44" glam.workspace = true -eframe = { version = "0.37", default-features = false, features = ["default_fonts", "wgpu", "x11", "wayland", "accesskit", "persistence", "links"] } +# `clipboard`: copied text (such as the agent pairing token) reaches the system +# clipboard on the desktop, not only BenchUp. +eframe = { version = "0.37", default-features = false, features = ["default_fonts", "wgpu", "x11", "wayland", "accesskit", "persistence", "links", "clipboard"] } bytemuck = { version = "1", features = ["derive"] } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp"] } zip = { version = "8", default-features = false } rfd = { version = "0.17.2", features = ["file-handle-inner"] } sys-locale = { version = "0.3.2", features = ["js"] } +# Agent connection pairing tokens and nonces (OS randomness, Web Crypto on the web). +uuid = { version = "1", features = ["v4", "js"] } [target.'cfg(target_arch = "wasm32")'.dependencies] wgpu = { version = "30", features = ["webgpu", "webgl"] } diff --git a/crates/benchup-app/src/agent_link.rs b/crates/benchup-app/src/agent_link.rs new file mode 100644 index 0000000..9a00129 --- /dev/null +++ b/crates/benchup-app/src/agent_link.rs @@ -0,0 +1,1239 @@ +//! Agent connection: lets an AI agent on this computer edit the open project +//! through `benchup bridge` (see `crates/benchup-cli/src/bridge.rs`). +//! +//! A browser tab cannot listen on a port, so BenchUp connects out to the +//! bridge on `ws://127.0.0.1:`, on the web and on the desktop. It +//! proves it knows the pairing token with a challenge-response (the token is +//! never sent) and checks that the bridge knows it too, then runs the calls it +//! receives and answers each with a result: +//! * bridge to app: `challenge {nonce, protocol}`, `ready {proof}`, `error`, +//! `cancel {id}`, `call {id, name, arguments, client, deadline_ms}`; +//! * app to bridge: `authenticate` (sent by the socket), `prove {nonce, +//! proof}`, `result {id, ok, result}`, `status {message}`. +//! +//! Every call that changes the model is one undo step, `Agent: …`, recorded +//! like the designer's own edits, so shared projects keep their read-only +//! state and edit leases. Calls run in arrival order, a few per frame, from +//! `App::logic`, so a minimized window or hidden tab still answers them. +//! Rendered views are made off the UI thread where threads exist. +//! +//! Control. Reading calls always run, even while changes queued before them +//! wait. A changing call (or one that changes the selection) runs at once unless +//! the designer is in the middle of an operation (drawing, pushing, typing a +//! value, …) or has taken control; then BenchUp asks them to grant or deny +//! access. Granting cancels their operation and lets the agent continue; +//! denying refuses the waiting calls. While the agent is editing, the status +//! bar offers "Take control", after which its changes wait for a grant again. +use super::*; +use benchup_tools::{Call, ToolEnv, batch, pairing}; +use serde_json::{Value, json}; +use std::collections::VecDeque; + +#[path = "agent_link_ui.rs"] +mod ui; +#[cfg(test)] +#[path = "agent_link_tests.rs"] +mod tests; + +pub(super) const DEFAULT_PORT: u16 = pairing::DEFAULT_PORT; +const BACKOFF_START: f64 = 1.; +const BACKOFF_MAX: f64 = 10.; +/// The bridge must answer the handshake within this long. +const READY_TIMEOUT: f64 = 15.; +/// The bridge gives up on a call after 120 s; answer first. +const QUEUE_TIMEOUT: f64 = 100.; +const QUEUE_LIMIT: usize = 64; +/// Calls run per frame, so a burst cannot freeze the UI. +const CALLS_PER_FRAME: usize = 8; +const MAX_CLIENT_CHARS: usize = pairing::MAX_CLIENT_CHARS; +/// The largest message accepted from the bridge (the browser build allows +/// about the same); the bridge refuses larger calls itself. +const MAX_MESSAGE_BYTES: usize = pairing::MAX_APP_MESSAGE_BYTES; +/// The largest result sent: the bridge drops the connection on anything +/// bigger, so a larger answer is replaced with an error. +const MAX_RESULT_BYTES: usize = pairing::MAX_BRIDGE_MESSAGE_BYTES; +/// A call is not run in the last second before its deadline. +const DEADLINE_MARGIN_MS: u64 = 1000; +/// How long to wait before trying again to make or save a token. +const TOKEN_RETRY: f64 = 30.; +/// Granted control returns to normal after the agent is quiet this long. +const AGENT_IDLE: f64 = 8.; +const TOKEN_FILE: &str = "agent-link.token"; +const TOKEN_STORAGE: &str = "benchup.agent_link.token"; + +/// Remembered across restarts (in `GeneralSettings`). The pairing token is +/// deliberately not here: it stays in this device's private storage. +#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] +#[serde(default)] +pub(super) struct Preferences { + pub enabled: bool, + pub port: u16, +} + +impl Default for Preferences { + fn default() -> Self { + Self { enabled: false, port: DEFAULT_PORT } + } +} + +impl Preferences { + pub(super) fn normalize(&mut self) { + if self.port < 1024 { + self.port = DEFAULT_PORT; + } + } +} + +#[derive(Clone, Debug, PartialEq)] +pub(super) enum Status { + Off, + Connecting, + Connected { client: Option }, + Error(String), +} + +/// Who may change the model right now. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub(super) enum Control { + /// The agent edits while the designer is idle; otherwise it asks. + #[default] + Shared, + /// The designer granted access; the agent edits until it goes quiet. + Agent, + /// The designer took control; every change waits for a grant. + Designer, +} + +/// One live connection, abstracted so tests need no bridge. +pub(super) trait Link { + /// Everything received since the last call. An `Err` ends the connection. + fn events(&mut self) -> Vec>; + fn send(&mut self, message: Value) -> Result<(), String>; +} +pub(super) trait Connector { + /// Starts connecting; the socket sends the `authenticate` hello itself. + fn connect(&mut self, ctx: &egui::Context, url: &str, connection: &str) -> Box; +} + +struct SocketLink(crate::realtime::Socket); +impl Link for SocketLink { + fn events(&mut self) -> Vec> { + use std::sync::mpsc::TryRecvError; + let mut events = Vec::new(); + loop { + match self.0.events.try_recv() { + Ok(event) => events.push(event), + Err(TryRecvError::Empty) => break, + Err(TryRecvError::Disconnected) => { + events.push(Err(tr!("realtime.websocket_disconnected").into())); + break; + } + } + } + events + } + fn send(&mut self, message: Value) -> Result<(), String> { + self.0.send(message) + } +} +struct SocketConnector; +impl Connector for SocketConnector { + fn connect(&mut self, ctx: &egui::Context, url: &str, connection: &str) -> Box { + // The ticket stays empty: the token is proved, never sent. + Box::new(SocketLink(crate::realtime::Socket::connect_limited( + ctx, + url.into(), + String::new(), + connection.into(), + Some(MAX_MESSAGE_BYTES), + ))) + } +} + +/// What the bridge can send. +#[derive(Debug, PartialEq)] +pub(super) enum Frame { + Challenge { nonce: String, protocol: Option }, + Ready { proof: Option, protocol: Option }, + /// A refusal: its `code` (translated when known) and English text. + Error { code: Option, message: String }, + Cancel { id: String }, + Call { id: String, client: String, deadline_ms: Option, call: Result<(String, Value), String> }, + /// Unknown type, or not an object: ignored. + Ignored, +} + +/// Characters shown in the UI: not control or invisible formatting +/// characters (bidi overrides, zero-width characters, tag characters). +fn printable(c: char) -> bool { + !c.is_control() + && !matches!(c as u32, + 0xAD | 0x600..=0x605 | 0x61C | 0x6DD | 0x70F | 0x180E + | 0x200B..=0x200F | 0x2028..=0x202E | 0x2060..=0x206F + | 0xFEFF | 0xFFF9..=0xFFFB | 0xE0001 | 0xE0020..=0xE007F) +} +pub(super) fn clean_client(client: &str) -> String { + let clean: String = client.chars().filter(|c| printable(*c)).collect(); + clean.trim().chars().take(MAX_CLIENT_CHARS).collect::().trim().to_owned() +} + +/// Seconds for the connection's timers. Not `egui::InputState::time`, which +/// stands still while the window is minimized or the tab is hidden. +pub(super) fn clock() -> f64 { + now_ms() as f64 / 1000. +} + +/// The length of `value` as JSON text, without building the text. +fn json_len(value: &Value) -> usize { + struct Count(usize); + impl std::io::Write for Count { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + self.0 += bytes.len(); + Ok(bytes.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let mut count = Count(0); + let _ = serde_json::to_writer(&mut count, value); + count.0 +} + +/// Unix time in milliseconds (the unit of a call's `deadline_ms`). +fn now_ms() -> u64 { + #[cfg(not(target_arch = "wasm32"))] + { + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_millis() as u64 + } + #[cfg(target_arch = "wasm32")] + { + js_sys::Date::now() as u64 + } +} + +/// 32 random bytes from the OS (native) or Web Crypto (browser) as 64 +/// lowercase hex characters, or an error if there is no secure randomness. +fn new_nonce() -> Result { + std::panic::catch_unwind(|| { + let (a, b) = (uuid::Uuid::new_v4(), uuid::Uuid::new_v4()); + let bytes: Vec = a.as_bytes().iter().chain(b.as_bytes()).copied().collect(); + pairing::to_hex(&bytes) + }) + .map_err(|_| tr!("agent_link.error.no_randomness").to_owned()) +} + +/// A new random version-4 UUID. If no secure randomness is available no +/// token is made: it must never be guessable. +pub(super) fn new_token() -> Result { + // `uuid` panics rather than return weak bytes when the system RNG fails. + std::panic::catch_unwind(|| uuid::Uuid::new_v4().to_string()) + .map_err(|_| tr!("agent_link.error.no_randomness").to_owned()) +} + +/// Strict frame parsing: anything unexpected is `Ignored`, except a call that +/// has an id, which is always answered (with an error if it is malformed). +pub(super) fn parse_frame(value: &Value) -> Frame { + let Some(object) = value.as_object() else { return Frame::Ignored }; + match object.get("type").and_then(Value::as_str) { + Some("challenge") => Frame::Challenge { + nonce: object.get("nonce").and_then(Value::as_str).unwrap_or_default().to_owned(), + protocol: object.get("protocol").and_then(Value::as_u64), + }, + Some("ready") => Frame::Ready { + proof: object.get("proof").and_then(Value::as_str).map(str::to_owned), + protocol: object.get("protocol").and_then(Value::as_u64), + }, + Some("cancel") => match object.get("id").and_then(Value::as_str).filter(|id| !id.is_empty()) { + Some(id) => Frame::Cancel { id: id.to_owned() }, + None => Frame::Ignored, + }, + Some("error") => Frame::Error { + code: object.get("code").and_then(Value::as_str).map(str::to_owned), + message: object + .get("message") + .and_then(Value::as_str) + .unwrap_or_default() + .chars() + .filter(|c| printable(*c)) + .take(200) + .collect(), + }, + Some("call") => { + let Some(id) = object.get("id").and_then(Value::as_str).filter(|id| !id.is_empty()) else { + return Frame::Ignored; + }; + let client = object.get("client").and_then(Value::as_str).map(clean_client).unwrap_or_default(); + let call = (|| { + let name = object + .get("name") + .and_then(Value::as_str) + .filter(|name| !name.is_empty()) + .ok_or_else(|| tr!("agent_link.error.missing_tool").to_owned())?; + let arguments = match object.get("arguments") { + None | Some(Value::Null) => json!({}), + Some(arguments) if arguments.is_object() => arguments.clone(), + Some(_) => return Err(tr!("agent_link.error.arguments_not_object").to_owned()), + }; + Ok((name.to_owned(), arguments)) + })(); + let deadline_ms = object.get("deadline_ms").and_then(|v| { + v.as_u64().or_else(|| v.as_f64().filter(|f| f.is_finite() && *f >= 0.).map(|f| f as u64)) + }); + Frame::Call { id: id.to_owned(), client, deadline_ms, call } + } + _ => Frame::Ignored, + } +} + +/// Whether a call can change the model, so it may need the designer's grant. +pub(super) fn changes_model(name: &str, arguments: &Value) -> bool { + match name { + "undo" => true, + "render_view" => false, + "run_batch" => { + !arguments.get("dry_run").and_then(Value::as_bool).unwrap_or(false) + && arguments.get("calls").and_then(Value::as_array).is_some_and(|calls| { + calls.iter().any(|call| call.get("tool").and_then(Value::as_str).is_some_and(benchup_tools::is_mutating)) + }) + } + name => benchup_tools::is_mutating(name), + } +} + +/// Whether a call replaces the designer's selection (the `select` tool). +pub(super) fn changes_selection(name: &str, arguments: &Value) -> bool { + match name { + "select" => true, + "run_batch" => { + !arguments.get("dry_run").and_then(Value::as_bool).unwrap_or(false) + && arguments.get("calls").and_then(Value::as_array).is_some_and(|calls| { + calls.iter().any(|call| call.get("tool").and_then(Value::as_str) == Some("select")) + }) + } + _ => false, + } +} + +/// Whether a call must wait for the designer when they are busy or have +/// taken control: it changes the model, or swaps the selection an operation +/// in progress works on. +pub(super) fn needs_grant(name: &str, arguments: &Value) -> bool { + changes_model(name, arguments) || changes_selection(name, arguments) +} + +struct Queued { + id: String, + client: String, + name: String, + arguments: Value, + /// Changes the model or the selection, so it may need a grant. + changes: bool, + queued_at: f64, + /// The bridge was told the call is waiting. + notified: bool, + /// Unix ms after which the bridge no longer wants an answer. + deadline_ms: u64, + /// The document epoch when the call arrived. + epoch: u64, +} + +/// Where the pairing handshake is. +#[derive(Clone, Debug, PartialEq)] +enum Phase { + /// Waiting for the bridge's `challenge`. + Challenge, + /// `prove` sent; waiting for a `ready` carrying the bridge's proof. + Proved { bridge_nonce: String, app_nonce: String }, + /// The bridge proved the token. Only now are calls accepted. + Ready, +} + +struct Active { + link: Box, + port: u16, + token: String, + started: f64, + phase: Phase, +} + +pub(super) struct AgentLink { + pub status: Status, + /// The pairing token, loaded from this device on demand. + pub token: Option, + pub token_error: Option, + token_checked: bool, + /// After a failure, no new token is tried before this time. + token_retry_at: f64, + /// Makes tokens; replaced in tests to simulate a missing RNG. + generator: fn() -> Result, + /// Session-only: revealing a secret is never remembered. + pub show_token: bool, + active: Option, + connector: Box, + queue: VecDeque, + retry_at: f64, + backoff: f64, + /// The open document, refreshed by the app every frame: replies carry it. + document_name: String, + epoch: u64, + /// Makes the app's handshake nonce; replaced in tests. + nonce_source: fn() -> Result, + pub control: Control, + /// The grant/deny dialog is waiting for the designer. + pub asking: bool, + /// The designer denied access; further changes are refused without + /// asking until they finish what they are doing. + denied: bool, + /// When the agent last changed the model. + last_change: Option, + /// The last copy button pressed and until when "Copied" shows (session only). + copied: Option<(ui::CopiedText, f64)>, + /// The `render_view` being made on another thread: its call id, and + /// where the answer arrives. + #[cfg(not(target_arch = "wasm32"))] + rendering: Option<(String, std::sync::mpsc::Receiver<(bool, Value)>)>, + /// Results larger than this are refused (lowered in tests). + max_result_bytes: usize, + /// Token storage; tests use a temporary slot. + #[cfg(test)] + token_slot: Option, +} + +impl Default for AgentLink { + fn default() -> Self { + Self::with_connector(Box::new(SocketConnector)) + } +} + +/// `claude mcp add` command for this token (and port, when not the default). +/// The token goes in the environment, never in the program's arguments (which +/// other local users can read). +pub(super) fn claude_command(token: &str, port: u16) -> String { + let port = if port == DEFAULT_PORT { String::new() } else { format!(" --port {port}") }; + format!("claude mcp add benchup -e BENCHUP_AGENT_TOKEN={token} -- benchup bridge{port}") +} + +/// Generic `mcpServers` entry; the token travels in the environment. +pub(super) fn mcp_json(token: &str, port: u16) -> String { + let mut args = vec![json!("bridge")]; + if port != DEFAULT_PORT { + args.extend([json!("--port"), json!(port.to_string())]); + } + serde_json::to_string_pretty(&json!({ + "mcpServers": {"benchup": {"command": "benchup", "args": args, "env": {"BENCHUP_AGENT_TOKEN": token}}} + })) + .unwrap_or_default() +} + +/// Shell lines for agents that use the `benchup` command (`--live`). +pub(super) fn shell_setup(token: &str, port: u16) -> String { + let port = if port == DEFAULT_PORT { String::new() } else { format!("\nexport BENCHUP_BRIDGE_PORT={port}") }; + format!("export BENCHUP_AGENT_TOKEN={token}{port}\nbenchup scene --live") +} + +impl AgentLink { + pub(super) fn with_connector(connector: Box) -> Self { + Self { + status: Status::Off, + token: None, + token_error: None, + token_checked: false, + token_retry_at: 0., + generator: new_token, + show_token: false, + active: None, + connector, + queue: VecDeque::new(), + retry_at: 0., + backoff: BACKOFF_START, + document_name: String::new(), + epoch: 0, + nonce_source: new_nonce, + control: Control::Shared, + asking: false, + denied: false, + last_change: None, + copied: None, + #[cfg(not(target_arch = "wasm32"))] + rendering: None, + max_result_bytes: MAX_RESULT_BYTES, + #[cfg(test)] + token_slot: None, + } + } + + fn stored_token(&self) -> Option { + #[cfg(test)] + if let Some(dir) = &self.token_slot { + return super::credentials::load_in(dir, TOKEN_FILE); + } + super::credentials::load(TOKEN_FILE, TOKEN_STORAGE) + } + fn store_token(&self, token: &str) -> Result<(), String> { + #[cfg(test)] + if let Some(dir) = &self.token_slot { + return super::credentials::save_in(dir, TOKEN_FILE, token); + } + super::credentials::save(TOKEN_FILE, TOKEN_STORAGE, token) + } + + /// Reads the saved token, without creating one (safe to call from the UI). + pub(super) fn load_token(&mut self) { + if !self.token_checked { + self.token_checked = true; + self.token = self.stored_token(); + } + } + /// The saved token, or a new one saved now; `None` if none could be made + /// (the reason is in `token_error`). After a failure it waits + /// `TOKEN_RETRY` before trying again, rather than write every frame. + fn ensure_token(&mut self, now: f64) -> Option { + self.load_token(); + if self.token.is_none() && now >= self.token_retry_at { + self.regenerate(); + if self.token.is_none() { + self.token_retry_at = now + TOKEN_RETRY; + } + } + self.token.clone() + } + /// Replaces the token. Connections made with the old one are dropped. If + /// a new token cannot be made or saved, the old one stays (an unsaved + /// token would stop pairing after a restart) and `token_error` says why. + pub(super) fn regenerate(&mut self) { + let made = (self.generator)().and_then(|token| self.store_token(&token).map(|()| token)); + match made { + Ok(token) => { + self.token_error = None; + self.token_checked = true; + self.token = Some(token); + } + Err(error) => self.token_error = Some(error), + } + } + + pub(super) fn connected(&self) -> bool { + matches!(self.status, Status::Connected { .. }) + } + /// The agent changed the model recently, or is waiting to. + pub(super) fn editing(&self, now: f64) -> bool { + self.control == Control::Agent + || self.queue.iter().any(|queued| queued.changes) + || self.last_change.is_some_and(|at| now - at < AGENT_IDLE) + } + /// Who is asking, for the dialog. + pub(super) fn waiting_client(&self) -> Option<&str> { + self.queue.iter().find(|queued| queued.changes).map(|queued| queued.client.as_str()) + } + pub(super) fn waiting_changes(&self) -> usize { + self.queue.iter().filter(|queued| queued.changes).count() + } + pub(super) fn client_name(&self) -> Option<&str> { + match &self.status { + Status::Connected { client: Some(client) } => Some(client), + _ => None, + } + } + + fn send(&mut self, message: Value) { + if let Some(active) = &mut self.active { + let _ = active.link.send(message); + } + } + /// A `result` frame; every result object says which project was open, so + /// an agent can notice a switch. + fn result_frame(&self, id: &str, ok: bool, mut result: Value) -> Value { + if let Some(object) = result.as_object_mut() { + object.insert("document".into(), json!({"name": self.document_name})); + } + json!({"type": "result", "id": id, "ok": ok, "result": result}) + } + /// Sends a result, or an error if it is too large for the bridge. + /// `applied`: the call changed the model, which stays changed. + fn send_result(&mut self, id: &str, ok: bool, result: Value, applied: bool) { + let mut frame = self.result_frame(id, ok, result); + if json_len(&frame) > self.max_result_bytes { + let mut refusal = json!({"ok": false, + "error": tr!("agent_link.error.result_too_large", size = self.max_result_bytes / (1024 * 1024)), + "hint": tr!("agent_link.error.result_too_large_hint")}); + if applied { + refusal["applied"] = json!(true); + } + frame = self.result_frame(id, false, refusal); + } + self.send(frame); + } + fn refuse(&mut self, id: &str, error: &str, hint: Option<&str>) { + let mut result = json!({"ok": false, "error": error}); + if let Some(hint) = hint { + result["hint"] = json!(hint); + } + self.send_result(id, false, result, false); + } + /// Refuses every waiting call that would change the model. + fn refuse_changes(&mut self, error: &str, hint: Option<&str>) { + let (refused, kept): (VecDeque, VecDeque) = + std::mem::take(&mut self.queue).into_iter().partition(|queued| queued.changes); + self.queue = kept; + for queued in refused { + self.refuse(&queued.id, error, hint); + } + self.asking = false; + } + + /// The designer takes control: waiting changes are refused and later ones + /// wait for a grant. + pub(super) fn take_control(&mut self) { + self.control = Control::Designer; + self.last_change = None; + self.refuse_changes(tr!("agent_link.error.taken_control"), Some(tr!("agent_link.error.denied_hint"))); + } + /// The designer lets the agent edit again without asking. + pub(super) fn release_control(&mut self) { + self.control = Control::Shared; + self.denied = false; + } + /// The designer refused the waiting changes. + pub(super) fn deny(&mut self) { + self.denied = true; + self.refuse_changes(tr!("agent_link.error.denied"), Some(tr!("agent_link.error.denied_hint"))); + } + + /// Drops the connection; queued calls can no longer be answered. + fn stop(&mut self) { + self.active = None; + self.queue.clear(); + self.asking = false; + #[cfg(not(target_arch = "wasm32"))] + { + self.rendering = None; + } + } + fn fail(&mut self, now: f64, message: String) { + self.stop(); + self.status = Status::Error(message); + self.retry_at = now + self.backoff; + self.backoff = (self.backoff * 2.).min(BACKOFF_MAX); + } + + /// Keeps the connection in the state the preferences ask for, and + /// collects incoming calls into the queue. Never blocks. + pub(super) fn pump(&mut self, ctx: &egui::Context, now: f64, prefs: &Preferences) { + if !prefs.enabled { + if let Some(mut active) = self.active.take() { + // Answer what is waiting, then close. + for queued in std::mem::take(&mut self.queue) { + let reply = self.result_frame(&queued.id, false, json!({"ok": false, "error": tr!("agent_link.error.disabled")})); + let _ = active.link.send(reply); + } + } + self.stop(); + self.status = Status::Off; + self.control = Control::Shared; + self.backoff = BACKOFF_START; + self.retry_at = 0.; + return; + } + let Some(token) = self.ensure_token(now) else { + // No secure token, so nothing connects. + self.stop(); + self.status = Status::Error(self.token_error.clone().unwrap_or_default()); + return; + }; + if self.active.as_ref().is_some_and(|a| a.port != prefs.port || a.token != token) { + self.stop(); + self.backoff = BACKOFF_START; + self.retry_at = 0.; + } + if self.active.is_none() { + if now < self.retry_at { + ctx.request_repaint_after(Duration::from_secs_f64(self.retry_at - now)); + return; + } + let url = format!("ws://127.0.0.1:{}", prefs.port); + let link = self.connector.connect(ctx, &url, concat!("benchup-app-", env!("CARGO_PKG_VERSION"))); + self.active = Some(Active { link, port: prefs.port, token: token.clone(), started: now, phase: Phase::Challenge }); + self.status = Status::Connecting; + } + let Some(active) = &mut self.active else { return }; + let events = active.link.events(); + let started = active.started; + for event in events { + let value = match event { + Ok(value) => value, + Err(message) => return self.fail(now, message), + }; + let Some(phase) = self.active.as_ref().map(|a| a.phase.clone()) else { return }; + match parse_frame(&value) { + Frame::Challenge { nonce, protocol } => { + // Exactly one challenge, first. + if phase != Phase::Challenge || !pairing::valid_nonce(&nonce) { + return self.fail(now, tr!("agent_link.error.handshake").into()); + } + if protocol != Some(u64::from(pairing::PROTOCOL)) { + return self.fail(now, tr!("agent_link.error.protocol").into()); + } + let app_nonce = match (self.nonce_source)() { + Ok(nonce) => nonce, + Err(message) => return self.fail(now, message), + }; + let proof = pairing::app_proof(&token, &nonce, &app_nonce); + self.send(json!({"type": "prove", "nonce": app_nonce, "proof": proof})); + if let Some(active) = &mut self.active { + active.phase = Phase::Proved { bridge_nonce: nonce, app_nonce }; + } + } + Frame::Ready { proof, protocol } => match phase { + Phase::Challenge => return self.fail(now, tr!("agent_link.error.protocol").into()), + Phase::Proved { bridge_nonce, app_nonce } => { + if protocol.is_some_and(|p| p != u64::from(pairing::PROTOCOL)) { + return self.fail(now, tr!("agent_link.error.protocol").into()); + } + let expected = pairing::bridge_proof(&token, &bridge_nonce, &app_nonce); + if !proof.as_deref().is_some_and(|proof| pairing::proofs_match(proof, &expected)) { + return self.fail(now, tr!("agent_link.error.unproven").into()); + } + if let Some(active) = &mut self.active { + active.phase = Phase::Ready; + } + self.status = Status::Connected { client: None }; + self.backoff = BACKOFF_START; + } + Phase::Ready => {} + }, + Frame::Error { code, message } => { + let reason = match code.as_deref() { + Some(pairing::refusal::APP_BUSY) => tr!("agent_link.error.app_busy").to_owned(), + Some(pairing::refusal::TOKEN_MISMATCH) => tr!("agent_link.error.unproven").to_owned(), + Some(pairing::refusal::UNKNOWN_ROLE | pairing::refusal::UNEXPECTED) => { + tr!("agent_link.error.protocol").to_owned() + } + // An older bridge, or a reason this app does not know. + _ => tr!("agent_link.error.bridge", message = message), + }; + return self.fail(now, reason); + } + Frame::Cancel { id } => { + if phase == Phase::Ready { + self.queue.retain(|queued| queued.id != id); + } + } + Frame::Call { id, client, deadline_ms, call } => { + if phase != Phase::Ready { + // Calls before a verified `ready` are not trusted. + continue; + } + // No deadline: the bridge's own limit, as a cap. + let deadline_ms = deadline_ms.unwrap_or(now_ms() + (QUEUE_TIMEOUT * 1000.) as u64); + match call { + Err(message) => self.refuse(&id, &message, None), + Ok(_) if self.queue.len() >= QUEUE_LIMIT => self.refuse(&id, tr!("agent_link.error.queue_full"), None), + Ok((name, arguments)) => { + if !client.is_empty() { + self.status = Status::Connected { client: Some(client.clone()) }; + } + let changes = needs_grant(&name, &arguments); + let epoch = self.epoch; + self.queue.push_back(Queued { + id, + client, + name, + arguments, + changes, + queued_at: now, + notified: false, + deadline_ms, + epoch, + }); + } + } + } + Frame::Ignored => {} + } + } + if self.status == Status::Connecting { + if now - started > READY_TIMEOUT { + self.fail(now, tr!("agent_link.error.connect_timeout").into()); + } else { + ctx.request_repaint_after(Duration::from_secs_f64((READY_TIMEOUT - (now - started)).max(0.1))); + } + } + } + + /// Drops calls past their deadline and answers those that waited longer + /// than the bridge will. The only place deadlines are checked. + fn expire(&mut self, now: f64) { + // The bridge has already answered calls that are about to pass their + // deadline; running them would only change the model unseen. + let limit = now_ms() + DEADLINE_MARGIN_MS; + self.queue.retain(|queued| queued.deadline_ms > limit); + let (stale, kept): (VecDeque, VecDeque) = + std::mem::take(&mut self.queue).into_iter().partition(|queued| now - queued.queued_at > QUEUE_TIMEOUT); + self.queue = kept; + for queued in stale { + self.refuse(&queued.id, tr!("agent_link.error.busy"), None); + } + } + /// Tells the bridge once that a call is waiting. + fn notify_waiting(&mut self, message: &str) { + let mut fresh = false; + for queued in self.queue.iter_mut().filter(|q| !q.notified) { + queued.notified = true; + fresh = true; + } + if fresh { + self.send(json!({"type": "status", "message": message})); + } + } +} + +/// Changing tools named in a call, for the history label. +fn summary(name: &str, arguments: &Value) -> String { + if name != "run_batch" { + return name.to_owned(); + } + let mut tools: Vec<&str> = Vec::new(); + for call in arguments.get("calls").and_then(Value::as_array).into_iter().flatten() { + if let Some(tool) = call.get("tool").and_then(Value::as_str).filter(|tool| benchup_tools::is_mutating(tool)) + && !tools.contains(&tool) + { + tools.push(tool); + } + } + let more = tools.len() > 3; + tools.truncate(3); + let mut text = tools.join(", "); + if more { + text.push_str(", …"); + } + text +} + +impl BenchUpApp { + /// Called from `App::logic` every frame, also while the window is + /// minimized or the tab hidden: keeps the connection up and runs waiting + /// calls. + pub(super) fn poll_agent_link(&mut self, ctx: &egui::Context) { + let now = clock(); + let prefs = self.general_settings.agent.clone(); + self.agent_link.epoch = self.doc_epoch; + self.agent_link.document_name = self.doc.name.clone(); + self.agent_link.pump(ctx, now, &prefs); + self.agent_link.expire(now); + self.finish_agent_render(); + let busy = self.user_operation_active(); + let link = &mut self.agent_link; + if link.control == Control::Agent + && !link.queue.iter().any(|q| q.changes) + && link.last_change.is_none_or(|at| now - at >= AGENT_IDLE) + { + link.control = Control::Shared; + } + if link.denied && !busy && link.control != Control::Designer { + link.denied = false; + } + let mut ran = 0; + let mut waiting = None; + // On the web a render runs here, so at most one per frame. + #[cfg(target_arch = "wasm32")] + let mut rendered = false; + // Calls that must wait stay queued, in order; the calls behind them + // still run if they can (reads always can). + let mut index = 0; + while index < self.agent_link.queue.len() { + // A file operation is replacing the model: wait for it. + if self.project.blocked() { + waiting = Some(tr!("agent_link.queued_status")); + break; + } + if ran >= CALLS_PER_FRAME { + ctx.request_repaint(); + break; + } + let queued = &self.agent_link.queue[index]; + let (changes, stale, render) = + (queued.changes, queued.epoch != self.doc_epoch, queued.name == "render_view"); + let edits = changes && changes_model(&queued.name, &queued.arguments); + if stale { + if let Some(queued) = self.agent_link.queue.remove(index) { + self.agent_link.refuse( + &queued.id, + tr!("agent_link.error.document_changed"), + Some(tr!("agent_link.error.document_changed_hint")), + ); + } + continue; + } + if changes { + let ask = self.agent_must_ask(); + if ask && self.agent_link.denied { + if let Some(queued) = self.agent_link.queue.remove(index) { + self.agent_link.refuse(&queued.id, tr!("agent_link.error.denied"), Some(tr!("agent_link.error.denied_hint"))); + } + continue; + } + if ask { + waiting = Some(tr!("agent_link.waiting_for_designer")); + index += 1; + continue; + } + // A shared project's selection lease is on its way to the + // server: the edit waits for it rather than fail. + if edits && self.shared_lease_pending(ctx) { + waiting = Some(tr!("agent_link.waiting_for_server")); + index += 1; + continue; + } + if self.user_operation_active() { + // Granted control: the designer's new operation gives way. + self.cancel_user_operation(ctx); + } + } + if render { + #[cfg(not(target_arch = "wasm32"))] + if self.agent_link.rendering.is_some() { + index += 1; + continue; + } + #[cfg(target_arch = "wasm32")] + if rendered { + ctx.request_repaint(); + index += 1; + continue; + } + } + let Some(queued) = self.agent_link.queue.remove(index) else { break }; + ran += 1; + if render { + #[cfg(not(target_arch = "wasm32"))] + { + self.start_agent_render(ctx, queued.id, queued.arguments); + continue; + } + #[cfg(target_arch = "wasm32")] + { + rendered = true; + } + } + let selection_before = self.selected.clone(); + let (ok, result) = self.run_agent_request(&queued.name, &queued.arguments, ctx); + // `select` or `undo` may change the selection; in a shared + // project the server must lease it before edits can follow. + self.selection_changed_outside_frame(&selection_before); + let applied = ok && changes_model(&queued.name, &queued.arguments); + if applied { + self.agent_link.last_change = Some(now); + } + self.agent_link.document_name = self.doc.name.clone(); + self.agent_link.send_result(&queued.id, ok, result, applied); + } + // Worked out afresh every frame: the dialog shows only while a waiting + // change actually needs the designer's grant (it goes away as soon as + // they finish or cancel their operation, even with calls still queued). + let must_ask = self.agent_must_ask(); + let link = &mut self.agent_link; + link.asking = must_ask && !link.denied && link.queue.iter().any(|q| q.changes); + if let Some(message) = waiting { + self.agent_link.notify_waiting(message); + // Deadlines, leases and file operations end without input events. + ctx.request_repaint_after(Duration::from_millis(250)); + } + if self.agent_link.editing(now) { + ctx.request_repaint_after(Duration::from_secs(1)); + } + } + + /// Renders a view on another thread from a copy of the model, so a large + /// render does not freeze the window; `finish_agent_render` answers. + #[cfg(not(target_arch = "wasm32"))] + fn start_agent_render(&mut self, ctx: &egui::Context, id: String, arguments: Value) { + let model = self.doc.model.clone(); + let (tx, rx) = std::sync::mpsc::channel(); + let ctx = ctx.clone(); + std::thread::spawn(move || { + let _ = tx.send(render_answer(&model, &arguments)); + ctx.request_repaint(); + }); + self.agent_link.rendering = Some((id, rx)); + } + + /// Answers the render made on another thread, once it is done. + fn finish_agent_render(&mut self) { + #[cfg(not(target_arch = "wasm32"))] + { + use std::sync::mpsc::TryRecvError; + let Some((id, rx)) = &self.agent_link.rendering else { return }; + let answer = match rx.try_recv() { + Ok(answer) => answer, + Err(TryRecvError::Empty) => return, + Err(TryRecvError::Disconnected) => (false, json!({"ok": false, "error": "the render failed"})), + }; + let id = id.clone(); + self.agent_link.rendering = None; + self.agent_link.document_name = self.doc.name.clone(); + self.agent_link.send_result(&id, answer.0, answer.1, false); + } + } + + /// A waiting change needs the designer's grant before it runs. + fn agent_must_ask(&self) -> bool { + match self.agent_link.control { + Control::Designer => true, + Control::Shared => self.user_operation_active(), + Control::Agent => false, + } + } + + /// The designer is in the middle of something an agent edit would break. + pub(super) fn user_operation_active(&self) -> bool { + self.drawing.is_some() + || self.modeling.is_some() + || self.line_start.is_some() + || self.grain_anchor.is_some() + || self.paste_preview.is_some() + // A finished Move-copy only offers an array until the next edit; + // it is an operation once the designer starts typing the count. + || (self.last_move_copy.is_some() && !self.length_input.trim().is_empty()) + || self.solid_first.is_some() + || self.measurement.is_some() + || self.text_editor.is_some() + || self.object_creation.is_some() + || self.object_rename.is_some() + || self.gesture.as_ref().is_some_and(|gesture| { + !matches!( + gesture.kind, + GestureKind::Navigate { .. } | GestureKind::DrawingNavigate | GestureKind::Compass { .. } + ) + }) + } + + /// Ends the designer's current operation without applying it, like Escape. + pub(super) fn cancel_user_operation(&mut self, ctx: &egui::Context) { + self.stop_drawing(); + self.modeling = None; + self.measurement = None; + self.solid_first = None; + self.last_move_copy = None; + self.paste_preview = None; + self.text_editor = None; + self.object_creation = None; + self.object_rename = None; + self.flip_hover = None; + ctx.memory_mut(|m| { + if let Some(id) = m.focused() { + m.surrender_focus(id); + } + }); + ctx.request_repaint(); + } + + /// The designer granted access from the dialog. + pub(super) fn grant_agent_access(&mut self, ctx: &egui::Context) { + self.cancel_user_operation(ctx); + let link = &mut self.agent_link; + link.control = Control::Agent; + link.denied = false; + link.asking = false; + link.last_change = Some(clock()); + ctx.request_repaint(); + } + + /// The tool environment for the open document: purchasing catalog, + /// read-only and lease rules for shared projects, and the selection. + pub(super) fn tool_env(&self) -> ToolEnv { + let mut catalog: std::collections::BTreeMap<_, _> = self.doc.model.stock_materials.clone(); + catalog.extend(self.general_settings.stock.wood.iter().map(|stock| (stock.id, stock.definition()))); + ToolEnv { + // Only a view-only share is read-only. Other refusals (no + // connection, a lease on its way) are reported when the edit is + // recorded, with the actual reason. + read_only: self.shared_view_only(), + // Agents see the current selection: a designer can say "change + // this" and the agent finds what "this" is. + selection: self.selected.clone(), + hide_selection: false, + catalog, + before: None, + lease: self.shared_lease(), + created: BTreeSet::new(), + } + } + + /// Runs one request from the bridge: a tool, `run_batch`, `undo` or + /// `render_view`. + pub(super) fn run_agent_request(&mut self, name: &str, arguments: &Value, ctx: &egui::Context) -> (bool, Value) { + match name { + "undo" => self.agent_undo(arguments.get("redo").and_then(Value::as_bool).unwrap_or(false), ctx), + "render_view" => render_answer(&self.doc.model, arguments), + "run_batch" => { + let Some(list) = arguments.get("calls").and_then(Value::as_array).filter(|list| !list.is_empty()) else { + return (false, json!({"ok": false, "error": tr!("agent_link.error.calls_list")})); + }; + let mut calls = Vec::with_capacity(list.len()); + for call in list { + let Some(tool) = call.get("tool").and_then(Value::as_str) else { + return (false, json!({"ok": false, "error": tr!("agent_link.error.calls_list")})); + }; + calls.push((tool.to_owned(), call.get("arguments").cloned().unwrap_or_else(|| json!({})))); + } + let dry_run = arguments.get("dry_run").and_then(Value::as_bool).unwrap_or(false); + // `single` (`benchup call --live --dry-run`): one call keeps + // "$" text as written and answers with its own result. + let single = calls.len() == 1 && arguments.get("single").and_then(Value::as_bool).unwrap_or(false); + let label = tr!("agent_link.history_label", summary = summary(name, arguments)); + let (mut value, ok) = self.run_agent_calls(&calls, !single, dry_run, &label, ctx); + if dry_run && let Some(object) = value.as_object_mut() { + object.insert("dry_run".into(), json!(true)); + } + (ok, value) + } + _ => { + let label = tr!("agent_link.history_label", summary = summary(name, arguments)); + let (value, ok) = self.run_agent_calls(&[(name.to_owned(), arguments.clone())], false, false, &label, ctx); + (ok, value) + } + } + } + + /// Runs calls in order as one all-or-nothing step: if any fails, the model + /// is left as it was. Changes are committed as one history step, like the + /// designer's own edits; a dry run reports the results and changes nothing. + pub(super) fn run_agent_calls( + &mut self, + calls: &[(String, Value)], + batch: bool, + dry_run: bool, + label: &str, + ctx: &egui::Context, + ) -> (Value, bool) { + let mut env = self.tool_env(); + let scope = self.doc.model.edit_context; + let mut results: Vec = Vec::with_capacity(calls.len()); + let (mut created, mut modified) = (Vec::new(), Vec::new()); + for (index, (name, arguments)) in calls.iter().enumerate() { + let failure = |result: Value, earlier: Vec| batch::Applied::Failed { index, result, earlier }; + let arguments = if batch { batch::resolve(arguments, &results) } else { Ok(arguments.clone()) }; + let outcome = match arguments { + Err(error) => Err(json!({"ok": false, "error": error, + "hint": "Refer to an earlier call as \"$N.field\" (N counts calls from 1), e.g. \"$1.created[0]\"."})), + Ok(_) if matches!(name.as_str(), "run_batch" | "undo" | "render_view") => { + Err(json!({"ok": false, "error": tr!("agent_link.error.nested_session", tool = name)})) + } + Ok(arguments) => { + let outcome = benchup_tools::execute(&mut self.doc.model, &Call::new(format!("agent-{index}"), name, arguments), &mut env); + if outcome.ok { Ok(outcome) } else { Err(outcome.result) } + } + }; + let mut outcome = match outcome { + Ok(outcome) => outcome, + Err(result) => { + if let Some(before) = env.before.take() { + self.doc.model = before; + } + agent_restore_scope(&mut self.doc.model, scope); + return batch::report(calls, failure(result, results), batch); + } + }; + created.extend(outcome.created); + modified.extend(outcome.modified); + if name == "get_scene" { + outcome.result["project"] = json!({"name": self.doc.name}); + } + results.push(outcome.result); + } + let selection = calls.iter().any(|(name, _)| name == "select").then(|| env.selection.clone()); + if dry_run { + if let Some(before) = env.before.take() { + self.doc.model = before; + } + agent_restore_scope(&mut self.doc.model, scope); + return batch::report(calls, batch::Applied::Ok(results), batch); + } + let Some(mut before) = env.before.take() else { + // Nothing was edited (reads, a selection, or a no-op). + agent_restore_scope(&mut self.doc.model, scope); + if let Some(selection) = selection { + self.apply_agent_selection(selection); + } + ctx.request_repaint(); + return batch::report(calls, batch::Applied::Ok(results), batch); + }; + // The snapshot belongs to the scope the designer was in, so undo + // leaves them there whatever enter_object did meanwhile. + before.edit_context = scope; + agent_restore_scope(&mut self.doc.model, scope); + // One full comparison: the model is compared once here, not again + // while recording. + if before != self.doc.model && !self.commit_changed_edit(label, before, ctx) { + // Shared edit access lapsed: rolled back whole. + return (json!({"ok": false, "error": self.shared_edit_block_reason(ctx)}), false); + } + self.focus_touched(created.into_iter().chain(modified)); + if let Some(selection) = selection { + self.apply_agent_selection(selection); + } + ctx.request_repaint(); + batch::report(calls, batch::Applied::Ok(results), batch) + } + + /// Selects what an agent just made or changed (top-level, visible entities). + fn focus_touched(&mut self, touched: impl IntoIterator) { + let model = &self.doc.model; + let focus: BTreeSet = touched + .into_iter() + .filter(|id| model.entity_in_context(*id) == Some(*id) && !model.is_hidden(*id)) + .collect(); + if !focus.is_empty() && !(self.account.sharing_available() && self.doc.shared_project.is_some()) { + self.selected = focus; + } + } + + /// Follows the selection an agent asked for with the `select` tool. + fn apply_agent_selection(&mut self, selection: BTreeSet) { + self.selected = selection + .into_iter() + .filter(|id| self.doc.model.contains_entity(id) && !self.doc.model.is_hidden(*id)) + .collect(); + } + + fn agent_undo(&mut self, redo: bool, ctx: &egui::Context) -> (bool, Value) { + let label = if redo { self.doc.history.redo_label() } else { self.doc.history.undo_label() }.map(str::to_owned); + let Some(label) = label else { + let error = if redo { tr!("agent_link.error.nothing_to_redo") } else { tr!("agent_link.error.nothing_to_undo") }; + return (false, json!({"ok": false, "error": error})); + }; + if !self.shared_edit_allowed(ctx) { + return (false, json!({"ok": false, "error": self.shared_edit_block_reason(ctx)})); + } + let position = self.doc.history.position(); + self.undo(ctx, redo); + if self.doc.history.position() == position { + return (false, json!({"ok": false, "error": self.shared_edit_block_reason(ctx)})); + } + let key = if redo { "redone" } else { "undone" }; + let mut result = json!({"ok": true}); + result[key] = json!(label); + (true, result) + } + +} + +/// The answer to `render_view` for `model`. +fn render_answer(model: &Model, arguments: &Value) -> (bool, Value) { + let view = arguments.get("view").and_then(Value::as_str).unwrap_or("iso"); + let size = + |key: &str, default: u32| arguments.get(key).and_then(Value::as_u64).map_or(default, |v| v.min(u64::from(u32::MAX)) as u32); + match benchup_tools::render::render(model, view, size("width", 1000), size("height", 750)) { + Ok(rendered) => { + (true, json!({"ok": true, "png_base64": benchup_tools::render::base64(&rendered.png), "info": rendered.info})) + } + Err(error) => (false, json!({"ok": false, "error": error})), + } +} + +/// Puts the designer back in the scope they had before an agent acted, or at +/// the top level if that object is gone. +fn agent_restore_scope(model: &mut Model, scope: Option) { + model.edit_context = scope.filter(|id| model.objects.contains_key(id)); +} diff --git a/crates/benchup-app/src/agent_link_tests.rs b/crates/benchup-app/src/agent_link_tests.rs new file mode 100644 index 0000000..a2a0862 --- /dev/null +++ b/crates/benchup-app/src/agent_link_tests.rs @@ -0,0 +1,595 @@ +use super::*; +use std::sync::{Arc, Mutex}; + +const TOKEN: &str = "6f1b0c8e-0d1a-4a52-9b0e-agent-test"; +const BRIDGE_NONCE: &str = "abababababababababababababababababababababababababababababababab"; + +/// What crossed the fake socket. +#[derive(Default)] +struct Wire { + incoming: Vec>, + sent: Vec, + connects: usize, +} +#[derive(Clone, Default)] +struct Fake(Arc>); +impl Fake { + fn push(&self, frame: Value) { + self.0.lock().unwrap().incoming.push(Ok(frame)); + } + fn sent(&self) -> Vec { + std::mem::take(&mut self.0.lock().unwrap().sent) + } +} +struct FakeLink(Fake); +impl Link for FakeLink { + fn events(&mut self) -> Vec> { + std::mem::take(&mut self.0.0.lock().unwrap().incoming) + } + fn send(&mut self, message: Value) -> Result<(), String> { + self.0.0.lock().unwrap().sent.push(message); + Ok(()) + } +} +impl Connector for Fake { + fn connect(&mut self, _: &egui::Context, url: &str, _: &str) -> Box { + assert!(url.starts_with("ws://127.0.0.1:"), "only loopback: {url}"); + self.0.lock().unwrap().connects += 1; + Box::new(FakeLink(self.clone())) + } +} + +struct Setup { + app: BenchUpApp, + ctx: egui::Context, + wire: Fake, + _dir: tempfile::TempDir, + next: u64, +} + +impl Setup { + /// An app whose agent connection is on, with a fake bridge, before the handshake. + fn new() -> Self { + let wire = Fake::default(); + let dir = tempfile::tempdir().unwrap(); + let mut app = BenchUpApp::with_document(Document::default(), None, true); + app.agent_link = AgentLink::with_connector(Box::new(wire.clone())); + app.agent_link.token_slot = Some(dir.path().to_owned()); + app.agent_link.token = Some(TOKEN.into()); + app.agent_link.token_checked = true; + app.general_settings.agent.enabled = true; + Self { app, ctx: egui::Context::default(), wire, _dir: dir, next: 0 } + } + fn poll(&mut self) { + self.app.poll_agent_link(&self.ctx); + } + /// Connected and paired. + fn paired() -> Self { + let mut setup = Self::new(); + setup.poll(); + setup.wire.push(json!({"type": "challenge", "nonce": BRIDGE_NONCE, "protocol": 2})); + setup.poll(); + let prove = setup.wire.sent().pop().unwrap(); + assert_eq!(prove["type"], "prove"); + let app_nonce = prove["nonce"].as_str().unwrap(); + assert_eq!(prove["proof"], pairing::app_proof(TOKEN, BRIDGE_NONCE, app_nonce)); + let proof = pairing::bridge_proof(TOKEN, BRIDGE_NONCE, app_nonce); + setup.wire.push(json!({"type": "ready", "proof": proof, "protocol": 2})); + setup.poll(); + assert!(setup.app.agent_link.connected(), "{:?}", setup.app.agent_link.status); + setup + } + /// Sends a call; returns its id. + fn send(&mut self, name: &str, arguments: Value) -> String { + self.next += 1; + let id = format!("c{}", self.next); + self.wire.push(json!({"type": "call", "id": id, "name": name, "arguments": arguments, + "client": "claude-code", "deadline_ms": now_ms() + 60_000})); + id + } + /// The result frame for `id`, if one was sent. + fn answer(&self, id: &str) -> Option { + self.wire.sent().into_iter().find(|frame| frame["type"] == "result" && frame["id"] == id) + } + fn call(&mut self, name: &str, arguments: Value) -> Value { + let id = self.send(name, arguments); + self.poll(); + self.answer(&id).unwrap_or_else(|| panic!("{name} was not answered")) + } + /// Polls until `id` is answered (renders finish on another thread). + fn wait_for(&mut self, id: &str) -> Value { + let started = std::time::Instant::now(); + loop { + self.poll(); + if let Some(answer) = self.answer(id) { + return answer; + } + assert!(started.elapsed() < Duration::from_secs(10), "{id} was not answered"); + std::thread::sleep(Duration::from_millis(5)); + } + } + /// The open project is shared with this user. + fn share(&mut self) { + self.app.account.server = "https://sharing.example".into(); + self.app.account.server_reachable = true; + self.app.doc.shared_project = Some(benchup_core::SharedProject { + server: self.app.account.server.clone(), + id: "a".repeat(48), + revision: 1, + server_updated_at: 100, + file_updated_at: 100, + }); + } +} + +fn board(name: &str, x: f64) -> Value { + json!({"name": name, "origin_mm": [x, 0, 0], "size_mm": [600, 300, 18]}) +} + +fn labels(app: &BenchUpApp) -> Vec { + app.doc.history.labels().map(str::to_owned).collect() +} + +#[test] +fn calls_wait_for_a_bridge_that_proves_the_token() { + let mut setup = Setup::new(); + setup.poll(); + assert_eq!(setup.app.agent_link.status, Status::Connecting); + // A call before pairing is ignored, never run. + let early = setup.send("create_board", board("Early", 0.)); + setup.wire.push(json!({"type": "challenge", "nonce": BRIDGE_NONCE, "protocol": 2})); + setup.poll(); + assert!(setup.answer(&early).is_none()); + setup.wire.push(json!({"type": "ready", "proof": "00".repeat(32), "protocol": 2})); + setup.poll(); + assert!(matches!(setup.app.agent_link.status, Status::Error(_)), "a bridge without the token is refused"); + assert!(setup.app.doc.model.objects.is_empty()); +} + +#[test] +fn turning_it_off_disconnects_and_nothing_connects_while_off() { + let mut setup = Setup::new(); + setup.app.general_settings.agent.enabled = false; + setup.poll(); + assert_eq!(setup.wire.0.lock().unwrap().connects, 0); + assert_eq!(setup.app.agent_link.status, Status::Off); +} + +#[test] +fn each_changing_call_is_one_undo_step_and_reads_add_none() { + let mut setup = Setup::paired(); + let answer = setup.call("create_board", board("Side", 0.)); + assert_eq!(answer["ok"], true, "{answer}"); + assert_eq!(answer["result"]["document"]["name"], setup.app.doc.name); + let id = answer["result"]["created"][0].as_u64().unwrap(); + assert_eq!(labels(&setup.app), ["Agent: create_board"]); + assert_eq!(setup.app.selected, BTreeSet::from([id]), "what the agent made is selected"); + + let scene = setup.call("get_scene", json!({})); + assert_eq!(scene["ok"], true); + assert_eq!(scene["result"]["project"]["name"], setup.app.doc.name); + assert_eq!(labels(&setup.app).len(), 1, "reading adds no undo step"); + assert_eq!(setup.app.agent_link.client_name(), Some("claude-code")); + + let undone = setup.call("undo", json!({})); + assert_eq!(undone["result"]["undone"], "Agent: create_board", "{undone}"); + assert!(setup.app.doc.model.objects.is_empty()); +} + +#[test] +fn a_batch_is_one_step_all_or_nothing_and_a_dry_run_changes_nothing() { + let mut setup = Setup::paired(); + let calls = json!([ + {"tool": "create_board", "arguments": board("Left", 0.)}, + {"tool": "create_board", "arguments": board("Right", 700.)}, + {"tool": "get_object", "arguments": {"id": "$1.created[0]"}}, + ]); + let answer = setup.call("run_batch", json!({"calls": calls})); + assert_eq!(answer["ok"], true, "{answer}"); + assert_eq!(answer["result"]["results"].as_array().unwrap().len(), 3); + assert_eq!(labels(&setup.app), ["Agent: create_board"]); + assert_eq!(setup.app.doc.model.root_entities().len(), 2); + + let failing = json!([ + {"tool": "create_board", "arguments": board("Top", 0.)}, + {"tool": "move", "arguments": {"ids": [999_999], "delta_mm": [1, 0, 0]}}, + ]); + let answer = setup.call("run_batch", json!({"calls": failing})); + assert_eq!(answer["ok"], false); + assert_eq!(answer["result"]["failed_call"], 2); + assert_eq!(setup.app.doc.model.root_entities().len(), 2, "nothing of a failed batch stays"); + + let dry = json!([{"tool": "create_board", "arguments": board("Trial", 0.)}]); + let answer = setup.call("run_batch", json!({"calls": dry, "dry_run": true})); + assert_eq!(answer["ok"], true); + assert_eq!(answer["result"]["dry_run"], true); + assert_eq!(setup.app.doc.model.root_entities().len(), 2); + assert_eq!(labels(&setup.app).len(), 1); +} + +#[test] +fn the_designer_mid_operation_grants_access_and_their_operation_is_cancelled() { + let mut setup = Setup::paired(); + setup.app.line_start = Some(DVec3::ZERO); + let id = setup.send("create_board", board("Side", 0.)); + let scene = setup.send("get_scene", json!({})); + setup.poll(); + assert!(setup.app.agent_link.asking, "the designer is asked first"); + let sent = setup.wire.sent(); + assert!(!sent.iter().any(|f| f["id"] == id)); + assert!(sent.iter().any(|f| f["id"] == scene), "the read queued behind it does not wait"); + assert!(setup.app.doc.model.objects.is_empty()); + + setup.app.grant_agent_access(&setup.ctx); + assert!(setup.app.line_start.is_none(), "granting cancels the designer's line"); + setup.poll(); + let answer = setup.answer(&id).unwrap(); + assert_eq!(answer["ok"], true); + assert_eq!(setup.app.agent_link.control, Control::Agent); + assert!(!setup.app.agent_link.asking); + + // With access granted, a new operation of the designer gives way. + setup.app.line_start = Some(DVec3::ZERO); + let answer = setup.call("create_board", board("Other", 700.)); + assert_eq!(answer["ok"], true); + assert!(setup.app.line_start.is_none()); +} + +#[test] +fn reads_run_while_the_designer_works_and_a_denial_refuses_changes() { + let mut setup = Setup::paired(); + setup.app.line_start = Some(DVec3::ZERO); + let scene = setup.call("get_scene", json!({})); + assert_eq!(scene["ok"], true, "reading never needs a grant"); + assert!(!setup.app.agent_link.asking); + + let id = setup.send("create_board", board("Side", 0.)); + setup.poll(); + assert!(setup.app.agent_link.asking); + setup.app.agent_link.deny(); + let answer = setup.answer(&id).unwrap(); + assert_eq!(answer["ok"], false); + assert_eq!(answer["result"]["error"], "The designer denied access to the project"); + assert!(answer["result"]["hint"].is_string()); + assert!(setup.app.line_start.is_some(), "the designer keeps working"); + + // While they keep working, further changes are refused without asking. + let again = setup.call("create_board", board("Again", 0.)); + assert_eq!(again["ok"], false); + assert!(!setup.app.agent_link.asking); + + // Once they finish, the agent may edit again. + setup.app.line_start = None; + let answer = setup.call("create_board", board("Later", 0.)); + assert_eq!(answer["ok"], true, "{answer}"); +} + +#[test] +fn taking_control_makes_changes_wait_until_access_is_granted_or_released() { + let mut setup = Setup::paired(); + setup.call("create_board", board("Side", 0.)); + let now = clock(); + assert!(setup.app.agent_link.editing(now), "the status bar offers Take control"); + + setup.app.agent_link.take_control(); + assert_eq!(setup.app.agent_link.control, Control::Designer); + let id = setup.send("create_board", board("Next", 700.)); + setup.poll(); + assert!(setup.app.agent_link.asking, "changes wait for a grant while the designer has control"); + assert!(setup.answer(&id).is_none()); + + setup.app.agent_link.release_control(); + setup.poll(); + assert_eq!(setup.answer(&id).unwrap()["ok"], true); + assert_eq!(setup.app.agent_link.control, Control::Shared); +} + +#[test] +fn shared_projects_without_edit_access_stay_read_only() { + let mut setup = Setup::paired(); + setup.share(); + let answer = setup.call("create_board", board("Side", 0.)); + assert_eq!(answer["ok"], false, "{answer}"); + assert!(setup.app.doc.model.objects.is_empty()); + assert!(setup.app.doc.history.labels().next().is_none()); + assert_eq!(setup.call("get_scene", json!({}))["ok"], true, "reading is still allowed"); +} + +#[test] +fn a_call_queued_for_a_project_that_was_replaced_is_refused() { + let mut setup = Setup::paired(); + setup.app.project.external_change = true; // a file operation blocks calls + let id = setup.send("create_board", board("Side", 0.)); + setup.poll(); + assert!(setup.answer(&id).is_none(), "it waits while the project is blocked"); + setup.app.doc_epoch += 1; + setup.app.project.external_change = false; + setup.poll(); + let answer = setup.answer(&id).unwrap(); + assert_eq!(answer["result"]["error"], "The open project changed before this call ran"); + assert!(setup.app.doc.model.objects.is_empty()); +} + +#[test] +fn malformed_calls_are_answered_with_an_error() { + let mut setup = Setup::paired(); + setup.wire.push(json!({"type": "call", "id": "x", "name": "move", "arguments": [1, 2]})); + setup.poll(); + let answer = setup.answer("x").unwrap(); + assert_eq!(answer["ok"], false); + assert_eq!(answer["result"]["error"], "The arguments must be a JSON object"); + let nested = setup.call("run_batch", json!({"calls": [{"tool": "undo"}]})); + assert_eq!(nested["ok"], false); +} + +#[test] +fn rendered_views_come_back_as_png() { + let mut setup = Setup::paired(); + setup.call("create_board", board("Side", 0.)); + let first = setup.send("render_view", json!({"view": "front", "width": 200, "height": 150})); + let second = setup.send("render_view", json!({"view": "top", "width": 200, "height": 150})); + let scene = setup.send("get_scene", json!({})); + setup.poll(); + assert!(setup.answer(&scene).is_some(), "reads do not wait for a render"); + for id in [first, second] { + let answer = setup.wait_for(&id); + assert_eq!(answer["ok"], true, "{answer}"); + assert!(answer["result"]["png_base64"].as_str().unwrap().starts_with("iVBOR")); + } +} + +#[test] +fn reads_behind_a_change_waiting_for_a_grant_still_run() { + let mut setup = Setup::paired(); + setup.app.line_start = Some(DVec3::ZERO); + let change = setup.send("create_board", board("Side", 0.)); + let later = setup.send("create_board", board("Other", 700.)); + let scene = setup.send("get_scene", json!({})); + setup.poll(); + let sent = setup.wire.sent(); + assert!(sent.iter().any(|f| f["id"] == scene && f["ok"] == true), "the read is not stuck behind the change"); + assert!(!sent.iter().any(|f| f["id"] == change || f["id"] == later)); + assert!(setup.app.agent_link.asking); + + setup.app.line_start = None; + setup.poll(); + let sent = setup.wire.sent(); + assert!(sent.iter().any(|f| f["id"] == change && f["ok"] == true)); + assert!(sent.iter().any(|f| f["id"] == later && f["ok"] == true)); + assert_eq!(setup.app.doc.model.root_entities().len(), 2); +} + +#[test] +fn an_edit_waits_for_a_shared_lease_on_its_way_instead_of_failing_as_read_only() { + let mut setup = Setup::paired(); + let made = setup.call("create_board", board("Side", 0.)); + let board_id = made["result"]["created"][0].as_u64().unwrap(); + setup.share(); + let lease = BTreeSet::from([board_id]); + setup.app.selected = BTreeSet::new(); + setup.app.simulate_shared_access("edit", lease.clone(), BTreeSet::new()); + let id = setup.send("move", json!({"ids": [board_id], "delta_mm": [10, 0, 0]})); + setup.poll(); + assert!(setup.answer(&id).is_none(), "the edit waits for the server to confirm the lease"); + + setup.app.selected = lease.clone(); + setup.app.simulate_shared_access("edit", lease.clone(), lease); + let answer = setup.wait_for(&id); + assert_eq!(answer["ok"], true, "{answer}"); + + // A view-only share is read-only at once. + setup.app.simulate_shared_access("view", BTreeSet::new(), BTreeSet::new()); + let answer = setup.call("create_board", board("Other", 700.)); + assert_eq!(answer["ok"], false); + assert!(answer["result"]["error"].as_str().unwrap().contains("read-only"), "{answer}"); +} + +#[test] +fn an_agent_selection_in_a_shared_project_asks_the_server_for_its_lease() { + let mut setup = Setup::paired(); + let a = setup.call("create_board", board("Left", 0.))["result"]["created"][0].as_u64().unwrap(); + let b = setup.call("create_board", board("Right", 700.))["result"]["created"][0].as_u64().unwrap(); + setup.share(); + let lease = BTreeSet::from([a, b]); + setup.app.selected = lease.clone(); + setup.app.simulate_shared_access("edit", lease.clone(), lease); + // Agent calls run in `App::logic`, before the frame snapshots the + // selection, so the change must be reported to sharing explicitly; + // otherwise later edits wait for a lease that is never requested. + let answer = setup.call("select", json!({"ids": [a]})); + assert_eq!(answer["ok"], true, "{answer}"); + assert_eq!(setup.app.selected, BTreeSet::from([a])); + let (desired, dirty) = setup.app.requested_shared_selection(); + assert_eq!(desired, &BTreeSet::from([a])); + assert!(dirty, "the lease request is sent to the server"); +} + +#[test] +fn a_result_too_large_for_the_bridge_becomes_an_error() { + let mut setup = Setup::paired(); + setup.app.agent_link.max_result_bytes = 256; + let answer = setup.call("create_board", board("Side", 0.)); + assert_eq!(answer["ok"], false); + assert_eq!(answer["result"]["applied"], true, "the agent learns the change stayed: {answer}"); + assert!(answer["result"]["hint"].is_string()); + assert_eq!(setup.app.agent_link.queue.len(), 0); + assert!(setup.app.agent_link.connected(), "the connection stays up"); +} + +#[test] +fn bridge_refusals_are_shown_in_the_designers_language() { + let mut setup = Setup::new(); + setup.poll(); + setup.wire.push(json!({"type": "error", "code": pairing::refusal::APP_BUSY, "message": "another window"})); + setup.poll(); + assert_eq!(setup.app.agent_link.status, Status::Error(tr!("agent_link.error.app_busy").to_owned())); +} + +#[test] +fn changing_calls_are_recognized() { + assert!(changes_model("create_board", &json!({}))); + assert!(!changes_model("get_scene", &json!({}))); + assert!(changes_model("undo", &json!({}))); + assert!(!changes_model("render_view", &json!({}))); + let batch = json!({"calls": [{"tool": "get_scene"}, {"tool": "move"}]}); + assert!(changes_model("run_batch", &batch)); + assert!(!changes_model("run_batch", &json!({"calls": [{"tool": "get_scene"}]}))); + let mut dry = batch.clone(); + dry["dry_run"] = json!(true); + assert!(!changes_model("run_batch", &dry)); + + // Selecting changes no model but still needs a grant. + assert!(!changes_model("select", &json!({"ids": [1]}))); + assert!(needs_grant("select", &json!({"ids": [1]}))); + let selecting = json!({"calls": [{"tool": "get_scene"}, {"tool": "select", "arguments": {"ids": [1]}}]}); + assert!(needs_grant("run_batch", &selecting)); + let mut dry = selecting.clone(); + dry["dry_run"] = json!(true); + assert!(!needs_grant("run_batch", &dry), "a dry run leaves the selection alone"); + assert!(!needs_grant("get_selection", &json!({}))); +} + +#[test] +fn a_selection_waits_while_the_designer_is_mid_operation() { + let mut setup = Setup::paired(); + let made = setup.call("create_board", board("Side", 0.)); + let id = made["result"]["created"][0].as_u64().unwrap(); + setup.app.selected.clear(); + setup.app.line_start = Some(DVec3::ZERO); + let select = setup.send("select", json!({"ids": [id]})); + let batch = setup.send("run_batch", json!({"calls": [{"tool": "select", "arguments": {"ids": [id]}}]})); + setup.poll(); + assert!(setup.app.agent_link.asking, "the designer is asked first"); + assert!(setup.answer(&select).is_none()); + assert!(setup.app.selected.is_empty(), "the selection under their operation is untouched"); + + setup.app.line_start = None; + setup.poll(); + let sent = setup.wire.sent(); + assert!(sent.iter().any(|f| f["id"] == select && f["ok"] == true)); + assert!(sent.iter().any(|f| f["id"] == batch && f["ok"] == true)); + assert_eq!(setup.app.selected, BTreeSet::from([id])); +} + +#[test] +fn a_finished_move_copy_does_not_keep_the_designer_busy() { + let mut setup = Setup::paired(); + let model = setup.app.doc.model.clone(); + setup.app.last_move_copy = Some(MoveCopy { base: model.clone(), source: model, delta: DVec3::X }); + assert!(!setup.app.user_operation_active(), "an idle designer after a Move-copy is not busy"); + let answer = setup.call("create_board", board("Side", 0.)); + assert_eq!(answer["ok"], true, "{answer}"); + assert!(setup.app.last_move_copy.is_none(), "the agent's edit ends the array offer, like any edit"); + + // Typing an array count is an operation. + let model = setup.app.doc.model.clone(); + setup.app.last_move_copy = Some(MoveCopy { base: model.clone(), source: model, delta: DVec3::X }); + setup.app.length_input = "x3".into(); + assert!(setup.app.user_operation_active()); +} + +#[test] +fn the_dialog_closes_when_queued_changes_no_longer_need_a_grant() { + let mut setup = Setup::paired(); + setup.app.line_start = Some(DVec3::ZERO); + let ids: Vec = + (0..CALLS_PER_FRAME + 4).map(|i| setup.send("create_board", board(&format!("B{i}"), i as f64 * 700.))).collect(); + setup.poll(); + assert!(setup.app.agent_link.asking); + + // The designer presses Escape: the changes run without a grant, over + // several frames, and the dialog goes away at once. + setup.app.line_start = None; + setup.poll(); + assert!(setup.app.agent_link.waiting_changes() > 0, "some calls wait for the next frame"); + assert!(!setup.app.agent_link.asking, "no grant is needed any more"); + setup.poll(); + let sent = setup.wire.sent(); + for id in &ids { + assert!(sent.iter().any(|f| f["id"] == *id && f["ok"] == true), "{id} ran"); + } +} + +#[test] +fn setup_snippets_keep_the_token_out_of_program_arguments() { + assert_eq!(claude_command("T", DEFAULT_PORT), "claude mcp add benchup -e BENCHUP_AGENT_TOKEN=T -- benchup bridge"); + assert!(claude_command("T", 5000).ends_with("benchup bridge --port 5000")); + let config: Value = serde_json::from_str(&mcp_json("T", DEFAULT_PORT)).unwrap(); + assert_eq!(config["mcpServers"]["benchup"]["args"], json!(["bridge"])); + assert_eq!(config["mcpServers"]["benchup"]["env"]["BENCHUP_AGENT_TOKEN"], "T"); + assert!(shell_setup("T", 5000).contains("BENCHUP_BRIDGE_PORT=5000")); + assert_eq!(ui::masked("abcd-1234").chars().rev().take(4).collect::(), "4321"); +} + +#[test] +fn preferences_default_off_and_older_settings_load() { + let settings: settings::GeneralSettings = serde_json::from_str(r#"{"show_fps": true}"#).unwrap(); + assert_eq!(settings.agent, Preferences { enabled: false, port: DEFAULT_PORT }); + let mut saved = settings::GeneralSettings::default(); + saved.agent = Preferences { enabled: true, port: 5000 }; + let restored: settings::GeneralSettings = serde_json::from_str(&serde_json::to_string(&saved).unwrap()).unwrap(); + assert_eq!(restored.agent, saved.agent); + let mut low = Preferences { enabled: true, port: 80 }; + low.normalize(); + assert_eq!(low.port, DEFAULT_PORT); +} + +#[test] +fn the_token_is_made_once_and_kept_on_this_device() { + let dir = tempfile::tempdir().unwrap(); + let mut link = AgentLink::with_connector(Box::new(Fake::default())); + link.token_slot = Some(dir.path().to_owned()); + let token = link.ensure_token(0.).unwrap(); + assert_eq!(token.len(), 36); + let mut again = AgentLink::with_connector(Box::new(Fake::default())); + again.token_slot = Some(dir.path().to_owned()); + again.load_token(); + assert_eq!(again.token.as_deref(), Some(token.as_str())); + again.regenerate(); + assert_ne!(again.token.as_deref(), Some(token.as_str())); + + let mut broken = AgentLink::with_connector(Box::new(Fake::default())); + broken.token_slot = Some(dir.path().join("empty")); + broken.generator = || Err("no randomness".into()); + assert!(broken.ensure_token(0.).is_none(), "no weak token is ever made"); +} + +#[test] +fn a_token_that_cannot_be_made_is_not_retried_every_frame() { + static TRIES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + let dir = tempfile::tempdir().unwrap(); + let mut link = AgentLink::with_connector(Box::new(Fake::default())); + link.token_slot = Some(dir.path().to_owned()); + link.generator = || { + TRIES.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + Err("no randomness".into()) + }; + for frame in 0..100 { + assert!(link.ensure_token(f64::from(frame) / 60.).is_none()); + } + assert_eq!(TRIES.load(std::sync::atomic::Ordering::SeqCst), 1); + link.ensure_token(TOKEN_RETRY + 1.); + assert_eq!(TRIES.load(std::sync::atomic::Ordering::SeqCst), 2, "it tries again later"); +} + +#[test] +fn a_token_that_cannot_be_saved_is_not_used() { + let dir = tempfile::tempdir().unwrap(); + let mut link = AgentLink::with_connector(Box::new(Fake::default())); + link.token_slot = Some(dir.path().to_owned()); + let saved = link.ensure_token(0.).unwrap(); + + // Storage becomes unwritable (a file where the directory should be). + let blocked = dir.path().join("blocked"); + std::fs::write(&blocked, "").unwrap(); + link.token_slot = Some(blocked.join("slot")); + link.regenerate(); + assert!(link.token_error.is_some()); + assert_eq!(link.token.as_deref(), Some(saved.as_str()), "the saved token stays in use"); + + // With nothing saved yet, no unsaved token is handed out. + let mut fresh = AgentLink::with_connector(Box::new(Fake::default())); + fresh.token_slot = Some(blocked.join("slot")); + assert!(fresh.ensure_token(0.).is_none()); + assert!(fresh.token_error.is_some()); +} diff --git a/crates/benchup-app/src/agent_link_ui.rs b/crates/benchup-app/src/agent_link_ui.rs new file mode 100644 index 0000000..8d7886e --- /dev/null +++ b/crates/benchup-app/src/agent_link_ui.rs @@ -0,0 +1,183 @@ +//! Settings → Agent connection, the status-bar control indicator and the +//! dialog that asks the designer to grant or deny access. +use super::*; +use crate::inspector_table::inspector_field; + +/// The token with all but its last characters hidden. +pub(crate) fn masked(token: &str) -> String { + let tail: String = token.chars().rev().take(4).collect::>().into_iter().rev().collect(); + format!("••••••••-••••-••••-••••-••••••••{tail}") +} + +/// Which copy button was pressed last, to confirm it next to that row. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) enum CopiedText { + Token, + Setup, +} + +/// How long "Copied" stays next to the button. +const COPIED_SECONDS: f64 = 2.; + +impl AgentLink { + /// Puts `text` on the system clipboard and confirms it briefly. + pub(crate) fn copy(&mut self, ctx: &egui::Context, text: String, what: CopiedText) { + ctx.copy_text(text); + self.copied = Some((what, ctx.input(|i| i.time) + COPIED_SECONDS)); + ctx.request_repaint_after(Duration::from_secs_f64(COPIED_SECONDS)); + } + + fn copied_label(&self, ui: &mut egui::Ui, what: CopiedText) { + let now = ui.ctx().input(|i| i.time); + if self.copied.is_some_and(|(copied, until)| copied == what && now < until) { + ui.text_label(RichText::new(tr!("agent_link.copied")).color(Palette::for_ui(ui).muted)); + } + } +} + +impl BenchUpApp { + pub(crate) fn agent_link_settings(&mut self, ui: &mut egui::Ui) { + let colors = Palette::for_ui(ui); + ui.text_weak(tr!("agent_link.help")); + let prefs = &mut self.general_settings.agent; + ui.checkbox(&mut prefs.enabled, tr!("agent_link.allow")).on_hover_text(tr!("agent_link.allow_help")); + inspector_field(ui, "settings/agent_connection/port", tr!("agent_link.port"), |ui| { + ui.add(egui::DragValue::new(&mut prefs.port).range(1024..=65535).speed(1)) + .on_hover_text(tr!("agent_link.port_help")) + }); + prefs.normalize(); + let port = prefs.port; + let status = match &self.agent_link.status { + Status::Off => tr!("agent_link.status.off").to_owned(), + Status::Connecting => tr!("agent_link.status.connecting").to_owned(), + Status::Connected { client: None } => tr!("agent_link.status.waiting").to_owned(), + Status::Connected { client: Some(client) } => tr!("agent_link.status.connected", client = client), + Status::Error(message) => tr!("agent_link.status.error", message = message), + }; + let status_color = if self.agent_link.connected() { colors.ink } else { colors.muted }; + ui.text_label(RichText::new(status).color(status_color)); + + ui.separator(); + ui.text_strong(tr!("agent_link.token")); + self.agent_link.load_token(); + match self.agent_link.token.clone() { + None => { + ui.text_weak(tr!("agent_link.token_none")); + } + Some(token) => { + let shown = if self.agent_link.show_token { token } else { masked(&token) }; + ui.text_label(RichText::new(shown).monospace()); + } + } + if let Some(error) = &self.agent_link.token_error { + ui.text_label(RichText::new(tr!("agent_link.token_save_failed", error = error)).color(colors.accent)); + } + ui.text_weak(tr!("agent_link.token_help")); + let has_token = self.agent_link.token.is_some(); + ui.horizontal_wrapped(|ui| { + let label = if self.agent_link.show_token { tr!("agent_link.hide") } else { tr!("agent_link.show") }; + if ui.add_enabled(has_token, egui::Button::new(label)).clicked() { + self.agent_link.show_token = !self.agent_link.show_token; + } + if ui.add_enabled(has_token, egui::Button::new(tr!("agent_link.copy_token"))).clicked() + && let Some(token) = self.agent_link.token.clone() + { + self.agent_link.copy(ui.ctx(), token, CopiedText::Token); + } + let label = if has_token { tr!("agent_link.regenerate") } else { tr!("agent_link.generate") }; + if ui.button(label).on_hover_text(tr!("agent_link.regenerate_help")).clicked() { + self.agent_link.regenerate(); + } + self.agent_link.copied_label(ui, CopiedText::Token); + }); + + ui.separator(); + ui.text_strong(tr!("agent_link.setup")); + ui.text_weak(tr!("agent_link.setup_help")); + if let Some(token) = self.agent_link.token.clone() { + ui.horizontal_wrapped(|ui| { + if ui.button(tr!("agent_link.copy_command")).on_hover_text(tr!("agent_link.command_help")).clicked() { + self.agent_link.copy(ui.ctx(), claude_command(&token, port), CopiedText::Setup); + } + if ui.button(tr!("agent_link.copy_json")).on_hover_text(tr!("agent_link.json_help")).clicked() { + self.agent_link.copy(ui.ctx(), mcp_json(&token, port), CopiedText::Setup); + } + if ui.button(tr!("agent_link.copy_shell")).on_hover_text(tr!("agent_link.shell_help")).clicked() { + self.agent_link.copy(ui.ctx(), shell_setup(&token, port), CopiedText::Setup); + } + self.agent_link.copied_label(ui, CopiedText::Setup); + }); + } + } + + /// The status-bar indicator: who is in control, and the button to take + /// or give it back. Nothing while no agent is connected. + pub(crate) fn agent_status_chip(&mut self, ui: &mut egui::Ui) { + if !self.agent_link.connected() { + return; + } + let colors = Palette::for_ui(ui); + let now = super::clock(); + let client = self.agent_link.client_name().map(str::to_owned); + // Laid out right to left: the button, then the text, then the dot. + let (text, color) = if self.agent_link.control == Control::Designer { + if ui.small_button(tr!("agent_link.release_control")).on_hover_text(tr!("agent_link.release_control_help")).clicked() { + self.agent_link.release_control(); + } + (tr!("agent_link.chip.designer").to_owned(), colors.muted) + } else if self.agent_link.editing(now) { + if ui.small_button(tr!("agent_link.take_control")).on_hover_text(tr!("agent_link.take_control_help")).clicked() { + self.agent_link.take_control(); + } + (tr!("agent_link.chip.editing").to_owned(), colors.accent) + } else { + (tr!("agent_link.chip.connected").to_owned(), colors.muted) + }; + let label = ui.text_label(RichText::new(text).size(12.).color(color)); + if let Some(client) = client { + label.on_hover_text(client); + } + dot(ui, color); + ui.add_space(8.); + } + + /// Asks the designer to grant or deny access while the agent waits. + pub(crate) fn agent_access_window(&mut self, ctx: &egui::Context) { + if !self.agent_link.asking { + return; + } + let client = self.agent_link.waiting_client().filter(|c| !c.is_empty()).unwrap_or(tr!("agent_link.an_agent")).to_owned(); + let waiting = self.agent_link.waiting_changes(); + let designer = self.agent_link.control == Control::Designer; + let (mut grant, mut deny) = (false, false); + Modal::new("agent_access", 440.).dismissible(false).show(ctx, |modal| { + modal.header(|ui| { + ui.text_heading(tr!("agent_link.permission.title")); + }); + modal.body(|ui| { + ui.text_label(tr!("agent_link.permission.body", client = client)); + ui.text_label(if designer { + tr!("agent_link.permission.designer") + } else { + tr!("agent_link.permission.busy") + }); + ui.text_weak(tr!("agent_link.permission.waiting", count = waiting)); + }); + modal.footer(|ui| { + // Least impactful first: it sits at the right edge. + if ui.button(tr!("agent_link.deny")).clicked() { + deny = true; + } + if primary_button(ui, tr!("agent_link.grant"), true).clicked() { + grant = true; + } + }); + }); + if grant { + self.grant_agent_access(ctx); + } else if deny { + self.agent_link.deny(); + ctx.request_repaint(); + } + } +} diff --git a/crates/benchup-app/src/app.rs b/crates/benchup-app/src/app.rs index 24d1c54..8f4b7ba 100644 --- a/crates/benchup-app/src/app.rs +++ b/crates/benchup-app/src/app.rs @@ -99,6 +99,10 @@ pub(crate) mod viewport_benchmark; #[path = "web_viewport_benchmark.rs"] mod web_viewport_benchmark; +#[path = "agent_link.rs"] +mod agent_link; +#[path = "credentials.rs"] +mod credentials; #[path = "fasteners.rs"] mod fasteners; #[path = "sharing.rs"] @@ -362,9 +366,14 @@ pub struct BenchUpApp { fasteners: fasteners::Preferences, fastener_dialog: fasteners::Dialog, sharing: sharing::Sharing, + agent_link: agent_link::AgentLink, #[cfg(not(target_arch = "wasm32"))] updates: crate::updates::Updater, doc: Document, + /// Bumped whenever another project is opened (new, open, recovery, a + /// different shared project), so waiting agent calls can tell. A + /// collaborator's revision of the open project does not bump it. + doc_epoch: u64, project: projects::Project, screenshot: screenshot_dialog::ScreenshotDialog, screenshot_gpu: Option<(eframe::wgpu::Device, eframe::wgpu::Queue)>, @@ -558,6 +567,8 @@ impl BenchUpApp { fasteners: Default::default(), fastener_dialog: Default::default(), sharing: Default::default(), + agent_link: Default::default(), + doc_epoch: 0, doc, project: projects::Project { modified, @@ -828,23 +839,29 @@ impl BenchUpApp { } } fn finish_edit(&mut self, label: &str, before: Model, ctx: &egui::Context) { - let edited = before != self.doc.model; - if edited && !self.shared_edit_allowed(ctx) { + if before != self.doc.model { + self.commit_changed_edit(label, before, ctx); + } + } + /// Records an edit the caller already knows changed the model, without + /// comparing the models again. Returns false if it was refused (shared + /// project without edit access) and rolled back. + fn commit_changed_edit(&mut self, label: &str, before: Model, ctx: &egui::Context) -> bool { + if !self.shared_edit_allowed(ctx) { self.doc.model = before; self.say(ctx, self.shared_edit_block_reason(ctx)); - return; - } - if edited { - let author = self.current_history_author(); - self.doc.history.record_applied_attributed( - label, - before, - &self.doc.model, - Some(author), - Some(sharing::now_timestamp()), - ); - self.finish_recorded_edit(ctx, true); + return false; } + let author = self.current_history_author(); + self.doc.history.record_applied_attributed( + label, + before, + &self.doc.model, + Some(author), + Some(sharing::now_timestamp()), + ); + self.finish_recorded_edit(ctx, true); + true } fn finish_recorded_edit(&mut self, ctx: &egui::Context, geometry_changed: bool) { self.solid_first = None; @@ -3441,6 +3458,7 @@ impl BenchUpApp { .color(colors.muted), ); }); + self.agent_status_chip(ui); ui.allocate_ui_with_layout( ui.available_size(), egui::Layout::left_to_right(egui::Align::Center), @@ -7142,6 +7160,14 @@ impl eframe::App for BenchUpApp { self.space_mouse.filter_input(raw_input); } + /// Runs before `ui` every frame, and also while the window is minimized + /// or the browser tab hidden, when `ui` does not run. + fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + benchup_core::i18n::set_language(&self.general_settings.language); + // Agent calls must be answered even when BenchUp is not on screen. + self.poll_agent_link(ctx); + } + fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { let ctx = ui.ctx().clone(); benchup_core::i18n::set_language(&self.general_settings.language); @@ -7216,6 +7242,7 @@ impl eframe::App for BenchUpApp { } self.account.show(&ctx); self.share_dialog(&ctx); + self.agent_access_window(&ctx); self.material_removal_window(&ctx); if self.about { self.about_window(&ctx); diff --git a/crates/benchup-app/src/credentials.rs b/crates/benchup-app/src/credentials.rs new file mode 100644 index 0000000..89c1799 --- /dev/null +++ b/crates/benchup-app/src/credentials.rs @@ -0,0 +1,104 @@ +//! Secrets that stay on this device, such as the agent pairing token: a +//! private (0600) file in the data directory natively, `localStorage` on the +//! web. They never enter eframe storage, preferences, or project files. +//! Callers name the slot with a file name and a storage key. +#[cfg(target_arch = "wasm32")] +use benchup_core::tr; + +#[cfg(any(not(target_arch = "wasm32"), test))] +pub(crate) fn save_in(dir: &std::path::Path, file_name: &str, value: &str) -> Result<(), String> { + use std::io::Write; + std::fs::create_dir_all(dir).map_err(|e| e.to_string())?; + // Write a private temp file next to the target, then rename it over the + // target: the secret is never readable by others and an existing file is + // never truncated in place. + let temp = dir.join(format!(".{file_name}.{}.tmp", uuid::Uuid::new_v4().simple())); + let mut options = std::fs::OpenOptions::new(); + options.create_new(true).write(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + let written = (|| -> std::io::Result<()> { + let mut file = options.open(&temp)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + file.set_permissions(std::fs::Permissions::from_mode(0o600))?; + } + file.write_all(value.trim().as_bytes())?; + file.sync_all()?; + drop(file); + std::fs::rename(&temp, dir.join(file_name)) + })(); + if let Err(error) = written { + let _ = std::fs::remove_file(&temp); + return Err(error.to_string()); + } + Ok(()) +} +#[cfg(any(not(target_arch = "wasm32"), test))] +pub(crate) fn load_in(dir: &std::path::Path, file_name: &str) -> Option { + let value = std::fs::read_to_string(dir.join(file_name)).ok()?; + let value = value.trim(); + (!value.is_empty()).then(|| value.to_owned()) +} + +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn load(file_name: &str, _storage_key: &str) -> Option { + load_in(&crate::storage::data_dir().ok()?, file_name) +} +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn save(file_name: &str, _storage_key: &str, value: &str) -> Result<(), String> { + save_in(&crate::storage::data_dir()?, file_name, value) +} + +#[cfg(target_arch = "wasm32")] +fn local_storage() -> Result { + web_sys::window() + .ok_or(tr!("agent_link.error.no_browser"))? + .local_storage() + .map_err(|_| tr!("agent_link.error.storage_unavailable"))? + .ok_or(tr!("agent_link.error.storage_unavailable")) + .map_err(Into::into) +} +#[cfg(target_arch = "wasm32")] +pub(crate) fn load(_file_name: &str, storage_key: &str) -> Option { + let value = local_storage().ok()?.get_item(storage_key).ok()??; + let value = value.trim(); + (!value.is_empty()).then(|| value.to_owned()) +} +#[cfg(target_arch = "wasm32")] +pub(crate) fn save(_file_name: &str, storage_key: &str, value: &str) -> Result<(), String> { + local_storage()? + .set_item(storage_key, value.trim()) + .map_err(|_| tr!("agent_link.error.cannot_save_token").to_owned()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[cfg(unix)] + #[test] + fn saving_over_a_world_readable_file_leaves_a_private_intact_file() { + use std::os::unix::fs::PermissionsExt; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("secret"); + std::fs::write(&path, "old").unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); + save_in(dir.path(), "secret", " new-value \n").unwrap(); + assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600); + assert_eq!(load_in(dir.path(), "secret").as_deref(), Some("new-value")); + let leftovers: Vec<_> = std::fs::read_dir(dir.path()).unwrap().collect(); + assert_eq!(leftovers.len(), 1, "no temp file is left behind"); + } + + #[test] + fn a_new_file_round_trips() { + let dir = tempfile::tempdir().unwrap(); + save_in(&dir.path().join("nested"), "k", "v").unwrap(); + assert_eq!(load_in(&dir.path().join("nested"), "k").as_deref(), Some("v")); + } +} diff --git a/crates/benchup-app/src/projects.rs b/crates/benchup-app/src/projects.rs index 3a93fd9..1206bf3 100644 --- a/crates/benchup-app/src/projects.rs +++ b/crates/benchup-app/src/projects.rs @@ -237,6 +237,7 @@ impl BenchUpApp { egui::DragAndDrop::clear_payload(ctx); storage::invalidate_selection_source(); self.doc = doc; + self.doc_epoch += 1; self.viewports .reframe_project(&self.doc.camera, &self.doc.viewport_cameras); self.selected = Self::restore_document_selection(&mut self.doc); diff --git a/crates/benchup-app/src/realtime.rs b/crates/benchup-app/src/realtime.rs index 6b929c7..2dd368f 100644 --- a/crates/benchup-app/src/realtime.rs +++ b/crates/benchup-app/src/realtime.rs @@ -47,7 +47,21 @@ pub(crate) fn valid_endpoint(server: &str, endpoint: &str) -> bool { } impl Socket { pub fn connect(ctx: &egui::Context, url: String, ticket: String, connection: String) -> Self { + Self::connect_limited(ctx, url, ticket, connection, None) + } + /// Like `connect`, with a cap on the size of one incoming message + /// (`None` keeps the library default). The browser build already caps + /// messages at about 17 MB, so the cap only changes the native build. + pub fn connect_limited( + ctx: &egui::Context, + url: String, + ticket: String, + connection: String, + max_message: Option, + ) -> Self { let (sender, events) = mpsc::channel(); + #[cfg(target_arch = "wasm32")] + let _ = max_message; #[cfg(not(target_arch = "wasm32"))] { let (outgoing, commands) = mpsc::channel::(); @@ -86,8 +100,20 @@ impl Socket { stream .set_write_timeout(Some(Duration::from_secs(10))) .map_err(|e| e.to_string())?; - let (mut socket, _) = - tungstenite::client_tls(request, stream).map_err(|e| e.to_string())?; + let (mut socket, _) = match max_message { + None => tungstenite::client_tls(request, stream), + Some(limit) => tungstenite::client_tls_with_config( + request, + stream, + Some( + tungstenite::protocol::WebSocketConfig::default() + .max_message_size(Some(limit)) + .max_frame_size(Some(limit)), + ), + None, + ), + } + .map_err(|e| e.to_string())?; match socket.get_mut() { MaybeTlsStream::Plain(s) => { s.set_read_timeout(Some(Duration::from_millis(20))) diff --git a/crates/benchup-app/src/settings.rs b/crates/benchup-app/src/settings.rs index 103cbca..a3cce0c 100644 --- a/crates/benchup-app/src/settings.rs +++ b/crates/benchup-app/src/settings.rs @@ -20,6 +20,7 @@ pub(super) enum Section { Rendering, Tools, ExportScales, + AgentConnection, } impl Section { @@ -39,12 +40,12 @@ impl Section { ), ( tr!("settings.system"), - &[Self::Diagnostics, Self::Rendering, Self::ExportScales], + &[Self::Diagnostics, Self::Rendering, Self::ExportScales, Self::AgentConnection], ), ] } #[cfg(test)] - const ALL: [Self; 11] = [ + const ALL: [Self; 12] = [ Self::Units, Self::Navigation, Self::Shortcuts, @@ -56,6 +57,7 @@ impl Section { Self::Rendering, Self::Tools, Self::ExportScales, + Self::AgentConnection, ]; fn label(self) -> &'static str { match self { @@ -70,6 +72,7 @@ impl Section { Self::Rendering => tr!("settings.rendering"), Self::Tools => tr!("settings.tools"), Self::ExportScales => tr!("settings.export_scales"), + Self::AgentConnection => tr!("agent_link.title"), } } @@ -86,6 +89,7 @@ impl Section { Self::Rendering => "settings_colors", Self::Tools => "settings_shortcuts", Self::ExportScales => "units", + Self::AgentConnection => "share", } } @@ -208,6 +212,7 @@ pub(super) struct GeneralSettings { pub tool_keys: Vec, pub stock: stock_settings::Settings, pub export_scale: export_scale::Preferences, + pub agent: agent_link::Preferences, } impl Default for GeneralSettings { @@ -231,6 +236,7 @@ impl Default for GeneralSettings { default_units: Default::default(), stock: Default::default(), export_scale: Default::default(), + agent: Default::default(), tool_keys: TOOLS.iter().map(|(_, key)| key.to_string()).collect(), } } @@ -293,6 +299,7 @@ impl GeneralSettings { self.space_mouse.normalize(); self.stock.normalize(); self.export_scale.normalize(); + self.agent.normalize(); // Earlier builds saved bare codes; every catalog now uses language_REGION. match self.language.as_str() { "en" => self.language = "en_US".into(), @@ -580,6 +587,7 @@ impl BenchUpApp { Section::Rendering => "settings/rendering", Section::Tools => "settings/tools", Section::ExportScales => "settings/export_scales", + Section::AgentConnection => "settings/agent_connection", }; let scroll = sidebar::restored_scroll_area( ui, @@ -634,6 +642,7 @@ impl BenchUpApp { fn settings_contents(&mut self, ui: &mut egui::Ui) { match self.general_settings.section { Section::ExportScales => self.general_settings.export_scale.settings_ui(ui), + Section::AgentConnection => self.agent_link_settings(ui), Section::Tools => { ui.checkbox( &mut self.general_settings.reset_axis_locks_on_action, diff --git a/crates/benchup-app/src/sharing.rs b/crates/benchup-app/src/sharing.rs index 3eeda10..d137ce1 100644 --- a/crates/benchup-app/src/sharing.rs +++ b/crates/benchup-app/src/sharing.rs @@ -368,11 +368,17 @@ impl BenchUpApp { .map_or(0, |l| l.file_updated_at), }); let path = self.project.path.clone(); + // A collaborator's revision of the same project is not a different + // project: waiting agent calls still apply. + let other_project = self.doc.shared_project.as_ref().is_none_or(|open| open.id != remote.id); // Do not replace the whole project: preserve file operation / unsaved prompts. let edit_context = self.doc.model.edit_context; let previous_selection = self.sharing.desired.clone(); self.doc.project_author = doc.project_author; self.doc.model = doc.model; + if other_project { + self.doc_epoch += 1; + } self.doc.model.edit_context = edit_context.filter(|id| self.doc.model.objects.contains_key(id)); self.doc.name = doc.name; @@ -742,6 +748,54 @@ impl BenchUpApp { == &self.sharing.confirmed && self.sharing.desired == self.sharing.confirmed) } + /// The project is shared with this user for viewing only. + pub(super) fn shared_view_only(&self) -> bool { + self.account.sharing_available() && self.doc.shared_project.is_some() && self.sharing.role == "view" + } + /// Editing waits only for the server to confirm the selection lease: + /// access, connection and project data are all fine. + pub(super) fn shared_lease_pending(&self, ctx: &egui::Context) -> bool { + self.account.sharing_available() + && self.doc.shared_project.is_some() + && matches!(self.sharing.role.as_str(), "owner" | "edit") + && self.sharing.conflict.is_none() + && self.sharing.baseline.is_some() + && ctx.input(|i| i.time) - self.sharing.last_ack < 10. + && (self.sharing.frame_selection.as_ref().unwrap_or(&self.selected) != &self.sharing.confirmed + || self.sharing.desired != self.sharing.confirmed) + } + /// Puts the shared-project state where agent tests need it: `role` + /// access, a live connection (acknowledged at time 0), and the lease + /// `desired` and `confirmed` so far. + #[cfg(test)] + pub(super) fn simulate_shared_access(&mut self, role: &str, desired: BTreeSet, confirmed: BTreeSet) { + self.sharing.role = role.into(); + self.sharing.baseline = Some(serde_json::json!({})); + self.sharing.last_ack = 0.; + self.sharing.desired = desired; + self.sharing.confirmed = confirmed; + } + /// The selection changed outside the frame's own bookkeeping (an agent + /// call runs in `App::logic`, before `sharing_before_frame`): ask the + /// server for the lease on it, as `sharing_after_frame` does for clicks. + pub(super) fn selection_changed_outside_frame(&mut self, before: &BTreeSet) { + if self.account.sharing_available() && self.doc.shared_project.is_some() && *before != self.selected { + self.sharing.desired = self.selected.clone(); + self.sharing.selection_dirty = true; + } + } + /// The selection this user asked the server for, and whether it still has + /// to be sent. + #[cfg(test)] + pub(super) fn requested_shared_selection(&self) -> (&BTreeSet, bool) { + (&self.sharing.desired, self.sharing.selection_dirty) + } + /// The entities this user holds the edit lease on, when the project is + /// shared and sharing is available; `None` means edits are unrestricted. + pub(super) fn shared_lease(&self) -> Option> { + (self.account.sharing_available() && self.doc.shared_project.is_some()) + .then(|| self.sharing.confirmed.clone()) + } pub(super) fn shared_edit_block_reason(&self, ctx: &egui::Context) -> &'static str { if !matches!(self.sharing.role.as_str(), "owner" | "edit") { tr!("sharing.editing_access_has_not_been_confirmed_check_your") @@ -1929,6 +1983,36 @@ mod websocket_state_tests { assert_eq!(app.sharing.desired, BTreeSet::from([edge])); assert!(!app.sharing.selection_dirty); } + #[test] + fn a_collaborator_revision_keeps_waiting_agent_calls_but_another_project_does_not() { + let ctx = egui::Context::default(); + let mut app = BenchUpApp::with_document(Document::default(), None, true); + app.doc.shared_project = Some(benchup_core::SharedProject { + server: "https://sharing.example".into(), + id: "a".repeat(48), + revision: 1, + server_updated_at: 100, + file_updated_at: 100, + }); + let remote = |app: &BenchUpApp, id: &str, revision: u64| Remote { + id: id.repeat(48), + role: "edit".into(), + revision, + updated_at: 100, + server_time: 100, + document: Some(serde_json::to_value(app.doc.clone()).unwrap()), + requests: vec![], + locks: vec![], + actor: None, + }; + let epoch = app.doc_epoch; + let revision = remote(&app, "a", 2); + app.apply_shared_remote(&ctx, &revision).unwrap(); + assert_eq!(app.doc_epoch, epoch, "a revision of the open project is the same project"); + let other = remote(&app, "b", 3); + app.apply_shared_remote(&ctx, &other).unwrap(); + assert_eq!(app.doc_epoch, epoch + 1); + } } #[cfg(test)] diff --git a/crates/benchup-app/src/sidebar.rs b/crates/benchup-app/src/sidebar.rs index 8589b42..206609e 100644 --- a/crates/benchup-app/src/sidebar.rs +++ b/crates/benchup-app/src/sidebar.rs @@ -1630,6 +1630,30 @@ mod preference_tests { assert_eq!(old.general.export_scale, export_scale::Preferences::default()); } + #[test] + fn agent_connection_survives_reload_without_a_model_edit() { + let ctx = egui::Context::default(); + let mut storage = SavedPreferences::default(); + let mut app = BenchUpApp::with_document(Document::default(), None, true); + app.persist_inspector_changes(&ctx, Some(&mut storage), false); + app.general_settings.section = settings::Section::AgentConnection; + app.general_settings.agent.enabled = true; + app.general_settings.agent.port = 5000; + storage.flushed = false; + app.persist_inspector_changes(&ctx, Some(&mut storage), false); + assert!(storage.flushed); + assert!(!app.project.modified); + assert!(app.dirty_since.is_none()); + let mut restored = BenchUpApp::with_document(Document::default(), None, true); + restored.restore_inspector_preferences(&egui::Context::default(), Some(&storage)); + assert!(restored.general_settings.agent.enabled); + assert_eq!(restored.general_settings.agent.port, 5000); + assert_eq!(restored.general_settings.section, settings::Section::AgentConnection); + // Older preferences leave the connection off. + let old: InspectorPreferences = serde_json::from_str("{}").unwrap(); + assert!(!old.general.agent.enabled); + } + #[test] fn exchange_choices_survive_reload_without_a_model_edit() { let ctx = egui::Context::default(); diff --git a/crates/benchup-cli/Cargo.toml b/crates/benchup-cli/Cargo.toml index 955af8a..c31e10f 100644 --- a/crates/benchup-cli/Cargo.toml +++ b/crates/benchup-cli/Cargo.toml @@ -14,6 +14,8 @@ path = "src/main.rs" benchup-core = { path = "../benchup-core" } benchup-tools = { path = "../benchup-tools" } serde_json.workspace = true +getrandom = "0.4" +tungstenite = { version = "0.29", default-features = false, features = ["handshake"] } [dev-dependencies] tempfile = "3" diff --git a/crates/benchup-cli/skill/benchup/SKILL.md b/crates/benchup-cli/skill/benchup/SKILL.md index daa2206..15b18ad 100644 --- a/crates/benchup-cli/skill/benchup/SKILL.md +++ b/crates/benchup-cli/skill/benchup/SKILL.md @@ -5,7 +5,7 @@ description: Design or edit furniture and woodworking projects in BenchUp (.bup # BenchUp projects -Use the `benchup` CLI to read and edit `.bup` projects. It edits the file, not an open BenchUp window. +Use the `benchup` CLI to read and edit `.bup` projects. Without `--live` it edits the file, not an open BenchUp window. When the user wants you to edit the project open in BenchUp and `BENCHUP_AGENT_TOKEN` is set (Settings → Agent connection in BenchUp), use `--live` instead of the file name: `benchup scene --live`, `benchup call --live --batch -`, `benchup render --live`. 1. Run `benchup guide` once and follow it. It explains the axes (the front faces -Y), joint roles, the batch workflow and recipes. 2. Read the project with `benchup scene file.bup` (on big projects, filter it: `benchup scene file.bup '{"depth":0}'`). Create a new one with `benchup new file.bup --name "..."`. diff --git a/crates/benchup-cli/src/bridge.rs b/crates/benchup-cli/src/bridge.rs new file mode 100644 index 0000000..b3c56ae --- /dev/null +++ b/crates/benchup-cli/src/bridge.rs @@ -0,0 +1,956 @@ +//! `benchup bridge`: lets agents edit the project open in BenchUp. +//! +//! ```text +//! agent ──MCP (stdio)──▶ benchup bridge (127.0.0.1) ◀──WebSocket── BenchUp (web or desktop) +//! benchup call --live ──WebSocket──┘ +//! ``` +//! +//! A browser tab cannot listen on a port, so BenchUp connects out to the +//! bridge, which listens on 127.0.0.1 only. Whoever connects proves it knows +//! the pairing token shown in BenchUp with a challenge-response +//! ([`benchup_tools::pairing`]); the token itself is never sent. Browsers must +//! also come from an allowed `Origin`, so other web pages cannot reach it. +//! +//! Two kinds of peers connect: +//! * the app (`role` absent or `"app"`): one at a time; it runs the calls it +//! is sent and answers each with a result. While one is connected, another +//! app is refused (before `ready`) rather than replacing it, so two BenchUp +//! windows sharing a token do not keep pushing each other off; +//! * clients (`role: "client"`): `benchup call --live` and friends, or a +//! second `benchup bridge` serving MCP while this one holds the port. Each +//! sends `request` frames, which are forwarded to the app. +//! +//! Frames are JSON text. Handshake, for both kinds: +//! * peer: `{"type":"authenticate","connection":NAME,"role":"app"|"client"}` +//! * bridge: `{"type":"challenge","nonce":HEX64,"protocol":2,"server":"benchup-bridge","version":V}` +//! * peer: `{"type":"prove","nonce":HEX64,"proof":HMAC(token,"benchup-agent-link/\n\n")}` +//! * bridge: `{"type":"ready","proof":HMAC(token,"benchup-agent-link/bridge\n…")}` +//! +//! Then, bridge to app: `call {id, name, arguments, client, deadline_ms}` and +//! `cancel {id}`; app to bridge: `result {id, ok, result}` and `status +//! {message}`. Client to bridge: `request {id, name, arguments, client}`; +//! bridge to client: `response {id, ok, result}`. Besides the shared tools, +//! `name` may be `run_batch` (`{calls, dry_run, single}`, one undo step; +//! `single: true` with one call runs it as written, without `$N` +//! references, and answers with that call's own result), `undo` +//! (`{redo}`) or `render_view` (`{view, width, height}`, answered with +//! `png_base64` and `info`). +use crate::mcp::Backend; +use benchup_tools::pairing; +use serde_json::{Value, json}; +use std::collections::HashMap; +use std::io::{Read, Write}; +use std::net::{Ipv4Addr, SocketAddr, TcpListener, TcpStream}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; +use tungstenite::protocol::WebSocketConfig; +use tungstenite::{ + Message, WebSocket, + handshake::server::{ErrorResponse, Request, Response}, +}; + +pub const DEFAULT_PORT: u16 = pairing::DEFAULT_PORT; +/// The published web app. Local development servers are always allowed too. +pub const DEFAULT_ORIGIN: &str = "https://benchup3d.github.io"; +pub const TOKEN_ENV: &str = "BENCHUP_AGENT_TOKEN"; +pub const PORT_ENV: &str = "BENCHUP_BRIDGE_PORT"; +pub const MIN_TOKEN_LEN: usize = 16; +/// The handshake, WebSocket upgrade included, must finish within this long. +const AUTH_TIMEOUT: Duration = Duration::from_secs(5); +const POLL: Duration = Duration::from_millis(25); +/// Largest message accepted before the peer has proved the pairing token. +const PRE_AUTH_MAX_BYTES: usize = 1024 * 1024; +/// Text frames before authentication are tiny; anything bigger is hostile. +const PRE_AUTH_TEXT_MAX: usize = 64 * 1024; +/// Largest message from an authenticated peer (scene dumps, rendered views). +const MAX_MESSAGE_BYTES: usize = pairing::MAX_BRIDGE_MESSAGE_BYTES; +/// Largest call forwarded to the app: it drops the whole connection on +/// anything bigger, so larger calls are refused here, one at a time. +const MAX_CALL_BYTES: usize = pairing::MAX_APP_MESSAGE_BYTES; +/// A refusal: a stable `code` the app translates, and English text for logs +/// and command-line clients. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Refusal { + code: &'static str, + message: &'static str, +} +/// Refusal sent to an app while another one is connected. +const APP_BUSY: Refusal = + Refusal { code: pairing::refusal::APP_BUSY, message: "another BenchUp window is already connected to this bridge" }; +const UNKNOWN_ROLE: Refusal = Refusal { code: pairing::refusal::UNKNOWN_ROLE, message: "unknown role" }; +const TOKEN_MISMATCH: Refusal = Refusal { code: pairing::refusal::TOKEN_MISMATCH, message: "pairing token does not match" }; +const UNEXPECTED: Refusal = Refusal { code: pairing::refusal::UNEXPECTED, message: "unexpected message during pairing" }; +/// Connections that have not finished authenticating; extras are closed at once. +const MAX_UNAUTHENTICATED: usize = 8; +const SERVER: &str = "benchup-bridge"; + +#[derive(Clone, Debug)] +pub struct Config { + /// 0 picks a free port (tests). + pub port: u16, + pub token: String, + pub extra_origins: Vec, + /// How long the app may take to answer one call. + pub call_timeout: Duration, + /// How long a call waits for BenchUp to connect when it is not connected + /// yet (the app retries every few seconds). + pub app_wait: Duration, +} + +impl Config { + pub fn new(token: impl Into) -> Self { + Self { + port: DEFAULT_PORT, + token: token.into(), + extra_origins: Vec::new(), + call_timeout: Duration::from_secs(120), + app_wait: Duration::from_secs(15), + } + } +} + +/// The pairing token from the environment (never from the command line, +/// which other local users can read). +pub fn token_from_env() -> Result { + let token = std::env::var(TOKEN_ENV).unwrap_or_default(); + let token = token.trim(); + if token.is_empty() { + return Err(format!( + "set {TOKEN_ENV} to the pairing token shown in BenchUp (Settings → Agent connection)" + )); + } + if token.len() < MIN_TOKEN_LEN { + return Err(format!("{TOKEN_ENV} is too short to be a BenchUp pairing token")); + } + Ok(token.to_owned()) +} + +/// The bridge port: `BENCHUP_BRIDGE_PORT`, or the default. +pub fn port_from_env() -> Result { + match std::env::var(PORT_ENV) { + Ok(port) if !port.trim().is_empty() => { + port.trim().parse().map_err(|_| format!("{PORT_ENV} must be a port number")) + } + _ => Ok(DEFAULT_PORT), + } +} + +fn random_nonce() -> Result { + let mut bytes = [0u8; 32]; + getrandom::fill(&mut bytes).map_err(|e| format!("the operating system has no secure randomness: {e}"))?; + Ok(pairing::to_hex(&bytes)) +} + +fn now_ms() -> u64 { + SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |d| d.as_millis() as u64) +} + +/// Whether a WebSocket `Origin` may connect: none (a native program), the +/// published web app, a local development server, or one passed with +/// `--allow-origin`. +pub fn origin_allowed(origin: Option<&str>, extra: &[String]) -> bool { + let Some(origin) = origin else { return true }; + if origin == DEFAULT_ORIGIN || extra.iter().any(|o| o == origin) { + return true; + } + ["http://localhost", "http://127.0.0.1"].iter().any(|base| { + origin.strip_prefix(base).is_some_and(|rest| { + rest.is_empty() + || rest.strip_prefix(':').is_some_and(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())) + }) + }) +} + +/// Why a forwarded call has no answer. +#[derive(Debug, PartialEq)] +enum Failure { + NotConnected, + Timeout, + Disconnected, + TooLarge, +} + +fn failure_answer(failure: Failure) -> (Value, bool) { + let value = match failure { + Failure::NotConnected => json!({"ok": false, "error": "BenchUp is not connected to the bridge", + "hint": "Open BenchUp, go to Settings → Agent connection, turn on Allow agent connection and check that the pairing token matches BENCHUP_AGENT_TOKEN."}), + Failure::Timeout => json!({"ok": false, "error": "BenchUp did not answer in time", + "hint": "The call was cancelled. The designer may be asked to grant access; call get_scene to check the current state before retrying."}), + Failure::Disconnected => json!({"ok": false, "error": "BenchUp disconnected before answering", + "hint": "Check with get_scene whether the change was applied once BenchUp reconnects."}), + Failure::TooLarge => json!({"ok": false, "error": format!("the call is larger than BenchUp accepts ({} MB)", MAX_CALL_BYTES / (1024 * 1024)), + "hint": "Split the work into several smaller calls or batches."}), + }; + (value, false) +} + +struct App { + id: u64, + tx: Sender, + #[cfg_attr(not(test), allow(dead_code))] + name: String, +} + +#[derive(Default)] +struct Shared { + app: Option, + next_conn: u64, + next_call: u64, + /// call id -> (app connection id, where to deliver the answer) + pending: HashMap>)>, + last_activity: Option, +} + +/// Counts a connection against the unauthenticated-connection cap until released. +struct Slot(Option>); +impl Slot { + fn release(&mut self) { + if let Some(counter) = self.0.take() { + counter.fetch_sub(1, Ordering::SeqCst); + } + } +} +impl Drop for Slot { + fn drop(&mut self) { + self.release(); + } +} + +/// A stream whose reads stop at a deadline, so slow or silent peers cannot +/// hold a connection slot, and whose poll interval is adjustable. +struct Guarded { + stream: TcpStream, + deadline: Option, + poll: Duration, +} +impl Read for Guarded { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let mut wait = self.poll; + if let Some(deadline) = self.deadline { + let remaining = deadline.saturating_duration_since(Instant::now()); + if remaining.is_zero() { + return Err(std::io::ErrorKind::TimedOut.into()); + } + wait = wait.min(remaining); + } + self.stream.set_read_timeout(Some(wait))?; + self.stream.read(buf) + } +} +impl Write for Guarded { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.stream.write(buf) + } + fn flush(&mut self) -> std::io::Result<()> { + self.stream.flush() + } +} + +/// Why the bridge could not start. +#[derive(Debug)] +pub enum StartError { + /// Another program, usually another bridge, holds the port. + InUse(u16), + Other(String), +} + +impl std::fmt::Display for StartError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::InUse(port) => write!(f, "port {port} on 127.0.0.1 is already in use (is another bridge running?)"), + Self::Other(message) => f.write_str(message), + } + } +} + +struct Inner { + shared: Mutex, + config: Config, +} + +/// The listening bridge. Cloning shares it. +#[derive(Clone)] +pub struct Hub { + inner: Arc, + port: u16, +} + +impl Hub { + /// Binds 127.0.0.1 only and accepts connections in the background. + pub fn start(config: Config) -> Result { + if config.token.len() < MIN_TOKEN_LEN { + return Err(StartError::Other(format!("the pairing token must be at least {MIN_TOKEN_LEN} characters"))); + } + random_nonce().map_err(StartError::Other)?; // refuse to start without OS randomness + let listener = TcpListener::bind(SocketAddr::from((Ipv4Addr::LOCALHOST, config.port))).map_err(|e| { + if e.kind() == std::io::ErrorKind::AddrInUse { + StartError::InUse(config.port) + } else { + StartError::Other(format!("cannot listen on 127.0.0.1:{}: {e}", config.port)) + } + })?; + let port = listener.local_addr().map_err(|e| StartError::Other(e.to_string()))?.port(); + let inner = Arc::new(Inner { shared: Mutex::new(Shared::default()), config }); + let accept = inner.clone(); + let unauthenticated = Arc::new(AtomicUsize::new(0)); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + if unauthenticated.fetch_add(1, Ordering::SeqCst) >= MAX_UNAUTHENTICATED { + unauthenticated.fetch_sub(1, Ordering::SeqCst); + continue; // dropping the stream closes it + } + let slot = Slot(Some(unauthenticated.clone())); + let inner = accept.clone(); + std::thread::spawn(move || handle_connection(stream, inner, slot)); + } + }); + Ok(Self { inner, port }) + } + + pub fn port(&self) -> u16 { + self.port + } + + /// The connected app's name, if one is connected. + #[cfg(test)] + pub fn app(&self) -> Option { + self.inner.shared.lock().unwrap().app.as_ref().map(|app| app.name.clone()) + } + + /// When a call last went through, for `--idle-exit`. + fn idle_for(&self) -> Option { + self.inner.shared.lock().unwrap().last_activity.map(|at| at.elapsed()) + } + + /// Sends one call to the app and waits for its answer. When no app is + /// connected, waits up to `app_wait` for one. + pub fn forward(&self, client: &str, name: &str, arguments: Value) -> (Value, bool) { + match self.try_forward(client, name, arguments) { + Ok(answer) => answer, + Err(failure) => failure_answer(failure), + } + } + + fn try_forward(&self, client: &str, name: &str, arguments: Value) -> Result<(Value, bool), Failure> { + let config = &self.inner.config; + let waited = Instant::now(); + let (answer_tx, answer_rx) = mpsc::channel(); + let (call_id, tx) = loop { + { + let mut shared = self.inner.shared.lock().unwrap(); + shared.last_activity = Some(Instant::now()); + if let Some(app) = &shared.app { + let (conn, tx) = (app.id, app.tx.clone()); + shared.next_call += 1; + let call_id = format!("c{}", shared.next_call); + let deadline_ms = now_ms() + config.call_timeout.as_millis() as u64; + let frame = json!({"type": "call", "id": call_id, "name": name, "arguments": arguments, + "client": client, "deadline_ms": deadline_ms}) + .to_string(); + if frame.len() > MAX_CALL_BYTES { + return Err(Failure::TooLarge); + } + if tx.send(frame).is_ok() { + shared.pending.insert(call_id.clone(), (conn, answer_tx)); + break (call_id, tx); + } + } + } + if waited.elapsed() >= config.app_wait { + return Err(Failure::NotConnected); + } + std::thread::sleep(Duration::from_millis(50)); + }; + let answer = match answer_rx.recv_timeout(config.call_timeout) { + Ok(answer) => answer, + Err(RecvTimeoutError::Timeout) => { + // A result that arrives later finds no pending call and is ignored. + self.inner.shared.lock().unwrap().pending.remove(&call_id); + let _ = tx.send(json!({"type": "cancel", "id": call_id}).to_string()); + Err(Failure::Timeout) + } + Err(RecvTimeoutError::Disconnected) => Err(Failure::Disconnected), + }; + self.inner.shared.lock().unwrap().last_activity = Some(Instant::now()); + answer + } + + /// Exits the process once no call has gone through for `idle` (the + /// bridge `benchup call --live` starts on its own does not linger). + pub fn exit_when_idle(&self, idle: Duration) { + self.inner.shared.lock().unwrap().last_activity.get_or_insert_with(Instant::now); + let hub = self.clone(); + std::thread::spawn(move || { + loop { + std::thread::sleep(Duration::from_secs(1).min(idle)); + if hub.idle_for().is_some_and(|quiet| quiet >= idle) { + eprintln!("benchup bridge: no calls for {} s, exiting", idle.as_secs()); + std::process::exit(0); + } + } + }); + } +} + +fn is_timeout(error: &tungstenite::Error) -> bool { + matches!(error, tungstenite::Error::Io(e) + if matches!(e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut)) +} + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Role { + App, + Client, +} + +fn reject(ws: &mut WebSocket, refusal: Refusal) { + let frame = json!({"type": "error", "code": refusal.code, "message": refusal.message}); + let _ = ws.send(Message::text(frame.to_string())); + let _ = ws.close(None); + let _ = ws.flush(); +} + +/// The pairing handshake: who connected, or `None` (the socket is closed). +/// Once the peer has proved the token, `admit` decides whether it may stay; +/// a refusal is sent instead of `ready`. +fn authenticate( + ws: &mut WebSocket, + token: &str, + deadline: Instant, + admit: &mut dyn FnMut(Role, &str) -> Result<(), Refusal>, +) -> Option<(Role, String)> { + let bridge_nonce = random_nonce().ok()?; + let mut hello: Option<(Role, String)> = None; + loop { + // Checked on every iteration so traffic cannot stretch the window. + if Instant::now() >= deadline { + return None; + } + match ws.read() { + Ok(Message::Text(text)) => { + if text.len() > PRE_AUTH_TEXT_MAX { + return None; + } + let frame: Value = serde_json::from_str(&text).unwrap_or(Value::Null); + match (&hello, frame.get("type").and_then(Value::as_str)) { + (None, Some("authenticate")) => { + let role = match frame.get("role").and_then(Value::as_str) { + None | Some("app") => Role::App, + Some("client") => Role::Client, + Some(_) => { + reject(ws, UNKNOWN_ROLE); + return None; + } + }; + let name: String = frame + .get("connection") + .and_then(Value::as_str) + .unwrap_or_default() + .chars() + .filter(|c| !c.is_control()) + .take(pairing::MAX_CLIENT_CHARS) + .collect(); + let challenge = json!({"type": "challenge", "nonce": bridge_nonce, "server": SERVER, + "version": env!("CARGO_PKG_VERSION"), "protocol": pairing::PROTOCOL}); + ws.send(Message::text(challenge.to_string())).ok()?; + hello = Some((role, name)); + } + (Some((role, name)), Some("prove")) => { + let peer_nonce = frame.get("nonce").and_then(Value::as_str).unwrap_or(""); + let proof = frame.get("proof").and_then(Value::as_str).unwrap_or(""); + let expected = match role { + Role::App => pairing::app_proof(token, &bridge_nonce, peer_nonce), + Role::Client => pairing::client_proof(token, &bridge_nonce, peer_nonce), + }; + if !pairing::valid_nonce(peer_nonce) || !pairing::proofs_match(proof, &expected) { + reject(ws, TOKEN_MISMATCH); + return None; + } + if let Err(refusal) = admit(*role, name) { + reject(ws, refusal); + return None; + } + let ready = json!({"type": "ready", "server": SERVER, "version": env!("CARGO_PKG_VERSION"), + "protocol": pairing::PROTOCOL, + "proof": pairing::bridge_proof(token, &bridge_nonce, peer_nonce)}); + ws.send(Message::text(ready.to_string())).ok()?; + return Some((*role, name.clone())); + } + _ => { + reject(ws, UNEXPECTED); + return None; + } + } + } + Ok(Message::Close(_)) => return None, + // Before the hello these are ignored (and never extend the deadline); + // afterwards anything but the proof is a protocol error. + Ok(_) if hello.is_some() => { + reject(ws, UNEXPECTED); + return None; + } + Ok(_) => {} + Err(e) if is_timeout(&e) => {} + Err(_) => return None, + } + } +} + +fn handle_connection(stream: TcpStream, inner: Arc, mut slot: Slot) { + let _ = stream.set_nodelay(true); + let _ = stream.set_write_timeout(Some(AUTH_TIMEOUT)); + let deadline = Instant::now() + AUTH_TIMEOUT; + let guarded = Guarded { stream, deadline: Some(deadline), poll: AUTH_TIMEOUT }; + let extra_origins = inner.config.extra_origins.clone(); + let check_origin = |request: &Request, response: Response| -> Result { + let origin = request.headers().get("origin").map(|v| v.to_str().unwrap_or("\u{0}invalid")); + if origin_allowed(origin, &extra_origins) { + Ok(response) + } else { + eprintln!("benchup bridge: refused a connection from origin {origin:?}"); + let mut rejection = ErrorResponse::new(Some("origin not allowed".to_owned())); + *rejection.status_mut() = tungstenite::http::StatusCode::FORBIDDEN; + Err(rejection) + } + }; + let limits = + WebSocketConfig::default().max_message_size(Some(PRE_AUTH_MAX_BYTES)).max_frame_size(Some(PRE_AUTH_MAX_BYTES)); + let Ok(mut ws) = tungstenite::accept_hdr_with_config(guarded, check_origin, Some(limits)) else { return }; + ws.get_mut().poll = POLL; + // The app is registered while pairing, atomically, so a second one is + // refused instead of replacing it. + let mut registered: Option<(u64, Receiver)> = None; + let mut admit = |role: Role, name: &str| -> Result<(), Refusal> { + if role == Role::Client { + return Ok(()); + } + let mut guard = inner.shared.lock().unwrap(); + if guard.app.is_some() { + return Err(APP_BUSY); + } + let (tx, rx) = mpsc::channel(); + guard.next_conn += 1; + let id = guard.next_conn; + guard.app = Some(App { id, tx, name: name.to_owned() }); + registered = Some((id, rx)); + Ok(()) + }; + let authenticated = authenticate(&mut ws, &inner.config.token, deadline, &mut admit); + let Some((role, name)) = authenticated else { + // Registered, but the `ready` could not be sent. + if let Some((id, _)) = registered { + unregister_app(&inner, id); + } + return; + }; + // Authenticated: the unauthenticated slot and the small limits go away. + slot.release(); + ws.get_mut().deadline = None; + let _ = ws.get_mut().stream.set_write_timeout(Some(Duration::from_secs(30))); + ws.set_config(|c| { + c.max_message_size = Some(MAX_MESSAGE_BYTES); + c.max_frame_size = Some(MAX_MESSAGE_BYTES); + }); + match (role, registered) { + (Role::App, Some((conn_id, rx))) => serve_app(ws, &inner, &name, conn_id, rx), + (Role::App, None) => {} + (Role::Client, _) => serve_client(ws, Hub { inner, port: 0 }, name), + } +} + +/// Forgets the app connection `conn_id`, if it is still the registered one, +/// and fails the calls it was asked. +fn unregister_app(inner: &Inner, conn_id: u64) { + let mut guard = inner.shared.lock().unwrap(); + if guard.app.as_ref().is_some_and(|a| a.id == conn_id) { + guard.app = None; + eprintln!("benchup bridge: BenchUp disconnected"); + } + let lost: Vec = guard.pending.iter().filter(|(_, (c, _))| *c == conn_id).map(|(k, _)| k.clone()).collect(); + for key in lost { + if let Some((_, answer)) = guard.pending.remove(&key) { + let _ = answer.send(Err(Failure::Disconnected)); + } + } +} + +fn serve_app(mut ws: WebSocket, inner: &Inner, name: &str, conn_id: u64, rx: Receiver) { + eprintln!("benchup bridge: BenchUp connected ({name})"); + 'connection: loop { + loop { + match rx.try_recv() { + Ok(text) => { + if ws.send(Message::text(text)).is_err() { + break 'connection; + } + } + Err(TryRecvError::Empty) => break, + Err(TryRecvError::Disconnected) => { + // No longer registered: close. + let _ = ws.close(None); + let _ = ws.flush(); + break 'connection; + } + } + } + match ws.read() { + Ok(Message::Text(text)) => handle_app_frame(&text, conn_id, &inner.shared), + Ok(Message::Close(_)) => break, + Ok(_) => {} + Err(e) if is_timeout(&e) => {} + Err(_) => break, + } + } + unregister_app(inner, conn_id); +} + +fn handle_app_frame(text: &str, conn_id: u64, shared: &Mutex) { + let Ok(frame) = serde_json::from_str::(text) else { return }; + match frame.get("type").and_then(Value::as_str) { + Some("result") => { + let Some(id) = frame.get("id").and_then(Value::as_str) else { return }; + let mut guard = shared.lock().unwrap(); + // Only the connection that was asked may answer. + if guard.pending.get(id).is_some_and(|(c, _)| *c == conn_id) + && let Some((_, answer)) = guard.pending.remove(id) + { + let ok = frame.get("ok").and_then(Value::as_bool).unwrap_or(false); + let _ = answer.send(Ok((frame.get("result").cloned().unwrap_or(Value::Null), ok))); + } + } + Some("status") => { + let message: String = + frame.get("message").and_then(Value::as_str).unwrap_or("").chars().filter(|c| !c.is_control()).take(200).collect(); + eprintln!("benchup bridge: BenchUp: {message}"); + } + _ => {} + } +} + +/// A command-line client: each `request` is forwarded to the app in turn. +fn serve_client(mut ws: WebSocket, hub: Hub, name: String) { + loop { + match ws.read() { + Ok(Message::Text(text)) => { + let frame: Value = serde_json::from_str(&text).unwrap_or(Value::Null); + if frame.get("type").and_then(Value::as_str) != Some("request") { + continue; + } + let id = frame.get("id").cloned().unwrap_or(Value::Null); + let (result, ok) = match frame.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()) { + None => (json!({"ok": false, "error": "the request names no tool"}), false), + Some(tool) => { + let arguments = frame.get("arguments").cloned().unwrap_or_else(|| json!({})); + let client = frame.get("client").and_then(Value::as_str).unwrap_or(&name); + hub.forward(client, tool, arguments) + } + }; + let response = json!({"type": "response", "id": id, "ok": ok, "result": result}); + if ws.send(Message::text(response.to_string())).is_err() { + return; + } + } + Ok(Message::Close(_)) => return, + Ok(_) => {} + Err(e) if is_timeout(&e) => {} + Err(_) => return, + } + } +} + +/// Talks to a running bridge as a client. +#[derive(Clone, Debug)] +pub struct Client { + pub port: u16, + pub token: String, + /// Shown to the designer as the agent's name. + pub name: String, + /// How long to wait for one answer (the bridge's own limits plus a margin). + pub timeout: Duration, +} + +/// Why a client request failed before the bridge answered. +#[derive(Debug, PartialEq)] +pub enum ClientError { + /// Nothing listens on the port. + NoBridge, + Failed(String), +} + +impl std::fmt::Display for ClientError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::NoBridge => f.write_str("no bridge is running"), + Self::Failed(message) => f.write_str(message), + } + } +} + +impl Client { + pub fn new(port: u16, token: impl Into, name: impl Into) -> Self { + Self { port, token: token.into(), name: name.into(), timeout: Duration::from_secs(150) } + } + + fn read_frame(ws: &mut WebSocket) -> Result { + loop { + match ws.read() { + Ok(Message::Text(text)) => { + return serde_json::from_str(&text).map_err(|e| ClientError::Failed(format!("bad frame from the bridge: {e}"))); + } + Ok(Message::Close(_)) => return Err(ClientError::Failed("the bridge closed the connection".into())), + Ok(_) => {} + Err(e) if is_timeout(&e) => return Err(ClientError::Failed("the bridge did not answer in time".into())), + Err(e) => return Err(ClientError::Failed(format!("lost the bridge: {e}"))), + } + } + } + + /// Connects and pairs; the bridge must prove it knows the token too. + fn connect(&self) -> Result, ClientError> { + let address = SocketAddr::from((Ipv4Addr::LOCALHOST, self.port)); + let stream = TcpStream::connect_timeout(&address, Duration::from_secs(2)).map_err(|e| match e.kind() { + std::io::ErrorKind::ConnectionRefused => ClientError::NoBridge, + _ => ClientError::Failed(format!("cannot reach the bridge on 127.0.0.1:{}: {e}", self.port)), + })?; + let _ = stream.set_nodelay(true); + stream.set_read_timeout(Some(AUTH_TIMEOUT)).map_err(|e| ClientError::Failed(e.to_string()))?; + let url = format!("ws://127.0.0.1:{}/", self.port); + let limits = WebSocketConfig::default().max_message_size(Some(MAX_MESSAGE_BYTES * 2)).max_frame_size(Some(MAX_MESSAGE_BYTES * 2)); + let (mut ws, _) = tungstenite::client::client_with_config(url.as_str(), stream, Some(limits)) + .map_err(|e| ClientError::Failed(format!("the bridge refused the connection: {e}")))?; + let send = |ws: &mut WebSocket, frame: Value| { + ws.send(Message::text(frame.to_string())).map_err(|e| ClientError::Failed(format!("lost the bridge: {e}"))) + }; + send(&mut ws, json!({"type": "authenticate", "role": "client", "connection": self.name}))?; + let challenge = Self::read_frame(&mut ws)?; + let bridge_nonce = challenge.get("nonce").and_then(Value::as_str).unwrap_or_default().to_owned(); + if challenge.get("type").and_then(Value::as_str) != Some("challenge") + || challenge.get("protocol").and_then(Value::as_u64) != Some(u64::from(pairing::PROTOCOL)) + || !pairing::valid_nonce(&bridge_nonce) + { + return Err(ClientError::Failed(format!( + "something other than a compatible benchup bridge listens on 127.0.0.1:{}", + self.port + ))); + } + let nonce = random_nonce().map_err(ClientError::Failed)?; + let proof = pairing::client_proof(&self.token, &bridge_nonce, &nonce); + send(&mut ws, json!({"type": "prove", "nonce": nonce, "proof": proof}))?; + let ready = Self::read_frame(&mut ws)?; + match ready.get("type").and_then(Value::as_str) { + Some("ready") => {} + Some("error") => { + return Err(ClientError::Failed(format!( + "the bridge refused this client: {}. Is {TOKEN_ENV} the token shown in BenchUp?", + ready.get("message").and_then(Value::as_str).unwrap_or("pairing failed") + ))); + } + _ => return Err(ClientError::Failed("unexpected answer from the bridge while pairing".into())), + } + let expected = pairing::bridge_proof(&self.token, &bridge_nonce, &nonce); + if !ready.get("proof").and_then(Value::as_str).is_some_and(|proof| pairing::proofs_match(proof, &expected)) { + return Err(ClientError::Failed("the bridge could not prove it knows the pairing token".into())); + } + ws.get_mut().set_read_timeout(Some(self.timeout)).map_err(|e| ClientError::Failed(e.to_string()))?; + Ok(ws) + } + + /// Runs one call in the open app through the bridge. + pub fn request(&self, name: &str, arguments: Value) -> Result<(Value, bool), ClientError> { + let mut ws = self.connect()?; + let request = json!({"type": "request", "id": 1, "name": name, "arguments": arguments, "client": self.name}); + ws.send(Message::text(request.to_string())).map_err(|e| ClientError::Failed(format!("lost the bridge: {e}")))?; + let response = Self::read_frame(&mut ws)?; + let _ = ws.close(None); + let _ = ws.flush(); + if response.get("type").and_then(Value::as_str) != Some("response") { + return Err(ClientError::Failed("unexpected answer from the bridge".into())); + } + let ok = response.get("ok").and_then(Value::as_bool).unwrap_or(false); + Ok((response.get("result").cloned().unwrap_or(Value::Null), ok)) + } +} + +/// How long a bridge started by `benchup call --live` waits for calls +/// before exiting. +pub const AUTO_IDLE: Duration = Duration::from_secs(30 * 60); + +/// `--live` commands: runs `name` in the open app. Starts a bridge in the +/// background when none is running, so CLI-first agents need no setup +/// beyond `BENCHUP_AGENT_TOKEN`. +pub fn cli_request(name: &str, arguments: Value) -> Result<(Value, bool), String> { + let client = Client::new(port_from_env()?, token_from_env()?, "benchup-cli"); + match client.request(name, arguments.clone()) { + Err(ClientError::NoBridge) => {} + other => return other.map_err(|e| e.to_string()), + } + start_background_bridge(client.port)?; + let started = Instant::now(); + loop { + match client.request(name, arguments.clone()) { + Err(ClientError::NoBridge) if started.elapsed() < Duration::from_secs(5) => { + std::thread::sleep(Duration::from_millis(100)); + } + Err(ClientError::NoBridge) => return Err("the background bridge did not start; run `benchup bridge --no-mcp` to see why".into()), + other => return other.map_err(|e| e.to_string()), + } + } +} + +fn start_background_bridge(port: u16) -> Result<(), String> { + let exe = std::env::current_exe().map_err(|e| format!("cannot find the benchup executable: {e}"))?; + std::process::Command::new(exe) + .args(["bridge", "--no-mcp", "--port", &port.to_string(), "--idle-exit", &AUTO_IDLE.as_secs().to_string()]) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + .map_err(|e| format!("cannot start a bridge: {e}"))?; + Ok(()) +} + +/// Decodes standard base64 (rendered views come back as `png_base64`). +pub fn decode_base64(text: &str) -> Result, String> { + let value = |c: u8| -> Result { + Ok(match c { + b'A'..=b'Z' => c - b'A', + b'a'..=b'z' => c - b'a' + 26, + b'0'..=b'9' => c - b'0' + 52, + b'+' => 62, + b'/' => 63, + _ => return Err("invalid base64".into()), + } as u32) + }; + let bytes: Vec = text.bytes().filter(|c| !c.is_ascii_whitespace()).collect(); + if bytes.len() % 4 != 0 { + return Err("invalid base64 length".into()); + } + let mut out = Vec::with_capacity(bytes.len() / 4 * 3); + for chunk in bytes.chunks(4) { + let pad = chunk.iter().rev().take_while(|&&c| c == b'=').count(); + if pad > 2 { + return Err("invalid base64 padding".into()); + } + let mut n = 0u32; + for &c in &chunk[..4 - pad] { + n = (n << 6) | value(c)?; + } + n <<= 6 * pad as u32; + let decoded = [(n >> 16) as u8, (n >> 8) as u8, n as u8]; + out.extend_from_slice(&decoded[..3 - pad]); + } + Ok(out) +} + +/// Where MCP calls go in live mode: this process's own bridge, or the one +/// already holding the port. +pub enum Target { + Hub(Hub), + Client(Client), +} + +/// The MCP backend of `benchup bridge`. +pub struct Live { + pub target: Target, + /// While forwarding through another bridge: how to start this process's + /// own once that one is gone (the bridge `--live` starts in the + /// background exits when idle). + takeover: Option, + /// The MCP client's name (from `initialize`), shown to the designer. + client: String, +} + +impl Live { + pub fn new(target: Target) -> Self { + Self { target, takeover: None, client: "agent".into() } + } + + /// Forwards through the bridge that holds the port, and listens on the + /// port itself with `config` if that bridge exits. + pub fn forwarding(client: Client, config: Config) -> Self { + Self { target: Target::Client(client), takeover: Some(config), client: "agent".into() } + } + + /// The bridge calls were forwarded through is gone: listen on its port. + fn take_over(&mut self) -> bool { + let Some(config) = &self.takeover else { return false }; + match Hub::start(config.clone()) { + Ok(hub) => { + eprintln!("benchup bridge: the other bridge exited; waiting for BenchUp on ws://127.0.0.1:{}", hub.port()); + self.target = Target::Hub(hub); + self.takeover = None; + true + } + Err(_) => false, + } + } +} + +const LIVE_INSTRUCTIONS: &str = "Edits the BenchUp 3D woodworking project open in the app, live, while the designer watches. \ +Lengths are millimetres, Z is up. Call get_scene first to see the model. Each changing call (and each run_batch) is one undo \ +step labelled \"Agent: …\" in BenchUp's history. Every call starts in the scope the designer is in, so put enter_object and \ +the calls that need it in one run_batch. The current selection is what the designer has selected, so \"this\" usually means \ +get_selection. When the designer is in the middle of a tool or has taken control, they are asked to grant access first; a \ +refusal comes back as an error: wait and ask them before retrying. Shared projects keep their read-only state and edit leases. \ +Call get_guide once for conventions (front is -y), joint roles and woodworking recipes."; + +impl Live { + fn forward(&mut self, name: &str, arguments: Value) -> (Value, bool) { + let bridge = match &self.target { + Target::Hub(hub) => return hub.forward(&self.client, name, arguments), + Target::Client(bridge) => Client { name: self.client.clone(), ..bridge.clone() }, + }; + let mut answer = bridge.request(name, arguments.clone()); + if answer == Err(ClientError::NoBridge) { + if self.take_over() + && let Target::Hub(hub) = &self.target + { + return hub.forward(&self.client, name, arguments); + } + // Another process took the port first: go through it. + answer = bridge.request(name, arguments); + } + answer.unwrap_or_else(|error| (json!({"ok": false, "error": format!("cannot reach the bridge: {error}")}), false)) + } +} + +impl Backend for Live { + fn call(&mut self, _client: &str, name: &str, arguments: Value) -> (Value, bool) { + self.forward(name, arguments) + } + + fn set_client(&mut self, client: &str) { + self.client = client.chars().filter(|c| !c.is_control()).take(pairing::MAX_CLIENT_CHARS).collect(); + } + + fn batch(&mut self, calls: Vec<(String, Value)>, dry_run: bool) -> (Value, bool) { + let calls: Vec = calls.into_iter().map(|(tool, arguments)| json!({"tool": tool, "arguments": arguments})).collect(); + self.forward("run_batch", json!({"calls": calls, "dry_run": dry_run})) + } + + fn history(&mut self, redo: bool) -> (Value, bool) { + self.forward("undo", json!({"redo": redo})) + } + + fn render(&mut self, view: &str, width: u32, height: u32) -> Result<(String, Value), String> { + let (mut result, ok) = self.forward("render_view", json!({"view": view, "width": width, "height": height})); + if !ok { + return Err(result.get("error").and_then(Value::as_str).unwrap_or("BenchUp could not render the view").to_owned()); + } + // Passed on as it came: MCP wants base64 too. + match result.get_mut("png_base64").map(Value::take) { + Some(Value::String(png)) if !png.is_empty() => Ok((png, result.get("info").cloned().unwrap_or(Value::Null))), + _ => Err("BenchUp sent no image".into()), + } + } + + fn instructions(&self) -> &'static str { + LIVE_INSTRUCTIONS + } +} + +#[cfg(test)] +#[path = "bridge_tests.rs"] +mod tests; diff --git a/crates/benchup-cli/src/bridge_tests.rs b/crates/benchup-cli/src/bridge_tests.rs new file mode 100644 index 0000000..096e152 --- /dev/null +++ b/crates/benchup-cli/src/bridge_tests.rs @@ -0,0 +1,256 @@ +use super::*; +use std::net::TcpStream; +use tungstenite::client::IntoClientRequest; + +const TOKEN: &str = "0123456789abcdef-test-token"; + +fn config() -> Config { + let mut config = Config::new(TOKEN); + config.port = 0; + config.app_wait = Duration::from_millis(300); + config.call_timeout = Duration::from_secs(5); + config +} + +/// Opens a WebSocket to the bridge, optionally with a browser `Origin`. +fn socket(port: u16, origin: Option<&str>) -> Result, String> { + let stream = TcpStream::connect(("127.0.0.1", port)).map_err(|e| e.to_string())?; + stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + let mut request = format!("ws://127.0.0.1:{port}/").into_client_request().unwrap(); + if let Some(origin) = origin { + request.headers_mut().insert("origin", origin.parse().unwrap()); + } + tungstenite::client::client(request, stream).map(|(ws, _)| ws).map_err(|e| e.to_string()) +} + +fn read(ws: &mut WebSocket) -> Value { + loop { + match ws.read().expect("a frame") { + Message::Text(text) => return serde_json::from_str(&text).unwrap(), + _ => continue, + } + } +} + +fn send(ws: &mut WebSocket, frame: Value) { + ws.send(Message::text(frame.to_string())).unwrap(); +} + +/// Pairs as the app (as BenchUp does) and returns the socket once `ready` +/// has been verified, or the bridge's error frame. +fn pair_app(port: u16, token: &str, origin: Option<&str>) -> Result, Value> { + let mut ws = socket(port, origin).map_err(|e| json!({"error": e}))?; + send(&mut ws, json!({"type": "authenticate", "ticket": "", "connection": "benchup-app-test"})); + let challenge = read(&mut ws); + assert_eq!(challenge["type"], "challenge"); + assert_eq!(challenge["protocol"], pairing::PROTOCOL); + let bridge_nonce = challenge["nonce"].as_str().unwrap().to_owned(); + let nonce = "ab".repeat(32); + send(&mut ws, json!({"type": "prove", "nonce": nonce, "proof": pairing::app_proof(token, &bridge_nonce, &nonce)})); + let ready = read(&mut ws); + if ready["type"] != "ready" { + return Err(ready); + } + assert!(pairing::proofs_match(ready["proof"].as_str().unwrap(), &pairing::bridge_proof(token, &bridge_nonce, &nonce))); + Ok(ws) +} + +/// A fake BenchUp that answers every call with what it received. +fn echo_app(hub: &Hub) -> std::thread::JoinHandle<()> { + let mut ws = pair_app(hub.port(), TOKEN, Some(DEFAULT_ORIGIN)).unwrap(); + let started = Instant::now(); + while hub.app().is_none() { + assert!(started.elapsed() < Duration::from_secs(5), "the app never registered"); + std::thread::sleep(Duration::from_millis(10)); + } + std::thread::spawn(move || { + loop { + let Ok(Message::Text(text)) = ws.read() else { return }; + let frame: Value = serde_json::from_str(&text).unwrap(); + if frame["type"] != "call" { + continue; + } + assert!(frame["deadline_ms"].as_u64().unwrap() > now_ms()); + let result = json!({"ok": true, "name": frame["name"], "arguments": frame["arguments"], "client": frame["client"]}); + send(&mut ws, json!({"type": "result", "id": frame["id"], "ok": true, "result": result})); + } + }) +} + +#[test] +fn origins_are_limited_to_the_web_app_and_local_servers() { + assert!(origin_allowed(None, &[])); + assert!(origin_allowed(Some(DEFAULT_ORIGIN), &[])); + assert!(origin_allowed(Some("http://localhost:8080"), &[])); + assert!(origin_allowed(Some("http://127.0.0.1"), &[])); + assert!(!origin_allowed(Some("https://evil.example"), &[])); + assert!(!origin_allowed(Some("http://localhost.evil.example"), &[])); + assert!(!origin_allowed(Some("http://localhost:"), &[])); + assert!(!origin_allowed(Some("https://benchup3d.github.io.evil.example"), &[])); + assert!(origin_allowed(Some("https://mine.example"), &["https://mine.example".into()])); +} + +#[test] +fn base64_round_trips() { + for bytes in [&b""[..], b"a", b"ab", b"abc", b"\x89PNG\r\n\x1a\n\x00\xff"] { + assert_eq!(decode_base64(&benchup_tools::render::base64(bytes)).unwrap(), bytes); + } + assert!(decode_base64("abc").is_err()); +} + +#[test] +fn clients_reach_the_app_through_the_bridge() { + let hub = Hub::start(config()).unwrap(); + let _app = echo_app(&hub); + let client = Client::new(hub.port(), TOKEN, "benchup-cli"); + let (result, ok) = client.request("get_scene", json!({"depth": 0})).unwrap(); + assert!(ok, "{result}"); + assert_eq!(result["name"], "get_scene"); + assert_eq!(result["arguments"], json!({"depth": 0})); + assert_eq!(result["client"], "benchup-cli"); + + // MCP session tools become calls the app knows. + let mut live = Live::new(Target::Client(client)); + live.set_client("claude-code"); + let (result, _) = live.batch(vec![("create_board".into(), json!({"name": "A"}))], true); + assert_eq!(result["name"], "run_batch"); + assert_eq!(result["arguments"], json!({"calls": [{"tool": "create_board", "arguments": {"name": "A"}}], "dry_run": true})); + assert_eq!(result["client"], "claude-code"); + let (result, _) = live.history(true); + assert_eq!((result["name"].clone(), result["arguments"].clone()), (json!("undo"), json!({"redo": true}))); +} + +#[test] +fn a_wrong_token_or_origin_is_refused() { + let hub = Hub::start(config()).unwrap(); + let error = pair_app(hub.port(), "a-different-token-entirely", None).err().unwrap(); + assert_eq!(error["message"], "pairing token does not match"); + assert!(socket(hub.port(), Some("https://evil.example")).is_err(), "other web pages get HTTP 403"); + + let client = Client::new(hub.port(), "a-different-token-entirely", "benchup-cli"); + let ClientError::Failed(message) = client.request("get_scene", json!({})).unwrap_err() else { panic!() }; + assert!(message.contains("pairing token does not match"), "{message}"); + assert!(hub.app().is_none()); +} + +#[test] +fn calls_without_benchup_fail_with_a_hint() { + let hub = Hub::start(config()).unwrap(); + let (result, ok) = hub.forward("agent", "get_scene", json!({})); + assert!(!ok); + assert_eq!(result["error"], "BenchUp is not connected to the bridge"); + assert!(result["hint"].as_str().unwrap().contains("Agent connection")); +} + +#[test] +fn a_call_waits_for_benchup_to_connect() { + let mut config = config(); + config.app_wait = Duration::from_secs(5); + let hub = Hub::start(config).unwrap(); + let late = hub.clone(); + let app = std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(200)); + echo_app(&late) + }); + let (result, ok) = hub.forward("agent", "get_scene", json!({})); + assert!(ok, "{result}"); + drop(app); +} + +#[test] +fn an_unanswered_call_times_out_and_is_cancelled() { + let mut config = config(); + config.call_timeout = Duration::from_millis(300); + let hub = Hub::start(config).unwrap(); + let mut ws = pair_app(hub.port(), TOKEN, None).unwrap(); + while hub.app().is_none() { + std::thread::sleep(Duration::from_millis(10)); + } + let (result, ok) = hub.forward("agent", "move", json!({})); + assert!(!ok); + assert_eq!(result["error"], "BenchUp did not answer in time"); + let call = read(&mut ws); + assert_eq!(call["type"], "call"); + let cancel = read(&mut ws); + assert_eq!((cancel["type"].clone(), cancel["id"].clone()), (json!("cancel"), call["id"].clone())); +} + +#[test] +fn a_second_bridge_on_the_same_port_reports_it_in_use() { + let hub = Hub::start(config()).unwrap(); + let mut second = config(); + second.port = hub.port(); + assert!(matches!(Hub::start(second), Err(StartError::InUse(_)))); +} + +#[test] +fn nothing_listening_is_reported_as_no_bridge() { + let port = TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); + let client = Client::new(port, TOKEN, "benchup-cli"); + assert_eq!(client.request("get_scene", json!({})).unwrap_err(), ClientError::NoBridge); +} + +#[test] +fn a_second_app_is_refused_while_the_first_stays_connected() { + let hub = Hub::start(config()).unwrap(); + let _app = echo_app(&hub); + let error = pair_app(hub.port(), TOKEN, None).err().unwrap(); + assert_eq!(error["type"], "error"); + assert_eq!((error["code"].clone(), error["message"].clone()), (json!(APP_BUSY.code), json!(APP_BUSY.message))); + // The first app still answers. + let (result, ok) = hub.forward("agent", "get_scene", json!({})); + assert!(ok, "{result}"); +} + +#[test] +fn the_next_app_connects_once_the_first_leaves() { + let hub = Hub::start(config()).unwrap(); + let mut first = pair_app(hub.port(), TOKEN, None).unwrap(); + while hub.app().is_none() { + std::thread::sleep(Duration::from_millis(10)); + } + first.close(None).unwrap(); + let _ = first.flush(); + drop(first); + let started = Instant::now(); + loop { + match pair_app(hub.port(), TOKEN, None) { + Ok(_) => break, + Err(error) => { + assert_eq!((error["code"].clone(), error["message"].clone()), (json!(APP_BUSY.code), json!(APP_BUSY.message))); + assert!(started.elapsed() < Duration::from_secs(5), "the first app was never forgotten"); + std::thread::sleep(Duration::from_millis(50)); + } + } + } +} + +#[test] +fn a_call_too_large_for_the_app_is_refused_without_dropping_it() { + let hub = Hub::start(config()).unwrap(); + let _app = echo_app(&hub); + let (result, ok) = hub.forward("agent", "get_scene", json!({"padding": "x".repeat(MAX_CALL_BYTES)})); + assert!(!ok); + assert!(result["error"].as_str().unwrap().contains("larger than BenchUp accepts"), "{result}"); + let (result, ok) = hub.forward("agent", "get_scene", json!({})); + assert!(ok, "{result}"); +} + +#[test] +fn an_mcp_bridge_forwarding_through_another_takes_the_port_over_when_it_exits() { + // The other bridge (say, the background one `--live` started) has exited. + let port = TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); + let mut takeover = config(); + takeover.port = port; + let mut live = Live::forwarding(Client::new(port, TOKEN, "agent"), takeover); + let (result, ok) = live.forward("get_scene", json!({})); + assert!(!ok); + assert_eq!(result["error"], "BenchUp is not connected to the bridge", "it listens itself and waits for BenchUp"); + let Target::Hub(hub) = &live.target else { panic!("still forwarding to a bridge that is gone") }; + assert_eq!(hub.port(), port); + + // BenchUp reconnects to the same port and calls go through again. + let _app = echo_app(hub); + let (result, ok) = live.forward("get_scene", json!({})); + assert!(ok, "{result}"); +} diff --git a/crates/benchup-cli/src/main.rs b/crates/benchup-cli/src/main.rs index c3578a9..515a6e7 100644 --- a/crates/benchup-cli/src/main.rs +++ b/crates/benchup-cli/src/main.rs @@ -2,6 +2,7 @@ //! //! Every command prints JSON on stdout. Exit status: 0 success, 1 the tool //! reported an error (the file is unchanged), 2 usage or I/O error. +mod bridge; mod launch; mod mcp; mod project; @@ -36,10 +37,20 @@ usage: benchup undo undo the last change benchup redo redo the last undone change benchup mcp serve the tools over MCP (stdio) + benchup bridge [--no-mcp] [--port PORT] [--allow-origin ORIGIN]... + serve the project open in BenchUp over MCP + (stdio); BenchUp connects to it on 127.0.0.1 benchup open [--app EXECUTABLE] open the project in the BenchUp desktop app (or in EXECUTABLE, default $BENCHUP_APP) +live mode: scene, get, call, undo, redo and render take --live instead of +and act on the project open in BenchUp, e.g. `benchup call --live create_board '{..}'`. +Set BENCHUP_AGENT_TOKEN to the pairing token in BenchUp (Settings → Agent connection). +A bridge is started in the background when none is running (BENCHUP_BRIDGE_PORT +changes its port). Each change is one undo step, \"Agent: ...\", in BenchUp. + options: + --live act on the project open in BenchUp, through `benchup bridge` --dry-run (call) report the results without changing the file --brief (call) leave out the object summaries of changing calls --allow-shared edit the local copy of a shared project @@ -150,7 +161,7 @@ fn apply(project: &mut Project, calls: &[(String, Value)]) -> Result Result { - let (value, ok) = project::report(calls, project.run(calls, save)?, batch); + let (value, ok) = benchup_tools::batch::report(calls, project.run(calls, save)?, batch); Ok(Output { value, ok }) } @@ -186,6 +197,18 @@ fn tools(args: Args) -> Result { } } +/// Runs one call in the project open in BenchUp (`--live`). +fn live(name: &str, arguments: Value) -> Result { + let (value, ok) = bridge::cli_request(name, arguments)?; + Ok(Output { value, ok }) +} + +/// The positionals after ``, which `--live` commands leave out. +fn after_file(args: &Args) -> Result<&[String], String> { + let skip = usize::from(!args.flag("--live")); + args.positional.get(skip..).ok_or_else(|| USAGE.to_owned()) +} + fn run(mut argv: Vec) -> Result { if argv.is_empty() || matches!(argv[0].as_str(), "-h" | "--help" | "help") { println!("{USAGE}"); @@ -202,17 +225,19 @@ fn run(mut argv: Vec) -> Result { } "tools" => tools(parse_args(argv, &["--json", "--names"], &[])?), "scene" => { - let args = parse_args(argv, &["--allow-shared"], &[])?; - let filter = match args.positional.len() { - 1 => json!({}), - 2 => parse_object(&args.positional[1], "FILTER")?, + let args = parse_args(argv, &["--allow-shared", "--live"], &[])?; + let filter = match after_file(&args)? { + [] => json!({}), + [filter] => parse_object(filter, "FILTER")?, _ => return Err(USAGE.into()), }; + if args.flag("--live") { + return live("get_scene", filter); + } apply(&mut open(&args)?, &[("get_scene".into(), filter)]) } "render" => { - let args = parse_args(argv, &["--allow-shared"], &["-o", "--output", "--view", "--size"])?; - let path = &args.expect(1)?[0]; + let args = parse_args(argv, &["--allow-shared", "--live"], &["-o", "--output", "--view", "--size"])?; let view = args.value("--view").unwrap_or("iso"); let (width, height) = match args.value("--size") { None => (1000, 750), @@ -221,21 +246,36 @@ fn run(mut argv: Vec) -> Result { .and_then(|(w, h)| Some((w.trim().parse().ok()?, h.trim().parse().ok()?))) .ok_or("--size is WIDTHxHEIGHT in pixels, e.g. 1200x900")?, }; - let project = open(&args)?; - let rendered = benchup_tools::render::render(&project.doc.model, view, width, height)?; - let output = match args.value("-o").or(args.value("--output")) { - Some(output) => std::path::PathBuf::from(output), - None => Path::new(path).with_extension(format!("{view}.png")), + let output = args.value("-o").or(args.value("--output")).map(std::path::PathBuf::from); + let (png, info, output) = if args.flag("--live") { + args.expect(0)?; + let answer = live("render_view", json!({"view": view, "width": width, "height": height}))?; + if !answer.ok { + return Ok(answer); + } + let png = bridge::decode_base64(answer.value["png_base64"].as_str().unwrap_or_default())?; + let output = output.unwrap_or_else(|| format!("benchup-live.{view}.png").into()); + (png, answer.value["info"].clone(), output) + } else { + let path = &args.expect(1)?[0]; + let project = open(&args)?; + let rendered = benchup_tools::render::render(&project.doc.model, view, width, height)?; + let output = output.unwrap_or_else(|| Path::new(path).with_extension(format!("{view}.png"))); + (rendered.png, rendered.info, output) }; - std::fs::write(&output, &rendered.png).map_err(|e| format!("cannot write {}: {e}", output.display()))?; - let mut value = rendered.info; + std::fs::write(&output, &png).map_err(|e| format!("cannot write {}: {e}", output.display()))?; + let mut value = if info.is_object() { info } else { json!({}) }; value["ok"] = json!(true); value["file"] = json!(output.display().to_string()); Ok(ok(value)) } "get" => { - let args = parse_args(argv, &["--allow-shared"], &[])?; - let id: u64 = args.expect(2)?[1].parse().map_err(|_| "the id must be a number".to_owned())?; + let args = parse_args(argv, &["--allow-shared", "--live"], &[])?; + let [id] = after_file(&args)? else { return Err(USAGE.into()) }; + let id: u64 = id.parse().map_err(|_| "the id must be a number".to_owned())?; + if args.flag("--live") { + return live("get_object", json!({"id": id})); + } apply(&mut open(&args)?, &[("get_object".into(), json!({"id": id}))]) } "guide" => { @@ -243,28 +283,43 @@ fn run(mut argv: Vec) -> Result { std::process::exit(0); } "call" => { - let args = parse_args(argv, &["--allow-shared", "--dry-run", "--brief"], &["--batch"])?; - let calls = match (args.value("--batch"), args.positional.as_slice()) { - (Some(source), [_]) => parse_batch(&read_input(source)?)?, - (None, [_, tool]) => vec![(tool.clone(), json!({}))], - (None, [_, tool, arguments]) => { + let args = parse_args(argv, &["--allow-shared", "--dry-run", "--brief", "--live"], &["--batch"])?; + let calls = match (args.value("--batch"), after_file(&args)?) { + (Some(source), []) => parse_batch(&read_input(source)?)?, + (None, [tool]) => vec![(tool.clone(), json!({}))], + (None, [tool, arguments]) => { let text = if arguments == "-" { read_input("-")? } else { arguments.clone() }; vec![(tool.clone(), parse_object(&text, "ARGS")?)] } _ => return Err(USAGE.into()), }; let batch = args.value("--batch").is_some(); - let mut output = run_calls(&mut open(&args)?, &calls, !args.flag("--dry-run"), batch)?; - if args.flag("--dry-run") { + let dry_run = args.flag("--dry-run"); + let mut output = if !args.flag("--live") { + run_calls(&mut open(&args)?, &calls, !dry_run, batch)? + } else if batch || dry_run { + let list: Vec = calls.iter().map(|(tool, arguments)| json!({"tool": tool, "arguments": arguments})).collect(); + // `single`: one call takes "$" text as written and answers + // with its own result, as without --live. + live("run_batch", json!({"calls": list, "dry_run": dry_run, "single": !batch}))? + } else { + let (tool, arguments) = calls.into_iter().next().ok_or(USAGE)?; + live(&tool, arguments)? + }; + if dry_run && output.value.is_object() { output.value["dry_run"] = json!(true); } if args.flag("--brief") { - project::brief(&mut output.value); + benchup_tools::batch::brief(&mut output.value); } Ok(output) } "undo" | "redo" => { - let args = parse_args(argv, &["--allow-shared"], &[])?; + let args = parse_args(argv, &["--allow-shared", "--live"], &[])?; + if args.flag("--live") { + args.expect(0)?; + return live("undo", json!({"redo": command == "redo"})); + } args.expect(1)?; let value = open(&args)?.step_history(command == "redo")?; let ok = value["ok"] == true; @@ -278,6 +333,7 @@ fn run(mut argv: Vec) -> Result { }); Ok(ok(launch::open(path, app.as_deref())?)) } + "bridge" => bridge_command(argv), "mcp" => { let args = parse_args(argv, &["--allow-shared"], &[])?; args.expect(1)?; @@ -290,6 +346,56 @@ fn run(mut argv: Vec) -> Result { } } +/// `benchup bridge`: listens for BenchUp on 127.0.0.1 and serves MCP on +/// stdio, or only listens (`--no-mcp`) for `--live` commands. +fn bridge_command(argv: Vec) -> Result { + let mut no_mcp = false; + let mut config = bridge::Config::new(bridge::token_from_env()?); + config.port = bridge::port_from_env()?; + let mut idle_exit = None; + let mut it = argv.into_iter(); + while let Some(arg) = it.next() { + let mut value = |name: &str| it.next().ok_or(format!("{name} needs a value")); + match arg.as_str() { + "--no-mcp" => no_mcp = true, + "--port" => config.port = value("--port")?.parse().map_err(|_| "--port must be a port number")?, + "--allow-origin" => config.extra_origins.push(value("--allow-origin")?), + "--idle-exit" => { + let seconds: u64 = value("--idle-exit")?.parse().map_err(|_| "--idle-exit is a number of seconds")?; + idle_exit = Some(std::time::Duration::from_secs(seconds.max(1))); + } + _ => return Err(format!("unknown option {arg}\n\n{USAGE}")), + } + } + let target = match bridge::Hub::start(config.clone()) { + Ok(hub) => { + eprintln!("benchup bridge: waiting for BenchUp on ws://127.0.0.1:{}", hub.port()); + if let Some(idle) = idle_exit { + hub.exit_when_idle(idle); + } + bridge::Target::Hub(hub) + } + // Another bridge holds the port: serve MCP through it, and take the + // port over if it exits. + Err(bridge::StartError::InUse(port)) if !no_mcp => { + eprintln!("benchup bridge: another bridge is listening on port {port}; forwarding to it"); + let client = bridge::Client::new(port, config.token.clone(), "agent"); + let mut backend = bridge::Live::forwarding(client, config); + mcp::serve(stdin().lock(), stdout().lock(), &mut backend).map_err(|e| e.to_string())?; + std::process::exit(0); + } + Err(error) => return Err(error.to_string()), + }; + if no_mcp { + loop { + std::thread::park(); + } + } + let mut backend = bridge::Live::new(target); + mcp::serve(stdin().lock(), stdout().lock(), &mut backend).map_err(|e| e.to_string())?; + std::process::exit(0); +} + fn main() -> ExitCode { match run(std::env::args().skip(1).collect()) { Ok(output) => { diff --git a/crates/benchup-cli/src/mcp.rs b/crates/benchup-cli/src/mcp.rs index 6f4c2b9..7a78477 100644 --- a/crates/benchup-cli/src/mcp.rs +++ b/crates/benchup-cli/src/mcp.rs @@ -16,8 +16,15 @@ pub trait Backend { fn batch(&mut self, calls: Vec<(String, Value)>, dry_run: bool) -> (Value, bool); /// Undo or redo the last change to the file (`undo`). fn history(&mut self, redo: bool) -> (Value, bool); - /// A PNG of the model from a view, and what it shows (`render_view`). - fn render(&mut self, view: &str, width: u32, height: u32) -> Result<(Vec, Value), String>; + /// A PNG of the model from a view, as base64, and what it shows + /// (`render_view`). + fn render(&mut self, view: &str, width: u32, height: u32) -> Result<(String, Value), String>; + /// What the agent is told about this server in `initialize`. + fn instructions(&self) -> &'static str { + INSTRUCTIONS + } + /// The MCP client introduced itself (`clientInfo.name`). + fn set_client(&mut self, _client: &str) {} } /// `render_view` is served here rather than by the shared tools: its result @@ -77,7 +84,7 @@ fn render_view(backend: &mut dyn Backend, arguments: &Value) -> Value { match backend.render(view, size("width", 1000), size("height", 750)) { Ok((png, info)) => json!({ "content": [ - {"type": "image", "data": benchup_tools::render::base64(&png), "mimeType": "image/png"}, + {"type": "image", "data": png, "mimeType": "image/png"}, {"type": "text", "text": info.to_string()}, ], "isError": false, @@ -112,12 +119,13 @@ fn handle(message: &Value, backend: &mut dyn Backend, client: &mut String) -> Op let version = SUPPORTED.iter().find(|v| **v == requested).unwrap_or(&SUPPORTED[0]); if let Some(name) = params.pointer("/clientInfo/name").and_then(Value::as_str) { *client = name.to_owned(); + backend.set_client(name); } ok(json!({ "protocolVersion": version, "capabilities": {"tools": {"listChanged": false}}, "serverInfo": {"name": "benchup", "version": env!("CARGO_PKG_VERSION")}, - "instructions": INSTRUCTIONS, + "instructions": backend.instructions(), })) } "ping" => ok(json!({})), diff --git a/crates/benchup-cli/src/project.rs b/crates/benchup-cli/src/project.rs index 50f0d08..b9cd45e 100644 --- a/crates/benchup-cli/src/project.rs +++ b/crates/benchup-cli/src/project.rs @@ -1,6 +1,7 @@ //! A `.bup` file opened for tool calls. Every change is recorded in the //! document history (so BenchUp can undo it) and saved atomically. use benchup_core::{Document, Model}; +use benchup_tools::batch::{Applied, report, resolve}; use benchup_tools::{Call, ToolEnv, execute, files, is_mutating}; use serde_json::{Value, json}; use std::hash::{DefaultHasher, Hash, Hasher}; @@ -31,14 +32,6 @@ pub struct Project { saved: Vec, } -/// Result of applying one or more calls. -pub enum Applied { - /// Every call succeeded; one result per call. - Ok(Vec), - /// Call `index` failed; nothing was changed or saved; `earlier` holds the results of the calls before it. - Failed { index: usize, result: Value, earlier: Vec }, -} - impl Project { pub fn open(path: impl Into, allow_shared: bool) -> Result { let path = path.into(); @@ -189,92 +182,6 @@ fn load(path: &Path, bytes: &[u8], allow_shared: bool) -> Result { - Field(&'a str), - Index(i64), -} - -/// `$N` followed by `.field` and `[index]` steps (N counts calls from 1, -/// negative indexes count from the end), or `None` when `text` is ordinary -/// text such as "$12.50 hardware" or "$5 shelf". -fn parse_reference(text: &str) -> Option<(usize, Vec>)> { - let body = text.strip_prefix('$')?; - let digits = body.find(|c: char| !c.is_ascii_digit()).unwrap_or(body.len()); - let call = body[..digits].parse().ok()?; - let mut rest = &body[digits..]; - let mut steps = Vec::new(); - while !rest.is_empty() { - if let Some(tail) = rest.strip_prefix('.') { - let end = tail.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')).unwrap_or(tail.len()); - let field = &tail[..end]; - if !field.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_') { - return None; - } - steps.push(Step::Field(field)); - rest = &tail[end..]; - } else if let Some(tail) = rest.strip_prefix('[') { - let end = tail.find(']')?; - steps.push(Step::Index(tail[..end].parse().ok()?)); - rest = &tail[end + 1..]; - } else { - return None; - } - } - Some((call, steps)) -} - -/// Replaces every `$N.path` reference with that value from an earlier result. -fn resolve(value: &Value, results: &[Value]) -> Result { - match value { - Value::String(text) => match parse_reference(text) { - Some((call, steps)) => reference(text, call, &steps, results), - None => Ok(value.clone()), - }, - // A reference to a list of ids inside a list is spliced in, so - // "ids": ["$1.created", "$2.created"] joins both lists. Other lists, - // such as a point, stay one item. - Value::Array(items) => { - let mut out = Vec::with_capacity(items.len()); - for item in items { - let is_reference = matches!(item, Value::String(t) if parse_reference(t).is_some()); - match resolve(item, results)? { - Value::Array(inner) if is_reference && inner.iter().all(|v| v.is_u64() || v.is_i64()) => { - out.extend(inner) - } - other => out.push(other), - } - } - Ok(Value::Array(out)) - } - Value::Object(map) => map - .iter() - .map(|(k, v)| resolve(v, results).map(|v| (k.clone(), v))) - .collect::>() - .map(Value::Object), - other => Ok(other.clone()), - } -} - -fn reference(text: &str, call: usize, steps: &[Step], results: &[Value]) -> Result { - let mut current = call - .checked_sub(1) - .and_then(|i| results.get(i)) - .ok_or(format!("{text} refers to call {call}, but only {} earlier calls ran", results.len()))?; - for step in steps { - let next = match step { - Step::Field(field) => current.get(field), - Step::Index(index) => { - let items = current.as_array().ok_or(format!("{text}: not a list"))?; - let index = if *index < 0 { items.len() as i64 + index } else { *index }; - usize::try_from(index).ok().and_then(|i| items.get(i)) - } - }; - current = next.ok_or(format!("{text} does not exist in the result of call {call}"))?; - } - Ok(current.clone()) -} - impl Project { /// Undo (`redo == false`) or redo the last history entry and save. pub fn step_history(&mut self, redo: bool) -> Result { @@ -297,53 +204,6 @@ impl Project { } } -/// The JSON answer for applied calls, as the CLI prints it and MCP returns -/// it: one call's result, or `{"ok", "results"}` for a batch. A failed batch -/// leads with the error and hint of the call that failed; the results of the -/// calls before it are kept brief (ids and bounds, no object summaries). -/// `batch` is true for `--batch` and `run_batch`, which always answer with -/// `results`, even for one call. -pub fn report(calls: &[(String, Value)], applied: Applied, batch: bool) -> (Value, bool) { - match applied { - Applied::Ok(mut results) if !batch => (results.pop().unwrap_or(Value::Null), true), - Applied::Ok(results) => (json!({"ok": true, "results": results}), true), - Applied::Failed { result, .. } if !batch => (result, false), - Applied::Failed { index, result, mut earlier } => { - earlier.iter_mut().for_each(brief); - let mut out = json!({ - "ok": false, - "failed_call": index + 1, - "tool": calls[index].0, - "error": result.get("error").cloned().unwrap_or(Value::Null), - "result": result, - "earlier_results": earlier, - "note": "nothing was changed: earlier_results shows what the calls before it returned, then everything was rolled back; fix this call and run the whole batch again", - }); - if let Some(hint) = out["result"].get("hint").cloned() { - out["hint"] = hint; - } - (out, false) - } - } -} - -/// Drops the object summaries that changing tools attach to their results, -/// keeping ids, bounds, errors and the answers of reading tools. -pub fn brief(value: &mut Value) { - if let Some(map) = value.as_object_mut() { - if map.contains_key("created") { - map.remove("objects"); - } - for key in ["results", "result", "earlier_results"] { - match map.get_mut(key) { - Some(Value::Array(items)) => items.iter_mut().for_each(brief), - Some(item @ Value::Object(_)) => brief(item), - _ => {} - } - } - } -} - /// MCP: each `tools/call` is applied (and saved) on its own, on the file as /// it is now on disk. impl crate::mcp::Backend for Project { @@ -387,10 +247,10 @@ impl crate::mcp::Backend for Project { } } - fn render(&mut self, view: &str, width: u32, height: u32) -> Result<(Vec, Value), String> { + fn render(&mut self, view: &str, width: u32, height: u32) -> Result<(String, Value), String> { self.refresh()?; let rendered = benchup_tools::render::render(&self.doc.model, view, width, height)?; - Ok((rendered.png, rendered.info)) + Ok((benchup_tools::render::base64(&rendered.png), rendered.info)) } } @@ -435,20 +295,6 @@ mod tests { assert_eq!(scene["project"]["name"], "Saved elsewhere"); } - #[test] - fn references_are_strict_and_only_lists_of_ids_are_spliced() { - let results = [json!({"created": [7, 8], "center_mm": [1.5, 2.0, 3.0]})]; - let text = json!({"text": "$12.50 hardware", "name": "$5 shelf", "note": "$1.", "cost": "$3.99"}); - assert_eq!(resolve(&text, &results).unwrap(), text); - let points = json!({"points_mm": ["$1.center_mm", [0, 0, 0]], "ids": ["$1.created", 9]}); - assert_eq!( - resolve(&points, &results).unwrap(), - json!({"points_mm": [[1.5, 2.0, 3.0], [0, 0, 0]], "ids": [7, 8, 9]}) - ); - assert_eq!(resolve(&json!("$1.created[-1]"), &results).unwrap(), json!(8)); - assert!(resolve(&json!("$2.created"), &results).is_err(), "a reference to a missing call is an error"); - } - #[test] fn a_single_call_takes_dollar_text_as_written() { let dir = tempfile::tempdir().unwrap(); diff --git a/crates/benchup-cli/tests/live.rs b/crates/benchup-cli/tests/live.rs new file mode 100644 index 0000000..f7b6e49 --- /dev/null +++ b/crates/benchup-cli/tests/live.rs @@ -0,0 +1,138 @@ +//! `benchup bridge` and `--live` commands against a stand-in for BenchUp. +use benchup_tools::pairing; +use serde_json::{Value, json}; +use std::net::{TcpListener, TcpStream}; +use std::process::{Child, Command, Output, Stdio}; +use std::time::{Duration, Instant}; +use tungstenite::{Message, WebSocket}; + +const TOKEN: &str = "0123456789abcdef-live-test"; + +struct Bridge(Child); +impl Drop for Bridge { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +fn free_port() -> u16 { + TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port() +} + +fn benchup(port: u16, args: &[&str]) -> Command { + let mut command = Command::new(env!("CARGO_BIN_EXE_benchup")); + command.args(args).env("BENCHUP_AGENT_TOKEN", TOKEN).env("BENCHUP_BRIDGE_PORT", port.to_string()); + command +} + +fn run(port: u16, args: &[&str]) -> (Value, Output) { + let output = benchup(port, args).stdin(Stdio::null()).output().unwrap(); + let value = serde_json::from_slice(&output.stdout) + .unwrap_or_else(|_| panic!("not JSON: {}", String::from_utf8_lossy(&output.stderr))); + (value, output) +} + +fn read(ws: &mut WebSocket) -> Value { + loop { + if let Message::Text(text) = ws.read().unwrap() { + return serde_json::from_str(&text).unwrap(); + } + } +} + +/// Connects like BenchUp does, retrying until the bridge listens. +fn connect_app(port: u16) -> WebSocket { + let started = Instant::now(); + let stream = loop { + match TcpStream::connect(("127.0.0.1", port)) { + Ok(stream) => break stream, + Err(_) if started.elapsed() < Duration::from_secs(10) => std::thread::sleep(Duration::from_millis(50)), + Err(e) => panic!("the bridge did not start: {e}"), + } + }; + let (mut ws, _) = tungstenite::client::client(format!("ws://127.0.0.1:{port}/"), stream).unwrap(); + ws.send(Message::text(json!({"type": "authenticate", "ticket": "", "connection": "benchup-app-test"}).to_string())).unwrap(); + let challenge = read(&mut ws); + let bridge_nonce = challenge["nonce"].as_str().unwrap().to_owned(); + let nonce = "cd".repeat(32); + let proof = pairing::app_proof(TOKEN, &bridge_nonce, &nonce); + ws.send(Message::text(json!({"type": "prove", "nonce": nonce, "proof": proof}).to_string())).unwrap(); + assert_eq!(read(&mut ws)["type"], "ready"); + ws +} + +/// Answers calls the way the app does for the few tools used here. +fn serve_app(mut ws: WebSocket) -> std::thread::JoinHandle> { + std::thread::spawn(move || { + let mut seen = Vec::new(); + while let Ok(message) = ws.read() { + let Message::Text(text) = message else { continue }; + let call: Value = serde_json::from_str(&text).unwrap(); + if call["type"] != "call" { + continue; + } + let result = match call["name"].as_str().unwrap() { + "get_scene" => json!({"ok": true, "object_count": 2, "client": call["client"]}), + // `single`: one call answers with its own result. + "run_batch" if call["arguments"]["single"] == true => json!({"ok": true, "created": [7], "dry_run": true}), + "run_batch" => json!({"ok": true, "results": [{"ok": true, "created": [7]}], "dry_run": call["arguments"]["dry_run"]}), + "render_view" => json!({"ok": true, "png_base64": "iVBORw==", "info": {"view": call["arguments"]["view"]}}), + _ => json!({"ok": false, "error": "denied by the designer"}), + }; + let ok = result["ok"] == true; + ws.send(Message::text(json!({"type": "result", "id": call["id"], "ok": ok, "result": result}).to_string())).unwrap(); + seen.push(call); + if seen.len() == 4 { + break; + } + } + seen + }) +} + +#[test] +fn live_commands_run_in_the_app_through_the_bridge() { + let port = free_port(); + let _bridge = Bridge(benchup(port, &["bridge", "--no-mcp"]).stdin(Stdio::null()).stderr(Stdio::null()).spawn().unwrap()); + let app = serve_app(connect_app(port)); + + let (scene, output) = run(port, &["scene", "--live"]); + assert!(output.status.success(), "{scene}"); + assert_eq!(scene["object_count"], 2); + assert_eq!(scene["client"], "benchup-cli"); + + // A dry run of one call answers with that call's result. + let (result, _) = run(port, &["call", "--live", "--dry-run", "create_board", r#"{"name":"$1"}"#]); + assert_eq!(result["created"], json!([7])); + assert_eq!(result["dry_run"], true); + assert!(result.get("result").is_none(), "{result}"); + + let dir = tempfile::tempdir().unwrap(); + let png = dir.path().join("view.png"); + let (render, _) = run(port, &["render", "--live", "--view", "front", "-o", png.to_str().unwrap()]); + assert_eq!(render["view"], "front"); + assert_eq!(std::fs::read(&png).unwrap(), b"\x89PNG"); + + let (refused, output) = run(port, &["call", "--live", "erase", r#"{"ids":[1]}"#]); + assert_eq!(output.status.code(), Some(1), "a refused call is a tool error"); + assert_eq!(refused["error"], "denied by the designer"); + + let calls = app.join().unwrap(); + assert_eq!(calls[1]["name"], "run_batch"); + assert_eq!(calls[1]["arguments"]["calls"][0]["tool"], "create_board"); + // One call is run as written: "$1" is a name, not a reference. + assert_eq!(calls[1]["arguments"]["single"], true); + assert_eq!(calls[1]["arguments"]["calls"][0]["arguments"]["name"], "$1"); +} + +#[test] +fn live_commands_need_the_pairing_token() { + let output = Command::new(env!("CARGO_BIN_EXE_benchup")) + .args(["scene", "--live"]) + .env_remove("BENCHUP_AGENT_TOKEN") + .output() + .unwrap(); + assert_eq!(output.status.code(), Some(2)); + assert!(String::from_utf8_lossy(&output.stderr).contains("BENCHUP_AGENT_TOKEN")); +} diff --git a/crates/benchup-tools/Cargo.toml b/crates/benchup-tools/Cargo.toml index 36d2589..30c50f5 100644 --- a/crates/benchup-tools/Cargo.toml +++ b/crates/benchup-tools/Cargo.toml @@ -11,6 +11,7 @@ benchup-core = { path = "../benchup-core" } serde.workspace = true serde_json.workspace = true flate2 = { version = "1", default-features = false, features = ["rust_backend"] } +sha2 = "0.11" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tempfile = "3" diff --git a/crates/benchup-tools/src/batch.rs b/crates/benchup-tools/src/batch.rs new file mode 100644 index 0000000..5f81d15 --- /dev/null +++ b/crates/benchup-tools/src/batch.rs @@ -0,0 +1,167 @@ +//! Several tool calls as one step: `$N` references to earlier results and +//! the JSON report of the whole run. Shared by `benchup call --batch`, MCP +//! `run_batch` on a file and the open app (through `benchup bridge`), so a +//! batch reads and answers the same way everywhere. +use serde_json::{Value, json}; + +/// Result of applying one or more calls. +pub enum Applied { + /// Every call succeeded; one result per call. + Ok(Vec), + /// Call `index` failed; nothing was changed or saved; `earlier` holds the results of the calls before it. + Failed { index: usize, result: Value, earlier: Vec }, +} + +/// One step of a reference after `$N`. +pub enum Step<'a> { + Field(&'a str), + Index(i64), +} + +/// `$N` followed by `.field` and `[index]` steps (N counts calls from 1, +/// negative indexes count from the end), or `None` when `text` is ordinary +/// text such as "$12.50 hardware" or "$5 shelf". +pub fn parse_reference(text: &str) -> Option<(usize, Vec>)> { + let body = text.strip_prefix('$')?; + let digits = body.find(|c: char| !c.is_ascii_digit()).unwrap_or(body.len()); + let call = body[..digits].parse().ok()?; + let mut rest = &body[digits..]; + let mut steps = Vec::new(); + while !rest.is_empty() { + if let Some(tail) = rest.strip_prefix('.') { + let end = tail.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')).unwrap_or(tail.len()); + let field = &tail[..end]; + if !field.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_') { + return None; + } + steps.push(Step::Field(field)); + rest = &tail[end..]; + } else if let Some(tail) = rest.strip_prefix('[') { + let end = tail.find(']')?; + steps.push(Step::Index(tail[..end].parse().ok()?)); + rest = &tail[end + 1..]; + } else { + return None; + } + } + Some((call, steps)) +} + +/// Replaces every `$N.path` reference with that value from an earlier result. +pub fn resolve(value: &Value, results: &[Value]) -> Result { + match value { + Value::String(text) => match parse_reference(text) { + Some((call, steps)) => reference(text, call, &steps, results), + None => Ok(value.clone()), + }, + // A reference to a list of ids inside a list is spliced in, so + // "ids": ["$1.created", "$2.created"] joins both lists. Other lists, + // such as a point, stay one item. + Value::Array(items) => { + let mut out = Vec::with_capacity(items.len()); + for item in items { + let is_reference = matches!(item, Value::String(t) if parse_reference(t).is_some()); + match resolve(item, results)? { + Value::Array(inner) if is_reference && inner.iter().all(|v| v.is_u64() || v.is_i64()) => { + out.extend(inner) + } + other => out.push(other), + } + } + Ok(Value::Array(out)) + } + Value::Object(map) => map + .iter() + .map(|(k, v)| resolve(v, results).map(|v| (k.clone(), v))) + .collect::>() + .map(Value::Object), + other => Ok(other.clone()), + } +} + +fn reference(text: &str, call: usize, steps: &[Step], results: &[Value]) -> Result { + let mut current = call + .checked_sub(1) + .and_then(|i| results.get(i)) + .ok_or(format!("{text} refers to call {call}, but only {} earlier calls ran", results.len()))?; + for step in steps { + let next = match step { + Step::Field(field) => current.get(field), + Step::Index(index) => { + let items = current.as_array().ok_or(format!("{text}: not a list"))?; + let index = if *index < 0 { items.len() as i64 + index } else { *index }; + usize::try_from(index).ok().and_then(|i| items.get(i)) + } + }; + current = next.ok_or(format!("{text} does not exist in the result of call {call}"))?; + } + Ok(current.clone()) +} + +/// The JSON answer for applied calls, as the CLI prints it and MCP returns +/// it: one call's result, or `{"ok", "results"}` for a batch. A failed batch +/// leads with the error and hint of the call that failed; the results of the +/// calls before it are kept brief (ids and bounds, no object summaries). +/// `batch` is true for `--batch` and `run_batch`, which always answer with +/// `results`, even for one call. +pub fn report(calls: &[(String, Value)], applied: Applied, batch: bool) -> (Value, bool) { + match applied { + Applied::Ok(mut results) if !batch => (results.pop().unwrap_or(Value::Null), true), + Applied::Ok(results) => (json!({"ok": true, "results": results}), true), + Applied::Failed { result, .. } if !batch => (result, false), + Applied::Failed { index, result, mut earlier } => { + earlier.iter_mut().for_each(brief); + let mut out = json!({ + "ok": false, + "failed_call": index + 1, + "tool": calls[index].0, + "error": result.get("error").cloned().unwrap_or(Value::Null), + "result": result, + "earlier_results": earlier, + "note": "nothing was changed: earlier_results shows what the calls before it returned, then everything was rolled back; fix this call and run the whole batch again", + }); + if let Some(hint) = out["result"].get("hint").cloned() { + out["hint"] = hint; + } + (out, false) + } + } +} + +/// Drops the object summaries that changing tools attach to their results, +/// keeping ids, bounds, errors and the answers of reading tools. +pub fn brief(value: &mut Value) { + if let Some(map) = value.as_object_mut() { + if map.contains_key("created") { + map.remove("objects"); + } + for key in ["results", "result", "earlier_results"] { + match map.get_mut(key) { + Some(Value::Array(items)) => items.iter_mut().for_each(brief), + Some(item @ Value::Object(_)) => brief(item), + _ => {} + } + } + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn references_are_strict_and_only_lists_of_ids_are_spliced() { + let results = [json!({"created": [7, 8], "center_mm": [1.5, 2.0, 3.0]})]; + let text = json!({"text": "$12.50 hardware", "name": "$5 shelf", "note": "$1.", "cost": "$3.99"}); + assert_eq!(resolve(&text, &results).unwrap(), text); + let points = json!({"points_mm": ["$1.center_mm", [0, 0, 0]], "ids": ["$1.created", 9]}); + assert_eq!( + resolve(&points, &results).unwrap(), + json!({"points_mm": [[1.5, 2.0, 3.0], [0, 0, 0]], "ids": [7, 8, 9]}) + ); + assert_eq!(resolve(&json!("$1.created[-1]"), &results).unwrap(), json!(8)); + assert!(resolve(&json!("$2.created"), &results).is_err(), "a reference to a missing call is an error"); + } + +} diff --git a/crates/benchup-tools/src/lib.rs b/crates/benchup-tools/src/lib.rs index 39b6f1b..ab10f84 100644 --- a/crates/benchup-tools/src/lib.rs +++ b/crates/benchup-tools/src/lib.rs @@ -10,10 +10,15 @@ //! detail) for an agent to read. //! * [`files`] reads and writes `.bup` project files (gzip-compressed JSON). //! * [`render`] draws a PNG preview of a standard view without a GPU. +//! * [`batch`] runs several calls as one step, with `$N` references to +//! earlier results, and reports them the same way everywhere. +//! * [`pairing`] is the challenge-response handshake of `benchup bridge`. //! //! The crate has no network or UI dependencies and builds for `wasm32`. +pub mod batch; pub mod files; pub mod ops; +pub mod pairing; pub mod render; pub mod review; pub mod scene; diff --git a/crates/benchup-tools/src/pairing.rs b/crates/benchup-tools/src/pairing.rs new file mode 100644 index 0000000..c991576 --- /dev/null +++ b/crates/benchup-tools/src/pairing.rs @@ -0,0 +1,157 @@ +//! Challenge-response pairing with the `benchup bridge` (protocol 2). +//! +//! The bridge listens on 127.0.0.1; BenchUp (the app) and command-line +//! clients connect to it. The raw pairing token is never sent: each side +//! proves it knows the token with an HMAC-SHA256 over both nonces, and the +//! proofs are separated by role so one side's proof cannot be replayed as +//! another's. +use sha2::{Digest, Sha256}; + +pub const PROTOCOL: u32 = 2; + +/// The port the bridge listens on and the app connects to by default. +pub const DEFAULT_PORT: u16 = 47615; +/// The longest client name shown to the designer. +pub const MAX_CLIENT_CHARS: usize = 64; +/// The largest message the app accepts from the bridge; the bridge refuses +/// larger calls itself rather than forwarding them and losing the connection. +pub const MAX_APP_MESSAGE_BYTES: usize = 16 * 1024 * 1024; +/// The largest message the bridge accepts from a paired peer (scene dumps, +/// rendered views). The app answers with an error rather than send a larger +/// result, which would make the bridge drop the connection. +pub const MAX_BRIDGE_MESSAGE_BYTES: usize = 32 * 1024 * 1024; + +/// Machine-readable `code`s of the bridge's `error` frames, so the app can +/// show the reason in the designer's language. +pub mod refusal { + pub const UNKNOWN_ROLE: &str = "unknown_role"; + pub const TOKEN_MISMATCH: &str = "token_mismatch"; + pub const APP_BUSY: &str = "app_busy"; + pub const UNEXPECTED: &str = "unexpected"; +} + +const BLOCK: usize = 64; + +/// HMAC-SHA256 (RFC 2104). +pub fn hmac_sha256(key: &[u8], message: &[u8]) -> [u8; 32] { + let mut k = [0u8; BLOCK]; + if key.len() > BLOCK { + k[..32].copy_from_slice(&Sha256::digest(key)); + } else { + k[..key.len()].copy_from_slice(key); + } + let mut ipad = [0x36u8; BLOCK]; + let mut opad = [0x5cu8; BLOCK]; + for i in 0..BLOCK { + ipad[i] ^= k[i]; + opad[i] ^= k[i]; + } + let mut inner = Sha256::new(); + inner.update(ipad); + inner.update(message); + let inner = inner.finalize(); + let mut outer = Sha256::new(); + outer.update(opad); + outer.update(inner); + outer.finalize().into() +} + +/// Lowercase hex encoding. +pub fn to_hex(bytes: &[u8]) -> String { + const HEX: &[u8; 16] = b"0123456789abcdef"; + let mut s = String::with_capacity(bytes.len() * 2); + for b in bytes { + s.push(HEX[(b >> 4) as usize] as char); + s.push(HEX[(b & 15) as usize] as char); + } + s +} + +fn proof(role: &str, token: &str, bridge_nonce: &str, app_nonce: &str) -> String { + let message = format!("benchup-agent-link/{role}\n{bridge_nonce}\n{app_nonce}"); + to_hex(&hmac_sha256(token.as_bytes(), message.as_bytes())) +} + +/// Proof the app sends in its `prove` frame. +pub fn app_proof(token: &str, bridge_nonce: &str, app_nonce: &str) -> String { + proof("app", token, bridge_nonce, app_nonce) +} + +/// Proof a command-line client (`benchup call --live`, a second MCP server) +/// sends in its `prove` frame. +pub fn client_proof(token: &str, bridge_nonce: &str, client_nonce: &str) -> String { + proof("client", token, bridge_nonce, client_nonce) +} + +/// Proof the bridge sends in its `ready` frame. +pub fn bridge_proof(token: &str, bridge_nonce: &str, app_nonce: &str) -> String { + proof("bridge", token, bridge_nonce, app_nonce) +} + +/// Constant-time comparison of two proofs. +pub fn proofs_match(a: &str, b: &str) -> bool { + let (a, b) = (a.as_bytes(), b.as_bytes()); + // Proof lengths are public (always 64 hex characters), so comparing + // them directly leaks nothing. + if a.len() != b.len() { + return false; + } + let mut diff = 0u8; + for (x, y) in a.iter().zip(b) { + diff |= x ^ y; + } + std::hint::black_box(diff) == 0 +} + +/// A nonce is exactly 64 lowercase hex characters. +pub fn valid_nonce(nonce: &str) -> bool { + nonce.len() == 64 && nonce.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rfc4231_vectors() { + assert_eq!( + to_hex(&hmac_sha256(&[0x0b; 20], b"Hi There")), + "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" + ); + assert_eq!( + to_hex(&hmac_sha256(b"Jefe", b"what do ya want for nothing?")), + "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" + ); + assert_eq!( + to_hex(&hmac_sha256( + &[0xaa; 131], + b"Test Using Larger Than Block-Size Key - Hash Key First" + )), + "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54" + ); + } + + #[test] + fn proofs_are_role_separated() { + let n = "a".repeat(64); + let m = "b".repeat(64); + assert_ne!(app_proof("t", &n, &m), bridge_proof("t", &n, &m)); + assert_ne!(app_proof("t", &n, &m), client_proof("t", &n, &m)); + assert!(proofs_match(&app_proof("t", &n, &m), &app_proof("t", &n, &m))); + assert!(!proofs_match(&app_proof("t", &n, &m), &app_proof("u", &n, &m))); + assert!(!proofs_match("ab", "abc")); + // Lengths that differ by a multiple of 256, padded with NULs. + let proof = app_proof("t", &n, &m); + let padded = format!("{proof}{}", "\0".repeat(256)); + assert!(!proofs_match(&padded, &proof)); + assert!(!proofs_match(&proof, &padded)); + } + + #[test] + fn nonce_validation() { + assert!(valid_nonce(&"0f".repeat(32))); + assert!(!valid_nonce(&"0F".repeat(32))); + assert!(!valid_nonce("abc")); + assert!(!valid_nonce(&"g".repeat(64))); + } +} diff --git a/docs/agent-guide.md b/docs/agent-guide.md index b6d8c96..a9e1adb 100644 --- a/docs/agent-guide.md +++ b/docs/agent-guide.md @@ -2,7 +2,7 @@ [Documentation](README.md) · [Command line](cli.md) -This guide is written for AI agents that edit BenchUp projects through the [`benchup` command line](cli.md) or its MCP server (see [Over MCP](#over-mcp) for the differences). Agents read it with `benchup guide` (over MCP, the `get_guide` tool), and it always matches the installed version. +This guide is written for AI agents that edit BenchUp projects through the [`benchup` command line](cli.md) or its MCP server (see [Over MCP](#over-mcp) for the differences), either in a `.bup` file or in the project open in BenchUp (see [In the open app](#in-the-open-app)). Agents read it with `benchup guide` (over MCP, the `get_guide` tool), and it always matches the installed version. ## Conventions @@ -21,7 +21,7 @@ This guide is written for AI agents that edit BenchUp projects through the [`ben - `get_object` on a joint shows its settings and status. - `get_cut_list` lists the boards to cut; `get_estimate` gives the time to build them. - `benchup render file.bup --view front` (over MCP, `render_view`) draws the model as a PNG, joints and profiles cut, with dimensions and text notes (in capitals, without accents). Its output lists dimensions whose labels overlap and the notes with their position. It shows the whole model; details of a few millimetres need `--size 2400x1800`. -5. With the CLI, `benchup open file.bup` shows the result in the desktop app. The CLI edits the file, not the open window, but BenchUp versions after 0.7.1 reload the file by themselves about a second after each change (with older versions, run `open` again). If the project has unsaved changes in the app, BenchUp asks the user first: tell them to choose "Reload from disk" to see your edit, because keeping their changes and saving overwrites it. +5. In the open app (`--live` or `benchup bridge`), the designer sees each change as you make it. With a file, `benchup open file.bup` shows the result in the desktop app. The CLI edits the file, not the open window, but BenchUp versions after 0.7.1 reload the file by themselves about a second after each change (with older versions, run `open` again). If the project has unsaved changes in the app, BenchUp asks the user first: tell them to choose "Reload from disk" to see your edit, because keeping their changes and saving overwrites it. ### Batches, results and dry runs @@ -40,6 +40,18 @@ The tools are the same. What the CLI does with flags, MCP does with three extra Each other call is saved on its own, so prefer `run_batch` for any change of more than one call. The schemas are in the server's tool list; there is no `--brief`. +### In the open app + +`benchup bridge` (MCP) and the CLI's `--live` commands (`benchup call --live …`, `benchup scene --live`) work on the project open in BenchUp, while the designer watches. The tools and batches are the same, with these differences: + +- Each changing call, and each batch, is one undo step labelled `Agent: …` in BenchUp's history. Prefer one batch per change, so the designer can undo it in one step. +- Every call starts in the scope the designer is in. Put `enter_object` and the calls that need it in one batch, ending with `exit_to_root`. +- `get_selection` is what the designer has selected: when they say "this" or "these", read it first. What you create or change is selected afterwards. +- If the designer is in the middle of an operation or has taken control, BenchUp asks them to grant access and your call waits. An error saying they **denied access** or **took control** means: stop, tell them what you want to change, and wait for their go-ahead before retrying. Reading calls still work. +- Every result has `document.name`, the open project. If it changes, or a call says the open project changed, read the scene again before going on. +- Shared projects keep their rules: in a view-only project, or for objects someone else is editing, changing calls fail with the reason. After `select`, an edit waits until the server confirms the selection. +- An answer too large for the bridge comes back as an error. If it says `applied: true`, the change was made: do not repeat it; ask for less (filter `get_scene`, render a smaller view). + ## Groups, components and scope - Every board is a group. Editing tools (`move`, `resize_board`, `rename`, `tag`, `erase`, `add_joint`, `push_pull`…) reach parts inside groups and components by themselves when all the ids in a call share one group. Ids from different groups need separate calls. diff --git a/docs/cli.md b/docs/cli.md index f41835a..fa14ed2 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -2,7 +2,7 @@ [Documentation](README.md) · [Agent guide](agent-guide.md) · [Developer README](../README.md) -`benchup` reads and edits BenchUp `.bup` projects from a shell or an AI coding agent (Claude Code, Codex, Cursor…), without opening the app. It reads every project format the app saves, including files with a viewport preview. Lengths are millimetres and Z is up. +`benchup` reads and edits BenchUp `.bup` projects from a shell or an AI coding agent (Claude Code, Codex, Cursor…), without opening the app. With [`benchup bridge`](#live-editing-in-the-open-app), it also edits the project open in BenchUp, on the desktop and on the web. It reads every project format the app saves, including files with a viewport preview. Lengths are millimetres and Z is up. ## Installation @@ -41,6 +41,11 @@ benchup mcp shelf.bup # the same tools over MCP (stdio), p benchup open shelf.bup # open it in the BenchUp desktop app benchup open shelf.bup --app target/debug/benchup-app # or in a specific executable + +benchup bridge # MCP (stdio) for the project open in BenchUp +benchup scene --live # --live: the same commands on the open project +benchup call --live create_board '{"name":"Side","origin_mm":[0,0,0],"size_mm":[18,300,900]}' +benchup call --live --batch ops.jsonl # one undo step in BenchUp, all-or-nothing ``` A batch file has one call per line. Blank lines and lines starting with `#` are skipped: @@ -69,7 +74,42 @@ In a batch, `"$N.field"` is a value from the result of call N (counting from 1): - **Views and cameras:** the CLI keeps the camera of each view saved in the file, so editing a project does not move the user's views. It cannot list or change saved views yet, and `benchup render` draws the standard views (iso, front, back, left, right, top, bottom), not saved cameras; use BenchUp's [views](controls.md#views-and-cameras) and Tools → Screenshot for those. - **Shared projects:** local copies of shared projects are refused unless you pass `--allow-shared`, because edits would diverge from the server. - **Opening in the app:** `benchup open` hands the file to the installed desktop app (by bundle id on macOS, through the `.bup` file association on Linux and Windows). On macOS, when BenchUp is not running yet, it starts the app with the file as a launch argument. `--app `, or the `BENCHUP_APP` environment variable, starts that executable instead, which is handy for a development build. The app must be installed separately: [download it](https://github.com/BenchUp3D/editor/releases/latest). -- **Not live, but reloaded:** the CLI edits the file, not a running BenchUp window. BenchUp versions after 0.7.1 that have the project open reload it about a second after the CLI saves, keeping the camera. If the project has unsaved changes in the app, BenchUp asks whether to keep them or reload from disk; keeping them and saving overwrites the CLI's edits. With BenchUp 0.7.1 or older, run `benchup open` again after a change to reload it, and save or discard your app edits before editing with the CLI. Live editing through a local bridge is planned ([#13](https://github.com/BenchUp3D/editor/issues/13)). +- **Files and reloading:** without `--live`, the CLI edits the file, not a running BenchUp window. BenchUp versions after 0.7.1 that have the project open reload it about a second after the CLI saves, keeping the camera. If the project has unsaved changes in the app, BenchUp asks whether to keep them or reload from disk; keeping them and saving overwrites the CLI's edits. With BenchUp 0.7.1 or older, run `benchup open` again after a change to reload it, and save or discard your app edits before editing with the CLI. To edit the open project directly, use [live editing](#live-editing-in-the-open-app). + +## Live editing in the open app + +`benchup bridge` lets an agent edit the project open in BenchUp, on the desktop or in the browser, while you watch. There is no server: the bridge runs on your computer and listens on `127.0.0.1` only, and BenchUp connects out to it (a browser tab cannot listen on a port). + +``` +agent ──MCP (stdio) or benchup … --live──▶ benchup bridge (127.0.0.1:47615) ◀──WebSocket── BenchUp +``` + +1. In BenchUp, open **Settings → Agent connection** and turn on **Allow agent connection**. It is off by default; the setting is remembered. +2. Copy the pairing token shown there (or one of the setup snippets) and give it to the agent as `BENCHUP_AGENT_TOKEN`. The token stays on that device; **New token** replaces it and disconnects agents that use the old one. +3. Connect the agent: + + ```sh + # MCP clients such as Claude Code + claude mcp add benchup -e BENCHUP_AGENT_TOKEN= -- benchup bridge + + # agents that run commands + export BENCHUP_AGENT_TOKEN= + benchup scene --live + ``` + + `scene`, `get`, `call` (with `--batch` and `--dry-run`), `undo`, `redo` and `render` take `--live` instead of a file. When no bridge is running, the first `--live` command starts one in the background, which exits after 30 minutes without calls. A `benchup bridge` that finds another bridge on its port forwards MCP calls through it, and listens on the port itself once that bridge exits. `BENCHUP_BRIDGE_PORT`, or `benchup bridge --port`, changes the port; set the same port in BenchUp. + +How it behaves: + +- **Undo:** each changing call is one step in BenchUp's history, labelled `Agent: `; a batch (`--batch` or MCP `run_batch`) is one step. The designer can undo it as usual, and `benchup undo --live` steps through the same history. +- **One BenchUp at a time:** the bridge serves the first BenchUp window or tab that connects. Another one with the same token is refused (its Agent connection shows the reason and keeps retrying) until the first closes or turns the connection off. +- **Who is in control:** reading calls always run, even while changes queued before them wait, and BenchUp answers calls while its window is minimized or its tab is in the background. A changing call, or one that changes the selection, runs at once unless the designer is in the middle of an operation (drawing, pushing, typing a value…) or has taken control. Then BenchUp asks them to **Grant access**, which cancels their operation and lets the agent continue, or **Deny access**, which refuses the waiting changes with an error the agent can read. While the agent is editing, the status bar shows **Take control**: after that, its changes wait for a grant until the designer chooses **Let the agent edit**. +- **Shared projects:** the agent is treated like the designer. View-only projects stay read-only, edits need the same edit lease, and its steps are attributed to the designer. An edit made while the server is still confirming a new selection waits for the confirmation instead of failing. +- **Scope and selection:** every call starts in the scope the designer is in, so put `enter_object` and the calls that need it in one batch. The agent sees the designer's selection (`get_selection`), so the designer can say "change this". +- **Project switches:** every result names the open project. A call that was waiting when another project was opened is refused, so the agent can look again with `get_scene`. A collaborator's new revision of the same shared project does not refuse waiting calls. +- **Timeouts and size:** a call waits up to 15 seconds for BenchUp to connect and up to 120 seconds for its answer, then fails with a hint. A call larger than 16 MB is refused by the bridge; split it into smaller batches. An answer larger than 32 MB comes back as an error (with `applied: true` when the call still changed the model); filter `get_scene` or render a smaller view. +- **Security:** only programs that know the token can use the bridge, and the token is never sent: both sides prove they know it with a challenge-response (HMAC-SHA256 over fresh nonces). Browsers must come from `https://benchup3d.github.io` or a local development server (`--allow-origin` adds another), so other web pages cannot reach it. Pass the token in the environment, never as a command-line argument, which other users on the computer can see. +- **Web browsers:** Chrome and Safari may ask whether the site may connect to devices on your local network the first time BenchUp connects to the bridge; allow it. ## Using it from an agent @@ -86,3 +126,9 @@ With Claude Code, either let it call the CLI directly ("use `benchup` to add a s ```sh claude mcp add benchup -- benchup mcp /path/to/shelf.bup ``` + +To work on the project open in BenchUp instead, register the bridge with the token from Settings → Agent connection (see [Live editing](#live-editing-in-the-open-app)): + +```sh +claude mcp add benchup -e BENCHUP_AGENT_TOKEN= -- benchup bridge +``` From 7fb4046526ccd97d8b1035e689a570f55efe223a Mon Sep 17 00:00:00 2001 From: Danny Coulombe Date: Sun, 15 Nov 2026 16:10:18 -0500 Subject: [PATCH 2/4] Add agent activity statuses and Codex setup --- assets/i18n/en_US.json | 21 +++++ assets/i18n/es_419.json | 21 +++++ assets/i18n/it_IT.json | 21 +++++ assets/i18n/pt_BR.json | 21 +++++ assets/i18n/vi_VN.json | 21 +++++ assets/i18n/zh_CN.json | 21 +++++ crates/benchup-app/src/agent_link.rs | 66 ++++++++++++-- crates/benchup-app/src/agent_link_activity.rs | 84 ++++++++++++++++++ crates/benchup-app/src/agent_link_tests.rs | 86 +++++++++++++++++++ crates/benchup-app/src/agent_link_ui.rs | 17 ++-- 10 files changed, 368 insertions(+), 11 deletions(-) create mode 100644 crates/benchup-app/src/agent_link_activity.rs diff --git a/assets/i18n/en_US.json b/assets/i18n/en_US.json index 45bd4d1..3dbd90d 100644 --- a/assets/i18n/en_US.json +++ b/assets/i18n/en_US.json @@ -55,14 +55,35 @@ "account.storage_unavailable": "Storage unavailable", "account.the_server_did_not_sign_out": "The server did not sign out.", "account.use_https_or_http_on_localhost_for_development": "Use HTTPS, or HTTP on localhost for development.", + "agent_link.activity.batch": "Applying a batch of changes", + "agent_link.activity.checking": "Checking the model", + "agent_link.activity.creating": "Creating geometry", + "agent_link.activity.current": "AI: {activity}…", + "agent_link.activity.deleting": "Deleting objects", + "agent_link.activity.estimating": "Calculating quantities and costs", + "agent_link.activity.finding": "Finding objects", + "agent_link.activity.joints": "Updating joints and profiles", + "agent_link.activity.materials": "Updating materials and finishes", + "agent_link.activity.preview": "Previewing changes", + "agent_link.activity.reading": "Reading the project", + "agent_link.activity.redo": "Redoing changes", + "agent_link.activity.rendering": "Rendering a view", + "agent_link.activity.selecting": "Selecting objects", + "agent_link.activity.undo": "Undoing changes", + "agent_link.activity.updating": "Updating objects", + "agent_link.activity.waiting": "AI: Waiting — {activity}", + "agent_link.activity.waiting_access": "AI: Waiting for access — {activity}", + "agent_link.activity.working": "Working on the project", "agent_link.allow": "Allow agent connection", "agent_link.allow_help": "When on, BenchUp connects to benchup bridge and runs the calls an agent sends. Each change is one undo step.", "agent_link.an_agent": "An agent", "agent_link.chip.connected": "Agent connected", "agent_link.chip.designer": "You have control", "agent_link.chip.editing": "Agent is editing", + "agent_link.codex_command_help": "Adds BenchUp to Codex as an MCP server that uses this token.", "agent_link.command_help": "Adds BenchUp to Claude Code as an MCP server that uses this token.", "agent_link.copied": "Copied", + "agent_link.copy_codex_command": "Copy Codex command", "agent_link.copy_command": "Copy Claude Code command", "agent_link.copy_json": "Copy MCP configuration", "agent_link.copy_shell": "Copy shell setup", diff --git a/assets/i18n/es_419.json b/assets/i18n/es_419.json index f21a017..1b9af0b 100644 --- a/assets/i18n/es_419.json +++ b/assets/i18n/es_419.json @@ -55,14 +55,35 @@ "account.storage_unavailable": "Almacenamiento no disponible", "account.the_server_did_not_sign_out": "El servidor no cerró la sesión.", "account.use_https_or_http_on_localhost_for_development": "Usa HTTPS, o HTTP en localhost para desarrollo.", + "agent_link.activity.batch": "Aplicando un lote de cambios", + "agent_link.activity.checking": "Revisando el modelo", + "agent_link.activity.creating": "Creando geometría", + "agent_link.activity.current": "IA: {activity}…", + "agent_link.activity.deleting": "Eliminando objetos", + "agent_link.activity.estimating": "Calculando cantidades y costos", + "agent_link.activity.finding": "Buscando objetos", + "agent_link.activity.joints": "Actualizando uniones y perfiles", + "agent_link.activity.materials": "Actualizando materiales y acabados", + "agent_link.activity.preview": "Previsualizando cambios", + "agent_link.activity.reading": "Leyendo el proyecto", + "agent_link.activity.redo": "Rehaciendo cambios", + "agent_link.activity.rendering": "Renderizando una vista", + "agent_link.activity.selecting": "Seleccionando objetos", + "agent_link.activity.undo": "Deshaciendo cambios", + "agent_link.activity.updating": "Actualizando objetos", + "agent_link.activity.waiting": "IA: En espera — {activity}", + "agent_link.activity.waiting_access": "IA: Esperando acceso — {activity}", + "agent_link.activity.working": "Trabajando en el proyecto", "agent_link.allow": "Permitir conexión de agentes", "agent_link.allow_help": "Al activarla, BenchUp se conecta a benchup bridge y ejecuta las llamadas que envía un agente. Cada cambio es un paso de deshacer.", "agent_link.an_agent": "Un agente", "agent_link.chip.connected": "Agente conectado", "agent_link.chip.designer": "Tienes el control", "agent_link.chip.editing": "El agente está editando", + "agent_link.codex_command_help": "Añade BenchUp a Codex como servidor MCP con este token.", "agent_link.command_help": "Añade BenchUp a Claude Code como servidor MCP con este token.", "agent_link.copied": "Copiado", + "agent_link.copy_codex_command": "Copiar comando de Codex", "agent_link.copy_command": "Copiar comando de Claude Code", "agent_link.copy_json": "Copiar configuración MCP", "agent_link.copy_shell": "Copiar configuración de terminal", diff --git a/assets/i18n/it_IT.json b/assets/i18n/it_IT.json index 5e7e880..77dcfb6 100644 --- a/assets/i18n/it_IT.json +++ b/assets/i18n/it_IT.json @@ -55,14 +55,35 @@ "account.storage_unavailable": "Archiviazione non disponibile", "account.the_server_did_not_sign_out": "Il server non ha effettuato la disconnessione.", "account.use_https_or_http_on_localhost_for_development": "Usa HTTPS, oppure HTTP su localhost per lo sviluppo.", + "agent_link.activity.batch": "Applicazione di un gruppo di modifiche", + "agent_link.activity.checking": "Verifica del modello", + "agent_link.activity.creating": "Creazione della geometria", + "agent_link.activity.current": "IA: {activity}…", + "agent_link.activity.deleting": "Eliminazione degli oggetti", + "agent_link.activity.estimating": "Calcolo delle quantità e dei costi", + "agent_link.activity.finding": "Ricerca degli oggetti", + "agent_link.activity.joints": "Aggiornamento di giunti e profili", + "agent_link.activity.materials": "Aggiornamento di materiali e finiture", + "agent_link.activity.preview": "Anteprima delle modifiche", + "agent_link.activity.reading": "Lettura del progetto", + "agent_link.activity.redo": "Ripristino delle modifiche", + "agent_link.activity.rendering": "Rendering di una vista", + "agent_link.activity.selecting": "Selezione degli oggetti", + "agent_link.activity.undo": "Annullamento delle modifiche", + "agent_link.activity.updating": "Aggiornamento degli oggetti", + "agent_link.activity.waiting": "IA: In attesa — {activity}", + "agent_link.activity.waiting_access": "IA: In attesa di accesso — {activity}", + "agent_link.activity.working": "Elaborazione del progetto", "agent_link.allow": "Consenti connessione agenti", "agent_link.allow_help": "Se attiva, BenchUp si collega a benchup bridge ed esegue le chiamate inviate da un agente. Ogni modifica è un passo di annullamento.", "agent_link.an_agent": "Un agente", "agent_link.chip.connected": "Agente collegato", "agent_link.chip.designer": "Hai il controllo", "agent_link.chip.editing": "L'agente sta modificando", + "agent_link.codex_command_help": "Aggiunge BenchUp a Codex come server MCP con questo token.", "agent_link.command_help": "Aggiunge BenchUp a Claude Code come server MCP con questo token.", "agent_link.copied": "Copiato", + "agent_link.copy_codex_command": "Copia comando Codex", "agent_link.copy_command": "Copia comando Claude Code", "agent_link.copy_json": "Copia configurazione MCP", "agent_link.copy_shell": "Copia configurazione terminale", diff --git a/assets/i18n/pt_BR.json b/assets/i18n/pt_BR.json index 52be8c0..a43db12 100644 --- a/assets/i18n/pt_BR.json +++ b/assets/i18n/pt_BR.json @@ -55,14 +55,35 @@ "account.storage_unavailable": "Armazenamento indisponível", "account.the_server_did_not_sign_out": "O servidor não encerrou a sessão.", "account.use_https_or_http_on_localhost_for_development": "Use HTTPS, ou HTTP no localhost para desenvolvimento.", + "agent_link.activity.batch": "Aplicando um lote de alterações", + "agent_link.activity.checking": "Verificando o modelo", + "agent_link.activity.creating": "Criando geometria", + "agent_link.activity.current": "IA: {activity}…", + "agent_link.activity.deleting": "Excluindo objetos", + "agent_link.activity.estimating": "Calculando quantidades e custos", + "agent_link.activity.finding": "Buscando objetos", + "agent_link.activity.joints": "Atualizando juntas e perfis", + "agent_link.activity.materials": "Atualizando materiais e acabamentos", + "agent_link.activity.preview": "Visualizando alterações", + "agent_link.activity.reading": "Lendo o projeto", + "agent_link.activity.redo": "Refazendo alterações", + "agent_link.activity.rendering": "Renderizando uma vista", + "agent_link.activity.selecting": "Selecionando objetos", + "agent_link.activity.undo": "Desfazendo alterações", + "agent_link.activity.updating": "Atualizando objetos", + "agent_link.activity.waiting": "IA: Aguardando — {activity}", + "agent_link.activity.waiting_access": "IA: Aguardando acesso — {activity}", + "agent_link.activity.working": "Trabalhando no projeto", "agent_link.allow": "Permitir conexão de agentes", "agent_link.allow_help": "Quando ativada, o BenchUp se conecta ao benchup bridge e executa as chamadas que um agente envia. Cada alteração é uma etapa de desfazer.", "agent_link.an_agent": "Um agente", "agent_link.chip.connected": "Agente conectado", "agent_link.chip.designer": "Você tem o controle", "agent_link.chip.editing": "O agente está editando", + "agent_link.codex_command_help": "Adiciona o BenchUp ao Codex como servidor MCP com este token.", "agent_link.command_help": "Adiciona o BenchUp ao Claude Code como servidor MCP com este token.", "agent_link.copied": "Copiado", + "agent_link.copy_codex_command": "Copiar comando do Codex", "agent_link.copy_command": "Copiar comando do Claude Code", "agent_link.copy_json": "Copiar configuração MCP", "agent_link.copy_shell": "Copiar configuração do terminal", diff --git a/assets/i18n/vi_VN.json b/assets/i18n/vi_VN.json index 8fe4ee2..ba256a7 100644 --- a/assets/i18n/vi_VN.json +++ b/assets/i18n/vi_VN.json @@ -55,14 +55,35 @@ "account.storage_unavailable": "Không có bộ nhớ", "account.the_server_did_not_sign_out": "Máy chủ không đăng xuất.", "account.use_https_or_http_on_localhost_for_development": "Sử dụng HTTPS hoặc HTTP trên localhost để phát triển.", + "agent_link.activity.batch": "Đang áp dụng một loạt thay đổi", + "agent_link.activity.checking": "Đang kiểm tra mô hình", + "agent_link.activity.creating": "Đang tạo hình học", + "agent_link.activity.current": "AI: {activity}…", + "agent_link.activity.deleting": "Đang xóa đối tượng", + "agent_link.activity.estimating": "Đang tính số lượng và chi phí", + "agent_link.activity.finding": "Đang tìm đối tượng", + "agent_link.activity.joints": "Đang cập nhật mối ghép và biên dạng", + "agent_link.activity.materials": "Đang cập nhật vật liệu và lớp hoàn thiện", + "agent_link.activity.preview": "Đang xem trước thay đổi", + "agent_link.activity.reading": "Đang đọc dự án", + "agent_link.activity.redo": "Đang làm lại thay đổi", + "agent_link.activity.rendering": "Đang kết xuất góc nhìn", + "agent_link.activity.selecting": "Đang chọn đối tượng", + "agent_link.activity.undo": "Đang hoàn tác thay đổi", + "agent_link.activity.updating": "Đang cập nhật đối tượng", + "agent_link.activity.waiting": "AI: Đang chờ — {activity}", + "agent_link.activity.waiting_access": "AI: Đang chờ cấp quyền — {activity}", + "agent_link.activity.working": "Đang xử lý dự án", "agent_link.allow": "Cho phép kết nối tác tử", "agent_link.allow_help": "Khi bật, BenchUp kết nối tới benchup bridge và thực hiện các lệnh gọi do tác tử gửi. Mỗi thay đổi là một bước hoàn tác.", "agent_link.an_agent": "Một tác tử", "agent_link.chip.connected": "Tác tử đã kết nối", "agent_link.chip.designer": "Bạn đang điều khiển", "agent_link.chip.editing": "Tác tử đang chỉnh sửa", + "agent_link.codex_command_help": "Thêm BenchUp vào Codex làm máy chủ MCP dùng mã này.", "agent_link.command_help": "Thêm BenchUp vào Claude Code làm máy chủ MCP dùng mã này.", "agent_link.copied": "Đã sao chép", + "agent_link.copy_codex_command": "Sao chép lệnh Codex", "agent_link.copy_command": "Sao chép lệnh Claude Code", "agent_link.copy_json": "Sao chép cấu hình MCP", "agent_link.copy_shell": "Sao chép thiết lập dòng lệnh", diff --git a/assets/i18n/zh_CN.json b/assets/i18n/zh_CN.json index 17b3adf..283fe8a 100644 --- a/assets/i18n/zh_CN.json +++ b/assets/i18n/zh_CN.json @@ -55,14 +55,35 @@ "account.storage_unavailable": "存储不可用", "account.the_server_did_not_sign_out": "服务器未执行退出登录。", "account.use_https_or_http_on_localhost_for_development": "开发时请使用 HTTPS,或在 localhost 上使用 HTTP。", + "agent_link.activity.batch": "正在批量应用更改", + "agent_link.activity.checking": "正在检查模型", + "agent_link.activity.creating": "正在创建几何体", + "agent_link.activity.current": "AI:{activity}…", + "agent_link.activity.deleting": "正在删除对象", + "agent_link.activity.estimating": "正在计算用量和成本", + "agent_link.activity.finding": "正在查找对象", + "agent_link.activity.joints": "正在更新接头和轮廓", + "agent_link.activity.materials": "正在更新材料和饰面", + "agent_link.activity.preview": "正在预览更改", + "agent_link.activity.reading": "正在读取项目", + "agent_link.activity.redo": "正在重做更改", + "agent_link.activity.rendering": "正在渲染视图", + "agent_link.activity.selecting": "正在选择对象", + "agent_link.activity.undo": "正在撤销更改", + "agent_link.activity.updating": "正在更新对象", + "agent_link.activity.waiting": "AI:等待中 — {activity}", + "agent_link.activity.waiting_access": "AI:等待访问许可 — {activity}", + "agent_link.activity.working": "正在处理项目", "agent_link.allow": "允许智能体连接", "agent_link.allow_help": "开启后,BenchUp 会连接到 benchup bridge 并执行智能体发送的调用。每次更改都是一个撤销步骤。", "agent_link.an_agent": "一个智能体", "agent_link.chip.connected": "智能体已连接", "agent_link.chip.designer": "由你控制", "agent_link.chip.editing": "智能体正在编辑", + "agent_link.codex_command_help": "将 BenchUp 作为使用此令牌的 MCP 服务器添加到 Codex。", "agent_link.command_help": "将 BenchUp 作为使用此令牌的 MCP 服务器添加到 Claude Code。", "agent_link.copied": "已复制", + "agent_link.copy_codex_command": "复制 Codex 命令", "agent_link.copy_command": "复制 Claude Code 命令", "agent_link.copy_json": "复制 MCP 配置", "agent_link.copy_shell": "复制终端设置", diff --git a/crates/benchup-app/src/agent_link.rs b/crates/benchup-app/src/agent_link.rs index 9a00129..b38955e 100644 --- a/crates/benchup-app/src/agent_link.rs +++ b/crates/benchup-app/src/agent_link.rs @@ -29,6 +29,9 @@ use benchup_tools::{Call, ToolEnv, batch, pairing}; use serde_json::{Value, json}; use std::collections::VecDeque; +#[path = "agent_link_activity.rs"] +mod activity; +use activity::Activity; #[path = "agent_link_ui.rs"] mod ui; #[cfg(test)] @@ -58,6 +61,8 @@ const DEADLINE_MARGIN_MS: u64 = 1000; const TOKEN_RETRY: f64 = 30.; /// Granted control returns to normal after the agent is quiet this long. const AGENT_IDLE: f64 = 8.; +/// Keep quick calls readable between successive agent requests. +const ACTIVITY_LINGER: f64 = 3.; const TOKEN_FILE: &str = "agent-link.token"; const TOKEN_STORAGE: &str = "benchup.agent_link.token"; @@ -387,12 +392,14 @@ pub(super) struct AgentLink { denied: bool, /// When the agent last changed the model. last_change: Option, + /// Session-only telemetry, cleared on disconnect or project replacement. + last_activity: Option<(Activity, f64)>, /// The last copy button pressed and until when "Copied" shows (session only). copied: Option<(ui::CopiedText, f64)>, - /// The `render_view` being made on another thread: its call id, and - /// where the answer arrives. + /// The `render_view` being made on another thread: its call id, where + /// the answer arrives, and the document epoch it belongs to. #[cfg(not(target_arch = "wasm32"))] - rendering: Option<(String, std::sync::mpsc::Receiver<(bool, Value)>)>, + rendering: Option<(String, std::sync::mpsc::Receiver<(bool, Value)>, u64)>, /// Results larger than this are refused (lowered in tests). max_result_bytes: usize, /// Token storage; tests use a temporary slot. @@ -414,6 +421,12 @@ pub(super) fn claude_command(token: &str, port: u16) -> String { format!("claude mcp add benchup -e BENCHUP_AGENT_TOKEN={token} -- benchup bridge{port}") } +/// `codex mcp add` command; the bridge receives the token in its environment. +pub(super) fn codex_command(token: &str, port: u16) -> String { + let port = if port == DEFAULT_PORT { String::new() } else { format!(" --port {port}") }; + format!("codex mcp add benchup --env BENCHUP_AGENT_TOKEN={token} -- benchup bridge{port}") +} + /// Generic `mcpServers` entry; the token travels in the environment. pub(super) fn mcp_json(token: &str, port: u16) -> String { let mut args = vec![json!("bridge")]; @@ -454,6 +467,7 @@ impl AgentLink { asking: false, denied: false, last_change: None, + last_activity: None, copied: None, #[cfg(not(target_arch = "wasm32"))] rendering: None, @@ -536,6 +550,29 @@ impl AgentLink { } } + /// Work the app can observe, including reads and long-running renders. + /// Resolve labels here so changing the application language updates them. + pub(super) fn activity_text(&self, now: f64) -> Option { + if !self.connected() { + return None; + } + #[cfg(not(target_arch = "wasm32"))] + if self.rendering.as_ref().is_some_and(|(_, _, epoch)| *epoch == self.epoch) { + return Some(tr!("agent_link.activity.current", activity = Activity::Rendering.label())); + } + if let Some(queued) = self.queue.iter().find(|queued| queued.epoch == self.epoch) { + let activity = Activity::for_call(&queued.name, &queued.arguments).label(); + return Some(if self.asking && queued.changes { + tr!("agent_link.activity.waiting_access", activity = activity) + } else { + tr!("agent_link.activity.waiting", activity = activity) + }); + } + self.last_activity + .filter(|(_, at)| now - at < ACTIVITY_LINGER) + .map(|(activity, _)| tr!("agent_link.activity.current", activity = activity.label())) + } + fn send(&mut self, message: Value) { if let Some(active) = &mut self.active { let _ = active.link.send(message); @@ -587,6 +624,7 @@ impl AgentLink { pub(super) fn take_control(&mut self) { self.control = Control::Designer; self.last_change = None; + self.last_activity = None; self.refuse_changes(tr!("agent_link.error.taken_control"), Some(tr!("agent_link.error.denied_hint"))); } /// The designer lets the agent edit again without asking. @@ -597,6 +635,7 @@ impl AgentLink { /// The designer refused the waiting changes. pub(super) fn deny(&mut self) { self.denied = true; + self.last_activity = None; self.refuse_changes(tr!("agent_link.error.denied"), Some(tr!("agent_link.error.denied_hint"))); } @@ -605,6 +644,7 @@ impl AgentLink { self.active = None; self.queue.clear(); self.asking = false; + self.last_activity = None; #[cfg(not(target_arch = "wasm32"))] { self.rendering = None; @@ -817,7 +857,10 @@ impl BenchUpApp { pub(super) fn poll_agent_link(&mut self, ctx: &egui::Context) { let now = clock(); let prefs = self.general_settings.agent.clone(); - self.agent_link.epoch = self.doc_epoch; + if self.agent_link.epoch != self.doc_epoch { + self.agent_link.last_activity = None; + self.agent_link.epoch = self.doc_epoch; + } self.agent_link.document_name = self.doc.name.clone(); self.agent_link.pump(ctx, now, &prefs); self.agent_link.expire(now); @@ -918,6 +961,7 @@ impl BenchUpApp { } let selection_before = self.selected.clone(); let (ok, result) = self.run_agent_request(&queued.name, &queued.arguments, ctx); + self.agent_link.last_activity = ok.then(|| (Activity::for_call(&queued.name, &queued.arguments), clock())); // `select` or `undo` may change the selection; in a shared // project the server must lease it before edits can follow. self.selection_changed_outside_frame(&selection_before); @@ -942,6 +986,12 @@ impl BenchUpApp { if self.agent_link.editing(now) { ctx.request_repaint_after(Duration::from_secs(1)); } + if let Some((_, at)) = self.agent_link.last_activity { + let remaining = ACTIVITY_LINGER - (clock() - at); + if remaining > 0. { + ctx.request_repaint_after(Duration::from_secs_f64(remaining)); + } + } } /// Renders a view on another thread from a copy of the model, so a large @@ -955,7 +1005,7 @@ impl BenchUpApp { let _ = tx.send(render_answer(&model, &arguments)); ctx.request_repaint(); }); - self.agent_link.rendering = Some((id, rx)); + self.agent_link.rendering = Some((id, rx, self.doc_epoch)); } /// Answers the render made on another thread, once it is done. @@ -963,14 +1013,18 @@ impl BenchUpApp { #[cfg(not(target_arch = "wasm32"))] { use std::sync::mpsc::TryRecvError; - let Some((id, rx)) = &self.agent_link.rendering else { return }; + let Some((id, rx, epoch)) = &self.agent_link.rendering else { return }; let answer = match rx.try_recv() { Ok(answer) => answer, Err(TryRecvError::Empty) => return, Err(TryRecvError::Disconnected) => (false, json!({"ok": false, "error": "the render failed"})), }; let id = id.clone(); + let current_document = *epoch == self.doc_epoch; self.agent_link.rendering = None; + if current_document { + self.agent_link.last_activity = answer.0.then(|| (Activity::Rendering, clock())); + } self.agent_link.document_name = self.doc.name.clone(); self.agent_link.send_result(&id, answer.0, answer.1, false); } diff --git a/crates/benchup-app/src/agent_link_activity.rs b/crates/benchup-app/src/agent_link_activity.rs new file mode 100644 index 0000000..348b588 --- /dev/null +++ b/crates/benchup-app/src/agent_link_activity.rs @@ -0,0 +1,84 @@ +//! Short, localized descriptions of the work reported by agent tool calls. +use benchup_core::tr; +use serde_json::Value; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub(super) enum Activity { + Reading, + Finding, + Creating, + Updating, + Materials, + Joints, + Deleting, + Selecting, + Checking, + Estimating, + Rendering, + Batch, + Preview, + Undo, + Redo, + Working, +} + +impl Activity { + pub(super) fn for_call(name: &str, arguments: &Value) -> Self { + match name { + "run_batch" if arguments.get("dry_run").and_then(Value::as_bool) == Some(true) => Self::Preview, + "run_batch" => { + let calls = arguments.get("calls").and_then(Value::as_array); + if let Some(calls) = calls.filter(|calls| calls.len() == 1) { + // Single-call batches are how the CLI forwards ordinary calls. + // Do not recurse into untrusted nested batches. + let call = &calls[0]; + Self::for_tool(call.get("tool").and_then(Value::as_str).unwrap_or_default()) + } else { + Self::Batch + } + } + "undo" if arguments.get("redo").and_then(Value::as_bool) == Some(true) => Self::Redo, + "undo" => Self::Undo, + name => Self::for_tool(name), + } + } + + fn for_tool(name: &str) -> Self { + match name { + "get_scene" | "get_object" | "get_selection" | "list_stock_materials" | "get_guide" | "measure_space" => Self::Reading, + "find_objects" | "find_surfaces" => Self::Finding, + "create_board" | "create_profile_board" | "create_copy" | "draw_polyline" | "add_dimension" | "add_text" => Self::Creating, + "move" | "move_to" | "rotate" | "scale" | "resize_board" | "push_pull" | "rename" | "group" | "ungroup" + | "tag" | "split_face" | "merge_faces" | "change_thickness" | "distribute" => Self::Updating, + "create_stock_material" | "set_material" | "set_grain" | "paint" | "set_finish" => Self::Materials, + "add_joint" | "remove_joint" | "flip_joint" | "set_joint" | "add_profile" | "remove_profile" => Self::Joints, + "erase" => Self::Deleting, + "select" | "enter_object" | "exit_to_root" => Self::Selecting, + "check_model" => Self::Checking, + "get_cut_list" | "get_estimate" => Self::Estimating, + "render_view" => Self::Rendering, + _ => Self::Working, + } + } + + pub(super) fn label(self) -> &'static str { + match self { + Self::Reading => tr!("agent_link.activity.reading"), + Self::Finding => tr!("agent_link.activity.finding"), + Self::Creating => tr!("agent_link.activity.creating"), + Self::Updating => tr!("agent_link.activity.updating"), + Self::Materials => tr!("agent_link.activity.materials"), + Self::Joints => tr!("agent_link.activity.joints"), + Self::Deleting => tr!("agent_link.activity.deleting"), + Self::Selecting => tr!("agent_link.activity.selecting"), + Self::Checking => tr!("agent_link.activity.checking"), + Self::Estimating => tr!("agent_link.activity.estimating"), + Self::Rendering => tr!("agent_link.activity.rendering"), + Self::Batch => tr!("agent_link.activity.batch"), + Self::Preview => tr!("agent_link.activity.preview"), + Self::Undo => tr!("agent_link.activity.undo"), + Self::Redo => tr!("agent_link.activity.redo"), + Self::Working => tr!("agent_link.activity.working"), + } + } +} diff --git a/crates/benchup-app/src/agent_link_tests.rs b/crates/benchup-app/src/agent_link_tests.rs index a2a0862..0a99c0d 100644 --- a/crates/benchup-app/src/agent_link_tests.rs +++ b/crates/benchup-app/src/agent_link_tests.rs @@ -176,6 +176,92 @@ fn each_changing_call_is_one_undo_step_and_reads_add_none() { assert!(setup.app.doc.model.objects.is_empty()); } +#[test] +fn activity_includes_reads_and_returns_to_idle_without_another_call() { + let mut setup = Setup::paired(); + assert!(setup.app.agent_link.activity_text(clock()).is_none()); + + assert_eq!(setup.call("get_scene", json!({}))["ok"], true); + let (_, at) = setup.app.agent_link.last_activity.unwrap(); + assert_eq!(setup.app.agent_link.activity_text(at), Some("AI: Reading the project…".into())); + assert!(setup.app.agent_link.activity_text(at + ACTIVITY_LINGER).is_none()); + assert!(!setup.app.agent_link.editing(at), "reading does not take editing control"); + + assert_eq!(setup.call("create_board", board("Side", 0.))["ok"], true); + assert_eq!(setup.app.agent_link.activity_text(clock()), Some("AI: Creating geometry…".into())); + // A failed request must not leave a misleading description of work. + assert_eq!(setup.call("get_object", json!({"id": u64::MAX}))["ok"], false); + assert!(setup.app.agent_link.activity_text(clock()).is_none()); +} + +#[test] +fn waiting_activity_clears_on_cancellation_denial_and_disconnect() { + let mut setup = Setup::paired(); + setup.app.line_start = Some(DVec3::ZERO); + let id = setup.send("create_board", board("Side", 0.)); + setup.poll(); + assert_eq!(setup.app.agent_link.activity_text(clock()), Some("AI: Waiting for access — Creating geometry".into())); + setup.wire.push(json!({"type": "cancel", "id": id})); + setup.poll(); + assert!(setup.app.agent_link.activity_text(clock()).is_none()); + + setup.send("create_board", board("Side", 0.)); + setup.poll(); + setup.app.agent_link.deny(); + assert!(setup.app.agent_link.activity_text(clock()).is_none()); + + assert_eq!(setup.call("get_scene", json!({}))["ok"], true, "reads still work after denying edits"); + assert!(setup.app.agent_link.activity_text(clock()).is_some()); + setup.wire.0.lock().unwrap().incoming.push(Err("disconnected".into())); + setup.poll(); + assert!(setup.app.agent_link.activity_text(clock()).is_none()); + assert!(setup.app.agent_link.last_activity.is_none()); +} + +#[test] +fn single_call_batches_describe_the_action_and_previews_are_identified() { + let mut setup = Setup::paired(); + let calls = json!([{"tool": "create_board", "arguments": board("Side", 0.)}]); + assert_eq!(setup.call("run_batch", json!({"calls": calls, "dry_run": true}))["ok"], true); + assert_eq!(setup.app.agent_link.activity_text(clock()), Some("AI: Previewing changes…".into())); + assert!(setup.app.doc.model.objects.is_empty()); + + assert_eq!(setup.call("run_batch", json!({"calls": calls}))["ok"], true); + assert_eq!(setup.app.agent_link.activity_text(clock()), Some("AI: Creating geometry…".into())); + setup.app.doc_epoch += 1; + setup.poll(); + assert!(setup.app.agent_link.activity_text(clock()).is_none(), "activity belongs to its project"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn rendering_activity_stays_visible_until_the_render_finishes() { + let mut setup = Setup::paired(); + // Hold a render open deterministically without running a GPU render. + let (tx, rx) = std::sync::mpsc::channel(); + setup.app.agent_link.rendering = Some(("render".into(), rx, setup.app.doc_epoch)); + let later = clock() + ACTIVITY_LINGER * 10.; + assert_eq!(setup.app.agent_link.activity_text(later), Some("AI: Rendering a view…".into())); + // Concurrent read calls must not hide an in-progress render. + assert_eq!(setup.call("get_scene", json!({}))["ok"], true); + assert_eq!(setup.app.agent_link.activity_text(later), Some("AI: Rendering a view…".into())); + tx.send((true, json!({"png_base64": "test"}))).unwrap(); + setup.poll(); + let (_, at) = setup.app.agent_link.last_activity.unwrap(); + assert!(setup.app.agent_link.rendering.is_none()); + assert_eq!(setup.app.agent_link.activity_text(at), Some("AI: Rendering a view…".into())); + assert!(setup.app.agent_link.activity_text(at + ACTIVITY_LINGER).is_none()); + + let (tx, rx) = std::sync::mpsc::channel(); + setup.app.agent_link.rendering = Some(("old-project-render".into(), rx, setup.app.doc_epoch)); + setup.app.doc_epoch += 1; + setup.poll(); + assert!(setup.app.agent_link.activity_text(clock()).is_none(), "the previous project's render stays hidden"); + tx.send((true, json!({"png_base64": "test"}))).unwrap(); + setup.poll(); + assert!(setup.app.agent_link.activity_text(clock()).is_none(), "finishing an old render does not revive its activity"); +} + #[test] fn a_batch_is_one_step_all_or_nothing_and_a_dry_run_changes_nothing() { let mut setup = Setup::paired(); diff --git a/crates/benchup-app/src/agent_link_ui.rs b/crates/benchup-app/src/agent_link_ui.rs index 8d7886e..170d182 100644 --- a/crates/benchup-app/src/agent_link_ui.rs +++ b/crates/benchup-app/src/agent_link_ui.rs @@ -99,6 +99,9 @@ impl BenchUpApp { if ui.button(tr!("agent_link.copy_command")).on_hover_text(tr!("agent_link.command_help")).clicked() { self.agent_link.copy(ui.ctx(), claude_command(&token, port), CopiedText::Setup); } + if ui.button(tr!("agent_link.copy_codex_command")).on_hover_text(tr!("agent_link.codex_command_help")).clicked() { + self.agent_link.copy(ui.ctx(), codex_command(&token, port), CopiedText::Setup); + } if ui.button(tr!("agent_link.copy_json")).on_hover_text(tr!("agent_link.json_help")).clicked() { self.agent_link.copy(ui.ctx(), mcp_json(&token, port), CopiedText::Setup); } @@ -133,11 +136,15 @@ impl BenchUpApp { } else { (tr!("agent_link.chip.connected").to_owned(), colors.muted) }; - let label = ui.text_label(RichText::new(text).size(12.).color(color)); - if let Some(client) = client { - label.on_hover_text(client); - } - dot(ui, color); + let text = self.agent_link.activity_text(now).unwrap_or(text); + let tooltip = match client { + Some(client) => format!("{text}\n{client}"), + None => text.clone(), + }; + let label = ui.add(crate::natural_text::Label::new(RichText::new(text).size(12.).color(color)).truncate()); + label.on_hover_text(tooltip); + // The dot reports the live connection; text and buttons report control. + dot(ui, Color32::from_rgb(34, 197, 94)); ui.add_space(8.); } From 47ca9b68848774190b70585b1fb013a62cc94913 Mon Sep 17 00:00:00 2001 From: Danny Coulombe Date: Mon, 16 Nov 2026 06:45:44 -0500 Subject: [PATCH 3/4] Add GPU agent feedback and reliable task completion --- assets/i18n/en_US.json | 1 + assets/i18n/es_419.json | 1 + assets/i18n/it_IT.json | 1 + assets/i18n/pt_BR.json | 1 + assets/i18n/vi_VN.json | 1 + assets/i18n/zh_CN.json | 1 + crates/benchup-app/src/agent_effects.rs | 797 ++++++++++++++++++ crates/benchup-app/src/agent_effects.wgsl | 48 ++ crates/benchup-app/src/agent_effects_tests.rs | 172 ++++ crates/benchup-app/src/agent_link.rs | 132 ++- crates/benchup-app/src/agent_link_activity.rs | 9 + crates/benchup-app/src/agent_link_tests.rs | 140 ++- crates/benchup-app/src/agent_link_ui.rs | 5 +- crates/benchup-app/src/agent_robot.wgsl | 103 +++ crates/benchup-app/src/app.rs | 6 + crates/benchup-app/src/edges.wgsl | 67 +- crates/benchup-app/src/renderer.rs | 91 +- crates/benchup-app/src/resident_appearance.rs | 22 +- crates/benchup-app/src/resident_scene.rs | 67 +- crates/benchup-app/src/resident_scene.wgsl | 43 +- crates/benchup-app/src/scene_buffers.rs | 1 + crates/benchup-cli/src/bridge.rs | 5 + crates/benchup-cli/src/bridge_tests.rs | 9 + crates/benchup-cli/src/mcp.rs | 50 ++ docs/agent-guide.md | 1 + docs/cli.md | 23 + scripts/benchmark_viewport_web.mjs | 20 +- scripts/codex_benchup_notify.mjs | 111 +++ scripts/test_agent_effects.mjs | 174 ++++ scripts/test_codex_benchup_notify.mjs | 112 +++ scripts/test_grain_shader.mjs | 2 +- 31 files changed, 2145 insertions(+), 71 deletions(-) create mode 100644 crates/benchup-app/src/agent_effects.rs create mode 100644 crates/benchup-app/src/agent_effects.wgsl create mode 100644 crates/benchup-app/src/agent_effects_tests.rs create mode 100644 crates/benchup-app/src/agent_robot.wgsl create mode 100644 scripts/codex_benchup_notify.mjs create mode 100644 scripts/test_agent_effects.mjs create mode 100644 scripts/test_codex_benchup_notify.mjs diff --git a/assets/i18n/en_US.json b/assets/i18n/en_US.json index 3dbd90d..bff95d2 100644 --- a/assets/i18n/en_US.json +++ b/assets/i18n/en_US.json @@ -89,6 +89,7 @@ "agent_link.copy_shell": "Copy shell setup", "agent_link.copy_token": "Copy token", "agent_link.deny": "Deny access", + "agent_link.error.activity_phase": "Expected phase: analyzing, preparing, or finished.", "agent_link.error.app_busy": "Another BenchUp window is already connected to this bridge", "agent_link.error.arguments_not_object": "The arguments must be a JSON object", "agent_link.error.bridge": "The bridge refused the connection: {message}", diff --git a/assets/i18n/es_419.json b/assets/i18n/es_419.json index 1b9af0b..f105910 100644 --- a/assets/i18n/es_419.json +++ b/assets/i18n/es_419.json @@ -89,6 +89,7 @@ "agent_link.copy_shell": "Copiar configuración de terminal", "agent_link.copy_token": "Copiar token", "agent_link.deny": "Denegar acceso", + "agent_link.error.activity_phase": "Se esperaba una fase: analyzing, preparing o finished.", "agent_link.error.app_busy": "Otra ventana de BenchUp ya está conectada a este puente", "agent_link.error.arguments_not_object": "Los argumentos deben ser un objeto JSON", "agent_link.error.bridge": "El puente rechazó la conexión: {message}", diff --git a/assets/i18n/it_IT.json b/assets/i18n/it_IT.json index 77dcfb6..4406c0f 100644 --- a/assets/i18n/it_IT.json +++ b/assets/i18n/it_IT.json @@ -89,6 +89,7 @@ "agent_link.copy_shell": "Copia configurazione terminale", "agent_link.copy_token": "Copia token", "agent_link.deny": "Nega accesso", + "agent_link.error.activity_phase": "Fase prevista: analyzing, preparing o finished.", "agent_link.error.app_busy": "Un'altra finestra di BenchUp è già collegata a questo bridge", "agent_link.error.arguments_not_object": "Gli argomenti devono essere un oggetto JSON", "agent_link.error.bridge": "Il bridge ha rifiutato la connessione: {message}", diff --git a/assets/i18n/pt_BR.json b/assets/i18n/pt_BR.json index a43db12..abcf829 100644 --- a/assets/i18n/pt_BR.json +++ b/assets/i18n/pt_BR.json @@ -89,6 +89,7 @@ "agent_link.copy_shell": "Copiar configuração do terminal", "agent_link.copy_token": "Copiar token", "agent_link.deny": "Negar acesso", + "agent_link.error.activity_phase": "Fase esperada: analyzing, preparing ou finished.", "agent_link.error.app_busy": "Outra janela do BenchUp já está conectada a esta ponte", "agent_link.error.arguments_not_object": "Os argumentos devem ser um objeto JSON", "agent_link.error.bridge": "A ponte recusou a conexão: {message}", diff --git a/assets/i18n/vi_VN.json b/assets/i18n/vi_VN.json index ba256a7..133da10 100644 --- a/assets/i18n/vi_VN.json +++ b/assets/i18n/vi_VN.json @@ -89,6 +89,7 @@ "agent_link.copy_shell": "Sao chép thiết lập dòng lệnh", "agent_link.copy_token": "Sao chép mã", "agent_link.deny": "Từ chối", + "agent_link.error.activity_phase": "Giai đoạn phải là analyzing, preparing hoặc finished.", "agent_link.error.app_busy": "Một cửa sổ BenchUp khác đã kết nối với cầu nối này", "agent_link.error.arguments_not_object": "Đối số phải là một đối tượng JSON", "agent_link.error.bridge": "Cầu nối đã từ chối kết nối: {message}", diff --git a/assets/i18n/zh_CN.json b/assets/i18n/zh_CN.json index 283fe8a..adaca61 100644 --- a/assets/i18n/zh_CN.json +++ b/assets/i18n/zh_CN.json @@ -89,6 +89,7 @@ "agent_link.copy_shell": "复制终端设置", "agent_link.copy_token": "复制令牌", "agent_link.deny": "拒绝访问", + "agent_link.error.activity_phase": "phase 必须为 analyzing、preparing 或 finished。", "agent_link.error.app_busy": "另一个 BenchUp 窗口已连接到此桥接程序", "agent_link.error.arguments_not_object": "参数必须是 JSON 对象", "agent_link.error.bridge": "桥接程序拒绝了连接:{message}", diff --git a/crates/benchup-app/src/agent_effects.rs b/crates/benchup-app/src/agent_effects.rs new file mode 100644 index 0000000..0d224a2 --- /dev/null +++ b/crates/benchup-app/src/agent_effects.rs @@ -0,0 +1,797 @@ +//! Session-only agent feedback. Geometry is captured once per committed edit; +//! cameras, the robot, scanning and digital assembly animate on the GPU. +use super::*; +use std::collections::BTreeSet; +use wgpu::util::DeviceExt; + +const LIFETIME: f64 = 0.95; +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) enum Mode { + Looking, + Preparing, +} + +#[derive(Default)] +pub(crate) struct State { + starts: Arc>, + ghosts: Vec<(f64, Arc)>, +} + +#[derive(Clone, Default)] +pub(crate) struct Frame { + pub starts: Arc>, + ghosts: Vec<(f64, Arc)>, + pub now: f64, + pub activity: Option<(Mode, f64)>, + pub bounds: Option<(DVec3, DVec3)>, +} + +pub(crate) fn timestamp(now: f64) -> f32 { + now.rem_euclid(4096.) as f32 +} + +/// A prepared change is installed only after the model commit succeeds. +pub(crate) struct Change { + ids: BTreeSet, + ghost: Option>, +} +impl Change { + pub(crate) fn between( + before: &Model, + after: &Model, + display: &benchup_core::DisplaySettings, + ) -> Self { + let vertices = changed_ids( + before.vertices.iter().map(|(&id, v)| (id, v)), + after.vertices.iter().map(|(&id, v)| (id, v)), + ); + let mut ids = changed_ids( + before.faces.iter().map(|(&id, v)| (id, v)), + after.faces.iter().map(|(&id, v)| (id, v)), + ); + ids.extend(changed_ids( + before.edges.iter().map(|(&id, v)| (id, v)), + after.edges.iter().map(|(&id, v)| (id, v)), + )); + ids.extend(changed_ids( + before.face_colors.iter().map(|(&id, v)| (id, v)), + after.face_colors.iter().map(|(&id, v)| (id, v)), + )); + // A move can keep every face/edge ID while changing just its vertices. + for model in [before, after] { + ids.extend( + model + .faces + .values() + .filter(|face| { + face.vertices + .iter() + .chain(face.holes.iter().flatten()) + .any(|id| vertices.contains(id)) + }) + .map(|face| face.id), + ); + ids.extend( + model + .edges + .values() + .filter(|edge| vertices.contains(&edge.a) || vertices.contains(&edge.b)) + .map(|edge| edge.id), + ); + } + // Membership and Outliner metadata can change on an ancestor when a + // single child is added/erased. They do not change surviving geometry. + let mut objects: BTreeSet<_> = changed_ids( + before.objects.iter().map(|(&id, v)| (id, v)), + after.objects.iter().map(|(&id, v)| (id, v)), + ) + .into_iter() + .filter(|id| match (before.objects.get(id), after.objects.get(id)) { + (Some(a), Some(b)) => { + a.transform != b.transform + || crate::app::surface_appearance::appearance_changed(a, b) + } + _ => false, + }) + .collect(); + let joints = changed_ids( + before.connections.iter().map(|(&id, v)| (id, v)), + after.connections.iter().map(|(&id, v)| (id, v)), + ); + let routes = changed_ids( + before.routes.iter().map(|(&id, v)| (id, v)), + after.routes.iter().map(|(&id, v)| (id, v)), + ); + for model in [before, after] { + for joint in joints.iter().filter_map(|id| model.connections.get(id)) { + objects.extend(joint.members); + } + objects.extend(routes.iter().filter_map(|id| model.parent(*id))); + objects.extend(model.objects.values().filter_map(|object| { + let material = object.stock_material_id?; + (before.stock_materials.get(&material) != after.stock_materials.get(&material)) + .then_some(object.id) + })); + ids.extend(model.expand_entities(&objects)); + } + // Tag visibility/hide operations also visibly add or remove geometry. + let mut visibility: BTreeSet<_> = before + .hidden + .symmetric_difference(&after.hidden) + .copied() + .collect(); + let tags = changed_ids( + before.tags.iter().map(|(&id, v)| (id, v)), + after.tags.iter().map(|(&id, v)| (id, v)), + ); + visibility.extend(changed_ids( + before.entity_tags.iter().map(|(&id, v)| (id, v)), + after.entity_tags.iter().map(|(&id, v)| (id, v)), + )); + for model in [before, after] { + visibility.extend( + model + .entity_tags + .iter() + .filter(|(_, tag)| tags.contains(*tag)) + .map(|(&id, _)| id), + ); + ids.extend(model.expand_entities(&visibility)); + } + let ghost = Ghost::new(before, &ids, display).map(Arc::new); + Self { ids, ghost } + } +} +impl State { + pub(crate) fn record(&mut self, change: Change, now: f64) { + self.expire(now); + // Rapid edits of the same geometry replace its prior departure instead + // of stacking full overlapping snapshots for the entire animation. + self.ghosts + .retain(|(_, ghost)| !ghost.ids.is_subset(&change.ids)); + let starts = Arc::make_mut(&mut self.starts); + for id in change.ids { + starts.insert(id, now); + } + if let Some(ghost) = change.ghost { + self.ghosts.push((now, ghost)); + } + } + fn expire(&mut self, now: f64) { + if self.starts.values().any(|at| now - at >= LIFETIME) { + Arc::make_mut(&mut self.starts).retain(|_, at| now - *at < LIFETIME); + } + self.ghosts.retain(|(at, _)| now - at < LIFETIME); + } + pub(crate) fn frame( + &mut self, + now: f64, + activity: Option<(Mode, f64)>, + bounds: Option<(DVec3, DVec3)>, + ) -> Frame { + self.expire(now); + Frame { + starts: self.starts.clone(), + ghosts: self.ghosts.clone(), + now, + activity, + bounds, + } + } +} +impl Frame { + pub(crate) fn stamp(&self, id: u64) -> f32 { + self.starts.get(&id).map_or(-1., |at| timestamp(*at)) + } + /// Precision meshes call this only while rebuilding after an edit. Retained + /// geometry instead inherits object timestamps through its state texture. + pub(crate) fn remap(&mut self, model: &Model) { + if self.starts.is_empty() { + return; + } + let mut inherited = Vec::new(); + for (&id, &at) in self.starts.iter() { + if model.objects.contains_key(&id) { + inherited.extend( + model + .expand_entities(&BTreeSet::from([id])) + .into_iter() + .map(|id| (id, at)), + ); + } + if let Some(derived) = model.routing_appearance.by_source.get(&id) { + inherited.extend(derived.iter().map(|id| (*id, at))); + } + } + if !inherited.is_empty() { + let starts = Arc::make_mut(&mut self.starts); + for (id, at) in inherited { + starts + .entry(id) + .and_modify(|previous| *previous = previous.max(at)) + .or_insert(at); + } + } + } + pub(crate) fn parameters(&self, camera: &Camera, rect: Rect) -> [f32; 4] { + let mut parameters = [timestamp(self.now), -1., 0., 1.]; + if let (Some((Mode::Looking, at)), Some((lo, hi))) = (self.activity, self.bounds) { + let size = DVec2::new(rect.width() as f64, rect.height() as f64); + let mut min = 1_f32; + let mut max = 0_f32; + for x in [lo.x, hi.x] { + for y in [lo.y, hi.y] { + for z in [lo.z, hi.z] { + if let Some(p) = camera.project(DVec3::new(x, y, z), size) { + let y = p.y as f32 / rect.height().max(1.); + min = min.min(y); + max = max.max(y); + } + } + } + } + min = min.clamp(0., 1.); + max = max.clamp(0., 1.); + // A cosine sweep starts at the bottom: up, down, then up. + let sweep = (1. - ((self.now - at) as f32 * std::f32::consts::PI).cos()) * 0.5; + parameters[1] = max + (min - max) * sweep; + parameters[2] = min; + parameters[3] = max; + } + parameters + } + pub(crate) fn animated(&self) -> bool { + self.activity.is_some() || !self.starts.is_empty() || !self.ghosts.is_empty() + } +} + +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct GhostVertex { + a: [f32; 3], + b: [f32; 3], + corner: [f32; 2], + kind: u32, +} +struct Ghost { + vertices: Vec, + ids: BTreeSet, + origin: DVec3, + gpu: OnceLock>, +} +impl Ghost { + fn new( + model: &Model, + ids: &BTreeSet, + display: &benchup_core::DisplaySettings, + ) -> Option { + let (lo, hi) = model.selection_bounds(ids)?; + let origin = (lo + hi) * 0.5; + let mut entities = EntityCache::default(); + entities.update(model); + let mut vertices = Vec::new(); + let mut rendered = BTreeSet::new(); + for id in ids + .iter() + .filter(|id| entities.get(**id).displayed(display)) + { + let start = vertices.len(); + if let Some(face) = model + .faces + .get(id) + .filter(|_| display.face_style != benchup_core::FaceStyle::Wireframe) + { + for triangle in model.face_triangles(face) { + // Honor active section planes in departing snapshots too. + let (polygon, len) = display.section_triangle(triangle); + for i in 1..len.saturating_sub(1) { + for p in [polygon[0], polygon[i], polygon[i + 1]] { + vertices.push(GhostVertex { + a: (p - origin).as_vec3().to_array(), + b: [0.; 3], + corner: [0.; 2], + kind: 0, + }); + } + } + } + } + if let Some(edge) = model.edges.get(id) { + let (a, b) = model.points(edge); + if let Some((a, b)) = display.section_segment(a, b) { + for corner in [ + [0., -1.], + [1., -1.], + [0., 1.], + [0., 1.], + [1., -1.], + [1., 1.], + ] { + vertices.push(GhostVertex { + a: (a - origin).as_vec3().to_array(), + b: (b - origin).as_vec3().to_array(), + corner, + kind: 1, + }); + } + } + } + if vertices.len() != start { + rendered.insert(*id); + } + } + (!vertices.is_empty()).then_some(Self { + vertices, + ids: rendered, + origin, + gpu: OnceLock::new(), + }) + } +} + +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct Uniform { + matrix: [[f32; 4]; 4], + viewport: [f32; 4], + robot: [f32; 4], + animation: [f32; 4], + paper: [f32; 4], +} +struct Pipelines { + ghost: wgpu::RenderPipeline, + robot: wgpu::RenderPipeline, +} +type UniformBuffers = (wgpu::Buffer, wgpu::BindGroup); +type UniformPool = Arc>>; +#[derive(Default)] +pub(crate) struct Cache { + robot: UniformPool, + ghost: UniformPool, +} +struct Draw { + pool: UniformPool, + ghost: Option>, + uniform: Uniform, + gpu: OnceLock<(wgpu::Buffer, wgpu::BindGroup)>, +} + +pub(crate) fn paint( + painter: &egui::Painter, + rect: Rect, + camera: &Camera, + frame: &Frame, + paper: Color32, + cache: &Cache, +) { + let size = DVec2::new(rect.width() as f64, rect.height() as f64); + let base = |origin| Uniform { + matrix: (camera.matrix(size) * glam::DMat4::from_translation(origin)) + .as_mat4() + .to_cols_array_2d(), + viewport: [ + rect.width(), + rect.height(), + painter.ctx().pixels_per_point(), + 0., + ], + robot: [0.; 4], + animation: [0.; 4], + paper: paper.to_array().map(|v| v as f32 / 255.), + }; + for (at, ghost) in &frame.ghosts { + let mut uniform = base(ghost.origin); + uniform.animation[0] = ((frame.now - at) / LIFETIME).clamp(0., 1.) as f32; + painter.add(egui_wgpu::Callback::new_paint_callback( + rect, + Draw { + pool: cache.ghost.clone(), + ghost: Some(ghost.clone()), + uniform, + gpu: OnceLock::new(), + }, + )); + } + if let Some((mode, at)) = frame.activity { + let mut uniform = base(DVec3::ZERO); + let mut right = rect.width() * 0.5; + let mut top = rect.height() * 0.5; + if let Some((lo, hi)) = frame.bounds { + right = f32::NEG_INFINITY; + top = f32::INFINITY; + for x in [lo.x, hi.x] { + for y in [lo.y, hi.y] { + for z in [lo.z, hi.z] { + if let Some(p) = camera.project(DVec3::new(x, y, z), size) { + right = right.max(p.x as f32); + top = top.min(p.y as f32); + } + } + } + } + } + // A bounded billboard sits beside the assembly and remains in the pane. + let x = (right + 40.).clamp(44., (rect.width() - 44.).max(44.)); + let y = (top + 30.).clamp(44., (rect.height() - 44.).max(44.)); + uniform.robot = [x, y, 38., if mode == Mode::Preparing { 1. } else { 0. }]; + uniform.animation[1] = (frame.now - at) as f32; + painter.add(egui_wgpu::Callback::new_paint_callback( + rect, + Draw { + pool: cache.robot.clone(), + ghost: None, + uniform, + gpu: OnceLock::new(), + }, + )); + } + if frame.animated() { + painter + .ctx() + .request_repaint_after(std::time::Duration::from_millis(16)); + } +} + +pub(crate) fn install( + device: &wgpu::Device, + format: wgpu::TextureFormat, + renderer: &mut egui_wgpu::Renderer, +) { + let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Agent robot and digital transitions"), + source: wgpu::ShaderSource::Wgsl( + concat!( + include_str!("surface_appearance.wgsl"), + "\n", + include_str!("agent_effects.wgsl"), + "\n", + include_str!("agent_robot.wgsl") + ) + .into(), + ), + }); + let attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x3,2=>Float32x2,3=>Uint32]; + let buffers = [Some(wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &attributes, + })]; + let pipeline = |robot| { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(if robot { + "Agent robot" + } else { + "Agent departing geometry" + }), + layout: None, + vertex: wgpu::VertexState { + module: &shader, + entry_point: Some(if robot { + "robot_vertex" + } else { + "ghost_vertex" + }), + buffers: if robot { &[] } else { &buffers }, + compilation_options: Default::default(), + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: Some(if robot { "robot_color" } else { "ghost_color" }), + targets: &[Some(wgpu::ColorTargetState { + format, + blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), + write_mask: wgpu::ColorWrites::ALL, + })], + compilation_options: Default::default(), + }), + primitive: Default::default(), + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(false), + depth_compare: Some(wgpu::CompareFunction::Always), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + multiview_mask: None, + cache: None, + }) + }; + renderer.callback_resources.insert(Pipelines { + ghost: pipeline(false), + robot: pipeline(true), + }); + renderer + .callback_resources + .insert(TargetSrgb(format.is_srgb())); +} +impl egui_wgpu::CallbackTrait for Draw { + fn prepare( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + _screen: &egui_wgpu::ScreenDescriptor, + _encoder: &mut wgpu::CommandEncoder, + resources: &mut egui_wgpu::CallbackResources, + ) -> Vec { + if let Some(pipelines) = resources.get::() { + self.gpu.get_or_init(|| { + let mut uniform = self.uniform; + // egui's callback target can be either linear or sRGB. + // The renderer records that bit in the shared viewport pipeline. + uniform.viewport[3] = if resources.get::().is_some_and(|v| v.0) { + 1. + } else { + 0. + }; + if let Some((buffer, bind)) = self.pool.lock().ok().and_then(|mut pool| pool.pop()) + { + queue.write_buffer(&buffer, 0, bytemuck::bytes_of(&uniform)); + return (buffer, bind); + } + let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Agent animation parameters"), + contents: bytemuck::bytes_of(&uniform), + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + }); + let pipeline = if self.ghost.is_some() { + &pipelines.ghost + } else { + &pipelines.robot + }; + let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Agent animation"), + layout: &pipeline.get_bind_group_layout(0), + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: buffer.as_entire_binding(), + }], + }); + (buffer, bind) + }); + if let Some(ghost) = &self.ghost { + ghost.gpu.get_or_init(|| { + // Large assemblies must also fit WebGL's buffer limit. + let limit = device.limits().max_buffer_size.min(16 * 1024 * 1024); + let count = + (limit as usize / std::mem::size_of::() / 3 * 3).max(3); + ghost + .vertices + .chunks(count) + .map(|vertices| { + let buffer = + device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Agent retained departing geometry"), + contents: bytemuck::cast_slice(vertices), + usage: wgpu::BufferUsages::VERTEX, + }); + (buffer, vertices.len() as u32) + }) + .collect() + }); + } + } + vec![] + } + fn paint( + &self, + _info: egui::PaintCallbackInfo, + pass: &mut wgpu::RenderPass<'static>, + resources: &egui_wgpu::CallbackResources, + ) { + if let (Some(pipelines), Some((_, bind))) = (resources.get::(), self.gpu.get()) { + pass.set_pipeline(if self.ghost.is_some() { + &pipelines.ghost + } else { + &pipelines.robot + }); + pass.set_bind_group(0, bind, &[]); + if let Some(ghost) = &self.ghost { + if let Some(buffers) = ghost.gpu.get() { + for (buffer, count) in buffers { + pass.set_vertex_buffer(0, buffer.slice(..)); + pass.draw(0..*count, 0..1); + } + } + } else { + pass.draw(0..6, 0..1); + } + } + } +} +impl Drop for Draw { + fn drop(&mut self) { + if let Some(buffers) = self.gpu.take() + && let Ok(mut pool) = self.pool.lock() + && pool.len() < 32 + { + pool.push(buffers); + } + } +} +struct TargetSrgb(bool); + +// Precision renderer packets share their retained geometry across animation +// frames. Each pane owns its uniform, so split views cannot overwrite cameras. +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct ProjectedUniform { + parameters: [f32; 4], + size: [f32; 4], +} +pub(super) struct Projected { + uniform: ProjectedUniform, + gpu: OnceLock<(wgpu::Buffer, wgpu::BindGroup)>, +} +struct ProjectedPipelines { + faces: wgpu::RenderPipeline, + materials: wgpu::RenderPipeline, + shadows: wgpu::RenderPipeline, + material_shadows: wgpu::RenderPipeline, + depth: wgpu::RenderPipeline, + edges: wgpu::RenderPipeline, + empty: wgpu::TextureView, + sampler: wgpu::Sampler, +} +pub(super) fn projected_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout { + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Agent precision shading parameters"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 2, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: wgpu::BufferSize::new( + std::mem::size_of::() as u64, + ), + }, + count: None, + }, + ], + }) +} +pub(super) fn install_projected( + device: &wgpu::Device, + format: wgpu::TextureFormat, + renderer: &mut egui_wgpu::Renderer, +) { + let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Agent precision scene shading"), + source: wgpu::ShaderSource::Wgsl( + concat!( + include_str!("surface_appearance.wgsl"), + "\n", + include_str!("agent_effects.wgsl"), + "\n", + include_str!("edges.wgsl") + ) + .into(), + ), + }); + let pipeline = |pass| create_pipeline_with_effects(device, &shader, format, pass, 1, true); + let empty = device + .create_texture(&wgpu::TextureDescriptor { + label: Some("Unused agent shadow mask"), + size: wgpu::Extent3d { + width: 1, + height: 1, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + .create_view(&Default::default()); + renderer.callback_resources.insert(ProjectedPipelines { + faces: pipeline(PipelinePass::Faces), + materials: pipeline(PipelinePass::MaterialFaces), + shadows: pipeline(PipelinePass::ShadowFaces), + material_shadows: pipeline(PipelinePass::MaterialShadowFaces), + depth: pipeline(PipelinePass::EdgeDepth), + edges: pipeline(PipelinePass::Edges), + empty, + sampler: device.create_sampler(&Default::default()), + }); +} +impl Projected { + pub(super) fn new(frame: &Frame, camera: &Camera, rect: Rect) -> Option { + frame.animated().then(|| Self { + uniform: ProjectedUniform { + parameters: frame.parameters(camera, rect), + size: [ + rect.width(), + rect.height(), + if frame.starts.is_empty() { 0. } else { 1. }, + 0., + ], + }, + gpu: OnceLock::new(), + }) + } + pub(super) fn prepare( + &self, + scene: &SceneCallback, + device: &wgpu::Device, + resources: &egui_wgpu::CallbackResources, + ) { + if let Some(pipelines) = resources.get::() { + self.gpu.get_or_init(|| { + let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Agent precision scene animation"), + contents: bytemuck::bytes_of(&self.uniform), + usage: wgpu::BufferUsages::UNIFORM, + }); + let view = scene + .antialiased + .get() + .map_or(&pipelines.empty, |mask| &mask.view); + let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Agent precision scene animation"), + layout: &pipelines.faces.get_bind_group_layout(0), + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(view), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::Sampler(&pipelines.sampler), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: buffer.as_entire_binding(), + }, + ], + }); + (buffer, bind) + }); + } + } + pub(super) fn paint( + &self, + scene: &SceneCallback, + pass: &mut wgpu::RenderPass<'static>, + resources: &egui_wgpu::CallbackResources, + ) -> bool { + let (Some(pipelines), Some((_, bind))) = + (resources.get::(), self.gpu.get()) + else { + return false; + }; + let materials = !scene.material_vertices.is_empty(); + let faces = match (scene.antialiased.get().is_some(), materials) { + (true, true) => &pipelines.material_shadows, + (true, false) => &pipelines.shadows, + (false, true) => &pipelines.materials, + (false, false) => &pipelines.faces, + }; + pass.set_bind_group(0, bind, &[]); + scene.draw(pass, [faces, &pipelines.depth, &pipelines.edges]); + true + } +} + +#[cfg(test)] +#[path = "agent_effects_tests.rs"] +mod tests; diff --git a/crates/benchup-app/src/agent_effects.wgsl b/crates/benchup-app/src/agent_effects.wgsl new file mode 100644 index 0000000..f39b5bd --- /dev/null +++ b/crates/benchup-app/src/agent_effects.wgsl @@ -0,0 +1,48 @@ +// Shared by retained geometry, precision geometry and departing snapshots. +// Time wraps every 4096 seconds so f32 uniforms retain millisecond precision. +fn agent_age(now: f32, started: f32) -> f32 { + let delta = now - started; + return delta - floor(delta / 4096.0) * 4096.0; +} +fn agent_hash(cell: vec2) -> f32 { + // Integer mixing avoids transcendental instructions in every fragment of + // the color, visibility, and ghost passes, and agrees across backends. + var hash = (bitcast(i32(cell.x)) * 0x9e3779b9u) ^ (bitcast(i32(cell.y)) * 0x85ebca6bu); + hash = (hash ^ (hash >> 16u)) * 0x7feb352du; + hash = (hash ^ (hash >> 15u)) * 0x846ca68bu; + return f32((hash ^ (hash >> 16u)) >> 8u) / 16777216.0; +} +fn agent_glyph(cell: vec2, pixel: vec2) -> f32 { + let p = abs(pixel - vec2(0.5)); + let one = p.x < 0.09 && p.y < 0.34; + let zero = p.x < 0.27 && p.y < 0.36 && (p.x > 0.14 || p.y > 0.23); + return select(0.0, 1.0, select(zero, one, agent_hash(cell) > 0.5)); +} +fn agent_reveal(stamp: f32, now: f32, uv: vec2, size: vec2) -> f32 { + if stamp < 0.0 { return 1.0; } + let progress = clamp(agent_age(now, stamp) / 0.95, 0.0, 1.0); + if progress >= 1.0 { return 1.0; } + let cell = floor(uv * size / 7.0); + let threshold = mix(0.0, 0.82, agent_hash(cell)) + (1.0 - uv.y) * 0.18; + return progress - threshold; +} +fn agent_surface(color: vec4, stamp: f32, uv: vec2, size: vec2, effect: vec4) -> vec4 { + if stamp < 0.0 && effect.y < 0.0 { return color; } + let reveal = agent_reveal(stamp, effect.x, uv, size); + if reveal < 0.0 { discard; } + var tint = 0.0; + if stamp >= 0.0 && agent_age(effect.x, stamp) < 0.95 { + // Pixel cells and small binary strokes resolve into the real material. + let pixel = fract(uv * size / 7.0); + let code = mix(0.18, 0.95, agent_glyph(floor(uv * size / 7.0), pixel)); + tint = (1.0 - smoothstep(0.0, 0.24, reveal)) * code; + } + if stamp >= -1.0 && effect.y >= 0.0 { + let height = max(effect.w - effect.z, 0.01); + let distance = abs(uv.y - effect.y) / height; + let band = exp(-distance * distance * 1700.0); + let line = 1.0 - smoothstep(0.0, 1.8 / max(size.y, 1.0), abs(uv.y - effect.y)); + tint = max(tint, band * 0.38 + line * 0.48); + } + return vec4(mix(color.rgb, vec3(0.12, 0.85, 1.0) * color.a, clamp(tint, 0.0, 0.9)), color.a); +} diff --git a/crates/benchup-app/src/agent_effects_tests.rs b/crates/benchup-app/src/agent_effects_tests.rs new file mode 100644 index 0000000..9bc23a3 --- /dev/null +++ b/crates/benchup-app/src/agent_effects_tests.rs @@ -0,0 +1,172 @@ +use super::*; + +#[test] +fn inserting_or_erasing_one_child_never_animates_the_surviving_assembly() { + let mut before = Model::default(); + let mut children = BTreeSet::new(); + for i in 0..4096 { + let edge = before + .add_edge(DVec3::Y * i as f64, DVec3::Y * i as f64 + DVec3::X) + .unwrap(); + children.insert(edge); + } + let owner = before.make_object(&children, false, "Assembly").unwrap(); + let mut after = before.clone(); + let added = after.add_edge(DVec3::Z, DVec3::Z + DVec3::X).unwrap(); + after + .objects + .get_mut(&owner) + .unwrap() + .entities + .insert(added); + let created = Change::between(&before, &after, &Default::default()); + assert_eq!(created.ids, BTreeSet::from([added])); + assert!( + created.ghost.is_none(), + "creation must not ghost the old assembly" + ); + let deleted = Change::between(&after, &before, &Default::default()); + assert_eq!(deleted.ids, BTreeSet::from([added])); + assert_eq!(deleted.ghost.as_ref().unwrap().vertices.len(), 6); +} + +#[test] +fn outliner_metadata_never_allocates_a_departing_snapshot() { + let mut before = Model::default(); + let edge = before.add_edge(DVec3::ZERO, DVec3::X).unwrap(); + let owner = before + .make_object(&BTreeSet::from([edge]), false, "Part") + .unwrap(); + let mut after = before.clone(); + after.objects.get_mut(&owner).unwrap().name = "Renamed".into(); + after.objects.get_mut(&owner).unwrap().outliner_order = Some(100); + let change = Change::between(&before, &after, &Default::default()); + assert!(change.ids.is_empty()); + assert!(change.ghost.is_none()); +} + +#[test] +fn rapid_edits_reuse_one_departing_snapshot_per_changed_region() { + let mut before = Model::default(); + let edge = before.add_edge(DVec3::ZERO, DVec3::X).unwrap(); + let mut after = before.clone(); + let vertex = after.edges[&edge].a; + after.vertices.get_mut(&vertex).unwrap().position.z = 1.; + let mut state = State::default(); + for i in 0..16 { + state.record( + Change::between(&before, &after, &Default::default()), + 10. + i as f64 * 0.01, + ); + } + assert_eq!(state.ghosts.len(), 1, "no sixteen-layer overdraw trail"); +} + +#[test] +fn precision_meshes_inherit_timestamps_for_assemblies_and_machined_surfaces() { + let mut model = Model::default(); + let edge = model.add_edge(DVec3::ZERO, DVec3::X).unwrap(); + let owner = model + .make_object(&BTreeSet::from([edge]), false, "Part") + .unwrap(); + let derived = model.add_edge(DVec3::Z, DVec3::Z + DVec3::X).unwrap(); + model + .routing_appearance + .by_source + .insert(edge, BTreeSet::from([derived])); + let mut frame = Frame { + starts: Arc::new(BTreeMap::from([(owner, 10.), (edge, 10.2)])), + ..Default::default() + }; + frame.remap(&model); + assert_eq!(frame.stamp(edge), timestamp(10.2)); + assert_eq!(frame.stamp(derived), timestamp(10.2)); + assert_eq!(frame.stamp(owner), timestamp(10.)); +} + +#[test] +fn changed_vertices_and_deleted_geometry_keep_independent_transition_times() { + let mut before = Model::default(); + let edge = before.add_edge(DVec3::ZERO, DVec3::X).unwrap(); + let untouched = before.add_edge(DVec3::Y, DVec3::Y + DVec3::X).unwrap(); + let mut moved = before.clone(); + let vertex = moved.edges[&edge].a; + moved.vertices.get_mut(&vertex).unwrap().position.z += 1.; + let change = Change::between(&before, &moved, &Default::default()); + assert!(change.ids.contains(&edge)); + assert!(!change.ids.contains(&untouched)); + assert!( + change.ghost.is_some(), + "retain the old position for dissolution" + ); + let mut state = State::default(); + state.record(change, 4095.8); + let frame = state.frame(4096.1, None, before.bounds()); + assert!((frame.stamp(edge) - 4095.8).abs() < 0.001); + assert_eq!(frame.stamp(untouched), -1.); + assert!((timestamp(frame.now) - 0.1).abs() < 0.001); + + let mut erased = moved.clone(); + erased.erase(&BTreeSet::from([edge])); + let change = Change::between(&moved, &erased, &Default::default()); + assert!(change.ghost.is_some(), "erase still has departing geometry"); + state.record(change, 4096.2); + let frame = state.frame(4096.8, None, erased.bounds()); + assert_eq!( + frame.ghosts.len(), + 1, + "the earlier transition expires independently" + ); + assert!((frame.stamp(edge) - 0.2).abs() < 0.001); + assert!(!state.frame(4097.2, None, None).animated()); +} + +#[test] +fn scan_runs_up_down_up_without_scanning_or_projecting_the_model() { + let mut camera = Camera::default(); + camera.fit( + Some((DVec3::splat(-1.), DVec3::splat(1.))), + DVec2::new(800., 600.), + ); + let rect = Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800., 600.)); + let frame = Frame { + now: 10., + activity: Some((Mode::Looking, 10.)), + bounds: Some((DVec3::splat(-1.), DVec3::splat(1.))), + ..Default::default() + }; + let mut ys = Vec::new(); + for second in 0..4 { + let frame = Frame { + now: 10. + second as f64, + ..frame.clone() + }; + let parameters = frame.parameters(&camera, rect); + ys.push(parameters[1]); + } + assert!(ys[0] > ys[1]); + assert!((ys[0] - ys[2]).abs() < 0.001); + assert!((ys[1] - ys[3]).abs() < 0.001); + let preparing = Frame { + activity: Some((Mode::Preparing, 10.)), + ..frame + }; + assert_eq!(preparing.parameters(&camera, rect)[1], -1.); +} + +#[test] +fn unchanged_and_hidden_geometry_do_not_leave_departing_ghosts() { + let mut model = Model::default(); + let edge = model.add_edge(DVec3::ZERO, DVec3::X).unwrap(); + let unchanged = Change::between(&model, &model, &Default::default()); + assert!(unchanged.ids.is_empty()); + assert!(unchanged.ghost.is_none()); + model.hidden.insert(edge); + let mut after = model.clone(); + after.erase(&BTreeSet::from([edge])); + assert!( + Change::between(&model, &after, &Default::default()) + .ghost + .is_none() + ); +} diff --git a/crates/benchup-app/src/agent_link.rs b/crates/benchup-app/src/agent_link.rs index b38955e..42bfe92 100644 --- a/crates/benchup-app/src/agent_link.rs +++ b/crates/benchup-app/src/agent_link.rs @@ -362,6 +362,14 @@ struct Active { phase: Phase, } +/// Explicit tasks span pauses; legacy callers only report individual calls. +struct Work { + activity: Activity, + since: f64, + updated: f64, + explicit: bool, +} + pub(super) struct AgentLink { pub status: Status, /// The pairing token, loaded from this device on demand. @@ -394,6 +402,10 @@ pub(super) struct AgentLink { last_change: Option, /// Session-only telemetry, cleared on disconnect or project replacement. last_activity: Option<(Activity, f64)>, + /// Whole tasks, including quiet planning between calls. Ended explicitly. + work: std::collections::BTreeMap, + /// Viewport feedback belongs to this connection and document only. + visuals: renderer::agent_effects::State, /// The last copy button pressed and until when "Copied" shows (session only). copied: Option<(ui::CopiedText, f64)>, /// The `render_view` being made on another thread: its call id, where @@ -468,6 +480,8 @@ impl AgentLink { denied: false, last_change: None, last_activity: None, + work: Default::default(), + visuals: Default::default(), copied: None, #[cfg(not(target_arch = "wasm32"))] rendering: None, @@ -550,6 +564,74 @@ impl AgentLink { } } + /// A task stays visible across arbitrary pauses between tool calls. A + /// live agent sends `agent_activity {phase: "finished"}` before replying. + fn observe_work(&mut self, client: &str, activity: Activity, now: f64) { + if self.work.len() >= QUEUE_LIMIT && !self.work.contains_key(client) { return; } + let work = self.work.entry(client.to_owned()).or_insert(Work { + activity, since: now, updated: now, explicit: false, + }); + if work.activity.viewport_mode() != activity.viewport_mode() { work.since = now; } + work.activity = activity; + work.updated = now; + } + + fn current_work(&self, now: f64) -> Option<(Activity, f64)> { + self.work.values() + .filter(|work| work.explicit || now - work.updated < ACTIVITY_LINGER) + .max_by(|a, b| a.updated.total_cmp(&b.updated)) + .map(|work| (work.activity, work.since)) + } + + fn report_activity(&mut self, client: &str, arguments: &Value, now: f64) -> (bool, Value) { + let phase = arguments.get("phase").and_then(Value::as_str); + if arguments.as_object().is_none_or(|object| object.len() != 1) + || !matches!(phase, Some("analyzing" | "preparing" | "finished")) + { + return (false, json!({"ok": false, "error": tr!("agent_link.error.activity_phase")})); + } + match phase.unwrap() { + "finished" => { + self.work.remove(client); + // A completed task cannot revive the short status/robot linger. + self.last_activity = None; + if self.work.is_empty() && !self.queue.iter().any(|queued| queued.changes) { + self.last_change = None; + if self.control == Control::Agent { self.control = Control::Shared; } + } + } + phase => { + if self.work.len() >= QUEUE_LIMIT && !self.work.contains_key(client) { + return (false, json!({"ok": false, "error": tr!("agent_link.error.busy")})); + } + self.observe_work(client, if phase == "analyzing" { Activity::Reading } else { Activity::Creating }, now); + if let Some(work) = self.work.get_mut(client) { work.explicit = true; } + self.last_activity = None; + } + } + (true, json!({"ok": true, "phase": phase.unwrap()})) + } + + pub(super) fn viewport_activity(&self, now: f64) -> Option<(renderer::agent_effects::Mode, f64)> { + if !self.connected() { return None; } + #[cfg(not(target_arch = "wasm32"))] + if self.rendering.as_ref().is_some_and(|(_, _, epoch)| *epoch == self.epoch) { + return Some((renderer::agent_effects::Mode::Looking, self.last_activity.map_or(now, |(_, at)| at))); + } + if let Some(queued) = self.queue.iter().find(|q| q.epoch == self.epoch && q.name != "agent_activity") { + return Some((Activity::for_call(&queued.name, &queued.arguments).viewport_mode(), queued.queued_at)); + } + if let Some((activity, since)) = self.current_work(now) { + return Some((activity.viewport_mode(), since)); + } + None + } + + pub(super) fn viewport_frame(&mut self, now: f64, bounds: Option<(DVec3, DVec3)>) -> renderer::agent_effects::Frame { + let activity = self.viewport_activity(now); + self.visuals.frame(now, activity, bounds) + } + /// Work the app can observe, including reads and long-running renders. /// Resolve labels here so changing the application language updates them. pub(super) fn activity_text(&self, now: f64) -> Option { @@ -570,7 +652,14 @@ impl AgentLink { } self.last_activity .filter(|(_, at)| now - at < ACTIVITY_LINGER) - .map(|(activity, _)| tr!("agent_link.activity.current", activity = activity.label())) + .map(|(activity, _)| activity) + .or_else(|| self.current_work(now).map(|(activity, _)| activity)) + .map(|activity| { + // The real render is handled above. A completed render is not + // evidence that the agent is still rendering during its planning. + let activity = if activity == Activity::Rendering { Activity::Working } else { activity }; + tr!("agent_link.activity.current", activity = activity.label()) + }) } fn send(&mut self, message: Value) { @@ -625,6 +714,8 @@ impl AgentLink { self.control = Control::Designer; self.last_change = None; self.last_activity = None; + self.visuals = Default::default(); + self.work.clear(); self.refuse_changes(tr!("agent_link.error.taken_control"), Some(tr!("agent_link.error.denied_hint"))); } /// The designer lets the agent edit again without asking. @@ -636,6 +727,8 @@ impl AgentLink { pub(super) fn deny(&mut self) { self.denied = true; self.last_activity = None; + self.visuals = Default::default(); + self.work.clear(); self.refuse_changes(tr!("agent_link.error.denied"), Some(tr!("agent_link.error.denied_hint"))); } @@ -645,6 +738,8 @@ impl AgentLink { self.queue.clear(); self.asking = false; self.last_activity = None; + self.visuals = Default::default(); + self.work.clear(); #[cfg(not(target_arch = "wasm32"))] { self.rendering = None; @@ -770,9 +865,12 @@ impl AgentLink { Err(message) => self.refuse(&id, &message, None), Ok(_) if self.queue.len() >= QUEUE_LIMIT => self.refuse(&id, tr!("agent_link.error.queue_full"), None), Ok((name, arguments)) => { - if !client.is_empty() { + if !client.is_empty() && !(name == "agent_activity" && arguments.get("phase").and_then(Value::as_str) == Some("finished")) { self.status = Status::Connected { client: Some(client.clone()) }; } + if name != "agent_activity" { + self.observe_work(&client, Activity::for_call(&name, &arguments), now); + } let changes = needs_grant(&name, &arguments); let epoch = self.epoch; self.queue.push_back(Queued { @@ -804,6 +902,7 @@ impl AgentLink { /// Drops calls past their deadline and answers those that waited longer /// than the bridge will. The only place deadlines are checked. fn expire(&mut self, now: f64) { + self.work.retain(|_, work| work.explicit || now - work.updated < ACTIVITY_LINGER); // The bridge has already answered calls that are about to pass their // deadline; running them would only change the model unseen. let limit = now_ms() + DEADLINE_MARGIN_MS; @@ -859,6 +958,8 @@ impl BenchUpApp { let prefs = self.general_settings.agent.clone(); if self.agent_link.epoch != self.doc_epoch { self.agent_link.last_activity = None; + self.agent_link.visuals = Default::default(); + self.agent_link.work.clear(); self.agent_link.epoch = self.doc_epoch; } self.agent_link.document_name = self.doc.name.clone(); @@ -960,8 +1061,17 @@ impl BenchUpApp { } } let selection_before = self.selected.clone(); - let (ok, result) = self.run_agent_request(&queued.name, &queued.arguments, ctx); - self.agent_link.last_activity = ok.then(|| (Activity::for_call(&queued.name, &queued.arguments), clock())); + let activity_report = queued.name == "agent_activity"; + let (ok, result) = if activity_report { + self.agent_link.report_activity(&queued.client, &queued.arguments, clock()) + } else { + self.run_agent_request(&queued.name, &queued.arguments, ctx) + }; + if !activity_report { + let activity = Activity::for_call(&queued.name, &queued.arguments); + if ok { self.agent_link.observe_work(&queued.client, activity, clock()); } + self.agent_link.last_activity = ok.then(|| (activity, clock())); + } // `select` or `undo` may change the selection; in a shared // project the server must lease it before edits can follow. self.selection_changed_outside_frame(&selection_before); @@ -1005,6 +1115,7 @@ impl BenchUpApp { let _ = tx.send(render_answer(&model, &arguments)); ctx.request_repaint(); }); + self.agent_link.last_activity = Some((Activity::Rendering, clock())); self.agent_link.rendering = Some((id, rx, self.doc_epoch)); } @@ -1219,9 +1330,13 @@ impl BenchUpApp { agent_restore_scope(&mut self.doc.model, scope); // One full comparison: the model is compared once here, not again // while recording. - if before != self.doc.model && !self.commit_changed_edit(label, before, ctx) { - // Shared edit access lapsed: rolled back whole. - return (json!({"ok": false, "error": self.shared_edit_block_reason(ctx)}), false); + if before != self.doc.model { + let transition = renderer::agent_effects::Change::between(&before, &self.doc.model, &self.doc.display); + if !self.commit_changed_edit(label, before, ctx) { + // Shared edit access lapsed: rolled back whole. + return (json!({"ok": false, "error": self.shared_edit_block_reason(ctx)}), false); + } + self.agent_link.visuals.record(transition, clock()); } self.focus_touched(created.into_iter().chain(modified)); if let Some(selection) = selection { @@ -1261,10 +1376,13 @@ impl BenchUpApp { return (false, json!({"ok": false, "error": self.shared_edit_block_reason(ctx)})); } let position = self.doc.history.position(); + let before = self.doc.model.clone(); self.undo(ctx, redo); if self.doc.history.position() == position { return (false, json!({"ok": false, "error": self.shared_edit_block_reason(ctx)})); } + let transition = renderer::agent_effects::Change::between(&before, &self.doc.model, &self.doc.display); + self.agent_link.visuals.record(transition, clock()); let key = if redo { "redone" } else { "undone" }; let mut result = json!({"ok": true}); result[key] = json!(label); diff --git a/crates/benchup-app/src/agent_link_activity.rs b/crates/benchup-app/src/agent_link_activity.rs index 348b588..7eaa01e 100644 --- a/crates/benchup-app/src/agent_link_activity.rs +++ b/crates/benchup-app/src/agent_link_activity.rs @@ -61,6 +61,15 @@ impl Activity { } } + pub(super) fn viewport_mode(self) -> crate::renderer::agent_effects::Mode { + use crate::renderer::agent_effects::Mode; + match self { + Self::Reading | Self::Finding | Self::Selecting | Self::Checking + | Self::Estimating | Self::Rendering => Mode::Looking, + _ => Mode::Preparing, + } + } + pub(super) fn label(self) -> &'static str { match self { Self::Reading => tr!("agent_link.activity.reading"), diff --git a/crates/benchup-app/src/agent_link_tests.rs b/crates/benchup-app/src/agent_link_tests.rs index 0a99c0d..561602f 100644 --- a/crates/benchup-app/src/agent_link_tests.rs +++ b/crates/benchup-app/src/agent_link_tests.rs @@ -1,6 +1,126 @@ use super::*; use std::sync::{Arc, Mutex}; +#[test] +fn legacy_call_activity_expires_and_never_claims_a_completed_render_is_running() { + let mut setup = Setup::paired(); + setup.app.agent_link.observe_work("legacy", Activity::Rendering, 100.); + setup.app.agent_link.last_activity = Some((Activity::Rendering, 100.)); + assert!(setup.app.agent_link.viewport_activity(100.).is_some()); + assert_eq!(setup.app.agent_link.activity_text(100.), Some("AI: Working on the project…".into())); + assert!(setup.app.agent_link.viewport_activity(100. + ACTIVITY_LINGER).is_none()); + assert!(setup.app.agent_link.activity_text(100. + ACTIVITY_LINGER).is_none()); + setup.app.agent_link.expire(100. + ACTIVITY_LINGER); + assert!(setup.app.agent_link.work.is_empty()); +} + +#[test] +fn completion_notification_clears_a_declared_task_after_its_last_render() { + let mut setup = Setup::paired(); + setup.app.agent_link.report_activity("codex-mcp-client", &json!({"phase": "analyzing"}), 100.); + setup.app.agent_link.observe_work("codex-mcp-client", Activity::Rendering, 101.); + setup.app.agent_link.last_activity = Some((Activity::Rendering, 101.)); + setup.app.agent_link.last_change = Some(101.); + setup.app.agent_link.control = Control::Agent; + assert!(setup.app.agent_link.viewport_activity(10_000.).is_some()); + assert_ne!(setup.app.agent_link.activity_text(10_000.), Some("AI: Rendering a view…".into())); + let (ok, _) = setup.app.agent_link.report_activity("codex-mcp-client", &json!({"phase": "finished"}), 10_001.); + assert!(ok); + assert!(setup.app.agent_link.viewport_activity(10_001.).is_none()); + assert!(setup.app.agent_link.activity_text(10_001.).is_none()); + assert_eq!(setup.app.agent_link.control, Control::Shared); + assert!(!setup.app.agent_link.editing(10_001.)); +} + +#[test] +fn work_reports_keep_the_robot_visible_through_long_pauses_and_finish_without_an_edit() { + use renderer::agent_effects::Mode; + let mut setup = Setup::paired(); + let history = setup.app.doc.history.position(); + let selection = setup.app.selected.clone(); + assert_eq!(setup.call("agent_activity", json!({"phase": "analyzing"}))["ok"], true); + let looking = setup.app.agent_link.viewport_activity(clock()).unwrap(); + assert_eq!(looking.0, Mode::Looking); + assert_eq!(setup.app.agent_link.viewport_activity(clock() + 7200.), Some(looking)); + assert_eq!(setup.call("agent_activity", json!({"phase": "preparing"}))["ok"], true); + let preparing = setup.app.agent_link.viewport_activity(clock()).unwrap(); + assert_eq!(preparing.0, Mode::Preparing); + assert_eq!(setup.app.agent_link.viewport_activity(clock() + 7200.), Some(preparing)); + assert_eq!(setup.call("agent_activity", json!({"phase": "finished"}))["ok"], true); + assert!(setup.app.agent_link.viewport_activity(clock()).is_none()); + assert!(!setup.app.agent_link.viewport_frame(clock(), None).animated()); + assert_eq!(setup.app.doc.history.position(), history); + assert_eq!(setup.app.selected, selection); + assert!(setup.app.doc.model.objects.is_empty()); +} + +#[test] +fn work_reports_are_validated_and_one_agent_cannot_finish_another_agents_work() { + let mut setup = Setup::paired(); + assert!(setup.app.agent_link.report_activity("first", &json!({"phase": "analyzing"}), 100.).0); + assert!(setup.app.agent_link.report_activity("second", &json!({"phase": "preparing"}), 101.).0); + for arguments in [json!({}), json!({"phase": "idle"}), json!({"phase": 1}), json!({"phase": "finished", "extra": true})] { + assert!(!setup.app.agent_link.report_activity("first", &arguments, 102.).0); + assert_eq!(setup.app.agent_link.work.len(), 2); + } + assert!(setup.app.agent_link.report_activity("second", &json!({"phase": "finished"}), 103.).0); + assert_eq!(setup.app.agent_link.work.len(), 1); + assert!(setup.app.agent_link.viewport_activity(10_000.).is_some()); + setup.app.agent_link.stop(); + assert!(setup.app.agent_link.work.is_empty()); + assert!(setup.app.agent_link.viewport_activity(10_000.).is_none()); +} + +#[test] +fn viewport_feedback_tracks_observed_work_and_document_lifetime() { + use renderer::agent_effects::Mode; + let mut setup = Setup::paired(); + assert!(setup.app.agent_link.viewport_activity(clock()).is_none()); + setup.call("agent_activity", json!({"phase": "analyzing"})); + setup.call("get_scene", json!({})); + let (_, at) = setup.app.agent_link.last_activity.unwrap(); + let looking = setup.app.agent_link.viewport_activity(at).unwrap(); + assert_eq!(looking.0, Mode::Looking); + assert_eq!(setup.app.agent_link.viewport_activity(at + 3600.), Some(looking)); + + setup.call("create_board", board("Animated", 0.)); + let (_, at) = setup.app.agent_link.last_activity.unwrap(); + assert_eq!(setup.app.agent_link.viewport_activity(at).unwrap().0, Mode::Preparing); + let frame = setup.app.agent_link.viewport_frame(at, None); + assert!(!frame.starts.is_empty()); + setup.app.agent_link.take_control(); + assert!(!setup.app.agent_link.viewport_frame(at, None).animated()); + + setup.app.agent_link.release_control(); + setup.call("create_board", board("Another", 700.)); + setup.app.doc_epoch += 1; + setup.poll(); + assert!(!setup.app.agent_link.viewport_frame(clock(), None).animated()); +} + +#[test] +fn failed_and_previewed_edits_never_start_geometry_transitions() { + let mut setup = Setup::paired(); + let calls = json!([{"tool": "create_board", "arguments": board("Preview", 0.)}]); + assert_eq!(setup.call("run_batch", json!({"calls": calls, "dry_run": true}))["ok"], true); + assert!(setup.app.agent_link.viewport_frame(clock(), None).starts.is_empty()); + let calls = json!([ + {"tool": "create_board", "arguments": board("Rolled back", 0.)}, + {"tool": "move", "arguments": {"ids": [999_999], "delta_mm": [1, 0, 0]}} + ]); + assert_eq!(setup.call("run_batch", json!({"calls": calls}))["ok"], false); + assert!(setup.app.agent_link.viewport_frame(clock(), None).starts.is_empty()); + assert!(setup.app.doc.model.objects.is_empty()); + + setup.app.line_start = Some(DVec3::ZERO); + let id = setup.send("create_board", board("Waiting", 0.)); + setup.poll(); + assert!(setup.app.agent_link.viewport_frame(clock(), None).starts.is_empty()); + setup.wire.push(json!({"type": "cancel", "id": id})); + setup.poll(); + assert!(setup.app.agent_link.viewport_frame(clock(), None).starts.is_empty()); +} + const TOKEN: &str = "6f1b0c8e-0d1a-4a52-9b0e-agent-test"; const BRIDGE_NONCE: &str = "abababababababababababababababababababababababababababababababab"; @@ -177,25 +297,28 @@ fn each_changing_call_is_one_undo_step_and_reads_add_none() { } #[test] -fn activity_includes_reads_and_returns_to_idle_without_another_call() { +fn activity_includes_reads_and_returns_to_idle_when_work_finishes() { let mut setup = Setup::paired(); assert!(setup.app.agent_link.activity_text(clock()).is_none()); + assert_eq!(setup.call("agent_activity", json!({"phase": "analyzing"}))["ok"], true); assert_eq!(setup.call("get_scene", json!({}))["ok"], true); let (_, at) = setup.app.agent_link.last_activity.unwrap(); assert_eq!(setup.app.agent_link.activity_text(at), Some("AI: Reading the project…".into())); - assert!(setup.app.agent_link.activity_text(at + ACTIVITY_LINGER).is_none()); + assert!(setup.app.agent_link.activity_text(at + 3600.).is_some()); assert!(!setup.app.agent_link.editing(at), "reading does not take editing control"); assert_eq!(setup.call("create_board", board("Side", 0.))["ok"], true); assert_eq!(setup.app.agent_link.activity_text(clock()), Some("AI: Creating geometry…".into())); - // A failed request must not leave a misleading description of work. + // A failed call does not finish the task: the AI may recover or explain it. assert_eq!(setup.call("get_object", json!({"id": u64::MAX}))["ok"], false); + assert!(setup.app.agent_link.activity_text(clock()).is_some()); + assert_eq!(setup.call("agent_activity", json!({"phase": "finished"}))["ok"], true); assert!(setup.app.agent_link.activity_text(clock()).is_none()); } #[test] -fn waiting_activity_clears_on_cancellation_denial_and_disconnect() { +fn waiting_task_survives_call_cancellation_and_clears_on_denial_or_disconnect() { let mut setup = Setup::paired(); setup.app.line_start = Some(DVec3::ZERO); let id = setup.send("create_board", board("Side", 0.)); @@ -203,7 +326,7 @@ fn waiting_activity_clears_on_cancellation_denial_and_disconnect() { assert_eq!(setup.app.agent_link.activity_text(clock()), Some("AI: Waiting for access — Creating geometry".into())); setup.wire.push(json!({"type": "cancel", "id": id})); setup.poll(); - assert!(setup.app.agent_link.activity_text(clock()).is_none()); + assert!(setup.app.agent_link.activity_text(clock()).is_some()); setup.send("create_board", board("Side", 0.)); setup.poll(); @@ -237,6 +360,7 @@ fn single_call_batches_describe_the_action_and_previews_are_identified() { #[test] fn rendering_activity_stays_visible_until_the_render_finishes() { let mut setup = Setup::paired(); + setup.call("agent_activity", json!({"phase": "analyzing"})); // Hold a render open deterministically without running a GPU render. let (tx, rx) = std::sync::mpsc::channel(); setup.app.agent_link.rendering = Some(("render".into(), rx, setup.app.doc_epoch)); @@ -249,8 +373,10 @@ fn rendering_activity_stays_visible_until_the_render_finishes() { setup.poll(); let (_, at) = setup.app.agent_link.last_activity.unwrap(); assert!(setup.app.agent_link.rendering.is_none()); - assert_eq!(setup.app.agent_link.activity_text(at), Some("AI: Rendering a view…".into())); - assert!(setup.app.agent_link.activity_text(at + ACTIVITY_LINGER).is_none()); + assert_eq!(setup.app.agent_link.activity_text(at), Some("AI: Working on the project…".into())); + assert!(setup.app.agent_link.activity_text(at + 3600.).is_some()); + assert_eq!(setup.call("agent_activity", json!({"phase": "finished"}))["ok"], true); + assert!(setup.app.agent_link.activity_text(clock()).is_none()); let (tx, rx) = std::sync::mpsc::channel(); setup.app.agent_link.rendering = Some(("old-project-render".into(), rx, setup.app.doc_epoch)); diff --git a/crates/benchup-app/src/agent_link_ui.rs b/crates/benchup-app/src/agent_link_ui.rs index 170d182..e807398 100644 --- a/crates/benchup-app/src/agent_link_ui.rs +++ b/crates/benchup-app/src/agent_link_ui.rs @@ -113,10 +113,9 @@ impl BenchUpApp { } } - /// The status-bar indicator: who is in control, and the button to take - /// or give it back. Nothing while no agent is connected. + /// The connected agent's activity and controls for taking or releasing control. pub(crate) fn agent_status_chip(&mut self, ui: &mut egui::Ui) { - if !self.agent_link.connected() { + if !self.general_settings.agent.enabled || !self.agent_link.connected() { return; } let colors = Palette::for_ui(ui); diff --git a/crates/benchup-app/src/agent_robot.wgsl b/crates/benchup-app/src/agent_robot.wgsl new file mode 100644 index 0000000..59c20e0 --- /dev/null +++ b/crates/benchup-app/src/agent_robot.wgsl @@ -0,0 +1,103 @@ +struct Uniform { + matrix: mat4x4, + viewport: vec4, + robot: vec4, + animation: vec4, + paper: vec4, +} +@group(0) @binding(0) var u: Uniform; +struct Output { + @builtin(position) position: vec4, + @location(0) uv: vec2, + @location(1) clip: vec3, + @location(2) @interpolate(flat) kind: u32, +} +fn agent_target(color: vec4) -> vec4 { + if u.viewport.w > 0.0 { + return vec4(finish_srgb_to_linear(color.rgb), color.a); + } + return color; +} +@vertex fn robot_vertex(@builtin(vertex_index) vertex: u32) -> Output { + let corners = array, 6>(vec2(-1.,-1.), vec2(1.,-1.), vec2(-1.,1.), vec2(-1.,1.), vec2(1.,-1.), vec2(1.,1.)); + let uv = corners[vertex]; + let bob = sin(u.animation.y * 2.5) * 2.0; + let pixel = u.robot.xy + vec2(0.0,bob) + uv * u.robot.z; + var out: Output; + out.position = vec4(pixel / u.viewport.xy * vec2(2.0,-2.0) + vec2(-1.0,1.0),0.0,1.0); + out.uv = uv; out.clip = vec3(0.0); out.kind = 0u; + return out; +} +fn box_distance(p: vec2, size: vec2, radius: f32) -> f32 { + let q = abs(p) - size + vec2(radius); + return min(max(q.x,q.y),0.0) + length(max(q,vec2(0.0))) - radius; +} +fn fill(distance: f32, aa: f32) -> f32 { return 1.0-smoothstep(-aa,aa,distance); } +@fragment fn robot_color(in: Output) -> @location(0) vec4 { + let p = in.uv; + let aa = 1.0 / (u.robot.z * u.viewport.z); + let blue = vec3(0.12,0.85,1.0); + let ink = vec3(0.04,0.15,0.21); + // Quiet circular badge, antenna, ears, head and two tracking eyes. + var color = u.paper.rgb; + var alpha = fill(length(p)-0.87,aa) * 0.94; + let ring = fill(abs(length(p)-0.84)-0.015,aa); + color = mix(color,blue,ring*0.55); + let antenna = min(box_distance(p-vec2(0.0,-0.51),vec2(0.018,0.1),0.01),length(p-vec2(0.0,-0.63))-0.055); + let ears = box_distance(vec2(abs(p.x)-0.46,p.y+0.08),vec2(0.055,0.13),0.035); + color = mix(color,blue,max(fill(antenna,aa),fill(ears,aa))); + let head = box_distance(p-vec2(0.0,-0.08),vec2(0.4,0.32),0.10); + color = mix(color,blue,fill(head,aa)); + let face = box_distance(p-vec2(0.0,-0.09),vec2(0.32,0.23),0.065); + color = mix(color,ink,fill(face,aa)); + let look = sin(u.animation.y*2.1)*0.06; + let blink = 0.022 + 0.06 * smoothstep(0.04,0.13,abs(sin(u.animation.y*1.8))); + let eye = box_distance(vec2(abs(p.x-look)-0.145,p.y+0.13),vec2(0.055,blink),0.018); + color = mix(color,blue,fill(eye,aa)); + let mouth = box_distance(p-vec2(0.0,0.035),vec2(0.10,0.016),0.01); + color = mix(color,blue,fill(mouth,aa)*0.75); + // Preparation uses a rotating, toothed gear under the same robot. + let q = p-vec2(0.0,0.51); + let radius = length(q); + if u.robot.w > 0.5 { + let angle = atan2(q.y,q.x) - u.animation.y*2.6; + let teeth = 0.175 + 0.033*smoothstep(-0.2,0.2,cos(angle*8.0)); + let gear = fill(radius-teeth,aa) * (1.0-fill(radius-0.072,aa)); + color = mix(color,blue,gear); + } else { + // A small radar arc follows the looking motion. + let angle = atan2(q.y,q.x) + u.animation.y*2.3; + let radar = fill(abs(radius-0.15)-0.018,aa) * smoothstep(-0.7,0.9,cos(angle)); + color = mix(color,blue,radar); + } + if alpha < 0.001 { discard; } + return agent_target(vec4(color,1.0)) * alpha; +} +@vertex fn ghost_vertex(@location(0) a: vec3, @location(1) b: vec3, @location(2) corner: vec2, @location(3) kind: u32) -> Output { + var out: Output; + var clip = u.matrix * vec4(a,1.0); + if kind == 1u { + var end = u.matrix * vec4(b,1.0); + if clip.z < 0.0 && end.z >= 0.0 { clip=mix(clip,end,clip.z/(clip.z-end.z)); } + if end.z < 0.0 && clip.z >= 0.0 { end=mix(end,clip,end.z/(end.z-clip.z)); } + let delta = (end.xy / max(end.w,0.00001) - clip.xy / max(clip.w,0.00001)) * u.viewport.xy; + let normal = vec2(-delta.y,delta.x)/max(length(delta),0.001); + clip = mix(clip,end,corner.x); + clip = vec4(clip.xy + normal*corner.y*2.0/u.viewport.xy*clip.w,clip.zw); + } + out.position=clip; out.clip=clip.xyw; out.uv=corner; out.kind=kind; + return out; +} +@fragment fn ghost_color(in: Output) -> @location(0) vec4 { + let uv = in.clip.xy / in.clip.z * vec2(0.5,-0.5) + vec2(0.5); + let progress = u.animation.x; + let cell = floor(uv * u.viewport.xy / 7.0); + let noise = agent_hash(cell); + if noise < progress { discard; } + let pixel = fract(uv * u.viewport.xy / 7.0); + let glyph = mix(0.2,0.9,agent_glyph(cell,pixel)); + var alpha = (1.0-progress)*select(0.32*glyph,0.8,in.kind==1u); + if in.kind==1u { alpha*=1.0-smoothstep(0.6,1.0,abs(in.uv.y)); } + let color = agent_target(vec4(1.0,0.48,0.27,1.0)); + return color * alpha; +} diff --git a/crates/benchup-app/src/app.rs b/crates/benchup-app/src/app.rs index b59ddd8..845cc80 100644 --- a/crates/benchup-app/src/app.rs +++ b/crates/benchup-app/src/app.rs @@ -5979,6 +5979,9 @@ impl BenchUpApp { if model.faces.len() >= 1024 { self.face_cache.update_for_revision(model, cache_revision); } + self.scene_cache.agent_effects = if self.history_preview.is_none() { + self.agent_link.viewport_frame(agent_link::clock(), self.render_model.bounds()) + } else { Default::default() }; let resident = key .as_ref() .filter(|key| renderer::resident_supported(key, model, &self.face_cache)); @@ -6049,6 +6052,7 @@ impl BenchUpApp { && self.tool == Tool::Move && !selected_entities.is_empty(); let mut mesh = renderer::Mesh::cached(camera, size, &mut self.scene_cache); + mesh.remap_agent_effects(model); mesh.set_scene_clip_planes(camera, self.render_model.bounds()); mesh.reference_image_depth_bounds(model); mesh.set_pixels_per_point(ui.ctx().pixels_per_point()); @@ -6436,6 +6440,8 @@ impl BenchUpApp { crate::reference_image_gpu::paint(&p, rect, model, entities, self.scene_cache.image_projection(), display, &self.images.gpu, self.image_appearance_preview()); + renderer::agent_effects::paint(&p, rect, camera, &self.scene_cache.agent_effects, colors.paper, + &self.scene_cache.agent_effects_cache); self.paint_fasteners(&p,rect,model); self.paint_joinery(&p, rect); let screen = |point: DVec3| view.project(point).map(|v| rect.min + pv(v)); diff --git a/crates/benchup-app/src/edges.wgsl b/crates/benchup-app/src/edges.wgsl index 84dbb20..8f9df8b 100644 --- a/crates/benchup-app/src/edges.wgsl +++ b/crates/benchup-app/src/edges.wgsl @@ -8,9 +8,10 @@ struct EdgeVertex { @location(5) grain: vec4, @location(6) @interpolate(flat) finish: vec4, @location(7) @interpolate(flat) srgb: u32, + @location(8) @interpolate(flat) effect: f32, } -fn edge_vertex(position: vec3, color: vec4, lateral: f32, shadow_color: vec4, dash_phase: f32, grain: vec4, finish: vec4, srgb: u32) -> EdgeVertex { +fn edge_vertex(position: vec3, color: vec4, lateral: f32, shadow_color: vec4, dash_phase: f32, grain: vec4, finish: vec4, srgb: u32, effect: f32) -> EdgeVertex { var output: EdgeVertex; output.position = vec4(position, 1.0); output.color = color; @@ -20,46 +21,47 @@ fn edge_vertex(position: vec3, color: vec4, lateral: f32, shadow_color output.grain = grain; output.finish = finish; output.srgb = srgb; + output.effect = effect; output.uv = position.xy * vec2(0.5, -0.5) + vec2(0.5); return output; } fn edge_vertex_srgb(position: vec3, color: vec4, lateral: f32, - shadow_color: vec4, dash_phase: f32) -> EdgeVertex { + shadow_color: vec4, dash_phase: f32, effect: f32) -> EdgeVertex { let linear = finish_srgb_to_linear(color.rgb); let shadow_linear = finish_srgb_to_linear(shadow_color.rgb); return edge_vertex(position, vec4(linear, color.a), lateral, - vec4(shadow_linear, shadow_color.a), dash_phase, vec4(0.0), vec4(0.0), 0u); + vec4(shadow_linear, shadow_color.a), dash_phase, vec4(0.0), vec4(0.0), 0u, effect); } @vertex fn vertex_main(@location(0) position: vec3, @location(1) color: vec4, @location(2) lateral: f32, - @location(3) shadow_color: vec4, @location(4) dash_phase: f32) -> EdgeVertex { - return edge_vertex(position, color, lateral, shadow_color, dash_phase, vec4(0.0), vec4(0.0), 0u); + @location(3) shadow_color: vec4, @location(4) dash_phase: f32, @location(7) effect: f32) -> EdgeVertex { + return edge_vertex(position, color, lateral, shadow_color, dash_phase, vec4(0.0), vec4(0.0), 0u, effect); } @vertex fn vertex_main_srgb(@location(0) position: vec3, @location(1) color: vec4, @location(2) lateral: f32, - @location(3) shadow_color: vec4, @location(4) dash_phase: f32) -> EdgeVertex { - return edge_vertex_srgb(position, color, lateral, shadow_color, dash_phase); + @location(3) shadow_color: vec4, @location(4) dash_phase: f32, @location(7) effect: f32) -> EdgeVertex { + return edge_vertex_srgb(position, color, lateral, shadow_color, dash_phase, effect); } @vertex fn vertex_material(@location(0) position: vec3, @location(1) color: vec4, @location(2) lateral: f32, @location(3) shadow_color: vec4, @location(4) dash_phase: f32, - @location(5) grain: vec4, @location(6) finish: vec4) -> EdgeVertex { - return edge_vertex(position, color, lateral, shadow_color, dash_phase, grain, finish, 0u); + @location(5) grain: vec4, @location(6) finish: vec4, @location(7) effect: f32) -> EdgeVertex { + return edge_vertex(position, color, lateral, shadow_color, dash_phase, grain, finish, 0u, effect); } @vertex fn vertex_material_srgb(@location(0) position: vec3, @location(1) color: vec4, @location(2) lateral: f32, @location(3) shadow_color: vec4, @location(4) dash_phase: f32, - @location(5) grain: vec4, @location(6) finish: vec4) -> EdgeVertex { + @location(5) grain: vec4, @location(6) finish: vec4, @location(7) effect: f32) -> EdgeVertex { // Finish shading uses the same color space as in the retained shader. if grain.w > 0.0 { - return edge_vertex(position, color, lateral, shadow_color, dash_phase, grain, finish, 1u); + return edge_vertex(position, color, lateral, shadow_color, dash_phase, grain, finish, 1u, effect); } - return edge_vertex_srgb(position, color, lateral, shadow_color, dash_phase); + return edge_vertex_srgb(position, color, lateral, shadow_color, dash_phase, effect); } fn stroke_coverage(input: EdgeVertex) -> f32 { @@ -234,3 +236,44 @@ fn grain_direction_coverage(packed: vec4, dx: vec4, dy: vec4, wid } return coverage; } + +// Precision display modes retain their projected buffers too. Only these +// fragment variants consume the live animation parameters; ordinary rendering +// and offscreen shadow masks keep their existing binding layouts. +struct AgentEffect { parameters: vec4, size: vec4 } +@group(0) @binding(2) var agent_effect: AgentEffect; +fn active_stamp(stamp: f32) -> f32 { + // Old retained vertices must not reassemble when the timer wraps later. + return select(min(stamp, -1.0), stamp, agent_effect.size.z > 0.0); +} +fn animated_color(input: EdgeVertex, color: vec4) -> vec4 { + return agent_surface(color, active_stamp(input.effect), input.uv, agent_effect.size.xy, agent_effect.parameters); +} +@fragment fn fragment_main_effect(input: EdgeVertex) -> @location(0) vec4 { + let coverage = stroke_coverage(input); + if coverage < 0.001 { discard; } + return animated_color(input, input.color) * coverage; +} +@fragment fn fragment_edge_depth_effect(input: EdgeVertex) -> @location(0) vec4 { + let coverage = stroke_coverage(input); + if input.position.z <= 0.0 || input.position.z >= 1.0 || input.color.a < 1.0 || coverage < 0.001 { discard; } + if agent_reveal(active_stamp(input.effect), agent_effect.parameters.x, input.uv, agent_effect.size.xy) < 0.0 { discard; } + return vec4(0.0); +} +@fragment fn fragment_material_faces_effect(input: EdgeVertex) -> @location(0) vec4 { + let coverage = stroke_coverage(input); + let color = shade_finish(input, input.color, 0.0); + if coverage < 0.001 { discard; } + return animated_color(input, color) * coverage; +} +@fragment fn fragment_shadow_faces_effect(input: EdgeVertex) -> @location(0) vec4 { + let coverage = textureSample(shadow_mask, shadow_sampler, input.uv).r; + let color = mix(input.color, input.shadow_color, coverage * select(0.0, 1.0, input.shadow_color.a > 0.0)); + return animated_color(input, color); +} +@fragment fn fragment_material_shadow_faces_effect(input: EdgeVertex) -> @location(0) vec4 { + let coverage = textureSample(shadow_mask, shadow_sampler, input.uv).r; + let amount = coverage * select(0.0, 1.0, input.shadow_color.a > 0.0); + let color = shade_finish(input, mix(input.color, input.shadow_color, amount), amount); + return animated_color(input, color); +} diff --git a/crates/benchup-app/src/renderer.rs b/crates/benchup-app/src/renderer.rs index 5a56aab..90b22f4 100644 --- a/crates/benchup-app/src/renderer.rs +++ b/crates/benchup-app/src/renderer.rs @@ -9,6 +9,8 @@ use std::sync::{Arc, Mutex, OnceLock}; mod resident; #[path = "scale_handles.rs"] mod scale_handles; +#[path = "agent_effects.rs"] +pub(crate) mod agent_effects; #[path = "scene_buffers.rs"] mod scene_buffers; use scene_buffers::GpuBuffers; @@ -60,6 +62,7 @@ struct Vertex { lateral: f32, shadow_color: [u8; 4], dash_phase: f32, + effect: f32, } /// Optional stream for the projected face path. Untextured scenes and all @@ -259,6 +262,8 @@ pub struct SceneKey { #[derive(Default)] pub struct SceneCache { pub(crate) scale_transform: Option, + pub(crate) agent_effects: agent_effects::Frame, + pub(crate) agent_effects_cache: agent_effects::Cache, image_matrix: Option, image_depth: Option<[f32; 4]>, resident: resident::Cache, @@ -323,7 +328,7 @@ impl SceneCache { if let Some(callback) = &self.retained { painter.add(egui_wgpu::Callback::new_paint_callback( rect, - SharedScene(Arc::clone(callback)), + SharedScene(Arc::clone(callback), agent_effects::Projected::new(&self.agent_effects, &key.camera, rect)), )); if let Some((_, _, annotations)) = &self.annotations { painter.extend(annotations.iter().cloned()); @@ -390,6 +395,7 @@ struct CachedJointSurface { } pub struct Mesh { + agent_effects: agent_effects::Frame, joint_guide_bounds: BTreeMap, visible_joint_surfaces: BTreeMap, Arc, (u64, u64))>, joint_surfaces: JointSurfaceCache, @@ -447,6 +453,7 @@ impl Mesh { } pub fn new(camera: &Camera, size: DVec2) -> Self { Self { + agent_effects: Default::default(), joint_guide_bounds: BTreeMap::new(), visible_joint_surfaces: BTreeMap::new(), joint_surfaces: JointSurfaceCache { @@ -477,6 +484,9 @@ impl Mesh { gpu_pool: Default::default(), } } + pub(crate) fn remap_agent_effects(&mut self, model: &Model) { + self.agent_effects.remap(model); + } pub fn cached(camera: &Camera, size: DVec2, cache: &mut SceneCache) -> Self { cache.key = None; #[cfg(test)] @@ -484,6 +494,7 @@ impl Mesh { cache.mesh_builds += 1; } let mut mesh = Self::new(camera, size); + mesh.agent_effects = cache.agent_effects.clone(); mesh.joint_surfaces = std::mem::take(&mut cache.joint_surfaces); mesh.joint_surfaces.used.clear(); if mesh.joint_surfaces.camera != *camera || mesh.joint_surfaces.size != size { @@ -1019,6 +1030,8 @@ impl Mesh { fn draw_face(&mut self, face: &FaceDraw<'_>) { let start = self.vertices.len(); self.draw_face_flat(face); + let stamp = self.agent_effects.stamp(face.face.id); + for vertex in &mut self.vertices[start..] { vertex.effect = stamp; } let Some((model, lighting)) = face.smooth else { return }; // The precision path already projects/clips faces on the CPU. Adjust // only the routed vertices it produced; the retained path interpolates @@ -1113,6 +1126,7 @@ impl Mesh { // Rebuilding the default view puts them in different depth ranges. mesh.view = self.view.clone(); mesh.display = self.display.clone(); + mesh.agent_effects = self.agent_effects.clone(); (chunk, mesh) }) .collect(); @@ -1179,6 +1193,7 @@ impl Mesh { let surfaces = std::mem::take(&mut self.edge_surfaces); self.paint_overlays_with_outlines(edges, 128, self.clip_stroke_footprints, |mesh, edge| { mesh.set_entity(Some(edge.id)); + let start = mesh.vertices.len(); let [a, b] = edge.points; if edge.back { mesh.overlay_line(a, b, 1., edge.color.gamma_multiply(0.4), true); @@ -1201,6 +1216,8 @@ impl Mesh { ); } } + let stamp = mesh.agent_effects.stamp(edge.id); + for vertex in &mut mesh.vertices[start..] { vertex.effect = stamp; } }); self.edge_surfaces = surfaces; } @@ -1297,6 +1314,7 @@ impl Mesh { fn overlay_worker(&self, read_outlines: bool) -> Self { let mut mesh = Self::new(&self.joint_surfaces.camera, self.size); mesh.view = self.view.clone(); + mesh.agent_effects = self.agent_effects.clone(); mesh.display = self.display.clone(); mesh.pixels_per_point = self.pixels_per_point; mesh.gpu_edge_occlusion = self.gpu_edge_occlusion; @@ -1494,6 +1512,7 @@ impl Mesh { finish: [254, 255, 254, (width * self.pixels_per_point * 255. / 8.).round().clamp(1., 255.) as u8], }); self.vertices.push(Vertex { + effect: -2., position: [(p.x / self.size.x * 2. - 1.) as f32, (1. - p.y / self.size.y * 2.) as f32, p.z.clamp(0., 1.) as f32], // Match the underlying surface's vertices and depth exactly. @@ -1685,6 +1704,7 @@ impl Mesh { for &(endpoint, lateral) in strip { let p = [a, b][endpoint] + n * lateral; self.vertices.push(Vertex { + effect: -2., dash_phase: -1., position: [ (p.x / self.size.x * 2. - 1.) as f32, @@ -1781,6 +1801,7 @@ impl Mesh { for &p in polygon { let depth = origin.z + gradient.dot(p - origin.truncate()); self.vertices.push(Vertex { + effect: -2., position: [ (p.x / self.size.x * 2. - 1.) as f32, (1. - p.y / self.size.y * 2.) as f32, @@ -2047,6 +2068,7 @@ impl Mesh { for (endpoint, lateral) in [(0, 1.), (0, -1.), (1, 1.), (1, -1.)] { let p = [a, b][endpoint] + offset * lateral; self.vertices.push(Vertex { + effect: -2., dash_phase: -1., position: [ (p.x / self.size.x * 2. - 1.) as f32, @@ -2126,6 +2148,7 @@ impl Mesh { view.for_each_triangle_depth([polygon[0], polygon[i], polygon[i + 1]], |triangle| { for p in triangle { vertices.push(Vertex { + effect: -2., dash_phase: -1., position: [ (p.x / size.x * 2. - 1.) as f32, @@ -2245,6 +2268,7 @@ impl Mesh { }); } vertices.push(Vertex { + effect: -2., dash_phase: -1., position: [ (p.x / size.x * 2. - 1.) as f32, @@ -2275,10 +2299,11 @@ impl Mesh { self.edge_surfaces .retain(|_, surfaces| !surfaces.is_empty()); cache.edge_surfaces = std::mem::take(&mut self.edge_surfaces); + let effects = agent_effects::Projected::new(&cache.agent_effects, &self.joint_surfaces.camera, rect); let callback = self.retain(cache); painter.add(egui_wgpu::Callback::new_paint_callback( rect, - SharedScene(callback), + SharedScene(callback, effects), )); } @@ -3436,7 +3461,7 @@ pub(crate) fn install_resources( ) { let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("BenchUp edges"), - source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("edges.wgsl")).into()), + source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("agent_effects.wgsl"), "\n", include_str!("edges.wgsl")).into()), }); let face_pipeline = create_pipeline(device, &shader, format, PipelinePass::Faces); let edge_pipeline = create_pipeline(device, &shader, format, PipelinePass::Edges); @@ -3471,6 +3496,8 @@ pub(crate) fn install_resources( resident::install(device, format, renderer); crate::reference_image_gpu::install(device, format, renderer); scale_handles::install(device, format, renderer); + agent_effects::install(device, format, renderer); + agent_effects::install_projected(device, format, renderer); } #[derive(Clone, Copy, PartialEq)] enum PipelinePass { @@ -3499,9 +3526,16 @@ fn create_pipeline_with_samples( format: wgpu::TextureFormat, pass: PipelinePass, samples: u32, +) -> wgpu::RenderPipeline { + create_pipeline_with_effects(device, shader, format, pass, samples, false) +} + +fn create_pipeline_with_effects( + device: &wgpu::Device, shader: &wgpu::ShaderModule, format: wgpu::TextureFormat, + pass: PipelinePass, samples: u32, effects: bool, ) -> wgpu::RenderPipeline { let material = matches!(pass, PipelinePass::MaterialFaces | PipelinePass::MaterialShadowFaces); - let attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Unorm8x4,2=>Float32,3=>Unorm8x4,4=>Float32]; + let attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Unorm8x4,2=>Float32,3=>Unorm8x4,4=>Float32,7=>Float32]; let material_attributes = wgpu::vertex_attr_array![5=>Float32x4,6=>Unorm8x4]; let mut layouts = vec![Some(wgpu::VertexBufferLayout { array_stride: std::mem::size_of::() as u64, @@ -3515,6 +3549,10 @@ fn create_pipeline_with_samples( attributes: &material_attributes, })); } + let effect_layout = effects.then(|| agent_effects::projected_layout(device)); + let layout = effect_layout.as_ref().map(|group| device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Agent precision geometry layout"), bind_group_layouts: &[Some(group)], immediate_size: 0, + })); device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some(match pass { PipelinePass::Faces => "BenchUp filled faces", @@ -3526,7 +3564,7 @@ fn create_pipeline_with_samples( PipelinePass::MaskShadow => "BenchUp shadow coverage", PipelinePass::ShadowFaces => "BenchUp faces with antialiased shadows", }), - layout: None, + layout: layout.as_ref(), vertex: wgpu::VertexState { module: shader, entry_point: Some(match (material, format.is_srgb()) { @@ -3540,13 +3578,18 @@ fn create_pipeline_with_samples( }, fragment: Some(wgpu::FragmentState { module: shader, - entry_point: Some(match pass { - PipelinePass::EdgeDepth => "fragment_edge_depth", - PipelinePass::MaskBase => "fragment_mask_base", - PipelinePass::MaskShadow => "fragment_mask_shadow", - PipelinePass::ShadowFaces => "fragment_shadow_faces", - PipelinePass::MaterialFaces => "fragment_material_faces", - PipelinePass::MaterialShadowFaces => "fragment_material_shadow_faces", + entry_point: Some(match (pass, effects) { + (PipelinePass::EdgeDepth, true) => "fragment_edge_depth_effect", + (PipelinePass::ShadowFaces, true) => "fragment_shadow_faces_effect", + (PipelinePass::MaterialFaces, true) => "fragment_material_faces_effect", + (PipelinePass::MaterialShadowFaces, true) => "fragment_material_shadow_faces_effect", + (_, true) => "fragment_main_effect", + (PipelinePass::EdgeDepth, false) => "fragment_edge_depth", + (PipelinePass::MaskBase, false) => "fragment_mask_base", + (PipelinePass::MaskShadow, false) => "fragment_mask_shadow", + (PipelinePass::ShadowFaces, false) => "fragment_shadow_faces", + (PipelinePass::MaterialFaces, false) => "fragment_material_faces", + (PipelinePass::MaterialShadowFaces, false) => "fragment_material_shadow_faces", _ => "fragment_main", }), targets: &[Some(wgpu::ColorTargetState { @@ -3580,6 +3623,7 @@ fn create_pipeline_with_samples( struct AntialiasedScene { bind_group: wgpu::BindGroup, + view: wgpu::TextureView, } struct SceneCallback { @@ -3596,7 +3640,7 @@ struct SceneCallback { antialiased: OnceLock, } -struct SharedScene(Arc); +struct SharedScene(Arc, Option); impl egui_wgpu::CallbackTrait for SharedScene { fn prepare( @@ -3607,7 +3651,9 @@ impl egui_wgpu::CallbackTrait for SharedScene { encoder: &mut wgpu::CommandEncoder, resources: &mut egui_wgpu::CallbackResources, ) -> Vec { - self.0.prepare(device, queue, screen, encoder, resources) + let commands = self.0.prepare(device, queue, screen, encoder, resources); + if let Some(effects) = &self.1 { effects.prepare(&self.0, device, resources); } + commands } fn paint( @@ -3616,7 +3662,9 @@ impl egui_wgpu::CallbackTrait for SharedScene { pass: &mut wgpu::RenderPass<'static>, resources: &egui_wgpu::CallbackResources, ) { - self.0.paint(info, pass, resources); + if !self.1.as_ref().is_some_and(|effects| effects.paint(&self.0, pass, resources)) { + self.0.paint(info, pass, resources); + } } } @@ -3749,7 +3797,7 @@ impl egui_wgpu::CallbackTrait for SceneCallback { }, ], }); - AntialiasedScene { bind_group } + AntialiasedScene { bind_group, view: resolved } }); } vec![] @@ -4594,7 +4642,7 @@ mod tests { pollster::block_on(adapter.request_device(&Default::default())).unwrap(); let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: None, - source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("edges.wgsl")).into()), + source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("agent_effects.wgsl"), "\n", include_str!("edges.wgsl")).into()), }); let pipelines = [ PipelinePass::Faces, @@ -5799,6 +5847,7 @@ mod tests { [-0.8, 0.8, 0.5], ] .map(|position| Vertex { + effect: -2., dash_phase: -1., position, color: [255, 0, 0, 255], @@ -5813,6 +5862,7 @@ mod tests { [0.1, 0.6, 0.5_f32.next_down()], ] .map(|position| Vertex { + effect: -2., dash_phase: -1., position, color: if enabled { [255; 4] } else { [0, 0, 0, 255] }, @@ -5878,6 +5928,7 @@ mod tests { let y1 = -0.8 + 1.6 * (y + 1) as f32 / 48.; for [x, y] in [[x0, y0], [x1, y0], [x1, y1], [x0, y0], [x1, y1], [x0, y1]] { fragmented_mesh.shadow_vertices.push(Vertex { + effect: -2., dash_phase: -1., position: [x, y, depth(x, y)], color: [255; 4], @@ -6195,6 +6246,7 @@ mod tests { mesh.vertices = depths .into_iter() .map(|z| Vertex { + effect: -2., dash_phase: -1., position: [0., 0., z], color: Color32::WHITE.to_array(), @@ -6341,6 +6393,7 @@ mod tests { material_vertices: Vec::new(), shadow_range: 1..1, vertices: vec![Vertex { + effect: -2., dash_phase: -1., position: [0., 0., 0.5], color: [255; 4], @@ -6413,7 +6466,7 @@ mod tests { pollster::block_on(adapter.request_device(&Default::default())).unwrap(); let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: None, - source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("edges.wgsl")).into()), + source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("agent_effects.wgsl"), "\n", include_str!("edges.wgsl")).into()), }); let format = wgpu::TextureFormat::Rgba8Unorm; let pipelines = [PipelinePass::EdgeDepth, PipelinePass::Edges] @@ -6588,7 +6641,7 @@ mod tests { pollster::block_on(adapter.request_device(&Default::default())).unwrap(); let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: None, - source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("edges.wgsl")).into()), + source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("agent_effects.wgsl"), "\n", include_str!("edges.wgsl")).into()), }); let format = wgpu::TextureFormat::Rgba8Unorm; let pipelines = [PipelinePass::EdgeDepth, PipelinePass::Edges] diff --git a/crates/benchup-app/src/resident_appearance.rs b/crates/benchup-app/src/resident_appearance.rs index 31f7d6d..44c4f9b 100644 --- a/crates/benchup-app/src/resident_appearance.rs +++ b/crates/benchup-app/src/resident_appearance.rs @@ -11,6 +11,7 @@ pub(super) const OUTLINER: u32 = 8; pub(super) const LOCKED: u32 = 16; pub(super) const ERASING: u32 = 32; pub(super) const REMOVED: u32 = 64; +const ASSEMBLING: u32 = 128; #[derive(Default)] pub(super) struct Cache { key: Option, @@ -24,6 +25,7 @@ struct Key { context: Option, erasing: BTreeSet, removed: BTreeSet, + assembling: Arc>, } #[derive(Clone)] pub(super) struct Hierarchy { @@ -154,11 +156,16 @@ impl Hierarchy { self.slot(id), ] } - pub fn update( + #[cfg(test)] + pub fn update(&self, cache: &mut Cache, key: &SceneKey, removed: &BTreeSet) -> Arc> { + self.update_with_effects(cache, key, removed, &Default::default()) + } + pub fn update_with_effects( &self, cache: &mut Cache, key: &SceneKey, removed: &BTreeSet, + assembling: &Arc>, ) -> Arc> { // Primitive hover is already a uniform lookup in the vertex shader. // Only assembly hover needs inherited state in the interaction texture. @@ -166,14 +173,15 @@ impl Hierarchy { if cache.key.as_ref().is_some_and(|previous| previous.hover == hover && previous.context == key.edit_context && previous.selected == key.selected && previous.outliner == key.outliner_hover - && previous.erasing == key.erasing && previous.removed == *removed) + && previous.erasing == key.erasing && previous.removed == *removed + && previous.assembling == *assembling) { return Arc::clone(&cache.values); } let next = Key { selected: key.selected.clone(), hover, outliner: key.outliner_hover.clone(), context: key.edit_context, - erasing: key.erasing.clone(), removed: removed.clone(), + erasing: key.erasing.clone(), removed: removed.clone(), assembling: assembling.clone(), }; if cache.values.len() != self.base.len() { cache.values = Arc::new(self.base.clone()); @@ -186,6 +194,7 @@ impl Hierarchy { .chain(&previous.outliner) .chain(&previous.erasing) .chain(&previous.removed) + .chain(previous.assembling.keys()) { let slot = self.slot(*id) as usize; if slot != 0 { @@ -235,6 +244,13 @@ impl Hierarchy { } } } + for (&id, &at) in assembling.iter() { + let slot = self.slot(id) as usize; + if slot != 0 { + let millis = (agent_effects::timestamp(at) * 1000.).round() as u32; + flags[slot] |= ASSEMBLING | (millis << 8); + } + } cache.key = Some(next); Arc::clone(&cache.values) } diff --git a/crates/benchup-app/src/resident_scene.rs b/crates/benchup-app/src/resident_scene.rs index d3cd962..a6664a9 100644 --- a/crates/benchup-app/src/resident_scene.rs +++ b/crates/benchup-app/src/resident_scene.rs @@ -84,6 +84,7 @@ struct Uniform { marker_fill: [f32; 4], scale_pivot: [f32; 4], scale_factors: [f32; 4], + agent_effect: [f32; 4], } #[derive(Default)] pub(super) struct Cache { @@ -644,10 +645,11 @@ impl SceneCache { let toward_eye = (eye - key.camera.target).normalize(); let light = (toward_eye * 0.8 - key.camera.right() * 0.45 + key.camera.up() * 0.65).normalize(); - let appearance = self.resident.hierarchy.as_ref().unwrap().update( + let appearance = self.resident.hierarchy.as_ref().unwrap().update_with_effects( &mut self.resident.appearance, key, &self.resident.removed, + &self.agent_effects.starts, ); if self.resident.state_capacity != appearance.len() { self.resident.states = Arc::new(OnceLock::new()); @@ -720,6 +722,7 @@ impl SceneCache { (scale.pivot - geometry.origin).extend(1.).as_vec4().to_array()), scale_factors: self.scale_transform.map_or([1.; 4], |scale| scale.factors.extend(0.).as_vec4().to_array()), + agent_effect: self.agent_effects.parameters(&key.camera, rect), flags: [ 0., if key.camera.projection == benchup_core::Projection::Parallel { @@ -1011,7 +1014,7 @@ pub(super) fn install( ) { let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("BenchUp resident world geometry"), - source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("resident_scene.wgsl")).into()), + source: wgpu::ShaderSource::Wgsl(concat!(include_str!("surface_appearance.wgsl"), "\n", include_str!("agent_effects.wgsl"), "\n", include_str!("resident_scene.wgsl")).into()), }); let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("Resident camera"), @@ -1105,7 +1108,7 @@ pub(super) fn install( fragment: Some(wgpu::FragmentState { module: &shader, entry_point: Some(if visibility { - "fragment_visibility" + "fragment_visibility_effect" } else if depth { "edge_depth" } else if edge { @@ -1512,8 +1515,9 @@ mod tests { ..Default::default() }; let shared = include_str!("surface_appearance.wgsl"); - for shader in [include_str!("resident_scene.wgsl"), include_str!("edges.wgsl")] { - let source = format!("{shared}\n{shader}"); + for shader in [include_str!("resident_scene.wgsl"), include_str!("edges.wgsl"), include_str!("agent_robot.wgsl")] { + let effects = include_str!("agent_effects.wgsl"); + let source = format!("{shared}\n{effects}\n{shader}"); let module = wgsl::parse_str(&source).unwrap(); let info = valid::Validator::new(Default::default(), Default::default()) .validate(&module) @@ -2028,6 +2032,59 @@ mod tests { assert_eq!(flags(&values, face) & (ERASING | REMOVED), 0); } + #[test] + fn agent_animation_and_selection_retain_large_scene_geometry() { + let model = model(4096); + let mut entities = EntityCache::default(); + let mut faces = FaceCache::default(); + entities.update(&model); + faces.update(&model); + let mut cache = SceneCache::default(); + let mut key = key(); + let ctx = egui::Context::default(); + let rect = Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800., 600.)); + let mut geometry = None; + let mut animation = agent_effects::State::default(); + for (now, mode) in [(5., Some(agent_effects::Mode::Looking)), + (5.3, Some(agent_effects::Mode::Looking)), + (5.6, Some(agent_effects::Mode::Preparing)), (6.1, None)] { + key.selected = model.faces.keys().take(if now > 5. { 10 } else { 0 }).copied().collect(); + cache.agent_effects = animation.frame(now, mode.map(|mode| (mode, 5.)), model.bounds()); + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| { + let references = cache.resident_reference_mesh(&key, &model); + cache.paint_resident(&key, &model, &faces, &entities, ui, rect, references); + }); + output.textures_delta.clear(); + let current = cache.resident.geometry.as_ref().unwrap(); + if let Some(previous) = &geometry { assert!(Arc::ptr_eq(previous, current)); } + geometry = Some(current.clone()); + assert_eq!(cache.resident_builds(), 1); + } + } + + #[test] + fn agent_timestamps_update_appearance_without_rebuilding_geometry_and_restore_cleanly() { + let model = model(1); + let mut entities = EntityCache::default(); + let mut faces = FaceCache::default(); + entities.update(&model); + faces.update(&model); + let hierarchy = appearance::Hierarchy::new(&model, &entities, &faces); + let mut cache = appearance::Cache::default(); + let key = key(); + let id = *model.faces.keys().next().unwrap(); + let slot = hierarchy.slot(id) as usize; + let starts = Arc::new(BTreeMap::from([(id, 4095.5)])); + let values = hierarchy.update_with_effects(&mut cache, &key, &Default::default(), &starts); + assert_eq!(values[slot] & 128, 128); + assert_eq!(values[slot] >> 8, 4_095_500); + assert!(Arc::ptr_eq(&values, + &hierarchy.update_with_effects(&mut cache, &key, &Default::default(), &starts))); + let restored = hierarchy.update(&mut cache, &key, &Default::default()); + assert_eq!(restored[slot] & 128, 0); + assert_eq!(restored[slot] >> 8, 0); + } + #[test] fn visibility_face_ids_address_interaction_states_even_with_hidden_faces() { let mut model = model(1024); diff --git a/crates/benchup-app/src/resident_scene.wgsl b/crates/benchup-app/src/resident_scene.wgsl index cac82bc..88f8e48 100644 --- a/crates/benchup-app/src/resident_scene.wgsl +++ b/crates/benchup-app/src/resident_scene.wgsl @@ -20,6 +20,7 @@ struct Camera { marker_fill: vec4, scale_pivot: vec4, scale_factors: vec4, + agent_effect: vec4, } @group(0) @binding(0) var camera: Camera; @group(0) @binding(1) var interaction_states: texture_2d; @@ -34,6 +35,18 @@ fn interaction_state(pair: vec2) -> u32 { if (state&8u)!=0u {state|=2u;} return state; } +fn agent_stamp(pair: vec2) -> f32 { + let own = slot_state(pair.y); + let owner = slot_state(pair.x); + let state = select(owner, own, (own&128u)!=0u); + return select(-1.0, f32(state>>8u)*0.001, (state&128u)!=0u); +} +fn agent_uv(clip: vec3) -> vec2 { + return clip.xy / clip.z * vec2(0.5,-0.5) + vec2(0.5); +} +fn agent_color(color: vec4, stamp: f32, clip: vec3) -> vec4 { + return agent_surface(color, stamp, agent_uv(clip), camera.viewport.xy, camera.agent_effect); +} fn preview_factors(state: u32) -> vec3 { return select(vec3(1.0), camera.scale_factors.xyz, camera.scale_pivot.w > 0.0 && (state&1u)!=0u); @@ -53,12 +66,16 @@ struct Output { @location(2) center: vec3, @location(3) @interpolate(flat) faces: vec4, @location(4) shadow: vec3, + @location(7) @interpolate(flat) stamp: f32, + @location(8) clip: vec3, } struct FaceOutput { @builtin(position) position: vec4, @location(0) color: vec4, @location(3) @interpolate(flat) faces: vec4, @location(4) shadow: vec3, + @location(7) @interpolate(flat) stamp: f32, + @location(8) clip: vec3, @location(5) grain: vec3, @location(6) @interpolate(flat) finish: vec4, } @@ -101,6 +118,8 @@ fn surface_vertex(position: vec3, normal: vec3, anchor: vec3, col var output: FaceOutput; output.position = camera.matrix * vec4(preview_point(position,state),1.0); if ((state&64u)!=0u && (camera.interaction.w&4u)==0u) || (camera.interaction.y!=0u && (state&4u)==0u) {output.position=vec4(2.0,2.0,2.0,1.0);} + output.stamp = agent_stamp(interaction); + output.clip = output.position.xyw; output.color = shaded; output.shadow = (camera.shadow_matrix * vec4(preview_point(position,state),1.0)).xyz; output.grain = grain; @@ -131,6 +150,8 @@ fn shadow_vertex(@location(0) position: vec3, @location(4) face_id: u32, output.position=vec4(2.0,2.0,2.0,1.0); } output.faces = vec4(face_id,0u,0u,0u); + output.stamp = -2.0; + output.clip = output.position.xyw; return output; } // Evaluate the receiving plane at each shadow texel rather than adding a large @@ -168,7 +189,7 @@ fn face_color(input: FaceOutput) -> @location(0) vec4 { let amount = shadow_coverage(input); let dark = floor(input.color.rgb * (1.0-camera.shadow_params.x) * 255.0 + vec3(0.5))/255.0; let rgb = mix(input.color.rgb,dark,amount); - return target_color(vec4(rgb, input.color.a)); + return target_color(agent_color(vec4(rgb, input.color.a), input.stamp, input.clip)); } @fragment fn material_color(input: FaceOutput) -> @location(0) vec4 { @@ -176,7 +197,7 @@ fn material_color(input: FaceOutput) -> @location(0) vec4 { let dark = floor(input.color.rgb * (1.0-camera.shadow_params.x) * 255.0 + vec3(0.5))/255.0; let rgb = mix(input.color.rgb,dark,amount); let finish = vec4(input.finish.xyz, input.finish.w * (1.0 - amount * camera.shadow_params.x)); - return target_color(vec4(finish_color(rgb, input.color.a, input.grain, finish), input.color.a)); + return target_color(agent_color(vec4(finish_color(rgb, input.color.a, input.grain, finish), input.color.a), input.stamp, input.clip)); } // Offset depth only across the actual incident face, never its extension past // a silhouette. Using homogeneous planes also works at near-plane crossings. @@ -214,6 +235,8 @@ fn edge_vertex(@location(0) world_a: vec3, @location(1) world_b: vec3, else if (state&1u)!=0u {edge_color=camera.edge_selected;} else if (state&2u)!=0u {edge_color=select(camera.edge_hovered,camera.edge_selected,(camera.interaction.w&1u)!=0u && (state&8u)==0u);} output.color = target_color(edge_color); + output.stamp = agent_stamp(interaction); + output.clip = vec3(0.0,0.0,1.0); output.center = vec3(0.0); output.faces = faces; output.lateral = 0.0; @@ -267,6 +290,7 @@ fn edge_vertex(@location(0) world_a: vec3, @location(1) world_b: vec3, let biased = bitcast(bitcast(depth) - min(1u,bitcast(depth))); output.position = vec4(clip.xy + offset * clip.w,biased * clip.w,clip.w); output.lateral = selector.y; + output.clip = output.position.xyw; return output; } fn coverage(input: Output) -> f32 { @@ -278,7 +302,7 @@ fn coverage(input: Output) -> f32 { fn fragment_main(input: Output) -> @location(0) vec4 { let amount = coverage(input); if amount < 0.001 { discard; } - return input.color * amount; + return agent_color(input.color, input.stamp, input.clip) * amount; } // A steep incident plane must not pull a hidden edge through another object. // Test the unexpanded centerline as well as the stroke footprint. Incident-face @@ -301,15 +325,24 @@ fn fragment_visibility(@builtin(position) position: vec4, return vec2(faces.x,bitcast(position.z)); } @fragment +fn fragment_visibility_effect(@builtin(position) position: vec4, + @location(3) @interpolate(flat) faces: vec4, + @location(7) @interpolate(flat) stamp: f32, + @location(8) clip: vec3) -> @location(0) vec2 { + if agent_reveal(stamp, camera.agent_effect.x, agent_uv(clip), camera.viewport.xy) < 0.0 { discard; } + return vec2(faces.x,bitcast(position.z)); +} +@fragment fn edge_color(input: Output) -> @location(0) vec4 { let amount = coverage(input); if amount < 0.001 || hidden_center(input) { discard; } - return input.color * amount; + return agent_color(input.color, input.stamp, input.clip) * amount; } @fragment fn edge_depth(input: Output) -> @location(0) vec4 { let amount = coverage(input); if input.position.z <= 0.0 || input.position.z >= 1.0 || input.color.a < 1.0 || amount < 0.001 || hidden_center(input) { discard; } + if agent_reveal(input.stamp, camera.agent_effect.x, agent_uv(input.clip), camera.viewport.xy) < 0.0 { discard; } return vec4(0.0); } @@ -320,6 +353,8 @@ fn marker_vertex(@location(0) point: vec3, @location(1) edges: vec4, @builtin(vertex_index) vertex: u32) -> Output { var output: Output; output.position = vec4(2.0,2.0,2.0,1.0); + output.stamp = -2.0; + output.clip = vec3(0.0,0.0,1.0); var selected = false; for(var i=0u;i<4u;i++) { let own = slot_state(edges[i]); diff --git a/crates/benchup-app/src/scene_buffers.rs b/crates/benchup-app/src/scene_buffers.rs index 5ac09f5..27dd57e 100644 --- a/crates/benchup-app/src/scene_buffers.rs +++ b/crates/benchup-app/src/scene_buffers.rs @@ -189,6 +189,7 @@ mod tests { fn vertices(count: usize) -> Vec { (0..count) .map(|i| Vertex { + effect: -2., position: [i as f32, 0., 0.5], color: [255; 4], lateral: 0., diff --git a/crates/benchup-cli/src/bridge.rs b/crates/benchup-cli/src/bridge.rs index b3c56ae..4e74a3f 100644 --- a/crates/benchup-cli/src/bridge.rs +++ b/crates/benchup-cli/src/bridge.rs @@ -889,6 +889,9 @@ impl Live { } const LIVE_INSTRUCTIONS: &str = "Edits the BenchUp 3D woodworking project open in the app, live, while the designer watches. \ +Begin each task with agent_activity {phase: \"analyzing\"}; use phase: \"preparing\" while planning changes. \ +Always send agent_activity {phase: \"finished\"} after the final check and before your reply, including when stopping after an error. \ +The robot remains visible throughout the task, including pauses between tool calls. \ Lengths are millimetres, Z is up. Call get_scene first to see the model. Each changing call (and each run_batch) is one undo \ step labelled \"Agent: …\" in BenchUp's history. Every call starts in the scope the designer is in, so put enter_object and \ the calls that need it in one run_batch. The current selection is what the designer has selected, so \"this\" usually means \ @@ -917,6 +920,8 @@ impl Live { } impl Backend for Live { + fn supports_activity(&self) -> bool { true } + fn call(&mut self, _client: &str, name: &str, arguments: Value) -> (Value, bool) { self.forward(name, arguments) } diff --git a/crates/benchup-cli/src/bridge_tests.rs b/crates/benchup-cli/src/bridge_tests.rs index 096e152..a97210f 100644 --- a/crates/benchup-cli/src/bridge_tests.rs +++ b/crates/benchup-cli/src/bridge_tests.rs @@ -112,6 +112,15 @@ fn clients_reach_the_app_through_the_bridge() { // MCP session tools become calls the app knows. let mut live = Live::new(Target::Client(client)); live.set_client("claude-code"); + assert!(live.supports_activity()); + assert!(live.instructions().contains("agent_activity")); + for phase in ["analyzing", "preparing", "finished"] { + let (result, ok) = live.call("claude-code", "agent_activity", json!({"phase": phase})); + assert!(ok); + assert_eq!(result["name"], "agent_activity"); + assert_eq!(result["arguments"], json!({"phase": phase})); + assert_eq!(result["client"], "claude-code"); + } let (result, _) = live.batch(vec![("create_board".into(), json!({"name": "A"}))], true); assert_eq!(result["name"], "run_batch"); assert_eq!(result["arguments"], json!({"calls": [{"tool": "create_board", "arguments": {"name": "A"}}], "dry_run": true})); diff --git a/crates/benchup-cli/src/mcp.rs b/crates/benchup-cli/src/mcp.rs index 7a78477..ef0d91e 100644 --- a/crates/benchup-cli/src/mcp.rs +++ b/crates/benchup-cli/src/mcp.rs @@ -23,6 +23,8 @@ pub trait Backend { fn instructions(&self) -> &'static str { INSTRUCTIONS } + /// Only live sessions can report activity in the open app. + fn supports_activity(&self) -> bool { false } /// The MCP client introduced itself (`clientInfo.name`). fn set_client(&mut self, _client: &str) {} } @@ -41,6 +43,17 @@ fn render_view_tool() -> Value { }) } +fn agent_activity_tool() -> Value { + json!({ + "name": "agent_activity", + "description": "Report the lifetime of your work in the open BenchUp app. Call with phase: analyzing BEFORE you start reading/planning, preparing while preparing changes, and finished AFTER your final verification and BEFORE replying to the user, including when stopping after an error. The robot stays visible between tool calls until finished. Ordinary calls update its looking/gear animation automatically. This changes no model data, selection or undo history. Only available in live mode.", + "inputSchema": {"type": "object", "properties": { + "phase": {"type": "string", "enum": ["analyzing", "preparing", "finished"]} + }, "required": ["phase"], "additionalProperties": false}, + "annotations": {"readOnlyHint": true, "destructiveHint": false, "idempotentHint": true, "openWorldHint": false} + }) +} + /// Tools that only make sense for a file served over MCP: batches with dry /// runs and `$N` references, and undo. The shared tools act one call at a time. fn session_tools() -> Vec { @@ -133,6 +146,7 @@ fn handle(message: &Value, backend: &mut dyn Backend, client: &mut String) -> Op let mut tools = benchup_tools::mcp_tools(); tools.push(render_view_tool()); tools.extend(session_tools()); + if backend.supports_activity() { tools.push(agent_activity_tool()); } ok(json!({"tools": tools})) } "tools/call" => { @@ -181,3 +195,39 @@ pub fn serve(reader: R, mut writer: W, backend: &mut dyn B } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + #[derive(Default)] + struct ActivityBackend { live: bool, calls: Vec<(String, Value)> } + impl Backend for ActivityBackend { + fn supports_activity(&self) -> bool { self.live } + fn call(&mut self, _: &str, name: &str, arguments: Value) -> (Value, bool) { + self.calls.push((name.into(), arguments)); + (json!({"ok": true}), true) + } + fn batch(&mut self, _: Vec<(String, Value)>, _: bool) -> (Value, bool) { unreachable!() } + fn history(&mut self, _: bool) -> (Value, bool) { unreachable!() } + fn render(&mut self, _: &str, _: u32, _: u32) -> Result<(String, Value), String> { unreachable!() } + } + + #[test] + fn activity_tool_is_advertised_only_for_live_backends_and_reports_are_forwarded() { + let mut backend = ActivityBackend::default(); + let mut client = "test-agent".into(); + let list = json!({"jsonrpc": "2.0", "id": 1, "method": "tools/list"}); + let offline = handle(&list, &mut backend, &mut client).unwrap(); + assert!(!offline["result"]["tools"].as_array().unwrap().iter().any(|tool| tool["name"] == "agent_activity")); + backend.live = true; + let live = handle(&list, &mut backend, &mut client).unwrap(); + let tool = live["result"]["tools"].as_array().unwrap().iter().find(|tool| tool["name"] == "agent_activity").unwrap(); + assert_eq!(tool["inputSchema"]["properties"]["phase"]["enum"], json!(["analyzing", "preparing", "finished"])); + for phase in ["analyzing", "preparing", "finished"] { + let call = json!({"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "agent_activity", "arguments": {"phase": phase}}}); + let answer = handle(&call, &mut backend, &mut client).unwrap(); + assert_eq!(answer["result"]["isError"], false); + assert_eq!(backend.calls.last().unwrap(), &("agent_activity".to_owned(), json!({"phase": phase}))); + } + } +} diff --git a/docs/agent-guide.md b/docs/agent-guide.md index a9e1adb..d6bc08d 100644 --- a/docs/agent-guide.md +++ b/docs/agent-guide.md @@ -44,6 +44,7 @@ Each other call is saved on its own, so prefer `run_batch` for any change of mor `benchup bridge` (MCP) and the CLI's `--live` commands (`benchup call --live …`, `benchup scene --live`) work on the project open in BenchUp, while the designer watches. The tools and batches are the same, with these differences: +- Begin each task with `agent_activity {"phase":"analyzing"}` before reading or planning, and use `{"phase":"preparing"}` while preparing changes. Ordinary calls update the robot's looking/gear mode automatically. The robot stays visible across pauses between calls. After final verification, **always** call `agent_activity {"phase":"finished"}` before replying to the user, including when stopping after an error. From the CLI, use `benchup call --live agent_activity '{"phase":"analyzing"}'` (and likewise for the other phases). Activity reports change no model data, selection, or undo history; they are live-only. - Each changing call, and each batch, is one undo step labelled `Agent: …` in BenchUp's history. Prefer one batch per change, so the designer can undo it in one step. - Every call starts in the scope the designer is in. Put `enter_object` and the calls that need it in one batch, ending with `exit_to_root`. - `get_selection` is what the designer has selected: when they say "this" or "these", read it first. What you create or change is selected afterwards. diff --git a/docs/cli.md b/docs/cli.md index a55e448..6a179ce 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -99,6 +99,29 @@ agent ──MCP (stdio) or benchup … --live──▶ benchup bridge (127.0.0.1 `scene`, `get`, `call` (with `--batch` and `--dry-run`), `undo`, `redo` and `render` take `--live` instead of a file. When no bridge is running, the first `--live` command starts one in the background, which exits after 30 minutes without calls. A `benchup bridge` that finds another bridge on its port forwards MCP calls through it, and listens on the port itself once that bridge exits. `BENCHUP_BRIDGE_PORT`, or `benchup bridge --port`, changes the port; set the same port in BenchUp. +For **Codex CLI**, also connect its turn-completion notification to BenchUp. +This clears the robot and activity text when Codex replies, even if the model +omits `agent_activity {"phase":"finished"}`. With Node 22+ and Python 3.11+: + +```sh +mkdir -p ~/.codex/hooks +cp scripts/codex_benchup_notify.mjs ~/.codex/hooks/benchup-notify.mjs +``` + +Add this top-level setting to `~/.codex/config.toml` (use the actual absolute +path, or the equivalent path under `CODEX_HOME`), then restart Codex CLI: + +```toml +notify = ["node", "/absolute/path/to/.codex/hooks/benchup-notify.mjs"] +``` + +Preserve or chain an existing `notify` handler instead of replacing it. The +hook reads the token and port from the existing BenchUp MCP configuration; +no token belongs in the notification command. It only sends activity-completion +reports to an already running bridge, and does not open an app or edit a model. +Calls from clients that do not declare a task show a short activity indicator; +declared tasks stay visible between calls until completion. + How it behaves: - **Undo:** each changing call is one step in BenchUp's history, labelled `Agent: `; a batch (`--batch` or MCP `run_batch`) is one step. The designer can undo it as usual, and `benchup undo --live` steps through the same history. diff --git a/scripts/benchmark_viewport_web.mjs b/scripts/benchmark_viewport_web.mjs index 0e8d934..d521244 100644 --- a/scripts/benchmark_viewport_web.mjs +++ b/scripts/benchmark_viewport_web.mjs @@ -254,11 +254,11 @@ if d.property(d.root,'_NET_ACTIVE_WINDOW') != [window]: raise RuntimeError('The result.shadows=shadowState.shadows;result.shadow_builds=shadowState.shadow_builds; if(args.includes('--require-shadows')&&(!result.shadows||!(result.shadow_builds>0)))throw Error('Shadows were not enabled during the benchmark'); if(args.includes('--require-shadows')&&['orbit','select','groups'].includes(mode)&&result.shadow_builds!==1)throw Error('Unchanged sunlight was rebuilt during camera/selection/scope changes: '+result.shadow_builds); - if(args.includes('--validate-markers'))result.marker_regression=await evaluate(`(${validateMarkers.toString()})(${JSON.stringify(await readFile(new URL('../crates/benchup-app/src/resident_scene.wgsl',import.meta.url),'utf8'))})`); - if(args.includes('--validate-shadows'))result.shadow_regression=await evaluate(`(${validateSunlight.toString()})(${JSON.stringify(await readFile(new URL('../crates/benchup-app/src/resident_scene.wgsl',import.meta.url),'utf8'))})`); + if(args.includes('--validate-markers'))result.marker_regression=await evaluate(`(${validateMarkers.toString()})(${JSON.stringify((await Promise.all(['surface_appearance.wgsl','agent_effects.wgsl','resident_scene.wgsl'].map(name=>readFile(new URL('../crates/benchup-app/src/'+name,import.meta.url),'utf8')))).join('\n'))})`); + if(args.includes('--validate-shadows'))result.shadow_regression=await evaluate(`(${validateSunlight.toString()})(${JSON.stringify((await Promise.all(['surface_appearance.wgsl','agent_effects.wgsl','resident_scene.wgsl'].map(name=>readFile(new URL('../crates/benchup-app/src/'+name,import.meta.url),'utf8')))).join('\n'))})`); if(mode==='erase-edge'&&scope==='leaf')result.cache_regression=await evaluate(`(${validateEraseCaches.toString()})(${eraseSetup.context},${eraseSetup.edge})`); if(args.includes('--validate-snapping'))result.snapping_regression=await evaluate(`(${validateSnapping.toString()})()`); - if(args.includes('--validate-shader'))result.visibility_regression=await evaluate(`(${validateVisibility.toString()})(${JSON.stringify(await readFile(new URL('../crates/benchup-app/src/resident_scene.wgsl',import.meta.url),'utf8'))})`); + if(args.includes('--validate-shader'))result.visibility_regression=await evaluate(`(${validateVisibility.toString()})(${JSON.stringify((await Promise.all(['surface_appearance.wgsl','agent_effects.wgsl','resident_scene.wgsl'].map(name=>readFile(new URL('../crates/benchup-app/src/'+name,import.meta.url),'utf8')))).join('\n'))})`); result.project=project; result.browser_executable=executable;result.unsafe_webgpu=args.includes('--unsafe-webgpu');result.x11=args.includes('--x11'); result.scope=scope; @@ -725,11 +725,11 @@ async function validateSunlight(shader) { device.pushErrorScope('validation'); const module=device.createShaderModule({code:shader+` @vertex -fn test_sun_vertex(@builtin(vertex_index) vertex: u32, @builtin(instance_index) instance: u32) -> Output { +fn test_sun_vertex(@builtin(vertex_index) vertex: u32, @builtin(instance_index) instance: u32) -> FaceOutput { let positions=array,3>(vec2(-1.0,-1.0),vec2(3.0,-1.0),vec2(-1.0,3.0)); let depths=array(0.45,0.45,0.6,0.3,0.4000005,0.40001,0.4,0.6); let centers=array(-0.5,-0.5,0.5,-0.5,-0.5,-0.5,-0.5,0.0); - var value: Output; + var value: FaceOutput; value.position=vec4(positions[vertex],0.5,1.0); let xy=vec2(centers[instance],0.0)+positions[vertex]*0.01; value.shadow=vec3(xy,depths[instance]+0.2*(xy.x-centers[instance])); @@ -737,7 +737,7 @@ fn test_sun_vertex(@builtin(vertex_index) vertex: u32, @builtin(instance_index) return value; } @fragment -fn test_sun_fragment(value: Output) -> @location(0) vec4 { +fn test_sun_fragment(value: FaceOutput) -> @location(0) vec4 { return vec4(shadow_coverage(value),0.0,0.0,1.0); }`}); const compilation=await module.getCompilationInfo(); @@ -748,8 +748,8 @@ fn test_sun_fragment(value: Output) -> @location(0) vec4 { const bits=new Uint32Array(32*32*2),f=new Float32Array(1),u=new Uint32Array(f.buffer); for(let y=0;y<32;y++)for(let x=0;x<16;x++){const i=(y*32+x)*2;bits[i]=11;f[0]=.3+.4*(x+.5)/32;bits[i+1]=u[0];} device.queue.writeTexture({texture:sunlight},bits,{bytesPerRow:256},[32,32]); - const uniform=device.createBuffer({size:480,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST}); - const camera=new Float32Array(120);camera[32]=.35;camera[34]=.000002; + const uniform=device.createBuffer({size:496,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST}); + const camera=new Float32Array(124);camera[32]=.35;camera[34]=.000002; const bind0=device.createBindGroup({layout:pipeline.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:uniform}}]}); const bind1=device.createBindGroup({layout:pipeline.getBindGroupLayout(1),entries:[{binding:1,resource:sunlight.createView()}]}); const target=device.createTexture({size:[8,1],format:'rgba8unorm',usage:GPUTextureUsage.RENDER_ATTACHMENT|GPUTextureUsage.COPY_SRC}); @@ -834,8 +834,8 @@ async function validateMarkers(shader) { const vertices=device.createBuffer({size:values.byteLength,usage:GPUBufferUsage.VERTEX|GPUBufferUsage.COPY_DST});device.queue.writeBuffer(vertices,0,values); const states=device.createTexture({size:[512,1],format:'r32uint',usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}); const flags=new Uint32Array(512);flags[1]=1;flags[3]=65;flags[4]=1;device.queue.writeTexture({texture:states},flags,{bytesPerRow:2048},[512,1]); - const uniform=device.createBuffer({size:480,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST}); - const camera=new Float32Array(120);for(let i=0;i<4;i++)camera[i*5]=1;camera.set([64,64,1,1.8],64);camera.set([1,0,0,1],88);camera.set([1,1,1,1],108); + const uniform=device.createBuffer({size:496,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST}); + const camera=new Float32Array(124);for(let i=0;i<4;i++)camera[i*5]=1;camera.set([64,64,1,1.8],64);camera.set([1,0,0,1],88);camera.set([1,1,1,1],108); const bind=device.createBindGroup({layout:pipeline.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:uniform}},{binding:1,resource:states.createView()}]}); const target=device.createTexture({size:[64,64],format:'rgba8unorm',usage:GPUTextureUsage.RENDER_ATTACHMENT|GPUTextureUsage.COPY_SRC}); const readback=device.createBuffer({size:16384,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}); diff --git a/scripts/codex_benchup_notify.mjs b/scripts/codex_benchup_notify.mjs new file mode 100644 index 0000000..40d249e --- /dev/null +++ b/scripts/codex_benchup_notify.mjs @@ -0,0 +1,111 @@ +#!/usr/bin/env node +// Codex `notify` hook: finish live BenchUp activity on agent-turn-complete. +// Requires Node 22+ and Python 3.11+; no builds or third-party packages. +import {createHmac, randomBytes, timingSafeEqual} from 'node:crypto'; +import {execFileSync} from 'node:child_process'; +import {fileURLToPath} from 'node:url'; +import {resolve} from 'node:path'; + +// Codex versions use different MCP client names. Leave generic shell clients +// and other hosts (e.g. Claude) alone; they may still be working. +export const CLIENTS = ['codex', 'codex-cli', 'codex-mcp-client']; + +export function readServers() { + // Parse TOML with the standard library. Pairing secrets stay in this pipe + // and memory; they are never placed in argv, logs, or the hook config. + const source = ` +import json, os, pathlib, tomllib +path = pathlib.Path(os.environ.get('CODEX_HOME', str(pathlib.Path.home()/'.codex')))/'config.toml' +data = tomllib.loads(path.read_text()) if path.exists() else {} +servers = [] +for server in data.get('mcp_servers', {}).values(): + command = pathlib.Path(server.get('command', '')).name.lower() + if command not in ('benchup', 'benchup.exe'): continue + env = server.get('env', {}) + token = env.get('BENCHUP_AGENT_TOKEN', os.environ.get('BENCHUP_AGENT_TOKEN')) + port = env.get('BENCHUP_BRIDGE_PORT', os.environ.get('BENCHUP_BRIDGE_PORT', '47615')) + args = server.get('args', []) + if '--port' in args: port = args[args.index('--port')+1] + if token: servers.append({'token': token, 'port': int(port)}) +if not servers and os.environ.get('BENCHUP_AGENT_TOKEN'): + servers.append({'token': os.environ['BENCHUP_AGENT_TOKEN'], 'port': int(os.environ.get('BENCHUP_BRIDGE_PORT', '47615'))}) +print(json.dumps(servers)) +`; + return JSON.parse(execFileSync('python3', ['-c', source], {encoding: 'utf8', timeout: 2000, stdio: ['ignore', 'pipe', 'pipe']})); +} + +function proof(role, token, bridgeNonce, clientNonce) { + return createHmac('sha256', token).update(`benchup-agent-link/${role}\n${bridgeNonce}\n${clientNonce}`).digest('hex'); +} + +export async function finishServer({port, token}, {clients = CLIENTS, timeout = 6000} = {}) { + if (!Number.isInteger(port) || port < 1024 || port > 65535 || typeof token !== 'string' || !token) { + throw Error('Invalid BenchUp connection'); + } + const socket = new WebSocket(`ws://127.0.0.1:${port}/`); + const messages = [], waiters = []; + let failure; + const fail = () => { + failure = Error('BenchUp notification connection ended'); + while (waiters.length) waiters.shift().reject(failure); + }; + socket.addEventListener('error', fail); + socket.addEventListener('close', fail); + socket.addEventListener('message', event => { + try { + if (typeof event.data !== 'string' || event.data.length > 65536) throw Error(); + const message = JSON.parse(event.data); + if (waiters.length) waiters.shift().resolve(message); + else messages.push(message); + } catch { fail(); socket.close(); } + }); + const receive = () => failure ? Promise.reject(failure) : messages.length + ? Promise.resolve(messages.shift()) + : new Promise((resolve, reject) => waiters.push({resolve, reject})); + let timer; + const deadline = new Promise((_, reject) => { + timer = setTimeout(() => { fail(); socket.close(); reject(Error('BenchUp notification timed out')); }, timeout); + }); + try { + return await Promise.race([deadline, (async () => { + await new Promise((resolve, reject) => { + socket.addEventListener('open', resolve, {once: true}); + socket.addEventListener('error', () => reject(Error('BenchUp bridge is unavailable')), {once: true}); + }); + const send = value => socket.send(JSON.stringify(value)); + send({type: 'authenticate', role: 'client', connection: 'benchup-codex-notify'}); + const challenge = await receive(); + if (challenge.type !== 'challenge' || challenge.protocol !== 2 || !/^[0-9a-f]{64}$/.test(challenge.nonce)) throw Error('Incompatible BenchUp bridge'); + const nonce = randomBytes(32).toString('hex'); + send({type: 'prove', nonce, proof: proof('client', token, challenge.nonce, nonce)}); + const ready = await receive(); + const expected = proof('bridge', token, challenge.nonce, nonce); + if (ready.type !== 'ready' || typeof ready.proof !== 'string' || ready.proof.length !== expected.length || + !timingSafeEqual(Buffer.from(ready.proof), Buffer.from(expected))) throw Error('BenchUp bridge proof failed'); + let finished = 0; + for (const [index, client] of clients.entries()) { + send({type: 'request', id: index + 1, name: 'agent_activity', arguments: {phase: 'finished'}, client}); + const answer = await receive(); + if (answer.type !== 'response' || answer.id !== index + 1 || answer.ok !== true) throw Error('BenchUp did not accept completion'); + finished++; + } + return finished; + })()]); + } finally { + clearTimeout(timer); + socket.close(); + } +} + +export async function notify(event, {servers = readServers, finish = finishServer} = {}) { + if (!event || event.type !== 'agent-turn-complete') return []; + // An unavailable/closed app is normal. Do not launch a bridge or app just + // because Codex completed an unrelated turn, and never leak payloads/errors. + return Promise.allSettled(servers().map(server => finish(server))); +} + +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + try { await notify(JSON.parse(process.argv[2] ?? '{}')); } + catch { /* Notification hooks must not interfere with Codex's final reply. */ } + process.exit(0); +} diff --git a/scripts/test_agent_effects.mjs b/scripts/test_agent_effects.mjs new file mode 100644 index 0000000..b0c5501 --- /dev/null +++ b/scripts/test_agent_effects.mjs @@ -0,0 +1,174 @@ +#!/usr/bin/env node +// Validate agent feedback on a hardware WebGPU adapter; never build. +import {readFile, writeFile, readdir, mkdtemp, rm} from 'node:fs/promises'; +import {spawn} from 'node:child_process'; +import {createServer} from 'node:http'; +import {tmpdir, homedir} from 'node:os'; +import {join} from 'node:path'; +const cache=join(homedir(),'.cache/ms-playwright'); +const version=(await readdir(cache)).filter(n=>/^chromium-\d+$/.test(n)).sort().reverse()[0]; +const executable=process.env.CHROME_BIN??join(cache,version,'chrome-linux64/chrome'); +const profile=await mkdtemp(join(tmpdir(),'benchup-agent-effects-')); +const server=createServer((_request,response)=>{response.writeHead(200,{'content-type':'text/html'});response.end('Agent GPU regression')}); +await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve)); +const browser=spawn(executable,['--headless=new','--no-sandbox','--disable-dev-shm-usage', + '--enable-unsafe-webgpu','--ignore-gpu-blocklist','--use-angle=vulkan','--enable-features=Vulkan', + '--remote-debugging-port=0',`--user-data-dir=${profile}`,'about:blank'],{stdio:['ignore','ignore','ignore']}); +let socket; +try { + const deadline=Date.now()+15000; + let port; + while(!port&&Date.now()setTimeout(r,100))}} + if(!port)throw Error('Chromium did not start'); + const tabs=await(await fetch(`http://127.0.0.1:${port}/json/list`)).json(); + socket=new WebSocket(tabs.find(t=>t.type==='page').webSocketDebuggerUrl); + await new Promise((resolve,reject)=>{socket.onopen=resolve;socket.onerror=reject}); + let sequence=0; + const pending=new Map(); + socket.onmessage=event=>{const m=JSON.parse(event.data);const p=pending.get(m.id);if(p){pending.delete(m.id);m.error?p.reject(Error(JSON.stringify(m.error))):p.resolve(m.result)}}; + const call=(method,params={})=>new Promise((resolve,reject)=>{const id=++sequence;pending.set(id,{resolve,reject});socket.send(JSON.stringify({id,method,params}))}); + await call('Page.navigate',{url:`http://127.0.0.1:${server.address().port}/`}); + await new Promise(resolve=>setTimeout(resolve,200)); + const sources=Object.fromEntries(await Promise.all(['surface_appearance.wgsl','agent_effects.wgsl','edges.wgsl','resident_scene.wgsl','agent_robot.wgsl'].map(async n=>[n,await readFile(new URL('../crates/benchup-app/src/'+n,import.meta.url),'utf8')]))); + const expression=`(${validate.toString()})(${JSON.stringify(sources)})`; + const result=await call('Runtime.evaluate',{expression,awaitPromise:true,returnByValue:true}); + if(result.exceptionDetails)throw Error(result.exceptionDetails.exception?.description??JSON.stringify(result.exceptionDetails)); + const {preview,...report}=result.result.value; + if(preview)await writeFile('/tmp/benchup-agent-feedback.png',Buffer.from(preview.split(',')[1],'base64')); + console.log(JSON.stringify({...report,preview:'/tmp/benchup-agent-feedback.png'},null,2)); +} finally { + socket?.close();browser.kill();server.close(); + await new Promise(resolve=>browser.exitCode!==null?resolve():browser.once('exit',resolve)); + await rm(profile,{recursive:true,force:true,maxRetries:5,retryDelay:100}); +} + +async function validate(sources) { + if(!navigator.gpu)throw Error('WebGPU is unavailable'); + const adapter=await navigator.gpu.requestAdapter(); + if(!adapter||adapter.info.isFallbackAdapter)throw Error('A hardware GPU is required'); + const device=await adapter.requestDevice(); + const modules={};let pipelineCount=0; + try { + device.pushErrorScope('validation'); + for(const name of ['edges.wgsl','resident_scene.wgsl','agent_robot.wgsl']){ + const code=sources['surface_appearance.wgsl']+'\n'+sources['agent_effects.wgsl']+'\n'+sources[name]; + const module=device.createShaderModule({label:name,code}); + const info=await module.getCompilationInfo(); + const errors=info.messages.filter(m=>m.type==='error'); + if(errors.length)throw Error(name+': '+errors.map(m=>`${m.lineNum}:${m.linePos} ${m.message}`).join('\n')); + modules[name]=module; + } + const attributes=(formats,locations)=>{ + const bytes={float32:4,float32x2:8,float32x3:12,float32x4:16,uint32:4,uint32x2:8,uint32x4:16,unorm8x4:4}; + let offset=0;return formats.map((format,i)=>{const value={shaderLocation:locations?.[i]??i,offset,format};offset+=bytes[format];return value}); + }; + const compact={arrayStride:32,attributes:attributes(['float32x3','unorm8x4','float32','unorm8x4','float32','float32'],[0,1,2,3,4,7])}; + const material={arrayStride:20,attributes:attributes(['float32x4','unorm8x4'],[5,6])}; + const face={arrayStride:52,attributes:attributes(['float32x3','float32x3','float32x3','unorm8x4','uint32','uint32x2'])}; + const surface={arrayStride:16,attributes:attributes(['float32x3','unorm8x4'],[6,7])}; + const edge={arrayStride:152,stepMode:'instance',attributes:attributes([...Array(10).fill('float32x3'),'unorm8x4','float32','uint32x4','uint32x2'])}; + const ghost={arrayStride:36,attributes:attributes(['float32x3','float32x3','float32x2','uint32'])}; + const make=async(module,vertex,fragment,buffers,format='rgba8unorm',layout='auto')=>{ + pipelineCount++; + return await device.createRenderPipelineAsync({layout,vertex:{module,entryPoint:vertex,buffers},fragment:{module,entryPoint:fragment,targets:[{format}]},depthStencil:{format:'depth32float',depthWriteEnabled:false,depthCompare:'always'}}); + }; + const projectedLayout=device.createBindGroupLayout({entries:[ + {binding:0,visibility:GPUShaderStage.FRAGMENT,texture:{sampleType:'float'}}, + {binding:1,visibility:GPUShaderStage.FRAGMENT,sampler:{type:'filtering'}}, + {binding:2,visibility:GPUShaderStage.FRAGMENT,buffer:{type:'uniform',minBindingSize:32}} + ]}); + const projectedPipelineLayout=device.createPipelineLayout({bindGroupLayouts:[projectedLayout]}); + for(const srgb of [false,true])for(const effects of [false,true]){ + for(const [fragment,materials] of [['fragment_main',false],['fragment_edge_depth',false],['fragment_shadow_faces',false],['fragment_material_faces',true],['fragment_material_shadow_faces',true]]){ + await make(modules['edges.wgsl'],materials?(srgb?'vertex_material_srgb':'vertex_material'):(srgb?'vertex_main_srgb':'vertex_main'),fragment+(effects?'_effect':''),materials?[compact,material]:[compact],srgb?'rgba8unorm-srgb':'rgba8unorm',effects?projectedPipelineLayout:'auto'); + } + } + for(const [vertex,fragment,buffers,format] of [ + ['face_vertex','face_color',[face]],['material_vertex','material_color',[face,surface]], + ['face_vertex','fragment_visibility_effect',[face],'rg32uint'],['shadow_vertex','fragment_visibility',[face],'rg32uint'], + ['edge_vertex','edge_color',[edge]],['edge_vertex','edge_depth',[edge]], + ['marker_vertex','marker_color',[{arrayStride:28,stepMode:'instance',attributes:attributes(['float32x3','uint32x4'])}]], + ])await make(modules['resident_scene.wgsl'],vertex,fragment,buffers,format); + const robot=await make(modules['agent_robot.wgsl'],'robot_vertex','robot_color',[]); + const departing=await make(modules['agent_robot.wgsl'],'ghost_vertex','ghost_color',[ghost]); + const W=192,H=192; + const capture=async(pipeline,bind,vertices=null,count=6)=>{ + const image=device.createTexture({size:[W,H],format:'rgba8unorm',usage:GPUTextureUsage.RENDER_ATTACHMENT|GPUTextureUsage.COPY_SRC}); + const depth=device.createTexture({size:[W,H],format:'depth32float',usage:GPUTextureUsage.RENDER_ATTACHMENT}); + const readback=device.createBuffer({size:W*H*4,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ}); + const encoder=device.createCommandEncoder(),pass=encoder.beginRenderPass({colorAttachments:[{view:image.createView(),clearValue:[0,0,0,0],loadOp:'clear',storeOp:'store'}],depthStencilAttachment:{view:depth.createView(),depthClearValue:1,depthLoadOp:'clear',depthStoreOp:'discard'}}); + pass.setPipeline(pipeline);pass.setBindGroup(0,bind);if(vertices)pass.setVertexBuffer(0,vertices);pass.draw(count);pass.end(); + encoder.copyTextureToBuffer({texture:image},{buffer:readback,bytesPerRow:W*4},[W,H]);device.queue.submit([encoder.finish()]); + await readback.mapAsync(GPUMapMode.READ);const bytes=new Uint8Array(readback.getMappedRange()).slice();readback.unmap(); + image.destroy();depth.destroy();readback.destroy();return bytes; + }; + const buffer=(data,usage)=>{const value=device.createBuffer({size:data.byteLength,usage:usage|GPUBufferUsage.COPY_DST});device.queue.writeBuffer(value,0,data);return value}; + const robotData=new Float32Array(32);for(let i=0;i<4;i++)robotData[i*5]=1; + robotData.set([W,H,1,0],16);robotData.set([96,96,72,0],20);robotData.set([0.055,0.075,0.10,1],28); + const robotUniform=buffer(robotData,GPUBufferUsage.UNIFORM); + const robotBind=device.createBindGroup({layout:robot.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:robotUniform}}]}); + const samples=[]; + for(const [gear,time] of [[0,.2],[0,1.1],[1,.2],[1,.8]]){ + robotData[23]=gear;robotData[25]=time;device.queue.writeBuffer(robotUniform,0,robotData); + samples.push(await capture(robot,robotBind)); + } + const difference=(a,b)=>a.reduce((n,v,i)=>n+Number(Math.abs(v-b[i])>15),0); + const eyeMotion=difference(samples[0],samples[1]),gearMotion=difference(samples[2],samples[3]); + if(eyeMotion<100||gearMotion<100)throw Error('Robot eyes/gear did not animate'); + const assembly=await make(modules['edges.wgsl'],'vertex_main','fragment_main_effect',[compact],'rgba8unorm',projectedPipelineLayout); + const data=new ArrayBuffer(96),dv=new DataView(data); + for(const [i,p] of [[-1,-1],[3,-1],[-1,3]].entries()){ + const base=i*32;dv.setFloat32(base,p[0],true);dv.setFloat32(base+4,p[1],true);dv.setFloat32(base+8,.5,true); + [145,160,174,255].forEach((v,j)=>dv.setUint8(base+12+j,v));dv.setFloat32(base+24,-1,true);dv.setFloat32(base+28,10,true); + } + const geometry=buffer(data,GPUBufferUsage.VERTEX),effectData=new Float32Array([10,-1,0,1,W,H,1,0]); + const effectUniform=buffer(effectData,GPUBufferUsage.UNIFORM); + const empty=device.createTexture({size:[1,1],format:'rgba8unorm',usage:GPUTextureUsage.TEXTURE_BINDING}); + const effectBind=device.createBindGroup({layout:projectedLayout,entries:[{binding:0,resource:empty.createView()},{binding:1,resource:device.createSampler()},{binding:2,resource:{buffer:effectUniform}}]}); + const coverage=[]; + for(const time of [10,10.475,10.96]){ + effectData[0]=time;device.queue.writeBuffer(effectUniform,0,effectData); + const image=await capture(assembly,effectBind,geometry,3);samples.push(image); + coverage.push(image.reduce((n,v,i)=>n+Number(i%4===3&&v>0),0)/(W*H)); + } + if(coverage[0]>.01||coverage[1]<.35||coverage[1]>.65||coverage[2]<.999)throw Error('Incorrect digital reveal: '+coverage); + effectData[0]=10;effectData[6]=0;device.queue.writeBuffer(effectUniform,0,effectData); + const expired=await capture(assembly,effectBind,geometry,3); + if(expired.some((v,i)=>i%4===3&&v!==255))throw Error('Expired vertices reassembled when the timer wrapped'); + // Scanning uses the same shader in ordinary and retained geometry. + for(let i=0;i<3;i++)dv.setFloat32(i*32+28,-1,true);device.queue.writeBuffer(geometry,0,data); + const scanRows=[]; + for(const y of [.85,.15,.85,.15]){ + effectData.set([12,y,0,1]);device.queue.writeBuffer(effectUniform,0,effectData); + const image=await capture(assembly,effectBind,geometry,3);samples.push(image); + const rows=Array.from({length:H},(_,row)=>image[(row*W+W/2)*4+2]-image[(row*W+W/2)*4]); + const peak=rows.indexOf(Math.max(...rows));scanRows.push(peak); + if(Math.abs(peak-y*H)>3)throw Error('Scan band missed expected row: '+JSON.stringify({y,peak})); + } + const ghostData=new ArrayBuffer(108),ghostView=new DataView(ghostData); + for(const [i,p] of [[-1,-1],[3,-1],[-1,3]].entries()){ + ghostView.setFloat32(i*36,p[0],true);ghostView.setFloat32(i*36+4,p[1],true);ghostView.setFloat32(i*36+8,.5,true); + } + const ghostGeometry=buffer(ghostData,GPUBufferUsage.VERTEX); + const ghostBind=device.createBindGroup({layout:departing.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:robotUniform}}]}); + const dissolveCoverage=[]; + for(const progress of [0,.5,1]){ + robotData[24]=progress;device.queue.writeBuffer(robotUniform,0,robotData); + const image=await capture(departing,ghostBind,ghostGeometry,3);samples.push(image); + dissolveCoverage.push(image.reduce((n,v,i)=>n+Number(i%4===3&&v>0),0)/(W*H)); + } + if(dissolveCoverage[0]<.99||dissolveCoverage[1]<.35||dissolveCoverage[1]>.65||dissolveCoverage[2]!==0)throw Error('Incorrect digital dissolve: '+dissolveCoverage); + const sheet=new OffscreenCanvas(W*5,(H+32)*Math.ceil(samples.length/5)),ctx=sheet.getContext('2d'); + ctx.fillStyle='#101820';ctx.fillRect(0,0,sheet.width,sheet.height);ctx.font='14px sans-serif';ctx.fillStyle='#d9f8ff'; + const captions=['Looking / 0.2s','Looking / 1.1s','Preparing / 0.2s','Preparing / 0.8s','Reveal / start','Reveal / half','Reveal / complete','Scan / bottom','Scan / top','Scan / bottom','Scan / top','Dissolve / start','Dissolve / half','Dissolve / complete']; + for(const [i,bytes] of samples.entries()){ + const pixels=bytes.slice();for(let j=0;j{const reader=new FileReader();reader.onload=()=>resolve(reader.result);reader.readAsDataURL(blob)}); + const error=await device.popErrorScope();if(error)throw Error(error.message); + return {passed:true,shader_modules:3,pipelines:pipelineCount,eye_motion_pixels:eyeMotion,gear_motion_pixels:gearMotion,reveal_coverage:coverage,dissolve_coverage:dissolveCoverage,scan_rows:scanRows,adapter:adapter.info.vendor,preview}; + }finally{device.destroy()} +} diff --git a/scripts/test_codex_benchup_notify.mjs b/scripts/test_codex_benchup_notify.mjs new file mode 100644 index 0000000..e4d3aff --- /dev/null +++ b/scripts/test_codex_benchup_notify.mjs @@ -0,0 +1,112 @@ +#!/usr/bin/env node +import {test} from 'node:test'; +import assert from 'node:assert/strict'; +import {createServer} from 'node:http'; +import {createHash, createHmac} from 'node:crypto'; +import {mkdtempSync, writeFileSync, rmSync} from 'node:fs'; +import {join} from 'node:path'; +import {tmpdir} from 'node:os'; +import {CLIENTS, finishServer, notify, readServers} from './codex_benchup_notify.mjs'; + +const TOKEN = 'notification-test-token'; +const proof = (role, nonce, clientNonce) => createHmac('sha256', TOKEN) + .update(`benchup-agent-link/${role}\n${nonce}\n${clientNonce}`).digest('hex'); + +async function bridge({badProof = false, silent = false} = {}) { + const server = createServer(), sockets = new Set(), frames = []; + server.on('upgrade', (request, socket) => { + sockets.add(socket); socket.on('close', () => sockets.delete(socket)); + const accept = createHash('sha1').update(request.headers['sec-websocket-key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest('base64'); + socket.write(`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n\r\n`); + const send = value => { + const payload = Buffer.from(JSON.stringify(value)); + assert(payload.length < 126); + socket.write(Buffer.concat([Buffer.from([0x81, payload.length]), payload])); + }; + let buffered = Buffer.alloc(0); + socket.on('data', data => { + buffered = Buffer.concat([buffered, data]); + while (buffered.length >= 2) { + const opcode = buffered[0] & 15, masked = (buffered[1] & 128) !== 0; + let length = buffered[1] & 127, offset = 2; + if (length === 126) { if (buffered.length < 4) break; length = buffered.readUInt16BE(2); offset = 4; } + if (buffered.length < offset + (masked ? 4 : 0) + length) break; + const mask = buffered.subarray(offset, offset + 4); if (masked) offset += 4; + const payload = Buffer.from(buffered.subarray(offset, offset + length)); + if (masked) for (let i = 0; i < payload.length; i++) payload[i] ^= mask[i % 4]; + buffered = buffered.subarray(offset + length); + if (opcode === 8) { socket.end(Buffer.from([0x88, 0])); continue; } + if (opcode !== 1 || silent) continue; + const frame = JSON.parse(payload.toString()); frames.push(frame); + if (frame.type === 'authenticate') send({type: 'challenge', protocol: 2, nonce: 'a'.repeat(64)}); + else if (frame.type === 'prove') { + assert.equal(frame.proof, proof('client', 'a'.repeat(64), frame.nonce)); + send({type: 'ready', proof: badProof ? '0'.repeat(64) : proof('bridge', 'a'.repeat(64), frame.nonce)}); + } else if (frame.type === 'request') send({type: 'response', id: frame.id, ok: true, result: {ok: true}}); + } + }); + }); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); + return {port: server.address().port, frames, close: async () => { + for (const socket of sockets) socket.destroy(); + await new Promise(resolve => server.close(resolve)); + }}; +} + +test('ignores unrelated events without reading config or contacting BenchUp', async () => { + assert.deepEqual(await notify({type: 'approval-requested'}, {servers: () => { throw Error('must not read'); }}), []); +}); + +test('authenticates, verifies the bridge, and sends completion reports only', async () => { + const peer = await bridge(); + try { + assert.equal(await finishServer({port: peer.port, token: TOKEN}), CLIENTS.length); + const requests = peer.frames.filter(frame => frame.type === 'request'); + assert.deepEqual(requests.map(frame => frame.client), CLIENTS); + assert(CLIENTS.every(client => client.startsWith('codex'))); + assert(requests.every(frame => frame.name === 'agent_activity' && frame.arguments.phase === 'finished')); + assert(!JSON.stringify(peer.frames).includes(TOKEN), 'never send the raw pairing token'); + } finally { await peer.close(); } +}); + +test('never sends completion to an unproven bridge', async () => { + const peer = await bridge({badProof: true}); + try { + await assert.rejects(finishServer({port: peer.port, token: TOKEN}), /proof failed/); + assert(!peer.frames.some(frame => frame.type === 'request')); + } finally { await peer.close(); } +}); + +test('a missing app or unresponsive bridge cannot hang the notification', async () => { + const peer = await bridge({silent: true}); + try { + const started = Date.now(); + const results = await notify({type: 'agent-turn-complete'}, { + servers: () => [{port: peer.port, token: TOKEN}], + finish: server => finishServer(server, {timeout: 80}), + }); + assert.equal(results[0].status, 'rejected'); + assert(Date.now() - started < 1500); + } finally { await peer.close(); } +}); + +test('reads only BenchUp connection settings and respects its custom port', () => { + const root = mkdtempSync(join(tmpdir(), 'benchup-notify-test-')); + const previous = process.env.CODEX_HOME; + try { + process.env.CODEX_HOME = root; + writeFileSync(join(root, 'config.toml'), ` +[mcp_servers.benchup] +command = "benchup" +args = ["bridge", "--port", "48123"] +[mcp_servers.benchup.env] +BENCHUP_AGENT_TOKEN = "fake-test-token" +[mcp_servers.unrelated] +command = "unrelated" +`); + assert.deepEqual(readServers(), [{port: 48123, token: 'fake-test-token'}]); + } finally { + if (previous === undefined) delete process.env.CODEX_HOME; else process.env.CODEX_HOME = previous; + rmSync(root, {recursive: true, force: true}); + } +}); diff --git a/scripts/test_grain_shader.mjs b/scripts/test_grain_shader.mjs index 2bc1bf7..94c816c 100644 --- a/scripts/test_grain_shader.mjs +++ b/scripts/test_grain_shader.mjs @@ -25,7 +25,7 @@ try { socket.onmessage=event=>{const m=JSON.parse(event.data);const p=pending.get(m.id);if(p){pending.delete(m.id);m.error?p.reject(Error(JSON.stringify(m.error))):p.resolve(m.result)}}; const call=(method,params={})=>new Promise((resolve,reject)=>{const id=++sequence;pending.set(id,{resolve,reject});socket.send(JSON.stringify({id,method,params}))}); await call('Page.navigate',{url:'http://127.0.0.1:8080/'}); - const source=(await Promise.all(['surface_appearance.wgsl','edges.wgsl'].map(n=>readFile(new URL('../crates/benchup-app/src/'+n,import.meta.url),'utf8')))).join('\n'); + const source=(await Promise.all(['surface_appearance.wgsl','agent_effects.wgsl','edges.wgsl'].map(n=>readFile(new URL('../crates/benchup-app/src/'+n,import.meta.url),'utf8')))).join('\n'); const pairs=Array.from({length:32},(_,i)=>[i%2?-i*31:i*47,i%3?i-17:-i*113]); const expected=pairs.map(([column,row])=>{ const mask=(1n<<64n)-1n; From ca58565dab7995cc80d7b68636670dc896507b5f Mon Sep 17 00:00:00 2001 From: Danny Coulombe Date: Mon, 16 Nov 2026 06:48:14 -0500 Subject: [PATCH 4/4] Improve agent setup and token refresh UX --- assets/i18n/en_US.json | 10 +- assets/i18n/es_419.json | 10 +- assets/i18n/it_IT.json | 10 +- assets/i18n/pt_BR.json | 10 +- assets/i18n/vi_VN.json | 10 +- assets/i18n/zh_CN.json | 10 +- crates/benchup-app/src/agent_link.rs | 30 ++++++ crates/benchup-app/src/agent_link_tests.rs | 88 ++++++++++++++++ crates/benchup-app/src/agent_link_ui.rs | 111 +++++++++++++++++---- crates/benchup-app/src/app.rs | 1 + crates/benchup-app/src/settings.rs | 31 ++++-- crates/benchup-app/src/sidebar.rs | 49 ++++++++- 12 files changed, 327 insertions(+), 43 deletions(-) diff --git a/assets/i18n/en_US.json b/assets/i18n/en_US.json index bff95d2..d7b4256 100644 --- a/assets/i18n/en_US.json +++ b/assets/i18n/en_US.json @@ -130,12 +130,14 @@ "agent_link.port": "Bridge port", "agent_link.port_help": "The port benchup bridge listens on (BENCHUP_BRIDGE_PORT). The default is 47615.", "agent_link.queued_status": "BenchUp is busy; the call is waiting", - "agent_link.regenerate": "New token", + "agent_link.refresh_confirmation.body": "Are you sure you want to refresh the pairing token? Agents using the current token will disconnect and must be set up again.", + "agent_link.refresh_confirmation.title": "Refresh token?", + "agent_link.regenerate": "Refresh token", "agent_link.regenerate_help": "Replace the token. Agents using the old one are disconnected.", "agent_link.release_control": "Let the agent edit", "agent_link.release_control_help": "The agent may change the project again without asking while you are not using a tool.", "agent_link.setup": "Set up an agent", - "agent_link.setup_help": "Install the benchup command, then add it to your agent with one of these.", + "agent_link.setup_help": "Install the benchup command, then select your agent and copy its setup below.", "agent_link.shell_help": "For agents that run benchup commands: set the token, then use --live, for example benchup scene --live.", "agent_link.show": "Show", "agent_link.status.connected": "Connected: {client}", @@ -143,6 +145,10 @@ "agent_link.status.error": "Not connected: {message}", "agent_link.status.off": "Off", "agent_link.status.waiting": "Connected to the bridge. No agent has called yet.", + "agent_link.tab.claude_code": "Claude Code", + "agent_link.tab.codex": "Codex", + "agent_link.tab.mcp": "MCP configuration", + "agent_link.tab.shell": "Shell", "agent_link.take_control": "Take control", "agent_link.take_control_help": "Pause the agent. Its next changes wait until you grant access.", "agent_link.title": "Agent connection", diff --git a/assets/i18n/es_419.json b/assets/i18n/es_419.json index f105910..0dc8717 100644 --- a/assets/i18n/es_419.json +++ b/assets/i18n/es_419.json @@ -130,12 +130,14 @@ "agent_link.port": "Puerto del puente", "agent_link.port_help": "El puerto en el que escucha benchup bridge (BENCHUP_BRIDGE_PORT). El predeterminado es 47615.", "agent_link.queued_status": "BenchUp está ocupado; la llamada espera", - "agent_link.regenerate": "Nuevo token", + "agent_link.refresh_confirmation.body": "¿Seguro que quieres renovar el token de emparejamiento? Los agentes que usan el token actual se desconectarán y tendrás que configurarlos de nuevo.", + "agent_link.refresh_confirmation.title": "¿Renovar el token?", + "agent_link.regenerate": "Renovar token", "agent_link.regenerate_help": "Reemplaza el token. Los agentes que usen el anterior se desconectan.", "agent_link.release_control": "Dejar editar al agente", "agent_link.release_control_help": "El agente podrá volver a cambiar el proyecto sin preguntar mientras no uses una herramienta.", "agent_link.setup": "Configurar un agente", - "agent_link.setup_help": "Instala el comando benchup y añádelo a tu agente con una de estas opciones.", + "agent_link.setup_help": "Instala el comando benchup, luego selecciona tu agente y copia su configuración de abajo.", "agent_link.shell_help": "Para agentes que ejecutan comandos benchup: define el token y usa --live, por ejemplo benchup scene --live.", "agent_link.show": "Mostrar", "agent_link.status.connected": "Conectado: {client}", @@ -143,6 +145,10 @@ "agent_link.status.error": "Sin conexión: {message}", "agent_link.status.off": "Desactivada", "agent_link.status.waiting": "Conectado al puente. Ningún agente ha llamado todavía.", + "agent_link.tab.claude_code": "Claude Code", + "agent_link.tab.codex": "Codex", + "agent_link.tab.mcp": "Configuración MCP", + "agent_link.tab.shell": "Terminal", "agent_link.take_control": "Tomar el control", "agent_link.take_control_help": "Pausa al agente. Sus próximos cambios esperarán a que concedas acceso.", "agent_link.title": "Conexión de agentes", diff --git a/assets/i18n/it_IT.json b/assets/i18n/it_IT.json index 4406c0f..e7c7428 100644 --- a/assets/i18n/it_IT.json +++ b/assets/i18n/it_IT.json @@ -130,12 +130,14 @@ "agent_link.port": "Porta del bridge", "agent_link.port_help": "La porta su cui ascolta benchup bridge (BENCHUP_BRIDGE_PORT). Il valore predefinito è 47615.", "agent_link.queued_status": "BenchUp è occupato; la chiamata è in attesa", - "agent_link.regenerate": "Nuovo token", + "agent_link.refresh_confirmation.body": "Vuoi davvero rinnovare il token di abbinamento? Gli agenti che usano il token attuale verranno disconnessi e dovranno essere configurati di nuovo.", + "agent_link.refresh_confirmation.title": "Rinnovare il token?", + "agent_link.regenerate": "Rinnova token", "agent_link.regenerate_help": "Sostituisce il token. Gli agenti che usano quello vecchio vengono scollegati.", "agent_link.release_control": "Lascia modificare l'agente", "agent_link.release_control_help": "L'agente potrà di nuovo modificare il progetto senza chiedere mentre non usi uno strumento.", "agent_link.setup": "Configura un agente", - "agent_link.setup_help": "Installa il comando benchup, poi aggiungilo al tuo agente con una di queste opzioni.", + "agent_link.setup_help": "Installa il comando benchup, poi seleziona il tuo agente e copia la configurazione qui sotto.", "agent_link.shell_help": "Per agenti che eseguono comandi benchup: imposta il token, poi usa --live, ad esempio benchup scene --live.", "agent_link.show": "Mostra", "agent_link.status.connected": "Collegato: {client}", @@ -143,6 +145,10 @@ "agent_link.status.error": "Non collegato: {message}", "agent_link.status.off": "Disattivata", "agent_link.status.waiting": "Collegato al bridge. Nessun agente ha ancora chiamato.", + "agent_link.tab.claude_code": "Claude Code", + "agent_link.tab.codex": "Codex", + "agent_link.tab.mcp": "Configurazione MCP", + "agent_link.tab.shell": "Terminale", "agent_link.take_control": "Prendi il controllo", "agent_link.take_control_help": "Mette in pausa l'agente. Le sue prossime modifiche attendono finché non concedi l'accesso.", "agent_link.title": "Connessione agenti", diff --git a/assets/i18n/pt_BR.json b/assets/i18n/pt_BR.json index abcf829..5406d62 100644 --- a/assets/i18n/pt_BR.json +++ b/assets/i18n/pt_BR.json @@ -130,12 +130,14 @@ "agent_link.port": "Porta da ponte", "agent_link.port_help": "A porta em que o benchup bridge escuta (BENCHUP_BRIDGE_PORT). O padrão é 47615.", "agent_link.queued_status": "O BenchUp está ocupado; a chamada está aguardando", - "agent_link.regenerate": "Novo token", + "agent_link.refresh_confirmation.body": "Tem certeza de que deseja atualizar o token de pareamento? Os agentes que usam o token atual serão desconectados e precisarão ser configurados novamente.", + "agent_link.refresh_confirmation.title": "Atualizar o token?", + "agent_link.regenerate": "Atualizar token", "agent_link.regenerate_help": "Substitui o token. Agentes que usam o antigo são desconectados.", "agent_link.release_control": "Deixar o agente editar", "agent_link.release_control_help": "O agente poderá alterar o projeto novamente sem perguntar enquanto você não estiver usando uma ferramenta.", "agent_link.setup": "Configurar um agente", - "agent_link.setup_help": "Instale o comando benchup e adicione-o ao seu agente com uma destas opções.", + "agent_link.setup_help": "Instale o comando benchup, selecione seu agente e copie a configuração abaixo.", "agent_link.shell_help": "Para agentes que executam comandos benchup: defina o token e use --live, por exemplo benchup scene --live.", "agent_link.show": "Mostrar", "agent_link.status.connected": "Conectado: {client}", @@ -143,6 +145,10 @@ "agent_link.status.error": "Sem conexão: {message}", "agent_link.status.off": "Desativada", "agent_link.status.waiting": "Conectado à ponte. Nenhum agente chamou ainda.", + "agent_link.tab.claude_code": "Claude Code", + "agent_link.tab.codex": "Codex", + "agent_link.tab.mcp": "Configuração MCP", + "agent_link.tab.shell": "Terminal", "agent_link.take_control": "Assumir o controle", "agent_link.take_control_help": "Pausa o agente. As próximas alterações dele esperam até você conceder acesso.", "agent_link.title": "Conexão de agentes", diff --git a/assets/i18n/vi_VN.json b/assets/i18n/vi_VN.json index 133da10..98126f3 100644 --- a/assets/i18n/vi_VN.json +++ b/assets/i18n/vi_VN.json @@ -130,12 +130,14 @@ "agent_link.port": "Cổng cầu nối", "agent_link.port_help": "Cổng mà benchup bridge lắng nghe (BENCHUP_BRIDGE_PORT). Mặc định là 47615.", "agent_link.queued_status": "BenchUp đang bận; lệnh gọi đang chờ", - "agent_link.regenerate": "Mã mới", + "agent_link.refresh_confirmation.body": "Bạn có chắc muốn làm mới mã ghép nối không? Các tác tử đang dùng mã hiện tại sẽ bị ngắt kết nối và cần được thiết lập lại.", + "agent_link.refresh_confirmation.title": "Làm mới mã?", + "agent_link.regenerate": "Làm mới mã", "agent_link.regenerate_help": "Thay mã. Các tác tử dùng mã cũ sẽ bị ngắt kết nối.", "agent_link.release_control": "Để tác tử chỉnh sửa", "agent_link.release_control_help": "Tác tử có thể lại thay đổi dự án mà không cần hỏi khi bạn không dùng công cụ nào.", "agent_link.setup": "Thiết lập tác tử", - "agent_link.setup_help": "Cài đặt lệnh benchup, sau đó thêm nó vào tác tử bằng một trong các cách sau.", + "agent_link.setup_help": "Cài đặt lệnh benchup, sau đó chọn tác tử và sao chép thiết lập bên dưới.", "agent_link.shell_help": "Cho tác tử chạy lệnh benchup: đặt mã, rồi dùng --live, ví dụ benchup scene --live.", "agent_link.show": "Hiện", "agent_link.status.connected": "Đã kết nối: {client}", @@ -143,6 +145,10 @@ "agent_link.status.error": "Chưa kết nối: {message}", "agent_link.status.off": "Tắt", "agent_link.status.waiting": "Đã kết nối với cầu nối. Chưa có tác tử nào gọi.", + "agent_link.tab.claude_code": "Claude Code", + "agent_link.tab.codex": "Codex", + "agent_link.tab.mcp": "Cấu hình MCP", + "agent_link.tab.shell": "Dòng lệnh", "agent_link.take_control": "Giành quyền điều khiển", "agent_link.take_control_help": "Tạm dừng tác tử. Các thay đổi tiếp theo của nó sẽ chờ bạn cấp quyền.", "agent_link.title": "Kết nối tác tử", diff --git a/assets/i18n/zh_CN.json b/assets/i18n/zh_CN.json index adaca61..3a5e228 100644 --- a/assets/i18n/zh_CN.json +++ b/assets/i18n/zh_CN.json @@ -130,12 +130,14 @@ "agent_link.port": "桥接端口", "agent_link.port_help": "benchup bridge 监听的端口(BENCHUP_BRIDGE_PORT)。默认为 47615。", "agent_link.queued_status": "BenchUp 正忙;调用正在等待", - "agent_link.regenerate": "新令牌", + "agent_link.refresh_confirmation.body": "确定要刷新配对令牌吗?使用当前令牌的智能体将断开连接,需要重新设置。", + "agent_link.refresh_confirmation.title": "刷新令牌?", + "agent_link.regenerate": "刷新令牌", "agent_link.regenerate_help": "替换令牌。使用旧令牌的智能体将断开连接。", "agent_link.release_control": "让智能体编辑", "agent_link.release_control_help": "在你未使用工具时,智能体可以再次无需询问地更改项目。", "agent_link.setup": "设置智能体", - "agent_link.setup_help": "安装 benchup 命令,然后用以下任一方式将其添加到你的智能体。", + "agent_link.setup_help": "安装 benchup 命令,然后选择你的智能体并复制下方的设置。", "agent_link.shell_help": "适用于运行 benchup 命令的智能体:设置令牌,然后使用 --live,例如 benchup scene --live。", "agent_link.show": "显示", "agent_link.status.connected": "已连接:{client}", @@ -143,6 +145,10 @@ "agent_link.status.error": "未连接:{message}", "agent_link.status.off": "已关闭", "agent_link.status.waiting": "已连接到桥接程序。尚无智能体调用。", + "agent_link.tab.claude_code": "Claude Code", + "agent_link.tab.codex": "Codex", + "agent_link.tab.mcp": "MCP 配置", + "agent_link.tab.shell": "终端", "agent_link.take_control": "接管控制", "agent_link.take_control_help": "暂停智能体。它接下来的更改将等待你授予访问权限。", "agent_link.title": "智能体连接", diff --git a/crates/benchup-app/src/agent_link.rs b/crates/benchup-app/src/agent_link.rs index 42bfe92..f33355a 100644 --- a/crates/benchup-app/src/agent_link.rs +++ b/crates/benchup-app/src/agent_link.rs @@ -89,6 +89,21 @@ impl Preferences { } } +/// Stable identifiers for the remembered agent setup tab. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "snake_case")] +pub(super) enum SetupTab { + #[default] + ClaudeCode, + Codex, + Mcp, + Shell, +} + +impl SetupTab { + pub(super) const ALL: [Self; 4] = [Self::ClaudeCode, Self::Codex, Self::Mcp, Self::Shell]; +} + #[derive(Clone, Debug, PartialEq)] pub(super) enum Status { Off, @@ -382,6 +397,9 @@ pub(super) struct AgentLink { generator: fn() -> Result, /// Session-only: revealing a secret is never remembered. pub show_token: bool, + pub(super) setup_tab: SetupTab, + /// Confirmation is an in-progress action, never a saved preference. + refresh_confirmation: bool, active: Option, connector: Box, queue: VecDeque, @@ -467,6 +485,8 @@ impl AgentLink { token_retry_at: 0., generator: new_token, show_token: false, + setup_tab: SetupTab::default(), + refresh_confirmation: false, active: None, connector, queue: VecDeque::new(), @@ -526,6 +546,15 @@ impl AgentLink { } self.token.clone() } + /// User-requested replacement requires confirmation when a token exists. + pub(super) fn request_token_refresh(&mut self) { + if self.token.is_some() { + self.refresh_confirmation = true; + } else { + self.regenerate(); + } + } + /// Replaces the token. Connections made with the old one are dropped. If /// a new token cannot be made or saved, the old one stays (an unsaved /// token would stop pairing after a restart) and `token_error` says why. @@ -536,6 +565,7 @@ impl AgentLink { self.token_error = None; self.token_checked = true; self.token = Some(token); + self.copied = None; } Err(error) => self.token_error = Some(error), } diff --git a/crates/benchup-app/src/agent_link_tests.rs b/crates/benchup-app/src/agent_link_tests.rs index 561602f..5a82d55 100644 --- a/crates/benchup-app/src/agent_link_tests.rs +++ b/crates/benchup-app/src/agent_link_tests.rs @@ -1,5 +1,6 @@ use super::*; use std::sync::{Arc, Mutex}; +use crate::test_ui::ContextExt as _; #[test] fn legacy_call_activity_expires_and_never_claims_a_completed_render_is_running() { @@ -733,6 +734,93 @@ fn setup_snippets_keep_the_token_out_of_program_arguments() { assert_eq!(ui::masked("abcd-1234").chars().rev().take(4).collect::(), "4321"); } +fn setup_ui_frame(setup: &mut Setup, events: Vec, settings: bool) -> egui::FullOutput { + let ctx = setup.ctx.clone(); + ctx.run_test_ui(egui::RawInput { + screen_rect: Some(Rect::from_min_size(Pos2::ZERO, egui::vec2(1000., 1100.))), + events, + ..Default::default() + }, |ui| { + if settings { + setup.app.agent_link_settings(ui); + } + setup.app.agent_refresh_window(&ctx); + }) +} + +fn setup_text_rect(output: &egui::FullOutput, label: &str) -> Rect { + fn find(shape: &egui::Shape, label: &str) -> Option { + match shape { + egui::Shape::Text(text) if text.galley.text() == label => Some(Rect::from_min_size(text.pos, text.galley.size())), + egui::Shape::Vec(shapes) => shapes.iter().find_map(|shape| find(shape, label)), + _ => None, + } + } + output.shapes.iter().find_map(|shape| find(&shape.shape, label)).unwrap_or_else(|| panic!("missing text: {label}")) +} + +fn setup_ui_click(setup: &mut Setup, pos: Pos2, settings: bool) -> egui::FullOutput { + let button = |pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + setup_ui_frame(setup, vec![egui::Event::PointerMoved(pos), button(true)], settings); + setup_ui_frame(setup, vec![egui::Event::PointerMoved(pos), button(false)], settings) +} + +#[test] +fn setup_tabs_display_and_copy_the_selected_command() { + let mut setup = Setup::new(); + setup.app.general_settings.agent.port = 5000; + for tab in SetupTab::ALL { + setup.app.agent_link.setup_tab = tab; + setup_ui_frame(&mut setup, vec![], true); + let output = setup_ui_frame(&mut setup, vec![], true); + let expected = tab.command(TOKEN, 5000); + let code = setup_text_rect(&output, &expected); + let copy = setup_text_rect(&output, "Copy"); + assert!(copy.left() > code.right(), "Copy stays to the right of the selectable command"); + assert!((copy.top() - code.top()).abs() < 4., "Copy shares the command's first line instead of stacking above it"); + let output = setup_ui_click(&mut setup, copy.center(), true); + assert!(output.platform_output.commands.iter().any(|command| matches!(command, egui::OutputCommand::CopyText(text) if text == &expected))); + } +} + +#[test] +fn refreshing_a_token_requires_confirmation_and_cancel_keeps_the_token() { + let mut setup = Setup::new(); + setup_ui_frame(&mut setup, vec![], true); + let output = setup_ui_frame(&mut setup, vec![], true); + setup_ui_click(&mut setup, setup_text_rect(&output, "Refresh token").center(), true); + assert!(setup.app.agent_link.refresh_confirmation); + assert_eq!(setup.app.agent_link.token.as_deref(), Some(TOKEN), "requesting refresh does not replace the token"); + + setup_ui_frame(&mut setup, vec![], false); + let output = setup_ui_frame(&mut setup, vec![], false); + let cancel = setup_text_rect(&output, "Cancel"); + let refresh = setup_text_rect(&output, "Refresh token"); + assert!(cancel.center().x > refresh.center().x, "Cancel is the rightmost action"); + setup_ui_click(&mut setup, cancel.center(), false); + assert!(!setup.app.agent_link.refresh_confirmation); + assert_eq!(setup.app.agent_link.token.as_deref(), Some(TOKEN)); + + setup.app.agent_link.request_token_refresh(); + setup_ui_frame(&mut setup, vec![], false); + setup_ui_frame(&mut setup, vec![egui::Event::Key { + key: Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE, + }], false); + assert!(!setup.app.agent_link.refresh_confirmation); + assert_eq!(setup.app.agent_link.token.as_deref(), Some(TOKEN)); + + setup.app.agent_link.request_token_refresh(); + setup_ui_frame(&mut setup, vec![], false); + let output = setup_ui_frame(&mut setup, vec![], false); + setup_ui_click(&mut setup, setup_text_rect(&output, "Refresh token").center(), false); + assert!(!setup.app.agent_link.refresh_confirmation); + assert_ne!(setup.app.agent_link.token.as_deref(), Some(TOKEN)); + let mut restored = AgentLink::with_connector(Box::new(Fake::default())); + restored.token_slot = Some(setup._dir.path().to_owned()); + restored.load_token(); + assert_eq!(restored.token, setup.app.agent_link.token, "the confirmed token is saved immediately"); +} + #[test] fn preferences_default_off_and_older_settings_load() { let settings: settings::GeneralSettings = serde_json::from_str(r#"{"show_fps": true}"#).unwrap(); diff --git a/crates/benchup-app/src/agent_link_ui.rs b/crates/benchup-app/src/agent_link_ui.rs index e807398..20a5c95 100644 --- a/crates/benchup-app/src/agent_link_ui.rs +++ b/crates/benchup-app/src/agent_link_ui.rs @@ -11,9 +11,38 @@ pub(crate) fn masked(token: &str) -> String { /// Which copy button was pressed last, to confirm it next to that row. #[derive(Clone, Copy, Debug, PartialEq)] -pub(crate) enum CopiedText { +pub(super) enum CopiedText { Token, - Setup, + Setup(SetupTab), +} + +impl SetupTab { + fn label(self) -> &'static str { + match self { + Self::ClaudeCode => tr!("agent_link.tab.claude_code"), + Self::Codex => tr!("agent_link.tab.codex"), + Self::Mcp => tr!("agent_link.tab.mcp"), + Self::Shell => tr!("agent_link.tab.shell"), + } + } + + fn help(self) -> &'static str { + match self { + Self::ClaudeCode => tr!("agent_link.command_help"), + Self::Codex => tr!("agent_link.codex_command_help"), + Self::Mcp => tr!("agent_link.json_help"), + Self::Shell => tr!("agent_link.shell_help"), + } + } + + pub(super) fn command(self, token: &str, port: u16) -> String { + match self { + Self::ClaudeCode => claude_command(token, port), + Self::Codex => codex_command(token, port), + Self::Mcp => mcp_json(token, port), + Self::Shell => shell_setup(token, port), + } + } } /// How long "Copied" stays next to the button. @@ -21,7 +50,7 @@ const COPIED_SECONDS: f64 = 2.; impl AgentLink { /// Puts `text` on the system clipboard and confirms it briefly. - pub(crate) fn copy(&mut self, ctx: &egui::Context, text: String, what: CopiedText) { + pub(super) fn copy(&mut self, ctx: &egui::Context, text: String, what: CopiedText) { ctx.copy_text(text); self.copied = Some((what, ctx.input(|i| i.time) + COPIED_SECONDS)); ctx.request_repaint_after(Duration::from_secs_f64(COPIED_SECONDS)); @@ -86,7 +115,7 @@ impl BenchUpApp { } let label = if has_token { tr!("agent_link.regenerate") } else { tr!("agent_link.generate") }; if ui.button(label).on_hover_text(tr!("agent_link.regenerate_help")).clicked() { - self.agent_link.regenerate(); + self.agent_link.request_token_refresh(); } self.agent_link.copied_label(ui, CopiedText::Token); }); @@ -94,22 +123,68 @@ impl BenchUpApp { ui.separator(); ui.text_strong(tr!("agent_link.setup")); ui.text_weak(tr!("agent_link.setup_help")); + ui.horizontal_wrapped(|ui| { + for tab in SetupTab::ALL { + ui.selectable_value(&mut self.agent_link.setup_tab, tab, tab.label()); + } + }); + let tab = self.agent_link.setup_tab; + ui.text_weak(tab.help()); if let Some(token) = self.agent_link.token.clone() { - ui.horizontal_wrapped(|ui| { - if ui.button(tr!("agent_link.copy_command")).on_hover_text(tr!("agent_link.command_help")).clicked() { - self.agent_link.copy(ui.ctx(), claude_command(&token, port), CopiedText::Setup); - } - if ui.button(tr!("agent_link.copy_codex_command")).on_hover_text(tr!("agent_link.codex_command_help")).clicked() { - self.agent_link.copy(ui.ctx(), codex_command(&token, port), CopiedText::Setup); - } - if ui.button(tr!("agent_link.copy_json")).on_hover_text(tr!("agent_link.json_help")).clicked() { - self.agent_link.copy(ui.ctx(), mcp_json(&token, port), CopiedText::Setup); - } - if ui.button(tr!("agent_link.copy_shell")).on_hover_text(tr!("agent_link.shell_help")).clicked() { - self.agent_link.copy(ui.ctx(), shell_setup(&token, port), CopiedText::Setup); - } - self.agent_link.copied_label(ui, CopiedText::Setup); + let command = tab.command(&token, port); + egui::Frame::new() + .fill(colors.canvas) + .stroke(Stroke::new(1., colors.border)) + .corner_radius(8) + .inner_margin(12) + .show(ui, |ui| { + ui.set_width(ui.available_width()); + // Share one top-aligned row so Copy never reserves a header + // above the command or vertically centers it beside the code. + ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| { + if ui.small_button(tr!("context_menu.copy")).on_hover_text(tr!("system_info.copy_to_clipboard")).clicked() { + self.agent_link.copy(ui.ctx(), command.clone(), CopiedText::Setup(tab)); + } + self.agent_link.copied_label(ui, CopiedText::Setup(tab)); + // An immutable buffer keeps selection and keyboard copying + // enabled without letting edits diverge from the copied setup. + let mut text = command.as_str(); + ui.add( + egui::TextEdit::multiline(&mut text) + .id_salt(("agent_setup_command", tab)) + .font(egui::TextStyle::Monospace) + .desired_width(ui.available_width()) + .desired_rows(if tab == SetupTab::Mcp { 12 } else { 4 }) + .frame(egui::Frame::NONE), + ).on_hover_text(tab.help()); + }); + }); + } + } + + pub(crate) fn agent_refresh_window(&mut self, ctx: &egui::Context) { + if !self.agent_link.refresh_confirmation { + return; + } + let (mut refresh, mut cancel) = (false, false); + let response = Modal::new("agent_refresh_token", 440.).show(ctx, |modal| { + modal.header(|ui| { + ui.text_heading(tr!("agent_link.refresh_confirmation.title")); + }); + modal.body(|ui| { + ui.text_label(tr!("agent_link.refresh_confirmation.body")); }); + modal.footer(|ui| { + // Footer actions render right to left: Cancel stays on the right. + cancel = ui.button(tr!("common.cancel")).clicked(); + refresh = primary_button(ui, tr!("agent_link.regenerate"), true).clicked(); + }); + }); + if refresh { + self.agent_link.regenerate(); + } + if refresh || cancel || response.should_close() { + self.agent_link.refresh_confirmation = false; } } diff --git a/crates/benchup-app/src/app.rs b/crates/benchup-app/src/app.rs index 845cc80..d948fb6 100644 --- a/crates/benchup-app/src/app.rs +++ b/crates/benchup-app/src/app.rs @@ -7492,6 +7492,7 @@ impl eframe::App for BenchUpApp { self.account.show(&ctx); self.share_dialog(&ctx); self.agent_access_window(&ctx); + self.agent_refresh_window(&ctx); self.material_removal_window(&ctx); if self.about { self.about_window(&ctx); diff --git a/crates/benchup-app/src/settings.rs b/crates/benchup-app/src/settings.rs index a3cce0c..36bd500 100644 --- a/crates/benchup-app/src/settings.rs +++ b/crates/benchup-app/src/settings.rs @@ -16,7 +16,8 @@ pub(super) enum Section { Grid, StockPricing, SurfaceTreatment, - Diagnostics, + // Older preferences opened this page under its former Diagnostics name. + #[serde(alias = "Diagnostics")] Rendering, Tools, ExportScales, @@ -40,12 +41,12 @@ impl Section { ), ( tr!("settings.system"), - &[Self::Diagnostics, Self::Rendering, Self::ExportScales, Self::AgentConnection], + &[Self::AgentConnection, Self::Rendering, Self::ExportScales], ), ] } #[cfg(test)] - const ALL: [Self; 12] = [ + const ALL: [Self; 11] = [ Self::Units, Self::Navigation, Self::Shortcuts, @@ -53,7 +54,6 @@ impl Section { Self::Colors, Self::StockPricing, Self::SurfaceTreatment, - Self::Diagnostics, Self::Rendering, Self::Tools, Self::ExportScales, @@ -68,7 +68,6 @@ impl Section { Self::Grid => tr!("settings.grid"), Self::StockPricing => tr!("settings.material"), Self::SurfaceTreatment => tr!("settings.sanding_finishing"), - Self::Diagnostics => tr!("settings.diagnostics"), Self::Rendering => tr!("settings.rendering"), Self::Tools => tr!("settings.tools"), Self::ExportScales => tr!("settings.export_scales"), @@ -85,7 +84,6 @@ impl Section { Self::Grid => "settings_grid", Self::StockPricing => "settings_stock", Self::SurfaceTreatment => "time_estimates_sanding", - Self::Diagnostics => "diagnostics", Self::Rendering => "settings_colors", Self::Tools => "settings_shortcuts", Self::ExportScales => "units", @@ -583,7 +581,6 @@ impl BenchUpApp { Section::Grid => "settings/grid", Section::StockPricing => "settings/stock", Section::SurfaceTreatment => "settings/treatment", - Section::Diagnostics => "settings/diagnostics", Section::Rendering => "settings/rendering", Section::Tools => "settings/tools", Section::ExportScales => "settings/export_scales", @@ -658,9 +655,6 @@ impl BenchUpApp { Section::Rendering => { ui.checkbox(&mut self.doc.display.shadow_antialiasing, tr!("settings.shadow_antialiasing")) .on_hover_text(tr!("settings.smooth_shadow_boundaries_with_4_multisampling_only_shadows")); - } - Section::Diagnostics => { - ui.text_weak(tr!("settings.tools_for_checking_performance_and_troubleshooting")); ui.checkbox(&mut self.general_settings.show_fps, tr!("settings.show_frames_per_second")) .on_hover_text(tr!("settings.display_the_rendering_frame_rate_in_the_lower")); } @@ -1018,6 +1012,23 @@ mod tests { assert_eq!(grouped.iter().filter(|&&value| value == section).count(), 1); } } + + #[test] + fn rendering_includes_frame_rate_and_loads_the_former_diagnostics_page() { + let legacy: GeneralSettings = serde_json::from_str(r#"{"section":"Diagnostics","show_fps":true}"#).unwrap(); + assert_eq!(legacy.section, Section::Rendering); + assert!(legacy.show_fps); + assert_eq!(serde_json::to_value(&legacy).unwrap()["section"], "Rendering"); + + let ctx = egui::Context::default(); + let mut app = BenchUpApp::with_document(Document::default(), None, true); + app.general_settings = legacy; + let output = ctx.run_test_ui(egui::RawInput::default(), |ui| app.settings_contents(ui)); + let text = rendered_text(&output.shapes); + for label in [tr!("settings.shadow_antialiasing"), tr!("settings.show_frames_per_second")] { + assert!(text.iter().any(|value| value == label)); + } + } use super::*; #[test] diff --git a/crates/benchup-app/src/sidebar.rs b/crates/benchup-app/src/sidebar.rs index 2029fe2..a65db6e 100644 --- a/crates/benchup-app/src/sidebar.rs +++ b/crates/benchup-app/src/sidebar.rs @@ -94,6 +94,7 @@ pub(super) struct InspectorPreferences { section_aliases: std::collections::BTreeMap, scroll_positions: std::collections::BTreeMap, modal_sizes: std::collections::BTreeMap, + agent_setup_tab: agent_link::SetupTab, } impl Default for InspectorPreferences { @@ -142,6 +143,7 @@ impl Default for InspectorPreferences { section_aliases: Default::default(), scroll_positions: Default::default(), modal_sizes: Default::default(), + agent_setup_tab: Default::default(), } } } @@ -179,6 +181,26 @@ mod guide_preference_tests { fn flush(&mut self) { self.flushed = true; } } + #[test] + fn agent_setup_tab_defaults_and_saves_without_a_model_edit() { + let old: InspectorPreferences = serde_json::from_str("{}").unwrap(); + assert_eq!(old.agent_setup_tab, agent_link::SetupTab::ClaudeCode); + let ctx = egui::Context::default(); + let mut app = BenchUpApp::with_document(Document::default(), None, true); + let mut storage = Storage::default(); + for tab in agent_link::SetupTab::ALL { + app.agent_link.setup_tab = tab; + app.persist_inspector_changes(&ctx, Some(&mut storage), false); + assert!(storage.flushed); + assert!(!app.project.modified); + assert!(app.dirty_since.is_none()); + let mut restored = BenchUpApp::with_document(Document::default(), None, true); + restored.restore_inspector_preferences(&egui::Context::default(), Some(&storage)); + assert_eq!(restored.agent_link.setup_tab, tab); + storage.flushed = false; + } + } + #[test] fn tool_rail_separators_default_and_restore_without_model_edits() { let legacy: InspectorPreferences = serde_json::from_str("{}").unwrap(); @@ -907,6 +929,7 @@ impl BenchUpApp { self.account.open = preferences.account_open; self.account.menu_open = preferences.account_menu_open; self.general_settings = preferences.general.clone(); + self.agent_link.setup_tab = preferences.agent_setup_tab; if first_launch { self.general_settings.language = detect_language().into(); } @@ -1127,6 +1150,7 @@ impl BenchUpApp { .positions .clone() }), + agent_setup_tab: self.agent_link.setup_tab, } } @@ -2573,13 +2597,13 @@ mod preference_tests { } #[test] - fn frame_rate_diagnostics_save_immediately_and_default_off() { + fn frame_rate_rendering_settings_save_immediately_and_default_off() { let ctx = egui::Context::default(); let mut storage = SavedPreferences::default(); let mut app = BenchUpApp::with_document(Document::default(), None, true); for enabled in [true, false] { app.general_settings.show_fps = enabled; - app.general_settings.section = settings::Section::Diagnostics; + app.general_settings.section = settings::Section::Rendering; storage.flushed = false; app.persist_inspector_changes(&ctx, Some(&mut storage), false); assert!(storage.flushed); @@ -2587,12 +2611,31 @@ mod preference_tests { let mut restored = BenchUpApp::with_document(Document::default(), None, true); restored.restore_inspector_preferences(&egui::Context::default(), Some(&storage)); assert_eq!(restored.general_settings.show_fps, enabled); - assert_eq!(restored.general_settings.section, settings::Section::Diagnostics); + assert_eq!(restored.general_settings.section, settings::Section::Rendering); } let old: settings::GeneralSettings = serde_json::from_str("{}").unwrap(); assert!(!old.show_fps); } + #[test] + fn saved_diagnostics_page_restores_as_rendering_without_losing_preferences() { + let mut storage = SavedPreferences::default(); + let mut legacy = InspectorPreferences::default(); + legacy.general.section = settings::Section::Rendering; + legacy.general.show_fps = true; + legacy.general.orbit_speed = 2.; + eframe::set_value(&mut storage, PREFERENCE_KEY, &legacy); + // Exercise the storage format used by desktop and browser installs. + let saved = storage.values.get_mut(PREFERENCE_KEY).unwrap(); + assert!(saved.contains("Rendering")); + *saved = saved.replacen("Rendering", "Diagnostics", 1); + let mut app = BenchUpApp::with_document(Document::default(), None, true); + app.restore_inspector_preferences(&egui::Context::default(), Some(&storage)); + assert_eq!(app.general_settings.section, settings::Section::Rendering); + assert!(app.general_settings.show_fps); + assert_eq!(app.general_settings.orbit_speed, 2.); + } + #[test] fn shadows_and_sun_save_together_and_default_off_for_older_preferences() { let ctx = egui::Context::default();