import { randomBytes } from "node:crypto"; // ============================================================ // Canary Token — Inject invisible markers into system prompts // If they leak into responses, prompt extraction is detected // ============================================================ export interface CanaryResult { injectedPrompt: string; canaryToken: string; } /** Inject a canary token into a system prompt */ export function injectCanary(systemPrompt: string): CanaryResult { const canaryToken = randomBytes(8).toString("hex"); const instruction = `\n\n`; return { injectedPrompt: systemPrompt + instruction, canaryToken, }; } /** Check if a canary token leaked into a response */ export function checkCanaryLeak( response: string, canaryToken: string, ): boolean { return response.includes(canaryToken); }