( request: SwarmPermissionRequest, )
| 213 | * @returns The written request |
| 214 | */ |
| 215 | export async function writePermissionRequest( |
| 216 | request: SwarmPermissionRequest, |
| 217 | ): Promise<SwarmPermissionRequest> { |
| 218 | await ensurePermissionDirsAsync(request.teamName) |
| 219 | |
| 220 | const pendingPath = getPendingRequestPath(request.teamName, request.id) |
| 221 | const lockDir = getPendingDir(request.teamName) |
| 222 | |
| 223 | // Create a directory-level lock file for atomic writes |
| 224 | const lockFilePath = join(lockDir, '.lock') |
| 225 | await writeFile(lockFilePath, '', 'utf-8') |
| 226 | |
| 227 | let release: (() => Promise<void>) | undefined |
| 228 | try { |
| 229 | release = await lockfile.lock(lockFilePath) |
| 230 | |
| 231 | // Write the request file |
| 232 | await writeFile(pendingPath, jsonStringify(request, null, 2), 'utf-8') |
| 233 | |
| 234 | logForDebugging( |
| 235 | `[PermissionSync] Wrote pending request ${request.id} from ${request.workerName} for ${request.toolName}`, |
| 236 | ) |
| 237 | |
| 238 | return request |
| 239 | } catch (error) { |
| 240 | logForDebugging( |
| 241 | `[PermissionSync] Failed to write permission request: ${error}`, |
| 242 | ) |
| 243 | logError(error) |
| 244 | throw error |
| 245 | } finally { |
| 246 | if (release) { |
| 247 | await release() |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Read all pending permission requests for a team |
nothing calls this directly
no test coverage detected