@withgateway/sdk 0.3.0 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/README.md +6 -1
  2. package/dist/cli.js +3971 -2338
  3. package/dist/cli.js.map +1 -1
  4. package/dist/cli.mjs +3950 -2318
  5. package/dist/cli.mjs.map +1 -1
  6. package/dist/files/index.d.mts +1 -1
  7. package/dist/files/index.d.ts +1 -1
  8. package/dist/files/index.js +214 -41
  9. package/dist/files/index.js.map +1 -1
  10. package/dist/files/index.mjs +206 -35
  11. package/dist/files/index.mjs.map +1 -1
  12. package/dist/{index-DAb-FCMg.d.ts → index-DUHmS74p.d.ts} +3 -13
  13. package/dist/{index-D6oi1TWE.d.mts → index-mJhG3vZU.d.mts} +3 -13
  14. package/dist/index.d.mts +4 -4
  15. package/dist/index.d.ts +4 -4
  16. package/dist/index.js +1456 -564
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +1433 -557
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/{media-CbcAfQvd.d.mts → media-BIApiPzS.d.mts} +21 -2
  21. package/dist/{media-CbcAfQvd.d.ts → media-BIApiPzS.d.ts} +21 -2
  22. package/dist/traces/index.d.mts +1 -1
  23. package/dist/traces/index.d.ts +1 -1
  24. package/dist/traces/index.js +211 -38
  25. package/dist/traces/index.js.map +1 -1
  26. package/dist/traces/index.mjs +203 -32
  27. package/dist/traces/index.mjs.map +1 -1
  28. package/dist/tracing/index.d.mts +2 -2
  29. package/dist/tracing/index.d.ts +2 -2
  30. package/dist/tracing/index.js +96 -21
  31. package/dist/tracing/index.js.map +1 -1
  32. package/dist/tracing/index.mjs +96 -21
  33. package/dist/tracing/index.mjs.map +1 -1
  34. package/dist/worlds/index.d.mts +300 -123
  35. package/dist/worlds/index.d.ts +300 -123
  36. package/dist/worlds/index.js +1385 -508
  37. package/dist/worlds/index.js.map +1 -1
  38. package/dist/worlds/index.mjs +1377 -501
  39. package/dist/worlds/index.mjs.map +1 -1
  40. package/package.json +1 -1
  41. package/runtime/gatewaysdk/environment/_bundle.py +2 -27
  42. package/runtime/gatewaysdk/environment/_materialize.py +49 -69
  43. package/runtime/gatewaysdk/environment/_world.py +122 -197
  44. package/runtime/gatewaysdk/environment/backing.py +137 -0
  45. package/runtime/gatewaysdk/environment/core.py +1 -1
  46. package/runtime/gatewaysdk/environment/legacy_sqlite.py +301 -0
  47. package/runtime/gatewaysdk/environment/reward_keys.py +25 -0
  48. package/runtime/gatewaysdk/environment/runtime.py +8 -13
  49. package/runtime/gatewaysdk/environment/schema/__init__.py +4 -4
  50. package/runtime/gatewaysdk/environment/schema/admin_routes.py +86 -0
  51. package/runtime/gatewaysdk/environment/schema/api.py +266 -515
  52. package/runtime/gatewaysdk/environment/schema/batch.py +42 -37
  53. package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +17 -5
  54. package/runtime/gatewaysdk/environment/schema/call_log.py +291 -0
  55. package/runtime/gatewaysdk/environment/schema/call_redaction.py +344 -0
  56. package/runtime/gatewaysdk/environment/schema/cast.py +31 -4
  57. package/runtime/gatewaysdk/environment/schema/compiler.py +334 -158
  58. package/runtime/gatewaysdk/environment/schema/conform.py +8 -12
  59. package/runtime/gatewaysdk/environment/schema/connector.py +27 -62
  60. package/runtime/gatewaysdk/environment/schema/data.py +50 -207
  61. package/runtime/gatewaysdk/environment/schema/database/__init__.py +24 -0
  62. package/runtime/gatewaysdk/environment/schema/database/audit.py +121 -0
  63. package/runtime/gatewaysdk/environment/schema/database/base.py +995 -0
  64. package/runtime/gatewaysdk/environment/schema/database/budget.py +33 -0
  65. package/runtime/gatewaysdk/environment/schema/database/connections.py +101 -0
  66. package/runtime/gatewaysdk/environment/schema/database/engines.py +8 -0
  67. package/runtime/gatewaysdk/environment/schema/database/guard.py +208 -0
  68. package/runtime/gatewaysdk/environment/schema/database/ingest.py +154 -0
  69. package/runtime/gatewaysdk/environment/schema/database/matching.py +58 -0
  70. package/runtime/gatewaysdk/environment/schema/database/provenance.py +30 -0
  71. package/runtime/gatewaysdk/environment/schema/database/rows.py +53 -0
  72. package/runtime/gatewaysdk/environment/schema/database/runtime_rows.py +39 -0
  73. package/runtime/gatewaysdk/environment/schema/database/sqlite.py +367 -0
  74. package/runtime/gatewaysdk/environment/schema/domain_migration.py +236 -0
  75. package/runtime/gatewaysdk/environment/schema/envelope.py +169 -0
  76. package/runtime/gatewaysdk/environment/schema/extract.py +6 -13
  77. package/runtime/gatewaysdk/environment/schema/grading.py +45 -30
  78. package/runtime/gatewaysdk/environment/schema/handler_errors.py +142 -12
  79. package/runtime/gatewaysdk/environment/schema/host.py +158 -250
  80. package/runtime/gatewaysdk/environment/schema/lanes.py +53 -0
  81. package/runtime/gatewaysdk/environment/schema/local.py +44 -71
  82. package/runtime/gatewaysdk/environment/schema/mapping.py +5 -25
  83. package/runtime/gatewaysdk/environment/schema/openapi_import.py +112 -73
  84. package/runtime/gatewaysdk/environment/schema/openapi_paging.py +147 -0
  85. package/runtime/gatewaysdk/environment/schema/openapi_payloads.py +288 -0
  86. package/runtime/gatewaysdk/environment/schema/openapi_routes.py +97 -102
  87. package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +113 -30
  88. package/runtime/gatewaysdk/environment/schema/platform.py +124 -178
  89. package/runtime/gatewaysdk/environment/schema/platform_import.py +2 -3
  90. package/runtime/gatewaysdk/environment/schema/platform_query.py +61 -5
  91. package/runtime/gatewaysdk/environment/schema/primary_key.py +61 -0
  92. package/runtime/gatewaysdk/environment/schema/proof.py +19 -12
  93. package/runtime/gatewaysdk/environment/schema/runtime.py +77 -160
  94. package/runtime/gatewaysdk/environment/schema/served_names.py +58 -0
  95. package/runtime/gatewaysdk/environment/schema/session_files.py +166 -0
  96. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +2 -2
  97. package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +22 -14
  98. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +29 -3
  99. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +16 -16
  100. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +18 -18
  101. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +3 -2
  102. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +15 -0
  103. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/openapi-import.md +50 -21
  104. package/runtime/gatewaysdk/environment/schema/slack.py +4 -8
  105. package/runtime/gatewaysdk/environment/schema/snapshot.py +385 -129
  106. package/runtime/gatewaysdk/environment/schema/store_contract.py +8 -14
  107. package/runtime/gatewaysdk/environment/schema/tasks.py +140 -55
  108. package/runtime/gatewaysdk/environment/schema/tools_world.py +110 -87
  109. package/runtime/gatewaysdk/environment/schema/ui.py +24 -5
  110. package/runtime/gatewaysdk/environment/schema/ui_static/app.js +100 -130
  111. package/runtime/gatewaysdk/environment/schema/ui_static/index.html +1 -8
  112. package/runtime/gatewaysdk/environment/schema/validation.py +166 -40
  113. package/runtime/gatewaysdk/environment/schema/workers.py +142 -57
  114. package/runtime/gatewaysdk/environment/schema/world_sql.py +81 -0
  115. package/runtime/gatewaysdk/environment/schema/world_tests.py +71 -39
  116. package/runtime/gatewaysdk/environment/score.py +57 -0
  117. package/runtime/gatewaysdk/environment/secret_values.py +36 -0
  118. package/runtime/gatewaysdk/environment/world.py +42 -3
  119. package/runtime/gatewaysdk/environment/world_session_contract.py +32 -5
  120. package/skills/benchmarks-getting-started/SKILL.md +2 -2
  121. package/skills/connector-schema-authoring/SKILL.md +22 -14
  122. package/skills/connector-schema-authoring/connector-worlds.md +26 -10
  123. package/skills/connector-schema-authoring/world-schema.md +111 -51
  124. package/skills/world-data-ingestion/ROWS.md +29 -3
  125. package/skills/world-data-ingestion/SKILL.md +16 -16
  126. package/skills/worlds-getting-started/SKILL.md +18 -18
  127. package/skills/worlds-getting-started/references/cli-mcp-map.md +3 -2
  128. package/skills/worlds-getting-started/references/gotchas.md +15 -0
  129. package/skills/worlds-getting-started/references/openapi-import.md +50 -21
  130. package/runtime/gatewaysdk/environment/schema/store.py +0 -1013
@@ -42,6 +42,28 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
42
42
  ));
43
43
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
44
44
 
45
+ // src/worlds/platformEndpoint.ts
46
+ function platformEndpoint(options) {
47
+ const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
48
+ const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
49
+ if (!publicKey || !secretKey) return null;
50
+ const host = options.host ?? process.env.GATEWAY_HOST ?? DEFAULT_HOST;
51
+ return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
52
+ }
53
+ function requirePlatformEndpoint(options) {
54
+ const endpoint = platformEndpoint(options);
55
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
56
+ return endpoint;
57
+ }
58
+ var DEFAULT_HOST, GATEWAY_KEYS_MISSING;
59
+ var init_platformEndpoint = __esm({
60
+ "src/worlds/platformEndpoint.ts"() {
61
+ "use strict";
62
+ DEFAULT_HOST = "https://withgateway.ai";
63
+ GATEWAY_KEYS_MISSING = "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).";
64
+ }
65
+ });
66
+
45
67
  // src/worlds/transport.ts
