poe-code 10.0.1 → 10.0.3

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.
@@ -44,6 +44,7 @@ export function run(source, options = {}) {
44
44
  const lifecycle = { hostCallbackDepth: 0 };
45
45
  const dumpController = createDumpController(lifecycle);
46
46
  const promiseTracker = createSandboxPromiseRejectionTracker();
47
+ let completedSnapshot;
47
48
  const execute = async () => {
48
49
  const promiseReplay = new PromiseReplay(options.snapshot?.promiseReplay);
49
50
  return promiseReplayContext.run(promiseReplay, async () => {
@@ -52,8 +53,18 @@ export function run(source, options = {}) {
52
53
  let leaveHostReplay;
53
54
  let leaveInputReplay;
54
55
  let createFailureSnapshot;
55
- let lastCheckpoint;
56
+ let cancellationSnapshot;
57
+ let cancellationSnapshotError;
56
58
  let snapshotScheduler;
59
+ const captureCancellationSnapshot = () => {
60
+ try {
61
+ cancellationSnapshot = createFailureSnapshot?.();
62
+ }
63
+ catch (reason) {
64
+ cancellationSnapshotError = { reason };
65
+ }
66
+ };
67
+ options.signal?.addEventListener("abort", captureCancellationSnapshot, { once: true });
57
68
  try {
58
69
  const restoredSnapshot = options.snapshot === undefined ? undefined : restore(options.snapshot, { source });
59
70
  if (restoredSnapshot !== undefined) {
@@ -165,6 +176,9 @@ export function run(source, options = {}) {
165
176
  bindings: executionScope.snapshot().bindings,
166
177
  clock: options.clock,
167
178
  hostCalls: hostCalls.snapshot(),
179
+ replay: hostCalls.snapshotReplay(),
180
+ initialInputs: initialInputs.snapshot,
181
+ promiseReplay: promiseReplay.snapshot(),
168
182
  random,
169
183
  sourceHash
170
184
  });
@@ -196,7 +210,6 @@ export function run(source, options = {}) {
196
210
  randomResumeState: typeof yieldPoint.replayState === "number" ? yieldPoint.replayState : undefined,
197
211
  sourceHash
198
212
  });
199
- lastCheckpoint = snapshot;
200
213
  return snapshot;
201
214
  };
202
215
  activeSnapshotScheduler.onYield(createSnapshot);
@@ -242,7 +255,6 @@ export function run(source, options = {}) {
242
255
  : undefined,
243
256
  sourceHash
244
257
  });
245
- lastCheckpoint = snapshot;
246
258
  return snapshot;
247
259
  };
248
260
  activeSnapshotScheduler.onYield(createSnapshot);
@@ -278,6 +290,7 @@ export function run(source, options = {}) {
278
290
  if (replayError !== undefined)
279
291
  snapshot.replayError = replayError;
280
292
  dumpController.finalize(snapshot);
293
+ completedSnapshot = snapshot;
281
294
  return {
282
295
  ...result,
283
296
  snapshot
@@ -288,9 +301,11 @@ export function run(source, options = {}) {
288
301
  materializeWrappedErrorCause(error);
289
302
  if (createFailureSnapshot !== undefined && snapshotScheduler !== undefined) {
290
303
  try {
291
- const snapshot = lastCheckpoint ?? createFailureSnapshot();
292
- await snapshotScheduler.write(snapshot);
304
+ if (cancellationSnapshotError !== undefined)
305
+ throw cancellationSnapshotError.reason;
306
+ const snapshot = cancellationSnapshot ?? createFailureSnapshot();
293
307
  dumpController.finalize(snapshot);
308
+ await snapshotScheduler.write(snapshot);
294
309
  }
295
310
  catch (snapshotError) {
296
311
  if (snapshotError instanceof UnsnapshotableValueError) {
@@ -305,6 +320,7 @@ export function run(source, options = {}) {
305
320
  throw error;
306
321
  }
307
322
  finally {
323
+ options.signal?.removeEventListener("abort", captureCancellationSnapshot);
308
324
  leaveInputReplay?.();
309
325
  leaveHostReplay?.();
310
326
  promiseReplay.dispose();
@@ -313,7 +329,18 @@ export function run(source, options = {}) {
313
329
  }
314
330
  });
315
331
  };
316
- const result = withRunResources(options.signal, () => withSandboxPromiseRejectionTracker(promiseTracker, execute));
332
+ const result = withRunResources(options.signal, () => withSandboxPromiseRejectionTracker(promiseTracker, execute)).catch(async (error) => {
333
+ if (completedSnapshot !== undefined) {
334
+ try {
335
+ await createSnapshotScheduler(options).write(completedSnapshot);
336
+ }
337
+ catch (snapshotError) {
338
+ console.warn("Failed to write failure snapshot.", snapshotError);
339
+ }
340
+ }
341
+ dumpController.fail(error);
342
+ throw error;
343
+ });
317
344
  return attachDumpController(result, dumpController);
318
345
  }
319
346
  async function throwIfReturnedPromiseRejected(result) {
@@ -1,7 +1,7 @@
1
1
  import { type Diagnostic } from "../lint.js";
2
2
  import type { ModuleRegistry } from "../modules/registry.js";
3
3
  import type { OtelSink } from "../observability/otel.js";
4
- import { type RunResult } from "../run.js";
4
+ import { type RunOptions, type RunResult } from "../run.js";
5
5
  import type { SnapshotBackend } from "../snapshot/backend.js";
6
6
  type HarnessMeta = {
7
7
  filepath: string;
@@ -9,6 +9,7 @@ type HarnessMeta = {
9
9
  version: unknown;
10
10
  };
11
11
  export type RunHarnessOptions = {
12
+ budget?: RunOptions["budget"];
12
13
  modulesFor: (frontmatter: Record<string, unknown>, meta: HarnessMeta) => ModuleRegistry;
13
14
  otelSink?: OtelSink;
14
15
  signal?: AbortSignal;
@@ -66,6 +66,7 @@ export async function runHarness(filepath, options) {
66
66
  throw new LintError(lintErrors);
67
67
  }
68
68
  return runHarnessSource(executableSource, {
69
+ budget: options.budget,
69
70
  entryPointArgs: hasDefaultExport(executableSource, filepath) ? [] : undefined,
70
71
  filename: filepath,
71
72
  modules,
@@ -102,6 +103,7 @@ export async function runHarnessPair(filepath, options) {
102
103
  throw new LintError(lintErrors);
103
104
  }
104
105
  return runHarnessSource(executableSource, {
106
+ budget: options.budget,
105
107
  entryPointArgs: [frontmatter],
106
108
  filename: pair.scriptPath,
107
109
  importMeta: {
@@ -1,17 +1,20 @@
1
1
  import type { RunResult, RunSnapshot } from "../run.js";
2
+ export type DumpOptions = {
3
+ onFailure?: "throw" | "checkpoint";
4
+ };
2
5
  type DumpController = {
3
6
  fail(error: unknown): void;
4
7
  finalize(snapshot: RunSnapshot): void;
5
8
  onYield(createSnapshot: () => RunSnapshot): void;
6
9
  requestCurrentSnapshot(): Promise<string>;
7
- requestSnapshot(): Promise<string>;
10
+ requestSnapshot(options?: DumpOptions): Promise<string>;
8
11
  };
9
12
  export type RunLifecycle = {
10
13
  hostCallbackDepth: number;
11
14
  };
12
15
  export declare function attachDumpController(result: Promise<RunResult>, controller: DumpController): Promise<RunResult>;
13
16
  export declare function createDumpController(lifecycle?: RunLifecycle): DumpController;
14
- export declare function dump(result: Pick<RunResult, "snapshot"> | PromiseLike<RunResult>): Promise<string>;
17
+ export declare function dump(result: Pick<RunResult, "snapshot"> | PromiseLike<RunResult>, options?: DumpOptions): Promise<string>;
15
18
  export declare function dumpCurrent(result: Pick<RunResult, "snapshot"> | PromiseLike<RunResult>): Promise<string>;
16
19
  export declare function serializeRunSnapshot(snapshot: RunSnapshot): string;
17
20
  export {};
@@ -61,13 +61,14 @@ export function createDumpController(lifecycle) {
61
61
  }
62
62
  return this.requestSnapshot();
63
63
  },
64
- requestSnapshot() {
64
+ requestSnapshot(options = {}) {
65
65
  assertDumpAllowed();
66
66
  if (failed !== undefined) {
67
- if (isDataBudgetError(failed.error) &&
68
- (latestSnapshot !== undefined || latestSnapshotFactory !== undefined)) {
67
+ if ((options.onFailure === "checkpoint" ||
68
+ (options.onFailure === undefined && isDataBudgetError(failed.error))) &&
69
+ finalSnapshot !== undefined) {
69
70
  try {
70
- return Promise.resolve(serializeRunSnapshot(latestSnapshot ?? latestSnapshotFactory()));
71
+ return Promise.resolve(serializeRunSnapshot(finalSnapshot));
71
72
  }
72
73
  catch (error) {
73
74
  return Promise.reject(error);
@@ -134,10 +135,10 @@ function isDataBudgetError(error) {
134
135
  "budget" in error &&
135
136
  error.budget === "dataSize");
136
137
  }
137
- export function dump(result) {
138
+ export function dump(result, options = {}) {
138
139
  const controller = readDumpController(result);
139
140
  if (controller !== undefined) {
140
- return controller.requestSnapshot();
141
+ return controller.requestSnapshot(options);
141
142
  }
142
143
  if (hasSnapshot(result)) {
143
144
  try {