@mastra/code-sdk 1.3.0 → 1.4.0-alpha.1

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.
@@ -0,0 +1,500 @@
1
+ import { randomUUID } from "crypto";
2
+ import { homedir } from "os";
3
+ import { join } from "path";
4
+ import { appendFile, chmod, mkdir, rename, rm, writeFile } from "fs/promises";
5
+ import { Session } from "inspector/promises";
6
+ import { PerformanceObserver } from "perf_hooks";
7
+ import { arch, platform } from "process";
8
+ import { getHeapSpaceStatistics, getHeapStatistics } from "v8";
9
+ //#region src/process-memory-diagnostics.ts
10
+ const PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS = {
11
+ sampleIntervalMs: 1e4,
12
+ captureIntervalMs: 3e5,
13
+ allocationIntervalBytes: 524288
14
+ };
15
+ const PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS = {
16
+ sampleIntervalMs: 1e3,
17
+ captureIntervalMs: 1e4,
18
+ allocationIntervalBytes: 32768
19
+ };
20
+ const MAX_TIMER_DELAY_MS = 2147483647;
21
+ const MAX_PENDING_GC_EVENTS = 1e3;
22
+ var ProcessMemoryDiagnosticsConfigError = class extends Error {
23
+ constructor(message) {
24
+ super(message);
25
+ this.name = "ProcessMemoryDiagnosticsConfigError";
26
+ }
27
+ };
28
+ function isEnabled(value) {
29
+ return [
30
+ "1",
31
+ "true",
32
+ "yes",
33
+ "on"
34
+ ].includes(value?.trim().toLowerCase() ?? "");
35
+ }
36
+ function parseBoundedInteger(env, name, fallback, minimum, maximum) {
37
+ const raw = env[name];
38
+ if (raw === void 0 || raw.trim() === "") return fallback;
39
+ const value = Number(raw);
40
+ if (!Number.isSafeInteger(value) || value < minimum || maximum !== void 0 && value > maximum) throw new ProcessMemoryDiagnosticsConfigError(`${name} must be an integer ${maximum === void 0 ? `greater than or equal to ${minimum}` : `between ${minimum} and ${maximum}`}; received ${JSON.stringify(raw)}.`);
41
+ return value;
42
+ }
43
+ function getDefaultProfileParentDirectory() {
44
+ if (process.env.MASTRA_APP_DATA_DIR) return join(process.env.MASTRA_APP_DATA_DIR, "profiles");
45
+ return join(platform === "darwin" ? join(homedir(), "Library", "Application Support") : platform === "win32" ? process.env.APPDATA || join(homedir(), "AppData", "Roaming") : process.env.XDG_DATA_HOME || join(homedir(), ".local", "share"), "mastracode", "profiles");
46
+ }
47
+ function parseProcessMemoryDiagnosticsEnvironment(env = process.env) {
48
+ return {
49
+ enabled: isEnabled(env.MASTRACODE_PROFILE),
50
+ config: {
51
+ parentDirectory: env.MASTRACODE_PROFILE_DIR?.trim() || getDefaultProfileParentDirectory(),
52
+ sampleIntervalMs: parseBoundedInteger(env, "MASTRACODE_PROFILE_SAMPLE_INTERVAL_MS", PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.sampleIntervalMs, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.sampleIntervalMs, MAX_TIMER_DELAY_MS),
53
+ captureIntervalMs: parseBoundedInteger(env, "MASTRACODE_PROFILE_CAPTURE_INTERVAL_MS", PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.captureIntervalMs, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.captureIntervalMs, MAX_TIMER_DELAY_MS),
54
+ allocationIntervalBytes: parseBoundedInteger(env, "MASTRACODE_PROFILE_ALLOCATION_INTERVAL_BYTES", PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.allocationIntervalBytes, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.allocationIntervalBytes)
55
+ }
56
+ };
57
+ }
58
+ function createProcessMemoryDiagnosticsFromEnvironment(env = process.env, dependencies = {}) {
59
+ try {
60
+ const { enabled, config } = parseProcessMemoryDiagnosticsEnvironment(env);
61
+ return {
62
+ diagnostics: new ProcessMemoryDiagnostics(config, dependencies),
63
+ enabled,
64
+ error: null
65
+ };
66
+ } catch (error) {
67
+ const config = {
68
+ parentDirectory: env.MASTRACODE_PROFILE_DIR?.trim() || getDefaultProfileParentDirectory(),
69
+ ...PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS
70
+ };
71
+ const message = error instanceof Error ? error.message : String(error);
72
+ return {
73
+ diagnostics: new ProcessMemoryDiagnostics(config, dependencies, message),
74
+ enabled: isEnabled(env.MASTRACODE_PROFILE),
75
+ error: message
76
+ };
77
+ }
78
+ }
79
+ async function startConfiguredProcessMemoryDiagnostics(setup, warn) {
80
+ if (!setup.enabled) return setup.diagnostics;
81
+ if (setup.error) {
82
+ warn(`Process memory diagnostics were not started: ${setup.error}`);
83
+ return setup.diagnostics;
84
+ }
85
+ const status = await setup.diagnostics.start();
86
+ if (status.state !== "active") warn(`Process memory diagnostics were not started: ${status.error ?? "unknown inspector error"}`);
87
+ return setup.diagnostics;
88
+ }
89
+ async function stopProcessMemoryDiagnosticsWithTimeout(diagnostics, warn, timeoutMs = 5e3) {
90
+ let timeout;
91
+ const timedOut = await Promise.race([diagnostics.stop().then(() => false, (error) => {
92
+ warn(`Process memory diagnostics did not stop cleanly: ${errorMessage(error)}`);
93
+ return false;
94
+ }), new Promise((resolve) => {
95
+ timeout = setTimeout(() => resolve(true), timeoutMs);
96
+ timeout.unref();
97
+ })]);
98
+ if (timeout) clearTimeout(timeout);
99
+ if (timedOut) warn(`Process memory diagnostics did not stop within ${timeoutMs}ms; final artifacts may be incomplete.`);
100
+ }
101
+ function defaultInspectorSession() {
102
+ const session = new Session();
103
+ return {
104
+ connect: () => session.connect(),
105
+ disconnect: () => session.disconnect(),
106
+ post: async (method, params) => await session.post(method, params)
107
+ };
108
+ }
109
+ function defaultPerformanceObserver(callback) {
110
+ return new PerformanceObserver((list) => callback(list.getEntries()));
111
+ }
112
+ function errorMessage(error) {
113
+ return error instanceof Error ? error.message : String(error);
114
+ }
115
+ function safeTimestamp(date) {
116
+ return date.toISOString().replaceAll(":", "-");
117
+ }
118
+ function unrefTimer(timer) {
119
+ timer.unref?.();
120
+ }
121
+ var ProcessMemoryDiagnostics = class {
122
+ config;
123
+ state = "inactive";
124
+ outputDirectory = null;
125
+ inspector = null;
126
+ observer = null;
127
+ sampleTimer = null;
128
+ captureTimer = null;
129
+ startedAt = null;
130
+ sampleCount = 0;
131
+ captureCount = 0;
132
+ gcEventCount = 0;
133
+ latestSample = null;
134
+ latestCapturePath = null;
135
+ latestError;
136
+ samplingActive = false;
137
+ stopRequested = false;
138
+ startingPromise = null;
139
+ stoppingPromise = null;
140
+ restartAfterStopPromise = null;
141
+ restartRequested = false;
142
+ pendingGcEvents = [];
143
+ gcEventBufferOverflowed = false;
144
+ artifactWriteFailed = false;
145
+ captureQueue = Promise.resolve();
146
+ writeQueue = Promise.resolve();
147
+ configError;
148
+ createInspectorSession;
149
+ createPerformanceObserver;
150
+ now;
151
+ randomId;
152
+ constructor(config, dependencies = {}, initialError = null) {
153
+ this.config = { ...config };
154
+ this.configError = initialError;
155
+ this.latestError = initialError;
156
+ this.createInspectorSession = dependencies.createInspectorSession ?? defaultInspectorSession;
157
+ this.createPerformanceObserver = dependencies.createPerformanceObserver ?? defaultPerformanceObserver;
158
+ this.now = dependencies.now ?? (() => /* @__PURE__ */ new Date());
159
+ this.randomId = dependencies.randomId ?? randomUUID;
160
+ }
161
+ getStatus() {
162
+ return {
163
+ state: this.state,
164
+ outputDirectory: this.outputDirectory,
165
+ config: { ...this.config },
166
+ sampleCount: this.sampleCount,
167
+ captureCount: this.captureCount,
168
+ gcEventCount: this.gcEventCount,
169
+ latestSample: this.latestSample,
170
+ latestCapturePath: this.latestCapturePath,
171
+ error: this.latestError
172
+ };
173
+ }
174
+ start() {
175
+ if (this.state === "active") return Promise.resolve(this.getStatus());
176
+ if (this.state === "starting" && this.startingPromise) return this.startingPromise;
177
+ if (this.state === "stopping" && this.stoppingPromise) {
178
+ this.restartRequested = true;
179
+ this.restartAfterStopPromise ??= this.stoppingPromise.then(() => {
180
+ this.restartAfterStopPromise = null;
181
+ if (!this.restartRequested) return this.getStatus();
182
+ this.restartRequested = false;
183
+ return this.start();
184
+ });
185
+ return this.restartAfterStopPromise;
186
+ }
187
+ if (this.configError) {
188
+ this.latestError = this.configError;
189
+ this.state = "error";
190
+ return Promise.resolve(this.getStatus());
191
+ }
192
+ this.state = "starting";
193
+ this.stoppingPromise = null;
194
+ this.restartRequested = false;
195
+ this.stopRequested = false;
196
+ this.outputDirectory = null;
197
+ this.sampleCount = 0;
198
+ this.captureCount = 0;
199
+ this.gcEventCount = 0;
200
+ this.pendingGcEvents = [];
201
+ this.gcEventBufferOverflowed = false;
202
+ this.artifactWriteFailed = false;
203
+ this.latestSample = null;
204
+ this.latestCapturePath = null;
205
+ this.latestError = null;
206
+ this.startedAt = this.now();
207
+ const startingPromise = this.startRun();
208
+ this.startingPromise = startingPromise;
209
+ startingPromise.finally(() => {
210
+ if (this.startingPromise === startingPromise) this.startingPromise = null;
211
+ });
212
+ return startingPromise;
213
+ }
214
+ async startRun() {
215
+ try {
216
+ await this.createArtifacts();
217
+ if (await this.abortStartIfRequested()) return this.getStatus();
218
+ this.observeGc();
219
+ this.inspector = this.createInspectorSession();
220
+ this.inspector.connect();
221
+ await this.startSampling();
222
+ if (await this.abortStartIfRequested()) return this.getStatus();
223
+ await this.takeSample();
224
+ if (await this.abortStartIfRequested()) return this.getStatus();
225
+ this.sampleTimer = setInterval(() => {
226
+ this.takeSample().catch((error) => this.recordError("Process sample failed", error));
227
+ }, this.config.sampleIntervalMs);
228
+ unrefTimer(this.sampleTimer);
229
+ this.captureTimer = setInterval(() => {
230
+ this.capture("periodic").catch((error) => this.recordError("Periodic allocation capture failed", error));
231
+ }, this.config.captureIntervalMs);
232
+ unrefTimer(this.captureTimer);
233
+ this.state = "active";
234
+ return this.getStatus();
235
+ } catch (error) {
236
+ this.latestError = `Unable to start process memory diagnostics: ${errorMessage(error)}`;
237
+ await this.cleanupAfterStartFailure();
238
+ this.state = "error";
239
+ return this.getStatus();
240
+ }
241
+ }
242
+ async abortStartIfRequested() {
243
+ if (!this.stopRequested) return false;
244
+ await this.cleanupAfterStartFailure();
245
+ this.state = "inactive";
246
+ return true;
247
+ }
248
+ capture(reason = "manual") {
249
+ if (this.state !== "active") return Promise.reject(new Error(this.latestError ?? "Process memory diagnostics are not active."));
250
+ return this.enqueueCapture(() => this.captureEpoch(reason, false));
251
+ }
252
+ async stop() {
253
+ if (this.stoppingPromise) {
254
+ this.restartRequested = false;
255
+ return this.stoppingPromise;
256
+ }
257
+ if (this.state === "inactive") return this.getStatus();
258
+ if (this.state === "error" && !this.inspector && !this.outputDirectory) return this.getStatus();
259
+ const startingPromise = this.state === "starting" ? this.startingPromise : null;
260
+ this.stopRequested = true;
261
+ this.state = "stopping";
262
+ this.clearTimersAndObserver();
263
+ if (startingPromise) {
264
+ this.stoppingPromise = startingPromise.then(() => this.getStatus());
265
+ return this.stoppingPromise;
266
+ }
267
+ this.stoppingPromise = (async () => {
268
+ let stopError = null;
269
+ try {
270
+ if (this.outputDirectory) await this.takeSample();
271
+ await this.enqueueCapture(async () => {
272
+ if (this.inspector && this.samplingActive) await this.captureEpoch("stop", true);
273
+ });
274
+ await this.writeQueue;
275
+ if (this.gcEventBufferOverflowed) throw new Error(`GC event buffer exceeded ${MAX_PENDING_GC_EVENTS} records before the next process sample.`);
276
+ if (this.artifactWriteFailed) throw new Error("One or more process memory diagnostic artifact writes failed.");
277
+ this.latestError = null;
278
+ } catch (error) {
279
+ stopError = `Unable to stop process memory diagnostics cleanly: ${errorMessage(error)}`;
280
+ this.latestError = stopError;
281
+ } finally {
282
+ this.disconnectInspector();
283
+ this.state = stopError ? "error" : "inactive";
284
+ }
285
+ return this.getStatus();
286
+ })();
287
+ return this.stoppingPromise;
288
+ }
289
+ async createArtifacts() {
290
+ await mkdir(this.config.parentDirectory, {
291
+ recursive: true,
292
+ mode: 448
293
+ });
294
+ await chmod(this.config.parentDirectory, 448);
295
+ const runName = `run-${safeTimestamp(this.startedAt)}-${process.pid}-${this.randomId().replaceAll(/[^a-zA-Z0-9-]/g, "").slice(0, 12)}`;
296
+ this.outputDirectory = join(this.config.parentDirectory, runName);
297
+ await mkdir(this.outputDirectory, { mode: 448 });
298
+ await chmod(this.outputDirectory, 448);
299
+ const metadata = {
300
+ schemaVersion: 1,
301
+ runId: runName,
302
+ startedAt: this.startedAt.toISOString(),
303
+ pid: process.pid,
304
+ nodeVersion: process.version,
305
+ platform,
306
+ arch,
307
+ config: this.config
308
+ };
309
+ await writeFile(join(this.outputDirectory, "metadata.json"), `${JSON.stringify(metadata, null, 2)}\n`, {
310
+ flag: "wx",
311
+ mode: 384
312
+ });
313
+ await writeFile(join(this.outputDirectory, "process-samples.jsonl"), "", {
314
+ flag: "wx",
315
+ mode: 384
316
+ });
317
+ await writeFile(join(this.outputDirectory, "gc-events.jsonl"), "", {
318
+ flag: "wx",
319
+ mode: 384
320
+ });
321
+ }
322
+ observeGc() {
323
+ const observer = this.createPerformanceObserver((entries) => {
324
+ const before = this.latestSample?.memory ?? null;
325
+ const after = process.memoryUsage();
326
+ const timestamp = this.now().toISOString();
327
+ for (const entry of entries) {
328
+ const gcEntry = entry;
329
+ this.gcEventCount += 1;
330
+ if (this.pendingGcEvents.length >= MAX_PENDING_GC_EVENTS) {
331
+ this.gcEventBufferOverflowed = true;
332
+ this.latestError = `GC event buffer exceeded ${MAX_PENDING_GC_EVENTS} records before the next process sample.`;
333
+ continue;
334
+ }
335
+ this.pendingGcEvents.push({
336
+ timestamp,
337
+ sequence: this.gcEventCount,
338
+ name: entry.name,
339
+ startTime: entry.startTime,
340
+ duration: entry.duration,
341
+ kind: gcEntry.detail?.kind ?? gcEntry.kind ?? null,
342
+ flags: gcEntry.detail?.flags ?? gcEntry.flags ?? null,
343
+ before,
344
+ after,
345
+ latestSampleSequence: this.latestSample?.sequence ?? null
346
+ });
347
+ }
348
+ });
349
+ observer.observe({ entryTypes: ["gc"] });
350
+ this.observer = observer;
351
+ }
352
+ async startSampling() {
353
+ if (!this.inspector) throw new Error("Inspector session is unavailable.");
354
+ await this.inspector.post("HeapProfiler.startSampling", {
355
+ samplingInterval: this.config.allocationIntervalBytes,
356
+ includeObjectsCollectedByMajorGC: true,
357
+ includeObjectsCollectedByMinorGC: true
358
+ });
359
+ this.samplingActive = true;
360
+ }
361
+ async takeSample() {
362
+ if (!this.outputDirectory || !this.startedAt) throw new Error("Diagnostics output directory is unavailable.");
363
+ const sample = {
364
+ timestamp: this.now().toISOString(),
365
+ sequence: this.sampleCount + 1,
366
+ elapsedMs: Math.max(0, this.now().getTime() - this.startedAt.getTime()),
367
+ memory: process.memoryUsage(),
368
+ resourceUsage: process.resourceUsage(),
369
+ heap: getHeapStatistics(),
370
+ heapSpaces: getHeapSpaceStatistics()
371
+ };
372
+ this.sampleCount = sample.sequence;
373
+ this.latestSample = sample;
374
+ await this.enqueueWrite("process-samples.jsonl", sample);
375
+ const gcEvents = this.pendingGcEvents.splice(0);
376
+ if (gcEvents.length > 0) await this.enqueueWriteBatch("gc-events.jsonl", gcEvents);
377
+ return sample;
378
+ }
379
+ enqueueWrite(fileName, value) {
380
+ return this.enqueueWriteBatch(fileName, [value]);
381
+ }
382
+ enqueueWriteBatch(fileName, values) {
383
+ const operation = this.writeQueue.then(async () => {
384
+ if (!this.outputDirectory || values.length === 0) return;
385
+ const lines = values.map((value) => JSON.stringify(value)).join("\n");
386
+ try {
387
+ await appendFile(join(this.outputDirectory, fileName), `${lines}\n`, { mode: 384 });
388
+ } catch (error) {
389
+ this.artifactWriteFailed = true;
390
+ throw error;
391
+ }
392
+ });
393
+ this.writeQueue = operation.catch(() => void 0);
394
+ return operation;
395
+ }
396
+ enqueueCapture(operation) {
397
+ const result = this.captureQueue.then(operation);
398
+ this.captureQueue = result.catch(() => void 0);
399
+ return result;
400
+ }
401
+ async captureEpoch(reason, final) {
402
+ if (!this.inspector || !this.samplingActive || !this.outputDirectory) throw new Error(this.latestError ?? "Allocation sampling is not active.");
403
+ let response;
404
+ try {
405
+ response = await this.inspector.post("HeapProfiler.stopSampling");
406
+ this.samplingActive = false;
407
+ } catch (error) {
408
+ this.latestError = `Allocation capture failed: ${errorMessage(error)}`;
409
+ if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();
410
+ throw new Error(this.latestError, { cause: error });
411
+ }
412
+ const profile = response.profile;
413
+ if (!profile || typeof profile !== "object") {
414
+ this.latestError = "Allocation capture failed: inspector returned no profile.";
415
+ if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();
416
+ throw new Error(this.latestError);
417
+ }
418
+ const sequence = this.captureCount + 1;
419
+ const timestamp = this.now().toISOString();
420
+ const filename = `allocation-${String(sequence).padStart(6, "0")}-${safeTimestamp(new Date(timestamp))}.heapprofile`;
421
+ const finalPath = join(this.outputDirectory, filename);
422
+ const temporaryPath = join(this.outputDirectory, `.${filename}.${this.randomId()}.tmp`);
423
+ try {
424
+ await writeFile(temporaryPath, `${JSON.stringify(profile)}\n`, {
425
+ flag: "wx",
426
+ mode: 384
427
+ });
428
+ await chmod(temporaryPath, 384);
429
+ await rename(temporaryPath, finalPath);
430
+ await chmod(finalPath, 384);
431
+ this.captureCount = sequence;
432
+ this.latestCapturePath = finalPath;
433
+ this.latestError = null;
434
+ } catch (error) {
435
+ this.artifactWriteFailed = true;
436
+ this.latestError = `Unable to persist allocation capture: ${errorMessage(error)}`;
437
+ throw new Error(this.latestError, { cause: error });
438
+ } finally {
439
+ if (!final && !this.stopRequested && this.state === "active") try {
440
+ await this.startSampling();
441
+ } catch (error) {
442
+ this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`;
443
+ this.state = "error";
444
+ }
445
+ }
446
+ if (!final && !this.stopRequested && !this.samplingActive) throw new Error(this.latestError ?? "Allocation sampling did not restart.");
447
+ return {
448
+ path: finalPath,
449
+ sequence,
450
+ timestamp,
451
+ reason
452
+ };
453
+ }
454
+ async restartSamplingAfterFailure() {
455
+ try {
456
+ await this.startSampling();
457
+ } catch (error) {
458
+ this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`;
459
+ this.state = "error";
460
+ }
461
+ }
462
+ recordError(prefix, error) {
463
+ this.latestError = `${prefix}: ${errorMessage(error)}`;
464
+ }
465
+ clearTimersAndObserver() {
466
+ if (this.sampleTimer) clearInterval(this.sampleTimer);
467
+ if (this.captureTimer) clearInterval(this.captureTimer);
468
+ this.sampleTimer = null;
469
+ this.captureTimer = null;
470
+ this.observer?.disconnect();
471
+ this.observer = null;
472
+ }
473
+ disconnectInspector() {
474
+ this.samplingActive = false;
475
+ if (!this.inspector) return;
476
+ try {
477
+ this.inspector.disconnect();
478
+ } catch {}
479
+ this.inspector = null;
480
+ }
481
+ async cleanupAfterStartFailure() {
482
+ this.clearTimersAndObserver();
483
+ this.disconnectInspector();
484
+ await this.writeQueue;
485
+ try {
486
+ if (this.outputDirectory) await rm(this.outputDirectory, {
487
+ recursive: true,
488
+ force: true
489
+ });
490
+ } catch (error) {
491
+ this.latestError = `${this.latestError} Failed to remove partial artifacts: ${errorMessage(error)}`;
492
+ } finally {
493
+ this.outputDirectory = null;
494
+ }
495
+ }
496
+ };
497
+ //#endregion
498
+ export { PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS, ProcessMemoryDiagnostics, ProcessMemoryDiagnosticsConfigError, createProcessMemoryDiagnosticsFromEnvironment, parseProcessMemoryDiagnosticsEnvironment, startConfiguredProcessMemoryDiagnostics, stopProcessMemoryDiagnosticsWithTimeout };
499
+
500
+ //# sourceMappingURL=process-memory-diagnostics.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-memory-diagnostics.js","names":[],"sources":["../src/process-memory-diagnostics.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\nimport { appendFile, chmod, mkdir, rename, rm, writeFile } from 'node:fs/promises';\nimport { Session } from 'node:inspector/promises';\nimport { homedir } from 'node:os';\nimport { join } from 'node:path';\nimport { PerformanceObserver, type PerformanceEntry } from 'node:perf_hooks';\nimport { arch, platform } from 'node:process';\nimport { getHeapSpaceStatistics, getHeapStatistics } from 'node:v8';\n\nexport const PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS = {\n sampleIntervalMs: 10_000,\n captureIntervalMs: 300_000,\n allocationIntervalBytes: 524_288,\n} as const;\n\nexport const PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS = {\n sampleIntervalMs: 1_000,\n captureIntervalMs: 10_000,\n allocationIntervalBytes: 32_768,\n} as const;\n\nconst MAX_TIMER_DELAY_MS = 2_147_483_647;\nconst MAX_PENDING_GC_EVENTS = 1_000;\n\nexport interface ProcessMemoryDiagnosticsConfig {\n parentDirectory: string;\n sampleIntervalMs: number;\n captureIntervalMs: number;\n allocationIntervalBytes: number;\n}\n\nexport interface ProcessMemoryDiagnosticsEnvironment {\n MASTRACODE_PROFILE?: string;\n MASTRACODE_PROFILE_DIR?: string;\n MASTRACODE_PROFILE_SAMPLE_INTERVAL_MS?: string;\n MASTRACODE_PROFILE_CAPTURE_INTERVAL_MS?: string;\n MASTRACODE_PROFILE_ALLOCATION_INTERVAL_BYTES?: string;\n}\n\nexport interface ProcessMemoryDiagnosticsMemorySample {\n timestamp: string;\n sequence: number;\n elapsedMs: number;\n memory: ReturnType<typeof process.memoryUsage>;\n resourceUsage: ReturnType<typeof process.resourceUsage>;\n heap: ReturnType<typeof getHeapStatistics>;\n heapSpaces: ReturnType<typeof getHeapSpaceStatistics>;\n}\n\nexport interface ProcessMemoryDiagnosticsStatus {\n state: 'inactive' | 'starting' | 'active' | 'stopping' | 'error';\n outputDirectory: string | null;\n config: ProcessMemoryDiagnosticsConfig;\n sampleCount: number;\n captureCount: number;\n gcEventCount: number;\n latestSample: ProcessMemoryDiagnosticsMemorySample | null;\n latestCapturePath: string | null;\n error: string | null;\n}\n\nexport interface ProcessMemoryDiagnosticsCapture {\n path: string;\n sequence: number;\n timestamp: string;\n reason: 'manual' | 'periodic' | 'stop';\n}\n\ninterface InspectorSessionAdapter {\n connect(): void;\n disconnect(): void;\n post(method: string, params?: Record<string, unknown>): Promise<Record<string, unknown>>;\n}\n\ninterface PerformanceObserverAdapter {\n observe(options: Parameters<PerformanceObserver['observe']>[0]): void;\n disconnect(): void;\n}\n\ninterface ProcessMemoryDiagnosticsDependencies {\n createInspectorSession?: () => InspectorSessionAdapter;\n createPerformanceObserver?: (callback: (entries: PerformanceEntry[]) => void) => PerformanceObserverAdapter;\n now?: () => Date;\n randomId?: () => string;\n}\n\nexport interface ProcessMemoryDiagnosticsSetup {\n diagnostics: ProcessMemoryDiagnostics;\n enabled: boolean;\n error: string | null;\n}\n\nexport class ProcessMemoryDiagnosticsConfigError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'ProcessMemoryDiagnosticsConfigError';\n }\n}\n\nfunction isEnabled(value: string | undefined): boolean {\n return ['1', 'true', 'yes', 'on'].includes(value?.trim().toLowerCase() ?? '');\n}\n\nfunction parseBoundedInteger(\n env: ProcessMemoryDiagnosticsEnvironment,\n name: keyof ProcessMemoryDiagnosticsEnvironment,\n fallback: number,\n minimum: number,\n maximum?: number,\n): number {\n const raw = env[name];\n if (raw === undefined || raw.trim() === '') return fallback;\n const value = Number(raw);\n if (!Number.isSafeInteger(value) || value < minimum || (maximum !== undefined && value > maximum)) {\n const range = maximum === undefined ? `greater than or equal to ${minimum}` : `between ${minimum} and ${maximum}`;\n throw new ProcessMemoryDiagnosticsConfigError(\n `${name} must be an integer ${range}; received ${JSON.stringify(raw)}.`,\n );\n }\n return value;\n}\n\nfunction getDefaultProfileParentDirectory(): string {\n if (process.env.MASTRA_APP_DATA_DIR) return join(process.env.MASTRA_APP_DATA_DIR, 'profiles');\n\n const baseDirectory =\n platform === 'darwin'\n ? join(homedir(), 'Library', 'Application Support')\n : platform === 'win32'\n ? process.env.APPDATA || join(homedir(), 'AppData', 'Roaming')\n : process.env.XDG_DATA_HOME || join(homedir(), '.local', 'share');\n return join(baseDirectory, 'mastracode', 'profiles');\n}\n\nexport function parseProcessMemoryDiagnosticsEnvironment(env: ProcessMemoryDiagnosticsEnvironment = process.env): {\n enabled: boolean;\n config: ProcessMemoryDiagnosticsConfig;\n} {\n return {\n enabled: isEnabled(env.MASTRACODE_PROFILE),\n config: {\n parentDirectory: env.MASTRACODE_PROFILE_DIR?.trim() || getDefaultProfileParentDirectory(),\n sampleIntervalMs: parseBoundedInteger(\n env,\n 'MASTRACODE_PROFILE_SAMPLE_INTERVAL_MS',\n PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.sampleIntervalMs,\n PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.sampleIntervalMs,\n MAX_TIMER_DELAY_MS,\n ),\n captureIntervalMs: parseBoundedInteger(\n env,\n 'MASTRACODE_PROFILE_CAPTURE_INTERVAL_MS',\n PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.captureIntervalMs,\n PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.captureIntervalMs,\n MAX_TIMER_DELAY_MS,\n ),\n allocationIntervalBytes: parseBoundedInteger(\n env,\n 'MASTRACODE_PROFILE_ALLOCATION_INTERVAL_BYTES',\n PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.allocationIntervalBytes,\n PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.allocationIntervalBytes,\n ),\n },\n };\n}\n\nexport function createProcessMemoryDiagnosticsFromEnvironment(\n env: ProcessMemoryDiagnosticsEnvironment = process.env,\n dependencies: ProcessMemoryDiagnosticsDependencies = {},\n): ProcessMemoryDiagnosticsSetup {\n try {\n const { enabled, config } = parseProcessMemoryDiagnosticsEnvironment(env);\n return { diagnostics: new ProcessMemoryDiagnostics(config, dependencies), enabled, error: null };\n } catch (error) {\n const config: ProcessMemoryDiagnosticsConfig = {\n parentDirectory: env.MASTRACODE_PROFILE_DIR?.trim() || getDefaultProfileParentDirectory(),\n ...PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS,\n };\n const message = error instanceof Error ? error.message : String(error);\n return {\n diagnostics: new ProcessMemoryDiagnostics(config, dependencies, message),\n enabled: isEnabled(env.MASTRACODE_PROFILE),\n error: message,\n };\n }\n}\n\nexport async function startConfiguredProcessMemoryDiagnostics(\n setup: ProcessMemoryDiagnosticsSetup,\n warn: (message: string) => void,\n): Promise<ProcessMemoryDiagnostics> {\n if (!setup.enabled) return setup.diagnostics;\n if (setup.error) {\n warn(`Process memory diagnostics were not started: ${setup.error}`);\n return setup.diagnostics;\n }\n\n const status = await setup.diagnostics.start();\n if (status.state !== 'active') {\n warn(`Process memory diagnostics were not started: ${status.error ?? 'unknown inspector error'}`);\n }\n return setup.diagnostics;\n}\n\nexport async function stopProcessMemoryDiagnosticsWithTimeout(\n diagnostics: ProcessMemoryDiagnostics,\n warn: (message: string) => void,\n timeoutMs = 5_000,\n): Promise<void> {\n let timeout: ReturnType<typeof setTimeout> | undefined;\n const timedOut = await Promise.race([\n diagnostics.stop().then(\n () => false,\n error => {\n warn(`Process memory diagnostics did not stop cleanly: ${errorMessage(error)}`);\n return false;\n },\n ),\n new Promise<true>(resolve => {\n timeout = setTimeout(() => resolve(true), timeoutMs);\n timeout.unref();\n }),\n ]);\n if (timeout) clearTimeout(timeout);\n if (timedOut)\n warn(`Process memory diagnostics did not stop within ${timeoutMs}ms; final artifacts may be incomplete.`);\n}\n\nfunction defaultInspectorSession(): InspectorSessionAdapter {\n const session = new Session();\n return {\n connect: () => session.connect(),\n disconnect: () => session.disconnect(),\n post: async (method, params) =>\n (await session.post(\n method as Parameters<Session['post']>[0],\n params as Parameters<Session['post']>[1],\n )) as unknown as Record<string, unknown>,\n };\n}\n\nfunction defaultPerformanceObserver(callback: (entries: PerformanceEntry[]) => void) {\n const observer = new PerformanceObserver(list => callback(list.getEntries()));\n return observer;\n}\n\nfunction errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\nfunction safeTimestamp(date: Date): string {\n return date.toISOString().replaceAll(':', '-');\n}\n\nfunction unrefTimer(timer: ReturnType<typeof setInterval>): void {\n timer.unref?.();\n}\n\nexport class ProcessMemoryDiagnostics {\n readonly config: ProcessMemoryDiagnosticsConfig;\n\n private state: ProcessMemoryDiagnosticsStatus['state'] = 'inactive';\n private outputDirectory: string | null = null;\n private inspector: InspectorSessionAdapter | null = null;\n private observer: PerformanceObserverAdapter | null = null;\n private sampleTimer: ReturnType<typeof setInterval> | null = null;\n private captureTimer: ReturnType<typeof setInterval> | null = null;\n private startedAt: Date | null = null;\n private sampleCount = 0;\n private captureCount = 0;\n private gcEventCount = 0;\n private latestSample: ProcessMemoryDiagnosticsMemorySample | null = null;\n private latestCapturePath: string | null = null;\n private latestError: string | null;\n private samplingActive = false;\n private stopRequested = false;\n private startingPromise: Promise<ProcessMemoryDiagnosticsStatus> | null = null;\n private stoppingPromise: Promise<ProcessMemoryDiagnosticsStatus> | null = null;\n private restartAfterStopPromise: Promise<ProcessMemoryDiagnosticsStatus> | null = null;\n private restartRequested = false;\n private pendingGcEvents: Array<Record<string, unknown>> = [];\n private gcEventBufferOverflowed = false;\n private artifactWriteFailed = false;\n private captureQueue: Promise<unknown> = Promise.resolve();\n private writeQueue: Promise<unknown> = Promise.resolve();\n\n private readonly configError: string | null;\n private readonly createInspectorSession: () => InspectorSessionAdapter;\n private readonly createPerformanceObserver: NonNullable<\n ProcessMemoryDiagnosticsDependencies['createPerformanceObserver']\n >;\n private readonly now: () => Date;\n private readonly randomId: () => string;\n\n constructor(\n config: ProcessMemoryDiagnosticsConfig,\n dependencies: ProcessMemoryDiagnosticsDependencies = {},\n initialError: string | null = null,\n ) {\n this.config = { ...config };\n this.configError = initialError;\n this.latestError = initialError;\n this.createInspectorSession = dependencies.createInspectorSession ?? defaultInspectorSession;\n this.createPerformanceObserver = dependencies.createPerformanceObserver ?? defaultPerformanceObserver;\n this.now = dependencies.now ?? (() => new Date());\n this.randomId = dependencies.randomId ?? randomUUID;\n }\n\n getStatus(): ProcessMemoryDiagnosticsStatus {\n return {\n state: this.state,\n outputDirectory: this.outputDirectory,\n config: { ...this.config },\n sampleCount: this.sampleCount,\n captureCount: this.captureCount,\n gcEventCount: this.gcEventCount,\n latestSample: this.latestSample,\n latestCapturePath: this.latestCapturePath,\n error: this.latestError,\n };\n }\n\n start(): Promise<ProcessMemoryDiagnosticsStatus> {\n if (this.state === 'active') return Promise.resolve(this.getStatus());\n if (this.state === 'starting' && this.startingPromise) return this.startingPromise;\n if (this.state === 'stopping' && this.stoppingPromise) {\n this.restartRequested = true;\n this.restartAfterStopPromise ??= this.stoppingPromise.then(() => {\n this.restartAfterStopPromise = null;\n if (!this.restartRequested) return this.getStatus();\n this.restartRequested = false;\n return this.start();\n });\n return this.restartAfterStopPromise;\n }\n if (this.configError) {\n this.latestError = this.configError;\n this.state = 'error';\n return Promise.resolve(this.getStatus());\n }\n\n this.state = 'starting';\n this.stoppingPromise = null;\n this.restartRequested = false;\n this.stopRequested = false;\n this.outputDirectory = null;\n this.sampleCount = 0;\n this.captureCount = 0;\n this.gcEventCount = 0;\n this.pendingGcEvents = [];\n this.gcEventBufferOverflowed = false;\n this.artifactWriteFailed = false;\n this.latestSample = null;\n this.latestCapturePath = null;\n this.latestError = null;\n this.startedAt = this.now();\n\n const startingPromise = this.startRun();\n this.startingPromise = startingPromise;\n void startingPromise.finally(() => {\n if (this.startingPromise === startingPromise) this.startingPromise = null;\n });\n return startingPromise;\n }\n\n private async startRun(): Promise<ProcessMemoryDiagnosticsStatus> {\n try {\n await this.createArtifacts();\n if (await this.abortStartIfRequested()) return this.getStatus();\n\n this.observeGc();\n this.inspector = this.createInspectorSession();\n this.inspector.connect();\n await this.startSampling();\n if (await this.abortStartIfRequested()) return this.getStatus();\n\n await this.takeSample();\n if (await this.abortStartIfRequested()) return this.getStatus();\n\n this.sampleTimer = setInterval(() => {\n void this.takeSample().catch(error => this.recordError('Process sample failed', error));\n }, this.config.sampleIntervalMs);\n unrefTimer(this.sampleTimer);\n\n this.captureTimer = setInterval(() => {\n void this.capture('periodic').catch(error => this.recordError('Periodic allocation capture failed', error));\n }, this.config.captureIntervalMs);\n unrefTimer(this.captureTimer);\n\n this.state = 'active';\n return this.getStatus();\n } catch (error) {\n this.latestError = `Unable to start process memory diagnostics: ${errorMessage(error)}`;\n await this.cleanupAfterStartFailure();\n this.state = 'error';\n return this.getStatus();\n }\n }\n\n private async abortStartIfRequested(): Promise<boolean> {\n if (!this.stopRequested) return false;\n await this.cleanupAfterStartFailure();\n this.state = 'inactive';\n return true;\n }\n\n capture(reason: 'manual' | 'periodic' = 'manual'): Promise<ProcessMemoryDiagnosticsCapture> {\n if (this.state !== 'active') {\n return Promise.reject(new Error(this.latestError ?? 'Process memory diagnostics are not active.'));\n }\n return this.enqueueCapture(() => this.captureEpoch(reason, false));\n }\n\n async stop(): Promise<ProcessMemoryDiagnosticsStatus> {\n if (this.stoppingPromise) {\n this.restartRequested = false;\n return this.stoppingPromise;\n }\n if (this.state === 'inactive') return this.getStatus();\n if (this.state === 'error' && !this.inspector && !this.outputDirectory) return this.getStatus();\n\n const startingPromise = this.state === 'starting' ? this.startingPromise : null;\n this.stopRequested = true;\n this.state = 'stopping';\n this.clearTimersAndObserver();\n\n if (startingPromise) {\n this.stoppingPromise = startingPromise.then(() => this.getStatus());\n return this.stoppingPromise;\n }\n\n this.stoppingPromise = (async () => {\n let stopError: string | null = null;\n try {\n if (this.outputDirectory) await this.takeSample();\n await this.enqueueCapture(async () => {\n if (this.inspector && this.samplingActive) await this.captureEpoch('stop', true);\n });\n await this.writeQueue;\n if (this.gcEventBufferOverflowed) {\n throw new Error(`GC event buffer exceeded ${MAX_PENDING_GC_EVENTS} records before the next process sample.`);\n }\n if (this.artifactWriteFailed) throw new Error('One or more process memory diagnostic artifact writes failed.');\n this.latestError = null;\n } catch (error) {\n stopError = `Unable to stop process memory diagnostics cleanly: ${errorMessage(error)}`;\n this.latestError = stopError;\n } finally {\n this.disconnectInspector();\n this.state = stopError ? 'error' : 'inactive';\n }\n return this.getStatus();\n })();\n\n return this.stoppingPromise;\n }\n\n private async createArtifacts(): Promise<void> {\n await mkdir(this.config.parentDirectory, { recursive: true, mode: 0o700 });\n await chmod(this.config.parentDirectory, 0o700);\n const runName = `run-${safeTimestamp(this.startedAt!)}-${process.pid}-${this.randomId()\n .replaceAll(/[^a-zA-Z0-9-]/g, '')\n .slice(0, 12)}`;\n this.outputDirectory = join(this.config.parentDirectory, runName);\n await mkdir(this.outputDirectory, { mode: 0o700 });\n await chmod(this.outputDirectory, 0o700);\n\n const metadata = {\n schemaVersion: 1,\n runId: runName,\n startedAt: this.startedAt!.toISOString(),\n pid: process.pid,\n nodeVersion: process.version,\n platform,\n arch,\n config: this.config,\n };\n await writeFile(join(this.outputDirectory, 'metadata.json'), `${JSON.stringify(metadata, null, 2)}\\n`, {\n flag: 'wx',\n mode: 0o600,\n });\n await writeFile(join(this.outputDirectory, 'process-samples.jsonl'), '', { flag: 'wx', mode: 0o600 });\n await writeFile(join(this.outputDirectory, 'gc-events.jsonl'), '', { flag: 'wx', mode: 0o600 });\n }\n\n private observeGc(): void {\n const observer = this.createPerformanceObserver(entries => {\n const before = this.latestSample?.memory ?? null;\n const after = process.memoryUsage();\n const timestamp = this.now().toISOString();\n for (const entry of entries) {\n const gcEntry = entry as PerformanceEntry & {\n kind?: number;\n flags?: number;\n detail?: { kind?: number; flags?: number };\n };\n this.gcEventCount += 1;\n if (this.pendingGcEvents.length >= MAX_PENDING_GC_EVENTS) {\n this.gcEventBufferOverflowed = true;\n this.latestError = `GC event buffer exceeded ${MAX_PENDING_GC_EVENTS} records before the next process sample.`;\n continue;\n }\n this.pendingGcEvents.push({\n timestamp,\n sequence: this.gcEventCount,\n name: entry.name,\n startTime: entry.startTime,\n duration: entry.duration,\n kind: gcEntry.detail?.kind ?? gcEntry.kind ?? null,\n flags: gcEntry.detail?.flags ?? gcEntry.flags ?? null,\n before,\n after,\n latestSampleSequence: this.latestSample?.sequence ?? null,\n });\n }\n });\n observer.observe({ entryTypes: ['gc'] });\n this.observer = observer;\n }\n\n private async startSampling(): Promise<void> {\n if (!this.inspector) throw new Error('Inspector session is unavailable.');\n await this.inspector.post('HeapProfiler.startSampling', {\n samplingInterval: this.config.allocationIntervalBytes,\n includeObjectsCollectedByMajorGC: true,\n includeObjectsCollectedByMinorGC: true,\n });\n this.samplingActive = true;\n }\n\n private async takeSample(): Promise<ProcessMemoryDiagnosticsMemorySample> {\n if (!this.outputDirectory || !this.startedAt) throw new Error('Diagnostics output directory is unavailable.');\n const sample: ProcessMemoryDiagnosticsMemorySample = {\n timestamp: this.now().toISOString(),\n sequence: this.sampleCount + 1,\n elapsedMs: Math.max(0, this.now().getTime() - this.startedAt.getTime()),\n memory: process.memoryUsage(),\n resourceUsage: process.resourceUsage(),\n heap: getHeapStatistics(),\n heapSpaces: getHeapSpaceStatistics(),\n };\n this.sampleCount = sample.sequence;\n this.latestSample = sample;\n await this.enqueueWrite('process-samples.jsonl', sample);\n const gcEvents = this.pendingGcEvents.splice(0);\n if (gcEvents.length > 0) await this.enqueueWriteBatch('gc-events.jsonl', gcEvents);\n return sample;\n }\n\n private enqueueWrite(fileName: string, value: unknown): Promise<void> {\n return this.enqueueWriteBatch(fileName, [value]);\n }\n\n private enqueueWriteBatch(fileName: string, values: unknown[]): Promise<void> {\n const operation = this.writeQueue.then(async () => {\n if (!this.outputDirectory || values.length === 0) return;\n const lines = values.map(value => JSON.stringify(value)).join('\\n');\n try {\n await appendFile(join(this.outputDirectory, fileName), `${lines}\\n`, { mode: 0o600 });\n } catch (error) {\n this.artifactWriteFailed = true;\n throw error;\n }\n });\n this.writeQueue = operation.catch(() => undefined);\n return operation;\n }\n\n private enqueueCapture<T>(operation: () => Promise<T>): Promise<T> {\n const result = this.captureQueue.then(operation);\n this.captureQueue = result.catch(() => undefined);\n return result;\n }\n\n private async captureEpoch(\n reason: ProcessMemoryDiagnosticsCapture['reason'],\n final: boolean,\n ): Promise<ProcessMemoryDiagnosticsCapture> {\n if (!this.inspector || !this.samplingActive || !this.outputDirectory) {\n throw new Error(this.latestError ?? 'Allocation sampling is not active.');\n }\n\n let response: Record<string, unknown>;\n try {\n response = await this.inspector.post('HeapProfiler.stopSampling');\n this.samplingActive = false;\n } catch (error) {\n this.latestError = `Allocation capture failed: ${errorMessage(error)}`;\n if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();\n throw new Error(this.latestError, { cause: error });\n }\n\n const profile = response.profile;\n if (!profile || typeof profile !== 'object') {\n this.latestError = 'Allocation capture failed: inspector returned no profile.';\n if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();\n throw new Error(this.latestError);\n }\n\n const sequence = this.captureCount + 1;\n const timestamp = this.now().toISOString();\n const filename = `allocation-${String(sequence).padStart(6, '0')}-${safeTimestamp(new Date(timestamp))}.heapprofile`;\n const finalPath = join(this.outputDirectory, filename);\n const temporaryPath = join(this.outputDirectory, `.${filename}.${this.randomId()}.tmp`);\n\n try {\n await writeFile(temporaryPath, `${JSON.stringify(profile)}\\n`, { flag: 'wx', mode: 0o600 });\n await chmod(temporaryPath, 0o600);\n await rename(temporaryPath, finalPath);\n await chmod(finalPath, 0o600);\n this.captureCount = sequence;\n this.latestCapturePath = finalPath;\n this.latestError = null;\n } catch (error) {\n this.artifactWriteFailed = true;\n this.latestError = `Unable to persist allocation capture: ${errorMessage(error)}`;\n throw new Error(this.latestError, { cause: error });\n } finally {\n if (!final && !this.stopRequested && this.state === 'active') {\n try {\n await this.startSampling();\n } catch (error) {\n this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`;\n this.state = 'error';\n }\n }\n }\n\n if (!final && !this.stopRequested && !this.samplingActive) {\n throw new Error(this.latestError ?? 'Allocation sampling did not restart.');\n }\n\n return { path: finalPath, sequence, timestamp, reason };\n }\n\n private async restartSamplingAfterFailure(): Promise<void> {\n try {\n await this.startSampling();\n } catch (error) {\n this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`;\n this.state = 'error';\n }\n }\n\n private recordError(prefix: string, error: unknown): void {\n this.latestError = `${prefix}: ${errorMessage(error)}`;\n }\n\n private clearTimersAndObserver(): void {\n if (this.sampleTimer) clearInterval(this.sampleTimer);\n if (this.captureTimer) clearInterval(this.captureTimer);\n this.sampleTimer = null;\n this.captureTimer = null;\n this.observer?.disconnect();\n this.observer = null;\n }\n\n private disconnectInspector(): void {\n this.samplingActive = false;\n if (!this.inspector) return;\n try {\n this.inspector.disconnect();\n } catch {\n // Best-effort cleanup after an inspector failure.\n }\n this.inspector = null;\n }\n\n private async cleanupAfterStartFailure(): Promise<void> {\n this.clearTimersAndObserver();\n this.disconnectInspector();\n await this.writeQueue;\n try {\n if (this.outputDirectory) await rm(this.outputDirectory, { recursive: true, force: true });\n } catch (error) {\n this.latestError = `${this.latestError} Failed to remove partial artifacts: ${errorMessage(error)}`;\n } finally {\n this.outputDirectory = null;\n }\n }\n}\n"],"mappings":";;;;;;;;;AASA,MAAa,sCAAsC;CACjD,kBAAkB;CAClB,mBAAmB;CACnB,yBAAyB;AAC3B;AAEA,MAAa,sCAAsC;CACjD,kBAAkB;CAClB,mBAAmB;CACnB,yBAAyB;AAC3B;AAEA,MAAM,qBAAqB;AAC3B,MAAM,wBAAwB;AAsE9B,IAAa,sCAAb,cAAyD,MAAM;CAC7D,YAAY,SAAiB;EAC3B,MAAM,OAAO;EACb,KAAK,OAAO;CACd;AACF;AAEA,SAAS,UAAU,OAAoC;CACrD,OAAO;EAAC;EAAK;EAAQ;EAAO;CAAI,CAAC,CAAC,SAAS,OAAO,KAAK,CAAC,CAAC,YAAY,KAAK,EAAE;AAC9E;AAEA,SAAS,oBACP,KACA,MACA,UACA,SACA,SACQ;CACR,MAAM,MAAM,IAAI;CAChB,IAAI,QAAQ,KAAA,KAAa,IAAI,KAAK,MAAM,IAAI,OAAO;CACnD,MAAM,QAAQ,OAAO,GAAG;CACxB,IAAI,CAAC,OAAO,cAAc,KAAK,KAAK,QAAQ,WAAY,YAAY,KAAA,KAAa,QAAQ,SAEvF,MAAM,IAAI,oCACR,GAAG,KAAK,sBAFI,YAAY,KAAA,IAAY,4BAA4B,YAAY,WAAW,QAAQ,OAAO,UAElE,aAAa,KAAK,UAAU,GAAG,EAAE,EACvE;CAEF,OAAO;AACT;AAEA,SAAS,mCAA2C;CAClD,IAAI,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O,EAAE,WAAW,GAAG,SAAS,GAAG,IAAI,CAErE;AAED;;;GAGG;AACH,eAAO,MAAM,oBAAoB,EAAE,uBAkBlC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,qBAAqB,EAAE,uBA2DnC,CAAC;AA+CF;;;;;;;;;GASG;AACH,wBAAgB,iCAAiC,CAC/C,OAAO,EAAE,MAAM,EACf,aAAa,CAAC,EAAE,aAAa,GAC5B,uBAAuB,GAAG,SAAS,CAiCrC;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,GAAE;IAAE,WAAW,CAAC,EAAE,eAAe,CAAA;CAAO,GAAG,OAAO,KAAK,CAmDnG;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,GAAE,MAAmC,EAC5C,OAAO,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAAC,WAAW,CAAC,EAAE,eAAe,CAAC;IAAC,aAAa,CAAC,EAAE,aAAa,CAAA;CAAE,GAC3G,iBAAiB,CA4BnB"}
@@ -1,4 +1,5 @@
1
1
  import { AuthStorage } from "../auth/storage.js";
2
+ import { ProviderAuthRequiredError } from "../auth/provider-auth-error.js";
2
3
  import { wrapLanguageModel } from "ai";
3
4
  import { createAnthropic } from "@ai-sdk/anthropic";
4
5
  //#region src/providers/claude-max.ts
@@ -196,7 +197,7 @@ function buildAnthropicOAuthFetch(opts = {}) {
196
197
  storage.reload();
197
198
  if (storage.get("anthropic")?.type === "api_key") throw new Error("Anthropic API key credential is configured, but OAuth is required.");
198
199
  const accessToken = await storage.getApiKey("anthropic");
199
- if (!accessToken) throw new Error("Not logged in to Anthropic. Run /login first.");
200
+ if (!accessToken) throw new ProviderAuthRequiredError("Not logged in to Anthropic.");
200
201
  const headers = new Headers();
201
202
  if (init?.headers) (init.headers instanceof Headers ? init.headers : Array.isArray(init.headers) ? new Headers(init.headers) : new Headers(init.headers)).forEach((value, key) => {
202
203
  const lower = key.toLowerCase();