# 1. A text input on the GPU

> **Status: partly implemented.** The shared Coax chat screen module
> (`apps/shared/chat/screen.coax`), the primary desktop consumer
> (`apps/openagents-desktop/page.coax`), and the mobile consumer
> (`apps/openagents-ios/gpu-page.coax`) pass their `#test` checks for
> the `tree` / `type composer.input hello` / `state draft` / `counts` trace
> and duplicate-ID refusal on `cpu`, beside
> `openagents/examples/cases/composer-type.ops`. Remaining for native hosts
> (`macos-arm64`, `linux-x86_64`, `ios-simulator`, `ios-device`): wiring the
> standalone `composer.input` screen into the desktop and iOS host shells and
> running their native accessibility UI tests.

## Premise

This is the first chapter, so there is nothing before it. You build the
smallest OpenAgents app that runs: one screen grounded in the primary
desktop app (`apps/openagents-desktop`)—and consumed by mobile
(`apps/openagents-ios`) from the same shared Coax module—with one text
input, the chat page's composer, drawn on the GPU by the shared component
kit. You can type into it with the keyboard, accessibility can find it, and
`coax drive` can type into it. Nothing else is in the app yet.

## Draft

The book is written first and the app is built to it
([README](README.md#book-driven-development)). So the app starts as small
as a chapter can make it, and each chapter adds one thing you can see.
Chapter 1 adds a text input.

The app is compiled Coax, linked into the native desktop binary (and the
mobile binary from the same shared module). There is no JavaScript, no
JavaScriptCore, no Wasm and no over-the-air update. Desktop is the primary
host from chapter 1 onward, with [`apps/shared/chat/screen.coax`](../../apps/shared/chat/screen.coax)
keeping the screen logic shared across desktop and mobile.

The screen is one Metal view. The shared display-list kit in
[`components/`](../../components) draws every component on it
([GPU components on native](../../components/gpu-components.md)). The
platform supplies only three things a GPU surface can't: the editor of the
focused text field (the keyboard, input methods, AutoFill), the
accessibility tree, and system sheets. This chapter uses the first two.

One frame on the screen goes through four pieces, all on main today
(stage G0):

| Piece | What it does | File |
| --- | --- | --- |
| Frame context, `UiFrame` | Carries the sheet, the text measure, the skin, the input snapshot and the clock for one frame | [`components/kit/frame.coax`](../../components/kit/frame.coax) |
| Keyed state, `KeyedStore` | Keeps state that outlives a frame (entry motion, caret) under a stable ID | [`components/core/keyed.coax`](../../components/core/keyed.coax) |
| Semantic table, `ElTable` | One row per thing you can press, type into or read: ID, role, label, state, value, bounds, action | [`components/core/elements.coax`](../../components/core/elements.coax) |
| Surface commit, `UiSurface` | Commits the display list, the hit regions and the rows together, or refuses the frame and keeps the last one | [`components/paint/surface.coax`](../../components/paint/surface.coax) |

The composer is a component call with a stable ID, `composer.input`. It
measures itself, paints its frame, records one semantic row with the role
of a text field and the label "Message", and records its hit region over
the same box. It never calls the host and never changes app state.

When you tap the field, the host places the platform editor over the
row's committed bounds and makes it first responder. While the field has
focus, the editor draws the text, the placeholder and the caret in the
skin's face and colours, and the GPU paints only the field's frame. There
is one editing authority at a time: the platform owns the composition of an
input method, and the edit buffer in
[`components/core/edit.coax`](../../components/core/edit.coax) receives the
whole value after each edit. Two carets or the same glyphs drawn twice are a
defect.

The GPU login screen already works this way. On macOS Desktop (`apps/openagents-desktop`), `NSTextInputClient` handles IME and editing over the Metal surface while `NSAccessibilityElement` exposes every committed row to VoiceOver; on iOS (`apps/openagents-ios`), [`NativeLogin.swift`](../../apps/shared/native/NativeLogin.swift) keeps one `UITextField` over the focused row and builds one `UIAccessibilityElement` per committed row ([G1 as built](../../components/gpu-components.md#g1-as-built)). This chapter reuses those native overlays for the composer instead of writing a second one.

`coax drive` reads the same rows too. `tree` prints them, `type
composer.input TEXT` types into the field through the same path as the
editor, and `state draft` prints what the app holds. A host that can't do an
operation refuses it by name.

A still screen sleeps. With nothing typed and no motion due, no frame runs
and nothing is submitted to the GPU.

## What we build

Implemented on `cpu` in [`apps/shared/chat/screen.coax`](../../apps/shared/chat/screen.coax), consumed by the primary desktop app ([`apps/openagents-desktop/page.coax`](../../apps/openagents-desktop/page.coax)) and the mobile app ([`apps/openagents-ios/gpu-page.coax`](../../apps/openagents-ios/gpu-page.coax)):

- The shared compiled Coax chat screen with a GPU composer surface, consumed
  by desktop as the primary host and by mobile as a second native host.
- The composer as a frame-context component with ID `composer.input`, its
  row and its hit region.
- The platform editor overlay on the focused field, shared with the login
  screen.
- One accessibility element per committed row.
- The driver operations `tree`, `type`, `state` and `counts` against the
  screen.

It builds on what exists today:

- [`components/gpu-components.md`](../../components/gpu-components.md)
- [`components/rendering.md`](../../components/rendering.md)
- [`components/kit/frame.coax`](../../components/kit/frame.coax)
- [`components/kit/form.coax`](../../components/kit/form.coax) (`ctx_field`)
- [`components/kit/composer.coax`](../../components/kit/composer.coax)
- [`components/core/edit.coax`](../../components/core/edit.coax)
- [`apps/shared/login/screen.coax`](../../apps/shared/login/screen.coax)
- [`apps/shared/native/NativeLogin.swift`](../../apps/shared/native/NativeLogin.swift)
- [`apps/shared/host/apple/ui-scene.m`](../../apps/shared/host/apple/ui-scene.m)
- [`apps/openagents-desktop`](../../apps/openagents-desktop)
- [`apps/openagents-ios`](../../apps/openagents-ios)

### The chapter's check

- **Source:** [`apps/shared/chat/screen.coax`](../../apps/shared/chat/screen.coax),
  [`apps/openagents-desktop/page.coax`](../../apps/openagents-desktop/page.coax),
  [`apps/openagents-ios/gpu-page.coax`](../../apps/openagents-ios/gpu-page.coax),
  [`openagents/examples/composer-input.coax`](../examples/composer-input.coax),
  and [`openagents/examples/cases/composer-type.ops`](../examples/cases/composer-type.ops).
- **Trace:** committed debug/release stdout in [`openagents/examples/composer-input.out`](../examples/composer-input.out)
  and drive trace in [`openagents/examples/cases/composer-type.trace`](../examples/cases/composer-type.trace):
  `composer.input` and `composer.send` commit in the semantic table; `type composer.input hello`
  answers `typed`; `state draft` prints `hello`; `counts` prints `refused 0`; and an idle frame
  sleeps with `0` vertices.
- **Refusal:** a frame that records two rows with the ID `composer.input`
  is refused atomically: the surface keeps its last committed generation (`generation 2`),
  keeps the draft `hello`, and `counts` prints `refused 1 lost 1`. Typing into an unknown
  field (`composer.nope`) answers `no such field`.
- **Independent check:** `coax/bin/coax test apps/shared/chat/screen.coax` and
  `coax/bin/coax test openagents/examples/composer-input.coax` on `cpu`, plus native
  accessibility UI tests on desktop and iOS (remaining for native targets) that find
  the field through the accessibility tree with no `coax drive`.
- **Commands:** `coax/bin/coax run openagents/examples/composer-input.coax`,
  `coax/bin/coax test openagents/examples/composer-input.coax`, and
  `coax/bin/coax test apps/shared/chat/screen.coax`.

Implemented paths: [`apps/shared/chat/screen.coax`](../../apps/shared/chat/screen.coax), [`apps/openagents-desktop/page.coax`](../../apps/openagents-desktop/page.coax), [`apps/openagents-ios/gpu-page.coax`](../../apps/openagents-ios/gpu-page.coax), [`openagents/examples/cases/composer-type.ops`](../examples/cases/composer-type.ops). The primary app is
[`apps/openagents-desktop`](../../apps/openagents-desktop), mobile consumes the
same shared logic in [`apps/openagents-ios`](../../apps/openagents-ios), and
components extend [`components/`](../../components).

## Sources

- **Episodes:**
  - [Episode 289, *OpenAgents*](../../docs/transcripts/289.md): 0:30, the same agent behind every surface
  - [Episode 196, *Ditch the TUI*](../../docs/transcripts/196.md): one interface for every device
- **Old code at `coder-v1.0.0`, crates:** `openagents-ui`
- **Also:**
  - [GPU components on native](../../components/gpu-components.md), stages G0 and G1
  - [Coax Native](../../coax/native.md)
- **Promises covered** ([audit](../../docs/audits/2026-11-10-episode-289-port-audit.md#1-what-episode-289-promised)): P1

## Open questions for the owner

1. Does the first TestFlight build of the lean app ship with only this
   chapter's text input, or does it wait until chapter 4 gives it a reply?

## Source listing

The shared composer layout in [`apps/shared/chat/screen.coax`](../../apps/shared/chat/screen.coax) places `composer.input` and `composer.send` on the GPU frame and exercises duplicate-ID atomic refusal:

<!-- source: apps/shared/chat/screen.coax composer-layout -->
```jai
cs_layout_composer :: (s: [] ComposerScreen, f: [] UiFrame, w: f32, h: f32, duplicate_id: bool) {
  k := f[0].skin;
  margin := cs_margin(s);
  panel_max := cs_panel_max(s);
  pw := ifx w - 2 * margin < panel_max then w - 2 * margin else panel_max;
  px := (w - pw) / 2;
  fh := form_field_height(k);
  py := cs_composer_top(s, k, h);
  send_w := button_width(f[0].m, k, "Send");
  gap := k.space;
  field_w := pw - send_w - gap;
  if field_w < 80 { field_w = 80; }
  send_x := px + field_w + gap;
  send_y := py + (fh - k.control_h) / 2;
  send_state := ifx s[0].state[0].send_enabled then 0 else EL_S_DISABLED;
  _ := ctx_field(f, "composer.input", px, py, field_w, "Message", cs_draft(s), "Message", s[0].focused, 0, CS_HIT_COMPOSER);
  _ := ctx_button(f, "composer.send", send_x, send_y, "Send", s[0].state[0].send_enabled, send_state, CS_HIT_SEND);
  if duplicate_id {
    _ := ctx_field(f, "composer.input", px, py + fh + k.space, field_w, "Message", cs_draft(s), "Message", false, 0, CS_HIT_COMPOSER);
  }
}
```

Run the Chapter 1 example in [`openagents/examples/composer-input.coax`](../examples/composer-input.coax) (committed transcript [`openagents/examples/composer-input.out`](../examples/composer-input.out)):

<!-- source: openagents/examples/composer-input.coax composer-input-drive -->
```jai
ci_drive_line :: (s: [] ComposerScreen, op: string, out: [] u8) -> string {
  n := cs_drive(s, op, out);
  if n <= 0 { return ""; }
  // Trim trailing newline for single-line structured transcript printing.
  end := n;
  while end > 0 && out[end - 1] == '\n' { end -= 1; }
  return cast(string) out[0..<end];
}

ci_run :: (s: [] ComposerScreen, o: [] CsOut) -> bool {
  cs_test_atlas(s[0].atlas[..]);
  s[0].form_factor = CHAT_FORM_DESKTOP;
  v0 := cs_render(s, o[0].v[..], 1280, 420, 1, 0.016, false);
  if v0 <= 0 { return false; }

  buf: [4096] u8;
  print("composer_row=% send_row=%\n", cs_find_composer(s, "composer.input"), cs_find_composer(s, "composer.send"));
  print("type=%\n", ci_drive_line(s, "type composer.input hello", buf[..]));
  _ := cs_render(s, o[0].v[..], 1280, 420, 1, 0.016, false);
  print("draft=%\n", ci_drive_line(s, "state draft", buf[..]));
  print("counts_before=%\n", ci_drive_line(s, "counts", buf[..]));

  // Still screen sleeps: a second render with no dirty surface submits 0 vertices.
  v_idle := cs_render(s, o[0].v[..], 1280, 420, 1, 0.016, false);
  print("idle_vertices=%\n", v_idle);

  // Refusal: a frame recording two rows with ID composer.input is refused atomically,
  // keeping the last committed generation and draft intact.
  _ = cs_render(s, o[0].v[..], 1280, 420, 1, 0.016, true);
  print("counts_after_dup=%\n", ci_drive_line(s, "counts", buf[..]));
  print("draft_kept=%\n", ci_drive_line(s, "state draft", buf[..]));
  print("bad_field=%\n", ci_drive_line(s, "type composer.nope hi", buf[..]));
  return s[0].surface[0].refused == 1 && cs_draft(s) == "hello";
}
```

```shell
coax/bin/coax run openagents/examples/composer-input.coax
```

Expected output:

```text
composer_row=0 send_row=1
type=typed
draft=hello
counts_before=generation 2 refused 0 lost 0 prims 4/512 text 4/128 hits 2/64 rows 2/128
keyed 0/32 refused 0 (full 0, duplicate 0 this frame) retired 0
idle_vertices=0
counts_after_dup=generation 2 refused 1 lost 1 prims 4/512 text 4/128 hits 2/64 rows 2/128
keyed 0/32 refused 0 (full 0, duplicate 0 this frame) retired 0
draft_kept=hello
bad_field=no such field
ok=true
```

Planned paths: none — extends existing `apps/shared/chat/screen.coax`, `apps/openagents-desktop/page.coax`, and `apps/openagents-ios/gpu-page.coax`.
