{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/core/governance/service.ts"],"names":[],"mappings":"AACA,OAAO,EAGN,mBAAmB,EAInB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAEX,wBAAwB,EACxB,uBAAuB,EACvB,qBAAqB,EACrB,kBAAkB,EAClB,0BAA0B,EAE1B,yBAAyB,EACzB,mCAAmC,EACnC,gBAAgB,EAChB,oBAAoB,EACpB,wBAAwB,EACxB,MAAM,YAAY,CAAC;AAkDpB;;;;GAIG;AACH,qBAAa,iBAAiB;IAC7B,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC;IAClC,YAAY,OAAO,EAAE;QAAE,KAAK,CAAC,EAAE,mBAAmB,CAAC;QAAC,MAAM,EAAE,gBAAgB,CAAA;KAAE,EAG7E;IACK,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAO9E;IACD,gEAAgE;IAC1D,cAAc,CAAC,KAAK,EAAE;QAC3B,SAAS,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;KACb,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAoCjD;IACD,iFAAiF;IAC3E,qBAAqB,CAAC,KAAK,EAAE;QAClC,SAAS,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;QACd,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;QACjC,IAAI,EAAE,MAAM,CAAC;KACb,GAAG,OAAO,CAAC,IAAI,CAAC,CAuDhB;IACK,WAAW,CAChB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,oBAAoB,EAChC,IAAI,EAAE,MAAM,EACZ,WAAW,CAAC,EAAE,qBAAqB,GACjC,OAAO,CAAC,IAAI,CAAC,CAQf;IACK,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAQpG;IACK,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,0BAA0B,GAAG,OAAO,CAAC,IAAI,CAAC,CAQ5G;IACK,qBAAqB,CAAC,KAAK,EAAE,yBAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAe3E;IACD,gFAAgF;IAC1E,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,wBAAwB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CA6CpG;IACK,wBAAwB,CAAC,KAAK,EAAE,mCAAmC,GAAG,OAAO,CAAC,uBAAuB,CAAC,CA2D3G;IACK,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,wBAAwB,GAAG,SAAS,CAAC,CA6B/E;CACD","sourcesContent":["import { effectiveBudget } from \"./evaluator.js\";\nimport {\n\tconsumeGovernanceInternal,\n\tcreateGovernanceLedger,\n\tFileGovernanceStore,\n\trecordGovernanceDecision,\n\trecordGovernanceEscalation,\n\trecordGovernanceRetry,\n} from \"./ledger.js\";\nimport type {\n\tGovernanceBudget,\n\tGovernanceConsumeRequest,\n\tGovernanceConsumeResult,\n\tGovernanceCorrelation,\n\tGovernanceDecision,\n\tGovernanceEscalationRecord,\n\tGovernanceLedger,\n\tGovernanceModelTransition,\n\tGovernanceOrchestrationChildRequest,\n\tGovernancePolicy,\n\tGovernanceRetryClass,\n\tGovernanceStatusSnapshot,\n} from \"./types.js\";\n\nconst RESOURCE_TO_LIMIT: Partial<Record<keyof GovernanceLedger[\"usage\"], keyof GovernanceBudget>> = {\n\tturns: \"maxTurns\",\n\tcontextTokens: \"maxContextTokens\",\n\tgeneratedTokens: \"maxGeneratedTokens\",\n\ttoolCalls: \"maxToolCalls\",\n\tretries: \"maxRetries\",\n\twallClockMs: \"maxWallClockMs\",\n\tinferenceRequests: \"maxInferenceRequests\",\n\tchildren: \"maxChildren\",\n\tlogicalAgents: \"maxLogicalAgents\",\n\ttotalRetries: \"maxTotalRetries\",\n\treplans: \"maxReplans\",\n\tfanOut: \"maxFanOut\",\n\tdepth: \"maxDepth\",\n\treadyChildren: \"maxReadyChildren\",\n\tcloudSpendUsd: \"maxCloudSpendUsd\",\n\tmodelEscalations: \"maxModelEscalations\",\n};\nconst COUNT_RESOURCES = new Set<keyof GovernanceLedger[\"usage\"]>([\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\"modelEscalations\",\n]);\n\nfunction hardLimitFor(\n\tpolicy: GovernancePolicy,\n\tresource: keyof GovernanceLedger[\"usage\"] | keyof GovernanceBudget,\n): number | undefined {\n\tconst field = RESOURCE_TO_LIMIT[resource as keyof GovernanceLedger[\"usage\"]];\n\tconst budgetField = field ?? (resource as keyof GovernanceBudget);\n\treturn effectiveBudget(policy)[budgetField];\n}\nfunction isLocalProvider(policy: GovernancePolicy, provider: string): boolean {\n\treturn provider === policy.localModel.provider || provider.startsWith(\"llamacpp-\");\n}\n\n/**\n * Durable Governance admission facade. It performs only policy arithmetic and\n * accounting. Callers perform the admitted action through their owning\n * authority (Scheduler, Worker, provider stream, or executor).\n */\nexport class GovernanceService {\n\treadonly store: FileGovernanceStore;\n\treadonly policy: GovernancePolicy;\n\tconstructor(options: { store?: FileGovernanceStore; policy: GovernancePolicy }) {\n\t\tthis.store = options.store ?? new FileGovernanceStore();\n\t\tthis.policy = options.policy;\n\t}\n\tasync ensureMission(missionId: string, parentMissionId?: string): Promise<void> {\n\t\tconst result = await this.store.create(createGovernanceLedger(missionId, parentMissionId));\n\t\tif (result !== \"conflict\") return;\n\t\tconst existing = await this.store.load(missionId);\n\t\tif (existing.status !== \"ok\") throw new Error(`GOVERNANCE_LEDGER_CONFLICT:${missionId}`);\n\t\tif (parentMissionId !== undefined && existing.ledger.parentMissionId !== parentMissionId)\n\t\t\tthrow new Error(`GOVERNANCE_PARENT_CONFLICT:${missionId}`);\n\t}\n\t/** Atomic admission for a single provider inference request. */\n\tasync admitInference(input: {\n\t\tmissionId: string;\n\t\teventId: string;\n\t\tprovider: string;\n\t\tmodel: string;\n\t\tatMs: number;\n\t}): Promise<{ allowed: boolean; reason?: string }> {\n\t\tawait this.ensureMission(input.missionId);\n\t\tconst local = isLocalProvider(this.policy, input.provider);\n\t\tif (!local && !this.policy.cloudAllowed) return { allowed: false, reason: \"CLOUD_PROHIBITED\" };\n\t\tconst result = await this.store.mutate<{ allowed: boolean; reason?: string }>(input.missionId, (ledger) => {\n\t\t\tconst limit = hardLimitFor(this.policy, \"inferenceRequests\");\n\t\t\tif (limit !== undefined && ledger.usage.inferenceRequests + 1 > limit)\n\t\t\t\treturn { kind: \"noop\", value: { allowed: false, reason: \"INFERENCE_REQUESTS_HARD_LIMIT\" } };\n\t\t\tconst spendLimit = hardLimitFor(this.policy, \"cloudSpendUsd\");\n\t\t\tif (\n\t\t\t\t!local &&\n\t\t\t\tspendLimit !== undefined &&\n\t\t\t\t(ledger.cost.unknownPaidEvents > 0 || ledger.cost.knownUsd >= spendLimit)\n\t\t\t)\n\t\t\t\treturn {\n\t\t\t\t\tkind: \"noop\",\n\t\t\t\t\tvalue: {\n\t\t\t\t\t\tallowed: false,\n\t\t\t\t\t\treason: ledger.cost.unknownPaidEvents > 0 ? \"UNKNOWN_CLOUD_COST\" : \"CLOUD_COST_HARD_LIMIT\",\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\tconst accounted = consumeGovernanceInternal(ledger, {\n\t\t\t\teventId: input.eventId,\n\t\t\t\tscope: \"session\",\n\t\t\t\tresource: \"inferenceRequests\",\n\t\t\t\tamount: 1,\n\t\t\t\tatMs: input.atMs,\n\t\t\t\tprovider: input.provider,\n\t\t\t\tmodel: input.model,\n\t\t\t\tcostStatus: local ? \"NONE\" : undefined,\n\t\t\t});\n\t\t\treturn { kind: \"write\", ledger: accounted.ledger, value: { allowed: true } };\n\t\t});\n\t\tif (result.status !== \"ok\")\n\t\t\tthrow new Error(`GOVERNANCE_LEDGER_${result.status.toUpperCase()}:${input.missionId}`);\n\t\treturn result.value;\n\t}\n\t/** Record authoritative provider result telemetry after the provider returns. */\n\tasync recordInferenceResult(input: {\n\t\tmissionId: string;\n\t\teventId: string;\n\t\tprovider: string;\n\t\tmodel: string;\n\t\tinputTokens?: number;\n\t\toutputTokens?: number;\n\t\tcostUsd?: number;\n\t\tcostStatus?: \"KNOWN\" | \"UNKNOWN\";\n\t\tatMs: number;\n\t}): Promise<void> {\n\t\tawait this.ensureMission(input.missionId);\n\t\tconst local = isLocalProvider(this.policy, input.provider);\n\t\tawait this.store.mutate(input.missionId, (ledger) => {\n\t\t\tif (ledger.events.some((event) => event.eventId === input.eventId)) return { kind: \"noop\", value: undefined };\n\t\t\tconst next = JSON.parse(JSON.stringify(ledger)) as GovernanceLedger;\n\t\t\tif (input.inputTokens !== undefined && (!Number.isSafeInteger(input.inputTokens) || input.inputTokens < 0))\n\t\t\t\tthrow new Error(\"INVALID_INPUT_TOKEN_USAGE\");\n\t\t\tif (input.outputTokens !== undefined && (!Number.isSafeInteger(input.outputTokens) || input.outputTokens < 0))\n\t\t\t\tthrow new Error(\"INVALID_OUTPUT_TOKEN_USAGE\");\n\t\t\tif (input.inputTokens !== undefined) next.usage.contextTokens += input.inputTokens;\n\t\t\tif (input.outputTokens !== undefined) next.usage.generatedTokens += input.outputTokens;\n\t\t\tif (\n\t\t\t\tinput.inputTokens !== undefined &&\n\t\t\t\thardLimitFor(this.policy, \"maxContextTokens\") !== undefined &&\n\t\t\t\tnext.usage.contextTokens > hardLimitFor(this.policy, \"maxContextTokens\")!\n\t\t\t)\n\t\t\t\tthrow new Error(\"INPUT_TOKEN_HARD_LIMIT\");\n\t\t\tif (\n\t\t\t\tinput.outputTokens !== undefined &&\n\t\t\t\thardLimitFor(this.policy, \"maxGeneratedTokens\") !== undefined &&\n\t\t\t\tnext.usage.generatedTokens > hardLimitFor(this.policy, \"maxGeneratedTokens\")!\n\t\t\t)\n\t\t\t\tthrow new Error(\"GENERATED_TOKEN_HARD_LIMIT\");\n\t\t\tif (!local && input.costStatus === undefined && input.costUsd === undefined)\n\t\t\t\tthrow new Error(\"UNKNOWN_CLOUD_COST\");\n\t\t\tif (local && input.costUsd !== undefined) throw new Error(\"LOCAL_COST_MUST_BE_NONE\");\n\t\t\tif (!local && input.costUsd !== undefined && Number.isFinite(input.costUsd) && input.costUsd >= 0) {\n\t\t\t\tconst spendLimit = hardLimitFor(this.policy, \"cloudSpendUsd\");\n\t\t\t\tif (spendLimit !== undefined && next.cost.knownUsd + input.costUsd > spendLimit)\n\t\t\t\t\tthrow new Error(\"CLOUD_COST_HARD_LIMIT\");\n\t\t\t\tnext.cost.knownUsd += input.costUsd;\n\t\t\t\tnext.usage.cloudSpendUsd += input.costUsd;\n\t\t\t\tnext.cost.status = \"KNOWN\";\n\t\t\t} else if (!local && input.costStatus === \"UNKNOWN\") {\n\t\t\t\tnext.cost.unknownPaidEvents += 1;\n\t\t\t\tnext.cost.status = \"UNKNOWN\";\n\t\t\t} else if (local) {\n\t\t\t\tnext.cost.status = next.cost.unknownPaidEvents > 0 ? \"UNKNOWN\" : \"NONE\";\n\t\t\t}\n\t\t\tnext.events.push({\n\t\t\t\teventId: input.eventId,\n\t\t\t\tscope: \"session\",\n\t\t\t\tkind: \"consume\",\n\t\t\t\tresource: \"generatedTokens\",\n\t\t\t\tamount: input.outputTokens ?? 0,\n\t\t\t\tprovider: input.provider,\n\t\t\t\tmodel: input.model,\n\t\t\t\tcostStatus: input.costStatus,\n\t\t\t\tcostUsd: input.costUsd,\n\t\t\t\tatMs: input.atMs,\n\t\t\t});\n\t\t\tnext.revision += 1;\n\t\t\treturn { kind: \"write\", ledger: next, value: undefined };\n\t\t});\n\t}\n\tasync recordRetry(\n\t\tmissionId: string,\n\t\teventId: string,\n\t\tretryClass: GovernanceRetryClass,\n\t\tatMs: number,\n\t\tcorrelation?: GovernanceCorrelation,\n\t): Promise<void> {\n\t\tawait this.ensureMission(missionId);\n\t\tconst result = await this.store.mutate(missionId, (ledger) => ({\n\t\t\tkind: \"write\" as const,\n\t\t\tledger: recordGovernanceRetry(ledger, eventId, retryClass, atMs, correlation),\n\t\t\tvalue: undefined,\n\t\t}));\n\t\tif (result.status !== \"ok\") throw new Error(`GOVERNANCE_LEDGER_${result.status.toUpperCase()}:${missionId}`);\n\t}\n\tasync recordDecision(missionId: string, eventId: string, decision: GovernanceDecision): Promise<void> {\n\t\tawait this.ensureMission(missionId);\n\t\tconst result = await this.store.mutate(missionId, (ledger) => ({\n\t\t\tkind: \"write\" as const,\n\t\t\tledger: recordGovernanceDecision(ledger, eventId, decision),\n\t\t\tvalue: undefined,\n\t\t}));\n\t\tif (result.status !== \"ok\") throw new Error(`GOVERNANCE_LEDGER_${result.status.toUpperCase()}:${missionId}`);\n\t}\n\tasync recordEscalation(missionId: string, eventId: string, record: GovernanceEscalationRecord): Promise<void> {\n\t\tawait this.ensureMission(missionId);\n\t\tconst result = await this.store.mutate(missionId, (ledger) => ({\n\t\t\tkind: \"write\" as const,\n\t\t\tledger: recordGovernanceEscalation(ledger, eventId, record),\n\t\t\tvalue: undefined,\n\t\t}));\n\t\tif (result.status !== \"ok\") throw new Error(`GOVERNANCE_LEDGER_${result.status.toUpperCase()}:${missionId}`);\n\t}\n\tasync recordModelTransition(input: GovernanceModelTransition): Promise<void> {\n\t\tawait this.recordEscalation(input.missionId, input.eventId, {\n\t\t\tescalationId: input.eventId,\n\t\t\tfrom: input.from,\n\t\t\tto: input.to,\n\t\t\treason: input.reason,\n\t\t\tmissionId: input.missionId,\n\t\t\torchestrationId: input.orchestrationId,\n\t\t\tnodeId: input.nodeId,\n\t\t\tlogicalAgentId: input.logicalAgentId,\n\t\t\tassignmentId: input.assignmentId,\n\t\t\texecutionId: input.executionId,\n\t\t\tsessionId: input.sessionId,\n\t\t\tatMs: input.atMs,\n\t\t});\n\t}\n\t/** Record a policy-governed non-inference unit (child, retry, replan, etc.). */\n\tasync consume(missionId: string, request: GovernanceConsumeRequest): Promise<GovernanceConsumeResult> {\n\t\tawait this.ensureMission(missionId);\n\t\tif (request.provider && !isLocalProvider(this.policy, request.provider) && !this.policy.cloudAllowed)\n\t\t\treturn { allowed: false, reason: \"CLOUD_PROHIBITED\", ledger: createGovernanceLedger(missionId) };\n\t\tif (COUNT_RESOURCES.has(request.resource) && (!Number.isSafeInteger(request.amount) || request.amount <= 0))\n\t\t\treturn {\n\t\t\t\tallowed: false,\n\t\t\t\treason: \"COUNT_AMOUNT_MUST_BE_POSITIVE_INTEGER\",\n\t\t\t\tledger: createGovernanceLedger(missionId),\n\t\t\t};\n\t\tif (request.resource === \"cloudSpendUsd\" && (!Number.isFinite(request.amount) || request.amount <= 0))\n\t\t\treturn { allowed: false, reason: \"COST_AMOUNT_MUST_BE_POSITIVE\", ledger: createGovernanceLedger(missionId) };\n\t\tif (request.resource === \"cloudSpendUsd\" && request.costUsd !== undefined)\n\t\t\treturn {\n\t\t\t\tallowed: false,\n\t\t\t\treason: \"COST_MUST_USE_ONE_AUTHORITATIVE_AMOUNT\",\n\t\t\t\tledger: createGovernanceLedger(missionId),\n\t\t\t};\n\t\tif (request.costUsd !== undefined && (!Number.isFinite(request.costUsd) || request.costUsd < 0))\n\t\t\treturn { allowed: false, reason: \"INVALID_COST\", ledger: createGovernanceLedger(missionId) };\n\t\tlet outcome: GovernanceConsumeResult = {\n\t\t\tallowed: false,\n\t\t\treason: \"UNSET\",\n\t\t\tledger: createGovernanceLedger(missionId),\n\t\t};\n\t\tconst result = await this.store.mutate(missionId, (ledger) => {\n\t\t\tconst limit = hardLimitFor(this.policy, request.resource);\n\t\t\tif (limit !== undefined && ledger.usage[request.resource] + request.amount > limit) {\n\t\t\t\toutcome = { allowed: false, reason: `${String(request.resource).toUpperCase()}_HARD_LIMIT`, ledger };\n\t\t\t\treturn { kind: \"noop\", value: outcome };\n\t\t\t}\n\t\t\tif (\n\t\t\t\trequest.costStatus === \"UNKNOWN\" &&\n\t\t\t\trequest.provider &&\n\t\t\t\t!isLocalProvider(this.policy, request.provider) &&\n\t\t\t\thardLimitFor(this.policy, \"cloudSpendUsd\") !== undefined\n\t\t\t) {\n\t\t\t\toutcome = { allowed: false, reason: \"UNKNOWN_CLOUD_COST\", ledger };\n\t\t\t\treturn { kind: \"noop\", value: outcome };\n\t\t\t}\n\t\t\toutcome = consumeGovernanceInternal(ledger, request);\n\t\t\treturn { kind: \"write\", ledger: outcome.ledger, value: outcome };\n\t\t});\n\t\tif (result.status !== \"ok\") throw new Error(`GOVERNANCE_LEDGER_${result.status.toUpperCase()}:${missionId}`);\n\t\treturn outcome;\n\t}\n\tasync recordOrchestrationChild(input: GovernanceOrchestrationChildRequest): Promise<GovernanceConsumeResult> {\n\t\tawait this.ensureMission(input.missionId);\n\t\tconst requests: GovernanceConsumeRequest[] = [\n\t\t\t{\n\t\t\t\teventId: input.eventId,\n\t\t\t\tscope: \"orchestration\",\n\t\t\t\tresource: \"children\",\n\t\t\t\tamount: 1,\n\t\t\t\tatMs: input.atMs,\n\t\t\t\tchildId: input.childId,\n\t\t\t\tcorrelation: input.correlation,\n\t\t\t},\n\t\t\t{\n\t\t\t\teventId: `${input.eventId}:fanout`,\n\t\t\t\tscope: \"orchestration\",\n\t\t\t\tresource: \"fanOut\",\n\t\t\t\tamount: 1,\n\t\t\t\tatMs: input.atMs,\n\t\t\t\tchildId: input.childId,\n\t\t\t\tcorrelation: input.correlation,\n\t\t\t},\n\t\t\t{\n\t\t\t\teventId: `${input.eventId}:depth`,\n\t\t\t\tscope: \"orchestration\",\n\t\t\t\tresource: \"depth\",\n\t\t\t\tamount: input.orchestrationDepth,\n\t\t\t\tatMs: input.atMs,\n\t\t\t\tchildId: input.childId,\n\t\t\t\tcorrelation: input.correlation,\n\t\t\t},\n\t\t];\n\t\tlet outcome: GovernanceConsumeResult = {\n\t\t\tallowed: false,\n\t\t\treason: \"UNSET\",\n\t\t\tledger: createGovernanceLedger(input.missionId),\n\t\t};\n\t\tconst result = await this.store.mutate(input.missionId, (ledger) => {\n\t\t\tif (requests.every((request) => ledger.events.some((event) => event.eventId === request.eventId)))\n\t\t\t\treturn { kind: \"noop\" as const, value: { allowed: true, ledger } };\n\t\t\tlet next = ledger;\n\t\t\tfor (const request of requests) {\n\t\t\t\tconst limit = hardLimitFor(this.policy, request.resource);\n\t\t\t\tif (limit !== undefined && next.usage[request.resource] + request.amount > limit) {\n\t\t\t\t\toutcome = { allowed: false, reason: `${String(request.resource).toUpperCase()}_HARD_LIMIT`, ledger };\n\t\t\t\t\treturn { kind: \"noop\" as const, value: outcome };\n\t\t\t\t}\n\t\t\t\tconst accounted = consumeGovernanceInternal(next, request);\n\t\t\t\tif (!accounted.allowed) {\n\t\t\t\t\toutcome = { allowed: false, reason: accounted.reason, ledger };\n\t\t\t\t\treturn { kind: \"noop\" as const, value: outcome };\n\t\t\t\t}\n\t\t\t\tnext = accounted.ledger;\n\t\t\t}\n\t\t\toutcome = { allowed: true, ledger: next };\n\t\t\treturn { kind: \"write\" as const, ledger: next, value: outcome };\n\t\t});\n\t\tif (result.status !== \"ok\")\n\t\t\tthrow new Error(`GOVERNANCE_LEDGER_${result.status.toUpperCase()}:${input.missionId}`);\n\t\treturn result.value;\n\t}\n\tasync snapshot(missionId: string): Promise<GovernanceStatusSnapshot | undefined> {\n\t\tconst loaded = await this.store.load(missionId);\n\t\tif (loaded.status !== \"ok\") return undefined;\n\t\tconst budget = effectiveBudget(this.policy);\n\t\tconst remaining: Partial<GovernanceLedger[\"usage\"]> = {};\n\t\tfor (const [resource, field] of Object.entries(RESOURCE_TO_LIMIT) as Array<\n\t\t\t[keyof GovernanceLedger[\"usage\"], keyof GovernanceBudget]\n\t\t>) {\n\t\t\tconst limit = budget[field];\n\t\t\tif (limit !== undefined) remaining[resource] = Math.max(0, limit - loaded.ledger.usage[resource]);\n\t\t}\n\t\tconst status =\n\t\t\tloaded.ledger.cost.status === \"UNKNOWN\"\n\t\t\t\t? \"UNKNOWN\"\n\t\t\t\t: Object.values(remaining).some((value) => value === 0)\n\t\t\t\t\t? \"LIMIT_REACHED\"\n\t\t\t\t\t: \"NORMAL\";\n\t\treturn {\n\t\t\tmissionId,\n\t\t\tparentMissionId: loaded.ledger.parentMissionId,\n\t\t\trevision: loaded.ledger.revision,\n\t\t\tstatus,\n\t\t\tusage: loaded.ledger.usage,\n\t\t\tremaining,\n\t\t\tretries: loaded.ledger.retries,\n\t\t\tcost: loaded.ledger.cost,\n\t\t\tescalations: loaded.ledger.escalationHistory.length,\n\t\t\tlastDecision: loaded.ledger.lastDecision,\n\t\t};\n\t}\n}\n"]}