Files
reverse-engineering-anything/tests/boundary/application/evtxArchiveService.test.ts
T
morluto 44fbcfc787 feat(evtx)!: page event records with offset and report page facts (#15636)
* feat(evtx): page event records with offset and report page facts

inspect_evtx_archive stopped walking at max_records, so records past the first 20,000 (the newest events of a default-size Application log) could not be listed. The walk now covers every record. max_records stays a 20,000-record page bound, which limits retained Evidence. A new zero-based offset selects the first record listed. The result reports page { offset, limit, returned, total, next_offset }, and the record-limit-reached diagnostic names offset=next_offset. Coverage now describes parse loss only, so a page that leaves records out by selection is no longer partial, and chunk ending facts and endpoint checks cover records past the page.

Fixes #15612

* feat(evtx)!: qualify the file-tail offset for small and wrapped logs

The suggested tail offset is now the larger of 0 and page.total minus max_records, so a log smaller than one page gets a valid offset. File order is oldest first only in a log that has not wrapped; the guide and descriptions now say how chunks[] locates the newest records in a wrapped log.

BREAKING CHANGE: inspect_evtx_archive results now require page. Evidence saved before this change has no page, and inspect_analysis_view refuses to summarise it. Re-run inspect_evtx_archive on the log to produce current Evidence.
2026-12-01 17:15:02 -05:00

239 lines
9.0 KiB
TypeScript

import { open, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, onTestFinished } from "vitest";
import { evtxLog } from "../../fixtures/evtxLog.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
import {
AnalysisInputError,
AnalysisResourceConstraintError,
} from "../../../src/domain/analysisErrorCore.js";
import { analysisErrorRefusal } from "../../../src/domain/analysisErrorRefusal.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { EVTX_ARCHIVE_BUDGETS } from "../../../src/domain/evtxArchive/evtxArchiveQuery.js";
import { evtxArchiveSchema } from "../../../src/domain/evtxArchive/evtxArchive.js";
import { inspectEvtxArchiveEvidence } from "../../../src/application/evtxArchive/EvtxArchiveService.js";
it("reports the selected path and cause when it cannot resolve the event log", async () => {
const root = await createTestTempDirectory("rea-evtx-missing-");
const blocker = join(root, "file");
await writeFile(blocker, "x");
const resources = new ArtifactResourceScope();
onTestFinished(() => resources.close());
for (const [path, code] of [
[join(root, "missing.evtx"), "ENOENT"],
[join(blocker, "child.evtx"), "ENOTDIR"],
] as const) {
const result = await inspectEvtxArchiveEvidence({ path }, resources);
expect(result.ok).toBe(false);
if (result.ok || !(result.error instanceof AnalysisInputError))
throw new Error("Expected input refusal");
// The path is well formed; the host refused it as a regular file, so the
// refusal names its own next step instead of the input schema.
expect(result.error.conformsToInputContract).toBe(true);
expect(projectAnalysisError(result.error)).toMatchObject({
code: "invalid_request",
remediation: {
action:
"Select an existing, readable regular file as path, then retry.",
},
});
expect(projectAnalysisError(result.error)).not.toHaveProperty("recovery");
expect(result.error.issues).toEqual([
{
path: ["path"],
reason: "invalid_value",
message: expect.stringContaining(path),
},
]);
expect(result.error.issues[0]?.message).toContain(code);
}
});
it("names max_file_bytes as adjustable when the log exceeds the caller budget", async () => {
const root = await createTestTempDirectory("rea-evtx-budget-");
const path = join(root, "events.evtx");
await writeFile(path, "ElfFile\0");
const resources = new ArtifactResourceScope();
onTestFinished(() => resources.close());
const result = await inspectEvtxArchiveEvidence(
{ path, max_file_bytes: 4 },
resources,
);
expect(result.ok).toBe(false);
if (result.ok || !(result.error instanceof AnalysisResourceConstraintError))
throw new Error("Expected a resource-constraint refusal");
// Recovery is derived by the shared adjustable-budget formatter from the
// typed limit, so it names the per-call control rather than a bolt-on
// sentence, and the CLI adapter can spell it as --max-file-bytes.
expect(projectAnalysisError(result.error).remediation.action).toContain(
"max_file_bytes",
);
});
it("binds the refused max_file_bytes as an adjustable budget that names the needed size", async () => {
const root = await createTestTempDirectory("rea-evtx-budget-limit-");
const path = join(root, "events.evtx");
await writeFile(path, "ElfFile\0");
const resources = new ArtifactResourceScope();
onTestFinished(() => resources.close());
const result = await inspectEvtxArchiveEvidence(
{ path, max_file_bytes: 4 },
resources,
);
if (result.ok || !(result.error instanceof AnalysisResourceConstraintError))
throw new Error("Expected a resource-constraint refusal");
// The public refusal must be an adjustable budget, not a bolt-on sentence:
// without a bound parameter limit the caller learns neither the control nor
// the value that would admit the log.
expect(analysisErrorRefusal(result.error)).toEqual({
kind: "adjustable_budget",
operation: "inspect_evtx_archive",
budget: {
control_kind: "parameter",
parameter: "max_file_bytes",
effective_value: 4,
unit: "bytes",
minimum: 1,
maximum: EVTX_ARCHIVE_BUDGETS.max_file_bytes.maximum,
default_value: EVTX_ARCHIVE_BUDGETS.max_file_bytes.default_value,
applies_at: "next_call",
required_value: 8,
},
});
});
it("keeps budget fields the caller omitted out of Evidence parameters", async () => {
const root = await createTestTempDirectory("rea-evtx-evidence-parameters-");
const path = join(root, "events.evtx");
await writeFile(path, "ElfFile\0");
const resources = new ArtifactResourceScope();
onTestFinished(() => resources.close());
const result = await inspectEvtxArchiveEvidence({ path }, resources);
if (!result.ok) throw new Error(result.error.message);
// A caller that selected only the path must not read back REA's resolved
// budgets as their own selection.
expect(result.value.evidence.parameters).toEqual({ path });
});
it("reports a fixed ceiling when a complete log cannot fit any valid file budget", async () => {
const root = await createTestTempDirectory("rea-evtx-ceiling-");
const path = join(root, "large.evtx");
const file = await open(path, "w");
try {
await file.truncate(EVTX_ARCHIVE_BUDGETS.max_file_bytes.maximum + 1);
} finally {
await file.close();
}
const resources = new ArtifactResourceScope();
onTestFinished(() => resources.close());
const result = await inspectEvtxArchiveEvidence({ path }, resources);
if (result.ok) throw new Error("Expected size refusal");
expect(analysisErrorRefusal(result.error)).toMatchObject({
kind: "fixed_ceiling",
ceiling: {
kind: "input",
limit: EVTX_ARCHIVE_BUDGETS.max_file_bytes.maximum,
},
});
});
const inventory = async (bytes: Buffer, input: object = {}) => {
const root = await createTestTempDirectory("rea-evtx-inventory-");
const path = join(root, "exported.evtx");
await writeFile(path, bytes);
const resources = new ArtifactResourceScope();
onTestFinished(() => resources.close());
const result = await inspectEvtxArchiveEvidence(
{ path, ...input },
resources,
);
if (!result.ok) throw result.error;
return {
archive: evtxArchiveSchema.parse(result.value.evidence.normalized_result),
parameters: result.value.evidence.parameters,
path,
};
};
it("treats all-zero slots after the declared chunk count as unused space", async () => {
const { archive } = await inventory(
evtxLog(1, [Buffer.alloc(65_536), Buffer.alloc(65_536)]),
);
expect(archive.file_version).toMatchObject({
chunk_count: 1,
physical_chunk_count: 3,
});
expect(archive.summary).toEqual({ chunks: 1, events: 1, diagnostics: 0 });
expect(archive.source_coverage.status).toBe("complete-within-scope");
});
it("keeps a nonzero slot after the declared chunk count visible", async () => {
// Bytes past the declared chunks may hold recoverable records, so only an
// all-zero slot is unused space.
const dirty = Buffer.alloc(65_536);
dirty[65_535] = 1;
const { archive } = await inventory(
evtxLog(1, [Buffer.alloc(65_536), dirty]),
);
expect(
archive.diagnostics.map(({ code, chunk_number }) => [code, chunk_number]),
).toEqual([
["chunk-count-mismatch", null],
["invalid-chunk", 2],
]);
expect(archive.source_coverage.status).toBe("partial");
});
it("reaches the newest records of a log larger than one page through total and offset", async () => {
const log = evtxLog(25_000);
const first = await inventory(log);
// The default page is the 20,000-record ceiling; the rest is counted, not lost.
expect(first.archive.page).toEqual({
offset: 0,
limit: 20_000,
returned: 20_000,
total: 25_000,
next_offset: 20_000,
});
expect(first.archive.source_coverage.status).toBe("complete-within-scope");
expect(
first.archive.diagnostics.map(({ code, message }) => ({ code, message })),
).toEqual([
{
code: "record-limit-reached",
message:
"Listed 20000 of 25000 records at max_records=20000; repeat with offset=20000 for the next records.",
},
]);
const newest = await inventory(log, { offset: 25_000 - 10, max_records: 10 });
expect(
newest.archive.events.map((event) => [
event.ordinal,
event.event_record_id,
]),
).toEqual(
Array.from({ length: 10 }, (_, index) => [
24_990 + index,
String(24_991 + index),
]),
);
expect(newest.archive.page).toEqual({
offset: 24_990,
limit: 10,
returned: 10,
total: 25_000,
next_offset: null,
});
expect(newest.archive.diagnostics).toEqual([]);
expect(newest.archive.source_coverage.status).toBe("complete-within-scope");
// The selected window is part of the request, so each page is its own Evidence.
expect(newest.parameters).toEqual({
path: newest.path,
offset: 24_990,
max_records: 10,
});
});