@osfactory/otel-hook 0.9.0 → 0.10.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.
Files changed (49) hide show
  1. package/README.md +42 -1
  2. package/dist/{adapter-BJiz6lqM.d.ts → adapter-B9RwW6TO.d.ts} +14 -2
  3. package/dist/cli.js +1272 -784
  4. package/dist/cli.js.map +1 -1
  5. package/dist/config.d.ts +5 -3
  6. package/dist/config.js +28 -5
  7. package/dist/config.js.map +1 -1
  8. package/dist/{destinations-C6Bm89W6.d.ts → destinations-DWOkVxV1.d.ts} +1 -1
  9. package/dist/{events-B3MHoJTD.d.ts → events-CSD_YiJ2.d.ts} +2 -0
  10. package/dist/{export-destinations-d8tZV0G4.d.ts → export-destinations-lORbe25J.d.ts} +5 -5
  11. package/dist/{filesystem-store-r-nNA3BA.d.ts → filesystem-store-DX3dGIgu.d.ts} +1 -1
  12. package/dist/{hook-zA1b4cWd.d.ts → hook-BAxXoKsp.d.ts} +10 -5
  13. package/dist/index.d.ts +14 -14
  14. package/dist/index.js +643 -110
  15. package/dist/index.js.map +1 -1
  16. package/dist/install.d.ts +4 -4
  17. package/dist/install.js +31 -2
  18. package/dist/install.js.map +1 -1
  19. package/dist/integration.d.ts +17 -10
  20. package/dist/integration.js +3864 -3107
  21. package/dist/integration.js.map +1 -1
  22. package/dist/lifecycle.d.ts +3 -3
  23. package/dist/lifecycle.js +8 -1
  24. package/dist/lifecycle.js.map +1 -1
  25. package/dist/{memory-Bl9ANVff.d.ts → memory-aJilVG4R.d.ts} +2 -2
  26. package/dist/model.d.ts +1 -1
  27. package/dist/model.js +7 -0
  28. package/dist/model.js.map +1 -1
  29. package/dist/{payload-Ci8_ERm6.d.ts → payload-D7gc0-WQ.d.ts} +41 -2
  30. package/dist/{ports-n7KFOpKo.d.ts → ports-plO5MKrY.d.ts} +63 -2
  31. package/dist/providers.d.ts +146 -10
  32. package/dist/providers.js +397 -13
  33. package/dist/providers.js.map +1 -1
  34. package/dist/{registry-B8ZHc2bc.d.ts → registry-C7XO4DzD.d.ts} +2 -2
  35. package/dist/runtime.d.ts +8 -8
  36. package/dist/runtime.js +1356 -1217
  37. package/dist/runtime.js.map +1 -1
  38. package/dist/{schema-BxiVsU-d.d.ts → schema-CHb3GU1r.d.ts} +30 -1
  39. package/dist/{semconv-fPRb-dpU.d.ts → semconv-Dw7gcQcg.d.ts} +1 -1
  40. package/dist/state.d.ts +4 -4
  41. package/dist/state.js +607 -39
  42. package/dist/state.js.map +1 -1
  43. package/dist/telemetry.d.ts +6 -6
  44. package/dist/telemetry.js +13 -3
  45. package/dist/telemetry.js.map +1 -1
  46. package/dist/testing.d.ts +10 -8
  47. package/dist/testing.js +209 -25
  48. package/dist/testing.js.map +1 -1
  49. package/package.json +1 -1
package/README.md CHANGED
@@ -822,6 +822,43 @@ export OTEL_RESOURCE_ATTRIBUTES="session.id=run-2041,factory.work_unit.id=wu-42,
822
822
  - Langfuse filters on span attributes, which is why `factory.*` and `vcs.*` are
