{"record":{"id":"4cec3822c85b360d","repo":"siyuan-note/siyuan","slug":"createdoctree-exceeds-the-maximum-depth-of-d","errorCode":null,"errorMessage":"createDocTree exceeds the maximum depth of %d","messagePattern":"createDocTree exceeds the maximum depth of (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":123,"sourceCode":"func parseTemplateDocTreeDefinition(def any) ([]*TemplateDocTreeNode, error) {\n\tstate := &templateDocTreeParseState{}\n\tnodes, err := state.parseNodes(def, 1)\n\tif nil != err {\n\t\treturn nil, err\n\t}\n\tif 0 == len(nodes) {\n\t\treturn nil, errors.New(\"createDocTree requires at least one document\")\n\t}\n\treturn nodes, nil\n}\n\ntype templateDocTreeParseState struct {\n\tcount int\n}\n\nfunc (state *templateDocTreeParseState) parseNodes(value any, depth int) ([]*TemplateDocTreeNode, error) {\n\tif maxTemplateDocTreeDepth < depth {\n\t\treturn nil, fmt.Errorf(\"createDocTree exceeds the maximum depth of %d\", maxTemplateDocTreeDepth)\n\t}\n\tvalues, ok := value.([]any)\n\tif !ok {\n\t\treturn nil, errors.New(\"createDocTree definition must be a list\")\n\t}\n\tif 0 == len(values) {\n\t\treturn nil, errors.New(\"createDocTree document list must not be empty\")\n\t}\n\n\tnodes := make([]*TemplateDocTreeNode, 0, len(values))\n\tfor _, value := range values {\n\t\tdefinition, ok := value.(map[string]any)\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"createDocTree document must be a dictionary\")\n\t\t}\n\t\tfor key := range definition {\n\t\t\tswitch key {\n\t\t\tcase \"title\", \"template\", \"define\", \"children\":","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L105-L141","documentation":"parseNodes enforces maxTemplateDocTreeDepth (16) when recursing into nested 'children' arrays of the createDocTree definition. If the nesting level exceeds this limit, parsing aborts with a formatted error naming the maximum. This protects the kernel from deeply recursive definitions causing unbounded work.","triggerScenarios":"Submitting a createDocTree definition whose children arrays nest more than 16 levels deep, e.g. a generated outline with 17+ nested subsections.","commonSituations":"Programmatic generation of very deep document hierarchies (converted file trees, deeply nested mind maps); recursive generators without a depth cap; hand-built structures that grew over time.","solutions":["Flatten or split the hierarchy so nesting stays within 16 levels; create additional root docs instead.","Add a depth guard in the code that generates the definition and split oversized trees into multiple createDocTree calls.","If depth 16 is genuinely needed, split the tree: create top levels first, then attach deeper subtrees under the newly created docs."],"exampleFix":"// before\nbuildChildren(items, depth) // recurses unbounded, depth 20\n// after\nconst MAX = 16;\nif (depth > MAX) { splitIntoSeparateCall(items); return; }","handlingStrategy":"validation","validationCode":"function depthOf(def, d = 1) {\n  return def.reduce((m, n) => Math.max(m, Array.isArray(n.children) ? depthOf(n.children, d + 1) : d), d);\n}\nif (depthOf(def) > 16) throw new Error(\"definition exceeds max depth 16\");","typeGuard":null,"tryCatchPattern":"try {\n  await createDocTree(payload);\n} catch (e) {\n  if (/exceeds the maximum depth/.test(String(e))) {\n    // split the tree into multiple calls rooted at shallower docs\n  } else { throw e; }\n}","preventionTips":["Cap nesting depth in any generator producing the definition","Prefer flattening hierarchies beyond ~10 levels for usability anyway","Split very deep trees across multiple createDocTree invocations attached to intermediate docs"],"tags":["limit-exceeded","recursion","validation"],"backgroundTag":"value-out-of-range","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"}