@bli-cockpit/telemetry-core 0.1.104 → 0.1.105

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.
@@ -267,6 +267,7 @@ export declare const CollectorHeartbeatSchema: z.ZodObject<{
267
267
  unreadable_samples: "unreadable_samples";
268
268
  cleanup_failed: "cleanup_failed";
269
269
  }>>;
270
+ failure_reasons: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
270
271
  }, z.core.$strict>>;
271
272
  }, z.core.$strict>>;
272
273
  setup_receipt: z.ZodOptional<z.ZodObject<{
@@ -51,8 +51,20 @@ export declare const MemoryHookSampleSchema: z.ZodObject<{
51
51
  person_ms: z.ZodOptional<z.ZodNumber>;
52
52
  total_ms: z.ZodOptional<z.ZodNumber>;
53
53
  }, z.core.$strict>>;
54
+ failure_reason: z.ZodOptional<z.ZodString>;
54
55
  }, z.core.$strict>;
55
56
  export type MemoryHookSample = z.infer<typeof MemoryHookSampleSchema>;
57
+ /** At most this many distinct failure reasons are kept per window; the rest are summed under the overflow label. */
58
+ export declare const MEMORY_HOOK_FAILURE_REASONS_MAX = 8;
59
+ /** A failed or timed-out sample written by a client older than the reason field (memory-mcp 0.1.99 and before). */
60
+ export declare const MEMORY_HOOK_FAILURE_REASON_NOT_RECORDED = "not_recorded";
61
+ /** Distinct reasons past `MEMORY_HOOK_FAILURE_REASONS_MAX`, summed so the counts still add up. */
62
+ export declare const MEMORY_HOOK_FAILURE_REASON_OTHER = "other_reasons";
63
+ /**
64
+ * BLI-26024: one hook reason, made safe for the sample's `failure_reason`. A reason the schema would refuse must never
65
+ * cost the whole sample, so characters outside the label set become `_` and the label is cut to 60 characters.
66
+ */
67
+ export declare function memoryHookFailureLabel(reason: string): string;
56
68
  export declare const MemoryHookSamplingLeaseSchema: z.ZodObject<{
57
69
  schema_version: z.ZodLiteral<"memory-hook-sampling-lease.v1">;
58
70
  continuous_since: z.ZodString;
@@ -117,5 +129,6 @@ export declare const MemoryHookPerformanceSchema: z.ZodObject<{
117
129
  unreadable_samples: "unreadable_samples";
118
130
  cleanup_failed: "cleanup_failed";
119
131
  }>>;
132
+ failure_reasons: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
120
133
  }, z.core.$strict>;
121
134
  export type MemoryHookPerformance = z.infer<typeof MemoryHookPerformanceSchema>;
@@ -61,8 +61,29 @@ export const MemoryHookSampleSchema = z
61
61
  producer_version: SemverSchema,
62
62
  /** Optional so samples written before BLI-23554 still parse. */
63
63
  server_timings: MemoryHookServerTimingsSchema.optional(),
64
+ /**
65
+ * BLI-26024: WHY a `failed` or `timeouts` run lost its recall, as the hook's own closed log label
66
+ * (`dashboard_unreachable`, `http_401`, `timeout`, `unknown_internal:hook_threw:TypeError`, ...). Never text from a
67
+ * response, a prompt or a memory. Optional so samples written before it still parse; the reader counts a
68
+ * failed sample without one as `not_recorded`.
69
+ */
70
+ failure_reason: z.string().min(1).max(60).regex(/^[a-z0-9_:.-]+$/iu).optional(),
64
71
  })
65
72
  .strict();
73
+ /** At most this many distinct failure reasons are kept per window; the rest are summed under the overflow label. */
74
+ export const MEMORY_HOOK_FAILURE_REASONS_MAX = 8;
75
+ /** A failed or timed-out sample written by a client older than the reason field (memory-mcp 0.1.99 and before). */
76
+ export const MEMORY_HOOK_FAILURE_REASON_NOT_RECORDED = "not_recorded";
77
+ /** Distinct reasons past `MEMORY_HOOK_FAILURE_REASONS_MAX`, summed so the counts still add up. */
78
+ export const MEMORY_HOOK_FAILURE_REASON_OTHER = "other_reasons";
79
+ /**
80
+ * BLI-26024: one hook reason, made safe for the sample's `failure_reason`. A reason the schema would refuse must never
81
+ * cost the whole sample, so characters outside the label set become `_` and the label is cut to 60 characters.
82
+ */
83
+ export function memoryHookFailureLabel(reason) {
84
+ const cleaned = reason.trim().replace(/[^a-z0-9_:.-]/giu, "_").slice(0, 60);
85
+ return cleaned.length > 0 ? cleaned : "unnamed";
86
+ }
66
87
  export const MemoryHookSamplingLeaseSchema = z
