Skip to content

fix(input): stop placeholder ids from being recycled and clobbering live placeholders - #998

Merged
will-lamerton merged 3 commits into
Nano-Collective:mainfrom
arpan7sarkar:fix/placeholder-id-collision
Aug 30, 2026
Merged

will-lamerton merged 3 commits into
Nano-Collective:mainfrom
arpan7sarkar:fix/placeholder-id-collision

Conversation

@arpan7sarkar

Copy link
Copy Markdown
Contributor

Description

Closes #969

Paste placeholder ids were numbered from the count of live entries (existingPasteCount + 1), so deleting one freed its id for reuse.
Paste twice, delete the first, paste again: the third paste got id 2 and overwrote the second paste, destroying its content and leaving two chips labelled #2 - one of which reached the model as literal text.

Ids are now namespaced by type (paste_1, file_1) and allocated from the highest id ever used, so a deletion can never free one.
Placeholder lookup matches on each entry's own display text instead of a paste-shaped regex, which also makes @file mentions delete atomically instead of leaving an orphan entry.

Note: the reported paste/@file collision was never possible - mentions have been keyed file_${n} since the feature shipped.
The symptom was real but came from reuse within the paste keyspace.

Also included: a 9-line fix for 51 pre-existing failing specs in source/vscode/, where the harness never loaded the new uri-utils.js / slash-command-utils.js webview helpers.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Changeset

  • Added a changeset (pnpm changeset) describing this change for the changelog

Testing

Automated Tests

  • New features include passing tests in .spec.ts/tsx files
  • All existing tests pass (pnpm test:all completes successfully)
  • Tests cover both success and error scenarios

pnpm test:ava - 6664 passed, 0 failed. Types, lint, format and knip clean; test:audit and test:security need network and were left to CI.

Manual Testing

  • Tested with Ollama
  • Tested with OpenRouter
  • Tested with OpenAI-compatible API
  • Tested MCP integration (if applicable)

No provider round-trip is involved - the defect lives in input state, before anything is sent.
To check in the TUI: paste a multi-line block twice, backspace once into the first chip, then paste again. Before, two chips read #2; after, #2 and #3.

Checklist

  • If this was for an open issue, I was assigned to it
  • Code follows project style guidelines
  • Self-review completed
  • Documentation updated (if needed)
  • No breaking changes (or clearly documented)
  • Appropriate logging added using structured logging (see CONTRIBUTING.md)

Copilot AI lite review requested due to automatic review settings August 28, 2026 06:27

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Placeholder lookup was done with a hardcoded /\[Paste #(\d+): \d+ chars\]/
regex that parsed the map key back out of the rendered label. That tied three
separate concerns to one value and broke in three ways:

- @file mentions were invisible to it, so deleting a mention's text left its
  entry stranded in placeholderContent forever.
- assemblePrompt replaced via String.replace(displayText, ...), which only
  substitutes the first match, so two placeholders rendering identically left
  one unexpanded and shipped a raw "[Paste Nano-Collective#2: 287 chars]" to the model.
- deletePlaceholder interpolated the id into a RegExp, needing sanitization
  and a nosemgrep suppression to stay safe.

Add findPlaceholderOccurrences, which scans the display value for each entry's
own display text and gives every entry its own position. Deletion, assembly
and cursor lookups all go through it, so they work for every placeholder type
and never confuse duplicates. Dropping the constructed RegExp removes the
injection concern outright, so the sanitizing and the suppression go too.

Two deletePlaceholder specs were passing for the wrong reasons: they called a
stale first-render snapshot of the hook, which blanked the input and made the
assertions vacuously true, and one ended in a bare t.pass(). They now drive
the live hook, keep the map key distinct from the display label, and assert
the resulting string exactly.
Both handlers numbered placeholders from how many entries were currently
alive:

  const pasteId = (existingPasteCount + 1).toString();

Count-based numbering frees an id the moment its placeholder is deleted, so
the next allocation lands on a key that is still in use. Paste twice, delete
the first, paste again: the third paste is handed id "2" and overwrites the
second paste's content, leaving two chips both labelled "[Paste Nano-Collective#2: ...]".

Allocate from the highest id ever used instead, and namespace by type
(paste_1, file_1) so the two kinds can never share a key. Legacy bare-numeric
paste keys are still recognised, because prompt-history persists InputState to
disk and older sessions replay it.

The visible "#N" is now a display counter carried in displayText rather than
the map key, so chunked-paste continuation restates the existing label via
resizePasteDisplayText instead of rebuilding it from the id.

Note the collision in the original report - paste ids overwriting @file
mentions - was never possible: file mentions have been keyed file_N since the
feature shipped. The reported symptom came from this reuse instead.
semgrep's detect-non-literal-regexp flagged

  new RegExp(`^${prefix}_(\\d+)$`)

as a possible ReDoS vector. The prefix only ever comes from a fixed record
keyed by PlaceholderType, so it was not reachable from user input, but the
regex was not needed either: a startsWith on the marker plus a hardcoded
/^\d+$/ on the remainder does the same job and is simpler to read.

No behaviour change; the allocator specs cover it.
@arpan7sarkar
arpan7sarkar force-pushed the fix/placeholder-id-collision branch from 40d6f2f to 564ebb1 Compare August 28, 2026 11:14
@will-lamerton

Copy link
Copy Markdown
Member

Great work @arpan7sarkar :)

@will-lamerton
will-lamerton merged commit cad13a0 into Nano-Collective:main Aug 30, 2026
11 checks passed
will-lamerton added a commit that referenced this pull request Aug 30, 2026
Placeholder lookup matches on each entry's display text, so paste entries
persisted to prompt history before placeholders carried a displayText were
skipped: their label survived into the assembled prompt and reached the
model as literal text instead of expanding to the pasted content.

Rebuild the label for those entries from the ordinal in their key and the
content they hold, restoring the legacy fallback that the display-text
lookup replaced in #998.
@will-lamerton will-lamerton mentioned this pull request Aug 30, 2026
2 of 8 tasks
@arpan7sarkar

Copy link
Copy Markdown
Contributor Author

Thanks @will-lamerton , It's really great to be part of building something meaningfull for the community :-)

@will-lamerton will-lamerton mentioned this pull request Aug 30, 2026
14 of 18 tasks
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.

[Bug] Paste placeholder ID collides with file mention IDs

3 participants