Skip to content

fix(work): cap total inlined comment size in build_prompt's dispatch prompt - #449

Closed
getappz wants to merge 1 commit into
masterfrom
task/85
Closed

fix(work): cap total inlined comment size in build_prompt's dispatch prompt#449
getappz wants to merge 1 commit into
masterfrom
task/85

Conversation

@getappz

@getappz getappz commented Aug 11, 2026

Copy link
Copy Markdown
Owner

Summary

Test plan

  • cargo build --bin agentflare --lib
  • cargo test --bin agentflare cli::work:: (34 passed, including new build_prompt_caps_an_oversized_comment_thread_at_the_tail_with_a_pointer)
  • cargo clippy --locked --workspace --all-targets --all-features -- -D warnings -A unsafe_code -A clippy::pedantic
  • cargo fmt --check

Closes #85.

Summary by CodeRabbit

  • Improvements
    • Limited prior discussion context to 8,000 characters.
    • Preserved recent comments when older discussion exceeds the limit.
    • Added guidance for retrieving the complete discussion history.
  • Bug Fixes
    • Prevented oversized comment threads from exceeding the available prompt context.

…prompt

build_prompt's comment loop concatenated every prior comment on an item
with no aggregate cap on total count/size, unlike latest_handoff_content's
HANDOFF_ASSET_MAX_CHARS tail-and-pointer discipline. #81 bounds each
individual posted comment, but a thread with many bounded comments could
still sum to an unbounded total -- and since #441 moved prompt delivery
from argv (E2BIG as an accidental backstop) to stdin (no OS limit),
nothing else caught that growth. Apply the same tail-cap-and-pointer
pattern to the comments section, with a regression test.
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The CLI now caps the combined “Prior discussion” prompt section at 8,000 characters. Oversized comment histories use a UTF-8-safe tail preview with metadata and MCP retrieval instructions. A test verifies truncation and latest-comment preservation.

Changes

Comment History Prompt Handling

Layer / File(s) Summary
Inline comment history limit and validation
src/cli/work.rs
The prompt builder preserves histories within the limit and truncates oversized histories with retrieval metadata. Tests verify the item identifier, comment count, retrieval guidance, and latest comment body.

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

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR caps inlined comment history, but linked issue #85 requires a ponytail playbook skill, mode registration, and review-scope configuration. Implement the requirements in issue #85 or link this PR to the issue that tracks comment-history prompt truncation.
Out of Scope Changes check ⚠️ Warning The comment-history truncation changes are unrelated to the coding objectives in linked issue #85. Relink the PR to the relevant comment-size issue, or add the required issue #85 implementation before merging.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: limiting the total inlined comment size in the dispatch prompt.
Description check ✅ Passed The description includes the summary and completed test plan, but it omits the optional Notes for reviewers section.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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 task/85

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

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

Actionable comments posted: 3

🤖 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.

Inline comments:
In `@src/cli/work.rs`:
- Around line 188-192: Update the comment-history construction around the
comments loop and total_chars calculation to measure each rendered comment’s
length before appending it, avoiding allocation of the full unbounded section.
When the history exceeds the cap, build only the required tail; otherwise
preserve the complete rendered history.
- Around line 196-203: Update the prompt metadata in the truncation branch using
the existing truncation boundary and comment collection to compute and
explicitly report the omitted comment count and omitted character count.
Preserve the existing displayed tail content and total counts, and make the
character units explicit in the message.
- Around line 1144-1164: Strengthen the regression test around build_prompt by
assigning each comment a unique body containing multibyte text, then assert the
latest comment’s marker is present. Extract the inline comment tail from the
generated prompt and assert its character count is at most
COMMENTS_INLINE_MAX_CHARS, replacing the byte-length threshold and ambiguous
identical-body assertion.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 439c9afc-96a2-4d76-9631-dea39bc134c0

📥 Commits

Reviewing files that changed from the base of the PR and between a3efa61 and 4dfd54b.

📒 Files selected for processing (1)
  • src/cli/work.rs

