-
Notifications
You must be signed in to change notification settings - Fork 183
fix(artifacts): isolate in-memory composite keys #576
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
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -101,17 +101,19 @@ export class InMemoryArtifactService implements BaseArtifactService { | |
| userId, | ||
| sessionId, | ||
| }: ListArtifactKeysRequest): Promise<string[]> { | ||
| const sessionPrefix = `${appName}/${userId}/${sessionId}/`; | ||
| const usernamespacePrefix = `${appName}/${userId}/user/`; | ||
| const filenames: string[] = []; | ||
|
|
||
| for (const path in this.artifacts) { | ||
| if (path.startsWith(sessionPrefix)) { | ||
| const filename = path.replace(sessionPrefix, ''); | ||
| filenames.push(filename); | ||
| } else if (path.startsWith(usernamespacePrefix)) { | ||
| const filename = path.replace(usernamespacePrefix, ''); | ||
| filenames.push(filename); | ||
| const key = JSON.parse(path) as ArtifactStorageKey; | ||
| if ( | ||
| key[0] === 'session' && | ||
| key[1] === appName && | ||
| key[2] === userId && | ||
| key[3] === sessionId | ||
| ) { | ||
| filenames.push(key[4]); | ||
| } else if (key[0] === 'user' && key[1] === appName && key[2] === userId) { | ||
| filenames.push(key[3]); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -212,12 +214,16 @@ function artifactPath( | |
| filename: string, | ||
| ): string { | ||
| if (fileHasUserNamespace(filename)) { | ||
| return `${appName}/${userId}/user/${filename}`; | ||
| return JSON.stringify(['user', appName, userId, filename]); | ||
| } | ||
|
|
||
| return `${appName}/${userId}/${sessionId}/${filename}`; | ||
| return JSON.stringify(['session', appName, userId, sessionId, filename]); | ||
|
Collaborator
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. Not a nit, but out of scope for this PR — worth a follow-up issue rather than a change here. The encoding on this line is right, but // core/src/artifacts/gcs_artifact_service.ts:283-285
const prefix = isUser
? `${appName}/${userId}/user/${cleanFilename}`
: `${appName}/${userId}/${sessionId}/${cleanFilename}`;
It is actually worse there than it was here. GCS strips the For contrast, I am not asking you to fix GCS in this PR; flagging it so #574 doesn't get closed as fully resolved when only one of the two implementations is fixed. |
||
| } | ||
|
|
||
| type ArtifactStorageKey = | ||
| | ['session', string, string, string, string] | ||
| | ['user', string, string, string]; | ||
|
Collaborator
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. Nit, optional. The declaration is positional, so the reads in type ArtifactStorageKey =
| ['session', string, string, string, string]
| ['user', string, string, string];Labelled tuple members are erased at runtime and make the indices self-explanatory: type ArtifactStorageKey =
| [
'session',
appName: string,
userId: string,
sessionId: string,
filename: string,
]
| ['user', appName: string, userId: string, filename: string];Genuine trade-off: prettier will wrap the session arm as shown, so 3 lines become 9. Take it or leave it — I checked the labels don't affect the Separately, while you're in this area: the JSDoc above |
||
|
|
||
| /** | ||
| * Checks if the filename has a user namespace prefix. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,40 @@ | |
| */ | ||
|
|
||
| import {InMemoryArtifactService} from '@google/adk'; | ||
| import {describe} from 'vitest'; | ||
| import {describe, expect, it} from 'vitest'; | ||
| import {runArtifactServiceTests} from './artifact_service_test_utils.js'; | ||
|
|
||
| describe('InMemoryArtifactService', () => { | ||
| runArtifactServiceTests( | ||
| async () => new InMemoryArtifactService(), | ||
| async () => {}, | ||
| ); | ||
|
|
||
| it('keeps artifacts with ambiguous path components isolated', async () => { | ||
| const service = new InMemoryArtifactService(); | ||
|
|
||
| await service.saveArtifact({ | ||
| appName: 'app', | ||
| userId: 'user', | ||
| sessionId: 'session', | ||
| filename: 'nested/report.txt', | ||
| artifact: {text: 'artifact-a'}, | ||
| }); | ||
| await service.saveArtifact({ | ||
| appName: 'app', | ||
| userId: 'user', | ||
| sessionId: 'session/nested', | ||
| filename: 'report.txt', | ||
| artifact: {text: 'artifact-b'}, | ||
| }); | ||
|
|
||
| const artifact = await service.loadArtifact({ | ||
| appName: 'app', | ||
| userId: 'user', | ||
| sessionId: 'session', | ||
| filename: 'nested/report.txt', | ||
| }); | ||
|
|
||
| expect(artifact?.text).toBe('artifact-a'); | ||
|
Collaborator
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. Not a nit. This pins the separator collision from #574, but not the other half of the change. The scope marker also fixes a cross-session listing leak, and nothing exercises it. Under the old key scheme: const usernamespacePrefix = `${appName}/${userId}/user/`;
...
} else if (path.startsWith(usernamespacePrefix)) {an artifact saved into a session literally named it('does not leak a session named "user" into other sessions', async () => {
const service = new InMemoryArtifactService();
await service.saveArtifact({
appName: 'app',
userId: 'user',
sessionId: 'user',
filename: 'foo.txt',
artifact: {text: 'session-scoped'},
});
const keys = await service.listArtifactKeys({
appName: 'app',
userId: 'user',
sessionId: 'other',
});
expect(keys).toEqual([]);
});I traced this by hand rather than running it: on the pre-change code the save lands at Also worth one more line in the test above — loading |
||
| }); | ||
| }); | ||
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.
Can we just encode/decode every part of the path (appName, userId, sessionId, filename) to not to have the delimiter (
/)?No need to create an array as a key. I think this is antipatern and better to keep keys as numbers or strings but not as objects.