From 2e2b066713e85a44754ad999a9d7aa1735fe3205 Mon Sep 17 00:00:00 2001 From: Tommaso Sciortino Date: Tue, 11 Nov 2025 13:22:22 -0800 Subject: [PATCH] Move temp dir from system prompt to first user msg (#12895) --- .../core/__snapshots__/prompts.test.ts.snap | 36 +++++++++---------- packages/core/src/core/client.test.ts | 23 ++++++++++++ packages/core/src/core/prompts.ts | 14 ++------ .../core/src/utils/environmentContext.test.ts | 7 ++++ packages/core/src/utils/environmentContext.ts | 2 ++ 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/packages/core/src/core/__snapshots__/prompts.test.ts.snap b/packages/core/src/core/__snapshots__/prompts.test.ts.snap index 5a9b64bbc8..4dc8d62e09 100644 --- a/packages/core/src/core/__snapshots__/prompts.test.ts.snap +++ b/packages/core/src/core/__snapshots__/prompts.test.ts.snap @@ -57,8 +57,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) @@ -159,8 +159,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) @@ -271,8 +271,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) @@ -368,8 +368,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) @@ -465,8 +465,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) @@ -562,8 +562,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) @@ -659,8 +659,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) @@ -756,8 +756,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) @@ -853,8 +853,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: /tmp/project-temp. For example: 'command > /tmp/project-temp/out.log 2> /tmp/project-temp/err.log'. -- After the command runs, inspect the temp files (e.g. '/tmp/project-temp/out.log' and '/tmp/project-temp/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. ## Tone and Style (CLI Interaction) diff --git a/packages/core/src/core/client.test.ts b/packages/core/src/core/client.test.ts index 2be53b6af6..66ccb30a1f 100644 --- a/packages/core/src/core/client.test.ts +++ b/packages/core/src/core/client.test.ts @@ -336,6 +336,29 @@ describe('Gemini Client (client.ts)', () => { }); }); + describe('startChat', () => { + it('should include environment context when resuming a session', async () => { + const extraHistory: Content[] = [ + { role: 'user', parts: [{ text: 'Old message' }] }, + { role: 'model', parts: [{ text: 'Old response' }] }, + ]; + + const chat = await client.startChat(extraHistory); + const history = chat.getHistory(); + + // The first message should be the environment context + expect(history[0].role).toBe('user'); + expect(history[0].parts?.[0]?.text).toContain('This is the Gemini CLI'); + expect(history[0].parts?.[0]?.text).toContain( + "The project's temporary directory is:", + ); + + // The subsequent messages should be the extra history + expect(history[1]).toEqual(extraHistory[0]); + expect(history[2]).toEqual(extraHistory[1]); + }); + }); + describe('tryCompressChat', () => { const mockGetHistory = vi.fn(); diff --git a/packages/core/src/core/prompts.ts b/packages/core/src/core/prompts.ts index 519d2ead81..fd603ecd99 100644 --- a/packages/core/src/core/prompts.ts +++ b/packages/core/src/core/prompts.ts @@ -192,7 +192,6 @@ Mandates'). # Operational Guidelines ${(function () { if (config.getEnableShellOutputEfficiency()) { - const tempDir = config.storage.getProjectTempDir(); return ` ## Shell tool output token efficiency: @@ -202,17 +201,8 @@ IT IS CRITICAL TO FOLLOW THESE GUIDELINES TO AVOID EXCESSIVE TOKEN CONSUMPTION. - Aim to minimize tool output tokens while still capturing necessary information. - If a command is expected to produce a lot of output, use quiet or silent flags where available and appropriate. - Always consider the trade-off between output verbosity and the need for information. If a command's full output is essential for understanding the result, avoid overly aggressive quieting that might obscure important details. -- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory: ${tempDir}. For example: 'command > ${path.posix.join( - tempDir, - 'out.log', - )} 2> ${path.posix.join(tempDir, 'err.log')}'. -- After the command runs, inspect the temp files (e.g. '${path.posix.join( - tempDir, - 'out.log', - )}' and '${path.posix.join( - tempDir, - 'err.log', - )}') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. +- If a command does not have quiet/silent flags or for commands with potentially long output that may not be useful, redirect stdout and stderr to temp files in the project's temporary directory. For example: 'command > /out.log 2> /err.log'. +- After the command runs, inspect the temp files (e.g. '/out.log' and '/err.log') using commands like 'grep', 'tail', 'head', ... (or platform equivalents). Remove the temp files when done. `; } return ''; diff --git a/packages/core/src/utils/environmentContext.test.ts b/packages/core/src/utils/environmentContext.test.ts index dcc0cb7f3a..609c03d821 100644 --- a/packages/core/src/utils/environmentContext.test.ts +++ b/packages/core/src/utils/environmentContext.test.ts @@ -18,6 +18,7 @@ import { getDirectoryContextString, } from './environmentContext.js'; import type { Config } from '../config/config.js'; +import type { Storage } from '../config/storage.js'; import { getFolderStructure } from './getFolderStructure.js'; vi.mock('../config/config.js'); @@ -35,6 +36,9 @@ describe('getDirectoryContextString', () => { getDirectories: vi.fn().mockReturnValue(['/test/dir']), }), getFileService: vi.fn(), + storage: { + getProjectTempDir: vi.fn().mockReturnValue('/tmp/project-temp'), + } as unknown as Storage, }; vi.mocked(getFolderStructure).mockResolvedValue('Mock Folder Structure'); }); @@ -90,6 +94,9 @@ describe('getEnvironmentContext', () => { getFileService: vi.fn(), getToolRegistry: vi.fn().mockReturnValue(mockToolRegistry), + storage: { + getProjectTempDir: vi.fn().mockReturnValue('/tmp/project-temp'), + } as unknown as Storage, }; vi.mocked(getFolderStructure).mockResolvedValue('Mock Folder Structure'); diff --git a/packages/core/src/utils/environmentContext.ts b/packages/core/src/utils/environmentContext.ts index 59d7686386..162c2e5961 100644 --- a/packages/core/src/utils/environmentContext.ts +++ b/packages/core/src/utils/environmentContext.ts @@ -59,11 +59,13 @@ export async function getEnvironmentContext(config: Config): Promise { }); const platform = process.platform; const directoryContext = await getDirectoryContextString(config); + const tempDir = config.storage.getProjectTempDir(); const context = ` This is the Gemini CLI. We are setting up the context for our chat. Today's date is ${today} (formatted according to the user's locale). My operating system is: ${platform} +The project's temporary directory is: ${tempDir} ${directoryContext} `.trim();