Skip to content

fix(dashboard): bound the large-file window diff and never invent a failed write's diff - #20441

Merged
iamwhatever merged 1 commit into
mainfrom
fix/fast-excerpt-failed-write
Oct 16, 2026
Merged

iamwhatever merged 1 commit into
mainfrom
fix/fast-excerpt-failed-write

Conversation

@iamwhatever

@iamwhatever iamwhatever commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Storing a small change in a large file finishes quickly, a failed write never shows a change, and a one-file turn never loses its only diff to the 400K turn notice.

Three faults remain on main after #19977 in the dashboard file-change card:

  • A one-line edit in a ~1.3 MB file of repeated rows holds the turn for about a second; many spread edits hold it for minutes (220 s measured), and the result is thrown away.
  • A strReplace that was refused (for example, re-issued after an earlier turn already applied it) still shows a -old/+new hunk.
  • A one-file turn can show the "over 400,000 characters" notice in place of its only file, when that file's stored cuts look equal (cancel/error exits, files over 2 MB, windows that do not fit).

Why it matters

The window diff runs before the turn saves, so a slow diff delays every turn that edits a log, CSV or fixture file. A card that shows an edit that never ran, or hides the one edit that did, misleads the person reviewing the agent's work.

Not a goal

  • Settling the already-written oldStr-inside-newStr strReplace (still keeps the top cut; a pre-write snapshot would lift it).
  • A general error taxonomy for file-change rows: one reason, unverified_write, is added.
  • Moving the flushes or the cancel path (perf(dashboard): move the turn-end file-change snapshot off the event loop #19781 covers that), or the second redaction pass.
  • Skipping the whole after read for writes whose sides cannot fit.

What changed (motivation → approach → change)

Fault Root cause Change
Slow diff SequenceMatcher ran over both whole sides; on repeated rows its match walk is lines times repeats Shared top and bottom lines are set aside first; each match call is charged its walk up front and the diff gives up past _EXCERPT_DIFF_BUDGET (about half a second); sides whose lengths differ by more than _MAX_SNAPSHOT are not diffed
Invented change A write that did not run kept its fragment or reverse-substitution before; the backend's failed can also follow bytes that reached disk Content decides, never the status word. Every entry a failed or refused call recorded alone is unverified_write from its result on, set before any await. With a whole pre-call snapshot (create's content block) the file is re-read off the loop: equal, no entry; different, the mark is cleared and the real diff shows. Otherwise, or when that read is cancelled or fails, the write keeps only the path as omitted_reason: "unverified_write" (neither word proves the file was left alone): no hunk, no "no changes", no WakaTime lines
Lost one-file diff The turn budget protected only an entry whose cut sides differ A truncated entry counts as one that may differ, so it can be the protected entry; whole sides compared equal store two empty sides, and path-only entries never take the slot

The card states the new reason once, through the shared error surface (ErrorNotice, with the agent hand-off): "Diff unavailable: the write failed, and the file before it is unknown." It sits beside the turn-budget sentence when both apply, and the failed row's own File only tag takes the same red and warning icon, so the notice points at its file. The key diff_unavailable_unverified_write is in every catalog under website/src/i18n/locales/ (the en-XA pseudolocale regenerated).

Measured, one _excerpt_pair call:

shape main this PR
one-line edit, 1.3 MB repeated rows 933 ms 9 ms
3-line insert, 1.3 MB repeated rows 1008 ms 8 ms
1 line in 50 changed, 1.3 MB repeated rows 220 s (None) 13 ms (None)
two far edits, 1.3 MB repeated rows 876 ms (windows) 12 ms (top cut, flagged)
two far edits, 0.7 MB distinct lines — 31 ms (windows)

The spec (learn-cron-dashboard.md) states the bound, the failed-write rule and the protection rule.

Backwards compatibility

Compatible: omitted_reason: "unverified_write" is a new value on the existing path-only shape (content_omitted); an older dashboard reads it as a path-only row with the turn-budget sentence, and old transcripts carry no such value.

Reader: src/kiro_crew/dashboard/chat_turn/file_changes.py:_flush_file_changes -- chat -- a call's entry is gone before the flush only when content proves the call changed nothing (test_a_failed_create_is_judged_by_its_content_block, test_a_refused_reissue_shows_no_invented_hunk)
Reader: src/kiro_crew/dashboard/chat_turn/file_changes.py:_turn_after_reads -- chat -- reads the same list at turn end; a dropped entry is never read (test_a_retry_after_a_proven_no_change_failure_keeps_its_own_before)
Reader: src/kiro_crew/dashboard/chat_turn/file_changes.py:_turn_line_changes (WakaTime) -- chat -- counts no lines for a dropped or unverified entry (test_a_failed_write_counts_no_lines)
Reader: src/kiro_crew/dashboard/slot_persistence/transcript_merge.py:_without_file_change_copies -- chat -- rewrites only entries that still hold a copy, so an unverified entry keeps its reason
Reader: website/src/components/FileChangeChips.tsx:noticeEntries -- chat -- one sentence per reason (FileChangeChips.unverifiedWrite.test.tsx, FileChangeChips.olderTurn.test.tsx)

