perf(reconnect): pre-index toolCalls and remove unreachable guard (#4811)

This commit is contained in:
Rod Boev
2026-06-24 10:17:52 -04:00
committed by nesquena-hermes
parent e86ebb6379
commit f330fa7f44
2 changed files with 13 additions and 5 deletions
+10 -3
View File
@@ -2551,6 +2551,15 @@ function attachLiveStream(activeSid, streamId, uploaded=[], options={}){
if(!byIdx.has(idx)) byIdx.set(idx,[]);
byIdx.get(idx).push(row);
};
// Pre-index S.toolCalls by assistant_msg_idx for O(m+n) lookup
const toolsByIdx=new Map();
if(S.toolCalls) for(const tc of S.toolCalls){
const ti=typeof tc.toolIdx==='number'? tc.toolIdx : parseInt(tc.assistant_msg_idx,10);
if(Number.isFinite(ti)){
if(!toolsByIdx.has(ti)) toolsByIdx.set(ti,[]);
toolsByIdx.get(ti).push(tc);
}
}
let encounter=0;
for(let idx=turnStart+1;idx<lastAsstIndex;idx+=1){
const message=messages[idx];
@@ -2575,12 +2584,10 @@ function attachLiveStream(activeSid, streamId, uploaded=[], options={}){
if(tid) seenToolIds.add(tid);
}
// Merge S.toolCalls for this index, dedup by tool id
const toolCalls=Array.isArray(S.toolCalls)?S.toolCalls:[];
for(const tool of toolCalls){
for(const tool of (toolsByIdx.get(idx)||[])){
if(!tool||typeof tool!=='object') continue;
const toolIdx=Number(tool.assistant_msg_idx);
if(!Number.isFinite(toolIdx)||toolIdx!==idx) continue;
if(toolIdx<=turnStart||toolIdx>=lastAsstIndex) continue;
const row=_anchorSceneToolRowFromCall(tool,0,idx);
const tid=row.tool_call_id||(row.tool&&row.tool.id);
if(tid&&seenToolIds.has(tid)) continue;
@@ -709,8 +709,9 @@ def test_settled_anchor_scene_persists_the_full_assistant_turn_not_only_tail():
assert "messages.slice(turnStart+1,lastAsstIndex+1)" in complete
assert "message.reasoning||message._reasoning||message.reasoning_content||message.thinking" in reasoning_text
assert "const reasoning=_anchorSceneMessageReasoningText(message);" in rows_by_message
assert "const toolCalls=Array.isArray(S.toolCalls)?S.toolCalls:[]" in rows_by_message
assert "toolIdx<=turnStart||toolIdx>=lastAsstIndex" in rows_by_message
assert "const toolsByIdx=new Map();" in rows_by_message
assert "if(S.toolCalls) for(const tc of S.toolCalls){" in rows_by_message
assert "for(const tool of (toolsByIdx.get(idx)||[]))" in rows_by_message
assert "_anchorSceneToolRowFromCall(tool,0,idx)" in rows_by_message