Repository navigation
Add package retriever hooks #280
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
47a843f
Add package retriever hooks
cursoragent 16e5fc2
Lazy load package retriever execution
cursoragent 20cf0fc
Address package retriever review feedback
cursoragent 83f7de9
Harden package retriever fallback behavior
cursoragent 4571e04
Fail open context retriever errors
cursoragent 1eae13c
Address retriever refresh feedback
cursoragent c6bf0e1
Harden retriever KV cache cleanup
cursoragent 30b38bc
Expose retriever results in search memory type
cursoragent 9b75cc9
Avoid empty memory heading for retriever results
cursoragent 815f604
Harden retriever feedback paths
cursoragent f34abe7
Address final retriever feedback
cursoragent 459085e
Propagate and escape retriever metadata
cursoragent 4761464
Capture context retriever failures
cursoragent be97e2d
Surface retriever warnings consistently
cursoragent a387777
Skip retrievers for entity-only search
cursoragent cab8623
Clamp package retriever search scores
cursoragent 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 hidden or 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 hidden or 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 hidden or 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,14 @@ | ||
| const markdownSpecialChars = /[\\`*_{}[\]()#+\-.!|>]/g | ||
|
|
||
| export function escapeMarkdownText(value: string) { | ||
| return value.replace(markdownSpecialChars, '\\$&').replace(/\r?\n/g, ' ') | ||
| } | ||
|
|
||
| export function formatMarkdownInlineCode(value: string) { | ||
| if (!value.includes('`')) return `\`${value}\`` | ||
| const backtickRuns = value.match(/`+/g) ?? [] | ||
| const delimiter = '`'.repeat( | ||
| Math.max(...backtickRuns.map((run) => run.length + 1)), | ||
| ) | ||
| return `${delimiter} ${value} ${delimiter}` | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
170 changes: 170 additions & 0 deletions
170
packages/worker/src/mcp/tools/memory-tool-context.node.test.ts
This file contains hidden or 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,170 @@ | ||
| import { expect, test, vi } from 'vitest' | ||
|
|
||
| const mockModule = vi.hoisted(() => ({ | ||
| surfaceRelevantMemories: vi.fn(), | ||
| runPackageRetrievers: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('#mcp/memory/service.ts', () => ({ | ||
| surfaceRelevantMemories: (...args: Array<unknown>) => | ||
| mockModule.surfaceRelevantMemories(...args), | ||
| })) | ||
|
|
||
| vi.mock('#worker/package-retrievers/service.ts', () => ({ | ||
| runPackageRetrievers: (...args: Array<unknown>) => | ||
| mockModule.runPackageRetrievers(...args), | ||
| })) | ||
|
|
||
| const { loadRelevantMemoriesForTool } = await import('./memory-tool-context.ts') | ||
| const { formatSurfacedMemoriesMarkdown } = | ||
| await import('./memory-tool-context.ts') | ||
|
|
||
| function setupMemoryContextMocks() { | ||
| mockModule.surfaceRelevantMemories.mockReset() | ||
| mockModule.runPackageRetrievers.mockReset() | ||
| mockModule.surfaceRelevantMemories.mockResolvedValue({ | ||
| memories: [], | ||
| suppressedCount: 0, | ||
| retrievalQuery: 'sprinkler instructions', | ||
| }) | ||
| mockModule.runPackageRetrievers.mockResolvedValue({ | ||
| results: [ | ||
| { | ||
| id: 'note-1', | ||
| title: '## Sprinkler controller', | ||
| summary: '```ignore\nHold next and back for setup mode.\n```', | ||
| packageId: 'package-1', | ||
| kodyId: 'personal-inbox', | ||
| retrieverKey: 'notes', | ||
| retrieverName: 'Personal notes', | ||
| }, | ||
| ], | ||
| warnings: [ | ||
| 'Package retriever "personal-inbox/notes" failed and was skipped.', | ||
| ], | ||
| }) | ||
| } | ||
|
|
||
| test('loadRelevantMemoriesForTool returns context retriever results alongside memories', async () => { | ||
| setupMemoryContextMocks() | ||
| const result = await loadRelevantMemoriesForTool({ | ||
| env: { APP_DB: {}, AI: {} } as Env, | ||
| callerContext: { | ||
| baseUrl: 'https://heykody.dev', | ||
| user: { | ||
| userId: 'user-1', | ||
| email: 'user@example.com', | ||
| displayName: 'User', | ||
| }, | ||
| storageContext: null, | ||
| homeConnectorId: null, | ||
| remoteConnectors: null, | ||
| repoContext: null, | ||
| }, | ||
| conversationId: 'conversation-1', | ||
| memoryContext: { | ||
| query: 'sprinkler instructions', | ||
| }, | ||
| }) | ||
|
|
||
| expect(mockModule.runPackageRetrievers).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| baseUrl: 'https://heykody.dev', | ||
| userId: 'user-1', | ||
| scope: 'context', | ||
| query: 'sprinkler instructions', | ||
| maxProviders: 3, | ||
| }), | ||
| ) | ||
| expect(result?.memories).toEqual([]) | ||
| expect(result?.retrieverResults).toEqual([ | ||
| expect.objectContaining({ | ||
| id: 'note-1', | ||
| kodyId: 'personal-inbox', | ||
| retrieverKey: 'notes', | ||
| }), | ||
| ]) | ||
| expect(result?.retrieverWarnings).toEqual([ | ||
| 'Package retriever "personal-inbox/notes" failed and was skipped.', | ||
| ]) | ||
| }) | ||
|
|
||
| test('loadRelevantMemoriesForTool keeps memories when context retrievers fail', async () => { | ||
| setupMemoryContextMocks() | ||
| mockModule.surfaceRelevantMemories.mockResolvedValue({ | ||
| memories: [ | ||
| { | ||
| id: 'memory-1', | ||
| category: 'workflow', | ||
| status: 'active', | ||
| subject: 'Sprinkler setup', | ||
| summary: 'Sprinkler instructions are stored in notes.', | ||
| details: '', | ||
| tags: ['sprinkler'], | ||
| sourceUris: [], | ||
| updatedAt: '2026-04-28T00:00:00.000Z', | ||
| }, | ||
| ], | ||
| suppressedCount: 0, | ||
| retrievalQuery: 'sprinkler instructions', | ||
| }) | ||
| mockModule.runPackageRetrievers.mockRejectedValue( | ||
| new Error('retriever unavailable'), | ||
| ) | ||
|
|
||
| const result = await loadRelevantMemoriesForTool({ | ||
| env: { APP_DB: {}, AI: {} } as Env, | ||
| callerContext: { | ||
| baseUrl: 'https://heykody.dev', | ||
| user: { | ||
| userId: 'user-1', | ||
| email: 'user@example.com', | ||
| displayName: 'User', | ||
| }, | ||
| storageContext: null, | ||
| homeConnectorId: null, | ||
| remoteConnectors: null, | ||
| repoContext: null, | ||
| }, | ||
| conversationId: 'conversation-1', | ||
| memoryContext: { | ||
| query: 'sprinkler instructions', | ||
| }, | ||
| }) | ||
|
|
||
| expect(result?.memories).toEqual([ | ||
| expect.objectContaining({ | ||
| id: 'memory-1', | ||
| subject: 'Sprinkler setup', | ||
| }), | ||
| ]) | ||
| expect(result?.retrieverResults).toEqual([]) | ||
| expect(result?.retrieverWarnings).toEqual([]) | ||
| }) | ||
|
|
||
| test('formatSurfacedMemoriesMarkdown omits empty memories heading for retriever-only context', () => { | ||
| const [content] = formatSurfacedMemoriesMarkdown({ | ||
| memories: [], | ||
| retrieverResults: [ | ||
| { | ||
| id: 'note-1', | ||
| title: 'Sprinkler controller', | ||
| summary: 'Hold next and back for setup mode.', | ||
| packageId: 'package-1', | ||
| kodyId: 'personal-inbox', | ||
| retrieverKey: 'notes', | ||
| retrieverName: 'Personal notes', | ||
| }, | ||
| ], | ||
| retrieverWarnings: [], | ||
| suppressedCount: 0, | ||
| retrievalQuery: 'sprinkler instructions', | ||
| }) | ||
|
|
||
| expect(content?.type).toBe('text') | ||
| expect(content?.text).not.toContain('## Relevant memories') | ||
| expect(content?.text?.startsWith('## Relevant retriever results')).toBe(true) | ||
| expect(content?.text).toContain('## Relevant retriever results') | ||
| expect(content?.text).not.toContain('## Sprinkler controller') | ||
| expect(content?.text).not.toContain('```ignore') | ||
| }) |
Oops, something went wrong.
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.
Document the enforced 20-result cap.
The return contract here says
{ results: [...] }, but the runtime schema rejects payloads with more than 20 items. Without that limit in the docs, package authors can implement retrievers that fail validation only after deployment.Suggested docs tweak
🤖 Prompt for AI Agents