{"record":{"id":"76bdf75573550bf3","repo":"siyuan-note/siyuan","slug":"createdoctree-exceeds-the-maximum-document-count-o","errorCode":null,"errorMessage":"createDocTree exceeds the maximum document count of %d","messagePattern":"createDocTree exceeds the maximum document count of (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":177,"sourceCode":"\t\tif 512 < utf8.RuneCountInString(title) {\n\t\t\treturn nil, fmt.Errorf(\"createDocTree document title exceeds %d characters\", 512)\n\t\t}\n\n\t\ttemplateName, err := templateDocTreeStringField(definition, \"template\")\n\t\tif nil != err {\n\t\t\treturn nil, err\n\t\t}\n\t\tdefineName, err := templateDocTreeStringField(definition, \"define\")\n\t\tif nil != err {\n\t\t\treturn nil, err\n\t\t}\n\t\tif \"\" != templateName && \"\" != defineName {\n\t\t\treturn nil, errors.New(\"createDocTree document template and define are mutually exclusive\")\n\t\t}\n\n\t\tstate.count++\n\t\tif maxTemplateDocTreeDocs < state.count {\n\t\t\treturn nil, fmt.Errorf(\"createDocTree exceeds the maximum document count of %d\", maxTemplateDocTreeDocs)\n\t\t}\n\t\tid := ast.NewNodeID()\n\t\tnode := &TemplateDocTreeNode{\n\t\t\tID:       id,\n\t\t\tRootID:   id,\n\t\t\tTitle:    title,\n\t\t\tDepth:    depth,\n\t\t\tTemplate: templateName,\n\t\t\tDefine:   defineName,\n\t\t}\n\t\tif childrenValue, exists := definition[\"children\"]; exists {\n\t\t\tchildren, parseErr := state.parseNodes(childrenValue, depth+1)\n\t\t\tif nil != parseErr {\n\t\t\t\treturn nil, parseErr\n\t\t\t}\n\t\t\tnode.Children = children\n\t\t}\n\t\tnodes = append(nodes, node)","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/model/template_doc_tree.go#L159-L195","documentation":"The createDocTree template action declares more than 128 documents (maxTemplateDocTreeDocs) across the whole tree, including all nested children. During parsing of the definition, a running counter increments per document node; once the 129th document is seen, parsing aborts and returns this error instead of creating a plan. It is a hard safety limit to prevent a template from flooding the notebook with documents.","triggerScenarios":"Calling createDocTree with a definition array (nested via \"children\") whose total flattened node count exceeds 128; also hit indirectly by renderTemplateSource when the template's createDocTree calls plus children exceed the cap.","commonSituations":"Generating a doc tree for a large project/outline (e.g. a 12-month plan with children per week), bulk-importing a sitemap into docs via a template, or looping/generator logic in the template that emits hundreds of docs.","solutions":["Reduce the number of documents in the createDocTree definition to 128 or fewer (merge children or drop levels).","Split into multiple template insertions, each creating a partial tree under a different parent.","Create documents programmatically via the API (/api/filetree/createDocWithMkDirs) in batches instead of one template."],"exampleFix":"// before\ncreateDocTree(.{\"title\":\"Week \" . $i . \"\",\"children\":[...25 docs...]}) x 10  // 250+ docs\n// after\ncreateDocTree with at most 128 total docs, or split across two template inserts","handlingStrategy":"validation","validationCode":"function countDocs(def) {\n  return def.reduce((n, d) => n + 1 + (Array.isArray(d.children) ? countDocs(d.children) : 0), 0);\n}\nif (countDocs(definition) > 128) throw new Error(\"createDocTree definition exceeds 128 documents\");","typeGuard":"const isDocList = (v) => Array.isArray(v) && v.every((d) => d && typeof d === \"object\");","tryCatchPattern":"try {\n  await insertTemplateWithDocTree(def);\n} catch (e) {\n  if (String(e).includes(\"exceeds the maximum document count\")) {\n    // split the definition into batches of <= 128 docs\n  }\n}","preventionTips":["Count flattened nodes (including children) before rendering a template","Keep generated doc trees small; use API-based batch creation for large sets","Add a unit test asserting generated definitions stay within 128 docs"],"tags":["template","limit-exceeded","document-tree"],"backgroundTag":"value-out-of-range","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}