Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/context/imports/ContextImportError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const CONTEXT_IMPORT_ERROR_CODES = [
"unsupported_input",
"input_limit_exceeded",
"invalid_limits",
"empty_input",
"header_missing",
"row_limit_exceeded",
"column_limit_exceeded",
"cell_limit_exceeded",
] as const;

export type ContextImportErrorCode = (typeof CONTEXT_IMPORT_ERROR_CODES)[number];

/**
* Explicit, whole-input failure of a Context import parser.
*
* A thrown error means no proposals were produced at all; the parser never returns a silently
* truncated or partially imported result. Malformed individual rows are not errors: they are
* reported inside a successful result so the original stays available for later work.
*/
export class ContextImportError extends Error {
readonly code: ContextImportErrorCode;

constructor(code: ContextImportErrorCode, message: string) {
super(message);
this.name = "ContextImportError";
this.code = code;
}
}
24 changes: 24 additions & 0 deletions lib/context/imports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Context imports — deterministic catalog export parser

`parseCatalogExport(text)` turns the UTF-8 text of a generic catalog export CSV into **proposals**: one `catalog_export_row` per well-formed row, each with a `pointer` (1-based data row, physical source line, and the column index of every recognised field), trimmed string `claims` (track/artist/release title, release date, label, track number), shape-checked `identifiers` (`isrc`, `upc`), the unmapped columns verbatim, and the raw cells. The result also carries `parserVersion` (`catalog-export-csv-v1`), `profileVersion` (`generic-catalog-export-v1`), a `contentFingerprint` (SHA-256 of the UTF-8 encoding of the decoded text passed in) and a `headerFingerprint` (SHA-256 of the verbatim header cells), so a later slice can recognise an exact replay of the same text, a changed version with the same shape, or a different export layout.

`contentFingerprint` is a parse-input fingerprint, **not** the byte hash of a retained original: a decoder may strip a byte-order mark or replace invalid bytes, so the same file can hash differently here than its stored byte SHA-256. Source versions must stay keyed on the original's own byte hash.

## What it does not do

- It does **not** import, review, accept, link, store or expose anything. Every proposal is literally `status: "proposed"`; there is no producer for a reviewed or accepted state in this module. "Parsed" is a separate state from available/imported/reviewed.
- It does **not** establish identity, roster membership, catalog interest, rights or mandates. Claims are the customer's cell text carried as data; instruction-like text in a cell stays a plain string and produces no flag or behaviour.
- It never invents, pads or repairs an identifier. ISRC (2 letters, 3 alphanumerics, 7 digits) and UPC/EAN/GTIN (12–14 digits) are checked by shape only after removing ASCII hyphens/spaces and upper-casing ASCII letters; tabs, line breaks or any other character make the cell `malformed` with the exact decoded cell text kept; an empty or ASCII-space-only cell is `unknown`; and when the export has no recognised column for the identifier it is `uncollected` (counted separately in the summary). Check digits are not validated.
- Malformed rows (`empty_row`, `column_count_mismatch`, `unterminated_quote`, `text_after_closing_quote`) are reported with pointers beside the proposals, never dropped or fixed. A cell with text after its closing quote (for example `"Heroes" (Live)`) is not valid CSV, so its record is malformed and the cell keeps its literal source text, quotes included. A quote that nothing later in the input can close is an `unterminated_quote` at its starting line, however much text follows. Blank lines after the last record are not records.
- Whole-input failures throw `ContextImportError` with a code (`unsupported_input`, `empty_input`, `header_missing`, `row_limit_exceeded`, `column_limit_exceeded`, `cell_limit_exceeded`, `input_limit_exceeded`, `invalid_limits`) and no partial result. Decoded input is capped at 1,048,576 UTF-16 code units before scanning; data rows at 10,000, columns per record at 1,000, and ordinary cells at 10,000 code units. An unterminated quoted tail is retained as malformed even when it exceeds the cell cap, but remains bounded by the total input cap. Tokenizer overrides can only tighten the limits: integer rows from 0 to 10,000, columns from 1 to 1,000, and cell length from 1 to 10,000. Pending blank lines use constant-size bookkeeping and are checked against the row cap before interior blank records are materialized. Callers must still bound the byte size of an original before decoding it.
- Duplicates are signals, not deletions: `same_isrc` and `same_row_content` list the rows involved, ordered by first row; a shared UPC is expected within one release and is not reported.
- The header profile is a small distributor-neutral alias set (see `resolveCatalogExportField.ts`); when two columns map to the same field the first wins and the second is retained as unmapped. A bare `Title`, `Song` or `Song Title` stays unmapped because it may name a release or a composition rather than a recording; the parser does not guess a row's grain, and `catalog_export_row` makes no recording/release claim. The profile has only been exercised on synthetic fixtures, not calibrated against a real distributor export. Nothing distributor- or customer-specific belongs here.
- Pure: no database, HTTP, MCP, storage, provider, environment, clock or randomness. Same text in, identical result out.

## Where a later slice connects it

A retained original (api#987/#990 receipts, database#94/#95) would be read as text by the server, passed through `parseCatalogExport`, and the proposals persisted against that exact source version with their pointers and `parserVersion`/`profileVersion`, then surfaced through the shared Context operation and evidence manifest (api#983/#985). Reviewing a proposal into an assertion, staling only affected reviews when a new version arrives, and conflict resolution between versions are all owned by that follow-up; this module only provides the fingerprints and pointers it needs.

## Unsupported here

PDF, images/OCR, DDEX, royalty or sales statements, XLSX/zip containers, tab- or semicolon-delimited text, non-UTF-8 encodings, and any paid model extraction. Only some of these are detected: PDF and zip signatures (with or without a byte-order mark), C0/C1 control characters, U+FFFD replacement characters, unpaired surrogates and a single-column header containing a tab or semicolon fail with `unsupported_input`. Other formats and source encodings that decode to ordinary text are not detected and must be excluded before calling this text parser.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
track_title,artist_name,release_title,isrc,upc
Example Song One,Example Artist,Example Release,ZZTST2600001,000000000017
Example Song Two,Example Artist
Example Song Three,Test Artist Two,Another Release,ZZTST2600003,000000000024

Example Song Four,Test Artist Two,Another Release,ZZTST2600004,000000000031
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
track_title,artist_name,release_title,isrc,upc,release_date,label,track_number,notes
Example Song One,Example Artist,Example Release,ZZ-TST-26-00001,000000000017,2026-01-16,Example Label,1,first row
"Example Song, Two",Example Artist,Example Release,ZZTST2600002,000000000017,2026-01-16,Example Label,2,"quoted, with comma"
Example Song Three,Test Artist Two,Another Release,,,2025-11-02,,3,no identifiers
Example Song Four,Test Artist Two,Another Release,not-an-isrc,12345,2025-11-02,Example Label,4,malformed identifiers
Example Song One,Example Artist,Example Release,ZZ-TST-26-00001,000000000017,2026-01-16,Example Label,1,first row
Loading
Loading