{"record":{"id":"1c50221bf35550a2","repo":"siyuan-note/siyuan","slug":"a-document-tree-plan-must-be-applied-in-a-single-t","errorCode":null,"errorMessage":"a document tree plan must be applied in a single transaction","messagePattern":"a document tree plan must be applied in a single transaction","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":456,"sourceCode":"\t\t\"title\":    node.Title,\n\t\t\"id\":       node.ID,\n\t\t\"parentID\": node.ParentID,\n\t\t\"rootID\":   node.RootID,\n\t\t\"hPath\":    node.HPath,\n\t\t\"name\":     \"\",\n\t\t\"alias\":    \"\",\n\t}\n}\n\n// AttachTemplateDocTreePlans 将一次性计划转换为内核事务操作，父文档内容与全部子文档共用一条撤销记录。\nfunc AttachTemplateDocTreePlans(transactions []*Transaction) (attached bool, err error) {\n\tvar target *Transaction\n\tfor _, transaction := range transactions {\n\t\tif nil == transaction || \"\" == transaction.TemplateDocTreePlanID {\n\t\t\tcontinue\n\t\t}\n\t\tif nil != target || 1 != len(transactions) {\n\t\t\treturn false, errors.New(\"a document tree plan must be applied in a single transaction\")\n\t\t}\n\t\ttarget = transaction\n\t}\n\tif nil == target {\n\t\treturn false, nil\n\t}\n\tif target.isReplay {\n\t\treturn false, errors.New(\"template document tree plans cannot be attached to replay transactions\")\n\t}\n\tif 0 == len(target.DoOperations) || 0 == len(target.UndoOperations) {\n\t\treturn false, errors.New(\"template document tree plan requires reversible parent operations\")\n\t}\n\tif err = validateTemplateDocTreeParentOperations(target); nil != err {\n\t\treturn false, err\n\t}\n\tfor _, operation := range append(append([]*Operation{}, target.DoOperations...), target.UndoOperations...) {\n\t\tif nil != operation && (\"restoreCreatedDoc\" == operation.Action || \"removeCreatedDoc\" == operation.Action) {\n\t\t\treturn false, errors.New(\"template document tree transaction contains a reserved operation\")","sourceCodeStart":438,"sourceCodeEnd":474,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L438-L474","documentation":"AttachTemplateDocTreePlans is invoked when transactions are committed and only accepts a batch containing exactly one transaction that carries a TemplateDocTreePlanID. If more than one transaction is present and one of them carries a plan, or multiple carry plans, the kernel rejects the commit because a document tree plan must be atomic in a single transaction to keep undo/redo consistent.","triggerScenarios":"Calling the transaction-commit path (e.g. /api/transactions) with multiple transactions in one request where at least one has a templateDocTreePlanID, or batching two plan-bearing transactions together.","commonSituations":"Plugin or integration code batches transactions; a frontend retry accumulates several plan transactions in one payload.","solutions":["Send the plan-bearing transaction alone in its own request (transactions array length must be 1)","Split batched operations into separate commit requests so the plan transaction is isolated","Ensure the frontend does not attach TemplateDocTreePlanID to more than one transaction"],"exampleFix":"// before\nPOST /api/transactions {\"transactions\": [txWithPlan, otherTx]}\n// after\nPOST /api/transactions {\"transactions\": [txWithPlan]}\nPOST /api/transactions {\"transactions\": [otherTx]}","handlingStrategy":"validation","validationCode":"if (transactions.length !== 1 || !transactions[0].templateDocTreePlanID) throw new Error('plan must be its own single transaction')","typeGuard":"null","tryCatchPattern":"try { await commitTransactions(txs) } catch (e) { if (String(e).includes('single transaction')) { for (const tx of txs) await commitTransactions([tx]) } else { throw e } }","preventionTips":["Always send plan-bearing transactions in a dedicated request with exactly one transaction","Do not batch template plan commits with unrelated operations","Guard client code so TemplateDocTreePlanID is set on at most one transaction per request"],"tags":["transaction","atomicity","template"],"backgroundTag":"invalid-argument-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}