@aztec/simulator 5.0.0-nightly.20260616 → 5.0.0-nightly.20260618

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.
package/dest/client.d.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  export * from './private/acvm/index.js';
2
2
  export { WASMSimulator } from './private/acvm_wasm.js';
3
- export { SimulatorRecorderWrapper } from './private/circuit_recording/simulator_recorder_wrapper.js';
4
- export { MemoryCircuitRecorder } from './private/circuit_recording/memory_circuit_recorder.js';
5
3
  export { type CircuitSimulator, type DecodedError } from './private/circuit_simulator.js';
6
4
  export * from './common/index.js';
7
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xpZW50LmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY2xpZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMseUJBQXlCLENBQUM7QUFDeEMsT0FBTyxFQUFFLGFBQWEsRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBQ3ZELE9BQU8sRUFBRSx3QkFBd0IsRUFBRSxNQUFNLDJEQUEyRCxDQUFDO0FBQ3JHLE9BQU8sRUFBRSxxQkFBcUIsRUFBRSxNQUFNLHdEQUF3RCxDQUFDO0FBQy9GLE9BQU8sRUFBRSxLQUFLLGdCQUFnQixFQUFFLEtBQUssWUFBWSxFQUFFLE1BQU0sZ0NBQWdDLENBQUM7QUFDMUYsY0FBYyxtQkFBbUIsQ0FBQyJ9
5
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xpZW50LmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY2xpZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMseUJBQXlCLENBQUM7QUFDeEMsT0FBTyxFQUFFLGFBQWEsRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBQ3ZELE9BQU8sRUFBRSxLQUFLLGdCQUFnQixFQUFFLEtBQUssWUFBWSxFQUFFLE1BQU0sZ0NBQWdDLENBQUM7QUFDMUYsY0FBYyxtQkFBbUIsQ0FBQyJ9
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2DAA2D,CAAC;AACrG,OAAO,EAAE,qBAAqB,EAAE,MAAM,wDAAwD,CAAC;AAC/F,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAC1F,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAC1F,cAAc,mBAAmB,CAAC"}
package/dest/client.js CHANGED
@@ -1,5 +1,3 @@
1
1
  export * from './private/acvm/index.js';
2
2
  export { WASMSimulator } from './private/acvm_wasm.js';
3
- export { SimulatorRecorderWrapper } from './private/circuit_recording/simulator_recorder_wrapper.js';
4
- export { MemoryCircuitRecorder } from './private/circuit_recording/memory_circuit_recorder.js';
5
3
  export * from './common/index.js';
@@ -21,10 +21,22 @@ export declare class CircuitRecording {
21
21
  constructor(circuitName: string, functionName: string, bytecodeSHA512Hash: string, inputs: Record<string, string>);
22
22
  setParent(recording?: CircuitRecording): void;
23
23
  }
24
+ /** Inputs needed to open a recording for a single circuit execution. */
25
+ export type RecordingMetadata = {
26
+ input: ACVMWitness;
27
+ bytecode: Buffer;
28
+ circuitName: string;
29
+ functionName: string;
30
+ };
24
31
  /**
25
32
  * Class responsible for recording circuit inputs necessary to replay the circuit. These inputs are the initial witness
26
33
  * map and the oracle calls made during the circuit execution/witness generation.
27
34
  *
35
+ * The active recording for an execution lives in `AsyncLocalStorage`, so each (possibly nested) circuit execution owns
36
+ * its own recording and concurrent or re-entrant executions cannot corrupt one another's state. Nested executions
37
+ * (`aztec_prv_callPrivateFunction`, utility calls) re-enter {@link record}, which links the child to the recording
38
+ * active in the enclosing async context and lets ALS restore the parent automatically when the child completes.
39
+ *
28
40
  * Example recording object:
29
41
  * ```json
30
42
  * {
@@ -69,20 +81,21 @@ export declare class CircuitRecording {
69
81
  export declare class CircuitRecorder {
70
82
  #private;
71
83
  protected readonly logger: Logger;
72
- protected recording?: CircuitRecording;
73
- private stackDepth;
74
- private newCircuit;
75
84
  protected constructor(loggerOrBindings?: Logger | LoggerBindings);
76
85
  /**
77
- * Initializes a new circuit recording session.
78
- * @param recordDir - Directory to store the recording
79
- * @param input - Circuit input witness
80
- * @param circuitBytecode - Compiled circuit bytecode
81
- * @param circuitName - Name of the circuit
82
- * @param functionName - Name of the circuit function (defaults to 'main'). This is meaningful only for
83
- * contracts as protocol circuits artifacts always contain a single entrypoint function called 'main'.
86
+ * Records a single circuit execution. Opens a recording for the circuit (linked as a child of the recording active
87
+ * in the current async context, if any), runs `fn` within that recording's context, and finalizes it. The recording
88
+ * is returned alongside the result so callers can derive per-circuit stats (e.g. oracle timings).
89
+ *
90
+ * Recorder bookkeeping never alters execution: if `fn` throws, the error is attached to the recording and re-thrown
91
+ * unchanged.
92
+ * @param metadata - Identifies the circuit and its initial witness.
93
+ * @param fn - Runs the circuit execution; its oracle calls are recorded into this recording.
84
94
  */
85
- start(input: ACVMWitness, circuitBytecode: Buffer, circuitName: string, functionName: string): Promise<void>;
95
+ record<T>(metadata: RecordingMetadata, fn: () => Promise<T>): Promise<{
96
+ result: T;
97
+ recording: CircuitRecording;
98
+ }>;
86
99
  /**
87
100
  * Wraps a callback to record all oracle/foreign calls.
88
101
  * @param callback - The original callback to wrap, either a user circuit callback or protocol circuit callback.
@@ -90,20 +103,20 @@ export declare class CircuitRecorder {
90
103
  */
91
104
  wrapCallback(callback: ACIRCallback | ForeignCallHandler | undefined): ACIRCallback | ForeignCallHandler | undefined;
92
105
  /**
93
- * Records a single oracle/foreign call with its inputs and outputs.
106
+ * Records a single oracle/foreign call with its inputs and outputs against the recording active in the current
107
+ * async context.
94
108
  * @param name - Name of the call
95
109
  * @param inputs - Input arguments
96
110
  * @param outputs - Output results
97
111
  */
98
- recordCall(name: string, inputs: unknown[], outputs: unknown, time: number, stackDepth: number): Promise<OracleCall>;
99
- /**
100
- * Finalizes the recording by resetting the state and returning the recording object.
101
- */
102
- finish(): Promise<CircuitRecording>;
103
- /**
104
- * Finalizes the recording by resetting the state and returning the recording object with an attached error.
105
- * @param error - The error that occurred during circuit execution
106
- */
107
- finishWithError(error: unknown): Promise<CircuitRecording>;
112
+ recordCall(name: string, inputs: unknown[], outputs: unknown, time: number): Promise<OracleCall>;
113
+ /** The recording active in the current async context, if any. */
114
+ protected currentRecording(): CircuitRecording | undefined;
115
+ /** Hook invoked when a recording opens, within the recording's context. Overridden to persist recordings. */
116
+ protected onStart(_recording: CircuitRecording): Promise<void>;
117
+ /** Hook invoked when a recording completes successfully, within the recording's context. */
118
+ protected onFinish(_recording: CircuitRecording): Promise<void>;
119
+ /** Hook invoked when a recording's execution throws, within the recording's context. */
120
+ protected onError(_recording: CircuitRecording, _error: unknown): Promise<void>;
108
121
  }
