@bli-cockpit/telemetry-core 0.1.111 → 0.1.112

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 (35) hide show
  1. package/dist/collector-heartbeat.d.ts +17 -0
  2. package/dist/evidence-upload.d.ts +16 -0
  3. package/dist/evidence-upload.js +16 -0
  4. package/dist/index.d.ts +3 -0
  5. package/dist/index.js +3 -0
  6. package/dist/log-digest/log-projects.d.ts +1 -0
  7. package/dist/log-digest/log-projects.js +10 -3
  8. package/dist/log-digest/sources/collector.d.ts +2 -6
  9. package/dist/log-digest/sources/collector.js +70 -30
  10. package/dist/log-digest/sources/types.d.ts +1 -0
  11. package/dist/log-digest/sources/vercel-query.d.ts +17 -0
  12. package/dist/log-digest/sources/vercel-query.js +86 -0
  13. package/dist/log-digest/sources/vercel.d.ts +1 -1
  14. package/dist/log-digest/sources/vercel.js +77 -13
  15. package/dist/log-digest/tagged.d.ts +0 -1
  16. package/dist/log-digest/tagged.js +2 -4
  17. package/dist/log-digest/tower-classes.js +9 -1
  18. package/dist/mail-time.d.ts +1 -0
  19. package/dist/mail-time.js +24 -0
  20. package/dist/memory-hook-hours.d.ts +1 -0
  21. package/dist/memory-hook-hours.js +2 -1
  22. package/dist/memory-install-receipt.d.ts +18 -0
  23. package/dist/memory-install-receipt.js +12 -0
  24. package/dist/newest-check-runs.d.ts +21 -0
  25. package/dist/newest-check-runs.js +20 -0
  26. package/dist/provider-billing.d.ts +5 -0
  27. package/dist/provider-billing.js +70 -0
  28. package/dist/session-facts-text.js +3 -0
  29. package/dist/session-facts.d.ts +2 -1
  30. package/dist/session-facts.js +10 -3
  31. package/dist/session-git-refs.d.ts +41 -0
  32. package/dist/session-git-refs.js +362 -0
  33. package/dist/work-project-reference.d.ts +7 -0
  34. package/dist/work-project-reference.js +8 -0
  35. package/package.json +17 -2
@@ -0,0 +1,24 @@
1
+ function unknownMailTime(reason) {
2
+ console.warn("[mail time] timestamp unreadable", JSON.stringify({ reason }));
3
+ return "unknown time";
4
+ }
5
+ export function formatMailTime(instant, timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC") {
6
+ if (typeof instant !== "string" || !instant.trim())
7
+ return unknownMailTime("missing_timestamp");
8
+ const date = new Date(instant);
9
+ if (!Number.isFinite(date.getTime()))
10
+ return unknownMailTime("invalid_timestamp");
11
+ const parts = new Intl.DateTimeFormat("en-GB", {
12
+ timeZone,
13
+ year: "numeric",
14
+ month: "2-digit",
15
+ day: "2-digit",
16
+ hour: "2-digit",
17
+ minute: "2-digit",
18
+ hourCycle: "h23",
19
+ timeZoneName: "shortOffset",
20
+ }).formatToParts(date);
21
+ const fields = Object.fromEntries(parts.map(part => [part.type, part.value]));
22
+ const zone = ["GMT", "GMT+0"].includes(fields.timeZoneName) ? "UTC" : fields.timeZoneName;
23
+ return `${fields.year}-${fields.month}-${fields.day} ${fields.hour}:${fields.minute} ${zone}`;
24
+ }
@@ -19,6 +19,7 @@ export declare const MemoryHookHourSchema: z.ZodObject<{
19
19
  printed: z.ZodNumber;
20
20
  timeouts: z.ZodNumber;
21
21
  failed: z.ZodNumber;
22
+ skipped: z.ZodOptional<z.ZodNumber>;
22
23
  }, z.core.$strict>;
23
24
  export type MemoryHookHour = z.infer<typeof MemoryHookHourSchema>;
24
25
  /** The prompt hook's hours inside the last 24 (the current hour included), oldest first, hours with no run left out. */
@@ -21,6 +21,7 @@ export const MemoryHookHourSchema = z
21
21
  printed: z.number().int().min(0),
22
22
  timeouts: z.number().int().min(0),
