DevLog
← Back to Blog

One Template, Two Surfaces: How Antigravity Sidecars, Artifacts & Hook Hydration Work

By • 7 min read
This article is also available in Chinese: 🌐 阅读中文版 →

If you build coding harnesses or agent orchestrators, plain chat hits a wall the moment your agent does real background work. Dumping branch tables, pending approvals, and token logs into every reply burns thousands of output tokens and buries the actual conversation.

But moving that dashboard to http://127.0.0.1:4242 reveals a second wall the minute you walk away from your desk: your phone cannot reach 127.0.0.1.

While building github.com/zjshen14/agy-hq (our zero-dependency Go companion harness for Google Antigravity), I had to wire one UI architecture across two very different delivery surfaces: desktop IDE sidebars next to your code, and offline conversation HTML artifacts synced automatically to mobile Remote Control.

This post covers three things: how Antigravity’s two UI surfaces work, why we deleted an 841-line second renderer (console_space.go) and made one ~110 KB template serve both, and how a ~60-token pre-tool hook placeholder stopped the model from re-typing HTML on its first turn.


1. Antigravity in 30 Seconds: Sidecar Webviews vs. HTML Artifacts

If you come from Claude Code, Cursor, or OpenCode, here is the short mental model. Antigravity gives plugins two places to show UI:

  • On your desktop, a sidecar is a live web panel next to the chat—like a cockpit wired directly to your machine.
  • On your phone, an artifact (a file saved inside <appDataDir>/brain/<conversation-id>/) travels with the conversation over Antigravity’s cloud relay—like a printed snapshot that cannot call back to 127.0.0.1 on your laptop.

So the engineering trick is to make one HTML page work cleanly as both.

Architecture diagram comparing Antigravity's two UI surfaces: on desktop, sidecar.json AUX_PANE launches launch.sh (Go hq sidecar console primary, Node main.mjs fallback) and preload.js provides sidecar.fetch token injection plus theme CSS variables; on mobile Remote Control, StaticMode renders SSR HTML plus #hq-bootstrap-state JSON inside hq_console_live.html with clipboard command buttons.

One Go backend feeds two delivery paths: live loopback sidecars on desktop, and self-contained HTML artifacts on mobile.

Surface A: Desktop IDE Sidecar Plugin (AUX_PANE)

In .agents/plugins/agy-hq/sidecars/agy-hq-console/, the plugin registers a UI sidecar via sidecar.json:

  1. Auxiliary pane registration: Declaring SIDECAR_UI_ENTRYPOINT_AUX_PANE tells Antigravity to host the panel beside the chat and open it via a native pill ([Interactive Console](sidecar://agy-hq-console/)).
  2. Go-first launcher (launch.sh): Tries our compiled hq sidecar console proxy first, falling back cleanly to Node main.mjs only if no hq binary is found on the machine.
  3. Auth and theming come for free (preload.js): Antigravity hands each sidecar session its own secret token (x-sidecar-token), and preload.js attaches it to every request through window.sidecar.fetch. The same script forwards IDE theme-change events as CSS variables, so the panel matches light or dark mode automatically.
  4. Live loopback proxy (127.0.0.1:4242): On desktop, the panel polls /api/state on our Go server (hq webui) every 5 seconds with full interactive buttons for approvals, pausing daemons, and triggering runs.

Surface B: HTML Artifacts & the Mobile localhost Cliff

Why not just tell mobile users to set up Tailscale or Cloudflare Tunnels and open :4242 in Safari?

Because two everyday constraints break port forwarding cleanly:

  • Workstations sleep and close lids: Even with a mesh VPN, a sleeping laptop isn’t serving HTTP requests while you check alerts away from your desk.
  • Managed devices block inbound tunnels: Corporate laptops and locked-down phones often forbid reverse tunnels or third-party mesh daemons altogether.

Antigravity solves transport for conversation Artifacts out of the box: any standalone HTML file written inside the session’s artifact directory (hq_console_live.html) syncs automatically as an embedded preview in the mobile Artifact Pane—no SSH tunnel, no exposed ports.

The catch is network isolation. Because an HTML Artifact has zero access to 127.0.0.1:4242, any button expecting POST /api/approve would fail silently on mobile unless the page itself adapts.


2. Retiring an 841-Line Adapter (console_space.go) with StaticMode

Our first attempt built two separate renderers: console.html for 127.0.0.1:4242, and an 841-line Living Space adapter (internal/webui/console_space.go) solely to emit the offline hq_console_live.html snapshot. Very quickly, every new badge or telemetry card landed on desktop first and drifted behind on mobile.

So we deleted all 841 lines of console_space.go (commit 2a03953) and unified both surfaces behind one dual-mode ~110 KB Go template (console.html) toggled by a single StaticMode boolean:

  • One Go state pass: Desktop webviews and mobile snapshots read from the exact same Go state loader (loadState / RenderStaticConsoleHTML) and enforce the same local/private/ credential omission.
  • Everything ships inside one file: When StaticMode is true, Go pre-renders every tab into plain SSR HTML and tucks compact observability/memory bootstrap JSON into an inline <script id="hq-bootstrap-state"> tag so charts and search filters run offline with zero external requests. The desktop’s green LIVE pill swaps to a cyan SNAPSHOT badge, and the 5-second /api/state poller stays off.
  • Offline tabs stay interactive: Switching across Needs Me, Work, Memory, Tokens, and Ops or expanding accordions works instantaneously in the mobile viewer.
  • Safe clipboard fallback for writes: Clicking Approve, Reject, or Run Now in a static snapshot copies the verified CLI command—complete with its SHA-256 digest (hq approve ... --sha256=...)—to your phone clipboard and pops a green toast at the top of the screen (“Copied command (paste in chat)…”) so you can paste and execute directly in mobile chat.

Synthetic mobile screenshot (390x800) of the offline StaticMode hq_console_live.html artifact showing the cyan SNAPSHOT header pill, the top green flash toast reading Copied command (paste in chat): hq approve prop-demo-acme-q3-alert --sha256=e951958440e4, and the condensed Needs Me approval queue.

Real --demo 390×800 render of hq_console_live.html in StaticMode: identical layout to desktop, with a cyan SNAPSHOT pill and top clipboard fallback banner.


3. Cut Turn-1 Token Waste by 99%: Pre-Tool Sentinel Hydration

Unifying desktop and mobile HTML solved template drift, but exposed a silly bottleneck on the very first turn of [HQ] Front Desk—our long-lived coordinator chat pinned to the sidebar that routes incoming work.

To surface artifacts in Antigravity’s Artifact Pane automatically, the coordinator writes hq_console.md and hq_console_live.html via write_file. Originally, the model read our console summary and re-typed thousands of tokens of Markdown tables and HTML back into tool arguments, burning ~9,500 output tokens on Turn 1 and risking typos inside SHA-256 digests.

The principle: If Go already has the exact bytes, the model shouldn’t re-type them. Re-typing burns output tokens and introduces typos—and a single typo in a SHA-256 prefix breaks approval. This same hook pattern works in any harness with a pre-tool hook, including Claude Code hooks and OpenCode plugins.

Flowchart comparing Before (~9,500 output tokens + 841-line console_space.go lagging behind WebUI) vs After (model calls write_file with sentinel '{{HQ_SYNC}}' using ~60 output tokens; PreToolUse hook hydrateConsoleSyncContent swaps in pre-rendered RenderStaticConsoleHTML(StaticMode: true) bytes via hook overwrite JSON).

Intercepting {{HQ_SYNC}} in PreToolUse drops Turn-1 UI hydration from ~9,500 output tokens to ~60 while eliminating copy-paste drift.

Here is how the three-step handoff works:

  1. Turn 1 emits a sentinel: The coordinator sends two tiny parallel write_file calls targeting hq_console.md and hq_console_live.html with Content: "{{HQ_SYNC}}", costing ~60 tokens total.
  2. PreToolUse swaps bytes in-place: Before Antigravity writes either file, our Go PreToolUse hook (hydrateConsoleSyncContent) spots the placeholder, reads the console files Go has already rendered to disk, and returns them inside the hook response’s overwrite field. Antigravity writes the real bytes directly—the model never had to generate them.
  3. Zero-token updates afterward: CLI actions (hq status, hq approve, goal updates) keep both artifact files synced via atomic swaps in 0 LLM tokens, so your mobile Artifact Pane always reflects current state.

Putting It Together & Quickstart

Desktop gets a low-latency loopback cockpit, and the phone gets only what travels with the conversation. Every local-first agent harness eventually hits that boundary. One ~110 KB template with a StaticMode switch—paired with a pre-tool hook that hydrates a sentinel—gives you one UI across both surfaces with near-zero first-turn cost.

Spin up an isolated demo sandbox locally (your real ~/.gemini and vaults stay untouched), then click the Interactive Console sidecar link in chat or open hq_console_live.html in the Artifact Pane:

./bin/hq init --demo && ./bin/hq webui

(When inspecting a real vault without starting background dispatchers or enabling HTTP writes, pass ./bin/hq webui --read-only, which implies --no-heartbeat.)

Share this post

Scan with WeChat

Scan this QR code with WeChat to read or share on mobile.

Link copied to clipboard!