On this page

ADR-0305: Declarative app_host runner (Ideas #06 + #28)

Context

Every Tenon app hand-wires the same host code: TTY probing, raw-mode entry and restoration, argv routing, a CLI/Actions surface, an MCP stdio loop, optional loopback HTTP, trace and recording flags, state persistence, and plugin extension points. examples/tui/todo spreads this across main.tn, live.tn, mcp_actions.tn and tui_driver, and each copy re-decides exit codes and approval behaviour.

Decision

Add plugins/app_host/ (app_host.lib, app_host.ext), pure .tn, zero Rust changes. An app supplies one type implementing ActionApp (app_actions), App (tui) and Persist, and main.tn shrinks to a run_app call (plugins/app_host/example/main.tn, 6 lines).

  1. Functional core, thin shells. run_headless(env, spec, app, fs, stdin_text) -> HostResult is pure over a HostFs interface and handles CLI, piped script and MCP stdio. Only run_app, run_interactive and run_http touch Cli, Tty and Net. Tests run the core with test_caps VirtualCli and VirtualFs, so C and interp behave identically.
  2. Probe and degrade. HostEnv carries tty.is_terminal results, TERM and NO_COLOR; tui_headless.choose_output decides. TUI needs a terminal on stdout and stdin and TERM != dumb. Anything else is headless and strips color.
  3. Exit codes. 0 success or --help; 2 unknown flag, command, action or bad args; 1 other failure; 3 an action needing approval is unresolved. Headless never presents, confirms or auto-approves (ADR-0225). A piped script stops at the first such action.
  4. Routing. state | actions | act NAME [JSON], --mcp, --http [loopback addr], --trace-out, --record x.tui|x.cast, --store. Non-loopback --http, conflicting modes and bad record extensions exit 2.
  5. Store without a rename primitive. Fs offers no rename, so saves write a hash-framed copy to <path>.next and then to <path>. Loading verifies the SHA-256 and falls back to .next; a torn write never loses the last good state.
  6. Interactive driver. Same loop as tui_driver: wait_ms == -1 sleeps idle, Event.Presented flows from the runtime, caps negotiation honors Caps.cluster. Added: PollHook scheduling (due hooks arrive as Event.Feed of poll:<name> lines and shorten the wait only when due), .tui recording through tui.scenario, .cast through a local asciicast v2 encoder. tui_replay is not used because it does not compile against the current Caps on main.
  7. Extension slots (script_host). ExtRegistry mounts plugins whose api passes script_host.api.accepts, exposes ext.<plugin>.<action> specs, and routes calls as an ext.call ActionEffect for the shell to send to a script_host session. Traps and crashes feed script_host.policy.Health; a disabled plugin loses its actions and its panel renders [ext <p> disabled] while the host and other plugins continue. ext_on_reload re-enables it.
  8. Loopback HTTP. http_handle wraps app_actions_mcp.handle_sse_post as a pure request-to-response function; run_http adds a bounded accept loop (max_requests).

Deviations and limits

Consequences

Apps get consistent exit codes, approval safety and surfaces from one declarative call. New surfaces are added in one place. Cost: one more plugin and a Persist interface for apps that want persistence.