footprintjs 4.7.0 → 4.8.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.
@@ -2,16 +2,29 @@
2
2
  * KeyedRecorder<T> — base class for Map-based recorders keyed by runtimeStageId.
3
3
  *
4
4
  * Provides typed key-value storage with O(1) lookup, insertion-ordered iteration,
5
- * and common accessors. Recorder implementations extend this and call store()
6
- * from their event hooks.
5
+ * and three standard operations on auto-collected traversal data:
6
+ *
7
+ * - **Translate** (raw): `getByKey(id)` — per-step value
8
+ * - **Accumulate** (progressive): `accumulate(fn, initial, keys?)` — running total up to a point
9
+ * - **Aggregate** (summary): `aggregate(fn, initial)` — reduce all entries
10
+ *
11
+ * Data is automatically collected during the single DFS traversal.
12
+ * The consumer chooses the operation at read time.
7
13
  *
8
14
  * @example
9
15
  * ```typescript
10
16
  * class TokenRecorder extends KeyedRecorder<LLMCallEntry> {
11
- * onLLMCall(event: LLMCallEvent) {
12
- * this.store(event.runtimeStageId, { model: event.model, ... });
13
- * }
14
- * getStats() { return aggregate(this.values()); }
17
+ * readonly id = 'tokens';
18
+ * onLLMCall(event) { this.store(event.runtimeStageId, { tokens: event.usage }); }
19
+ *
20
+ * // Translate: per-step
21
+ * getForStep(id: string) { return this.getByKey(id); }
22
+ *
23
+ * // Aggregate: total
24
+ * getTotalTokens() { return this.aggregate((sum, e) => sum + e.tokens, 0); }
25
+ *
26
+ * // Accumulate: progressive up to slider position
27
+ * getTokensUpTo(keys: Set<string>) { return this.accumulate((sum, e) => sum + e.tokens, 0, keys); }
15
28
  * }
16
29
  * ```
17
30
  */
@@ -28,6 +41,16 @@ export declare abstract class KeyedRecorder<T> {
28
41
  values(): T[];
29
42
  /** Number of entries stored. */
30
43
  get size(): number;
44
+ /** Reduce ALL entries to a single value. For dashboards, totals, summaries. */
45
+ aggregate<R>(fn: (acc: R, entry: T, key: string) => R, initial: R): R;
46
+ /**
47
+ * Reduce entries, optionally filtered by a set of keys.
48
+ * For time-travel progressive view: pass the runtimeStageIds visible at the current slider position.
49
+ * Without keys, reduces all entries (same as aggregate).
50
+ */
51
+ accumulate<R>(fn: (acc: R, entry: T, key: string) => R, initial: R, keys?: ReadonlySet<string>): R;
52
+ /** Return entries whose keys are in the set, preserving insertion order. */
53
+ filterByKeys(keys: ReadonlySet<string>): T[];
31
54
  /** Clear all stored data. Called by executor before each run(). */
32
55
  clear(): void;
33
56
  }
@@ -1,89 +1,96 @@
1
1
  /**
2
- * MetricRecorder — Production-focused recorder for timing and execution counts.
2
+ * MetricRecorder — per-step timing and execution counts, keyed by runtimeStageId.
3
3
  *
4
- * Tracks read/write/commit counts per stage and measures stage execution duration.
5
- *
6
- * Each instance gets a unique auto-increment ID (`metrics-1`, `metrics-2`, ...),
7
- * so multiple recorders with different configs coexist. Pass an explicit ID to
8
- * override a specific instance (e.g., a framework-attached recorder).
4
+ * Stores per-invocation data during traversal. Aggregated views computed on read.
5
+ * Extends KeyedRecorder<StepMetrics> for O(1) lookup and standard operations.
9
6
  *
10
7
  * @example
11
8
  * ```typescript
12
- * // Track all stages (default)
13
- * executor.attachRecorder(new MetricRecorder());
9
+ * const metric = new MetricRecorder();
10
+ * executor.attachRecorder(metric);
11
+ * await executor.run();
14
12
  *
15
- * // Track only LLM-related stages
16
- * executor.attachRecorder(new MetricRecorder({
17
- * stageFilter: (name) => ['CallLLM', 'ParseResponse'].includes(name),
18
- * }));
13
+ * // Per-step (time-travel):
14
+ * metric.getByKey('call-llm#5'); // { stageName, readCount, writeCount, duration }
19
15
  *
20
- * // Two recorders: one for LLM timing, one for everything else
21
- * executor.attachRecorder(new MetricRecorder({
22
- * stageFilter: (name) => name === 'CallLLM',
23
- * }));
24
- * executor.attachRecorder(new MetricRecorder({
25
- * stageFilter: (name) => name !== 'CallLLM',
26
- * }));
16
+ * // Aggregated (backward compat):
17
+ * metric.getMetrics(); // { totalDuration, totalReads, stageMetrics: Map<stageName, aggregated> }
27
18
  *
28
- * // Override a framework-attached recorder by passing its well-known ID
29
- * executor.attachRecorder(new MetricRecorder({ id: 'metrics' }));
19
+ * // Progressive (slider):
20
+ * metric.accumulate((sum, m) => sum + m.duration, 0, visibleKeys);
30
21
  * ```
31
22
  */
