DIT-13463: Stable occurrence index on scan candidates - #2
Merged
Conversation
This was referenced Aug 17, 2026
jholiga
approved these changes
Aug 18, 2026
jholiga
left a comment
Collaborator
There was a problem hiding this comment.
1 small thing, otherwise LGTM! Just a note that we'll also want to bump the version in package.json before publishing the changes from your 3 PRs to npm
| @@ -252,6 +253,29 @@ export function makeCandidateId( | |||
| * `extractFile` route through here so there's only one copy of it. Extractor | |||
Collaborator
There was a problem hiding this comment.
I think this comment should be above extractFromResolvedFile
Collaborator
Author
I'll do that last after all three go in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Adds
occurrence_indexto scan candidates: which repeat this string is within its file, counting from 0 in source order.If a file has three
"Save"strings, only the line number tells them apart today — and line numbers move whenever someone adds an import. The index doesn't, so a code link can still say which occurrence it points at after the file shifts.assignOccurrenceIndexes(hits)insrc/extract.ts, used byextractFromResolvedFileand exported so it can be unit tested.(line, column)first, which is the point of the PR. The JS extractor emits hits grouped by node kind (src/lang/extractors/javascript.ts:38), not source order, so counting them as they arrive gives an index that changes between runs of an unmodified file. Sorting happens on indices internally, so emitted candidate order is unchanged.occurrence_indexadded toDittoScanCandidateSchema.Nothing server-side is needed to merge — zod strips unknown keys, so the field is ignored on arrival for now. A ditto-app follow-up adds it to
PtdCandidateSchema, where the code link write will read it.Context
DIT-13463, part of DIT-13405.
A code link identifies a hardcoded string by (file, value, occurrence index) — never by line number, which is display-only. This is the last of those three fields. Schema design.
Screenshots
None — extraction output only.
Test Plan
Example e2e test: In v0-demo
Make two identical copies of a real app.
mkdir -p /tmp/demo-a git -C ../v0-demo archive HEAD | tar -x -C /tmp/demo-a cp -R /tmp/demo-a /tmp/demo-bgit archive HEADstreams a tarball of the committed tree straight intotar -xEdit one copy, in the two ways that matter.
In
/tmp/demo-b/src/features/payments/SendMoney.tsx:t("send.title")below the two already there. This checks that a new repeat takes the next index rather than disturbing the existing ones.Extract both and compare.
The script goes in the repo root:
./dist/index.jsresolves relative to the script's own location, not the working directory.The filter matches on
value_raw, noti18n_key. These aret("send.title")call arguments in a.tsxfile, so they're extracted as hardcoded literals and arrive withi18n_key: null— filtering on the key prints nothing at all.3. What to look for
Every line moved by 4. Every index stayed. That's the whole result: line numbers churn on an unrelated edit,
occurrence_indexdoesn't, so a code link keyed on it still points at the same string. Assigned in extraction order instead, these would have shuffled. The new occurrence is#2because it comes after the others in the file.Known limit: add that third
t("send.title")above the existing two instead and it becomes#0, renumbering the rest to#1and#2. Keyed strings survive that viai18nKey; hardcoded ones fall through to the value-in-file rung during reconciliation. The index only has to be stable against edits elsewhere in the file, not against inserting a new copy of itself.Note: linking this package into the CLI (
yarn link @dittowords/text-extract) and runningyarn scandoes not exercise this code until cli#151 merges — the CLI still uses its own copy of the extractor until then.