Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

256 linesCodeBlameRaw
1// The runner's Durable Object error report, from answers shaped as
2// Cloudflare gives them, without the network.
3
4import assert from "node:assert/strict";
5import { test } from "node:test";
6
7import {
8 DO_QUERIES,
9 KEYS,
10 SERVICE_KEYS,
11 bucketsOf,
12 byStatus,
13 classify,
14 doQuery,
15 format,
16 parseArgs,
17 parseCalculations,
18 parseDoGroups,
19 parseEvents,
20 telemetryQuestions,
21 windowOf,
22} from "./runner-errors.mjs";
23
24const answer = (rows) => ({ data: { viewer: { accounts: [{ rows }] } } });
25const byNamespace = DO_QUERIES.find((query) => query.key === "by_namespace").variants[1];
26
27test("the command line has defaults and bounds", () => {
28 assert.deepEqual(parseArgs([], {}), {
29 help: false,
30 json: false,
31 keys: false,
32 days: 7,
33 samples: 10,
34 script: "g1t-runner",
35 account: "1e6f2cffa3f445920836e8ebe446bb58",
36 });
37 const options = parseArgs(["--days", "90", "--samples", "3", "--script", "other", "--json", "--keys"], { CLOUDFLARE_ACCOUNT_ID: "acct" });
38 assert.equal(options.days, 31);
39 assert.equal(options.samples, 3);
40 assert.equal(options.script, "other");
41 assert.equal(options.account, "acct");
42 assert.ok(options.json && options.keys);
43 assert.equal(parseArgs(["--days", "soon"], {}).days, 7);
44});
45
46test("the window is the last N days", () => {
47 assert.deepEqual(windowOf(new Date("2026-10-08T12:00:00Z"), 7), { start: "2026-10-01T12:00:00.000Z", end: "2026-10-08T12:00:00.000Z" });
48});
49
50test("the query is for one script, with the variant's sums and dimensions", () => {
51 const query = doQuery(byNamespace);
52 assert.match(
53 query,
54 /durableObjectsInvocationsAdaptiveGroups\(limit: 10000, filter: \{ datetime_geq: \$start, datetime_leq: \$end, scriptName: \$script \}\)/,
55 );
56 assert.match(query, /sum \{ requests errors \}/);
57 assert.match(query, /dimensions \{ namespaceId status \}/);
58 assert.match(query, /\$script: String!/);
59});
60
61test("rows are named, summed per status and sorted by requests", () => {
62 const body = answer([
63 { sum: { requests: 900, errors: 300 }, dimensions: { namespaceId: "ns1", status: "scriptThrewException" } },
64 { sum: { requests: 1000, errors: 0 }, dimensions: { namespaceId: "ns1", status: "success" } },
65 { sum: { requests: 40, errors: 20 }, dimensions: { namespaceId: "ns2", status: "internalError" } },
66 { sum: { requests: 10, errors: 0 }, dimensions: { namespaceId: "ns2", status: "success" } },
67 ]);
68 const parsed = parseDoGroups(body, byNamespace, { ns1: "g1t-runner_AttemptSandbox" });
69 assert.deepEqual(parsed.dims, ["namespace_id", "status"]);
70 assert.deepEqual(parsed.rows[0], { namespace_id: "g1t-runner_AttemptSandbox", status: "success", requests: 1000, errors: 0 });
71 assert.equal(parsed.rows.at(-1).namespace_id, "ns2");
72 assert.deepEqual(parsed.totals, { requests: 1950, errors: 320 });
73 assert.deepEqual(byStatus(parsed), [
74 { status: "success", requests: 1010, errors: 0 },
75 { status: "scriptThrewException", requests: 900, errors: 300 },
76 { status: "internalError", requests: 40, errors: 20 },
77 ]);
78});
79
80test("Cloudflare's refusal becomes the error, so the next variant is tried", () => {
81 assert.throws(() => parseDoGroups({ errors: [{ message: "unknown field wallTime" }] }, byNamespace), /unknown field wallTime/);
82 assert.throws(() => parseDoGroups({ data: {} }, byNamespace), /no rows/);
83});
84
85test("the telemetry questions filter on the script and on failures", () => {
86 const questions = telemetryQuestions({ script: "g1t-runner", from: 1, to: 2, samples: 5 });
87 assert.deepEqual(
88 questions.map((q) => q.key),
89 ["failed_invocations", "exceptions", "error_logs", "samples"],
90 );
91 const failed = questions[0].body;
92 assert.equal(failed.view, "calculations");
93 assert.deepEqual(failed.timeframe, { from: 1, to: 2 });
94 assert.deepEqual(failed.parameters.filters, [
95 { key: SERVICE_KEYS[0], operation: "eq", type: "string", value: "g1t-runner" },
96 { key: KEYS.outcome, operation: "neq", type: "string", value: "ok" },
97 ]);
98 assert.deepEqual(
99 failed.parameters.groupBys.map((g) => g.value),
100 [KEYS.entrypoint, KEYS.eventType, KEYS.outcome],
101 );
102 assert.equal(questions[3].body.view, "events");
103 assert.equal(questions[3].body.limit, 5);
104 const other = telemetryQuestions({ script: "g1t-runner", from: 1, to: 2, samples: 5, serviceKey: "$metadata.service" });
105 assert.equal(other[1].body.parameters.filters[0].key, "$metadata.service");
106});
107
108test("a calculations answer becomes rows with counts, largest first", () => {
109 const body = {
110 success: true,
111 result: {
112 calculations: [
113 {
114 aggregates: [
115 {
116 groups: [
117 { key: KEYS.entrypoint, value: "AttemptSandbox" },
118 { key: KEYS.eventType, value: "rpc" },
119 ],
120 value: 12,
121 },
122 {
123 groups: [
124 { key: KEYS.entrypoint, value: "AttemptSandbox" },
125 { key: KEYS.eventType, value: "alarm" },
126 ],
127 value: 540,
128 },
129 {
130 groups: [
131 { key: KEYS.entrypoint, value: "" },
132 { key: KEYS.eventType, value: "alarm" },
133 ],
134 count: 3,
135 },
136 ],
137 },
138 ],
139 },
140 };
141 assert.deepEqual(parseCalculations(body), [
142 { [KEYS.entrypoint]: "AttemptSandbox", [KEYS.eventType]: "alarm", count: 540 },
143 { [KEYS.entrypoint]: "AttemptSandbox", [KEYS.eventType]: "rpc", count: 12 },
144 { [KEYS.entrypoint]: "(none)", [KEYS.eventType]: "alarm", count: 3 },
145 ]);
146 assert.throws(() => parseCalculations({ success: false, errors: [{ message: "Unauthorized" }] }), /Unauthorized/);
147 assert.throws(() => parseCalculations({ success: true, result: {} }), /no calculations/);
148});
149
150test("an events answer becomes plain records, the object id cut short", () => {
151 const body = {
152 success: true,
153 result: {
154 events: {
155 events: [
156 {
157 timestamp: Date.UTC(2026, 9, 7, 3, 4, 5),
158 $workers: { entrypoint: "AttemptSandbox", eventType: "alarm", outcome: "exception", durableObjectId: "0123456789abcdef0123" },
159 $metadata: { error: "Durable Object reset because its code was updated." },
160 },
161 { $workers: { eventType: "rpc", outcome: "canceled" }, source: { message: "sandbox not started" } },
162 ],
163 },
164 },
165 };
166 assert.deepEqual(parseEvents(body), [
167 {
168 at: "2026-10-07T03:04:05.000Z",
169 entrypoint: "AttemptSandbox",
170 event_type: "alarm",
171 outcome: "exception",
172 object: "0123456789ab",
173 error: "Durable Object reset because its code was updated.",
174 message: null,
175 },
176 { at: null, entrypoint: null, event_type: "rpc", outcome: "canceled", object: null, error: null, message: "sandbox not started" },
177 ]);
178});
179
180test("messages fall into buckets, expected or not", () => {
181 assert.equal(classify("Durable Object reset because its code was updated.").bucket, "deploy_reset");
182 assert.equal(classify("Durable Object reset because its code was updated.").expected, true);
183 assert.equal(classify("sandbox stop not reported checks report_checks failed with status 500").bucket, "stop_not_reported");
184 assert.equal(classify("sandbox alarm failed abc retry 2 storage overloaded").bucket, "alarm_failed");
185 assert.equal(classify("there is no container instance that can be provided to this durable object").bucket, "no_capacity");
186 assert.equal(classify("sandbox not started agent g1t could not read this project's guardrails: down").bucket, "not_started");
187 assert.equal(classify("container exited with unexpected exit code: 137").bucket, "container_exited");
188 assert.equal(classify("Network connection lost.").bucket, "connection_lost");
189 assert.equal(classify("", "canceled").bucket, "caller_gone");
190 assert.equal(classify("something new").bucket, "other");
191 assert.equal(classify("something new").expected, false);
192});
193
194test("buckets sum the counts of their messages", () => {
195 const rows = [
196 { [KEYS.error]: "Durable Object reset because its code was updated.", count: 200 },
197 { [KEYS.error]: "Network connection lost.", count: 7 },
198 { [KEYS.error]: "Durable Object reset because its code was updated (2).", count: 50 },
199 ];
200 assert.deepEqual(
201 bucketsOf(rows, KEYS.error).map((b) => [b.bucket, b.count]),
202 [
203 ["deploy_reset", 250],
204 ["connection_lost", 7],
205 ],
206 );
207});
208
209test("the text report shows rates, statuses, buckets and what could not be read", () => {
210 const parsed = parseDoGroups(
211 answer([
212 { sum: { requests: 100, errors: 31 }, dimensions: { namespaceId: "ns1", status: "scriptThrewException" } },
213 { sum: { requests: 200, errors: 0 }, dimensions: { namespaceId: "ns1", status: "success" } },
214 ]),
215 byNamespace,
216 { ns1: "g1t-runner_AttemptSandbox" },
217 );
218 const rows = [{ [KEYS.error]: "Durable Object reset because its code was updated.", count: 31 }];
219 const text = format({
220 account: "acct",
221 script: "g1t-runner",
222 start: "2026-10-01T00:00:00.000Z",
223 end: "2026-10-08T00:00:00.000Z",
224 analytics: [
225 { key: "by_namespace", label: "By namespace", ...parsed, by_status: byStatus(parsed) },
226 { key: "by_day", label: "By day", error: "unknown field date" },
227 ],
228 telemetry: [
229 { key: "exceptions", label: "Exceptions", rows, buckets: bucketsOf(rows, KEYS.error) },
230 {
231 key: "samples",
232 label: "Samples",
233 rows: [
234 {
235 at: "2026-10-07T00:00:00.000Z",
236 entrypoint: "AttemptSandbox",
237 event_type: "alarm",
238 outcome: "exception",
239 object: "abc",
240 error: "boom",
241 message: null,
242 },
243 ],
244 },
245 ],
246 keys: null,
247 notes: ["Telemetry is filtered on $workers.scriptName."],
248 });
249 assert.match(text, /g1t-runner_AttemptSandbox\s+scriptThrewException\s+100\s+31\s+31\.0%/);
250 assert.match(text, /by status:/);
251 assert.match(text, /could not read: unknown field date/);
252 assert.match(text, /31\s+deploy_reset \(expected\)/);
253 assert.match(text, /AttemptSandbox alarm exception abc/);
254 assert.match(text, /\n {4}boom/);
255 assert.doesNotMatch(text, /Bearer|authorization/i);
256});