( req: BatchRequest, sessionName: string, invoke: BatchInvoke, )
| 70 | | Extract<DaemonResponse, { ok: false }>; |
| 71 | |
| 72 | export async function runBatch( |
| 73 | req: BatchRequest, |
| 74 | sessionName: string, |
| 75 | invoke: BatchInvoke, |
| 76 | ): Promise<BatchRunResponse> { |
| 77 | const flags = readBatchFlags(req.flags); |
| 78 | const batchOnError = flags?.batchOnError ?? 'stop'; |
| 79 | if (batchOnError !== 'stop') { |
| 80 | return batchErrorResponse('INVALID_ARGS', `Unsupported batch on-error mode: ${batchOnError}.`); |
| 81 | } |
| 82 | const batchMaxSteps = flags?.batchMaxSteps ?? DEFAULT_BATCH_MAX_STEPS; |
| 83 | if (!isValidBatchMaxSteps(batchMaxSteps)) { |
| 84 | return batchErrorResponse( |
| 85 | 'INVALID_ARGS', |
| 86 | `Invalid batch max-steps: ${String(flags?.batchMaxSteps)}`, |
| 87 | ); |
| 88 | } |
| 89 | try { |
| 90 | const steps = validateAndNormalizeBatchSteps(flags?.batchSteps, batchMaxSteps); |
| 91 | const startedAt = Date.now(); |
| 92 | const partialResults: BatchStepResult[] = []; |
| 93 | for (const [index, step] of steps.entries()) { |
| 94 | const stepResponse = await runBatchStep( |
| 95 | req, |
| 96 | sessionName, |
| 97 | step, |
| 98 | invoke, |
| 99 | index + 1, |
| 100 | index === steps.length - 1, |
| 101 | ); |
| 102 | if (!stepResponse.ok) { |
| 103 | return { |
| 104 | ok: false, |
| 105 | error: { |
| 106 | code: stepResponse.error.code, |
| 107 | message: `Batch failed at step ${stepResponse.step} (${step.command}): ${stepResponse.error.message}`, |
| 108 | hint: stepResponse.error.hint, |
| 109 | diagnosticId: stepResponse.error.diagnosticId, |
| 110 | logPath: stepResponse.error.logPath, |
| 111 | details: { |
| 112 | ...(stepResponse.error.details ?? {}), |
| 113 | step: stepResponse.step, |
| 114 | command: step.command, |
| 115 | positionals: step.positionals, |
| 116 | executed: index, |
| 117 | total: steps.length, |
| 118 | partialResults, |
| 119 | }, |
| 120 | }, |
| 121 | }; |
| 122 | } |
| 123 | partialResults.push(stepResponse.result); |
| 124 | } |
| 125 | const data: BatchRunResult = { |
| 126 | total: steps.length, |
| 127 | executed: steps.length, |
| 128 | totalDurationMs: Date.now() - startedAt, |
| 129 | results: partialResults, |
no test coverage detected
searching dependent graphs…