Skip to content

Commit 54c71ac

Browse files
committed
fix: always include all core fragments in diff context selection
1 parent c150638 commit 54c71ac

9 files changed

Lines changed: 568 additions & 111 deletions

src/treemapper/diffctx/select.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class _SelectionState:
2525
selected_ids: set[FragmentId] = field(default_factory=set)
2626
remaining_budget: int = 0
2727
utility_state: UtilityState = field(default_factory=UtilityState)
28-
skipped_core: list[Fragment] = field(default_factory=list)
2928

3029

3130
def _log_and_return(result: SelectionResult, core_ids: set[FragmentId]) -> SelectionResult:
@@ -47,25 +46,16 @@ def _select_core_fragments(
4746
concepts: frozenset[str],
4847
state: _SelectionState,
4948
) -> None:
50-
for frag in core_fragments:
51-
if frag.token_count <= state.remaining_budget:
52-
if not _is_subset_of_selected(frag, state.selected_ids):
53-
state.selected.append(frag)
54-
state.selected_ids.add(frag.id)
55-
state.remaining_budget -= frag.token_count
56-
apply_fragment(frag, rel.get(frag.id, 0.0), concepts, state.utility_state)
57-
else:
58-
state.skipped_core.append(frag)
59-
60-
61-
def _log_skipped_core(skipped: list[Fragment]) -> None:
62-
if skipped:
63-
skipped_tokens = sum(f.token_count for f in skipped)
64-
logging.warning(
65-
"Core fragments (%d tokens) exceed budget. %d core fragments skipped.",
66-
skipped_tokens,
67-
len(skipped),
68-
)
49+
sorted_core = sorted(core_fragments, key=lambda f: rel.get(f.id, 0.0), reverse=True)
50+
51+
for frag in sorted_core:
52+
if _is_subset_of_selected(frag, state.selected_ids):
53+
continue
54+
55+
state.selected.append(frag)
56+
state.selected_ids.add(frag.id)
57+
state.remaining_budget -= frag.token_count
58+
apply_fragment(frag, rel.get(frag.id, 0.0), concepts, state.utility_state)
6959

7060

7161
@dataclass
@@ -171,7 +161,6 @@ def lazy_greedy_select(
171161

172162
state = _SelectionState(remaining_budget=budget_tokens)
173163
_select_core_fragments(core_fragments, rel, concepts, state)
174-
_log_skipped_core(state.skipped_core)
175164

176165
if state.remaining_budget <= 0:
177166
used = budget_tokens - state.remaining_budget
@@ -313,7 +302,7 @@ def _determine_final_result(
313302
elif greedy_utility <= 0:
314303
reason = "no_utility"
315304
elif not state.selected or len(state.selected) == len(base_selected):
316-
reason = "budget_exhausted" if state.skipped_core else "no_candidates"
305+
reason = "no_candidates"
317306
elif selections_for_baseline >= _BASELINE_K and threshold > 0 and has_remaining_candidates:
318307
reason = "stopped_by_tau"
319308
else:

src/treemapper/treemapper.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ def main() -> None:
6565
print(f"Clipboard unavailable: {e}", file=sys.stderr)
6666

6767
if args.output_file:
68-
write_string_to_file(output_content, args.output_file, args.output_format)
69-
print(f"Saved to {args.output_file}", file=sys.stderr)
68+
try:
69+
write_string_to_file(output_content, args.output_file, args.output_format)
70+
print(f"Saved to {args.output_file}", file=sys.stderr)
71+
except IsADirectoryError:
72+
print(f"Error: {args.output_file} is a directory", file=sys.stderr)
73+
sys.exit(1)
74+
except OSError:
75+
sys.exit(1)
7076
elif args.force_stdout or not args.copy or not clipboard_ok:
7177
# force_stdout: -o - was explicitly passed (forces stdout even with --copy)
7278
sys.stdout.write(output_content)

0 commit comments

Comments
 (0)