Skip to content

fix(chat): fork the native claude conversation when a message is edited - #19871

Merged
bolichen97 merged 1 commit into
mainfrom
replace/18319
Oct 13, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
replace/18319

Conversation

@iamwhatever

Copy link
Copy Markdown
Contributor

Replaces #18319 by @rinbn, rebased onto current main as a same-repo branch so CI runs with repository secrets. The commit keeps @rinbn as author.

Problem / Motivation

Goal: Editing a message on the Claude backend keeps the conversation the model had up to that message, tool results included, instead of shrinking it to a text replay.

Edit + Send (POST /api/chat/slots/{slot}/rewind) on a Claude-backed session throws the native Claude Code conversation away. The replacement turn starts a new native session whose only view of the retained history is the text session replay: user and assistant text, at most 80k characters, no tool calls and no tool results. After an edit the model no longer knows anything it had only seen in tool output, such as file contents and command results, and on a long session most of the context is gone. Nothing tells the user.

Why it matters

Every dashboard user on the Claude backend who edits a message loses context silently, and the loss grows with the session: a session holding a few hundred thousand tokens comes back with at most 80k characters of text, even when the edit is to the latest message and that message was stopped before any reply. The answers that follow are then built on a model that has forgotten the tool output earlier turns relied on.

Not a goal

What changed (motivation → approach → change)

After this change, editing a message on the Claude backend leaves the model with its native conversation up to that message, tool calls and results included, instead of a text replay of at most 80k characters.

The rewind discarded the native session because that is the only boundary kiro-cli offers. claude-agent-acp can do better: it implements session/fork with a fork point, which calls the Agent SDK's forkSession(sessionId, { upToMessageId }). That copies the stored transcript up to and including one assistant message into a new session and leaves the source untouched. So on Claude the rewind now forks instead:

  • Recording the fork point. Text chunks carry the adapter's messageId as AcpEvent.message_id (acp/types.py), read on both the direct-client and shared-runtime paths (acp/client.py, acp/_dispatch.py) and copied through providers/acp.py. Each assistant segment row stores it as meta.native_message_id (dashboard/chat_runner.py, dashboard/state.py).
  • Arming the fork. After the edit commits, rewind (dashboard/chat_rewind.py) takes the retained prefix's last assistant row, skipping runtime notices and wire-only rows (the done every finished turn leaves in the live window), and stores native_fork = {sid, message_id} on the session-map entry (session_map.py). Any write or clear of the entry's sid drops it, so a reset issued before the next turn wins.
  • Running it. The next cold start (session_allocation.py) treats a pending fork like a resume: it stays off the warm pool, keeps the stored cwd, and refuses to run on a speculative start. It hands the fork to the Claude client and drops it from the map. A temporary or incognito session, which never restores native history, drops it without forking. The client sends session/fork, then runs session/load on the fork, so the turn takes the ordinary resumed path with no replay.
  • Who may fork. The fork point travels in claude-agent-acp's own _meta.jetbrains.air.fork key, and a fork that ignored it would copy the edited-away suffix too. So the client forks only for a backend in the new ACP_BACKENDS_FORK_AT_MESSAGE set (claude; agent_sdk/backends.py, re-exported by acp_backends.py, classified off-card in agent_sdk/backend_cards.py and listed in harness-onboarding.md), at or above CLAUDE_ACP_FORK_POINT_MIN_VERSION (0.71.0), and only when the handshake advertised sessionCapabilities.fork. 0.70.0 advertises fork and copies the whole session; 0.71.0 cuts at the point (both measured, see Manual verification). The floor is checked with the existing _adapter_version_at_least.
  • Fallback. Anything else, including a row with no id or a fork error, leaves the start on session/new plus replay, as before. The adapter answers -32602 for an unknown fork point.

Recorded frames from every adapter already carry messageId, so the frame-replay snapshots gain message_id lines and nothing else.

Backwards compatibility

Compatible: every new field (AcpEvent.message_id, row meta.native_message_id, session-map native_fork) is optional and empty by default, and a row or entry without one takes today's replay path.

Removes nothing: the change adds fields and a path that falls back to the existing behavior; no field, key, endpoint or check is removed or tightened.

Tests

