/*
 * git.manageTag storage core.
 * Owns the fixed git-tag contract and delegates host execution to BaseToolExecutorPort.git.runGit.
 */
export const gitManageTagDescriptor = {
    toolId: "git.manageTag",
    toolKind: "git.manageTag",
    capability: "manage-tag",
    route: "agent_executionEngine.basic_toolLayer.baseTools.gitBase.branch",
    defaultDryRun: true,
    defaultDispatch: "dry-run",
    tapOwnsApproval: true,
    requiresTapApproval: true,
    runtimeEntryPort: "BaseToolExecutorPort.git.runGit",
    operationRisk: "history-mutation",
    permissionsRequired: ["git:read", "git:write", "filesystem:read", "filesystem:write"],
    defaultTimeoutMs: 30_000,
    maxTimeoutMs: 600_000,
    unsafeSideEffects: true,
};
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.manageTag:dry-run";
}
function runtimeId(context) {
    return stringValue(context?.runtimeId)?.trim() || "runtime.unspecified";
}
function auditEvent(type, context, repositoryPath, metadata) {
    return {
        type,
        toolId: gitManageTagDescriptor.toolId,
        invocationId: invocationId(context),
        dryRun: dryRunEnabled(context),
        repositoryPath,
        metadata: {
            ...(context?.auditMetadata ?? {}),
            ...(metadata ?? {}),
        },
    };
}
function failure(code, message, boundary, context, repositoryPath) {
    return {
        ok: false,
        toolId: gitManageTagDescriptor.toolId,
        error: {
            code,
            message,
            boundary,
            publicSafe: true,
            safeForRuntimeInspection: true,
            internalDetailExposed: false,
        },
        audit: [auditEvent("agentCore.basicTool.git.manageTag.rejected", context, repositoryPath, { code })],
        events: ["basicTool.git.manageTag.rejected"],
    };
}
function normalizeContext(rawContext, legacyRequest) {
    const contextRecord = rawContext === undefined ? {} : rawContext;
    if (!isRecord(contextRecord)) {
        return failure("INVALID_CONTEXT", "git.manageTag context must be an object", "input", undefined);
    }
    const guard = contextRecord.guard;
    if (guard !== undefined && !isRecord(guard)) {
        return failure("INVALID_CONTEXT", "git.manageTag context.guard must be an object", "input", undefined);
    }
    const allowedRepositoryRoots = stringArrayValue(contextRecord.allowedRepositoryRoots);
    if (contextRecord.allowedRepositoryRoots !== undefined && allowedRepositoryRoots === undefined) {
        return failure("INVALID_CONTEXT", "git.manageTag 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.manageTag 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.manageTag 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.manageTag requires target.repositoryPath", "input", context);
    }
    if (normalized.includes("\0")) {
        return failure("INVALID_ARGUMENT", "git.manageTag repositoryPath cannot contain NUL bytes", "input", context, normalized);
    }
    return normalized;
}
function normalizeAction(value) {
    if (value === undefined)
        return "list";
    if (value === "list" || value === "create" || value === "delete" || value === "annotate")
        return value;
    return failure("INVALID_ACTION", "git.manageTag target.action must be list, create, annotate, or delete", "input", undefined);
}
function isUnsafeRef(value) {
    return (value.length === 0 ||
        value.includes("\0") ||
        /\s/u.test(value) ||
        value.startsWith("-") ||
        value.startsWith("/") ||
        value.endsWith("/") ||
        value.includes("..") ||
        value.includes("@{") ||
        value.includes("\\") ||
        value.includes("//") ||
        value.endsWith(".lock") ||
        value.includes(":"));
}
function normalizeRequiredTagName(value, action, context, repositoryPath) {
    if (action === "list")
        return undefined;
    const normalized = stringValue(value)?.trim() ?? "";
    if (normalized.length === 0) {
        return failure("MISSING_TAG_NAME", `git.manageTag action ${action} requires target.tagName`, "input", context, repositoryPath);
    }
    if (isUnsafeRef(normalized)) {
        return failure("UNSAFE_REF", "git.manageTag target.tagName must be a safe ref", "input", context, repositoryPath);
    }
    return normalized;
}
function normalizeOptionalTargetRef(value, context, repositoryPath) {
    if (value === undefined)
        return undefined;
    const normalized = stringValue(value)?.trim() ?? "";
    if (normalized.length === 0)
        return undefined;
    if (isUnsafeRef(normalized)) {
        return failure("UNSAFE_REF", "git.manageTag target.targetRef must be a safe ref", "input", context, repositoryPath);
    }
    return normalized;
}
function normalizeMessage(value, action, context, repositoryPath) {
    if (value === undefined) {
        return action === "annotate"
            ? failure("MISSING_MESSAGE", "git.manageTag action annotate requires target.message", "input", context, repositoryPath)
            : undefined;
    }
    const normalized = stringValue(value)?.trim() ?? "";
    if (normalized.length === 0) {
        return action === "annotate"
            ? failure("MISSING_MESSAGE", "git.manageTag action annotate requires target.message", "input", context, repositoryPath)
            : undefined;
    }
    if (normalized.includes("\0")) {
        return failure("INVALID_ARGUMENT", "git.manageTag target.message cannot contain NUL bytes", "input", context, repositoryPath);
    }
    return normalized;
}
function normalizeTimeout(value, context, repositoryPath) {
    if (value === undefined)
        return undefined;
    if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > gitManageTagDescriptor.maxTimeoutMs) {
        return failure("INVALID_TIMEOUT", `git.manageTag timeoutMs must be an integer from 1 to ${gitManageTagDescriptor.maxTimeoutMs}`, "input", context, repositoryPath);
    }
    return value;
}
function normalizeRequest(request) {
    if (request !== undefined && !isRecord(request)) {
        return failure("INVALID_ARGUMENT", "git.manageTag 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.manageTag target must be an object", "input", context);
    }
    const repositoryPath = normalizeRepositoryPath(targetRecord.repositoryPath, context);
    if (typeof repositoryPath !== "string")
        return repositoryPath;
    const action = normalizeAction(targetRecord.action);
    if (typeof action !== "string")
        return action;
    const tagName = normalizeRequiredTagName(targetRecord.tagName ?? targetRecord.tag ?? targetRecord.name, action, context, repositoryPath);
    if (tagName !== undefined && typeof tagName !== "string")
        return tagName;
    const targetRef = normalizeOptionalTargetRef(targetRecord.targetRef ?? targetRecord.ref ?? targetRecord.revision, context, repositoryPath);
    if (targetRef !== undefined && typeof targetRef !== "string")
        return targetRef;
    const message = normalizeMessage(targetRecord.message, action, context, repositoryPath);
    if (message !== undefined && typeof message !== "string")
        return message;
    const timeoutMs = normalizeTimeout(requestRecord.timeoutMs, context, repositoryPath);
    if (timeoutMs !== undefined && typeof timeoutMs !== "number")
        return timeoutMs;
    return {
        target: {
            repositoryPath,
            action,
            tagName,
            targetRef,
            message,
            force: targetRecord.force === true,
        },
        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.manageTag target repository is outside the allowed repository roots", "scope", context, repositoryPath);
}
function permissionsForTarget(target) {
    return target.action === "list"
        ? ["git:read", "filesystem:read"]
        : ["git:read", "git:write", "filesystem:read", "filesystem:write"];
}
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.manageTag is missing permissions: ${missing.join(", ")}`, "permission", context, target.repositoryPath);
}
function ensureGovernance(target, context) {
    if (dryRunEnabled(context))
        return undefined;
    if (target.action === "list")
        return undefined;
    if (context.guard?.allowed === true || context.guard?.accepted === true)
        return undefined;
    return failure("GOVERNANCE_REJECTED", context.guard?.reason ?? "git.manageTag requires an affirmative runtime guard for real tag mutation", "governance", context, target.repositoryPath);
}
function providerArgs(target) {
    if (target.action === "list") {
        return ["tag", "--list"];
    }
    if (target.action === "create") {
        return ["tag", ...(target.force ? ["--force"] : []), target.tagName ?? "", target.targetRef ?? "HEAD"];
    }
    if (target.action === "delete") {
        return ["tag", "-d", target.tagName ?? ""];
    }
    return [
        "tag",
        "-a",
        ...(target.force ? ["--force"] : []),
        target.tagName ?? "",
        target.targetRef ?? "HEAD",
        "-m",
        target.message ?? "",
    ];
}
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-tag",
    allowedSubcommand: "tag",
};
function riskForTarget(target) {
    const category = target.action === "list" ? "read-only-inspection" : target.action === "delete" ? "destructive" : "history-mutation";
    return {
        category,
        riskLevel: target.action === "list" ? "normal" : "risky",
        mutatesRepository: target.action !== "list",
        mutatesWorkingTree: false,
        mutatesIndex: false,
        createsTag: target.action === "create" || target.action === "annotate",
        deletesTag: target.action === "delete",
        spawnsProcess: true,
        requiresTapApproval: target.action !== "list",
        runtimeOwnsExecution: true,
    };
}
function plan(normalized, dispatch, dryRun) {
    const risk = riskForTarget(normalized.target);
    return {
        toolId: "git.manageTag",
        toolKind: "git.manageTag",
        capability: "manage-tag",
        runtimeId: runtimeId(normalized.context),
        invocationId: invocationId(normalized.context),
        repositoryPath: normalized.target.repositoryPath,
        action: normalized.target.action,
        tagName: normalized.target.tagName,
        targetRef: normalized.target.targetRef,
        force: normalized.target.force,
        gitArgs: providerArgs(normalized.target),
        commandPreview: commandPreview(normalized.target),
        requiredPermissions: permissionsForTarget(normalized.target),
        runtimeEntry,
        risk,
        dispatch,
        dryRun,
        wouldMutateRepository: normalized.target.action !== "list",
        unsafeSideEffects: normalized.target.action !== "list",
        outputEnvelope: { stdoutPreview: "", stderrPreview: "", parsed: false },
        audit: {
            guard: "git-tag-runtime-guard",
            event: "basicTool.git.manageTag.planned",
            governanceRequired: normalized.target.action !== "list",
            tapCanWrap: true,
            metadata: normalized.context.auditMetadata ?? {},
        },
    };
}
function lineCount(text) {
    if (text.length === 0)
        return 0;
    return text.split(/\r?\n/u).length;
}
function firstOutputLine(stdout, stderr) {
    return `${stdout}\n${stderr}`
        .split(/\r?\n/u)
        .map((line) => line.trim())
        .find(Boolean);
}
function parseListedTags(stdout) {
    return stdout
        .split(/\r?\n/u)
        .map((line) => line.trim())
        .filter(Boolean);
}
export function parseGitManageTagResult(providerResult, target) {
    return {
        parser: "git-tag-output-v1",
        action: target.action,
        tagName: target.tagName,
        targetRef: target.targetRef,
        force: target.force,
        exitCode: providerResult?.exitCode,
        stdoutLineCount: lineCount(providerResult?.stdout ?? ""),
        stderrLineCount: lineCount(providerResult?.stderr ?? ""),
        tagNames: target.action === "list" && providerResult !== undefined ? parseListedTags(providerResult.stdout) : [],
        operationHint: providerResult === undefined ? undefined : firstOutputLine(providerResult.stdout, providerResult.stderr),
        tagCreated: providerResult?.exitCode === 0 && (target.action === "create" || target.action === "annotate"),
        tagDeleted: providerResult?.exitCode === 0 && target.action === "delete",
    };
}
function success(normalized, dryRun, providerResult) {
    const executionPlan = plan(normalized, dryRun ? "dry-run" : "runtime-git-executor", dryRun);
    const risk = riskForTarget(normalized.target);
    return {
        ok: true,
        toolId: "git.manageTag",
        plan: executionPlan,
        output: {
            kind: "agentCore.basicTool.git.manageTag",
            target: normalized.target,
            runtimeEntry,
            risk,
            gitArgs: executionPlan.gitArgs,
            commandPreview: executionPlan.commandPreview,
            timeoutMs: normalized.timeoutMs ?? gitManageTagDescriptor.defaultTimeoutMs,
            dryRun,
            executionBlocked: dryRun,
            providerCalled: providerResult !== undefined,
            exitCode: providerResult?.exitCode,
            stdout: providerResult?.stdout,
            stderr: providerResult?.stderr,
            permissionsRequired: permissionsForTarget(normalized.target),
            unsafeSideEffects: normalized.target.action !== "list",
            managesTag: true,
            resultEnvelope: parseGitManageTagResult(providerResult, normalized.target),
        },
        audit: [
            auditEvent(dryRun ? "agentCore.basicTool.git.manageTag.dryRun" : "agentCore.basicTool.git.manageTag.executed", normalized.context, normalized.target.repositoryPath, {
                action: normalized.target.action,
                tagName: normalized.target.tagName,
                targetRef: normalized.target.targetRef,
                exitCode: providerResult?.exitCode,
            }),
        ],
        events: [dryRun ? "basicTool.git.manageTag.dryRun" : "basicTool.git.manageTag.executed"],
    };
}
export function planGitTagManagement(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 planGitManageTag = planGitTagManagement;
export async function executeGitManageTag(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.manageTag 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.manageTag provider rejected the request or failed safely", "provider", normalized.context, normalized.target.repositoryPath);
    }
}
