Skip to content

refactor(history): unchain the whole-file build and correct its docs - #45362

Merged
bolichen97 merged 1 commit into
mainfrom
refactor/simplify-toplevel-1201
Dec 1, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
refactor/simplify-toplevel-1201

Conversation

@bolichen97

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Make history_rewrite.py's whole-file metadata build test for an absent transcript once instead of twice, and make the module's docstrings and the history spec's module map describe what the module does today, with no change to any transcript write.

Part of the module-by-module backend simplification sweep. The detector's one new actionable site this run is the chained ternary in history_rewrite._whole_file_build. Every other site in the queue is one the sweep leaves alone: a keystone file, a test-pinned ternary, or a site an earlier run already declined. So this PR takes that site plus the stale prose in the same file:

  • _whole_file_build tests text is not None twice: once to build lines, and again in a chained ternary for head ((lines[0] if lines else "") if text is not None else None).
  • _stage_streamed's docstring says it returns "(staged name or None when build wrote nothing, digest of the bytes it was built from)". When build writes nothing, the second item is _UNREAD, not a digest. The same (None, _UNREAD) pair comes back when the rows change between the count and the copy.
  • The inline comment on that return says "as the whole-file pass always did", which narrates earlier code.
  • The module docstring and the history spec's module map say the module owns compaction and rotation. Most of the module is now rewrite_transcript and the metadata-line rewrites (rewrite_metadata_line, rewrite_head_streaming), which history_marker.py and history_projection.py import from it.

Nothing is broken at runtime. This is a readability cleanup.

Why it matters

rewrite_metadata_line decides whether to publish an unlocked staged copy or rebuild under the lock from _stage_streamed's result. A docstring that promises a digest where the code returns the never-matching _UNREAD misleads a reader working on that decision. A module summary that lists only compaction and rotation sends a reader looking for the metadata-line rewrites to the wrong place.

Not a goal

  • The keystone and test-pinned ternaries the detector excludes in other top-level files (diagnostics.py, safety_override_grant.py, sandbox_plan.py, tool_permission.py).
  • rewrite_transcript's docstring sentence that every history-store rename over a transcript goes through it. The metadata-line paths rename through publish_replacement and atomic_write directly. The history spec's "Durable replace" paragraph makes the matching claim, and correcting the two together is a spec change of its own.
  • The lines[:] copy in _maybe_rotate, and any other code in this file.
  • Any other module.

What changed (motivation → approach → change)

No transcript byte, lock, sync, digest compare or archive step changes.

Where Before After Why it is the same
_whole_file_build lines = ... if text is not None else [], then head = (lines[0] if lines else "") if text is not None else None, then rest = lines[1:] if text is None: head = None; rest = [], else: split once, head = lines[0] if lines else "", rest = lines[1:] None gives None and [] both ways; a string gives the same lines[0] (or "") and lines[1:]. The test is still is None, so "" takes the string branch as before. Nothing after the block reads lines.
_stage_streamed docstring (staged name or None when build wrote nothing, digest ...) (staged name, digest ...), or (None, _UNREAD) when build wrote nothing or the rows changed between the count and the copy; either way the caller judges again under the lock Docstring only. It now matches the four return statements.
the comment on return None, _UNREAD "as the whole-file pass always did" "Nothing is staged, so rewrite_metadata_line runs build again under the lock." Comment only. With staged left None, rewrite_metadata_line's locked branch calls _whole_file_build again.
module docstring; docs/system-specs/modules/history.md module map compaction and size-based rotation only also names rewrite_transcript and the two metadata-line rewrite helpers, rewrite_metadata_line and rewrite_head_streaming Docstring and spec text only. All three are defined in this module. The wording says the module holds them, not that every transcript replace goes through it: history_projection's unsynced metadata write and home_path_rewrite replace a transcript themselves.

rest carries a list[str] annotation because its first assignment is an empty list. head's first assignment is None, so mypy reads it as str | None from the later str assignment with no annotation.

