( params: zod.infer<typeof AskSchema>, logger: Logger = gatewayLog, )
| 457 | } |
| 458 | |
| 459 | async function handleAsk( |
| 460 | params: zod.infer<typeof AskSchema>, |
| 461 | logger: Logger = gatewayLog, |
| 462 | ) { |
| 463 | if (!existsSync(params.dir)) { |
| 464 | return {success: false, error: `Directory "${params.dir}" does not exist`}; |
| 465 | } |
| 466 | |
| 467 | // Scope enforcement: coding requires a folder with `coding` scope |
| 468 | if (listFolders().length > 0) { |
| 469 | const resolved = resolveFolderForPath(params.dir, 'coding'); |
| 470 | if (!resolved) { |
| 471 | const err = folderScopeError(params.dir, 'coding'); |
| 472 | return { |
| 473 | success: false, |
| 474 | error: `${err.error.code}: ${err.error.message}`, |
| 475 | }; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (params.worktree && (!params.baseBranch || !params.branch)) { |
| 480 | return { |
| 481 | success: false, |
| 482 | error: |
| 483 | 'Parameters "baseBranch" and "branch" are required when worktree is true', |
| 484 | }; |
| 485 | } |
| 486 | |
| 487 | const resolved = resolveAgent(params.agent); |
| 488 | if ('error' in resolved) return {success: false, error: resolved.error}; |
| 489 | const agentName = resolved.agent; |
| 490 | |
| 491 | let config = getAgentConfig(agentName); |
| 492 | if (!config) { |
| 493 | return { |
| 494 | success: false, |
| 495 | error: `Agent "${agentName}" not configured. Run 'corebrain coding config --agent ${agentName}' to set up.`, |
| 496 | }; |
| 497 | } |
| 498 | |
| 499 | const isResume = Boolean(params.sessionId); |
| 500 | let sessionId = params.sessionId || randomUUID(); |
| 501 | |
| 502 | // On resume, if a previous PTY is still alive for this id, kill it before |
| 503 | // respawning. The TUI has no API to inject a new prompt into a running |
| 504 | // instance, so the only way to deliver `params.prompt` is a fresh |
| 505 | // `--resume <id>` process. SIGTERM gives the agent a chance to flush its |
| 506 | // transcript; we escalate to SIGKILL only if it doesn't exit in time. |
| 507 | if (isResume && isProcessRunning(sessionId)) { |
| 508 | logger(`coding_ask resume: killing running pty for sessionId=${sessionId}`); |
| 509 | stopProcess(sessionId); |
| 510 | let exited = await waitForProcessExit(sessionId, 5_000); |
| 511 | if (!exited) { |
| 512 | logger(`coding_ask resume: SIGTERM did not exit, escalating to SIGKILL`); |
| 513 | ptyManager.kill(sessionId, 'SIGKILL'); |
| 514 | exited = await waitForProcessExit(sessionId, 2_000); |
| 515 | } |
| 516 | if (!exited) { |
no test coverage detected