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