@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.mjs CHANGED
@@ -190,10 +190,10 @@ var init_users = __esm({
190
190
  totalItems: Number(meta.totalItems ?? rows.length)
191
191
  };
192
192
  }
193
- async requestJson(method, path16, payload) {
193
+ async requestJson(method, path17, payload) {
194
194
  let response;
195
195
  try {
196
- response = await fetch(`${this.host}${path16}`, {
196
+ response = await fetch(`${this.host}${path17}`, {
197
197
  method,
198
198
  headers: {
199
199
  Authorization: this.authHeader(),
@@ -204,17 +204,17 @@ var init_users = __esm({
204
204
  });
205
205
  } catch (error) {
206
206
  throw new UserProfileError(
207
- `${method} ${path16} failed: ${error instanceof Error ? error.message : String(error)}`,
207
+ `${method} ${path17} failed: ${error instanceof Error ? error.message : String(error)}`,
208
208
  { cause: error }
209
209
  );
210
210
  }
211
211
  const text2 = await response.text();
212
212
  if (!response.ok) {
213
213
  throw new UserProfileError(
214
- `${method} ${path16} failed (${response.status}): ${text2.slice(0, 500)}`
214
+ `${method} ${path17} failed (${response.status}): ${text2.slice(0, 500)}`
215
215
  );
216
216
  }
217
- this.log("%s %s -> %s", method, path16, response.status);
217
+ this.log("%s %s -> %s", method, path17, response.status);
218
218
  return safeJson4(text2);
219
219
  }
220
220
  authHeader() {
@@ -403,10 +403,10 @@ var init_scores = __esm({
403
403
  metadata: signalMetadata
404
404
  });
405
405
  }
406
- async request(method, path16, payload) {
406
+ async request(method, path17, payload) {
407
407
  let response;
408
408
  try {
409
- response = await fetch(`${this.host}${path16}`, {
409
+ response = await fetch(`${this.host}${path17}`, {
410
410
  method,
411
411
  headers: {
412
412
  Authorization: this.authHeader(),
@@ -417,17 +417,17 @@ var init_scores = __esm({
417
417
  });
418
418
  } catch (error) {
419
419
  throw new ScoreError(
420
- `${method} ${path16} failed: ${error instanceof Error ? error.message : String(error)}`,
420
+ `${method} ${path17} failed: ${error instanceof Error ? error.message : String(error)}`,
421
421
  { cause: error }
422
422
  );
423
423
  }
424
424
  const text2 = await response.text();
425
425
  if (!response.ok) {
426
426
  throw new ScoreError(
427
- `${method} ${path16} failed (${response.status}): ${text2.slice(0, 500)}`
427
+ `${method} ${path17} failed (${response.status}): ${text2.slice(0, 500)}`
428
428
  );
429
429
  }
430
- this.log("%s %s -> %s", method, path16, response.status);
430
+ this.log("%s %s -> %s", method, path17, response.status);
431
431
  }
432
432
  authHeader() {
433
433
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -450,6 +450,28 @@ var init_scores = __esm({
450
450
  }
451
451
  });
452
452
 
453
+ // src/worlds/platformEndpoint.ts
454
+ function platformEndpoint(options) {
455
+ const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
456
+ const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
457
+ if (!publicKey || !secretKey) return null;
458
+ const host = options.host ?? process.env.GATEWAY_HOST ?? DEFAULT_HOST;
459
+ return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
460
+ }
461
+ function requirePlatformEndpoint(options) {
462
+ const endpoint = platformEndpoint(options);
463
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
464
+ return endpoint;
465
+ }
466
+ var DEFAULT_HOST, GATEWAY_KEYS_MISSING;
467
+ var init_platformEndpoint = __esm({
468
+ "src/worlds/platformEndpoint.ts"() {
469
+ "use strict";
470
+ DEFAULT_HOST = "https://withgateway.ai";
471
+ GATEWAY_KEYS_MISSING = "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).";
472
+ }
473
+ });
474
+
453
475
  // src/worlds/transport.ts
454
476
  function platformTimeoutMs(env = process.env) {
455
477
  const raw = env[PLATFORM_TIMEOUT_ENV];
@@ -458,7 +480,7 @@ function platformTimeoutMs(env = process.env) {
458
480
  if (Number.isInteger(value) && value > 0) return value;
459
481
  throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
460
482
  }
461
- var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, PlatformTransport;
483
+ var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, sleep2, RetryPolicy, Attempt, PlatformTransport;
462
484
  var init_transport = __esm({
463
485
  "src/worlds/transport.ts"() {
464
486
  "use strict";
@@ -520,6 +542,56 @@ var init_transport = __esm({
520
542
  route;
521
543
  failure;
522
544
  };
545
+ sleep2 = (ms) => new Promise((resolve11) => setTimeout(resolve11, ms));
546
+ RetryPolicy = class _RetryPolicy {
547
+ constructor(delaysMs, wait = sleep2, budgetMs = Number.POSITIVE_INFINITY, now = Date.now) {
548
+ this.delaysMs = delaysMs;
549
+ this.wait = wait;
550
+ this.budgetMs = budgetMs;
551
+ this.now = now;
552
+ }
553
+ delaysMs;
554
+ wait;
555
+ budgetMs;
556
+ now;
557
+ static NONE = new _RetryPolicy([]);
558
+ /** A platform restart or a short edge outage: no wait starts that would end past five minutes, so a step ends
559
+ * within the budget plus one request timeout. */
560
+ static FILING = new _RetryPolicy([250, 500, 1e3, 2e3, 4e3, 8e3, 16e3, 3e4], sleep2, 3e5);
561
+ static TRANSIENT_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
562
+ async run(attempt) {
563
+ const started = this.now();
564
+ for (let tries = 0; ; tries += 1) {
565
+ const outcome = await Attempt.of(attempt);
566
+ const delayMs = this.delaysMs[tries];
567
+ const spent = delayMs === void 0 || this.now() - started + delayMs >= this.budgetMs;
568
+ if (!outcome.transient || spent) return outcome.settle();
569
+ await this.wait(delayMs);
570
+ }
571
+ }
572
+ };
573
+ Attempt = class _Attempt {
574
+ constructor(reply, error, transient) {
575
+ this.reply = reply;
576
+ this.error = error;
577
+ this.transient = transient;
578
+ }
579
+ reply;
580
+ error;
581
+ transient;
582
+ static async of(attempt) {
583
+ try {
584
+ const reply = await attempt();
585
+ return new _Attempt(reply, null, RetryPolicy.TRANSIENT_STATUSES.has(reply.status));
586
+ } catch (error) {
587
+ return new _Attempt(null, error, error instanceof UnreachableError);
588
+ }
589
+ }
590
+ settle() {
591
+ if (this.reply) return this.reply;
592
+ throw this.error;
593
+ }
594
+ };
523
595
  PlatformTransport = class {
524
596
  constructor(endpoint, timeoutMs = platformTimeoutMs()) {
525
597
  this.endpoint = endpoint;
@@ -533,6 +605,9 @@ var init_transport = __esm({
533
605
  }
534
606
  async send(method, route, body, opts = {}) {
535
607
  const timeoutMs = this.budget(opts.timeoutMs);
608
+ return (opts.retry ?? RetryPolicy.NONE).run(() => this.once(method, route, body, timeoutMs));
609
+ }
610
+ async once(method, route, body, timeoutMs) {
536
611
  const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
537
612
  const controller = new AbortController();
538
613
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -726,9 +801,9 @@ function surfaceKinds(s) {
726
801
  if (s.browser) kinds.push("browser");
727
802
  return kinds;
728
803
  }
729
- function uiUrl(ui, path16) {
804
+ function uiUrl(ui, path17) {
730
805
  const base = ui.url.replace(/\/+$/, "");
731
- const rel = path16.startsWith("/") ? path16 : `/${path16}`;
806
+ const rel = path17.startsWith("/") ? path17 : `/${path17}`;
732
807
  if (!ui.token) return base + rel;
733
808
  const [beforeHash, hash] = splitHash(rel);
734
809
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -969,7 +1044,7 @@ var init_browser = __esm({
969
1044
  }
970
1045
  async send(batch) {
971
1046
  const retries = this.opts.maxRetries ?? EVENT_MAX_RETRIES;
972
- const sleep3 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1047
+ const sleep5 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
973
1048
  for (let attempt = 0; attempt < retries; attempt++) {
974
1049
  try {
975
1050
  const base = await this.resolveBase();
@@ -986,7 +1061,7 @@ var init_browser = __esm({
986
1061
  );
987
1062
  return;
988
1063
  }
989
- await sleep3(500 * 2 ** attempt);
1064
+ await sleep5(500 * 2 ** attempt);
990
1065
  }
991
1066
  }
992
1067
  }
@@ -1623,13 +1698,13 @@ var init_screen = __esm({
1623
1698
  });
1624
1699
  }
1625
1700
  async drag(action) {
1626
- const path16 = action.args.path ?? [];
1627
- if (path16.length < 2) throw new Error("a drag needs at least two points");
1701
+ const path17 = action.args.path ?? [];
1702
+ if (path17.length < 2) throw new Error("a drag needs at least two points");
1628
1703
  return this.held(action.args.modifiers ?? [], async () => {
1629
- await this.moveTo(path16[0]);
1704
+ await this.moveTo(path17[0]);
1630
1705
  await this.page.mouse.down({ button: "left" });
1631
1706
  try {
1632
- for (const point2 of path16.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1707
+ for (const point2 of path17.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1633
1708
  } finally {
1634
1709
  await this.page.mouse.up({ button: "left" });
1635
1710
  }
@@ -1720,7 +1795,7 @@ var init_media = __esm({
1720
1795
  put;
1721
1796
  maxBytes;
1722
1797
  /** The uploader for `GATEWAY_HOST` / `GATEWAY_PUBLIC_KEY` /
1723
- * `GATEWAY_SECRET_KEY` (the keys tracing exports with); null when any is unset. */
1798
+ * `GATEWAY_SECRET_KEY` only; null when any is unset, so a run traced with a saved login uploads none. */
1724
1799
  static fromEnv(env = process.env) {
1725
1800
  const host = env.GATEWAY_HOST;
1726
1801
  const publicKey = env.GATEWAY_PUBLIC_KEY;
@@ -2590,7 +2665,7 @@ function idleWarning(idleExpiresAt, now = /* @__PURE__ */ new Date()) {
2590
2665
  const minutes = Math.max(0, Math.floor(leftMs / 6e4));
2591
2666
  return `warning: session expires in ${minutes}m (idle TTL); call a tool to extend it or \`gateway worlds session pin\` to keep it`;
2592
2667
  }
2593
- var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge;
2668
+ var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge, stillStartingHint;
2594
2669
  var init_sessionContract = __esm({
2595
2670
  "src/worlds/sessionContract.ts"() {
2596
2671
  "use strict";
@@ -2626,6 +2701,65 @@ var init_sessionContract = __esm({
2626
2701
  };
2627
2702
  }
2628
2703
  };
2704
+ stillStartingHint = (sessionId) => `It keeps opening: poll \`gateway worlds session status ${sessionId}\` until ready, or end it with \`gateway worlds session close ${sessionId}\`.`;
2705
+ }
2706
+ });
2707
+
2708
+ // src/worlds/sessionReadiness.ts
2709
+ var READY_TIMEOUT_MS, READY_POLL_MS, sleep3, WorldSessionTimeout, WorldSessionCallError, WorldSessionOpenFailed, ENDED, SessionReadiness;
2710
+ var init_sessionReadiness = __esm({
2711
+ "src/worlds/sessionReadiness.ts"() {
2712
+ "use strict";
2713
+ READY_TIMEOUT_MS = 10 * 6e4;
2714
+ READY_POLL_MS = 2e3;
2715
+ sleep3 = (ms) => new Promise((r) => setTimeout(r, ms));
2716
+ WorldSessionTimeout = class extends Error {
2717
+ };
2718
+ WorldSessionCallError = class extends Error {
2719
+ };
2720
+ WorldSessionOpenFailed = class extends WorldSessionCallError {
2721
+ constructor(sessionId, state2, code, reason) {
2722
+ super(
2723
+ reason === null ? `World session ${sessionId} ${state2.toLowerCase()} before it was ready.` : `World session ${sessionId} failed to open: ${reason}${code ? ` (${code})` : ""}`
2724
+ );
2725
+ this.sessionId = sessionId;
2726
+ this.state = state2;
2727
+ this.code = code;
2728
+ this.name = "WorldSessionOpenFailed";
2729
+ }
2730
+ sessionId;
2731
+ state;
2732
+ code;
2733
+ };
2734
+ ENDED = /* @__PURE__ */ new Set(["FAILED", "STOPPED"]);
2735
+ SessionReadiness = class {
2736
+ constructor(sessionId, status) {
2737
+ this.sessionId = sessionId;
2738
+ this.status = status;
2739
+ }
2740
+ sessionId;
2741
+ status;
2742
+ async wait(timeoutMs = READY_TIMEOUT_MS) {
2743
+ const deadline = Date.now() + timeoutMs;
2744
+ for (; ; ) {
2745
+ const state2 = await this.status();
2746
+ if (state2.ready) return state2;
2747
+ if (ENDED.has(state2.state))
2748
+ throw new WorldSessionOpenFailed(
2749
+ this.sessionId,
2750
+ state2.state,
2751
+ state2.failure?.code ?? null,
2752
+ state2.failure?.message ?? null
2753
+ );
2754
+ const left = deadline - Date.now();
2755
+ if (left <= 0)
2756
+ throw new WorldSessionTimeout(
2757
+ `World session ${this.sessionId} still ${state2.state} after ${Math.round(timeoutMs / 1e3)}s.`
2758
+ );
2759
+ await sleep3(Math.min(READY_POLL_MS, left));
2760
+ }
2761
+ }
2762
+ };
2629
2763
  }
2630
2764
  });
2631
2765
 
@@ -2721,11 +2855,184 @@ var init_taskRef = __esm({
2721
2855
  }
2722
2856
  });
2723
2857
 
2858
+ // src/worlds/worldNotice.ts
2859
+ var TOOL_CONTRACT_UNREAD, DEPRECATION, NOTICE_KINDS, WorldNotice, WorldToolsUnread;
2860
+ var init_worldNotice = __esm({
2861
+ "src/worlds/worldNotice.ts"() {
2862
+ "use strict";
2863
+ TOOL_CONTRACT_UNREAD = "tool-contract-unread";
2864
+ DEPRECATION = { label: "deprecated", next: "Instead", refusesRun: false };
2865
+ NOTICE_KINDS = /* @__PURE__ */ new Map([
2866
+ [TOOL_CONTRACT_UNREAD, { label: "warning", next: "Then", refusesRun: true }]
2867
+ ]);
2868
+ WorldNotice = class _WorldNotice {
2869
+ constructor(notice, kind) {
2870
+ this.notice = notice;
2871
+ this.kind = kind;
2872
+ }
2873
+ notice;
2874
+ kind;
2875
+ static of(notice) {
2876
+ return new _WorldNotice(notice, NOTICE_KINDS.get(notice.code) ?? DEPRECATION);
2877
+ }
2878
+ get refusesRun() {
2879
+ return this.kind.refusesRun;
2880
+ }
2881
+ line() {
2882
+ const { code, message, replacement } = this.notice;
2883
+ return `${this.kind.label} (${code}): ${message} ${this.kind.next}: ${replacement}`;
2884
+ }
2885
+ };
2886
+ WorldToolsUnread = class _WorldToolsUnread extends Error {
2887
+ constructor(world, notice) {
2888
+ super(
2889
+ `refused before the agent ran, nothing filed: world ${world} has no readable tools: ${notice.notice.message} Then: ${notice.notice.replacement}`
2890
+ );
2891
+ this.world = world;
2892
+ this.notice = notice;
2893
+ this.name = "WorldToolsUnread";
2894
+ this.code = notice.notice.code;
2895
+ }
2896
+ world;
2897
+ notice;
2898
+ code;
2899
+ static refuse(world, notices) {
2900
+ const refusing = notices.map((notice) => WorldNotice.of(notice)).find((notice) => notice.refusesRun);
2901
+ if (refusing) throw new _WorldToolsUnread(world, refusing);
2902
+ }
2903
+ };
2904
+ }
2905
+ });
2906
+
2907
+ // src/worlds/grade.ts
2908
+ var messageOf, Score, GradeRefused, GradePayload;
2909
+ var init_grade = __esm({
2910
+ "src/worlds/grade.ts"() {
2911
+ "use strict";
2912
+ messageOf = (error) => (error instanceof Error ? error.message || error.name : String(error)) || "Error";
2913
+ Score = class _Score {
2914
+ static RULE = "a score is a number from 0 to 1";
2915
+ static admits(value) {
2916
+ return Number.isFinite(value) && value >= 0 && value <= 1;
2917
+ }
2918
+ /** Every number among `scores` that is not a score, as `'<key>' = <value>`
2919
+ * and the rule, or null when all of them are. Pairs, not a map, so that no
2920
+ * key hides another. */
2921
+ static problem(scores2) {
2922
+ const refused = [...scores2].flatMap(
2923
+ ([key2, value]) => typeof value === "number" && !_Score.admits(value) ? [`'${key2}' = ${String(value)}`] : []
2924
+ );
2925
+ return refused.length ? `${refused.join(", ")}; ${_Score.RULE}` : null;
2926
+ }
2927
+ };
2928
+ GradeRefused = class extends Error {
2929
+ name = "GradeRefused";
2930
+ };
2931
+ GradePayload = class _GradePayload {
2932
+ /** A grade payload's numbers, checked: every number in it must be a score,
2933
+ * and its rewards and verifiers objects by name, or the grade is refused; a
2934
+ * cross-world verifier's grade that breaks either rule is that verifier's
2935
+ * error, the way the World Host answers one that could not grade. */
2936
+ static read(raw) {
2937
+ if (!_GradePayload.objectOrAbsent(raw))
2938
+ throw new GradeRefused("the grade is not an object");
2939
+ const record = _GradePayload.record(raw);
2940
+ const problem = _GradePayload.problem(record) ?? _GradePayload.verifiersProblem(record.verifiers);
2941
+ if (problem) throw new GradeRefused(problem);
2942
+ const verifiers = _GradePayload.verifiers(record.verifiers);
2943
+ return {
2944
+ ..._GradePayload.numbers(record),
2945
+ ...typeof record.reason === "string" ? { reason: record.reason } : {},
2946
+ ...typeof record.error === "string" ? { error: record.error } : {},
2947
+ ...verifiers ? { verifiers } : {}
2948
+ };
2949
+ }
2950
+ /** A world whose grade call failed (the platform refused it, the host had
2951
+ * lost the session): that world's error, never a score. */
2952
+ static failedCall(error) {
2953
+ return { ..._GradePayload.failed(messageOf(error)), raw: null };
2954
+ }
2955
+ static problem(record) {
2956
+ if (!_GradePayload.objectOrAbsent(record.rewards))
2957
+ return "the grade's rewards are not an object";
2958
+ const scores2 = Score.problem(_GradePayload.scores(record));
2959
+ if (scores2) return `the grade answered ${scores2}`;
2960
+ const twice = _GradePayload.twice(record);
2961
+ return twice.length ? `the grade answered ${twice.map((key2) => `'${key2}'`).join(", ")} both beside and inside rewards` : null;
2962
+ }
2963
+ static twice(record) {
2964
+ return Object.entries(_GradePayload.record(record.rewards)).filter(
2965
+ ([key2, value]) => key2 !== "reward" && typeof value === "number" && typeof record[key2] === "number"
2966
+ ).map(([key2]) => key2);
2967
+ }
2968
+ static verifiersProblem(value) {
2969
+ return _GradePayload.objectOrAbsent(value) ? null : "the grade's verifiers are not an object";
2970
+ }
2971
+ static scores(record) {
2972
+ return [
2973
+ ["reward", record.reward],
2974
+ ...Object.entries(_GradePayload.record(record.rewards)),
2975
+ ...Object.entries(record).filter(
2976
+ ([key2]) => key2 !== "reward" && key2 !== "rewards"
2977
+ )
2978
+ ];
2979
+ }
2980
+ static isRecord(value) {
2981
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2982
+ }
2983
+ static objectOrAbsent(value) {
2984
+ return value === void 0 || value === null || _GradePayload.isRecord(value);
2985
+ }
2986
+ static record(value) {
2987
+ return _GradePayload.isRecord(value) ? value : {};
2988
+ }
2989
+ /** `reward`, and every numeric key under `rewards` (the host's shape) or at
2990
+ * the top level (a bundled verifier's). */
2991
+ static numbers(record) {
2992
+ const rewards = {};
2993
+ for (const [key2, value] of Object.entries(
2994
+ _GradePayload.record(record.rewards)
2995
+ ))
2996
+ if (typeof value === "number") rewards[key2] = value;
2997
+ for (const [key2, value] of Object.entries(record))
2998
+ if (key2 !== "reward" && typeof value === "number") rewards[key2] = value;
2999
+ return {
3000
+ reward: typeof record.reward === "number" ? record.reward : null,
3001
+ rewards
3002
+ };
3003
+ }
3004
+ static verifiers(value) {
3005
+ if (!_GradePayload.isRecord(value)) return void 0;
3006
+ return Object.fromEntries(
3007
+ Object.entries(value).map(([name, grade]) => [
3008
+ name,
3009
+ _GradePayload.verifier(name, grade)
3010
+ ])
3011
+ );
3012
+ }
3013
+ static verifier(name, grade) {
3014
+ if (!_GradePayload.isRecord(grade))
3015
+ return _GradePayload.failed(`the grade's verifier '${name}' is not an object`);
3016
+ const problem = _GradePayload.problem(grade);
3017
+ if (problem) return _GradePayload.failed(problem);
3018
+ return {
3019
+ ..._GradePayload.numbers(grade),
3020
+ ...typeof grade.error === "string" ? { error: grade.error } : {}
3021
+ };
3022
+ }
3023
+ static failed(error) {
3024
+ return { reward: null, rewards: {}, error };
3025
+ }
3026
+ };
3027
+ }
3028
+ });
3029
+
2724
3030
  // src/worlds/session.ts
2725
3031
  var session_exports2 = {};
2726
3032
  __export(session_exports2, {
2727
3033
  WorldSession: () => WorldSession,
2728
3034
  WorldSessionCallError: () => WorldSessionCallError,
3035
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
2729
3036
  WorldSessionTimeout: () => WorldSessionTimeout,
2730
3037
  attachSession: () => attachSession,
2731
3038
  isTaskIdRef: () => isTaskIdRef,
@@ -2737,34 +3044,6 @@ __export(session_exports2, {
2737
3044
  taskLabel: () => taskLabel
2738
3045
  });
2739
3046
  import { randomUUID } from "crypto";
2740
- function numericGrade(record) {
2741
- const rewards = {};
2742
- const nested = record.rewards;
2743
- if (nested && typeof nested === "object") {
2744
- for (const [key2, value] of Object.entries(nested)) {
2745
- if (typeof value === "number") rewards[key2] = value;
2746
- }
2747
- }
2748
- for (const [key2, value] of Object.entries(record)) {
2749
- if (key2 !== "reward" && typeof value === "number") rewards[key2] = value;
2750
- }
2751
- return {
2752
- reward: typeof record.reward === "number" ? record.reward : null,
2753
- rewards
2754
- };
2755
- }
2756
- function verifierGrades(value) {
2757
- if (!value || typeof value !== "object") return void 0;
2758
- const out = {};
2759
- for (const [name, grade] of Object.entries(value)) {
2760
- const record = grade && typeof grade === "object" ? grade : {};
2761
- out[name] = {
2762
- ...numericGrade(record),
2763
- ...typeof record.error === "string" ? { error: record.error } : {}
2764
- };
2765
- }
2766
- return out;
2767
- }
2768
3047
  function schemaFromSignature(spec) {
2769
3048
  if (spec.inputSchema)
2770
3049
  return {
@@ -2872,6 +3151,7 @@ async function runSessions(opts) {
2872
3151
  const taskRef = tasks[next++];
2873
3152
  let task = taskLabel(taskRef);
2874
3153
  let session = null;
3154
+ let keepWarm = opts.keepWarm ?? true;
2875
3155
  try {
2876
3156
  session = await openSession(opts.world, {
2877
3157
  task: taskRef,
@@ -2879,6 +3159,7 @@ async function runSessions(opts) {
2879
3159
  options
2880
3160
  });
2881
3161
  task = taskLabel(taskRef, session.taskInfo);
3162
+ WorldToolsUnread.refuse(opts.world.split("@", 1)[0], session.deprecations);
2882
3163
  await session.ready();
2883
3164
  const toolkit = await session.toolkit();
2884
3165
  const live = session;
@@ -2901,19 +3182,19 @@ async function runSessions(opts) {
2901
3182
  results.push(result);
2902
3183
  opts.onResult?.(result);
2903
3184
  } catch (error) {
3185
+ if (error instanceof WorldToolsUnread) keepWarm = false;
2904
3186
  const result = {
2905
3187
  task,
2906
3188
  sessionId: session?.sessionId ?? "",
2907
3189
  reward: 0,
2908
3190
  rewards: {},
2909
- // An Error with an empty message would read as "no error" to any
2910
- // caller checking truthiness; name the type instead.
3191
+ // An empty message reads as "no error" to a truthiness check; name the type instead.
2911
3192
  error: (error instanceof Error ? error.message : String(error)) || (error instanceof Error ? error.name : "Unknown error")
2912
3193
  };
2913
3194
  results.push(result);
2914
3195
  opts.onResult?.(result);
2915
3196
  } finally {
2916
- await session?.close({ keepWarm: opts.keepWarm ?? true }).catch(() => void 0);
3197
+ await session?.close({ keepWarm }).catch(() => void 0);
2917
3198
  }
2918
3199
  }
2919
3200
  })
@@ -2927,7 +3208,7 @@ async function runSessions(opts) {
2927
3208
  passed: failed === 0 && (opts.failUnder === void 0 || mean >= opts.failUnder)
2928
3209
  };
2929
3210
  }
2930
- var 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;
3211
+ var CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep4, modelToolName, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
2931
3212
  var init_session2 = __esm({
2932
3213
  "src/worlds/session.ts"() {
2933
3214
  "use strict";
@@ -2935,22 +3216,20 @@ var init_session2 = __esm({
2935
3216
  init_browser();
2936
3217
  init_nativeComputer();
2937
3218
  init_sessionContract();
3219
+ init_sessionReadiness();
2938
3220
  init_session();
2939
3221
  init_taskRef();
3222
+ init_worldNotice();
3223
+ init_grade();
2940
3224
  init_taskRef();
2941
- READY_TIMEOUT_MS = 10 * 6e4;
2942
- READY_POLL_MS = 2e3;
3225
+ init_sessionReadiness();
2943
3226
  CALL_TIMEOUT_MS = 3 * 6e4;
2944
3227
  CALL_POLL_MS = 400;
2945
3228
  JUDGED_GRADE_TIMEOUT_MS = 15 * 6e4;
2946
3229
  JUDGED_GRADE_POLL_MS = 2e3;
2947
3230
  EXPORT_PAGE = 500;
2948
- sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
3231
+ sleep4 = (ms) => new Promise((r) => setTimeout(r, ms));
2949
3232
  modelToolName = (name) => name.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64) || "tool";
2950
- WorldSessionTimeout = class extends Error {
2951
- };
2952
- WorldSessionCallError = class extends Error {
2953
- };
2954
3233
  WorldSession = class {
2955
3234
  constructor(sessionId, current, options) {
2956
3235
  this.sessionId = sessionId;
@@ -3068,18 +3347,17 @@ var init_session2 = __esm({
3068
3347
  }
3069
3348
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
3070
3349
  browserRecorder(agentId) {
3071
- const path16 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
3350
+ const path17 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
3072
3351
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
3073
3352
  return new BrowserActionRecorder(
3074
3353
  async (events) => {
3075
- await apiRequest(this.options, "POST", path16, { events });
3354
+ await apiRequest(this.options, "POST", path17, { events });
3076
3355
  },
3077
3356
  id,
3078
3357
  {
3079
- // Continue this agent's timeline where the platform has it, so the
3080
- // same id reopened later never re-numbers from zero.
3358
+ // Continue this agent's timeline where the platform has it, so a reopened id never re-numbers from zero.
3081
3359
  resumeFrom: async () => {
3082
- const summary = await apiRequest(this.options, "GET", path16);
3360
+ const summary = await apiRequest(this.options, "GET", path17);
3083
3361
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
3084
3362
  }
3085
3363
  }
@@ -3107,29 +3385,22 @@ var init_session2 = __esm({
3107
3385
  tools: this.current.tools,
3108
3386
  surfaces: this.current.surfaces,
3109
3387
  task: this.current.task,
3388
+ deprecations: this.current.deprecations,
3110
3389
  ...fresh
3111
3390
  };
3112
3391
  return this.current;
3113
3392
  }
3114
3393
  /**
3115
- * Wait until the container is warm — meaning the process inside it has
3116
- * asked the platform for work, not merely that a job was accepted.
3394
+ * Wait until the session answers calls: a World Host holds it, or the
3395
+ * process inside a pod has asked the platform for work. Throws
3396
+ * `WorldSessionOpenFailed` (with the platform's reason) when it ended
3397
+ * first, and `WorldSessionTimeout` when `timeoutMs` (default ten minutes)
3398
+ * ran out while it was still starting.
3117
3399
  */
3118
3400
  async ready(opts = {}) {
3119
- const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
3120
- for (; ; ) {
3121
- const state2 = await this.status();
3122
- if (state2.ready) return state2;
3123
- if (state2.state === "FAILED" || state2.state === "STOPPED")
3124
- throw new WorldSessionCallError(
3125
- `World session ${state2.state.toLowerCase()} before it was ready.`
3126
- );
3127
- if (Date.now() > deadline)
3128
- throw new WorldSessionTimeout(
3129
- `World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
3130
- );
3131
- await sleep2(READY_POLL_MS);
3132
- }
3401
+ return new SessionReadiness(this.sessionId, () => this.status()).wait(
3402
+ opts.timeoutMs
3403
+ );
3133
3404
  }
3134
3405
  /** Enqueue one interaction and wait for the container's answer. */
3135
3406
  async exchange(body, timeoutMs) {
@@ -3159,7 +3430,7 @@ var init_session2 = __esm({
3159
3430
  throw new WorldSessionTimeout(
3160
3431
  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.`
3161
3432
  );
3162
- await sleep2(pollMs2);
3433
+ await sleep4(pollMs2);
3163
3434
  }
3164
3435
  }
3165
3436
  /**
@@ -3208,7 +3479,9 @@ var init_session2 = __esm({
3208
3479
  * calls); assertions and python graders ignore it. `worlds` names the
3209
3480
  * task's other sessions by alias (`{alias: sessionId}`): the platform
3210
3481
  * reads each one's evidence and hands it to the cross-world verifiers
3211
- * anchored on this session, whose grades come back under `verifiers`. */
3482
+ * anchored on this session, whose grades come back under `verifiers`. A
3483
+ * score that is not a number from 0 to 1 is refused by key and value
3484
+ * (`GradeRefused`); a verifier's is that verifier's `error`. */
3212
3485
  async grade(opts = {}) {
3213
3486
  const raw = await this.exchange(
3214
3487
  {
@@ -3218,14 +3491,7 @@ var init_session2 = __esm({
3218
3491
  },
3219
3492
  opts.timeoutMs
3220
3493
  );
3221
- const record = raw && typeof raw === "object" ? raw : {};
3222
- const verifiers = verifierGrades(record.verifiers);
3223
- return {
3224
- ...numericGrade(record),
3225
- ...typeof record.reason === "string" ? { reason: record.reason } : {},
3226
- ...verifiers ? { verifiers } : {},
3227
- raw
3228
- };
3494
+ return { ...GradePayload.read(raw), raw };
3229
3495
  }
3230
3496
  /**
3231
3497
  * Put rows into the LIVE world through its contract, in one batch:
@@ -3465,28 +3731,52 @@ var init_tasks = __esm({
3465
3731
  }
3466
3732
  });
3467
3733
 
3734
+ // src/worlds/surfaceDescriptor.ts
3735
+ function surfaceDescriptor(s) {
3736
+ return {
3737
+ surfaces: surfaceKinds(s),
3738
+ ...s.api ? { api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} } } : {},
3739
+ ...s.ui ? { ui: { url: s.ui.url, entry: uiEntryUrl(s.ui), apiPath: s.ui.apiPath ?? null } } : {},
3740
+ ...s.browser ? {
3741
+ browser: {
3742
+ wsUrl: s.browser.wsUrl,
3743
+ viewport: s.browser.viewport,
3744
+ ...s.browser.tier ? { tier: s.browser.tier } : {}
3745
+ }
3746
+ } : {}
3747
+ };
3748
+ }
3749
+ var init_surfaceDescriptor = __esm({
3750
+ "src/worlds/surfaceDescriptor.ts"() {
3751
+ "use strict";
3752
+ init_browser();
3753
+ }
3754
+ });
3755
+
3468
3756
  // src/worlds/taskSessions.ts
3469
3757
  function aggregateTaskGrade(worlds) {
3470
- const rewards = {};
3471
- const ungraded = [];
3472
- const graded = [];
3758
+ const fold = new TaskGradeFold();
3473
3759
  const verifiers = {};
3474
- const fold = (key2, grade) => {
3475
- if (typeof grade.reward === "number") {
3476
- rewards[key2] = grade.reward;
3477
- graded.push(grade.reward);
3478
- } else ungraded.push(key2);
3479
- for (const [name, value] of Object.entries(grade.rewards))
3480
- rewards[`${key2}.${name}`] = value;
3481
- };
3482
3760
  for (const [alias, grade] of Object.entries(worlds)) {
3483
- fold(alias, grade);
3761
+ fold.add(alias, grade, `world ${alias}`);
3484
3762
  Object.assign(verifiers, grade.verifiers ?? {});
3485
3763
  }
3486
3764
  for (const [name, grade] of Object.entries(verifiers))
3487
- fold(`${VERIFIER_REWARD_PREFIX}${name}`, grade);
3488
- const reward = graded.length > 0 ? graded.reduce((a, b) => a + b, 0) / graded.length : null;
3489
- return { reward, rewards, worlds, ungraded, verifiers };
3765
+ fold.add(`${VERIFIER_REWARD_PREFIX}${name}`, grade, `verifier ${name}`);
3766
+ return {
3767
+ reward: fold.reward(),
3768
+ rewards: fold.rewards,
3769
+ worlds,
3770
+ ungraded: fold.ungraded,
3771
+ verifiers,
3772
+ errors: fold.errors
3773
+ };
3774
+ }
3775
+ function taskGradeError(grade) {
3776
+ const lines = Object.entries(grade.errors).map(
3777
+ ([key2, why]) => `${key2}: ${why}`
3778
+ );
3779
+ return lines.length ? lines.join("; ") : null;
3490
3780
  }
3491
3781
  function fromResponse(answer, options) {
3492
3782
  const sessions2 = {};
@@ -3609,12 +3899,14 @@ async function attachManifest(manifest, options = {}) {
3609
3899
  function isTaskManifest(value) {
3610
3900
  return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.sessions === "object" && value.sessions !== null && !("instruction" in value && "worlds" in value);
3611
3901
  }
3612
- var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskSessions;
3902
+ var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskGradeFold, TaskSessions;
3613
3903
  var init_taskSessions = __esm({
3614
3904
  "src/worlds/taskSessions.ts"() {
3615
3905
  "use strict";
3616
3906
  init_worlds();
3617
3907
  init_browser();
3908
+ init_grade();
3909
+ init_surfaceDescriptor();
3618
3910
  init_session2();
3619
3911
  TASK_SESSIONS = "/api/public/world-task-sessions";
3620
3912
  VERIFIER_REWARD_PREFIX = "x.";
@@ -3626,19 +3918,38 @@ var init_taskSessions = __esm({
3626
3918
  sessionId: state2.sessionId,
3627
3919
  ready: state2.ready,
3628
3920
  state: state2.state,
3629
- surfaces: surfaceKinds(s),
3630
- ...s.api ? {
3631
- api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} }
3632
- } : {},
3633
- ...s.ui ? {
3634
- ui: {
3635
- url: s.ui.url,
3636
- entry: uiEntryUrl(s.ui),
3637
- apiPath: s.ui.apiPath ?? null
3638
- }
3639
- } : {},
3640
- ...s.browser ? { browser: { wsUrl: s.browser.wsUrl, viewport: s.browser.viewport } } : {}
3921
+ ...surfaceDescriptor(s)
3641
3922
  });
3923
+ TaskGradeFold = class {
3924
+ rewards = {};
3925
+ ungraded = [];
3926
+ errors = {};
3927
+ sources = /* @__PURE__ */ new Map();
3928
+ graded = [];
3929
+ add(key2, grade, source) {
3930
+ if (grade.error) this.errors[key2] = grade.error;
3931
+ if (typeof grade.reward === "number") {
3932
+ this.put(key2, grade.reward, source);
3933
+ this.graded.push(grade.reward);
3934
+ } else this.ungraded.push(key2);
3935
+ for (const [name, value] of Object.entries(grade.rewards))
3936
+ this.put(`${key2}.${name}`, value, source);
3937
+ }
3938
+ reward() {
3939
+ if (Object.keys(this.errors).length > 0 || !this.graded.length)
3940
+ return null;
3941
+ return this.graded.reduce((a, b) => a + b, 0) / this.graded.length;
3942
+ }
3943
+ put(key2, value, source) {
3944
+ const first = this.sources.get(key2) ?? source;
3945
+ this.sources.set(key2, first);
3946
+ if (first !== source) {
3947
+ this.errors[key2] = `two rewards are named ${key2} (${first} and ${source}); rename one of them`;
3948
+ return;
3949
+ }
3950
+ this.rewards[key2] = value;
3951
+ }
3952
+ };
3642
3953
  TaskSessions = class {
3643
3954
  constructor(taskId, taskVersion, name, instruction, sessions2, worlds, hash = null) {
3644
3955
  this.taskId = taskId;
@@ -3717,11 +4028,13 @@ var init_taskSessions = __esm({
3717
4028
  * agent's own account of the whole investigation) goes to every world's
3718
4029
  * judge; a world graded by assertions or python ignores it. Each grade
3719
4030
  * names the task's other sessions by alias, so the cross-world verifiers
3720
- * anchored on that world can read them. */
4031
+ * anchored on that world can read them. A world whose grade call fails is
4032
+ * that world's error: the task's grade names it and keeps every other
4033
+ * world's grade. */
3721
4034
  async grade(opts = {}) {
3722
4035
  return aggregateTaskGrade(
3723
4036
  await this.each(
3724
- (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) })
4037
+ (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) }).catch((error) => GradePayload.failedCall(error))
3725
4038
  )
3726
4039
  );
3727
4040
  }
@@ -3769,12 +4082,16 @@ var init_evals = __esm({
3769
4082
  "src/worlds/evals.ts"() {
3770
4083
  "use strict";
3771
4084
  init_worlds();
4085
+ init_transport();
3772
4086
  EVALUATIONS = "/api/public/benchmark-containers/evaluations";
3773
4087
  GatewayEvalsClient = class {
3774
- constructor(options = {}) {
4088
+ constructor(options = {}, retry = RetryPolicy.FILING) {
3775
4089
  this.options = options;
4090
+ this.retry = retry;
3776
4091
  }
3777
4092
  options;
4093
+ retry;
4094
+ keyed = /* @__PURE__ */ new Set();
3778
4095
  async createEvaluation(versionId, opts = {}) {
3779
4096
  const body = { versionId };
3780
4097
  if (opts.model !== void 0) body.model = opts.model;
@@ -3782,12 +4099,18 @@ var init_evals = __esm({
3782
4099
  if (opts.rolloutsPerExample !== void 0)
3783
4100
  body.rolloutsPerExample = opts.rolloutsPerExample;
3784
4101
  if (opts.metadata !== void 0) body.metadata = opts.metadata;
4102
+ if (opts.idempotencyKey !== void 0)
4103
+ body.idempotencyKey = opts.idempotencyKey;
3785
4104
  const answer = await apiRequest(
3786
4105
  this.options,
3787
4106
  "POST",
3788
4107
  EVALUATIONS,
3789
- body
4108
+ body,
4109
+ {
4110
+ retry: opts.idempotencyKey === void 0 ? RetryPolicy.NONE : this.retry
4111
+ }
3790
4112
  );
4113
+ if (opts.idempotencyKey !== void 0) this.keyed.add(answer.evaluationId);
3791
4114
  return answer.evaluationId;
3792
4115
  }
3793
4116
  async pushSamples(evaluationId, samples) {
@@ -3795,7 +4118,8 @@ var init_evals = __esm({
3795
4118
  this.options,
3796
4119
  "POST",
3797
4120
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/samples`,
3798
- { samples }
4121
+ { samples },
4122
+ { retry: this.retryFor(evaluationId) }
3799
4123
  );
3800
4124
  return answer.inserted ?? samples.length;
3801
4125
  }
@@ -3808,10 +4132,347 @@ var init_evals = __esm({
3808
4132
  this.options,
3809
4133
  "POST",
3810
4134
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/finalize`,
3811
- body
4135
+ body,
4136
+ { retry: this.retryFor(evaluationId) }
3812
4137
  );
3813
4138
  return answer.status;
3814
4139
  }
4140
+ retryFor(evaluationId) {
4141
+ return this.keyed.has(evaluationId) ? this.retry : RetryPolicy.NONE;
4142
+ }
4143
+ };
4144
+ }
4145
+ });
4146
+
4147
+ // src/worlds/runSignals.ts
4148
+ import { constants } from "os";
4149
+ var OWNER, RunSignals;
4150
+ var init_runSignals = __esm({
4151
+ "src/worlds/runSignals.ts"() {
4152
+ "use strict";
4153
+ OWNER = /* @__PURE__ */ Symbol("RunSignals");
4154
+ RunSignals = class _RunSignals {
4155
+ constructor(target = process) {
4156
+ this.target = target;
4157
+ }
4158
+ target;
4159
+ static SIGNALS = ["SIGINT", "SIGTERM"];
4160
+ cleanups = [];
4161
+ heardSignal = null;
4162
+ heardCount = 0;
4163
+ listening = false;
4164
+ left = false;
4165
+ listener = Object.assign(
4166
+ (signal) => this.heard(signal),
4167
+ { [OWNER]: this }
4168
+ );
4169
+ /** The signal that arrived, once one has: start nothing new. */
4170
+ get signal() {
4171
+ return this.owner().heardSignal;
4172
+ }
4173
+ async during(cleanup, work) {
4174
+ return this.owner().hold(cleanup, work);
4175
+ }
4176
+ owner() {
4177
+ const owners = this.target.listeners("SIGINT").map((listener) => listener[OWNER]);
4178
+ return owners.find((owner) => owner !== void 0) ?? this;
4179
+ }
4180
+ async hold(cleanup, work) {
4181
+ this.listen();
4182
+ this.cleanups.push(cleanup);
4183
+ try {
4184
+ return await work();
4185
+ } finally {
4186
+ this.cleanups.splice(this.cleanups.indexOf(cleanup), 1);
4187
+ if (!this.cleanups.length && this.heardSignal === null) this.unlisten();
4188
+ }
4189
+ }
4190
+ heard(signal) {
4191
+ this.heardCount += 1;
4192
+ if (this.heardCount > 2) return this.leave(signal);
4193
+ this.heardSignal ??= signal;
4194
+ void this.cleanUp(signal, this.heardCount === 2).finally(() => this.leave(signal));
4195
+ }
4196
+ async cleanUp(signal, urgent) {
4197
+ for (const cleanup of [...this.cleanups].reverse())
4198
+ await Promise.resolve().then(() => cleanup(signal, urgent)).catch(() => void 0);
4199
+ }
4200
+ leave(signal) {
4201
+ if (this.left) return;
4202
+ this.left = true;
4203
+ this.unlisten();
4204
+ if (this.target.listenerCount(signal) === 0)
4205
+ this.target.exit(128 + constants.signals[signal]);
4206
+ }
4207
+ listen() {
4208
+ if (this.listening) return;
4209
+ this.listening = true;
4210
+ for (const signal of _RunSignals.SIGNALS)
4211
+ this.target.on(signal, this.listener);
4212
+ }
4213
+ unlisten() {
4214
+ this.listening = false;
4215
+ for (const signal of _RunSignals.SIGNALS)
4216
+ this.target.off(signal, this.listener);
4217
+ }
4218
+ };
4219
+ }
4220
+ });
4221
+
4222
+ // src/worlds/taskRunDir.ts
4223
+ import * as fs from "fs";
4224
+ import * as path from "path";
4225
+ function sessionRecord({ api: api2, ui, browser, ...session }) {
4226
+ return {
4227
+ ...session,
4228
+ ...api2 ? { api: { url: api2.url } } : {},
4229
+ ...ui ? { ui: { ...ui, entry: withoutUiToken(ui.entry) } } : {},
4230
+ ...browser ? { browser: browserRecord(browser) } : {}
4231
+ };
4232
+ }
4233
+ var TaskRunDir, browserRecord;
4234
+ var init_taskRunDir = __esm({
4235
+ "src/worlds/taskRunDir.ts"() {
4236
+ "use strict";
4237
+ init_browser();
4238
+ TaskRunDir = class {
4239
+ constructor(path17) {
4240
+ this.path = path17;
4241
+ }
4242
+ path;
4243
+ writeManifest(manifest) {
4244
+ this.writeJson("manifest.json", {
4245
+ ...manifest,
4246
+ sessions: Object.fromEntries(
4247
+ Object.entries(manifest.sessions).map(([alias, session]) => [
4248
+ alias,
4249
+ sessionRecord(session)
4250
+ ])
4251
+ )
4252
+ });
4253
+ }
4254
+ writeJson(name, value) {
4255
+ this.write(name, JSON.stringify(value, null, 2));
4256
+ }
4257
+ write(name, text2) {
4258
+ fs.mkdirSync(this.path, { recursive: true });
4259
+ fs.writeFileSync(path.join(this.path, name), text2);
4260
+ }
4261
+ };
4262
+ browserRecord = ({ viewport, tier }) => ({
4263
+ viewport,
4264
+ ...tier ? { tier } : {}
4265
+ });
4266
+ }
4267
+ });
4268
+
4269
+ // src/worlds/taskRunOutcome.ts
4270
+ var TaskRunOutcome;
4271
+ var init_taskRunOutcome = __esm({
4272
+ "src/worlds/taskRunOutcome.ts"() {
4273
+ "use strict";
4274
+ init_grade();
4275
+ init_taskSessions();
4276
+ TaskRunOutcome = class {
4277
+ constructor(runId, live, model, runDir) {
4278
+ this.runId = runId;
4279
+ this.live = live;
4280
+ this.model = model;
4281
+ this.runDir = runDir;
4282
+ }
4283
+ runId;
4284
+ live;
4285
+ model;
4286
+ runDir;
4287
+ report = null;
4288
+ extra = {};
4289
+ notes = {};
4290
+ agentError = null;
4291
+ grade = null;
4292
+ traced = false;
4293
+ runError = null;
4294
+ fileError = null;
4295
+ evaluationId;
4296
+ evaluationStatus = null;
4297
+ started = Date.now();
4298
+ /** The run stopped short of a grade; the first reason stands. */
4299
+ stopped(error) {
4300
+ this.runError ??= messageOf(error).slice(0, 2e3);
4301
+ }
4302
+ agentRaised(error) {
4303
+ this.agentError = messageOf(error).slice(0, 2e3);
4304
+ }
4305
+ /** Every reason the run failed, on one line: the evaluation's
4306
+ * `metrics.error`. A run that never reached a grade has failed whatever
4307
+ * its reasons say. */
4308
+ failure() {
4309
+ const reasons = [
4310
+ this.agentError === null ? null : `agent raised: ${this.agentError}`,
4311
+ this.runError === null ? null : `the run stopped: ${this.runError}`,
4312
+ this.gradeError(),
4313
+ this.grade === null && this.runError === null ? "the run stopped before it was graded" : null
4314
+ ].filter((reason) => reason !== null);
4315
+ return reasons.length ? reasons.join("; ") : null;
4316
+ }
4317
+ async file(evals, setRunId) {
4318
+ let created;
4319
+ try {
4320
+ created = await evals.createEvaluation(this.ref(this.live.aliases[0]).versionId, {
4321
+ model: this.model,
4322
+ rolloutsPerExample: 1,
4323
+ idempotencyKey: this.runId,
4324
+ ...setRunId ? { runId: setRunId } : {},
4325
+ metadata: this.metadata()
4326
+ });
4327
+ await evals.pushSamples(created, [this.sample()]);
4328
+ this.evaluationStatus = await evals.finalizeEvaluation(created, this.finalized());
4329
+ this.evaluationId = created;
4330
+ } catch (error) {
4331
+ this.fileError = created ? `evaluation ${created}: ${messageOf(error)}` : messageOf(error);
4332
+ if (created)
4333
+ await evals.finalizeEvaluation(created, { status: "FAILED", metrics: { error: this.fileError } }).catch(() => void 0);
4334
+ }
4335
+ }
4336
+ summary() {
4337
+ const live = this.live;
4338
+ const { evaluationId: _agentEvaluationId, ...extra } = this.extra;
4339
+ return {
4340
+ ...extra,
4341
+ runId: this.runId,
4342
+ taskId: live.taskId,
4343
+ taskVersion: live.taskVersion,
4344
+ name: live.name,
4345
+ instruction: live.instruction ?? "",
4346
+ model: this.model,
4347
+ aliases: live.aliases,
4348
+ reward: this.grade?.reward ?? null,
4349
+ rewards: this.grade?.rewards ?? {},
4350
+ ungraded: this.grade?.ungraded ?? live.aliases,
4351
+ agentError: this.agentError,
4352
+ runError: this.runError,
4353
+ gradeError: this.gradeError(),
4354
+ fileError: this.fileError,
4355
+ evaluationStatus: this.evaluationStatus,
4356
+ report: this.report,
4357
+ traced: this.traced,
4358
+ runDir: this.runDir,
4359
+ durationSeconds: Math.round((Date.now() - this.started) / 10) / 100,
4360
+ notes: this.notes,
4361
+ filed: this.evaluationId !== void 0,
4362
+ ...this.evaluationId ? { evaluationId: this.evaluationId } : {}
4363
+ };
4364
+ }
4365
+ gradeError() {
4366
+ return this.grade ? taskGradeError(this.grade) : null;
4367
+ }
4368
+ ref(alias) {
4369
+ const { slug, ref } = this.live.worldRef(alias);
4370
+ const session = this.live.sessions[alias];
4371
+ return { slug, ref, versionId: session.versionId, sessionId: session.sessionId };
4372
+ }
4373
+ metadata() {
4374
+ const live = this.live;
4375
+ return {
4376
+ source: "worlds-task-run",
4377
+ task: { id: live.taskId, version: live.taskVersion, name: live.name, hash: live.hash },
4378
+ anchor: live.aliases[0],
4379
+ worlds: Object.fromEntries(live.aliases.map((alias) => [alias, this.ref(alias)])),
4380
+ runId: this.runId
4381
+ };
4382
+ }
4383
+ sample() {
4384
+ const live = this.live;
4385
+ const reward = this.grade?.reward ?? null;
4386
+ const gradeError = this.gradeError();
4387
+ const sample = {
4388
+ exampleId: live.name ?? live.taskId ?? this.runId,
4389
+ task: live.name ?? live.taskId ?? this.runId,
4390
+ rolloutIndex: 0,
4391
+ prompt: live.instruction,
4392
+ ...this.report === null ? {} : { completion: this.report },
4393
+ rewards: this.grade?.rewards ?? {},
4394
+ info: {
4395
+ sessionId: this.runId,
4396
+ worlds: Object.fromEntries(
4397
+ live.aliases.map((alias) => [
4398
+ alias,
4399
+ { ...this.ref(alias), reward: this.grade?.worlds[alias]?.reward ?? null }
4400
+ ])
4401
+ ),
4402
+ ungraded: this.grade?.ungraded ?? live.aliases,
4403
+ traced: this.traced,
4404
+ runDir: this.runDir,
4405
+ ...this.agentError ? { agentError: this.agentError } : {},
4406
+ ...this.runError ? { runError: this.runError } : {},
4407
+ ...gradeError ? { verifyError: gradeError } : {}
4408
+ }
4409
+ };
4410
+ if (typeof reward === "number")
4411
+ Object.assign(sample, { reward, score: reward, correct: reward >= 1 });
4412
+ return sample;
4413
+ }
4414
+ finalized() {
4415
+ const reward = this.grade?.reward ?? null;
4416
+ const failure = this.failure();
4417
+ return {
4418
+ status: failure ? "FAILED" : "COMPLETED",
4419
+ metrics: {
4420
+ ...typeof reward === "number" ? { avg_reward: reward } : {},
4421
+ reward,
4422
+ ...Object.fromEntries(
4423
+ Object.entries(this.grade?.rewards ?? {}).map(([key2, value]) => [`rewards.${key2}`, value])
4424
+ ),
4425
+ ...failure ? { error: failure } : {}
4426
+ }
4427
+ };
4428
+ }
4429
+ };
4430
+ }
4431
+ });
4432
+
4433
+ // src/tracing/prerequisites.ts
4434
+ function getEnv(key2) {
4435
+ const value = process.env[key2];
4436
+ return value ? value : void 0;
4437
+ }
4438
+ function isGatewayConfigured() {
4439
+ return !!(getEnv(GATEWAY_ENV.HOST) && getEnv(GATEWAY_ENV.PUBLIC_KEY) && getEnv(GATEWAY_ENV.SECRET_KEY));
4440
+ }
4441
+ var GATEWAY_ENV, OTLP_TRACES_PATH, OPENTELEMETRY_PEERS, PINNED_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
4442
+ var init_prerequisites = __esm({
4443
+ "src/tracing/prerequisites.ts"() {
4444
+ "use strict";
4445
+ GATEWAY_ENV = {
4446
+ HOST: "GATEWAY_HOST",
4447
+ PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
4448
+ SECRET_KEY: "GATEWAY_SECRET_KEY",
4449
+ API_KEY: "GATEWAY_API_KEY",
4450
+ ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
4451
+ };
4452
+ OTLP_TRACES_PATH = "/api/public/otel/v1/traces";
4453
+ OPENTELEMETRY_PEERS = {
4454
+ "@opentelemetry/api": "1.9.0",
4455
+ "@opentelemetry/exporter-trace-otlp-http": "0.222.0",
4456
+ "@opentelemetry/resources": "2.11.0",
4457
+ "@opentelemetry/sdk-trace-base": "2.11.0",
4458
+ "@opentelemetry/sdk-trace-node": "2.11.0"
4459
+ };
4460
+ PINNED_PEERS = Object.entries(OPENTELEMETRY_PEERS).map(([name, version]) => `${name}@${version}`).join(" ");
4461
+ MODULE_NOT_FOUND = /* @__PURE__ */ new Set(["MODULE_NOT_FOUND", "ERR_MODULE_NOT_FOUND"]);
4462
+ OpenTelemetryMissing = class _OpenTelemetryMissing extends Error {
4463
+ constructor() {
4464
+ super(
4465
+ `OpenTelemetry packages not found. Install them next to @withgateway/sdk, at the versions it is tested with:
4466
+ npm install ${PINNED_PEERS}
4467
+ npm install -g ${PINNED_PEERS} (the gateway CLI, when it was installed with npm install -g)`
4468
+ );
4469
+ this.name = "OpenTelemetryMissing";
4470
+ }
4471
+ static explains(error) {
4472
+ if (error instanceof _OpenTelemetryMissing) return true;
4473
+ const { code, message } = error ?? {};
4474
+ return MODULE_NOT_FOUND.has(String(code)) && String(message).includes("@opentelemetry/");
4475
+ }
3815
4476
  };
3816
4477
  }
3817
4478
  });
@@ -4116,7 +4777,7 @@ var require_package = __commonJS({
4116
4777
  "package.json"(exports, module) {
4117
4778
  module.exports = {
4118
4779
  name: "@withgateway/sdk",
4119
- version: "0.3.0",
4780
+ version: "0.3.3",
4120
4781
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
4121
4782
  main: "./dist/index.js",
4122
4783
  module: "./dist/index.mjs",
@@ -4612,10 +5273,6 @@ __export(init_exports, {
4612
5273
  isInitialized: () => isInitialized,
4613
5274
  shutdown: () => shutdown2
4614
5275
  });
4615
- function getEnv(key2) {
4616
- const value = process.env[key2];
4617
- return value ? value : void 0;
4618
- }
4619
5276
  function log(message, ...args) {
4620
5277
  if (state.debug) {
4621
5278
  console.log(`[gatewaysdk.tracing] ${message}`, ...args);
@@ -4625,10 +5282,10 @@ function warn(message, ...args) {
4625
5282
  console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
4626
5283
  }
4627
5284
  function buildProvider(config, logFn) {
4628
- const host = config.host ?? getEnv(ENV_VARS.HOST);
4629
- const publicKey = config.publicKey ?? getEnv(ENV_VARS.PUBLIC_KEY);
4630
- const secretKey = config.secretKey ?? getEnv(ENV_VARS.SECRET_KEY);
4631
- const endpoint = config.endpoint ?? getEnv(ENV_VARS.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${DEFAULT_OTLP_PATH}` : void 0);
5285
+ const host = config.host ?? getEnv(GATEWAY_ENV.HOST);
5286
+ const publicKey = config.publicKey ?? getEnv(GATEWAY_ENV.PUBLIC_KEY);
5287
+ const secretKey = config.secretKey ?? getEnv(GATEWAY_ENV.SECRET_KEY);
5288
+ const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${OTLP_TRACES_PATH}` : void 0);
4632
5289
  const missing = [];
4633
5290
  if (!host) missing.push("host (or GATEWAY_HOST env var)");
4634
5291
  if (!publicKey) missing.push("publicKey (or GATEWAY_PUBLIC_KEY env var)");
@@ -4665,9 +5322,7 @@ function buildProvider(config, logFn) {
4665
5322
  ATTR_SERVICE_NAME = "service.name";
4666
5323
  }
4667
5324
  } catch {
4668
- throw new Error(
4669
- "OpenTelemetry packages not found. Install them with:\nnpm install @opentelemetry/sdk-trace-node @opentelemetry/sdk-trace-base @opentelemetry/exporter-trace-otlp-http @opentelemetry/resources"
4670
- );
5325
+ throw new OpenTelemetryMissing();
4671
5326
  }
4672
5327
  const resourceAttrs = {
4673
5328
  [ATTR_SERVICE_NAME]: config.serviceName ?? "gatewaysdk-tracing"
@@ -4873,13 +5528,7 @@ async function shutdown2() {
4873
5528
  function isInitialized() {
4874
5529
  return state.initialized;
4875
5530
  }
4876
- function isGatewayConfigured() {
4877
- const host = getEnv(ENV_VARS.HOST);
4878
- const publicKey = getEnv(ENV_VARS.PUBLIC_KEY);
4879
- const secretKey = getEnv(ENV_VARS.SECRET_KEY);
4880
- return !!(host && publicKey && secretKey);
4881
- }
4882
- var ENV_VARS, DEFAULT_OTLP_PATH, state;
5531
+ var state;
4883
5532
  var init_init = __esm({
4884
5533
  "src/tracing/init.ts"() {
4885
5534
  "use strict";
@@ -4888,13 +5537,8 @@ var init_init = __esm({
4888
5537
  init_session();
4889
5538
  init_scores();
4890
5539
  init_build();
4891
- ENV_VARS = {
4892
- HOST: "GATEWAY_HOST",
4893
- PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
4894
- SECRET_KEY: "GATEWAY_SECRET_KEY",
4895
- ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
4896
- };
4897
- DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
5540
+ init_prerequisites();
5541
+ init_prerequisites();
4898
5542
  state = {
4899
5543
  initialized: false,
4900
5544
  provider: null
@@ -4902,89 +5546,97 @@ var init_init = __esm({
4902
5546
  }
4903
5547
  });
4904
5548
 
5549
+ // src/worlds/taskTracing.ts
5550
+ var HOST_WITH_AUTHORITY, TRACING_HELD_ELSEWHERE, TaskTracing;
5551
+ var init_taskTracing = __esm({
5552
+ "src/worlds/taskTracing.ts"() {
5553
+ "use strict";
5554
+ init_prerequisites();
5555
+ init_platformEndpoint();
5556
+ HOST_WITH_AUTHORITY = /^[a-z][a-z0-9+.-]*:\/\/[^/\\?#@\s]+(\/|$)/i;
5557
+ 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.";
5558
+ TaskTracing = class _TaskTracing {
5559
+ constructor(load = () => Promise.resolve().then(() => (init_init(), init_exports))) {
5560
+ this.load = load;
5561
+ }
5562
+ load;
5563
+ static warned = false;
5564
+ static target = null;
5565
+ async start(requested, options) {
5566
+ if (!requested) return false;
5567
+ const endpoint = platformEndpoint(options);
5568
+ try {
5569
+ await this.initOnce(_TaskTracing.config(endpoint));
5570
+ return true;
5571
+ } catch (error) {
5572
+ _TaskTracing.warnOnce(_TaskTracing.reason(error, endpoint));
5573
+ return false;
5574
+ }
5575
+ }
5576
+ // init() keeps the first provider for the whole process and ignores later calls, so a later run is
5577
+ // traced only when it exports to the same place with the same keys.
5578
+ async initOnce(config) {
5579
+ const tracing2 = await this.load();
5580
+ const target = JSON.stringify([config.endpoint, config.publicKey, config.secretKey]);
5581
+ if (!tracing2.isInitialized()) {
5582
+ tracing2.init(config);
5583
+ _TaskTracing.target = target;
5584
+ return;
5585
+ }
5586
+ if (_TaskTracing.target !== target) throw new Error(TRACING_HELD_ELSEWHERE);
5587
+ }
5588
+ static config(endpoint) {
5589
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
5590
+ return { ...endpoint, endpoint: _TaskTracing.destination(endpoint) };
5591
+ }
5592
+ // B1: a saved login's keys go to their own host only. The redirect is compared as text, never parsed:
5593
+ // a fixed "<host>/" prefix leaves no URL parser room to read another host into it.
5594
+ static destination(endpoint) {
5595
+ const redirect = getEnv(GATEWAY_ENV.ENDPOINT);
5596
+ if (!redirect) return `${endpoint.host}${OTLP_TRACES_PATH}`;
5597
+ if (_TaskTracing.underHost(redirect, endpoint.host) || _TaskTracing.environmentKeys(endpoint)) return redirect;
5598
+ throw new Error(
5599
+ `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.`
5600
+ );
5601
+ }
5602
+ static underHost(redirect, host) {
5603
+ return HOST_WITH_AUTHORITY.test(host) && redirect.startsWith(`${host}/`);
5604
+ }
5605
+ static environmentKeys(endpoint) {
5606
+ const secretKey = getEnv(GATEWAY_ENV.SECRET_KEY) ?? getEnv(GATEWAY_ENV.API_KEY);
5607
+ return endpoint.publicKey === getEnv(GATEWAY_ENV.PUBLIC_KEY) && endpoint.secretKey === secretKey;
5608
+ }
5609
+ static warnOnce(reason) {
5610
+ if (_TaskTracing.warned) return;
5611
+ _TaskTracing.warned = true;
5612
+ console.warn(
5613
+ `[gatewaysdk.tracing] Task runs in this process are not traced. ${reason}
5614
+ Pass --no-trace (trace: false) to run untraced without this warning.`
5615
+ );
5616
+ }
5617
+ static reason(error, endpoint) {
5618
+ if (OpenTelemetryMissing.explains(error)) return new OpenTelemetryMissing().message;
5619
+ const message = error instanceof Error ? error.message : String(error);
5620
+ const keys = endpoint ? [endpoint.publicKey, endpoint.secretKey] : [];
5621
+ return keys.reduce((text2, key2) => text2.split(key2).join("[redacted]"), message);
5622
+ }
5623
+ };
5624
+ }
5625
+ });
5626
+
4905
5627
  // src/worlds/taskRun.ts
4906
5628
  import { randomBytes } from "crypto";
4907
- import * as fs from "fs";
4908
- import * as path from "path";
4909
- function worldRef(live, alias) {
4910
- const { slug, ref } = live.worldRef(alias);
4911
- const session = live.sessions[alias];
4912
- return {
4913
- slug,
4914
- ref,
4915
- versionId: session.versionId,
4916
- sessionId: session.sessionId
4917
- };
4918
- }
4919
- async function fileTaskRun(live, runId, model, report, grade, agentError, traced, runDir, options, setRunId) {
4920
- const aliases = live.aliases;
4921
- const anchor = aliases[0];
4922
- const worldsMeta = Object.fromEntries(
4923
- aliases.map((alias) => [alias, worldRef(live, alias)])
4924
- );
4925
- const evals = new GatewayEvalsClient(options);
4926
- const evaluationId = await evals.createEvaluation(
4927
- worldRef(live, anchor).versionId,
4928
- {
4929
- model,
4930
- rolloutsPerExample: 1,
4931
- ...setRunId ? { runId: setRunId } : {},
4932
- metadata: {
4933
- source: "worlds-task-run",
4934
- task: {
4935
- id: live.taskId,
4936
- version: live.taskVersion,
4937
- name: live.name,
4938
- hash: live.hash
4939
- },
4940
- anchor,
4941
- worlds: worldsMeta,
4942
- runId
4943
- }
4944
- }
4945
- );
4946
- const reward = grade.reward;
4947
- const sample = {
4948
- exampleId: live.name ?? live.taskId ?? runId,
4949
- task: live.name ?? live.taskId ?? runId,
4950
- rolloutIndex: 0,
4951
- prompt: live.instruction,
4952
- ...report === null ? {} : { completion: report },
4953
- rewards: grade.rewards,
4954
- info: {
4955
- sessionId: runId,
4956
- worlds: Object.fromEntries(
4957
- aliases.map((alias) => [
4958
- alias,
4959
- {
4960
- ...worldRef(live, alias),
4961
- reward: grade.worlds[alias]?.reward ?? null
4962
- }
4963
- ])
4964
- ),
4965
- ungraded: grade.ungraded,
4966
- traced,
4967
- runDir,
4968
- ...agentError ? { agentError } : {}
4969
- }
4970
- };
4971
- if (typeof reward === "number") {
4972
- sample.reward = reward;
4973
- sample.score = reward;
4974
- sample.correct = reward >= 1;
4975
- }
4976
- await evals.pushSamples(evaluationId, [sample]);
4977
- await evals.finalizeEvaluation(evaluationId, {
4978
- status: agentError ? "FAILED" : "COMPLETED",
4979
- metrics: {
4980
- ...typeof reward === "number" ? { avg_reward: reward } : {},
4981
- reward,
4982
- ...Object.fromEntries(
4983
- Object.entries(grade.rewards).map(([k, v]) => [`rewards.${k}`, v])
4984
- )
4985
- }
4986
- });
4987
- return evaluationId;
5629
+ import * as path2 from "path";
5630
+ function taskRunFailure(summary) {
5631
+ if (summary.agentError !== null) return `agent raised: ${summary.agentError}`;
5632
+ if (summary.runError !== null) return `the run stopped: ${summary.runError}`;
5633
+ if (summary.gradeError !== null)
5634
+ return `the task could not be graded: ${summary.gradeError}`;
5635
+ if (summary.fileError !== null)
5636
+ return `the run could not be filed: ${summary.fileError}`;
5637
+ if (summary.filed && summary.evaluationStatus !== "COMPLETED")
5638
+ return `the platform settled evaluation ${summary.evaluationId} as ${summary.evaluationStatus}`;
5639
+ return null;
4988
5640
  }
4989
5641
  function reportFromResult(result) {
4990
5642
  if (result === void 0 || result === null)
@@ -5002,120 +5654,47 @@ function reportFromResult(result) {
5002
5654
  );
5003
5655
  return { report, extra };
5004
5656
  }
5657
+ async function refuseUnreadTools(live) {
5658
+ try {
5659
+ for (const alias of live.aliases)
5660
+ WorldToolsUnread.refuse(`${live.worldRef(alias).slug} (alias ${alias})`, live.session(alias).deprecations);
5661
+ } catch (error) {
5662
+ await live.close({ keepWarm: false }).catch(() => void 0);
5663
+ throw error;
5664
+ }
5665
+ }
5005
5666
  async function runTask(ref, agent, opts) {
5006
5667
  const runId = `wtr-${randomBytes(6).toString("hex")}`;
5007
- const outDir = opts.outDir ?? "runs";
5008
- const runDir = path.join(outDir, runId);
5009
- fs.mkdirSync(runDir, { recursive: true });
5010
5668
  const options = opts.options ?? {};
5011
- const live = await openTask(ref, {
5012
- surfaces: opts.surfaces,
5013
- reuseWarm: opts.reuseWarm,
5014
- browserTier: opts.browserTier,
5015
- options
5016
- });
5017
- const started = Date.now();
5018
- try {
5019
- await live.ready();
5020
- let traced = false;
5021
- if (opts.trace !== false) {
5022
- try {
5023
- const { isGatewayConfigured: isGatewayConfigured2, init: init3 } = await Promise.resolve().then(() => (init_init(), init_exports));
5024
- if (isGatewayConfigured2()) {
5025
- init3();
5026
- traced = true;
5027
- }
5028
- } catch {
5029
- traced = false;
5030
- }
5031
- }
5032
- const taskRun = new TaskRun(
5033
- runId,
5034
- live.instruction ?? "",
5035
- opts.model,
5036
- live.sessions,
5037
- await live.manifest()
5038
- );
5039
- let agentError = null;
5040
- let report = null;
5041
- let extra = {};
5042
- try {
5043
- const invoke = () => agent(taskRun);
5044
- const result = traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(runId, invoke) : await invoke();
5045
- ({ report, extra } = reportFromResult(result));
5046
- } catch (error) {
5047
- const message = (error instanceof Error ? error.message : String(error)) || "Unknown error";
5048
- agentError = message.slice(0, 2e3);
5049
- }
5050
- if (traced)
5051
- await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing2) => tracing2.flush()).catch(() => void 0);
5052
- const grade = report !== null ? await live.grade({ report }) : await live.grade();
5053
- fs.writeFileSync(
5054
- path.join(runDir, "manifest.json"),
5055
- JSON.stringify(taskRun.manifest, null, 2)
5056
- );
5057
- fs.writeFileSync(
5058
- path.join(runDir, "grades.json"),
5059
- JSON.stringify(grade, null, 2)
5060
- );
5061
- if (report) fs.writeFileSync(path.join(runDir, "report.md"), report);
5062
- const summary = {
5063
- runId,
5064
- taskId: live.taskId,
5065
- taskVersion: live.taskVersion,
5066
- name: live.name,
5067
- instruction: taskRun.instruction,
5068
- model: opts.model,
5069
- aliases: live.aliases,
5070
- reward: grade.reward,
5071
- rewards: grade.rewards,
5072
- ungraded: grade.ungraded,
5073
- agentError,
5074
- report,
5075
- traced,
5076
- runDir,
5077
- durationSeconds: Math.round((Date.now() - started) / 10) / 100,
5078
- notes: taskRun.notes,
5079
- filed: false,
5080
- ...extra
5081
- };
5082
- if (opts.file !== false) {
5083
- summary.evaluationId = await fileTaskRun(
5084
- live,
5085
- runId,
5086
- opts.model,
5087
- report,
5088
- grade,
5089
- agentError,
5090
- traced,
5091
- runDir,
5092
- options,
5093
- opts.setRunId
5094
- );
5095
- summary.filed = true;
5096
- }
5097
- fs.writeFileSync(
5098
- path.join(runDir, "run.json"),
5099
- JSON.stringify(summary, null, 2)
5100
- );
5101
- return summary;
5102
- } finally {
5103
- if (!opts.keepUp) {
5104
- try {
5105
- await live.close({ keepWarm: false });
5106
- } catch {
5107
- }
5108
- }
5109
- }
5669
+ const opening = new TaskOpening(
5670
+ openTask(ref, {
5671
+ surfaces: opts.surfaces,
5672
+ reuseWarm: opts.reuseWarm,
5673
+ browserTier: opts.browserTier,
5674
+ options
5675
+ }).then(async (live) => {
5676
+ await refuseUnreadTools(live);
5677
+ return new LiveTaskRun(runId, live, opts, options);
5678
+ })
5679
+ );
5680
+ return new RunSignals().during(
5681
+ (signal, urgent) => opening.interrupt(signal, urgent),
5682
+ () => opening.complete(agent)
5683
+ );
5110
5684
  }
5111
- var TaskRun, describeValue, RESULT_CONTRACT;
5685
+ var TaskRun, FILING_UNFINISHED, describeValue, RESULT_CONTRACT, LiveTaskRun, TaskOpening;
5112
5686
  var init_taskRun = __esm({
5113
5687
  "src/worlds/taskRun.ts"() {
5114
5688
  "use strict";
5115
5689
  init_browser();
5116
5690
  init_evals();
5691
+ init_runSignals();
5117
5692
  init_session2();
5118
5693
  init_taskSessions();
5694
+ init_taskRunDir();
5695
+ init_taskRunOutcome();
5696
+ init_taskTracing();
5697
+ init_worldNotice();
5119
5698
  TaskRun = class {
5120
5699
  constructor(runId, instruction, model, sessions2, manifest) {
5121
5700
  this.runId = runId;
@@ -5178,8 +5757,127 @@ var init_taskRun = __esm({
5178
5757
  Object.assign(this.notes, notes);
5179
5758
  }
5180
5759
  };
5760
+ FILING_UNFINISHED = "the filing did not finish";
5181
5761
  describeValue = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
5182
5762
  RESULT_CONTRACT = "return a string report, an object {report?: string, ...} or nothing";
5763
+ LiveTaskRun = class {
5764
+ constructor(runId, live, opts, options) {
5765
+ this.live = live;
5766
+ this.opts = opts;
5767
+ this.options = options;
5768
+ this.files = new TaskRunDir(path2.join(opts.outDir ?? "runs", runId));
5769
+ this.outcome = new TaskRunOutcome(runId, live, opts.model, this.files.path);
5770
+ }
5771
+ live;
5772
+ opts;
5773
+ options;
5774
+ files;
5775
+ outcome;
5776
+ played = false;
5777
+ interrupted = false;
5778
+ filing = null;
5779
+ ending = null;
5780
+ async complete(agent) {
5781
+ try {
5782
+ await this.play(agent).catch((error) => this.outcome.stopped(error));
5783
+ this.played = true;
5784
+ await this.file();
5785
+ return this.record();
5786
+ } finally {
5787
+ await this.end();
5788
+ }
5789
+ }
5790
+ async interrupt(signal, urgent = false) {
5791
+ const sessions2 = this.opts.keepUp ? "leaving its sessions up" : "closing its sessions";
5792
+ const how = urgent ? " now, without waiting" : "";
5793
+ process.stderr.write(`${this.outcome.runId} interrupted by ${signal}; ${sessions2}${how}
5794
+ `);
5795
+ if (!this.played) this.outcome.stopped(`interrupted by ${signal}`);
5796
+ this.interrupted = true;
5797
+ try {
5798
+ if (!urgent) await this.filing;
5799
+ this.record();
5800
+ } catch (error) {
5801
+ const why = error instanceof Error ? error.message : String(error);
5802
+ process.stderr.write(`${this.outcome.runId}: run.json not written: ${why}
5803
+ `);
5804
+ } finally {
5805
+ await this.end();
5806
+ }
5807
+ }
5808
+ summary() {
5809
+ return this.outcome.summary();
5810
+ }
5811
+ async play(agent) {
5812
+ await this.live.ready();
5813
+ this.outcome.traced = await new TaskTracing().start(this.opts.trace !== false, this.options);
5814
+ const run = new TaskRun(
5815
+ this.outcome.runId,
5816
+ this.live.instruction ?? "",
5817
+ this.opts.model,
5818
+ this.live.sessions,
5819
+ await this.live.manifest()
5820
+ );
5821
+ this.outcome.notes = run.notes;
5822
+ this.files.writeManifest(run.manifest);
5823
+ await this.callAgent(agent, run);
5824
+ const report = this.outcome.report;
5825
+ this.outcome.grade = await this.live.grade(report !== null ? { report } : {});
5826
+ this.files.writeJson("grades.json", this.outcome.grade);
5827
+ if (report) this.files.write("report.md", report);
5828
+ }
5829
+ async callAgent(agent, run) {
5830
+ try {
5831
+ const invoke = () => agent(run);
5832
+ const result = this.outcome.traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(run.runId, invoke) : await invoke();
5833
+ ({ report: this.outcome.report, extra: this.outcome.extra } = reportFromResult(result));
5834
+ } catch (error) {
5835
+ this.outcome.agentRaised(error);
5836
+ }
5837
+ if (this.outcome.traced)
5838
+ await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing2) => tracing2.flush()).catch(() => void 0);
5839
+ }
5840
+ async file() {
5841
+ if (this.opts.file === false || this.interrupted) return;
5842
+ this.files.writeJson("run.json", { ...this.outcome.summary(), fileError: FILING_UNFINISHED });
5843
+ this.filing = this.outcome.file(new GatewayEvalsClient(this.options), this.opts.setRunId);
5844
+ await this.filing;
5845
+ }
5846
+ record() {
5847
+ const summary = this.outcome.summary();
5848
+ this.files.writeJson("run.json", summary);
5849
+ return summary;
5850
+ }
5851
+ end() {
5852
+ this.ending ??= this.opts.keepUp ? Promise.resolve() : this.live.close({ keepWarm: false }).then(
5853
+ () => void 0,
5854
+ () => void 0
5855
+ );
5856
+ return this.ending;
5857
+ }
5858
+ };
5859
+ TaskOpening = class {
5860
+ interrupting = null;
5861
+ run = null;
5862
+ opened;
5863
+ constructor(opening) {
5864
+ this.opened = opening.then((run) => this.run = run);
5865
+ }
5866
+ async complete(agent) {
5867
+ const run = await this.opened;
5868
+ if (!this.interrupting) return run.complete(agent);
5869
+ await this.interrupting;
5870
+ return run.summary();
5871
+ }
5872
+ interrupt(signal, urgent = false) {
5873
+ if (urgent) return this.run?.interrupt(signal, true) ?? Promise.resolve();
5874
+ this.interrupting ??= this.opened.then(
5875
+ (run) => run.interrupt(signal),
5876
+ () => void 0
5877
+ );
5878
+ return this.interrupting;
5879
+ }
5880
+ };
5183
5881
  }
5184
5882
  });
5185
5883
 
@@ -5259,69 +5957,153 @@ async function runSet(datasetId, agent, opts) {
5259
5957
  source: opts.source ?? "sdk",
5260
5958
  ...git && Object.keys(git).length ? { git } : {}
5261
5959
  });
5262
- const close = (status) => apiRequest(
5263
- options,
5264
- "POST",
5265
- `${SET_RUNS}/${encodeURIComponent(opened.id)}/close`,
5266
- { status }
5267
- );
5268
- const onInterrupt = () => {
5269
- void close("failed").finally(() => process.exit(130));
5270
- };
5271
- process.once("SIGINT", onInterrupt);
5272
- const planned = await runPlannedTasks(opened, agent, opts, options).finally(
5273
- () => process.off("SIGINT", onInterrupt)
5274
- );
5275
- const { tasks, failed } = planned;
5276
- const requested = failed ? "failed" : "completed";
5277
- const setRun = await close(requested);
5278
- return {
5279
- setRun,
5280
- tasks,
5281
- failed: failed || setRun.status !== "completed",
5282
- closedAsRequested: setRun.status === requested
5283
- };
5960
+ return new SetRunner(opened, agent, opts, options).run();
5284
5961
  }
5285
- async function runPlannedTasks(opened, agent, opts, options) {
5286
- const tasks = [];
5287
- let failed = false;
5288
- for (const task of opened.tasks) {
5289
- try {
5290
- const summary = await runTask(task.worldTaskId, agent, {
5291
- model: opts.model,
5292
- surfaces: opts.surfaces,
5293
- browserTier: opts.browserTier,
5294
- outDir: opts.outDir,
5295
- trace: opts.trace,
5296
- setRunId: opened.id,
5297
- options
5298
- });
5299
- tasks.push(summary);
5300
- failed ||= summary.agentError !== null;
5301
- opts.onTask?.(summary);
5302
- } catch (error) {
5303
- failed = true;
5304
- const message = error instanceof Error ? error.message : String(error);
5305
- process.stderr.write(`task ${task.name} could not run: ${message}
5306
- `);
5307
- }
5308
- }
5309
- return { tasks, failed };
5310
- }
5311
- var SET_RUNS;
5962
+ var SET_RUNS, SetRunner;
5312
5963
  var init_setRun = __esm({
5313
5964
  "src/worlds/setRun.ts"() {
5314
5965
  "use strict";
5315
5966
  init_worlds();
5316
5967
  init_checkoutProvenance();
5968
+ init_runSignals();
5317
5969
  init_taskRun();
5318
5970
  SET_RUNS = "/api/public/set-runs";
5971
+ SetRunner = class {
5972
+ constructor(opened, agent, opts, options) {
5973
+ this.opened = opened;
5974
+ this.agent = agent;
5975
+ this.opts = opts;
5976
+ this.options = options;
5977
+ }
5978
+ opened;
5979
+ agent;
5980
+ opts;
5981
+ options;
5982
+ tasks = [];
5983
+ failures = [];
5984
+ failed = false;
5985
+ current = null;
5986
+ interrupted = null;
5987
+ signals = new RunSignals();
5988
+ async run() {
5989
+ await this.signals.during(
5990
+ (signal2) => this.interrupt(signal2).then(() => void 0),
5991
+ () => this.runTasks()
5992
+ );
5993
+ const signal = this.signals.signal;
5994
+ if (signal !== null) return this.result(await this.interrupt(signal), "failed");
5995
+ const requested = this.failed ? "failed" : "completed";
5996
+ return this.result(await this.close(requested, this.failures), requested);
5997
+ }
5998
+ interrupt(signal) {
5999
+ const stopped = this.current ? [{ worldTaskId: this.current, reason: `interrupted by ${signal}` }] : [];
6000
+ this.interrupted ??= this.close("failed", [...this.failures, ...stopped]);
6001
+ return this.interrupted;
6002
+ }
6003
+ async runTasks() {
6004
+ for (const task of this.opened.tasks) {
6005
+ if (this.signals.signal !== null) return;
6006
+ this.current = task.worldTaskId;
6007
+ await this.runOne(task);
6008
+ }
6009
+ this.current = null;
6010
+ }
6011
+ async runOne(task) {
6012
+ try {
6013
+ const summary = await runTask(task.worldTaskId, this.agent, {
6014
+ model: this.opts.model,
6015
+ surfaces: this.opts.surfaces,
6016
+ browserTier: this.opts.browserTier,
6017
+ outDir: this.opts.outDir,
6018
+ trace: this.opts.trace,
6019
+ setRunId: this.opened.id,
6020
+ options: this.options
6021
+ });
6022
+ this.tasks.push(summary);
6023
+ this.failed ||= taskRunFailure(summary) !== null;
6024
+ if (summary.fileError !== null)
6025
+ this.failures.push({ worldTaskId: task.worldTaskId, reason: summary.fileError });
6026
+ this.opts.onTask?.(summary);
6027
+ } catch (error) {
6028
+ this.failed = true;
6029
+ const message = error instanceof Error ? error.message : String(error);
6030
+ process.stderr.write(`task ${task.name} could not run: ${message}
6031
+ `);
6032
+ this.failures.push({
6033
+ worldTaskId: task.worldTaskId,
6034
+ reason: message || (error instanceof Error ? error.name : "Error")
6035
+ });
6036
+ }
6037
+ }
6038
+ close(status, failures = []) {
6039
+ return apiRequest(
6040
+ this.options,
6041
+ "POST",
6042
+ `${SET_RUNS}/${encodeURIComponent(this.opened.id)}/close`,
6043
+ { status, ...failures.length ? { failures } : {} }
6044
+ );
6045
+ }
6046
+ result(setRun, requested) {
6047
+ return {
6048
+ setRun,
6049
+ tasks: this.tasks,
6050
+ failed: this.failed || setRun.status !== "completed",
6051
+ closedAsRequested: setRun.status === requested
6052
+ };
6053
+ }
6054
+ };
6055
+ }
6056
+ });
6057
+
6058
+ // src/worlds/requestLog.ts
6059
+ var WorldRequestLog;
6060
+ var init_requestLog = __esm({
6061
+ "src/worlds/requestLog.ts"() {
6062
+ "use strict";
6063
+ WorldRequestLog = class _WorldRequestLog {
6064
+ constructor(requests) {
6065
+ this.requests = requests;
6066
+ }
6067
+ requests;
6068
+ static async read(session) {
6069
+ const state2 = await session.state();
6070
+ const calls = state2?.calls;
6071
+ if (!Array.isArray(calls)) return null;
6072
+ return new _WorldRequestLog(calls.filter((c) => typeof c === "object" && c !== null));
6073
+ }
6074
+ /** How many older requests the world no longer keeps (its log is bounded): the gap
6075
+ * before the first entry it still holds. */
6076
+ get dropped() {
6077
+ const first = Number(this.requests[0]?.seq) || 1;
6078
+ return Math.max(0, first - 1);
6079
+ }
6080
+ /** Each request in the shape of a ledger line, `kind: "request"`, so one JSONL
6081
+ * file holds both. */
6082
+ asCalls() {
6083
+ return this.requests.map((r) => ({
6084
+ seq: Number(r.seq) || 0,
6085
+ kind: "request",
6086
+ tool: r.operation ?? null,
6087
+ args: {
6088
+ method: r.method ?? null,
6089
+ path: r.path ?? null,
6090
+ args: r.args ?? null,
6091
+ query: r.query ?? null,
6092
+ body: r.body ?? null,
6093
+ via: r.via ?? null
6094
+ },
6095
+ result: { status: r.status ?? null, rows: r.rows ?? null, found: r.found ?? null },
6096
+ error: null,
6097
+ completedAt: r.at ?? null
6098
+ }));
6099
+ }
6100
+ };
5319
6101
  }
5320
6102
  });
5321
6103
 
5322
6104
  // src/worlds/checkoutTarget.ts
5323
6105
  import * as fs2 from "fs";
5324
- import * as path2 from "path";
6106
+ import * as path3 from "path";
5325
6107
  var CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
5326
6108
  var init_checkoutTarget = __esm({
5327
6109
  "src/worlds/checkoutTarget.ts"() {
@@ -5350,17 +6132,17 @@ var init_checkoutTarget = __esm({
5350
6132
  }
5351
6133
  /** The absolute destination; a relative target may not resolve above `cwd`. */
5352
6134
  static confined(target, cwd) {
5353
- const dir = path2.resolve(cwd, target);
5354
- if (path2.isAbsolute(target)) return dir;
5355
- const rel = path2.relative(cwd, dir);
5356
- if (rel === "" || !rel.startsWith("..") && !path2.isAbsolute(rel))
6135
+ const dir = path3.resolve(cwd, target);
6136
+ if (path3.isAbsolute(target)) return dir;
6137
+ const rel = path3.relative(cwd, dir);
6138
+ if (rel === "" || !rel.startsWith("..") && !path3.isAbsolute(rel))
5357
6139
  return dir;
5358
6140
  throw new CheckoutRefusedError(
5359
6141
  `--target ${target} resolves outside the current directory (${cwd}) \u2014 run from that directory, or pass an absolute path`
5360
6142
  );
5361
6143
  }
5362
6144
  static fresh(slug, cwd) {
5363
- const base = path2.join(cwd, slug);
6145
+ const base = path3.join(cwd, slug);
5364
6146
  let dir = base;
5365
6147
  let i = 1;
5366
6148
  while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
@@ -5387,7 +6169,7 @@ var init_checkoutTarget = __esm({
5387
6169
  ].filter(_CheckoutTarget.confinedRel).sort();
5388
6170
  }
5389
6171
  static confinedRel(rel) {
5390
- if (rel === "" || path2.isAbsolute(rel)) return false;
6172
+ if (rel === "" || path3.isAbsolute(rel)) return false;
5391
6173
  return !rel.split(/[\\/]/).includes("..");
5392
6174
  }
5393
6175
  /** Why `dir` cannot be overwritten, or null when it is a clean checkout of `slug`. */
@@ -5405,9 +6187,9 @@ var init_checkoutTarget = __esm({
5405
6187
  /** Remove the replaced checkout's files (and the dirs they leave empty). */
5406
6188
  clear() {
5407
6189
  for (const rel of this.replaces) {
5408
- const abs = path2.join(this.dir, rel);
6190
+ const abs = path3.join(this.dir, rel);
5409
6191
  fs2.rmSync(abs, { force: true });
5410
- this.pruneEmptyParents(path2.dirname(abs));
6192
+ this.pruneEmptyParents(path3.dirname(abs));
5411
6193
  }
5412
6194
  }
5413
6195
  pruneEmptyParents(from) {
@@ -5415,7 +6197,7 @@ var init_checkoutTarget = __esm({
5415
6197
  while (cur !== this.dir && cur.startsWith(this.dir)) {
5416
6198
  if (fs2.existsSync(cur) && fs2.readdirSync(cur).length > 0) return;
5417
6199
  fs2.rmSync(cur, { recursive: true, force: true });
5418
- cur = path2.dirname(cur);
6200
+ cur = path3.dirname(cur);
5419
6201
  }
5420
6202
  }
5421
6203
  };
@@ -5425,7 +6207,7 @@ var init_checkoutTarget = __esm({
5425
6207
  // src/worlds/worldDir.ts
5426
6208
  import { createHash as createHash4 } from "crypto";
5427
6209
  import * as fs3 from "fs";
5428
- import * as path3 from "path";
6210
+ import * as path4 from "path";
5429
6211
  var 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;
5430
6212
  var init_worldDir = __esm({
5431
6213
  "src/worlds/worldDir.ts"() {
@@ -5455,17 +6237,17 @@ var init_worldDir = __esm({
5455
6237
  worldRoot;
5456
6238
  pathsMemo;
5457
6239
  static open(dir) {
5458
- const present = (rel) => fs3.existsSync(path3.join(dir, rel));
6240
+ const present = (rel) => fs3.existsSync(path4.join(dir, rel));
5459
6241
  const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
5460
6242
  return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
5461
6243
  }
5462
6244
  // ── Files ──────────────────────────────────────────────────────────────
5463
6245
  has(rel) {
5464
- return fs3.existsSync(path3.join(this.dir, rel));
6246
+ return fs3.existsSync(path4.join(this.dir, rel));
5465
6247
  }
5466
6248
  text(rel) {
5467
6249
  try {
5468
- return fs3.readFileSync(path3.join(this.dir, rel), "utf-8");
6250
+ return fs3.readFileSync(path4.join(this.dir, rel), "utf-8");
5469
6251
  } catch {
5470
6252
  return null;
5471
6253
  }
@@ -5484,7 +6266,7 @@ var init_worldDir = __esm({
5484
6266
  const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
5485
6267
  const out = [];
5486
6268
  const walk = (rel) => {
5487
- for (const entry of fs3.readdirSync(rel ? path3.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
6269
+ for (const entry of fs3.readdirSync(rel ? path4.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
5488
6270
  const child = rel ? `${rel}/${entry.name}` : entry.name;
5489
6271
  if (entry.isDirectory()) {
5490
6272
  if (!skippedDir(entry.name)) walk(child);
@@ -5505,7 +6287,7 @@ var init_worldDir = __esm({
5505
6287
  get exclusions() {
5506
6288
  const kept = new Set(this.archivePaths);
5507
6289
  const out = [];
5508
- const filesUnder = (rel) => fs3.readdirSync(path3.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
6290
+ const filesUnder = (rel) => fs3.readdirSync(path4.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
5509
6291
  const child = `${rel}/${entry.name}`;
5510
6292
  if (entry.isDirectory()) return filesUnder(child);
5511
6293
  return entry.isFile() ? [child] : [];
@@ -5536,7 +6318,7 @@ var init_worldDir = __esm({
5536
6318
  const hasher = createHash4("sha256");
5537
6319
  for (const rel of this.archivePaths) {
5538
6320
  hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
5539
- hasher.update(fs3.readFileSync(path3.join(this.dir, rel)));
6321
+ hasher.update(fs3.readFileSync(path4.join(this.dir, rel)));
5540
6322
  }
5541
6323
  return hasher.digest("hex");
5542
6324
  }
@@ -5601,7 +6383,7 @@ var init_worldDir = __esm({
5601
6383
 
5602
6384
  // src/worlds/tar.ts
5603
6385
  import * as fs4 from "fs";
5604
- import * as path4 from "path";
6386
+ import * as path5 from "path";
5605
6387
  import { gunzipSync } from "zlib";
5606
6388
  function archiveMode(mode) {
5607
6389
  return (mode & 73) !== 0 ? TAR_EXEC_MODE : TAR_FILE_MODE;
@@ -5687,8 +6469,8 @@ var init_tar = __esm({
5687
6469
  for (const member of this.members()) {
5688
6470
  if (member.typeflag !== "0" && member.typeflag !== "\0") continue;
5689
6471
  if (!_TarArchive.isConfined(member.name)) continue;
5690
- const target = path4.join(dir, member.name);
5691
- fs4.mkdirSync(path4.dirname(target), { recursive: true });
6472
+ const target = path5.join(dir, member.name);
6473
+ fs4.mkdirSync(path5.dirname(target), { recursive: true });
5692
6474
  const mode = archiveMode(member.mode);
5693
6475
  fs4.writeFileSync(target, member.bytes, { mode });
5694
6476
  fs4.chmodSync(target, mode);
@@ -5732,7 +6514,7 @@ __export(hub_exports, {
5732
6514
  import { createHash as createHash5 } from "crypto";
5733
6515
  import { gzipSync } from "zlib";
5734
6516
  import * as fs5 from "fs";
5735
- import * as path5 from "path";
6517
+ import * as path6 from "path";
5736
6518
  function collectArchiveRelPaths(dir) {
5737
6519
  const world = WorldDir.open(dir);
5738
6520
  if (world.hasRootGitignore) {
@@ -5795,7 +6577,7 @@ function tarMember(name, payload, mode) {
5795
6577
  function buildSourceTarball(dir) {
5796
6578
  const parts2 = [];
5797
6579
  for (const relPath of collectArchiveRelPaths(dir)) {
5798
- const abs = path5.join(dir, relPath);
6580
+ const abs = path6.join(dir, relPath);
5799
6581
  parts2.push(
5800
6582
  ...tarMember(
5801
6583
  relPath,
@@ -5809,7 +6591,7 @@ function buildSourceTarball(dir) {
5809
6591
  }
5810
6592
  function readLocalMeta(dir) {
5811
6593
  try {
5812
- const raw = fs5.readFileSync(path5.join(dir, META_RELPATH), "utf-8");
6594
+ const raw = fs5.readFileSync(path6.join(dir, META_RELPATH), "utf-8");
5813
6595
  const parsed = JSON.parse(raw);
5814
6596
  return parsed && typeof parsed === "object" ? parsed : null;
5815
6597
  } catch {
@@ -5832,16 +6614,16 @@ function resolvePushSlug(dir, opts = {}) {
5832
6614
  }
5833
6615
  const first = declared[0];
5834
6616
  if (first) return { ...base, slug: first.slug, source: first.source };
5835
- return { ...base, slug: path5.basename(path5.resolve(dir)), source: "directory name" };
6617
+ return { ...base, slug: path6.basename(path6.resolve(dir)), source: "directory name" };
5836
6618
  }
5837
6619
  function writeLocalMeta(dir, meta) {
5838
- fs5.mkdirSync(path5.join(dir, ".gateway"), { recursive: true });
5839
- fs5.writeFileSync(path5.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
6620
+ fs5.mkdirSync(path6.join(dir, ".gateway"), { recursive: true });
6621
+ fs5.writeFileSync(path6.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
5840
6622
  }
5841
6623
  function snapshotTreeHashes(dir) {
5842
6624
  const out = {};
5843
6625
  for (const relPath of collectArchiveRelPaths(dir)) {
5844
- out[relPath] = createHash5("sha256").update(fs5.readFileSync(path5.join(dir, relPath))).digest("hex");
6626
+ out[relPath] = createHash5("sha256").update(fs5.readFileSync(path6.join(dir, relPath))).digest("hex");
5845
6627
  }
5846
6628
  return out;
5847
6629
  }
@@ -5859,7 +6641,7 @@ function localTreeState(dir) {
5859
6641
  }
5860
6642
  function readPyproject(dir) {
5861
6643
  try {
5862
- const text2 = fs5.readFileSync(path5.join(dir, "pyproject.toml"), "utf-8");
6644
+ const text2 = fs5.readFileSync(path6.join(dir, "pyproject.toml"), "utf-8");
5863
6645
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
5864
6646
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
5865
6647
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -5882,7 +6664,7 @@ function readWorldContractVersion(dir) {
5882
6664
  }
5883
6665
  function buildFileManifest(dir) {
5884
6666
  return collectArchiveRelPaths(dir).map((relPath) => {
5885
- const payload = fs5.readFileSync(path5.join(dir, relPath));
6667
+ const payload = fs5.readFileSync(path6.join(dir, relPath));
5886
6668
  return {
5887
6669
  path: relPath,
5888
6670
  isDirectory: false,
@@ -5928,7 +6710,7 @@ async function push(dir, opts = {}) {
5928
6710
  const pyproject = readPyproject(dir);
5929
6711
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
5930
6712
  if (contractVersion) pyproject.version = contractVersion;
5931
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path5.basename(path5.resolve(dir));
6713
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path6.basename(path6.resolve(dir));
5932
6714
  const metadata = {
5933
6715
  ...opts.name !== void 0 ? { name: opts.name } : {},
5934
6716
  ...opts.description !== void 0 ? { description: opts.description } : {},
@@ -6054,7 +6836,7 @@ async function propose(dir, opts) {
6054
6836
  if (state2.clean)
6055
6837
  throw new Error("Nothing to propose \u2014 the tree matches its base.");
6056
6838
  const changes = state2.dirty.map((relPath) => {
6057
- const abs = path5.join(dir, relPath);
6839
+ const abs = path6.join(dir, relPath);
6058
6840
  if (!fs5.existsSync(abs))
6059
6841
  return { path: relPath, action: "delete" };
6060
6842
  return {
@@ -6095,7 +6877,7 @@ var init_hub = __esm({
6095
6877
  init_worldDir();
6096
6878
  init_tar();
6097
6879
  DEFAULT_BRANCH = "main";
6098
- META_RELPATH = path5.join(".gateway", ".env-metadata.json");
6880
+ META_RELPATH = path6.join(".gateway", ".env-metadata.json");
6099
6881
  FILE_MODE = TAR_FILE_MODE;
6100
6882
  MINTED_SUFFIX = /^-\d+$/;
6101
6883
  PushSlugConflict = class extends Error {
@@ -6323,7 +7105,7 @@ function rebaseConflicts(error) {
6323
7105
  }
6324
7106
  const conflicts = body?.conflicts;
6325
7107
  if (!Array.isArray(conflicts)) return null;
6326
- return conflicts.filter(isConflictPath).map(({ path: path16, reason }) => ({ path: path16, reason }));
7108
+ return conflicts.filter(isConflictPath).map(({ path: path17, reason }) => ({ path: path17, reason }));
6327
7109
  }
6328
7110
  function isConflictPath(value) {
6329
7111
  const entry = value;
@@ -6363,8 +7145,8 @@ var init_sandbox = __esm({
6363
7145
  this.options = options;
6364
7146
  }
6365
7147
  options;
6366
- request(method, path16, body) {
6367
- return apiRequest(this.options, method, path16, body, {
7148
+ request(method, path17, body) {
7149
+ return apiRequest(this.options, method, path17, body, {
6368
7150
  timeoutMs: SANDBOX_REQUEST_TIMEOUT_MS
6369
7151
  });
6370
7152
  }
@@ -6895,6 +7677,17 @@ async function queryRows(slugRef, entity, opts = {}, options = {}) {
6895
7677
  `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
6896
7678
  );
6897
7679
  }
7680
+ async function sqlRows(slugRef, sql, opts = {}, options = {}) {
7681
+ const [slug, ref] = slugRef.split("@", 2);
7682
+ if (!slug || ref === "") throw new Error(`sqlRows needs a slug, optionally slug@ref (got ${slugRef})`);
7683
+ if (!sql.trim()) throw new Error("sqlRows needs one SQL statement");
7684
+ return apiRequest(options, "POST", `/api/public/worlds/${encodeURIComponent(slug)}/data/sql`, {
7685
+ sql,
7686
+ ...ref ? { ref } : {},
7687
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
7688
+ ...opts.scenario ? { scenario: opts.scenario } : {}
7689
+ });
7690
+ }
6898
7691
  var init_query = __esm({
6899
7692
  "src/worlds/query.ts"() {
6900
7693
  "use strict";
@@ -6951,7 +7744,7 @@ var init_router = __esm({
6951
7744
  // src/cli/config.ts
6952
7745
  import * as fs6 from "fs";
6953
7746
  import * as os from "os";
6954
- import * as path6 from "path";
7747
+ import * as path7 from "path";
6955
7748
  async function api(creds, method, route, body, init3 = {}) {
6956
7749
  const reply = await new PlatformTransport(creds).send(method, route, body, init3);
6957
7750
  if (!reply.ok) throw new ApiError2(reply.status, method, route, reply.text);
@@ -6987,7 +7780,7 @@ var init_config = __esm({
6987
7780
  import { spawn, spawnSync } from "child_process";
6988
7781
  import * as fs7 from "fs";
6989
7782
  import * as net from "net";
6990
- import * as path7 from "path";
7783
+ import * as path8 from "path";
6991
7784
  function tailOf(output) {
6992
7785
  return output.length > DIAGNOSTIC_TAIL ? `\u2026${output.slice(-DIAGNOSTIC_TAIL)}` : output;
6993
7786
  }
@@ -7002,16 +7795,16 @@ function runtimeRoot() {
7002
7795
  const candidates = [];
7003
7796
  if (process.env.GATEWAY_RUNTIME_PATH)
7004
7797
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
7005
- const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path7.dirname(fs7.realpathSync(process.argv[1])) : null;
7798
+ const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path8.dirname(fs7.realpathSync(process.argv[1])) : null;
7006
7799
  if (entry) {
7007
- candidates.push(path7.resolve(entry, "..", "runtime"));
7008
- candidates.push(path7.resolve(entry, "..", "..", "runtime"));
7009
- candidates.push(path7.resolve(entry, "..", "..", "sdk", "src"));
7800
+ candidates.push(path8.resolve(entry, "..", "runtime"));
7801
+ candidates.push(path8.resolve(entry, "..", "..", "runtime"));
7802
+ candidates.push(path8.resolve(entry, "..", "..", "sdk", "src"));
7010
7803
  }
7011
- candidates.push(path7.resolve(process.cwd(), "runtime"));
7804
+ candidates.push(path8.resolve(process.cwd(), "runtime"));
7012
7805
  for (const root of candidates)
7013
7806
  if (fs7.existsSync(
7014
- path7.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7807
+ path8.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7015
7808
  ))
7016
7809
  return root;
7017
7810
  return null;
@@ -7100,13 +7893,12 @@ async function localBridge(request, local, opts = {}) {
7100
7893
  ["-I", "-B", "-c", BRIDGE_BOOTSTRAP, local.root],
7101
7894
  {
7102
7895
  stdio: ["pipe", "pipe", "pipe"],
7103
- // This is the author's machine running the author's world: the one
7104
- // place the runtime may import and run a world's own Python (check's
7105
- // handler execution, `worlds call`, `worlds test` tool cases).
7896
+ // The author's machine running the author's world: the one place the runtime may run its Python.
7106
7897
  env: {
7107
7898
  PATH: process.env.PATH ?? "",
7108
7899
  HOME: process.env.HOME ?? "",
7109
7900
  TMPDIR: process.env.TMPDIR ?? "",
7901
+ ...process.env.VIRTUAL_ENV ? { VIRTUAL_ENV: process.env.VIRTUAL_ENV } : {},
7110
7902
  GATEWAY_RUNTIME_EXECUTE: "1"
7111
7903
  }
7112
7904
  }
@@ -7152,13 +7944,14 @@ async function localBridge(request, local, opts = {}) {
7152
7944
  child.stdin.end(input);
7153
7945
  });
7154
7946
  }
7155
- var MIN_PYTHON, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
7947
+ var MIN_PYTHON, MIN_PYTHON_LABEL, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
7156
7948
  var init_local = __esm({
7157
7949
  "src/cli/runtime/local.ts"() {
7158
7950
  "use strict";
7159
7951
  init_router();
7160
7952
  init_config();
7161
7953
  MIN_PYTHON = [3, 12];
7954
+ MIN_PYTHON_LABEL = MIN_PYTHON.join(".");
7162
7955
  DIAGNOSTIC_TAIL = 600;
7163
7956
  BRIDGE_BOOTSTRAP = `import sys,runpy,types,os
7164
7957
  root=sys.argv[1]
@@ -7177,18 +7970,46 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
7177
7970
  }
7178
7971
  });
7179
7972
 
7973
+ // src/worlds/databaseEngines.ts
7974
+ function isWorldSnapshotOutput(path17) {
7975
+ return SNAPSHOT_OUTPUTS.has(path17.slice(path17.lastIndexOf("/") + 1));
7976
+ }
7977
+ var WORLD_DATABASE_ENGINES, DEFAULT_WORLD_DATABASE_ENGINE, WORLD_SNAPSHOT_FILE_PATH, WORLD_SNAPSHOT_SIDECAR_PATH, WORLD_SNAPSHOT_CONTENT_TYPE, SNAPSHOT_OUTPUTS;
7978
+ var init_databaseEngines = __esm({
7979
+ "src/worlds/databaseEngines.ts"() {
7980
+ "use strict";
7981
+ WORLD_DATABASE_ENGINES = [
7982
+ {
7983
+ engine: "sqlite",
7984
+ label: "SQLite",
7985
+ target: "sqlite-json/1",
7986
+ snapshotName: "state.sqlite",
7987
+ contentType: "application/vnd.sqlite3"
7988
+ }
7989
+ ];
7990
+ DEFAULT_WORLD_DATABASE_ENGINE = WORLD_DATABASE_ENGINES[0];
7991
+ WORLD_SNAPSHOT_FILE_PATH = `db/${DEFAULT_WORLD_DATABASE_ENGINE.snapshotName}`;
7992
+ WORLD_SNAPSHOT_SIDECAR_PATH = "db/state.json";
7993
+ WORLD_SNAPSHOT_CONTENT_TYPE = DEFAULT_WORLD_DATABASE_ENGINE.contentType;
7994
+ SNAPSHOT_OUTPUTS = /* @__PURE__ */ new Set([
7995
+ ...WORLD_DATABASE_ENGINES.map((engine) => engine.snapshotName),
7996
+ WORLD_SNAPSHOT_SIDECAR_PATH.slice("db/".length)
7997
+ ]);
7998
+ }
7999
+ });
8000
+
7180
8001
  // src/cli/runtime/client.ts
7181
8002
  import * as fs8 from "fs";
7182
- import * as path8 from "path";
8003
+ import * as path9 from "path";
7183
8004
  function isBuildOutput(relPath) {
7184
- return BUILD_OUTPUT.test(relPath);
8005
+ return isWorldSnapshotOutput(relPath);
7185
8006
  }
7186
8007
  function readWorldFiles(dir, opts = {}) {
7187
8008
  if (!fs8.existsSync(dir) || !fs8.statSync(dir).isDirectory())
7188
8009
  throw new CliError(`Not a directory: ${dir}`);
7189
8010
  const out = [];
7190
8011
  const walk = (rel) => {
7191
- const abs = rel ? path8.join(dir, rel) : dir;
8012
+ const abs = rel ? path9.join(dir, rel) : dir;
7192
8013
  const entries = fs8.readdirSync(abs, { withFileTypes: true });
7193
8014
  entries.sort((a, b) => a.name.localeCompare(b.name));
7194
8015
  for (const entry of entries) {
@@ -7199,7 +8020,7 @@ function readWorldFiles(dir, opts = {}) {
7199
8020
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
7200
8021
  out.push({
7201
8022
  path: childRel,
7202
- content: fs8.readFileSync(path8.join(dir, childRel)).toString("base64")
8023
+ content: fs8.readFileSync(path9.join(dir, childRel)).toString("base64")
7203
8024
  });
7204
8025
  }
7205
8026
  }
@@ -7219,15 +8040,15 @@ function writeWorldFiles(dir, files, opts = {}) {
7219
8040
  if (opts.clean && fs8.existsSync(dir)) {
7220
8041
  const keep = new Set(files.map((f) => f.path));
7221
8042
  for (const existing of readWorldFiles(dir)) {
7222
- if (!keep.has(existing.path)) fs8.rmSync(path8.join(dir, existing.path));
8043
+ if (!keep.has(existing.path)) fs8.rmSync(path9.join(dir, existing.path));
7223
8044
  }
7224
8045
  pruneEmptyDirs(dir);
7225
8046
  }
7226
8047
  fs8.mkdirSync(dir, { recursive: true });
7227
8048
  const written = [];
7228
8049
  for (const file of files) {
7229
- const target = path8.join(dir, file.path);
7230
- fs8.mkdirSync(path8.dirname(target), { recursive: true });
8050
+ const target = path9.join(dir, file.path);
8051
+ fs8.mkdirSync(path9.dirname(target), { recursive: true });
7231
8052
  fs8.writeFileSync(target, Buffer.from(file.content, "base64"));
7232
8053
  written.push(file.path);
7233
8054
  }
@@ -7237,7 +8058,7 @@ function pruneEmptyDirs(root) {
7237
8058
  const walk = (abs) => {
7238
8059
  let empty = true;
7239
8060
  for (const entry of fs8.readdirSync(abs, { withFileTypes: true })) {
7240
- const child = path8.join(abs, entry.name);
8061
+ const child = path9.join(abs, entry.name);
7241
8062
  if (entry.isDirectory()) {
7242
8063
  if (SKIP_DIRS.has(entry.name)) {
7243
8064
  empty = false;
@@ -7274,13 +8095,9 @@ function yamlDocument(request) {
7274
8095
  return isYamlSource(String(request.source ?? ""), String(request.document ?? ""));
7275
8096
  return request.command === "conform" && Array.isArray(request.openapi) && request.openapi.some((doc) => YAML_NAME.test(doc.path ?? ""));
7276
8097
  }
7277
- function runsLocally(request) {
7278
- const local = localRuntime();
7279
- return local !== null && !(yamlDocument(request) && !localPythonHasYaml(local.python));
7280
- }
7281
8098
  async function runtime(creds, request, opts = {}) {
7282
- const local = localRuntime();
7283
- if (local && runsLocally(request)) {
8099
+ const local = new RuntimeRoute().local(request);
8100
+ if (local) {
7284
8101
  const payload2 = await localBridge(request, local, opts);
7285
8102
  const detail2 = detailFrom(payload2);
7286
8103
  if (detail2) throw new BridgeRejected(detail2);
@@ -7307,17 +8124,17 @@ async function runtime(creds, request, opts = {}) {
7307
8124
  if (payload === null) throw new CliError(`POST ${route} (${request.command}) returned non-JSON`, 1);
7308
8125
  return payload;
7309
8126
  }
7310
- var MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BUILD_OUTPUT, BridgeRejected, YAML_NAME;
8127
+ var MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BridgeRejected, YAML_NAME, YAML_NEEDS_PYYAML, YAML_NOT_ON_PLATFORM, RuntimeRoute;
7311
8128
  var init_client = __esm({
7312
8129
  "src/cli/runtime/client.ts"() {
7313
8130
  "use strict";
7314
8131
  init_router();
7315
8132
  init_local();
7316
8133
  init_transport();
8134
+ init_databaseEngines();
7317
8135
  MAX_WORLD_FILES = 2e3;
7318
8136
  RUNTIME_TIMEOUT_MS = 18e4;
7319
8137
  SKIP_DIRS = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
7320
- BUILD_OUTPUT = /(^|\/)state\.(sqlite|json)$/;
7321
8138
  BridgeRejected = class extends CliError {
7322
8139
  constructor(detail) {
7323
8140
  super(formatBridgeError(detail), 1);
@@ -7326,6 +8143,26 @@ var init_client = __esm({
7326
8143
  detail;
7327
8144
  };
7328
8145
  YAML_NAME = /\.ya?ml$/i;
8146
+ 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";
8147
+ 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)";
8148
+ RuntimeRoute = class _RuntimeRoute {
8149
+ constructor(here = localRuntime, hasYaml = localPythonHasYaml) {
8150
+ this.here = here;
8151
+ this.hasYaml = hasYaml;
8152
+ }
8153
+ here;
8154
+ hasYaml;
8155
+ local(request) {
8156
+ const local = this.here();
8157
+ const yaml = yamlDocument(request);
8158
+ if (yaml && local === null) throw _RuntimeRoute.refusal(YAML_NOT_ON_PLATFORM);
8159
+ if (yaml && local !== null && !this.hasYaml(local.python)) throw _RuntimeRoute.refusal(YAML_NEEDS_PYYAML);
8160
+ return local;
8161
+ }
8162
+ static refusal(message) {
8163
+ return new BridgeRejected({ code: "conform", message, schema_path: "/openapi/source" });
8164
+ }
8165
+ };
7329
8166
  }
7330
8167
  });
7331
8168
 
@@ -7340,10 +8177,11 @@ __export(data_exports, {
7340
8177
  ingestLocalToolCalls: () => ingestLocalToolCalls,
7341
8178
  queryLocalWorldData: () => queryLocalWorldData,
7342
8179
  queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
7343
- showLocalWorldData: () => showLocalWorldData
8180
+ showLocalWorldData: () => showLocalWorldData,
8181
+ sqlLocalWorldData: () => sqlLocalWorldData
7344
8182
  });
7345
8183
  import * as fs9 from "fs";
7346
- import * as path9 from "path";
8184
+ import * as path10 from "path";
7347
8185
  async function importLocalWorldData(dir, rows, opts, creds) {
7348
8186
  const res = await runtime(creds, {
7349
8187
  command: "import-data",
@@ -7374,8 +8212,8 @@ async function importLocalWorldFiles(dir, sources, opts) {
7374
8212
  );
7375
8213
  const res = await runtime(null, {
7376
8214
  command: "import-files",
7377
- world: path9.resolve(dir),
7378
- sources: sources.map((source) => path9.resolve(source)),
8215
+ world: path10.resolve(dir),
8216
+ sources: sources.map((source) => path10.resolve(source)),
7379
8217
  mode: opts.mode ?? "append",
7380
8218
  ...opts.entity ? { entity: opts.entity } : {},
7381
8219
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
@@ -7401,9 +8239,9 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7401
8239
  );
7402
8240
  const res = await runtime(null, {
7403
8241
  command: "chunk-files",
7404
- world: path9.resolve(dir),
7405
- sources: sources.map((source) => path9.resolve(source)),
7406
- out: path9.resolve(out),
8242
+ world: path10.resolve(dir),
8243
+ sources: sources.map((source) => path10.resolve(source)),
8244
+ out: path10.resolve(out),
7407
8245
  ...opts.entity ? { entity: opts.entity } : {},
7408
8246
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
7409
8247
  ...opts.chunkBytes ? { chunk_bytes: opts.chunkBytes } : {},
@@ -7414,8 +8252,8 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7414
8252
  function dropBuildOutputs(dir) {
7415
8253
  const dropped = [];
7416
8254
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
7417
- if (!BUILD_OUTPUT2.test(file.path)) continue;
7418
- fs9.rmSync(path9.join(dir, file.path), { force: true });
8255
+ if (!isBuildOutput(file.path)) continue;
8256
+ fs9.rmSync(path10.join(dir, file.path), { force: true });
7419
8257
  dropped.push(file.path);
7420
8258
  }
7421
8259
  return dropped;
@@ -7482,7 +8320,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7482
8320
  const payload = await localBridge(
7483
8321
  {
7484
8322
  command: "query",
7485
- world: path9.resolve(dir),
8323
+ world: path10.resolve(dir),
7486
8324
  entity,
7487
8325
  across: true,
7488
8326
  ...opts.compare ? { compare: true } : {},
@@ -7498,6 +8336,28 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7498
8336
  if (detail) throw new BridgeRejected(detail);
7499
8337
  return payload;
7500
8338
  }
8339
+ async function sqlLocalWorldData(dir, sql, opts) {
8340
+ const local = localRuntime();
8341
+ if (!local)
8342
+ throw new CliError(
8343
+ "`worlds data sql` on a directory reads the world's database in place, so it needs the bundled runtime on this machine. " + localRuntimeReason(),
8344
+ 1
8345
+ );
8346
+ const payload = await localBridge(
8347
+ {
8348
+ command: "sql",
8349
+ world: path10.resolve(dir),
8350
+ sql,
8351
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
8352
+ ...opts.scenario ? { scenario: opts.scenario } : {}
8353
+ },
8354
+ local
8355
+ );
8356
+ const detail = detailFrom(payload);
8357
+ if (detail) throw new BridgeRejected(detail);
8358
+ const { status: _status, ...answer } = payload;
8359
+ return answer;
8360
+ }
7501
8361
  async function ingestLocalToolCalls(dir, records, opts, creds) {
7502
8362
  const res = await runtime(creds, {
7503
8363
  command: "ingest-calls",
@@ -7529,14 +8389,12 @@ async function ingestLocalToolCalls(dir, records, opts, creds) {
7529
8389
  ...res.batch !== void 0 ? { batch: res.batch } : {}
7530
8390
  };
7531
8391
  }
7532
- var BUILD_OUTPUT2;
7533
8392
  var init_data = __esm({
7534
8393
  "src/cli/runtime/data.ts"() {
7535
8394
  "use strict";
7536
8395
  init_router();
7537
8396
  init_client();
7538
8397
  init_local();
7539
- BUILD_OUTPUT2 = /(^|\/)state\.(sqlite|json)$/;
7540
8398
  }
7541
8399
  });
7542
8400
 
@@ -7555,7 +8413,7 @@ __export(transform_exports, {
7555
8413
  import { spawn as spawn2 } from "child_process";
7556
8414
  import * as fs10 from "fs";
7557
8415
  import * as os2 from "os";
7558
- import * as path10 from "path";
8416
+ import * as path11 from "path";
7559
8417
  import { createRequire } from "module";
7560
8418
  function checkRows(value, where) {
7561
8419
  if (!isObject(value)) throw new CliError(`${where} must return {entity: [rows]}; got ${value === null ? "null" : Array.isArray(value) ? "an array" : typeof value}`, 1);
@@ -7580,27 +8438,27 @@ function typescriptPrefix() {
7580
8438
  }
7581
8439
  }
7582
8440
  function command(script, wrapperDir) {
7583
- const suffix = path10.extname(script).toLowerCase();
8441
+ const suffix = path11.extname(script).toLowerCase();
7584
8442
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
7585
8443
  if (NODE_SUFFIXES.includes(suffix)) {
7586
- const wrapper = path10.join(wrapperDir, "transform-wrapper.mjs");
8444
+ const wrapper = path11.join(wrapperDir, "transform-wrapper.mjs");
7587
8445
  fs10.writeFileSync(wrapper, NODE_WRAPPER);
7588
8446
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
7589
8447
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
7590
8448
  }
7591
- throw new CliError(`${path10.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
8449
+ throw new CliError(`${path11.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
7592
8450
  }
7593
8451
  async function runTransform(script, records, opts = {}) {
7594
8452
  if (!fs10.existsSync(script) || !fs10.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
7595
- const resolved = path10.resolve(script);
7596
- const name = path10.basename(script);
8453
+ const resolved = path11.resolve(script);
8454
+ const name = path11.basename(script);
7597
8455
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
7598
- const scratch = fs10.mkdtempSync(path10.join(os2.tmpdir(), "gateway-transform-"));
8456
+ const scratch = fs10.mkdtempSync(path11.join(os2.tmpdir(), "gateway-transform-"));
7599
8457
  try {
7600
8458
  const { file, args } = command(resolved, scratch);
7601
8459
  const env = Object.fromEntries(Object.entries(process.env).filter(([key2]) => !SCRUBBED.test(key2)));
7602
8460
  const done = await new Promise((resolve11, reject) => {
7603
- const child = spawn2(file, args, { cwd: path10.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
8461
+ const child = spawn2(file, args, { cwd: path11.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
7604
8462
  let stdout = "";
7605
8463
  let stderr = "";
7606
8464
  let timedOut = false;
@@ -7761,7 +8619,7 @@ async function forEachLimited(items, limit, fn) {
7761
8619
  if (failure) throw failure;
7762
8620
  }
7763
8621
  async function waitForBatch(client2, batchId, opts = {}) {
7764
- const sleep3 = opts.sleep ?? client2.sleep;
8622
+ const sleep5 = opts.sleep ?? client2.sleep;
7765
8623
  let delay = opts.initialDelayMs ?? 1e3;
7766
8624
  let last = "";
7767
8625
  for (; ; ) {
@@ -7772,7 +8630,7 @@ async function waitForBatch(client2, batchId, opts = {}) {
7772
8630
  last = phase;
7773
8631
  }
7774
8632
  if (isSettled(status)) return status;
7775
- await sleep3(delay);
8633
+ await sleep5(delay);
7776
8634
  delay = Math.min(1e4, Math.round(delay * 1.5));
7777
8635
  }
7778
8636
  }
@@ -7794,10 +8652,10 @@ var init_batches = __esm({
7794
8652
  ]);
7795
8653
  sleepMs = (ms) => new Promise((resolve11) => setTimeout(resolve11, ms));
7796
8654
  WorldDataApi = class {
7797
- constructor(creds, slug, sleep3 = sleepMs) {
8655
+ constructor(creds, slug, sleep5 = sleepMs) {
7798
8656
  this.creds = creds;
7799
8657
  this.slug = slug;
7800
- this.sleep = sleep3;
8658
+ this.sleep = sleep5;
7801
8659
  this.base = `/api/public/worlds/${encodeURIComponent(slug)}/data`;
7802
8660
  }
7803
8661
  creds;
@@ -8080,7 +8938,7 @@ var init_push = __esm({
8080
8938
  // src/worlds/ingest.ts
8081
8939
  import * as fs12 from "fs";
8082
8940
  import * as os3 from "os";
8083
- import * as path11 from "path";
8941
+ import * as path12 from "path";
8084
8942
  function parseToolCallRecord(raw) {
8085
8943
  const none = { record: null, skip: null };
8086
8944
  if (!isObject2(raw)) return { ...none, refusal: "record is not an object" };
@@ -8204,19 +9062,13 @@ function toolCallsFromObservations(observations, opts = {}) {
8204
9062
  }
8205
9063
  return out;
8206
9064
  }
8207
- function credentialsOf(options) {
8208
- const host = (options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai").replace(/\/+$/, "");
8209
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8210
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8211
- return publicKey && secretKey ? { host, publicKey, secretKey } : null;
8212
- }
8213
9065
  async function ingest(world, records, options = {}) {
8214
9066
  const { ingestLocalToolCalls: ingestLocalToolCalls2, importLocalWorldData: importLocalWorldData2, checkLocalWorldData: checkLocalWorldData2 } = await Promise.resolve().then(() => (init_data(), data_exports));
8215
9067
  if (options.transform !== void 0 && options.map !== void 0) throw new Error("records go through one of transform, map");
8216
9068
  const redact = options.redact ?? "off";
8217
9069
  const mode = options.mode ?? "append";
8218
9070
  const dryRun = Boolean(options.dryRun);
8219
- const creds = credentialsOf(options);
9071
+ const creds = platformEndpoint(options);
8220
9072
  const local = fs12.existsSync(world) && fs12.statSync(world).isDirectory();
8221
9073
  const map = options.map === void 0 ? void 0 : options.map;
8222
9074
  let rows = null;
@@ -8230,12 +9082,12 @@ async function ingest(world, records, options = {}) {
8230
9082
  const landed = dryRun ? await checkLocalWorldData2(world, rows, creds, { redact }) : await importLocalWorldData2(world, rows, { mode, redact }, creds);
8231
9083
  return transformReport(world, records.length, rows, stderr, { dryRun, redact, changed: !dryRun && landed.changed, kind: landed.kind, entityCounts: landed.entity_counts });
8232
9084
  }
8233
- if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
9085
+ if (!creds) throw new Error(GATEWAY_KEYS_MISSING);
8234
9086
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
8235
9087
  const slug = world.split("@", 1)[0];
8236
- const scratch = fs12.mkdtempSync(path11.join(os3.tmpdir(), "gateway-ingest-"));
9088
+ const scratch = fs12.mkdtempSync(path12.join(os3.tmpdir(), "gateway-ingest-"));
8237
9089
  try {
8238
- const pulled = await pull2(world, path11.join(scratch, slug), options);
9090
+ const pulled = await pull2(world, path12.join(scratch, slug), options);
8239
9091
  let report;
8240
9092
  let projected;
8241
9093
  if (rows === null) {
@@ -8314,6 +9166,7 @@ var init_ingest = __esm({
8314
9166
  "src/worlds/ingest.ts"() {
8315
9167
  "use strict";
8316
9168
  init_worlds();
9169
+ init_platformEndpoint();
8317
9170
  isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
8318
9171
  isoOf = (value) => value instanceof Date ? value.toISOString() : typeof value === "string" ? value : void 0;
8319
9172
  }
@@ -8414,7 +9267,7 @@ var init_dryRun = __esm({
8414
9267
  /** Poll until the run settles or `timeoutSeconds` pass; the last verdict either way. */
8415
9268
  async settle(runId, opts) {
8416
9269
  const now = opts.now ?? Date.now;
8417
- const sleep3 = opts.sleep ?? ((ms) => new Promise((resolve11) => setTimeout(resolve11, ms)));
9270
+ const sleep5 = opts.sleep ?? ((ms) => new Promise((resolve11) => setTimeout(resolve11, ms)));
8418
9271
  const deadline = now() + opts.timeoutSeconds * 1e3;
8419
9272
  for (; ; ) {
8420
9273
  const budget = Math.floor(
@@ -8424,7 +9277,7 @@ var init_dryRun = __esm({
8424
9277
  const verdict = await this.status(runId, budget);
8425
9278
  if (verdict.settled || now() >= deadline) return verdict;
8426
9279
  opts.onProgress?.(verdict);
8427
- if (now() - asked < 1e3) await sleep3(1e3);
9280
+ if (now() - asked < 1e3) await sleep5(1e3);
8428
9281
  }
8429
9282
  }
8430
9283
  };
@@ -8492,6 +9345,8 @@ __export(worlds_exports, {
8492
9345
  EXPLORATORY_TASK: () => EXPLORATORY_TASK,
8493
9346
  EXPLORATORY_TASK_SOURCE: () => EXPLORATORY_TASK_SOURCE,
8494
9347
  GatewayEvalsClient: () => GatewayEvalsClient,
9348
+ GradePayload: () => GradePayload,
9349
+ GradeRefused: () => GradeRefused,
8495
9350
  HostedDryRunClient: () => HostedDryRunClient,
8496
9351
  IDLE_WARNING_MINUTES: () => IDLE_WARNING_MINUTES,
8497
9352
  KEY_NAMES: () => KEY_NAMES,
@@ -8507,6 +9362,7 @@ __export(worlds_exports, {
8507
9362
  PlatformTransport: () => PlatformTransport,
8508
9363
  PushSlugConflict: () => PushSlugConflict,
8509
9364
  RECOVER_BUDGET_MS: () => RECOVER_BUDGET_MS,
9365
+ RetryPolicy: () => RetryPolicy,
8510
9366
  SANDBOX_REQUEST_TIMEOUT_MS: () => SANDBOX_REQUEST_TIMEOUT_MS,
8511
9367
  SECRET_KEY: () => SECRET_KEY,
8512
9368
  SESSION_STATES: () => SESSION_STATES,
@@ -8514,8 +9370,10 @@ __export(worlds_exports, {
8514
9370
  STATUS_WAIT_CAP_SECONDS: () => STATUS_WAIT_CAP_SECONDS,
8515
9371
  STRIP_PREFIX: () => STRIP_PREFIX,
8516
9372
  SURFACE_KINDS: () => SURFACE_KINDS,
9373
+ Score: () => Score,
8517
9374
  Screen: () => Screen,
8518
9375
  SecretScrubber: () => SecretScrubber,
9376
+ TOOL_CONTRACT_UNREAD: () => TOOL_CONTRACT_UNREAD,
8519
9377
  TRANSFORMS: () => TRANSFORMS,
8520
9378
  TaskEditClient: () => TaskEditClient,
8521
9379
  TaskRun: () => TaskRun,
@@ -8527,13 +9385,17 @@ __export(worlds_exports, {
8527
9385
  WorldBrowser: () => WorldBrowser,
8528
9386
  WorldCallTooLarge: () => WorldCallTooLarge,
8529
9387
  WorldEditClient: () => WorldEditClient,
9388
+ WorldNotice: () => WorldNotice,
9389
+ WorldRequestLog: () => WorldRequestLog,
8530
9390
  WorldSandboxClient: () => WorldSandboxClient,
8531
9391
  WorldSession: () => WorldSession,
8532
9392
  WorldSessionCallError: () => WorldSessionCallError,
9393
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
8533
9394
  WorldSessionTimeout: () => WorldSessionTimeout,
8534
9395
  WorldSet: () => WorldSet,
8535
9396
  WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
8536
9397
  WorldTask: () => WorldTask,
9398
+ WorldToolsUnread: () => WorldToolsUnread,
8537
9399
  aggregateTaskGrade: () => aggregateTaskGrade,
8538
9400
  apiRequest: () => apiRequest,
8539
9401
  applyTransforms: () => applyTransforms,
@@ -8576,6 +9438,7 @@ __export(worlds_exports, {
8576
9438
  listTaskSessions: () => listTaskSessions,
8577
9439
  listTasks: () => listTasks,
8578
9440
  localTreeState: () => localTreeState,
9441
+ messageOf: () => messageOf,
8579
9442
  migrateProjectWorlds: () => migrateProjectWorlds,
8580
9443
  migrateWorld: () => migrateWorld,
8581
9444
  modelToolName: () => modelToolName,
@@ -8619,9 +9482,14 @@ __export(worlds_exports, {
8619
9482
  setEnvSecret: () => setEnvSecret,
8620
9483
  shapeText: () => shapeText,
8621
9484
  snapshotTreeHashes: () => snapshotTreeHashes,
9485
+ sqlRows: () => sqlRows,
9486
+ stillStartingHint: () => stillStartingHint,
8622
9487
  storedForms: () => storedForms,
9488
+ surfaceDescriptor: () => surfaceDescriptor,
8623
9489
  surfaceKinds: () => surfaceKinds,
9490
+ taskGradeError: () => taskGradeError,
8624
9491
  taskLabel: () => taskLabel,
9492
+ taskRunFailure: () => taskRunFailure,
8625
9493
  toolCallRecordsIn: () => toolCallRecordsIn,
8626
9494
  toolCallsFromObservations: () => toolCallsFromObservations,
8627
9495
  uiEntryUrl: () => uiEntryUrl,
@@ -8635,17 +9503,6 @@ __export(worlds_exports, {
8635
9503
  withoutUiToken: () => withoutUiToken,
8636
9504
  writeLocalMeta: () => writeLocalMeta
8637
9505
  });
8638
- function resolveEnv(options) {
8639
- const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
8640
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8641
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8642
- if (!publicKey || !secretKey) {
8643
- throw new Error(
8644
- "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey)."
8645
- );
8646
- }
8647
- return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
8648
- }
8649
9506
  function parseErrorBody(text2, limit = 600) {
8650
9507
  const raw = { detail: text2.slice(0, limit), code: null };
8651
9508
  let parsed;
@@ -8665,9 +9522,9 @@ function parseErrorBody(text2, limit = 600) {
8665
9522
  code: stableCode
8666
9523
  };
8667
9524
  }
8668
- async function apiRequest(options, method, path16, body, init3 = {}) {
8669
- const reply = await new PlatformTransport(resolveEnv(options)).send(method, path16, body, init3);
8670
- if (!reply.ok) throw new ApiError(reply.status, method, path16, reply.text);
9525
+ async function apiRequest(options, method, path17, body, init3 = {}) {
9526
+ const reply = await new PlatformTransport(requirePlatformEndpoint(options)).send(method, path17, body, init3);
9527
+ if (!reply.ok) throw new ApiError(reply.status, method, path17, reply.text);
8671
9528
  return JSON.parse(reply.text);
8672
9529
  }
8673
9530
  async function setEnvSecret(slug, key2, value, options = {}) {
@@ -8779,7 +9636,6 @@ function detectProvenance() {
8779
9636
  kind: "ci",
8780
9637
  id: env.GITHUB_ACTOR,
8781
9638
  name: env.GITHUB_ACTOR,
8782
- // GitHub serves every account's avatar at /<login>.png.
8783
9639
  imageUrl: `${server}/${env.GITHUB_ACTOR}.png`
8784
9640
  };
8785
9641
  }
@@ -8848,9 +9704,11 @@ var ApiError, WorldTask, WorldSet;
8848
9704
  var init_worlds = __esm({
8849
9705
  "src/worlds/index.ts"() {
8850
9706
  "use strict";
9707
+ init_platformEndpoint();
8851
9708
  init_transport();
8852
9709
  init_transport();
8853
9710
  init_session2();
9711
+ init_grade();
8854
9712
  init_sessionList();
8855
9713
  init_sessionContract();
8856
9714
  init_taskRef();
@@ -8861,6 +9719,8 @@ var init_worlds = __esm({
8861
9719
  init_checkoutProvenance();
8862
9720
  init_evals();
8863
9721
  init_browser();
9722
+ init_surfaceDescriptor();
9723
+ init_requestLog();
8864
9724
  init_browserReconnect();
8865
9725
  init_nativeComputer();
8866
9726
  init_hub();
@@ -8874,10 +9734,11 @@ var init_worlds = __esm({
8874
9734
  init_sandbox();
8875
9735
  init_editSessions();
8876
9736
  init_dryRun();
9737
+ init_worldNotice();
8877
9738
  ApiError = class extends Error {
8878
- constructor(status, method, path16, text2) {
9739
+ constructor(status, method, path17, text2) {
8879
9740
  const parsed = parseErrorBody(text2);
8880
- super(`${method} ${path16} -> ${status}: ${parsed.detail}`);
9741
+ super(`${method} ${path17} -> ${status}: ${parsed.detail}`);
8881
9742
  this.status = status;
8882
9743
  this.text = text2;
8883
9744
  this.name = "ApiError";
@@ -9532,8 +10393,8 @@ var GatewayContext = class _GatewayContext {
9532
10393
  }
9533
10394
  return resolved;
9534
10395
  }
9535
- async requestJson(method, path16, payload) {
9536
- const response = await fetch(`${this.host}${path16}`, {
10396
+ async requestJson(method, path17, payload) {
10397
+ const response = await fetch(`${this.host}${path17}`, {
9537
10398
  method,
9538
10399
  headers: {
9539
10400
  Authorization: this.authHeader(),
@@ -9544,7 +10405,7 @@ var GatewayContext = class _GatewayContext {
9544
10405
  const text2 = await response.text();
9545
10406
  const body = text2 ? safeJson(text2) : {};
9546
10407
  if (!response.ok) {
9547
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path16}`;
10408
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path17}`;
9548
10409
  if (response.status === 409) {
9549
10410
  throw new GatewayContextConflictError(
9550
10411
  message,
@@ -9553,7 +10414,7 @@ var GatewayContext = class _GatewayContext {
9553
10414
  }
9554
10415
  throw new GatewayContextError(message);
9555
10416
  }
9556
- this.log("%s %s -> %s", method, path16, response.status);
10417
+ this.log("%s %s -> %s", method, path17, response.status);
9557
10418
  return body;
9558
10419
  }
9559
10420
  authHeader() {
@@ -9660,11 +10521,11 @@ function primaryPathForKind(kind, snapshot) {
9660
10521
  const preferred = kind === "agent" ? "AGENTS.md" : kind === "prompt" ? "PROMPT.md" : kind === "skill" ? "SKILL.md" : "MEMORY.md";
9661
10522
  if (readFile(snapshot, preferred)) return preferred;
9662
10523
  return Object.entries(snapshot.files).find(
9663
- ([path16, entry]) => entry.type === "file" && path16.endsWith(".md")
10524
+ ([path17, entry]) => entry.type === "file" && path17.endsWith(".md")
9664
10525
  )?.[0] ?? null;
9665
10526
  }
9666
- function parseJsonFile(snapshot, path16) {
9667
- const content = readFile(snapshot, path16);
10527
+ function parseJsonFile(snapshot, path17) {
10528
+ const content = readFile(snapshot, path17);
9668
10529
  if (!content) return {};
9669
10530
  try {
9670
10531
  const parsed = JSON.parse(content);
@@ -9673,8 +10534,8 @@ function parseJsonFile(snapshot, path16) {
9673
10534
  return {};
9674
10535
  }
9675
10536
  }
9676
- function readFile(snapshot, path16) {
9677
- const entry = snapshot.files[path16];
10537
+ function readFile(snapshot, path17) {
10538
+ const entry = snapshot.files[path17];
9678
10539
  return entry?.type === "file" ? entry.content : null;
9679
10540
  }
9680
10541
  function provenanceFor(snapshot, label) {
@@ -9776,16 +10637,16 @@ var GatewayMemory = class _GatewayMemory {
9776
10637
  `/api/public/context/memory/${encodeIdentifier2(identifier)}${suffix}`
9777
10638
  );
9778
10639
  }
9779
- async read(target, path16, options = {}) {
10640
+ async read(target, path17, options = {}) {
9780
10641
  const snapshot = typeof target === "string" ? await this.pull(target, options) : target;
9781
- const file = snapshot.files[path16];
10642
+ const file = snapshot.files[path17];
9782
10643
  return file?.type === "file" ? file.content : null;
9783
10644
  }
9784
- async write(target, path16, content, options = {}) {
10645
+ async write(target, path17, content, options = {}) {
9785
10646
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
9786
10647
  return this.push(snapshot.repo.repoHandle, {
9787
10648
  files: {
9788
- [path16]: {
10649
+ [path17]: {
9789
10650
  content,
9790
10651
  ...options.mimeType ? { mimeType: options.mimeType } : {}
9791
10652
  }
@@ -9795,10 +10656,10 @@ var GatewayMemory = class _GatewayMemory {
9795
10656
  ...runtimeContextPayload2(options)
9796
10657
  });
9797
10658
  }
9798
- async delete(target, path16, options = {}) {
10659
+ async delete(target, path17, options = {}) {
9799
10660
  const snapshot = typeof target === "string" ? await this.pull(target) : target;
9800
10661
  return this.push(snapshot.repo.repoHandle, {
9801
- files: { [path16]: null },
10662
+ files: { [path17]: null },
9802
10663
  parentCommit: options.parentCommit ?? snapshot.commit.commitHash,
9803
10664
  message: options.message,
9804
10665
  ...runtimeContextPayload2(options)
@@ -9811,8 +10672,8 @@ var GatewayMemory = class _GatewayMemory {
9811
10672
  payload
9812
10673
  );
9813
10674
  }
9814
- async requestJson(method, path16, payload) {
9815
- const response = await fetch(`${this.host}${path16}`, {
10675
+ async requestJson(method, path17, payload) {
10676
+ const response = await fetch(`${this.host}${path17}`, {
9816
10677
  method,
9817
10678
  headers: {
9818
10679
  Authorization: this.authHeader(),
@@ -9823,7 +10684,7 @@ var GatewayMemory = class _GatewayMemory {
9823
10684
  const text2 = await response.text();
9824
10685
  const body = text2 ? safeJson2(text2) : {};
9825
10686
  if (!response.ok) {
9826
- const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path16}`;
10687
+ const message = typeof body.message === "string" ? body.message : `Gateway API ${response.status} on ${path17}`;
9827
10688
  if (response.status === 409) {
9828
10689
  throw new GatewayMemoryConflictError(
9829
10690
  message,
@@ -9832,7 +10693,7 @@ var GatewayMemory = class _GatewayMemory {
9832
10693
  }
9833
10694
  throw new GatewayMemoryError(message);
9834
10695
  }
9835
- this.log("%s %s -> %s", method, path16, response.status);
10696
+ this.log("%s %s -> %s", method, path17, response.status);
9836
10697
  return body;
9837
10698
  }
9838
10699
  authHeader() {
@@ -9994,10 +10855,10 @@ var PersonasClient = class _PersonasClient {
9994
10855
  done: Boolean(payload.done)
9995
10856
  };
9996
10857
  }
9997
- async requestJson(method, path16, payload) {
10858
+ async requestJson(method, path17, payload) {
9998
10859
  let response;
9999
10860
  try {
10000
- response = await fetch(`${this.host}${path16}`, {
10861
+ response = await fetch(`${this.host}${path17}`, {
10001
10862
  method,
10002
10863
  headers: {
10003
10864
  Authorization: this.authHeader(),
@@ -10008,7 +10869,7 @@ var PersonasClient = class _PersonasClient {
10008
10869
  });
10009
10870
  } catch (error) {
10010
10871
  const reason = error instanceof Error ? error.message : String(error);
10011
- throw new PersonaError(`${method} ${path16} failed: ${reason}`, {
10872
+ throw new PersonaError(`${method} ${path17} failed: ${reason}`, {
10012
10873
  cause: error
10013
10874
  });
10014
10875
  }
@@ -10016,10 +10877,10 @@ var PersonasClient = class _PersonasClient {
10016
10877
  if (!response.ok) {
10017
10878
  const detail = text2.slice(0, 500);
10018
10879
  throw new PersonaError(
10019
- `${method} ${path16} failed (${response.status}): ${detail}`
10880
+ `${method} ${path17} failed (${response.status}): ${detail}`
10020
10881
  );
10021
10882
  }
10022
- this.log("%s %s -> %s", method, path16, response.status);
10883
+ this.log("%s %s -> %s", method, path17, response.status);
10023
10884
  return safeJson3(text2);
10024
10885
  }
10025
10886
  authHeader() {
@@ -10244,10 +11105,10 @@ var SessionRecording = class _SessionRecording {
10244
11105
  };
10245
11106
  }
10246
11107
  /** Dump the full session as a JSON fixture (for regression tests). */
10247
- toFixture(path16) {
10248
- mkdirSync(dirname(path16), { recursive: true });
10249
- writeFileSync(path16, JSON.stringify(this.toJSON(), null, 2), "utf-8");
10250
- return path16;
11108
+ toFixture(path17) {
11109
+ mkdirSync(dirname(path17), { recursive: true });
11110
+ writeFileSync(path17, JSON.stringify(this.toJSON(), null, 2), "utf-8");
11111
+ return path17;
10251
11112
  }
10252
11113
  static fromApi(raw) {
10253
11114
  return new _SessionRecording({
@@ -11278,8 +12139,8 @@ var GatewaySecurity = class _GatewaySecurity {
11278
12139
  );
11279
12140
  }
11280
12141
  }
11281
- async postJson(path16, payload) {
11282
- const response = await fetch(`${this.host}${path16}`, {
12142
+ async postJson(path17, payload) {
12143
+ const response = await fetch(`${this.host}${path17}`, {
11283
12144
  method: "POST",
11284
12145
  headers: {
11285
12146
  Authorization: this.authHeader(),
@@ -11290,7 +12151,7 @@ var GatewaySecurity = class _GatewaySecurity {
11290
12151
  if (!response.ok) {
11291
12152
  const body = await response.text();
11292
12153
  throw new GatewaySecurityError(
11293
- `Gateway API ${response.status} on ${path16}: ${body || response.statusText}`
12154
+ `Gateway API ${response.status} on ${path17}: ${body || response.statusText}`
11294
12155
  );
11295
12156
  }
11296
12157
  return await response.json();
@@ -11383,7 +12244,7 @@ __export(files_exports, {
11383
12244
  init_worlds();
11384
12245
  import { createHash as createHash10 } from "crypto";
11385
12246
  import * as fs13 from "fs";
11386
- import * as path12 from "path";
12247
+ import * as path13 from "path";
11387
12248
  import { Readable, Transform } from "stream";
11388
12249
  import { pipeline } from "stream/promises";
11389
12250
  var DRIVE_PREFIX = "drive:";
@@ -11434,7 +12295,7 @@ async function readFile2(options = {}, filePath, limits = {}) {
11434
12295
  async function downloadFile(options = {}, filePath, outPath, onProgress) {
11435
12296
  const file = await getFile(options, filePath);
11436
12297
  const res = await openDownload(file.url);
11437
- fs13.mkdirSync(path12.dirname(path12.resolve(outPath)), { recursive: true });
12298
+ fs13.mkdirSync(path13.dirname(path13.resolve(outPath)), { recursive: true });
11438
12299
  const source = Readable.fromWeb(res.body);
11439
12300
  await pipeline(source, counting((n) => onProgress?.(n, file.sizeBytes)), fs13.createWriteStream(outPath));
11440
12301
  return { ...file, out: outPath };
@@ -11483,7 +12344,7 @@ var CONTENT_TYPES = {
11483
12344
  ".mp4": "video/mp4"
11484
12345
  };
11485
12346
  function contentTypeFor(name) {
11486
- return CONTENT_TYPES[path12.extname(name).toLowerCase()] ?? "application/octet-stream";
12347
+ return CONTENT_TYPES[path13.extname(name).toLowerCase()] ?? "application/octet-stream";
11487
12348
  }
11488
12349
  function counting(report) {
11489
12350
  let seen = 0;
@@ -13305,9 +14166,9 @@ function alternatives(raw) {
13305
14166
  if (!parts2.length || parts2.includes("*")) return [];
13306
14167
  return parts2;
13307
14168
  }
13308
- function hasPath(node, path16) {
14169
+ function hasPath(node, path17) {
13309
14170
  let current = node;
13310
- for (const part of path16.split(".")) {
14171
+ for (const part of path17.split(".")) {
13311
14172
  if (current && typeof current === "object" && !Array.isArray(current) && part in current) {
13312
14173
  current = current[part];
13313
14174
  continue;
@@ -13348,7 +14209,7 @@ function matches(m, event) {
13348
14209
  if (m.args.length) {
13349
14210
  const haystacks = event.event === "pre_request" || event.event === "post_response" ? [event.request?.query, event.request?.body] : [event.toolInput];
13350
14211
  const present = haystacks.filter((h) => h !== void 0 && h !== null);
13351
- if (!m.args.every((path16) => present.some((h) => hasPath(h, path16))))
14212
+ if (!m.args.every((path17) => present.some((h) => hasPath(h, path17))))
13352
14213
  return false;
13353
14214
  }
13354
14215
  return true;
@@ -14184,7 +15045,7 @@ function parseHandler(event, raw, position) {
14184
15045
  // src/hooks/handlers.ts
14185
15046
  import { spawn as spawn3 } from "child_process";
14186
15047
  import * as fs15 from "fs";
14187
- import * as path13 from "path";
15048
+ import * as path14 from "path";
14188
15049
  import { pathToFileURL } from "url";
14189
15050
  function parseStdout(text2, handler) {
14190
15051
  const stripped = text2.trim();
@@ -14385,7 +15246,7 @@ var RecordHandler = class {
14385
15246
  record.source = this.opts.source ?? null;
14386
15247
  if (Array.isArray(this.sink)) this.sink.push(record);
14387
15248
  else if (typeof this.sink === "string") {
14388
- fs15.mkdirSync(path13.dirname(this.sink), { recursive: true });
15249
+ fs15.mkdirSync(path14.dirname(this.sink), { recursive: true });
14389
15250
  fs15.appendFileSync(this.sink, JSON.stringify(record) + "\n");
14390
15251
  } else await this.sink(record);
14391
15252
  return allow();
@@ -14596,7 +15457,7 @@ async function importCallable(target, cwd) {
14596
15457
  const hash = target.lastIndexOf("#");
14597
15458
  const modulePath = target.slice(0, hash);
14598
15459
  const exportName = target.slice(hash + 1) || "default";
14599
- const resolved = modulePath.startsWith(".") || path13.isAbsolute(modulePath) ? pathToFileURL(path13.resolve(cwd, modulePath)).href : modulePath;
15460
+ const resolved = modulePath.startsWith(".") || path14.isAbsolute(modulePath) ? pathToFileURL(path14.resolve(cwd, modulePath)).href : modulePath;
14600
15461
  let module;
14601
15462
  try {
14602
15463
  module = await import(resolved);
@@ -14660,7 +15521,7 @@ async function buildHandler(spec, aliases, opts) {
14660
15521
 
14661
15522
  // src/hooks/live.ts
14662
15523
  import * as fs16 from "fs";
14663
- import * as path14 from "path";
15524
+ import * as path15 from "path";
14664
15525
  var USER_AGENT = "gateway-hooks/1 (+https://withgateway.ai)";
14665
15526
  var TAGS = /<(script|style)[^>]*>.*?<\/\1>/gis;
14666
15527
  var STRIP = /<[^>]+>/g;
@@ -14680,13 +15541,13 @@ var Pacer = class {
14680
15541
  /** Waits until the interval since the tool's last call has passed; resolves to the
14681
15542
  * seconds waited. `sleep`/`clock` are injectable for tests. */
14682
15543
  async wait(name, intervalMs, opts = {}) {
14683
- const sleep3 = opts.sleep ?? ((seconds) => new Promise((resolve11) => setTimeout(resolve11, seconds * 1e3)));
15544
+ const sleep5 = opts.sleep ?? ((seconds) => new Promise((resolve11) => setTimeout(resolve11, seconds * 1e3)));
14684
15545
  const clock = opts.clock ?? (() => Date.now() / 1e3);
14685
15546
  const now = clock();
14686
15547
  const due = (this.last.get(name) ?? -Infinity) + intervalMs / 1e3;
14687
15548
  const waited = Math.max(0, due - now);
14688
15549
  this.last.set(name, Math.max(now, due));
14689
- if (waited) await sleep3(waited);
15550
+ if (waited) await sleep5(waited);
14690
15551
  return Math.round(waited * 1e3) / 1e3;
14691
15552
  }
14692
15553
  };
@@ -15028,7 +15889,7 @@ var CallableTool = class _CallableTool {
15028
15889
  }
15029
15890
  };
15030
15891
  function readConnection(root) {
15031
- const file = path14.join(root, "connector.toml");
15892
+ const file = path15.join(root, "connector.toml");
15032
15893
  if (!fs16.existsSync(file)) throw new Error(`no connector.toml at ${root}`);
15033
15894
  const raw = parseToml(fs16.readFileSync(file, "utf8"));
15034
15895
  const connector = raw.connector ?? {};
@@ -15044,7 +15905,7 @@ function readConnection(root) {
15044
15905
  };
15045
15906
  }
15046
15907
  return {
15047
- slug: connector.slug ?? path14.basename(root),
15908
+ slug: connector.slug ?? path15.basename(root),
15048
15909
  baseUrl: (connector.base_url ?? "").replace(/\/+$/, ""),
15049
15910
  scheme: auth.scheme ?? "none",
15050
15911
  ...typeof auth.name === "string" ? { authName: auth.name } : {},
@@ -15061,8 +15922,8 @@ var ConnectorTool = class {
15061
15922
  this.spec = spec;
15062
15923
  this.live = live;
15063
15924
  let location = spec.options.connector;
15064
- if (!path14.isAbsolute(location)) location = path14.resolve(baseDir, location);
15065
- const root = fs16.existsSync(location) && fs16.statSync(location).isFile() ? path14.dirname(location) : location;
15925
+ if (!path15.isAbsolute(location)) location = path15.resolve(baseDir, location);
15926
+ const root = fs16.existsSync(location) && fs16.statSync(location).isFile() ? path15.dirname(location) : location;
15066
15927
  try {
15067
15928
  this.connection = readConnection(root);
15068
15929
  } catch (error) {
@@ -15219,7 +16080,7 @@ var Recorder = class {
15219
16080
  }
15220
16081
  file;
15221
16082
  write(line) {
15222
- fs16.mkdirSync(path14.dirname(this.file), { recursive: true });
16083
+ fs16.mkdirSync(path15.dirname(this.file), { recursive: true });
15223
16084
  fs16.appendFileSync(this.file, canonical2(line) + "\n");
15224
16085
  }
15225
16086
  };
@@ -15315,7 +16176,7 @@ var LiveTools = class _LiveTools {
15315
16176
  ...config.live.record ? { record: interpolatePath(config.live.record, run.runId, directory) } : {},
15316
16177
  ...config.live.replay ? { replay: interpolatePath(config.live.replay, run.runId, directory) } : {}
15317
16178
  };
15318
- return _LiveTools.create(run, live, path14.dirname(path14.resolve(file)));
16179
+ return _LiveTools.create(run, live, path15.dirname(path15.resolve(file)));
15319
16180
  }
15320
16181
  /** The declared tools for a model. `openai`: `{type: "function", function: {name,
15321
16182
  * description, parameters}}`; `anthropic`: `{name, description, input_schema}`. */
@@ -15642,7 +16503,7 @@ var RedactionAudit = class _RedactionAudit {
15642
16503
  // src/hooks/run.ts
15643
16504
  import { randomBytes as randomBytes3 } from "crypto";
15644
16505
  import * as fs17 from "fs";
15645
- import * as path15 from "path";
16506
+ import * as path16 from "path";
15646
16507
  var BLOCKING_EVENTS = [
15647
16508
  "pre_tool_call",
15648
16509
  "pre_request",
@@ -15693,12 +16554,12 @@ var HookRun = class _HookRun {
15693
16554
  declaredOnly: options.declaredOnly ?? config.aliasDeclaredOnly
15694
16555
  });
15695
16556
  this.cwd = options.cwd ?? process.cwd();
15696
- this.runDir = options.runDir ?? path15.join(this.cwd, "runs", this.runId);
16557
+ this.runDir = options.runDir ?? path16.join(this.cwd, "runs", this.runId);
15697
16558
  const chosen = options.legend !== void 0 ? options.legend : config.aliasLegend;
15698
- this.legendPath = chosen ? path15.resolve(this.cwd, interpolatePath(chosen, this.runId, this.runDir)) : null;
16559
+ this.legendPath = chosen ? path16.resolve(this.cwd, interpolatePath(chosen, this.runId, this.runDir)) : null;
15699
16560
  this.sessionId = options.session;
15700
16561
  this.source = options.source;
15701
- this.configDir = config.source ? path15.dirname(path15.resolve(config.source)) : this.cwd;
16562
+ this.configDir = config.source ? path16.dirname(path16.resolve(config.source)) : this.cwd;
15702
16563
  }
15703
16564
  /** Build from a parsed config (an object in the file's shape, or a `HookConfig`);
15704
16565
  * handlers are constructed now, so a callable that does not import fails here. */
@@ -16108,7 +16969,7 @@ var HookRun = class _HookRun {
16108
16969
  throw new Error(
16109
16970
  "no legend path: set [alias] legend, pass legend to the run, or give a path"
16110
16971
  );
16111
- fs17.mkdirSync(path15.dirname(target), { recursive: true });
16972
+ fs17.mkdirSync(path16.dirname(target), { recursive: true });
16112
16973
  fs17.writeFileSync(target, JSON.stringify(this.legend(), null, 2) + "\n");
16113
16974
  return target;
16114
16975
  }
@@ -16205,14 +17066,14 @@ var HookedSession = class {
16205
17066
  * `apiHeaders()`), `pre_request` before it and `post_response` after. Returns
16206
17067
  * `{status, body, headers}`; a 4xx/5xx is returned, not thrown, so the agent sees
16207
17068
  * the vendor's own error shape (rewritten like any result). */
16208
- async request(method, path16, opts = {}) {
17069
+ async request(method, path17, opts = {}) {
16209
17070
  if (!this.inner.api && !this.requestFn)
16210
17071
  throw new Error(
16211
17072
  "this session has no api surface (pass requestFn for a transport of your own)"
16212
17073
  );
16213
17074
  let request = {
16214
17075
  method: method.toUpperCase(),
16215
- path: path16,
17076
+ path: path17,
16216
17077
  query: { ...opts.query ?? {} },
16217
17078
  body: opts.body ?? null
16218
17079
  };
@@ -16684,6 +17545,8 @@ export {
16684
17545
  GatewaySecurityError,
16685
17546
  GatewayToolApprovalRequiredError,
16686
17547
  GatewayToolBlockedError,
17548
+ GradePayload,
17549
+ GradeRefused,
16687
17550
  HostedDryRunClient,
16688
17551
  IDLE_WARNING_MINUTES,
16689
17552
  KEY_NAMES,
@@ -16709,6 +17572,7 @@ export {
16709
17572
  ReplayReport,
16710
17573
  ReplaySuite,
16711
17574
  ReplayTask,
17575
+ RetryPolicy,
16712
17576
  SANDBOX_REQUEST_TIMEOUT_MS,
16713
17577
  SECRET_KEY,
16714
17578
  SESSION_STATES,
@@ -16718,6 +17582,7 @@ export {
16718
17582
  SUCCESS_METRIC_NAME_KEY,
16719
17583
  SUCCESS_SIGNAL_KEY,
16720
17584
  SURFACE_KINDS,
17585
+ Score,
16721
17586
  ScoreError,
16722
17587
  ScoresClient,
16723
17588
  Screen,
@@ -16726,6 +17591,7 @@ export {
16726
17591
  SessionRecording,
16727
17592
  SessionTrace,
16728
17593
  StubMissError,
17594
+ TOOL_CONTRACT_UNREAD,
16729
17595
  TRANSFORMS,
16730
17596
  TaskEditClient,
16731
17597
  TaskRun,
@@ -16741,13 +17607,17 @@ export {
16741
17607
  WorldBrowser,
16742
17608
  WorldCallTooLarge,
16743
17609
  WorldEditClient,
17610
+ WorldNotice,
17611
+ WorldRequestLog,
16744
17612
  WorldSandboxClient,
16745
17613
  WorldSession,
16746
17614
  WorldSessionCallError,
17615
+ WorldSessionOpenFailed,
16747
17616
  WorldSessionTimeout,
16748
17617
  WorldSet,
16749
17618
  WorldSurfaceUnavailable,
16750
17619
  WorldTask,
17620
+ WorldToolsUnread,
16751
17621
  addExperimentAttributes,
16752
17622
  aggregateTaskGrade,
16753
17623
  apiRequest,
@@ -16814,6 +17684,7 @@ export {
16814
17684
  loadSession,
16815
17685
  localTreeState,
16816
17686
  memory,
17687
+ messageOf,
16817
17688
  migrateProjectWorlds,
16818
17689
  migrateWorld,
16819
17690
  modelToolName,
@@ -16871,11 +17742,16 @@ export {
16871
17742
  setExperimentContext,
16872
17743
  shapeText,
16873
17744
  snapshotTreeHashes,
17745
+ sqlRows,
17746
+ stillStartingHint,
16874
17747
  storedForms,
16875
17748
  success,
16876
17749
  suite,
17750
+ surfaceDescriptor,
16877
17751
  surfaceKinds,
17752
+ taskGradeError,
16878
17753
  taskLabel,
17754
+ taskRunFailure,
16879
17755
  toolCallRecordsIn,
16880
17756
  toolCallsFromObservations,
16881
17757
  traces_exports as traces,