Skip to content

Commit 9c2470a

Browse files
committed
feat(context): rebalance token budget for structural diffs and update SYSTEM_PROMPT
Reduce symbol budget from 30% to 20% when structural diffs are present, freeing space for the raw diff. Add SYSTEM_PROMPT guidance to prefer STRUCTURED CHANGES for signature-level details over raw diff lines.
1 parent 4a7d6e6 commit 9c2470a

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/services/context.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ impl ContextBuilder {
5151
let used = SYSTEM_PROMPT_RESERVE + change_summary.len() + file_breakdown.len();
5252
let remaining = max_context.saturating_sub(used);
5353

54-
// Symbols get 30% when signatures are present (richer content), 20% otherwise
55-
let symbol_pct = if symbols.iter().any(|s| s.signature.is_some()) {
54+
// When structural diffs are available, symbols need less budget (diffs carry the detail).
55+
// When only signatures are available, symbols still get 30%.
56+
// Base case without signatures: 20%.
57+
let has_structural_diffs = !diffs.is_empty();
58+
let symbol_pct = if has_structural_diffs {
59+
20
60+
} else if symbols.iter().any(|s| s.signature.is_some()) {
5661
30
5762
} else {
5863
20

src/services/llm/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Rules:
2929
- Never copy labels, field names, or evidence tags from the prompt into your output.
3030
- If public APIs are both added and removed, this is an API replacement (refactor), not a new feature.
3131
- When SYMBOLS CHANGED shows full signatures, reference the actual parameter/type names in your subject rather than generic descriptions.
32+
- When STRUCTURED CHANGES shows parameter/return/visibility changes, use those specifics in the subject (e.g., "add timeout parameter to connect function"). STRUCTURED CHANGES is the most reliable source for what changed in function signatures — prefer it over reading raw diff lines.
3233
- When CONNECTIONS shows that a caller and callee both changed, consider mentioning the relationship in the body.
3334
3435
Symbol markers: [+] added, [-] removed, [~] modified (signature changed).

tests/context.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,3 +1827,43 @@ fn python_comment_only_change_classified_as_docs() {
18271827
"Python comment-only change should suggest Docs"
18281828
);
18291829
}
1830+
1831+
// ─── Token budget rebalancing with structural diffs ───────────────────────────
1832+
1833+
#[test]
1834+
fn symbol_budget_reduced_when_structural_diffs_present() {
1835+
use commitbee::domain::diff::{ChangeDetail, SymbolDiff};
1836+
1837+
let changes = make_staged_changes(vec![make_file_change(
1838+
"src/lib.rs",
1839+
ChangeStatus::Modified,
1840+
&"+pub fn foo() {}\n".repeat(50),
1841+
50,
1842+
0,
1843+
)]);
1844+
let mut sym = make_symbol("foo", SymbolKind::Function, "src/lib.rs", true, true);
1845+
sym.signature = Some("pub fn foo()".into());
1846+
let diffs = vec![SymbolDiff {
1847+
name: "foo".into(),
1848+
file: "src/lib.rs".into(),
1849+
line: 1,
1850+
parent_scope: None,
1851+
changes: vec![ChangeDetail::BodyModified {
1852+
additions: 5,
1853+
deletions: 2,
1854+
}],
1855+
}];
1856+
1857+
// With diffs: symbol budget should be reduced (20%)
1858+
let ctx_with = ContextBuilder::build(&changes, &[sym.clone()], &diffs, &default_config());
1859+
// Without diffs: symbol budget at 30% (signatures present)
1860+
let ctx_without = ContextBuilder::build(&changes, &[sym], &[], &default_config());
1861+
1862+
// The diff section should be larger when structural diffs reduce symbol budget
1863+
assert!(
1864+
ctx_with.truncated_diff.len() >= ctx_without.truncated_diff.len(),
1865+
"structural diffs should free budget for raw diff: with={} without={}",
1866+
ctx_with.truncated_diff.len(),
1867+
ctx_without.truncated_diff.len()
1868+
);
1869+
}

0 commit comments

Comments
 (0)