Skip to content

fix(assistant): stop restaging references the composer already holds - #588

Open
Zhangbo-cn wants to merge 10 commits into
ThinkFlowLab:mainfrom
Zhangbo-cn:fix/assistant-reference-dedupe
Open

Zhangbo-cn wants to merge 10 commits into
ThinkFlowLab:mainfrom
Zhangbo-cn:fix/assistant-reference-dedupe

Conversation

@Zhangbo-cn

@Zhangbo-cn Zhangbo-cn commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When a node's reference is already in the assistant composer, selecting that node again renders the same reference twice: the pending "1 reference to insert" tray appears next to the inserted chip. The reported path uses a text node: upload a note, click the node, click the composer, then click the node again.

Root cause: WorkspaceView bumps assistantSelectionRequest on every node click, so usePendingCanvasReferences restages the selected node from the selection change alone. It does not know that the composer already holds that reference, and AssistantPanel rendered every pending item.

A mixed selection exposed a second defect in the first fix: when part of the selection was already referenced, the composer kept the filtered item visible but the insertion freshness check compared the filtered batch with the unfiltered selection. The check failed and the staged item was silently dropped, so clicking the composer did nothing.

Refs #587

Changes

  • src/canvas/chatReferenceContent.ts: add referenceNodeId(part, asset) and referencedCanvasNodeIds(content, assets). They resolve a draft reference to its canvas source from the matched reference asset: a canvas part uses its nodeId, a pinned (binding: "asset") part uses the asset's logicalNodeId ?? nodeId. referenceIsOffCanvas now shares the same resolution instead of duplicating it.
  • src/canvas/pendingCanvasReferences.ts: add unreferencedPendingItems(items, referenced) and sameStagedPendingReferences(selected, referenced, batch), which compares the batch with the same filtered projection the tray renders.
  • src/canvas/AssistantPanel.tsx: derive referenced node ids from value.draftContent and chat.referenceAssets (the assets the assistant retains for the draft, a superset of the reduced attachment list) instead of the current attachment list, filter the staged items, cancel pending and hide the tray when nothing remains, and use the filtered freshness check before insertion.
  • Tests: unit cases in tests/chatReferenceContent.test.ts and tests/pendingCanvasReferences.test.ts; new gate workflow tests/browser/assistant_reference_dedupe.mjs, registered in tests/browser/manifest.json and scripts/run-browser-suite.mjs. The browser test covers the reported text-node path and the image-node mixed selection. It opens the assistant only when the workspace has it closed and arranges the uploaded nodes before selecting them, so the gate exercises reference staging instead of node overlap on the current workspace.

No user-visible copy was added; existing zh-CN and en-US strings are reused. The composer attachment strip is unchanged.

Evidence

Reproduction (isolated fixture API and Vite dev server; no real projects, models, or paid calls):

  1. Create a project and open the assistant (current main opens it by default; the test opens it only when closed).
  2. Upload note.txt, click the new text node, and click the composer. The reference is inserted as a chip and the tray closes.
  3. Click the same text node again.
  4. Upload reference.png, click it, and insert it the same way.
  5. Upload reference-2.png, click the first image node, then Shift-click the second image node.
  6. Click the composer.

Before (728b4077, current base), step 3 brings the tray back with the already-inserted text reference, next to the chip:

Before: re-selecting the text node restages the reference beside the chip

After (9732c497, current head), step 3 leaves the tray absent and keeps exactly one chip:

After: re-selecting the text node keeps a single chip and no tray

Before (35dccfe0, the pushed first-fix head, same final script), step 6 leaves the filtered item staged and inserts nothing: the tray still reads "1 reference to insert" for reference-2.png after the composer click, with two chips:

Before: the mixed selection is staged but the composer click inserts nothing

After (9732c497, current head), step 6 inserts the remaining unreferenced node, closes the tray, and leaves three chips:

After: the mixed selection inserts the remaining unreferenced node

Failing before, passing after: tests/browser/assistant_reference_dedupe.mjs asserts tray and chip counts at each step.

  • On 728b40774628307f315a2b29c466237a8c434a11, with the final script copied onto that tree, the reported path fails first:
BASE SHA: 728b40774628307f315a2b29c466237a8c434a11
AssertionError [ERR_ASSERTION]: re-selecting a referenced text node must not restage it

1 !== 0
  • On the first-fix tree 35dccfe0f2ebd917368dc06fcae0ab6bf37e18ec, the same script reaches step 6 and fails there (chips 2, tray 1):
PREV HEAD SHA: 35dccfe0f2ebd917368dc06fcae0ab6bf37e18ec
locator.waitFor: Timeout 15000ms exceeded.
  - waiting for locator('.cw-assistant-panel:not([hidden])').locator('.cw-chat-reference-editor [data-reference]').nth(2) to be visible
  • On the current head the same script passes; it passed six consecutive runs against the isolated services: {"passed":true,"checks":["text node staged once","text node inserted once","text node re-select stays filtered","stage once","insert once","re-select stays filtered","mixed selection filtered","no duplicate chip","no paid job"]}.

Ran

