/*
 * git.manageSubmodule storage core.
 * Owns the fixed git submodule contract and delegates host execution to BaseToolExecutorPort.git.runGit.
 */
export const gitManageSubmoduleDescriptor = {
    toolId: "git.manageSubmodule",
    toolKind: "git.manageSubmodule",
    capability: "manage-git-submodule",
    route: "agent_executionEngine.basic_toolLayer.baseTools.gitBase.advanced",
    defaultDryRun: true,
    defaultDispatch: "dry-run",
    tapOwnsApproval: true,
    requiresTapApproval: true,
    runtimeEntryPort: "BaseToolExecutorPort.git.runGit",
    operationRisk: "submodule-management",
    permissionsRequired: ["git:read", "filesystem:read"],
    defaultTimeoutMs: 120_000,
    maxTimeoutMs: 900_000,
};
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function stringValue(value) {
    return typeof value === "string" ? value : undefined;
}
function booleanValue(value) {
    return typeof value === "boolean" ? value : undefined;
}
function stringArrayValue(value) {
    if (value === undefined)
        return undefined;
    if (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
        return undefined;
    return value;
}
function cleanList(values) {
    if (!Array.isArray(values))
        return [];
    return [...new Set(values.map((value) => value.trim()).filter(Boolean))];
}
function dryRunEnabled(context) {
    return context?.dryRun !== false;
}
function invocationId(context) {
    return stringValue(context?.invocationId)?.trim() || "git.manageSubmodule:dry-run";
}
function runtimeId(context) {
    return stringValue(context?.runtimeId)?.trim() || "runtime.unspecified";
}
function auditEvent(type, context, repositoryPath, metadata) {
    return {
        type,
        toolId: gitManageSubmoduleDescriptor.toolId,
        invocationId: invocationId(context),
        dryRun: dryRunEnabled(context),
        repositoryPath,
        metadata: { ...(context?.auditMetadata ?? {}), ...(metadata ?? {}) },
    };
}
function failure(code, message, boundary, context, repositoryPath) {
    return {
        ok: false,
        toolId: gitManageSubmoduleDescriptor.toolId,
        error: {
            code,
            message,
            boundary,
            publicSafe: true,
            safeForRuntimeInspection: true,
            internalDetailExposed: false,
        },
        audit: [auditEvent("agentCore.basicTool.git.manageSubmodule.rejected", context, repositoryPath, { code })],
        events: ["basicTool.git.manageSubmodule.rejected"],
    };
}
function normalizeContext(rawContext, legacyRequest) {
    const contextRecord = rawContext === undefined ? {} : rawContext;
    if (!isRecord(contextRecord))
        return failure("INVALID_CONTEXT", "git.manageSubmodule context must be an object", "input", undefined);
    const guard = contextRecord.guard;
    if (guard !== undefined && !isRecord(guard)) {
        return failure("INVALID_CONTEXT", "git.manageSubmodule context.guard must be an object", "input", undefined);
    }
    const allowedRepositoryRoots = stringArrayValue(contextRecord.allowedRepositoryRoots);
    if (contextRecord.allowedRepositoryRoots !== undefined && allowedRepositoryRoots === undefined) {
        return failure("INVALID_CONTEXT", "git.manageSubmodule context.allowedRepositoryRoots must be a string array", "input", undefined);
    }
    const grantedPermissions = stringArrayValue(contextRecord.grantedPermissions);
    if (contextRecord.grantedPermissions !== undefined && grantedPermissions === undefined) {
        return failure("INVALID_CONTEXT", "git.manageSubmodule context.grantedPermissions must be a string array", "input", undefined);
    }
    const auditMetadata = isRecord(contextRecord.auditMetadata) ? contextRecord.auditMetadata : undefined;
    if (contextRecord.auditMetadata !== undefined && auditMetadata === undefined) {
        return failure("INVALID_CONTEXT", "git.manageSubmodule context.auditMetadata must be an object", "input", undefined);
    }
    return {
        runtimeId: stringValue(contextRecord.runtimeId) ?? stringValue(legacyRequest.runtimeId),
        sessionId: stringValue(contextRecord.sessionId),
        invocationId: stringValue(contextRecord.invocationId) ?? stringValue(legacyRequest.invocationId),
        dryRun: booleanValue(contextRecord.dryRun) ?? booleanValue(legacyRequest.dryRun),
        guard: guard === undefined
            ? undefined
            : { allowed: booleanValue(guard.allowed), accepted: booleanValue(guard.accepted), reason: stringValue(guard.reason) },
        allowedRepositoryRoots,
        grantedPermissions: grantedPermissions,
        auditMetadata,
    };
}
function normalizeRepositoryPath(value, context) {
    const normalized = stringValue(value)?.trim() ?? "";
    if (normalized.length === 0)
        return failure("MISSING_REPOSITORY_PATH", "git.manageSubmodule requires target.repositoryPath", "input", context);
    if (normalized.includes("\0"))
        return failure("INVALID_ARGUMENT", "git.manageSubmodule repositoryPath cannot contain NUL bytes", "input", context, normalized);
    return normalized;
}
function normalizeSubmodulePath(value, context, repositoryPath, required) {
    const raw = stringValue(value)?.trim() ?? "";
    if (raw.length === 0) {
        return required ? failure("MISSING_SUBMODULE_PATH", "git.manageSubmodule requires target.submodulePath", "input", context, repositoryPath) : undefined;
    }
    const normalized = raw.replaceAll("\\", "/").replace(/\/+$/u, "");
    if (normalized.includes("\0") || normalized.startsWith("/") || normalized === ".." || normalized.startsWith("../") || normalized.includes("/../")) {
        return failure("INVALID_ARGUMENT", "git.manageSubmodule target.submodulePath must be repository-relative", "input", context, repositoryPath);
    }
    return normalized;
}
function safeRefLike(value, field, context, repositoryPath) {
    if (value === undefined)
        return undefined;
    const normalized = stringValue(value)?.trim() ?? "";
    if (normalized.length === 0)
        return undefined;
    if (normalized.includes("\0") || /\s/u.test(normalized) || normalized.startsWith("-")) {
        return failure("INVALID_ARGUMENT", `git.manageSubmodule ${field} must be a safe ref-like value`, "input", context, repositoryPath);
    }
    return normalized;
}
function safeRemoteUrl(value, context, repositoryPath) {
    if (value === undefined)
        return undefined;
    const normalized = stringValue(value)?.trim() ?? "";
    if (normalized.length === 0)
        return undefined;
    if (normalized.includes("\0") || /\s/u.test(normalized) || normalized.startsWith("-")) {
        return failure("INVALID_ARGUMENT", "git.manageSubmodule target.remoteUrl must be a safe URL or repository path", "input", context, repositoryPath);
    }
    return normalized;
}
function normalizeAction(value, context, repositoryPath) {
    if (value === undefined || value === "status")
        return "status";
    if (value === "add" || value === "update" || value === "sync" || value === "deinit")
        return value;
    return failure("INVALID_ACTION", "git.manageSubmodule target.action must be status, add, update, sync, or deinit", "input", context, repositoryPath);
}
function normalizeTimeout(value, context, repositoryPath) {
    if (value === undefined)
        return undefined;
    if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > gitManageSubmoduleDescriptor.maxTimeoutMs) {
        return failure("INVALID_TIMEOUT", `git.manageSubmodule timeoutMs must be an integer from 1 to ${gitManageSubmoduleDescriptor.maxTimeoutMs}`, "input", context, repositoryPath);
    }
    return value;
}
function normalizeRequest(request) {
    if (request !== undefined && !isRecord(request)) {
        return failure("INVALID_ARGUMENT", "git.manageSubmodule request must be an object", "input", undefined);
    }
    const requestRecord = isRecord(request) ? request : {};
    const context = normalizeContext(requestRecord.context, requestRecord);
    if ("ok" in context)
        return context;
    const targetRecord = isRecord(requestRecord.target) ? requestRecord.target : requestRecord;
    if (requestRecord.target !== undefined && !isRecord(requestRecord.target)) {
        return failure("INVALID_ARGUMENT", "git.manageSubmodule target must be an object", "input", context);
    }
    const repositoryPath = normalizeRepositoryPath(targetRecord.repositoryPath ?? requestRecord.repositoryPath ?? requestRecord.cwd, context);
    if (typeof repositoryPath !== "string")
        return repositoryPath;
    const action = normalizeAction(targetRecord.action ?? requestRecord.action, context, repositoryPath);
    if (typeof action !== "string")
        return action;
    const submodulePath = normalizeSubmodulePath(targetRecord.submodulePath ?? targetRecord.path ?? requestRecord.submodulePath ?? requestRecord.path, context, repositoryPath, action === "add" || action === "deinit");
    if (submodulePath !== undefined && typeof submodulePath !== "string")
        return submodulePath;
    const remoteUrl = safeRemoteUrl(targetRecord.remoteUrl ?? targetRecord.url ?? requestRecord.remoteUrl ?? requestRecord.url, context, repositoryPath);
    if (remoteUrl !== undefined && typeof remoteUrl !== "string")
        return remoteUrl;
    if (action === "add" && remoteUrl === undefined) {
        return failure("MISSING_REMOTE_URL", "git.manageSubmodule add requires target.remoteUrl", "input", context, repositoryPath);
    }
    const branch = safeRefLike(targetRecord.branch ?? requestRecord.branch, "target.branch", context, repositoryPath);
    if (branch !== undefined && typeof branch !== "string")
        return branch;
    const recursiveValue = targetRecord.recursive ?? requestRecord.recursive;
    const recursive = recursiveValue === undefined ? true : booleanValue(recursiveValue);
    if (typeof recursive !== "boolean") {
        return failure("INVALID_ARGUMENT", "git.manageSubmodule target.recursive must be a boolean", "input", context, repositoryPath);
    }
    const timeoutMs = normalizeTimeout(requestRecord.timeoutMs, context, repositoryPath);
    if (timeoutMs !== undefined && typeof timeoutMs !== "number")
        return timeoutMs;
    return {
        target: { repositoryPath, action, submodulePath, remoteUrl, branch, recursive },
        context,
        timeoutMs,
    };
}
function normalizeAllowedRoot(root) {
    const trimmed = root.trim();
    return trimmed === "/" ? trimmed : trimmed.replace(/\/+$/u, "");
}
function ensureScope(repositoryPath, context) {
    const allowedRoots = cleanList(context?.allowedRepositoryRoots).map(normalizeAllowedRoot);
    if (allowedRoots.length === 0)
        return undefined;
    const allowed = allowedRoots.some((root) => repositoryPath === root || repositoryPath.startsWith(`${root}/`));
    return allowed
        ? undefined
        : failure("SCOPE_REJECTED", "git.manageSubmodule target repository is outside the allowed repository roots", "scope", context, repositoryPath);
}
function mayUseNetwork(target) {
    return target.action === "add" || target.action === "update";
}
function mutates(target) {
    return target.action !== "status";
}
function permissionsForTarget(target) {
    if (!mutates(target))
        return ["git:read", "filesystem:read"];
    return [
        "git:read",
        "git:write",
        "filesystem:read",
        "filesystem:write",
        ...(mayUseNetwork(target) ? ["network:egress"] : []),
    ];
}
function ensurePermissions(target, context) {
    const granted = cleanList(context?.grantedPermissions);
    if (granted.length === 0)
        return undefined;
    const missing = permissionsForTarget(target).filter((permission) => !granted.includes(permission));
    return missing.length === 0
        ? undefined
        : failure("PERMISSION_DENIED", `git.manageSubmodule is missing permissions: ${missing.join(", ")}`, "permission", context, target.repositoryPath);
}
function ensureGovernance(target, context) {
    if (dryRunEnabled(context))
        return undefined;
    if (!mutates(target))
        return undefined;
    if (context.guard?.allowed === true || context.guard?.accepted === true)
        return undefined;
    return failure("GOVERNANCE_REJECTED", context.guard?.reason ?? "git.manageSubmodule requires an affirmative runtime guard for submodule mutations", "governance", context, target.repositoryPath);
}
function providerArgs(target) {
    if (target.action === "status") {
        return ["submodule", "status", ...(target.recursive ? ["--recursive"] : []), ...(target.submodulePath === undefined ? [] : ["--", target.submodulePath])];
    }
    if (target.action === "add") {
        return [
            "submodule",
            "add",
            ...(target.branch === undefined ? [] : ["-b", target.branch]),
            target.remoteUrl ?? "",
            target.submodulePath ?? "",
        ];
    }
    if (target.action === "update") {
        return ["submodule", "update", "--init", ...(target.recursive ? ["--recursive"] : []), ...(target.submodulePath === undefined ? [] : ["--", target.submodulePath])];
    }
    if (target.action === "sync") {
        return ["submodule", "sync", ...(target.recursive ? ["--recursive"] : []), ...(target.submodulePath === undefined ? [] : ["--", target.submodulePath])];
    }
    return ["submodule", "deinit", "--", target.submodulePath ?? ""];
}
function commandPreview(target) {
    return ["git", "-C", target.repositoryPath, ...providerArgs(target)];
}
const runtimeEntry = {
    owner: "runtime",
    port: "BaseToolExecutorPort.git.runGit",
    method: "runGit",
    binary: "git",
    argvMode: "fixed-manage-submodule",
    allowedSubcommand: "submodule",
};
function riskForTarget(target) {
    const changes = mutates(target);
    const network = mayUseNetwork(target);
    const destructive = target.action === "deinit";
    return {
        category: !changes ? "read-only-inspection" : destructive ? "destructive" : network ? "remote-network" : "workspace-mutation",
        riskLevel: !changes ? "normal" : destructive ? "destructive" : "risky",
        mutatesRepository: changes,
        mutatesWorkingTree: changes,
        mutatesFilesystem: changes,
        mutatesGitMetadata: changes,
        mayUseNetwork: network,
        spawnsProcess: true,
        requiresTapApproval: changes,
        runtimeOwnsExecution: true,
    };
}
function plan(normalized, dispatch, dryRun) {
    return {
        toolId: "git.manageSubmodule",
        toolKind: "git.manageSubmodule",
        capability: "manage-git-submodule",
        runtimeId: runtimeId(normalized.context),
        invocationId: invocationId(normalized.context),
        repositoryPath: normalized.target.repositoryPath,
        action: normalized.target.action,
        gitArgs: providerArgs(normalized.target),
        commandPreview: commandPreview(normalized.target),
        requiredPermissions: permissionsForTarget(normalized.target),
        runtimeEntry,
        risk: riskForTarget(normalized.target),
        dispatch,
        dryRun,
        wouldModifyGitMetadata: mutates(normalized.target),
        wouldUseNetwork: mayUseNetwork(normalized.target),
        unsafeSideEffects: mutates(normalized.target),
        outputEnvelope: { stdoutPreview: "", stderrPreview: "", parsed: false },
        audit: {
            guard: "git-submodule-runtime-guard",
            event: "basicTool.git.manageSubmodule.planned",
            governanceRequired: mutates(normalized.target),
            tapCanWrap: true,
            metadata: normalized.context.auditMetadata ?? {},
        },
    };
}
function lineCount(text) {
    if (text.length === 0)
        return 0;
    return text.split(/\r?\n/u).filter((line) => line.length > 0).length;
}
function firstOutputLine(stdout, stderr) {
    return `${stdout}\n${stderr}`.split(/\r?\n/u).map((line) => line.trim()).find(Boolean);
}
function parseStatusLine(line) {
    const raw = line.trimEnd();
    if (raw.trim().length === 0)
        return undefined;
    const statusChar = raw[0];
    const rest = raw.slice(1).trim();
    const match = rest.match(/^(\S+)\s+(\S+)(?:\s+\(([^)]+)\))?/u);
    if (match === null)
        return { raw, status: "unknown" };
    return {
        raw,
        status: statusChar === " " ? "initialized" : statusChar === "-" ? "uninitialized" : statusChar === "+" ? "modified" : "unknown",
        commit: match[1],
        path: match[2],
        ref: match[3],
    };
}
export function parseGitManageSubmoduleResult(providerResult, target) {
    const entries = target.action === "status"
        ? (providerResult?.stdout ?? "")
            .split(/\r?\n/u)
            .map(parseStatusLine)
            .filter((entry) => entry !== undefined)
        : [];
    return {
        parser: "git-submodule-output-v1",
        action: target.action,
        submodulePath: target.submodulePath,
        remoteUrl: target.remoteUrl,
        branch: target.branch,
        recursive: target.recursive,
        exitCode: providerResult?.exitCode,
        stdoutLineCount: lineCount(providerResult?.stdout ?? ""),
        stderrLineCount: lineCount(providerResult?.stderr ?? ""),
        operationHint: providerResult === undefined ? undefined : firstOutputLine(providerResult.stdout, providerResult.stderr),
        entries,
        submoduleChanged: providerResult?.exitCode === 0 && mutates(target),
    };
}
function success(normalized, dryRun, providerResult) {
    const executionPlan = plan(normalized, dryRun ? "dry-run" : "runtime-git-executor", dryRun);
    return {
        ok: true,
        toolId: "git.manageSubmodule",
        plan: executionPlan,
        output: {
            kind: "agentCore.basicTool.git.manageSubmodule",
            target: normalized.target,
            runtimeEntry,
            risk: riskForTarget(normalized.target),
            gitArgs: executionPlan.gitArgs,
            commandPreview: executionPlan.commandPreview,
            timeoutMs: normalized.timeoutMs ?? gitManageSubmoduleDescriptor.defaultTimeoutMs,
            dryRun,
            executionBlocked: dryRun,
            providerCalled: providerResult !== undefined,
            exitCode: providerResult?.exitCode,
            stdout: providerResult?.stdout,
            stderr: providerResult?.stderr,
            permissionsRequired: permissionsForTarget(normalized.target),
            unsafeSideEffects: mutates(normalized.target),
            mayUseNetwork: mayUseNetwork(normalized.target),
            resultEnvelope: parseGitManageSubmoduleResult(providerResult, normalized.target),
        },
        audit: [
            auditEvent(dryRun ? "agentCore.basicTool.git.manageSubmodule.dryRun" : "agentCore.basicTool.git.manageSubmodule.executed", normalized.context, normalized.target.repositoryPath, { action: normalized.target.action, submodulePath: normalized.target.submodulePath, exitCode: providerResult?.exitCode }),
        ],
        events: [dryRun ? "basicTool.git.manageSubmodule.dryRun" : "basicTool.git.manageSubmodule.executed"],
    };
}
export function planManageSubmodule(request = {}) {
    const normalized = normalizeRequest(request);
    if ("ok" in normalized)
        return normalized;
    const scopeFailure = ensureScope(normalized.target.repositoryPath, normalized.context);
    if (scopeFailure !== undefined)
        return scopeFailure;
    const permissionFailure = ensurePermissions(normalized.target, normalized.context);
    if (permissionFailure !== undefined)
        return permissionFailure;
    return success(normalized, true);
}
export const planGitSubmoduleManagement = planManageSubmodule;
export const planGitManageSubmodule = planManageSubmodule;
export async function executeGitManageSubmodule(request = {}) {
    const normalized = normalizeRequest(request);
    if ("ok" in normalized)
        return normalized;
    const scopeFailure = ensureScope(normalized.target.repositoryPath, normalized.context);
    if (scopeFailure !== undefined)
        return scopeFailure;
    const permissionFailure = ensurePermissions(normalized.target, normalized.context);
    if (permissionFailure !== undefined)
        return permissionFailure;
    const governanceFailure = ensureGovernance(normalized.target, normalized.context);
    if (governanceFailure !== undefined)
        return governanceFailure;
    if (dryRunEnabled(normalized.context))
        return success(normalized, true);
    if (request.provider === undefined) {
        return failure("PROVIDER_UNAVAILABLE", "git.manageSubmodule requires runtime.execEngine.git.runGit for real execution", "provider", normalized.context, normalized.target.repositoryPath);
    }
    try {
        const providerResult = await request.provider({ repositoryPath: normalized.target.repositoryPath, args: providerArgs(normalized.target), timeoutMs: normalized.timeoutMs }, normalized.context);
        return success(normalized, false, providerResult);
    }
    catch {
        return failure("PROVIDER_REJECTED", "git.manageSubmodule provider rejected the request or failed safely", "provider", normalized.context, normalized.target.repositoryPath);
    }
}
