|
| 1 | +import type { ExecutionResult } from "./runtime-contracts.js" |
| 2 | +import { normalizeJsonValue } from "./object-utils.js" |
| 3 | + |
| 4 | +export const COMMAND_ARTIFACT_STRING_MAX_BYTES = 1024 * 1024 |
| 5 | +export const COMMAND_ARTIFACT_TOTAL_STRING_MAX_BYTES = 16 * 1024 * 1024 |
| 6 | +export const COMMAND_ARTIFACT_MAX_NODES = 25_000 |
| 7 | +export const COMMAND_ARTIFACT_MAX_RECORDS = 1_000 |
| 8 | +const COMMAND_ARTIFACT_MAX_TRUNCATIONS = 1_000 |
| 9 | +const COMMAND_ARTIFACT_KEY_MAX_BYTES = 256 |
| 10 | +const COMMAND_ARTIFACT_IDENTITY_MAX_BYTES = 1024 |
| 11 | +const COMMAND_ARTIFACT_PATH_MAX_BYTES = 4096 |
| 12 | +const EXECUTION_RESULT_KEYS = new Set(["id", "command", "args", "exitCode", "stdout", "stderr", "result", "diagnostics", "artifactRefs", "startedAt", "finishedAt", "artifactCapture"]) |
| 13 | + |
| 14 | +export interface CommandArtifactTruncation { |
| 15 | + path: string |
| 16 | + reason: "string-byte-limit" | "total-string-byte-limit" | "node-limit" |
| 17 | + observedBytes?: number |
| 18 | + capturedBytes?: number |
| 19 | + configuredLimitBytes?: number |
| 20 | +} |
| 21 | + |
| 22 | +export interface CommandArtifactCapture { |
| 23 | + schema: "wp-codebox/command-artifact-capture/v1" |
| 24 | + truncated: true |
| 25 | + limits: { |
| 26 | + capturedStringBytesPerValue: number |
| 27 | + capturedStringBytesTotal: number |
| 28 | + nodes: number |
| 29 | + records: number |
| 30 | + } |
| 31 | + fields: CommandArtifactTruncation[] |
| 32 | + omittedFieldCount?: number |
| 33 | + omittedCommandCount?: number |
| 34 | +} |
| 35 | + |
| 36 | +export type BoundedExecutionResult<T extends ExecutionResult = ExecutionResult> = T & { artifactCapture?: CommandArtifactCapture } |
| 37 | + |
| 38 | +export function boundedExecutionResultsForArtifacts<T extends ExecutionResult>(commands: T[]): Array<BoundedExecutionResult<T>> { |
| 39 | + const budget = { |
| 40 | + remainingBytes: COMMAND_ARTIFACT_TOTAL_STRING_MAX_BYTES, |
| 41 | + remainingNodes: COMMAND_ARTIFACT_MAX_NODES, |
| 42 | + remainingTruncations: COMMAND_ARTIFACT_MAX_TRUNCATIONS, |
| 43 | + } |
| 44 | + const selectedCommands = commands.length <= COMMAND_ARTIFACT_MAX_RECORDS |
| 45 | + ? commands |
| 46 | + : [...commands.slice(0, COMMAND_ARTIFACT_MAX_RECORDS - 1), commands.at(-1)!] |
| 47 | + |
| 48 | + return selectedCommands.map((command, selectedIndex) => { |
| 49 | + const fields: CommandArtifactTruncation[] = [] |
| 50 | + let omittedFieldCount = 0 |
| 51 | + const capture = (value: unknown, path: string): unknown => boundedArtifactValue(value, path, budget, (field) => { |
| 52 | + if (fields.length < 100 && budget.remainingTruncations > 0) { |
| 53 | + fields.push(field) |
| 54 | + budget.remainingTruncations -= 1 |
| 55 | + } else { |
| 56 | + omittedFieldCount += 1 |
| 57 | + } |
| 58 | + }) |
| 59 | + const extensions = Object.fromEntries( |
| 60 | + Object.entries(command) |
| 61 | + .filter(([key]) => !EXECUTION_RESULT_KEYS.has(key)) |
| 62 | + .map(([key, value]) => [key, capture(normalizeJsonValue(value), key)]), |
| 63 | + ) |
| 64 | + const projected = { |
| 65 | + ...extensions, |
| 66 | + id: boundedIdentityString(command.id), |
| 67 | + command: boundedIdentityString(command.command), |
| 68 | + args: capture(normalizeJsonValue(command.args), "args") as string[], |
| 69 | + exitCode: command.exitCode, |
| 70 | + stdout: capture(command.stdout, "stdout") as string, |
| 71 | + stderr: capture(command.stderr, "stderr") as string, |
| 72 | + ...(command.result === undefined ? {} : { result: capture(normalizeJsonValue(command.result), "result") as ExecutionResult["result"] }), |
| 73 | + ...(command.diagnostics === undefined ? {} : { diagnostics: capture(normalizeJsonValue(command.diagnostics), "diagnostics") }), |
| 74 | + ...(command.artifactRefs === undefined ? {} : { artifactRefs: capture(normalizeJsonValue(command.artifactRefs), "artifactRefs") as ExecutionResult["artifactRefs"] }), |
| 75 | + startedAt: boundedIdentityString(command.startedAt), |
| 76 | + finishedAt: boundedIdentityString(command.finishedAt), |
| 77 | + } as BoundedExecutionResult<T> |
| 78 | + const omittedCommandCount = commands.length - selectedCommands.length |
| 79 | + if (fields.length > 0 || omittedFieldCount > 0 || (omittedCommandCount > 0 && selectedIndex === selectedCommands.length - 1)) { |
| 80 | + projected.artifactCapture = { |
| 81 | + schema: "wp-codebox/command-artifact-capture/v1", |
| 82 | + truncated: true, |
| 83 | + limits: { |
| 84 | + capturedStringBytesPerValue: COMMAND_ARTIFACT_STRING_MAX_BYTES, |
| 85 | + capturedStringBytesTotal: COMMAND_ARTIFACT_TOTAL_STRING_MAX_BYTES, |
| 86 | + nodes: COMMAND_ARTIFACT_MAX_NODES, |
| 87 | + records: COMMAND_ARTIFACT_MAX_RECORDS, |
| 88 | + }, |
| 89 | + fields, |
| 90 | + ...(omittedFieldCount > 0 ? { omittedFieldCount } : {}), |
| 91 | + ...(omittedCommandCount > 0 && selectedIndex === selectedCommands.length - 1 ? { omittedCommandCount } : {}), |
| 92 | + } |
| 93 | + } |
| 94 | + return projected |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +function boundedArtifactValue( |
| 99 | + value: unknown, |
| 100 | + path: string, |
| 101 | + budget: { remainingBytes: number; remainingNodes: number; remainingTruncations: number }, |
| 102 | + recordTruncation: (field: CommandArtifactTruncation) => void, |
| 103 | +): unknown { |
| 104 | + if (value === null || value === undefined || (typeof value !== "string" && typeof value !== "object")) { |
| 105 | + return value |
| 106 | + } |
| 107 | + if (budget.remainingNodes <= 0) { |
| 108 | + recordTruncation({ path: truncateUtf8(path, COMMAND_ARTIFACT_PATH_MAX_BYTES), reason: "node-limit" }) |
| 109 | + if (typeof value === "string") return "" |
| 110 | + return Array.isArray(value) ? [] : {} |
| 111 | + } |
| 112 | + budget.remainingNodes -= 1 |
| 113 | + if (typeof value === "string") { |
| 114 | + const observedBytes = Buffer.byteLength(value, "utf8") |
| 115 | + const totalBudgetLimited = budget.remainingBytes < Math.min(observedBytes, COMMAND_ARTIFACT_STRING_MAX_BYTES) |
| 116 | + const configuredLimitBytes = Math.min(COMMAND_ARTIFACT_STRING_MAX_BYTES, budget.remainingBytes) |
| 117 | + const captured = truncateUtf8(value, configuredLimitBytes) |
| 118 | + const capturedBytes = Buffer.byteLength(captured, "utf8") |
| 119 | + budget.remainingBytes -= capturedBytes |
| 120 | + if (capturedBytes < observedBytes) { |
| 121 | + recordTruncation({ |
| 122 | + path: truncateUtf8(path, COMMAND_ARTIFACT_PATH_MAX_BYTES), |
| 123 | + reason: totalBudgetLimited ? "total-string-byte-limit" : "string-byte-limit", |
| 124 | + observedBytes, |
| 125 | + capturedBytes, |
| 126 | + configuredLimitBytes, |
| 127 | + }) |
| 128 | + } |
| 129 | + return captured |
| 130 | + } |
| 131 | + if (Array.isArray(value)) { |
| 132 | + return value.map((item, index) => boundedArtifactValue(item, `${path}[${index}]`, budget, recordTruncation)) |
| 133 | + } |
| 134 | + if (value && typeof value === "object") { |
| 135 | + return Object.fromEntries(Object.entries(value).map(([key, item]) => { |
| 136 | + const capturedKey = truncateUtf8(key, COMMAND_ARTIFACT_KEY_MAX_BYTES) |
| 137 | + return [capturedKey, boundedArtifactValue(item, `${path}.${capturedKey}`, budget, recordTruncation)] |
| 138 | + })) |
| 139 | + } |
| 140 | + return value |
| 141 | +} |
| 142 | + |
| 143 | +function boundedIdentityString(value: string): string { |
| 144 | + return truncateUtf8(value, COMMAND_ARTIFACT_IDENTITY_MAX_BYTES) |
| 145 | +} |
| 146 | + |
| 147 | +export function truncateUtf8(value: string, maxBytes: number): string { |
| 148 | + if (maxBytes <= 0) return "" |
| 149 | + if (Buffer.byteLength(value, "utf8") <= maxBytes) return value |
| 150 | + |
| 151 | + let low = 0 |
| 152 | + let high = Math.min(value.length, maxBytes) |
| 153 | + while (low < high) { |
| 154 | + const middle = Math.ceil((low + high) / 2) |
| 155 | + if (Buffer.byteLength(value.slice(0, middle), "utf8") <= maxBytes) low = middle |
| 156 | + else high = middle - 1 |
| 157 | + } |
| 158 | + const captured = value.slice(0, low) |
| 159 | + return /[\uD800-\uDBFF]$/.test(captured) ? captured.slice(0, -1) : captured |
| 160 | +} |
0 commit comments