On the current head (9732c497f6349fffe9136f1c08928d6a8b6dfa77):

  • npx tsc --noEmit — passed
  • npm test — 1114/1114 passed
  • npm run check:i18n — 3839 keys, 0 problems
  • npm run check:browser-manifest — 110 entries, gate=38
  • npm run check:browser-locale — 102 pages, 0 problems
  • node tests/browser/assistant_reference_dedupe.mjs — passed six consecutive runs against the isolated fixture API and Vite
  • The same test on 728b4077 — failed with 1 !== 0 (above)
  • The same test on 35dccfe0 — failed waiting for the third chip (above)

Not run

  • Release checks on this head: run 47319253270 at 9732c497 passed every lane — checks / browser (isolated) (10m3s), checks / browser (assistant), checks / browser (voice), checks / frontend, checks / python, checks / static, checks / desktop / macos, checks / desktop / windows, and the aggregate checks / checks; CodeQL and the tests workflow are green too. These lanes cover npm run build and the other browser lanes that were not run locally.
  • The full gate lane was not run locally; the new workflow was exercised on its own against the same isolated services.
  • Python suites were not run: the diff does not touch backend code.
  • Pinned binding: "asset" references restored from a saved conversation without retained asset metadata are not covered by the new tests: without a resolvable canvas node id the staged item is not filtered. The reported canvas-node paths are covered; this bounded case is unchanged by the PR.

Checks note

The previous head (ca602748) failed Release checks / static: the branch was 39 commits behind main, and pre-commit --from-ref <base> --to-ref HEAD flagged a pre-existing trailing space in README.zh-CN.md, a file this PR does not touch. The branch is now merged with main (728b4077), which refreshes the base and removes that stale file from the scan; the PR's own files have no whitespace findings.

Review

  • Reviewed base: 728b40774628307f315a2b29c466237a8c434a11 (origin/main)
  • Reviewed head: 9732c497f6349fffe9136f1c08928d6a8b6dfa77
  • Self-review (opendirector-review, candidate mode): PASS, 93/100.
  • Independent candidate review (non-contributing subagent, same skill and pinned base/head): 93/100, WITHHOLD only because Release checks had not run on the unpushed head; no blocking code defect. It independently reproduced tsc, npm test (1114/1114), check:i18n (3839/0), check:browser-manifest (110, gate=38), check:browser-locale (102/0), and the targeted unit tests, and inspected the four screenshots and both failing-before logs.
  • Non-blocking notes from the reviews: a pinned binding: "asset" reference restored without retained asset metadata cannot be resolved to a canvas node id, so it is not filtered; the same gap could allow a duplicate if that metadata resolved asynchronously after a click, which was not observed. The reported canvas-node paths derive their id from part.nodeId and are safe.
  • Material validation gaps: at review time Release checks had not run on this head; they have since passed at 9732c497 (run 47319253270), resolving that blocking condition. npm run build, the other browser lanes, desktop jobs, and the Python suites were not run locally; the green Release checks cover the build, browser, and desktop lanes, and the diff does not touch backend code.

🤖 Generated with Claude Code

Clicking a node bumps the assistant selection request, which stages the node
again even after its reference chip was inserted. The tray then drew a second
copy of the same title under the chip (two boxes for one reference).

Filter the staged batch against the references already present in the draft
content and attachments, cancel the tray when nothing is left, and keep the
insert path on the filtered batch.
@github-actions github-actions Bot added area:agent Agent runtime, assistant chat and tools type:bug Something is broken labels Nov 29, 2026
@github-actions
github-actions Bot requested review from FayeSpica and wuzhf9 November 29, 2026 14:27
Zhangbo-cn and others added 4 commits November 29, 2026 23:17
The strip repeated every reference the editor already shows as a chip and, after an
insert, added a collapsible 'collapsed attachments' block under the input. Keep the
chip as the single representation; sent messages keep their own strip.
Co-Authored-By: Claude Code <noreply@anthropic.com>
Filter the pending batch by the same source identity visibility uses,
derive node ids from the reference catalog instead of the reduced
attachment list, and cover mixed selections in unit and browser tests.

Co-Authored-By: Claude Code <noreply@anthropic.com>
The dedupe browser test only exercised image nodes. Upload a text note
and check the staged/inserted/restaged cycle on the text composer.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Zhangbo-cn pushed a commit to Zhangbo-cn/OpenDirector that referenced this pull request Dec 4, 2026
Co-Authored-By: Claude Code <noreply@anthropic.com>
dev and others added 2 commits December 4, 2026 18:01
…ce-dedupe

Co-Authored-By: Claude Code <noreply@anthropic.com>
Open the chat panel only once it is actually visible, and lay the
uploaded nodes out before clicking them, so the gate exercises
reference staging instead of node overlap.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Zhangbo-cn pushed a commit to Zhangbo-cn/OpenDirector that referenced this pull request Dec 4, 2026
Co-Authored-By: Claude Code <noreply@anthropic.com>
Zhangbo-cn pushed a commit to Zhangbo-cn/OpenDirector that referenced this pull request Dec 4, 2026
Co-Authored-By: Claude Code <noreply@anthropic.com>
Zhangbo-cn added a commit to Zhangbo-cn/OpenDirector that referenced this pull request Dec 4, 2026
Zhangbo-cn added a commit to Zhangbo-cn/OpenDirector that referenced this pull request Dec 4, 2026
Zhangbo-cn added a commit to Zhangbo-cn/OpenDirector that referenced this pull request Dec 4, 2026
Zhangbo-cn added a commit to Zhangbo-cn/OpenDirector that referenced this pull request Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent runtime, assistant chat and tools type:bug Something is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant