|
| 1 | +import * as crypto from "node:crypto"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as os from "node:os"; |
| 4 | +import * as path from "node:path"; |
| 5 | + |
| 6 | +const INTENT_NARRATION_LOG_FILE = "intent-narration.log"; |
| 7 | +const LOG_PREVIEW_LENGTH = 160; |
| 8 | + |
| 9 | +export const DEFAULT_INTENT_NARRATION_PHRASES = [ |
| 10 | + "let me run", |
| 11 | + "let me just", |
| 12 | + "let me execute", |
| 13 | + "I'll run it now", |
| 14 | + "I'll just run", |
| 15 | + "I'll just call", |
| 16 | + "running it now", |
| 17 | + "I'm going to run", |
| 18 | + "I will run it now", |
| 19 | + "doing it now", |
| 20 | + "executing now", |
| 21 | + "calling it now", |
| 22 | + "invoking now", |
| 23 | + "I'm running it", |
| 24 | + "for real", |
| 25 | + "no more loops", |
| 26 | +] as const; |
| 27 | + |
| 28 | +export const DEFAULT_INTENT_NARRATION_INSTRUCTION = "No prose intent. Emit the tool call now."; |
| 29 | + |
| 30 | +export type IntentNarrationGuardSettings = { |
| 31 | + enabled?: boolean; |
| 32 | + phrases?: string[]; |
| 33 | + additionalPhrases?: string[]; |
| 34 | + instruction?: string; |
| 35 | + hardStopRejections?: number; |
| 36 | + hardStopWindow?: number; |
| 37 | +}; |
| 38 | + |
| 39 | +export type ResolvedIntentNarrationGuardSettings = { |
| 40 | + enabled: boolean; |
| 41 | + phrases: string[]; |
| 42 | + instruction: string; |
| 43 | + hardStopRejections: number; |
| 44 | + hardStopWindow: number; |
| 45 | +}; |
| 46 | + |
| 47 | +export const DEFAULT_INTENT_NARRATION_GUARD_SETTINGS: ResolvedIntentNarrationGuardSettings = { |
| 48 | + enabled: true, |
| 49 | + phrases: [...DEFAULT_INTENT_NARRATION_PHRASES], |
| 50 | + instruction: DEFAULT_INTENT_NARRATION_INSTRUCTION, |
| 51 | + hardStopRejections: 4, |
| 52 | + hardStopWindow: 6, |
| 53 | +}; |
| 54 | + |
| 55 | +export type IntentNarrationRejectionEvent = { |
| 56 | + timestamp: string; |
| 57 | + sessionId: string; |
| 58 | + stepId: string; |
| 59 | + matchedPhrase: string; |
| 60 | + textHash: string; |
| 61 | + textPreview: string; |
| 62 | + totalRejections: number; |
| 63 | + windowRejections: number; |
| 64 | + windowSize: number; |
| 65 | + hardStopped: boolean; |
| 66 | +}; |
| 67 | + |
| 68 | +export function findIntentNarrationPhrase( |
| 69 | + content: string, |
| 70 | + hasToolCall: boolean, |
| 71 | + settings: ResolvedIntentNarrationGuardSettings |
| 72 | +): string | null { |
| 73 | + if (!settings.enabled || hasToolCall) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + const normalizedContent = normalizeForMatching(content); |
| 78 | + if (!normalizedContent) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + for (const phrase of settings.phrases) { |
| 83 | + const normalizedPhrase = normalizeForMatching(phrase); |
| 84 | + if (normalizedPhrase && normalizedContent.includes(normalizedPhrase)) { |
| 85 | + return phrase; |
| 86 | + } |
| 87 | + } |
| 88 | + return null; |
| 89 | +} |
| 90 | + |
| 91 | +export function recordRejectionInWindow(history: boolean[], rejected: boolean, windowSize: number): boolean[] { |
| 92 | + const boundedWindow = Math.max(1, Math.floor(windowSize)); |
| 93 | + return [...history, rejected].slice(-boundedWindow); |
| 94 | +} |
| 95 | + |
| 96 | +export function shouldHardStopIntentNarration( |
| 97 | + history: boolean[], |
| 98 | + settings: ResolvedIntentNarrationGuardSettings |
| 99 | +): boolean { |
| 100 | + if (settings.hardStopRejections <= 0) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + return history.filter(Boolean).length >= settings.hardStopRejections; |
| 104 | +} |
| 105 | + |
| 106 | +export function createIntentNarrationRejectionEvent(input: { |
| 107 | + content: string; |
| 108 | + sessionId: string; |
| 109 | + stepId: string; |
| 110 | + matchedPhrase: string; |
| 111 | + totalRejections: number; |
| 112 | + rejectionHistory: boolean[]; |
| 113 | + windowSize: number; |
| 114 | + hardStopped: boolean; |
| 115 | +}): IntentNarrationRejectionEvent { |
| 116 | + const normalizedPreview = input.content.replace(/\s+/g, " ").trim(); |
| 117 | + return { |
| 118 | + timestamp: new Date().toISOString(), |
| 119 | + sessionId: input.sessionId, |
| 120 | + stepId: input.stepId, |
| 121 | + matchedPhrase: input.matchedPhrase, |
| 122 | + textHash: `sha256:${crypto.createHash("sha256").update(input.content).digest("hex")}`, |
| 123 | + textPreview: |
| 124 | + normalizedPreview.length > LOG_PREVIEW_LENGTH |
| 125 | + ? `${normalizedPreview.slice(0, LOG_PREVIEW_LENGTH)}…` |
| 126 | + : normalizedPreview, |
| 127 | + totalRejections: input.totalRejections, |
| 128 | + windowRejections: input.rejectionHistory.filter(Boolean).length, |
| 129 | + windowSize: input.windowSize, |
| 130 | + hardStopped: input.hardStopped, |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +export function logIntentNarrationRejection(event: IntentNarrationRejectionEvent): void { |
| 135 | + try { |
| 136 | + const logPath = getIntentNarrationLogPath(); |
| 137 | + fs.mkdirSync(path.dirname(logPath), { recursive: true }); |
| 138 | + fs.appendFileSync(logPath, `${JSON.stringify(event)}\n`, "utf8"); |
| 139 | + } catch { |
| 140 | + // Guard diagnostics must never change agent-loop behavior. |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +export function getIntentNarrationLogPath(): string { |
| 145 | + return path.join(os.homedir(), ".deepcode", "logs", INTENT_NARRATION_LOG_FILE); |
| 146 | +} |
| 147 | + |
| 148 | +function normalizeForMatching(value: string): string { |
| 149 | + return value |
| 150 | + .normalize("NFKC") |
| 151 | + .replace(/[\u2018\u2019\u02bc]/g, "'") |
| 152 | + .replace(/\s+/g, " ") |
| 153 | + .trim() |
| 154 | + .toLowerCase(); |
| 155 | +} |
0 commit comments