@withgateway/sdk 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. package/README.md +8 -6
  2. package/dist/cli.js +4372 -2265
  3. package/dist/cli.js.map +1 -1
  4. package/dist/cli.mjs +4362 -2256
  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 +259 -58
  9. package/dist/files/index.js.map +1 -1
  10. package/dist/files/index.mjs +249 -50
  11. package/dist/files/index.mjs.map +1 -1
  12. package/dist/hooks/index.d.mts +1 -1
  13. package/dist/hooks/index.d.ts +1 -1
  14. package/dist/hooks/index.js +124 -11
  15. package/dist/hooks/index.js.map +1 -1
  16. package/dist/hooks/index.mjs +124 -11
  17. package/dist/hooks/index.mjs.map +1 -1
  18. package/dist/{index-C3UzD0G3.d.mts → index-CDPQBsU5.d.mts} +19 -3
  19. package/dist/{index-C3UzD0G3.d.ts → index-CDPQBsU5.d.ts} +19 -3
  20. package/dist/{index-CBqbB_0J.d.ts → index-DUHmS74p.d.ts} +1 -1
  21. package/dist/{index-D-AMnAMa.d.mts → index-mJhG3vZU.d.mts} +1 -1
  22. package/dist/index.d.mts +5 -5
  23. package/dist/index.d.ts +5 -5
  24. package/dist/index.js +2240 -789
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +2150 -739
  27. package/dist/index.mjs.map +1 -1
  28. package/dist/{media-CbcAfQvd.d.mts → media-BIApiPzS.d.mts} +21 -2
  29. package/dist/{media-CbcAfQvd.d.ts → media-BIApiPzS.d.ts} +21 -2
  30. package/dist/traces/index.d.mts +1 -1
  31. package/dist/traces/index.d.ts +1 -1
  32. package/dist/traces/index.js +253 -52
  33. package/dist/traces/index.js.map +1 -1
  34. package/dist/traces/index.mjs +243 -44
  35. package/dist/traces/index.mjs.map +1 -1
  36. package/dist/tracing/index.d.mts +2 -2
  37. package/dist/tracing/index.d.ts +2 -2
  38. package/dist/tracing/index.js +75 -13
  39. package/dist/tracing/index.js.map +1 -1
  40. package/dist/tracing/index.mjs +75 -13
  41. package/dist/tracing/index.mjs.map +1 -1
  42. package/dist/worlds/index.d.mts +893 -482
  43. package/dist/worlds/index.d.ts +893 -482
  44. package/dist/worlds/index.js +1968 -669
  45. package/dist/worlds/index.js.map +1 -1
  46. package/dist/worlds/index.mjs +1960 -662
  47. package/dist/worlds/index.mjs.map +1 -1
  48. package/package.json +1 -1
  49. package/runtime/gatewaysdk/environment/_bundle.py +2 -27
  50. package/runtime/gatewaysdk/environment/_materialize.py +48 -88
  51. package/runtime/gatewaysdk/environment/_world.py +155 -237
  52. package/runtime/gatewaysdk/environment/backing.py +133 -0
  53. package/runtime/gatewaysdk/environment/core.py +2 -17
  54. package/runtime/gatewaysdk/environment/legacy_sqlite.py +288 -0
  55. package/runtime/gatewaysdk/environment/reward_keys.py +25 -0
  56. package/runtime/gatewaysdk/environment/runtime.py +8 -13
  57. package/runtime/gatewaysdk/environment/schema/__init__.py +4 -4
  58. package/runtime/gatewaysdk/environment/schema/admin_routes.py +86 -0
  59. package/runtime/gatewaysdk/environment/schema/admission.py +174 -0
  60. package/runtime/gatewaysdk/environment/schema/api.py +347 -892
  61. package/runtime/gatewaysdk/environment/schema/assertions.py +458 -0
  62. package/runtime/gatewaysdk/environment/schema/batch.py +44 -39
  63. package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +17 -5
  64. package/runtime/gatewaysdk/environment/schema/cache_layout.py +5 -0
  65. package/runtime/gatewaysdk/environment/schema/call_capture.py +309 -0
  66. package/runtime/gatewaysdk/environment/schema/call_log.py +503 -0
  67. package/runtime/gatewaysdk/environment/schema/call_redaction.py +526 -0
  68. package/runtime/gatewaysdk/environment/schema/cast.py +15 -35
  69. package/runtime/gatewaysdk/environment/schema/compiler.py +334 -158
  70. package/runtime/gatewaysdk/environment/schema/conform.py +14 -39
  71. package/runtime/gatewaysdk/environment/schema/connector.py +113 -211
  72. package/runtime/gatewaysdk/environment/schema/connectors/confluence/connector.toml +1 -12
  73. package/runtime/gatewaysdk/environment/schema/connectors/github/connector.toml +1 -0
  74. package/runtime/gatewaysdk/environment/schema/connectors/servicenow/connector.toml +2 -5
  75. package/runtime/gatewaysdk/environment/schema/cors.py +54 -0
  76. package/runtime/gatewaysdk/environment/schema/credentials.py +313 -8
  77. package/runtime/gatewaysdk/environment/schema/data.py +60 -216
  78. package/runtime/gatewaysdk/environment/schema/database/__init__.py +24 -0
  79. package/runtime/gatewaysdk/environment/schema/database/audit.py +121 -0
  80. package/runtime/gatewaysdk/environment/schema/database/base.py +936 -0
  81. package/runtime/gatewaysdk/environment/schema/database/budget.py +33 -0
  82. package/runtime/gatewaysdk/environment/schema/database/connections.py +101 -0
  83. package/runtime/gatewaysdk/environment/schema/database/engines.py +8 -0
  84. package/runtime/gatewaysdk/environment/schema/database/guard.py +215 -0
  85. package/runtime/gatewaysdk/environment/schema/database/ingest.py +154 -0
  86. package/runtime/gatewaysdk/environment/schema/database/matching.py +58 -0
  87. package/runtime/gatewaysdk/environment/schema/database/provenance.py +30 -0
  88. package/runtime/gatewaysdk/environment/schema/database/reads.py +169 -0
  89. package/runtime/gatewaysdk/environment/schema/database/rows.py +53 -0
  90. package/runtime/gatewaysdk/environment/schema/database/runtime_rows.py +39 -0
  91. package/runtime/gatewaysdk/environment/schema/database/sqlite.py +393 -0
  92. package/runtime/gatewaysdk/environment/schema/declared_routes.py +210 -0
  93. package/runtime/gatewaysdk/environment/schema/domain_migration.py +236 -0
  94. package/runtime/gatewaysdk/environment/schema/door.py +899 -0
  95. package/runtime/gatewaysdk/environment/schema/envelope.py +227 -0
  96. package/runtime/gatewaysdk/environment/schema/extract.py +6 -13
  97. package/runtime/gatewaysdk/environment/schema/grading.py +90 -103
  98. package/runtime/gatewaysdk/environment/schema/handler_errors.py +163 -12
  99. package/runtime/gatewaysdk/environment/schema/host.py +237 -1795
  100. package/runtime/gatewaysdk/environment/schema/host_control.py +144 -0
  101. package/runtime/gatewaysdk/environment/schema/host_handoff.py +370 -0
  102. package/runtime/gatewaysdk/environment/schema/host_loops.py +97 -0
  103. package/runtime/gatewaysdk/environment/schema/host_owners.py +121 -0
  104. package/runtime/gatewaysdk/environment/schema/host_proxy.py +111 -0
  105. package/runtime/gatewaysdk/environment/schema/host_requests.py +6 -0
  106. package/runtime/gatewaysdk/environment/schema/host_session.py +126 -0
  107. package/runtime/gatewaysdk/environment/schema/host_surfaces.py +79 -21
  108. package/runtime/gatewaysdk/environment/schema/host_transfers.py +127 -0
  109. package/runtime/gatewaysdk/environment/schema/host_versions.py +242 -0
  110. package/runtime/gatewaysdk/environment/schema/host_workers.py +577 -0
  111. package/runtime/gatewaysdk/environment/schema/journal.py +437 -0
  112. package/runtime/gatewaysdk/environment/schema/lanes.py +53 -0
  113. package/runtime/gatewaysdk/environment/schema/ledger_spill.py +828 -0
  114. package/runtime/gatewaysdk/environment/schema/local.py +58 -170
  115. package/runtime/gatewaysdk/environment/schema/mapping.py +5 -25
  116. package/runtime/gatewaysdk/environment/schema/openapi_import.py +112 -73
  117. package/runtime/gatewaysdk/environment/schema/openapi_paging.py +147 -0
  118. package/runtime/gatewaysdk/environment/schema/openapi_payloads.py +288 -0
  119. package/runtime/gatewaysdk/environment/schema/openapi_routes.py +97 -102
  120. package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +113 -30
  121. package/runtime/gatewaysdk/environment/schema/platform.py +137 -187
  122. package/runtime/gatewaysdk/environment/schema/platform_import.py +2 -3
  123. package/runtime/gatewaysdk/environment/schema/platform_query.py +61 -5
  124. package/runtime/gatewaysdk/environment/schema/primary_key.py +61 -0
  125. package/runtime/gatewaysdk/environment/schema/proof.py +19 -12
  126. package/runtime/gatewaysdk/environment/schema/request_body.py +238 -0
  127. package/runtime/gatewaysdk/environment/schema/request_ledger.py +958 -0
  128. package/runtime/gatewaysdk/environment/schema/route_bindings.py +25 -3
  129. package/runtime/gatewaysdk/environment/schema/route_miss.py +54 -0
  130. package/runtime/gatewaysdk/environment/schema/runtime.py +178 -308
  131. package/runtime/gatewaysdk/environment/schema/served_names.py +58 -0
  132. package/runtime/gatewaysdk/environment/schema/session_files.py +275 -0
  133. package/runtime/gatewaysdk/environment/schema/session_move.py +275 -0
  134. package/runtime/gatewaysdk/environment/schema/skills/agents/connector-template-author.md +5 -1
  135. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +10 -11
  136. package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +49 -16
  137. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +39 -9
  138. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +27 -27
  139. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +33 -33
  140. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +13 -7
  141. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +49 -1
  142. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/journal-checks.md +182 -0
  143. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/openapi-import.md +50 -21
  144. package/runtime/gatewaysdk/environment/schema/slack.py +4 -8
  145. package/runtime/gatewaysdk/environment/schema/snapshot.py +516 -140
  146. package/runtime/gatewaysdk/environment/schema/spill_files.py +228 -0
  147. package/runtime/gatewaysdk/environment/schema/store_contract.py +8 -14
  148. package/runtime/gatewaysdk/environment/schema/tasks.py +267 -276
  149. package/runtime/gatewaysdk/environment/schema/tools_world.py +146 -242
  150. package/runtime/gatewaysdk/environment/schema/ui.py +24 -5
  151. package/runtime/gatewaysdk/environment/schema/ui_static/app.js +100 -130
  152. package/runtime/gatewaysdk/environment/schema/ui_static/index.html +1 -8
  153. package/runtime/gatewaysdk/environment/schema/validation.py +235 -41
  154. package/runtime/gatewaysdk/environment/schema/verbs.py +32 -36
  155. package/runtime/gatewaysdk/environment/schema/worker.py +6 -4
  156. package/runtime/gatewaysdk/environment/schema/workers.py +345 -96
  157. package/runtime/gatewaysdk/environment/schema/world_host.py +953 -0
  158. package/runtime/gatewaysdk/environment/schema/world_sql.py +81 -0
  159. package/runtime/gatewaysdk/environment/schema/world_tests.py +80 -50
  160. package/runtime/gatewaysdk/environment/score.py +57 -0
  161. package/runtime/gatewaysdk/environment/secret_values.py +36 -0
  162. package/runtime/gatewaysdk/environment/world.py +43 -4
  163. package/runtime/gatewaysdk/environment/world_session_contract.py +46 -5
  164. package/skills/agents/connector-template-author.md +5 -1
  165. package/skills/benchmarks-getting-started/SKILL.md +10 -11
  166. package/skills/connector-schema-authoring/SKILL.md +49 -16
  167. package/skills/connector-schema-authoring/connector-worlds.md +142 -11
  168. package/skills/connector-schema-authoring/world-schema.md +103 -51
  169. package/skills/world-data-ingestion/ROWS.md +39 -9
  170. package/skills/world-data-ingestion/SKILL.md +27 -27
  171. package/skills/worlds-getting-started/SKILL.md +33 -33
  172. package/skills/worlds-getting-started/references/cli-mcp-map.md +13 -7
  173. package/skills/worlds-getting-started/references/gotchas.md +49 -1
  174. package/skills/worlds-getting-started/references/journal-checks.md +182 -0
  175. package/skills/worlds-getting-started/references/openapi-import.md +50 -21
  176. package/runtime/gatewaysdk/environment/schema/store.py +0 -1013
@@ -42,6 +42,28 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
42
42
  ));
43
43
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
44
44
 
45
+ // src/worlds/platformEndpoint.ts
46
+ function platformEndpoint(options) {
47
+ const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
48
+ const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
49
+ if (!publicKey || !secretKey) return null;
50
+ const host = options.host ?? process.env.GATEWAY_HOST ?? DEFAULT_HOST;
51
+ return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
52
+ }
53
+ function requirePlatformEndpoint(options) {
54
+ const endpoint = platformEndpoint(options);
55
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
56
+ return endpoint;
57
+ }
58
+ var DEFAULT_HOST, GATEWAY_KEYS_MISSING;
59
+ var init_platformEndpoint = __esm({
60
+ "src/worlds/platformEndpoint.ts"() {
61
+ "use strict";
62
+ DEFAULT_HOST = "https://withgateway.ai";
63
+ GATEWAY_KEYS_MISSING = "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).";
64
+ }
65
+ });
66
+
45
67
  // src/worlds/transport.ts