Tests

  • TestExcerptDiffWork: counts find_longest_match line visits (no wall clock). A small edit, insert, delete or append in 1.3 MB of repeated rows visits under 10,000 lines (main: over 50 M, stopped); 1-in-50 spread edits stop at the budget; two far edits in distinct lines still window; a length gap past the cap skips the diff; an over-budget edit keeps the top cut and stays protected.
  • TestOneFileTurnKeepsItsEntry: equal top cuts on a truncated entry are protected; an inline (cancel/error) flush and a file past the whole-read bound keep their entry with no turn notice.
  • TestFailedWriteRecordsNoChange: failed create, no bytes changed: no entry; failed create, torn bytes: the actual before/after; failed strReplace with a fragment or reverse-substitution before, a torn read, or a later disk change: path only as unverified_write, no hunk; refused re-issue: path only, no hunk; an unverified write never takes the protected slot and counts no lines; a completed edit keeps its diff; a refusal on a path another call wrote, or under a collapsed call id, drops nothing; a snapshot a failed or refused call records after its result (the refinement that can follow it in one frame, whether or not the call's first event carried rawInput) still keeps only the path, while a different call's later write shows its own diff; a call id that collapsed onto another call's leaves that call's diff alone; a cancel at each await between a failed result and the flush (the repeat-loop steer, the settlement read, the MCP app render, the PostToolUse hook, the compaction notice, the next stream event, the turn-end reads) stores only path-only unverified_write entries, never a hunk.
  • FileChangeChips.unverifiedWrite.test.tsx: the dedicated sentence on the card, the row tag and the pill row; beside the turn-budget sentence as two reasons; never "too large" or "No changes".
  • Updated to the new contract: the refused/failed turn-loop tests, the unchanged large file (a no-change entry instead of a demoted one), the spec phrase test. A new test keeps protection on a large real change when a proven-unchanged large rewrite is newer.
  • Determinism: 32 backend ids, 20 runs at -n0, 10 at -n 4 --dist loadgroup beside two sibling files, one shuffled order (seed 176); the post-result refinement test (4 cases) 20 runs at -n0, 10 at -n 4 beside two sibling files, and the whole file shuffled (seed 8823); the collapsed-id test and the 7-point cancel test (8 cases) the same, and the whole file shuffled (seed 4242); frontend files 10 shuffled runs; all green.

Manual verification

The card rendered from the built SPA with an unverified_write row beside a normal diff row (screenshot below).

Screenshots / video

A file-change card: a red error notice with a warning icon reads "Diff unavailable: the write failed, and the file before it is unknown." beside "Ask the agent"; loader.py is tagged File only, history.py keeps its -1 +2 diff

Two notices, each said once: a failed write beside a turn-budget row; the open hint ends only the last.

A card with three files: the failed-write sentence as a red error notice with Ask the agent, then the plain muted turn-budget sentence ending in "Click a file name to open it."; loader.py's File only tag in red with a warning icon, large.csv's in plain grey, history.py keeping its -1 +2 diff

The minimal pill style: the filename, the File only marker and the sentence once under the pills.

Minimal pill row: loader.py with a red warning-icon File only marker, history.py -1 +2, then the red error line "Diff unavailable: the write failed, and the file before it is unknown." with Ask the agent, and "Click a file name to open it." on its own line

Committed rather than attached: the local gh (2.96) has no --attach.

Related Issues

Closes #20419

Pattern harvest

Rule candidate: review-prompt
Pattern: an unbounded difflib.SequenceMatcher over whole files on a request path; trim shared ends and bound the work.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@iamwhatever
iamwhatever requested review from a team and KiroCrewCaptain as code owners October 14, 2026 19:17
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 14, 2026
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — 🟡 CONCERNS

UX-level review of af4aa79353978506d9f9c90c3004fb512dc6e78c — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

UX-Verdict: CONCERNS

The same "File only" tag now means both "this write failed" and "this file was too big", so only the red colour and the icon tell them apart.

Watch

  • One label, two meanings. DemotedTag and MinimalChip show the same file_only string on the failed loader.py row and on the size-limited large.csv row. The blind reader said: "Same words, two meanings. Only the colour tells them apart." Every turn with a failed write will show this, and a reader can mistake an error for a size limit. Moderate: it doesn't happen often, but it misleads the person reviewing the agent's work. Fix: give the failed row its own tag text, such as "Write failed".
  • The error doesn't name its file. The notice reads "Diff unavailable: the write failed, and the file before it is unknown." The reader was "guessing it's about loader.py only because both are red." When a card has several rows, the reader has to match colours to find the file. Fix: put the basename in the sentence ("…the write to {{file}} failed…").
  • The open-file hint sits under the wrong line. In shot-02 and shot-03, "Click a file name to open it." comes right after the error. The reader "can't tell whether opening a file is the fix for the error or for the size problem." Low severity, but it can send people the wrong way. Fix: leave the hint out under the unverified-write notice, or word it so it's clearly a general tip.

Suggestions

  • Change diff_unavailable_unverified_write to "Changes unknown: the edit to {{file}} failed, and its earlier contents weren't saved." This drops "diff" and "write", which the reader flagged as unknown words.

[UX-REVIEWED] af4aa79

Description read

[DESCRIPTION-READ] 2a722202be54e4c1d0fff784374e7a8d72daada41a71bc31ba9b73e340f5a146

That sha256 names everything this verdict read: the PR description, plus the 6 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 Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of af4aa79353978506d9f9c90c3004fb512dc6e78c — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that a successful write after a failed one should replace the failed call's entry: the card then shows only the later diff and hides any torn bytes from the failed write.

[DESIGN-REVIEWED] af4aa79

Description read

[DESCRIPTION-READ] edeadb6bdf254060eb8972185b67d13582fbec3c0ee224c490258085ea04d919

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 Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — 🟡 CONCERNS

Premise-level review of af4aa79353978506d9f9c90c3004fb512dc6e78c — 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/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

Re-reading the file on a failed create is extra machinery the Goal doesn't need. Marking the write path-only already means a failed write never shows a change.

Not justified as shipped

  • Item 4 — oversized: the Goal is "a failed write never shows a change". Item 3 already does that on its own by marking every failed/refused entry unverified_write before any await. On top of it, the PR adds pre_call_digest, _disk_digest, _settle_failed_write, _forget_entries and a new await asyncio.to_thread(_settle_failed_write, …) on the tool-result path, plus a seven-point cancel test. All of that only helps create, the one command whose content block supplies a digest. What it gets back: a failed create that changed nothing disappears from the card instead of showing "File only, write failed", and a torn create shows its bytes instead of the path alone.
    Subtraction: delete pre_call_digest, _disk_digest, _settle_failed_write, _forget_entries and the _digest_backed block in chat_runner.py, so every failed or refused write stays path-only.
    Clears when: the mechanism is removed, or the PR names a reported case where a failed create showing path-only misled someone.
  • Item 9 — undeclared: import difflib # noqa: F401 in chat_runner.py has 0 consumers. Grepping chat_runner.difflib|cr.difflib finds nothing, and the tests import difflib themselves.
    Subtraction: delete that import.
    Clears when: the line is deleted.

What this change ships

Inventory (9 items) — 7 justified

Intent: the file-change card should finish fast on large files, never show an edit that didn't happen, and never lose a one-file turn's diff. FIX.

  1. A small edit in a large repeated-row file no longer stalls the turn (shared top/bottom lines set aside, diff work capped) — justified
  2. Sides whose lengths differ by more than one snapshot are not diffed — justified
  3. A failed or refused write shows the path only, with a new "write failed" reason — justified
  4. A failed create is re-read: unchanged means the entry is dropped, changed means the real diff shows — oversized
  5. A one-file turn whose file was cut short keeps its entry instead of showing the 400K notice — justified
  6. Files whose whole contents compare equal are stored as a "no changes" entry — justified
  7. The failed-write notice goes through ErrorNotice with the agent hand-off, and the row tag turns red with a warning icon — justified
  8. The card can show two reason sentences at once — justified
  9. Unused difflib re-export in chat_runner.py — undeclared, zero consumers

[FIRST-PRINCIPLES-REVIEWED] af4aa79

Description read

[DESCRIPTION-READ] 73f73d194efeb7ba735bd22cfc024ab65843f8944c3ab2bca45e6af092e3c4b2

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 Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] af4aa79

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

False positive or not applicable? A repository writer can comment:
/ai-review override fable af4aa79353978506d9f9c90c3004fb512dc6e78c: <one-sentence reason>

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] af4aa79

False positive or not applicable? A repository writer can comment:
/ai-review override gpt af4aa79353978506d9f9c90c3004fb512dc6e78c: <one-sentence reason>

@iamwhatever
iamwhatever force-pushed the fix/fast-excerpt-failed-write branch from 6ee2fec to 00e225b Compare October 14, 2026 20:03
@iamwhatever

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: dropping a failed write's file-change entry

  • span=5624ed395977 A failed status deletes the snapshot outright — fixed in 00e225b

