BigPizzaV3/CodexPlusPlus · error

历史消息所属轮次不匹配

Error message

历史消息所属轮次不匹配

What it means

While mapping `page.data` rows from `thread/items/list` inside readNativeHistory, any row carrying a non-null `turnId` that differs from the turn currently being fetched throws this error. It is an internal-consistency check: items from another turn must never appear in this turn's page, so a mismatch means the wire contract or the cursor/pagination state is broken.

Solutions

  1. Clear the plugin's conversation-history cache (partial pages keyed `${threadId}:${turnId}:partial`) and re-run the history read.
  2. Log the offending row's `turnId` vs the expected `turn.id` to confirm whether the backend returns foreign-turn items.
  3. Update the plugin to match the installed Codex Desktop build if `turnId` semantics changed.
  4. If the backend legitimately returns thread-scoped rows, relax the check to trust the API's turn scoping instead of throwing.

Example fix

// before
if(row.turnId!=null&&row.turnId!==turn.id)throw Error('历史消息所属轮次不匹配');
// after
if(row.turnId!=null&&row.turnId!==turn.id){console.warn('skipping foreign-turn item',row.turnId,'!=',turn.id);continue;}
Defensive patterns

Strategy: validation

Validate before calling

for(const row of page.data){if(row.turnId!=null&&row.turnId!==turn.id){console.warn('foreign turnId',row.turnId,'expected',turn.id);continue;}}

Type guard

function rowBelongsToTurn(row,turnId){return row?.turnId==null||row.turnId===turnId;}

Try / catch

try{messages=await readNativeHistory(send,threadId,cache);}catch(e){if(/轮次不匹配/.test(e.message)){await cache.clear?.();return retryOnce();}throw e;}

Prevention

When it happens

Trigger: A row returned by `thread/items/list` for `turnId: turn.id` has `row.turnId` set to a different turn's id — e.g. the backend changed `turnId` semantics, returns thread-scoped items without honoring the turnId filter, or a cached partial page (`${key}:partial`) was stored under the wrong turn and resumed with the wrong cursor.

Common situations: Codex Desktop update changed the items payload so `turnId` now references a parent/branch turn; corrupted or stale partial cache entries mixing turns; custom relay reordering or merging items across turns.

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 BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/96c0b1677c7f131c. Report an issue: GitHub.

Appendix: source

Thrown at tools/conversation-canvas/native-history.mjs:48

    if(cached&&turn.status==='completed')parsed=cached;
    else{
      const partial=turn.status==='completed'?await cache.get(`${key}:partial`):null;
      parsed=partial?.messages?.slice()||[];let itemCursor=partial?.cursor??null,limit=32;const cursors=new Set();
      do{
        signal?.throwIfAborted();
        if(cursors.has(itemCursor))throw Error('历史消息分页游标重复');
        let page;
        for(;;){
          try{page=await send('thread/items/list',{threadId,turnId:turn.id,cursor:itemCursor,limit,sortDirection:'asc'});break;}
          catch(error){
            if(limit>1&&/decoded message length too large/i.test(error.message)){limit=Math.max(1,Math.floor(limit/2));continue;}
            throw error;
          }
        }
        if(!Array.isArray(page?.data))throw Error('历史消息接口返回格式无效');
        cursors.add(itemCursor);
        for(const row of page.data){
          if(row.turnId!=null&&row.turnId!==turn.id)throw Error('历史消息所属轮次不匹配');
          const item=row.item??row;
          if(!['userMessage','agentMessage'].includes(item.type))continue;
          if(typeof item.id!=='string')throw Error('历史消息缺少稳定 ID');
          const role=item.type==='userMessage'?'user':'assistant';
          const content=role==='user'?(item.content??[]):[{text:item.text??''}];
          const message=parseMessage({type:'response_item',payload:{type:'message',id:item.id,role,phase:role==='assistant'?(item.phase??'final_answer'):'',content}},item.id);
          if(message)parsed.push({...message,turnId:turn.id,turnStatus:turn.status});
        }
        itemCursor=page.nextCursor??null;
        onProgress(`正在读取历史 ${index+1}/${turns.length} · 已读 ${parsed.length} 条消息…`);
        if(turn.status==='completed'&&itemCursor!==null)await cache.set(`${key}:partial`,{messages:parsed,cursor:itemCursor});
      }while(itemCursor!==null);
      if(turn.status==='completed'){await cache.set(key,parsed);await cache.delete(`${key}:partial`);}
    }
    // Older completed-turn caches contain text and IDs but no turn metadata.
    // The current directory and cache key provide the authoritative owner.
    for(const message of parsed)if(!seen.has(message.id)){seen.add(message.id);messages.push({...message,turnId:turn.id,turnStatus:turn.status});}
  }

View on GitHub (pinned to b1ed92e5e4)