siyuan-note/siyuan · error

template document tree plan contains an invalid document…

Error message

template document tree plan contains an invalid document snapshot

What it means

AttachTemplateDocTreePlans validates every pre-rendered document tree snapshot stored in a template doc-tree plan before appending restoreCreatedDoc operations to the parent transaction. A tree fails validation if it is nil, has no Root, its ID does not match its Root.ID, its Box does not match the plan's box, or a file already exists at tree.Path in that notebook. This is an internal invariant check that the plan snapshot is a coherent, not-yet-created document.

Solutions

  1. Re-render the template doc-tree plan to obtain fresh snapshots, then re-attach it in one atomic step
  2. Check box.Exist(tree.Path) for each planned document before attaching and remove or rename conflicting paths
  3. Ensure the plan is not reused after expiry; call AttachTemplateDocTreePlans promptly after rendering
  4. Do not mutate plan.trees between render and attach; treat the plan as read-only

Example fix

// before: attaching a stale plan after the doc was already created elsewhere
attached, err := AttachTemplateDocTreePlans(target, planID)
// after: verify no path conflicts before attaching
box := Conf.Box(plan.boxID)
for _, tree := range plan.trees {
    if box.Exist(tree.Path) {
        return fmt.Errorf("planned doc path already exists: %s", tree.Path)
    }
}
attached, err := AttachTemplateDocTreePlans(target, planID)
Defensive patterns

Strategy: validation

Validate before calling

box := Conf.Box(plan.boxID)
for _, tree := range plan.trees {
    if tree == nil || tree.Root == nil || tree.ID != tree.Root.ID || tree.Box != plan.boxID || box.Exist(tree.Path) {
        return errors.New("invalid plan snapshot")
    }
}

Type guard

func isValidTreeSnapshot(tree *parse.Tree, boxID string) bool {
    return tree != nil && tree.Root != nil && tree.ID == tree.Root.ID && tree.Box == boxID
}

Prevention

When it happens

Trigger: Calling AttachTemplateDocTreePlans with a plan whose plan.trees slice contains a nil *parse.Tree, a tree without a Root node, a tree whose ID differs from tree.Root.ID, a tree whose Box field differs from plan.boxID, or a tree whose Path already exists on disk in the target notebook (box.Exist(tree.Path) true).

Common situations: A stale plan is attached after the target path was created by another transaction or sync run; concurrent operations in the same notebook created the file between rendering and attachment; corrupted in-memory plan state from a bug or from reusing an expired/mutated plan object.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/template_doc_tree.go:503

	if !ok || plan.id != planID || time.Now().After(plan.expiresAt) {
		return false, errors.New("template document tree plan is invalid or has expired")
	}
	if !transactionTargetsTemplateRoot(target, plan.rootID, plan.boxID) {
		return false, errors.New("template document tree plan does not match the edited document")
	}
	rootTree, loadErr := LoadTreeByBlockID(plan.rootID)
	if nil != loadErr || nil == rootTree || rootTree.Box != plan.boxID || rootTree.Path != plan.rootPath ||
		rootTree.HPath != plan.rootHPath {
		return false, errors.New("the document used to render the template has changed")
	}
	target.templateDocTreeRootSnapshot = rootTree
	box := Conf.Box(plan.boxID)
	if nil == box {
		return false, ErrBoxNotFound
	}
	for _, tree := range plan.trees {
		if nil == tree || nil == tree.Root || tree.ID != tree.Root.ID || tree.Box != plan.boxID || box.Exist(tree.Path) {
			return false, errors.New("template document tree plan contains an invalid document snapshot")
		}
	}

	for _, tree := range plan.trees {
		target.DoOperations = append(target.DoOperations, &Operation{
			Action:                "restoreCreatedDoc",
			ID:                    tree.ID,
			Tree:                  tree,
			templateDocTreeRootID: plan.rootID,
		})
	}
	for index := len(plan.trees) - 1; 0 <= index; index-- {
		tree := plan.trees[index]
		target.UndoOperations = append(target.UndoOperations, &Operation{
			Action:                "removeCreatedDoc",
			ID:                    tree.ID,
			Tree:                  tree,
			templateDocTreeRootID: plan.rootID,

View on GitHub (pinned to 9f775e8a12)