823
823
  [copied onto every span](#on-records-too); `factory.local.*` is not.
824
824
 
825
+ #### Claude Code: model and token usage
826
+
827
+ Claude Code's `Stop` hook names no model and carries no tokens. Out of the box,
828
+ each generation span carries the session's model, which `SessionStart` reports
829
+ and the state store remembers (so `--state-dir`, or the default durable store,
830
+ matters).
831
+
832
+ For the exact per-turn model and the turn's summed token usage, turn on the
833
+ opt-in setting. otel-hook then reads the session transcript the payload names,
834
+ from where the previous turn ended, and sums every model request of the turn:
835
+
836
+ ```bash
837
+ otel-hook run --provider claude-code --claude-transcript-usage
838
+ # or: OTEL_HOOK_CLAUDE_TRANSCRIPT_USAGE=1
839
+ # or, in --config-file: { "providers": { "claudeCode": { "readTranscriptUsage": true } } }
840
+ ```
841
+
842
+ This is the one sanctioned exception to "no transcript reads in the hook": off by
843
+ default, done by the hook runtime and never by the adapter (ADR 0003), limited to
844
+ the one `.jsonl` file the payload names, capped at 8 MiB per turn and 500 ms, and
845
+ fail-open. When a turn cannot be summed exactly (no `SessionStart` was seen, the
846
+ state was lost, the turn wrote more than the cap), the last request is reported
847
+ instead and the span says so with `otelhook.usage.partial=true`.
848
+
849
+ A harness that wants to attach usage itself can call the library helper, which
850
+ reports the last request only (`usage_partial: true`):
851
+
852
+ ```ts
853
+ import { attachClaudeTranscriptTurn } from "@osfactory/otel-hook";
854
+
855
+ // Bounded: last 1 MiB of transcript_path, 250 ms, never throws, never
856
+ // overwrites a field the payload already has. Non-stop payloads pass through.
857
+ const enriched = await attachClaudeTranscriptTurn(hookPayload);
858
+ ```
859
+
860
+ See [finding 8](docs/claude-code-usage-contract.md#finding-8-the-model-is-on-sessionstart-and-in-the-transcript-never-on-stop).
861
+
825
862
  ### Multiple destinations
826
863
 
827
864
  `exporter.destinations` sends every batch to several OTLP endpoints at once — a
@@ -1166,7 +1203,11 @@ fixing one means updating that test rather than discovering a silent change.
1166
1203
  `usage.cache_read_input_tokens` and `usage.cache_creation_input_tokens` (whose
1167
1204
  TTL split is a breakdown, reconciled and never added). No hook callback carries
1168
1205
  a token counter at all, so `usage` is read only when a wrapping harness
1169
- attaches it. A harness that attaches an excluded counter anyway is told which
1206
+ attaches it, or the host turns on `providers.claudeCode.readTranscriptUsage`
1207
+ (`--claude-transcript-usage`), which makes the hook runtime sum each turn from
1208
+ `transcript_path`; the adapter itself never reads the transcript. Without it, generations
1209
+ still carry the session's model from `SessionStart`, remembered in the state
1210
+ store. A harness that attaches an excluded counter anyway is told which
1170
1211
  field was declined. `ADAPTER-NOTE-001`; see
1171
1212
  [docs/claude-code-usage-contract.md](docs/claude-code-usage-contract.md).
1172
1213
  6. **`contextTokensBefore` is an explicit exclusion for Claude Code.** Neither
@@ -1,11 +1,11 @@
1
1
  import { z } from 'zod';
2
2
  import { OtelHookErrorInfo } from './errors.js';
3
- import { I as UsageTemporality, b as CanonicalEvent, u as TargetHint, e as CanonicalEventType } from './events-B3MHoJTD.js';
3
+ import { I as UsageTemporality, n as ModelDescriptor, b as CanonicalEvent, u as TargetHint, e as CanonicalEventType } from './events-CSD_YiJ2.js';
4
4
  import { I as IdentityClaim, d as InvocationIdentity } from './identity-BrVBs901.js';
5
5
  import { R as ResolvedProviderId, g as ProviderId, h as SourceTransport, d as EventId } from './primitives-CJ8-rn-f.js';
6
6
  import { e as PrivacyLimits } from './policy-CimmCfRp.js';
7
7
  import { P as PrivacyService } from './service-CbI9CObT.js';
8
- import { C as Clock, I as IdGenerator, d as Logger } from './ports-n7KFOpKo.js';
8
+ import { C as Clock, I as IdGenerator, d as Logger } from './ports-plO5MKrY.js';
9
9
  import { A as AttributionOutcome, a as AttributionReason } from './taxonomy-DwhTtuWd.js';
10
10
 
11
11
  /** Raw input offered to adapters. `payload` is `unknown` on purpose. */
@@ -185,6 +185,18 @@ type ProviderParseInput = ProviderDetectionInput & {
185
185
  * consecutively from here so event ids stay stable across replays.
186
186
  */
187
187
  readonly sequenceBase: number;
188
+ /**
189
+ * The model this session last reported, as the core remembers it across
190
+ * invocations (`session.start` or a `generation.*` edge with a known model).
191
+ *
192
+ * Read from the state store by the core and handed in, because an adapter must
193
+ * not hold cross-invocation state itself (ADR 0006). Absent when no earlier
194
+ * invocation of this session named a model, or when the store is unreadable.
195
+ * An adapter whose generation callbacks carry no model id (Claude Code's
196
+ * `Stop`) uses it instead of the `unknown` sentinel; one whose callbacks carry
197
+ * their own model can ignore it.
198
+ */
199
+ readonly sessionModel?: ModelDescriptor;
188
200
  };
189
201
  type ProviderParseResult = {
190
202
  readonly status: "parsed";