@osfactory/otel-hook 0.1.0
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/LICENSE +21 -0
- package/README.md +1020 -0
- package/dist/adapter-DIgRACtL.d.ts +303 -0
- package/dist/cli.js +12607 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +233 -0
- package/dist/config.js +996 -0
- package/dist/config.js.map +1 -0
- package/dist/content-CoB7aNP1.d.ts +118 -0
- package/dist/diagnostics.d.ts +58 -0
- package/dist/diagnostics.js +46 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/errors.d.ts +91 -0
- package/dist/errors.js +322 -0
- package/dist/errors.js.map +1 -0
- package/dist/events-VxBnDFaO.d.ts +2044 -0
- package/dist/filesystem-store-J8jjDMTh.d.ts +65 -0
- package/dist/hook-CGpo-vks.d.ts +292 -0
- package/dist/identity-BrVBs901.d.ts +227 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +11798 -0
- package/dist/index.js.map +1 -0
- package/dist/install.d.ts +1021 -0
- package/dist/install.js +3271 -0
- package/dist/install.js.map +1 -0
- package/dist/integration.d.ts +303 -0
- package/dist/integration.js +5942 -0
- package/dist/integration.js.map +1 -0
- package/dist/keys-Dej5bN3L.d.ts +7 -0
- package/dist/lifecycle.d.ts +428 -0
- package/dist/lifecycle.js +1077 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/memory-DXVBc8V9.d.ts +94 -0
- package/dist/model.d.ts +45 -0
- package/dist/model.js +1023 -0
- package/dist/model.js.map +1 -0
- package/dist/otlp-log-sink-CeIFDDpn.d.ts +457 -0
- package/dist/payload-CHr53E_v.d.ts +201 -0
- package/dist/policy-D1CrlYn3.d.ts +73 -0
- package/dist/ports-Q8x0ODhs.d.ts +200 -0
- package/dist/primitives-CJ8-rn-f.d.ts +75 -0
- package/dist/privacy.d.ts +51 -0
- package/dist/privacy.js +633 -0
- package/dist/privacy.js.map +1 -0
- package/dist/providers.d.ts +257 -0
- package/dist/providers.js +4398 -0
- package/dist/providers.js.map +1 -0
- package/dist/registry-BzYmC9OC.d.ts +49 -0
- package/dist/runtime.d.ts +32 -0
- package/dist/runtime.js +2907 -0
- package/dist/runtime.js.map +1 -0
- package/dist/schema-Cn0-xz8L.d.ts +224 -0
- package/dist/semconv-Baoz-0OI.d.ts +188 -0
- package/dist/service-j4jEO6cZ.d.ts +68 -0
- package/dist/state.d.ts +36 -0
- package/dist/state.js +710 -0
- package/dist/state.js.map +1 -0
- package/dist/taxonomy-DwhTtuWd.d.ts +87 -0
- package/dist/telemetry.d.ts +77 -0
- package/dist/telemetry.js +2294 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/testing.d.ts +155 -0
- package/dist/testing.js +6513 -0
- package/dist/testing.js.map +1 -0
- package/package.json +124 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { O as OtelHookConfig } from './schema-Cn0-xz8L.js';
|
|
2
|
+
import { OverallHealth } from './diagnostics.js';
|
|
3
|
+
import { OtelHookErrorInfo } from './errors.js';
|
|
4
|
+
import { DeliveryClaimOutcome, UsageAccumulatorSnapshot, LifecycleCleanupReport, CallbackDeduplicator, UsageAccumulator, SpanCorrelator, LifecycleJanitor } from './lifecycle.js';
|
|
5
|
+
import { P as PrivacyService } from './service-j4jEO6cZ.js';
|
|
6
|
+
import { P as ProviderRegistry } from './registry-BzYmC9OC.js';
|
|
7
|
+
import { a as DeliveryOrigin, c as DeliveryUnavailableReason, H as HookIngestInput, e as HookIngestOutcome, U as UsageObservation, O as OtelHook } from './hook-CGpo-vks.js';
|
|
8
|
+
import { C as Clock, I as IdGenerator, c as Logger } from './ports-Q8x0ODhs.js';
|
|
9
|
+
import { F as FilesystemStateStore } from './filesystem-store-J8jjDMTh.js';
|
|
10
|
+
import { o as OtlpTelemetrySink, n as OtlpLogTelemetrySink, b as DurableSpool, D as DurableLogSpool } from './otlp-log-sink-CeIFDDpn.js';
|
|
11
|
+
import 'zod';
|
|
12
|
+
import './primitives-CJ8-rn-f.js';
|
|
13
|
+
import './taxonomy-DwhTtuWd.js';
|
|
14
|
+
import '@opentelemetry/api';
|
|
15
|
+
import './events-VxBnDFaO.js';
|
|
16
|
+
import './semconv-Baoz-0OI.js';
|
|
17
|
+
import '@opentelemetry/core';
|
|
18
|
+
import '@opentelemetry/resources';
|
|
19
|
+
import '@opentelemetry/sdk-trace';
|
|
20
|
+
import './content-CoB7aNP1.js';
|
|
21
|
+
import './policy-D1CrlYn3.js';
|
|
22
|
+
import './adapter-DIgRACtL.js';
|
|
23
|
+
import './identity-BrVBs901.js';
|
|
24
|
+
import './keys-Dej5bN3L.js';
|
|
25
|
+
import '@opentelemetry/sdk-logs';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A hook process lives for milliseconds and then exits, but the facts it needs
|
|
29
|
+
* — the previous cumulative token baseline, the next sequence number, a running
|
|
30
|
+
* per-scope rollup, which deliveries have already been handled — span a whole
|
|
31
|
+
* session. This module is the wiring that makes those two facts coexist without
|
|
32
|
+
* a daemon: filesystem-backed state, a filesystem spool for batches an
|
|
33
|
+
* unreachable collector refused, and a *bounded* flush so a hanging collector
|
|
34
|
+
* costs the host a known number of milliseconds rather than a stalled agent.
|
|
35
|
+
*
|
|
36
|
+
* ## Delivery deduplication
|
|
37
|
+
*
|
|
38
|
+
* A redelivered callback must not be exported or accounted twice, and it must
|
|
39
|
+
* stay suppressed across a process restart — which rules out anything held in
|
|
40
|
+
* memory. Two identities can carry that:
|
|
41
|
+
*
|
|
42
|
+
* 1. An explicit, host-supplied delivery id ({@link HookProcessInput.delivery}),
|
|
43
|
+
* unique by construction because only the host knows it.
|
|
44
|
+
* 2. One normalized from payload fields the selected adapter vouches for — a
|
|
45
|
+
* `tool_use_id`, an `agent_id`, a `prompt_id`. Adapters declare coverage as
|
|
46
|
+
* `deliveryIdentifier` and report components per callback, and the runtime
|
|
47
|
+
* digests them into an opaque, installation-and-session-scoped pair.
|
|
48
|
+
*
|
|
49
|
+
* Neither `invocationId` nor `eventId` can serve: some adapters seed the former
|
|
50
|
+
* with a clock reading (Claude Code documents each hook firing as a distinct
|
|
51
|
+
* invocation), and the latter is seeded with a session sequence number that has
|
|
52
|
+
* already advanced by the time a redelivery arrives.
|
|
53
|
+
*
|
|
54
|
+
* Ownership is taken *before* `ingest` and committed only once the batch is
|
|
55
|
+
* somewhere durable: a delivery arriving inside the window sees the claim and
|
|
56
|
+
* stands down, and a batch that reached neither the collector nor the spool
|
|
57
|
+
* releases its claim for retry instead of committing a loss.
|
|
58
|
+
*
|
|
59
|
+
* **This is at-least-once export, not exactly-once.** The collector is a separate
|
|
60
|
+
* system and there is no transaction spanning an OTLP acceptance and a local state
|
|
61
|
+
* write, so a process killed after the collector took a batch and before the claim
|
|
62
|
+
* commits will re-export on redelivery. That ordering is deliberate: a duplicate
|
|
63
|
+
* carries the same derived trace and span id and can be dropped at the collector,
|
|
64
|
+
* whereas committing first would turn the same window into silent loss. Local
|
|
65
|
+
* accounting *is* at-most-once, because it commits under the claim's own lock.
|
|
66
|
+
*
|
|
67
|
+
* ## Ordering and locks
|
|
68
|
+
*
|
|
69
|
+
* Two locks, always acquired in one order — delivery-scope lock, then state lock —
|
|
70
|
+
* and never nested one state lock inside another:
|
|
71
|
+
*
|
|
72
|
+
* - An in-process lock on the delivery scope serializes same-scope deliveries in
|
|
73
|
+
* arrival order, so deduplication never reorders what it lets through.
|
|
74
|
+
* - `ingest` holds the store's *cross-process* session lock across the session
|
|
75
|
+
* state critical section, which is what stops two hook processes from stamping
|
|
76
|
+
* two batches with one sequence range. Export happens after that lock is
|
|
77
|
+
* released: the span correlator takes the same session lock from inside the sink,
|
|
78
|
+
* and it is not reentrant.
|
|
79
|
+
*
|
|
80
|
+
* ## Deliberate limitations
|
|
81
|
+
*
|
|
82
|
+
* 1. **Deduplication coverage is per provider, and partial for every adapter.** An
|
|
83
|
+
* adapter that declares `deliveryIdentifier: "partial"` identifies only the
|
|
84
|
+
* callbacks carrying a field that separates a genuine second firing from a
|
|
85
|
+
* redelivery — for most that means per-tool-call and per-subagent edges, and for
|
|
86
|
+
* the Gemini CLI, whose protocol has no such field at all, only the
|
|
87
|
+
* once-per-session edges. The rest are exported without the at-most-once
|
|
88
|
+
* guarantee unless the host supplies a delivery id; with `requireCallbackId` the
|
|
89
|
+
* runtime says so per callback instead of leaving it to be inferred.
|
|
90
|
+
* 2. **Cross-process span pairing needs a durable state root.** The
|
|
91
|
+
* {@link SpanCorrelator} is wired into the sink, so a `*.end` emitted by a
|
|
92
|
+
* later process carries its start time, duration, parent, and start-only
|
|
93
|
+
* attributes. That correlation lives entirely in the state store: point two
|
|
94
|
+
* invocations at different `stateRootDir`s and each exports a lone,
|
|
95
|
+
* explicitly classified orphan instead. A disabled exporter records nothing,
|
|
96
|
+
* since there is no span to pair.
|
|
97
|
+
*/
|
|
98
|
+
type HookRuntimeDelivery = {
|
|
99
|
+
/**
|
|
100
|
+
* Host-supplied identifier for *this* delivery of a hook callback. Stable
|
|
101
|
+
* across a redelivery of the same callback, unique between distinct
|
|
102
|
+
* callbacks. Nothing derives it: only the host knows it.
|
|
103
|
+
*/
|
|
104
|
+
readonly callbackId: string;
|
|
105
|
+
/** Namespace the id is unique within. Defaults to `"delivery"`. */
|
|
106
|
+
readonly scope?: string;
|
|
107
|
+
};
|
|
108
|
+
type HookProcessInput = HookIngestInput & {
|
|
109
|
+
readonly delivery?: HookRuntimeDelivery;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* What deduplication did with this callback.
|
|
113
|
+
*
|
|
114
|
+
* Reported rather than inferred: "exported once because it was fresh" and
|
|
115
|
+
* "exported once because nothing could tell it apart from a redelivery" produce
|
|
116
|
+
* identical telemetry, and only the first is a guarantee.
|
|
117
|
+
*/
|
|
118
|
+
type DeliveryReport = {
|
|
119
|
+
/**
|
|
120
|
+
* True when a replay-stable identity was established and a claim enforced.
|
|
121
|
+
*
|
|
122
|
+
* Not a promise of exactly-once export: see this module's note on crash
|
|
123
|
+
* semantics. It means a redelivery arriving *while local state is intact* is
|
|
124
|
+
* suppressed.
|
|
125
|
+
*/
|
|
126
|
+
readonly deduplicated: boolean;
|
|
127
|
+
/** Where the identity came from. Absent when none was established. */
|
|
128
|
+
readonly origin?: DeliveryOrigin;
|
|
129
|
+
/** How the claim resolved. Absent when no identity was established. */
|
|
130
|
+
readonly outcome?: DeliveryClaimOutcome;
|
|
131
|
+
/** Why no identity was established. Absent when one was. */
|
|
132
|
+
readonly reason?: DeliveryUnavailableReason;
|
|
133
|
+
/** The adapter's declared coverage, when a provider was selected. */
|
|
134
|
+
readonly capability?: string;
|
|
135
|
+
/**
|
|
136
|
+
* The provider's own name for the callback that could not be identified.
|
|
137
|
+
*
|
|
138
|
+
* Present only alongside `reason`. Coverage is per callback, so a host auditing
|
|
139
|
+
* its own gaps needs the callback name to act on the report at all — the
|
|
140
|
+
* provider id and the capability are the same for every one of them.
|
|
141
|
+
*/
|
|
142
|
+
readonly sourceEventName?: string;
|
|
143
|
+
/**
|
|
144
|
+
* The adapter's own account of why this callback has no identity, and what field
|
|
145
|
+
* would close the gap. Absent when the adapter documents none.
|
|
146
|
+
*/
|
|
147
|
+
readonly detail?: string;
|
|
148
|
+
/** Adapter-supplied justifications for a provider-derived identity. */
|
|
149
|
+
readonly evidence?: readonly string[];
|
|
150
|
+
/**
|
|
151
|
+
* True when a claim was released because the batch reached neither the
|
|
152
|
+
* collector nor the spool, so redelivering this callback is expected to
|
|
153
|
+
* re-export it rather than be suppressed.
|
|
154
|
+
*/
|
|
155
|
+
readonly retryable?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* Spans permanently lost on a callback that is nonetheless **committed**.
|
|
158
|
+
*
|
|
159
|
+
* Present only for a partial batch: some spans reached the collector or the
|
|
160
|
+
* spool and some did not. The callback is not retried, because retrying would
|
|
161
|
+
* re-export the accepted spans, so these observations are gone. Reported as a
|
|
162
|
+
* number rather than folded into `retryable` because the two demand opposite
|
|
163
|
+
* responses — a retryable callback needs redelivering, a partial loss needs
|
|
164
|
+
* investigating.
|
|
165
|
+
*/
|
|
166
|
+
readonly partialLoss?: number;
|
|
167
|
+
/**
|
|
168
|
+
* True when another process reclaimed this callback's claim before this one
|
|
169
|
+
* committed.
|
|
170
|
+
*
|
|
171
|
+
* Deduplication did not hold for this callback: both deliveries may have
|
|
172
|
+
* exported it. Reported rather than hidden, because the cause is a
|
|
173
|
+
* configured stale window that is too short for this installation and only an
|
|
174
|
+
* operator can fix that.
|
|
175
|
+
*/
|
|
176
|
+
readonly superseded?: boolean;
|
|
177
|
+
};
|
|
178
|
+
type HookProcessOutcome = {
|
|
179
|
+
readonly ingest: HookIngestOutcome;
|
|
180
|
+
/**
|
|
181
|
+
* True when a matching delivery id had already been handled, so this
|
|
182
|
+
* invocation's events were deliberately not exported. `ingest.emitted` is 0 in
|
|
183
|
+
* that case; the provider's hook response is still produced normally, because
|
|
184
|
+
* a redelivered callback still expects its protocol response.
|
|
185
|
+
*/
|
|
186
|
+
readonly duplicateDelivery: boolean;
|
|
187
|
+
/** Why this callback was, or was not, deduplicated. */
|
|
188
|
+
readonly delivery: DeliveryReport;
|
|
189
|
+
/** Running per-scope totals after applying this invocation's deltas. */
|
|
190
|
+
readonly usageRollups: readonly UsageRollup[];
|
|
191
|
+
/** Cleanup performed opportunistically; absent when no sweep ran. */
|
|
192
|
+
readonly cleanup?: LifecycleCleanupReport;
|
|
193
|
+
/** Diagnostics raised by this module, in addition to `ingest.diagnostics`. */
|
|
194
|
+
readonly diagnostics: readonly OtelHookErrorInfo[];
|
|
195
|
+
};
|
|
196
|
+
type UsageRollup = {
|
|
197
|
+
readonly scope: UsageObservation["scope"];
|
|
198
|
+
readonly scopeKey: string;
|
|
199
|
+
readonly snapshot: UsageAccumulatorSnapshot;
|
|
200
|
+
};
|
|
201
|
+
type HookShutdownReport = {
|
|
202
|
+
/** False when the bounded flush window elapsed before delivery finished. */
|
|
203
|
+
readonly flushCompleted: boolean;
|
|
204
|
+
readonly flushTimeoutMillis: number;
|
|
205
|
+
readonly health: OverallHealth;
|
|
206
|
+
};
|
|
207
|
+
interface HookRuntime {
|
|
208
|
+
readonly hook: OtelHook;
|
|
209
|
+
readonly config: OtelHookConfig;
|
|
210
|
+
readonly stateStore: FilesystemStateStore;
|
|
211
|
+
/** The traces signal. Named `sink` because it is the one every installation has. */
|
|
212
|
+
readonly sink: OtlpTelemetrySink;
|
|
213
|
+
/** The logs signal. A no-op sink unless `exporter.logs.enabled` is set. */
|
|
214
|
+
readonly logSink: OtlpLogTelemetrySink;
|
|
215
|
+
readonly spool?: DurableSpool;
|
|
216
|
+
/** Retry queue for the logs signal. Absent when spooling or logs are disabled. */
|
|
217
|
+
readonly logSpool?: DurableLogSpool;
|
|
218
|
+
readonly privacy: PrivacyService;
|
|
219
|
+
readonly deduplicator: CallbackDeduplicator;
|
|
220
|
+
readonly usageAccumulator: UsageAccumulator;
|
|
221
|
+
/** Backs the cross-process pairing the sink applies to every exported span. */
|
|
222
|
+
readonly spanCorrelator: SpanCorrelator;
|
|
223
|
+
readonly janitor: LifecycleJanitor;
|
|
224
|
+
/** Ingest one observation, then apply lifecycle accumulation. Never throws. */
|
|
225
|
+
process(input: HookProcessInput): Promise<HookProcessOutcome>;
|
|
226
|
+
/** Bounded flush + shutdown. Never throws; idempotent. */
|
|
227
|
+
shutdown(): Promise<HookShutdownReport>;
|
|
228
|
+
health(): OverallHealth;
|
|
229
|
+
}
|
|
230
|
+
type HookRuntimeOptions = {
|
|
231
|
+
readonly config: OtelHookConfig;
|
|
232
|
+
readonly registry: ProviderRegistry;
|
|
233
|
+
/** Root of the state and spool trees. One directory per provider/installation below it. */
|
|
234
|
+
readonly stateRootDir: string;
|
|
235
|
+
/**
|
|
236
|
+
* Namespace separating one deployment's state from another's. Not identity: it
|
|
237
|
+
* never reaches an event, and no session or workspace is derived from it.
|
|
238
|
+
*/
|
|
239
|
+
readonly installationId: string;
|
|
240
|
+
/** State/spool namespace segment; the selected provider id, or a placeholder. */
|
|
241
|
+
readonly providerNamespace: string;
|
|
242
|
+
/** Exporter header *values*, deliberately absent from the config snapshot. */
|
|
243
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
244
|
+
readonly clock?: Clock;
|
|
245
|
+
readonly ids?: IdGenerator;
|
|
246
|
+
readonly logger?: Logger;
|
|
247
|
+
readonly privacy?: PrivacyService;
|
|
248
|
+
/** Upper bound on flush + shutdown. Default 2,000ms. */
|
|
249
|
+
readonly flushTimeoutMillis?: number;
|
|
250
|
+
/** Persist batches an unreachable collector refused. Default true. */
|
|
251
|
+
readonly enableSpool?: boolean;
|
|
252
|
+
/** Bounded lock waits keep a stuck peer from stalling the host. Default 1,000ms. */
|
|
253
|
+
readonly stateLockTimeoutMillis?: number;
|
|
254
|
+
/** Sweep expired lifecycle state when a session ends. Default true. */
|
|
255
|
+
readonly sweepOnSessionEnd?: boolean;
|
|
256
|
+
readonly lifecycleMaxAgeMillis?: number;
|
|
257
|
+
/**
|
|
258
|
+
* Normalize a delivery identity from the adapter when the host supplies none.
|
|
259
|
+
* Default true. Set false to keep deduplication strictly opt-in per callback.
|
|
260
|
+
*/
|
|
261
|
+
readonly deriveDeliveryIdentity?: boolean;
|
|
262
|
+
/**
|
|
263
|
+
* Raise `delivery-identifier-unavailable` for every callback that could not be
|
|
264
|
+
* deduplicated, naming the provider and the capability that is missing. Default
|
|
265
|
+
* false: without it, an unidentifiable callback is a silent absence of a
|
|
266
|
+
* guarantee, which is exactly what a host auditing its own coverage cannot see.
|
|
267
|
+
*/
|
|
268
|
+
readonly requireCallbackId?: boolean;
|
|
269
|
+
/**
|
|
270
|
+
* How long an uncommitted claim is respected before a later delivery may
|
|
271
|
+
* assume the process that took it died. Default 60,000ms.
|
|
272
|
+
*/
|
|
273
|
+
readonly staleClaimMillis?: number;
|
|
274
|
+
/** TTL for dedup records specifically. Defaults to `lifecycleMaxAgeMillis`. */
|
|
275
|
+
readonly deliveryRetentionMillis?: number;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* Smallest stale-claim window that cannot expire while a live process is still
|
|
279
|
+
* working on the callback it claimed.
|
|
280
|
+
*
|
|
281
|
+
* A claim is taken before `ingest` and committed after accounting, so the window has
|
|
282
|
+
* to cover the whole of that: every bounded lock acquisition on the way, every
|
|
283
|
+
* export attempt the exporter policy permits, the spool write that follows a refused
|
|
284
|
+
* export, and the bounded flush. If `staleClaimMillis` is shorter, a second delivery
|
|
285
|
+
* arriving mid-flight declares the first one abandoned and *reclaims* it — and then
|
|
286
|
+
* both processes export the same callback. That is not a smaller guarantee, it is
|
|
287
|
+
* the opposite of the one being advertised, produced by a value that looks like a
|
|
288
|
+
* harmless tuning knob.
|
|
289
|
+
*
|
|
290
|
+
* Derived from the same policy the work is bounded by, rather than a constant a
|
|
291
|
+
* later timeout change could silently outgrow. It is a *floor*, not a prediction:
|
|
292
|
+
* being generous costs only recovery latency after a crash, while being short costs
|
|
293
|
+
* correctness.
|
|
294
|
+
*/
|
|
295
|
+
declare const minimumStaleClaimMillis: (input: {
|
|
296
|
+
readonly exportTimeoutMillis: number;
|
|
297
|
+
readonly maxRetryAttempts: number;
|
|
298
|
+
readonly flushTimeoutMillis: number;
|
|
299
|
+
readonly stateLockTimeoutMillis: number;
|
|
300
|
+
}) => number;
|
|
301
|
+
declare const createHookRuntime: (options: HookRuntimeOptions) => HookRuntime;
|
|
302
|
+
|
|
303
|
+
export { type DeliveryReport, type HookProcessInput, type HookProcessOutcome, type HookRuntime, type HookRuntimeDelivery, type HookRuntimeOptions, type HookShutdownReport, type UsageRollup, createHookRuntime, minimumStaleClaimMillis };
|