Skip to content

refactor(chat): move the queued-send stash into its own module and make its write race-safe - #22480

Closed
rnoack1 wants to merge 1 commit into
kirodotdev:mainfrom
rnoack1:split/queued-send-stash-module
Closed

rnoack1 wants to merge 1 commit into
kirodotdev:mainfrom
rnoack1:split/queued-send-stash-module

Conversation

@rnoack1

@rnoack1 rnoack1 commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Cancelling a queued message puts back exactly what the user sent, attachments included, even when the server's broadcast beat its own receipt or the message was edited while queued.

Split out of #6825: the queued-send stash, carved apart from the persisted recovery store because it is a separate in-memory mechanism.

The queued-send stash holds what the composer looked like at send time, so a cancel can restore it. Two paths leave it wrong today:

  • queue_push can arrive before the HTTP receipt for the same send. The receipt's write then overwrites the adopted record's sent (the server's broadcast text) with the sender's un-redacted copy. The cancel guard compares sent against the card, the comparison fails on a redacted entry, and cancel falls back to the parser.
  • An edit keeps the entry's queue id but changes its text, so the stashed record describes text the queue no longer holds. The same guard fails and cancel again falls back to the parser.

The parser cannot recover an attachment path that contains a space, and it never had the attachments of a redacted entry, so in both cases the cancel restores the text without the files the user attached.

Why it matters

Cancelling a queued message is how a user takes it back to fix it. Losing the attachments on that path means the user has to find and re-attach files they already attached, and may not notice they are gone before resending.

Not a goal

  • Persisting the stash across a reload. That is the persisted recovery store, a separate unit of fix(chat): keep the optimistic user bubble across a slot refetch #6825.
  • The pre-send stash keyed by sendId. It can only be adopted once queue_push carries sendId, which is a separate decision, so it is left for a later change.
  • Edit-settlement bookkeeping for a rejected edit. That belongs with the change that surfaces a rejected edit to the user.

What changed (motivation → approach → change)

  • Own module. The store moves from hooks/useQueuedMessageActions.ts to utils/queuedSendStash.ts, so the chat slice can reach it later without importing the hook, which would close an import cycle. The hook re-exports every name, so all existing importers are unchanged. The record's current aliases and quote fields are carried over exactly.
  • Race-safe write. stashQueuedSend writes the receipt's record only when no record is stashed yet for that queue id. The three writers in components/useChatPaneSend.ts and pages/chat/page/composerSend.ts use it in place of a raw .set.
  • Edit re-stash. On a successful edit the hook calls restashEditedSend with the submitted text and the server's returned text. The record's raw becomes what the user submitted, its sent what the card shows, and its files drop any attachment the edit removed. The removal test checks both ends of a path mention, so /tmp/report.pdf is not kept alive by /tmp/report.pdf.bak.

Backwards compatibility

Compatible: every symbol the hook exported before is still exported from it with the same shape, and the record type is unchanged.

Removes nothing: the inline declaration in the hook is replaced by a re-export of the same store, so no reader loses access to it.

Tests

  • utils/queuedSendStash.pathBoundary.test.ts: an edit that drops an attachment prunes it from the stash, and a longer path sharing its prefix does not keep it alive.
  • The existing stash-consumer suites (useQueuedMessageActions.test.tsx, ChatPane.draftRecovery.test.tsx, ChatPageW3Coverage.test.tsx) pass unchanged, which pins that the move kept the store's contract: 104 of 104.

Wider sweep (src/utils, src/hooks, src/test/ChatPane*, src/test/ChatPage*, src/test/QueueStack*): 2734 of 2734. tsc --noEmit and eslint --max-warnings 0 are clean on every changed file.

Manual verification

N/A for this draft: the receipt race and the edit re-stash are both pure store transitions, covered by the unit suites above.

Screenshots / video

Why no screenshot: this moves an in-memory store and changes how it is written, and nothing on screen renders differently.

Related Issues

Part of #6825.

Pattern harvest

Not generalizable: the overwrite is specific to a store written from two independently-ordered sources, and the fix is local to its one writer.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • 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

…ke its write race-safe

A `queue_push` that beat its own HTTP receipt had its broadcast text overwritten, breaking cancel on
a redacted entry; an edit left the record describing text the queue no longer held.
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — 🟡 CONCERNS

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

Design-Verdict: CONCERNS

stashQueuedSend guards against a queue_push writer that doesn't exist on base, so half of the stated Goal fixes nothing in this tree.

Watch

  • The receipt-race claim has no code behind it. The description says the receipt "overwrites the adopted record's sent (the server's broadcast text)". On base, the only writers of queuedSendStash are the three receipt paths this patch converts (useChatPaneSend.ts:369, :485, composerSend.ts:776). Nothing on queue_push ever adopts a record. So if (!queueId || queuedSendStash.has(queueId)) return can never be hit by the race the PR describes: the first Goal clause ("even when the server's broadcast beat its own receipt") is not delivered. The guard also silently turns every later receipt-path write into a no-op, a change in behaviour that nothing exercises today. Its only tests are pathBoundary ones that never test the race. The guard is groundwork for the queue_push adoption in the other fix(chat): keep the optimistic user bubble across a slot refetch #6825 unit, not a fix.
    Clears when: the description and Goal stop claiming the receipt-race fix and say the first-writer-wins guard prepares for the future queue_push adopter; or the adopter lands in this PR with a test where queue_push stashes before the receipt and cancel still restores the attachments.

[DESIGN-REVIEWED] bf96918

Description read

[DESCRIPTION-READ] 041683120aa9421a957dfb77af971420e6b2f93b5d00d0734fa50fcb8200bd0b

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

UX Review (Opus 5.5, fork) — ✅ PASS

UX-level review of bf969181d0aff610aa95732447f3c2882e96e96f 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 that cancelling an edited queued message brings back its attachment chips in the composer. The unit tests only cover the stash records.

[UX-REVIEWED] bf96918

Description read

[DESCRIPTION-READ] 72e46c5d5d0d404f74bd0e41193f229efb4ebd86bd2f47fd58d344708212f5ed

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

First Principles Review (Opus 5.5, fork) — 🔴 BLOCK

Premise-level review of bf969181d0aff610aa95732447f3c2882e96e96f 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: BLOCK

The receipt-write guard in stashQueuedSend protects against a queue_push adopter that does not exist on base, so dropping it costs nobody anything.

Not justified as shipped

  • Item 2 — zero option: the description says the receipt "overwrites the adopted record's sent", but no queue_push path on base writes the stash.
    Subtraction: delete the queuedSendStash.has(queueId) guard and keep the plain .set, or defer it until a change adds an adopter.
    Clears when: the guard is removed, or a writer that adopts on queue_push exists on base.
  • Item 3 — inherited: the edit defect has no failing test on base. The base hook says a card with meta.files already "restores whole" when it contains a spaced path, and it chose "an edited entry — falls to the strict parser" on purpose.
    Clears when: a test that fails on base shows attachments lost on cancel after an edit.
  • Item 5 — zero consumers: nothing imports stashQueuedSend, restashEditedSend or QueuedSendRecord from the hook (0 hits for from '.*useQueuedMessageActions').
    Subtraction: shrink the hook's export {…} to queuedSendStash.
    Clears when: the extra re-exports are deleted.
  • Item 1 — inherited: the reason given is "so the chat slice can reach it later", and no slice consumer ships here. The title says refactor, but items 2–4 change behaviour.
    Clears when: the slice consumer lands with the move.

What this change ships

Inventory (5 items) — 1 justified

Intent: cancelling a queued message, even one that was edited, restores the text and attachments the user sent. FIX.

  1. The stash moves to utils/queuedSendStash.ts — inherited, "later" consumer
  2. A send receipt no longer overwrites a record already stashed — zero consumers, guarded writer absent
  3. Cancelling after an edit restores from the stash, not the parser — inherited
  4. An edit drops the attachments it removed from the stash — justified
  5. The hook re-exports the two new functions and the record type — zero consumers

Blockers

  • The guard has no adopter to race against. Grepping queuedSendStash (non-test) finds three writers, all on the receipt path (composerSend.ts:776, useChatPaneSend.ts:369, useChatPaneSend.ts:485), plus one consumer (useQueuedMessageActions.ts:234). None of the 14 non-test queue_push files writes the stash. The change says it fixes "queue_push can arrive before the HTTP receipt", but that race cannot happen on base, and no test exercises it.
    Delete the has() early return in stashQueuedSend.
    Clears when: the guard is deleted, or a queue_push stash writer is on base.

[FIRST-PRINCIPLES-REVIEWED] bf96918

Description read

[DESCRIPTION-READ] c87dc44932a208dd56b0421e89f9cc19b84c5e24709d6d7070849ed1667050f6

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

Opus 5.5 Review (fork) — 🔴 changes requested (blocking)

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

Blocks merge: a cancel after an edit puts the card's wire-format text, with its attachment markers and quote block, back into the composer.

BLOCKING — website/src/utils/queuedSendStash.ts:293
queuedSendStash.set(queueId, { ...rec, raw: submitted, sent: display, files })
Queue a send "summarize this" with /tmp/a.pdf staged, so the card holds summarize this\n[attached_file 1] /tmp/a.pdf. EditInput seeds from that wire text through queuedOwnText, and reglueQuote puts any quote block back at the head. After the PATCH, restashEditedSend stores the wire text as raw, so the sent === msg.content check now matches. onCancel then calls restoreDraft(stashed.raw, …, stashed.quote). The composer gets the literal [attached_file 1] /tmp/a.pdf line, plus the > block next to a restaged quote card. A resend then carries the attachment and the quote twice. Before this change, an edited card missed the stash and went to the parser, which strips both.
Fix: in restashEditedSend, set raw to restoreQueuedContent(stripQuoteBlock(submitted, rec.quote) if quoted, rec.files).text instead of submitted, and take aliases from that same result. Or delete the record on edit so cancel uses the parser path.

[BLOCK-MERGE] bf96918
[OPUS-REVIEWED] bf96918

@github-actions

Copy link
Copy Markdown
Contributor

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

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

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

BLOCKING -- website/src/utils/queuedSendStash.ts:100 -- Cancelling an edited entry duplicates its serialized quote and attachments
queuedSendStash.set(queueId, { ...rec, raw: submitted, sent: display, files })
Edit a queued message with a quote or attachment -> QueueStack submits wire-form content -> cancel restores that content alongside the retained quote/files -> buildOutgoingTurn serializes them again, duplicating quote blocks or attachment markers.
Anchor: residual/crash-data-loss-corruption
Fix: Deserialize the submitted content into composer text, strip the retained quote block, and refresh attachment aliases before storing raw.

BLOCKING -- website/src/utils/queuedSendStash.ts:97 -- A spaced filename revives an attachment removed by the edit
const files = rec.files.filter(p => declared.has(p) || mentionsPath(refs, p))
Queue /tmp/Report.pdf and /tmp/Report.pdf copy.pdf, then delete only the first marker -> the server removes the first attachment, but mentionsPath accepts the space inside the surviving filename -> cancel re-stages the removed file and resend attaches it again.
Anchor: residual/crash-data-loss-corruption
Fix: Prune using exact numbered attachment identities or authoritative post-edit attachment metadata, rather than whitespace-bounded path substrings.

[BLOCK-MERGE] bf96918
[GPT-REVIEWED] bf96918

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

Both fenced findings stay blocking. Neither depends on implausible conditions, so I can't FLAG either one.

  • F1 — the claim is real and the path is common: after an edit, raw holds the submitted text, which still contains the quote block. QueueStack puts it back on edit (website/src/components/QueueStack.tsx:440), and the restash keeps rec.quote (patched website/src/utils/queuedSendStash.ts:100). On cancel, the stash matches, so both raw and the quote come back (website/src/hooks/useQueuedMessageActions.ts:236,248,261-262). Editing a quoted queued message and then cancelling is a normal action, nothing extreme. No FLAG.

  • F2 — the claim is real: mentionsPath treats whitespace as the end of a path (patched website/src/utils/queuedSendStash.ts:264-266, applied at :97). That lets /tmp/Report.pdf copy.pdf keep a deleted /tmp/Report.pdf attached. The " copy" name comes from ordinary file duplication, so the conditions are rare but realistic. No FLAG.

    [ADJUDICATION] bf96918 total=0 uphold=0 downgrade=0
    [GPT-ADJUDICATED] bf96918

    [ADJUDICATION-FENCED] bf96918 fenced=2 flagged=0
    UPHOLD-FENCED F1 website/src/utils/queuedSendStash.ts:100 -- QueueStack.tsx:440 re-glues the quote block into the submitted edit and the restash keeps rec.quote, so the common edit-then-cancel path restores both and a resend duplicates the quote.
    UPHOLD-FENCED F2 website/src/utils/queuedSendStash.ts:97 -- mentionsPath accepts whitespace as a path boundary, so an ordinary "Report.pdf copy.pdf" sibling keeps a deleted attachment staged; the conditions are plausible, not contradictory.
    [GPT-ADJUDICATED-FENCED] bf96918

@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 20, 2026
@rnoack1 rnoack1 closed this Oct 21, 2026
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 21, 2026
@rnoack1
rnoack1 deleted the split/queued-send-stash-module branch November 13, 2026 17:03
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant