Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

23.1 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'

import { GuidanceEngine, triggerMatches, type GuidanceCall } from '@/hono/guidance/engine'
import { GUIDANCE_RULES, type GuidanceRule } from '@/hono/guidance/rules'
import type { ResolvedState } from '@/hono/request-state-resolver'

import { makeToolExecutorState } from '../shared/test-utils'
import { makeGuidanceRedis } from './helpers/guidance-redis-stubs'

const rule: GuidanceRule = {
    id: 'test-rule',
    flag: 'mcp-guidance-test',
    trigger: {
        tool: 'execute-sql',
        argPath: 'query',
        argPattern: /session_replay_events/i,
        outcome: 'any',
        presets: ['user'],
    },
    threshold: 2,
    maxPerSession: 1,
    suppressIfCalled: ['vision-*'],
    requiresTool: ['vision-scanners-inline-scan', 'vision-requests-get'],
    text: 'Try Replay Vision.',
}

const replayQuery: GuidanceCall = {
    toolName: 'execute-sql',
    args: { query: 'select session_id from session_replay_events' },
    isError: false,
}
const otherQuery: GuidanceCall = { toolName: 'execute-sql', args: { query: 'select 1 from events' }, isError: false }
const visionCall: GuidanceCall = { toolName: 'vision-scanners-inline-scan', args: {}, isError: false }

function makeState(overrides: Partial<ResolvedState> = {}): ResolvedState {
    return makeToolExecutorState(
        [{ name: 'execute-sql' }, { name: 'vision-scanners-inline-scan' }, { name: 'vision-requests-get' }],
        {
            toolFeatureFlags: { [rule.flag]: true },
            suppressAnalytics: false,
            ...overrides,
        }
    )
}

async function run(
    engine: GuidanceEngine,
    state: ResolvedState,
    calls: GuidanceCall[]
): Promise<(string | undefined)[]> {
    const shown: (string | undefined)[] = []
    for (const call of calls) {
        const notice = await engine.evaluate(state, call)
        shown.push(notice && `${notice.ruleId}:${notice.outcome}`)
    }
    return shown
}

