-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(deep-cody): update chat memory management #6264
Draft
abeatrix
wants to merge
2
commits into
main
Choose a base branch
from
bee/deep-memory-map
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { ContextItemSource } from '@sourcegraph/cody-shared' | ||
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { URI } from 'vscode-uri' | ||
import { localStorage } from '../../services/LocalStorageProvider' | ||
import { CodyChatMemory } from './CodyChatMemory' | ||
|
||
// Mock localStorage | ||
vi.mock('../../services/LocalStorageProvider', () => ({ | ||
localStorage: { | ||
getChatMemory: vi.fn(), | ||
setChatMemory: vi.fn(), | ||
}, | ||
})) | ||
|
||
describe('CodyChatMemory Workflows', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks() | ||
vi.useFakeTimers() | ||
}) | ||
|
||
afterAll(() => { | ||
vi.useRealTimers() | ||
}) | ||
|
||
describe('Chat Session Workflows', () => { | ||
interface TestScenario { | ||
name: string | ||
actions: Array<{ | ||
type: 'initialize' | 'load' | 'retrieve' | 'unload' | ||
input?: string | ||
expectedContent?: string | null | ||
expectedStorageCall?: boolean | ||
}> | ||
} | ||
|
||
const scenarios: TestScenario[] = [ | ||
{ | ||
name: 'New user first chat session', | ||
actions: [ | ||
{ | ||
type: 'initialize', | ||
expectedContent: null, | ||
expectedStorageCall: true, | ||
}, | ||
{ | ||
type: 'load', | ||
input: 'User prefers TypeScript', | ||
// Update to match new timestamp + content format | ||
expectedContent: | ||
'## \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z\\nUser prefers TypeScript', | ||
expectedStorageCall: true, | ||
}, | ||
{ | ||
type: 'retrieve', | ||
expectedContent: 'User prefers TypeScript', | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Multiple chat interactions in one session', | ||
actions: [ | ||
{ | ||
type: 'load', | ||
input: 'User likes unit testing', | ||
expectedContent: 'User likes unit testing', | ||
}, | ||
{ | ||
type: 'load', | ||
input: 'User works on VS Code extensions', | ||
expectedContent: 'User works on VS Code extensions', | ||
}, | ||
{ | ||
type: 'retrieve', | ||
// Update regex to match new Map-based format with timestamps | ||
expectedContent: | ||
'## \\d{4}.*User likes unit testing.*## \\d{4}.*User works on VS Code extensions', | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Memory capacity management with timestamps', | ||
actions: [ | ||
...Array.from({ length: 10 }, (_, i) => ({ | ||
type: 'load' as const, | ||
input: `Memory item ${i}`, | ||
// Verify only last 8 items are kept | ||
expectedContent: i >= 2 ? `Memory item ${i}` : null, | ||
})), | ||
{ | ||
type: 'retrieve', | ||
// Verify chronological order is maintained | ||
expectedContent: 'Memory item 2.*Memory item 9', | ||
}, | ||
], | ||
}, | ||
// Add new test scenario for timestamp ordering | ||
{ | ||
name: 'Timestamp ordering verification', | ||
actions: [ | ||
{ | ||
type: 'load', | ||
input: 'First message', | ||
}, | ||
{ | ||
type: 'load', | ||
input: 'Second message', | ||
}, | ||
{ | ||
type: 'retrieve', | ||
// Verify messages appear in chronological order with timestamps | ||
expectedContent: '.*First message.*Second message', | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
for (const scenario of scenarios) { | ||
it(scenario.name, () => { | ||
for (const action of scenario.actions) { | ||
switch (action.type) { | ||
case 'load': | ||
// Advance by 1 second to ensure unique timestamps | ||
vi.advanceTimersByTime(1000) | ||
CodyChatMemory.load(action.input!) | ||
if (action.expectedStorageCall) { | ||
expect(localStorage.setChatMemory).toHaveBeenCalled() | ||
} | ||
break | ||
|
||
case 'retrieve': { | ||
const retrieved = CodyChatMemory.retrieve() | ||
if (action.expectedContent === null) { | ||
expect(retrieved).toBeUndefined() | ||
} else { | ||
if (action.expectedContent) { | ||
expect(retrieved?.content).toMatch( | ||
new RegExp(action.expectedContent, 's') | ||
) | ||
} | ||
expect(retrieved?.source).toBe(ContextItemSource.Agentic) | ||
expect(retrieved?.uri).toEqual(URI.file('Cody Memory')) | ||
} | ||
break | ||
} | ||
case 'unload': { | ||
const lastState = CodyChatMemory.reset() | ||
if (action.expectedContent) { | ||
expect(lastState?.content).toContain(action.expectedContent) | ||
} | ||
break | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about keeping the prompt more generally? Similar to the following:
'Here are important memos/notes from past conversations' (Up to you about the phrasing)
This would also make it possible for users to store notes that are not directly related to them, but correspond to specifications/requirements and may not confuse Cody.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PriNova Are you referring to the
about me (user)
part of the prompt?Something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I missed the correct line comment. It is meant for the prompt.