import { BestAgentError } from "@best-agent/foundation";
import { PLAN_TODO_KINDS, } from "@best-agent/protocol";
import { z } from "zod";
import { isPersistableProviderToolName } from "../provider/provider-tool-names.js";
const ProviderKindSchema = z.enum([
    "openai",
    "openai-responses",
    "anthropic",
    "google",
    "xai",
    "xai-responses",
    "deepseek",
    "moonshotai",
    "aihubmix",
    "aihubmix-responses",
    "zenmux",
    "customProvider",
]);
const ProviderCompatibilityModeSchema = z.enum(["auto", "compatible", "native"]);
const LegacyProviderDecisionTransportSchema = z.enum(["native_tools", "strict_json_envelope"]);
const LegacyProviderDecisionTransportModeSchema = z.enum([
    "auto",
    "native_tools",
    "strict_json_envelope",
]);
function normalizePersistedDecisionTransport(_value) {
    return "native_tools";
}
function normalizePersistedDecisionTransportMode(value) {
    return value === "auto" ? "auto" : normalizePersistedDecisionTransport(value);
}
const ProviderDecisionTransportSchema = LegacyProviderDecisionTransportSchema.transform(normalizePersistedDecisionTransport);
const ProviderDecisionTransportModeSchema = LegacyProviderDecisionTransportModeSchema.transform(normalizePersistedDecisionTransportMode);
const ToolInstructionEvidenceSchema = z.strictObject({
    end: z.number(),
    parser: z.string(),
    start: z.number(),
    text: z.string(),
});
const ToolPolicyConflictSchema = z.strictObject({
    evidence: z.array(ToolInstructionEvidenceSchema),
    kind: z.enum([
        "allow-only-conflict",
        "allow-only-forbidden",
        "required-forbidden",
        "required-not-allowed",
    ]),
    toolNames: z.array(z.string()),
});
const ProviderTurnActionPolicySchema = z.strictObject({
    allowedActions: z.array(z.enum(["phase_result", "plan_update", "respond", "tool"])),
    directResponseAllowed: z.boolean(),
    required: z
        .union([
        z.strictObject({
            kind: z.literal("phase_result"),
            phaseName: z.string(),
            reason: z.string(),
        }),
        z.strictObject({
            kind: z.literal("plan_update"),
            reason: z.string(),
        }),
        z.strictObject({
            conflicts: z.array(ToolPolicyConflictSchema).optional(),
            evidence: z.array(ToolInstructionEvidenceSchema).optional(),
            kind: z.literal("tool"),
            reason: z.string(),
            requiredAllToolNames: z.array(z.string()).optional(),
            requiredAnyToolGroups: z.array(z.array(z.string())).optional(),
            requiredAnyToolNames: z.array(z.string()).optional(),
            selectableToolNames: z.array(z.string()).optional(),
            toolNames: z.array(z.string()),
        }),
    ])
        .optional(),
});
const ProviderReasoningEffortSchema = z.enum(["none", "minimal", "low", "medium", "high", "xhigh"]);
const FailureRecoverabilitySchema = z.enum([
    "retryable",
    "repairable",
    "blocked",
    "interrupted",
    "terminal",
    "unknown",
]);
const FailureScopeSchema = z.enum([
    "turn",
    "provider",
    "tool",
    "approval",
    "workflow",
    "child-thread",
]);
const SessionUsageOperationSchema = z.enum([
    "compaction",
    "decide",
    "deep-research-subquestion",
    "deep-research-synthesis",
    "generate",
    "memory-refresh",
    "manual-compact-followup",
    "post-compact-followup",
    "repair",
    "tool-followup",
    "steer",
]);
const RunPhaseNameSchema = z.enum(["inspect", "plan", "change", "verify", "summarize", "wait"]);
const PhaseResultPhaseNameSchema = z.enum([
    "inspect",
    "plan",
    "change",
    "explore",
    "build",
    "verify",
]);
const PlanStatusSchema = z.enum([
    "draft",
    "review_ready",
    "approved",
    "rejected",
    "superseded",
    "executing",
    "completed",
]);
const PlanTodoStatusSchema = z.enum([
    "pending",
    "in_progress",
    "blocked",
    "done",
    "cancelled",
]);
const VerdictStatusSchema = z.enum([
    "satisfied",
    "unsatisfied",
    "inconclusive",
    "blocked",
]);
const ToolResultOutcomeSchema = z.enum(["success", "error"]);
const ToolFailureKindSchema = z.enum([
    "interrupted",
    "denied",
    "validation-failed",
    "dispatch-failed",
    "execution-failed",
    "heartbeat-timeout",
    "stale-result-rejected",
    "replay-disallowed",
    "verification-failed",
]);
const ToolInputValueSchema = z.lazy(() => z.union([
    z.string(),
    z.number(),
    z.boolean(),
    z.null(),
    z.array(ToolInputValueSchema),
    z.record(z.string(), ToolInputValueSchema),
]));
const ProviderToolNameSchema = z.string().refine(isPersistableProviderToolName);
const PersistedProviderToolNameSchema = z.preprocess((value) => typeof value === "string" && isPersistableProviderToolName(value) ? value : undefined, ProviderToolNameSchema.optional());
const ActionIdSchema = z.string().transform((value) => value);
const AssetIdSchema = z.string().transform((value) => value);
const ItemIdSchema = z.string().transform((value) => value);
const ThreadIdSchema = z.string().transform((value) => value);
const TurnIdSchema = z.string().transform((value) => value);
const ApprovalIdSchema = z
    .string()
    .transform((value) => value);
