# 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 ```