Files
reverse-engineering-anything/src/native/elf/KernelBuildConfigProvider.ts
T
morluto b138f52e12 refactor: prune internal seams and preserve configuration ownership (#10430)
* refactor: prune internal seams and preserve configuration ownership

* fix(setup): keep YAML aliases and inherited registration settings on refresh

- Reuse an unchanged YAML collection only while every alias in it still
  resolves after the edit; otherwise rebuild it so the alias becomes its
  value. A sibling anchor replaced or moved in the same refresh no longer
  leaves an unresolved alias in Hermes or Goose output.
- Keep a connection selector whose existing value already chooses stdio
  (for example an inherited Hermes transport: stdio) instead of superseding
  it; remote selectors are still replaced. Explicit stdio selectors are
  therefore no longer rewritten (Kimi expectation updated).
- When the whole Hermes registration is inherited through a merge,
  override it with that merge and write only the changed fields.

* fix(setup): edit registration fields whose YAML key is an alias

A key written as an alias to a string scalar (for example *field with
field: &field env) was treated as a source-only key, so any change to
its value was refused in Hermes and Goose. Resolve such keys to their
string, keep the aliased spelling while its anchor survives the edit,
and write the plain key when the same edit rebuilds the anchor.

* test(process): bound cleanup-boundary waits and always release gates

Both cancellation tests awaited a bare started promise and released
their gates only on success, so a missed boundary waited for the outer
deadline and could leave cleanup parked. Use awaitAcknowledgement and
release and join the cleanup in finally.

* fix(setup): repair only aliases an edit leaves unresolved

Replace the pre-edit alias survival guess, which expanded aliases to
sibling anchors that survive unchanged, with an exact repair: record
what each alias in the replaced value resolves to, write the edit, then
replace only aliases left without a preceding anchor by that value.

- Keep an aliased key's source position, so its anchor stays ahead of
  later aliases.
- Treat an aliased key as its string only when no other key in the map
  resolves to the same string; an ambiguous one stays guarded.
- Override an inherited Hermes registration only when the requested
  entry keeps every inherited field, so an explicit replacement can drop
  remote selectors.

* fix(setup): repair aliases whose binding an edit changes, keeping comments

An alias whose own anchor was removed could resolve to an earlier anchor
with the same name and silently change value. Record the node each alias
resolved to and repair any alias that no longer resolves to that node.
Carry the alias's comments and spacing onto the repaired value.

* test(setup): split the Hermes anchor and merge refresh cases

* fix(setup): refuse removing an overridden key its merge would restore

The merge-retention check skipped keys the mapping also overrides, so
dropping such a key (for example a remote url during an explicit stdio
replacement) let the retained merge restore it. Check removal before
skipping overridden keys, so the edit is refused with the existing
message instead of producing a registration that cannot be applied.
2026-11-14 04:08:37 -05:00

212 lines
6.5 KiB
TypeScript

import {
projectOwnedArtifactIdentityFailure,
projectSelectedArtifactFileFailure,
projectStableArtifactFailure,
stableArtifactInputFailure,
projectArtifactCloseFailure,
} from "../../artifacts/StableArtifactFailure.js";
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
import {
artifactFilePrimaryFacts,
type ArtifactFilePrimaryFacts,
} from "../../artifacts/ArtifactFileOperation.js";
import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
import type { KernelBuildConfigPort } from "../../application/binaryDiagnostics/KernelBuildConfigPort.js";
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
import { ArtifactOperationError } from "../../domain/artifactOperationError.js";
import {
AnalysisInputError,
AnalysisResourceConstraintError,
} from "../../domain/analysisErrorCore.js";
import {
MAX_INSPECT_KERNEL_BUILD_CONFIG_CONFIG_BYTES,
MAX_INSPECT_KERNEL_BUILD_CONFIG_ENTRIES,
MAX_INSPECT_KERNEL_BUILD_CONFIG_FILE_BYTES,
MAX_INSPECT_KERNEL_BUILD_CONFIG_PAYLOAD_BYTES,
type InspectKernelBuildConfigInput,
} from "../../domain/native/kernelBuildConfig.js";
import { err, ok } from "../../domain/result.js";
import { KERNEL_BUILD_CONFIG_PROVIDER } from "../../domain/investigationProviders.js";
import {
KernelBuildConfigBudgetFailure,
parseKernelBuildConfig,
} from "./KernelBuildConfig.js";
const OPERATION = "inspect_kernel_build_config";
/** Portable parser over one bounded, identity-checked regular file. */
export class KernelBuildConfigProvider implements KernelBuildConfigPort {
readonly identity = KERNEL_BUILD_CONFIG_PROVIDER;
readonly #resources = new ArtifactResourceScope();
async close(): Promise<void> {
try {
await this.#resources.close();
} catch (cause: unknown) {
throw projectArtifactCloseFailure(
cause,
KERNEL_BUILD_CONFIG_PROVIDER.id,
OPERATION,
);
}
}
async inspect(
input: InspectKernelBuildConfigInput,
options?: ExecutionOptions,
) {
try {
return await this.#resources.run(async () => {
const snapshot = await readStableArtifact(
input.path,
input.max_file_bytes,
options?.signal,
undefined,
this.#resources,
);
return ok(
await parseKernelBuildConfig(
snapshot.bytes,
{
path: input.path,
sha256: snapshot.sha256,
bytes: snapshot.bytes.length,
},
input,
options?.signal,
),
);
});
} catch (cause) {
return err(kernelBuildConfigFailure(cause, input));
}
}
}
const ceiling = (
knob: string,
maximum: number,
selected: number,
description: string,
): string =>
selected >= maximum
? `The image exceeds the ${String(maximum)} ${knob} ceiling this operation accepts; select a smaller artifact.`
: `Raise ${knob} above ${String(selected)} or select a smaller artifact; the limit is the caller-selected ${description}.`;
const inputBudgetRemediation = (
boundary: "file-size" | "capacity",
input: InspectKernelBuildConfigInput,
cause: ArtifactReaderFailure,
): string => {
if (boundary === "file-size")
return input.max_file_bytes >= MAX_INSPECT_KERNEL_BUILD_CONFIG_FILE_BYTES
? `The image exceeds the ${String(MAX_INSPECT_KERNEL_BUILD_CONFIG_FILE_BYTES)}-byte input ceiling this operation accepts; select a smaller artifact.`
: `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.`;
const knob =
cause instanceof KernelBuildConfigBudgetFailure ? cause.knob : null;
if (knob === "max_entries")
return ceiling(
knob,
MAX_INSPECT_KERNEL_BUILD_CONFIG_ENTRIES,
input.max_entries,
"CONFIG_* entry budget",
);
if (knob === "max_config_bytes")
return ceiling(
knob,
MAX_INSPECT_KERNEL_BUILD_CONFIG_CONFIG_BYTES,
input.max_config_bytes,
"inflated IKCFG gzip budget",
);
if (knob === "max_payload_bytes")
return ceiling(
knob,
MAX_INSPECT_KERNEL_BUILD_CONFIG_PAYLOAD_BYTES,
input.max_payload_bytes,
"inflated bzImage payload budget",
);
return "Select a smaller artifact. The caller-selected kernel build config budgets refused this image.";
};
const kernelBuildConfigFailurePrimary = (
facts: ArtifactFilePrimaryFacts,
input: InspectKernelBuildConfigInput,
) => {
const { cause, origin } = facts;
const identityFailure = projectOwnedArtifactIdentityFailure(
facts,
OPERATION,
input.path,
);
if (identityFailure !== undefined) return identityFailure;
if (
origin === "owned-or-classified" &&
cause instanceof ArtifactReaderFailure
) {
if (cause.reason === "limit") {
return new AnalysisResourceConstraintError(
OPERATION,
cause.boundary ?? "memory",
cause.message,
{
max_file_bytes: input.max_file_bytes,
max_entries: input.max_entries,
max_config_bytes: input.max_config_bytes,
max_payload_bytes: input.max_payload_bytes,
},
{
cause,
...(cause.boundary !== "capacity" && cause.boundary !== "file-size"
? {}
: {
retryable: false as const,
remediationAction: inputBudgetRemediation(
cause.boundary,
input,
cause,
),
}),
},
);
}
if (cause.reason === "path" || cause.reason === "format")
return stableArtifactInputFailure(cause, OPERATION);
}
const fileFailure = projectSelectedArtifactFileFailure(
cause,
OPERATION,
input.path,
"kernel image",
);
if (fileFailure !== undefined) return fileFailure;
return new ArtifactOperationError(
OPERATION,
origin === "owned-or-classified" && cause instanceof ArtifactReaderFailure
? cause.reason
: "io",
undefined,
cause instanceof Error
? cause.message
: "Kernel build config inspection failed.",
{ cause },
);
};
const kernelBuildConfigFailure = (
cause: unknown,
input: InspectKernelBuildConfigInput,
) => {
const primary = kernelBuildConfigFailurePrimary(
artifactFilePrimaryFacts(cause),
input,
);
return projectStableArtifactFailure(
cause,
primary,
input.path,
KERNEL_BUILD_CONFIG_PROVIDER.id,
OPERATION,
);
};