|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import type { ProcessInputStepArgs } from '../index'; |
| 4 | +import { deriveLoadedNamesFromMessages, LegacyMapLoadedToolStore, ContextLoadedToolStore } from './tool-search-stores'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Build a minimal ProcessInputStepArgs carrying conversation messages with the |
| 8 | + * given search_tools / load_tool tool-invocation results. |
| 9 | + */ |
| 10 | +function argsWithMessages( |
| 11 | + invocations: Array<{ toolName: 'search_tools' | 'load_tool'; result: unknown }>, |
| 12 | +): ProcessInputStepArgs { |
| 13 | + return { |
| 14 | + messages: [ |
| 15 | + { |
| 16 | + id: 'm1', |
| 17 | + role: 'assistant', |
| 18 | + content: { |
| 19 | + format: 2, |
| 20 | + parts: invocations.map((inv, i) => ({ |
| 21 | + type: 'tool-invocation' as const, |
| 22 | + toolInvocation: { |
| 23 | + state: 'result' as const, |
| 24 | + toolCallId: `call-${i}`, |
| 25 | + toolName: inv.toolName, |
| 26 | + args: {}, |
| 27 | + result: inv.result, |
| 28 | + }, |
| 29 | + })), |
| 30 | + }, |
| 31 | + }, |
| 32 | + ], |
| 33 | + } as unknown as ProcessInputStepArgs; |
| 34 | +} |
| 35 | + |
| 36 | +describe('deriveLoadedNamesFromMessages', () => { |
| 37 | + it('reads names from a search_tools result (results[].name)', () => { |
| 38 | + const args = argsWithMessages([ |
| 39 | + { toolName: 'search_tools', result: { results: [{ name: 'weather' }, { name: 'calendar' }] } }, |
| 40 | + ]); |
| 41 | + expect([...deriveLoadedNamesFromMessages(args)].sort()).toEqual(['calendar', 'weather']); |
| 42 | + }); |
| 43 | + |
| 44 | + it('reads names from a load_tool result (loaded[])', () => { |
| 45 | + const args = argsWithMessages([{ toolName: 'load_tool', result: { loaded: ['github_create_issue'] } }]); |
| 46 | + expect([...deriveLoadedNamesFromMessages(args)]).toEqual(['github_create_issue']); |
| 47 | + }); |
| 48 | + |
| 49 | + it('unions across multiple invocations and ignores other tools', () => { |
| 50 | + const args = argsWithMessages([ |
| 51 | + { toolName: 'search_tools', result: { results: [{ name: 'weather' }] } }, |
| 52 | + { toolName: 'load_tool', result: { loaded: ['calendar'] } }, |
| 53 | + ]); |
| 54 | + expect([...deriveLoadedNamesFromMessages(args)].sort()).toEqual(['calendar', 'weather']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns empty when messages are missing', () => { |
| 58 | + expect(deriveLoadedNamesFromMessages({} as ProcessInputStepArgs).size).toBe(0); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('LegacyMapLoadedToolStore', () => { |
| 63 | + const emptyArgs = argsWithMessages([]); |
| 64 | + |
| 65 | + it('tracks loaded tools per thread', () => { |
| 66 | + const store = new LegacyMapLoadedToolStore({ ttl: 0 }); |
| 67 | + store.addLoaded(['weather'], { threadId: 'thread-1', args: emptyArgs }); |
| 68 | + store.addLoaded(['calendar'], { threadId: 'thread-2', args: emptyArgs }); |
| 69 | + |
| 70 | + expect([...store.getLoadedNames({ threadId: 'thread-1', args: emptyArgs })]).toEqual(['weather']); |
| 71 | + expect([...store.getLoadedNames({ threadId: 'thread-2', args: emptyArgs })]).toEqual(['calendar']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('shares the default entry across anonymous requests (original behavior)', () => { |
| 75 | + const store = new LegacyMapLoadedToolStore({ ttl: 0 }); |
| 76 | + store.addLoaded(['weather'], { threadId: undefined, args: emptyArgs }); |
| 77 | + expect([...store.getLoadedNames({ threadId: undefined, args: emptyArgs })]).toEqual(['weather']); |
| 78 | + }); |
| 79 | + |
| 80 | + it('clears a single thread and all threads', () => { |
| 81 | + const store = new LegacyMapLoadedToolStore({ ttl: 0 }); |
| 82 | + store.addLoaded(['weather'], { threadId: 'thread-1', args: emptyArgs }); |
| 83 | + store.addLoaded(['calendar'], { threadId: 'thread-2', args: emptyArgs }); |
| 84 | + |
| 85 | + store.clearState('thread-1'); |
| 86 | + expect(store.getLoadedNames({ threadId: 'thread-1', args: emptyArgs }).size).toBe(0); |
| 87 | + expect([...store.getLoadedNames({ threadId: 'thread-2', args: emptyArgs })]).toEqual(['calendar']); |
| 88 | + |
| 89 | + store.clearAllState(); |
| 90 | + expect(store.getLoadedNames({ threadId: 'thread-2', args: emptyArgs }).size).toBe(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it('evicts stale state past the ttl and reports stats', async () => { |
| 94 | + const store = new LegacyMapLoadedToolStore({ ttl: 40 }); |
| 95 | + store.addLoaded(['weather'], { threadId: 'thread-1', args: emptyArgs }); |
| 96 | + expect(store.getStateStats().threadCount).toBe(1); |
| 97 | + |
| 98 | + await new Promise(r => setTimeout(r, 70)); |
| 99 | + expect(store.cleanupStaleState()).toBeGreaterThanOrEqual(1); |
| 100 | + expect(store.getStateStats().threadCount).toBe(0); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('ContextLoadedToolStore', () => { |
| 105 | + it('derives loaded names purely from the messages (restart-safe)', () => { |
| 106 | + const store = new ContextLoadedToolStore(); |
| 107 | + const args = argsWithMessages([{ toolName: 'load_tool', result: { loaded: ['weather'] } }]); |
| 108 | + |
| 109 | + // A brand-new store instance (simulating a process restart) still resolves |
| 110 | + // loaded names from the conversation messages alone. |
| 111 | + const names = store.getLoadedNames({ threadId: 'thread-1', args }); |
| 112 | + expect([...names]).toEqual(['weather']); |
| 113 | + }); |
| 114 | + |
| 115 | + it('bridges activation with a same-process supplemental set before the messages catch up', () => { |
| 116 | + const store = new ContextLoadedToolStore(); |
| 117 | + const emptyArgs = argsWithMessages([]); |
| 118 | + |
| 119 | + store.addLoaded(['weather'], { threadId: 'thread-1', args: emptyArgs }); |
| 120 | + // Messages do not yet contain the result, but the supplemental set carries it. |
| 121 | + expect([...store.getLoadedNames({ threadId: 'thread-1', args: emptyArgs })]).toEqual(['weather']); |
| 122 | + }); |
| 123 | + |
| 124 | + it('hands ownership to the messages once the result appears, so eviction de-loads', () => { |
| 125 | + const store = new ContextLoadedToolStore(); |
| 126 | + const withResult = argsWithMessages([{ toolName: 'load_tool', result: { loaded: ['weather'] } }]); |
| 127 | + |
| 128 | + store.addLoaded(['weather'], { threadId: 'thread-1', args: withResult }); |
| 129 | + // First read sees it in the messages and prunes the supplemental entry. |
| 130 | + expect([...store.getLoadedNames({ threadId: 'thread-1', args: withResult })]).toEqual(['weather']); |
| 131 | + |
| 132 | + // Simulate the result block leaving the messages. |
| 133 | + const evicted = argsWithMessages([]); |
| 134 | + expect(store.getLoadedNames({ threadId: 'thread-1', args: evicted }).size).toBe(0); |
| 135 | + }); |
| 136 | + |
| 137 | + it('does not share supplemental state across anonymous (no-threadId) requests', () => { |
| 138 | + const store = new ContextLoadedToolStore(); |
| 139 | + const emptyArgs = argsWithMessages([]); |
| 140 | + |
| 141 | + store.addLoaded(['weather'], { threadId: undefined, args: emptyArgs }); |
| 142 | + expect(store.getLoadedNames({ threadId: undefined, args: emptyArgs }).size).toBe(0); |
| 143 | + }); |
| 144 | + |
| 145 | + it('drops the supplemental entry once it empties, so thread keys do not leak', () => { |
| 146 | + const store = new ContextLoadedToolStore(); |
| 147 | + const supplemental = (store as unknown as { supplemental: Map<string, Set<string>> }).supplemental; |
| 148 | + |
| 149 | + // Activated before the messages catch up -> entry created. |
| 150 | + store.addLoaded(['weather'], { threadId: 'thread-1', args: argsWithMessages([]) }); |
| 151 | + expect(supplemental.has('thread-1')).toBe(true); |
| 152 | + |
| 153 | + // Once the result appears in the messages, the name is pruned and the now-empty |
| 154 | + // entry is removed from the map rather than lingering as a dead key. |
| 155 | + const withResult = argsWithMessages([{ toolName: 'load_tool', result: { loaded: ['weather'] } }]); |
| 156 | + store.getLoadedNames({ threadId: 'thread-1', args: withResult }); |
| 157 | + expect(supplemental.has('thread-1')).toBe(false); |
| 158 | + }); |
| 159 | +}); |
0 commit comments