{"version":3,"file":"observability.mjs","names":["identifier","errorCode","Observability","#options","options","observation","activation","result","execute","error","started","#now","#emit","#duration","cause","option"],"sources":["../../../../../src/components/foundation/modules/observability/observability.ts"],"sourcesContent":["export type CommandObservation = Readonly<{\n\toperation: string\n\tclassification: string\n\toutcome: 'completed' | 'denied' | 'failed'\n\terrorCode?: string\n\tdurationMs: number\n}>\n\nexport type ObservabilityOptions = Readonly<{\n\tenabled?: boolean | (() => boolean)\n\tclassifyError: (cause: unknown) => Readonly<{\n\t\toutcome: 'denied' | 'failed'\n\t\terrorCode: string\n\t}>\n\temit?: (observation: CommandObservation) => void\n\tclock?: () => number\n}>\n\nconst identifier = /^[A-Za-z][A-Za-z0-9_.-]{0,159}$/\nconst errorCode = /^[A-Z][A-Z0-9_]{0,95}$/\n\n/** Payload-free command telemetry that cannot change command behavior. */\nexport class Observability {\n\treadonly #options: ObservabilityOptions\n\n\tconstructor(options: ObservabilityOptions) {\n\t\tthis.#options = options\n\t}\n\n\tasync observe<Result>(\n\t\tactivation: Readonly<{ operation: string; classification: string }>,\n\t\texecute: () => Result | Promise<Result>,\n\t): Promise<Result> {\n\t\tconst observation = this.start(activation)\n\t\ttry {\n\t\t\tconst result = await execute()\n\t\t\tobservation.completed()\n\t\t\treturn result\n\t\t} catch (error) {\n\t\t\tobservation.failed(error)\n\t\t\tthrow error\n\t\t}\n\t}\n\n\t/** Inert synchronous exit hooks for execution interpreters that own their runtime. */\n\tstart(activation: Readonly<{ operation: string; classification: string }>) {\n\t\tconst started = this.#now()\n\t\treturn {\n\t\t\tcompleted: () =>\n\t\t\t\tthis.#emit({\n\t\t\t\t\t...activation,\n\t\t\t\t\toutcome: 'completed',\n\t\t\t\t\tdurationMs: this.#duration(started),\n\t\t\t\t}),\n\t\t\tfailed: (cause: unknown) => {\n\t\t\t\ttry {\n\t\t\t\t\tthis.#emit({\n\t\t\t\t\t\t...activation,\n\t\t\t\t\t\t...this.#options.classifyError(cause),\n\t\t\t\t\t\tdurationMs: this.#duration(started),\n\t\t\t\t\t})\n\t\t\t\t} catch {\n\t\t\t\t\t// Preserve the original command failure when telemetry fails.\n\t\t\t\t}\n\t\t\t},\n\t\t}\n\t}\n\n\t#emit(observation: CommandObservation): void {\n\t\ttry {\n\t\t\tconst option = this.#options.enabled\n\t\t\tconst enabled =\n\t\t\t\toption === undefined || option === true || option === false ? option : option()\n\t\t\tif (\n\t\t\t\tenabled !== true ||\n\t\t\t\t!identifier.test(observation.operation) ||\n\t\t\t\t!identifier.test(observation.classification) ||\n\t\t\t\t(observation.errorCode !== undefined && !errorCode.test(observation.errorCode))\n\t\t\t) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif (this.#options.emit) {\n\t\t\t\tthis.#options.emit(Object.freeze(observation))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconsole.info(JSON.stringify({ event: 'command.execution', ...observation }))\n\t\t} catch {\n\t\t\t// Observability is intentionally behaviorally inert.\n\t\t}\n\t}\n\n\t#now(): number {\n\t\ttry {\n\t\t\treturn this.#options.clock?.() ?? Date.now()\n\t\t} catch {\n\t\t\treturn Date.now()\n\t\t}\n\t}\n\n\t#duration(started: number): number {\n\t\treturn Math.max(0, Math.min(600_000, this.#now() - started))\n\t}\n}\n"],"mappings":"AAkBA,MAAMA,EAAa,kCACbC,EAAY,yBAGlB,IAAaC,EAAb,KAA2B,CAC1B,GAEA,YAAY,EAA+B,CAC1C,KAAKC,GAAWC,CACjB,CAEA,MAAM,QACL,EACA,EACkB,CAClB,IAAMC,EAAc,KAAK,MAAMC,CAAU,EACzC,GAAI,CACH,IAAMC,EAAS,MAAMC,EAAQ,EAE7B,OADAH,EAAY,UAAU,EACfE,CACR,OAASE,EAAO,CAEf,MADAJ,EAAY,OAAOI,CAAK,EAClBA,CACP,CACD,CAGA,MAAM,EAAqE,CAC1E,IAAMC,EAAU,KAAKC,GAAK,EAC1B,MAAO,CACN,cACC,KAAKC,GAAM,CACV,GAAGN,EACH,QAAS,YACT,WAAY,KAAKO,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