siyuan-note/siyuan · error

a document tree plan must be applied in a single transaction

Error message

a document tree plan must be applied in a single transaction

What it means

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.

Solutions

  1. Send the plan-bearing transaction alone in its own request (transactions array length must be 1)
  2. Split batched operations into separate commit requests so the plan transaction is isolated
  3. Ensure the frontend does not attach TemplateDocTreePlanID to more than one transaction

Example fix

// before
POST /api/transactions {"transactions": [txWithPlan, otherTx]}
// after
POST /api/transactions {"transactions": [txWithPlan]}
POST /api/transactions {"transactions": [otherTx]}
Defensive patterns

Strategy: validation

Validate before calling

if (transactions.length !== 1 || !transactions[0].templateDocTreePlanID) throw new Error('plan must be its own single transaction')

Type guard

null

Try / catch

try { await commitTransactions(txs) } catch (e) { if (String(e).includes('single transaction')) { for (const tx of txs) await commitTransactions([tx]) } else { throw e } }

Prevention

When it happens

Trigger: 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.

Common situations: Plugin or integration code batches transactions; a frontend retry accumulates several plan transactions in one payload.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/1c50221bf35550a2. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/template_doc_tree.go:456

		"title":    node.Title,
		"id":       node.ID,
		"parentID": node.ParentID,
		"rootID":   node.RootID,
		"hPath":    node.HPath,
		"name":     "",
		"alias":    "",
	}
}

// AttachTemplateDocTreePlans 将一次性计划转换为内核事务操作,父文档内容与全部子文档共用一条撤销记录。
func AttachTemplateDocTreePlans(transactions []*Transaction) (attached bool, err error) {
	var target *Transaction
	for _, transaction := range transactions {
		if nil == transaction || "" == transaction.TemplateDocTreePlanID {
			continue
		}
		if nil != target || 1 != len(transactions) {
			return false, errors.New("a document tree plan must be applied in a single transaction")
		}
		target = transaction
	}
	if nil == target {
		return false, nil
	}
	if target.isReplay {
		return false, errors.New("template document tree plans cannot be attached to replay transactions")
	}
	if 0 == len(target.DoOperations) || 0 == len(target.UndoOperations) {
		return false, errors.New("template document tree plan requires reversible parent operations")
	}
	if err = validateTemplateDocTreeParentOperations(target); nil != err {
		return false, err
	}
	for _, operation := range append(append([]*Operation{}, target.DoOperations...), target.UndoOperations...) {
		if nil != operation && ("restoreCreatedDoc" == operation.Action || "removeCreatedDoc" == operation.Action) {
			return false, errors.New("template document tree transaction contains a reserved operation")

View on GitHub (pinned to 9f775e8a12)