From e5b239428419810109e9cc9fe7d72b3a5e85b66c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 17 Feb 2026 18:33:44 +0000 Subject: [PATCH] test: add coverage for circular agentContext in memory formatter Add a test case to `test/memory-formatter.test.mjs` to ensure that `renderPromptWithMemoryContext` handles circular references in `agentContext` by falling back to `String(agentContext)` instead of throwing a `JSON.stringify` error. This addresses a missing edge case in JSON serialization testing. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- test/memory-formatter.test.mjs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/memory-formatter.test.mjs b/test/memory-formatter.test.mjs index 8e1d311..6b1e506 100644 --- a/test/memory-formatter.test.mjs +++ b/test/memory-formatter.test.mjs @@ -193,3 +193,34 @@ test('renderPromptWithMemoryContext handles missing query or service', async () const output = await renderPromptWithMemoryContext(params); assert.equal(output, 'Base prompt'); }); + +test('renderPromptWithMemoryContext handles circular agentContext gracefully', async () => { + const circular = {}; + circular.self = circular; + + let capturedQuery = ''; + const memoryService = { + async getRelevantMemories({ query }) { + capturedQuery = query; + return []; + }, + async updateMemoryUsage() {} + }; + + const params = { + basePrompt: 'Base prompt', + userRequest: 'Hello', + agentContext: circular, + agent_id: 'agent-1', + memoryService, + env: { TORCH_MEMORY_RETRIEVAL_ENABLED: 'true' } + }; + + const output = await renderPromptWithMemoryContext(params); + + // It should not throw. + // The query should contain the string representation of the circular object. + // String(circular) is "[object Object]" + assert.ok(capturedQuery.includes('[object Object]'), 'Query should contain the stringified circular object fallback'); + assert.match(output, /Base prompt/); +});