-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathindex.ts
More file actions
541 lines (514 loc) · 15.7 KB
/
index.ts
File metadata and controls
541 lines (514 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import { Account } from "@/account"
import { Config } from "@/config/config"
import { Installation } from "@/installation"
import { Log } from "@/util/log"
import { createHash } from "crypto"
const log = Log.create({ service: "telemetry" })
export namespace Telemetry {
const FLUSH_INTERVAL_MS = 5_000
const MAX_BUFFER_SIZE = 200
const REQUEST_TIMEOUT_MS = 10_000
export type TokensPayload = {
input: number
output: number
reasoning: number
cache_read: number
cache_write: number
}
export type Event =
| {
type: "session_start"
timestamp: number
session_id: string
model_id: string
provider_id: string
agent: string
project_id: string
}
| {
type: "session_end"
timestamp: number
session_id: string
total_cost: number
total_tokens: number
tool_call_count: number
duration_ms: number
}
| {
type: "generation"
timestamp: number
session_id: string
message_id: string
model_id: string
provider_id: string
agent: string
finish_reason: string
tokens: TokensPayload
cost: number
duration_ms: number
}
| {
type: "tool_call"
timestamp: number
session_id: string
message_id: string
tool_name: string
tool_type: "standard" | "mcp"
tool_category: string
status: "success" | "error"
duration_ms: number
sequence_index: number
previous_tool: string | null
error?: string
}
| {
type: "bridge_call"
timestamp: number
session_id: string
method: string
status: "success" | "error"
duration_ms: number
error?: string
}
| {
type: "error"
timestamp: number
session_id: string
error_name: string
error_message: string
context: string
}
| {
type: "command"
timestamp: number
session_id: string
command_name: string
command_source: "command" | "mcp" | "skill" | "unknown"
message_id: string
}
| {
type: "context_overflow_recovered"
timestamp: number
session_id: string
model_id: string
provider_id: string
tokens_used: number
}
| {
type: "compaction_triggered"
timestamp: number
session_id: string
trigger: "overflow_detection" | "error_recovery"
attempt: number
}
| {
type: "tool_outputs_pruned"
timestamp: number
session_id: string
count: number
tokens_pruned: number
}
| {
type: "auth_login"
timestamp: number
session_id: string
provider_id: string
method: "oauth" | "api_key"
status: "success" | "error"
error?: string
}
| {
type: "auth_logout"
timestamp: number
session_id: string
provider_id: string
}
| {
type: "mcp_server_status"
timestamp: number
session_id: string
server_name: string
transport: "stdio" | "sse" | "streamable-http"
status: "connected" | "disconnected" | "error"
error?: string
duration_ms?: number
}
| {
type: "provider_error"
timestamp: number
session_id: string
provider_id: string
model_id: string
error_type: string
error_message: string
http_status?: number
}
| {
type: "engine_started"
timestamp: number
session_id: string
engine_version: string
python_version: string
extras?: string
status: "started" | "restarted" | "upgraded"
duration_ms: number
}
| {
type: "engine_error"
timestamp: number
session_id: string
phase: "uv_download" | "venv_create" | "pip_install" | "startup" | "runtime"
error_message: string
}
| {
type: "upgrade_attempted"
timestamp: number
session_id: string
from_version: string
to_version: string
method: "npm" | "bun" | "brew" | "other"
status: "success" | "error"
error?: string
}
| {
type: "session_forked"
timestamp: number
session_id: string
parent_session_id: string
message_count: number
}
| {
type: "permission_denied"
timestamp: number
session_id: string
tool_name: string
tool_category: string
source: "user" | "config_rule"
}
| {
type: "doom_loop_detected"
timestamp: number
session_id: string
tool_name: string
repeat_count: number
}
| {
type: "environment_census"
timestamp: number
session_id: string
warehouse_types: string[]
warehouse_count: number
dbt_detected: boolean
dbt_adapter: string | null
dbt_model_count_bucket: string
dbt_source_count_bucket: string
dbt_test_count_bucket: string
connection_sources: string[]
mcp_server_count: number
skill_count: number
os: string
feature_flags: string[]
}
| {
type: "context_utilization"
timestamp: number
session_id: string
model_id: string
tokens_used: number
context_limit: number
utilization_pct: number
generation_number: number
cache_hit_ratio: number
}
| {
type: "agent_outcome"
timestamp: number
session_id: string
agent: string
tool_calls: number
generations: number
duration_ms: number
cost: number
compactions: number
outcome: "completed" | "abandoned" | "aborted" | "error"
}
| {
type: "error_recovered"
timestamp: number
session_id: string
error_type: string
recovery_strategy: string
attempts: number
recovered: boolean
duration_ms: number
}
| {
type: "mcp_server_census"
timestamp: number
session_id: string
server_name: string
transport: "stdio" | "sse" | "streamable-http"
tool_count: number
resource_count: number
}
| {
type: "memory_operation"
timestamp: number
session_id: string
operation: "write" | "delete"
scope: "global" | "project"
block_id: string
is_update: boolean
duplicate_count: number
tags_count: number
}
| {
type: "memory_injection"
timestamp: number
session_id: string
block_count: number
total_chars: number
budget: number
scopes_used: string[]
}
const FILE_TOOLS = new Set(["read", "write", "edit", "glob", "grep", "bash"])
// Order matters: more specific patterns (e.g. "warehouse_usage") are checked
// before broader ones (e.g. "warehouse") to avoid miscategorization.
const CATEGORY_PATTERNS: Array<{ category: string; keywords: string[] }> = [
{ category: "finops", keywords: ["cost", "finops", "warehouse_usage"] },
{ category: "sql", keywords: ["sql", "query"] },
{ category: "schema", keywords: ["schema", "column", "table"] },
{ category: "dbt", keywords: ["dbt"] },
{ category: "warehouse", keywords: ["warehouse", "connection"] },
{ category: "lineage", keywords: ["lineage", "dag"] },
{ category: "memory", keywords: ["memory"] },
]
export function categorizeToolName(name: string, type: "standard" | "mcp"): string {
if (type === "mcp") return "mcp"
const n = name.toLowerCase()
if (FILE_TOOLS.has(n)) return "file"
for (const { category, keywords } of CATEGORY_PATTERNS) {
if (keywords.some((kw) => n.includes(kw))) return category
}
return "standard"
}
export function bucketCount(n: number): string {
if (n <= 0) return "0"
if (n <= 10) return "1-10"
if (n <= 50) return "10-50"
if (n <= 200) return "50-200"
return "200+"
}
type AppInsightsConfig = {
iKey: string
endpoint: string // e.g. https://xxx.applicationinsights.azure.com/v2/track
}
let enabled = false
let buffer: Event[] = []
let flushTimer: ReturnType<typeof setInterval> | undefined
let userEmail = ""
let sessionId = ""
let projectId = ""
let appInsights: AppInsightsConfig | undefined
let droppedEvents = 0
let initPromise: Promise<void> | undefined
let initDone = false
function parseConnectionString(cs: string): AppInsightsConfig | undefined {
const parts: Record<string, string> = {}
for (const segment of cs.split(";")) {
const idx = segment.indexOf("=")
if (idx === -1) continue
parts[segment.slice(0, idx).trim()] = segment.slice(idx + 1).trim()
}
const iKey = parts["InstrumentationKey"]
const ingestionEndpoint = parts["IngestionEndpoint"]
if (!iKey || !ingestionEndpoint) return undefined
const base = ingestionEndpoint.endsWith("/") ? ingestionEndpoint : ingestionEndpoint + "/"
return { iKey, endpoint: `${base}v2/track` }
}
function toAppInsightsEnvelopes(events: Event[], cfg: AppInsightsConfig): object[] {
return events.map((event) => {
const { type, timestamp, ...fields } = event as any
const sid: string = fields.session_id ?? sessionId
const properties: Record<string, string> = {
cli_version: Installation.VERSION,
project_id: fields.project_id ?? projectId,
}
const measurements: Record<string, number> = {}
// Flatten all fields — nested `tokens` object gets prefixed keys
for (const [k, v] of Object.entries(fields)) {
if (k === "session_id" || k === "project_id") continue
if (k === "tokens" && typeof v === "object" && v !== null) {
for (const [tk, tv] of Object.entries(v as Record<string, unknown>)) {
if (typeof tv === "number") measurements[`tokens_${tk}`] = tv
}
} else if (typeof v === "number") {
measurements[k] = v
} else if (v !== undefined && v !== null) {
properties[k] = typeof v === "object" ? JSON.stringify(v) : String(v)
}
}
return {
name: `Microsoft.ApplicationInsights.${cfg.iKey}.Event`,
time: new Date(timestamp).toISOString(),
iKey: cfg.iKey,
tags: {
"ai.session.id": sid || "startup",
"ai.user.id": userEmail,
"ai.cloud.role": "altimate",
"ai.application.ver": Installation.VERSION,
},
data: {
baseType: "EventData",
baseData: {
ver: 2,
name: type,
properties,
measurements,
},
},
}
})
}
// Instrumentation key is intentionally public — safe to hardcode in client-side tooling.
// Override with APPLICATIONINSIGHTS_CONNECTION_STRING env var for local dev / testing.
const DEFAULT_CONNECTION_STRING =
"InstrumentationKey=5095f5e6-477e-4262-b7ae-2118de18550d;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=6564474f-329b-4b7d-849e-e70cb4181294"
// Deduplicates concurrent calls: non-awaited init() in middleware/worker
// won't race with await init() in session prompt.
export function init(): Promise<void> {
if (!initPromise) {
initPromise = doInit()
}
return initPromise
}
async function doInit() {
try {
if (process.env.ALTIMATE_TELEMETRY_DISABLED === "true") {
buffer = []
return
}
// Config.get() may throw outside Instance context (e.g. CLI middleware
// before Instance.provide()). Treat config failures as "not disabled" —
// the env var check above is the early-init escape hatch.
try {
const userConfig = await Config.get() as any
if (userConfig.telemetry?.disabled) {
buffer = []
return
}
} catch {
// Config unavailable — proceed with telemetry enabled
}
// App Insights: env var overrides default (for dev/testing), otherwise use the baked-in key
const connectionString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING ?? DEFAULT_CONNECTION_STRING
const cfg = parseConnectionString(connectionString)
if (!cfg) {
buffer = []
return
}
appInsights = cfg
try {
const account = Account.active()
if (account) {
userEmail = createHash("sha256").update(account.email.toLowerCase().trim()).digest("hex")
}
} catch {
// Account unavailable — proceed without user ID
}
enabled = true
log.info("telemetry initialized", { mode: "appinsights" })
const timer = setInterval(flush, FLUSH_INTERVAL_MS)
if (typeof timer === "object" && timer && "unref" in timer) (timer as any).unref()
flushTimer = timer
} catch {
buffer = []
} finally {
initDone = true
}
}
export function setContext(opts: { sessionId: string; projectId: string }) {
sessionId = opts.sessionId
projectId = opts.projectId
}
export function getContext() {
return { sessionId, projectId }
}
/** Returns true only after init() has completed and telemetry is enabled. */
export function isEnabled(): boolean {
return initDone && enabled
}
export function track(event: Event) {
// Before init completes: buffer (flushed once init enables, or cleared if disabled).
// After init completed and disabled telemetry: drop silently.
if (initDone && !enabled) return
buffer.push(event)
if (buffer.length > MAX_BUFFER_SIZE) {
buffer.shift()
droppedEvents++
}
}
export async function flush() {
if (!enabled || buffer.length === 0 || !appInsights) return
const events = buffer.splice(0, buffer.length)
if (droppedEvents > 0) {
events.push({
type: "error",
timestamp: Date.now(),
session_id: sessionId,
error_name: "TelemetryBufferOverflow",
error_message: `${droppedEvents} events dropped due to buffer overflow`,
context: "telemetry",
} as Event)
droppedEvents = 0
}
const controller = new AbortController()
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS)
try {
const response = await fetch(appInsights.endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(toAppInsightsEnvelopes(events, appInsights)),
signal: controller.signal,
})
if (!response.ok) {
log.debug("telemetry flush failed", { status: response.status })
}
} catch {
// Re-add events that haven't been retried yet to avoid data loss
const retriable = events.filter((e) => !(e as any)._retried)
for (const e of retriable) {
;(e as any)._retried = true
}
const space = Math.max(0, MAX_BUFFER_SIZE - buffer.length)
buffer.unshift(...retriable.slice(0, space))
} finally {
clearTimeout(timeout)
}
}
export async function shutdown() {
// Wait for init to complete so we know whether telemetry is enabled
// and have a valid endpoint to flush to. init() is fire-and-forget
// in CLI middleware, so it may still be in-flight when shutdown runs.
if (initPromise) {
try {
await initPromise
} catch {
// init failed — nothing to flush
}
}
if (flushTimer) {
clearInterval(flushTimer)
flushTimer = undefined
}
await flush()
enabled = false
appInsights = undefined
buffer = []
droppedEvents = 0
sessionId = ""
projectId = ""
initPromise = undefined
initDone = false
}
}