{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../src/core/governance/validation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKX,gBAAgB,EAIhB,MAAM,YAAY,CAAC;AAoJpB,wBAAgB,wBAAwB,CACvC,KAAK,EAAE,OAAO,EACd,SAAS,EAAE,MAAM,GACf;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,gBAAgB,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAoDlF","sourcesContent":["import type {\n\tGovernanceAction,\n\tGovernanceDecision,\n\tGovernanceEscalationRecord,\n\tGovernanceEvent,\n\tGovernanceLedger,\n\tGovernanceRetryClass,\n\tGovernanceScope,\n\tGovernanceStatus,\n} from \"./types.js\";\nimport { GOVERNANCE_DECISION_HISTORY_LIMIT } from \"./types.js\";\n\nconst RETRY_KEYS = [\n\t\"tool\",\n\t\"output_contract\",\n\t\"execution\",\n\t\"planner\",\n\t\"replan\",\n\t\"remote_execution\",\n\t\"provider\",\n] as const;\nconst USAGE_KEYS = [\n\t\"turns\",\n\t\"contextTokens\",\n\t\"generatedTokens\",\n\t\"toolCalls\",\n\t\"retries\",\n\t\"wallClockMs\",\n\t\"inferenceRequests\",\n\t\"children\",\n\t\"logicalAgents\",\n\t\"totalRetries\",\n\t\"replans\",\n\t\"fanOut\",\n\t\"depth\",\n\t\"readyChildren\",\n\t\"cloudSpendUsd\",\n\t\"modelEscalations\",\n] as const;\nconst ACTIONS: readonly GovernanceAction[] = [\n\t\"CONTINUE\",\n\t\"PARK\",\n\t\"THROTTLE\",\n\t\"DENY_NEW_CHILD\",\n\t\"DENY_INFERENCE\",\n\t\"REQUEST_REPLAN\",\n\t\"RETRY_OUTPUT_CONTRACT\",\n\t\"RETRY_TOOL\",\n\t\"RETRY_EXECUTION\",\n\t\"RETRY_PLANNER\",\n\t\"RETRY_REMOTE_EXECUTION\",\n\t\"ESCALATE_MODEL\",\n\t\"TERMINATE_CHILD\",\n\t\"TERMINATE_MISSION\",\n];\nconst STATUSES: readonly GovernanceStatus[] = [\"NORMAL\", \"NEAR_LIMIT\", \"LIMIT_REACHED\", \"UNKNOWN\"];\nconst SCOPES: readonly GovernanceScope[] = [\"child\", \"parent\", \"orchestration\", \"session\"];\nconst RETRY_CLASSES: readonly GovernanceRetryClass[] = [\n\t\"tool\",\n\t\"output_contract\",\n\t\"execution\",\n\t\"planner\",\n\t\"replan\",\n\t\"remote_execution\",\n\t\"provider\",\n];\n\nfunction record(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\nfunction nonNegativeFinite(value: unknown): value is number {\n\treturn typeof value === \"number\" && Number.isFinite(value) && value >= 0;\n}\nfunction count(value: unknown): value is number {\n\treturn nonNegativeFinite(value) && Number.isSafeInteger(value);\n}\nfunction nonEmptyString(value: unknown): value is string {\n\treturn typeof value === \"string\" && value.trim().length > 0;\n}\nfunction oneOf<T extends string>(value: unknown, values: readonly T[]): value is T {\n\treturn typeof value === \"string\" && values.includes(value as T);\n}\nfunction model(value: unknown): value is { provider: string; model: string } {\n\treturn record(value) && nonEmptyString(value.provider) && nonEmptyString(value.model);\n}\nfunction decision(value: unknown): value is GovernanceDecision {\n\tif (\n\t\t!record(value) ||\n\t\t!oneOf(value.action, ACTIONS) ||\n\t\t!oneOf(value.status, STATUSES) ||\n\t\t!nonEmptyString(value.reason)\n\t)\n\t\treturn false;\n\tif (!Array.isArray(value.evidence) || !count(value.atMs) || typeof value.preserveIdentity !== \"boolean\")\n\t\treturn false;\n\tif (value.from !== undefined && !model(value.from)) return false;\n\tif (value.to !== undefined && !model(value.to)) return false;\n\treturn value.evidence.every(\n\t\t(item) =>\n\t\t\trecord(item) &&\n\t\t\tnonEmptyString(item.code) &&\n\t\t\trecord(item.observed) &&\n\t\t\tObject.values(item.observed).every(\n\t\t\t\t(observed) =>\n\t\t\t\t\tobserved === undefined ||\n\t\t\t\t\ttypeof observed === \"string\" ||\n\t\t\t\t\ttypeof observed === \"number\" ||\n\t\t\t\t\ttypeof observed === \"boolean\",\n\t\t\t),\n\t);\n}\nfunction escalation(value: unknown): value is GovernanceEscalationRecord {\n\treturn (\n\t\trecord(value) &&\n\t\tnonEmptyString(value.escalationId) &&\n\t\tmodel(value.from) &&\n\t\tmodel(value.to) &&\n\t\tnonEmptyString(value.reason) &&\n\t\tnonEmptyString(value.missionId) &&\n\t\t(value.orchestrationId === undefined || nonEmptyString(value.orchestrationId)) &&\n\t\t(value.nodeId === undefined || nonEmptyString(value.nodeId)) &&\n\t\t(value.logicalAgentId === undefined || nonEmptyString(value.logicalAgentId)) &&\n\t\t(value.assignmentId === undefined || nonEmptyString(value.assignmentId)) &&\n\t\t(value.executionId === undefined || nonEmptyString(value.executionId)) &&\n\t\t(value.sessionId === undefined || nonEmptyString(value.sessionId)) &&\n\t\tcount(value.atMs)\n\t);\n}\nfunction event(value: unknown): value is GovernanceEvent {\n\tif (\n\t\t!record(value) ||\n\t\t!nonEmptyString(value.eventId) ||\n\t\t!oneOf(value.scope, SCOPES) ||\n\t\t!oneOf(value.kind, [\"consume\", \"retry\", \"decision\", \"escalation\", \"progress\"] as const) ||\n\t\t!count(value.atMs)\n\t)\n\t\treturn false;\n\tif (value.resource !== undefined && !USAGE_KEYS.includes(value.resource as (typeof USAGE_KEYS)[number]))\n\t\treturn false;\n\tif (value.amount !== undefined && !nonNegativeFinite(value.amount)) return false;\n\tif (value.retryClass !== undefined && !oneOf(value.retryClass, RETRY_CLASSES)) return false;\n\tif (value.costStatus !== undefined && !oneOf(value.costStatus, [\"KNOWN\", \"UNKNOWN\", \"NONE\"] as const)) return false;\n\tif (value.costUsd !== undefined && !nonNegativeFinite(value.costUsd)) return false;\n\tif (value.correlation !== undefined) {\n\t\tif (!record(value.correlation)) return false;\n\t\tfor (const key of [\"parentMissionId\", \"orchestrationId\", \"nodeId\", \"phase\"])\n\t\t\tif (value.correlation[key] !== undefined && !nonEmptyString(value.correlation[key])) return false;\n\t\tif (value.correlation.attempt !== undefined && !count(value.correlation.attempt)) return false;\n\t}\n\treturn (\n\t\t(value.provider === undefined || nonEmptyString(value.provider)) &&\n\t\t(value.model === undefined || nonEmptyString(value.model)) &&\n\t\t(value.childId === undefined || nonEmptyString(value.childId)) &&\n\t\t(value.reason === undefined || nonEmptyString(value.reason))\n\t);\n}\n\nexport function validateGovernanceLedger(\n\tvalue: unknown,\n\tmissionId: string,\n): { valid: true; ledger: GovernanceLedger } | { valid: false; diagnostic: string } {\n\tif (!record(value)) return { valid: false, diagnostic: \"ledger is not an object\" };\n\tconst ledger = value as Partial<GovernanceLedger>;\n\tif (ledger.schemaVersion !== 1 || ledger.missionId !== missionId || !count(ledger.revision))\n\t\treturn { valid: false, diagnostic: \"invalid identity or revision\" };\n\tif (ledger.parentMissionId !== undefined && !nonEmptyString(ledger.parentMissionId))\n\t\treturn { valid: false, diagnostic: \"invalid parent mission identity\" };\n\tif (!record(ledger.usage) || !record(ledger.retries) || !record(ledger.cost))\n\t\treturn { valid: false, diagnostic: \"missing ledger sections\" };\n\tif (\n\t\t!Array.isArray(ledger.events) ||\n\t\t!Array.isArray(ledger.escalationHistory) ||\n\t\t!Array.isArray(ledger.decisionHistory)\n\t)\n\t\treturn { valid: false, diagnostic: \"missing ledger history sections\" };\n\tfor (const key of USAGE_KEYS) {\n\t\tif (key === \"cloudSpendUsd\" ? !nonNegativeFinite(ledger.usage[key]) : !count(ledger.usage[key]))\n\t\t\treturn { valid: false, diagnostic: `invalid usage.${key}` };\n\t}\n\tfor (const key of RETRY_KEYS)\n\t\tif (!count(ledger.retries[key])) return { valid: false, diagnostic: `invalid retries.${key}` };\n\tif (\n\t\t!oneOf(ledger.cost.status, [\"KNOWN\", \"UNKNOWN\", \"NONE\"] as const) ||\n\t\t!nonNegativeFinite(ledger.cost.knownUsd) ||\n\t\t!count(ledger.cost.unknownPaidEvents) ||\n\t\t!count(ledger.cost.localInferenceRequests)\n\t)\n\t\treturn { valid: false, diagnostic: \"invalid cost accounting\" };\n\tif (ledger.decisionHistory.length > GOVERNANCE_DECISION_HISTORY_LIMIT)\n\t\treturn { valid: false, diagnostic: \"decision history exceeds bounded limit\" };\n\tif (ledger.lastDecision !== undefined && !decision(ledger.lastDecision))\n\t\treturn { valid: false, diagnostic: \"invalid last decision\" };\n\tfor (const item of ledger.decisionHistory)\n\t\tif (!decision(item)) return { valid: false, diagnostic: \"invalid decision history entry\" };\n\tif (ledger.lastDecision !== undefined && ledger.decisionHistory.length > 0) {\n\t\tconst latest = ledger.decisionHistory[ledger.decisionHistory.length - 1];\n\t\tif (JSON.stringify(latest) !== JSON.stringify(ledger.lastDecision))\n\t\t\treturn { valid: false, diagnostic: \"last decision does not match decision history\" };\n\t}\n\tconst ids = new Set<string>();\n\tfor (const item of ledger.events) {\n\t\tif (!event(item) || ids.has(item.eventId))\n\t\t\treturn { valid: false, diagnostic: \"invalid or duplicate governance event\" };\n\t\tids.add(item.eventId);\n\t}\n\tconst escalationIds = new Set<string>();\n\tfor (const item of ledger.escalationHistory) {\n\t\tif (!escalation(item) || item.missionId !== missionId || escalationIds.has(item.escalationId))\n\t\t\treturn { valid: false, diagnostic: \"invalid or duplicate escalation record\" };\n\t\tescalationIds.add(item.escalationId);\n\t}\n\treturn { valid: true, ledger: ledger as GovernanceLedger };\n}\n"]}