( command: string, abortSignal: AbortSignal, isNonInteractiveSession: boolean, toolName: string, policySpec: string, eventName: string, querySource: QuerySource, preCheck?: (command: string) => CommandPrefixResult | null, )
| 170 | } |
| 171 | |
| 172 | async function getCommandPrefixImpl( |
| 173 | command: string, |
| 174 | abortSignal: AbortSignal, |
| 175 | isNonInteractiveSession: boolean, |
| 176 | toolName: string, |
| 177 | policySpec: string, |
| 178 | eventName: string, |
| 179 | querySource: QuerySource, |
| 180 | preCheck?: (command: string) => CommandPrefixResult | null, |
| 181 | ): Promise<CommandPrefixResult | null> { |
| 182 | if (process.env.NODE_ENV === 'test') { |
| 183 | return null |
| 184 | } |
| 185 | |
| 186 | // Run pre-check if provided (e.g., isHelpCommand for Bash) |
| 187 | if (preCheck) { |
| 188 | const preCheckResult = preCheck(command) |
| 189 | if (preCheckResult !== null) { |
| 190 | return preCheckResult |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | let preflightCheckTimeoutId: NodeJS.Timeout | undefined |
| 195 | const startTime = Date.now() |
| 196 | let result: CommandPrefixResult | null = null |
| 197 | |
| 198 | try { |
| 199 | // Log a warning if the pre-flight check takes too long |
| 200 | preflightCheckTimeoutId = setTimeout( |
| 201 | (tn, nonInteractive) => { |
| 202 | const message = `[${tn}Tool] Pre-flight check is taking longer than expected. Run with ANTHROPIC_LOG=debug to check for failed or slow API requests.` |
| 203 | if (nonInteractive) { |
| 204 | process.stderr.write(jsonStringify({ level: 'warn', message }) + '\n') |
| 205 | } else { |
| 206 | // biome-ignore lint/suspicious/noConsole: intentional warning |
| 207 | console.warn(chalk.yellow(`⚠️ ${message}`)) |
| 208 | } |
| 209 | }, |
| 210 | 10000, // 10 seconds |
| 211 | toolName, |
| 212 | isNonInteractiveSession, |
| 213 | ) |
| 214 | |
| 215 | const useSystemPromptPolicySpec = getFeatureValue_CACHED_MAY_BE_STALE( |
| 216 | 'tengu_cork_m4q', |
| 217 | false, |
| 218 | ) |
| 219 | |
| 220 | const response = await queryHaiku({ |
| 221 | systemPrompt: asSystemPrompt( |
| 222 | useSystemPromptPolicySpec |
| 223 | ? [ |
| 224 | `Your task is to process ${toolName} commands that an AI coding agent wants to run.\n\n${policySpec}`, |
| 225 | ] |
| 226 | : [ |
| 227 | `Your task is to process ${toolName} commands that an AI coding agent wants to run.\n\nThis policy spec defines how to determine the prefix of a ${toolName} command:`, |
| 228 | ], |
| 229 | ), |
no test coverage detected