/*
 * 文件定位：Agent 执行引擎 / 基础工具原语层 / 基础工具集合 / Shell 基础工具 / Shell 管理。
 * 核心目的：提供 Shell 基础工具 / Shell 管理 中的“管理 Shell 会话”基础能力原语。
 * 能力要求1：需要定义该能力的输入、输出、错误、权限需求和可观测事件。
 * 能力要求2：这些基础工具是 Agent 成立的底层能力，不是 TAP 的最终高级工具库。
 * 能力要求3：后续 TAP 可以基于这些原语构建更强的工具编排、审批、替换和专业能力库。
 * 边界：保留 Agent 基础工具原语，不替代 TAP 的高级工具系统。
 * 对接：需要被 runtime.execEngine 拉起，并和 mainLoop、stateEngine、事件暴露、工具调用策略接通。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const shellSessionManagementDescriptor = {
    toolId: "shell.shellSessionManagement",
    capability: "manage-shell-sessions",
    route: "agent_executionEngine.basic_toolLayer.baseTools.shellBase.shellManagement",
    defaultDryRun: true,
    tapOwnsApproval: true,
    unsafeSideEffects: true,
};
function cleanList(values) {
    if (!Array.isArray(values)) {
        return [];
    }
    return [...new Set(values.map((value) => stringValue(value)?.trim() ?? "").filter(Boolean))];
}
function stringValue(value) {
    return typeof value === "string" ? value : undefined;
}
function dryRunEnabled(context) {
    return context?.dryRun !== false;
}
function invocationId(context) {
    return stringValue(context?.invocationId)?.trim() || "shell.shellSessionManagement:dry-run";
}
function auditEvent(type, context, sessionId, metadata) {
    return {
        type,
        toolId: shellSessionManagementDescriptor.toolId,
        invocationId: invocationId(context),
        dryRun: dryRunEnabled(context),
        sessionId,
        metadata: {
            ...(typeof context?.auditMetadata === "object" && context.auditMetadata !== null ? context.auditMetadata : {}),
            ...(metadata ?? {}),
        },
    };
}
function failure(code, message, boundary, context, sessionId) {
    return {
        ok: false,
        toolId: shellSessionManagementDescriptor.toolId,
        error: { code, message, boundary, publicSafe: true, internalDetailExposed: false },
        audit: [auditEvent("agentCore.basicTool.shell.shellSessionManagement.rejected", context, sessionId, { code })],
        events: ["basicTool.shell.shellSessionManagement.rejected"],
    };
}
function normalizeAction(action, context) {
    if (action === undefined) {
        return "inspect";
    }
    if (action === "inspect" || action === "create" || action === "attach" || action === "detach" || action === "close") {
        return action;
    }
    return failure("INVALID_ACTION", "shell.shellSessionManagement target.action is not supported", "input", context);
}
function normalizeSingleLine(value, errorCode, message, context, boundary = "input") {
    if (value !== undefined && typeof value !== "string") {
        return failure(errorCode, message, boundary, context);
    }
    const normalized = stringValue(value)?.trim();
    if (normalized === undefined || normalized.length === 0) {
        return undefined;
    }
    if (normalized.includes("\0") || /[\r\n]/u.test(normalized)) {
        return failure(errorCode, message, boundary, context, normalized);
    }
    return normalized;
}
function normalizeTarget(target, context) {
    const action = normalizeAction(target?.action, context);
    if (typeof action !== "string") {
        return action;
    }
    const sessionId = normalizeSingleLine(target?.sessionId, "INVALID_SESSION_ID", "shell.shellSessionManagement sessionId must be a safe single-line string", context);
    if (sessionId !== undefined && typeof sessionId !== "string") {
        return sessionId;
    }
    if (action !== "create" && sessionId === undefined) {
        return failure("MISSING_SESSION_ID", `shell.shellSessionManagement action ${action} requires target.sessionId`, "input", context);
    }
    const sessionName = normalizeSingleLine(target?.sessionName, "INVALID_SESSION_NAME", "shell.shellSessionManagement sessionName must be a safe single-line string", context);
    if (sessionName !== undefined && typeof sessionName !== "string") {
        return sessionName;
    }
    const shellType = normalizeSingleLine(target?.shellType, "INVALID_SHELL_TYPE", "shell.shellSessionManagement shellType must be a safe single-line string", context);
    if (shellType !== undefined && typeof shellType !== "string") {
        return shellType;
    }
    const workingDirectory = normalizeSingleLine(target?.workingDirectory, "INVALID_WORKING_DIRECTORY", "shell.shellSessionManagement workingDirectory must be a safe path string", context, "scope");
    if (workingDirectory !== undefined && typeof workingDirectory !== "string") {
        return workingDirectory;
    }
    const reason = normalizeSingleLine(target?.reason, "INVALID_SESSION_NAME", "shell.shellSessionManagement reason must be a safe single-line string", context);
    if (reason !== undefined && typeof reason !== "string") {
        return reason;
    }
    return {
        action,
        sessionId,
        sessionName,
        shellType,
        workingDirectory,
        reason,
    };
}
function requiredPermissions(target) {
    if (target.action === "create") {
        return ["shell:session:create"];
    }
    if (target.action === "attach" || target.action === "detach") {
        return ["shell:session:attach"];
    }
    if (target.action === "close") {
        return ["shell:session:close"];
    }
    return ["shell:session:inspect"];
}
function ensureScope(target, context) {
    const allowedSessionIds = cleanList(context?.allowedSessionIds);
    if (allowedSessionIds.length === 0 || target.sessionId === undefined) {
        return undefined;
    }
    if (allowedSessionIds.includes(target.sessionId)) {
        return undefined;
    }
    return failure("SESSION_SCOPE_DENIED", "shell.shellSessionManagement target session is outside runtime governance scope", "scope", context, target.sessionId);
}
function ensurePermissions(target, context) {
    if (context?.grantedPermissions === undefined) {
        return undefined;
    }
    const grantedPermissions = cleanList(context?.grantedPermissions);
    const missing = requiredPermissions(target).filter((permission) => !grantedPermissions.includes(permission));
    if (missing.length === 0) {
        return undefined;
    }
    return failure("PERMISSION_DENIED", `shell.shellSessionManagement is missing permissions: ${missing.join(", ")}`, "permission", context, target.sessionId);
}
function ensureGovernance(context) {
    if (context?.guard?.accepted !== false) {
        return undefined;
    }
    return failure("GOVERNANCE_REJECTED", stringValue(context.guard.reason) ?? "shell.shellSessionManagement was rejected by runtime governance", "governance", context);
}
function ensureDryRunOnly(target, context) {
    if (dryRunEnabled(context)) {
        return undefined;
    }
    return failure("REAL_EXECUTION_BLOCKED", "shell.shellSessionManagement only returns a guarded dry-run session envelope in the first implementation", "contract", context, target.sessionId);
}
export function planShellSessionManagement(request = {}) {
    const target = normalizeTarget(request.target, request.context);
    if ("ok" in target) {
        return target;
    }
    const governanceFailure = ensureGovernance(request.context);
    if (governanceFailure !== undefined) {
        return governanceFailure;
    }
    const scopeFailure = ensureScope(target, request.context);
    if (scopeFailure !== undefined) {
        return scopeFailure;
    }
    const permissionFailure = ensurePermissions(target, request.context);
    if (permissionFailure !== undefined) {
        return permissionFailure;
    }
    const realExecutionFailure = ensureDryRunOnly(target, request.context);
    if (realExecutionFailure !== undefined) {
        return realExecutionFailure;
    }
    return {
        ok: true,
        toolId: shellSessionManagementDescriptor.toolId,
        output: {
            kind: "agentCore.basicTool.shell.shellSessionManagement",
            target,
            permissionsRequired: requiredPermissions(target),
            dryRun: true,
            executionBlocked: true,
            unsafeSideEffects: target.action !== "inspect",
            sessionEnvelope: {
                operation: target.action,
                wouldCreateSession: target.action === "create",
                wouldAttachSession: target.action === "attach",
                wouldCloseSession: target.action === "close",
                runtimeSessionState: "unchanged",
            },
        },
        audit: [
            auditEvent("agentCore.basicTool.shell.shellSessionManagement.dryRun", request.context, target.sessionId, {
                action: target.action,
                shellType: target.shellType,
            }),
        ],
        events: ["basicTool.shell.shellSessionManagement.dryRun"],
    };
}