test/test_rewind_native_fork.py:

  • parse_message_id reads a chunk's id and rejects missing, non-string and over-long ones.
  • The runtime text event carries the id.
  • A segment row records the id once, and the next segment does not inherit it.
  • The session map round-trips native_fork through disk, and drops it on set, on clear_sid (even with no sid), and on clear_native_fork.
  • The Claude handshake sends session/fork with the fork point and then session/load on the fork. Each of these starts fresh instead: a missing capability, an adapter below 0.71.0, a failed fork, and a non-Claude backend (DeepSeek advertising fork).
  • A cold start hands the fork to a Claude client and adopts the forked sid. It drops the fork on another backend and on a temporary session.
  • _native_fork_point over compaction, recycle and session-reload notices, a wire-only done row, a trailing tool row, a trailing user row, a row with no id, and an empty prefix.
  • The rewind endpoint arms the fork when the edited message follows an assistant row and a done row, which is the live window a finished turn leaves. It does not arm when the edited message is the first one.

Other test changes:

  • test/fixtures/acp_frames/**/*.expected.json were regenerated with scripts/update_acp_frame_snapshots.py; the only change is the added message_id lines.
  • test_post_compaction_continuation.py now pins the notice flag on the forwarded chunk with a regex, so the extra keyword argument on that yield no longer breaks it.

Verification

On this replacement (main ac8a96a24 + cherry-pick). One conflict in src/kiro_crew/acp/client.py: main added CLAUDE_ACP_STEERING_MIN_VERSION and its own _adapter_version_at_least / steering_advertised. Kept main's helper and added CLAUDE_ACP_FORK_POINT_MIN_VERSION beside it; no behaviour change from the fork's head.

  • test/test_rewind_native_fork.py on base: ImportError (parse_message_id). With this PR: 27 passed.
  • test_rewind_native_fork.py + test_post_compaction_continuation.py + test_acp_frame_replay.py: 125 passed.
  • isort, flake8, black on changed files and mypy src/kiro_crew/: clean.
  • Not re-run here: no claude-agent-acp binary is installed on this host, so neither the fork path nor the session/new fallback was run against a live adapter for this replacement. The fork point rides the vendor _meta.jetbrains.air.fork field. The live runs below are the original author's, on fix(chat): fork the native claude conversation when a message is edited #18319's head.

Original author's manual verification

