@withgateway/sdk 0.3.0 → 0.3.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.
Files changed (130) hide show
  1. package/README.md +6 -1
  2. package/dist/cli.js +3971 -2338
  3. package/dist/cli.js.map +1 -1
  4. package/dist/cli.mjs +3950 -2318
  5. package/dist/cli.mjs.map +1 -1
  6. package/dist/files/index.d.mts +1 -1
  7. package/dist/files/index.d.ts +1 -1
  8. package/dist/files/index.js +214 -41
  9. package/dist/files/index.js.map +1 -1
  10. package/dist/files/index.mjs +206 -35
  11. package/dist/files/index.mjs.map +1 -1
  12. package/dist/{index-DAb-FCMg.d.ts → index-DUHmS74p.d.ts} +3 -13
  13. package/dist/{index-D6oi1TWE.d.mts → index-mJhG3vZU.d.mts} +3 -13
  14. package/dist/index.d.mts +4 -4
  15. package/dist/index.d.ts +4 -4
  16. package/dist/index.js +1456 -564
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +1433 -557
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/{media-CbcAfQvd.d.mts → media-BIApiPzS.d.mts} +21 -2
  21. package/dist/{media-CbcAfQvd.d.ts → media-BIApiPzS.d.ts} +21 -2
  22. package/dist/traces/index.d.mts +1 -1
  23. package/dist/traces/index.d.ts +1 -1
  24. package/dist/traces/index.js +211 -38
  25. package/dist/traces/index.js.map +1 -1
  26. package/dist/traces/index.mjs +203 -32
  27. package/dist/traces/index.mjs.map +1 -1
  28. package/dist/tracing/index.d.mts +2 -2
  29. package/dist/tracing/index.d.ts +2 -2
  30. package/dist/tracing/index.js +96 -21
  31. package/dist/tracing/index.js.map +1 -1
  32. package/dist/tracing/index.mjs +96 -21
  33. package/dist/tracing/index.mjs.map +1 -1
  34. package/dist/worlds/index.d.mts +300 -123
  35. package/dist/worlds/index.d.ts +300 -123
  36. package/dist/worlds/index.js +1385 -508
  37. package/dist/worlds/index.js.map +1 -1
  38. package/dist/worlds/index.mjs +1377 -501
  39. package/dist/worlds/index.mjs.map +1 -1
  40. package/package.json +1 -1
  41. package/runtime/gatewaysdk/environment/_bundle.py +2 -27
  42. package/runtime/gatewaysdk/environment/_materialize.py +49 -69
  43. package/runtime/gatewaysdk/environment/_world.py +122 -197
  44. package/runtime/gatewaysdk/environment/backing.py +137 -0
  45. package/runtime/gatewaysdk/environment/core.py +1 -1
  46. package/runtime/gatewaysdk/environment/legacy_sqlite.py +301 -0
  47. package/runtime/gatewaysdk/environment/reward_keys.py +25 -0
  48. package/runtime/gatewaysdk/environment/runtime.py +8 -13
  49. package/runtime/gatewaysdk/environment/schema/__init__.py +4 -4
  50. package/runtime/gatewaysdk/environment/schema/admin_routes.py +86 -0
  51. package/runtime/gatewaysdk/environment/schema/api.py +266 -515
  52. package/runtime/gatewaysdk/environment/schema/batch.py +42 -37
  53. package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +17 -5
  54. package/runtime/gatewaysdk/environment/schema/call_log.py +291 -0
  55. package/runtime/gatewaysdk/environment/schema/call_redaction.py +344 -0
  56. package/runtime/gatewaysdk/environment/schema/cast.py +31 -4
  57. package/runtime/gatewaysdk/environment/schema/compiler.py +334 -158
  58. package/runtime/gatewaysdk/environment/schema/conform.py +8 -12
  59. package/runtime/gatewaysdk/environment/schema/connector.py +27 -62
  60. package/runtime/gatewaysdk/environment/schema/data.py +50 -207
  61. package/runtime/gatewaysdk/environment/schema/database/__init__.py +24 -0
  62. package/runtime/gatewaysdk/environment/schema/database/audit.py +121 -0
  63. package/runtime/gatewaysdk/environment/schema/database/base.py +995 -0
  64. package/runtime/gatewaysdk/environment/schema/database/budget.py +33 -0
  65. package/runtime/gatewaysdk/environment/schema/database/connections.py +101 -0
  66. package/runtime/gatewaysdk/environment/schema/database/engines.py +8 -0
  67. package/runtime/gatewaysdk/environment/schema/database/guard.py +208 -0
  68. package/runtime/gatewaysdk/environment/schema/database/ingest.py +154 -0
  69. package/runtime/gatewaysdk/environment/schema/database/matching.py +58 -0
  70. package/runtime/gatewaysdk/environment/schema/database/provenance.py +30 -0
  71. package/runtime/gatewaysdk/environment/schema/database/rows.py +53 -0
  72. package/runtime/gatewaysdk/environment/schema/database/runtime_rows.py +39 -0
  73. package/runtime/gatewaysdk/environment/schema/database/sqlite.py +367 -0
  74. package/runtime/gatewaysdk/environment/schema/domain_migration.py +236 -0
  75. package/runtime/gatewaysdk/environment/schema/envelope.py +169 -0
  76. package/runtime/gatewaysdk/environment/schema/extract.py +6 -13
  77. package/runtime/gatewaysdk/environment/schema/grading.py +45 -30
  78. package/runtime/gatewaysdk/environment/schema/handler_errors.py +142 -12
  79. package/runtime/gatewaysdk/environment/schema/host.py +158 -250
  80. package/runtime/gatewaysdk/environment/schema/lanes.py +53 -0
  81. package/runtime/gatewaysdk/environment/schema/local.py +44 -71
  82. package/runtime/gatewaysdk/environment/schema/mapping.py +5 -25
  83. package/runtime/gatewaysdk/environment/schema/openapi_import.py +112 -73
  84. package/runtime/gatewaysdk/environment/schema/openapi_paging.py +147 -0
  85. package/runtime/gatewaysdk/environment/schema/openapi_payloads.py +288 -0
  86. package/runtime/gatewaysdk/environment/schema/openapi_routes.py +97 -102
  87. package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +113 -30
  88. package/runtime/gatewaysdk/environment/schema/platform.py +124 -178
  89. package/runtime/gatewaysdk/environment/schema/platform_import.py +2 -3
  90. package/runtime/gatewaysdk/environment/schema/platform_query.py +61 -5
  91. package/runtime/gatewaysdk/environment/schema/primary_key.py +61 -0
  92. package/runtime/gatewaysdk/environment/schema/proof.py +19 -12
  93. package/runtime/gatewaysdk/environment/schema/runtime.py +77 -160
  94. package/runtime/gatewaysdk/environment/schema/served_names.py +58 -0
  95. package/runtime/gatewaysdk/environment/schema/session_files.py +166 -0
  96. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +2 -2
  97. package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +22 -14
  98. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +29 -3
  99. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +16 -16
  100. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +18 -18
  101. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +3 -2
  102. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +15 -0
  103. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/openapi-import.md +50 -21
  104. package/runtime/gatewaysdk/environment/schema/slack.py +4 -8
  105. package/runtime/gatewaysdk/environment/schema/snapshot.py +385 -129
  106. package/runtime/gatewaysdk/environment/schema/store_contract.py +8 -14
  107. package/runtime/gatewaysdk/environment/schema/tasks.py +140 -55
  108. package/runtime/gatewaysdk/environment/schema/tools_world.py +110 -87
  109. package/runtime/gatewaysdk/environment/schema/ui.py +24 -5
  110. package/runtime/gatewaysdk/environment/schema/ui_static/app.js +100 -130
  111. package/runtime/gatewaysdk/environment/schema/ui_static/index.html +1 -8
  112. package/runtime/gatewaysdk/environment/schema/validation.py +166 -40
  113. package/runtime/gatewaysdk/environment/schema/workers.py +142 -57
  114. package/runtime/gatewaysdk/environment/schema/world_sql.py +81 -0
  115. package/runtime/gatewaysdk/environment/schema/world_tests.py +71 -39
  116. package/runtime/gatewaysdk/environment/score.py +57 -0
  117. package/runtime/gatewaysdk/environment/secret_values.py +36 -0
  118. package/runtime/gatewaysdk/environment/world.py +42 -3
  119. package/runtime/gatewaysdk/environment/world_session_contract.py +32 -5
  120. package/skills/benchmarks-getting-started/SKILL.md +2 -2
  121. package/skills/connector-schema-authoring/SKILL.md +22 -14
  122. package/skills/connector-schema-authoring/connector-worlds.md +26 -10
  123. package/skills/connector-schema-authoring/world-schema.md +111 -51
  124. package/skills/world-data-ingestion/ROWS.md +29 -3
  125. package/skills/world-data-ingestion/SKILL.md +16 -16
  126. package/skills/worlds-getting-started/SKILL.md +18 -18
  127. package/skills/worlds-getting-started/references/cli-mcp-map.md +3 -2
  128. package/skills/worlds-getting-started/references/gotchas.md +15 -0
  129. package/skills/worlds-getting-started/references/openapi-import.md +50 -21
  130. package/runtime/gatewaysdk/environment/schema/store.py +0 -1013
package/dist/index.js CHANGED
@@ -204,10 +204,10 @@ var init_users = __esm({
204
204
  totalItems: Number(meta.totalItems ?? rows.length)
205
205
  };
206
206
  }
207
- async requestJson(method, path16, payload) {
207
+ async requestJson(method, path17, payload) {
208
208
  let response;
209
209
  try {
210
- response = await fetch(`${this.host}${path16}`, {
210
+ response = await fetch(`${this.host}${path17}`, {
211
211
  method,
212
212
  headers: {
213
213
  Authorization: this.authHeader(),
@@ -218,17 +218,17 @@ var init_users = __esm({
218
218
  });
219
219
  } catch (error) {
220
220
  throw new UserProfileError(
221
- `${method} ${path16} failed: ${error instanceof Error ? error.message : String(error)}`,
221
+ `${method} ${path17} failed: ${error instanceof Error ? error.message : String(error)}`,
222
222
  { cause: error }
223
223
  );
224
224
  }
225
225
  const text2 = await response.text();
226
226
  if (!response.ok) {
227
227
  throw new UserProfileError(
228
- `${method} ${path16} failed (${response.status}): ${text2.slice(0, 500)}`
228
+ `${method} ${path17} failed (${response.status}): ${text2.slice(0, 500)}`
229
229
  );
230
230
  }
231
- this.log("%s %s -> %s", method, path16, response.status);
231
+ this.log("%s %s -> %s", method, path17, response.status);
232
232
  return safeJson4(text2);
233
233
  }
234
234
  authHeader() {
@@ -417,10 +417,10 @@ var init_scores = __esm({
417
417
  metadata: signalMetadata
418
418
  });
419
419
  }
420
- async request(method, path16, payload) {
420
+ async request(method, path17, payload) {
421
421
  let response;
422
422
  try {
423
- response = await fetch(`${this.host}${path16}`, {
423
+ response = await fetch(`${this.host}${path17}`, {
424
424
  method,
425
425
  headers: {
426
426
  Authorization: this.authHeader(),
@@ -431,17 +431,17 @@ var init_scores = __esm({
431
431
  });
432
432
  } catch (error) {
433
433
  throw new ScoreError(
434
- `${method} ${path16} failed: ${error instanceof Error ? error.message : String(error)}`,
434
+ `${method} ${path17} failed: ${error instanceof Error ? error.message : String(error)}`,
435
435
  { cause: error }
436
436
  );
437
437
  }
438
438
  const text2 = await response.text();
439
439
  if (!response.ok) {
440
440
  throw new ScoreError(
441
- `${method} ${path16} failed (${response.status}): ${text2.slice(0, 500)}`
441
+ `${method} ${path17} failed (${response.status}): ${text2.slice(0, 500)}`
442
442
  );
443
443
  }
444
- this.log("%s %s -> %s", method, path16, response.status);
444
+ this.log("%s %s -> %s", method, path17, response.status);
445
445
  }
446
446
  authHeader() {
447
447
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -464,6 +464,28 @@ var init_scores = __esm({
464
464
  }
465
465
  });
466
466
 
467
+ // src/worlds/platformEndpoint.ts
468
+ function platformEndpoint(options) {
469
+ const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
470
+ const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
471
+ if (!publicKey || !secretKey) return null;
472
+ const host = options.host ?? process.env.GATEWAY_HOST ?? DEFAULT_HOST;
473
+ return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
474
+ }
475
+ function requirePlatformEndpoint(options) {
476
+ const endpoint = platformEndpoint(options);
477
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
478
+ return endpoint;
479
+ }
480
+ var DEFAULT_HOST, GATEWAY_KEYS_MISSING;
481
+ var init_platformEndpoint = __esm({
482
+ "src/worlds/platformEndpoint.ts"() {
483
+ "use strict";
484
+ DEFAULT_HOST = "https://withgateway.ai";
485
+ GATEWAY_KEYS_MISSING = "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).";
486
+ }
487
+ });
488
+
467
489
  // src/worlds/transport.ts
468
490
  function platformTimeoutMs(env = process.env) {
469
491
  const raw = env[PLATFORM_TIMEOUT_ENV];
@@ -472,7 +494,7 @@ function platformTimeoutMs(env = process.env) {
472
494
  if (Number.isInteger(value) && value > 0) return value;
473
495
  throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
474
496
  }
475
- var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, PlatformTransport;
497
+ var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, sleep2, RetryPolicy, Attempt, PlatformTransport;
476
498
  var init_transport = __esm({
477
499
  "src/worlds/transport.ts"() {
478
500
  "use strict";
@@ -534,6 +556,56 @@ var init_transport = __esm({
534
556
  route;
535
557
  failure;
536
558
  };
559
+ sleep2 = (ms) => new Promise((resolve11) => setTimeout(resolve11, ms));
560
+ RetryPolicy = class _RetryPolicy {
561
+ constructor(delaysMs, wait = sleep2, budgetMs = Number.POSITIVE_INFINITY, now = Date.now) {
562
+ this.delaysMs = delaysMs;
563
+ this.wait = wait;
564
+ this.budgetMs = budgetMs;
565
+ this.now = now;
566
+ }
567
+ delaysMs;
568
+ wait;
569
+ budgetMs;
570
+ now;
571
+ static NONE = new _RetryPolicy([]);
572
+ /** A platform restart or a short edge outage: no wait starts that would end past five minutes, so a step ends
573
+ * within the budget plus one request timeout. */
574
+ static FILING = new _RetryPolicy([250, 500, 1e3, 2e3, 4e3, 8e3, 16e3, 3e4], sleep2, 3e5);
575
+ static TRANSIENT_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
576
+ async run(attempt) {
577
+ const started = this.now();
578
+ for (let tries = 0; ; tries += 1) {
579
+ const outcome = await Attempt.of(attempt);
580
+ const delayMs = this.delaysMs[tries];
581
+ const spent = delayMs === void 0 || this.now() - started + delayMs >= this.budgetMs;
582
+ if (!outcome.transient || spent) return outcome.settle();
583
+ await this.wait(delayMs);
584
+ }
585
+ }
586
+ };
587
+ Attempt = class _Attempt {
588
+ constructor(reply, error, transient) {
589
+ this.reply = reply;
590
+ this.error = error;
591
+ this.transient = transient;
592
+ }
593
+ reply;
594
+ error;
595
+ transient;
596
+ static async of(attempt) {
597
+ try {
598
+ const reply = await attempt();
599
+ return new _Attempt(reply, null, RetryPolicy.TRANSIENT_STATUSES.has(reply.status));
600
+ } catch (error) {
601
+ return new _Attempt(null, error, error instanceof UnreachableError);
602
+ }
603
+ }
604
+ settle() {
605
+ if (this.reply) return this.reply;
606
+ throw this.error;
607
+ }
608
+ };
537
609
  PlatformTransport = class {
538
610
  constructor(endpoint, timeoutMs = platformTimeoutMs()) {
539
611
  this.endpoint = endpoint;
@@ -547,6 +619,9 @@ var init_transport = __esm({
547
619
  }
548
620
  async send(method, route, body, opts = {}) {
549
621
  const timeoutMs = this.budget(opts.timeoutMs);
622
+ return (opts.retry ?? RetryPolicy.NONE).run(() => this.once(method, route, body, timeoutMs));
623
+ }
624
+ async once(method, route, body, timeoutMs) {
550
625
  const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
551
626
  const controller = new AbortController();
552
627
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -740,9 +815,9 @@ function surfaceKinds(s) {
740
815
  if (s.browser) kinds.push("browser");
741
816
  return kinds;
742
817
  }
743
- function uiUrl(ui, path16) {
818
+ function uiUrl(ui, path17) {
744
819
  const base = ui.url.replace(/\/+$/, "");
745
- const rel = path16.startsWith("/") ? path16 : `/${path16}`;
820
+ const rel = path17.startsWith("/") ? path17 : `/${path17}`;
746
821
  if (!ui.token) return base + rel;
747
822
  const [beforeHash, hash] = splitHash(rel);
748
823
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -983,7 +1058,7 @@ var init_browser = __esm({
983
1058
  }
984
1059
  async send(batch) {
985
1060
  const retries = this.opts.maxRetries ?? EVENT_MAX_RETRIES;
986
- const sleep3 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1061
+ const sleep5 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
987
1062
  for (let attempt = 0; attempt < retries; attempt++) {
988
1063
  try {
989
1064
  const base = await this.resolveBase();
@@ -1000,7 +1075,7 @@ var init_browser = __esm({
1000
1075
  );
1001
1076
  return;
1002
1077
  }
1003
- await sleep3(500 * 2 ** attempt);
1078
+ await sleep5(500 * 2 ** attempt);
1004
1079
  }
1005
1080
  }
1006
1081
  }
@@ -1637,13 +1712,13 @@ var init_screen = __esm({
1637
1712
  });
1638
1713
  }
1639
1714
  async drag(action) {
1640
- const path16 = action.args.path ?? [];
1641
- if (path16.length < 2) throw new Error("a drag needs at least two points");
1715
+ const path17 = action.args.path ?? [];
1716
+ if (path17.length < 2) throw new Error("a drag needs at least two points");
1642
1717
  return this.held(action.args.modifiers ?? [], async () => {
1643
- await this.moveTo(path16[0]);
1718
+ await this.moveTo(path17[0]);
1644
1719
  await this.page.mouse.down({ button: "left" });
1645
1720
  try {
1646
- for (const point2 of path16.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1721
+ for (const point2 of path17.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1647
1722
  } finally {
1648
1723
  await this.page.mouse.up({ button: "left" });
1649
1724
  }
@@ -1734,7 +1809,7 @@ var init_media = __esm({
1734
1809
  put;
1735
1810
  maxBytes;
1736
1811
  /** The uploader for `GATEWAY_HOST` / `GATEWAY_PUBLIC_KEY` /
1737
- * `GATEWAY_SECRET_KEY` (the keys tracing exports with); null when any is unset. */
1812
+ * `GATEWAY_SECRET_KEY` only; null when any is unset, so a run traced with a saved login uploads none. */
1738
1813
  static fromEnv(env = process.env) {
1739
1814
  const host = env.GATEWAY_HOST;
1740
1815
  const publicKey = env.GATEWAY_PUBLIC_KEY;
@@ -2604,7 +2679,7 @@ function idleWarning(idleExpiresAt, now = /* @__PURE__ */ new Date()) {
2604
2679
  const minutes = Math.max(0, Math.floor(leftMs / 6e4));
2605
2680
  return `warning: session expires in ${minutes}m (idle TTL); call a tool to extend it or \`gateway worlds session pin\` to keep it`;
2606
2681
  }
2607
- var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge;
2682
+ var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge, stillStartingHint;
2608
2683
  var init_sessionContract = __esm({
2609
2684
  "src/worlds/sessionContract.ts"() {
2610
2685
  "use strict";
@@ -2640,6 +2715,65 @@ var init_sessionContract = __esm({
2640
2715
  };
2641
2716
  }
2642
2717
  };
2718
+ stillStartingHint = (sessionId) => `It keeps opening: poll \`gateway worlds session status ${sessionId}\` until ready, or end it with \`gateway worlds session close ${sessionId}\`.`;
2719
+ }
2720
+ });
2721
+
2722
+ // src/worlds/sessionReadiness.ts
2723
+ var READY_TIMEOUT_MS, READY_POLL_MS, sleep3, WorldSessionTimeout, WorldSessionCallError, WorldSessionOpenFailed, ENDED, SessionReadiness;
2724
+ var init_sessionReadiness = __esm({
2725
+ "src/worlds/sessionReadiness.ts"() {
2726
+ "use strict";
2727
+ READY_TIMEOUT_MS = 10 * 6e4;
2728
+ READY_POLL_MS = 2e3;
2729
+ sleep3 = (ms) => new Promise((r) => setTimeout(r, ms));
2730
+ WorldSessionTimeout = class extends Error {
2731
+ };
2732
+ WorldSessionCallError = class extends Error {
2733
+ };
2734
+ WorldSessionOpenFailed = class extends WorldSessionCallError {
2735
+ constructor(sessionId, state2, code, reason) {
2736
+ super(
2737
+ reason === null ? `World session ${sessionId} ${state2.toLowerCase()} before it was ready.` : `World session ${sessionId} failed to open: ${reason}${code ? ` (${code})` : ""}`
2738
+ );
2739
+ this.sessionId = sessionId;
2740
+ this.state = state2;
2741
+ this.code = code;
2742
+ this.name = "WorldSessionOpenFailed";
2743
+ }
2744
+ sessionId;
2745
+ state;
2746
+ code;
2747
+ };
2748
+ ENDED = /* @__PURE__ */ new Set(["FAILED", "STOPPED"]);
2749
+ SessionReadiness = class {
2750
+ constructor(sessionId, status) {
2751
+ this.sessionId = sessionId;
2752
+ this.status = status;
2753
+ }
2754
+ sessionId;
2755
+ status;
2756
+ async wait(timeoutMs = READY_TIMEOUT_MS) {
2757
+ const deadline = Date.now() + timeoutMs;
2758
+ for (; ; ) {
2759
+ const state2 = await this.status();
2760
+ if (state2.ready) return state2;
2761
+ if (ENDED.has(state2.state))
2762
+ throw new WorldSessionOpenFailed(
2763
+ this.sessionId,
2764
+ state2.state,
2765
+ state2.failure?.code ?? null,
2766
+ state2.failure?.message ?? null
2767
+ );
2768
+ const left = deadline - Date.now();
2769
+ if (left <= 0)
2770
+ throw new WorldSessionTimeout(
2771
+ `World session ${this.sessionId} still ${state2.state} after ${Math.round(timeoutMs / 1e3)}s.`
2772
+ );
2773
+ await sleep3(Math.min(READY_POLL_MS, left));
2774
+ }
2775
+ }
2776
+ };
2643
2777
  }
2644
2778
  });
2645
2779
 
@@ -2735,11 +2869,184 @@ var init_taskRef = __esm({
2735
2869
  }
2736
2870
  });
2737
2871
 
2872
+ // src/worlds/worldNotice.ts
2873
+ var TOOL_CONTRACT_UNREAD, DEPRECATION, NOTICE_KINDS, WorldNotice, WorldToolsUnread;
2874
+ var init_worldNotice = __esm({
2875
+ "src/worlds/worldNotice.ts"() {
2876
+ "use strict";
2877
+ TOOL_CONTRACT_UNREAD = "tool-contract-unread";
2878
+ DEPRECATION = { label: "deprecated", next: "Instead", refusesRun: false };
2879
+ NOTICE_KINDS = /* @__PURE__ */ new Map([
2880
+ [TOOL_CONTRACT_UNREAD, { label: "warning", next: "Then", refusesRun: true }]
2881
+ ]);
2882
+ WorldNotice = class _WorldNotice {
2883
+ constructor(notice, kind) {
2884
+ this.notice = notice;
2885
+ this.kind = kind;
2886
+ }
2887
+ notice;
2888
+ kind;
2889
+ static of(notice) {
2890
+ return new _WorldNotice(notice, NOTICE_KINDS.get(notice.code) ?? DEPRECATION);
2891
+ }
2892
+ get refusesRun() {
2893
+ return this.kind.refusesRun;
2894
+ }
2895
+ line() {
2896
+ const { code, message, replacement } = this.notice;
2897
+ return `${this.kind.label} (${code}): ${message} ${this.kind.next}: ${replacement}`;
2898
+ }
2899
+ };
2900
+ WorldToolsUnread = class _WorldToolsUnread extends Error {
2901
+ constructor(world, notice) {
2902
+ super(
2903
+ `refused before the agent ran, nothing filed: world ${world} has no readable tools: ${notice.notice.message} Then: ${notice.notice.replacement}`
2904
+ );
2905
+ this.world = world;
2906
+ this.notice = notice;
2907
+ this.name = "WorldToolsUnread";
2908
+ this.code = notice.notice.code;
2909
+ }
2910
+ world;
2911
+ notice;
2912
+ code;
2913
+ static refuse(world, notices) {
2914
+ const refusing = notices.map((notice) => WorldNotice.of(notice)).find((notice) => notice.refusesRun);
2915
+ if (refusing) throw new _WorldToolsUnread(world, refusing);
2916
+ }
2917
+ };
2918
+ }
2919
+ });
2920
+
2921
+ // src/worlds/grade.ts
2922
+ var messageOf, Score, GradeRefused, GradePayload;
2923
+ var init_grade = __esm({
2924
+ "src/worlds/grade.ts"() {
2925
+ "use strict";
2926
+ messageOf = (error) => (error instanceof Error ? error.message || error.name : String(error)) || "Error";
2927
+ Score = class _Score {
2928
+ static RULE = "a score is a number from 0 to 1";
2929
+ static admits(value) {
2930
+ return Number.isFinite(value) && value >= 0 && value <= 1;
2931
+ }
2932
+ /** Every number among `scores` that is not a score, as `'<key>' = <value>`
2933
+ * and the rule, or null when all of them are. Pairs, not a map, so that no
2934
+ * key hides another. */
2935
+ static problem(scores2) {
2936
+ const refused = [...scores2].flatMap(
2937
+ ([key2, value]) => typeof value === "number" && !_Score.admits(value) ? [`'${key2}' = ${String(value)}`] : []
2938
+ );
2939
+ return refused.length ? `${refused.join(", ")}; ${_Score.RULE}` : null;
2940
+ }
2941
+ };
2942
+ GradeRefused = class extends Error {
2943
+ name = "GradeRefused";
2944
+ };
2945
+ GradePayload = class _GradePayload {
2946
+ /** A grade payload's numbers, checked: every number in it must be a score,
2947
+ * and its rewards and verifiers objects by name, or the grade is refused; a
2948
+ * cross-world verifier's grade that breaks either rule is that verifier's
2949
+ * error, the way the World Host answers one that could not grade. */
2950
+ static read(raw) {
2951
+ if (!_GradePayload.objectOrAbsent(raw))
2952
+ throw new GradeRefused("the grade is not an object");
2953
+ const record = _GradePayload.record(raw);
2954
+ const problem = _GradePayload.problem(record) ?? _GradePayload.verifiersProblem(record.verifiers);
2955
+ if (problem) throw new GradeRefused(problem);
2956
+ const verifiers = _GradePayload.verifiers(record.verifiers);
2957
+ return {
2958
+ ..._GradePayload.numbers(record),
2959
+ ...typeof record.reason === "string" ? { reason: record.reason } : {},
2960
+ ...typeof record.error === "string" ? { error: record.error } : {},
2961
+ ...verifiers ? { verifiers } : {}
2962
+ };
2963
+ }
2964
+ /** A world whose grade call failed (the platform refused it, the host had
2965
+ * lost the session): that world's error, never a score. */
2966
+ static failedCall(error) {
2967
+ return { ..._GradePayload.failed(messageOf(error)), raw: null };
2968
+ }
2969
+ static problem(record) {
2970
+ if (!_GradePayload.objectOrAbsent(record.rewards))
2971
+ return "the grade's rewards are not an object";
2972
+ const scores2 = Score.problem(_GradePayload.scores(record));
2973
+ if (scores2) return `the grade answered ${scores2}`;
2974
+ const twice = _GradePayload.twice(record);
2975
+ return twice.length ? `the grade answered ${twice.map((key2) => `'${key2}'`).join(", ")} both beside and inside rewards` : null;
2976
+ }
2977
+ static twice(record) {
2978
+ return Object.entries(_GradePayload.record(record.rewards)).filter(
2979
+ ([key2, value]) => key2 !== "reward" && typeof value === "number" && typeof record[key2] === "number"
2980
+ ).map(([key2]) => key2);
2981
+ }
2982
+ static verifiersProblem(value) {
2983
+ return _GradePayload.objectOrAbsent(value) ? null : "the grade's verifiers are not an object";
2984
+ }
2985
+ static scores(record) {
2986
+ return [
2987
+ ["reward", record.reward],
2988
+ ...Object.entries(_GradePayload.record(record.rewards)),
2989
+ ...Object.entries(record).filter(
2990
+ ([key2]) => key2 !== "reward" && key2 !== "rewards"
2991
+ )
2992
+ ];
2993
+ }
2994
+ static isRecord(value) {
2995
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2996
+ }
2997
+ static objectOrAbsent(value) {
2998
+ return value === void 0 || value === null || _GradePayload.isRecord(value);
2999
+ }
3000
+ static record(value) {
3001
+ return _GradePayload.isRecord(value) ? value : {};
3002
+ }
3003
+ /** `reward`, and every numeric key under `rewards` (the host's shape) or at
3004
+ * the top level (a bundled verifier's). */
3005
+ static numbers(record) {
3006
+ const rewards = {};
3007
+ for (const [key2, value] of Object.entries(
3008
+ _GradePayload.record(record.rewards)
3009
+ ))
3010
+ if (typeof value === "number") rewards[key2] = value;
3011
+ for (const [key2, value] of Object.entries(record))
3012
+ if (key2 !== "reward" && typeof value === "number") rewards[key2] = value;
3013
+ return {
3014
+ reward: typeof record.reward === "number" ? record.reward : null,
3015
+ rewards
3016
+ };
3017
+ }
3018
+ static verifiers(value) {
3019
+ if (!_GradePayload.isRecord(value)) return void 0;
3020
+ return Object.fromEntries(
3021
+ Object.entries(value).map(([name, grade]) => [
3022
+ name,
3023
+ _GradePayload.verifier(name, grade)
3024
+ ])
3025
+ );
3026
+ }
3027
+ static verifier(name, grade) {
3028
+ if (!_GradePayload.isRecord(grade))
3029
+ return _GradePayload.failed(`the grade's verifier '${name}' is not an object`);
3030
+ const problem = _GradePayload.problem(grade);
3031
+ if (problem) return _GradePayload.failed(problem);
3032
+ return {
3033
+ ..._GradePayload.numbers(grade),
3034
+ ...typeof grade.error === "string" ? { error: grade.error } : {}
3035
+ };
3036
+ }
3037
+ static failed(error) {
3038
+ return { reward: null, rewards: {}, error };
3039
+ }
3040
+ };
3041
+ }
3042
+ });
3043
+
2738
3044
  // src/worlds/session.ts
2739
3045
  var session_exports2 = {};
2740
3046
  __export(session_exports2, {
2741
3047
  WorldSession: () => WorldSession,
2742
3048
  WorldSessionCallError: () => WorldSessionCallError,
3049
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
2743
3050
  WorldSessionTimeout: () => WorldSessionTimeout,
2744
3051
  attachSession: () => attachSession,
2745
3052
  isTaskIdRef: () => isTaskIdRef,
@@ -2750,34 +3057,6 @@ __export(session_exports2, {
2750
3057
  schemaFromSignature: () => schemaFromSignature,
2751
3058
  taskLabel: () => taskLabel
2752
3059
  });
2753
- function numericGrade(record) {
2754
- const rewards = {};
2755
- const nested = record.rewards;
2756
- if (nested && typeof nested === "object") {
2757
- for (const [key2, value] of Object.entries(nested)) {
2758
- if (typeof value === "number") rewards[key2] = value;
2759
- }
2760
- }
2761
- for (const [key2, value] of Object.entries(record)) {
2762
- if (key2 !== "reward" && typeof value === "number") rewards[key2] = value;
2763
- }
2764
- return {
2765
- reward: typeof record.reward === "number" ? record.reward : null,
2766
- rewards
2767
- };
2768
- }
2769
- function verifierGrades(value) {
2770
- if (!value || typeof value !== "object") return void 0;
2771
- const out = {};
2772
- for (const [name, grade] of Object.entries(value)) {
2773
- const record = grade && typeof grade === "object" ? grade : {};
2774
- out[name] = {
2775
- ...numericGrade(record),
2776
- ...typeof record.error === "string" ? { error: record.error } : {}
2777
- };
2778
- }
2779
- return out;
2780
- }
2781
3060
  function schemaFromSignature(spec) {
2782
3061
  if (spec.inputSchema)
2783
3062
  return {
@@ -2885,6 +3164,7 @@ async function runSessions(opts) {
2885
3164
  const taskRef = tasks[next++];
2886
3165
  let task = taskLabel(taskRef);
2887
3166
  let session = null;
3167
+ let keepWarm = opts.keepWarm ?? true;
2888
3168
  try {
2889
3169
  session = await openSession(opts.world, {
2890
3170
  task: taskRef,
@@ -2892,6 +3172,7 @@ async function runSessions(opts) {
2892
3172
  options
2893
3173
  });
2894
3174
  task = taskLabel(taskRef, session.taskInfo);
3175
+ WorldToolsUnread.refuse(opts.world.split("@", 1)[0], session.deprecations);
2895
3176
  await session.ready();
2896
3177
  const toolkit = await session.toolkit();
2897
3178
  const live = session;
@@ -2914,19 +3195,19 @@ async function runSessions(opts) {
2914
3195
  results.push(result);
2915
3196
  opts.onResult?.(result);
2916
3197
  } catch (error) {
3198
+ if (error instanceof WorldToolsUnread) keepWarm = false;
2917
3199
  const result = {
2918
3200
  task,
2919
3201
  sessionId: session?.sessionId ?? "",
2920
3202
  reward: 0,
2921
3203
  rewards: {},
2922
- // An Error with an empty message would read as "no error" to any
2923
- // caller checking truthiness; name the type instead.
3204
+ // An empty message reads as "no error" to a truthiness check; name the type instead.
2924
3205
  error: (error instanceof Error ? error.message : String(error)) || (error instanceof Error ? error.name : "Unknown error")
2925
3206
  };
2926
3207
  results.push(result);
2927
3208
  opts.onResult?.(result);
2928
3209
  } finally {
2929
- await session?.close({ keepWarm: opts.keepWarm ?? true }).catch(() => void 0);
3210
+ await session?.close({ keepWarm }).catch(() => void 0);
2930
3211
  }
2931
3212
  }
2932
3213
  })
@@ -2940,7 +3221,7 @@ async function runSessions(opts) {
2940
3221
  passed: failed === 0 && (opts.failUnder === void 0 || mean >= opts.failUnder)
2941
3222
  };
2942
3223
  }
2943
- var import_node_crypto2, READY_TIMEOUT_MS, READY_POLL_MS, CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep2, modelToolName, WorldSessionTimeout, WorldSessionCallError, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
3224
+ var import_node_crypto2, CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep4, modelToolName, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
2944
3225
  var init_session2 = __esm({
2945
3226
  "src/worlds/session.ts"() {
2946
3227
  "use strict";
@@ -2949,22 +3230,20 @@ var init_session2 = __esm({
2949
3230
  init_browser();
2950
3231
  init_nativeComputer();
2951
3232
  init_sessionContract();
3233
+ init_sessionReadiness();
2952
3234
  init_session();
2953
3235
  init_taskRef();
3236
+ init_worldNotice();
3237
+ init_grade();
2954
3238
  init_taskRef();
2955
- READY_TIMEOUT_MS = 10 * 6e4;
2956
- READY_POLL_MS = 2e3;
3239
+ init_sessionReadiness();
2957
3240
  CALL_TIMEOUT_MS = 3 * 6e4;
2958
3241
  CALL_POLL_MS = 400;
2959
3242
  JUDGED_GRADE_TIMEOUT_MS = 15 * 6e4;
2960
3243
  JUDGED_GRADE_POLL_MS = 2e3;
2961
3244
  EXPORT_PAGE = 500;
2962
- sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
3245
+ sleep4 = (ms) => new Promise((r) => setTimeout(r, ms));
2963
3246
  modelToolName = (name) => name.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64) || "tool";
2964
- WorldSessionTimeout = class extends Error {
2965
- };
2966
- WorldSessionCallError = class extends Error {
2967
- };
2968
3247
  WorldSession = class {
2969
3248
  constructor(sessionId, current, options) {
2970
3249
  this.sessionId = sessionId;
@@ -3082,18 +3361,17 @@ var init_session2 = __esm({
3082
3361
  }
3083
3362
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
3084
3363
  browserRecorder(agentId) {
3085
- const path16 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
3364
+ const path17 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
3086
3365
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${(0, import_node_crypto2.randomUUID)().replace(/-/g, "").slice(0, 8)}`;
3087
3366
  return new BrowserActionRecorder(
3088
3367
  async (events) => {
3089
- await apiRequest(this.options, "POST", path16, { events });
3368
+ await apiRequest(this.options, "POST", path17, { events });
3090
3369
  },
3091
3370
  id,
3092
3371
  {
3093
- // Continue this agent's timeline where the platform has it, so the
3094
- // same id reopened later never re-numbers from zero.
3372
+ // Continue this agent's timeline where the platform has it, so a reopened id never re-numbers from zero.
3095
3373
  resumeFrom: async () => {
3096
- const summary = await apiRequest(this.options, "GET", path16);
3374
+ const summary = await apiRequest(this.options, "GET", path17);
3097
3375
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
3098
3376
  }
3099
3377
  }
@@ -3121,29 +3399,22 @@ var init_session2 = __esm({
3121
3399
  tools: this.current.tools,
3122
3400
  surfaces: this.current.surfaces,
3123
3401
  task: this.current.task,
3402
+ deprecations: this.current.deprecations,
3124
3403
  ...fresh
3125
3404
  };
3126
3405
  return this.current;
3127
3406
  }
3128
3407
  /**
3129
- * Wait until the container is warm — meaning the process inside it has
3130
- * asked the platform for work, not merely that a job was accepted.
3408
+ * Wait until the session answers calls: a World Host holds it, or the
3409
+ * process inside a pod has asked the platform for work. Throws
3410
+ * `WorldSessionOpenFailed` (with the platform's reason) when it ended
3411
+ * first, and `WorldSessionTimeout` when `timeoutMs` (default ten minutes)
3412
+ * ran out while it was still starting.
3131
3413
  */
3132
3414
  async ready(opts = {}) {
3133
- const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
3134
- for (; ; ) {
3135
- const state2 = await this.status();
3136
- if (state2.ready) return state2;
3137
- if (state2.state === "FAILED" || state2.state === "STOPPED")
3138
- throw new WorldSessionCallError(
3139
- `World session ${state2.state.toLowerCase()} before it was ready.`
3140
- );
3141
- if (Date.now() > deadline)
3142
- throw new WorldSessionTimeout(
3143
- `World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
3144
- );
3145
- await sleep2(READY_POLL_MS);
3146
- }
3415
+ return new SessionReadiness(this.sessionId, () => this.status()).wait(
3416
+ opts.timeoutMs
3417
+ );
3147
3418
  }
3148
3419
  /** Enqueue one interaction and wait for the container's answer. */
3149
3420
  async exchange(body, timeoutMs) {
@@ -3173,7 +3444,7 @@ var init_session2 = __esm({
3173
3444
  throw new WorldSessionTimeout(
3174
3445
  judged ? `Grade ${callId} still ${call.state} after ${Math.round(budgetMs / 1e3)}s \u2014 the judge has not answered; the platform finalizes the call on its own, read it back with export().` : `Call ${callId} still ${call.state} after ${Math.round(budgetMs / 1e3)}s \u2014 the container may have died.`
3175
3446
  );
3176
- await sleep2(pollMs2);
3447
+ await sleep4(pollMs2);
3177
3448
  }
3178
3449
  }
3179
3450
  /**
@@ -3222,7 +3493,9 @@ var init_session2 = __esm({
3222
3493
  * calls); assertions and python graders ignore it. `worlds` names the
3223
3494
  * task's other sessions by alias (`{alias: sessionId}`): the platform
3224
3495
  * reads each one's evidence and hands it to the cross-world verifiers
3225
- * anchored on this session, whose grades come back under `verifiers`. */
3496
+ * anchored on this session, whose grades come back under `verifiers`. A
3497
+ * score that is not a number from 0 to 1 is refused by key and value
3498
+ * (`GradeRefused`); a verifier's is that verifier's `error`. */
3226
3499
  async grade(opts = {}) {
3227
3500
  const raw = await this.exchange(
3228
3501
  {
@@ -3232,14 +3505,7 @@ var init_session2 = __esm({
3232
3505
  },
3233
3506
  opts.timeoutMs
3234
3507
  );
3235
- const record = raw && typeof raw === "object" ? raw : {};
3236
- const verifiers = verifierGrades(record.verifiers);
3237
- return {
3238
- ...numericGrade(record),
3239
- ...typeof record.reason === "string" ? { reason: record.reason } : {},
3240
- ...verifiers ? { verifiers } : {},
3241
- raw
3242
- };
3508
+ return { ...GradePayload.read(raw), raw };
3243
3509
  }
3244
3510
  /**
3245
3511
  * Put rows into the LIVE world through its contract, in one batch:
@@ -3479,28 +3745,52 @@ var init_tasks = __esm({
3479
3745
  }
3480
3746
  });
3481
3747
 
3748
+ // src/worlds/surfaceDescriptor.ts
3749
+ function surfaceDescriptor(s) {
3750
+ return {
3751
+ surfaces: surfaceKinds(s),
3752
+ ...s.api ? { api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} } } : {},
3753
+ ...s.ui ? { ui: { url: s.ui.url, entry: uiEntryUrl(s.ui), apiPath: s.ui.apiPath ?? null } } : {},
3754
+ ...s.browser ? {
3755
+ browser: {
3756
+ wsUrl: s.browser.wsUrl,
3757
+ viewport: s.browser.viewport,
3758
+ ...s.browser.tier ? { tier: s.browser.tier } : {}
3759
+ }
3760
+ } : {}
3761
+ };
3762
+ }
3763
+ var init_surfaceDescriptor = __esm({
3764
+ "src/worlds/surfaceDescriptor.ts"() {
3765
+ "use strict";
3766
+ init_browser();
3767
+ }
3768
+ });
3769
+
3482
3770
  // src/worlds/taskSessions.ts
3483
3771
  function aggregateTaskGrade(worlds) {
3484
- const rewards = {};
3485
- const ungraded = [];
3486
- const graded = [];
3772
+ const fold = new TaskGradeFold();
3487
3773
  const verifiers = {};
3488
- const fold = (key2, grade) => {
3489
- if (typeof grade.reward === "number") {
3490
- rewards[key2] = grade.reward;
3491
- graded.push(grade.reward);
3492
- } else ungraded.push(key2);
3493
- for (const [name, value] of Object.entries(grade.rewards))
3494
- rewards[`${key2}.${name}`] = value;
3495
- };
3496
3774
  for (const [alias, grade] of Object.entries(worlds)) {
3497
- fold(alias, grade);
3775
+ fold.add(alias, grade, `world ${alias}`);
3498
3776
  Object.assign(verifiers, grade.verifiers ?? {});
3499
3777
  }
3500
3778
  for (const [name, grade] of Object.entries(verifiers))
3501
- fold(`${VERIFIER_REWARD_PREFIX}${name}`, grade);
3502
- const reward = graded.length > 0 ? graded.reduce((a, b) => a + b, 0) / graded.length : null;
3503
- return { reward, rewards, worlds, ungraded, verifiers };
3779
+ fold.add(`${VERIFIER_REWARD_PREFIX}${name}`, grade, `verifier ${name}`);
3780
+ return {
3781
+ reward: fold.reward(),
3782
+ rewards: fold.rewards,
3783
+ worlds,
3784
+ ungraded: fold.ungraded,
3785
+ verifiers,
3786
+ errors: fold.errors
3787
+ };
3788
+ }
3789
+ function taskGradeError(grade) {
3790
+ const lines = Object.entries(grade.errors).map(
3791
+ ([key2, why]) => `${key2}: ${why}`
3792
+ );
3793
+ return lines.length ? lines.join("; ") : null;
3504
3794
  }
3505
3795
  function fromResponse(answer, options) {
3506
3796
  const sessions2 = {};
@@ -3623,12 +3913,14 @@ async function attachManifest(manifest, options = {}) {
3623
3913
  function isTaskManifest(value) {
3624
3914
  return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.sessions === "object" && value.sessions !== null && !("instruction" in value && "worlds" in value);
3625
3915
  }
3626
- var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskSessions;
3916
+ var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskGradeFold, TaskSessions;
3627
3917
  var init_taskSessions = __esm({
3628
3918
  "src/worlds/taskSessions.ts"() {
3629
3919
  "use strict";
3630
3920
  init_worlds();
3631
3921
  init_browser();
3922
+ init_grade();
3923
+ init_surfaceDescriptor();
3632
3924
  init_session2();
3633
3925
  TASK_SESSIONS = "/api/public/world-task-sessions";
3634
3926
  VERIFIER_REWARD_PREFIX = "x.";
@@ -3640,19 +3932,38 @@ var init_taskSessions = __esm({
3640
3932
  sessionId: state2.sessionId,
3641
3933
  ready: state2.ready,
3642
3934
  state: state2.state,
3643
- surfaces: surfaceKinds(s),
3644
- ...s.api ? {
3645
- api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} }
3646
- } : {},
3647
- ...s.ui ? {
3648
- ui: {
3649
- url: s.ui.url,
3650
- entry: uiEntryUrl(s.ui),
3651
- apiPath: s.ui.apiPath ?? null
3652
- }
3653
- } : {},
3654
- ...s.browser ? { browser: { wsUrl: s.browser.wsUrl, viewport: s.browser.viewport } } : {}
3935
+ ...surfaceDescriptor(s)
3655
3936
  });
3937
+ TaskGradeFold = class {
3938
+ rewards = {};
3939
+ ungraded = [];
3940
+ errors = {};
3941
+ sources = /* @__PURE__ */ new Map();
3942
+ graded = [];
3943
+ add(key2, grade, source) {
3944
+ if (grade.error) this.errors[key2] = grade.error;
3945
+ if (typeof grade.reward === "number") {
3946
+ this.put(key2, grade.reward, source);
3947
+ this.graded.push(grade.reward);
3948
+ } else this.ungraded.push(key2);
3949
+ for (const [name, value] of Object.entries(grade.rewards))
3950
+ this.put(`${key2}.${name}`, value, source);
3951
+ }
3952
+ reward() {
3953
+ if (Object.keys(this.errors).length > 0 || !this.graded.length)
3954
+ return null;
3955
+ return this.graded.reduce((a, b) => a + b, 0) / this.graded.length;
3956
+ }
3957
+ put(key2, value, source) {
3958
+ const first = this.sources.get(key2) ?? source;
3959
+ this.sources.set(key2, first);
3960
+ if (first !== source) {
3961
+ this.errors[key2] = `two rewards are named ${key2} (${first} and ${source}); rename one of them`;
3962
+ return;
3963
+ }
3964
+ this.rewards[key2] = value;
3965
+ }
3966
+ };
3656
3967
  TaskSessions = class {
3657
3968
  constructor(taskId, taskVersion, name, instruction, sessions2, worlds, hash = null) {
3658
3969
  this.taskId = taskId;
@@ -3731,11 +4042,13 @@ var init_taskSessions = __esm({
3731
4042
  * agent's own account of the whole investigation) goes to every world's
3732
4043
  * judge; a world graded by assertions or python ignores it. Each grade
3733
4044
  * names the task's other sessions by alias, so the cross-world verifiers
3734
- * anchored on that world can read them. */
4045
+ * anchored on that world can read them. A world whose grade call fails is
4046
+ * that world's error: the task's grade names it and keeps every other
4047
+ * world's grade. */
3735
4048
  async grade(opts = {}) {
3736
4049
  return aggregateTaskGrade(
3737
4050
  await this.each(
3738
- (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) })
4051
+ (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) }).catch((error) => GradePayload.failedCall(error))
3739
4052
  )
3740
4053
  );
3741
4054
  }
@@ -3783,12 +4096,16 @@ var init_evals = __esm({
3783
4096
  "src/worlds/evals.ts"() {
3784
4097
  "use strict";
3785
4098
  init_worlds();
4099
+ init_transport();
3786
4100
  EVALUATIONS = "/api/public/benchmark-containers/evaluations";
3787
4101
  GatewayEvalsClient = class {
3788
- constructor(options = {}) {
4102
+ constructor(options = {}, retry = RetryPolicy.FILING) {
3789
4103
  this.options = options;
4104
+ this.retry = retry;
3790
4105
  }
3791
4106
  options;
4107
+ retry;
4108
+ keyed = /* @__PURE__ */ new Set();
3792
4109
  async createEvaluation(versionId, opts = {}) {
3793
4110
  const body = { versionId };
3794
4111
  if (opts.model !== void 0) body.model = opts.model;
@@ -3796,12 +4113,18 @@ var init_evals = __esm({
3796
4113
  if (opts.rolloutsPerExample !== void 0)
3797
4114
  body.rolloutsPerExample = opts.rolloutsPerExample;
3798
4115
  if (opts.metadata !== void 0) body.metadata = opts.metadata;
4116
+ if (opts.idempotencyKey !== void 0)
4117
+ body.idempotencyKey = opts.idempotencyKey;
3799
4118
  const answer = await apiRequest(
3800
4119
  this.options,
3801
4120
  "POST",
3802
4121
  EVALUATIONS,
3803
- body
4122
+ body,
4123
+ {
4124
+ retry: opts.idempotencyKey === void 0 ? RetryPolicy.NONE : this.retry
4125
+ }
3804
4126
  );
4127
+ if (opts.idempotencyKey !== void 0) this.keyed.add(answer.evaluationId);
3805
4128
  return answer.evaluationId;
3806
4129
  }
3807
4130
  async pushSamples(evaluationId, samples) {
@@ -3809,7 +4132,8 @@ var init_evals = __esm({
3809
4132
  this.options,
3810
4133
  "POST",
3811
4134
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/samples`,
3812
- { samples }
4135
+ { samples },
4136
+ { retry: this.retryFor(evaluationId) }
3813
4137
  );
3814
4138
  return answer.inserted ?? samples.length;
3815
4139
  }
@@ -3822,10 +4146,347 @@ var init_evals = __esm({
3822
4146
  this.options,
3823
4147
  "POST",
3824
4148
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/finalize`,
3825
- body
4149
+ body,
4150
+ { retry: this.retryFor(evaluationId) }
3826
4151
  );
3827
4152
  return answer.status;
3828
4153
  }
4154
+ retryFor(evaluationId) {
4155
+ return this.keyed.has(evaluationId) ? this.retry : RetryPolicy.NONE;
4156
+ }
4157
+ };
4158
+ }
4159
+ });
4160
+
4161
+ // src/worlds/runSignals.ts
4162
+ var import_node_os, OWNER, RunSignals;
4163
+ var init_runSignals = __esm({
4164
+ "src/worlds/runSignals.ts"() {
4165
+ "use strict";
4166
+ import_node_os = require("os");
4167
+ OWNER = /* @__PURE__ */ Symbol("RunSignals");
4168
+ RunSignals = class _RunSignals {
4169
+ constructor(target = process) {
4170
+ this.target = target;
4171
+ }
4172
+ target;
4173
+ static SIGNALS = ["SIGINT", "SIGTERM"];
4174
+ cleanups = [];
4175
+ heardSignal = null;
4176
+ heardCount = 0;
4177
+ listening = false;
4178
+ left = false;
4179
+ listener = Object.assign(
4180
+ (signal) => this.heard(signal),
4181
+ { [OWNER]: this }
4182
+ );
4183
+ /** The signal that arrived, once one has: start nothing new. */
4184
+ get signal() {
4185
+ return this.owner().heardSignal;
4186
+ }
4187
+ async during(cleanup, work) {
4188
+ return this.owner().hold(cleanup, work);
4189
+ }
4190
+ owner() {
4191
+ const owners = this.target.listeners("SIGINT").map((listener) => listener[OWNER]);
4192
+ return owners.find((owner) => owner !== void 0) ?? this;
4193
+ }
4194
+ async hold(cleanup, work) {
4195
+ this.listen();
4196
+ this.cleanups.push(cleanup);
4197
+ try {
4198
+ return await work();
4199
+ } finally {
4200
+ this.cleanups.splice(this.cleanups.indexOf(cleanup), 1);
4201
+ if (!this.cleanups.length && this.heardSignal === null) this.unlisten();
4202
+ }
4203
+ }
4204
+ heard(signal) {
4205
+ this.heardCount += 1;
4206
+ if (this.heardCount > 2) return this.leave(signal);
4207
+ this.heardSignal ??= signal;
4208
+ void this.cleanUp(signal, this.heardCount === 2).finally(() => this.leave(signal));
4209
+ }
4210
+ async cleanUp(signal, urgent) {
4211
+ for (const cleanup of [...this.cleanups].reverse())
4212
+ await Promise.resolve().then(() => cleanup(signal, urgent)).catch(() => void 0);
4213
+ }
4214
+ leave(signal) {
4215
+ if (this.left) return;
4216
+ this.left = true;
4217
+ this.unlisten();
4218
+ if (this.target.listenerCount(signal) === 0)
4219
+ this.target.exit(128 + import_node_os.constants.signals[signal]);
4220
+ }
4221
+ listen() {
4222
+ if (this.listening) return;
4223
+ this.listening = true;
4224
+ for (const signal of _RunSignals.SIGNALS)
4225
+ this.target.on(signal, this.listener);
4226
+ }
4227
+ unlisten() {
4228
+ this.listening = false;
4229
+ for (const signal of _RunSignals.SIGNALS)
4230
+ this.target.off(signal, this.listener);
4231
+ }
4232
+ };
4233
+ }
4234
+ });
4235
+
4236
+ // src/worlds/taskRunDir.ts
4237
+ function sessionRecord({ api: api2, ui, browser, ...session }) {
4238
+ return {
4239
+ ...session,
4240
+ ...api2 ? { api: { url: api2.url } } : {},
4241
+ ...ui ? { ui: { ...ui, entry: withoutUiToken(ui.entry) } } : {},
4242
+ ...browser ? { browser: browserRecord(browser) } : {}
4243
+ };
4244
+ }
4245
+ var fs, path, TaskRunDir, browserRecord;
4246
+ var init_taskRunDir = __esm({
4247
+ "src/worlds/taskRunDir.ts"() {
4248
+ "use strict";
4249
+ fs = __toESM(require("fs"));
4250
+ path = __toESM(require("path"));
4251
+ init_browser();
4252
+ TaskRunDir = class {
4253
+ constructor(path17) {
4254
+ this.path = path17;
4255
+ }
4256
+ path;
4257
+ writeManifest(manifest) {
4258
+ this.writeJson("manifest.json", {
4259
+ ...manifest,
4260
+ sessions: Object.fromEntries(
4261
+ Object.entries(manifest.sessions).map(([alias, session]) => [
4262
+ alias,
4263
+ sessionRecord(session)
4264
+ ])
4265
+ )
4266
+ });
4267
+ }
4268
+ writeJson(name, value) {
4269
+ this.write(name, JSON.stringify(value, null, 2));
4270
+ }
4271
+ write(name, text2) {
4272
+ fs.mkdirSync(this.path, { recursive: true });
4273
+ fs.writeFileSync(path.join(this.path, name), text2);
4274
+ }
4275
+ };
4276
+ browserRecord = ({ viewport, tier }) => ({
4277
+ viewport,
4278
+ ...tier ? { tier } : {}
4279
+ });
4280
+ }
4281
+ });
4282
+
4283
+ // src/worlds/taskRunOutcome.ts
4284
+ var TaskRunOutcome;
4285
+ var init_taskRunOutcome = __esm({
4286
+ "src/worlds/taskRunOutcome.ts"() {
4287
+ "use strict";
4288
+ init_grade();
4289
+ init_taskSessions();
4290
+ TaskRunOutcome = class {
4291
+ constructor(runId, live, model, runDir) {
4292
+ this.runId = runId;
4293
+ this.live = live;
4294
+ this.model = model;
4295
+ this.runDir = runDir;
4296
+ }
4297
+ runId;
4298
+ live;
4299
+ model;
4300
+ runDir;
4301
+ report = null;
4302
+ extra = {};
4303
+ notes = {};
4304
+ agentError = null;
4305
+ grade = null;
4306
+ traced = false;
4307
+ runError = null;
4308
+ fileError = null;
4309
+ evaluationId;
4310
+ evaluationStatus = null;
4311
+ started = Date.now();
4312
+ /** The run stopped short of a grade; the first reason stands. */
4313
+ stopped(error) {
4314
+ this.runError ??= messageOf(error).slice(0, 2e3);
4315
+ }
4316
+ agentRaised(error) {
4317
+ this.agentError = messageOf(error).slice(0, 2e3);
4318
+ }
4319
+ /** Every reason the run failed, on one line: the evaluation's
4320
+ * `metrics.error`. A run that never reached a grade has failed whatever
4321
+ * its reasons say. */
4322
+ failure() {
4323
+ const reasons = [
4324
+ this.agentError === null ? null : `agent raised: ${this.agentError}`,
4325
+ this.runError === null ? null : `the run stopped: ${this.runError}`,
4326
+ this.gradeError(),
4327
+ this.grade === null && this.runError === null ? "the run stopped before it was graded" : null
4328
+ ].filter((reason) => reason !== null);
4329
+ return reasons.length ? reasons.join("; ") : null;
4330
+ }
4331
+ async file(evals, setRunId) {
4332
+ let created;
4333
+ try {
4334
+ created = await evals.createEvaluation(this.ref(this.live.aliases[0]).versionId, {
4335
+ model: this.model,
4336
+ rolloutsPerExample: 1,
4337
+ idempotencyKey: this.runId,
4338
+ ...setRunId ? { runId: setRunId } : {},
4339
+ metadata: this.metadata()
4340
+ });
4341
+ await evals.pushSamples(created, [this.sample()]);
4342
+ this.evaluationStatus = await evals.finalizeEvaluation(created, this.finalized());
4343
+ this.evaluationId = created;
4344
+ } catch (error) {
4345
+ this.fileError = created ? `evaluation ${created}: ${messageOf(error)}` : messageOf(error);
4346
+ if (created)
4347
+ await evals.finalizeEvaluation(created, { status: "FAILED", metrics: { error: this.fileError } }).catch(() => void 0);
4348
+ }
4349
+ }
4350
+ summary() {
4351
+ const live = this.live;
4352
+ const { evaluationId: _agentEvaluationId, ...extra } = this.extra;
4353
+ return {
4354
+ ...extra,
4355
+ runId: this.runId,
4356
+ taskId: live.taskId,
4357
+ taskVersion: live.taskVersion,
4358
+ name: live.name,
4359
+ instruction: live.instruction ?? "",
4360
+ model: this.model,
4361
+ aliases: live.aliases,
4362
+ reward: this.grade?.reward ?? null,
4363
+ rewards: this.grade?.rewards ?? {},
4364
+ ungraded: this.grade?.ungraded ?? live.aliases,
4365
+ agentError: this.agentError,
4366
+ runError: this.runError,
4367
+ gradeError: this.gradeError(),
4368
+ fileError: this.fileError,
4369
+ evaluationStatus: this.evaluationStatus,
4370
+ report: this.report,
4371
+ traced: this.traced,
4372
+ runDir: this.runDir,
4373
+ durationSeconds: Math.round((Date.now() - this.started) / 10) / 100,
4374
+ notes: this.notes,
4375
+ filed: this.evaluationId !== void 0,
4376
+ ...this.evaluationId ? { evaluationId: this.evaluationId } : {}
4377
+ };
4378
+ }
4379
+ gradeError() {
4380
+ return this.grade ? taskGradeError(this.grade) : null;
4381
+ }
4382
+ ref(alias) {
4383
+ const { slug, ref } = this.live.worldRef(alias);
4384
+ const session = this.live.sessions[alias];
4385
+ return { slug, ref, versionId: session.versionId, sessionId: session.sessionId };
4386
+ }
4387
+ metadata() {
4388
+ const live = this.live;
4389
+ return {
4390
+ source: "worlds-task-run",
4391
+ task: { id: live.taskId, version: live.taskVersion, name: live.name, hash: live.hash },
4392
+ anchor: live.aliases[0],
4393
+ worlds: Object.fromEntries(live.aliases.map((alias) => [alias, this.ref(alias)])),
4394
+ runId: this.runId
4395
+ };
4396
+ }
4397
+ sample() {
4398
+ const live = this.live;
4399
+ const reward = this.grade?.reward ?? null;
4400
+ const gradeError = this.gradeError();
4401
+ const sample = {
4402
+ exampleId: live.name ?? live.taskId ?? this.runId,
4403
+ task: live.name ?? live.taskId ?? this.runId,
4404
+ rolloutIndex: 0,
4405
+ prompt: live.instruction,
4406
+ ...this.report === null ? {} : { completion: this.report },
4407
+ rewards: this.grade?.rewards ?? {},
4408
+ info: {
4409
+ sessionId: this.runId,
4410
+ worlds: Object.fromEntries(
4411
+ live.aliases.map((alias) => [
4412
+ alias,
4413
+ { ...this.ref(alias), reward: this.grade?.worlds[alias]?.reward ?? null }
4414
+ ])
4415
+ ),
4416
+ ungraded: this.grade?.ungraded ?? live.aliases,
4417
+ traced: this.traced,
4418
+ runDir: this.runDir,
4419
+ ...this.agentError ? { agentError: this.agentError } : {},
4420
+ ...this.runError ? { runError: this.runError } : {},
4421
+ ...gradeError ? { verifyError: gradeError } : {}
4422
+ }
4423
+ };
4424
+ if (typeof reward === "number")
4425
+ Object.assign(sample, { reward, score: reward, correct: reward >= 1 });
4426
+ return sample;
4427
+ }
4428
+ finalized() {
4429
+ const reward = this.grade?.reward ?? null;
4430
+ const failure = this.failure();
4431
+ return {
4432
+ status: failure ? "FAILED" : "COMPLETED",
4433
+ metrics: {
4434
+ ...typeof reward === "number" ? { avg_reward: reward } : {},
4435
+ reward,
4436
+ ...Object.fromEntries(
4437
+ Object.entries(this.grade?.rewards ?? {}).map(([key2, value]) => [`rewards.${key2}`, value])
4438
+ ),
4439
+ ...failure ? { error: failure } : {}
4440
+ }
4441
+ };
4442
+ }
4443
+ };
4444
+ }
4445
+ });
4446
+
4447
+ // src/tracing/prerequisites.ts
4448
+ function getEnv(key2) {
4449
+ const value = process.env[key2];
4450
+ return value ? value : void 0;
4451
+ }
4452
+ function isGatewayConfigured() {
4453
+ return !!(getEnv(GATEWAY_ENV.HOST) && getEnv(GATEWAY_ENV.PUBLIC_KEY) && getEnv(GATEWAY_ENV.SECRET_KEY));
4454
+ }
4455
+ var GATEWAY_ENV, OTLP_TRACES_PATH, OPENTELEMETRY_PEERS, PINNED_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
4456
+ var init_prerequisites = __esm({
4457
+ "src/tracing/prerequisites.ts"() {
4458
+ "use strict";
4459
+ GATEWAY_ENV = {
4460
+ HOST: "GATEWAY_HOST",
4461
+ PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
4462
+ SECRET_KEY: "GATEWAY_SECRET_KEY",
4463
+ API_KEY: "GATEWAY_API_KEY",
4464
+ ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
4465
+ };
4466
+ OTLP_TRACES_PATH = "/api/public/otel/v1/traces";
4467
+ OPENTELEMETRY_PEERS = {
4468
+ "@opentelemetry/api": "1.9.0",
4469
+ "@opentelemetry/exporter-trace-otlp-http": "0.222.0",
4470
+ "@opentelemetry/resources": "2.11.0",
4471
+ "@opentelemetry/sdk-trace-base": "2.11.0",
4472
+ "@opentelemetry/sdk-trace-node": "2.11.0"
4473
+ };
4474
+ PINNED_PEERS = Object.entries(OPENTELEMETRY_PEERS).map(([name, version]) => `${name}@${version}`).join(" ");
4475
+ MODULE_NOT_FOUND = /* @__PURE__ */ new Set(["MODULE_NOT_FOUND", "ERR_MODULE_NOT_FOUND"]);
4476
+ OpenTelemetryMissing = class _OpenTelemetryMissing extends Error {
4477
+ constructor() {
4478
+ super(
4479
+ `OpenTelemetry packages not found. Install them next to @withgateway/sdk, at the versions it is tested with:
4480
+ npm install ${PINNED_PEERS}
4481
+ npm install -g ${PINNED_PEERS} (the gateway CLI, when it was installed with npm install -g)`
4482
+ );
4483
+ this.name = "OpenTelemetryMissing";
4484
+ }
4485
+ static explains(error) {
4486
+ if (error instanceof _OpenTelemetryMissing) return true;
4487
+ const { code, message } = error ?? {};
4488
+ return MODULE_NOT_FOUND.has(String(code)) && String(message).includes("@opentelemetry/");
4489
+ }
3829
4490
  };
3830
4491
  }
3831
4492
  });
@@ -4130,7 +4791,7 @@ var require_package = __commonJS({
4130
4791
  "package.json"(exports2, module2) {
4131
4792
  module2.exports = {
4132
4793
  name: "@withgateway/sdk",
4133
- version: "0.3.0",
4794
+ version: "0.3.3",
4134
4795
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
4135
4796
  main: "./dist/index.js",
4136
4797
  module: "./dist/index.mjs",
@@ -4626,10 +5287,6 @@ __export(init_exports, {
4626
5287
  isInitialized: () => isInitialized,
4627
5288
  shutdown: () => shutdown2
4628
5289
  });
4629
- function getEnv(key2) {
4630
- const value = process.env[key2];
4631
- return value ? value : void 0;
4632
- }
4633
5290
  function log(message, ...args) {
4634
5291
  if (state.debug) {
4635
5292
  console.log(`[gatewaysdk.tracing] ${message}`, ...args);
@@ -4639,10 +5296,10 @@ function warn(message, ...args) {
4639
5296
  console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
4640
5297
  }
4641
5298
  function buildProvider(config, logFn) {
4642
- const host = config.host ?? getEnv(ENV_VARS.HOST);
4643
- const publicKey = config.publicKey ?? getEnv(ENV_VARS.PUBLIC_KEY);
4644
- const secretKey = config.secretKey ?? getEnv(ENV_VARS.SECRET_KEY);
4645
- const endpoint = config.endpoint ?? getEnv(ENV_VARS.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${DEFAULT_OTLP_PATH}` : void 0);
5299
+ const host = config.host ?? getEnv(GATEWAY_ENV.HOST);
5300
+ const publicKey = config.publicKey ?? getEnv(GATEWAY_ENV.PUBLIC_KEY);
5301
+ const secretKey = config.secretKey ?? getEnv(GATEWAY_ENV.SECRET_KEY);
5302
+ const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${OTLP_TRACES_PATH}` : void 0);
4646
5303
  const missing = [];
4647
5304
  if (!host) missing.push("host (or GATEWAY_HOST env var)");
4648
5305
  if (!publicKey) missing.push("publicKey (or GATEWAY_PUBLIC_KEY env var)");
@@ -4679,9 +5336,7 @@ function buildProvider(config, logFn) {
4679
5336
  ATTR_SERVICE_NAME = "service.name";
4680
5337
  }
4681
5338
  } catch {
4682
- throw new Error(
4683
- "OpenTelemetry packages not found. Install them with:\nnpm install @opentelemetry/sdk-trace-node @opentelemetry/sdk-trace-base @opentelemetry/exporter-trace-otlp-http @opentelemetry/resources"
4684
- );
5339
+ throw new OpenTelemetryMissing();
4685
5340
  }
4686
5341
  const resourceAttrs = {
4687
5342
  [ATTR_SERVICE_NAME]: config.serviceName ?? "gatewaysdk-tracing"
@@ -4887,13 +5542,7 @@ async function shutdown2() {
4887
5542
  function isInitialized() {
4888
5543
  return state.initialized;
4889
5544
  }
4890
- function isGatewayConfigured() {
4891
- const host = getEnv(ENV_VARS.HOST);
4892
- const publicKey = getEnv(ENV_VARS.PUBLIC_KEY);
4893
- const secretKey = getEnv(ENV_VARS.SECRET_KEY);
4894
- return !!(host && publicKey && secretKey);
4895
- }
4896
- var ENV_VARS, DEFAULT_OTLP_PATH, state;
5545
+ var state;
4897
5546
  var init_init = __esm({
4898
5547
  "src/tracing/init.ts"() {
4899
5548
  "use strict";
@@ -4902,13 +5551,8 @@ var init_init = __esm({
4902
5551
  init_session();
4903
5552
  init_scores();
4904
5553
  init_build();
4905
- ENV_VARS = {
4906
- HOST: "GATEWAY_HOST",
4907
- PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
4908
- SECRET_KEY: "GATEWAY_SECRET_KEY",
4909
- ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
4910
- };
4911
- DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
5554
+ init_prerequisites();
5555
+ init_prerequisites();
4912
5556
  state = {
4913
5557
  initialized: false,
4914
5558
  provider: null
@@ -4916,86 +5560,95 @@ var init_init = __esm({
4916
5560
  }
4917
5561
  });
4918
5562
 
4919
- // src/worlds/taskRun.ts
4920
- function worldRef(live, alias) {
4921
- const { slug, ref } = live.worldRef(alias);
4922
- const session = live.sessions[alias];
4923
- return {
4924
- slug,
4925
- ref,
4926
- versionId: session.versionId,
4927
- sessionId: session.sessionId
4928
- };
4929
- }
4930
- async function fileTaskRun(live, runId, model, report, grade, agentError, traced, runDir, options, setRunId) {
4931
- const aliases = live.aliases;
4932
- const anchor = aliases[0];
4933
- const worldsMeta = Object.fromEntries(
4934
- aliases.map((alias) => [alias, worldRef(live, alias)])
4935
- );
4936
- const evals = new GatewayEvalsClient(options);
4937
- const evaluationId = await evals.createEvaluation(
4938
- worldRef(live, anchor).versionId,
4939
- {
4940
- model,
4941
- rolloutsPerExample: 1,
4942
- ...setRunId ? { runId: setRunId } : {},
4943
- metadata: {
4944
- source: "worlds-task-run",
4945
- task: {
4946
- id: live.taskId,
4947
- version: live.taskVersion,
4948
- name: live.name,
4949
- hash: live.hash
4950
- },
4951
- anchor,
4952
- worlds: worldsMeta,
4953
- runId
5563
+ // src/worlds/taskTracing.ts
5564
+ var HOST_WITH_AUTHORITY, TRACING_HELD_ELSEWHERE, TaskTracing;
5565
+ var init_taskTracing = __esm({
5566
+ "src/worlds/taskTracing.ts"() {
5567
+ "use strict";
5568
+ init_prerequisites();
5569
+ init_platformEndpoint();
5570
+ HOST_WITH_AUTHORITY = /^[a-z][a-z0-9+.-]*:\/\/[^/\\?#@\s]+(\/|$)/i;
5571
+ TRACING_HELD_ELSEWHERE = "Tracing in this process already sends spans to another destination or with other keys (an earlier run, or the agent's own tracing.init()), so this run's spans would not reach its own project.";
5572
+ TaskTracing = class _TaskTracing {
5573
+ constructor(load = () => Promise.resolve().then(() => (init_init(), init_exports))) {
5574
+ this.load = load;
5575
+ }
5576
+ load;
5577
+ static warned = false;
5578
+ static target = null;
5579
+ async start(requested, options) {
5580
+ if (!requested) return false;
5581
+ const endpoint = platformEndpoint(options);
5582
+ try {
5583
+ await this.initOnce(_TaskTracing.config(endpoint));
5584
+ return true;
5585
+ } catch (error) {
5586
+ _TaskTracing.warnOnce(_TaskTracing.reason(error, endpoint));
5587
+ return false;
5588
+ }
4954
5589
  }
4955
- }
4956
- );
4957
- const reward = grade.reward;
4958
- const sample = {
4959
- exampleId: live.name ?? live.taskId ?? runId,
4960
- task: live.name ?? live.taskId ?? runId,
4961
- rolloutIndex: 0,
4962
- prompt: live.instruction,
4963
- ...report === null ? {} : { completion: report },
4964
- rewards: grade.rewards,
4965
- info: {
4966
- sessionId: runId,
4967
- worlds: Object.fromEntries(
4968
- aliases.map((alias) => [
4969
- alias,
4970
- {
4971
- ...worldRef(live, alias),
4972
- reward: grade.worlds[alias]?.reward ?? null
4973
- }
4974
- ])
4975
- ),
4976
- ungraded: grade.ungraded,
4977
- traced,
4978
- runDir,
4979
- ...agentError ? { agentError } : {}
4980
- }
4981
- };
4982
- if (typeof reward === "number") {
4983
- sample.reward = reward;
4984
- sample.score = reward;
4985
- sample.correct = reward >= 1;
4986
- }
4987
- await evals.pushSamples(evaluationId, [sample]);
4988
- await evals.finalizeEvaluation(evaluationId, {
4989
- status: agentError ? "FAILED" : "COMPLETED",
4990
- metrics: {
4991
- ...typeof reward === "number" ? { avg_reward: reward } : {},
4992
- reward,
4993
- ...Object.fromEntries(
4994
- Object.entries(grade.rewards).map(([k, v]) => [`rewards.${k}`, v])
4995
- )
4996
- }
4997
- });
4998
- return evaluationId;
5590
+ // init() keeps the first provider for the whole process and ignores later calls, so a later run is
5591
+ // traced only when it exports to the same place with the same keys.
5592
+ async initOnce(config) {
5593
+ const tracing2 = await this.load();
5594
+ const target = JSON.stringify([config.endpoint, config.publicKey, config.secretKey]);
5595
+ if (!tracing2.isInitialized()) {
5596
+ tracing2.init(config);
5597
+ _TaskTracing.target = target;
5598
+ return;
5599
+ }
5600
+ if (_TaskTracing.target !== target) throw new Error(TRACING_HELD_ELSEWHERE);
5601
+ }
5602
+ static config(endpoint) {
5603
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
5604
+ return { ...endpoint, endpoint: _TaskTracing.destination(endpoint) };
5605
+ }
5606
+ // B1: a saved login's keys go to their own host only. The redirect is compared as text, never parsed:
5607
+ // a fixed "<host>/" prefix leaves no URL parser room to read another host into it.
5608
+ static destination(endpoint) {
5609
+ const redirect = getEnv(GATEWAY_ENV.ENDPOINT);
5610
+ if (!redirect) return `${endpoint.host}${OTLP_TRACES_PATH}`;
5611
+ if (_TaskTracing.underHost(redirect, endpoint.host) || _TaskTracing.environmentKeys(endpoint)) return redirect;
5612
+ throw new Error(
5613
+ `GATEWAY_OTLP_ENDPOINT is not under ${endpoint.host}, and this run's keys are not GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY: they are sent to ${endpoint.host} only. Unset GATEWAY_OTLP_ENDPOINT, or set both keys in the environment.`
5614
+ );
5615
+ }
5616
+ static underHost(redirect, host) {
5617
+ return HOST_WITH_AUTHORITY.test(host) && redirect.startsWith(`${host}/`);
5618
+ }
5619
+ static environmentKeys(endpoint) {
5620
+ const secretKey = getEnv(GATEWAY_ENV.SECRET_KEY) ?? getEnv(GATEWAY_ENV.API_KEY);
5621
+ return endpoint.publicKey === getEnv(GATEWAY_ENV.PUBLIC_KEY) && endpoint.secretKey === secretKey;
5622
+ }
5623
+ static warnOnce(reason) {
5624
+ if (_TaskTracing.warned) return;
5625
+ _TaskTracing.warned = true;
5626
+ console.warn(
5627
+ `[gatewaysdk.tracing] Task runs in this process are not traced. ${reason}
5628
+ Pass --no-trace (trace: false) to run untraced without this warning.`
5629
+ );
5630
+ }
5631
+ static reason(error, endpoint) {
5632
+ if (OpenTelemetryMissing.explains(error)) return new OpenTelemetryMissing().message;
5633
+ const message = error instanceof Error ? error.message : String(error);
5634
+ const keys = endpoint ? [endpoint.publicKey, endpoint.secretKey] : [];
5635
+ return keys.reduce((text2, key2) => text2.split(key2).join("[redacted]"), message);
5636
+ }
5637
+ };
5638
+ }
5639
+ });
5640
+
5641
+ // src/worlds/taskRun.ts
5642
+ function taskRunFailure(summary) {
5643
+ if (summary.agentError !== null) return `agent raised: ${summary.agentError}`;
5644
+ if (summary.runError !== null) return `the run stopped: ${summary.runError}`;
5645
+ if (summary.gradeError !== null)
5646
+ return `the task could not be graded: ${summary.gradeError}`;
5647
+ if (summary.fileError !== null)
5648
+ return `the run could not be filed: ${summary.fileError}`;
5649
+ if (summary.filed && summary.evaluationStatus !== "COMPLETED")
5650
+ return `the platform settled evaluation ${summary.evaluationId} as ${summary.evaluationStatus}`;
5651
+ return null;
4999
5652
  }
5000
5653
  function reportFromResult(result) {
5001
5654
  if (result === void 0 || result === null)
@@ -5013,123 +5666,49 @@ function reportFromResult(result) {
5013
5666
  );
5014
5667
  return { report, extra };
5015
5668
  }
5669
+ async function refuseUnreadTools(live) {
5670
+ try {
5671
+ for (const alias of live.aliases)
5672
+ WorldToolsUnread.refuse(`${live.worldRef(alias).slug} (alias ${alias})`, live.session(alias).deprecations);
5673
+ } catch (error) {
5674
+ await live.close({ keepWarm: false }).catch(() => void 0);
5675
+ throw error;
5676
+ }
5677
+ }
5016
5678
  async function runTask(ref, agent, opts) {
5017
5679
  const runId = `wtr-${(0, import_node_crypto4.randomBytes)(6).toString("hex")}`;
5018
- const outDir = opts.outDir ?? "runs";
5019
- const runDir = path.join(outDir, runId);
5020
- fs.mkdirSync(runDir, { recursive: true });
5021
5680
  const options = opts.options ?? {};
5022
- const live = await openTask(ref, {
5023
- surfaces: opts.surfaces,
5024
- reuseWarm: opts.reuseWarm,
5025
- browserTier: opts.browserTier,
5026
- options
5027
- });
5028
- const started = Date.now();
5029
- try {
5030
- await live.ready();
5031
- let traced = false;
5032
- if (opts.trace !== false) {
5033
- try {
5034
- const { isGatewayConfigured: isGatewayConfigured2, init: init3 } = await Promise.resolve().then(() => (init_init(), init_exports));
5035
- if (isGatewayConfigured2()) {
5036
- init3();
5037
- traced = true;
5038
- }
5039
- } catch {
5040
- traced = false;
5041
- }
5042
- }
5043
- const taskRun = new TaskRun(
5044
- runId,
5045
- live.instruction ?? "",
5046
- opts.model,
5047
- live.sessions,
5048
- await live.manifest()
5049
- );
5050
- let agentError = null;
5051
- let report = null;
5052
- let extra = {};
5053
- try {
5054
- const invoke = () => agent(taskRun);
5055
- const result = traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(runId, invoke) : await invoke();
5056
- ({ report, extra } = reportFromResult(result));
5057
- } catch (error) {
5058
- const message = (error instanceof Error ? error.message : String(error)) || "Unknown error";
5059
- agentError = message.slice(0, 2e3);
5060
- }
5061
- if (traced)
5062
- await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing2) => tracing2.flush()).catch(() => void 0);
5063
- const grade = report !== null ? await live.grade({ report }) : await live.grade();
5064
- fs.writeFileSync(
5065
- path.join(runDir, "manifest.json"),
5066
- JSON.stringify(taskRun.manifest, null, 2)
5067
- );
5068
- fs.writeFileSync(
5069
- path.join(runDir, "grades.json"),
5070
- JSON.stringify(grade, null, 2)
5071
- );
5072
- if (report) fs.writeFileSync(path.join(runDir, "report.md"), report);
5073
- const summary = {
5074
- runId,
5075
- taskId: live.taskId,
5076
- taskVersion: live.taskVersion,
5077
- name: live.name,
5078
- instruction: taskRun.instruction,
5079
- model: opts.model,
5080
- aliases: live.aliases,
5081
- reward: grade.reward,
5082
- rewards: grade.rewards,
5083
- ungraded: grade.ungraded,
5084
- agentError,
5085
- report,
5086
- traced,
5087
- runDir,
5088
- durationSeconds: Math.round((Date.now() - started) / 10) / 100,
5089
- notes: taskRun.notes,
5090
- filed: false,
5091
- ...extra
5092
- };
5093
- if (opts.file !== false) {
5094
- summary.evaluationId = await fileTaskRun(
5095
- live,
5096
- runId,
5097
- opts.model,
5098
- report,
5099
- grade,
5100
- agentError,
5101
- traced,
5102
- runDir,
5103
- options,
5104
- opts.setRunId
5105
- );
5106
- summary.filed = true;
5107
- }
5108
- fs.writeFileSync(
5109
- path.join(runDir, "run.json"),
5110
- JSON.stringify(summary, null, 2)
5111
- );
5112
- return summary;
5113
- } finally {
5114
- if (!opts.keepUp) {
5115
- try {
5116
- await live.close({ keepWarm: false });
5117
- } catch {
5118
- }
5119
- }
5120
- }
5681
+ const opening = new TaskOpening(
5682
+ openTask(ref, {
5683
+ surfaces: opts.surfaces,
5684
+ reuseWarm: opts.reuseWarm,
5685
+ browserTier: opts.browserTier,
5686
+ options
5687
+ }).then(async (live) => {
5688
+ await refuseUnreadTools(live);
5689
+ return new LiveTaskRun(runId, live, opts, options);
5690
+ })
5691
+ );
5692
+ return new RunSignals().during(
5693
+ (signal, urgent) => opening.interrupt(signal, urgent),
5694
+ () => opening.complete(agent)
5695
+ );
5121
5696
  }
5122
- var import_node_crypto4, fs, path, TaskRun, describeValue, RESULT_CONTRACT;
5697
+ var import_node_crypto4, path2, TaskRun, FILING_UNFINISHED, describeValue, RESULT_CONTRACT, LiveTaskRun, TaskOpening;
5123
5698
  var init_taskRun = __esm({
5124
5699
  "src/worlds/taskRun.ts"() {
5125
5700
  "use strict";
5126
5701
  import_node_crypto4 = require("crypto");
5127
- fs = __toESM(require("fs"));
5128
- path = __toESM(require("path"));
5702
+ path2 = __toESM(require("path"));
5129
5703
  init_browser();
5130
5704
  init_evals();
5705
+ init_runSignals();
5131
5706
  init_session2();
5132
5707
  init_taskSessions();
5708
+ init_taskRunDir();
5709
+ init_taskRunOutcome();
5710
+ init_taskTracing();
5711
+ init_worldNotice();
5133
5712
  TaskRun = class {
5134
5713
  constructor(runId, instruction, model, sessions2, manifest) {
5135
5714
  this.runId = runId;
@@ -5192,8 +5771,127 @@ var init_taskRun = __esm({
5192
5771
  Object.assign(this.notes, notes);
5193
5772
  }
5194
5773
  };
5774
+ FILING_UNFINISHED = "the filing did not finish";
5195
5775
  describeValue = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
5196
5776
  RESULT_CONTRACT = "return a string report, an object {report?: string, ...} or nothing";
5777
+ LiveTaskRun = class {
5778
+ constructor(runId, live, opts, options) {
5779
+ this.live = live;
5780
+ this.opts = opts;
5781
+ this.options = options;
5782
+ this.files = new TaskRunDir(path2.join(opts.outDir ?? "runs", runId));
5783
+ this.outcome = new TaskRunOutcome(runId, live, opts.model, this.files.path);
5784
+ }
5785
+ live;
5786
+ opts;
5787
+ options;
5788
+ files;
5789
+ outcome;
5790
+ played = false;
5791
+ interrupted = false;
5792
+ filing = null;
5793
+ ending = null;
5794
+ async complete(agent) {
5795
+ try {
5796
+ await this.play(agent).catch((error) => this.outcome.stopped(error));
5797
+ this.played = true;
5798
+ await this.file();
5799
+ return this.record();
5800
+ } finally {
5801
+ await this.end();
5802
+ }
5803
+ }
5804
+ async interrupt(signal, urgent = false) {
5805
+ const sessions2 = this.opts.keepUp ? "leaving its sessions up" : "closing its sessions";
5806
+ const how = urgent ? " now, without waiting" : "";
5807
+ process.stderr.write(`${this.outcome.runId} interrupted by ${signal}; ${sessions2}${how}
5808
+ `);
5809
+ if (!this.played) this.outcome.stopped(`interrupted by ${signal}`);
5810
+ this.interrupted = true;
5811
+ try {
5812
+ if (!urgent) await this.filing;
5813
+ this.record();
5814
+ } catch (error) {
5815
+ const why = error instanceof Error ? error.message : String(error);
5816
+ process.stderr.write(`${this.outcome.runId}: run.json not written: ${why}
5817
+ `);
5818
+ } finally {
5819
+ await this.end();
5820
+ }
5821
+ }
5822
+ summary() {
5823
+ return this.outcome.summary();
5824
+ }
5825
+ async play(agent) {
5826
+ await this.live.ready();
5827
+ this.outcome.traced = await new TaskTracing().start(this.opts.trace !== false, this.options);
5828
+ const run = new TaskRun(
5829
+ this.outcome.runId,
5830
+ this.live.instruction ?? "",
5831
+ this.opts.model,
5832
+ this.live.sessions,
5833
+ await this.live.manifest()
5834
+ );
5835
+ this.outcome.notes = run.notes;
5836
+ this.files.writeManifest(run.manifest);
5837
+ await this.callAgent(agent, run);
5838
+ const report = this.outcome.report;
5839
+ this.outcome.grade = await this.live.grade(report !== null ? { report } : {});
5840
+ this.files.writeJson("grades.json", this.outcome.grade);
5841
+ if (report) this.files.write("report.md", report);
5842
+ }
5843
+ async callAgent(agent, run) {
5844
+ try {
5845
+ const invoke = () => agent(run);
5846
+ const result = this.outcome.traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(run.runId, invoke) : await invoke();
5847
+ ({ report: this.outcome.report, extra: this.outcome.extra } = reportFromResult(result));
5848
+ } catch (error) {
5849
+ this.outcome.agentRaised(error);
5850
+ }
5851
+ if (this.outcome.traced)
5852
+ await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing2) => tracing2.flush()).catch(() => void 0);
5853
+ }
5854
+ async file() {
5855
+ if (this.opts.file === false || this.interrupted) return;
5856
+ this.files.writeJson("run.json", { ...this.outcome.summary(), fileError: FILING_UNFINISHED });
5857
+ this.filing = this.outcome.file(new GatewayEvalsClient(this.options), this.opts.setRunId);
5858
+ await this.filing;
5859
+ }
5860
+ record() {
5861
+ const summary = this.outcome.summary();
5862
+ this.files.writeJson("run.json", summary);
5863
+ return summary;
5864
+ }
5865
+ end() {
5866
+ this.ending ??= this.opts.keepUp ? Promise.resolve() : this.live.close({ keepWarm: false }).then(
5867
+ () => void 0,
5868
+ () => void 0
5869
+ );
5870
+ return this.ending;
5871
+ }
5872
+ };
5873
+ TaskOpening = class {
5874
+ interrupting = null;
5875
+ run = null;
5876
+ opened;
5877
+ constructor(opening) {
5878
+ this.opened = opening.then((run) => this.run = run);
5879
+ }
5880
+ async complete(agent) {
5881
+ const run = await this.opened;
5882
+ if (!this.interrupting) return run.complete(agent);
5883
+ await this.interrupting;
5884
+ return run.summary();
5885
+ }
5886
+ interrupt(signal, urgent = false) {
5887
+ if (urgent) return this.run?.interrupt(signal, true) ?? Promise.resolve();
5888
+ this.interrupting ??= this.opened.then(
5889
+ (run) => run.interrupt(signal),
5890
+ () => void 0
5891
+ );
5892
+ return this.interrupting;
5893
+ }
5894
+ };
5197
5895
  }
5198
5896
  });
5199
5897
 
@@ -5273,73 +5971,157 @@ async function runSet(datasetId, agent, opts) {
5273
5971
  source: opts.source ?? "sdk",
5274
5972
  ...git && Object.keys(git).length ? { git } : {}
5275
5973
  });
5276
- const close = (status) => apiRequest(
5277
- options,
5278
- "POST",
5279
- `${SET_RUNS}/${encodeURIComponent(opened.id)}/close`,
5280
- { status }
5281
- );
5282
- const onInterrupt = () => {
5283
- void close("failed").finally(() => process.exit(130));
5284
- };
5285
- process.once("SIGINT", onInterrupt);
5286
- const planned = await runPlannedTasks(opened, agent, opts, options).finally(
5287
- () => process.off("SIGINT", onInterrupt)
5288
- );
5289
- const { tasks, failed } = planned;
5290
- const requested = failed ? "failed" : "completed";
5291
- const setRun = await close(requested);
5292
- return {
5293
- setRun,
5294
- tasks,
5295
- failed: failed || setRun.status !== "completed",
5296
- closedAsRequested: setRun.status === requested
5297
- };
5974
+ return new SetRunner(opened, agent, opts, options).run();
5298
5975
  }
5299
- async function runPlannedTasks(opened, agent, opts, options) {
5300
- const tasks = [];
5301
- let failed = false;
5302
- for (const task of opened.tasks) {
5303
- try {
5304
- const summary = await runTask(task.worldTaskId, agent, {
5305
- model: opts.model,
5306
- surfaces: opts.surfaces,
5307
- browserTier: opts.browserTier,
5308
- outDir: opts.outDir,
5309
- trace: opts.trace,
5310
- setRunId: opened.id,
5311
- options
5312
- });
5313
- tasks.push(summary);
5314
- failed ||= summary.agentError !== null;
5315
- opts.onTask?.(summary);
5316
- } catch (error) {
5317
- failed = true;
5318
- const message = error instanceof Error ? error.message : String(error);
5319
- process.stderr.write(`task ${task.name} could not run: ${message}
5320
- `);
5321
- }
5322
- }
5323
- return { tasks, failed };
5324
- }
5325
- var SET_RUNS;
5976
+ var SET_RUNS, SetRunner;
5326
5977
  var init_setRun = __esm({
5327
5978
  "src/worlds/setRun.ts"() {
5328
5979
  "use strict";
5329
5980
  init_worlds();
5330
5981
  init_checkoutProvenance();
5982
+ init_runSignals();
5331
5983
  init_taskRun();
5332
5984
  SET_RUNS = "/api/public/set-runs";
5985
+ SetRunner = class {
5986
+ constructor(opened, agent, opts, options) {
5987
+ this.opened = opened;
5988
+ this.agent = agent;
5989
+ this.opts = opts;
5990
+ this.options = options;
5991
+ }
5992
+ opened;
5993
+ agent;
5994
+ opts;
5995
+ options;
5996
+ tasks = [];
5997
+ failures = [];
5998
+ failed = false;
5999
+ current = null;
6000
+ interrupted = null;
6001
+ signals = new RunSignals();
6002
+ async run() {
6003
+ await this.signals.during(
6004
+ (signal2) => this.interrupt(signal2).then(() => void 0),
6005
+ () => this.runTasks()
6006
+ );
6007
+ const signal = this.signals.signal;
6008
+ if (signal !== null) return this.result(await this.interrupt(signal), "failed");
6009
+ const requested = this.failed ? "failed" : "completed";
6010
+ return this.result(await this.close(requested, this.failures), requested);
6011
+ }
6012
+ interrupt(signal) {
6013
+ const stopped = this.current ? [{ worldTaskId: this.current, reason: `interrupted by ${signal}` }] : [];
6014
+ this.interrupted ??= this.close("failed", [...this.failures, ...stopped]);
6015
+ return this.interrupted;
6016
+ }
6017
+ async runTasks() {
6018
+ for (const task of this.opened.tasks) {
6019
+ if (this.signals.signal !== null) return;
6020
+ this.current = task.worldTaskId;
6021
+ await this.runOne(task);
6022
+ }
6023
+ this.current = null;
6024
+ }
6025
+ async runOne(task) {
6026
+ try {
6027
+ const summary = await runTask(task.worldTaskId, this.agent, {
6028
+ model: this.opts.model,
6029
+ surfaces: this.opts.surfaces,
6030
+ browserTier: this.opts.browserTier,
6031
+ outDir: this.opts.outDir,
6032
+ trace: this.opts.trace,
6033
+ setRunId: this.opened.id,
6034
+ options: this.options
6035
+ });
6036
+ this.tasks.push(summary);
6037
+ this.failed ||= taskRunFailure(summary) !== null;
6038
+ if (summary.fileError !== null)
6039
+ this.failures.push({ worldTaskId: task.worldTaskId, reason: summary.fileError });
6040
+ this.opts.onTask?.(summary);
6041
+ } catch (error) {
6042
+ this.failed = true;
6043
+ const message = error instanceof Error ? error.message : String(error);
6044
+ process.stderr.write(`task ${task.name} could not run: ${message}
6045
+ `);
6046
+ this.failures.push({
6047
+ worldTaskId: task.worldTaskId,
6048
+ reason: message || (error instanceof Error ? error.name : "Error")
6049
+ });
6050
+ }
6051
+ }
6052
+ close(status, failures = []) {
6053
+ return apiRequest(
6054
+ this.options,
6055
+ "POST",
6056
+ `${SET_RUNS}/${encodeURIComponent(this.opened.id)}/close`,
6057
+ { status, ...failures.length ? { failures } : {} }
6058
+ );
6059
+ }
6060
+ result(setRun, requested) {
6061
+ return {
6062
+ setRun,
6063
+ tasks: this.tasks,
6064
+ failed: this.failed || setRun.status !== "completed",
6065
+ closedAsRequested: setRun.status === requested
6066
+ };
6067
+ }
6068
+ };
6069
+ }
6070
+ });
6071
+
6072
+ // src/worlds/requestLog.ts
6073
+ var WorldRequestLog;
6074
+ var init_requestLog = __esm({
6075
+ "src/worlds/requestLog.ts"() {
6076
+ "use strict";
6077
+ WorldRequestLog = class _WorldRequestLog {
6078
+ constructor(requests) {
6079
+ this.requests = requests;
6080
+ }
6081
+ requests;
6082
+ static async read(session) {
6083
+ const state2 = await session.state();
6084
+ const calls = state2?.calls;
6085
+ if (!Array.isArray(calls)) return null;
6086
+ return new _WorldRequestLog(calls.filter((c) => typeof c === "object" && c !== null));
6087
+ }
6088
+ /** How many older requests the world no longer keeps (its log is bounded): the gap
6089
+ * before the first entry it still holds. */
6090
+ get dropped() {
6091
+ const first = Number(this.requests[0]?.seq) || 1;
6092
+ return Math.max(0, first - 1);
6093
+ }
6094
+ /** Each request in the shape of a ledger line, `kind: "request"`, so one JSONL
6095
+ * file holds both. */
6096
+ asCalls() {
6097
+ return this.requests.map((r) => ({
6098
+ seq: Number(r.seq) || 0,
6099
+ kind: "request",
6100
+ tool: r.operation ?? null,
6101
+ args: {
6102
+ method: r.method ?? null,
6103
+ path: r.path ?? null,
6104
+ args: r.args ?? null,
6105
+ query: r.query ?? null,
6106
+ body: r.body ?? null,
6107
+ via: r.via ?? null
6108
+ },
6109
+ result: { status: r.status ?? null, rows: r.rows ?? null, found: r.found ?? null },
6110
+ error: null,
6111
+ completedAt: r.at ?? null
6112
+ }));
6113
+ }
6114
+ };
5333
6115
  }
5334
6116
  });
5335
6117
 
5336
6118
  // src/worlds/checkoutTarget.ts
5337
- var fs2, path2, CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
6119
+ var fs2, path3, CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
5338
6120
  var init_checkoutTarget = __esm({
5339
6121
  "src/worlds/checkoutTarget.ts"() {
5340
6122
  "use strict";
5341
6123
  fs2 = __toESM(require("fs"));
5342
- path2 = __toESM(require("path"));
6124
+ path3 = __toESM(require("path"));
5343
6125
  CheckoutRefusedError = class extends Error {
5344
6126
  constructor(message) {
5345
6127
  super(`checkout refused: ${message}`);
@@ -5364,17 +6146,17 @@ var init_checkoutTarget = __esm({
5364
6146
  }
5365
6147
  /** The absolute destination; a relative target may not resolve above `cwd`. */
5366
6148
  static confined(target, cwd) {
5367
- const dir = path2.resolve(cwd, target);
5368
- if (path2.isAbsolute(target)) return dir;
5369
- const rel = path2.relative(cwd, dir);
5370
- if (rel === "" || !rel.startsWith("..") && !path2.isAbsolute(rel))
6149
+ const dir = path3.resolve(cwd, target);
6150
+ if (path3.isAbsolute(target)) return dir;
6151
+ const rel = path3.relative(cwd, dir);
6152
+ if (rel === "" || !rel.startsWith("..") && !path3.isAbsolute(rel))
5371
6153
  return dir;
5372
6154
  throw new CheckoutRefusedError(
5373
6155
  `--target ${target} resolves outside the current directory (${cwd}) \u2014 run from that directory, or pass an absolute path`
5374
6156
  );
5375
6157
  }
5376
6158
  static fresh(slug, cwd) {
5377
- const base = path2.join(cwd, slug);
6159
+ const base = path3.join(cwd, slug);
5378
6160
  let dir = base;
5379
6161
  let i = 1;
5380
6162
  while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
@@ -5401,7 +6183,7 @@ var init_checkoutTarget = __esm({
5401
6183
  ].filter(_CheckoutTarget.confinedRel).sort();
5402
6184
  }
5403
6185
  static confinedRel(rel) {
5404
- if (rel === "" || path2.isAbsolute(rel)) return false;
6186
+ if (rel === "" || path3.isAbsolute(rel)) return false;
5405
6187
  return !rel.split(/[\\/]/).includes("..");
5406
6188
  }
5407
6189
  /** Why `dir` cannot be overwritten, or null when it is a clean checkout of `slug`. */
@@ -5419,9 +6201,9 @@ var init_checkoutTarget = __esm({
5419
6201
  /** Remove the replaced checkout's files (and the dirs they leave empty). */
5420
6202
  clear() {
5421
6203
  for (const rel of this.replaces) {
5422
- const abs = path2.join(this.dir, rel);
6204
+ const abs = path3.join(this.dir, rel);
5423
6205
  fs2.rmSync(abs, { force: true });
5424
- this.pruneEmptyParents(path2.dirname(abs));
6206
+ this.pruneEmptyParents(path3.dirname(abs));
5425
6207
  }
5426
6208
  }
5427
6209
  pruneEmptyParents(from) {
@@ -5429,7 +6211,7 @@ var init_checkoutTarget = __esm({
5429
6211
  while (cur !== this.dir && cur.startsWith(this.dir)) {
5430
6212
  if (fs2.existsSync(cur) && fs2.readdirSync(cur).length > 0) return;
5431
6213
  fs2.rmSync(cur, { recursive: true, force: true });
5432
- cur = path2.dirname(cur);
6214
+ cur = path3.dirname(cur);
5433
6215
  }
5434
6216
  }
5435
6217
  };
@@ -5437,13 +6219,13 @@ var init_checkoutTarget = __esm({
5437
6219
  });
5438
6220
 
5439
6221
  // src/worlds/worldDir.ts
5440
- var import_node_crypto5, fs3, path3, WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, BENCHMARK_CONFIG, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
6222
+ var import_node_crypto5, fs3, path4, WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, BENCHMARK_CONFIG, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
5441
6223
  var init_worldDir = __esm({
5442
6224
  "src/worlds/worldDir.ts"() {
5443
6225
  "use strict";
5444
6226
  import_node_crypto5 = require("crypto");
5445
6227
  fs3 = __toESM(require("fs"));
5446
- path3 = __toESM(require("path"));
6228
+ path4 = __toESM(require("path"));
5447
6229
  WORLD_MANIFEST = "gateway-env.toml";
5448
6230
  API_BUNDLE_MANIFEST = "gateway-api-world.toml";
5449
6231
  BUNDLED_WORLD_PREFIX = "server/world/";
@@ -5469,17 +6251,17 @@ var init_worldDir = __esm({
5469
6251
  worldRoot;
5470
6252
  pathsMemo;
5471
6253
  static open(dir) {
5472
- const present = (rel) => fs3.existsSync(path3.join(dir, rel));
6254
+ const present = (rel) => fs3.existsSync(path4.join(dir, rel));
5473
6255
  const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
5474
6256
  return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
5475
6257
  }
5476
6258
  // ── Files ──────────────────────────────────────────────────────────────
5477
6259
  has(rel) {
5478
- return fs3.existsSync(path3.join(this.dir, rel));
6260
+ return fs3.existsSync(path4.join(this.dir, rel));
5479
6261
  }
5480
6262
  text(rel) {
5481
6263
  try {
5482
- return fs3.readFileSync(path3.join(this.dir, rel), "utf-8");
6264
+ return fs3.readFileSync(path4.join(this.dir, rel), "utf-8");
5483
6265
  } catch {
5484
6266
  return null;
5485
6267
  }
@@ -5498,7 +6280,7 @@ var init_worldDir = __esm({
5498
6280
  const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
5499
6281
  const out = [];
5500
6282
  const walk = (rel) => {
5501
- for (const entry of fs3.readdirSync(rel ? path3.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
6283
+ for (const entry of fs3.readdirSync(rel ? path4.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
5502
6284
  const child = rel ? `${rel}/${entry.name}` : entry.name;
5503
6285
  if (entry.isDirectory()) {
5504
6286
  if (!skippedDir(entry.name)) walk(child);
@@ -5519,7 +6301,7 @@ var init_worldDir = __esm({
5519
6301
  get exclusions() {
5520
6302
  const kept = new Set(this.archivePaths);
5521
6303
  const out = [];
5522
- const filesUnder = (rel) => fs3.readdirSync(path3.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
6304
+ const filesUnder = (rel) => fs3.readdirSync(path4.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
5523
6305
  const child = `${rel}/${entry.name}`;
5524
6306
  if (entry.isDirectory()) return filesUnder(child);
5525
6307
  return entry.isFile() ? [child] : [];
@@ -5550,7 +6332,7 @@ var init_worldDir = __esm({
5550
6332
  const hasher = (0, import_node_crypto5.createHash)("sha256");
5551
6333
  for (const rel of this.archivePaths) {
5552
6334
  hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
5553
- hasher.update(fs3.readFileSync(path3.join(this.dir, rel)));
6335
+ hasher.update(fs3.readFileSync(path4.join(this.dir, rel)));
5554
6336
  }
5555
6337
  return hasher.digest("hex");
5556
6338
  }
@@ -5640,12 +6422,12 @@ function paxPath(body) {
5640
6422
  }
5641
6423
  return found;
5642
6424
  }
5643
- var fs4, path4, import_node_zlib, TAR_FILE_MODE, TAR_EXEC_MODE, TarArchive;
6425
+ var fs4, path5, import_node_zlib, TAR_FILE_MODE, TAR_EXEC_MODE, TarArchive;
5644
6426
  var init_tar = __esm({
5645
6427
  "src/worlds/tar.ts"() {
5646
6428
  "use strict";
5647
6429
  fs4 = __toESM(require("fs"));
5648
- path4 = __toESM(require("path"));
6430
+ path5 = __toESM(require("path"));
5649
6431
  import_node_zlib = require("zlib");
5650
6432
  TAR_FILE_MODE = 420;
5651
6433
  TAR_EXEC_MODE = 493;
@@ -5701,8 +6483,8 @@ var init_tar = __esm({
5701
6483
  for (const member of this.members()) {
5702
6484
  if (member.typeflag !== "0" && member.typeflag !== "\0") continue;
5703
6485
  if (!_TarArchive.isConfined(member.name)) continue;
5704
- const target = path4.join(dir, member.name);
5705
- fs4.mkdirSync(path4.dirname(target), { recursive: true });
6486
+ const target = path5.join(dir, member.name);
6487
+ fs4.mkdirSync(path5.dirname(target), { recursive: true });
5706
6488
  const mode = archiveMode(member.mode);
5707
6489
  fs4.writeFileSync(target, member.bytes, { mode });
5708
6490
  fs4.chmodSync(target, mode);
@@ -5805,7 +6587,7 @@ function tarMember(name, payload, mode) {
5805
6587
  function buildSourceTarball(dir) {
5806
6588
  const parts2 = [];
5807
6589
  for (const relPath of collectArchiveRelPaths(dir)) {
5808
- const abs = path5.join(dir, relPath);
6590
+ const abs = path6.join(dir, relPath);
5809
6591
  parts2.push(
5810
6592
  ...tarMember(
5811
6593
  relPath,
@@ -5819,7 +6601,7 @@ function buildSourceTarball(dir) {
5819
6601
  }
5820
6602
  function readLocalMeta(dir) {
5821
6603
  try {
5822
- const raw = fs5.readFileSync(path5.join(dir, META_RELPATH), "utf-8");
6604
+ const raw = fs5.readFileSync(path6.join(dir, META_RELPATH), "utf-8");
5823
6605
  const parsed = JSON.parse(raw);
5824
6606
  return parsed && typeof parsed === "object" ? parsed : null;
5825
6607
  } catch {
@@ -5842,16 +6624,16 @@ function resolvePushSlug(dir, opts = {}) {
5842
6624
  }
5843
6625
  const first = declared[0];
5844
6626
  if (first) return { ...base, slug: first.slug, source: first.source };
5845
- return { ...base, slug: path5.basename(path5.resolve(dir)), source: "directory name" };
6627
+ return { ...base, slug: path6.basename(path6.resolve(dir)), source: "directory name" };
5846
6628
  }
5847
6629
  function writeLocalMeta(dir, meta) {
5848
- fs5.mkdirSync(path5.join(dir, ".gateway"), { recursive: true });
5849
- fs5.writeFileSync(path5.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
6630
+ fs5.mkdirSync(path6.join(dir, ".gateway"), { recursive: true });
6631
+ fs5.writeFileSync(path6.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
5850
6632
  }
5851
6633
  function snapshotTreeHashes(dir) {
5852
6634
  const out = {};
5853
6635
  for (const relPath of collectArchiveRelPaths(dir)) {
5854
- out[relPath] = (0, import_node_crypto6.createHash)("sha256").update(fs5.readFileSync(path5.join(dir, relPath))).digest("hex");
6636
+ out[relPath] = (0, import_node_crypto6.createHash)("sha256").update(fs5.readFileSync(path6.join(dir, relPath))).digest("hex");
5855
6637
  }
5856
6638
  return out;
5857
6639
  }
@@ -5869,7 +6651,7 @@ function localTreeState(dir) {
5869
6651
  }
5870
6652
  function readPyproject(dir) {
5871
6653
  try {
5872
- const text2 = fs5.readFileSync(path5.join(dir, "pyproject.toml"), "utf-8");
6654
+ const text2 = fs5.readFileSync(path6.join(dir, "pyproject.toml"), "utf-8");
5873
6655
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
5874
6656
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
5875
6657
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -5892,7 +6674,7 @@ function readWorldContractVersion(dir) {
5892
6674
  }
5893
6675
  function buildFileManifest(dir) {
5894
6676
  return collectArchiveRelPaths(dir).map((relPath) => {
5895
- const payload = fs5.readFileSync(path5.join(dir, relPath));
6677
+ const payload = fs5.readFileSync(path6.join(dir, relPath));
5896
6678
  return {
5897
6679
  path: relPath,
5898
6680
  isDirectory: false,
@@ -5938,7 +6720,7 @@ async function push(dir, opts = {}) {
5938
6720
  const pyproject = readPyproject(dir);
5939
6721
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
5940
6722
  if (contractVersion) pyproject.version = contractVersion;
5941
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path5.basename(path5.resolve(dir));
6723
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path6.basename(path6.resolve(dir));
5942
6724
  const metadata = {
5943
6725
  ...opts.name !== void 0 ? { name: opts.name } : {},
5944
6726
  ...opts.description !== void 0 ? { description: opts.description } : {},
@@ -6064,7 +6846,7 @@ async function propose(dir, opts) {
6064
6846
  if (state2.clean)
6065
6847
  throw new Error("Nothing to propose \u2014 the tree matches its base.");
6066
6848
  const changes = state2.dirty.map((relPath) => {
6067
- const abs = path5.join(dir, relPath);
6849
+ const abs = path6.join(dir, relPath);
6068
6850
  if (!fs5.existsSync(abs))
6069
6851
  return { path: relPath, action: "delete" };
6070
6852
  return {
@@ -6096,20 +6878,20 @@ async function createRef(slug, name, kind, versionId, options = {}) {
6096
6878
  ...versionId ? { versionId } : {}
6097
6879
  });
6098
6880
  }
6099
- var import_node_crypto6, import_node_zlib2, fs5, path5, DEFAULT_BRANCH, META_RELPATH, FILE_MODE, MINTED_SUFFIX, PushSlugConflict, sha256b64, DetachedPushError;
6881
+ var import_node_crypto6, import_node_zlib2, fs5, path6, DEFAULT_BRANCH, META_RELPATH, FILE_MODE, MINTED_SUFFIX, PushSlugConflict, sha256b64, DetachedPushError;
6100
6882
  var init_hub = __esm({
6101
6883
  "src/worlds/hub.ts"() {
6102
6884
  "use strict";
6103
6885
  import_node_crypto6 = require("crypto");
6104
6886
  import_node_zlib2 = require("zlib");
6105
6887
  fs5 = __toESM(require("fs"));
6106
- path5 = __toESM(require("path"));
6888
+ path6 = __toESM(require("path"));
6107
6889
  init_worlds();
6108
6890
  init_checkoutTarget();
6109
6891
  init_worldDir();
6110
6892
  init_tar();
6111
6893
  DEFAULT_BRANCH = "main";
6112
- META_RELPATH = path5.join(".gateway", ".env-metadata.json");
6894
+ META_RELPATH = path6.join(".gateway", ".env-metadata.json");
6113
6895
  FILE_MODE = TAR_FILE_MODE;
6114
6896
  MINTED_SUFFIX = /^-\d+$/;
6115
6897
  PushSlugConflict = class extends Error {
@@ -6337,7 +7119,7 @@ function rebaseConflicts(error) {
6337
7119
  }
6338
7120
  const conflicts = body?.conflicts;
6339
7121
  if (!Array.isArray(conflicts)) return null;
6340
- return conflicts.filter(isConflictPath).map(({ path: path16, reason }) => ({ path: path16, reason }));
7122
+ return conflicts.filter(isConflictPath).map(({ path: path17, reason }) => ({ path: path17, reason }));
6341
7123
  }
6342
7124
  function isConflictPath(value) {
6343
7125
  const entry = value;
@@ -6377,8 +7159,8 @@ var init_sandbox = __esm({
6377
7159
  this.options = options;
6378
7160
  }
6379
7161
  options;
6380
- request(method, path16, body) {
6381
- return apiRequest(this.options, method, path16, body, {
7162
+ request(method, path17, body) {
7163
+ return apiRequest(this.options, method, path17, body, {
6382
7164
  timeoutMs: SANDBOX_REQUEST_TIMEOUT_MS
6383
7165
  });
6384
7166
  }
@@ -6909,6 +7691,17 @@ async function queryRows(slugRef, entity, opts = {}, options = {}) {
6909
7691
  `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
6910
7692
  );
6911
7693
  }
7694
+ async function sqlRows(slugRef, sql, opts = {}, options = {}) {
7695
+ const [slug, ref] = slugRef.split("@", 2);
7696
+ if (!slug || ref === "") throw new Error(`sqlRows needs a slug, optionally slug@ref (got ${slugRef})`);
7697
+ if (!sql.trim()) throw new Error("sqlRows needs one SQL statement");
7698
+ return apiRequest(options, "POST", `/api/public/worlds/${encodeURIComponent(slug)}/data/sql`, {
7699
+ sql,
7700
+ ...ref ? { ref } : {},
7701
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
7702
+ ...opts.scenario ? { scenario: opts.scenario } : {}
7703
+ });
7704
+ }
6912
7705
  var init_query = __esm({
6913
7706
  "src/worlds/query.ts"() {
6914
7707
  "use strict";
@@ -6968,13 +7761,13 @@ async function api(creds, method, route, body, init3 = {}) {
6968
7761
  if (!reply.ok) throw new ApiError2(reply.status, method, route, reply.text);
6969
7762
  return reply.text ? JSON.parse(reply.text) : {};
6970
7763
  }
6971
- var fs6, os, path6, ApiError2;
7764
+ var fs6, os, path7, ApiError2;
6972
7765
  var init_config = __esm({
6973
7766
  "src/cli/config.ts"() {
6974
7767
  "use strict";
6975
7768
  fs6 = __toESM(require("fs"));
6976
7769
  os = __toESM(require("os"));
6977
- path6 = __toESM(require("path"));
7770
+ path7 = __toESM(require("path"));
6978
7771
  init_router();
6979
7772
  init_worlds();
6980
7773
  init_transport();
@@ -7012,16 +7805,16 @@ function runtimeRoot() {
7012
7805
  const candidates = [];
7013
7806
  if (process.env.GATEWAY_RUNTIME_PATH)
7014
7807
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
7015
- const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path7.dirname(fs7.realpathSync(process.argv[1])) : null;
7808
+ const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path8.dirname(fs7.realpathSync(process.argv[1])) : null;
7016
7809
  if (entry) {
7017
- candidates.push(path7.resolve(entry, "..", "runtime"));
7018
- candidates.push(path7.resolve(entry, "..", "..", "runtime"));
7019
- candidates.push(path7.resolve(entry, "..", "..", "sdk", "src"));
7810
+ candidates.push(path8.resolve(entry, "..", "runtime"));
7811
+ candidates.push(path8.resolve(entry, "..", "..", "runtime"));
7812
+ candidates.push(path8.resolve(entry, "..", "..", "sdk", "src"));
7020
7813
  }
7021
- candidates.push(path7.resolve(process.cwd(), "runtime"));
7814
+ candidates.push(path8.resolve(process.cwd(), "runtime"));
7022
7815
  for (const root of candidates)
7023
7816
  if (fs7.existsSync(
7024
- path7.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7817
+ path8.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7025
7818
  ))
7026
7819
  return root;
7027
7820
  return null;
@@ -7110,13 +7903,12 @@ async function localBridge(request, local, opts = {}) {
7110
7903
  ["-I", "-B", "-c", BRIDGE_BOOTSTRAP, local.root],
7111
7904
  {
7112
7905
  stdio: ["pipe", "pipe", "pipe"],
7113
- // This is the author's machine running the author's world: the one
7114
- // place the runtime may import and run a world's own Python (check's
7115
- // handler execution, `worlds call`, `worlds test` tool cases).
7906
+ // The author's machine running the author's world: the one place the runtime may run its Python.
7116
7907
  env: {
7117
7908
  PATH: process.env.PATH ?? "",
7118
7909
  HOME: process.env.HOME ?? "",
7119
7910
  TMPDIR: process.env.TMPDIR ?? "",
7911
+ ...process.env.VIRTUAL_ENV ? { VIRTUAL_ENV: process.env.VIRTUAL_ENV } : {},
7120
7912
  GATEWAY_RUNTIME_EXECUTE: "1"
7121
7913
  }
7122
7914
  }
@@ -7162,17 +7954,18 @@ async function localBridge(request, local, opts = {}) {
7162
7954
  child.stdin.end(input);
7163
7955
  });
7164
7956
  }
7165
- var import_node_child_process2, fs7, net, path7, MIN_PYTHON, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
7957
+ var import_node_child_process2, fs7, net, path8, MIN_PYTHON, MIN_PYTHON_LABEL, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
7166
7958
  var init_local = __esm({
7167
7959
  "src/cli/runtime/local.ts"() {
7168
7960
  "use strict";
7169
7961
  import_node_child_process2 = require("child_process");
7170
7962
  fs7 = __toESM(require("fs"));
7171
7963
  net = __toESM(require("net"));
7172
- path7 = __toESM(require("path"));
7964
+ path8 = __toESM(require("path"));
7173
7965
  init_router();
7174
7966
  init_config();
7175
7967
  MIN_PYTHON = [3, 12];
7968
+ MIN_PYTHON_LABEL = MIN_PYTHON.join(".");
7176
7969
  DIAGNOSTIC_TAIL = 600;
7177
7970
  BRIDGE_BOOTSTRAP = `import sys,runpy,types,os
7178
7971
  root=sys.argv[1]
@@ -7191,16 +7984,44 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
7191
7984
  }
7192
7985
  });
7193
7986
 
7987
+ // src/worlds/databaseEngines.ts
7988
+ function isWorldSnapshotOutput(path17) {
7989
+ return SNAPSHOT_OUTPUTS.has(path17.slice(path17.lastIndexOf("/") + 1));
7990
+ }
7991
+ var WORLD_DATABASE_ENGINES, DEFAULT_WORLD_DATABASE_ENGINE, WORLD_SNAPSHOT_FILE_PATH, WORLD_SNAPSHOT_SIDECAR_PATH, WORLD_SNAPSHOT_CONTENT_TYPE, SNAPSHOT_OUTPUTS;
7992
+ var init_databaseEngines = __esm({
7993
+ "src/worlds/databaseEngines.ts"() {
7994
+ "use strict";
7995
+ WORLD_DATABASE_ENGINES = [
7996
+ {
7997
+ engine: "sqlite",
7998
+ label: "SQLite",
7999
+ target: "sqlite-json/1",
8000
+ snapshotName: "state.sqlite",
8001
+ contentType: "application/vnd.sqlite3"
8002
+ }
8003
+ ];
8004
+ DEFAULT_WORLD_DATABASE_ENGINE = WORLD_DATABASE_ENGINES[0];
8005
+ WORLD_SNAPSHOT_FILE_PATH = `db/${DEFAULT_WORLD_DATABASE_ENGINE.snapshotName}`;
8006
+ WORLD_SNAPSHOT_SIDECAR_PATH = "db/state.json";
8007
+ WORLD_SNAPSHOT_CONTENT_TYPE = DEFAULT_WORLD_DATABASE_ENGINE.contentType;
8008
+ SNAPSHOT_OUTPUTS = /* @__PURE__ */ new Set([
8009
+ ...WORLD_DATABASE_ENGINES.map((engine) => engine.snapshotName),
8010
+ WORLD_SNAPSHOT_SIDECAR_PATH.slice("db/".length)
8011
+ ]);
8012
+ }
8013
+ });
8014
+
7194
8015
  // src/cli/runtime/client.ts
7195
8016
  function isBuildOutput(relPath) {
7196
- return BUILD_OUTPUT.test(relPath);
8017
+ return isWorldSnapshotOutput(relPath);
7197
8018
  }
7198
8019
  function readWorldFiles(dir, opts = {}) {
7199
8020
  if (!fs8.existsSync(dir) || !fs8.statSync(dir).isDirectory())
7200
8021
  throw new CliError(`Not a directory: ${dir}`);
7201
8022
  const out = [];
7202
8023
  const walk = (rel) => {
7203
- const abs = rel ? path8.join(dir, rel) : dir;
8024
+ const abs = rel ? path9.join(dir, rel) : dir;
7204
8025
  const entries = fs8.readdirSync(abs, { withFileTypes: true });
7205
8026
  entries.sort((a, b) => a.name.localeCompare(b.name));
7206
8027
  for (const entry of entries) {
@@ -7211,7 +8032,7 @@ function readWorldFiles(dir, opts = {}) {
7211
8032
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
7212
8033
  out.push({
7213
8034
  path: childRel,
7214
- content: fs8.readFileSync(path8.join(dir, childRel)).toString("base64")
8035
+ content: fs8.readFileSync(path9.join(dir, childRel)).toString("base64")
7215
8036
  });
7216
8037
  }
7217
8038
  }
@@ -7231,15 +8052,15 @@ function writeWorldFiles(dir, files, opts = {}) {
7231
8052
  if (opts.clean && fs8.existsSync(dir)) {
7232
8053
  const keep = new Set(files.map((f) => f.path));
7233
8054
  for (const existing of readWorldFiles(dir)) {
7234
- if (!keep.has(existing.path)) fs8.rmSync(path8.join(dir, existing.path));
8055
+ if (!keep.has(existing.path)) fs8.rmSync(path9.join(dir, existing.path));
7235
8056
  }
7236
8057
  pruneEmptyDirs(dir);
7237
8058
  }
7238
8059
  fs8.mkdirSync(dir, { recursive: true });
7239
8060
  const written = [];
7240
8061
  for (const file of files) {
7241
- const target = path8.join(dir, file.path);
7242
- fs8.mkdirSync(path8.dirname(target), { recursive: true });
8062
+ const target = path9.join(dir, file.path);
8063
+ fs8.mkdirSync(path9.dirname(target), { recursive: true });
7243
8064
  fs8.writeFileSync(target, Buffer.from(file.content, "base64"));
7244
8065
  written.push(file.path);
7245
8066
  }
@@ -7249,7 +8070,7 @@ function pruneEmptyDirs(root) {
7249
8070
  const walk = (abs) => {
7250
8071
  let empty = true;
7251
8072
  for (const entry of fs8.readdirSync(abs, { withFileTypes: true })) {
7252
- const child = path8.join(abs, entry.name);
8073
+ const child = path9.join(abs, entry.name);
7253
8074
  if (entry.isDirectory()) {
7254
8075
  if (SKIP_DIRS.has(entry.name)) {
7255
8076
  empty = false;
@@ -7286,13 +8107,9 @@ function yamlDocument(request) {
7286
8107
  return isYamlSource(String(request.source ?? ""), String(request.document ?? ""));
7287
8108
  return request.command === "conform" && Array.isArray(request.openapi) && request.openapi.some((doc) => YAML_NAME.test(doc.path ?? ""));
7288
8109
  }
7289
- function runsLocally(request) {
7290
- const local = localRuntime();
7291
- return local !== null && !(yamlDocument(request) && !localPythonHasYaml(local.python));
7292
- }
7293
8110
  async function runtime(creds, request, opts = {}) {
7294
- const local = localRuntime();
7295
- if (local && runsLocally(request)) {
8111
+ const local = new RuntimeRoute().local(request);
8112
+ if (local) {
7296
8113
  const payload2 = await localBridge(request, local, opts);
7297
8114
  const detail2 = detailFrom(payload2);
7298
8115
  if (detail2) throw new BridgeRejected(detail2);
@@ -7319,19 +8136,19 @@ async function runtime(creds, request, opts = {}) {
7319
8136
  if (payload === null) throw new CliError(`POST ${route} (${request.command}) returned non-JSON`, 1);
7320
8137
  return payload;
7321
8138
  }
7322
- var fs8, path8, MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BUILD_OUTPUT, BridgeRejected, YAML_NAME;
8139
+ var fs8, path9, MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BridgeRejected, YAML_NAME, YAML_NEEDS_PYYAML, YAML_NOT_ON_PLATFORM, RuntimeRoute;
7323
8140
  var init_client = __esm({
7324
8141
  "src/cli/runtime/client.ts"() {
7325
8142
  "use strict";
7326
8143
  fs8 = __toESM(require("fs"));
7327
- path8 = __toESM(require("path"));
8144
+ path9 = __toESM(require("path"));
7328
8145
  init_router();
7329
8146
  init_local();
7330
8147
  init_transport();
8148
+ init_databaseEngines();
7331
8149
  MAX_WORLD_FILES = 2e3;
7332
8150
  RUNTIME_TIMEOUT_MS = 18e4;
7333
8151
  SKIP_DIRS = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
7334
- BUILD_OUTPUT = /(^|\/)state\.(sqlite|json)$/;
7335
8152
  BridgeRejected = class extends CliError {
7336
8153
  constructor(detail) {
7337
8154
  super(formatBridgeError(detail), 1);
@@ -7340,6 +8157,26 @@ var init_client = __esm({
7340
8157
  detail;
7341
8158
  };
7342
8159
  YAML_NAME = /\.ya?ml$/i;
8160
+ YAML_NEEDS_PYYAML = "Reading a YAML document needs PyYAML in the Python that runs the schema runtime: pip install pyyaml there, set GATEWAY_PYTHON to an interpreter that has it, or convert the document to JSON";
8161
+ YAML_NOT_ON_PLATFORM = "The platform's schema runtime reads JSON documents only: convert the document to JSON, or read it here with GATEWAY_RUNTIME=local and a Python that has PyYAML (pip install pyyaml, or set GATEWAY_PYTHON to one)";
8162
+ RuntimeRoute = class _RuntimeRoute {
8163
+ constructor(here = localRuntime, hasYaml = localPythonHasYaml) {
8164
+ this.here = here;
8165
+ this.hasYaml = hasYaml;
8166
+ }
8167
+ here;
8168
+ hasYaml;
8169
+ local(request) {
8170
+ const local = this.here();
8171
+ const yaml = yamlDocument(request);
8172
+ if (yaml && local === null) throw _RuntimeRoute.refusal(YAML_NOT_ON_PLATFORM);
8173
+ if (yaml && local !== null && !this.hasYaml(local.python)) throw _RuntimeRoute.refusal(YAML_NEEDS_PYYAML);
8174
+ return local;
8175
+ }
8176
+ static refusal(message) {
8177
+ return new BridgeRejected({ code: "conform", message, schema_path: "/openapi/source" });
8178
+ }
8179
+ };
7343
8180
  }
7344
8181
  });
7345
8182
 
@@ -7354,7 +8191,8 @@ __export(data_exports, {
7354
8191
  ingestLocalToolCalls: () => ingestLocalToolCalls,
7355
8192
  queryLocalWorldData: () => queryLocalWorldData,
7356
8193
  queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
7357
- showLocalWorldData: () => showLocalWorldData
8194
+ showLocalWorldData: () => showLocalWorldData,
8195
+ sqlLocalWorldData: () => sqlLocalWorldData
7358
8196
  });
7359
8197
  async function importLocalWorldData(dir, rows, opts, creds) {
7360
8198
  const res = await runtime(creds, {
@@ -7386,8 +8224,8 @@ async function importLocalWorldFiles(dir, sources, opts) {
7386
8224
  );
7387
8225
  const res = await runtime(null, {
7388
8226
  command: "import-files",
7389
- world: path9.resolve(dir),
7390
- sources: sources.map((source) => path9.resolve(source)),
8227
+ world: path10.resolve(dir),
8228
+ sources: sources.map((source) => path10.resolve(source)),
7391
8229
  mode: opts.mode ?? "append",
7392
8230
  ...opts.entity ? { entity: opts.entity } : {},
7393
8231
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
@@ -7413,9 +8251,9 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7413
8251
  );
7414
8252
  const res = await runtime(null, {
7415
8253
  command: "chunk-files",
7416
- world: path9.resolve(dir),
7417
- sources: sources.map((source) => path9.resolve(source)),
7418
- out: path9.resolve(out),
8254
+ world: path10.resolve(dir),
8255
+ sources: sources.map((source) => path10.resolve(source)),
8256
+ out: path10.resolve(out),
7419
8257
  ...opts.entity ? { entity: opts.entity } : {},
7420
8258
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
7421
8259
  ...opts.chunkBytes ? { chunk_bytes: opts.chunkBytes } : {},
@@ -7426,8 +8264,8 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7426
8264
  function dropBuildOutputs(dir) {
7427
8265
  const dropped = [];
7428
8266
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
7429
- if (!BUILD_OUTPUT2.test(file.path)) continue;
7430
- fs9.rmSync(path9.join(dir, file.path), { force: true });
8267
+ if (!isBuildOutput(file.path)) continue;
8268
+ fs9.rmSync(path10.join(dir, file.path), { force: true });
7431
8269
  dropped.push(file.path);
7432
8270
  }
7433
8271
  return dropped;
@@ -7494,7 +8332,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7494
8332
  const payload = await localBridge(
7495
8333
  {
7496
8334
  command: "query",
7497
- world: path9.resolve(dir),
8335
+ world: path10.resolve(dir),
7498
8336
  entity,
7499
8337
  across: true,
7500
8338
  ...opts.compare ? { compare: true } : {},
@@ -7510,6 +8348,28 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7510
8348
  if (detail) throw new BridgeRejected(detail);
7511
8349
  return payload;
7512
8350
  }
8351
+ async function sqlLocalWorldData(dir, sql, opts) {
8352
+ const local = localRuntime();
8353
+ if (!local)
8354
+ throw new CliError(
8355
+ "`worlds data sql` on a directory reads the world's database in place, so it needs the bundled runtime on this machine. " + localRuntimeReason(),
8356
+ 1
8357
+ );
8358
+ const payload = await localBridge(
8359
+ {
8360
+ command: "sql",
8361
+ world: path10.resolve(dir),
8362
+ sql,
8363
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
8364
+ ...opts.scenario ? { scenario: opts.scenario } : {}
8365
+ },
8366
+ local
8367
+ );
8368
+ const detail = detailFrom(payload);
8369
+ if (detail) throw new BridgeRejected(detail);
8370
+ const { status: _status, ...answer } = payload;
8371
+ return answer;
8372
+ }
7513
8373
  async function ingestLocalToolCalls(dir, records, opts, creds) {
7514
8374
  const res = await runtime(creds, {
7515
8375
  command: "ingest-calls",
@@ -7541,16 +8401,15 @@ async function ingestLocalToolCalls(dir, records, opts, creds) {
7541
8401
  ...res.batch !== void 0 ? { batch: res.batch } : {}
7542
8402
  };
7543
8403
  }
7544
- var fs9, path9, BUILD_OUTPUT2;
8404
+ var fs9, path10;
7545
8405
  var init_data = __esm({
7546
8406
  "src/cli/runtime/data.ts"() {
7547
8407
  "use strict";
7548
8408
  fs9 = __toESM(require("fs"));
7549
- path9 = __toESM(require("path"));
8409
+ path10 = __toESM(require("path"));
7550
8410
  init_router();
7551
8411
  init_client();
7552
8412
  init_local();
7553
- BUILD_OUTPUT2 = /(^|\/)state\.(sqlite|json)$/;
7554
8413
  }
7555
8414
  });
7556
8415
 
@@ -7589,27 +8448,27 @@ function typescriptPrefix() {
7589
8448
  }
7590
8449
  }
7591
8450
  function command(script, wrapperDir) {
7592
- const suffix = path10.extname(script).toLowerCase();
8451
+ const suffix = path11.extname(script).toLowerCase();
7593
8452
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
7594
8453
  if (NODE_SUFFIXES.includes(suffix)) {
7595
- const wrapper = path10.join(wrapperDir, "transform-wrapper.mjs");
8454
+ const wrapper = path11.join(wrapperDir, "transform-wrapper.mjs");
7596
8455
  fs10.writeFileSync(wrapper, NODE_WRAPPER);
7597
8456
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
7598
8457
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
7599
8458
  }
7600
- throw new CliError(`${path10.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
8459
+ throw new CliError(`${path11.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
7601
8460
  }
7602
8461
  async function runTransform(script, records, opts = {}) {
7603
8462
  if (!fs10.existsSync(script) || !fs10.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
7604
- const resolved = path10.resolve(script);
7605
- const name = path10.basename(script);
8463
+ const resolved = path11.resolve(script);
8464
+ const name = path11.basename(script);
7606
8465
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
7607
- const scratch = fs10.mkdtempSync(path10.join(os2.tmpdir(), "gateway-transform-"));
8466
+ const scratch = fs10.mkdtempSync(path11.join(os2.tmpdir(), "gateway-transform-"));
7608
8467
  try {
7609
8468
  const { file, args } = command(resolved, scratch);
7610
8469
  const env = Object.fromEntries(Object.entries(process.env).filter(([key2]) => !SCRUBBED.test(key2)));
7611
8470
  const done = await new Promise((resolve11, reject) => {
7612
- const child = (0, import_node_child_process3.spawn)(file, args, { cwd: path10.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
8471
+ const child = (0, import_node_child_process3.spawn)(file, args, { cwd: path11.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
7613
8472
  let stdout = "";
7614
8473
  let stderr = "";
7615
8474
  let timedOut = false;
@@ -7647,14 +8506,14 @@ async function transformRecords(transform, records, opts = {}) {
7647
8506
  if (typeof transform === "function") return { rows: checkRows(await transform(records), "transform(records)"), stderr: "" };
7648
8507
  return runTransform(transform, records, opts);
7649
8508
  }
7650
- var import_node_child_process3, fs10, os2, path10, import_node_module, import_meta, DEFAULT_TRANSFORM_TIMEOUT_MS, NODE_SUFFIXES, PYTHON_SUFFIXES, SCRUBBED, PY_WRAPPER, NODE_WRAPPER, isObject, tail;
8509
+ var import_node_child_process3, fs10, os2, path11, import_node_module, import_meta, DEFAULT_TRANSFORM_TIMEOUT_MS, NODE_SUFFIXES, PYTHON_SUFFIXES, SCRUBBED, PY_WRAPPER, NODE_WRAPPER, isObject, tail;
7651
8510
  var init_transform = __esm({
7652
8511
  "src/cli/platform/worldData/transform.ts"() {
7653
8512
  "use strict";
7654
8513
  import_node_child_process3 = require("child_process");
7655
8514
  fs10 = __toESM(require("fs"));
7656
8515
  os2 = __toESM(require("os"));
7657
- path10 = __toESM(require("path"));
8516
+ path11 = __toESM(require("path"));
7658
8517
  import_node_module = require("module");
7659
8518
  init_router();
7660
8519
  import_meta = {};
@@ -7774,7 +8633,7 @@ async function forEachLimited(items, limit, fn) {
7774
8633
  if (failure) throw failure;
7775
8634
  }
7776
8635
  async function waitForBatch(client2, batchId, opts = {}) {
7777
- const sleep3 = opts.sleep ?? client2.sleep;
8636
+ const sleep5 = opts.sleep ?? client2.sleep;
7778
8637
  let delay = opts.initialDelayMs ?? 1e3;
7779
8638
  let last = "";
7780
8639
  for (; ; ) {
@@ -7785,7 +8644,7 @@ async function waitForBatch(client2, batchId, opts = {}) {
7785
8644
  last = phase;
7786
8645
  }
7787
8646
  if (isSettled(status)) return status;
7788
- await sleep3(delay);
8647
+ await sleep5(delay);
7789
8648
  delay = Math.min(1e4, Math.round(delay * 1.5));
7790
8649
  }
7791
8650
  }
@@ -7809,10 +8668,10 @@ var init_batches = __esm({
7809
8668
  ]);
7810
8669
  sleepMs = (ms) => new Promise((resolve11) => setTimeout(resolve11, ms));
7811
8670
  WorldDataApi = class {
7812
- constructor(creds, slug, sleep3 = sleepMs) {
8671
+ constructor(creds, slug, sleep5 = sleepMs) {
7813
8672
  this.creds = creds;
7814
8673
  this.slug = slug;
7815
- this.sleep = sleep3;
8674
+ this.sleep = sleep5;
7816
8675
  this.base = `/api/public/worlds/${encodeURIComponent(slug)}/data`;
7817
8676
  }
7818
8677
  creds;
@@ -8216,19 +9075,13 @@ function toolCallsFromObservations(observations, opts = {}) {
8216
9075
  }
8217
9076
  return out;
8218
9077
  }
8219
- function credentialsOf(options) {
8220
- const host = (options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai").replace(/\/+$/, "");
8221
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8222
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8223
- return publicKey && secretKey ? { host, publicKey, secretKey } : null;
8224
- }
8225
9078
  async function ingest(world, records, options = {}) {
8226
9079
  const { ingestLocalToolCalls: ingestLocalToolCalls2, importLocalWorldData: importLocalWorldData2, checkLocalWorldData: checkLocalWorldData2 } = await Promise.resolve().then(() => (init_data(), data_exports));
8227
9080
  if (options.transform !== void 0 && options.map !== void 0) throw new Error("records go through one of transform, map");
8228
9081
  const redact = options.redact ?? "off";
8229
9082
  const mode = options.mode ?? "append";
8230
9083
  const dryRun = Boolean(options.dryRun);
8231
- const creds = credentialsOf(options);
9084
+ const creds = platformEndpoint(options);
8232
9085
  const local = fs12.existsSync(world) && fs12.statSync(world).isDirectory();
8233
9086
  const map = options.map === void 0 ? void 0 : options.map;
8234
9087
  let rows = null;
@@ -8242,12 +9095,12 @@ async function ingest(world, records, options = {}) {
8242
9095
  const landed = dryRun ? await checkLocalWorldData2(world, rows, creds, { redact }) : await importLocalWorldData2(world, rows, { mode, redact }, creds);
8243
9096
  return transformReport(world, records.length, rows, stderr, { dryRun, redact, changed: !dryRun && landed.changed, kind: landed.kind, entityCounts: landed.entity_counts });
8244
9097
  }
8245
- if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
9098
+ if (!creds) throw new Error(GATEWAY_KEYS_MISSING);
8246
9099
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
8247
9100
  const slug = world.split("@", 1)[0];
8248
- const scratch = fs12.mkdtempSync(path11.join(os3.tmpdir(), "gateway-ingest-"));
9101
+ const scratch = fs12.mkdtempSync(path12.join(os3.tmpdir(), "gateway-ingest-"));
8249
9102
  try {
8250
- const pulled = await pull2(world, path11.join(scratch, slug), options);
9103
+ const pulled = await pull2(world, path12.join(scratch, slug), options);
8251
9104
  let report;
8252
9105
  let projected;
8253
9106
  if (rows === null) {
@@ -8321,14 +9174,15 @@ async function sessionToolObservations(sessionId, options = {}) {
8321
9174
  for (const observation of trace5.observations ?? []) out.push({ traceId: trace5.id ?? null, ...observation });
8322
9175
  return out;
8323
9176
  }
8324
- var fs12, os3, path11, isObject2, isoOf;
9177
+ var fs12, os3, path12, isObject2, isoOf;
8325
9178
  var init_ingest = __esm({
8326
9179
  "src/worlds/ingest.ts"() {
8327
9180
  "use strict";
8328
9181
  fs12 = __toESM(require("fs"));
8329
9182
  os3 = __toESM(require("os"));
8330
- path11 = __toESM(require("path"));
9183
+ path12 = __toESM(require("path"));
8331
9184
  init_worlds();
9185
+ init_platformEndpoint();
8332
9186
  isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
8333
9187
  isoOf = (value) => value instanceof Date ? value.toISOString() : typeof value === "string" ? value : void 0;
8334
9188
  }
@@ -8429,7 +9283,7 @@ var init_dryRun = __esm({
8429
9283
  /** Poll until the run settles or `timeoutSeconds` pass; the last verdict either way. */
8430
9284
  async settle(runId, opts) {
8431
9285
  const now = opts.now ?? Date.now;
8432
- const sleep3 = opts.sleep ?? ((ms) => new Promise((resolve11) => setTimeout(resolve11, ms)));
9286
+ const sleep5 = opts.sleep ?? ((ms) => new Promise((resolve11) => setTimeout(resolve11, ms)));
8433
9287
  const deadline = now() + opts.timeoutSeconds * 1e3;
8434
9288
  for (; ; ) {
8435
9289
  const budget = Math.floor(
@@ -8439,7 +9293,7 @@ var init_dryRun = __esm({
8439
9293
  const verdict = await this.status(runId, budget);
8440
9294
  if (verdict.settled || now() >= deadline) return verdict;
8441
9295
  opts.onProgress?.(verdict);
8442
- if (now() - asked < 1e3) await sleep3(1e3);
9296
+ if (now() - asked < 1e3) await sleep5(1e3);
8443
9297
  }
8444
9298
  }
8445
9299
  };
@@ -8507,6 +9361,8 @@ __export(worlds_exports, {
8507
9361
  EXPLORATORY_TASK: () => EXPLORATORY_TASK,
8508
9362
  EXPLORATORY_TASK_SOURCE: () => EXPLORATORY_TASK_SOURCE,
8509
9363
  GatewayEvalsClient: () => GatewayEvalsClient,
9364
+ GradePayload: () => GradePayload,
9365
+ GradeRefused: () => GradeRefused,
8510
9366
  HostedDryRunClient: () => HostedDryRunClient,
8511
9367
  IDLE_WARNING_MINUTES: () => IDLE_WARNING_MINUTES,
8512
9368
  KEY_NAMES: () => KEY_NAMES,
@@ -8522,6 +9378,7 @@ __export(worlds_exports, {
8522
9378
  PlatformTransport: () => PlatformTransport,
8523
9379
  PushSlugConflict: () => PushSlugConflict,
8524
9380
  RECOVER_BUDGET_MS: () => RECOVER_BUDGET_MS,
9381
+ RetryPolicy: () => RetryPolicy,
8525
9382
  SANDBOX_REQUEST_TIMEOUT_MS: () => SANDBOX_REQUEST_TIMEOUT_MS,
8526
9383
  SECRET_KEY: () => SECRET_KEY,
8527
9384
  SESSION_STATES: () => SESSION_STATES,
@@ -8529,8 +9386,10 @@ __export(worlds_exports, {
8529
9386
  STATUS_WAIT_CAP_SECONDS: () => STATUS_WAIT_CAP_SECONDS,
8530
9387
  STRIP_PREFIX: () => STRIP_PREFIX,
8531
9388
  SURFACE_KINDS: () => SURFACE_KINDS,
9389
+ Score: () => Score,
8532
9390
  Screen: () => Screen,
8533
9391
  SecretScrubber: () => SecretScrubber,
9392
+ TOOL_CONTRACT_UNREAD: () => TOOL_CONTRACT_UNREAD,
8534
9393
  TRANSFORMS: () => TRANSFORMS,
8535
9394
  TaskEditClient: () => TaskEditClient,
8536
9395
  TaskRun: () => TaskRun,
@@ -8542,13 +9401,17 @@ __export(worlds_exports, {
8542
9401
  WorldBrowser: () => WorldBrowser,
8543
9402
  WorldCallTooLarge: () => WorldCallTooLarge,
8544
9403
  WorldEditClient: () => WorldEditClient,
9404
+ WorldNotice: () => WorldNotice,
9405
+ WorldRequestLog: () => WorldRequestLog,
8545
9406
  WorldSandboxClient: () => WorldSandboxClient,
8546
9407
  WorldSession: () => WorldSession,
8547
9408
  WorldSessionCallError: () => WorldSessionCallError,
9409
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
8548
9410
  WorldSessionTimeout: () => WorldSessionTimeout,
8549
9411
  WorldSet: () => WorldSet,
8550
9412
  WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
8551
9413
  WorldTask: () => WorldTask,
9414
+ WorldToolsUnread: () => WorldToolsUnread,
8552
9415
  aggregateTaskGrade: () => aggregateTaskGrade,
8553
9416
  apiRequest: () => apiRequest,
8554
9417
  applyTransforms: () => applyTransforms,
@@ -8591,6 +9454,7 @@ __export(worlds_exports, {
8591
9454
  listTaskSessions: () => listTaskSessions,
8592
9455
  listTasks: () => listTasks,
8593
9456
  localTreeState: () => localTreeState,
9457
+ messageOf: () => messageOf,
8594
9458
  migrateProjectWorlds: () => migrateProjectWorlds,
8595
9459
  migrateWorld: () => migrateWorld,
8596
9460
  modelToolName: () => modelToolName,
@@ -8634,9 +9498,14 @@ __export(worlds_exports, {
8634
9498
  setEnvSecret: () => setEnvSecret,
8635
9499
  shapeText: () => shapeText,
8636
9500
  snapshotTreeHashes: () => snapshotTreeHashes,
9501
+ sqlRows: () => sqlRows,
9502
+ stillStartingHint: () => stillStartingHint,
8637
9503
  storedForms: () => storedForms,
9504
+ surfaceDescriptor: () => surfaceDescriptor,
8638
9505
  surfaceKinds: () => surfaceKinds,
9506
+ taskGradeError: () => taskGradeError,
8639
9507
  taskLabel: () => taskLabel,
9508
+ taskRunFailure: () => taskRunFailure,
8640
9509
  toolCallRecordsIn: () => toolCallRecordsIn,
8641
9510
  toolCallsFromObservations: () => toolCallsFromObservations,
8642
9511
  uiEntryUrl: () => uiEntryUrl,
@@ -8650,17 +9519,6 @@ __export(worlds_exports, {
8650
9519
  withoutUiToken: () => withoutUiToken,
8651
9520
  writeLocalMeta: () => writeLocalMeta
8652
9521
  });
8653
- function resolveEnv(options) {
8654
- const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
8655
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8656
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8657
- if (!publicKey || !secretKey) {
8658
- throw new Error(
8659
- "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey)."
8660
- );
8661
- }
8662
- return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
8663
- }
8664
9522
  function parseErrorBody(text2, limit = 600) {
8665
9523
  const raw = { detail: text2.slice(0, limit), code: null };
8666
9524
  let parsed;
@@ -8680,9 +9538,9 @@ function parseErrorBody(text2, limit = 600) {
8680
9538
  code: stableCode
8681
9539
  };
8682
9540
  }
8683
- async function apiRequest(options, method, path16, body, init3 = {}) {
8684
- const reply = await new PlatformTransport(resolveEnv(options)).send(method, path16, body, init3);
8685
- if (!reply.ok) throw new ApiError(reply.status, method, path16, reply.text);
9541
+ async function apiRequest(options, method, path17, body, init3 = {}) {
9542
+ const reply = await new PlatformTransport(requirePlatformEndpoint(options)).send(method, path17, body, init3);
9543
+ if (!reply.ok) throw new ApiError(reply.status, method, path17, reply.text);
8686
9544
  return JSON.parse(reply.text);
8687
9545
  }
8688
9546
  async function setEnvSecret(slug, key2, value, options = {}) {
@@ -8794,7 +9652,6 @@ function detectProvenance() {
8794
9652
  kind: "ci",
8795
9653
  id: env.GITHUB_ACTOR,
8796
9654
  name: env.GITHUB_ACTOR,
8797
- // GitHub serves every account's avatar at /<login>.png.
8798
9655
  imageUrl: `${server}/${env.GITHUB_ACTOR}.png`
8799
9656
  };
8800
9657
  }
@@ -8863,9 +9720,11 @@ var ApiError, WorldTask, WorldSet;
8863
9720
  var init_worlds = __esm({
8864
9721
  "src/worlds/index.ts"() {
8865
9722
  "use strict";
9723
+ init_platformEndpoint();
8866
9724
  init_transport();
8867
9725
  init_transport();
8868
9726
  init_session2();
9727
+ init_grade();
8869
9728
  init_sessionList();
8870
9729
  init_sessionContract();
8871
9730
  init_taskRef();
@@ -8876,6 +9735,8 @@ var init_worlds = __esm({
8876
9735
  init_checkoutProvenance();
8877
9736
  init_evals();
8878
9737
  init_browser();
9738
+ init_surfaceDescriptor();
9739
+ init_requestLog();
8879
9740
  init_browserReconnect();
8880
9741
  init_nativeComputer();
8881
9742
  init_hub();
@@ -8889,10 +9750,11 @@ var init_worlds = __esm({
8889
9750
  init_sandbox();
8890
9751
  init_editSessions();
8891
9752
  init_dryRun();
9753
+ init_worldNotice();
8892
9754
  ApiError = class extends Error {
8893
- constructor(status, method, path16, text2) {
9755
+ constructor(status, method, path17, text2) {
8894
9756
  const parsed = parseErrorBody(text2);
8895
- super(`${method} ${path16} -> ${status}: ${parsed.detail}`);
9757
+ super(`${method} ${path17} -> ${status}: ${parsed.detail}`);
8896
9758
  this.status = status;
8897
9759
  this.text = text2;
8898
9760
  this.name = "ApiError";
@@ -9029,6 +9891,8 @@ __export(src_exports, {
9029
9891
  GatewaySecurityError: () => GatewaySecurityError,
9030
9892
  GatewayToolApprovalRequiredError: () => GatewayToolApprovalRequiredError,
9031
9893
  GatewayToolBlockedError: () => GatewayToolBlockedError,
9894
+ GradePayload: () => GradePayload,
9895
+ GradeRefused: () => GradeRefused,
9032
9896
  HostedDryRunClient: () => HostedDryRunClient,
9033
9897
  IDLE_WARNING_MINUTES: () => IDLE_WARNING_MINUTES,
9034
9898
  KEY_NAMES: () => KEY_NAMES,
@@ -9054,6 +9918,7 @@ __export(src_exports, {
9054
9918
  ReplayReport: () => ReplayReport,
9055
9919
  ReplaySuite: () => ReplaySuite,
9056
9920
  ReplayTask: () => ReplayTask,
9921
+ RetryPolicy: () => RetryPolicy,
9057
9922
  SANDBOX_REQUEST_TIMEOUT_MS: () => SANDBOX_REQUEST_TIMEOUT_MS,
9058
9923
  SECRET_KEY: () => SECRET_KEY,
9059
9924
  SESSION_STATES: () => SESSION_STATES,
@@ -9063,6 +9928,7 @@ __export(src_exports, {
9063
9928
  SUCCESS_METRIC_NAME_KEY: () => SUCCESS_METRIC_NAME_KEY,
9064
9929
  SUCCESS_SIGNAL_KEY: () => SUCCESS_SIGNAL_KEY,
9065
9930
  SURFACE_KINDS: () => SURFACE_KINDS,
9931
+ Score: () => Score,
9066
9932
  ScoreError: () => ScoreError,
9067
9933
  ScoresClient: () => ScoresClient,
9068
9934
  Screen: () => Screen,
@@ -9071,6 +9937,7 @@ __export(src_exports, {
9071
9937
  SessionRecording: () => SessionRecording,
9072
9938
  SessionTrace: () => SessionTrace,
9073
9939
  StubMissError: () => StubMissError,
9940
+ TOOL_CONTRACT_UNREAD: () => TOOL_CONTRACT_UNREAD,
9074
9941
  TRANSFORMS: () => TRANSFORMS,
9075
9942
  TaskEditClient: () => TaskEditClient,
9076
9943
  TaskRun: () => TaskRun,
@@ -9086,13 +9953,17 @@ __export(src_exports, {
9086
9953
  WorldBrowser: () => WorldBrowser,
9087
9954
  WorldCallTooLarge: () => WorldCallTooLarge,
9088
9955
  WorldEditClient: () => WorldEditClient,
9956
+ WorldNotice: () => WorldNotice,
9957
+ WorldRequestLog: () => WorldRequestLog,
9089
9958
  WorldSandboxClient: () => WorldSandboxClient,
9090
9959
  WorldSession: () => WorldSession,
9091
9960
  WorldSessionCallError: () => WorldSessionCallError,
9961
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
9092
9962
  WorldSessionTimeout: () => WorldSessionTimeout,
9093
9963
  WorldSet: () => WorldSet,
9094
9964
  WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
9095
9965
  WorldTask: () => WorldTask,
9966
+ WorldToolsUnread: () => WorldToolsUnread,
9096
9967
  addExperimentAttributes: () => addExperimentAttributes,
9097
9968
  aggregateTaskGrade: () => aggregateTaskGrade,
9098
9969
  apiRequest: () => apiRequest,
@@ -9159,6 +10030,7 @@ __export(src_exports, {
9159
10030
  loadSession: () => loadSession,
9160
10031
  localTreeState: () => localTreeState,
9161
10032
  memory: () => memory,
10033
+ messageOf: () => messageOf,
9162
10034
  migrateProjectWorlds: () => migrateProjectWorlds,
9163
10035
  migrateWorld: () => migrateWorld,
9164
10036
  modelToolName: () => modelToolName,
@@ -9216,11 +10088,16 @@ __export(src_exports, {
9216
10088
  setExperimentContext: () => setExperimentContext,
9217
10089
  shapeText: () => shapeText,
9218
10090
  snapshotTreeHashes: () => snapshotTreeHashes,
10091
+ sqlRows: () => sqlRows,
10092
+ stillStartingHint: () => stillStartingHint,
9219
10093
  storedForms: () => storedForms,
9220
10094
  success: () => success,
9221
10095
  suite: () => suite,
10096
+ surfaceDescriptor: () => surfaceDescriptor,
9222
10097
  surfaceKinds: () => surfaceKinds,
10098
+ taskGradeError: () => taskGradeError,
9223
10099
  taskLabel: () => taskLabel,
10100
+ taskRunFailure: () => taskRunFailure,
9224
10101
  toolCallRecordsIn: () => toolCallRecordsIn,
9225
10102
  toolCallsFromObservations: () => toolCallsFromObservations,
9226
10103
  traces: () => traces_exports,
@@ -9809,8 +10686,8 @@ var GatewayContext = class _GatewayContext {
9809
10686
  }
9810
10687
  return resolved;
9811
10688
  }
9812
- async requestJson(method, path16, payload) {
9813
- const response = await fetch(`${this.host}${path16}`, {
10689
+ async requestJson(method, path17, payload) {
10690
+ const response = await fetch(`${this.host}${path17}`, {
9814
10691
  method,
9815
10692
  headers: {
9816
10693
  Authorization: this.authHeader(),
@@ -9821,7 +10698,7 @@ var GatewayContext = class _GatewayContext {
9821
10698
  const text2 = await response.text();
9822
10699
  const body = text2 ? safeJson(text2) : {};
9823
10700
  if (!response.ok) {
9824
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path16}`;
10701
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path17}`;
9825
10702
  if (response.status === 409) {
9826
10703
  throw new GatewayContextConflictError(
9827
10704
  message,
@@ -9830,7 +10707,7 @@ var GatewayContext = class _GatewayContext {
9830
10707
  }
9831
10708
  throw new GatewayContextError(message);
9832
10709
  }
9833
- this.log("%s %s -> %s", method, path16, response.status);
10710
+ this.log("%s %s -> %s", method, path17, response.status);
9834
10711
  return body;
9835
10712
  }
9836
10713
  authHeader() {
@@ -9937,11 +10814,11 @@ function primaryPathForKind(kind, snapshot) {
9937
10814
  const preferred = kind === "agent" ? "AGENTS.md" : kind === "prompt" ? "PROMPT.md" : kind === "skill" ? "SKILL.md" : "MEMORY.md";
9938
10815
  if (readFile(snapshot, preferred)) return preferred;
9939
10816
  return Object.entries(snapshot.files).find(
9940
- ([path16, entry]) => entry.type === "file" && path16.endsWith(".md")
10817
+ ([path17, entry]) => entry.type === "file" && path17.endsWith(".md")
9941
10818
  )?.[0] ?? null;
9942
10819
  }
9943
- function parseJsonFile(snapshot, path16) {
9944
- const content = readFile(snapshot, path16);
10820
+ function parseJsonFile(snapshot, path17) {
10821
+ const content = readFile(snapshot, path17);
9945
10822
  if (!content) return {};
9946
10823
  try {
9947
10824
  const parsed = JSON.parse(content);
@@ -9950,8 +10827,8 @@ function parseJsonFile(snapshot, path16) {
9950
10827
  return {};
9951
10828
  }
9952
10829
  }
9953
- function readFile(snapshot, path16) {
9954
- const entry = snapshot.files[path16];
10830
+ function readFile(snapshot, path17) {
10831
+ const entry = snapshot.files[path17];
9955
10832
  return entry?.type === "file" ? entry.content : null;
9956
10833
  }
9957
10834
  function provenanceFor(snapshot, label) {
@@ -10053,16 +10930,16 @@ var GatewayMemory = class _GatewayMemory {
10053
10930
  `/api/public/context/memory/${encodeIdentifier2(identifier)}${suffix}`
10054
10931
  );
10055
10932
  }
10056
- async read(target, path16, options = {}) {
10933
+ async read(target, path17, options = {}) {
10057
10934
  const snapshot = typeof target === "string" ? await this.pull(target, options) : target;
10058
- const file = snapshot.files[path16];
10935
+ const file = snapshot.files[path17];
10059
10936
  return file?.type === "file" ? file.content : null;
10060
10937
  }
10061
- async write(target, path16, content, options = {}) {
10938
+ async write(target, path17, content, options = {}) {
10062
10939
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
10063
10940
  return this.push(snapshot.repo.repoHandle, {
10064
10941
  files: {
10065
- [path16]: {
10942
+ [path17]: {
10066
10943
  content,
10067
10944
  ...options.mimeType ? { mimeType: options.mimeType } : {}
10068
10945
  }
@@ -10072,10 +10949,10 @@ var GatewayMemory = class _GatewayMemory {
10072
10949
  ...runtimeContextPayload2(options)
10073
10950
  });
10074
10951
  }
10075
- async delete(target, path16, options = {}) {
10952
+ async delete(target, path17, options = {}) {
10076
10953
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
10077
10954
  return this.push(snapshot.repo.repoHandle, {
10078
- files: { [path16]: null },
10955
+ files: { [path17]: null },
10079
10956
  parentCommit: options.parentCommit ?? snapshot.commit.commitHash,
10080
10957
  message: options.message,
10081
10958
  ...runtimeContextPayload2(options)
@@ -10088,8 +10965,8 @@ var GatewayMemory = class _GatewayMemory {
10088
10965
  payload
10089
10966
  );
10090
10967
  }
10091
- async requestJson(method, path16, payload) {
10092
- const response = await fetch(`${this.host}${path16}`, {
10968
+ async requestJson(method, path17, payload) {
10969
+ const response = await fetch(`${this.host}${path17}`, {
10093
10970
  method,
10094
10971
  headers: {
10095
10972
  Authorization: this.authHeader(),
@@ -10100,7 +10977,7 @@ var GatewayMemory = class _GatewayMemory {
10100
10977
  const text2 = await response.text();
10101
10978
  const body = text2 ? safeJson2(text2) : {};
10102
10979
  if (!response.ok) {
10103
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path16}`;
10980
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path17}`;
10104
10981
  if (response.status === 409) {
10105
10982
  throw new GatewayMemoryConflictError(
10106
10983
  message,
@@ -10109,7 +10986,7 @@ var GatewayMemory = class _GatewayMemory {
10109
10986
  }
10110
10987
  throw new GatewayMemoryError(message);
10111
10988
  }
10112
- this.log("%s %s -> %s", method, path16, response.status);
10989
+ this.log("%s %s -> %s", method, path17, response.status);
10113
10990
  return body;
10114
10991
  }
10115
10992
  authHeader() {
@@ -10271,10 +11148,10 @@ var PersonasClient = class _PersonasClient {
10271
11148
  done: Boolean(payload.done)
10272
11149
  };
10273
11150
  }
10274
- async requestJson(method, path16, payload) {
11151
+ async requestJson(method, path17, payload) {
10275
11152
  let response;
10276
11153
  try {
10277
- response = await fetch(`${this.host}${path16}`, {
11154
+ response = await fetch(`${this.host}${path17}`, {
10278
11155
  method,
10279
11156
  headers: {
10280
11157
  Authorization: this.authHeader(),
@@ -10285,7 +11162,7 @@ var PersonasClient = class _PersonasClient {
10285
11162
  });
10286
11163
  } catch (error) {
10287
11164
  const reason = error instanceof Error ? error.message : String(error);
10288
- throw new PersonaError(`${method} ${path16} failed: ${reason}`, {
11165
+ throw new PersonaError(`${method} ${path17} failed: ${reason}`, {
10289
11166
  cause: error
10290
11167
  });
10291
11168
  }
@@ -10293,10 +11170,10 @@ var PersonasClient = class _PersonasClient {
10293
11170
  if (!response.ok) {
10294
11171
  const detail = text2.slice(0, 500);
10295
11172
  throw new PersonaError(
10296
- `${method} ${path16} failed (${response.status}): ${detail}`
11173
+ `${method} ${path17} failed (${response.status}): ${detail}`
10297
11174
  );
10298
11175
  }
10299
- this.log("%s %s -> %s", method, path16, response.status);
11176
+ this.log("%s %s -> %s", method, path17, response.status);
10300
11177
  return safeJson3(text2);
10301
11178
  }
10302
11179
  authHeader() {
@@ -10521,10 +11398,10 @@ var SessionRecording = class _SessionRecording {
10521
11398
  };
10522
11399
  }
10523
11400
  /** Dump the full session as a JSON fixture (for regression tests). */
10524
- toFixture(path16) {
10525
- (0, import_node_fs.mkdirSync)((0, import_node_path.dirname)(path16), { recursive: true });
10526
- (0, import_node_fs.writeFileSync)(path16, JSON.stringify(this.toJSON(), null, 2), "utf-8");
10527
- return path16;
11401
+ toFixture(path17) {
11402
+ (0, import_node_fs.mkdirSync)((0, import_node_path.dirname)(path17), { recursive: true });
11403
+ (0, import_node_fs.writeFileSync)(path17, JSON.stringify(this.toJSON(), null, 2), "utf-8");
11404
+ return path17;
10528
11405
  }
10529
11406
  static fromApi(raw) {
10530
11407
  return new _SessionRecording({
@@ -11555,8 +12432,8 @@ var GatewaySecurity = class _GatewaySecurity {
11555
12432
  );
11556
12433
  }
11557
12434
  }
11558
- async postJson(path16, payload) {
11559
- const response = await fetch(`${this.host}${path16}`, {
12435
+ async postJson(path17, payload) {
12436
+ const response = await fetch(`${this.host}${path17}`, {
11560
12437
  method: "POST",
11561
12438
  headers: {
11562
12439
  Authorization: this.authHeader(),
@@ -11567,7 +12444,7 @@ var GatewaySecurity = class _GatewaySecurity {
11567
12444
  if (!response.ok) {
11568
12445
  const body = await response.text();
11569
12446
  throw new GatewaySecurityError(
11570
- `Gateway API ${response.status} on ${path16}: ${body || response.statusText}`
12447
+ `Gateway API ${response.status} on ${path17}: ${body || response.statusText}`
11571
12448
  );
11572
12449
  }
11573
12450
  return await response.json();
@@ -11659,7 +12536,7 @@ __export(files_exports, {
11659
12536
  });
11660
12537
  var import_node_crypto11 = require("crypto");
11661
12538
  var fs13 = __toESM(require("fs"));
11662
- var path12 = __toESM(require("path"));
12539
+ var path13 = __toESM(require("path"));
11663
12540
  var import_node_stream = require("stream");
11664
12541
  var import_promises = require("stream/promises");
11665
12542
  init_worlds();
@@ -11711,7 +12588,7 @@ async function readFile2(options = {}, filePath, limits = {}) {
11711
12588
  async function downloadFile(options = {}, filePath, outPath, onProgress) {
11712
12589
  const file = await getFile(options, filePath);
11713
12590
  const res = await openDownload(file.url);
11714
- fs13.mkdirSync(path12.dirname(path12.resolve(outPath)), { recursive: true });
12591
+ fs13.mkdirSync(path13.dirname(path13.resolve(outPath)), { recursive: true });
11715
12592
  const source = import_node_stream.Readable.fromWeb(res.body);
11716
12593
  await (0, import_promises.pipeline)(source, counting((n) => onProgress?.(n, file.sizeBytes)), fs13.createWriteStream(outPath));
11717
12594
  return { ...file, out: outPath };
@@ -11760,7 +12637,7 @@ var CONTENT_TYPES = {
11760
12637
  ".mp4": "video/mp4"
11761
12638
  };
11762
12639
  function contentTypeFor(name) {
11763
- return CONTENT_TYPES[path12.extname(name).toLowerCase()] ?? "application/octet-stream";
12640
+ return CONTENT_TYPES[path13.extname(name).toLowerCase()] ?? "application/octet-stream";
11764
12641
  }
11765
12642
  function counting(report) {
11766
12643
  let seen = 0;
@@ -13582,9 +14459,9 @@ function alternatives(raw) {
13582
14459
  if (!parts2.length || parts2.includes("*")) return [];
13583
14460
  return parts2;
13584
14461
  }
13585
- function hasPath(node, path16) {
14462
+ function hasPath(node, path17) {
13586
14463
  let current = node;
13587
- for (const part of path16.split(".")) {
14464
+ for (const part of path17.split(".")) {
13588
14465
  if (current && typeof current === "object" && !Array.isArray(current) && part in current) {
13589
14466
  current = current[part];
13590
14467
  continue;
@@ -13625,7 +14502,7 @@ function matches(m, event) {
13625
14502
  if (m.args.length) {
13626
14503
  const haystacks = event.event === "pre_request" || event.event === "post_response" ? [event.request?.query, event.request?.body] : [event.toolInput];
13627
14504
  const present = haystacks.filter((h) => h !== void 0 && h !== null);
13628
- if (!m.args.every((path16) => present.some((h) => hasPath(h, path16))))
14505
+ if (!m.args.every((path17) => present.some((h) => hasPath(h, path17))))
13629
14506
  return false;
13630
14507
  }
13631
14508
  return true;
@@ -14461,7 +15338,7 @@ function parseHandler(event, raw, position) {
14461
15338
  // src/hooks/handlers.ts
14462
15339
  var import_node_child_process4 = require("child_process");
14463
15340
  var fs15 = __toESM(require("fs"));
14464
- var path13 = __toESM(require("path"));
15341
+ var path14 = __toESM(require("path"));
14465
15342
  var import_node_url = require("url");
14466
15343
  function parseStdout(text2, handler) {
14467
15344
  const stripped = text2.trim();
@@ -14662,7 +15539,7 @@ var RecordHandler = class {
14662
15539
  record.source = this.opts.source ?? null;
14663
15540
  if (Array.isArray(this.sink)) this.sink.push(record);
14664
15541
  else if (typeof this.sink === "string") {
14665
- fs15.mkdirSync(path13.dirname(this.sink), { recursive: true });
15542
+ fs15.mkdirSync(path14.dirname(this.sink), { recursive: true });
14666
15543
  fs15.appendFileSync(this.sink, JSON.stringify(record) + "\n");
14667
15544
  } else await this.sink(record);
14668
15545
  return allow();
@@ -14873,7 +15750,7 @@ async function importCallable(target, cwd) {
14873
15750
  const hash = target.lastIndexOf("#");
14874
15751
  const modulePath = target.slice(0, hash);
14875
15752
  const exportName = target.slice(hash + 1) || "default";
14876
- const resolved = modulePath.startsWith(".") || path13.isAbsolute(modulePath) ? (0, import_node_url.pathToFileURL)(path13.resolve(cwd, modulePath)).href : modulePath;
15753
+ const resolved = modulePath.startsWith(".") || path14.isAbsolute(modulePath) ? (0, import_node_url.pathToFileURL)(path14.resolve(cwd, modulePath)).href : modulePath;
14877
15754
  let module2;
14878
15755
  try {
14879
15756
  module2 = await import(resolved);
@@ -14937,7 +15814,7 @@ async function buildHandler(spec, aliases, opts) {
14937
15814
 
14938
15815
  // src/hooks/live.ts
14939
15816
  var fs16 = __toESM(require("fs"));
14940
- var path14 = __toESM(require("path"));
15817
+ var path15 = __toESM(require("path"));
14941
15818
  var USER_AGENT = "gateway-hooks/1 (+https://withgateway.ai)";
14942
15819
  var TAGS = /<(script|style)[^>]*>.*?<\/\1>/gis;
14943
15820
  var STRIP = /<[^>]+>/g;
@@ -14957,13 +15834,13 @@ var Pacer = class {
14957
15834
  /** Waits until the interval since the tool's last call has passed; resolves to the
14958
15835
  * seconds waited. `sleep`/`clock` are injectable for tests. */
14959
15836
  async wait(name, intervalMs, opts = {}) {
14960
- const sleep3 = opts.sleep ?? ((seconds) => new Promise((resolve11) => setTimeout(resolve11, seconds * 1e3)));
15837
+ const sleep5 = opts.sleep ?? ((seconds) => new Promise((resolve11) => setTimeout(resolve11, seconds * 1e3)));
14961
15838
  const clock = opts.clock ?? (() => Date.now() / 1e3);
14962
15839
  const now = clock();
14963
15840
  const due = (this.last.get(name) ?? -Infinity) + intervalMs / 1e3;
14964
15841
  const waited = Math.max(0, due - now);
14965
15842
  this.last.set(name, Math.max(now, due));
14966
- if (waited) await sleep3(waited);
15843
+ if (waited) await sleep5(waited);
14967
15844
  return Math.round(waited * 1e3) / 1e3;
14968
15845
  }
14969
15846
  };
@@ -15305,7 +16182,7 @@ var CallableTool = class _CallableTool {
15305
16182
  }
15306
16183
  };
15307
16184
  function readConnection(root) {
15308
- const file = path14.join(root, "connector.toml");
16185
+ const file = path15.join(root, "connector.toml");
15309
16186
  if (!fs16.existsSync(file)) throw new Error(`no connector.toml at ${root}`);
15310
16187
  const raw = parseToml(fs16.readFileSync(file, "utf8"));
15311
16188
  const connector = raw.connector ?? {};
@@ -15321,7 +16198,7 @@ function readConnection(root) {
15321
16198
  };
15322
16199
  }
15323
16200
  return {
15324
- slug: connector.slug ?? path14.basename(root),
16201
+ slug: connector.slug ?? path15.basename(root),
15325
16202
  baseUrl: (connector.base_url ?? "").replace(/\/+$/, ""),
15326
16203
  scheme: auth.scheme ?? "none",
15327
16204
  ...typeof auth.name === "string" ? { authName: auth.name } : {},
@@ -15338,8 +16215,8 @@ var ConnectorTool = class {
15338
16215
  this.spec = spec;
15339
16216
  this.live = live;
15340
16217
  let location = spec.options.connector;
15341
- if (!path14.isAbsolute(location)) location = path14.resolve(baseDir, location);
15342
- const root = fs16.existsSync(location) && fs16.statSync(location).isFile() ? path14.dirname(location) : location;
16218
+ if (!path15.isAbsolute(location)) location = path15.resolve(baseDir, location);
16219
+ const root = fs16.existsSync(location) && fs16.statSync(location).isFile() ? path15.dirname(location) : location;
15343
16220
  try {
15344
16221
  this.connection = readConnection(root);
15345
16222
  } catch (error) {
@@ -15496,7 +16373,7 @@ var Recorder = class {
15496
16373
  }
15497
16374
  file;
15498
16375
  write(line) {
15499
- fs16.mkdirSync(path14.dirname(this.file), { recursive: true });
16376
+ fs16.mkdirSync(path15.dirname(this.file), { recursive: true });
15500
16377
  fs16.appendFileSync(this.file, canonical2(line) + "\n");
15501
16378
  }
15502
16379
  };
@@ -15592,7 +16469,7 @@ var LiveTools = class _LiveTools {
15592
16469
  ...config.live.record ? { record: interpolatePath(config.live.record, run.runId, directory) } : {},
15593
16470
  ...config.live.replay ? { replay: interpolatePath(config.live.replay, run.runId, directory) } : {}
15594
16471
  };
15595
- return _LiveTools.create(run, live, path14.dirname(path14.resolve(file)));
16472
+ return _LiveTools.create(run, live, path15.dirname(path15.resolve(file)));
15596
16473
  }
15597
16474
  /** The declared tools for a model. `openai`: `{type: "function", function: {name,
15598
16475
  * description, parameters}}`; `anthropic`: `{name, description, input_schema}`. */
@@ -15919,7 +16796,7 @@ var RedactionAudit = class _RedactionAudit {
15919
16796
  // src/hooks/run.ts
15920
16797
  var import_node_crypto14 = require("crypto");
15921
16798
  var fs17 = __toESM(require("fs"));
15922
- var path15 = __toESM(require("path"));
16799
+ var path16 = __toESM(require("path"));
15923
16800
  var BLOCKING_EVENTS = [
15924
16801
  "pre_tool_call",
15925
16802
  "pre_request",
@@ -15970,12 +16847,12 @@ var HookRun = class _HookRun {
15970
16847
  declaredOnly: options.declaredOnly ?? config.aliasDeclaredOnly
15971
16848
  });
15972
16849
  this.cwd = options.cwd ?? process.cwd();
15973
- this.runDir = options.runDir ?? path15.join(this.cwd, "runs", this.runId);
16850
+ this.runDir = options.runDir ?? path16.join(this.cwd, "runs", this.runId);
15974
16851
  const chosen = options.legend !== void 0 ? options.legend : config.aliasLegend;
15975
- this.legendPath = chosen ? path15.resolve(this.cwd, interpolatePath(chosen, this.runId, this.runDir)) : null;
16852
+ this.legendPath = chosen ? path16.resolve(this.cwd, interpolatePath(chosen, this.runId, this.runDir)) : null;
15976
16853
  this.sessionId = options.session;
15977
16854
  this.source = options.source;
15978
- this.configDir = config.source ? path15.dirname(path15.resolve(config.source)) : this.cwd;
16855
+ this.configDir = config.source ? path16.dirname(path16.resolve(config.source)) : this.cwd;
15979
16856
  }
15980
16857
  /** Build from a parsed config (an object in the file's shape, or a `HookConfig`);
15981
16858
  * handlers are constructed now, so a callable that does not import fails here. */
@@ -16385,7 +17262,7 @@ var HookRun = class _HookRun {
16385
17262
  throw new Error(
16386
17263
  "no legend path: set [alias] legend, pass legend to the run, or give a path"
16387
17264
  );
16388
- fs17.mkdirSync(path15.dirname(target), { recursive: true });
17265
+ fs17.mkdirSync(path16.dirname(target), { recursive: true });
16389
17266
  fs17.writeFileSync(target, JSON.stringify(this.legend(), null, 2) + "\n");
16390
17267
  return target;
16391
17268
  }
@@ -16482,14 +17359,14 @@ var HookedSession = class {
16482
17359
  * `apiHeaders()`), `pre_request` before it and `post_response` after. Returns
16483
17360
  * `{status, body, headers}`; a 4xx/5xx is returned, not thrown, so the agent sees
16484
17361
  * the vendor's own error shape (rewritten like any result). */
16485
- async request(method, path16, opts = {}) {
17362
+ async request(method, path17, opts = {}) {
16486
17363
  if (!this.inner.api && !this.requestFn)
16487
17364
  throw new Error(
16488
17365
  "this session has no api surface (pass requestFn for a transport of your own)"
16489
17366
  );
16490
17367
  let request = {
16491
17368
  method: method.toUpperCase(),
16492
- path: path16,
17369
+ path: path17,
16493
17370
  query: { ...opts.query ?? {} },
16494
17371
  body: opts.body ?? null
16495
17372
  };
@@ -16958,6 +17835,8 @@ var tracing = {
16958
17835
  GatewaySecurityError,
16959
17836
  GatewayToolApprovalRequiredError,
16960
17837
  GatewayToolBlockedError,
17838
+ GradePayload,
17839
+ GradeRefused,
16961
17840
  HostedDryRunClient,
16962
17841
  IDLE_WARNING_MINUTES,
16963
17842
  KEY_NAMES,
@@ -16983,6 +17862,7 @@ var tracing = {
16983
17862
  ReplayReport,
16984
17863
  ReplaySuite,
16985
17864
  ReplayTask,
17865
+ RetryPolicy,
16986
17866
  SANDBOX_REQUEST_TIMEOUT_MS,
16987
17867
  SECRET_KEY,
16988
17868
  SESSION_STATES,
@@ -16992,6 +17872,7 @@ var tracing = {
16992
17872
  SUCCESS_METRIC_NAME_KEY,
16993
17873
  SUCCESS_SIGNAL_KEY,
16994
17874
  SURFACE_KINDS,
17875
+ Score,
16995
17876
  ScoreError,
16996
17877
  ScoresClient,
16997
17878
  Screen,
@@ -17000,6 +17881,7 @@ var tracing = {
17000
17881
  SessionRecording,
17001
17882
  SessionTrace,
17002
17883
  StubMissError,
17884
+ TOOL_CONTRACT_UNREAD,
17003
17885
  TRANSFORMS,
17004
17886
  TaskEditClient,
17005
17887
  TaskRun,
@@ -17015,13 +17897,17 @@ var tracing = {
17015
17897
  WorldBrowser,
17016
17898
  WorldCallTooLarge,
17017
17899
  WorldEditClient,
17900
+ WorldNotice,
17901
+ WorldRequestLog,
17018
17902
  WorldSandboxClient,
17019
17903
  WorldSession,
17020
17904
  WorldSessionCallError,
17905
+ WorldSessionOpenFailed,
17021
17906
  WorldSessionTimeout,
17022
17907
  WorldSet,
17023
17908
  WorldSurfaceUnavailable,
17024
17909
  WorldTask,
17910
+ WorldToolsUnread,
17025
17911
  addExperimentAttributes,
17026
17912
  aggregateTaskGrade,
17027
17913
  apiRequest,
@@ -17088,6 +17974,7 @@ var tracing = {
17088
17974
  loadSession,
17089
17975
  localTreeState,
17090
17976
  memory,
17977
+ messageOf,
17091
17978
  migrateProjectWorlds,
17092
17979
  migrateWorld,
17093
17980
  modelToolName,
@@ -17145,11 +18032,16 @@ var tracing = {
17145
18032
  setExperimentContext,
17146
18033
  shapeText,
17147
18034
  snapshotTreeHashes,
18035
+ sqlRows,
18036
+ stillStartingHint,
17148
18037
  storedForms,
17149
18038
  success,
17150
18039
  suite,
18040
+ surfaceDescriptor,
17151
18041
  surfaceKinds,
18042
+ taskGradeError,
17152
18043
  taskLabel,
18044
+ taskRunFailure,
17153
18045
  toolCallRecordsIn,
17154
18046
  toolCallsFromObservations,
17155
18047
  traces,