@tangle-network/agent-app 0.60.62 → 0.60.63

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 (53) hide show
  1. package/dist/app-auth/index.js +3 -3
  2. package/dist/assistant/index.js +7 -7
  3. package/dist/chat-routes/index.js +12 -12
  4. package/dist/chat-store/index.js +3 -3
  5. package/dist/chunk-5UQJ7F3P.js +41 -0
  6. package/dist/chunk-5UQJ7F3P.js.map +1 -0
  7. package/dist/{chunk-YXYQ6MJL.js → chunk-5V6VTO5Q.js} +65 -2
  8. package/dist/chunk-5V6VTO5Q.js.map +1 -0
  9. package/dist/chunk-J2O76TTS.js +458 -0
  10. package/dist/chunk-J2O76TTS.js.map +1 -0
  11. package/dist/{chunk-2YLSWZ5W.js → chunk-JBEFTXDO.js} +4 -4
  12. package/dist/{chunk-JGZ7WEPL.js → chunk-JC7YSRBR.js} +15 -1
  13. package/dist/chunk-JC7YSRBR.js.map +1 -0
  14. package/dist/{chunk-4WJREKXX.js → chunk-O3ZM5QPC.js} +6 -6
  15. package/dist/{chunk-FWLCPAST.js → chunk-SH56N4PV.js} +3 -3
  16. package/dist/{chunk-ZGUXI5SI.js → chunk-XNKLQG26.js} +5 -5
  17. package/dist/chunk-Y6LE5CAW.js +503 -0
  18. package/dist/chunk-Y6LE5CAW.js.map +1 -0
  19. package/dist/design-canvas-react/index.js +4 -4
  20. package/dist/launch-invariants/auth-lookup-cache.d.ts +57 -0
  21. package/dist/launch-invariants/catalog.d.ts +51 -0
  22. package/dist/launch-invariants/cli.d.ts +10 -0
  23. package/dist/launch-invariants/cli.js +88 -0
  24. package/dist/launch-invariants/cli.js.map +1 -0
  25. package/dist/launch-invariants/conformance.d.ts +49 -0
  26. package/dist/launch-invariants/index.d.ts +19 -0
  27. package/dist/launch-invariants/index.js +253 -0
  28. package/dist/launch-invariants/index.js.map +1 -0
  29. package/dist/launch-invariants/limit-alarms.d.ts +66 -0
  30. package/dist/launch-invariants/schedule.d.ts +114 -0
  31. package/dist/launch-invariants/settlement.d.ts +90 -0
  32. package/dist/launch-invariants/sized-reads.d.ts +50 -0
  33. package/dist/launch-invariants/testing.d.ts +175 -0
  34. package/dist/launch-invariants/testing.js +45 -0
  35. package/dist/launch-invariants/testing.js.map +1 -0
  36. package/dist/public-consultation/index.d.ts +1 -1
  37. package/dist/public-consultation/index.js +7 -7
  38. package/dist/stream/index.d.ts +1 -0
  39. package/dist/stream/index.js +9 -5
  40. package/dist/stream/orphaned-turns.d.ts +56 -0
  41. package/dist/stream/turn-buffer.d.ts +12 -0
  42. package/dist/teams/invitations-api.js +3 -3
  43. package/dist/turn-health/index.js +6 -17
  44. package/dist/turn-health/index.js.map +1 -1
  45. package/dist/turn-stream/index.js +1 -1
  46. package/dist/web-react/index.js +13 -13
  47. package/package.json +13 -2
  48. package/dist/chunk-JGZ7WEPL.js.map +0 -1
  49. package/dist/chunk-YXYQ6MJL.js.map +0 -1
  50. /package/dist/{chunk-2YLSWZ5W.js.map → chunk-JBEFTXDO.js.map} +0 -0
  51. /package/dist/{chunk-4WJREKXX.js.map → chunk-O3ZM5QPC.js.map} +0 -0
  52. /package/dist/{chunk-FWLCPAST.js.map → chunk-SH56N4PV.js.map} +0 -0
  53. /package/dist/{chunk-ZGUXI5SI.js.map → chunk-XNKLQG26.js.map} +0 -0