Comment thread src/cli/work.rs
Comment on lines +188 to +192
let mut section = String::new();
for c in comments {
prompt.push_str(&format!("- [{}] {}\n", c.author_agent, c.body));
section.push_str(&format!("- [{}] {}\n", c.author_agent, c.body));
}
let total_chars = section.chars().count();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win

Apply the cap before materializing the full history.

The code renders every comment into section before it checks total_chars. A large thread can still allocate the complete unbounded history, even though the final prompt contains only the tail. Count rendered lengths first, then build only the bounded tail when truncation is required.

🤖 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 `@src/cli/work.rs` around lines 188 - 192, Update the comment-history
construction around the comments loop and total_chars calculation to measure
each rendered comment’s length before appending it, avoiding allocation of the
full unbounded section. When the history exceeds the cap, build only the
required tail; otherwise preserve the complete rendered history.

Comment thread src/cli/work.rs
Comment on lines +196 to +203
prompt.push_str(&format!(
"(showing the last {COMMENTS_INLINE_MAX_CHARS} of {total_chars} chars across \
{} comments -- full history via `mcp__flare__comment` action=list \
item_id={})\n\n{}\n",
comments.len(),
item.id,
tail_chars(&section, COMMENTS_INLINE_MAX_CHARS)
));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Report omitted counts explicitly.

The message reports “the last 8,000 of total_chars chars across comments.len() comments.” It does not state how many comments were omitted, and it makes the character count implicit. Compute omission counts from the truncation boundary and include them in the prompt metadata.

🤖 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 `@src/cli/work.rs` around lines 196 - 203, Update the prompt metadata in the
truncation branch using the existing truncation boundary and comment collection
to compute and explicitly report the omitted comment count and omitted character
count. Preserve the existing displayed tail content and total counts, and make
the character units explicit in the message.

Comment thread src/cli/work.rs
Comment on lines +1144 to +1164
let comments: Vec<agentflare_backend::comment::ItemComment> = (0..50)
.map(|i| agentflare_backend::comment::ItemComment {
id: format!("c{i}"),
item_id: "item-1".into(),
author_agent: "alice".into(),
body: "x".repeat(500),
created_at: 0,
updated_at: 0,
})
.collect();
let prompt = build_prompt(&item, &comments, None);
assert!(
prompt.len() < comments.len() * 500,
"capped prompt must be much shorter than the unbounded concatenation"
);
assert!(prompt.contains("full history via `mcp__flare__comment` action=list"));
assert!(prompt.contains(&item.id));
assert!(prompt.contains("50 comments"));
// the tail cut lands inside the run of bounded comments, so the very
// last (most recent) comment's full body must still be intact.
assert!(prompt.contains(&format!("{}\n", "x".repeat(500))));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Make the regression test prove the truncation contract.

All comment bodies are identical, so the final contains assertion passes even if an older comment is retained. prompt.len() measures bytes and only checks a loose threshold. Give each body a unique marker, include multibyte text, assert the latest marker is present, and assert the extracted tail has at most COMMENTS_INLINE_MAX_CHARS characters.

🤖 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 `@src/cli/work.rs` around lines 1144 - 1164, Strengthen the regression test
around build_prompt by assigning each comment a unique body containing multibyte
text, then assert the latest comment’s marker is present. Extract the inline
comment tail from the generated prompt and assert its character count is at most
COMMENTS_INLINE_MAX_CHARS, replacing the byte-length threshold and ambiguous
identical-body assertion.

@getappz

getappz commented Aug 12, 2026

Copy link
Copy Markdown
Owner Author

Duplicate — the same fix already landed via #450 (independently, same problem/pattern: COMMENTS_PROMPT_MAX_CHARS vs. this PR's COMMENTS_INLINE_MAX_CHARS, both tail-cap-and-pointer on the comment thread). Confirmed by diffing against current master: git log origin/master -S "COMMENTS_PROMPT_MAX_CHARS" shows #450 already shipped it. Closing to avoid re-litigating already-shipped work — no action needed on item #85 itself.

@getappz getappz closed this Aug 12, 2026
@getappz
getappz deleted the task/85 branch August 15, 2026 08:07
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