Prevent subagent repair from corrupting background sessions

Guard the tool-pair validator when transformed assistant messages belong to a tracked subagent session, while keeping normal main-session orphaned tool_use repair intact.

Constraint: code-yeongyu/oh-my-openagent#3996 reports background Oracle sessions hanging after synthetic placeholder tool_result insertion.
Rejected: disable tool-pair-validator globally | would regress main-session compaction/orphaned tool_use repair.
Confidence: high
Scope-risk: narrow
Directive: Keep subagent skip coverage and normal repair coverage together when changing tool-pair validation.
Tested: bun test src/hooks/tool-pair-validator/hook.test.ts src/plugin/messages-transform.test.ts
Tested: bun run typecheck
Not-tested: live Oracle background task e2e; no remote push or PR comment performed.
This commit is contained in:
PeterPonyu
2026-05-14 21:32:48 -04:00
parent c6c7a103dd
commit 17030b9a0d
2 changed files with 59 additions and 2 deletions
+15 -1
View File
@@ -1,5 +1,6 @@
import type { Message, Part } from "@opencode-ai/sdk"
import { subagentSessions } from "../../features/claude-code-session-state"
import { log } from "../../shared/logger"
const TOOL_RESULT_PLACEHOLDER = "Tool output unavailable (context compacted)"
@@ -134,6 +135,11 @@ function getMessageID(message: TransformMessageInfo): string | undefined {
return typeof candidate.id === "string" ? candidate.id : undefined
}
function getMessageSessionID(message: TransformMessageInfo): string | undefined {
const candidate = message as { sessionID?: unknown }
return typeof candidate.sessionID === "string" ? candidate.sessionID : undefined
}
function repairMissingToolResults(messages: MessageWithParts[], assistantIndex: number): void {
const assistantMessage = messages[assistantIndex]
const toolUseIDs = extractUniqueToolUseIDs(assistantMessage.parts)
@@ -173,7 +179,15 @@ export function createToolPairValidatorHook(): MessagesTransformHook {
return {
"experimental.chat.messages.transform": async (_input, output) => {
for (let i = 0; i < output.messages.length; i++) {
if (output.messages[i].info.role !== "assistant") {
const messageInfo = output.messages[i].info
if (messageInfo.role !== "assistant") {
continue
}
const sessionID = getMessageSessionID(messageInfo)
if (sessionID && subagentSessions.has(sessionID)) {
log("[tool-pair-validator] Skipping repair for subagent session", { sessionID })
continue
}