67
88
  .object({
68
89
  schema_version: z.literal(MEMORY_HOOK_SAMPLING_LEASE_SCHEMA_VERSION),
@@ -188,9 +209,32 @@ export const MemoryHookPerformanceSchema = z
188
209
  unreadable_samples: CountSchema,
189
210
  capped: z.boolean(),
190
211
  reasons: z.array(PerformanceReasonSchema).max(MEMORY_HOOK_PERFORMANCE_REASONS.length),
212
+ /**
213
+ * BLI-26024: the failed and timed-out samples split by their reason label. Present only when the window has at
214
+ * least one; sums to `outcomes.failed + outcomes.timeouts`. Absent on a collector older than the field, which reads
215
+ * as "reasons not reported", never as "nothing failed".
216
+ */
217
+ failure_reasons: z
218
+ .record(z.string().min(1).max(60).regex(/^[a-z0-9_:.-]+$/iu), CountSchema)
219
+ .optional(),
191
220
  })
192
221
  .strict()
193
222
  .superRefine((value, context) => {
223
+ if (value.failure_reasons) {
224
+ const keys = Object.keys(value.failure_reasons);
225
+ // The kept reasons, plus `not_recorded` and the overflow label.
226
+ if (keys.length > MEMORY_HOOK_FAILURE_REASONS_MAX + 2) {
227
+ context.addIssue({ code: "custom", path: ["failure_reasons"], message: "too many failure reasons" });
228
+ }
229
+ const reasonTotal = Object.values(value.failure_reasons).reduce((sum, count) => sum + count, 0);
230
+ if (reasonTotal !== value.outcomes.failed + value.outcomes.timeouts) {
231
+ context.addIssue({
232
+ code: "custom",
233
+ path: ["failure_reasons"],
234
+ message: "failure reason counts must sum to failed plus timeouts",
235
+ });
236
+ }
237
+ }
194
238
  const outcomeTotal = Object.values(value.outcomes).reduce((sum, count) => sum + count, 0);
195
239
  if (outcomeTotal !== value.samples) {
196
240
  context.addIssue({
@@ -172,6 +172,7 @@ export declare const MemoryInstallReceiptSchema: z.ZodObject<{
172
172
  unreadable_samples: "unreadable_samples";
173
173
  cleanup_failed: "cleanup_failed";
174
174
  }>>;
175
+ failure_reasons: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
175
176
  }, z.core.$strict>>;
176
177
  }, z.core.$strict>;
177
178
  export type MemoryInstallReceipt = z.infer<typeof MemoryInstallReceiptSchema>;
@@ -63,6 +63,19 @@ export declare function readReadCacheEntry(entryPath: string): ReadCacheEntry |
63
63
  export declare function writeReadCacheEntryAtomic(entryPath: string, entry: Omit<ReadCacheEntry, "storedAt"> & {
64
64
  storedAt?: number;
65
65
  }): void;
66
+ /**
67
+ * Where a surface's invalidation marker lives: NEXT TO the surface directory,
68
+ * never inside it, because invalidating removes the directory (BLI-25636).
69
+ */
70
+ export declare function readCacheInvalidationMarkerPath(stateDir: string, deviceToken: string, surface: ReadCacheSurface): string;
71
+ /**
72
+ * The surface's current invalidation stamp, or `""` when it was never
73
+ * invalidated (or the marker is unreadable). A reader takes the stamp before
74
+ * its request leaves and compares it after the answer returns: a different
75
+ * stamp means a write emptied the surface in between, so the answer is older
76
+ * than the write and must not be cached.
77
+ */
78
+ export declare function readReadCacheInvalidationStamp(stateDir: string, deviceToken: string, surface: ReadCacheSurface): string;
66
79
  /**
67
80
  * Throws away every cached read for one surface — the whole answer to "any
68
81
  * write through the CLI or MCP invalidates the affected cache" (BLI-10746).
@@ -71,6 +84,13 @@ export declare function writeReadCacheEntryAtomic(entryPath: string, entry: Omit
71
84
  * cached queries a given write affects is its own project, and "the next
72
85
  * read of this surface is live" costs one extra request, never a wrong
73
86
  * answer.
87
+ *
88
+ * It also leaves a marker with a new unique stamp (BLI-25636), because
89
+ * deleting alone cannot stop a read that left BEFORE the write and returns
90
+ * AFTER it from writing its pre-write answer into the emptied surface. The
91
+ * stamp is unique per call (time, pid, random), so two invalidations in the
92
+ * same millisecond still differ. The directory is removed even when the
93
+ * marker could not be written: an emptied surface is the part that matters.
74
94
  */
75
95
  export declare function invalidateReadCacheSurface(stateDir: string, deviceToken: string, surface: ReadCacheSurface): {
76
96
  ok: true;
@@ -114,6 +114,28 @@ export function writeReadCacheEntryAtomic(entryPath, entry) {
114
114
  }
115
115
  }
116
116
  }
117
+ /**
118
+ * Where a surface's invalidation marker lives: NEXT TO the surface directory,
119
+ * never inside it, because invalidating removes the directory (BLI-25636).
120
+ */
121
+ export function readCacheInvalidationMarkerPath(stateDir, deviceToken, surface) {
122
+ return path.join(stateDir, "read-cache", operatorScope(deviceToken), `${surface}.invalidated_at`);
123
+ }
124
+ /**
125
+ * The surface's current invalidation stamp, or `""` when it was never
126
+ * invalidated (or the marker is unreadable). A reader takes the stamp before
127
+ * its request leaves and compares it after the answer returns: a different
128
+ * stamp means a write emptied the surface in between, so the answer is older
129
+ * than the write and must not be cached.
130
+ */
131
+ export function readReadCacheInvalidationStamp(stateDir, deviceToken, surface) {
132
+ try {
133
+ return fs.readFileSync(readCacheInvalidationMarkerPath(stateDir, deviceToken, surface), "utf8");
134
+ }
135
+ catch {
136
+ return "";
137
+ }
138
+ }
117
139
  /**
118
140
  * Throws away every cached read for one surface — the whole answer to "any
119
141
  * write through the CLI or MCP invalidates the affected cache" (BLI-10746).
@@ -122,14 +144,31 @@ export function writeReadCacheEntryAtomic(entryPath, entry) {
122
144
  * cached queries a given write affects is its own project, and "the next
123
145
  * read of this surface is live" costs one extra request, never a wrong
124
146
  * answer.
147
+ *
148
+ * It also leaves a marker with a new unique stamp (BLI-25636), because
149
+ * deleting alone cannot stop a read that left BEFORE the write and returns
150
+ * AFTER it from writing its pre-write answer into the emptied surface. The
151
+ * stamp is unique per call (time, pid, random), so two invalidations in the
152
+ * same millisecond still differ. The directory is removed even when the
153
+ * marker could not be written: an emptied surface is the part that matters.
125
154
  */
126
155
  export function invalidateReadCacheSurface(stateDir, deviceToken, surface) {
127
156
  const dir = readCacheSurfaceDir(stateDir, deviceToken, surface);
157
+ let failure = null;
158
+ try {
159
+ const markerPath = readCacheInvalidationMarkerPath(stateDir, deviceToken, surface);
160
+ fs.mkdirSync(path.dirname(markerPath), { recursive: true, mode: 0o700 });
161
+ const stamp = `${Date.now()}-${process.pid}-${crypto.randomBytes(6).toString("hex")}`;
162
+ fs.writeFileSync(markerPath, stamp, { mode: 0o600 });
163
+ }
164
+ catch (error) {
165
+ failure = `marker_${error instanceof Error ? error.name : "unknown"}`;
166
+ }
128
167
  try {
129
168
  const existed = fs.existsSync(dir);
130
169
  if (existed)
131
170
  fs.rmSync(dir, { recursive: true, force: true });
132
- return { ok: true, removed: existed };
171
+ return failure ? { ok: false, reason: failure } : { ok: true, removed: existed };
133
172
  }
134
173
  catch (error) {
135
174
  return { ok: false, reason: error instanceof Error ? error.name : "unknown" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bli-cockpit/telemetry-core",
3
- "version": "0.1.104",
3
+ "version": "0.1.105",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",