Backwards compatibility

Compatible: _whole_file_build returns the same bytes for every input, and every other hunk is a docstring, a comment or spec text.
Removes nothing: no name, signature, import or behaviour is removed; the module-level names are identical before and after.

Tests

No new tests: the change preserves behaviour. I did not run tests locally, because this host's standing rule leaves the suites to CI. Existing tests reach both branches of _whole_file_build:

  • text is None: test_update_metadata_still_upserts_a_missing_transcript in test/test_history_locking_remediation_locked_section.py (update_metadata on a transcript that does not exist).
  • a string: test_a_row_with_a_carriage_return_takes_the_whole_file_path (a row the chunked copy refuses) and test_the_fallback_frees_the_stale_staged_copy_before_writing (the rebuild under the lock), same file.

No test reads this module's source or docstrings, or the edited spec line.

The diff touches nothing under website/, .github/ or scripts/, so the frontend suites do not run.

Static gates run locally on this head (flake8, isort, and the black, subprocess-encoding, comment-history, brand, harness-parity, line-cap, docs-lint, per-file-coverage self-test and vendor-manifest checks) all exit 0. mypy is left to CI under the same host rule.

Before opening, five read-only reviewers (AUTOSDE and CI contracts, behaviour, imports, security and boot path, comment accuracy) looked at the diff. The first four found nothing: both branches of _whole_file_build bind the same values, no import or module-level name changed, and no lock, digest compare, sync or archive step changed. The comment reviewer found two Low issues, and an independent refuter confirmed each. First, the first draft of the module docstring and the spec line said the module owns "the rewrites that replace a transcript file", which other modules also do. Second, the commit message called all three edits docstrings and called the incomplete module docstring false. Both are fixed here, and a verifier checked them closed. Nothing was dropped as a false positive.

Manual verification

N/A — unit coverage sufficient: the one code change rebinds two locals to the same values, and the rest is docstrings, a comment and one spec line.

Real-machine verification

N/A -- the diff rebinds two locals to identical values and edits docstrings, a comment and spec text; nothing behaves differently on any host.

Screenshots / video

Why no screenshot: backend-only readability change in the transcript rewrite module; no frontend or rendered surface changes.

Related Issues

Part of #16523

Checklist

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • Existing tests pass and new tests added for new functionality (CI runs them; not run locally)
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Accepted under the project's Apache-2.0 license.

@bolichen97
bolichen97 requested review from a team and KiroCrewCaptain as code owners December 1, 2026 07:45
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision d449176a79ee373e99ce72b80149383862653183 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of d449176a79ee373e99ce72b80149383862653183 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks. The one code edit binds the same two locals, and the docstring, comment and spec edits match the code on this head.

What this change ships

Inventory (4 items) — 4 justified

Intent: make the code that rewrites transcript metadata easier to read correctly, with no change in behaviour. This is a FIX to readability and docs.

  1. The whole-file metadata build now checks for a missing transcript once instead of twice. Every input gets the same output as before. — justified
  2. _stage_streamed's docstring now gives the real return, (None, _UNREAD), for both the case where build wrote nothing and the case where the rows changed. Before, it promised a digest there. — justified
  3. The inline comment on that return now says what happens next instead of describing earlier code. — justified
  4. The module docstring and the history spec's module map now list rewrite_transcript and the two metadata-line rewrite helpers beside compaction and rotation. — justified

[FIRST-PRINCIPLES-REVIEWED] d449176

Description read

[DESCRIPTION-READ] d4d33aabd2cad2d8331bda8d34b8993e91e24804ce8fd9ce9909478ac32269c8

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of d449176a79ee373e99ce72b80149383862653183 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the code change binds the same two values on both branches, and everything else is docstrings, a comment and one line of spec text.

[DESIGN-REVIEWED] d449176

Description read

