BakeNative is under active development. APIs may change.
Skip to Content
BakeNative

Working with AI agents

BakeNative assumes an agent writes most of your app. Here is how to set one up so it can build, check and fix its own work.

Give the agent the renderer, target list and build command. New projects use React in src/App.tsx. Octane apps use src/App.tsrx. The CLI checks target support for either renderer. A native React build must also produce a valid first render at build time.

Why .tsrx works well for agents

Octane’s compiler and template syntax give an agent these tools:

  • Markup is the body. A component is @{ … }: statements and JSX-like markup in one block, with loops as @for (… ; key …). There is no return (...) to keep balanced and no map with a forgotten key.
  • No dependency arrays to get wrong. The compiler infers effect dependencies.
  • Hooks are not order-sensitive. Hook slots are assigned by call site, so a hook inside a condition is fine.
  • Styles live with the component. A <style> block in the component is scoped to it, so an agent changing one screen does not break another.
  • It is still web code. The same file builds for the browser, so the agent can check its work in a browser before you ever open a simulator.
import { useState, useEffect } from 'octane';

export function Counter(props) @{
  const [count, setCount] = useState(0);

  if (!props.paused) {
    useEffect(() => {
      console.log('count is now', count);
    }); // dependencies inferred: [count]
  }

  <button onClick={() => setCount(count + 1)}>{'Count: ' + count}</button>
}

Give the agent these docs

The docs site serves every page as Markdown, made for agents:

URLContents
/llms.txtIndex of all pages with one-line descriptions
/llms-full.txtEvery page in one file
/docs/<page>.mdOne page as Markdown, e.g. /docs/guides/writing-apps.md

Point the agent at them from your project's AGENTS.md (or CLAUDE.md):

AGENTS.md
# BakeNative app

This app is written in React `.tsx` and built with BakeNative for the web and native targets (iOS, macOS, Android, Windows).
Read https://bake-native.com/llms.txt before changing code, and
https://bake-native.com/docs/guides/react-native.md for supported primitives and native updates.

- Entry: src/App.tsx (export `App`). Global CSS: src/styles.css.
- Build and check: `pnpm bake build --target web,ios,mac --bake --json`
  Done means `"ok": true` and no diagnostics.
- The first render must be deterministic: no Date, Math.random or
  environment reads before the first commit (it becomes the baked first screen).
