The Open Standard for Generative UI
fix(lang-core): use last complete definition for duplicate IDs in streaming parser (#1140)
AB55df79c2ff64
diff --git a/.changeset/fix-stream-redefined-ids.md b/.changeset/fix-stream-redefined-ids.mdnew file mode 100644index 000000000..f7f452bfe--- /dev/null+++ b/.changeset/fix-stream-redefined-ids.md@@ -0,0 +1,5 @@+---+"@openuidev/lang-core": patch+---++Fix streaming parsing so the latest definition of a repeated statement ID renders progressively as it arrives, matching how new statements stream. Completed programs use the last definition, matching the non-streaming parser.diff --git a/packages/lang-core/src/parser/__tests__/parser.test.ts b/packages/lang-core/src/parser/__tests__/parser.test.tsindex b95ed32b2..71a88ec90 100644--- a/packages/lang-core/src/parser/__tests__/parser.test.ts+++ b/packages/lang-core/src/parser/__tests__/parser.test.ts@@ -287,3 +287,86 @@ root = Title("hello")expect(result.root?.props.text).toBe("hello");});});++// ── redefined statement IDs (parse vs stream) ───────────────────────────────++describe("redefined statement IDs", () => {+ const withNewline = `root = Stack([a])+a = Title("x")+a = Title("y")+`;+ const withoutNewline = `root = Stack([a])+a = Title("x")+a = Title("y")`;++ const titleText = (result: ReturnType<typeof parse>) => {+ const children = result.root?.props?.children as any[] | undefined;+ return children?.[0]?.props?.text as string | undefined;+ };++ const pushAll = (+ text: string,+ write: (sp: ReturnType<typeof createStreamParser>, text: string) => void,+ ) => {+ const sp = createStreamParser(schema);+ write(sp, text);+ return titleText(sp.getResult());+ };++ it("parse() last-wins with and without a trailing newline", () => {+ expect(titleText(parse(withNewline, schema))).toBe("y");+ expect(titleText(parse(withoutNewline, schema))).toBe("y");+ });++ it("stream parser last-wins for a single push, matching parse()", () => {+ expect(pushAll(withNewline, (sp, text) => sp.push(text))).toBe("y");+ expect(pushAll(withoutNewline, (sp, text) => sp.push(text))).toBe("y");+ });++ it("stream parser last-wins line-by-line and character-by-character", () => {+ expect(+ pushAll(withNewline, (sp, text) => {+ for (const line of text.split(/(?<=\n)/)) {+ if (line) sp.push(line);+ }+ }),+ ).toBe("y");+ expect(+ pushAll(withNewline, (sp, text) => {+ for (const ch of text) sp.push(ch);+ }),+ ).toBe("y");+ expect(+ pushAll(withoutNewline, (sp, text) => {+ for (const ch of text) sp.push(ch);+ }),+ ).toBe("y");+ });++ it("stream parser last-wins across a two-chunk split", () => {+ expect(+ pushAll("", (sp) => {+ sp.push(`root = Stack([a])\na = Title("x")\n`);+ sp.push(`a = Title("y")\n`);+ }),+ ).toBe("y");+ expect(+ pushAll("", (sp) => {+ sp.push(`root = Stack([a])\na = Title("x")\n`);+ sp.push(`a = Title("y")`);+ }),+ ).toBe("y");+ });++ it("stream parser last-wins via set(), matching the renderer path", () => {+ const sp = createStreamParser(schema);+ expect(titleText(sp.set(withoutNewline))).toBe("y");+ });++ it("does not let an incomplete pending redefinition clobber a completed statement", () => {+ const sp = createStreamParser(schema);+ sp.push(`root = Stack([a])\na = Title("x")\n`);+ expect(titleText(sp.push(`a = Title("y`))).toBe("x");+ expect(titleText(sp.push(`")\n`))).toBe("y");+ });+});diff --git a/packages/lang-core/src/parser/parser.ts b/packages/lang-core/src/parser/parser.tsindex 2d201d559..c0482c6e7 100644--- a/packages/lang-core/src/parser/parser.ts+++ b/packages/lang-core/src/parser/parser.ts@@ -472,7 +472,7 @@ export function createStreamParser(cat: ParamMap, rootName?: string): StreamPars// a prefix of the new cleaned text (e.g. an opening ```fence``` just appeared// and shifted the stripped output), the watermark + cache are stale, so reset// and re-scan. When the prefix is stable (the common streaming case) the cache- // is kept, so a partial trailing statement never blanks already-completed ones.+ // is kept; pending text is overlaid on a copy when building the current result.function refreshCleaned() {const next = preprocess(buf);if (!next.startsWith(cleaned.slice(0, completedEnd))) {@@ -581,13 +581,11 @@ export function createStreamParser(cat: ParamMap, rootName?: string): StreamPars);}- // Merge: completed cache + re-parsed pending statement.- // Pending statements can only add NEW IDs — they cannot overwrite completed ones.- // This prevents mid-stream partial text (e.g. `root = Card`) from corrupting- // existing completed statements during edit streaming.+ // New IDs can render progressively; existing IDs are replaced only when+ // the pending expression no longer needs automatic closing.const allStmtMap = new Map(completedStmtMap);for (const s of stmts) {- if (completedStmtMap.has(s.id)) continue;+ if (completedStmtMap.has(s.id) && wasIncomplete) continue;const expr = parseExpression(s.tokens);const stmt = classifyStatement(s, expr);allStmtMap.set(s.id, stmt);