{"record":{"id":"05203ec85735668a","repo":"BigPizzaV3/CodexPlusPlus","slug":"long-organizer-05203e","errorCode":null,"errorMessage":"本批改变了共同目标根节点","messagePattern":"本批改变了共同目标根节点","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tools/conversation-canvas/long-organizer.mjs","lineNumber":101,"sourceCode":"  for(const n of value.upserts){\n    if(!n||typeof n.id!=='string'||patch.has(n.id)||(!allowed.has(n.id)&&(!n.id.startsWith(prefix)||old.has(n.id))))throw Error('本批节点 ID 无效或覆盖了未提供的节点');\n    if(!Array.isArray(n.sources)||!n.sources.length||!n.sources.every(id=>parts.has(id)))throw Error('本批引用了不存在的消息片段');\n    for(const field of ['title','summary','description','status'])if(typeof n[field]!=='string'||n[field].length>3000)throw Error('本批节点描述过长或无效');\n    const previous=old.get(n.id),evidence=[...(previous?.evidence||[])];\n    for(const id of new Set(n.sources)){covered.add(id);const {messageId,start,end,digest}=parts.get(id);evidence.push({messageId,start,end,digest});}\n    const revisions=[...(previous?.revisions||[])];\n    if(previous&&['summary','description','status'].some(k=>previous[k]!==n[k]))revisions.push({batch:state.batchCount,summary:previous.summary,description:previous.description,status:previous.status});\n    patch.set(n.id,{...n,sources:[...new Set([...(previous?.sources||[]),...n.sources.map(id=>parts.get(id).messageId)])],evidence,revisions});\n  }\n  if(covered.size!==batch.length){\n    const error=new Error('本批仍有未归入任务树的消息片段，进度未提交');\n    error.missingPartIds=batch.filter(part=>!covered.has(part.partId)).map(part=>part.partId);\n    throw error;\n  }\n  for(const n of patch.values())if(n.parent!==null&&!allowed.has(n.parent)&&!patch.has(n.parent)&&old.get(n.id)?.parent!==n.parent)throw Error('本批父节点未提供或不存在');\n  const nodes=state.nodes.map(n=>patch.get(n.id)||n);for(const [id,n] of patch)if(!old.has(id))nodes.push(n);\n  const oldRoot=state.nodes.find(n=>n.parent===null);\n  if(oldRoot&&nodes.find(n=>n.parent===null)?.id!==oldRoot.id)throw Error('本批改变了共同目标根节点');\n  const currentNodeId=value.currentNodeId??state.currentNodeId??null;\n  validateOrganization({requestId,nodes,currentNodeId},[...new Set(nodes.flatMap(n=>n.sources))].map(id=>({id})),requestId);\n  return {...state,schemaVersion:2,nodes,currentNodeId,batchCount:state.batchCount+1,done:{...state.done,...Object.fromEntries(batch.map(p=>[p.partId,true]))}};\n}\n\n// Save is an atomic durable checkpoint. A failed model call never commits its\n// coverage; a restart can resume its pending side-chat turn before resubmitting.\nexport async function organizeLong({messages,record,save,run,signal,progress=()=>{},onGraph=()=>{},compact=false}){\n  const history=await prepareHistory(messages,signal);\n  const partIndex=new Map(history.parts.map(part=>[part.partId,part]));\n  const previous=record?.published;\n  let job=record?.job;\n  if(!job||!compatible(job.state.manifest,history.manifest)){\n    const incremental=previous?.done&&compatible(previous.manifest,history.manifest);\n    const state=incremental?{...previous,manifest:history.manifest}:{schemaVersion:2,nodes:[],currentNodeId:null,batchCount:0,done:{},manifest:history.manifest};\n    job={kind:incremental||!previous?.nodes?.length?'incremental':'rebuild',state,session:{},pending:null};\n  }else job={...job,state:{...job.state,manifest:history.manifest}};\n  let document={version:3,published:previous??null,job};","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/tools/conversation-canvas/long-organizer.mjs#L83-L119","documentation":"mergeBatch enforces a single shared root node (parent===null). If state already has a root and the merged node set's root differs from it, the whole batch is rejected. This prevents the model from replacing the conversation's common goal root mid-job.","triggerScenarios":"An upsert changes the existing root's parent, or a new node with parent:null is introduced while an old root exists (e.g. old root re-parented under a new node).","commonSituations":"Model tries to 're-organize' around a new goal node; resume flow where the model rewrites the hierarchy from scratch.","solutions":["Keep the original root node with parent:null unchanged in every batch","Nest all changes under the existing root instead of creating a new one","If a new goal is truly needed, restart organizeLong rather than mutating the root mid-run","Instruct the model that the root id is immutable"],"exampleFix":"// before\n{ id: 'new-goal', parent: null }\n// after\n{ id: 'existing-root-id', parent: null, summary: updatedSummary }","handlingStrategy":"validation","validationCode":"if (oldRoot && upserts.some(n => n.parent === null && n.id !== oldRoot.id)) throw new Error('root change attempted');","typeGuard":null,"tryCatchPattern":"try { mergeBatch(...) } catch (e) { if (e.message === '本批改变了共同目标根节点') { rejectRootChange(); } else throw e; }","preventionTips":["Treat the root node id as immutable across batches","Nest updates under the existing root","Never emit a second parent:null node","Document the root invariant in the task prompt"],"tags":["validation","tree-structure","state-invariant"],"backgroundTag":"invalid-state-transition","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}