Skip to content

perf(agent): avoid preview allocations for non-truncated strings (fix #894) - #924

Merged
ilblackdragon merged 5 commits into
nearai:stagingfrom
G7CNF:codex/issue-894-truncate-cow
Mar 22, 2026
Merged

ilblackdragon merged 5 commits into
nearai:stagingfrom
G7CNF:codex/issue-894-truncate-cow

Conversation

@G7CNF

@G7CNF G7CNF commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #894 by eliminating unnecessary heap allocation in truncate_for_preview when the input is already within the byte budget.

What changed

  • src/agent/agentic_loop.rs
    • Changed return type from String to Cow<'_, str>.
    • Returns Cow::Borrowed(s) for non-truncated strings.
    • Returns Cow::Owned(...) only when truncation occurs.
    • Added tests proving borrowed vs owned behavior.
  • src/worker/container.rs
    • Added .into() on one match arm so both arms unify as Cow<'_, str>.

Issue

Before / After (explicit)

Before (staging):

pub fn truncate_for_preview(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.to_string()
    } else {
        ...
    }
}

After (this PR):

pub fn truncate_for_preview(s: &str, max: usize) -> Cow<'_, str> {
    if s.len() <= max {
        Cow::Borrowed(s)
    } else {
        Cow::Owned(...)
    }
}

Behavioral verification added:

  • test_truncate_short_string_borrowed -> verifies non-truncated result is borrowed
  • test_truncate_long_string_owned -> verifies truncated result is owned

Validation

Targeted tests run locally:

cargo test -q agent::agentic_loop::tests::test_truncate_short_string_borrowed -- --nocapture
cargo test -q agent::agentic_loop::tests::test_truncate_long_string_owned -- --nocapture
cargo test -q worker::container::tests::test_truncate_multibyte_safe -- --nocapture

All passed.

Scope

Localized performance fix; no user-facing semantic change in preview content.

@github-actions github-actions Bot added scope: agent Agent core (agent loop, router, scheduler) scope: worker Container worker size: S 10-49 changed lines risk: medium Business logic, config, or moderate-risk modules contributor: new First-time contributor labels Mar 11, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a performance improvement by optimizing the truncate_for_preview function. It refactors the function to use std::borrow::Cow<'_, str>, which allows it to return a borrowed reference to the input string when no truncation is necessary, thereby avoiding unnecessary heap allocations. When truncation is required, it still returns an owned string. This change reduces memory overhead and improves efficiency without altering the external behavior of the preview content.

Highlights

  • Performance Optimization: Eliminated unnecessary heap allocations in truncate_for_preview by returning a borrowed string (Cow::Borrowed) when truncation is not needed.
  • Type Signature Change: Updated the return type of truncate_for_preview from String to std::borrow::Cow<'_, str> to enable zero-copy optimization.
  • Behavioral Verification: Added new unit tests to explicitly verify that truncate_for_preview returns a borrowed string when input is short and an owned string when truncation occurs.
  • Type Coercion: Adjusted a match arm in src/worker/container.rs to correctly handle the new Cow<'_, str> return type by adding an .into() call.
Changelog
  • src/agent/agentic_loop.rs
    • Imported the Cow type from std::borrow.
    • Modified the truncate_for_preview function to return Cow<'_, str> instead of String.
    • Updated the logic within truncate_for_preview to return Cow::Borrowed(s) for non-truncated strings and Cow::Owned(format!...) for truncated strings.
    • Added test_truncate_short_string_borrowed to assert that short strings result in a borrowed Cow.
    • Added test_truncate_long_string_owned to assert that truncated strings result in an owned Cow.
  • src/worker/container.rs
    • Added .into() to the Err arm of a match expression to convert the format! result into a Cow<'_, str>, ensuring type compatibility with the Ok arm.
Activity
  • Targeted tests were run locally and all passed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a performance optimization by using Cow<'_ , str> in truncate_for_preview to avoid unnecessary string allocations. The changes are well-implemented and no security vulnerabilities were identified during the audit. However, an issue was found in src/worker/container.rs where a ToolError is passed to truncate_for_preview instead of a &str, which should be addressed for correctness.

