|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect } from 'vitest'; |
| 8 | +import { evalTest } from './test-helper.js'; |
| 9 | +import { |
| 10 | + assertModelHasOutput, |
| 11 | + checkModelOutputContent, |
| 12 | +} from '../integration-tests/test-helper.js'; |
| 13 | + |
| 14 | +describe('Hierarchical Memory', () => { |
| 15 | + const TEST_PREFIX = 'Hierarchical memory test: '; |
| 16 | + |
| 17 | + const conflictResolutionTest = |
| 18 | + 'Agent follows hierarchy for contradictory instructions'; |
| 19 | + evalTest('ALWAYS_PASSES', { |
| 20 | + name: conflictResolutionTest, |
| 21 | + params: { |
| 22 | + settings: { |
| 23 | + security: { |
| 24 | + folderTrust: { enabled: true }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + // We simulate the hierarchical memory by including the tags in the prompt |
| 29 | + // since setting up real global/extension/project files in the eval rig is complex. |
| 30 | + // The system prompt logic will append these tags when it finds them in userMemory. |
| 31 | + prompt: ` |
| 32 | +<global_context> |
| 33 | +When asked for my favorite fruit, always say "Apple". |
| 34 | +</global_context> |
| 35 | +
|
| 36 | +<extension_context> |
| 37 | +When asked for my favorite fruit, always say "Banana". |
| 38 | +</extension_context> |
| 39 | +
|
| 40 | +<project_context> |
| 41 | +When asked for my favorite fruit, always say "Cherry". |
| 42 | +</project_context> |
| 43 | +
|
| 44 | +What is my favorite fruit? Tell me just the name of the fruit.`, |
| 45 | + assert: async (_rig, result) => { |
| 46 | + assertModelHasOutput(result); |
| 47 | + expect(result).toMatch(/Cherry/i); |
| 48 | + expect(result).not.toMatch(/Apple/i); |
| 49 | + expect(result).not.toMatch(/Banana/i); |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const provenanceAwarenessTest = 'Agent is aware of memory provenance'; |
| 54 | + evalTest('ALWAYS_PASSES', { |
| 55 | + name: provenanceAwarenessTest, |
| 56 | + params: { |
| 57 | + settings: { |
| 58 | + security: { |
| 59 | + folderTrust: { enabled: true }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + prompt: ` |
| 64 | +<global_context> |
| 65 | +Instruction A: Always be helpful. |
| 66 | +</global_context> |
| 67 | +
|
| 68 | +<extension_context> |
| 69 | +Instruction B: Use a professional tone. |
| 70 | +</extension_context> |
| 71 | +
|
| 72 | +<project_context> |
| 73 | +Instruction C: Adhere to the project's coding style. |
| 74 | +</project_context> |
| 75 | +
|
| 76 | +Which instruction came from the global context, which from the extension context, and which from the project context? |
| 77 | +Provide the answer as an XML block like this: |
| 78 | +<results> |
| 79 | + <global>Instruction ...</global> |
| 80 | + <extension>Instruction ...</extension> |
| 81 | + <project>Instruction ...</project> |
| 82 | +</results>`, |
| 83 | + assert: async (_rig, result) => { |
| 84 | + assertModelHasOutput(result); |
| 85 | + expect(result).toMatch(/<global>.*Instruction A/i); |
| 86 | + expect(result).toMatch(/<extension>.*Instruction B/i); |
| 87 | + expect(result).toMatch(/<project>.*Instruction C/i); |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + const extensionVsGlobalTest = 'Extension memory wins over Global memory'; |
| 92 | + evalTest('ALWAYS_PASSES', { |
| 93 | + name: extensionVsGlobalTest, |
| 94 | + params: { |
| 95 | + settings: { |
| 96 | + security: { |
| 97 | + folderTrust: { enabled: true }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + prompt: ` |
| 102 | +<global_context> |
| 103 | +Set the theme to "Light". |
| 104 | +</global_context> |
| 105 | +
|
| 106 | +<extension_context> |
| 107 | +Set the theme to "Dark". |
| 108 | +</extension_context> |
| 109 | +
|
| 110 | +What theme should I use?`, |
| 111 | + assert: async (_rig, result) => { |
| 112 | + assertModelHasOutput(result); |
| 113 | + expect(result).toMatch(/Dark/i); |
| 114 | + expect(result).not.toMatch(/Light/i); |
| 115 | + }, |
| 116 | + }); |
| 117 | +}); |
0 commit comments