109
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2lyY3VpdF9yZWNvcmRlci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL3ByaXZhdGUvY2lyY3VpdF9yZWNvcmRpbmcvY2lyY3VpdF9yZWNvcmRlci50cyJdLCJuYW1lcyI6W10sIm1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AAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAa/F;IAED,iEAAiE;IACjE,SAAS,CAAC,gBAAgB,IAAI,gBAAgB,GAAG,SAAS,CAEzD;IAED,6GAA6G;IAC7G,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAE7D;IAED,4FAA4F;IAC5F,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAE9D;IAED,wFAAwF;IACxF,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,gBAAgB,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAE9E;CACF"}
@@ -1,6 +1,7 @@
1
1
  import { sha512 } from '@aztec/foundation/crypto/sha512';
2
2
  import { resolveLogger } from '@aztec/foundation/log';
3
3
  import { Timer } from '@aztec/foundation/timer';
4
+ import { AsyncLocalStorage } from 'node:async_hooks';
4
5
  export class CircuitRecording {
5
6
  circuitName;
6
7
  functionName;
@@ -26,6 +27,11 @@ export class CircuitRecording {
26
27
  * Class responsible for recording circuit inputs necessary to replay the circuit. These inputs are the initial witness
27
28
  * map and the oracle calls made during the circuit execution/witness generation.
28
29
  *
30
+ * The active recording for an execution lives in `AsyncLocalStorage`, so each (possibly nested) circuit execution owns
31
+ * its own recording and concurrent or re-entrant executions cannot corrupt one another's state. Nested executions
32
+ * (`aztec_prv_callPrivateFunction`, utility calls) re-enter {@link record}, which links the child to the recording
33
+ * active in the enclosing async context and lets ALS restore the parent automatically when the child completes.
34
+ *
29
35
  * Example recording object:
30
36
  * ```json
31
37
  * {
@@ -68,27 +74,38 @@ export class CircuitRecording {
68
74
  * ```
69
75
  */ export class CircuitRecorder {
70
76
  logger;
71
- recording;
72
- stackDepth = 0;
73
- newCircuit = true;
77
+ #recordings = new AsyncLocalStorage();
74
78
  constructor(loggerOrBindings){
75
79
  this.logger = resolveLogger('simulator:acvm:recording', loggerOrBindings);
76
80
  }
77
81
  /**
78
- * Initializes a new circuit recording session.
79
- * @param recordDir - Directory to store the recording
80
- * @param input - Circuit input witness
81
- * @param circuitBytecode - Compiled circuit bytecode
82
- * @param circuitName - Name of the circuit
83
- * @param functionName - Name of the circuit function (defaults to 'main'). This is meaningful only for
84
- * contracts as protocol circuits artifacts always contain a single entrypoint function called 'main'.
85
- */ start(input, circuitBytecode, circuitName, functionName) {
86
- if (this.newCircuit) {
87
- const parentRef = this.recording;
88
- this.recording = new CircuitRecording(circuitName, functionName, sha512(circuitBytecode).toString('hex'), Object.fromEntries(input));
89
- this.recording.setParent(parentRef);
90
- }
91
- return Promise.resolve();
82
+ * Records a single circuit execution. Opens a recording for the circuit (linked as a child of the recording active
83
+ * in the current async context, if any), runs `fn` within that recording's context, and finalizes it. The recording
84
+ * is returned alongside the result so callers can derive per-circuit stats (e.g. oracle timings).
85
+ *
86
+ * Recorder bookkeeping never alters execution: if `fn` throws, the error is attached to the recording and re-thrown
87
+ * unchanged.
88
+ * @param metadata - Identifies the circuit and its initial witness.
89
+ * @param fn - Runs the circuit execution; its oracle calls are recorded into this recording.
90
+ */ record(metadata, fn) {
91
+ const parent = this.#recordings.getStore();
92
+ const recording = new CircuitRecording(metadata.circuitName, metadata.functionName, sha512(metadata.bytecode).toString('hex'), Object.fromEntries(metadata.input));
93
+ recording.setParent(parent);
94
+ return this.#recordings.run(recording, async ()=>{
95
+ await this.onStart(recording);
96
+ try {
97
+ const result = await fn();
98
+ await this.onFinish(recording);
99
+ return {
100
+ result,
101
+ recording
102
+ };
103
+ } catch (error) {
104
+ recording.error = JSON.stringify(error);
105
+ await this.onError(recording, error);
106
+ throw error;
107
+ }
108
+ });
92
109
  }
93
110
  /**
94
111
  * Wraps a callback to record all oracle/foreign calls.
@@ -109,7 +126,9 @@ export class CircuitRecording {
109
126
  return typeof callback === 'object' && callback !== null && !('call' in callback);
110
127
  }
111
128
  /**
112
- * Wraps a user circuit callback to record all oracle calls.
129
+ * Wraps a user circuit callback to record all oracle calls. A nested circuit entered via an oracle (e.g.
130
+ * `aztec_prv_callPrivateFunction`) re-enters {@link record}, so its own oracle calls land on the child recording and
131
+ * this circuit's calls (including the entering oracle call itself) land on this recording once the child completes.
113
132
  * @param callback - The original circuit callback.
114
133
  * @returns A wrapped callback that records all oracle interactions which is to be provided to the ACVM.
115
134
  */ #wrapUserCircuitCallback(callback) {
@@ -120,37 +139,16 @@ export class CircuitRecording {
120
139
  if (!fn || typeof fn !== 'function') {
121
140
  throw new Error(`Oracle method ${name} not found when setting up recording callback`);
122
141
  }
123
- const isExternalCall = name === 'aztec_prv_callPrivateFunction';
124
142
  recordingCallback[name] = (...args)=>{
125
143
  const timer = new Timer();
126
- // If we're entering another circuit via `aztec_prv_callPrivateFunction`, we increase the stack depth and set the
127
- // newCircuit variable to ensure we are creating a new recording object.
128
- if (isExternalCall) {
129
- this.stackDepth++;
130
- this.newCircuit = true;
131
- }
132
144
  const result = fn.call(callback, ...args);
133
145
  if (result instanceof Promise) {
134
146
  return result.then(async (r)=>{
135
- // Once we leave the nested circuit, we decrease the stack depth and set newCircuit to false
136
- // so that the parent circuit continues with its existing recording
137
- // Note: recording restoration is handled by finish()
138
- if (isExternalCall) {
139
- this.stackDepth--;
140
- this.newCircuit = false;
141
- }
142
- await this.recordCall(name, args, r, timer.ms(), this.stackDepth);
147
+ await this.recordCall(name, args, r, timer.ms());
143
148
  return r;
144
149
  });
145
150
  }
146
- // Once we leave the nested circuit, we decrease the stack depth and set newCircuit to false
147
- // so that the parent circuit continues with its existing recording
148
- // Note: recording restoration is handled by finish()
149
- if (isExternalCall) {
150
- this.stackDepth--;
151
- this.newCircuit = false;
152
- }
153
- void this.recordCall(name, args, result, timer.ms(), this.stackDepth);
151
+ void this.recordCall(name, args, result, timer.ms());
154
152
  return result;
155
153
  };
156
154
  }
@@ -164,49 +162,47 @@ export class CircuitRecording {
164
162
  return async (name, inputs)=>{
165
163
  const timer = new Timer();
166
164
  const result = await callback(name, inputs);
167
- await this.recordCall(name, inputs, result, timer.ms(), 0);
165
+ await this.recordCall(name, inputs, result, timer.ms());
168
166
  return result;
169
167
  };
170
168
  }
171
169
  /**
172
- * Records a single oracle/foreign call with its inputs and outputs.
170
+ * Records a single oracle/foreign call with its inputs and outputs against the recording active in the current
171
+ * async context.
173
172
  * @param name - Name of the call
174
173
  * @param inputs - Input arguments
175
174
  * @param outputs - Output results
176
- */ recordCall(name, inputs, outputs, time, stackDepth) {
175
+ */ recordCall(name, inputs, outputs, time) {
176
+ const recording = this.#recordings.getStore();
177
177
  const entry = {
178
178
  name,
179
179
  inputs,
180
180
  outputs,
181
181
  time,
182
- stackDepth
182
+ stackDepth: depthOf(recording)
183
183
  };
184
- this.recording.oracleCalls.push(entry);
184
+ // Outside any active recording context (e.g. a stray call after the scope closed, or a direct unit-test call)
185
+ // there is nowhere to record; return the entry without throwing into the execution path.
186
+ recording?.oracleCalls.push(entry);
185
187
  return Promise.resolve(entry);
186
188
  }
187
- /**
188
- * Finalizes the recording by resetting the state and returning the recording object.
189
- */ finish() {
190
- const result = this.recording;
191
- // If this is the top-level circuit recording, we reset the state for the next simulator call
192
- if (!result.parent) {
193
- this.newCircuit = true;
194
- this.recording = undefined;
195
- } else {
196
- // For nested circuits (utility calls, nested contract calls), restore to parent recording
197
- // Note: we don't set newCircuit=false here because:
198
- // - For privateCallPrivateFunction, the callback wrapper will set it to false
199
- // - For utility calls, we want newCircuit to remain true so the next circuit creates its own recording
200
- this.recording = result.parent;
201
- }
202
- return Promise.resolve(result);
189
+ /** The recording active in the current async context, if any. */ currentRecording() {
190
+ return this.#recordings.getStore();
203
191
  }
204
- /**
205
- * Finalizes the recording by resetting the state and returning the recording object with an attached error.
206
- * @param error - The error that occurred during circuit execution
207
- */ async finishWithError(error) {
208
- const result = await this.finish();
209
- result.error = JSON.stringify(error);
210
- return result;
192
+ /** Hook invoked when a recording opens, within the recording's context. Overridden to persist recordings. */ onStart(_recording) {
193
+ return Promise.resolve();
194
+ }
195
+ /** Hook invoked when a recording completes successfully, within the recording's context. */ onFinish(_recording) {
196
+ return Promise.resolve();
197
+ }
198
+ /** Hook invoked when a recording's execution throws, within the recording's context. */ onError(_recording, _error) {
199
+ return Promise.resolve();
200
+ }
201
+ }
202
+ /** Depth of a recording in the call tree: 0 for a top-level circuit, incremented per nested circuit. */ function depthOf(recording) {
203
+ let depth = 0;
204
+ for(let ancestor = recording?.parent; ancestor; ancestor = ancestor.parent){
205
+ depth++;
211
206
  }
207
+ return depth;
212
208
  }
@@ -1,32 +1,20 @@
1
1
  import type { Logger } from '@aztec/foundation/log';
2
- import type { ACVMWitness } from '../acvm/acvm_types.js';
3
2
  import { CircuitRecorder, type CircuitRecording } from './circuit_recorder.js';
4
3
  export declare class FileCircuitRecorder extends CircuitRecorder {
5
4
  #private;
6
5
  private readonly recordDir;
7
- recording?: CircuitRecording & {
8
- filePath: string;
9
- isFirstCall: boolean;
10
- };
11
6
  constructor(recordDir: string, logger?: Logger);
12
- start(input: ACVMWitness, circuitBytecode: Buffer, circuitName: string, functionName?: string): Promise<void>;
7
+ protected onStart(recording: CircuitRecording): Promise<void>;
13
8
  /**
14
9
  * Records a single oracle/foreign call with its inputs and outputs.
15
10
  * @param name - Name of the call
16
11
  * @param inputs - Input arguments
17
12
  * @param outputs - Output results
18
13
  */
19
- recordCall(name: string, inputs: unknown[], outputs: unknown, time: number, stackDepth: number): Promise<import("./circuit_recorder.js").OracleCall>;
20
- /**
21
- * Finalizes the recording file by adding closing brackets. Without calling this method, the recording file is
22
- * incomplete and it fails to parse.
23
- */
24
- finish(): Promise<CircuitRecording>;
25
- /**
26
- * Finalizes the recording file by adding the error and closing brackets. Without calling this method or `finish`,
27
- * the recording file is incomplete and it fails to parse.
28
- * @param error - The error that occurred during circuit execution
29
- */
30
- finishWithError(error: unknown): Promise<CircuitRecording>;
14
+ recordCall(name: string, inputs: unknown[], outputs: unknown, time: number): Promise<import("./circuit_recorder.js").OracleCall>;
15
+ /** Closes the recording file with the trailing brackets so the JSON parses. */
16
+ protected onFinish(recording: CircuitRecording): Promise<void>;
17
+ /** Closes the recording file with the execution error and trailing brackets so the JSON parses. */
18
+ protected onError(recording: CircuitRecording, error: unknown): Promise<void>;
31
19
  }
32
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmlsZV9jaXJjdWl0X3JlY29yZGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvcHJpdmF0ZS9jaXJjdWl0X3JlY29yZGluZy9maWxlX2NpcmN1aXRfcmVjb3JkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsTUFBTSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFLcEQsT0FBTyxLQUFLLEVBQUUsV0FBVyxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFDekQsT0FBTyxFQUFFLGVBQWUsRUFBRSxLQUFLLGdCQUFnQixFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFFL0UscUJBQWEsbUJBQW9CLFNBQVEsZUFBZTs7SUFJcEQsT0FBTyxDQUFDLFFBQVEsQ0FBQyxTQUFTO0lBSHBCLFNBQVMsQ0FBQyxFQUFFLGdCQUFnQixHQUFHO1FBQUUsUUFBUSxFQUFFLE1BQU0sQ0FBQztRQUFDLFdBQVcsRUFBRSxPQUFPLENBQUE7S0FBRSxDQUFDO0lBRWxGLFlBQ21CLFNBQVMsRUFBRSxNQUFNLEVBQ2xDLE1BQU0sQ0FBQyxFQUFFLE1BQU0sRUFHaEI7SUFFYyxLQUFLLENBQ2xCLEtBQUssRUFBRSxXQUFXLEVBQ2xCLGVBQWUsRUFBRSxNQUFNLEVBQ3ZCLFdBQVcsRUFBRSxNQUFNLEVBQ25CLFlBQVks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zs7OztPQUtHO0lBQ1ksVUFBVSxDQUFDLElBQUksRUFBRSxNQUFNLEVBQUUsTUFBTSxFQUFFLE9BQU8sRUFBRSxFQUFFLE9BQU8sRUFBRSxPQUFPLEVBQUUsSUFBSSxFQUFFLE1BQU0sdURBY3hGO0lBRUQsK0VBQStFO0lBQy9FLFVBQXlCLFFBQVEsQ0FBQyxTQUFTLEVBQUUsZ0JBQWdCLEdBQUcsT0FBTyxDQUFDLElBQUksQ0FBQyxDQVU1RTtJQUVELG1HQUFtRztJQUNuRyxVQUF5QixPQUFPLENBQUMsU0FBUyxFQUFFLGdCQUFnQixFQUFFLEtBQUssRUFBRSxPQUFPLEdBQUcsT0FBTyxDQUFDLElBQUksQ0FBQyxDQVkzRjtDQUNGIn0=
@@ -1 +1 @@
1
- {"version":3,"file":"file_circuit_recorder.d.ts","sourceRoot":"","sources":["../../../src/private/circuit_recording/file_circuit_recorder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAKpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE/E,qBAAa,mBAAoB,SAAQ,eAAe;;IAIpD,OAAO,CAAC,QAAQ,CAAC,SAAS;IAHpB,SAAS,CAAC,EAAE,gBAAgB,GAAG;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,OAAO,CAAA;KAAE,CAAC;IAElF,YACmB,SAAS,EAAE,MAAM,EAClC,MAAM,CAAC,EAAE,MAAM,EAGhB;IAEc,KAAK,CAClB,KAAK,EAAE,WAAW,EAClB,eAAe,EAAE,MAAM,EACvB,WAAW,EAAE,MAAM,EACnB,YAAY,GAAE,MAAe,iBAgC9B;IAqCD;;;;;OAKG;IACY,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,uDAU5G;IAED;;;OAGG;IACY,MAAM,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAWjD;IAED;;;;OAIG;IACY,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAaxE;CACF"}
1
+ {"version":3,"file":"file_circuit_recorder.d.ts","sourceRoot":"","sources":["../../../src/private/circuit_recording/file_circuit_recorder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAKpD,OAAO,EAAE,eAAe,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAK/E,qBAAa,mBAAoB,SAAQ,eAAe;;IAIpD,OAAO,CAAC,QAAQ,CAAC,SAAS;IAD5B,YACmB,SAAS,EAAE,MAAM,EAClC,MAAM,CAAC,EAAE,MAAM,EAGhB;IAED,UAAyB,OAAO,CAAC,SAAS,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CA6B3E;IAqCD;;;;;OAKG;IACY,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,uDAcxF;IAED,+EAA+E;IAC/E,UAAyB,QAAQ,CAAC,SAAS,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAU5E;IAED,mGAAmG;IACnG,UAAyB,OAAO,CAAC,SAAS,EAAE,gBAAgB,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAY3F;CACF"}
@@ -3,17 +3,15 @@ import path from 'path';
3
3
  import { CircuitRecorder } from './circuit_recorder.js';
4
4
  export class FileCircuitRecorder extends CircuitRecorder {
5
5
  recordDir;
6
+ #fileState;
6
7
  constructor(recordDir, logger){
7
- super(logger), this.recordDir = recordDir;
8
+ super(logger), this.recordDir = recordDir, this.#fileState = new WeakMap();
8
9
  }
9
- async start(input, circuitBytecode, circuitName, functionName = 'main') {
10
- await super.start(input, circuitBytecode, circuitName, functionName);
10
+ async onStart(recording) {
11
11
  const recordingStringWithoutClosingBracket = JSON.stringify({
12
- ...this.recording,
13
- isFirstCall: undefined,
12
+ ...recording,
14
13
  parent: undefined,
15
- oracleCalls: undefined,
16
- filePath: undefined
14
+ oracleCalls: undefined
17
15
  }, null, 2).slice(0, -2);
18
16
  try {
19
17
  // Check if the recording directory exists and is a directory
@@ -31,8 +29,11 @@ export class FileCircuitRecorder extends CircuitRecorder {
31
29
  throw err;
32
30
  }
33
31
  }
34
- this.recording.isFirstCall = true;
35
- this.recording.filePath = await FileCircuitRecorder.#computeFilePathAndStoreInitialRecording(this.recordDir, this.recording.circuitName, this.recording.functionName, recordingStringWithoutClosingBracket);
32
+ const filePath = await FileCircuitRecorder.#computeFilePathAndStoreInitialRecording(this.recordDir, recording.circuitName, recording.functionName, recordingStringWithoutClosingBracket);
33
+ this.#fileState.set(recording, {
34
+ filePath,
35
+ isFirstCall: true
36
+ });
36
37
  }
37
38
  /**
38
39
  * Computes a unique file path for the recording by trying different counter values.
@@ -67,55 +68,50 @@ export class FileCircuitRecorder extends CircuitRecorder {
67
68
  * @param name - Name of the call
68
69
  * @param inputs - Input arguments
69
70
  * @param outputs - Output results
70
- */ async recordCall(name, inputs, outputs, time, stackDepth) {
71
- const entry = await super.recordCall(name, inputs, outputs, time, stackDepth);
72
- try {
73
- const prefix = this.recording.isFirstCall ? ' ' : ' ,';
74
- this.recording.isFirstCall = false;
75
- await fs.appendFile(this.recording.filePath, prefix + JSON.stringify(entry) + '\n');
76
- } catch (err) {
77
- this.logger.error('Failed to log circuit call', {
78
- error: err
79
- });
71
+ */ async recordCall(name, inputs, outputs, time) {
72
+ const entry = await super.recordCall(name, inputs, outputs, time);
73
+ const recording = this.currentRecording();
74
+ const state = recording && this.#fileState.get(recording);
75
+ if (state) {
76
+ try {
77
+ const prefix = state.isFirstCall ? ' ' : ' ,';
78
+ state.isFirstCall = false;
79
+ await fs.appendFile(state.filePath, prefix + JSON.stringify(entry) + '\n');
80
+ } catch (err) {
81
+ this.logger.error('Failed to log circuit call', {
82
+ error: err
83
+ });
84
+ }
80
85
  }
81
86
  return entry;
82
87
  }
83
- /**
84
- * Finalizes the recording file by adding closing brackets. Without calling this method, the recording file is
85
- * incomplete and it fails to parse.
86
- */ async finish() {
87
- // Finish sets the recording to undefined if we are at the topmost circuit,
88
- // so we save the current file path before that
89
- const filePath = this.recording.filePath;
90
- const result = await super.finish();
88
+ /** Closes the recording file with the trailing brackets so the JSON parses. */ async onFinish(recording) {
89
+ const state = this.#fileState.get(recording);
90
+ if (!state) {
91
+ return;
92
+ }
91
93
  try {
92
- await fs.appendFile(filePath, ' ]\n}\n');
94
+ await fs.appendFile(state.filePath, ' ]\n}\n');
93
95
  } catch (err) {
94
96
  this.logger.error('Failed to finalize recording file', {
95
97
  error: err
96
98
  });
97
99
  }
98
- return result;
99
100
  }
100
- /**
101
- * Finalizes the recording file by adding the error and closing brackets. Without calling this method or `finish`,
102
- * the recording file is incomplete and it fails to parse.
103
- * @param error - The error that occurred during circuit execution
104
- */ async finishWithError(error) {
105
- // Finish sets the recording to undefined if we are at the topmost circuit,
106
- // so we save the current file path before that
107
- const filePath = this.recording.filePath;
108
- const result = await super.finishWithError(error);
101
+ /** Closes the recording file with the execution error and trailing brackets so the JSON parses. */ async onError(recording, error) {
102
+ const state = this.#fileState.get(recording);
103
+ if (!state) {
104
+ return;
105
+ }
109
106
  try {
110
- await fs.appendFile(filePath, ' ],\n');
111
- await fs.appendFile(filePath, ` "error": ${JSON.stringify(error)}\n`);
112
- await fs.appendFile(filePath, '}\n');
107
+ await fs.appendFile(state.filePath, ' ],\n');
108
+ await fs.appendFile(state.filePath, ` "error": ${JSON.stringify(error)}\n`);
109
+ await fs.appendFile(state.filePath, '}\n');
113
110
  } catch (err) {
114
111
  this.logger.error('Failed to finalize recording file with error', {
115
112
  error: err
116
113
  });
117
114
  }
118
- return result;
119
115
  }
120
116
  }
121
117
  /**
@@ -18,4 +18,4 @@ export declare class SimulatorRecorderWrapper implements CircuitSimulator {
18
18
  executeProtocolCircuit(input: ACVMWitness, artifact: NoirCompiledCircuitWithName, callback: ForeignCallHandler | undefined): Promise<ACVMSuccess>;
19
19
  executeUserCircuit(input: ACVMWitness, artifact: FunctionArtifactWithContractName, callback: ACIRCallback): Promise<ACIRExecutionResult>;
20
20
  }
21
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2ltdWxhdG9yX3JlY29yZGVyX3dyYXBwZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9wcml2YXRlL2NpcmN1aXRfcmVjb3JkaW5nL3NpbXVsYXRvcl9yZWNvcmRlcl93cmFwcGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxFQUFFLGtCQUFrQixFQUFFLE1BQU0scUJBQXFCLENBQUM7QUFDOUQsT0FBTyxLQUFLLEVBQUUsZ0NBQWdDLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQUMxRSxPQUFPLEtBQUssRUFBRSwyQkFBMkIsRUFBRSxNQUFNLG9CQUFvQixDQUFDO0FBRXRFLE9BQU8sS0FBSyxFQUFFLFlBQVksRUFBcUIsbUJBQW1CLEVBQUUsTUFBTSxpQkFBaUIsQ0FBQztBQUM1RixPQUFPLEtBQUssRUFBRSxXQUFXLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUN6RCxPQUFPLEtBQUssRUFBRSxXQUFXLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQUNyRCxPQUFPLEtBQUssRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQ2hFLE9BQU8sS0FBSyxFQUFFLGVBQWUsRUFBRSx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B,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE7D;;;GAGG;AACH,qBAAa,wBAAyB,YAAW,gBAAgB;;IAE7D,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,QAAQ;IAFlB,YACU,SAAS,EAAE,gBAAgB,EAC3B,QAAQ,EAAE,eAAe,EAC/B;IAEJ,sBAAsB,CACpB,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,2BAA2B,EACrC,QAAQ,EAAE,kBAAkB,GAAG,SAAS,GACvC,OAAO,CAAC,WAAW,CAAC,CAWtB;IAED,kBAAkB,CAChB,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,gCAAgC,EAC1C,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC,mBAAmB,CAAC,CAS9B;CAkCF"}
@@ -16,21 +16,18 @@
16
16
  return this.#simulate((wrappedCallback)=>this.simulator.executeUserCircuit(input, artifact, wrappedCallback), input, artifact.bytecode, artifact.contractName, artifact.name, callback);
17
17
  }
18
18
  async #simulate(simulateFn, input, bytecode, contractName, functionName, callback) {
19
- // Start recording circuit execution
20
- await this.recorder.start(input, bytecode, contractName, functionName);
21
- // If callback was provided, we wrap it in a circuit recorder callback wrapper
19
+ // If a callback was provided, we wrap it so that its oracle calls are recorded. The wrapped callback reads the
20
+ // active recording lazily, so it picks up the recording opened by record() below.
22
21
  const wrappedCallback = this.recorder.wrapCallback(callback);
23
- let result;
24
- try {
25
- result = await simulateFn(wrappedCallback);
26
- } catch (error) {
27
- // If an error occurs, we finalize the recording file with the error
28
- await this.recorder.finishWithError(error);
29
- throw error;
30
- }
31
- // Witness generation is complete so we finish the circuit recorder
32
- const recording = await this.recorder.finish();
33
- result.oracles = recording.oracleCalls?.reduce((acc, { time, name })=>{
22
+ // record() opens a recording for this circuit, runs the simulation within it, and finalizes it. A simulation
23
+ // failure is re-thrown unchanged, so recorder bookkeeping never masks the underlying error.
24
+ const { result, recording } = await this.recorder.record({
25
+ input,
26
+ bytecode,
27
+ circuitName: contractName,
28
+ functionName
29
+ }, ()=>simulateFn(wrappedCallback));
30
+ result.oracles = recording.oracleCalls.reduce((acc, { time, name })=>{
34
31
  if (!acc[name]) {
35
32
  acc[name] = {
36
33
  times: []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/simulator",
3
- "version": "5.0.0-nightly.20260616",
3
+ "version": "5.0.0-nightly.20260618",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./server": "./dest/server.js",
@@ -64,26 +64,26 @@
64
64
  ]
65
65
  },
66
66
  "dependencies": {
67
- "@aztec/constants": "5.0.0-nightly.20260616",
68
- "@aztec/foundation": "5.0.0-nightly.20260616",
69
- "@aztec/native": "5.0.0-nightly.20260616",
70
- "@aztec/noir-acvm_js": "5.0.0-nightly.20260616",
71
- "@aztec/noir-noirc_abi": "5.0.0-nightly.20260616",
72
- "@aztec/noir-protocol-circuits-types": "5.0.0-nightly.20260616",
73
- "@aztec/noir-types": "5.0.0-nightly.20260616",
74
- "@aztec/protocol-contracts": "5.0.0-nightly.20260616",
75
- "@aztec/standard-contracts": "5.0.0-nightly.20260616",
76
- "@aztec/stdlib": "5.0.0-nightly.20260616",
77
- "@aztec/telemetry-client": "5.0.0-nightly.20260616",
78
- "@aztec/world-state": "5.0.0-nightly.20260616",
67
+ "@aztec/constants": "5.0.0-nightly.20260618",
68
+ "@aztec/foundation": "5.0.0-nightly.20260618",
69
+ "@aztec/native": "5.0.0-nightly.20260618",
70
+ "@aztec/noir-acvm_js": "5.0.0-nightly.20260618",
71
+ "@aztec/noir-noirc_abi": "5.0.0-nightly.20260618",
72
+ "@aztec/noir-protocol-circuits-types": "5.0.0-nightly.20260618",
73
+ "@aztec/noir-types": "5.0.0-nightly.20260618",
74
+ "@aztec/protocol-contracts": "5.0.0-nightly.20260618",
75
+ "@aztec/standard-contracts": "5.0.0-nightly.20260618",
76
+ "@aztec/stdlib": "5.0.0-nightly.20260618",
77
+ "@aztec/telemetry-client": "5.0.0-nightly.20260618",
78
+ "@aztec/world-state": "5.0.0-nightly.20260618",
79
79
  "lodash.clonedeep": "^4.5.0",
80
80
  "lodash.merge": "^4.6.2",
81
81
  "tslib": "^2.4.0"
82
82
  },
83
83
  "devDependencies": {
84
- "@aztec/kv-store": "5.0.0-nightly.20260616",
85
- "@aztec/noir-contracts.js": "5.0.0-nightly.20260616",
86
- "@aztec/noir-test-contracts.js": "5.0.0-nightly.20260616",
84
+ "@aztec/kv-store": "5.0.0-nightly.20260618",
85
+ "@aztec/noir-contracts.js": "5.0.0-nightly.20260618",
86
+ "@aztec/noir-test-contracts.js": "5.0.0-nightly.20260618",
87
87
  "@jest/globals": "^30.0.0",
88
88
  "@types/jest": "^30.0.0",
89
89
  "@types/lodash.clonedeep": "^4.5.7",
package/src/client.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  export * from './private/acvm/index.js';
2
2
  export { WASMSimulator } from './private/acvm_wasm.js';
3
- export { SimulatorRecorderWrapper } from './private/circuit_recording/simulator_recorder_wrapper.js';
4
- export { MemoryCircuitRecorder } from './private/circuit_recording/memory_circuit_recorder.js';
5
3
  export { type CircuitSimulator, type DecodedError } from './private/circuit_simulator.js';
6
4
  export * from './common/index.js';
@@ -3,6 +3,8 @@ import { type Logger, type LoggerBindings, resolveLogger } from '@aztec/foundati
3
3
  import { Timer } from '@aztec/foundation/timer';
4
4
  import type { ForeignCallHandler, ForeignCallInput, ForeignCallOutput } from '@aztec/noir-acvm_js';
5
5
 
6
+ import { AsyncLocalStorage } from 'node:async_hooks';
7
+
6
8
  import type { ACIRCallback } from '../acvm/acvm.js';
7
9
  import type { ACVMWitness } from '../acvm/acvm_types.js';
8
10
 
@@ -43,10 +45,23 @@ export class CircuitRecording {
43
45
  }
44
46
  }
45
47
 
48
+ /** Inputs needed to open a recording for a single circuit execution. */
49
+ export type RecordingMetadata = {
50
+ input: ACVMWitness;
51
+ bytecode: Buffer;
52
+ circuitName: string;
53
+ functionName: string;
54
+ };
55
+
46
56
  /**
47
57
  * Class responsible for recording circuit inputs necessary to replay the circuit. These inputs are the initial witness
48
58
  * map and the oracle calls made during the circuit execution/witness generation.
49
59
  *
60
+ * The active recording for an execution lives in `AsyncLocalStorage`, so each (possibly nested) circuit execution owns
61
+ * its own recording and concurrent or re-entrant executions cannot corrupt one another's state. Nested executions
62
+ * (`aztec_prv_callPrivateFunction`, utility calls) re-enter {@link record}, which links the child to the recording
63
+ * active in the enclosing async context and lets ALS restore the parent automatically when the child completes.
64
+ *
50
65
  * Example recording object:
51
66
  * ```json
52
67
  * {
@@ -91,37 +106,44 @@ export class CircuitRecording {
91
106
  export class CircuitRecorder {
92
107
  protected readonly logger: Logger;
93
108
 
94
- protected recording?: CircuitRecording;
95
-
96
- private stackDepth: number = 0;
97
- private newCircuit: boolean = true;
109
+ readonly #recordings = new AsyncLocalStorage<CircuitRecording>();
98
110
 
99
111
  protected constructor(loggerOrBindings?: Logger | LoggerBindings) {
100
112
  this.logger = resolveLogger('simulator:acvm:recording', loggerOrBindings);
101
113
  }
102
114
 
103
115
  /**
104
- * Initializes a new circuit recording session.
105
- * @param recordDir - Directory to store the recording
106
- * @param input - Circuit input witness
107
- * @param circuitBytecode - Compiled circuit bytecode
108
- * @param circuitName - Name of the circuit
109
- * @param functionName - Name of the circuit function (defaults to 'main'). This is meaningful only for
110
- * contracts as protocol circuits artifacts always contain a single entrypoint function called 'main'.
116
+ * Records a single circuit execution. Opens a recording for the circuit (linked as a child of the recording active
117
+ * in the current async context, if any), runs `fn` within that recording's context, and finalizes it. The recording
118
+ * is returned alongside the result so callers can derive per-circuit stats (e.g. oracle timings).
119
+ *
120
+ * Recorder bookkeeping never alters execution: if `fn` throws, the error is attached to the recording and re-thrown
121
+ * unchanged.
122
+ * @param metadata - Identifies the circuit and its initial witness.
123
+ * @param fn - Runs the circuit execution; its oracle calls are recorded into this recording.
111
124
  */
112
- start(input: ACVMWitness, circuitBytecode: Buffer, circuitName: string, functionName: string): Promise<void> {
113
- if (this.newCircuit) {
114
- const parentRef = this.recording;
115
- this.recording = new CircuitRecording(
116
- circuitName,
117
- functionName,
118
- sha512(circuitBytecode).toString('hex'),
119
- Object.fromEntries(input),
120
- );
121
- this.recording.setParent(parentRef);
122
- }
125
+ record<T>(metadata: RecordingMetadata, fn: () => Promise<T>): Promise<{ result: T; recording: CircuitRecording }> {
126
+ const parent = this.#recordings.getStore();
127
+ const recording = new CircuitRecording(
128
+ metadata.circuitName,
129
+ metadata.functionName,
130
+ sha512(metadata.bytecode).toString('hex'),
131
+ Object.fromEntries(metadata.input),
132
+ );
133
+ recording.setParent(parent);
123
134
 
124
- return Promise.resolve();
135
+ return this.#recordings.run(recording, async () => {
136
+ await this.onStart(recording);
137
+ try {
138
+ const result = await fn();
139
+ await this.onFinish(recording);
140
+ return { result, recording };
141
+ } catch (error) {
142
+ recording.error = JSON.stringify(error);
143
+ await this.onError(recording, error);
144
+ throw error;
145
+ }
146
+ });
125
147
  }
126
148
 
127
149
  /**
@@ -147,7 +169,9 @@ export class CircuitRecorder {
147
169
  }
148
170
 
149
171
  /**
150
- * Wraps a user circuit callback to record all oracle calls.
172
+ * Wraps a user circuit callback to record all oracle calls. A nested circuit entered via an oracle (e.g.
173
+ * `aztec_prv_callPrivateFunction`) re-enters {@link record}, so its own oracle calls land on the child recording and
174
+ * this circuit's calls (including the entering oracle call itself) land on this recording once the child completes.
151
175
  * @param callback - The original circuit callback.
152
176
  * @returns A wrapped callback that records all oracle interactions which is to be provided to the ACVM.
153
177
  */
@@ -161,38 +185,16 @@ export class CircuitRecorder {
161
185
  throw new Error(`Oracle method ${name} not found when setting up recording callback`);
162
186
  }
163
187
 
164
- const isExternalCall = (name as keyof ACIRCallback) === 'aztec_prv_callPrivateFunction';
165
-
166
188
  recordingCallback[name as keyof ACIRCallback] = (...args: ForeignCallInput[]): ReturnType<typeof fn> => {
167
189
  const timer = new Timer();
168
- // If we're entering another circuit via `aztec_prv_callPrivateFunction`, we increase the stack depth and set the
169
- // newCircuit variable to ensure we are creating a new recording object.
170
- if (isExternalCall) {
171
- this.stackDepth++;
172
- this.newCircuit = true;
173
- }
174
190
  const result = fn.call(callback, ...args);
175
191
  if (result instanceof Promise) {
176
192
  return result.then(async r => {
177
- // Once we leave the nested circuit, we decrease the stack depth and set newCircuit to false
178
- // so that the parent circuit continues with its existing recording
179
- // Note: recording restoration is handled by finish()
180
- if (isExternalCall) {
181
- this.stackDepth--;
182
- this.newCircuit = false;
183
- }
184
- await this.recordCall(name, args, r, timer.ms(), this.stackDepth);
193
+ await this.recordCall(name, args, r, timer.ms());
185
194
  return r;
186
195
  }) as ReturnType<typeof fn>;
187
196
  }
188
- // Once we leave the nested circuit, we decrease the stack depth and set newCircuit to false
189
- // so that the parent circuit continues with its existing recording
190
- // Note: recording restoration is handled by finish()
191
- if (isExternalCall) {
192
- this.stackDepth--;
193
- this.newCircuit = false;
194
- }
195
- void this.recordCall(name, args, result, timer.ms(), this.stackDepth);
197
+ void this.recordCall(name, args, result, timer.ms());
196
198
  return result;
197
199
  };
198
200
  }
@@ -209,55 +211,59 @@ export class CircuitRecorder {
209
211
  return async (name: string, inputs: ForeignCallInput[]): Promise<ForeignCallOutput[]> => {
210
212
  const timer = new Timer();
211
213
  const result = await callback(name, inputs);
212
- await this.recordCall(name, inputs, result, timer.ms(), 0);
214
+ await this.recordCall(name, inputs, result, timer.ms());
213
215
  return result;
214
216
  };
215
217
  }
216
218
 
217
219
  /**
218
- * Records a single oracle/foreign call with its inputs and outputs.
220
+ * Records a single oracle/foreign call with its inputs and outputs against the recording active in the current
221
+ * async context.
219
222
  * @param name - Name of the call
220
223
  * @param inputs - Input arguments
221
224
  * @param outputs - Output results
222
225
  */
223
- recordCall(name: string, inputs: unknown[], outputs: unknown, time: number, stackDepth: number): Promise<OracleCall> {
226
+ recordCall(name: string, inputs: unknown[], outputs: unknown, time: number): Promise<OracleCall> {
227
+ const recording = this.#recordings.getStore();
224
228
  const entry = {
225
229
  name,
226
230
  inputs,
227
231
  outputs,
228
232
  time,
229
- stackDepth,
233
+ stackDepth: depthOf(recording),
230
234
  };
231
- this.recording!.oracleCalls.push(entry);
235
+ // Outside any active recording context (e.g. a stray call after the scope closed, or a direct unit-test call)
236
+ // there is nowhere to record; return the entry without throwing into the execution path.
237
+ recording?.oracleCalls.push(entry);
232
238
  return Promise.resolve(entry);
233
239
  }
234
240
 
235
- /**
236
- * Finalizes the recording by resetting the state and returning the recording object.
237
- */
238
- finish(): Promise<CircuitRecording> {
239
- const result = this.recording;
240
- // If this is the top-level circuit recording, we reset the state for the next simulator call
241
- if (!result!.parent) {
242
- this.newCircuit = true;
243
- this.recording = undefined;
244
- } else {
245
- // For nested circuits (utility calls, nested contract calls), restore to parent recording
246
- // Note: we don't set newCircuit=false here because:
247
- // - For privateCallPrivateFunction, the callback wrapper will set it to false
248
- // - For utility calls, we want newCircuit to remain true so the next circuit creates its own recording
249
- this.recording = result!.parent;
250
- }
251
- return Promise.resolve(result!);
241
+ /** The recording active in the current async context, if any. */
242
+ protected currentRecording(): CircuitRecording | undefined {
243
+ return this.#recordings.getStore();
252
244
  }
253
245
 
254
- /**
255
- * Finalizes the recording by resetting the state and returning the recording object with an attached error.
256
- * @param error - The error that occurred during circuit execution
257
- */
258
- async finishWithError(error: unknown): Promise<CircuitRecording> {
259
- const result = await this.finish();
260
- result.error = JSON.stringify(error);
261
- return result;
246
+ /** Hook invoked when a recording opens, within the recording's context. Overridden to persist recordings. */
247
+ protected onStart(_recording: CircuitRecording): Promise<void> {
248
+ return Promise.resolve();
249
+ }
250
+
251
+ /** Hook invoked when a recording completes successfully, within the recording's context. */
252
+ protected onFinish(_recording: CircuitRecording): Promise<void> {
253
+ return Promise.resolve();
254
+ }
255
+
256
+ /** Hook invoked when a recording's execution throws, within the recording's context. */
257
+ protected onError(_recording: CircuitRecording, _error: unknown): Promise<void> {
258
+ return Promise.resolve();
259
+ }
260
+ }
261
+
262
+ /** Depth of a recording in the call tree: 0 for a top-level circuit, incremented per nested circuit. */
263
+ function depthOf(recording: CircuitRecording | undefined): number {
264
+ let depth = 0;
265
+ for (let ancestor = recording?.parent; ancestor; ancestor = ancestor.parent) {
266
+ depth++;
262
267
  }
268
+ return depth;
263
269
  }
@@ -3,11 +3,13 @@ import type { Logger } from '@aztec/foundation/log';
3
3
  import fs from 'fs/promises';
4
4
  import path from 'path';
5
5
 
6
- import type { ACVMWitness } from '../acvm/acvm_types.js';
7
6
  import { CircuitRecorder, type CircuitRecording } from './circuit_recorder.js';
8
7
 
8
+ /** Per-recording file state, keyed by recording so concurrent/nested executions don't share it. */
9
+ type RecordingFileState = { filePath: string; isFirstCall: boolean };
10
+
9
11
  export class FileCircuitRecorder extends CircuitRecorder {
10
- declare recording?: CircuitRecording & { filePath: string; isFirstCall: boolean };
12
+ readonly #fileState = new WeakMap<CircuitRecording, RecordingFileState>();
11
13
 
12
14
  constructor(
13
15
  private readonly recordDir: string,
@@ -16,16 +18,9 @@ export class FileCircuitRecorder extends CircuitRecorder {
16
18
  super(logger);
17
19
  }
18
20
 
19
- override async start(
20
- input: ACVMWitness,
21
- circuitBytecode: Buffer,
22
- circuitName: string,
23
- functionName: string = 'main',
24
- ) {
25
- await super.start(input, circuitBytecode, circuitName, functionName);
26
-
21
+ protected override async onStart(recording: CircuitRecording): Promise<void> {
27
22
  const recordingStringWithoutClosingBracket = JSON.stringify(
28
- { ...this.recording, isFirstCall: undefined, parent: undefined, oracleCalls: undefined, filePath: undefined },
23
+ { ...recording, parent: undefined, oracleCalls: undefined },
29
24
  null,
30
25
  2,
31
26
  ).slice(0, -2);
@@ -45,13 +40,13 @@ export class FileCircuitRecorder extends CircuitRecorder {
45
40
  }
46
41
  }
47
42
 
48
- this.recording!.isFirstCall = true;
49
- this.recording!.filePath = await FileCircuitRecorder.#computeFilePathAndStoreInitialRecording(
43
+ const filePath = await FileCircuitRecorder.#computeFilePathAndStoreInitialRecording(
50
44
  this.recordDir,
51
- this.recording!.circuitName,
52
- this.recording!.functionName,
45
+ recording.circuitName,
46
+ recording.functionName,
53
47
  recordingStringWithoutClosingBracket,
54
48
  );
49
+ this.#fileState.set(recording, { filePath, isFirstCall: true });
55
50
  }
56
51
 
57
52
  /**
@@ -95,53 +90,48 @@ export class FileCircuitRecorder extends CircuitRecorder {
95
90
  * @param inputs - Input arguments
96
91
  * @param outputs - Output results
97
92
  */
98
- override async recordCall(name: string, inputs: unknown[], outputs: unknown, time: number, stackDepth: number) {
99
- const entry = await super.recordCall(name, inputs, outputs, time, stackDepth);
100
- try {
101
- const prefix = this.recording!.isFirstCall ? ' ' : ' ,';
102
- this.recording!.isFirstCall = false;
103
- await fs.appendFile(this.recording!.filePath, prefix + JSON.stringify(entry) + '\n');
104
- } catch (err) {
105
- this.logger.error('Failed to log circuit call', { error: err });
93
+ override async recordCall(name: string, inputs: unknown[], outputs: unknown, time: number) {
94
+ const entry = await super.recordCall(name, inputs, outputs, time);
95
+ const recording = this.currentRecording();
96
+ const state = recording && this.#fileState.get(recording);
97
+ if (state) {
98
+ try {
99
+ const prefix = state.isFirstCall ? ' ' : ' ,';
100
+ state.isFirstCall = false;
101
+ await fs.appendFile(state.filePath, prefix + JSON.stringify(entry) + '\n');
102
+ } catch (err) {
103
+ this.logger.error('Failed to log circuit call', { error: err });
104
+ }
106
105
  }
107
106
  return entry;
108
107
  }
109
108
 
110
- /**
111
- * Finalizes the recording file by adding closing brackets. Without calling this method, the recording file is
112
- * incomplete and it fails to parse.
113
- */
114
- override async finish(): Promise<CircuitRecording> {
115
- // Finish sets the recording to undefined if we are at the topmost circuit,
116
- // so we save the current file path before that
117
- const filePath = this.recording!.filePath;
118
- const result = await super.finish();
109
+ /** Closes the recording file with the trailing brackets so the JSON parses. */
110
+ protected override async onFinish(recording: CircuitRecording): Promise<void> {
111
+ const state = this.#fileState.get(recording);
112
+ if (!state) {
113
+ return;
114
+ }
119
115
  try {
120
- await fs.appendFile(filePath, ' ]\n}\n');
116
+ await fs.appendFile(state.filePath, ' ]\n}\n');
121
117
  } catch (err) {
122
118
  this.logger.error('Failed to finalize recording file', { error: err });
123
119
  }
124
- return result!;
125
120
  }
126
121
 
127
- /**
128
- * Finalizes the recording file by adding the error and closing brackets. Without calling this method or `finish`,
129
- * the recording file is incomplete and it fails to parse.
130
- * @param error - The error that occurred during circuit execution
131
- */
132
- override async finishWithError(error: unknown): Promise<CircuitRecording> {
133
- // Finish sets the recording to undefined if we are at the topmost circuit,
134
- // so we save the current file path before that
135
- const filePath = this.recording!.filePath;
136
- const result = await super.finishWithError(error);
122
+ /** Closes the recording file with the execution error and trailing brackets so the JSON parses. */
123
+ protected override async onError(recording: CircuitRecording, error: unknown): Promise<void> {
124
+ const state = this.#fileState.get(recording);
125
+ if (!state) {
126
+ return;
127
+ }
137
128
  try {
138
- await fs.appendFile(filePath, ' ],\n');
139
- await fs.appendFile(filePath, ` "error": ${JSON.stringify(error)}\n`);
140
- await fs.appendFile(filePath, '}\n');
129
+ await fs.appendFile(state.filePath, ' ],\n');
130
+ await fs.appendFile(state.filePath, ` "error": ${JSON.stringify(error)}\n`);
131
+ await fs.appendFile(state.filePath, '}\n');
141
132
  } catch (err) {
142
133
  this.logger.error('Failed to finalize recording file with error', { error: err });
143
134
  }
144
- return result!;
145
135
  }
146
136
  }
147
137
 
@@ -58,24 +58,18 @@ export class SimulatorRecorderWrapper implements CircuitSimulator {
58
58
  functionName: string,
59
59
  callback: C,
60
60
  ): Promise<T> {
61
- // Start recording circuit execution
62
- await this.recorder.start(input, bytecode, contractName, functionName);
63
-
64
- // If callback was provided, we wrap it in a circuit recorder callback wrapper
61
+ // If a callback was provided, we wrap it so that its oracle calls are recorded. The wrapped callback reads the
62
+ // active recording lazily, so it picks up the recording opened by record() below.
65
63
  const wrappedCallback = this.recorder.wrapCallback(callback);
66
- let result: T;
67
- try {
68
- result = await simulateFn(wrappedCallback as C);
69
- } catch (error) {
70
- // If an error occurs, we finalize the recording file with the error
71
- await this.recorder.finishWithError(error);
72
- throw error;
73
- }
74
64
 
75
- // Witness generation is complete so we finish the circuit recorder
76
- const recording = await this.recorder.finish();
65
+ // record() opens a recording for this circuit, runs the simulation within it, and finalizes it. A simulation
66
+ // failure is re-thrown unchanged, so recorder bookkeeping never masks the underlying error.
67
+ const { result, recording } = await this.recorder.record(
68
+ { input, bytecode, circuitName: contractName, functionName },
69
+ () => simulateFn(wrappedCallback as C),
70
+ );
77
71
 
78
- (result as ACIRExecutionResult).oracles = recording.oracleCalls?.reduce(
72
+ (result as ACIRExecutionResult).oracles = recording.oracleCalls.reduce(
79
73
  (acc, { time, name }) => {
80
74
  if (!acc[name]) {
81
75
  acc[name] = { times: [] };