#!/usr/bin/env node
// Copyright (c) 2026 Hellmai Ltd
// SPDX-License-Identifier: AGPL-3.0-only
import { createWUParser } from '@lumenflow/core/arg-parser';
import { die } from '@lumenflow/core/error-handler';
import { ExecutionContextSchema, initializeKernelRuntime, TaskSpecSchema, } from '@lumenflow/kernel';
import { runCLI } from './cli-entry-point.js';
const TASK_CLAIM_COMMAND_NAME = 'task-claim';
const TASK_CLAIM_LOG_PREFIX = '[task:claim]';
const TASK_CLAIM_DESCRIPTION = 'Claim a task directly through KernelRuntime';
const TASK_CLAIM_DEFAULT_WORKSPACE_ROOT = '.';
const TASK_CREATE_COMMAND_NAME = 'task-create';
const TASK_CREATE_LOG_PREFIX = '[task:create]';
const TASK_CREATE_DESCRIPTION = 'Create a task directly through KernelRuntime';
const TASK_CREATE_DEFAULT_WORKSPACE_ROOT = '.';
const TASK_COMPLETE_COMMAND_NAME = 'task-complete';
const TASK_COMPLETE_LOG_PREFIX = '[task:complete]';
const TASK_COMPLETE_DESCRIPTION = 'Complete a task directly through KernelRuntime';
const TASK_COMPLETE_DEFAULT_WORKSPACE_ROOT = '.';
const TASK_BLOCK_COMMAND_NAME = 'task-block';
const TASK_BLOCK_LOG_PREFIX = '[task:block]';
const TASK_BLOCK_DESCRIPTION = 'Block a task directly through KernelRuntime';
const TASK_BLOCK_DEFAULT_WORKSPACE_ROOT = '.';
const TASK_UNBLOCK_COMMAND_NAME = 'task-unblock';
const TASK_UNBLOCK_LOG_PREFIX = '[task:unblock]';
const TASK_UNBLOCK_DESCRIPTION = 'Unblock a task directly through KernelRuntime';
const TASK_UNBLOCK_DEFAULT_WORKSPACE_ROOT = '.';
const TASK_INSPECT_COMMAND_NAME = 'task-inspect';
const TASK_INSPECT_LOG_PREFIX = '[task:inspect]';
const TASK_INSPECT_DESCRIPTION = 'Inspect a task directly through KernelRuntime';
const TASK_INSPECT_DEFAULT_WORKSPACE_ROOT = '.';
const TOOL_EXECUTE_COMMAND_NAME = 'tool-execute';
const TOOL_EXECUTE_LOG_PREFIX = '[tool:execute]';
const TOOL_EXECUTE_DESCRIPTION = 'Execute a tool directly through KernelRuntime';
const TOOL_EXECUTE_DEFAULT_WORKSPACE_ROOT = '.';
const TASK_CLAIM_OPTIONS = {
    taskId: {
        name: 'taskId',
        flags: '--task-id <taskId>',
        description: 'Task ID to claim (e.g., WU-1772)',
    },
    by: {
        name: 'by',
        flags: '--by <identity>',
        description: 'Actor identity for the claim (e.g., agent@example.com)',
    },
    sessionId: {
        name: 'sessionId',
        flags: '--session-id <sessionId>',
        description: 'Session ID for runtime event correlation',
    },
    timestamp: {
        name: 'timestamp',
        flags: '--timestamp <iso8601>',
        description: 'Optional ISO-8601 timestamp override',
    },
    domainData: {
        name: 'domainData',
        flags: '--domain-data <json>',
        description: 'Optional JSON object payload for task-scoped metadata',
    },
    workspaceRoot: {
        name: 'workspaceRoot',
        flags: '--workspace-root <path>',
        description: 'Workspace root path (default: current directory)',
        default: TASK_CLAIM_DEFAULT_WORKSPACE_ROOT,
    },
    json: {
        name: 'json',
        flags: '--json',
        description: 'Output claim result as JSON',
        type: 'boolean',
    },
};
const TASK_CREATE_OPTIONS = {
    taskSpec: {
        name: 'taskSpec',
        flags: '--task-spec <json>',
        description: 'Task spec JSON payload for KernelRuntime.createTask',
    },
    workspaceRoot: {
        name: 'workspaceRoot',
        flags: '--workspace-root <path>',
        description: 'Workspace root path (default: current directory)',
        default: TASK_CREATE_DEFAULT_WORKSPACE_ROOT,
    },
    json: {
        name: 'json',
        flags: '--json',
        description: 'Output create result as JSON',
        type: 'boolean',
    },
};
const TASK_COMPLETE_OPTIONS = {
    complete: {
        name: 'complete',
        flags: '--complete',
        description: 'Run task completion flow',
        type: 'boolean',
    },
    taskId: {
        name: 'taskId',
        flags: '--task-id <taskId>',
        description: 'Task ID to complete (e.g., WU-1786)',
    },
    runId: {
        name: 'runId',
        flags: '--run-id <runId>',
        description: 'Optional run ID override',
    },
    timestamp: {
        name: 'timestamp',
        flags: '--timestamp <iso8601>',
        description: 'Optional ISO-8601 timestamp override',
    },
    evidenceRefs: {
        name: 'evidenceRefs',
        flags: '--evidence-refs <json>',
        description: 'Optional JSON array of receipt/evidence references',
    },
    workspaceRoot: {
        name: 'workspaceRoot',
        flags: '--workspace-root <path>',
        description: 'Workspace root path (default: current directory)',
        default: TASK_COMPLETE_DEFAULT_WORKSPACE_ROOT,
    },
    json: {
        name: 'json',
        flags: '--json',
        description: 'Output complete result as JSON',
        type: 'boolean',
    },
};
const TASK_BLOCK_OPTIONS = {
    block: {
        name: 'block',
        flags: '--block',
        description: 'Run task block flow',
        type: 'boolean',
    },
    taskId: {
        name: 'taskId',
        flags: '--task-id <taskId>',
        description: 'Task ID to block (e.g., WU-1787)',
    },
    reason: {
        name: 'reason',
        flags: '--reason <reason>',
        description: 'Reason for blocking the task',
    },
    timestamp: {
        name: 'timestamp',
        flags: '--timestamp <iso8601>',
        description: 'Optional ISO-8601 timestamp override',
    },
    workspaceRoot: {
        name: 'workspaceRoot',
        flags: '--workspace-root <path>',
        description: 'Workspace root path (default: current directory)',
        default: TASK_BLOCK_DEFAULT_WORKSPACE_ROOT,
    },
    json: {
        name: 'json',
        flags: '--json',
        description: 'Output block result as JSON',
        type: 'boolean',
    },
};
const TASK_UNBLOCK_OPTIONS = {
    unblock: {
        name: 'unblock',
        flags: '--unblock',
        description: 'Run task unblock flow',
        type: 'boolean',
    },
    taskId: {
        name: 'taskId',
        flags: '--task-id <taskId>',
        description: 'Task ID to unblock (e.g., WU-1787)',
    },
    timestamp: {
        name: 'timestamp',
        flags: '--timestamp <iso8601>',
        description: 'Optional ISO-8601 timestamp override',
    },
    workspaceRoot: {
        name: 'workspaceRoot',
        flags: '--workspace-root <path>',
        description: 'Workspace root path (default: current directory)',
        default: TASK_UNBLOCK_DEFAULT_WORKSPACE_ROOT,
    },
    json: {
        name: 'json',
        flags: '--json',
        description: 'Output unblock result as JSON',
        type: 'boolean',
    },
};
const TASK_INSPECT_OPTIONS = {
    inspect: {
        name: 'inspect',
        flags: '--inspect',
        description: 'Run task inspect flow',
        type: 'boolean',
    },
    taskId: {
        name: 'taskId',
        flags: '--task-id <taskId>',
        description: 'Task ID to inspect (e.g., WU-1788)',
    },
    workspaceRoot: {
        name: 'workspaceRoot',
        flags: '--workspace-root <path>',
        description: 'Workspace root path (default: current directory)',
        default: TASK_INSPECT_DEFAULT_WORKSPACE_ROOT,
    },
    json: {
        name: 'json',
        flags: '--json',
        description: 'Output inspect result as JSON',
        type: 'boolean',
    },
};
const TOOL_EXECUTE_OPTIONS = {
    toolExecute: {
        name: 'toolExecute',
        flags: '--tool-execute',
        description: 'Run tool execution flow',
        type: 'boolean',
    },
    toolName: {
        name: 'toolName',
        flags: '--tool-name <toolName>',
        description: 'Tool capability name to execute',
    },
    toolInput: {
        name: 'toolInput',
        flags: '--tool-input <json>',
        description: 'Optional JSON input payload for the tool (default: {})',
    },
    taskId: {
        name: 'taskId',
        flags: '--task-id <taskId>',
        description: 'Task ID for execution context',
    },
    runId: {
        name: 'runId',
        flags: '--run-id <runId>',
        description: 'Run ID for execution context',
    },
    sessionId: {
        name: 'sessionId',
        flags: '--session-id <sessionId>',
        description: 'Session ID for execution context',
    },
    allowedScopes: {
        name: 'allowedScopes',
        flags: '--allowed-scopes <json>',
        description: 'Optional JSON array of runtime-allowed tool scopes (default: [])',
    },
    metadata: {
        name: 'metadata',
        flags: '--metadata <json>',
        description: 'Optional JSON object metadata for execution context',
    },
    workspaceRoot: {
        name: 'workspaceRoot',
        flags: '--workspace-root <path>',
        description: 'Workspace root path (default: current directory)',
        default: TOOL_EXECUTE_DEFAULT_WORKSPACE_ROOT,
    },
    json: {
        name: 'json',
        flags: '--json',
        description: 'Output tool execution result as JSON',
        type: 'boolean',
    },
};
export function parseTaskClaimDomainData(raw) {
    if (!raw) {
        return undefined;
    }
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch {
        die(`${TASK_CLAIM_LOG_PREFIX} --domain-data must be valid JSON`);
    }
    if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
        die(`${TASK_CLAIM_LOG_PREFIX} --domain-data must be a JSON object`);
    }
    return parsed;
}
export function parseTaskCreateSpec(raw) {
    if (!raw) {
        die(`${TASK_CREATE_LOG_PREFIX} --task-spec is required`);
    }
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch {
        die(`${TASK_CREATE_LOG_PREFIX} --task-spec must be valid JSON`);
    }
    const validated = TaskSpecSchema.safeParse(parsed);
    if (!validated.success) {
        die(`${TASK_CREATE_LOG_PREFIX} --task-spec is invalid: ${validated.error.message}`);
    }
    return validated.data;
}
export function parseTaskBlockReason(raw) {
    const reason = raw?.trim() ?? '';
    if (reason.length === 0) {
        die(`${TASK_BLOCK_LOG_PREFIX} --reason is required`);
    }
    return reason;
}
export function parseTaskCompleteEvidenceRefs(raw) {
    if (!raw) {
        return undefined;
    }
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch {
        die(`${TASK_COMPLETE_LOG_PREFIX} --evidence-refs must be valid JSON`);
    }
    if (!Array.isArray(parsed) || !parsed.every((item) => typeof item === 'string')) {
        die(`${TASK_COMPLETE_LOG_PREFIX} --evidence-refs must be a JSON array of strings`);
    }
    return parsed;
}
export function parseTaskToolExecuteInput(raw) {
    if (!raw) {
        return {};
    }
    try {
        return JSON.parse(raw);
    }
    catch {
        die(`${TOOL_EXECUTE_LOG_PREFIX} --tool-input must be valid JSON`);
    }
}
export function parseTaskToolExecuteScopes(raw) {
    if (!raw) {
        return [];
    }
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch {
        die(`${TOOL_EXECUTE_LOG_PREFIX} --allowed-scopes must be valid JSON`);
    }
    const validated = ExecutionContextSchema.safeParse({
        run_id: 'run-placeholder',
        task_id: 'task-placeholder',
        session_id: 'session-placeholder',
        allowed_scopes: parsed,
    });
    if (!validated.success) {
        die(`${TOOL_EXECUTE_LOG_PREFIX} --allowed-scopes must be a valid tool scope array: ${validated.error.message}`);
    }
    return validated.data.allowed_scopes;
}
export function parseTaskToolExecuteMetadata(raw) {
    if (!raw) {
        return undefined;
    }
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch {
        die(`${TOOL_EXECUTE_LOG_PREFIX} --metadata must be valid JSON`);
    }
    if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
        die(`${TOOL_EXECUTE_LOG_PREFIX} --metadata must be a JSON object`);
    }
    return parsed;
}
export function parseTaskClaimArgs() {
    const options = Object.values(TASK_CLAIM_OPTIONS);
    const parsed = createWUParser({
        name: TASK_CLAIM_COMMAND_NAME,
        description: TASK_CLAIM_DESCRIPTION,
        options,
        required: ['taskId', 'by', 'sessionId'],
    });
    return {
        input: {
            task_id: parsed.taskId,
            by: parsed.by,
            session_id: parsed.sessionId,
            timestamp: parsed.timestamp,
            domain_data: parseTaskClaimDomainData(parsed.domainData),
        },
        workspaceRoot: parsed.workspaceRoot || process.cwd(),
        json: parsed.json ?? false,
    };
}
export function parseTaskCreateArgs() {
    const options = Object.values(TASK_CREATE_OPTIONS);
    const parsed = createWUParser({
        name: TASK_CREATE_COMMAND_NAME,
        description: TASK_CREATE_DESCRIPTION,
        options,
        required: ['taskSpec'],
    });
    return {
        input: parseTaskCreateSpec(parsed.taskSpec),
        workspaceRoot: parsed.workspaceRoot || process.cwd(),
        json: parsed.json ?? false,
    };
}
export function parseTaskCompleteArgs() {
    const options = Object.values(TASK_COMPLETE_OPTIONS);
    const parsed = createWUParser({
        name: TASK_COMPLETE_COMMAND_NAME,
        description: TASK_COMPLETE_DESCRIPTION,
        options,
        required: ['taskId'],
    });
    return {
        input: {
            task_id: parsed.taskId,
            run_id: parsed.runId,
            timestamp: parsed.timestamp,
            evidence_refs: parseTaskCompleteEvidenceRefs(parsed.evidenceRefs),
        },
        workspaceRoot: parsed.workspaceRoot || process.cwd(),
        json: parsed.json ?? false,
    };
}
export function parseTaskBlockArgs() {
    const options = Object.values(TASK_BLOCK_OPTIONS);
    const parsed = createWUParser({
        name: TASK_BLOCK_COMMAND_NAME,
        description: TASK_BLOCK_DESCRIPTION,
        options,
        required: ['taskId', 'reason'],
    });
    return {
        input: {
            task_id: parsed.taskId,
            reason: parseTaskBlockReason(parsed.reason),
            timestamp: parsed.timestamp,
        },
        workspaceRoot: parsed.workspaceRoot || process.cwd(),
        json: parsed.json ?? false,
    };
}
export function parseTaskUnblockArgs() {
    const options = Object.values(TASK_UNBLOCK_OPTIONS);
    const parsed = createWUParser({
        name: TASK_UNBLOCK_COMMAND_NAME,
        description: TASK_UNBLOCK_DESCRIPTION,
        options,
        required: ['taskId'],
    });
    return {
        input: {
            task_id: parsed.taskId,
            timestamp: parsed.timestamp,
        },
        workspaceRoot: parsed.workspaceRoot || process.cwd(),
        json: parsed.json ?? false,
    };
}
export function parseTaskInspectArgs() {
    const options = Object.values(TASK_INSPECT_OPTIONS);
    const parsed = createWUParser({
        name: TASK_INSPECT_COMMAND_NAME,
        description: TASK_INSPECT_DESCRIPTION,
        options,
        required: ['taskId'],
    });
    return {
        taskId: parsed.taskId,
        workspaceRoot: parsed.workspaceRoot || process.cwd(),
        json: parsed.json ?? false,
    };
}
export function parseTaskToolExecuteArgs() {
    const options = Object.values(TOOL_EXECUTE_OPTIONS);
    const parsed = createWUParser({
        name: TOOL_EXECUTE_COMMAND_NAME,
        description: TOOL_EXECUTE_DESCRIPTION,
        options,
        required: ['toolName', 'taskId', 'runId', 'sessionId'],
    });
    const parsedContext = ExecutionContextSchema.safeParse({
        run_id: parsed.runId,
        task_id: parsed.taskId,
        session_id: parsed.sessionId,
        allowed_scopes: parseTaskToolExecuteScopes(parsed.allowedScopes),
        metadata: parseTaskToolExecuteMetadata(parsed.metadata),
    });
    if (!parsedContext.success) {
        die(`${TOOL_EXECUTE_LOG_PREFIX} execution context is invalid: ${parsedContext.error.message}`);
    }
    return {
        toolName: parsed.toolName,
        toolInput: parseTaskToolExecuteInput(parsed.toolInput),
        context: parsedContext.data,
        workspaceRoot: parsed.workspaceRoot || process.cwd(),
        json: parsed.json ?? false,
    };
}
export async function runTaskClaim(args) {
    const runtime = await initializeKernelRuntime({ workspaceRoot: args.workspaceRoot });
    return runtime.claimTask(args.input);
}
export async function runTaskCreate(args) {
    const runtime = await initializeKernelRuntime({ workspaceRoot: args.workspaceRoot });
    return runtime.createTask(args.input);
}
export async function runTaskComplete(args) {
    const runtime = await initializeKernelRuntime({ workspaceRoot: args.workspaceRoot });
    return runtime.completeTask(args.input);
}
export async function runTaskBlock(args) {
    const runtime = await initializeKernelRuntime({ workspaceRoot: args.workspaceRoot });
    return runtime.blockTask(args.input);
}
export async function runTaskUnblock(args) {
    const runtime = await initializeKernelRuntime({ workspaceRoot: args.workspaceRoot });
    return runtime.unblockTask(args.input);
}
export async function runTaskInspect(args) {
    const runtime = await initializeKernelRuntime({ workspaceRoot: args.workspaceRoot });
    return runtime.inspectTask(args.taskId);
}
export async function runTaskToolExecute(args) {
    const runtime = await initializeKernelRuntime({ workspaceRoot: args.workspaceRoot });
    return runtime.executeTool(args.toolName, args.toolInput, args.context);
}
function formatTaskClaimSummary(result) {
    const eventKinds = result.events.map((event) => event.kind).join(', ');
    return [
        `${TASK_CLAIM_LOG_PREFIX} Claimed task ${result.task_id}`,
        `${TASK_CLAIM_LOG_PREFIX} Run ID: ${result.run.run_id}`,
        `${TASK_CLAIM_LOG_PREFIX} Events: ${eventKinds}`,
    ].join('\n');
}
function formatTaskCreateSummary(result) {
    return [
        `${TASK_CREATE_LOG_PREFIX} Created task ${result.task.id}`,
        `${TASK_CREATE_LOG_PREFIX} Task spec: ${result.task_spec_path}`,
        `${TASK_CREATE_LOG_PREFIX} Event: ${result.event.kind}`,
    ].join('\n');
}
function formatTaskCompleteSummary(result) {
    const eventKinds = result.events.map((event) => event.kind).join(', ');
    return [
        `${TASK_COMPLETE_LOG_PREFIX} Completed task ${result.task_id}`,
        `${TASK_COMPLETE_LOG_PREFIX} Run ID: ${result.run_id}`,
        `${TASK_COMPLETE_LOG_PREFIX} Events: ${eventKinds}`,
    ].join('\n');
}
function formatTaskBlockSummary(result) {
    return [
        `${TASK_BLOCK_LOG_PREFIX} Blocked task ${result.task_id}`,
        `${TASK_BLOCK_LOG_PREFIX} Event: ${result.event.kind}`,
        `${TASK_BLOCK_LOG_PREFIX} Reason: ${result.event.reason}`,
    ].join('\n');
}
function formatTaskUnblockSummary(result) {
    return [
        `${TASK_UNBLOCK_LOG_PREFIX} Unblocked task ${result.task_id}`,
        `${TASK_UNBLOCK_LOG_PREFIX} Event: ${result.event.kind}`,
    ].join('\n');
}
function formatTaskInspectSummary(result) {
    return [
        `${TASK_INSPECT_LOG_PREFIX} Inspected task ${result.task_id}`,
        `${TASK_INSPECT_LOG_PREFIX} State: ${result.state.status}`,
        `${TASK_INSPECT_LOG_PREFIX} Runs: ${result.run_history.length}`,
        `${TASK_INSPECT_LOG_PREFIX} Events: ${result.events.length}`,
    ].join('\n');
}
function formatTaskToolExecuteSummary(toolName, result) {
    const receiptId = result.metadata && typeof result.metadata.receipt_id === 'string'
        ? result.metadata.receipt_id
        : null;
    const status = result.success ? 'success' : 'failure';
    return [
        `${TOOL_EXECUTE_LOG_PREFIX} Executed tool ${toolName}`,
        `${TOOL_EXECUTE_LOG_PREFIX} Status: ${status}`,
        receiptId ? `${TOOL_EXECUTE_LOG_PREFIX} Receipt: ${receiptId}` : null,
    ]
        .filter((line) => line !== null)
        .join('\n');
}
export async function main() {
    if (process.argv.includes('--task-spec')) {
        const args = parseTaskCreateArgs();
        const result = await runTaskCreate(args);
        if (args.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        console.log(formatTaskCreateSummary(result));
        return;
    }
    if (process.argv.includes('--complete')) {
        const args = parseTaskCompleteArgs();
        const result = await runTaskComplete(args);
        if (args.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        console.log(formatTaskCompleteSummary(result));
        return;
    }
    if (process.argv.includes('--block')) {
        const args = parseTaskBlockArgs();
        const result = await runTaskBlock(args);
        if (args.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        console.log(formatTaskBlockSummary(result));
        return;
    }
    if (process.argv.includes('--unblock')) {
        const args = parseTaskUnblockArgs();
        const result = await runTaskUnblock(args);
        if (args.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        console.log(formatTaskUnblockSummary(result));
        return;
    }
    if (process.argv.includes('--inspect')) {
        const args = parseTaskInspectArgs();
        const result = await runTaskInspect(args);
        if (args.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        console.log(formatTaskInspectSummary(result));
        return;
    }
    if (process.argv.includes('--tool-execute')) {
        const args = parseTaskToolExecuteArgs();
        const result = await runTaskToolExecute(args);
        if (args.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        console.log(formatTaskToolExecuteSummary(args.toolName, result));
        return;
    }
    const args = parseTaskClaimArgs();
    const result = await runTaskClaim(args);
    if (args.json) {
        console.log(JSON.stringify(result, null, 2));
        return;
    }
    console.log(formatTaskClaimSummary(result));
}
if (import.meta.main) {
    void runCLI(main);
}
//# sourceMappingURL=task-claim.js.map