index.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * SessionTelemetryBackend Service Definition for the DeepSeek Harness.
  3. *
  4. * This package owns the CAPTURE side of session-event reporting — the complete
  5. * one-record-per-event ledger mirror, what records carry, when
  6. * they are captured (adoption, the per-append firehose, lifecycle
  7. * forwarding), live versus on-demand canonical-log capture, and the HMR
  8. * cursor. Everything downstream of
  9. * {@link SessionTelemetryBackend.emit} — batching, retry, queueing, and loss policy — is the
  10. * reporting SDK's territory and is deliberately not modelled here. The
  11. * design and its trade-offs are pinned in
  12. * .agents/notes/implemented/feature/2026-07-23-session-telemetry-otel-revival.md.
  13. *
  14. * @module @deepseek-ai/dsh-session-telemetry
  15. */
  16. import { Context, Service } from '@deepseek-ai/cordis'
  17. declare module '@deepseek-ai/cordis' {
  18. interface Context {
  19. sessionTelemetry: SessionTelemetryBackend
  20. }
  21. interface Events {
  22. /**
  23. * Transform one outbound record before it reaches the backend. This
  24. * waterfall is the Service Definition's redaction extension point. It ships NO rules
  25. * of its own: the
  26. * innermost `next()` passes the record through unchanged, and with no
  27. * listener mounted records reach the backend as captured, so exported
  28. * data is exactly as clean as the rules a deployment mounts. Listeners
  29. * stack by transforming `next()`'s return value; returning without
  30. * `next()` replaces everything beneath. Dispatched synchronously on the
  31. * capture hot path inside the coordinator's containment: a throwing
  32. * listener withholds that one record (fail-closed) and never reaches the
  33. * agent loop. Live capture dispatches at append time; on-demand capture
  34. * dispatches while reading the canonical log. Redaction applies to the
  35. * exported copy only; the canonical session log is never rewritten.
  36. * @param record - the candidate record, already the coordinator's own deep
  37. * copy; listeners return a (possibly new) record and must not mutate it.
  38. * @mode waterfall
  39. */
  40. 'session-telemetry/record'(record: SessionTelemetryRecord, next: () => SessionTelemetryRecord): SessionTelemetryRecord
  41. }
  42. }
  43. /**
  44. * Severity of a telemetry record, pre-mapped at capture so a receiver can
  45. * alert with zero configuration: `error` for events whose own outcome flag
  46. * says so (the tool-result block's `isError`, `turn/end` error reasons) and for
  47. * `agent-error` operational records. Captured events otherwise default to
  48. * `info`; `warn` remains available to `session-telemetry/record` policies and
  49. * backends.
  50. */
  51. export type SessionTelemetrySeverity = 'info' | 'warn' | 'error'
  52. /**
  53. * One logical record handed to a backend — the capture contract's whole outbound
  54. * vocabulary. Ledger records mirror session-log events one-to-one;
  55. * operational records (`channel: 'ops'`) carry the two signals with no log
  56. * home (`agent-error`, `shutdown`) and deliberately omit `event.seq`-style
  57. * identity so they can never be mistaken for ledger rows.
  58. */
  59. export interface SessionTelemetryRecord {
  60. /** Ledger (session-log mirror) or ops (operational signal) channel; backends keep the two under separate instrumentation scopes. */
  61. channel: 'ledger' | 'ops'
  62. /** Unix epoch milliseconds — the source event's append time for ledger records, the emission time for ops records. */
  63. time: number
  64. /** Pre-mapped alerting severity; see {@link SessionTelemetrySeverity}. */
  65. severity: SessionTelemetrySeverity
  66. /**
  67. * Identity attributes, deliberately minimal: ledger records carry
  68. * `session.id`, `session.format_version`, `event.type`, `event.seq`, plus optional
  69. * `session.cwd` / `session.parent_id`; a seeded Session also carries
  70. * `session.seed_length` from its exact inherited event count;
  71. * ops records carry `telemetry.op`, `session.id`, and (for `agent-error`)
  72. * `agent.id`, `turn`, `step`, `error.name`. Anything recoverable from the
  73. * body is intentionally NOT duplicated here.
  74. */
  75. attributes: Record<string, string | number>
  76. /**
  77. * The complete payload: a deep copy of the session event's `data` for
  78. * ledger records (JSON-serializable by `Session.append`'s own
  79. * validation), or the op payload for ops records. Never mutated after
  80. * handoff.
  81. */
  82. body: unknown
  83. }
  84. /**
  85. * The minimum backend contract the coordinator requires. {@link SessionTelemetryBackend} is
  86. * its service-registered form; tests compose the coordinator with a bare
  87. * implementation of this interface.
  88. */
  89. export interface SessionTelemetrySink {
  90. /**
  91. * Hand one record to the backend's pipeline. MUST be a non-blocking
  92. * enqueue — the coordinator calls this synchronously from the
  93. * `session/event` hot path or an explicit canonical-log capture, so anything
  94. * slower than a queue push would tax the agent loop or feedback handling.
  95. * Errors thrown here are contained by the coordinator and logged; they
  96. * never reach the loop.
  97. * @param record - the logical record to report; owned by the backend after the call.
  98. */
  99. emit(record: SessionTelemetryRecord): void
  100. /**
  101. * Optional hint that a turn ended. A backend may forward it to its SDK's
  102. * flush so records are exported after each turn. Called
  103. * fire-and-forget; implementations must not block and must not throw
  104. * meaningfully (the coordinator contains exceptions). Most backends should
  105. * leave this unimplemented and let their SDK's own batching cadence govern
  106. * export timing: a backend that does implement it owns the interaction
  107. * between its concurrent flushes and {@link shutdown}'s drain (the OTel
  108. * backend leaves it unimplemented for exactly that hazard — see the
  109. * revival Agent Note).
  110. */
  111. flush?(): void
  112. /**
  113. * Forward the fiber's disposal to the SDK: flush whatever is queued and
  114. * reach quiescence, per the SDK's own shutdown contract. Everything
  115. * emitted before this call must still be delivered — including records
  116. * enqueued while a {@link flush} hint is in flight, so a backend whose SDK
  117. * guards against concurrent flushes orders behind the outstanding one (the
  118. * coordinator emits its dispose-time `shutdown` markers immediately before
  119. * calling this). Awaited by the coordinator's dispose; a rejection is
  120. * logged as a warning and never fails application teardown.
  121. * The coordinator captures dispose-time shutdown markers immediately before
  122. * this call for live capture; on-demand capture creates no ops records.
  123. * @returns resolves when the backend's pipeline has quiesced.
  124. */
  125. shutdown(): Promise<void>
  126. }
  127. /**
  128. * Deployment-selected session-sharing mode, not confirmation of SDK delivery.
  129. */
  130. export type SessionTelemetrySharingStatus = 'full' | 'feedback-only' | 'disabled'
  131. /**
  132. * Loadable form of the backend contract: one implementation per context —
  133. * the cordis `Service` registration under the `telemetry` key throws on a
  134. * duplicate, cordis' standard behavior. A backend composes a
  135. * {@link SessionTelemetryCoordinator} in its constructor to install the capture side.
  136. */
  137. export abstract class SessionTelemetryBackend extends Service implements SessionTelemetrySink {
  138. constructor(ctx: Context) {
  139. super(ctx, 'sessionTelemetry')
  140. }
  141. /**
  142. * Deployment-selected sharing mode, independent of SDK delivery.
  143. */
  144. abstract readonly sharing: SessionTelemetrySharingStatus
  145. /**
  146. * See {@link SessionTelemetrySink.emit} — that declaration is the contract's one home.
  147. * @param record - the logical record to report; owned by the backend after the call.
  148. */
  149. abstract emit(record: SessionTelemetryRecord): void
  150. /** See {@link SessionTelemetrySink.flush}. */
  151. flush?(): void
  152. /**
  153. * See {@link SessionTelemetrySink.shutdown}.
  154. * @returns resolves when the backend's pipeline has quiesced.
  155. */
  156. abstract shutdown(): Promise<void>
  157. }
  158. export { SessionTelemetryCoordinator, type SessionTelemetryCapture, type SessionTelemetryCaptureOptions } from './coordinator.ts'