docs/pkg/cli.md

pkg/cli — argument-driven client policy

This package is the CLI library behind local ez-agent invocations with arguments. It parses actions, selects safe prompt/resume profiles, calls the daemon, and presents results in the terminal. It also owns the complete help text, including the opt-in AgentHub forms that cmd/ez-agent dispatches before local CLI parsing. For binary selection, invocation syntax, AgentHub mode, and no-argument TUI behavior, see cmd/ez-agent; for the TUI, see pkg/tui. Socket lifecycle and autolaunch live in internal/frontend; typed RPC and wire frames in pkg/ezagentapi and pkg/protocol; daemon lifecycle and session persistence in pkg/service and pkg/service/session.

What does this package own?

RoleMain authority
Parse, dispatch, prompt and management presentationmain.go
Complete local and AgentHub usage textmain.go: WriteUsage
Daemon adapter and asynchronous event queueclient.go, async_queue.go
Fresh/resume profile selection and validationlaunch_policy.go
Prompt stream ordering and terminal assemblyprompt_stream.go, stream_assembler.go
Canonical completion and hidden compaction ancestrycanonical_prompt.go

The binary owns signal handling, defaults to autolaunch, and sends a local argument-bearing invocation to cli.RunContext; an argument-free invocation enters the local TUI. A first --hub argument instead enters the AgentHub frontend and never reaches local CLI parsing. The package receives autolaunch from its caller.

For connected CLI operations, cancellation closes only the client's transport. This releases pending RPCs and event waits, returns an error, and lets the binary exit with status 1. In particular, Ctrl-C must not send an agent abort or daemon stop: accepted work continues in the persistent daemon after the client exits. The binary prints Continuing in background. to stdout after SIGINT interrupts a scripted CLI invocation. SIGTERM uses the same cancellation path without the notice; TUI modes do not print it.

WriteUsage and usage returned with parsing errors consult internal/hubfeature.Enabled. Without the unreleased Hub marker beside the real client executable, they omit Hub forms, sections and examples while preserving local help. cmd/ez-agent rejects disabled --hub invocations, including help and offline bookmark actions, before any frontend or state access. The local parser still accepts -p --hub as prompt text. See the client guide for the marker policy.

internal/frontend owns socket readiness, autolaunch, and shutdown; ezagentapi and protocol own typed RPC and wire-frame contracts.

How are fresh prompts and resumes selected safely?

  • Fresh: resolve a cleaned, absolute CWD for discovery and leaf creation, including when inherited from the shell. Repeated Unix path separators such as //workspace/main become /workspace/main, matching the TUI. Resume: retain the leaf's saved configuration scope; --cwd does not replace it.
  • A matching, non-temporary EZ_AGENT_LEAF_ID parent can supply model, thinking, and mode defaults for a fresh child. It supplies neither tools nor plugins; --no-parent disables this hint, and explicit capability lists replace active lists.
  • Resume starts from the stored profile. Explicit values override it; changing model without an explicit thinking level clears inherited thinking so the new model default may apply.
  • Validate explicit model, thinking, mode, tools, and plugins before creating or changing a leaf. Mode is applied through builtin.setmode before the prompt.
  • Never resume the environment leaf or its metadata ancestors. Parent grouping metadata after fresh creation is best effort: warn on failure, keep the child.

How does a prompt become terminal output?

serviceClient adapts selected callbacks into an unbounded FIFO of typed CLI events. Events accepted before a terminal connection error drain first; producers must stop before the queue closes. Receipt keeps queueing while the display waits for lifecycle validation or streaming grace timers; JSONL callbacks do not wait for presentation. Callback arrival order can differ from wire order.

promptStream restricts output to its leaf and validated turns. It retains text and message-end events that overtake turn_start, then replays them when that turn matches the daemon's active turn. That match also admits a turn_start whose agent_start callback has not arrived yet. Unvalidated and abandoned turns cannot print. The display preserves predecessor-before-successor order for admitted turns and checks persisted canonical turns at completion to recover anything still missing.

prompt_completed ends one model run, not necessarily all leaf activity. After that first completion the CLI follows leaf_updated and subsequent agent_end notifications, keeps waiting through background and resumed model processing, and checks current stats before finishing at idle. Releasing the last generic background owner can finish the wait without creating another turn. Error and abort outcomes remain distinct from successful idle completion. Positive snapshot iterations reject stale updates; initial subscription snapshots cannot complete an unstarted invocation.

If compaction hides the accepted prompt, completion reconstructs its physical ancestry through validated continuation boundaries and retained copies. Original assistant output remains in the display; synthetic summaries and copied context are not printed again. Replaced, incomplete, or unproven ancestry is an error, not a successful completion.

Offsets are UTF-8 bytes, not runes; conflicting byte overlaps are errors. Startup status is separate stderr output; ordinary command and prompt presentation uses normal process output. RunWithWriter only redirects non-daemon output (such as help and licenses), so tests of daemon-backed output must use process streams.

Which command families are here?

Prompting and resume, session/list/vacuum management, model and effective configuration lookup, and macro discovery/info/calls are dispatched here. Macro calls have a 30-second client timeout. --third-party-licenses is deliberately local: it exits before daemon or configuration access. --deep requires --vacuum, and --no-parent cannot accompany -s/--leaf. Management actions dispatch before prompt execution; --list-sessions and --list-leaves hide temporary leaves. Use ez-agent --help for current option syntax; this page does not reproduce its flag catalogue. The AgentHub lines in that help are owned by the binary parser and internal/frontend, not this package's local dispatcher.

Where do connected contracts live?