|
| 1 | +import { z } from "zod" |
| 2 | + |
| 3 | +/** |
| 4 | + * TaskPermissions defines fine-grained permission boundaries for a subtask. |
| 5 | + * |
| 6 | + * These permissions allow the orchestrator (or parent task) to restrict what |
| 7 | + * a child task can do, making parallel execution safer by preventing |
| 8 | + * unintended side effects across task boundaries. |
| 9 | + * |
| 10 | + * ## Design Notes |
| 11 | + * |
| 12 | + * Phase 3a introduces the types and plumbing. Enforcement is deferred to |
| 13 | + * Phase 3b (read-only parallelism) and Phase 3d (write parallelism). |
| 14 | + * |
| 15 | + * The permission model is intentionally additive: if no permissions are |
| 16 | + * specified, the task inherits full capabilities from its mode. Permissions |
| 17 | + * can only *restrict*, never *expand* beyond what the mode allows. |
| 18 | + */ |
| 19 | +export const taskPermissionsSchema = z.object({ |
| 20 | + /** |
| 21 | + * Glob patterns restricting which files the task may read. |
| 22 | + * If empty or undefined, the task can read any file (subject to mode restrictions). |
| 23 | + * Examples: ["docs/**", "src/utils/**"] |
| 24 | + */ |
| 25 | + fileReadPatterns: z.array(z.string()).optional(), |
| 26 | + |
| 27 | + /** |
| 28 | + * Glob patterns restricting which files the task may write/edit. |
| 29 | + * If empty or undefined, the task can write any file (subject to mode restrictions). |
| 30 | + * Examples: ["docs/**", "package.json"] |
| 31 | + */ |
| 32 | + fileWritePatterns: z.array(z.string()).optional(), |
| 33 | + |
| 34 | + /** |
| 35 | + * Allowlist of shell commands the task may execute. |
| 36 | + * If empty or undefined, the task can execute any command (subject to mode restrictions). |
| 37 | + * Matched as prefixes against the command string. |
| 38 | + * Examples: ["npm test", "npx vitest", "git status"] |
| 39 | + */ |
| 40 | + allowedCommands: z.array(z.string()).optional(), |
| 41 | + |
| 42 | + /** |
| 43 | + * Blocklist of shell commands the task may NOT execute. |
| 44 | + * Takes precedence over allowedCommands. |
| 45 | + * Examples: ["rm -rf", "git push"] |
| 46 | + */ |
| 47 | + blockedCommands: z.array(z.string()).optional(), |
| 48 | + |
| 49 | + /** |
| 50 | + * Whether the task is restricted to read-only operations. |
| 51 | + * When true, the task cannot use write tools (write_to_file, apply_diff, |
| 52 | + * execute_command, etc.). This is the primary mechanism for Phase 3b |
| 53 | + * read-only parallelism. |
| 54 | + */ |
| 55 | + readOnly: z.boolean().optional(), |
| 56 | + |
| 57 | + /** |
| 58 | + * Explicit list of tool names the task is allowed to use. |
| 59 | + * If empty or undefined, all tools available to the mode are allowed. |
| 60 | + * Examples: ["read_file", "list_files", "search_files"] |
| 61 | + */ |
| 62 | + allowedTools: z.array(z.string()).optional(), |
| 63 | +}) |
| 64 | + |
| 65 | +export type TaskPermissions = z.infer<typeof taskPermissionsSchema> |
| 66 | + |
| 67 | +/** |
| 68 | + * TaskContext encapsulates all per-task configuration that a Task needs |
| 69 | + * to operate independently of the ClineProvider's shared mutable state. |
| 70 | + * |
| 71 | + * ## Purpose |
| 72 | + * |
| 73 | + * Today, Task reads mode, API config, and other settings from the provider |
| 74 | + * via `provider.getState()` at construction time and during execution. |
| 75 | + * This couples Task execution to the provider's current state, which |
| 76 | + * prevents multiple tasks from running concurrently (since they'd all |
| 77 | + * read/write the same shared state). |
| 78 | + * |
| 79 | + * TaskContext captures a snapshot of everything a Task needs at creation |
| 80 | + * time, so the Task can operate with its own isolated configuration. |
| 81 | + * |
| 82 | + * ## Lifecycle |
| 83 | + * |
| 84 | + * 1. Built by the parent (orchestrator or provider) when creating a subtask |
| 85 | + * 2. Passed to the Task constructor as an immutable snapshot |
| 86 | + * 3. The Task uses this context instead of reaching back to the provider |
| 87 | + * for mode/config/permissions during execution |
| 88 | + * |
| 89 | + * ## Phase 3a Scope |
| 90 | + * |
| 91 | + * In Phase 3a, TaskContext is optional -- tasks that don't receive one |
| 92 | + * fall back to the existing provider.getState() behavior. This ensures |
| 93 | + * full backward compatibility while enabling incremental adoption. |
| 94 | + */ |
| 95 | +export const taskContextSchema = z.object({ |
| 96 | + /** |
| 97 | + * The mode slug for this task (e.g., "code", "architect", "ask"). |
| 98 | + * Snapshot at task creation time -- does not change if the provider's |
| 99 | + * mode changes later. |
| 100 | + */ |
| 101 | + mode: z.string(), |
| 102 | + |
| 103 | + /** |
| 104 | + * The API configuration profile name for this task. |
| 105 | + * Allows subtasks to use different models (including local ones) |
| 106 | + * from the parent task. |
| 107 | + */ |
| 108 | + apiConfigName: z.string().optional(), |
| 109 | + |
| 110 | + /** |
| 111 | + * Permission boundaries for this task. |
| 112 | + * If undefined, the task inherits full capabilities from its mode. |
| 113 | + */ |
| 114 | + permissions: taskPermissionsSchema.optional(), |
| 115 | + |
| 116 | + /** |
| 117 | + * Whether this task should inherit skills from the parent. |
| 118 | + * Defaults to true if not specified. |
| 119 | + */ |
| 120 | + inheritSkills: z.boolean().optional(), |
| 121 | + |
| 122 | + /** |
| 123 | + * Additional skill overrides for this task. |
| 124 | + * These are merged with (or replace) inherited skills depending |
| 125 | + * on the inheritSkills setting. |
| 126 | + */ |
| 127 | + skillOverrides: z.array(z.string()).optional(), |
| 128 | + |
| 129 | + /** |
| 130 | + * The workspace path for this task. |
| 131 | + * Allows subtasks to operate in different workspace roots. |
| 132 | + */ |
| 133 | + workspacePath: z.string().optional(), |
| 134 | + |
| 135 | + /** |
| 136 | + * ID of the parent task that created this context. |
| 137 | + * Used for lineage tracking and result aggregation. |
| 138 | + */ |
| 139 | + parentTaskId: z.string().optional(), |
| 140 | + |
| 141 | + /** |
| 142 | + * ID of the root task in the delegation chain. |
| 143 | + * Used for hierarchical task management. |
| 144 | + */ |
| 145 | + rootTaskId: z.string().optional(), |
| 146 | +}) |
| 147 | + |
| 148 | +export type TaskContext = z.infer<typeof taskContextSchema> |
| 149 | + |
| 150 | +/** |
| 151 | + * Merge two TaskPermissions objects, producing the most restrictive |
| 152 | + * combination. This is used when a parent task's permissions should |
| 153 | + * further constrain a child task's permissions. |
| 154 | + * |
| 155 | + * Rules: |
| 156 | + * - readOnly: true if either is true |
| 157 | + * - allowedTools: intersection if both specified, otherwise the one that's specified |
| 158 | + * - fileReadPatterns / fileWritePatterns: intersection if both specified |
| 159 | + * - allowedCommands: intersection if both specified |
| 160 | + * - blockedCommands: union (all blocked commands from both) |
| 161 | + */ |
| 162 | +export function mergePermissions( |
| 163 | + parent: TaskPermissions | undefined, |
| 164 | + child: TaskPermissions | undefined, |
| 165 | +): TaskPermissions | undefined { |
| 166 | + if (!parent && !child) { |
| 167 | + return undefined |
| 168 | + } |
| 169 | + |
| 170 | + if (!parent) { |
| 171 | + return child |
| 172 | + } |
| 173 | + |
| 174 | + if (!child) { |
| 175 | + return parent |
| 176 | + } |
| 177 | + |
| 178 | + return { |
| 179 | + readOnly: parent.readOnly || child.readOnly || undefined, |
| 180 | + |
| 181 | + fileReadPatterns: intersectArrays(parent.fileReadPatterns, child.fileReadPatterns), |
| 182 | + |
| 183 | + fileWritePatterns: intersectArrays(parent.fileWritePatterns, child.fileWritePatterns), |
| 184 | + |
| 185 | + allowedCommands: intersectArrays(parent.allowedCommands, child.allowedCommands), |
| 186 | + |
| 187 | + blockedCommands: unionArrays(parent.blockedCommands, child.blockedCommands), |
| 188 | + |
| 189 | + allowedTools: intersectArrays(parent.allowedTools, child.allowedTools), |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +/** Return intersection of two optional arrays, or the defined one if only one exists. */ |
| 194 | +function intersectArrays(a: string[] | undefined, b: string[] | undefined): string[] | undefined { |
| 195 | + if (!a && !b) { |
| 196 | + return undefined |
| 197 | + } |
| 198 | + |
| 199 | + if (!a) { |
| 200 | + return b |
| 201 | + } |
| 202 | + |
| 203 | + if (!b) { |
| 204 | + return a |
| 205 | + } |
| 206 | + |
| 207 | + const setB = new Set(b) |
| 208 | + const result = a.filter((item) => setB.has(item)) |
| 209 | + return result.length > 0 ? result : [] |
| 210 | +} |
| 211 | + |
| 212 | +/** Return union of two optional arrays. */ |
| 213 | +function unionArrays(a: string[] | undefined, b: string[] | undefined): string[] | undefined { |
| 214 | + if (!a && !b) { |
| 215 | + return undefined |
| 216 | + } |
| 217 | + |
| 218 | + if (!a) { |
| 219 | + return b |
| 220 | + } |
| 221 | + |
| 222 | + if (!b) { |
| 223 | + return a |
| 224 | + } |
| 225 | + |
| 226 | + return Array.from(new Set([...a, ...b])) |
| 227 | +} |
0 commit comments