tfui latest

CLI I/O Contract

Every tfui <command> opens its plugin standalone, with the TUI on stderr, and writes its output to stdout on exit. A headless run has no TUI and writes the output as soon as the plugin is done.

This follows the fzf model: interactive UI on stderr, structured output on stdout, so a user gets full interactivity and pipe composability at once.

Modes

Mode TUI stdout Selected by
Standalone Alt-screen on stderr; the user reviews and confirms Plugin output on exit Default: tfui <command> with a TTY on stderr
Headless None, no keyboard input Plugin output as soon as it is done -ci, CI=1, an AI agent environment, or a stderr that is not a TTY
Full TUI Alt-screen on stdout, all plugins; on stderr with -dry-run Nothing; the recorded commands with -dry-run tfui with no command

Mode resolution

if -ci OR CI=1:                    → headless
if CLAUDECODE or AI_AGENT set:     → headless (AI agent environment, see ADR-0022)
if stderr not TTY:                 → headless (nowhere to render)
otherwise:                         → standalone TUI

Agent environment variables match on non-empty. CI matches 1 exactly; CI=true does not match.

What headless changes

  • The macro driver runs the plugin in place of a terminal: no alt-screen, no keyboard input.
  • Output is written when the plugin is ready or reaches a terminal state.
  • The action verbs (taint, untaint, import) skip their confirmation: CLI arguments are declared intent, as in terraform’s own prompt-free commands (ADR-0022).
  • tfui apply requires -auto-approve, as terraform apply does (see the table below).
  • Exit codes are the same as in standalone mode.

Rules

  1. stdout = data. Tree views, JSON, resource lists, summaries. Never TUI rendering.
  2. stderr = TUI. Alt-screen rendering in standalone mode, and status messages. Never data.
  3. -json controls format, not mode. Both standalone and headless runs respect -json — it changes what’s written to stdout, not whether the TUI appears.
  4. -ci controls mode, not format. Disables TUI entirely. Output format determined by -json flag independently.
  5. A piped stdout keeps the TUI. tfui plan | jq renders the TUI on stderr and sends the output to jq on exit.

Full I/O Table

Standalone mode

Command stdout (on exit) stderr Exit
tfui plan Tree view TUI (alt-screen) 0/2
tfui plan -json Plan JSON TUI (alt-screen) 0/2
tfui apply — TUI; “Apply complete. Resources: …” on exit; a failed plan’s terraform error 0/1
tfui apply -json — TUI; outcome message on exit 0/1
tfui state Addresses (one/line) TUI (alt-screen) 0
tfui state -json Resource JSON array TUI (alt-screen) 0
tfui validate Diagnostics text TUI (alt-screen) 0/1
tfui validate -json Diagnostics JSON TUI (alt-screen) 0/1
tfui output key=value pairs TUI (alt-screen) 0
tfui output -json Outputs JSON TUI (alt-screen) 0
tfui init “Initialized successfully.” TUI (alt-screen) 0/1
tfui version Version text TUI (alt-screen) 0
tfui version -json Version JSON TUI (alt-screen) 0
tfui risk Risk text report TUI (alt-screen); after it, the threshold-met line (threshold from -risk-threshold or tfui.hcl) or the failure diagnostic 0/1/3
tfui risk -json Risk JSON report TUI (alt-screen); after it, the threshold-met line (threshold from -risk-threshold or tfui.hcl) or the failure diagnostic 0/1/3

Headless mode

Command stdout stderr Exit
tfui plan -ci Tree view — 0/2
tfui plan -ci -json Plan JSON — 0/2
tfui apply -ci -auto-approve — “Apply complete. Resources: …” 0
tfui apply -ci (no -auto-approve) — “apply not allowed for non-interactive use” (mirrors terraform) 1
tfui state -ci Addresses (one/line) — 0
tfui validate -ci Diagnostics text — 0/1
tfui validate -ci -json Diagnostics JSON — 0/1
tfui output -ci key=value pairs — 0
tfui output -ci -json Outputs JSON — 0
tfui init -ci “Initialized successfully.” — 0/1
tfui version -ci Version text — 0
tfui risk -ci (threshold off or not met) Risk text report — 0
tfui risk -ci -json (threshold off or not met) Risk JSON report — 0
tfui risk -ci (threshold met, from -risk-threshold or tfui.hcl) Same complete report risk threshold met: high >= high (actual levels substituted) 3
tfui risk -ci -json (threshold met, from -risk-threshold or tfui.hcl) Same complete JSON report risk threshold met: high >= high (actual levels substituted) 3
tfui taint addr -ci — “✓ Tainted addr” 0
tfui untaint addr -ci — “✓ Untainted addr” 0
tfui import addr id -ci — “✓ Imported id as addr” 0
tfui state rm\|mv\|taint\|untaint ... -ci — — 0

