@withgateway/sdk 0.3.1 → 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 (129) hide show
  1. package/README.md +4 -3
  2. package/dist/cli.js +2880 -2032
  3. package/dist/cli.js.map +1 -1
  4. package/dist/cli.mjs +2982 -2135
  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 +180 -41
  9. package/dist/files/index.js.map +1 -1
  10. package/dist/files/index.mjs +172 -35
  11. package/dist/files/index.mjs.map +1 -1
  12. package/dist/{index-CBqbB_0J.d.ts → index-DUHmS74p.d.ts} +1 -1
  13. package/dist/{index-D-AMnAMa.d.mts → index-mJhG3vZU.d.mts} +1 -1
  14. package/dist/index.d.mts +4 -4
  15. package/dist/index.d.ts +4 -4
  16. package/dist/index.js +1309 -540
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +1287 -532
  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 +177 -38
  25. package/dist/traces/index.js.map +1 -1
  26. package/dist/traces/index.mjs +169 -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 +69 -13
  31. package/dist/tracing/index.js.map +1 -1
  32. package/dist/tracing/index.mjs +69 -13
  33. package/dist/tracing/index.mjs.map +1 -1
  34. package/dist/worlds/index.d.mts +249 -120
  35. package/dist/worlds/index.d.ts +249 -120
  36. package/dist/worlds/index.js +1240 -484
  37. package/dist/worlds/index.js.map +1 -1
  38. package/dist/worlds/index.mjs +1231 -476
  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 -514
  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/compiler.py +334 -158
  57. package/runtime/gatewaysdk/environment/schema/conform.py +8 -12
  58. package/runtime/gatewaysdk/environment/schema/connector.py +27 -62
  59. package/runtime/gatewaysdk/environment/schema/data.py +50 -207
  60. package/runtime/gatewaysdk/environment/schema/database/__init__.py +24 -0
  61. package/runtime/gatewaysdk/environment/schema/database/audit.py +121 -0
  62. package/runtime/gatewaysdk/environment/schema/database/base.py +995 -0
  63. package/runtime/gatewaysdk/environment/schema/database/budget.py +33 -0
  64. package/runtime/gatewaysdk/environment/schema/database/connections.py +101 -0
  65. package/runtime/gatewaysdk/environment/schema/database/engines.py +8 -0
  66. package/runtime/gatewaysdk/environment/schema/database/guard.py +208 -0
  67. package/runtime/gatewaysdk/environment/schema/database/ingest.py +154 -0
  68. package/runtime/gatewaysdk/environment/schema/database/matching.py +58 -0
  69. package/runtime/gatewaysdk/environment/schema/database/provenance.py +30 -0
  70. package/runtime/gatewaysdk/environment/schema/database/rows.py +53 -0
  71. package/runtime/gatewaysdk/environment/schema/database/runtime_rows.py +39 -0
  72. package/runtime/gatewaysdk/environment/schema/database/sqlite.py +367 -0
  73. package/runtime/gatewaysdk/environment/schema/domain_migration.py +236 -0
  74. package/runtime/gatewaysdk/environment/schema/envelope.py +169 -0
  75. package/runtime/gatewaysdk/environment/schema/extract.py +6 -13
  76. package/runtime/gatewaysdk/environment/schema/grading.py +45 -30
  77. package/runtime/gatewaysdk/environment/schema/handler_errors.py +142 -12
  78. package/runtime/gatewaysdk/environment/schema/host.py +158 -250
  79. package/runtime/gatewaysdk/environment/schema/lanes.py +53 -0
  80. package/runtime/gatewaysdk/environment/schema/local.py +44 -71
  81. package/runtime/gatewaysdk/environment/schema/mapping.py +5 -25
  82. package/runtime/gatewaysdk/environment/schema/openapi_import.py +112 -73
  83. package/runtime/gatewaysdk/environment/schema/openapi_paging.py +147 -0
  84. package/runtime/gatewaysdk/environment/schema/openapi_payloads.py +288 -0
  85. package/runtime/gatewaysdk/environment/schema/openapi_routes.py +97 -102
  86. package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +113 -30
  87. package/runtime/gatewaysdk/environment/schema/platform.py +123 -171
  88. package/runtime/gatewaysdk/environment/schema/platform_import.py +2 -3
  89. package/runtime/gatewaysdk/environment/schema/platform_query.py +61 -5
  90. package/runtime/gatewaysdk/environment/schema/primary_key.py +61 -0
  91. package/runtime/gatewaysdk/environment/schema/proof.py +19 -12
  92. package/runtime/gatewaysdk/environment/schema/runtime.py +77 -160
  93. package/runtime/gatewaysdk/environment/schema/served_names.py +58 -0
  94. package/runtime/gatewaysdk/environment/schema/session_files.py +166 -0
  95. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +2 -2
  96. package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +19 -11
  97. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +29 -3
  98. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +14 -14
  99. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +12 -12
  100. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +3 -2
  101. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +15 -0
  102. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/openapi-import.md +50 -21
  103. package/runtime/gatewaysdk/environment/schema/slack.py +4 -8
  104. package/runtime/gatewaysdk/environment/schema/snapshot.py +385 -129
  105. package/runtime/gatewaysdk/environment/schema/store_contract.py +8 -14
  106. package/runtime/gatewaysdk/environment/schema/tasks.py +140 -55
  107. package/runtime/gatewaysdk/environment/schema/tools_world.py +110 -87
  108. package/runtime/gatewaysdk/environment/schema/ui.py +24 -5
  109. package/runtime/gatewaysdk/environment/schema/ui_static/app.js +100 -130
  110. package/runtime/gatewaysdk/environment/schema/ui_static/index.html +1 -8
  111. package/runtime/gatewaysdk/environment/schema/validation.py +166 -40
  112. package/runtime/gatewaysdk/environment/schema/workers.py +142 -57
  113. package/runtime/gatewaysdk/environment/schema/world_sql.py +81 -0
  114. package/runtime/gatewaysdk/environment/schema/world_tests.py +71 -39
  115. package/runtime/gatewaysdk/environment/score.py +57 -0
  116. package/runtime/gatewaysdk/environment/secret_values.py +36 -0
  117. package/runtime/gatewaysdk/environment/world.py +42 -3
  118. package/runtime/gatewaysdk/environment/world_session_contract.py +32 -5
  119. package/skills/benchmarks-getting-started/SKILL.md +2 -2
  120. package/skills/connector-schema-authoring/SKILL.md +19 -11
  121. package/skills/connector-schema-authoring/connector-worlds.md +13 -4
  122. package/skills/connector-schema-authoring/world-schema.md +103 -51
  123. package/skills/world-data-ingestion/ROWS.md +29 -3
  124. package/skills/world-data-ingestion/SKILL.md +14 -14
  125. package/skills/worlds-getting-started/SKILL.md +12 -12
  126. package/skills/worlds-getting-started/references/cli-mcp-map.md +3 -2
  127. package/skills/worlds-getting-started/references/gotchas.md +15 -0
  128. package/skills/worlds-getting-started/references/openapi-import.md +50 -21
  129. package/runtime/gatewaysdk/environment/schema/store.py +0 -1013
@@ -28,6 +28,28 @@ var __export = (target, all) => {
28
28
  __defProp(target, name, { get: all[name], enumerable: true });
29
29
  };
30
30
 
31
+ // src/worlds/platformEndpoint.ts
32
+ function platformEndpoint(options) {
33
+ const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
34
+ const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
35
+ if (!publicKey || !secretKey) return null;
36
+ const host = options.host ?? process.env.GATEWAY_HOST ?? DEFAULT_HOST;
37
+ return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
38
+ }
39
+ function requirePlatformEndpoint(options) {
40
+ const endpoint = platformEndpoint(options);
41
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
42
+ return endpoint;
43
+ }
44
+ var DEFAULT_HOST, GATEWAY_KEYS_MISSING;
45
+ var init_platformEndpoint = __esm({
46
+ "src/worlds/platformEndpoint.ts"() {
47
+ "use strict";
48
+ DEFAULT_HOST = "https://withgateway.ai";
49
+ GATEWAY_KEYS_MISSING = "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).";
50
+ }
51
+ });
52
+
31
53
  // src/worlds/transport.ts
