Skip to content

fix(website): edit a picked file mention as one atomic unit (#14675) - #17353

Closed
chenmingwei23 wants to merge 2 commits into
kirodotdev:mainfrom
chenmingwei23:fix/14675-v2
Closed

chenmingwei23 wants to merge 2 commits into
kirodotdev:mainfrom
chenmingwei23:fix/14675-v2

Conversation

@chenmingwei23

@chenmingwei23 chenmingwei23 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Editing a picked file mention no longer silently drops the file from the message.

After #6511, picking a file into the composer stages a chip and inserts @src/main.ts into the text. Hand-editing the mention broke the pair: one Backspace at the end turned it into @src/main.t, the chip silently unstaged (the spelling no longer matched a recorded alias), but the half-written reference was left in the composer and sent as plain prose — the agent received a message naming a file with no attachment.

Why it matters

The chip vanishing is easy to miss while typing, so the user sends a message that points the agent at a file it never received. It happens on every hand-edit of a mention, so it is frequent, silent, and lasting friction.

Not a goal

  • Making a mention atomic in the opt-in Lexical composer. This change covers the default <textarea> composer (the surface the bug reproduces on and the one the #6511 tests exercise); the Lexical editor already handles tokens as editor nodes on a separate path.
  • Changing the existing "exact-or-nothing" reconciliation that unstages a chip when its alias stops matching — that stays; this only stops a half-reference being left behind.

What changed (motivation → approach → change)

  • Symptom: one keystroke left @src/main.t in the text while the chip unstaged.
  • Root cause: a picked mention was plain text with no keystroke-level handling, so Backspace/Delete edited it one character at a time while the chip followed a separate list.
  • Change: a picked mention is now one atomic unit. A new pure mentionSpanAt(text, at, forward, aliases, winShaped) in fileTokens.ts returns the span of the mention on/adjacent to the caret, and handleMentionKey (wired into useComposerKeyDown after the paste-token atom, IME-safe) removes the whole span through the composer's own onChange; the existing reconciliation then unstages the chip. The span is found through the shared mentionTokenRegex and mentionBoundaryFor — the same grammar the chip ✕ (removeFileChip) and the reconciliation already use — chosen over a second hand-rolled definition (First Principles review) so the three can never disagree. A recorded @a.ts is therefore never matched inside a longer hand-typed word @a.tsx, and a leading ( is consumed only when its matching ) follows the mention (GPT/Opus review).

Backwards compatibility

Compatible: adds a keystroke handler on recorded picked-file mentions in the default composer; a mention with no recorded alias, a folder token, and every other key fall through to the native edit exactly as before. Removes nothing: no record, validator, key, or public function is narrowed or deleted — mentionSpanAt is a new export, and onMentionKey is a new optional prop.

Tests

  • fileTokens.test.ts — 8 unit cases on the pure mentionSpanAt: Backspace-at-end, Delete-at-start, caret-inside, the @a.tsx boundary rejection, the :line suffix, the wrapper-pairing (both (@a.ts:42) consumed and (@a.ts + y)'s ( kept), null when no mention is at the caret, and longer-sibling preference.
  • ChatPage.dirStaging.test.tsx — 8 component cases through the real composer stack: Backspace at end / inside, Delete at start, wrapped (@a.ts:42), a staged sibling left untouched, native fall-through when no mention is at the caret, the @a.tsx boundary case, and the unbalanced-paren case.
  • Full suites green: dirStaging (74), fileTokens (106), ChatInput (176), ChatInput.paste (30). tsc and eslint src clean.

Manual verification

Captured headlessly via website/scripts/capture-mention-atomic-delete.mjs, which drives the shipped pure mentionSpanAt in Chromium and asserts the before arm reproduces the bug and the after arm fixes it. See Screenshots.

Screenshots / video

Before — one Backspace leaves a @src/main.t half-reference while the chip unstages (the bug):

before: picked mention and chip
before: after one Backspace, half-reference left

After (this PR) — one Backspace removes the whole @src/main.ts and the chip together, no half-reference:

after: picked mention and chip
after: after one Backspace, whole mention gone

Related Issues

Fixes #14675

Pattern harvest

Rule candidate: review-prompt
Pattern: an atomic-delete / token-span scan that uses a raw indexOf/substring match instead of the module's shared boundary matcher silently claims text inside a longer word and consumes an unmatched wrapper — reuse the one boundary grammar rather than re-deriving it.

Checklist

  • At most two commits (one here), with a Conventional Commits title (fix: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable) — N/A, no user-facing docs affected
  • No secrets, credentials, or internal references in the diff

@chenmingwei23
chenmingwei23 requested a review from a team as a code owner October 6, 2026 11:27
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Oct 6, 2026
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ✅ PASS

UX-level review of 56528d780781de1a857a3601fa56102d47e01c51 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

UX-Verdict: PASS

Check by hand in the real composer: after Backspace removes a mention, the caret should sit where the mention began. Undo (Ctrl+Z) should also restore it.

The screenshots are headless harness captures. They show the before and after text states, not the real composer.

[UX-REVIEWED] 56528d7

Description read

[DESCRIPTION-READ] 0d22094e36d37834cf54d229e9d1cfb1d03ae2a3dc76f9d8eeaf09be546014e9

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

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 56528d780781de1a857a3601fa56102d47e01c51 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Nothing to check.

[DESIGN-REVIEWED] 56528d7

Description read

[DESCRIPTION-READ] cbb4c2facfb3056389dd853829ced92d567fef68d7fbd972781b0836fd011138

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

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of 56528d780781de1a857a3601fa56102d47e01c51 via the fork AI-review pipeline — 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

mentionSpanAt is a third spelling of the mention grammar. The PR claims it is "shared" with the reconciliation and the chip ✕, but the diff changes neither.

Not justified as shipped

  • Item 4 — duplicate: the description says the span grammar is "the same grammar the chip ✕ (removeFileChip) and the reconciliation already use", and the fileTokens.ts doc comment says it is "SHARED with the reconciliation staleness check and the remove-chip strip". The diff only adds mentionSpanAt. removeFileChip still builds its own RegExp ((^|\\s)${esc}(?: |(?=\\s)|$)). The reconciliation call sites are untouched, so wrapper pairing and :line handling now live in two places that can diverge.
    Subtraction: delete the "SHARED with…" claims from the comment and description, or point removeFileChip at mentionSpanAt and delete its inline regex.
    Clears when: removeFileChip calls mentionSpanAt, or the "shared" wording is removed.
  • Item 6 — oversized: the block-comment head above mentionSpanAtCaret (the "Returns true when it consumed the key…" paragraph) is stale. The "Returns the resulting text and caret…" paragraph is pasted twice. The capture entry website/capture/mention-atomic-delete.tsx reimplements the composer's Backspace/Delete logic, including space collapsing, which duplicates handleMentionKey. It tests a copy, not the shipped handler.
    Subtraction: drop the duplicated and stale comment paragraphs, and drop the reimplemented key handler from the capture entry. Also drop the four committed PNGs if ChatPage.dirStaging.test.tsx already covers the behavior.
    Clears when: the duplicate comment paragraphs and the capture's hand-copied handler are removed.

What this change ships

Inventory (7 items) — 5 justified

Intent: stop a hand-edit of a picked @file mention from leaving a half-reference with no chip. FIX.

  1. Backspace or Delete on or next to a staged mention removes the whole mention and its chip — justified
  2. Wrapper parens and :line suffix go with the mention — justified
  3. Trailing or leading space collapses after the removal — justified
  4. New exported mentionSpanAt grammar function — duplicate of removeFileChip's inline regex in composerFileMentions.ts
  5. New optional onMentionKey prop, wired from ChatPage through ChatInput into useComposerKeyDown — justified
  6. Capture page, script and four PNGs committed — oversized; the capture entry copies the handler rather than driving the shipped one
  7. Unstaged, unrecorded and folder mentions still delete natively — justified

[FIRST-PRINCIPLES-REVIEWED] 56528d7

Description read

[DESCRIPTION-READ] 3d7dbf0673e724ee9f9eb43892c3ab2b5aa44ce38daf0de8896e93b7a2702cde

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

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed 56528d780781de1a857a3601fa56102d47e01c51 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 56528d7

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — 🔴 changes requested (blocking)

Reviewed 56528d780781de1a857a3601fa56102d47e01c51 via the fork AI-review pipeline; updated in place on each push.

1 of 1 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

BLOCKING -- website/src/components/chat-input/keyboard.ts:164 -- First mention deletion after an identical-draft slot switch cannot be undone (origin: validation)
onChange(result.value)
Switch between slots with identical staged @a.ts drafts -> the first atomic Backspace reaches useUndoHistory without the user-edit flag -> its pending slot-settling branch replaces the undo history with the deleted text, so Ctrl+Z cannot restore the mention.
Anchor: residual/crash-data-loss-corruption
Fix: Pass valueFromUserRef into useComposerKeyDown and set it to true before applying a recognized mention edit.
[BLOCK-MERGE] 56528d7
[GPT-REVIEWED] 56528d7

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

No findings were open for adjudication. I flagged the one fenced finding, F1. A flag doesn't stop it blocking: it still needs an /ai-review override from a repository writer.

F1 (fenced): the defect is real, but the harm is closer to LOW than to "data loss".

  • Conditions confirmed:

    • The new mention-delete path calls onChange(result.value) without setting the user-edit flag (diff, keyboard.ts:+321). Typed edits do set it, through useEditorInput.
    • A slot switch reseeds the undo history and arms the slot-settling flag (draftHistory.ts:160-164).
    • When the restored draft is identical to the old one, value doesn't change. The effect, which only re-runs when value or autoFocusKey change (draftHistory.ts:218), stays idle and the flag stays armed.
    • The next change, the atomic Backspace, then hits !fromUser, and the history is reseeded at the post-delete text (draftHistory.ts:166-176).
  • Rarity: it needs two slots with byte-identical drafts, each containing a staged mention, and the user's very first keystroke after switching has to be a Backspace or Delete on that mention.

  • Outcome: a few characters are removed in plain view, and only Ctrl+Z fails. Nothing is silently corrupted. The undo-based recovery path is "none", but the user can re-pick the file in the @ picker.

    [ADJUDICATION] 56528d7 total=0 uphold=0 downgrade=0
    [GPT-ADJUDICATED] 56528d7

    [ADJUDICATION-FENCED] 56528d7 fenced=1 flagged=1
    FLAG F1 website/src/components/chat-input/keyboard.ts:164 -- Needs byte-identical drafts in two slots, each with a staged mention, plus a mention delete as the very first keystroke after the switch (draftHistory.ts:160-176,218); the loss is a visible deletion of a few characters, fixed by re-picking the file in the @ picker, so it is not silent corruption and its harm is not unbounded.
    [GPT-ADJUDICATED-FENCED] 56528d7

🏷️ Fenced finding(s) machine-flagged as likely edge case

The security fence keeps these findings blocking regardless of adjudication; the only clearance path is a human override recorded by a repository writer, who must independently verify a rationale before recording it — it is machine-authored, and a wrong override on a security-class finding ships exactly the class the fence exists to stop. (This lane's comment deliberately carries no override command.)

  • F1 website/src/components/chat-input/keyboard.ts:164 — Needs byte-identical drafts in two slots, each with a staged mention, plus a mention delete as the very first keystroke after the switch (draftHistory.ts:160-176,218); the loss is a visible deletion of a few characters, fixed by re-picking the file in the @ picker, so it is not silent corruption and its harm is not unbounded.

@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 6, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 6, 2026
@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 6, 2026
…ev#14675)

What broke: after kirodotdev#6511, hand-editing a picked `@src/main.ts` mention
broke the chip/text pair. One Backspace at the end turned it into
`@src/main.t`; the reconciliation unstaged the chip (the spelling no
longer matched a recorded alias), but the half-written reference was left
in the composer and sent as plain prose naming a file with no attachment.

What changed: a picked file mention is now ONE atomic unit on a hand
edit. A Backspace or Delete on or adjacent to a recorded `@mention`
removes the whole mention and its chip together, so a half-edited form
can never exist. This mirrors the collapsed-paste token, which already
deletes as a unit (chat-input/paste.ts handleTokenKey).

- fileTokens.ts: new pure `mentionSpanAt(text, at, forward, aliases,
  winShaped)` returns the span of the mention on/adjacent to the caret.
  It is the ONE grammar shared by the keystroke, the reconciliation
  staleness check and the remove-chip strip (fork First Principles
  review: no second span definition). Occurrences are found through the
  shared `mentionTokenRegex`, not a raw `indexOf` scan, so a recorded
  `@a.ts` is never matched inside a longer hand-typed word `@a.tsx`
  (fork GPT/Opus review: a substring scan would carve it out and strand
  an `x`). A leading opening wrapper joins the span only when its matching
  closer follows the mention, so a lone `(` before an unwrapped mention
  stays and no unbalanced `)` ships (fork GPT/Opus review).
- composerFileMentions.ts: `handleMentionKey` (pure, returns
  `{ value, caret } | null`) calls the shared `mentionSpanAt`.
- keyboard.ts / props.ts / ChatInput.tsx / ChatPage.tsx: wire
  `onMentionKey` into useComposerKeyDown, after the paste-token atom and
  skipped while the IME is composing. The edit is applied through the
  composer's own onChange and the caret lands where the mention began.

Scoped to recorded aliases and the default textarea composer: a typed or
draft-restored `@rel` with no bookkeeping, and a folder token, fall
through to the native per-character edit.

Tested: 8 cases in ChatPage.dirStaging.test.tsx (Backspace at end/inside,
Delete at start, wrapped file:line, sibling untouched, native
fall-through, the `@a.tsx` boundary case, and the unbalanced-paren case)
and 8 unit cases on the pure `mentionSpanAt` in fileTokens.test.ts.
Full suites green: dirStaging (74), fileTokens (106), ChatInput (176),
ChatInput.paste (30). tsc and `eslint src` clean. Screenshot evidence
(before: half-reference left; after: whole mention removed) committed
under temp-screenshots/ and produced by scripts/capture-mention-atomic-
delete.mjs over the SHIPPED pure function; the before arm is asserted to
reproduce the bug.

Fixes kirodotdev#14675
@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 6, 2026
@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 6, 2026
…eardown flake in test_dashboard_server_coverage, not in this frontend-only diff)
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 7, 2026
@chenmingwei23

Copy link
Copy Markdown
Contributor Author

Superseded by #17929, which rebuilds this as a minimal fix (6 files) with a reproduction that fails on current main and passes with the change. Closing this one in favour of #17929.

@github-actions github-actions Bot removed the readiness: checking Automated validation is still running label Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Editing a picked file mention drops the file from the message

1 participant