@graphrefly/graphrefly 0.47.0 → 0.47.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base/composition/index.cjs +24 -16
- package/dist/base/composition/index.cjs.map +1 -1
- package/dist/base/composition/index.js +6 -6
- package/dist/base/index.cjs +142 -86
- package/dist/base/index.cjs.map +1 -1
- package/dist/base/index.js +11 -11
- package/dist/base/io/index.cjs +114 -68
- package/dist/base/io/index.cjs.map +1 -1
- package/dist/base/io/index.js +5 -5
- package/dist/base/sources/browser/index.cjs +13 -9
- package/dist/base/sources/browser/index.cjs.map +1 -1
- package/dist/base/sources/browser/index.js +13 -9
- package/dist/base/sources/browser/index.js.map +1 -1
- package/dist/base/sources/event/index.cjs +1 -1
- package/dist/base/sources/event/index.cjs.map +1 -1
- package/dist/base/sources/event/index.js +1 -1
- package/dist/base/sources/index.cjs +21 -13
- package/dist/base/sources/index.cjs.map +1 -1
- package/dist/base/sources/index.js +3 -3
- package/dist/base/sources/node/index.cjs +43 -37
- package/dist/base/sources/node/index.cjs.map +1 -1
- package/dist/base/sources/node/index.js +43 -37
- package/dist/base/sources/node/index.js.map +1 -1
- package/dist/{chunk-VLAGJZSL.js → chunk-3O3NKZJW.js} +2 -2
- package/dist/{chunk-YJ4U2D2C.js → chunk-446I4EGD.js} +9 -7
- package/dist/chunk-446I4EGD.js.map +1 -0
- package/dist/{chunk-DKNHAICT.js → chunk-5GVURVIG.js} +14 -8
- package/dist/chunk-5GVURVIG.js.map +1 -0
- package/dist/{chunk-2OB3CEJS.js → chunk-6MRSX3YK.js} +2 -2
- package/dist/{chunk-EVYY4X5A.js → chunk-6ZLCPUXS.js} +2 -2
- package/dist/{chunk-ZVXXDWIB.js → chunk-7AVQIGF6.js} +514 -33
- package/dist/chunk-7AVQIGF6.js.map +1 -0
- package/dist/{chunk-7EGRP2VX.js → chunk-7BULJTL6.js} +2 -2
- package/dist/{chunk-7EGRP2VX.js.map → chunk-7BULJTL6.js.map} +1 -1
- package/dist/{chunk-FW23JYNQ.js → chunk-CEVNQ74M.js} +2 -2
- package/dist/{chunk-CGHORL6G.js → chunk-DDTS7F5O.js} +7 -5
- package/dist/chunk-DDTS7F5O.js.map +1 -0
- package/dist/{chunk-OCUDSN63.js → chunk-EL5VHUGK.js} +79 -47
- package/dist/chunk-EL5VHUGK.js.map +1 -0
- package/dist/{chunk-4GYMCUDZ.js → chunk-EP4WVQLX.js} +5 -5
- package/dist/{chunk-SOOKUYVM.js → chunk-F7EKHR32.js} +13 -9
- package/dist/chunk-F7EKHR32.js.map +1 -0
- package/dist/{chunk-JKTC747G.js → chunk-FQSQONOU.js} +4 -4
- package/dist/{chunk-JGFRAFDL.js → chunk-FVINAAKA.js} +3 -3
- package/dist/{chunk-RAGGHLCV.js → chunk-GUNIRPEJ.js} +8 -6
- package/dist/{chunk-RAGGHLCV.js.map → chunk-GUNIRPEJ.js.map} +1 -1
- package/dist/{chunk-BU3SEFA5.js → chunk-IOJDYUA7.js} +2 -2
- package/dist/{chunk-Y52CS6YA.js → chunk-JA67ZQG2.js} +2 -2
- package/dist/{chunk-Y52CS6YA.js.map → chunk-JA67ZQG2.js.map} +1 -1
- package/dist/{chunk-DM4OMPWK.js → chunk-KNU73RZW.js} +2 -2
- package/dist/{chunk-GWRNLJNW.js → chunk-KRFGO5QH.js} +19 -15
- package/dist/{chunk-GWRNLJNW.js.map → chunk-KRFGO5QH.js.map} +1 -1
- package/dist/{chunk-Z4YXAUDN.js → chunk-KUFXLAEY.js} +11 -7
- package/dist/{chunk-Z4YXAUDN.js.map → chunk-KUFXLAEY.js.map} +1 -1
- package/dist/{chunk-Z6EGP5D7.js → chunk-LDCSZ72P.js} +2 -2
- package/dist/{chunk-5IMMNARC.js → chunk-MS3WPRJR.js} +37 -25
- package/dist/chunk-MS3WPRJR.js.map +1 -0
- package/dist/{chunk-CXANAIZU.js → chunk-N65E26UL.js} +3 -3
- package/dist/{chunk-O3MT7DYI.js → chunk-N6MNJNHB.js} +2 -2
- package/dist/{chunk-Q3EYOCZB.js → chunk-NPRP3MCV.js} +111 -2
- package/dist/chunk-NPRP3MCV.js.map +1 -0
- package/dist/{chunk-A7KV5UK4.js → chunk-OXD5LFQP.js} +2 -2
- package/dist/{chunk-ZT4WMQW4.js → chunk-PTWADEH3.js} +9 -7
- package/dist/chunk-PTWADEH3.js.map +1 -0
- package/dist/{chunk-IHTWQEDR.js → chunk-QFE5BQH7.js} +2 -2
- package/dist/{chunk-IHTWQEDR.js.map → chunk-QFE5BQH7.js.map} +1 -1
- package/dist/{chunk-22SG74BD.js → chunk-R6ZCSXKX.js} +3 -3
- package/dist/{chunk-PZWISPIQ.js → chunk-S7HN5FHL.js} +17 -11
- package/dist/chunk-S7HN5FHL.js.map +1 -0
- package/dist/{chunk-RJOG4IJU.js → chunk-T7SP3EYR.js} +18 -12
- package/dist/chunk-T7SP3EYR.js.map +1 -0
- package/dist/{chunk-4S53H2KR.js → chunk-VAZXUK6G.js} +2 -2
- package/dist/{chunk-IJRR6YAI.js → chunk-VLDRAMP7.js} +18 -12
- package/dist/chunk-VLDRAMP7.js.map +1 -0
- package/dist/{chunk-TNX5ZGDJ.js → chunk-VNXAF2KE.js} +4 -4
- package/dist/{chunk-EHRRQ4IC.js → chunk-VP3TIUDF.js} +2 -2
- package/dist/{chunk-6XZYT4SW.js → chunk-WGDEBIP4.js} +5 -5
- package/dist/{chunk-E5OZPDIW.js → chunk-X7BA5PWG.js} +7 -5
- package/dist/chunk-X7BA5PWG.js.map +1 -0
- package/dist/compat/index.cjs +1 -1
- package/dist/compat/index.cjs.map +1 -1
- package/dist/compat/index.js +2 -2
- package/dist/compat/nestjs/index.cjs +1 -1
- package/dist/compat/nestjs/index.cjs.map +1 -1
- package/dist/compat/nestjs/index.js +2 -2
- package/dist/index.cjs +1657 -982
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +58 -36
- package/dist/index.js.map +1 -1
- package/dist/presets/ai/index.cjs +42 -26
- package/dist/presets/ai/index.cjs.map +1 -1
- package/dist/presets/ai/index.js +11 -11
- package/dist/presets/harness/index.cjs +53 -33
- package/dist/presets/harness/index.cjs.map +1 -1
- package/dist/presets/harness/index.js +22 -22
- package/dist/presets/index.cjs +76 -48
- package/dist/presets/index.cjs.map +1 -1
- package/dist/presets/index.js +28 -28
- package/dist/presets/inspect/index.cjs.map +1 -1
- package/dist/presets/inspect/index.js +4 -4
- package/dist/presets/resilience/index.cjs +35 -23
- package/dist/presets/resilience/index.cjs.map +1 -1
- package/dist/presets/resilience/index.js +5 -5
- package/dist/solutions/index.cjs +71 -45
- package/dist/solutions/index.cjs.map +1 -1
- package/dist/solutions/index.js +24 -24
- package/dist/{timeout-U5O4ESK3.js → timeout-BEABACRP.js} +2 -2
- package/dist/utils/ai/browser.cjs.map +1 -1
- package/dist/utils/ai/browser.js +9 -9
- package/dist/utils/ai/index.cjs +41 -25
- package/dist/utils/ai/index.cjs.map +1 -1
- package/dist/utils/ai/index.js +18 -18
- package/dist/utils/ai/node.js +3 -3
- package/dist/utils/domain-templates/index.cjs +1 -1
- package/dist/utils/domain-templates/index.cjs.map +1 -1
- package/dist/utils/domain-templates/index.js +2 -2
- package/dist/utils/graphspec/index.cjs +1 -1
- package/dist/utils/graphspec/index.cjs.map +1 -1
- package/dist/utils/graphspec/index.js +2 -2
- package/dist/utils/harness/index.cjs +16 -10
- package/dist/utils/harness/index.cjs.map +1 -1
- package/dist/utils/harness/index.js +1 -1
- package/dist/utils/index.cjs +1069 -452
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts +2 -2
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +45 -23
- package/dist/utils/inspect/index.cjs.map +1 -1
- package/dist/utils/inspect/index.js +2 -2
- package/dist/utils/memory/index.cjs +513 -37
- package/dist/utils/memory/index.cjs.map +1 -1
- package/dist/utils/memory/index.d.cts +641 -3
- package/dist/utils/memory/index.d.ts +641 -3
- package/dist/utils/memory/index.js +19 -1
- package/dist/utils/messaging/index.cjs +109 -0
- package/dist/utils/messaging/index.cjs.map +1 -1
- package/dist/utils/messaging/index.d.cts +115 -2
- package/dist/utils/messaging/index.d.ts +115 -2
- package/dist/utils/messaging/index.js +5 -1
- package/dist/utils/orchestration/index.cjs +5 -3
- package/dist/utils/orchestration/index.cjs.map +1 -1
- package/dist/utils/orchestration/index.js +2 -2
- package/dist/utils/process/index.js +2 -2
- package/dist/utils/reduction/index.cjs +1 -1
- package/dist/utils/reduction/index.cjs.map +1 -1
- package/dist/utils/reduction/index.js +1 -1
- package/dist/utils/resilience/index.cjs +35 -23
- package/dist/utils/resilience/index.cjs.map +1 -1
- package/dist/utils/resilience/index.js +4 -4
- package/dist/utils/surface/index.cjs +1 -1
- package/dist/utils/surface/index.cjs.map +1 -1
- package/dist/utils/surface/index.js +3 -3
- package/package.json +1 -1
- package/dist/chunk-5IMMNARC.js.map +0 -1
- package/dist/chunk-CGHORL6G.js.map +0 -1
- package/dist/chunk-DKNHAICT.js.map +0 -1
- package/dist/chunk-E5OZPDIW.js.map +0 -1
- package/dist/chunk-IJRR6YAI.js.map +0 -1
- package/dist/chunk-OCUDSN63.js.map +0 -1
- package/dist/chunk-PZWISPIQ.js.map +0 -1
- package/dist/chunk-Q3EYOCZB.js.map +0 -1
- package/dist/chunk-RJOG4IJU.js.map +0 -1
- package/dist/chunk-SOOKUYVM.js.map +0 -1
- package/dist/chunk-YJ4U2D2C.js.map +0 -1
- package/dist/chunk-ZT4WMQW4.js.map +0 -1
- package/dist/chunk-ZVXXDWIB.js.map +0 -1
- /package/dist/{chunk-VLAGJZSL.js.map → chunk-3O3NKZJW.js.map} +0 -0
- /package/dist/{chunk-2OB3CEJS.js.map → chunk-6MRSX3YK.js.map} +0 -0
- /package/dist/{chunk-EVYY4X5A.js.map → chunk-6ZLCPUXS.js.map} +0 -0
- /package/dist/{chunk-FW23JYNQ.js.map → chunk-CEVNQ74M.js.map} +0 -0
- /package/dist/{chunk-4GYMCUDZ.js.map → chunk-EP4WVQLX.js.map} +0 -0
- /package/dist/{chunk-JKTC747G.js.map → chunk-FQSQONOU.js.map} +0 -0
- /package/dist/{chunk-JGFRAFDL.js.map → chunk-FVINAAKA.js.map} +0 -0
- /package/dist/{chunk-BU3SEFA5.js.map → chunk-IOJDYUA7.js.map} +0 -0
- /package/dist/{chunk-DM4OMPWK.js.map → chunk-KNU73RZW.js.map} +0 -0
- /package/dist/{chunk-Z6EGP5D7.js.map → chunk-LDCSZ72P.js.map} +0 -0
- /package/dist/{chunk-CXANAIZU.js.map → chunk-N65E26UL.js.map} +0 -0
- /package/dist/{chunk-O3MT7DYI.js.map → chunk-N6MNJNHB.js.map} +0 -0
- /package/dist/{chunk-A7KV5UK4.js.map → chunk-OXD5LFQP.js.map} +0 -0
- /package/dist/{chunk-22SG74BD.js.map → chunk-R6ZCSXKX.js.map} +0 -0
- /package/dist/{chunk-4S53H2KR.js.map → chunk-VAZXUK6G.js.map} +0 -0
- /package/dist/{chunk-TNX5ZGDJ.js.map → chunk-VNXAF2KE.js.map} +0 -0
- /package/dist/{chunk-EHRRQ4IC.js.map → chunk-VP3TIUDF.js.map} +0 -0
- /package/dist/{chunk-6XZYT4SW.js.map → chunk-WGDEBIP4.js.map} +0 -0
- /package/dist/{timeout-U5O4ESK3.js.map → timeout-BEABACRP.js.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/harness/actuator-executor.ts","../src/utils/harness/auto-solidify.ts","../src/utils/harness/bridge.ts"],"sourcesContent":["/**\n * actuatorExecutor — bridge a side-effecting actuator into the harness\n * EXECUTE work fn.\n *\n * `refineExecutor` covers the artifact-typed case (refine a candidate\n * `T` against an evaluator); `actuatorExecutor` covers the side-effecting\n * case (write a catalog entry, mutate a template registry, edit a doc on\n * disk). The user's `apply` callback owns the side effect; the executor\n * wraps it in the per-claim lifecycle:\n *\n * 1. **One DATA per claim.** The producer captures the first DATA from\n * the bridged `apply` result, emits a {@link HarnessJobPayload} with\n * `execution` filled in, and completes. Subsequent inner DATAs are\n * ignored.\n * 2. **Cancel-on-teardown.** When the JobFlow pump unsubscribes (after\n * capturing first DATA, or on graph teardown), the producer's cleanup\n * fires `ac.abort()` which propagates into `apply`'s `signal`.\n * 3. **Errors surfaced as failure payload.** A thrown / ERROR result is\n * mapped via `onError` into a `failure`-outcome `ExecuteOutput` so the\n * dispatch effect can route the item rather than silently dropping it.\n *\n * **What `apply` may return.** Anything `fromAny` accepts: `Promise<R>`,\n * `Node<R>`, `AsyncIterable<R>`, `Iterable<R>`, or a synchronous `R`.\n * `Promise<R>` is the typical shape (`writeFile`, `fetch`, `db.execute`).\n *\n * **Pairing with `evalVerifier`.** `ExecuteOutput.artifact` is set to\n * the actuation record; an `evalVerifier<R>` whose `extractArtifact`\n * returns the record (or the post-apply world state) closes EXECUTE →\n * VERIFY with consistent typing end-to-end.\n *\n * @module\n */\n\nimport { COMPLETE, DATA, ERROR, type Messages, type Node, node } from \"@graphrefly/pure-ts/core\";\nimport { fromAny, type NodeInput } from \"@graphrefly/pure-ts/extra\";\nimport type { JobEnvelope } from \"../job-queue/index.js\";\n\nimport type { ExecuteOutput, HarnessExecutor, HarnessJobPayload, TriagedItem } from \"./types.js\";\n\n/**\n * What an actuator's `apply` may return. Mirrors `NodeInput<R>` plus a\n * raw `R` for synchronous side effects.\n */\nexport type ActuatorResult<R> = NodeInput<R>;\n\n/** Configuration for {@link actuatorExecutor}. */\nexport interface ActuatorExecutorConfig<R> {\n\t/**\n\t * Apply the side effect for this triaged item. Receives the abort\n\t * signal — actuators that own real I/O should thread `signal` into\n\t * `fetch`, `fs.writeFile`, child-process kills, etc. so that the\n\t * pump's teardown actually cancels in-flight work.\n\t *\n\t * The first DATA emitted by the bridged result wins; later DATAs are\n\t * discarded. ERROR (or a synchronous throw) is mapped via `onError`.\n\t */\n\tapply: (item: TriagedItem, opts: { signal: AbortSignal }) => ActuatorResult<R>;\n\n\t/**\n\t * Optional gate — when provided and returning `false`, the actuator\n\t * is skipped and the executor emits an `ExecuteOutput` with\n\t * `outcome: \"failure\"`. Use to route interventions the actuator can't\n\t * handle into the failure path.\n\t */\n\tshouldApply?: (item: TriagedItem) => boolean;\n\n\t/** Detail string for the skip path. Default: includes intervention name. */\n\tskipDetail?: (item: TriagedItem) => string;\n\n\t/**\n\t * Map a successfully-applied actuation record into an `ExecuteOutput<R>`.\n\t */\n\ttoOutput?: (record: R, item: TriagedItem) => ExecuteOutput<R>;\n\n\t/**\n\t * Map a thrown / ERROR result into an `ExecuteOutput<R>`.\n\t */\n\tonError?: (err: unknown, item: TriagedItem) => ExecuteOutput<R>;\n\n\t/** Node name prefix for `describe()` introspection. */\n\tname?: string;\n}\n\nfunction defaultToOutput<R>(record: R, item: TriagedItem): ExecuteOutput<R> {\n\treturn {\n\t\toutcome: \"success\",\n\t\tdetail: `actuator applied ${item.intervention} for ${truncate(item.summary)}`,\n\t\tartifact: record,\n\t};\n}\n\nfunction defaultOnError<R>(err: unknown, item: TriagedItem): ExecuteOutput<R> {\n\tconst message = err instanceof Error ? err.message : String(err);\n\treturn {\n\t\toutcome: \"failure\",\n\t\tdetail: `actuator threw on ${item.intervention}: ${message}`,\n\t};\n}\n\nfunction defaultSkipDetail(item: TriagedItem): string {\n\treturn `actuator skipped ${item.intervention} (shouldApply returned false)`;\n}\n\nfunction truncate(s: string, max = 80): string {\n\treturn s.length <= max ? s : `${s.slice(0, max - 1)}…`;\n}\n\n/**\n * Build a {@link HarnessExecutor} backed by a side-effecting actuator.\n *\n * @example File-system actuator that writes a catalog entry and emits the diff.\n * ```ts\n * const harness = harnessLoop(\"repair\", {\n * adapter,\n * executor: actuatorExecutor<CatalogPatch>({\n * async apply(item, { signal }) {\n * const patch = patchFromItem(item);\n * await fs.writeFile(patch.path, patch.contents, { signal });\n * return patch;\n * },\n * shouldApply: (item) => item.intervention === \"catalog-fn\",\n * }),\n * verifier: evalVerifier<CatalogPatch>({ ... }),\n * });\n * ```\n */\nexport function actuatorExecutor<R>(config: ActuatorExecutorConfig<R>): HarnessExecutor<R> {\n\tconst name = config.name ?? \"actuator-executor\";\n\tconst toOutput = config.toOutput ?? defaultToOutput<R>;\n\tconst onError = config.onError ?? defaultOnError<R>;\n\tconst skipDetail = config.skipDetail ?? defaultSkipDetail;\n\n\treturn (job: JobEnvelope<HarnessJobPayload<R>>, opts) => {\n\t\tconst item = job.payload.item;\n\n\t\tif (config.shouldApply && !config.shouldApply(item)) {\n\t\t\t// Synchronous failure payload — return as a plain object;\n\t\t\t// `fromAny` accepts the bare value and emits one DATA.\n\t\t\treturn {\n\t\t\t\t...job.payload,\n\t\t\t\texecution: { item, outcome: \"failure\", detail: skipDetail(item) },\n\t\t\t} satisfies HarnessJobPayload<R>;\n\t\t}\n\n\t\treturn node<HarnessJobPayload<R>>(\n\t\t\t[],\n\t\t\t(_data, actions) => {\n\t\t\t\tconst ac = new AbortController();\n\t\t\t\t// Link pump-supplied signal (Tier 6.5 2.5b): parent abort\n\t\t\t\t// (e.g. `harness.destroy()`) cascades into the inner AC and\n\t\t\t\t// thus into `apply(item, { signal })` + `fromAny({ signal })`.\n\t\t\t\tconst parentSignal = opts?.signal;\n\t\t\t\tlet unlinkParent: () => void = () => undefined;\n\t\t\t\tif (parentSignal) {\n\t\t\t\t\tif (parentSignal.aborted) {\n\t\t\t\t\t\tac.abort();\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconst onParentAbort = (): void => ac.abort();\n\t\t\t\t\t\tparentSignal.addEventListener(\"abort\", onParentAbort, { once: true });\n\t\t\t\t\t\tunlinkParent = () => parentSignal.removeEventListener(\"abort\", onParentAbort);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tlet captured = false;\n\t\t\t\tlet unsub: (() => void) | null = null;\n\t\t\t\tconst emitOnce = (out: ExecuteOutput<R>): void => {\n\t\t\t\t\tif (captured) return;\n\t\t\t\t\tcaptured = true;\n\t\t\t\t\tactions.down([\n\t\t\t\t\t\t[DATA, { ...job.payload, execution: { item, ...out } }],\n\t\t\t\t\t\t[COMPLETE],\n\t\t\t\t\t] satisfies Messages);\n\t\t\t\t\tunsub?.();\n\t\t\t\t\tunsub = null;\n\t\t\t\t};\n\t\t\t\tlet inner: Node<R>;\n\t\t\t\ttry {\n\t\t\t\t\tconst rawResult = config.apply(item, { signal: ac.signal });\n\t\t\t\t\tinner = fromAny<R>(rawResult, { signal: ac.signal });\n\t\t\t\t} catch (err) {\n\t\t\t\t\temitOnce(onError(err, item));\n\t\t\t\t\treturn {\n\t\t\t\t\t\tonDeactivation: () => {\n\t\t\t\t\t\t\tunlinkParent();\n\t\t\t\t\t\t\tac.abort();\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tunsub = inner.subscribe((batch) => {\n\t\t\t\t\tfor (const m of batch) {\n\t\t\t\t\t\tif (captured) return;\n\t\t\t\t\t\tif (m[0] === DATA) {\n\t\t\t\t\t\t\temitOnce(toOutput(m[1] as R, item));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (m[0] === ERROR) {\n\t\t\t\t\t\t\temitOnce(onError(m[1], item));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (m[0] === COMPLETE) {\n\t\t\t\t\t\t\temitOnce(onError(new Error(\"actuator inner completed without emitting DATA\"), item));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\t// Sync DATA delivery (cached state / `fromAny` over a sync value):\n\t\t\t\t// the callback ran reentrantly before `unsub` was assigned, so\n\t\t\t\t// `emitOnce`'s `unsub?.()` was a no-op. Drop the upstream subscription\n\t\t\t\t// now that we have the handle. Without this, the inner stays\n\t\t\t\t// subscribed until producer teardown — leaks at high volume.\n\t\t\t\tif (captured && unsub) {\n\t\t\t\t\tunsub();\n\t\t\t\t\tunsub = null;\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tonDeactivation: () => {\n\t\t\t\t\t\tunlinkParent();\n\t\t\t\t\t\tac.abort();\n\t\t\t\t\t\tunsub?.();\n\t\t\t\t\t\tunsub = null;\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t},\n\t\t\t{ name: `${name}/inner`, describeKind: \"producer\" },\n\t\t);\n\t};\n}\n\n// ---------------------------------------------------------------------------\n// dispatchActuator\n// ---------------------------------------------------------------------------\n\n/**\n * Apply callback shape consumed by {@link dispatchActuator}. Same shape as\n * {@link ActuatorExecutorConfig.apply}.\n */\nexport type ActuatorApplyFn<R> = (\n\titem: TriagedItem,\n\topts: { signal: AbortSignal },\n) => ActuatorResult<R>;\n\n/** Configuration for {@link dispatchActuator}. */\nexport interface DispatchActuatorConfig<R> {\n\t/**\n\t * Per-intervention apply callbacks. Keyed by `TriagedItem.intervention`.\n\t * Items whose intervention is not in `routes` fall through to `default`\n\t * (when set) or emit a skip-failure `ExecuteOutput`.\n\t */\n\troutes: Readonly<Partial<Record<TriagedItem[\"intervention\"], ActuatorApplyFn<R>>>>;\n\t/** Fallback apply callback for items whose intervention is not in `routes`. */\n\tdefault?: ActuatorApplyFn<R>;\n\t/** Node name prefix for `describe()` introspection. */\n\tname?: string;\n}\n\n/**\n * Multi-intervention actuator — dispatches each `TriagedItem` to one of\n * several `apply` callbacks based on `item.intervention`.\n *\n * Internally builds a single `actuatorExecutor` whose `apply` resolves the\n * intervention → callback at call-time. Items with no matching route and no\n * `default` emit a skip-failure with detail\n * `\"no route for intervention 'X'\"`.\n */\nexport function dispatchActuator<R>(config: DispatchActuatorConfig<R>): HarnessExecutor<R> {\n\tconst name = config.name ?? \"dispatch-actuator\";\n\tconst defaultFn = config.default ?? null;\n\tconst hasDefault = defaultFn != null;\n\treturn actuatorExecutor<R>({\n\t\tapply: (item, opts) => {\n\t\t\tconst fn = Object.hasOwn(config.routes, item.intervention)\n\t\t\t\t? config.routes[item.intervention]!\n\t\t\t\t: defaultFn;\n\t\t\tif (!fn) {\n\t\t\t\tthrow new Error(`dispatchActuator: no route for intervention '${item.intervention}'`);\n\t\t\t}\n\t\t\treturn fn(item, opts);\n\t\t},\n\t\tshouldApply: (item) => Object.hasOwn(config.routes, item.intervention) || hasDefault,\n\t\tskipDetail: (item) => `no route for intervention '${item.intervention}'`,\n\t\tname,\n\t});\n}\n","/**\n * autoSolidify — promote successful VERIFY runs into a durable artifact\n * (catalog entry, skill, template, doc edit, …).\n *\n * Closes the dogfood retrospective loop: when the harness's VERIFY\n * stage reports `verified: true`, the validated intervention should\n * become an authoring artifact the next loop run can rely on. This\n * primitive is the generic substrate — pass a `write` callback that\n * does the actual promotion (e.g. `overlay.upsertTemplate` for the\n * dogfood catalog overlay; `fs.writeFile` for a doc edit; `ctx.skill`\n * for a Hermes-style skill registry).\n *\n * @example Wire the catalog overlay as the solidify target.\n * ```ts\n * const solidified = autoSolidify({\n * verifyResults: harness.verifyResults.latest,\n * extract: (vr) => vr.execution.artifact ?? null,\n * write: (entry, vr) => overlay.upsertFn(`learned-${vr.item.summary}`, entry),\n * });\n * solidified.subscribe(() => {}); // keep alive for log\n * ```\n *\n * **Why a node and not just an effect.** The returned `Node<R>` emits\n * each promoted artifact, so callers can pipe solidifications through\n * the standard reactive surface (`describe()`, `observe()`, replay\n * buffers) instead of side-channel logging. An audit / dashboard that\n * wants \"what was learned this run?\" subscribes to the returned node;\n * the `write` callback owns the durable side effect.\n *\n * **Idempotency is the caller's responsibility.** The primitive\n * promotes every `verified: true` wave that passes the predicate. If\n * the harness re-verifies the same item (e.g. via reingestion), the\n * `write` callback is invoked again. Wrap your write fn with a\n * dedup-by-key guard if your target store would otherwise bloat. The\n * inner `seen` set inside this factory is intentionally absent — the\n * harness already retains via topic logs and the user may want\n * re-promotion semantics that are domain-specific.\n *\n * @module\n */\n\nimport { COMPLETE, DATA, ERROR, type Messages, type Node, node } from \"@graphrefly/pure-ts/core\";\n\nimport type { VerifyResult } from \"./types.js\";\n\n/**\n * Configuration for {@link autoSolidify}.\n *\n * `R` is the artifact type the upstream EXECUTE stage produced (and\n * `evalVerifier` carries through `execution.artifact`). `T` is the\n * promotion shape — what `write` consumes and what the returned node\n * emits. Often `T = R`, but they diverge when the actuator's raw\n * artifact needs a transform before storing (e.g. wrap a `CatalogPatch`\n * into a `CatalogEntry` with effectiveness metadata).\n */\nexport interface AutoSolidifyConfig<R, T = R> {\n\t/** Reactive verify-result stream. Typically `harness.verifyResults.latest`. */\n\tverifyResults: Node<VerifyResult<R> | null>;\n\t/**\n\t * Pull the value-to-promote out of a verified VerifyResult.\n\t * Default: `(vr) => vr.execution.artifact as T | null`. Return `null`\n\t * to skip a particular VerifyResult even when `verified: true` (e.g.\n\t * an LLM-default executor produces no artifact and there's nothing to\n\t * solidify).\n\t */\n\textract?: (vr: VerifyResult<R>) => T | null;\n\t/**\n\t * Optional gate beyond `verified === true`. When provided, the\n\t * primitive only promotes when this returns `true`. Default: pass\n\t * everything verified.\n\t *\n\t * Useful predicates:\n\t * - `(vr) => vr.item.intervention === \"catalog-fn\"` — only catalog work.\n\t * - `(vr) => (vr.findings ?? []).every(f => !/regression/i.test(f))` —\n\t * skip even-passes that mention regressions.\n\t */\n\tpredicate?: (vr: VerifyResult<R>) => boolean;\n\t/**\n\t * Promote — usually a side effect (write to overlay, fs, KG, etc.).\n\t * Receives the extracted artifact AND the originating VerifyResult so\n\t * the writer can use any context it needs (item summary, eval task\n\t * IDs, finding text, …) when shaping the durable record.\n\t */\n\twrite: (artifact: T, vr: VerifyResult<R>) => void;\n\t/** Node name for `describe()` introspection. Default `\"auto-solidify\"`. */\n\tname?: string;\n}\n\n/**\n * Build a `Node<T>` that subscribes to `verifyResults`, filters to\n * verified passes that produced an extractable artifact, runs `write`,\n * and emits the artifact. Use the returned node as a subscription\n * point for audit / dashboard / log pipelines.\n *\n * **Terminal-on-error semantics.** A throw from `predicate`, `extract`,\n * or `write` surfaces as `[[ERROR]]` on the returned node and\n * **terminates** it — the upstream subscription tears down and no\n * further DATA is emitted. This matches the spec's terminal-frame\n * contract for ERROR. If you want the solidify node to stay live\n * across user-callback throws, wrap your callbacks with try/catch\n * internally and emit a sentinel value or no-op on failure. A future\n * non-terminal `errors: Node<unknown>` companion may surface failures\n * without terminating the success stream — flagged as a follow-up.\n *\n * @returns A `Node<T>` that emits one DATA per promoted artifact.\n * Stays live as long as `verifyResults` is live AND no user callback\n * has thrown.\n */\nexport function autoSolidify<R, T = R>(config: AutoSolidifyConfig<R, T>): Node<T> {\n\tconst name = config.name ?? \"auto-solidify\";\n\tconst extract =\n\t\tconfig.extract ?? ((vr: VerifyResult<R>) => (vr.execution.artifact ?? null) as T | null);\n\tconst predicate = config.predicate ?? (() => true);\n\n\treturn node<T>(\n\t\t[],\n\t\t(_data, actions) => {\n\t\t\tlet unsub: (() => void) | null = null;\n\t\t\tlet terminated = false;\n\t\t\tconst tearDown = (): void => {\n\t\t\t\tif (terminated) return;\n\t\t\t\tterminated = true;\n\t\t\t\tunsub?.();\n\t\t\t\tunsub = null;\n\t\t\t};\n\t\t\tconst emitTerminalError = (err: unknown): void => {\n\t\t\t\tif (terminated) return;\n\t\t\t\tactions.down([[ERROR, err]] satisfies Messages);\n\t\t\t\ttearDown();\n\t\t\t};\n\t\t\tunsub = config.verifyResults.subscribe((batch) => {\n\t\t\t\tif (terminated) return;\n\t\t\t\tfor (const m of batch) {\n\t\t\t\t\tif (terminated) return;\n\t\t\t\t\tif (m[0] !== DATA) {\n\t\t\t\t\t\tif (m[0] === COMPLETE) {\n\t\t\t\t\t\t\t// Upstream verifyResults completed (rare; harness destroy).\n\t\t\t\t\t\t\t// Forward COMPLETE and tear down — solidify is terminal too.\n\t\t\t\t\t\t\tactions.down([[COMPLETE]] satisfies Messages);\n\t\t\t\t\t\t\ttearDown();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tconst vr = m[1] as VerifyResult<R> | null;\n\t\t\t\t\tif (vr == null) continue;\n\t\t\t\t\tif (!vr.verified) continue;\n\t\t\t\t\t// User callbacks (predicate / extract / write) are isolated\n\t\t\t\t\t// in try/catch so a throw lands as a single terminal ERROR\n\t\t\t\t\t// rather than propagating into the upstream emitter where\n\t\t\t\t\t// it would skip later messages in the same batch and leave\n\t\t\t\t\t// the solidify node un-terminated.\n\t\t\t\t\tlet pass: boolean;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tpass = predicate(vr);\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\temitTerminalError(err);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tif (!pass) continue;\n\t\t\t\t\tlet artifact: T | null;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tartifact = extract(vr);\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\temitTerminalError(err);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tif (artifact == null) continue;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconfig.write(artifact, vr);\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\temitTerminalError(err);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tactions.down([[DATA, artifact]] satisfies Messages);\n\t\t\t\t}\n\t\t\t});\n\t\t\t// If `subscribe` fired terminally during the call (push-on-subscribe\n\t\t\t// of an already-COMPLETE upstream), `tearDown()` ran inside the\n\t\t\t// callback before `unsub` was assigned, so the unsub is still\n\t\t\t// dangling. Drop it now if we're already terminated.\n\t\t\tif (terminated && unsub) {\n\t\t\t\tunsub();\n\t\t\t\tunsub = null;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tonDeactivation: () => {\n\t\t\t\t\ttearDown();\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t\t{ name, describeKind: \"producer\" },\n\t);\n}\n","/**\n * Harness bridge factories (roadmap §9.0).\n *\n * Intake bridges, eval source wrapper, before/after comparison,\n * affected-task filter, code-change bridge, and notification effect.\n * All are compositions of existing primitives — no new abstractions.\n *\n * @module\n */\n\nimport { type Node, node } from \"@graphrefly/pure-ts/core\";\nimport { fromAny, switchMap } from \"@graphrefly/pure-ts/extra\";\nimport type { Graph } from \"@graphrefly/pure-ts/graph\";\nimport type { TopicGraph } from \"../messaging/index.js\";\n\nimport type { IntakeItem, Severity, TriagedItem } from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Generic intake bridge\n// ---------------------------------------------------------------------------\n\n/** Options for {@link createIntakeBridge}. */\nexport interface CreateIntakeBridgeOptions<T> {\n\t/** Graph to register the effect node on (B.1 narrow-waist visibility). */\n\tgraph: Graph;\n\t/** Reactive node emitting domain-specific data. */\n\tsource: Node<T>;\n\t/** TopicGraph to publish IntakeItem entries to. */\n\tintakeTopic: TopicGraph<IntakeItem>;\n\t/** Converts source data into IntakeItem[]. Return empty array to skip. */\n\tparser: (value: T) => IntakeItem[];\n\t/** Effect-node name (default `\"intake-bridge\"`). */\n\tname?: string;\n}\n\n/**\n * Generic source→intake bridge factory.\n *\n * Watches a source node for new values, passes each through a user-supplied\n * `parser` that produces zero or more `IntakeItem`s, and publishes them to\n * the given intake topic.\n *\n * This is the generalized pattern behind {@link evalIntakeBridge}. Use it for\n * CI results, test failures, Slack messages, monitoring alerts, or any domain\n * where structured results should flow into a harness loop.\n *\n * The effect node is registered on the supplied `graph` so it appears in\n * `describe()` and is owned by the graph's lifecycle.\n *\n * @returns The effect node (for lifecycle management).\n */\nexport function createIntakeBridge<T>(opts: CreateIntakeBridgeOptions<T>): Node<unknown> {\n\tconst { graph, source, intakeTopic, parser, name = \"intake-bridge\" } = opts;\n\tconst eff = node(\n\t\t[source as Node<unknown>],\n\t\t(batchData, _actions, ctx) => {\n\t\t\tconst data = batchData.map((batch, i) =>\n\t\t\t\tbatch != null && batch.length > 0 ? batch.at(-1) : ctx.prevData[i],\n\t\t\t);\n\t\t\tconst value = data[0];\n\t\t\tif (value === undefined) return;\n\t\t\tconst items = parser(value as T);\n\t\t\tfor (const item of items) {\n\t\t\t\tintakeTopic.publish(item);\n\t\t\t}\n\t\t},\n\t\t{ describeKind: \"effect\" },\n\t);\n\tgraph.add(eff, { name });\n\treturn eff;\n}\n\n// ---------------------------------------------------------------------------\n// Generic eval result shape\n// ---------------------------------------------------------------------------\n\n/**\n * Minimal eval result shape accepted by the bridge.\n *\n * TS eval runner uses `EvalRun` from `evals/lib/types.ts` which is a superset\n * of this shape. The bridge only reads what it needs.\n */\nexport interface EvalRunResult {\n\trun_id: string;\n\tmodel: string;\n\ttasks: EvalTaskResult[];\n}\n\nexport interface EvalTaskResult {\n\ttask_id: string;\n\tvalid: boolean;\n\tjudge_scores?: EvalJudgeScore[];\n}\n\nexport interface EvalJudgeScore {\n\tclaim: string;\n\tpass: boolean;\n\treasoning: string;\n}\n\n// ---------------------------------------------------------------------------\n// Bridge factory\n// ---------------------------------------------------------------------------\n\nexport interface EvalIntakeBridgeOptions {\n\t/** Graph to register the effect node on (B.1 narrow-waist visibility). */\n\tgraph: Graph;\n\t/** Node emitting EvalRunResult (or EvalRunResult[]). */\n\tsource: Node<EvalRunResult | EvalRunResult[]>;\n\t/** TopicGraph to publish IntakeItem entries to. */\n\tintakeTopic: TopicGraph<IntakeItem>;\n\t/** Effect-node name (default `\"eval-intake-bridge\"`). */\n\tname?: string;\n\t/** Minimum severity for eval-sourced items (default `\"medium\"`). */\n\tdefaultSeverity?: Severity;\n}\n\n/**\n * Create an effect node that watches an eval results source and publishes\n * per-criterion findings to an intake topic.\n *\n * Each failing judge criterion produces a separate IntakeItem — not one\n * item per task. This gives the triage stage granular findings to classify.\n *\n * The effect node is registered on the supplied `graph` so it appears in\n * `describe()` and is owned by the graph's lifecycle.\n *\n * @returns The effect node (for lifecycle management).\n */\nexport function evalIntakeBridge(opts: EvalIntakeBridgeOptions): Node<unknown> {\n\tconst {\n\t\tgraph,\n\t\tsource,\n\t\tintakeTopic,\n\t\tname = \"eval-intake-bridge\",\n\t\tdefaultSeverity = \"medium\",\n\t} = opts;\n\n\tconst eff = node(\n\t\t[source as Node<unknown>],\n\t\t(batchData, _actions, ctx) => {\n\t\t\tconst data = batchData.map((batch, i) =>\n\t\t\t\tbatch != null && batch.length > 0 ? batch.at(-1) : ctx.prevData[i],\n\t\t\t);\n\t\t\tconst results = data[0];\n\t\t\tif (results === undefined) return;\n\t\t\tconst runs = Array.isArray(results)\n\t\t\t\t? (results as EvalRunResult[])\n\t\t\t\t: [results as EvalRunResult];\n\n\t\t\tfor (const run of runs) {\n\t\t\t\tfor (const task of run.tasks) {\n\t\t\t\t\t// Only process tasks with failures\n\t\t\t\t\tif (task.valid && task.judge_scores?.every((s) => s.pass)) continue;\n\n\t\t\t\t\t// Task-level validity failure (no judge scores or overall invalid)\n\t\t\t\t\tif (!task.valid && (!task.judge_scores || task.judge_scores.length === 0)) {\n\t\t\t\t\t\tintakeTopic.publish({\n\t\t\t\t\t\t\tsource: \"eval\",\n\t\t\t\t\t\t\tsummary: `Task ${task.task_id} invalid (model: ${run.model})`,\n\t\t\t\t\t\t\tevidence: `Run ${run.run_id}: task produced invalid output`,\n\t\t\t\t\t\t\taffectsAreas: [\"graphspec\"],\n\t\t\t\t\t\t\taffectsEvalTasks: [task.task_id],\n\t\t\t\t\t\t\tseverity: defaultSeverity,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Per-criterion findings\n\t\t\t\t\tif (task.judge_scores) {\n\t\t\t\t\t\tfor (const score of task.judge_scores) {\n\t\t\t\t\t\t\tif (score.pass) continue;\n\t\t\t\t\t\t\tintakeTopic.publish({\n\t\t\t\t\t\t\t\tsource: \"eval\",\n\t\t\t\t\t\t\t\tsummary: `${task.task_id}: ${score.claim} (model: ${run.model})`,\n\t\t\t\t\t\t\t\tevidence: score.reasoning,\n\t\t\t\t\t\t\t\taffectsAreas: [\"graphspec\"],\n\t\t\t\t\t\t\t\taffectsEvalTasks: [task.task_id],\n\t\t\t\t\t\t\t\tseverity: defaultSeverity,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t{ describeKind: \"effect\" },\n\t);\n\tgraph.add(eff, { name });\n\treturn eff;\n}\n\n// ---------------------------------------------------------------------------\n// Composition A: Eval-driven improvement loop\n// ---------------------------------------------------------------------------\n\n/**\n * Wrap any eval runner as a reactive producer node.\n *\n * When `trigger` emits, calls `runner()` and emits the result downstream.\n * Uses `switchMap` + `fromAny` — the async boundary stays in the source\n * layer (spec §5.10). A new trigger cancels any in-flight run.\n *\n * Pure transform via operator composition — does not construct an\n * effect/derived node, so no `graph` parameter is needed.\n *\n * ```ts\n * const trigger = state(0); // bump to trigger a new run\n * const results = evalSource(trigger, () => runEvals(config));\n * results.subscribe(msgs => { ... });\n * trigger.emit(1); // fires the runner\n * ```\n *\n * @param trigger - Any node; each new DATA emission fires the runner.\n * @param runner - Returns an EvalRunResult (or promise of one).\n */\nexport function evalSource<T extends EvalRunResult>(\n\ttrigger: Node<unknown>,\n\trunner: () => T | Promise<T>,\n): Node<T> {\n\treturn switchMap(trigger, () => fromAny(runner()) as Node<T>);\n}\n\n// ---------------------------------------------------------------------------\n\n/** Per-task delta produced by {@link beforeAfterCompare}. */\nexport interface EvalTaskDelta {\n\ttaskId: string;\n\tbefore: boolean;\n\tafter: boolean;\n\t/** Score-level diff (after − before), undefined if no scores present. */\n\tscoreDiff?: number;\n}\n\n/** Output of {@link beforeAfterCompare}. */\nexport interface EvalDelta {\n\t/** Task IDs that newly fail in `after` (were passing in `before`). */\n\tnewFailures: string[];\n\t/** Task IDs that now pass in `after` (were failing in `before`). */\n\tresolved: string[];\n\t/** Full per-task breakdown. */\n\ttaskDeltas: EvalTaskDelta[];\n\t/** True when net resolutions > net failures. */\n\toverallImproved: boolean;\n}\n\n/** Options for {@link beforeAfterCompare}. */\nexport interface BeforeAfterCompareOptions {\n\t/** Graph to register the derived node on (B.1 narrow-waist visibility). */\n\tgraph: Graph;\n\t/** Node holding the baseline eval result. */\n\tbefore: Node<EvalRunResult>;\n\t/** Node holding the new eval result. */\n\tafter: Node<EvalRunResult>;\n\t/** Derived-node name (default `\"eval-delta\"`). */\n\tname?: string;\n}\n\n/**\n * Derived node that computes before/after eval deltas.\n *\n * Pure computation: no LLM, no async. Compares per-task validity and\n * pass counts between two `EvalRunResult` snapshots.\n *\n * The derived node is registered on the supplied `graph` so it appears in\n * `describe()` and is owned by the graph's lifecycle.\n */\nexport function beforeAfterCompare(opts: BeforeAfterCompareOptions): Node<EvalDelta> {\n\tconst { graph, before, after, name = \"eval-delta\" } = opts;\n\tconst der = node<EvalDelta>(\n\t\t[before as Node<unknown>, after as Node<unknown>],\n\t\t(batchData, actions, ctx) => {\n\t\t\tconst data = batchData.map((batch, i) =>\n\t\t\t\tbatch != null && batch.length > 0 ? batch.at(-1) : ctx.prevData[i],\n\t\t\t);\n\t\t\tconst bRes = data[0] as EvalRunResult;\n\t\t\tconst aRes = data[1] as EvalRunResult;\n\n\t\t\tconst beforeMap = new Map<string, EvalTaskResult>(bRes.tasks.map((t) => [t.task_id, t]));\n\t\t\tconst afterMap = new Map<string, EvalTaskResult>(aRes.tasks.map((t) => [t.task_id, t]));\n\n\t\t\tconst allIds = new Set([...beforeMap.keys(), ...afterMap.keys()]);\n\t\t\tconst taskDeltas: EvalTaskDelta[] = [];\n\t\t\tconst newFailures: string[] = [];\n\t\t\tconst resolved: string[] = [];\n\n\t\t\tfor (const id of allIds) {\n\t\t\t\tconst bt = beforeMap.get(id);\n\t\t\t\tconst at = afterMap.get(id);\n\t\t\t\tconst beforeValid = bt?.valid ?? false;\n\t\t\t\tconst afterValid = at?.valid ?? false;\n\n\t\t\t\tconst beforeScore = bt?.judge_scores\n\t\t\t\t\t? bt.judge_scores.filter((s) => s.pass).length\n\t\t\t\t\t: undefined;\n\t\t\t\tconst afterScore = at?.judge_scores\n\t\t\t\t\t? at.judge_scores.filter((s) => s.pass).length\n\t\t\t\t\t: undefined;\n\t\t\t\tconst scoreDiff =\n\t\t\t\t\tbeforeScore !== undefined && afterScore !== undefined\n\t\t\t\t\t\t? afterScore - beforeScore\n\t\t\t\t\t\t: undefined;\n\n\t\t\t\ttaskDeltas.push({ taskId: id, before: beforeValid, after: afterValid, scoreDiff });\n\t\t\t\tif (beforeValid && !afterValid) newFailures.push(id);\n\t\t\t\tif (!beforeValid && afterValid) resolved.push(id);\n\t\t\t}\n\n\t\t\tactions.emit({\n\t\t\t\tnewFailures,\n\t\t\t\tresolved,\n\t\t\t\ttaskDeltas,\n\t\t\t\toverallImproved: resolved.length > newFailures.length,\n\t\t\t});\n\t\t},\n\t\t{ describeKind: \"derived\" },\n\t);\n\tgraph.add(der as Node<unknown>, { name });\n\treturn der;\n}\n\n// ---------------------------------------------------------------------------\n\n/** Options for {@link affectedTaskFilter}. */\nexport interface AffectedTaskFilterOptions {\n\t/** Graph to register the derived node on (B.1 narrow-waist visibility). */\n\tgraph: Graph;\n\t/** Node holding the current list of triaged items. */\n\tissues: Node<readonly TriagedItem[]>;\n\t/**\n\t * Optional node (or plain array) of all known task IDs.\n\t * When provided, output is the intersection.\n\t */\n\tfullTaskSet?: Node<readonly string[]> | readonly string[];\n\t/** Derived-node name (default `\"affected-task-filter\"`). */\n\tname?: string;\n}\n\n/**\n * Derived node that selects which eval task IDs to re-run.\n *\n * Collects `affectsEvalTasks` from all triaged items, deduplicates, then\n * optionally intersects with `fullTaskSet`. Returns a sorted array of IDs.\n *\n * Use this to avoid re-running the full eval suite after each fix: only the\n * tasks that the triaged items claim to affect are returned.\n *\n * The derived node is registered on the supplied `graph` so it appears in\n * `describe()` and is owned by the graph's lifecycle.\n */\nexport function affectedTaskFilter(opts: AffectedTaskFilterOptions): Node<string[]> {\n\tconst { graph, issues, fullTaskSet, name = \"affected-task-filter\" } = opts;\n\n\tlet taskSetNode: Node<unknown> | null = null;\n\tif (fullTaskSet != null) {\n\t\tif (Array.isArray(fullTaskSet)) {\n\t\t\t// Static-array form: register the inline state node so it appears\n\t\t\t// in `describe()`/`explain()` walks (EC8 — qa 2026-04-30).\n\t\t\tconst inlineSet = node([], { initial: fullTaskSet as readonly string[] });\n\t\t\tgraph.add(inlineSet, { name: `${name}/fullTaskSet` });\n\t\t\ttaskSetNode = inlineSet as Node<unknown>;\n\t\t} else {\n\t\t\t// User-supplied Node — owned by the caller's graph; don't re-add.\n\t\t\ttaskSetNode = fullTaskSet as Node<unknown>;\n\t\t}\n\t}\n\n\tconst deps: Node<unknown>[] = [issues as Node<unknown>];\n\tif (taskSetNode) deps.push(taskSetNode);\n\n\tconst der = node<string[]>(\n\t\tdeps,\n\t\t(batchData, actions, ctx) => {\n\t\t\tconst data = batchData.map((batch, i) =>\n\t\t\t\tbatch != null && batch.length > 0 ? batch.at(-1) : ctx.prevData[i],\n\t\t\t);\n\t\t\tconst items = data[0] as readonly TriagedItem[];\n\t\t\tconst all = taskSetNode ? new Set(data[1] as readonly string[]) : null;\n\n\t\t\tconst affected = new Set<string>();\n\t\t\tfor (const item of items) {\n\t\t\t\tfor (const id of item.affectsEvalTasks ?? []) {\n\t\t\t\t\tif (all == null || all.has(id)) affected.add(id);\n\t\t\t\t}\n\t\t\t}\n\t\t\tactions.emit([...affected].sort());\n\t\t},\n\t\t{ describeKind: \"derived\" },\n\t);\n\tgraph.add(der as Node<unknown>, { name });\n\treturn der;\n}\n\n// ---------------------------------------------------------------------------\n// Composition D: Quality gate (CI/CD)\n// ---------------------------------------------------------------------------\n\n/** A single lint error emitted by a CI tool. */\nexport interface LintError {\n\tfile: string;\n\tline: number;\n\tcol: number;\n\trule: string;\n\tmessage: string;\n}\n\n/** A single test failure emitted by a test runner. */\nexport interface TestFailure {\n\ttestId: string;\n\tfile: string;\n\tmessage: string;\n}\n\n/** Structured code-change / CI event. */\nexport interface CodeChange {\n\t/** Files touched by the change. */\n\tfiles: string[];\n\tlintErrors?: LintError[];\n\ttestFailures?: TestFailure[];\n}\n\n/** Options for {@link codeChangeBridge}. */\nexport interface CodeChangeBridgeOptions {\n\t/** Graph to register the effect node on (B.1 narrow-waist visibility). */\n\tgraph: Graph;\n\t/** Node emitting CodeChange events. */\n\tsource: Node<CodeChange>;\n\t/** TopicGraph to publish IntakeItem entries to. */\n\tintakeTopic: TopicGraph<IntakeItem>;\n\t/** Optional custom parser (overrides default). */\n\tparser?: (change: CodeChange) => IntakeItem[];\n\t/** Effect-node name (default `\"code-change-bridge\"`). */\n\tname?: string;\n\t/** Default severity for generated IntakeItems (default `\"high\"`). */\n\tdefaultSeverity?: Severity;\n}\n\n/**\n * Intake bridge for code-change / CI events.\n *\n * Watches a source node for `CodeChange` events and publishes one\n * `IntakeItem` per lint error and per test failure to the intake topic.\n * Pass a custom `parser` to override the default mapping.\n *\n * The effect node is registered on the supplied `graph` so it appears in\n * `describe()` and is owned by the graph's lifecycle.\n */\nexport function codeChangeBridge(opts: CodeChangeBridgeOptions): Node<unknown> {\n\tconst {\n\t\tgraph,\n\t\tsource,\n\t\tintakeTopic,\n\t\tparser,\n\t\tname = \"code-change-bridge\",\n\t\tdefaultSeverity = \"high\",\n\t} = opts;\n\n\tfunction defaultParser(change: CodeChange): IntakeItem[] {\n\t\tconst items: IntakeItem[] = [];\n\t\tfor (const err of change.lintErrors ?? []) {\n\t\t\titems.push({\n\t\t\t\tsource: \"code-change\",\n\t\t\t\tsummary: `Lint: ${err.rule} in ${err.file}:${err.line}`,\n\t\t\t\tevidence: err.message,\n\t\t\t\taffectsAreas: [err.file],\n\t\t\t\tseverity: defaultSeverity,\n\t\t\t});\n\t\t}\n\t\tfor (const fail of change.testFailures ?? []) {\n\t\t\titems.push({\n\t\t\t\tsource: \"test\",\n\t\t\t\tsummary: `Test failure: ${fail.testId}`,\n\t\t\t\tevidence: fail.message,\n\t\t\t\taffectsAreas: [fail.file],\n\t\t\t\taffectsEvalTasks: [fail.testId],\n\t\t\t\tseverity: defaultSeverity,\n\t\t\t});\n\t\t}\n\t\treturn items;\n\t}\n\n\tconst resolve = parser ?? defaultParser;\n\n\tconst eff = node(\n\t\t[source as Node<unknown>],\n\t\t(batchData, _actions, ctx) => {\n\t\t\tconst data = batchData.map((batch, i) =>\n\t\t\t\tbatch != null && batch.length > 0 ? batch.at(-1) : ctx.prevData[i],\n\t\t\t);\n\t\t\tconst change = data[0];\n\t\t\tif (change === undefined) return;\n\t\t\tfor (const item of resolve(change as CodeChange)) {\n\t\t\t\tintakeTopic.publish(item);\n\t\t\t}\n\t\t},\n\t\t{ describeKind: \"effect\" },\n\t);\n\tgraph.add(eff, { name });\n\treturn eff;\n}\n\n// ---------------------------------------------------------------------------\n\n/** Transport function for {@link notifyEffect}. Sync or async. */\nexport type NotifyTransport<T> = (item: T) => void | Promise<void>;\n\n/** Options for {@link notifyEffect}. */\nexport interface NotifyEffectOptions<T> {\n\t/** Graph to register the effect node on (B.1 narrow-waist visibility). */\n\tgraph: Graph;\n\t/** TopicGraph whose latest entry triggers the notification. */\n\ttopic: TopicGraph<T>;\n\t/** Called with each new item. May return a Promise. */\n\ttransport: NotifyTransport<T>;\n\t/** Effect-node name (default `\"notify-effect\"`). */\n\tname?: string;\n}\n\n/**\n * Effect node that sends each new topic entry to an external channel.\n *\n * The `transport` function is called for every item published to `topic`.\n * Async transports are bridged via `fromAny` (spec §5.10 compliant).\n *\n * Typical use: Slack webhook, GitHub PR comment, email notification, etc.\n * The factory provides reactive wiring; the transport supplies domain logic.\n *\n * The effect node is registered on the supplied `graph` so it appears in\n * `describe()` and is owned by the graph's lifecycle.\n *\n * ```ts\n * notifyEffect({ graph, topic: alertQueue, transport: async (item) => {\n * await fetch(SLACK_WEBHOOK, { method: 'POST', body: JSON.stringify({ text: item.summary }) });\n * }});\n * ```\n */\nexport function notifyEffect<T>(opts: NotifyEffectOptions<T>): Node<unknown> {\n\tconst { graph, topic, transport, name = \"notify-effect\" } = opts;\n\t// SENTINEL contract on `topic.latest` (COMPOSITION-GUIDE §1a + spec §5.12):\n\t// - `topic.publish(undefined)` is rejected at the publish boundary, so\n\t// `undefined` is exclusively the protocol SENTINEL on the read side.\n\t// - `topic.latest` stays SENTINEL on empty (no eager DATA emission), so\n\t// the partial-false first-run gate holds this fn until the first publish.\n\t// - Legit `null` DATA (when `T` includes `null`) reaches `transport`;\n\t// user transports must handle `null` themselves per v5.\n\t// The `=== undefined` guard below is defense-in-depth for any future\n\t// empty-batch wave where `prevData[0]` is still SENTINEL — in normal flow\n\t// the first-run gate has already filtered the empty case.\n\tconst eff = node(\n\t\t[topic.latest as Node<unknown>],\n\t\t(batchData, _actions, ctx) => {\n\t\t\tconst data = batchData.map((batch, i) =>\n\t\t\t\tbatch != null && batch.length > 0 ? batch.at(-1) : ctx.prevData[i],\n\t\t\t);\n\t\t\tconst item = data[0];\n\t\t\tif (item === undefined) return;\n\t\t\t// transport is a side effect (webhook, Slack, email). Async transports\n\t\t\t// are fire-and-forget — the Promise result does not feed back into the\n\t\t\t// graph. Suppress unhandled-rejection noise by voiding the return.\n\t\t\tvoid transport(item as T);\n\t\t},\n\t\t{ describeKind: \"effect\" },\n\t);\n\tgraph.add(eff, { name });\n\treturn eff;\n}\n"],"mappings":";AAiCA,SAAS,UAAU,MAAM,OAAiC,YAAY;AACtE,SAAS,eAA+B;AAiDxC,SAAS,gBAAmB,QAAW,MAAqC;AAC3E,SAAO;AAAA,IACN,SAAS;AAAA,IACT,QAAQ,oBAAoB,KAAK,YAAY,QAAQ,SAAS,KAAK,OAAO,CAAC;AAAA,IAC3E,UAAU;AAAA,EACX;AACD;AAEA,SAAS,eAAkB,KAAc,MAAqC;AAC7E,QAAM,UAAU,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAC/D,SAAO;AAAA,IACN,SAAS;AAAA,IACT,QAAQ,qBAAqB,KAAK,YAAY,KAAK,OAAO;AAAA,EAC3D;AACD;AAEA,SAAS,kBAAkB,MAA2B;AACrD,SAAO,oBAAoB,KAAK,YAAY;AAC7C;AAEA,SAAS,SAAS,GAAW,MAAM,IAAY;AAC9C,S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W,UAAU,QAAQ;AAC7B,YAAM,OAAO,UAAU;AAAA,QAAI,CAAC,OAAO,MAClC,SAAS,QAAQ,MAAM,SAAS,IAAI,MAAM,GAAG,EAAE,IAAI,IAAI,SAAS,CAAC;AAAA,MAClE;AACA,YAAM,SAAS,KAAK,CAAC;AACrB,UAAI,WAAW,OAAW;AAC1B,iBAAW,QAAQ,QAAQ,MAAoB,GAAG;AACjD,oBAAY,QAAQ,IAAI;AAAA,MACzB;AAAA,IACD;AAAA,IACA,EAAE,cAAc,SAAS;AAAA,EAC1B;AACA,QAAM,IAAI,KAAK,EAAE,KAAK,CAAC;AACvB,SAAO;AACR;AAqCO,SAAS,aAAgB,MAA6C;AAC5E,QAAM,EAAE,OAAO,OAAO,WAAW,OAAO,gBAAgB,IAAI;AAW5D,QAAM,MAAMA;AAAA,IACX,CAAC,MAAM,MAAuB;AAAA,IAC9B,CAAC,WAAW,UAAU,QAAQ;AAC7B,YAAM,OAAO,UAAU;AAAA,QAAI,CAAC,OAAO,MAClC,SAAS,QAAQ,MAAM,SAAS,IAAI,MAAM,GAAG,EAAE,IAAI,IAAI,SAAS,CAAC;AAAA,MAClE;AACA,YAAM,OAAO,KAAK,CAAC;AACnB,UAAI,SAAS,OAAW;AAIxB,WAAK,UAAU,IAAS;AAAA,IACzB;AAAA,IACA,EAAE,cAAc,SAAS;AAAA,EAC1B;AACA,QAAM,IAAI,KAAK,EAAE,KAAK,CAAC;AACvB,SAAO;AACR;","names":["COMPLETE","DATA","ERROR","node","node","fromAny"]}
|
|
@@ -222,7 +222,7 @@ function withBreaker(breaker, options) {
|
|
|
222
222
|
}
|
|
223
223
|
});
|
|
224
224
|
syncState();
|
|
225
|
-
return unsub;
|
|
225
|
+
return { onDeactivation: unsub };
|
|
226
226
|
},
|
|
227
227
|
{
|
|
228
228
|
...operatorOpts(),
|
|
@@ -373,8 +373,10 @@ function budgetGate(source, constraints, opts) {
|
|
|
373
373
|
})
|
|
374
374
|
);
|
|
375
375
|
}
|
|
376
|
-
return
|
|
377
|
-
|
|
376
|
+
return {
|
|
377
|
+
onDeactivation: () => {
|
|
378
|
+
for (const u of unsubs) u();
|
|
379
|
+
}
|
|
378
380
|
};
|
|
379
381
|
},
|
|
380
382
|
{
|
|
@@ -512,9 +514,11 @@ function fallback(source, fb, options) {
|
|
|
512
514
|
} else a.down([m]);
|
|
513
515
|
}
|
|
514
516
|
});
|
|
515
|
-
return
|
|
516
|
-
|
|
517
|
-
|
|
517
|
+
return {
|
|
518
|
+
onDeactivation: () => {
|
|
519
|
+
sourceUnsub?.();
|
|
520
|
+
fallbackUnsub?.();
|
|
521
|
+
}
|
|
518
522
|
};
|
|
519
523
|
},
|
|
520
524
|
{
|
|
@@ -976,11 +980,13 @@ function rateLimiter(source, opts) {
|
|
|
976
980
|
} else a.down([m]);
|
|
977
981
|
}
|
|
978
982
|
});
|
|
979
|
-
return
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
983
|
+
return {
|
|
984
|
+
onDeactivation: () => {
|
|
985
|
+
terminated = true;
|
|
986
|
+
timer.cancel();
|
|
987
|
+
unsub();
|
|
988
|
+
optMirror.unsub();
|
|
989
|
+
}
|
|
984
990
|
};
|
|
985
991
|
},
|
|
986
992
|
{
|
|
@@ -1036,4 +1042,4 @@ export {
|
|
|
1036
1042
|
RateLimiterOverflowError,
|
|
1037
1043
|
rateLimiter
|
|
1038
1044
|
};
|
|
1039
|
-
//# sourceMappingURL=chunk-
|
|
1045
|
+
//# sourceMappingURL=chunk-T7SP3EYR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/resilience/breaker.ts","../src/utils/resilience/budget-gate.ts","../src/utils/resilience/fallback.ts","../src/utils/resilience/rate-limiter.ts","../src/utils/resilience/adaptive-rate-limiter.ts"],"sourcesContent":["/**\n * Circuit breaker — open/half-open/closed state machine + companion bundle.\n *\n * - {@link circuitBreaker} returns a synchronous breaker handle (counters,\n * state machine, optional reactive-options subscription).\n * - {@link withBreaker} wraps a `Node<T>` and surfaces a reactive\n * `Node<CircuitState>` companion (`bundle.breakerState`) for telemetry.\n */\n\nimport {\n\tCOMPLETE,\n\tDATA,\n\tDIRTY,\n\tERROR,\n\tfactoryTag,\n\tmonotonicNs,\n\ttype Node,\n\tnode,\n\tRESOLVED,\n} from \"@graphrefly/pure-ts/core\";\nimport {\n\tclampNonNegative,\n\tisNode,\n\tmsgVal,\n\ttype NodeOrValue,\n\toperatorOpts,\n} from \"../../base/resilience/_internal.js\";\nimport { type BackoffStrategy, NS_PER_SEC } from \"../../base/resilience/backoff.js\";\nimport type { GateState } from \"./gate-state.js\";\n\nexport type CircuitState = \"closed\" | \"open\" | \"half-open\";\n\n/**\n * Lifecycle-shaped state companion emitted by {@link withBreaker}\n * (DS-13.5.B, locked 2026-05-01). Pre-1.0 break vs the prior\n * `Node<CircuitState>` (string-only) shape.\n *\n * `status` extends {@link GateState} with `\"half-open\"`. The numeric\n * fields surface the breaker's full internal state for telemetry and\n * `describe()` traversal.\n *\n * @category extra/resilience\n */\nexport interface BreakerState {\n\treadonly status: GateState | \"half-open\";\n\treadonly failureCount: number;\n\treadonly openCycle: number;\n\treadonly lastOpenedAtNs: number;\n\treadonly halfOpenAttempts: number;\n\treadonly lastCooldownNs: number;\n}\n\n/**\n * Thrown when {@link withBreaker} is configured with `onOpen: \"error\"` and the breaker rejects work.\n *\n * @category extra\n */\nexport class CircuitOpenError extends Error {\n\toverride name = \"CircuitOpenError\";\n\tconstructor() {\n\t\tsuper(\"Circuit breaker is open\");\n\t}\n}\n\nexport interface CircuitBreakerOptions {\n\t/** Number of consecutive failures before opening. Default: 5. */\n\tfailureThreshold?: number;\n\t/** Base cooldown in nanoseconds before transitioning to half-open. Default: 30s. */\n\tcooldownNs?: number;\n\t/** Backoff strategy for cooldown escalation across consecutive open cycles. Overrides `cooldownNs` when provided. */\n\tcooldown?: BackoffStrategy;\n\t/** Max trial requests allowed in half-open state. Default: 1. */\n\thalfOpenMax?: number;\n\t/**\n\t * Clock function returning **nanoseconds** with `monotonicNs()` semantics\n\t * (monotonically non-decreasing; suitable for elapsed-time arithmetic — never\n\t * use `Date.now()` because wall-clock skew can flip elapsed math negative).\n\t * Default: `monotonicNs` from `core/clock`. Override for deterministic tests.\n\t */\n\tnow?: () => number;\n}\n\nexport interface CircuitBreaker {\n\t/** Whether a request should be allowed through. Triggers open→half-open transition when cooldown expires. */\n\tcanExecute(): boolean;\n\t/** Record a successful execution. Resets to closed. */\n\trecordSuccess(): void;\n\t/** Record a failed execution. May transition to open. */\n\trecordFailure(error?: unknown): void;\n\t/**\n\t * Current circuit state (read-only, does not trigger transitions).\n\t *\n\t * **Telemetry:** wrap with {@link withBreaker} to surface this as a reactive\n\t * `Node<CircuitState>` companion (`bundle.breakerState`) — every state\n\t * transition (`closed`/`open`/`half-open`) emits to subscribers.\n\t */\n\treadonly state: CircuitState;\n\t/** Number of consecutive failures in the current closed period. */\n\treadonly failureCount: number;\n\t/** Manually reset to closed state, clearing all counters. */\n\treset(): void;\n\t/**\n\t * Release the reactive-options subscription (Tier 6.5 3.2.4, 2026-04-29).\n\t * No-op when constructed with static options. Call when retiring a\n\t * breaker whose options came from a `Node<CircuitBreakerOptions>` to\n\t * avoid leaking the option-Node subscription.\n\t */\n\tdispose(): void;\n}\n\n/**\n * Factory for a synchronous circuit breaker with `closed`, `open`, and `half-open` states.\n *\n * Supports escalating cooldown via an optional {@link BackoffStrategy} — each consecutive\n * open→half-open→open cycle increments the backoff attempt.\n *\n * @param options - Threshold, cooldown, half-open limit, and optional clock\n * override; OR a `Node<CircuitBreakerOptions>` carrying the same shape\n * reactively (Tier 6.5 3.2.4).\n * @returns {@link CircuitBreaker} instance.\n *\n * @remarks\n * **Timing:** Uses `monotonicNs()` by default (nanoseconds). Override `now` for tests.\n *\n * **Reactive options (locked semantics, Tier 6.5 3.2.4, 2026-04-29).**\n * When `options` is a `Node<CircuitBreakerOptions>`, the breaker\n * subscribes at construction and re-reads `failureThreshold` /\n * `cooldownNs` / `cooldown` / `halfOpenMax` / `now` on each DATA. **An\n * option swap RESETS the breaker to `\"closed\"`** with all counters\n * cleared — operators tuning a runaway breaker get a clean baseline.\n * If retaining failure history across re-tunings matters, derive a new\n * breaker per-tuning instead. Call `breaker.dispose()` when retiring to\n * release the option-Node subscription.\n *\n * @example\n * ```ts\n * import { circuitBreaker, exponential, NS_PER_SEC } from \"@graphrefly/graphrefly-ts\";\n *\n * const b = circuitBreaker({\n * failureThreshold: 3,\n * cooldown: exponential({ baseNs: 1 * NS_PER_SEC }),\n * });\n * ```\n *\n * @category extra\n */\nexport function circuitBreaker(options?: NodeOrValue<CircuitBreakerOptions>): CircuitBreaker {\n\tlet threshold = 5;\n\tlet baseCooldownNs = 30 * NS_PER_SEC;\n\tlet cooldownStrategy: BackoffStrategy | null = null;\n\tlet halfOpenMax = 1;\n\tlet now: () => number = monotonicNs;\n\n\tfunction applyOptions(o: CircuitBreakerOptions | undefined): void {\n\t\tthreshold = Math.max(1, o?.failureThreshold ?? 5);\n\t\tbaseCooldownNs = clampNonNegative(o?.cooldownNs ?? 30 * NS_PER_SEC);\n\t\tcooldownStrategy = o?.cooldown ?? null;\n\t\thalfOpenMax = Math.max(1, o?.halfOpenMax ?? 1);\n\t\tnow = o?.now ?? monotonicNs;\n\t}\n\n\tlet _state: CircuitState = \"closed\";\n\tlet _failureCount = 0;\n\tlet _openCycle = 0;\n\tlet _lastOpenedAt = 0;\n\tlet _lastCooldownNs = baseCooldownNs;\n\tlet _halfOpenAttempts = 0;\n\n\t// DS-13.5.B (locked 2026-05-01): reactive option swaps preserve\n\t// internal state — no reset across rebind. `now` is mode-locked at\n\t// construction (clock override is structural); a mid-flight `now`\n\t// change is logged and skipped (the prior `now` is preserved).\n\t// Empty `{}` emits are no-ops.\n\t//\n\t// QA A2 (2026-05-03): bad-`now` mid-flight does NOT throw — sync\n\t// throw inside a subscribe callback corrupts host scheduler state\n\t// (mirrors timeout's `actions.down([[ERROR]])` rationale; sink-side\n\t// throws break the wave's dispatch contract).\n\t//\n\t// QA A8 (2026-05-03): the push-on-subscribe re-delivery of the\n\t// cached opts fires the subscribe callback once at attach time with\n\t// the same value used for the eager `applyOptions(initialOpts)`\n\t// call above. Skip the first cached emit so opts are not re-applied\n\t// twice on construction.\n\tlet initialOpts: CircuitBreakerOptions | undefined;\n\tlet optsUnsub: (() => void) | undefined;\n\tif (isNode(options)) {\n\t\tconst optsNode = options as Node<CircuitBreakerOptions>;\n\t\tinitialOpts = optsNode.cache as CircuitBreakerOptions | undefined;\n\t\tapplyOptions(initialOpts);\n\t\tconst lockedNow = initialOpts?.now;\n\t\tconst hadInitialCache = initialOpts !== undefined;\n\t\tlet firstEmit = hadInitialCache;\n\t\toptsUnsub = optsNode.subscribe((msgs) => {\n\t\t\tfor (const m of msgs) {\n\t\t\t\tif (m[0] !== DATA) continue;\n\t\t\t\tif (firstEmit) {\n\t\t\t\t\tfirstEmit = false;\n\t\t\t\t\tcontinue; // QA A8: skip push-on-subscribe replay of cached opts\n\t\t\t\t}\n\t\t\t\tconst next = m[1] as CircuitBreakerOptions | undefined;\n\t\t\t\tif (next == null || typeof next !== \"object\") continue;\n\t\t\t\tif (Object.keys(next).length === 0) continue; // empty {} no-op\n\t\t\t\tif (\"now\" in next && next.now !== lockedNow) {\n\t\t\t\t\t// QA A2: log + skip; do NOT throw inside a subscribe\n\t\t\t\t\t// callback — host scheduler corruption hazard.\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\"circuitBreaker: ignoring mid-flight `now` change — clock override is mode-locked at construction. Prior `now` preserved.\",\n\t\t\t\t\t);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// State-preserving merge: only re-apply the axes that\n\t\t\t\t// changed; preserve `_state`, `_failureCount`, etc.\n\t\t\t\tconst merged: CircuitBreakerOptions = {\n\t\t\t\t\t...(initialOpts ?? {}),\n\t\t\t\t\t...next,\n\t\t\t\t\t...(lockedNow !== undefined ? { now: lockedNow } : {}),\n\t\t\t\t};\n\t\t\t\tapplyOptions(merged);\n\t\t\t\tinitialOpts = merged;\n\t\t\t}\n\t\t});\n\t} else {\n\t\tapplyOptions(options as CircuitBreakerOptions | undefined);\n\t}\n\t_lastCooldownNs = baseCooldownNs;\n\n\tfunction getCooldownNs(): number {\n\t\tif (!cooldownStrategy) return baseCooldownNs;\n\t\tconst delayNs = cooldownStrategy(_openCycle);\n\t\treturn delayNs !== null ? delayNs : baseCooldownNs;\n\t}\n\n\tfunction transitionToOpen(): void {\n\t\t_state = \"open\";\n\t\t_lastCooldownNs = getCooldownNs();\n\t\t_lastOpenedAt = now();\n\t\t_halfOpenAttempts = 0;\n\t}\n\n\tconst breaker: CircuitBreaker = {\n\t\tcanExecute(): boolean {\n\t\t\tif (_state === \"closed\") return true;\n\n\t\t\tif (_state === \"open\") {\n\t\t\t\tconst elapsed = now() - _lastOpenedAt;\n\t\t\t\tif (elapsed >= _lastCooldownNs) {\n\t\t\t\t\t_state = \"half-open\";\n\t\t\t\t\t_halfOpenAttempts = 1;\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tif (_halfOpenAttempts < halfOpenMax) {\n\t\t\t\t_halfOpenAttempts++;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t},\n\n\t\trecordSuccess(): void {\n\t\t\tif (_state === \"half-open\") {\n\t\t\t\t_state = \"closed\";\n\t\t\t\t_failureCount = 0;\n\t\t\t\t_openCycle = 0;\n\t\t\t} else if (_state === \"closed\") {\n\t\t\t\t_failureCount = 0;\n\t\t\t}\n\t\t},\n\n\t\trecordFailure(_error?: unknown): void {\n\t\t\tif (_state === \"half-open\") {\n\t\t\t\t_openCycle++;\n\t\t\t\ttransitionToOpen();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (_state === \"closed\") {\n\t\t\t\t_failureCount++;\n\t\t\t\tif (_failureCount >= threshold) {\n\t\t\t\t\ttransitionToOpen();\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\tget state(): CircuitState {\n\t\t\treturn _state;\n\t\t},\n\n\t\tget failureCount(): number {\n\t\t\treturn _failureCount;\n\t\t},\n\n\t\treset(): void {\n\t\t\t_state = \"closed\";\n\t\t\t_failureCount = 0;\n\t\t\t_openCycle = 0;\n\t\t\t_halfOpenAttempts = 0;\n\t\t},\n\n\t\tdispose(): void {\n\t\t\toptsUnsub?.();\n\t\t},\n\n\t\t// Internal accessors used by withBreaker for the BreakerState\n\t\t// companion (DS-13.5.B). Not part of the public CircuitBreaker\n\t\t// interface but exposed for the bundle wiring.\n\t};\n\t(breaker as unknown as { _stateSnapshot: () => BreakerState })._stateSnapshot = () => ({\n\t\tstatus: _state,\n\t\tfailureCount: _failureCount,\n\t\topenCycle: _openCycle,\n\t\tlastOpenedAtNs: _lastOpenedAt,\n\t\thalfOpenAttempts: _halfOpenAttempts,\n\t\tlastCooldownNs: _lastCooldownNs,\n\t});\n\n\treturn breaker;\n}\n\nexport type WithBreakerBundle<T> = {\n\tnode: Node<T>;\n\tbreakerState: Node<BreakerState>;\n};\n\n/**\n * Returns a unary wrapper that gates upstream `DATA` through a {@link CircuitBreaker}.\n *\n * @param breaker - Shared breaker instance (typically one per resource).\n * @param options - `onOpen: \"skip\"` emits `RESOLVED` when open; `\"error\"` emits {@link CircuitOpenError}.\n * @returns Function mapping `Node<T>` to `{ node, breakerState }` companion nodes.\n *\n * @remarks\n * **Success path:** `COMPLETE` calls {@link CircuitBreaker.recordSuccess}. **Failure path:** upstream `ERROR` calls {@link CircuitBreaker.recordFailure} and is forwarded.\n *\n * **State telemetry:** `breakerState: Node<CircuitState>` is a reactive companion that mirrors `breaker.state` — every transition (`closed`/`open`/`half-open`) emits a `DATA`. Also accessible via `node.meta.breakerState` for `describe()` traversal.\n *\n * @example\n * ```ts\n * import { state, withBreaker, circuitBreaker } from \"@graphrefly/graphrefly-ts\";\n *\n * const b = circuitBreaker({ failureThreshold: 2 });\n * const s = state(1);\n * const { node, breakerState } = withBreaker(b)(s);\n * ```\n *\n * @category extra\n */\nexport function withBreaker<T>(\n\tbreaker: CircuitBreaker,\n\toptions?: { onOpen?: \"skip\" | \"error\"; meta?: Record<string, unknown> },\n): (source: Node<T>) => WithBreakerBundle<T> {\n\tconst onOpen = options?.onOpen ?? \"skip\";\n\tconst callerMeta = options?.meta;\n\n\treturn (source: Node<T>): WithBreakerBundle<T> => {\n\t\tconst snapshot = (breaker as unknown as { _stateSnapshot?: () => BreakerState })._stateSnapshot;\n\t\tconst initialSnapshot: BreakerState = snapshot\n\t\t\t? snapshot()\n\t\t\t: {\n\t\t\t\t\tstatus: breaker.state,\n\t\t\t\t\tfailureCount: breaker.failureCount,\n\t\t\t\t\topenCycle: 0,\n\t\t\t\t\tlastOpenedAtNs: 0,\n\t\t\t\t\thalfOpenAttempts: 0,\n\t\t\t\t\tlastCooldownNs: 0,\n\t\t\t\t};\n\t\tconst wrapped = node<T>(\n\t\t\t[],\n\t\t\t(_deps, a) => {\n\t\t\t\tfunction syncState(): void {\n\t\t\t\t\tconst s = snapshot\n\t\t\t\t\t\t? snapshot()\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tstatus: breaker.state,\n\t\t\t\t\t\t\t\tfailureCount: breaker.failureCount,\n\t\t\t\t\t\t\t\topenCycle: 0,\n\t\t\t\t\t\t\t\tlastOpenedAtNs: 0,\n\t\t\t\t\t\t\t\thalfOpenAttempts: 0,\n\t\t\t\t\t\t\t\tlastCooldownNs: 0,\n\t\t\t\t\t\t\t};\n\t\t\t\t\twrapped.meta.breakerState.down([[DIRTY], [DATA, s]]);\n\t\t\t\t}\n\n\t\t\t\tconst unsub = source.subscribe((msgs) => {\n\t\t\t\t\tfor (const m of msgs) {\n\t\t\t\t\t\tconst t = m[0];\n\t\t\t\t\t\tif (t === DIRTY) a.down([[DIRTY]]);\n\t\t\t\t\t\telse if (t === DATA) {\n\t\t\t\t\t\t\tif (breaker.canExecute()) {\n\t\t\t\t\t\t\t\tsyncState();\n\t\t\t\t\t\t\t\ta.emit(m[1] as T);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tsyncState();\n\t\t\t\t\t\t\t\tif (onOpen === \"error\") a.down([[ERROR, new CircuitOpenError()]]);\n\t\t\t\t\t\t\t\telse a.down([[RESOLVED]]);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if (t === RESOLVED) a.down([[RESOLVED]]);\n\t\t\t\t\t\telse if (t === COMPLETE) {\n\t\t\t\t\t\t\tbreaker.recordSuccess();\n\t\t\t\t\t\t\tsyncState();\n\t\t\t\t\t\t\ta.down([[COMPLETE]]);\n\t\t\t\t\t\t} else if (t === ERROR) {\n\t\t\t\t\t\t\tbreaker.recordFailure(msgVal(m));\n\t\t\t\t\t\t\tsyncState();\n\t\t\t\t\t\t\ta.down([m]);\n\t\t\t\t\t\t} else a.down([m]);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tsyncState();\n\t\t\t\treturn { onDeactivation: unsub };\n\t\t\t},\n\t\t\t{\n\t\t\t\t...operatorOpts(),\n\t\t\t\tmeta: {\n\t\t\t\t\t...(callerMeta ?? {}),\n\t\t\t\t\tbreakerState: initialSnapshot,\n\t\t\t\t\t...factoryTag(\"withBreaker\", { onOpen }),\n\t\t\t\t},\n\t\t\t\tcompleteWhenDepsComplete: false,\n\t\t\t\tinitial: source.cache,\n\t\t\t},\n\t\t);\n\n\t\treturn { node: wrapped, breakerState: wrapped.meta.breakerState as Node<BreakerState> };\n\t};\n}\n","/**\n * `budgetGate` — numeric-constraint flow gate (Tier 2.2 promotion from\n * `patterns/reduction/`).\n *\n * Lives alongside the other `extra/resilience/` flow controls (`retry`,\n * `circuitBreaker`, `rateLimiter`, `tokenBucket`, `fallback`, `withStatus`).\n *\n * @module\n */\n\nimport type { NodeActions } from \"@graphrefly/pure-ts/core\";\nimport {\n\tCOMPLETE,\n\tDATA,\n\tDIRTY,\n\tERROR,\n\ttype Message,\n\ttype Node,\n\ttype NodeOptions,\n\tnode,\n\tPAUSE,\n\tRESOLVED,\n\tRESUME,\n} from \"@graphrefly/pure-ts/core\";\nimport { domainMeta } from \"../../base/meta/domain-meta.js\";\nimport type { GateState } from \"./gate-state.js\";\n\n/** A reactive constraint for {@link budgetGate}. */\nexport type BudgetConstraint<T = unknown> = {\n\t/** Constraint node whose value is checked. */\n\tnode: Node<T>;\n\t/** Returns `true` when the constraint is satisfied (budget available). */\n\tcheck: (value: T) => boolean;\n\t/**\n\t * Optional human-readable name for `BudgetGateState.constraintsSnapshot`.\n\t * Defaults to the constraint Node's `.name` (or `\"\"` when unset).\n\t */\n\tname?: string;\n};\n\n/** Options for {@link budgetGate}. */\nexport type BudgetGateOptions = Omit<NodeOptions<unknown>, \"describeKind\" | \"name\" | \"meta\"> & {\n\tmeta?: Record<string, unknown>;\n};\n\n/**\n * Per-constraint snapshot inside {@link BudgetGateState}. The `value` field is\n * typed as `unknown` because constraint values are generic — most callers\n * carry numeric budgets but the gate doesn't enforce that. Cast at the\n * subscriber site if you need a narrower type.\n *\n * @category extra/resilience\n */\nexport interface BudgetConstraintSnapshot {\n\treadonly name: string;\n\treadonly satisfied: boolean;\n\treadonly value: unknown;\n}\n\n/**\n * Lifecycle-shaped state companion emitted by {@link budgetGate} (DS-13.5.B,\n * locked 2026-05-01). `status` is `\"open\"` when every constraint's `check`\n * returns true; `\"closed\"` otherwise. The `constraintsSnapshot` array\n * preserves constraint ordering and reflects the most recent values seen\n * via per-constraint reactive updates.\n *\n * @category extra/resilience\n */\nexport interface BudgetGateState {\n\treadonly status: GateState;\n\treadonly constraintsSnapshot: ReadonlyArray<BudgetConstraintSnapshot>;\n}\n\n/**\n * Bundle returned by {@link budgetGate}: the gated output node and its\n * gate-state companion. Pre-1.0 break vs. the prior `Node<T>` return —\n * unwrap via `.node` for downstream wiring.\n *\n * @category extra/resilience\n */\nexport interface BudgetGateBundle<T> {\n\tnode: Node<T>;\n\tbudgetGateState: Node<BudgetGateState>;\n}\n\n/**\n * Unbounded head-index queue with O(1) `push` and O(1) `shift`.\n *\n * Distinct from {@link RingBuffer} (drop-oldest, fixed capacity) because\n * `budgetGate` MUST NOT silently drop buffered DATA when the gate is closed —\n * upstream is asked to PAUSE and the buffered items are guaranteed to flush\n * once the constraint relaxes (or on terminal force-flush). A drop-oldest\n * eviction would break that contract by losing items between PAUSE and\n * RESUME.\n *\n * Storage grows on demand and is compacted opportunistically: once the head\n * pointer crosses the midpoint of the underlying array, we slice the consumed\n * prefix away. Memory bound is **worst-case ~3× live size** (DF3, 2026-04-29\n * doc tighten — a queue that grows to N, drains to 0.6N, then re-pushes 0.4N\n * retains ~3× live size between compactions). Amortized footprint trends\n * lower under steady-state usage. Trade-off: keeps `push` and `shift` O(1) —\n * replacing the prior `buffer.slice(1)` per drain which was O(N²) over a\n * long-lived bucket (Tier 3.3.1 fix).\n */\nclass HeadIndexQueue<T> {\n\tprivate buf: T[] = [];\n\tprivate head = 0;\n\n\tget size(): number {\n\t\treturn this.buf.length - this.head;\n\t}\n\n\tpush(item: T): void {\n\t\tthis.buf.push(item);\n\t}\n\n\t/** O(1) — removes and returns the oldest item, or `undefined` when empty. */\n\tshift(): T | undefined {\n\t\tif (this.head >= this.buf.length) return undefined;\n\t\tconst item = this.buf[this.head]!;\n\t\t// Release the slot for GC. Cheaper than splice; cost folded into the\n\t\t// periodic compaction below.\n\t\t(this.buf as Array<T | undefined>)[this.head] = undefined;\n\t\tthis.head++;\n\t\t// Compact when more than half the array is consumed prefix.\n\t\tif (this.head > 32 && this.head * 2 > this.buf.length) {\n\t\t\tthis.buf = this.buf.slice(this.head);\n\t\t\tthis.head = 0;\n\t\t}\n\t\treturn item;\n\t}\n\n\tclear(): void {\n\t\tthis.buf = [];\n\t\tthis.head = 0;\n\t}\n}\n\n/**\n * Pass-through that respects reactive constraint nodes.\n *\n * DATA flows through when all constraints are satisfied. When any constraint\n * is exceeded, `PAUSE` is sent upstream and DATA is buffered in a FIFO queue.\n * When constraints relax, the queue drains in arrival order and `RESUME` is\n * sent upstream.\n *\n * ## Invariants (do not refactor without preserving)\n *\n * 1. **Terminal force-flush.** On `COMPLETE` / `ERROR` arriving from `source`,\n * every buffered item is emitted downstream BEFORE the terminal message is\n * forwarded. The constraint is intentionally bypassed for the flush — once\n * upstream is done, the caller must see the buffered work, not lose it.\n * See COMPOSITION-GUIDE §19 (terminal-emission operators).\n *\n * 2. **PAUSE-release ordering.** When a constraint flips from saturated →\n * released, the queue drains in FIFO order downstream BEFORE `RESUME` is\n * sent upstream. Reversing the order (RESUME-then-drain) would let new\n * upstream DATA interleave with the queue tail, breaking arrival-order\n * delivery. See COMPOSITION-GUIDE §9, §9a (diamond + batch coalescing).\n *\n * 3. **Deferred RESOLVED.** A `RESOLVED` from `source` while the queue is\n * non-empty is held until the queue drains, then forwarded — so downstream\n * sees `[buffered DATA…, RESOLVED]` in causal order rather than\n * `[RESOLVED, buffered DATA…]`.\n *\n * **Stall risk (qa D4):** if the constraint never relaxes AND no terminal\n * arrives from `source`, the deferred RESOLVED is held forever. Downstream\n * consumers that depend on `RESOLVED` for an `awaitSettled`-style\n * coordination wait stall in this case. PAUSE is sent upstream so source\n * backpressure stops further DATA, but the gate itself has no escape\n * hatch — by design (the producer-pattern is fire-and-forget; recovery\n * happens at the compositor level via timeout, retry, or cancellation).\n *\n * 4. **Constraint DIRTY suppression.** Constraint-node DIRTY does NOT\n * propagate downstream — only `source`-DIRTY does. The gate's downstream\n * semantics track `source`'s wave, not constraint waves.\n *\n * 5. **Lazy PAUSE (qa D3).** PAUSE is sent upstream ONLY when a `source` DATA\n * arrives that fails the constraint check (the first blocked item). A\n * constraint flipping closed BEFORE any source DATA arrives does NOT emit\n * a preemptive PAUSE — upstream may push DATA freely until the first\n * item is buffered. This matches the producer-pattern lazy-activation\n * philosophy (don't impose backpressure for hypothetical future blocks).\n * For eager-PAUSE semantics, wrap the gate in a compositor that watches\n * constraints + source independently.\n *\n * ## Queue\n *\n * The internal buffer is an unbounded {@link HeadIndexQueue} (O(1) push,\n * O(1) shift, opportunistic compaction). It does NOT use {@link RingBuffer}\n * because RingBuffer's drop-oldest eviction would silently lose buffered\n * items between PAUSE and RESUME. Backpressure (PAUSE) is the upstream\n * contract for bounding the queue, not capacity-driven eviction here.\n *\n * ## Producer-pattern: source edge is invisible to `describe()`\n *\n * `budgetGate` is constructed via `node([], fn)` and subscribes to `source`\n * and the constraint nodes manually inside its activation fn. Because no\n * dep is declared at construction, **`describe()` shows no edge from\n * `source` (or any constraint) into the returned node** — the gate looks\n * like a standalone leaf source. This is intentional (see COMPOSITION-GUIDE\n * §24 \"Edges are derived, not declared\"): if you want the constraint /\n * source dependency to appear in describe output, surface it at the\n * compositor level (e.g. annotate via `meta.ai.upstream`, or wrap the gate\n * in a parent factory that exposes the deps as constructor args).\n *\n * ## Reference equality + Tier 6.5 3.2.5 locked semantics\n *\n * **Constraint VALUES are reactive.** Each `BudgetConstraint.node` is\n * subscribed at activation; per-value changes flip the gate (re-evaluate\n * in the same wave) and trigger PAUSE/RESUME upstream. Per the locked\n * semantic rule for the reactive-options-widening batch (Tier 6.5 3.2.5,\n * 2026-04-29): \"constraints array re-evaluated immediately against\n * current source; adding/removing constraints triggers gate\n * re-evaluation in the same wave\" — the per-value half is shipped via\n * the existing constraint-Node subscription model.\n *\n * **The constraints ARRAY shape is static.** The factory captures the\n * `constraints` array reference and each `check` function at\n * construction; it does NOT diff subsequent arrays. To add or remove\n * constraints reactively, build the swap at the compositor level (a\n * `switchMap` rebuild over a constraint-shape Node), or construct a new\n * gate. Dynamic constraint-array reactivity is intentionally deferred —\n * the subscription churn (resub on every constraint add/remove) and\n * `latestValues` shape mutation overshoot the budget-gate's\n * fire-and-forget ergonomics.\n *\n * @param source - Input node.\n * @param constraints - Reactive constraint checks. MUST be non-empty.\n * @param opts - Optional node options.\n * @returns Gated node.\n *\n * @throws {RangeError} when `constraints.length === 0`. The gate has no\n * meaningful identity without at least one check — degenerate to plain\n * pass-through (e.g. via `derived([source], ([v]) => v)`) instead.\n *\n * @category resilience\n */\nexport function budgetGate<T>(\n\tsource: Node<T>,\n\tconstraints: ReadonlyArray<BudgetConstraint>,\n\topts?: BudgetGateOptions,\n): BudgetGateBundle<T> {\n\tif (constraints.length === 0) throw new RangeError(\"budgetGate requires at least one constraint\");\n\n\tconst constraintNodes = constraints.map((c) => c.node);\n\tconst allDeps = [source as Node, ...constraintNodes] as Node[];\n\n\tconst buffer = new HeadIndexQueue<T>();\n\tlet paused = false;\n\tlet pendingResolved = false;\n\tconst lockId = Symbol(\"budget-gate\");\n\n\t// Latest DATA from each constraint. Seeded at **activation time** (inside the\n\t// producer fn below) — a wiring-time boundary read, not a reactive-callback\n\t// read — so concurrent constraint updates between factory-time and\n\t// activation-time are reflected before `checkBudget()` first runs. The\n\t// subscribe handler updates this array on each constraint DATA message, so\n\t// `checkBudget` never reads `.cache` from inside a reactive callback.\n\tconst latestValues: unknown[] = new Array(constraints.length);\n\n\tfunction checkBudget(): boolean {\n\t\treturn constraints.every((c, i) => c.check(latestValues[i]));\n\t}\n\n\t// DS-13.5.B (locked 2026-05-01): lifecycle-shaped state companion.\n\t// Initialized with `status: \"closed\"` until activation seeds the values\n\t// and the first `checkBudget()` runs.\n\t//\n\t// QA A3 (2026-05-03): equality uses structural compare on\n\t// `(status, name, satisfied, value)` tuples via `Object.is` per\n\t// `value` — NOT `JSON.stringify`. Caller-supplied constraint values\n\t// (`unknown`) can be circular, BigInt, or otherwise non-serializable;\n\t// `JSON.stringify` would throw and corrupt the wave dispatch.\n\tfunction budgetGateStateEqual(a: BudgetGateState, b: BudgetGateState): boolean {\n\t\tif (a === b) return true;\n\t\tif (a.status !== b.status) return false;\n\t\tconst sa = a.constraintsSnapshot;\n\t\tconst sb = b.constraintsSnapshot;\n\t\tif (sa.length !== sb.length) return false;\n\t\tfor (let i = 0; i < sa.length; i++) {\n\t\t\tconst ai = sa[i];\n\t\t\tconst bi = sb[i];\n\t\t\tif (ai === undefined || bi === undefined) return false;\n\t\t\tif (ai.name !== bi.name) return false;\n\t\t\tif (ai.satisfied !== bi.satisfied) return false;\n\t\t\tif (!Object.is(ai.value, bi.value)) return false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tconst budgetGateState = node<BudgetGateState>([], {\n\t\tname: \"budgetGateState\",\n\t\tdescribeKind: \"state\",\n\t\tinitial: {\n\t\t\tstatus: \"closed\",\n\t\t\tconstraintsSnapshot: constraints.map((c) => ({\n\t\t\t\tname: c.name ?? c.node.name ?? \"\",\n\t\t\t\tsatisfied: false,\n\t\t\t\tvalue: undefined,\n\t\t\t})),\n\t\t},\n\t\tequals: budgetGateStateEqual,\n\t});\n\n\tlet lastEmittedState: BudgetGateState | null = null;\n\n\tfunction publishState(): void {\n\t\tconst snapshot: BudgetConstraintSnapshot[] = constraints.map((c, i) => {\n\t\t\tconst v = latestValues[i];\n\t\t\tlet satisfied = false;\n\t\t\ttry {\n\t\t\t\tsatisfied = c.check(v as never);\n\t\t\t} catch (err) {\n\t\t\t\t// QA A3: log the bug-throw rather than silently mapping to\n\t\t\t\t// `satisfied=false`. The constraint's check function failing\n\t\t\t\t// is a programmer error — at minimum surface it to console.\n\t\t\t\tconsole.error(\n\t\t\t\t\t`budgetGate: constraint \"${c.name ?? c.node.name ?? `[${i}]`}\" check threw; treating as not satisfied.`,\n\t\t\t\t\terr,\n\t\t\t\t);\n\t\t\t\tsatisfied = false;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tname: c.name ?? c.node.name ?? \"\",\n\t\t\t\tsatisfied,\n\t\t\t\tvalue: v,\n\t\t\t};\n\t\t});\n\t\tconst status: GateState = snapshot.every((s) => s.satisfied) ? \"open\" : \"closed\";\n\t\tconst next: BudgetGateState = { status, constraintsSnapshot: snapshot };\n\t\tif (lastEmittedState != null && budgetGateStateEqual(lastEmittedState, next)) {\n\t\t\treturn;\n\t\t}\n\t\tlastEmittedState = next;\n\t\tbudgetGateState.down([[DIRTY], [DATA, next]]);\n\t}\n\n\tfunction flushBuffer(actions: NodeActions): void {\n\t\t// FIFO drain — invariant 2 (PAUSE-release ordering). Stop early if a\n\t\t// later constraint check flips false mid-drain (the queue's tail stays\n\t\t// buffered for the next RESUME).\n\t\twhile (buffer.size > 0 && checkBudget()) {\n\t\t\tconst item = buffer.shift()!;\n\t\t\tactions.emit(item);\n\t\t}\n\t\t// Drain deferred RESOLVED once buffer is empty (invariant 3).\n\t\tif (buffer.size === 0 && pendingResolved) {\n\t\t\tpendingResolved = false;\n\t\t\tactions.down([[RESOLVED]]);\n\t\t}\n\t}\n\n\t// Producer pattern: manually subscribe to all deps for per-message interception.\n\t// Source / constraint edges are intentionally NOT declared as `_deps` — see\n\t// the JSDoc \"Producer-pattern\" section above and COMPOSITION-GUIDE §24.\n\tconst out = node<T>(\n\t\t[],\n\t\t(_data, gateActions) => {\n\t\t\t// Seed `latestValues` at activation (not factory time) so any constraint\n\t\t\t// updates between factory return and first subscribe are captured before\n\t\t\t// source's push-on-subscribe fires `checkBudget()`.\n\t\t\tfor (let i = 0; i < constraints.length; i++) {\n\t\t\t\tlatestValues[i] = constraints[i]!.node.cache;\n\t\t\t}\n\t\t\t// Seed the companion state at activation as well.\n\t\t\tpublishState();\n\t\t\tconst unsubs: Array<() => void> = [];\n\t\t\tfor (let depIdx = 0; depIdx < allDeps.length; depIdx++) {\n\t\t\t\tconst dep = allDeps[depIdx];\n\t\t\t\tunsubs.push(\n\t\t\t\t\tdep.subscribe((msgs) => {\n\t\t\t\t\t\tfor (const msg of msgs) {\n\t\t\t\t\t\t\t_handleBudgetMessage(msg, depIdx, gateActions);\n\t\t\t\t\t\t}\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tonDeactivation: () => {\n\t\t\t\t\tfor (const u of unsubs) u();\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t\t{\n\t\t\t...opts,\n\t\t\tdescribeKind: \"derived\",\n\t\t\tmeta: domainMeta(\"resilience\", \"budget_gate\", opts?.meta),\n\t\t} as NodeOptions<T>,\n\t);\n\n\treturn { node: out, budgetGateState };\n\n\tfunction _handleBudgetMessage(msg: Message, depIndex: number, actions: NodeActions): boolean {\n\t\tconst t = msg[0];\n\n\t\t// Source messages (dep 0)\n\t\tif (depIndex === 0) {\n\t\t\tif (t === DATA) {\n\t\t\t\tif (checkBudget() && buffer.size === 0) {\n\t\t\t\t\tactions.emit(msg[1] as T);\n\t\t\t\t} else {\n\t\t\t\t\tbuffer.push(msg[1] as T);\n\t\t\t\t\tif (!paused) {\n\t\t\t\t\t\tpaused = true;\n\t\t\t\t\t\tactions.up([[PAUSE, lockId]]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (t === DIRTY) {\n\t\t\t\tactions.down([[DIRTY]]);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (t === RESOLVED) {\n\t\t\t\tif (buffer.size === 0) {\n\t\t\t\t\tactions.down([[RESOLVED]]);\n\t\t\t\t} else {\n\t\t\t\t\t// Buffer non-empty: defer RESOLVED until buffer drains (invariant 3).\n\t\t\t\t\tpendingResolved = true;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (t === COMPLETE || t === ERROR) {\n\t\t\t\t// Invariant 1: terminal force-flush. Drain every buffered item\n\t\t\t\t// downstream BEFORE forwarding the terminal — bypass the constraint\n\t\t\t\t// since \"upstream done\" must not lose buffered work.\n\t\t\t\twhile (buffer.size > 0) {\n\t\t\t\t\tactions.emit(buffer.shift()!);\n\t\t\t\t}\n\t\t\t\tpendingResolved = false;\n\t\t\t\t// Release PAUSE lock before forwarding terminal so upstream sees a\n\t\t\t\t// clean release rather than a still-paused terminal.\n\t\t\t\tif (paused) {\n\t\t\t\t\tpaused = false;\n\t\t\t\t\tactions.up([[RESUME, lockId]]);\n\t\t\t\t}\n\t\t\t\tactions.down([msg]);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\n\t\t// Constraint node messages (dep 1+): capture DATA then re-check budget\n\t\tif (t === DATA) {\n\t\t\tlatestValues[depIndex - 1] = msg[1];\n\t\t}\n\t\tif (t === DATA || t === RESOLVED) {\n\t\t\t// qa A2: hoist `checkBudget()` to a local — both branches consult it\n\t\t\t// and `c.check(value)` may be expensive or non-pure (closes over time,\n\t\t\t// counters, etc.); calling it twice was a 2× cost amplifier and an\n\t\t\t// inconsistency risk if the predicate flips between calls.\n\t\t\t//\n\t\t\t// qa A3: each constraint's `c.check(latestValues[i])` runs against\n\t\t\t// the constraint's last cached value. If a constraint's cache is\n\t\t\t// `undefined` (constraint Node hasn't emitted DATA yet OR was\n\t\t\t// activated before any push-on-subscribe), the predicate sees\n\t\t\t// `undefined`. Treat undefined as \"constraint not ready ⇒ closed\"\n\t\t\t// (conservative — don't release the gate on incomplete state).\n\t\t\tconst ok = checkBudget();\n\t\t\tif (ok && buffer.size > 0) {\n\t\t\t\t// Invariant 2: drain FIFO downstream BEFORE releasing PAUSE upstream.\n\t\t\t\tflushBuffer(actions);\n\t\t\t\tif (buffer.size === 0 && paused) {\n\t\t\t\t\tpaused = false;\n\t\t\t\t\tactions.up([[RESUME, lockId]]);\n\t\t\t\t}\n\t\t\t} else if (!ok && !paused && buffer.size > 0) {\n\t\t\t\t// Defensive — buffer.size > 0 implies paused=true under normal flow\n\t\t\t\t// (a buffered source DATA always sets paused). Kept for clarity if\n\t\t\t\t// invariants ever shift.\n\t\t\t\tpaused = true;\n\t\t\t\tactions.up([[PAUSE, lockId]]);\n\t\t\t}\n\t\t\t// DS-13.5.B: re-publish gate state on constraint update.\n\t\t\tif (t === DATA) publishState();\n\t\t\treturn true;\n\t\t}\n\t\tif (t === DIRTY) {\n\t\t\t// Invariant 4: constraint DIRTY does not propagate downstream.\n\t\t\treturn true;\n\t\t}\n\t\tif (t === ERROR) {\n\t\t\t// Constraint error → forward downstream\n\t\t\tactions.down([msg]);\n\t\t\treturn true;\n\t\t}\n\t\tif (t === COMPLETE) {\n\t\t\t// Constraint completed — locked at last value, no-op\n\t\t\treturn true;\n\t\t}\n\t\t// Unknown constraint types → default forwarding\n\t\treturn false;\n\t}\n}\n","/**\n * Fallback — replace upstream ERROR with a static or computed source.\n *\n * Accepts scalar / `Node` / `PromiseLike` / `AsyncIterable` fallbacks; non-Node\n * inputs are routed through `fromAny` so the fallback participates in the\n * reactive protocol uniformly.\n */\n\nimport {\n\tCOMPLETE,\n\tDATA,\n\tDIRTY,\n\tERROR,\n\tfactoryTag,\n\ttype Node,\n\tnode,\n\tRESOLVED,\n\tTEARDOWN,\n} from \"@graphrefly/pure-ts/core\";\nimport { fromAny } from \"@graphrefly/pure-ts/extra\";\nimport {\n\tisAsyncIterable,\n\tisNode,\n\tisThenable,\n\toperatorOpts,\n} from \"../../base/resilience/_internal.js\";\n\n/** Inputs accepted by {@link fallback}. */\nexport type FallbackInput<T> = T | Node<T> | PromiseLike<T> | AsyncIterable<T>;\n\n/**\n * On upstream terminal `ERROR`, switch to a fallback source instead of propagating the error.\n *\n * Accepts any of:\n * - **scalar value** — emits `[[DATA, fb], [COMPLETE]]`\n * - **`Node<T>`** — subscribes and forwards all messages (push-on-subscribe delivers current cache)\n * - **`Promise<T>` / thenable** — resolves into a one-shot `DATA` then `COMPLETE` (via {@link fromAny})\n * - **`AsyncIterable<T>`** — streams each yielded value as `DATA`, then `COMPLETE` (via {@link fromAny})\n *\n * Non-`Node` inputs are routed through {@link fromAny} so the fallback participates in the\n * reactive protocol uniformly. Bare strings, arrays, and other synchronous scalars are treated\n * as single values (NOT split into characters / elements) to avoid the `fromAny`-on-string\n * iteration gotcha.\n *\n * Composes naturally with {@link retry}:\n * `pipe(source, retry({count:3}), fallback(\"default\"))`.\n *\n * @param source - Upstream node.\n * @param fb - Fallback value, node, promise, or async iterable.\n * @returns Node that replaces errors with the fallback.\n *\n * @example\n * ```ts\n * import { fallback, throwError } from \"@graphrefly/graphrefly-ts\";\n *\n * const safe = fallback(throwError(new Error(\"boom\")), \"default\");\n * safe.cache; // \"default\" after subscribe\n * ```\n *\n * @category extra\n */\nexport function fallback<T>(\n\tsource: Node<T>,\n\tfb: FallbackInput<T>,\n\toptions?: { meta?: Record<string, unknown> },\n): Node<T> {\n\tconst callerMeta = options?.meta;\n\treturn node<T>(\n\t\t(_data, a) => {\n\t\t\tlet fallbackUnsub: (() => void) | undefined;\n\t\t\tlet sourceUnsub: (() => void) | undefined;\n\n\t\t\tfunction switchToFallback(): void {\n\t\t\t\tsourceUnsub?.();\n\t\t\t\tsourceUnsub = undefined;\n\t\t\t\tif (isNode(fb) || isThenable(fb) || isAsyncIterable(fb)) {\n\t\t\t\t\tconst fbNode = fromAny(fb as Node<T> | PromiseLike<T> | AsyncIterable<T>);\n\t\t\t\t\tfallbackUnsub = fbNode.subscribe((fMsgs) => {\n\t\t\t\t\t\ta.down(fMsgs);\n\t\t\t\t\t\t// qa A14: clear fallbackUnsub on terminal so the teardown\n\t\t\t\t\t\t// closure doesn't double-call it. Idempotency of\n\t\t\t\t\t\t// fromAny's unsub is implementation-defined; explicit\n\t\t\t\t\t\t// self-clear is safer.\n\t\t\t\t\t\tfor (const fm of fMsgs) {\n\t\t\t\t\t\t\tconst ft = fm[0];\n\t\t\t\t\t\t\tif (ft === COMPLETE || ft === ERROR || ft === TEARDOWN) {\n\t\t\t\t\t\t\t\tfallbackUnsub = undefined;\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\ta.emit(fb as T);\n\t\t\t\t\ta.down([[COMPLETE]]);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tsourceUnsub = source.subscribe((msgs) => {\n\t\t\t\tfor (const m of msgs) {\n\t\t\t\t\tconst t = m[0];\n\t\t\t\t\tif (t === DIRTY) a.down([[DIRTY]]);\n\t\t\t\t\telse if (t === DATA) a.emit(m[1] as T);\n\t\t\t\t\telse if (t === RESOLVED) a.down([[RESOLVED]]);\n\t\t\t\t\telse if (t === COMPLETE) a.down([[COMPLETE]]);\n\t\t\t\t\telse if (t === ERROR) {\n\t\t\t\t\t\tswitchToFallback();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t} else if (t === TEARDOWN) {\n\t\t\t\t\t\tfallbackUnsub?.();\n\t\t\t\t\t\ta.down([m]);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t} else a.down([m]);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tonDeactivation: () => {\n\t\t\t\t\tsourceUnsub?.();\n\t\t\t\t\tfallbackUnsub?.();\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t\t{\n\t\t\t...operatorOpts(),\n\t\t\tinitial: source.cache,\n\t\t\tmeta: { ...(callerMeta ?? {}), ...factoryTag(\"fallback\") },\n\t\t},\n\t);\n}\n","/**\n * Rate limiters — `tokenBucket` (raw meter), `rateLimiter` (operator with\n * bounded queue + reactive backpressure companions), and the re-export of\n * `adaptiveRateLimiter` from its standalone module.\n */\n\nimport {\n\tCOMPLETE,\n\tDATA,\n\tDIRTY,\n\tERROR,\n\tfactoryTag,\n\tmonotonicNs,\n\ttype Node,\n\tnode,\n\tRESOLVED,\n\tResettableTimer,\n\tRingBuffer,\n\tTEARDOWN,\n} from \"@graphrefly/pure-ts/core\";\nimport {\n\tisNode,\n\ttype NodeOrValue,\n\toperatorOpts,\n\tresolveReactiveOption,\n} from \"../../base/resilience/_internal.js\";\nimport { NS_PER_MS, NS_PER_SEC } from \"../../base/resilience/backoff.js\";\nimport type { GateState } from \"./gate-state.js\";\n\n// `adaptiveRateLimiter` lives in extra/adaptive-rate-limiter.ts (kept independent\n// because it has its own internal control-loop machinery).\nexport * from \"./adaptive-rate-limiter.js\";\n\nexport interface TokenBucket {\n\t/**\n\t * Number of tokens currently available (after refill).\n\t *\n\t * **Float-valued.** When `refillPerSecond` is fractional (or `capacity` × elapsed-fraction\n\t * yields a non-integer), the bucket accumulates fractional refill credit between\n\t * `tryConsume`s. Consumers should not assume integer tokens — e.g. with\n\t * `tokenBucket(10, 2.5)` after 100ms of elapsed time `available()` may report `0.25`.\n\t */\n\tavailable(): number;\n\t/** Try to consume `cost` tokens. Returns `true` if successful. */\n\ttryConsume(cost?: number): boolean;\n\t/**\n\t * Return `cost` tokens to the bucket (capped at capacity). Used when a\n\t * multi-bucket admission fails partway — e.g., `adaptiveRateLimiter`\n\t * consumes from an rpm bucket, then a tpm bucket; if tpm fails, call\n\t * `rpmBucket.putBack(requestCost)` so the rpm slot isn't wasted.\n\t * No-op for non-positive `cost`.\n\t */\n\tputBack(cost?: number): void;\n}\n\n/** Optional configuration for {@link tokenBucket}. */\nexport interface TokenBucketOptions {\n\t/**\n\t * Clock function returning **nanoseconds** with `monotonicNs()` semantics\n\t * (monotonically non-decreasing). Default: `monotonicNs` from `core/clock`.\n\t * Override for deterministic tests — eliminates the need for `vi.useFakeTimers`\n\t * to drive token-refill scheduling.\n\t */\n\tclock?: () => number;\n}\n\n/**\n * Token-bucket meter (capacity + refill rate per second). Use with {@link rateLimiter} or custom gates.\n *\n * @param capacity - Maximum tokens (must be positive).\n * @param refillPerSecond - Tokens added per elapsed second (non-negative; may be fractional).\n * @param opts - Optional `clock` override for deterministic testing.\n * @returns {@link TokenBucket} instance.\n *\n * @remarks\n * **Float behavior:** the internal token counter is float-valued — fractional refill\n * accumulates between `tryConsume` calls. See {@link TokenBucket.available} for caveats.\n *\n * **Clock injection:** pass `opts.clock` to drive refill scheduling deterministically\n * in tests. The contract matches {@link circuitBreaker}'s `now` option: must return\n * `monotonicNs()`-style nanoseconds, never `Date.now()` (wall-clock skew breaks\n * elapsed math).\n *\n * @example\n * ```ts\n * import { tokenBucket } from \"@graphrefly/graphrefly-ts\";\n *\n * const bucket = tokenBucket(10, 2); // capacity 10, refill 2 tokens/sec\n * bucket.tryConsume(3); // true — 7 tokens remaining\n * bucket.available(); // ~7 (plus any elapsed refill — float-valued)\n *\n * // Deterministic test:\n * let t = 0;\n * const tb = tokenBucket(5, 1, { clock: () => t });\n * tb.tryConsume(5); // exhausts\n * t = 1_000_000_000; // advance 1s → +1 refill\n * tb.tryConsume(1); // true\n * ```\n *\n * @category extra\n */\nexport function tokenBucket(\n\tcapacity: number,\n\trefillPerSecond: number,\n\topts?: TokenBucketOptions,\n): TokenBucket {\n\tif (capacity <= 0) throw new RangeError(\"capacity must be > 0\");\n\tif (refillPerSecond < 0) throw new RangeError(\"refillPerSecond must be >= 0\");\n\n\tconst clock = opts?.clock ?? monotonicNs;\n\n\tlet tokens = capacity;\n\tlet updatedAt = clock();\n\n\tfunction refill(now: number): void {\n\t\tif (refillPerSecond > 0) {\n\t\t\tconst elapsedNs = now - updatedAt;\n\t\t\ttokens = Math.min(capacity, tokens + (elapsedNs / NS_PER_SEC) * refillPerSecond);\n\t\t}\n\t\tupdatedAt = now;\n\t}\n\n\treturn {\n\t\tavailable(): number {\n\t\t\trefill(clock());\n\t\t\treturn tokens;\n\t\t},\n\t\ttryConsume(cost = 1): boolean {\n\t\t\tif (cost <= 0) return true;\n\t\t\tconst now = clock();\n\t\t\trefill(now);\n\t\t\tif (tokens >= cost) {\n\t\t\t\ttokens -= cost;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t},\n\t\tputBack(cost = 1): void {\n\t\t\tif (cost <= 0) return;\n\t\t\trefill(clock());\n\t\t\ttokens = Math.min(capacity, tokens + cost);\n\t\t},\n\t};\n}\n\nexport type RateLimiterOverflowPolicy = \"drop-oldest\" | \"drop-newest\" | \"error\";\n\nexport type RateLimiterOptions = {\n\t/** Maximum `DATA` emissions per window (must be > 0). */\n\tmaxEvents: number;\n\t/** Window length in nanoseconds (must be > 0). */\n\twindowNs: number;\n\t/**\n\t * Cap on items queued while waiting for token refill.\n\t *\n\t * **Required.** Pass a finite positive integer (>= 1) for a bounded queue, OR\n\t * the literal `Infinity` to opt in to an unbounded queue (caller acknowledges\n\t * the unbounded-memory-growth risk on a high-rate source). Omitting this\n\t * throws at construction time — the silent-unbounded-buffer footgun is the\n\t * most common rateLimiter mis-configuration.\n\t */\n\tmaxBuffer: number;\n\t/** Overflow policy when `maxBuffer` is exceeded. Default: `\"drop-newest\"`. */\n\tonOverflow?: RateLimiterOverflowPolicy;\n\t/**\n\t * Caller-supplied metadata merged into the produced node's `meta` (Tier 5.2\n\t * D8 widening). Use {@link domainMeta} to tag the layer for `describe()` /\n\t * mermaid grouping (e.g. `domainMeta(\"resilient\", \"rate-limit\")`). The\n\t * primitive's own `factoryTag(\"rateLimiter\", opts)` and the `droppedCount`\n\t * / `rateLimitState` companion seeds always win against caller-supplied\n\t * keys so the audit trail can't be silently overwritten.\n\t */\n\tmeta?: Record<string, unknown>;\n};\n\n/**\n * Thrown by {@link rateLimiter} when `onOverflow: \"error\"` and the pending buffer is full.\n *\n * @category extra\n */\nexport class RateLimiterOverflowError extends Error {\n\toverride name = \"RateLimiterOverflowError\";\n\tconstructor(maxBuffer: number) {\n\t\tsuper(`rateLimiter buffer overflow (maxBuffer=${maxBuffer})`);\n\t}\n}\n\n/**\n * Combined runtime state surfaced by {@link rateLimiter} alongside `droppedCount`.\n * Tier 5.2 D7 widening — exposes pending-buffer occupancy and a `paused`\n * flag so consumers can render backpressure (UI), feed `lens.health`, or\n * gate downstream effects.\n */\n/**\n * Lifecycle-shaped state companion emitted by {@link rateLimiter}.\n *\n * **DS-13.5.B widening (2026-05-01).** `status` extends {@link GateState}\n * with `\"throttled\"` (= `paused === true`). Pre-1.0 break vs the prior\n * shape (which omitted `status`).\n *\n * - `\"open\"` — passing through (no buffering, no recent overflow drops).\n * - `\"throttled\"` — at least one item queued awaiting a token refill.\n * - `\"closed\"` — reserved for future terminal lifecycle reporting.\n */\nexport type RateLimiterState = {\n\t/** DS-13.5.B status field — `\"open\" | \"closed\" | \"throttled\"`. */\n\tstatus: GateState | \"throttled\";\n\t/** Cumulative `DATA` items dropped due to overflow since this subscription cycle started. */\n\tdroppedCount: number;\n\t/** Items currently buffered awaiting a token refill. `0` when the limiter is passing through. */\n\tpendingCount: number;\n\t/** `true` when at least one item is queued (the limiter is actively throttling). */\n\tpaused: boolean;\n};\n\nfunction rateLimiterStateEqual(a: RateLimiterState, b: RateLimiterState): boolean {\n\treturn (\n\t\ta.status === b.status &&\n\t\ta.droppedCount === b.droppedCount &&\n\t\ta.pendingCount === b.pendingCount &&\n\t\ta.paused === b.paused\n\t);\n}\n\nconst RATE_LIMITER_INITIAL_STATE: RateLimiterState = Object.freeze({\n\tstatus: \"open\" as const,\n\tdroppedCount: 0,\n\tpendingCount: 0,\n\tpaused: false,\n});\n\n/** Bundle returned by {@link rateLimiter}. */\nexport type RateLimiterBundle<T> = {\n\t/** The throttled stream — at most `maxEvents` `DATA` per `windowNs`. */\n\tnode: Node<T>;\n\t/**\n\t * Reactive companion: count of `DATA` items dropped since the producer\n\t * activated.\n\t *\n\t * - Increments on every drop under any overflow policy (`drop-newest`,\n\t * `drop-oldest`). The `error` policy terminates the stream after a single\n\t * overflow, so `droppedCount` increments at most once in that path.\n\t * - **Lifecycle scoping (qa A1 + EC7):** the counter retains its final\n\t * value through terminal (`COMPLETE` / `ERROR` / `TEARDOWN`) so consumers\n\t * see the final drop count, not zero. The closure-held counter resets to\n\t * `0` only when the producer fn re-runs — which only happens on a new\n\t * subscription cycle, and only if the producer was constructed with\n\t * `resubscribable: true`. The default `rateLimiter` producer is NOT\n\t * resubscribable, so a single producer-fn run is the typical lifetime.\n\t * - Producer-pattern note: this companion is invisible to `describe()`\n\t * traversal from `node` (effect-mirror limitation; same shape as\n\t * `withBreaker.breakerState` and `withStatus.status`). Surface it via\n\t * `node.meta.droppedCount` if you need it in topology snapshots.\n\t */\n\tdroppedCount: Node<number>;\n\t/**\n\t * Reactive companion: combined `{droppedCount, pendingCount, paused}` view.\n\t *\n\t * - `pendingCount` reflects the live buffer occupancy and updates on every\n\t * push / shift / overflow drop; `paused` is shorthand for\n\t * `pendingCount > 0`.\n\t * - Equality-deduped — re-emits only when one of the three fields actually\n\t * changes (so a busy steady-state where every DATA passes immediately\n\t * produces one `paused: false` emission, not one per DATA).\n\t * - **Lifecycle scoping (qa EC7):** same contract as `droppedCount` —\n\t * retains its final value through terminal; resets to the initial\n\t * `{droppedCount: 0, pendingCount: 0, paused: false}` only on a new\n\t * producer-fn run (resubscribable upstream required).\n\t * - Same producer-pattern caveat as `droppedCount` re: `describe()` visibility.\n\t */\n\trateLimitState: Node<RateLimiterState>;\n};\n\n/**\n * Token-bucket rate limiter: at most `maxEvents` `DATA` values per `windowNs`.\n *\n * Uses {@link tokenBucket} internally (capacity = `maxEvents`, refill = `maxEvents / windowSeconds`).\n * Excess items are queued FIFO (in a fixed-capacity {@link RingBuffer} for O(1) push/shift)\n * until a token is available. The queue is bounded by the **required** `maxBuffer` option\n * with a configurable overflow policy.\n *\n * @param source - Upstream node.\n * @param opts - Rate + bounded-buffer configuration. `maxBuffer` is required (use `Infinity` to opt in to unbounded).\n * @returns `{ node, droppedCount }` bundle. Subscribe to `node` for the throttled stream and to `droppedCount` for backpressure pressure.\n *\n * @throws {RangeError} when `maxEvents` / `windowNs` is non-positive, when `maxBuffer` is omitted, or when `maxBuffer` is a finite value < 1.\n *\n * @remarks\n * **Terminal:** `COMPLETE` / `ERROR` cancel the refill timer, drop the pending queue,\n * reset `droppedCount` to `0`, and propagate.\n *\n * @example\n * ```ts\n * import { rateLimiter, state, NS_PER_SEC } from \"@graphrefly/graphrefly-ts\";\n *\n * const src = state(0);\n * // Allow at most 5 DATA values per second; queue up to 100 excess items, drop newest beyond.\n * const { node: limited, droppedCount } = rateLimiter(src, {\n * maxEvents: 5,\n * windowNs: NS_PER_SEC,\n * maxBuffer: 100,\n * });\n * droppedCount.subscribe(([m]) => console.log(\"dropped so far:\", m[1]));\n * ```\n *\n * @category extra\n */\nexport function rateLimiter<T>(\n\tsource: Node<T>,\n\topts: NodeOrValue<RateLimiterOptions>,\n): RateLimiterBundle<T> {\n\t// Eager validation of static-form opts. Reactive-form opts re-validate\n\t// on each emit via `applyOpts` (invalid runtime config keeps the previous\n\t// values rather than throwing — the producer body's swap path never\n\t// throws into the dataplane).\n\tconst isReactive = isNode(opts);\n\tif (!isReactive) {\n\t\tconst o = opts as RateLimiterOptions;\n\t\tif (o.maxEvents <= 0) throw new RangeError(\"maxEvents must be > 0\");\n\t\tif (o.windowNs <= 0) throw new RangeError(\"windowNs must be > 0\");\n\t\tif (o.maxBuffer === undefined) {\n\t\t\tthrow new RangeError(\n\t\t\t\t\"rateLimiter requires explicit maxBuffer (use Infinity to opt in to unbounded)\",\n\t\t\t);\n\t\t}\n\t\tconst isUnbounded0 = o.maxBuffer === Infinity;\n\t\tif (!isUnbounded0 && (!Number.isInteger(o.maxBuffer) || o.maxBuffer < 1)) {\n\t\t\tthrow new RangeError(\"maxBuffer must be a positive integer (or Infinity for unbounded)\");\n\t\t}\n\t}\n\t// Mode (bounded vs unbounded) is locked at construction time per the\n\t// Tier 6.5 3.2.3 swap rule — runtime opt swaps change the cap WITHIN\n\t// the same mode. Toggling between bounded/unbounded requires re-mounting\n\t// the rateLimiter; the queue type is structural, not a tunable. For\n\t// reactive opts we read the FIRST value (cached or undefined) to lock\n\t// the mode; if the cache is undefined at construction we conservatively\n\t// default to bounded with a placeholder cap, and the first emit re-locks.\n\tconst initialOpts: RateLimiterOptions | undefined = isReactive\n\t\t? ((opts as Node<RateLimiterOptions>).cache as RateLimiterOptions | undefined)\n\t\t: (opts as RateLimiterOptions);\n\tconst initialMaxBuffer = initialOpts?.maxBuffer;\n\tconst isUnbounded = initialMaxBuffer === Infinity;\n\n\tconst out = node<T>(\n\t\t(_data, a) => {\n\t\t\t// Mutable closure-state — replaced on each option swap.\n\t\t\tlet maxEvents = initialOpts?.maxEvents ?? 1;\n\t\t\tlet windowNs = initialOpts?.windowNs ?? NS_PER_SEC;\n\t\t\tlet maxBuffer = initialMaxBuffer ?? 1;\n\t\t\tlet onOverflow: RateLimiterOverflowPolicy = initialOpts?.onOverflow ?? \"drop-newest\";\n\t\t\tlet refillPerSec = (maxEvents * NS_PER_SEC) / windowNs;\n\t\t\tlet tokenTimeNs = NS_PER_SEC / refillPerSec;\n\t\t\tlet bucket = tokenBucket(maxEvents, refillPerSec);\n\n\t\t\t// RingBuffer for O(1) push + shift. Unbounded mode falls back to a plain\n\t\t\t// array (RingBuffer requires a positive integer capacity); the caller\n\t\t\t// explicitly opted in via `maxBuffer: Infinity` and accepts the cost.\n\t\t\t// Bounded mode allocates with the INITIAL `maxBuffer`; runtime cap\n\t\t\t// reductions enforce drop-oldest at push time without resizing the ring.\n\t\t\tconst pending: { push: (v: T) => void; shift: () => T | undefined; size: number } =\n\t\t\t\tisUnbounded ? makeArrayQueue<T>() : ringBufferQueue<T>(Math.max(1, maxBuffer));\n\t\t\tconst timer = new ResettableTimer();\n\t\t\tlet terminated = false;\n\t\t\tlet dropped = 0;\n\n\t\t\t// Mirror the dropped counter + combined state to the meta companions.\n\t\t\t// The `emit` call is the same subscribe-callback effect-mirror\n\t\t\t// pattern used by `withBreaker.breakerState` / `withStatus.status`\n\t\t\t// (sanctioned per audit § F.7).\n\t\t\tconst droppedNode = out.meta.droppedCount;\n\t\t\tconst stateNode = out.meta.rateLimitState;\n\t\t\tlet lastState: RateLimiterState = RATE_LIMITER_INITIAL_STATE;\n\t\t\tfunction syncState(): void {\n\t\t\t\tdroppedNode.emit(dropped);\n\t\t\t\tconst isPaused = pending.size > 0;\n\t\t\t\tconst next: RateLimiterState = {\n\t\t\t\t\tstatus: isPaused ? \"throttled\" : \"open\",\n\t\t\t\t\tdroppedCount: dropped,\n\t\t\t\t\tpendingCount: pending.size,\n\t\t\t\t\tpaused: isPaused,\n\t\t\t\t};\n\t\t\t\t// Equality-dedup at the emit boundary so steady-state pass-through\n\t\t\t\t// (every DATA passes immediately — pendingCount stays 0, paused\n\t\t\t\t// stays false) doesn't generate one state DATA per source DATA.\n\t\t\t\tif (!rateLimiterStateEqual(lastState, next)) {\n\t\t\t\t\tlastState = next;\n\t\t\t\t\tstateNode.emit(next);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Reset for this subscription cycle — `dropped` is the closure\n\t\t\t// variable (already 0 at construction); `pending.size` is also 0\n\t\t\t// (fresh queue per producer activation). The companion Node caches\n\t\t\t// may still hold a prior cycle's terminal values, so re-emit the\n\t\t\t// initial state explicitly.\n\t\t\tlastState = RATE_LIMITER_INITIAL_STATE;\n\t\t\tdroppedNode.emit(0);\n\t\t\tstateNode.emit(RATE_LIMITER_INITIAL_STATE);\n\n\t\t\t// Tier 6.5 3.2.3 (2026-04-29): reactive option swap handler.\n\t\t\t// Locked semantics: `maxEvents`/`windowNs` swap rebuilds the\n\t\t\t// token bucket at the next refill window (tokens reset to new\n\t\t\t// capacity, refill rate updates immediately). `maxBuffer` shrink\n\t\t\t// drops oldest pending entries until size ≤ new cap. `onOverflow`\n\t\t\t// swap takes effect at the next overflow check. Mode toggling\n\t\t\t// (bounded ↔ unbounded) is NOT supported — locked at construction.\n\t\t\tconst optMirror = resolveReactiveOption<RateLimiterOptions>(\n\t\t\t\topts as NodeOrValue<RateLimiterOptions>,\n\t\t\t\t(next) => {\n\t\t\t\t\tif (terminated) return;\n\t\t\t\t\tif (next == null) return;\n\t\t\t\t\t// QA A9 (2026-05-03): explicit empty `{}` short-circuit\n\t\t\t\t\t// for symmetry with timeout / retry / circuitBreaker\n\t\t\t\t\t// (DS-13.5.B locked rule: empty `{}` is a no-op — no\n\t\t\t\t\t// rebind, no companion fire). Pre-fix, empty `{}` was\n\t\t\t\t\t// implicitly a no-op via the validation gate's\n\t\t\t\t\t// `next.maxEvents > 0` check on `undefined`; this\n\t\t\t\t\t// makes the rule explicit and resilient to future\n\t\t\t\t\t// validation refactors.\n\t\t\t\t\tif (typeof next === \"object\" && Object.keys(next).length === 0) return;\n\t\t\t\t\t// Validate; if invalid, keep previous values (no throw into dataplane).\n\t\t\t\t\tif (!(next.maxEvents > 0) || !(next.windowNs > 0)) return;\n\t\t\t\t\tconst nextBuf = next.maxBuffer;\n\t\t\t\t\tif (nextBuf === undefined) return;\n\t\t\t\t\tconst nextUnbounded = nextBuf === Infinity;\n\t\t\t\t\tif (nextUnbounded !== isUnbounded) {\n\t\t\t\t\t\t// Mode toggle not supported — skip silently. Caller using\n\t\t\t\t\t\t// reactive opts must keep maxBuffer in the same mode.\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tif (!nextUnbounded && (!Number.isInteger(nextBuf) || nextBuf < 1)) return;\n\n\t\t\t\t\t// qa F-C (Tier 5 /qa pass, 2026-04-29): reactive `maxBuffer`\n\t\t\t\t\t// is monotonically non-increasing. The pending RingBuffer is\n\t\t\t\t\t// allocated once at construction; growing the cap reactively\n\t\t\t\t\t// would let the overflow check pass more pushes than the\n\t\t\t\t\t// ring's capacity → silent drop-oldest at the substrate level\n\t\t\t\t\t// (RingBuffer.push wraps), bypassing our `dropped` counter\n\t\t\t\t\t// and `onOverflow: \"error\"` arm. Reject grow swaps with a\n\t\t\t\t\t// console.warn and keep the previous cap. Shrink stays\n\t\t\t\t\t// supported (drop-oldest below).\n\t\t\t\t\tif (!nextUnbounded && nextBuf > maxBuffer) {\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t`rateLimiter: reactive maxBuffer grow (${maxBuffer} → ${nextBuf}) ` +\n\t\t\t\t\t\t\t\t\"rejected. The pending ring buffer is allocated at construction; \" +\n\t\t\t\t\t\t\t\t\"reactive maxBuffer is monotonically non-increasing. Recreate \" +\n\t\t\t\t\t\t\t\t\"the rateLimiter with the larger cap if growth is required.\",\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tmaxEvents = next.maxEvents;\n\t\t\t\t\twindowNs = next.windowNs;\n\t\t\t\t\tmaxBuffer = nextBuf;\n\t\t\t\t\tonOverflow = next.onOverflow ?? \"drop-newest\";\n\t\t\t\t\trefillPerSec = (maxEvents * NS_PER_SEC) / windowNs;\n\t\t\t\t\ttokenTimeNs = NS_PER_SEC / refillPerSec;\n\t\t\t\t\t// Rebuild bucket — tokens snap to new capacity. The old refill\n\t\t\t\t\t// timer continues to fire `tryEmit` which will use the new\n\t\t\t\t\t// bucket (same closure variable).\n\t\t\t\t\tbucket = tokenBucket(maxEvents, refillPerSec);\n\n\t\t\t\t\t// Drop-oldest until pending.size <= maxBuffer (bounded only).\n\t\t\t\t\tif (!nextUnbounded) {\n\t\t\t\t\t\twhile (pending.size > maxBuffer) {\n\t\t\t\t\t\t\tpending.shift();\n\t\t\t\t\t\t\tdropped += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tsyncState();\n\t\t\t\t},\n\t\t\t);\n\n\t\t\tfunction tryEmit(): void {\n\t\t\t\twhile (pending.size > 0) {\n\t\t\t\t\tif (bucket.tryConsume(1)) {\n\t\t\t\t\t\ta.emit(pending.shift() as T);\n\t\t\t\t\t\tsyncState();\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Wait one full token-refill interval. Avoids calling bucket.available()\n\t\t\t\t\t\t// which would advance the internal refill clock and steal fractional credit.\n\t\t\t\t\t\t// §5.10: setTimeout (not fromTimer) — refill-delay scheduling needs clearTimeout/setTimeout;\n\t\t\t\t\t\t// fromTimer creates a new Node per reset, adding lifecycle overhead per retry.\n\t\t\t\t\t\ttimer.start(Math.max(1, tokenTimeNs / NS_PER_MS), tryEmit);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction recordDrop(): void {\n\t\t\t\tdropped += 1;\n\t\t\t\tsyncState();\n\t\t\t}\n\n\t\t\tfunction resetForTerminal(): void {\n\t\t\t\tterminated = true;\n\t\t\t\ttimer.cancel();\n\t\t\t\t// RingBuffer.clear-equivalent: drain remaining slots so refs GC.\n\t\t\t\twhile (pending.size > 0) pending.shift();\n\t\t\t\t// qa A1: companions retain their last-emitted DATA value\n\t\t\t\t// through terminal (consumer sees the final drop count, not 0).\n\t\t\t\t// The closure-held `dropped` resets to 0 so a re-subscribe\n\t\t\t\t// cycle starts fresh; the activation block above re-emits\n\t\t\t\t// `RATE_LIMITER_INITIAL_STATE` at that point.\n\t\t\t\tdropped = 0;\n\t\t\t}\n\n\t\t\tconst unsub = source.subscribe((msgs) => {\n\t\t\t\tfor (const m of msgs) {\n\t\t\t\t\tif (terminated) return;\n\t\t\t\t\tconst t = m[0];\n\t\t\t\t\tif (t === DIRTY) a.down([[DIRTY]]);\n\t\t\t\t\telse if (t === DATA) {\n\t\t\t\t\t\tif (!isUnbounded && pending.size >= maxBuffer) {\n\t\t\t\t\t\t\tif (onOverflow === \"drop-newest\") {\n\t\t\t\t\t\t\t\trecordDrop();\n\t\t\t\t\t\t\t} else if (onOverflow === \"drop-oldest\") {\n\t\t\t\t\t\t\t\tpending.shift();\n\t\t\t\t\t\t\t\tpending.push(m[1] as T);\n\t\t\t\t\t\t\t\trecordDrop();\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\trecordDrop();\n\t\t\t\t\t\t\t\tresetForTerminal();\n\t\t\t\t\t\t\t\ta.down([[ERROR, new RateLimiterOverflowError(maxBuffer)]]);\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tpending.push(m[1] as T);\n\t\t\t\t\t\t\tsyncState();\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttryEmit();\n\t\t\t\t\t} else if (t === RESOLVED) a.down([[RESOLVED]]);\n\t\t\t\t\telse if (t === COMPLETE) {\n\t\t\t\t\t\tresetForTerminal();\n\t\t\t\t\t\ta.down([[COMPLETE]]);\n\t\t\t\t\t} else if (t === ERROR) {\n\t\t\t\t\t\tresetForTerminal();\n\t\t\t\t\t\ta.down([m]);\n\t\t\t\t\t} else if (t === TEARDOWN) {\n\t\t\t\t\t\tresetForTerminal();\n\t\t\t\t\t\ta.down([m]);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t} else a.down([m]);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tonDeactivation: () => {\n\t\t\t\t\tterminated = true;\n\t\t\t\t\ttimer.cancel();\n\t\t\t\t\tunsub();\n\t\t\t\t\toptMirror.unsub();\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t\t{\n\t\t\t...operatorOpts(),\n\t\t\tinitial: source.cache,\n\t\t\tmeta: {\n\t\t\t\t// Caller-supplied meta first; companion seeds + factoryTag\n\t\t\t\t// override below so they always win.\n\t\t\t\t...(isReactive ? {} : ((opts as RateLimiterOptions).meta ?? {})),\n\t\t\t\tdroppedCount: 0,\n\t\t\t\trateLimitState: RATE_LIMITER_INITIAL_STATE,\n\t\t\t\t...factoryTag(\"rateLimiter\", isReactive ? { reactiveOpts: true } : opts),\n\t\t\t},\n\t\t},\n\t);\n\n\treturn {\n\t\tnode: out,\n\t\tdroppedCount: out.meta.droppedCount as Node<number>,\n\t\trateLimitState: out.meta.rateLimitState as Node<RateLimiterState>,\n\t};\n}\n\n/**\n * RingBuffer-backed queue adapter — exposes the small `{ push, shift, size }`\n * shape rateLimiter needs without leaking the rest of `RingBuffer`'s API.\n */\nfunction ringBufferQueue<T>(capacity: number): {\n\tpush: (v: T) => void;\n\tshift: () => T | undefined;\n\tsize: number;\n} {\n\tconst buf = new RingBuffer<T>(capacity);\n\treturn {\n\t\tpush: (v: T) => buf.push(v),\n\t\tshift: () => buf.shift(),\n\t\tget size(): number {\n\t\t\treturn buf.size;\n\t\t},\n\t} as { push: (v: T) => void; shift: () => T | undefined; size: number };\n}\n\n/**\n * Plain-array fallback queue for `maxBuffer: Infinity`. Accepts the O(N) shift\n * cost — the caller opted in to unbounded growth.\n */\nfunction makeArrayQueue<T>(): {\n\tpush: (v: T) => void;\n\tshift: () => T | undefined;\n\tsize: number;\n} {\n\tconst arr: T[] = [];\n\treturn {\n\t\tpush: (v: T) => {\n\t\t\tarr.push(v);\n\t\t},\n\t\tshift: () => arr.shift(),\n\t\tget size(): number {\n\t\t\treturn arr.length;\n\t\t},\n\t} as { push: (v: T) => void; shift: () => T | undefined; size: number };\n}\n","/**\n * Adaptive rate limiter — reactive, live-tunable, 429-aware.\n *\n * Wraps two `tokenBucket` instances (requests, tokens) with:\n * - Reactive `rpm` / `tpm` knobs that can be re-tuned at runtime via `NodeInput<number>`.\n * - An adaptation signal input (`Node<RateLimitSignal>`) that feeds back\n * provider 429 / retry-after / x-ratelimit-* headers to tighten limits.\n * - A `clampCooldownMs` TTL on signal-induced caps so a transient 429 doesn't\n * permanently throttle — caps decay back to user-configured values after\n * the cooldown elapses.\n * - TPM-miss recovery: consumed RPM tokens are returned to the request\n * bucket when the TPM admit fails, via `TokenBucket.putBack`.\n * - Imperative `acquire()` for bridging to Promise-based call paths\n * (used by the `withRateLimiter` adapter middleware).\n *\n * **Timer policy:** sleeps use `ResettableTimer` (documented spec §5.10\n * escape hatch in `src/extra/timer.ts`) rather than `fromTimer` to avoid\n * allocating a new Node per acquire cycle.\n *\n * Design lives in `docs/optimizations.md` § \"Reactive adaptive rate limiter\".\n */\n\nimport { DATA, monotonicNs, type Node, node, ResettableTimer } from \"@graphrefly/pure-ts/core\";\nimport { fromAny, type NodeInput } from \"@graphrefly/pure-ts/extra\";\nimport { NS_PER_SEC } from \"../../base/resilience/backoff.js\";\nimport { type TokenBucket, tokenBucket } from \"./rate-limiter.js\";\n\n// ---------------------------------------------------------------------------\n// Signal shape\n// ---------------------------------------------------------------------------\n\n/**\n * Rate-limit signal emitted by an adaptation source (e.g., an HTTP 429 parser).\n *\n * Any subset of fields may be present. The adaptive rate limiter uses:\n * - `retryAfterMs` — blocks acquire() for this duration.\n * - `rpmCap` / `tpmCap` — tightens effective rpm/tpm to this value (decays\n * back to the user-configured cap after `clampCooldownMs`).\n * - `usageHint` — updates the last-known rpm/tpm usage ratio for logging.\n */\nexport interface RateLimitSignal {\n\t/** Throttle duration — pause acquire() for this long. */\n\tretryAfterMs?: number;\n\t/** Hard cap for requests-per-minute; effective rpm = min(current, rpmCap) while clamp is active. */\n\trpmCap?: number;\n\t/** Hard cap for tokens-per-minute; effective tpm = min(current, tpmCap) while clamp is active. */\n\ttpmCap?: number;\n\t/** Observed usage-percentage hint (0..1) — for observability, not gating. */\n\tusageHint?: { rpm?: number; tpm?: number };\n\t/** Free-form provider-specific payload. */\n\tmetadata?: Record<string, unknown>;\n}\n\n// ---------------------------------------------------------------------------\n// Options\n// ---------------------------------------------------------------------------\n\nexport interface AdaptiveRateLimiterOptions {\n\tname?: string;\n\t/** Effective requests-per-minute cap. Reactive — reads push-on-subscribe. */\n\trpm?: NodeInput<number>;\n\t/** Effective tokens-per-minute cap. Reactive. */\n\ttpm?: NodeInput<number>;\n\t/** Source of adaptation signals (429 parser output, etc.). */\n\tadaptation?: NodeInput<RateLimitSignal>;\n\t/**\n\t * How long (ms) a signal-induced `rpmCap` / `tpmCap` stays in effect before\n\t * relaxing back to the user-configured value. Default 60_000 (one minute).\n\t * Set to `Infinity` to make signal caps sticky until manually cleared.\n\t * A fresh signal with the same cap resets the cooldown.\n\t */\n\tclampCooldownMs?: number;\n\t/** Burst capacity overshoot above the steady-state rpm/tpm. Default 1 (no burst). */\n\tburstMultiplier?: number;\n}\n\n// ---------------------------------------------------------------------------\n// Bundle\n// ---------------------------------------------------------------------------\n\nexport interface AdaptiveRateLimiterBundle {\n\t/** Effective requests-per-minute (post-signal-clamp). Reactive. */\n\treadonly effectiveRpm: Node<number>;\n\t/** Effective tokens-per-minute (post-signal-clamp). Reactive. */\n\treadonly effectiveTpm: Node<number>;\n\t/** Last adaptation signal observed. */\n\treadonly lastSignal: Node<RateLimitSignal>;\n\t/** Pending `acquire()` callers waiting for capacity. */\n\treadonly pending: Node<number>;\n\t/** Current request-token-bucket fill (approximate). */\n\treadonly rpmAvailable: Node<number>;\n\t/** Current token-bucket fill (approximate). */\n\treadonly tpmAvailable: Node<number>;\n\t/**\n\t * Imperative bridge: wait until `requestCost` request-tokens and\n\t * `tokenCost` tokens are available, then consume them. Honors the\n\t * most recent `retryAfterMs` from adaptation signals. Rejects with\n\t * an `AbortError`-named error if `signal` aborts while waiting.\n\t * `requestCost` defaults to 1; `tokenCost` defaults to 0 (rpm-only gating).\n\t */\n\tacquire(opts?: { requestCost?: number; tokenCost?: number; signal?: AbortSignal }): Promise<void>;\n\t/**\n\t * Feed back observed token usage (post-call) so the TPM bucket reflects\n\t * real consumption rather than the pre-call estimate. A positive `delta`\n\t * debits additional TPM (undershot estimate); a negative `delta` credits\n\t * back overshoot (`putBack`).\n\t */\n\trecordUsage(delta: number): void;\n\t/** Manually feed an adaptation signal — useful for tests. */\n\trecordSignal(sig: RateLimitSignal): void;\n\t/** Dispose internal subscriptions and pending timers. */\n\tdispose(): void;\n}\n\n// ---------------------------------------------------------------------------\n// Error construction\n// ---------------------------------------------------------------------------\n\nfunction makeAbortError(reason: string): Error {\n\tconst err = new Error(reason) as Error & { name: string };\n\terr.name = \"AbortError\";\n\treturn err;\n}\n\n// ---------------------------------------------------------------------------\n// Implementation\n// ---------------------------------------------------------------------------\n\n/**\n * Create an adaptive rate limiter. Compose with any call source via\n * `await limiter.acquire({ requestCost, tokenCost, signal })`.\n */\nexport function adaptiveRateLimiter(\n\topts: AdaptiveRateLimiterOptions = {},\n): AdaptiveRateLimiterBundle {\n\tconst burst = Math.max(1, opts.burstMultiplier ?? 1);\n\tconst clampCooldownMs = opts.clampCooldownMs ?? 60_000;\n\n\t// Resolve reactive rpm/tpm inputs. Callers may pass `NodeInput` which\n\t// could be a literal number or a Node. `fromAny` normalizes to a Node.\n\tconst rpmInputNode =\n\t\topts.rpm != null\n\t\t\t? fromAny(opts.rpm as NodeInput<number>)\n\t\t\t: node<number>([], { initial: Number.POSITIVE_INFINITY });\n\tconst tpmInputNode =\n\t\topts.tpm != null\n\t\t\t? fromAny(opts.tpm as NodeInput<number>)\n\t\t\t: node<number>([], { initial: Number.POSITIVE_INFINITY });\n\n\t// Signal cap state — updated by recordSignal() / adaptation source.\n\t// The decay timer relaxes the cap back to Infinity after `clampCooldownMs`.\n\tconst signalRpmCap = node<number>([], {\n\t\tinitial: Number.POSITIVE_INFINITY,\n\t\tname: \"adaptiveRateLimiter/signalRpmCap\",\n\t});\n\tconst signalTpmCap = node<number>([], {\n\t\tinitial: Number.POSITIVE_INFINITY,\n\t\tname: \"adaptiveRateLimiter/signalTpmCap\",\n\t});\n\tconst lastSignal = node<RateLimitSignal>([], {\n\t\tinitial: {},\n\t\tname: \"adaptiveRateLimiter/lastSignal\",\n\t});\n\n\t// Compute effective rpm/tpm: min of user-configured cap and signal cap.\n\tconst effectiveRpm = node<number>(\n\t\t[rpmInputNode, signalRpmCap],\n\t\t(batchData, actions, ctx) => {\n\t\t\tconst data = batchData.map((batch, i) =>\n\t\t\t\tbatch != null && batch.length > 0 ? batch.at(-1) : ctx.prevData[i],\n\t\t\t);\n\t\t\tactions.emit(Math.min(Number(data[0] ?? Infinity), Number(data[1] ?? Infinity)));\n\t\t},\n\t\t{ name: \"adaptiveRateLimiter/effectiveRpm\", describeKind: \"derived\" },\n\t);\n\tconst effectiveTpm = node<number>(\n\t\t[tpmInputNode, signalTpmCap],\n\t\t(batchData, actions, ctx) => {\n\t\t\tconst data = batchData.map((batch, i) =>\n\t\t\t\tbatch != null && batch.length > 0 ? batch.at(-1) : ctx.prevData[i],\n\t\t\t);\n\t\t\tactions.emit(Math.min(Number(data[0] ?? Infinity), Number(data[1] ?? Infinity)));\n\t\t},\n\t\t{ name: \"adaptiveRateLimiter/effectiveTpm\", describeKind: \"derived\" },\n\t);\n\n\t// Token buckets — rebuilt when effective caps change.\n\tlet rpmBucket: TokenBucket = makeBucket(\n\t\tNumber(rpmInputNode.cache ?? Number.POSITIVE_INFINITY),\n\t\tburst,\n\t);\n\tlet tpmBucket: TokenBucket = makeBucket(\n\t\tNumber(tpmInputNode.cache ?? Number.POSITIVE_INFINITY),\n\t\tburst,\n\t);\n\n\t// A signal `rpmCap`/`tpmCap` of 0 means \"halt admission entirely\" (e.g.,\n\t// some providers emit this during hard quota exhaustion). We honor it by\n\t// marking the bucket as closed via a long throttle-until; the bucket itself\n\t// stays at its previous capacity so decay can relax it naturally.\n\tlet rpmHardStop = false;\n\tlet tpmHardStop = false;\n\n\tconst unsubRpm = effectiveRpm.subscribe((msgs) => {\n\t\tfor (const msg of msgs) {\n\t\t\tif (msg[0] === DATA) {\n\t\t\t\tconst v = Number(msg[1]);\n\t\t\t\tif (Number.isFinite(v) && v > 0) {\n\t\t\t\t\trpmBucket = makeBucket(v, burst);\n\t\t\t\t\trpmHardStop = false;\n\t\t\t\t} else if (v === Infinity) {\n\t\t\t\t\trpmBucket = makeBucket(Infinity, burst);\n\t\t\t\t\trpmHardStop = false;\n\t\t\t\t} else if (v <= 0) {\n\t\t\t\t\t// Hard stop — no admission until cap relaxes.\n\t\t\t\t\trpmHardStop = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t});\n\tconst unsubTpm = effectiveTpm.subscribe((msgs) => {\n\t\tfor (const msg of msgs) {\n\t\t\tif (msg[0] === DATA) {\n\t\t\t\tconst v = Number(msg[1]);\n\t\t\t\tif (Number.isFinite(v) && v > 0) {\n\t\t\t\t\ttpmBucket = makeBucket(v, burst);\n\t\t\t\t\ttpmHardStop = false;\n\t\t\t\t} else if (v === Infinity) {\n\t\t\t\t\ttpmBucket = makeBucket(Infinity, burst);\n\t\t\t\t\ttpmHardStop = false;\n\t\t\t\t} else if (v <= 0) {\n\t\t\t\t\ttpmHardStop = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t});\n\n\t// Throttle-until: set by retryAfterMs signals.\n\tlet throttleUntilNs = 0;\n\n\t// Clamp-decay timers — when they fire, the signal cap is relaxed back to Infinity.\n\tconst rpmDecayTimer = new ResettableTimer();\n\tconst tpmDecayTimer = new ResettableTimer();\n\n\t// Adaptation source subscription.\n\tlet unsubAdapt: (() => void) | undefined;\n\tif (opts.adaptation != null) {\n\t\tconst adaptNode = fromAny(opts.adaptation as NodeInput<RateLimitSignal>);\n\t\tunsubAdapt = adaptNode.subscribe((msgs) => {\n\t\t\tfor (const msg of msgs) {\n\t\t\t\tif (msg[0] === DATA) applySignal(msg[1] as RateLimitSignal);\n\t\t\t}\n\t\t});\n\t}\n\n\tfunction applySignal(sig: RateLimitSignal): void {\n\t\tlastSignal.emit(sig);\n\t\t// Accept `rpmCap`/`tpmCap` of 0 as a valid hard-stop signal. Only\n\t\t// reject non-finite caps (NaN/Infinity).\n\t\tif (sig.rpmCap != null && Number.isFinite(sig.rpmCap) && sig.rpmCap >= 0) {\n\t\t\tsignalRpmCap.emit(sig.rpmCap);\n\t\t\t// Schedule decay. Uses ResettableTimer — each new clamp resets the cooldown.\n\t\t\tif (Number.isFinite(clampCooldownMs) && clampCooldownMs > 0) {\n\t\t\t\trpmDecayTimer.start(clampCooldownMs, () => signalRpmCap.emit(Number.POSITIVE_INFINITY));\n\t\t\t}\n\t\t}\n\t\tif (sig.tpmCap != null && Number.isFinite(sig.tpmCap) && sig.tpmCap >= 0) {\n\t\t\tsignalTpmCap.emit(sig.tpmCap);\n\t\t\tif (Number.isFinite(clampCooldownMs) && clampCooldownMs > 0) {\n\t\t\t\ttpmDecayTimer.start(clampCooldownMs, () => signalTpmCap.emit(Number.POSITIVE_INFINITY));\n\t\t\t}\n\t\t}\n\t\tif (sig.retryAfterMs != null && sig.retryAfterMs > 0) {\n\t\t\tconst resumeAt = monotonicNs() + sig.retryAfterMs * 1_000_000;\n\t\t\tif (resumeAt > throttleUntilNs) throttleUntilNs = resumeAt;\n\t\t}\n\t}\n\n\tconst pending = node<number>([], { initial: 0, name: \"adaptiveRateLimiter/pending\" });\n\tconst rpmAvailableNode = node<number>([], {\n\t\tinitial: Number.POSITIVE_INFINITY,\n\t\tname: \"adaptiveRateLimiter/rpmAvailable\",\n\t});\n\tconst tpmAvailableNode = node<number>([], {\n\t\tinitial: Number.POSITIVE_INFINITY,\n\t\tname: \"adaptiveRateLimiter/tpmAvailable\",\n\t});\n\n\tconst bumpPending = (delta: number): void => {\n\t\tpending.emit((pending.cache ?? 0) + delta);\n\t};\n\tconst refreshAvailable = (): void => {\n\t\trpmAvailableNode.emit(rpmBucket.available());\n\t\ttpmAvailableNode.emit(tpmBucket.available());\n\t};\n\n\tasync function acquire(\n\t\tacquireOpts: { requestCost?: number; tokenCost?: number; signal?: AbortSignal } = {},\n\t): Promise<void> {\n\t\tconst requestCost = acquireOpts.requestCost ?? 1;\n\t\tconst tokenCost = acquireOpts.tokenCost ?? 0;\n\t\tconst abortSignal = acquireOpts.signal;\n\n\t\tbumpPending(1);\n\t\ttry {\n\t\t\twhile (true) {\n\t\t\t\tif (abortSignal?.aborted) throw makeAbortError(\"AdaptiveRateLimiter.acquire aborted\");\n\n\t\t\t\t// Honor retry-after window.\n\t\t\t\tconst now = monotonicNs();\n\t\t\t\tif (throttleUntilNs > now) {\n\t\t\t\t\tconst waitMs = Math.ceil((throttleUntilNs - now) / 1_000_000);\n\t\t\t\t\tawait sleepReactive(waitMs, abortSignal);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\t// Hard-stop (cap=0) → wait for the decay timer to relax.\n\t\t\t\tif ((requestCost > 0 && rpmHardStop) || (tokenCost > 0 && tpmHardStop)) {\n\t\t\t\t\tawait sleepReactive(250, abortSignal);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\t// Capture local refs so a concurrent rpm/tpm cap-change rebuilding\n\t\t\t\t// the bucket doesn't send `putBack` to a different bucket than\n\t\t\t\t// `tryConsume` debited. If the cap relaxes mid-flight, the OLD\n\t\t\t\t// bucket gets the credit (safe — it's closed over a closure the\n\t\t\t\t// new acquires don't see), and new acquires pick up the new\n\t\t\t\t// bucket on their own next iteration.\n\t\t\t\tconst rpmAtAcquire = rpmBucket;\n\t\t\t\tconst tpmAtAcquire = tpmBucket;\n\n\t\t\t\t// Try consume RPM first.\n\t\t\t\tconst gotRpm = rpmAtAcquire.tryConsume(requestCost);\n\t\t\t\tif (!gotRpm) {\n\t\t\t\t\tawait sleepReactive(estimateWaitMs(rpmAtAcquire, requestCost), abortSignal);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// Then TPM — if it fails, return the RPM token (no wasted slot).\n\t\t\t\tconst gotTpm = tokenCost > 0 ? tpmAtAcquire.tryConsume(tokenCost) : true;\n\t\t\t\tif (!gotTpm) {\n\t\t\t\t\trpmAtAcquire.putBack(requestCost);\n\t\t\t\t\tawait sleepReactive(estimateWaitMs(tpmAtAcquire, tokenCost), abortSignal);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\trefreshAvailable();\n\t\t\t\treturn;\n\t\t\t}\n\t\t} finally {\n\t\t\tbumpPending(-1);\n\t\t}\n\t}\n\n\tfunction recordUsage(delta: number): void {\n\t\tif (delta > 0) {\n\t\t\t// Undershoot: debit additional tokens. Non-blocking — if it fails, the\n\t\t\t// next acquire will just wait longer.\n\t\t\ttpmBucket.tryConsume(delta);\n\t\t} else if (delta < 0) {\n\t\t\t// Overshoot: credit back.\n\t\t\ttpmBucket.putBack(-delta);\n\t\t}\n\t\trefreshAvailable();\n\t}\n\n\tfunction dispose(): void {\n\t\tunsubRpm();\n\t\tunsubTpm();\n\t\tunsubAdapt?.();\n\t\trpmDecayTimer.cancel();\n\t\ttpmDecayTimer.cancel();\n\t}\n\n\treturn {\n\t\teffectiveRpm,\n\t\teffectiveTpm,\n\t\tlastSignal,\n\t\tpending,\n\t\trpmAvailable: rpmAvailableNode,\n\t\ttpmAvailable: tpmAvailableNode,\n\t\tacquire,\n\t\trecordUsage,\n\t\trecordSignal: applySignal,\n\t\tdispose,\n\t};\n}\n\n// ---------------------------------------------------------------------------\n// Internals\n// ---------------------------------------------------------------------------\n\nfunction makeBucket(perMinute: number, burst: number): TokenBucket {\n\tif (!Number.isFinite(perMinute) || perMinute === Infinity) {\n\t\treturn tokenBucket(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);\n\t}\n\tconst capacity = Math.max(1, perMinute * burst);\n\tconst refillPerSecond = perMinute / 60;\n\treturn tokenBucket(capacity, refillPerSecond);\n}\n\nfunction estimateWaitMs(bucket: TokenBucket, needed: number): number {\n\tconst have = bucket.available();\n\tconst deficit = Math.max(0, needed - have);\n\tif (deficit <= 0) return 25; // retry quickly; primary path already failed so pacing is forced\n\t// Heuristic: wait 100ms per missing unit, clamped.\n\treturn Math.min(5_000, Math.max(50, deficit * 100));\n}\n\n/**\n * Promise-based sleep using `ResettableTimer` (spec §5.10 escape hatch).\n * Cleanly removes abort listener on both the timer-fires and abort paths;\n * no leaked `AbortSignal.addEventListener` registrations.\n */\nfunction sleepReactive(ms: number, signal?: AbortSignal): Promise<void> {\n\tif (ms <= 0) return Promise.resolve();\n\tif (signal?.aborted) return Promise.reject(makeAbortError(\"AdaptiveRateLimiter.acquire aborted\"));\n\treturn new Promise((resolve, reject) => {\n\t\tconst timer = new ResettableTimer();\n\t\tlet onAbort: (() => void) | undefined;\n\t\tconst cleanup = (): void => {\n\t\t\ttimer.cancel();\n\t\t\tif (signal && onAbort) signal.removeEventListener(\"abort\", onAbort);\n\t\t};\n\t\ttimer.start(ms, () => {\n\t\t\tcleanup();\n\t\t\tresolve();\n\t\t});\n\t\tif (signal) {\n\t\t\tonAbort = (): void => {\n\t\t\t\tcleanup();\n\t\t\t\treject(makeAbortError(\"AdaptiveRateLimiter.acquire aborted\"));\n\t\t\t};\n\t\t\tsignal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t}\n\t});\n}\n\nexport { NS_PER_SEC };\n"],"mappings":";;;;;;;;;;;;;;;;;;AASA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,OACM;AAsCA,IAAM,mBAAN,cAA+B,MAAM;AAAA,EAClC,OAAO;AAAA,EAChB,cAAc;AACb,UAAM,yBAAyB;AAAA,EAChC;AACD;AAoFO,SAAS,eAAe,SAA8D;AAC5F,MAAI,YAAY;AAChB,MAAI,iBAAiB,KAAK;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AA;AAAA;AAAA,QAGL,GAAI,aAAa,CAAC,IAAM,KAA4B,QAAQ,CAAC;AAAA,QAC7D,cAAc;AAAA,QACd,gBAAgB;AAAA,QAChB,GAAGC,YAAW,eAAe,aAAa,EAAE,cAAc,KAAK,IAAI,IAAI;AAAA,MACxE;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AAAA,IACN,MAAM;AAAA,IACN,cAAc,IAAI,KAAK;AAAA,IACvB,gBAAgB,IAAI,KAAK;AAAA,EAC1B;AACD;AAMA,SAAS,gBAAmB,UAI1B;AACD,QAAM,MAAM,IAAI,WAAc,QAAQ;AACtC,SAAO;AAAA,IACN,MAAM,CAAC,MAAS,IAAI,KAAK,CAAC;AAAA,IAC1B,OAAO,MAAM,IAAI,MAAM;AAAA,IACvB,IAAI,OAAe;AAClB,aAAO,IAAI;AAAA,IACZ;AAAA,EACD;AACD;AAMA,SAAS,iBAIP;AACD,QAAM,MAAW,CAAC;AAClB,SAAO;AAAA,IACN,MAAM,CAAC,MAAS;AACf,UAAI,KAAK,CAAC;AAAA,IACX;AAAA,IACA,OAAO,MAAM,IAAI,MAAM;AAAA,IACvB,IAAI,OAAe;AAClB,aAAO,IAAI;AAAA,IACZ;AAAA,EACD;AACD;","names":["COMPLETE","DATA","DIRTY","ERROR","node","RESOLVED","node","DIRTY","DATA","RESOLVED","COMPLETE","ERROR","COMPLETE","DATA","DIRTY","ERROR","factoryTag","node","RESOLVED","node","COMPLETE","ERROR","DIRTY","DATA","RESOLVED","factoryTag","COMPLETE","DATA","DIRTY","ERROR","factoryTag","monotonicNs","node","RESOLVED","ResettableTimer","TEARDOWN","DATA","monotonicNs","node","fromAny","fromAny","node","DATA","monotonicNs","monotonicNs","node","ResettableTimer","DIRTY","DATA","ERROR","RESOLVED","COMPLETE","TEARDOWN","factoryTag"]}
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
compileSpec,
|
|
3
3
|
validateSpec,
|
|
4
4
|
validateSpecAgainstCatalog
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-6MRSX3YK.js";
|
|
6
6
|
|
|
7
7
|
// src/utils/surface/errors.ts
|
|
8
8
|
var SurfaceError = class extends Error {
|
|
@@ -379,4 +379,4 @@ export {
|
|
|
379
379
|
listSnapshots,
|
|
380
380
|
deleteSnapshot
|
|
381
381
|
};
|
|
382
|
-
//# sourceMappingURL=chunk-
|
|
382
|
+
//# sourceMappingURL=chunk-VAZXUK6G.js.map
|
|
@@ -34,15 +34,19 @@ function externalProducer(register, opts) {
|
|
|
34
34
|
cleanup = typeof ret === "function" ? ret : void 0;
|
|
35
35
|
} catch (err) {
|
|
36
36
|
triad.error(err);
|
|
37
|
-
return
|
|
38
|
-
|
|
37
|
+
return {
|
|
38
|
+
onDeactivation: () => {
|
|
39
|
+
active = false;
|
|
40
|
+
}
|
|
39
41
|
};
|
|
40
42
|
}
|
|
41
|
-
return
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
return {
|
|
44
|
+
onDeactivation: () => {
|
|
45
|
+
active = false;
|
|
46
|
+
try {
|
|
47
|
+
cleanup?.();
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
46
50
|
}
|
|
47
51
|
};
|
|
48
52
|
}, sourceOpts(opts));
|
|
@@ -68,10 +72,12 @@ function externalBundle(register, channels, opts) {
|
|
|
68
72
|
const n = node(
|
|
69
73
|
(_data, _a) => {
|
|
70
74
|
activatedCount++;
|
|
71
|
-
return
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
+
return {
|
|
76
|
+
onDeactivation: () => {
|
|
77
|
+
teardownCount++;
|
|
78
|
+
if (activatedCount > 0 && teardownCount >= activatedCount && teardownCount >= channels.length) {
|
|
79
|
+
finishCleanup();
|
|
80
|
+
}
|
|
75
81
|
}
|
|
76
82
|
};
|
|
77
83
|
},
|
|
@@ -125,4 +131,4 @@ export {
|
|
|
125
131
|
externalProducer,
|
|
126
132
|
externalBundle
|
|
127
133
|
};
|
|
128
|
-
//# sourceMappingURL=chunk-
|
|
134
|
+
//# sourceMappingURL=chunk-VLDRAMP7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/base/composition/external-register.ts"],"sourcesContent":["/**\n * External-register helpers — the common `register({emit, error, complete})`\n * contract shared by webhook, MCP, syslog, StatsD, OTel and other callback-\n * based integrations. Absorbs the `active` flag that every such adapter needs\n * to guard against emits after teardown (§5.10 boundary pattern).\n *\n * Two shapes:\n *\n * - {@link externalProducer} — single channel. Lazy activation: the register\n * fn runs when the node gains its first subscriber; its returned cleanup\n * runs on deactivation.\n *\n * - {@link externalBundle} — multiple named channels. Eager activation: the\n * register fn runs at bundle construction time so externally-owned servers\n * (HTTP endpoints, UDP sockets) start accepting traffic immediately. A\n * shared refcount fires the returned cleanup once every channel has fully\n * torn down.\n */\n\nimport {\n\tbatch,\n\tCOMPLETE,\n\tDATA,\n\tERROR,\n\ttype Node,\n\ttype NodeOptions,\n\tnode,\n} from \"@graphrefly/pure-ts/core\";\n\ntype ExtraOpts = Omit<NodeOptions<unknown>, \"describeKind\">;\n\nfunction sourceOpts<T>(opts?: ExtraOpts): NodeOptions<T> {\n\treturn { describeKind: \"producer\", ...opts } as NodeOptions<T>;\n}\n\n/**\n * Standard emit-triad passed to a single-channel external registrar.\n *\n * Post-teardown calls on any of these are automatically no-ops — the\n * registrar does not need its own guard flag.\n *\n * @category extra\n */\nexport type EmitTriad<T> = {\n\t/** Emit a value as `DATA`. */\n\temit: (value: T) => void;\n\t/** Terminate with `ERROR`. Subsequent `emit` / `error` / `complete` are ignored. */\n\terror: (err: unknown) => void;\n\t/** Terminate with `COMPLETE`. Subsequent `emit` / `error` / `complete` are ignored. */\n\tcomplete: () => void;\n};\n\n/**\n * Multi-channel emit bundle. Each declared channel name maps to an emit fn;\n * `error` and `complete` terminate every channel atomically.\n *\n * @category extra\n */\nexport type BundleTriad<TChannels extends Record<string, unknown>> = {\n\t[K in keyof TChannels]: (value: TChannels[K]) => void;\n} & {\n\t/** Terminate every channel with `ERROR`. */\n\terror: (err: unknown) => void;\n\t/** Terminate every channel with `COMPLETE`. */\n\tcomplete: () => void;\n};\n\n/**\n * Generic external registrator contract. The caller installs handlers into a\n * third-party library / framework / server and optionally returns a cleanup\n * callback. Returning `undefined` / `void` is equivalent to a no-op cleanup.\n *\n * @category extra\n */\nexport type ExternalRegister<H> = (handlers: H) => (() => void) | undefined;\n\n/**\n * Wraps a callback-style external integration as a reactive source.\n *\n * The registrar installs the supplied `emit` / `error` / `complete` handlers\n * into the external SDK; post-teardown calls are silently dropped. Synchronous\n * exceptions thrown by the registrar surface as terminal `ERROR`.\n *\n * @param register - Installs handlers. Optionally returns a cleanup fn.\n * @param opts - Node options (name, equals, resubscribable, ...).\n *\n * @example\n * ```ts\n * import { externalProducer } from \"@graphrefly/graphrefly-ts\";\n *\n * const hook$ = externalProducer<Payload>(({ emit, error }) => {\n * const id = transport.onMessage((raw) => {\n * try { emit(parse(raw)); } catch (e) { error(e); }\n * });\n * return () => transport.off(id);\n * });\n * ```\n *\n * @category extra\n */\nexport function externalProducer<T = unknown>(\n\tregister: ExternalRegister<EmitTriad<T>>,\n\topts?: ExtraOpts,\n): Node<T> {\n\treturn node<T>((_data, a) => {\n\t\tlet active = true;\n\t\tconst triad: EmitTriad<T> = {\n\t\t\temit(value) {\n\t\t\t\tif (!active) return;\n\t\t\t\ta.emit(value);\n\t\t\t},\n\t\t\terror(err) {\n\t\t\t\tif (!active) return;\n\t\t\t\tactive = false;\n\t\t\t\ta.down([[ERROR, err]]);\n\t\t\t},\n\t\t\tcomplete() {\n\t\t\t\tif (!active) return;\n\t\t\t\tactive = false;\n\t\t\t\ta.down([[COMPLETE]]);\n\t\t\t},\n\t\t};\n\t\tlet cleanup: (() => void) | undefined;\n\t\ttry {\n\t\t\tconst ret = register(triad);\n\t\t\tcleanup = typeof ret === \"function\" ? ret : undefined;\n\t\t} catch (err) {\n\t\t\ttriad.error(err);\n\t\t\treturn {\n\t\t\t\tonDeactivation: () => {\n\t\t\t\t\tactive = false;\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\tonDeactivation: () => {\n\t\t\t\tactive = false;\n\t\t\t\ttry {\n\t\t\t\t\tcleanup?.();\n\t\t\t\t} catch {\n\t\t\t\t\t/* registrar cleanup failure is not a reactive signal */\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t}, sourceOpts(opts));\n}\n\n/**\n * Options for {@link externalBundle}.\n *\n * @category extra\n */\nexport type ExternalBundleOptions<TChannels extends Record<string, unknown>> = {\n\t/** Base name prefix for channel nodes; each node is named `${name}::${channel}`. */\n\tname?: string;\n\t/** Per-channel node options (equals, resubscribable, ...). */\n\tchannelOpts?: { [K in keyof TChannels]?: ExtraOpts };\n};\n\n/**\n * Multi-channel variant — one `Node<T>` per named channel, sharing a single\n * registrar. Activation is eager: the registrar runs at construction time so\n * externally-owned servers (HTTP, UDP, queue consumers) can start accepting\n * traffic immediately. The returned cleanup fires once every channel has been\n * subscribed and then fully deactivated (refcount-on-teardown).\n *\n * Any call to `error` or `complete` propagates to every channel atomically.\n *\n * @param register - Installs handlers for each channel plus shared error/complete.\n * @param channels - Ordered channel names; determines the returned object shape.\n * @param opts - Optional name prefix and per-channel node options.\n *\n * @example\n * ```ts\n * import { externalBundle } from \"@graphrefly/graphrefly-ts\";\n *\n * type OTelChannels = { traces: Span; metrics: Metric; logs: LogRec };\n * const otel = externalBundle<OTelChannels>(\n * ({ traces, metrics, logs, error }) => {\n * app.post(\"/v1/traces\", (req, res) => { traces(req.body); res.sendStatus(200); });\n * app.post(\"/v1/metrics\", (req, res) => { metrics(req.body); res.sendStatus(200); });\n * app.post(\"/v1/logs\", (req, res) => { logs(req.body); res.sendStatus(200); });\n * server.on(\"error\", error);\n * return () => server.close();\n * },\n * [\"traces\", \"metrics\", \"logs\"],\n * );\n * otel.traces.subscribe(...);\n * ```\n *\n * @category extra\n */\nexport function externalBundle<TChannels extends Record<string, unknown>>(\n\tregister: ExternalRegister<BundleTriad<TChannels>>,\n\tchannels: readonly (keyof TChannels & string)[],\n\topts?: ExternalBundleOptions<TChannels>,\n): { [K in keyof TChannels]: Node<TChannels[K]> } & { dispose(): void } {\n\tlet active = true;\n\tlet cleanup: (() => void) | undefined;\n\tlet activatedCount = 0;\n\tlet teardownCount = 0;\n\n\tconst nodes = {} as { [K in keyof TChannels]: Node<TChannels[K]> };\n\tconst channelNodes: Array<Node<unknown>> = [];\n\n\tconst finishCleanup = () => {\n\t\tconst fn = cleanup;\n\t\tcleanup = undefined;\n\t\ttry {\n\t\t\tfn?.();\n\t\t} catch {\n\t\t\t/* registrar cleanup failure is not a reactive signal */\n\t\t}\n\t};\n\n\tfor (const ch of channels) {\n\t\tconst name = opts?.name ? `${opts.name}::${ch}` : ch;\n\t\tconst chOpts = opts?.channelOpts?.[ch];\n\t\tconst n = node<TChannels[typeof ch]>(\n\t\t\t(_data, _a) => {\n\t\t\t\tactivatedCount++;\n\t\t\t\treturn {\n\t\t\t\t\tonDeactivation: () => {\n\t\t\t\t\t\tteardownCount++;\n\t\t\t\t\t\t// Cleanup fires once every channel has activated at least once\n\t\t\t\t\t\t// and then deactivated. Channels that never subscribe do not\n\t\t\t\t\t\t// gate cleanup — use the explicit `.dispose()` method for\n\t\t\t\t\t\t// unconditional teardown.\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tactivatedCount > 0 &&\n\t\t\t\t\t\t\tteardownCount >= activatedCount &&\n\t\t\t\t\t\t\tteardownCount >= channels.length\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tfinishCleanup();\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t},\n\t\t\tsourceOpts({ ...chOpts, name }),\n\t\t);\n\t\tnodes[ch as keyof TChannels] = n as Node<TChannels[typeof ch]>;\n\t\tchannelNodes.push(n as Node<unknown>);\n\t}\n\n\tconst bundle = {} as BundleTriad<TChannels>;\n\tfor (const ch of channels) {\n\t\t(bundle as Record<string, unknown>)[ch] = (value: unknown) => {\n\t\t\tif (!active) return;\n\t\t\t(nodes[ch as keyof TChannels] as Node<unknown>).down([[DATA, value]]);\n\t\t};\n\t}\n\tbundle.error = (err: unknown) => {\n\t\tif (!active) return;\n\t\tactive = false;\n\t\tbatch(() => {\n\t\t\tfor (const n of channelNodes) n.down([[ERROR, err]]);\n\t\t});\n\t\tfinishCleanup();\n\t};\n\tbundle.complete = () => {\n\t\tif (!active) return;\n\t\tactive = false;\n\t\tbatch(() => {\n\t\t\tfor (const n of channelNodes) n.down([[COMPLETE]]);\n\t\t});\n\t\tfinishCleanup();\n\t};\n\n\t// Eager activation — register fires at construction time so externally-\n\t// owned servers can start accepting traffic immediately. Synchronous throws\n\t// propagate to the caller (no subscribers exist yet, so there is no\n\t// reactive ERROR path to deliver to). This matches the existing `fromOTel`\n\t// contract.\n\tconst ret = register(bundle);\n\tcleanup = typeof ret === \"function\" ? ret : undefined;\n\n\tconst dispose = () => {\n\t\tif (!active) return;\n\t\tactive = false;\n\t\t// Fire COMPLETE on every channel so downstream sees a clean terminal.\n\t\tbatch(() => {\n\t\t\tfor (const n of channelNodes) {\n\t\t\t\ttry {\n\t\t\t\t\tn.down([[COMPLETE]]);\n\t\t\t\t} catch {\n\t\t\t\t\t/* terminal filter / re-entrance — swallow */\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\tfinishCleanup();\n\t};\n\n\treturn Object.assign(nodes, { dispose });\n}\n"],"mappings":";AAmBA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAGA;AAAA,OACM;AAIP,SAAS,WAAc,MAAkC;AACxD,SAAO,EAAE,cAAc,YAAY,GAAG,KAAK;AAC5C;AAmEO,SAAS,iBACf,UACA,MACU;AACV,SAAO,KAAQ,CAAC,OAAO,MAAM;AAC5B,QAAI,SAAS;AACb,UAAM,QAAsB;AAAA,MAC3B,KAAK,OAAO;AACX,YAAI,CAAC,OAAQ;A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|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-VP3TIUDF.js";
|
|
5
5
|
import {
|
|
6
6
|
refineLoop
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-PTWADEH3.js";
|
|
8
8
|
import {
|
|
9
9
|
QUEUE_NAMES
|
|
10
10
|
} from "./chunk-OIWU3NYV.js";
|
|
11
11
|
import {
|
|
12
12
|
topic
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-NPRP3MCV.js";
|
|
14
14
|
|
|
15
15
|
// src/presets/harness/actor-pool.ts
|
|
16
16
|
import { node } from "@graphrefly/pure-ts/core";
|
|
@@ -571,4 +571,4 @@ export {
|
|
|
571
571
|
harnessProfile,
|
|
572
572
|
harnessTrace
|
|
573
573
|
};
|
|
574
|
-
//# sourceMappingURL=chunk-
|
|
574
|
+
//# sourceMappingURL=chunk-VNXAF2KE.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
aiMeta
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-MS3WPRJR.js";
|
|
4
4
|
|
|
5
5
|
// src/presets/ai/context/index.ts
|
|
6
6
|
import { node, wallClockNs } from "@graphrefly/pure-ts/core";
|
|
@@ -208,4 +208,4 @@ export {
|
|
|
208
208
|
tierCompress,
|
|
209
209
|
renderContextView
|
|
210
210
|
};
|
|
211
|
-
//# sourceMappingURL=chunk-
|
|
211
|
+
//# sourceMappingURL=chunk-VP3TIUDF.js.map
|
|
@@ -4,19 +4,19 @@ import {
|
|
|
4
4
|
fallback,
|
|
5
5
|
rateLimiter,
|
|
6
6
|
withBreaker
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-T7SP3EYR.js";
|
|
8
8
|
import {
|
|
9
9
|
withTimeout
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-GUNIRPEJ.js";
|
|
11
11
|
import {
|
|
12
12
|
domainMeta
|
|
13
13
|
} from "./chunk-FMPF42Q4.js";
|
|
14
14
|
import {
|
|
15
15
|
withStatus
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-JA67ZQG2.js";
|
|
17
17
|
import {
|
|
18
18
|
retry
|
|
19
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-KUFXLAEY.js";
|
|
20
20
|
import {
|
|
21
21
|
NS_PER_MS
|
|
22
22
|
} from "./chunk-P5LBT622.js";
|
|
@@ -253,4 +253,4 @@ export {
|
|
|
253
253
|
ResilientPipelineGraph,
|
|
254
254
|
resilientPipeline
|
|
255
255
|
};
|
|
256
|
-
//# sourceMappingURL=chunk-
|
|
256
|
+
//# sourceMappingURL=chunk-WGDEBIP4.js.map
|