[DESCRIPTION-READ] 05a3dcbb7f8a5e64ac9ae76a8ecd7875a602bc68970956421551cc04fb94f62c

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed d449176a79ee373e99ce72b80149383862653183 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] d449176

Verdict parsed from the review's SHA-scoped output markers for commit d449176a79ee373e99ce72b80149383862653183.

False positive or not applicable? A repository writer can comment:
/ai-review override fable d449176a79ee373e99ce72b80149383862653183 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of d449176a79ee373e99ce72b80149383862653183 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] d449176

False positive or not applicable? A repository writer can comment:
/ai-review override gpt d449176a79ee373e99ce72b80149383862653183 false-positive: <reason, 20 to 500 characters>

@bolichen97

Copy link
Copy Markdown
Contributor Author

The red Backend Tests (Windows) (6) on d48d6fe4bcf is not from this diff. It is test_api_agents_create_template.py::TestFirstGreetingOwed::test_a_crewmate_named_after_a_windows_device_owes_its_welcome_elsewhere (409 == 200), red on its retry too. The same node fails on two unrelated PRs that share this base, c50397cfbdc (#41716). That change writes mates/agents/Nul.json before it publishes the binding, and on Windows that name is the NUL device. This PR touches only history_rewrite.py and one spec line, and neither is on that path.

Tracked in #45371, which needs a product decision. No rerun: the failure is deterministic. This PR waits for that fix and rebases onto it.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Dec 1, 2026
_whole_file_build tested `text is not None` twice, once for `lines`
and again around a chained ternary for `head`. One if/else now binds
`head` and `rest` together; the values are unchanged on both paths
(None and [] for an absent file, line 0 or "" and the rows after it
otherwise).

Two docstrings and a comment in the same file did not match the code:

- _stage_streamed: the documented return paired "None when build
  wrote nothing" with a real digest. When build writes nothing, and
  when the rows change between the count and the copy, it returns
  (None, _UNREAD), and the caller judges again under the lock.
- the inline comment on that return said "as the whole-file pass
  always did"; it now says what the caller does with the result.
- the module docstring named only compaction and rotation, leaving out
  rewrite_transcript and the two metadata-line rewrite helpers. The
  history spec's module map carried the same short list and is updated
  with it.
@bolichen97
bolichen97 force-pushed the refactor/simplify-toplevel-1201 branch from d48d6fe to d449176 Compare December 1, 2026 12:31
@bolichen97

Copy link
Copy Markdown
Contributor Author

Rebased onto main at d93608c, which carries #45390 (the fix for #45371, the Backend Tests (Windows) (6) red on the previous head). git range-diff reports the commit unchanged (=); main's two history.md edits since the old base touch other sections. Static gates re-run green on the new head d449176a79e.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Dec 1, 2026

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed head d449176. The one code change rewrites the chained ternary in the whole-file metadata build as an if/else. It binds the same head and remaining rows for an absent file, an empty file and a normal transcript, so history output, row order and handling of damaged lines do not change. The other edits are docstrings, one comment and one line of the spec. I checked them against the module's actual return paths and definitions, and they are accurate. CI on this head is green across the Linux and Windows backend shards, coverage, lint and type checks, so nothing blocks.

@bolichen97
bolichen97 merged commit c2ab205 into main Dec 1, 2026
103 checks passed
@bolichen97
bolichen97 deleted the refactor/simplify-toplevel-1201 branch December 1, 2026 13:17
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Dec 1, 2026
@bolichen97

Copy link
Copy Markdown
Contributor Author

Green-age re-check, no push. green_age.py reported STALE: main gained 15 commits since d93608c, and each one touches only files in the same directories as this PR, never the same files. None of the 284 files main changed since that base imports or names history_rewrite. A local rebase onto 892d4a7 applies cleanly, git range-diff reports the commit unchanged (=), and the static gates pass on the rebased tree. The head stays d449176a79e.

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.

2 participants