/**
 * Spawn provider interface for CLEO provider adapters.
 *
 * @task T5240
 * @task T9214 — orchestrator-defer waiver field (W4 UX hardening)
 * @task T9215 — DelegateTaskEnvelope discriminated-union type (W1 wiring)
 */
/**
 * Parse and validate a `delegate_task` JSON envelope from adapter stdout.
 *
 * Scans a line of stdout for the sentinel `"tool":"delegate_task"` and
 * attempts to parse the full envelope. Returns `null` if the line is not
 * a valid `DelegateTaskEnvelope` — callers should pass through non-matching
 * lines without transformation.
 *
 * Validation checks:
 * - `tool` equals `'delegate_task'`
 * - `args.tasks` is a non-empty array
 * - Each task has a non-empty `taskId`
 * - `args.parent` has a valid `taskId` and `role`
 *
 * @param line - Single line of stdout from the spawned agent process.
 * @returns Parsed envelope or `null` if the line is not a sentinel.
 *
 * @task T9215
 */
export function parseDelegateTaskEnvelope(line) {
    if (!line.includes('"delegate_task"'))
        return null;
    let parsed;
    try {
        // Find the first JSON object on the line
        const start = line.indexOf('{');
        const end = line.lastIndexOf('}');
        if (start === -1 || end === -1 || end <= start)
            return null;
        parsed = JSON.parse(line.slice(start, end + 1));
    }
    catch {
        return null;
    }
    if (typeof parsed !== 'object' || parsed === null)
        return null;
    const obj = parsed;
    if (obj['tool'] !== 'delegate_task')
        return null;
    const args = obj['args'];
    if (typeof args !== 'object' || args === null)
        return null;
    const argsObj = args;
    const tasks = argsObj['tasks'];
    if (!Array.isArray(tasks) || tasks.length === 0)
        return null;
    for (const t of tasks) {
        if (typeof t !== 'object' || t === null)
            return null;
        const task = t;
        if (typeof task['taskId'] !== 'string' || task['taskId'].length === 0)
            return null;
    }
    const parent = argsObj['parent'];
    if (typeof parent !== 'object' || parent === null)
        return null;
    const parentObj = parent;
    if (typeof parentObj['taskId'] !== 'string' || parentObj['taskId'].length === 0)
        return null;
    if (parentObj['role'] !== 'orchestrator' && parentObj['role'] !== 'lead')
        return null;
    return parsed;
}
//# sourceMappingURL=spawn.js.map