Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(documents): retain descendant IDs when replacing blocks

Afloat7eb653b32e31
diff --git a/packages/document-model/AGENTS.md b/packages/document-model/AGENTS.md
index 31ebd86b..96b9a62f 100644
--- a/packages/document-model/AGENTS.md
+++ b/packages/document-model/AGENTS.md
@@ -20,3 +20,6 @@
- Test deterministic behavior through exported interfaces with fixed
Yjs/BlockNote fixtures. Do not test private helpers or reproduce the
implementation to compute expected values.
+- Replacing or updating blocks may retain the IDs of their removed descendants.
+ Check replacement ID collisions against the blocks outside the complete
+ replaced subtrees, while keeping all replacement IDs unique.
diff --git a/packages/document-model/src/semantic.test.ts b/packages/document-model/src/semantic.test.ts
index 99e602b2..ef8a1612 100644
--- a/packages/document-model/src/semantic.test.ts
+++ b/packages/document-model/src/semantic.test.ts
@@ -214,6 +214,96 @@ describe("Room Document semantic mutation model", () => {
expect(Y.encodeStateVector(applied)).toEqual(first.endStateVector);
});
+ it.each(["update_block", "replace_blocks"] as const)(
+ "%s retains the stable IDs and content of the replaced block's descendants",
+ (kind) => {
+ const state = stateWithBlocks([
+ {
+ id: "parent",
+ type: "paragraph",
+ content: "Original parent",
+ children: [
+ {
+ id: "child",
+ type: "paragraph",
+ content: [
+ {
+ type: "text",
+ text: "Retained child",
+ styles: { bold: true },
+ },
+ ],
+ children: [
+ {
+ id: "image",
+ type: "image",
+ props: {
+ url: "document-asset:asset_image",
+ caption: "Retained chart",
+ },
+ },
+ ],
+ },
+ ],
+ },
+ { id: "outside", type: "paragraph", content: "Unchanged neighbor" },
+ ]);
+ const before = readRoomDocumentBlocks(state);
+ const parent = before[0]!;
+ if (parent.type !== "paragraph") throw new Error("expected paragraph");
+ const block = { ...parent, content: "Updated parent" };
+ const input: Parameters<typeof applyRoomDocumentSemanticMutation>[0] = {
+ mutationId: "replace-retained-descendants",
+ schemaVersion: "blocknote-semantic-v1",
+ currentTitle: "Title",
+ stateBytes: state,
+ allowedDocumentAssetIds: ["asset_image"],
+ operations:
+ kind === "update_block"
+ ? [{ kind, stableBlockId: "parent", block }]
+ : [{ kind, stableBlockIds: ["parent"], blocks: [block] }],
+ };
+ const result = applyRoomDocumentSemanticMutation(input);
+ expect(result.status).toBe("applied");
+ if (result.status !== "applied") throw new Error("conflict");
+ const after = readRoomDocumentBlocks(result.stateBytes);
+ expect(after[0]).toMatchObject({
+ id: "parent",
+ content: [{ text: "Updated parent" }],
+ });
+ expect(after[0]!.children).toEqual(before[0]!.children);
+ expect(after[1]).toEqual(before[1]);
+ expect(result.completeReferences.documentAssetIds).toEqual([
+ "asset_image",
+ ]);
+ expect(readRoomDocumentBlocks(state)).toEqual(before);
+ expect(applyRoomDocumentSemanticMutation(input)).toEqual(result);
+ const applied = new Y.Doc();
+ try {
+ Y.applyUpdate(applied, state);
+ Y.applyUpdate(applied, result.update);
+ expect(fingerprintRoomDocumentState(applied)).toBe(result.stateHash);
+ } finally {
+ applied.destroy();
+ }
+ const colliding = {
+ ...block,
+ children: [
+ { id: "outside", type: "paragraph" as const, content: "Collision" },
+ ],
+ };
+ expect(() =>
+ applyRoomDocumentSemanticMutation({
+ ...input,
+ operations:
+ kind === "update_block"
+ ? [{ kind, stableBlockId: "parent", block: colliding }]
+ : [{ kind, stableBlockIds: ["parent"], blocks: [colliding] }],
+ }),
+ ).toThrow(/already exist/);
+ },
+ );
+
it("conflicts on missing or structurally ambiguous stable block targets", () => {
const state = stateWithBlocks([
{ id: "one", type: "paragraph", content: "One" },
diff --git a/packages/document-model/src/semantic.ts b/packages/document-model/src/semantic.ts
index 0fbfb250..93816636 100644
--- a/packages/document-model/src/semantic.ts
+++ b/packages/document-model/src/semantic.ts
@@ -369,7 +369,10 @@ function applyBlockOperation(
) {
return conflict("invalid_structure");
}
- if (hasIDCollision(root, replacements, new Set(targetIDs))) {
+ const removedIDs = collectIDs(
+ range.siblings.slice(range.start, range.start + range.width),
+ );
+ if (hasIDCollision(root, replacements, removedIDs)) {
throw invalidSemantic("Replacement Block IDs already exist");
}
range.siblings.splice(range.start, range.width, ...replacements);