End to end on this build. A dev gateway from this branch (KIROCREW_PROFILE=standalone, an empty KIROCREW_HOME with only agent.acp_backend: claude, claude-agent-acp 0.87.0), driven through the dashboard API. Edit + Send went through POST /api/chat/slots/{slot}/rewind with {ts, content}, the same call the UI makes.

  • Turn 1: "Use the Read tool to read secret.txt … reply with exactly one word: done". The file says ZEBRA-4417. The reply was done, and the row carried meta.native_message_id.
  • Turn 2: "my favorite color is MAGENTA" → noted.
  • Edit turn 2 into "Answer from memory only. Do not use any tools. (a) the vault code in secret.txt? (b) my favorite color? …". The rewind answered at_message_index: 4, because the live window held turn 1's done row.
  • The answer was a=ZEBRA-4417; b=UNKNOWN. The text replay could not have produced (a): turn 1's only assistant text is "done".
  • The session map moved sid from 0515ca67… to 4c930cb0…, kept 0515ca67… as discarded_sid, and dropped native_fork.
  • The forked transcript holds turn 1's tool_result and no MAGENTA. The original transcript still holds turn 2, unchanged.
  • The prompt the forked session received after the edit opens with the slim [SESSION RESUMED …] header (9,147 chars), with no [SESSION CONTEXT replay block. So the fork is not followed by a second, text copy of the history.

The adapter, directly over ACP, outside Kiro Crew. Each probe used the same S1: turn 1 read secret.txt with the Read tool (content ZEBRA-9182, reply only "done"), turn 2 said "my favorite color is MAGENTA". S1 was then forked at turn 1's messageId and both sessions were asked without tools.

Adapter Advertises fork The fork's answer Fork point
0.70.0 yes a=ZEBRA-9182; b=MAGENTA ignored: the edited-away turn came along
0.71.0 yes a=ZEBRA-9182; b=UNKNOWN honoured
0.87.0 yes a=ZEBRA-9182; b=UNKNOWN honoured; same answer from a fresh adapter process with no in-memory message map

On every version S1 itself answered a=ZEBRA-9182; b=MAGENTA, and its transcript was unchanged by the fork. An unknown fork point on 0.87.0 returned -32602 Invalid params: Fork point message ... was not found. Grepping the published npm tarballs agrees with the table: _meta.jetbrains.air.fork first appears in 0.71.0 (dist/fork-session.js); 0.70.0 and earlier have only a whole-session fork. That is where CLAUDE_ACP_FORK_POINT_MIN_VERSION comes from.

Related Issues

Fixes #18298
Replaces #18319

Related: #14512, #8067, #5395.

Pattern harvest

Not generalizable: the loss came from one endpoint using the only boundary kiro-cli offers on every backend, not from a code shape that recurs elsewhere.

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

Edit + Send on the claude backend discarded the native session and
primed a fresh one with the text replay, which carries no tool calls or
tool results and stops at 80k characters. The claude adapter can fork a
stored session up to a given assistant message, so the rewind now arms
that fork at the retained prefix's last assistant message and the next
cold start restores the fork like any resume. Anything that keeps the
fork from running falls back to the replay.

Closes #18298
@iamwhatever
iamwhatever requested review from a team and KiroCrewCaptain as code owners October 13, 2026 18:21
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 5f7c4463f9b7692c40e1eb263ffd236c9da42c08 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

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 5f7c4463f9b7692c40e1eb263ffd236c9da42c08 — 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: PASS

This rebased head never ran against a live claude-agent-acp, so the _meta.jetbrains.air.fork cut has only been checked on #18319's head.

What this change ships

Inventory (8 items) — 8 justified

Intent: when a user edits a message on the Claude backend, the model keeps its native history up to that point, tool results included. This is a FIX.

  1. On Claude, Edit + Send continues from a fork of the native conversation cut at the last kept assistant message, not an 80k-char text replay — justified
  2. Each assistant transcript row now stores the adapter's message id (meta.native_message_id) — justified
  3. A pending-fork record (native_fork) is saved on the session-map entry, and any sid write or clear drops it — justified
  4. A cold start with a pending fork skips the warm pool, reuses the stored cwd and refuses a speculative start — justified
  5. Temporary or incognito sessions drop a pending fork and start fresh — justified
  6. Forking is limited to ACP_BACKENDS_FORK_AT_MESSAGE (claude), adapter ≥0.71.0 and an advertised fork capability — justified
  7. A fork that fails, or a row with no id, falls back to today's replay — justified
  8. Recorded frame snapshots gain message_id lines, and one source-text test pin moves to a regex — justified

[FIRST-PRINCIPLES-REVIEWED] 5f7c446

Description read

[DESCRIPTION-READ] 473328ef29eecc5f1baab7a21f5021e4cfca28f0edd6f3140e578c34f0431366

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

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — 🟡 CONCERNS

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

Design-Verdict: CONCERNS

Whether the fork is correct rests on claude-agent-acp's private _meta.jetbrains.air.fork key. Only a minimum version guards it, and nothing checks the result after the fork.

Watch

  • Silent suffix leak if the adapter changes. The diff sends the fork point as "_meta": {"jetbrains": {"air": {"fork": {"version": 1, "messageId": message_id}}}}, a vendor key that is not part of the ACP protocol. The only check is _adapter_version_at_least(..., (0, 71, 0)), with no upper bound. A later claude-agent-acp could rename or drop the key. That version would still advertise sessionCapabilities.fork and would just ignore an unknown _meta field. The fork would then copy the whole session, including the edited-away turns. The user edits a message, and the model still remembers what the user meant to erase. No error, log line or test shows it, because session/load on the fork succeeds. This is the same failure the 0.70.0 measurement found, coming back unnoticed. You can still undo this, but no one would notice it had happened.
    Clears when: after session/load on the fork, the code confirms the cut. For example, it checks that the restored history's last assistant id equals message_id, or it requires the adapter to echo the fork point, and falls back to session/new plus replay when the check fails. Another option is to pin a version range the author has verified, plus a frame-replay fixture from a live adapter that fails when the key stops being honoured.
  • Not checked against a live adapter on this revision. By the PR's own account, neither the fork path nor the fallback was run on this rebased head ("no claude-agent-acp binary is installed on this host"). The only live evidence is from fix(chat): fork the native claude conversation when a message is edited #18319's head, and this rebase resolved a conflict in acp/client.py.
    Clears when: one end-to-end Edit + Send on the Claude backend at this head, with the session/fork → session/load frames and the absence of a text replay recorded in the PR.

[DESIGN-REVIEWED] 5f7c446

Description read

[DESCRIPTION-READ] 6de1ec59f51513f4cddc2548ebba44636039bdc90e447b0d6579f7e7fa469336

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

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 5f7c446

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

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 5f7c446

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

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

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 13, 2026

@bolichen97 bolichen97 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.

Reviewed the diff: the change matches its stated goal and tests cover it. Readiness passed at this head.

Posted by an AI assistant at bolichen97's request.

@bolichen97
bolichen97 merged commit 4616bcf into main Oct 13, 2026
101 checks passed
@bolichen97
bolichen97 deleted the replace/18319 branch October 13, 2026 23:23
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 13, 2026
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.

Editing a message on the Claude backend discards the native conversation; only a text replay survives

3 participants