const PromptContentPartSchema = z.union([
    z.strictObject({
        text: z.string(),
        type: z.literal("text"),
    }),
    z.strictObject({
        filename: z.string().optional(),
        image: z.string().optional(),
        mediaType: z.string().optional(),
        path: z.string().optional(),
        type: z.literal("image"),
    }),
    z.strictObject({
        data: z.string().optional(),
        filename: z.string().optional(),
        mediaType: z.string().optional(),
        path: z.string().optional(),
        type: z.literal("file"),
    }),
]);
const ConversationToolCallSchema = z.strictObject({
    input: ToolInputValueSchema,
    inputDigest: z.string().optional(),
    inputStableDigest: z.string().optional(),
    providerToolName: PersistedProviderToolNameSchema,
    toolCallId: z.string(),
    toolName: z.string(),
});
const ConversationMessageSchema = z.union([
    z.strictObject({
        content: z.string(),
        contentParts: z.array(PromptContentPartSchema).optional(),
        role: z.literal("user"),
    }),
    z.strictObject({
        content: z.string(),
        role: z.literal("assistant"),
        toolCalls: z.array(ConversationToolCallSchema).optional(),
    }),
    z.strictObject({
        code: z.string(),
        content: z.string(),
        role: z.literal("runtime"),
        severity: z.enum(["warning", "error"]),
    }),
    z.strictObject({
        content: z.string(),
        errorCode: z.string().optional(),
        failureKind: ToolFailureKindSchema.optional(),
        outcome: ToolResultOutcomeSchema,
        providerToolName: PersistedProviderToolNameSchema,
        role: z.literal("tool"),
        toolCallId: z.string(),
        toolName: z.string(),
    }),
]);
const ProviderCacheControlSchema = z.strictObject({
    stablePrefixText: z.string().optional(),
    stablePrefixTokens: z.number().optional(),
});
const ProviderToolOptionSchema = z.object({
    description: z.string(),
    effect: z.enum(["read", "search", "write", "execute", "verify", "process"]).optional(),
    inputSchema: z.unknown().optional(),
    name: z.string(),
});
const ClosurePolicySchema = z.strictObject({
    deterministicSufficient: z.boolean(),
    humanVerdictRequired: z.boolean(),
    modelVerdictAllowed: z.boolean(),
    onInconclusive: z.enum(["reopen", "block", "escalate", "expand"]),
    requiredAgreement: z.enum([
        "any-one",
        "deterministic-and-human",
        "deterministic-and-model",
        "human-and-model",
        "all-allowed",
    ]),
});
const AdvancedPhaseLoopStateSchema = z.strictObject({
    closurePolicy: ClosurePolicySchema,
    hostProfileName: z.string(),
    summaryProfileName: z.string(),
    threadMode: z.enum(["default", "plan"]),
});
const PlanTodoSchema = z.strictObject({
    content: z.string(),
    dependsOn: z.array(z.string()),
    id: z.string(),
    kind: z.enum(PLAN_TODO_KINDS),
    owner: z.string(),
    status: PlanTodoStatusSchema,
});
const PlanProjectionSchema = z.strictObject({
    acceptanceCriteria: z.array(z.string()),
    affectedPaths: z.array(z.string()),
    approvedRevision: z.number().optional(),
    evidenceRefs: z.array(z.string()),
    graphId: z.string(),
    graphRevision: z.number(),
    openQuestions: z.array(z.string()),
    rejectedRevision: z.number().optional(),
    risks: z.array(z.string()),
    rootNodeId: z.string().optional(),
    status: PlanStatusSchema,
    summary: z.string(),
    threadId: ThreadIdSchema,
    title: z.string(),
    todos: z.array(PlanTodoSchema),
    updatedAt: z.string(),
});
const PhaseResultCandidateVerdictSchema = z.strictObject({
    blockingReason: z.string().optional(),
    status: VerdictStatusSchema,
    summary: z.string(),
});
const PhaseResultEnvelopeSchema = z.strictObject({
    artifactRefs: z.array(z.string()).optional(),
    candidateVerdict: PhaseResultCandidateVerdictSchema.optional(),
    phaseName: PhaseResultPhaseNameSchema,
    source: z
        .enum(["native_tools", "runtime", "strict_json_envelope"])
        .transform((source) => source === "strict_json_envelope" ? "native_tools" : source),
    summary: z.string(),
    supportsEvidenceIds: z.array(z.string()),
});
const ProviderGenerateCheckpointRequestBaseSchema = z.strictObject({
    cacheControl: ProviderCacheControlSchema.optional(),
    messages: z.array(ConversationMessageSchema),
    systemPrompt: z.string().optional(),
});
const ProviderGenerateCheckpointRequestSchema = ProviderGenerateCheckpointRequestBaseSchema;
const ProviderDecideCheckpointRequestSchema = ProviderGenerateCheckpointRequestBaseSchema.extend({
    actionPolicy: ProviderTurnActionPolicySchema.optional(),
    allowPhaseResult: z.boolean().optional(),
    allowPlanUpdate: z.boolean().optional(),
    decisionTransport: ProviderDecisionTransportSchema,
    phaseResultPhaseName: PhaseResultPhaseNameSchema.optional(),
    toolUsePolicy: z
        // TODO: drop tool-first-implementation after legacy checkpoints are migrated.
        .enum(["answer-when-sufficient", "tool-first-implementation", "required-tool-only"])
        .optional(),
    tools: z.array(ProviderToolOptionSchema),
});
const ProviderCorrectionTraceSchema = z
    .strictObject({
    code: z.enum([
        "PROVIDER_NO_ACTION",
        "PROVIDER_DECISION_INVALID",
        "TERMINAL_OUTPUT_INVALID",
        "PROMPT_DELIVERABLE_MISSING",
    ]),
    error: z
        .strictObject({
        errorCode: z.string(),
        message: z.string(),
        recoverability: FailureRecoverabilitySchema,
        scope: FailureScopeSchema,
    })
        .optional(),
    id: z.string(),
    prompt: z.string().optional(),
})
    .transform(({ prompt: _legacyPrompt, ...value }) => value);