Apply writes nothing on stdout: the exit code is the machine signal, and the outcome summary goes to stderr, the status channel.

Headless failures

Every plugin command reports operational failure with empty stdout and exit 1. Terraform failures pass through the Terraform error on stderr; Risk policy failures use fixed diagnostics as specified in Risk command v1.

Command stdout stderr Exit
tfui plan -ci (terraform fails) — terraform error 1
tfui validate -ci (run itself fails) — terraform error 1
tfui apply -ci -auto-approve (fails) — terraform error 1
tfui taint/untaint/import ... -ci (fails) — terraform error 1
tfui init -ci (fails) — terraform error 1
tfui risk -ci (policy failure) — fixed diagnostic 1

Full TUI mode (tfui no command)

Command stdout stderr Exit
tfui (alt-screen) — 0/1
tfui -plan file (alt-screen) — 0/1
tfui -state file (alt-screen) — 0/1
tfui -macro tape -plan file Commands — 0/1
tfui -dry-run Recorded commands, one per line, on exit TUI (alt-screen) 0/1
tfui -dry-run -json Recorded commands as a JSON array, on exit TUI (alt-screen) 0/1
tfui -dry-run (stderr not a terminal) — no TTY on stderr error 1
tfui -ci (alt-screen) — 0/1

-ci has no effect on the full TUI: tfui -ci runs the same TUI as tfui, and still needs a terminal. Mode resolution applies to commands only.

Pre-seeded data on subcommands

-plan and -state work on any command. Pre-seeded state answers the state reads, and a pre-seeded plan answers the plan, instead of terraform (CLI Reference):

Command stdout (on exit) stderr Exit
tfui state -state file Addresses (from pre-seeded data) TUI (alt-screen) 0
tfui state -ci -state file Addresses (from pre-seeded data) — 0
tfui plan -plan file Tree view (from pre-seeded data) TUI (alt-screen) 0/2

Imperative commands (direct execution, no TUI)

Command stdout stderr Exit
tfui workspace show Workspace name — 0/1
tfui workspace list Workspace names — 0/1
tfui workspace select name — Status message 0/1
tfui workspace new name — Status message 0/1
tfui workspace delete name — Status message 0/1
tfui force-unlock id — Status/prompt 0/1
tfui scaffold -yes HCL content Status message 0/1

Exit Codes

Code Meaning Scope
0 Success / no changes All commands
1 Error / validation failure All commands
2 Plan has changes, as terraform’s -detailed-exitcode plan command only
3 Complete assessment meets the selected threshold risk command only; see Risk command v1
130 Interrupted by SIGINT All commands
143 Terminated by SIGTERM All commands

A flag or argument error that stops the command before its plugin runs exits 1. A macro run’s codes are in the macro language.

Risk command v1

The first-version risk contract defines the report and comparison used by tfui risk. Risk uses the same Standalone/Headless mode resolution: TUI on stderr in Standalone, no TUI in Headless; -json changes only stdout format. The reference owns report schemas and threshold semantics; this table owns its channels and exits.

Risk terminal outcome stdout (on quit in Standalone; on terminal completion in Headless) stderr after TUI Exit
Complete, threshold off or below; or empty eligible set Complete text/JSON report Empty 0
Complete, inclusive threshold met Same complete text/JSON report risk threshold met: high >= high (actual levels substituted), followed by LF 3
Configuration/source/input/compilation/evaluation/validation failure, including partial Empty, including JSON mode Fixed diagnostic from the risk contract, followed by LF 1
Terraform acquisition fails Empty Existing Terraform error pass-through 1
SIGINT / SIGTERM or interrupted acquisition Empty Existing interrupt behavior 130 / 143

Exit 3 is Risk-only: assessment completed and met the explicitly selected threshold, from -risk-threshold or the Risk threshold setting in tfui.hcl. It is not an operational failure and does not replace plan-only exit 2. Partial classifications remain inspectable in the TUI but never produce a successful command report or threshold comparison. Configuration errors detected before plugin activation also keep stdout empty and exit 1. Quitting an unfinished assessment follows the interrupt contract; quitting a completed one keeps its report and result. tfui plan -json stays Terraform JSON, with no risk augmentation. None of these interfaces alters Apply or turns show JSON into an executable plan.

Interrupts