refused (decided before the tool runs) still drops the entry that call alone recorded.
failed drops an entry only when it carries pre_call_digest, a read proven to precede the write (a strReplace read classified pre-write, or create's content block), and a fresh whole read off the loop still matches it (_unchanged_since_snapshot).
Any other failed entry is kept, so a card may show a change that did not land but never hides one that did.
Pinned by test_a_failure_after_a_partial_write_keeps_the_diff and test_a_failed_create_is_judged_by_its_content_block[True]; removing the check reds 5 tests.
Known limit, named in spec and body: a failed strReplace whose disk already held the edit has no pre-call read and keeps its hunk.

@iamwhatever

iamwhatever commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: a second classifier name that also returned the disk content it read

  • span=59393ed8f57e two names for one classifier — fixed by removal in 7085ff9

_classify_str_replace_seen is gone; _classify_str_replace_before is again the one classifier, with its base signature.
The read it existed to return no longer counts as pre-call proof (see the GPT disposition for span 2bc3ebc231fc), so nothing needs it.

@iamwhatever
iamwhatever force-pushed the fix/fast-excerpt-failed-write branch from 00e225b to 7085ff9 Compare October 14, 2026 20:47
@iamwhatever

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: pre_call_digest from a strReplace disk read classified pre-write

  • span=2bc3ebc231fc Partial-write content is incorrectly treated as a pre-call snapshot — fixed in 7085ff9

A strReplace disk read no longer sets pre_call_digest: on the auto-approve path the read can follow part of the write, so a torn prefix can look pre-write.
The only source left is create's content block, which the backend builds before it writes; every other failed entry is kept.
Pinned by test_a_torn_write_read_as_pre_write_is_kept (your exact shape: old\n to old\nlong added text\n leaving head\nold\nlong), red on 00e225b and green on 7085ff9.

@iamwhatever

iamwhatever commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: truncated entries count as may-differ for the protected slot

  • span=605e4291342d a proven-unchanged large rewrite takes the protected slot — fixed in 7085ff9

_turn_after_reads stores two empty sides with no flag when the whole before and after compare equal, so that entry is a no-change entry and is never protected.
Pinned by test_proven_unchanged_large_file_leaves_protection_to_the_real_change (a too-large-to-window real change stays whole beside a newer unchanged rewrite), red on 00e225b and green on 7085ff9.

@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 Oct 14, 2026
@iamwhatever iamwhatever changed the title fix(dashboard): bound the large-file window diff and record no change for a failed write fix(dashboard): bound the large-file window diff and never invent a failed write's diff Oct 14, 2026
@iamwhatever
iamwhatever force-pushed the fix/fast-excerpt-failed-write branch from 7085ff9 to db1d12e Compare October 14, 2026 21:33
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 14, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in docs/system-specs/modules/learn-cron-dashboard.md since this head went green (37449b3 -> a37cfae); CI re-running.

@iamwhatever
iamwhatever force-pushed the fix/fast-excerpt-failed-write branch 3 times, most recently from fc3e85f to b806238 Compare October 14, 2026 23:44
@iamwhatever

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: unverified_write notice rendered as a plain muted div

  • span=0483c38db065 Failed-write errors bypass the required error surface — fixed in b806238

The unverified_write sentence now renders through ErrorNotice (inline variant, askAgent on: the card sits in a persisted transcript and holds no draft, the same shape ApprovalCard uses in the transcript) in both the card and the minimal pill row; the turn-budget sentence keeps its plain notice because it is not a failure.
Pinned by FileChangeChips.unverifiedWrite.test.tsx (role=alert, Ask the agent present; budget notice has no alert); screenshots of both styles in temp-screenshots/20419/.

@iamwhatever

iamwhatever commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: per-row tag for an unverified_write row

  • span=6785575ffcce The notice doesn't name the failed file — rebutted (declined for this PR, out of goal)

The unverified_write notice already tells the truth: the diff is unavailable because the write failed and the earlier file is unknown, and no hunk or "no changes" is claimed. A separate per-row "Write failed" tag is a new UI feature beyond this fix's goal, so it is declined here.
The failed row's existing File only tag does take the error colour and icon of the notice (no new string), which lets a reader trace the notice to its file.

@iamwhatever

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: unverified_write notice wording

  • span=82d94d510800 engineering phrasing — rebutted

The wording was set by the decision this PR implements: say only that the diff is unavailable because the write failed and the earlier file is unknown, and never imply the file is unchanged. "Changes not shown ... can't be confirmed" drops the cause (the write failed), which is the fact the reader needs.
"write" matches the tool name the card reports (fs_write), and the sentence is translated in every catalog.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Confirm every backend sets refused only before the tool ran — fixed in b806238

It cannot be confirmed for every selectable backend, so the word is no longer trusted: _settle_failed_write judges failed and refused alike, by content only.
With create's pre-call content block, equal file means no entry and a different file shows the real diff; without one, both keep the path as unverified_write.
Pinned by test_a_refused_reissue_shows_no_invented_hunk and test_a_write_that_did_not_complete_claims_no_change[refused].

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • Does kiro-cli's refused really mean the tool never ran — fixed in b806238

Not provable from the stream (tool results never carry Crew's own refusal; refused is the backend's word), so nothing is deleted on that word any more: _settle_failed_write takes no status and decides by content alone.
Only a whole pre-call snapshot that still matches the file removes an entry; every other refused or failed write keeps its path as unverified_write.

@iamwhatever

Copy link
Copy Markdown
Contributor Author

/ai-review override first-principles 9a3f0eb: agent: false positive, the runner's import difflib is read by owner functions through chat_turn.compose (which binds no module objects), and deleting it makes _excerpt_pair raise NameError (7 TestExcerptDiffWork failures on a clean archive of this head; rebuttals issuecomment-6203869120 and issuecomment-6203869904).

@github-actions

Copy link
Copy Markdown
Contributor

Human judgment recorded

@iamwhatever marked the first-principles AI finding as false positive, not applicable, or explicitly accepted for 9a3f0ebd4cd11f73ca5f937a35296d5f8bda12b0.

agent: false positive, the runner's import difflib is read by owner functions through chat_turn.compose (which binds no module objects), and deleting it makes _excerpt_pair raise NameError (7 TestExcerptDiffWork failures on a clean archive of this head; rebuttals issuecomment-6203869120 and issuecomment-6203869904).

This decision applies only to this commit. A new push requires a new judgment.

@iamwhatever
iamwhatever force-pushed the fix/fast-excerpt-failed-write branch from 9a3f0eb to a4ee966 Compare October 15, 2026 23:25
@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 readiness: action required A blocking check or review needs attention labels Oct 15, 2026
@iamwhatever
iamwhatever force-pushed the fix/fast-excerpt-failed-write branch from a4ee966 to 29c7973 Compare October 15, 2026 23:57
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 15, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: the post-result refinement re-mark now checks the call id still names one call

  • span=db37e66c9e96 Recheck call identity before applying failed-write refinements — fixed in 29c7973

The refinement re-mark now requires _refined_writer not in _tcid_collapsed beside in _failed_write_calls; a key in that set already passed the result branch's _tcid_identifies checks, so the pair is the same identity rule. A call whose id collapsed onto a failed call's key keeps its own diff.
Pinned by TestFailedWriteRecordsNoChange::test_a_collapsed_refinement_keeps_another_calls_diff: red without the guard (the other call's entry became unverified_write), green with it.

@iamwhatever

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: unproven failed-write entries are marked before the cancellable settlement await

  • span=db37e66c9e96 Preserve failed-write state before the cancellable settlement await — fixed in 29c7973

On a failed/refused result every entry without pre_call_digest is marked unverified_write synchronously; only digest-backed entries reach the asyncio.to_thread(_settle_failed_write, ...) read, and the await is skipped when there are none. A cancel there leaves create's captured pre-call content as the before, never a reverse substitution.
Pinned by test_cancelled_settlement_keeps_unproven_write_path_only[unproven-only, mixed]: both red without the fix (invented value = 1 -> value = 2), green with it.

@cruisercohen cruisercohen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting one change on 29c7973.

Both changes since a4ee966 check out. The refinement re-mark now skips a collapsed id, and an entry without pre_call_digest is marked unverified_write before the off-loop settlement read. But an earlier await in the same result branch still runs before that marking, and it reopens the invented-diff case from #20419.

Blocking: a cancel during the repeat-loop steer persists an invented hunk. In chat_runner.py, await _steer_repeat_loop_notice(...) at line 11276 runs before the failed/refused block at lines 11319-11332. The third identical failed result sends that notice, which waits up to STEER_NOTICE_BOUND_SECS (5 s) on a backend that supports refusal steers. If the turn is cancelled during that wait, the _failed_write_calls entry and the unverified_write mark are both skipped, and the teardown flush stores the reverse substitution as the before. GPT 6.1's finding on this head is correct.

I reproduced it locally on this head with the turn harness. The setup: three identical strReplace calls (value = 1 to value = 2) against a file that already holds value = 2, each with a failed result, and a provider whose steer raises CancelledError. The persisted entry has before head\nvalue = 1\ntail\n, after head\nvalue = 2\ntail\n and no omitted_reason, while the file never changed. Without the cancel, the same three calls store the path-only unverified_write entry.

Fix: move the two _loop_notice lines below the failed-write block. Alternatively, run the synchronous _failed_write_calls.add and the unverified_write marking before any await in this branch, and keep the digest-backed settlement off the loop. Moving the steer below the block turned the probe green locally, and all 21 TestFailedWriteRecordsNoChange tests still pass. A test like the probe (a steer that raises CancelledError on the third repeat) would pin 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 Oct 16, 2026
@iamwhatever
iamwhatever force-pushed the fix/fast-excerpt-failed-write branch from 29c7973 to af4aa79 Compare October 16, 2026 01:02
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 16, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: one fail-closed default replaces the per-await marks: a failed or refused call's entries are unverified_write from its result on, and only a completed content check drops or clears them

  • span=db37e66c9e96 Mark failed writes before awaiting the repeat-loop steer — fixed in af4aa79

Fixed for the whole class, failed-write cancel windows, not for this one await. On a failed or refused result, while the id still names one call, every entry that call recorded alone is marked unverified_write synchronously, before the result's first await (chat_runner.py, right after _tool_terminal). Only _settle_failed_write's completed content check changes that: unchanged, the entry is removed; changed, the mark is cleared and the real diff shows. No pre-call snapshot, or a file that is gone, unreadable or past the bound, stays path-only. The earlier mark before the settlement read is gone, so the rule lives in one place; production code is net -1 line.
Pinned by test_cancelled_failed_write_keeps_unproven_paths_only, one case per await between the result and the flush: the repeat-loop steer, the settlement read, the MCP app render, the PostToolUse hook, the compaction notice, the next stream event and the turn-end reads. Each stores only path-only unverified_write entries. On 29c7973 the steer case stores the invented value = 1 -> value = 2 hunk and the settlement case the digest-backed sibling's text; both pass now. Removing the early mark fails all seven.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • A successful write after a failed one replaces the failed call's entry and hides its torn bytes — answered

Intended, and only for an entry still marked unverified_write, one with no proven pre-call content (_record_turn_snapshot's first loop pops only those). For it no before exists that could show torn bytes as a change: its before is a fragment or a reverse substitution, the invented diff this PR removes. The later write's own snapshot is the file as that write found it, torn bytes included, so its diff is real (test_a_retry_after_an_unproven_failure_shows_its_own_diff).
A failed create whose content check proved a change has its mark cleared, is never popped, and keeps its content-block before when another call writes the path later, so its torn bytes stay on the card (test_a_failed_create_is_judged_by_its_content_block[True] for the clear; the second loop of _record_turn_snapshot keeps the first before).

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • span=b81b89ca6c0e Item 4: the content re-read on a failed create is more than the Goal needs — rebutted

The re-read is the behavior the decision this PR implements specifies: a failed or refused write is content-checked, and only otherwise kept path-only. It does what path-only cannot. A failed create that left the file as its content block says records no entry, instead of a "write failed" notice on a file that never changed. One whose bytes did reach disk (the backend's failed can follow a partial write) shows that real diff instead of hiding it.
Its scope is unchanged since the approved head 7b0111f52a. This round only made it the one step that may lift the fail-closed mark: _settle_failed_write now returns (same, changed). Pinned by test_a_failed_create_is_judged_by_its_content_block[False, True] and test_a_retry_after_a_proven_no_change_failure_keeps_its_own_before.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • span=023a45912520 Item 9: runner import difflib # noqa: F401 has zero consumers — rebutted

Its consumer is the composition: chat_turn.compose runs every owner function on the runner's globals(), and _bind binds an owner's names there except module objects, so _budgeted_opcodes reads difflib from the runner namespace, not from file_changes.py's own import.
Reproduced on this head (git archive of af4aa79 with line 6 deleted): 7 TestExcerptDiffWork tests fail with NameError: name 'difflib' is not defined at chat_turn/file_changes.py:120, reached from _excerpt_pair. Same ruling as spans 5f898398f0ae and 5d52adfe5098.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • span=3238e6630d75 One label, two meanings — rebutted (out of goal)

Both rows are file-only: neither shows a diff. Why differs, and the card says it once in its notice; the red tag's colour, icon and hover text tie the failed row to it with no new string (same ruling as spans 60b09543c17f, 1f81c6d5ff7c and 27458d10d5f0).

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • span=86248dac1230 The error doesn't name its file — rebutted (out of goal)

The notice wording is fixed by the decision this PR implements and translated in every catalog; naming files is a new feature (same ruling as spans cefe4878ee29, 454eb213173f, 62f3d699f047 and 48933b62c80c). The failed row carries the notice's colour, icon and sentence as hover text.

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • span=6b76370c2ab6 The open-file hint sits under the wrong line — rebutted

The hint covers every row on the card. It sits on its own muted line outside the ErrorNotice alert element, so it is not read as part of the error or the hand-off (fixed that way for span 4f48da889d80), and every row, the failed one included, opens on click (same ruling as spans 01912cb17c86 and c0e482a6a0dc).

@iamwhatever

Copy link
Copy Markdown
Contributor Author
  • span=9eee8da4d5c7 Reword diff_unavailable_unverified_write to name the file and drop "diff" and "write" — rebutted (out of goal)

The sentence is the one the decision this PR implements settled ("Diff unavailable: the write failed, and the file before it is unknown."), translated in all 13 catalogs; new copy with a {{file}} slot is a new string in each and a new feature (same rulings as spans 201375e5450b and cefe4878ee29).

@iamwhatever

Copy link
Copy Markdown
Contributor Author

@cruisercohen, on your review of 29c79739bf: fixed on af4aa79353 with your second option. Every entry a failed or refused call recorded alone is marked unverified_write before any await in the result branch, so the steer now runs after the mark. Only the off-loop content check of a digest-backed entry may then drop it (unchanged) or clear the mark (changed).

test_cancelled_failed_write_keeps_unproven_paths_only pins it, one case per await between the result and the flush. Its steer case is your probe's window: the repeat-loop notice fires on the failed result and the steer raises CancelledError, and the stored entry is path-only unverified_write, never the value = 1 -> value = 2 hunk. The case forces the notice that the third identical failed result sends rather than replaying three calls. It fails on 29c79739bf and passes here; the other six cases cover the settlement read, the MCP app render, the PostToolUse hook, the compaction notice, the next stream event and the turn-end reads.

@iamwhatever

Copy link
Copy Markdown
Contributor Author

Rebased onto main (5ddd790e67). The old head 7b0111f52a was 120 commits behind and conflicted. Three fixes on top, for GPT 6.1 findings on the rebased heads; nothing else changes behavior.

Conflicts resolved:

  • refactor(dashboard): move chat runner helpers into chat_turn owners #20755 moved _turn_after_reads, _flush_file_changes and the snapshot constants from chat_runner.py into chat_turn/file_changes.py. This PR's changes to them moved there with them. _EXCERPT_DIFF_BUDGET now sits beside _EXCERPT_CONTEXT_LINES, and chat_turn.compose binds it back onto the runner.
  • chat_runner.py imports: both sides kept.
  • Spec paragraph in learn-cron-dashboard.md: this PR's text, plus main's chat_turn/file_changes.py::_turn_after_reads path.

Fixes (each with a turn-loop test and a spec clause):

  • span 2bc3ebc231fc: a failed write's refinement, which can follow its result in one frame, could still show an invented diff. The runner remembers each identified call whose result said failed or refused and marks any snapshot that call records afterwards unverified_write.
  • span db37e66c9e96 (1): that re-mark applies only while the call id still names one call, so a call whose id collapsed onto it keeps its own diff.
  • span db37e66c9e96 (2): failed-write cancel windows. A cancel at an await between a failed result and the flush (the settlement read, then the repeat-loop steer) could store an invented diff. Now every entry a failed or refused call recorded alone is unverified_write from its result on, set before any await; only the completed content check removes it (unchanged) or clears the mark (changed). One test cancels at each of the seven awaits between the result and the flush.

Rebase audit: main added no locale catalog, no new reader of omitted_reason, and no new path into _flush_file_changes or _settle_failed_write. The diff_unavailable_unverified_write key is in all 13 catalogs that hold its sibling keys.

Interdiff: force-push compare. That view also shows main's commits, so the patch-only range-diff is below.

git range-diff 9163daf0fc..7b0111f52a 5ddd790e67..af4aa79353
1:  7b0111f52a ! 1:  af4aa79353 fix(dashboard): bound the large-file window diff and never invent a failed write's diff
    @@ docs/system-specs/modules/learn-cron-dashboard.md: When that ceiling fires, `_bo
      
      Streaming chunks are cleaned up after each response (only final assistant message kept). Transient roles (`chunk`, `done`, `queued`, `permission`) are excluded from history saves.
      
    --**File-change snapshots** (`chat_turn/file_changes.py::_snapshot_write_target`): captures before/after content for write-tool invocations, attached as `file_changes` meta by `_flush_file_changes` on the last assistant message **this t … (line cut here; the full line is in the spec)
    -+**File-change snapshots** (`chat_turn/file_changes.py::_snapshot_write_target`): captures before/after content for write-tool invocations, attached as `file_changes` meta by `_flush_file_changes` on the last assistant message **this t … (line cut here; the full line is in the spec)
    +-**File-change snapshots** (`chat_turn/file_changes.py::_snapshot_write_target`): captures before/after content for write-tool invocations, attached as `file_changes` meta by `_flush_file_changes` on the last assistant message **this t … (line cut here; the full line is in the spec)
    ++**File-change snapshots** (`chat_turn/file_changes.py::_snapshot_write_target`): captures before/after content for write-tool invocations, attached as `file_changes` meta by `_flush_file_changes` on the last assistant message **this t … (line cut here; the full line is in the spec)
      
      The in-turn accumulator (`_record_turn_snapshot`) holds every distinct path of the turn once, with each path's first before-snapshot and truncation flag preserved. A before proven to be the whole file (strReplace reconstruction, a dis … (line cut here; the full line is in the spec)
      
    @@ src/kiro_crew/dashboard/chat_runner.py
      import functools  # noqa: F401
      import hashlib
      import inspect
    -@@ src/kiro_crew/dashboard/chat_runner.py: from kiro_crew.dashboard.chat_turn.directives import (  # noqa: F401
    - from kiro_crew.dashboard.chat_turn.file_changes import (  # noqa: F401
    +@@ src/kiro_crew/dashboard/chat_runner.py: from kiro_crew.dashboard.chat_turn.file_changes import (  # noqa: F401
    +     _MAX_RECONSTRUCT_BYTES,
          _PATH_TRUNCATION_MARKER,
          _apply_turn_snapshot_budget,
     +    _budgeted_opcodes,
    @@ src/kiro_crew/dashboard/chat_runner.py: from kiro_crew.dashboard.chat_turn.direc
     +    _disk_digest,
          _excerpt_pair,
     +    _failed_write_entries,
    +     _flush_file_changes,
     +    _forget_entries,
          _line_change_input,
          _note_reply_row,
    @@ src/kiro_crew/dashboard/chat_runner.py: from kiro_crew.dashboard.chat_turn.file_
          _Snapshot,
          _snapshot_write_target,
          _truncate_snapshot,
    -@@ src/kiro_crew/dashboard/chat_runner.py: _MAX_TURN_FULL_CONTENT_CHARS = 2 * _MAX_RECONSTRUCT_BYTES
    - # Unchanged lines ``_excerpt_pair`` shows around each changed run of a windowed
    - # large-file diff. Runs closer than twice this share one window.
    - _EXCERPT_CONTEXT_LINES = 20
    -+# Work ``_excerpt_pair``'s line diff may do, in line-occurrence visits
    -+# (``_budgeted_opcodes``). Repeated lines make that diff superlinear; past this
    -+# the windows are given up and the top cut is kept. About half a second of CPU.
    -+_EXCERPT_DIFF_BUDGET = 4_000_000
    - 
    - # Chars of path+before+after one TURN's file_changes may carry across every
    --# entry EXCEPT its protected one -- the most recent entry whose content differs,
    -+# entry EXCEPT its protected one -- the most recent entry whose content differs
    -+# or, truncated, may differ,
    - # which ``_apply_turn_snapshot_budget`` keeps whole and never charges here.
    - # ``_MAX_SNAPSHOT`` bounds one file; without a turn bound a single turn that
    - # rewrites a dozen large files still attaches megabytes to one message, and
    -@@ src/kiro_crew/dashboard/chat_runner.py: def _turn_after_reads(
    -     before ``_excerpt_pair`` cuts windows: a window can start inside a credential
    -     (a PEM body below its BEGIN line), and the redactor recognizes one only with
    -     its delimiters in view. The flush redacts the windows again, which leaves
    --    already-redacted text unchanged.
    -+    already-redacted text unchanged. Whole sides proven equal store two empty
    -+    sides: a no-change entry, not a truncated one that could take the turn
    -+    budget's protected slot from a real change.
    -     """
    -     reads: dict[str, tuple[_Snapshot | None, tuple[str, str] | None]] = {}
    -     for key, (after, whole) in _read_turn_afters(changes, turn_had_shell).items():
    --        excerpt = _excerpt_pair(redact(whole[0]), redact(whole[1])) if whole else None
    -+        excerpt = None
    -+        if whole:
    -+            excerpt = (
    -+                ("", "")
    -+                if whole[0] == whole[1]
    -+                else _excerpt_pair(redact(whole[0]), redact(whole[1]))
    -+            )
    -         reads[key] = (after, excerpt)
    -     return reads
    - 
    -@@ src/kiro_crew/dashboard/chat_runner.py: def _flush_file_changes(
    -             }
    -             if fc.get("pending_str_replace"):
    -                 pending_by_path[key] = fc["pending_str_replace"]
    -+        if fc.get("unverified_write"):
    -+            deduped[key]["_unverified_write"] = True
    -         # The accumulator moves a path's first snapshot to the tail on every
    -         # write, so its position carries last-write recency without extra rows.
    -         # Assigning on every occurrence also supports directly supplied repeats.
    -@@ src/kiro_crew/dashboard/chat_runner.py: def _flush_file_changes(
    -             entry["before"], entry["after"] = excerpt
    -         elif before_truncated or (after is not None and after.truncated):
    -             entry.update(truncated=True, snapshot_limit_chars=_MAX_SNAPSHOT)
    -+        if entry.pop("_unverified_write", False):
    -+            # A failed write with no whole pre-call snapshot: no diff can be
    -+            # proven either way (_settle_failed_write), so only the path is kept,
    -+            # with the reason the card states.
    -+            entry.update(
    -+                before="",
    -+                after="",
    -+                truncated=True,
    -+                content_omitted=True,
    -+                omitted_reason="unverified_write",
    -+            )
    -+            entry.pop("snapshot_limit_chars", None)
    -     # Scrub exfil URLs and credentials from path/before/after BEFORE attaching
    -     # to message meta. _save_slot_to_history runs _redact_meta on persist, but
    -     # the in-memory slot.messages reaches the dashboard UI via SSE/WS BEFORE
    +@@ src/kiro_crew/dashboard/chat_runner.py: async def _run_chat(
    +     # keeps what is retained at 16 characters however long the id was.
    +     _tcid_first_source: dict[str, str] = {}
    +     _tcid_collapsed: set[str] = set()
    ++    # Identified failed calls only: bounded by the turn's first-source map.
    ++    _failed_write_calls: set[str] = set()
    +     # tool_call_id -> canonical directive-tool name (forgery gate). Written
    +     # ONLY at EVENT_TOOL_CALL, ONLY from the out-of-band _meta.kiro identity
    +     # (event.tool_name + event.mcp_server_name), never from the title. This is
    +@@ src/kiro_crew/dashboard/chat_runner.py: async def _run_chat(
    +                             _file_snapshot_upd,
    +                             _tcid_identity_key(getattr(event, "tool_call_id", None)),
    +                         )
    ++                        # A refinement can follow its failed result, even as the
    ++                        # call's first snapshot: claim no diff, while the id still
    ++                        # names one call.
    ++                        _refined_writer = _tcid_identity_key(event.tool_call_id)
    ++                        if (
    ++                            _refined_writer in _failed_write_calls
    ++                            and _refined_writer not in _tcid_collapsed
    ++                        ):
    ++                            for _held in _failed_write_entries(slot, _refined_writer):
    ++                                _held["unverified_write"] = True
    +                     # Refresh the toolLog entry (sseToolActivity merges by id).
    +                     state.broadcast_ws(
    +                         "tool_call",
    +@@ src/kiro_crew/dashboard/chat_runner.py: async def _run_chat(
    +                 # stream actually reported. `stop_reason` on this event describes
    +                 # the turn, not the tool, so it is not used here.
    +                 _tool_terminal = event.tool_final or (event.tool_status in TERMINAL_TOOL_STATUSES)
    ++                _not_written = []
    ++                if (
    ++                    _tool_terminal
    ++                    and _tcid_identifies
    ++                    and (event.refusal or event.tool_status in ("failed", "refused"))
    ++                ):
    ++                    _failed_write_calls.add(_tcid_row_key)
    ++                    _not_written = _failed_write_entries(slot, _tcid_row_key)
    ++                    # Cancellation at any await must leave only paths until a
    ++                    # completed content check proves what the call changed.
    ++                    for _held in _not_written:
    ++                        _held["unverified_write"] = True
    +                 _loop_notice = _repeat_loop.note_result(
    +                     event.tool_call_id,
    +                     status=event.tool_status,
     @@ src/kiro_crew/dashboard/chat_runner.py: async def _run_chat(
                              result_digest=event.tool_output_digest,
                              result_bytes=event.tool_output_bytes,
                          )
    -+                    # A write that did not change its file must not show an edit
    -+                    # (_failed_write_entries). Only an id that still names one
    -+                    # call may drop: a collapsed id would also match another
    -+                    # call's successful write. The re-read is offloaded.
    -+                    if _tool_status in ("failed", "refused") and _tcid_identifies:
    -+                        _not_written = _failed_write_entries(
    -+                            slot, _tcid_identity_key(getattr(event, "tool_call_id", None))
    ++                    _digest_backed = [held for held in _not_written if held.get("pre_call_digest")]
    ++                    if _digest_backed:
    ++                        _same, _changed = await asyncio.to_thread(
    ++                            _settle_failed_write, _digest_backed
     +                        )
    -+                        if _not_written:
    -+                            _same, _unproven = await asyncio.to_thread(
    -+                                _settle_failed_write, _not_written
    -+                            )
    -+                            _forget_entries(slot, _same)
    -+                            for _held in _unproven:
    -+                                _held["unverified_write"] = True
    ++                        _forget_entries(slot, _same)
    ++                        for _held in _changed:
    ++                            _held.pop("unverified_write", None)
                      # MCP Apps (flag-independent on this side): if gatewayd spooled a
                      # UI payload it injected an opaque marker into the result text.
                      # Load it, push an mcp_app_render event to this slot, and strip
     
      ## src/kiro_crew/dashboard/chat_turn/file_changes.py ##
    -@@
    +@@ src/kiro_crew/dashboard/chat_turn/file_changes.py: the end-of-turn file-change flush."""
      
      from __future__ import annotations
      
    @@ src/kiro_crew/dashboard/chat_turn/file_changes.py
      from pathlib import Path
      from typing import TYPE_CHECKING, Any, NamedTuple
      
    - if TYPE_CHECKING:
    -     from kiro_crew.dashboard.chat_runner import (
    -         _EXCERPT_CONTEXT_LINES,
    -+        _EXCERPT_DIFF_BUDGET,
    -         _MAX_RECONSTRUCT_BYTES,
    -         _MAX_SLOT_MESSAGES,
    -         _MAX_SNAPSHOT,
     @@ src/kiro_crew/dashboard/chat_turn/file_changes.py: def _safe_read_file_text(path: str, *, whole: bool) -> _Snapshot | None:
              return None
      
    @@ src/kiro_crew/dashboard/chat_turn/file_changes.py: def _record_turn_snapshot(
     +def _settle_failed_write(
     +    entries: list[dict[str, Any]],
     +) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
    -+    """Split a failed or refused call's entries into (no change, unprovable).
    ++    """Split a failed or refused call's entries into (no change, changed).
     +
     +    Content decides, never the backend's status word: neither ``failed`` nor
     +    ``refused`` proves the tool left the file alone. A whole pre-call snapshot
    @@ src/kiro_crew/dashboard/chat_turn/file_changes.py: def _record_turn_snapshot(
     +    entry goes; different means real bytes changed, so it stays and shows that
     +    diff. Without one the outcome is unprovable: the before is a fragment, a
     +    reverse substitution or a disk read that may already hold part of the
    -+    write, so neither "no change" nor the requested hunk can be claimed; the
    -+    caller marks it ``unverified_write`` and the flush keeps only its path.
    -+    Entries whose snapshot differs from the file are returned in neither list
    -+    and stay as they are.
    ++    write, so neither "no change" nor the requested hunk can be claimed.
    ++    Unprovable entries are returned in neither list and stay ``unverified_write``.
     +
     +    Reads up to ``_MAX_RECONSTRUCT_BYTES`` per path, so it runs off the loop.
    -+    A file that is gone, unreadable or past the bound is never dropped as unchanged.
    ++    A file that is gone, unreadable or past the bound is unprovable too.
     +    """
     +    same: list[dict[str, Any]] = []
    -+    unproven: list[dict[str, Any]] = []
    ++    changed: list[dict[str, Any]] = []
     +    for held in entries:
     +        digest = held.get("pre_call_digest")
     +        if not digest:
    -+            unproven.append(held)
     +            continue
     +        now = _safe_read_file_text(held["path"], whole=True)
    -+        if now is not None and _disk_digest(now.content) == digest:
    -+            same.append(held)
    -+    return same, unproven
    ++        if now is not None:
    ++            (same if _disk_digest(now.content) == digest else changed).append(held)
    ++    return same, changed
     +
     +
     +def _forget_entries(slot: "_ChatSlot", entries: list[dict[str, Any]]) -> None:
    @@ src/kiro_crew/dashboard/chat_turn/file_changes.py: def _line_change_input(fc: di
              return None
          return {"content": fc.get("content") or "", "after": after.content}
      
    +@@ src/kiro_crew/dashboard/chat_turn/file_changes.py: _MAX_RECONSTRUCT_BYTES = 2_000_000
    + _EXCERPT_CONTEXT_LINES = 20
    + 
    + 
    ++# Work ``_excerpt_pair``'s line diff may do, in line-occurrence visits
    ++# (``_budgeted_opcodes``). Repeated lines make that diff superlinear; past this
    ++# the windows are given up and the top cut is kept. About half a second of CPU.
    ++_EXCERPT_DIFF_BUDGET = 4_000_000
    ++
    ++
    + # Chars of path+before+after one TURN's file_changes may carry across every
    +-# entry EXCEPT its protected one -- the most recent entry whose content differs,
    ++# entry EXCEPT its protected one -- the most recent entry whose content differs
    ++# or, truncated, may differ,
    + # which ``_apply_turn_snapshot_budget`` keeps whole and never charges here.
    + # ``_MAX_SNAPSHOT`` bounds one file; without a turn bound a single turn that
    + # rewrites a dozen large files still attaches megabytes to one message, and
    +@@ src/kiro_crew/dashboard/chat_turn/file_changes.py: def _turn_after_reads(
    +     before ``_excerpt_pair`` cuts windows: a window can start inside a credential
    +     (a PEM body below its BEGIN line), and the redactor recognizes one only with
    +     its delimiters in view. The flush redacts the windows again, which leaves
    +-    already-redacted text unchanged.
    ++    already-redacted text unchanged. Whole sides proven equal store two empty
    ++    sides: a no-change entry, not a truncated one that could take the turn
    ++    budget's protected slot from a real change.
    +     """
    +     reads: dict[str, tuple[_Snapshot | None, tuple[str, str] | None]] = {}
    +     for key, (after, whole) in _read_turn_afters(changes, turn_had_shell).items():
    +-        excerpt = _excerpt_pair(redact(whole[0]), redact(whole[1])) if whole else None
    ++        excerpt = None
    ++        if whole:
    ++            excerpt = (
    ++                ("", "")
    ++                if whole[0] == whole[1]
    ++                else _excerpt_pair(redact(whole[0]), redact(whole[1]))
    ++            )
    +         reads[key] = (after, excerpt)
    +     return reads
    + 
    +@@ src/kiro_crew/dashboard/chat_turn/file_changes.py: def _flush_file_changes(
    +             }
    +             if fc.get("pending_str_replace"):
    +                 pending_by_path[key] = fc["pending_str_replace"]
    ++        if fc.get("unverified_write"):
    ++            deduped[key]["_unverified_write"] = True
    +         # The accumulator moves a path's first snapshot to the tail on every
    +         # write, so its position carries last-write recency without extra rows.
    +         # Assigning on every occurrence also supports directly supplied repeats.
    +@@ src/kiro_crew/dashboard/chat_turn/file_changes.py: def _flush_file_changes(
    +             entry["before"], entry["after"] = excerpt
    +         elif before_truncated or (after is not None and after.truncated):
    +             entry.update(truncated=True, snapshot_limit_chars=_MAX_SNAPSHOT)
    ++        if entry.pop("_unverified_write", False):
    ++            # A failed write with no proven content change: no diff can be
    ++            # claimed (_settle_failed_write), so only the path is kept,
    ++            # with the reason the card states.
    ++            entry.update(
    ++                before="",
    ++                after="",
    ++                truncated=True,
    ++                content_omitted=True,
    ++                omitted_reason="unverified_write",
    ++            )
    ++            entry.pop("snapshot_limit_chars", None)
    +     # Scrub exfil URLs and credentials from path/before/after BEFORE attaching
    +     # to message meta. _save_slot_to_history runs _redact_meta on persist, but
    +     # the in-memory slot.messages reaches the dashboard UI via SSE/WS BEFORE
     
      ## temp-screenshots/20419/unverified-write-and-budget-card-dark.png (new) ##
     @@
    @@ test/test_file_change_snapshots.py: class TestLargeFileEditRedaction:
     +        _assert_unverified(entry)
     +
     +    @pytest.mark.asyncio
    ++    @pytest.mark.parametrize("status", ["failed", "refused"])
    ++    @pytest.mark.parametrize("initial_input", [True, False], ids=["raw-input", "no-raw-input"])
    ++    async def test_a_terminal_write_refinement_claims_no_diff(
    ++        self, short_tmp_dir, status, initial_input
    ++    ):
    ++        f = short_tmp_dir / "cfg"
    ++        f.write_text(self.DISK)
    ++        call = _str_replace_call("call-1", f, "value = 1", "value = 2")
    ++        if not initial_input:
    ++            call = AcpEvent(
    ++                kind=EVENT_TOOL_CALL,
    ++                tool_call_id="call-1",
    ++                tool_name="fs_write",
    ++                tool_kind="edit",
    ++                title="Write",
    ++            )
    ++        refinement = _str_replace_call("call-1", f, "value = 1", "value = 2")
    ++        refinement.kind = EVENT_TOOL_CALL_UPDATE
    ++        # One terminal frame can emit its result before its rawInput refinement.
    ++        record = await _snapshot_turn(call, _result("call-1", status), refinement, *_LANDS)
    ++
    ++        (entry,) = _persisted_meta(record, "done")["file_changes"]
    ++        assert entry["path"] == str(f)
    ++        _assert_unverified(entry)
    ++        assert f.read_text() == self.DISK
    ++
    ++    @pytest.mark.asyncio
     +    async def test_a_torn_write_read_as_pre_write_claims_no_diff(self, short_tmp_dir):
     +        # The write landed in part before the dashboard read the file: the torn
     +        # prefix holds oldStr once and no newStr, so it reads as pre-write, and
    @@ test/test_file_change_snapshots.py: class TestLargeFileEditRedaction:
     +        assert "omitted_reason" not in changes["good"]
     +        assert changes["good"]["before"] == "head\nvalue = 1\ntail\n"
     +
    ++    @pytest.mark.asyncio
    ++    async def test_a_collapsed_refinement_keeps_another_calls_diff(self, short_tmp_dir):
    ++        bad, good = short_tmp_dir / "bad", short_tmp_dir / "good"
    ++        bad.write_text(self.DISK)
    ++        good.write_text(self.DISK)
    ++        refinement = _str_replace_call(_KEY_ID_B, good, "value = 2", "value = 3")
    ++        refinement.kind = EVENT_TOOL_CALL_UPDATE
    ++        record = await _snapshot_turn(
    ++            _str_replace_call(_KEY_ID_A, bad, "value = 1", "value = 2"),
    ++            _result(_KEY_ID_A, "failed"),
    ++            _str_replace_call(_KEY_ID_B, good, "value = 2", "value = 3"),
    ++            Do(lambda ctx: good.write_text("head\nvalue = 3\ntail\n")),
    ++            refinement,
    ++            _result(_KEY_ID_B, "completed"),
    ++            *_LANDS,
    ++        )
    ++        changes = {Path(c["path"]).name: c for c in _persisted_meta(record, "done")["file_changes"]}
    ++        _assert_unverified(changes["bad"])
    ++        assert "omitted_reason" not in changes["good"]
    ++        assert (changes["good"]["before"], changes["good"]["after"]) == (
    ++            self.DISK,
    ++            "head\nvalue = 3\ntail\n",
    ++        )
    ++
    ++    @pytest.mark.asyncio
    ++    @pytest.mark.parametrize(
    ++        "cancel_point",
    ++        [
    ++            "steer",
    ++            "settlement",
    ++            "app-render",
    ++            "post-tool-hook",
    ++            "compaction-notice",
    ++            "stream",
    ++            "after-reads",
    ++        ],
    ++    )
    ++    async def test_cancelled_failed_write_keeps_unproven_paths_only(
    ++        self, short_tmp_dir, monkeypatch, cancel_point
    ++    ):
    ++        import asyncio
    ++
    ++        import turn_harness
    ++
    ++        from kiro_crew.dashboard import chat_runner
    ++
    ++        reached = []
    ++
    ++        def cancel(*args, **kwargs):
    ++            reached.append(cancel_point)
    ++            if cancel_point == "settlement":
    ++                (entries,) = args
    ++                assert len(entries) == 1 and entries[0].get("pre_call_digest")
    ++            raise asyncio.CancelledError
    ++
    ++        async def cancel_async(*args, **kwargs):
    ++            cancel(*args, **kwargs)
    ++
    ++        if cancel_point == "steer":
    ++            monkeypatch.setattr(
    ++                chat_runner.RepeatLoopTracker, "note_result", lambda *a, **k: "repeat"
    ++            )
    ++            monkeypatch.setattr(chat_runner, "_steer_repeat_loop_notice", cancel_async)
    ++        elif cancel_point == "settlement":
    ++            monkeypatch.setattr(chat_runner, "_settle_failed_write", cancel)
    ++        elif cancel_point == "app-render":
    ++            monkeypatch.setattr(chat_runner.mcp_apps_render, "handle_tool_result", cancel_async)
    ++        elif cancel_point == "post-tool-hook":
    ++
    ++            async def fire(self, event, *args, **kwargs):
    ++                if event == chat_runner.HOOK_EVENT_POST_TOOL_USE:
    ++                    cancel()
    ++                return []
    ++
    ++            monkeypatch.setattr(turn_harness._NoHooks, "fire", fire)
    ++        elif cancel_point == "compaction-notice":
    ++            monkeypatch.setattr(chat_runner, "_notice_compaction_after_turn", cancel_async)
    ++        elif cancel_point == "after-reads":
    ++            monkeypatch.setattr(chat_runner, "_turn_after_reads", cancel)
    ++
    ++        f, other = short_tmp_dir / "cfg", short_tmp_dir / "other"
    ++        f.write_text(self.DISK)
    ++        other.write_text("old\n")
    ++        refinement = _write_call(1, other, "old\n")[0]
    ++        refinement.kind = EVENT_TOOL_CALL_UPDATE
    ++        ending = (
    ++            [Do(lambda ctx: reached.append(cancel_point)), Raise(asyncio.CancelledError())]
    ++            if cancel_point == "stream"
    ++            else _LANDS
    ++        )
    ++        record = await _snapshot_turn(
    ++            _str_replace_call("call-1", f, "value = 1", "value = 2"),
    ++            refinement,
    ++            _result("call-1", "failed"),
    ++            *ending,
    ++        )
    ++        assert reached == [cancel_point]
    ++        entries = [
    ++            c
    ++            for row in record.rows("assistant")
    ++            for c in row.get("meta", {}).get("file_changes", [])
    ++        ]
    ++        changes = {Path(c["path"]).name: c for c in entries}
    ++        assert len(entries) == len(changes)
    ++        assert set(changes) == (
    ++            {"cfg", "other"} if cancel_point in ("steer", "settlement") else {"cfg"}
    ++        )
    ++        for entry in entries:
    ++            _assert_unverified(entry)
    ++        assert f.read_text() == self.DISK
    ++        assert other.read_text() == "old\n"
    ++
     +    def test_an_unknown_writer_selects_nothing(self) -> None:
     +        from kiro_crew.dashboard.chat_runner import _failed_write_entries
     +

@cruisercohen cruisercohen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving af4aa79. The blocking point from my review on 29c7973 is met.

  • A failed or refused result now marks the call's own entries unverified_write synchronously, right after _tool_terminal and before the result branch's first await. Only _settle_failed_write's completed content read changes that: an unchanged file removes the entry, and a changed one clears the mark so the real diff shows.
  • test_cancelled_failed_write_keeps_unproven_paths_only covers a cancel at each await between the result and the flush, the repeat-loop steer included.
  • Locally on this head, my probe from the last review (three identical failed strReplace calls, with a steer that raises CancelledError) now stores the path-only entry, and TestFailedWriteRecordsNoChange passes. A failed create that changed the file before failing still shows its real before and after.

Every check on this head passes, and GPT 6.1 has no blocking findings.

Non-blocking: a failed create of a new file that never reached disk reads back as gone, so it stays path-only with the "earlier file is unknown" notice. Its earlier state was known (no file), so "no change" would be accurate there. Path-only claims nothing false, so I would not hold the PR on it.

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.

File-change card: slow diff on large files, invented change after a failed write, lost diff on a one-file turn

4 participants