Skip to content

feat(cli): make MAX_CODE_BLOCK_LINES configurable via env vars - #9301

Merged
jamadeo merged 7 commits into
aaif-goose:mainfrom
spendletonliveaction:fix/configurable-max-code-block-lines
May 22, 2026
Merged

feat(cli): make MAX_CODE_BLOCK_LINES configurable via env vars#9301
jamadeo merged 7 commits into
aaif-goose:mainfrom
spendletonliveaction:fix/configurable-max-code-block-lines

Conversation

@spendletonliveaction

Copy link
Copy Markdown
Contributor

Closes #9300.

Summary

Makes the hard-coded MAX_CODE_BLOCK_LINES (50) and TRUNCATED_SHOW_LINES (20) configurable via environment variables in crates/goose-cli/src/session/streaming_buffer.rs. Also adds an option to disable code block truncation entirely.

Motivation

The streaming buffer truncates code blocks longer than 50 lines, saving full content to a temp file. This forces users who want to see complete code output to locate and parse the temp file — painful for workflows that pipe output or need inline visibility. Users had no way to adjust or bypass this behavior.

Design

Three new environment variables, read once via LazyLock (zero per-call overhead):

Variable Effect Default
GOOSE_MAX_CODE_BLOCK_LINES Threshold line count before truncation kicks in 50
GOOSE_TRUNCATED_SHOW_LINES Number of lines shown before the "... (N more lines)" cut 20
GOOSE_NO_CODE_TRUNCATION Set to 1 or true to disable truncation entirely unset (truncation enabled)

When GOOSE_NO_CODE_TRUNCATION is active, truncate_code_blocks returns content unchanged — no temp file is created.

fn max_code_block_lines() -> Option<usize> {
    static VALUE: LazyLock<Option<usize>> = LazyLock::new(|| {
        if std::env::var("GOOSE_NO_CODE_TRUNCATION")
            .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
            .unwrap_or(false)
        {
            return None;
        }
        Some(
            std::env::var("GOOSE_MAX_CODE_BLOCK_LINES")
                .ok()
                .and_then(|v| v.parse().ok())
                .unwrap_or(DEFAULT_MAX_CODE_BLOCK_LINES),
        )
    });
    *VALUE
}

Tests

$ cargo test -p goose-cli -- streaming_buffer
running 1 test
test crates/goose-cli/src/session/streaming_buffer.rs - session::streaming_buffer (line 10) ... ok
test result: ok. 1 passed; 0 failed

cargo fmt -p goose-cli --check and cargo clippy -p goose-cli -- -D warnings both clean.

Backwards compatibility

No behavior change when env vars are unset. Existing defaults (50/20) are preserved. No new dependencies. No public API change.

Checklist

Adds three environment variables to control code block truncation in
streaming output:

- GOOSE_MAX_CODE_BLOCK_LINES: threshold before truncation (default: 50)
- GOOSE_TRUNCATED_SHOW_LINES: lines shown before the cut (default: 20)
- GOOSE_NO_CODE_TRUNCATION: set to "1" or "true" to disable entirely

Closes aaif-goose#9300.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Stephen Pendleton <spendleton@bluecatnetworks.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fef648529d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/goose-cli/src/session/streaming_buffer.rs
…length

If GOOSE_TRUNCATED_SHOW_LINES exceeds the actual code block line count,
the subtraction would underflow. Clamp to lines.len() before subtracting.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Stephen Pendleton <spendleton@bluecatnetworks.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0c89f67d4f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/goose-cli/src/session/streaming_buffer.rs Outdated
Stephen Pendleton and others added 3 commits May 18, 2026 10:17
When GOOSE_TRUNCATED_SHOW_LINES exceeds GOOSE_MAX_CODE_BLOCK_LINES,
blocks just over the threshold would render all lines then show
"... (0 more lines)" with a temp file. Clamp to both max_lines and
lines.len() so truncation always omits at least one line.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Stephen Pendleton <spendleton@bluecatnetworks.com>
…SE_NO_CODE_TRUNCATION to env vars guide

Signed-off-by: Douwe Osinga <douwe@squareup.com>

@DOsinga DOsinga left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is clean and well-scoped. Merged main and added a commit to document the three new env vars (GOOSE_MAX_CODE_BLOCK_LINES, GOOSE_TRUNCATED_SHOW_LINES, GOOSE_NO_CODE_TRUNCATION) in the environment variables guide.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7d5669251a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/goose-cli/src/session/streaming_buffer.rs
The previous truncation matched the closing fence with `find("\n```")`
or `find("\n~~~")`. Two issues:

1. A block opened with a longer outer fence (e.g. ` ```` `) would be
   prematurely closed by a shorter inner fence (e.g. ` ``` `), leaving
   the inner-fence run as a stray closer in the output.

2. A code line that merely *begins* with backticks (` ``` not a fence`)
   was misread as the closing fence.

Track the actual opening fence run length and require closing fences
to occupy the whole line (only fence chars + optional trailing
whitespace), matching CommonMark fence semantics.

Extracted `truncate_code_blocks_with(content, max_lines, show_lines)`
so the behaviour is testable without setting env vars, and added tests
for longer outer backtick/tilde fences and non-fence backtick lines.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Stephen Pendleton <spendleton@bluecatnetworks.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 75bbb0210a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/goose-cli/src/session/streaming_buffer.rs
GOOSE_MAX_CODE_BLOCK_LINES and GOOSE_TRUNCATED_SHOW_LINES are
documented as positive integers, but the previous parser accepted 0.
With max=0, every non-empty fenced block was treated as over-limit and
rendered with zero visible lines plus a temp-file pointer, effectively
hiding all inline code output for users who supplied an invalid value.

Validate the parsed value with `> 0` so zero (and other invalid input
like negatives or non-numerics, which were already rejected by parse
failure) falls back to the documented default.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Stephen Pendleton <spendleton@bluecatnetworks.com>
@jamadeo
jamadeo added this pull request to the merge queue May 22, 2026
Merged via the queue into aaif-goose:main with commit f3260f4 May 22, 2026
23 checks passed
shafqatevo pushed a commit to shafqatevo/goose that referenced this pull request Aug 7, 2026
…goose#9301)

Signed-off-by: Stephen Pendleton <spendleton@bluecatnetworks.com>
Signed-off-by: Douwe Osinga <douwe@squareup.com>
Co-authored-by: Stephen Pendleton <spendleton@bluecatnetworks.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Douwe Osinga <douwe@squareup.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Make MAX_CODE_BLOCK_LINES configurable

3 participants