32
54
  function platformTimeoutMs(env = process.env) {
33
55
  const raw = env[PLATFORM_TIMEOUT_ENV];
@@ -36,7 +58,7 @@ function platformTimeoutMs(env = process.env) {
36
58
  if (Number.isInteger(value) && value > 0) return value;
37
59
  throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
38
60
  }
39
- var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, PlatformTransport;
61
+ var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, sleep, RetryPolicy, Attempt, PlatformTransport;
40
62
  var init_transport = __esm({
41
63
  "src/worlds/transport.ts"() {
42
64
  "use strict";
@@ -98,6 +120,56 @@ var init_transport = __esm({
98
120
  route;
99
121
  failure;
100
122
  };
123
+ sleep = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
124
+ RetryPolicy = class _RetryPolicy {
125
+ constructor(delaysMs, wait = sleep, budgetMs = Number.POSITIVE_INFINITY, now = Date.now) {
126
+ this.delaysMs = delaysMs;
127
+ this.wait = wait;
128
+ this.budgetMs = budgetMs;
129
+ this.now = now;
130
+ }
131
+ delaysMs;
132
+ wait;
133
+ budgetMs;
134
+ now;
135
+ static NONE = new _RetryPolicy([]);
136
+ /** A platform restart or a short edge outage: no wait starts that would end past five minutes, so a step ends
137
+ * within the budget plus one request timeout. */
138
+ static FILING = new _RetryPolicy([250, 500, 1e3, 2e3, 4e3, 8e3, 16e3, 3e4], sleep, 3e5);
139
+ static TRANSIENT_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
140
+ async run(attempt) {
141
+ const started = this.now();
142
+ for (let tries = 0; ; tries += 1) {
143
+ const outcome = await Attempt.of(attempt);
144
+ const delayMs = this.delaysMs[tries];
145
+ const spent = delayMs === void 0 || this.now() - started + delayMs >= this.budgetMs;
146
+ if (!outcome.transient || spent) return outcome.settle();
147
+ await this.wait(delayMs);
148
+ }
149
+ }
150
+ };
151
+ Attempt = class _Attempt {
152
+ constructor(reply, error, transient) {
153
+ this.reply = reply;
154
+ this.error = error;
155
+ this.transient = transient;
156
+ }
157
+ reply;
158
+ error;
159
+ transient;
160
+ static async of(attempt) {
161
+ try {
162
+ const reply = await attempt();
163
+ return new _Attempt(reply, null, RetryPolicy.TRANSIENT_STATUSES.has(reply.status));
164
+ } catch (error) {
165
+ return new _Attempt(null, error, error instanceof UnreachableError);
166
+ }
167
+ }
168
+ settle() {
169
+ if (this.reply) return this.reply;
170
+ throw this.error;
171
+ }
172
+ };
101
173
  PlatformTransport = class {
102
174
  constructor(endpoint, timeoutMs = platformTimeoutMs()) {
103
175
  this.endpoint = endpoint;
@@ -111,6 +183,9 @@ var init_transport = __esm({
111
183
  }
112
184
  async send(method, route, body, opts = {}) {
113
185
  const timeoutMs = this.budget(opts.timeoutMs);
186
+ return (opts.retry ?? RetryPolicy.NONE).run(() => this.once(method, route, body, timeoutMs));
187
+ }
188
+ async once(method, route, body, timeoutMs) {
114
189
  const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
115
190
  const controller = new AbortController();
116
191
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -304,9 +379,9 @@ function surfaceKinds(s) {
304
379
  if (s.browser) kinds.push("browser");
305
380
  return kinds;
306
381
  }
307
- function uiUrl(ui, path12) {
382
+ function uiUrl(ui, path13) {
308
383
  const base = ui.url.replace(/\/+$/, "");
309
- const rel = path12.startsWith("/") ? path12 : `/${path12}`;
384
+ const rel = path13.startsWith("/") ? path13 : `/${path13}`;
310
385
  if (!ui.token) return base + rel;
311
386
  const [beforeHash, hash] = splitHash(rel);
312
387
  const joiner = beforeHash.includes("?") ? "&" : "?";
@@ -547,7 +622,7 @@ var init_browser = __esm({
547
622
  }
548
623
  async send(batch) {
549
624
  const retries = this.opts.maxRetries ?? EVENT_MAX_RETRIES;
550
- const sleep2 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
625
+ const sleep4 = this.opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
551
626
  for (let attempt = 0; attempt < retries; attempt++) {
552
627
  try {
553
628
  const base = await this.resolveBase();
@@ -564,7 +639,7 @@ var init_browser = __esm({
564
639
  );
565
640
  return;
566
641
  }
567
- await sleep2(500 * 2 ** attempt);
642
+ await sleep4(500 * 2 ** attempt);
568
643
  }
569
644
  }
570
645
  }
@@ -1201,13 +1276,13 @@ var init_screen = __esm({
1201
1276
  });
1202
1277
  }
1203
1278
  async drag(action) {
1204
- const path12 = action.args.path ?? [];
1205
- if (path12.length < 2) throw new Error("a drag needs at least two points");
1279
+ const path13 = action.args.path ?? [];
1280
+ if (path13.length < 2) throw new Error("a drag needs at least two points");
1206
1281
  return this.held(action.args.modifiers ?? [], async () => {
1207
- await this.moveTo(path12[0]);
1282
+ await this.moveTo(path13[0]);
1208
1283
  await this.page.mouse.down({ button: "left" });
1209
1284
  try {
1210
- for (const point2 of path12.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1285
+ for (const point2 of path13.slice(1)) await this.moveTo(point2, DRAG_STEPS);
1211
1286
  } finally {
1212
1287
  await this.page.mouse.up({ button: "left" });
1213
1288
  }
@@ -1298,7 +1373,7 @@ var init_media = __esm({
1298
1373
  put;
1299
1374
  maxBytes;
1300
1375
  /** The uploader for `GATEWAY_HOST` / `GATEWAY_PUBLIC_KEY` /
1301
- * `GATEWAY_SECRET_KEY` (the keys tracing exports with); null when any is unset. */
1376
+ * `GATEWAY_SECRET_KEY` only; null when any is unset, so a run traced with a saved login uploads none. */
1302
1377
  static fromEnv(env = process.env) {
1303
1378
  const host = env.GATEWAY_HOST;
1304
1379
  const publicKey = env.GATEWAY_PUBLIC_KEY;
@@ -2168,7 +2243,7 @@ function idleWarning(idleExpiresAt, now = /* @__PURE__ */ new Date()) {
2168
2243
  const minutes = Math.max(0, Math.floor(leftMs / 6e4));
2169
2244
  return `warning: session expires in ${minutes}m (idle TTL); call a tool to extend it or \`gateway worlds session pin\` to keep it`;
2170
2245
  }
2171
- var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge;
2246
+ var CALL_BODY_LIMIT_BYTES, CALL_BODY_LIMIT_LABEL, IDLE_WARNING_MINUTES, EXPLORATORY_TASK, EXPLORATORY_TASK_SOURCE, NO_GRADER_REASON, WorldCallTooLarge, stillStartingHint;
2172
2247
  var init_sessionContract = __esm({
2173
2248
  "src/worlds/sessionContract.ts"() {
2174
2249
  "use strict";
@@ -2204,6 +2279,65 @@ var init_sessionContract = __esm({
2204
2279
  };
2205
2280
  }
2206
2281
  };
2282
+ stillStartingHint = (sessionId) => `It keeps opening: poll \`gateway worlds session status ${sessionId}\` until ready, or end it with \`gateway worlds session close ${sessionId}\`.`;
2283
+ }
2284
+ });
2285
+
2286
+ // src/worlds/sessionReadiness.ts
2287
+ var READY_TIMEOUT_MS, READY_POLL_MS, sleep2, WorldSessionTimeout, WorldSessionCallError, WorldSessionOpenFailed, ENDED, SessionReadiness;
2288
+ var init_sessionReadiness = __esm({
2289
+ "src/worlds/sessionReadiness.ts"() {
2290
+ "use strict";
2291
+ READY_TIMEOUT_MS = 10 * 6e4;
2292
+ READY_POLL_MS = 2e3;
2293
+ sleep2 = (ms) => new Promise((r) => setTimeout(r, ms));
2294
+ WorldSessionTimeout = class extends Error {
2295
+ };
2296
+ WorldSessionCallError = class extends Error {
2297
+ };
2298
+ WorldSessionOpenFailed = class extends WorldSessionCallError {
2299
+ constructor(sessionId, state2, code, reason) {
2300
+ super(
2301
+ reason === null ? `World session ${sessionId} ${state2.toLowerCase()} before it was ready.` : `World session ${sessionId} failed to open: ${reason}${code ? ` (${code})` : ""}`
2302
+ );
2303
+ this.sessionId = sessionId;
2304
+ this.state = state2;
2305
+ this.code = code;
2306
+ this.name = "WorldSessionOpenFailed";
2307
+ }
2308
+ sessionId;
2309
+ state;
2310
+ code;
2311
+ };
2312
+ ENDED = /* @__PURE__ */ new Set(["FAILED", "STOPPED"]);
2313
+ SessionReadiness = class {
2314
+ constructor(sessionId, status) {
2315
+ this.sessionId = sessionId;
2316
+ this.status = status;
2317
+ }
2318
+ sessionId;
2319
+ status;
2320
+ async wait(timeoutMs = READY_TIMEOUT_MS) {
2321
+ const deadline = Date.now() + timeoutMs;
2322
+ for (; ; ) {
2323
+ const state2 = await this.status();
2324
+ if (state2.ready) return state2;
2325
+ if (ENDED.has(state2.state))
2326
+ throw new WorldSessionOpenFailed(
2327
+ this.sessionId,
2328
+ state2.state,
2329
+ state2.failure?.code ?? null,
2330
+ state2.failure?.message ?? null
2331
+ );
2332
+ const left = deadline - Date.now();
2333
+ if (left <= 0)
2334
+ throw new WorldSessionTimeout(
2335
+ `World session ${this.sessionId} still ${state2.state} after ${Math.round(timeoutMs / 1e3)}s.`
2336
+ );
2337
+ await sleep2(Math.min(READY_POLL_MS, left));
2338
+ }
2339
+ }
2340
+ };
2207
2341
  }
2208
2342
  });
2209
2343
 
@@ -2299,11 +2433,184 @@ var init_taskRef = __esm({
2299
2433
  }
2300
2434
  });
2301
2435
 
2436
+ // src/worlds/worldNotice.ts
2437
+ var TOOL_CONTRACT_UNREAD, DEPRECATION, NOTICE_KINDS, WorldNotice, WorldToolsUnread;
2438
+ var init_worldNotice = __esm({
2439
+ "src/worlds/worldNotice.ts"() {
2440
+ "use strict";
2441
+ TOOL_CONTRACT_UNREAD = "tool-contract-unread";
2442
+ DEPRECATION = { label: "deprecated", next: "Instead", refusesRun: false };
2443
+ NOTICE_KINDS = /* @__PURE__ */ new Map([
2444
+ [TOOL_CONTRACT_UNREAD, { label: "warning", next: "Then", refusesRun: true }]
2445
+ ]);
2446
+ WorldNotice = class _WorldNotice {
2447
+ constructor(notice, kind) {
2448
+ this.notice = notice;
2449
+ this.kind = kind;
2450
+ }
2451
+ notice;
2452
+ kind;
2453
+ static of(notice) {
2454
+ return new _WorldNotice(notice, NOTICE_KINDS.get(notice.code) ?? DEPRECATION);
2455
+ }
2456
+ get refusesRun() {
2457
+ return this.kind.refusesRun;
2458
+ }
2459
+ line() {
2460
+ const { code, message, replacement } = this.notice;
2461
+ return `${this.kind.label} (${code}): ${message} ${this.kind.next}: ${replacement}`;
2462
+ }
2463
+ };
2464
+ WorldToolsUnread = class _WorldToolsUnread extends Error {
2465
+ constructor(world, notice) {
2466
+ super(
2467
+ `refused before the agent ran, nothing filed: world ${world} has no readable tools: ${notice.notice.message} Then: ${notice.notice.replacement}`
2468
+ );
2469
+ this.world = world;
2470
+ this.notice = notice;
2471
+ this.name = "WorldToolsUnread";
2472
+ this.code = notice.notice.code;
2473
+ }
2474
+ world;
2475
+ notice;
2476
+ code;
2477
+ static refuse(world, notices) {
2478
+ const refusing = notices.map((notice) => WorldNotice.of(notice)).find((notice) => notice.refusesRun);
2479
+ if (refusing) throw new _WorldToolsUnread(world, refusing);
2480
+ }
2481
+ };
2482
+ }
2483
+ });
2484
+
2485
+ // src/worlds/grade.ts
2486
+ var messageOf, Score, GradeRefused, GradePayload;
2487
+ var init_grade = __esm({
2488
+ "src/worlds/grade.ts"() {
2489
+ "use strict";
2490
+ messageOf = (error) => (error instanceof Error ? error.message || error.name : String(error)) || "Error";
2491
+ Score = class _Score {
2492
+ static RULE = "a score is a number from 0 to 1";
2493
+ static admits(value) {
2494
+ return Number.isFinite(value) && value >= 0 && value <= 1;
2495
+ }
2496
+ /** Every number among `scores` that is not a score, as `'<key>' = <value>`
2497
+ * and the rule, or null when all of them are. Pairs, not a map, so that no
2498
+ * key hides another. */
2499
+ static problem(scores2) {
2500
+ const refused = [...scores2].flatMap(
2501
+ ([key, value]) => typeof value === "number" && !_Score.admits(value) ? [`'${key}' = ${String(value)}`] : []
2502
+ );
2503
+ return refused.length ? `${refused.join(", ")}; ${_Score.RULE}` : null;
2504
+ }
2505
+ };
2506
+ GradeRefused = class extends Error {
2507
+ name = "GradeRefused";
2508
+ };
2509
+ GradePayload = class _GradePayload {
2510
+ /** A grade payload's numbers, checked: every number in it must be a score,
2511
+ * and its rewards and verifiers objects by name, or the grade is refused; a
2512
+ * cross-world verifier's grade that breaks either rule is that verifier's
2513
+ * error, the way the World Host answers one that could not grade. */
2514
+ static read(raw) {
2515
+ if (!_GradePayload.objectOrAbsent(raw))
2516
+ throw new GradeRefused("the grade is not an object");
2517
+ const record = _GradePayload.record(raw);
2518
+ const problem = _GradePayload.problem(record) ?? _GradePayload.verifiersProblem(record.verifiers);
2519
+ if (problem) throw new GradeRefused(problem);
2520
+ const verifiers = _GradePayload.verifiers(record.verifiers);
2521
+ return {
2522
+ ..._GradePayload.numbers(record),
2523
+ ...typeof record.reason === "string" ? { reason: record.reason } : {},
2524
+ ...typeof record.error === "string" ? { error: record.error } : {},
2525
+ ...verifiers ? { verifiers } : {}
2526
+ };
2527
+ }
2528
+ /** A world whose grade call failed (the platform refused it, the host had
2529
+ * lost the session): that world's error, never a score. */
2530
+ static failedCall(error) {
2531
+ return { ..._GradePayload.failed(messageOf(error)), raw: null };
2532
+ }
2533
+ static problem(record) {
2534
+ if (!_GradePayload.objectOrAbsent(record.rewards))
2535
+ return "the grade's rewards are not an object";
2536
+ const scores2 = Score.problem(_GradePayload.scores(record));
2537
+ if (scores2) return `the grade answered ${scores2}`;
2538
+ const twice = _GradePayload.twice(record);
2539
+ return twice.length ? `the grade answered ${twice.map((key) => `'${key}'`).join(", ")} both beside and inside rewards` : null;
2540
+ }
2541
+ static twice(record) {
2542
+ return Object.entries(_GradePayload.record(record.rewards)).filter(
2543
+ ([key, value]) => key !== "reward" && typeof value === "number" && typeof record[key] === "number"
2544
+ ).map(([key]) => key);
2545
+ }
2546
+ static verifiersProblem(value) {
2547
+ return _GradePayload.objectOrAbsent(value) ? null : "the grade's verifiers are not an object";
2548
+ }
2549
+ static scores(record) {
2550
+ return [
2551
+ ["reward", record.reward],
2552
+ ...Object.entries(_GradePayload.record(record.rewards)),
2553
+ ...Object.entries(record).filter(
2554
+ ([key]) => key !== "reward" && key !== "rewards"
2555
+ )
2556
+ ];
2557
+ }
2558
+ static isRecord(value) {
2559
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2560
+ }
2561
+ static objectOrAbsent(value) {
2562
+ return value === void 0 || value === null || _GradePayload.isRecord(value);
2563
+ }
2564
+ static record(value) {
2565
+ return _GradePayload.isRecord(value) ? value : {};
2566
+ }
2567
+ /** `reward`, and every numeric key under `rewards` (the host's shape) or at
2568
+ * the top level (a bundled verifier's). */
2569
+ static numbers(record) {
2570
+ const rewards = {};
2571
+ for (const [key, value] of Object.entries(
2572
+ _GradePayload.record(record.rewards)
2573
+ ))
2574
+ if (typeof value === "number") rewards[key] = value;
2575
+ for (const [key, value] of Object.entries(record))
2576
+ if (key !== "reward" && typeof value === "number") rewards[key] = value;
2577
+ return {
2578
+ reward: typeof record.reward === "number" ? record.reward : null,
2579
+ rewards
2580
+ };
2581
+ }
2582
+ static verifiers(value) {
2583
+ if (!_GradePayload.isRecord(value)) return void 0;
2584
+ return Object.fromEntries(
2585
+ Object.entries(value).map(([name, grade]) => [
2586
+ name,
2587
+ _GradePayload.verifier(name, grade)
2588
+ ])
2589
+ );
2590
+ }
2591
+ static verifier(name, grade) {
2592
+ if (!_GradePayload.isRecord(grade))
2593
+ return _GradePayload.failed(`the grade's verifier '${name}' is not an object`);
2594
+ const problem = _GradePayload.problem(grade);
2595
+ if (problem) return _GradePayload.failed(problem);
2596
+ return {
2597
+ ..._GradePayload.numbers(grade),
2598
+ ...typeof grade.error === "string" ? { error: grade.error } : {}
2599
+ };
2600
+ }
2601
+ static failed(error) {
2602
+ return { reward: null, rewards: {}, error };
2603
+ }
2604
+ };
2605
+ }
2606
+ });
2607
+
2302
2608
  // src/worlds/session.ts
2303
2609
  var session_exports2 = {};
2304
2610
  __export(session_exports2, {
2305
2611
  WorldSession: () => WorldSession,
2306
2612
  WorldSessionCallError: () => WorldSessionCallError,
2613
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
2307
2614
  WorldSessionTimeout: () => WorldSessionTimeout,
2308
2615
  attachSession: () => attachSession,
2309
2616
  isTaskIdRef: () => isTaskIdRef,
@@ -2315,34 +2622,6 @@ __export(session_exports2, {
2315
2622
  taskLabel: () => taskLabel
2316
2623
  });
2317
2624
  import { randomUUID } from "crypto";
2318
- function numericGrade(record) {
2319
- const rewards = {};
2320
- const nested = record.rewards;
2321
- if (nested && typeof nested === "object") {
2322
- for (const [key, value] of Object.entries(nested)) {
2323
- if (typeof value === "number") rewards[key] = value;
2324
- }
2325
- }
2326
- for (const [key, value] of Object.entries(record)) {
2327
- if (key !== "reward" && typeof value === "number") rewards[key] = value;
2328
- }
2329
- return {
2330
- reward: typeof record.reward === "number" ? record.reward : null,
2331
- rewards
2332
- };
2333
- }
2334
- function verifierGrades(value) {
2335
- if (!value || typeof value !== "object") return void 0;
2336
- const out = {};
2337
- for (const [name, grade] of Object.entries(value)) {
2338
- const record = grade && typeof grade === "object" ? grade : {};
2339
- out[name] = {
2340
- ...numericGrade(record),
2341
- ...typeof record.error === "string" ? { error: record.error } : {}
2342
- };
2343
- }
2344
- return out;
2345
- }
2346
2625
  function schemaFromSignature(spec) {
2347
2626
  if (spec.inputSchema)
2348
2627
  return {
@@ -2450,6 +2729,7 @@ async function runSessions(opts) {
2450
2729
  const taskRef = tasks[next++];
2451
2730
  let task = taskLabel(taskRef);
2452
2731
  let session = null;
2732
+ let keepWarm = opts.keepWarm ?? true;
2453
2733
  try {
2454
2734
  session = await openSession(opts.world, {
2455
2735
  task: taskRef,
@@ -2457,6 +2737,7 @@ async function runSessions(opts) {
2457
2737
  options
2458
2738
  });
2459
2739
  task = taskLabel(taskRef, session.taskInfo);
2740
+ WorldToolsUnread.refuse(opts.world.split("@", 1)[0], session.deprecations);
2460
2741
  await session.ready();
2461
2742
  const toolkit = await session.toolkit();
2462
2743
  const live = session;
@@ -2479,19 +2760,19 @@ async function runSessions(opts) {
2479
2760
  results.push(result);
2480
2761
  opts.onResult?.(result);
2481
2762
  } catch (error) {
2763
+ if (error instanceof WorldToolsUnread) keepWarm = false;
2482
2764
  const result = {
2483
2765
  task,
2484
2766
  sessionId: session?.sessionId ?? "",
2485
2767
  reward: 0,
2486
2768
  rewards: {},
2487
- // An Error with an empty message would read as "no error" to any
2488
- // caller checking truthiness; name the type instead.
2769
+ // An empty message reads as "no error" to a truthiness check; name the type instead.
2489
2770
  error: (error instanceof Error ? error.message : String(error)) || (error instanceof Error ? error.name : "Unknown error")
2490
2771
  };
2491
2772
  results.push(result);
2492
2773
  opts.onResult?.(result);
2493
2774
  } finally {
2494
- await session?.close({ keepWarm: opts.keepWarm ?? true }).catch(() => void 0);
2775
+ await session?.close({ keepWarm }).catch(() => void 0);
2495
2776
  }
2496
2777
  }
2497
2778
  })
@@ -2505,7 +2786,7 @@ async function runSessions(opts) {
2505
2786
  passed: failed === 0 && (opts.failUnder === void 0 || mean >= opts.failUnder)
2506
2787
  };
2507
2788
  }
2508
- var READY_TIMEOUT_MS, READY_POLL_MS, CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep, modelToolName, WorldSessionTimeout, WorldSessionCallError, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
2789
+ var CALL_TIMEOUT_MS, CALL_POLL_MS, JUDGED_GRADE_TIMEOUT_MS, JUDGED_GRADE_POLL_MS, EXPORT_PAGE, sleep3, modelToolName, WorldSession, OPEN_SESSION_OPTION_KEYS, isOpenSessionOptions;
2509
2790
  var init_session2 = __esm({
2510
2791
  "src/worlds/session.ts"() {
2511
2792
  "use strict";
@@ -2513,22 +2794,20 @@ var init_session2 = __esm({
2513
2794
  init_browser();
2514
2795
  init_nativeComputer();
2515
2796
  init_sessionContract();
2797
+ init_sessionReadiness();
2516
2798
  init_session();
2517
2799
  init_taskRef();
2800
+ init_worldNotice();
2801
+ init_grade();
2518
2802
  init_taskRef();
2519
- READY_TIMEOUT_MS = 10 * 6e4;
2520
- READY_POLL_MS = 2e3;
2803
+ init_sessionReadiness();
2521
2804
  CALL_TIMEOUT_MS = 3 * 6e4;
2522
2805
  CALL_POLL_MS = 400;
2523
2806
  JUDGED_GRADE_TIMEOUT_MS = 15 * 6e4;
2524
2807
  JUDGED_GRADE_POLL_MS = 2e3;
2525
2808
  EXPORT_PAGE = 500;
2526
- sleep = (ms) => new Promise((r) => setTimeout(r, ms));
2809
+ sleep3 = (ms) => new Promise((r) => setTimeout(r, ms));
2527
2810
  modelToolName = (name) => name.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64) || "tool";
2528
- WorldSessionTimeout = class extends Error {
2529
- };
2530
- WorldSessionCallError = class extends Error {
2531
- };
2532
2811
  WorldSession = class {
2533
2812
  constructor(sessionId, current, options) {
2534
2813
  this.sessionId = sessionId;
@@ -2646,18 +2925,17 @@ var init_session2 = __esm({
2646
2925
  }
2647
2926
  /** The reporter `browser()` uses; exposed for a page you drive yourself. */
2648
2927
  browserRecorder(agentId) {
2649
- const path12 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
2928
+ const path13 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
2650
2929
  const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
2651
2930
  return new BrowserActionRecorder(
2652
2931
  async (events) => {
2653
- await apiRequest(this.options, "POST", path12, { events });
2932
+ await apiRequest(this.options, "POST", path13, { events });
2654
2933
  },
2655
2934
  id,
2656
2935
  {
2657
- // Continue this agent's timeline where the platform has it, so the
2658
- // same id reopened later never re-numbers from zero.
2936
+ // Continue this agent's timeline where the platform has it, so a reopened id never re-numbers from zero.
2659
2937
  resumeFrom: async () => {
2660
- const summary = await apiRequest(this.options, "GET", path12);
2938
+ const summary = await apiRequest(this.options, "GET", path13);
2661
2939
  return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
2662
2940
  }
2663
2941
  }
@@ -2685,29 +2963,22 @@ var init_session2 = __esm({
2685
2963
  tools: this.current.tools,
2686
2964
  surfaces: this.current.surfaces,
2687
2965
  task: this.current.task,
2966
+ deprecations: this.current.deprecations,
2688
2967
  ...fresh
2689
2968
  };
2690
2969
  return this.current;
2691
2970
  }
2692
2971
  /**
2693
- * Wait until the container is warm — meaning the process inside it has
2694
- * asked the platform for work, not merely that a job was accepted.
2972
+ * Wait until the session answers calls: a World Host holds it, or the
2973
+ * process inside a pod has asked the platform for work. Throws
2974
+ * `WorldSessionOpenFailed` (with the platform's reason) when it ended
2975
+ * first, and `WorldSessionTimeout` when `timeoutMs` (default ten minutes)
2976
+ * ran out while it was still starting.
2695
2977
  */
2696
2978
  async ready(opts = {}) {
2697
- const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
2698
- for (; ; ) {
2699
- const state2 = await this.status();
2700
- if (state2.ready) return state2;
2701
- if (state2.state === "FAILED" || state2.state === "STOPPED")
2702
- throw new WorldSessionCallError(
2703
- `World session ${state2.state.toLowerCase()} before it was ready.`
2704
- );
2705
- if (Date.now() > deadline)
2706
- throw new WorldSessionTimeout(
2707
- `World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
2708
- );
2709
- await sleep(READY_POLL_MS);
2710
- }
2979
+ return new SessionReadiness(this.sessionId, () => this.status()).wait(
2980
+ opts.timeoutMs
2981
+ );
2711
2982
  }
2712
2983
  /** Enqueue one interaction and wait for the container's answer. */
2713
2984
  async exchange(body, timeoutMs) {
@@ -2737,7 +3008,7 @@ var init_session2 = __esm({
2737
3008
  throw new WorldSessionTimeout(
2738
3009
  judged ? `Grade ${callId} still ${call.state} after ${Math.round(budgetMs / 1e3)}s \u2014 the judge has not answered; the platform finalizes the call on its own, read it back with export().` : `Call ${callId} still ${call.state} after ${Math.round(budgetMs / 1e3)}s \u2014 the container may have died.`
2739
3010
  );
2740
- await sleep(pollMs2);
3011
+ await sleep3(pollMs2);
2741
3012
  }
2742
3013
  }
2743
3014
  /**
@@ -2786,7 +3057,9 @@ var init_session2 = __esm({
2786
3057
  * calls); assertions and python graders ignore it. `worlds` names the
2787
3058
  * task's other sessions by alias (`{alias: sessionId}`): the platform
2788
3059
  * reads each one's evidence and hands it to the cross-world verifiers
2789
- * anchored on this session, whose grades come back under `verifiers`. */
3060
+ * anchored on this session, whose grades come back under `verifiers`. A
3061
+ * score that is not a number from 0 to 1 is refused by key and value
3062
+ * (`GradeRefused`); a verifier's is that verifier's `error`. */
2790
3063
  async grade(opts = {}) {
2791
3064
  const raw = await this.exchange(
2792
3065
  {
@@ -2796,14 +3069,7 @@ var init_session2 = __esm({
2796
3069
  },
2797
3070
  opts.timeoutMs
2798
3071
  );
2799
- const record = raw && typeof raw === "object" ? raw : {};
2800
- const verifiers = verifierGrades(record.verifiers);
2801
- return {
2802
- ...numericGrade(record),
2803
- ...typeof record.reason === "string" ? { reason: record.reason } : {},
2804
- ...verifiers ? { verifiers } : {},
2805
- raw
2806
- };
3072
+ return { ...GradePayload.read(raw), raw };
2807
3073
  }
2808
3074
  /**
2809
3075
  * Put rows into the LIVE world through its contract, in one batch:
@@ -3067,26 +3333,28 @@ var init_surfaceDescriptor = __esm({
3067
3333
 
3068
3334
  // src/worlds/taskSessions.ts
3069
3335
  function aggregateTaskGrade(worlds) {
3070
- const rewards = {};
3071
- const ungraded = [];
3072
- const graded = [];
3336
+ const fold = new TaskGradeFold();
3073
3337
  const verifiers = {};
3074
- const fold = (key, grade) => {
3075
- if (typeof grade.reward === "number") {
3076
- rewards[key] = grade.reward;
3077
- graded.push(grade.reward);
3078
- } else ungraded.push(key);
3079
- for (const [name, value] of Object.entries(grade.rewards))
3080
- rewards[`${key}.${name}`] = value;
3081
- };
3082
3338
  for (const [alias, grade] of Object.entries(worlds)) {
3083
- fold(alias, grade);
3339
+ fold.add(alias, grade, `world ${alias}`);
3084
3340
  Object.assign(verifiers, grade.verifiers ?? {});
3085
3341
  }
3086
3342
  for (const [name, grade] of Object.entries(verifiers))
3087
- fold(`${VERIFIER_REWARD_PREFIX}${name}`, grade);
3088
- const reward = graded.length > 0 ? graded.reduce((a, b) => a + b, 0) / graded.length : null;
3089
- return { reward, rewards, worlds, ungraded, verifiers };
3343
+ fold.add(`${VERIFIER_REWARD_PREFIX}${name}`, grade, `verifier ${name}`);
3344
+ return {
3345
+ reward: fold.reward(),
3346
+ rewards: fold.rewards,
3347
+ worlds,
3348
+ ungraded: fold.ungraded,
3349
+ verifiers,
3350
+ errors: fold.errors
3351
+ };
3352
+ }
3353
+ function taskGradeError(grade) {
3354
+ const lines = Object.entries(grade.errors).map(
3355
+ ([key, why]) => `${key}: ${why}`
3356
+ );
3357
+ return lines.length ? lines.join("; ") : null;
3090
3358
  }
3091
3359
  function fromResponse(answer, options) {
3092
3360
  const sessions = {};
@@ -3209,12 +3477,13 @@ async function attachManifest(manifest, options = {}) {
3209
3477
  function isTaskManifest(value) {
3210
3478
  return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.sessions === "object" && value.sessions !== null && !("instruction" in value && "worlds" in value);
3211
3479
  }
3212
- var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskSessions;
3480
+ var TASK_SESSIONS, VERIFIER_REWARD_PREFIX, manifestSession, TaskGradeFold, TaskSessions;
3213
3481
  var init_taskSessions = __esm({
3214
3482
  "src/worlds/taskSessions.ts"() {
3215
3483
  "use strict";
3216
3484
  init_worlds();
3217
3485
  init_browser();
3486
+ init_grade();
3218
3487
  init_surfaceDescriptor();
3219
3488
  init_session2();
3220
3489
  TASK_SESSIONS = "/api/public/world-task-sessions";
@@ -3229,6 +3498,36 @@ var init_taskSessions = __esm({
3229
3498
  state: state2.state,
3230
3499
  ...surfaceDescriptor(s)
3231
3500
  });
3501
+ TaskGradeFold = class {
3502
+ rewards = {};
3503
+ ungraded = [];
3504
+ errors = {};
3505
+ sources = /* @__PURE__ */ new Map();
3506
+ graded = [];
3507
+ add(key, grade, source) {
3508
+ if (grade.error) this.errors[key] = grade.error;
3509
+ if (typeof grade.reward === "number") {
3510
+ this.put(key, grade.reward, source);
3511
+ this.graded.push(grade.reward);
3512
+ } else this.ungraded.push(key);
3513
+ for (const [name, value] of Object.entries(grade.rewards))
3514
+ this.put(`${key}.${name}`, value, source);
3515
+ }
3516
+ reward() {
3517
+ if (Object.keys(this.errors).length > 0 || !this.graded.length)
3518
+ return null;
3519
+ return this.graded.reduce((a, b) => a + b, 0) / this.graded.length;
3520
+ }
3521
+ put(key, value, source) {
3522
+ const first = this.sources.get(key) ?? source;
3523
+ this.sources.set(key, first);
3524
+ if (first !== source) {
3525
+ this.errors[key] = `two rewards are named ${key} (${first} and ${source}); rename one of them`;
3526
+ return;
3527
+ }
3528
+ this.rewards[key] = value;
3529
+ }
3530
+ };
3232
3531
  TaskSessions = class {
3233
3532
  constructor(taskId, taskVersion, name, instruction, sessions, worlds, hash = null) {
3234
3533
  this.taskId = taskId;
@@ -3307,11 +3606,13 @@ var init_taskSessions = __esm({
3307
3606
  * agent's own account of the whole investigation) goes to every world's
3308
3607
  * judge; a world graded by assertions or python ignores it. Each grade
3309
3608
  * names the task's other sessions by alias, so the cross-world verifiers
3310
- * anchored on that world can read them. */
3609
+ * anchored on that world can read them. A world whose grade call fails is
3610
+ * that world's error: the task's grade names it and keeps every other
3611
+ * world's grade. */
3311
3612
  async grade(opts = {}) {
3312
3613
  return aggregateTaskGrade(
3313
3614
  await this.each(
3314
- (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) })
3615
+ (session, alias) => session.grade({ ...opts, worlds: this.otherSessions(alias) }).catch((error) => GradePayload.failedCall(error))
3315
3616
  )
3316
3617
  );
3317
3618
  }
@@ -3359,12 +3660,16 @@ var init_evals = __esm({
3359
3660
  "src/worlds/evals.ts"() {
3360
3661
  "use strict";
3361
3662
  init_worlds();
3663
+ init_transport();
3362
3664
  EVALUATIONS = "/api/public/benchmark-containers/evaluations";
3363
3665
  GatewayEvalsClient = class {
3364
- constructor(options = {}) {
3666
+ constructor(options = {}, retry = RetryPolicy.FILING) {
3365
3667
  this.options = options;
3668
+ this.retry = retry;
3366
3669
  }
3367
3670
  options;
3671
+ retry;
3672
+ keyed = /* @__PURE__ */ new Set();
3368
3673
  async createEvaluation(versionId, opts = {}) {
3369
3674
  const body = { versionId };
3370
3675
  if (opts.model !== void 0) body.model = opts.model;
@@ -3372,12 +3677,18 @@ var init_evals = __esm({
3372
3677
  if (opts.rolloutsPerExample !== void 0)
3373
3678
  body.rolloutsPerExample = opts.rolloutsPerExample;
3374
3679
  if (opts.metadata !== void 0) body.metadata = opts.metadata;
3680
+ if (opts.idempotencyKey !== void 0)
3681
+ body.idempotencyKey = opts.idempotencyKey;
3375
3682
  const answer = await apiRequest(
3376
3683
  this.options,
3377
3684
  "POST",
3378
3685
  EVALUATIONS,
3379
- body
3686
+ body,
3687
+ {
3688
+ retry: opts.idempotencyKey === void 0 ? RetryPolicy.NONE : this.retry
3689
+ }
3380
3690
  );
3691
+ if (opts.idempotencyKey !== void 0) this.keyed.add(answer.evaluationId);
3381
3692
  return answer.evaluationId;
3382
3693
  }
3383
3694
  async pushSamples(evaluationId, samples) {
@@ -3385,7 +3696,8 @@ var init_evals = __esm({
3385
3696
  this.options,
3386
3697
  "POST",
3387
3698
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/samples`,
3388
- { samples }
3699
+ { samples },
3700
+ { retry: this.retryFor(evaluationId) }
3389
3701
  );
3390
3702
  return answer.inserted ?? samples.length;
3391
3703
  }
@@ -3398,10 +3710,300 @@ var init_evals = __esm({
3398
3710
  this.options,
3399
3711
  "POST",
3400
3712
  `${EVALUATIONS}/${encodeURIComponent(evaluationId)}/finalize`,
3401
- body
3713
+ body,
3714
+ { retry: this.retryFor(evaluationId) }
3402
3715
  );
3403
3716
  return answer.status;
3404
3717
  }
3718
+ retryFor(evaluationId) {
3719
+ return this.keyed.has(evaluationId) ? this.retry : RetryPolicy.NONE;
3720
+ }
3721
+ };
3722
+ }
3723
+ });
3724
+
3725
+ // src/worlds/runSignals.ts
3726
+ import { constants } from "os";
3727
+ var OWNER, RunSignals;
3728
+ var init_runSignals = __esm({
3729
+ "src/worlds/runSignals.ts"() {
3730
+ "use strict";
3731
+ OWNER = /* @__PURE__ */ Symbol("RunSignals");
3732
+ RunSignals = class _RunSignals {
3733
+ constructor(target = process) {
3734
+ this.target = target;
3735
+ }
3736
+ target;
3737
+ static SIGNALS = ["SIGINT", "SIGTERM"];
3738
+ cleanups = [];
3739
+ heardSignal = null;
3740
+ heardCount = 0;
3741
+ listening = false;
3742
+ left = false;
3743
+ listener = Object.assign(
3744
+ (signal) => this.heard(signal),
3745
+ { [OWNER]: this }
3746
+ );
3747
+ /** The signal that arrived, once one has: start nothing new. */
3748
+ get signal() {
3749
+ return this.owner().heardSignal;
3750
+ }
3751
+ async during(cleanup, work) {
3752
+ return this.owner().hold(cleanup, work);
3753
+ }
3754
+ owner() {
3755
+ const owners = this.target.listeners("SIGINT").map((listener) => listener[OWNER]);
3756
+ return owners.find((owner) => owner !== void 0) ?? this;
3757
+ }
3758
+ async hold(cleanup, work) {
3759
+ this.listen();
3760
+ this.cleanups.push(cleanup);
3761
+ try {
3762
+ return await work();
3763
+ } finally {
3764
+ this.cleanups.splice(this.cleanups.indexOf(cleanup), 1);
3765
+ if (!this.cleanups.length && this.heardSignal === null) this.unlisten();
3766
+ }
3767
+ }
3768
+ heard(signal) {
3769
+ this.heardCount += 1;
3770
+ if (this.heardCount > 2) return this.leave(signal);
3771
+ this.heardSignal ??= signal;
3772
+ void this.cleanUp(signal, this.heardCount === 2).finally(() => this.leave(signal));
3773
+ }
3774
+ async cleanUp(signal, urgent) {
3775
+ for (const cleanup of [...this.cleanups].reverse())
3776
+ await Promise.resolve().then(() => cleanup(signal, urgent)).catch(() => void 0);
3777
+ }
3778
+ leave(signal) {
3779
+ if (this.left) return;
3780
+ this.left = true;
3781
+ this.unlisten();
3782
+ if (this.target.listenerCount(signal) === 0)
3783
+ this.target.exit(128 + constants.signals[signal]);
3784
+ }
3785
+ listen() {
3786
+ if (this.listening) return;
3787
+ this.listening = true;
3788
+ for (const signal of _RunSignals.SIGNALS)
3789
+ this.target.on(signal, this.listener);
3790
+ }
3791
+ unlisten() {
3792
+ this.listening = false;
3793
+ for (const signal of _RunSignals.SIGNALS)
3794
+ this.target.off(signal, this.listener);
3795
+ }
3796
+ };
3797
+ }
3798
+ });
3799
+
3800
+ // src/worlds/taskRunDir.ts
3801
+ import * as fs from "fs";
3802
+ import * as path from "path";
3803
+ function sessionRecord({ api: api2, ui, browser, ...session }) {
3804
+ return {
3805
+ ...session,
3806
+ ...api2 ? { api: { url: api2.url } } : {},
3807
+ ...ui ? { ui: { ...ui, entry: withoutUiToken(ui.entry) } } : {},
3808
+ ...browser ? { browser: browserRecord(browser) } : {}
3809
+ };
3810
+ }
3811
+ var TaskRunDir, browserRecord;
3812
+ var init_taskRunDir = __esm({
3813
+ "src/worlds/taskRunDir.ts"() {
3814
+ "use strict";
3815
+ init_browser();
3816
+ TaskRunDir = class {
3817
+ constructor(path13) {
3818
+ this.path = path13;
3819
+ }
3820
+ path;
3821
+ writeManifest(manifest) {
3822
+ this.writeJson("manifest.json", {
3823
+ ...manifest,
3824
+ sessions: Object.fromEntries(
3825
+ Object.entries(manifest.sessions).map(([alias, session]) => [
3826
+ alias,
3827
+ sessionRecord(session)
3828
+ ])
3829
+ )
3830
+ });
3831
+ }
3832
+ writeJson(name, value) {
3833
+ this.write(name, JSON.stringify(value, null, 2));
3834
+ }
3835
+ write(name, text2) {
3836
+ fs.mkdirSync(this.path, { recursive: true });
3837
+ fs.writeFileSync(path.join(this.path, name), text2);
3838
+ }
3839
+ };
3840
+ browserRecord = ({ viewport, tier }) => ({
3841
+ viewport,
3842
+ ...tier ? { tier } : {}
3843
+ });
3844
+ }
3845
+ });
3846
+
3847
+ // src/worlds/taskRunOutcome.ts
3848
+ var TaskRunOutcome;
3849
+ var init_taskRunOutcome = __esm({
3850
+ "src/worlds/taskRunOutcome.ts"() {
3851
+ "use strict";
3852
+ init_grade();
3853
+ init_taskSessions();
3854
+ TaskRunOutcome = class {
3855
+ constructor(runId, live, model, runDir) {
3856
+ this.runId = runId;
3857
+ this.live = live;
3858
+ this.model = model;
3859
+ this.runDir = runDir;
3860
+ }
3861
+ runId;
3862
+ live;
3863
+ model;
3864
+ runDir;
3865
+ report = null;
3866
+ extra = {};
3867
+ notes = {};
3868
+ agentError = null;
3869
+ grade = null;
3870
+ traced = false;
3871
+ runError = null;
3872
+ fileError = null;
3873
+ evaluationId;
3874
+ evaluationStatus = null;
3875
+ started = Date.now();
3876
+ /** The run stopped short of a grade; the first reason stands. */
3877
+ stopped(error) {
3878
+ this.runError ??= messageOf(error).slice(0, 2e3);
3879
+ }
3880
+ agentRaised(error) {
3881
+ this.agentError = messageOf(error).slice(0, 2e3);
3882
+ }
3883
+ /** Every reason the run failed, on one line: the evaluation's
3884
+ * `metrics.error`. A run that never reached a grade has failed whatever
3885
+ * its reasons say. */
3886
+ failure() {
3887
+ const reasons = [
3888
+ this.agentError === null ? null : `agent raised: ${this.agentError}`,
3889
+ this.runError === null ? null : `the run stopped: ${this.runError}`,
3890
+ this.gradeError(),
3891
+ this.grade === null && this.runError === null ? "the run stopped before it was graded" : null
3892
+ ].filter((reason) => reason !== null);
3893
+ return reasons.length ? reasons.join("; ") : null;
3894
+ }
3895
+ async file(evals, setRunId) {
3896
+ let created;
3897
+ try {
3898
+ created = await evals.createEvaluation(this.ref(this.live.aliases[0]).versionId, {
3899
+ model: this.model,
3900
+ rolloutsPerExample: 1,
3901
+ idempotencyKey: this.runId,
3902
+ ...setRunId ? { runId: setRunId } : {},
3903
+ metadata: this.metadata()
3904
+ });
3905
+ await evals.pushSamples(created, [this.sample()]);
3906
+ this.evaluationStatus = await evals.finalizeEvaluation(created, this.finalized());
3907
+ this.evaluationId = created;
3908
+ } catch (error) {
3909
+ this.fileError = created ? `evaluation ${created}: ${messageOf(error)}` : messageOf(error);
3910
+ if (created)
3911
+ await evals.finalizeEvaluation(created, { status: "FAILED", metrics: { error: this.fileError } }).catch(() => void 0);
3912
+ }
3913
+ }
3914
+ summary() {
3915
+ const live = this.live;
3916
+ const { evaluationId: _agentEvaluationId, ...extra } = this.extra;
3917
+ return {
3918
+ ...extra,
3919
+ runId: this.runId,
3920
+ taskId: live.taskId,
3921
+ taskVersion: live.taskVersion,
3922
+ name: live.name,
3923
+ instruction: live.instruction ?? "",
3924
+ model: this.model,
3925
+ aliases: live.aliases,
3926
+ reward: this.grade?.reward ?? null,
3927
+ rewards: this.grade?.rewards ?? {},
3928
+ ungraded: this.grade?.ungraded ?? live.aliases,
3929
+ agentError: this.agentError,
3930
+ runError: this.runError,
3931
+ gradeError: this.gradeError(),
3932
+ fileError: this.fileError,
3933
+ evaluationStatus: this.evaluationStatus,
3934
+ report: this.report,
3935
+ traced: this.traced,
3936
+ runDir: this.runDir,
3937
+ durationSeconds: Math.round((Date.now() - this.started) / 10) / 100,
3938
+ notes: this.notes,
3939
+ filed: this.evaluationId !== void 0,
3940
+ ...this.evaluationId ? { evaluationId: this.evaluationId } : {}
3941
+ };
3942
+ }
3943
+ gradeError() {
3944
+ return this.grade ? taskGradeError(this.grade) : null;
3945
+ }
3946
+ ref(alias) {
3947
+ const { slug, ref } = this.live.worldRef(alias);
3948
+ const session = this.live.sessions[alias];
3949
+ return { slug, ref, versionId: session.versionId, sessionId: session.sessionId };
3950
+ }
3951
+ metadata() {
3952
+ const live = this.live;
3953
+ return {
3954
+ source: "worlds-task-run",
3955
+ task: { id: live.taskId, version: live.taskVersion, name: live.name, hash: live.hash },
3956
+ anchor: live.aliases[0],
3957
+ worlds: Object.fromEntries(live.aliases.map((alias) => [alias, this.ref(alias)])),
3958
+ runId: this.runId
3959
+ };
3960
+ }
3961
+ sample() {
3962
+ const live = this.live;
3963
+ const reward = this.grade?.reward ?? null;
3964
+ const gradeError = this.gradeError();
3965
+ const sample = {
3966
+ exampleId: live.name ?? live.taskId ?? this.runId,
3967
+ task: live.name ?? live.taskId ?? this.runId,
3968
+ rolloutIndex: 0,
3969
+ prompt: live.instruction,
3970
+ ...this.report === null ? {} : { completion: this.report },
3971
+ rewards: this.grade?.rewards ?? {},
3972
+ info: {
3973
+ sessionId: this.runId,
3974
+ worlds: Object.fromEntries(
3975
+ live.aliases.map((alias) => [
3976
+ alias,
3977
+ { ...this.ref(alias), reward: this.grade?.worlds[alias]?.reward ?? null }
3978
+ ])
3979
+ ),
3980
+ ungraded: this.grade?.ungraded ?? live.aliases,
3981
+ traced: this.traced,
3982
+ runDir: this.runDir,
3983
+ ...this.agentError ? { agentError: this.agentError } : {},
3984
+ ...this.runError ? { runError: this.runError } : {},
3985
+ ...gradeError ? { verifyError: gradeError } : {}
3986
+ }
3987
+ };
3988
+ if (typeof reward === "number")
3989
+ Object.assign(sample, { reward, score: reward, correct: reward >= 1 });
3990
+ return sample;
3991
+ }
3992
+ finalized() {
3993
+ const reward = this.grade?.reward ?? null;
3994
+ const failure = this.failure();
3995
+ return {
3996
+ status: failure ? "FAILED" : "COMPLETED",
3997
+ metrics: {
3998
+ ...typeof reward === "number" ? { avg_reward: reward } : {},
3999
+ reward,
4000
+ ...Object.fromEntries(
4001
+ Object.entries(this.grade?.rewards ?? {}).map(([key, value]) => [`rewards.${key}`, value])
4002
+ ),
4003
+ ...failure ? { error: failure } : {}
4004
+ }
4005
+ };
4006
+ }
3405
4007
  };
3406
4008
  }
3407
4009
  });
@@ -3414,7 +4016,7 @@ function getEnv(key) {
3414
4016
  function isGatewayConfigured() {
3415
4017
  return !!(getEnv(GATEWAY_ENV.HOST) && getEnv(GATEWAY_ENV.PUBLIC_KEY) && getEnv(GATEWAY_ENV.SECRET_KEY));
3416
4018
  }
3417
- var GATEWAY_ENV, OPENTELEMETRY_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
4019
+ var GATEWAY_ENV, OTLP_TRACES_PATH, OPENTELEMETRY_PEERS, PINNED_PEERS, MODULE_NOT_FOUND, OpenTelemetryMissing;
3418
4020
  var init_prerequisites = __esm({
3419
4021
  "src/tracing/prerequisites.ts"() {
3420
4022
  "use strict";
@@ -3422,21 +4024,25 @@ var init_prerequisites = __esm({
3422
4024
  HOST: "GATEWAY_HOST",
3423
4025
  PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
3424
4026
  SECRET_KEY: "GATEWAY_SECRET_KEY",
4027
+ API_KEY: "GATEWAY_API_KEY",
3425
4028
  ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
3426
4029
  };
3427
- OPENTELEMETRY_PEERS = [
3428
- "@opentelemetry/api",
3429
- "@opentelemetry/exporter-trace-otlp-http",
3430
- "@opentelemetry/resources",
3431
- "@opentelemetry/sdk-trace-base",
3432
- "@opentelemetry/sdk-trace-node"
3433
- ];
4030
+ OTLP_TRACES_PATH = "/api/public/otel/v1/traces";
4031
+ OPENTELEMETRY_PEERS = {
4032
+ "@opentelemetry/api": "1.9.0",
4033
+ "@opentelemetry/exporter-trace-otlp-http": "0.222.0",
4034
+ "@opentelemetry/resources": "2.11.0",
4035
+ "@opentelemetry/sdk-trace-base": "2.11.0",
4036
+ "@opentelemetry/sdk-trace-node": "2.11.0"
4037
+ };
4038
+ PINNED_PEERS = Object.entries(OPENTELEMETRY_PEERS).map(([name, version]) => `${name}@${version}`).join(" ");
3434
4039
  MODULE_NOT_FOUND = /* @__PURE__ */ new Set(["MODULE_NOT_FOUND", "ERR_MODULE_NOT_FOUND"]);
3435
4040
  OpenTelemetryMissing = class _OpenTelemetryMissing extends Error {
3436
4041
  constructor() {
3437
4042
  super(
3438
- `OpenTelemetry packages not found. Install them with:
3439
- npm install ${OPENTELEMETRY_PEERS.join(" ")}`
4043
+ `OpenTelemetry packages not found. Install them next to @withgateway/sdk, at the versions it is tested with:
4044
+ npm install ${PINNED_PEERS}
4045
+ npm install -g ${PINNED_PEERS} (the gateway CLI, when it was installed with npm install -g)`
3440
4046
  );
3441
4047
  this.name = "OpenTelemetryMissing";
3442
4048
  }
@@ -3611,10 +4217,10 @@ var init_users = __esm({
3611
4217
  totalItems: Number(meta.totalItems ?? rows.length)
3612
4218
  };
3613
4219
  }
3614
- async requestJson(method, path12, payload) {
4220
+ async requestJson(method, path13, payload) {
3615
4221
  let response;
3616
4222
  try {
3617
- response = await fetch(`${this.host}${path12}`, {
4223
+ response = await fetch(`${this.host}${path13}`, {
3618
4224
  method,
3619
4225
  headers: {
3620
4226
  Authorization: this.authHeader(),
@@ -3625,17 +4231,17 @@ var init_users = __esm({
3625
4231
  });
3626
4232
  } catch (error) {
3627
4233
  throw new UserProfileError(
3628
- `${method} ${path12} failed: ${error instanceof Error ? error.message : String(error)}`,
4234
+ `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
3629
4235
  { cause: error }
3630
4236
  );
3631
4237
  }
3632
4238
  const text2 = await response.text();
3633
4239
  if (!response.ok) {
3634
4240
  throw new UserProfileError(
3635
- `${method} ${path12} failed (${response.status}): ${text2.slice(0, 500)}`
4241
+ `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
3636
4242
  );
3637
4243
  }
3638
- this.log("%s %s -> %s", method, path12, response.status);
4244
+ this.log("%s %s -> %s", method, path13, response.status);
3639
4245
  return safeJson(text2);
3640
4246
  }
3641
4247
  authHeader() {
@@ -4080,10 +4686,10 @@ var init_scores = __esm({
4080
4686
  metadata: signalMetadata
4081
4687
  });
4082
4688
  }
4083
- async request(method, path12, payload) {
4689
+ async request(method, path13, payload) {
4084
4690
  let response;
4085
4691
  try {
4086
- response = await fetch(`${this.host}${path12}`, {
4692
+ response = await fetch(`${this.host}${path13}`, {
4087
4693
  method,
4088
4694
  headers: {
4089
4695
  Authorization: this.authHeader(),
@@ -4094,17 +4700,17 @@ var init_scores = __esm({
4094
4700
  });
4095
4701
  } catch (error) {
4096
4702
  throw new ScoreError(
4097
- `${method} ${path12} failed: ${error instanceof Error ? error.message : String(error)}`,
4703
+ `${method} ${path13} failed: ${error instanceof Error ? error.message : String(error)}`,
4098
4704
  { cause: error }
4099
4705
  );
4100
4706
  }
4101
4707
  const text2 = await response.text();
4102
4708
  if (!response.ok) {
4103
4709
  throw new ScoreError(
4104
- `${method} ${path12} failed (${response.status}): ${text2.slice(0, 500)}`
4710
+ `${method} ${path13} failed (${response.status}): ${text2.slice(0, 500)}`
4105
4711
  );
4106
4712
  }
4107
- this.log("%s %s -> %s", method, path12, response.status);
4713
+ this.log("%s %s -> %s", method, path13, response.status);
4108
4714
  }
4109
4715
  authHeader() {
4110
4716
  return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
@@ -4132,7 +4738,7 @@ var require_package = __commonJS({
4132
4738
  "package.json"(exports, module) {
4133
4739
  module.exports = {
4134
4740
  name: "@withgateway/sdk",
4135
- version: "0.3.1",
4741
+ version: "0.3.3",
4136
4742
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
4137
4743
  main: "./dist/index.js",
4138
4744
  module: "./dist/index.mjs",
@@ -4553,7 +5159,7 @@ function buildProvider(config, logFn) {
4553
5159
  const host = config.host ?? getEnv(GATEWAY_ENV.HOST);
4554
5160
  const publicKey = config.publicKey ?? getEnv(GATEWAY_ENV.PUBLIC_KEY);
4555
5161
  const secretKey = config.secretKey ?? getEnv(GATEWAY_ENV.SECRET_KEY);
4556
- const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${DEFAULT_OTLP_PATH}` : void 0);
5162
+ const endpoint = config.endpoint ?? getEnv(GATEWAY_ENV.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${OTLP_TRACES_PATH}` : void 0);
4557
5163
  const missing = [];
4558
5164
  if (!host) missing.push("host (or GATEWAY_HOST env var)");
4559
5165
  if (!publicKey) missing.push("publicKey (or GATEWAY_PUBLIC_KEY env var)");
@@ -4796,7 +5402,7 @@ async function shutdown() {
4796
5402
  function isInitialized() {
4797
5403
  return state.initialized;
4798
5404
  }
4799
- var DEFAULT_OTLP_PATH, state;
5405
+ var state;
4800
5406
  var init_init = __esm({
4801
5407
  "src/tracing/init.ts"() {
4802
5408
  "use strict";
@@ -4807,7 +5413,6 @@ var init_init = __esm({
4807
5413
  init_build();
4808
5414
  init_prerequisites();
4809
5415
  init_prerequisites();
4810
- DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
4811
5416
  state = {
4812
5417
  initialized: false,
4813
5418
  provider: null
@@ -4816,39 +5421,78 @@ var init_init = __esm({
4816
5421
  });
4817
5422
 
4818
5423
  // src/worlds/taskTracing.ts
4819
- var TaskTracing;
5424
+ var HOST_WITH_AUTHORITY, TRACING_HELD_ELSEWHERE, TaskTracing;
4820
5425
  var init_taskTracing = __esm({
4821
5426
  "src/worlds/taskTracing.ts"() {
4822
5427
  "use strict";
4823
5428
  init_prerequisites();
5429
+ init_platformEndpoint();
5430
+ HOST_WITH_AUTHORITY = /^[a-z][a-z0-9+.-]*:\/\/[^/\\?#@\s]+(\/|$)/i;
5431
+ TRACING_HELD_ELSEWHERE = "Tracing in this process already sends spans to another destination or with other keys (an earlier run, or the agent's own tracing.init()), so this run's spans would not reach its own project.";
4824
5432
  TaskTracing = class _TaskTracing {
4825
5433
  constructor(load = () => Promise.resolve().then(() => (init_init(), init_exports))) {
4826
5434
  this.load = load;
4827
5435
  }
4828
5436
  load;
4829
5437
  static warned = false;
4830
- async start(requested) {
4831
- if (!requested || !isGatewayConfigured()) return false;
5438
+ static target = null;
5439
+ async start(requested, options) {
5440
+ if (!requested) return false;
5441
+ const endpoint = platformEndpoint(options);
4832
5442
  try {
4833
- (await this.load()).init();
5443
+ await this.initOnce(_TaskTracing.config(endpoint));
4834
5444
  return true;
4835
5445
  } catch (error) {
4836
- _TaskTracing.warnOnce(error);
5446
+ _TaskTracing.warnOnce(_TaskTracing.reason(error, endpoint));
4837
5447
  return false;
4838
5448
  }
4839
5449
  }
4840
- static warnOnce(error) {
5450
+ // init() keeps the first provider for the whole process and ignores later calls, so a later run is
5451
+ // traced only when it exports to the same place with the same keys.
5452
+ async initOnce(config) {
5453
+ const tracing = await this.load();
5454
+ const target = JSON.stringify([config.endpoint, config.publicKey, config.secretKey]);
5455
+ if (!tracing.isInitialized()) {
5456
+ tracing.init(config);
5457
+ _TaskTracing.target = target;
5458
+ return;
5459
+ }
5460
+ if (_TaskTracing.target !== target) throw new Error(TRACING_HELD_ELSEWHERE);
5461
+ }
5462
+ static config(endpoint) {
5463
+ if (!endpoint) throw new Error(GATEWAY_KEYS_MISSING);
5464
+ return { ...endpoint, endpoint: _TaskTracing.destination(endpoint) };
5465
+ }
5466
+ // B1: a saved login's keys go to their own host only. The redirect is compared as text, never parsed:
5467
+ // a fixed "<host>/" prefix leaves no URL parser room to read another host into it.
5468
+ static destination(endpoint) {
5469
+ const redirect = getEnv(GATEWAY_ENV.ENDPOINT);
5470
+ if (!redirect) return `${endpoint.host}${OTLP_TRACES_PATH}`;
5471
+ if (_TaskTracing.underHost(redirect, endpoint.host) || _TaskTracing.environmentKeys(endpoint)) return redirect;
5472
+ throw new Error(
5473
+ `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.`
5474
+ );
5475
+ }
5476
+ static underHost(redirect, host) {
5477
+ return HOST_WITH_AUTHORITY.test(host) && redirect.startsWith(`${host}/`);
5478
+ }
5479
+ static environmentKeys(endpoint) {
5480
+ const secretKey = getEnv(GATEWAY_ENV.SECRET_KEY) ?? getEnv(GATEWAY_ENV.API_KEY);
5481
+ return endpoint.publicKey === getEnv(GATEWAY_ENV.PUBLIC_KEY) && endpoint.secretKey === secretKey;
5482
+ }
5483
+ static warnOnce(reason) {
4841
5484
  if (_TaskTracing.warned) return;
4842
5485
  _TaskTracing.warned = true;
4843
5486
  console.warn(
4844
- `[gatewaysdk.tracing] Task runs in this process are not traced. ${_TaskTracing.reason(error)}
5487
+ `[gatewaysdk.tracing] Task runs in this process are not traced. ${reason}
4845
5488
  Pass --no-trace (trace: false) to run untraced without this warning.`
4846
5489
  );
4847
5490
  }
4848
- static reason(error) {
4849
- if (OpenTelemetryMissing.explains(error))
4850
- return new OpenTelemetryMissing().message;
4851
- return error instanceof Error ? error.message : String(error);
5491
+ static reason(error, endpoint) {
5492
+ if (OpenTelemetryMissing.explains(error)) return new OpenTelemetryMissing().message;
5493
+ const message = error instanceof Error ? error.message : String(error);
5494
+ const keys = endpoint ? [endpoint.publicKey, endpoint.secretKey] : [];
5495
+ return keys.reduce((text2, key) => text2.split(key).join("[redacted]"), message);
4852
5496
  }
4853
5497
  };
4854
5498
  }
@@ -4856,87 +5500,17 @@ Pass --no-trace (trace: false) to run untraced without this warning.`
4856
5500
 
4857
5501
  // src/worlds/taskRun.ts
4858
5502
  import { randomBytes } from "crypto";
4859
- import * as fs from "fs";
4860
- import * as path from "path";
4861
- function worldRef(live, alias) {
4862
- const { slug, ref } = live.worldRef(alias);
4863
- const session = live.sessions[alias];
4864
- return {
4865
- slug,
4866
- ref,
4867
- versionId: session.versionId,
4868
- sessionId: session.sessionId
4869
- };
4870
- }
4871
- async function fileTaskRun(live, runId, model, report, grade, agentError, traced, runDir, options, setRunId) {
4872
- const aliases = live.aliases;
4873
- const anchor = aliases[0];
4874
- const worldsMeta = Object.fromEntries(
4875
- aliases.map((alias) => [alias, worldRef(live, alias)])
4876
- );
4877
- const evals = new GatewayEvalsClient(options);
4878
- const evaluationId = await evals.createEvaluation(
4879
- worldRef(live, anchor).versionId,
4880
- {
4881
- model,
4882
- rolloutsPerExample: 1,
4883
- ...setRunId ? { runId: setRunId } : {},
4884
- metadata: {
4885
- source: "worlds-task-run",
4886
- task: {
4887
- id: live.taskId,
4888
- version: live.taskVersion,
4889
- name: live.name,
4890
- hash: live.hash
4891
- },
4892
- anchor,
4893
- worlds: worldsMeta,
4894
- runId
4895
- }
4896
- }
4897
- );
4898
- const reward = grade.reward;
4899
- const sample = {
4900
- exampleId: live.name ?? live.taskId ?? runId,
4901
- task: live.name ?? live.taskId ?? runId,
4902
- rolloutIndex: 0,
4903
- prompt: live.instruction,
4904
- ...report === null ? {} : { completion: report },
4905
- rewards: grade.rewards,
4906
- info: {
4907
- sessionId: runId,
4908
- worlds: Object.fromEntries(
4909
- aliases.map((alias) => [
4910
- alias,
4911
- {
4912
- ...worldRef(live, alias),
4913
- reward: grade.worlds[alias]?.reward ?? null
4914
- }
4915
- ])
4916
- ),
4917
- ungraded: grade.ungraded,
4918
- traced,
4919
- runDir,
4920
- ...agentError ? { agentError } : {}
4921
- }
4922
- };
4923
- if (typeof reward === "number") {
4924
- sample.reward = reward;
4925
- sample.score = reward;
4926
- sample.correct = reward >= 1;
4927
- }
4928
- await evals.pushSamples(evaluationId, [sample]);
4929
- await evals.finalizeEvaluation(evaluationId, {
4930
- status: agentError ? "FAILED" : "COMPLETED",
4931
- metrics: {
4932
- ...typeof reward === "number" ? { avg_reward: reward } : {},
4933
- reward,
4934
- ...Object.fromEntries(
4935
- Object.entries(grade.rewards).map(([k, v]) => [`rewards.${k}`, v])
4936
- )
4937
- }
4938
- });
4939
- return evaluationId;
5503
+ import * as path2 from "path";
5504
+ function taskRunFailure(summary) {
5505
+ if (summary.agentError !== null) return `agent raised: ${summary.agentError}`;
5506
+ if (summary.runError !== null) return `the run stopped: ${summary.runError}`;
5507
+ if (summary.gradeError !== null)
5508
+ return `the task could not be graded: ${summary.gradeError}`;
5509
+ if (summary.fileError !== null)
5510
+ return `the run could not be filed: ${summary.fileError}`;
5511
+ if (summary.filed && summary.evaluationStatus !== "COMPLETED")
5512
+ return `the platform settled evaluation ${summary.evaluationId} as ${summary.evaluationStatus}`;
5513
+ return null;
4940
5514
  }
4941
5515
  function reportFromResult(result) {
4942
5516
  if (result === void 0 || result === null)
@@ -4954,110 +5528,47 @@ function reportFromResult(result) {
4954
5528
  );
4955
5529
  return { report, extra };
4956
5530
  }
5531
+ async function refuseUnreadTools(live) {
5532
+ try {
5533
+ for (const alias of live.aliases)
5534
+ WorldToolsUnread.refuse(`${live.worldRef(alias).slug} (alias ${alias})`, live.session(alias).deprecations);
5535
+ } catch (error) {
5536
+ await live.close({ keepWarm: false }).catch(() => void 0);
5537
+ throw error;
5538
+ }
5539
+ }
4957
5540
  async function runTask(ref, agent, opts) {
4958
5541
  const runId = `wtr-${randomBytes(6).toString("hex")}`;
4959
- const outDir = opts.outDir ?? "runs";
4960
- const runDir = path.join(outDir, runId);
4961
- fs.mkdirSync(runDir, { recursive: true });
4962
5542
  const options = opts.options ?? {};
4963
- const live = await openTask(ref, {
4964
- surfaces: opts.surfaces,
4965
- reuseWarm: opts.reuseWarm,
4966
- browserTier: opts.browserTier,
4967
- options
4968
- });
4969
- const started = Date.now();
4970
- try {
4971
- await live.ready();
4972
- const traced = await new TaskTracing().start(opts.trace !== false);
4973
- const taskRun = new TaskRun(
4974
- runId,
4975
- live.instruction ?? "",
4976
- opts.model,
4977
- live.sessions,
4978
- await live.manifest()
4979
- );
4980
- let agentError = null;
4981
- let report = null;
4982
- let extra = {};
4983
- try {
4984
- const invoke = () => agent(taskRun);
4985
- const result = traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(runId, invoke) : await invoke();
4986
- ({ report, extra } = reportFromResult(result));
4987
- } catch (error) {
4988
- const message = (error instanceof Error ? error.message : String(error)) || "Unknown error";
4989
- agentError = message.slice(0, 2e3);
4990
- }
4991
- if (traced)
4992
- await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing) => tracing.flush()).catch(() => void 0);
4993
- const grade = report !== null ? await live.grade({ report }) : await live.grade();
4994
- fs.writeFileSync(
4995
- path.join(runDir, "manifest.json"),
4996
- JSON.stringify(taskRun.manifest, null, 2)
4997
- );
4998
- fs.writeFileSync(
4999
- path.join(runDir, "grades.json"),
5000
- JSON.stringify(grade, null, 2)
5001
- );
5002
- if (report) fs.writeFileSync(path.join(runDir, "report.md"), report);
5003
- const summary = {
5004
- runId,
5005
- taskId: live.taskId,
5006
- taskVersion: live.taskVersion,
5007
- name: live.name,
5008
- instruction: taskRun.instruction,
5009
- model: opts.model,
5010
- aliases: live.aliases,
5011
- reward: grade.reward,
5012
- rewards: grade.rewards,
5013
- ungraded: grade.ungraded,
5014
- agentError,
5015
- report,
5016
- traced,
5017
- runDir,
5018
- durationSeconds: Math.round((Date.now() - started) / 10) / 100,
5019
- notes: taskRun.notes,
5020
- filed: false,
5021
- ...extra
5022
- };
5023
- if (opts.file !== false) {
5024
- summary.evaluationId = await fileTaskRun(
5025
- live,
5026
- runId,
5027
- opts.model,
5028
- report,
5029
- grade,
5030
- agentError,
5031
- traced,
5032
- runDir,
5033
- options,
5034
- opts.setRunId
5035
- );
5036
- summary.filed = true;
5037
- }
5038
- fs.writeFileSync(
5039
- path.join(runDir, "run.json"),
5040
- JSON.stringify(summary, null, 2)
5041
- );
5042
- return summary;
5043
- } finally {
5044
- if (!opts.keepUp) {
5045
- try {
5046
- await live.close({ keepWarm: false });
5047
- } catch {
5048
- }
5049
- }
5050
- }
5543
+ const opening = new TaskOpening(
5544
+ openTask(ref, {
5545
+ surfaces: opts.surfaces,
5546
+ reuseWarm: opts.reuseWarm,
5547
+ browserTier: opts.browserTier,
5548
+ options
5549
+ }).then(async (live) => {
5550
+ await refuseUnreadTools(live);
5551
+ return new LiveTaskRun(runId, live, opts, options);
5552
+ })
5553
+ );
5554
+ return new RunSignals().during(
5555
+ (signal, urgent) => opening.interrupt(signal, urgent),
5556
+ () => opening.complete(agent)
5557
+ );
5051
5558
  }
5052
- var TaskRun, describeValue, RESULT_CONTRACT;
5559
+ var TaskRun, FILING_UNFINISHED, describeValue, RESULT_CONTRACT, LiveTaskRun, TaskOpening;
5053
5560
  var init_taskRun = __esm({
5054
5561
  "src/worlds/taskRun.ts"() {
5055
5562
  "use strict";
5056
5563
  init_browser();
5057
5564
  init_evals();
5565
+ init_runSignals();
5058
5566
  init_session2();
5059
5567
  init_taskSessions();
5568
+ init_taskRunDir();
5569
+ init_taskRunOutcome();
5060
5570
  init_taskTracing();
5571
+ init_worldNotice();
5061
5572
  TaskRun = class {
5062
5573
  constructor(runId, instruction, model, sessions, manifest) {
5063
5574
  this.runId = runId;
@@ -5120,8 +5631,127 @@ var init_taskRun = __esm({
5120
5631
  Object.assign(this.notes, notes);
5121
5632
  }
5122
5633
  };
5634
+ FILING_UNFINISHED = "the filing did not finish";
5123
5635
  describeValue = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
5124
5636
  RESULT_CONTRACT = "return a string report, an object {report?: string, ...} or nothing";
5637
+ LiveTaskRun = class {
5638
+ constructor(runId, live, opts, options) {
5639
+ this.live = live;
5640
+ this.opts = opts;
5641
+ this.options = options;
5642
+ this.files = new TaskRunDir(path2.join(opts.outDir ?? "runs", runId));
5643
+ this.outcome = new TaskRunOutcome(runId, live, opts.model, this.files.path);
5644
+ }
5645
+ live;
5646
+ opts;
5647
+ options;
5648
+ files;
5649
+ outcome;
5650
+ played = false;
5651
+ interrupted = false;
5652
+ filing = null;
5653
+ ending = null;
5654
+ async complete(agent) {
5655
+ try {
5656
+ await this.play(agent).catch((error) => this.outcome.stopped(error));
5657
+ this.played = true;
5658
+ await this.file();
5659
+ return this.record();
5660
+ } finally {
5661
+ await this.end();
5662
+ }
5663
+ }
5664
+ async interrupt(signal, urgent = false) {
5665
+ const sessions = this.opts.keepUp ? "leaving its sessions up" : "closing its sessions";
5666
+ const how = urgent ? " now, without waiting" : "";
5667
+ process.stderr.write(`${this.outcome.runId} interrupted by ${signal}; ${sessions}${how}
5668
+ `);
5669
+ if (!this.played) this.outcome.stopped(`interrupted by ${signal}`);
5670
+ this.interrupted = true;
5671
+ try {
5672
+ if (!urgent) await this.filing;
5673
+ this.record();
5674
+ } catch (error) {
5675
+ const why = error instanceof Error ? error.message : String(error);
5676
+ process.stderr.write(`${this.outcome.runId}: run.json not written: ${why}
5677
+ `);
5678
+ } finally {
5679
+ await this.end();
5680
+ }
5681
+ }
5682
+ summary() {
5683
+ return this.outcome.summary();
5684
+ }
5685
+ async play(agent) {
5686
+ await this.live.ready();
5687
+ this.outcome.traced = await new TaskTracing().start(this.opts.trace !== false, this.options);
5688
+ const run = new TaskRun(
5689
+ this.outcome.runId,
5690
+ this.live.instruction ?? "",
5691
+ this.opts.model,
5692
+ this.live.sessions,
5693
+ await this.live.manifest()
5694
+ );
5695
+ this.outcome.notes = run.notes;
5696
+ this.files.writeManifest(run.manifest);
5697
+ await this.callAgent(agent, run);
5698
+ const report = this.outcome.report;
5699
+ this.outcome.grade = await this.live.grade(report !== null ? { report } : {});
5700
+ this.files.writeJson("grades.json", this.outcome.grade);
5701
+ if (report) this.files.write("report.md", report);
5702
+ }
5703
+ async callAgent(agent, run) {
5704
+ try {
5705
+ const invoke = () => agent(run);
5706
+ const result = this.outcome.traced ? await (await Promise.resolve().then(() => (init_session(), session_exports))).withSession(run.runId, invoke) : await invoke();
5707
+ ({ report: this.outcome.report, extra: this.outcome.extra } = reportFromResult(result));
5708
+ } catch (error) {
5709
+ this.outcome.agentRaised(error);
5710
+ }
5711
+ if (this.outcome.traced)
5712
+ await Promise.resolve().then(() => (init_init(), init_exports)).then((tracing) => tracing.flush()).catch(() => void 0);
5713
+ }
5714
+ async file() {
5715
+ if (this.opts.file === false || this.interrupted) return;
5716
+ this.files.writeJson("run.json", { ...this.outcome.summary(), fileError: FILING_UNFINISHED });
5717
+ this.filing = this.outcome.file(new GatewayEvalsClient(this.options), this.opts.setRunId);
5718
+ await this.filing;
5719
+ }
5720
+ record() {
5721
+ const summary = this.outcome.summary();
5722
+ this.files.writeJson("run.json", summary);
5723
+ return summary;
5724
+ }
5725
+ end() {
5726
+ this.ending ??= this.opts.keepUp ? Promise.resolve() : this.live.close({ keepWarm: false }).then(
5727
+ () => void 0,
5728
+ () => void 0
5729
+ );
5730
+ return this.ending;
5731
+ }
5732
+ };
5733
+ TaskOpening = class {
5734
+ interrupting = null;
5735
+ run = null;
5736
+ opened;
5737
+ constructor(opening) {
5738
+ this.opened = opening.then((run) => this.run = run);
5739
+ }
5740
+ async complete(agent) {
5741
+ const run = await this.opened;
5742
+ if (!this.interrupting) return run.complete(agent);
5743
+ await this.interrupting;
5744
+ return run.summary();
5745
+ }
5746
+ interrupt(signal, urgent = false) {
5747
+ if (urgent) return this.run?.interrupt(signal, true) ?? Promise.resolve();
5748
+ this.interrupting ??= this.opened.then(
5749
+ (run) => run.interrupt(signal),
5750
+ () => void 0
5751
+ );
5752
+ return this.interrupting;
5753
+ }
5754
+ };
5125
5755
  }
5126
5756
  });
5127
5757
 
@@ -5201,63 +5831,101 @@ async function runSet(datasetId, agent, opts) {
5201
5831
  source: opts.source ?? "sdk",
5202
5832
  ...git && Object.keys(git).length ? { git } : {}
5203
5833
  });
5204
- const close = (status) => apiRequest(
5205
- options,
5206
- "POST",
5207
- `${SET_RUNS}/${encodeURIComponent(opened.id)}/close`,
5208
- { status }
5209
- );
5210
- const onInterrupt = () => {
5211
- void close("failed").finally(() => process.exit(130));
5212
- };
5213
- process.once("SIGINT", onInterrupt);
5214
- const planned = await runPlannedTasks(opened, agent, opts, options).finally(
5215
- () => process.off("SIGINT", onInterrupt)
5216
- );
5217
- const { tasks, failed } = planned;
5218
- const requested = failed ? "failed" : "completed";
5219
- const setRun = await close(requested);
5220
- return {
5221
- setRun,
5222
- tasks,
5223
- failed: failed || setRun.status !== "completed",
5224
- closedAsRequested: setRun.status === requested
5225
- };
5226
- }
5227
- async function runPlannedTasks(opened, agent, opts, options) {
5228
- const tasks = [];
5229
- let failed = false;
5230
- for (const task of opened.tasks) {
5231
- try {
5232
- const summary = await runTask(task.worldTaskId, agent, {
5233
- model: opts.model,
5234
- surfaces: opts.surfaces,
5235
- browserTier: opts.browserTier,
5236
- outDir: opts.outDir,
5237
- trace: opts.trace,
5238
- setRunId: opened.id,
5239
- options
5240
- });
5241
- tasks.push(summary);
5242
- failed ||= summary.agentError !== null;
5243
- opts.onTask?.(summary);
5244
- } catch (error) {
5245
- failed = true;
5246
- const message = error instanceof Error ? error.message : String(error);
5247
- process.stderr.write(`task ${task.name} could not run: ${message}
5248
- `);
5249
- }
5250
- }
5251
- return { tasks, failed };
5834
+ return new SetRunner(opened, agent, opts, options).run();
5252
5835
  }
5253
- var SET_RUNS;
5836
+ var SET_RUNS, SetRunner;
5254
5837
  var init_setRun = __esm({
5255
5838
  "src/worlds/setRun.ts"() {
5256
5839
  "use strict";
5257
5840
  init_worlds();
5258
5841
  init_checkoutProvenance();
5842
+ init_runSignals();
5259
5843
  init_taskRun();
5260
5844
  SET_RUNS = "/api/public/set-runs";
5845
+ SetRunner = class {
5846
+ constructor(opened, agent, opts, options) {
5847
+ this.opened = opened;
5848
+ this.agent = agent;
5849
+ this.opts = opts;
5850
+ this.options = options;
5851
+ }
5852
+ opened;
5853
+ agent;
5854
+ opts;
5855
+ options;
5856
+ tasks = [];
5857
+ failures = [];
5858
+ failed = false;
5859
+ current = null;
5860
+ interrupted = null;
5861
+ signals = new RunSignals();
5862
+ async run() {
5863
+ await this.signals.during(
5864
+ (signal2) => this.interrupt(signal2).then(() => void 0),
5865
+ () => this.runTasks()
5866
+ );
5867
+ const signal = this.signals.signal;
5868
+ if (signal !== null) return this.result(await this.interrupt(signal), "failed");
5869
+ const requested = this.failed ? "failed" : "completed";
5870
+ return this.result(await this.close(requested, this.failures), requested);
5871
+ }
5872
+ interrupt(signal) {
5873
+ const stopped = this.current ? [{ worldTaskId: this.current, reason: `interrupted by ${signal}` }] : [];
5874
+ this.interrupted ??= this.close("failed", [...this.failures, ...stopped]);
5875
+ return this.interrupted;
5876
+ }
5877
+ async runTasks() {
5878
+ for (const task of this.opened.tasks) {
5879
+ if (this.signals.signal !== null) return;
5880
+ this.current = task.worldTaskId;
5881
+ await this.runOne(task);
5882
+ }
5883
+ this.current = null;
5884
+ }
5885
+ async runOne(task) {
5886
+ try {
5887
+ const summary = await runTask(task.worldTaskId, this.agent, {
5888
+ model: this.opts.model,
5889
+ surfaces: this.opts.surfaces,
5890
+ browserTier: this.opts.browserTier,
5891
+ outDir: this.opts.outDir,
5892
+ trace: this.opts.trace,
5893
+ setRunId: this.opened.id,
5894
+ options: this.options
5895
+ });
5896
+ this.tasks.push(summary);
5897
+ this.failed ||= taskRunFailure(summary) !== null;
5898
+ if (summary.fileError !== null)
5899
+ this.failures.push({ worldTaskId: task.worldTaskId, reason: summary.fileError });
5900
+ this.opts.onTask?.(summary);
5901
+ } catch (error) {
5902
+ this.failed = true;
5903
+ const message = error instanceof Error ? error.message : String(error);
5904
+ process.stderr.write(`task ${task.name} could not run: ${message}
5905
+ `);
5906
+ this.failures.push({
5907
+ worldTaskId: task.worldTaskId,
5908
+ reason: message || (error instanceof Error ? error.name : "Error")
5909
+ });
5910
+ }
5911
+ }
5912
+ close(status, failures = []) {
5913
+ return apiRequest(
5914
+ this.options,
5915
+ "POST",
5916
+ `${SET_RUNS}/${encodeURIComponent(this.opened.id)}/close`,
5917
+ { status, ...failures.length ? { failures } : {} }
5918
+ );
5919
+ }
5920
+ result(setRun, requested) {
5921
+ return {
5922
+ setRun,
5923
+ tasks: this.tasks,
5924
+ failed: this.failed || setRun.status !== "completed",
5925
+ closedAsRequested: setRun.status === requested
5926
+ };
5927
+ }
5928
+ };
5261
5929
  }
5262
5930
  });
5263
5931
 
@@ -5309,7 +5977,7 @@ var init_requestLog = __esm({
5309
5977
 
5310
5978
  // src/worlds/checkoutTarget.ts
5311
5979
  import * as fs2 from "fs";
5312
- import * as path2 from "path";
5980
+ import * as path3 from "path";
5313
5981
  var CheckoutRefusedError, NAMED_EDITS, CheckoutTarget;
5314
5982
  var init_checkoutTarget = __esm({
5315
5983
  "src/worlds/checkoutTarget.ts"() {
@@ -5338,17 +6006,17 @@ var init_checkoutTarget = __esm({
5338
6006
  }
5339
6007
  /** The absolute destination; a relative target may not resolve above `cwd`. */
5340
6008
  static confined(target, cwd) {
5341
- const dir = path2.resolve(cwd, target);
5342
- if (path2.isAbsolute(target)) return dir;
5343
- const rel = path2.relative(cwd, dir);
5344
- if (rel === "" || !rel.startsWith("..") && !path2.isAbsolute(rel))
6009
+ const dir = path3.resolve(cwd, target);
6010
+ if (path3.isAbsolute(target)) return dir;
6011
+ const rel = path3.relative(cwd, dir);
6012
+ if (rel === "" || !rel.startsWith("..") && !path3.isAbsolute(rel))
5345
6013
  return dir;
5346
6014
  throw new CheckoutRefusedError(
5347
6015
  `--target ${target} resolves outside the current directory (${cwd}) \u2014 run from that directory, or pass an absolute path`
5348
6016
  );
5349
6017
  }
5350
6018
  static fresh(slug, cwd) {
5351
- const base = path2.join(cwd, slug);
6019
+ const base = path3.join(cwd, slug);
5352
6020
  let dir = base;
5353
6021
  let i = 1;
5354
6022
  while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
@@ -5375,7 +6043,7 @@ var init_checkoutTarget = __esm({
5375
6043
  ].filter(_CheckoutTarget.confinedRel).sort();
5376
6044
  }
5377
6045
  static confinedRel(rel) {
5378
- if (rel === "" || path2.isAbsolute(rel)) return false;
6046
+ if (rel === "" || path3.isAbsolute(rel)) return false;
5379
6047
  return !rel.split(/[\\/]/).includes("..");
5380
6048
  }
5381
6049
  /** Why `dir` cannot be overwritten, or null when it is a clean checkout of `slug`. */
@@ -5393,9 +6061,9 @@ var init_checkoutTarget = __esm({
5393
6061
  /** Remove the replaced checkout's files (and the dirs they leave empty). */
5394
6062
  clear() {
5395
6063
  for (const rel of this.replaces) {
5396
- const abs = path2.join(this.dir, rel);
6064
+ const abs = path3.join(this.dir, rel);
5397
6065
  fs2.rmSync(abs, { force: true });
5398
- this.pruneEmptyParents(path2.dirname(abs));
6066
+ this.pruneEmptyParents(path3.dirname(abs));
5399
6067
  }
5400
6068
  }
5401
6069
  pruneEmptyParents(from) {
@@ -5403,7 +6071,7 @@ var init_checkoutTarget = __esm({
5403
6071
  while (cur !== this.dir && cur.startsWith(this.dir)) {
5404
6072
  if (fs2.existsSync(cur) && fs2.readdirSync(cur).length > 0) return;
5405
6073
  fs2.rmSync(cur, { recursive: true, force: true });
5406
- cur = path2.dirname(cur);
6074
+ cur = path3.dirname(cur);
5407
6075
  }
5408
6076
  }
5409
6077
  };
@@ -5413,7 +6081,7 @@ var init_checkoutTarget = __esm({
5413
6081
  // src/worlds/worldDir.ts
5414
6082
  import { createHash as createHash3 } from "crypto";
5415
6083
  import * as fs3 from "fs";
5416
- import * as path3 from "path";
6084
+ import * as path4 from "path";
5417
6085
  var WORLD_MANIFEST, API_BUNDLE_MANIFEST, BUNDLED_WORLD_PREFIX, CONNECTOR_SPEC, PYPROJECT, BENCHMARK_CONFIG, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS, BROAD_ROOT_MARKERS, SKIPPED_DIRS, V1_DEP, V1_IMPORT, V1_TASKSET, HARBOR_TASK, skippedDir, byCodePoint, WorldDir;
5418
6086
  var init_worldDir = __esm({
5419
6087
  "src/worlds/worldDir.ts"() {
@@ -5443,17 +6111,17 @@ var init_worldDir = __esm({
5443
6111
  worldRoot;
5444
6112
  pathsMemo;
5445
6113
  static open(dir) {
5446
- const present = (rel) => fs3.existsSync(path3.join(dir, rel));
6114
+ const present = (rel) => fs3.existsSync(path4.join(dir, rel));
5447
6115
  const bundled = present(API_BUNDLE_MANIFEST) && WORLD_ROOT_MARKERS.some((m) => present(BUNDLED_WORLD_PREFIX + m));
5448
6116
  return new _WorldDir(dir, bundled ? BUNDLED_WORLD_PREFIX : "");
5449
6117
  }
5450
6118
  // ── Files ──────────────────────────────────────────────────────────────
5451
6119
  has(rel) {
5452
- return fs3.existsSync(path3.join(this.dir, rel));
6120
+ return fs3.existsSync(path4.join(this.dir, rel));
5453
6121
  }
5454
6122
  text(rel) {
5455
6123
  try {
5456
- return fs3.readFileSync(path3.join(this.dir, rel), "utf-8");
6124
+ return fs3.readFileSync(path4.join(this.dir, rel), "utf-8");
5457
6125
  } catch {
5458
6126
  return null;
5459
6127
  }
@@ -5472,7 +6140,7 @@ var init_worldDir = __esm({
5472
6140
  const broad = BROAD_ROOT_MARKERS.some((m) => this.has(m));
5473
6141
  const out = [];
5474
6142
  const walk = (rel) => {
5475
- for (const entry of fs3.readdirSync(rel ? path3.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
6143
+ for (const entry of fs3.readdirSync(rel ? path4.join(this.dir, rel) : this.dir, { withFileTypes: true })) {
5476
6144
  const child = rel ? `${rel}/${entry.name}` : entry.name;
5477
6145
  if (entry.isDirectory()) {
5478
6146
  if (!skippedDir(entry.name)) walk(child);
@@ -5493,7 +6161,7 @@ var init_worldDir = __esm({
5493
6161
  get exclusions() {
5494
6162
  const kept = new Set(this.archivePaths);
5495
6163
  const out = [];
5496
- const filesUnder = (rel) => fs3.readdirSync(path3.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
6164
+ const filesUnder = (rel) => fs3.readdirSync(path4.join(this.dir, rel), { withFileTypes: true }).flatMap((entry) => {
5497
6165
  const child = `${rel}/${entry.name}`;
5498
6166
  if (entry.isDirectory()) return filesUnder(child);
5499
6167
  return entry.isFile() ? [child] : [];
@@ -5524,7 +6192,7 @@ var init_worldDir = __esm({
5524
6192
  const hasher = createHash3("sha256");
5525
6193
  for (const rel of this.archivePaths) {
5526
6194
  hasher.update(Buffer.from(`file:${rel}`, "utf-8"));
5527
- hasher.update(fs3.readFileSync(path3.join(this.dir, rel)));
6195
+ hasher.update(fs3.readFileSync(path4.join(this.dir, rel)));
5528
6196
  }
5529
6197
  return hasher.digest("hex");
5530
6198
  }
@@ -5589,7 +6257,7 @@ var init_worldDir = __esm({
5589
6257
 
5590
6258
  // src/worlds/tar.ts
5591
6259
  import * as fs4 from "fs";
5592
- import * as path4 from "path";
6260
+ import * as path5 from "path";
5593
6261
  import { gunzipSync } from "zlib";
5594
6262
  function archiveMode(mode) {
5595
6263
  return (mode & 73) !== 0 ? TAR_EXEC_MODE : TAR_FILE_MODE;
@@ -5675,8 +6343,8 @@ var init_tar = __esm({
5675
6343
  for (const member of this.members()) {
5676
6344
  if (member.typeflag !== "0" && member.typeflag !== "\0") continue;
5677
6345
  if (!_TarArchive.isConfined(member.name)) continue;
5678
- const target = path4.join(dir, member.name);
5679
- fs4.mkdirSync(path4.dirname(target), { recursive: true });
6346
+ const target = path5.join(dir, member.name);
6347
+ fs4.mkdirSync(path5.dirname(target), { recursive: true });
5680
6348
  const mode = archiveMode(member.mode);
5681
6349
  fs4.writeFileSync(target, member.bytes, { mode });
5682
6350
  fs4.chmodSync(target, mode);
@@ -5720,7 +6388,7 @@ __export(hub_exports, {
5720
6388
  import { createHash as createHash4 } from "crypto";
5721
6389
  import { gzipSync } from "zlib";
5722
6390
  import * as fs5 from "fs";
5723
- import * as path5 from "path";
6391
+ import * as path6 from "path";
5724
6392
  function collectArchiveRelPaths(dir) {
5725
6393
  const world = WorldDir.open(dir);
5726
6394
  if (world.hasRootGitignore) {
@@ -5783,7 +6451,7 @@ function tarMember(name, payload, mode) {
5783
6451
  function buildSourceTarball(dir) {
5784
6452
  const parts = [];
5785
6453
  for (const relPath of collectArchiveRelPaths(dir)) {
5786
- const abs = path5.join(dir, relPath);
6454
+ const abs = path6.join(dir, relPath);
5787
6455
  parts.push(
5788
6456
  ...tarMember(
5789
6457
  relPath,
@@ -5797,7 +6465,7 @@ function buildSourceTarball(dir) {
5797
6465
  }
5798
6466
  function readLocalMeta(dir) {
5799
6467
  try {
5800
- const raw = fs5.readFileSync(path5.join(dir, META_RELPATH), "utf-8");
6468
+ const raw = fs5.readFileSync(path6.join(dir, META_RELPATH), "utf-8");
5801
6469
  const parsed = JSON.parse(raw);
5802
6470
  return parsed && typeof parsed === "object" ? parsed : null;
5803
6471
  } catch {
@@ -5820,16 +6488,16 @@ function resolvePushSlug(dir, opts = {}) {
5820
6488
  }
5821
6489
  const first = declared[0];
5822
6490
  if (first) return { ...base, slug: first.slug, source: first.source };
5823
- return { ...base, slug: path5.basename(path5.resolve(dir)), source: "directory name" };
6491
+ return { ...base, slug: path6.basename(path6.resolve(dir)), source: "directory name" };
5824
6492
  }
5825
6493
  function writeLocalMeta(dir, meta) {
5826
- fs5.mkdirSync(path5.join(dir, ".gateway"), { recursive: true });
5827
- fs5.writeFileSync(path5.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
6494
+ fs5.mkdirSync(path6.join(dir, ".gateway"), { recursive: true });
6495
+ fs5.writeFileSync(path6.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
5828
6496
  }
5829
6497
  function snapshotTreeHashes(dir) {
5830
6498
  const out = {};
5831
6499
  for (const relPath of collectArchiveRelPaths(dir)) {
5832
- out[relPath] = createHash4("sha256").update(fs5.readFileSync(path5.join(dir, relPath))).digest("hex");
6500
+ out[relPath] = createHash4("sha256").update(fs5.readFileSync(path6.join(dir, relPath))).digest("hex");
5833
6501
  }
5834
6502
  return out;
5835
6503
  }
@@ -5847,7 +6515,7 @@ function localTreeState(dir) {
5847
6515
  }
5848
6516
  function readPyproject(dir) {
5849
6517
  try {
5850
- const text2 = fs5.readFileSync(path5.join(dir, "pyproject.toml"), "utf-8");
6518
+ const text2 = fs5.readFileSync(path6.join(dir, "pyproject.toml"), "utf-8");
5851
6519
  const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
5852
6520
  const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
5853
6521
  const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
@@ -5870,7 +6538,7 @@ function readWorldContractVersion(dir) {
5870
6538
  }
5871
6539
  function buildFileManifest(dir) {
5872
6540
  return collectArchiveRelPaths(dir).map((relPath) => {
5873
- const payload = fs5.readFileSync(path5.join(dir, relPath));
6541
+ const payload = fs5.readFileSync(path6.join(dir, relPath));
5874
6542
  return {
5875
6543
  path: relPath,
5876
6544
  isDirectory: false,
@@ -5916,7 +6584,7 @@ async function push(dir, opts = {}) {
5916
6584
  const pyproject = readPyproject(dir);
5917
6585
  const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
5918
6586
  if (contractVersion) pyproject.version = contractVersion;
5919
- const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path5.basename(path5.resolve(dir));
6587
+ const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path6.basename(path6.resolve(dir));
5920
6588
  const metadata = {
5921
6589
  ...opts.name !== void 0 ? { name: opts.name } : {},
5922
6590
  ...opts.description !== void 0 ? { description: opts.description } : {},
@@ -6042,7 +6710,7 @@ async function propose(dir, opts) {
6042
6710
  if (state2.clean)
6043
6711
  throw new Error("Nothing to propose \u2014 the tree matches its base.");
6044
6712
  const changes = state2.dirty.map((relPath) => {
6045
- const abs = path5.join(dir, relPath);
6713
+ const abs = path6.join(dir, relPath);
6046
6714
  if (!fs5.existsSync(abs))
6047
6715
  return { path: relPath, action: "delete" };
6048
6716
  return {
@@ -6083,7 +6751,7 @@ var init_hub = __esm({
6083
6751
  init_worldDir();
6084
6752
  init_tar();
6085
6753
  DEFAULT_BRANCH = "main";
6086
- META_RELPATH = path5.join(".gateway", ".env-metadata.json");
6754
+ META_RELPATH = path6.join(".gateway", ".env-metadata.json");
6087
6755
  FILE_MODE = TAR_FILE_MODE;
6088
6756
  MINTED_SUFFIX = /^-\d+$/;
6089
6757
  PushSlugConflict = class extends Error {
@@ -6311,7 +6979,7 @@ function rebaseConflicts(error) {
6311
6979
  }
6312
6980
  const conflicts = body?.conflicts;
6313
6981
  if (!Array.isArray(conflicts)) return null;
6314
- return conflicts.filter(isConflictPath).map(({ path: path12, reason }) => ({ path: path12, reason }));
6982
+ return conflicts.filter(isConflictPath).map(({ path: path13, reason }) => ({ path: path13, reason }));
6315
6983
  }
6316
6984
  function isConflictPath(value) {
6317
6985
  const entry = value;
@@ -6351,8 +7019,8 @@ var init_sandbox = __esm({
6351
7019
  this.options = options;
6352
7020
  }
6353
7021
  options;
6354
- request(method, path12, body) {
6355
- return apiRequest(this.options, method, path12, body, {
7022
+ request(method, path13, body) {
7023
+ return apiRequest(this.options, method, path13, body, {
6356
7024
  timeoutMs: SANDBOX_REQUEST_TIMEOUT_MS
6357
7025
  });
6358
7026
  }
@@ -6883,6 +7551,17 @@ async function queryRows(slugRef, entity, opts = {}, options = {}) {
6883
7551
  `/api/public/worlds/${encodeURIComponent(slug)}/data${queryRowsSearch(entity, opts, ref)}`
6884
7552
  );
6885
7553
  }
7554
+ async function sqlRows(slugRef, sql, opts = {}, options = {}) {
7555
+ const [slug, ref] = slugRef.split("@", 2);
7556
+ if (!slug || ref === "") throw new Error(`sqlRows needs a slug, optionally slug@ref (got ${slugRef})`);
7557
+ if (!sql.trim()) throw new Error("sqlRows needs one SQL statement");
7558
+ return apiRequest(options, "POST", `/api/public/worlds/${encodeURIComponent(slug)}/data/sql`, {
7559
+ sql,
7560
+ ...ref ? { ref } : {},
7561
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
7562
+ ...opts.scenario ? { scenario: opts.scenario } : {}
7563
+ });
7564
+ }
6886
7565
  var init_query = __esm({
6887
7566
  "src/worlds/query.ts"() {
6888
7567
  "use strict";
@@ -6939,7 +7618,7 @@ var init_router = __esm({
6939
7618
  // src/cli/config.ts
6940
7619
  import * as fs6 from "fs";
6941
7620
  import * as os from "os";
6942
- import * as path6 from "path";
7621
+ import * as path7 from "path";
6943
7622
  async function api(creds, method, route, body, init2 = {}) {
6944
7623
  const reply = await new PlatformTransport(creds).send(method, route, body, init2);
6945
7624
  if (!reply.ok) throw new ApiError2(reply.status, method, route, reply.text);
@@ -6975,7 +7654,7 @@ var init_config = __esm({
6975
7654
  import { spawn, spawnSync } from "child_process";
6976
7655
  import * as fs7 from "fs";
6977
7656
  import * as net from "net";
6978
- import * as path7 from "path";
7657
+ import * as path8 from "path";
6979
7658
  function tailOf(output) {
6980
7659
  return output.length > DIAGNOSTIC_TAIL ? `\u2026${output.slice(-DIAGNOSTIC_TAIL)}` : output;
6981
7660
  }
@@ -6990,16 +7669,16 @@ function runtimeRoot() {
6990
7669
  const candidates = [];
6991
7670
  if (process.env.GATEWAY_RUNTIME_PATH)
6992
7671
  candidates.push(process.env.GATEWAY_RUNTIME_PATH);
6993
- const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path7.dirname(fs7.realpathSync(process.argv[1])) : null;
7672
+ const entry = process.argv[1] && fs7.existsSync(process.argv[1]) ? path8.dirname(fs7.realpathSync(process.argv[1])) : null;
6994
7673
  if (entry) {
6995
- candidates.push(path7.resolve(entry, "..", "runtime"));
6996
- candidates.push(path7.resolve(entry, "..", "..", "runtime"));
6997
- candidates.push(path7.resolve(entry, "..", "..", "sdk", "src"));
7674
+ candidates.push(path8.resolve(entry, "..", "runtime"));
7675
+ candidates.push(path8.resolve(entry, "..", "..", "runtime"));
7676
+ candidates.push(path8.resolve(entry, "..", "..", "sdk", "src"));
6998
7677
  }
6999
- candidates.push(path7.resolve(process.cwd(), "runtime"));
7678
+ candidates.push(path8.resolve(process.cwd(), "runtime"));
7000
7679
  for (const root of candidates)
7001
7680
  if (fs7.existsSync(
7002
- path7.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7681
+ path8.join(root, "gatewaysdk", "environment", "schema", "platform.py")
7003
7682
  ))
7004
7683
  return root;
7005
7684
  return null;
@@ -7088,13 +7767,12 @@ async function localBridge(request, local, opts = {}) {
7088
7767
  ["-I", "-B", "-c", BRIDGE_BOOTSTRAP, local.root],
7089
7768
  {
7090
7769
  stdio: ["pipe", "pipe", "pipe"],
7091
- // This is the author's machine running the author's world: the one
7092
- // place the runtime may import and run a world's own Python (check's
7093
- // handler execution, `worlds call`, `worlds test` tool cases).
7770
+ // The author's machine running the author's world: the one place the runtime may run its Python.
7094
7771
  env: {
7095
7772
  PATH: process.env.PATH ?? "",
7096
7773
  HOME: process.env.HOME ?? "",
7097
7774
  TMPDIR: process.env.TMPDIR ?? "",
7775
+ ...process.env.VIRTUAL_ENV ? { VIRTUAL_ENV: process.env.VIRTUAL_ENV } : {},
7098
7776
  GATEWAY_RUNTIME_EXECUTE: "1"
7099
7777
  }
7100
7778
  }
@@ -7166,18 +7844,46 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
7166
7844
  }
7167
7845
  });
7168
7846
 
7847
+ // src/worlds/databaseEngines.ts
7848
+ function isWorldSnapshotOutput(path13) {
7849
+ return SNAPSHOT_OUTPUTS.has(path13.slice(path13.lastIndexOf("/") + 1));
7850
+ }
7851
+ var WORLD_DATABASE_ENGINES, DEFAULT_WORLD_DATABASE_ENGINE, WORLD_SNAPSHOT_FILE_PATH, WORLD_SNAPSHOT_SIDECAR_PATH, WORLD_SNAPSHOT_CONTENT_TYPE, SNAPSHOT_OUTPUTS;
7852
+ var init_databaseEngines = __esm({
7853
+ "src/worlds/databaseEngines.ts"() {
7854
+ "use strict";
7855
+ WORLD_DATABASE_ENGINES = [
7856
+ {
7857
+ engine: "sqlite",
7858
+ label: "SQLite",
7859
+ target: "sqlite-json/1",
7860
+ snapshotName: "state.sqlite",
7861
+ contentType: "application/vnd.sqlite3"
7862
+ }
7863
+ ];
7864
+ DEFAULT_WORLD_DATABASE_ENGINE = WORLD_DATABASE_ENGINES[0];
7865
+ WORLD_SNAPSHOT_FILE_PATH = `db/${DEFAULT_WORLD_DATABASE_ENGINE.snapshotName}`;
7866
+ WORLD_SNAPSHOT_SIDECAR_PATH = "db/state.json";
7867
+ WORLD_SNAPSHOT_CONTENT_TYPE = DEFAULT_WORLD_DATABASE_ENGINE.contentType;
7868
+ SNAPSHOT_OUTPUTS = /* @__PURE__ */ new Set([
7869
+ ...WORLD_DATABASE_ENGINES.map((engine) => engine.snapshotName),
7870
+ WORLD_SNAPSHOT_SIDECAR_PATH.slice("db/".length)
7871
+ ]);
7872
+ }
7873
+ });
7874
+
7169
7875
  // src/cli/runtime/client.ts
7170
7876
  import * as fs8 from "fs";
7171
- import * as path8 from "path";
7877
+ import * as path9 from "path";
7172
7878
  function isBuildOutput(relPath) {
7173
- return BUILD_OUTPUT.test(relPath);
7879
+ return isWorldSnapshotOutput(relPath);
7174
7880
  }
7175
7881
  function readWorldFiles(dir, opts = {}) {
7176
7882
  if (!fs8.existsSync(dir) || !fs8.statSync(dir).isDirectory())
7177
7883
  throw new CliError(`Not a directory: ${dir}`);
7178
7884
  const out = [];
7179
7885
  const walk = (rel) => {
7180
- const abs = rel ? path8.join(dir, rel) : dir;
7886
+ const abs = rel ? path9.join(dir, rel) : dir;
7181
7887
  const entries = fs8.readdirSync(abs, { withFileTypes: true });
7182
7888
  entries.sort((a, b) => a.name.localeCompare(b.name));
7183
7889
  for (const entry of entries) {
@@ -7188,7 +7894,7 @@ function readWorldFiles(dir, opts = {}) {
7188
7894
  if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
7189
7895
  out.push({
7190
7896
  path: childRel,
7191
- content: fs8.readFileSync(path8.join(dir, childRel)).toString("base64")
7897
+ content: fs8.readFileSync(path9.join(dir, childRel)).toString("base64")
7192
7898
  });
7193
7899
  }
7194
7900
  }
@@ -7208,15 +7914,15 @@ function writeWorldFiles(dir, files, opts = {}) {
7208
7914
  if (opts.clean && fs8.existsSync(dir)) {
7209
7915
  const keep = new Set(files.map((f) => f.path));
7210
7916
  for (const existing of readWorldFiles(dir)) {
7211
- if (!keep.has(existing.path)) fs8.rmSync(path8.join(dir, existing.path));
7917
+ if (!keep.has(existing.path)) fs8.rmSync(path9.join(dir, existing.path));
7212
7918
  }
7213
7919
  pruneEmptyDirs(dir);
7214
7920
  }
7215
7921
  fs8.mkdirSync(dir, { recursive: true });
7216
7922
  const written = [];
7217
7923
  for (const file of files) {
7218
- const target = path8.join(dir, file.path);
7219
- fs8.mkdirSync(path8.dirname(target), { recursive: true });
7924
+ const target = path9.join(dir, file.path);
7925
+ fs8.mkdirSync(path9.dirname(target), { recursive: true });
7220
7926
  fs8.writeFileSync(target, Buffer.from(file.content, "base64"));
7221
7927
  written.push(file.path);
7222
7928
  }
@@ -7226,7 +7932,7 @@ function pruneEmptyDirs(root) {
7226
7932
  const walk = (abs) => {
7227
7933
  let empty = true;
7228
7934
  for (const entry of fs8.readdirSync(abs, { withFileTypes: true })) {
7229
- const child = path8.join(abs, entry.name);
7935
+ const child = path9.join(abs, entry.name);
7230
7936
  if (entry.isDirectory()) {
7231
7937
  if (SKIP_DIRS.has(entry.name)) {
7232
7938
  empty = false;
@@ -7263,13 +7969,9 @@ function yamlDocument(request) {
7263
7969
  return isYamlSource(String(request.source ?? ""), String(request.document ?? ""));
7264
7970
  return request.command === "conform" && Array.isArray(request.openapi) && request.openapi.some((doc) => YAML_NAME.test(doc.path ?? ""));
7265
7971
  }
7266
- function runsLocally(request) {
7267
- const local = localRuntime();
7268
- return local !== null && !(yamlDocument(request) && !localPythonHasYaml(local.python));
7269
- }
7270
7972
  async function runtime(creds, request, opts = {}) {
7271
- const local = localRuntime();
7272
- if (local && runsLocally(request)) {
7973
+ const local = new RuntimeRoute().local(request);
7974
+ if (local) {
7273
7975
  const payload2 = await localBridge(request, local, opts);
7274
7976
  const detail2 = detailFrom(payload2);
7275
7977
  if (detail2) throw new BridgeRejected(detail2);
@@ -7296,17 +7998,17 @@ async function runtime(creds, request, opts = {}) {
7296
7998
  if (payload === null) throw new CliError(`POST ${route} (${request.command}) returned non-JSON`, 1);
7297
7999
  return payload;
7298
8000
  }
7299
- var MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BUILD_OUTPUT, BridgeRejected, YAML_NAME;
8001
+ var MAX_WORLD_FILES, RUNTIME_TIMEOUT_MS, SKIP_DIRS, BridgeRejected, YAML_NAME, YAML_NEEDS_PYYAML, YAML_NOT_ON_PLATFORM, RuntimeRoute;
7300
8002
  var init_client = __esm({
7301
8003
  "src/cli/runtime/client.ts"() {
7302
8004
  "use strict";
7303
8005
  init_router();
7304
8006
  init_local();
7305
8007
  init_transport();
8008
+ init_databaseEngines();
7306
8009
  MAX_WORLD_FILES = 2e3;
7307
8010
  RUNTIME_TIMEOUT_MS = 18e4;
7308
8011
  SKIP_DIRS = /* @__PURE__ */ new Set(["__pycache__", ".git", "node_modules", ".gateway"]);
7309
- BUILD_OUTPUT = /(^|\/)state\.(sqlite|json)$/;
7310
8012
  BridgeRejected = class extends CliError {
7311
8013
  constructor(detail) {
7312
8014
  super(formatBridgeError(detail), 1);
@@ -7315,6 +8017,26 @@ var init_client = __esm({
7315
8017
  detail;
7316
8018
  };
7317
8019
  YAML_NAME = /\.ya?ml$/i;
8020
+ 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";
8021
+ 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)";
8022
+ RuntimeRoute = class _RuntimeRoute {
8023
+ constructor(here = localRuntime, hasYaml = localPythonHasYaml) {
8024
+ this.here = here;
8025
+ this.hasYaml = hasYaml;
8026
+ }
8027
+ here;
8028
+ hasYaml;
8029
+ local(request) {
8030
+ const local = this.here();
8031
+ const yaml = yamlDocument(request);
8032
+ if (yaml && local === null) throw _RuntimeRoute.refusal(YAML_NOT_ON_PLATFORM);
8033
+ if (yaml && local !== null && !this.hasYaml(local.python)) throw _RuntimeRoute.refusal(YAML_NEEDS_PYYAML);
8034
+ return local;
8035
+ }
8036
+ static refusal(message) {
8037
+ return new BridgeRejected({ code: "conform", message, schema_path: "/openapi/source" });
8038
+ }
8039
+ };
7318
8040
  }
7319
8041
  });
7320
8042
 
@@ -7329,10 +8051,11 @@ __export(data_exports, {
7329
8051
  ingestLocalToolCalls: () => ingestLocalToolCalls,
7330
8052
  queryLocalWorldData: () => queryLocalWorldData,
7331
8053
  queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
7332
- showLocalWorldData: () => showLocalWorldData
8054
+ showLocalWorldData: () => showLocalWorldData,
8055
+ sqlLocalWorldData: () => sqlLocalWorldData
7333
8056
  });
7334
8057
  import * as fs9 from "fs";
7335
- import * as path9 from "path";
8058
+ import * as path10 from "path";
7336
8059
  async function importLocalWorldData(dir, rows, opts, creds) {
7337
8060
  const res = await runtime(creds, {
7338
8061
  command: "import-data",
@@ -7363,8 +8086,8 @@ async function importLocalWorldFiles(dir, sources, opts) {
7363
8086
  );
7364
8087
  const res = await runtime(null, {
7365
8088
  command: "import-files",
7366
- world: path9.resolve(dir),
7367
- sources: sources.map((source) => path9.resolve(source)),
8089
+ world: path10.resolve(dir),
8090
+ sources: sources.map((source) => path10.resolve(source)),
7368
8091
  mode: opts.mode ?? "append",
7369
8092
  ...opts.entity ? { entity: opts.entity } : {},
7370
8093
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
@@ -7390,9 +8113,9 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7390
8113
  );
7391
8114
  const res = await runtime(null, {
7392
8115
  command: "chunk-files",
7393
- world: path9.resolve(dir),
7394
- sources: sources.map((source) => path9.resolve(source)),
7395
- out: path9.resolve(out),
8116
+ world: path10.resolve(dir),
8117
+ sources: sources.map((source) => path10.resolve(source)),
8118
+ out: path10.resolve(out),
7396
8119
  ...opts.entity ? { entity: opts.entity } : {},
7397
8120
  ...opts.delimiter ? { delimiter: opts.delimiter } : {},
7398
8121
  ...opts.chunkBytes ? { chunk_bytes: opts.chunkBytes } : {},
@@ -7403,8 +8126,8 @@ async function chunkLocalWorldFiles(dir, sources, out, opts) {
7403
8126
  function dropBuildOutputs(dir) {
7404
8127
  const dropped = [];
7405
8128
  for (const file of readWorldFiles(dir, { buildOutputs: true })) {
7406
- if (!BUILD_OUTPUT2.test(file.path)) continue;
7407
- fs9.rmSync(path9.join(dir, file.path), { force: true });
8129
+ if (!isBuildOutput(file.path)) continue;
8130
+ fs9.rmSync(path10.join(dir, file.path), { force: true });
7408
8131
  dropped.push(file.path);
7409
8132
  }
7410
8133
  return dropped;
@@ -7471,7 +8194,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7471
8194
  const payload = await localBridge(
7472
8195
  {
7473
8196
  command: "query",
7474
- world: path9.resolve(dir),
8197
+ world: path10.resolve(dir),
7475
8198
  entity,
7476
8199
  across: true,
7477
8200
  ...opts.compare ? { compare: true } : {},
@@ -7487,6 +8210,28 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
7487
8210
  if (detail) throw new BridgeRejected(detail);
7488
8211
  return payload;
7489
8212
  }
8213
+ async function sqlLocalWorldData(dir, sql, opts) {
8214
+ const local = localRuntime();
8215
+ if (!local)
8216
+ throw new CliError(
8217
+ "`worlds data sql` on a directory reads the world's database in place, so it needs the bundled runtime on this machine. " + localRuntimeReason(),
8218
+ 1
8219
+ );
8220
+ const payload = await localBridge(
8221
+ {
8222
+ command: "sql",
8223
+ world: path10.resolve(dir),
8224
+ sql,
8225
+ ...opts.limit !== void 0 ? { limit: opts.limit } : {},
8226
+ ...opts.scenario ? { scenario: opts.scenario } : {}
8227
+ },
8228
+ local
8229
+ );
8230
+ const detail = detailFrom(payload);
8231
+ if (detail) throw new BridgeRejected(detail);
8232
+ const { status: _status, ...answer } = payload;
8233
+ return answer;
8234
+ }
7490
8235
  async function ingestLocalToolCalls(dir, records, opts, creds) {
7491
8236
  const res = await runtime(creds, {
7492
8237
  command: "ingest-calls",
@@ -7518,14 +8263,12 @@ async function ingestLocalToolCalls(dir, records, opts, creds) {
7518
8263
  ...res.batch !== void 0 ? { batch: res.batch } : {}
7519
8264
  };
7520
8265
  }
7521
- var BUILD_OUTPUT2;
7522
8266
  var init_data = __esm({
7523
8267
  "src/cli/runtime/data.ts"() {
7524
8268
  "use strict";
7525
8269
  init_router();
7526
8270
  init_client();
7527
8271
  init_local();
7528
- BUILD_OUTPUT2 = /(^|\/)state\.(sqlite|json)$/;
7529
8272
  }
7530
8273
  });
7531
8274
 
@@ -7544,7 +8287,7 @@ __export(transform_exports, {
7544
8287
  import { spawn as spawn2 } from "child_process";
7545
8288
  import * as fs10 from "fs";
7546
8289
  import * as os2 from "os";
7547
- import * as path10 from "path";
8290
+ import * as path11 from "path";
7548
8291
  import { createRequire } from "module";
7549
8292
  function checkRows(value, where) {
7550
8293
  if (!isObject(value)) throw new CliError(`${where} must return {entity: [rows]}; got ${value === null ? "null" : Array.isArray(value) ? "an array" : typeof value}`, 1);
@@ -7569,27 +8312,27 @@ function typescriptPrefix() {
7569
8312
  }
7570
8313
  }
7571
8314
  function command(script, wrapperDir) {
7572
- const suffix = path10.extname(script).toLowerCase();
8315
+ const suffix = path11.extname(script).toLowerCase();
7573
8316
  if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
7574
8317
  if (NODE_SUFFIXES.includes(suffix)) {
7575
- const wrapper = path10.join(wrapperDir, "transform-wrapper.mjs");
8318
+ const wrapper = path11.join(wrapperDir, "transform-wrapper.mjs");
7576
8319
  fs10.writeFileSync(wrapper, NODE_WRAPPER);
7577
8320
  const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
7578
8321
  return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
7579
8322
  }
7580
- throw new CliError(`${path10.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
8323
+ throw new CliError(`${path11.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
7581
8324
  }
7582
8325
  async function runTransform(script, records, opts = {}) {
7583
8326
  if (!fs10.existsSync(script) || !fs10.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
7584
- const resolved = path10.resolve(script);
7585
- const name = path10.basename(script);
8327
+ const resolved = path11.resolve(script);
8328
+ const name = path11.basename(script);
7586
8329
  const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
7587
- const scratch = fs10.mkdtempSync(path10.join(os2.tmpdir(), "gateway-transform-"));
8330
+ const scratch = fs10.mkdtempSync(path11.join(os2.tmpdir(), "gateway-transform-"));
7588
8331
  try {
7589
8332
  const { file, args } = command(resolved, scratch);
7590
8333
  const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !SCRUBBED.test(key)));
7591
8334
  const done = await new Promise((resolve7, reject) => {
7592
- const child = spawn2(file, args, { cwd: path10.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
8335
+ const child = spawn2(file, args, { cwd: path11.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
7593
8336
  let stdout = "";
7594
8337
  let stderr = "";
7595
8338
  let timedOut = false;
@@ -7750,7 +8493,7 @@ async function forEachLimited(items, limit, fn) {
7750
8493
  if (failure) throw failure;
7751
8494
  }
7752
8495
  async function waitForBatch(client, batchId, opts = {}) {
7753
- const sleep2 = opts.sleep ?? client.sleep;
8496
+ const sleep4 = opts.sleep ?? client.sleep;
7754
8497
  let delay = opts.initialDelayMs ?? 1e3;
7755
8498
  let last = "";
7756
8499
  for (; ; ) {
@@ -7761,7 +8504,7 @@ async function waitForBatch(client, batchId, opts = {}) {
7761
8504
  last = phase;
7762
8505
  }
7763
8506
  if (isSettled(status)) return status;
7764
- await sleep2(delay);
8507
+ await sleep4(delay);
7765
8508
  delay = Math.min(1e4, Math.round(delay * 1.5));
7766
8509
  }
7767
8510
  }
@@ -7783,10 +8526,10 @@ var init_batches = __esm({
7783
8526
  ]);
7784
8527
  sleepMs = (ms) => new Promise((resolve7) => setTimeout(resolve7, ms));
7785
8528
  WorldDataApi = class {
7786
- constructor(creds, slug, sleep2 = sleepMs) {
8529
+ constructor(creds, slug, sleep4 = sleepMs) {
7787
8530
  this.creds = creds;
7788
8531
  this.slug = slug;
7789
- this.sleep = sleep2;
8532
+ this.sleep = sleep4;
7790
8533
  this.base = `/api/public/worlds/${encodeURIComponent(slug)}/data`;
7791
8534
  }
7792
8535
  creds;
@@ -8069,7 +8812,7 @@ var init_push = __esm({
8069
8812
  // src/worlds/ingest.ts
8070
8813
  import * as fs12 from "fs";
8071
8814
  import * as os3 from "os";
8072
- import * as path11 from "path";
8815
+ import * as path12 from "path";
8073
8816
  function parseToolCallRecord(raw) {
8074
8817
  const none = { record: null, skip: null };
8075
8818
  if (!isObject2(raw)) return { ...none, refusal: "record is not an object" };
@@ -8193,19 +8936,13 @@ function toolCallsFromObservations(observations, opts = {}) {
8193
8936
  }
8194
8937
  return out;
8195
8938
  }
8196
- function credentialsOf(options) {
8197
- const host = (options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai").replace(/\/+$/, "");
8198
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8199
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8200
- return publicKey && secretKey ? { host, publicKey, secretKey } : null;
8201
- }
8202
8939
  async function ingest(world, records, options = {}) {
8203
8940
  const { ingestLocalToolCalls: ingestLocalToolCalls2, importLocalWorldData: importLocalWorldData2, checkLocalWorldData: checkLocalWorldData2 } = await Promise.resolve().then(() => (init_data(), data_exports));
8204
8941
  if (options.transform !== void 0 && options.map !== void 0) throw new Error("records go through one of transform, map");
8205
8942
  const redact = options.redact ?? "off";
8206
8943
  const mode = options.mode ?? "append";
8207
8944
  const dryRun = Boolean(options.dryRun);
8208
- const creds = credentialsOf(options);
8945
+ const creds = platformEndpoint(options);
8209
8946
  const local = fs12.existsSync(world) && fs12.statSync(world).isDirectory();
8210
8947
  const map = options.map === void 0 ? void 0 : options.map;
8211
8948
  let rows = null;
@@ -8219,12 +8956,12 @@ async function ingest(world, records, options = {}) {
8219
8956
  const landed = dryRun ? await checkLocalWorldData2(world, rows, creds, { redact }) : await importLocalWorldData2(world, rows, { mode, redact }, creds);
8220
8957
  return transformReport(world, records.length, rows, stderr, { dryRun, redact, changed: !dryRun && landed.changed, kind: landed.kind, entityCounts: landed.entity_counts });
8221
8958
  }
8222
- if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
8959
+ if (!creds) throw new Error(GATEWAY_KEYS_MISSING);
8223
8960
  const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
8224
8961
  const slug = world.split("@", 1)[0];
8225
- const scratch = fs12.mkdtempSync(path11.join(os3.tmpdir(), "gateway-ingest-"));
8962
+ const scratch = fs12.mkdtempSync(path12.join(os3.tmpdir(), "gateway-ingest-"));
8226
8963
  try {
8227
- const pulled = await pull2(world, path11.join(scratch, slug), options);
8964
+ const pulled = await pull2(world, path12.join(scratch, slug), options);
8228
8965
  let report;
8229
8966
  let projected;
8230
8967
  if (rows === null) {
@@ -8303,6 +9040,7 @@ var init_ingest = __esm({
8303
9040
  "src/worlds/ingest.ts"() {
8304
9041
  "use strict";
8305
9042
  init_worlds();
9043
+ init_platformEndpoint();
8306
9044
  isObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
8307
9045
  isoOf = (value) => value instanceof Date ? value.toISOString() : typeof value === "string" ? value : void 0;
8308
9046
  }
@@ -8403,7 +9141,7 @@ var init_dryRun = __esm({
8403
9141
  /** Poll until the run settles or `timeoutSeconds` pass; the last verdict either way. */
8404
9142
  async settle(runId, opts) {
8405
9143
  const now = opts.now ?? Date.now;
8406
- const sleep2 = opts.sleep ?? ((ms) => new Promise((resolve7) => setTimeout(resolve7, ms)));
9144
+ const sleep4 = opts.sleep ?? ((ms) => new Promise((resolve7) => setTimeout(resolve7, ms)));
8407
9145
  const deadline = now() + opts.timeoutSeconds * 1e3;
8408
9146
  for (; ; ) {
8409
9147
  const budget = Math.floor(
@@ -8413,7 +9151,7 @@ var init_dryRun = __esm({
8413
9151
  const verdict = await this.status(runId, budget);
8414
9152
  if (verdict.settled || now() >= deadline) return verdict;
8415
9153
  opts.onProgress?.(verdict);
8416
- if (now() - asked < 1e3) await sleep2(1e3);
9154
+ if (now() - asked < 1e3) await sleep4(1e3);
8417
9155
  }
8418
9156
  }
8419
9157
  };
@@ -8481,6 +9219,8 @@ __export(worlds_exports, {
8481
9219
  EXPLORATORY_TASK: () => EXPLORATORY_TASK,
8482
9220
  EXPLORATORY_TASK_SOURCE: () => EXPLORATORY_TASK_SOURCE,
8483
9221
  GatewayEvalsClient: () => GatewayEvalsClient,
9222
+ GradePayload: () => GradePayload,
9223
+ GradeRefused: () => GradeRefused,
8484
9224
  HostedDryRunClient: () => HostedDryRunClient,
8485
9225
  IDLE_WARNING_MINUTES: () => IDLE_WARNING_MINUTES,
8486
9226
  KEY_NAMES: () => KEY_NAMES,
@@ -8496,6 +9236,7 @@ __export(worlds_exports, {
8496
9236
  PlatformTransport: () => PlatformTransport,
8497
9237
  PushSlugConflict: () => PushSlugConflict,
8498
9238
  RECOVER_BUDGET_MS: () => RECOVER_BUDGET_MS,
9239
+ RetryPolicy: () => RetryPolicy,
8499
9240
  SANDBOX_REQUEST_TIMEOUT_MS: () => SANDBOX_REQUEST_TIMEOUT_MS,
8500
9241
  SECRET_KEY: () => SECRET_KEY,
8501
9242
  SESSION_STATES: () => SESSION_STATES,
@@ -8503,8 +9244,10 @@ __export(worlds_exports, {
8503
9244
  STATUS_WAIT_CAP_SECONDS: () => STATUS_WAIT_CAP_SECONDS,
8504
9245
  STRIP_PREFIX: () => STRIP_PREFIX,
8505
9246
  SURFACE_KINDS: () => SURFACE_KINDS,
9247
+ Score: () => Score,
8506
9248
  Screen: () => Screen,
8507
9249
  SecretScrubber: () => SecretScrubber,
9250
+ TOOL_CONTRACT_UNREAD: () => TOOL_CONTRACT_UNREAD,
8508
9251
  TRANSFORMS: () => TRANSFORMS,
8509
9252
  TaskEditClient: () => TaskEditClient,
8510
9253
  TaskRun: () => TaskRun,
@@ -8516,14 +9259,17 @@ __export(worlds_exports, {
8516
9259
  WorldBrowser: () => WorldBrowser,
8517
9260
  WorldCallTooLarge: () => WorldCallTooLarge,
8518
9261
  WorldEditClient: () => WorldEditClient,
9262
+ WorldNotice: () => WorldNotice,
8519
9263
  WorldRequestLog: () => WorldRequestLog,
8520
9264
  WorldSandboxClient: () => WorldSandboxClient,
8521
9265
  WorldSession: () => WorldSession,
8522
9266
  WorldSessionCallError: () => WorldSessionCallError,
9267
+ WorldSessionOpenFailed: () => WorldSessionOpenFailed,
8523
9268
  WorldSessionTimeout: () => WorldSessionTimeout,
8524
9269
  WorldSet: () => WorldSet,
8525
9270
  WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
8526
9271
  WorldTask: () => WorldTask,
9272
+ WorldToolsUnread: () => WorldToolsUnread,
8527
9273
  aggregateTaskGrade: () => aggregateTaskGrade,
8528
9274
  apiRequest: () => apiRequest,
8529
9275
  applyTransforms: () => applyTransforms,
@@ -8566,6 +9312,7 @@ __export(worlds_exports, {
8566
9312
  listTaskSessions: () => listTaskSessions,
8567
9313
  listTasks: () => listTasks,
8568
9314
  localTreeState: () => localTreeState,
9315
+ messageOf: () => messageOf,
8569
9316
  migrateProjectWorlds: () => migrateProjectWorlds,
8570
9317
  migrateWorld: () => migrateWorld,
8571
9318
  modelToolName: () => modelToolName,
@@ -8609,10 +9356,14 @@ __export(worlds_exports, {
8609
9356
  setEnvSecret: () => setEnvSecret,
8610
9357
  shapeText: () => shapeText,
8611
9358
  snapshotTreeHashes: () => snapshotTreeHashes,
9359
+ sqlRows: () => sqlRows,
9360
+ stillStartingHint: () => stillStartingHint,
8612
9361
  storedForms: () => storedForms,
8613
9362
  surfaceDescriptor: () => surfaceDescriptor,
8614
9363
  surfaceKinds: () => surfaceKinds,
9364
+ taskGradeError: () => taskGradeError,
8615
9365
  taskLabel: () => taskLabel,
9366
+ taskRunFailure: () => taskRunFailure,
8616
9367
  toolCallRecordsIn: () => toolCallRecordsIn,
8617
9368
  toolCallsFromObservations: () => toolCallsFromObservations,
8618
9369
  uiEntryUrl: () => uiEntryUrl,
@@ -8626,17 +9377,6 @@ __export(worlds_exports, {
8626
9377
  withoutUiToken: () => withoutUiToken,
8627
9378
  writeLocalMeta: () => writeLocalMeta
8628
9379
  });
8629
- function resolveEnv(options) {
8630
- const host = options.host ?? process.env.GATEWAY_HOST ?? "https://withgateway.ai";
8631
- const publicKey = options.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
8632
- const secretKey = options.secretKey ?? process.env.GATEWAY_SECRET_KEY;
8633
- if (!publicKey || !secretKey) {
8634
- throw new Error(
8635
- "Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey)."
8636
- );
8637
- }
8638
- return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
8639
- }
8640
9380
  function parseErrorBody(text2, limit = 600) {
8641
9381
  const raw = { detail: text2.slice(0, limit), code: null };
8642
9382
  let parsed;
@@ -8656,9 +9396,9 @@ function parseErrorBody(text2, limit = 600) {
8656
9396
  code: stableCode
8657
9397
  };
8658
9398
  }
8659
- async function apiRequest(options, method, path12, body, init2 = {}) {
8660
- const reply = await new PlatformTransport(resolveEnv(options)).send(method, path12, body, init2);
8661
- if (!reply.ok) throw new ApiError(reply.status, method, path12, reply.text);
9399
+ async function apiRequest(options, method, path13, body, init2 = {}) {
9400
+ const reply = await new PlatformTransport(requirePlatformEndpoint(options)).send(method, path13, body, init2);
9401
+ if (!reply.ok) throw new ApiError(reply.status, method, path13, reply.text);
8662
9402
  return JSON.parse(reply.text);
8663
9403
  }
8664
9404
  async function setEnvSecret(slug, key, value, options = {}) {
@@ -8770,7 +9510,6 @@ function detectProvenance() {
8770
9510
  kind: "ci",
8771
9511
  id: env.GITHUB_ACTOR,
8772
9512
  name: env.GITHUB_ACTOR,
8773
- // GitHub serves every account's avatar at /<login>.png.
8774
9513
  imageUrl: `${server}/${env.GITHUB_ACTOR}.png`
8775
9514
  };
8776
9515
  }
@@ -8838,9 +9577,11 @@ async function resolveWorld(slugRef, slug, ref, pin, options) {
8838
9577
  var ApiError, WorldTask, WorldSet;
8839
9578
  var init_worlds = __esm({
8840
9579
  "src/worlds/index.ts"() {
9580
+ init_platformEndpoint();
8841
9581
  init_transport();
8842
9582
  init_transport();
8843
9583
  init_session2();
9584
+ init_grade();
8844
9585
  init_sessionList();
8845
9586
  init_sessionContract();
8846
9587
  init_taskRef();
@@ -8866,10 +9607,11 @@ var init_worlds = __esm({
8866
9607
  init_sandbox();
8867
9608
  init_editSessions();
8868
9609
  init_dryRun();
9610
+ init_worldNotice();
8869
9611
  ApiError = class extends Error {
8870
- constructor(status, method, path12, text2) {
9612
+ constructor(status, method, path13, text2) {
8871
9613
  const parsed = parseErrorBody(text2);
8872
- super(`${method} ${path12} -> ${status}: ${parsed.detail}`);
9614
+ super(`${method} ${path13} -> ${status}: ${parsed.detail}`);
8873
9615
  this.status = status;
8874
9616
  this.text = text2;
8875
9617
  this.name = "ApiError";
@@ -8984,6 +9726,8 @@ export {
8984
9726
  EXPLORATORY_TASK,
8985
9727
  EXPLORATORY_TASK_SOURCE,
8986
9728
  GatewayEvalsClient,
9729
+ GradePayload,
9730
+ GradeRefused,
8987
9731
  HostedDryRunClient,
8988
9732
  IDLE_WARNING_MINUTES,
8989
9733
  KEY_NAMES,
@@ -8999,6 +9743,7 @@ export {
8999
9743
  PlatformTransport,
9000
9744
  PushSlugConflict,
9001
9745
  RECOVER_BUDGET_MS,
9746
+ RetryPolicy,
9002
9747
  SANDBOX_REQUEST_TIMEOUT_MS,
9003
9748
  SECRET_KEY,
9004
9749
  SESSION_STATES,
@@ -9006,8 +9751,10 @@ export {
9006
9751
  STATUS_WAIT_CAP_SECONDS,
9007
9752
  STRIP_PREFIX,
9008
9753
  SURFACE_KINDS,
9754
+ Score,
9009
9755
  Screen,
9010
9756
  SecretScrubber,
9757
+ TOOL_CONTRACT_UNREAD,
9011
9758
  TRANSFORMS,
9012
9759
  TaskEditClient,
9013
9760
  TaskRun,
@@ -9019,14 +9766,17 @@ export {
9019
9766
  WorldBrowser,
9020
9767
  WorldCallTooLarge,
9021
9768
  WorldEditClient,
9769
+ WorldNotice,
9022
9770
  WorldRequestLog,
9023
9771
  WorldSandboxClient,
9024
9772
  WorldSession,
9025
9773
  WorldSessionCallError,
9774
+ WorldSessionOpenFailed,
9026
9775
  WorldSessionTimeout,
9027
9776
  WorldSet,
9028
9777
  WorldSurfaceUnavailable,
9029
9778
  WorldTask,
9779
+ WorldToolsUnread,
9030
9780
  aggregateTaskGrade,
9031
9781
  apiRequest,
9032
9782
  applyTransforms,
@@ -9069,6 +9819,7 @@ export {
9069
9819
  listTaskSessions,
9070
9820
  listTasks,
9071
9821
  localTreeState,
9822
+ messageOf,
9072
9823
  migrateProjectWorlds,
9073
9824
  migrateWorld,
9074
9825
  modelToolName,
@@ -9112,10 +9863,14 @@ export {
9112
9863
  setEnvSecret,
9113
9864
  shapeText,
9114
9865
  snapshotTreeHashes,
9866
+ sqlRows,
9867
+ stillStartingHint,
9115
9868
  storedForms,
9116
9869
  surfaceDescriptor,
9117
9870
  surfaceKinds,
9871
+ taskGradeError,
9118
9872
  taskLabel,
9873
+ taskRunFailure,
9119
9874
  toolCallRecordsIn,
9120
9875
  toolCallsFromObservations,
9121
9876
  uiEntryUrl,