@tracecode/harness 0.4.0

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 (55) hide show
  1. package/CHANGELOG.md +113 -0
  2. package/LICENSE +674 -0
  3. package/README.md +266 -0
  4. package/dist/browser.cjs +1352 -0
  5. package/dist/browser.cjs.map +1 -0
  6. package/dist/browser.d.cts +49 -0
  7. package/dist/browser.d.ts +49 -0
  8. package/dist/browser.js +1317 -0
  9. package/dist/browser.js.map +1 -0
  10. package/dist/cli.cjs +70 -0
  11. package/dist/cli.cjs.map +1 -0
  12. package/dist/cli.d.cts +1 -0
  13. package/dist/cli.d.ts +1 -0
  14. package/dist/cli.js +70 -0
  15. package/dist/cli.js.map +1 -0
  16. package/dist/core.cjs +286 -0
  17. package/dist/core.cjs.map +1 -0
  18. package/dist/core.d.cts +69 -0
  19. package/dist/core.d.ts +69 -0
  20. package/dist/core.js +254 -0
  21. package/dist/core.js.map +1 -0
  22. package/dist/index.cjs +2603 -0
  23. package/dist/index.cjs.map +1 -0
  24. package/dist/index.d.cts +6 -0
  25. package/dist/index.d.ts +6 -0
  26. package/dist/index.js +2538 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/internal/browser.cjs +647 -0
  29. package/dist/internal/browser.cjs.map +1 -0
  30. package/dist/internal/browser.d.cts +143 -0
  31. package/dist/internal/browser.d.ts +143 -0
  32. package/dist/internal/browser.js +617 -0
  33. package/dist/internal/browser.js.map +1 -0
  34. package/dist/javascript.cjs +549 -0
  35. package/dist/javascript.cjs.map +1 -0
  36. package/dist/javascript.d.cts +11 -0
  37. package/dist/javascript.d.ts +11 -0
  38. package/dist/javascript.js +518 -0
  39. package/dist/javascript.js.map +1 -0
  40. package/dist/python.cjs +744 -0
  41. package/dist/python.cjs.map +1 -0
  42. package/dist/python.d.cts +97 -0
  43. package/dist/python.d.ts +97 -0
  44. package/dist/python.js +698 -0
  45. package/dist/python.js.map +1 -0
  46. package/dist/runtime-types-C7d1LFbx.d.ts +85 -0
  47. package/dist/runtime-types-Dvgn07z9.d.cts +85 -0
  48. package/dist/types-Bzr1Ohcf.d.cts +96 -0
  49. package/dist/types-Bzr1Ohcf.d.ts +96 -0
  50. package/package.json +89 -0
  51. package/workers/javascript/javascript-worker.js +2918 -0
  52. package/workers/python/generated-python-harness-snippets.js +20 -0
  53. package/workers/python/pyodide-worker.js +1197 -0
  54. package/workers/python/runtime-core.js +1529 -0
  55. package/workers/vendor/typescript.js +200276 -0
