|
| 1 | +import * as pathe from 'pathe'; |
| 2 | + |
| 3 | +import type { ResolvedToolExecutionHookContext } from '#/agent/toolExecutor/toolHooks'; |
| 4 | +import { IHostEnvironment } from '#/os/interface/hostEnvironment'; |
| 5 | +import type { IHostEnvironment as HostEnvironment } from '#/os/interface/hostEnvironment'; |
| 6 | +import { ISessionWorkspaceContext } from '#/session/workspaceContext/workspaceContext'; |
| 7 | +import type { ISessionWorkspaceContext as WorkspaceContext } from '#/session/workspaceContext/workspaceContext'; |
| 8 | +import type { |
| 9 | + PermissionPolicy, |
| 10 | + PermissionPolicyResult, |
| 11 | +} from '#/agent/permissionPolicy/types'; |
| 12 | + |
| 13 | +import { writeFileAccesses } from './path-utils'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Files whose contents a routine follow-up command executes. |
| 17 | + * |
| 18 | + * Writes inside the workspace are otherwise approved without asking, which is |
| 19 | + * the right default for source files: editing them is the job, and the change |
| 20 | + * is visible in the diff before anything runs it. These are different. Nothing |
| 21 | + * happens when they are written, and then the next `npm install`, test run, or |
| 22 | + * CI job executes what they now say — so the write is the dangerous act and |
| 23 | + * the prompt has to happen there, not at the point it finally runs. |
| 24 | + */ |
| 25 | +const EXECUTION_TRIGGER_BASENAMES = new Set<string>([ |
| 26 | + 'package.json', |
| 27 | + 'makefile', |
| 28 | + 'gnumakefile', |
| 29 | + 'justfile', |
| 30 | + 'taskfile.yml', |
| 31 | + 'taskfile.yaml', |
| 32 | + 'jenkinsfile', |
| 33 | + 'setup.py', |
| 34 | + 'conftest.py', |
| 35 | + '.pre-commit-config.yaml', |
| 36 | + '.gitlab-ci.yml', |
| 37 | + 'azure-pipelines.yml', |
| 38 | +]); |
| 39 | + |
| 40 | +/** |
| 41 | + * Directories where every file is executed by CI or a git operation. |
| 42 | + * Compared against workspace-relative POSIX paths. |
| 43 | + */ |
| 44 | +const EXECUTION_TRIGGER_DIR_PREFIXES = [ |
| 45 | + '.github/workflows/', |
| 46 | + '.github/actions/', |
| 47 | + '.circleci/', |
| 48 | + '.husky/', |
| 49 | +]; |
| 50 | + |
| 51 | +export function isExecutionTriggerPath(relativePath: string): boolean { |
| 52 | + const normalized = relativePath.replaceAll('\\', '/').toLowerCase(); |
| 53 | + if (normalized.startsWith('../')) return false; |
| 54 | + if (EXECUTION_TRIGGER_BASENAMES.has(pathe.basename(normalized))) return true; |
| 55 | + return EXECUTION_TRIGGER_DIR_PREFIXES.some((prefix) => normalized.startsWith(prefix)); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Ask before writing a file that a later command will execute. |
| 60 | + * |
| 61 | + * Sits ahead of the blanket in-workspace write approval, and ahead of auto |
| 62 | + * mode, for the same reason the sensitive-file check does: these are the |
| 63 | + * writes where "it was inside the repo" is not a good enough reason to skip |
| 64 | + * the prompt. Session history and user `allow` rules still take precedence, |
| 65 | + * so an operator who has decided this is fine is not asked twice. |
| 66 | + */ |
| 67 | +export class ExecutionTriggerWriteAskPermissionPolicyService implements PermissionPolicy { |
| 68 | + readonly name = 'execution-trigger-write-ask'; |
| 69 | + |
| 70 | + constructor( |
| 71 | + @IHostEnvironment private readonly env: HostEnvironment, |
| 72 | + @ISessionWorkspaceContext private readonly workspace: WorkspaceContext, |
| 73 | + ) {} |
| 74 | + |
| 75 | + evaluate(context: ResolvedToolExecutionHookContext): PermissionPolicyResult | undefined { |
| 76 | + const cwd = this.workspace.workDir; |
| 77 | + if (cwd.length === 0) return undefined; |
| 78 | + const writes = writeFileAccesses(context); |
| 79 | + if (writes.length === 0) return undefined; |
| 80 | + |
| 81 | + const pathClass = this.env.pathClass; |
| 82 | + const base = pathClass === 'win32' ? cwd.toLowerCase() : cwd; |
| 83 | + const triggers = writes.some((access) => { |
| 84 | + const target = pathClass === 'win32' ? access.path.toLowerCase() : access.path; |
| 85 | + return isExecutionTriggerPath(pathe.relative(base, target)); |
| 86 | + }); |
| 87 | + return triggers ? { kind: 'ask' } : undefined; |
| 88 | + } |
| 89 | +} |
0 commit comments