All observability/telemetry code must follow a consistent “semantics contract”: events must be emitted exactly once, with correct timing/attribution, and with strict schemas that respect opt-out and sanitization.
Apply these rules when adding or changing telemetry:
1) Preserve lifecycle ordering
2) Keep attribution correct (no stale causes)
3) Enforce telemetry policy via helpers + strict payloads
4) Avoid double counting (single source of truth)
5) Use granular, condition-accurate reasons
6) Sanitize and align temporal context
Example pattern (typed helper + schema + opt-out safe emit):
// core-events.ts
export function capturePendingPromptsDiscarded(
telemetry: ITelemetryService | undefined,
details: { sessionId: string; count: number; reason: "queue_abort" | "session_teardown" },
): void {
emit(telemetry, CORE_TELEMETRY_EVENTS.SESSION.PENDING_PROMPTS_DISCARDED, details);
}
// host/runtime usage
session.config.telemetry?.capture({
event: CORE_TELEMETRY_EVENTS.SESSION.PENDING_PROMPTS_DISCARDED,
...details,
});
// tests should assert:
// - capture called once with exact catalog event name
// - payload contains only {sessionId,count,reason}
// - captureRequired is never called when telemetry is disabled
This contract prevents the most expensive observability failures: misleading dashboards, broken alerting due to misclassification, incorrect trace/span status due to ordering, and silent telemetry regressions from “helpful” guards or duplicated emission.