Skip to content

Commit 6f20e80

Browse files
dipreeclaude
andcommitted
fix(session): keep 'session current --json' stdout parseable when no session exists
'entire session current --json' printed the plain-text hint 'No active session found in this worktree.' to stdout and exited 0, crashing downstream JSON parsers (seen in every codex review-runner sandbox, where the platform pipes the output into JSON.parse). In --json and --transcript modes, report the no-session case on stderr and exit non-zero via SilentError so callers can detect it. The human default keeps the stdout hint and zero exit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent c4cf088 commit 6f20e80

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

cmd/entire/cli/session_current.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ Examples:
4141

4242
sessionID := strategy.FindMostRecentSession(ctx)
4343
if sessionID == "" {
44+
// Machine-readable modes must not emit prose on stdout with a
45+
// zero exit — downstream parsers treat stdout as JSON (or raw
46+
// transcript bytes) and would choke on the hint text. Report
47+
// on stderr and exit non-zero so callers can detect the
48+
// no-session case. The human default keeps the stdout hint.
49+
if jsonFlag || transcriptFlag {
50+
cmd.SilenceUsage = true
51+
fmt.Fprintln(cmd.ErrOrStderr(), "No active session found in this worktree.")
52+
return NewSilentError(errors.New("no active session found in this worktree"))
53+
}
4454
fmt.Fprintln(cmd.OutOrStdout(), "No active session found in this worktree.")
4555
return nil
4656
}

cmd/entire/cli/session_current_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,37 @@ func TestSessionCurrent_NoSessionsPrintsHint(t *testing.T) {
3737
}
3838
}
3939

40+
// Machine-readable modes must keep stdout parseable: with --json and no
41+
// active session, the hint text goes to stderr and the command exits
42+
// non-zero, instead of printing prose to stdout with exit 0 (which crashed
43+
// downstream JSON parsers in the review runner sandboxes).
44+
func TestSessionCurrent_JSONNoSessionErrorsWithCleanStdout(t *testing.T) {
45+
// t.Chdir cannot coexist with t.Parallel; this test mutates process CWD.
46+
dir := t.TempDir()
47+
testutil.InitRepo(t, dir)
48+
t.Chdir(dir)
49+
50+
for _, flag := range []string{"--json", "--transcript"} {
51+
cmd := newSessionCurrentCmd()
52+
var stdout, stderr bytes.Buffer
53+
cmd.SetOut(&stdout)
54+
cmd.SetErr(&stderr)
55+
cmd.SetContext(context.Background())
56+
cmd.SetArgs([]string{flag})
57+
58+
err := cmd.Execute()
59+
if err == nil {
60+
t.Errorf("%s: expected non-zero exit when no session exists", flag)
61+
}
62+
if stdout.Len() != 0 {
63+
t.Errorf("%s: stdout must stay clean for parsers, got: %q", flag, stdout.String())
64+
}
65+
if !strings.Contains(stderr.String(), "No active session") {
66+
t.Errorf("%s: expected 'No active session' on stderr, got: %q", flag, stderr.String())
67+
}
68+
}
69+
}
70+
4071
func TestSessionCurrent_JSONPrintsCurrentSessionInfo(t *testing.T) {
4172
// t.Chdir cannot coexist with t.Parallel; this test mutates process CWD.
4273
dir := t.TempDir()

0 commit comments

Comments
 (0)