@@ -0,0 +1,458 @@
1
+ // src/launch-invariants/catalog.ts
2
+ var LAUNCH_BUDGETS = {
3
+ /** Most bytes one query may return in a scheduled job or report route: one sized batch with room for its listing. */
4
+ queryBytes: 8 * 1024 * 1024,
5
+ /** Most the live heap may grow while one scheduled job runs on a day-sized fixture. */
6
+ heapGrowthBytes: 40 * 1024 * 1024,
7
+ /** Most bytes of a large column one sized read returns; a single larger row is read alone. */
8
+ sizedReadBytes: 4 * 1024 * 1024,
9
+ /** Most stored events one long turn may write. */
10
+ turnEvents: 5e3,
11
+ /** A turn whose owner stopped reaches its terminal state within this long. */
12
+ settleWithinMs: 15 * 6e4,
13
+ /** Turn recovery and orphan settlement run at least this often. */
14
+ recoveryEveryMs: 15 * 6e4,
15
+ /** Longest a cached auth lookup may be served while the store cannot answer. */
16
+ authCacheTtlMs: 6e4,
17
+ /** A platform-limit alarm fires at this share of the limit. */
18
+ alarmRatio: 0.8
19
+ };
20
+ var LAUNCH_INVARIANTS = [
21
+ {
22
+ id: "bounded-reads",
23
+ title: "Bounded reads",
24
+ rule: "No query in a scheduled job or report route returns more than 8 MB; large columns are read in sized batches of at most 4 MB.",
25
+ incident: "GTM turn health read 500 replies with their parts (122 MB of a 128 MB Worker); the reliability report scanned 1.63 M events and overloaded D1."
26
+ },
27
+ {
28
+ id: "memory-budget",
29
+ title: "Memory budget",
30
+ rule: "Every scheduled job runs on a day-sized fixture with live-heap growth of at most 40 MB, and every scheduled job is covered.",
31
+ incident: "GTM sweeps exceeded the Worker's 128 MB six times; a regex slice kept in a Map pinned whole tool outputs."
32
+ },
33
+ {
34
+ id: "isolated-jobs",
35
+ title: "Isolated scheduled jobs",
36
+ rule: "Each scheduled invocation runs exactly one job, and turn recovery and orphan settlement run at least every 15 minutes.",
37
+ incident: "At 21:00Z turn health ran out of memory while the hourly sweeps shared one run, and none of the eight queued after it ran."
38
+ },
39
+ {
40
+ id: "honest-settlement",
41
+ title: "Honest turn settlement",
42
+ rule: "Every turn reaches exactly one terminal state (an answer, a typed failure with Retry, or a Stop counted on its own) within 15 minutes of its owner stopping; failures are attributed to the request time, and a completed run with an aborted tool is a failure.",
43
+ incident: "Stale running rows (51 settled by hand), failure notices 8\u201311 h late, and completions with aborted tools counted as answers."
44
+ },
45
+ {
46
+ id: "coalesced-events",
47
+ title: "Coalesced turn events",
48
+ rule: "A recorded long turn stores at most 5,000 events: text and reasoning deltas are merged before storage.",
49
+ incident: "A reasoning model wrote 59,659 events in one turn and 1.63 M in a day."
50
+ },
51
+ {
52
+ id: "auth-survives-d1-stall",
53
+ title: "Auth survives a D1 stall",
54
+ rule: "A key or session verified within 60 s keeps working while D1 errors or stalls; the cache is used only then, never past revoke or expiry, and is keyed by SHA-256.",
55
+ incident: "An overloaded D1 failed sign-in and API-key checks for credentials that were valid moments before."
56
+ },
57
+ {
58
+ id: "authorize-before-stream",
59
+ title: "Authorization before any stream",
60
+ rule: "A refused request gets a plain response; no stream opens and no product state is touched before authorization resolves.",
61
+ incident: "agent-app 0.60.48\u20130.60.49 opened a progress-first stream before authorize ran."
62
+ },
63
+ {
64
+ id: "limit-alarms",
65
+ title: "Platform-limit alarms at 80%",
66
+ rule: "Worker memory, D1 rows per query, sandbox disk, snapshot count and key rate each have a declared limit and an alarm that fires at 80%.",
67
+ incident: "19 platform-limit events on 2026-10-10, each found after it failed a customer."
68
+ }
69
+ ];
70
+ var LAUNCH_INVARIANT_IDS = LAUNCH_INVARIANTS.map((invariant) => invariant.id);
71
+ function describeVerdict(verdict) {
72
+ const head = `${verdict.pass ? "PASS" : "FAIL"} ${verdict.invariant} \u2014 ${verdict.subject}`;
73
+ return verdict.details.length ? `${head}
74
+ ${verdict.details.join("\n ")}` : head;
75
+ }
76
+
77
+ // src/launch-invariants/schedule.ts
78
+ function createScheduledDispatch(options) {
79
+ const problems = validateEntries(options.entries, Object.keys(options.jobs));
80
+ if (problems.length) throw new Error(`scheduled dispatch: ${problems.join("; ")}`);
81
+ const byCron = new Map(options.entries.map((entry) => [entry.cron, entry]));
82
+ const retryDelaysMs = options.retryDelaysMs ?? [];
83
+ const log = options.log ?? {
84
+ info: (message, detail) => console.log(message, detail ?? ""),
85
+ error: (message, detail) => console.error(message, detail ?? "")
86
+ };
87
+ const sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
88
+ function jobFor(cron, scheduledTime) {
89
+ const entry = byCron.get(cron);
90
+ if (!entry) return null;
91
+ if ("job" in entry) return entry.job;
92
+ const minute = new Date(scheduledTime).getUTCMinutes();
93
+ return entry.minutes[minute] ?? null;
94
+ }
95
+ async function runJob(job, cron, scheduledTime, context) {
96
+ const run = options.jobs[job];
97
+ const started = Date.now();
98
+ let lastError = "";
99
+ for (let attempt = 1; attempt <= retryDelaysMs.length + 1; attempt += 1) {
100
+ try {
101
+ await run({ cron, scheduledTime, context, attempt });
102
+ return { job, ok: true, attempts: attempt, durationMs: Date.now() - started };
103
+ } catch (error) {
104
+ lastError = error instanceof Error ? error.message : String(error);
105
+ const delay = retryDelaysMs[attempt - 1];
106
+ if (delay === void 0) break;
107
+ log.error(`[scheduled] ${job} failed; retrying`, { attempt, error: lastError });
108
+ await sleep(delay);
109
+ }
110
+ }
111
+ log.error(`[scheduled] ${job} failed`, { attempts: retryDelaysMs.length + 1, error: lastError });
112
+ return { job, ok: false, attempts: retryDelaysMs.length + 1, durationMs: Date.now() - started, error: lastError };
113
+ }
114
+ return {
115
+ entries: options.entries,
116
+ slots: () => slotsOf(options.entries),
117
+ jobFor,
118
+ async dispatch(event, context, waitUntil) {
119
+ const scheduledTime = new Date(event.scheduledTime);
120
+ const job = jobFor(event.cron, scheduledTime);
121
+ if (!job) {
122
+ log.error(`[scheduled] no job handles cron "${event.cron}" at minute ${scheduledTime.getUTCMinutes()}; the configured crons and the schedule disagree`);
123
+ return { handled: false, job: null, done: Promise.resolve(null) };
124
+ }
125
+ const done = runJob(job, event.cron, scheduledTime, context);
126
+ if (waitUntil) waitUntil(done);
127
+ else await done;
128
+ return { handled: true, job, done };
129
+ }
130
+ };
131
+ }
132
+ function slotsOf(entries) {
133
+ return entries.flatMap((entry) => "job" in entry ? [{ cron: entry.cron, job: entry.job }] : Object.entries(entry.minutes).map(([minute, job]) => ({ cron: entry.cron, minute: Number(minute), job })));
134
+ }
135
+ function validateEntries(entries, jobNames) {
136
+ const problems = [];
137
+ const known = new Set(jobNames);
138
+ const seen = /* @__PURE__ */ new Set();
139
+ for (const entry of entries) {
140
+ if (seen.has(entry.cron)) problems.push(`cron "${entry.cron}" is listed twice`);
141
+ seen.add(entry.cron);
142
+ const fields = entry.cron.trim().split(/\s+/);
143
+ if (fields.length !== 5) problems.push(`cron "${entry.cron}" does not have five fields`);
144
+ for (const slot of slotsOf([entry])) {
145
+ if (!known.has(slot.job)) problems.push(`cron "${entry.cron}" names job "${slot.job}", which is not defined`);
146
+ }
147
+ if ("minutes" in entry) {
148
+ const listed = minuteList(fields[0] ?? "");
149
+ const mapped = Object.keys(entry.minutes).map(Number).sort((a, b) => a - b);
150
+ if (!listed) problems.push(`cron "${entry.cron}" maps jobs by minute, so its minute field must be a list of minutes`);
151
+ else if (listed.join(",") !== mapped.join(",")) {
152
+ problems.push(`cron "${entry.cron}" lists minutes ${listed.join(",")} but maps jobs to ${mapped.join(",")}`);
153
+ }
154
+ }
155
+ }
156
+ return problems;
157
+ }
158
+ function minuteList(field) {
159
+ if (!/^\d{1,2}(,\d{1,2})*$/.test(field)) return null;
160
+ const minutes = field.split(",").map(Number);
161
+ if (minutes.some((minute) => minute > 59)) return null;
162
+ return [...new Set(minutes)].sort((a, b) => a - b);
163
+ }
164
+ function expandField(field, min, max) {
165
+ const values = /* @__PURE__ */ new Set();
166
+ for (const part of field.split(",")) {
167
+ const match = /^(\*|\d+(?:-\d+)?)(?:\/(\d+))?$/.exec(part);
168
+ if (!match) return null;
169
+ const [, range, stepText] = match;
170
+ const step = stepText ? Number(stepText) : 1;
171
+ if (!(step >= 1)) return null;
172
+ let lo = min;
173
+ let hi = max;
174
+ if (range !== "*") {
175
+ const [a, b] = range.split("-").map(Number);
176
+ lo = a;
177
+ hi = b ?? (stepText ? max : a);
178
+ }
179
+ if (lo < min || hi > max || lo > hi) return null;
180
+ for (let value = lo; value <= hi; value += step) values.add(value);
181
+ }
182
+ return values;
183
+ }
184
+ function cronDailyFirings(cron) {
185
+ const fields = cron.trim().split(/\s+/);
186
+ if (fields.length !== 5) return null;
187
+ const [minuteField, hourField, dayField, monthField, weekdayField] = fields;
188
+ if (dayField !== "*" || monthField !== "*" || weekdayField !== "*" && weekdayField !== "?") return null;
189
+ const minutes = expandField(minuteField, 0, 59);
190
+ const hours = expandField(hourField, 0, 23);
191
+ if (!minutes || !hours) return null;
192
+ const firings = /* @__PURE__ */ new Set();
193
+ for (const hour of hours) for (const minute of minutes) firings.add(hour * 60 + minute);
194
+ return firings;
195
+ }
196
+ function longestGapMinutes(firings) {
197
+ const sorted = [...new Set(firings)].sort((a, b) => a - b);
198
+ if (sorted.length === 0) return null;
199
+ let gap = sorted[0] + 1440 - sorted[sorted.length - 1];
200
+ for (let index = 1; index < sorted.length; index += 1) gap = Math.max(gap, sorted[index] - sorted[index - 1]);
201
+ return gap;
202
+ }
203
+ function parseWranglerCrons(source, format) {
204
+ return format === "json" ? parseJsonCrons(source) : parseTomlCrons(source);
205
+ }
206
+ function parseTomlCrons(source) {
207
+ const result = { top: [], envs: {} };
208
+ let table = "";
209
+ let collecting = null;
210
+ let buffer = "";
211
+ for (const raw of source.split("\n")) {
212
+ const line = stripTomlComment(raw);
213
+ if (collecting) {
214
+ buffer += ` ${line}`;
215
+ if (line.includes("]")) {
216
+ collecting.push(...quoted(buffer));
217
+ collecting = null;
218
+ buffer = "";
219
+ }
220
+ continue;
221
+ }
222
+ const header = /^\s*\[([^\[\]]+)\]\s*$/.exec(line);
223
+ if (header) {
224
+ table = header[1].trim();
225
+ continue;
226
+ }
227
+ const assignment = /^\s*crons\s*=\s*(.*)$/.exec(line);
228
+ if (!assignment) continue;
229
+ const envName = /^env\.([^.]+)\.triggers$/.exec(table)?.[1];
230
+ const target = table === "triggers" ? result.top : envName ? result.envs[envName] ??= [] : null;
231
+ if (!target) continue;
232
+ const rest = assignment[1];
233
+ if (rest.includes("]")) target.push(...quoted(rest));
234
+ else {
235
+ collecting = target;
236
+ buffer = rest;
237
+ }
238
+ }
239
+ return result;
240
+ }
241
+ function stripTomlComment(line) {
242
+ let quote = null;
243
+ for (let index = 0; index < line.length; index += 1) {
244
+ const char = line[index];
245
+ if (quote) {
246
+ if (char === quote) quote = null;
247
+ } else if (char === '"' || char === "'") quote = char;
248
+ else if (char === "#") return line.slice(0, index);
249
+ }
250
+ return line;
251
+ }
252
+ function quoted(text) {
253
+ return [...text.matchAll(/"([^"]*)"|'([^']*)'/g)].map((match) => match[1] ?? match[2] ?? "");
254
+ }
255
+ function parseJsonCrons(source) {
256
+ const config = JSON.parse(stripJsonComments(source));
257
+ return {
258
+ top: [...config.triggers?.crons ?? []],
259
+ envs: Object.fromEntries(Object.entries(config.env ?? {}).filter(([, env]) => Array.isArray(env.triggers?.crons)).map(([name, env]) => [name, [...env.triggers.crons]]))
260
+ };
261
+ }
262
+ function stripJsonComments(source) {
263
+ let out = "";
264
+ let inString = false;
265
+ for (let index = 0; index < source.length; index += 1) {
266
+ const char = source[index];
267
+ const next = source[index + 1];
268
+ if (inString) {
269
+ out += char;
270
+ if (char === "\\") out += source[++index] ?? "";
271
+ else if (char === '"') inString = false;
272
+ continue;
273
+ }
274
+ if (char === '"') {
275
+ inString = true;
276
+ out += char;
277
+ } else if (char === "/" && next === "/") {
278
+ while (index < source.length && source[index] !== "\n") index += 1;
279
+ out += "\n";
280
+ } else if (char === "/" && next === "*") {
281
+ index += 2;
282
+ while (index < source.length && !(source[index] === "*" && source[index + 1] === "/")) index += 1;
283
+ index += 1;
284
+ } else out += char;
285
+ }
286
+ return out.replace(/,(\s*[}\]])/g, "$1");
287
+ }
288
+ function checkIsolatedJobs(input) {
289
+ const everyMinutes = (input.everyMs ?? LAUNCH_BUDGETS.recoveryEveryMs) / 6e4;
290
+ const findings = [];
291
+ const configured = new Set(input.configuredCrons);
292
+ const dispatched = new Set(input.slots.map((slot) => slot.cron));
293
+ for (const cron of configured) if (!dispatched.has(cron)) findings.push(`configured cron "${cron}" runs no job: no schedule entry matches it`);
294
+ for (const cron of dispatched) if (!configured.has(cron)) findings.push(`schedule cron "${cron}" is not configured, so it never runs`);
295
+ for (const slot of input.slots) {
296
+ const where = `cron "${slot.cron}"${slot.minute !== void 0 ? ` at minute ${slot.minute}` : ""}`;
297
+ if (slot.jobs.length !== 1) findings.push(`${where} runs ${slot.jobs.length} jobs in one invocation (${slot.jobs.join(", ") || "none"}); one failure ends the rest`);
298
+ }
299
+ const byCron = /* @__PURE__ */ new Map();
300
+ for (const slot of input.slots) byCron.set(slot.cron, [...byCron.get(slot.cron) ?? [], slot]);
301
+ for (const [cron, slots] of byCron) {
302
+ const minuteSlots = slots.filter((slot) => slot.minute !== void 0);
303
+ if (minuteSlots.length === 0) continue;
304
+ const listed = minuteList(cron.trim().split(/\s+/)[0] ?? "");
305
+ const mapped = [...new Set(minuteSlots.map((slot) => slot.minute))].sort((a, b) => a - b);
306
+ if (!listed || listed.join(",") !== mapped.join(",")) {
307
+ findings.push(`cron "${cron}" fires at minutes ${listed?.join(",") ?? "(not a minute list)"} but jobs are mapped to ${mapped.join(",")}`);
308
+ }
309
+ }
310
+ if (input.recoveryJobs.length === 0) findings.push("no turn recovery or orphan settlement job is declared");
311
+ const gaps = {};
312
+ for (const job of input.recoveryJobs) {
313
+ const firings = /* @__PURE__ */ new Set();
314
+ let daily = false;
315
+ for (const slot of input.slots) {
316
+ if (slot.jobs.length !== 1 || slot.jobs[0] !== job) continue;
317
+ const fires = cronDailyFirings(slot.cron);
318
+ if (!fires) continue;
319
+ daily = true;
320
+ for (const minuteOfDay of fires) if (slot.minute === void 0 || minuteOfDay % 60 === slot.minute) firings.add(minuteOfDay);
321
+ }
322
+ const gap = daily ? longestGapMinutes(firings) : null;
323
+ gaps[job] = gap;
324
+ if (gap === null) findings.push(`recovery job "${job}" is not scheduled to run every day on its own`);
325
+ else if (gap > everyMinutes) findings.push(`recovery job "${job}" can wait ${gap} minutes between runs; the most is ${everyMinutes}`);
326
+ }
327
+ return {
328
+ invariant: "isolated-jobs",
329
+ subject: "schedule",
330
+ pass: findings.length === 0,
331
+ details: findings.length ? findings : [
332
+ `${input.slots.length} scheduled invocations, one job each, across ${configured.size} configured crons`,
333
+ ...input.recoveryJobs.map((job) => `${job} runs at least every ${gaps[job]} minutes`)
334
+ ],
335
+ data: { configuredCrons: [...configured], slots: input.slots.length, recoveryGapsMinutes: gaps }
336
+ };
337
+ }
338
+
339
+ // src/launch-invariants/limit-alarms.ts
340
+ var LIMIT_RESOURCES = [
341
+ "worker-memory",
342
+ "d1-rows-per-query",
343
+ "sandbox-disk",
344
+ "snapshot-count",
345
+ "key-rate"
346
+ ];
347
+ var UNITS = {
348
+ "worker-memory": "bytes",
349
+ "d1-rows-per-query": "rows",
350
+ "sandbox-disk": "bytes",
351
+ "snapshot-count": "snapshots",
352
+ "key-rate": "requests/min"
353
+ };
354
+ function createLimitAlarms(options) {
355
+ const ratio = Math.min(options.ratio ?? LAUNCH_BUDGETS.alarmRatio, 1);
356
+ const now = options.now ?? (() => Date.now());
357
+ for (const resource of LIMIT_RESOURCES) {
358
+ const limit = options.budgets[resource];
359
+ if (!(typeof limit === "number" && limit > 0 && Number.isFinite(limit))) {
360
+ throw new Error(`limit alarms: ${resource} needs a positive limit`);
361
+ }
362
+ }
363
+ return {
364
+ budgets: options.budgets,
365
+ async observe(resource, used, context) {
366
+ const limit = options.budgets[resource];
367
+ const share = used / limit;
368
+ const level = share >= 1 ? "critical" : share >= ratio ? "warning" : "ok";
369
+ const observation = { resource, used, limit, share, level };
370
+ if (level === "ok") return observation;
371
+ const alert = {
372
+ product: options.product,
373
+ severity: level,
374
+ // Keyed by product and resource only, so a throttled sink pages once per resource.
375
+ key: `limit:${options.product}:${resource}`,
376
+ title: `${options.product}: ${resource} at ${Math.round(share * 100)}% of its limit`,
377
+ details: [
378
+ `${formatAmount(used, resource)} of ${formatAmount(limit, resource)} ${UNITS[resource]}`,
379
+ ...context?.subject ? [`in ${context.subject}`] : [],
380
+ ...context?.detail ? [context.detail] : []
381
+ ],
382
+ data: { resource, used, limit, share, ...context?.subject ? { subject: context.subject } : {} },
383
+ at: now()
384
+ };
385
+ await options.sink.deliver(alert);
386
+ return observation;
387
+ }
388
+ };
389
+ }
390
+ function formatAmount(value, resource) {
391
+ if (UNITS[resource] === "bytes") return `${(value / (1024 * 1024)).toFixed(1)} MB`;
392
+ return Math.round(value).toLocaleString("en-US");
393
+ }
394
+ function withD1LimitAlarms(d1, alarms, options = {}) {
395
+ const observe = (sql, result) => {
396
+ const meta = result?.meta;
397
+ const rowsRead = typeof meta?.rows_read === "number" ? meta.rows_read : rowsOf(result);
398
+ const pending = [];
399
+ if (rowsRead !== null) pending.push(alarms.observe("d1-rows-per-query", rowsRead, { subject: options.subject, detail: sql }));
400
+ const results = result?.results ?? (Array.isArray(result) ? result : null);
401
+ if (results && results.length > 0) {
402
+ pending.push(alarms.observe("worker-memory", estimatedBytes(results), { subject: options.subject, detail: `one response: ${sql}` }));
403
+ }
404
+ return Promise.all(pending).catch(() => void 0);
405
+ };
406
+ const statement = (target, sql) => new Proxy(target, {
407
+ get(stmt, key) {
408
+ const value = Reflect.get(stmt, key);
409
+ if (typeof value !== "function") return value;
410
+ if (key === "bind") return (...params) => statement(value.apply(stmt, params), sql);
411
+ if (key === "all" || key === "raw" || key === "run" || key === "first") {
412
+ return async (...args) => {
413
+ const result = await value.apply(stmt, args);
414
+ if (key !== "first") await observe(sql, result);
415
+ return result;
416
+ };
417
+ }
418
+ return value.bind(stmt);
419
+ }
420
+ });
421
+ return new Proxy(d1, {
422
+ get(binding, key) {
423
+ const value = Reflect.get(binding, key);
424
+ if (key === "prepare") {
425
+ return (sql) => statement(value.call(binding, sql), sql.replace(/\s+/g, " ").slice(0, 160));
426
+ }
427
+ return typeof value === "function" ? value.bind(binding) : value;
428
+ }
429
+ });
430
+ }
431
+ function estimatedBytes(results) {
432
+ const picks = [.../* @__PURE__ */ new Set([0, Math.floor(results.length / 2), results.length - 1])];
433
+ const sampled = picks.reduce((sum, index) => sum + (JSON.stringify(results[index]) ?? "").length, 0);
434
+ return Math.round(sampled / picks.length * results.length);
435
+ }
436
+ function rowsOf(result) {
437
+ if (Array.isArray(result)) return result.length;
438
+ const results = result?.results;
439
+ return Array.isArray(results) ? results.length : null;
440
+ }
441
+
442
+ export {
443
+ LAUNCH_BUDGETS,
444
+ LAUNCH_INVARIANTS,
445
+ LAUNCH_INVARIANT_IDS,
446
+ describeVerdict,
447
+ createScheduledDispatch,
448
+ slotsOf,
449
+ cronDailyFirings,
450
+ longestGapMinutes,
451
+ parseWranglerCrons,
452
+ checkIsolatedJobs,
453
+ LIMIT_RESOURCES,
454
+ createLimitAlarms,
455
+ withD1LimitAlarms,
456
+ estimatedBytes
457
+ };
458
+ //# sourceMappingURL=chunk-J2O76TTS.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/launch-invariants/catalog.ts","../src/launch-invariants/schedule.ts","../src/launch-invariants/limit-alarms.ts"],"sourcesContent":["/**\n * The launch invariants every agent app holds, and the budgets they are held to.\n *\n * Each one is a failure GTM shipped to production on 2026-10-10 and fixed by\n * hand. They are listed here once so every app checks the same rule with the\n * same numbers, and `agent-app-invariants` reports each by its id.\n */\n\n/** Budgets the invariants are measured against. */\nexport const LAUNCH_BUDGETS = {\n /** Most bytes one query may return in a scheduled job or report route: one sized batch with room for its listing. */\n queryBytes: 8 * 1024 * 1024,\n /** Most the live heap may grow while one scheduled job runs on a day-sized fixture. */\n heapGrowthBytes: 40 * 1024 * 1024,\n /** Most bytes of a large column one sized read returns; a single larger row is read alone. */\n sizedReadBytes: 4 * 1024 * 1024,\n /** Most stored events one long turn may write. */\n turnEvents: 5_000,\n /** A turn whose owner stopped reaches its terminal state within this long. */\n settleWithinMs: 15 * 60_000,\n /** Turn recovery and orphan settlement run at least this often. */\n recoveryEveryMs: 15 * 60_000,\n /** Longest a cached auth lookup may be served while the store cannot answer. */\n authCacheTtlMs: 60_000,\n /** A platform-limit alarm fires at this share of the limit. */\n alarmRatio: 0.8,\n} as const\n\nexport type LaunchInvariantId =\n | 'bounded-reads'\n | 'memory-budget'\n | 'isolated-jobs'\n | 'honest-settlement'\n | 'coalesced-events'\n | 'auth-survives-d1-stall'\n | 'authorize-before-stream'\n | 'limit-alarms'\n\nexport interface LaunchInvariant {\n id: LaunchInvariantId\n title: string\n /** The rule, as the conformance report states it. */\n rule: string\n /** The production failure the rule answers. */\n incident: string\n}\n\nexport const LAUNCH_INVARIANTS: readonly LaunchInvariant[] = [\n {\n id: 'bounded-reads',\n title: 'Bounded reads',\n rule: 'No query in a scheduled job or report route returns more than 8 MB; large columns are read in sized batches of at most 4 MB.',\n incident: 'GTM turn health read 500 replies with their parts (122 MB of a 128 MB Worker); the reliability report scanned 1.63 M events and overloaded D1.',\n },\n {\n id: 'memory-budget',\n title: 'Memory budget',\n rule: 'Every scheduled job runs on a day-sized fixture with live-heap growth of at most 40 MB, and every scheduled job is covered.',\n incident: 'GTM sweeps exceeded the Worker\\'s 128 MB six times; a regex slice kept in a Map pinned whole tool outputs.',\n },\n {\n id: 'isolated-jobs',\n title: 'Isolated scheduled jobs',\n rule: 'Each scheduled invocation runs exactly one job, and turn recovery and orphan settlement run at least every 15 minutes.',\n incident: 'At 21:00Z turn health ran out of memory while the hourly sweeps shared one run, and none of the eight queued after it ran.',\n },\n {\n id: 'honest-settlement',\n title: 'Honest turn settlement',\n rule: 'Every turn reaches exactly one terminal state (an answer, a typed failure with Retry, or a Stop counted on its own) within 15 minutes of its owner stopping; failures are attributed to the request time, and a completed run with an aborted tool is a failure.',\n incident: 'Stale running rows (51 settled by hand), failure notices 8–11 h late, and completions with aborted tools counted as answers.',\n },\n {\n id: 'coalesced-events',\n title: 'Coalesced turn events',\n rule: 'A recorded long turn stores at most 5,000 events: text and reasoning deltas are merged before storage.',\n incident: 'A reasoning model wrote 59,659 events in one turn and 1.63 M in a day.',\n },\n {\n id: 'auth-survives-d1-stall',\n title: 'Auth survives a D1 stall',\n rule: 'A key or session verified within 60 s keeps working while D1 errors or stalls; the cache is used only then, never past revoke or expiry, and is keyed by SHA-256.',\n incident: 'An overloaded D1 failed sign-in and API-key checks for credentials that were valid moments before.',\n },\n {\n id: 'authorize-before-stream',\n title: 'Authorization before any stream',\n rule: 'A refused request gets a plain response; no stream opens and no product state is touched before authorization resolves.',\n incident: 'agent-app 0.60.48–0.60.49 opened a progress-first stream before authorize ran.',\n },\n {\n id: 'limit-alarms',\n title: 'Platform-limit alarms at 80%',\n rule: 'Worker memory, D1 rows per query, sandbox disk, snapshot count and key rate each have a declared limit and an alarm that fires at 80%.',\n incident: '19 platform-limit events on 2026-10-10, each found after it failed a customer.',\n },\n]\n\n/** The invariant ids in report order. */\nexport const LAUNCH_INVARIANT_IDS: readonly LaunchInvariantId[] = LAUNCH_INVARIANTS.map((invariant) => invariant.id)\n\n/** One checked outcome of one invariant, for one subject (a job, a route, a scenario). */\nexport interface InvariantVerdict {\n invariant: LaunchInvariantId\n /** What was checked: a job name, a route, a scenario. */\n subject: string\n pass: boolean\n /** Findings, one line each. On a pass, what was proven. */\n details: string[]\n /** Measurements behind the verdict, for the report. */\n data?: Record<string, unknown>\n}\n\n/** A verdict whose message names every finding, for a test runner's failure output. */\nexport function describeVerdict(verdict: InvariantVerdict): string {\n const head = `${verdict.pass ? 'PASS' : 'FAIL'} ${verdict.invariant} — ${verdict.subject}`\n return verdict.details.length ? `${head}\\n ${verdict.details.join('\\n ')}` : head\n}\n","/**\n * Scheduled jobs that cannot take each other down.\n *\n * On 2026-10-10 at 21:00Z GTM's turn health ran out of memory while the hourly\n * sweeps shared one scheduled run, and none of the eight queued after it ran.\n * Here each scheduled invocation runs exactly one job. A cron can carry a\n * minute list (`0,1,2 * * * *`) with one job per minute, so a Worker keeps a\n * few trigger expressions while every job gets its own invocation, its own\n * memory and its own failure.\n *\n * Cloudflare hands the handler the configured string verbatim as `event.cron`,\n * so dispatch is exact-string; a tick no entry handles is reported as\n * unhandled instead of silently running nothing. {@link checkIsolatedJobs}\n * holds the table against the deployment's configured crons.\n */\nimport { LAUNCH_BUDGETS, type InvariantVerdict } from './catalog.js'\n\n/** One cron expression and the job it runs, or one job per listed minute. */\nexport type ScheduleEntry =\n | { cron: string; job: string }\n | { cron: string; minutes: Readonly<Record<number, string>> }\n\n/** One scheduled invocation: the cron, the minute for a minute-list entry, and the job it runs. */\nexport interface ScheduleSlot {\n cron: string\n minute?: number\n job: string\n}\n\nexport interface ScheduledJobInput<Ctx> {\n cron: string\n scheduledTime: Date\n context: Ctx\n /** 1 for the first try. */\n attempt: number\n}\n\nexport type ScheduledJob<Ctx> = (input: ScheduledJobInput<Ctx>) => Promise<unknown>\n\nexport interface ScheduledDispatchOptions<Ctx> {\n entries: readonly ScheduleEntry[]\n jobs: Readonly<Record<string, ScheduledJob<Ctx>>>\n /** Waits before each retry of a failed job; empty (the default) runs a job once. Jobs that retry must be idempotent. */\n retryDelaysMs?: readonly number[]\n log?: {\n info(message: string, detail?: Record<string, unknown>): void\n error(message: string, detail?: Record<string, unknown>): void\n }\n /** Tests pass a no-op. */\n sleep?: (ms: number) => Promise<void>\n}\n\nexport interface ScheduledJobOutcome {\n job: string\n ok: boolean\n attempts: number\n durationMs: number\n error?: string\n}\n\nexport interface ScheduledDispatchResult {\n handled: boolean\n job: string | null\n /** Settles when the job's last attempt ends; never rejects. */\n done: Promise<ScheduledJobOutcome | null>\n}\n\nexport interface ScheduledDispatch<Ctx> {\n readonly entries: readonly ScheduleEntry[]\n /** Every invocation the table can make. */\n slots(): ScheduleSlot[]\n /** The job a tick runs, or null when no entry handles it. */\n jobFor(cron: string, scheduledTime: number | Date): string | null\n /**\n * Run the tick's one job. With `waitUntil` the job runs in the background and\n * this returns at once; without it, this waits for the job.\n */\n dispatch(\n event: { cron: string; scheduledTime: number | Date },\n context: Ctx,\n waitUntil?: (promise: Promise<unknown>) => void,\n ): Promise<ScheduledDispatchResult>\n}\n\nexport function createScheduledDispatch<Ctx>(options: ScheduledDispatchOptions<Ctx>): ScheduledDispatch<Ctx> {\n const problems = validateEntries(options.entries, Object.keys(options.jobs))\n if (problems.length) throw new Error(`scheduled dispatch: ${problems.join('; ')}`)\n const byCron = new Map(options.entries.map((entry) => [entry.cron, entry]))\n const retryDelaysMs = options.retryDelaysMs ?? []\n const log = options.log ?? {\n info: (message: string, detail?: Record<string, unknown>) => console.log(message, detail ?? ''),\n error: (message: string, detail?: Record<string, unknown>) => console.error(message, detail ?? ''),\n }\n const sleep = options.sleep ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)))\n\n function jobFor(cron: string, scheduledTime: number | Date): string | null {\n const entry = byCron.get(cron)\n if (!entry) return null\n if ('job' in entry) return entry.job\n const minute = new Date(scheduledTime).getUTCMinutes()\n return entry.minutes[minute] ?? null\n }\n\n async function runJob(job: string, cron: string, scheduledTime: Date, context: Ctx): Promise<ScheduledJobOutcome> {\n const run = options.jobs[job]!\n const started = Date.now()\n let lastError = ''\n for (let attempt = 1; attempt <= retryDelaysMs.length + 1; attempt += 1) {\n try {\n await run({ cron, scheduledTime, context, attempt })\n return { job, ok: true, attempts: attempt, durationMs: Date.now() - started }\n } catch (error) {\n lastError = error instanceof Error ? error.message : String(error)\n const delay = retryDelaysMs[attempt - 1]\n if (delay === undefined) break\n log.error(`[scheduled] ${job} failed; retrying`, { attempt, error: lastError })\n await sleep(delay)\n }\n }\n log.error(`[scheduled] ${job} failed`, { attempts: retryDelaysMs.length + 1, error: lastError })\n return { job, ok: false, attempts: retryDelaysMs.length + 1, durationMs: Date.now() - started, error: lastError }\n }\n\n return {\n entries: options.entries,\n slots: () => slotsOf(options.entries),\n jobFor,\n async dispatch(event, context, waitUntil) {\n const scheduledTime = new Date(event.scheduledTime)\n const job = jobFor(event.cron, scheduledTime)\n if (!job) {\n log.error(`[scheduled] no job handles cron \"${event.cron}\" at minute ${scheduledTime.getUTCMinutes()}; the configured crons and the schedule disagree`)\n return { handled: false, job: null, done: Promise.resolve(null) }\n }\n const done = runJob(job, event.cron, scheduledTime, context)\n if (waitUntil) waitUntil(done)\n else await done\n return { handled: true, job, done }\n },\n }\n}\n\n/** Every invocation a schedule table can make. */\nexport function slotsOf(entries: readonly ScheduleEntry[]): ScheduleSlot[] {\n return entries.flatMap((entry) => 'job' in entry\n ? [{ cron: entry.cron, job: entry.job }]\n : Object.entries(entry.minutes).map(([minute, job]) => ({ cron: entry.cron, minute: Number(minute), job })))\n}\n\nfunction validateEntries(entries: readonly ScheduleEntry[], jobNames: readonly string[]): string[] {\n const problems: string[] = []\n const known = new Set(jobNames)\n const seen = new Set<string>()\n for (const entry of entries) {\n if (seen.has(entry.cron)) problems.push(`cron \"${entry.cron}\" is listed twice`)\n seen.add(entry.cron)\n const fields = entry.cron.trim().split(/\\s+/)\n if (fields.length !== 5) problems.push(`cron \"${entry.cron}\" does not have five fields`)\n for (const slot of slotsOf([entry])) {\n if (!known.has(slot.job)) problems.push(`cron \"${entry.cron}\" names job \"${slot.job}\", which is not defined`)\n }\n if ('minutes' in entry) {\n const listed = minuteList(fields[0] ?? '')\n const mapped = Object.keys(entry.minutes).map(Number).sort((a, b) => a - b)\n if (!listed) problems.push(`cron \"${entry.cron}\" maps jobs by minute, so its minute field must be a list of minutes`)\n else if (listed.join(',') !== mapped.join(',')) {\n problems.push(`cron \"${entry.cron}\" lists minutes ${listed.join(',')} but maps jobs to ${mapped.join(',')}`)\n }\n }\n }\n return problems\n}\n\n/** The minutes of a plain minute list (`5` or `0,15,30`), or null for any other form. */\nfunction minuteList(field: string): number[] | null {\n if (!/^\\d{1,2}(,\\d{1,2})*$/.test(field)) return null\n const minutes = field.split(',').map(Number)\n if (minutes.some((minute) => minute > 59)) return null\n return [...new Set(minutes)].sort((a, b) => a - b)\n}\n\n// ── cron analysis ────────────────────────────────────────────────────────────\n\nfunction expandField(field: string, min: number, max: number): Set<number> | null {\n const values = new Set<number>()\n for (const part of field.split(',')) {\n const match = /^(\\*|\\d+(?:-\\d+)?)(?:\\/(\\d+))?$/.exec(part)\n if (!match) return null\n const [, range, stepText] = match\n const step = stepText ? Number(stepText) : 1\n if (!(step >= 1)) return null\n let lo = min\n let hi = max\n if (range !== '*') {\n const [a, b] = range!.split('-').map(Number)\n lo = a!\n hi = b ?? (stepText ? max : a!)\n }\n if (lo < min || hi > max || lo > hi) return null\n for (let value = lo; value <= hi; value += step) values.add(value)\n }\n return values\n}\n\n/**\n * The minutes of the day (0–1439, UTC) a cron fires, or null when it does not\n * fire every day (a day-of-month, month or weekday restriction) or cannot be read.\n */\nexport function cronDailyFirings(cron: string): Set<number> | null {\n const fields = cron.trim().split(/\\s+/)\n if (fields.length !== 5) return null\n const [minuteField, hourField, dayField, monthField, weekdayField] = fields as [string, string, string, string, string]\n if (dayField !== '*' || monthField !== '*' || (weekdayField !== '*' && weekdayField !== '?')) return null\n const minutes = expandField(minuteField, 0, 59)\n const hours = expandField(hourField, 0, 23)\n if (!minutes || !hours) return null\n const firings = new Set<number>()\n for (const hour of hours) for (const minute of minutes) firings.add(hour * 60 + minute)\n return firings\n}\n\n/** The longest wait, in minutes, between consecutive firings over a day, or null when they do not fire daily. */\nexport function longestGapMinutes(firings: Iterable<number>): number | null {\n const sorted = [...new Set(firings)].sort((a, b) => a - b)\n if (sorted.length === 0) return null\n let gap = sorted[0]! + 1440 - sorted[sorted.length - 1]!\n for (let index = 1; index < sorted.length; index += 1) gap = Math.max(gap, sorted[index]! - sorted[index - 1]!)\n return gap\n}\n\n/** The cron expressions a wrangler config declares, top level and per environment. */\nexport interface WranglerCrons {\n top: string[]\n envs: Record<string, string[]>\n}\n\n/** Read `[triggers] crons` (and `[env.<name>.triggers]`) from wrangler.toml, or `triggers.crons` from wrangler.json(c). */\nexport function parseWranglerCrons(source: string, format: 'toml' | 'json'): WranglerCrons {\n return format === 'json' ? parseJsonCrons(source) : parseTomlCrons(source)\n}\n\nfunction parseTomlCrons(source: string): WranglerCrons {\n const result: WranglerCrons = { top: [], envs: {} }\n let table = ''\n let collecting: string[] | null = null\n let buffer = ''\n for (const raw of source.split('\\n')) {\n const line = stripTomlComment(raw)\n if (collecting) {\n buffer += ` ${line}`\n if (line.includes(']')) {\n collecting.push(...quoted(buffer))\n collecting = null\n buffer = ''\n }\n continue\n }\n const header = /^\\s*\\[([^\\[\\]]+)\\]\\s*$/.exec(line)\n if (header) {\n table = header[1]!.trim()\n continue\n }\n const assignment = /^\\s*crons\\s*=\\s*(.*)$/.exec(line)\n if (!assignment) continue\n const envName = /^env\\.([^.]+)\\.triggers$/.exec(table)?.[1]\n const target = table === 'triggers' ? result.top : envName ? (result.envs[envName] ??= []) : null\n if (!target) continue\n const rest = assignment[1]!\n if (rest.includes(']')) target.push(...quoted(rest))\n else {\n collecting = target\n buffer = rest\n }\n }\n return result\n}\n\nfunction stripTomlComment(line: string): string {\n let quote: string | null = null\n for (let index = 0; index < line.length; index += 1) {\n const char = line[index]!\n if (quote) {\n if (char === quote) quote = null\n } else if (char === '\"' || char === \"'\") quote = char\n else if (char === '#') return line.slice(0, index)\n }\n return line\n}\n\nfunction quoted(text: string): string[] {\n return [...text.matchAll(/\"([^\"]*)\"|'([^']*)'/g)].map((match) => match[1] ?? match[2] ?? '')\n}\n\nfunction parseJsonCrons(source: string): WranglerCrons {\n const config = JSON.parse(stripJsonComments(source)) as {\n triggers?: { crons?: string[] }\n env?: Record<string, { triggers?: { crons?: string[] } }>\n }\n return {\n top: [...(config.triggers?.crons ?? [])],\n envs: Object.fromEntries(Object.entries(config.env ?? {})\n .filter(([, env]) => Array.isArray(env.triggers?.crons))\n .map(([name, env]) => [name, [...env.triggers!.crons!]])),\n }\n}\n\nfunction stripJsonComments(source: string): string {\n let out = ''\n let inString = false\n for (let index = 0; index < source.length; index += 1) {\n const char = source[index]!\n const next = source[index + 1]\n if (inString) {\n out += char\n if (char === '\\\\') out += source[++index] ?? ''\n else if (char === '\"') inString = false\n continue\n }\n if (char === '\"') {\n inString = true\n out += char\n } else if (char === '/' && next === '/') {\n while (index < source.length && source[index] !== '\\n') index += 1\n out += '\\n'\n } else if (char === '/' && next === '*') {\n index += 2\n while (index < source.length && !(source[index] === '*' && source[index + 1] === '/')) index += 1\n index += 1\n } else out += char\n }\n return out.replace(/,(\\s*[}\\]])/g, '$1')\n}\n\n// ── the invariant ────────────────────────────────────────────────────────────\n\n/** What one scheduled invocation was observed to run. */\nexport interface ObservedSlot {\n cron: string\n minute?: number\n /** The jobs the invocation ran; exactly one passes. */\n jobs: readonly string[]\n}\n\nexport interface IsolatedJobsInput {\n /** Every cron the deployment configures, from each wrangler environment. */\n configuredCrons: readonly string[]\n /** What each invocation runs: from {@link slotsOf} for a {@link createScheduledDispatch} table, or observed by driving the app's own handler. */\n slots: readonly ObservedSlot[]\n /** Jobs that recover and settle turns; each must run at least every 15 minutes. */\n recoveryJobs: readonly string[]\n /** Default {@link LAUNCH_BUDGETS.recoveryEveryMs}. */\n everyMs?: number\n}\n\n/** One job per invocation, the table and the deployment agree, and recovery runs at least every 15 minutes. */\nexport function checkIsolatedJobs(input: IsolatedJobsInput): InvariantVerdict {\n const everyMinutes = (input.everyMs ?? LAUNCH_BUDGETS.recoveryEveryMs) / 60_000\n const findings: string[] = []\n const configured = new Set(input.configuredCrons)\n const dispatched = new Set(input.slots.map((slot) => slot.cron))\n for (const cron of configured) if (!dispatched.has(cron)) findings.push(`configured cron \"${cron}\" runs no job: no schedule entry matches it`)\n for (const cron of dispatched) if (!configured.has(cron)) findings.push(`schedule cron \"${cron}\" is not configured, so it never runs`)\n for (const slot of input.slots) {\n const where = `cron \"${slot.cron}\"${slot.minute !== undefined ? ` at minute ${slot.minute}` : ''}`\n if (slot.jobs.length !== 1) findings.push(`${where} runs ${slot.jobs.length} jobs in one invocation (${slot.jobs.join(', ') || 'none'}); one failure ends the rest`)\n }\n const byCron = new Map<string, ObservedSlot[]>()\n for (const slot of input.slots) byCron.set(slot.cron, [...(byCron.get(slot.cron) ?? []), slot])\n for (const [cron, slots] of byCron) {\n const minuteSlots = slots.filter((slot) => slot.minute !== undefined)\n if (minuteSlots.length === 0) continue\n const listed = minuteList(cron.trim().split(/\\s+/)[0] ?? '')\n const mapped = [...new Set(minuteSlots.map((slot) => slot.minute!))].sort((a, b) => a - b)\n if (!listed || listed.join(',') !== mapped.join(',')) {\n findings.push(`cron \"${cron}\" fires at minutes ${listed?.join(',') ?? '(not a minute list)'} but jobs are mapped to ${mapped.join(',')}`)\n }\n }\n if (input.recoveryJobs.length === 0) findings.push('no turn recovery or orphan settlement job is declared')\n const gaps: Record<string, number | null> = {}\n for (const job of input.recoveryJobs) {\n const firings = new Set<number>()\n let daily = false\n for (const slot of input.slots) {\n if (slot.jobs.length !== 1 || slot.jobs[0] !== job) continue\n const fires = cronDailyFirings(slot.cron)\n if (!fires) continue\n daily = true\n for (const minuteOfDay of fires) if (slot.minute === undefined || minuteOfDay % 60 === slot.minute) firings.add(minuteOfDay)\n }\n const gap = daily ? longestGapMinutes(firings) : null\n gaps[job] = gap\n if (gap === null) findings.push(`recovery job \"${job}\" is not scheduled to run every day on its own`)\n else if (gap > everyMinutes) findings.push(`recovery job \"${job}\" can wait ${gap} minutes between runs; the most is ${everyMinutes}`)\n }\n return {\n invariant: 'isolated-jobs',\n subject: 'schedule',\n pass: findings.length === 0,\n details: findings.length ? findings : [\n `${input.slots.length} scheduled invocations, one job each, across ${configured.size} configured crons`,\n ...input.recoveryJobs.map((job) => `${job} runs at least every ${gaps[job]} minutes`),\n ],\n data: { configuredCrons: [...configured], slots: input.slots.length, recoveryGapsMinutes: gaps },\n }\n}\n","/**\n * Alarms that fire at 80% of a platform limit, before the limit fails a customer.\n *\n * On 2026-10-10 GTM recorded 19 platform-limit events (Worker memory, D1,\n * sandbox disk, snapshot count, key rate) and learned of each one after it had\n * failed someone. Each resource here has a declared limit; an observation at\n * or above 80% of it delivers a warning, and at or above the limit a critical\n * alert, through the same {@link AlertSink} the turn-health sweep uses (wrap it\n * in `createThrottledAlertSink` so a hot loop pages once).\n *\n * Where each observation comes from:\n * - `d1-rows-per-query`: {@link withD1LimitAlarms} reads `meta.rows_read` on every query.\n * - `worker-memory`: workerd exposes no heap reading, so the D1 wrapper observes\n * the bytes each response returns (the reads that overran memory today), and a\n * tail consumer reports an `exceededMemory` outcome at the full limit.\n * - `sandbox-disk` and `snapshot-count`: the Sandbox SDK's box usage and snapshot list, read on a schedule.\n * - `key-rate`: the per-key request counter the operator API keeps.\n */\nimport type { AlertSink, TurnHealthAlert } from '../turn-health/sink.js'\nimport { LAUNCH_BUDGETS } from './catalog.js'\n\nexport type LimitResource = 'worker-memory' | 'd1-rows-per-query' | 'sandbox-disk' | 'snapshot-count' | 'key-rate'\n\nexport const LIMIT_RESOURCES: readonly LimitResource[] = [\n 'worker-memory',\n 'd1-rows-per-query',\n 'sandbox-disk',\n 'snapshot-count',\n 'key-rate',\n]\n\n/** The limit of each resource, in its own unit: bytes, rows, bytes, snapshots, requests per minute. */\nexport type LimitBudgets = Readonly<Record<LimitResource, number>>\n\nconst UNITS: Readonly<Record<LimitResource, string>> = {\n 'worker-memory': 'bytes',\n 'd1-rows-per-query': 'rows',\n 'sandbox-disk': 'bytes',\n 'snapshot-count': 'snapshots',\n 'key-rate': 'requests/min',\n}\n\nexport type LimitLevel = 'ok' | 'warning' | 'critical'\n\nexport interface LimitObservation {\n resource: LimitResource\n used: number\n limit: number\n /** used / limit. */\n share: number\n level: LimitLevel\n}\n\nexport interface LimitAlarms {\n readonly budgets: LimitBudgets\n /** Record one reading; a warning or critical reading is delivered before this resolves. */\n observe(resource: LimitResource, used: number, context?: { subject?: string; detail?: string }): Promise<LimitObservation>\n}\n\nexport interface LimitAlarmOptions {\n product: string\n budgets: LimitBudgets\n sink: AlertSink\n /** Default {@link LAUNCH_BUDGETS.alarmRatio}; a product cannot raise it past the limit itself. */\n ratio?: number\n now?: () => number\n}\n\nexport function createLimitAlarms(options: LimitAlarmOptions): LimitAlarms {\n const ratio = Math.min(options.ratio ?? LAUNCH_BUDGETS.alarmRatio, 1)\n const now = options.now ?? (() => Date.now())\n for (const resource of LIMIT_RESOURCES) {\n const limit = options.budgets[resource]\n if (!(typeof limit === 'number' && limit > 0 && Number.isFinite(limit))) {\n throw new Error(`limit alarms: ${resource} needs a positive limit`)\n }\n }\n return {\n budgets: options.budgets,\n async observe(resource, used, context) {\n const limit = options.budgets[resource]\n const share = used / limit\n const level: LimitLevel = share >= 1 ? 'critical' : share >= ratio ? 'warning' : 'ok'\n const observation = { resource, used, limit, share, level }\n if (level === 'ok') return observation\n const alert: TurnHealthAlert = {\n product: options.product,\n severity: level,\n // Keyed by product and resource only, so a throttled sink pages once per resource.\n key: `limit:${options.product}:${resource}`,\n title: `${options.product}: ${resource} at ${Math.round(share * 100)}% of its limit`,\n details: [\n `${formatAmount(used, resource)} of ${formatAmount(limit, resource)} ${UNITS[resource]}`,\n ...(context?.subject ? [`in ${context.subject}`] : []),\n ...(context?.detail ? [context.detail] : []),\n ],\n data: { resource, used, limit, share, ...(context?.subject ? { subject: context.subject } : {}) },\n at: now(),\n }\n await options.sink.deliver(alert)\n return observation\n },\n }\n}\n\nfunction formatAmount(value: number, resource: LimitResource): string {\n if (UNITS[resource] === 'bytes') return `${(value / (1024 * 1024)).toFixed(1)} MB`\n return Math.round(value).toLocaleString('en-US')\n}\n\n/** The D1 surface the wrapper measures; Cloudflare `D1Database` satisfies it. */\ninterface D1Measurable {\n prepare(sql: string): unknown\n}\n\n/**\n * The D1 binding with each query's rows read and response size observed.\n * Rows read come from `meta.rows_read` (the rows D1 scanned, not only those\n * returned). The response size is estimated from three sampled rows, so the\n * wrapper never serializes a whole response: that copy would double the memory\n * of the very read it watches.\n */\nexport function withD1LimitAlarms<D extends D1Measurable>(d1: D, alarms: LimitAlarms, options: { subject?: string } = {}): D {\n const observe = (sql: string, result: unknown) => {\n const meta = (result as { meta?: { rows_read?: unknown } } | null)?.meta\n const rowsRead = typeof meta?.rows_read === 'number' ? meta.rows_read : rowsOf(result)\n const pending: Promise<unknown>[] = []\n if (rowsRead !== null) pending.push(alarms.observe('d1-rows-per-query', rowsRead, { subject: options.subject, detail: sql }))\n const results = (result as { results?: unknown[] } | null)?.results ?? (Array.isArray(result) ? result : null)\n if (results && results.length > 0) {\n pending.push(alarms.observe('worker-memory', estimatedBytes(results), { subject: options.subject, detail: `one response: ${sql}` }))\n }\n // An alarm delivery failure must not fail the query that triggered it.\n return Promise.all(pending).catch(() => undefined)\n }\n const statement = (target: object, sql: string): object => new Proxy(target, {\n get(stmt, key) {\n const value = Reflect.get(stmt, key) as unknown\n if (typeof value !== 'function') return value\n if (key === 'bind') return (...params: unknown[]) => statement(value.apply(stmt, params) as object, sql)\n if (key === 'all' || key === 'raw' || key === 'run' || key === 'first') {\n return async (...args: unknown[]) => {\n const result: unknown = await value.apply(stmt, args)\n if (key !== 'first') await observe(sql, result)\n return result\n }\n }\n return value.bind(stmt)\n },\n })\n return new Proxy(d1, {\n get(binding, key) {\n const value = Reflect.get(binding, key) as unknown\n if (key === 'prepare') {\n return (sql: string) => statement((value as (sql: string) => object).call(binding, sql), sql.replace(/\\s+/g, ' ').slice(0, 160))\n }\n return typeof value === 'function' ? value.bind(binding) : value\n },\n })\n}\n\n/** The serialized size of a result set, from its first, middle and last rows. */\nexport function estimatedBytes(results: readonly unknown[]): number {\n const picks = [...new Set([0, Math.floor(results.length / 2), results.length - 1])]\n const sampled = picks.reduce((sum, index) => sum + (JSON.stringify(results[index]) ?? '').length, 0)\n return Math.round((sampled / picks.length) * results.length)\n}\n\nfunction rowsOf(result: unknown): number | null {\n if (Array.isArray(result)) return result.length\n const results = (result as { results?: unknown } | null)?.results\n return Array.isArray(results) ? results.length : null\n}\n"],"mappings":";AASO,IAAM,iBAAiB;AAAA;AAAA,EAE5B,YAAY,IAAI,OAAO;AAAA;AAAA,EAEvB,iBAAiB,KAAK,OAAO;AAAA;AAAA,EAE7B,gBAAgB,IAAI,OAAO;AAAA;AAAA,EAE3B,YAAY;AAAA;AAAA,EAEZ,gBAAgB,KAAK;AAAA;AAAA,EAErB,iBAAiB,KAAK;AAAA;AAAA,EAEtB,gBAAgB;AAAA;AAAA,EAEhB,YAAY;AACd;AAqBO,IAAM,oBAAgD;AAAA,EAC3D;AAAA,IACE,IAAI;AAAA,IACJ,OAAO;AAAA,IACP,MAAM;AAAA,IACN,UAAU;AAAA,EACZ;AAAA,EACA;AAAA,IACE,IAAI;AAAA,IACJ,OAAO;AAAA,IACP,MAAM;AAAA,IACN,UAAU;AAAA,EACZ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AAM,QAAQ,MAAM,EAAG,QAAO,OAAO;AACzC,QAAM,UAAW,QAAyC;AAC1D,SAAO,MAAM,QAAQ,OAAO,IAAI,QAAQ,SAAS;AACnD;","names":[]}
@@ -4,13 +4,13 @@ import {
4
4
  import {
5
5
  mentionKindForPath
6
6
  } from "./chunk-6E7LKHYV.js";
7
- import {
8
- persistedPartToInteraction
9
- } from "./chunk-6VQCFJWP.js";
10
7
  import {
11
8
  persistedPartToPlan,
12
9
  planToPersistedPart
13
10
  } from "./chunk-LQYIBL3G.js";
11
+ import {
12
+ persistedPartToInteraction
13
+ } from "./chunk-6VQCFJWP.js";
14
14
 
15
15
  // src/chat-store/parts.ts
16
16
  function toChatMessageParts(parts) {
@@ -135,4 +135,4 @@ export {
135
135
  buildAttachmentPromptBlock,
136
136
  historyContentWithAttachments
137
137
  };
138
- //# sourceMappingURL=chunk-2YLSWZ5W.js.map
138
+ //# sourceMappingURL=chunk-JBEFTXDO.js.map
@@ -349,6 +349,14 @@ function createD1TurnEventStore(db, options = {}) {
349
349
  db.prepare("DELETE FROM turn_status WHERE turnId = ?").bind(turnId)
350
350
  ]);
351
351
  },
