mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 19:51:20 -05:00
2e59f08992
* feat(gpu)!: add per-call object, work, report, metadata, structure, and parser budgets to inspect_gpu_code_objects Replace the pending GPU object and parser ceilings in GpuCodeObjectsProvider with domain-owned optional per-call parameters (max_object_bytes, max_work_bytes, max_report_bytes, max_metadata_bytes, max_structure_bytes, max_parser_entries, max_parser_visits, and max_token_bytes) reporting details.limit and adjustable_budget, remove superseded hardcoded constants, and register only the recursive container and AMDGPU MessagePack nesting caps as a fixed representation allowance. BREAKING CHANGE: inspect_gpu_code_objects adds optional per-call budget parameters and reports details.limit with details.refusal.kind "adjustable_budget" instead of "fixed_ceiling" on top-level object, work, report, metadata, structure, and parser budget refusals. * fix(gpu): clamp padding scans, classify multi-frame CCOB budget cuts, and keep max_work_bytes at 512 MiB * fix(gpu): align --max-work-bytes help text, release partial decode before refill, and admit floats under small token budgets * test(gpu): keep multi-frame CCOB v1 decoded size within cut budgets * fix(gpu): report decoder inputExhausted to refill and bound trailing Zstandard frames * fix(gpu): peek next Zstd frame, charge AMD structure reads to work budget, and keep 128-token PTX limit * fix(gpu): fix CCOB refill work double-charge, meter section reads, decouple non-GPU recognition, and align frame/recovery bounds * refactor(gpu): drop redundant lookahead on non-refill path and pin refill/readBounded regressions * fix(gpu): keep standalone PTX peak heap at 1x, use explicit scan reads, forward CUDA cancel signal, and report max_report_bytes required_value * fix(gpu): report lower-bound report bytes in reported_limits and enforce max_token_bytes on recognized PTX headers
188 lines
5.8 KiB
TypeScript
188 lines
5.8 KiB
TypeScript
import { nodeCodecResult } from "../kernel/nodeCodecResult.js";
|
|
import { inflate } from "node:zlib";
|
|
import { scheduler } from "node:timers/promises";
|
|
import { promisify } from "node:util";
|
|
import { assertArtifactReadActive } from "../resources/index.js";
|
|
import { decodeLz4Block } from "../compression/Lz4Block.js";
|
|
import {
|
|
decodeZstandardFramesCooperative,
|
|
isZstandardTruncationReason,
|
|
ZSTANDARD_WINDOW_LOG_MAX,
|
|
} from "../domain/compression/zstandardExactFrame.js";
|
|
import {
|
|
ZstandardFrameAllowanceError,
|
|
type ZstandardFrameAllowance,
|
|
} from "../domain/compression/zstandardFrameAllowance.js";
|
|
import {
|
|
BinaryFormatFailure,
|
|
BinaryResourceFailure,
|
|
} from "../domain/binaryContainer.js";
|
|
import {
|
|
DEFAULT_GPU_OBJECT_BYTES,
|
|
GPU_OBJECT_BYTES_LIMIT,
|
|
} from "../domain/gpu/gpuCodeObjectsBudgets.js";
|
|
import { rejectGpu } from "./GpuSource.js";
|
|
const unzip = promisify(inflate);
|
|
|
|
/** Codec-owned marker for a stream that ran out of input bytes rather than corrupting or overflowing. */
|
|
class GpuTruncatedPayloadFailure extends BinaryFormatFailure {
|
|
constructor(
|
|
message: string,
|
|
context?: {
|
|
readonly format?: string;
|
|
readonly field?: string;
|
|
readonly offset?: number;
|
|
},
|
|
) {
|
|
super("malformed", message, context);
|
|
}
|
|
}
|
|
|
|
/** True when a GPU codec failure indicates truncated compressed input rather than corrupt bytes or output overflow. */
|
|
export const isTruncatedGpuPayloadFailure = (
|
|
error: unknown,
|
|
): error is BinaryFormatFailure => error instanceof GpuTruncatedPayloadFailure;
|
|
/** Completed codec bytes, their encoded extent, and whether input ended where another frame could still continue. */
|
|
export const decodeGpuPayloadStream = async (
|
|
bytes: Buffer,
|
|
compression: "zlib" | "zstd",
|
|
maximumDecodedSize: number,
|
|
signal?: AbortSignal,
|
|
): Promise<{
|
|
buffer: Buffer;
|
|
consumedBytes: number;
|
|
inputExhausted: boolean;
|
|
frameAllowanceReached?: ZstandardFrameAllowance;
|
|
}> => {
|
|
assertArtifactReadActive(signal);
|
|
const maxOutputLength = Math.max(1, maximumDecodedSize);
|
|
if (compression === "zstd") {
|
|
const decoded = await decodeZstandardFramesCooperative(
|
|
bytes,
|
|
{
|
|
maxOutputLength,
|
|
windowLogMax: ZSTANDARD_WINDOW_LOG_MAX,
|
|
mode: "prefix",
|
|
},
|
|
async () => {
|
|
await scheduler.yield();
|
|
assertArtifactReadActive(signal);
|
|
},
|
|
);
|
|
assertArtifactReadActive(signal);
|
|
if (!decoded.ok) {
|
|
if (
|
|
decoded.reason === "invalid_allowance" &&
|
|
decoded.configuration !== undefined
|
|
)
|
|
throw decoded.configuration;
|
|
if (decoded.reason === "work_limit")
|
|
throw new ZstandardFrameAllowanceError(
|
|
{
|
|
maxFrames: decoded.value ?? 0,
|
|
source: decoded.source ?? "default",
|
|
},
|
|
decoded.offset,
|
|
);
|
|
if (isZstandardTruncationReason(decoded.reason))
|
|
throw new GpuTruncatedPayloadFailure(
|
|
"GPU payload decompression failed or exceeded its declared length",
|
|
{
|
|
format: "zstd",
|
|
field: decoded.reason,
|
|
offset: decoded.offset,
|
|
},
|
|
);
|
|
throw new BinaryFormatFailure(
|
|
"malformed",
|
|
"GPU payload decompression failed or exceeded its declared length",
|
|
{
|
|
format: "zstd",
|
|
field: decoded.reason,
|
|
offset: decoded.offset,
|
|
},
|
|
);
|
|
}
|
|
return {
|
|
buffer: decoded.bytes,
|
|
consumedBytes: decoded.consumedBytes,
|
|
inputExhausted: decoded.inputExhausted,
|
|
...(decoded.frameAllowanceReached === undefined
|
|
? {}
|
|
: { frameAllowanceReached: decoded.frameAllowanceReached }),
|
|
};
|
|
}
|
|
let result: unknown;
|
|
try {
|
|
result = await unzip(bytes, { maxOutputLength, info: true });
|
|
} catch (cause: unknown) {
|
|
assertArtifactReadActive(signal);
|
|
const code =
|
|
cause instanceof Error && "code" in cause
|
|
? String(cause.code)
|
|
: undefined;
|
|
if (code === "Z_BUF_ERROR")
|
|
throw new GpuTruncatedPayloadFailure(
|
|
"GPU payload decompression failed or exceeded its declared length",
|
|
{ format: "zlib", field: code },
|
|
);
|
|
return rejectGpu(
|
|
"GPU payload decompression failed or exceeded its declared length",
|
|
);
|
|
}
|
|
assertArtifactReadActive(signal);
|
|
const { buffer: decoded, consumedBytes: consumed } = nodeCodecResult(result);
|
|
if (
|
|
!Number.isSafeInteger(consumed) ||
|
|
consumed <= 0 ||
|
|
consumed > bytes.length
|
|
)
|
|
rejectGpu("GPU decoder returned an invalid complete codec extent");
|
|
return { buffer: decoded, consumedBytes: consumed, inputExhausted: false };
|
|
};
|
|
export const decodeGpuPayload = async (
|
|
bytes: Buffer,
|
|
compression: "zlib" | "zstd" | "lz4",
|
|
expected: number,
|
|
signal?: AbortSignal,
|
|
maxObjectBytes: number = DEFAULT_GPU_OBJECT_BYTES,
|
|
): Promise<Buffer> => {
|
|
assertArtifactReadActive(signal);
|
|
if (expected > maxObjectBytes)
|
|
throw new BinaryResourceFailure(
|
|
"structure",
|
|
maxObjectBytes,
|
|
`GPU payload decoded length exceeds the ${String(maxObjectBytes)}-byte object budget`,
|
|
GPU_OBJECT_BYTES_LIMIT,
|
|
"bytes",
|
|
);
|
|
if (compression === "lz4") {
|
|
try {
|
|
return await decodeLz4Block(bytes, expected, maxObjectBytes, signal);
|
|
} catch (cause) {
|
|
if (cause instanceof BinaryResourceFailure)
|
|
throw new BinaryResourceFailure(
|
|
"structure",
|
|
maxObjectBytes,
|
|
cause.message,
|
|
GPU_OBJECT_BYTES_LIMIT,
|
|
"bytes",
|
|
);
|
|
throw cause;
|
|
}
|
|
}
|
|
const decoded = await decodeGpuPayloadStream(
|
|
bytes,
|
|
compression,
|
|
expected,
|
|
signal,
|
|
);
|
|
if (decoded.consumedBytes !== bytes.length)
|
|
rejectGpu(
|
|
"GPU compressed payload contains bytes outside its complete codec stream",
|
|
);
|
|
if (decoded.buffer.length !== expected)
|
|
rejectGpu("GPU decompressed length disagrees with its declaration");
|
|
return decoded.buffer;
|
|
};
|