23
23
  failed: z.number().int().min(0),
24
+ skipped: z.number().int().min(0).optional(),
24
25
  })
25
26
  .strict();
26
27
  /** The prompt hook's hours inside the last 24 (the current hour included), oldest first, hours with no run left out. */
@@ -35,7 +36,7 @@ export function promptHookHours(file, now) {
35
36
  const counts = events.prompt;
36
37
  if (!counts || counts.runs === 0)
37
38
  continue;
38
- hours.push({ hour, runs: counts.runs, printed: counts.printed, timeouts: counts.timeouts, failed: counts.failed });
39
+ hours.push({ hour, runs: counts.runs, printed: counts.printed, timeouts: counts.timeouts, failed: counts.failed, skipped: counts.skipped });
39
40
  }
40
41
  return hours.sort((left, right) => left.hour.localeCompare(right.hour));
41
42
  }
@@ -71,6 +71,20 @@ export type MemoryInstallHooksPiece = z.infer<typeof MemoryInstallHooksPieceSche
71
71
  export declare const MemoryInstallReceiptSchema: z.ZodObject<{
72
72
  schema_version: z.ZodLiteral<"memory-install-receipt.v1">;
73
73
  checked_at: z.ZodString;
74
+ tower: z.ZodOptional<z.ZodObject<{
75
+ claude: z.ZodEnum<{
76
+ unknown: "unknown";
77
+ ok: "ok";
78
+ stale: "stale";
79
+ missing: "missing";
80
+ }>;
81
+ codex: z.ZodEnum<{
82
+ unknown: "unknown";
83
+ ok: "ok";
84
+ stale: "stale";
85
+ missing: "missing";
86
+ }>;
87
+ }, z.core.$strict>>;
74
88
  claude: z.ZodObject<{
75
89
  mcp: z.ZodEnum<{
76
90
  unknown: "unknown";
@@ -115,6 +129,8 @@ export declare const MemoryInstallReceiptSchema: z.ZodObject<{
115
129
  hook_failed_24h: z.ZodOptional<z.ZodNumber>;
116
130
  hook_via_daemon_24h: z.ZodOptional<z.ZodNumber>;
117
131
  hook_via_direct_24h: z.ZodOptional<z.ZodNumber>;
132
+ hook_skipped_24h: z.ZodOptional<z.ZodNumber>;
133
+ hook_skipped_automated_24h: z.ZodOptional<z.ZodNumber>;
118
134
  hook_skipped_trivial_24h: z.ZodOptional<z.ZodNumber>;
119
135
  hook_billing_exhausted_24h: z.ZodOptional<z.ZodNumber>;
120
136
  hook_chars_p50: z.ZodOptional<z.ZodNumber>;
@@ -130,6 +146,7 @@ export declare const MemoryInstallReceiptSchema: z.ZodObject<{
130
146
  printed: z.ZodNumber;
131
147
  timeouts: z.ZodNumber;
132
148
  failed: z.ZodNumber;
149
+ skipped: z.ZodOptional<z.ZodNumber>;
133
150
  }, z.core.$strict>>>;
134
151
  hook_stats_reason: z.ZodOptional<z.ZodString>;
135
152
  hook_performance: z.ZodOptional<z.ZodObject<{
@@ -176,6 +193,7 @@ export declare const MemoryInstallReceiptSchema: z.ZodObject<{
176
193
  }, z.core.$strict>>;
177
194
  }, z.core.$strict>;
178
195
  export type MemoryInstallReceipt = z.infer<typeof MemoryInstallReceiptSchema>;
196
+ export declare function towerMcpInstallLine(receipt: MemoryInstallReceipt): string;
179
197
  /** Every piece is `ok`. The one question the board asks first. */
180
198
  export declare function memoryInstallFullyOn(receipt: MemoryInstallReceipt): boolean;
181
199
  /**
@@ -79,6 +79,10 @@ export const MemoryInstallReceiptSchema = z
79
79
  .object({
80
80
  schema_version: z.literal(MEMORY_INSTALL_RECEIPT_SCHEMA_VERSION),
81
81
  checked_at: IsoDateTimeSchema,
82
+ tower: z.object({
83
+ claude: MemoryInstallPieceSchema,
84
+ codex: MemoryInstallPieceSchema,
85
+ }).strict().optional(),
82
86
  claude: z
83
87
  .object({
84
88
  mcp: MemoryInstallPieceSchema,
@@ -154,6 +158,8 @@ export const MemoryInstallReceiptSchema = z
154
158
  * machine is on a memory-mcp older than the rule, never that it skipped
155
159
  * none.
156
160
  */
161
+ hook_skipped_24h: z.number().int().min(0).optional(),
162
+ hook_skipped_automated_24h: z.number().int().min(0).optional(),
157
163
  hook_skipped_trivial_24h: z.number().int().min(0).optional(),
158
164
  /**
159
165
  * BLI-3891: runs whose recall failed because the PROVIDER ACCOUNT has no
@@ -217,6 +223,12 @@ export const MemoryInstallReceiptSchema = z
217
223
  hook_performance: MemoryHookPerformanceSchema.optional(),
218
224
  })
219
225
  .strict();
226
+ export function towerMcpInstallLine(receipt) {
227
+ if (!receipt.tower)
228
+ return "";
229
+ const mark = (piece) => piece === "ok" ? "✓" : piece;
230
+ return ` · Tower MCP Claude ${mark(receipt.tower.claude)} Codex ${mark(receipt.tower.codex)}`;
231
+ }
220
232
  /** Every piece is `ok`. The one question the board asks first. */
221
233
  export function memoryInstallFullyOn(receipt) {
222
234
  return (receipt.claude.mcp === "ok" &&
@@ -0,0 +1,21 @@
1
+ /**
2
+ * BLI-26545: GitHub keeps every run of a check on a commit. When a pull request leaves draft, the Visual QA run already in
3
+ * flight is cancelled and a new one starts; both stay on the head commit, in different check suites. Branch protection and
4
+ * GitHub's own merge box judge each check by its newest run, so Tower's CI verdict does too: a cancelled or failed run that a
5
+ * later run of the same check replaced is history, not the current state.
6
+ *
7
+ * Runs share a check when their keys match (the caller decides the key: app and name for the REST list, workflow and name
8
+ * for the GraphQL rollup). The newest is the highest GitHub id, which only grows; when an id is missing, the later start
9
+ * time wins; when both are missing, the later run in the list wins.
10
+ */
11
+ export interface CheckRunOrder {
12
+ key: string;
13
+ workflowRunId?: number | null;
14
+ id: number | null;
15
+ startedAt: string | null;
16
+ }
17
+ /** The newest run of each check, in the order the checks first appear; `superseded` counts the older runs left out. */
18
+ export declare function newestRunPerCheck<T>(runs: readonly T[], orderOf: (run: T) => CheckRunOrder): {
19
+ runs: T[];
20
+ superseded: number;
21
+ };
@@ -0,0 +1,20 @@
1
+ function isNewer(candidate, current) {
2
+ if (candidate.workflowRunId != null && current.workflowRunId != null && candidate.workflowRunId !== current.workflowRunId)
3
+ return candidate.workflowRunId > current.workflowRunId;
4
+ if (candidate.id !== null && current.id !== null)
5
+ return candidate.id > current.id;
6
+ if (candidate.startedAt && current.startedAt)
7
+ return Date.parse(candidate.startedAt) >= Date.parse(current.startedAt);
8
+ return true;
9
+ }
10
+ /** The newest run of each check, in the order the checks first appear; `superseded` counts the older runs left out. */
11
+ export function newestRunPerCheck(runs, orderOf) {
12
+ const newest = new Map();
13
+ for (const run of runs) {
14
+ const order = orderOf(run);
15
+ const held = newest.get(order.key);
16
+ if (!held || isNewer(order, held.order))
17
+ newest.set(order.key, { run, order });
18
+ }
19
+ return { runs: [...newest.values()].map((entry) => entry.run), superseded: runs.length - newest.size };
20
+ }
@@ -0,0 +1,5 @@
1
+ export declare const PROVIDER_BILLING_EXHAUSTED = "provider_billing_exhausted";
2
+ export declare function providerBillingRefusal(error: unknown, scope?: "account" | "request"): {
3
+ status: number | null;
4
+ marker: string;
5
+ } | null;
@@ -0,0 +1,70 @@
1
+ export const PROVIDER_BILLING_EXHAUSTED = "provider_billing_exhausted";
2
+ const BILLING_MARKERS = [
3
+ "insufficient_quota",
4
+ "insufficient quota",
5
+ "no credits remaining",
6
+ "exceeded your current quota",
7
+ "billing_hard_limit_reached",
8
+ "billing hard limit",
9
+ "credit balance is too low",
10
+ "check your plan and billing",
11
+ "quota exceeded for your account",
12
+ ];
13
+ export function providerBillingRefusal(error, scope = "account") {
14
+ const status = statusOf(error);
15
+ if (status === 402)
16
+ return { status, marker: "status_402" };
17
+ if (status !== null && status !== 429)
18
+ return null;
19
+ const text = billingText(error);
20
+ const marker = BILLING_MARKERS.find(candidate => text.includes(candidate));
21
+ if (marker)
22
+ return { status, marker };
23
+ if (scope === "request" && messageOf(error).toLowerCase().startsWith("this request requires more credits, or fewer max_tokens")) {
24
+ return { status, marker: "insufficient_request_credit" };
25
+ }
26
+ return null;
27
+ }
28
+ function statusOf(error) {
29
+ if (typeof error !== "object" || error === null)
30
+ return null;
31
+ const record = error;
32
+ const direct = record["statusCode"];
33
+ if (typeof direct === "number" && Number.isInteger(direct))
34
+ return direct;
35
+ const status = record["status"];
36
+ if (typeof status === "number" && Number.isInteger(status))
37
+ return status;
38
+ return null;
39
+ }
40
+ function billingText(error) {
41
+ const parts = [messageOf(error)];
42
+ if (typeof error === "object" && error !== null) {
43
+ const record = error;
44
+ const body = record["responseBody"];
45
+ if (typeof body === "string")
46
+ parts.push(body);
47
+ const data = record["data"];
48
+ if (data !== undefined && data !== null) {
49
+ try {
50
+ parts.push(JSON.stringify(data));
51
+ }
52
+ catch {
53
+ // A circular or unserialisable `data` tells us nothing about billing;
54
+ // the message and the body above are the fields that carry the marker.
55
+ }
56
+ }
57
+ const cause = record["cause"];
58
+ if (cause !== undefined && cause !== null && cause !== error) {
59
+ parts.push(messageOf(cause));
60
+ }
61
+ }
62
+ return parts.join(" ").slice(0, 4_000).toLowerCase();
63
+ }
64
+ function messageOf(error) {
65
+ if (error instanceof Error)
66
+ return error.message;
67
+ if (typeof error === "string")
68
+ return error;
69
+ return String(error ?? "");
70
+ }
@@ -29,6 +29,9 @@ export function codexUserTurnText(payload) {
29
29
  }
30
30
  export function claudeAssistantText(record) {
31
31
  const message = record["message"];
32
+ if (message && typeof message === "object" && "type" in message && message.type === "message") {
33
+ return textFromPlainTextBlocks("content" in message ? message.content : null);
34
+ }
32
35
  return textFromPlainTextBlocks(message);
33
36
  }
34
37
  export function codexAssistantText(payload) {
@@ -4,7 +4,7 @@ import { type SessionSpawn } from "./session-facts-spawn.js";
4
4
  import { SkillUseTally } from "./session-facts-skills.js";
5
5
  import type { SkillUseCount } from "./session-facts-skills.js";
6
6
  /** Bump when a counting rule changes, never when a field is added. */
7
- export declare const SESSION_FACTS_VERSION = "det.v6";
7
+ export declare const SESSION_FACTS_VERSION = "det.v7";
8
8
  /** How many distinct models and tool names one session may report. */
9
9
  export type SessionFactsSource = "codex" | "claude_code" | "opencode" | "pi";
10
10
  export interface SessionFactsUsage {
@@ -71,6 +71,7 @@ export interface SessionFactsAccumulator {
71
71
  copied_usage_records?: number;
72
72
  usage_by_model: Record<string, SessionFactsUsage>;
73
73
  seen_usage_ids: Set<string>;
74
+ seen_claude_tool_ids: Set<string>;
74
75
  record_count: number;
75
76
  user_prompt_count: number;
76
77
  assistant_response_count: number;
@@ -44,12 +44,13 @@ import { classifyClaudeUserRecord, classifyCodexTaskStarted, codexTurnId, finali
44
44
  import { claudeSpawn, codexSpawn } from "./session-facts-spawn.js";
45
45
  import { observeClaudeSkillUse, observeCodexSkillUse, SKILL_USE_MAX_SKILLS, SkillUseTally } from "./session-facts-skills.js";
46
46
  /** Bump when a counting rule changes, never when a field is added. */
47
- export const SESSION_FACTS_VERSION = "det.v6";
47
+ export const SESSION_FACTS_VERSION = "det.v7";
48
48
  export function newSessionFactsAccumulator(source) {
49
49
  return {
50
50
  source,
51
51
  usage_by_model: {},
52
52
  seen_usage_ids: new Set(),
53
+ seen_claude_tool_ids: new Set(),
53
54
  record_count: 0,
54
55
  user_prompt_count: 0,
55
56
  assistant_response_count: 0,
@@ -137,6 +138,12 @@ function observeClaudeRecord(acc, record) {
137
138
  const b = block;
138
139
  if (b["type"] !== "tool_use")
139
140
  continue;
141
+ const id = typeof b["id"] === "string" ? b["id"] : null;
142
+ if (id) {
143
+ if (acc.seen_claude_tool_ids.has(id))
144
+ continue;
145
+ acc.seen_claude_tool_ids.add(id);
146
+ }
140
147
  countTool(acc, typeof b["name"] === "string" ? b["name"] : "unknown");
141
148
  }
142
149
  observeClaudeSkillUse(acc.skill_tally, record);
@@ -171,8 +178,8 @@ function observeCodexRecord(acc, record) {
171
178
  acc.assistant_response_count += 1;
172
179
  return;
173
180
  }
174
- if (itemType === "function_call") {
175
- countTool(acc, typeof p["name"] === "string" ? p["name"] : "unknown");
181
+ if (itemType === "function_call" || itemType === "custom_tool_call" || itemType === "local_shell_call") {
182
+ countTool(acc, typeof p["name"] === "string" ? p["name"] : itemType === "local_shell_call" ? "local_shell" : "unknown");
176
183
  }
177
184
  observeCodexSkillUse(acc.skill_tally, p);
178
185
  return;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * BLI-27624: every branch, commit and pull request a coding session touched, read from its transcript lines.
3
+ *
4
+ * A session often starts on main and creates its branch partway through, so the one branch the collector records
5
+ * cannot match a pull request's head branch. The transcript says what really happened: Claude stamps every record
6
+ * with the branch it was on, a "git checkout -b" or "git push" names the branch, "git commit" and "git push" print the
7
+ * new commit, and "gh pr create" prints the pull request address.
8
+ *
9
+ * Streaming and allowlisted: only branch names, commit hashes and "owner/repo#number" pairs leave the text. No prompt, reply, command
10
+ * or file is kept. The collector runs it while it reads a file (the same pass as the session facts) and the server runs
11
+ * it over stored evidence, so both sides agree on what a session touched.
12
+ */
13
+ export interface SessionGitRefs {
14
+ branches: string[];
15
+ /** Commit hashes, 7 to 40 hex characters: a printed commit line carries only the short form. */
16
+ commit_shas: string[];
17
+ pull_requests: {
18
+ repo: string;
19
+ number: number;
20
+ }[];
21
+ }
22
+ export declare const GIT_REFS_BRANCH_LIMIT = 50;
23
+ export declare const GIT_REFS_SHA_LIMIT = 200;
24
+ export declare const GIT_REFS_PULL_REQUEST_LIMIT = 30;
25
+ export interface GitRefsAccumulator {
26
+ /** A line of JSONL text: cheap string checks first, so most lines are never parsed. */
27
+ observeLine(line: string): void;
28
+ /** A record the caller already parsed. */
29
+ observeRecord(record: Record<string, unknown>): void;
30
+ finalize(): SessionGitRefs;
31
+ }
32
+ export declare function extractSessionGitRefs(content: string): SessionGitRefs;
33
+ export declare function hasGitRefs(refs: SessionGitRefs): boolean;
34
+ export declare function emptyGitRefs(): SessionGitRefs;
35
+ export declare function createGitRefsAccumulator(): GitRefsAccumulator;
36
+ export interface ReadCommand {
37
+ branches: string[];
38
+ createsPullRequest: boolean;
39
+ }
40
+ /** The branches a shell command creates or pushes and whether it opens a pull request. Variables and substitutions are skipped, never guessed. */
41
+ export declare function readCommand(rawCommand: string): ReadCommand;