export const codeDebugCaptureStateDescriptor = { toolName: "code.debugCaptureState", toolId: "code.debugCaptureState", toolFamily: "codeBase.debugCode", purpose: "capture debug state through runtime-owned debug support", dryRunOnly: false, unsafeSideEffects: false };
function sv(value) { return typeof value === "string" ? value : undefined; }
function blank(value) { return typeof value !== "string" || value.trim().length === 0; }
function list(values) { return Array.isArray(values) ? [...new Set(values.map((value) => (typeof value === "string" ? value.trim() : "")).filter(Boolean))] : []; }
function failure(code, message, boundary) { return { ok: false, error: { code, message, boundary, safeForRuntimeInspection: true, internalDetailExposed: false }, events: ["agentCore.basicTool.code.debugCaptureState.rejected"] }; }
function auditEvent(type, normalized, metadata) { return { type, toolId: "code.debugCaptureState", invocationId: normalized?.invocationId ?? "code.debugCaptureState:unknown", dryRun: normalized?.dryRun ?? true, metadata: { ...(normalized?.context.auditMetadata ?? {}), ...(metadata ?? {}) } }; }
function toolFailure(code, message, boundary, normalized) { return { ok: false, toolId: "code.debugCaptureState", error: { code, message, boundary, safeForRuntimeInspection: true, internalDetailExposed: false }, audit: [auditEvent("agentCore.basicTool.code.debugCaptureState.rejected", normalized, { code, boundary })], events: ["agentCore.basicTool.code.debugCaptureState.rejected"] }; }
function normalizeCapture(capture) { return { includeStack: capture?.includeStack ?? true, includeVariables: capture?.includeVariables ?? false, includeBreakpoints: capture?.includeBreakpoints ?? true, maxVariables: capture?.maxVariables ?? 0 }; }
function rejected(...gates) { return gates.find((gate) => gate?.accepted === false || gate?.allowed === false); }
function approved(...gates) { return gates.some((gate) => gate?.accepted === true || gate?.allowed === true); }
function normalize(request = {}) {
    const context = request.context ?? {};
    const runtimeId = sv(context.runtimeId ?? request.runtimeId)?.trim() ?? "";
    const sessionId = sv(context.sessionId ?? request.sessionId)?.trim() ?? "";
    if (runtimeId.length === 0)
        return failure("MISSING_RUNTIME_ID", "code.debugCaptureState requires runtimeId", "input");
    if (sessionId.length === 0)
        return failure("MISSING_SESSION_ID", "code.debugCaptureState requires sessionId", "input");
    if (request.target === undefined || request.target.kind === undefined)
        return failure("MISSING_TARGET", "code.debugCaptureState requires a target kind", "input");
    if (blank(request.target.id))
        return failure("MISSING_TARGET_ID", "code.debugCaptureState requires a target id", "input");
    if (request.capture?.maxVariables !== undefined && (!Number.isInteger(request.capture.maxVariables) || request.capture.maxVariables < 0))
        return failure("INVALID_CAPTURE_LIMIT", "code.debugCaptureState maxVariables cannot be negative", "input");
    const deniedGate = rejected(request.contract, request.governance, request.guard, context.guard);
    if (deniedGate !== undefined)
        return failure(deniedGate === request.contract ? "CONTRACT_REJECTED" : "GOVERNANCE_REJECTED", deniedGate.reason ?? "code.debugCaptureState was rejected by runtime governance", deniedGate === request.contract ? "contract" : "governance");
    const requested = list(context.requestedScopes ?? request.requestedScopes);
    const allowed = list(context.allowedScopes ?? request.allowedScopes);
    const denied = requested.filter((scope) => !allowed.includes(scope));
    if (denied.length > 0)
        return failure("SCOPE_DENIED", `debug capture scope ${denied[0]} is outside runtime governance`, "scope");
    return { runtimeId, sessionId, invocationId: sv(context.invocationId ?? request.invocationId)?.trim() || `${runtimeId}:${sessionId}:code.debugCaptureState`, target: { kind: request.target.kind, id: request.target.id?.trim() ?? "", cwd: request.target.cwd?.trim() || undefined }, capture: normalizeCapture(request.capture), dryRun: request.dryRun !== false && context.dryRun !== false, context };
}
function planFrom(n) { return { toolName: "code.debugCaptureState", runtimeId: n.runtimeId, sessionId: n.sessionId, invocationId: n.invocationId, target: n.target, permissions: ["debug:read"], capture: n.capture, execution: { dryRun: true, captured: false, unsafeSideEffects: false }, audit: { governanceRequired: true, tapHandoffReady: true }, storage: { logic: { persisted: false }, records: [{ toolName: "code.debugCaptureState" }] } }; }
function empty(n) { return { kind: "agentCore.basicTool.code.debugCaptureState.output", runtimeId: n.runtimeId, sessionId: n.sessionId, invocationId: n.invocationId, target: n.target, capture: n.capture, runtimeEntry: { owner: "runtime", port: "BaseToolExecutorPort.debug.captureState", method: "captureState" }, dryRun: true, providerCalled: false, state: "planned", stack: [], variables: [], breakpoints: [], truncated: false, unsafeSideEffects: false }; }
export function planCodeDebugCaptureState(request = {}) { if (request.dryRun === false || request.context?.dryRun === false)
    return failure("REAL_DEBUG_CAPTURE_NOT_ALLOWED", "planCodeDebugCaptureState only creates a dry-run capture envelope", "governance"); const n = normalize(request); if ("ok" in n)
    return n; return { ok: true, plan: planFrom(n), events: ["agentCore.basicTool.code.debugCaptureState.planned"] }; }
export async function executeCodeDebugCaptureState(request = {}) {
    const n = normalize(request);
    if ("ok" in n)
        return toolFailure(n.error.code, n.error.message, n.error.boundary);
    if (n.dryRun)
        return { ok: true, toolId: "code.debugCaptureState", output: empty(n), audit: [auditEvent("agentCore.basicTool.code.debugCaptureState.planned", n)], events: ["agentCore.basicTool.code.debugCaptureState.planned"] };
    if (!approved(request.guard, request.governance, n.context.guard))
        return toolFailure("GOVERNANCE_REJECTED", "code.debugCaptureState real capture requires an affirmative runtime guard", "governance", n);
    if (request.provider === undefined)
        return toolFailure("PROVIDER_UNAVAILABLE", "code.debugCaptureState requires runtime debug.captureState support", "provider", n);
    try {
        const result = await request.provider({ target: n.target, capture: n.capture }, n.context);
        const vars = (result.variables ?? []).slice(0, n.capture.maxVariables || undefined);
        return { ok: true, toolId: "code.debugCaptureState", output: { ...empty(n), dryRun: false, providerCalled: true, state: result.state, stack: result.stack ?? [], variables: vars, breakpoints: result.breakpoints ?? [], truncated: (result.variables ?? []).length > vars.length }, audit: [auditEvent("agentCore.basicTool.code.debugCaptureState.completed", n)], events: ["agentCore.basicTool.code.debugCaptureState.completed"] };
    }
    catch {
        return toolFailure("PROVIDER_FAILURE", "code.debugCaptureState provider failed while reading runtime-owned debug state", "provider", n);
    }
}
