From 75cb0bd80e029b962ebec37d8fde792b2786f9cc Mon Sep 17 00:00:00 2001 From: KazenDev Date: Thu, 6 Aug 2026 00:43:56 +0200 Subject: [PATCH] fix(sdk): handle cyclic run-state on session resume --- sdk/src/run-state.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/sdk/src/run-state.ts b/sdk/src/run-state.ts index ea226e94ac..b26aaa5070 100644 --- a/sdk/src/run-state.ts +++ b/sdk/src/run-state.ts @@ -790,10 +790,19 @@ export async function applyOverridesToSessionState( maxAgentSteps?: number }, ): Promise { - // Deep clone to avoid mutating the original session state - const sessionState = JSON.parse( - JSON.stringify(baseSessionState), - ) as SessionState + // Deep clone to avoid mutating the original session state. A JSON + // round-trip is the fast path, but run state can carry cyclic values + // (e.g. recursive zod schemas inside tool blocks) that make + // JSON.stringify throw ("cannot serialize cyclic structures"); lodash + // cloneDeep handles cycles, so fall back to it. Mirrors cloneSessionState. + let sessionState: SessionState + try { + sessionState = JSON.parse( + JSON.stringify(baseSessionState), + ) as SessionState + } catch { + sessionState = cloneDeep(baseSessionState) + } // Apply maxAgentSteps override if (overrides.maxAgentSteps !== undefined) {