Skip to content

fix(dashboard): keep the fragment when another session writes a deferred strReplace path - #18043

Closed
buluoray wants to merge 1 commit into
mainfrom
fix/cross-slot-pending-strreplace-17619
Closed

buluoray wants to merge 1 commit into
mainfrom
fix/cross-slot-pending-strreplace-17619

Conversation

@buluoray

@buluoray buluoray commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A file-change chip never shows an intermediate "before" when a second session makes the same strReplace on the same file during the turn.

The deferred-strReplace "before" resolution is guarded by a writer-id gate in _record_turn_snapshot, but that gate only sees writes recorded on the current slot. A second, concurrent session making a byte-identical strReplace on the same file inside the turn window is never seen, so the turn-end read can equal the forward substitution and the saved chip shows an intermediate before (one deleted line missing).

Why it matters

The chip is the user's record of what the agent changed. The file on disk is correct, but the chip under-reports the change with no flag. It needs two sessions editing the same narrow file shape at once, so it is rare, but it is a silent wrong answer rather than a visible fallback.

Not a goal

  • Attributing shell edits per path: a turn with a shell tool already skips resolution (turn_had_shell), unchanged here.
  • Settling the post-write hypothesis, which stays deliberately unresolved.
  • Any change to what the chip renders; only whether a deferred before is settled.

What changed (motivation → approach → change)

  • Symptom → cause: the writer-id gate is slot-local, so a writer in another slot never drops the deferred pending_str_replace.
  • Approach: a process-wide write log the flush can read, as the issue suggests. _PathWriteLog (owner chat_turn/file_changes.py, instance chat_runner._PATH_WRITES because the composition contract keeps runner state on the runner) maps canonical path → slot → [last_seq, open].
  • _record_turn_snapshot notes every recorded write on it; _run_chat marks the turn start (_begin_turn_path_writes, stored in the new _ChatSlot._file_changes_since); _flush_file_changes closes the slot's paths at turn end and skips resolution when another slot wrote the path after this turn began OR still holds it open in an unfinished turn (a permission-gated write is recorded at request time and can land later).
  • Bounded to 4096 paths and 64 writers per path. A forgotten record keeps the uncertainty it carried: a closed one raises a watermark to its own write's sequence number (only turns begun before it treat every path as foreign), and an open one marks its slot as a suspect for every other turn until that slot's turn closes. So a bound only fails closed, including for a path re-created after eviction, and churn through closed history never disables resolution for later turns. Closing a turn re-stamps the slot's records with the close's sequence number, because a write requested before another turn began can land inside it on late approval. A slot whose write-tool snapshot is still being taken off the event loop counts as foreign for every other slot until it records. Each slot gets a token minted once and kept on it (_ChatSlot._path_write_token), never a reusable id().
  • Spec: docs/system-specs/modules/learn-cron-dashboard.md (File-change snapshots) records the new guard.

Signatures the guard is meant to cover:

  • Another slot records a write after this turn began → caught.
  • Another slot recorded before this turn began and its turn is still open → caught.
  • Another slot's turn finished before this turn began → not counted (correct: its write is already in this turn's snapshot).
  • A second writer in the SAME slot → still handled by the existing writer-id gate, unchanged.
  • A shell edit → still handled by turn_had_shell, unchanged.
  • A write outside any chat slot (an editor, a cron script) → NOT caught; it never passes through _record_turn_snapshot. Same as before this PR.
  • Another slot's auto-approved write in the gap between kiro-cli landing it on disk and its tool-call event reaching the dashboard → NOT caught; nothing records it until the event arrives. Bounded by ACP event latency; not measured on this host.

Backwards compatibility

Compatible: a deferred before is now settled in strictly fewer cases; every case that stops settling keeps the fragment it showed before #17367.

Removes nothing: no field, key or public function is removed or tightened; one slot attribute and module helpers are added.

Tests

test/test_file_change_snapshots.py:

  • test_identical_edit_by_another_session_keeps_the_fragment — two slots record the same deferred edit; the first keeps the fragment. Red with the flush check stubbed out.
  • test_other_session_still_in_its_turn_keeps_the_fragment — a slot that recorded before this turn and is still open counts. Red with the flush check stubbed out.
  • test_other_session_finished_before_this_turn_still_resolves — control: a finished earlier turn does not block resolution. Red with _close_turn_path_writes stubbed out.
  • TestPathWriteLog — an evicted closed write counts only for older turns; evicting old closed history leaves later turns resolvable; an evicted open write stays foreign until its turn closes; a path re-created after eviction still counts as foreign; writers per path are bounded and a drop fails closed; closing re-stamps records; closing a slot with forgotten open writes fails closed; an in-flight write snapshot counts until it ends and only write tools are marked; slot tokens are minted once; own writes never count; closed old writes do not.