46
68
  function platformTimeoutMs(env = process.env) {
47
69
  const raw = env[PLATFORM_TIMEOUT_ENV];
@@ -50,7 +72,7 @@ function platformTimeoutMs(env = process.env) {
50
72
  if (Number.isInteger(value) && value > 0) return value;
51
73
  throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
52
74
  }
53
- var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, PlatformTransport;
75
+ var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, sleep, RetryPolicy, Attempt, PlatformTransport;
54
76
  var init_transport = __esm({
55
77
  "src/worlds/transport.ts"() {
56
78
  "use strict";
@@ -112,6 +134,56 @@ var init_transport = __esm({
112
134
  route;
113
135
  failure;
114
136
  };
137
+ sleep = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
138
+ RetryPolicy = class _RetryPolicy {
139
+ constructor(delaysMs, wait = sleep, budgetMs = Number.POSITIVE_INFINITY, now = Date.now) {
140
+ this.delaysMs = delaysMs;
141
+ this.wait = wait;
142
+ this.budgetMs = budgetMs;
143
+ this.now = now;
144
+ }
145
+ delaysMs;
146
+ wait;
147
+ budgetMs;
148
+ now;
149
+ static NONE = new _RetryPolicy([]);
150
+ /** A platform restart or a short edge outage: no wait starts that would end past five minutes, so a step ends
151
+ * within the budget plus one request timeout. */
152
+ static FILING = new _RetryPolicy([250, 500, 1e3, 2e3, 4e3, 8e3, 16e3, 3e4], sleep, 3e5);
153
+ static TRANSIENT_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
154
+ async run(attempt) {
155
+ const started = this.now();
156
+ for (let tries = 0; ; tries += 1) {
157
+ const outcome = await Attempt.of(attempt);
158
+ const delayMs = this.delaysMs[tries];
159
+ const spent = delayMs === void 0 || this.now() - started + delayMs >= this.budgetMs;
160
+ if (!outcome.transient || spent) return outcome.settle();
161
+ await this.wait(delayMs);
162
+ }
163
+ }
164
+ };
165
+ Attempt = class _Attempt {
166
+ constructor(reply, error, transient) {
167
+ this.reply = reply;
168
+ this.error = error;
169
+ this.transient = transient;
170
+ }
171
+ reply;
172
+ error;
173
+ transient;
174
+ static async of(attempt) {
175
+ try {
176
+ const reply = await attempt();
177
+ return new _Attempt(reply, null, RetryPolicy.TRANSIENT_STATUSES.has(reply.status));
178
+ } catch (error) {
179
+ return new _Attempt(null, error, error instanceof UnreachableError);
180
+ }
181
+ }
182
+ settle() {
183
+ if (this.reply) return this.reply;
184
+ throw this.error;
185
+ }
186
+ };
115
187
  PlatformTransport = class {
116
188
  constructor(endpoint, timeoutMs = platformTimeoutMs()) {
117
189
  this.endpoint = endpoint;
@@ -125,6 +197,9 @@ var init_transport = __esm({
125
197
  }
126
198
  async send(method, route, body, opts = {}) {
127
199
  const timeoutMs = this.budget(opts.timeoutMs);
200
+ return (opts.retry ?? RetryPolicy.NONE).run(() => this.once(method, route, body, timeoutMs));
201
+ }
202
+ async once(method, route, body, timeoutMs) {
128
203
  const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
129
204
  const controller = new AbortController();
130
205
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -318,9 +393,9 @@ function surfaceKinds(s) {
318
393
  if (s.browser) kinds.push("browser");
319
394
  return kinds;
320
395
  }
321
- function uiUrl(ui, path12) {
396
+ function uiUrl(ui, path13) {
322
397
  const base = ui.url.replace(/\/+$/, "");
323
- const rel = path12.startsWith("/") ? path12 : `/${path12}`;
398
+ const rel = path13.startsWith("/") ? path13 : `/${path13}`;
324
399
  if (!ui.token) return base + rel;
325
400
  const [beforeHash, hash] = splitHash(rel);
326
401
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -561,7 +636,7 @@ var init_browser = __esm({
561
636
  }
562
637
  async send(batch) {
563
638
  const retries = this.opts.maxRetries ?? EVENT_MAX_RETRIES;
564
- const sleep2 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
639
+ const sleep4 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
565
640
  for (let attempt = 0; attempt < retries; attempt++) {
566
641
  try {
567
642
  const base = await this.resolveBase();
@@ -578,7 +653,7 @@ var init_browser = __esm({
578
653
  );
579
654
  return;
580
655
  }
581
- await sleep2(500 * 2 ** attempt);
656
+ await sleep4(500 * 2 ** attempt);
582
657
  }
583
658
  }
584
659
  }
@@ -1215,13 +1290,13 @@ var init_screen = __esm({
1215
1290
  });
1216
1291
  }
1217
1292
  async drag(action) {
1218
- const path12 = action.args.path ?? [];
1219
- if (path12.length < 2) throw new Error("a drag needs at least two points");
1293
+ const path13 = action.args.path ?? [];
1294
+ if (path13.length < 2) throw new Error("a drag needs at least two points");
1220
1295
  return this.held(action.args.modifiers ?? [], async () => {
1221
- await this.moveTo(path12[0]);
1296
+ await this.moveTo(path13[0]);
1222
1297
  await this.page.mouse.down({ button: "left" });
1223
1298
  try {
1224
- for (const point2 of path12.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1299
+ for (const point2 of path13.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1225
1300
  } finally {
1226
1301
  await this.page.mouse.up({ button: "left" });
1227
1302
  }
@@ -1312,7 +1387,7 @@ var init_media = __esm({
1312
1387
  put;
1313
1388
  maxBytes;
1314
1389
  /** The uploader for `GATEWAY_HOST` / `GATEWAY_PUBLIC_KEY` /
1315
- * `GATEWAY_SECRET_KEY` (the keys tracing exports with); null when any is unset. */
1390
+ * `GATEWAY_SECRET_KEY` only; null when any is unset, so a run traced with a saved login uploads none. */
1316
1391
  static fromEnv(env = process.env) {
1317
1392
  const host = env.GATEWAY_HOST;
1318
1393
  const publicKey = env.GATEWAY_PUBLIC_KEY;
@@ -2182,7 +2257,7 @@ function idleWarning(idleExpiresAt, now = /* @__PURE__ */ new Date()) {
2182
2257
  const minutes = Math.max(0, Math.floor(leftMs / 6e4));
2183
2258
  return `warning: session expires in ${minutes}m (idle TTL); call a tool to extend it or \`gateway worlds session pin\` to keep it`;
2184
2259
  }
2185
- var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge;
2260
+ var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge, stillStartingHint;
2186
2261
  var init_sessionContract = __esm({
2187
2262
  "src/worlds/sessionContract.ts"() {
2188
2263
  "use strict";
@@ -2218,6 +2293,65 @@ var init_sessionContract = __esm({
2218
2293
  };
2219
2294
  }
2220
2295
  };
2296
+ stillStartingHint = (sessionId) => `It keeps opening: poll \`gateway worlds session status ${sessionId}\` until ready, or end it with \`gateway worlds session close ${sessionId}\`.`;
2297
+ }
2298
+ });
2299
+
2300
+ // src/worlds/sessionReadiness.ts
2301
+ var READY_TIMEOUT_MS, READY_POLL_MS, sleep2, WorldSessionTimeout, WorldSessionCallError, WorldSessionOpenFailed, ENDED, SessionReadiness;
2302
+ var init_sessionReadiness = __esm({
2303
+ "src/worlds/sessionReadiness.ts"() {
2304
+ "use strict";
2305
+ READY_TIMEOUT_MS = 10 * 6e4;
2306
+ READY_POLL_MS = 2e3;
2307
+ sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
2308
+ WorldSessionTimeout = class extends Error {
2309
+ };
2310
+ WorldSessionCallError = class extends Error {
2311
+ };
2312
+ WorldSessionOpenFailed = class extends WorldSessionCallError {
2313
+ constructor(sessionId, state2, code, reason) {
2314
+ super(
2315
+ reason === null ? `World session ${sessionId} ${state2.toLowerCase()} before it was ready.` : `World session ${sessionId} failed to open: ${reason}${code ? ` (${code})` : ""}`
2316
+ );
2317
+ this.sessionId = sessionId;
2318
+ this.state = state2;
2319
+ this.code = code;
2320
+ this.name = "WorldSessionOpenFailed";
2321
+ }
2322
+ sessionId;
2323
+ state;
2324
+ code;
2325
+ };
2326
+ ENDED = /* @__PURE__ */ new Set(["FAILED", "STOPPED"]);
2327
+ SessionReadiness = class {
2328
+ constructor(sessionId, status) {
2329
+ this.sessionId = sessionId;
2330
+ this.status = status;
2331
+ }
2332
+ sessionId;
2333
+ status;
2334
+ async wait(timeoutMs = READY_TIMEOUT_MS) {
2335
+ const deadline = Date.now() + timeoutMs;
2336
+ for (; ; ) {
2337
+ const state2 = await this.status();
2338
+ if (state2.ready) return state2;
2339
+ if (ENDED.has(state2.state))
2340
+ throw new WorldSessionOpenFailed(
2341
+ this.sessionId,
2342
+ state2.state,
2343
+ state2.failure?.code ?? null,
2344
+ state2.failure?.message ?? null
2345
+ );
2346
+ const left = deadline - Date.now();
2347
+ if (left <= 0)
2348
+ throw new WorldSessionTimeout(
2349
+ `World session ${this.sessionId} still ${state2.state} after ${Math.round(timeoutMs / 1e3)}s.`
2350
+ );
2351
+ await sleep2(Math.min(READY_POLL_MS, left));
2352
+ }
2353
+ }
2354
+ };
2221
2355
  }
2222
2356
  });
2223
2357
 
@@ -2313,11 +2447,184 @@ var init_taskRef = __esm({
2313
2447
  }
2314
2448
  });
2315
2449
 
2450
+ // src/worlds/worldNotice.ts
2451
+ var TOOL_CONTRACT_UNREAD, DEPRECATION, NOTICE_KINDS, WorldNotice, WorldToolsUnread;
2452
+ var init_worldNotice = __esm({
2453
+ "src/worlds/worldNotice.ts"() {
2454
+ "use strict";
2455
+ TOOL_CONTRACT_UNREAD = "tool-contract-unread";
2456
+ DEPRECATION = { label: "deprecated", next: "Instead", refusesRun: false };
2457
+ NOTICE_KINDS = /* @__PURE__ */ new Map([
2458
+ [TOOL_CONTRACT_UNREAD, { label: "warning", next: "Then", refusesRun: true }]
2459
+ ]);
2460
+ WorldNotice = class _WorldNotice {
2461
+ constructor(notice, kind) {
2462
+ this.notice = notice;
2463
+ this.kind = kind;
2464
+ }
2465
+ notice;
2466
+ kind;
2467
+ static of(notice) {
2468
+ return new _WorldNotice(notice, NOTICE_KINDS.get(notice.code) ?? DEPRECATION);
2469
+ }
2470
+ get refusesRun() {
2471
+ return this.kind.refusesRun;
2472
+ }
2473
+ line() {
2474
+ const { code, message, replacement } = this.notice;
2475
+ return `${this.kind.label} (${code}): ${message} ${this.kind.next}: ${replacement}`;
2476
+ }
2477
+ };
2478
+ WorldToolsUnread = class _WorldToolsUnread extends Error {
2479
+ constructor(world, notice) {
2480
+ super(
2481
+ `refused before the agent ran, nothing filed: world ${world} has no readable tools: ${notice.notice.message} Then: ${notice.notice.replacement}`
2482
+ );
2483
+ this.world = world;
2484
+ this.notice = notice;
2485
+ this.name = "WorldToolsUnread";
2486
+ this.code = notice.notice.code;
2487
+ }
2488
+ world;
2489
+ notice;
2490
+ code;
2491
+ static refuse(world, notices) {
2492
+ const refusing = notices.map((notice) => WorldNotice.of(notice)).find((notice) => notice.refusesRun);
2493
+ if (refusing) throw new _WorldToolsUnread(world, refusing);
2494
+ }
2495
+ };
2496
+ }
2497
+ });
2498
+
2499
+ // src/worlds/grade.ts
2500
+ var messageOf, Score, GradeRefused, GradePayload;
2501
+ var init_grade = __esm({
2502
+ "src/worlds/grade.ts"() {
2503
+ "use strict";
2504
+ messageOf = (error) => (error instanceof Error ? error.message || error.name : String(error)) || "Error";
2505
+ Score = class _Score {
2506
+ static RULE = "a score is a number from 0 to 1";
2507
+ static admits(value) {
2508
+ return Number.isFinite(value) && value >= 0 && value <= 1;
2509
+ }
2510
+ /** Every number among `scores` that is not a score, as `'<key>' = <value>`
2511
+ * and the rule, or null when all of them are. Pairs, not a map, so that no
2512
+ * key hides another. */
2513
+ static problem(scores2) {
2514
+ const refused = [...scores2].flatMap(
2515
+ ([key, value]) => typeof value === "number" && !_Score.admits(value) ? [`'${key}' = ${String(value)}`] : []
2516
+ );
2517
+ return refused.length ? `${refused.join(", ")}; ${_Score.RULE}` : null;
2518
+ }
2519
+ };
2520
+ GradeRefused = class extends Error {
2521
+ name = "GradeRefused";
2522
+ };
2523
+ GradePayload = class _GradePayload {
2524
+ /** A grade payload's numbers, checked: every number in it must be a score,
2525
+ * and its rewards and verifiers objects by name, or the grade is refused; a
2526
+ * cross-world verifier's grade that breaks either rule is that verifier's
2527
+ * error, the way the World Host answers one that could not grade. */
2528
+ static read(raw) {
2529
+ if (!_GradePayload.objectOrAbsent(raw))
2530
+ throw new GradeRefused("the grade is not an object");
2531
+ const record = _GradePayload.record(raw);
2532
+ const problem = _GradePayload.problem(record) ?? _GradePayload.verifiersProblem(record.verifiers);
2533
+ if (problem) throw new GradeRefused(problem);
2534
+ const verifiers = _GradePayload.verifiers(record.verifiers);
2535
+ return {
2536
+ ..._GradePayload.numbers(record),
2537
+ ...typeof record.reason === "string" ? { reason: record.reason } : {},
2538
+ ...typeof record.error === "string" ? { error: record.error } : {},
2539
+ ...verifiers ? { verifiers } : {}
2540
+ };
2541
+ }
2542
+ /** A world whose grade call failed (the platform refused it, the host had
2543
+ * lost the session): that world's error, never a score. */
2544
+ static failedCall(error) {
2545
+ return { ..._GradePayload.failed(messageOf(error)), raw: null };
2546
+ }
2547
+ static problem(record) {
2548
+ if (!_GradePayload.objectOrAbsent(record.rewards))
2549
+ return "the grade's rewards are not an object";
2550
+ const scores2 = Score.problem(_GradePayload.scores(record));
2551
+ if (scores2) return `the grade answered ${scores2}`;
2552
+ const twice = _GradePayload.twice(record);
2553
+ return twice.length ? `the grade answered ${twice.map((key) => `'${key}'`).join(", ")} both beside and inside rewards` : null;
2554
+ }
2555
+ static twice(record) {
2556
+ return Object.entries(_GradePayload.record(record.rewards)).filter(
2557
+ ([key, value]) => key !== "reward" && typeof value === "number" && typeof record[key] === "number"
2558
+ ).map(([key]) => key);
2559
+ }
2560
+ static verifiersProblem(value) {
2561
+ return _GradePayload.objectOrAbsent(value) ? null : "the grade's verifiers are not an object";
2562
+ }
2563
+ static scores(record) {
2564
+ return [
2565
+ ["reward", record.reward],
2566
+ ...Object.entries(_GradePayload.record(record.rewards)),
2567
+ ...Object.entries(record).filter(
2568
+ ([key]) => key !== "reward" && key !== "rewards"
2569
+ )
2570
+ ];
2571
+ }
2572
+ static isRecord(value) {
2573
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2574
+ }
2575
+ static objectOrAbsent(value) {
2576
+ return value === void 0 || value === null || _GradePayload.isRecord(value);
2577
+ }
2578
+ static record(value) {
2579
+ return _GradePayload.isRecord(value) ? value : {};
2580
+ }
2581
+ /** `reward`, and every numeric key under `rewards` (the host's shape) or at
2582
+ * the top level (a bundled verifier's). */
2583
+ static numbers(record) {
2584
+ const rewards = {};
2585
+ for (const [key, value] of Object.entries(
2586
+ _GradePayload.record(record.rewards)
2587
+ ))
2588
+ if (typeof value === "number") rewards[key] = value;
2589
+ for (const [key, value] of Object.entries(record))
2590
+ if (key !== "reward" && typeof value === "number") rewards[key] = value;
2591
+ return {
2592
+ reward: typeof record.reward === "number" ? record.reward : null,
2593
+ rewards
2594
+ };
2595
+ }
2596
+ static verifiers(value) {
2597
+ if (!_GradePayload.isRecord(value)) return void 0;
2598
+ return Object.fromEntries(
2599
+ Object.entries(value).map(([name, grade]) => [
2600
+ name,
2601
+ _GradePayload.verifier(name, grade)
2602
+ ])
2603
+ );
2604
+ }
2605
+ static verifier(name, grade) {
2606
+ if (!_GradePayload.isRecord(grade))
2607
+ return _GradePayload.failed(`the grade's verifier '${name}' is not an object`);
2608
+ const problem = _GradePayload.problem(grade);
2609
+ if (problem) return _GradePayload.failed(problem);
2610
+ return {
2611
+ ..._GradePayload.numbers(grade),
2612
+ ...typeof grade.error === "string" ? { error: grade.error } : {}
2613
+ };
2614
+ }
2615
+ static failed(error) {
2616
+ return { reward: null, rewards: {}, error };
2617
+ }
2618
+ };
2619
+ }
2620
+ });
2621
+
2316
2622
  // src/worlds/session.ts
2317
2623
  var session_exports2 = {};
2318
2624
  __export(session_exports2, {
2319
2625
  WorldSession: () => WorldSession,
2320
2626
  WorldSessionCallError: () => WorldSessionCallError,
2627
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
2321
2628
  WorldSessionTimeout: () => WorldSessionTimeout,
2322
2629
  attachSession: () => attachSession,
2323
2630
  isTaskIdRef: () => isTaskIdRef,
@@ -2328,34 +2635,6 @@ __export(session_exports2, {
2328
2635
  schemaFromSignature: () => schemaFromSignature,
2329
2636
  taskLabel: () => taskLabel
2330
2637
  });
2331
- function numericGrade(record) {
2332
- const rewards = {};
2333
- const nested = record.rewards;
2334
- if (nested && typeof nested === "object") {
2335
- for (const [key, value] of Object.entries(nested)) {
2336
- if (typeof value === "number") rewards[key] = value;
2337
- }
2338
- }
2339
- for (const [key, value] of Object.entries(record)) {
2340
- if (key !== "reward" && typeof value === "number") rewards[key] = value;
2341
- }
2342
- return {
2343
- reward: typeof record.reward === "number" ? record.reward : null,
2344
- rewards
2345
- };
2346
- }
2347
- function verifierGrades(value) {
2348
- if (!value || typeof value !== "object") return void 0;
2349
- const out = {};
2350
- for (const [name, grade] of Object.entries(value)) {
2351
- const record = grade && typeof grade === "object" ? grade : {};
2352
- out[name] = {
2353
- ...numericGrade(record),
2354
- ...typeof record.error === "string" ? { error: record.error } : {}
2355
- };
2356
- }
2357
- return out;
2358
- }
2359
2638
  function schemaFromSignature(spec) {
2360
2639
  if (spec.inputSchema)
2361
2640
  return {
@@ -2463,6 +2742,7 @@ async function runSessions(opts) {
2463
2742
  const taskRef = tasks[next++];
2464
2743
  let task = taskLabel(taskRef);
2465
2744
  let session = null;
2745
+ let keepWarm = opts.keepWarm ?? true;
2466
2746
  try {
2467
2747
  session = await openSession(opts.world, {
2468
2748
  task: taskRef,
@@ -2470,6 +2750,7 @@ async function runSessions(opts) {
2470
2750
  options
2471
2751
  });
2472
2752
  task = taskLabel(taskRef, session.taskInfo);
2753
+ WorldToolsUnread.refuse(opts.world.split("@", 1)[0], session.deprecations);
2473
2754
  await session.ready();
2474
2755
  const toolkit = await session.toolkit();
2475
2756
  const live = session;
@@ -2492,19 +2773,19 @@ async function runSessions(opts) {
2492
2773
  results.push(result);
2493
2774
  opts.onResult?.(result);
2494
2775
  } catch (error) {
2776
+ if (error instanceof WorldToolsUnread) keepWarm = false;
2495
2777
  const result = {
2496
2778
  task,
2497
2779
  sessionId: session?.sessionId ?? "",
2498
2780
  reward: 0,
2499
2781
  rewards: {},
2500
- // An Error with an empty message would read as "no error" to any
2501
- // caller checking truthiness; name the type instead.
2782
+ // An empty message reads as "no error" to a truthiness check; name the type instead.
2502
2783
  error: (error instanceof Error ? error.message : String(error)) || (error instanceof Error ? error.name : "Unknown error")
2503
2784
  };
2504
2785
  results.push(result);
2505
2786
  opts.onResult?.(result);
2506
2787
  } finally {
2507
- await session?.close({ keepWarm: opts.keepWarm ?? true }).catch(() => void 0);
2788
+ await session?.close({ keepWarm }).catch(() => void 0);
2508
2789
  }
2509
2790
  }
2510
2791
  })
@@ -2518,7 +2799,7 @@ async function runSessions(opts) {
2518
2799
  passed: failed === 0 && (opts.failUnder === void 0 || mean >= opts.failUnder)
2519
2800
  };
2520
2801
  }
2521
- var import_node_crypto2, READY_TIMEOUT_MS, READY_POLL_MS, CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep, modelToolName, WorldSessionTimeout, WorldSessionCallError, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
2802
+ var import_node_crypto2, CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep3, modelToolName, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
2522
2803
  var init_session2 = __esm({
2523
2804
  "src/worlds/session.ts"() {
2524
2805
  "use strict";
@@ -2527,22 +2808,20 @@ var init_session2 = __esm({
2527
2808
  init_browser();
2528
2809
  init_nativeComputer();
2529
2810
  init_sessionContract();
2811
+ init_sessionReadiness();
2530
2812
  init_session();
2531
2813
  init_taskRef();
2814
+ init_worldNotice();
2815
+ init_grade();
2532
2816
  init_taskRef();
2533
- READY_TIMEOUT_MS = 10 * 6e4;
2534
- READY_POLL_MS = 2e3;
2817
+ init_sessionReadiness();
2535
2818
  CALL_TIMEOUT_MS = 3 * 6e4;
2536
2819
  CALL_POLL_MS = 400;
2537
2820
  JUDGED_GRADE_TIMEOUT_MS = 15 * 6e4;
2538
2821
  JUDGED_GRADE_POLL_MS = 2e3;
2539
2822
  EXPORT_PAGE = 500;
2540
- sleep = (ms) => new Promise((r) => setTimeout(r, ms));
2823
+ sleep3 = (ms) => new Promise((r) => setTimeout(r, ms));
2541
2824
  modelToolName = (name) => name.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64) || "tool";
2542
- WorldSessionTimeout = class extends Error {
2543
- };
2544
- WorldSessionCallError = class extends Error {
2545
- };
2546
2825
  WorldSession = class {
2547
2826
  constructor(sessionId, current, options) {
2548
2827
  this.sessionId = sessionId;
@@ -2660,18 +2939,17 @@ var init_session2 = __esm({
2660
2939
  }
2661
2940
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
2662
2941
  browserRecorder(agentId) {
2663
- const path12 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
2942
+ const path13 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
2664
2943
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${(0, import_node_crypto2.randomUUID)().replace(/-/g, "").slice(0, 8)}`;
2665
2944
  return new BrowserActionRecorder(
2666
2945
  async (events) => {
2667
- await apiRequest(this.options, "POST", path12, { events });
2946
+ await apiRequest(this.options, "POST", path13, { events });
2668
2947
  },
2669
2948
  id,
2670
2949
  {
2671
- // Continue this agent's timeline where the platform has it, so the
2672
- // same id reopened later never re-numbers from zero.
2950
+ // Continue this agent's timeline where the platform has it, so a reopened id never re-numbers from zero.
2673
2951
  resumeFrom: async () => {
2674
- const summary = await apiRequest(this.options, "GET", path12);
2952
+ const summary = await apiRequest(this.options, "GET", path13);
2675
2953
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
2676
2954
  }
2677
2955
  }
@@ -2699,29 +2977,22 @@ var init_session2 = __esm({
2699
2977
  tools: this.current.tools,
2700
2978
  surfaces: this.current.surfaces,
2701
2979
  task: this.current.task,
2980
+ deprecations: this.current.deprecations,
2702
2981
  ...fresh
2703
2982
  };
2704
2983
  return this.current;
2705
2984
  }
2706
2985
  /**
2707
- * Wait until the container is warm — meaning the process inside it has
2708
- * asked the platform for work, not merely that a job was accepted.
2986
+ * Wait until the session answers calls: a World Host holds it, or the
2987
+ * process inside a pod has asked the platform for work. Throws
2988
+ * `WorldSessionOpenFailed` (with the platform's reason) when it ended
2989
+ * first, and `WorldSessionTimeout` when `timeoutMs` (default ten minutes)
2990
+ * ran out while it was still starting.
2709
2991
  */
2710
2992
  async ready(opts = {}) {
2711
- const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
2712
- for (; ; ) {
2713
- const state2 = await this.status();
2714
- if (state2.ready) return state2;
2715
- if (state2.state === "FAILED" || state2.state === "STOPPED")
2716
- throw new WorldSessionCallError(
2717
- `World session ${state2.state.toLowerCase()} before it was ready.`
2718
- );
2719
- if (Date.now() > deadline)
2720
- throw new WorldSessionTimeout(
2721
- `World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
2722
- );
2723
- await sleep(READY_POLL_MS);
2724
- }
2993
+ return new SessionReadiness(this.sessionId, () => this.status()).wait(
2994
+ opts.timeoutMs
2995
+ );
2725
2996
  }
2726
2997
  /** Enqueue one interaction and wait for the container's answer. */
2727
2998
  async exchange(body, timeoutMs) {
@@ -2751,7 +3022,7 @@ var init_session2 = __esm({
2751
3022
  throw new WorldSessionTimeout(
2752
3023
  judged ? `Grade ${callId} still ${call.state} after ${Math.round(budgetMs / 1e3)}s \u2014 the judge has not answered; the platform finalizes the call on its own, read it back with export().` : `Call ${callId} still ${call.state} after ${Math.round(budgetMs / 1e3)}s \u2014 the container may have died.`
2753
3024
  );
2754
- await sleep(pollMs2);
3025
+ await sleep3(pollMs2);
2755
3026
  }
2756
3027
  }
2757
3028
  /**
@@ -2800,7 +3071,9 @@ var init_session2 = __esm({
2800
3071
  * calls); assertions and python graders ignore it. `worlds` names the
2801
3072
  * task's other sessions by alias (`{alias: sessionId}`): the platform
2802
3073
  * reads each one's evidence and hands it to the cross-world verifiers
2803
- * anchored on this session, whose grades come back under `verifiers`. */
3074
+ * anchored on this session, whose grades come back under `verifiers`. A
3075
+ * score that is not a number from 0 to 1 is refused by key and value
3076
+ * (`GradeRefused`); a verifier's is that verifier's `error`. */
2804
3077
  async grade(opts = {}) {
2805
3078
  const raw = await this.exchange(
2806
3079
  {
@@ -2810,14 +3083,7 @@ var init_session2 = __esm({
2810
3083
  },
2811
3084
  opts.timeoutMs
2812
3085
  );
2813
- const record = raw && typeof raw === "object" ? raw : {};
2814
- const verifiers = verifierGrades(record.verifiers);
2815
- return {
2816
- ...numericGrade(record),
2817
- ...typeof record.reason === "string" ? { reason: record.reason } : {},
2818
- ...verifiers ? { verifiers } : {},
2819
- raw
2820
- };
3086
+ return { ...GradePayload.read(raw), raw };
2821
3087
  }
2822
3088
  /**
2823
3089
  * Put rows into the LIVE world through its contract, in one batch:
@@ -3057,28 +3323,52 @@ var init_tasks = __esm({
3057
3323
  }
3058
3324
  });
3059
3325
 
3326
+ // src/worlds/surfaceDescriptor.ts
3327
+ function surfaceDescriptor(s) {
3328
+ return {
3329
+ surfaces: surfaceKinds(s),
3330
+ ...s.api ? { api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} } } : {},
3331
+ ...s.ui ? { ui: { url: s.ui.url, entry: uiEntryUrl(s.ui), apiPath: s.ui.apiPath ?? null } } : {},
3332
+ ...s.browser ? {
3333
+ browser: {
3334
+ wsUrl: s.browser.wsUrl,
3335
+ viewport: s.browser.viewport,
3336
+ ...s.browser.tier ? { tier: s.browser.tier } : {}
3337
+ }
3338
+ } : {}
3339
+ };
3340
+ }
3341
+ var init_surfaceDescriptor = __esm({
3342
+ "src/worlds/surfaceDescriptor.ts"() {
3343
+ "use strict";
3344
+ init_browser();
3345
+ }
3346
+ });
3347
+
3060
3348
  // src/worlds/taskSessions.ts
3061
3349
  function aggregateTaskGrade(worlds) {
3062
- const rewards = {};
3063
- const ungraded = [];
3064
- const graded = [];
3350
+ const fold = new TaskGradeFold();
3065
3351
  const verifiers = {};
3066
- const fold = (key, grade) => {
3067
- if (typeof grade.reward === "number") {
3068
- rewards[key] = grade.reward;
3069
- graded.push(grade.reward);
3070
- } else ungraded.push(key);
3071
- for (const [name, value] of Object.entries(grade.rewards))
3072
- rewards[`${key}.${name}`] = value;
3073
- };
3074
3352
  for (const [alias, grade] of Object.entries(worlds)) {
3075
- fold(alias, grade);
3353
+ fold.add(alias, grade, `world ${alias}`);
3076
3354
  Object.assign(verifiers, grade.verifiers ?? {});
3077
3355
  }
3078
3356
  for (const [name, grade] of Object.entries(verifiers))
3079
- fold(`${VERIFIER_REWARD_PREFIX}${name}`, grade);
3080
- const reward = graded.length > 0 ? graded.reduce((a, b) => a + b, 0) / graded.length : null;
3081
- return { reward, rewards, worlds, ungraded, verifiers };
3357
+ fold.add(`${VERIFIER_REWARD_PREFIX}${name}`, grade, `verifier ${name}`);
3358
+ return {
3359
+ reward: fold.reward(),
3360
+ rewards: fold.rewards,
3361
+ worlds,
3362
+ ungraded: fold.ungraded,
3363
+ verifiers,
3364
+ errors: fold.errors
3365
+ };
3366
+ }
3367
+ function taskGradeError(grade) {
3368
+ const lines = Object.entries(grade.errors).map(
3369
+ ([key, why]) => `${key}: ${why}`
3370
+ );
3371
+ return lines.length ? lines.join("; ") : null;
3082
3372
  }
3083
3373
  function fromResponse(answer, options) {
3084
3374
  const sessions = {};
@@ -3201,12 +3491,14 @@ async function attachManifest(manifest, options = {}) {
3201
3491
  function isTaskManifest(value) {
3202
3492
  return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.sessions === "object" && value.sessions !== null && !("instruction" in value && "worlds" in value);
3203
3493
  }
3204
- var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskSessions;
3494
+ var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskGradeFold, TaskSessions;
3205
3495
  var init_taskSessions = __esm({
3206
3496
  "src/worlds/taskSessions.ts"() {
3207
3497
  "use strict";
3208
3498
  init_worlds();
3209
3499
  init_browser();
3500
+ init_grade();
3501
+ init_surfaceDescriptor();
3210
3502
  init_session2();
3211
3503
  TASK_SESSIONS = "/api/public/world-task-sessions";
3212
3504
  VERIFIER_REWARD_PREFIX = "x.";
@@ -3218,19 +3510,38 @@ var init_taskSessions = __esm({
3218
3510
  sessionId: state2.sessionId,
3219
3511
  ready: state2.ready,
3220
3512
  state: state2.state,
3221
- surfaces: surfaceKinds(s),
3222
- ...s.api ? {
3223
- api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} }
3224
- } : {},
3225
- ...s.ui ? {
3226
- ui: {
3227
- url: s.ui.url,
3228
- entry: uiEntryUrl(s.ui),
3229
- apiPath: s.ui.apiPath ?? null
3230
- }
3231
- } : {},
3232
- ...s.browser ? { browser: { wsUrl: s.browser.wsUrl, viewport: s.browser.viewport } } : {}
3513
+ ...surfaceDescriptor(s)
3233
3514
  });
3515
+ TaskGradeFold = class {
3516
+ rewards = {};
3517
+ ungraded = [];
3518
+ errors = {};
3519
+ sources = /* @__PURE__ */ new Map();
3520
+ graded = [];
3521
+ add(key, grade, source) {
3522
+ if (grade.error) this.errors[key] = grade.error;
3523
+ if (typeof grade.reward === "number") {
3524
+ this.put(key, grade.reward, source);
3525
+ this.graded.push(grade.reward);
3526
+ } else this.ungraded.push(key);
3527
+ for (const [name, value] of Object.entries(grade.rewards))
3528
+ this.put(`${key}.${name}`, value, source);
3529
+ }
3530
+ reward() {
3531
+ if (Object.keys(this.errors).length > 0 || !this.graded.length)
3532
+ return null;
3533
+ return this.graded.reduce((a, b) => a + b, 0) / this.graded.length;
3534
+ }
3535
+ put(key, value, source) {
3536
+ const first = this.sources.get(key) ?? source;
3537
+ this.sources.set(key, first);
3538
+ if (first !== source) {
3539
+ this.errors[key] = `two rewards are named ${key} (${first} and ${source}); rename one of them`;
3540
+ return;
3541
+ }
3542
+ this.rewards[key] = value;
3543
+ }
3544
+ };
3234
3545
  TaskSessions = class {
3235
3546
  constructor(taskId, taskVersion, name, instruction, sessions, worlds, hash = null) {
3236
3547
  this.taskId = taskId;
@@ -3309,11 +3620,13 @@ var init_taskSessions = __esm({
3309
3620
  * agent's own account of the whole investigation) goes to every world's
3310
3621
  * judge; a world graded by assertions or python ignores it. Each grade
3311
3622
  * names the task's other sessions by alias, so the cross-world verifiers
3312
- * anchored on that world can read them. */
3623
+ * anchored on that world can read them. A world whose grade call fails is
3624
+ * that world's error: the task's grade names it and keeps every other
3625
+ * world's grade. */
3313
3626
  async grade(opts = {}) {
3314
3627
  return aggregateTaskGrade(
3315
3628
  await this.each(
3316
- (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) })
3629
+ (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) }).catch((error) => GradePayload.failedCall(error))
3317
3630
  )
3318
3631
  );
3319
3632
  }
@@ -3361,12 +3674,16 @@ var init_evals = __esm({
3361
3674
  "src/worlds/evals.ts"() {
3362
3675
  "use strict";
3363
3676
  init_worlds();
3677
+ init_transport();
3364
3678
  EVALUATIONS = "/api/public/benchmark-containers/evaluations";
3365
3679
  GatewayEvalsClient = class {
3366
- constructor(options = {}) {
3680
+ constructor(options = {}, retry = RetryPolicy.FILING) {
3367
3681
  this.options = options;
3682
+ this.retry = retry;
3368
3683
  }
3369
3684
  options;
3685
+ retry;
3686
+ keyed = /* @__PURE__ */ new Set();
3370
3687
  async createEvaluation(versionId, opts = {}) {
3371
3688
  const body = { versionId };
3372
3689
  if (opts.model !== void 0) body.model = opts.model;
@@ -3374,12 +3691,18 @@ var init_evals = __esm({
3374
3691
  if (opts.rolloutsPerExample !== void 0)
3375
3692
  body.rolloutsPerExample = opts.rolloutsPerExample;
3376
3693
  if (opts.metadata !== void 0) body.metadata = opts.metadata;
3694
+ if (opts.idempotencyKey !== void 0)
3695
+ body.idempotencyKey = opts.idempotencyKey;
3377
3696
  const answer = await apiRequest(
3378
3697
  this.options,
3379
3698
  "POST",
3380
3699
  EVALUATIONS,
3381
- body
3700
+ body,
3701
+ {
3702
+ retry: opts.idempotencyKey === void 0 ? RetryPolicy.NONE : this.retry
3703
+ }
3382
3704
  );
3705
+ if (opts.idempotencyKey !== void 0) this.keyed.add(answer.evaluationId);
3383
3706
  return answer.evaluationId;
3384
3707
  }
3385
3708
  async pushSamples(evaluationId, samples) {
@@ -3387,7 +3710,8 @@ var init_evals = __esm({
3387
3710
  this.options,
3388
3711
  "POST",
3389
3712
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/samples`,
3390
- { samples }
3713
+ { samples },
3714
+ { retry: this.retryFor(evaluationId) }
3391
3715
  );
3392
3716
  return answer.inserted ?? samples.length;
3393
3717
  }
@@ -3400,10 +3724,347 @@ var init_evals = __esm({
3400
3724
  this.options,
3401
3725
  "POST",
3402
3726
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/finalize`,
3403
- body
3727
+ body,
3728
+ { retry: this.retryFor(evaluationId) }
3404
3729
  );
3405
3730
  return answer.status;
3406
3731
  }
3732
+ retryFor(evaluationId) {
3733
+ return this.keyed.has(evaluationId) ? this.retry : RetryPolicy.NONE;
3734
+ }
3735
+ };
3736
+ }
3737
+ });
3738
+
3739
+ // src/worlds/runSignals.ts
3740
+ var import_node_os, OWNER, RunSignals;
3741
+ var init_runSignals = __esm({
3742
+ "src/worlds/runSignals.ts"() {
3743
+ "use strict";
3744
+ import_node_os = require("os");
3745
+ OWNER = /* @__PURE__ */ Symbol("RunSignals");
3746
+ RunSignals = class _RunSignals {
3747
+ constructor(target = process) {
3748
+ this.target = target;
3749
+ }
3750
+ target;
3751
+ static SIGNALS = ["SIGINT", "SIGTERM"];
3752
+ cleanups = [];
3753
+ heardSignal = null;
3754
+ heardCount = 0;
3755
+ listening = false;
3756
+ left = false;
3757
+ listener = Object.assign(
3758
+ (signal) => this.heard(signal),
3759
+ { [OWNER]: this }
3760
+ );
3761
+ /** The signal that arrived, once one has: start nothing new. */
3762
+ get signal() {
3763
+ return this.owner().heardSignal;
3764
+ }
3765
+ async during(cleanup, work) {
3766
+ return this.owner().hold(cleanup, work);
3767
+ }
3768
+ owner() {
3769
+ const owners = this.target.listeners("SIGINT").map((listener) => listener[OWNER]);
3770
+ return owners.find((owner) => owner !== void 0) ?? this;
3771
+ }
3772
+ async hold(cleanup, work) {
3773
+ this.listen();
3774
+ this.cleanups.push(cleanup);
3775
+ try {
3776
+ return await work();
3777
+ } finally {
3778
+ this.cleanups.splice(this.cleanups.indexOf(cleanup), 1);
3779
+ if (!this.cleanups.length && this.heardSignal === null) this.unlisten();
3780
+ }
3781
+ }
3782
+ heard(signal) {
3783
+ this.heardCount += 1;
3784
+ if (this.heardCount > 2) return this.leave(signal);
3785
+ this.heardSignal ??= signal;
3786
+ void this.cleanUp(signal, this.heardCount === 2).finally(() => this.leave(signal));
3787
+ }
3788
+ async cleanUp(signal, urgent) {
3789
+ for (const cleanup of [...this.cleanups].reverse())
3790
+ await Promise.resolve().then(() => cleanup(signal, urgent)).catch(() => void 0);
3791
+ }
3792
+ leave(signal) {
3793
+ if (this.left) return;
3794
+ this.left = true;
3795
+ this.unlisten();
3796
+ if (this.target.listenerCount(signal) === 0)
3797
+ this.target.exit(128 + import_node_os.constants.signals[signal]);
3798
+ }
3799
+ listen() {
3800
+ if (this.listening) return;
3801
+ this.listening = true;
3802
+ for (const signal of _RunSignals.SIGNALS)
3803
+ this.target.on(signal, this.listener);
3804
+ }
3805
+ unlisten() {
3806
+ this.listening = false;
3807
+ for (const signal of _RunSignals.SIGNALS)
3808
+ this.target.off(signal, this.listener);
3809
+ }
3810
+ };
3811
+ }
3812
+ });
3813
+
3814
+ // src/worlds/taskRunDir.ts
3815
+ function sessionRecord({ api: api2, ui, browser, ...session }) {
3816
+ return {
3817
+ ...session,
3818
+ ...api2 ? { api: { url: api2.url } } : {},
3819
+ ...ui ? { ui: { ...ui, entry: withoutUiToken(ui.entry) } } : {},
3820
+ ...browser ? { browser: browserRecord(browser) } : {}
3821
+ };
3822
+ }
3823
+ var fs, path, TaskRunDir, browserRecord;
3824
+ var init_taskRunDir = __esm({
3825
+ "src/worlds/taskRunDir.ts"() {
3826
+ "use strict";
3827
+ fs = __toESM(require("fs"));
3828
+ path = __toESM(require("path"));
3829
+ init_browser();
3830
+ TaskRunDir = class {
3831
+ constructor(path13) {
3832
+ this.path = path13;
3833
+ }
3834
+ path;
3835
+ writeManifest(manifest) {
3836
+ this.writeJson("manifest.json", {
3837
+ ...manifest,
3838
+ sessions: Object.fromEntries(
3839
+ Object.entries(manifest.sessions).map(([alias, session]) => [
3840
+ alias,
3841
+ sessionRecord(session)
3842
+ ])
3843
+ )
3844
+ });
3845
+ }
3846
+ writeJson(name, value) {
3847
+ this.write(name, JSON.stringify(value, null, 2));
3848
+ }
3849
+ write(name, text2) {
3850
+ fs.mkdirSync(this.path, { recursive: true });
3851
+ fs.writeFileSync(path.join(this.path, name), text2);
3852
+ }
3853
+ };
3854
+ browserRecord = ({ viewport, tier }) => ({
3855
+ viewport,
3856
+ ...tier ? { tier } : {}
3857
+ });
3858
+ }
3859
+ });
3860
+
3861
+ // src/worlds/taskRunOutcome.ts
3862
+ var TaskRunOutcome;
3863
+ var init_taskRunOutcome = __esm({
3864
+ "src/worlds/taskRunOutcome.ts"() {
3865
+ "use strict";
3866
+ init_grade();
3867
+ init_taskSessions();
3868
+ TaskRunOutcome = class {
3869
+ constructor(runId, live, model, runDir) {
3870
+ this.runId = runId;
3871
+ this.live = live;
3872
+ this.model = model;
3873
+ this.runDir = runDir;
3874
+ }
3875
+ runId;
3876
+ live;
3877
+ model;
3878
+ runDir;
3879
+ report = null;
3880
+ extra = {};
3881
+ notes = {};
3882
+ agentError = null;
3883
+ grade = null;
3884
+ traced = false;
3885
+ runError = null;
3886
+ fileError = null;
3887
+ evaluationId;
3888
+ evaluationStatus = null;
3889
+ started = Date.now();
3890
+ /** The run stopped short of a grade; the first reason stands. */
3891
+ stopped(error) {
3892
+ this.runError ??= messageOf(error).slice(0, 2e3);
3893
+ }
3894
+ agentRaised(error) {
3895
+ this.agentError = messageOf(error).slice(0, 2e3);
3896
+ }
3897
+ /** Every reason the run failed, on one line: the evaluation's
3898
+ * `metrics.error`. A run that never reached a grade has failed whatever
3899
+ * its reasons say. */
3900
+ failure() {
3901
+ const reasons = [
3902
+ this.agentError === null ? null : `agent raised: ${this.agentError}`,
3903
+ this.runError === null ? null : `the run stopped: ${this.runError}`,
3904
+ this.gradeError(),
3905
+ this.grade === null && this.runError === null ? "the run stopped before it was graded" : null
3906
+ ].filter((reason) => reason !== null);
3907
+ return reasons.length ? reasons.join("; ") : null;
3908
+ }
3909
+ async file(evals, setRunId) {
3910
+ let created;
3911
+ try {
3912
+ created = await evals.createEvaluation(this.ref(this.live.aliases[0]).versionId, {
3913
+ model: this.model,
3914
+ rolloutsPerExample: 1,
3915
+ idempotencyKey: this.runId,
3916
+ ...setRunId ? { runId: setRunId } : {},
3917
+ metadata: this.metadata()
3918
+ });
3919
+ await evals.pushSamples(created, [this.sample()]);
3920
+ this.evaluationStatus = await evals.finalizeEvaluation(created, this.finalized());
3921
+ this.evaluationId = created;
3922
+ } catch (error) {
3923
+ this.fileError = created ? `evaluation ${created}: ${messageOf(error)}` : messageOf(error);
3924
+ if (created)
3925
+ await evals.finalizeEvaluation(created, { status: "FAILED", metrics: { error: this.fileError } }).catch(() => void 0);
3926
+ }
3927
+ }
3928
+ summary() {
3929
+ const live = this.live;
3930
+ const { evaluationId: _agentEvaluationId, ...extra } = this.extra;
3931
+ return {
3932
+ ...extra,
3933
+ runId: this.runId,
3934
+ taskId: live.taskId,
3935
+ taskVersion: live.taskVersion,
3936
+ name: live.name,
3937
+ instruction: live.instruction ?? "",
3938
+ model: this.model,
3939
+ aliases: live.aliases,
3940
+ reward: this.grade?.reward ?? null,
3941
+ rewards: this.grade?.rewards ?? {},
3942
+ ungraded: this.grade?.ungraded ?? live.aliases,
3943
+ agentError: this.agentError,
3944
+ runError: this.runError,
3945
+ gradeError: this.gradeError(),
3946
+ fileError: this.fileError,
3947
+ evaluationStatus: this.evaluationStatus,
3948
+ report: this.report,
3949
+ traced: this.traced,
3950
+ runDir: this.runDir,
3951
+ durationSeconds: Math.round((Date.now() - this.started) / 10) / 100,
3952
+ notes: this.notes,
3953
+ filed: this.evaluationId !== void 0,
3954
+ ...this.evaluationId ? { evaluationId: this.evaluationId } : {}
3955
+ };
3956
+ }
3957
+ gradeError() {
3958
+ return this.grade ? taskGradeError(this.grade) : null;
3959
+ }
3960
+ ref(alias) {
3961
+ const { slug, ref } = this.live.worldRef(alias);
3962
+ const session = this.live.sessions[alias];
3963
+ return { slug, ref, versionId: session.versionId, sessionId: session.sessionId };
3964
+ }
3965
+ metadata() {
3966
+ const live = this.live;
3967
+ return {
3968
+ source: "worlds-task-run",
3969
+ task: { id: live.taskId, version: live.taskVersion, name: live.name, hash: live.hash },
3970
+ anchor: live.aliases[0],
3971
+ worlds: Object.fromEntries(live.aliases.map((alias) => [alias, this.ref(alias)])),
3972
+ runId: this.runId
3973
+ };
3974
+ }
3975
+ sample() {
3976
+ const live = this.live;
3977
+ const reward = this.grade?.reward ?? null;
3978
+ const gradeError = this.gradeError();
3979
+ const sample = {
3980
+ exampleId: live.name ?? live.taskId ?? this.runId,
3981
+ task: live.name ?? live.taskId ?? this.runId,
3982
+ rolloutIndex: 0,
3983
+ prompt: live.instruction,
3984
+ ...this.report === null ? {} : { completion: this.report },
3985
+ rewards: this.grade?.rewards ?? {},
3986
+ info: {
3987
+ sessionId: this.runId,
3988
+ worlds: Object.fromEntries(
3989
+ live.aliases.map((alias) => [
3990
+ alias,
3991
+ { ...this.ref(alias), reward: this.grade?.worlds[alias]?.reward ?? null }
3992
+ ])
3993
+ ),
3994
+ ungraded: this.grade?.ungraded ?? live.aliases,
3995
+ traced: this.traced,
3996
+ runDir: this.runDir,
3997
+ ...this.agentError ? { agentError: this.agentError } : {},
3998
+ ...this.runError ? { runError: this.runError } : {},
3999
+ ...gradeError ? { verifyError: gradeError } : {}
4000
+ }
4001
+ };
4002
+ if (typeof reward === "number")
4003
+ Object.assign(sample, { reward, score: reward, correct: reward >= 1 });
4004
+ return sample;
4005
+ }
4006
+ finalized() {
4007
+ const reward = this.grade?.reward ?? null;
4008
+ const failure = this.failure();
4009
+ return {
4010
+ status: failure ? "FAILED" : "COMPLETED",
4011
+ metrics: {
4012
+ ...typeof reward === "number" ? { avg_reward: reward } : {},
4013
+ reward,
4014
+ ...Object.fromEntries(
4015
+ Object.entries(this.grade?.rewards ?? {}).map(([key, value]) => [`rewards.${key}`, value])
4016
+ ),
4017
+ ...failure ? { error: failure } : {}
4018
+ }
4019
+ };
4020
+ }
4021
+ };
4022
+ }
4023
+ });
4024
+
4025
+ // src/tracing/prerequisites.ts
4026
+ function getEnv(key) {
4027
+ const value = process.env[key];
4028
+ return value ? value : void 0;
4029
+ }
4030
+ function isGatewayConfigured() {
4031
+ return !!(getEnv(GATEWAY_ENV.HOST) && getEnv(GATEWAY_ENV.PUBLIC_KEY) && getEnv(GATEWAY_ENV.SECRET_KEY));
4032
+ }
4033
+ var GATEWAY_ENV, OTLP_TRACES_PATH, OPENTELEMETRY_PEERS, PINNED_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
4034
+ var init_prerequisites = __esm({
4035
+ "src/tracing/prerequisites.ts"() {
4036
+ "use strict";
4037
+ GATEWAY_ENV = {
4038
+ HOST: "GATEWAY_HOST",
4039
+ PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
4040
+ SECRET_KEY: "GATEWAY_SECRET_KEY",
4041
+ API_KEY: "GATEWAY_API_KEY",
4042
+ ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
4043
+ };
4044
+ OTLP_TRACES_PATH = "/api/public/otel/v1/traces";
4045
+ OPENTELEMETRY_PEERS = {
4046
+ "@opentelemetry/api": "1.9.0",
4047
+ "@opentelemetry/exporter-trace-otlp-http": "0.222.0",
4048
+ "@opentelemetry/resources": "2.11.0",
4049
+ "@opentelemetry/sdk-trace-base": "2.11.0",
4050
+ "@opentelemetry/sdk-trace-node": "2.11.0"
4051
+ };
4052
+ PINNED_PEERS = Object.entries(OPENTELEMETRY_PEERS).map(([name, version]) => `${name}@${version}`).join(" ");
4053
+ MODULE_NOT_FOUND = /* @__PURE__ */ new Set(["MODULE_NOT_FOUND", "ERR_MODULE_NOT_FOUND"]);
4054
+ OpenTelemetryMissing = class _OpenTelemetryMissing extends Error {
4055
+ constructor() {
4056
+ super(
4057
+ `OpenTelemetry packages not found. Install them next to @withgateway/sdk, at the versions it is tested with:
4058
+ npm install ${PINNED_PEERS}
4059
+ npm install -g ${PINNED_PEERS} (the gateway CLI, when it was installed with npm install -g)`
4060
+ );
4061
+ this.name = "OpenTelemetryMissing";
4062
+ }
4063
+ static explains(error) {
4064
+ if (error instanceof _OpenTelemetryMissing) return true;
4065
+ const { code, message } = error ?? {};
4066
+ return MODULE_NOT_FOUND.has(String(code)) && String(message).includes("@opentelemetry/");
4067
+ }
3407
4068
  };
3408
4069
  }
3409
4070
  });
@@ -3570,10 +4231,10 @@ var init_users = __esm({
3570
4231
  totalItems: Number(meta.totalItems ?? rows.length)
3571
4232
  };
3572
4233
  }
3573
- async requestJson(method, path12, payload) {
4234
+ async requestJson(method, path13, payload) {
3574
4235
  let response;
3575
4236
  try {
3576
- response = await fetch(`${this.host}${path12}`, {
4237
+ response = await fetch(`${this.host}${path13}`, {
3577
4238
  method,
3578
4239
  headers: {
3579
4240
  Authorization: this.authHeader(),
@@ -3584,17 +4245,17 @@ var init_users = __esm({
3584
4245
  });
3585
4246
  } catch (error) {
3586
4247
  throw new UserProfileError(
3587
- `${method} ${path12} failed: ${error instanceof Error ? error.message : String(error)}`,
4248
+ `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
3588
4249
  { cause: error }
3589
4250
  );
3590
4251
  }
3591
4252
  const text2 = await response.text();
3592
4253
  if (!response.ok) {
3593
4254
  throw new UserProfileError(
3594
- `${method} ${path12} failed (${response.status}): ${text2.slice(0, 500)}`
4255
+ `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
3595
4256
  );
3596
4257
  }
3597
- this.log("%s %s -> %s", method, path12, response.status);
4258
+ this.log("%s %s -> %s", method, path13, response.status);
3598
4259
  return safeJson(text2);
3599
4260
  }
3600
4261
  authHeader() {
@@ -4039,10 +4700,10 @@ var init_scores = __esm({
4039
4700
  metadata: signalMetadata
4040
4701
  });
4041
4702
  }
4042
- async request(method, path12, payload) {
4703
+ async request(method, path13, payload) {
4043
4704
  let response;
4044
4705
  try {
4045
- response = await fetch(`${this.host}${path12}`, {
4706
+ response = await fetch(`${this.host}${path13}`, {
4046
4707
  method,
4047
4708
  headers: {
4048
4709
  Authorization: this.authHeader(),
@@ -4053,17 +4714,17 @@ var init_scores = __esm({
4053
4714
  });
4054
4715
  } catch (error) {
4055
4716
  throw new ScoreError(
4056
- `${method} ${path12} failed: ${error instanceof Error ? error.message : String(error)}`,
4717
+ `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
4057
4718
  { cause: error }
4058
4719
  );
4059
4720
  }
4060
4721
  const text2 = await response.text();
4061
4722
  if (!response.ok) {
4062
4723
  throw new ScoreError(
4063
- `${method} ${path12} failed (${response.status}): ${text2.slice(0, 500)}`
4724
+ `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
4064
4725
  );
4065
4726
  }
4066
- this.log("%s %s -> %s", method, path12, response.status);
4727
+ this.log("%s %s -> %s", method, path13, response.status);
4067
4728
  }
4068
4729
  authHeader() {
4069
4730
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -4091,7 +4752,7 @@ var require_package = __commonJS({
4091
4752
  "package.json"(exports2, module2) {
4092
4753
  module2.exports = {
4093
4754
  name: "@withgateway/sdk",
4094
- version: "0.3.0",
4755
+ version: "0.3.3",
4095
4756
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
4096
4757
  main: "./dist/index.js",
4097
4758
  module: "./dist/index.mjs",
@@ -4500,10 +5161,6 @@ __export(init_exports, {
4500
5161
  isInitialized: () => isInitialized,
4501
5162
  shutdown: () => shutdown
4502
5163
  });
4503
- function getEnv(key) {
4504
- const value = process.env[key];
4505
- return value ? value : void 0;
4506
- }
4507
5164
  function log(message, ...args) {
4508
5165
  if (state.debug) {
4509
5166
  console.log(`[gatewaysdk.tracing] ${message}`, ...args);
@@ -4513,10 +5170,10 @@ function warn(message, ...args) {
4513
5170
  console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
4514
5171
  }
4515
5172
  function buildProvider(config, logFn) {
4516
- const host = config.host ?? getEnv(ENV_VARS.HOST);
4517
- const publicKey = config.publicKey ?? getEnv(ENV_VARS.PUBLIC_KEY);
4518
- const secretKey = config.secretKey ?? getEnv(ENV_VARS.SECRET_KEY);
4519
- const endpoint = config.endpoint ?? getEnv(ENV_VARS.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${DEFAULT_OTLP_PATH}` : void 0);
5173
+ const host = config.host ?? getEnv(GATEWAY_ENV.HOST);
5174
+ const publicKey = config.publicKey ?? getEnv(GATEWAY_ENV.PUBLIC_KEY);
5175
+ const secretKey = config.secretKey ?? getEnv(GATEWAY_ENV.SECRET_KEY);
5176
+ const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${OTLP_TRACES_PATH}` : void 0);
4520
5177
  const missing = [];
4521
5178
  if (!host) missing.push("host (or GATEWAY_HOST env var)");
4522
5179
  if (!publicKey) missing.push("publicKey (or GATEWAY_PUBLIC_KEY env var)");
@@ -4553,9 +5210,7 @@ function buildProvider(config, logFn) {
4553
5210
  ATTR_SERVICE_NAME = "service.name";
4554
5211
  }
4555
5212
  } catch {
4556
- throw new Error(
4557
- "OpenTelemetry packages not found. Install them with:\nnpm install @opentelemetry/sdk-trace-node @opentelemetry/sdk-trace-base @opentelemetry/exporter-trace-otlp-http @opentelemetry/resources"
4558
- );
5213
+ throw new OpenTelemetryMissing();
4559
5214
  }
4560
5215
  const resourceAttrs = {
4561
5216
  [ATTR_SERVICE_NAME]: config.serviceName ?? "gatewaysdk-tracing"
@@ -4761,13 +5416,7 @@ async function shutdown() {
4761
5416
  function isInitialized() {
4762
5417
  return state.initialized;
4763
5418
  }
4764
- function isGatewayConfigured() {
4765
- const host = getEnv(ENV_VARS.HOST);
4766
- const publicKey = getEnv(ENV_VARS.PUBLIC_KEY);
4767
- const secretKey = getEnv(ENV_VARS.SECRET_KEY);
4768
- return !!(host && publicKey && secretKey);
4769
- }
4770
- var ENV_VARS, DEFAULT_OTLP_PATH, state;
5419
+ var state;
4771
5420
  var init_init = __esm({
4772
5421
  "src/tracing/init.ts"() {
4773
5422
  "use strict";
@@ -4776,13 +5425,8 @@ var init_init = __esm({
4776
5425
  init_session();
4777
5426
  init_scores();
4778
5427
  init_build();
4779
- ENV_VARS = {
4780
- HOST: "GATEWAY_HOST",
4781
- PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
4782
- SECRET_KEY: "GATEWAY_SECRET_KEY",
4783
- ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
4784
- };
4785
- DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
5428
+ init_prerequisites();
5429
+ init_prerequisites();
4786
5430
  state = {
4787
5431
  initialized: false,
4788
5432
  provider: null
@@ -4790,86 +5434,95 @@ var init_init = __esm({
4790
5434
  }
4791
5435
  });
4792
5436
 
4793
- // src/worlds/taskRun.ts
4794
- function worldRef(live, alias) {
4795
- const { slug, ref } = live.worldRef(alias);
4796
- const session = live.sessions[alias];
4797
- return {
4798
- slug,
4799
- ref,
4800
- versionId: session.versionId,
4801
- sessionId: session.sessionId
4802
- };
4803
- }
4804
- async function fileTaskRun(live, runId, model, report, grade, agentError, traced, runDir, options, setRunId) {
4805
- const aliases = live.aliases;
4806
- const anchor = aliases[0];
4807
- const worldsMeta = Object.fromEntries(
4808
- aliases.map((alias) => [alias, worldRef(live, alias)])
4809
- );
4810
- const evals = new GatewayEvalsClient(options);
4811
- const evaluationId = await evals.createEvaluation(
4812
- worldRef(live, anchor).versionId,
4813
- {
4814
- model,
4815
- rolloutsPerExample: 1,
4816
- ...setRunId ? { runId: setRunId } : {},
4817
- metadata: {
4818
- source: "worlds-task-run",
4819
- task: {
4820
- id: live.taskId,
4821
- version: live.taskVersion,
4822
- name: live.name,
4823
- hash: live.hash
4824
- },
4825
- anchor,
4826
- worlds: worldsMeta,
4827
- runId
5437
+ // src/worlds/taskTracing.ts
5438
+ var HOST_WITH_AUTHORITY, TRACING_HELD_ELSEWHERE, TaskTracing;
5439
+ var init_taskTracing = __esm({
5440
+ "src/worlds/taskTracing.ts"() {
5441
+ "use strict";
5442
+ init_prerequisites();
5443
+ init_platformEndpoint();
5444
+ HOST_WITH_AUTHORITY = /^[a-z][a-z0-9+.-]*:\/\/[^/\\?#@\s]+(\/|$)/i;
5445
+ 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.";
5446
+ TaskTracing = class _TaskTracing {
5447
+ constructor(load = () => Promise.resolve().then(() => (init_init(), init_exports))) {
5448
+ this.load = load;
5449
+ }
5450
+ load;
5451
+ static warned = false;
5452
+ static target = null;
5453
+ async start(requested, options) {
5454
+ if (!requested) return false;
5455
+ const endpoint = platformEndpoint(options);
5456
+ try {
5457
+ await this.initOnce(_TaskTracing.config(endpoint));
5458
+ return true;
5459
+ } catch (error) {
5460
+ _TaskTracing.warnOnce(_TaskTracing.reason(error, endpoint));
5461
+ return false;
5462
+ }
4828
5463
  }
4829
- }
4830
- );
4831
- const reward = grade.reward;
4832
- const sample = {
4833
- exampleId: live.name ?? live.taskId ?? runId,
4834
- task: live.name ?? live.taskId ?? runId,
4835
- rolloutIndex: 0,
4836
- prompt: live.instruction,
4837
- ...report === null ? {} : { completion: report },
4838
- rewards: grade.rewards,
4839
- info: {
4840
- sessionId: runId,
4841
- worlds: Object.fromEntries(
4842
- aliases.map((alias) => [
4843
- alias,
4844
- {
4845
- ...worldRef(live, alias),
4846
- reward: grade.worlds[alias]?.reward ?? null
4847
- }
4848
- ])
4849
- ),
4850
- ungraded: grade.ungraded,
4851
- traced,
4852
- runDir,
4853
- ...agentError ? { agentError } : {}
4854
- }
4855
- };
4856
- if (typeof reward === "number") {
4857
- sample.reward = reward;
4858
- sample.score = reward;
4859
- sample.correct = reward >= 1;
4860
- }
4861
- await evals.pushSamples(evaluationId, [sample]);
4862
- await evals.finalizeEvaluation(evaluationId, {
4863
- status: agentError ? "FAILED" : "COMPLETED",
4864
- metrics: {
4865
- ...typeof reward === "number" ? { avg_reward: reward } : {},
4866
- reward,
4867
- ...Object.fromEntries(
4868
- Object.entries(grade.rewards).map(([k, v]) => [`rewards.${k}`, v])
4869
- )
4870
- }
4871
- });
4872
- return evaluationId;
5464
+ // init() keeps the first provider for the whole process and ignores later calls, so a later run is
5465
+ // traced only when it exports to the same place with the same keys.
5466
+ async initOnce(config) {
5467
+ const tracing = await this.load();
5468
+ const target = JSON.stringify([config.endpoint, config.publicKey, config.secretKey]);
5469
+ if (!tracing.isInitialized()) {
5470
+ tracing.init(config);
5471
+ _TaskTracing.target = target;
5472
+ return;
5473
+ }
5474
+ if (_TaskTracing.target !== target) throw new Error(TRACING_HELD_ELSEWHERE);
5475
+ }
5476
+ static config(endpoint) {
5477
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
5478
+ return { ...endpoint, endpoint: _TaskTracing.destination(endpoint) };
5479
+ }
5480
+ // B1: a saved login's keys go to their own host only. The redirect is compared as text, never parsed:
5481
+ // a fixed "<host>/" prefix leaves no URL parser room to read another host into it.
5482
+ static destination(endpoint) {
5483
+ const redirect = getEnv(GATEWAY_ENV.ENDPOINT);
5484
+ if (!redirect) return `${endpoint.host}${OTLP_TRACES_PATH}`;
5485
+ if (_TaskTracing.underHost(redirect, endpoint.host) || _TaskTracing.environmentKeys(endpoint)) return redirect;
5486
+ throw new Error(
5487
+ `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.`
5488
+ );
5489
+ }
5490
+ static underHost(redirect, host) {
5491
+ return HOST_WITH_AUTHORITY.test(host) && redirect.startsWith(`${host}/`);
5492
+ }
5493
+ static environmentKeys(endpoint) {
5494
+ const secretKey = getEnv(GATEWAY_ENV.SECRET_KEY) ?? getEnv(GATEWAY_ENV.API_KEY);
5495
+ return endpoint.publicKey === getEnv(GATEWAY_ENV.PUBLIC_KEY) && endpoint.secretKey === secretKey;
5496
+ }
5497
+ static warnOnce(reason) {
5498
+ if (_TaskTracing.warned) return;
5499
+ _TaskTracing.warned = true;
5500
+ console.warn(
5501
+ `[gatewaysdk.tracing] Task runs in this process are not traced. ${reason}
5502
+ Pass --no-trace (trace: false) to run untraced without this warning.`
5503
+ );
5504
+ }
5505
+ static reason(error, endpoint) {
5506
+ if (OpenTelemetryMissing.explains(error)) return new OpenTelemetryMissing().message;
5507
+ const message = error instanceof Error ? error.message : String(error);
5508
+ const keys = endpoint ? [endpoint.publicKey, endpoint.secretKey] : [];
5509
+ return keys.reduce((text2, key) => text2.split(key).join("[redacted]"), message);
5510
+ }
5511
+ };
5512
+ }
5513
+ });
5514
+
5515
+ // src/worlds/taskRun.ts
5516
+ function taskRunFailure(summary) {
5517
+ if (summary.agentError !== null) return `agent raised: ${summary.agentError}`;
5518
+ if (summary.runError !== null) return `the run stopped: ${summary.runError}`;
5519
+ if (summary.gradeError !== null)
5520
+ return `the task could not be graded: ${summary.gradeError}`;
5521
+ if (summary.fileError !== null)
5522
+ return `the run could not be filed: ${summary.fileError}`;
5523
+ if (summary.filed && summary.evaluationStatus !== "COMPLETED")
5524
+ return `the platform settled evaluation ${summary.evaluationId} as ${summary.evaluationStatus}`;
5525
+ return null;
4873
5526
  }
4874
5527
  function reportFromResult(result) {
4875
5528
  if (result === void 0 || result === null)
@@ -4887,123 +5540,49 @@ function reportFromResult(result) {
4887
5540
  );
4888
5541
  return { report, extra };
4889
5542
  }
5543
+ async function refuseUnreadTools(live) {
5544
+ try {
5545
+ for (const alias of live.aliases)
5546
+ WorldToolsUnread.refuse(`${live.worldRef(alias).slug} (alias ${alias})`, live.session(alias).deprecations);
5547
+ } catch (error) {
5548
+ await live.close({ keepWarm: false }).catch(() => void 0);
5549
+ throw error;
5550
+ }
5551
+ }
4890
5552
  async function runTask(ref, agent, opts) {
4891
5553
  const runId = `wtr-${(0, import_node_crypto4.randomBytes)(6).toString("hex")}`;
4892
- const outDir = opts.outDir ?? "runs";
4893
- const runDir = path.join(outDir, runId);
4894
- fs.mkdirSync(runDir, { recursive: true });
4895
5554
  const options = opts.options ?? {};
4896
- const live = await openTask(ref, {
4897
- surfaces: opts.surfaces,
4898
- reuseWarm: opts.reuseWarm,
4899
- browserTier: opts.browserTier,
4900
- options
4901
- });
4902
- const started = Date.now();
4903
- try {
4904
- await live.ready();
4905
- let traced = false;
4906
- if (opts.trace !== false) {
4907
- try {
4908
- const { isGatewayConfigured: isGatewayConfigured2, init: init2 } = await Promise.resolve().then(() => (init_init(), init_exports));
4909
- if (isGatewayConfigured2()) {
4910
- init2();
4911
- traced = true;
4912
- }
4913
- } catch {
4914
- traced = false;
4915
- }
4916
- }
4917
- const taskRun = new TaskRun(
4918
- runId,
4919
- live.instruction ?? "",
4920
- opts.model,
4921
- live.sessions,
4922
- await live.manifest()
4923
- );
4924
- let agentError = null;
4925
- let report = null;
4926
- let extra = {};
4927
- try {
4928
- const invoke = () => agent(taskRun);
4929
- const result = traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(runId, invoke) : await invoke();
4930
- ({ report, extra } = reportFromResult(result));
4931
- } catch (error) {
4932
- const message = (error instanceof Error ? error.message : String(error)) || "Unknown error";
4933
- agentError = message.slice(0, 2e3);
4934
- }
4935
- if (traced)
4936
- await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing) => tracing.flush()).catch(() => void 0);
4937
- const grade = report !== null ? await live.grade({ report }) : await live.grade();
4938
- fs.writeFileSync(
4939
- path.join(runDir, "manifest.json"),
4940
- JSON.stringify(taskRun.manifest, null, 2)
4941
- );
4942
- fs.writeFileSync(
4943
- path.join(runDir, "grades.json"),
4944
- JSON.stringify(grade, null, 2)
4945
- );
4946
- if (report) fs.writeFileSync(path.join(runDir, "report.md"), report);
4947
- const summary = {
4948
- runId,
4949
- taskId: live.taskId,
4950
- taskVersion: live.taskVersion,
4951
- name: live.name,
4952
- instruction: taskRun.instruction,
4953
- model: opts.model,
4954
- aliases: live.aliases,
4955
- reward: grade.reward,
4956
- rewards: grade.rewards,
4957
- ungraded: grade.ungraded,
4958
- agentError,
4959
- report,
4960
- traced,
4961
- runDir,
4962
- durationSeconds: Math.round((Date.now() - started) / 10) / 100,
4963
- notes: taskRun.notes,
4964
- filed: false,
4965
- ...extra
4966
- };
4967
- if (opts.file !== false) {
4968
- summary.evaluationId = await fileTaskRun(
4969
- live,
4970
- runId,
4971
- opts.model,
4972
- report,
4973
- grade,
4974
- agentError,
4975
- traced,
4976
- runDir,
4977
- options,
4978
- opts.setRunId
4979
- );
4980
- summary.filed = true;
4981
- }
4982
- fs.writeFileSync(
4983
- path.join(runDir, "run.json"),
4984
- JSON.stringify(summary, null, 2)
4985
- );
4986
- return summary;
4987
- } finally {
4988
- if (!opts.keepUp) {
4989
- try {
4990
- await live.close({ keepWarm: false });
4991
- } catch {
4992
- }
4993
- }
4994
- }
5555
+ const opening = new TaskOpening(
5556
+ openTask(ref, {
5557
+ surfaces: opts.surfaces,
5558
+ reuseWarm: opts.reuseWarm,
5559
+ browserTier: opts.browserTier,
5560
+ options
5561
+ }).then(async (live) => {
5562
+ await refuseUnreadTools(live);
5563
+ return new LiveTaskRun(runId, live, opts, options);
5564
+ })
5565
+ );
5566
+ return new RunSignals().during(
5567
+ (signal, urgent) => opening.interrupt(signal, urgent),
5568
+ () => opening.complete(agent)
5569
+ );
4995
5570
  }
4996
- var import_node_crypto4, fs, path, TaskRun, describeValue, RESULT_CONTRACT;
5571
+ var import_node_crypto4, path2, TaskRun, FILING_UNFINISHED, describeValue, RESULT_CONTRACT, LiveTaskRun, TaskOpening;
4997
5572
  var init_taskRun = __esm({
4998
5573
  "src/worlds/taskRun.ts"() {
4999
5574
  "use strict";
5000
5575
  import_node_crypto4 = require("crypto");
5001
- fs = __toESM(require("fs"));
5002
- path = __toESM(require("path"));
5576
+ path2 = __toESM(require("path"));
5003
5577
  init_browser();
5004
5578
  init_evals();
5579
+ init_runSignals();
5005
5580
  init_session2();
5006
5581
  init_taskSessions();
5582
+ init_taskRunDir();
5583
+ init_taskRunOutcome();
5584
+ init_taskTracing();
5585
+ init_worldNotice();
5007
5586
  TaskRun = class {
5008
5587
  constructor(runId, instruction, model, sessions, manifest) {
5009
5588
  this.runId = runId;
@@ -5066,8 +5645,127 @@ var init_taskRun = __esm({
5066
5645
  Object.assign(this.notes, notes);
5067
5646
  }
5068
5647
  };
5648
+ FILING_UNFINISHED = "the filing did not finish";
5069
5649
  describeValue = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
5070
5650
  RESULT_CONTRACT = "return a string report, an object {report?: string, ...} or nothing";
5651
+ LiveTaskRun = class {
5652
+ constructor(runId, live, opts, options) {
5653
+ this.live = live;
5654
+ this.opts = opts;
5655
+ this.options = options;
5656
+ this.files = new TaskRunDir(path2.join(opts.outDir ?? "runs", runId));
5657
+ this.outcome = new TaskRunOutcome(runId, live, opts.model, this.files.path);
5658
+ }
5659
+ live;
5660
+ opts;
5661
+ options;
5662
+ files;
5663
+ outcome;
5664
+ played = false;
5665
+ interrupted = false;
5666
+ filing = null;
5667
+ ending = null;
5668
+ async complete(agent) {
5669
+ try {
5670
+ await this.play(agent).catch((error) => this.outcome.stopped(error));
5671
+ this.played = true;
5672
+ await this.file();
5673
+ return this.record();
5674
+ } finally {
5675
+ await this.end();
5676
+ }
5677
+ }
5678
+ async interrupt(signal, urgent = false) {
5679
+ const sessions = this.opts.keepUp ? "leaving its sessions up" : "closing its sessions";
5680
+ const how = urgent ? " now, without waiting" : "";
5681
+ process.stderr.write(`${this.outcome.runId} interrupted by ${signal}; ${sessions}${how}
5682
+ `);
5683
+ if (!this.played) this.outcome.stopped(`interrupted by ${signal}`);
5684
+ this.interrupted = true;
5685
+ try {
5686
+ if (!urgent) await this.filing;
5687
+ this.record();
5688
+ } catch (error) {
5689
+ const why = error instanceof Error ? error.message : String(error);
5690
+ process.stderr.write(`${this.outcome.runId}: run.json not written: ${why}
5691
+ `);
5692
+ } finally {
5693
+ await this.end();
5694
+ }
5695
+ }
5696
+ summary() {
5697
+ return this.outcome.summary();
5698
+ }
5699
+ async play(agent) {
5700
+ await this.live.ready();
5701
+ this.outcome.traced = await new TaskTracing().start(this.opts.trace !== false, this.options);
5702
+ const run = new TaskRun(
5703
+ this.outcome.runId,
5704
+ this.live.instruction ?? "",
5705
+ this.opts.model,
5706
+ this.live.sessions,
5707
+ await this.live.manifest()
5708
+ );
5709
+ this.outcome.notes = run.notes;
5710
+ this.files.writeManifest(run.manifest);
5711
+ await this.callAgent(agent, run);
5712
+ const report = this.outcome.report;
5713
+ this.outcome.grade = await this.live.grade(report !== null ? { report } : {});
5714
+ this.files.writeJson("grades.json", this.outcome.grade);
5715
+ if (report) this.files.write("report.md", report);
5716
+ }
5717
+ async callAgent(agent, run) {
5718
+ try {
5719
+ const invoke = () => agent(run);
5720
+ const result = this.outcome.traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(run.runId, invoke) : await invoke();
5721
+ ({ report: this.outcome.report, extra: this.outcome.extra } = reportFromResult(result));
5722
+ } catch (error) {
5723
+ this.outcome.agentRaised(error);
5724
+ }
5725
+ if (this.outcome.traced)
5726
+ await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing) => tracing.flush()).catch(() => void 0);
5727
+ }
5728
+ async file() {
5729
+ if (this.opts.file === false || this.interrupted) return;
5730
+ this.files.writeJson("run.json", { ...this.outcome.summary(), fileError: FILING_UNFINISHED });
5731
+ this.filing = this.outcome.file(new GatewayEvalsClient(this.options), this.opts.setRunId);
5732
+ await this.filing;
5733
+ }
5734
+ record() {
5735
+ const summary = this.outcome.summary();
5736
+ this.files.writeJson("run.json", summary);
5737
+ return summary;
5738
+ }
5739
+ end() {
5740
+ this.ending ??= this.opts.keepUp ? Promise.resolve() : this.live.close({ keepWarm: false }).then(
5741
+ () => void 0,
5742
+ () => void 0
5743
+ );
5744
+ return this.ending;
5745
+ }
5746
+ };
5747
+ TaskOpening = class {
5748
+ interrupting = null;
5749
+ run = null;
5750
+ opened;
5751
+ constructor(opening) {
5752
+ this.opened = opening.then((run) => this.run = run);
5753
+ }
5754
+ async complete(agent) {
5755
+ const run = await this.opened;
5756
+ if (!this.interrupting) return run.complete(agent);
5757
+ await this.interrupting;
5758
+ return run.summary();
5759
+ }
5760
+ interrupt(signal, urgent = false) {
5761
+ if (urgent) return this.run?.interrupt(signal, true) ?? Promise.resolve();
5762
+ this.interrupting ??= this.opened.then(
5763
+ (run) => run.interrupt(signal),
5764
+ () => void 0
5765
+ );
5766
+ return this.interrupting;
5767
+ }
5768
+ };
5071
5769
  }
5072
5770
  });
5073
5771
 
@@ -5147,73 +5845,157 @@ async function runSet(datasetId, agent, opts) {
5147
5845
  source: opts.source ?? "sdk",
5148
5846
  ...git && Object.keys(git).length ? { git } : {}
5149
5847
  });
5150
- const close = (status) => apiRequest(
5151
- options,
5152
- "POST",
5153
- `${SET_RUNS}/${encodeURIComponent(opened.id)}/close`,
5154
- { status }
5155
- );
5156
- const onInterrupt = () => {
5157
- void close("failed").finally(() => process.exit(130));
5158
- };
5159
- process.once("SIGINT", onInterrupt);
5160
- const planned = await runPlannedTasks(opened, agent, opts, options).finally(
5161
- () => process.off("SIGINT", onInterrupt)
5162
- );
5163
- const { tasks, failed } = planned;
5164
- const requested = failed ? "failed" : "completed";
5165
- const setRun = await close(requested);
5166
- return {
5167
- setRun,
5168
- tasks,
5169
- failed: failed || setRun.status !== "completed",
5170
- closedAsRequested: setRun.status === requested
5171
- };
5172
- }
5173
- async function runPlannedTasks(opened, agent, opts, options) {
5174
- const tasks = [];
5175
- let failed = false;
5176
- for (const task of opened.tasks) {
5177
- try {
5178
- const summary = await runTask(task.worldTaskId, agent, {
5179
- model: opts.model,
5180
- surfaces: opts.surfaces,
5181
- browserTier: opts.browserTier,
5182
- outDir: opts.outDir,
5183
- trace: opts.trace,
5184
- setRunId: opened.id,
5185
- options
5186
- });
5187
- tasks.push(summary);
5188
- failed ||= summary.agentError !== null;
5189
- opts.onTask?.(summary);
5190
- } catch (error) {
5191
- failed = true;
5192
- const message = error instanceof Error ? error.message : String(error);
5193
- process.stderr.write(`task ${task.name} could not run: ${message}
5194
- `);
5195
- }
5196
- }
5197
- return { tasks, failed };
5848
+ return new SetRunner(opened, agent, opts, options).run();
5198
5849
  }
5199
- var SET_RUNS;
5850
+ var SET_RUNS, SetRunner;
5200
5851
  var init_setRun = __esm({
5201
5852
  "src/worlds/setRun.ts"() {
5202
5853
  "use strict";
5203
5854
  init_worlds();
5204
5855
  init_checkoutProvenance();
5856
+ init_runSignals();
5205
5857
  init_taskRun();
5206
5858
  SET_RUNS = "/api/public/set-runs";
5859
+ SetRunner = class {
5860
+ constructor(opened, agent, opts, options) {
5861
+ this.opened = opened;
5862
+ this.agent = agent;
5863
+ this.opts = opts;
5864
+ this.options = options;
5865
+ }
5866
+ opened;
5867
+ agent;
5868
+ opts;
5869
+ options;
5870
+ tasks = [];
5871
+ failures = [];
5872
+ failed = false;
5873
+ current = null;
5874
+ interrupted = null;
5875
+ signals = new RunSignals();
5876
+ async run() {
5877
+ await this.signals.during(
5878
+ (signal2) => this.interrupt(signal2).then(() => void 0),
5879
+ () => this.runTasks()
5880
+ );
5881
+ const signal = this.signals.signal;
5882
+ if (signal !== null) return this.result(await this.interrupt(signal), "failed");
5883
+ const requested = this.failed ? "failed" : "completed";
5884
+ return this.result(await this.close(requested, this.failures), requested);
5885
+ }
5886
+ interrupt(signal) {
5887
+ const stopped = this.current ? [{ worldTaskId: this.current, reason: `interrupted by ${signal}` }] : [];
5888
+ this.interrupted ??= this.close("failed", [...this.failures, ...stopped]);
5889
+ return this.interrupted;
5890
+ }
5891
+ async runTasks() {
5892
+ for (const task of this.opened.tasks) {
5893
+ if (this.signals.signal !== null) return;
5894
+ this.current = task.worldTaskId;
5895
+ await this.runOne(task);
5896
+ }
5897
+ this.current = null;
5898
+ }
5899
+ async runOne(task) {
5900
+ try {
5901
+ const summary = await runTask(task.worldTaskId, this.agent, {
5902
+ model: this.opts.model,
5903
+ surfaces: this.opts.surfaces,
5904
+ browserTier: this.opts.browserTier,
5905
+ outDir: this.opts.outDir,
5906
+ trace: this.opts.trace,
5907
+ setRunId: this.opened.id,
5908
+ options: this.options
5909
+ });
5910
+ this.tasks.push(summary);
5911
+ this.failed ||= taskRunFailure(summary) !== null;
5912
+ if (summary.fileError !== null)
5913
+ this.failures.push({ worldTaskId: task.worldTaskId, reason: summary.fileError });
5914
+ this.opts.onTask?.(summary);
5915
+ } catch (error) {
5916
+ this.failed = true;
5917
+ const message = error instanceof Error ? error.message : String(error);
5918
+ process.stderr.write(`task ${task.name} could not run: ${message}
5919
+ `);
5920
+ this.failures.push({
5921
+ worldTaskId: task.worldTaskId,
5922
+ reason: message || (error instanceof Error ? error.name : "Error")
5923
+ });
5924
+ }
5925
+ }
5926
+ close(status, failures = []) {
5927
+ return apiRequest(
5928
+ this.options,
5929
+ "POST",
5930
+ `${SET_RUNS}/${encodeURIComponent(this.opened.id)}/close`,
5931
+ { status, ...failures.length ? { failures } : {} }
5932
+ );
5933
+ }
5934
+ result(setRun, requested) {
5935
+ return {
5936
+ setRun,
5937
+ tasks: this.tasks,
5938
+ failed: this.failed || setRun.status !== "completed",
5939
+ closedAsRequested: setRun.status === requested
5940
+ };
5941
+ }
5942
+ };
5943
+ }
5944
+ });
5945
+
5946
+ // src/worlds/requestLog.ts
5947
+ var WorldRequestLog;
5948
+ var init_requestLog = __esm({
5949
+ "src/worlds/requestLog.ts"() {
5950
+ "use strict";
5951
+ WorldRequestLog = class _WorldRequestLog {
5952
+ constructor(requests) {
5953
+ this.requests = requests;
5954
+ }
5955
+ requests;
5956
+ static async read(session) {
5957
+ const state2 = await session.state();
5958
+ const calls = state2?.calls;
5959
+ if (!Array.isArray(calls)) return null;
5960
+ return new _WorldRequestLog(calls.filter((c) => typeof c === "object" && c !== null));
5961
+ }
5962
+ /** How many older requests the world no longer keeps (its log is bounded): the gap
5963
+ * before the first entry it still holds. */
5964
+ get dropped() {
5965
+ const first = Number(this.requests[0]?.seq) || 1;
5966
+ return Math.max(0, first - 1);
5967
+ }
5968
+ /** Each request in the shape of a ledger line, `kind: "request"`, so one JSONL
5969
+ * file holds both. */
5970
+ asCalls() {
5971
+ return this.requests.map((r) => ({
5972
+ seq: Number(r.seq) || 0,
5973
+ kind: "request",
5974
+ tool: r.operation ?? null,
5975
+ args: {
5976
+ method: r.method ?? null,
5977
+ path: r.path ?? null,
5978
+ args: r.args ?? null,
5979
+ query: r.query ?? null,
5980
+ body: r.body ?? null,
5981
+ via: r.via ?? null
5982
+ },
5983
+ result: { status: r.status ?? null, rows: r.rows ?? null, found: r.found ?? null },
5984
+ error: null,
5985
+ completedAt: r.at ?? null
5986
+ }));
5987
+ }
5988
+ };
5207
5989
  }
5208
5990
  });
5209
5991
 
5210
5992
  // src/worlds/checkoutTarget.ts
5211
- var fs2, path2, CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
5993
+ var fs2, path3, CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
5212
5994
  var init_checkoutTarget = __esm({
5213
5995
  "src/worlds/checkoutTarget.ts"() {
5214
5996
  "use strict";
5215
5997
  fs2 = __toESM(require("fs"));
5216
- path2 = __toESM(require("path"));
5998
+ path3 = __toESM(require("path"));
5217
5999
  CheckoutRefusedError = class extends Error {
5218
6000
  constructor(message) {
5219
6001
  super(`checkout refused: ${message}`);
@@ -5238,17 +6020,17 @@ var init_checkoutTarget = __esm({
5238
6020
  }
5239
6021
  /** The absolute destination; a relative target may not resolve above `cwd`. */
5240
6022
  static confined(target, cwd) {
5241
- const dir = path2.resolve(cwd, target);
5242
- if (path2.isAbsolute(target)) return dir;
5243
- const rel = path2.relative(cwd, dir);
5244
- if (rel === "" || !rel.startsWith("..") && !path2.isAbsolute(rel))
6023
+ const dir = path3.resolve(cwd, target);
6024
+ if (path3.isAbsolute(target)) return dir;
6025
+ const rel = path3.relative(cwd, dir);
6026
+ if (rel === "" || !rel.startsWith("..") && !path3.isAbsolute(rel))
5245
6027
  return dir;
5246
6028
  throw new CheckoutRefusedError(
5247
6029
  `--target ${target} resolves outside the current directory (${cwd}) \u2014 run from that directory, or pass an absolute path`
5248
6030
  );
5249
6031
  }
5250
6032
  static fresh(slug, cwd) {
5251
- const base = path2.join(cwd, slug);
6033
+ const base = path3.join(cwd, slug);
5252
6034
  let dir = base;
5253
6035
  let i = 1;
5254
6036
  while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
@@ -5275,7 +6057,7 @@ var init_checkoutTarget = __esm({
5275
6057
  ].filter(_CheckoutTarget.confinedRel).sort();
5276
6058
  }
5277
6059
  static confinedRel(rel) {
5278
- if (rel === "" || path2.isAbsolute(rel)) return false;
6060
+ if (rel === "" || path3.isAbsolute(rel)) return false;
5279
6061
  return !rel.split(/[\\/]/).includes("..");
5280
6062
  }
5281
6063
  /** Why `dir` cannot be overwritten, or null when it is a clean checkout of `slug`. */
@@ -5293,9 +6075,9 @@ var init_checkoutTarget = __esm({
5293
6075
  /** Remove the replaced checkout's files (and the dirs they leave empty). */
5294
6076
  clear() {
5295
6077
  for (const rel of this.replaces) {
5296
- const abs = path2.join(this.dir, rel);
6078
+ const abs = path3.join(this.dir, rel);
5297
6079
  fs2.rmSync(abs, { force: true });
5298
- this.pruneEmptyParents(path2.dirname(abs));
6080
+ this.pruneEmptyParents(path3.dirname(abs));
5299
6081
  }
5300
6082
  }
5301
6083
  pruneEmptyParents(from) {
@@ -5303,7 +6085,7 @@ var init_checkoutTarget = __esm({
5303
6085
  while (cur !== this.dir && cur.startsWith(this.dir)) {
5304
6086
  if (fs2.existsSync(cur) && fs2.readdirSync(cur).length > 0) return;
5305
6087
  fs2.rmSync(cur, { recursive: true, force: true });
5306
- cur = path2.dirname(cur);
6088
+ cur = path3.dirname(cur);
5307
6089
  }
5308
6090
  }
5309
6091
  };
@@ -5311,13 +6093,13 @@ var init_checkoutTarget = __esm({
5311
6093
  });
5312
6094
 
5313
6095
  // src/worlds/worldDir.ts
5314
- var import_node_crypto5, fs3, path3, WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, BENCHMARK_CONFIG, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
6096
+ var import_node_crypto5, fs3, path4, WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, BENCHMARK_CONFIG, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
5315
6097
  var init_worldDir = __esm({
5316
6098
  "src/worlds/worldDir.ts"() {
5317
6099
  "use strict";
5318
6100
  import_node_crypto5 = require("crypto");
5319
6101
  fs3 = __toESM(require("fs"));
5320
- path3 = __toESM(require("path"));
6102
+ path4 = __toESM(require("path"));
5321
6103
  WORLD_MANIFEST = "gateway-env.toml";
5322
6104
  API_BUNDLE_MANIFEST = "gateway-api-world.toml";
5323
6105
  BUNDLED_WORLD_PREFIX = "server/world/";
@@ -5343,17 +6125,17 @@ var init_worldDir = __esm({
5343
6125
  worldRoot;
5344
6126
  pathsMemo;
5345
6127
  static open(dir) {
5346
- const present = (rel) => fs3.existsSync(path3.join(dir, rel));
6128
+ const present = (rel) => fs3.existsSync(path4.join(dir, rel));
5347
6129
  const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
5348
6130
  return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
5349
6131
  }
5350
6132
  // ── Files ──────────────────────────────────────────────────────────────
5351
6133
  has(rel) {
5352
- return fs3.existsSync(path3.join(this.dir, rel));
6134
+ return fs3.existsSync(path4.join(this.dir, rel));
5353
6135
  }
5354
6136
  text(rel) {
5355
6137
  try {
5356
- return fs3.readFileSync(path3.join(this.dir, rel), "utf-8");
6138
+ return fs3.readFileSync(path4.join(this.dir, rel), "utf-8");
5357
6139
  } catch {
5358
6140
  return null;
5359
6141
  }
@@ -5372,7 +6154,7 @@ var init_worldDir = __esm({
5372
6154
  const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
5373
6155
  const out = [];
5374
6156
  const walk = (rel) => {
5375
- for (const entry of fs3.readdirSync(rel ? path3.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
6157
+ for (const entry of fs3.readdirSync(rel ? path4.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
5376
6158
  const child = rel ? `${rel}/${entry.name}` : entry.name;
5377
6159
  if (entry.isDirectory()) {
5378
6160
  if (!skippedDir(entry.name)) walk(child);
@@ -5393,7 +6175,7 @@ var init_worldDir = __esm({
5393
6175
  get exclusions() {
5394
6176
  const kept = new Set(this.archivePaths);
5395
6177
  const out = [];
5396
- const filesUnder = (rel) => fs3.readdirSync(path3.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
6178
+ const filesUnder = (rel) => fs3.readdirSync(path4.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
5397
6179
  const child = `${rel}/${entry.name}`;
5398
6180
  if (entry.isDirectory()) return filesUnder(child);
5399
6181
  return entry.isFile() ? [child] : [];
@@ -5424,7 +6206,7 @@ var init_worldDir = __esm({
5424
6206
  const hasher = (0, import_node_crypto5.createHash)("sha256");
5425
6207
  for (const rel of this.archivePaths) {
5426
6208
  hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
5427
- hasher.update(fs3.readFileSync(path3.join(this.dir, rel)));
6209
+ hasher.update(fs3.readFileSync(path4.join(this.dir, rel)));
5428
6210
  }
5429
6211
  return hasher.digest("hex");
5430
6212
  }
@@ -5514,12 +6296,12 @@ function paxPath(body) {
5514
6296
  }
5515
6297
  return found;
5516
6298
  }
5517
- var fs4, path4, import_node_zlib, TAR_FILE_MODE, TAR_EXEC_MODE, TarArchive;
6299
+ var fs4, path5, import_node_zlib, TAR_FILE_MODE, TAR_EXEC_MODE, TarArchive;
5518
6300
  var init_tar = __esm({
5519
6301
  "src/worlds/tar.ts"() {
5520
6302
  "use strict";
5521
6303
  fs4 = __toESM(require("fs"));
5522
- path4 = __toESM(require("path"));
6304
+ path5 = __toESM(require("path"));
5523
6305
  import_node_zlib = require("zlib");
5524
6306
  TAR_FILE_MODE = 420;
5525
6307
  TAR_EXEC_MODE = 493;
@@ -5575,8 +6357,8 @@ var init_tar = __esm({
5575
6357
  for (const member of this.members()) {
5576
6358
  if (member.typeflag !== "0" && member.typeflag !== "\0") continue;
5577
6359
  if (!_TarArchive.isConfined(member.name)) continue;
5578
- const target = path4.join(dir, member.name);
5579
- fs4.mkdirSync(path4.dirname(target), { recursive: true });
6360
+ const target = path5.join(dir, member.name);
6361
+ fs4.mkdirSync(path5.dirname(target), { recursive: true });
5580
6362
  const mode = archiveMode(member.mode);
5581
6363
  fs4.writeFileSync(target, member.bytes, { mode });
5582
6364
  fs4.chmodSync(target, mode);
@@ -5679,7 +6461,7 @@ function tarMember(name, payload, mode) {
5679
6461
  function buildSourceTarball(dir) {
5680
6462
  const parts = [];
5681
6463
  for (const relPath of collectArchiveRelPaths(dir)) {
5682
- const abs = path5.join(dir, relPath);
6464
+ const abs = path6.join(dir, relPath);
5683
6465
  parts.push(
5684
6466
  ...tarMember(
5685
6467
  relPath,
@@ -5693,7 +6475,7 @@ function buildSourceTarball(dir) {
5693
6475
  }
5694
6476
  function readLocalMeta(dir) {
5695
6477
  try {
5696
- const raw = fs5.readFileSync(path5.join(dir, META_RELPATH), "utf-8");
6478
+ const raw = fs5.readFileSync(path6.join(dir, META_RELPATH), "utf-8");
5697
6479
  const parsed = JSON.parse(raw);
5698
6480
  return parsed && typeof parsed === "object" ? parsed : null;
5699
6481
  } catch {
@@ -5716,16 +6498,16 @@ function resolvePushSlug(dir, opts = {}) {
5716
6498
  }
5717
6499
  const first = declared[0];
5718
6500
  if (first) return { ...base, slug: first.slug, source: first.source };
5719
- return { ...base, slug: path5.basename(path5.resolve(dir)), source: "directory name" };
6501
+ return { ...base, slug: path6.basename(path6.resolve(dir)), source: "directory name" };
5720
6502
  }
5721
6503
  function writeLocalMeta(dir, meta) {
5722
- fs5.mkdirSync(path5.join(dir, ".gateway"), { recursive: true });
5723
- fs5.writeFileSync(path5.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
6504
+ fs5.mkdirSync(path6.join(dir, ".gateway"), { recursive: true });
6505
+ fs5.writeFileSync(path6.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
5724
6506
  }
5725
6507
  function snapshotTreeHashes(dir) {
5726
6508
  const out = {};
5727
6509
  for (const relPath of collectArchiveRelPaths(dir)) {
5728
- out[relPath] = (0, import_node_crypto6.createHash)("sha256").update(fs5.readFileSync(path5.join(dir, relPath))).digest("hex");
6510
+ out[relPath] = (0, import_node_crypto6.createHash)("sha256").update(fs5.readFileSync(path6.join(dir, relPath))).digest("hex");
5729
6511
  }
5730
6512
  return out;
5731
6513
  }
@@ -5743,7 +6525,7 @@ function localTreeState(dir) {
5743
6525
  }
5744
6526
  function readPyproject(dir) {
5745
6527
  try {
5746
- const text2 = fs5.readFileSync(path5.join(dir, "pyproject.toml"), "utf-8");
6528
+ const text2 = fs5.readFileSync(path6.join(dir, "pyproject.toml"), "utf-8");
5747
6529
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
5748
6530
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
5749
6531
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -5766,7 +6548,7 @@ function readWorldContractVersion(dir) {
5766
6548
  }
5767
6549
  function buildFileManifest(dir) {
5768
6550
  return collectArchiveRelPaths(dir).map((relPath) => {
5769
- const payload = fs5.readFileSync(path5.join(dir, relPath));
6551
+ const payload = fs5.readFileSync(path6.join(dir, relPath));
5770
6552
  return {
5771
6553
  path: relPath,
5772
6554
  isDirectory: false,
@@ -5812,7 +6594,7 @@ async function push(dir, opts = {}) {
5812
6594
  const pyproject = readPyproject(dir);
5813
6595
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
5814
6596
  if (contractVersion) pyproject.version = contractVersion;
5815
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path5.basename(path5.resolve(dir));
6597
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path6.basename(path6.resolve(dir));
5816
6598
  const metadata = {
5817
6599
  ...opts.name !== void 0 ? { name: opts.name } : {},
5818
6600
  ...opts.description !== void 0 ? { description: opts.description } : {},
@@ -5938,7 +6720,7 @@ async function propose(dir, opts) {
5938
6720
  if (state2.clean)
5939
6721
  throw new Error("Nothing to propose \u2014 the tree matches its base.");
5940
6722
  const changes = state2.dirty.map((relPath) => {
5941
- const abs = path5.join(dir, relPath);
6723
+ const abs = path6.join(dir, relPath);
5942
6724
  if (!fs5.existsSync(abs))
5943
6725
  return { path: relPath, action: "delete" };
5944
6726
  return {
@@ -5970,20 +6752,20 @@ async function createRef(slug, name, kind, versionId, options = {}) {
5970
6752
  ...versionId ? { versionId } : {}
5971
6753
  });
5972
6754
  }
5973
- var import_node_crypto6, import_node_zlib2, fs5, path5, DEFAULT_BRANCH, META_RELPATH, FILE_MODE, MINTED_SUFFIX, PushSlugConflict, sha256b64, DetachedPushError;
6755
+ var import_node_crypto6, import_node_zlib2, fs5, path6, DEFAULT_BRANCH, META_RELPATH, FILE_MODE, MINTED_SUFFIX, PushSlugConflict, sha256b64, DetachedPushError;
5974
6756
  var init_hub = __esm({
5975
6757
  "src/worlds/hub.ts"() {
5976
6758
  "use strict";
5977
6759
  import_node_crypto6 = require("crypto");
5978
6760
  import_node_zlib2 = require("zlib");
5979
6761
  fs5 = __toESM(require("fs"));
5980
- path5 = __toESM(require("path"));
6762
+ path6 = __toESM(require("path"));
5981
6763
  init_worlds();
5982
6764
  init_checkoutTarget();
5983
6765
  init_worldDir();
5984
6766
  init_tar();
5985
6767
  DEFAULT_BRANCH = "main";
5986
- META_RELPATH = path5.join(".gateway", ".env-metadata.json");
6768
+ META_RELPATH = path6.join(".gateway", ".env-metadata.json");
5987
6769
  FILE_MODE = TAR_FILE_MODE;
5988
6770
  MINTED_SUFFIX = /^-\d+$/;
5989
6771
  PushSlugConflict = class extends Error {
@@ -6211,7 +6993,7 @@ function rebaseConflicts(error) {
6211
6993
  }
6212
6994
  const conflicts = body?.conflicts;
6213
6995
  if (!Array.isArray(conflicts)) return null;
6214
- return conflicts.filter(isConflictPath).map(({ path: path12, reason }) => ({ path: path12, reason }));
6996
+ return conflicts.filter(isConflictPath).map(({ path: path13, reason }) => ({ path: path13, reason }));
6215
6997
  }
6216
6998
  function isConflictPath(value) {
6217
6999
  const entry = value;
@@ -6251,8 +7033,8 @@ var init_sandbox = __esm({
6251
7033
  this.options = options;
6252
7034
  }
6253
7035
  options;
6254
- request(method, path12, body) {
6255
- return apiRequest(this.options, method, path12, body, {
7036
+ request(method, path13, body) {
7037
+ return apiRequest(this.options, method, path13, body, {
6256
7038
  timeoutMs: SANDBOX_REQUEST_TIMEOUT_MS
6257
7039
  });
6258
7040
  }
@@ -6783,6 +7565,17 @@ async function queryRows(slugRef, entity, opts = {}, options = {}) {
6783
7565
  `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
6784
7566
  );
6785
7567
  }
7568
+ async function sqlRows(slugRef, sql, opts = {}, options = {}) {
7569
+ const [slug, ref] = slugRef.split("@", 2);
7570
+ if (!slug || ref === "") throw new Error(`sqlRows needs a slug, optionally slug@ref (got ${slugRef})`);
7571
+ if (!sql.trim()) throw new Error("sqlRows needs one SQL statement");
7572
+ return apiRequest(options, "POST", `/api/public/worlds/${encodeURIComponent(slug)}/data/sql`, {
7573
+ sql,
7574
+ ...ref ? { ref } : {},
7575
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
7576
+ ...opts.scenario ? { scenario: opts.scenario } : {}
7577
+ });
7578
+ }
6786
7579
  var init_query = __esm({
6787
7580
  "src/worlds/query.ts"() {
6788
7581
  "use strict";
@@ -6842,13 +7635,13 @@ async function api(creds, method, route, body, init2 = {}) {
6842
7635
  if (!reply.ok) throw new ApiError2(reply.status, method, route, reply.text);
6843
7636
  return reply.text ? JSON.parse(reply.text) : {};
6844
7637
  }
6845
- var fs6, os, path6, ApiError2;
7638
+ var fs6, os, path7, ApiError2;
6846
7639
  var init_config = __esm({
6847
7640
  "src/cli/config.ts"() {
6848
7641
  "use strict";
6849
7642
  fs6 = __toESM(require("fs"));
6850
7643
  os = __toESM(require("os"));
6851
- path6 = __toESM(require("path"));
7644
+ path7 = __toESM(require("path"));
6852
7645
  init_router();
6853
7646
  init_worlds();
6854
7647
  init_transport();
@@ -6886,16 +7679,16 @@ function runtimeRoot() {
6886
7679
  const candidates = [];
6887
7680
  if (process.env.GATEWAY_RUNTIME_PATH)
6888
7681
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
6889
- const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path7.dirname(fs7.realpathSync(process.argv[1])) : null;
7682
+ const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path8.dirname(fs7.realpathSync(process.argv[1])) : null;
6890
7683
  if (entry) {
6891
- candidates.push(path7.resolve(entry, "..", "runtime"));
6892
- candidates.push(path7.resolve(entry, "..", "..", "runtime"));
6893
- candidates.push(path7.resolve(entry, "..", "..", "sdk", "src"));
7684
+ candidates.push(path8.resolve(entry, "..", "runtime"));
7685
+ candidates.push(path8.resolve(entry, "..", "..", "runtime"));
7686
+ candidates.push(path8.resolve(entry, "..", "..", "sdk", "src"));
6894
7687
  }
6895
- candidates.push(path7.resolve(process.cwd(), "runtime"));
7688
+ candidates.push(path8.resolve(process.cwd(), "runtime"));
6896
7689
  for (const root of candidates)
6897
7690
  if (fs7.existsSync(
6898
- path7.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7691
+ path8.join(root, "gatewaysdk", "environment", "schema", "platform.py")
6899
7692
  ))
6900
7693
  return root;
6901
7694
  return null;
@@ -6984,13 +7777,12 @@ async function localBridge(request, local, opts = {}) {
6984
7777
  ["-I", "-B", "-c", BRIDGE_BOOTSTRAP, local.root],
6985
7778
  {
6986
7779
  stdio: ["pipe", "pipe", "pipe"],
6987
- // This is the author's machine running the author's world: the one
6988
- // place the runtime may import and run a world's own Python (check's
6989
- // handler execution, `worlds call`, `worlds test` tool cases).
7780
+ // The author's machine running the author's world: the one place the runtime may run its Python.
6990
7781
  env: {
6991
7782
  PATH: process.env.PATH ?? "",
6992
7783
  HOME: process.env.HOME ?? "",
6993
7784
  TMPDIR: process.env.TMPDIR ?? "",
7785
+ ...process.env.VIRTUAL_ENV ? { VIRTUAL_ENV: process.env.VIRTUAL_ENV } : {},
6994
7786
  GATEWAY_RUNTIME_EXECUTE: "1"
6995
7787
  }
6996
7788
  }
@@ -7036,17 +7828,18 @@ async function localBridge(request, local, opts = {}) {
7036
7828
  child.stdin.end(input);
7037
7829
  });
7038
7830
  }
7039
- var import_node_child_process2, fs7, net, path7, MIN_PYTHON, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
7831
+ var import_node_child_process2, fs7, net, path8, MIN_PYTHON, MIN_PYTHON_LABEL, DIAGNOSTIC_TAIL, BRIDGE_BOOTSTRAP, pythonCache, pythonProbes, PYTHON_CANDIDATES, UV_FIND;
7040
7832
  var init_local = __esm({
7041
7833
  "src/cli/runtime/local.ts"() {
7042
7834
  "use strict";
7043
7835
  import_node_child_process2 = require("child_process");
7044
7836
  fs7 = __toESM(require("fs"));
7045
7837
  net = __toESM(require("net"));
7046
- path7 = __toESM(require("path"));
7838
+ path8 = __toESM(require("path"));
7047
7839
  init_router();
7048
7840
  init_config();
7049
7841
  MIN_PYTHON = [3, 12];
7842
+ MIN_PYTHON_LABEL = MIN_PYTHON.join(".");
7050
7843
  DIAGNOSTIC_TAIL = 600;
7051
7844
  BRIDGE_BOOTSTRAP = `import sys,runpy,types,os
7052
7845
  root=sys.argv[1]
@@ -7065,16 +7858,44 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
7065
7858
  }
7066
7859
  });
7067
7860
 
7861
+ // src/worlds/databaseEngines.ts
7862
+ function isWorldSnapshotOutput(path13) {
7863
+ return SNAPSHOT_OUTPUTS.has(path13.slice(path13.lastIndexOf("/") + 1));
7864
+ }
7865
+ var WORLD_DATABASE_ENGINES, DEFAULT_WORLD_DATABASE_ENGINE, WORLD_SNAPSHOT_FILE_PATH, WORLD_SNAPSHOT_SIDECAR_PATH, WORLD_SNAPSHOT_CONTENT_TYPE, SNAPSHOT_OUTPUTS;
7866
+ var init_databaseEngines = __esm({
7867
+ "src/worlds/databaseEngines.ts"() {
7868
+ "use strict";
7869
+ WORLD_DATABASE_ENGINES = [
7870
+ {
7871
+ engine: "sqlite",
7872
+ label: "SQLite",
7873
+ target: "sqlite-json/1",
7874
+ snapshotName: "state.sqlite",
7875
+ contentType: "application/vnd.sqlite3"
7876
+ }
7877
+ ];
7878
+ DEFAULT_WORLD_DATABASE_ENGINE = WORLD_DATABASE_ENGINES[0];
7879
+ WORLD_SNAPSHOT_FILE_PATH = `db/${DEFAULT_WORLD_DATABASE_ENGINE.snapshotName}`;
7880
+ WORLD_SNAPSHOT_SIDECAR_PATH = "db/state.json";
7881
+ WORLD_SNAPSHOT_CONTENT_TYPE = DEFAULT_WORLD_DATABASE_ENGINE.contentType;
7882
+ SNAPSHOT_OUTPUTS = /* @__PURE__ */ new Set([
7883
+ ...WORLD_DATABASE_ENGINES.map((engine) => engine.snapshotName),
7884
+ WORLD_SNAPSHOT_SIDECAR_PATH.slice("db/".length)
7885
+ ]);
7886
+ }
7887
+ });
7888
+
7068
7889
  // src/cli/runtime/client.ts
7069
7890
  function isBuildOutput(relPath) {
7070
- return BUILD_OUTPUT.test(relPath);
7891
+ return isWorldSnapshotOutput(relPath);
7071
7892
  }
7072
7893
  function readWorldFiles(dir, opts = {}) {
7073
7894
  if (!fs8.existsSync(dir) || !fs8.statSync(dir).isDirectory())
7074
7895
  throw new CliError(`Not a directory: ${dir}`);
7075
7896
  const out = [];
7076
7897
  const walk = (rel) => {
7077
- const abs = rel ? path8.join(dir, rel) : dir;
7898
+ const abs = rel ? path9.join(dir, rel) : dir;
7078
7899
  const entries = fs8.readdirSync(abs, { withFileTypes: true });
7079
7900
  entries.sort((a, b) => a.name.localeCompare(b.name));
7080
7901
  for (const entry of entries) {
@@ -7085,7 +7906,7 @@ function readWorldFiles(dir, opts = {}) {
7085
7906
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
7086
7907
  out.push({
7087
7908
  path: childRel,
7088
- content: fs8.readFileSync(path8.join(dir, childRel)).toString("base64")
7909
+ content: fs8.readFileSync(path9.join(dir, childRel)).toString("base64")
7089
7910
  });
7090
7911
  }
7091
7912
  }
@@ -7105,15 +7926,15 @@ function writeWorldFiles(dir, files, opts = {}) {
7105
7926
  if (opts.clean && fs8.existsSync(dir)) {
7106
7927
  const keep = new Set(files.map((f) => f.path));
7107
7928
  for (const existing of readWorldFiles(dir)) {
7108
- if (!keep.has(existing.path)) fs8.rmSync(path8.join(dir, existing.path));
7929
+ if (!keep.has(existing.path)) fs8.rmSync(path9.join(dir, existing.path));
7109
7930
  }
7110
7931
  pruneEmptyDirs(dir);
7111
7932
  }
7112
7933
  fs8.mkdirSync(dir, { recursive: true });
7113
7934
  const written = [];
7114
7935
  for (const file of files) {
7115
- const target = path8.join(dir, file.path);
7116
- fs8.mkdirSync(path8.dirname(target), { recursive: true });
7936
+ const target = path9.join(dir, file.path);
7937
+ fs8.mkdirSync(path9.dirname(target), { recursive: true });
7117
7938
  fs8.writeFileSync(target, Buffer.from(file.content, "base64"));
7118
7939
  written.push(file.path);
7119
7940
  }
@@ -7123,7 +7944,7 @@ function pruneEmptyDirs(root) {
7123
7944
  const walk = (abs) => {
7124
7945
  let empty = true;
7125
7946
  for (const entry of fs8.readdirSync(abs, { withFileTypes: true })) {
7126
- const child = path8.join(abs, entry.name);
7947
+ const child = path9.join(abs, entry.name);
7127
7948
  if (entry.isDirectory()) {
7128
7949
  if (SKIP_DIRS.has(entry.name)) {
7129
7950
  empty = false;
@@ -7160,13 +7981,9 @@ function yamlDocument(request) {
7160
7981
  return isYamlSource(String(request.source ?? ""), String(request.document ?? ""));
7161
7982
  return request.command === "conform" && Array.isArray(request.openapi) && request.openapi.some((doc) => YAML_NAME.test(doc.path ?? ""));
7162
7983
  }
7163
- function runsLocally(request) {
7164
- const local = localRuntime();
7165
- return local !== null && !(yamlDocument(request) && !localPythonHasYaml(local.python));
7166
- }
7167
7984
  async function runtime(creds, request, opts = {}) {
7168
- const local = localRuntime();
7169
- if (local && runsLocally(request)) {
7985
+ const local = new RuntimeRoute().local(request);
7986
+ if (local) {
7170
7987
  const payload2 = await localBridge(request, local, opts);
7171
7988
  const detail2 = detailFrom(payload2);
7172
7989
  if (detail2) throw new BridgeRejected(detail2);
@@ -7193,19 +8010,19 @@ async function runtime(creds, request, opts = {}) {
7193
8010
  if (payload === null) throw new CliError(`POST ${route} (${request.command}) returned non-JSON`, 1);
7194
8011
  return payload;
7195
8012
  }
7196
- var fs8, path8, MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BUILD_OUTPUT, BridgeRejected, YAML_NAME;
8013
+ var fs8, path9, MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BridgeRejected, YAML_NAME, YAML_NEEDS_PYYAML, YAML_NOT_ON_PLATFORM, RuntimeRoute;
7197
8014
  var init_client = __esm({
7198
8015
  "src/cli/runtime/client.ts"() {
7199
8016
  "use strict";
7200
8017
  fs8 = __toESM(require("fs"));
7201
- path8 = __toESM(require("path"));
8018
+ path9 = __toESM(require("path"));
7202
8019
  init_router();
7203
8020
  init_local();
7204
8021
  init_transport();
8022
+ init_databaseEngines();
7205
8023
  MAX_WORLD_FILES = 2e3;
7206
8024
  RUNTIME_TIMEOUT_MS = 18e4;
7207
8025
  SKIP_DIRS = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
7208
- BUILD_OUTPUT = /(^|\/)state\.(sqlite|json)$/;
7209
8026
  BridgeRejected = class extends CliError {
7210
8027
  constructor(detail) {
7211
8028
  super(formatBridgeError(detail), 1);
@@ -7214,6 +8031,26 @@ var init_client = __esm({
7214
8031
  detail;
7215
8032
  };
7216
8033
  YAML_NAME = /\.ya?ml$/i;
8034
+ 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";
8035
+ 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)";
8036
+ RuntimeRoute = class _RuntimeRoute {
8037
+ constructor(here = localRuntime, hasYaml = localPythonHasYaml) {
8038
+ this.here = here;
8039
+ this.hasYaml = hasYaml;
8040
+ }
8041
+ here;
8042
+ hasYaml;
8043
+ local(request) {
8044
+ const local = this.here();
8045
+ const yaml = yamlDocument(request);
8046
+ if (yaml && local === null) throw _RuntimeRoute.refusal(YAML_NOT_ON_PLATFORM);
8047
+ if (yaml && local !== null && !this.hasYaml(local.python)) throw _RuntimeRoute.refusal(YAML_NEEDS_PYYAML);
8048
+ return local;
8049
+ }
8050
+ static refusal(message) {
8051
+ return new BridgeRejected({ code: "conform", message, schema_path: "/openapi/source" });
8052
+ }
8053
+ };
7217
8054
  }
7218
8055
  });
7219
8056
 
@@ -7228,7 +8065,8 @@ __export(data_exports, {
7228
8065
  ingestLocalToolCalls: () => ingestLocalToolCalls,
7229
8066
  queryLocalWorldData: () => queryLocalWorldData,
7230
8067
  queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
7231
- showLocalWorldData: () => showLocalWorldData
8068
+ showLocalWorldData: () => showLocalWorldData,
8069
+ sqlLocalWorldData: () => sqlLocalWorldData
7232
8070
  });
7233
8071
  async function importLocalWorldData(dir, rows, opts, creds) {
7234
8072
  const res = await runtime(creds, {
@@ -7260,8 +8098,8 @@ async function importLocalWorldFiles(dir, sources, opts) {
7260
8098
  );
7261
8099
  const res = await runtime(null, {
7262
8100
  command: "import-files",
7263
- world: path9.resolve(dir),
7264
- sources: sources.map((source) => path9.resolve(source)),
8101
+ world: path10.resolve(dir),
8102
+ sources: sources.map((source) => path10.resolve(source)),
7265
8103
  mode: opts.mode ?? "append",
7266
8104
  ...opts.entity ? { entity: opts.entity } : {},
7267
8105
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
@@ -7287,9 +8125,9 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7287
8125
  );
7288
8126
  const res = await runtime(null, {
7289
8127
  command: "chunk-files",
7290
- world: path9.resolve(dir),
7291
- sources: sources.map((source) => path9.resolve(source)),
7292
- out: path9.resolve(out),
8128
+ world: path10.resolve(dir),
8129
+ sources: sources.map((source) => path10.resolve(source)),
8130
+ out: path10.resolve(out),
7293
8131
  ...opts.entity ? { entity: opts.entity } : {},
7294
8132
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
7295
8133
  ...opts.chunkBytes ? { chunk_bytes: opts.chunkBytes } : {},
@@ -7300,8 +8138,8 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7300
8138
  function dropBuildOutputs(dir) {
7301
8139
  const dropped = [];
7302
8140
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
7303
- if (!BUILD_OUTPUT2.test(file.path)) continue;
7304
- fs9.rmSync(path9.join(dir, file.path), { force: true });
8141
+ if (!isBuildOutput(file.path)) continue;
8142
+ fs9.rmSync(path10.join(dir, file.path), { force: true });
7305
8143
  dropped.push(file.path);
7306
8144
  }
7307
8145
  return dropped;
@@ -7368,7 +8206,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7368
8206
  const payload = await localBridge(
7369
8207
  {
7370
8208
  command: "query",
7371
- world: path9.resolve(dir),
8209
+ world: path10.resolve(dir),
7372
8210
  entity,
7373
8211
  across: true,
7374
8212
  ...opts.compare ? { compare: true } : {},
@@ -7384,6 +8222,28 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7384
8222
  if (detail) throw new BridgeRejected(detail);
7385
8223
  return payload;
7386
8224
  }
8225
+ async function sqlLocalWorldData(dir, sql, opts) {
8226
+ const local = localRuntime();
8227
+ if (!local)
8228
+ throw new CliError(
8229
+ "`worlds data sql` on a directory reads the world's database in place, so it needs the bundled runtime on this machine. " + localRuntimeReason(),
8230
+ 1
8231
+ );
8232
+ const payload = await localBridge(
8233
+ {
8234
+ command: "sql",
8235
+ world: path10.resolve(dir),
8236
+ sql,
8237
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
8238
+ ...opts.scenario ? { scenario: opts.scenario } : {}
8239
+ },
8240
+ local
8241
+ );
8242
+ const detail = detailFrom(payload);
8243
+ if (detail) throw new BridgeRejected(detail);
8244
+ const { status: _status, ...answer } = payload;
8245
+ return answer;
8246
+ }
7387
8247
  async function ingestLocalToolCalls(dir, records, opts, creds) {
7388
8248
  const res = await runtime(creds, {
7389
8249
  command: "ingest-calls",
@@ -7415,16 +8275,15 @@ async function ingestLocalToolCalls(dir, records, opts, creds) {
7415
8275
  ...res.batch !== void 0 ? { batch: res.batch } : {}
7416
8276
  };
7417
8277
  }
7418
- var fs9, path9, BUILD_OUTPUT2;
8278
+ var fs9, path10;
7419
8279
  var init_data = __esm({
7420
8280
  "src/cli/runtime/data.ts"() {
7421
8281
  "use strict";
7422
8282
  fs9 = __toESM(require("fs"));
7423
- path9 = __toESM(require("path"));
8283
+ path10 = __toESM(require("path"));
7424
8284
  init_router();
7425
8285
  init_client();
7426
8286
  init_local();
7427
- BUILD_OUTPUT2 = /(^|\/)state\.(sqlite|json)$/;
7428
8287
  }
7429
8288
  });
7430
8289
 
@@ -7463,27 +8322,27 @@ function typescriptPrefix() {
7463
8322
  }
7464
8323
  }
7465
8324
  function command(script, wrapperDir) {
7466
- const suffix = path10.extname(script).toLowerCase();
8325
+ const suffix = path11.extname(script).toLowerCase();
7467
8326
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
7468
8327
  if (NODE_SUFFIXES.includes(suffix)) {
7469
- const wrapper = path10.join(wrapperDir, "transform-wrapper.mjs");
8328
+ const wrapper = path11.join(wrapperDir, "transform-wrapper.mjs");
7470
8329
  fs10.writeFileSync(wrapper, NODE_WRAPPER);
7471
8330
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
7472
8331
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
7473
8332
  }
7474
- throw new CliError(`${path10.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
8333
+ throw new CliError(`${path11.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
7475
8334
  }
7476
8335
  async function runTransform(script, records, opts = {}) {
7477
8336
  if (!fs10.existsSync(script) || !fs10.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
7478
- const resolved = path10.resolve(script);
7479
- const name = path10.basename(script);
8337
+ const resolved = path11.resolve(script);
8338
+ const name = path11.basename(script);
7480
8339
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
7481
- const scratch = fs10.mkdtempSync(path10.join(os2.tmpdir(), "gateway-transform-"));
8340
+ const scratch = fs10.mkdtempSync(path11.join(os2.tmpdir(), "gateway-transform-"));
7482
8341
  try {
7483
8342
  const { file, args } = command(resolved, scratch);
7484
8343
  const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !SCRUBBED.test(key)));
7485
8344
  const done = await new Promise((resolve7, reject) => {
7486
- const child = (0, import_node_child_process3.spawn)(file, args, { cwd: path10.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
8345
+ const child = (0, import_node_child_process3.spawn)(file, args, { cwd: path11.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
7487
8346
  let stdout = "";
7488
8347
  let stderr = "";
7489
8348
  let timedOut = false;
@@ -7521,14 +8380,14 @@ async function transformRecords(transform, records, opts = {}) {
7521
8380
  if (typeof transform === "function") return { rows: checkRows(await transform(records), "transform(records)"), stderr: "" };
7522
8381
  return runTransform(transform, records, opts);
7523
8382
  }
7524
- var import_node_child_process3, fs10, os2, path10, import_node_module, import_meta, DEFAULT_TRANSFORM_TIMEOUT_MS, NODE_SUFFIXES, PYTHON_SUFFIXES, SCRUBBED, PY_WRAPPER, NODE_WRAPPER, isObject, tail;
8383
+ var import_node_child_process3, fs10, os2, path11, import_node_module, import_meta, DEFAULT_TRANSFORM_TIMEOUT_MS, NODE_SUFFIXES, PYTHON_SUFFIXES, SCRUBBED, PY_WRAPPER, NODE_WRAPPER, isObject, tail;
7525
8384
  var init_transform = __esm({
7526
8385
  "src/cli/platform/worldData/transform.ts"() {
7527
8386
  "use strict";
7528
8387
  import_node_child_process3 = require("child_process");
7529
8388
  fs10 = __toESM(require("fs"));
7530
8389
  os2 = __toESM(require("os"));
7531
- path10 = __toESM(require("path"));
8390
+ path11 = __toESM(require("path"));
7532
8391
  import_node_module = require("module");
7533
8392
  init_router();
7534
8393
  import_meta = {};
@@ -7648,7 +8507,7 @@ async function forEachLimited(items, limit, fn) {
7648
8507
  if (failure) throw failure;
7649
8508
  }
7650
8509
  async function waitForBatch(client, batchId, opts = {}) {
7651
- const sleep2 = opts.sleep ?? client.sleep;
8510
+ const sleep4 = opts.sleep ?? client.sleep;
7652
8511
  let delay = opts.initialDelayMs ?? 1e3;
7653
8512
  let last = "";
7654
8513
  for (; ; ) {
@@ -7659,7 +8518,7 @@ async function waitForBatch(client, batchId, opts = {}) {
7659
8518
  last = phase;
7660
8519
  }
7661
8520
  if (isSettled(status)) return status;
7662
- await sleep2(delay);
8521
+ await sleep4(delay);
7663
8522
  delay = Math.min(1e4, Math.round(delay * 1.5));
7664
8523
  }
7665
8524
  }
@@ -7683,10 +8542,10 @@ var init_batches = __esm({
7683
8542
  ]);
7684
8543
  sleepMs = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
7685
8544
  WorldDataApi = class {
7686
- constructor(creds, slug, sleep2 = sleepMs) {
8545
+ constructor(creds, slug, sleep4 = sleepMs) {
7687
8546
  this.creds = creds;
7688
8547
  this.slug = slug;
7689
- this.sleep = sleep2;
8548
+ this.sleep = sleep4;
7690
8549
  this.base = `/api/public/worlds/${encodeURIComponent(slug)}/data`;
7691
8550
  }
7692
8551
  creds;
@@ -8090,19 +8949,13 @@ function toolCallsFromObservations(observations, opts = {}) {
8090
8949
  }
8091
8950
  return out;
8092
8951
  }
8093
- function credentialsOf(options) {
8094
- const host = (options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai").replace(/\/+$/, "");
8095
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8096
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8097
- return publicKey && secretKey ? { host, publicKey, secretKey } : null;
8098
- }
8099
8952
  async function ingest(world, records, options = {}) {
8100
8953
  const { ingestLocalToolCalls: ingestLocalToolCalls2, importLocalWorldData: importLocalWorldData2, checkLocalWorldData: checkLocalWorldData2 } = await Promise.resolve().then(() => (init_data(), data_exports));
8101
8954
  if (options.transform !== void 0 && options.map !== void 0) throw new Error("records go through one of transform, map");
8102
8955
  const redact = options.redact ?? "off";
8103
8956
  const mode = options.mode ?? "append";
8104
8957
  const dryRun = Boolean(options.dryRun);
8105
- const creds = credentialsOf(options);
8958
+ const creds = platformEndpoint(options);
8106
8959
  const local = fs12.existsSync(world) && fs12.statSync(world).isDirectory();
8107
8960
  const map = options.map === void 0 ? void 0 : options.map;
8108
8961
  let rows = null;
@@ -8116,12 +8969,12 @@ async function ingest(world, records, options = {}) {
8116
8969
  const landed = dryRun ? await checkLocalWorldData2(world, rows, creds, { redact }) : await importLocalWorldData2(world, rows, { mode, redact }, creds);
8117
8970
  return transformReport(world, records.length, rows, stderr, { dryRun, redact, changed: !dryRun && landed.changed, kind: landed.kind, entityCounts: landed.entity_counts });
8118
8971
  }
8119
- if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
8972
+ if (!creds) throw new Error(GATEWAY_KEYS_MISSING);
8120
8973
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
8121
8974
  const slug = world.split("@", 1)[0];
8122
- const scratch = fs12.mkdtempSync(path11.join(os3.tmpdir(), "gateway-ingest-"));
8975
+ const scratch = fs12.mkdtempSync(path12.join(os3.tmpdir(), "gateway-ingest-"));
8123
8976
  try {
8124
- const pulled = await pull2(world, path11.join(scratch, slug), options);
8977
+ const pulled = await pull2(world, path12.join(scratch, slug), options);
8125
8978
  let report;
8126
8979
  let projected;
8127
8980
  if (rows === null) {
@@ -8195,14 +9048,15 @@ async function sessionToolObservations(sessionId, options = {}) {
8195
9048
  for (const observation of trace3.observations ?? []) out.push({ traceId: trace3.id ?? null, ...observation });
8196
9049
  return out;
8197
9050
  }
8198
- var fs12, os3, path11, isObject2, isoOf;
9051
+ var fs12, os3, path12, isObject2, isoOf;
8199
9052
  var init_ingest = __esm({
8200
9053
  "src/worlds/ingest.ts"() {
8201
9054
  "use strict";
8202
9055
  fs12 = __toESM(require("fs"));
8203
9056
  os3 = __toESM(require("os"));
8204
- path11 = __toESM(require("path"));
9057
+ path12 = __toESM(require("path"));
8205
9058
  init_worlds();
9059
+ init_platformEndpoint();
8206
9060
  isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
8207
9061
  isoOf = (value) => value instanceof Date ? value.toISOString() : typeof value === "string" ? value : void 0;
8208
9062
  }
@@ -8303,7 +9157,7 @@ var init_dryRun = __esm({
8303
9157
  /** Poll until the run settles or `timeoutSeconds` pass; the last verdict either way. */
8304
9158
  async settle(runId, opts) {
8305
9159
  const now = opts.now ?? Date.now;
8306
- const sleep2 = opts.sleep ?? ((ms) => new Promise((resolve7) => setTimeout(resolve7, ms)));
9160
+ const sleep4 = opts.sleep ?? ((ms) => new Promise((resolve7) => setTimeout(resolve7, ms)));
8307
9161
  const deadline = now() + opts.timeoutSeconds * 1e3;
8308
9162
  for (; ; ) {
8309
9163
  const budget = Math.floor(
@@ -8313,7 +9167,7 @@ var init_dryRun = __esm({
8313
9167
  const verdict = await this.status(runId, budget);
8314
9168
  if (verdict.settled || now() >= deadline) return verdict;
8315
9169
  opts.onProgress?.(verdict);
8316
- if (now() - asked < 1e3) await sleep2(1e3);
9170
+ if (now() - asked < 1e3) await sleep4(1e3);
8317
9171
  }
8318
9172
  }
8319
9173
  };
@@ -8381,6 +9235,8 @@ __export(worlds_exports, {
8381
9235
  EXPLORATORY_TASK: () => EXPLORATORY_TASK,
8382
9236
  EXPLORATORY_TASK_SOURCE: () => EXPLORATORY_TASK_SOURCE,
8383
9237
  GatewayEvalsClient: () => GatewayEvalsClient,
9238
+ GradePayload: () => GradePayload,
9239
+ GradeRefused: () => GradeRefused,
8384
9240
  HostedDryRunClient: () => HostedDryRunClient,
8385
9241
  IDLE_WARNING_MINUTES: () => IDLE_WARNING_MINUTES,
8386
9242
  KEY_NAMES: () => KEY_NAMES,
@@ -8396,6 +9252,7 @@ __export(worlds_exports, {
8396
9252
  PlatformTransport: () => PlatformTransport,
8397
9253
  PushSlugConflict: () => PushSlugConflict,
8398
9254
  RECOVER_BUDGET_MS: () => RECOVER_BUDGET_MS,
9255
+ RetryPolicy: () => RetryPolicy,
8399
9256
  SANDBOX_REQUEST_TIMEOUT_MS: () => SANDBOX_REQUEST_TIMEOUT_MS,
8400
9257
  SECRET_KEY: () => SECRET_KEY,
8401
9258
  SESSION_STATES: () => SESSION_STATES,
@@ -8403,8 +9260,10 @@ __export(worlds_exports, {
8403
9260
  STATUS_WAIT_CAP_SECONDS: () => STATUS_WAIT_CAP_SECONDS,
8404
9261
  STRIP_PREFIX: () => STRIP_PREFIX,
8405
9262
  SURFACE_KINDS: () => SURFACE_KINDS,
9263
+ Score: () => Score,
8406
9264
  Screen: () => Screen,
8407
9265
  SecretScrubber: () => SecretScrubber,
9266
+ TOOL_CONTRACT_UNREAD: () => TOOL_CONTRACT_UNREAD,
8408
9267
  TRANSFORMS: () => TRANSFORMS,
8409
9268
  TaskEditClient: () => TaskEditClient,
8410
9269
  TaskRun: () => TaskRun,
@@ -8416,13 +9275,17 @@ __export(worlds_exports, {
8416
9275
  WorldBrowser: () => WorldBrowser,
8417
9276
  WorldCallTooLarge: () => WorldCallTooLarge,
8418
9277
  WorldEditClient: () => WorldEditClient,
9278
+ WorldNotice: () => WorldNotice,
9279
+ WorldRequestLog: () => WorldRequestLog,
8419
9280
  WorldSandboxClient: () => WorldSandboxClient,
8420
9281
  WorldSession: () => WorldSession,
8421
9282
  WorldSessionCallError: () => WorldSessionCallError,
9283
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
8422
9284
  WorldSessionTimeout: () => WorldSessionTimeout,
8423
9285
  WorldSet: () => WorldSet,
8424
9286
  WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
8425
9287
  WorldTask: () => WorldTask,
9288
+ WorldToolsUnread: () => WorldToolsUnread,
8426
9289
  aggregateTaskGrade: () => aggregateTaskGrade,
8427
9290
  apiRequest: () => apiRequest,
8428
9291
  applyTransforms: () => applyTransforms,
@@ -8465,6 +9328,7 @@ __export(worlds_exports, {
8465
9328
  listTaskSessions: () => listTaskSessions,
8466
9329
  listTasks: () => listTasks,
8467
9330
  localTreeState: () => localTreeState,
9331
+ messageOf: () => messageOf,
8468
9332
  migrateProjectWorlds: () => migrateProjectWorlds,
8469
9333
  migrateWorld: () => migrateWorld,
8470
9334
  modelToolName: () => modelToolName,
@@ -8508,9 +9372,14 @@ __export(worlds_exports, {
8508
9372
  setEnvSecret: () => setEnvSecret,
8509
9373
  shapeText: () => shapeText,
8510
9374
  snapshotTreeHashes: () => snapshotTreeHashes,
9375
+ sqlRows: () => sqlRows,
9376
+ stillStartingHint: () => stillStartingHint,
8511
9377
  storedForms: () => storedForms,
9378
+ surfaceDescriptor: () => surfaceDescriptor,
8512
9379
  surfaceKinds: () => surfaceKinds,
9380
+ taskGradeError: () => taskGradeError,
8513
9381
  taskLabel: () => taskLabel,
9382
+ taskRunFailure: () => taskRunFailure,
8514
9383
  toolCallRecordsIn: () => toolCallRecordsIn,
8515
9384
  toolCallsFromObservations: () => toolCallsFromObservations,
8516
9385
  uiEntryUrl: () => uiEntryUrl,
@@ -8525,17 +9394,6 @@ __export(worlds_exports, {
8525
9394
  writeLocalMeta: () => writeLocalMeta
8526
9395
  });
8527
9396
  module.exports = __toCommonJS(worlds_exports);
8528
- function resolveEnv(options) {
8529
- const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
8530
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8531
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8532
- if (!publicKey || !secretKey) {
8533
- throw new Error(
8534
- "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey)."
8535
- );
8536
- }
8537
- return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
8538
- }
8539
9397
  function parseErrorBody(text2, limit = 600) {
8540
9398
  const raw = { detail: text2.slice(0, limit), code: null };
8541
9399
  let parsed;
@@ -8555,9 +9413,9 @@ function parseErrorBody(text2, limit = 600) {
8555
9413
  code: stableCode
8556
9414
  };
8557
9415
  }
8558
- async function apiRequest(options, method, path12, body, init2 = {}) {
8559
- const reply = await new PlatformTransport(resolveEnv(options)).send(method, path12, body, init2);
8560
- if (!reply.ok) throw new ApiError(reply.status, method, path12, reply.text);
9416
+ async function apiRequest(options, method, path13, body, init2 = {}) {
9417
+ const reply = await new PlatformTransport(requirePlatformEndpoint(options)).send(method, path13, body, init2);
9418
+ if (!reply.ok) throw new ApiError(reply.status, method, path13, reply.text);
8561
9419
  return JSON.parse(reply.text);
8562
9420
  }
8563
9421
  async function setEnvSecret(slug, key, value, options = {}) {
@@ -8669,7 +9527,6 @@ function detectProvenance() {
8669
9527
  kind: "ci",
8670
9528
  id: env.GITHUB_ACTOR,
8671
9529
  name: env.GITHUB_ACTOR,
8672
- // GitHub serves every account's avatar at /<login>.png.
8673
9530
  imageUrl: `${server}/${env.GITHUB_ACTOR}.png`
8674
9531
  };
8675
9532
  }
@@ -8737,9 +9594,11 @@ async function resolveWorld(slugRef, slug, ref, pin, options) {
8737
9594
  var ApiError, WorldTask, WorldSet;
8738
9595
  var init_worlds = __esm({
8739
9596
  "src/worlds/index.ts"() {
9597
+ init_platformEndpoint();
8740
9598
  init_transport();
8741
9599
  init_transport();
8742
9600
  init_session2();
9601
+ init_grade();
8743
9602
  init_sessionList();
8744
9603
  init_sessionContract();
8745
9604
  init_taskRef();
@@ -8750,6 +9609,8 @@ var init_worlds = __esm({
8750
9609
  init_checkoutProvenance();
8751
9610
  init_evals();
8752
9611
  init_browser();
9612
+ init_surfaceDescriptor();
9613
+ init_requestLog();
8753
9614
  init_browserReconnect();
8754
9615
  init_nativeComputer();
8755
9616
  init_hub();
@@ -8763,10 +9624,11 @@ var init_worlds = __esm({
8763
9624
  init_sandbox();
8764
9625
  init_editSessions();
8765
9626
  init_dryRun();
9627
+ init_worldNotice();
8766
9628
  ApiError = class extends Error {
8767
- constructor(status, method, path12, text2) {
9629
+ constructor(status, method, path13, text2) {
8768
9630
  const parsed = parseErrorBody(text2);
8769
- super(`${method} ${path12} -> ${status}: ${parsed.detail}`);
9631
+ super(`${method} ${path13} -> ${status}: ${parsed.detail}`);
8770
9632
  this.status = status;
8771
9633
  this.text = text2;
8772
9634
  this.name = "ApiError";
@@ -8882,6 +9744,8 @@ init_worlds();
8882
9744
  EXPLORATORY_TASK,
8883
9745
  EXPLORATORY_TASK_SOURCE,
8884
9746
  GatewayEvalsClient,
9747
+ GradePayload,
9748
+ GradeRefused,
8885
9749
  HostedDryRunClient,
8886
9750
  IDLE_WARNING_MINUTES,
8887
9751
  KEY_NAMES,
@@ -8897,6 +9761,7 @@ init_worlds();
8897
9761
  PlatformTransport,
8898
9762
  PushSlugConflict,
8899
9763
  RECOVER_BUDGET_MS,
9764
+ RetryPolicy,
8900
9765
  SANDBOX_REQUEST_TIMEOUT_MS,
8901
9766
  SECRET_KEY,
8902
9767
  SESSION_STATES,
@@ -8904,8 +9769,10 @@ init_worlds();
8904
9769
  STATUS_WAIT_CAP_SECONDS,
8905
9770
  STRIP_PREFIX,
8906
9771
  SURFACE_KINDS,
9772
+ Score,
8907
9773
  Screen,
8908
9774
  SecretScrubber,
9775
+ TOOL_CONTRACT_UNREAD,
8909
9776
  TRANSFORMS,
8910
9777
  TaskEditClient,
8911
9778
  TaskRun,
@@ -8917,13 +9784,17 @@ init_worlds();
8917
9784
  WorldBrowser,
8918
9785
  WorldCallTooLarge,
8919
9786
  WorldEditClient,
9787
+ WorldNotice,
9788
+ WorldRequestLog,
8920
9789
  WorldSandboxClient,
8921
9790
  WorldSession,
8922
9791
  WorldSessionCallError,
9792
+ WorldSessionOpenFailed,
8923
9793
  WorldSessionTimeout,
8924
9794
  WorldSet,
8925
9795
  WorldSurfaceUnavailable,
8926
9796
  WorldTask,
9797
+ WorldToolsUnread,
8927
9798
  aggregateTaskGrade,
8928
9799
  apiRequest,
8929
9800
  applyTransforms,
@@ -8966,6 +9837,7 @@ init_worlds();
8966
9837
  listTaskSessions,
8967
9838
  listTasks,
8968
9839
  localTreeState,
9840
+ messageOf,
8969
9841
  migrateProjectWorlds,
8970
9842
  migrateWorld,
8971
9843
  modelToolName,
@@ -9009,9 +9881,14 @@ init_worlds();
9009
9881
  setEnvSecret,
9010
9882
  shapeText,
9011
9883
  snapshotTreeHashes,
9884
+ sqlRows,
9885
+ stillStartingHint,
9012
9886
  storedForms,
9887
+ surfaceDescriptor,
9013
9888
  surfaceKinds,
9889
+ taskGradeError,
9014
9890
  taskLabel,
9891
+ taskRunFailure,
9015
9892
  toolCallRecordsIn,
9016
9893
  toolCallsFromObservations,
9017
9894
  uiEntryUrl,