feat(core): sourceMutation data-hf-id targeting (R1, T7)#1272
Conversation
Elements now get data-hf-id minted by ensureHfIds; parser reads data-hf-id as model id, so HTML id attrs are no longer the model id. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Locks the preservation guarantee the write-back design depends on: a Studio edit targeting by id or selector (it never sends hfId) must not strip an existing data-hf-id, or the stable handle is destroyed by the next edit. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
miguel-heygen
left a comment
There was a problem hiding this comment.
Best PR in the stack. The CSS injection guard is the right security catch — attribute-selector injection via a crafted hfId is a real risk if this surface is ever caller-controlled, and escapeCssAttrValue is correctly ordered (backslash first, then quote). The try/catch wrapper on querySelectorAllWithTemplates adds a clean second line of defense.
The data-hf-id write-protection and splitElementInHtml clone cleanup are exactly right — id uniqueness invariant is preserved, and the next parse will mint a fresh id for the split half.
One minor thing:
P3 — findTargetElement uses raw string interpolation, not escapeCssAttrValue:
const matches = querySelectorAllWithTemplates(document, `[data-hf-id="${target.hfId}"]`);escapeCssAttrValue is defined in this file but not used in findTargetElement. If target.hfId comes from a caller (not always a round-tripped stable id), a value containing " or \ would produce a malformed CSS selector, and the try/catch on querySelectorAllWithTemplates would silently swallow the miss. Should be:
`[data-hf-id="${escapeCssAttrValue(target.hfId)}"]`This closes the same gap escapeCssAttrValue was introduced to address.
james-russo-rames-d-jusso
left a comment
There was a problem hiding this comment.
Core-side mutation targeting. Symmetric with #1271's patcher but uses real DOM selectors via querySelectorAllWithTemplates (which handles <template> content, important for sub-compositions) rather than regex. The two preservation tests at the end are the most important contribution in the whole stack — they pin that data-hf-id SURVIVES a patch targeted by id or selector. Without those, R2+ write-back would silently destroy the stable handle on first edit.
Concerns
[CSS selector injection via the data-hf-id string interpolation] Line 36-37:
const matches = querySelectorAllWithTemplates(document, `[data-hf-id="${target.hfId}"]`);If target.hfId ever carries a " or a closing-bracket / opening-bracket character, the selector parse goes sideways — either matches nothing, throws on invalid selector, or matches an unintended element if the attacker can craft the value. Right now callers are all internal and pass minted ids (which match /^hf-[a-z0-9]{4}$/), so the value is safe. But this PR is the foundation layer that future callers will build against, and once R2+ wires in user-facing flows (e.g. a Studio caller passing a value from an HTML form, or a serialized snapshot deserialized through a network boundary), the contract gets exercised more broadly.
Two defensive options:
CSS.escapeif the runtime has it (node-DOM polyfills usually do).\[data-hf-id="${CSS.escape(target.hfId)}"]``.- Validate at the boundary:
if (!/^hf-[a-z0-9]{4}$/.test(target.hfId)) return null;— rejects anything that doesn't match the spec, fails closed.
Either closes the loop. Cheap insurance.
[findTargetElement doesn't check multi-match for hfId] Same shape as the concern I raised on #1271's execDataAttrPattern: matches[0] is returned without checking matches.length > 1. By the mint contract this shouldn't happen, but the defensive check is one line:
const matches = querySelectorAllWithTemplates(document, `[data-hf-id="${escapedId}"]`);
if (matches.length > 1) {
console.warn(`sourceMutation: hfId ${target.hfId} matched ${matches.length} elements; using first`);
}
if (matches[0]) return matches[0];…or fail-closed if you'd rather. Worth doing — quietly patching one of two ambiguous matches is harder to debug than failing loudly.
Strengths worth calling out
[Two preservation tests close the most important gap] The last two tests:
it("preserves an existing data-hf-id when the element is patched by id", () => { ... })
it("preserves an existing data-hf-id when the element is patched by selector", () => { ... })…with the comment "the stable handle is destroyed by the next edit. This is the preservation guarantee the write-back design depends on." Exactly the right contract to pin. These are the tests that make this PR safe to merge even though R2+ isn't wired yet — anyone refactoring the patch path will hit a red test the moment they break preservation. Excellent.
Nits
[probeElementInSource gate update] Line 213: if (!target.id && !target.hfId && !target.selector) return false; — correct, but the OR chain will grow if more target fields are added. A hasTargetFields(target) helper would scale better. Minor.
[hfId?: string vs id?: string | null mismatch] Same observation as on #1271 — the new field omits the null arm. Probably fine.
Verdict
Strongest PR in the stack from a testing perspective — the preservation tests pin exactly what R2+ depends on. The CSS injection defense and multi-match check are both 1-2 line additions worth landing before merge, but neither is structurally blocking the contract. Clean execution; leaving as a comment.
— Review by Rames D Jusso

What
Adds
hfIdtargeting tosourceMutation(packages/core/src/studio-api/helpers/sourceMutation.ts) and closes a CSS injection gap.New targeting field:
findTargetElementtrieshfIdfirst viafindByHfId, thenid, thenselector.CSS injection guard:
A crafted value like
hf-1234"][data-x="ywould otherwise produce a valid compound selector that silently matches the wrong element.Stable id protection:
data-hf-idis blocked in bothattributeandhtml-attributeop cases — it can never be overwritten by a patch operation.Split clone cleanup:
splitElementInHtmlremovesdata-hf-idfrom the cloned element so the next parse mints a fresh stable id for the new half.probeElementInSourceupdated to accepthfIdas a valid probe target.Why
T7 spec — the server-side mutation API (
files.tsroute) usessourceMutationto apply patches to composition source files. It needs to accepthfIdtargeting for the same reason as sourcePatcher (T3): stable R1 ids must be usable as mutation targets from all surfaces. Depends on PR #1270.The injection fix is a security correctness issue: without it, a user-controlled
hfIdvalue could produce a CSS selector that matches an unintended element.How
escapeCssAttrValue: escapes\before"(order matters — escaping"first would double-escape the backslash)findByHfIdwraps the selector call in try/catch in case the escaped value still somehow produces an invalid selectorsplitElementInHtmlclone:clone.removeAttribute("data-hf-id")before inserting — stable id must be unique; the parser will mint a new one on next round-tripTest plan
sourceMutation.test.ts— hfId targeting, injection guard, attribute protection