diff --git a/.agents/skills/rea-artifact-format/SKILL.md b/.agents/skills/rea-artifact-format/SKILL.md new file mode 100644 index 00000000000..3643ae9569c --- /dev/null +++ b/.agents/skills/rea-artifact-format/SKILL.md @@ -0,0 +1,43 @@ +--- +name: rea-artifact-format +description: Add or change a file format that inspect_artifact recognizes and extract_artifact extracts, such as a firmware image, ROM or compiled program container; not for provider-backed native analysis. +--- + +# REA artifact format + +Follow the [artifact format guide](../../../docs/development/artifact-formats.md) +for the files to add, every registration point, the tests and the docs. Start +from the nearest existing format of the same shape and read its parser, reader +and tests before writing code. + +## Establish the layout before naming it + +Collect real samples with known inputs, such as files compiled from available +sources or images with published specifications. Compare fields across +samples and against those inputs. Name a field only when that comparison or a +specification establishes it; report the rest raw with offsets. Say which +facts are observed, which are inferred and which are unknown, in the code +comments, the guide and the pull request. + +Check every claimed field boundary against all samples, not one: a slot that +ends at its text's NUL in every example can still be shorter than assumed. + +## Decide what the reader exposes + +List stored byte ranges as entries when they are useful to hash, compare or +extract. Mark an entry encrypted only when the format declares it. Bytes that +only look opaque keep a limitation and a neutral name. Decrypting, unpacking +protection or recovering protected content is outside contribution scope +without a maintainer decision; report the limit instead. + +When two containers share a layout, prefer one format with a field that +distinguishes them over parallel registrations. + +## Keep it bounded and simple + +Bound every count and size before allocation. Reuse an existing limit or +control before adding a fixed ceiling, and register any fixed ceiling with its +reason. Do not add settings or mechanisms for unobserved inputs. + +Report the sample set, versions and results, the checks you ran, and what the +samples did not cover. diff --git a/.agents/skills/rea-artifact-format/agents/openai.yaml b/.agents/skills/rea-artifact-format/agents/openai.yaml new file mode 100644 index 00000000000..a256eee342f --- /dev/null +++ b/.agents/skills/rea-artifact-format/agents/openai.yaml @@ -0,0 +1,4 @@ +interface: + display_name: "REA artifact format" + short_description: "Add a file format to artifact inspection and extraction" + default_prompt: "Use $rea-artifact-format to establish this format's layout from samples, add its parser and reader with every registration point, and document what is and is not read." diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fb95c29bb4b..2dd6b229563 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,6 +14,10 @@ To fix a reproduced defect, use the [bug fix skill](.agents/skills/rea-bug-fix/S to trace it to the boundary that owns it, prove it with an independent regression, and remove what the fix supersedes. +To add a file format to artifact inspection, use the +[artifact format skill](.agents/skills/rea-artifact-format/SKILL.md) and the +[artifact format guide](docs/development/artifact-formats.md). + For provider wiring, start with [`src/composition/binary.ts`](src/composition/binary.ts) for deep candidates and the other `src/composition/` factories for tool services, including diff --git a/docs/.vitepress/documentation.ts b/docs/.vitepress/documentation.ts index 281a7dce40e..d4e7d42f174 100644 --- a/docs/.vitepress/documentation.ts +++ b/docs/.vitepress/documentation.ts @@ -660,6 +660,11 @@ export const documentationSections = [ source: "development/tool-design.md", route: "/tool-design", }, + { + text: "Adding an artifact format", + source: "development/artifact-formats.md", + route: "/artifact-formats", + }, { text: "Testing REA", source: "development/testing.md", diff --git a/docs/README.md b/docs/README.md index bfba5ebb8d4..9fcca25c430 100644 --- a/docs/README.md +++ b/docs/README.md @@ -219,6 +219,7 @@ The [REA website](https://rea.tools/) has worked examples and product pages. - [Settings and recovery](reference/limits-and-recovery.md) - [REA limit inventory](reference/rea-limit-inventory.md) - [MCP tool design](development/tool-design.md) +- [Adding an artifact format](development/artifact-formats.md) - [Testing REA](development/testing.md) - [Client and agent verification](development/client-verification.md) - [Reconstruction readiness conformance](development/reconstruction-readiness.md) diff --git a/docs/development/artifact-formats.md b/docs/development/artifact-formats.md new file mode 100644 index 00000000000..a955f98838f --- /dev/null +++ b/docs/development/artifact-formats.md @@ -0,0 +1,136 @@ +# Adding an artifact format + +This guide covers adding a file format that `inspect_artifact` recognizes and +`extract_artifact` can extract, such as a firmware image, a ROM, or a compiled +program container. Read it with the [tool design guide](tool-design.md) and the +[testing guide](testing.md). Format inspection and ordinary extraction are in +[contribution scope](https://github.com/morluto/rea/blob/main/CONTRIBUTING.md#contribution-scope); decrypting or +otherwise recovering protected content is not, and needs a maintainer decision +first. + +## Choose the shape + +Most formats are a container whose root has header facts and whose entries +are regions of the file. Pick the reader base from what the entries are: + +| Entries are | Use | +| -------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | +| Byte ranges stored in the file | `PositionalArtifactReader`; nothing is retained, and entries marked `encrypted` are refused on open | +| Bytes REA decodes, such as decompressed or de-interleaved data | `DecodedRegionArtifactReader`; the whole file is buffered under a fixed ceiling | +| Nothing to list; a dedicated tool reads the file | A standalone format in `STANDALONE_ARTIFACT_FORMATS`, routed to that tool | + +Start from the nearest existing format of the same shape. Image4 and Nintendo +DS are positional readers; bFLT, UF2 and SWF decode regions. + +## Files to add + +| File | Owns | +| ----------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | +| `src/domain/.ts` | Magic predicate, header parse and layout. The only place the format's bytes are interpreted | +| `src/domain/formatMetadata/Metadata.ts` | Strict zod schemas for the root metadata kind and the entry metadata kind | +| `src/artifacts/ArtifactReader.ts` | Maps the parsed layout to entries and coverage, and parse failures to reader failures | +| `src/domain/.fixture.ts` | A synthetic file builder for tests | +| `src/domain/.test.ts`, `src/artifacts/ArtifactReader.test.ts` | Parser semantics, then inventory and extraction | + +## Register the format + +A new format id is listed in several places. The typecheck fails when one of +the first group is missing; the second group is not enforced, so check each. + +Enforced by `tsc`: + +- `src/domain/artifactGraph.ts`: `ARTIFACT_FORMAT_IDS` and the content-format + list. +- `src/resources/ArtifactReader.ts`: the reader `format` union. +- `src/artifacts/artifactFormats.ts`: the `container(...)` descriptor, with a + `readerLimitation` that says what is and is not read. +- `src/domain/artifactFormatRecognition.ts`: the recognized-format union. +- `src/domain/targetFormats.ts`: the target format kind, usually `archive`. +- `src/domain/artifactFormatMetadata.ts`: both metadata schemas in the union, + and a `declarationsByKind` entry deciding what artifact comparison compares. + +Not enforced: + +- `src/domain/artifactFormatRecognition.ts`: a recognizer in + `ARTIFACT_FORMAT_RECOGNIZERS`. Without it the format is never recognized. +- `src/domain/analysisView/artifactInventoryView.ts`: the formats whose summary + includes hash status counts. +- `src/domain/native/peOverlay.ts`: `PE_OVERLAY_LEADING_FORMATS`, so PE overlay + inspection can name the format. +- `src/application/binary/TargetFormatVocabularies.test.ts`: the vocabulary + row. +- `scripts/ci/scopes.mjs`: globs for the new reader, parser and guide. +- `scripts/verify/fixed-ceiling-registry.json`: any fixed ceiling; see + [limits](#limits). + +Recognizers run in order, and the first claim wins. Put a short or common +magic after recognizers whose claim is more specific, and claim only what the +magic establishes; let the parser refuse a file that is too short. + +## Parse once, at the owning boundary + +- Read integers through `BinaryReader` from `src/domain/binaryContainer.ts`. + The architecture guards reject raw `Buffer` and `DataView` numeric reads. +- Throw `ParseFailureError` through `unrecognizedInput`, `malformedInput`, + `truncatedInput` and `unsupportedInput`, with the offset, field and, for + truncation, the available and required bytes. Use `limit` only for a named + allowance. +- Keep partial facts. A region that cannot be served is omitted with a + limitation and coverage `unknown`, not a whole-file refusal. +- Name a field only when its meaning is established from the specification or + from observation against known inputs. Report other fields raw with their + offset, and name enum values only where observed; anything else is `unknown`. +- Decode text strictly, for example `new TextDecoder(..., { fatal: true })`, so + ill-formed input is reported as `malformed` rather than silently replaced. +- Mark an entry `encrypted` only when the format declares encryption. Bytes + that only look opaque keep `encrypted: false` with a limitation saying their + encoding is unknown, and get a neutral name such as `.stored` rather than an + extension they do not hold. +- Preserve declared fields, including public cryptographic values such as IVs + and key identifiers. Redact only transport credentials and values the format + explicitly marks as sensitive; do not infer secrecy from a field's name. + +## Limits + +An admission limit must follow a real format, protection or resource +constraint. Bound counts and sizes before allocation. Prefer an existing +per-call or runtime control from +[analysis settings, limits, and recovery](../reference/limits-and-recovery.md). +A fixed ceiling needs a registry entry with its class and reason in +`scripts/verify/fixed-ceiling-registry.json`, a reader mapping through +`artifactFixedLimitFailure`, and a paragraph in the limits reference. + +## Test + +- Build fixtures synthetically. Do not commit third-party binaries. +- Cover the parser's facts, every refusal kind it can raise, and limitations + with `unknown` coverage. +- Test the reader through `scanCanonicalArtifactInventory` and `extractArtifact`, + and compare extracted bytes with `Buffer.equals`; the lint rule rejects deep + equality on buffers. +- Add a recognition case to `src/domain/artifactFormatRecognition.test.ts` and a + public workflow case to + `tests/boundary/providers/native/artifactProvider.standaloneFormats.test.ts`. +- Validate against real samples when you can, and report the sample set, + versions and results in the pull request instead of committing them. + +## Document + +- Add a guide under `docs/guides//` and register it in + `docs/.vitepress/documentation.ts` and `docs/README.md`. +- Add a row to `docs/reference/supported-targets.md` and an example to + `docs/getting-started/cli.md`. +- Describe the format for agents in + `.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md`, + then run `npm run cursor-plugin:generate`. + +## Check + +```sh +npx tsc --noEmit -p tsconfig.json +npm run check:fast +npx oxfmt --check . +npm run test:local -- src/domain/.test.ts src/artifacts/ArtifactReader.test.ts src/domain/artifactFormatRecognition.test.ts src/application/binary/TargetFormatVocabularies.test.ts +npm run test:focused -- tests/boundary/providers/native/artifactProvider.standaloneFormats.test.ts tests/conformance/artifactComparisonDocs.test.ts +npm run docs:check +```