const ArtifactSizeLimitSchema = z.strictObject({
    bytes: z.number(),
    inclusive: z.boolean(),
});
const ArtifactObligationConstraintSchema = z.union([
    z.strictObject({
        bytes: z.number(),
        inclusive: z.boolean(),
        kind: z.literal("max-bytes"),
    }),
    z.strictObject({
        allowedSiblingBasenames: z.array(z.string()),
        directory: z.string(),
        kind: z.literal("exclusive-directory"),
    }),
]);
const ArtifactObligationSchema = z
    .strictObject({
    allowedSiblingBasenames: z.array(z.string()).optional(),
    constraints: z.array(ArtifactObligationConstraintSchema).optional(),
    exclusiveDirectory: z.string().optional(),
    initialDigest: z.string().optional(),
    kind: z.enum([
        "write-target",
        "modify-target",
        "run-command",
        "collect-evidence",
        "structured-answer",
    ]),
    maxBytes: z.union([z.number(), ArtifactSizeLimitSchema]).optional(),
    obligationId: z.string(),
    required: z.boolean(),
    target: z.string().optional(),
    verifier: z.enum([
        "exists",
        "changed",
        "command-zero-exit",
        "evidence-present",
        "structured-output",
        "semantic-only",
    ]),
})
    .transform((obligation) => {
    const constraints = [
        ...(obligation.constraints ?? []),
        ...legacyArtifactObligationConstraints(obligation),
    ];
    return {
        ...(constraints.length > 0 ? { constraints } : {}),
        ...(obligation.initialDigest ? { initialDigest: obligation.initialDigest } : {}),
        kind: obligation.kind,
        obligationId: obligation.obligationId,
        required: obligation.required,
        ...(obligation.target ? { target: obligation.target } : {}),
        verifier: obligation.verifier,
    };
});
function legacyArtifactObligationConstraints(input) {
    const constraints = [];
    if (input.maxBytes !== undefined) {
        constraints.push(typeof input.maxBytes === "number"
            ? { kind: "max-bytes", bytes: input.maxBytes, inclusive: true }
            : {
                kind: "max-bytes",
                bytes: input.maxBytes.bytes,
                inclusive: input.maxBytes.inclusive,
            });
    }
    if (input.exclusiveDirectory && input.allowedSiblingBasenames) {
        constraints.push({
            kind: "exclusive-directory",
            directory: input.exclusiveDirectory,
            allowedSiblingBasenames: input.allowedSiblingBasenames,
        });
    }
    return constraints;
}
const ProviderReplayConfigShape = {
    baseURL: z.string().optional(),
    compatibilityMode: ProviderCompatibilityModeSchema.optional(),
    decisionTransport: ProviderDecisionTransportModeSchema.optional(),
    kind: ProviderKindSchema,
    maxInputTokens: z.number().optional(),
    maxOutputTokens: z.number().optional(),
    model: z.string(),
    reasoningEffort: ProviderReasoningEffortSchema.optional(),
    systemPrompt: z.string().optional(),
    timeoutMs: z.number().optional(),
};
const ProviderReplayConfigInputSchema = z
    .strictObject({
    apiKey: z.string().optional(),
    ...ProviderReplayConfigShape,
})
    .transform(({ apiKey: _apiKey, ...config }) => config);
