taist 0.1.13 → 0.1.15

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.
@@ -5,22 +5,35 @@
5
5
  * across function calls without explicit parameter passing.
6
6
  *
7
7
  * This is the same approach used by OpenTelemetry, Datadog, and other APM tools.
8
+ *
9
+ * IMPORTANT: Uses globalThis to share context across bundled modules.
10
+ * Without this, each bundle would have its own AsyncLocalStorage instance,
11
+ * breaking context propagation across bundle boundaries.
8
12
  */
9
13
 
10
14
  import { AsyncLocalStorage } from 'async_hooks';
11
15
 
12
- // Global storage for trace context
13
- export const traceContext = new AsyncLocalStorage();
16
+ // Use globalThis to share context across all bundles/modules
17
+ // This ensures trace context propagates even when code is bundled separately
18
+ const TAIST_CONTEXT_KEY = '__taist_trace_context__';
19
+ const TAIST_COUNTER_KEY = '__taist_id_counter__';
20
+
21
+ if (!globalThis[TAIST_CONTEXT_KEY]) {
22
+ globalThis[TAIST_CONTEXT_KEY] = new AsyncLocalStorage();
23
+ }
24
+ if (globalThis[TAIST_COUNTER_KEY] === undefined) {
25
+ globalThis[TAIST_COUNTER_KEY] = 0;
26
+ }
14
27
 
15
- // Counter for generating unique IDs
16
- let idCounter = 0;
28
+ // Global storage for trace context (shared via globalThis)
29
+ export const traceContext = globalThis[TAIST_CONTEXT_KEY];
17
30
 
18
31
  /**
19
32
  * Generate a unique trace/span ID
20
33
  * @returns {string} Unique identifier
21
34
  */
22
35
  export function generateId() {
23
- return `__${++idCounter}_${Date.now()}`;
36
+ return `__${++globalThis[TAIST_COUNTER_KEY]}_${Date.now()}`;
24
37
  }
25
38
 
26
39
  /**
@@ -76,5 +89,5 @@ export function startTrace(fn) {
76
89
  * Reset the ID counter (for testing)
77
90
  */
78
91
  export function resetIdCounter() {
79
- idCounter = 0;
92
+ globalThis[TAIST_COUNTER_KEY] = 0;
80
93
  }
@@ -21,6 +21,10 @@ export class TraceReporter extends EventEmitter {
21
21
  this.flushInterval = options.flushInterval || 1000;
22
22
  this.maxRetries = options.maxRetries || 3;
23
23
  this.retryDelay = options.retryDelay || 100;
24
+ // Immediate flush mode - sends each trace immediately instead of buffering
25
+ // This is the default to prevent trace loss on process exit
26
+ // Set TAIST_BUFFER_TRACES=true to enable batching for high-throughput scenarios
27
+ this.flushImmediate = options.flushImmediate ?? (process.env.TAIST_BUFFER_TRACES !== 'true');
24
28
 
25
29
  this.buffer = [];
26
30
  this.socket = null;
@@ -32,7 +36,7 @@ export class TraceReporter extends EventEmitter {
32
36
  this.closed = false;
33
37
  this.shuttingDown = false; // Prevents race between shutdown signal and SIGTERM
34
38
 
35
- logger.debug("[reporter] Created with socketPath:", this.socketPath);
39
+ logger.debug("[reporter] Created with socketPath:", this.socketPath, "flushImmediate:", this.flushImmediate);
36
40
 
37
41
  // Auto-setup if socket path is available
38
42
  if (this.socketPath) {
@@ -254,8 +258,11 @@ export class TraceReporter extends EventEmitter {
254
258
  });
255
259
  }
256
260
 
257
- // Auto-flush when batch size reached
258
- if (this.buffer.length >= this.batchSize) {
261
+ // Flush immediately if configured (for spawned processes with unpredictable exit)
262
+ // Otherwise auto-flush when batch size reached
263
+ if (this.flushImmediate) {
264
+ this.flush().catch(() => {});
265
+ } else if (this.buffer.length >= this.batchSize) {
259
266
  this.flush().catch(() => {});
260
267
  }
261
268
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "taist",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "Token-Optimized Testing Framework for AI-Assisted Development",
5
5
  "main": "index.js",
6
6
  "type": "module",