diff --git a/docs/system-specs/modules/history.md b/docs/system-specs/modules/history.md index bfa164ed0eab..9fa68a926d36 100644 --- a/docs/system-specs/modules/history.md +++ b/docs/system-specs/modules/history.md @@ -149,7 +149,10 @@ use: bounded-page index base on disk and carries it across first-line rewrites (see "Bounded transcript pages"), and the reads owner imports it inside the functions that use it, so the facade serves none of its names. -- `history_rewrite.py` owns locked compaction rewrites and size-based rotation. +- `history_rewrite.py` owns locked compaction rewrites and size-based rotation, + and holds the synced whole-file replace (`rewrite_transcript`) and two + metadata-line rewrite helpers (`rewrite_metadata_line`, + `rewrite_head_streaming`). - `history_listing_index.py` owns `SessionListingIndex`, the in-memory row index behind `list_sessions_indexed` and the index half of `listing_has_messages` (see "The listing index" under `ConversationLog`). `__init__` constructs it diff --git a/src/kiro_crew/history_rewrite.py b/src/kiro_crew/history_rewrite.py index ef16b6992014..5b6341ba900d 100644 --- a/src/kiro_crew/history_rewrite.py +++ b/src/kiro_crew/history_rewrite.py @@ -1,8 +1,11 @@ -"""Compaction rewrites and size-based rotation for conversation history. - -``ConversationLog`` remains the identity-bearing owner of transcript paths, -locks, cache generations, and all mutable state. This module owns the two -housekeeping rewrite workflows. Only facade bindings with established +"""Transcript rewrites for conversation history. + +``ConversationLog`` is the identity-bearing owner of transcript paths, locks, +cache generations, and all mutable state. This module holds the synced +whole-file replace (:func:`rewrite_transcript`), two metadata-line rewrite +helpers (:func:`rewrite_metadata_line`, :func:`rewrite_head_streaming`), and the +two housekeeping workflows, compaction and size-based rotation +(:class:`HistoryRewriteCoordinator`). Only facade bindings with established post-construction patch consumers are looked up at call time. """ @@ -253,9 +256,13 @@ def _file_digest(path: Path) -> bytes | None: def _whole_file_build(build: MetadataLineBuild, path: Path, text: str | None) -> bytes | None: """*build* on a file held whole in memory: the whole replacement, or ``None``.""" - lines = text.splitlines(keepends=True) if text is not None else [] - head = (lines[0] if lines else "") if text is not None else None - rest = lines[1:] + if text is None: + head = None + rest: list[str] = [] + else: + lines = text.splitlines(keepends=True) + head = lines[0] if lines else "" + rest = lines[1:] new_head = build(path, head, lambda: sum(1 for ln in rest if ln.strip())) if new_head is None: return None @@ -265,10 +272,11 @@ def _whole_file_build(build: MetadataLineBuild, path: Path, text: str | None) -> def _stage_streamed(path: Path, build: MetadataLineBuild) -> tuple[str | None, object] | None: """Stage the replacement without holding the transcript in memory, no lock held. - Returns ``(staged name or None when build wrote nothing, digest of the bytes - it was built from)``; the digest is :data:`_UNREAD` when the file changed - between the row count and the copy. None for a file only the whole-file - path reproduces exactly (see :func:`_streamable_head`, :func:`_copy_rows`). + Returns ``(staged name, digest of the bytes it was built from)``, or + ``(None, _UNREAD)`` when build wrote nothing or the file changed between the + row count and the copy; either way the caller judges again under the lock. + None for a file only the whole-file path reproduces exactly (see + :func:`_streamable_head`, :func:`_copy_rows`). """ read = _streamable_head(path) if read is None: @@ -293,7 +301,8 @@ def rows() -> int: try: new_head = build(path, head, rows) if new_head is None: - # Judged again under the lock, as the whole-file pass always did. + # Nothing is staged, so rewrite_metadata_line runs build again + # under the lock. return None, _UNREAD digest = hashlib.blake2b(digest_size=32)