Comment thread src/worker/container.rs
@G7CNF
G7CNF force-pushed the codex/issue-894-truncate-cow branch from 446afbf to d5a8947 Compare March 11, 2026 07:30
zmanian
zmanian previously approved these changes Mar 12, 2026

@zmanian zmanian 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.

LGTM. Clean optimization that avoids allocating for non-truncated strings.

  • Return type changes from String to Cow<'_, str> -- borrows the input when no truncation is needed, only allocates when the ellipsis suffix is appended.
  • Cow<str> implements Serialize, Display, Deref<Target=str>, and PartialEq<str>, so all existing call sites (including serde_json::json!() macros, format!(), and string comparisons) continue to work.
  • Note: this changes the truncate_for_preview in agentic_loop.rs only (used by container.rs and job.rs). The separate truncate_for_preview in agent_loop.rs is unchanged. The job.rs call sites were not modified in this diff but compile fine since Cow<str> serializes identically to String in JSON contexts.
  • The container.rs fix correctly adds .into() on the format!() result to produce Cow::Owned.
  • Good test additions verifying Cow::Borrowed vs Cow::Owned variants.

Approved.

@zmanian
zmanian enabled auto-merge (squash) March 12, 2026 18:29
auto-merge was automatically disabled March 14, 2026 09:48

Head branch was pushed to by a user without write access

auto-merge was automatically disabled March 14, 2026 09:48

Head branch was modified

@G7CNF
G7CNF force-pushed the codex/issue-894-truncate-cow branch from d5a8947 to 0a2bc38 Compare March 14, 2026 09:48
@github-actions github-actions Bot added contributor: experienced 6-19 merged PRs and removed contributor: new First-time contributor labels Mar 14, 2026
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
G7CNF and others added 2 commits March 14, 2026 09:57
- Remove redundant `.to_string()` on `&String` in container.rs error arm
- Bind `format!()` result to a let in job.rs to avoid Cow borrowing from temporary
- Merge borrowed/owned Cow assertions into existing tests, drop misleading comments

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added contributor: core 20+ merged PRs and removed contributor: experienced 6-19 merged PRs labels Mar 22, 2026
Keep dedicated `test_truncate_short_string_borrows` and
`test_truncate_long_string_owns` tests so the PR diff contains
new `#[test]` functions, satisfying the regression test enforcement check.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
bkutasi pushed a commit to bkutasi/ironclaw that referenced this pull request Mar 28, 2026
…earai#894) (nearai#924)

* perf(agent): avoid preview allocation on non-truncated strings

* Update src/worker/container.rs

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* chore(ci): annotate test assertions for no-panics gate

* fix: remove unnecessary allocation and consolidate tests

- Remove redundant `.to_string()` on `&String` in container.rs error arm
- Bind `format!()` result to a let in job.rs to avoid Cow borrowing from temporary
- Merge borrowed/owned Cow assertions into existing tests, drop misleading comments

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: restore separate test functions for CI regression check

Keep dedicated `test_truncate_short_string_borrows` and
`test_truncate_long_string_owns` tests so the PR diff contains
new `#[test]` functions, satisfying the regression test enforcement check.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: ilblackdragon@gmail.com <ilblackdragon@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
drchirag1991 pushed a commit to drchirag1991/ironclaw that referenced this pull request Apr 8, 2026
…earai#894) (nearai#924)

* perf(agent): avoid preview allocation on non-truncated strings

* Update src/worker/container.rs

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* chore(ci): annotate test assertions for no-panics gate

* fix: remove unnecessary allocation and consolidate tests

- Remove redundant `.to_string()` on `&String` in container.rs error arm
- Bind `format!()` result to a let in job.rs to avoid Cow borrowing from temporary
- Merge borrowed/owned Cow assertions into existing tests, drop misleading comments

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: restore separate test functions for CI regression check

Keep dedicated `test_truncate_short_string_borrows` and
`test_truncate_long_string_owns` tests so the PR diff contains
new `#[test]` functions, satisfying the regression test enforcement check.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: ilblackdragon@gmail.com <ilblackdragon@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: core 20+ merged PRs risk: medium Business logic, config, or moderate-risk modules scope: agent Agent core (agent loop, router, scheduler) scope: worker Container worker size: S 10-49 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MEDIUM] truncate_for_preview allocates String even for non-truncated short strings

3 participants