mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-21 02:24:09 -07:00
fix(core): resolve subagent chat recording gaps and directory inheritance (#24368)
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
type ToolCallRecord,
|
||||
type MessageRecord,
|
||||
} from './chatRecordingService.js';
|
||||
import type { WorkspaceContext } from '../utils/workspaceContext.js';
|
||||
import { CoreToolCallStatus } from '../scheduler/types.js';
|
||||
import type { Content, Part } from '@google/genai';
|
||||
import type { Config } from '../config/config.js';
|
||||
@@ -57,6 +58,9 @@ describe('ChatRecordingService', () => {
|
||||
},
|
||||
getModel: vi.fn().mockReturnValue('gemini-pro'),
|
||||
getDebugMode: vi.fn().mockReturnValue(false),
|
||||
getWorkspaceContext: vi.fn().mockReturnValue({
|
||||
getDirectories: vi.fn().mockReturnValue([]),
|
||||
}),
|
||||
getToolRegistry: vi.fn().mockReturnValue({
|
||||
getTool: vi.fn().mockReturnValue({
|
||||
displayName: 'Test Tool',
|
||||
@@ -66,6 +70,13 @@ describe('ChatRecordingService', () => {
|
||||
}),
|
||||
} as unknown as Config;
|
||||
|
||||
// Ensure mockConfig.config points to itself for AgentLoopContext parity
|
||||
Object.defineProperty(mockConfig, 'config', {
|
||||
get() {
|
||||
return mockConfig;
|
||||
},
|
||||
});
|
||||
|
||||
vi.mocked(getProjectHash).mockReturnValue('test-project-hash');
|
||||
chatRecordingService = new ChatRecordingService(mockConfig);
|
||||
});
|
||||
@@ -132,6 +143,31 @@ describe('ChatRecordingService', () => {
|
||||
expect(files[0]).toBe('test-session-id.json');
|
||||
});
|
||||
|
||||
it('should inherit workspace directories for subagents during initialization', () => {
|
||||
const mockDirectories = ['/project/dir1', '/project/dir2'];
|
||||
vi.mocked(mockConfig.getWorkspaceContext).mockReturnValue({
|
||||
getDirectories: vi.fn().mockReturnValue(mockDirectories),
|
||||
} as unknown as WorkspaceContext);
|
||||
|
||||
// Initialize as a subagent
|
||||
chatRecordingService.initialize(undefined, 'subagent');
|
||||
|
||||
// Recording a message triggers the disk write (deferred until then)
|
||||
chatRecordingService.recordMessage({
|
||||
type: 'user',
|
||||
content: 'ping',
|
||||
model: 'm',
|
||||
});
|
||||
|
||||
const sessionFile = chatRecordingService.getConversationFilePath()!;
|
||||
const conversation = JSON.parse(
|
||||
fs.readFileSync(sessionFile, 'utf8'),
|
||||
) as ConversationRecord;
|
||||
|
||||
expect(conversation.kind).toBe('subagent');
|
||||
expect(conversation.directories).toEqual(mockDirectories);
|
||||
});
|
||||
|
||||
it('should resume from an existing session if provided', () => {
|
||||
const chatsDir = path.join(testTempDir, 'chats');
|
||||
fs.mkdirSync(chatsDir, { recursive: true });
|
||||
|
||||
@@ -218,12 +218,22 @@ export class ChatRecordingService {
|
||||
}
|
||||
this.conversationFile = path.join(chatsDir, filename);
|
||||
|
||||
const directories =
|
||||
this.kind === 'subagent'
|
||||
? [
|
||||
...(this.context.config
|
||||
.getWorkspaceContext()
|
||||
?.getDirectories() ?? []),
|
||||
]
|
||||
: undefined;
|
||||
|
||||
this.writeConversation({
|
||||
sessionId: this.sessionId,
|
||||
projectHash: this.projectHash,
|
||||
startTime: new Date().toISOString(),
|
||||
lastUpdated: new Date().toISOString(),
|
||||
messages: [],
|
||||
directories,
|
||||
kind: this.kind,
|
||||
});
|
||||
}
|
||||
@@ -518,6 +528,13 @@ export class ChatRecordingService {
|
||||
): void {
|
||||
try {
|
||||
if (!this.conversationFile) return;
|
||||
|
||||
// Cache the conversation state even if we don't write to disk yet.
|
||||
// This ensures that subsequent reads (e.g. during recordMessage)
|
||||
// see the initial state (like directories) instead of trying to
|
||||
// read a non-existent file from disk.
|
||||
this.cachedConversation = conversation;
|
||||
|
||||
// Don't write the file yet until there's at least one message.
|
||||
if (conversation.messages.length === 0 && !allowEmpty) return;
|
||||
|
||||
@@ -527,7 +544,6 @@ export class ChatRecordingService {
|
||||
// Compare before updating lastUpdated so the timestamp doesn't
|
||||
// cause a false diff.
|
||||
if (this.cachedLastConvData === newContent) return;
|
||||
this.cachedConversation = conversation;
|
||||
conversation.lastUpdated = new Date().toISOString();
|
||||
const contentToWrite = JSON.stringify(conversation, null, 2);
|
||||
this.cachedLastConvData = contentToWrite;
|
||||
|
||||
Reference in New Issue
Block a user