/*
 * 文件定位：Agent 执行引擎 / 基础工具原语层 / 基础工具集合 / Shell 基础工具 / 进程控制。
 * 核心目的：提供 Shell 基础工具 / 进程控制 中的“脱离会话执行”基础能力原语。
 * 能力要求1：需要定义该能力的输入、输出、错误、权限需求和可观测事件。
 * 能力要求2：这些基础工具是 Agent 成立的底层能力，不是 TAP 的最终高级工具库。
 * 能力要求3：后续 TAP 可以基于这些原语构建更强的工具编排、审批、替换和专业能力库。
 * 边界：保留 Agent 基础工具原语，不替代 TAP 的高级工具系统。
 * 对接：需要被 runtime.execEngine 拉起，并和 mainLoop、stateEngine、事件暴露、工具调用策略接通。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
import { approvalRecord, cleanStringList, normalizeDirectory, readRecord, safeMetadata, stringValue, trimmedString, } from "../_shared/processControlJson.js";
export const shellDetachedExecutionDescriptor = {
    toolId: "shell.detachedExecution",
    capability: "shell-detached-execution",
    route: "agent_executionEngine.basic_toolLayer.baseTools.shellBase.processControl",
    defaultDryRun: true,
    tapOwnsApproval: true,
    permissionsRequired: ["shell:execute"],
    unsafeSideEffects: false,
};
function dryRunEnabled(context) {
    return readRecord(context)?.dryRun !== false;
}
function invocationId(context) {
    return trimmedString(readRecord(context)?.invocationId) || "shell.detachedExecution:dry-run";
}
function auditEvent(type, context, workingDirectory, metadata) {
    return {
        type,
        toolId: shellDetachedExecutionDescriptor.toolId,
        invocationId: invocationId(context),
        dryRun: dryRunEnabled(context),
        workingDirectory,
        metadata: {
            ...safeMetadata(readRecord(context)?.auditMetadata),
            ...(metadata ?? {}),
        },
    };
}
function failure(code, message, boundary, context, workingDirectory) {
    return {
        ok: false,
        toolId: shellDetachedExecutionDescriptor.toolId,
        error: { code, message, boundary, publicSafe: true, internalDetailExposed: false },
        audit: [auditEvent("agentCore.basicTool.shell.detachedExecution.rejected", context, workingDirectory, { code })],
        events: ["basicTool.shell.detachedExecution.rejected"],
    };
}
function normalizeOptionalPath(pathValue) {
    return trimmedString(pathValue);
}
function normalizeTarget(target, context) {
    const targetRecord = readRecord(target);
    const workingDirectoryForAudit = stringValue(targetRecord?.workingDirectory);
    const command = trimmedString(targetRecord?.command) ?? "";
    if (command.length === 0) {
        return failure("MISSING_COMMAND", "shell.detachedExecution requires a non-empty command", "input", context, workingDirectoryForAudit);
    }
    if (targetRecord?.shell !== undefined && typeof targetRecord.shell !== "string") {
        return failure("INVALID_SHELL", "shell.detachedExecution shell must be sh, bash, or zsh", "input", context, workingDirectoryForAudit);
    }
    const shell = stringValue(targetRecord?.shell) ?? "sh";
    if (shell !== "sh" && shell !== "bash" && shell !== "zsh") {
        return failure("INVALID_SHELL", "shell.detachedExecution shell must be sh, bash, or zsh", "input", context, workingDirectoryForAudit);
    }
    if (targetRecord?.restartPolicy !== undefined && typeof targetRecord.restartPolicy !== "string") {
        return failure("INVALID_RESTART_POLICY", "shell.detachedExecution restartPolicy must be none or on-failure", "resource", context, workingDirectoryForAudit);
    }
    const restartPolicy = stringValue(targetRecord?.restartPolicy) ?? "none";
    if (restartPolicy !== "none" && restartPolicy !== "on-failure") {
        return failure("INVALID_RESTART_POLICY", "shell.detachedExecution restartPolicy must be none or on-failure", "resource", context, workingDirectoryForAudit);
    }
    if (targetRecord?.workingDirectory !== undefined && trimmedString(targetRecord.workingDirectory) === undefined) {
        return failure("INVALID_CWD", "shell.detachedExecution workingDirectory must be a safe path string", "input", context, workingDirectoryForAudit);
    }
    if (targetRecord?.launchId !== undefined && trimmedString(targetRecord.launchId) === undefined) {
        return failure("INVALID_LAUNCH_ID", "shell.detachedExecution launchId must be a non-empty string when provided", "input", context, workingDirectoryForAudit);
    }
    for (const [fieldName, fieldValue] of [
        ["pidFilePath", targetRecord?.pidFilePath],
        ["stdoutLogPath", targetRecord?.stdoutLogPath],
        ["stderrLogPath", targetRecord?.stderrLogPath],
    ]) {
        if (fieldValue !== undefined && trimmedString(fieldValue) === undefined) {
            return failure("INVALID_OUTPUT_PATH", `shell.detachedExecution ${fieldName} must be a non-empty string when provided`, "input", context, workingDirectoryForAudit);
        }
    }
    const workingDirectory = trimmedString(targetRecord?.workingDirectory);
    return {
        command,
        workingDirectory: workingDirectory === undefined ? undefined : normalizeDirectory(workingDirectory),
        shell,
        launchId: trimmedString(targetRecord?.launchId) || `${invocationId(context)}:detached`,
        pidFilePath: normalizeOptionalPath(targetRecord?.pidFilePath),
        stdoutLogPath: normalizeOptionalPath(targetRecord?.stdoutLogPath),
        stderrLogPath: normalizeOptionalPath(targetRecord?.stderrLogPath),
        restartPolicy,
    };
}
function ensureScope(workingDirectory, context) {
    if (workingDirectory === undefined) {
        return undefined;
    }
    const allowedDirectories = cleanStringList(readRecord(context)?.allowedWorkingDirectories).map(normalizeDirectory);
    if (allowedDirectories.length === 0) {
        return undefined;
    }
    const allowed = allowedDirectories.some((directory) => directory === "/" || workingDirectory === directory || workingDirectory.startsWith(`${directory}/`));
    if (allowed) {
        return undefined;
    }
    return failure("SCOPE_REJECTED", "shell.detachedExecution workingDirectory is outside allowed execution scope", "scope", context, workingDirectory);
}
function ensurePermissions(workingDirectory, context) {
    if (readRecord(context)?.grantedPermissions === undefined) {
        return undefined;
    }
    const granted = cleanStringList(readRecord(context)?.grantedPermissions);
    const missing = shellDetachedExecutionDescriptor.permissionsRequired.filter((permission) => !granted.includes(permission));
    if (missing.length === 0) {
        return undefined;
    }
    return failure("PERMISSION_DENIED", `shell.detachedExecution is missing permissions: ${missing.join(", ")}`, "permission", context, workingDirectory);
}
function ensureDryRunOnly(workingDirectory, context) {
    if (dryRunEnabled(context)) {
        return undefined;
    }
    return failure("REAL_EXECUTION_BLOCKED", "shell.detachedExecution only returns a guarded dry-run detached execution plan in the first implementation", "contract", context, workingDirectory);
}
function ensureApproval(workingDirectory, context) {
    const approval = approvalRecord(context);
    if (approval?.accepted === true) {
        return undefined;
    }
    if (approval?.accepted === false) {
        return failure("APPROVAL_REJECTED", stringValue(approval.reason) ?? "shell.detachedExecution approval was rejected by TAP governance", "approval", context, workingDirectory);
    }
    return failure("APPROVAL_REQUIRED", "shell.detachedExecution requires TAP approval because the process may outlive the agent session", "approval", context, workingDirectory);
}
export function planShellDetachedExecution(request = {}) {
    const target = normalizeTarget(request.target, request.context);
    if ("ok" in target) {
        return target;
    }
    const scopeFailure = ensureScope(target.workingDirectory, request.context);
    if (scopeFailure !== undefined) {
        return scopeFailure;
    }
    const permissionFailure = ensurePermissions(target.workingDirectory, request.context);
    if (permissionFailure !== undefined) {
        return permissionFailure;
    }
    const realExecutionFailure = ensureDryRunOnly(target.workingDirectory, request.context);
    if (realExecutionFailure !== undefined) {
        return realExecutionFailure;
    }
    const approvalFailure = ensureApproval(target.workingDirectory, request.context);
    if (approvalFailure !== undefined) {
        return approvalFailure;
    }
    return {
        ok: true,
        toolId: shellDetachedExecutionDescriptor.toolId,
        output: {
            kind: "agentCore.basicTool.shell.detachedExecution",
            target,
            commandPreview: [target.shell, "-lc", target.command],
            permissionsRequired: shellDetachedExecutionDescriptor.permissionsRequired,
            approvalId: trimmedString(approvalRecord(request.context)?.approvalId),
            detachedContract: {
                outlivesAgentSession: true,
                requiresTapApproval: true,
                runtimeMustRecordHandle: true,
            },
            dryRun: true,
            executionBlocked: true,
            unsafeSideEffects: false,
            resultEnvelope: {
                planned: true,
                detachedHandle: target.launchId,
            },
        },
        audit: [
            auditEvent("agentCore.basicTool.shell.detachedExecution.dryRun", request.context, target.workingDirectory, {
                launchId: target.launchId,
                restartPolicy: target.restartPolicy,
                pidFilePath: target.pidFilePath,
            }),
        ],
        events: ["basicTool.shell.detachedExecution.dryRun"],
    };
}
