Skip to content

⚡ Bolt: [performance improvement] - #387

Open
Lucenx9 wants to merge 1 commit into
mainfrom
bolt-cow-normalize-11917458976536241630
Open

⚡ Bolt: [performance improvement]#387
Lucenx9 wants to merge 1 commit into
mainfrom
bolt-cow-normalize-11917458976536241630

Conversation

@Lucenx9

@Lucenx9 Lucenx9 commented Aug 2, 2026

Copy link
Copy Markdown
Owner

💡 What: Refactored normalize_agent_name to return std::borrow::Cow<'_, str> instead of String. The function now checks if the string contains uppercase characters before calling .to_lowercase(), and returns Cow::Borrowed for static literal matches. Call sites have been updated to .into_owned() where necessary.

🎯 Why: str::to_lowercase() eagerly allocates a String buffer. By parsing agent names (which are mostly already lowercase), the application incurred a 100% redundant heap allocation penalty for the lowercase conversion, and a second allocation for mapping it to a known literal (like "claude".to_string()).

📊 Impact: Reduces string allocations for normalize_agent_name from 2 to 0 for known valid agent names (the happy path).

🔬 Measurement: Verified by running the test suite (cargo test -p forktty-ui-gtk --no-default-features), which passed successfully, confirming correctness. Tested locally via test_normalize.rs to assert that Cow::Borrowed is successfully returned for unchanged strings.


PR created automatically by Jules for task 11917458976536241630 started by @Lucenx9

Summary

  • Refactor normalize_agent_name to return Cow<'_, str>.
  • Avoid allocations for lowercase, recognized agent names.
  • Preserve lowercase conversion, alias handling, validation, and error behavior.
  • Update GTK socket hook call sites to create owned strings when required.
  • No native GTK or VTE behavior changes.
  • Tests passed, including verification that unchanged names return Cow::Borrowed.
  • No security or privacy impact.

Replaces the eager `.to_lowercase()` call and `String` allocations with `std::borrow::Cow` in `normalize_agent_name`. It now checks for uppercase characters before allocating and returns borrowed strings for literal matches, eliminating an unnecessary double-allocation in the happy path.

Co-authored-by: Lucenx9 <185146821+Lucenx9@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Agent name normalization

Layer / File(s) Summary
Conditional agent name normalization
crates/forktty-ui-gtk/src/socket_cli/hooks/install.rs, .jules/bolt.md
normalize_agent_name returns borrowed or owned Cow<str> values based on casing and aliases.
Owned values at call sites
crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs, crates/forktty-ui-gtk/src/socket_cli/hooks.rs
Hook event storage and agent specification lookup convert normalized names into owned strings.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: rust, security

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title indicates a performance change but does not identify the affected area or the normalize_agent_name optimization. Use a concise, descriptive title such as gtk: optimize agent name normalization.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Privacy Boundary ✅ Passed The runtime diff only changes agent-name normalization to use Cow and owned call-site values; it adds no telemetry, remote network calls, or terminal-output persistence.
Terminal Command Safety ✅ Passed The diff only changes Cow normalization and ownership; all agent inputs still pass agent_spec allowlisting, and no shell, PTY, socket, worktree, or process execution code changed.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt-cow-normalize-11917458976536241630

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@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: 6a765e64fa

ℹ️ 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 .jules/bolt.md
@@ -0,0 +1,3 @@
## 2024-08-02 - [Cow Optimization for String Processing]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove the ad hoc Jules root directory

This task-specific optimization note introduces .jules/ as a new top-level content category even though it only documents this hooks implementation change and has no lasting product or tooling role. Remove it or place any durable documentation near the owning hooks module so the repository does not accumulate tool-specific scratch directories.

AGENTS.md reference: AGENTS.md:L131-L131

Useful? React with 👍 / 👎.

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

🧹 Nitpick comments (2)
crates/forktty-ui-gtk/src/socket_cli/hooks/install.rs (1)

273-285: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add regression coverage for the Cow contract.

Test canonical names, all aliases, and mixed-case input. Assert the normalized text and the expected Cow::Borrowed or Cow::Owned variant. Add one CLI lookup test for a mixed-case alias.

As per coding guidelines: “Add behavior-boundary tests for CLI output, socket JSON responses, and model/store invariants during extraction or refactoring.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/forktty-ui-gtk/src/socket_cli/hooks/install.rs` around lines 273 -
285, Add regression tests around normalize_agent_name covering canonical names,
every supported alias, and mixed-case inputs; assert both normalized text and
whether the result is Cow::Borrowed or Cow::Owned. Add a CLI lookup test that
resolves a mixed-case alias and verifies the expected agent. Use the existing
test utilities and symbols in the install hook and CLI lookup test modules.

Source: Coding guidelines

crates/forktty-ui-gtk/src/socket_cli/hooks.rs (1)

933-933: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Preserve the borrowed Cow at both lookup call sites.

Both changed call sites call .into_owned() before agent_spec. This allocates for Cow::Borrowed results and removes the optimization at those call sites.

  • crates/forktty-ui-gtk/src/socket_cli/hooks.rs#L933-L933: remove .into_owned() before agent_spec.
  • crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs#L38-L38: remove .into_owned() before agent_spec.

The supplied lookup bodies do not retain the normalized name.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/forktty-ui-gtk/src/socket_cli/hooks.rs` at line 933, Preserve the
borrowed Cow returned by normalize_agent_name at both agent_spec lookup call
sites: remove into_owned() in
crates/forktty-ui-gtk/src/socket_cli/hooks.rs:933-933 and
crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs:38-38. The agent_spec lookup
does not retain the normalized name, so pass the Cow directly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/forktty-ui-gtk/src/socket_cli/hooks.rs`:
- Line 933: Preserve the borrowed Cow returned by normalize_agent_name at both
agent_spec lookup call sites: remove into_owned() in
crates/forktty-ui-gtk/src/socket_cli/hooks.rs:933-933 and
crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs:38-38. The agent_spec lookup
does not retain the normalized name, so pass the Cow directly.

In `@crates/forktty-ui-gtk/src/socket_cli/hooks/install.rs`:
- Around line 273-285: Add regression tests around normalize_agent_name covering
canonical names, every supported alias, and mixed-case inputs; assert both
normalized text and whether the result is Cow::Borrowed or Cow::Owned. Add a CLI
lookup test that resolves a mixed-case alias and verifies the expected agent.
Use the existing test utilities and symbols in the install hook and CLI lookup
test modules.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3ec11436-4ebf-43c5-ae33-9d913ffff66d

📥 Commits

Reviewing files that changed from the base of the PR and between 5b45feb and 6a765e6.

📒 Files selected for processing (4)
  • .jules/bolt.md
  • crates/forktty-ui-gtk/src/socket_cli/hooks.rs
  • crates/forktty-ui-gtk/src/socket_cli/hooks/event.rs
  • crates/forktty-ui-gtk/src/socket_cli/hooks/install.rs

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.

1 participant