|
| 1 | +import { Component, Plugin } from "@executablemd/core/api"; |
| 2 | +import { ensure } from "effection"; |
| 3 | +import { ensureDir, readTextFile, writeTextFile } from "@effectionx/fs"; |
| 4 | +import { createHash } from "node:crypto"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { join, resolve } from "node:path"; |
| 7 | +import process from "node:process"; |
| 8 | + |
| 9 | +function data(value, key) { |
| 10 | + if (typeof value !== "object" || value === null) { |
| 11 | + return undefined; |
| 12 | + } |
| 13 | + const descriptor = Object.getOwnPropertyDescriptor(value, key); |
| 14 | + return descriptor && "value" in descriptor ? descriptor.value : undefined; |
| 15 | +} |
| 16 | +function text(value) { |
| 17 | + return typeof value === "string" |
| 18 | + ? value.length <= 8192 |
| 19 | + ? value |
| 20 | + : { availability: "truncated", prefix: value.slice(0, 8192), length: value.length } |
| 21 | + : { availability: "not-own-string-data" }; |
| 22 | +} |
| 23 | +export default Plugin({ |
| 24 | + name: "fixture-failure-attribution", |
| 25 | + *install() { |
| 26 | + const candidates = []; |
| 27 | + let observed = 0; |
| 28 | + let overflow = 0; |
| 29 | + yield* ensure(function* () { |
| 30 | + const destination = process.env.XMD_TEST_ARTIFACTS; |
| 31 | + if (!observed || !destination) { |
| 32 | + return; |
| 33 | + } |
| 34 | + const directory = join(destination, "failure-attribution"); |
| 35 | + const identity = `${process.pid}-${crypto.randomUUID()}`; |
| 36 | + try { |
| 37 | + yield* ensureDir(directory); |
| 38 | + for (const candidate of candidates) { |
| 39 | + const record = candidate.record; |
| 40 | + if (typeof record.position.path !== "string") { |
| 41 | + record.source = { availability: "missing-path" }; |
| 42 | + continue; |
| 43 | + } |
| 44 | + try { |
| 45 | + const path = record.position.path.startsWith("file:") |
| 46 | + ? fileURLToPath(record.position.path) |
| 47 | + : resolve(process.cwd(), record.position.path); |
| 48 | + const source = yield* readTextFile(path); |
| 49 | + record.source = { |
| 50 | + path: record.position.path, |
| 51 | + sha256: createHash("sha256").update(source).digest("hex"), |
| 52 | + elementAtOffset: source.slice(record.position.offset).startsWith(`<${record.name}`), |
| 53 | + }; |
| 54 | + } catch { |
| 55 | + record.source = { availability: "source-unreadable" }; |
| 56 | + } |
| 57 | + } |
| 58 | + function serialized() { |
| 59 | + return JSON.stringify( |
| 60 | + { |
| 61 | + schema: "fixture-failure-attribution.v1", |
| 62 | + pid: process.pid, |
| 63 | + classification: "observed; not proof of terminal attribution", |
| 64 | + observed, |
| 65 | + overflow, |
| 66 | + limits: { candidates: 16, stringCharacters: 8192, metadataBytes: 1048576 }, |
| 67 | + candidates: candidates.map(({ record }) => record), |
| 68 | + }, |
| 69 | + undefined, |
| 70 | + 2, |
| 71 | + ); |
| 72 | + } |
| 73 | + let metadata = serialized(); |
| 74 | + while (new TextEncoder().encode(metadata).length > 1048576 && candidates.length) { |
| 75 | + const lowest = Math.min(...candidates.map((candidate) => candidate.priority)); |
| 76 | + const remove = candidates.findLastIndex((candidate) => candidate.priority === lowest); |
| 77 | + candidates.splice(remove, 1); |
| 78 | + overflow++; |
| 79 | + metadata = serialized(); |
| 80 | + } |
| 81 | + yield* writeTextFile(join(directory, identity + ".json"), metadata); |
| 82 | + } catch (error) { |
| 83 | + // Diagnostics cannot replace the command's selected outcome. |
| 84 | + try { |
| 85 | + yield* writeTextFile( |
| 86 | + join(directory, identity + "-capture-error.json"), |
| 87 | + JSON.stringify({ incomplete: true, error: text(data(error, "message")) }), |
| 88 | + ); |
| 89 | + } catch { |
| 90 | + // The unavailable destination cannot carry an auxiliary fault either. |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + yield* Component.around({ |
| 95 | + *handleFailure([failure], next) { |
| 96 | + observed++; |
| 97 | + const environment = yield* Component.operations.env; |
| 98 | + const position = failure.position; |
| 99 | + const record = { |
| 100 | + observation: observed, |
| 101 | + classification: "observed", |
| 102 | + name: text(failure.name), |
| 103 | + origin: failure.origin ?? null, |
| 104 | + position: |
| 105 | + position === undefined |
| 106 | + ? { availability: "missing" } |
| 107 | + : { |
| 108 | + path: text(data(position, "path")), |
| 109 | + offset: data(position, "offset"), |
| 110 | + line: data(position, "line"), |
| 111 | + column: data(position, "column"), |
| 112 | + }, |
| 113 | + scenario: text(data(data(environment, "values"), "scenario")), |
| 114 | + errorName: text(data(failure.error, "name")), |
| 115 | + message: text(data(failure.error, "message")), |
| 116 | + report: text(data(failure.error, "report")), |
| 117 | + detail: { |
| 118 | + actual: text(data(data(failure.error, "detail"), "actual")), |
| 119 | + expected: text(data(data(failure.error, "detail"), "expected")), |
| 120 | + }, |
| 121 | + }; |
| 122 | + const repeated = candidates.find((candidate) => candidate.error === failure.error); |
| 123 | + if (repeated) { |
| 124 | + repeated.record.repeatedCount = (repeated.record.repeatedCount ?? 0) + 1; |
| 125 | + repeated.record.lastRepeatedObservation = observed; |
| 126 | + overflow++; |
| 127 | + } else { |
| 128 | + const path = data(position, "path"); |
| 129 | + const priority = |
| 130 | + typeof path === "string" && |
| 131 | + /(?:^|\/)examples\/adversarial-implementation\/tests\/components\/Fixture\/Fallback\.md$/.test( |
| 132 | + path.replaceAll("\\", "/"), |
| 133 | + ) && |
| 134 | + /^Assert\w*$/.test(failure.name) |
| 135 | + ? 2 |
| 136 | + : 1; |
| 137 | + const candidate = { error: failure.error, priority, record }; |
| 138 | + if (candidates.length < 16) { |
| 139 | + candidates.push(candidate); |
| 140 | + } else { |
| 141 | + const replace = candidates.findIndex( |
| 142 | + (entry) => |
| 143 | + entry.priority < priority || (priority === 2 && entry.priority === priority), |
| 144 | + ); |
| 145 | + if (replace !== -1) { |
| 146 | + candidates.splice(replace, 1, candidate); |
| 147 | + } |
| 148 | + overflow++; |
| 149 | + } |
| 150 | + } |
| 151 | + return yield* next(failure); |
| 152 | + }, |
| 153 | + }); |
| 154 | + }, |
| 155 | +}); |
0 commit comments