Run: pytest -n0 test/test_file_change_snapshots.py test/test_chat_runner_composition_contract.py test/test_crew_recency.py → 396 passed. black, isort, flake8 and mypy clean on the touched files.

Manual verification

N/A — the race needs two live sessions editing one file within one turn window; the unit tests drive the same record/flush entry points the turn loop calls, and the existing turn-loop tests (TestPendingStrReplaceThroughTheTurnLoop) pass with _run_chat now marking the turn start.

Related Issues

Closes #17619

Pattern harvest

Not generalizable: the defect is that one specific gate was scoped to a slot while the resource it guards is shared; the fix is the cross-slot signal that gate needed.

Decision for the owner

The decision is whether a deferred strReplace before may be settled when another session wrote the same file during the turn. This draft says no and adds a bounded process-wide per-path write log that keeps the fragment in that case, rather than accepting the documented limitation.

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

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 2d98b18f336a60147e6dfece0828018e976591f8 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — 🟡 CONCERNS

Premise-level review of 4d8751ef185dc4d2fb837a859fab0146fe995e8e — 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

The per-path log with eviction watermarks, lost-open sets and two bounds exists only to track paths; its own in-flight check already gives that up.

Not justified as shipped

  • Item 3 — oversized: the diff already treats "any other slot in flight" as foreign on every path (if any(other != token for other in self._in_flight): return True). So per-path precision is partly abandoned in this same change. A per-slot log (token → [last_seq, open]) would remove the harm the Goal names, and it would need no bounds, eviction or watermark. A false positive only keeps the fragment, which the description says is acceptable: "every case that stops settling keeps the fragment it showed before fix(dashboard): settle undecidable strReplace before-snapshots at flush #17367".
    Subtraction: replace _paths, max_paths/max_writers, _evicted_seq, _lost_open and _forget with one dict keyed by slot token. Make foreign_since ignore key. Delete _MAX_TRACKED_WRITE_PATHS, _MAX_TRACKED_WRITERS_PER_PATH and their four eviction tests.
    Clears when: the per-path map is replaced by a per-slot record, or the PR shows a measured rate of concurrent writes to different files that would make that fallback too frequent.

What this change ships

Inventory (5 items) — 4 justified

