@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
@@ -28,6 +28,28 @@ var __export = (target, all) => {
28
28
  __defProp(target, name, { get: all[name], enumerable: true });
29
29
  };
30
30
 
31
+ // src/worlds/platformEndpoint.ts
32
+ function platformEndpoint(options) {
33
+ const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
34
+ const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
35
+ if (!publicKey || !secretKey) return null;
36
+ const host = options.host ?? process.env.GATEWAY_HOST ?? DEFAULT_HOST;
37
+ return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
38
+ }
39
+ function requirePlatformEndpoint(options) {
40
+ const endpoint = platformEndpoint(options);
41
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
42
+ return endpoint;
43
+ }
44
+ var DEFAULT_HOST, GATEWAY_KEYS_MISSING;
45
+ var init_platformEndpoint = __esm({
46
+ "src/worlds/platformEndpoint.ts"() {
47
+ "use strict";
48
+ DEFAULT_HOST = "https://withgateway.ai";
49
+ GATEWAY_KEYS_MISSING = "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).";
50
+ }
51
+ });
52
+
31
53
  // src/worlds/transport.ts
32
54
  function platformTimeoutMs(env = process.env) {
33
55
  const raw = env[PLATFORM_TIMEOUT_ENV];
@@ -36,7 +58,7 @@ function platformTimeoutMs(env = process.env) {
36
58
  if (Number.isInteger(value) && value > 0) return value;
37
59
  throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
38
60
  }
39
- var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, PlatformTransport;
61
+ var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, FETCH_LIMIT, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, sleep, RetryPolicy, Attempt, PlatformTransport;
40
62
  var init_transport = __esm({
41
63
  "src/worlds/transport.ts"() {
42
64
  "use strict";
@@ -44,7 +66,13 @@ var init_transport = __esm({
44
66
  PLATFORM_TIMEOUT_DEFAULT_MS = 12e4;
45
67
  CHECK_HOST = "Check GATEWAY_HOST (or --host) points at a running platform, then retry.";
46
68
  RETRY = "Retry; if it repeats, check the network between here and the platform.";
69
+ FETCH_LIMIT = {
70
+ reason: "Node's fetch stopped waiting after 300 s",
71
+ next: "The platform may still be running it: check what it changed before you retry."
72
+ };
47
73
  CAUSES = {
74
+ UND_ERR_HEADERS_TIMEOUT: FETCH_LIMIT,
75
+ UND_ERR_BODY_TIMEOUT: FETCH_LIMIT,
48
76
  ECONNREFUSED: { reason: "connection refused", next: CHECK_HOST },
49
77
  ENOTFOUND: { reason: "DNS lookup failed", next: CHECK_HOST },
50
78
  EAI_AGAIN: { reason: "DNS lookup failed", next: CHECK_HOST },
@@ -98,6 +126,56 @@ var init_transport = __esm({
98
126
  route;
99
127
  failure;
100
128
  };
129
+ sleep = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
130
+ RetryPolicy = class _RetryPolicy {
131
+ constructor(delaysMs, wait = sleep, budgetMs = Number.POSITIVE_INFINITY, now = Date.now) {
132
+ this.delaysMs = delaysMs;
133
+ this.wait = wait;
134
+ this.budgetMs = budgetMs;
135
+ this.now = now;
136
+ }
137
+ delaysMs;
138
+ wait;
139
+ budgetMs;
140
+ now;
141
+ static NONE = new _RetryPolicy([]);
142
+ /** A platform restart or a short edge outage: no wait starts that would end past five minutes, so a step ends
143
+ * within the budget plus one request timeout. */
144
+ static FILING = new _RetryPolicy([250, 500, 1e3, 2e3, 4e3, 8e3, 16e3, 3e4], sleep, 3e5);
145
+ static TRANSIENT_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
146
+ async run(attempt) {
147
+ const started = this.now();
148
+ for (let tries = 0; ; tries += 1) {
149
+ const outcome = await Attempt.of(attempt);
150
+ const delayMs = this.delaysMs[tries];
151
+ const spent = delayMs === void 0 || this.now() - started + delayMs >= this.budgetMs;
152
+ if (!outcome.transient || spent) return outcome.settle();
153
+ await this.wait(delayMs);
154
+ }
155
+ }
156
+ };
157
+ Attempt = class _Attempt {
158
+ constructor(reply, error, transient) {
159
+ this.reply = reply;
160
+ this.error = error;
161
+ this.transient = transient;
162
+ }
163
+ reply;
164
+ error;
165
+ transient;
166
+ static async of(attempt) {
167
+ try {
168
+ const reply = await attempt();
169
+ return new _Attempt(reply, null, RetryPolicy.TRANSIENT_STATUSES.has(reply.status));
170
+ } catch (error) {
171
+ return new _Attempt(null, error, error instanceof UnreachableError);
172
+ }
173
+ }
174
+ settle() {
175
+ if (this.reply) return this.reply;
176
+ throw this.error;
177
+ }
178
+ };
101
179
  PlatformTransport = class {
102
180
  constructor(endpoint, timeoutMs = platformTimeoutMs()) {
103
181
  this.endpoint = endpoint;
@@ -111,6 +189,9 @@ var init_transport = __esm({
111
189
  }
112
190
  async send(method, route, body, opts = {}) {
113
191
  const timeoutMs = this.budget(opts.timeoutMs);
192
+ return (opts.retry ?? RetryPolicy.NONE).run(() => this.once(method, route, body, timeoutMs));
193
+ }
194
+ async once(method, route, body, timeoutMs) {
114
195
  const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
115
196
  const controller = new AbortController();
116
197
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -291,6 +372,12 @@ var init_browserReconnect = __esm({
291
372
  });
292
373
 
293
374
  // src/worlds/browser.ts
375
+ function assertSessionAuthMode(value) {
376
+ if (SESSION_AUTH_MODES.includes(value)) return;
377
+ throw new Error(
378
+ `Unknown session auth: ${String(value)}; choose from ${SESSION_AUTH_MODES.join(", ")}`
379
+ );
380
+ }
294
381
  function assertBrowserTier(value) {
295
382
  if (BROWSER_TIERS.includes(value)) return;
296
383
  throw new Error(
@@ -304,9 +391,9 @@ function surfaceKinds(s) {
304
391
  if (s.browser) kinds.push("browser");
305
392
  return kinds;
306
393
  }
307
- function uiUrl(ui, path12) {
394
+ function uiUrl(ui, path13) {
308
395
  const base = ui.url.replace(/\/+$/, "");
309
- const rel = path12.startsWith("/") ? path12 : `/${path12}`;
396
+ const rel = path13.startsWith("/") ? path13 : `/${path13}`;
310
397
  if (!ui.token) return base + rel;
311
398
  const [beforeHash, hash] = splitHash(rel);
312
399
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -327,6 +414,15 @@ function splitHash(url) {
327
414
  const h = url.indexOf("#");
328
415
  return h < 0 ? [url, ""] : [url.slice(0, h), url.slice(h)];
329
416
  }
417
+ function capabilityLabel(url) {
418
+ let host = "";
419
+ try {
420
+ host = url ? new URL(url).hostname : "";
421
+ } catch {
422
+ return null;
423
+ }
424
+ return CAPABILITY_HOST.exec(host)?.[1] ?? null;
425
+ }
330
426
  function redactActionArgs(action, args) {
331
427
  const secretField = action === "browser_type" && SECRET_KEY.test(String(args.selector ?? ""));
332
428
  const out = {};
@@ -386,7 +482,9 @@ async function openBrowser(surfaces, opts = {}) {
386
482
  };
387
483
  const scrubber = new SecretScrubber([
388
484
  surfaces.browser?.wsUrl,
389
- surfaces.ui?.token
485
+ surfaces.ui?.token,
486
+ capabilityLabel(surfaces.ui?.url),
487
+ capabilityLabel(surfaces.api?.url)
390
488
  ]);
391
489
  const reconnector = mode === "hosted" && surfaces.browser.protocol === "cdp" && opts.repair ? new BrowserReconnector(opts.repair, reattach, {
392
490
  scrub: (text2) => scrubber.scrub(text2)
@@ -451,13 +549,15 @@ function computerUseToolkit(browser, opts = {}) {
451
549
  specs
452
550
  };
453
551
  }
454
- 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;
552
+ 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;
455
553
  var init_browser = __esm({
456
554
  "src/worlds/browser.ts"() {
457
555
  "use strict";
458
556
  init_session2();
459
557
  init_browserReconnect();
460
558
  SURFACE_KINDS = ["tools", "api", "ui", "browser"];
559
+ PLATFORM_SESSION_HEADER = "X-World-Session";
560
+ SESSION_AUTH_MODES = ["vendor", "open"];
461
561
  BROWSER_TIERS = ["standard", "premium"];
462
562
  DEFAULT_VIEWPORT = { width: 1280, height: 800 };
463
563
  SETTLE_TIMEOUT_MS = 5e3;
@@ -478,6 +578,7 @@ var init_browser = __esm({
478
578
  surface;
479
579
  };
480
580
  UI_TOKEN_QUERY = "token";
581
+ CAPABILITY_HOST = /^o-[a-z0-9]+-([a-z2-7]{26})(?:-api)?\./;
481
582
  BrowserActionRecorder = class {
482
583
  constructor(post, agentId, opts = {}) {
483
584
  this.post = post;
@@ -547,7 +648,7 @@ var init_browser = __esm({
547
648
  }
548
649
  async send(batch) {
549
650
  const retries = this.opts.maxRetries ?? EVENT_MAX_RETRIES;
550
- const sleep2 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
651
+ const sleep4 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
551
652
  for (let attempt = 0; attempt < retries; attempt++) {
552
653
  try {
553
654
  const base = await this.resolveBase();
@@ -564,13 +665,16 @@ var init_browser = __esm({
564
665
  );
565
666
  return;
566
667
  }
567
- await sleep2(500 * 2 ** attempt);
668
+ await sleep4(500 * 2 ** attempt);
568
669
  }
569
670
  }
570
671
  }
571
672
  };
572
673
  WorldBrowser = class {
573
- constructor(page, ui, mode, teardown, viewport = DEFAULT_VIEWPORT, recorder = null, reconnector = null, scrubber = new SecretScrubber([ui?.token])) {
674
+ constructor(page, ui, mode, teardown, viewport = DEFAULT_VIEWPORT, recorder = null, reconnector = null, scrubber = new SecretScrubber([
675
+ ui?.token,
676
+ capabilityLabel(ui?.url)
677
+ ])) {
574
678
  this.ui = ui;
575
679
  this.mode = mode;
576
680
  this.teardown = teardown;
@@ -707,7 +811,15 @@ var init_browser = __esm({
707
811
  seq: recorder.nextSeq(),
708
812
  ts: (/* @__PURE__ */ new Date()).toISOString(),
709
813
  action,
710
- args: redactActionArgs(action, args),
814
+ args: redactActionArgs(
815
+ action,
816
+ Object.fromEntries(
817
+ Object.entries(args).map(([key, value]) => [
818
+ key,
819
+ typeof value === "string" ? this.scrubber.scrub(value) : value
820
+ ])
821
+ )
822
+ ),
711
823
  ok: failure === null,
712
824
  error,
713
825
  durationMs: Date.now() - started,
@@ -723,8 +835,8 @@ var init_browser = __esm({
723
835
  screenshot: null
724
836
  };
725
837
  try {
726
- out.url = this.page.url();
727
- out.title = await this.page.title();
838
+ out.url = this.scrubber.scrub(this.page.url());
839
+ out.title = this.scrubber.scrub(await this.page.title());
728
840
  const jpeg = await this.page.screenshot({
729
841
  type: "jpeg",
730
842
  quality: EVENT_SCREENSHOT_QUALITY
@@ -1201,13 +1313,13 @@ var init_screen = __esm({
1201
1313
  });
1202
1314
  }
1203
1315
  async drag(action) {
1204
- const path12 = action.args.path ?? [];
1205
- if (path12.length < 2) throw new Error("a drag needs at least two points");
1316
+ const path13 = action.args.path ?? [];
1317
+ if (path13.length < 2) throw new Error("a drag needs at least two points");
1206
1318
  return this.held(action.args.modifiers ?? [], async () => {
1207
- await this.moveTo(path12[0]);
1319
+ await this.moveTo(path13[0]);
1208
1320
  await this.page.mouse.down({ button: "left" });
1209
1321
  try {
1210
- for (const point2 of path12.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1322
+ for (const point2 of path13.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1211
1323
  } finally {
1212
1324
  await this.page.mouse.up({ button: "left" });
1213
1325
  }
@@ -1298,7 +1410,7 @@ var init_media = __esm({
1298
1410
  put;
1299
1411
  maxBytes;
1300
1412
  /** The uploader for `GATEWAY_HOST` / `GATEWAY_PUBLIC_KEY` /
1301
- * `GATEWAY_SECRET_KEY` (the keys tracing exports with); null when any is unset. */
1413
+ * `GATEWAY_SECRET_KEY` only; null when any is unset, so a run traced with a saved login uploads none. */
1302
1414
  static fromEnv(env = process.env) {
1303
1415
  const host = env.GATEWAY_HOST;
1304
1416
  const publicKey = env.GATEWAY_PUBLIC_KEY;
@@ -2168,13 +2280,16 @@ function idleWarning(idleExpiresAt, now = /* @__PURE__ */ new Date()) {
2168
2280
  const minutes = Math.max(0, Math.floor(leftMs / 6e4));
2169
2281
  return `warning: session expires in ${minutes}m (idle TTL); call a tool to extend it or \`gateway worlds session pin\` to keep it`;
2170
2282
  }
2171
- var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge;
2283
+ 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;
2172
2284
  var init_sessionContract = __esm({
2173
2285
  "src/worlds/sessionContract.ts"() {
2174
2286
  "use strict";
2175
2287
  CALL_BODY_LIMIT_BYTES = 4 * 1024 * 1024;
2176
2288
  CALL_BODY_LIMIT_LABEL = "4mb";
2177
2289
  IDLE_WARNING_MINUTES = 5;
2290
+ REQUEST_LOG_PAGE_MAX = 200;
2291
+ REQUEST_BODY_CAPTURE_BYTES = 64 * 1024;
2292
+ REQUESTS_NOT_RECORDED = "requests_not_recorded";
2178
2293
  EXPLORATORY_TASK = Object.freeze({
2179
2294
  instruction: "exploratory session"
2180
2295
  });
@@ -2204,6 +2319,276 @@ var init_sessionContract = __esm({
2204
2319
  };
2205
2320
  }
2206
2321
  };
2322
+ stillStartingHint = (sessionId) => `It keeps opening: poll \`gateway worlds session status ${sessionId}\` until ready, or end it with \`gateway worlds session close ${sessionId}\`.`;
2323
+ }
2324
+ });
2325
+
2326
+ // src/worlds/toolSchema.ts
2327
+ function schemaFromSignature(spec) {
2328
+ if (spec.inputSchema)
2329
+ return {
2330
+ type: "function",
2331
+ function: {
2332
+ name: modelToolName(spec.name),
2333
+ description: spec.doc ?? spec.name,
2334
+ parameters: spec.inputSchema
2335
+ }
2336
+ };
2337
+ const inside = /\(([\s\S]*)\)\s*$/.exec(spec.signature.trim())?.[1] ?? "";
2338
+ const properties = {};
2339
+ const required2 = [];
2340
+ const params = [];
2341
+ let depth = 0;
2342
+ let current = "";
2343
+ for (const ch of inside) {
2344
+ if ("([{".includes(ch)) depth++;
2345
+ if (")]}".includes(ch)) depth--;
2346
+ if (ch === "," && depth === 0) {
2347
+ params.push(current);
2348
+ current = "";
2349
+ } else current += ch;
2350
+ }
2351
+ if (current.trim()) params.push(current);
2352
+ for (const raw of params) {
2353
+ const param = raw.trim();
2354
+ if (!param || param.startsWith("*") || param === "self") continue;
2355
+ const eq = param.indexOf("=");
2356
+ const name = (eq === -1 ? param : param.slice(0, eq)).split(":")[0].trim();
2357
+ if (!name) continue;
2358
+ if (eq === -1) {
2359
+ properties[name] = { type: "string" };
2360
+ required2.push(name);
2361
+ continue;
2362
+ }
2363
+ const value = param.slice(eq + 1).trim();
2364
+ if (/^-?\d+(\.\d+)?$/.test(value)) properties[name] = { type: "number" };
2365
+ else if (value === "True" || value === "False")
2366
+ properties[name] = { type: "boolean" };
2367
+ else properties[name] = { type: "string" };
2368
+ }
2369
+ return {
2370
+ type: "function",
2371
+ function: {
2372
+ name: modelToolName(spec.name),
2373
+ description: spec.doc ?? spec.name,
2374
+ parameters: { type: "object", properties, required: required2 }
2375
+ }
2376
+ };
2377
+ }
2378
+ var modelToolName;
2379
+ var init_toolSchema = __esm({
2380
+ "src/worlds/toolSchema.ts"() {
2381
+ "use strict";
2382
+ modelToolName = (name) => name.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64) || "tool";
2383
+ }
2384
+ });
2385
+
2386
+ // src/worlds/requestLedger.ts
2387
+ function platformMessage(text2) {
2388
+ try {
2389
+ const { message } = JSON.parse(text2);
2390
+ return typeof message === "string" ? message : null;
2391
+ } catch {
2392
+ return null;
2393
+ }
2394
+ }
2395
+ var WORLD_REQUEST_VIAS, WorldRequestFilterError, NOT_RECORDED, WorldRequestsNotRecorded, STATUS, INSTANT, WORLDS, SESSIONS, Instant, WorldRequestQuery, WorldRequestLedger;
2396
+ var init_requestLedger = __esm({
2397
+ "src/worlds/requestLedger.ts"() {
2398
+ "use strict";
2399
+ init_worlds();
2400
+ init_sessionContract();
2401
+ WORLD_REQUEST_VIAS = ["api", "ui", "tool"];
2402
+ WorldRequestFilterError = class extends Error {
2403
+ };
2404
+ NOT_RECORDED = "This session's engine records no requests; only hosted World Host sessions do.";
2405
+ WorldRequestsNotRecorded = class _WorldRequestsNotRecorded extends Error {
2406
+ static from(error) {
2407
+ if (!(error instanceof ApiError) || error.code !== REQUESTS_NOT_RECORDED) return error;
2408
+ return new _WorldRequestsNotRecorded(platformMessage(error.text) || NOT_RECORDED);
2409
+ }
2410
+ static async mapped(read) {
2411
+ try {
2412
+ return await read;
2413
+ } catch (error) {
2414
+ throw _WorldRequestsNotRecorded.from(error);
2415
+ }
2416
+ }
2417
+ };
2418
+ STATUS = /^(?:[1-5]\d\d|[1-5]xx)$/;
2419
+ INSTANT = /^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?(Z|[+-]\d{2}:\d{2})?)?$/;
2420
+ WORLDS = "/api/public/worlds";
2421
+ SESSIONS = "/api/public/world-sessions";
2422
+ Instant = class _Instant {
2423
+ static parse(flag, raw) {
2424
+ const match = INSTANT.exec(raw);
2425
+ const millis = match ? _Instant.fromParts(match) : null;
2426
+ if (millis === null)
2427
+ throw new WorldRequestFilterError(
2428
+ `--${flag} must be an ISO 8601 date or time (2026-10-05, 2026-10-05T14:30:00Z), not \`${raw}\`.`
2429
+ );
2430
+ return millis;
2431
+ }
2432
+ static fromParts(match) {
2433
+ const [year, month, day, hour, minute, second] = [1, 2, 3, 4, 5, 6].map((i) => Number(match[i] ?? 0));
2434
+ const offset = _Instant.offsetMinutes(match[8]);
2435
+ if (offset === null || year < 1 || hour > 23 || minute > 59 || second > 59) return null;
2436
+ const date = /* @__PURE__ */ new Date(0);
2437
+ date.setUTCFullYear(year, month - 1, day);
2438
+ if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) return null;
2439
+ date.setUTCHours(hour, minute, second, Number((match[7] ?? "").padEnd(3, "0")));
2440
+ const utc = date.getTime() - offset * 6e4;
2441
+ const utcYear = new Date(utc).getUTCFullYear();
2442
+ return utcYear < 1 || utcYear > 9999 ? null : utc;
2443
+ }
2444
+ static offsetMinutes(zone) {
2445
+ if (zone === void 0 || zone === "Z") return 0;
2446
+ const hours = Number(zone.slice(1, 3));
2447
+ const minutes = Number(zone.slice(4, 6));
2448
+ if (hours > 23 || minutes > 59) return null;
2449
+ return (zone.startsWith("-") ? -1 : 1) * (hours * 60 + minutes);
2450
+ }
2451
+ };
2452
+ WorldRequestQuery = class _WorldRequestQuery {
2453
+ constructor(entries) {
2454
+ this.entries = entries;
2455
+ }
2456
+ entries;
2457
+ static parse(filter = {}) {
2458
+ const via = _WorldRequestQuery.via(filter.via);
2459
+ const status = _WorldRequestQuery.status(filter.status);
2460
+ const since = filter.since === void 0 ? void 0 : Instant.parse("since", filter.since);
2461
+ const until = filter.until === void 0 ? void 0 : Instant.parse("until", filter.until);
2462
+ if (since !== void 0 && until !== void 0 && since > until)
2463
+ throw new WorldRequestFilterError("--since must not be after --until.");
2464
+ const values = [
2465
+ ["session", filter.session],
2466
+ ["version", filter.version],
2467
+ ["via", via],
2468
+ ["operation", filter.operation],
2469
+ ["status", status],
2470
+ ["since", since === void 0 ? void 0 : new Date(since).toISOString()],
2471
+ ["until", until === void 0 ? void 0 : new Date(until).toISOString()],
2472
+ ["limit", filter.limit === void 0 ? void 0 : String(filter.limit)],
2473
+ ["cursor", filter.cursor]
2474
+ ];
2475
+ return new _WorldRequestQuery(values.filter((entry) => entry[1] !== void 0));
2476
+ }
2477
+ static via(value) {
2478
+ if (value === void 0) return void 0;
2479
+ const lower = value.toLowerCase();
2480
+ if (WORLD_REQUEST_VIAS.includes(lower)) return lower;
2481
+ throw new WorldRequestFilterError(`--via must be one of ${WORLD_REQUEST_VIAS.join(", ")}, not \`${value}\`.`);
2482
+ }
2483
+ static status(value) {
2484
+ if (value === void 0) return void 0;
2485
+ const lower = value.toLowerCase();
2486
+ if (STATUS.test(lower)) return lower;
2487
+ throw new WorldRequestFilterError(`--status must be a code (200) or a class (2xx), not \`${value}\`.`);
2488
+ }
2489
+ get filtered() {
2490
+ return this.entries.some(([key]) => key !== "limit");
2491
+ }
2492
+ params() {
2493
+ return this.entries.map(([key, value]) => [key, value]);
2494
+ }
2495
+ search() {
2496
+ return this.entries.length ? `?${new URLSearchParams(this.entries).toString()}` : "";
2497
+ }
2498
+ };
2499
+ WorldRequestLedger = class {
2500
+ constructor(options = {}) {
2501
+ this.options = options;
2502
+ }
2503
+ options;
2504
+ /** A page of the world's requests, newest first; `nextCursor` reads the page after it. Narrowed to a session
2505
+ * whose engine records none: `WorldRequestsNotRecorded`. */
2506
+ list(slug, query = WorldRequestQuery.parse()) {
2507
+ return WorldRequestsNotRecorded.mapped(
2508
+ apiRequest(
2509
+ this.options,
2510
+ "GET",
2511
+ `${WORLDS}/${encodeURIComponent(slug)}/requests${query.search()}`
2512
+ )
2513
+ );
2514
+ }
2515
+ /** One of the world's requests, with its headers and bodies. */
2516
+ get(slug, requestId) {
2517
+ return apiRequest(
2518
+ this.options,
2519
+ "GET",
2520
+ `${WORLDS}/${encodeURIComponent(slug)}/requests/${encodeURIComponent(requestId)}`
2521
+ );
2522
+ }
2523
+ /** A page of one session's requests, newest first; `WorldRequestsNotRecorded` when its engine records none. */
2524
+ forSession(sessionId, query = WorldRequestQuery.parse()) {
2525
+ return WorldRequestsNotRecorded.mapped(
2526
+ apiRequest(
2527
+ this.options,
2528
+ "GET",
2529
+ `${SESSIONS}/${encodeURIComponent(sessionId)}/requests${query.search()}`
2530
+ )
2531
+ );
2532
+ }
2533
+ };
2534
+ }
2535
+ });
2536
+
2537
+ // src/worlds/sessionReadiness.ts
2538
+ var READY_TIMEOUT_MS, READY_POLL_MS, sleep2, WorldSessionTimeout, WorldSessionCallError, WorldSessionOpenFailed, ENDED, SessionReadiness;
2539
+ var init_sessionReadiness = __esm({
2540
+ "src/worlds/sessionReadiness.ts"() {
2541
+ "use strict";
2542
+ READY_TIMEOUT_MS = 10 * 6e4;
2543
+ READY_POLL_MS = 2e3;
2544
+ sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
2545
+ WorldSessionTimeout = class extends Error {
2546
+ };
2547
+ WorldSessionCallError = class extends Error {
2548
+ };
2549
+ WorldSessionOpenFailed = class extends WorldSessionCallError {
2550
+ constructor(sessionId, state2, code, reason) {
2551
+ super(
2552
+ reason === null ? `World session ${sessionId} ${state2.toLowerCase()} before it was ready.` : `World session ${sessionId} failed to open: ${reason}${code ? ` (${code})` : ""}`
2553
+ );
2554
+ this.sessionId = sessionId;
2555
+ this.state = state2;
2556
+ this.code = code;
2557
+ this.name = "WorldSessionOpenFailed";
2558
+ }
2559
+ sessionId;
2560
+ state;
2561
+ code;
2562
+ };
2563
+ ENDED = /* @__PURE__ */ new Set(["FAILED", "STOPPED"]);
2564
+ SessionReadiness = class {
2565
+ constructor(sessionId, status) {
2566
+ this.sessionId = sessionId;
2567
+ this.status = status;
2568
+ }
2569
+ sessionId;
2570
+ status;
2571
+ async wait(timeoutMs = READY_TIMEOUT_MS) {
2572
+ const deadline = Date.now() + timeoutMs;
2573
+ for (; ; ) {
2574
+ const state2 = await this.status();
2575
+ if (state2.ready) return state2;
2576
+ if (ENDED.has(state2.state))
2577
+ throw new WorldSessionOpenFailed(
2578
+ this.sessionId,
2579
+ state2.state,
2580
+ state2.failure?.code ?? null,
2581
+ state2.failure?.message ?? null
2582
+ );
2583
+ const left = deadline - Date.now();
2584
+ if (left <= 0)
2585
+ throw new WorldSessionTimeout(
2586
+ `World session ${this.sessionId} still ${state2.state} after ${Math.round(timeoutMs / 1e3)}s.`
2587
+ );
2588
+ await sleep2(Math.min(READY_POLL_MS, left));
2589
+ }
2590
+ }
2591
+ };
2207
2592
  }
2208
2593
  });
2209
2594
 
@@ -2299,11 +2684,184 @@ var init_taskRef = __esm({
2299
2684
  }
2300
2685
  });
2301
2686
 
2687
+ // src/worlds/worldNotice.ts
2688
+ var TOOL_CONTRACT_UNREAD, DEPRECATION, NOTICE_KINDS, WorldNotice, WorldToolsUnread;
2689
+ var init_worldNotice = __esm({
2690
+ "src/worlds/worldNotice.ts"() {
2691
+ "use strict";
2692
+ TOOL_CONTRACT_UNREAD = "tool-contract-unread";
2693
+ DEPRECATION = { label: "deprecated", next: "Instead", refusesRun: false };
2694
+ NOTICE_KINDS = /* @__PURE__ */ new Map([
2695
+ [TOOL_CONTRACT_UNREAD, { label: "warning", next: "Then", refusesRun: true }]
2696
+ ]);
2697
+ WorldNotice = class _WorldNotice {
2698
+ constructor(notice, kind) {
2699
+ this.notice = notice;
2700
+ this.kind = kind;
2701
+ }
2702
+ notice;
2703
+ kind;
2704
+ static of(notice) {
2705
+ return new _WorldNotice(notice, NOTICE_KINDS.get(notice.code) ?? DEPRECATION);
2706
+ }
2707
+ get refusesRun() {
2708
+ return this.kind.refusesRun;
2709
+ }
2710
+ line() {
2711
+ const { code, message, replacement } = this.notice;
2712
+ return `${this.kind.label} (${code}): ${message} ${this.kind.next}: ${replacement}`;
2713
+ }
2714
+ };
2715
+ WorldToolsUnread = class _WorldToolsUnread extends Error {
2716
+ constructor(world, notice) {
2717
+ super(
2718
+ `refused before the agent ran, nothing filed: world ${world} has no readable tools: ${notice.notice.message} Then: ${notice.notice.replacement}`
2719
+ );
2720
+ this.world = world;
2721
+ this.notice = notice;
2722
+ this.name = "WorldToolsUnread";
2723
+ this.code = notice.notice.code;
2724
+ }
2725
+ world;
2726
+ notice;
2727
+ code;
2728
+ static refuse(world, notices) {
2729
+ const refusing = notices.map((notice) => WorldNotice.of(notice)).find((notice) => notice.refusesRun);
2730
+ if (refusing) throw new _WorldToolsUnread(world, refusing);
2731
+ }
2732
+ };
2733
+ }
2734
+ });
2735
+
2736
+ // src/worlds/grade.ts
2737
+ var messageOf, Score, GradeRefused, GradePayload;
2738
+ var init_grade = __esm({
2739
+ "src/worlds/grade.ts"() {
2740
+ "use strict";
2741
+ messageOf = (error) => (error instanceof Error ? error.message || error.name : String(error)) || "Error";
2742
+ Score = class _Score {
2743
+ static RULE = "a score is a number from 0 to 1";
2744
+ static admits(value) {
2745
+ return Number.isFinite(value) && value >= 0 && value <= 1;
2746
+ }
2747
+ /** Every number among `scores` that is not a score, as `'<key>' = <value>`
2748
+ * and the rule, or null when all of them are. Pairs, not a map, so that no
2749
+ * key hides another. */
2750
+ static problem(scores2) {
2751
+ const refused = [...scores2].flatMap(
2752
+ ([key, value]) => typeof value === "number" && !_Score.admits(value) ? [`'${key}' = ${String(value)}`] : []
2753
+ );
2754
+ return refused.length ? `${refused.join(", ")}; ${_Score.RULE}` : null;
2755
+ }
2756
+ };
2757
+ GradeRefused = class extends Error {
2758
+ name = "GradeRefused";
2759
+ };
2760
+ GradePayload = class _GradePayload {
2761
+ /** A grade payload's numbers, checked: every number in it must be a score,
2762
+ * and its rewards and verifiers objects by name, or the grade is refused; a
2763
+ * cross-world verifier's grade that breaks either rule is that verifier's
2764
+ * error, the way the World Host answers one that could not grade. */
2765
+ static read(raw) {
2766
+ if (!_GradePayload.objectOrAbsent(raw))
2767
+ throw new GradeRefused("the grade is not an object");
2768
+ const record = _GradePayload.record(raw);
2769
+ const problem = _GradePayload.problem(record) ?? _GradePayload.verifiersProblem(record.verifiers);
2770
+ if (problem) throw new GradeRefused(problem);
2771
+ const verifiers = _GradePayload.verifiers(record.verifiers);
2772
+ return {
2773
+ ..._GradePayload.numbers(record),
2774
+ ...typeof record.reason === "string" ? { reason: record.reason } : {},
2775
+ ...typeof record.error === "string" ? { error: record.error } : {},
2776
+ ...verifiers ? { verifiers } : {}
2777
+ };
2778
+ }
2779
+ /** A world whose grade call failed (the platform refused it, the host had
2780
+ * lost the session): that world's error, never a score. */
2781
+ static failedCall(error) {
2782
+ return { ..._GradePayload.failed(messageOf(error)), raw: null };
2783
+ }
2784
+ static problem(record) {
2785
+ if (!_GradePayload.objectOrAbsent(record.rewards))
2786
+ return "the grade's rewards are not an object";
2787
+ const scores2 = Score.problem(_GradePayload.scores(record));
2788
+ if (scores2) return `the grade answered ${scores2}`;
2789
+ const twice = _GradePayload.twice(record);
2790
+ return twice.length ? `the grade answered ${twice.map((key) => `'${key}'`).join(", ")} both beside and inside rewards` : null;
2791
+ }
2792
+ static twice(record) {
2793
+ return Object.entries(_GradePayload.record(record.rewards)).filter(
2794
+ ([key, value]) => key !== "reward" && typeof value === "number" && typeof record[key] === "number"
2795
+ ).map(([key]) => key);
2796
+ }
2797
+ static verifiersProblem(value) {
2798
+ return _GradePayload.objectOrAbsent(value) ? null : "the grade's verifiers are not an object";
2799
+ }
2800
+ static scores(record) {
2801
+ return [
2802
+ ["reward", record.reward],
2803
+ ...Object.entries(_GradePayload.record(record.rewards)),
2804
+ ...Object.entries(record).filter(
2805
+ ([key]) => key !== "reward" && key !== "rewards"
2806
+ )
2807
+ ];
2808
+ }
2809
+ static isRecord(value) {
2810
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2811
+ }
2812
+ static objectOrAbsent(value) {
2813
+ return value === void 0 || value === null || _GradePayload.isRecord(value);
2814
+ }
2815
+ static record(value) {
2816
+ return _GradePayload.isRecord(value) ? value : {};
2817
+ }
2818
+ /** `reward`, and every numeric key under `rewards` (the host's shape) or at
2819
+ * the top level (a bundled verifier's). */
2820
+ static numbers(record) {
2821
+ const rewards = {};
2822
+ for (const [key, value] of Object.entries(
2823
+ _GradePayload.record(record.rewards)
2824
+ ))
2825
+ if (typeof value === "number") rewards[key] = value;
2826
+ for (const [key, value] of Object.entries(record))
2827
+ if (key !== "reward" && typeof value === "number") rewards[key] = value;
2828
+ return {
2829
+ reward: typeof record.reward === "number" ? record.reward : null,
2830
+ rewards
2831
+ };
2832
+ }
2833
+ static verifiers(value) {
2834
+ if (!_GradePayload.isRecord(value)) return void 0;
2835
+ return Object.fromEntries(
2836
+ Object.entries(value).map(([name, grade]) => [
2837
+ name,
2838
+ _GradePayload.verifier(name, grade)
2839
+ ])
2840
+ );
2841
+ }
2842
+ static verifier(name, grade) {
2843
+ if (!_GradePayload.isRecord(grade))
2844
+ return _GradePayload.failed(`the grade's verifier '${name}' is not an object`);
2845
+ const problem = _GradePayload.problem(grade);
2846
+ if (problem) return _GradePayload.failed(problem);
2847
+ return {
2848
+ ..._GradePayload.numbers(grade),
2849
+ ...typeof grade.error === "string" ? { error: grade.error } : {}
2850
+ };
2851
+ }
2852
+ static failed(error) {
2853
+ return { reward: null, rewards: {}, error };
2854
+ }
2855
+ };
2856
+ }
2857
+ });
2858
+
2302
2859
  // src/worlds/session.ts
2303
2860
  var session_exports2 = {};
2304
2861
  __export(session_exports2, {
2305
2862
  WorldSession: () => WorldSession,
2306
2863
  WorldSessionCallError: () => WorldSessionCallError,
2864
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
2307
2865
  WorldSessionTimeout: () => WorldSessionTimeout,
2308
2866
  attachSession: () => attachSession,
2309
2867
  isTaskIdRef: () => isTaskIdRef,
@@ -2315,85 +2873,6 @@ __export(session_exports2, {
2315
2873
  taskLabel: () => taskLabel
2316
2874
  });
2317
2875
  import { randomUUID } from "crypto";
2318
- function numericGrade(record) {
2319
- const rewards = {};
2320
- const nested = record.rewards;
2321
- if (nested && typeof nested === "object") {
2322
- for (const [key, value] of Object.entries(nested)) {
2323
- if (typeof value === "number") rewards[key] = value;
2324
- }
2325
- }
2326
- for (const [key, value] of Object.entries(record)) {
2327
- if (key !== "reward" && typeof value === "number") rewards[key] = value;
2328
- }
2329
- return {
2330
- reward: typeof record.reward === "number" ? record.reward : null,
2331
- rewards
2332
- };
2333
- }
2334
- function verifierGrades(value) {
2335
- if (!value || typeof value !== "object") return void 0;
2336
- const out = {};
2337
- for (const [name, grade] of Object.entries(value)) {
2338
- const record = grade && typeof grade === "object" ? grade : {};
2339
- out[name] = {
2340
- ...numericGrade(record),
2341
- ...typeof record.error === "string" ? { error: record.error } : {}
2342
- };
2343
- }
2344
- return out;
2345
- }
2346
- function schemaFromSignature(spec) {
2347
- if (spec.inputSchema)
2348
- return {
2349
- type: "function",
2350
- function: {
2351
- name: modelToolName(spec.name),
2352
- description: spec.doc ?? spec.name,
2353
- parameters: spec.inputSchema
2354
- }
2355
- };
2356
- const inside = /\(([\s\S]*)\)\s*$/.exec(spec.signature.trim())?.[1] ?? "";
2357
- const properties = {};
2358
- const required2 = [];
2359
- const params = [];
2360
- let depth = 0;
2361
- let current = "";
2362
- for (const ch of inside) {
2363
- if ("([{".includes(ch)) depth++;
2364
- if (")]}".includes(ch)) depth--;
2365
- if (ch === "," && depth === 0) {
2366
- params.push(current);
2367
- current = "";
2368
- } else current += ch;
2369
- }
2370
- if (current.trim()) params.push(current);
2371
- for (const raw of params) {
2372
- const param = raw.trim();
2373
- if (!param || param.startsWith("*") || param === "self") continue;
2374
- const eq = param.indexOf("=");
2375
- const name = (eq === -1 ? param : param.slice(0, eq)).split(":")[0].trim();
2376
- if (!name) continue;
2377
- if (eq === -1) {
2378
- properties[name] = { type: "string" };
2379
- required2.push(name);
2380
- continue;
2381
- }
2382
- const value = param.slice(eq + 1).trim();
2383
- if (/^-?\d+(\.\d+)?$/.test(value)) properties[name] = { type: "number" };
2384
- else if (value === "True" || value === "False")
2385
- properties[name] = { type: "boolean" };
2386
- else properties[name] = { type: "string" };
2387
- }
2388
- return {
2389
- type: "function",
2390
- function: {
2391
- name: modelToolName(spec.name),
2392
- description: spec.doc ?? spec.name,
2393
- parameters: { type: "object", properties, required: required2 }
2394
- }
2395
- };
2396
- }
2397
2876
  async function attachSession(sessionId, options = {}) {
2398
2877
  const state2 = await apiRequest(
2399
2878
  options,
@@ -2414,6 +2893,7 @@ async function openSession(slugOrRef, taskOrOpts = {}, positionalOpts = {}) {
2414
2893
  `Unknown world surfaces: ${unknown.join(", ")}; choose from ${SURFACE_KINDS.join(", ")}`
2415
2894
  );
2416
2895
  if (opts.browserTier !== void 0) assertBrowserTier(opts.browserTier);
2896
+ if (opts.auth !== void 0) assertSessionAuthMode(opts.auth);
2417
2897
  const at = slugOrRef.indexOf("@");
2418
2898
  const slug = at === -1 ? slugOrRef : slugOrRef.slice(0, at);
2419
2899
  const ref = at === -1 ? "" : slugOrRef.slice(at + 1);
@@ -2426,7 +2906,8 @@ async function openSession(slugOrRef, taskOrOpts = {}, positionalOpts = {}) {
2426
2906
  ...task === void 0 ? {} : { task },
2427
2907
  ...opts.reuseWarm === void 0 ? {} : { reuseWarm: opts.reuseWarm },
2428
2908
  ...opts.surfaces?.length ? { surfaces: opts.surfaces } : {},
2429
- ...opts.browserTier === void 0 ? {} : { browserTier: opts.browserTier }
2909
+ ...opts.browserTier === void 0 ? {} : { browserTier: opts.browserTier },
2910
+ ...opts.auth === void 0 ? {} : { auth: opts.auth }
2430
2911
  }
2431
2912
  );
2432
2913
  return new WorldSession(state2.sessionId, state2, options);
@@ -2450,6 +2931,7 @@ async function runSessions(opts) {
2450
2931
  const taskRef = tasks[next++];
2451
2932
  let task = taskLabel(taskRef);
2452
2933
  let session = null;
2934
+ let keepWarm = opts.keepWarm ?? true;
2453
2935
  try {
2454
2936
  session = await openSession(opts.world, {
2455
2937
  task: taskRef,
@@ -2457,6 +2939,7 @@ async function runSessions(opts) {
2457
2939
  options
2458
2940
  });
2459
2941
  task = taskLabel(taskRef, session.taskInfo);
2942
+ WorldToolsUnread.refuse(opts.world.split("@", 1)[0], session.deprecations);
2460
2943
  await session.ready();
2461
2944
  const toolkit = await session.toolkit();
2462
2945
  const live = session;
@@ -2479,19 +2962,19 @@ async function runSessions(opts) {
2479
2962
  results.push(result);
2480
2963
  opts.onResult?.(result);
2481
2964
  } catch (error) {
2965
+ if (error instanceof WorldToolsUnread) keepWarm = false;
2482
2966
  const result = {
2483
2967
  task,
2484
2968
  sessionId: session?.sessionId ?? "",
2485
2969
  reward: 0,
2486
2970
  rewards: {},
2487
- // An Error with an empty message would read as "no error" to any
2488
- // caller checking truthiness; name the type instead.
2971
+ // An empty message reads as "no error" to a truthiness check; name the type instead.
2489
2972
  error: (error instanceof Error ? error.message : String(error)) || (error instanceof Error ? error.name : "Unknown error")
2490
2973
  };
2491
2974
  results.push(result);
2492
2975
  opts.onResult?.(result);
2493
2976
  } finally {
2494
- await session?.close({ keepWarm: opts.keepWarm ?? true }).catch(() => void 0);
2977
+ await session?.close({ keepWarm }).catch(() => void 0);
2495
2978
  }
2496
2979
  }
2497
2980
  })
@@ -2505,7 +2988,7 @@ async function runSessions(opts) {
2505
2988
  passed: failed === 0 && (opts.failUnder === void 0 || mean >= opts.failUnder)
2506
2989
  };
2507
2990
  }
2508
- var 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;
2991
+ var CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep3, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
2509
2992
  var init_session2 = __esm({
2510
2993
  "src/worlds/session.ts"() {
2511
2994
  "use strict";
@@ -2513,22 +2996,21 @@ var init_session2 = __esm({
2513
2996
  init_browser();
2514
2997
  init_nativeComputer();
2515
2998
  init_sessionContract();
2999
+ init_toolSchema();
3000
+ init_requestLedger();
3001
+ init_sessionReadiness();
2516
3002
  init_session();
2517
3003
  init_taskRef();
3004
+ init_worldNotice();
3005
+ init_grade();
2518
3006
  init_taskRef();
2519
- READY_TIMEOUT_MS = 10 * 6e4;
2520
- READY_POLL_MS = 2e3;
3007
+ init_sessionReadiness();
2521
3008
  CALL_TIMEOUT_MS = 3 * 6e4;
2522
3009
  CALL_POLL_MS = 400;
2523
3010
  JUDGED_GRADE_TIMEOUT_MS = 15 * 6e4;
2524
3011
  JUDGED_GRADE_POLL_MS = 2e3;
2525
3012
  EXPORT_PAGE = 500;
2526
- sleep = (ms) => new Promise((r) => setTimeout(r, ms));
2527
- modelToolName = (name) => name.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64) || "tool";
2528
- WorldSessionTimeout = class extends Error {
2529
- };
2530
- WorldSessionCallError = class extends Error {
2531
- };
3013
+ sleep3 = (ms) => new Promise((r) => setTimeout(r, ms));
2532
3014
  WorldSession = class {
2533
3015
  constructor(sessionId, current, options) {
2534
3016
  this.sessionId = sessionId;
@@ -2598,12 +3080,12 @@ var init_session2 = __esm({
2598
3080
  );
2599
3081
  return api2.url;
2600
3082
  }
2601
- /** Headers every request to `api()` needs. A World Host session gates its
2602
- * api surface on a per-session token; a pod or hosted twin answers with the
2603
- * world's own declared auth and this is `{}`. */
3083
+ /** Headers every request to `api()` needs: a World Host session's token in the
3084
+ * platform's `X-World-Session` header, which every world takes; `{}` for a pod
3085
+ * or hosted twin, which answers with the world's own declared auth. */
2604
3086
  async apiHeaders() {
2605
3087
  const { api: api2 } = await this.surfaces();
2606
- return api2?.token ? { Authorization: `Bearer ${api2.token}` } : {};
3088
+ return api2?.token ? { [PLATFORM_SESSION_HEADER]: api2.token } : {};
2607
3089
  }
2608
3090
  /** URL of the world's UI entry page for this session. */
2609
3091
  async ui() {
@@ -2646,18 +3128,17 @@ var init_session2 = __esm({
2646
3128
  }
2647
3129
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
2648
3130
  browserRecorder(agentId) {
2649
- const path12 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
3131
+ const path13 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
2650
3132
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
2651
3133
  return new BrowserActionRecorder(
2652
3134
  async (events) => {
2653
- await apiRequest(this.options, "POST", path12, { events });
3135
+ await apiRequest(this.options, "POST", path13, { events });
2654
3136
  },
2655
3137
  id,
2656
3138
  {
2657
- // Continue this agent's timeline where the platform has it, so the
2658
- // same id reopened later never re-numbers from zero.
3139
+ // Continue this agent's timeline where the platform has it, so a reopened id never re-numbers from zero.
2659
3140
  resumeFrom: async () => {
2660
- const summary = await apiRequest(this.options, "GET", path12);
3141
+ const summary = await apiRequest(this.options, "GET", path13);
2661
3142
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
2662
3143
  }
2663
3144
  }
@@ -2675,39 +3156,49 @@ var init_session2 = __esm({
2675
3156
  }
2676
3157
  /** The session's latest platform state (one request, no waiting). */
2677
3158
  async status() {
3159
+ return this.refresh(
3160
+ await apiRequest(
3161
+ this.options,
3162
+ "GET",
3163
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}`
3164
+ )
3165
+ );
3166
+ }
3167
+ /** The world started over under the same id, URLs and token, on what its pin (or `version`, which becomes
3168
+ * its pin) resolves to now: base data plus the task's seed. A grade then reads only the new world's requests. */
3169
+ async restart(opts = {}) {
2678
3170
  const fresh = await apiRequest(
2679
3171
  this.options,
2680
- "GET",
2681
- `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}`
3172
+ "POST",
3173
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/restart`,
3174
+ opts.version ? { version: opts.version } : {}
2682
3175
  );
3176
+ this.refresh(fresh);
3177
+ return fresh;
3178
+ }
3179
+ // A status read carries neither the objective nor the open's notices; keep the ones we opened with.
3180
+ refresh(fresh) {
2683
3181
  this.current = {
2684
3182
  instruction: this.current.instruction,
2685
3183
  tools: this.current.tools,
2686
3184
  surfaces: this.current.surfaces,
2687
3185
  task: this.current.task,
3186
+ deprecations: this.current.deprecations,
2688
3187
  ...fresh
2689
3188
  };
2690
3189
  return this.current;
2691
3190
  }
2692
3191
  /**
2693
- * Wait until the container is warm — meaning the process inside it has
2694
- * asked the platform for work, not merely that a job was accepted.
3192
+ * Wait until the session answers calls: a World Host holds it, or the
3193
+ * process inside a pod has asked the platform for work. Throws
3194
+ * `WorldSessionOpenFailed` (with the platform's reason) when it ended
3195
+ * first, and `WorldSessionTimeout` when `timeoutMs` (default ten minutes)
3196
+ * ran out while it was still starting.
2695
3197
  */
2696
3198
  async ready(opts = {}) {
2697
- const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
2698
- for (; ; ) {
2699
- const state2 = await this.status();
2700
- if (state2.ready) return state2;
2701
- if (state2.state === "FAILED" || state2.state === "STOPPED")
2702
- throw new WorldSessionCallError(
2703
- `World session ${state2.state.toLowerCase()} before it was ready.`
2704
- );
2705
- if (Date.now() > deadline)
2706
- throw new WorldSessionTimeout(
2707
- `World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
2708
- );
2709
- await sleep(READY_POLL_MS);
2710
- }
3199
+ return new SessionReadiness(this.sessionId, () => this.status()).wait(
3200
+ opts.timeoutMs
3201
+ );
2711
3202
  }
2712
3203
  /** Enqueue one interaction and wait for the container's answer. */
2713
3204
  async exchange(body, timeoutMs) {
@@ -2721,7 +3212,7 @@ var init_session2 = __esm({
2721
3212
  const { callId } = enqueued;
2722
3213
  const judged = enqueued.pending === true;
2723
3214
  const budgetMs = timeoutMs ?? (judged ? JUDGED_GRADE_TIMEOUT_MS : CALL_TIMEOUT_MS);
2724
- const pollMs2 = judged ? JUDGED_GRADE_POLL_MS : CALL_POLL_MS;
3215
+ const pollMs = judged ? JUDGED_GRADE_POLL_MS : CALL_POLL_MS;
2725
3216
  const deadline = Date.now() + budgetMs;
2726
3217
  for (; ; ) {
2727
3218
  const call = await apiRequest(
@@ -2737,7 +3228,7 @@ var init_session2 = __esm({
2737
3228
  throw new WorldSessionTimeout(
2738
3229
  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.`
2739
3230
  );
2740
- await sleep(pollMs2);
3231
+ await sleep3(pollMs);
2741
3232
  }
2742
3233
  }
2743
3234
  /**
@@ -2786,7 +3277,9 @@ var init_session2 = __esm({
2786
3277
  * calls); assertions and python graders ignore it. `worlds` names the
2787
3278
  * task's other sessions by alias (`{alias: sessionId}`): the platform
2788
3279
  * reads each one's evidence and hands it to the cross-world verifiers
2789
- * anchored on this session, whose grades come back under `verifiers`. */
3280
+ * anchored on this session, whose grades come back under `verifiers`. A
3281
+ * score that is not a number from 0 to 1 is refused by key and value
3282
+ * (`GradeRefused`); a verifier's is that verifier's `error`. */
2790
3283
  async grade(opts = {}) {
2791
3284
  const raw = await this.exchange(
2792
3285
  {
@@ -2796,14 +3289,7 @@ var init_session2 = __esm({
2796
3289
  },
2797
3290
  opts.timeoutMs
2798
3291
  );
2799
- const record = raw && typeof raw === "object" ? raw : {};
2800
- const verifiers = verifierGrades(record.verifiers);
2801
- return {
2802
- ...numericGrade(record),
2803
- ...typeof record.reason === "string" ? { reason: record.reason } : {},
2804
- ...verifiers ? { verifiers } : {},
2805
- raw
2806
- };
3292
+ return { ...GradePayload.read(raw), raw };
2807
3293
  }
2808
3294
  /**
2809
3295
  * Put rows into the LIVE world through its contract, in one batch:
@@ -2835,19 +3321,31 @@ var init_session2 = __esm({
2835
3321
  async reset(opts = {}) {
2836
3322
  await this.exchange({ kind: "reset" }, opts.timeoutMs);
2837
3323
  }
3324
+ /** One page of the session's entries in its world's request ledger, newest
3325
+ * first and without bodies: at most `limit` after `cursor` (the last page's
3326
+ * `nextCursor`). Reading it records nothing in the session. */
3327
+ async requests(opts = {}) {
3328
+ return new WorldRequestLedger(this.options).forSession(
3329
+ this.sessionId,
3330
+ WorldRequestQuery.parse({ cursor: opts.cursor, limit: opts.limit ?? REQUEST_LOG_PAGE_MAX })
3331
+ );
3332
+ }
3333
+ page(route, limit, cursor) {
3334
+ const query = new URLSearchParams({ limit: String(limit) });
3335
+ if (cursor) query.set("cursor", cursor);
3336
+ return apiRequest(
3337
+ this.options,
3338
+ "GET",
3339
+ `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/${route}?${query.toString()}`
3340
+ );
3341
+ }
2838
3342
  /** The session's call log, oldest first: every tool call, seed, reset,
2839
3343
  * grade with its result — the record of the episode. */
2840
3344
  async export() {
2841
3345
  const calls = [];
2842
3346
  let cursor;
2843
3347
  do {
2844
- const params = new URLSearchParams({ limit: String(EXPORT_PAGE) });
2845
- if (cursor) params.set("cursor", cursor);
2846
- const page = await apiRequest(
2847
- this.options,
2848
- "GET",
2849
- `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/calls?${params.toString()}`
2850
- );
3348
+ const page = await this.page("calls", EXPORT_PAGE, cursor);
2851
3349
  for (const call of page.calls) {
2852
3350
  calls.push({
2853
3351
  seq: Number(call.seq),
@@ -2929,6 +3427,7 @@ var init_session2 = __esm({
2929
3427
  "reuseWarm",
2930
3428
  "surfaces",
2931
3429
  "browserTier",
3430
+ "auth",
2932
3431
  "options"
2933
3432
  ]);
2934
3433
  isOpenSessionOptions = (value) => typeof value === "object" && value !== null && Object.keys(value).every((key) => OPEN_SESSION_OPTION_KEYS.has(key));
@@ -3043,11 +3542,50 @@ var init_tasks = __esm({
3043
3542
  }
3044
3543
  });
3045
3544
 
3545
+ // src/worlds/taskGrades.ts
3546
+ var TASKS2, compact2, TaskGrades;
3547
+ var init_taskGrades = __esm({
3548
+ "src/worlds/taskGrades.ts"() {
3549
+ "use strict";
3550
+ init_worlds();
3551
+ TASKS2 = "/api/public/world-tasks";
3552
+ compact2 = (body) => Object.fromEntries(Object.entries(body).filter(([, v]) => v !== void 0));
3553
+ TaskGrades = class {
3554
+ constructor(options = {}) {
3555
+ this.options = options;
3556
+ }
3557
+ options;
3558
+ try(sessionId, target) {
3559
+ return apiRequest(
3560
+ this.options,
3561
+ "POST",
3562
+ `${TASKS2}/try`,
3563
+ compact2({ sessionId, ...target })
3564
+ );
3565
+ }
3566
+ regrade(taskId, scope = {}) {
3567
+ return apiRequest(
3568
+ this.options,
3569
+ "POST",
3570
+ `${TASKS2}/${encodeURIComponent(taskId)}/regrade`,
3571
+ compact2({ ...scope })
3572
+ );
3573
+ }
3574
+ };
3575
+ }
3576
+ });
3577
+
3046
3578
  // src/worlds/surfaceDescriptor.ts
3047
3579
  function surfaceDescriptor(s) {
3048
3580
  return {
3049
3581
  surfaces: surfaceKinds(s),
3050
- ...s.api ? { api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} } } : {},
3582
+ ...s.api ? {
3583
+ api: {
3584
+ url: s.api.url,
3585
+ ...s.api.token ? { token: s.api.token } : {},
3586
+ ...s.api.auth ? { auth: s.api.auth } : {}
3587
+ }
3588
+ } : {},
3051
3589
  ...s.ui ? { ui: { url: s.ui.url, entry: uiEntryUrl(s.ui), apiPath: s.ui.apiPath ?? null } } : {},
3052
3590
  ...s.browser ? {
3053
3591
  browser: {
@@ -3067,26 +3605,28 @@ var init_surfaceDescriptor = __esm({
3067
3605
 
3068
3606
  // src/worlds/taskSessions.ts
3069
3607
  function aggregateTaskGrade(worlds) {
3070
- const rewards = {};
3071
- const ungraded = [];
3072
- const graded = [];
3608
+ const fold = new TaskGradeFold();
3073
3609
  const verifiers = {};
3074
- const fold = (key, grade) => {
3075
- if (typeof grade.reward === "number") {
3076
- rewards[key] = grade.reward;
3077
- graded.push(grade.reward);
3078
- } else ungraded.push(key);
3079
- for (const [name, value] of Object.entries(grade.rewards))
3080
- rewards[`${key}.${name}`] = value;
3081
- };
3082
3610
  for (const [alias, grade] of Object.entries(worlds)) {
3083
- fold(alias, grade);
3611
+ fold.add(alias, grade, `world ${alias}`);
3084
3612
  Object.assign(verifiers, grade.verifiers ?? {});
3085
3613
  }
3086
3614
  for (const [name, grade] of Object.entries(verifiers))
3087
- fold(`${VERIFIER_REWARD_PREFIX}${name}`, grade);
3088
- const reward = graded.length > 0 ? graded.reduce((a, b) => a + b, 0) / graded.length : null;
3089
- return { reward, rewards, worlds, ungraded, verifiers };
3615
+ fold.add(`${VERIFIER_REWARD_PREFIX}${name}`, grade, `verifier ${name}`);
3616
+ return {
3617
+ reward: fold.reward(),
3618
+ rewards: fold.rewards,
3619
+ worlds,
3620
+ ungraded: fold.ungraded,
3621
+ verifiers,
3622
+ errors: fold.errors
3623
+ };
3624
+ }
3625
+ function taskGradeError(grade) {
3626
+ const lines = Object.entries(grade.errors).map(
3627
+ ([key, why]) => `${key}: ${why}`
3628
+ );
3629
+ return lines.length ? lines.join("; ") : null;
3090
3630
  }
3091
3631
  function fromResponse(answer, options) {
3092
3632
  const sessions = {};
@@ -3095,8 +3635,7 @@ function fromResponse(answer, options) {
3095
3635
  sessions[alias] = new WorldSession(state2.sessionId, state2, options);
3096
3636
  worlds[alias] = {
3097
3637
  slug: state2.slug,
3098
- ref: state2.ref,
3099
- private: state2.private ?? null
3638
+ ref: state2.ref
3100
3639
  };
3101
3640
  }
3102
3641
  return new TaskSessions(
@@ -3171,7 +3710,6 @@ async function attachTask(taskId, options = {}) {
3171
3710
  {
3172
3711
  slug: row.slug ?? "",
3173
3712
  ref: null,
3174
- private: null,
3175
3713
  versionId: row.versionId,
3176
3714
  sessionId: row.sessionId,
3177
3715
  ready: row.ready,
@@ -3201,7 +3739,7 @@ async function attachManifest(manifest, options = {}) {
3201
3739
  Object.fromEntries(
3202
3740
  Object.entries(manifest.sessions).map(([alias, entry]) => [
3203
3741
  alias,
3204
- { slug: entry.slug, ref: entry.ref, private: entry.private ?? null }
3742
+ { slug: entry.slug, ref: entry.ref }
3205
3743
  ])
3206
3744
  )
3207
3745
  );
@@ -3209,12 +3747,13 @@ async function attachManifest(manifest, options = {}) {
3209
3747
  function isTaskManifest(value) {
3210
3748
  return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.sessions === "object" && value.sessions !== null && !("instruction" in value && "worlds" in value);
3211
3749
  }
3212
- var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskSessions;
3750
+ var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskGradeFold, TaskSessions;
3213
3751
  var init_taskSessions = __esm({
3214
3752
  "src/worlds/taskSessions.ts"() {
3215
3753
  "use strict";
3216
3754
  init_worlds();
3217
3755
  init_browser();
3756
+ init_grade();
3218
3757
  init_surfaceDescriptor();
3219
3758
  init_session2();
3220
3759
  TASK_SESSIONS = "/api/public/world-task-sessions";
@@ -3222,13 +3761,42 @@ var init_taskSessions = __esm({
3222
3761
  manifestSession = (s, state2, world) => ({
3223
3762
  slug: world.slug,
3224
3763
  ref: world.ref,
3225
- private: world.private,
3226
3764
  versionId: state2.versionId,
3227
3765
  sessionId: state2.sessionId,
3228
3766
  ready: state2.ready,
3229
3767
  state: state2.state,
3230
3768
  ...surfaceDescriptor(s)
3231
3769
  });
3770
+ TaskGradeFold = class {
3771
+ rewards = {};
3772
+ ungraded = [];
3773
+ errors = {};
3774
+ sources = /* @__PURE__ */ new Map();
3775
+ graded = [];
3776
+ add(key, grade, source) {
3777
+ if (grade.error) this.errors[key] = grade.error;
3778
+ if (typeof grade.reward === "number") {
3779
+ this.put(key, grade.reward, source);
3780
+ this.graded.push(grade.reward);
3781
+ } else this.ungraded.push(key);
3782
+ for (const [name, value] of Object.entries(grade.rewards))
3783
+ this.put(`${key}.${name}`, value, source);
3784
+ }
3785
+ reward() {
3786
+ if (Object.keys(this.errors).length > 0 || !this.graded.length)
3787
+ return null;
3788
+ return this.graded.reduce((a, b) => a + b, 0) / this.graded.length;
3789
+ }
3790
+ put(key, value, source) {
3791
+ const first = this.sources.get(key) ?? source;
3792
+ this.sources.set(key, first);
3793
+ if (first !== source) {
3794
+ this.errors[key] = `two rewards are named ${key} (${first} and ${source}); rename one of them`;
3795
+ return;
3796
+ }
3797
+ this.rewards[key] = value;
3798
+ }
3799
+ };
3232
3800
  TaskSessions = class {
3233
3801
  constructor(taskId, taskVersion, name, instruction, sessions, worlds, hash = null) {
3234
3802
  this.taskId = taskId;
@@ -3249,7 +3817,7 @@ var init_taskSessions = __esm({
3249
3817
  /** This alias's world as recorded at open/attach — what a `TaskRun`
3250
3818
  * filing needs to name, without exposing the whole map. */
3251
3819
  worldRef(alias) {
3252
- return this.worlds[alias] ?? { slug: "", ref: null, private: null };
3820
+ return this.worlds[alias] ?? { slug: "", ref: null };
3253
3821
  }
3254
3822
  /** The aliases, in the spec's order. */
3255
3823
  get aliases() {
@@ -3307,11 +3875,13 @@ var init_taskSessions = __esm({
3307
3875
  * agent's own account of the whole investigation) goes to every world's
3308
3876
  * judge; a world graded by assertions or python ignores it. Each grade
3309
3877
  * names the task's other sessions by alias, so the cross-world verifiers
3310
- * anchored on that world can read them. */
3878
+ * anchored on that world can read them. A world whose grade call fails is
3879
+ * that world's error: the task's grade names it and keeps every other
3880
+ * world's grade. */
3311
3881
  async grade(opts = {}) {
3312
3882
  return aggregateTaskGrade(
3313
3883
  await this.each(
3314
- (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) })
3884
+ (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) }).catch((error) => GradePayload.failedCall(error))
3315
3885
  )
3316
3886
  );
3317
3887
  }
@@ -3359,12 +3929,16 @@ var init_evals = __esm({
3359
3929
  "src/worlds/evals.ts"() {
3360
3930
  "use strict";
3361
3931
  init_worlds();
3932
+ init_transport();
3362
3933
  EVALUATIONS = "/api/public/benchmark-containers/evaluations";
3363
3934
  GatewayEvalsClient = class {
3364
- constructor(options = {}) {
3935
+ constructor(options = {}, retry = RetryPolicy.FILING) {
3365
3936
  this.options = options;
3937
+ this.retry = retry;
3366
3938
  }
3367
3939
  options;
3940
+ retry;
3941
+ keyed = /* @__PURE__ */ new Set();
3368
3942
  async createEvaluation(versionId, opts = {}) {
3369
3943
  const body = { versionId };
3370
3944
  if (opts.model !== void 0) body.model = opts.model;
@@ -3372,12 +3946,18 @@ var init_evals = __esm({
3372
3946
  if (opts.rolloutsPerExample !== void 0)
3373
3947
  body.rolloutsPerExample = opts.rolloutsPerExample;
3374
3948
  if (opts.metadata !== void 0) body.metadata = opts.metadata;
3949
+ if (opts.idempotencyKey !== void 0)
3950
+ body.idempotencyKey = opts.idempotencyKey;
3375
3951
  const answer = await apiRequest(
3376
3952
  this.options,
3377
3953
  "POST",
3378
3954
  EVALUATIONS,
3379
- body
3955
+ body,
3956
+ {
3957
+ retry: opts.idempotencyKey === void 0 ? RetryPolicy.NONE : this.retry
3958
+ }
3380
3959
  );
3960
+ if (opts.idempotencyKey !== void 0) this.keyed.add(answer.evaluationId);
3381
3961
  return answer.evaluationId;
3382
3962
  }
3383
3963
  async pushSamples(evaluationId, samples) {
@@ -3385,7 +3965,8 @@ var init_evals = __esm({
3385
3965
  this.options,
3386
3966
  "POST",
3387
3967
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/samples`,
3388
- { samples }
3968
+ { samples },
3969
+ { retry: this.retryFor(evaluationId) }
3389
3970
  );
3390
3971
  return answer.inserted ?? samples.length;
3391
3972
  }
@@ -3398,10 +3979,297 @@ var init_evals = __esm({
3398
3979
  this.options,
3399
3980
  "POST",
3400
3981
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/finalize`,
3401
- body
3982
+ body,
3983
+ { retry: this.retryFor(evaluationId) }
3402
3984
  );
3403
3985
  return answer.status;
3404
3986
  }
3987
+ retryFor(evaluationId) {
3988
+ return this.keyed.has(evaluationId) ? this.retry : RetryPolicy.NONE;
3989
+ }
3990
+ };
3991
+ }
3992
+ });
3993
+
3994
+ // src/worlds/runSignals.ts
3995
+ import { constants } from "os";
3996
+ var OWNER, RunSignals;
3997
+ var init_runSignals = __esm({
3998
+ "src/worlds/runSignals.ts"() {
3999
+ "use strict";
4000
+ OWNER = /* @__PURE__ */ Symbol("RunSignals");
4001
+ RunSignals = class _RunSignals {
4002
+ constructor(target = process) {
4003
+ this.target = target;
4004
+ }
4005
+ target;
4006
+ static SIGNALS = ["SIGINT", "SIGTERM"];
4007
+ cleanups = [];
4008
+ heardSignal = null;
4009
+ heardCount = 0;
4010
+ listening = false;
4011
+ left = false;
4012
+ listener = Object.assign(
4013
+ (signal) => this.heard(signal),
4014
+ { [OWNER]: this }
4015
+ );
4016
+ /** The signal that arrived, once one has: start nothing new. */
4017
+ get signal() {
4018
+ return this.owner().heardSignal;
4019
+ }
4020
+ async during(cleanup, work) {
4021
+ return this.owner().hold(cleanup, work);
4022
+ }
4023
+ owner() {
4024
+ const owners = this.target.listeners("SIGINT").map((listener) => listener[OWNER]);
4025
+ return owners.find((owner) => owner !== void 0) ?? this;
4026
+ }
4027
+ async hold(cleanup, work) {
4028
+ this.listen();
4029
+ this.cleanups.push(cleanup);
4030
+ try {
4031
+ return await work();
4032
+ } finally {
4033
+ this.cleanups.splice(this.cleanups.indexOf(cleanup), 1);
4034
+ if (!this.cleanups.length && this.heardSignal === null) this.unlisten();
4035
+ }
4036
+ }
4037
+ heard(signal) {
4038
+ this.heardCount += 1;
4039
+ if (this.heardCount > 2) return this.leave(signal);
4040
+ this.heardSignal ??= signal;
4041
+ void this.cleanUp(signal, this.heardCount === 2).finally(() => this.leave(signal));
4042
+ }
4043
+ async cleanUp(signal, urgent) {
4044
+ for (const cleanup of [...this.cleanups].reverse())
4045
+ await Promise.resolve().then(() => cleanup(signal, urgent)).catch(() => void 0);
4046
+ }
4047
+ leave(signal) {
4048
+ if (this.left) return;
4049
+ this.left = true;
4050
+ this.unlisten();
4051
+ if (this.target.listenerCount(signal) === 0)
4052
+ this.target.exit(128 + constants.signals[signal]);
4053
+ }
4054
+ listen() {
4055
+ if (this.listening) return;
4056
+ this.listening = true;
4057
+ for (const signal of _RunSignals.SIGNALS)
4058
+ this.target.on(signal, this.listener);
4059
+ }
4060
+ unlisten() {
4061
+ this.listening = false;
4062
+ for (const signal of _RunSignals.SIGNALS)
4063
+ this.target.off(signal, this.listener);
4064
+ }
4065
+ };
4066
+ }
4067
+ });
4068
+
4069
+ // src/worlds/taskRunDir.ts
4070
+ import * as fs from "fs";
4071
+ import * as path from "path";
4072
+ function sessionRecord({ api: api2, ui, browser, ...session }) {
4073
+ return {
4074
+ ...session,
4075
+ ...api2 ? { api: { url: api2.url } } : {},
4076
+ ...ui ? { ui: { ...ui, entry: withoutUiToken(ui.entry) } } : {},
4077
+ ...browser ? { browser: browserRecord(browser) } : {}
4078
+ };
4079
+ }
4080
+ var TaskRunDir, browserRecord;
4081
+ var init_taskRunDir = __esm({
4082
+ "src/worlds/taskRunDir.ts"() {
4083
+ "use strict";
4084
+ init_browser();
4085
+ TaskRunDir = class {
4086
+ constructor(path13) {
4087
+ this.path = path13;
4088
+ }
4089
+ path;
4090
+ writeManifest(manifest) {
4091
+ this.writeJson("manifest.json", {
4092
+ ...manifest,
4093
+ sessions: Object.fromEntries(
4094
+ Object.entries(manifest.sessions).map(([alias, session]) => [
4095
+ alias,
4096
+ sessionRecord(session)
4097
+ ])
4098
+ )
4099
+ });
4100
+ }
4101
+ writeJson(name, value) {
4102
+ this.write(name, JSON.stringify(value, null, 2));
4103
+ }
4104
+ write(name, text2) {
4105
+ fs.mkdirSync(this.path, { recursive: true });
4106
+ fs.writeFileSync(path.join(this.path, name), text2);
4107
+ }
4108
+ };
4109
+ browserRecord = ({ viewport, tier }) => ({
4110
+ viewport,
4111
+ ...tier ? { tier } : {}
4112
+ });
4113
+ }
4114
+ });
4115
+
4116
+ // src/worlds/taskRunOutcome.ts
4117
+ var TaskRunOutcome;
4118
+ var init_taskRunOutcome = __esm({
4119
+ "src/worlds/taskRunOutcome.ts"() {
4120
+ "use strict";
4121
+ init_grade();
4122
+ init_taskSessions();
4123
+ TaskRunOutcome = class {
4124
+ constructor(runId, live, model, runDir) {
4125
+ this.runId = runId;
4126
+ this.live = live;
4127
+ this.model = model;
4128
+ this.runDir = runDir;
4129
+ }
4130
+ runId;
4131
+ live;
4132
+ model;
4133
+ runDir;
4134
+ report = null;
4135
+ extra = {};
4136
+ notes = {};
4137
+ agentError = null;
4138
+ grade = null;
4139
+ traced = false;
4140
+ runError = null;
4141
+ fileError = null;
4142
+ evaluationId;
4143
+ evaluationStatus = null;
4144
+ started = Date.now();
4145
+ stopped(error) {
4146
+ this.runError ??= messageOf(error).slice(0, 2e3);
4147
+ }
4148
+ agentRaised(error) {
4149
+ this.agentError = messageOf(error).slice(0, 2e3);
4150
+ }
4151
+ failure() {
4152
+ const reasons = [
4153
+ this.agentError === null ? null : `agent raised: ${this.agentError}`,
4154
+ this.runError === null ? null : `the run stopped: ${this.runError}`,
4155
+ this.gradeError(),
4156
+ this.grade === null && this.runError === null ? "the run stopped before it was graded" : null
4157
+ ].filter((reason) => reason !== null);
4158
+ return reasons.length ? reasons.join("; ") : null;
4159
+ }
4160
+ async file(evals, setRunId) {
4161
+ let created;
4162
+ try {
4163
+ created = await evals.createEvaluation(this.ref(this.live.aliases[0]).versionId, {
4164
+ model: this.model,
4165
+ rolloutsPerExample: 1,
4166
+ idempotencyKey: this.runId,
4167
+ ...setRunId ? { runId: setRunId } : {},
4168
+ metadata: this.metadata()
4169
+ });
4170
+ await evals.pushSamples(created, [this.sample()]);
4171
+ this.evaluationStatus = await evals.finalizeEvaluation(created, this.finalized());
4172
+ this.evaluationId = created;
4173
+ } catch (error) {
4174
+ this.fileError = created ? `evaluation ${created}: ${messageOf(error)}` : messageOf(error);
4175
+ if (created)
4176
+ await evals.finalizeEvaluation(created, { status: "FAILED", metrics: { error: this.fileError } }).catch(() => void 0);
4177
+ }
4178
+ }
4179
+ summary() {
4180
+ const live = this.live;
4181
+ const { evaluationId: _agentEvaluationId, ...extra } = this.extra;
4182
+ return {
4183
+ ...extra,
4184
+ runId: this.runId,
4185
+ taskId: live.taskId,
4186
+ taskVersion: live.taskVersion,
4187
+ name: live.name,
4188
+ instruction: live.instruction ?? "",
4189
+ model: this.model,
4190
+ aliases: live.aliases,
4191
+ reward: this.grade?.reward ?? null,
4192
+ rewards: this.grade?.rewards ?? {},
4193
+ ungraded: this.grade?.ungraded ?? live.aliases,
4194
+ agentError: this.agentError,
4195
+ runError: this.runError,
4196
+ gradeError: this.gradeError(),
4197
+ fileError: this.fileError,
4198
+ evaluationStatus: this.evaluationStatus,
4199
+ report: this.report,
4200
+ traced: this.traced,
4201
+ runDir: this.runDir,
4202
+ durationSeconds: Math.round((Date.now() - this.started) / 10) / 100,
4203
+ notes: this.notes,
4204
+ filed: this.evaluationId !== void 0,
4205
+ ...this.evaluationId ? { evaluationId: this.evaluationId } : {}
4206
+ };
4207
+ }
4208
+ gradeError() {
4209
+ return this.grade ? taskGradeError(this.grade) : null;
4210
+ }
4211
+ ref(alias) {
4212
+ const { slug, ref } = this.live.worldRef(alias);
4213
+ const session = this.live.sessions[alias];
4214
+ return { slug, ref, versionId: session.versionId, sessionId: session.sessionId };
4215
+ }
4216
+ metadata() {
4217
+ const live = this.live;
4218
+ return {
4219
+ source: "worlds-task-run",
4220
+ task: { id: live.taskId, version: live.taskVersion, name: live.name, hash: live.hash },
4221
+ anchor: live.aliases[0],
4222
+ worlds: Object.fromEntries(live.aliases.map((alias) => [alias, this.ref(alias)])),
4223
+ runId: this.runId
4224
+ };
4225
+ }
4226
+ sample() {
4227
+ const live = this.live;
4228
+ const reward = this.grade?.reward ?? null;
4229
+ const gradeError = this.gradeError();
4230
+ const sample = {
4231
+ exampleId: live.name ?? live.taskId ?? this.runId,
4232
+ task: live.name ?? live.taskId ?? this.runId,
4233
+ rolloutIndex: 0,
4234
+ prompt: live.instruction,
4235
+ ...this.report === null ? {} : { completion: this.report },
4236
+ rewards: this.grade?.rewards ?? {},
4237
+ info: {
4238
+ sessionId: this.runId,
4239
+ worlds: Object.fromEntries(
4240
+ live.aliases.map((alias) => [
4241
+ alias,
4242
+ { ...this.ref(alias), reward: this.grade?.worlds[alias]?.reward ?? null }
4243
+ ])
4244
+ ),
4245
+ ungraded: this.grade?.ungraded ?? live.aliases,
4246
+ verifierVersion: live.taskVersion,
4247
+ traced: this.traced,
4248
+ runDir: this.runDir,
4249
+ ...this.agentError ? { agentError: this.agentError } : {},
4250
+ ...this.runError ? { runError: this.runError } : {},
4251
+ ...gradeError ? { verifyError: gradeError } : {}
4252
+ }
4253
+ };
4254
+ if (typeof reward === "number")
4255
+ Object.assign(sample, { reward, score: reward, correct: reward >= 1 });
4256
+ return sample;
4257
+ }
4258
+ finalized() {
4259
+ const reward = this.grade?.reward ?? null;
4260
+ const failure = this.failure();
4261
+ return {
4262
+ status: failure ? "FAILED" : "COMPLETED",
4263
+ metrics: {
4264
+ ...typeof reward === "number" ? { avg_reward: reward } : {},
4265
+ reward,
4266
+ ...Object.fromEntries(
4267
+ Object.entries(this.grade?.rewards ?? {}).map(([key, value]) => [`rewards.${key}`, value])
4268
+ ),
4269
+ ...failure ? { error: failure } : {}
4270
+ }
4271
+ };
4272
+ }
3405
4273
  };
3406
4274
  }
3407
4275
  });
@@ -3414,7 +4282,7 @@ function getEnv(key) {
3414
4282
  function isGatewayConfigured() {
3415
4283
  return !!(getEnv(GATEWAY_ENV.HOST) && getEnv(GATEWAY_ENV.PUBLIC_KEY) && getEnv(GATEWAY_ENV.SECRET_KEY));
3416
4284
  }
3417
- var GATEWAY_ENV, OPENTELEMETRY_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
4285
+ var GATEWAY_ENV, OTLP_TRACES_PATH, OPENTELEMETRY_PEERS, PINNED_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
3418
4286
  var init_prerequisites = __esm({
3419
4287
  "src/tracing/prerequisites.ts"() {
3420
4288
  "use strict";
@@ -3422,21 +4290,25 @@ var init_prerequisites = __esm({
3422
4290
  HOST: "GATEWAY_HOST",
3423
4291
  PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
3424
4292
  SECRET_KEY: "GATEWAY_SECRET_KEY",
4293
+ API_KEY: "GATEWAY_API_KEY",
3425
4294
  ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
3426
4295
  };
3427
- OPENTELEMETRY_PEERS = [
3428
- "@opentelemetry/api",
3429
- "@opentelemetry/exporter-trace-otlp-http",
3430
- "@opentelemetry/resources",
3431
- "@opentelemetry/sdk-trace-base",
3432
- "@opentelemetry/sdk-trace-node"
3433
- ];
4296
+ OTLP_TRACES_PATH = "/api/public/otel/v1/traces";
4297
+ OPENTELEMETRY_PEERS = {
4298
+ "@opentelemetry/api": "1.9.0",
4299
+ "@opentelemetry/exporter-trace-otlp-http": "0.222.0",
4300
+ "@opentelemetry/resources": "2.11.0",
4301
+ "@opentelemetry/sdk-trace-base": "2.11.0",
4302
+ "@opentelemetry/sdk-trace-node": "2.11.0"
4303
+ };
4304
+ PINNED_PEERS = Object.entries(OPENTELEMETRY_PEERS).map(([name, version]) => `${name}@${version}`).join(" ");
3434
4305
  MODULE_NOT_FOUND = /* @__PURE__ */ new Set(["MODULE_NOT_FOUND", "ERR_MODULE_NOT_FOUND"]);
3435
4306
  OpenTelemetryMissing = class _OpenTelemetryMissing extends Error {
3436
4307
  constructor() {
3437
4308
  super(
3438
- `OpenTelemetry packages not found. Install them with:
3439
- npm install ${OPENTELEMETRY_PEERS.join(" ")}`
4309
+ `OpenTelemetry packages not found. Install them next to @withgateway/sdk, at the versions it is tested with:
4310
+ npm install ${PINNED_PEERS}
4311
+ npm install -g ${PINNED_PEERS} (the gateway CLI, when it was installed with npm install -g)`
3440
4312
  );
3441
4313
  this.name = "OpenTelemetryMissing";
3442
4314
  }
@@ -3611,10 +4483,10 @@ var init_users = __esm({
3611
4483
  totalItems: Number(meta.totalItems ?? rows.length)
3612
4484
  };
3613
4485
  }
3614
- async requestJson(method, path12, payload) {
4486
+ async requestJson(method, path13, payload) {
3615
4487
  let response;
3616
4488
  try {
3617
- response = await fetch(`${this.host}${path12}`, {
4489
+ response = await fetch(`${this.host}${path13}`, {
3618
4490
  method,
3619
4491
  headers: {
3620
4492
  Authorization: this.authHeader(),
@@ -3625,17 +4497,17 @@ var init_users = __esm({
3625
4497
  });
3626
4498
  } catch (error) {
3627
4499
  throw new UserProfileError(
3628
- `${method} ${path12} failed: ${error instanceof Error ? error.message : String(error)}`,
4500
+ `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
3629
4501
  { cause: error }
3630
4502
  );
3631
4503
  }
3632
4504
  const text2 = await response.text();
3633
4505
  if (!response.ok) {
3634
4506
  throw new UserProfileError(
3635
- `${method} ${path12} failed (${response.status}): ${text2.slice(0, 500)}`
4507
+ `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
3636
4508
  );
3637
4509
  }
3638
- this.log("%s %s -> %s", method, path12, response.status);
4510
+ this.log("%s %s -> %s", method, path13, response.status);
3639
4511
  return safeJson(text2);
3640
4512
  }
3641
4513
  authHeader() {
@@ -4080,10 +4952,10 @@ var init_scores = __esm({
4080
4952
  metadata: signalMetadata
4081
4953
  });
4082
4954
  }
4083
- async request(method, path12, payload) {
4955
+ async request(method, path13, payload) {
4084
4956
  let response;
4085
4957
  try {
4086
- response = await fetch(`${this.host}${path12}`, {
4958
+ response = await fetch(`${this.host}${path13}`, {
4087
4959
  method,
4088
4960
  headers: {
4089
4961
  Authorization: this.authHeader(),
@@ -4094,17 +4966,17 @@ var init_scores = __esm({
4094
4966
  });
4095
4967
  } catch (error) {
4096
4968
  throw new ScoreError(
4097
- `${method} ${path12} failed: ${error instanceof Error ? error.message : String(error)}`,
4969
+ `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
4098
4970
  { cause: error }
4099
4971
  );
4100
4972
  }
4101
4973
  const text2 = await response.text();
4102
4974
  if (!response.ok) {
4103
4975
  throw new ScoreError(
4104
- `${method} ${path12} failed (${response.status}): ${text2.slice(0, 500)}`
4976
+ `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
4105
4977
  );
4106
4978
  }
4107
- this.log("%s %s -> %s", method, path12, response.status);
4979
+ this.log("%s %s -> %s", method, path13, response.status);
4108
4980
  }
4109
4981
  authHeader() {
4110
4982
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -4132,7 +5004,7 @@ var require_package = __commonJS({
4132
5004
  "package.json"(exports, module) {
4133
5005
  module.exports = {
4134
5006
  name: "@withgateway/sdk",
4135
- version: "0.3.1",
5007
+ version: "0.4.0",
4136
5008
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
4137
5009
  main: "./dist/index.js",
4138
5010
  module: "./dist/index.mjs",
@@ -4553,7 +5425,7 @@ function buildProvider(config, logFn) {
4553
5425
  const host = config.host ?? getEnv(GATEWAY_ENV.HOST);
4554
5426
  const publicKey = config.publicKey ?? getEnv(GATEWAY_ENV.PUBLIC_KEY);
4555
5427
  const secretKey = config.secretKey ?? getEnv(GATEWAY_ENV.SECRET_KEY);
4556
- const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${DEFAULT_OTLP_PATH}` : void 0);
5428
+ const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${OTLP_TRACES_PATH}` : void 0);
4557
5429
  const missing = [];
4558
5430
  if (!host) missing.push("host (or GATEWAY_HOST env var)");
4559
5431
  if (!publicKey) missing.push("publicKey (or GATEWAY_PUBLIC_KEY env var)");
@@ -4796,7 +5668,7 @@ async function shutdown() {
4796
5668
  function isInitialized() {
4797
5669
  return state.initialized;
4798
5670
  }
4799
- var DEFAULT_OTLP_PATH, state;
5671
+ var state;
4800
5672
  var init_init = __esm({
4801
5673
  "src/tracing/init.ts"() {
4802
5674
  "use strict";
@@ -4807,7 +5679,6 @@ var init_init = __esm({
4807
5679
  init_build();
4808
5680
  init_prerequisites();
4809
5681
  init_prerequisites();
4810
- DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
4811
5682
  state = {
4812
5683
  initialized: false,
4813
5684
  provider: null
@@ -4816,127 +5687,96 @@ var init_init = __esm({
4816
5687
  });
4817
5688
 
4818
5689
  // src/worlds/taskTracing.ts
4819
- var TaskTracing;
5690
+ var HOST_WITH_AUTHORITY, TRACING_HELD_ELSEWHERE, TaskTracing;
4820
5691
  var init_taskTracing = __esm({
4821
5692
  "src/worlds/taskTracing.ts"() {
4822
5693
  "use strict";
4823
5694
  init_prerequisites();
5695
+ init_platformEndpoint();
5696
+ HOST_WITH_AUTHORITY = /^[a-z][a-z0-9+.-]*:\/\/[^/\\?#@\s]+(\/|$)/i;
5697
+ 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.";
4824
5698
  TaskTracing = class _TaskTracing {
4825
5699
  constructor(load = () => Promise.resolve().then(() => (init_init(), init_exports))) {
4826
5700
  this.load = load;
4827
5701
  }
4828
5702
  load;
4829
5703
  static warned = false;
4830
- async start(requested) {
4831
- if (!requested || !isGatewayConfigured()) return false;
5704
+ static target = null;
5705
+ async start(requested, options) {
5706
+ if (!requested) return false;
5707
+ const endpoint = platformEndpoint(options);
4832
5708
  try {
4833
- (await this.load()).init();
5709
+ await this.initOnce(_TaskTracing.config(endpoint));
4834
5710
  return true;
4835
5711
  } catch (error) {
4836
- _TaskTracing.warnOnce(error);
5712
+ _TaskTracing.warnOnce(_TaskTracing.reason(error, endpoint));
4837
5713
  return false;
4838
5714
  }
4839
5715
  }
4840
- static warnOnce(error) {
5716
+ // init() keeps the first provider for the whole process and ignores later calls, so a later run is
5717
+ // traced only when it exports to the same place with the same keys.
5718
+ async initOnce(config) {
5719
+ const tracing = await this.load();
5720
+ const target = JSON.stringify([config.endpoint, config.publicKey, config.secretKey]);
5721
+ if (!tracing.isInitialized()) {
5722
+ tracing.init(config);
5723
+ _TaskTracing.target = target;
5724
+ return;
5725
+ }
5726
+ if (_TaskTracing.target !== target) throw new Error(TRACING_HELD_ELSEWHERE);
5727
+ }
5728
+ static config(endpoint) {
5729
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
5730
+ return { ...endpoint, endpoint: _TaskTracing.destination(endpoint) };
5731
+ }
5732
+ // B1: a saved login's keys go to their own host only. The redirect is compared as text, never parsed:
5733
+ // a fixed "<host>/" prefix leaves no URL parser room to read another host into it.
5734
+ static destination(endpoint) {
5735
+ const redirect = getEnv(GATEWAY_ENV.ENDPOINT);
5736
+ if (!redirect) return `${endpoint.host}${OTLP_TRACES_PATH}`;
5737
+ if (_TaskTracing.underHost(redirect, endpoint.host) || _TaskTracing.environmentKeys(endpoint)) return redirect;
5738
+ throw new Error(
5739
+ `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.`
5740
+ );
5741
+ }
5742
+ static underHost(redirect, host) {
5743
+ return HOST_WITH_AUTHORITY.test(host) && redirect.startsWith(`${host}/`);
5744
+ }
5745
+ static environmentKeys(endpoint) {
5746
+ const secretKey = getEnv(GATEWAY_ENV.SECRET_KEY) ?? getEnv(GATEWAY_ENV.API_KEY);
5747
+ return endpoint.publicKey === getEnv(GATEWAY_ENV.PUBLIC_KEY) && endpoint.secretKey === secretKey;
5748
+ }
5749
+ static warnOnce(reason) {
4841
5750
  if (_TaskTracing.warned) return;
4842
5751
  _TaskTracing.warned = true;
4843
5752
  console.warn(
4844
- `[gatewaysdk.tracing] Task runs in this process are not traced. ${_TaskTracing.reason(error)}
5753
+ `[gatewaysdk.tracing] Task runs in this process are not traced. ${reason}
4845
5754
  Pass --no-trace (trace: false) to run untraced without this warning.`
4846
5755
  );
4847
5756
  }
4848
- static reason(error) {
4849
- if (OpenTelemetryMissing.explains(error))
4850
- return new OpenTelemetryMissing().message;
4851
- return error instanceof Error ? error.message : String(error);
5757
+ static reason(error, endpoint) {
5758
+ if (OpenTelemetryMissing.explains(error)) return new OpenTelemetryMissing().message;
5759
+ const message = error instanceof Error ? error.message : String(error);
5760
+ const keys = endpoint ? [endpoint.publicKey, endpoint.secretKey] : [];
5761
+ return keys.reduce((text2, key) => text2.split(key).join("[redacted]"), message);
4852
5762
  }
4853
5763
  };
4854
5764
  }
4855
- });
4856
-
4857
- // src/worlds/taskRun.ts
4858
- import { randomBytes } from "crypto";
4859
- import * as fs from "fs";
4860
- import * as path from "path";
4861
- function worldRef(live, alias) {
4862
- const { slug, ref } = live.worldRef(alias);
4863
- const session = live.sessions[alias];
4864
- return {
4865
- slug,
4866
- ref,
4867
- versionId: session.versionId,
4868
- sessionId: session.sessionId
4869
- };
4870
- }
4871
- async function fileTaskRun(live, runId, model, report, grade, agentError, traced, runDir, options, setRunId) {
4872
- const aliases = live.aliases;
4873
- const anchor = aliases[0];
4874
- const worldsMeta = Object.fromEntries(
4875
- aliases.map((alias) => [alias, worldRef(live, alias)])
4876
- );
4877
- const evals = new GatewayEvalsClient(options);
4878
- const evaluationId = await evals.createEvaluation(
4879
- worldRef(live, anchor).versionId,
4880
- {
4881
- model,
4882
- rolloutsPerExample: 1,
4883
- ...setRunId ? { runId: setRunId } : {},
4884
- metadata: {
4885
- source: "worlds-task-run",
4886
- task: {
4887
- id: live.taskId,
4888
- version: live.taskVersion,
4889
- name: live.name,
4890
- hash: live.hash
4891
- },
4892
- anchor,
4893
- worlds: worldsMeta,
4894
- runId
4895
- }
4896
- }
4897
- );
4898
- const reward = grade.reward;
4899
- const sample = {
4900
- exampleId: live.name ?? live.taskId ?? runId,
4901
- task: live.name ?? live.taskId ?? runId,
4902
- rolloutIndex: 0,
4903
- prompt: live.instruction,
4904
- ...report === null ? {} : { completion: report },
4905
- rewards: grade.rewards,
4906
- info: {
4907
- sessionId: runId,
4908
- worlds: Object.fromEntries(
4909
- aliases.map((alias) => [
4910
- alias,
4911
- {
4912
- ...worldRef(live, alias),
4913
- reward: grade.worlds[alias]?.reward ?? null
4914
- }
4915
- ])
4916
- ),
4917
- ungraded: grade.ungraded,
4918
- traced,
4919
- runDir,
4920
- ...agentError ? { agentError } : {}
4921
- }
4922
- };
4923
- if (typeof reward === "number") {
4924
- sample.reward = reward;
4925
- sample.score = reward;
4926
- sample.correct = reward >= 1;
4927
- }
4928
- await evals.pushSamples(evaluationId, [sample]);
4929
- await evals.finalizeEvaluation(evaluationId, {
4930
- status: agentError ? "FAILED" : "COMPLETED",
4931
- metrics: {
4932
- ...typeof reward === "number" ? { avg_reward: reward } : {},
4933
- reward,
4934
- ...Object.fromEntries(
4935
- Object.entries(grade.rewards).map(([k, v]) => [`rewards.${k}`, v])
4936
- )
4937
- }
4938
- });
4939
- return evaluationId;
5765
+ });
5766
+
5767
+ // src/worlds/taskRun.ts
5768
+ import { randomBytes } from "crypto";
5769
+ import * as path2 from "path";
5770
+ function taskRunFailure(summary) {
5771
+ if (summary.agentError !== null) return `agent raised: ${summary.agentError}`;
5772
+ if (summary.runError !== null) return `the run stopped: ${summary.runError}`;
5773
+ if (summary.gradeError !== null)
5774
+ return `the task could not be graded: ${summary.gradeError}`;
5775
+ if (summary.fileError !== null)
5776
+ return `the run could not be filed: ${summary.fileError}`;
5777
+ if (summary.filed && summary.evaluationStatus !== "COMPLETED")
5778
+ return `the platform settled evaluation ${summary.evaluationId} as ${summary.evaluationStatus}`;
5779
+ return null;
4940
5780
  }
4941
5781
  function reportFromResult(result) {
4942
5782
  if (result === void 0 || result === null)
@@ -4954,110 +5794,47 @@ function reportFromResult(result) {
4954
5794
  );
4955
5795
  return { report, extra };
4956
5796
  }
5797
+ async function refuseUnreadTools(live) {
5798
+ try {
5799
+ for (const alias of live.aliases)
5800
+ WorldToolsUnread.refuse(`${live.worldRef(alias).slug} (alias ${alias})`, live.session(alias).deprecations);
5801
+ } catch (error) {
5802
+ await live.close({ keepWarm: false }).catch(() => void 0);
5803
+ throw error;
5804
+ }
5805
+ }
4957
5806
  async function runTask(ref, agent, opts) {
4958
5807
  const runId = `wtr-${randomBytes(6).toString("hex")}`;
4959
- const outDir = opts.outDir ?? "runs";
4960
- const runDir = path.join(outDir, runId);
4961
- fs.mkdirSync(runDir, { recursive: true });
4962
5808
  const options = opts.options ?? {};
4963
- const live = await openTask(ref, {
4964
- surfaces: opts.surfaces,
4965
- reuseWarm: opts.reuseWarm,
4966
- browserTier: opts.browserTier,
4967
- options
4968
- });
4969
- const started = Date.now();
4970
- try {
4971
- await live.ready();
4972
- const traced = await new TaskTracing().start(opts.trace !== false);
4973
- const taskRun = new TaskRun(
4974
- runId,
4975
- live.instruction ?? "",
4976
- opts.model,
4977
- live.sessions,
4978
- await live.manifest()
4979
- );
4980
- let agentError = null;
4981
- let report = null;
4982
- let extra = {};
4983
- try {
4984
- const invoke = () => agent(taskRun);
4985
- const result = traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(runId, invoke) : await invoke();
4986
- ({ report, extra } = reportFromResult(result));
4987
- } catch (error) {
4988
- const message = (error instanceof Error ? error.message : String(error)) || "Unknown error";
4989
- agentError = message.slice(0, 2e3);
4990
- }
4991
- if (traced)
4992
- await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing) => tracing.flush()).catch(() => void 0);
4993
- const grade = report !== null ? await live.grade({ report }) : await live.grade();
4994
- fs.writeFileSync(
4995
- path.join(runDir, "manifest.json"),
4996
- JSON.stringify(taskRun.manifest, null, 2)
4997
- );
4998
- fs.writeFileSync(
4999
- path.join(runDir, "grades.json"),
5000
- JSON.stringify(grade, null, 2)
5001
- );
5002
- if (report) fs.writeFileSync(path.join(runDir, "report.md"), report);
5003
- const summary = {
5004
- runId,
5005
- taskId: live.taskId,
5006
- taskVersion: live.taskVersion,
5007
- name: live.name,
5008
- instruction: taskRun.instruction,
5009
- model: opts.model,
5010
- aliases: live.aliases,
5011
- reward: grade.reward,
5012
- rewards: grade.rewards,
5013
- ungraded: grade.ungraded,
5014
- agentError,
5015
- report,
5016
- traced,
5017
- runDir,
5018
- durationSeconds: Math.round((Date.now() - started) / 10) / 100,
5019
- notes: taskRun.notes,
5020
- filed: false,
5021
- ...extra
5022
- };
5023
- if (opts.file !== false) {
5024
- summary.evaluationId = await fileTaskRun(
5025
- live,
5026
- runId,
5027
- opts.model,
5028
- report,
5029
- grade,
5030
- agentError,
5031
- traced,
5032
- runDir,
5033
- options,
5034
- opts.setRunId
5035
- );
5036
- summary.filed = true;
5037
- }
5038
- fs.writeFileSync(
5039
- path.join(runDir, "run.json"),
5040
- JSON.stringify(summary, null, 2)
5041
- );
5042
- return summary;
5043
- } finally {
5044
- if (!opts.keepUp) {
5045
- try {
5046
- await live.close({ keepWarm: false });
5047
- } catch {
5048
- }
5049
- }
5050
- }
5809
+ const opening = new TaskOpening(
5810
+ openTask(ref, {
5811
+ surfaces: opts.surfaces,
5812
+ reuseWarm: opts.reuseWarm,
5813
+ browserTier: opts.browserTier,
5814
+ options
5815
+ }).then(async (live) => {
5816
+ await refuseUnreadTools(live);
5817
+ return new LiveTaskRun(runId, live, opts, options);
5818
+ })
5819
+ );
5820
+ return new RunSignals().during(
5821
+ (signal, urgent) => opening.interrupt(signal, urgent),
5822
+ () => opening.complete(agent)
5823
+ );
5051
5824
  }
5052
- var TaskRun, describeValue, RESULT_CONTRACT;
5825
+ var TaskRun, FILING_UNFINISHED, describeValue, RESULT_CONTRACT, LiveTaskRun, TaskOpening;
5053
5826
  var init_taskRun = __esm({
5054
5827
  "src/worlds/taskRun.ts"() {
5055
5828
  "use strict";
5056
5829
  init_browser();
5057
5830
  init_evals();
5831
+ init_runSignals();
5058
5832
  init_session2();
5059
5833
  init_taskSessions();
5834
+ init_taskRunDir();
5835
+ init_taskRunOutcome();
5060
5836
  init_taskTracing();
5837
+ init_worldNotice();
5061
5838
  TaskRun = class {
5062
5839
  constructor(runId, instruction, model, sessions, manifest) {
5063
5840
  this.runId = runId;
@@ -5120,8 +5897,127 @@ var init_taskRun = __esm({
5120
5897
  Object.assign(this.notes, notes);
5121
5898
  }
5122
5899
  };
5900
+ FILING_UNFINISHED = "the filing did not finish";
5123
5901
  describeValue = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
5124
5902
  RESULT_CONTRACT = "return a string report, an object {report?: string, ...} or nothing";
5903
+ LiveTaskRun = class {
5904
+ constructor(runId, live, opts, options) {
5905
+ this.live = live;
5906
+ this.opts = opts;
5907
+ this.options = options;
5908
+ this.files = new TaskRunDir(path2.join(opts.outDir ?? "runs", runId));
5909
+ this.outcome = new TaskRunOutcome(runId, live, opts.model, this.files.path);
5910
+ }
5911
+ live;
5912
+ opts;
5913
+ options;
5914
+ files;
5915
+ outcome;
5916
+ played = false;
5917
+ interrupted = false;
5918
+ filing = null;
5919
+ ending = null;
5920
+ async complete(agent) {
5921
+ try {
5922
+ await this.play(agent).catch((error) => this.outcome.stopped(error));
5923
+ this.played = true;
5924
+ await this.file();
5925
+ return this.record();
5926
+ } finally {
5927
+ await this.end();
5928
+ }
5929
+ }
5930
+ async interrupt(signal, urgent = false) {
5931
+ const sessions = this.opts.keepUp ? "leaving its sessions up" : "closing its sessions";
5932
+ const how = urgent ? " now, without waiting" : "";
5933
+ process.stderr.write(`${this.outcome.runId} interrupted by ${signal}; ${sessions}${how}
5934
+ `);
5935
+ if (!this.played) this.outcome.stopped(`interrupted by ${signal}`);
5936
+ this.interrupted = true;
5937
+ try {
5938
+ if (!urgent) await this.filing;
5939
+ this.record();
5940
+ } catch (error) {
5941
+ const why = error instanceof Error ? error.message : String(error);
5942
+ process.stderr.write(`${this.outcome.runId}: run.json not written: ${why}
5943
+ `);
5944
+ } finally {
5945
+ await this.end();
5946
+ }
5947
+ }
5948
+ summary() {
5949
+ return this.outcome.summary();
5950
+ }
5951
+ async play(agent) {
5952
+ await this.live.ready();
5953
+ this.outcome.traced = await new TaskTracing().start(this.opts.trace !== false, this.options);
5954
+ const run = new TaskRun(
5955
+ this.outcome.runId,
5956
+ this.live.instruction ?? "",
5957
+ this.opts.model,
5958
+ this.live.sessions,
5959
+ await this.live.manifest()
5960
+ );
5961
+ this.outcome.notes = run.notes;
5962
+ this.files.writeManifest(run.manifest);
5963
+ await this.callAgent(agent, run);
5964
+ const report = this.outcome.report;
5965
+ this.outcome.grade = await this.live.grade(report !== null ? { report } : {});
5966
+ this.files.writeJson("grades.json", this.outcome.grade);
5967
+ if (report) this.files.write("report.md", report);
5968
+ }
5969
+ async callAgent(agent, run) {
5970
+ try {
5971
+ const invoke = () => agent(run);
5972
+ const result = this.outcome.traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(run.runId, invoke) : await invoke();
5973
+ ({ report: this.outcome.report, extra: this.outcome.extra } = reportFromResult(result));
5974
+ } catch (error) {
5975
+ this.outcome.agentRaised(error);
5976
+ }
5977
+ if (this.outcome.traced)
5978
+ await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing) => tracing.flush()).catch(() => void 0);
5979
+ }
5980
+ async file() {
5981
+ if (this.opts.file === false || this.interrupted) return;
5982
+ this.files.writeJson("run.json", { ...this.outcome.summary(), fileError: FILING_UNFINISHED });
5983
+ this.filing = this.outcome.file(new GatewayEvalsClient(this.options), this.opts.setRunId);
5984
+ await this.filing;
5985
+ }
5986
+ record() {
5987
+ const summary = this.outcome.summary();
5988
+ this.files.writeJson("run.json", summary);
5989
+ return summary;
5990
+ }
5991
+ end() {
5992
+ this.ending ??= this.opts.keepUp ? Promise.resolve() : this.live.close({ keepWarm: false }).then(
5993
+ () => void 0,
5994
+ () => void 0
5995
+ );
5996
+ return this.ending;
5997
+ }
5998
+ };
5999
+ TaskOpening = class {
6000
+ interrupting = null;
6001
+ run = null;
6002
+ opened;
6003
+ constructor(opening) {
6004
+ this.opened = opening.then((run) => this.run = run);
6005
+ }
6006
+ async complete(agent) {
6007
+ const run = await this.opened;
6008
+ if (!this.interrupting) return run.complete(agent);
6009
+ await this.interrupting;
6010
+ return run.summary();
6011
+ }
6012
+ interrupt(signal, urgent = false) {
6013
+ if (urgent) return this.run?.interrupt(signal, true) ?? Promise.resolve();
6014
+ this.interrupting ??= this.opened.then(
6015
+ (run) => run.interrupt(signal),
6016
+ () => void 0
6017
+ );
6018
+ return this.interrupting;
6019
+ }
6020
+ };
5125
6021
  }
5126
6022
  });
5127
6023
 
@@ -5201,106 +6097,182 @@ async function runSet(datasetId, agent, opts) {
5201
6097
  source: opts.source ?? "sdk",
5202
6098
  ...git && Object.keys(git).length ? { git } : {}
5203
6099
  });
5204
- const close = (status) => apiRequest(
5205
- options,
5206
- "POST",
5207
- `${SET_RUNS}/${encodeURIComponent(opened.id)}/close`,
5208
- { status }
5209
- );
5210
- const onInterrupt = () => {
5211
- void close("failed").finally(() => process.exit(130));
5212
- };
5213
- process.once("SIGINT", onInterrupt);
5214
- const planned = await runPlannedTasks(opened, agent, opts, options).finally(
5215
- () => process.off("SIGINT", onInterrupt)
5216
- );
5217
- const { tasks, failed } = planned;
5218
- const requested = failed ? "failed" : "completed";
5219
- const setRun = await close(requested);
5220
- return {
5221
- setRun,
5222
- tasks,
5223
- failed: failed || setRun.status !== "completed",
5224
- closedAsRequested: setRun.status === requested
5225
- };
5226
- }
5227
- async function runPlannedTasks(opened, agent, opts, options) {
5228
- const tasks = [];
5229
- let failed = false;
5230
- for (const task of opened.tasks) {
5231
- try {
5232
- const summary = await runTask(task.worldTaskId, agent, {
5233
- model: opts.model,
5234
- surfaces: opts.surfaces,
5235
- browserTier: opts.browserTier,
5236
- outDir: opts.outDir,
5237
- trace: opts.trace,
5238
- setRunId: opened.id,
5239
- options
5240
- });
5241
- tasks.push(summary);
5242
- failed ||= summary.agentError !== null;
5243
- opts.onTask?.(summary);
5244
- } catch (error) {
5245
- failed = true;
5246
- const message = error instanceof Error ? error.message : String(error);
5247
- process.stderr.write(`task ${task.name} could not run: ${message}
5248
- `);
5249
- }
5250
- }
5251
- return { tasks, failed };
6100
+ return new SetRunner(opened, agent, opts, options).run();
5252
6101
  }
5253
- var SET_RUNS;
6102
+ var SET_RUNS, SetRunner;
5254
6103
  var init_setRun = __esm({
5255
6104
  "src/worlds/setRun.ts"() {
5256
6105
  "use strict";
5257
6106
  init_worlds();
5258
6107
  init_checkoutProvenance();
6108
+ init_runSignals();
5259
6109
  init_taskRun();
5260
6110
  SET_RUNS = "/api/public/set-runs";
6111
+ SetRunner = class {
6112
+ constructor(opened, agent, opts, options) {
6113
+ this.opened = opened;
6114
+ this.agent = agent;
6115
+ this.opts = opts;
6116
+ this.options = options;
6117
+ }
6118
+ opened;
6119
+ agent;
6120
+ opts;
6121
+ options;
6122
+ tasks = [];
6123
+ failures = [];
6124
+ failed = false;
6125
+ current = null;
6126
+ interrupted = null;
6127
+ signals = new RunSignals();
6128
+ async run() {
6129
+ await this.signals.during(
6130
+ (signal2) => this.interrupt(signal2).then(() => void 0),
6131
+ () => this.runTasks()
6132
+ );
6133
+ const signal = this.signals.signal;
6134
+ if (signal !== null) return this.result(await this.interrupt(signal), "failed");
6135
+ const requested = this.failed ? "failed" : "completed";
6136
+ return this.result(await this.close(requested, this.failures), requested);
6137
+ }
6138
+ interrupt(signal) {
6139
+ const stopped = this.current ? [{ worldTaskId: this.current, reason: `interrupted by ${signal}` }] : [];
6140
+ this.interrupted ??= this.close("failed", [...this.failures, ...stopped]);
6141
+ return this.interrupted;
6142
+ }
6143
+ async runTasks() {
6144
+ for (const task of this.opened.tasks) {
6145
+ if (this.signals.signal !== null) return;
6146
+ this.current = task.worldTaskId;
6147
+ await this.runOne(task);
6148
+ }
6149
+ this.current = null;
6150
+ }
6151
+ async runOne(task) {
6152
+ try {
6153
+ const summary = await runTask(task.worldTaskId, this.agent, {
6154
+ model: this.opts.model,
6155
+ surfaces: this.opts.surfaces,
6156
+ browserTier: this.opts.browserTier,
6157
+ outDir: this.opts.outDir,
6158
+ trace: this.opts.trace,
6159
+ setRunId: this.opened.id,
6160
+ options: this.options
6161
+ });
6162
+ this.tasks.push(summary);
6163
+ this.failed ||= taskRunFailure(summary) !== null;
6164
+ if (summary.fileError !== null)
6165
+ this.failures.push({ worldTaskId: task.worldTaskId, reason: summary.fileError });
6166
+ this.opts.onTask?.(summary);
6167
+ } catch (error) {
6168
+ this.failed = true;
6169
+ const message = error instanceof Error ? error.message : String(error);
6170
+ process.stderr.write(`task ${task.name} could not run: ${message}
6171
+ `);
6172
+ this.failures.push({
6173
+ worldTaskId: task.worldTaskId,
6174
+ reason: message || (error instanceof Error ? error.name : "Error")
6175
+ });
6176
+ }
6177
+ }
6178
+ close(status, failures = []) {
6179
+ return apiRequest(
6180
+ this.options,
6181
+ "POST",
6182
+ `${SET_RUNS}/${encodeURIComponent(this.opened.id)}/close`,
6183
+ { status, ...failures.length ? { failures } : {} }
6184
+ );
6185
+ }
6186
+ result(setRun, requested) {
6187
+ return {
6188
+ setRun,
6189
+ tasks: this.tasks,
6190
+ failed: this.failed || setRun.status !== "completed",
6191
+ closedAsRequested: setRun.status === requested
6192
+ };
6193
+ }
6194
+ };
5261
6195
  }
5262
6196
  });
5263
6197
 
5264
6198
  // src/worlds/requestLog.ts
5265
- var WorldRequestLog;
6199
+ function worldRequestsUnrecorded(counts) {
6200
+ 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)));
6201
+ return parts.length > 0 ? `Not recorded: ${parts.join(", ")}` : null;
6202
+ }
6203
+ function counted(value) {
6204
+ return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : 0;
6205
+ }
6206
+ var UNRECORDED, WorldRequestLog;
5266
6207
  var init_requestLog = __esm({
5267
6208
  "src/worlds/requestLog.ts"() {
5268
6209
  "use strict";
6210
+ init_requestLedger();
6211
+ UNRECORDED = [
6212
+ ["lost", "{n} request", "{n} requests"],
6213
+ ["shed", "{n} request's headers and bodies", "{n} requests' headers and bodies"],
6214
+ ["hostsLost", "the requests {n} lost host held", "the requests {n} lost hosts held"]
6215
+ ];
5269
6216
  WorldRequestLog = class _WorldRequestLog {
5270
- constructor(requests) {
6217
+ constructor(requests, notRecorded = null, unrecorded = null) {
5271
6218
  this.requests = requests;
6219
+ this.notRecorded = notRecorded;
6220
+ this.unrecorded = unrecorded;
5272
6221
  }
5273
6222
  requests;
5274
- static async read(session) {
5275
- const state2 = await session.state();
5276
- const calls = state2?.calls;
5277
- if (!Array.isArray(calls)) return null;
5278
- return new _WorldRequestLog(calls.filter((c) => typeof c === "object" && c !== null));
5279
- }
5280
- /** How many older requests the world no longer keeps (its log is bounded): the gap
5281
- * before the first entry it still holds. */
5282
- get dropped() {
5283
- const first = Number(this.requests[0]?.seq) || 1;
5284
- return Math.max(0, first - 1);
5285
- }
5286
- /** Each request in the shape of a ledger line, `kind: "request"`, so one JSONL
5287
- * file holds both. */
6223
+ notRecorded;
6224
+ unrecorded;
6225
+ /** Every entry the ledger holds for the session, oldest first, and the line for what it did not record;
6226
+ * for a session the World Host did not serve, none, and `notRecorded` says why. */
6227
+ static async load(session) {
6228
+ try {
6229
+ return await _WorldRequestLog.pages(session);
6230
+ } catch (error) {
6231
+ const refused = WorldRequestsNotRecorded.from(error);
6232
+ if (!(refused instanceof WorldRequestsNotRecorded)) throw error;
6233
+ return new _WorldRequestLog([], refused.message);
6234
+ }
6235
+ }
6236
+ static async pages(session) {
6237
+ const requests = [];
6238
+ let cursor;
6239
+ for (; ; ) {
6240
+ const page = await session.requests(cursor ? { cursor } : {});
6241
+ requests.push(...page.requests);
6242
+ if (!page.nextCursor || page.requests.length === 0)
6243
+ return new _WorldRequestLog(requests.reverse(), null, worldRequestsUnrecorded(page));
6244
+ cursor = page.nextCursor;
6245
+ }
6246
+ }
6247
+ /** Each entry in the shape of a ledger line, `kind: "request"`, so one JSONL
6248
+ * file holds both: `args` what was asked, `result` what came back under the
6249
+ * entry's `requestId`. */
5288
6250
  asCalls() {
5289
6251
  return this.requests.map((r) => ({
5290
- seq: Number(r.seq) || 0,
6252
+ seq: r.seq,
5291
6253
  kind: "request",
5292
- tool: r.operation ?? null,
6254
+ tool: r.operation,
5293
6255
  args: {
5294
- method: r.method ?? null,
5295
- path: r.path ?? null,
5296
- args: r.args ?? null,
5297
- query: r.query ?? null,
5298
- body: r.body ?? null,
5299
- via: r.via ?? null
6256
+ method: r.method,
6257
+ path: r.path,
6258
+ query: r.query,
6259
+ via: r.via,
6260
+ contentType: r.request.contentType,
6261
+ bodyBytes: r.request.bodyBytes,
6262
+ truncated: r.request.truncated
6263
+ },
6264
+ result: {
6265
+ requestId: r.id,
6266
+ status: r.response.status,
6267
+ rows: r.rows,
6268
+ found: r.found,
6269
+ durationMs: r.durationMs,
6270
+ contentType: r.response.contentType,
6271
+ bodyBytes: r.response.bodyBytes,
6272
+ truncated: r.response.truncated
5300
6273
  },
5301
- result: { status: r.status ?? null, rows: r.rows ?? null, found: r.found ?? null },
5302
6274
  error: null,
5303
- completedAt: r.at ?? null
6275
+ completedAt: r.at
5304
6276
  }));
5305
6277
  }
5306
6278
  };
@@ -5309,7 +6281,7 @@ var init_requestLog = __esm({
5309
6281
 
5310
6282
  // src/worlds/checkoutTarget.ts
5311
6283
  import * as fs2 from "fs";
5312
- import * as path2 from "path";
6284
+ import * as path3 from "path";
5313
6285
  var CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
5314
6286
  var init_checkoutTarget = __esm({
5315
6287
  "src/worlds/checkoutTarget.ts"() {
@@ -5338,17 +6310,17 @@ var init_checkoutTarget = __esm({
5338
6310
  }
5339
6311
  /** The absolute destination; a relative target may not resolve above `cwd`. */
5340
6312
  static confined(target, cwd) {
5341
- const dir = path2.resolve(cwd, target);
5342
- if (path2.isAbsolute(target)) return dir;
5343
- const rel = path2.relative(cwd, dir);
5344
- if (rel === "" || !rel.startsWith("..") && !path2.isAbsolute(rel))
6313
+ const dir = path3.resolve(cwd, target);
6314
+ if (path3.isAbsolute(target)) return dir;
6315
+ const rel = path3.relative(cwd, dir);
6316
+ if (rel === "" || !rel.startsWith("..") && !path3.isAbsolute(rel))
5345
6317
  return dir;
5346
6318
  throw new CheckoutRefusedError(
5347
6319
  `--target ${target} resolves outside the current directory (${cwd}) \u2014 run from that directory, or pass an absolute path`
5348
6320
  );
5349
6321
  }
5350
6322
  static fresh(slug, cwd) {
5351
- const base = path2.join(cwd, slug);
6323
+ const base = path3.join(cwd, slug);
5352
6324
  let dir = base;
5353
6325
  let i = 1;
5354
6326
  while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
@@ -5375,7 +6347,7 @@ var init_checkoutTarget = __esm({
5375
6347
  ].filter(_CheckoutTarget.confinedRel).sort();
5376
6348
  }
5377
6349
  static confinedRel(rel) {
5378
- if (rel === "" || path2.isAbsolute(rel)) return false;
6350
+ if (rel === "" || path3.isAbsolute(rel)) return false;
5379
6351
  return !rel.split(/[\\/]/).includes("..");
5380
6352
  }
5381
6353
  /** Why `dir` cannot be overwritten, or null when it is a clean checkout of `slug`. */
@@ -5393,9 +6365,9 @@ var init_checkoutTarget = __esm({
5393
6365
  /** Remove the replaced checkout's files (and the dirs they leave empty). */
5394
6366
  clear() {
5395
6367
  for (const rel of this.replaces) {
5396
- const abs = path2.join(this.dir, rel);
6368
+ const abs = path3.join(this.dir, rel);
5397
6369
  fs2.rmSync(abs, { force: true });
5398
- this.pruneEmptyParents(path2.dirname(abs));
6370
+ this.pruneEmptyParents(path3.dirname(abs));
5399
6371
  }
5400
6372
  }
5401
6373
  pruneEmptyParents(from) {
@@ -5403,7 +6375,7 @@ var init_checkoutTarget = __esm({
5403
6375
  while (cur !== this.dir && cur.startsWith(this.dir)) {
5404
6376
  if (fs2.existsSync(cur) && fs2.readdirSync(cur).length > 0) return;
5405
6377
  fs2.rmSync(cur, { recursive: true, force: true });
5406
- cur = path2.dirname(cur);
6378
+ cur = path3.dirname(cur);
5407
6379
  }
5408
6380
  }
5409
6381
  };
@@ -5413,7 +6385,7 @@ var init_checkoutTarget = __esm({
5413
6385
  // src/worlds/worldDir.ts
5414
6386
  import { createHash as createHash3 } from "crypto";
5415
6387
  import * as fs3 from "fs";
5416
- import * as path3 from "path";
6388
+ import * as path4 from "path";
5417
6389
  var WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, BENCHMARK_CONFIG, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
5418
6390
  var init_worldDir = __esm({
5419
6391
  "src/worlds/worldDir.ts"() {
@@ -5443,17 +6415,17 @@ var init_worldDir = __esm({
5443
6415
  worldRoot;
5444
6416
  pathsMemo;
5445
6417
  static open(dir) {
5446
- const present = (rel) => fs3.existsSync(path3.join(dir, rel));
6418
+ const present = (rel) => fs3.existsSync(path4.join(dir, rel));
5447
6419
  const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
5448
6420
  return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
5449
6421
  }
5450
6422
  // ── Files ──────────────────────────────────────────────────────────────
5451
6423
  has(rel) {
5452
- return fs3.existsSync(path3.join(this.dir, rel));
6424
+ return fs3.existsSync(path4.join(this.dir, rel));
5453
6425
  }
5454
6426
  text(rel) {
5455
6427
  try {
5456
- return fs3.readFileSync(path3.join(this.dir, rel), "utf-8");
6428
+ return fs3.readFileSync(path4.join(this.dir, rel), "utf-8");
5457
6429
  } catch {
5458
6430
  return null;
5459
6431
  }
@@ -5472,7 +6444,7 @@ var init_worldDir = __esm({
5472
6444
  const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
5473
6445
  const out = [];
5474
6446
  const walk = (rel) => {
5475
- for (const entry of fs3.readdirSync(rel ? path3.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
6447
+ for (const entry of fs3.readdirSync(rel ? path4.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
5476
6448
  const child = rel ? `${rel}/${entry.name}` : entry.name;
5477
6449
  if (entry.isDirectory()) {
5478
6450
  if (!skippedDir(entry.name)) walk(child);
@@ -5493,7 +6465,7 @@ var init_worldDir = __esm({
5493
6465
  get exclusions() {
5494
6466
  const kept = new Set(this.archivePaths);
5495
6467
  const out = [];
5496
- const filesUnder = (rel) => fs3.readdirSync(path3.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
6468
+ const filesUnder = (rel) => fs3.readdirSync(path4.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
5497
6469
  const child = `${rel}/${entry.name}`;
5498
6470
  if (entry.isDirectory()) return filesUnder(child);
5499
6471
  return entry.isFile() ? [child] : [];
@@ -5524,7 +6496,7 @@ var init_worldDir = __esm({
5524
6496
  const hasher = createHash3("sha256");
5525
6497
  for (const rel of this.archivePaths) {
5526
6498
  hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
5527
- hasher.update(fs3.readFileSync(path3.join(this.dir, rel)));
6499
+ hasher.update(fs3.readFileSync(path4.join(this.dir, rel)));
5528
6500
  }
5529
6501
  return hasher.digest("hex");
5530
6502
  }
@@ -5589,7 +6561,7 @@ var init_worldDir = __esm({
5589
6561
 
5590
6562
  // src/worlds/tar.ts
5591
6563
  import * as fs4 from "fs";
5592
- import * as path4 from "path";
6564
+ import * as path5 from "path";
5593
6565
  import { gunzipSync } from "zlib";
5594
6566
  function archiveMode(mode) {
5595
6567
  return (mode & 73) !== 0 ? TAR_EXEC_MODE : TAR_FILE_MODE;
@@ -5675,8 +6647,8 @@ var init_tar = __esm({
5675
6647
  for (const member of this.members()) {
5676
6648
  if (member.typeflag !== "0" && member.typeflag !== "\0") continue;
5677
6649
  if (!_TarArchive.isConfined(member.name)) continue;
5678
- const target = path4.join(dir, member.name);
5679
- fs4.mkdirSync(path4.dirname(target), { recursive: true });
6650
+ const target = path5.join(dir, member.name);
6651
+ fs4.mkdirSync(path5.dirname(target), { recursive: true });
5680
6652
  const mode = archiveMode(member.mode);
5681
6653
  fs4.writeFileSync(target, member.bytes, { mode });
5682
6654
  fs4.chmodSync(target, mode);
@@ -5720,7 +6692,7 @@ __export(hub_exports, {
5720
6692
  import { createHash as createHash4 } from "crypto";
5721
6693
  import { gzipSync } from "zlib";
5722
6694
  import * as fs5 from "fs";
5723
- import * as path5 from "path";
6695
+ import * as path6 from "path";
5724
6696
  function collectArchiveRelPaths(dir) {
5725
6697
  const world = WorldDir.open(dir);
5726
6698
  if (world.hasRootGitignore) {
@@ -5783,7 +6755,7 @@ function tarMember(name, payload, mode) {
5783
6755
  function buildSourceTarball(dir) {
5784
6756
  const parts = [];
5785
6757
  for (const relPath of collectArchiveRelPaths(dir)) {
5786
- const abs = path5.join(dir, relPath);
6758
+ const abs = path6.join(dir, relPath);
5787
6759
  parts.push(
5788
6760
  ...tarMember(
5789
6761
  relPath,
@@ -5797,7 +6769,7 @@ function buildSourceTarball(dir) {
5797
6769
  }
5798
6770
  function readLocalMeta(dir) {
5799
6771
  try {
5800
- const raw = fs5.readFileSync(path5.join(dir, META_RELPATH), "utf-8");
6772
+ const raw = fs5.readFileSync(path6.join(dir, META_RELPATH), "utf-8");
5801
6773
  const parsed = JSON.parse(raw);
5802
6774
  return parsed && typeof parsed === "object" ? parsed : null;
5803
6775
  } catch {
@@ -5820,16 +6792,16 @@ function resolvePushSlug(dir, opts = {}) {
5820
6792
  }
5821
6793
  const first = declared[0];
5822
6794
  if (first) return { ...base, slug: first.slug, source: first.source };
5823
- return { ...base, slug: path5.basename(path5.resolve(dir)), source: "directory name" };
6795
+ return { ...base, slug: path6.basename(path6.resolve(dir)), source: "directory name" };
5824
6796
  }
5825
6797
  function writeLocalMeta(dir, meta) {
5826
- fs5.mkdirSync(path5.join(dir, ".gateway"), { recursive: true });
5827
- fs5.writeFileSync(path5.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
6798
+ fs5.mkdirSync(path6.join(dir, ".gateway"), { recursive: true });
6799
+ fs5.writeFileSync(path6.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
5828
6800
  }
5829
6801
  function snapshotTreeHashes(dir) {
5830
6802
  const out = {};
5831
6803
  for (const relPath of collectArchiveRelPaths(dir)) {
5832
- out[relPath] = createHash4("sha256").update(fs5.readFileSync(path5.join(dir, relPath))).digest("hex");
6804
+ out[relPath] = createHash4("sha256").update(fs5.readFileSync(path6.join(dir, relPath))).digest("hex");
5833
6805
  }
5834
6806
  return out;
5835
6807
  }
@@ -5847,7 +6819,7 @@ function localTreeState(dir) {
5847
6819
  }
5848
6820
  function readPyproject(dir) {
5849
6821
  try {
5850
- const text2 = fs5.readFileSync(path5.join(dir, "pyproject.toml"), "utf-8");
6822
+ const text2 = fs5.readFileSync(path6.join(dir, "pyproject.toml"), "utf-8");
5851
6823
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
5852
6824
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
5853
6825
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -5870,7 +6842,7 @@ function readWorldContractVersion(dir) {
5870
6842
  }
5871
6843
  function buildFileManifest(dir) {
5872
6844
  return collectArchiveRelPaths(dir).map((relPath) => {
5873
- const payload = fs5.readFileSync(path5.join(dir, relPath));
6845
+ const payload = fs5.readFileSync(path6.join(dir, relPath));
5874
6846
  return {
5875
6847
  path: relPath,
5876
6848
  isDirectory: false,
@@ -5916,7 +6888,7 @@ async function push(dir, opts = {}) {
5916
6888
  const pyproject = readPyproject(dir);
5917
6889
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
5918
6890
  if (contractVersion) pyproject.version = contractVersion;
5919
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path5.basename(path5.resolve(dir));
6891
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path6.basename(path6.resolve(dir));
5920
6892
  const metadata = {
5921
6893
  ...opts.name !== void 0 ? { name: opts.name } : {},
5922
6894
  ...opts.description !== void 0 ? { description: opts.description } : {},
@@ -5995,7 +6967,8 @@ function checkoutIdentity(ref, resolved) {
5995
6967
  async function pull(slugRef, target, options = {}, opts = {}) {
5996
6968
  const at = slugRef.indexOf("@");
5997
6969
  const slug = at === -1 ? slugRef : slugRef.slice(0, at);
5998
- const ref = at === -1 ? "latest" : slugRef.slice(at + 1);
6970
+ const asked = at === -1 ? void 0 : slugRef.slice(at + 1);
6971
+ const ref = asked ?? "latest";
5999
6972
  const landing = CheckoutTarget.resolve(slug, target, {
6000
6973
  cwd: process.cwd(),
6001
6974
  force: opts.force ?? false,
@@ -6003,7 +6976,7 @@ async function pull(slugRef, target, options = {}, opts = {}) {
6003
6976
  });
6004
6977
  const details = await apiRequest(options, "POST", "/api/public/benchmark-containers/resolve-version", {
6005
6978
  slug,
6006
- ref
6979
+ ...asked ? { ref: asked } : {}
6007
6980
  });
6008
6981
  if (!details.downloadUrl) {
6009
6982
  throw new Error(`${slug}@${ref} resolved but has no uploaded bundle`);
@@ -6042,7 +7015,7 @@ async function propose(dir, opts) {
6042
7015
  if (state2.clean)
6043
7016
  throw new Error("Nothing to propose \u2014 the tree matches its base.");
6044
7017
  const changes = state2.dirty.map((relPath) => {
6045
- const abs = path5.join(dir, relPath);
7018
+ const abs = path6.join(dir, relPath);
6046
7019
  if (!fs5.existsSync(abs))
6047
7020
  return { path: relPath, action: "delete" };
6048
7021
  return {
@@ -6083,7 +7056,7 @@ var init_hub = __esm({
6083
7056
  init_worldDir();
6084
7057
  init_tar();
6085
7058
  DEFAULT_BRANCH = "main";
6086
- META_RELPATH = path5.join(".gateway", ".env-metadata.json");
7059
+ META_RELPATH = path6.join(".gateway", ".env-metadata.json");
6087
7060
  FILE_MODE = TAR_FILE_MODE;
6088
7061
  MINTED_SUFFIX = /^-\d+$/;
6089
7062
  PushSlugConflict = class extends Error {
@@ -6189,7 +7162,7 @@ async function validateWorld(dir, opts) {
6189
7162
  options,
6190
7163
  "POST",
6191
7164
  "/api/public/benchmark-containers/resolve-version",
6192
- { slug, ref: branch }
7165
+ { slug, ...branch === DEFAULT_BRANCH ? {} : { ref: branch } }
6193
7166
  );
6194
7167
  tipVersionId = tip.versionId ?? null;
6195
7168
  } catch {
@@ -6296,11 +7269,16 @@ var init_validate = __esm({
6296
7269
  });
6297
7270
 
6298
7271
  // src/worlds/sandbox.ts
7272
+ import { createHash as createHash5, randomUUID as randomUUID2 } from "crypto";
6299
7273
  function parseSlugRef(slugRef) {
6300
7274
  const at = slugRef.indexOf("@");
6301
7275
  if (at === -1) return { slug: slugRef };
6302
7276
  return { slug: slugRef.slice(0, at), ref: slugRef.slice(at + 1) };
6303
7277
  }
7278
+ function nameGlob(pattern) {
7279
+ const literal = (part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
7280
+ return new RegExp(`^${pattern.split("*").map(literal).join(".*")}$`);
7281
+ }
6304
7282
  function rebaseConflicts(error) {
6305
7283
  if (!(error instanceof ApiError) || error.status !== 409) return null;
6306
7284
  let body;
@@ -6311,14 +7289,15 @@ function rebaseConflicts(error) {
6311
7289
  }
6312
7290
  const conflicts = body?.conflicts;
6313
7291
  if (!Array.isArray(conflicts)) return null;
6314
- return conflicts.filter(isConflictPath).map(({ path: path12, reason }) => ({ path: path12, reason }));
7292
+ return conflicts.filter(isConflictPath).map(({ path: path13, reason }) => ({ path: path13, reason }));
6315
7293
  }
6316
7294
  function isConflictPath(value) {
6317
7295
  const entry = value;
6318
7296
  return typeof entry?.path === "string" && typeof entry.reason === "string";
6319
7297
  }
6320
7298
  function uncommittedPaths(error) {
6321
- if (!(error instanceof ApiError) || error.status !== 409 || error.code !== "sandbox_uncommitted") return null;
7299
+ if (!(error instanceof ApiError) || error.status !== 409 || error.code !== "sandbox_uncommitted")
7300
+ return null;
6322
7301
  let body;
6323
7302
  try {
6324
7303
  body = JSON.parse(error.text);
@@ -6329,6 +7308,53 @@ function uncommittedPaths(error) {
6329
7308
  if (!Array.isArray(uncommitted)) return null;
6330
7309
  return uncommitted.filter((p) => typeof p === "string");
6331
7310
  }
7311
+ function carriedWarnings(error) {
7312
+ const kept = typeof error === "object" && error !== null ? carried.get(error) ?? [] : [];
7313
+ const shown = error instanceof Error ? error.message : "";
7314
+ return unique([...kept, ...bodyWarnings(error)]).filter((warning) => !shown.includes(warning));
7315
+ }
7316
+ function carrying(error, warnings) {
7317
+ if (warnings.length > 0 && typeof error === "object" && error !== null)
7318
+ carried.set(error, unique([...carried.get(error) ?? [], ...warnings]));
7319
+ return error;
7320
+ }
7321
+ function unique(warnings) {
7322
+ return [...new Set(warnings)];
7323
+ }
7324
+ function bodyWarnings(error) {
7325
+ if (!(error instanceof ApiError)) return [];
7326
+ let body;
7327
+ try {
7328
+ body = JSON.parse(error.text);
7329
+ } catch {
7330
+ return [];
7331
+ }
7332
+ const warnings = body?.warnings;
7333
+ if (!Array.isArray(warnings)) return [];
7334
+ return warnings.filter((w) => typeof w === "string");
7335
+ }
7336
+ function deferredWarnings(error) {
7337
+ if (!(error instanceof ApiError) || error.status !== 409 || error.code !== "sandbox_exec_deferred") return null;
7338
+ return bodyWarnings(error);
7339
+ }
7340
+ function rowsRefusal(error) {
7341
+ if (!(error instanceof ApiError) || error.status !== 400 || error.code !== "rows_refused") return null;
7342
+ let body;
7343
+ try {
7344
+ body = JSON.parse(error.text);
7345
+ } catch {
7346
+ return null;
7347
+ }
7348
+ const { message, batchId, refusalCount, refusals, reportUrl } = body;
7349
+ if (typeof batchId !== "string" || typeof refusalCount !== "number" || !Array.isArray(refusals)) return null;
7350
+ return {
7351
+ message: typeof message === "string" ? message : "",
7352
+ batchId,
7353
+ refusalCount,
7354
+ refusals: refusals.filter((r) => typeof r === "object" && r !== null),
7355
+ reportUrl: typeof reportUrl === "string" ? reportUrl : null
7356
+ };
7357
+ }
6332
7358
  function listQuery(filter) {
6333
7359
  const params = new URLSearchParams();
6334
7360
  if (filter.status) params.set("status", filter.status);
@@ -6337,22 +7363,58 @@ function listQuery(filter) {
6337
7363
  const qs = params.toString();
6338
7364
  return qs ? `?${qs}` : "";
6339
7365
  }
6340
- var isSandboxImportPending, sid, SANDBOXES, SANDBOX_REQUEST_TIMEOUT_MS, WorldSandboxClient;
7366
+ var 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;
6341
7367
  var init_sandbox = __esm({
6342
7368
  "src/worlds/sandbox.ts"() {
6343
7369
  "use strict";
6344
7370
  init_worlds();
7371
+ isSandboxCommitPending = (answer) => !("versionId" in answer);
6345
7372
  isSandboxImportPending = (answer) => !("versionId" in answer);
6346
7373
  sid = encodeURIComponent;
6347
7374
  SANDBOXES = "/api/public/benchmark-containers/sandboxes";
6348
- SANDBOX_REQUEST_TIMEOUT_MS = 66e4;
7375
+ SANDBOX_REQUEST_TIMEOUT_MS = 29e4;
7376
+ SANDBOX_WRITE_CHUNK_BYTES = 4 * 1024 * 1024;
7377
+ SANDBOX_FILE_MAX_BYTES = 256 * 1024 * 1024;
7378
+ SANDBOX_WRITE_SKIPPED = [
7379
+ ".git/",
7380
+ ".gateway/",
7381
+ "scratch/",
7382
+ ".scratch/",
7383
+ "__pycache__/",
7384
+ "*.pyc",
7385
+ ".pytest_cache/",
7386
+ ".mypy_cache/",
7387
+ ".ruff_cache/",
7388
+ "node_modules/",
7389
+ ".venv/",
7390
+ "venv/",
7391
+ ".DS_Store",
7392
+ "*.swp"
7393
+ ];
7394
+ WriteTreeFilter = class {
7395
+ directories = new Set(
7396
+ SANDBOX_WRITE_SKIPPED.filter((pattern) => pattern.endsWith("/")).map((pattern) => pattern.slice(0, -1))
7397
+ );
7398
+ files = SANDBOX_WRITE_SKIPPED.filter((pattern) => !pattern.endsWith("/")).map(nameGlob);
7399
+ skipsDirectory(name) {
7400
+ return this.directories.has(name);
7401
+ }
7402
+ skipsFile(name) {
7403
+ return this.files.some((glob) => glob.test(name));
7404
+ }
7405
+ };
7406
+ SANDBOX_EXEC_WAIT_MS = 4e4;
7407
+ SANDBOX_EXEC_TIMEOUT_MAX_S = 600;
7408
+ SANDBOX_EXEC_REPORT_MARGIN_MS = 35e3;
7409
+ SANDBOX_EXEC_START_ATTEMPTS = 3;
7410
+ carried = /* @__PURE__ */ new WeakMap();
6349
7411
  WorldSandboxClient = class {
6350
7412
  constructor(options = {}) {
6351
7413
  this.options = options;
6352
7414
  }
6353
7415
  options;
6354
- request(method, path12, body) {
6355
- return apiRequest(this.options, method, path12, body, {
7416
+ request(method, path13, body) {
7417
+ return apiRequest(this.options, method, path13, body, {
6356
7418
  timeoutMs: SANDBOX_REQUEST_TIMEOUT_MS
6357
7419
  });
6358
7420
  }
@@ -6370,7 +7432,10 @@ var init_sandbox = __esm({
6370
7432
  }
6371
7433
  /** The project's sandboxes, newest first — how an agent finds one it left open. */
6372
7434
  async list(filter = {}) {
6373
- const res = await this.request("GET", `${SANDBOXES}${listQuery(filter)}`);
7435
+ const res = await this.request(
7436
+ "GET",
7437
+ `${SANDBOXES}${listQuery(filter)}`
7438
+ );
6374
7439
  return res.sandboxes;
6375
7440
  }
6376
7441
  async read(sandboxId, filePath) {
@@ -6378,16 +7443,72 @@ var init_sandbox = __esm({
6378
7443
  "GET",
6379
7444
  `${SANDBOXES}/${sid(sandboxId)}/file?path=${encodeURIComponent(filePath)}`
6380
7445
  );
6381
- return { path: res.path, size: res.size, bytes: Buffer.from(res.contentBase64, "base64") };
7446
+ return {
7447
+ path: res.path,
7448
+ size: res.size,
7449
+ bytes: Buffer.from(res.contentBase64, "base64")
7450
+ };
6382
7451
  }
6383
- write(sandboxId, filePath, bytes) {
6384
- return this.request("PUT", `${SANDBOXES}/${sid(sandboxId)}/file`, {
6385
- path: filePath,
6386
- contentBase64: bytes.toString("base64")
6387
- });
7452
+ /** A file past one chunk goes up in parts under one upload id; the platform lands it whole once the last part's
7453
+ * size and SHA-256 check out. */
7454
+ async write(sandboxId, filePath, bytes) {
7455
+ if (bytes.length <= SANDBOX_WRITE_CHUNK_BYTES)
7456
+ return this.putFile(sandboxId, { path: filePath, contentBase64: bytes.toString("base64") });
7457
+ const whole = { upload: randomUUID2(), size: bytes.length, sha256: createHash5("sha256").update(bytes).digest("hex") };
7458
+ let answer = { path: filePath, received: 0 };
7459
+ for (let offset = 0; offset < bytes.length; offset += SANDBOX_WRITE_CHUNK_BYTES) {
7460
+ const part = bytes.subarray(offset, offset + SANDBOX_WRITE_CHUNK_BYTES);
7461
+ answer = await this.putFile(sandboxId, {
7462
+ path: filePath,
7463
+ contentBase64: part.toString("base64"),
7464
+ ...whole,
7465
+ offset,
7466
+ last: offset + part.length === bytes.length
7467
+ });
7468
+ }
7469
+ return answer;
7470
+ }
7471
+ putFile(sandboxId, body) {
7472
+ return this.request("PUT", `${SANDBOXES}/${sid(sandboxId)}/file`, body);
7473
+ }
7474
+ /** Runs as a job on the platform: a command that outlasts one request is waited on again until its result. */
7475
+ async exec(sandboxId, input) {
7476
+ const budgetMs = (input.timeoutMs ?? SANDBOX_EXEC_TIMEOUT_MAX_S * 1e3) + SANDBOX_EXEC_REPORT_MARGIN_MS;
7477
+ let answer = await this.startExec(sandboxId, { ...input, waitMs: SANDBOX_EXEC_WAIT_MS });
7478
+ const deadline = Date.now() + budgetMs;
7479
+ const warnings = [...answer.warnings ?? []];
7480
+ try {
7481
+ while ("state" in answer) {
7482
+ if (Date.now() >= deadline)
7483
+ throw new Error(
7484
+ `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.`
7485
+ );
7486
+ answer = await this.execStep(sandboxId, { execId: answer.execId, waitMs: SANDBOX_EXEC_WAIT_MS });
7487
+ warnings.push(...answer.warnings ?? []);
7488
+ }
7489
+ } catch (error) {
7490
+ throw carrying(error, warnings);
7491
+ }
7492
+ return { ...answer, warnings };
7493
+ }
7494
+ // A deferred start ran nothing (the platform was still getting the sandbox ready), so starting again is safe. What
7495
+ // getting ready found rides on the deferral alone (no later call reports it), so it is kept for the answer, or for
7496
+ // the error when a later attempt fails.
7497
+ async startExec(sandboxId, body) {
7498
+ const kept = [];
7499
+ for (let attempt = 1; ; attempt += 1) {
7500
+ try {
7501
+ const answer = await this.execStep(sandboxId, body);
7502
+ return { ...answer, warnings: unique([...kept, ...answer.warnings ?? []]) };
7503
+ } catch (error) {
7504
+ const warnings = deferredWarnings(error);
7505
+ if (warnings === null || attempt === SANDBOX_EXEC_START_ATTEMPTS) throw carrying(error, kept);
7506
+ kept.push(...warnings);
7507
+ }
7508
+ }
6388
7509
  }
6389
- exec(sandboxId, input) {
6390
- return this.request("POST", `${SANDBOXES}/${sid(sandboxId)}/exec`, input);
7510
+ execStep(sandboxId, body) {
7511
+ return this.request("POST", `${SANDBOXES}/${sid(sandboxId)}/exec`, body);
6391
7512
  }
6392
7513
  check(sandboxId) {
6393
7514
  return this.request("POST", `${SANDBOXES}/${sid(sandboxId)}/check`, {});
@@ -6395,7 +7516,11 @@ var init_sandbox = __esm({
6395
7516
  /** With `rebase`, a moved base is replayed onto; a 409 then names the conflicting paths (`rebaseConflicts`). */
6396
7517
  commit(sandboxId, input) {
6397
7518
  const { rebase, ...rest } = input;
6398
- return this.request("POST", `${SANDBOXES}/${sid(sandboxId)}/commit`, rebase ? { ...rest, rebase: true } : rest);
7519
+ return this.request(
7520
+ "POST",
7521
+ `${SANDBOXES}/${sid(sandboxId)}/commit`,
7522
+ rebase ? { ...rest, rebase: true } : rest
7523
+ );
6399
7524
  }
6400
7525
  /**
6401
7526
  * A rows file already in the sandbox tree into the PLATFORM world the
@@ -6408,7 +7533,11 @@ var init_sandbox = __esm({
6408
7533
  }
6409
7534
  /** Uncommitted work is a 409 (`uncommittedPaths`) unless `force` discards it. */
6410
7535
  close(sandboxId, opts = {}) {
6411
- return this.request("DELETE", `${SANDBOXES}/${sid(sandboxId)}`, opts.force ? { force: true } : void 0);
7536
+ return this.request(
7537
+ "DELETE",
7538
+ `${SANDBOXES}/${sid(sandboxId)}`,
7539
+ opts.force ? { force: true } : void 0
7540
+ );
6412
7541
  }
6413
7542
  };
6414
7543
  }
@@ -6727,7 +7856,7 @@ var init_json = __esm({
6727
7856
  });
6728
7857
 
6729
7858
  // src/worlds/transforms.ts
6730
- import { createHash as createHash5 } from "crypto";
7859
+ import { createHash as createHash6 } from "crypto";
6731
7860
  function parseTransform(name) {
6732
7861
  const simple = Object.prototype.hasOwnProperty.call(SIMPLE, name) ? SIMPLE[name] : void 0;
6733
7862
  if (simple) return simple;
@@ -6765,7 +7894,7 @@ var init_transforms = __esm({
6765
7894
  return at === -1 ? "" : value.slice(at + 1).toLowerCase();
6766
7895
  };
6767
7896
  stripPrefix = (prefix) => (value) => value.startsWith(prefix) ? value.slice(prefix.length) : value;
6768
- hex = (algorithm, value) => createHash5(algorithm).update(value, "utf8").digest("hex");
7897
+ hex = (algorithm, value) => createHash6(algorithm).update(value, "utf8").digest("hex");
6769
7898
  SIMPLE = {
6770
7899
  lower: (value) => value.toLowerCase(),
6771
7900
  upper: (value) => value.toUpperCase(),
@@ -6789,7 +7918,7 @@ var init_transforms = __esm({
6789
7918
  });
6790
7919
 
6791
7920
  // src/worlds/redaction.ts
6792
- import { createHash as createHash6 } from "crypto";
7921
+ import { createHash as createHash7 } from "crypto";
6793
7922
  function pyText(value) {
6794
7923
  if (typeof value === "string") return value;
6795
7924
  if (Number.isInteger(value)) return String(value);
@@ -6798,7 +7927,7 @@ function pyText(value) {
6798
7927
  return pyFloatRepr(value);
6799
7928
  }
6800
7929
  function hashForm(slug, plaintext) {
6801
- return "sha256:" + createHash6("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
7930
+ return "sha256:" + createHash7("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
6802
7931
  }
6803
7932
  function preserveForm(slug, plaintext) {
6804
7933
  const kind = typeof plaintext === "string" ? "str" : isInt(plaintext) ? "int" : "float";
@@ -6807,12 +7936,12 @@ function preserveForm(slug, plaintext) {
6807
7936
  }
6808
7937
  function shapeText(slug, text2, kind) {
6809
7938
  const seed = Buffer.from(`${slug}:${text2}`, "utf8");
6810
- let digest = createHash6("sha256").update(seed).digest();
7939
+ let digest = createHash7("sha256").update(seed).digest();
6811
7940
  let cursor = 0;
6812
7941
  const out = [];
6813
7942
  for (const char of text2) {
6814
7943
  if (cursor === digest.length) {
6815
- digest = createHash6("sha256").update(Buffer.concat([digest, seed])).digest();
7944
+ digest = createHash7("sha256").update(Buffer.concat([digest, seed])).digest();
6816
7945
  cursor = 0;
6817
7946
  }
6818
7947
  const byte = digest[cursor];
@@ -6883,6 +8012,17 @@ async function queryRows(slugRef, entity, opts = {}, options = {}) {
6883
8012
  `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
6884
8013
  );
6885
8014
  }
8015
+ async function sqlRows(slugRef, sql, opts = {}, options = {}) {
8016
+ const [slug, ref] = slugRef.split("@", 2);
8017
+ if (!slug || ref === "") throw new Error(`sqlRows needs a slug, optionally slug@ref (got ${slugRef})`);
8018
+ if (!sql.trim()) throw new Error("sqlRows needs one SQL statement");
8019
+ return apiRequest(options, "POST", `/api/public/worlds/${encodeURIComponent(slug)}/data/sql`, {
8020
+ sql,
8021
+ ...ref ? { ref } : {},
8022
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
8023
+ ...opts.scenario ? { scenario: opts.scenario } : {}
8024
+ });
8025
+ }
6886
8026
  var init_query = __esm({
6887
8027
  "src/worlds/query.ts"() {
6888
8028
  "use strict";
@@ -6939,7 +8079,7 @@ var init_router = __esm({
6939
8079
  // src/cli/config.ts
6940
8080
  import * as fs6 from "fs";
6941
8081
  import * as os from "os";
6942
- import * as path6 from "path";
8082
+ import * as path7 from "path";
6943
8083
  async function api(creds, method, route, body, init2 = {}) {
6944
8084
  const reply = await new PlatformTransport(creds).send(method, route, body, init2);
6945
8085
  if (!reply.ok) throw new ApiError2(reply.status, method, route, reply.text);
@@ -6954,7 +8094,7 @@ var init_config = __esm({
6954
8094
  init_transport();
6955
8095
  ApiError2 = class extends CliError {
6956
8096
  constructor(status, method, route, text2) {
6957
- const body = parseErrorBody(text2);
8097
+ const body = parseErrorBody(text2, status);
6958
8098
  super(`${method} ${route} -> ${status}: ${body.detail}`, 1);
6959
8099
  this.status = status;
6960
8100
  this.method = method;
@@ -6975,7 +8115,15 @@ var init_config = __esm({
6975
8115
  import { spawn, spawnSync } from "child_process";
6976
8116
  import * as fs7 from "fs";
6977
8117
  import * as net from "net";
6978
- import * as path7 from "path";
8118
+ import * as path8 from "path";
8119
+ function forwardedEnv() {
8120
+ return Object.fromEntries(
8121
+ FORWARDED_ENV.flatMap((name) => {
8122
+ const value = process.env[name];
8123
+ return value ? [[name, value]] : [];
8124
+ })
8125
+ );
8126
+ }
6979
8127
  function tailOf(output) {
6980
8128
  return output.length > DIAGNOSTIC_TAIL ? `\u2026${output.slice(-DIAGNOSTIC_TAIL)}` : output;
6981
8129
  }
@@ -6990,16 +8138,16 @@ function runtimeRoot() {
6990
8138
  const candidates = [];
6991
8139
  if (process.env.GATEWAY_RUNTIME_PATH)
6992
8140
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
6993
- const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path7.dirname(fs7.realpathSync(process.argv[1])) : null;
8141
+ const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path8.dirname(fs7.realpathSync(process.argv[1])) : null;
6994
8142
  if (entry) {
6995
- candidates.push(path7.resolve(entry, "..", "runtime"));
6996
- candidates.push(path7.resolve(entry, "..", "..", "runtime"));
6997
- candidates.push(path7.resolve(entry, "..", "..", "sdk", "src"));
8143
+ candidates.push(path8.resolve(entry, "..", "runtime"));
8144
+ candidates.push(path8.resolve(entry, "..", "..", "runtime"));
8145
+ candidates.push(path8.resolve(entry, "..", "..", "sdk", "src"));
6998
8146
  }
6999
- candidates.push(path7.resolve(process.cwd(), "runtime"));
8147
+ candidates.push(path8.resolve(process.cwd(), "runtime"));
7000
8148
  for (const root of candidates)
7001
8149
  if (fs7.existsSync(
7002
- path7.join(root, "gatewaysdk", "environment", "schema", "platform.py")
8150
+ path8.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7003
8151
  ))
7004
8152
  return root;
7005
8153
  return null;
@@ -7088,13 +8236,12 @@ async function localBridge(request, local, opts = {}) {
7088
8236
  ["-I", "-B", "-c", BRIDGE_BOOTSTRAP, local.root],
7089
8237
  {
7090
8238
  stdio: ["pipe", "pipe", "pipe"],
7091
- // This is the author's machine running the author's world: the one
7092
- // place the runtime may import and run a world's own Python (check's
7093
- // handler execution, `worlds call`, `worlds test` tool cases).
8239
+ // The author's machine running the author's world: the one place the runtime may run its Python.
7094
8240
  env: {
7095
8241
  PATH: process.env.PATH ?? "",
7096
8242
  HOME: process.env.HOME ?? "",
7097
8243
  TMPDIR: process.env.TMPDIR ?? "",
8244
+ ...forwardedEnv(),
7098
8245
  GATEWAY_RUNTIME_EXECUTE: "1"
7099
8246
  }
7100
8247
  }
@@ -7140,7 +8287,7 @@ async function localBridge(request, local, opts = {}) {
7140
8287
  child.stdin.end(input);
7141
8288
  });
7142
8289
  }
7143
- var MIN_PYTHON, MIN_PYTHON_LABEL, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
8290
+ var MIN_PYTHON, MIN_PYTHON_LABEL, DIAGNOSTIC_TAIL, FORWARDED_ENV, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
7144
8291
  var init_local = __esm({
7145
8292
  "src/cli/runtime/local.ts"() {
7146
8293
  "use strict";
@@ -7149,6 +8296,7 @@ var init_local = __esm({
7149
8296
  MIN_PYTHON = [3, 12];
7150
8297
  MIN_PYTHON_LABEL = MIN_PYTHON.join(".");
7151
8298
  DIAGNOSTIC_TAIL = 600;
8299
+ FORWARDED_ENV = ["VIRTUAL_ENV", "GATEWAY_SNAPSHOT_STORE"];
7152
8300
  BRIDGE_BOOTSTRAP = `import sys,runpy,types,os
7153
8301
  root=sys.argv[1]
7154
8302
  package=types.ModuleType('gatewaysdk')
@@ -7166,18 +8314,46 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
7166
8314
  }
7167
8315
  });
7168
8316
 
8317
+ // src/worlds/databaseEngines.ts
8318
+ function isWorldSnapshotOutput(path13) {
8319
+ return SNAPSHOT_OUTPUTS.has(path13.slice(path13.lastIndexOf("/") + 1));
8320
+ }
8321
+ var WORLD_DATABASE_ENGINES, DEFAULT_WORLD_DATABASE_ENGINE, WORLD_SNAPSHOT_FILE_PATH, WORLD_SNAPSHOT_SIDECAR_PATH, WORLD_SNAPSHOT_CONTENT_TYPE, SNAPSHOT_OUTPUTS;
8322
+ var init_databaseEngines = __esm({
8323
+ "src/worlds/databaseEngines.ts"() {
8324
+ "use strict";
8325
+ WORLD_DATABASE_ENGINES = [
8326
+ {
8327
+ engine: "sqlite",
8328
+ label: "SQLite",
8329
+ target: "sqlite-json/1",
8330
+ snapshotName: "state.sqlite",
8331
+ contentType: "application/vnd.sqlite3"
8332
+ }
8333
+ ];
8334
+ DEFAULT_WORLD_DATABASE_ENGINE = WORLD_DATABASE_ENGINES[0];
8335
+ WORLD_SNAPSHOT_FILE_PATH = `db/${DEFAULT_WORLD_DATABASE_ENGINE.snapshotName}`;
8336
+ WORLD_SNAPSHOT_SIDECAR_PATH = "db/state.json";
8337
+ WORLD_SNAPSHOT_CONTENT_TYPE = DEFAULT_WORLD_DATABASE_ENGINE.contentType;
8338
+ SNAPSHOT_OUTPUTS = /* @__PURE__ */ new Set([
8339
+ ...WORLD_DATABASE_ENGINES.map((engine) => engine.snapshotName),
8340
+ WORLD_SNAPSHOT_SIDECAR_PATH.slice("db/".length)
8341
+ ]);
8342
+ }
8343
+ });
8344
+
7169
8345
  // src/cli/runtime/client.ts
7170
8346
  import * as fs8 from "fs";
7171
- import * as path8 from "path";
8347
+ import * as path9 from "path";
7172
8348
  function isBuildOutput(relPath) {
7173
- return BUILD_OUTPUT.test(relPath);
8349
+ return isWorldSnapshotOutput(relPath);
7174
8350
  }
7175
8351
  function readWorldFiles(dir, opts = {}) {
7176
8352
  if (!fs8.existsSync(dir) || !fs8.statSync(dir).isDirectory())
7177
8353
  throw new CliError(`Not a directory: ${dir}`);
7178
8354
  const out = [];
7179
8355
  const walk = (rel) => {
7180
- const abs = rel ? path8.join(dir, rel) : dir;
8356
+ const abs = rel ? path9.join(dir, rel) : dir;
7181
8357
  const entries = fs8.readdirSync(abs, { withFileTypes: true });
7182
8358
  entries.sort((a, b) => a.name.localeCompare(b.name));
7183
8359
  for (const entry of entries) {
@@ -7188,7 +8364,7 @@ function readWorldFiles(dir, opts = {}) {
7188
8364
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
7189
8365
  out.push({
7190
8366
  path: childRel,
7191
- content: fs8.readFileSync(path8.join(dir, childRel)).toString("base64")
8367
+ content: fs8.readFileSync(path9.join(dir, childRel)).toString("base64")
7192
8368
  });
7193
8369
  }
7194
8370
  }
@@ -7208,15 +8384,15 @@ function writeWorldFiles(dir, files, opts = {}) {
7208
8384
  if (opts.clean && fs8.existsSync(dir)) {
7209
8385
  const keep = new Set(files.map((f) => f.path));
7210
8386
  for (const existing of readWorldFiles(dir)) {
7211
- if (!keep.has(existing.path)) fs8.rmSync(path8.join(dir, existing.path));
8387
+ if (!keep.has(existing.path)) fs8.rmSync(path9.join(dir, existing.path));
7212
8388
  }
7213
8389
  pruneEmptyDirs(dir);
7214
8390
  }
7215
8391
  fs8.mkdirSync(dir, { recursive: true });
7216
8392
  const written = [];
7217
8393
  for (const file of files) {
7218
- const target = path8.join(dir, file.path);
7219
- fs8.mkdirSync(path8.dirname(target), { recursive: true });
8394
+ const target = path9.join(dir, file.path);
8395
+ fs8.mkdirSync(path9.dirname(target), { recursive: true });
7220
8396
  fs8.writeFileSync(target, Buffer.from(file.content, "base64"));
7221
8397
  written.push(file.path);
7222
8398
  }
@@ -7226,7 +8402,7 @@ function pruneEmptyDirs(root) {
7226
8402
  const walk = (abs) => {
7227
8403
  let empty = true;
7228
8404
  for (const entry of fs8.readdirSync(abs, { withFileTypes: true })) {
7229
- const child = path8.join(abs, entry.name);
8405
+ const child = path9.join(abs, entry.name);
7230
8406
  if (entry.isDirectory()) {
7231
8407
  if (SKIP_DIRS.has(entry.name)) {
7232
8408
  empty = false;
@@ -7263,13 +8439,9 @@ function yamlDocument(request) {
7263
8439
  return isYamlSource(String(request.source ?? ""), String(request.document ?? ""));
7264
8440
  return request.command === "conform" && Array.isArray(request.openapi) && request.openapi.some((doc) => YAML_NAME.test(doc.path ?? ""));
7265
8441
  }
7266
- function runsLocally(request) {
7267
- const local = localRuntime();
7268
- return local !== null && !(yamlDocument(request) && !localPythonHasYaml(local.python));
7269
- }
7270
8442
  async function runtime(creds, request, opts = {}) {
7271
- const local = localRuntime();
7272
- if (local && runsLocally(request)) {
8443
+ const local = new RuntimeRoute().local(request);
8444
+ if (local) {
7273
8445
  const payload2 = await localBridge(request, local, opts);
7274
8446
  const detail2 = detailFrom(payload2);
7275
8447
  if (detail2) throw new BridgeRejected(detail2);
@@ -7296,17 +8468,17 @@ async function runtime(creds, request, opts = {}) {
7296
8468
  if (payload === null) throw new CliError(`POST ${route} (${request.command}) returned non-JSON`, 1);
7297
8469
  return payload;
7298
8470
  }
7299
- var MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BUILD_OUTPUT, BridgeRejected, YAML_NAME;
8471
+ var MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BridgeRejected, YAML_NAME, YAML_NEEDS_PYYAML, YAML_NOT_ON_PLATFORM, RuntimeRoute;
7300
8472
  var init_client = __esm({
7301
8473
  "src/cli/runtime/client.ts"() {
7302
8474
  "use strict";
7303
8475
  init_router();
7304
8476
  init_local();
7305
8477
  init_transport();
8478
+ init_databaseEngines();
7306
8479
  MAX_WORLD_FILES = 2e3;
7307
8480
  RUNTIME_TIMEOUT_MS = 18e4;
7308
8481
  SKIP_DIRS = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
7309
- BUILD_OUTPUT = /(^|\/)state\.(sqlite|json)$/;
7310
8482
  BridgeRejected = class extends CliError {
7311
8483
  constructor(detail) {
7312
8484
  super(formatBridgeError(detail), 1);
@@ -7315,6 +8487,26 @@ var init_client = __esm({
7315
8487
  detail;
7316
8488
  };
7317
8489
  YAML_NAME = /\.ya?ml$/i;
8490
+ 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";
8491
+ 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)";
8492
+ RuntimeRoute = class _RuntimeRoute {
8493
+ constructor(here = localRuntime, hasYaml = localPythonHasYaml) {
8494
+ this.here = here;
8495
+ this.hasYaml = hasYaml;
8496
+ }
8497
+ here;
8498
+ hasYaml;
8499
+ local(request) {
8500
+ const local = this.here();
8501
+ const yaml = yamlDocument(request);
8502
+ if (yaml && local === null) throw _RuntimeRoute.refusal(YAML_NOT_ON_PLATFORM);
8503
+ if (yaml && local !== null && !this.hasYaml(local.python)) throw _RuntimeRoute.refusal(YAML_NEEDS_PYYAML);
8504
+ return local;
8505
+ }
8506
+ static refusal(message) {
8507
+ return new BridgeRejected({ code: "conform", message, schema_path: "/openapi/source" });
8508
+ }
8509
+ };
7318
8510
  }
7319
8511
  });
7320
8512
 
@@ -7329,10 +8521,11 @@ __export(data_exports, {
7329
8521
  ingestLocalToolCalls: () => ingestLocalToolCalls,
7330
8522
  queryLocalWorldData: () => queryLocalWorldData,
7331
8523
  queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
7332
- showLocalWorldData: () => showLocalWorldData
8524
+ showLocalWorldData: () => showLocalWorldData,
8525
+ sqlLocalWorldData: () => sqlLocalWorldData
7333
8526
  });
7334
8527
  import * as fs9 from "fs";
7335
- import * as path9 from "path";
8528
+ import * as path10 from "path";
7336
8529
  async function importLocalWorldData(dir, rows, opts, creds) {
7337
8530
  const res = await runtime(creds, {
7338
8531
  command: "import-data",
@@ -7363,8 +8556,8 @@ async function importLocalWorldFiles(dir, sources, opts) {
7363
8556
  );
7364
8557
  const res = await runtime(null, {
7365
8558
  command: "import-files",
7366
- world: path9.resolve(dir),
7367
- sources: sources.map((source) => path9.resolve(source)),
8559
+ world: path10.resolve(dir),
8560
+ sources: sources.map((source) => path10.resolve(source)),
7368
8561
  mode: opts.mode ?? "append",
7369
8562
  ...opts.entity ? { entity: opts.entity } : {},
7370
8563
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
@@ -7390,9 +8583,9 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7390
8583
  );
7391
8584
  const res = await runtime(null, {
7392
8585
  command: "chunk-files",
7393
- world: path9.resolve(dir),
7394
- sources: sources.map((source) => path9.resolve(source)),
7395
- out: path9.resolve(out),
8586
+ world: path10.resolve(dir),
8587
+ sources: sources.map((source) => path10.resolve(source)),
8588
+ out: path10.resolve(out),
7396
8589
  ...opts.entity ? { entity: opts.entity } : {},
7397
8590
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
7398
8591
  ...opts.chunkBytes ? { chunk_bytes: opts.chunkBytes } : {},
@@ -7403,8 +8596,8 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7403
8596
  function dropBuildOutputs(dir) {
7404
8597
  const dropped = [];
7405
8598
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
7406
- if (!BUILD_OUTPUT2.test(file.path)) continue;
7407
- fs9.rmSync(path9.join(dir, file.path), { force: true });
8599
+ if (!isBuildOutput(file.path)) continue;
8600
+ fs9.rmSync(path10.join(dir, file.path), { force: true });
7408
8601
  dropped.push(file.path);
7409
8602
  }
7410
8603
  return dropped;
@@ -7471,7 +8664,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7471
8664
  const payload = await localBridge(
7472
8665
  {
7473
8666
  command: "query",
7474
- world: path9.resolve(dir),
8667
+ world: path10.resolve(dir),
7475
8668
  entity,
7476
8669
  across: true,
7477
8670
  ...opts.compare ? { compare: true } : {},
@@ -7487,6 +8680,28 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7487
8680
  if (detail) throw new BridgeRejected(detail);
7488
8681
  return payload;
7489
8682
  }
8683
+ async function sqlLocalWorldData(dir, sql, opts) {
8684
+ const local = localRuntime();
8685
+ if (!local)
8686
+ throw new CliError(
8687
+ "`worlds data sql` on a directory reads the world's database in place, so it needs the bundled runtime on this machine. " + localRuntimeReason(),
8688
+ 1
8689
+ );
8690
+ const payload = await localBridge(
8691
+ {
8692
+ command: "sql",
8693
+ world: path10.resolve(dir),
8694
+ sql,
8695
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
8696
+ ...opts.scenario ? { scenario: opts.scenario } : {}
8697
+ },
8698
+ local
8699
+ );
8700
+ const detail = detailFrom(payload);
8701
+ if (detail) throw new BridgeRejected(detail);
8702
+ const { status: _status, ...answer } = payload;
8703
+ return answer;
8704
+ }
7490
8705
  async function ingestLocalToolCalls(dir, records, opts, creds) {
7491
8706
  const res = await runtime(creds, {
7492
8707
  command: "ingest-calls",
@@ -7518,14 +8733,12 @@ async function ingestLocalToolCalls(dir, records, opts, creds) {
7518
8733
  ...res.batch !== void 0 ? { batch: res.batch } : {}
7519
8734
  };
7520
8735
  }
7521
- var BUILD_OUTPUT2;
7522
8736
  var init_data = __esm({
7523
8737
  "src/cli/runtime/data.ts"() {
7524
8738
  "use strict";
7525
8739
  init_router();
7526
8740
  init_client();
7527
8741
  init_local();
7528
- BUILD_OUTPUT2 = /(^|\/)state\.(sqlite|json)$/;
7529
8742
  }
7530
8743
  });
7531
8744
 
@@ -7544,7 +8757,7 @@ __export(transform_exports, {
7544
8757
  import { spawn as spawn2 } from "child_process";
7545
8758
  import * as fs10 from "fs";
7546
8759
  import * as os2 from "os";
7547
- import * as path10 from "path";
8760
+ import * as path11 from "path";
7548
8761
  import { createRequire } from "module";
7549
8762
  function checkRows(value, where) {
7550
8763
  if (!isObject(value)) throw new CliError(`${where} must return {entity: [rows]}; got ${value === null ? "null" : Array.isArray(value) ? "an array" : typeof value}`, 1);
@@ -7569,27 +8782,27 @@ function typescriptPrefix() {
7569
8782
  }
7570
8783
  }
7571
8784
  function command(script, wrapperDir) {
7572
- const suffix = path10.extname(script).toLowerCase();
8785
+ const suffix = path11.extname(script).toLowerCase();
7573
8786
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
7574
8787
  if (NODE_SUFFIXES.includes(suffix)) {
7575
- const wrapper = path10.join(wrapperDir, "transform-wrapper.mjs");
8788
+ const wrapper = path11.join(wrapperDir, "transform-wrapper.mjs");
7576
8789
  fs10.writeFileSync(wrapper, NODE_WRAPPER);
7577
8790
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
7578
8791
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
7579
8792
  }
7580
- throw new CliError(`${path10.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
8793
+ throw new CliError(`${path11.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
7581
8794
  }
7582
8795
  async function runTransform(script, records, opts = {}) {
7583
8796
  if (!fs10.existsSync(script) || !fs10.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
7584
- const resolved = path10.resolve(script);
7585
- const name = path10.basename(script);
8797
+ const resolved = path11.resolve(script);
8798
+ const name = path11.basename(script);
7586
8799
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
7587
- const scratch = fs10.mkdtempSync(path10.join(os2.tmpdir(), "gateway-transform-"));
8800
+ const scratch = fs10.mkdtempSync(path11.join(os2.tmpdir(), "gateway-transform-"));
7588
8801
  try {
7589
8802
  const { file, args } = command(resolved, scratch);
7590
8803
  const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !SCRUBBED.test(key)));
7591
8804
  const done = await new Promise((resolve7, reject) => {
7592
- const child = spawn2(file, args, { cwd: path10.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
8805
+ const child = spawn2(file, args, { cwd: path11.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
7593
8806
  let stdout = "";
7594
8807
  let stderr = "";
7595
8808
  let timedOut = false;
@@ -7712,18 +8925,22 @@ if (fn) {
7712
8925
  });
7713
8926
 
7714
8927
  // src/cli/platform/worldData/batches.ts
7715
- import { createHash as createHash7 } from "crypto";
8928
+ import { createHash as createHash8 } from "crypto";
7716
8929
  import * as fs11 from "fs";
7717
8930
  function isSettled(status) {
7718
8931
  if (!TERMINAL_STATES.has(status.state)) return false;
7719
8932
  if (status.state !== "applied") return true;
7720
8933
  return status.dryRun === true || status.publish !== "now" || !status.snapshotId;
7721
8934
  }
8935
+ function worldDataPollMs() {
8936
+ const raw = Number(process.env.GATEWAY_WORLD_DATA_POLL_MS ?? 1e3);
8937
+ return Number.isFinite(raw) && raw >= 0 ? raw : 1e3;
8938
+ }
7722
8939
  function uploadHeadersFor(gz) {
7723
8940
  return {
7724
8941
  "content-type": "application/gzip",
7725
8942
  "content-length": String(gz.length),
7726
- "x-amz-checksum-sha256": createHash7("sha256").update(gz).digest("base64")
8943
+ "x-amz-checksum-sha256": createHash8("sha256").update(gz).digest("base64")
7727
8944
  };
7728
8945
  }
7729
8946
  async function forEachLimited(items, limit, fn) {
@@ -7750,7 +8967,7 @@ async function forEachLimited(items, limit, fn) {
7750
8967
  if (failure) throw failure;
7751
8968
  }
7752
8969
  async function waitForBatch(client, batchId, opts = {}) {
7753
- const sleep2 = opts.sleep ?? client.sleep;
8970
+ const sleep4 = opts.sleep ?? client.sleep;
7754
8971
  let delay = opts.initialDelayMs ?? 1e3;
7755
8972
  let last = "";
7756
8973
  for (; ; ) {
@@ -7761,7 +8978,9 @@ async function waitForBatch(client, batchId, opts = {}) {
7761
8978
  last = phase;
7762
8979
  }
7763
8980
  if (isSettled(status)) return status;
7764
- await sleep2(delay);
8981
+ if (opts.deadlineMs !== void 0 && (opts.now ?? Date.now)() >= opts.deadlineMs)
8982
+ return status;
8983
+ await sleep4(delay);
7765
8984
  delay = Math.min(1e4, Math.round(delay * 1.5));
7766
8985
  }
7767
8986
  }
@@ -7783,10 +9002,10 @@ var init_batches = __esm({
7783
9002
  ]);
7784
9003
  sleepMs = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
7785
9004
  WorldDataApi = class {
7786
- constructor(creds, slug, sleep2 = sleepMs) {
9005
+ constructor(creds, slug, sleep4 = sleepMs) {
7787
9006
  this.creds = creds;
7788
9007
  this.slug = slug;
7789
- this.sleep = sleep2;
9008
+ this.sleep = sleep4;
7790
9009
  this.base = `/api/public/worlds/${encodeURIComponent(slug)}/data`;
7791
9010
  }
7792
9011
  creds;
@@ -7810,11 +9029,6 @@ var init_batches = __esm({
7810
9029
  }
7811
9030
  );
7812
9031
  }
7813
- /**
7814
- * Stage a project-drive rows file as one chunk, on the platform: the server
7815
- * streams it out of drive storage into chunk storage and answers with the
7816
- * digest and gzipped size it derived. No bytes pass through this process.
7817
- */
7818
9032
  async stageDrive(batchId, chunk) {
7819
9033
  const staged = await api(this.creds, "POST", `${this.base}/batches/${batchId}/chunks`, {
7820
9034
  ordinal: chunk.ordinal,
@@ -7868,11 +9082,6 @@ var init_batches = __esm({
7868
9082
  message ? { message } : {}
7869
9083
  );
7870
9084
  }
7871
- /**
7872
- * PUT the gzipped chunk to its presigned URL with exactly the headers the
7873
- * URL was signed for (the platform's `uploadHeaders`; the same three when it
7874
- * sends none), with backoff on a dropped connection or a 5xx.
7875
- */
7876
9085
  async put(uploadUrl, gz, signed) {
7877
9086
  const headers = signed ?? uploadHeadersFor(gz);
7878
9087
  const delays = [500, 1500, 4e3];
@@ -7899,7 +9108,6 @@ var init_batches = __esm({
7899
9108
  await this.sleep(delays[attempt]);
7900
9109
  }
7901
9110
  }
7902
- /** Register, upload unless the server already holds the content, confirm. */
7903
9111
  async upload(batchId, chunk) {
7904
9112
  const registered = await this.register(batchId, chunk);
7905
9113
  if (!registered.uploaded) {
@@ -7926,7 +9134,7 @@ var init_batches = __esm({
7926
9134
  });
7927
9135
 
7928
9136
  // src/cli/platform/worldData/chunker.ts
7929
- import { createHash as createHash8 } from "crypto";
9137
+ import { createHash as createHash9 } from "crypto";
7930
9138
  import { deflateRawSync } from "zlib";
7931
9139
  function chunkLine(row, declared) {
7932
9140
  if (declared !== null) return dumps(row.row) + "\n";
@@ -7967,12 +9175,12 @@ function finishChunk(ordinal, raw, rows, entity, firstRow = 0) {
7967
9175
  const gz = gzipDeterministic(raw, { name: chunkName(ordinal) });
7968
9176
  return {
7969
9177
  ordinal,
7970
- digest: createHash8("sha256").update(gz).digest("hex"),
9178
+ digest: createHash9("sha256").update(gz).digest("hex"),
7971
9179
  bytes: gz.length,
7972
9180
  rows,
7973
9181
  entity,
7974
9182
  rawBytes: raw.length,
7975
- contentSha256: createHash8("sha256").update(raw).digest("hex"),
9183
+ contentSha256: createHash9("sha256").update(raw).digest("hex"),
7976
9184
  firstRow,
7977
9185
  gz
7978
9186
  };
@@ -8040,22 +9248,33 @@ async function pushRows(creds, slug, rows, opts = {}) {
8040
9248
  publish: opts.publish ?? "now",
8041
9249
  ...opts.message ? { message: opts.message } : {}
8042
9250
  });
8043
- if (!opened.batchId) throw new Error("the platform opened no batch (no batchId in the answer)");
9251
+ if (!opened.batchId)
9252
+ throw new Error("the platform opened no batch (no batchId in the answer)");
8044
9253
  const tagged = (async function* () {
8045
9254
  for (const [entity, listed] of Object.entries(rows))
8046
9255
  for (let index = 0; index < listed.length; index++)
8047
- yield { entity, row: parseJson(JSON.stringify(listed[index])), where: `${entity}[${index}]` };
9256
+ yield {
9257
+ entity,
9258
+ row: parseJson(JSON.stringify(listed[index])),
9259
+ where: `${entity}[${index}]`
9260
+ };
8048
9261
  })();
8049
- const chunks = buildChunks(tagged, { chunkBytes: opts.chunkBytes ?? DEFAULT_CHUNK_BYTES, chunkRows: null, entity: null });
8050
- await forEachLimited(chunks, Math.max(1, opts.parallel ?? 4), async (chunk) => {
8051
- await client.upload(opened.batchId, chunk);
9262
+ const chunks = buildChunks(tagged, {
9263
+ chunkBytes: opts.chunkBytes ?? DEFAULT_CHUNK_BYTES,
9264
+ chunkRows: null,
9265
+ entity: null
8052
9266
  });
9267
+ await forEachLimited(
9268
+ chunks,
9269
+ Math.max(1, opts.parallel ?? 4),
9270
+ async (chunk) => {
9271
+ await client.upload(opened.batchId, chunk);
9272
+ }
9273
+ );
8053
9274
  await client.complete(opened.batchId);
8054
- return waitForBatch(client, opened.batchId, { initialDelayMs: pollMs() });
8055
- }
8056
- function pollMs() {
8057
- const raw = Number(process.env.GATEWAY_WORLD_DATA_POLL_MS ?? 1e3);
8058
- return Number.isFinite(raw) && raw >= 0 ? raw : 1e3;
9275
+ return waitForBatch(client, opened.batchId, {
9276
+ initialDelayMs: worldDataPollMs()
9277
+ });
8059
9278
  }
8060
9279
  var init_push = __esm({
8061
9280
  "src/cli/platform/worldData/push.ts"() {
@@ -8069,7 +9288,7 @@ var init_push = __esm({
8069
9288
  // src/worlds/ingest.ts
8070
9289
  import * as fs12 from "fs";
8071
9290
  import * as os3 from "os";
8072
- import * as path11 from "path";
9291
+ import * as path12 from "path";
8073
9292
  function parseToolCallRecord(raw) {
8074
9293
  const none = { record: null, skip: null };
8075
9294
  if (!isObject2(raw)) return { ...none, refusal: "record is not an object" };
@@ -8193,19 +9412,13 @@ function toolCallsFromObservations(observations, opts = {}) {
8193
9412
  }
8194
9413
  return out;
8195
9414
  }
8196
- function credentialsOf(options) {
8197
- const host = (options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai").replace(/\/+$/, "");
8198
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8199
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8200
- return publicKey && secretKey ? { host, publicKey, secretKey } : null;
8201
- }
8202
9415
  async function ingest(world, records, options = {}) {
8203
9416
  const { ingestLocalToolCalls: ingestLocalToolCalls2, importLocalWorldData: importLocalWorldData2, checkLocalWorldData: checkLocalWorldData2 } = await Promise.resolve().then(() => (init_data(), data_exports));
8204
9417
  if (options.transform !== void 0 && options.map !== void 0) throw new Error("records go through one of transform, map");
8205
9418
  const redact = options.redact ?? "off";
8206
9419
  const mode = options.mode ?? "append";
8207
9420
  const dryRun = Boolean(options.dryRun);
8208
- const creds = credentialsOf(options);
9421
+ const creds = platformEndpoint(options);
8209
9422
  const local = fs12.existsSync(world) && fs12.statSync(world).isDirectory();
8210
9423
  const map = options.map === void 0 ? void 0 : options.map;
8211
9424
  let rows = null;
@@ -8219,12 +9432,12 @@ async function ingest(world, records, options = {}) {
8219
9432
  const landed = dryRun ? await checkLocalWorldData2(world, rows, creds, { redact }) : await importLocalWorldData2(world, rows, { mode, redact }, creds);
8220
9433
  return transformReport(world, records.length, rows, stderr, { dryRun, redact, changed: !dryRun && landed.changed, kind: landed.kind, entityCounts: landed.entity_counts });
8221
9434
  }
8222
- if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
9435
+ if (!creds) throw new Error(GATEWAY_KEYS_MISSING);
8223
9436
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
8224
9437
  const slug = world.split("@", 1)[0];
8225
- const scratch = fs12.mkdtempSync(path11.join(os3.tmpdir(), "gateway-ingest-"));
9438
+ const scratch = fs12.mkdtempSync(path12.join(os3.tmpdir(), "gateway-ingest-"));
8226
9439
  try {
8227
- const pulled = await pull2(world, path11.join(scratch, slug), options);
9440
+ const pulled = await pull2(world, path12.join(scratch, slug), options);
8228
9441
  let report;
8229
9442
  let projected;
8230
9443
  if (rows === null) {
@@ -8303,18 +9516,19 @@ var init_ingest = __esm({
8303
9516
  "src/worlds/ingest.ts"() {
8304
9517
  "use strict";
8305
9518
  init_worlds();
9519
+ init_platformEndpoint();
8306
9520
  isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
8307
9521
  isoOf = (value) => value instanceof Date ? value.toISOString() : typeof value === "string" ? value : void 0;
8308
9522
  }
8309
9523
  });
8310
9524
 
8311
9525
  // src/worlds/editSessions.ts
8312
- var WORLDS, WorldEditClient, TASK_EDITS, TaskEditClient;
9526
+ var WORLDS2, WorldEditClient, TASK_EDITS, TaskEditClient;
8313
9527
  var init_editSessions = __esm({
8314
9528
  "src/worlds/editSessions.ts"() {
8315
9529
  "use strict";
8316
9530
  init_worlds();
8317
- WORLDS = "/api/public/worlds";
9531
+ WORLDS2 = "/api/public/worlds";
8318
9532
  WorldEditClient = class {
8319
9533
  constructor(options = {}) {
8320
9534
  this.options = options;
@@ -8326,19 +9540,19 @@ var init_editSessions = __esm({
8326
9540
  if (query.status) qs.set("status", query.status);
8327
9541
  if (query.limit !== void 0) qs.set("limit", String(query.limit));
8328
9542
  const suffix = qs.size ? `?${qs.toString()}` : "";
8329
- return apiRequest(this.options, "GET", `${WORLDS}/${encodeURIComponent(slug)}/edits${suffix}`);
9543
+ return apiRequest(this.options, "GET", `${WORLDS2}/${encodeURIComponent(slug)}/edits${suffix}`);
8330
9544
  }
8331
9545
  /** Seal a session into a version on main (the credential's current one when `sessionId` is omitted). */
8332
9546
  close(slug, input = {}) {
8333
- return apiRequest(this.options, "POST", `${WORLDS}/${encodeURIComponent(slug)}/edits/close`, input);
9547
+ return apiRequest(this.options, "POST", `${WORLDS2}/${encodeURIComponent(slug)}/edits/close`, input);
8334
9548
  }
8335
9549
  /** Drop the session's private branch without making a version. */
8336
9550
  discard(slug, input = {}) {
8337
- return apiRequest(this.options, "POST", `${WORLDS}/${encodeURIComponent(slug)}/edits/discard`, input);
9551
+ return apiRequest(this.options, "POST", `${WORLDS2}/${encodeURIComponent(slug)}/edits/discard`, input);
8338
9552
  }
8339
9553
  /** Undo a sealed session (source and data) as a new sealed session. */
8340
9554
  rollback(slug, input) {
8341
- return apiRequest(this.options, "POST", `${WORLDS}/${encodeURIComponent(slug)}/edits/rollback`, input);
9555
+ return apiRequest(this.options, "POST", `${WORLDS2}/${encodeURIComponent(slug)}/edits/rollback`, input);
8342
9556
  }
8343
9557
  };
8344
9558
  TASK_EDITS = "/api/public/world-task-edits";
@@ -8403,7 +9617,7 @@ var init_dryRun = __esm({
8403
9617
  /** Poll until the run settles or `timeoutSeconds` pass; the last verdict either way. */
8404
9618
  async settle(runId, opts) {
8405
9619
  const now = opts.now ?? Date.now;
8406
- const sleep2 = opts.sleep ?? ((ms) => new Promise((resolve7) => setTimeout(resolve7, ms)));
9620
+ const sleep4 = opts.sleep ?? ((ms) => new Promise((resolve7) => setTimeout(resolve7, ms)));
8407
9621
  const deadline = now() + opts.timeoutSeconds * 1e3;
8408
9622
  for (; ; ) {
8409
9623
  const budget = Math.floor(
@@ -8413,7 +9627,7 @@ var init_dryRun = __esm({
8413
9627
  const verdict = await this.status(runId, budget);
8414
9628
  if (verdict.settled || now() >= deadline) return verdict;
8415
9629
  opts.onProgress?.(verdict);
8416
- if (now() - asked < 1e3) await sleep2(1e3);
9630
+ if (now() - asked < 1e3) await sleep4(1e3);
8417
9631
  }
8418
9632
  }
8419
9633
  };
@@ -8481,6 +9695,8 @@ __export(worlds_exports, {
8481
9695
  EXPLORATORY_TASK: () => EXPLORATORY_TASK,
8482
9696
  EXPLORATORY_TASK_SOURCE: () => EXPLORATORY_TASK_SOURCE,
8483
9697
  GatewayEvalsClient: () => GatewayEvalsClient,
9698
+ GradePayload: () => GradePayload,
9699
+ GradeRefused: () => GradeRefused,
8484
9700
  HostedDryRunClient: () => HostedDryRunClient,
8485
9701
  IDLE_WARNING_MINUTES: () => IDLE_WARNING_MINUTES,
8486
9702
  KEY_NAMES: () => KEY_NAMES,
@@ -8490,52 +9706,80 @@ __export(worlds_exports, {
8490
9706
  NativeComputer: () => NativeComputer,
8491
9707
  OpenAIComputer: () => OpenAIComputer,
8492
9708
  OpenAIPreviewComputer: () => OpenAIPreviewComputer,
9709
+ PLATFORM_SESSION_HEADER: () => PLATFORM_SESSION_HEADER,
8493
9710
  PLATFORM_TIMEOUT_DEFAULT_MS: () => PLATFORM_TIMEOUT_DEFAULT_MS,
8494
9711
  PLATFORM_TIMEOUT_ENV: () => PLATFORM_TIMEOUT_ENV,
8495
9712
  PendingSafetyChecks: () => PendingSafetyChecks,
8496
9713
  PlatformTransport: () => PlatformTransport,
8497
9714
  PushSlugConflict: () => PushSlugConflict,
8498
9715
  RECOVER_BUDGET_MS: () => RECOVER_BUDGET_MS,
9716
+ REQUESTS_NOT_RECORDED: () => REQUESTS_NOT_RECORDED,
9717
+ REQUEST_BODY_CAPTURE_BYTES: () => REQUEST_BODY_CAPTURE_BYTES,
9718
+ REQUEST_LOG_PAGE_MAX: () => REQUEST_LOG_PAGE_MAX,
9719
+ RetryPolicy: () => RetryPolicy,
9720
+ SANDBOX_EXEC_REPORT_MARGIN_MS: () => SANDBOX_EXEC_REPORT_MARGIN_MS,
9721
+ SANDBOX_EXEC_START_ATTEMPTS: () => SANDBOX_EXEC_START_ATTEMPTS,
9722
+ SANDBOX_EXEC_TIMEOUT_MAX_S: () => SANDBOX_EXEC_TIMEOUT_MAX_S,
9723
+ SANDBOX_EXEC_WAIT_MS: () => SANDBOX_EXEC_WAIT_MS,
9724
+ SANDBOX_FILE_MAX_BYTES: () => SANDBOX_FILE_MAX_BYTES,
8499
9725
  SANDBOX_REQUEST_TIMEOUT_MS: () => SANDBOX_REQUEST_TIMEOUT_MS,
9726
+ SANDBOX_WRITE_CHUNK_BYTES: () => SANDBOX_WRITE_CHUNK_BYTES,
9727
+ SANDBOX_WRITE_SKIPPED: () => SANDBOX_WRITE_SKIPPED,
8500
9728
  SECRET_KEY: () => SECRET_KEY,
9729
+ SESSION_AUTH_MODES: () => SESSION_AUTH_MODES,
8501
9730
  SESSION_STATES: () => SESSION_STATES,
8502
9731
  SPAN_PREFIX: () => SPAN_PREFIX,
8503
9732
  STATUS_WAIT_CAP_SECONDS: () => STATUS_WAIT_CAP_SECONDS,
8504
9733
  STRIP_PREFIX: () => STRIP_PREFIX,
8505
9734
  SURFACE_KINDS: () => SURFACE_KINDS,
9735
+ Score: () => Score,
8506
9736
  Screen: () => Screen,
8507
9737
  SecretScrubber: () => SecretScrubber,
9738
+ TOOL_CONTRACT_UNREAD: () => TOOL_CONTRACT_UNREAD,
8508
9739
  TRANSFORMS: () => TRANSFORMS,
8509
9740
  TaskEditClient: () => TaskEditClient,
9741
+ TaskGrades: () => TaskGrades,
8510
9742
  TaskRun: () => TaskRun,
8511
9743
  TaskSessions: () => TaskSessions,
8512
9744
  TransportCause: () => TransportCause,
8513
9745
  UnreachableError: () => UnreachableError,
8514
9746
  VERIFIER_REWARD_PREFIX: () => VERIFIER_REWARD_PREFIX,
8515
9747
  WHITESPACE: () => WHITESPACE,
9748
+ WORLD_REQUEST_VIAS: () => WORLD_REQUEST_VIAS,
8516
9749
  WorldBrowser: () => WorldBrowser,
8517
9750
  WorldCallTooLarge: () => WorldCallTooLarge,
8518
9751
  WorldEditClient: () => WorldEditClient,
9752
+ WorldNotice: () => WorldNotice,
9753
+ WorldRequestFilterError: () => WorldRequestFilterError,
9754
+ WorldRequestLedger: () => WorldRequestLedger,
8519
9755
  WorldRequestLog: () => WorldRequestLog,
9756
+ WorldRequestQuery: () => WorldRequestQuery,
9757
+ WorldRequestsNotRecorded: () => WorldRequestsNotRecorded,
8520
9758
  WorldSandboxClient: () => WorldSandboxClient,
8521
9759
  WorldSession: () => WorldSession,
8522
9760
  WorldSessionCallError: () => WorldSessionCallError,
9761
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
8523
9762
  WorldSessionTimeout: () => WorldSessionTimeout,
8524
9763
  WorldSet: () => WorldSet,
8525
9764
  WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
8526
9765
  WorldTask: () => WorldTask,
9766
+ WorldToolsUnread: () => WorldToolsUnread,
9767
+ WriteTreeFilter: () => WriteTreeFilter,
8527
9768
  aggregateTaskGrade: () => aggregateTaskGrade,
8528
9769
  apiRequest: () => apiRequest,
8529
9770
  applyTransforms: () => applyTransforms,
8530
9771
  archiveMode: () => archiveMode,
8531
9772
  assertBrowserTier: () => assertBrowserTier,
9773
+ assertSessionAuthMode: () => assertSessionAuthMode,
8532
9774
  assertTaskRef: () => assertTaskRef,
8533
9775
  attachManifest: () => attachManifest,
8534
9776
  attachSession: () => attachSession,
8535
9777
  attachTask: () => attachTask,
8536
9778
  buildFileManifest: () => buildFileManifest,
8537
9779
  buildSourceTarball: () => buildSourceTarball,
9780
+ capabilityLabel: () => capabilityLabel,
8538
9781
  captureToolCalls: () => captureToolCalls,
9782
+ carriedWarnings: () => carriedWarnings,
8539
9783
  checkoutIdentity: () => checkoutIdentity,
8540
9784
  closeConnection: () => closeConnection,
8541
9785
  collectArchiveRelPaths: () => collectArchiveRelPaths,
@@ -8554,7 +9798,9 @@ __export(worlds_exports, {
8554
9798
  idleWarning: () => idleWarning,
8555
9799
  ingest: () => ingest,
8556
9800
  ingestToolCalls: () => ingestToolCalls,
9801
+ invalidFields: () => invalidFields,
8557
9802
  isBrowserGone: () => isBrowserGone,
9803
+ isSandboxCommitPending: () => isSandboxCommitPending,
8558
9804
  isSandboxImportPending: () => isSandboxImportPending,
8559
9805
  isTaskIdRef: () => isTaskIdRef,
8560
9806
  isTaskManifest: () => isTaskManifest,
@@ -8566,6 +9812,7 @@ __export(worlds_exports, {
8566
9812
  listTaskSessions: () => listTaskSessions,
8567
9813
  listTasks: () => listTasks,
8568
9814
  localTreeState: () => localTreeState,
9815
+ messageOf: () => messageOf,
8569
9816
  migrateProjectWorlds: () => migrateProjectWorlds,
8570
9817
  migrateWorld: () => migrateWorld,
8571
9818
  modelToolName: () => modelToolName,
@@ -8600,6 +9847,7 @@ __export(worlds_exports, {
8600
9847
  resolveBrowserMode: () => resolveBrowserMode,
8601
9848
  resolvePushSlug: () => resolvePushSlug,
8602
9849
  roundForm: () => roundForm,
9850
+ rowsRefusal: () => rowsRefusal,
8603
9851
  runSessions: () => runSessions,
8604
9852
  runSet: () => runSet,
8605
9853
  runTask: () => runTask,
@@ -8609,10 +9857,14 @@ __export(worlds_exports, {
8609
9857
  setEnvSecret: () => setEnvSecret,
8610
9858
  shapeText: () => shapeText,
8611
9859
  snapshotTreeHashes: () => snapshotTreeHashes,
9860
+ sqlRows: () => sqlRows,
9861
+ stillStartingHint: () => stillStartingHint,
8612
9862
  storedForms: () => storedForms,
8613
9863
  surfaceDescriptor: () => surfaceDescriptor,
8614
9864
  surfaceKinds: () => surfaceKinds,
9865
+ taskGradeError: () => taskGradeError,
8615
9866
  taskLabel: () => taskLabel,
9867
+ taskRunFailure: () => taskRunFailure,
8616
9868
  toolCallRecordsIn: () => toolCallRecordsIn,
8617
9869
  toolCallsFromObservations: () => toolCallsFromObservations,
8618
9870
  uiEntryUrl: () => uiEntryUrl,
@@ -8624,41 +9876,45 @@ __export(worlds_exports, {
8624
9876
  validateWorld: () => validateWorld,
8625
9877
  waitForRun: () => waitForRun,
8626
9878
  withoutUiToken: () => withoutUiToken,
9879
+ worldRequestsUnrecorded: () => worldRequestsUnrecorded,
8627
9880
  writeLocalMeta: () => writeLocalMeta
8628
9881
  });
8629
- function resolveEnv(options) {
8630
- const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
8631
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8632
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8633
- if (!publicKey || !secretKey) {
8634
- throw new Error(
8635
- "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey)."
8636
- );
8637
- }
8638
- return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
9882
+ function edgeAnswer(text2, status) {
9883
+ if (status !== void 0 && status >= 500) return EDGE_ANSWERS[status] ?? EDGE_FAILURE;
9884
+ return /^\s*</.test(text2) ? EDGE_PAGE : null;
8639
9885
  }
8640
- function parseErrorBody(text2, limit = 600) {
8641
- const raw = { detail: text2.slice(0, limit), code: null };
9886
+ function parseErrorBody(text2, status) {
9887
+ const raw = { detail: text2.slice(0, DETAIL_LIMIT), code: null };
8642
9888
  let parsed;
8643
9889
  try {
8644
9890
  parsed = JSON.parse(text2);
8645
9891
  } catch {
8646
- return raw;
9892
+ return { detail: (edgeAnswer(text2, status) ?? text2).slice(0, DETAIL_LIMIT), code: null };
8647
9893
  }
8648
9894
  if (parsed === null || typeof parsed !== "object") return raw;
8649
- const { message, code } = parsed;
9895
+ const { message, code, error } = parsed;
8650
9896
  const stableCode = typeof code === "string" ? code : null;
8651
9897
  if (typeof message !== "string") return { ...raw, code: stableCode };
8652
- if (stableCode === null)
8653
- return { detail: message.slice(0, limit), code: null };
9898
+ const fields = invalidFields(error);
9899
+ const said = fields ? `${message}: ${fields}` : message;
9900
+ if (stableCode === null) return { detail: said.slice(0, DETAIL_LIMIT), code: null };
8654
9901
  return {
8655
- detail: `${message} (${stableCode})`.slice(0, limit),
9902
+ detail: `${said} (${stableCode})`.slice(0, DETAIL_LIMIT),
8656
9903
  code: stableCode
8657
9904
  };
8658
9905
  }
8659
- async function apiRequest(options, method, path12, body, init2 = {}) {
8660
- const reply = await new PlatformTransport(resolveEnv(options)).send(method, path12, body, init2);
8661
- if (!reply.ok) throw new ApiError(reply.status, method, path12, reply.text);
9906
+ function invalidFields(error) {
9907
+ const issues = Array.isArray(error) ? error : [];
9908
+ return issues.filter(
9909
+ (issue) => issue !== null && typeof issue === "object" && typeof issue.message === "string"
9910
+ ).map((issue) => {
9911
+ const path13 = Array.isArray(issue.path) ? issue.path.map(String).join(".") : "";
9912
+ return path13 ? `${path13}: ${issue.message}` : issue.message;
9913
+ }).join("; ");
9914
+ }
9915
+ async function apiRequest(options, method, path13, body, init2 = {}) {
9916
+ const reply = await new PlatformTransport(requirePlatformEndpoint(options)).send(method, path13, body, init2);
9917
+ if (!reply.ok) throw new ApiError(reply.status, method, path13, reply.text);
8662
9918
  return JSON.parse(reply.text);
8663
9919
  }
8664
9920
  async function setEnvSecret(slug, key, value, options = {}) {
@@ -8770,7 +10026,6 @@ function detectProvenance() {
8770
10026
  kind: "ci",
8771
10027
  id: env.GITHUB_ACTOR,
8772
10028
  name: env.GITHUB_ACTOR,
8773
- // GitHub serves every account's avatar at /<login>.png.
8774
10029
  imageUrl: `${server}/${env.GITHUB_ACTOR}.png`
8775
10030
  };
8776
10031
  }
@@ -8835,16 +10090,19 @@ async function resolveWorld(slugRef, slug, ref, pin, options) {
8835
10090
  }
8836
10091
  return world;
8837
10092
  }
8838
- var ApiError, WorldTask, WorldSet;
10093
+ var ApiError, DETAIL_LIMIT, EDGE_ANSWERS, EDGE_FAILURE, EDGE_PAGE, WorldTask, WorldSet;
8839
10094
  var init_worlds = __esm({
8840
10095
  "src/worlds/index.ts"() {
10096
+ init_platformEndpoint();
8841
10097
  init_transport();
8842
10098
  init_transport();
8843
10099
  init_session2();
10100
+ init_grade();
8844
10101
  init_sessionList();
8845
10102
  init_sessionContract();
8846
10103
  init_taskRef();
8847
10104
  init_tasks();
10105
+ init_taskGrades();
8848
10106
  init_taskSessions();
8849
10107
  init_taskRun();
8850
10108
  init_setRun();
@@ -8853,6 +10111,7 @@ var init_worlds = __esm({
8853
10111
  init_browser();
8854
10112
  init_surfaceDescriptor();
8855
10113
  init_requestLog();
10114
+ init_requestLedger();
8856
10115
  init_browserReconnect();
8857
10116
  init_nativeComputer();
8858
10117
  init_hub();
@@ -8866,10 +10125,11 @@ var init_worlds = __esm({
8866
10125
  init_sandbox();
8867
10126
  init_editSessions();
8868
10127
  init_dryRun();
10128
+ init_worldNotice();
8869
10129
  ApiError = class extends Error {
8870
- constructor(status, method, path12, text2) {
8871
- const parsed = parseErrorBody(text2);
8872
- super(`${method} ${path12} -> ${status}: ${parsed.detail}`);
10130
+ constructor(status, method, path13, text2) {
10131
+ const parsed = parseErrorBody(text2, status);
10132
+ super(`${method} ${path13} -> ${status}: ${parsed.detail}`);
8873
10133
  this.status = status;
8874
10134
  this.text = text2;
8875
10135
  this.name = "ApiError";
@@ -8879,6 +10139,14 @@ var init_worlds = __esm({
8879
10139
  text;
8880
10140
  code;
8881
10141
  };
10142
+ DETAIL_LIMIT = 600;
10143
+ EDGE_ANSWERS = {
10144
+ 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.",
10145
+ 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.",
10146
+ 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."
10147
+ };
10148
+ 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.";
10149
+ 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.";
8882
10150
  WorldTask = class {
8883
10151
  constructor(world, name, variant) {
8884
10152
  this.world = world;
@@ -8914,12 +10182,7 @@ var init_worlds = __esm({
8914
10182
  }
8915
10183
  return hit;
8916
10184
  }
8917
- /**
8918
- * Open a LIVE session against one of this world's tasks — a bundled task's
8919
- * name or an inline `WorldTaskSpec`: the platform boots (or reuses) a warm
8920
- * container, and your agent calls its tools through the platform. See
8921
- * worlds/session.ts.
8922
- */
10185
+ /** Open a live session on a bundled task's name or an inline `WorldTaskSpec`; the platform boots or reuses a warm container. */
8923
10186
  async open(task, opts = {}) {
8924
10187
  const { openSession: openSession2 } = await Promise.resolve().then(() => (init_session2(), session_exports2));
8925
10188
  return openSession2(this.slug, {
@@ -8931,11 +10194,7 @@ var init_worlds = __esm({
8931
10194
  options: this.options
8932
10195
  });
8933
10196
  }
8934
- /**
8935
- * Start a HOSTED run of a run config against this world — the platform
8936
- * executes the (Python) world in parallel containers per the config's
8937
- * `parallelism` and files one graded run per model. Returns those runs.
8938
- */
10197
+ /** Start a hosted run of a run config on this version (or `opts.versionId`): one graded run per model, provenance detected unless given. */
8939
10198
  async dispatch(runConfigId, opts = {}) {
8940
10199
  const provenance = opts.provenance ?? detectProvenance();
8941
10200
  return apiRequest(
@@ -8984,6 +10243,8 @@ export {
8984
10243
  EXPLORATORY_TASK,
8985
10244
  EXPLORATORY_TASK_SOURCE,
8986
10245
  GatewayEvalsClient,
10246
+ GradePayload,
10247
+ GradeRefused,
8987
10248
  HostedDryRunClient,
8988
10249
  IDLE_WARNING_MINUTES,
8989
10250
  KEY_NAMES,
@@ -8993,52 +10254,80 @@ export {
8993
10254
  NativeComputer,
8994
10255
  OpenAIComputer,
8995
10256
  OpenAIPreviewComputer,
10257
+ PLATFORM_SESSION_HEADER,
8996
10258
  PLATFORM_TIMEOUT_DEFAULT_MS,
8997
10259
  PLATFORM_TIMEOUT_ENV,
8998
10260
  PendingSafetyChecks,
8999
10261
  PlatformTransport,
9000
10262
  PushSlugConflict,
9001
10263
  RECOVER_BUDGET_MS,
10264
+ REQUESTS_NOT_RECORDED,
10265
+ REQUEST_BODY_CAPTURE_BYTES,
10266
+ REQUEST_LOG_PAGE_MAX,
10267
+ RetryPolicy,
10268
+ SANDBOX_EXEC_REPORT_MARGIN_MS,
10269
+ SANDBOX_EXEC_START_ATTEMPTS,
10270
+ SANDBOX_EXEC_TIMEOUT_MAX_S,
10271
+ SANDBOX_EXEC_WAIT_MS,
10272
+ SANDBOX_FILE_MAX_BYTES,
9002
10273
  SANDBOX_REQUEST_TIMEOUT_MS,
10274
+ SANDBOX_WRITE_CHUNK_BYTES,
10275
+ SANDBOX_WRITE_SKIPPED,
9003
10276
  SECRET_KEY,
10277
+ SESSION_AUTH_MODES,
9004
10278
  SESSION_STATES,
9005
10279
  SPAN_PREFIX,
9006
10280
  STATUS_WAIT_CAP_SECONDS,
9007
10281
  STRIP_PREFIX,
9008
10282
  SURFACE_KINDS,
10283
+ Score,
9009
10284
  Screen,
9010
10285
  SecretScrubber,
10286
+ TOOL_CONTRACT_UNREAD,
9011
10287
  TRANSFORMS,
9012
10288
  TaskEditClient,
10289
+ TaskGrades,
9013
10290
  TaskRun,
9014
10291
  TaskSessions,
9015
10292
  TransportCause,
9016
10293
  UnreachableError,
9017
10294
  VERIFIER_REWARD_PREFIX,
9018
10295
  WHITESPACE,
10296
+ WORLD_REQUEST_VIAS,
9019
10297
  WorldBrowser,
9020
10298
  WorldCallTooLarge,
9021
10299
  WorldEditClient,
10300
+ WorldNotice,
10301
+ WorldRequestFilterError,
10302
+ WorldRequestLedger,
9022
10303
  WorldRequestLog,
10304
+ WorldRequestQuery,
10305
+ WorldRequestsNotRecorded,
9023
10306
  WorldSandboxClient,
9024
10307
  WorldSession,
9025
10308
  WorldSessionCallError,
10309
+ WorldSessionOpenFailed,
9026
10310
  WorldSessionTimeout,
9027
10311
  WorldSet,
9028
10312
  WorldSurfaceUnavailable,
9029
10313
  WorldTask,
10314
+ WorldToolsUnread,
10315
+ WriteTreeFilter,
9030
10316
  aggregateTaskGrade,
9031
10317
  apiRequest,
9032
10318
  applyTransforms,
9033
10319
  archiveMode,
9034
10320
  assertBrowserTier,
10321
+ assertSessionAuthMode,
9035
10322
  assertTaskRef,
9036
10323
  attachManifest,
9037
10324
  attachSession,
9038
10325
  attachTask,
9039
10326
  buildFileManifest,
9040
10327
  buildSourceTarball,
10328
+ capabilityLabel,
9041
10329
  captureToolCalls,
10330
+ carriedWarnings,
9042
10331
  checkoutIdentity,
9043
10332
  closeConnection,
9044
10333
  collectArchiveRelPaths,
@@ -9057,7 +10346,9 @@ export {
9057
10346
  idleWarning,
9058
10347
  ingest,
9059
10348
  ingestToolCalls,
10349
+ invalidFields,
9060
10350
  isBrowserGone,
10351
+ isSandboxCommitPending,
9061
10352
  isSandboxImportPending,
9062
10353
  isTaskIdRef,
9063
10354
  isTaskManifest,
@@ -9069,6 +10360,7 @@ export {
9069
10360
  listTaskSessions,
9070
10361
  listTasks,
9071
10362
  localTreeState,
10363
+ messageOf,
9072
10364
  migrateProjectWorlds,
9073
10365
  migrateWorld,
9074
10366
  modelToolName,
@@ -9103,6 +10395,7 @@ export {
9103
10395
  resolveBrowserMode,
9104
10396
  resolvePushSlug,
9105
10397
  roundForm,
10398
+ rowsRefusal,
9106
10399
  runSessions,
9107
10400
  runSet,
9108
10401
  runTask,
@@ -9112,10 +10405,14 @@ export {
9112
10405
  setEnvSecret,
9113
10406
  shapeText,
9114
10407
  snapshotTreeHashes,
10408
+ sqlRows,
10409
+ stillStartingHint,
9115
10410
  storedForms,
9116
10411
  surfaceDescriptor,
9117
10412
  surfaceKinds,
10413
+ taskGradeError,
9118
10414
  taskLabel,
10415
+ taskRunFailure,
9119
10416
  toolCallRecordsIn,
9120
10417
  toolCallsFromObservations,
9121
10418
  uiEntryUrl,
@@ -9127,6 +10424,7 @@ export {
9127
10424
  validateWorld,
9128
10425
  waitForRun,
9129
10426
  withoutUiToken,
10427
+ worldRequestsUnrecorded,
9130
10428
  writeLocalMeta
9131
10429
  };
9132
10430
  //# sourceMappingURL=index.mjs.map