-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(server-core): enforce memory conversation ownership #1388
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
6e64a3c
95ba4d9
96bcbab
8c5ff4f
ca88a84
c7c2c28
a4d2f31
2cf761b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { InMemoryStorageAdapter } from "./adapters/storage/in-memory"; | ||
| import { InMemoryVectorAdapter } from "./adapters/vector/in-memory"; | ||
| import { Memory } from "./index"; | ||
|
|
||
| describe("Memory conversation mutation guards", () => { | ||
| let storage: InMemoryStorageAdapter; | ||
| let memory: Memory; | ||
|
|
||
| beforeEach(async () => { | ||
| storage = new InMemoryStorageAdapter(); | ||
| memory = new Memory({ | ||
| storage, | ||
| vector: new InMemoryVectorAdapter(), | ||
| }); | ||
|
|
||
| await memory.createConversation({ | ||
| id: "conv-1", | ||
| userId: "user-1", | ||
| resourceId: "agent-1", | ||
| title: "Conversation", | ||
| metadata: {}, | ||
| }); | ||
| }); | ||
|
|
||
| it("continues unguarded deletes when vector cleanup cannot read the conversation", async () => { | ||
| const readError = new Error("read unavailable"); | ||
| const getSpy = vi.spyOn(storage, "getConversation").mockRejectedValueOnce(readError); | ||
| const deleteSpy = vi.spyOn(storage, "deleteConversation"); | ||
| const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); | ||
|
|
||
| await expect(memory.deleteConversation("conv-1")).resolves.toBeUndefined(); | ||
|
|
||
| expect(getSpy).toHaveBeenCalledWith("conv-1"); | ||
| expect(deleteSpy).toHaveBeenCalledWith("conv-1", undefined); | ||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| "Failed to delete vectors for conversation conv-1:", | ||
| readError, | ||
| ); | ||
| await expect(storage.getConversation("conv-1")).resolves.toBeNull(); | ||
|
|
||
| warnSpy.mockRestore(); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
|
||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,10 @@ export type ConversationQueryOptions = { | |
| orderDirection?: "ASC" | "DESC"; | ||
| }; | ||
|
|
||
| export type ConversationMutationOptions = { | ||
| expectedUserId?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Options for getting messages | ||
| */ | ||
|
|
@@ -443,8 +447,9 @@ export interface StorageAdapter { | |
| updateConversation( | ||
| id: string, | ||
| updates: Partial<Omit<Conversation, "id" | "createdAt" | "updatedAt">>, | ||
| options?: ConversationMutationOptions, | ||
| ): Promise<Conversation>; | ||
| deleteConversation(id: string): Promise<void>; | ||
| deleteConversation(id: string, options?: ConversationMutationOptions): Promise<void>; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Persistent storage adapters ignore the new Prompt for AI agents |
||
|
|
||
| saveConversationSteps?(steps: ConversationStepRecord[]): Promise<void>; | ||
| getConversationSteps?( | ||
|
|
||
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.
P3: The new adapter tests do not verify the security-critical failure path: a guarded update or delete must reject when the owner does not match or when the database affects zero rows. Adding negative tests for each adapter, including assertions that the conversation and child data remain unchanged, would protect the revalidation behavior claimed by this change.
Prompt for AI agents