@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,428 @@
|
|
|
1
|
+
import { d as StateStore, C as Clock } from './ports-Q8x0ODhs.js';
|
|
2
|
+
import { Attributes } from '@opentelemetry/api';
|
|
3
|
+
import { b as CanonicalEvent, f as CanonicalUsage } from './events-VxBnDFaO.js';
|
|
4
|
+
import { d as SpanFamily, g as SpanScopeRef, a as SpanCorrelation } from './semconv-Baoz-0OI.js';
|
|
5
|
+
import 'zod';
|
|
6
|
+
import './errors.js';
|
|
7
|
+
import './primitives-CJ8-rn-f.js';
|
|
8
|
+
import './taxonomy-DwhTtuWd.js';
|
|
9
|
+
import '@opentelemetry/core';
|
|
10
|
+
import '@opentelemetry/resources';
|
|
11
|
+
import '@opentelemetry/sdk-trace';
|
|
12
|
+
|
|
13
|
+
type DedupCheckResult = {
|
|
14
|
+
/** True when `callbackId` was already marked seen for this scope. */
|
|
15
|
+
readonly duplicate: boolean;
|
|
16
|
+
};
|
|
17
|
+
type DedupCleanupResult = {
|
|
18
|
+
readonly removed: number;
|
|
19
|
+
readonly scanned: number;
|
|
20
|
+
/**
|
|
21
|
+
* Aged-out records the sweep deliberately kept because they are uncommitted
|
|
22
|
+
* claims younger than the stale-claim window — a live process may still be
|
|
23
|
+
* working on them.
|
|
24
|
+
*
|
|
25
|
+
* Reported rather than merely not-removed, because the two are indistinguishable
|
|
26
|
+
* in `removed` and only one of them means "retention is configured shorter than
|
|
27
|
+
* this installation's own export budget". Absent when nothing was retained.
|
|
28
|
+
*/
|
|
29
|
+
readonly retainedInFlight?: number;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* What happened when a delivery tried to take ownership of a callback id.
|
|
33
|
+
*
|
|
34
|
+
* - `fresh`: nothing had this id. The caller owns it and must `commit`.
|
|
35
|
+
* - `duplicate`: a previous delivery ran this callback to completion.
|
|
36
|
+
* - `in-flight`: another delivery holds the id right now — a concurrent process,
|
|
37
|
+
* or one that has not committed yet. Treated as a duplicate.
|
|
38
|
+
* - `reclaimed`: a claim was abandoned long enough ago to be assumed dead, and
|
|
39
|
+
* this delivery has taken it over. The caller owns it and must `commit`.
|
|
40
|
+
*/
|
|
41
|
+
type DeliveryClaimOutcome = "fresh" | "duplicate" | "in-flight" | "reclaimed";
|
|
42
|
+
type DeliveryClaimResult = {
|
|
43
|
+
readonly outcome: DeliveryClaimOutcome;
|
|
44
|
+
/** True for `duplicate` and `in-flight`: the caller must not export or account. */
|
|
45
|
+
readonly duplicate: boolean;
|
|
46
|
+
/** True for `fresh` and `reclaimed`: the caller owns the id and should commit. */
|
|
47
|
+
readonly owned: boolean;
|
|
48
|
+
/** How many deliveries have claimed this id, including this one. */
|
|
49
|
+
readonly attempt: number;
|
|
50
|
+
/** Age of the claim that was taken over. Present only for `reclaimed`. */
|
|
51
|
+
readonly abandonedForMillis?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Opaque proof that *this* delivery owns the claim. Present when `owned`.
|
|
54
|
+
*
|
|
55
|
+
* No floor computation can be proven exact, so the stale window may still be too
|
|
56
|
+
* short for some real installation. Passing this back to {@link
|
|
57
|
+
* CallbackDeduplicator.commit} turns that from a silent double-count into a
|
|
58
|
+
* detected, reported condition: if a peer reclaimed the id in the meantime the
|
|
59
|
+
* record carries a different token, and the commit refuses rather than
|
|
60
|
+
* overwriting the peer's in-flight work.
|
|
61
|
+
*/
|
|
62
|
+
readonly owner?: string;
|
|
63
|
+
};
|
|
64
|
+
/** Whether a commit actually took effect. */
|
|
65
|
+
type DeliveryCommitResult = {
|
|
66
|
+
readonly status: "committed" | "superseded";
|
|
67
|
+
/** The attempt number recorded, for diagnostics. */
|
|
68
|
+
readonly attempt: number;
|
|
69
|
+
};
|
|
70
|
+
type DeliveryClaimOptions = {
|
|
71
|
+
/**
|
|
72
|
+
* How long an uncommitted claim is respected before it is assumed to belong to
|
|
73
|
+
* a process that died mid-flight. Default 60,000ms.
|
|
74
|
+
*/
|
|
75
|
+
readonly staleClaimMillis?: number;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Delivery guard: suppresses a redelivered callback while local state is intact.
|
|
79
|
+
*
|
|
80
|
+
* Deliberately not described as "exactly-once". This records intent and completion
|
|
81
|
+
* in a local store; the collector it protects is a separate system with no shared
|
|
82
|
+
* transaction, so a process killed between an accepted export and `commit` leaves a
|
|
83
|
+
* callback that will be exported again. What this guarantees is that a redelivery
|
|
84
|
+
* seen by a *live* store is suppressed, and that a crash is recoverable rather than
|
|
85
|
+
* either silently lost or permanently blocked.
|
|
86
|
+
*
|
|
87
|
+
* Coding-agent hosts redeliver callbacks — a retried hook invocation, a
|
|
88
|
+
* duplicated event, a replayed transcript — and a deterministic identity makes
|
|
89
|
+
* the replay recomputable, but something still has to remember which identities
|
|
90
|
+
* have already been handled. This is that memory: a bounded, TTL-swept set of
|
|
91
|
+
* seen ids per scope, independent of what the id actually names.
|
|
92
|
+
*
|
|
93
|
+
* Ownership is two-phase on purpose. `claim` records an intent before the caller
|
|
94
|
+
* exports anything and `commit` records completion afterwards, so the window
|
|
95
|
+
* where a crash could produce a double export does not exist: a second delivery
|
|
96
|
+
* arriving in that window sees `in-flight` and stands down. The cost of that
|
|
97
|
+
* choice is that a process killed between the two leaves an uncommitted claim,
|
|
98
|
+
* which is why an old one is reclaimable rather than permanent — permanent would
|
|
99
|
+
* turn every crash into silently lost telemetry.
|
|
100
|
+
*/
|
|
101
|
+
interface CallbackDeduplicator {
|
|
102
|
+
/**
|
|
103
|
+
* Single-phase check: marks the id complete immediately and reports whether it
|
|
104
|
+
* had been seen. Retained for callers that have nothing to protect between the
|
|
105
|
+
* check and the side effect; `claim`/`commit` is the stronger guarantee.
|
|
106
|
+
*/
|
|
107
|
+
checkAndMark(scope: string, callbackId: string): Promise<DedupCheckResult>;
|
|
108
|
+
/** Take ownership of a callback id before acting on it. */
|
|
109
|
+
claim(scope: string, callbackId: string, options?: DeliveryClaimOptions): Promise<DeliveryClaimResult>;
|
|
110
|
+
/**
|
|
111
|
+
* Record that a claimed callback ran to completion.
|
|
112
|
+
*
|
|
113
|
+
* `owner` is the token {@link DeliveryClaimResult.owner} handed out. Supplied and
|
|
114
|
+
* matching, the claim completes. Supplied and *not* matching, the claim has been
|
|
115
|
+
* reclaimed by a peer while this delivery was still working, and the commit is
|
|
116
|
+
* refused — reported as `"superseded"` so the caller can say so instead of
|
|
117
|
+
* silently marking a callback handled that somebody else is mid-flight on.
|
|
118
|
+
*/
|
|
119
|
+
commit(scope: string, callbackId: string, owner?: string): Promise<DeliveryCommitResult>;
|
|
120
|
+
/**
|
|
121
|
+
* Give up a claim without completing it, so the next delivery of the same
|
|
122
|
+
* callback is treated as fresh instead of waiting out the stale window.
|
|
123
|
+
*/
|
|
124
|
+
release(scope: string, callbackId: string): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Drop dedup records older than `maxAgeMillis`.
|
|
127
|
+
*
|
|
128
|
+
* An uncommitted claim is held back until it is also older than
|
|
129
|
+
* `staleClaimMillis`, whatever the retention says. Retention is an operator's
|
|
130
|
+
* answer to "how long should a handled callback stay recognizable"; the stale
|
|
131
|
+
* window is a statement about how long one process's own work can take. Deleting
|
|
132
|
+
* a claim inside that window would hand the callback to a concurrent delivery as
|
|
133
|
+
* *fresh* while the holder is still exporting it — a retention setting silently
|
|
134
|
+
* converting suppression into a double export, which is the opposite of what
|
|
135
|
+
* tightening retention looks like it does.
|
|
136
|
+
*/
|
|
137
|
+
cleanup(maxAgeMillis: number, options?: {
|
|
138
|
+
readonly maxEntries?: number;
|
|
139
|
+
readonly sessionId?: string;
|
|
140
|
+
/** Floor on how long an uncommitted claim survives. Default 60,000ms. */
|
|
141
|
+
readonly staleClaimMillis?: number;
|
|
142
|
+
}): Promise<DedupCleanupResult>;
|
|
143
|
+
}
|
|
144
|
+
type CallbackDeduplicatorDependencies = {
|
|
145
|
+
readonly stateStore: StateStore;
|
|
146
|
+
readonly clock: Clock;
|
|
147
|
+
};
|
|
148
|
+
declare const createCallbackDeduplicator: (deps: CallbackDeduplicatorDependencies) => CallbackDeduplicator;
|
|
149
|
+
|
|
150
|
+
/** Correlation scopes that pair a `*.start` event with a later `*.end` event. */
|
|
151
|
+
type LifecycleScope = SpanFamily;
|
|
152
|
+
/**
|
|
153
|
+
* Shape version of a persisted span record.
|
|
154
|
+
*
|
|
155
|
+
* A record written under a different version is discarded on read rather than
|
|
156
|
+
* interpreted: guessing at an older layout is exactly the kind of silent
|
|
157
|
+
* mis-attribution this library refuses to do. See `docs/state-retention.md`.
|
|
158
|
+
*/
|
|
159
|
+
declare const SPAN_RECORD_VERSION = 2;
|
|
160
|
+
/** Facts about a span recovered from, or written to, persisted state. */
|
|
161
|
+
type SpanCorrelationFacts = {
|
|
162
|
+
readonly startedAt?: number;
|
|
163
|
+
readonly endedAt?: number;
|
|
164
|
+
readonly parent?: SpanScopeRef;
|
|
165
|
+
readonly attributes?: Attributes;
|
|
166
|
+
};
|
|
167
|
+
type SpanStartInput = {
|
|
168
|
+
readonly sessionId: string;
|
|
169
|
+
/** Attribution boundary: a scope recorded by one provider never pairs with another's. */
|
|
170
|
+
readonly providerId: string;
|
|
171
|
+
readonly scope: LifecycleScope;
|
|
172
|
+
readonly scopeKey: string;
|
|
173
|
+
/** The canonical event id of the start event; used to detect redelivery. */
|
|
174
|
+
readonly eventId: string;
|
|
175
|
+
readonly occurredAt: number;
|
|
176
|
+
/** Where this span hangs, so an end-only process reparents it identically. */
|
|
177
|
+
readonly parent?: SpanScopeRef;
|
|
178
|
+
/** Attributes only the start edge knows; carried forward to the end-side span. */
|
|
179
|
+
readonly attributes?: Attributes;
|
|
180
|
+
};
|
|
181
|
+
type SpanEndInput = {
|
|
182
|
+
readonly sessionId: string;
|
|
183
|
+
readonly providerId: string;
|
|
184
|
+
readonly scope: LifecycleScope;
|
|
185
|
+
readonly scopeKey: string;
|
|
186
|
+
readonly eventId: string;
|
|
187
|
+
readonly occurredAt: number;
|
|
188
|
+
};
|
|
189
|
+
type SpanStartResult = {
|
|
190
|
+
readonly status: "recorded";
|
|
191
|
+
readonly facts: SpanCorrelationFacts;
|
|
192
|
+
}
|
|
193
|
+
/** The same start (by event id) was already recorded; state is unchanged. */
|
|
194
|
+
| {
|
|
195
|
+
readonly status: "duplicate";
|
|
196
|
+
readonly facts: SpanCorrelationFacts;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* The record was completed, but an OTLP record for this scope has already been
|
|
200
|
+
* published under its span id — so this edge must not export one.
|
|
201
|
+
*/
|
|
202
|
+
| {
|
|
203
|
+
readonly status: "published";
|
|
204
|
+
readonly facts: SpanCorrelationFacts;
|
|
205
|
+
}
|
|
206
|
+
/** A different provider owns this scope key in this session; nothing was written. */
|
|
207
|
+
| {
|
|
208
|
+
readonly status: "conflict";
|
|
209
|
+
readonly reason: "provider-mismatch";
|
|
210
|
+
};
|
|
211
|
+
/** Why an end could not be paired with a recorded start. */
|
|
212
|
+
type SpanEndOrphanReason = "no-start-recorded" | "start-expired" | "already-closed" | "provider-mismatch" | "state-incompatible" | "state-corrupt";
|
|
213
|
+
type SpanEndResult = {
|
|
214
|
+
readonly status: "matched";
|
|
215
|
+
readonly startedAt: number;
|
|
216
|
+
readonly durationMillis: number;
|
|
217
|
+
readonly facts: SpanCorrelationFacts;
|
|
218
|
+
}
|
|
219
|
+
/** This exact end (by event id) was already applied; the same facts are replayed. */
|
|
220
|
+
| {
|
|
221
|
+
readonly status: "duplicate";
|
|
222
|
+
readonly facts: SpanCorrelationFacts;
|
|
223
|
+
} | {
|
|
224
|
+
readonly status: "orphaned";
|
|
225
|
+
readonly reason: SpanEndOrphanReason;
|
|
226
|
+
readonly facts: SpanCorrelationFacts;
|
|
227
|
+
};
|
|
228
|
+
type SpanCleanupResult = {
|
|
229
|
+
readonly removed: number;
|
|
230
|
+
readonly scanned: number;
|
|
231
|
+
/**
|
|
232
|
+
* Records dropped that held a start and never received an end.
|
|
233
|
+
*
|
|
234
|
+
* These are the spans that will never be exported at all: the end edge is what
|
|
235
|
+
* emits a lifecycle span, so a scope whose end never arrives — a session the
|
|
236
|
+
* host killed, a tool call that outlived the retention window — produces no
|
|
237
|
+
* OTLP record. That is the standard OpenTelemetry outcome for a span that never
|
|
238
|
+
* ends, and it is deliberately not papered over with a synthetic end time,
|
|
239
|
+
* because a fabricated duration is a wrong measurement rather than a missing
|
|
240
|
+
* one. Counting them keeps the cost visible: a non-zero number here means real
|
|
241
|
+
* activity went unreported, which is a signal about the host or the retention
|
|
242
|
+
* window rather than noise. See `docs/state-retention.md`.
|
|
243
|
+
*/
|
|
244
|
+
readonly expiredOpen: number;
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Cross-invocation start/end correlation for session, generation, tool, and
|
|
248
|
+
* subagent scopes.
|
|
249
|
+
*
|
|
250
|
+
* A coding-agent hook fires once per lifecycle edge, usually as a separate
|
|
251
|
+
* short-lived process for the start and for the end. This records an open span
|
|
252
|
+
* in the shared state store so the end side can report a real start time,
|
|
253
|
+
* duration, parent, and start-only attributes even though its process has no
|
|
254
|
+
* memory of the start — and so a redelivered start or end (an at-least-once
|
|
255
|
+
* host retry) re-exports an identical span instead of being double-counted.
|
|
256
|
+
*
|
|
257
|
+
* ## What it refuses to do
|
|
258
|
+
*
|
|
259
|
+
* - **Pair across sessions.** The state key is session-scoped, so two sessions
|
|
260
|
+
* cannot see each other's open spans.
|
|
261
|
+
* - **Pair across providers.** The key is provider-scoped *and* the record
|
|
262
|
+
* carries the provider that wrote it, so a mismatch is reported rather than
|
|
263
|
+
* guessed at — the same defense in depth the durable spool uses.
|
|
264
|
+
* - **Trust a stale start.** A start older than `maxStartAgeMillis` when its end
|
|
265
|
+
* arrives is dropped and classified `start-expired`, rather than reporting a
|
|
266
|
+
* duration that really measures a machine suspend or a reused id.
|
|
267
|
+
* - **Interpret a record it does not understand.** A corrupt or wrong-version
|
|
268
|
+
* record is deleted and classified, never partially read.
|
|
269
|
+
*/
|
|
270
|
+
interface SpanCorrelator {
|
|
271
|
+
recordStart(input: SpanStartInput): Promise<SpanStartResult>;
|
|
272
|
+
recordEnd(input: SpanEndInput): Promise<SpanEndResult>;
|
|
273
|
+
/**
|
|
274
|
+
* Records every lifecycle edge in a batch and reports what the exported spans
|
|
275
|
+
* should say. Never rejects: a state failure degrades one scope to
|
|
276
|
+
* `state-unavailable` rather than losing the batch (fail-open telemetry).
|
|
277
|
+
*/
|
|
278
|
+
correlateBatch(events: readonly CanonicalEvent[]): Promise<readonly SpanCorrelation[]>;
|
|
279
|
+
/** Bounded sweep: drops spans untouched for longer than `maxAgeMillis`. */
|
|
280
|
+
cleanup(maxAgeMillis: number, options?: {
|
|
281
|
+
readonly maxEntries?: number;
|
|
282
|
+
readonly sessionId?: string;
|
|
283
|
+
}): Promise<SpanCleanupResult>;
|
|
284
|
+
}
|
|
285
|
+
type SpanCorrelatorDependencies = {
|
|
286
|
+
readonly stateStore: StateStore;
|
|
287
|
+
readonly clock: Clock;
|
|
288
|
+
/**
|
|
289
|
+
* A recorded start older than this when its end arrives is not trusted.
|
|
290
|
+
* Default 24 hours, matching the janitor's retention window.
|
|
291
|
+
*/
|
|
292
|
+
readonly maxStartAgeMillis?: number;
|
|
293
|
+
};
|
|
294
|
+
declare const createSpanCorrelator: (deps: SpanCorrelatorDependencies) => SpanCorrelator;
|
|
295
|
+
|
|
296
|
+
type UsageAccumulatorKey = {
|
|
297
|
+
readonly sessionId: string;
|
|
298
|
+
readonly scope: string;
|
|
299
|
+
readonly scopeKey: string;
|
|
300
|
+
};
|
|
301
|
+
type UsageAccumulatorSnapshot = {
|
|
302
|
+
readonly total: CanonicalUsage;
|
|
303
|
+
/** Increments every time {@link UsageAccumulator.recordReset} is called for this key. */
|
|
304
|
+
readonly epoch: number;
|
|
305
|
+
};
|
|
306
|
+
type RollupCleanupResult = {
|
|
307
|
+
readonly removed: number;
|
|
308
|
+
readonly scanned: number;
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* Which delivery an accumulation belongs to, and which of its observations this is.
|
|
312
|
+
*
|
|
313
|
+
* Supplying it makes the accumulation idempotent for *that delivery*: a second
|
|
314
|
+
* application of the same `(callbackId, ordinal)` is a no-op that returns the
|
|
315
|
+
* current total. Omitting it keeps the old unconditional behaviour, which is right
|
|
316
|
+
* for a caller that has no delivery identity to key on — there is nothing to
|
|
317
|
+
* recognize a repeat by, and refusing to accumulate would lose a real observation.
|
|
318
|
+
*
|
|
319
|
+
* `callbackId` must be an opaque digest, not a raw host or provider id: it is
|
|
320
|
+
* written into a state record.
|
|
321
|
+
*/
|
|
322
|
+
type DeliveryStamp = {
|
|
323
|
+
readonly callbackId: string;
|
|
324
|
+
/** Zero-based index of this observation within the delivery, per rollup key. */
|
|
325
|
+
readonly ordinal: number;
|
|
326
|
+
};
|
|
327
|
+
type AccumulateOptions = {
|
|
328
|
+
readonly delivery?: DeliveryStamp;
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Rolls a stream of `delta` usage observations up into a running `cumulative`
|
|
332
|
+
* total per scope, independent of the orchestrator's own per-scope
|
|
333
|
+
* cumulative-to-delta baseline (`docs/usage-semantics.md`) — that baseline
|
|
334
|
+
* answers "what changed since last time"; this answers "what is the running
|
|
335
|
+
* total", which a diagnostics view or a parent scope (tool -> generation ->
|
|
336
|
+
* session) needs and the core does not compute.
|
|
337
|
+
*
|
|
338
|
+
* A provider counter can restart mid-session (a process restart, a replayed
|
|
339
|
+
* transcript). Rather than let a smaller cumulative total silently deflate the
|
|
340
|
+
* running rollup, {@link UsageAccumulator.recordReset} starts a new epoch: the
|
|
341
|
+
* total resets to zero and the epoch number increments, so a downstream
|
|
342
|
+
* consumer can tell "the counter restarted" from "usage actually decreased"
|
|
343
|
+
* and never sums across the boundary as if it were one continuous series.
|
|
344
|
+
*/
|
|
345
|
+
interface UsageAccumulator {
|
|
346
|
+
/**
|
|
347
|
+
* Fold `delta` into the running total for `key`.
|
|
348
|
+
*
|
|
349
|
+
* With `options.delivery`, folding is idempotent for that delivery: a redelivered
|
|
350
|
+
* callback whose claim was reclaimed or superseded re-derives the same delta and
|
|
351
|
+
* would otherwise count it twice. The marker lives in the same record as the total
|
|
352
|
+
* it describes, so the check and the write are one atomic operation — which is the
|
|
353
|
+
* only reason this is expressible without a multi-key transaction. What it does
|
|
354
|
+
* *not* cover is a delivery whose marker a later, different delivery has since
|
|
355
|
+
* overwritten: only the most recent delivery per rollup key is recognizable, which
|
|
356
|
+
* is exactly the retry window and nothing beyond it.
|
|
357
|
+
*/
|
|
358
|
+
accumulateDelta(key: UsageAccumulatorKey, delta: CanonicalUsage, options?: AccumulateOptions): Promise<UsageAccumulatorSnapshot>;
|
|
359
|
+
/** Start a new epoch. Idempotent per delivery when `options.delivery` is given. */
|
|
360
|
+
recordReset(key: UsageAccumulatorKey, options?: AccumulateOptions): Promise<{
|
|
361
|
+
readonly epoch: number;
|
|
362
|
+
}>;
|
|
363
|
+
read(key: UsageAccumulatorKey): Promise<UsageAccumulatorSnapshot | undefined>;
|
|
364
|
+
cleanup(maxAgeMillis: number, options?: {
|
|
365
|
+
readonly maxEntries?: number;
|
|
366
|
+
readonly sessionId?: string;
|
|
367
|
+
}): Promise<RollupCleanupResult>;
|
|
368
|
+
}
|
|
369
|
+
type UsageAccumulatorDependencies = {
|
|
370
|
+
readonly stateStore: StateStore;
|
|
371
|
+
readonly clock: Clock;
|
|
372
|
+
};
|
|
373
|
+
declare const createUsageAccumulator: (deps: UsageAccumulatorDependencies) => UsageAccumulator;
|
|
374
|
+
|
|
375
|
+
type LifecycleJanitorOptions = {
|
|
376
|
+
readonly spanCorrelator?: SpanCorrelator;
|
|
377
|
+
readonly deduplicator?: CallbackDeduplicator;
|
|
378
|
+
readonly usageAccumulator?: UsageAccumulator;
|
|
379
|
+
readonly spanMaxAgeMillis?: number;
|
|
380
|
+
readonly dedupMaxAgeMillis?: number;
|
|
381
|
+
/**
|
|
382
|
+
* Floor on how long an uncommitted delivery claim survives the sweep, whatever
|
|
383
|
+
* `dedupMaxAgeMillis` says.
|
|
384
|
+
*
|
|
385
|
+
* Passed through rather than left to the deduplicator's default so it can be the
|
|
386
|
+
* *effective* window the runtime computed, which is raised from the requested one
|
|
387
|
+
* to cover a process's whole export budget. A sweep using a smaller number than
|
|
388
|
+
* the claim path uses would delete claims the claim path still considers live.
|
|
389
|
+
*/
|
|
390
|
+
readonly dedupStaleClaimMillis?: number;
|
|
391
|
+
readonly usageMaxAgeMillis?: number;
|
|
392
|
+
/** Caps how many keys any single component scans per sweep. */
|
|
393
|
+
readonly maxEntriesPerComponent?: number;
|
|
394
|
+
};
|
|
395
|
+
type LifecycleSweepStats = {
|
|
396
|
+
readonly removed: number;
|
|
397
|
+
readonly scanned: number;
|
|
398
|
+
/**
|
|
399
|
+
* Span records dropped with a start and no end — spans that were never
|
|
400
|
+
* exported. Reported only by the span sweep; see `SpanCleanupResult`.
|
|
401
|
+
*/
|
|
402
|
+
readonly expiredOpen?: number;
|
|
403
|
+
/**
|
|
404
|
+
* Aged-out records kept back because they are delivery claims a live process may
|
|
405
|
+
* still hold. Reported only by the dedup sweep; see `DedupCleanupResult`.
|
|
406
|
+
*/
|
|
407
|
+
readonly retainedInFlight?: number;
|
|
408
|
+
};
|
|
409
|
+
type LifecycleCleanupReport = {
|
|
410
|
+
readonly span?: LifecycleSweepStats;
|
|
411
|
+
readonly dedup?: LifecycleSweepStats;
|
|
412
|
+
readonly usage?: LifecycleSweepStats;
|
|
413
|
+
};
|
|
414
|
+
/**
|
|
415
|
+
* Fans a single bounded cleanup pass out across whichever lifecycle
|
|
416
|
+
* components are configured.
|
|
417
|
+
*
|
|
418
|
+
* Each component already bounds its own sweep (`maxEntries`); this exists so
|
|
419
|
+
* a caller — a CLI subcommand, a periodic timer, an opportunistic call at the
|
|
420
|
+
* end of `flush()` — can run "the" cleanup without wiring each component by
|
|
421
|
+
* hand, and so the bound is enforced uniformly rather than per call site.
|
|
422
|
+
*/
|
|
423
|
+
interface LifecycleJanitor {
|
|
424
|
+
runOnce(sessionId?: string): Promise<LifecycleCleanupReport>;
|
|
425
|
+
}
|
|
426
|
+
declare const createLifecycleJanitor: (options: LifecycleJanitorOptions) => LifecycleJanitor;
|
|
427
|
+
|
|
428
|
+
export { type CallbackDeduplicator, type CallbackDeduplicatorDependencies, type DedupCheckResult, type DedupCleanupResult, type DeliveryClaimOptions, type DeliveryClaimOutcome, type DeliveryClaimResult, type LifecycleCleanupReport, type LifecycleJanitor, type LifecycleJanitorOptions, type LifecycleScope, type LifecycleSweepStats, type RollupCleanupResult, SPAN_RECORD_VERSION, type SpanCleanupResult, type SpanCorrelationFacts, type SpanCorrelator, type SpanCorrelatorDependencies, type SpanEndInput, type SpanEndOrphanReason, type SpanEndResult, type SpanStartInput, type SpanStartResult, type UsageAccumulator, type UsageAccumulatorDependencies, type UsageAccumulatorKey, type UsageAccumulatorSnapshot, createCallbackDeduplicator, createLifecycleJanitor, createSpanCorrelator, createUsageAccumulator };
|