describe('GuidanceEngine', () => {
    it('shows the notice once the session reaches the threshold, and only once', async () => {
        const engine = new GuidanceEngine(makeGuidanceRedis(), [rule])
        await expect(run(engine, makeState(), [replayQuery, otherQuery, replayQuery, replayQuery])).resolves.toEqual([
            undefined,
            undefined,
            'test-rule:shown',
            undefined,
        ])
    })

    it('stamps a suppressed outcome once when the session already called a suggested tool', async () => {
        const engine = new GuidanceEngine(makeGuidanceRedis(), [rule])
        await expect(run(engine, makeState(), [visionCall, replayQuery, replayQuery, replayQuery])).resolves.toEqual([
            undefined,
            undefined,
            'test-rule:suppressed',
            undefined,
        ])
    })

    it.each([
        ['without org consent', false, 'no_consent'],
        ['when consent is unknown', undefined, 'no_consent'],
        ['with org consent', true, 'shown'],
    ])('stamps the consent outcome %s for a rule that needs it', async (_label, aiConsentGiven, outcome) => {
        const engine = new GuidanceEngine(makeGuidanceRedis(), [{ ...rule, requiresAiConsent: true }])
        const state = makeState({
            aiConsentGiven,
            ...(aiConsentGiven === true ? {} : { allTools: [{ name: 'execute-sql' }] as ResolvedState['allTools'] }),
        })
        await engine.evaluate(state, replayQuery)
        const notice = await engine.evaluate(state, replayQuery)
        expect(notice).toMatchObject({ ruleId: 'test-rule', outcome })
        expect(notice?.text === undefined).toBe(outcome !== 'shown')
    })

    it('stamps a flag_off outcome without text when the flag is off', async () => {
        const engine = new GuidanceEngine(makeGuidanceRedis(), [rule])
        const state = makeState({ toolFeatureFlags: {} })
        await engine.evaluate(state, replayQuery)
        await expect(engine.evaluate(state, replayQuery)).resolves.toEqual({
            ruleId: 'test-rule',
            outcome: 'flag_off',
        })
    })

    it.each([
        [
            'sessions',
            (first: ResolvedState): Partial<ResolvedState> => ({
                requestContext: { ...first.requestContext, mcpSessionId: 'sess-2' },
            }),
        ],
        ['callers echoing the same session handle', (): Partial<ResolvedState> => ({ distinctId: 'other-caller' })],
    ])('keeps %s apart', async (_label, overrides) => {
        const engine = new GuidanceEngine(makeGuidanceRedis(), [rule])
        const first = makeState({ requestContext: { ...makeState().requestContext, mcpSessionId: 'sess-1' } })
        const second = makeState({ ...first, ...overrides(first) })
        await run(engine, first, [replayQuery])
        await expect(run(engine, second, [replayQuery, replayQuery])).resolves.toEqual([undefined, 'test-rule:shown'])
    })

    describe('a client that sends no handle', () => {
        const withHandle = (id: string, carriedContextHandle: boolean): ResolvedState =>
            makeState({
                carriedContextHandle,
                requestContext: { ...makeState().requestContext, mcpConversationId: id },
            })

        it('counts minted handles under the caller', async () => {
            const engine = new GuidanceEngine(makeGuidanceRedis(), [rule])
            await run(engine, withHandle('minted-1', false), [replayQuery])
            await expect(run(engine, withHandle('minted-2', false), [replayQuery])).resolves.toEqual([
                'test-rule:shown',
            ])
        })

        it.each([
            ['the match count', replayQuery, ['test-rule:shown', undefined]],
            ['suppression', visionCall, [undefined, 'test-rule:suppressed']],
        ])('keeps %s when the client later echoes the minted handle', async (_label, firstCall, expected) => {
            const engine = new GuidanceEngine(makeGuidanceRedis(), [rule])
            await run(engine, withHandle('minted-1', false), [firstCall])
            await expect(run(engine, withHandle('minted-1', true), [replayQuery, replayQuery])).resolves.toEqual(
                expected
            )
        })

        describe('with a notice that fires on the first match', () => {
            const firstMatchRule: GuidanceRule = { ...rule, threshold: 1 }

            it.each([
                ['after the caller counters expire', [31]],
                ['after the first day of a busy conversation', Array.from({ length: 80 }, () => 20)],
            ])('keeps the per-conversation cap %s', async (_label, gapsInMinutes) => {
                const redis = makeGuidanceRedis()
                const engine = new GuidanceEngine(redis, [firstMatchRule])
                const outcomes = await run(engine, withHandle('minted-1', false), [replayQuery])
                for (const minutes of gapsInMinutes) {
                    redis.advance(minutes * 60)
                    outcomes.push(...(await run(engine, withHandle('minted-1', true), [replayQuery])))
                }
                expect(outcomes.filter(Boolean)).toEqual(['test-rule:shown'])
            })

            it('gives a second conversation its own notice once it echoes its handle', async () => {
                const engine = new GuidanceEngine(makeGuidanceRedis(), [firstMatchRule])
                await run(engine, withHandle('minted-1', false), [replayQuery])
                await expect(run(engine, withHandle('minted-2', false), [replayQuery])).resolves.toEqual([undefined])
                await expect(run(engine, withHandle('minted-2', true), [replayQuery, replayQuery])).resolves.toEqual([
                    'test-rule:shown',
                    undefined,
                ])
            })

            it('does not repeat in the conversation that already got the notice from the caller counters', async () => {
                const engine = new GuidanceEngine(makeGuidanceRedis(), [firstMatchRule])
                await run(engine, withHandle('minted-1', false), [replayQuery])
                await run(engine, withHandle('minted-2', false), [replayQuery])
                await expect(run(engine, withHandle('minted-1', true), [replayQuery])).resolves.toEqual([undefined])
            })

            it('caps a client that never echoes under the caller', async () => {
                const engine = new GuidanceEngine(makeGuidanceRedis(), [firstMatchRule])
                await run(engine, withHandle('minted-1', false), [replayQuery])
                await expect(run(engine, withHandle('minted-2', false), [replayQuery])).resolves.toEqual([undefined])
            })
        })
    })

    it('reports a suppressed outcome on a later call when another rule wins the threshold call', async () => {
        const second: GuidanceRule = {
            ...rule,
            id: 'second-rule',
            flag: 'mcp-guidance-second',
            suppressIfCalled: ['read-data-schema'],
        }
        const engine = new GuidanceEngine(makeGuidanceRedis(), [rule, second])
        const state = makeState({ toolFeatureFlags: { [rule.flag]: true, [second.flag]: true } })
        const schemaCall: GuidanceCall = { toolName: 'read-data-schema', args: {}, isError: false }
        await expect(run(engine, state, [schemaCall, replayQuery, replayQuery, replayQuery])).resolves.toEqual([
            undefined,
            undefined,
            'test-rule:shown',
            'second-rule:suppressed',
        ])
    })

    it('reserves the allowance once when matching calls run concurrently', async () => {
        const engine = new GuidanceEngine(makeGuidanceRedis(), [rule])
        const state = makeState()
        await engine.evaluate(state, replayQuery)
        const shown = await Promise.all([engine.evaluate(state, replayQuery), engine.evaluate(state, replayQuery)])
        expect(shown.filter(Boolean)).toHaveLength(1)
    })

    it('lets a later rule keep its allowance when an earlier rule fires on the same call', async () => {
        const second: GuidanceRule = { ...rule, id: 'second-rule', flag: 'mcp-guidance-second' }
        const redis = makeGuidanceRedis()
        const engine = new GuidanceEngine(redis, [rule, second])
        const state = makeState({ toolFeatureFlags: { [rule.flag]: true, [second.flag]: true } })
        await run(engine, state, [replayQuery, replayQuery])
        expect([...redis.store.keys()].filter((key) => key.endsWith(':shown'))).toEqual([
            expect.stringContaining(':test-rule:shown'),
        ])
    })

    it.each([
        ['impersonated session', { isImpersonated: true }],
        ['analytics suppressed', { suppressAnalytics: true }],
        ['sandbox token', { apiKeyScopes: ['internal_run:read'] }],
        ['scout token', { apiKeyScopes: ['signal_scout_internal:write'] }],
        ['suggested tool not in roster', { allTools: [{ name: 'execute-sql' }] as ResolvedState['allTools'] }],
        [
            'one named tool missing from roster',
            {
                allTools: [
                    { name: 'execute-sql' },
                    { name: 'vision-scanners-inline-scan' },
                ] as ResolvedState['allTools'],
            },
        ],
    ])('shows nothing for %s', async (_label, overrides) => {
        const engine = new GuidanceEngine(makeGuidanceRedis(), [rule])
        await expect(run(engine, makeState(overrides), [replayQuery, replayQuery])).resolves.toEqual([
            undefined,
            undefined,
        ])
    })

    it('shows the notice to a scout when the rule lists the scout preset', async () => {
        const scoutRule: GuidanceRule = { ...rule, trigger: { ...rule.trigger, presets: ['user', 'scout'] } }
        const engine = new GuidanceEngine(makeGuidanceRedis(), [scoutRule])
        const state = makeState({ apiKeyScopes: ['signal_scout_internal:write', 'internal_run:read'] })
        await expect(run(engine, state, [replayQuery, replayQuery])).resolves.toEqual([undefined, 'test-rule:shown'])
    })

    it('matches only the configured outcome', async () => {
        const errorRule: GuidanceRule = { ...rule, trigger: { ...rule.trigger, outcome: 'error' }, threshold: 1 }
        const engine = new GuidanceEngine(makeGuidanceRedis(), [errorRule])
        await expect(run(engine, makeState(), [replayQuery, { ...replayQuery, isError: true }])).resolves.toEqual([
            undefined,
            'test-rule:shown',
        ])
    })

    it.each([
        ['an empty result', true, true],
        ['a populated result', false, false],
        ['a result of unknown size', undefined, false],
    ])('a resultEmpty rule handles %s', (_label, resultEmpty, expected) => {
        const emptyRule: GuidanceRule = {
            ...rule,
            trigger: { tool: 'execute-sql', resultEmpty: true, outcome: 'any', presets: ['user'] },
        }
        expect(triggerMatches(emptyRule, { ...otherQuery, resultEmpty })).toBe(expected)
    })

    describe('a rule with a condition', () => {
        const conditionalRule = (condition: GuidanceRule['condition']): GuidanceRule => ({
            ...rule,
            threshold: 1,
            condition,
        })

        it('does not spend the session cap on a call the condition rejects', async () => {
            const condition = vi.fn().mockResolvedValueOnce(false).mockResolvedValue(true)
            const engine = new GuidanceEngine(makeGuidanceRedis(), [conditionalRule(condition)])
            await expect(run(engine, makeState(), [replayQuery, replayQuery])).resolves.toEqual([
                undefined,
                'test-rule:shown',
            ])
        })

        it('skips the condition once the rule reaches its cap and keeps its counters alive', async () => {
            const condition = vi.fn().mockResolvedValue(true)
            const redis = makeGuidanceRedis()
            const engine = new GuidanceEngine(redis, [conditionalRule(condition)])
            await run(engine, makeState(), [replayQuery, replayQuery])
            expect(condition).toHaveBeenCalledTimes(1)
            expect(redis.evalCalls.at(-1)?.[6]).toBe('touch')
        })

        it('counts an admitted match when an earlier rule wins the call', async () => {
            const first: GuidanceRule = { ...conditionalRule(vi.fn().mockResolvedValue(true)), id: 'first-rule' }
            const second: GuidanceRule = {
                ...conditionalRule(vi.fn().mockResolvedValue(true)),
                id: 'second-rule',
                threshold: 2,
            }
            const engine = new GuidanceEngine(makeGuidanceRedis(), [first, second])
            await expect(run(engine, makeState(), [replayQuery, replayQuery])).resolves.toEqual([
                'first-rule:shown',
                'second-rule:shown',
            ])
        })

        it('reserves no notice when the gate finishes after the budget', async () => {
            vi.useFakeTimers()
            try {
                const redis = makeGuidanceRedis()
                const evaluateScript = redis.eval.bind(redis)
                const lateEval = vi.spyOn(redis, 'eval').mockImplementation(async (script, keys, ...args) => {
                    await new Promise((resolve) => setTimeout(resolve, 400))
                    return evaluateScript(script, keys, ...args)
                })
                const engine = new GuidanceEngine(redis, [conditionalRule(vi.fn().mockResolvedValue(true))])
                const pending = engine.evaluate(makeState(), replayQuery)
                await vi.advanceTimersByTimeAsync(1000)
                await expect(pending).resolves.toBeUndefined()
                expect(lateEval.mock.calls.map((call) => call[8])).toEqual(['touch'])
            } finally {
                vi.useRealTimers()
            }
        })

        it.each(['touch', 'match'])('holds the tool result to the budget when Redis stalls on %s', async (stalled) => {
            vi.useFakeTimers()
            try {
                const redis = makeGuidanceRedis()
                vi.spyOn(redis, 'eval').mockImplementation((_script, _keys, ...args) =>
                    args[6] === 'touch' && stalled === 'match' ? Promise.resolve(0) : new Promise(() => {})
                )
                const admitted = (id: string): GuidanceRule => ({
                    ...conditionalRule(vi.fn().mockResolvedValue(true)),
                    id,
                })
                const engine = new GuidanceEngine(redis, [admitted('a'), admitted('b'), admitted('c')])
                const pending = engine.evaluate(makeState(), replayQuery)
                await vi.advanceTimersByTimeAsync(310)
                await expect(pending).resolves.toBeUndefined()
            } finally {
                vi.useRealTimers()
            }
        })

        it('applies the condition to a caller with the flag off', async () => {
            const condition = vi.fn().mockResolvedValueOnce(false).mockResolvedValue(true)
            const engine = new GuidanceEngine(makeGuidanceRedis(), [conditionalRule(condition)])
            const state = makeState({ toolFeatureFlags: {} })
            await expect(run(engine, state, [replayQuery, replayQuery])).resolves.toEqual([
                undefined,
                'test-rule:flag_off',
            ])
        })

        it('records suppression without reading the condition', async () => {
            const condition = vi.fn().mockResolvedValue(true)
            const engine = new GuidanceEngine(makeGuidanceRedis(), [conditionalRule(condition)])
            await expect(run(engine, makeState(), [visionCall, replayQuery])).resolves.toEqual([
                undefined,
                'test-rule:suppressed',
            ])
            expect(condition).toHaveBeenCalledTimes(1)
        })
    })

    it('builds function text from the call, only when the notice is shown', async () => {
        const text = vi.fn((call: GuidanceCall) => `Used ${call.toolName}.`)
        const engine = new GuidanceEngine(makeGuidanceRedis(), [{ ...rule, threshold: 1, text }])
        await expect(engine.evaluate(makeState({ toolFeatureFlags: {} }), replayQuery)).resolves.toEqual({
            ruleId: 'test-rule',
            outcome: 'flag_off',
        })
        expect(text).not.toHaveBeenCalled()
        const shown = await new GuidanceEngine(makeGuidanceRedis(), [{ ...rule, threshold: 1, text }]).evaluate(
            makeState(),
            replayQuery
        )
        expect(shown?.text).toBe('Used execute-sql.')
    })

    it('shows nothing and logs when Redis fails', async () => {
        const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
        const engine = new GuidanceEngine(makeGuidanceRedis(true), [{ ...rule, threshold: 1 }])
        await expect(engine.evaluate(makeState(), replayQuery)).resolves.toBeUndefined()
        expect(warn).toHaveBeenCalledWith('[Guidance] evaluation failed:', expect.any(String))
        warn.mockRestore()
    })

    it.each([
        ['replay-sql-to-vision', 'select 1 from session_replay_events', true],
        ['replay-sql-to-vision', 'select 1 from raw_session_replay_events', true],
        ['replay-sql-to-vision', 'select 1 from session_replay_features', true],
        ['replay-sql-to-vision', 'select 1 from console_logs_log_entries', true],
        ['replay-sql-to-vision', 'select 1 from system.session_recordings', true],
        ['replay-sql-to-vision', 'select 1 from log_entries', false],
        ['replay-sql-to-vision', 'select 1 from batch_export_log_entries', false],
        ['replay-sql-to-vision', 'select 1 from sessions', false],
        ['replay-sql-to-vision', 'select 1 from events', false],
        [
            'sql-aggregate-to-typed-runner',
            "SELECT toDate(timestamp) AS day, count() FROM events WHERE event = 'signup' GROUP BY day",
            true,
        ],
        ['sql-aggregate-to-typed-runner', 'select properties.$browser, uniq(person_id)\nfrom events\ngroup by 1', true],
        ['sql-aggregate-to-typed-runner', "SELECT countIf(event = 'a') FROM events", true],
        ['sql-aggregate-to-typed-runner', 'SELECT event, timestamp FROM events LIMIT 10', false],
        ['sql-aggregate-to-typed-runner', 'SELECT count() FROM events e JOIN persons p ON e.person_id = p.id', false],
        ['sql-aggregate-to-typed-runner', 'WITH x AS (SELECT 1) SELECT count() FROM events', false],
        ['sql-aggregate-to-typed-runner', 'SELECT count() OVER (PARTITION BY event) FROM events', false],
        ['sql-aggregate-to-typed-runner', 'SELECT count() FROM (SELECT person_id FROM events)', false],
        [
            'sql-aggregate-to-typed-runner',
            'SELECT count() FROM events WHERE person_id IN (SELECT id FROM persons)',
            false,
        ],
        [
            'sql-aggregate-to-typed-runner',
            'SELECT event, count(), max(timestamp) AS last_seen FROM events GROUP BY event',
            true,
        ],
        [
            'sql-aggregate-to-typed-runner',
            'SELECT event, uniq(person_id) FROM events GROUP BY event ORDER BY uniq(person_id) DESC',
            true,
        ],
        ['sql-aggregate-to-typed-runner', "SELECT max(timestamp), count() FROM events WHERE event = 'a'", false],
        ['sql-aggregate-to-typed-runner', 'SELECT person_id, count() FROM events GROUP BY person_id', false],
        ['sql-aggregate-to-typed-runner', 'SELECT uniq(person_id), groupArray(event) FROM events', false],
        ['sql-aggregate-to-typed-runner', "SELECT count(), avg(dateDiff('hour', timestamp, now())) FROM events", false],
        [
            'sql-aggregate-to-typed-runner',
            'SELECT event, count() FROM events GROUP BY event HAVING count() > 10',
            false,
        ],
        ['sql-aggregate-to-typed-runner', 'SELECT count() FROM events e, persons p WHERE e.person_id = p.id', false],
        ['sql-aggregate-to-typed-runner', 'SELECT person_id, count() FROM events GROUP BY 1', false],
        [
            'sql-aggregate-to-typed-runner',
            'SELECT properties.$session_id AS sid, count() FROM events GROUP BY sid',
            false,
        ],
        ['sql-aggregate-to-typed-runner', 'SELECT count(), max(e.timestamp) FROM events e', false],
        ['sql-aggregate-to-typed-runner', 'SELECT count() FROM system.insights', false],
        ['sql-aggregate-to-typed-runner', 'SELECT count() FROM posthog.ai_events', false],
        ['sql-aggregate-to-typed-runner', 'SELECT count() FROM events_archive', false],
    ])('the %s rule matches %j: %s', (ruleId, query, expected) => {
        const rule = GUIDANCE_RULES.find((r) => r.id === ruleId)!
        const call: GuidanceCall = { toolName: 'execute-sql', args: { query }, isError: false }
        expect(triggerMatches(rule, call)).toBe(expected)
    })

    it.each([
        ['a direct warehouse connection', { query: 'SELECT count() FROM events', connectionId: 'conn-1' }],
        ['a query longer than the cap', { query: `SELECT count() FROM events /* ${'x'.repeat(5000)} */` }],
    ])('the sql-aggregate-to-typed-runner rule skips %s', (_label, args) => {
        const rule = GUIDANCE_RULES.find((r) => r.id === 'sql-aggregate-to-typed-runner')!
        expect(triggerMatches(rule, { toolName: 'execute-sql', args, isError: false })).toBe(false)
    })

    it('ships every rule with a distinct id and flag', () => {
        expect(new Set(GUIDANCE_RULES.map((r) => r.id)).size).toBe(GUIDANCE_RULES.length)
        expect(new Set(GUIDANCE_RULES.map((r) => r.flag)).size).toBe(GUIDANCE_RULES.length)
    })
})