From 9e4450e400d33b9c203db2fa0270caa312d1eefa Mon Sep 17 00:00:00 2001 From: Mark Henderson Date: Tue, 3 Mar 2026 20:36:41 -0800 Subject: [PATCH] Fix: strip tool messages from preloaded history (Gemini ordering error) + cast PRD param to text Made-with: Cursor --- app/api/projects/[projectId]/atlas-chat/route.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/api/projects/[projectId]/atlas-chat/route.ts b/app/api/projects/[projectId]/atlas-chat/route.ts index 46cae5e..018f5d2 100644 --- a/app/api/projects/[projectId]/atlas-chat/route.ts +++ b/app/api/projects/[projectId]/atlas-chat/route.ts @@ -55,7 +55,7 @@ async function savePrd(projectId: string, prdContent: string): Promise { try { await query( `UPDATE fs_projects - SET data = data || jsonb_build_object('prd', $2, 'stage', 'architecture'), + SET data = data || jsonb_build_object('prd', $2::text, 'stage', 'architecture'), updated_at = NOW() WHERE id = $1`, [projectId, prdContent] @@ -111,8 +111,13 @@ export async function POST( const sessionId = `atlas_${projectId}`; - // Load conversation history from DB to persist across agent runner restarts - const history = await loadAtlasHistory(projectId); + // Load conversation history from DB to persist across agent runner restarts. + // Strip tool_call / tool_response messages — replaying them across sessions + // causes Gemini to reject the request with a turn-ordering error. + const rawHistory = await loadAtlasHistory(projectId); + const history = rawHistory.filter((m: any) => + (m.role === "user" || m.role === "assistant") && m.content + ); // __init__ is a special internal trigger used only when there is no existing history. // If history already exists, ignore the init request (conversation already started).