const PendingCompactionFollowUpMarkerSchema = z.strictObject({
    id: z.string(),
    operation: SessionUsageOperationSchema,
});
const ToolReplayPolicySchema = z.enum([
    "auto-if-idempotent",
    "repair-only",
    "never",
]);
const ToolExecutionClassSchema = z.enum([
    "read-only",
    "workspace-mutation",
    "external-side-effect",
]);
const PendingProviderToolCallSchema = z.strictObject({
    providerToolName: PersistedProviderToolNameSchema,
    replayToolInputAssetId: AssetIdSchema.optional(),
    replayToolInputDigest: z.string().optional(),
    toolCallId: ItemIdSchema,
    toolInput: ToolInputValueSchema,
    toolName: z.string(),
});
const CompletedProviderToolCallSchema = z
    .strictObject({
    providerToolName: PersistedProviderToolNameSchema,
    replayToolInputAssetId: AssetIdSchema.optional(),
    replayToolInputDigest: z.string().optional(),
    errorCode: z.string().optional(),
    failureKind: ToolFailureKindSchema.optional(),
    inputDigest: z.string().optional(),
    inputStableDigest: z.string().optional(),
    outcome: ToolResultOutcomeSchema,
    replayPolicy: ToolReplayPolicySchema.optional(),
    toolCallId: ItemIdSchema,
    toolInput: ToolInputValueSchema,
    toolName: z.string(),
    toolOutput: z.string(),
})
    .transform(({ inputDigest, inputStableDigest, ...call }) => ({
    ...call,
    ...((inputStableDigest ?? inputDigest)
        ? { inputStableDigest: inputStableDigest ?? inputDigest }
        : {}),
}));
function uniqueStrings(values) {
    return [...new Set(values)].filter((value) => value.length > 0);
}
function normalizePersistedRequiredAnyToolGroups(input) {
    const normalizedGroups = [];
    for (const group of [
        ...(input.requiredAnyToolGroups ?? []),
        ...(input.requiredAnyToolNames ? [input.requiredAnyToolNames] : []),
    ]) {
        const normalizedGroup = uniqueStrings(group);
        if (normalizedGroup.length > 1 &&
            !normalizedGroups.some((existingGroup) => existingGroup.length === normalizedGroup.length &&
                existingGroup.every((toolName, index) => toolName === normalizedGroup[index]))) {
            normalizedGroups.push(normalizedGroup);
        }
    }
    return normalizedGroups;
}
function buildAggregateRequiredToolNames(input) {
    const aggregate = [];
    const add = (toolName) => {
        if (toolName && !aggregate.includes(toolName)) {
            aggregate.push(toolName);
        }
    };
    for (const toolName of input.requiredToolNames) {
        add(toolName);
    }
    for (const toolName of input.requiredAllToolNames) {
        add(toolName);
    }
    for (const group of input.requiredAnyToolGroups) {
        if (!aggregate.some((toolName) => group.includes(toolName))) {
            add(group[0]);
        }
    }
    return aggregate;
}
const RequiredToolConstraintSchema = z
    .strictObject({
    allowedToolNames: z.array(z.string()).optional(),
    conflicts: z.array(ToolPolicyConflictSchema).optional(),
    evidence: z.array(ToolInstructionEvidenceSchema).optional(),
    forbiddenToolNames: z.array(z.string()),
    primaryToolName: z.string().optional(),
    reason: z.enum([
        "explicit-exclusive-tool-set",
        "explicit-forbidden-tool",
        "explicit-required-tool",
        "repo-wide-coverage",
        "requested-subagent",
    ]),
    requiredAllToolNames: z.array(z.string()).optional(),
    requiredAnyToolGroups: z.array(z.array(z.string())).optional(),
    requiredAnyToolNames: z.array(z.string()).optional(),
    requiredReleasePolicy: z.enum(["after-required-tools-succeeded", "none"]),
    requiredToolNames: z.array(z.string()),
    restrictionScope: z.enum(["turn", "until-required-satisfied"]),
    selectableToolNames: z.array(z.string()).optional(),
    visibleToolNames: z.array(z.string()).optional(),
})
    .transform((constraint) => {
    const legacyAllowedToolNames = constraint.allowedToolNames;
    const requiredAnyToolGroups = normalizePersistedRequiredAnyToolGroups(constraint);
    const legacyRequiredAnyToolNames = new Set(requiredAnyToolGroups.flat());
    const requiredAllToolNames = constraint.requiredAllToolNames
        ? uniqueStrings(constraint.requiredAllToolNames)
        : uniqueStrings(constraint.requiredToolNames.filter((toolName) => !legacyRequiredAnyToolNames.has(toolName)));
    const requiredToolNames = buildAggregateRequiredToolNames({
        requiredAllToolNames,
        requiredAnyToolGroups,
        requiredToolNames: constraint.requiredToolNames,
    });
    return {
        ...(constraint.conflicts ? { conflicts: constraint.conflicts } : {}),
        ...(constraint.evidence ? { evidence: constraint.evidence } : {}),
        forbiddenToolNames: constraint.forbiddenToolNames,
        ...(constraint.primaryToolName ? { primaryToolName: constraint.primaryToolName } : {}),
        reason: constraint.reason,
        ...(requiredAllToolNames.length > 0 ? { requiredAllToolNames } : {}),
        ...(requiredAnyToolGroups.length > 0 ? { requiredAnyToolGroups } : {}),
        requiredReleasePolicy: constraint.requiredReleasePolicy,
        requiredToolNames,
        restrictionScope: constraint.restrictionScope,
        ...((constraint.selectableToolNames ?? legacyAllowedToolNames)
            ? { selectableToolNames: constraint.selectableToolNames ?? legacyAllowedToolNames }
            : {}),
        ...((constraint.visibleToolNames ?? legacyAllowedToolNames)
            ? { visibleToolNames: constraint.visibleToolNames ?? legacyAllowedToolNames }
            : {}),
    };
});
const ArtifactRefSchema = z
    .strictObject({
    assetId: z.string(),
    contentType: z.string().optional(),
    kind: z.enum(["attachment", "artifact"]).optional(),
    name: z.string().optional(),
})
    .transform((ref) => ({
    assetId: ref.assetId,
    ...(ref.contentType ? { contentType: ref.contentType } : {}),
    ...(ref.kind ? { kind: ref.kind } : {}),
    ...(ref.name ? { name: ref.name } : {}),
}));
const ToolCallRefSchema = z
    .strictObject({
    attemptId: z.string().optional(),
    runId: z.string().optional(),
    stepId: z.string().optional(),
    threadId: z.string(),
    toolCallId: z.string(),
    turnId: z.string(),
})
    .transform((ref) => ({
    ...(ref.attemptId ? { attemptId: ref.attemptId } : {}),
    ...(ref.runId ? { runId: ref.runId } : {}),
    ...(ref.stepId ? { stepId: ref.stepId } : {}),
    threadId: ref.threadId,
    toolCallId: ref.toolCallId,
    turnId: ref.turnId,
}));
const ToolResultPresentationSchema = z.strictObject({
    kind: z.enum(["inline", "artifact", "mixed"]),
    title: z.string().optional(),
});
const ToolResultItemSchema = z.strictObject({
    callId: ItemIdSchema,
    createdAt: z.string(),
    errorCode: z.string().optional(),
    failureKind: ToolFailureKindSchema.optional(),
    id: ItemIdSchema,
    kind: z.literal("tool-result"),
    outcome: ToolResultOutcomeSchema,
    output: z.string(),
    structuredContent: z.record(z.string(), z.unknown()).optional(),
    threadId: ThreadIdSchema,
    toolCallRef: ToolCallRefSchema.optional(),
    toolName: z.string(),
    traceAssetId: AssetIdSchema.optional(),
    turnId: TurnIdSchema,
});
const ToolResultEnvelopeSchema = z
    .strictObject({
    artifactRefs: z.array(ArtifactRefSchema).optional(),
    evidencePayload: z.record(z.string(), z.unknown()).optional(),
    failureKind: ToolFailureKindSchema.optional(),
    inputDigest: z.string().optional(),
    inputRawDigest: z.string().optional(),
    inputStableDigest: z.string().optional(),
    outcome: ToolResultOutcomeSchema,
    presentation: ToolResultPresentationSchema.optional(),
    rawText: z.string().optional(),
    stderrSummary: z.string().optional(),
    stdoutSummary: z.string().optional(),
    summary: z.string(),
    toolCallId: ItemIdSchema,
    toolCallRef: ToolCallRefSchema.optional(),
    toolName: z.string(),
    touchedFilePaths: z.array(z.string()).optional(),
    verificationHints: z.array(z.string()).optional(),
})
    .refine((envelope) => envelope.inputRawDigest || envelope.inputDigest, {
    message: "Tool result envelope requires inputRawDigest.",
})
    .transform(({ inputDigest, inputRawDigest, ...envelope }) => ({
    ...envelope,
    inputRawDigest: inputRawDigest ?? inputDigest,
}));
const ThreadEvidenceRecordSchema = z
    .strictObject({
    artifactIds: z.array(z.string()).optional(),
    artifactRefs: z.array(ArtifactRefSchema).optional(),
    attemptId: z.string().optional(),
    confidence: z.number(),
    contradictsEvidenceIds: z.array(z.string()).optional(),
    createdAt: z.string(),
    diagnosticCode: z.string().optional(),
    evidenceId: z.string(),
    freshness: z.string(),
    kind: z.enum([
        "tool-outcome",
        "verification-verdict",
        "artifact-fact",
        "workspace-fact",
        "research-finding",
        "handoff-fact",
    ]),
    payload: z.record(z.string(), z.unknown()),
    relatedFilePaths: z.array(z.string()).optional(),
    runId: z.string().optional(),
    sourceItemId: z.string().optional(),
    sourceToolName: z.string().optional(),
    stepId: z.string().optional(),
    summary: z.string(),
    supportsClaimIds: z.array(z.string()).optional(),
    threadId: z.string(),
    toolCallRef: ToolCallRefSchema.optional(),
    turnId: z.string(),
})
    .transform((record) => ({
    ...(record.artifactIds
        ? { artifactIds: record.artifactIds.map((artifactId) => artifactId) }
        : {}),
    ...(record.artifactRefs ? { artifactRefs: record.artifactRefs } : {}),
    ...(record.attemptId
        ? { attemptId: record.attemptId }
        : {}),
    confidence: record.confidence,
    ...(record.contradictsEvidenceIds
        ? { contradictsEvidenceIds: record.contradictsEvidenceIds }
        : {}),
    createdAt: record.createdAt,
    ...(record.diagnosticCode ? { diagnosticCode: record.diagnosticCode } : {}),
    evidenceId: record.evidenceId,
    freshness: record.freshness,
    kind: record.kind,
    payload: record.payload,
    ...(record.relatedFilePaths ? { relatedFilePaths: record.relatedFilePaths } : {}),
    ...(record.runId ? { runId: record.runId } : {}),
    ...(record.sourceItemId
        ? { sourceItemId: record.sourceItemId }
        : {}),
    ...(record.sourceToolName ? { sourceToolName: record.sourceToolName } : {}),
    ...(record.stepId ? { stepId: record.stepId } : {}),
    summary: record.summary,
    ...(record.supportsClaimIds ? { supportsClaimIds: record.supportsClaimIds } : {}),
    threadId: record.threadId,
    ...(record.toolCallRef ? { toolCallRef: record.toolCallRef } : {}),
    turnId: record.turnId,
}));
const ApprovalGrantScopeSchema = z.enum([
    "once",
    "session",
    "thread",
    "workspace",
]);
const CapabilityKindSchema = z.enum([
    "tool",
    "filesystem",
    "shell",
    "network",
    "mcp",
    "plugin-hook",
]);
const CapabilityRequestBaseSchema = z.strictObject({
    approvalScopes: z.array(ApprovalGrantScopeSchema).optional(),
    kind: CapabilityKindSchema.optional(),
    reason: z.string().optional(),
});
const CapabilityRequestSchema = z.union([
    CapabilityRequestBaseSchema.extend({
        group: z.literal("tool"),
        toolName: z.string(),
    }),
    CapabilityRequestBaseSchema.extend({
        action: z.enum(["read", "write"]),
        group: z.literal("filesystem"),
        path: z.string(),
        roots: z.array(z.string()).optional(),
    }),
    CapabilityRequestBaseSchema.extend({
        argv: z.array(z.string()),
        cwd: z.string(),
        group: z.literal("shell"),
    }),
    CapabilityRequestBaseSchema.extend({
        group: z.literal("network"),
        host: z.string(),
    }),
]);
const GuardianAssessmentSchema = z.strictObject({
    evidence: z.array(z.string()),
    rationale: z.string(),
    riskLevel: z.enum(["low", "medium", "high"]),
    riskScore: z.number(),
});
const ApprovalRequestSchema = z.strictObject({
    action: z.string(),
    availableScopes: z.array(ApprovalGrantScopeSchema),
    group: z.enum(["tool", "filesystem", "shell", "network"]),
    guardianAssessment: GuardianAssessmentSchema.optional(),
    id: ApprovalIdSchema,
    kind: CapabilityKindSchema,
    reason: z.string().optional(),
    requestedAt: z.string(),
    reviewMode: z.enum(["direct", "guardian"]),
    riskClass: z.enum(["low", "medium", "high"]),
    sandboxProfile: z.enum(["none", "no-network", "no-write", "no-network-no-write"]),
    threadId: ThreadIdSchema,
    toolCallId: ItemIdSchema,
    toolCallRef: ToolCallRefSchema.optional(),
    turnId: TurnIdSchema,
});
const ApprovalResolutionSchema = z.strictObject({
    decision: z.enum(["allow", "reject", "expired"]),
    scope: ApprovalGrantScopeSchema,
});
const AskUserOptionSchema = z.strictObject({
    description: z.string().optional(),
    label: z.string(),
});
const AskUserQuestionSchema = z.union([
    z.strictObject({
        allowCustom: z.boolean().optional(),
        header: z.string().optional(),
        id: z.string(),
        kind: z.literal("choice").optional(),
        multiSelect: z.boolean().optional(),
        options: z.array(AskUserOptionSchema),
        question: z.string(),
    }),
    z.strictObject({
        header: z.string().optional(),
        id: z.string(),
        kind: z.literal("input"),
        multiline: z.boolean().optional(),
        placeholder: z.string().optional(),
        question: z.string(),
    }),
]);
const ActionResolutionSchema = z.union([
    z.strictObject({
        answers: z.record(z.string(), z.union([z.string(), z.array(z.string())])),
        status: z.literal("answered"),
    }),
    z.strictObject({
        status: z.literal("expired"),
    }),
]);
const PersistedToolResumeContextShape = {
    activeSkillIds: z.array(z.string()),
    activeReplayToolInputAssetId: AssetIdSchema.optional(),
    activeReplayToolInputDigest: z.string().optional(),
    allowPhaseResult: z.boolean().optional(),
    allowPlanUpdate: z.boolean().optional(),
    advancedPhase: AdvancedPhaseLoopStateSchema.optional(),
    artifactObligations: z.array(ArtifactObligationSchema).optional(),
    approvalTurnId: TurnIdSchema.optional(),
    availableTools: z.array(ProviderToolOptionSchema),
    cacheControl: ProviderCacheControlSchema.optional(),
    completedToolCalls: z.array(CompletedProviderToolCallSchema).optional(),
    customLoopId: z.string().optional(),
    decisionTransport: ProviderDecisionTransportSchema.optional(),
    fixedRouteConfigOverride: z.boolean().optional(),
    messages: z.array(ConversationMessageSchema),
    mode: z.enum(["provider-tool-loop", "tool-plan", "custom-loop"]),
    objective: z.string().optional(),
    pendingToolCalls: z.array(PendingProviderToolCallSchema).optional(),
    phaseName: RunPhaseNameSchema.optional(),
    phaseResultPhaseName: PhaseResultPhaseNameSchema.optional(),
    profileName: z.string(),
    providerTurnBudgeted: z.boolean().optional(),
    providerTurnCount: z.number().optional(),
    reasoning: z.string().optional(),
    repairAttempt: z.number().optional(),
    executionFailureCount: z.number().optional(),
    requiredToolConstraint: RequiredToolConstraintSchema.optional(),
    routeConfigOverride: ProviderReplayConfigInputSchema.optional(),
    systemPrompt: z.string().optional(),
    toolCallId: ItemIdSchema,
    toolInput: ToolInputValueSchema,
    toolName: z.string(),
    providerToolName: PersistedProviderToolNameSchema,
    toolStepCount: z.number(),
};
const ApprovalCheckpointPayloadSchema = z.strictObject({
    ...PersistedToolResumeContextShape,
    approval: ApprovalRequestSchema,
    approvalGrantAction: z.string().optional(),
    approvalGrantActionSource: z.enum(["legacy", "raw-request-digest"]).optional(),
    kind: z.literal("approval"),
    request: CapabilityRequestSchema,
    resolution: ApprovalResolutionSchema.optional(),
});
const ActionCheckpointPayloadSchema = z.strictObject({
    ...PersistedToolResumeContextShape,
    actionId: ActionIdSchema,
    kind: z.literal("action"),
    questions: z.array(AskUserQuestionSchema),
    resolution: ActionResolutionSchema.optional(),
});
const ToolOutputCheckpointPayloadSchema = z.strictObject({
    ...PersistedToolResumeContextShape,
    evidenceRecords: z.array(ThreadEvidenceRecordSchema).optional(),
    kind: z.literal("tool-output"),
    toolEnvelope: ToolResultEnvelopeSchema.optional(),
    toolInputStableDigest: z.string().optional(),
    toolOutput: z.string(),
    toolResultItem: ToolResultItemSchema.optional(),
});
const ToolDispatchCheckpointPayloadSchema = z.strictObject({
    ...PersistedToolResumeContextShape,
    executionClass: ToolExecutionClassSchema,
    idempotent: z.boolean(),
    kind: z.literal("tool-dispatch"),
    replayPolicy: ToolReplayPolicySchema.optional(),
    toolInputStableDigest: z.string().optional(),
});
const SubagentSpawnCheckpointPayloadSchema = z.strictObject({
    childThreadId: ThreadIdSchema.optional(),
    kind: z.literal("subagent-spawn"),
});
const SubagentJoinCheckpointPayloadSchema = z.strictObject({
    childThreadIds: z.array(ThreadIdSchema),
    kind: z.literal("subagent-join"),
});
const ResponseOutputCheckpointPayloadSchema = z.strictObject({
    activeSkillIds: z.array(z.string()),
    agentText: z.string(),
    kind: z.literal("response-output"),
    planProjection: PlanProjectionSchema.optional(),
    reasoningSummary: z.string().optional(),
    reasoningText: z.string().optional(),
});
const VerificationVerdictCheckpointPayloadSchema = z.strictObject({
    activeSkillIds: z.array(z.string()),
    advancedPhase: AdvancedPhaseLoopStateSchema,
    evidenceId: z.string().optional(),
    kind: z.literal("verification-verdict"),
    phaseOutput: z.string(),
    phaseResult: PhaseResultEnvelopeSchema.optional(),
    sourceItemId: ItemIdSchema,
    verdictId: z.string(),
});
const PhaseTransitionCheckpointPayloadSchema = z.strictObject({
    activeSkillIds: z.array(z.string()),
    kind: z.literal("phase-transition"),
    nextPhaseName: RunPhaseNameSchema,
    reopened: z.boolean().optional(),
    state: AdvancedPhaseLoopStateSchema,
    transitionMessage: z.string().optional(),
});
const ProviderRequestReplayContextSchema = z.strictObject({
    activeSkillIds: z.array(z.string()).optional(),
    advancedPhase: AdvancedPhaseLoopStateSchema.optional(),
    availableTools: z.array(ProviderToolOptionSchema).optional(),
    compactionFollowUpMarker: PendingCompactionFollowUpMarkerSchema.optional(),
    downgradeEligible: z.boolean().optional(),
    escalationLevel: z.number().optional(),
    objective: z.string().optional(),
    payloadDecisionTransport: ProviderDecisionTransportSchema.optional(),
    phaseName: RunPhaseNameSchema.optional(),
    profileName: z.string().optional(),
    providerTurnBudgeted: z.boolean().optional(),
    providerTurnCount: z.number().optional(),
    correctionTrace: ProviderCorrectionTraceSchema.optional(),
    requiredToolConstraint: RequiredToolConstraintSchema.optional(),
    routeConfigOverride: ProviderReplayConfigInputSchema.optional(),
    toolStepCount: z.number().optional(),
});
const ProviderRequestCheckpointPayloadBaseSchema = z.strictObject({
    artifactObligations: z.array(ArtifactObligationSchema).optional(),
    decisionTransport: ProviderDecisionTransportSchema.optional(),
    kind: z.literal("provider-request"),
    model: z.string().optional(),
    operation: SessionUsageOperationSchema,
    providerConfig: ProviderReplayConfigInputSchema.optional(),
    providerKind: ProviderKindSchema.optional(),
    replayContext: ProviderRequestReplayContextSchema.optional(),
    correctionTrace: ProviderCorrectionTraceSchema.optional(),
});
const ProviderRequestCheckpointPayloadSchema = z.union([
    ProviderRequestCheckpointPayloadBaseSchema.extend({
        request: z.undefined().optional(),
        requestKind: z.undefined().optional(),
    }),
    ProviderRequestCheckpointPayloadBaseSchema.extend({
        request: ProviderDecideCheckpointRequestSchema,
        requestKind: z.literal("decide"),
    }),
    ProviderRequestCheckpointPayloadBaseSchema.extend({
        request: ProviderGenerateCheckpointRequestSchema,
        requestKind: z.literal("generate"),
    }),
]);
const RUN_CHECKPOINT_ALLOWED_PREDECESSORS = {
    "provider-request": [
        "phase-transition",
        "provider-request",
        "tool-output",
        "verification-verdict",
    ],
};
function toThreadMetadata(payload) {
    const metadata = {};
    for (const [key, value] of Object.entries(payload)) {
        metadata[key] = value;
    }
    return metadata;
}
function encodeCheckpointPayload(schema, payload) {
    return toThreadMetadata(schema.parse(payload));
}
export function serializeApprovalCheckpointPayload(payload) {
    return encodeCheckpointPayload(ApprovalCheckpointPayloadSchema, payload);
}
export function serializeActionCheckpointPayload(payload) {
    return encodeCheckpointPayload(ActionCheckpointPayloadSchema, payload);
}
export function serializeToolOutputCheckpointPayload(payload) {
    return encodeCheckpointPayload(ToolOutputCheckpointPayloadSchema, payload);
}
export function serializeToolDispatchCheckpointPayload(payload) {
    return encodeCheckpointPayload(ToolDispatchCheckpointPayloadSchema, payload);
}
export function serializeSubagentSpawnCheckpointPayload(payload) {
    return encodeCheckpointPayload(SubagentSpawnCheckpointPayloadSchema, payload);
}
export function serializeSubagentJoinCheckpointPayload(payload) {
    return encodeCheckpointPayload(SubagentJoinCheckpointPayloadSchema, payload);
}
export function serializeResponseOutputCheckpointPayload(payload) {
    return encodeCheckpointPayload(ResponseOutputCheckpointPayloadSchema, payload);
}
export function serializeVerificationVerdictCheckpointPayload(payload) {
    return encodeCheckpointPayload(VerificationVerdictCheckpointPayloadSchema, payload);
}
export function serializePhaseTransitionCheckpointPayload(payload) {
    return encodeCheckpointPayload(PhaseTransitionCheckpointPayloadSchema, payload);
}
export function serializeProviderRequestCheckpointPayload(payload) {
    return encodeCheckpointPayload(ProviderRequestCheckpointPayloadSchema, payload);
}
export function parseApprovalCheckpointPayload(checkpoint) {
    const parsed = ApprovalCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid approval payload.`);
    }
    return parsed.data;
}
export function parseActionCheckpointPayload(checkpoint) {
    const parsed = ActionCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid action payload.`);
    }
    return parsed.data;
}
export function parseToolOutputCheckpointPayload(checkpoint) {
    const parsed = ToolOutputCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid tool-output payload.`);
    }
    return parsed.data;
}
export function parseToolDispatchCheckpointPayload(checkpoint) {
    const parsed = ToolDispatchCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid tool-dispatch payload.`);
    }
    return parsed.data;
}
export function parseSubagentSpawnCheckpointPayload(checkpoint) {
    const parsed = SubagentSpawnCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid subagent-spawn payload.`);
    }
    return parsed.data;
}
export function parseSubagentJoinCheckpointPayload(checkpoint) {
    const parsed = SubagentJoinCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid subagent-join payload.`);
    }
    return parsed.data;
}
export function parseResponseOutputCheckpointPayload(checkpoint) {
    const parsed = ResponseOutputCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid response-output payload.`);
    }
    return parsed.data;
}
export function parseVerificationVerdictCheckpointPayload(checkpoint) {
    const parsed = VerificationVerdictCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid verification-verdict payload.`);
    }
    return parsed.data;
}
export function parsePhaseTransitionCheckpointPayload(checkpoint) {
    const parsed = PhaseTransitionCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid phase-transition payload.`);
    }
    return parsed.data;
}
export function parseProviderRequestCheckpointPayload(checkpoint) {
    const parsed = ProviderRequestCheckpointPayloadSchema.safeParse(checkpoint.payload);
    if (!parsed.success) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint ${checkpoint.id} has invalid provider-request payload.`);
    }
    return parsed.data;
}
export function parseToolResumeCheckpointPayload(checkpoint) {
    if (checkpoint.kind === "approval-wait") {
        return parseApprovalCheckpointPayload(checkpoint);
    }
    if (checkpoint.kind === "action-wait") {
        return parseActionCheckpointPayload(checkpoint);
    }
    return parseToolOutputCheckpointPayload(checkpoint);
}
export function canRunCheckpointSupersede(pendingKind, nextKind) {
    return RUN_CHECKPOINT_ALLOWED_PREDECESSORS[nextKind]?.includes(pendingKind) === true;
}
//# sourceMappingURL=checkpoint-codec.js.map