CodeWhale icon indicating copy to clipboard operation
CodeWhale copied to clipboard

refs(#2264): Phase 4 — replace Session.messages: Vec<Message> with AppendLog

Open encyc opened this issue 6 months ago • 4 comments

Refs #2264 (Phase 4 — AppendLog backing store).

Summary

Replaces Session.messages: Vec<Message> with AppendLog, the append-only wrapper introduced in Phase 1. AppendLog now implements Deref<Target=Vec<Message>> + DerefMut + From/Into, making it a transparent drop-in — all existing .len(), .iter(), .push(), .remove(), .clone() calls work without change.

AppendLog changes

  • Add Deref/DerefMut for transparent Vec access
  • Add From<Vec<Message>> and Into<Vec<Message>> for conversions
  • Add into_inner() for explicit unwrapping
  • Remove explicit iter()/len()/is_empty()/as_slice() — Deref provides std::slice::Iter (not opaque impl Iterator)

Files

File Change
prompt_zones.rs +28/−25: Deref + From impls
session.rs +2/−2: messages: AppendLog
engine.rs +8/−8: .into() on assignments
turn_loop.rs +2/−2: .into() + .into_inner()
capacity_flow.rs +1/−1: .into()
tests.rs +2/−2: &* deref for assertions

Testing

  • 37 prompt_zones + prefix_cache tests pass
  • cargo check: clean (6 pre-existing schema_migration warnings)

Greptile Summary

This PR wires AppendLog (from Phase 1) as the backing store for Session.messages, replacing the raw Vec<Message>. The conversion is kept transparent by adding Deref<Target=Vec<Message>>, DerefMut, From/Into, and into_inner(), so all existing call sites continue to compile with minimal mechanical changes.

  • AppendLog in prompt_zones.rs gains Deref, DerefMut, and the From/Into pair; the redundant explicit methods (len, is_empty, iter, as_slice) are removed in favour of slice auto-deref.
  • Session.messages in session.rs is retyped to AppendLog; eight assignment sites in engine.rs, capacity_flow.rs, and turn_loop.rs add .into() to convert Vec<Message> results into AppendLog.
  • Two test assertions are updated to use &* to explicitly deref AppendLog to &Vec<Message> for comparison.

Confidence Score: 4/5

Safe to merge — the mechanical conversions are all correct and cargo check is clean. The only concern is a design gap in prompt_zones.rs that future contributors should be aware of.

All Vec to AppendLog assignment sites are converted correctly, type inference via From/Into is sound, and the test updates are accurate. The one open question is DerefMut: it fully exposes the inner Vec for arbitrary mutation (remove, truncate, clear, last_mut), so the append-only contract is documented but not enforced at the type level. Existing callers already depend on those mutations, so no behaviour changes with this PR, but the abstraction is now leaky and could mislead future contributors into thinking the type guards against removals or rewrites.

crates/tui/src/prompt_zones.rs — the DerefMut implementation and its interaction with the append-only doc-comment.

Important Files Changed

Filename Overview
crates/tui/src/prompt_zones.rs Adds Deref/DerefMut, From/Into, and into_inner() to AppendLog; removes the explicit iter/len/is_empty/as_slice methods. DerefMut fully exposes the inner Vec for arbitrary mutation, undermining the append-only invariant the type is named and documented for.
crates/tui/src/core/session.rs Changes Session.messages field from Vec to AppendLog and initialises it with AppendLog::new(); minimal and correct change.
crates/tui/src/core/engine.rs Updates eight assignment sites from direct Vec assignment to .into() conversions; all existing .remove(), .truncate(), .len(), and .iter() calls continue to work via DerefMut/Deref.
crates/tui/src/core/engine/turn_loop.rs Converts one assignment site to .into() and changes messages_with_turn_metadata() to use clone().into_inner(); the latter allocates an unnecessary intermediate AppendLog.
crates/tui/src/core/engine/capacity_flow.rs Single assignment converted to .into(); no other changes.
crates/tui/src/core/engine/tests.rs Updates two assertions to use &* deref to compare Vec with &*AppendLog; change is correct.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["result.messages: Vec<Message>"] -->|".into()"| B["AppendLog"]
    B -->|"Deref"| C["&Vec<Message> (read-only callers)"]
    B -->|"DerefMut"| D["&mut Vec<Message> (remove / truncate / clear / last_mut)"]
    B -->|".clone().into() or .clone().into_inner()"| E["Vec<Message> (SessionUpdated / API calls)"]
    B -->|"into_inner()"| F["Vec<Message> (consumed)"]
    G["Session.messages: AppendLog"] --- B

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

Reviews (1): Last reviewed commit: "refs(#2264): Phase 4 — replace Session.m..." | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.

encyc avatar Jun 02 '26 06: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 06: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 06:06 github-actions[bot]

Thanks @encyc — this is useful v0.9 cache-architecture work and a good concrete follow-up to #2264. I rechecked it during the current stewardship pass.

I’m leaving the PR open rather than merging or harvesting it as-is because GitHub now reports the branch as conflicting/dirty against current main, and the current approach makes AppendLog a transparent Vec<Message> via DerefMut. That keeps the mechanical migration small, but it also means the type no longer enforces the append-only invariant that made the design valuable in the first place. Future callers could still remove/truncate/clear history through the Vec API without the compiler noticing.

The direction I’d be comfortable landing for v0.9 is narrower and stricter:

  • keep Session.messages migration scoped and well-tested;
  • avoid unrestricted DerefMut for AppendLog;
  • add explicit mutation methods for the current legitimate paths (push, controlled truncation/compaction/reset), with names that make cache-impact obvious;
  • preserve the focused checks from this PR, plus current cargo check/test -p codewhale-tui --locked coverage after rebasing.

If we harvest this later, I’ll keep your contribution credited. The implementation here clarified the exact tradeoff between a drop-in migration and a real append-only contract.

Hmbown avatar Jun 06 '26 02:06 Hmbown

@Hmbown thanks for the detailed review — the tradeoff between a drop-in migration and a real append-only contract was exactly the right call. I've pushed an update that addresses all three points:

1. Removed DerefMut Deref (immutable) is kept for read access; DerefMut is gone. Callers can no longer reach Vec::remove / Vec::clear / Vec::truncate through the wrapper.

2. Explicit, cache-impact-named mutation methods

Method Cache semantics
push(msg) extends prefix ✅
push_batch(batch) fewer cache invalidations than repeated push
truncate_to(new_len) discards oldest → cache loss ⚠️
trim_front(count) removes from head → shared-prefix loss 🔴
clear() full reset 🔴
last_mut() mutable access to last message only

3. Rebased on latest main (v0.8.53) Merged upstream and fixed 8 Vec<Message> → AppendLog conversion sites introduced post-v0.8.50. All conflicts resolved.

Tests

  • cargo check -p codewhale-tui: clean
  • prompt_zones tests: 16 passed, 0 failed
  • engine::tests: 120 passed, 0 failed, 1 ignored

encyc avatar Jun 06 '26 03:06 encyc

@encyc — this Phase 4 of the #2264 AppendLog series is next in line from our side, and your earlier review-feedback fixes look good. It's currently conflicting with main (which has moved a lot this week, including v0.8.55). Could you rebase onto current main? We'd like to keep the series moving — thanks for staying with this multi-phase work.

Hmbown avatar Jun 10 '26 03:06 Hmbown

/lgtm

Hmbown avatar Jun 10 '26 03:06 Hmbown

@encyc is already approved for pr contributions in .github/APPROVED_CONTRIBUTORS.

github-actions[bot] avatar Jun 10 '26 03:06 github-actions[bot]

@Hmbown — rebased onto current main (v0.8.57). Conflicts in engine.rs, turn_loop.rs, and tests.rs were mechanical — upstream had added messages_revision tracking and a few new assignment sites since the last rebase, all resolved.

  • cargo check -p codewhale-tui: ✅ clean
  • cargo test -p codewhale-tui: ✅ 4362 passed, 0 failed

Ready for the next step whenever you are. Thanks for the /lgtm and for keeping this series moving.

encyc avatar Jun 10 '26 09:06 encyc