tfui owns SIGINT and SIGTERM in the TUI and in every plugin verb run, headless or not. force-unlock and the workspace commands leave them to their default action: the signal ends tfui, and the shell reports 130 or 143. In the runs tfui owns, the first signal stops terraform gracefully: tfui interrupts each running terraform process once, ends the run, and waits for terraform to exit, with no timeout, so terraform can release the state lock and write its state. tfui then runs plugin cleanup and exits 130 for SIGINT or 143 for SIGTERM, whatever the plugin would have returned. It writes nothing to stdout after a signal. A second signal, or any signal while tfui waits, ends tfui at once: it writes terraform was stopped without finishing to stderr, runs plugin cleanup, and exits with the code of that signal. A signal after terraform has finished, while tfui writes output or runs plugin cleanup, ends tfui once cleanup has run, with the code of that signal and without the note. tfui keeps both signals until the process exits, so one that arrives after the run returned, even one the parent process had set to ignore, still sets the exit code. On a system where terraform does not run in its own process group (anything but Linux), terraform already receives the terminal’s Ctrl+C, and tfui does not forward the signal again (ADR-0023).

Quitting the TUI while terraform runs interrupts terraform the same way and writes Waiting for terraform to stop… (Ctrl+C again to force) to stderr while tfui waits. The run was cut short, so tfui treats it as SIGINT: no stdout, the plugin’s stderr, exit 130. A quit after terraform has finished keeps the run’s own output and exit code. While an editor opened from the TUI holds the terminal, tfui ignores both signals, so a Ctrl+C there is the editor’s. A macro run starts no terraform process and never waits: the first signal stops its tape before the next command, or inside a sleep or wait.

Plan tree view

tfui plan writes this to stdout unless -json is given:

+ aws_s3_bucket.logs
~ aws_iam_role.api
- aws_security_group.old
-/+ aws_instance.web

Plan: 1 to add, 2 to change, 1 to destroy.
Risk: high

Symbols: + create, ~ update, - delete, -/+ replace, <= read. A replace counts as a change. Risk: appears only when a change carries risk.

Pipe Scenarios

Standalone + pipe (fzf model)

# TUI renders on stderr, user reviews plan interactively.
# On quit, tree view flows to grep via stdout.
tfui plan | grep "aws_"

# TUI renders on stderr, user reviews plan interactively.
# On quit, JSON flows to jq via stdout.
tfui plan -json | jq '.resource_changes[].address'

# TUI renders on stderr for state browsing.
# On quit, addresses flow to wc.
tfui state | wc -l

In pipe scenarios, the TUI blocks the downstream process until the user quits. This is intentional — the user reviews interactively, then the result flows.

Headless in scripts

# No TUI, immediate output:
CI=1 tfui plan -json | jq '.resource_changes[].address'

# Explicit -ci flag:
tfui validate -ci -json | jq '.valid'

# Auto-detected (stderr not TTY in most CI runners):
tfui plan -json > plan-output.json

# Inside an AI coding agent (CLAUDECODE / AI_AGENT set): bare invocations
# are already headless — no flags needed (ADR-0022):
tfui plan
tfui validate

Macro recording

# Headless; stdout: the terraform commands the tape would run
tfui -macro deploy.tape -plan ./plan.json

Decisions and Tradeoffs

Why TUI on stderr?

The fzf model is proven: render UI on stderr (or /dev/tty), emit result on stdout. This means:

  • tfui plan | jq works — TUI is interactive while pipe stays clean
  • stdout is always machine-parseable (no ANSI escape sequences mixed in)
  • The TUI is feedback/interaction, stdout is data — clean Unix separation

Why block until user quits?

The user reviews the plan interactively (expand/collapse, filter, inspect attributes), then presses q to exit. Only then does the output flow to stdout. This is the same UX as fzf, less, or git log with a pager — review, then exit, then the terminal has the result.

Why -ci instead of auto-detecting stdout pipe?

When stdout is piped (tfui plan | jq), we still show the TUI on stderr. The user wants both: interactive review AND piped output. Auto-disabling TUI on pipe would break this.

-ci is the explicit “I don’t want any TUI at all” signal; the other triggers are in Mode resolution. Runners that set CI=true, such as GitHub Actions, run headless because their stderr is not a TTY.

Why agent environments force headless

Recorded in ADR-0022. Terraform for AI Coding Agents is the agent-facing overview.

Why full TUI (tfui no args) uses stdout?

The full multi-plugin TUI has no single “result” to emit — it’s a dashboard. Rendering on stdout (alt-screen) is standard for full-screen TUI apps. Only standalone mode (single plugin invocation) uses the stderr+stdout split, and tfui -dry-run, whose result is the recorded commands for review; they are shell-quoted and carry -chdir=<dir>, the directory terraform runs in (the -chdir member, else the basedir, else the project).

Edit this page on GitHub