|
| 1 | +/** |
| 2 | + * The slot holds the last main-session turn's CacheSafeParams (including the |
| 3 | + * full conversation history in forkContextMessages). It must never survive a |
| 4 | + * conversation boundary: /clear regenerates the session id and in-process |
| 5 | + * /resume switches it — in both cases a post-turn fork (/recap, SDK |
| 6 | + * side_question, SDK promptSuggestion) reading a stale snapshot would send |
| 7 | + * the previous conversation's history to the API. |
| 8 | + * |
| 9 | + * Uses the real bootstrap/state module (no mocks): regenerateSessionId and |
| 10 | + * switchSession are exactly what /clear and /resume call in production. |
| 11 | + */ |
| 12 | +import { beforeEach, describe, expect, test } from 'bun:test' |
| 13 | +import { |
| 14 | + getSessionId, |
| 15 | + regenerateSessionId, |
| 16 | + switchSession, |
| 17 | +} from 'src/bootstrap/state.js' |
| 18 | +import { asSessionId } from 'src/types/ids.js' |
| 19 | +import { |
| 20 | + getLastCacheSafeParams, |
| 21 | + saveCacheSafeParams, |
| 22 | +} from 'src/utils/cacheSafeParamsSlot.js' |
| 23 | +import type { CacheSafeParams } from 'src/utils/forkedAgent.js' |
| 24 | + |
| 25 | +function makeParams(tag: string): CacheSafeParams { |
| 26 | + // Only identity matters for the slot; the shape is opaque to it. |
| 27 | + return { forkContextMessages: [tag] } as unknown as CacheSafeParams |
| 28 | +} |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + saveCacheSafeParams(null) |
| 32 | +}) |
| 33 | + |
| 34 | +describe('saveCacheSafeParams / getLastCacheSafeParams', () => { |
| 35 | + test('returns the saved params within the same session', () => { |
| 36 | + const params = makeParams('same-session') |
| 37 | + saveCacheSafeParams(params) |
| 38 | + expect(getLastCacheSafeParams()).toBe(params) |
| 39 | + // Repeated reads keep returning it |
| 40 | + expect(getLastCacheSafeParams()).toBe(params) |
| 41 | + }) |
| 42 | + |
| 43 | + test('returns null when nothing was saved', () => { |
| 44 | + expect(getLastCacheSafeParams()).toBeNull() |
| 45 | + }) |
| 46 | + |
| 47 | + test('saving null clears the slot', () => { |
| 48 | + saveCacheSafeParams(makeParams('to-clear')) |
| 49 | + saveCacheSafeParams(null) |
| 50 | + expect(getLastCacheSafeParams()).toBeNull() |
| 51 | + }) |
| 52 | + |
| 53 | + test('a later save overwrites an earlier one', () => { |
| 54 | + saveCacheSafeParams(makeParams('first')) |
| 55 | + const second = makeParams('second') |
| 56 | + saveCacheSafeParams(second) |
| 57 | + expect(getLastCacheSafeParams()).toBe(second) |
| 58 | + }) |
| 59 | + |
| 60 | + test('snapshot is invalidated by /clear (regenerateSessionId)', () => { |
| 61 | + saveCacheSafeParams(makeParams('pre-clear')) |
| 62 | + regenerateSessionId() |
| 63 | + // Without the session-id check this returned the pre-clear conversation's |
| 64 | + // history, which /recap and SDK side_question then sent to the API. |
| 65 | + expect(getLastCacheSafeParams()).toBeNull() |
| 66 | + }) |
| 67 | + |
| 68 | + test('snapshot is invalidated by in-process /resume (switchSession)', () => { |
| 69 | + saveCacheSafeParams(makeParams('session-a')) |
| 70 | + switchSession(asSessionId('00000000-0000-4000-8000-00000000resu')) |
| 71 | + expect(getLastCacheSafeParams()).toBeNull() |
| 72 | + }) |
| 73 | + |
| 74 | + test('stale snapshot stays dropped even if the original session id returns', () => { |
| 75 | + const original = getSessionId() |
| 76 | + saveCacheSafeParams(makeParams('original-session')) |
| 77 | + switchSession(asSessionId('00000000-0000-4000-8000-0000000other')) |
| 78 | + expect(getLastCacheSafeParams()).toBeNull() |
| 79 | + // Switching back does not resurrect it — the read already released it. |
| 80 | + switchSession(original) |
| 81 | + expect(getLastCacheSafeParams()).toBeNull() |
| 82 | + }) |
| 83 | + |
| 84 | + test('a save after the session switch is valid for the new session', () => { |
| 85 | + saveCacheSafeParams(makeParams('old')) |
| 86 | + regenerateSessionId() |
| 87 | + const fresh = makeParams('new-session') |
| 88 | + saveCacheSafeParams(fresh) |
| 89 | + expect(getLastCacheSafeParams()).toBe(fresh) |
| 90 | + }) |
| 91 | +}) |
0 commit comments