Make IsMachineOutput honor an explicit --styled/--md

Merged
JE
3mo ago
+112-2

Follow-through on the one declined thread from #654: IsMachineOutput read flags and config but never Flags.Styled/Flags.MD, while ApplyFlags lets those flags overwrite a configured json/quiet writer. The same invocation was human to the renderer and machine to every gate, and humanFormatOverride in chat.go is this exact rule written once at one call site because the predicate couldn't say it.

Change

One clause in IsMachineOutput, between the machine-flag block and the config switch — machine flags must stay above it, since --json --styled resolves JSON-first in ApplyFlags and the predicate mirrors that ordering.

Deliberately not delegated to EffectiveFormat(). That would also pull in FormatAuto-on-a-pipe (EffectiveFormat()==FormatJSON where IsMachineOutput() is false today), flipping upgrade stdout→stderr, attachment progress, the skill-update nudge, and quickstart/skill/wizard gating for every basecamp foo | grep. It would also be order-sensitive: the existing TestIsMachineOutput* tables never call ApplyFlags, so every row would read the writer's JSON default and pass vacuously.

Callers whose behavior changes

Only when an explicit --styled/--md overrides a configured json/quiet — all flip toward "treat as human", which is what the flag asked for:

  • shouldIncludeStatsInError / shouldPrintStatsToStderr (context.go)
  • root post-run notices (root.go:207,221)
  • attachment progress (attachments.go)
  • quickstart.go routing, skill.go dump→picker (still TTY-gated), upgrade.go
  • the setup/wizard gate (wizardCanRun, setupCanRun after #660)

humanFormatOverride in chat.go stays — the breadcrumb must still carry the flag for the future invocation it suggests.

Tests

  • New table in context_test.go: --styled over config json and --md over config quiet → not machine; config json alone and --json --styled → machine. Existing rows unchanged.
  • TestMachinePredicatesAgree in internal/commands: the regression net for "these must not drift again" — every flag×config combination asserts app.IsMachineOutput() == machineReadsThisOutput(cmd), with all three stdio streams pinned to pipes and the precondition asserted. The one deliberate divergence (FormatAuto on a pipe) is asserted as a divergence rather than skipped, so a change to either side of it shows up.
  • Mutation-checked: deleting the clause fails exactly the six new rows and nothing else; TestChatDeleteConfirmationMatrix passes with and without.

Note: #660 also touches wizard.go (renames the gate to setupCanRun and moves its doc comment). The comment amendment here will need a trivial rebase whichever lands second.

bin/ci green.


Summary by cubic

Makes IsMachineOutput honor explicit --styled/--md flags, so an invocation that overrides a configured json/quiet format is treated as human by all gates instead of being routed as machine while rendering human.

Behavior changes

  • All callers that consulted IsMachineOutput now flip to human for --styled/--md over machine config, covering error stats, post-run notices, attachment progress, quickstart/skill/upgrade routing, and wizard gating.
  • Machine flags (--json, --quiet, etc.) still win over style flags, matching ApplyFlags ordering.

Testing

  • Adds a table test covering the override behavior and a TestMachinePredicatesAgree regression net that asserts the full flag×config matrix against the renderer, with the one deliberate FormatAuto-on-pipe divergence enforced explicitly.