/**
 * CAR Extension Protocol Types
 *
 * Provides comprehensive type definitions for the Layer 4 Runtime Assurance
 * extension system. This module defines the core extension interface and all
 * supporting types for capability, action, monitoring, trust, and policy hooks.
 *
 * @packageDocumentation
 * @module @vorion/car-extensions/types
 * @license Apache-2.0
 */
import { z } from 'zod';
/**
 * Zod schema for TrustTier validation
 */
export const TrustTierSchema = z.union([
    z.literal(0),
    z.literal(1),
    z.literal(2),
    z.literal(3),
    z.literal(4),
    z.literal(5),
    z.literal(6),
    z.literal(7),
]);
/**
 * Zod schema for AgentIdentity validation
 */
export const AgentIdentitySchema = z.object({
    did: z.string().min(1),
    carId: z.string().min(1),
    publisher: z.string().min(1),
    name: z.string().min(1),
    trustTier: TrustTierSchema.optional(),
    trustScore: z.number().min(0).max(1000).optional(),
    domains: z.number().int().min(0),
    level: z.number().int().min(0).max(7),
    version: z.string().regex(/^\d+\.\d+\.\d+$/),
    metadata: z.record(z.unknown()).optional(),
});
/**
 * Zod schema for Attestation validation
 */
export const AttestationSchema = z.object({
    id: z.string().uuid(),
    agentDid: z.string().min(1),
    issuerDid: z.string().min(1),
    type: z.enum(['certification', 'capability', 'trust', 'compliance']),
    claims: z.record(z.unknown()),
    issuedAt: z.date(),
    expiresAt: z.date(),
    signature: z.string().min(1),
    signatureAlgorithm: z.string().min(1),
    revoked: z.boolean(),
    revokedAt: z.date().optional(),
});
/**
 * Zod schema for RequestContext validation
 */
export const RequestContextSchema = z.object({
    source: z.string().min(1),
    purpose: z.string().optional(),
    resource: z.string().optional(),
    metadata: z.record(z.unknown()).optional(),
});
/**
 * Zod schema for Constraint validation
 */
export const ConstraintSchema = z.object({
    type: z.enum(['rate_limit', 'time_window', 'resource', 'approval', 'custom']),
    params: z.record(z.unknown()),
});
/**
 * Zod schema for CapabilityRequest validation
 */
export const CapabilityRequestSchema = z.object({
    domains: z.array(z.string().min(1)),
    level: z.number().int().min(0).max(7),
    context: RequestContextSchema,
    ttl: z.number().int().positive().optional(),
});
/**
 * Zod schema for CapabilityGrant validation
 */
export const CapabilityGrantSchema = z.object({
    id: z.string().uuid(),
    carId: z.string().min(1),
    domains: z.number().int().min(0),
    level: z.number().int().min(0).max(7),
    issuedAt: z.date(),
    expiresAt: z.date(),
    constraints: z.array(ConstraintSchema).optional(),
    token: z.string().optional(),
});
/**
 * Zod schema for ActionTarget validation
 */
export const ActionTargetSchema = z.object({
    type: z.string().min(1),
    id: z.string().min(1),
    metadata: z.record(z.unknown()).optional(),
});
/**
 * Zod schema for ActionRequest validation
 */
export const ActionRequestSchema = z.object({
    type: z.string().min(1),
    target: ActionTargetSchema,
    params: z.record(z.unknown()),
    context: RequestContextSchema,
});
/**
 * Zod schema for SideEffect validation
 */
export const SideEffectSchema = z.object({
    type: z.string().min(1),
    target: z.string().min(1),
    description: z.string().min(1),
});
/**
 * Zod schema for ActionResult validation
 */
export const ActionResultSchema = z.object({
    success: z.boolean(),
    data: z.unknown().optional(),
    sideEffects: z.array(SideEffectSchema).optional(),
});
/**
 * Zod schema for ActionRecord validation
 */
export const ActionRecordSchema = ActionRequestSchema.extend({
    id: z.string().uuid(),
    startedAt: z.date(),
    completedAt: z.date().optional(),
    result: ActionResultSchema.optional(),
    error: z.instanceof(Error).optional(),
});
/**
 * Zod schema for PreCheckResult validation
 */