- Native has no DOM (fetch works; WebSocket does not). Branch with `import.meta.env.BAKE_TARGET`.
- Drive the running app with agent-device (`snapshot -i` -> `press @ref --settle` -> `screenshot`);
  read what only BakeNative knows with `bake inspect tree | node | perf | clock | net | prefer | state | errors`
  (debug builds; https://bake-native.com/docs/guides/agents.md#driving-the-running-app).
- Native functions: a spec (`bake native new`) -> implement each platform's generated contract -> `bake build` registers it -> `bake native types`, as in
  https://bake-native.com/docs/guides/native-modules.md#adding-a-native-function

The build-check loop

bake build --json prints one JSON report instead of human-readable output, so an agent can tell exactly what failed:

pnpm bake build --target web,ios,mac --bake --json
{
  "ok": false,
  "diagnostics": ["App.tsrx:12:5: error: Unexpected token"],
  "targets": {
    "web": { "ok": true, "ms": 410, "dir": "dist/web" },
    "mac": { "ok": false, "diagnostics": ["..."] }
  }
}

The exit code is 0 when every target built, 1 when a target failed, and 2 for a usage error. See the CLI reference for every field.

Checking the result

Building is not the same as looking right. Give the agent ways to see what it made:

  • Web: serve dist/web and let the agent open it with a browser tool. This is the fastest loop and catches most layout and logic mistakes.
  • Native vs web: the dev tool shows the native and web renderings side by side and lists every element whose position or size differs.
  • Launch: log LaunchMetrics from the native view. If phase is remounted, the first render differed between the bake and the device; mismatch says where. Ask the agent to make that first render deterministic (see Troubleshooting).

Driving the running app

Two tools, each for what it knows best:

  • agent-device drives the UI the way a person does, through the platform's accessibility tree, on web, iOS, Android and macOS: snapshot, press, fill, screenshot, logs. It is the way an agent operates any app, BakeNative or not.
  • bake inspect reads what only BakeNative knows about the same screen: the core's view tree with rects and computed style, perf counters, the deterministic clock, fetch mocks, environment overrides and the app's own state. It never replaces agent-device for input.

The loop an agent runs on a debug build:

agent-device snapshot -i                  # what is on screen, with @e refs
agent-device press @e4 --settle           # act like a person
agent-device screenshot after.png         # look at it
bake inspect tree                          # why it looks that way: rects, style, test ids
bake inspect node e4                       # one view: props, listeners, resolved CSS

bake inspect tree nodes carry testId (agent-device's identifier), role and name in the same vocabulary as an agent-device snapshot, so the two join on test id, or on role plus name. Their ref (e12) is the view's host id and stays the same for the view's life, unlike agent-device's per-snapshot @e numbering. source (the .tsrx file and line) is empty until the build records it.

bake inspect commands

All output is JSON. Exit status: 0 answered, 1 the app refused the query (the reason is in error), 2 usage error, no app or no transport.

CommandWhat it does
treeEvery view, nested: ref, id, role, name, text, value, testId, rect, states, style, source
node <id>One view (12, e12 or @e12): props, listeners, frame, computed style, the value each declared CSS property resolved to
style, layoutStyle-phase counters; the laid-out element tree
perf [on|off] [--history]The perf monitor's counters (--full: every stage cost); on/off shows or hides the overlay; --history adds the last 30 s in 500 ms buckets (fps, frame work p50 / max, hitches and hitch time, frames over budget, memory), recorded while the monitor is on
clock [driven|live]The clock's state, or switch it
clock advance <ms>Run the timers due within ms virtual ms and commit their frames
clock settle [max_ms]Run every timer due within max_ms (default 10 s)
clock until <text|no-text|idle|net-idle|timers> [value]Advance a frame at a time until the condition holds (--step, --max)
capture [png]macOS and the iOS simulator: the app's root view as on screen at the clock (every animation at the clock's time), its hash, size and clock_ns; writes the PNG when given a path. macOS takes the window's composited pixels (ScreenCaptureKit, screen recording permission)
net / net route <glob> … / net unroute [glob] / net requests [--clear]fetch mocks (--json, --body, --status, --abort [code], --continue, --method, --delay, --times) and the request log
prefer [--scheme dark|light|system] [--font-scale N|system] [--locale TAG|system]Override dark mode, text size and locale until set back to system
state [name…]What the app exposes with exposeState
act click <ref> / act fill <ref> <text>Click a view or type into a field by its ref (e12), as the host's input would, and answer after the frame it caused; for drivers without OS input
errors [--clear]JS errors and render failures the core caught (the red error overlay's records): kind (uncaught, unhandledRejection, render, native, runtime), message, Hermes stack, source, module, code; and overlay, what the overlay shows
a11yThe core's semantic tree: roles, names, states, positions
hello, listThis app's transport facts; every debug app the transports see

Pick the app with --pid (required when several debug apps run: bake inspect never guesses), the platform with --target windows|macos|ios-sim|android (default: the machine you are on), and a device with --device <serial or simulator UDID>.

Only debug builds answer: the transport (a named pipe on Windows, a localhost port on macOS and the iOS simulator, an adb forwarded socket on Android) and the detailed queries are compiled out of release builds. Windows, macOS and the iOS simulator serve it; Android is next, and bake inspect says so instead of guessing. The prebuilt Apple runtime from npm is a release build, so an iOS or macOS app that links it answers only when it is built against a BakeNative checkout (iOS and macOS).

On macOS and the iOS simulator, a debug app listens on 127.0.0.1 and writes /tmp/bakenative-inspect/<pid>.json (readable by your user only) with its port and a random token that bake inspect sends with every query. A simulator app is a process of your Mac, so bake inspect --target ios-sim reaches it the same way; --device <UDID> picks among simulators. An iOS device is not served (its localhost is not your Mac's). act, state, prefer and errors need a debug core: build the Apple core with BAKE_RUST_FEATURES=perf-monitor tools/build-rust-apple.sh (the default core is optimized and answers tree, node, style, layout, clock, net and perf).

Exposing app state

state and the locale part of prefer read hooks the app installs. Import them from @bake-native/octane/devtools:

import { exposeState, onLocaleChange } from '@bake-native/octane/devtools';

exposeState('cart', () => cart.items.map((i) => ({ id: i.id, qty: i.qty })));
onLocaleChange((locale) => setLocale(locale ?? 'en-US'));
bake inspect state cart          # { "cart": [{ "id": "a1", "qty": 2 }] }
bake inspect prefer --locale ko-KR --scheme dark

Values must be JSON. An app without @bake-native/octane/devtools answers state and prefer --locale with an error that says what to import.

e2e tests with time and network control

The clock, net and perf controls above are what e2e tests use to make a run deterministic: drive the clock, mock a request, then read the screen. The BakeNative repository's own e2e tests wrap them in an internal test tool (tests/e2e/inspect on examples/inspect, web, Windows, macOS and the iOS simulator with one test file); it is not a published package. In your own tests, call bake inspect clock, bake inspect net and bake inspect perf from your e2e runner.

Under a driven clock nothing moves on its own: timers, requestAnimationFrame, Date.now(), Math.random (a fixed seed) and CSS animations and transitions all stand at the clock's time. On Apple hosts each animation starts at the clock time of the frame that started it and is shown at the clock's current time after every clock step, so a screenshot taken after the same steps shows the same pixels on every run. What does not follow the clock is the system's own UI (the status bar, a navigation bar's buttons) and JS taking the first screen over, which runs in real time: wait for it before the first capture. The repository's showcase test (tests/e2e/clock) checks this on web, macOS and the iOS simulator: ten fresh runs of the same clicks and clock steps, every capture byte-identical to the first run's, unchanged while real time passes, and different between two clock times.

Native code is agent-friendly too

Adding a native function follows a fixed recipe: bake native new writes the spec and an implementation of its generated contract for each target with a unit test that calls the function without a JS runtime; bake build registers it; bake native types writes the TypeScript. An agent can add and test a native function end to end. See Native modules. For a feature on several platforms, give it a spec and both platforms in one task: Native features: spec first.

Prompts that work

  • "Add a settings screen with a dark-mode toggle. Keep it in one .tsrx file with a scoped <style>. Build with --json and fix diagnostics until ok is true."
  • "This screen remounts on launch (mismatch: row 12: text "3" vs "4"). Find what makes the first render non-deterministic and move it into an effect."
  • "Make the list match the web layout on iOS. Compare with the dev tool's Native vs Web table and fix the largest differences first."

On this page