CodeWhale icon indicating copy to clipboard operation
CodeWhale copied to clipboard

refs(#2264): Phase 3 — emit PrefixCacheChange events from FrozenPrefix layer

Open encyc opened this issue 6 months ago • 2 comments

Refs #2264 (Phase 3 — user-visible events).

Summary

Upgrades the three-zone diagnostic layer from silent tracing::debug! to user-visible Event::PrefixCacheChange events. The TUI footer now shows three-zone prefix status alongside the existing PrefixStabilityManager data.

Turn Event
First "frozen: a1b2c3d4e5f6" (stability 100%, not a change)
Drift "prefix drift: system prompt changed (frozen=..., current=...)"
Stable no event (existing PrefixStabilityManager handles this)

Files

File Change
crates/tui/src/core/engine/turn_loop.rs +25 / −2

Testing

  • cargo check: clean
  • 37 prompt_zones + prefix_cache tests pass

Greptile Summary

Phase 3 upgrades the three-zone diagnostic layer to emit a one-shot PrefixCacheChange event when the frozen baseline is first established, and correctly silences the duplicate drift event (relying on PrefixStabilityManager for drift reporting from that point on).

  • The frozen-init event (turn 1, None branch) computes pinned_combined_hash via FrozenPrefix::combined_hash, which hashes tools using full serde_json serialization. All subsequent PM events use PrefixFingerprint::combined_sha256, which hashes only API-visible fields via tool_to_api_json. These two algorithms produce different digests for the same tool set, so last_pinned_prefix_hash silently flips to a new value on turn 2, making /cache stats output inconsistent.
  • The drift path no longer emits a redundant event, which removes the per-drift double-counting of prefix_change_count that would have occurred in Phase 2.

Confidence Score: 3/5

The frozen-init event introduces a hash computed with a different tool-serialization algorithm than all subsequent PM events, causing last_pinned_prefix_hash to show an inconsistent value between turn 1 and turn 2 even when nothing has changed.

The pinned_combined_hash field in the frozen-init event is computed by FrozenPrefix::combined_hash (full serde_json serialization of every tool field), while every subsequent PrefixCacheChange event from PrefixStabilityManager uses PrefixFingerprint::combined_sha256 (API-only fields via tool_to_api_json). These two digests diverge for the same tool set, so /cache stats shows a hash on turn 1 that never matches what PM reports from turn 2 onward.

crates/tui/src/core/engine/turn_loop.rs — the frozen-init event block (None branch, ~line 352) needs pinned_combined_hash computed via PrefixFingerprint::compute (the same path PM uses) rather than FrozenPrefix::hash().

Important Files Changed

Filename Overview
crates/tui/src/core/engine/turn_loop.rs Phase 3 adds a one-shot frozen-init event on the first turn; drift now stays silent (just tracing::debug!). The init event's pinned_combined_hash is computed with a different tool-serialization algorithm than all subsequent PM events, causing last_pinned_prefix_hash to show an inconsistent hash between turn 1 and turn 2.

Sequence Diagram

sequenceDiagram
    participant TL as TurnLoop
    participant PM as PrefixStabilityManager
    participant TZ as ThreeZoneLayer
    participant TX as tx_event
    participant UI as TUI (ui.rs)

    Note over TL: Turn 1 (frozen_prefix = None)
    TL->>PM: check_and_update(system, tools)
    PM-->>TX: "PrefixCacheChange { changed:false, stability_pct:X%, hash:PM_hash }"
    TX-->>UI: "prefix_checks_total++, prefix_stability_pct=X%"
    TL->>TZ: frozen_prefix is None, freeze()
    TZ-->>TX: "PrefixCacheChange { changed:false, stability_pct:100, hash:FrozenPrefix_hash }"
    TX-->>UI: "prefix_checks_total++ now 2, prefix_stability_pct=100 overwritten"
    Note right of UI: PM_hash != FrozenPrefix_hash different tool serialization

    Note over TL: Turn 2+ stable
    TL->>PM: check_and_update Ok
    PM-->>TX: "PrefixCacheChange { changed:false, stability_pct:Y%, hash:PM_hash }"
    TX-->>UI: "prefix_checks_total++, prefix_stability_pct=Y%"
    Note over TZ: No event emitted

    Note over TL: Turn N drift
    TL->>PM: check_and_update Err
    PM-->>TX: "PrefixCacheChange { changed:true, stability_pct:Z%, hash:PM_hash }"
    TX-->>UI: prefix_checks_total++, prefix_change_count++
    TL->>TZ: frozen.verify() Err drift
    TZ->>TZ: tracing debug only, re-freeze silently
    Note over TZ: No duplicate event Phase 3 fix

Fix All in Codex Fix All in Claude Code Fix All in Cursor

Reviews (2): Last reviewed commit: "fix: don't double-count drift — only emi..." | Re-trigger Greptile

encyc avatar Jun 02 '26 05:06 encyc

[!WARNING] You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

gemini-code-assist avatar Jun 02 '26 05:06 gemini-code-assist

Thanks @encyc for taking the time to contribute.

This repository is currently observing a maintainer-managed contribution gate in dry-run mode, so this pull request is staying open. When enforcement is enabled, pull requests from contributors who are not listed in .github/APPROVED_CONTRIBUTORS will be closed automatically.

Please read CONTRIBUTING.md for the expected contribution shape. A maintainer can grant PR access by commenting /lgtm on a pull request.

github-actions[bot] avatar Jun 02 '26 05:06 github-actions[bot]

Thanks @encyc — the PrefixCacheChange first-freeze event behavior is already present on the public v0.9.0 integration branch (codex/v0.9.0-stewardship) through 29acb87a9d.

Verification recorded in the execution map: cargo test -p codewhale-tui --locked prefix_cache passed.

Closing this PR as already harvested/present in the integration branch.

Hmbown avatar Jun 05 '26 02:06 Hmbown