export const PreCheckResultSchema = z.object({
    allow: z.boolean(),
    reason: z.string().optional(),
    constraints: z.array(ConstraintSchema).optional(),
});
/**
 * Zod schema for ActionModification validation
 */
export const ActionModificationSchema = z.object({
    field: z.string().min(1),
    original: z.unknown(),
    modified: z.unknown(),
    reason: z.string().min(1),
});
/**
 * Zod schema for ApprovalRequirement validation
 */
export const ApprovalRequirementSchema = z.object({
    type: z.enum(['human', 'system', 'manager']),
    approver: z.string().optional(),
    timeout: z.number().int().positive().optional(),
});
/**
 * Zod schema for PreActionResult validation
 */
export const PreActionResultSchema = z.object({
    proceed: z.boolean(),
    reason: z.string().optional(),
    modifications: z.array(ActionModificationSchema).optional(),
    requiredApprovals: z.array(ApprovalRequirementSchema).optional(),
});
/**
 * Zod schema for ExpiryDecision validation
 */
export const ExpiryDecisionSchema = z.object({
    action: z.enum(['renew', 'expire', 'revoke']),
    newTtl: z.number().int().positive().optional(),
    reason: z.string().optional(),
});
/**
 * Zod schema for FailureResponse validation
 */
export const FailureResponseSchema = z.object({
    retry: z.boolean(),
    retryDelay: z.number().int().positive().optional(),
    maxRetries: z.number().int().positive().optional(),
    fallback: ActionRequestSchema.optional(),
});
/**
 * Zod schema for BehaviorMetrics validation
 */
export const BehaviorMetricsSchema = z.object({
    windowStart: z.date(),
    windowEnd: z.date(),
    requestCount: z.number().int().min(0),
    errorCount: z.number().int().min(0),
    avgResponseTime: z.number().min(0),
    p99ResponseTime: z.number().min(0),
    actionsByType: z.record(z.number().int().min(0)),
    domainsAccessed: z.array(z.number().int().min(0)),
    maxLevelUsed: z.number().int().min(0).max(7),
    custom: z.record(z.number()).optional(),
});
/**
 * Zod schema for BehaviorVerificationResult validation
 */
export const BehaviorVerificationResultSchema = z.object({
    inBounds: z.boolean(),
    driftScore: z.number().min(0).max(100),
    driftCategories: z.array(z.string()),
    recommendation: z.enum(['continue', 'warn', 'suspend', 'revoke']),
    details: z.string().optional(),
});
/**
 * Zod schema for MetricsReport validation
 */
export const MetricsReportSchema = z.object({
    timestamp: z.date(),
    carId: z.string().min(1),
    metrics: BehaviorMetricsSchema,
    health: z.enum(['healthy', 'degraded', 'unhealthy']),
});
/**
 * Zod schema for AnomalyReport validation
 */
export const AnomalyReportSchema = z.object({
    id: z.string().uuid(),
    detectedAt: z.date(),
    type: z.enum(['behavior', 'security', 'performance', 'compliance']),
    severity: z.enum(['low', 'medium', 'high', 'critical']),
    description: z.string().min(1),
    evidence: z.record(z.unknown()),
});
/**
 * Zod schema for AnomalyResponse validation
 */
export const AnomalyResponseSchema = z.object({
    action: z.enum(['ignore', 'log', 'alert', 'suspend', 'revoke']),
    notified: z.array(z.string()).optional(),
    escalated: z.boolean().optional(),
});
/**
 * Zod schema for RevocationEvent validation
 */
export const RevocationEventSchema = z.object({
    id: z.string().uuid(),
    agentDid: z.string().min(1),
    carId: z.string().min(1),
    revokedAt: z.date(),
    reason: z.string().min(1),
    scope: z.enum(['full', 'attestation', 'capability']),
    attestationIds: z.array(z.string().uuid()).optional(),
});
/**
 * Zod schema for TrustAdjustment validation
 */
export const TrustAdjustmentSchema = z.object({
    type: z.enum(['increment', 'decrement', 'set']),
    amount: z.number().optional(),
    value: z.number().min(0).max(1000).optional(),
    reason: z.string().min(1),
    evidence: z.string().optional(),
});
/**
 * Zod schema for TrustAdjustmentResult validation
 */
