You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The count variable in extractNewTurnMessages is declared but never incremented, so newCount is always 0. This affects diagnostics and runLocalPrecheck which rely on accurate new message count.
letcount=0;consteffectiveStartIndex=findEffectiveTurnStartIndex(messages,startIndex);// First pass: collect toolUse inputs indexed by toolCallId/toolUseId// Scan all messages (including after startIndex) to find toolUse before each toolResultconsttoolUseInputs: Record<string,Record<string,unknown>>={};for(leti=0;i<messages.length;i++){constmsg=messages[i]asRecord<string,unknown>;if(!msg||typeofmsg!=="object")continue;constrole=msg.roleasstring;if(role==="assistant"){constcontent=msg.content;if(Array.isArray(content)){for(constblockofcontent){constb=blockasRecord<string,unknown>;// Handle toolCall, toolUse, tool_call typesif(b?.type==="toolCall"||b?.type==="toolUse"||b?.type==="tool_call"){constid=(b.idasstring)||(b.toolUseIdasstring)||(b.toolCallIdasstring);// Try multiple field names for tool input: arguments, input, toolInputconstinput=b.arguments??b.input??b.toolInput;if(id&&input&&typeofinput==="object"){toolUseInputs[id]=inputasRecord<string,unknown>;}}}}}}for(leti=effectiveStartIndex;i<messages.length;i++){constmsg=messages[i]asRecord<string,unknown>;if(!msg||typeofmsg!=="object")continue;constrole=msg.roleasstring;if(!role||role==="system")continue;count++;// toolResult -> type: "tool"if(role==="toolResult"){consttoolName=typeofmsg.toolName==="string" ? msg.toolName : "tool";constoutput=formatToolResultContent(msg.content)||"";// Try multiple field names for tool call IDconsttoolCallId=(msg.toolCallIdasstring)||(msg.toolUseIdasstring)||(msg.tool_call_idasstring);consttoolInput=toolCallId&&toolUseInputs[toolCallId]
? toolUseInputs[toolCallId]
: (typeofmsg.toolInput==="object"&&msg.toolInput!==null
? msg.toolInputasRecord<string,unknown>
: undefined);if(output){pushExtractedPart(result,"user",{type: "tool",toolCallId: toolCallId||undefined,
toolName,
toolInput,toolOutput: `[${toolName} result]: ${output}`,toolStatus: "completed",});}continue;}// user/assistant -> type: "text"consttext=extractSingleMessageText(msg);if(text){constcleanedText=role==="assistant"
? (HEARTBEAT_RE.test(text) ? "" : text.trim())
: sanitizeUserTextForCapture(text);if(cleanedText){constovRole: "user"|"assistant"=role==="assistant" ? "assistant" : "user";pushExtractedPart(result,ovRole,{type: "text",text: cleanedText,});}}}return{messages: result,newCount: count, effectiveStartIndex };
Tool result output is now wrapped in [${toolName} result]: ${output} without mention in PR summary. This may break existing extraction logic that expects raw tool output.
Break the backward loop when encountering a non-user, non-system role (e.g., "assistant", "toolResult") to prevent capturing user messages from earlier turns. This ensures we only look back within the current turn boundary.
for (let i = firstRelevantIndex - 1; i >= 0; i -= 1) {
const msg = messages[i] as Record<string, unknown>;
const role = typeof msg?.role === "string" ? msg.role : "";
if (!role || role === "system") {
continue;
}
if (role === "user") {
let earliestUserIndex = i;
for (let j = i - 1; j >= 0; j -= 1) {
const prev = messages[j] as Record<string, unknown>;
const prevRole = typeof prev?.role === "string" ? prev.role : "";
if (!prevRole || prevRole === "system") {
continue;
}
if (prevRole !== "user") {
break;
}
earliestUserIndex = j;
}
return earliestUserIndex;
}
+ // Stop at the first non-user, non-system message (previous turn boundary)+ break;
}
Suggestion importance[1-10]: 7
__
Why: Adding a break when encountering a non-user, non-system role prevents capturing user messages from earlier turns, ensuring correct turn boundary detection.
Medium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
prePromptMessageCountstarts on the assistant side of the turn<relevant-memories>stripping while preserving assistant textCloses #1248
Testing
pnpm vitest run tests/ut/context-engine-afterTurn.test.ts