46
68
  function platformTimeoutMs(env = process.env) {
47
69
  const raw = env[PLATFORM_TIMEOUT_ENV];
@@ -50,7 +72,7 @@ function platformTimeoutMs(env = process.env) {
50
72
  if (Number.isInteger(value) && value > 0) return value;
51
73
  throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
52
74
  }
53
- var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, PlatformTransport;
75
+ var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, FETCH_LIMIT, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, sleep, RetryPolicy, Attempt, PlatformTransport;
54
76
  var init_transport = __esm({
55
77
  "src/worlds/transport.ts"() {
56
78
  "use strict";
@@ -58,7 +80,13 @@ var init_transport = __esm({
58
80
  PLATFORM_TIMEOUT_DEFAULT_MS = 12e4;
59
81
  CHECK_HOST = "Check GATEWAY_HOST (or --host) points at a running platform, then retry.";
60
82
  RETRY = "Retry; if it repeats, check the network between here and the platform.";
83
+ FETCH_LIMIT = {
84
+ reason: "Node's fetch stopped waiting after 300 s",
85
+ next: "The platform may still be running it: check what it changed before you retry."
86
+ };
61
87
  CAUSES = {
88
+ UND_ERR_HEADERS_TIMEOUT: FETCH_LIMIT,
89
+ UND_ERR_BODY_TIMEOUT: FETCH_LIMIT,
62
90
  ECONNREFUSED: { reason: "connection refused", next: CHECK_HOST },
63
91
  ENOTFOUND: { reason: "DNS lookup failed", next: CHECK_HOST },
64
92
  EAI_AGAIN: { reason: "DNS lookup failed", next: CHECK_HOST },
@@ -112,6 +140,56 @@ var init_transport = __esm({
112
140
  route;
113
141
  failure;
114
142
  };
143
+ sleep = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
144
+ RetryPolicy = class _RetryPolicy {
145
+ constructor(delaysMs, wait = sleep, budgetMs = Number.POSITIVE_INFINITY, now = Date.now) {
146
+ this.delaysMs = delaysMs;
147
+ this.wait = wait;
148
+ this.budgetMs = budgetMs;
149
+ this.now = now;
150
+ }
151
+ delaysMs;
152
+ wait;
153
+ budgetMs;
154
+ now;
155
+ static NONE = new _RetryPolicy([]);
156
+ /** A platform restart or a short edge outage: no wait starts that would end past five minutes, so a step ends
157
+ * within the budget plus one request timeout. */
158
+ static FILING = new _RetryPolicy([250, 500, 1e3, 2e3, 4e3, 8e3, 16e3, 3e4], sleep, 3e5);
159
+ static TRANSIENT_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
160
+ async run(attempt) {
161
+ const started = this.now();
162
+ for (let tries = 0; ; tries += 1) {
163
+ const outcome = await Attempt.of(attempt);
164
+ const delayMs = this.delaysMs[tries];
165
+ const spent = delayMs === void 0 || this.now() - started + delayMs >= this.budgetMs;
166
+ if (!outcome.transient || spent) return outcome.settle();
167
+ await this.wait(delayMs);
168
+ }
169
+ }
170
+ };
171
+ Attempt = class _Attempt {
172
+ constructor(reply, error, transient) {
173
+ this.reply = reply;
174
+ this.error = error;
175
+ this.transient = transient;
176
+ }
177
+ reply;
178
+ error;
179
+ transient;
180
+ static async of(attempt) {
181
+ try {
182
+ const reply = await attempt();
183
+ return new _Attempt(reply, null, RetryPolicy.TRANSIENT_STATUSES.has(reply.status));
184
+ } catch (error) {
185
+ return new _Attempt(null, error, error instanceof UnreachableError);
186
+ }
187
+ }
188
+ settle() {
189
+ if (this.reply) return this.reply;
190
+ throw this.error;
191
+ }
192
+ };
115
193
  PlatformTransport = class {
116
194
  constructor(endpoint, timeoutMs = platformTimeoutMs()) {
117
195
  this.endpoint = endpoint;
@@ -125,6 +203,9 @@ var init_transport = __esm({
125
203
  }
126
204
  async send(method, route, body, opts = {}) {
127
205
  const timeoutMs = this.budget(opts.timeoutMs);
206
+ return (opts.retry ?? RetryPolicy.NONE).run(() => this.once(method, route, body, timeoutMs));
207
+ }
208
+ async once(method, route, body, timeoutMs) {
128
209
  const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
129
210
  const controller = new AbortController();
130
211
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -305,6 +386,12 @@ var init_browserReconnect = __esm({
305
386
  });
306
387
 
307
388
  // src/worlds/browser.ts
389
+ function assertSessionAuthMode(value) {
390
+ if (SESSION_AUTH_MODES.includes(value)) return;
391
+ throw new Error(
392
+ `Unknown session auth: ${String(value)}; choose from ${SESSION_AUTH_MODES.join(", ")}`
393
+ );
394
+ }
308
395
  function assertBrowserTier(value) {
309
396
  if (BROWSER_TIERS.includes(value)) return;
310
397
  throw new Error(
@@ -318,9 +405,9 @@ function surfaceKinds(s) {
318
405
  if (s.browser) kinds.push("browser");
319
406
  return kinds;
320
407
  }
321
- function uiUrl(ui, path12) {
408
+ function uiUrl(ui, path13) {
322
409
  const base = ui.url.replace(/\/+$/, "");
323
- const rel = path12.startsWith("/") ? path12 : `/${path12}`;
410
+ const rel = path13.startsWith("/") ? path13 : `/${path13}`;
324
411
  if (!ui.token) return base + rel;
325
412
  const [beforeHash, hash] = splitHash(rel);
326
413
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -341,6 +428,15 @@ function splitHash(url) {
341
428
  const h = url.indexOf("#");
342
429
  return h < 0 ? [url, ""] : [url.slice(0, h), url.slice(h)];
343
430
  }
431
+ function capabilityLabel(url) {
432
+ let host = "";
433
+ try {
434
+ host = url ? new URL(url).hostname : "";
435
+ } catch {
436
+ return null;
437
+ }
438
+ return CAPABILITY_HOST.exec(host)?.[1] ?? null;
439
+ }
344
440
  function redactActionArgs(action, args) {
345
441
  const secretField = action === "browser_type" && SECRET_KEY.test(String(args.selector ?? ""));
346
442
  const out = {};
@@ -400,7 +496,9 @@ async function openBrowser(surfaces, opts = {}) {
400
496
  };
401
497
  const scrubber = new SecretScrubber([
402
498
  surfaces.browser?.wsUrl,
403
- surfaces.ui?.token
499
+ surfaces.ui?.token,
500
+ capabilityLabel(surfaces.ui?.url),
501
+ capabilityLabel(surfaces.api?.url)
404
502
  ]);
405
503
  const reconnector = mode === "hosted" && surfaces.browser.protocol === "cdp" && opts.repair ? new BrowserReconnector(opts.repair, reattach, {
406
504
  scrub: (text2) => scrubber.scrub(text2)
@@ -465,13 +563,15 @@ function computerUseToolkit(browser, opts = {}) {
465
563
  specs
466
564
  };
467
565
  }
468
- var SURFACE_KINDS, BROWSER_TIERS, DEFAULT_VIEWPORT, SETTLE_TIMEOUT_MS, SETTLE_QUIET_MS, SETTLE_POLL_MS, EVENT_BATCH_SIZE, EVENT_FLUSH_INTERVAL_MS, EVENT_MAX_RETRIES, EVENT_SCREENSHOT_QUALITY, EVENT_MAX_STRING, SECRET_KEY, SECRET_VALUE, WorldSurfaceUnavailable, UI_TOKEN_QUERY, BrowserActionRecorder, WorldBrowser, COMPUTER_USE_SPECS, str;
566
+ var SURFACE_KINDS, PLATFORM_SESSION_HEADER, SESSION_AUTH_MODES, BROWSER_TIERS, DEFAULT_VIEWPORT, SETTLE_TIMEOUT_MS, SETTLE_QUIET_MS, SETTLE_POLL_MS, EVENT_BATCH_SIZE, EVENT_FLUSH_INTERVAL_MS, EVENT_MAX_RETRIES, EVENT_SCREENSHOT_QUALITY, EVENT_MAX_STRING, SECRET_KEY, SECRET_VALUE, WorldSurfaceUnavailable, UI_TOKEN_QUERY, CAPABILITY_HOST, BrowserActionRecorder, WorldBrowser, COMPUTER_USE_SPECS, str;
469
567
  var init_browser = __esm({
470
568
  "src/worlds/browser.ts"() {
471
569
  "use strict";
472
570
  init_session2();
473
571
  init_browserReconnect();
474
572
  SURFACE_KINDS = ["tools", "api", "ui", "browser"];
573
+ PLATFORM_SESSION_HEADER = "X-World-Session";
574
+ SESSION_AUTH_MODES = ["vendor", "open"];
475
575
  BROWSER_TIERS = ["standard", "premium"];
476
576
  DEFAULT_VIEWPORT = { width: 1280, height: 800 };
477
577
  SETTLE_TIMEOUT_MS = 5e3;
@@ -492,6 +592,7 @@ var init_browser = __esm({
492
592
  surface;
493
593
  };
494
594
  UI_TOKEN_QUERY = "token";
595
+ CAPABILITY_HOST = /^o-[a-z0-9]+-([a-z2-7]{26})(?:-api)?\./;
495
596
  BrowserActionRecorder = class {
496
597
  constructor(post, agentId, opts = {}) {
497
598
  this.post = post;
@@ -561,7 +662,7 @@ var init_browser = __esm({
561
662
  }
562
663
  async send(batch) {
563
664
  const retries = this.opts.maxRetries ?? EVENT_MAX_RETRIES;
564
- const sleep2 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
665
+ const sleep4 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
565
666
  for (let attempt = 0; attempt < retries; attempt++) {
566
667
  try {
567
668
  const base = await this.resolveBase();
@@ -578,13 +679,16 @@ var init_browser = __esm({
578
679
  );
579
680
  return;
580
681
  }
581
- await sleep2(500 * 2 ** attempt);
682
+ await sleep4(500 * 2 ** attempt);
582
683
  }
583
684
  }
584
685
  }
585
686
  };
586
687
  WorldBrowser = class {
587
- constructor(page, ui, mode, teardown, viewport = DEFAULT_VIEWPORT, recorder = null, reconnector = null, scrubber = new SecretScrubber([ui?.token])) {
688
+ constructor(page, ui, mode, teardown, viewport = DEFAULT_VIEWPORT, recorder = null, reconnector = null, scrubber = new SecretScrubber([
689
+ ui?.token,
690
+ capabilityLabel(ui?.url)
691
+ ])) {
588
692
  this.ui = ui;
589
693
  this.mode = mode;
590
694
  this.teardown = teardown;
@@ -721,7 +825,15 @@ var init_browser = __esm({
721
825
  seq: recorder.nextSeq(),
722
826
  ts: (/* @__PURE__ */ new Date()).toISOString(),
723
827
  action,
724
- args: redactActionArgs(action, args),
828
+ args: redactActionArgs(
829
+ action,
830
+ Object.fromEntries(
831
+ Object.entries(args).map(([key, value]) => [
832
+ key,
833
+ typeof value === "string" ? this.scrubber.scrub(value) : value
834
+ ])
835
+ )
836
+ ),
725
837
  ok: failure === null,
726
838
  error,
727
839
  durationMs: Date.now() - started,
@@ -737,8 +849,8 @@ var init_browser = __esm({
737
849
  screenshot: null
738
850
  };
739
851
  try {
740
- out.url = this.page.url();
741
- out.title = await this.page.title();
852
+ out.url = this.scrubber.scrub(this.page.url());
853
+ out.title = this.scrubber.scrub(await this.page.title());
742
854
  const jpeg = await this.page.screenshot({
743
855
  type: "jpeg",
744
856
  quality: EVENT_SCREENSHOT_QUALITY
@@ -1215,13 +1327,13 @@ var init_screen = __esm({
1215
1327
  });
1216
1328
  }
1217
1329
  async drag(action) {
1218
- const path12 = action.args.path ?? [];
1219
- if (path12.length < 2) throw new Error("a drag needs at least two points");
1330
+ const path13 = action.args.path ?? [];
1331
+ if (path13.length < 2) throw new Error("a drag needs at least two points");
1220
1332
  return this.held(action.args.modifiers ?? [], async () => {
1221
- await this.moveTo(path12[0]);
1333
+ await this.moveTo(path13[0]);
1222
1334
  await this.page.mouse.down({ button: "left" });
1223
1335
  try {
1224
- for (const point2 of path12.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1336
+ for (const point2 of path13.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1225
1337
  } finally {
1226
1338
  await this.page.mouse.up({ button: "left" });
1227
1339
  }
@@ -1312,7 +1424,7 @@ var init_media = __esm({
1312
1424
  put;
1313
1425
  maxBytes;
1314
1426
  /** The uploader for `GATEWAY_HOST` / `GATEWAY_PUBLIC_KEY` /
1315
- * `GATEWAY_SECRET_KEY` (the keys tracing exports with); null when any is unset. */
1427
+ * `GATEWAY_SECRET_KEY` only; null when any is unset, so a run traced with a saved login uploads none. */
1316
1428
  static fromEnv(env = process.env) {
1317
1429
  const host = env.GATEWAY_HOST;
1318
1430
  const publicKey = env.GATEWAY_PUBLIC_KEY;
@@ -2182,13 +2294,16 @@ function idleWarning(idleExpiresAt, now = /* @__PURE__ */ new Date()) {
2182
2294
  const minutes = Math.max(0, Math.floor(leftMs / 6e4));
2183
2295
  return `warning: session expires in ${minutes}m (idle TTL); call a tool to extend it or \`gateway worlds session pin\` to keep it`;
2184
2296
  }
2185
- var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge;
2297
+ var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, REQUEST_LOG_PAGE_MAX, REQUEST_BODY_CAPTURE_BYTES, REQUESTS_NOT_RECORDED, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge, stillStartingHint;
2186
2298
  var init_sessionContract = __esm({
2187
2299
  "src/worlds/sessionContract.ts"() {
2188
2300
  "use strict";
2189
2301
  CALL_BODY_LIMIT_BYTES = 4 * 1024 * 1024;
2190
2302
  CALL_BODY_LIMIT_LABEL = "4mb";
2191
2303
  IDLE_WARNING_MINUTES = 5;
2304
+ REQUEST_LOG_PAGE_MAX = 200;
2305
+ REQUEST_BODY_CAPTURE_BYTES = 64 * 1024;
2306
+ REQUESTS_NOT_RECORDED = "requests_not_recorded";
2192
2307
  EXPLORATORY_TASK = Object.freeze({
2193
2308
  instruction: "exploratory session"
2194
2309
  });
@@ -2218,6 +2333,276 @@ var init_sessionContract = __esm({
2218
2333
  };
2219
2334
  }
2220
2335
  };
2336
+ stillStartingHint = (sessionId) => `It keeps opening: poll \`gateway worlds session status ${sessionId}\` until ready, or end it with \`gateway worlds session close ${sessionId}\`.`;
2337
+ }
2338
+ });
2339
+
2340
+ // src/worlds/toolSchema.ts
2341
+ function schemaFromSignature(spec) {
2342
+ if (spec.inputSchema)
2343
+ return {
2344
+ type: "function",
2345
+ function: {
2346
+ name: modelToolName(spec.name),
2347
+ description: spec.doc ?? spec.name,
2348
+ parameters: spec.inputSchema
2349
+ }
2350
+ };
2351
+ const inside = /\(([\s\S]*)\)\s*$/.exec(spec.signature.trim())?.[1] ?? "";
2352
+ const properties = {};
2353
+ const required2 = [];
2354
+ const params = [];
2355
+ let depth = 0;
2356
+ let current = "";
2357
+ for (const ch of inside) {
2358
+ if ("([{".includes(ch)) depth++;
2359
+ if (")]}".includes(ch)) depth--;
2360
+ if (ch === "," && depth === 0) {
2361
+ params.push(current);
2362
+ current = "";
2363
+ } else current += ch;
2364
+ }
2365
+ if (current.trim()) params.push(current);
2366
+ for (const raw of params) {
2367
+ const param = raw.trim();
2368
+ if (!param || param.startsWith("*") || param === "self") continue;
2369
+ const eq = param.indexOf("=");
2370
+ const name = (eq === -1 ? param : param.slice(0, eq)).split(":")[0].trim();
2371
+ if (!name) continue;
2372
+ if (eq === -1) {
2373
+ properties[name] = { type: "string" };
2374
+ required2.push(name);
2375
+ continue;
2376
+ }
2377
+ const value = param.slice(eq + 1).trim();
2378
+ if (/^-?\d+(\.\d+)?$/.test(value)) properties[name] = { type: "number" };
2379
+ else if (value === "True" || value === "False")
2380
+ properties[name] = { type: "boolean" };
2381
+ else properties[name] = { type: "string" };
2382
+ }
2383
+ return {
2384
+ type: "function",
2385
+ function: {
2386
+ name: modelToolName(spec.name),
2387
+ description: spec.doc ?? spec.name,
2388
+ parameters: { type: "object", properties, required: required2 }
2389
+ }
2390
+ };
2391
+ }
2392
+ var modelToolName;
2393
+ var init_toolSchema = __esm({
2394
+ "src/worlds/toolSchema.ts"() {
2395
+ "use strict";
2396
+ modelToolName = (name) => name.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64) || "tool";
2397
+ }
2398
+ });
2399
+
2400
+ // src/worlds/requestLedger.ts
2401
+ function platformMessage(text2) {
2402
+ try {
2403
+ const { message } = JSON.parse(text2);
2404
+ return typeof message === "string" ? message : null;
2405
+ } catch {
2406
+ return null;
2407
+ }
2408
+ }
2409
+ var WORLD_REQUEST_VIAS, WorldRequestFilterError, NOT_RECORDED, WorldRequestsNotRecorded, STATUS, INSTANT, WORLDS, SESSIONS, Instant, WorldRequestQuery, WorldRequestLedger;
2410
+ var init_requestLedger = __esm({
2411
+ "src/worlds/requestLedger.ts"() {
2412
+ "use strict";
2413
+ init_worlds();
2414
+ init_sessionContract();
2415
+ WORLD_REQUEST_VIAS = ["api", "ui", "tool"];
2416
+ WorldRequestFilterError = class extends Error {
2417
+ };
2418
+ NOT_RECORDED = "This session's engine records no requests; only hosted World Host sessions do.";
2419
+ WorldRequestsNotRecorded = class _WorldRequestsNotRecorded extends Error {
2420
+ static from(error) {
2421
+ if (!(error instanceof ApiError) || error.code !== REQUESTS_NOT_RECORDED) return error;
2422
+ return new _WorldRequestsNotRecorded(platformMessage(error.text) || NOT_RECORDED);
2423
+ }
2424
+ static async mapped(read) {
2425
+ try {
2426
+ return await read;
2427
+ } catch (error) {
2428
+ throw _WorldRequestsNotRecorded.from(error);
2429
+ }
2430
+ }
2431
+ };
2432
+ STATUS = /^(?:[1-5]\d\d|[1-5]xx)$/;
2433
+ INSTANT = /^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?(Z|[+-]\d{2}:\d{2})?)?$/;
2434
+ WORLDS = "/api/public/worlds";
2435
+ SESSIONS = "/api/public/world-sessions";
2436
+ Instant = class _Instant {
2437
+ static parse(flag, raw) {
2438
+ const match = INSTANT.exec(raw);
2439
+ const millis = match ? _Instant.fromParts(match) : null;
2440
+ if (millis === null)
2441
+ throw new WorldRequestFilterError(
2442
+ `--${flag} must be an ISO 8601 date or time (2026-10-05, 2026-10-05T14:30:00Z), not \`${raw}\`.`
2443
+ );
2444
+ return millis;
2445
+ }
2446
+ static fromParts(match) {
2447
+ const [year, month, day, hour, minute, second] = [1, 2, 3, 4, 5, 6].map((i) => Number(match[i] ?? 0));
2448
+ const offset = _Instant.offsetMinutes(match[8]);
2449
+ if (offset === null || year < 1 || hour > 23 || minute > 59 || second > 59) return null;
2450
+ const date = /* @__PURE__ */ new Date(0);
2451
+ date.setUTCFullYear(year, month - 1, day);
2452
+ if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) return null;
2453
+ date.setUTCHours(hour, minute, second, Number((match[7] ?? "").padEnd(3, "0")));
2454
+ const utc = date.getTime() - offset * 6e4;
2455
+ const utcYear = new Date(utc).getUTCFullYear();
2456
+ return utcYear < 1 || utcYear > 9999 ? null : utc;
2457
+ }
2458
+ static offsetMinutes(zone) {
2459
+ if (zone === void 0 || zone === "Z") return 0;
2460
+ const hours = Number(zone.slice(1, 3));
2461
+ const minutes = Number(zone.slice(4, 6));
2462
+ if (hours > 23 || minutes > 59) return null;
2463
+ return (zone.startsWith("-") ? -1 : 1) * (hours * 60 + minutes);
2464
+ }
2465
+ };
2466
+ WorldRequestQuery = class _WorldRequestQuery {
2467
+ constructor(entries) {
2468
+ this.entries = entries;
2469
+ }
2470
+ entries;
2471
+ static parse(filter = {}) {
2472
+ const via = _WorldRequestQuery.via(filter.via);
2473
+ const status = _WorldRequestQuery.status(filter.status);
2474
+ const since = filter.since === void 0 ? void 0 : Instant.parse("since", filter.since);
2475
+ const until = filter.until === void 0 ? void 0 : Instant.parse("until", filter.until);
2476
+ if (since !== void 0 && until !== void 0 && since > until)
2477
+ throw new WorldRequestFilterError("--since must not be after --until.");
2478
+ const values = [
2479
+ ["session", filter.session],
2480
+ ["version", filter.version],
2481
+ ["via", via],
2482
+ ["operation", filter.operation],
2483
+ ["status", status],
2484
+ ["since", since === void 0 ? void 0 : new Date(since).toISOString()],
2485
+ ["until", until === void 0 ? void 0 : new Date(until).toISOString()],
2486
+ ["limit", filter.limit === void 0 ? void 0 : String(filter.limit)],
2487
+ ["cursor", filter.cursor]
2488
+ ];
2489
+ return new _WorldRequestQuery(values.filter((entry) => entry[1] !== void 0));
2490
+ }
2491
+ static via(value) {
2492
+ if (value === void 0) return void 0;
2493
+ const lower = value.toLowerCase();
2494
+ if (WORLD_REQUEST_VIAS.includes(lower)) return lower;
2495
+ throw new WorldRequestFilterError(`--via must be one of ${WORLD_REQUEST_VIAS.join(", ")}, not \`${value}\`.`);
2496
+ }
2497
+ static status(value) {
2498
+ if (value === void 0) return void 0;
2499
+ const lower = value.toLowerCase();
2500
+ if (STATUS.test(lower)) return lower;
2501
+ throw new WorldRequestFilterError(`--status must be a code (200) or a class (2xx), not \`${value}\`.`);
2502
+ }
2503
+ get filtered() {
2504
+ return this.entries.some(([key]) => key !== "limit");
2505
+ }
2506
+ params() {
2507
+ return this.entries.map(([key, value]) => [key, value]);
2508
+ }
2509
+ search() {
2510
+ return this.entries.length ? `?${new URLSearchParams(this.entries).toString()}` : "";
2511
+ }
2512
+ };
2513
+ WorldRequestLedger = class {
2514
+ constructor(options = {}) {
2515
+ this.options = options;
2516
+ }
2517
+ options;
2518
+ /** A page of the world's requests, newest first; `nextCursor` reads the page after it. Narrowed to a session
2519
+ * whose engine records none: `WorldRequestsNotRecorded`. */
2520
+ list(slug, query = WorldRequestQuery.parse()) {
2521
+ return WorldRequestsNotRecorded.mapped(
2522
+ apiRequest(
2523
+ this.options,
2524
+ "GET",
2525
+ `${WORLDS}/${encodeURIComponent(slug)}/requests${query.search()}`
2526
+ )
2527
+ );
2528
+ }
2529
+ /** One of the world's requests, with its headers and bodies. */
2530
+ get(slug, requestId) {
2531
+ return apiRequest(
2532
+ this.options,
2533
+ "GET",
2534
+ `${WORLDS}/${encodeURIComponent(slug)}/requests/${encodeURIComponent(requestId)}`
2535
+ );
2536
+ }
2537
+ /** A page of one session's requests, newest first; `WorldRequestsNotRecorded` when its engine records none. */
2538
+ forSession(sessionId, query = WorldRequestQuery.parse()) {
2539
+ return WorldRequestsNotRecorded.mapped(
2540
+ apiRequest(
2541
+ this.options,
2542
+ "GET",
2543
+ `${SESSIONS}/${encodeURIComponent(sessionId)}/requests${query.search()}`
2544
+ )
2545
+ );
2546
+ }
2547
+ };
2548
+ }
2549
+ });
2550
+
2551
+ // src/worlds/sessionReadiness.ts
2552
+ var READY_TIMEOUT_MS, READY_POLL_MS, sleep2, WorldSessionTimeout, WorldSessionCallError, WorldSessionOpenFailed, ENDED, SessionReadiness;
2553
+ var init_sessionReadiness = __esm({
2554
+ "src/worlds/sessionReadiness.ts"() {
2555
+ "use strict";
2556
+ READY_TIMEOUT_MS = 10 * 6e4;
2557
+ READY_POLL_MS = 2e3;
2558
+ sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
2559
+ WorldSessionTimeout = class extends Error {
2560
+ };
2561
+ WorldSessionCallError = class extends Error {
2562
+ };
2563
+ WorldSessionOpenFailed = class extends WorldSessionCallError {
2564
+ constructor(sessionId, state2, code, reason) {
2565
+ super(
2566
+ reason === null ? `World session ${sessionId} ${state2.toLowerCase()} before it was ready.` : `World session ${sessionId} failed to open: ${reason}${code ? ` (${code})` : ""}`
2567
+ );
2568
+ this.sessionId = sessionId;
2569
+ this.state = state2;
2570
+ this.code = code;
2571
+ this.name = "WorldSessionOpenFailed";
2572
+ }
2573
+ sessionId;
2574
+ state;
2575
+ code;
2576
+ };
2577
+ ENDED = /* @__PURE__ */ new Set(["FAILED", "STOPPED"]);
2578
+ SessionReadiness = class {
2579
+ constructor(sessionId, status) {
2580
+ this.sessionId = sessionId;
2581
+ this.status = status;
2582
+ }
2583
+ sessionId;
2584
+ status;
2585
+ async wait(timeoutMs = READY_TIMEOUT_MS) {
2586
+ const deadline = Date.now() + timeoutMs;
2587
+ for (; ; ) {
2588
+ const state2 = await this.status();
2589
+ if (state2.ready) return state2;
2590
+ if (ENDED.has(state2.state))
2591
+ throw new WorldSessionOpenFailed(
2592
+ this.sessionId,
2593
+ state2.state,
2594
+ state2.failure?.code ?? null,
2595
+ state2.failure?.message ?? null
2596
+ );
2597
+ const left = deadline - Date.now();
2598
+ if (left <= 0)
2599
+ throw new WorldSessionTimeout(
2600
+ `World session ${this.sessionId} still ${state2.state} after ${Math.round(timeoutMs / 1e3)}s.`
2601
+ );
2602
+ await sleep2(Math.min(READY_POLL_MS, left));
2603
+ }
2604
+ }
2605
+ };
2221
2606
  }
2222
2607
  });
2223
2608
 
@@ -2313,11 +2698,184 @@ var init_taskRef = __esm({
2313
2698
  }
2314
2699
  });
2315
2700
 
2701
+ // src/worlds/worldNotice.ts
2702
+ var TOOL_CONTRACT_UNREAD, DEPRECATION, NOTICE_KINDS, WorldNotice, WorldToolsUnread;
2703
+ var init_worldNotice = __esm({
2704
+ "src/worlds/worldNotice.ts"() {
2705
+ "use strict";
2706
+ TOOL_CONTRACT_UNREAD = "tool-contract-unread";
2707
+ DEPRECATION = { label: "deprecated", next: "Instead", refusesRun: false };
2708
+ NOTICE_KINDS = /* @__PURE__ */ new Map([
2709
+ [TOOL_CONTRACT_UNREAD, { label: "warning", next: "Then", refusesRun: true }]
2710
+ ]);
2711
+ WorldNotice = class _WorldNotice {
2712
+ constructor(notice, kind) {
2713
+ this.notice = notice;
2714
+ this.kind = kind;
2715
+ }
2716
+ notice;
2717
+ kind;
2718
+ static of(notice) {
2719
+ return new _WorldNotice(notice, NOTICE_KINDS.get(notice.code) ?? DEPRECATION);
2720
+ }
2721
+ get refusesRun() {
2722
+ return this.kind.refusesRun;
2723
+ }
2724
+ line() {
2725
+ const { code, message, replacement } = this.notice;
2726
+ return `${this.kind.label} (${code}): ${message} ${this.kind.next}: ${replacement}`;
2727
+ }
2728
+ };
2729
+ WorldToolsUnread = class _WorldToolsUnread extends Error {
2730
+ constructor(world, notice) {
2731
+ super(
2732
+ `refused before the agent ran, nothing filed: world ${world} has no readable tools: ${notice.notice.message} Then: ${notice.notice.replacement}`
2733
+ );
2734
+ this.world = world;
2735
+ this.notice = notice;
2736
+ this.name = "WorldToolsUnread";
2737
+ this.code = notice.notice.code;
2738
+ }
2739
+ world;
2740
+ notice;
2741
+ code;
2742
+ static refuse(world, notices) {
2743
+ const refusing = notices.map((notice) => WorldNotice.of(notice)).find((notice) => notice.refusesRun);
2744
+ if (refusing) throw new _WorldToolsUnread(world, refusing);
2745
+ }
2746
+ };
2747
+ }
2748
+ });
2749
+
2750
+ // src/worlds/grade.ts
2751
+ var messageOf, Score, GradeRefused, GradePayload;
2752
+ var init_grade = __esm({
2753
+ "src/worlds/grade.ts"() {
2754
+ "use strict";
2755
+ messageOf = (error) => (error instanceof Error ? error.message || error.name : String(error)) || "Error";
2756
+ Score = class _Score {
2757
+ static RULE = "a score is a number from 0 to 1";
2758
+ static admits(value) {
2759
+ return Number.isFinite(value) && value >= 0 && value <= 1;
2760
+ }
2761
+ /** Every number among `scores` that is not a score, as `'<key>' = <value>`
2762
+ * and the rule, or null when all of them are. Pairs, not a map, so that no
2763
+ * key hides another. */
2764
+ static problem(scores2) {
2765
+ const refused = [...scores2].flatMap(
2766
+ ([key, value]) => typeof value === "number" && !_Score.admits(value) ? [`'${key}' = ${String(value)}`] : []
2767
+ );
2768
+ return refused.length ? `${refused.join(", ")}; ${_Score.RULE}` : null;
2769
+ }
2770
+ };
2771
+ GradeRefused = class extends Error {
2772
+ name = "GradeRefused";
2773
+ };
2774
+ GradePayload = class _GradePayload {
2775
+ /** A grade payload's numbers, checked: every number in it must be a score,
2776
+ * and its rewards and verifiers objects by name, or the grade is refused; a
2777
+ * cross-world verifier's grade that breaks either rule is that verifier's
2778
+ * error, the way the World Host answers one that could not grade. */
2779
+ static read(raw) {
2780
+ if (!_GradePayload.objectOrAbsent(raw))
2781
+ throw new GradeRefused("the grade is not an object");
2782
+ const record = _GradePayload.record(raw);
2783
+ const problem = _GradePayload.problem(record) ?? _GradePayload.verifiersProblem(record.verifiers);
2784
+ if (problem) throw new GradeRefused(problem);
2785
+ const verifiers = _GradePayload.verifiers(record.verifiers);
2786
+ return {
2787
+ ..._GradePayload.numbers(record),
2788
+ ...typeof record.reason === "string" ? { reason: record.reason } : {},
2789
+ ...typeof record.error === "string" ? { error: record.error } : {},
2790
+ ...verifiers ? { verifiers } : {}
2791
+ };
2792
+ }
2793
+ /** A world whose grade call failed (the platform refused it, the host had
2794
+ * lost the session): that world's error, never a score. */
2795
+ static failedCall(error) {
2796
+ return { ..._GradePayload.failed(messageOf(error)), raw: null };
2797
+ }
2798
+ static problem(record) {
2799
+ if (!_GradePayload.objectOrAbsent(record.rewards))
2800
+ return "the grade's rewards are not an object";
2801
+ const scores2 = Score.problem(_GradePayload.scores(record));
2802
+ if (scores2) return `the grade answered ${scores2}`;
2803
+ const twice = _GradePayload.twice(record);
2804
+ return twice.length ? `the grade answered ${twice.map((key) => `'${key}'`).join(", ")} both beside and inside rewards` : null;
2805
+ }
2806
+ static twice(record) {
2807
+ return Object.entries(_GradePayload.record(record.rewards)).filter(
2808
+ ([key, value]) => key !== "reward" && typeof value === "number" && typeof record[key] === "number"
2809
+ ).map(([key]) => key);
2810
+ }
2811
+ static verifiersProblem(value) {
2812
+ return _GradePayload.objectOrAbsent(value) ? null : "the grade's verifiers are not an object";
2813
+ }
2814
+ static scores(record) {
2815
+ return [
2816
+ ["reward", record.reward],
2817
+ ...Object.entries(_GradePayload.record(record.rewards)),
2818
+ ...Object.entries(record).filter(
2819
+ ([key]) => key !== "reward" && key !== "rewards"
2820
+ )
2821
+ ];
2822
+ }
2823
+ static isRecord(value) {
2824
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2825
+ }
2826
+ static objectOrAbsent(value) {
2827
+ return value === void 0 || value === null || _GradePayload.isRecord(value);
2828
+ }
2829
+ static record(value) {
2830
+ return _GradePayload.isRecord(value) ? value : {};
2831
+ }
2832
+ /** `reward`, and every numeric key under `rewards` (the host's shape) or at
2833
+ * the top level (a bundled verifier's). */
2834
+ static numbers(record) {
2835
+ const rewards = {};
2836
+ for (const [key, value] of Object.entries(
2837
+ _GradePayload.record(record.rewards)
2838
+ ))
2839
+ if (typeof value === "number") rewards[key] = value;
2840
+ for (const [key, value] of Object.entries(record))
2841
+ if (key !== "reward" && typeof value === "number") rewards[key] = value;
2842
+ return {
2843
+ reward: typeof record.reward === "number" ? record.reward : null,
2844
+ rewards
2845
+ };
2846
+ }
2847
+ static verifiers(value) {
2848
+ if (!_GradePayload.isRecord(value)) return void 0;
2849
+ return Object.fromEntries(
2850
+ Object.entries(value).map(([name, grade]) => [
2851
+ name,
2852
+ _GradePayload.verifier(name, grade)
2853
+ ])
2854
+ );
2855
+ }
2856
+ static verifier(name, grade) {
2857
+ if (!_GradePayload.isRecord(grade))
2858
+ return _GradePayload.failed(`the grade's verifier '${name}' is not an object`);
2859
+ const problem = _GradePayload.problem(grade);
2860
+ if (problem) return _GradePayload.failed(problem);
2861
+ return {
2862
+ ..._GradePayload.numbers(grade),
2863
+ ...typeof grade.error === "string" ? { error: grade.error } : {}
2864
+ };
2865
+ }
2866
+ static failed(error) {
2867
+ return { reward: null, rewards: {}, error };
2868
+ }
2869
+ };
2870
+ }
2871
+ });
2872
+
2316
2873
  // src/worlds/session.ts
2317
2874
  var session_exports2 = {};
2318
2875
  __export(session_exports2, {
2319
2876
  WorldSession: () => WorldSession,
2320
2877
  WorldSessionCallError: () => WorldSessionCallError,
2878
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
2321
2879
  WorldSessionTimeout: () => WorldSessionTimeout,
2322
2880
  attachSession: () => attachSession,
2323
2881
  isTaskIdRef: () => isTaskIdRef,
@@ -2328,85 +2886,6 @@ __export(session_exports2, {
2328
2886
  schemaFromSignature: () => schemaFromSignature,
2329
2887
  taskLabel: () => taskLabel
2330
2888
  });
2331
- function numericGrade(record) {
2332
- const rewards = {};
2333
- const nested = record.rewards;
2334
- if (nested && typeof nested === "object") {
2335
- for (const [key, value] of Object.entries(nested)) {
2336
- if (typeof value === "number") rewards[key] = value;
2337
- }
2338
- }
2339
- for (const [key, value] of Object.entries(record)) {
2340
- if (key !== "reward" && typeof value === "number") rewards[key] = value;
2341
- }
2342
- return {
2343
- reward: typeof record.reward === "number" ? record.reward : null,
2344
- rewards
2345
- };
2346
- }
2347
- function verifierGrades(value) {
2348
- if (!value || typeof value !== "object") return void 0;
2349
- const out = {};
2350
- for (const [name, grade] of Object.entries(value)) {
2351
- const record = grade && typeof grade === "object" ? grade : {};
2352
- out[name] = {
2353
- ...numericGrade(record),
2354
- ...typeof record.error === "string" ? { error: record.error } : {}
2355
- };
2356
- }
2357
- return out;
2358
- }
2359
- function schemaFromSignature(spec) {
2360
- if (spec.inputSchema)
2361
- return {
2362
- type: "function",
2363
- function: {
2364
- name: modelToolName(spec.name),
2365
- description: spec.doc ?? spec.name,
2366
- parameters: spec.inputSchema
2367
- }
2368
- };
2369
- const inside = /\(([\s\S]*)\)\s*$/.exec(spec.signature.trim())?.[1] ?? "";
2370
- const properties = {};
2371
- const required2 = [];
2372
- const params = [];
2373
- let depth = 0;
2374
- let current = "";
2375
- for (const ch of inside) {
2376
- if ("([{".includes(ch)) depth++;
2377
- if (")]}".includes(ch)) depth--;
2378
- if (ch === "," && depth === 0) {
2379
- params.push(current);
2380
- current = "";
2381
- } else current += ch;
2382
- }
2383
- if (current.trim()) params.push(current);
2384
- for (const raw of params) {
2385
- const param = raw.trim();
2386
- if (!param || param.startsWith("*") || param === "self") continue;
2387
- const eq = param.indexOf("=");
2388
- const name = (eq === -1 ? param : param.slice(0, eq)).split(":")[0].trim();
2389
- if (!name) continue;
2390
- if (eq === -1) {
2391
- properties[name] = { type: "string" };
2392
- required2.push(name);
2393
- continue;
2394
- }
2395
- const value = param.slice(eq + 1).trim();
2396
- if (/^-?\d+(\.\d+)?$/.test(value)) properties[name] = { type: "number" };
2397
- else if (value === "True" || value === "False")
2398
- properties[name] = { type: "boolean" };
2399
- else properties[name] = { type: "string" };
2400
- }
2401
- return {
2402
- type: "function",
2403
- function: {
2404
- name: modelToolName(spec.name),
2405
- description: spec.doc ?? spec.name,
2406
- parameters: { type: "object", properties, required: required2 }
2407
- }
2408
- };
2409
- }
2410
2889
  async function attachSession(sessionId, options = {}) {
2411
2890
  const state2 = await apiRequest(
2412
2891
  options,
@@ -2427,6 +2906,7 @@ async function openSession(slugOrRef, taskOrOpts = {}, positionalOpts = {}) {
2427
2906
  `Unknown world surfaces: ${unknown.join(", ")}; choose from ${SURFACE_KINDS.join(", ")}`
2428
2907
  );
2429
2908
  if (opts.browserTier !== void 0) assertBrowserTier(opts.browserTier);
2909
+ if (opts.auth !== void 0) assertSessionAuthMode(opts.auth);
2430
2910
  const at = slugOrRef.indexOf("@");
2431
2911
  const slug = at === -1 ? slugOrRef : slugOrRef.slice(0, at);
2432
2912
  const ref = at === -1 ? "" : slugOrRef.slice(at + 1);
@@ -2439,7 +2919,8 @@ async function openSession(slugOrRef, taskOrOpts = {}, positionalOpts = {}) {
2439
2919
  ...task === void 0 ? {} : { task },
2440
2920
  ...opts.reuseWarm === void 0 ? {} : { reuseWarm: opts.reuseWarm },
2441
2921
  ...opts.surfaces?.length ? { surfaces: opts.surfaces } : {},
2442
- ...opts.browserTier === void 0 ? {} : { browserTier: opts.browserTier }
2922
+ ...opts.browserTier === void 0 ? {} : { browserTier: opts.browserTier },
2923
+ ...opts.auth === void 0 ? {} : { auth: opts.auth }
2443
2924
  }
2444
2925
  );
2445
2926
  return new WorldSession(state2.sessionId, state2, options);
@@ -2463,6 +2944,7 @@ async function runSessions(opts) {
2463
2944
  const taskRef = tasks[next++];
2464
2945
  let task = taskLabel(taskRef);
2465
2946
  let session = null;
2947
+ let keepWarm = opts.keepWarm ?? true;
2466
2948
  try {
2467
2949
  session = await openSession(opts.world, {
2468
2950
  task: taskRef,
@@ -2470,6 +2952,7 @@ async function runSessions(opts) {
2470
2952
  options
2471
2953
  });
2472
2954
  task = taskLabel(taskRef, session.taskInfo);
2955
+ WorldToolsUnread.refuse(opts.world.split("@", 1)[0], session.deprecations);
2473
2956
  await session.ready();
2474
2957
  const toolkit = await session.toolkit();
2475
2958
  const live = session;
@@ -2492,19 +2975,19 @@ async function runSessions(opts) {
2492
2975
  results.push(result);
2493
2976
  opts.onResult?.(result);
2494
2977
  } catch (error) {
2978
+ if (error instanceof WorldToolsUnread) keepWarm = false;
2495
2979
  const result = {
2496
2980
  task,
2497
2981
  sessionId: session?.sessionId ?? "",
2498
2982
  reward: 0,
2499
2983
  rewards: {},
2500
- // An Error with an empty message would read as "no error" to any
2501
- // caller checking truthiness; name the type instead.
2984
+ // An empty message reads as "no error" to a truthiness check; name the type instead.
2502
2985
  error: (error instanceof Error ? error.message : String(error)) || (error instanceof Error ? error.name : "Unknown error")
2503
2986
  };
2504
2987
  results.push(result);
2505
2988
  opts.onResult?.(result);
2506
2989
  } finally {
2507
- await session?.close({ keepWarm: opts.keepWarm ?? true }).catch(() => void 0);
2990
+ await session?.close({ keepWarm }).catch(() => void 0);
2508
2991
  }
2509
2992
  }
2510
2993
  })
@@ -2518,7 +3001,7 @@ async function runSessions(opts) {
2518
3001
  passed: failed === 0 && (opts.failUnder === void 0 || mean >= opts.failUnder)
2519
3002
  };
2520
3003
  }
2521
- var import_node_crypto2, READY_TIMEOUT_MS, READY_POLL_MS, CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep, modelToolName, WorldSessionTimeout, WorldSessionCallError, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
3004
+ var import_node_crypto2, CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep3, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
2522
3005
  var init_session2 = __esm({
2523
3006
  "src/worlds/session.ts"() {
2524
3007
  "use strict";
@@ -2527,22 +3010,21 @@ var init_session2 = __esm({
2527
3010
  init_browser();
2528
3011
  init_nativeComputer();
2529
3012
  init_sessionContract();
3013
+ init_toolSchema();
3014
+ init_requestLedger();
3015
+ init_sessionReadiness();
2530
3016
  init_session();
2531
3017
  init_taskRef();
3018
+ init_worldNotice();
3019
+ init_grade();
2532
3020
  init_taskRef();
2533
- READY_TIMEOUT_MS = 10 * 6e4;
2534
- READY_POLL_MS = 2e3;
3021
+ init_sessionReadiness();
2535
3022
  CALL_TIMEOUT_MS = 3 * 6e4;
2536
3023
  CALL_POLL_MS = 400;
2537
3024
  JUDGED_GRADE_TIMEOUT_MS = 15 * 6e4;
2538
3025
  JUDGED_GRADE_POLL_MS = 2e3;
2539
3026
  EXPORT_PAGE = 500;
2540
- sleep = (ms) => new Promise((r) => setTimeout(r, ms));
2541
- modelToolName = (name) => name.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64) || "tool";
2542
- WorldSessionTimeout = class extends Error {
2543
- };
2544
- WorldSessionCallError = class extends Error {
2545
- };
3027
+ sleep3 = (ms) => new Promise((r) => setTimeout(r, ms));
2546
3028
  WorldSession = class {
2547
3029
  constructor(sessionId, current, options) {
2548
3030
  this.sessionId = sessionId;
@@ -2612,12 +3094,12 @@ var init_session2 = __esm({
2612
3094
  );
2613
3095
  return api2.url;
2614
3096
  }
2615
- /** Headers every request to `api()` needs. A World Host session gates its
2616
- * api surface on a per-session token; a pod or hosted twin answers with the
2617
- * world's own declared auth and this is `{}`. */
3097
+ /** Headers every request to `api()` needs: a World Host session's token in the
3098
+ * platform's `X-World-Session` header, which every world takes; `{}` for a pod
3099
+ * or hosted twin, which answers with the world's own declared auth. */
2618
3100
  async apiHeaders() {
2619
3101
  const { api: api2 } = await this.surfaces();
2620
- return api2?.token ? { Authorization: `Bearer ${api2.token}` } : {};
3102
+ return api2?.token ? { [PLATFORM_SESSION_HEADER]: api2.token } : {};
2621
3103
  }
2622
3104
  /** URL of the world's UI entry page for this session. */
2623
3105
  async ui() {
@@ -2660,18 +3142,17 @@ var init_session2 = __esm({
2660
3142
  }
2661
3143
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
2662
3144
  browserRecorder(agentId) {
2663
- const path12 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
3145
+ const path13 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
2664
3146
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${(0, import_node_crypto2.randomUUID)().replace(/-/g, "").slice(0, 8)}`;
2665
3147
  return new BrowserActionRecorder(
2666
3148
  async (events) => {
2667
- await apiRequest(this.options, "POST", path12, { events });
3149
+ await apiRequest(this.options, "POST", path13, { events });
2668
3150
  },
2669
3151
  id,
2670
3152
  {
2671
- // Continue this agent's timeline where the platform has it, so the
2672
- // same id reopened later never re-numbers from zero.
3153
+ // Continue this agent's timeline where the platform has it, so a reopened id never re-numbers from zero.
2673
3154
  resumeFrom: async () => {
2674
- const summary = await apiRequest(this.options, "GET", path12);
3155
+ const summary = await apiRequest(this.options, "GET", path13);
2675
3156
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
2676
3157
  }
2677
3158
  }
@@ -2689,39 +3170,49 @@ var init_session2 = __esm({
2689
3170
  }
2690
3171
  /** The session's latest platform state (one request, no waiting). */
2691
3172
  async status() {
3173
+ return this.refresh(
3174
+ await apiRequest(
3175
+ this.options,
3176
+ "GET",
3177
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}`
3178
+ )
3179
+ );
3180
+ }
3181
+ /** The world started over under the same id, URLs and token, on what its pin (or `version`, which becomes
3182
+ * its pin) resolves to now: base data plus the task's seed. A grade then reads only the new world's requests. */
3183
+ async restart(opts = {}) {
2692
3184
  const fresh = await apiRequest(
2693
3185
  this.options,
2694
- "GET",
2695
- `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}`
3186
+ "POST",
3187
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/restart`,
3188
+ opts.version ? { version: opts.version } : {}
2696
3189
  );
3190
+ this.refresh(fresh);
3191
+ return fresh;
3192
+ }
3193
+ // A status read carries neither the objective nor the open's notices; keep the ones we opened with.
3194
+ refresh(fresh) {
2697
3195
  this.current = {
2698
3196
  instruction: this.current.instruction,
2699
3197
  tools: this.current.tools,
2700
3198
  surfaces: this.current.surfaces,
2701
3199
  task: this.current.task,
3200
+ deprecations: this.current.deprecations,
2702
3201
  ...fresh
2703
3202
  };
2704
3203
  return this.current;
2705
3204
  }
2706
3205
  /**
2707
- * Wait until the container is warm — meaning the process inside it has
2708
- * asked the platform for work, not merely that a job was accepted.
3206
+ * Wait until the session answers calls: a World Host holds it, or the
3207
+ * process inside a pod has asked the platform for work. Throws
3208
+ * `WorldSessionOpenFailed` (with the platform's reason) when it ended
3209
+ * first, and `WorldSessionTimeout` when `timeoutMs` (default ten minutes)
3210
+ * ran out while it was still starting.
2709
3211
  */
2710
3212
  async ready(opts = {}) {
2711
- const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
2712
- for (; ; ) {
2713
- const state2 = await this.status();
2714
- if (state2.ready) return state2;
2715
- if (state2.state === "FAILED" || state2.state === "STOPPED")
2716
- throw new WorldSessionCallError(
2717
- `World session ${state2.state.toLowerCase()} before it was ready.`
2718
- );
2719
- if (Date.now() > deadline)
2720
- throw new WorldSessionTimeout(
2721
- `World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
2722
- );
2723
- await sleep(READY_POLL_MS);
2724
- }
3213
+ return new SessionReadiness(this.sessionId, () => this.status()).wait(
3214
+ opts.timeoutMs
3215
+ );
2725
3216
  }
2726
3217
  /** Enqueue one interaction and wait for the container's answer. */
2727
3218
  async exchange(body, timeoutMs) {
@@ -2735,7 +3226,7 @@ var init_session2 = __esm({
2735
3226
  const { callId } = enqueued;
2736
3227
  const judged = enqueued.pending === true;
2737
3228
  const budgetMs = timeoutMs ?? (judged ? JUDGED_GRADE_TIMEOUT_MS : CALL_TIMEOUT_MS);
2738
- const pollMs2 = judged ? JUDGED_GRADE_POLL_MS : CALL_POLL_MS;
3229
+ const pollMs = judged ? JUDGED_GRADE_POLL_MS : CALL_POLL_MS;
2739
3230
  const deadline = Date.now() + budgetMs;
2740
3231
  for (; ; ) {
2741
3232
  const call = await apiRequest(
@@ -2751,7 +3242,7 @@ var init_session2 = __esm({
2751
3242
  throw new WorldSessionTimeout(
2752
3243
  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.`
2753
3244
  );
2754
- await sleep(pollMs2);
3245
+ await sleep3(pollMs);
2755
3246
  }
2756
3247
  }
2757
3248
  /**
@@ -2800,7 +3291,9 @@ var init_session2 = __esm({
2800
3291
  * calls); assertions and python graders ignore it. `worlds` names the
2801
3292
  * task's other sessions by alias (`{alias: sessionId}`): the platform
2802
3293
  * reads each one's evidence and hands it to the cross-world verifiers
2803
- * anchored on this session, whose grades come back under `verifiers`. */
3294
+ * anchored on this session, whose grades come back under `verifiers`. A
3295
+ * score that is not a number from 0 to 1 is refused by key and value
3296
+ * (`GradeRefused`); a verifier's is that verifier's `error`. */
2804
3297
  async grade(opts = {}) {
2805
3298
  const raw = await this.exchange(
2806
3299
  {
@@ -2810,14 +3303,7 @@ var init_session2 = __esm({
2810
3303
  },
2811
3304
  opts.timeoutMs
2812
3305
  );
2813
- const record = raw && typeof raw === "object" ? raw : {};
2814
- const verifiers = verifierGrades(record.verifiers);
2815
- return {
2816
- ...numericGrade(record),
2817
- ...typeof record.reason === "string" ? { reason: record.reason } : {},
2818
- ...verifiers ? { verifiers } : {},
2819
- raw
2820
- };
3306
+ return { ...GradePayload.read(raw), raw };
2821
3307
  }
2822
3308
  /**
2823
3309
  * Put rows into the LIVE world through its contract, in one batch:
@@ -2849,19 +3335,31 @@ var init_session2 = __esm({
2849
3335
  async reset(opts = {}) {
2850
3336
  await this.exchange({ kind: "reset" }, opts.timeoutMs);
2851
3337
  }
3338
+ /** One page of the session's entries in its world's request ledger, newest
3339
+ * first and without bodies: at most `limit` after `cursor` (the last page's
3340
+ * `nextCursor`). Reading it records nothing in the session. */
3341
+ async requests(opts = {}) {
3342
+ return new WorldRequestLedger(this.options).forSession(
3343
+ this.sessionId,
3344
+ WorldRequestQuery.parse({ cursor: opts.cursor, limit: opts.limit ?? REQUEST_LOG_PAGE_MAX })
3345
+ );
3346
+ }
3347
+ page(route, limit, cursor) {
3348
+ const query = new URLSearchParams({ limit: String(limit) });
3349
+ if (cursor) query.set("cursor", cursor);
3350
+ return apiRequest(
3351
+ this.options,
3352
+ "GET",
3353
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/${route}?${query.toString()}`
3354
+ );
3355
+ }
2852
3356
  /** The session's call log, oldest first: every tool call, seed, reset,
2853
3357
  * grade with its result — the record of the episode. */
2854
3358
  async export() {
2855
3359
  const calls = [];
2856
3360
  let cursor;
2857
3361
  do {
2858
- const params = new URLSearchParams({ limit: String(EXPORT_PAGE) });
2859
- if (cursor) params.set("cursor", cursor);
2860
- const page = await apiRequest(
2861
- this.options,
2862
- "GET",
2863
- `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/calls?${params.toString()}`
2864
- );
3362
+ const page = await this.page("calls", EXPORT_PAGE, cursor);
2865
3363
  for (const call of page.calls) {
2866
3364
  calls.push({
2867
3365
  seq: Number(call.seq),
@@ -2943,6 +3441,7 @@ var init_session2 = __esm({
2943
3441
  "reuseWarm",
2944
3442
  "surfaces",
2945
3443
  "browserTier",
3444
+ "auth",
2946
3445
  "options"
2947
3446
  ]);
2948
3447
  isOpenSessionOptions = (value) => typeof value === "object" && value !== null && Object.keys(value).every((key) => OPEN_SESSION_OPTION_KEYS.has(key));
@@ -3057,11 +3556,50 @@ var init_tasks = __esm({
3057
3556
  }
3058
3557
  });
3059
3558
 
3559
+ // src/worlds/taskGrades.ts
3560
+ var TASKS2, compact2, TaskGrades;
3561
+ var init_taskGrades = __esm({
3562
+ "src/worlds/taskGrades.ts"() {
3563
+ "use strict";
3564
+ init_worlds();
3565
+ TASKS2 = "/api/public/world-tasks";
3566
+ compact2 = (body) => Object.fromEntries(Object.entries(body).filter(([, v]) => v !== void 0));
3567
+ TaskGrades = class {
3568
+ constructor(options = {}) {
3569
+ this.options = options;
3570
+ }
3571
+ options;
3572
+ try(sessionId, target) {
3573
+ return apiRequest(
3574
+ this.options,
3575
+ "POST",
3576
+ `${TASKS2}/try`,
3577
+ compact2({ sessionId, ...target })
3578
+ );
3579
+ }
3580
+ regrade(taskId, scope = {}) {
3581
+ return apiRequest(
3582
+ this.options,
3583
+ "POST",
3584
+ `${TASKS2}/${encodeURIComponent(taskId)}/regrade`,
3585
+ compact2({ ...scope })
3586
+ );
3587
+ }
3588
+ };
3589
+ }
3590
+ });
3591
+
3060
3592
  // src/worlds/surfaceDescriptor.ts
3061
3593
  function surfaceDescriptor(s) {
3062
3594
  return {
3063
3595
  surfaces: surfaceKinds(s),
3064
- ...s.api ? { api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} } } : {},
3596
+ ...s.api ? {
3597
+ api: {
3598
+ url: s.api.url,
3599
+ ...s.api.token ? { token: s.api.token } : {},
3600
+ ...s.api.auth ? { auth: s.api.auth } : {}
3601
+ }
3602
+ } : {},
3065
3603
  ...s.ui ? { ui: { url: s.ui.url, entry: uiEntryUrl(s.ui), apiPath: s.ui.apiPath ?? null } } : {},
3066
3604
  ...s.browser ? {
3067
3605
  browser: {
@@ -3081,26 +3619,28 @@ var init_surfaceDescriptor = __esm({
3081
3619
 
3082
3620
  // src/worlds/taskSessions.ts
3083
3621
  function aggregateTaskGrade(worlds) {
3084
- const rewards = {};
3085
- const ungraded = [];
3086
- const graded = [];
3622
+ const fold = new TaskGradeFold();
3087
3623
  const verifiers = {};
3088
- const fold = (key, grade) => {
3089
- if (typeof grade.reward === "number") {
3090
- rewards[key] = grade.reward;
3091
- graded.push(grade.reward);
3092
- } else ungraded.push(key);
3093
- for (const [name, value] of Object.entries(grade.rewards))
3094
- rewards[`${key}.${name}`] = value;
3095
- };
3096
3624
  for (const [alias, grade] of Object.entries(worlds)) {
3097
- fold(alias, grade);
3625
+ fold.add(alias, grade, `world ${alias}`);
3098
3626
  Object.assign(verifiers, grade.verifiers ?? {});
3099
3627
  }
3100
3628
  for (const [name, grade] of Object.entries(verifiers))
3101
- fold(`${VERIFIER_REWARD_PREFIX}${name}`, grade);
3102
- const reward = graded.length > 0 ? graded.reduce((a, b) => a + b, 0) / graded.length : null;
3103
- return { reward, rewards, worlds, ungraded, verifiers };
3629
+ fold.add(`${VERIFIER_REWARD_PREFIX}${name}`, grade, `verifier ${name}`);
3630
+ return {
3631
+ reward: fold.reward(),
3632
+ rewards: fold.rewards,
3633
+ worlds,
3634
+ ungraded: fold.ungraded,
3635
+ verifiers,
3636
+ errors: fold.errors
3637
+ };
3638
+ }
3639
+ function taskGradeError(grade) {
3640
+ const lines = Object.entries(grade.errors).map(
3641
+ ([key, why]) => `${key}: ${why}`
3642
+ );
3643
+ return lines.length ? lines.join("; ") : null;
3104
3644
  }
3105
3645
  function fromResponse(answer, options) {
3106
3646
  const sessions = {};
@@ -3109,8 +3649,7 @@ function fromResponse(answer, options) {
3109
3649
  sessions[alias] = new WorldSession(state2.sessionId, state2, options);
3110
3650
  worlds[alias] = {
3111
3651
  slug: state2.slug,
3112
- ref: state2.ref,
3113
- private: state2.private ?? null
3652
+ ref: state2.ref
3114
3653
  };
3115
3654
  }
3116
3655
  return new TaskSessions(
@@ -3185,7 +3724,6 @@ async function attachTask(taskId, options = {}) {
3185
3724
  {
3186
3725
  slug: row.slug ?? "",
3187
3726
  ref: null,
3188
- private: null,
3189
3727
  versionId: row.versionId,
3190
3728
  sessionId: row.sessionId,
3191
3729
  ready: row.ready,
@@ -3215,7 +3753,7 @@ async function attachManifest(manifest, options = {}) {
3215
3753
  Object.fromEntries(
3216
3754
  Object.entries(manifest.sessions).map(([alias, entry]) => [
3217
3755
  alias,
3218
- { slug: entry.slug, ref: entry.ref, private: entry.private ?? null }
3756
+ { slug: entry.slug, ref: entry.ref }
3219
3757
  ])
3220
3758
  )
3221
3759
  );
@@ -3223,12 +3761,13 @@ async function attachManifest(manifest, options = {}) {
3223
3761
  function isTaskManifest(value) {
3224
3762
  return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.sessions === "object" && value.sessions !== null && !("instruction" in value && "worlds" in value);
3225
3763
  }
3226
- var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskSessions;
3764
+ var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskGradeFold, TaskSessions;
3227
3765
  var init_taskSessions = __esm({
3228
3766
  "src/worlds/taskSessions.ts"() {
3229
3767
  "use strict";
3230
3768
  init_worlds();
3231
3769
  init_browser();
3770
+ init_grade();
3232
3771
  init_surfaceDescriptor();
3233
3772
  init_session2();
3234
3773
  TASK_SESSIONS = "/api/public/world-task-sessions";
@@ -3236,13 +3775,42 @@ var init_taskSessions = __esm({
3236
3775
  manifestSession = (s, state2, world) => ({
3237
3776
  slug: world.slug,
3238
3777
  ref: world.ref,
3239
- private: world.private,
3240
3778
  versionId: state2.versionId,
3241
3779
  sessionId: state2.sessionId,
3242
3780
  ready: state2.ready,
3243
3781
  state: state2.state,
3244
3782
  ...surfaceDescriptor(s)
3245
3783
  });
3784
+ TaskGradeFold = class {
3785
+ rewards = {};
3786
+ ungraded = [];
3787
+ errors = {};
3788
+ sources = /* @__PURE__ */ new Map();
3789
+ graded = [];
3790
+ add(key, grade, source) {
3791
+ if (grade.error) this.errors[key] = grade.error;
3792
+ if (typeof grade.reward === "number") {
3793
+ this.put(key, grade.reward, source);
3794
+ this.graded.push(grade.reward);
3795
+ } else this.ungraded.push(key);
3796
+ for (const [name, value] of Object.entries(grade.rewards))
3797
+ this.put(`${key}.${name}`, value, source);
3798
+ }
3799
+ reward() {
3800
+ if (Object.keys(this.errors).length > 0 || !this.graded.length)
3801
+ return null;
3802
+ return this.graded.reduce((a, b) => a + b, 0) / this.graded.length;
3803
+ }
3804
+ put(key, value, source) {
3805
+ const first = this.sources.get(key) ?? source;
3806
+ this.sources.set(key, first);
3807
+ if (first !== source) {
3808
+ this.errors[key] = `two rewards are named ${key} (${first} and ${source}); rename one of them`;
3809
+ return;
3810
+ }
3811
+ this.rewards[key] = value;
3812
+ }
3813
+ };
3246
3814
  TaskSessions = class {
3247
3815
  constructor(taskId, taskVersion, name, instruction, sessions, worlds, hash = null) {
3248
3816
  this.taskId = taskId;
@@ -3263,7 +3831,7 @@ var init_taskSessions = __esm({
3263
3831
  /** This alias's world as recorded at open/attach — what a `TaskRun`
3264
3832
  * filing needs to name, without exposing the whole map. */
3265
3833
  worldRef(alias) {
3266
- return this.worlds[alias] ?? { slug: "", ref: null, private: null };
3834
+ return this.worlds[alias] ?? { slug: "", ref: null };
3267
3835
  }
3268
3836
  /** The aliases, in the spec's order. */
3269
3837
  get aliases() {
@@ -3321,11 +3889,13 @@ var init_taskSessions = __esm({
3321
3889
  * agent's own account of the whole investigation) goes to every world's
3322
3890
  * judge; a world graded by assertions or python ignores it. Each grade
3323
3891
  * names the task's other sessions by alias, so the cross-world verifiers
3324
- * anchored on that world can read them. */
3892
+ * anchored on that world can read them. A world whose grade call fails is
3893
+ * that world's error: the task's grade names it and keeps every other
3894
+ * world's grade. */
3325
3895
  async grade(opts = {}) {
3326
3896
  return aggregateTaskGrade(
3327
3897
  await this.each(
3328
- (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) })
3898
+ (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) }).catch((error) => GradePayload.failedCall(error))
3329
3899
  )
3330
3900
  );
3331
3901
  }
@@ -3373,12 +3943,16 @@ var init_evals = __esm({
3373
3943
  "src/worlds/evals.ts"() {
3374
3944
  "use strict";
3375
3945
  init_worlds();
3946
+ init_transport();
3376
3947
  EVALUATIONS = "/api/public/benchmark-containers/evaluations";
3377
3948
  GatewayEvalsClient = class {
3378
- constructor(options = {}) {
3949
+ constructor(options = {}, retry = RetryPolicy.FILING) {
3379
3950
  this.options = options;
3951
+ this.retry = retry;
3380
3952
  }
3381
3953
  options;
3954
+ retry;
3955
+ keyed = /* @__PURE__ */ new Set();
3382
3956
  async createEvaluation(versionId, opts = {}) {
3383
3957
  const body = { versionId };
3384
3958
  if (opts.model !== void 0) body.model = opts.model;
@@ -3386,12 +3960,18 @@ var init_evals = __esm({
3386
3960
  if (opts.rolloutsPerExample !== void 0)
3387
3961
  body.rolloutsPerExample = opts.rolloutsPerExample;
3388
3962
  if (opts.metadata !== void 0) body.metadata = opts.metadata;
3963
+ if (opts.idempotencyKey !== void 0)
3964
+ body.idempotencyKey = opts.idempotencyKey;
3389
3965
  const answer = await apiRequest(
3390
3966
  this.options,
3391
3967
  "POST",
3392
3968
  EVALUATIONS,
3393
- body
3969
+ body,
3970
+ {
3971
+ retry: opts.idempotencyKey === void 0 ? RetryPolicy.NONE : this.retry
3972
+ }
3394
3973
  );
3974
+ if (opts.idempotencyKey !== void 0) this.keyed.add(answer.evaluationId);
3395
3975
  return answer.evaluationId;
3396
3976
  }
3397
3977
  async pushSamples(evaluationId, samples) {
@@ -3399,7 +3979,8 @@ var init_evals = __esm({
3399
3979
  this.options,
3400
3980
  "POST",
3401
3981
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/samples`,
3402
- { samples }
3982
+ { samples },
3983
+ { retry: this.retryFor(evaluationId) }
3403
3984
  );
3404
3985
  return answer.inserted ?? samples.length;
3405
3986
  }
@@ -3412,10 +3993,297 @@ var init_evals = __esm({
3412
3993
  this.options,
3413
3994
  "POST",
3414
3995
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/finalize`,
3415
- body
3996
+ body,
3997
+ { retry: this.retryFor(evaluationId) }
3416
3998
  );
3417
3999
  return answer.status;
3418
4000
  }
4001
+ retryFor(evaluationId) {
4002
+ return this.keyed.has(evaluationId) ? this.retry : RetryPolicy.NONE;
4003
+ }
4004
+ };
4005
+ }
4006
+ });
4007
+
4008
+ // src/worlds/runSignals.ts
4009
+ var import_node_os, OWNER, RunSignals;
4010
+ var init_runSignals = __esm({
4011
+ "src/worlds/runSignals.ts"() {
4012
+ "use strict";
4013
+ import_node_os = require("os");
4014
+ OWNER = /* @__PURE__ */ Symbol("RunSignals");
4015
+ RunSignals = class _RunSignals {
4016
+ constructor(target = process) {
4017
+ this.target = target;
4018
+ }
4019
+ target;
4020
+ static SIGNALS = ["SIGINT", "SIGTERM"];
4021
+ cleanups = [];
4022
+ heardSignal = null;
4023
+ heardCount = 0;
4024
+ listening = false;
4025
+ left = false;
4026
+ listener = Object.assign(
4027
+ (signal) => this.heard(signal),
4028
+ { [OWNER]: this }
4029
+ );
4030
+ /** The signal that arrived, once one has: start nothing new. */
4031
+ get signal() {
4032
+ return this.owner().heardSignal;
4033
+ }
4034
+ async during(cleanup, work) {
4035
+ return this.owner().hold(cleanup, work);
4036
+ }
4037
+ owner() {
4038
+ const owners = this.target.listeners("SIGINT").map((listener) => listener[OWNER]);
4039
+ return owners.find((owner) => owner !== void 0) ?? this;
4040
+ }
4041
+ async hold(cleanup, work) {
4042
+ this.listen();
4043
+ this.cleanups.push(cleanup);
4044
+ try {
4045
+ return await work();
4046
+ } finally {
4047
+ this.cleanups.splice(this.cleanups.indexOf(cleanup), 1);
4048
+ if (!this.cleanups.length && this.heardSignal === null) this.unlisten();
4049
+ }
4050
+ }
4051
+ heard(signal) {
4052
+ this.heardCount += 1;
4053
+ if (this.heardCount > 2) return this.leave(signal);
4054
+ this.heardSignal ??= signal;
4055
+ void this.cleanUp(signal, this.heardCount === 2).finally(() => this.leave(signal));
4056
+ }
4057
+ async cleanUp(signal, urgent) {
4058
+ for (const cleanup of [...this.cleanups].reverse())
4059
+ await Promise.resolve().then(() => cleanup(signal, urgent)).catch(() => void 0);
4060
+ }
4061
+ leave(signal) {
4062
+ if (this.left) return;
4063
+ this.left = true;
4064
+ this.unlisten();
4065
+ if (this.target.listenerCount(signal) === 0)
4066
+ this.target.exit(128 + import_node_os.constants.signals[signal]);
4067
+ }
4068
+ listen() {
4069
+ if (this.listening) return;
4070
+ this.listening = true;
4071
+ for (const signal of _RunSignals.SIGNALS)
4072
+ this.target.on(signal, this.listener);
4073
+ }
4074
+ unlisten() {
4075
+ this.listening = false;
4076
+ for (const signal of _RunSignals.SIGNALS)
4077
+ this.target.off(signal, this.listener);
4078
+ }
4079
+ };
4080
+ }
4081
+ });
4082
+
4083
+ // src/worlds/taskRunDir.ts
4084
+ function sessionRecord({ api: api2, ui, browser, ...session }) {
4085
+ return {
4086
+ ...session,
4087
+ ...api2 ? { api: { url: api2.url } } : {},
4088
+ ...ui ? { ui: { ...ui, entry: withoutUiToken(ui.entry) } } : {},
4089
+ ...browser ? { browser: browserRecord(browser) } : {}
4090
+ };
4091
+ }
4092
+ var fs, path, TaskRunDir, browserRecord;
4093
+ var init_taskRunDir = __esm({
4094
+ "src/worlds/taskRunDir.ts"() {
4095
+ "use strict";
4096
+ fs = __toESM(require("fs"));
4097
+ path = __toESM(require("path"));
4098
+ init_browser();
4099
+ TaskRunDir = class {
4100
+ constructor(path13) {
4101
+ this.path = path13;
4102
+ }
4103
+ path;
4104
+ writeManifest(manifest) {
4105
+ this.writeJson("manifest.json", {
4106
+ ...manifest,
4107
+ sessions: Object.fromEntries(
4108
+ Object.entries(manifest.sessions).map(([alias, session]) => [
4109
+ alias,
4110
+ sessionRecord(session)
4111
+ ])
4112
+ )
4113
+ });
4114
+ }
4115
+ writeJson(name, value) {
4116
+ this.write(name, JSON.stringify(value, null, 2));
4117
+ }
4118
+ write(name, text2) {
4119
+ fs.mkdirSync(this.path, { recursive: true });
4120
+ fs.writeFileSync(path.join(this.path, name), text2);
4121
+ }
4122
+ };
4123
+ browserRecord = ({ viewport, tier }) => ({
4124
+ viewport,
4125
+ ...tier ? { tier } : {}
4126
+ });
4127
+ }
4128
+ });
4129
+
4130
+ // src/worlds/taskRunOutcome.ts
4131
+ var TaskRunOutcome;
4132
+ var init_taskRunOutcome = __esm({
4133
+ "src/worlds/taskRunOutcome.ts"() {
4134
+ "use strict";
4135
+ init_grade();
4136
+ init_taskSessions();
4137
+ TaskRunOutcome = class {
4138
+ constructor(runId, live, model, runDir) {
4139
+ this.runId = runId;
4140
+ this.live = live;
4141
+ this.model = model;
4142
+ this.runDir = runDir;
4143
+ }
4144
+ runId;
4145
+ live;
4146
+ model;
4147
+ runDir;
4148
+ report = null;
4149
+ extra = {};
4150
+ notes = {};
4151
+ agentError = null;
4152
+ grade = null;
4153
+ traced = false;
4154
+ runError = null;
4155
+ fileError = null;
4156
+ evaluationId;
4157
+ evaluationStatus = null;
4158
+ started = Date.now();
4159
+ stopped(error) {
4160
+ this.runError ??= messageOf(error).slice(0, 2e3);
4161
+ }
4162
+ agentRaised(error) {
4163
+ this.agentError = messageOf(error).slice(0, 2e3);
4164
+ }
4165
+ failure() {
4166
+ const reasons = [
4167
+ this.agentError === null ? null : `agent raised: ${this.agentError}`,
4168
+ this.runError === null ? null : `the run stopped: ${this.runError}`,
4169
+ this.gradeError(),
4170
+ this.grade === null && this.runError === null ? "the run stopped before it was graded" : null
4171
+ ].filter((reason) => reason !== null);
4172
+ return reasons.length ? reasons.join("; ") : null;
4173
+ }
4174
+ async file(evals, setRunId) {
4175
+ let created;
4176
+ try {
4177
+ created = await evals.createEvaluation(this.ref(this.live.aliases[0]).versionId, {
4178
+ model: this.model,
4179
+ rolloutsPerExample: 1,
4180
+ idempotencyKey: this.runId,
4181
+ ...setRunId ? { runId: setRunId } : {},
4182
+ metadata: this.metadata()
4183
+ });
4184
+ await evals.pushSamples(created, [this.sample()]);
4185
+ this.evaluationStatus = await evals.finalizeEvaluation(created, this.finalized());
4186
+ this.evaluationId = created;
4187
+ } catch (error) {
4188
+ this.fileError = created ? `evaluation ${created}: ${messageOf(error)}` : messageOf(error);
4189
+ if (created)
4190
+ await evals.finalizeEvaluation(created, { status: "FAILED", metrics: { error: this.fileError } }).catch(() => void 0);
4191
+ }
4192
+ }
4193
+ summary() {
4194
+ const live = this.live;
4195
+ const { evaluationId: _agentEvaluationId, ...extra } = this.extra;
4196
+ return {
4197
+ ...extra,
4198
+ runId: this.runId,
4199
+ taskId: live.taskId,
4200
+ taskVersion: live.taskVersion,
4201
+ name: live.name,
4202
+ instruction: live.instruction ?? "",
4203
+ model: this.model,
4204
+ aliases: live.aliases,
4205
+ reward: this.grade?.reward ?? null,
4206
+ rewards: this.grade?.rewards ?? {},
4207
+ ungraded: this.grade?.ungraded ?? live.aliases,
4208
+ agentError: this.agentError,
4209
+ runError: this.runError,
4210
+ gradeError: this.gradeError(),
4211
+ fileError: this.fileError,
4212
+ evaluationStatus: this.evaluationStatus,
4213
+ report: this.report,
4214
+ traced: this.traced,
4215
+ runDir: this.runDir,
4216
+ durationSeconds: Math.round((Date.now() - this.started) / 10) / 100,
4217
+ notes: this.notes,
4218
+ filed: this.evaluationId !== void 0,
4219
+ ...this.evaluationId ? { evaluationId: this.evaluationId } : {}
4220
+ };
4221
+ }
4222
+ gradeError() {
4223
+ return this.grade ? taskGradeError(this.grade) : null;
4224
+ }
4225
+ ref(alias) {
4226
+ const { slug, ref } = this.live.worldRef(alias);
4227
+ const session = this.live.sessions[alias];
4228
+ return { slug, ref, versionId: session.versionId, sessionId: session.sessionId };
4229
+ }
4230
+ metadata() {
4231
+ const live = this.live;
4232
+ return {
4233
+ source: "worlds-task-run",
4234
+ task: { id: live.taskId, version: live.taskVersion, name: live.name, hash: live.hash },
4235
+ anchor: live.aliases[0],
4236
+ worlds: Object.fromEntries(live.aliases.map((alias) => [alias, this.ref(alias)])),
4237
+ runId: this.runId
4238
+ };
4239
+ }
4240
+ sample() {
4241
+ const live = this.live;
4242
+ const reward = this.grade?.reward ?? null;
4243
+ const gradeError = this.gradeError();
4244
+ const sample = {
4245
+ exampleId: live.name ?? live.taskId ?? this.runId,
4246
+ task: live.name ?? live.taskId ?? this.runId,
4247
+ rolloutIndex: 0,
4248
+ prompt: live.instruction,
4249
+ ...this.report === null ? {} : { completion: this.report },
4250
+ rewards: this.grade?.rewards ?? {},
4251
+ info: {
4252
+ sessionId: this.runId,
4253
+ worlds: Object.fromEntries(
4254
+ live.aliases.map((alias) => [
4255
+ alias,
4256
+ { ...this.ref(alias), reward: this.grade?.worlds[alias]?.reward ?? null }
4257
+ ])
4258
+ ),
4259
+ ungraded: this.grade?.ungraded ?? live.aliases,
4260
+ verifierVersion: live.taskVersion,
4261
+ traced: this.traced,
4262
+ runDir: this.runDir,
4263
+ ...this.agentError ? { agentError: this.agentError } : {},
4264
+ ...this.runError ? { runError: this.runError } : {},
4265
+ ...gradeError ? { verifyError: gradeError } : {}
4266
+ }
4267
+ };
4268
+ if (typeof reward === "number")
4269
+ Object.assign(sample, { reward, score: reward, correct: reward >= 1 });
4270
+ return sample;
4271
+ }
4272
+ finalized() {
4273
+ const reward = this.grade?.reward ?? null;
4274
+ const failure = this.failure();
4275
+ return {
4276
+ status: failure ? "FAILED" : "COMPLETED",
4277
+ metrics: {
4278
+ ...typeof reward === "number" ? { avg_reward: reward } : {},
4279
+ reward,
4280
+ ...Object.fromEntries(
4281
+ Object.entries(this.grade?.rewards ?? {}).map(([key, value]) => [`rewards.${key}`, value])
4282
+ ),
4283
+ ...failure ? { error: failure } : {}
4284
+ }
4285
+ };
4286
+ }
3419
4287
  };
3420
4288
  }
3421
4289
  });
@@ -3428,7 +4296,7 @@ function getEnv(key) {
3428
4296
  function isGatewayConfigured() {
3429
4297
  return !!(getEnv(GATEWAY_ENV.HOST) && getEnv(GATEWAY_ENV.PUBLIC_KEY) && getEnv(GATEWAY_ENV.SECRET_KEY));
3430
4298
  }
3431
- var GATEWAY_ENV, OPENTELEMETRY_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
4299
+ var GATEWAY_ENV, OTLP_TRACES_PATH, OPENTELEMETRY_PEERS, PINNED_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
3432
4300
  var init_prerequisites = __esm({
3433
4301
  "src/tracing/prerequisites.ts"() {
3434
4302
  "use strict";
@@ -3436,21 +4304,25 @@ var init_prerequisites = __esm({
3436
4304
  HOST: "GATEWAY_HOST",
3437
4305
  PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
3438
4306
  SECRET_KEY: "GATEWAY_SECRET_KEY",
4307
+ API_KEY: "GATEWAY_API_KEY",
3439
4308
  ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
3440
4309
  };
3441
- OPENTELEMETRY_PEERS = [
3442
- "@opentelemetry/api",
3443
- "@opentelemetry/exporter-trace-otlp-http",
3444
- "@opentelemetry/resources",
3445
- "@opentelemetry/sdk-trace-base",
3446
- "@opentelemetry/sdk-trace-node"
3447
- ];
4310
+ OTLP_TRACES_PATH = "/api/public/otel/v1/traces";
4311
+ OPENTELEMETRY_PEERS = {
4312
+ "@opentelemetry/api": "1.9.0",
4313
+ "@opentelemetry/exporter-trace-otlp-http": "0.222.0",
4314
+ "@opentelemetry/resources": "2.11.0",
4315
+ "@opentelemetry/sdk-trace-base": "2.11.0",
4316
+ "@opentelemetry/sdk-trace-node": "2.11.0"
4317
+ };
4318
+ PINNED_PEERS = Object.entries(OPENTELEMETRY_PEERS).map(([name, version]) => `${name}@${version}`).join(" ");
3448
4319
  MODULE_NOT_FOUND = /* @__PURE__ */ new Set(["MODULE_NOT_FOUND", "ERR_MODULE_NOT_FOUND"]);
3449
4320
  OpenTelemetryMissing = class _OpenTelemetryMissing extends Error {
3450
4321
  constructor() {
3451
4322
  super(
3452
- `OpenTelemetry packages not found. Install them with:
3453
- npm install ${OPENTELEMETRY_PEERS.join(" ")}`
4323
+ `OpenTelemetry packages not found. Install them next to @withgateway/sdk, at the versions it is tested with:
4324
+ npm install ${PINNED_PEERS}
4325
+ npm install -g ${PINNED_PEERS} (the gateway CLI, when it was installed with npm install -g)`
3454
4326
  );
3455
4327
  this.name = "OpenTelemetryMissing";
3456
4328
  }
@@ -3625,10 +4497,10 @@ var init_users = __esm({
3625
4497
  totalItems: Number(meta.totalItems ?? rows.length)
3626
4498
  };
3627
4499
  }
3628
- async requestJson(method, path12, payload) {
4500
+ async requestJson(method, path13, payload) {
3629
4501
  let response;
3630
4502
  try {
3631
- response = await fetch(`${this.host}${path12}`, {
4503
+ response = await fetch(`${this.host}${path13}`, {
3632
4504
  method,
3633
4505
  headers: {
3634
4506
  Authorization: this.authHeader(),
@@ -3639,17 +4511,17 @@ var init_users = __esm({
3639
4511
  });
3640
4512
  } catch (error) {
3641
4513
  throw new UserProfileError(
3642
- `${method} ${path12} failed: ${error instanceof Error ? error.message : String(error)}`,
4514
+ `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
3643
4515
  { cause: error }
3644
4516
  );
3645
4517
  }
3646
4518
  const text2 = await response.text();
3647
4519
  if (!response.ok) {
3648
4520
  throw new UserProfileError(
3649
- `${method} ${path12} failed (${response.status}): ${text2.slice(0, 500)}`
4521
+ `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
3650
4522
  );
3651
4523
  }
3652
- this.log("%s %s -> %s", method, path12, response.status);
4524
+ this.log("%s %s -> %s", method, path13, response.status);
3653
4525
  return safeJson(text2);
3654
4526
  }
3655
4527
  authHeader() {
@@ -4094,10 +4966,10 @@ var init_scores = __esm({
4094
4966
  metadata: signalMetadata
4095
4967
  });
4096
4968
  }
4097
- async request(method, path12, payload) {
4969
+ async request(method, path13, payload) {
4098
4970
  let response;
4099
4971
  try {
4100
- response = await fetch(`${this.host}${path12}`, {
4972
+ response = await fetch(`${this.host}${path13}`, {
4101
4973
  method,
4102
4974
  headers: {
4103
4975
  Authorization: this.authHeader(),
@@ -4108,17 +4980,17 @@ var init_scores = __esm({
4108
4980
  });
4109
4981
  } catch (error) {
4110
4982
  throw new ScoreError(
4111
- `${method} ${path12} failed: ${error instanceof Error ? error.message : String(error)}`,
4983
+ `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
4112
4984
  { cause: error }
4113
4985
  );
4114
4986
  }
4115
4987
  const text2 = await response.text();
4116
4988
  if (!response.ok) {
4117
4989
  throw new ScoreError(
4118
- `${method} ${path12} failed (${response.status}): ${text2.slice(0, 500)}`
4990
+ `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
4119
4991
  );
4120
4992
  }
4121
- this.log("%s %s -> %s", method, path12, response.status);
4993
+ this.log("%s %s -> %s", method, path13, response.status);
4122
4994
  }
4123
4995
  authHeader() {
4124
4996
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -4146,7 +5018,7 @@ var require_package = __commonJS({
4146
5018
  "package.json"(exports2, module2) {
4147
5019
  module2.exports = {
4148
5020
  name: "@withgateway/sdk",
4149
- version: "0.3.1",
5021
+ version: "0.4.0",
4150
5022
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
4151
5023
  main: "./dist/index.js",
4152
5024
  module: "./dist/index.mjs",
@@ -4567,7 +5439,7 @@ function buildProvider(config, logFn) {
4567
5439
  const host = config.host ?? getEnv(GATEWAY_ENV.HOST);
4568
5440
  const publicKey = config.publicKey ?? getEnv(GATEWAY_ENV.PUBLIC_KEY);
4569
5441
  const secretKey = config.secretKey ?? getEnv(GATEWAY_ENV.SECRET_KEY);
4570
- const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${DEFAULT_OTLP_PATH}` : void 0);
5442
+ const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${OTLP_TRACES_PATH}` : void 0);
4571
5443
  const missing = [];
4572
5444
  if (!host) missing.push("host (or GATEWAY_HOST env var)");
4573
5445
  if (!publicKey) missing.push("publicKey (or GATEWAY_PUBLIC_KEY env var)");
@@ -4810,7 +5682,7 @@ async function shutdown() {
4810
5682
  function isInitialized() {
4811
5683
  return state.initialized;
4812
5684
  }
4813
- var DEFAULT_OTLP_PATH, state;
5685
+ var state;
4814
5686
  var init_init = __esm({
4815
5687
  "src/tracing/init.ts"() {
4816
5688
  "use strict";
@@ -4821,7 +5693,6 @@ var init_init = __esm({
4821
5693
  init_build();
4822
5694
  init_prerequisites();
4823
5695
  init_prerequisites();
4824
- DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
4825
5696
  state = {
4826
5697
  initialized: false,
4827
5698
  provider: null
@@ -4830,124 +5701,94 @@ var init_init = __esm({
4830
5701
  });
4831
5702
 
4832
5703
  // src/worlds/taskTracing.ts
4833
- var TaskTracing;
5704
+ var HOST_WITH_AUTHORITY, TRACING_HELD_ELSEWHERE, TaskTracing;
4834
5705
  var init_taskTracing = __esm({
4835
5706
  "src/worlds/taskTracing.ts"() {
4836
5707
  "use strict";
4837
5708
  init_prerequisites();
5709
+ init_platformEndpoint();
5710
+ HOST_WITH_AUTHORITY = /^[a-z][a-z0-9+.-]*:\/\/[^/\\?#@\s]+(\/|$)/i;
5711
+ 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.";
4838
5712
  TaskTracing = class _TaskTracing {
4839
5713
  constructor(load = () => Promise.resolve().then(() => (init_init(), init_exports))) {
4840
5714
  this.load = load;
4841
5715
  }
4842
5716
  load;
4843
5717
  static warned = false;
4844
- async start(requested) {
4845
- if (!requested || !isGatewayConfigured()) return false;
5718
+ static target = null;
5719
+ async start(requested, options) {
5720
+ if (!requested) return false;
5721
+ const endpoint = platformEndpoint(options);
4846
5722
  try {
4847
- (await this.load()).init();
5723
+ await this.initOnce(_TaskTracing.config(endpoint));
4848
5724
  return true;
4849
5725
  } catch (error) {
4850
- _TaskTracing.warnOnce(error);
5726
+ _TaskTracing.warnOnce(_TaskTracing.reason(error, endpoint));
4851
5727
  return false;
4852
5728
  }
4853
5729
  }
4854
- static warnOnce(error) {
5730
+ // init() keeps the first provider for the whole process and ignores later calls, so a later run is
5731
+ // traced only when it exports to the same place with the same keys.
5732
+ async initOnce(config) {
5733
+ const tracing = await this.load();
5734
+ const target = JSON.stringify([config.endpoint, config.publicKey, config.secretKey]);
5735
+ if (!tracing.isInitialized()) {
5736
+ tracing.init(config);
5737
+ _TaskTracing.target = target;
5738
+ return;
5739
+ }
5740
+ if (_TaskTracing.target !== target) throw new Error(TRACING_HELD_ELSEWHERE);
5741
+ }
5742
+ static config(endpoint) {
5743
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
5744
+ return { ...endpoint, endpoint: _TaskTracing.destination(endpoint) };
5745
+ }
5746
+ // B1: a saved login's keys go to their own host only. The redirect is compared as text, never parsed:
5747
+ // a fixed "<host>/" prefix leaves no URL parser room to read another host into it.
5748
+ static destination(endpoint) {
5749
+ const redirect = getEnv(GATEWAY_ENV.ENDPOINT);
5750
+ if (!redirect) return `${endpoint.host}${OTLP_TRACES_PATH}`;
5751
+ if (_TaskTracing.underHost(redirect, endpoint.host) || _TaskTracing.environmentKeys(endpoint)) return redirect;
5752
+ throw new Error(
5753
+ `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.`
5754
+ );
5755
+ }
5756
+ static underHost(redirect, host) {
5757
+ return HOST_WITH_AUTHORITY.test(host) && redirect.startsWith(`${host}/`);
5758
+ }
5759
+ static environmentKeys(endpoint) {
5760
+ const secretKey = getEnv(GATEWAY_ENV.SECRET_KEY) ?? getEnv(GATEWAY_ENV.API_KEY);
5761
+ return endpoint.publicKey === getEnv(GATEWAY_ENV.PUBLIC_KEY) && endpoint.secretKey === secretKey;
5762
+ }
5763
+ static warnOnce(reason) {
4855
5764
  if (_TaskTracing.warned) return;
4856
5765
  _TaskTracing.warned = true;
4857
5766
  console.warn(
4858
- `[gatewaysdk.tracing] Task runs in this process are not traced. ${_TaskTracing.reason(error)}
5767
+ `[gatewaysdk.tracing] Task runs in this process are not traced. ${reason}
4859
5768
  Pass --no-trace (trace: false) to run untraced without this warning.`
4860
5769
  );
4861
5770
  }
4862
- static reason(error) {
4863
- if (OpenTelemetryMissing.explains(error))
4864
- return new OpenTelemetryMissing().message;
4865
- return error instanceof Error ? error.message : String(error);
5771
+ static reason(error, endpoint) {
5772
+ if (OpenTelemetryMissing.explains(error)) return new OpenTelemetryMissing().message;
5773
+ const message = error instanceof Error ? error.message : String(error);
5774
+ const keys = endpoint ? [endpoint.publicKey, endpoint.secretKey] : [];
5775
+ return keys.reduce((text2, key) => text2.split(key).join("[redacted]"), message);
4866
5776
  }
4867
5777
  };
4868
5778
  }
4869
5779
  });
4870
5780
 
4871
5781
  // src/worlds/taskRun.ts
4872
- function worldRef(live, alias) {
4873
- const { slug, ref } = live.worldRef(alias);
4874
- const session = live.sessions[alias];
4875
- return {
4876
- slug,
4877
- ref,
4878
- versionId: session.versionId,
4879
- sessionId: session.sessionId
4880
- };
4881
- }
4882
- async function fileTaskRun(live, runId, model, report, grade, agentError, traced, runDir, options, setRunId) {
4883
- const aliases = live.aliases;
4884
- const anchor = aliases[0];
4885
- const worldsMeta = Object.fromEntries(
4886
- aliases.map((alias) => [alias, worldRef(live, alias)])
4887
- );
4888
- const evals = new GatewayEvalsClient(options);
4889
- const evaluationId = await evals.createEvaluation(
4890
- worldRef(live, anchor).versionId,
4891
- {
4892
- model,
4893
- rolloutsPerExample: 1,
4894
- ...setRunId ? { runId: setRunId } : {},
4895
- metadata: {
4896
- source: "worlds-task-run",
4897
- task: {
4898
- id: live.taskId,
4899
- version: live.taskVersion,
4900
- name: live.name,
4901
- hash: live.hash
4902
- },
4903
- anchor,
4904
- worlds: worldsMeta,
4905
- runId
4906
- }
4907
- }
4908
- );
4909
- const reward = grade.reward;
4910
- const sample = {
4911
- exampleId: live.name ?? live.taskId ?? runId,
4912
- task: live.name ?? live.taskId ?? runId,
4913
- rolloutIndex: 0,
4914
- prompt: live.instruction,
4915
- ...report === null ? {} : { completion: report },
4916
- rewards: grade.rewards,
4917
- info: {
4918
- sessionId: runId,
4919
- worlds: Object.fromEntries(
4920
- aliases.map((alias) => [
4921
- alias,
4922
- {
4923
- ...worldRef(live, alias),
4924
- reward: grade.worlds[alias]?.reward ?? null
4925
- }
4926
- ])
4927
- ),
4928
- ungraded: grade.ungraded,
4929
- traced,
4930
- runDir,
4931
- ...agentError ? { agentError } : {}
4932
- }
4933
- };
4934
- if (typeof reward === "number") {
4935
- sample.reward = reward;
4936
- sample.score = reward;
4937
- sample.correct = reward >= 1;
4938
- }
4939
- await evals.pushSamples(evaluationId, [sample]);
4940
- await evals.finalizeEvaluation(evaluationId, {
4941
- status: agentError ? "FAILED" : "COMPLETED",
4942
- metrics: {
4943
- ...typeof reward === "number" ? { avg_reward: reward } : {},
4944
- reward,
4945
- ...Object.fromEntries(
4946
- Object.entries(grade.rewards).map(([k, v]) => [`rewards.${k}`, v])
4947
- )
4948
- }
4949
- });
4950
- return evaluationId;
5782
+ function taskRunFailure(summary) {
5783
+ if (summary.agentError !== null) return `agent raised: ${summary.agentError}`;
5784
+ if (summary.runError !== null) return `the run stopped: ${summary.runError}`;
5785
+ if (summary.gradeError !== null)
5786
+ return `the task could not be graded: ${summary.gradeError}`;
5787
+ if (summary.fileError !== null)
5788
+ return `the run could not be filed: ${summary.fileError}`;
5789
+ if (summary.filed && summary.evaluationStatus !== "COMPLETED")
5790
+ return `the platform settled evaluation ${summary.evaluationId} as ${summary.evaluationStatus}`;
5791
+ return null;
4951
5792
  }
4952
5793
  function reportFromResult(result) {
4953
5794
  if (result === void 0 || result === null)
@@ -4965,113 +5806,49 @@ function reportFromResult(result) {
4965
5806
  );
4966
5807
  return { report, extra };
4967
5808
  }
5809
+ async function refuseUnreadTools(live) {
5810
+ try {
5811
+ for (const alias of live.aliases)
5812
+ WorldToolsUnread.refuse(`${live.worldRef(alias).slug} (alias ${alias})`, live.session(alias).deprecations);
5813
+ } catch (error) {
5814
+ await live.close({ keepWarm: false }).catch(() => void 0);
5815
+ throw error;
5816
+ }
5817
+ }
4968
5818
  async function runTask(ref, agent, opts) {
4969
5819
  const runId = `wtr-${(0, import_node_crypto4.randomBytes)(6).toString("hex")}`;
4970
- const outDir = opts.outDir ?? "runs";
4971
- const runDir = path.join(outDir, runId);
4972
- fs.mkdirSync(runDir, { recursive: true });
4973
5820
  const options = opts.options ?? {};
4974
- const live = await openTask(ref, {
4975
- surfaces: opts.surfaces,
4976
- reuseWarm: opts.reuseWarm,
4977
- browserTier: opts.browserTier,
4978
- options
4979
- });
4980
- const started = Date.now();
4981
- try {
4982
- await live.ready();
4983
- const traced = await new TaskTracing().start(opts.trace !== false);
4984
- const taskRun = new TaskRun(
4985
- runId,
4986
- live.instruction ?? "",
4987
- opts.model,
4988
- live.sessions,
4989
- await live.manifest()
4990
- );
4991
- let agentError = null;
4992
- let report = null;
4993
- let extra = {};
4994
- try {
4995
- const invoke = () => agent(taskRun);
4996
- const result = traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(runId, invoke) : await invoke();
4997
- ({ report, extra } = reportFromResult(result));
4998
- } catch (error) {
4999
- const message = (error instanceof Error ? error.message : String(error)) || "Unknown error";
5000
- agentError = message.slice(0, 2e3);
5001
- }
5002
- if (traced)
5003
- await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing) => tracing.flush()).catch(() => void 0);
5004
- const grade = report !== null ? await live.grade({ report }) : await live.grade();
5005
- fs.writeFileSync(
5006
- path.join(runDir, "manifest.json"),
5007
- JSON.stringify(taskRun.manifest, null, 2)
5008
- );
5009
- fs.writeFileSync(
5010
- path.join(runDir, "grades.json"),
5011
- JSON.stringify(grade, null, 2)
5012
- );
5013
- if (report) fs.writeFileSync(path.join(runDir, "report.md"), report);
5014
- const summary = {
5015
- runId,
5016
- taskId: live.taskId,
5017
- taskVersion: live.taskVersion,
5018
- name: live.name,
5019
- instruction: taskRun.instruction,
5020
- model: opts.model,
5021
- aliases: live.aliases,
5022
- reward: grade.reward,
5023
- rewards: grade.rewards,
5024
- ungraded: grade.ungraded,
5025
- agentError,
5026
- report,
5027
- traced,
5028
- runDir,
5029
- durationSeconds: Math.round((Date.now() - started) / 10) / 100,
5030
- notes: taskRun.notes,
5031
- filed: false,
5032
- ...extra
5033
- };
5034
- if (opts.file !== false) {
5035
- summary.evaluationId = await fileTaskRun(
5036
- live,
5037
- runId,
5038
- opts.model,
5039
- report,
5040
- grade,
5041
- agentError,
5042
- traced,
5043
- runDir,
5044
- options,
5045
- opts.setRunId
5046
- );
5047
- summary.filed = true;
5048
- }
5049
- fs.writeFileSync(
5050
- path.join(runDir, "run.json"),
5051
- JSON.stringify(summary, null, 2)
5052
- );
5053
- return summary;
5054
- } finally {
5055
- if (!opts.keepUp) {
5056
- try {
5057
- await live.close({ keepWarm: false });
5058
- } catch {
5059
- }
5060
- }
5061
- }
5821
+ const opening = new TaskOpening(
5822
+ openTask(ref, {
5823
+ surfaces: opts.surfaces,
5824
+ reuseWarm: opts.reuseWarm,
5825
+ browserTier: opts.browserTier,
5826
+ options
5827
+ }).then(async (live) => {
5828
+ await refuseUnreadTools(live);
5829
+ return new LiveTaskRun(runId, live, opts, options);
5830
+ })
5831
+ );
5832
+ return new RunSignals().during(
5833
+ (signal, urgent) => opening.interrupt(signal, urgent),
5834
+ () => opening.complete(agent)
5835
+ );
5062
5836
  }
5063
- var import_node_crypto4, fs, path, TaskRun, describeValue, RESULT_CONTRACT;
5837
+ var import_node_crypto4, path2, TaskRun, FILING_UNFINISHED, describeValue, RESULT_CONTRACT, LiveTaskRun, TaskOpening;
5064
5838
  var init_taskRun = __esm({
5065
5839
  "src/worlds/taskRun.ts"() {
5066
5840
  "use strict";
5067
5841
  import_node_crypto4 = require("crypto");
5068
- fs = __toESM(require("fs"));
5069
- path = __toESM(require("path"));
5842
+ path2 = __toESM(require("path"));
5070
5843
  init_browser();
5071
5844
  init_evals();
5845
+ init_runSignals();
5072
5846
  init_session2();
5073
5847
  init_taskSessions();
5848
+ init_taskRunDir();
5849
+ init_taskRunOutcome();
5074
5850
  init_taskTracing();
5851
+ init_worldNotice();
5075
5852
  TaskRun = class {
5076
5853
  constructor(runId, instruction, model, sessions, manifest) {
5077
5854
  this.runId = runId;
@@ -5134,8 +5911,127 @@ var init_taskRun = __esm({
5134
5911
  Object.assign(this.notes, notes);
5135
5912
  }
5136
5913
  };
5914
+ FILING_UNFINISHED = "the filing did not finish";
5137
5915
  describeValue = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
5138
5916
  RESULT_CONTRACT = "return a string report, an object {report?: string, ...} or nothing";
5917
+ LiveTaskRun = class {
5918
+ constructor(runId, live, opts, options) {
5919
+ this.live = live;
5920
+ this.opts = opts;
5921
+ this.options = options;
5922
+ this.files = new TaskRunDir(path2.join(opts.outDir ?? "runs", runId));
5923
+ this.outcome = new TaskRunOutcome(runId, live, opts.model, this.files.path);
5924
+ }
5925
+ live;
5926
+ opts;
5927
+ options;
5928
+ files;
5929
+ outcome;
5930
+ played = false;
5931
+ interrupted = false;
5932
+ filing = null;
5933
+ ending = null;
5934
+ async complete(agent) {
5935
+ try {
5936
+ await this.play(agent).catch((error) => this.outcome.stopped(error));
5937
+ this.played = true;
5938
+ await this.file();
5939
+ return this.record();
5940
+ } finally {
5941
+ await this.end();
5942
+ }
5943
+ }
5944
+ async interrupt(signal, urgent = false) {
5945
+ const sessions = this.opts.keepUp ? "leaving its sessions up" : "closing its sessions";
5946
+ const how = urgent ? " now, without waiting" : "";
5947
+ process.stderr.write(`${this.outcome.runId} interrupted by ${signal}; ${sessions}${how}
5948
+ `);
5949
+ if (!this.played) this.outcome.stopped(`interrupted by ${signal}`);
5950
+ this.interrupted = true;
5951
+ try {
5952
+ if (!urgent) await this.filing;
5953
+ this.record();
5954
+ } catch (error) {
5955
+ const why = error instanceof Error ? error.message : String(error);
5956
+ process.stderr.write(`${this.outcome.runId}: run.json not written: ${why}
5957
+ `);
5958
+ } finally {
5959
+ await this.end();
5960
+ }
5961
+ }
5962
+ summary() {
5963
+ return this.outcome.summary();
5964
+ }
5965
+ async play(agent) {
5966
+ await this.live.ready();
5967
+ this.outcome.traced = await new TaskTracing().start(this.opts.trace !== false, this.options);
5968
+ const run = new TaskRun(
5969
+ this.outcome.runId,
5970
+ this.live.instruction ?? "",
5971
+ this.opts.model,
5972
+ this.live.sessions,
5973
+ await this.live.manifest()
5974
+ );
5975
+ this.outcome.notes = run.notes;
5976
+ this.files.writeManifest(run.manifest);
5977
+ await this.callAgent(agent, run);
5978
+ const report = this.outcome.report;
5979
+ this.outcome.grade = await this.live.grade(report !== null ? { report } : {});
5980
+ this.files.writeJson("grades.json", this.outcome.grade);
5981
+ if (report) this.files.write("report.md", report);
5982
+ }
5983
+ async callAgent(agent, run) {
5984
+ try {
5985
+ const invoke = () => agent(run);
5986
+ const result = this.outcome.traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(run.runId, invoke) : await invoke();
5987
+ ({ report: this.outcome.report, extra: this.outcome.extra } = reportFromResult(result));
5988
+ } catch (error) {
5989
+ this.outcome.agentRaised(error);
5990
+ }
5991
+ if (this.outcome.traced)
5992
+ await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing) => tracing.flush()).catch(() => void 0);
5993
+ }
5994
+ async file() {
5995
+ if (this.opts.file === false || this.interrupted) return;
5996
+ this.files.writeJson("run.json", { ...this.outcome.summary(), fileError: FILING_UNFINISHED });
5997
+ this.filing = this.outcome.file(new GatewayEvalsClient(this.options), this.opts.setRunId);
5998
+ await this.filing;
5999
+ }
6000
+ record() {
6001
+ const summary = this.outcome.summary();
6002
+ this.files.writeJson("run.json", summary);
6003
+ return summary;
6004
+ }
6005
+ end() {
6006
+ this.ending ??= this.opts.keepUp ? Promise.resolve() : this.live.close({ keepWarm: false }).then(
6007
+ () => void 0,
6008
+ () => void 0
6009
+ );
6010
+ return this.ending;
6011
+ }
6012
+ };
6013
+ TaskOpening = class {
6014
+ interrupting = null;
6015
+ run = null;
6016
+ opened;
6017
+ constructor(opening) {
6018
+ this.opened = opening.then((run) => this.run = run);
6019
+ }
6020
+ async complete(agent) {
6021
+ const run = await this.opened;
6022
+ if (!this.interrupting) return run.complete(agent);
6023
+ await this.interrupting;
6024
+ return run.summary();
6025
+ }
6026
+ interrupt(signal, urgent = false) {
6027
+ if (urgent) return this.run?.interrupt(signal, true) ?? Promise.resolve();
6028
+ this.interrupting ??= this.opened.then(
6029
+ (run) => run.interrupt(signal),
6030
+ () => void 0
6031
+ );
6032
+ return this.interrupting;
6033
+ }
6034
+ };
5139
6035
  }
5140
6036
  });
5141
6037
 
@@ -5215,106 +6111,182 @@ async function runSet(datasetId, agent, opts) {
5215
6111
  source: opts.source ?? "sdk",
5216
6112
  ...git && Object.keys(git).length ? { git } : {}
5217
6113
  });
5218
- const close = (status) => apiRequest(
5219
- options,
5220
- "POST",
5221
- `${SET_RUNS}/${encodeURIComponent(opened.id)}/close`,
5222
- { status }
5223
- );
5224
- const onInterrupt = () => {
5225
- void close("failed").finally(() => process.exit(130));
5226
- };
5227
- process.once("SIGINT", onInterrupt);
5228
- const planned = await runPlannedTasks(opened, agent, opts, options).finally(
5229
- () => process.off("SIGINT", onInterrupt)
5230
- );
5231
- const { tasks, failed } = planned;
5232
- const requested = failed ? "failed" : "completed";
5233
- const setRun = await close(requested);
5234
- return {
5235
- setRun,
5236
- tasks,
5237
- failed: failed || setRun.status !== "completed",
5238
- closedAsRequested: setRun.status === requested
5239
- };
5240
- }
5241
- async function runPlannedTasks(opened, agent, opts, options) {
5242
- const tasks = [];
5243
- let failed = false;
5244
- for (const task of opened.tasks) {
5245
- try {
5246
- const summary = await runTask(task.worldTaskId, agent, {
5247
- model: opts.model,
5248
- surfaces: opts.surfaces,
5249
- browserTier: opts.browserTier,
5250
- outDir: opts.outDir,
5251
- trace: opts.trace,
5252
- setRunId: opened.id,
5253
- options
5254
- });
5255
- tasks.push(summary);
5256
- failed ||= summary.agentError !== null;
5257
- opts.onTask?.(summary);
5258
- } catch (error) {
5259
- failed = true;
5260
- const message = error instanceof Error ? error.message : String(error);
5261
- process.stderr.write(`task ${task.name} could not run: ${message}
5262
- `);
5263
- }
5264
- }
5265
- return { tasks, failed };
6114
+ return new SetRunner(opened, agent, opts, options).run();
5266
6115
  }
5267
- var SET_RUNS;
6116
+ var SET_RUNS, SetRunner;
5268
6117
  var init_setRun = __esm({
5269
6118
  "src/worlds/setRun.ts"() {
5270
6119
  "use strict";
5271
6120
  init_worlds();
5272
6121
  init_checkoutProvenance();
6122
+ init_runSignals();
5273
6123
  init_taskRun();
5274
6124
  SET_RUNS = "/api/public/set-runs";
6125
+ SetRunner = class {
6126
+ constructor(opened, agent, opts, options) {
6127
+ this.opened = opened;
6128
+ this.agent = agent;
6129
+ this.opts = opts;
6130
+ this.options = options;
6131
+ }
6132
+ opened;
6133
+ agent;
6134
+ opts;
6135
+ options;
6136
+ tasks = [];
6137
+ failures = [];
6138
+ failed = false;
6139
+ current = null;
6140
+ interrupted = null;
6141
+ signals = new RunSignals();
6142
+ async run() {
6143
+ await this.signals.during(
6144
+ (signal2) => this.interrupt(signal2).then(() => void 0),
6145
+ () => this.runTasks()
6146
+ );
6147
+ const signal = this.signals.signal;
6148
+ if (signal !== null) return this.result(await this.interrupt(signal), "failed");
6149
+ const requested = this.failed ? "failed" : "completed";
6150
+ return this.result(await this.close(requested, this.failures), requested);
6151
+ }
6152
+ interrupt(signal) {
6153
+ const stopped = this.current ? [{ worldTaskId: this.current, reason: `interrupted by ${signal}` }] : [];
6154
+ this.interrupted ??= this.close("failed", [...this.failures, ...stopped]);
6155
+ return this.interrupted;
6156
+ }
6157
+ async runTasks() {
6158
+ for (const task of this.opened.tasks) {
6159
+ if (this.signals.signal !== null) return;
6160
+ this.current = task.worldTaskId;
6161
+ await this.runOne(task);
6162
+ }
6163
+ this.current = null;
6164
+ }
6165
+ async runOne(task) {
6166
+ try {
6167
+ const summary = await runTask(task.worldTaskId, this.agent, {
6168
+ model: this.opts.model,
6169
+ surfaces: this.opts.surfaces,
6170
+ browserTier: this.opts.browserTier,
6171
+ outDir: this.opts.outDir,
6172
+ trace: this.opts.trace,
6173
+ setRunId: this.opened.id,
6174
+ options: this.options
6175
+ });
6176
+ this.tasks.push(summary);
6177
+ this.failed ||= taskRunFailure(summary) !== null;
6178
+ if (summary.fileError !== null)
6179
+ this.failures.push({ worldTaskId: task.worldTaskId, reason: summary.fileError });
6180
+ this.opts.onTask?.(summary);
6181
+ } catch (error) {
6182
+ this.failed = true;
6183
+ const message = error instanceof Error ? error.message : String(error);
6184
+ process.stderr.write(`task ${task.name} could not run: ${message}
6185
+ `);
6186
+ this.failures.push({
6187
+ worldTaskId: task.worldTaskId,
6188
+ reason: message || (error instanceof Error ? error.name : "Error")
6189
+ });
6190
+ }
6191
+ }
6192
+ close(status, failures = []) {
6193
+ return apiRequest(
6194
+ this.options,
6195
+ "POST",
6196
+ `${SET_RUNS}/${encodeURIComponent(this.opened.id)}/close`,
6197
+ { status, ...failures.length ? { failures } : {} }
6198
+ );
6199
+ }
6200
+ result(setRun, requested) {
6201
+ return {
6202
+ setRun,
6203
+ tasks: this.tasks,
6204
+ failed: this.failed || setRun.status !== "completed",
6205
+ closedAsRequested: setRun.status === requested
6206
+ };
6207
+ }
6208
+ };
5275
6209
  }
5276
6210
  });
5277
6211
 
5278
6212
  // src/worlds/requestLog.ts
5279
- var WorldRequestLog;
6213
+ function worldRequestsUnrecorded(counts) {
6214
+ const parts = UNRECORDED.map(([field2, one, many]) => [counted(counts[field2]), one, many]).filter(([count]) => count > 0).map(([count, one, many]) => (count === 1 ? one : many).replace("{n}", String(count)));
6215
+ return parts.length > 0 ? `Not recorded: ${parts.join(", ")}` : null;
6216
+ }
6217
+ function counted(value) {
6218
+ return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : 0;
6219
+ }
6220
+ var UNRECORDED, WorldRequestLog;
5280
6221
  var init_requestLog = __esm({
5281
6222
  "src/worlds/requestLog.ts"() {
5282
6223
  "use strict";
6224
+ init_requestLedger();
6225
+ UNRECORDED = [
6226
+ ["lost", "{n} request", "{n} requests"],
6227
+ ["shed", "{n} request's headers and bodies", "{n} requests' headers and bodies"],
6228
+ ["hostsLost", "the requests {n} lost host held", "the requests {n} lost hosts held"]
6229
+ ];
5283
6230
  WorldRequestLog = class _WorldRequestLog {
5284
- constructor(requests) {
6231
+ constructor(requests, notRecorded = null, unrecorded = null) {
5285
6232
  this.requests = requests;
6233
+ this.notRecorded = notRecorded;
6234
+ this.unrecorded = unrecorded;
5286
6235
  }
5287
6236
  requests;
5288
- static async read(session) {
5289
- const state2 = await session.state();
5290
- const calls = state2?.calls;
5291
- if (!Array.isArray(calls)) return null;
5292
- return new _WorldRequestLog(calls.filter((c) => typeof c === "object" && c !== null));
5293
- }
5294
- /** How many older requests the world no longer keeps (its log is bounded): the gap
5295
- * before the first entry it still holds. */
5296
- get dropped() {
5297
- const first = Number(this.requests[0]?.seq) || 1;
5298
- return Math.max(0, first - 1);
5299
- }
5300
- /** Each request in the shape of a ledger line, `kind: "request"`, so one JSONL
5301
- * file holds both. */
6237
+ notRecorded;
6238
+ unrecorded;
6239
+ /** Every entry the ledger holds for the session, oldest first, and the line for what it did not record;
6240
+ * for a session the World Host did not serve, none, and `notRecorded` says why. */
6241
+ static async load(session) {
6242
+ try {
6243
+ return await _WorldRequestLog.pages(session);
6244
+ } catch (error) {
6245
+ const refused = WorldRequestsNotRecorded.from(error);
6246
+ if (!(refused instanceof WorldRequestsNotRecorded)) throw error;
6247
+ return new _WorldRequestLog([], refused.message);
6248
+ }
6249
+ }
6250
+ static async pages(session) {
6251
+ const requests = [];
6252
+ let cursor;
6253
+ for (; ; ) {
6254
+ const page = await session.requests(cursor ? { cursor } : {});
6255
+ requests.push(...page.requests);
6256
+ if (!page.nextCursor || page.requests.length === 0)
6257
+ return new _WorldRequestLog(requests.reverse(), null, worldRequestsUnrecorded(page));
6258
+ cursor = page.nextCursor;
6259
+ }
6260
+ }
6261
+ /** Each entry in the shape of a ledger line, `kind: "request"`, so one JSONL
6262
+ * file holds both: `args` what was asked, `result` what came back under the
6263
+ * entry's `requestId`. */
5302
6264
  asCalls() {
5303
6265
  return this.requests.map((r) => ({
5304
- seq: Number(r.seq) || 0,
6266
+ seq: r.seq,
5305
6267
  kind: "request",
5306
- tool: r.operation ?? null,
6268
+ tool: r.operation,
5307
6269
  args: {
5308
- method: r.method ?? null,
5309
- path: r.path ?? null,
5310
- args: r.args ?? null,
5311
- query: r.query ?? null,
5312
- body: r.body ?? null,
5313
- via: r.via ?? null
6270
+ method: r.method,
6271
+ path: r.path,
6272
+ query: r.query,
6273
+ via: r.via,
6274
+ contentType: r.request.contentType,
6275
+ bodyBytes: r.request.bodyBytes,
6276
+ truncated: r.request.truncated
6277
+ },
6278
+ result: {
6279
+ requestId: r.id,
6280
+ status: r.response.status,
6281
+ rows: r.rows,
6282
+ found: r.found,
6283
+ durationMs: r.durationMs,
6284
+ contentType: r.response.contentType,
6285
+ bodyBytes: r.response.bodyBytes,
6286
+ truncated: r.response.truncated
5314
6287
  },
5315
- result: { status: r.status ?? null, rows: r.rows ?? null, found: r.found ?? null },
5316
6288
  error: null,
5317
- completedAt: r.at ?? null
6289
+ completedAt: r.at
5318
6290
  }));
5319
6291
  }
5320
6292
  };
@@ -5322,12 +6294,12 @@ var init_requestLog = __esm({
5322
6294
  });
5323
6295
 
5324
6296
  // src/worlds/checkoutTarget.ts
5325
- var fs2, path2, CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
6297
+ var fs2, path3, CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
5326
6298
  var init_checkoutTarget = __esm({
5327
6299
  "src/worlds/checkoutTarget.ts"() {
5328
6300
  "use strict";
5329
6301
  fs2 = __toESM(require("fs"));
5330
- path2 = __toESM(require("path"));
6302
+ path3 = __toESM(require("path"));
5331
6303
  CheckoutRefusedError = class extends Error {
5332
6304
  constructor(message) {
5333
6305
  super(`checkout refused: ${message}`);
@@ -5352,17 +6324,17 @@ var init_checkoutTarget = __esm({
5352
6324
  }
5353
6325
  /** The absolute destination; a relative target may not resolve above `cwd`. */
5354
6326
  static confined(target, cwd) {
5355
- const dir = path2.resolve(cwd, target);
5356
- if (path2.isAbsolute(target)) return dir;
5357
- const rel = path2.relative(cwd, dir);
5358
- if (rel === "" || !rel.startsWith("..") && !path2.isAbsolute(rel))
6327
+ const dir = path3.resolve(cwd, target);
6328
+ if (path3.isAbsolute(target)) return dir;
6329
+ const rel = path3.relative(cwd, dir);
6330
+ if (rel === "" || !rel.startsWith("..") && !path3.isAbsolute(rel))
5359
6331
  return dir;
5360
6332
  throw new CheckoutRefusedError(
5361
6333
  `--target ${target} resolves outside the current directory (${cwd}) \u2014 run from that directory, or pass an absolute path`
5362
6334
  );
5363
6335
  }
5364
6336
  static fresh(slug, cwd) {
5365
- const base = path2.join(cwd, slug);
6337
+ const base = path3.join(cwd, slug);
5366
6338
  let dir = base;
5367
6339
  let i = 1;
5368
6340
  while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
@@ -5389,7 +6361,7 @@ var init_checkoutTarget = __esm({
5389
6361
  ].filter(_CheckoutTarget.confinedRel).sort();
5390
6362
  }
5391
6363
  static confinedRel(rel) {
5392
- if (rel === "" || path2.isAbsolute(rel)) return false;
6364
+ if (rel === "" || path3.isAbsolute(rel)) return false;
5393
6365
  return !rel.split(/[\\/]/).includes("..");
5394
6366
  }
5395
6367
  /** Why `dir` cannot be overwritten, or null when it is a clean checkout of `slug`. */
@@ -5407,9 +6379,9 @@ var init_checkoutTarget = __esm({
5407
6379
  /** Remove the replaced checkout's files (and the dirs they leave empty). */
5408
6380
  clear() {
5409
6381
  for (const rel of this.replaces) {
5410
- const abs = path2.join(this.dir, rel);
6382
+ const abs = path3.join(this.dir, rel);
5411
6383
  fs2.rmSync(abs, { force: true });
5412
- this.pruneEmptyParents(path2.dirname(abs));
6384
+ this.pruneEmptyParents(path3.dirname(abs));
5413
6385
  }
5414
6386
  }
5415
6387
  pruneEmptyParents(from) {
@@ -5417,7 +6389,7 @@ var init_checkoutTarget = __esm({
5417
6389
  while (cur !== this.dir && cur.startsWith(this.dir)) {
5418
6390
  if (fs2.existsSync(cur) && fs2.readdirSync(cur).length > 0) return;
5419
6391
  fs2.rmSync(cur, { recursive: true, force: true });
5420
- cur = path2.dirname(cur);
6392
+ cur = path3.dirname(cur);
5421
6393
  }
5422
6394
  }
5423
6395
  };
@@ -5425,13 +6397,13 @@ var init_checkoutTarget = __esm({
5425
6397
  });
5426
6398
 
5427
6399
  // src/worlds/worldDir.ts
5428
- var import_node_crypto5, fs3, path3, WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, BENCHMARK_CONFIG, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
6400
+ var import_node_crypto5, fs3, path4, WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, BENCHMARK_CONFIG, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
5429
6401
  var init_worldDir = __esm({
5430
6402
  "src/worlds/worldDir.ts"() {
5431
6403
  "use strict";
5432
6404
  import_node_crypto5 = require("crypto");
5433
6405
  fs3 = __toESM(require("fs"));
5434
- path3 = __toESM(require("path"));
6406
+ path4 = __toESM(require("path"));
5435
6407
  WORLD_MANIFEST = "gateway-env.toml";
5436
6408
  API_BUNDLE_MANIFEST = "gateway-api-world.toml";
5437
6409
  BUNDLED_WORLD_PREFIX = "server/world/";
@@ -5457,17 +6429,17 @@ var init_worldDir = __esm({
5457
6429
  worldRoot;
5458
6430
  pathsMemo;
5459
6431
  static open(dir) {
5460
- const present = (rel) => fs3.existsSync(path3.join(dir, rel));
6432
+ const present = (rel) => fs3.existsSync(path4.join(dir, rel));
5461
6433
  const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
5462
6434
  return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
5463
6435
  }
5464
6436
  // ── Files ──────────────────────────────────────────────────────────────
5465
6437
  has(rel) {
5466
- return fs3.existsSync(path3.join(this.dir, rel));
6438
+ return fs3.existsSync(path4.join(this.dir, rel));
5467
6439
  }
5468
6440
  text(rel) {
5469
6441
  try {
5470
- return fs3.readFileSync(path3.join(this.dir, rel), "utf-8");
6442
+ return fs3.readFileSync(path4.join(this.dir, rel), "utf-8");
5471
6443
  } catch {
5472
6444
  return null;
5473
6445
  }
@@ -5486,7 +6458,7 @@ var init_worldDir = __esm({
5486
6458
  const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
5487
6459
  const out = [];
5488
6460
  const walk = (rel) => {
5489
- for (const entry of fs3.readdirSync(rel ? path3.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
6461
+ for (const entry of fs3.readdirSync(rel ? path4.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
5490
6462
  const child = rel ? `${rel}/${entry.name}` : entry.name;
5491
6463
  if (entry.isDirectory()) {
5492
6464
  if (!skippedDir(entry.name)) walk(child);
@@ -5507,7 +6479,7 @@ var init_worldDir = __esm({
5507
6479
  get exclusions() {
5508
6480
  const kept = new Set(this.archivePaths);
5509
6481
  const out = [];
5510
- const filesUnder = (rel) => fs3.readdirSync(path3.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
6482
+ const filesUnder = (rel) => fs3.readdirSync(path4.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
5511
6483
  const child = `${rel}/${entry.name}`;
5512
6484
  if (entry.isDirectory()) return filesUnder(child);
5513
6485
  return entry.isFile() ? [child] : [];
@@ -5538,7 +6510,7 @@ var init_worldDir = __esm({
5538
6510
  const hasher = (0, import_node_crypto5.createHash)("sha256");
5539
6511
  for (const rel of this.archivePaths) {
5540
6512
  hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
5541
- hasher.update(fs3.readFileSync(path3.join(this.dir, rel)));
6513
+ hasher.update(fs3.readFileSync(path4.join(this.dir, rel)));
5542
6514
  }
5543
6515
  return hasher.digest("hex");
5544
6516
  }
@@ -5628,12 +6600,12 @@ function paxPath(body) {
5628
6600
  }
5629
6601
  return found;
5630
6602
  }
5631
- var fs4, path4, import_node_zlib, TAR_FILE_MODE, TAR_EXEC_MODE, TarArchive;
6603
+ var fs4, path5, import_node_zlib, TAR_FILE_MODE, TAR_EXEC_MODE, TarArchive;
5632
6604
  var init_tar = __esm({
5633
6605
  "src/worlds/tar.ts"() {
5634
6606
  "use strict";
5635
6607
  fs4 = __toESM(require("fs"));
5636
- path4 = __toESM(require("path"));
6608
+ path5 = __toESM(require("path"));
5637
6609
  import_node_zlib = require("zlib");
5638
6610
  TAR_FILE_MODE = 420;
5639
6611
  TAR_EXEC_MODE = 493;
@@ -5689,8 +6661,8 @@ var init_tar = __esm({
5689
6661
  for (const member of this.members()) {
5690
6662
  if (member.typeflag !== "0" && member.typeflag !== "\0") continue;
5691
6663
  if (!_TarArchive.isConfined(member.name)) continue;
5692
- const target = path4.join(dir, member.name);
5693
- fs4.mkdirSync(path4.dirname(target), { recursive: true });
6664
+ const target = path5.join(dir, member.name);
6665
+ fs4.mkdirSync(path5.dirname(target), { recursive: true });
5694
6666
  const mode = archiveMode(member.mode);
5695
6667
  fs4.writeFileSync(target, member.bytes, { mode });
5696
6668
  fs4.chmodSync(target, mode);
@@ -5793,7 +6765,7 @@ function tarMember(name, payload, mode) {
5793
6765
  function buildSourceTarball(dir) {
5794
6766
  const parts = [];
5795
6767
  for (const relPath of collectArchiveRelPaths(dir)) {
5796
- const abs = path5.join(dir, relPath);
6768
+ const abs = path6.join(dir, relPath);
5797
6769
  parts.push(
5798
6770
  ...tarMember(
5799
6771
  relPath,
@@ -5807,7 +6779,7 @@ function buildSourceTarball(dir) {
5807
6779
  }
5808
6780
  function readLocalMeta(dir) {
5809
6781
  try {
5810
- const raw = fs5.readFileSync(path5.join(dir, META_RELPATH), "utf-8");
6782
+ const raw = fs5.readFileSync(path6.join(dir, META_RELPATH), "utf-8");
5811
6783
  const parsed = JSON.parse(raw);
5812
6784
  return parsed && typeof parsed === "object" ? parsed : null;
5813
6785
  } catch {
@@ -5830,16 +6802,16 @@ function resolvePushSlug(dir, opts = {}) {
5830
6802
  }
5831
6803
  const first = declared[0];
5832
6804
  if (first) return { ...base, slug: first.slug, source: first.source };
5833
- return { ...base, slug: path5.basename(path5.resolve(dir)), source: "directory name" };
6805
+ return { ...base, slug: path6.basename(path6.resolve(dir)), source: "directory name" };
5834
6806
  }
5835
6807
  function writeLocalMeta(dir, meta) {
5836
- fs5.mkdirSync(path5.join(dir, ".gateway"), { recursive: true });
5837
- fs5.writeFileSync(path5.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
6808
+ fs5.mkdirSync(path6.join(dir, ".gateway"), { recursive: true });
6809
+ fs5.writeFileSync(path6.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
5838
6810
  }
5839
6811
  function snapshotTreeHashes(dir) {
5840
6812
  const out = {};
5841
6813
  for (const relPath of collectArchiveRelPaths(dir)) {
5842
- out[relPath] = (0, import_node_crypto6.createHash)("sha256").update(fs5.readFileSync(path5.join(dir, relPath))).digest("hex");
6814
+ out[relPath] = (0, import_node_crypto6.createHash)("sha256").update(fs5.readFileSync(path6.join(dir, relPath))).digest("hex");
5843
6815
  }
5844
6816
  return out;
5845
6817
  }
@@ -5857,7 +6829,7 @@ function localTreeState(dir) {
5857
6829
  }
5858
6830
  function readPyproject(dir) {
5859
6831
  try {
5860
- const text2 = fs5.readFileSync(path5.join(dir, "pyproject.toml"), "utf-8");
6832
+ const text2 = fs5.readFileSync(path6.join(dir, "pyproject.toml"), "utf-8");
5861
6833
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
5862
6834
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
5863
6835
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -5880,7 +6852,7 @@ function readWorldContractVersion(dir) {
5880
6852
  }
5881
6853
  function buildFileManifest(dir) {
5882
6854
  return collectArchiveRelPaths(dir).map((relPath) => {
5883
- const payload = fs5.readFileSync(path5.join(dir, relPath));
6855
+ const payload = fs5.readFileSync(path6.join(dir, relPath));
5884
6856
  return {
5885
6857
  path: relPath,
5886
6858
  isDirectory: false,
@@ -5926,7 +6898,7 @@ async function push(dir, opts = {}) {
5926
6898
  const pyproject = readPyproject(dir);
5927
6899
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
5928
6900
  if (contractVersion) pyproject.version = contractVersion;
5929
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path5.basename(path5.resolve(dir));
6901
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path6.basename(path6.resolve(dir));
5930
6902
  const metadata = {
5931
6903
  ...opts.name !== void 0 ? { name: opts.name } : {},
5932
6904
  ...opts.description !== void 0 ? { description: opts.description } : {},
@@ -6005,7 +6977,8 @@ function checkoutIdentity(ref, resolved) {
6005
6977
  async function pull(slugRef, target, options = {}, opts = {}) {
6006
6978
  const at = slugRef.indexOf("@");
6007
6979
  const slug = at === -1 ? slugRef : slugRef.slice(0, at);
6008
- const ref = at === -1 ? "latest" : slugRef.slice(at + 1);
6980
+ const asked = at === -1 ? void 0 : slugRef.slice(at + 1);
6981
+ const ref = asked ?? "latest";
6009
6982
  const landing = CheckoutTarget.resolve(slug, target, {
6010
6983
  cwd: process.cwd(),
6011
6984
  force: opts.force ?? false,
@@ -6013,7 +6986,7 @@ async function pull(slugRef, target, options = {}, opts = {}) {
6013
6986
  });
6014
6987
  const details = await apiRequest(options, "POST", "/api/public/benchmark-containers/resolve-version", {
6015
6988
  slug,
6016
- ref
6989
+ ...asked ? { ref: asked } : {}
6017
6990
  });
6018
6991
  if (!details.downloadUrl) {
6019
6992
  throw new Error(`${slug}@${ref} resolved but has no uploaded bundle`);
@@ -6052,7 +7025,7 @@ async function propose(dir, opts) {
6052
7025
  if (state2.clean)
6053
7026
  throw new Error("Nothing to propose \u2014 the tree matches its base.");
6054
7027
  const changes = state2.dirty.map((relPath) => {
6055
- const abs = path5.join(dir, relPath);
7028
+ const abs = path6.join(dir, relPath);
6056
7029
  if (!fs5.existsSync(abs))
6057
7030
  return { path: relPath, action: "delete" };
6058
7031
  return {
@@ -6084,20 +7057,20 @@ async function createRef(slug, name, kind, versionId, options = {}) {
6084
7057
  ...versionId ? { versionId } : {}
6085
7058
  });
6086
7059
  }
6087
- var import_node_crypto6, import_node_zlib2, fs5, path5, DEFAULT_BRANCH, META_RELPATH, FILE_MODE, MINTED_SUFFIX, PushSlugConflict, sha256b64, DetachedPushError;
7060
+ var import_node_crypto6, import_node_zlib2, fs5, path6, DEFAULT_BRANCH, META_RELPATH, FILE_MODE, MINTED_SUFFIX, PushSlugConflict, sha256b64, DetachedPushError;
6088
7061
  var init_hub = __esm({
6089
7062
  "src/worlds/hub.ts"() {
6090
7063
  "use strict";
6091
7064
  import_node_crypto6 = require("crypto");
6092
7065
  import_node_zlib2 = require("zlib");
6093
7066
  fs5 = __toESM(require("fs"));
6094
- path5 = __toESM(require("path"));
7067
+ path6 = __toESM(require("path"));
6095
7068
  init_worlds();
6096
7069
  init_checkoutTarget();
6097
7070
  init_worldDir();
6098
7071
  init_tar();
6099
7072
  DEFAULT_BRANCH = "main";
6100
- META_RELPATH = path5.join(".gateway", ".env-metadata.json");
7073
+ META_RELPATH = path6.join(".gateway", ".env-metadata.json");
6101
7074
  FILE_MODE = TAR_FILE_MODE;
6102
7075
  MINTED_SUFFIX = /^-\d+$/;
6103
7076
  PushSlugConflict = class extends Error {
@@ -6203,7 +7176,7 @@ async function validateWorld(dir, opts) {
6203
7176
  options,
6204
7177
  "POST",
6205
7178
  "/api/public/benchmark-containers/resolve-version",
6206
- { slug, ref: branch }
7179
+ { slug, ...branch === DEFAULT_BRANCH ? {} : { ref: branch } }
6207
7180
  );
6208
7181
  tipVersionId = tip.versionId ?? null;
6209
7182
  } catch {
@@ -6315,6 +7288,10 @@ function parseSlugRef(slugRef) {
6315
7288
  if (at === -1) return { slug: slugRef };
6316
7289
  return { slug: slugRef.slice(0, at), ref: slugRef.slice(at + 1) };
6317
7290
  }
7291
+ function nameGlob(pattern) {
7292
+ const literal = (part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
7293
+ return new RegExp(`^${pattern.split("*").map(literal).join(".*")}$`);
7294
+ }
6318
7295
  function rebaseConflicts(error) {
6319
7296
  if (!(error instanceof ApiError) || error.status !== 409) return null;
6320
7297
  let body;
@@ -6325,14 +7302,15 @@ function rebaseConflicts(error) {
6325
7302
  }
6326
7303
  const conflicts = body?.conflicts;
6327
7304
  if (!Array.isArray(conflicts)) return null;
6328
- return conflicts.filter(isConflictPath).map(({ path: path12, reason }) => ({ path: path12, reason }));
7305
+ return conflicts.filter(isConflictPath).map(({ path: path13, reason }) => ({ path: path13, reason }));
6329
7306
  }
6330
7307
  function isConflictPath(value) {
6331
7308
  const entry = value;
6332
7309
  return typeof entry?.path === "string" && typeof entry.reason === "string";
6333
7310
  }
6334
7311
  function uncommittedPaths(error) {
6335
- if (!(error instanceof ApiError) || error.status !== 409 || error.code !== "sandbox_uncommitted") return null;
7312
+ if (!(error instanceof ApiError) || error.status !== 409 || error.code !== "sandbox_uncommitted")
7313
+ return null;
6336
7314
  let body;
6337
7315
  try {
6338
7316
  body = JSON.parse(error.text);
@@ -6343,6 +7321,53 @@ function uncommittedPaths(error) {
6343
7321
  if (!Array.isArray(uncommitted)) return null;
6344
7322
  return uncommitted.filter((p) => typeof p === "string");
6345
7323
  }
7324
+ function carriedWarnings(error) {
7325
+ const kept = typeof error === "object" && error !== null ? carried.get(error) ?? [] : [];
7326
+ const shown = error instanceof Error ? error.message : "";
7327
+ return unique([...kept, ...bodyWarnings(error)]).filter((warning) => !shown.includes(warning));
7328
+ }
7329
+ function carrying(error, warnings) {
7330
+ if (warnings.length > 0 && typeof error === "object" && error !== null)
7331
+ carried.set(error, unique([...carried.get(error) ?? [], ...warnings]));
7332
+ return error;
7333
+ }
7334
+ function unique(warnings) {
7335
+ return [...new Set(warnings)];
7336
+ }
7337
+ function bodyWarnings(error) {
7338
+ if (!(error instanceof ApiError)) return [];
7339
+ let body;
7340
+ try {
7341
+ body = JSON.parse(error.text);
7342
+ } catch {
7343
+ return [];
7344
+ }
7345
+ const warnings = body?.warnings;
7346
+ if (!Array.isArray(warnings)) return [];
7347
+ return warnings.filter((w) => typeof w === "string");
7348
+ }
7349
+ function deferredWarnings(error) {
7350
+ if (!(error instanceof ApiError) || error.status !== 409 || error.code !== "sandbox_exec_deferred") return null;
7351
+ return bodyWarnings(error);
7352
+ }
7353
+ function rowsRefusal(error) {
7354
+ if (!(error instanceof ApiError) || error.status !== 400 || error.code !== "rows_refused") return null;
7355
+ let body;
7356
+ try {
7357
+ body = JSON.parse(error.text);
7358
+ } catch {
7359
+ return null;
7360
+ }
7361
+ const { message, batchId, refusalCount, refusals, reportUrl } = body;
7362
+ if (typeof batchId !== "string" || typeof refusalCount !== "number" || !Array.isArray(refusals)) return null;
7363
+ return {
7364
+ message: typeof message === "string" ? message : "",
7365
+ batchId,
7366
+ refusalCount,
7367
+ refusals: refusals.filter((r) => typeof r === "object" && r !== null),
7368
+ reportUrl: typeof reportUrl === "string" ? reportUrl : null
7369
+ };
7370
+ }
6346
7371
  function listQuery(filter) {
6347
7372
  const params = new URLSearchParams();
6348
7373
  if (filter.status) params.set("status", filter.status);
@@ -6351,22 +7376,59 @@ function listQuery(filter) {
6351
7376
  const qs = params.toString();
6352
7377
  return qs ? `?${qs}` : "";
6353
7378
  }
6354
- var isSandboxImportPending, sid, SANDBOXES, SANDBOX_REQUEST_TIMEOUT_MS, WorldSandboxClient;
7379
+ var import_node_crypto7, isSandboxCommitPending, isSandboxImportPending, sid, SANDBOXES, SANDBOX_REQUEST_TIMEOUT_MS, SANDBOX_WRITE_CHUNK_BYTES, SANDBOX_FILE_MAX_BYTES, SANDBOX_WRITE_SKIPPED, WriteTreeFilter, SANDBOX_EXEC_WAIT_MS, SANDBOX_EXEC_TIMEOUT_MAX_S, SANDBOX_EXEC_REPORT_MARGIN_MS, SANDBOX_EXEC_START_ATTEMPTS, carried, WorldSandboxClient;
6355
7380
  var init_sandbox = __esm({
6356
7381
  "src/worlds/sandbox.ts"() {
6357
7382
  "use strict";
7383
+ import_node_crypto7 = require("crypto");
6358
7384
  init_worlds();
7385
+ isSandboxCommitPending = (answer) => !("versionId" in answer);
6359
7386
  isSandboxImportPending = (answer) => !("versionId" in answer);
6360
7387
  sid = encodeURIComponent;
6361
7388
  SANDBOXES = "/api/public/benchmark-containers/sandboxes";
6362
- SANDBOX_REQUEST_TIMEOUT_MS = 66e4;
7389
+ SANDBOX_REQUEST_TIMEOUT_MS = 29e4;
7390
+ SANDBOX_WRITE_CHUNK_BYTES = 4 * 1024 * 1024;
7391
+ SANDBOX_FILE_MAX_BYTES = 256 * 1024 * 1024;
7392
+ SANDBOX_WRITE_SKIPPED = [
7393
+ ".git/",
7394
+ ".gateway/",
7395
+ "scratch/",
7396
+ ".scratch/",
7397
+ "__pycache__/",
7398
+ "*.pyc",
7399
+ ".pytest_cache/",
7400
+ ".mypy_cache/",
7401
+ ".ruff_cache/",
7402
+ "node_modules/",
7403
+ ".venv/",
7404
+ "venv/",
7405
+ ".DS_Store",
7406
+ "*.swp"
7407
+ ];
7408
+ WriteTreeFilter = class {
7409
+ directories = new Set(
7410
+ SANDBOX_WRITE_SKIPPED.filter((pattern) => pattern.endsWith("/")).map((pattern) => pattern.slice(0, -1))
7411
+ );
7412
+ files = SANDBOX_WRITE_SKIPPED.filter((pattern) => !pattern.endsWith("/")).map(nameGlob);
7413
+ skipsDirectory(name) {
7414
+ return this.directories.has(name);
7415
+ }
7416
+ skipsFile(name) {
7417
+ return this.files.some((glob) => glob.test(name));
7418
+ }
7419
+ };
7420
+ SANDBOX_EXEC_WAIT_MS = 4e4;
7421
+ SANDBOX_EXEC_TIMEOUT_MAX_S = 600;
7422
+ SANDBOX_EXEC_REPORT_MARGIN_MS = 35e3;
7423
+ SANDBOX_EXEC_START_ATTEMPTS = 3;
7424
+ carried = /* @__PURE__ */ new WeakMap();
6363
7425
  WorldSandboxClient = class {
6364
7426
  constructor(options = {}) {
6365
7427
  this.options = options;
6366
7428
  }
6367
7429
  options;
6368
- request(method, path12, body) {
6369
- return apiRequest(this.options, method, path12, body, {
7430
+ request(method, path13, body) {
7431
+ return apiRequest(this.options, method, path13, body, {
6370
7432
  timeoutMs: SANDBOX_REQUEST_TIMEOUT_MS
6371
7433
  });
6372
7434
  }
@@ -6384,7 +7446,10 @@ var init_sandbox = __esm({
6384
7446
  }
6385
7447
  /** The project's sandboxes, newest first — how an agent finds one it left open. */
6386
7448
  async list(filter = {}) {
6387
- const res = await this.request("GET", `${SANDBOXES}${listQuery(filter)}`);
7449
+ const res = await this.request(
7450
+ "GET",
7451
+ `${SANDBOXES}${listQuery(filter)}`
7452
+ );
6388
7453
  return res.sandboxes;
6389
7454
  }
6390
7455
  async read(sandboxId, filePath) {
@@ -6392,16 +7457,72 @@ var init_sandbox = __esm({
6392
7457
  "GET",
6393
7458
  `${SANDBOXES}/${sid(sandboxId)}/file?path=${encodeURIComponent(filePath)}`
6394
7459
  );
6395
- return { path: res.path, size: res.size, bytes: Buffer.from(res.contentBase64, "base64") };
7460
+ return {
7461
+ path: res.path,
7462
+ size: res.size,
7463
+ bytes: Buffer.from(res.contentBase64, "base64")
7464
+ };
6396
7465
  }
6397
- write(sandboxId, filePath, bytes) {
6398
- return this.request("PUT", `${SANDBOXES}/${sid(sandboxId)}/file`, {
6399
- path: filePath,
6400
- contentBase64: bytes.toString("base64")
6401
- });
7466
+ /** A file past one chunk goes up in parts under one upload id; the platform lands it whole once the last part's
7467
+ * size and SHA-256 check out. */
7468
+ async write(sandboxId, filePath, bytes) {
7469
+ if (bytes.length <= SANDBOX_WRITE_CHUNK_BYTES)
7470
+ return this.putFile(sandboxId, { path: filePath, contentBase64: bytes.toString("base64") });
7471
+ const whole = { upload: (0, import_node_crypto7.randomUUID)(), size: bytes.length, sha256: (0, import_node_crypto7.createHash)("sha256").update(bytes).digest("hex") };
7472
+ let answer = { path: filePath, received: 0 };
7473
+ for (let offset = 0; offset < bytes.length; offset += SANDBOX_WRITE_CHUNK_BYTES) {
7474
+ const part = bytes.subarray(offset, offset + SANDBOX_WRITE_CHUNK_BYTES);
7475
+ answer = await this.putFile(sandboxId, {
7476
+ path: filePath,
7477
+ contentBase64: part.toString("base64"),
7478
+ ...whole,
7479
+ offset,
7480
+ last: offset + part.length === bytes.length
7481
+ });
7482
+ }
7483
+ return answer;
7484
+ }
7485
+ putFile(sandboxId, body) {
7486
+ return this.request("PUT", `${SANDBOXES}/${sid(sandboxId)}/file`, body);
7487
+ }
7488
+ /** Runs as a job on the platform: a command that outlasts one request is waited on again until its result. */
7489
+ async exec(sandboxId, input) {
7490
+ const budgetMs = (input.timeoutMs ?? SANDBOX_EXEC_TIMEOUT_MAX_S * 1e3) + SANDBOX_EXEC_REPORT_MARGIN_MS;
7491
+ let answer = await this.startExec(sandboxId, { ...input, waitMs: SANDBOX_EXEC_WAIT_MS });
7492
+ const deadline = Date.now() + budgetMs;
7493
+ const warnings = [...answer.warnings ?? []];
7494
+ try {
7495
+ while ("state" in answer) {
7496
+ if (Date.now() >= deadline)
7497
+ throw new Error(
7498
+ `exec ${answer.execId} in sandbox ${sandboxId} has not reported back within ${budgetMs / 1e3} s, past its timeout: the sandbox may have restarted. Check the sandbox's status, then run the command again.`
7499
+ );
7500
+ answer = await this.execStep(sandboxId, { execId: answer.execId, waitMs: SANDBOX_EXEC_WAIT_MS });
7501
+ warnings.push(...answer.warnings ?? []);
7502
+ }
7503
+ } catch (error) {
7504
+ throw carrying(error, warnings);
7505
+ }
7506
+ return { ...answer, warnings };
7507
+ }
7508
+ // A deferred start ran nothing (the platform was still getting the sandbox ready), so starting again is safe. What
7509
+ // getting ready found rides on the deferral alone (no later call reports it), so it is kept for the answer, or for
7510
+ // the error when a later attempt fails.
7511
+ async startExec(sandboxId, body) {
7512
+ const kept = [];
7513
+ for (let attempt = 1; ; attempt += 1) {
7514
+ try {
7515
+ const answer = await this.execStep(sandboxId, body);
7516
+ return { ...answer, warnings: unique([...kept, ...answer.warnings ?? []]) };
7517
+ } catch (error) {
7518
+ const warnings = deferredWarnings(error);
7519
+ if (warnings === null || attempt === SANDBOX_EXEC_START_ATTEMPTS) throw carrying(error, kept);
7520
+ kept.push(...warnings);
7521
+ }
7522
+ }
6402
7523
  }
6403
- exec(sandboxId, input) {
6404
- return this.request("POST", `${SANDBOXES}/${sid(sandboxId)}/exec`, input);
7524
+ execStep(sandboxId, body) {
7525
+ return this.request("POST", `${SANDBOXES}/${sid(sandboxId)}/exec`, body);
6405
7526
  }
6406
7527
  check(sandboxId) {
6407
7528
  return this.request("POST", `${SANDBOXES}/${sid(sandboxId)}/check`, {});
@@ -6409,7 +7530,11 @@ var init_sandbox = __esm({
6409
7530
  /** With `rebase`, a moved base is replayed onto; a 409 then names the conflicting paths (`rebaseConflicts`). */
6410
7531
  commit(sandboxId, input) {
6411
7532
  const { rebase, ...rest } = input;
6412
- return this.request("POST", `${SANDBOXES}/${sid(sandboxId)}/commit`, rebase ? { ...rest, rebase: true } : rest);
7533
+ return this.request(
7534
+ "POST",
7535
+ `${SANDBOXES}/${sid(sandboxId)}/commit`,
7536
+ rebase ? { ...rest, rebase: true } : rest
7537
+ );
6413
7538
  }
6414
7539
  /**
6415
7540
  * A rows file already in the sandbox tree into the PLATFORM world the
@@ -6422,7 +7547,11 @@ var init_sandbox = __esm({
6422
7547
  }
6423
7548
  /** Uncommitted work is a 409 (`uncommittedPaths`) unless `force` discards it. */
6424
7549
  close(sandboxId, opts = {}) {
6425
- return this.request("DELETE", `${SANDBOXES}/${sid(sandboxId)}`, opts.force ? { force: true } : void 0);
7550
+ return this.request(
7551
+ "DELETE",
7552
+ `${SANDBOXES}/${sid(sandboxId)}`,
7553
+ opts.force ? { force: true } : void 0
7554
+ );
6426
7555
  }
6427
7556
  };
6428
7557
  }
@@ -6758,11 +7887,11 @@ function applyTransforms(value, chain) {
6758
7887
  for (const step of steps) out = step(out);
6759
7888
  return out;
6760
7889
  }
6761
- var import_node_crypto7, WHITESPACE, WHITESPACE_EDGES, WHITESPACE_RUN, NOT_DIGIT, STRIP_PREFIX, trim, emailLocalPart, emailDomain, stripPrefix, hex, SIMPLE, TRANSFORMS, text;
7890
+ var import_node_crypto8, WHITESPACE, WHITESPACE_EDGES, WHITESPACE_RUN, NOT_DIGIT, STRIP_PREFIX, trim, emailLocalPart, emailDomain, stripPrefix, hex, SIMPLE, TRANSFORMS, text;
6762
7891
  var init_transforms = __esm({
6763
7892
  "src/worlds/transforms.ts"() {
6764
7893
  "use strict";
6765
- import_node_crypto7 = require("crypto");
7894
+ import_node_crypto8 = require("crypto");
6766
7895
  init_json();
6767
7896
  WHITESPACE = " \n\r\f\v";
6768
7897
  WHITESPACE_EDGES = new RegExp(`^[${WHITESPACE}]+|[${WHITESPACE}]+$`, "g");
@@ -6779,7 +7908,7 @@ var init_transforms = __esm({
6779
7908
  return at === -1 ? "" : value.slice(at + 1).toLowerCase();
6780
7909
  };
6781
7910
  stripPrefix = (prefix) => (value) => value.startsWith(prefix) ? value.slice(prefix.length) : value;
6782
- hex = (algorithm, value) => (0, import_node_crypto7.createHash)(algorithm).update(value, "utf8").digest("hex");
7911
+ hex = (algorithm, value) => (0, import_node_crypto8.createHash)(algorithm).update(value, "utf8").digest("hex");
6783
7912
  SIMPLE = {
6784
7913
  lower: (value) => value.toLowerCase(),
6785
7914
  upper: (value) => value.toUpperCase(),
@@ -6811,7 +7940,7 @@ function pyText(value) {
6811
7940
  return pyFloatRepr(value);
6812
7941
  }
6813
7942
  function hashForm(slug, plaintext) {
6814
- return "sha256:" + (0, import_node_crypto8.createHash)("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
7943
+ return "sha256:" + (0, import_node_crypto9.createHash)("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
6815
7944
  }
6816
7945
  function preserveForm(slug, plaintext) {
6817
7946
  const kind = typeof plaintext === "string" ? "str" : isInt(plaintext) ? "int" : "float";
@@ -6820,12 +7949,12 @@ function preserveForm(slug, plaintext) {
6820
7949
  }
6821
7950
  function shapeText(slug, text2, kind) {
6822
7951
  const seed = Buffer.from(`${slug}:${text2}`, "utf8");
6823
- let digest = (0, import_node_crypto8.createHash)("sha256").update(seed).digest();
7952
+ let digest = (0, import_node_crypto9.createHash)("sha256").update(seed).digest();
6824
7953
  let cursor = 0;
6825
7954
  const out = [];
6826
7955
  for (const char of text2) {
6827
7956
  if (cursor === digest.length) {
6828
- digest = (0, import_node_crypto8.createHash)("sha256").update(Buffer.concat([digest, seed])).digest();
7957
+ digest = (0, import_node_crypto9.createHash)("sha256").update(Buffer.concat([digest, seed])).digest();
6829
7958
  cursor = 0;
6830
7959
  }
6831
7960
  const byte = digest[cursor];
@@ -6862,11 +7991,11 @@ function storedForms(world, field2, plaintext, transform) {
6862
7991
  if (policy !== null) return [roundForm(plaintext, world.redact.round[field2])];
6863
7992
  return [plaintext];
6864
7993
  }
6865
- var import_node_crypto8, isInt;
7994
+ var import_node_crypto9, isInt;
6866
7995
  var init_redaction = __esm({
6867
7996
  "src/worlds/redaction.ts"() {
6868
7997
  "use strict";
6869
- import_node_crypto8 = require("crypto");
7998
+ import_node_crypto9 = require("crypto");
6870
7999
  init_json();
6871
8000
  init_transforms();
6872
8001
  isInt = (value) => typeof value === "number" && Number.isInteger(value);
@@ -6897,6 +8026,17 @@ async function queryRows(slugRef, entity, opts = {}, options = {}) {
6897
8026
  `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
6898
8027
  );
6899
8028
  }
8029
+ async function sqlRows(slugRef, sql, opts = {}, options = {}) {
8030
+ const [slug, ref] = slugRef.split("@", 2);
8031
+ if (!slug || ref === "") throw new Error(`sqlRows needs a slug, optionally slug@ref (got ${slugRef})`);
8032
+ if (!sql.trim()) throw new Error("sqlRows needs one SQL statement");
8033
+ return apiRequest(options, "POST", `/api/public/worlds/${encodeURIComponent(slug)}/data/sql`, {
8034
+ sql,
8035
+ ...ref ? { ref } : {},
8036
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
8037
+ ...opts.scenario ? { scenario: opts.scenario } : {}
8038
+ });
8039
+ }
6900
8040
  var init_query = __esm({
6901
8041
  "src/worlds/query.ts"() {
6902
8042
  "use strict";
@@ -6956,19 +8096,19 @@ async function api(creds, method, route, body, init2 = {}) {
6956
8096
  if (!reply.ok) throw new ApiError2(reply.status, method, route, reply.text);
6957
8097
  return reply.text ? JSON.parse(reply.text) : {};
6958
8098
  }
6959
- var fs6, os, path6, ApiError2;
8099
+ var fs6, os, path7, ApiError2;
6960
8100
  var init_config = __esm({
6961
8101
  "src/cli/config.ts"() {
6962
8102
  "use strict";
6963
8103
  fs6 = __toESM(require("fs"));
6964
8104
  os = __toESM(require("os"));
6965
- path6 = __toESM(require("path"));
8105
+ path7 = __toESM(require("path"));
6966
8106
  init_router();
6967
8107
  init_worlds();
6968
8108
  init_transport();
6969
8109
  ApiError2 = class extends CliError {
6970
8110
  constructor(status, method, route, text2) {
6971
- const body = parseErrorBody(text2);
8111
+ const body = parseErrorBody(text2, status);
6972
8112
  super(`${method} ${route} -> ${status}: ${body.detail}`, 1);
6973
8113
  this.status = status;
6974
8114
  this.method = method;
@@ -6986,6 +8126,14 @@ var init_config = __esm({
6986
8126
  });
6987
8127
 
6988
8128
  // src/cli/runtime/local.ts
8129
+ function forwardedEnv() {
8130
+ return Object.fromEntries(
8131
+ FORWARDED_ENV.flatMap((name) => {
8132
+ const value = process.env[name];
8133
+ return value ? [[name, value]] : [];
8134
+ })
8135
+ );
8136
+ }
6989
8137
  function tailOf(output) {
6990
8138
  return output.length > DIAGNOSTIC_TAIL ? `\u2026${output.slice(-DIAGNOSTIC_TAIL)}` : output;
6991
8139
  }
@@ -7000,16 +8148,16 @@ function runtimeRoot() {
7000
8148
  const candidates = [];
7001
8149
  if (process.env.GATEWAY_RUNTIME_PATH)
7002
8150
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
7003
- const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path7.dirname(fs7.realpathSync(process.argv[1])) : null;
8151
+ const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path8.dirname(fs7.realpathSync(process.argv[1])) : null;
7004
8152
  if (entry) {
7005
- candidates.push(path7.resolve(entry, "..", "runtime"));
7006
- candidates.push(path7.resolve(entry, "..", "..", "runtime"));
7007
- candidates.push(path7.resolve(entry, "..", "..", "sdk", "src"));
8153
+ candidates.push(path8.resolve(entry, "..", "runtime"));
8154
+ candidates.push(path8.resolve(entry, "..", "..", "runtime"));
8155
+ candidates.push(path8.resolve(entry, "..", "..", "sdk", "src"));
7008
8156
  }
7009
- candidates.push(path7.resolve(process.cwd(), "runtime"));
8157
+ candidates.push(path8.resolve(process.cwd(), "runtime"));
7010
8158
  for (const root of candidates)
7011
8159
  if (fs7.existsSync(
7012
- path7.join(root, "gatewaysdk", "environment", "schema", "platform.py")
8160
+ path8.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7013
8161
  ))
7014
8162
  return root;
7015
8163
  return null;
@@ -7098,13 +8246,12 @@ async function localBridge(request, local, opts = {}) {
7098
8246
  ["-I", "-B", "-c", BRIDGE_BOOTSTRAP, local.root],
7099
8247
  {
7100
8248
  stdio: ["pipe", "pipe", "pipe"],
7101
- // This is the author's machine running the author's world: the one
7102
- // place the runtime may import and run a world's own Python (check's
7103
- // handler execution, `worlds call`, `worlds test` tool cases).
8249
+ // The author's machine running the author's world: the one place the runtime may run its Python.
7104
8250
  env: {
7105
8251
  PATH: process.env.PATH ?? "",
7106
8252
  HOME: process.env.HOME ?? "",
7107
8253
  TMPDIR: process.env.TMPDIR ?? "",
8254
+ ...forwardedEnv(),
7108
8255
  GATEWAY_RUNTIME_EXECUTE: "1"
7109
8256
  }
7110
8257
  }
@@ -7150,19 +8297,20 @@ async function localBridge(request, local, opts = {}) {
7150
8297
  child.stdin.end(input);
7151
8298
  });
7152
8299
  }
7153
- var import_node_child_process2, fs7, net, path7, MIN_PYTHON, MIN_PYTHON_LABEL, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
8300
+ var import_node_child_process2, fs7, net, path8, MIN_PYTHON, MIN_PYTHON_LABEL, DIAGNOSTIC_TAIL, FORWARDED_ENV, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
7154
8301
  var init_local = __esm({
7155
8302
  "src/cli/runtime/local.ts"() {
7156
8303
  "use strict";
7157
8304
  import_node_child_process2 = require("child_process");
7158
8305
  fs7 = __toESM(require("fs"));
7159
8306
  net = __toESM(require("net"));
7160
- path7 = __toESM(require("path"));
8307
+ path8 = __toESM(require("path"));
7161
8308
  init_router();
7162
8309
  init_config();
7163
8310
  MIN_PYTHON = [3, 12];
7164
8311
  MIN_PYTHON_LABEL = MIN_PYTHON.join(".");
7165
8312
  DIAGNOSTIC_TAIL = 600;
8313
+ FORWARDED_ENV = ["VIRTUAL_ENV", "GATEWAY_SNAPSHOT_STORE"];
7166
8314
  BRIDGE_BOOTSTRAP = `import sys,runpy,types,os
7167
8315
  root=sys.argv[1]
7168
8316
  package=types.ModuleType('gatewaysdk')
@@ -7180,16 +8328,44 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
7180
8328
  }
7181
8329
  });
7182
8330
 
8331
+ // src/worlds/databaseEngines.ts
8332
+ function isWorldSnapshotOutput(path13) {
8333
+ return SNAPSHOT_OUTPUTS.has(path13.slice(path13.lastIndexOf("/") + 1));
8334
+ }
8335
+ var WORLD_DATABASE_ENGINES, DEFAULT_WORLD_DATABASE_ENGINE, WORLD_SNAPSHOT_FILE_PATH, WORLD_SNAPSHOT_SIDECAR_PATH, WORLD_SNAPSHOT_CONTENT_TYPE, SNAPSHOT_OUTPUTS;
8336
+ var init_databaseEngines = __esm({
8337
+ "src/worlds/databaseEngines.ts"() {
8338
+ "use strict";
8339
+ WORLD_DATABASE_ENGINES = [
8340
+ {
8341
+ engine: "sqlite",
8342
+ label: "SQLite",
8343
+ target: "sqlite-json/1",
8344
+ snapshotName: "state.sqlite",
8345
+ contentType: "application/vnd.sqlite3"
8346
+ }
8347
+ ];
8348
+ DEFAULT_WORLD_DATABASE_ENGINE = WORLD_DATABASE_ENGINES[0];
8349
+ WORLD_SNAPSHOT_FILE_PATH = `db/${DEFAULT_WORLD_DATABASE_ENGINE.snapshotName}`;
8350
+ WORLD_SNAPSHOT_SIDECAR_PATH = "db/state.json";
8351
+ WORLD_SNAPSHOT_CONTENT_TYPE = DEFAULT_WORLD_DATABASE_ENGINE.contentType;
8352
+ SNAPSHOT_OUTPUTS = /* @__PURE__ */ new Set([
8353
+ ...WORLD_DATABASE_ENGINES.map((engine) => engine.snapshotName),
8354
+ WORLD_SNAPSHOT_SIDECAR_PATH.slice("db/".length)
8355
+ ]);
8356
+ }
8357
+ });
8358
+
7183
8359
  // src/cli/runtime/client.ts
7184
8360
  function isBuildOutput(relPath) {
7185
- return BUILD_OUTPUT.test(relPath);
8361
+ return isWorldSnapshotOutput(relPath);
7186
8362
  }
7187
8363
  function readWorldFiles(dir, opts = {}) {
7188
8364
  if (!fs8.existsSync(dir) || !fs8.statSync(dir).isDirectory())
7189
8365
  throw new CliError(`Not a directory: ${dir}`);
7190
8366
  const out = [];
7191
8367
  const walk = (rel) => {
7192
- const abs = rel ? path8.join(dir, rel) : dir;
8368
+ const abs = rel ? path9.join(dir, rel) : dir;
7193
8369
  const entries = fs8.readdirSync(abs, { withFileTypes: true });
7194
8370
  entries.sort((a, b) => a.name.localeCompare(b.name));
7195
8371
  for (const entry of entries) {
@@ -7200,7 +8376,7 @@ function readWorldFiles(dir, opts = {}) {
7200
8376
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
7201
8377
  out.push({
7202
8378
  path: childRel,
7203
- content: fs8.readFileSync(path8.join(dir, childRel)).toString("base64")
8379
+ content: fs8.readFileSync(path9.join(dir, childRel)).toString("base64")
7204
8380
  });
7205
8381
  }
7206
8382
  }
@@ -7220,15 +8396,15 @@ function writeWorldFiles(dir, files, opts = {}) {
7220
8396
  if (opts.clean && fs8.existsSync(dir)) {
7221
8397
  const keep = new Set(files.map((f) => f.path));
7222
8398
  for (const existing of readWorldFiles(dir)) {
7223
- if (!keep.has(existing.path)) fs8.rmSync(path8.join(dir, existing.path));
8399
+ if (!keep.has(existing.path)) fs8.rmSync(path9.join(dir, existing.path));
7224
8400
  }
7225
8401
  pruneEmptyDirs(dir);
7226
8402
  }
7227
8403
  fs8.mkdirSync(dir, { recursive: true });
7228
8404
  const written = [];
7229
8405
  for (const file of files) {
7230
- const target = path8.join(dir, file.path);
7231
- fs8.mkdirSync(path8.dirname(target), { recursive: true });
8406
+ const target = path9.join(dir, file.path);
8407
+ fs8.mkdirSync(path9.dirname(target), { recursive: true });
7232
8408
  fs8.writeFileSync(target, Buffer.from(file.content, "base64"));
7233
8409
  written.push(file.path);
7234
8410
  }
@@ -7238,7 +8414,7 @@ function pruneEmptyDirs(root) {
7238
8414
  const walk = (abs) => {
7239
8415
  let empty = true;
7240
8416
  for (const entry of fs8.readdirSync(abs, { withFileTypes: true })) {
7241
- const child = path8.join(abs, entry.name);
8417
+ const child = path9.join(abs, entry.name);
7242
8418
  if (entry.isDirectory()) {
7243
8419
  if (SKIP_DIRS.has(entry.name)) {
7244
8420
  empty = false;
@@ -7275,13 +8451,9 @@ function yamlDocument(request) {
7275
8451
  return isYamlSource(String(request.source ?? ""), String(request.document ?? ""));
7276
8452
  return request.command === "conform" && Array.isArray(request.openapi) && request.openapi.some((doc) => YAML_NAME.test(doc.path ?? ""));
7277
8453
  }
7278
- function runsLocally(request) {
7279
- const local = localRuntime();
7280
- return local !== null && !(yamlDocument(request) && !localPythonHasYaml(local.python));
7281
- }
7282
8454
  async function runtime(creds, request, opts = {}) {
7283
- const local = localRuntime();
7284
- if (local && runsLocally(request)) {
8455
+ const local = new RuntimeRoute().local(request);
8456
+ if (local) {
7285
8457
  const payload2 = await localBridge(request, local, opts);
7286
8458
  const detail2 = detailFrom(payload2);
7287
8459
  if (detail2) throw new BridgeRejected(detail2);
@@ -7308,19 +8480,19 @@ async function runtime(creds, request, opts = {}) {
7308
8480
  if (payload === null) throw new CliError(`POST ${route} (${request.command}) returned non-JSON`, 1);
7309
8481
  return payload;
7310
8482
  }
7311
- var fs8, path8, MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BUILD_OUTPUT, BridgeRejected, YAML_NAME;
8483
+ var fs8, path9, MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BridgeRejected, YAML_NAME, YAML_NEEDS_PYYAML, YAML_NOT_ON_PLATFORM, RuntimeRoute;
7312
8484
  var init_client = __esm({
7313
8485
  "src/cli/runtime/client.ts"() {
7314
8486
  "use strict";
7315
8487
  fs8 = __toESM(require("fs"));
7316
- path8 = __toESM(require("path"));
8488
+ path9 = __toESM(require("path"));
7317
8489
  init_router();
7318
8490
  init_local();
7319
8491
  init_transport();
8492
+ init_databaseEngines();
7320
8493
  MAX_WORLD_FILES = 2e3;
7321
8494
  RUNTIME_TIMEOUT_MS = 18e4;
7322
8495
  SKIP_DIRS = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
7323
- BUILD_OUTPUT = /(^|\/)state\.(sqlite|json)$/;
7324
8496
  BridgeRejected = class extends CliError {
7325
8497
  constructor(detail) {
7326
8498
  super(formatBridgeError(detail), 1);
@@ -7329,6 +8501,26 @@ var init_client = __esm({
7329
8501
  detail;
7330
8502
  };
7331
8503
  YAML_NAME = /\.ya?ml$/i;
8504
+ 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";
8505
+ 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)";
8506
+ RuntimeRoute = class _RuntimeRoute {
8507
+ constructor(here = localRuntime, hasYaml = localPythonHasYaml) {
8508
+ this.here = here;
8509
+ this.hasYaml = hasYaml;
8510
+ }
8511
+ here;
8512
+ hasYaml;
8513
+ local(request) {
8514
+ const local = this.here();
8515
+ const yaml = yamlDocument(request);
8516
+ if (yaml && local === null) throw _RuntimeRoute.refusal(YAML_NOT_ON_PLATFORM);
8517
+ if (yaml && local !== null && !this.hasYaml(local.python)) throw _RuntimeRoute.refusal(YAML_NEEDS_PYYAML);
8518
+ return local;
8519
+ }
8520
+ static refusal(message) {
8521
+ return new BridgeRejected({ code: "conform", message, schema_path: "/openapi/source" });
8522
+ }
8523
+ };
7332
8524
  }
7333
8525
  });
7334
8526
 
@@ -7343,7 +8535,8 @@ __export(data_exports, {
7343
8535
  ingestLocalToolCalls: () => ingestLocalToolCalls,
7344
8536
  queryLocalWorldData: () => queryLocalWorldData,
7345
8537
  queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
7346
- showLocalWorldData: () => showLocalWorldData
8538
+ showLocalWorldData: () => showLocalWorldData,
8539
+ sqlLocalWorldData: () => sqlLocalWorldData
7347
8540
  });
7348
8541
  async function importLocalWorldData(dir, rows, opts, creds) {
7349
8542
  const res = await runtime(creds, {
@@ -7375,8 +8568,8 @@ async function importLocalWorldFiles(dir, sources, opts) {
7375
8568
  );
7376
8569
  const res = await runtime(null, {
7377
8570
  command: "import-files",
7378
- world: path9.resolve(dir),
7379
- sources: sources.map((source) => path9.resolve(source)),
8571
+ world: path10.resolve(dir),
8572
+ sources: sources.map((source) => path10.resolve(source)),
7380
8573
  mode: opts.mode ?? "append",
7381
8574
  ...opts.entity ? { entity: opts.entity } : {},
7382
8575
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
@@ -7402,9 +8595,9 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7402
8595
  );
7403
8596
  const res = await runtime(null, {
7404
8597
  command: "chunk-files",
7405
- world: path9.resolve(dir),
7406
- sources: sources.map((source) => path9.resolve(source)),
7407
- out: path9.resolve(out),
8598
+ world: path10.resolve(dir),
8599
+ sources: sources.map((source) => path10.resolve(source)),
8600
+ out: path10.resolve(out),
7408
8601
  ...opts.entity ? { entity: opts.entity } : {},
7409
8602
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
7410
8603
  ...opts.chunkBytes ? { chunk_bytes: opts.chunkBytes } : {},
@@ -7415,8 +8608,8 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7415
8608
  function dropBuildOutputs(dir) {
7416
8609
  const dropped = [];
7417
8610
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
7418
- if (!BUILD_OUTPUT2.test(file.path)) continue;
7419
- fs9.rmSync(path9.join(dir, file.path), { force: true });
8611
+ if (!isBuildOutput(file.path)) continue;
8612
+ fs9.rmSync(path10.join(dir, file.path), { force: true });
7420
8613
  dropped.push(file.path);
7421
8614
  }
7422
8615
  return dropped;
@@ -7483,7 +8676,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7483
8676
  const payload = await localBridge(
7484
8677
  {
7485
8678
  command: "query",
7486
- world: path9.resolve(dir),
8679
+ world: path10.resolve(dir),
7487
8680
  entity,
7488
8681
  across: true,
7489
8682
  ...opts.compare ? { compare: true } : {},
@@ -7499,6 +8692,28 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7499
8692
  if (detail) throw new BridgeRejected(detail);
7500
8693
  return payload;
7501
8694
  }
8695
+ async function sqlLocalWorldData(dir, sql, opts) {
8696
+ const local = localRuntime();
8697
+ if (!local)
8698
+ throw new CliError(
8699
+ "`worlds data sql` on a directory reads the world's database in place, so it needs the bundled runtime on this machine. " + localRuntimeReason(),
8700
+ 1
8701
+ );
8702
+ const payload = await localBridge(
8703
+ {
8704
+ command: "sql",
8705
+ world: path10.resolve(dir),
8706
+ sql,
8707
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
8708
+ ...opts.scenario ? { scenario: opts.scenario } : {}
8709
+ },
8710
+ local
8711
+ );
8712
+ const detail = detailFrom(payload);
8713
+ if (detail) throw new BridgeRejected(detail);
8714
+ const { status: _status, ...answer } = payload;
8715
+ return answer;
8716
+ }
7502
8717
  async function ingestLocalToolCalls(dir, records, opts, creds) {
7503
8718
  const res = await runtime(creds, {
7504
8719
  command: "ingest-calls",
@@ -7530,16 +8745,15 @@ async function ingestLocalToolCalls(dir, records, opts, creds) {
7530
8745
  ...res.batch !== void 0 ? { batch: res.batch } : {}
7531
8746
  };
7532
8747
  }
7533
- var fs9, path9, BUILD_OUTPUT2;
8748
+ var fs9, path10;
7534
8749
  var init_data = __esm({
7535
8750
  "src/cli/runtime/data.ts"() {
7536
8751
  "use strict";
7537
8752
  fs9 = __toESM(require("fs"));
7538
- path9 = __toESM(require("path"));
8753
+ path10 = __toESM(require("path"));
7539
8754
  init_router();
7540
8755
  init_client();
7541
8756
  init_local();
7542
- BUILD_OUTPUT2 = /(^|\/)state\.(sqlite|json)$/;
7543
8757
  }
7544
8758
  });
7545
8759
 
@@ -7578,27 +8792,27 @@ function typescriptPrefix() {
7578
8792
  }
7579
8793
  }
7580
8794
  function command(script, wrapperDir) {
7581
- const suffix = path10.extname(script).toLowerCase();
8795
+ const suffix = path11.extname(script).toLowerCase();
7582
8796
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
7583
8797
  if (NODE_SUFFIXES.includes(suffix)) {
7584
- const wrapper = path10.join(wrapperDir, "transform-wrapper.mjs");
8798
+ const wrapper = path11.join(wrapperDir, "transform-wrapper.mjs");
7585
8799
  fs10.writeFileSync(wrapper, NODE_WRAPPER);
7586
8800
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
7587
8801
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
7588
8802
  }
7589
- throw new CliError(`${path10.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
8803
+ throw new CliError(`${path11.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
7590
8804
  }
7591
8805
  async function runTransform(script, records, opts = {}) {
7592
8806
  if (!fs10.existsSync(script) || !fs10.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
7593
- const resolved = path10.resolve(script);
7594
- const name = path10.basename(script);
8807
+ const resolved = path11.resolve(script);
8808
+ const name = path11.basename(script);
7595
8809
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
7596
- const scratch = fs10.mkdtempSync(path10.join(os2.tmpdir(), "gateway-transform-"));
8810
+ const scratch = fs10.mkdtempSync(path11.join(os2.tmpdir(), "gateway-transform-"));
7597
8811
  try {
7598
8812
  const { file, args } = command(resolved, scratch);
7599
8813
  const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !SCRUBBED.test(key)));
7600
8814
  const done = await new Promise((resolve7, reject) => {
7601
- const child = (0, import_node_child_process3.spawn)(file, args, { cwd: path10.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
8815
+ const child = (0, import_node_child_process3.spawn)(file, args, { cwd: path11.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
7602
8816
  let stdout = "";
7603
8817
  let stderr = "";
7604
8818
  let timedOut = false;
@@ -7636,14 +8850,14 @@ async function transformRecords(transform, records, opts = {}) {
7636
8850
  if (typeof transform === "function") return { rows: checkRows(await transform(records), "transform(records)"), stderr: "" };
7637
8851
  return runTransform(transform, records, opts);
7638
8852
  }
7639
- var import_node_child_process3, fs10, os2, path10, import_node_module, import_meta, DEFAULT_TRANSFORM_TIMEOUT_MS, NODE_SUFFIXES, PYTHON_SUFFIXES, SCRUBBED, PY_WRAPPER, NODE_WRAPPER, isObject, tail;
8853
+ var import_node_child_process3, fs10, os2, path11, import_node_module, import_meta, DEFAULT_TRANSFORM_TIMEOUT_MS, NODE_SUFFIXES, PYTHON_SUFFIXES, SCRUBBED, PY_WRAPPER, NODE_WRAPPER, isObject, tail;
7640
8854
  var init_transform = __esm({
7641
8855
  "src/cli/platform/worldData/transform.ts"() {
7642
8856
  "use strict";
7643
8857
  import_node_child_process3 = require("child_process");
7644
8858
  fs10 = __toESM(require("fs"));
7645
8859
  os2 = __toESM(require("os"));
7646
- path10 = __toESM(require("path"));
8860
+ path11 = __toESM(require("path"));
7647
8861
  import_node_module = require("module");
7648
8862
  init_router();
7649
8863
  import_meta = {};
@@ -7732,11 +8946,15 @@ function isSettled(status) {
7732
8946
  if (status.state !== "applied") return true;
7733
8947
  return status.dryRun === true || status.publish !== "now" || !status.snapshotId;
7734
8948
  }
8949
+ function worldDataPollMs() {
8950
+ const raw = Number(process.env.GATEWAY_WORLD_DATA_POLL_MS ?? 1e3);
8951
+ return Number.isFinite(raw) && raw >= 0 ? raw : 1e3;
8952
+ }
7735
8953
  function uploadHeadersFor(gz) {
7736
8954
  return {
7737
8955
  "content-type": "application/gzip",
7738
8956
  "content-length": String(gz.length),
7739
- "x-amz-checksum-sha256": (0, import_node_crypto9.createHash)("sha256").update(gz).digest("base64")
8957
+ "x-amz-checksum-sha256": (0, import_node_crypto10.createHash)("sha256").update(gz).digest("base64")
7740
8958
  };
7741
8959
  }
7742
8960
  async function forEachLimited(items, limit, fn) {
@@ -7763,7 +8981,7 @@ async function forEachLimited(items, limit, fn) {
7763
8981
  if (failure) throw failure;
7764
8982
  }
7765
8983
  async function waitForBatch(client, batchId, opts = {}) {
7766
- const sleep2 = opts.sleep ?? client.sleep;
8984
+ const sleep4 = opts.sleep ?? client.sleep;
7767
8985
  let delay = opts.initialDelayMs ?? 1e3;
7768
8986
  let last = "";
7769
8987
  for (; ; ) {
@@ -7774,7 +8992,9 @@ async function waitForBatch(client, batchId, opts = {}) {
7774
8992
  last = phase;
7775
8993
  }
7776
8994
  if (isSettled(status)) return status;
7777
- await sleep2(delay);
8995
+ if (opts.deadlineMs !== void 0 && (opts.now ?? Date.now)() >= opts.deadlineMs)
8996
+ return status;
8997
+ await sleep4(delay);
7778
8998
  delay = Math.min(1e4, Math.round(delay * 1.5));
7779
8999
  }
7780
9000
  }
@@ -7782,11 +9002,11 @@ function phaseLine(status) {
7782
9002
  const phase = status.progress?.phase;
7783
9003
  return phase ? `${status.state} (${phase})` : status.state;
7784
9004
  }
7785
- var import_node_crypto9, fs11, TERMINAL_STATES, sleepMs, WorldDataApi;
9005
+ var import_node_crypto10, fs11, TERMINAL_STATES, sleepMs, WorldDataApi;
7786
9006
  var init_batches = __esm({
7787
9007
  "src/cli/platform/worldData/batches.ts"() {
7788
9008
  "use strict";
7789
- import_node_crypto9 = require("crypto");
9009
+ import_node_crypto10 = require("crypto");
7790
9010
  fs11 = __toESM(require("fs"));
7791
9011
  init_config();
7792
9012
  init_router();
@@ -7798,10 +9018,10 @@ var init_batches = __esm({
7798
9018
  ]);
7799
9019
  sleepMs = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
7800
9020
  WorldDataApi = class {
7801
- constructor(creds, slug, sleep2 = sleepMs) {
9021
+ constructor(creds, slug, sleep4 = sleepMs) {
7802
9022
  this.creds = creds;
7803
9023
  this.slug = slug;
7804
- this.sleep = sleep2;
9024
+ this.sleep = sleep4;
7805
9025
  this.base = `/api/public/worlds/${encodeURIComponent(slug)}/data`;
7806
9026
  }
7807
9027
  creds;
@@ -7825,11 +9045,6 @@ var init_batches = __esm({
7825
9045
  }
7826
9046
  );
7827
9047
  }
7828
- /**
7829
- * Stage a project-drive rows file as one chunk, on the platform: the server
7830
- * streams it out of drive storage into chunk storage and answers with the
7831
- * digest and gzipped size it derived. No bytes pass through this process.
7832
- */
7833
9048
  async stageDrive(batchId, chunk) {
7834
9049
  const staged = await api(this.creds, "POST", `${this.base}/batches/${batchId}/chunks`, {
7835
9050
  ordinal: chunk.ordinal,
@@ -7883,11 +9098,6 @@ var init_batches = __esm({
7883
9098
  message ? { message } : {}
7884
9099
  );
7885
9100
  }
7886
- /**
7887
- * PUT the gzipped chunk to its presigned URL with exactly the headers the
7888
- * URL was signed for (the platform's `uploadHeaders`; the same three when it
7889
- * sends none), with backoff on a dropped connection or a 5xx.
7890
- */
7891
9101
  async put(uploadUrl, gz, signed) {
7892
9102
  const headers = signed ?? uploadHeadersFor(gz);
7893
9103
  const delays = [500, 1500, 4e3];
@@ -7914,7 +9124,6 @@ var init_batches = __esm({
7914
9124
  await this.sleep(delays[attempt]);
7915
9125
  }
7916
9126
  }
7917
- /** Register, upload unless the server already holds the content, confirm. */
7918
9127
  async upload(batchId, chunk) {
7919
9128
  const registered = await this.register(batchId, chunk);
7920
9129
  if (!registered.uploaded) {
@@ -7980,12 +9189,12 @@ function finishChunk(ordinal, raw, rows, entity, firstRow = 0) {
7980
9189
  const gz = gzipDeterministic(raw, { name: chunkName(ordinal) });
7981
9190
  return {
7982
9191
  ordinal,
7983
- digest: (0, import_node_crypto10.createHash)("sha256").update(gz).digest("hex"),
9192
+ digest: (0, import_node_crypto11.createHash)("sha256").update(gz).digest("hex"),
7984
9193
  bytes: gz.length,
7985
9194
  rows,
7986
9195
  entity,
7987
9196
  rawBytes: raw.length,
7988
- contentSha256: (0, import_node_crypto10.createHash)("sha256").update(raw).digest("hex"),
9197
+ contentSha256: (0, import_node_crypto11.createHash)("sha256").update(raw).digest("hex"),
7989
9198
  firstRow,
7990
9199
  gz
7991
9200
  };
@@ -8025,11 +9234,11 @@ function crc32(data) {
8025
9234
  crc = table[(crc ^ data[i]) & 255] ^ crc >>> 8;
8026
9235
  return (crc ^ 4294967295) >>> 0;
8027
9236
  }
8028
- var import_node_crypto10, import_node_zlib3, DEFAULT_CHUNK_BYTES, GZIP_LEVEL, chunkName, table;
9237
+ var import_node_crypto11, import_node_zlib3, DEFAULT_CHUNK_BYTES, GZIP_LEVEL, chunkName, table;
8029
9238
  var init_chunker = __esm({
8030
9239
  "src/cli/platform/worldData/chunker.ts"() {
8031
9240
  "use strict";
8032
- import_node_crypto10 = require("crypto");
9241
+ import_node_crypto11 = require("crypto");
8033
9242
  import_node_zlib3 = require("zlib");
8034
9243
  init_json();
8035
9244
  DEFAULT_CHUNK_BYTES = 64 * 1024 * 1024;
@@ -8055,22 +9264,33 @@ async function pushRows(creds, slug, rows, opts = {}) {
8055
9264
  publish: opts.publish ?? "now",
8056
9265
  ...opts.message ? { message: opts.message } : {}
8057
9266
  });
8058
- if (!opened.batchId) throw new Error("the platform opened no batch (no batchId in the answer)");
9267
+ if (!opened.batchId)
9268
+ throw new Error("the platform opened no batch (no batchId in the answer)");
8059
9269
  const tagged = (async function* () {
8060
9270
  for (const [entity, listed] of Object.entries(rows))
8061
9271
  for (let index = 0; index < listed.length; index++)
8062
- yield { entity, row: parseJson(JSON.stringify(listed[index])), where: `${entity}[${index}]` };
9272
+ yield {
9273
+ entity,
9274
+ row: parseJson(JSON.stringify(listed[index])),
9275
+ where: `${entity}[${index}]`
9276
+ };
8063
9277
  })();
8064
- const chunks = buildChunks(tagged, { chunkBytes: opts.chunkBytes ?? DEFAULT_CHUNK_BYTES, chunkRows: null, entity: null });
8065
- await forEachLimited(chunks, Math.max(1, opts.parallel ?? 4), async (chunk) => {
8066
- await client.upload(opened.batchId, chunk);
9278
+ const chunks = buildChunks(tagged, {
9279
+ chunkBytes: opts.chunkBytes ?? DEFAULT_CHUNK_BYTES,
9280
+ chunkRows: null,
9281
+ entity: null
8067
9282
  });
9283
+ await forEachLimited(
9284
+ chunks,
9285
+ Math.max(1, opts.parallel ?? 4),
9286
+ async (chunk) => {
9287
+ await client.upload(opened.batchId, chunk);
9288
+ }
9289
+ );
8068
9290
  await client.complete(opened.batchId);
8069
- return waitForBatch(client, opened.batchId, { initialDelayMs: pollMs() });
8070
- }
8071
- function pollMs() {
8072
- const raw = Number(process.env.GATEWAY_WORLD_DATA_POLL_MS ?? 1e3);
8073
- return Number.isFinite(raw) && raw >= 0 ? raw : 1e3;
9291
+ return waitForBatch(client, opened.batchId, {
9292
+ initialDelayMs: worldDataPollMs()
9293
+ });
8074
9294
  }
8075
9295
  var init_push = __esm({
8076
9296
  "src/cli/platform/worldData/push.ts"() {
@@ -8205,19 +9425,13 @@ function toolCallsFromObservations(observations, opts = {}) {
8205
9425
  }
8206
9426
  return out;
8207
9427
  }
8208
- function credentialsOf(options) {
8209
- const host = (options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai").replace(/\/+$/, "");
8210
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8211
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8212
- return publicKey && secretKey ? { host, publicKey, secretKey } : null;
8213
- }
8214
9428
  async function ingest(world, records, options = {}) {
8215
9429
  const { ingestLocalToolCalls: ingestLocalToolCalls2, importLocalWorldData: importLocalWorldData2, checkLocalWorldData: checkLocalWorldData2 } = await Promise.resolve().then(() => (init_data(), data_exports));
8216
9430
  if (options.transform !== void 0 && options.map !== void 0) throw new Error("records go through one of transform, map");
8217
9431
  const redact = options.redact ?? "off";
8218
9432
  const mode = options.mode ?? "append";
8219
9433
  const dryRun = Boolean(options.dryRun);
8220
- const creds = credentialsOf(options);
9434
+ const creds = platformEndpoint(options);
8221
9435
  const local = fs12.existsSync(world) && fs12.statSync(world).isDirectory();
8222
9436
  const map = options.map === void 0 ? void 0 : options.map;
8223
9437
  let rows = null;
@@ -8231,12 +9445,12 @@ async function ingest(world, records, options = {}) {
8231
9445
  const landed = dryRun ? await checkLocalWorldData2(world, rows, creds, { redact }) : await importLocalWorldData2(world, rows, { mode, redact }, creds);
8232
9446
  return transformReport(world, records.length, rows, stderr, { dryRun, redact, changed: !dryRun && landed.changed, kind: landed.kind, entityCounts: landed.entity_counts });
8233
9447
  }
8234
- if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
9448
+ if (!creds) throw new Error(GATEWAY_KEYS_MISSING);
8235
9449
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
8236
9450
  const slug = world.split("@", 1)[0];
8237
- const scratch = fs12.mkdtempSync(path11.join(os3.tmpdir(), "gateway-ingest-"));
9451
+ const scratch = fs12.mkdtempSync(path12.join(os3.tmpdir(), "gateway-ingest-"));
8238
9452
  try {
8239
- const pulled = await pull2(world, path11.join(scratch, slug), options);
9453
+ const pulled = await pull2(world, path12.join(scratch, slug), options);
8240
9454
  let report;
8241
9455
  let projected;
8242
9456
  if (rows === null) {
@@ -8310,26 +9524,27 @@ async function sessionToolObservations(sessionId, options = {}) {
8310
9524
  for (const observation of trace3.observations ?? []) out.push({ traceId: trace3.id ?? null, ...observation });
8311
9525
  return out;
8312
9526
  }
8313
- var fs12, os3, path11, isObject2, isoOf;
9527
+ var fs12, os3, path12, isObject2, isoOf;
8314
9528
  var init_ingest = __esm({
8315
9529
  "src/worlds/ingest.ts"() {
8316
9530
  "use strict";
8317
9531
  fs12 = __toESM(require("fs"));
8318
9532
  os3 = __toESM(require("os"));
8319
- path11 = __toESM(require("path"));
9533
+ path12 = __toESM(require("path"));
8320
9534
  init_worlds();
9535
+ init_platformEndpoint();
8321
9536
  isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
8322
9537
  isoOf = (value) => value instanceof Date ? value.toISOString() : typeof value === "string" ? value : void 0;
8323
9538
  }
8324
9539
  });
8325
9540
 
8326
9541
  // src/worlds/editSessions.ts
8327
- var WORLDS, WorldEditClient, TASK_EDITS, TaskEditClient;
9542
+ var WORLDS2, WorldEditClient, TASK_EDITS, TaskEditClient;
8328
9543
  var init_editSessions = __esm({
8329
9544
  "src/worlds/editSessions.ts"() {
8330
9545
  "use strict";
8331
9546
  init_worlds();
8332
- WORLDS = "/api/public/worlds";
9547
+ WORLDS2 = "/api/public/worlds";
8333
9548
  WorldEditClient = class {
8334
9549
  constructor(options = {}) {
8335
9550
  this.options = options;
@@ -8341,19 +9556,19 @@ var init_editSessions = __esm({
8341
9556
  if (query.status) qs.set("status", query.status);
8342
9557
  if (query.limit !== void 0) qs.set("limit", String(query.limit));
8343
9558
  const suffix = qs.size ? `?${qs.toString()}` : "";
8344
- return apiRequest(this.options, "GET", `${WORLDS}/${encodeURIComponent(slug)}/edits${suffix}`);
9559
+ return apiRequest(this.options, "GET", `${WORLDS2}/${encodeURIComponent(slug)}/edits${suffix}`);
8345
9560
  }
8346
9561
  /** Seal a session into a version on main (the credential's current one when `sessionId` is omitted). */
8347
9562
  close(slug, input = {}) {
8348
- return apiRequest(this.options, "POST", `${WORLDS}/${encodeURIComponent(slug)}/edits/close`, input);
9563
+ return apiRequest(this.options, "POST", `${WORLDS2}/${encodeURIComponent(slug)}/edits/close`, input);
8349
9564
  }
8350
9565
  /** Drop the session's private branch without making a version. */
8351
9566
  discard(slug, input = {}) {
8352
- return apiRequest(this.options, "POST", `${WORLDS}/${encodeURIComponent(slug)}/edits/discard`, input);
9567
+ return apiRequest(this.options, "POST", `${WORLDS2}/${encodeURIComponent(slug)}/edits/discard`, input);
8353
9568
  }
8354
9569
  /** Undo a sealed session (source and data) as a new sealed session. */
8355
9570
  rollback(slug, input) {
8356
- return apiRequest(this.options, "POST", `${WORLDS}/${encodeURIComponent(slug)}/edits/rollback`, input);
9571
+ return apiRequest(this.options, "POST", `${WORLDS2}/${encodeURIComponent(slug)}/edits/rollback`, input);
8357
9572
  }
8358
9573
  };
8359
9574
  TASK_EDITS = "/api/public/world-task-edits";
@@ -8418,7 +9633,7 @@ var init_dryRun = __esm({
8418
9633
  /** Poll until the run settles or `timeoutSeconds` pass; the last verdict either way. */
8419
9634
  async settle(runId, opts) {
8420
9635
  const now = opts.now ?? Date.now;
8421
- const sleep2 = opts.sleep ?? ((ms) => new Promise((resolve7) => setTimeout(resolve7, ms)));
9636
+ const sleep4 = opts.sleep ?? ((ms) => new Promise((resolve7) => setTimeout(resolve7, ms)));
8422
9637
  const deadline = now() + opts.timeoutSeconds * 1e3;
8423
9638
  for (; ; ) {
8424
9639
  const budget = Math.floor(
@@ -8428,7 +9643,7 @@ var init_dryRun = __esm({
8428
9643
  const verdict = await this.status(runId, budget);
8429
9644
  if (verdict.settled || now() >= deadline) return verdict;
8430
9645
  opts.onProgress?.(verdict);
8431
- if (now() - asked < 1e3) await sleep2(1e3);
9646
+ if (now() - asked < 1e3) await sleep4(1e3);
8432
9647
  }
8433
9648
  }
8434
9649
  };
@@ -8496,6 +9711,8 @@ __export(worlds_exports, {
8496
9711
  EXPLORATORY_TASK: () => EXPLORATORY_TASK,
8497
9712
  EXPLORATORY_TASK_SOURCE: () => EXPLORATORY_TASK_SOURCE,
8498
9713
  GatewayEvalsClient: () => GatewayEvalsClient,
9714
+ GradePayload: () => GradePayload,
9715
+ GradeRefused: () => GradeRefused,
8499
9716
  HostedDryRunClient: () => HostedDryRunClient,
8500
9717
  IDLE_WARNING_MINUTES: () => IDLE_WARNING_MINUTES,
8501
9718
  KEY_NAMES: () => KEY_NAMES,
@@ -8505,52 +9722,80 @@ __export(worlds_exports, {
8505
9722
  NativeComputer: () => NativeComputer,
8506
9723
  OpenAIComputer: () => OpenAIComputer,
8507
9724
  OpenAIPreviewComputer: () => OpenAIPreviewComputer,
9725
+ PLATFORM_SESSION_HEADER: () => PLATFORM_SESSION_HEADER,
8508
9726
  PLATFORM_TIMEOUT_DEFAULT_MS: () => PLATFORM_TIMEOUT_DEFAULT_MS,
8509
9727
  PLATFORM_TIMEOUT_ENV: () => PLATFORM_TIMEOUT_ENV,
8510
9728
  PendingSafetyChecks: () => PendingSafetyChecks,
8511
9729
  PlatformTransport: () => PlatformTransport,
8512
9730
  PushSlugConflict: () => PushSlugConflict,
8513
9731
  RECOVER_BUDGET_MS: () => RECOVER_BUDGET_MS,
9732
+ REQUESTS_NOT_RECORDED: () => REQUESTS_NOT_RECORDED,
9733
+ REQUEST_BODY_CAPTURE_BYTES: () => REQUEST_BODY_CAPTURE_BYTES,
9734
+ REQUEST_LOG_PAGE_MAX: () => REQUEST_LOG_PAGE_MAX,
9735
+ RetryPolicy: () => RetryPolicy,
9736
+ SANDBOX_EXEC_REPORT_MARGIN_MS: () => SANDBOX_EXEC_REPORT_MARGIN_MS,
9737
+ SANDBOX_EXEC_START_ATTEMPTS: () => SANDBOX_EXEC_START_ATTEMPTS,
9738
+ SANDBOX_EXEC_TIMEOUT_MAX_S: () => SANDBOX_EXEC_TIMEOUT_MAX_S,
9739
+ SANDBOX_EXEC_WAIT_MS: () => SANDBOX_EXEC_WAIT_MS,
9740
+ SANDBOX_FILE_MAX_BYTES: () => SANDBOX_FILE_MAX_BYTES,
8514
9741
  SANDBOX_REQUEST_TIMEOUT_MS: () => SANDBOX_REQUEST_TIMEOUT_MS,
9742
+ SANDBOX_WRITE_CHUNK_BYTES: () => SANDBOX_WRITE_CHUNK_BYTES,
9743
+ SANDBOX_WRITE_SKIPPED: () => SANDBOX_WRITE_SKIPPED,
8515
9744
  SECRET_KEY: () => SECRET_KEY,
9745
+ SESSION_AUTH_MODES: () => SESSION_AUTH_MODES,
8516
9746
  SESSION_STATES: () => SESSION_STATES,
8517
9747
  SPAN_PREFIX: () => SPAN_PREFIX,
8518
9748
  STATUS_WAIT_CAP_SECONDS: () => STATUS_WAIT_CAP_SECONDS,
8519
9749
  STRIP_PREFIX: () => STRIP_PREFIX,
8520
9750
  SURFACE_KINDS: () => SURFACE_KINDS,
9751
+ Score: () => Score,
8521
9752
  Screen: () => Screen,
8522
9753
  SecretScrubber: () => SecretScrubber,
9754
+ TOOL_CONTRACT_UNREAD: () => TOOL_CONTRACT_UNREAD,
8523
9755
  TRANSFORMS: () => TRANSFORMS,
8524
9756
  TaskEditClient: () => TaskEditClient,
9757
+ TaskGrades: () => TaskGrades,
8525
9758
  TaskRun: () => TaskRun,
8526
9759
  TaskSessions: () => TaskSessions,
8527
9760
  TransportCause: () => TransportCause,
8528
9761
  UnreachableError: () => UnreachableError,
8529
9762
  VERIFIER_REWARD_PREFIX: () => VERIFIER_REWARD_PREFIX,
8530
9763
  WHITESPACE: () => WHITESPACE,
9764
+ WORLD_REQUEST_VIAS: () => WORLD_REQUEST_VIAS,
8531
9765
  WorldBrowser: () => WorldBrowser,
8532
9766
  WorldCallTooLarge: () => WorldCallTooLarge,
8533
9767
  WorldEditClient: () => WorldEditClient,
9768
+ WorldNotice: () => WorldNotice,
9769
+ WorldRequestFilterError: () => WorldRequestFilterError,
9770
+ WorldRequestLedger: () => WorldRequestLedger,
8534
9771
  WorldRequestLog: () => WorldRequestLog,
9772
+ WorldRequestQuery: () => WorldRequestQuery,
9773
+ WorldRequestsNotRecorded: () => WorldRequestsNotRecorded,
8535
9774
  WorldSandboxClient: () => WorldSandboxClient,
8536
9775
  WorldSession: () => WorldSession,
8537
9776
  WorldSessionCallError: () => WorldSessionCallError,
9777
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
8538
9778
  WorldSessionTimeout: () => WorldSessionTimeout,
8539
9779
  WorldSet: () => WorldSet,
8540
9780
  WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
8541
9781
  WorldTask: () => WorldTask,
9782
+ WorldToolsUnread: () => WorldToolsUnread,
9783
+ WriteTreeFilter: () => WriteTreeFilter,
8542
9784
  aggregateTaskGrade: () => aggregateTaskGrade,
8543
9785
  apiRequest: () => apiRequest,
8544
9786
  applyTransforms: () => applyTransforms,
8545
9787
  archiveMode: () => archiveMode,
8546
9788
  assertBrowserTier: () => assertBrowserTier,
9789
+ assertSessionAuthMode: () => assertSessionAuthMode,
8547
9790
  assertTaskRef: () => assertTaskRef,
8548
9791
  attachManifest: () => attachManifest,
8549
9792
  attachSession: () => attachSession,
8550
9793
  attachTask: () => attachTask,
8551
9794
  buildFileManifest: () => buildFileManifest,
8552
9795
  buildSourceTarball: () => buildSourceTarball,
9796
+ capabilityLabel: () => capabilityLabel,
8553
9797
  captureToolCalls: () => captureToolCalls,
9798
+ carriedWarnings: () => carriedWarnings,
8554
9799
  checkoutIdentity: () => checkoutIdentity,
8555
9800
  closeConnection: () => closeConnection,
8556
9801
  collectArchiveRelPaths: () => collectArchiveRelPaths,
@@ -8569,7 +9814,9 @@ __export(worlds_exports, {
8569
9814
  idleWarning: () => idleWarning,
8570
9815
  ingest: () => ingest,
8571
9816
  ingestToolCalls: () => ingestToolCalls,
9817
+ invalidFields: () => invalidFields,
8572
9818
  isBrowserGone: () => isBrowserGone,
9819
+ isSandboxCommitPending: () => isSandboxCommitPending,
8573
9820
  isSandboxImportPending: () => isSandboxImportPending,
8574
9821
  isTaskIdRef: () => isTaskIdRef,
8575
9822
  isTaskManifest: () => isTaskManifest,
@@ -8581,6 +9828,7 @@ __export(worlds_exports, {
8581
9828
  listTaskSessions: () => listTaskSessions,
8582
9829
  listTasks: () => listTasks,
8583
9830
  localTreeState: () => localTreeState,
9831
+ messageOf: () => messageOf,
8584
9832
  migrateProjectWorlds: () => migrateProjectWorlds,
8585
9833
  migrateWorld: () => migrateWorld,
8586
9834
  modelToolName: () => modelToolName,
@@ -8615,6 +9863,7 @@ __export(worlds_exports, {
8615
9863
  resolveBrowserMode: () => resolveBrowserMode,
8616
9864
  resolvePushSlug: () => resolvePushSlug,
8617
9865
  roundForm: () => roundForm,
9866
+ rowsRefusal: () => rowsRefusal,
8618
9867
  runSessions: () => runSessions,
8619
9868
  runSet: () => runSet,
8620
9869
  runTask: () => runTask,
@@ -8624,10 +9873,14 @@ __export(worlds_exports, {
8624
9873
  setEnvSecret: () => setEnvSecret,
8625
9874
  shapeText: () => shapeText,
8626
9875
  snapshotTreeHashes: () => snapshotTreeHashes,
9876
+ sqlRows: () => sqlRows,
9877
+ stillStartingHint: () => stillStartingHint,
8627
9878
  storedForms: () => storedForms,
8628
9879
  surfaceDescriptor: () => surfaceDescriptor,
8629
9880
  surfaceKinds: () => surfaceKinds,
9881
+ taskGradeError: () => taskGradeError,
8630
9882
  taskLabel: () => taskLabel,
9883
+ taskRunFailure: () => taskRunFailure,
8631
9884
  toolCallRecordsIn: () => toolCallRecordsIn,
8632
9885
  toolCallsFromObservations: () => toolCallsFromObservations,
8633
9886
  uiEntryUrl: () => uiEntryUrl,
@@ -8639,42 +9892,46 @@ __export(worlds_exports, {
8639
9892
  validateWorld: () => validateWorld,
8640
9893
  waitForRun: () => waitForRun,
8641
9894
  withoutUiToken: () => withoutUiToken,
9895
+ worldRequestsUnrecorded: () => worldRequestsUnrecorded,
8642
9896
  writeLocalMeta: () => writeLocalMeta
8643
9897
  });
8644
9898
  module.exports = __toCommonJS(worlds_exports);
8645
- function resolveEnv(options) {
8646
- const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
8647
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8648
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8649
- if (!publicKey || !secretKey) {
8650
- throw new Error(
8651
- "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey)."
8652
- );
8653
- }
8654
- return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
9899
+ function edgeAnswer(text2, status) {
9900
+ if (status !== void 0 && status >= 500) return EDGE_ANSWERS[status] ?? EDGE_FAILURE;
9901
+ return /^\s*</.test(text2) ? EDGE_PAGE : null;
8655
9902
  }
8656
- function parseErrorBody(text2, limit = 600) {
8657
- const raw = { detail: text2.slice(0, limit), code: null };
9903
+ function parseErrorBody(text2, status) {
9904
+ const raw = { detail: text2.slice(0, DETAIL_LIMIT), code: null };
8658
9905
  let parsed;
8659
9906
  try {
8660
9907
  parsed = JSON.parse(text2);
8661
9908
  } catch {
8662
- return raw;
9909
+ return { detail: (edgeAnswer(text2, status) ?? text2).slice(0, DETAIL_LIMIT), code: null };
8663
9910
  }
8664
9911
  if (parsed === null || typeof parsed !== "object") return raw;
8665
- const { message, code } = parsed;
9912
+ const { message, code, error } = parsed;
8666
9913
  const stableCode = typeof code === "string" ? code : null;
8667
9914
  if (typeof message !== "string") return { ...raw, code: stableCode };
8668
- if (stableCode === null)
8669
- return { detail: message.slice(0, limit), code: null };
9915
+ const fields = invalidFields(error);
9916
+ const said = fields ? `${message}: ${fields}` : message;
9917
+ if (stableCode === null) return { detail: said.slice(0, DETAIL_LIMIT), code: null };
8670
9918
  return {
8671
- detail: `${message} (${stableCode})`.slice(0, limit),
9919
+ detail: `${said} (${stableCode})`.slice(0, DETAIL_LIMIT),
8672
9920
  code: stableCode
8673
9921
  };
8674
9922
  }
8675
- async function apiRequest(options, method, path12, body, init2 = {}) {
8676
- const reply = await new PlatformTransport(resolveEnv(options)).send(method, path12, body, init2);
8677
- if (!reply.ok) throw new ApiError(reply.status, method, path12, reply.text);
9923
+ function invalidFields(error) {
9924
+ const issues = Array.isArray(error) ? error : [];
9925
+ return issues.filter(
9926
+ (issue) => issue !== null && typeof issue === "object" && typeof issue.message === "string"
9927
+ ).map((issue) => {
9928
+ const path13 = Array.isArray(issue.path) ? issue.path.map(String).join(".") : "";
9929
+ return path13 ? `${path13}: ${issue.message}` : issue.message;
9930
+ }).join("; ");
9931
+ }
9932
+ async function apiRequest(options, method, path13, body, init2 = {}) {
9933
+ const reply = await new PlatformTransport(requirePlatformEndpoint(options)).send(method, path13, body, init2);
9934
+ if (!reply.ok) throw new ApiError(reply.status, method, path13, reply.text);
8678
9935
  return JSON.parse(reply.text);
8679
9936
  }
8680
9937
  async function setEnvSecret(slug, key, value, options = {}) {
@@ -8786,7 +10043,6 @@ function detectProvenance() {
8786
10043
  kind: "ci",
8787
10044
  id: env.GITHUB_ACTOR,
8788
10045
  name: env.GITHUB_ACTOR,
8789
- // GitHub serves every account's avatar at /<login>.png.
8790
10046
  imageUrl: `${server}/${env.GITHUB_ACTOR}.png`
8791
10047
  };
8792
10048
  }
@@ -8851,16 +10107,19 @@ async function resolveWorld(slugRef, slug, ref, pin, options) {
8851
10107
  }
8852
10108
  return world;
8853
10109
  }
8854
- var ApiError, WorldTask, WorldSet;
10110
+ var ApiError, DETAIL_LIMIT, EDGE_ANSWERS, EDGE_FAILURE, EDGE_PAGE, WorldTask, WorldSet;
8855
10111
  var init_worlds = __esm({
8856
10112
  "src/worlds/index.ts"() {
10113
+ init_platformEndpoint();
8857
10114
  init_transport();
8858
10115
  init_transport();
8859
10116
  init_session2();
10117
+ init_grade();
8860
10118
  init_sessionList();
8861
10119
  init_sessionContract();
8862
10120
  init_taskRef();
8863
10121
  init_tasks();
10122
+ init_taskGrades();
8864
10123
  init_taskSessions();
8865
10124
  init_taskRun();
8866
10125
  init_setRun();
@@ -8869,6 +10128,7 @@ var init_worlds = __esm({
8869
10128
  init_browser();
8870
10129
  init_surfaceDescriptor();
8871
10130
  init_requestLog();
10131
+ init_requestLedger();
8872
10132
  init_browserReconnect();
8873
10133
  init_nativeComputer();
8874
10134
  init_hub();
@@ -8882,10 +10142,11 @@ var init_worlds = __esm({
8882
10142
  init_sandbox();
8883
10143
  init_editSessions();
8884
10144
  init_dryRun();
10145
+ init_worldNotice();
8885
10146
  ApiError = class extends Error {
8886
- constructor(status, method, path12, text2) {
8887
- const parsed = parseErrorBody(text2);
8888
- super(`${method} ${path12} -> ${status}: ${parsed.detail}`);
10147
+ constructor(status, method, path13, text2) {
10148
+ const parsed = parseErrorBody(text2, status);
10149
+ super(`${method} ${path13} -> ${status}: ${parsed.detail}`);
8889
10150
  this.status = status;
8890
10151
  this.text = text2;
8891
10152
  this.name = "ApiError";
@@ -8895,6 +10156,14 @@ var init_worlds = __esm({
8895
10156
  text;
8896
10157
  code;
8897
10158
  };
10159
+ DETAIL_LIMIT = 600;
10160
+ EDGE_ANSWERS = {
10161
+ 502: "Bad Gateway from the load balancer in front of the platform, not the platform's answer: the web server dropped the connection or restarted mid-request. Retry; if it repeats, the platform is restarting or overloaded.",
10162
+ 503: "Service Unavailable from the load balancer in front of the platform, not the platform's answer: no web server was ready to take the request (a deploy or a restart). Retry in a minute.",
10163
+ 504: "Gateway Time-out from the load balancer in front of the platform, not the platform's answer: the request sent nothing back within the load balancer's idle timeout (60 s unless the deployment raises it: Helm ingress.idleTimeoutSeconds) and was cut there, though the platform may still be running it. Check what it changed before you retry."
10164
+ };
10165
+ EDGE_FAILURE = "The platform failed without its JSON error body: a proxy in front of it answered, or the server stopped before its error handler ran. Retry; if it repeats, report the request and the time.";
10166
+ EDGE_PAGE = "An HTML page answered instead of the platform's JSON: a proxy in front of the platform, or the configured host is not the platform.";
8898
10167
  WorldTask = class {
8899
10168
  constructor(world, name, variant) {
8900
10169
  this.world = world;
@@ -8930,12 +10199,7 @@ var init_worlds = __esm({
8930
10199
  }
8931
10200
  return hit;
8932
10201
  }
8933
- /**
8934
- * Open a LIVE session against one of this world's tasks — a bundled task's
8935
- * name or an inline `WorldTaskSpec`: the platform boots (or reuses) a warm
8936
- * container, and your agent calls its tools through the platform. See
8937
- * worlds/session.ts.
8938
- */
10202
+ /** Open a live session on a bundled task's name or an inline `WorldTaskSpec`; the platform boots or reuses a warm container. */
8939
10203
  async open(task, opts = {}) {
8940
10204
  const { openSession: openSession2 } = await Promise.resolve().then(() => (init_session2(), session_exports2));
8941
10205
  return openSession2(this.slug, {
@@ -8947,11 +10211,7 @@ var init_worlds = __esm({
8947
10211
  options: this.options
8948
10212
  });
8949
10213
  }
8950
- /**
8951
- * Start a HOSTED run of a run config against this world — the platform
8952
- * executes the (Python) world in parallel containers per the config's
8953
- * `parallelism` and files one graded run per model. Returns those runs.
8954
- */
10214
+ /** Start a hosted run of a run config on this version (or `opts.versionId`): one graded run per model, provenance detected unless given. */
8955
10215
  async dispatch(runConfigId, opts = {}) {
8956
10216
  const provenance = opts.provenance ?? detectProvenance();
8957
10217
  return apiRequest(
@@ -9001,6 +10261,8 @@ init_worlds();
9001
10261
  EXPLORATORY_TASK,
9002
10262
  EXPLORATORY_TASK_SOURCE,
9003
10263
  GatewayEvalsClient,
10264
+ GradePayload,
10265
+ GradeRefused,
9004
10266
  HostedDryRunClient,
9005
10267
  IDLE_WARNING_MINUTES,
9006
10268
  KEY_NAMES,
@@ -9010,52 +10272,80 @@ init_worlds();
9010
10272
  NativeComputer,
9011
10273
  OpenAIComputer,
9012
10274
  OpenAIPreviewComputer,
10275
+ PLATFORM_SESSION_HEADER,
9013
10276
  PLATFORM_TIMEOUT_DEFAULT_MS,
9014
10277
  PLATFORM_TIMEOUT_ENV,
9015
10278
  PendingSafetyChecks,
9016
10279
  PlatformTransport,
9017
10280
  PushSlugConflict,
9018
10281
  RECOVER_BUDGET_MS,
10282
+ REQUESTS_NOT_RECORDED,
10283
+ REQUEST_BODY_CAPTURE_BYTES,
10284
+ REQUEST_LOG_PAGE_MAX,
10285
+ RetryPolicy,
10286
+ SANDBOX_EXEC_REPORT_MARGIN_MS,
10287
+ SANDBOX_EXEC_START_ATTEMPTS,
10288
+ SANDBOX_EXEC_TIMEOUT_MAX_S,
10289
+ SANDBOX_EXEC_WAIT_MS,
10290
+ SANDBOX_FILE_MAX_BYTES,
9019
10291
  SANDBOX_REQUEST_TIMEOUT_MS,
10292
+ SANDBOX_WRITE_CHUNK_BYTES,
10293
+ SANDBOX_WRITE_SKIPPED,
9020
10294
  SECRET_KEY,
10295
+ SESSION_AUTH_MODES,
9021
10296
  SESSION_STATES,
9022
10297
  SPAN_PREFIX,
9023
10298
  STATUS_WAIT_CAP_SECONDS,
9024
10299
  STRIP_PREFIX,
9025
10300
  SURFACE_KINDS,
10301
+ Score,
9026
10302
  Screen,
9027
10303
  SecretScrubber,
10304
+ TOOL_CONTRACT_UNREAD,
9028
10305
  TRANSFORMS,
9029
10306
  TaskEditClient,
10307
+ TaskGrades,
9030
10308
  TaskRun,
9031
10309
  TaskSessions,
9032
10310
  TransportCause,
9033
10311
  UnreachableError,
9034
10312
  VERIFIER_REWARD_PREFIX,
9035
10313
  WHITESPACE,
10314
+ WORLD_REQUEST_VIAS,
9036
10315
  WorldBrowser,
9037
10316
  WorldCallTooLarge,
9038
10317
  WorldEditClient,
10318
+ WorldNotice,
10319
+ WorldRequestFilterError,
10320
+ WorldRequestLedger,
9039
10321
  WorldRequestLog,
10322
+ WorldRequestQuery,
10323
+ WorldRequestsNotRecorded,
9040
10324
  WorldSandboxClient,
9041
10325
  WorldSession,
9042
10326
  WorldSessionCallError,
10327
+ WorldSessionOpenFailed,
9043
10328
  WorldSessionTimeout,
9044
10329
  WorldSet,
9045
10330
  WorldSurfaceUnavailable,
9046
10331
  WorldTask,
10332
+ WorldToolsUnread,
10333
+ WriteTreeFilter,
9047
10334
  aggregateTaskGrade,
9048
10335
  apiRequest,
9049
10336
  applyTransforms,
9050
10337
  archiveMode,
9051
10338
  assertBrowserTier,
10339
+ assertSessionAuthMode,
9052
10340
  assertTaskRef,
9053
10341
  attachManifest,
9054
10342
  attachSession,
9055
10343
  attachTask,
9056
10344
  buildFileManifest,
9057
10345
  buildSourceTarball,
10346
+ capabilityLabel,
9058
10347
  captureToolCalls,
10348
+ carriedWarnings,
9059
10349
  checkoutIdentity,
9060
10350
  closeConnection,
9061
10351
  collectArchiveRelPaths,
@@ -9074,7 +10364,9 @@ init_worlds();
9074
10364
  idleWarning,
9075
10365
  ingest,
9076
10366
  ingestToolCalls,
10367
+ invalidFields,
9077
10368
  isBrowserGone,
10369
+ isSandboxCommitPending,
9078
10370
  isSandboxImportPending,
9079
10371
  isTaskIdRef,
9080
10372
  isTaskManifest,
@@ -9086,6 +10378,7 @@ init_worlds();
9086
10378
  listTaskSessions,
9087
10379
  listTasks,
9088
10380
  localTreeState,
10381
+ messageOf,
9089
10382
  migrateProjectWorlds,
9090
10383
  migrateWorld,
9091
10384
  modelToolName,
@@ -9120,6 +10413,7 @@ init_worlds();
9120
10413
  resolveBrowserMode,
9121
10414
  resolvePushSlug,
9122
10415
  roundForm,
10416
+ rowsRefusal,
9123
10417
  runSessions,
9124
10418
  runSet,
9125
10419
  runTask,
@@ -9129,10 +10423,14 @@ init_worlds();
9129
10423
  setEnvSecret,
9130
10424
  shapeText,
9131
10425
  snapshotTreeHashes,
10426
+ sqlRows,
10427
+ stillStartingHint,
9132
10428
  storedForms,
9133
10429
  surfaceDescriptor,
9134
10430
  surfaceKinds,
10431
+ taskGradeError,
9135
10432
  taskLabel,
10433
+ taskRunFailure,
9136
10434
  toolCallRecordsIn,
9137
10435
  toolCallsFromObservations,
9138
10436
  uiEntryUrl,
@@ -9144,6 +10442,7 @@ init_worlds();
9144
10442
  validateWorld,
9145
10443
  waitForRun,
9146
10444
  withoutUiToken,
10445
+ worldRequestsUnrecorded,
9147
10446
  writeLocalMeta
9148
10447
  });
9149
10448
  //# sourceMappingURL=index.js.map