@@ -0,0 +1,647 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // packages/harness-browser/src/internal.ts
21
+ var internal_exports = {};
22
+ __export(internal_exports, {
23
+ JavaScriptWorkerClient: () => JavaScriptWorkerClient,
24
+ PyodideWorkerClient: () => PyodideWorkerClient,
25
+ isJavaScriptWorkerSupported: () => isJavaScriptWorkerSupported,
26
+ isWorkerSupported: () => isWorkerSupported
27
+ });
28
+ module.exports = __toCommonJS(internal_exports);
29
+
30
+ // packages/harness-browser/src/pyodide-worker-client.ts
31
+ var EXECUTION_TIMEOUT_MS = 1e4;
32
+ var INTERVIEW_MODE_TIMEOUT_MS = 5e3;
33
+ var TRACING_TIMEOUT_MS = 3e4;
34
+ var INIT_TIMEOUT_MS = 12e4;
35
+ var MESSAGE_TIMEOUT_MS = 2e4;
36
+ var WORKER_READY_TIMEOUT_MS = 1e4;
37
+ var PyodideWorkerClient = class {
38
+ constructor(options) {
39
+ this.options = options;
40
+ this.debug = options.debug ?? process.env.NODE_ENV === "development";
41
+ }
42
+ worker = null;
43
+ pendingMessages = /* @__PURE__ */ new Map();
44
+ messageId = 0;
45
+ isInitializing = false;
46
+ initPromise = null;
47
+ workerReadyPromise = null;
48
+ workerReadyResolve = null;
49
+ workerReadyReject = null;
50
+ debug;
51
+ /**
52
+ * Check if Web Workers are supported
53
+ */
54
+ isSupported() {
55
+ return typeof Worker !== "undefined";
56
+ }
57
+ /**
58
+ * Get or create the worker instance
59
+ */
60
+ getWorker() {
61
+ if (this.worker) return this.worker;
62
+ if (!this.isSupported()) {
63
+ throw new Error("Web Workers are not supported in this environment");
64
+ }
65
+ this.workerReadyPromise = new Promise((resolve, reject) => {
66
+ this.workerReadyResolve = resolve;
67
+ this.workerReadyReject = (error) => reject(error);
68
+ });
69
+ const workerUrl = this.debug && !this.options.workerUrl.includes("?") ? `${this.options.workerUrl}?dev=${Date.now()}` : this.options.workerUrl;
70
+ this.worker = new Worker(workerUrl);
71
+ this.worker.onmessage = (event) => {
72
+ const { id, type, payload } = event.data;
73
+ if (type === "worker-ready") {
74
+ this.workerReadyResolve?.();
75
+ this.workerReadyResolve = null;
76
+ this.workerReadyReject = null;
77
+ if (this.debug) console.log("[PyodideWorkerClient] worker-ready");
78
+ return;
79
+ }
80
+ if (this.debug && !id) {
81
+ console.log("[PyodideWorkerClient] event", { type, payload });
82
+ }
83
+ if (id) {
84
+ const pending = this.pendingMessages.get(id);
85
+ if (pending) {
86
+ this.pendingMessages.delete(id);
87
+ if (pending.timeoutId) globalThis.clearTimeout(pending.timeoutId);
88
+ if (type === "error") {
89
+ pending.reject(new Error(payload.error));
90
+ } else {
91
+ if (this.debug) console.log("[PyodideWorkerClient] recv", { id, type });
92
+ pending.resolve(payload);
93
+ }
94
+ }
95
+ }
96
+ };
97
+ this.worker.onerror = (error) => {
98
+ console.error("[PyodideWorkerClient] Worker error:", error);
99
+ const workerError = new Error("Worker error");
100
+ this.workerReadyReject?.(workerError);
101
+ this.workerReadyResolve = null;
102
+ this.workerReadyReject = null;
103
+ for (const [id, pending] of this.pendingMessages) {
104
+ if (pending.timeoutId) {
105
+ globalThis.clearTimeout(pending.timeoutId);
106
+ }
107
+ pending.reject(workerError);
108
+ this.pendingMessages.delete(id);
109
+ }
110
+ };
111
+ return this.worker;
112
+ }
113
+ /**
114
+ * Wait for worker bootstrap signal with timeout.
115
+ * Guards against deadlocks when the worker script fails before posting "worker-ready".
116
+ */
117
+ async waitForWorkerReady() {
118
+ const readyPromise = this.workerReadyPromise;
119
+ if (!readyPromise) return;
120
+ await new Promise((resolve, reject) => {
121
+ let settled = false;
122
+ const timeoutId = globalThis.setTimeout(() => {
123
+ if (settled) return;
124
+ settled = true;
125
+ const timeoutError = new Error(
126
+ `Python worker failed to initialize in time (${Math.round(WORKER_READY_TIMEOUT_MS / 1e3)}s)`
127
+ );
128
+ if (this.debug) {
129
+ console.warn("[PyodideWorkerClient] worker-ready timeout", { timeoutMs: WORKER_READY_TIMEOUT_MS });
130
+ }
131
+ this.terminateAndReset(timeoutError);
132
+ reject(timeoutError);
133
+ }, WORKER_READY_TIMEOUT_MS);
134
+ readyPromise.then(() => {
135
+ if (settled) return;
136
+ settled = true;
137
+ globalThis.clearTimeout(timeoutId);
138
+ resolve();
139
+ }).catch((error) => {
140
+ if (settled) return;
141
+ settled = true;
142
+ globalThis.clearTimeout(timeoutId);
143
+ reject(error instanceof Error ? error : new Error(String(error)));
144
+ });
145
+ });
146
+ }
147
+ /**
148
+ * Send a message to the worker and wait for a response
149
+ */
150
+ async sendMessage(type, payload, timeoutMs = MESSAGE_TIMEOUT_MS) {
151
+ const worker = this.getWorker();
152
+ await this.waitForWorkerReady();
153
+ const id = String(++this.messageId);
154
+ return new Promise((resolve, reject) => {
155
+ this.pendingMessages.set(id, {
156
+ resolve,
157
+ reject
158
+ });
159
+ if (this.debug) console.log("[PyodideWorkerClient] send", { id, type });
160
+ const timeoutId = globalThis.setTimeout(() => {
161
+ const pending2 = this.pendingMessages.get(id);
162
+ if (!pending2) return;
163
+ this.pendingMessages.delete(id);
164
+ if (this.debug) console.warn("[PyodideWorkerClient] timeout", { id, type });
165
+ pending2.reject(new Error(`Worker request timed out: ${type}`));
166
+ }, timeoutMs);
167
+ const pending = this.pendingMessages.get(id);
168
+ if (pending) pending.timeoutId = timeoutId;
169
+ worker.postMessage({ id, type, payload });
170
+ });
171
+ }
172
+ /**
173
+ * Execute code with a timeout - terminates worker if execution takes too long
174
+ */
175
+ async executeWithTimeout(executor, timeoutMs = EXECUTION_TIMEOUT_MS) {
176
+ return new Promise((resolve, reject) => {
177
+ let settled = false;
178
+ const timeoutId = globalThis.setTimeout(() => {
179
+ if (settled) return;
180
+ settled = true;
181
+ if (this.debug) {
182
+ console.warn("[PyodideWorkerClient] Execution timeout - terminating worker");
183
+ }
184
+ this.terminateAndReset();
185
+ const seconds = Math.round(timeoutMs / 1e3);
186
+ reject(new Error(`Execution timed out (possible infinite loop). Code execution was stopped after ${seconds} seconds.`));
187
+ }, timeoutMs);
188
+ executor().then((result) => {
189
+ if (settled) return;
190
+ settled = true;
191
+ globalThis.clearTimeout(timeoutId);
192
+ resolve(result);
193
+ }).catch((error) => {
194
+ if (settled) return;
195
+ settled = true;
196
+ globalThis.clearTimeout(timeoutId);
197
+ reject(error);
198
+ });
199
+ });
200
+ }
201
+ /**
202
+ * Terminate the worker and reset state for recreation
203
+ */
204
+ terminateAndReset(reason = new Error("Worker was terminated")) {
205
+ this.workerReadyReject?.(reason);
206
+ if (this.worker) {
207
+ this.worker.terminate();
208
+ this.worker = null;
209
+ }
210
+ this.initPromise = null;
211
+ this.isInitializing = false;
212
+ this.workerReadyPromise = null;
213
+ this.workerReadyResolve = null;
214
+ for (const [, pending] of this.pendingMessages) {
215
+ if (pending.timeoutId) globalThis.clearTimeout(pending.timeoutId);
216
+ pending.reject(reason);
217
+ }
218
+ this.pendingMessages.clear();
219
+ }
220
+ /**
221
+ * Initialize Pyodide in the worker
222
+ */
223
+ async init() {
224
+ if (this.initPromise) {
225
+ return this.initPromise;
226
+ }
227
+ if (this.isInitializing) {
228
+ await new Promise((resolve) => setTimeout(resolve, 100));
229
+ return this.init();
230
+ }
231
+ this.isInitializing = true;
232
+ this.initPromise = (async () => {
233
+ try {
234
+ return await this.sendMessage("init", void 0, INIT_TIMEOUT_MS);
235
+ } catch (error) {
236
+ const message = error instanceof Error ? error.message : String(error);
237
+ const shouldRetry = message.includes("Worker request timed out: init") || message.includes("Worker was terminated") || message.includes("Worker error") || message.includes("failed to initialize in time");
238
+ if (!shouldRetry) {
239
+ throw error;
240
+ }
241
+ if (this.debug) {
242
+ console.warn("[PyodideWorkerClient] init failed, resetting worker and retrying once", { message });
243
+ }
244
+ this.terminateAndReset();
245
+ return this.sendMessage("init", void 0, INIT_TIMEOUT_MS);
246
+ }
247
+ })();
248
+ try {
249
+ const result = await this.initPromise;
250
+ return result;
251
+ } catch (error) {
252
+ this.initPromise = null;
253
+ throw error;
254
+ } finally {
255
+ this.isInitializing = false;
256
+ }
257
+ }
258
+ /**
259
+ * Execute Python code with tracing for step-by-step visualization
260
+ * @param options.maxLineEvents - Max line events before abort (for complexity analysis, use higher values)
261
+ */
262
+ async executeWithTracing(code, functionName, inputs, options, executionStyle = "function") {
263
+ await this.init();
264
+ try {
265
+ return await this.executeWithTimeout(
266
+ () => this.sendMessage("execute-with-tracing", {
267
+ code,
268
+ functionName,
269
+ inputs,
270
+ executionStyle,
271
+ options
272
+ }, TRACING_TIMEOUT_MS + 5e3),
273
+ // Message timeout slightly longer than execution timeout
274
+ TRACING_TIMEOUT_MS
275
+ );
276
+ } catch (error) {
277
+ const errorMessage = error instanceof Error ? error.message : String(error);
278
+ const isClientTimeout = errorMessage.includes("Execution timed out") || errorMessage.includes("possible infinite loop");
279
+ if (isClientTimeout) {
280
+ return {
281
+ success: false,
282
+ error: errorMessage,
283
+ trace: [],
284
+ executionTimeMs: TRACING_TIMEOUT_MS,
285
+ consoleOutput: [],
286
+ traceLimitExceeded: true,
287
+ timeoutReason: "client-timeout",
288
+ lineEventCount: 0,
289
+ traceStepCount: 0
290
+ };
291
+ }
292
+ throw error;
293
+ }
294
+ }
295
+ /**
296
+ * Execute Python code without tracing (for running tests)
297
+ */
298
+ async executeCode(code, functionName, inputs, executionStyle = "function") {
299
+ await this.init();
300
+ return this.executeWithTimeout(
301
+ () => this.sendMessage("execute-code", {
302
+ code,
303
+ functionName,
304
+ inputs,
305
+ executionStyle
306
+ }, EXECUTION_TIMEOUT_MS + 5e3),
307
+ EXECUTION_TIMEOUT_MS
308
+ );
309
+ }
310
+ /**
311
+ * Execute Python code in interview mode - 5 second timeout, generic error messages
312
+ * Does not reveal which line caused the timeout
313
+ */
314
+ async executeCodeInterviewMode(code, functionName, inputs, executionStyle = "function") {
315
+ await this.init();
316
+ try {
317
+ const result = await this.executeWithTimeout(
318
+ () => this.sendMessage("execute-code-interview", {
319
+ code,
320
+ functionName,
321
+ inputs,
322
+ executionStyle
323
+ }, INTERVIEW_MODE_TIMEOUT_MS + 2e3),
324
+ INTERVIEW_MODE_TIMEOUT_MS
325
+ );
326
+ if (!result.success && result.error) {
327
+ const normalizedError = result.error.toLowerCase();
328
+ const isTimeoutOrResourceLimit = normalizedError.includes("timed out") || normalizedError.includes("execution timeout") || normalizedError.includes("infinite loop") || normalizedError.includes("interview_guard_triggered") || normalizedError.includes("memory-limit") || normalizedError.includes("line-limit") || normalizedError.includes("single-line-limit") || normalizedError.includes("recursion-limit");
329
+ if (isTimeoutOrResourceLimit) {
330
+ return {
331
+ success: false,
332
+ output: null,
333
+ error: "Time Limit Exceeded",
334
+ consoleOutput: result.consoleOutput ?? []
335
+ };
336
+ }
337
+ }
338
+ return result;
339
+ } catch (error) {
340
+ const errorMsg = error instanceof Error ? error.message : String(error);
341
+ if (errorMsg.includes("timed out") || errorMsg.includes("Execution timeout")) {
342
+ return {
343
+ success: false,
344
+ output: null,
345
+ error: "Time Limit Exceeded",
346
+ consoleOutput: []
347
+ };
348
+ }
349
+ return {
350
+ success: false,
351
+ output: null,
352
+ error: errorMsg,
353
+ consoleOutput: []
354
+ };
355
+ }
356
+ }
357
+ /**
358
+ * Check the status of the worker
359
+ */
360
+ async getStatus() {
361
+ return this.sendMessage("status");
362
+ }
363
+ /**
364
+ * Analyze Python code using AST (off main thread)
365
+ * Returns CodeFacts with semantic information about the code
366
+ */
367
+ async analyzeCode(code) {
368
+ await this.init();
369
+ return this.sendMessage("analyze-code", { code }, 5e3);
370
+ }
371
+ /**
372
+ * Terminate the worker and clean up resources
373
+ */
374
+ terminate() {
375
+ this.terminateAndReset();
376
+ }
377
+ };
378
+ function isWorkerSupported() {
379
+ return typeof Worker !== "undefined";
380
+ }
381
+
382
+ // packages/harness-browser/src/javascript-worker-client.ts
383
+ var EXECUTION_TIMEOUT_MS2 = 7e3;
384
+ var INTERVIEW_MODE_TIMEOUT_MS2 = 5e3;
385
+ var TRACING_TIMEOUT_MS2 = 7e3;
386
+ var INIT_TIMEOUT_MS2 = 1e4;
387
+ var MESSAGE_TIMEOUT_MS2 = 12e3;
388
+ var WORKER_READY_TIMEOUT_MS2 = 1e4;
389
+ var JavaScriptWorkerClient = class {
390
+ constructor(options) {
391
+ this.options = options;
392
+ this.debug = options.debug ?? process.env.NODE_ENV === "development";
393
+ }
394
+ worker = null;
395
+ pendingMessages = /* @__PURE__ */ new Map();
396
+ messageId = 0;
397
+ isInitializing = false;
398
+ initPromise = null;
399
+ workerReadyPromise = null;
400
+ workerReadyResolve = null;
401
+ workerReadyReject = null;
402
+ debug;
403
+ isSupported() {
404
+ return typeof Worker !== "undefined";
405
+ }
406
+ getWorker() {
407
+ if (this.worker) return this.worker;
408
+ if (!this.isSupported()) {
409
+ throw new Error("Web Workers are not supported in this environment");
410
+ }
411
+ this.workerReadyPromise = new Promise((resolve, reject) => {
412
+ this.workerReadyResolve = resolve;
413
+ this.workerReadyReject = (error) => reject(error);
414
+ });
415
+ const workerUrl = this.debug && !this.options.workerUrl.includes("?") ? `${this.options.workerUrl}?dev=${Date.now()}` : this.options.workerUrl;
416
+ this.worker = new Worker(workerUrl);
417
+ this.worker.onmessage = (event) => {
418
+ const { id, type, payload } = event.data;
419
+ if (type === "worker-ready") {
420
+ this.workerReadyResolve?.();
421
+ this.workerReadyResolve = null;
422
+ this.workerReadyReject = null;
423
+ if (this.debug) console.log("[JavaScriptWorkerClient] worker-ready");
424
+ return;
425
+ }
426
+ if (id) {
427
+ const pending = this.pendingMessages.get(id);
428
+ if (!pending) return;
429
+ this.pendingMessages.delete(id);
430
+ if (pending.timeoutId) globalThis.clearTimeout(pending.timeoutId);
431
+ if (type === "error") {
432
+ pending.reject(new Error(payload.error));
433
+ return;
434
+ }
435
+ pending.resolve(payload);
436
+ }
437
+ };
438
+ this.worker.onerror = (error) => {
439
+ console.error("[JavaScriptWorkerClient] Worker error:", error);
440
+ const workerError = new Error("Worker error");
441
+ this.workerReadyReject?.(workerError);
442
+ this.workerReadyResolve = null;
443
+ this.workerReadyReject = null;
444
+ for (const [id, pending] of this.pendingMessages) {
445
+ if (pending.timeoutId) globalThis.clearTimeout(pending.timeoutId);
446
+ pending.reject(workerError);
447
+ this.pendingMessages.delete(id);
448
+ }
449
+ };
450
+ return this.worker;
451
+ }
452
+ async waitForWorkerReady() {
453
+ const readyPromise = this.workerReadyPromise;
454
+ if (!readyPromise) return;
455
+ await new Promise((resolve, reject) => {
456
+ let settled = false;
457
+ const timeoutId = globalThis.setTimeout(() => {
458
+ if (settled) return;
459
+ settled = true;
460
+ const timeoutError = new Error(
461
+ `JavaScript worker failed to initialize in time (${Math.round(WORKER_READY_TIMEOUT_MS2 / 1e3)}s)`
462
+ );
463
+ this.terminateAndReset(timeoutError);
464
+ reject(timeoutError);
465
+ }, WORKER_READY_TIMEOUT_MS2);
466
+ readyPromise.then(() => {
467
+ if (settled) return;
468
+ settled = true;
469
+ globalThis.clearTimeout(timeoutId);
470
+ resolve();
471
+ }).catch((error) => {
472
+ if (settled) return;
473
+ settled = true;
474
+ globalThis.clearTimeout(timeoutId);
475
+ reject(error instanceof Error ? error : new Error(String(error)));
476
+ });
477
+ });
478
+ }
479
+ async sendMessage(type, payload, timeoutMs = MESSAGE_TIMEOUT_MS2) {
480
+ const worker = this.getWorker();
481
+ await this.waitForWorkerReady();
482
+ const id = String(++this.messageId);
483
+ return new Promise((resolve, reject) => {
484
+ this.pendingMessages.set(id, {
485
+ resolve,
486
+ reject
487
+ });
488
+ const timeoutId = globalThis.setTimeout(() => {
489
+ const pending2 = this.pendingMessages.get(id);
490
+ if (!pending2) return;
491
+ this.pendingMessages.delete(id);
492
+ pending2.reject(new Error(`Worker request timed out: ${type}`));
493
+ }, timeoutMs);
494
+ const pending = this.pendingMessages.get(id);
495
+ if (pending) pending.timeoutId = timeoutId;
496
+ worker.postMessage({ id, type, payload });
497
+ });
498
+ }
499
+ async executeWithTimeout(executor, timeoutMs) {
500
+ return new Promise((resolve, reject) => {
501
+ let settled = false;
502
+ const timeoutId = globalThis.setTimeout(() => {
503
+ if (settled) return;
504
+ settled = true;
505
+ this.terminateAndReset();
506
+ reject(
507
+ new Error(
508
+ `Execution timed out (possible infinite loop). Code execution was stopped after ${Math.round(timeoutMs / 1e3)} seconds.`
509
+ )
510
+ );
511
+ }, timeoutMs);
512
+ executor().then((result) => {
513
+ if (settled) return;
514
+ settled = true;
515
+ globalThis.clearTimeout(timeoutId);
516
+ resolve(result);
517
+ }).catch((error) => {
518
+ if (settled) return;
519
+ settled = true;
520
+ globalThis.clearTimeout(timeoutId);
521
+ reject(error);
522
+ });
523
+ });
524
+ }
525
+ terminateAndReset(reason = new Error("Worker was terminated")) {
526
+ this.workerReadyReject?.(reason);
527
+ if (this.worker) {
528
+ this.worker.terminate();
529
+ this.worker = null;
530
+ }
531
+ this.initPromise = null;
532
+ this.isInitializing = false;
533
+ this.workerReadyPromise = null;
534
+ this.workerReadyResolve = null;
535
+ this.workerReadyReject = null;
536
+ for (const [, pending] of this.pendingMessages) {
537
+ if (pending.timeoutId) globalThis.clearTimeout(pending.timeoutId);
538
+ pending.reject(reason);
539
+ }
540
+ this.pendingMessages.clear();
541
+ }
542
+ async init() {
543
+ if (this.initPromise) return this.initPromise;
544
+ if (this.isInitializing) {
545
+ await new Promise((resolve) => globalThis.setTimeout(resolve, 100));
546
+ return this.init();
547
+ }
548
+ this.isInitializing = true;
549
+ this.initPromise = this.sendMessage("init", void 0, INIT_TIMEOUT_MS2);
550
+ try {
551
+ return await this.initPromise;
552
+ } catch (error) {
553
+ this.initPromise = null;
554
+ throw error;
555
+ } finally {
556
+ this.isInitializing = false;
557
+ }
558
+ }
559
+ async executeWithTracing(code, functionName, inputs, options, executionStyle = "function", language = "javascript") {
560
+ await this.init();
561
+ return this.executeWithTimeout(
562
+ () => this.sendMessage(
563
+ "execute-with-tracing",
564
+ {
565
+ code,
566
+ functionName,
567
+ inputs,
568
+ options,
569
+ executionStyle,
570
+ language
571
+ },
572
+ TRACING_TIMEOUT_MS2 + 2e3
573
+ ),
574
+ TRACING_TIMEOUT_MS2
575
+ );
576
+ }
577
+ async executeCode(code, functionName, inputs, executionStyle = "function", language = "javascript") {
578
+ await this.init();
579
+ return this.executeWithTimeout(
580
+ () => this.sendMessage(
581
+ "execute-code",
582
+ {
583
+ code,
584
+ functionName,
585
+ inputs,
586
+ executionStyle,
587
+ language
588
+ },
589
+ EXECUTION_TIMEOUT_MS2 + 2e3
590
+ ),
591
+ EXECUTION_TIMEOUT_MS2
592
+ );
593
+ }
594
+ async executeCodeInterviewMode(code, functionName, inputs, executionStyle = "function", language = "javascript") {
595
+ await this.init();
596
+ try {
597
+ const result = await this.executeWithTimeout(
598
+ () => this.sendMessage(
599
+ "execute-code-interview",
600
+ {
601
+ code,
602
+ functionName,
603
+ inputs,
604
+ executionStyle,
605
+ language
606
+ },
607
+ INTERVIEW_MODE_TIMEOUT_MS2 + 2e3
608
+ ),
609
+ INTERVIEW_MODE_TIMEOUT_MS2
610
+ );
611
+ if (!result.success && result.error) {
612
+ const normalized = result.error.toLowerCase();
613
+ const isTimeoutOrResourceLimit = normalized.includes("timed out") || normalized.includes("infinite loop") || normalized.includes("line-limit") || normalized.includes("single-line-limit") || normalized.includes("recursion-limit") || normalized.includes("trace-limit") || normalized.includes("line events") || normalized.includes("trace steps") || normalized.includes("call depth");
614
+ if (isTimeoutOrResourceLimit) {
615
+ return {
616
+ success: false,
617
+ output: null,
618
+ error: "Time Limit Exceeded",
619
+ consoleOutput: result.consoleOutput ?? []
620
+ };
621
+ }
622
+ }
623
+ return result;
624
+ } catch {
625
+ return {
626
+ success: false,
627
+ output: null,
628
+ error: "Time Limit Exceeded",
629
+ consoleOutput: []
630
+ };
631
+ }
632
+ }
633
+ terminate() {
634
+ this.terminateAndReset();
635
+ }
636
+ };
637
+ function isJavaScriptWorkerSupported() {
638
+ return typeof Worker !== "undefined";
639
+ }
640
+ // Annotate the CommonJS export names for ESM import in node:
641
+ 0 && (module.exports = {
642
+ JavaScriptWorkerClient,
643
+ PyodideWorkerClient,
644
+ isJavaScriptWorkerSupported,
645
+ isWorkerSupported
646
+ });
647
+ //# sourceMappingURL=browser.cjs.map