Intent: stop a file-change chip showing a wrong "before" when another session makes the same edit to the same file during the turn. FIX (#17619).

  1. A deferred strReplace "before" is no longer settled when another slot wrote the same path inside this turn — justified
  2. A write by another slot that is still waiting on permission blocks settling until that slot's turn closes — justified
  3. A bounded process-wide per-path log, with eviction watermarks and a lost-open set — oversized
  4. Another slot's snapshot still in flight blocks settling on every path — justified
  5. Two new _ChatSlot attributes (turn-start sequence, a token minted once per slot) — justified

[FIRST-PRINCIPLES-REVIEWED] 4d8751e

Description read

[DESCRIPTION-READ] 9f825d8d3fecbb5c7fa6b8ac87cbe15b9beb79b34376dd0b13a26b8c0c41cb41

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 8, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Check that every turn exit reaches _close_turn_path_writes: a flush that raises before the close leaves an open record that marks that file as foreign until the slot's next turn.

[DESIGN-REVIEWED] 4d8751e

Description read

[DESCRIPTION-READ] 5898ed3eeeb51b8c2b6ce1e0e03d2e7d55418763069738fedbf9be265fc7d175

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 added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 8, 2026
@buluoray
buluoray force-pushed the fix/cross-slot-pending-strreplace-17619 branch from 0be9349 to 5417e3b Compare October 8, 2026 07:09
@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 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 4d8751e

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

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

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — 🔴 changes requested (blocking)

GPT 6.1 found at least one blocking issue that must be resolved before merging 4d8751ef185dc4d2fb837a859fab0146fe995e8e. 1 of 1 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

This comment is updated in place on each push.

BLOCKING -- src/kiro_crew/dashboard/chat_turn/file_changes.py:659 -- Array-valued shell commands now abort chat turns

if not isinstance(raw_params, dict) or raw_params.get("command") not in _WRITE_COMMANDS:
Codex shell rawInput={"command":["bash","-lc","echo acp-replay-corpus"]} → ACP event preserves the dictionary → _run_chat calls _begin_write_snapshot → membership raises TypeError, terminating the turn where _snapshot_write_target previously returned None.
Anchor: residual/crash-data-loss-corruption
Fix: Require command to be a string before testing membership in _WRITE_COMMANDS.

[BLOCK-MERGE] 4d8751e
[GPT-REVIEWED] 4d8751e

Adjudication (Opus 5.5) — is blocking on each finding proportionate?

F1 (fenced). This happens on every Codex shell call, not in some rare edge case. The Codex replay corpus at test/fixtures/acp_frames/codex/session.jsonl:6 sends rawInput.command as a list. chat_runner.py:12203 and :12433 pass that list straight into _begin_write_snapshot. There, file_changes.py:659 checks it against the frozenset from chat_runner.py:1822 without a string check, and a list is unhashable, so it raises TypeError. The older guard at file_changes.py:322 does check that cmd is a string. The fix is one isinstance(..., str) check, so the rarity argument a FLAG needs isn't there.

[ADJUDICATION] 4d8751ef185dc4d2fb837a859fab0146fe995e8e total=0 uphold=0 downgrade=0
[GPT-ADJUDICATED] 4d8751ef185dc4d2fb837a859fab0146fe995e8e

[ADJUDICATION-FENCED] 4d8751ef185dc4d2fb837a859fab0146fe995e8e fenced=1 flagged=0
UPHOLD-FENCED F1 src/kiro_crew/dashboard/chat_turn/file_changes.py:659 -- A Codex shell rawInput with a list-valued command (the normal shape, per test/fixtures/acp_frames/codex/session.jsonl:6) hits an unguarded frozenset membership test and raises TypeError on an ordinary path, so no extreme-condition rarity argument exists.
[GPT-ADJUDICATED-FENCED] 4d8751ef185dc4d2fb837a859fab0146fe995e8e

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

@buluoray
buluoray force-pushed the fix/cross-slot-pending-strreplace-17619 branch from 5417e3b to 43e6d9a Compare October 8, 2026 07:29
@buluoray

buluoray commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Review disposition for 43e6d9a27f (both findings were real; fixed, not overridden).

  • GPT 6.1 BLOCKING, file_changes.py:545, writer histories unbounded per path: fixed. _PathWriteLog now caps writers per path at _MAX_TRACKED_WRITERS_PER_PATH (64), dropping the oldest record. Each drop raises the same eviction watermark as a path eviction, so every turn that began before it withholds resolution. Test: test_writers_per_path_are_bounded_and_dropping_one_fails_closed (red with the cap loop disabled: assert 5 == 3).
  • GPT 6.1 BLOCKING, file_changes.py:555, and Opus 5.5 advisory, file_changes.py:553, a path re-created after eviction bypasses the guard: fixed. foreign_since checks the eviction watermark before it looks the path up, so a re-created entry holding only the caller is never trusted. Test: test_a_path_recreated_after_eviction_still_counts_as_foreign (red with the old lookup-first order).
  • Also fixed, same class: the slot token was id(slot), which a collected slot's address could alias. A token is now minted once from the log and kept on the slot (_ChatSlot._path_write_token). Test: test_slot_tokens_are_minted_once_and_never_reused (red when the helper returns id(slot)).

Local, targeted: pytest -n0 test/test_file_change_snapshots.py test/test_chat_runner_composition_contract.py test/test_crew_recency.py gives 390 passed. black, isort, flake8, mypy and the comment-history gate are clean.

@buluoray
buluoray force-pushed the fix/cross-slot-pending-strreplace-17619 branch from 43e6d9a to a9b09ee Compare October 8, 2026 07:51
@buluoray

buluoray commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Review disposition for a9b09ee39f (both findings on 43e6d9a27f were real; fixed, not overridden).

  • GPT 6.1 BLOCKING, file_changes.py:570: an evicted record that was still OPEN became invisible to turns begun after the eviction. Fixed. Evicting or dropping an open record now adds its slot to _lost_open, and every other slot's turn treats every path as foreign until that slot's turn closes (close() discards it). Test: test_evicted_open_write_stays_foreign_until_its_turn_closes. It goes red when an open record is treated like a closed one, and red when close() keeps the slot in the set.
  • Opus 5.5 advisory, file_changes.py:551: every eviction pinned the watermark to the current sequence number, so a long-lived process lost resolution for good once the log filled. Fixed together with the item above. A closed record now raises the watermark only to its own write's sequence number, which is the only window it can affect. Test: test_evicting_an_old_closed_write_leaves_later_turns_resolvable. It goes red with the old self._evicted_seq = self._seq.

Local, targeted: pytest -n0 test/test_file_change_snapshots.py test/test_chat_runner_composition_contract.py test/test_crew_recency.py gives 392 passed. black, isort, flake8, mypy and the comment-history gate are clean.

@buluoray
buluoray force-pushed the fix/cross-slot-pending-strreplace-17619 branch from a9b09ee to 4d8751e Compare October 8, 2026 08:15
@buluoray

buluoray commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Review disposition for 4d8751ef18 (both GPT 6.1 findings on a9b09ee39f were real; fixed, not overridden).

  • F1, file_changes.py:586: closing an overlapping turn kept the record's request-time sequence number, so a turn that began after the request but before the late approval stopped seeing it. Fixed. close() now re-stamps the slot's records with the close's sequence number. Closing a slot whose open records were already forgotten raises the eviction watermark to the close. Tests: test_a_write_requested_before_this_turn_and_closed_inside_it_counts, and test_closing_a_slot_with_forgotten_open_writes_fails_closed_for_running_turns. Each goes red with its line removed.
  • F2, file_changes.py:687: a write could be on disk while its slot was still awaiting _snapshot_write_target, with no record yet. Fixed. Both to_thread call sites in _run_chat are wrapped in _begin_write_snapshot / _end_write_snapshot (try/finally, write tools only). An in-flight slot counts as foreign for every other slot until it records. Tests: test_an_in_flight_write_snapshot_counts_until_it_ends and test_only_write_tools_are_marked_in_flight. Both go red with the in-flight check removed.

Residual, stated in the PR body and the spec rather than claimed fixed: an auto-approved write that kiro-cli lands before its tool-call event reaches the dashboard has no record anywhere until that event arrives. A flush inside that gap cannot see it. The gap is bounded by ACP event latency, which I did not measure on this host.

Local, targeted: pytest -n0 test/test_file_change_snapshots.py test/test_chat_runner_composition_contract.py test/test_crew_recency.py gives 396 passed. black, isort, flake8, mypy, the comment-history gate and the sync-io-in-async gate are clean.

@buluoray

buluoray commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

First Principles CONCERNS on 4d8751ef18 (per-path log is oversized; a per-slot log would do): I considered this and kept the per-path log, for a reason the review could not see from the diff.

A per-slot log makes ANY write by ANY other session during the turn keep the fragment. That would not be an occasional false positive: on a host running several agent sessions at once, the case this deferral exists for, concurrent writes to different files are the normal state. The deferred resolution from #17367 would then almost never settle, and those users would be back to the -1 +N chip #11387 reported. The global in-flight check is different in kind. It lasts only while one snapshot is read off the event loop (one file read), not for the length of another session's turn, so it does not give up per-path precision in practice. I have not measured the concurrent-write rate, so this rests on the deployment shape rather than a number.

If the owner prefers the smaller per-slot shape and accepts that trade-off, it is a straight subtraction, and I will make it on request.

…red strReplace path

The writer-id gate that guards a deferred strReplace "before" only sees writes
recorded on its own slot. A second session making the same edit on the same
file inside the turn window could leave the turn-end read equal to the forward
substitution, so the flush settled an intermediate state as the before.

Every recorded write is now also noted on a process-wide per-path log keyed by
slot. _run_chat marks the turn start, the flush closes the slot's paths at turn
end, and resolution is skipped for a path another slot wrote after this turn
began or still holds open in its own unfinished turn. The log is bounded and an
eviction fails closed.

Closes #17619
@buluoray
buluoray force-pushed the fix/cross-slot-pending-strreplace-17619 branch from 4d8751e to 2d98b18 Compare October 8, 2026 08:36
@buluoray

buluoray commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Review disposition for 2d98b18f33. The GPT 6.1 finding on 4d8751ef18 was real, introduced by this PR, and is fixed, not overridden.

  • file_changes.py:659, an argv-list command raised TypeError: _begin_write_snapshot tested raw_params.get("command") for membership in the _WRITE_COMMANDS frozenset without checking its type. A Codex shell call carries command as a list, which is unhashable, so every such tool call would have ended the turn. Fixed: the command must be a str before the membership test, the same guard _snapshot_write_target already applies. Regression: test_only_write_tools_are_marked_in_flight now passes {"command": ["bash", "-lc", "echo acp-replay-corpus"]}. It fails with TypeError: unhashable type: 'list' once the type check is removed.

Enumerated: _begin_write_snapshot is the only new reader of raw_tool_params in this diff. The two to_thread call sites hand the same value to _snapshot_write_target, which already type-checks it.

Local, targeted: 396 passed across test_file_change_snapshots.py, test_chat_runner_composition_contract.py and test_crew_recency.py.

@buluoray

buluoray commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Closing this draft without merging. Five review rounds each found a new concurrency edge case in the write log this PR adds (watermark, open-write tracking, close re-stamp, in-flight mark, and now a cancelled snapshot clearing its in-flight mark before the write is recorded). The defect it fixes is rare and cosmetic: a wrong diff in the file-change chip, never a change to files on disk. About 200 lines of concurrency code is not worth that, so the recommendation is to keep #17619 as a documented limitation. The branch is kept; this can be reopened if the owner disagrees.

@buluoray buluoray closed this Oct 8, 2026
@github-actions github-actions Bot removed the readiness: checking Automated validation is still running label Oct 8, 2026
@bolichen97
bolichen97 deleted the fix/cross-slot-pending-strreplace-17619 branch November 6, 2026 07:21
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 chip: slot-local writer-id gate misses a concurrent cross-session byte-identical strReplace

1 participant