352
+ async listStaleRunning(before, limit) {
353
+ const { results } = await db.prepare("SELECT turnId, scopeId, updatedAt FROM turn_status WHERE status = 'running' AND updatedAt < ? ORDER BY updatedAt ASC LIMIT ?").bind(new Date(before).toISOString(), Math.max(1, Math.floor(limit))).all();
354
+ return results.map((row) => ({ turnId: row.turnId, scopeId: row.scopeId ?? null, updatedAt: Date.parse(row.updatedAt) }));
355
+ },
356
+ async readTail(turnId, count) {
357
+ const { results } = await db.prepare("SELECT seq, event FROM turn_events WHERE turnId = ? ORDER BY seq DESC LIMIT ?").bind(turnId, Math.max(1, Math.floor(count))).all();
358
+ return results;
359
+ },
352
360
  async pruneTerminalTurns(before) {
353
361
  const cutoff = normalizeTurnCutoff(before);
354
362
  const terminalBefore = "status IN ('complete', 'error') AND updatedAt < ?";
@@ -417,6 +425,12 @@ function createMemoryTurnEventStore(options = {}) {
417
425
  const index = order.indexOf(turnId);
418
426
  if (index >= 0) order.splice(index, 1);
419
427
  },
428
+ async listStaleRunning(before, limit) {
429
+ return order.filter((turnId) => status.get(turnId) === "running" && (updatedAt.get(turnId) ?? Number.POSITIVE_INFINITY) < before).sort((left, right) => (updatedAt.get(left) ?? 0) - (updatedAt.get(right) ?? 0)).slice(0, Math.max(1, Math.floor(limit))).map((turnId) => ({ turnId, scopeId: scopes.get(turnId) ?? null, updatedAt: updatedAt.get(turnId) }));
430
+ },
431
+ async readTail(turnId, count) {
432
+ return (events.get(turnId) ?? []).slice(-Math.max(1, Math.floor(count))).reverse();
433
+ },
420
434
  async pruneTerminalTurns(before) {
421
435
  const cutoff = normalizeTurnCutoff(before).milliseconds;
422
436
  const removable = order.filter((turnId) => {
@@ -457,4 +471,4 @@ export {
457
471
  createD1TurnEventStore,
458
472
  createMemoryTurnEventStore
459
473
  };
460
- //# sourceMappingURL=chunk-JGZ7WEPL.js.map
474
+ //# sourceMappingURL=chunk-JC7YSRBR.js.map