23
+ import { KeyedRecorder } from '../../recorder/KeyedRecorder.js';
32
24
  import type { CommitEvent, PauseEvent, ReadEvent, Recorder, StageEvent, WriteEvent } from '../types.js';
33
- export interface StageMetrics {
25
+ /** Per-invocation metrics for a single execution step. */
26
+ export interface StepMetrics {
27
+ /** Human-readable stage name. */
34
28
  stageName: string;
29
+ /** Number of scope reads during this invocation. */
35
30
  readCount: number;
31
+ /** Number of scope writes during this invocation. */
36
32
  writeCount: number;
33
+ /** Number of commits during this invocation. */
37
34
  commitCount: number;
35
+ /** Number of pauses during this invocation. */
38
36
  pauseCount: number;
39
- totalDuration: number;
40
- invocationCount: number;
37
+ /** Duration in ms for this invocation. */
38
+ duration: number;
41
39
  }
40
+ /** Aggregated metrics across all invocations (backward compatible). */
42
41
  export interface AggregatedMetrics {
43
42
  totalDuration: number;
44
43
  totalReads: number;
45
44
  totalWrites: number;
46
45
  totalCommits: number;
47
46
  totalPauses: number;
47
+ /** Aggregated by stageName — sums across loop invocations. */
48
48
  stageMetrics: Map<string, StageMetrics>;
49
49
  }
50
- /** Options for MetricRecorder. All fields are optional. */
50
+ /** Aggregated per-stageName (backward compatible with pre-runtimeStageId API). */
51
+ export interface StageMetrics {
52
+ stageName: string;
53
+ readCount: number;
54
+ writeCount: number;
55
+ commitCount: number;
56
+ pauseCount: number;
57
+ totalDuration: number;
58
+ invocationCount: number;
59
+ }
60
+ /** Options for MetricRecorder. */
51
61
  export interface MetricRecorderOptions {
52
62
  /** Recorder ID. Defaults to auto-increment (`metrics-1`, `metrics-2`, ...). */
53
63
  id?: string;
54
- /**
55
- * Filter which stages are recorded. Return `true` to record, `false` to skip.
56
- * When omitted, all stages are recorded.
57
- *
58
- * @example
59
- * ```typescript
60
- * // Only track stages that start with "Call"
61
- * stageFilter: (name) => name.startsWith('Call')
62
- * ```
63
- */
64
+ /** Filter which stages are recorded. Return `true` to record, `false` to skip. */
64
65
  stageFilter?: (stageName: string) => boolean;
65
66
  }
66
- export declare class MetricRecorder implements Recorder {
67
+ export declare class MetricRecorder extends KeyedRecorder<StepMetrics> implements Recorder {
67
68
  private static _counter;
68
69
  readonly id: string;
69
- private metrics;
70
70
  private stageStartTimes;
71
+ private currentRuntimeStageId;
71
72
  private stageFilter?;
72
73
  constructor(idOrOptions?: string | MetricRecorderOptions);
73
74
  private shouldRecord;
75
+ /** Get or create the StepMetrics for the current stage. */
76
+ private current;
77
+ onStageStart(event: StageEvent): void;
74
78
  onRead(event: ReadEvent): void;
75
79
  onWrite(event: WriteEvent): void;
76
80
  onCommit(event: CommitEvent): void;
77
81
  onPause(event: PauseEvent): void;
78
- onStageStart(event: StageEvent): void;
79
82
  onStageEnd(event: StageEvent): void;
83
+ /** Aggregated metrics — computes totals on the fly from per-step data (backward compatible). */
80
84
  getMetrics(): AggregatedMetrics;
85
+ /** Get aggregated metrics for a specific stage name (backward compatible). */
81
86
  getStageMetrics(stageName: string): StageMetrics | undefined;
87
+ /** Snapshot for serialization (backward compatible format). */
82
88
  toSnapshot(): {
83
89
  name: string;
84
90
  data: unknown;
85
91
  };
86
- reset(): void;
92
+ /** Clear all state — called by executor before each run(). */
87
93
  clear(): void;
88
- private getOrCreateStageMetrics;
94
+ /** Alias for clear() (backward compat). */
95
+ reset(): void;
89
96
  }
@@ -1,5 +1,5 @@
1
1
  export type { CommitEvent, ErrorEvent, ReadEvent, Recorder, RecorderContext, StageEvent, WriteEvent, } from '../types.js';
2
2
  export type { DebugEntry, DebugRecorderOptions, DebugVerbosity } from './DebugRecorder.js';
3
3
  export { DebugRecorder } from './DebugRecorder.js';
4
- export type { AggregatedMetrics, StageMetrics } from './MetricRecorder.js';
4
+ export type { AggregatedMetrics, StageMetrics, StepMetrics } from './MetricRecorder.js';
5
5
  export { MetricRecorder } from './MetricRecorder.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "footprintjs",
3
- "version": "4.7.0",
3
+ "version": "4.8.0",
4
4
  "description": "Explainable backend flows — automatic causal traces, decision evidence, and MCP tool generation for AI agents",
5
5
  "license": "MIT",
6
6
  "author": "Sanjay Krishna Anbalagan",