export const TrustAdjustmentResultSchema = z.object({
    previousScore: z.number().min(0).max(1000),
    newScore: z.number().min(0).max(1000),
    previousTier: TrustTierSchema,
    newTier: TrustTierSchema,
    tierChanged: z.boolean(),
});
/**
 * Zod schema for AttestationVerificationResult validation
 */
export const AttestationVerificationResultSchema = z.object({
    valid: z.boolean(),
    errors: z.array(z.string()),
    warnings: z.array(z.string()),
    issuerVerified: z.boolean(),
    signatureVerified: z.boolean(),
    notExpired: z.boolean(),
    notRevoked: z.boolean(),
});
/**
 * Zod schema for EnvironmentContext validation
 */
export const EnvironmentContextSchema = z.object({
    sourceIp: z.string().ip().optional(),
    geoLocation: z.string().optional(),
    timeOfDay: z.string().regex(/^\d{2}:\d{2}$/),
    dayOfWeek: z.string(),
    isBusinessHours: z.boolean(),
    custom: z.record(z.unknown()).optional(),
});
/**
 * Zod schema for PolicyContext validation
 */
export const PolicyContextSchema = z.object({
    agent: AgentIdentitySchema,
    action: ActionRequestSchema.optional(),
    capability: CapabilityRequestSchema.optional(),
    environment: EnvironmentContextSchema,
    timestamp: z.date(),
});
/**
 * Zod schema for PolicyEvidence validation
 */
export const PolicyEvidenceSchema = z.object({
    policyId: z.string().min(1),
    ruleId: z.string().min(1),
    details: z.string().min(1),
});
/**
 * Zod schema for PolicyObligation validation
 */
export const PolicyObligationSchema = z.object({
    type: z.enum(['log', 'notify', 'audit', 'custom']),
    params: z.record(z.unknown()),
});
/**
 * Zod schema for PolicyDecision validation
 */
export const PolicyDecisionSchema = z.object({
    decision: z.enum(['allow', 'deny', 'require_approval']),
    reasons: z.array(z.string()),
    evidence: z.array(PolicyEvidenceSchema).optional(),
    obligations: z.array(PolicyObligationSchema).optional(),
});
/**
 * Zod schema for PolicySource validation
 */
export const PolicySourceSchema = z.object({
    type: z.enum(['file', 'url', 'inline']),
    location: z.string().min(1),
    format: z.enum(['rego', 'json', 'yaml']),
});
/**
 * Zod schema for CARExtension validation (metadata only)
 */
export const CARExtensionMetadataSchema = z.object({
    extensionId: z.string().regex(/^car-ext-[a-z]+-v\d+$/),
    name: z.string().min(1),
    version: z.string().regex(/^\d+\.\d+\.\d+$/),
    shortcode: z.string().regex(/^[a-z]{1,10}$/),
    publisher: z.string().min(1),
    description: z.string().min(1),
    requiredCARVersion: z.string().min(1),
});
/**
 * Zod schema for ValidationResult
 */
export const ValidationResultSchema = z.object({
    valid: z.boolean(),
    errors: z.array(z.string()),
    warnings: z.array(z.string()),
});
/**
 * Zod schema for ExtensionServiceConfig
 */
export const ExtensionServiceConfigSchema = z.object({
    defaultTimeout: z.number().int().positive().optional(),
    failFast: z.boolean().optional(),
    logExecution: z.boolean().optional(),
    maxConcurrency: z.number().int().positive().optional(),
});
// =============================================================================
// EXTENSION HOOK TIMEOUTS (from spec)
// =============================================================================
/**
 * Default timeouts for different hook types (in milliseconds)
 */
export const HOOK_TIMEOUTS = {
    preCheck: { default: 100, max: 500 },
    postGrant: { default: 100, max: 500 },
    onExpiry: { default: 100, max: 500 },
    preAction: { default: 200, max: 1000 },
    postAction: { default: 500, max: 2000 },
    onFailure: { default: 200, max: 1000 },
    verifyBehavior: { default: 5000, max: 30000 },
    collectMetrics: { default: 5000, max: 30000 },
    onAnomaly: { default: 1000, max: 5000 },
    onRevocation: { default: 500, max: 2000 },
    adjustTrust: { default: 200, max: 1000 },
    verifyAttestation: { default: 500, max: 2000 },
    evaluate: { default: 500, max: 2000 },
};
//# sourceMappingURL=types.js.map