Repository navigation
fix(agents): OpenAI function-call replay — preserve malformed arguments instead of silent {} replacement - #61527
100yenadmin wants to merge 8 commits into
Conversation
Greptile SummaryThis PR makes two distinct changes to
Key issues:
Confidence Score: 3/5The core argument-preservation fix is logically correct, but a dead-code lint failure under pnpm check must be resolved before landing. The malformed-argument fix is sound and the replay path already handles the returned string. However, removing hasExplicitBlockPhase from the blockPhase computation while leaving its full computation block in place creates an unused variable that oxlint will flag, breaking pnpm check. Additionally, the PR description overstates test coverage — there is no regression test for the primary fix's JSON.parse fallback path. src/agents/openai-ws-message-conversion.ts lines 332–341 (dead hasExplicitBlockPhase computation), src/agents/openai-ws-stream.test.ts (missing regression test for buildAssistantMessageFromResponse malformed-arg path)
|
There was a problem hiding this comment.
Pull request overview
Fixes silent data loss during OpenAI WS tool-call replay by preserving the original (possibly malformed) function_call.arguments payload instead of replacing it with {}.
Changes:
- Preserve raw
function_call.argumentsstring onJSON.parsefailure inbuildAssistantMessageFromResponse(). - Adjust replay phase inheritance for assistant text blocks so untagged blocks inherit the message phase.
- Update an existing replay test expectation to reflect the new phase-inheritance behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/agents/openai-ws-message-conversion.ts | Preserves malformed tool-call arguments for replay; changes assistant block phase inheritance during replay conversion. |
| src/agents/openai-ws-stream.test.ts | Updates a replay test expectation related to phase splitting/inheritance. |
|
Related issues: This PR also relates to #55378 (Telegram forum topics show pre-tool narration/work-note text as visible replies), #61734 (commentary-phase text leaks into user-visible output), and #25592 (text between tool calls leaks to messaging channels). These share the same root cause of phase-suppression logic not catching all commentary paths. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
src/agents/subagent-orphan-recovery.ts:303
OrphanRecoveryDepsadds a DI seam, butscheduleOrphanRecovery()still callsrecoverOrphanedSubagentSessions({ ...params, resumedSessionKeys }), which can reintroduce non-serializable fields (likegetActiveRuns) and makes it easy to accidentally pass the scheduler’sdelayMs/maxRetriesintorecoverOrphanedSubagentSessions()(which doesn’t use them). Consider explicitly passing only the fieldsrecoverOrphanedSubagentSessionsexpects (getActiveRuns, resumedSessionKeys, deps) to avoid accidental coupling as the param shapes diverge.
export function scheduleOrphanRecovery(params: {
getActiveRuns: () => Map<string, SubagentRunRecord>;
delayMs?: number;
maxRetries?: number;
deps?: OrphanRecoveryDeps;
}): void {
const initialDelay = params.delayMs ?? DEFAULT_RECOVERY_DELAY_MS;
const maxRetries = params.maxRetries ?? MAX_RECOVERY_RETRIES;
const resumedSessionKeys = new Set<string>();
const attemptRecovery = (attempt: number, delay: number) => {
setTimeout(() => {
void recoverOrphanedSubagentSessions({ ...params, resumedSessionKeys })
.then((result) => {
- Move notifyContextEngineSubagentEnded after sessions.delete in sweep to avoid duplicate notifications when session is still live - Add 5-minute ORPHAN_GRACE_MS window to resolveSubagentRunOrphanReason to prevent false-positive pruning of legitimate in-flight sessions - Thread loadConfig/callGateway through OrphanRecoveryDeps in subagent-orphan-recovery.ts instead of importing directly, enabling test injection via the existing DI pattern Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The catch block returns item.arguments (a string) but the ToolCall type expects Record<string, unknown>. Add a type assertion to satisfy TypeScript while preserving the raw string at runtime. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Closing as not reviewable in its current shape. The title says malformed function-call arguments preservation, but the branch still carries two other tracks: the replay phase-inheritance hunk already covered by #61529, and orphan-recovery / subagent-registry changes that belong with #61801. That overlap makes this PR hard to reason about and easy to land partially by accident. The malformed-arguments fix itself also still lacks its own focused regression test. If that bug still needs a patch after #61529 / #61801 settle, please reopen as a minimal malformed-arguments-only PR with the round-trip test. |
|
Thanks @steipete — fair points on the scope creep. The branch did carry phase-inheritance changes (covered by #61529) and orphan-recovery changes (covered by #61801). Agreed that mixing three tracks made it unreviewable. One correction: the PR did include a focused regression test for the malformed-arguments fix ( The |
What this fixes (plain English)
When a provider sends malformed (unparseable) JSON in tool call arguments, the system was silently replacing the original content with an empty object
{}. This destroyed the actual payload, making debugging impossible and causing replay failures. Now the original malformed string is preserved as-is.Technical details
Root cause: In
buildAssistantMessageFromResponse(), the catch block forJSON.parse(item.arguments)replaced the raw string with{}on failure. On replay,JSON.stringify({})produces"{}"instead of the original content.Fix:
{}Files changed:
src/agents/openai-ws-message-conversion.ts— preserve raw arguments on parse failuresrc/agents/openai-ws-stream.ts— defer ws text flush until phase is knownsrc/agents/openai-ws-stream.test.ts— regression test for malformed argument preservation; corrected phase-buffering test for requireKnownPhase behaviorsrc/agents/pi-embedded-helpers.validate-turns.test.ts— updated test fixturessrc/agents/subagent-orphan-recovery.ts— orphan reconciliation DI seamsrc/agents/subagent-registry-helpers.ts— orphan grace window helpersrc/agents/subagent-registry.ts— sweep ordering and overlap guardRelated
Test plan