Skip to content

fix(compaction): render APPROX_ATTRIBUTION kept_tail/pre ratio truthfully (>100% basis artifact) - #161

Merged
Kyzcreig merged 1 commit into
mainfrom
fix/compaction-approx-frac-display
Jul 1, 2026
Merged

Kyzcreig merged 1 commit into
mainfrom
fix/compaction-approx-frac-display

Conversation

@Kyzcreig

@Kyzcreig Kyzcreig commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

What

The in-turn A-floor observability marker printed kept_tail 75378 / pre 74447 = 101.3% in #logs (2026-07-01) — a confusing >100% that reads as "kept more than existed."

Why it's not a real bug (just a display artifact)

raw_tail_tokens is a documented upper bound estimated on the raw (pre-sanitize) suffix; pre_tokens is counted on the sanitized/in-context basis. Different bases → the raw bound can legitimately exceed pre. Compaction itself is unaffected — only the displayed fraction was misleading.

Fix (display-only)

New _fmt_gross_frac() renders the ratio truthfully:

  • raw > pre → ≥100% (raw-tail bound N ≥ pre M; raw vs sanitized basis) (bounded + basis note)
  • within bound → plain percent (unchanged)
  • pre=0 → n/a (pre=0) (no divide-by-zero)

The threshold comparison is unchanged — a bound over the 10% gross-error line still correctly triggers the two-line degrade. Only what's printed changed.

Tests

3 new unit tests incl. the real captured 75378/74447 case; 116 compaction tests green, ruff clean.

Reported via Apollo's 5-cron-alert investigation (Phase 6). Spec: ~/.hermes/plans/2026-07-01_five-cron-alerts-FIX-PRD.md.

…ully (>100% basis artifact)

The in-turn A-floor observability marker printed 'kept_tail 75378 / pre 74447 = 101.3%' — a
confusing >100% that reads as 'kept more than existed'. raw_tail_tokens is a documented UPPER BOUND
estimated on the RAW (pre-sanitize) suffix, while pre_tokens is counted on the sanitized/in-context
basis; the two are DIFFERENT bases, so the raw bound can legitimately exceed pre. Compaction itself is
unaffected — only the displayed fraction was misleading.

New _fmt_gross_frac() caps the displayed ratio at 100% and marks it as a bound with a basis note
('≥100% (raw-tail bound N ≥ pre M; raw vs sanitized basis)') when raw exceeds pre; normal within-bound
cases still render as a plain percent. The threshold COMPARISON is unchanged (a bound over the 10%
gross-error line still correctly triggers the two-line degrade) — this is display-only.

3 unit tests incl. the real captured 75378/74447 case; 116 compaction tests green, ruff clean.
@greptile-apps

greptile-apps Bot commented Jul 1, 2026

Copy link
Copy Markdown

Greptile Summary

Adds _fmt_gross_frac(), a small display-only helper that formats the raw_tail_tokens / pre_tokens ratio truthfully when the raw upper bound exceeds the sanitized pre-total. The compaction threshold comparison and all downstream logic are untouched.

  • _fmt_gross_frac handles three cases: pre=0 → "n/a (pre=0)"; frac > 1.0 → bounded "≥100%" with a basis note; otherwise plain "{:.1%}". Three new unit tests cover the exact live-captured 75378/74447 case, the within-bound path, and the zero-denominator guard.

Confidence Score: 5/5

Display-only change with no effect on compaction logic or threshold comparisons; safe to merge.

The change is surgically scoped to two log-format call sites. The threshold check that drives real behavior (_gross_frac > _APPROX_GROSS_MAX_FRAC) is untouched. The new helper correctly handles all three reachable states (over-bound, within-bound, zero denominator), and the live-captured regression case is explicitly asserted in the test suite.

No files require special attention.

Important Files Changed

Filename Overview
agent/conversation_compression.py New _fmt_gross_frac helper added; two call sites swapped from raw :.1% to the helper. Threshold comparison (_gross_frac > _APPROX_GROSS_MAX_FRAC) is untouched and continues to use the raw float.
tests/agent/test_compaction_stats_degrade_observability.py Three new unit tests covering the >100% bound case (real captured values), within-bound plain-percent path, and zero-denominator safety. All assertions are tight and specific.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["approx_attribution path\n_gross_tok, _pre_tok computed"] --> B{"_gross_frac > 10%?\n(threshold check — unchanged)"}
    B -- "yes" --> C["_warn_compaction_stats_once\n'degraded … two-line'\n_inturn_stats = None"]
    B -- "no" --> D["_warn_compaction_stats_once\n'APPROX_ATTRIBUTION in-turn …'\n_inturn_stats = _cand"]
    C --> E["_fmt_gross_frac(_gross_tok, _pre_tok)"]
    D --> F["_fmt_gross_frac(_gross_tok, _pre_tok)"]
    E --> G{"pre_tok <= 0?"}
    F --> G
    G -- "yes" --> H["return 'n/a (pre=0)'"]
    G -- "no" --> I{"gross/pre > 1.0?"}
    I -- "yes" --> J["return '≥100% (raw-tail bound N ≥ pre M; raw vs sanitized basis)'"]
    I -- "no" --> K["return '{:.1%}'"]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A["approx_attribution path\n_gross_tok, _pre_tok computed"] --> B{"_gross_frac > 10%?\n(threshold check — unchanged)"}
    B -- "yes" --> C["_warn_compaction_stats_once\n'degraded … two-line'\n_inturn_stats = None"]
    B -- "no" --> D["_warn_compaction_stats_once\n'APPROX_ATTRIBUTION in-turn …'\n_inturn_stats = _cand"]
    C --> E["_fmt_gross_frac(_gross_tok, _pre_tok)"]
    D --> F["_fmt_gross_frac(_gross_tok, _pre_tok)"]
    E --> G{"pre_tok <= 0?"}
    F --> G
    G -- "yes" --> H["return 'n/a (pre=0)'"]
    G -- "no" --> I{"gross/pre > 1.0?"}
    I -- "yes" --> J["return '≥100% (raw-tail bound N ≥ pre M; raw vs sanitized basis)'"]
    I -- "no" --> K["return '{:.1%}'"]
Loading

Reviews (1): Last reviewed commit: "fix(compaction): render APPROX_ATTRIBUTI..." | Re-trigger Greptile

@Kyzcreig
Kyzcreig merged commit 78a24c5 into main Jul 1, 2026
33 checks passed
@Kyzcreig
Kyzcreig deleted the fix/compaction-approx-frac-display branch July 1, 2026 23:01
@Kyzcreig
Kyzcreig restored the fix/compaction-approx-frac-display branch September 21, 2026 10:31
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