@vellumai/assistant 0.10.8-dev.202607141535.d82d641 → 0.10.8-dev.202607141817.ebca77a
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/AGENTS.md +8 -0
- package/knip.json +1 -4
- package/package.json +1 -1
- package/src/__tests__/oauth-provider-profiles.test.ts +3 -2
- package/src/cli/lib/__tests__/plugin-catalog-local.test.ts +1 -0
- package/src/cli/lib/bundled-marketplace.json +6 -4
- package/src/config/feature-flag-registry.json +1 -1
- package/src/oauth/seed-providers.ts +6 -0
- package/src/plugins/defaults/memory/graph-topology/__tests__/build-memory-graph.test.ts +81 -2
- package/src/plugins/defaults/memory/graph-topology/build-memory-graph.ts +80 -33
- package/src/providers/speech-to-text/__tests__/vellum-managed-realtime.test.ts +1 -1
- package/src/providers/speech-to-text/__tests__/vellum-speech-relay-connection.test.ts +4 -1
- package/src/providers/speech-to-text/vellum-speech-relay-connection.ts +5 -0
- package/src/telemetry/__tests__/telemetry-event-fixtures.ts +260 -0
- package/src/telemetry/telemetry-wire-validation.test.ts +149 -0
- package/src/telemetry/telemetry-wire-validation.ts +125 -0
- package/src/telemetry/types.test.ts +50 -0
- package/src/telemetry/types.ts +171 -91
- package/src/telemetry/usage-telemetry-reporter.ts +5 -0
- package/src/telemetry/watchdog-direct-emit.ts +6 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared sample telemetry events for the wire-contract test suites
|
|
3
|
+
* (`types.test.ts` and `telemetry-wire-validation.test.ts`).
|
|
4
|
+
*
|
|
5
|
+
* `wireEventSamples` holds exactly one daemon-typed, wire-valid sample per
|
|
6
|
+
* generated wire event type — when the wire contract gains an event type,
|
|
7
|
+
* add its sample here and both suites pick it up. The daemon-type
|
|
8
|
+
* annotations are themselves part of the contract test: they compile-check
|
|
9
|
+
* that realistic daemon values inhabit the hand-written override types.
|
|
10
|
+
*
|
|
11
|
+
* Imported by `*.test.ts` files only — never by the test preload — so
|
|
12
|
+
* importing daemon types from `src/` is safe (AGENTS.md "Test machinery
|
|
13
|
+
* isolation" scopes its no-`src/` rule to preload-time machinery). The
|
|
14
|
+
* imports are type-only; this module has no runtime side effects.
|
|
15
|
+
*/
|
|
16
|
+
import type {
|
|
17
|
+
AuthFallbackTelemetryEvent,
|
|
18
|
+
ConfigSettingTelemetryEvent,
|
|
19
|
+
LifecycleTelemetryEvent,
|
|
20
|
+
LlmUsageTelemetryEvent,
|
|
21
|
+
OnboardingResearchTelemetryEvent,
|
|
22
|
+
OnboardingTelemetryEvent,
|
|
23
|
+
SkillLoadedTelemetryEvent,
|
|
24
|
+
ToolExecutedTelemetryEvent,
|
|
25
|
+
TurnTelemetryEvent,
|
|
26
|
+
TurnTrace,
|
|
27
|
+
WatchdogTelemetryEvent,
|
|
28
|
+
} from "../types.js";
|
|
29
|
+
|
|
30
|
+
const RECORDED_AT = 1_750_000_000_000;
|
|
31
|
+
|
|
32
|
+
const llmUsage: LlmUsageTelemetryEvent = {
|
|
33
|
+
type: "llm_usage",
|
|
34
|
+
daemon_event_id: "evt-llm-usage-001",
|
|
35
|
+
recorded_at: RECORDED_AT,
|
|
36
|
+
assistant_version: "1.2.3",
|
|
37
|
+
conversation_id: "conv-xyz",
|
|
38
|
+
conversation_type: "standard",
|
|
39
|
+
// Zero is legitimate here (wire bound is min(0)) — the wire schema must
|
|
40
|
+
// not reject it.
|
|
41
|
+
turn_index: 0,
|
|
42
|
+
provider: "anthropic",
|
|
43
|
+
model: "claude-fable-5",
|
|
44
|
+
input_tokens: 1200,
|
|
45
|
+
output_tokens: 340,
|
|
46
|
+
cache_creation_input_tokens: 256,
|
|
47
|
+
cache_read_input_tokens: 1024,
|
|
48
|
+
llm_call_count: 3,
|
|
49
|
+
raw_usage: {
|
|
50
|
+
input_tokens: 1200,
|
|
51
|
+
cache_creation: { ephemeral_5m_input_tokens: 256 },
|
|
52
|
+
},
|
|
53
|
+
actor: "user",
|
|
54
|
+
llm_call_site: "mainAgent",
|
|
55
|
+
inference_profile: "balanced",
|
|
56
|
+
inference_profile_source: "active",
|
|
57
|
+
cost: 0.0123,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const trace: TurnTrace = {
|
|
61
|
+
schema_version: 3,
|
|
62
|
+
messages: [
|
|
63
|
+
{
|
|
64
|
+
id: "msg-1",
|
|
65
|
+
role: "user",
|
|
66
|
+
created_at: RECORDED_AT - 5_000,
|
|
67
|
+
content: [{ type: "text", text: "What's on my calendar today?" }],
|
|
68
|
+
model: null,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: "msg-2",
|
|
72
|
+
role: "assistant",
|
|
73
|
+
created_at: RECORDED_AT - 1_000,
|
|
74
|
+
content: [{ type: "text", text: "You have two meetings." }],
|
|
75
|
+
model: "claude-fable-5",
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
tool_calls: [
|
|
79
|
+
{
|
|
80
|
+
id: "inv-1",
|
|
81
|
+
tool_name: "bash",
|
|
82
|
+
input: { command: "ls" },
|
|
83
|
+
result: "ok",
|
|
84
|
+
decision: "allow",
|
|
85
|
+
duration_ms: 42,
|
|
86
|
+
created_at: RECORDED_AT - 3_000,
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
system_prompt: "You are a helpful assistant.",
|
|
90
|
+
tool_definitions: [
|
|
91
|
+
{
|
|
92
|
+
name: "bash",
|
|
93
|
+
description: "Run a shell command",
|
|
94
|
+
input_schema: { type: "object", properties: {} },
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** Exported separately: both suites build negative `client`-bag cases on it. */
|
|
100
|
+
export const turnEventSample: TurnTelemetryEvent = {
|
|
101
|
+
type: "turn",
|
|
102
|
+
daemon_event_id: "evt-turn-001",
|
|
103
|
+
recorded_at: RECORDED_AT,
|
|
104
|
+
assistant_version: "1.2.3",
|
|
105
|
+
conversation_id: "conv-xyz",
|
|
106
|
+
conversation_type: "standard",
|
|
107
|
+
turn_index: 1,
|
|
108
|
+
interface_id: "web",
|
|
109
|
+
channel_id: "vellum",
|
|
110
|
+
client: {
|
|
111
|
+
browser_family: "chrome",
|
|
112
|
+
browser_version: "124",
|
|
113
|
+
os: "macos",
|
|
114
|
+
interface_version: "0.8.2",
|
|
115
|
+
},
|
|
116
|
+
outcome: "failed",
|
|
117
|
+
failure_code: "PROVIDER_RATE_LIMIT",
|
|
118
|
+
trace,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const lifecycle: LifecycleTelemetryEvent = {
|
|
122
|
+
type: "lifecycle",
|
|
123
|
+
daemon_event_id: "evt-lifecycle-001",
|
|
124
|
+
recorded_at: RECORDED_AT,
|
|
125
|
+
assistant_version: "1.2.3",
|
|
126
|
+
event_name: "app_open",
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const onboarding: OnboardingTelemetryEvent = {
|
|
130
|
+
type: "onboarding",
|
|
131
|
+
daemon_event_id: "evt-onboarding-001",
|
|
132
|
+
recorded_at: RECORDED_AT,
|
|
133
|
+
assistant_version: "1.2.3",
|
|
134
|
+
screen: "tools",
|
|
135
|
+
tools: ["gmail", "calendar"],
|
|
136
|
+
tasks: ["email-triage"],
|
|
137
|
+
tone: "friendly",
|
|
138
|
+
google_connected: true,
|
|
139
|
+
google_scopes: ["https://www.googleapis.com/auth/gmail.readonly"],
|
|
140
|
+
ab_variant: "control",
|
|
141
|
+
session_id: "sess-123",
|
|
142
|
+
step_name: "select-tools",
|
|
143
|
+
step_index: 2,
|
|
144
|
+
completed_at: "2026-07-13T00:00:00Z",
|
|
145
|
+
funnel_version: "v2",
|
|
146
|
+
user_id: "user-123",
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const authFallback: AuthFallbackTelemetryEvent = {
|
|
150
|
+
type: "auth_fallback",
|
|
151
|
+
daemon_event_id: "evt-auth-fallback-001",
|
|
152
|
+
recorded_at: RECORDED_AT,
|
|
153
|
+
assistant_version: "1.2.3",
|
|
154
|
+
guard: "edge",
|
|
155
|
+
path: "/v1/conversations",
|
|
156
|
+
failure_kind: "missing_authorization",
|
|
157
|
+
count: 7,
|
|
158
|
+
window_start: RECORDED_AT - 300_000,
|
|
159
|
+
window_end: RECORDED_AT,
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const toolExecuted: ToolExecutedTelemetryEvent = {
|
|
163
|
+
type: "tool_executed",
|
|
164
|
+
daemon_event_id: "evt-tool-executed-001",
|
|
165
|
+
recorded_at: RECORDED_AT,
|
|
166
|
+
assistant_version: "1.2.3",
|
|
167
|
+
provider: "anthropic",
|
|
168
|
+
model: "claude-fable-5",
|
|
169
|
+
inference_profile: "balanced",
|
|
170
|
+
inference_profile_source: "call_site",
|
|
171
|
+
tool_name: "bash",
|
|
172
|
+
status: "fulfilled",
|
|
173
|
+
duration_ms: 42,
|
|
174
|
+
arg_bytes: 128,
|
|
175
|
+
result_bytes: 2048,
|
|
176
|
+
conversation_id: "conv-xyz",
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const skillLoaded: SkillLoadedTelemetryEvent = {
|
|
180
|
+
type: "skill_loaded",
|
|
181
|
+
daemon_event_id: "evt-skill-loaded-001",
|
|
182
|
+
recorded_at: RECORDED_AT,
|
|
183
|
+
assistant_version: "1.2.3",
|
|
184
|
+
provider: "anthropic",
|
|
185
|
+
model: "claude-fable-5",
|
|
186
|
+
inference_profile: "balanced",
|
|
187
|
+
inference_profile_source: "conversation",
|
|
188
|
+
skill_name: "plugin-builder",
|
|
189
|
+
skill_updated_at: "2026-06-01T00:00:00Z",
|
|
190
|
+
conversation_id: "conv-xyz",
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const watchdog: WatchdogTelemetryEvent = {
|
|
194
|
+
type: "watchdog",
|
|
195
|
+
daemon_event_id: "evt-watchdog-001",
|
|
196
|
+
recorded_at: RECORDED_AT,
|
|
197
|
+
assistant_version: "1.2.3",
|
|
198
|
+
check_name: "event_loop_blocked",
|
|
199
|
+
value: 1200,
|
|
200
|
+
detail: { reason: "gc", blocked_ms: 1200 },
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const configSetting: ConfigSettingTelemetryEvent = {
|
|
204
|
+
type: "config_setting",
|
|
205
|
+
daemon_event_id: "evt-config-setting-001",
|
|
206
|
+
recorded_at: RECORDED_AT,
|
|
207
|
+
assistant_version: "1.2.3",
|
|
208
|
+
config_key: "memory.enabled",
|
|
209
|
+
config_value: "true",
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/** One daemon-typed, wire-valid sample per generated wire event type. */
|
|
213
|
+
export const wireEventSamples = [
|
|
214
|
+
llmUsage,
|
|
215
|
+
turnEventSample,
|
|
216
|
+
lifecycle,
|
|
217
|
+
onboarding,
|
|
218
|
+
authFallback,
|
|
219
|
+
toolExecuted,
|
|
220
|
+
skillLoaded,
|
|
221
|
+
watchdog,
|
|
222
|
+
configSetting,
|
|
223
|
+
] as const;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Daemon-only extension event — NOT in the wire contract: the platform has
|
|
227
|
+
* no `onboarding_research` ingest serializer, so the server silently drops
|
|
228
|
+
* it. Both suites use this builder to pin that gap (schema rejection /
|
|
229
|
+
* unknown-type reporting).
|
|
230
|
+
*/
|
|
231
|
+
export function makeOnboardingResearchEvent(): OnboardingResearchTelemetryEvent {
|
|
232
|
+
return {
|
|
233
|
+
type: "onboarding_research",
|
|
234
|
+
daemon_event_id: "evt-onboarding-research-1",
|
|
235
|
+
recorded_at: RECORDED_AT,
|
|
236
|
+
assistant_version: "1.2.3",
|
|
237
|
+
conversation_id: "conv-xyz",
|
|
238
|
+
status: "done",
|
|
239
|
+
claims: [
|
|
240
|
+
{
|
|
241
|
+
claim: "Works on developer tooling",
|
|
242
|
+
confidence: "confident",
|
|
243
|
+
sources: ["https://example.com/profile"],
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
claim_count: 1,
|
|
247
|
+
claims_confident: 1,
|
|
248
|
+
claims_maybe: 0,
|
|
249
|
+
claims_guessing: 0,
|
|
250
|
+
suggestions: [
|
|
251
|
+
{
|
|
252
|
+
suggestion: "Set up email triage",
|
|
253
|
+
prompt: "Help me triage my inbox",
|
|
254
|
+
},
|
|
255
|
+
],
|
|
256
|
+
suggestion_count: 1,
|
|
257
|
+
plugins: ["gmail"],
|
|
258
|
+
installed_plugins: ["gmail", "calendar"],
|
|
259
|
+
};
|
|
260
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for pre-flush telemetry wire validation.
|
|
3
|
+
*
|
|
4
|
+
* Validation is observability only — it must count and warn, never mutate,
|
|
5
|
+
* filter, or block — and its warn payloads must carry the event type and
|
|
6
|
+
* issue `{ path, code }` shapes only, never field values. `daemon_event_id`
|
|
7
|
+
* is a field value too: activation-funnel ids embed the onboarding session
|
|
8
|
+
* id (traces/claims can hold PII). Issue paths are sanitized as well: dynamic
|
|
9
|
+
* record keys (e.g. `client` bag keys) are redacted to `*` before logging.
|
|
10
|
+
*/
|
|
11
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
12
|
+
|
|
13
|
+
import type { Logger } from "pino";
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
makeOnboardingResearchEvent,
|
|
17
|
+
turnEventSample,
|
|
18
|
+
wireEventSamples,
|
|
19
|
+
} from "./__tests__/telemetry-event-fixtures.js";
|
|
20
|
+
import { telemetryEventSchema } from "./telemetry-wire.generated.js";
|
|
21
|
+
import {
|
|
22
|
+
resetUnknownTypeWarningsForTests,
|
|
23
|
+
validateWireEvents,
|
|
24
|
+
} from "./telemetry-wire-validation.js";
|
|
25
|
+
|
|
26
|
+
let warnCalls: unknown[][] = [];
|
|
27
|
+
const stubLog = {
|
|
28
|
+
warn: (...args: unknown[]) => {
|
|
29
|
+
warnCalls.push(args);
|
|
30
|
+
},
|
|
31
|
+
} as unknown as Logger;
|
|
32
|
+
|
|
33
|
+
describe("validateWireEvents", () => {
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
warnCalls = [];
|
|
36
|
+
resetUnknownTypeWarningsForTests();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("a valid event of each wire type passes with no warnings", () => {
|
|
40
|
+
// Fixture coverage: one sample per generated union discriminator (with a
|
|
41
|
+
// floor of the 9 shipped types), so a new wire type without a fixture —
|
|
42
|
+
// or a shrunken union — turns this red.
|
|
43
|
+
const discriminators = telemetryEventSchema.options.map(
|
|
44
|
+
(option) => option.shape.type.value,
|
|
45
|
+
);
|
|
46
|
+
expect(discriminators.length).toBeGreaterThanOrEqual(9);
|
|
47
|
+
expect(new Set(wireEventSamples.map((event) => event.type))).toEqual(
|
|
48
|
+
new Set(discriminators),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const result = validateWireEvents(wireEventSamples, stubLog);
|
|
52
|
+
expect(result).toEqual({
|
|
53
|
+
checked: wireEventSamples.length,
|
|
54
|
+
invalid: 0,
|
|
55
|
+
unknownTypes: [],
|
|
56
|
+
});
|
|
57
|
+
expect(warnCalls).toHaveLength(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("invalid events are counted and warned with {path, code} issues and no field values", () => {
|
|
61
|
+
const sentinel = "SENTINEL_PII_VALUE_do_not_log";
|
|
62
|
+
const invalidTurn = {
|
|
63
|
+
...turnEventSample,
|
|
64
|
+
// Nested object values violate the wire contract's client bounds; the
|
|
65
|
+
// sentinel must never appear in any warn payload.
|
|
66
|
+
client: { nested: { secret: sentinel } },
|
|
67
|
+
};
|
|
68
|
+
const invalidLifecycle = {
|
|
69
|
+
type: "lifecycle",
|
|
70
|
+
// Mirrors the activation-funnel id shape (`version:sessionId:step`),
|
|
71
|
+
// which embeds the onboarding session id and exceeds the wire schema's
|
|
72
|
+
// 36-char daemon_event_id bound — the id itself must never be logged.
|
|
73
|
+
daemon_event_id: `activation_v1_2026_06:${sentinel}:activation_moment_1_complete`,
|
|
74
|
+
recorded_at: 1_700_000_000_011,
|
|
75
|
+
assistant_version: sentinel,
|
|
76
|
+
// event_name is missing.
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const result = validateWireEvents([invalidTurn, invalidLifecycle], stubLog);
|
|
80
|
+
expect(result).toEqual({ checked: 2, invalid: 2, unknownTypes: [] });
|
|
81
|
+
expect(warnCalls).toHaveLength(2);
|
|
82
|
+
|
|
83
|
+
for (const call of warnCalls) {
|
|
84
|
+
const bag = call[0] as {
|
|
85
|
+
eventType: string;
|
|
86
|
+
issues: Array<Record<string, unknown>>;
|
|
87
|
+
};
|
|
88
|
+
// The bag carries the event type and issues only — no daemon_event_id
|
|
89
|
+
// (it is a payload value; activation ids embed the session id).
|
|
90
|
+
expect(Object.keys(bag).sort()).toEqual(["eventType", "issues"]);
|
|
91
|
+
expect(bag.issues.length).toBeGreaterThan(0);
|
|
92
|
+
for (const issue of bag.issues) {
|
|
93
|
+
// Issues carry path + code only — never messages or field values.
|
|
94
|
+
expect(Object.keys(issue).sort()).toEqual(["code", "path"]);
|
|
95
|
+
expect(issue.path).toBeString();
|
|
96
|
+
expect(issue.code).toBeString();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
expect(
|
|
100
|
+
(warnCalls[0][0] as { issues: Array<{ path: string }> }).issues[0].path,
|
|
101
|
+
).toStartWith("client");
|
|
102
|
+
const lifecyclePaths = (
|
|
103
|
+
warnCalls[1][0] as { issues: Array<{ path: string }> }
|
|
104
|
+
).issues.map((issue) => issue.path);
|
|
105
|
+
expect(lifecyclePaths).toContain("daemon_event_id");
|
|
106
|
+
expect(lifecyclePaths).toContain("event_name");
|
|
107
|
+
|
|
108
|
+
// The planted sentinel (a field value) must be absent from all log args.
|
|
109
|
+
expect(JSON.stringify(warnCalls)).not.toInclude(sentinel);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("dynamic record keys in issue paths are redacted to *", () => {
|
|
113
|
+
const sentinel = "sentinel-user@example.com";
|
|
114
|
+
const invalidTurn = {
|
|
115
|
+
...turnEventSample,
|
|
116
|
+
// The client bag's keys come from an uncontrolled JSON metadata column;
|
|
117
|
+
// the superRefine emits issues at ['client', <key>], so a key carrying
|
|
118
|
+
// user text must be redacted from the logged path. The nested object
|
|
119
|
+
// value makes the bag invalid.
|
|
120
|
+
client: { [sentinel]: { nested: true } },
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const result = validateWireEvents([invalidTurn], stubLog);
|
|
124
|
+
expect(result).toEqual({ checked: 1, invalid: 1, unknownTypes: [] });
|
|
125
|
+
expect(warnCalls).toHaveLength(1);
|
|
126
|
+
|
|
127
|
+
const bag = warnCalls[0][0] as { issues: Array<{ path: string }> };
|
|
128
|
+
// The dynamic key component is replaced with a literal `*`; the
|
|
129
|
+
// schema-defined depth-0 component (`client`) is kept.
|
|
130
|
+
expect(bag.issues.map((issue) => issue.path)).toContain("client.*");
|
|
131
|
+
expect(JSON.stringify(warnCalls)).not.toInclude(sentinel);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("unknown event types are reported and warned once per process per type", () => {
|
|
135
|
+
const first = validateWireEvents([makeOnboardingResearchEvent()], stubLog);
|
|
136
|
+
const second = validateWireEvents([makeOnboardingResearchEvent()], stubLog);
|
|
137
|
+
|
|
138
|
+
expect(first).toEqual({
|
|
139
|
+
checked: 0,
|
|
140
|
+
invalid: 0,
|
|
141
|
+
unknownTypes: ["onboarding_research"],
|
|
142
|
+
});
|
|
143
|
+
expect(second.unknownTypes).toEqual(["onboarding_research"]);
|
|
144
|
+
|
|
145
|
+
// Rate-limited: the warn fires exactly once across both calls.
|
|
146
|
+
expect(warnCalls).toHaveLength(1);
|
|
147
|
+
expect(warnCalls[0][0]).toEqual({ eventType: "onboarding_research" });
|
|
148
|
+
});
|
|
149
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pre-flush validation of outgoing telemetry events against the
|
|
3
|
+
* platform-generated wire schemas (`telemetry-wire.generated.ts`).
|
|
4
|
+
*
|
|
5
|
+
* The platform ingest endpoint rejects individual events that violate its
|
|
6
|
+
* serializer bounds and skips event types it has no serializer for — both
|
|
7
|
+
* silently from the daemon's point of view (the batch still 2xxes). This
|
|
8
|
+
* module surfaces those would-be-silent drops as structured warnings at the
|
|
9
|
+
* source, right before each POST.
|
|
10
|
+
*
|
|
11
|
+
* Observability only: validation never mutates, filters, or blocks a batch.
|
|
12
|
+
* The server remains the authority on what it accepts; in particular the
|
|
13
|
+
* `.trim()`-transformed parse output is never substituted for the original
|
|
14
|
+
* events.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { z } from "zod";
|
|
18
|
+
|
|
19
|
+
import type { getLogger } from "../util/logger.js";
|
|
20
|
+
import { telemetryEventSchema } from "./telemetry-wire.generated.js";
|
|
21
|
+
|
|
22
|
+
type Logger = ReturnType<typeof getLogger>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Per-event-type wire schema, derived by introspecting the generated
|
|
26
|
+
* discriminated union (`.options` + each member's `type` literal), so a new
|
|
27
|
+
* generated event type is validated with zero hand edits here.
|
|
28
|
+
*/
|
|
29
|
+
const wireSchemaByType: ReadonlyMap<string, z.ZodType> = new Map(
|
|
30
|
+
telemetryEventSchema.options.map((option) => [
|
|
31
|
+
option.shape.type.value,
|
|
32
|
+
option,
|
|
33
|
+
]),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Event types already warned about as absent from the wire contract.
|
|
38
|
+
* Unknown-type warnings are rate-limited to once per process per type —
|
|
39
|
+
* without this, a daemon-only extension type (e.g. `onboarding_research`)
|
|
40
|
+
* would warn on every 5-minute flush forever.
|
|
41
|
+
*/
|
|
42
|
+
const warnedUnknownTypes = new Set<string>();
|
|
43
|
+
|
|
44
|
+
/** Clears the once-per-process unknown-type warning rate limit. Test-only. */
|
|
45
|
+
export function resetUnknownTypeWarningsForTests(): void {
|
|
46
|
+
warnedUnknownTypes.clear();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Render a zod issue path for logging without leaking dynamic record keys.
|
|
51
|
+
*
|
|
52
|
+
* Rule: keep numeric components (array indices — structural, never data) and
|
|
53
|
+
* keep the first (depth-0) component; redact every deeper string component to
|
|
54
|
+
* a literal `*`. Depth-0 is safe because every generated event schema is a
|
|
55
|
+
* flat `z.object`, so top-level path components are schema-defined field
|
|
56
|
+
* names. Deeper string components can be dynamic record keys — e.g. the turn
|
|
57
|
+
* schema's `client` bag superRefine emits issues at `['client', <key>]` where
|
|
58
|
+
* `<key>` comes from an uncontrolled JSON metadata column and could carry
|
|
59
|
+
* user text.
|
|
60
|
+
*/
|
|
61
|
+
function sanitizeIssuePath(path: ReadonlyArray<PropertyKey>): string {
|
|
62
|
+
return path
|
|
63
|
+
.map((component, depth) =>
|
|
64
|
+
typeof component === "number" || depth === 0 ? String(component) : "*",
|
|
65
|
+
)
|
|
66
|
+
.join(".");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface WireValidationResult {
|
|
70
|
+
/** Events whose type had a wire schema and were parsed against it. */
|
|
71
|
+
checked: number;
|
|
72
|
+
/** Checked events that failed their wire schema. */
|
|
73
|
+
invalid: number;
|
|
74
|
+
/** Distinct event types with no wire schema — the server drops these. */
|
|
75
|
+
unknownTypes: string[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validate a batch of outgoing telemetry events against the platform wire
|
|
80
|
+
* schemas, logging a structured warning for each event the server would
|
|
81
|
+
* silently drop. Warn payloads carry the event `type` and issue
|
|
82
|
+
* `{ path, code }` shapes only — never field values, and never dynamic key
|
|
83
|
+
* components inside paths (see {@link sanitizeIssuePath}). That includes
|
|
84
|
+
* `daemon_event_id`: traces/claims can hold PII, and activation-funnel ids
|
|
85
|
+
* embed the onboarding session id.
|
|
86
|
+
*
|
|
87
|
+
* Never mutates, filters, or blocks: callers send the batch unchanged
|
|
88
|
+
* regardless of the result.
|
|
89
|
+
*/
|
|
90
|
+
export function validateWireEvents(
|
|
91
|
+
events: readonly { type: string }[],
|
|
92
|
+
log: Logger,
|
|
93
|
+
): WireValidationResult {
|
|
94
|
+
let checked = 0;
|
|
95
|
+
let invalid = 0;
|
|
96
|
+
const unknownTypes = new Set<string>();
|
|
97
|
+
for (const event of events) {
|
|
98
|
+
const schema = wireSchemaByType.get(event.type);
|
|
99
|
+
if (!schema) {
|
|
100
|
+
unknownTypes.add(event.type);
|
|
101
|
+
if (!warnedUnknownTypes.has(event.type)) {
|
|
102
|
+
warnedUnknownTypes.add(event.type);
|
|
103
|
+
log.warn(
|
|
104
|
+
{ eventType: event.type },
|
|
105
|
+
"telemetry event type not in platform wire contract — server drops these; see telemetry-wire.generated.ts",
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
checked += 1;
|
|
111
|
+
const result = schema.safeParse(event);
|
|
112
|
+
if (!result.success) {
|
|
113
|
+
invalid += 1;
|
|
114
|
+
const issues = result.error.issues.map((issue) => ({
|
|
115
|
+
path: sanitizeIssuePath(issue.path),
|
|
116
|
+
code: issue.code,
|
|
117
|
+
}));
|
|
118
|
+
log.warn(
|
|
119
|
+
{ eventType: event.type, issues },
|
|
120
|
+
"telemetry event fails platform wire contract — server will silently drop it",
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { checked, invalid, unknownTypes: [...unknownTypes] };
|
|
125
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
makeOnboardingResearchEvent,
|
|
5
|
+
turnEventSample,
|
|
6
|
+
wireEventSamples,
|
|
7
|
+
} from "./__tests__/telemetry-event-fixtures.js";
|
|
8
|
+
import { telemetryEventSchema } from "./telemetry-wire.generated.js";
|
|
9
|
+
import type { TurnTelemetryEvent } from "./types.js";
|
|
10
|
+
|
|
11
|
+
// Runtime contract test: events constructed with the daemon's types (the
|
|
12
|
+
// shared fixtures are annotated with them) must parse against the generated
|
|
13
|
+
// wire schemas — the same validation the platform's ingest serializers
|
|
14
|
+
// apply. A daemon type whose values can't round-trip through
|
|
15
|
+
// `telemetryEventSchema` produces events the server silently drops.
|
|
16
|
+
|
|
17
|
+
describe("daemon telemetry types against the wire contract", () => {
|
|
18
|
+
for (const sample of wireEventSamples) {
|
|
19
|
+
test(`daemon-typed ${sample.type} event parses against the wire schema`, () => {
|
|
20
|
+
const result = telemetryEventSchema.safeParse(sample);
|
|
21
|
+
expect(result.error).toBeUndefined();
|
|
22
|
+
expect(result.success).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test("turn client bag with a nested value fails the wire superRefine", () => {
|
|
27
|
+
// Structurally valid for the daemon's `TurnTelemetryClientInfo` (extra
|
|
28
|
+
// properties are allowed outside fresh-literal checks), but the wire
|
|
29
|
+
// schema mirrors the server's validate_client: nested objects in the
|
|
30
|
+
// `client` bag reject the event at ingest.
|
|
31
|
+
const nestedClient = { os: "macos", screen: { width: 1512, height: 982 } };
|
|
32
|
+
const invalidTurn: TurnTelemetryEvent = {
|
|
33
|
+
...turnEventSample,
|
|
34
|
+
client: nestedClient,
|
|
35
|
+
};
|
|
36
|
+
const result = telemetryEventSchema.safeParse(invalidTurn);
|
|
37
|
+
expect(result.success).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("onboarding_research is not in the wire contract — the server drops it", () => {
|
|
41
|
+
// Documents the extension gap: `onboarding_research` is daemon-only and
|
|
42
|
+
// has no platform ingest serializer, so the server silently drops these
|
|
43
|
+
// events. The platform-repo follow-up (serializer + Terraform + dbt)
|
|
44
|
+
// closes this; until then the schema rejects the unknown discriminator.
|
|
45
|
+
const result = telemetryEventSchema.safeParse(
|
|
46
|
+
makeOnboardingResearchEvent(),
|
|
47
|
+
);
|
|
48
|
+
expect(result.success).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
});
|