Skip to content

Commit b7c8e91

Browse files
authored
Fix agent-help hidden-flag leak; make check-surface a real drift gate (#542)
* Fix agent-help hidden-flag leak; make check-surface a real drift gate The committed .surface snapshot is generated and enforced by the Go surface package (surface.SnapshotString) via TestSurfaceSnapshot, not by scripts/check-cli-surface.sh. The two generators diverge: the Go one walks the command tree (full inherited globals, excludes hidden flags, no --version leak), while the shell script walks --help --agent (curated salient globals post-#499, and — the bug here — emits hidden flags). Rather than churn .surface to the script's shape (which would break TestSurfaceSnapshot and enshrine the leak), fix the underlying issues: - emitAgentHelp leaked hidden flags because pflag's VisitAll visits them, so `basecamp recordings --help --agent` surfaced the MarkHidden'd, "Not supported" --assignee flag. Skip hidden flags at all three emission sites to match text --help and the tree walker. Add a regression test. - check-surface previously regenerated to /tmp and echoed a line count, catching nothing. Repoint it at TestSurfaceSnapshot (the authority) so it actually fails on drift, and add an update-surface target for regeneration. check-surface-compat / the cli-surface CI job are unchanged (they diff generated-vs-generated across versions). - TestSurfaceSnapshot's -update-surface wrote .surface without a trailing newline; add one so update-surface is idempotent and POSIX-clean. Closes #539. * Write .surface on acknowledged removal-only updates TestSurfaceSnapshot's -update-surface only wrote .surface when there were additions, so a removal-only change (removed lines acknowledged in .surface-breaking) left the removed CMD/FLAG lines in .surface — and consumers that read it as the current surface (check-skill-drift, check-smoke-coverage) would keep validating removed commands. Write whenever there are no unacknowledged removals instead. * Only rewrite .surface when the snapshot actually changed Refine the -update-surface write condition into a testable predicate, shouldWriteBaseline: write when update mode is on, there are no unacknowledged removals, and the baseline bytes differ from the freshly generated surface. The byte-difference guard keeps removal-only updates (acknowledged in .surface-breaking) writing while making an unchanged surface a true no-op. Add a TestShouldWriteBaseline regression test covering removal-only, addition, no-change, unacknowledged-removal, and non-update cases. Also refresh 'make help': describe check-surface as the drift gate and list the new update-surface target. * Harden surface drift gate and hidden-flag test from review - check-surface now fails when the committed .surface differs from the generated surface even if the only delta is an acknowledged removal that was never regenerated (baselineNeedsRegen). Previously such a baseline passed the gate while still feeding stale removed lines to check-skill-drift and check-smoke-coverage. - TestAgentHelpOmitsHiddenFlags now asserts Execute() succeeds and checks both flags and inherited_flags, matching the guarantee that no hidden flag appears anywhere in agent help. - check-surface's failure hint is now conditional (TestSurfaceSnapshot can fail for reasons other than drift). Add TestBaselineNeedsRegen covering the acknowledged-removal, no-change, addition, and unacknowledged-removal cases.
1 parent 51c3493 commit b7c8e91

4 files changed

Lines changed: 138 additions & 12 deletions

File tree

‎Makefile‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,23 @@ test-release:
354354
MACOS_SIGN_P12= MACOS_SIGN_PASSWORD= MACOS_NOTARY_KEY= MACOS_NOTARY_KEY_ID= MACOS_NOTARY_ISSUER_ID= \
355355
goreleaser release --snapshot --skip=publish,sign --clean
356356

357-
# Generate CLI surface snapshot (validates binary produces valid output)
357+
# Verify the committed CLI surface snapshot (.surface) matches the command tree.
358+
# TestSurfaceSnapshot is the authority: it diffs .surface against surface.Snapshot
359+
# and fails on any drift, pointing here for regeneration.
358360
.PHONY: check-surface
359-
check-surface: build
360-
@command -v jq >/dev/null 2>&1 || { \
361-
echo "ERROR: jq is required for check-surface but was not found."; \
362-
echo "Install with: brew install jq (macOS), apt-get install jq (Debian/Ubuntu)"; \
361+
check-surface: check-toolchain
362+
@BASECAMP_NO_KEYRING=1 $(GOTEST) $(BUILD_TAGS) ./internal/commands/ -run TestSurfaceSnapshot -count=1 || { \
363+
echo; echo "TestSurfaceSnapshot failed — if the committed .surface is stale, run: make update-surface"; \
363364
exit 1; \
364365
}
365-
scripts/check-cli-surface.sh $(BUILD_DIR)/$(BINARY) /tmp/cli-surface.txt
366-
@echo "CLI surface snapshot generated ($$(wc -l < /tmp/cli-surface.txt) entries)"
366+
@echo "CLI surface snapshot up to date ($$(wc -l < .surface) entries)"
367+
368+
# Regenerate the committed CLI surface snapshot (.surface) from the command tree.
369+
# Accepts additions; removals must be acknowledged in .surface-breaking.
370+
.PHONY: update-surface
371+
update-surface: check-toolchain
372+
@BASECAMP_NO_KEYRING=1 $(GOTEST) $(BUILD_TAGS) ./internal/commands/ -run TestSurfaceSnapshot -update-surface -count=1
373+
@echo "CLI surface snapshot regenerated ($$(wc -l < .surface) entries)"
367374

368375
# Compare CLI surface against baseline (fails on removals)
369376
.PHONY: check-surface-diff
@@ -542,7 +549,8 @@ help:
542549
@echo " lint-actions Lint GitHub Actions workflows (actionlint + zizmor)"
543550
@echo " tidy-check Verify go.mod/go.sum are tidy"
544551
@echo " check Run all checks (local CI gate)"
545-
@echo " check-surface Generate CLI surface snapshot (validates --help --agent output)"
552+
@echo " check-surface Verify committed .surface matches the command tree (TestSurfaceSnapshot)"
553+
@echo " update-surface Regenerate committed .surface from the command tree"
546554
@echo " check-surface-diff Compare CLI surface snapshots (fails on removals)"
547555
@echo " check-skill-drift Verify skill references match CLI surface"
548556
@echo ""

‎internal/cli/help_test.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,44 @@ func TestAgentHelpOmitsArgsWhenEmpty(t *testing.T) {
532532
assert.NotContains(t, out, `"args"`)
533533
}
534534

535+
// Hidden flags (e.g. recordings --assignee, MarkHidden'd as "Not supported")
536+
// must not leak into agent help — pflag's VisitAll visits hidden flags, so
537+
// emitAgentHelp filters them to match text --help, which never lists them.
538+
func TestAgentHelpOmitsHiddenFlags(t *testing.T) {
539+
isolateHelpTest(t)
540+
541+
var buf bytes.Buffer
542+
cmd := NewRootCmd()
543+
cmd.AddCommand(commands.NewRecordingsCmd())
544+
cmd.SetOut(&buf)
545+
cmd.SetArgs([]string{"recordings", "list", "--help", "--agent"})
546+
require.NoError(t, cmd.Execute())
547+
548+
var info struct {
549+
Flags []struct {
550+
Name string `json:"name"`
551+
} `json:"flags"`
552+
InheritedFlags []struct {
553+
Name string `json:"name"`
554+
} `json:"inherited_flags"`
555+
}
556+
require.NoError(t, json.Unmarshal(buf.Bytes(), &info))
557+
558+
// Hidden flags must not leak via any emission path — local/parent-scoped
559+
// (flags) or inherited (inherited_flags).
560+
everywhere := make([]string, 0, len(info.Flags)+len(info.InheritedFlags))
561+
localNames := make([]string, 0, len(info.Flags))
562+
for _, f := range info.Flags {
563+
everywhere = append(everywhere, f.Name)
564+
localNames = append(localNames, f.Name)
565+
}
566+
for _, f := range info.InheritedFlags {
567+
everywhere = append(everywhere, f.Name)
568+
}
569+
assert.NotContains(t, everywhere, "assignee", "hidden flag leaked into agent help (flags or inherited_flags)")
570+
assert.Contains(t, localNames, "limit", "visible flag should still be present")
571+
}
572+
535573
func TestLeafCommandHelpShowsArguments(t *testing.T) {
536574
isolateHelpTest(t)
537575

‎internal/cli/root.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,12 @@ func emitAgentHelp(cmd *cobra.Command) {
771771
}
772772
}
773773

774-
// Local flags
774+
// Local flags. Hidden flags are skipped to match text --help, which never
775+
// lists them — pflag's VisitAll visits hidden flags, so filter explicitly.
775776
cmd.NonInheritedFlags().VisitAll(func(f *pflag.Flag) {
777+
if f.Hidden {
778+
return
779+
}
776780
info.Flags = append(info.Flags, agentFlag{
777781
Name: f.Name,
778782
Shorthand: f.Shorthand,
@@ -785,6 +789,9 @@ func emitAgentHelp(cmd *cobra.Command) {
785789
// Parent-scoped flags (e.g. --room on chat subcommands) — promoted into
786790
// flags to match text help's parentScopedFlags promotion.
787791
parentScopedFlags(cmd).VisitAll(func(f *pflag.Flag) {
792+
if f.Hidden {
793+
return
794+
}
788795
info.Flags = append(info.Flags, agentFlag{
789796
Name: f.Name,
790797
Shorthand: f.Shorthand,
@@ -796,6 +803,9 @@ func emitAgentHelp(cmd *cobra.Command) {
796803

797804
// Inherited flags — shared logic with filterInheritedFlags (text help)
798805
curatedInheritedFlags(cmd).VisitAll(func(f *pflag.Flag) {
806+
if f.Hidden {
807+
return
808+
}
799809
info.InheritedFlags = append(info.InheritedFlags, agentFlag{
800810
Name: f.Name,
801811
Shorthand: f.Shorthand,

‎internal/commands/surface_test.go‎

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
package commands_test
22

33
import (
4+
"bytes"
45
"errors"
56
"flag"
67
"os"
78
"strings"
89
"testing"
910

11+
"github.com/stretchr/testify/assert"
12+
1013
"github.com/basecamp/cli/surface"
1114
)
1215

1316
var updateSurface = flag.Bool("update-surface", false, "Update .surface baseline file")
1417

18+
// shouldWriteBaseline reports whether -update-surface should rewrite .surface:
19+
// only in update mode, only when no unacknowledged removals remain, and only
20+
// when the baseline actually differs from the freshly generated surface. The
21+
// last condition matters for removal-only changes — removals acknowledged in
22+
// .surface-breaking leave zero additions, so gating on additions alone would
23+
// skip the write and leave stale removed lines in .surface — while still
24+
// leaving an unchanged surface untouched.
25+
func shouldWriteBaseline(update bool, unacknowledgedRemovals int, baseline, current []byte) bool {
26+
return update && unacknowledgedRemovals == 0 && !bytes.Equal(baseline, current)
27+
}
28+
29+
// baselineNeedsRegen reports whether a committed baseline is stale in a way the
30+
// additions/removals checks do not already surface: the bytes differ from the
31+
// generated surface, yet there are no unacknowledged removals and no additions.
32+
// The prime case is a removal acknowledged in .surface-breaking but never
33+
// regenerated — check-surface would otherwise pass while .surface still carries
34+
// the removed lines that check-skill-drift and check-smoke-coverage read.
35+
func baselineNeedsRegen(baseline, current []byte, unacknowledgedRemovals, additions int) bool {
36+
return !bytes.Equal(baseline, current) && unacknowledgedRemovals == 0 && additions == 0
37+
}
38+
1539
func TestSurfaceSnapshot(t *testing.T) {
1640
root := buildRootWithAllCommands()
1741

18-
current := surface.SnapshotString(root)
42+
// Trailing newline so -update-surface writes a POSIX-clean file; the
43+
// comparisons below TrimSpace, so it does not affect drift detection.
44+
current := surface.SnapshotString(root) + "\n"
1945

2046
baselinePath := "../../.surface"
2147

@@ -93,10 +119,54 @@ func TestSurfaceSnapshot(t *testing.T) {
93119
}
94120
}
95121

96-
// Write updated baseline only when -update-surface is set and no removals were found
97-
if *updateSurface && len(removals) == 0 && len(additions) > 0 {
122+
// Catch acknowledged-removal staleness (and any other byte drift the checks
123+
// above don't flag): the baseline no longer matches the generated surface,
124+
// so it must be regenerated to stay canonical for downstream consumers.
125+
if !*updateSurface && baselineNeedsRegen(baseline, []byte(current), len(removals), len(additions)) {
126+
t.Errorf("committed .surface is stale (differs from the generated surface with no additions or unacknowledged removals — e.g. an acknowledged removal was never regenerated); run make update-surface")
127+
}
128+
129+
if shouldWriteBaseline(*updateSurface, len(removals), baseline, []byte(current)) {
98130
if err := os.WriteFile(baselinePath, []byte(current), 0o644); err != nil {
99131
t.Fatalf("writing .surface: %v", err)
100132
}
101133
}
102134
}
135+
136+
func TestShouldWriteBaseline(t *testing.T) {
137+
base := []byte("A\nB\nC\n")
138+
139+
// Removal-only: current dropped B, acknowledged (0 unacknowledged removals),
140+
// no additions — must still write so the removed line leaves .surface.
141+
assert.True(t, shouldWriteBaseline(true, 0, base, []byte("A\nC\n")))
142+
143+
// Addition: current gained D — writes.
144+
assert.True(t, shouldWriteBaseline(true, 0, base, []byte("A\nB\nC\nD\n")))
145+
146+
// No change — must not write (avoids needless churn).
147+
assert.False(t, shouldWriteBaseline(true, 0, base, base))
148+
149+
// Unacknowledged removal present — must not write (drift is a failure).
150+
assert.False(t, shouldWriteBaseline(true, 1, base, []byte("A\nC\n")))
151+
152+
// Not in update mode — never writes.
153+
assert.False(t, shouldWriteBaseline(false, 0, base, []byte("A\nC\n")))
154+
}
155+
156+
func TestBaselineNeedsRegen(t *testing.T) {
157+
base := []byte("A\nB\nC\n")
158+
159+
// Acknowledged removal never regenerated: baseline still has B, current
160+
// dropped it, 0 unacknowledged removals, 0 additions — stale, must flag.
161+
assert.True(t, baselineNeedsRegen(base, []byte("A\nC\n"), 0, 0))
162+
163+
// Identical — not stale.
164+
assert.False(t, baselineNeedsRegen(base, base, 0, 0))
165+
166+
// Additions present — already reported by the additions check, don't
167+
// double-flag here.
168+
assert.False(t, baselineNeedsRegen(base, []byte("A\nB\nC\nD\n"), 0, 1))
169+
170+
// Unacknowledged removal — already reported by the removals check.
171+
assert.False(t, baselineNeedsRegen(base, []byte("A\nC\n"), 1, 0))
172+
}

0 commit comments

Comments
 (0)