Vai al contenuto

๐Ÿ“ฆ BRIM Plugin Guide

How to create a new Broker Report Import Manager plugin to support a new broker's CSV/Excel export format.

Base class: BRIMProvider (in backend/app/services/brim/brim_provider.py) Plugin folder: backend/app/services/brim_providers/<code>/ โ€” the code in plugin.py, beside an __init__.py holding only its docstring (see Auto-Discovery) Registry: BRIMProviderRegistry


๐Ÿค– Tip: Let an LLM write the plugin for you

Paste this entire page plus the Generic CSV Provider page into an LLM and use this prompt:

Suggested prompt

Here is the LibreFolio BRIM plugin specification: [paste this page]
Here is the Generic CSV reference implementation: [paste generic_csv page]

I have a broker called [BROKER_NAME]. Here is a sample of their CSV export: [paste header + 3โ€“5 rows]

Write a complete Python BRIMProvider plugin for this broker. Follow the conventions
exactly (BRIMParseOutput, BRIMExtractedAssetInfo, sign conventions, @register_provider).

The LLM will produce a complete, working plugin skeleton. Test it with the probe endpoint, then save it as brim_providers/<code>/plugin.py, beside an __init__.py holding only a docstring, and restart โ€” it will be auto-discovered.


๐Ÿ”„ Flow

The system calls plugin methods in two distinct phases:

graph TD
    subgraph "Phase 1 โ€” Detection (upload, and first read after an update)"
        D1["File uploaded"] --> D2["can_parse(file_path)<br/><small>Quick header/extension check</small>"]
        D0["Original read after an update<br/><small>plugins_signature โ‰  app version</small>"] --> D2
        D2 -->|true| D3["Plugin listed as<br/>compatible option"]
        D2 -->|false| D4["Skip"]
    end

    subgraph "Phase 2 โ€” Parsing (user selects plugin)"
        P1["parse(file_path, broker_id)<br/><small>Full parsing</small>"] --> P2["Returns:<br/>โ€ข BRIMParseOutput"]
        P2 --> P3["User reviews<br/>in preview UI"]
        P3 --> P4["POST /transactions<br/><small>Core handles persist</small>"]
    end

    D3 ~~~ P1

    style D0 fill:#e3f2fd,color:#1b1b1b,stroke:#1565c0
    style D1 fill:#e3f2fd,color:#1b1b1b,stroke:#1565c0
    style D2 fill:#e3f2fd,color:#1b1b1b,stroke:#1565c0
    style D3 fill:#e3f2fd,color:#1b1b1b,stroke:#1565c0
    style P1 fill:#e8f5e9,color:#1b1b1b,stroke:#2e7d32
    style P2 fill:#fff3e0,color:#1b1b1b,stroke:#e65100
    style P3 fill:#fff3e0,color:#1b1b1b,stroke:#e65100
    style P4 fill:#f3e5f5,color:#1b1b1b,stroke:#7b1fa2

Phase 1 runs automatically when a file is uploaded โ€” every registered plugin is asked if it can parse the file. Compatible plugins are listed for the user. It runs again, once per app version, when an original detected by another version is read: see The lifecycle of compatible_plugins.

A file's compatible_plugins are what the import wizard offers for it, so can_parse must not claim a file that parse would refuse, fallback plugins included: the Generic CSV (broker_generic_csv, detection_priority 0) says True only for a .csv whose header row names both required columns, date and type, directly or through its multilingual aliases (HEADER_MAPPINGS: data, fecha, datumโ€ฆ; tipo, operazione, actionโ€ฆ) โ€” it used to claim any CSV with a header, then fail at parse. A file no plugin recognises is offered every plugin; parsing it with a single-file plugin whose can_parse says no answers 400 Plugin '<name>' cannot parse file '<file name>' โ€” the plugin's provider_name and the name the file was uploaded with โ€”, followed by : <reason> when the plugin says why (cannot_parse_reason), and moves the file to failed with that message as its error.

The rule has one extension: a plugin may claim a file it recognises as its own broker's even when it cannot import it, provided parse explains what is wrong โ€” no transactions and one BRIMNotice in the file's language saying what to upload instead. The plugin does not fail to read such a file: it knows the file is its broker's, so cannot_parse_reason stays None, and parse succeeds instead of refusing it โ€” the file moves to parsed with the notice. Reference: DEGIRO's Transactions export, the list of orders, which has no dividends, deposits, withdrawals or currency conversions. broker_degiro claims it next to the Account Statement (_layout tells the two apart), and its parse returns no transactions and the degiro_orders_list warning, which asks for the Account Statement.

Phase 2 runs when the user selects a specific plugin โ€” the plugin parses the file, the user reviews the results, and confirms the import.

Plugin responsibility: Read the broker-specific file format and convert to standard TXCreateItem DTOs. Core responsibility: File storage, asset matching, duplicate detection, database persistence.

A bank that splits one account across several exports (Danske Bank: a custody XLSX and a cash CSV) adds a step between the two phases: the exports uploaded together form a report set, the plugin combines them, and phase 2 parses the combined file. See Multi-report plugins (report sets).

โ™ป๏ธ The lifecycle of compatible_plugins

A file's compatible_plugins is detected once per app version, not once for the file's whole life (brim_provider.py):

  1. At upload, save_uploaded_file asks every plugin (BRIMProviderRegistry.get_compatible_plugins) and stores the list in the file's sidecar with plugins_signature, the signature of the plugin catalogue that detected it: detection_signature(), which is the app version (get_version()).
  2. On read โ€” get_file_info and list_files, so also the wizard's Plugin column, its set grouping (setPluginFor) and collect_members โ€” an original whose stored signature is missing (a file uploaded before 1.2) or differs from the running version's is detected again, on the data file beside its sidecar, whatever its status. The read returns the new list and saves it with the current signature, once per version; under the current signature the stored list is trusted as it is.
  3. Kept as stored: a combined file is never detected again โ€” its list is the plugin that built it. An original whose data file is gone, or whose detection fails, keeps its stored list and nothing is saved: the next read tries again.

The effect: after an update, a plugin added or changed since a file's upload is offered for it, and one that no longer reads it drops out โ€” the Generic CSV, stricter since 1.2, drops from the CSVs that lack its date or type column, which 1.1.0 offered it for. Files uploaded together (one batch_id) before a report-set plugin could read them form their set after the update, without a new upload. Files uploaded with 1.1.0 or earlier are the exception: those versions recorded no batch_id, so the files belong to no set โ€” each is offered the set plugin, whose parse answers 422 set_required โ€” and must be uploaded again, together.

For a plugin author there is nothing to implement, but can_parse answers are cached per app version and evaluated again after an update:

  • a change to can_parse reaches the files already uploaded with the next app version, and needs no plugin_version bump: that version covers what parse and combine output, not detection;
  • a can_parse that raises counts as False (get_compatible_plugins skips the plugin), and that answer is cached like any other until the next version โ€” never raise;
  • in a development checkout the version is git describe --tags --always --dirty, unless LF_APP_VERSION is set (Version Stamps), read once per process: an edited can_parse reaches the files already uploaded only once that string changes (a commit) and the server restarts โ€” or upload the file again.

The detection itself takes no lock, because it reads the file. The save takes the broker metadata lock, reads the sidecar again under it and writes only if the sidecar is still where it was read and still lacks the current signature, changing only compatible_plugins and plugins_signature: a move, a deletion or another write made meanwhile is never undone. Because a read may now open the file, the API calls list_files and get_file_info (in _get_brim_file_with_access) through asyncio.to_thread.

๐Ÿ”’ The broker metadata lock

Every write of a broker's sidecars holds that broker's lock, broker_metadata_lock(broker_id) (_broker_metadata_lock inside brim_provider.py). A plugin never takes it: the core does, around its own writes.

  • Two layers. In the process, a re-entrant threading.RLock per broker; across processes, an advisory fcntl.flock on broker_reports/.locks/broker_<id>.lock (broker_none.lock for the legacy files at the root of a status folder). The file lock is taken once, by the outermost block of a thread, and released when that block ends, on an exception too. It must hold across processes because a server may run several uvicorn workers (./dev.py server --workers N); the Docker image runs one.
  • Holders: the upload (save_uploaded_file, around its sidecar write), _update_metadata, save_combined_file, save_parse_result, _move_file (move_to_parsed, move_to_failed), delete_file, and the save of a re-detection. The key is the broker of the sidecar's folder, <status>/broker_<id>/; a move changes only the status folder, so every writer of a file takes the same lock.
  • Reads take no lock: every write is an atomic rename (_write_metadata_atomic), so a reader sees the old sidecar or the new one.
  • Never across an await: re-entrant within a thread, the lock would let two coroutines of the event loop in together. combine_set holds it in a worker thread, from the reuse check to the save (POST /sets/combine).

๐Ÿงญ Import philosophy & currency rules

A BRIM plugin is a faithful transcriber, not a re-calculator. These rules are mandatory for every new plugin:

  • Verbatim / copy-paste import. Import the numbers exactly as they appear in the broker's report. Do not recompute totals, prices or amounts; transcribe them.
  • No forex system. A BRIM plugin must never invoke the FX subsystem and must never convert amounts using the report's own exchange-rate column. If the export has a rate column (e.g. Fineco's Cambio), ignore it.
  • Currency from the source. Derive each transaction's currency from the broker's own currency column (per row) and tag every monetary figure in that row with it. A single file may legitimately contain multiple currencies โ€” that's expected, keep them as reported.
  • Delimiter detection. Always resolve the separator with self.detect_csv_delimiter(file_path) โ€” never hardcode , or ;.
  • Encoding fallback. Never open an export with a fixed encoding such as open(file_path, encoding="utf-8-sig") โ€” banks and Excel on Windows often write Windows-1252 or Latin-1, and a UTF-8 read fails on the first accented byte. Read text with self._open_text(file_path) (add newline="" when the stream feeds csv and line endings must be kept) or through _brim_io.read_rows. The test suite enforces it (class TestWindows1252Invariance): test_no_plugin_opens_files_with_a_fixed_encoding fails on any open(..., encoding=...) in a module of a broker_*/ plugin folder, and test_sample_parses_identically_when_saved_as_windows_1252 requires every non-ASCII CSV sample, re-saved as Windows-1252, to be detected and parsed identically by each plugin that accepts the original.
  • Multiple export layouts. When a broker ships more than one report layout (e.g. with and without commission columns), detect the variant dynamically โ€” locate the header row and branch on the actual column set rather than assuming a fixed line offset.
  • Sign conventions. Set signs per TransactionType (BUY: qty > 0, cash < 0; SELL: qty < 0, cash > 0; DIVIDEND/INTEREST: qty = 0, cash > 0; FEE/TAX: qty = 0, cash < 0; ADJUSTMENT: qty โ‰  0, no cash), but take the magnitudes verbatim from the report.
  • One source row may emit several transactions. Keep economic legs explicit: securities-only buys can be DEPOSIT + BUY, sells can be SELL + WITHDRAWAL, maturities can be SELL at par + INTEREST, and snapshots can be DEPOSIT + ADJUSTMENT seeds.
  • Fake asset IDs. Emit high positive fake asset IDs (keyed by ISIN/ticker) plus BRIMExtractedAssetInfo so the core can drive the asset-matching UI.
  • The operation date, and only settled rows. Record every transaction on its operation date, the day the operation was made (the trade date), never on the settlement or value date (the developer's principle, 19/10/2026): a purchase made on the 27th is a BUY dated the 27th, whenever its cash moves. Why: the movement is recorded when it happens, not when the systems process it: in between the money is already gone and cannot be used, so hiding it would be wrong (the developer, 28/10/2026). No exception for a subscription during a placement or an IPO: the order of 17/02 is dated 17/02 even when the issue and the cash debit come on 25/02; LibreFolio shows the position at cost and the cash already gone in between, which is what happened. The report's other dates only tell whether the row is stable and booked, read from the columns the report has:
    • a status column, when there is one, decides; what it says follows the next rule (reserved, cancelled or pending are known; a word the plugin does not know is unknown);
    • otherwise a value or settlement date after the server's today (server_today() in backend/app/utils/datetime_utils.py, never the browser's date) marks a provisional row: Fineco, notice fineco_not_yet_settled (Fineco notes);
    • a report that covers a known period compares with its end instead: Danske Bank, notice excluded_not_yet_settled, for the custody trades valued after the cash statement's last day (Finnish notices);
    • with neither, the operation date itself must not be after today.
    • A provisional row is left out whole, with every transaction it would have produced (its fee, its premium), and arrives with the next import. Say so with one severity="info" notice per reason (a code, context.count, the rows as evidence), never a warning: nothing is wrong, the rows are simply not final yet.
    • A report set that compares balances with the bank's, as Danske Bank does, compares them in operation-date terms. A truth on a day adds what is still in flight then: the cash and the quantities of the trades the set imports with an operation date up to that day and a value date after it (_Analysis.in_flight). The set opens on the eve of the oldest operation date of the export, even when the cash statement starts later: the statement's first balance then stands for the opening (Danske Bank โ†’ Truth points).
    • The one exception is the Generic CSV: its files are the user's own, so the date column the user maps is the transaction date, whatever it means (trade_date, value_date and settlement_date are all aliases of its date column, Generic CSV).
    • Which date each plugin records (inventory W-B88, 23/10/2026; a column that only the plugin's own tests read is not a date the plugin uses). Every plugin that has both dates in its report records the operation date: Directa (Data operazione), DEGIRO (Datum / Date), InvestEngine (Trade Date/Time), Saxo (Trade Date), Crรฉdit Agricole (Data operazione for securities, Data Op. for cash), Schwab (Date: of 03/01/2025 as of 02/28/2025 the first date counts), Fineco and Danske Bank. Disnat records Date de transaction and falls back to Date de rรจglement only for a row whose trade date is - (a dividend has no other date). Avanza, Intesa and Rabobank only have a currency column called Valuta, never a value date. The test test_brim_operation_date.py (./dev.py test external brim-operation-date) fixes this per plugin, on rows whose two dates differ: a plugin that switches to the value date fails it.
  • Known rows quietly, unknown rows loudly. Leave a row out only for a reason the plugin can name (lot W-B85, the developer's principle of 19/10/2026):
    • Known and safe to leave out โ€” a blank, header or total row, a reservation, a row the broker marks as cancelled, a duplicate of another row: no notice, at most one backend log line per reason (logger.info(โ€ฆ, reason=โ€ฆ, count=โ€ฆ)). In a report set the row keeps its place in the set's accounting: an excluded outcome with its reason, counted in the summary's reasons and labelled in the wizard by importWizard.reportSet.reason.<reason>. Example: Danske Bank's reservation, a cash row labelled Varaus (reserved, not booked).
    • Unknown or unreadable โ€” a type, status or description the plugin does not recognise; a date, amount or required field it cannot read: leave it out, and say so with one severity="warning" notice per reason (a code, context.count, the rows as evidence) that carries context.report_issue = True. The helper RowSkips builds that notice for you (Rows left out): a plugin collects the rows and returns two shared notices, instead of writing a string warning for each row. The wizard then shows the link open an issue on GitHub under the warning, on every instance, self-hosted or hosted (context keys): the users become the canaries of a broker whose export changes. Example: Danske Bank's excluded_status, the rows whose Tila is neither executed nor booked.
    • Never silently leave out a row that may carry an economic effect (a split, a fee, a tax, a currency conversion) unless the plugin knows that is safe: a row it cannot import is unknown. A row the plugin reads but only in part (Trade Republic's fee and tax columns, not imported) is unknown too: list it.
    • A stable plugin follows the rule: it uses RowSkips, or its own codes with context.report_issue.
    • A plugin in beta may not yet. The plugins the lot W-B86 did not reach (the pending set of its contract test: the beta plugins, Intesa, the Generic CSV) may still write one plain string per skipped row (a legacy warning, no link): with no reliable reports there is no way yet to tell the safe rows from the unknown ones (decision of 19/10/2026, confirmed 22/10). It is a rule, not a lot: leaving the beta means adopting RowSkips (or own codes with report_issue), in the same change that removes the Beta label. The contract test says what to do: take the plugin out of ROW_SKIPS_PENDING, list it in ROW_SKIPS_BY_PLUGIN with an unknown and an unreadable row, bump plugin_version. Fineco did it when it left the beta (W-B92, version 1.5.0); the plugins that already use the helper are in Rows left out.
    • The provisional rows of The operation date, and only settled rows are neither: they keep their info note.
    • report_issue also serves a plugin in beta that imports a row but doubts its own reading, on an info note: Fineco's fineco_incomplete_rows imports the rows missing a required number and asks the user to open an issue if the file is right.

๐Ÿ”ข Decimal places

Every figure a plugin emits must fit its transactions column (C-97; the types are in Decimal Columns): a quantity keeps 10 decimal places, a cash amount 6, a per-unit cost_basis_override 12 โ€” the scale of the prices โ€” and any of them at most 15 significant digits. A cost_basis_override is also never negative (0 for a gift or the new shares of a split): below 0, the batch refuses its row when the user saves it (costBasisNegative, J-60, see Cost basis sign).

  • Read from the file: never rounded. A figure the report states goes in as written, under the verbatim rule above. Over a limit, neither the plugin nor the import rounds it: the batch refuses its row when the user saves it (decimalPlacesExceeded or significantDigitsExceeded, with the maximum; decision D2, see Decimal limits).
  • Computed by the plugin: rounded, explicitly. A figure the plugin derives can have any number of decimals: a per-unit cost as a total divided by the quantity (1000 / 3), a bond price divided by 100. Round it to its column where it is computed, with the one helper round_computed_value(value, model, column_name) (backend/app/db/precision.py): ROUND_HALF_EVEN to the scale of the model's column, then to 15 significant digits on a RealDecimal column (decision D6). On such a column the rounding always brings the result within the decimal places and the significant digits, but not within the third limit, the digits before the decimal point (15 for a quantity and a cash amount, decision N63; 12 for a per-unit cost), which no rounding brings back: the confirmation refuses a value past them with integerDigitsExceeded. broker_intesa/plugin.py calls it on the patrimonio seed's per-unit cost, broker_credit_agricole/plugin.py on the per-unit cost of a bond received by succession, its price divided by 100.
  • An exponent past ยฑ100: refused while parsing. Currency itself refuses an amount whose exponent (Decimal.adjusted()) is outside ยฑ100, whoever builds it (check_decimal_exponent() in backend/app/schemas/common.py, R13-SEC7-3): a plugin that builds such a Currency fails while it parses the file (pydantic's ValidationError, a ValueError), not at confirmation. No real report comes near it: no column takes more than 24 digits before the decimal point, or 12 after.

๐Ÿ“‹ ABC Methods

โœ… Required (Abstract)

Method Signature Description
provider_code @property โ†’ str Unique identifier, and the name of the plugin's folder (e.g., "broker_directa"). Must return a string literal โ€” see below
provider_name @property โ†’ str Display name, in English (e.g., "Directa CSV"); the UI shows the value of brimPlugins.<code>.name, in the user's language โ€” see Register the name and description
description @property โ†’ str Brief description for the UI, in English; the UI shows the value of brimPlugins.<code>.description
can_parse(file_path) โ†’ bool Quick check if this plugin can parse the file (check extension, header row)
parse(file_path, broker_id) โ†’ BRIMParseOutput Full parsing โ€” returns structured BRIMParseOutput object containing transactions, warnings, and extracted asset info

provider_code must return a string literal

Write return "broker_my_bank" โ€” never a module constant (return PROVIDER_CODE) or an expression. Two tools read the code from the source, without importing the module:

  • the R13 test in frontend/src/lib/components/ui/select/optionFilter.test.ts (run by ./dev.py test front-utility core-unit) scrapes every broker_*/plugin.py for what provider_code, provider_name and description return, and fails on anything but string literals (adjacent literals and a parenthesised run of literals are accepted; an f-string, a name, a call or a + are not);
  • the test runner's _PROVIDER_CODE_RE in scripts/test_runner/_backend_external.py builds the list of BRIM codes shown in the --providers / --exclude-providers help: a plugin it cannot read is missing from that list (the filter itself still works, because the option has no fixed choices).

Danske Bank once returned a constant: the R13 suite failed as a whole, and the runner's help listed one BRIM plugin too few. provider_name and description follow the same rule, because the R13 test reads them the same way.

๐Ÿ”ง Optional (Override)

Method Default Description
supported_extensions ['.csv'] Accepted file extensions
detection_priority 100 Auto-detection priority (higher = checked first). Use 0-49 for generic plugins.
cannot_parse_reason(file_path) None Why can_parse refuses a file, in one short sentence the user can act on โ€” appended to the parse guard's 400. See Saying why a file is refused
cannot_parse_detail(file_path) wraps cannot_parse_reason, no code The same refusal as a BRIMRefusal, with a stable code the frontend translates and its context โ€” asked by the import wizard right after an upload. See Saying why with a code
icon_url None Broker favicon URL for the UI (see Favicons)
docs_url None Link to a user-facing MkDocs page. Leave None if no page exists (avoids dead links).
plugin_version "1.0.0" Semver of the parsing logic โ€” bump it whenever output for the same input changes
test_file_pattern None Single filename substring used by the test suite to map a sample โ†’ this plugin
test_file_patterns derived from test_file_pattern List of filename substrings when one plugin owns several export formats (e.g. ["revolut-invest", "revolut-crypto"])
report_roles (and the rest of the report-set contract) [] Non-empty turns the plugin into a report-set plugin: see Multi-report plugins
generate_static_url(path) โ€” Helper to build /api/v1/uploads/plugin/brim/{path}

๐Ÿ—ฃ๏ธ Saying why a file is refused

cannot_parse_reason(file_path) -> Optional[str] is a concrete method of BRIMProvider, not an abstract one: it returns None by default, so a plugin written without it stays valid. Override it when your can_parse refuses a file for a cause the user can fix in the file โ€” a missing column, say.

  • When the core asks. In the guard of parse_file (brim_provider.py), after can_parse answered False for the file the parse runs your plugin on. The guard asks about the path it checked last: the relocated one, when a concurrent parse moved the file in the meantime. The default cannot_parse_detail asks it too, when the import wizard checks an upload against the broker's default plugin.
  • What it returns. One short English sentence saying why can_parse refuses the file, with a lowercase start and no final period, because it completes the guard's message; None when there is nothing to add.
  • What the user reads. The guard's Plugin '<name>' cannot parse file '<file name>' gains : <reason>. It names the plugin by its provider_name and the file by the name the user uploaded it with, never by the plugin code or the <file_id><ext> LibreFolio stores it under; a duck-typed plugin without provider_name is named by its code. The API answers 400 with that text, the file goes to failed with it as its error_message, and the wizard's parse-error box shows it after the user's file name:

    export.csv โ€” Plugin 'Generic CSV' cannot parse file 'export.csv': required column 'date' not found in the CSV header
    
  • Cost and failure. Keep it as cheap as can_parse and never raise. Without a reason โ€” the default None, or no such method at all, since the guard reads it with getattr โ€” the message stays plain. If the method raises anyway, the guard logs the exception and keeps the plain message: still a 400, never a 500.

The Generic CSV is the reference implementation. It checks, in this order, and gives each refusal a code too:

The file cannot_parse_reason answers code and context
has an extension other than .csv the Generic CSV reads only .csv files csv_only, with its own plugin_code
cannot be read the file could not be read file_unreadable
is empty, or starts with an empty row the file has no header row no_header_row
has a header row without date, type or both โ€” no alias of HEADER_MAPPINGS matches required column 'date' not found in the CSV header, the same with 'type', or required columns 'date' and 'type' not found in the CSV header missing_columns, with columns (date, type or date, type) and count (1 or 2)
has a header row naming both None โ€”

Its can_parse is return self.cannot_parse_reason(file_path) is None, and the sentence is the message of its cannot_parse_detail, so the three can never disagree: build yours the same way when your refusals have causes worth naming. The suite holds every registered plugin to the contract: test_every_plugin_answers_nothing_or_one_sentence, in backend/test_scripts/test_external/test_brim_providers.py (./dev.py test external brim-providers), asks the method about every file of sample_reports/ and about paths with nothing to read, and fails on an exception or on an answer that is neither None nor one sentence (a non-empty single line, lowercase start, no final period).

๐Ÿท๏ธ Saying why with a code

cannot_parse_detail(file_path) -> Optional[BRIMRefusal] gives the same refusal as cannot_parse_reason, with a stable code the frontend translates. It is a concrete method of BRIMProvider too: the default wraps cannot_parse_reason without a code (BRIMRefusal(message=reason), or None when there is no reason), so every plugin answers it unchanged. Override it when the user should read your reason in their own language.

BRIMRefusal (backend/app/schemas/brim.py):

Field Type Content
code Optional[str] Stable snake_case code (^[a-z][a-z0-9_]*$), translated by the frontend as importWizard.parseRefusal.<code>; None when the plugin gives only a sentence
message str, not empty The cannot_parse_reason sentence โ€” one short English sentence, lowercase start, no final period: the fallback text
context Dict[str, Any] The parameters of the translation, e.g. the plugin that reads the file
  • Same rules as cannot_parse_reason: as cheap as can_parse, None when there is nothing to add, and never raise.
  • One sentence, two methods: message must be what cannot_parse_reason returns. Write the detail and derive the sentence from it, as the Scalable plugins do โ€” the parse guard keeps reading cannot_parse_reason, the plugin check below reads cannot_parse_detail:

    def cannot_parse_detail(self, file_path: Path) -> Optional[BRIMRefusal]:
        return _scalable.refusal(self, file_path, _scalable.BROKER)
    
    def cannot_parse_reason(self, file_path: Path) -> Optional[str]:
        detail = self.cannot_parse_detail(file_path)
        return detail.message if detail else None
    

The endpoint. GET /api/v1/brokers/import/files/{file_id}/plugin-check?plugin_code=<code> (check_file_plugin in api/v1/brokers.py) asks one plugin about one uploaded file and answers a BRIMPluginCheck: plugin_code, can_parse and refusal.

{
  "plugin_code": "broker_scalable",
  "can_parse": false,
  "refusal": {
    "code": "scalable_deposit_file",
    "message": "this is the export of the Scalable overnight account: read it with the Scalable Capital overnight account plugin",
    "context": {"plugin_code": "broker_scalable_deposit", "plugin_name": "Scalable Capital overnight account"}
  }
}
  • Nothing is parsed, and the file keeps its status. The work is check_file_with_plugin (brim_provider.py), run off the event loop (asyncio.to_thread) because the plugin reads the file.
  • refusal comes only with can_parse: false, and stays null when the plugin has nothing to add. A can_parse that raises counts as false, and a cannot_parse_detail that raises leaves refusal empty: both are logged, never a 500. As in the parse guard, a refusal on a path that a concurrent parse has just moved is asked again on the new path.
  • Access: any user with a role on the file's broker (VIEWER+), the uploader of a file with no broker, and the administrator; 403 otherwise (Who reaches a report file). 404 for an id that is not a UUID, an unknown file, an unknown plugin, or a file whose content is gone.

In the import wizard. Right after an upload, the wizard asks the endpoint about the broker's default import plugin. When that plugin refuses the file, the wizard shows the reason under Notes from the plugin in its wrong-broker prompt, translated as importWizard.parseRefusal.<code>, with context as its parameters, and falls back to the English message when the refusal has no code or the code no translation. A plugin that context names by plugin_code gets its name in the UI language as plugin_name, from its catalogue key: the plugin_name the refusal carries โ€” the named plugin's English provider_name โ€” is only the fallback.

Example: Scalable Capital. One plugin per account, each account in its own LibreFolio broker: broker_scalable reads the broker account, broker_scalable_deposit the overnight account, and both import the shared reader _scalable.py. Each refuses the other account's file and names the plugin that reads it (_scalable.refusal):

code Given by For context
scalable_deposit_file broker_scalable the exporter's overnight account file broker_scalable_deposit, Scalable Capital overnight account
scalable_broker_file broker_scalable_deposit the exporter's broker account file broker_scalable, Scalable Capital broker
scalable_prime_file broker_scalable_deposit Scalable's own CSV (PRIME) broker_scalable, Scalable Capital broker
scalable_mixed_file both an exporter file mixing the two accounts empty: the message asks to export the two accounts separately

context names the other plugin, as plugin_code and plugin_name. Any other file gets None: the two plugins give a reason only for Scalable's own exports.

Shared codes. Some causes belong to no broker: the extension, a file that cannot be read, no header row, missing required columns, no movement rows. Their codes are shared: a plugin that refuses a file for one of these causes reuses the code, and its translation, instead of adding its own.

code Given today by context English text
csv_only Generic CSV plugin_code of the refusing plugin The {plugin_name} plugin reads only .csv files.
file_unreadable Generic CSV, DEGIRO empty The file could not be read.
no_header_row Generic CSV empty The file has no header row.
missing_columns Generic CSV columns, the names joined by ,; count, an integer that picks the plural Required column(s) {columns} not found in the CSV header.
no_movement_rows DEGIRO empty The file has no movement rows.

Example: DEGIRO. broker_degiro reads only the Account Statement exported as CSV. Besides the shared file_unreadable and no_movement_rows, it has two codes of its own, with an empty context: degiro_csv_only, for a file that is not a .csv (the message asks to download the Account Statement as CSV), and degiro_not_account_statement, for a CSV of another layout (12 columns with the ISIN fifth, unnamed amount columns ninth and eleventh, dates as DD-MM-YYYY). Unlike its notices, which keep the language of the file (the tip Warning language follows the input format, below), a refusal is shown in the UI language: a file the plugin refuses has no language it recognised.

Where the translations live. Every code is a key of importWizard.parseRefusal, in the import wizard's sources (frontend/src/lib/i18n/importWizard/{en,it,fr,es}.json), whether it is shared or a plugin's own: a plugin's fragment holds only its brimPlugins.<code> keys (The Ownership Gate). Add one with the i18n tool, which finds the source from the prefix:

./dev.py i18n add "importWizard.parseRefusal.mybroker_xlsx_only" --en "โ€ฆ" --it "โ€ฆ" --fr "โ€ฆ" --es "โ€ฆ"

A message with a count is an ICU plural, and its text must differ between languages (Locale-Sensitive ICU Messages, in the frontend i18n guide). TestGenericCSVRefusesWithACode (test_brim_providers.py) and TestRefusalCodes (test_brim_degiro.py) pin the codes and contexts of the two plugins file by file; defaultPluginCheck.test.ts (./dev.py test front-transaction tx-unit) checks that every code has its key in the four languages.

๐Ÿ“ฎ How a failed parse reaches the wizard

POST /api/v1/brokers/import/files/{file_id}/parse answers a failed parse with 400 and a detail object, never an English sentence the wizard would show as it is (lot W5-5, C-70b):

{"code": "parse_failed", "message": "Parse error: โ€ฆ", "plugin_code": "broker_x", "plugin_name": "Broker X",
 "reason": {"code": "missing_columns", "message": "โ€ฆ", "context": {"columns": "date", "count": 1}}}
  • A BRIMParseError(message, details={"code": โ€ฆ, "context": โ€ฆ}) gives reason from its details (code and context are null and {} when the plugin gave none) and message as the diagnostic Parse error: <message>.
  • The parse guard (can_parse false, BRIMCannotParseError, a ValueError) gives reason from the plugin's cannot_parse_detail; message is the guard's own sentence.
  • Any other ValueError (an unknown plugin) keeps a plain string detail; a missing file is still 404.
  • The file goes to failed with the English text as its stored error_message, as before: it is the diagnostic, not what the user reads.
flowchart LR
    A["Plugin raises<br/>BRIMParseError(message, details={code, context})"] --> B["API: 400 detail<br/>parse_failed {reason: code, context}"]
    A -. "English message" .-> L["logs and error_message<br/>of the failed file"]
    B --> C["Wizard: resolveParseFailureMessage<br/>(parseFailure.ts)"]
    C --> D["frame importWizard.parseFailed"]
    C --> E["reason importWizard.parseRefusal.&lt;code&gt;<br/>with context as values"]

The wizard (resolveParseFailureMessage, parseFailure.ts) shows importWizard.parseFailed with the plugin's name and the reason, translated as importWizard.parseRefusal.<code> with the context (the keys of the refusals, and the ones of every BRIMParseError code), else the plugin's own sentence (resolveParseRefusalMessage, defaultPluginCheck.ts). A failure with no reason at all shows importWizard.parseFailedNoReason. The same helper reads the 400 of the two file previews (GET โ€ฆ/files/{file_id}/preview and GET /api/v1/uploads/file/{file_id}/preview): {"code": "preview_unsupported" | "preview_unreadable", "message": "โ€ฆ"}, translated as importWizard.previewFailed.<code>.

๐Ÿ“ The rule: every BRIMParseError carries a code

Since lot W5-6 (backlog E-21) this is mandatory, for every raise BRIMParseError(...) in a plugin (backend/app/services/brim_providers/, shared readers such as _scalable.py included):

  • A code. details={"code": "<code>", ...}: a string constant, or a refusal's code (refused.code, as _scalable.py does for scalable_not_export). A raise without one would show the user an English sentence.
  • A context when the text has placeholders. A dict literal whose keys are exactly the placeholders of the user text ({"file": file_path.name}, {"columns": "date", "count": 1}). plugin_name is the one placeholder you never supply: the wizard adds it.
  • The English message stays the diagnostic. It goes to the logs and to the error_message of the failed file; the user never needs to read it. Write it for the developer who reads the log.
  • The user text lives in importWizard.parseRefusal.<code>, in the four languages. Add it with the i18n tool (the placeholders must be the same names in all four):

    ./dev.py i18n add "importWizard.parseRefusal.<code>" --en "โ€ฆ" --it "โ€ฆ" --fr "โ€ฆ" --es "โ€ฆ"
    
  • Other details keys stay. Keys such as "file" or "headers" keep sitting beside code and context; they are for diagnostics and are not sent to the wizard.

  • Do not lose the code on the way out. A coded raise inside a try needs except BRIMParseError: raise before any broad except Exception, otherwise the broad handler catches it and re-wraps it in a new BRIMParseError that has the code of the wrapper, not yours (or none).
try:
    rows = self._read_rows(file_path)  # may raise BRIMParseError(โ€ฆ, details={"code": "header_row_not_found", โ€ฆ})
    ...
except BRIMParseError:
    raise
except Exception as e:
    raise BRIMParseError(f"Error parsing file: {e}", details={"code": "file_unreadable", "context": {"error": str(e)}}) from e

Shared codes plugins reuse. Reuse these, with their translations, before adding a code of your own:

code context Cause
file_not_found file the path to parse does not exist (the plugin raises it in its except FileNotFoundError)
file_unreadable error the file could not be read or decoded
header_mismatch none the header row is not the one of this export
header_row_not_found layout, expected (both optional) the table's header row was not found in the file
missing_columns columns, count required columns are not in the header (count picks the plural)
no_valid_transactions none the file holds no valid transactions
file_empty none the file is empty
no_movement_rows none the file has no movement rows
no_header_row none the file has no header row

Plugin-specific codes are prefixed with the plugin's name, so they never collide: danske_* (Danske Bank: the cash, custody and combined layouts), degiro_* (degiro_csv_only, degiro_not_account_statement), intesa_layout_unknown and scalable_* (the Scalable refusals, plus scalable_not_export). The catalogue frontend/src/lib/i18n/importWizard/en.json, under parseRefusal, is the list that is always current.

The tests that guard it. ./dev.py test services brim-parse-error-codes (backend/test_scripts/test_services/test_brim_parse_error_codes.py) reads every plugin file statically (ast), so a new plugin or a new raise is caught without anybody remembering it, and names each offender by file:line:

  • every BRIMParseError has a code (and a context literal has string-constant keys);
  • a coded raise is not swallowed: a broad except Exception after it in the same try must be preceded by an except BRIMParseError that re-raises;
  • every code has a text in the four languages, with the same placeholders, and the placeholders are all supplied by the context of the raise sites of that code.

๐Ÿงฎ The gap-fix 409 is translated the same way

POST /brokers/import/gap-fix (below) refuses with 409 {code: "ledger_unreadable", message, reason: "values" | "currency_codes", params} when LibreFolio's side of the comparison cannot be computed (BRIMLedgerUnreadable, _set_error_detail in api/v1/brokers.py). The wizard shows it through resolveGapFixFailureMessage (parseFailure.ts) as importWizard.reportSet.gapFix.ledgerUnreadable.<reason> with params (values: count; currency_codes: codes and more). message stays English, the fallback when the key or the reason is missing; every other set error still shows its message.

๐Ÿงฐ Base-class helpers you should use

Defined on BRIMProvider โ€” call these instead of re-implementing:

Helper Use it in What it does
self._read_file_head(file_path, num_lines=15) can_parse Reads the first N lines trying TEXT_ENCODINGS (module constant in brim_provider.py) in order: UTF-8 (with or without BOM), then Windows-1252, then Latin-1, which always decodes
self.detect_csv_delimiter(file_path) parse Sniffs the delimiter (, ; \t) via csv.Sniffer with a char-count fallback; reads the head with the same TEXT_ENCODINGS fallback
self._open_text(file_path, newline=None) parse (and can_parse when you must read the header with csv) Drop-in replacement for open(...): an in-memory text stream decoded with TEXT_ENCODINGS; newline="" keeps line endings for csv
self._create_transaction(row_num, transactions, validation_issues, context, **tx_fields) parse Builds a TXCreateItem, appends on success, or records structured BRIMValidationIssues on ValidationError: each with the error's code and message, the field of a field's error (a packed business rule's sub-errors name none), and params by the batch's rule, shared in backend/app/utils/pydantic_errors.py (error_params(), loc_to_field(); Per-type rules): every error's plain context, a business rule's {type} as a field's {max_length: 500}, and null when the context is not plain data (a value_error) or there is none (W-B70). Never call TXCreateItem(...) directly.

๐Ÿ’ป Implementation Example

# backend/app/services/brim_providers/broker_mybroker/plugin.py

from pathlib import Path
from backend.app.services.brim.registry import BRIMProviderRegistry
from backend.app.services.brim.brim_provider import BRIMProvider
from backend.app.services.provider_registry import register_provider
from backend.app.schemas.brim import BRIMExtractedAssetInfo, BRIMParseOutput
from backend.app.schemas.transactions.transactions import TXCreateItem

@register_provider(BRIMProviderRegistry)
class MyBrokerProvider(BRIMProvider):

    @property
    def provider_code(self) -> str:
        return "broker_mybroker"

    @property
    def provider_name(self) -> str:
        return "My Broker (CSV)"

    @property
    def description(self) -> str:
        return "Import transactions from My Broker CSV exports"

    def can_parse(self, file_path: Path) -> bool:
        """Quick check: read first lines and look for known header."""
        content = self._read_file_head(file_path, num_lines=5)
        return "Date;Operation;ISIN;Amount" in content

    def parse(self, file_path: Path, broker_id: int) -> BRIMParseOutput:
        """Parse the CSV and return transactions in a BRIMParseOutput envelope."""
        transactions = []
        warnings = []
        extracted_assets = {}

        # ... your parsing logic ...

        return BRIMParseOutput(
            transactions=transactions,
            warnings=warnings,
            extracted_assets={
                fake_id: BRIMExtractedAssetInfo(
                    extracted_symbol=info["extracted_symbol"],
                    extracted_isin=info["extracted_isin"],
                    extracted_name=info["extracted_name"],
                )
                for fake_id, info in extracted_assets.items()
            }
        )

๐Ÿ” Auto-Discovery

A plugin is a folder named after its provider_code โ€” here brim_providers/broker_mybroker/:

backend/app/services/brim_providers/
โ”œโ”€โ”€ _brim_io.py, _brim_output.py, _scalable.py   โ† helpers shared by the plugins
โ”œโ”€โ”€ static/                                      โ† icons, served under /api/v1/uploads/plugin/brim/โ€ฆ
โ”œโ”€โ”€ sample_reports/                              โ† every plugin's sample exports (see below)
โ””โ”€โ”€ broker_mybroker/
    โ”œโ”€โ”€ __init__.py   โ† its docstring and nothing else: """BRIM import plugin ``broker_mybroker``: โ€ฆ"""
    โ”œโ”€โ”€ plugin.py     โ† the code above
    โ””โ”€โ”€ i18n/         โ† en.json, it.json, fr.json, es.json: its name and description (see below)

Restart the app: the BRIMProviderRegistry finds the folder, loads plugin.py as backend.app.services.brim_providers.broker_mybroker.plugin, and registers the class. The plugin will appear in the ImportPluginSelect dropdown, under its English provider_name until it has its catalogue keys. How discovery reads a folder, and the gate that keeps every plugin in one, are in Plugin Folder Layout.

  • The folder is the code. Name the folder exactly as provider_code returns: the UI reads the plugin's texts under brimPlugins.<code>, the fragment that holds them is the folder's, and ./dev.py test utils gate-plugin-i18n-ownership fails on a BRIM plugin whose code is not its folder's name. Keep the broker_ prefix: the encoding test above and the R13 test read only broker_*/ folders.
  • A name that starts with an underscore is not a plugin: auto_discover skips it (provider_registry.py). That is where code shared by several plugins lives, at the family root โ€” _brim_io.py and _brim_output.py for any plugin, _scalable.py for the two Scalable Capital plugins, whose plugin.py files hold only their properties (the string literals R13 reads among them) and calls to it. A helper only your plugin uses goes in its folder, as _*.py beside plugin.py.
  • Importing the plugin starts nothing: no thread, no process and no connection at module level, in plugin.py or in a helper it imports. Discovery also runs inside the BRIM parse pool's forkserver, and every parse worker is a fork of it (Preload and warm-up): a thread there is how deadlocks start, and the single-thread guard of ./dev.py test services brim-parse-pool fails when it finds one; a process or a connection would be inherited by every worker.
  • Import and patch the plugin's names from its plugin module, backend.app.services.brim_providers.broker_mybroker.plugin: the package re-exports nothing, so a test that patches the package fails (Importing and Patching a Plugin).

๐Ÿงญ Real-world reference plugins

Copy the structure of an existing, well-tested plugin rather than starting from scratch:

File Best example of
backend/app/services/brim_providers/broker_directa/plugin.py Equity/ETF, index-based columns, Italian types, ISIN grouping, tax/fee linking
backend/app/services/brim_providers/broker_generic_csv/plugin.py Column auto-detection, locale-aware number parsing
backend/app/services/brim_providers/broker_coinbase/plugin.py Crypto assets (symbol, no ISIN), staking as ADJUSTMENT, separate fee tx
backend/app/services/brim_providers/broker_revolut/plugin.py One plugin, two formats (invest + crypto) via header detection
backend/app/services/brim_providers/broker_intesa/plugin.py CSV/XLSX, two layouts in one plugin; patrimonio snapshot โ†’ liquidity DEPOSIT when present + per-holding ADJUSTMENT seed with per-unit cost_basis_override
backend/app/services/brim_providers/broker_credit_agricole/plugin.py Richest wizard integration and the second report set (securities + account exports, each in CSV or XLSX): 4-tier causale registry, evidence tables, info notices, blocking and splitting field todos โ€” read it before designing any field_todos. Also: rule M with most_rows, pairing on the value date, balances that open the cash and turn a hole into a correction, par-100 bond maturity split, succession rows as cashless ADJUSTMENT โ€” see Multi-report plugins
backend/app/services/brim_providers/broker_saxo/plugin.py Mixed trade/cash rows, verb-in-text events, localized verbs
backend/app/services/brim_providers/broker_danske_bank/plugin.py Report set (custody XLSX + cash CSV): roles, a pure combine with pairing, zones and truth points on segments, the parse of its combined file, Finnish notices โ€” see Multi-report plugins
backend/app/services/brim_providers/broker_degiro/plugin.py One layout in many languages: the Account Statement read by column position, the notice language picked from the header (_detect_language), linked FX_CONVERSION pairs (see Linked pairs), and its orders list (the Transactions export) claimed and answered with one notice instead of transactions (the rule's extension in Flow)

๐Ÿ“ฅ Canonical imports

from __future__ import annotations

import csv
from datetime import date as date_type
from datetime import datetime
from decimal import Decimal, InvalidOperation
from pathlib import Path
from typing import Dict, List, Optional

import structlog

from backend.app.db.models import TransactionType
from backend.app.schemas.brim import FAKE_ASSET_ID_BASE, BRIMAssetNotice, BRIMExtractedAssetInfo, BRIMParseOutput, BRIMValidationIssue
from backend.app.schemas.common import Currency
from backend.app.schemas.transactions.transactions import TXCreateItem
from backend.app.services.brim.registry import BRIMProviderRegistry
from backend.app.services.brim.brim_provider import BRIMParseError, BRIMProvider
from backend.app.services.provider_registry import register_provider

logger = structlog.get_logger(__name__)

โž•โž– Sign conventions (enforced by the test suite)

TransactionType (in backend/app/db/models.py) fixes the sign of quantity and cash amount. test_all_transactions_are_schema_valid fails the plugin if any emitted transaction breaks these rules, so flip source signs as needed:

Type quantity cash amount asset
BUY > 0 < 0 required
SELL < 0 > 0 required
DIVIDEND = 0 > 0 required
INTEREST = 0 > 0 optional
DEPOSIT = 0 > 0 none
WITHDRAWAL = 0 < 0 none
FEE = 0 < 0 optional
TAX = 0 < 0 optional
ADJUSTMENT / TRANSFER +/- must be None optional
  • DIVIDEND/INTEREST carry no quantity โ€” set quantity=0 and discard any token amount with a warning (see coinbase).
  • Crypto โ€” the fiat leg decides the type. If a row settles in fiat (your account currency) use a cash type: buy crypto with fiat โ†’ BUY, sell for fiat โ†’ SELL, a reward paid out in fiat โ†’ INTEREST, a stock payout โ†’ DIVIDEND, plain cash in/out โ†’ DEPOSIT/WITHDRAWAL. If a row has no fiat leg โ€” staking/airdrop paid in coins, a cryptoโ†”crypto trade, an on-chain transfer in/out, or a crypto-denominated fee โ€” model it as a cashless ADJUSTMENT (quantity +/-, cash=None) so the position still updates. This is what keeps every crypto row schema-valid, because ADJUSTMENT forbids a cash amount. Reference: broker_bitvavo/plugin.py, broker_cointracking/plugin.py, broker_delta/plugin.py, broker_cryptocom/plugin.py (note cryptocom emits INTEREST for fiat-valued rewards but a cashless ADJUSTMENT for coin-only rewards).
  • Prefer skipping a row with a notice (RowSkips, Rows left out) over emitting a schema-invalid transaction.

Warning language follows the input format

A plugin's user-facing warnings (and any BRIMAssetNotice.reason) should be written in the language of the export it parses. For a single-nation broker whose report is published in only one language โ€” e.g. Crรฉdit Agricole, Directa and Intesa Sanpaolo (Italian), Danske Bank (Finnish) โ€” emit the warnings in that language so they match the report the user is reading. Fineco is still an exception: it reads only the Italian export, but its one plain warning (No valid transactions found in file) is in English; its rows left out go to the two shared notices of RowSkips (below), and its two info notes, fineco_not_yet_settled and fineco_incomplete_rows, are in Italian. The two shared notices of RowSkips are the exception to the rule: the wizard words them in the user's language, and only the Reason column of their evidence is the plugin's text. When a broker ships differently localized export layouts (a UK vs. IT Fineco file, a non-Italian Crรฉdit Agricole entity), detect the format and emit each variant's warnings in its own language. Code, comments and docstrings stay in English.

A broker can also ship one layout in many languages: DEGIRO's Account Statement has the same twelve columns in every language, so the layout cannot tell the language, but the header can. broker_degiro/plugin.py (_detect_language) counts, for each language, how many of the file's named columns are among that language's header names (_HEADER_WORDS; ISIN and FX, the same everywhere, do not count) and picks the best language only with at least three matches and no tie (_MIN_LANGUAGE_MATCHES). A word two languages share decides nothing on its own โ€” Datum is Dutch and German โ€” and any other header gets English. The language chooses the messages only (_MESSAGES: English, Dutch, German, French, Spanish): rows are classified with the words of every language, because one DEGIRO file can mix them.

The user reads the file's language only if the notice code has no importWizard.brimNotice.<code> key. The wizard resolves a notice with resolveBrimNoticeMessage (frontend/src/lib/features/transactions/resolveBrimNotice.ts): a key, when it exists, replaces the plugin's message with the text in the UI language; without one, the plugin's message is shown as written. The degiro_* codes have no key on purpose, and test_no_i18n_key_overrides_the_language_of_the_file (test_brim_degiro.py) fails if one is added.

Field todos follow the same rule, keyed by their reason_code under the same namespace and resolved by resolveBrimTodoMessage (same file), wherever the wizard and the bulk editor list them. A todo is worth a key only when its message has no file language to honour, like the Generic CSV's corporate_action; every other todo keeps the file's language on purpose.

The comment under a notice's evidence table follows the notices' contract too, in its own namespace: importWizard.brimEvidence.<code> โ€” the notice's code, with its context as values โ€” replaces the evidence's comment when the key exists (resolveBrimEvidenceComment, in resolveBrimNotice.ts too, called by BrimNoticeList.svelte). There is one key per notice, so a notice with several commented tables shows the same text under each; and a key only replaces: a table without a comment stays without. The caption needs no key: an empty title gets the wizard's own, importWizard.evidenceRowsTitle (Source rows, Righe del file), where the table folds behind its caption โ€” the confirmation modal; the parse-detail modal shows such a table without a caption. The Scalable plugins write in English, so they leave title empty and give each of their twelve notice codes both keys: the user reads the whole notice โ€” message, caption and comment โ€” in the UI language. TestWhatTheWizardWords (test_brim_scalable.py, ./dev.py test external brim-scalable) holds them to it: both keys for every code in the four catalogues, and no title on the samples' evidence tables. A plugin that writes in its file's language gives its evidence no key either.

cost_basis_override is PER-UNIT, never a total

When a plugin freezes an inherited cost basis (WAC) on a TRANSFER/ADJUSTMENT seed โ€” e.g. an opening patrimonio snapshot, or a TRANSFER_IN โ€” the value you put in cost_basis_override (and its cost_basis_currency) must be the cost per single unit, not the position's total countervalue. The portfolio engine and the lot analysis both multiply it by quantity to reconstruct the total cost basis, so a total slipped in here is silently multiplied again and blows the cost basis up by a factor of quantity.

If your source only reports a total (e.g. Intesa's "Controvalore di carico fiscale โ‚ฌ"), divide it by the quantity first, and round the quotient to the column (see Decimal places):

unit_cost = round_computed_value(total_cost / qty, Transaction, "cost_basis_override")
cost_basis_override = Currency(code="EUR", amount=unit_cost)

Bond nominal note: for bonds quantity is the face value (e.g. 50000) and the seed per-unit cost lands near 1.0 โ€” that is correct, the engine handles the /100 quote scaling elsewhere. Canonical example: broker_intesa/plugin.py (patrimonio seed).

A plugin sets a cost basis on these incoming seeds only: on any other row โ€” a BUY, a cash row, a TRANSFER's giving leg โ€” _create_transaction() refuses the row, a costBasisNotAllowed validation issue instead of a transaction (rule 12b, W-B72, or rule 12's costBasisModeIncompatible when the row names a cost_basis_mode; an ADJUSTMENT taking units out is refused the same way, since it has no value: see Per-type rules).

Matured-bond redemption: close at par, surplus โ†’ INTEREST

A bond is redeemed at par (100) at maturity. When the bank credits more than par (e.g. a BTP Italia premio fedeltร  or a FOI inflation revaluation), that surplus is reddito di capitale โ€” the same nature as a coupon โ€” and must be booked as a separate INTEREST leg, not folded into the sale price. Folding it in would inflate the realised gain and leave a residual position when ctv/price โ‰  the true nominal.

Working assumption (document it on the plugin's own page): a maturity/redemption row is a bond quoted at par 100, so anything above 100 is interest. A plugin whose maturity causale is bond-specific (e.g. Crรฉdit Agricole's TITOLI SCADUTI) can apply this unconditionally; a plugin whose causale is generic (e.g. Fineco's Rimborso, which also covers equities) must still gate on io.looks_like_bond(name). Generalise only when a real counter-example appears.

The nominal to redeem comes from the held position (the BUY / succession / seed rows), never from the maturity row itself (whose quantity is often 0). Use the shared helper:

from backend.app.services.brim_providers import _brim_io as io

if ctv is not None:                       # (+ io.looks_like_bond(name) if the causale is generic)
    model = io.model_bond_maturity(ctv=ctv, price=price, held_qty=held or None)
    sell_qty  = -model.nominal            # SELL closes the nominal at par, e.g. -30000
    sell_cash = model.principal_cash      # e.g. 30000.00 (nominal ร— par/100)
    if model.surplus_cash > 0:            # surplus over par โ†’ INTEREST income
        emit_interest(cash=model.surplus_cash)   # e.g. 105.00
  • held_qty > 0 โ†’ source == "position": exact close, no verify flag. This is the normal case โ€” the nominal lives in the position, so there is nothing for the user to check. If the file is not chronologically ordered, pre-compute the position in a first pass (see broker_credit_agricole/plugin.py).
  • no position โ†’ source == "derived": the nominal is inferred from ctv/price (a best effort, imprecise because the quoted price embeds the premium โ€” e.g. 30105/100.40 = 29985 vs a true 30000). This happens on a partial download (a "last 3 months" export where the buy legs are missing). Attach a warning BRIMFieldTodo (reason_code="derived_quantity") so the user verifies the nominal โ€” the plugin sees only the file, so it cannot confirm the close against the position already stored in LibreFolio (that reconciliation happens in the portfolio engine at compute time).
  • Cash-only maturity in a bank/account export โ†’ nominal from a same-day companion row (still in-file). Some exports carry the redemption only as a cash row with no securities position and no reliable price (e.g. Crรฉdit Agricole's account TITOLI SCADUTI O ESTRATTI, whose truncated securities export had already dropped the opening BUY). Such a row has just the total cash, so it cannot split par vs premium on its own. Recover the nominal from another row in the same file: the final coupon paid on the same date states the security's ISIN + NOMINALE. Match the two by security code in a first pass, then feed that nominal as held_qty to model_bond_maturity (source == "position"). This never touches the database โ€” a plugin only ever reads its own file. If no same-day companion row identifies the security, book the whole redemption as a SELL for the full cash (everything as sold, no premium split) plus a warning โ€” never a plain cash DEPOSIT, so the proceeds reduce a position instead of inflating paid-in capital (consistent with the securities-export fallback). With no held position and no reported price, assume a par (100) redemption so the derived nominal equals the cash (SELL with quantity < 0, flagged for the user to verify). See broker_credit_agricole/plugin.py (_parse_account_movements, income_identity_by_date, _ACCT_MATURITY_CAUSALI).
  • A price at or below par yields surplus_cash == 0 โ†’ emit a single plain SELL (pass-through); don't invent negative income.
  • Keep the plugin's usual cash model: if BUYs get a balancing DEPOSIT and SELLs a WITHDRAWAL, the par principal nets to zero and only the INTEREST surplus adds cash.

Reference plugins: broker_credit_agricole/plugin.py (TITOLI SCADUTI in the securities export with the position pre-computed from succession legs, and the account cash-only TITOLI SCADUTI O ESTRATTI with the nominal recovered from the same-day coupon) and broker_fineco/plugin.py (Rimborso above par, nominal from the row).

๐Ÿ”— Linked pairs from a plugin

A currency conversion is two transactions that only exist together: the cash that leaves one currency and the cash that arrives in another. A plugin emits both legs as FX_CONVERSION and gives them the same link_uuid; the batch turns the shared id into reciprocal related_transaction_ids and never stores the id itself (see Transaction Service โ†’ Linked Pairs). broker_degiro/plugin.py is the first plugin to emit pairs (_pair_fx_legs, _fx_pair): Swissquote lists its conversion legs in the shared notice rows_not_recognized (it does not import them yet), and the Generic CSV refuses the paired types.

What the batch accepts โ€” resolve_create_links in batch_creates.py, with _validate_linked_pair and _validate_pair_description_tags in validation.py (all in backend/app/services/transactions/):

  • exactly two creates per link_uuid: a leg alone, or a third one, is linkUuidPairCount;
  • the same type on both legs (pairTypeMismatch);
  • two distinct brokers for a TRANSFER or a CASH_TRANSFER (pairSameBroker); an FX_CONVERSION must stay on one (pairBrokerMismatch). Every row a plugin emits carries the one target broker_id that parse() receives, so a transfer pair saved as the plugin emitted it is refused at this check, while an FX_CONVERSION pair saved the same way passes it;
  • for a TRANSFER, one asset on both legs (pairAssetMismatch) and exactly opposite quantities (pairQuantityMismatch, compared as Decimal, with no tolerance): what leaves one broker arrives at the other, so both legs carry the same quantity, rounded alike, with opposite signs;
  • an identical description (pairDescriptionMismatch) and identical tags (pairTagsMismatch). Brokers word the two legs differently, so build one description for both: DEGIRO keeps the text when both legs read the same, and joins the two otherwise (FX Credit / FX Debit);
  • each leg valid on its own: an FX_CONVERSION has quantity = 0, a non-zero cash amount in its own currency, and no asset.

The link_uuid is deterministic. test_parse_is_idempotent compares two parses of each sample, and test_sample_parses_identically_when_saved_as_windows_1252 compares a non-ASCII sample with its Windows-1252 copy: a uuid4() fails the first, an id hashed from the file's bytes fails the second. DEGIRO derives it with uuid5 from a SHA-256 of the decoded rows of the whole file and the line numbers of the two legs, so the same statement always gives the same ids, whatever its encoding, and two statements that differ give different ones.

Recognise the legs without the rate. No forex system still holds: the report's exchange rate neither converts nor matches anything. DEGIRO only asks whether a row's FX cell is filled โ€” that makes the row a leg โ€” and finds its partner by structure: the same order (same Order Id, or the same date and time when there is none), another currency, the opposite sign, and the adjacent row when several qualify. A leg whose partner is missing or ambiguous is listed in a notice (degiro_unpaired_fx) and not imported: never a guessed pair, and never a lone leg, which the batch would reject.

In the import wizard a pair is one row of the review step: the leg that pays (the negative amount; the first one the plugin emitted when both legs have the same sign), with the leg that receives shown as its To and the rate the two amounts imply โ€” computed from the amounts, not read from the report. The receiving leg is not a row of its own (pairs.hidden, step4Rows): the row's cash cell (data-testid="import-tx-pair-cash") holds a From line and a To line in the bulk editor's markup (renderFromToHtml mirrors renderDualHtml of TransactionBulkModal.svelte), then a chip such as USD โ†’ EUR @ 0.8554 (renderImpliedRateHtml: |to| / |from| through computeFxConversionInfo). Selecting the row selects both legs (setPairSelected: its tick shows the pair as selected only when both legs are, and select all, deselect all and select visible move both legs too), only complete pairs reach the bulk editor, and each pair reaches it with a fresh link_uuid, the same for both legs (frontend/src/lib/features/transactions/import/importPairs.ts, and pairCellHtml.ts beside it for the cell): the plugin's ids repeat whenever the same statement is parsed again, so two uploads of one statement would share them. The counters and the Import {n} transactions button count transactions, not rows, so a pair counts as two (step4SelectedCount and step4TotalCount, over step4LegRows): the English sample shows 17 transactions in 15 rows.

๐Ÿ“ก Talking to the import wizard

A plugin does not only produce transactions. It produces what it could not do, and that is what drives the wizard's UI. There are four output channels, and picking the right one is the single most important design decision in a plugin:

Channel Field on BRIMParseOutput The plugin is saying Where it shows
Transactions transactions "I read this row" Review table
Validation issues validation_issues "I read this row and the schema refused it" Parse-detail modal, red
Notices warnings "Something about the file you should know", the rows it left out included Parse-detail modal + confirmation modal
Field todos field_todos "I booked this row but a specific field is a guess" Correction step, before the duplicate check

A notice is not a todo

The line is not severity, it is actionability. If the user can fix it on one row, it is a BRIMFieldTodo; if it is a statement about the file as a whole, it is a BRIMNotice. Putting a fixable problem in warnings sends the user to a modal that offers no way to fix it, and showing the same finding in both channels teaches them to skip both. Crรฉdit Agricole has a test asserting a given finding appears in exactly one.

๐Ÿ”” BRIMNotice โ€” something about the file

warnings.append(
    BRIMNotice(
        severity="info",                       # "info" (blue) | "warning" (amber)
        code="ca_unknown_causale",             # machine-readable, i18n-stable
        message="12 righe con 3 causali non previste โ€ฆ",   # report's language
        context={"causali": [...], "row_count": 12},
        evidence=[...],                        # see below
    )
)
  • severity="info" โ€” the plugin did something correct and non-obvious: explain it. An unknown transaction code booked by sign, a succession row turned into a cashless ADJUSTMENT, rows left for the next import because they have not settled yet (the rule The operation date, and only settled rows, under Import philosophy). Blue, still surfaced in the confirmation modal.
  • severity="warning" โ€” the plugin had to fall back or guess: look at it. Also a row left out because the plugin does not know it, with context.report_issue (the rule Known rows quietly, unknown rows loudly, under Import philosophy; the helper RowSkips writes these notices, see Rows left out). A row the plugin knows it may leave out gets no notice at all.
  • A bare str still works (coerced to severity="warning", code="legacy"), but new code should never use it: the code is what lets the frontend and the tests address the notice.

๐Ÿ”Ž BRIMEvidence โ€” show the row, do not just describe it

Every notice and every todo can carry evidence tables. This is what turns "row 282 looks bundled" into something the user can check without opening the file:

BRIMEvidence(
    title="Riga di acquisto",                 # short caption
    headers=["Data Op.", "Causale", "Descrizione", "Importo"],
    rows=[["28/07/2025", "COMPRAVENDITA โ€ฆ", "NOTA INF. ACQ. TIT:BTP โ€ฆ", "-46603.73"]],
    row_numbers=[282],                        # 1-based source line, per row
    comment="L'importo di questa riga รจ un totale: comprende โ€ฆ",
)
  • row_numbers is not decoration. It renders a "row N in the file" button that opens the file preview scrolled to that line, so the user sees the neighbours too. Omit it and the evidence becomes an unverifiable claim.
  • Attach evidence at the moment the plugin gives up โ€” that is the only moment the source row is still in hand. Re-reading the preview later costs a fetch and misaligns indexes if the preview paginates.
  • comment is the reasoning, the message is the headline. The collapsed row in the correction list is read while scrolling twenty others: keep it to one line. Everything the plugin looked at, found and concluded belongs in comment, which is read only by someone who opened the row.
  • Several tables are allowed: Crรฉdit Agricole shows the purchase row and the coupon row that supplied the nominal, each with its own comment.
  • title may be empty. The field is required, but title="" lets the wizard show its own caption, translated (Source rows), where the table folds behind it: the Scalable plugins, whose notices are translated, leave it empty rather than write an English caption. The comment of a notice's table can be translated by code too โ€” both are explained in the tip Warning language follows the input format, under Sign conventions.

๐Ÿšฎ Rows left out: RowSkips

The helper of the rule Known rows quietly, unknown rows loudly (lot W-B86), in the section ROWS LEFT OUT of backend/app/services/brim_providers/_brim_io.py (the plugins import that module as io). A plugin collects the rows it cannot import as they come, and returns the notices once at the end:

from backend.app.services.brim_providers import _brim_io as io

skips = io.RowSkips(header_cells)          # or io.RowSkips() and skips.set_headers(reader.fieldnames) later

for line, row in enumerate(rows, start=2):
    if not row_date:
        skips.unreadable(line, row, "invalid date")                    # cannot read it
        continue
    if tx_type is None:
        skips.unknown(line, row, f"unknown type: '{raw_type}'")        # does not recognise it, or does not import it yet
        continue
    if raw_type in KNOWN_NO_EFFECT:                                     # known and safe: nothing is collected
        quiet += 1
        continue
    ...

return BRIMParseOutput(transactions=transactions, warnings=[*warnings, *skips.notices()], ...)
Method The row is Notice code
unknown(line, row, reason) a type, status or description the plugin does not recognise, or recognises but does not import yet (a split, a currency conversion, a fee column) rows_not_recognized
unreadable(line, row, reason) one whose date, amount, currency or required field cannot be read rows_unreadable
  • row is a list of cells or a csv.DictReader row (read in the order of the headers); line is its 1-based line in the file; reason is the short text of the last column.
  • notices() returns at most two notices, rows_not_recognized first: each is a warning, with context {"count": n, "report_issue": True} and one BRIMEvidence (the file's header, a last column Reason, the rows, their line numbers). A row shorter than the header is padded; a longer one gets numbered headers.
  • The wizard words them once for every plugin: importWizard.brimNotice.<code> and importWizard.brimEvidence.<code>, in the four catalogues (frontend/src/lib/i18n/importWizard/), with {count} as an ICU variable. The message in RowSkips is the English fallback.
  • A known row that is safe to drop is not collected: count it and write one log line at the end (logger.info(โ€ฆ, count=โ€ฆ)), as Revolut and Trading 212 do for Transfer, and Scalable for a cancelled order.
  • A plugin that already has a notice of its own for a case keeps it, with context.report_issue = True (DEGIRO's six degiro_* notices, Crรฉdit Agricole's ca_securities_unknown_causale, ca_unknown_causale and ca_account_invalid_amount, Scalable's scalable_unreadable_rows and scalable_not_imported).
  • Contract test: ./dev.py test external brim-row-skips (backend/test_scripts/test_external/test_brim_row_skips.py). Every registered plugin must be in exactly one of two sets: the plugins that follow the rule (and obey it), or the pending ones, which W-B86 does not cover yet. A new plugin lands in one of them, or the test fails.

๐Ÿงฉ BRIMFieldTodo โ€” one field, one question, one screen

BRIMFieldTodo(
    tx_index=len(transactions) - 1,     # index into transactions[]
    field="asset_id",                   # TXCreateItem field that needs input
    severity="blocker",                 # "blocker" | "warning"
    reason_code="ca_account_trade_unresolved",
    message="Riga 282: acquisto di BTP โ€ฆ โ€” l'importo potrebbe raggruppare piรน voci.",
    context={...},                      # see the contract below
    evidence=[source_row_evidence(row, offset, comment="โ€ฆ")],
)

field and severity together decide where the row lands, so they are not free-form:

severity field Wizard behaviour
blocker one of type, date, quantity, asset_id, cash, cash.amount, cash.code Correction step, "trades" panel. The step cannot be passed until every such row is settled.
blocker anything else (e.g. cost_basis_override) Carried on the transaction, but not shown in the correction step โ€” that screen cannot fix it.
warning asset_id Correction step, "charges" panel โ€” a fee or an income with no instrument.
warning any, with context.split_hint Correction step, "split" panel โ€” see below.
warning asset_event_id, reason_code="possible_split" Not in the correction step: it is carried to the Bulk editor, which marks the row ยซmay be a splitยป and answers it with a SPLIT event (_brim_io.possible_split_todo, Revolut and Trading 212: Possible splits).

A row is filed by its worst todo: a blocker that also carries split_hint lands in the trades panel, and its split zone appears there as soon as the user types the row into a BUY/SELL. That is deliberate โ€” an unresolved trade is a trade too, only a less complete one, and its total is bundled for exactly the same reason.

Why blockers must be duplicate-relevant fields

The correction step runs before the duplicate comparison, and only for fields the comparison actually reads (DUP_RELEVANT_FIELDS in ImportWizardModal.svelte). A purchase the plugin could only record as a cash withdrawal gets compared against cash withdrawals โ€” a real duplicate is missed, an imaginary one invented. Correcting after the comparison cannot repair that; correcting before it can.

โœ‚๏ธ A broker's split row: the possible split

A broker that reports a multiplicative split as a row (Revolut STOCK SPLIT, Trading 212 Stock split close + open) does not say how the position is meant to be read. The plugin imports the shares added as an incoming ADJUSTMENT with cost_basis_override of 0 (the new shares of a split cost nothing; a positive adjustment requires a cost) and sends _brim_io.possible_split_todo(...): a BRIMFieldTodo on asset_event_id, severity="warning", reason_code="possible_split", with context {ticker, added_quantity, isin?} and the source row as evidence. Without an event, those shares would dilute the average cost like a gift; linked to a SPLIT event, financial_math/splits.py makes the cost follow the old shares. A plugin never creates the event: the user does, in the Bulk editor. A split with no readable positive quantity (a reverse split), equal shares or no partner row is not imported: skips.unknown(...), the notice with the link to open an issue. Pair rows by a key the file gives (the ISIN) and the time, never by position.

๐Ÿ—๏ธ context keys the frontend understands

context is a free Dict[str, Any], but a handful of keys are a contract with the wizard. Those of a BRIMFieldTodo drive the correction step, and all of them are currently produced by broker_credit_agricole/plugin.py, which is the reference implementation:

Key Type Effect in the wizard
split_hint str (e.g. "trade_charges") Presence alone turns the row into a split candidate. The split zone then appears only if the row's type is BUY or SELL โ€” the plugin's own or the one the user just picked.
split_suggestions List[str] Bullet list of what the plugin found elsewhere in the file about this row.
compare_nominal bool Shows the "row amount vs face value" comparison. Set it False on sales: proceeds have no reason to resemble the face value, and showing them side by side invites the user to read a gap that means nothing.
nominale str/Decimal The face value shown in that comparison. Requires compare_nominal: True.
nominale_row int Source line of the row the nominal was read from; renders as a link into the file preview.
cash, currency str The row total, used when the transaction itself cannot supply it (unresolved rows).
row int Source line number โ€” always include it.

A BRIMNotice has one key of its own, read by the notice list (BrimNoticeList.svelte):

Key Type Effect in the wizard
report_issue bool Only the boolean True counts. The wizard then shows, under the notice's message, the link open an issue on GitHub (importWizard.fixStep.reportIssueLink, the same words as the correction step's link) to https://github.com/Librefolio/LibreFolio/issues/new, opened in a new tab (data-testid="brim-notice-report-issue"), on every instance, self-hosted or hosted. Two uses. A row the plugin does not know, left out: a warning, under the rule Known rows quietly, unknown rows loudly (Import philosophy) โ€” the two shared notices of RowSkips (Rows left out) always carry it, and so do Danske Bank's excluded_status, which asks the user to open an issue if the rows are real movements, and the notices of DEGIRO, Crรฉdit Agricole and Scalable listed in BRIM Architecture. A plugin in beta that suspects its own reading of rows it imported: an info note โ€” Fineco's fineco_incomplete_rows asks the user to check the rows and, if the file is right, to open an issue.

Everything else in context is free: it is passed through untouched and is useful for tests and for i18n interpolation.

Fire the todo on the shape of the row, not on a contradiction you happened to spot

Crรฉdit Agricole first raised the split todo only when the cash disagreed with a face value recovered from a coupon in the same file. A user importing period by period โ€” the purchase this month, the coupons next year โ€” never saw it, because by then the purchase was already imported. The trigger had to become "this is a trade, and this layout never separates price from charges": fired on every resolved BUY/SELL.

That inversion is only acceptable because the noise was measured before accepting it: on the real files it went from 2 to 3 warnings over 507 transactions, because account trades are rare in this layout. Measure yours the same way; a flag on every row is a flag on no row.

โœ‚๏ธ The split zone (split_hint)

When one source row bundles several economic facts โ€” a bond bought on the secondary market pays price, accrued interest and commissions in a single debit โ€” the plugin must not invent the breakdown. It flags the row, and the user, who has the contract note, types the charges. The wizard writes N extra FEE/TAX legs and leaves the rest on the trade, so the legs always re-sum to the source row.

The plugin's only job is the flag plus honest suggestions. The most valuable suggestion is the one that prevents a mistake: if the file already books a commission row within a few days, splitting it out of the trade total again would count it twice.

BRIMFieldTodo(
    ...,
    field="cash",
    severity="warning",
    context={
        "row": offset,
        "split_hint": "trade_charges",
        "compare_nominal": is_buy,
        "nominale": str(nominal), "nominale_row": nominal_row,
        "split_suggestions": [
            "Il file registra giร  una riga di spese il 30/07/2025 (โˆ’40,00 โ‚ฌ): se la scorpori "
            "di nuovo da questo totale la conti due volte.",
            "รˆ un'obbligazione: se non l'hai comprata all'emissione, parte dell'importo รจ "
            "rateo cedolare.",
        ],
    },
)

Accrued interest is a FEE, never an INTEREST

schemas/transactions/transactions.py rule 11 requires cash > 0 for INTEREST, and accrued interest paid on a purchase is money leaving. FEE is also the semantically right answer, not a workaround: accrued interest is not part of the security's cost โ€” it comes back with the first gross coupon โ€” and FEE is exactly the type the FIFO engine keeps out of the cost basis (_LOT_AFFECTING_TYPES in portfolio_service.py).

๐Ÿ†” Fake asset IDs

Asset-linked transactions reference a fake high positive asset id at parse time; the core maps it to a real asset later. Allocate them yourself, grouping rows of the same asset:

next_fake_id = FAKE_ASSET_ID_BASE          # 2**31 - 1 high positive sentinel
asset_to_fake_id: dict[str, int] = {}
extracted_assets_raw: dict[int, dict] = {}

asset_key = isin or ticker                 # stable key per asset
if asset_key in asset_to_fake_id:
    asset_id = asset_to_fake_id[asset_key]
else:
    asset_id = next_fake_id
    asset_to_fake_id[asset_key] = asset_id
    extracted_assets_raw[asset_id] = {
        "extracted_symbol": ticker or None,
        "extracted_isin": isin or None,     # None for crypto
        "extracted_name": name or None,
    }
    next_fake_id -= 1

Return them as BRIMExtractedAssetInfo in BRIMParseOutput.extracted_assets. Every tx.asset_id must appear as a key (checked by test_extracted_assets_consistent_with_transactions).

โš ๏ธ Per-asset import notices

A BRIMAssetNotice is not about a transaction, it is about the instrument: something the user needs to know at the moment they create it. Attach them to the BRIMExtractedAssetInfo:

extracted_assets[asset_id].notices.append(
    BRIMAssetNotice(
        kind=io.MATURITY_NOTICE_KIND,          # "maturity_suspected"
        reason="Rilevata almeno una transazione di scadenza/rimborso.",
        transaction_indexes=[tx_index],
    )
)

BRIMAssetMapping.notices carries them to the frontend; the asset-create modal groups them by kind into amber banners and de-duplicates identical reason texts, so the same notice raised by three files collapses into one bullet. Informational only โ€” they never change import behaviour.

The kind needs an i18n label under assets.modal.importNotices.kind.*; an unknown kind falls back to the generic label, so a new category degrades instead of breaking.

๐Ÿชฆ The maturity advisory (shared helper)

The one category shipped today. It is the only warning telling the user that a security is delisted and that no price provider will ever quote it โ€” without it, the asset is created silently and then fails to price with no explanation.

The responsibilities are deliberately split:

  • _brim_io.py provides schema-neutral detection/input support. detect_maturity_hits identifies affected asset IDs and their transaction indexes without importing BRIM schema types.
  • _brim_output.py provides schema-aware output construction and owns the canonical attach_maturity_notices helper.

When building a plugin, use that output helper rather than constructing the notice directly:

from backend.app.services.brim_providers._brim_output import attach_maturity_notices

attach_maturity_notices(transactions, extracted_assets, reason="provider-specific reason")

detect_maturity_hits scans description with looks_like_maturity (substrings scadut, scaden, rimbors, estinzion, redemption, matured, maturity) and skips rows with no asset, which is what keeps a cash refund (SCT:RIMBORSO on a bank transfer, a tax rebate) from ever labelling an instrument as expired. The human-readable reason stays provider-local because each export needs wording grounded in its own terminology.

Call it from every layout your plugin parses

Real bug, found in beta: Crรฉdit Agricole wired the scan into its securities-export branch only. A bond redeemed in the account statement got its SELL but no notice โ€” and the account file is the one that spans years, so that is where redemptions normally appear. Invoke attach_maturity_notices from every _parse_* layout branch; adding another supported layout means adding the call there too.

Crรฉdit Agricole and Intesa are the concrete references: see broker_credit_agricole/plugin.py and broker_intesa/plugin.py. Add or generalize a shared output helper only when a real second consumer needs the same semantics; otherwise keep the logic provider-local.

๐Ÿšช Opening-date gate

Do not implement an opening-date filter in the backend plugin. The shipped gate lives in the Svelte import wizard and uses the local status key before_opening (not a backend enum). The exact comparison is strict:

return info !== null && txDate !== '' && txDate < info.openedAt;

Rows before the broker opening date are deselected and non-importable; rows on the opening day remain importable. The wizard offers Edit broker date and re-checks after broker data refresh.

Do not re-check the batch's date range either: the import confirms through the transaction batch, which checks every row's date (check_dates(), decision N74): no later than tomorrow by the server's calendar (latest_range_end(); there is no first date since D-P4, the wizard asks a confirmation before a date earlier than 1900). A row past it gets dateOutOfRange on date and the batch is not saved (see Transaction dates). For the rows it keeps, a plugin emits the operation date the report states. A row whose value or settlement date is after the server's today (server_today()) โ€” or whose operation date is, when the report has no other date โ€” is provisional: leave it out, with an info notice, under the rule The operation date, and only settled rows (Import philosophy), rather than emit it. The batch would refuse anything after tomorrow with dateOutOfRange anyway.

๐Ÿ”€ One plugin, several export formats

A single broker often ships multiple export layouts (e.g. Revolut invest vs crypto). Do not create two plugins unless the files are genuinely indistinguishable and the user must choose. Instead:

  1. Make can_parse return True for any owned layout (detect by header).
  2. In parse, read the header row, pick the variant, and dispatch to a private helper (_parse_invest(...) / _parse_crypto(...)), keeping one BRIMProvider class.
  3. Register one sample per variant in sample_reports/ and list every filename substring in test_file_patterns:
@property
def test_file_patterns(self) -> List[str]:
    return ["revolut-invest", "revolut-crypto"]

The test suite loops over every matching sample, so each variant is exercised.

๐ŸŽ›๏ธ A plugin with a setting

A plugin may declare a setting the user (or the admin) can change in Settings โ€บ Plugins โ€บ Broker imports: the first one is date_order of the generic CSV plugin, the order of day and month of a date such as 03/04/2025. The contract, the wiring and the staleness rule are written once, in BRIM Architecture โ†’ A field in a BRIM plugin; how to declare a field is in Plugin Settings โ†’ Declaring Fields. Do not copy them here. Four rules for the author of the plugin:

  1. The default is the old behaviour. A plugin that already imports files must give the same result as before the field existed when nobody configured it.
  2. Read it inside parse() only, as self.settings.get("key", <default>), never in __init__, can_parse() or combine(): those run without the values.
  3. The parse stores the fingerprint: nothing to write in the plugin. parse_file_offloaded() takes the values the parse route resolved, and the route saves their fingerprint with the result, so a file parsed under another value shows the ยซparsed againยป banner. Do not keep a copy of the value in the result.
  4. Translations under pluginSettings.brim.<code>โ€ฆ (label, description and option keys, four languages, in the plugin's own i18n/ fragment). The ownership gate does not check them yet.

๐Ÿงบ Multi-report plugins (report sets)

Some banks split one account across several exports that only make sense together. Danske Bank Finland exports its equity savings account as a custody XLSX (trades with quantity and price, but no deposits and no balance) and a cash CSV (every cash movement with the running balance, but no quantities). A report-set plugin declares these exports as roles; the files uploaded together for one broker and recognised by the plugin form a set; the plugin combines them into one combined file; and the import wizard parses that file like any other.

Every default of the contract keeps a plugin single-file, so the existing plugins do not change. There are two report-set plugins: Danske Bank (backend/app/services/brim_providers/broker_danske_bank/plugin.py, the first) and Crรฉdit Agricole (backend/app/services/brim_providers/broker_credit_agricole/plugin.py, a securities export and an account export, version 2.0.0). They share a core (below) and differ in how they place rows and truth points; see Where Crรฉdit Agricole differs. The framework lives in backend/app/services/brim/brim_report_sets.py (members, preview, combine, history start) and backend/app/services/brim/brim_gap_fix.py (the corrections), the schemas in backend/app/schemas/brim.py, and the wizard side in Import Wizard โ†’ Report sets.

sequenceDiagram
    participant W as Import wizard
    participant A as API brokers/import
    participant P as Plugin
    participant C as Core and database
    W->>A: POST upload with batch_id, one file per call
    A->>P: can_parse
    W->>A: POST sets/preview
    A->>P: detect_role, describe_member, describe_set
    A->>C: history start of the broker
    A-->>W: roles, missing exports, segments, gaps, warnings
    W->>A: POST sets/combine
    A->>P: combine, a pure function
    A->>C: write the combined file, or reuse it
    A-->>W: the combined file and its summary
    W->>A: POST files/ID/parse on the combined file
    A->>P: parse
    A->>C: history start, asset candidates, duplicates
    A-->>W: transactions, checkpoints, verifications, history_start
    Note over W: unify assets, corrections, duplicates and review as usual
    W->>A: POST gap-fix, one per broker and plugin
    A->>C: LibreFolio state at each truth point
    A-->>W: comparisons and gap_fix proposals
    Note over W: the bulk editor saves, as usual

๐Ÿงฑ The shared core: brim_report_set_core.py

backend/app/services/brim/brim_report_set_core.py holds what every report-set plugin needs whatever the bank. All of it is pure and parameterised by what the bank changes (a delimiter, a day accessor, a candidate cap): the plugin passes its own constants and keeps what is bank-specific โ€” the columns, the classification of the rows, the pairing key, the truth points.

Area Functions Note
Text, numbers, dates norm, plain, money, source, compatible, match_key_text, add_business_days, CENT source builds the lf_source text; compatible is the prefix test for a truncated name
Reading xlsx_rows, csv_rows, layout, find_layout first worksheet with data; header scan by normalised label
Rule M RoleMerge, coverage, by_day, same_rows, contained_rows several files of one role, day by day (below)
Pairing assign, best_assignment, pair_by_key, assignments one to one, never a guess
Timeline merge_spans, Segment, build_timeline, zone segments, checkpoint on the eve, windows; used by Danske

RoleMerge(files, day_of, owner=โ€ฆ, contradiction=โ€ฆ) โ€” the two policies of rule M, both with the default Danske uses:

Parameter "latest" / "differs" (default, Danske) "most_rows" / "not_contained" (Crรฉdit Agricole)
owner โ€” who owns a day covered by several files the file that ends last the file with the most rows that day (ties: the file that ends last)
contradiction โ€” when a day is a mismatch two files do not hold the same rows a file holds a row the owner lacks; a file cut mid-day whose rows are all in the owner is no contradiction

Rows must expose signature. kept and superseded are the rows of the owners and the rows of a losing file that is not identical to the owner; mismatch_days feeds the overlap_mismatch notice.

Pairing. pair_by_key(left, right, left_key, right_key, assign_group) groups the two sides by key and hands each group to assign, which answers a list of pairs or None (ambiguous). assign pairs one against one; with more rows it pairs identical twins or lets best_assignment choose the assignment with the most compatible names, and gives up when more than max_candidates rows are on a side or when two assignments would import different things. outcome_of(left) says what a left row imports. pair_outcome_of(left, right) says it for a pair, for the plugin whose right-hand row also decides what is imported (Crรฉdit Agricole: the ISIN, the withholding, the name on the account side). When it is given the twin shortcut is skipped and two assignments that give the left rows different partners are ambiguous unless the partners are interchangeable.

๐Ÿ†• What a new report-set plugin provides

  1. Roles (report_roles) with required, multiple, extensions, and max_history / must_cover when the bank limits how far back an export goes.
  2. detect_role, describe_member, describe_set โ€” pure and cheap, no combining.
  3. A reader that keeps every figure as the file wrote it (cells) next to the parsed value, so the combined file can carry the verbatim values. A numeric or date cell of an XLSX must be written back in a form the parse reads (dd/mm/yyyy, the bank's decimal convention).
  4. An analysis (a class that reads the members once): rule M through RoleMerge, the classes, the pairing key and assign, the outcome of every row, the truth points.
  5. combine: the analysis written as a BRIMCombinedTable โ€” columns lf_*, then the verbatim <role>:<column>, with COMBINED_REQUIRED_HEADERS (lf_row_kind, lf_source); a summary.
  6. parse of the combined file only, plus BRIMSetRequiredError naming missing_roles for a member read alone, and BRIMParseError with a code for anything else.
  7. pre_checkpoint_policy, history_tag, test_sample_sets and the synthetic samples.

Where not to copy Danske

Danske's choices follow from its data, not from the pattern. Do not carry them over without checking them against your bank's:

  • the axis: Danske places every row on the value date; Crรฉdit Agricole's zones, intervals and balances are on the operation date, and the value date only pairs rows;
  • the zones tied to segments of the custody history, the settlement lag of 5 business days and the deferred outcome: Crรฉdit Agricole has none (its zones come from the balances of the account);
  • summarize: rows before the first checkpoint are summarised there; Crรฉdit Agricole uses import and a checkpoint absorbs nothing;
  • proofs of positions (E1โ€“E4): Danske proves positions from its own rows; Crรฉdit Agricole proves only the opening, from the transfers;
  • account numbers: whatever the original carries (Danske's cash statement, Crรฉdit Agricole's securities header: dossier and supporting account) is never copied into the combined file.

What is common is the core above and the contract below; what is bank-specific stays in the plugin.

๐ŸŽญ Roles and the contract

A report-set plugin overrides report_roles with one BRIMReportRole per export:

The examples are Danske's; Crรฉdit Agricole declares securities and account (both required and multiple, .csv and .xlsx; account with max_history P2Y and must_cover securities).

Field Meaning Danske custody Danske cash
code Role id, unique within the plugin custody cash
required The set cannot be combined without at least one file of this role (default True) True True
multiple Several files of this role are allowed in one set (default False) True True
extensions Accepted file extensions [".xlsx"] [".csv"]
description English description of the export; the UI falls back to it when importWizard.reportSet.roleName.<code> has no translation "Custody transactions (Sijoitukset โ†’ Tapahtumat), โ€ฆ" "Cash account statement of the equity savings account, โ€ฆ"
max_history How far back the bank exports this role, as an ISO 8601 duration P1Y P5Y
must_cover The role whose period this role must cover: it gives the period of a missing export and drives the coverage_starts_late / coverage_ends_early warnings โ€” custody

The rest of the contract, all on BRIMProvider (members is always Dict[str, List[Path]], role code โ†’ the files of that role):

Member Default In a report-set plugin
report_roles [] Non-empty: is_report_set_plugin becomes True
can_parse(path) โ€” True for every member and for the plugin's own combined files
detect_role(path) None The role of a member; None for a combined or a foreign file
describe_member(path) raises NotImplementedError BRIMMemberSummary: role, data rows, coverage per date axis (trade or value), and an account_fingerprint compared in memory only (never serialised)
describe_set(members) raises NotImplementedError BRIMSetShape: segments, proven gaps and notices, without combining
combine(members) raises NotImplementedError The combined table, from a pure function (below)
parse(path, broker_id) โ€” Parses the combined file; a member alone raises BRIMSetRequiredError
pre_checkpoint_policy "summarize" What happens to the rows before the first checkpoint: summarize (Danske) or import (Crรฉdit Agricole)
history_tag provider_code without broker_ The tag that marks the plugin's transactions (danske_bank, credit_agricole)
settlement_lag_business_days 0 The longest settlement delay (Danske: 5). Informational: the framework does not read it, and Danske's own zone rules use the same constant
test_sample_sets [] The sample sets of the generic test suite (see Testing)

๐Ÿ“ฆ The set and its API

A set is the files uploaded together (same batch_id) for one broker and recognised by one report-set plugin. Nobody declares which files go together, and the server never looks for members among the broker's other files: a set is identified by upload batch, broker and plugin. The user can only leave originals of the upload out โ€” read alone with another plugin, or removed from the set โ€” and every set request then names them in exclude_file_ids. The server stores no such choice: the wizard derives the list from the user's choices and from the memory of the last analysis, FilesTable (the Files page, a broker's Uploaded Reports) from the memory alone, and the memory is read back from GET /files (notably derived_from, status, processed_at and parsed_plugin_code) โ€” Import Wizard โ†’ The memory of the last analysis.

How the user changes a set: one rule for every report-set plugin. The wizard applies it, so a plugin has nothing to implement, but it is the contract every set lives in:

  • choosing a report-set plugin for a file puts the file in that plugin's set โ€” the one of its broker and upload batch;
  • taking a file out of its set never changes its tick: the file keeps its tick and loses its plugin, waiting for a new choice โ€” ยซnot with this pluginยป is not ยซnot at allยป;
  • Read as (another report-set plugin, or the files one by one) and Read alone with โ€นpluginโ€บ change only how the files are read, never which ones are ticked;
  • a set ticked only in part is not analysed: the analysis waits until the user ticks the whole set or unticks it, and nothing ticks it for them โ€” exclude_file_ids names only the files taken out of the set, so /sets/preview and /sets/combine would read its unticked members too.

A file taken out by mistake goes back when the set's plugin is chosen for it again (Import Wizard โ†’ How a set is read).

All routes live under /api/v1/brokers/import. The upload, /sets/preview, /sets/combine and the parse require EDITOR or OWNER access on their broker (the parse on the file's broker and on the broker_id it imports into), and the administrator passes; GET /files lists only the files the caller may see (Who reaches a report file).

  • POST /upload takes an optional form field batch_id, a UUID (anything else is a 422), stored in the file's sidecar. The wizard sends one per session, the Files page and a broker's Uploaded Reports one per upload action, and Upload the missing file reuses the set's own. A file uploaded without one belongs to no set: the wizard lists it as a single file, and its parse answers that the exports must be uploaded together.
  • GET /files returns BRIMFileListItem (BRIMFileInfo plus can_delete) with batch_id, kind (original or combined), derived_from (file_id, role, filename and deleted of each original), combined_into and combine_is_stale. GET /plugins returns the report_roles of each plugin.
  • POST /sets/preview โ€” body BRIMSetRequest {broker_id, plugin_code, batch_id, exclude_file_ids}, the last one defaulting to [] โ€” collects the members with collect_members (the original, non-failed files of that batch and broker whose compatible_plugins name the plugin, minus exclude_file_ids), asks the plugin for detect_role, describe_member and describe_set, reads the broker history from the database, and returns BRIMSetPreview: the members with role, rows and coverage; one status per role (present, missing or excess); missing, with the period the missing export must cover when must_cover gives one (from the day before the covered role starts to its last day); segments and gaps; the history LibreFolio already holds for the plugin: history_start (H0), history_end (the date of the most recent broker transaction carrying the plugin's history_tag as an exact tag) and history_count (how many of the broker's transactions carry that tag, gap-fix corrections included; default 0); warnings with stable codes; and complete. The three history fields come from one read of the tagged transactions (_tagged_dates), which also gives the dates of the earlier gap-fix corrections; the wizard's set card draws that history from H0 to history_end, with its count (Import Wizard โ†’ Report sets). It writes nothing. An unknown plugin or a batch without members answers 404 โ€” so does an exclude_file_ids that leaves no member (members_not_found) โ€” and a single-file plugin 400. An id in exclude_file_ids that is not an original of that broker and batch is refused rather than ignored, so a stale request never passes silently: 422 exclude_unknown (BRIMSetExcludeUnknown).
  • POST /sets/combine โ€” same body โ€” repeats the preview, with the same exclusions, and answers 422 set_incomplete, with missing_roles, unless the set is complete. A combined file of exactly these members, built by the same plugin version, is returned as is (reused: true): the reuse keys on the exact members (members_key), so a set with a file left out gets its own combined file, and putting the file back reuses the one built with it. Otherwise the core runs plugin.combine off the event loop, writes the table with write_combined_csv (UTF-8 with BOM, ;-separated) and its sidecar with save_combined_file (kind: "combined", derived_from, combine_plugin_code, combine_plugin_version, combine_summary), and adds the new file to the combined_into of each original. The reuse check, the combine and the save are one step under the broker's metadata lock, in a worker thread (_combine_under_broker_lock): two analyses of one set at once โ€” a double click, two tabs โ€” leave one combined file, because the second waits for the lock, finds the first one's file and answers reused: true. The broker's other metadata writes wait while plugin.combine runs. The answer is BRIMSetCombineResponse {combined, summary, reused}. A BRIMParseError or ValueError raised by the plugin becomes a 422 combine_failed, and the members stay as they are.
  • POST /files/{file_id}/parse on a member alone answers 422 (code: "set_required", with missing_roles) and does not move the file to failed. On the combined file it adds checkpoints, verifications and history_start to BRIMParseResponse.

complete is True when every required role has a file, no single-file role has several (excess_files) and the members do not come from different accounts (mixed_accounts, decided on the account fingerprints). No other warning blocks. The core's codes are unknown_role, excess_files, coverage_starts_late, coverage_ends_early, mixed_accounts, before_history_segment and covers_gap_fix; the plugin adds its own through describe_set (Danske: empty_member, mixed_accounts_in_file, overlap_mismatch, gap, balance_chain_broken; Crรฉdit Agricole: empty_member, overlap_mismatch, account_gap, balance_chain_broken, ambiguous, no_counterpart, no_balance). The wizard localises importWizard.reportSet.warning.<code> and falls back to the English message.

Combined files: generated names, stale versions, deleted originals

  • A combined file gets a generated name, <provider_name> โ€” combined <first day>โ€ฆ<last day>.csv, never an original's: those may carry an account number (Danske's cash statement does).
  • combine_is_stale is True once the plugin version has changed since the combine. The next combine of the set builds a new file, because reuse needs the same version; the old one stays.
  • Deleting an original marks it deleted in the derived_from of its combined files, which stay readable because they carry the verbatim values. Deleting a combined file removes it from the combined_into of its originals.

๐Ÿงช combine is pure

combine must be a pure function of the members and of the plugin version: it never reads the database. The same set always yields the same combined file, which is what makes reuse safe and the file reproducible. Whatever depends on the broker's history in LibreFolio is applied by the core at parse time (History start). plugin_version covers both combine and parse: bump it when either output changes for the same files.

combine returns a BRIMCombinedTable (headers, rows, summary): unique headers that include lf_row_kind and lf_source (COMBINED_REQUIRED_HEADERS), and rows as wide as the headers. The plugin never writes the file โ€” the core does, so the format is one for every plugin. summary is free-form and kept in the sidecar; the wizard's analysis detail reads its outcomes and reasons counters.

Danske's combined file has one line per source row (a pair is one line) plus the truth rows, all sorted by value date:

Column Content
lf_row_kind pair, standalone, excluded, summarized, deferred, truth_cash, truth_position or verification
lf_zone before, window, gap or after
lf_reason Why a row is excluded: no_counterpart, ambiguous, not_yet_settled, outside_cash_coverage, status, unknown_type, invalid or superseded
lf_checkpoint The checkpoint that absorbs the row, or the date of a truth row
lf_source Role and line of the source row: custody:12, custody#2:12 when the role has several files, custody:12 + cash:40 for a pair
lf_match_key The pairing key: value date, amount, currency
lf_value, lf_currency, lf_proof Truth rows: the cash or the quantity, its currency, and the proof (exact:E1, exact:E2, at_least:E3 or discarded:<rule>:<reason>)
custody:<column>, cash:<column> The verbatim source values โ€” except the custody account column, which is never copied

๐Ÿ—บ๏ธ Zones and outcomes

Danske places every row on the value-date axis:

  • a segment is the continuous span covered by the custody files (by trade date): files that overlap or touch form one segment, and two segments stay apart only when the cash statement proves trades between them (a cash trade row without counterpart);
  • the window of a segment runs from the day after its checkpoint to its last day plus the settlement lag (5 business days), capped at the next checkpoint; its border is the first 5 business days after the checkpoint;
  • before is up to the first checkpoint, a gap lies between a window and the next checkpoint, and after is beyond the last window.

The axis places the rows; it does not date them. The parse records each transaction on its operation date, under the rule The operation date, and only settled rows (Import philosophy): the custody trade date, or the Pvm of a cash-only row, its only date. Pairing, zones, not_yet_settled and deferred read the value date, because the bank moves the cash on that day.

Every row gets exactly one outcome, with a reason when it is excluded: no row leaves the set's accounting, not even one left out without a notice (reservation).

Row before window gap after
Cash with no counterpart by nature (deposit, withdrawal, tax, fee, interest) standalone, absorbed by the first checkpoint standalone standalone, imported with its own date standalone
Cash trade or income with its custody row โ€” pair: one transaction โ€” โ€”
Cash trade or income without its custody row summarized in the first checkpoint in the border: summarized in that checkpoint (a trade made before the custody export, settled after its start); after the segment's last day: the next zone; elsewhere excluded (no_counterpart) summarized in the next checkpoint deferred: it comes with the next import
Custody trade or income without its cash row โ€” excluded: not_yet_settled (settled after the last cash row), outside_cash_coverage (outside the cash statement), otherwise no_counterpart โ€” โ€”
Custody demerger line โ€” standalone: a cashless ADJUSTMENT โ€” โ€”
Not executed or not booked; reserved (Varaus); unknown type; invalid excluded (status, reservation, unknown_type, invalid) same same same

Pairing keys on value date, amount to the cent, currency and direction (Osto with a positive quantity, Myynti with a negative one, income with a positive amount). The name is only a consistency check โ€” the cash statement truncates its labels, so one name must be a prefix of the other โ€” and a unique key with a different name still pairs, with a name_mismatch notice. Pairing is one to one: identical candidates pair in file order, and candidates that cannot be told apart are all ambiguous (so are more than 8 per side). The plugin never guesses.

With several files of one role (rule M), each day belongs to the latest file covering it. Identical rows count once (identical partial fills inside one file stay two rows); where the files disagree on a day, the rows of the losing file are excluded (superseded), and describe_set reports overlap_mismatch.

๐Ÿ“ Truth points and the safety rule

A truth point is what the bank states at a date. The plugin returns two kinds in BRIMParseOutput:

  • BRIMCheckpoint โ€” as_of, kind (opening for the earliest, gap for the later ones), cash (one BRIMTruthCash per currency), positions (BRIMTruthPosition: the parse's fake asset_id, a quantity, exactness exact or at_least, an optional unit_cost), absorbed (BRIMAbsorbed: how many rows the checkpoint summarises, their net cash, the rows themselves and, for the opening, the balance before them) and evidence. The gap-fix compares it with LibreFolio and proposes corrections.
  • BRIMVerification โ€” as_of, cash, evidence: compared, never corrected.

Danske puts a checkpoint on the eve of each segment, the day before its oldest trade date โ€” the first segment's too, even when the cash statement starts after the custody export. Its cash is the statement's running balance at the end of that day plus the cash of the segment's border rows; for an opening that precedes the statement, that balance is the statement's first balance, the balance before its first booked row. Its positions come from proofs inside the segment, each brought back to the checkpoint by removing the movements imported in between:

  • E1 โ€” a Tuotto (income) states the shares held: exact, unless a movement of that security falls in the 30 days before it;
  • E2 โ€” the old line of a demerger states the whole holding: exact;
  • E3 โ€” selling more than was bought since the segment started proves at least the largest shortfall;
  • E4 โ€” an excluded row of the same security between the checkpoint and the proof discards it, except a trade settled before the cash statement starts (outside_cash_coverage): that one belongs to the opening.

Every truth โ€” a checkpoint's cash and positions, every verification โ€” also adds what is in flight on its day: the cash and, for a position, the quantity of the trades the set imports with a trade date up to that day and a value date after it. LibreFolio records them on their trade date and the bank books them on their value date, so both sides compare in operation-date terms.

Discarded proofs are reported (proof_discarded). A final verification compares the cash at the end of the last segment, or of the cash statement if it ends first. The running balance is checked day by day: when it does not add up, the cash of every checkpoint becomes a verification (balance_chain_broken).

The safety rule

A truth point may become a checkpoint โ€” that is, produce corrections โ€” only if no future import will bring, one by one, the rows it summarises. Otherwise it must be a verification. Danske's eve of a segment qualifies: it summarises only trades that no custody file of the set covers โ€” and, when the cash statement starts after the custody export, the trades settled before the statement's first day, which the set leaves out (outside_cash_coverage). The end of the last segment does not: the next import brings its not_yet_settled and deferred trades, and a correction there would count them twice.

โฎ๏ธ The history start (H0)

history_start() in brim_report_sets.py is the first day of the broker history LibreFolio already holds for the plugin: the date of the oldest broker transaction carrying the plugin's history_tag as an exact tag, where a gap_fix correction counts from the day after its date (it summarises everything up to the end of that day). The preview's history_end and history_count come from the same read of the tagged transactions. With no such transaction this is the first import, and apply_history() sets H0 to the day after the first checkpoint โ€” or, with pre_checkpoint_policy == "import", to the date of the oldest parsed transaction.

At parse time apply_history():

  • keeps only the checkpoints dated on or after the eve of H0, so a first import keeps its opening checkpoint and a segment entirely before the history is dropped;
  • on a later import, removes from the kept checkpoints their opening_cash and the absorbed rows dated before H0, which are already represented;
  • returns H0 as BRIMParseResponse.history_start: the wizard never selects a row dated strictly before it.

The preview reads the same history: before_history_segment warns about a segment older than the history (it will not be imported), and covers_gap_fix about an earlier gap_fix correction that falls inside a segment of the set (it must be removed by hand, or the cash counts twice).

The days around H0 and around a role's coverage (H0's eve, the day after a correction, the eve of the period a role must cover) are moved with shift_date() (utils/datetime_utils.py), which holds them at the calendar's ends: a report dated 0001-01-01 or 9999-12-31 is read, its rows then refused at Save All like any date out of range (N74), instead of failing the whole parse (W-B71).

๐Ÿงญ Where Crรฉdit Agricole differs from Danske

The second plugin shows which parts of the pattern are the plugin's own. Details: Crรฉdit Agricole Importer.

Danske Bank Crรฉdit Agricole
Roles custody XLSX, cash CSV securities and account, each CSV or XLSX
Rule M latest / differs most_rows / not_contained
Pairing key trade kind + value date + amount + currency + direction kind of fact + amount to the cent (half-up) + same value date + currency; dividends never pair
Axis of zones value date, over segments of the custody history operation date, over the balances of the account (before, window, gap)
Truth points the eve of each segment, positions from proofs E1โ€“E4 the opening balance, the end of each hole, positions of the opening from the transfers; every closing balance is a verification
Cash correction at the eve of a segment only for a hole (balances differ and no row lies between them); a broken interval is a warning
pre_checkpoint_policy summarize import
Cash in flight trades with a trade date up to the day and a value date after it pairs recorded (securities date) and not booked (account date), and the opposite
Combined lf_row_kind pair, standalone, excluded, summarized, deferred, truth_cash, truth_position, verification pair, standalone, excluded, truth_cash, truth_position, verification
Automatic cash legs in the parse none none (the 1.x plugin had them; 2.0.0 does not)

The shape of the analysis is the same in both: read the members, apply rule M, classify, pair, decide the outcome of every row, derive the truth points, write the combined file, parse only that file.

๐Ÿท๏ธ Tags

Every transaction of a report-set plugin carries import and its history_tag (the generic suite checks it); Danske adds demerger on demerger lines. H0 is computed from history_tag, so the tag must stay stable once users have imported with it; the default, the provider code without broker_, is the tag the existing plugins already write. The gap-fix corrections carry import, the history_tag and gap_fix.

๐Ÿงฎ POST /brokers/import/gap-fix

The wizard calls it after the review, once per broker and plugin, with BRIMGapFixRequest {broker_id, plugin_code, checkpoints, verifications, selection, pending_creates, pending_delete_tx_ids}: the truth points of the parse (positions already resolved to real assets by the wizard), the transactions it is about to hand to the editor, and the editor's unsaved rows and pending deletions. The answer, BRIMGapFixResponse, has one BRIMGapFixCheckpointResult per checkpoint in date order, then the verifications. It writes nothing.

For each checkpoint, LibreFolio's state at the end of as_of is the sum of the broker's saved transactions (TransactionService.get_balances_at_end_of, without pending_delete_tx_ids) and of the unsaved rows, the selection and the corrections proposed at the earlier checkpoints โ€” which count as accepted โ€” dated up to as_of. Then:

  • cash, per currency the bank states: a difference above 0.01 becomes a DEPOSIT (positive) or a WITHDRAWAL (negative) dated as_of;
  • positions: an exact position gets an ADJUSTMENT of the difference, positive or negative; an at_least one only the missing part, never negative. A positive adjustment without a known unit_cost gets a blocker todo gap_fix_cost on cost_basis_override (its tx_index counts within that checkpoint's proposals); a negative one carries no cost. A position whose asset is still a fake id is left out, with an unresolved_asset note;
  • explanation: how many absorbed rows LibreFolio does not have yet (matched on date, currency and amount, one transaction per row) and their cash, the opening balance (opening checkpoint only), and the part of the difference these do not explain.

Each verification gets the same cash comparison, ok when every currency is within 0.01, and never a proposal. The proposals are ordinary TXCreateItems described as Gap-fix <date> โ€” <provider_name>: <what> at the end of the day, as stated by the bank, and the bulk editor saves them like any other row. The endpoint accepts any registered plugin, since it only reads history_tag and provider_name.

When a transaction of the broker dated up to a checkpoint's as_of (and not among pending_delete_tx_ids) holds a value that is no number โ€” a quantity or an amount that reads None, which an older version or another tool left โ€” LibreFolio's side cannot be summed: the endpoint answers 409 {"code": "ledger_unreadable", "message": โ€ฆ} instead of comparing a wrong balance with the bank, which would propose a correction duplicating the row it cannot read (get_balances_at_end_of raises UnreadableLedgerError, brim_gap_fix turns it into BRIMLedgerUnreadable; W-B74). The wizard shows the refusal in that broker's group, in the UI language (how it is translated).

When those transactions hold a cash currency that is not a canonical ISO 4217 code โ€” a name or a number an earlier version saved as written (978, EURO), or a code in another case (eur) โ€” the endpoint answers the same 409 ledger_unreadable, naming at most five such codes, each with the code it stands for ('978' (EUR)): such a balance is a bucket of its own that the bank's EUR never meets, so the comparison would propose a correction for money already there (_refuse_non_iso_cash in brim_gap_fix.py, after the W-B74 check; E-19a). Only Transaction.currency counts: the cost basis is no cash.

๐Ÿงช Testing a report-set plugin

  1. Put synthetic members (invented values, the real column layout) in sample_reports/, describe them in its README.md, and declare the sets in test_sample_sets, one {role: [file names]} per set:

    @property
    def test_sample_sets(self) -> List[Dict[str, List[str]]]:
        return [{"custody": ["danske_bank-custody.xlsx"], "cash": ["danske_bank-cash.csv"]}]
    
  2. The generic suite (./dev.py test external brim-providers) never parses a member alone: it combines every declared set and parses the combined file. TestReportSetPlugin checks that the samples exist and cover the required roles; that each member is recognised with its role and auto-detected for the plugin; describe_member and describe_set; that combine is pure (two runs give the same table and leave the members untouched); that the combined files are recognised without a role; that a member alone raises BRIMSetRequiredError; and the parse contract โ€” positions point at extracted assets, every transaction carries import and the history tag, and there is exactly one opening checkpoint, the earliest. TestBRIMPlugin then runs its usual checks on the combined files. To put a set under TestPluginFrontendContract too, add it to CONTRACT_SAMPLES as a _ContractSampleSet (and any new todo reason code to KNOWN_REASON_CODES).

  3. Pin the plugin's own rules in a dedicated module, like test_external/test_brim_danske_bank.py (./dev.py test external brim-danske-bank), registered in the test runner.

The framework has its own suites: ./dev.py test services brim-report-sets, ./dev.py test services brim-gap-fix and ./dev.py test api brim.

๐Ÿงช Register a sample (required for tests)

The parametrized suite in backend/test_scripts/test_external/test_brim_providers.py auto-covers your plugin โ€” but only if a sample is present. Copy one real (anonymized) export into backend/app/services/brim_providers/sample_reports/, with a filename that contains your test_file_pattern/test_file_patterns substring:

cp mybroker-export.csv backend/app/services/brim_providers/sample_reports/mybroker-export.csv

Then run just the BRIM suite:

./dev.py test external brim-providers                       # all plugins
./dev.py test external brim-providers --providers broker_mybroker   # one plugin

test_all_plugins_used_at_least_once and test_specific_broker_detection_via_plugin_pattern will fail if the sample is missing or if can_parse collides with another plugin โ€” keep the header check specific.

A report-set plugin's members are never parsed alone: declare its samples as sets in test_sample_sets โ€” see Testing a report-set plugin.

๐ŸŒ Register the name and description (required)

provider_name and description are written in English, and the UI shows them in the user's language: every plugin ships two keys, in English, Italian, French and Spanish, in its own fragment brim_providers/<code>/i18n/{en,it,fr,es}.json. The frontend build merges the fragments into the catalogue it loads (A Plugin's Translations). Each file holds the plugin's keys with their full path:

{"brimPlugins": {"broker_mybroker": {"name": "My Broker (CSV)", "description": "Import transactions from My Broker CSV exports"}}}
Key In en In it, fr, es
brimPlugins.<code>.name provider_name, character for character A brand name stays as it is (DEGIRO, Interactive Brokers); only generic words are translated (Generic CSV โ†’ CSV generico; Scalable Capital overnight account โ†’ Scalable Capital conto deposito)
brimPlugins.<code>.description description, character for character Translated

Add them with the i18n tool, which writes the four languages in one go โ€” here for the plugin of the Implementation Example. --into names the plugin's folder: a new code under brimPlugins belongs to no plugin yet, so without it add refuses the key and asks for the target.

./dev.py i18n add "brimPlugins.broker_mybroker.name" --en "My Broker (CSV)" --it "My Broker (CSV)" --fr "My Broker (CSV)" --es "My Broker (CSV)" --into brim_providers/broker_mybroker
./dev.py i18n add "brimPlugins.broker_mybroker.description" --en "Import transactions from My Broker CSV exports" --it "โ€ฆ" --fr "โ€ฆ" --es "โ€ฆ" --into brim_providers/broker_mybroker
./dev.py i18n where "brimPlugins.broker_mybroker.name"   # backend/app/services/brim_providers/broker_mybroker/i18n/en.json (exists)

The first key creates the fragment; from then on add finds it by itself for another key of the plugin. Never edit frontend/src/lib/i18n/<lang>.json: it is generated, and a hand edit stops the build until the change is moved to its source.

  • Who reads them. brimPluginName and brimPluginDescription (frontend/src/lib/utils/brim/pluginText.ts): the plugin select (ImportPluginSelect, in the broker form's Default Import Plugin and the wizard's Plugin column: the name as the label; the description under each option, under the select and in its search), the import wizard (the Parse table, the report-set card, the missing-export warning and the wrong-broker prompt) and Settings โ†’ About (the name).
  • Without keys โ€” a plugin the catalogues do not know yet โ€” both helpers fall back to the plugin's own English text: the plugin still shows, in English.
  • In a refusal. A BRIMRefusal whose context names a plugin by plugin_code gets that plugin's name in the UI language as plugin_name, before importWizard.parseRefusal.<code> is filled in (Saying why with a code).
  • The tests. TestPluginTextCatalogues, in test_brim_providers.py (./dev.py test external brim-providers), reads the merged sources without a frontend build (load_catalog(language, fallback=False) from scripts/i18n_catalog.py) and holds them to the registry: the English keys equal each plugin's provider_name and description, the Italian, French and Spanish ones are filled in for every plugin, and no catalogue keeps a code that no registered plugin has. Change a property and its English key together (./dev.py i18n update), and remove the keys with the plugin (./dev.py i18n remove, which deletes the fragment once it is empty). ./dev.py test utils gate-plugin-i18n-ownership keeps the two keys in the plugin's own fragment and its code equal to its folder's name (The Ownership Gate). The two lookups and their fallbacks are pinned by pluginText.test.ts (./dev.py test front-transaction tx-unit).

๐Ÿ–ผ๏ธ Favicons

Set icon_url to the broker's favicon (https://<domain>/favicon.ico). It is rendered both in the app Settings โ†’ Import UI and on the mkdocs import page, so it must be embeddable cross-origin.

It is also the icon of every broker that uses the plugin as its default import plugin and has no custom icon: it comes before the favicon of the broker's Portal URL. A generic fallback plugin โ€” detection_priority below 50, like the Generic CSV โ€” comes after that favicon instead, because its icon is the same for every broker (FALLBACK_PLUGIN_PRIORITY_LIMIT in frontend/src/lib/utils/brim/pluginKind.ts, used by brokerIconChain.svelte.ts and getBrokerIconCandidates).

Two failure modes to check for:

  • Cloudflare / bot block โ€” the domain returns 403/404 to non-browser requests but the icon still loads in a real browser. curl -sI is not conclusive here; if it renders in the UI, keep it.
  • cross-origin-resource-policy: same-origin โ€” the icon downloads fine with curl but the browser refuses to embed it from another origin, so it renders nowhere in-app (this was the Delta case). Check for it:

    curl -sI -L https://<domain>/favicon.ico | grep -i cross-origin-resource-policy
    

    If it reports same-origin, don't hotlink it โ€” use Google's CORP-free favicon proxy (which sends cross-origin-resource-policy: cross-origin):

    return "https://www.google.com/s2/favicons?domain=<domain>&sz=64"
    

    When you place that proxy URL in an HTML <img src="โ€ฆ"> inside the docs, escape the ampersand as &amp;sz=64; the raw & is fine in the Python icon_url string.

If no working icon exists, ship icon_url = None and mark the broker in Providers List for a maintainer to fill in.

๐Ÿ“š Register the user-facing docs

Expose the plugin to users by wiring up its documentation. Point docs_url at the page.

docs_url accepts either an internal MkDocs slug or an external absolute URL:

  • Internal wiki slug (recommended, the convention used by every current plugin) โ€” an absolute /mkdocs/... path. Use this whenever you ship a page under user/transactions/import/. The frontend localizes it automatically by rewriting /mkdocs/ โ†’ /mkdocs/<lang>/, so only the internal slug form gets translated.
  • External URL โ€” a full https://... link to a broker help page. Allowed (opens in a new tab) but it is not localized, so prefer an internal page when one exists.
@property
def docs_url(self) -> Optional[str]:
    return "/mkdocs/user/transactions/import/<slug>/"   # internal slug (localized)
    # or: return "https://broker.example/help/exports"   # external URL (not localized)

Then, reusing <slug> everywhere:

  1. Page (ร—4 languages) โ€” create <slug>.en.md, .it.md, .fr.md, .es.md in mkdocs_src/docs/user/transactions/import/. A short beta placeholder is fine (favicon in the H1, a "how to export" line, a note that it was built from sample exports). Use directa.it.md as a fuller reference once real export steps are known. In each language, call the importer what its brimPlugins.<code>.name says (above): that is the name the user reads in the app.
  2. Index card โ€” add an <a href="<slug>/"> card in each mkdocs_src/docs/user/transactions/import/index.<lang>.md (in the matching broker group, before the final Request New Plugin / generic-csv card). Copy an existing card and swap the slug, favicon, name and description:

    <a href="<slug>/" class="card-link" style="flex-direction: column; align-items: stretch; gap: 0.5rem;">
    <div style="display: flex; align-items: center; gap: 0.75rem;">
    <img src="<favicon-url>" width="24" height="24" style="object-fit: contain; border-radius: 4px;" alt="favicon <Display>">
    <span class="card-title" style="margin: 0;"><Display></span>
    </div>
    <span class="card-desc">Import the <โ€ฆ> export from <Display>.</span>
    </a>
    
  3. Nav โ€” add - <Display>: user/transactions/import/<slug>.md in mkdocs_src/mkdocs.yml under the right ๐Ÿ“ฅ Import from Broker subgroup, and add any new group/leaf title to the nav_translations of each non-English locale.

  4. Validate โ€” ./dev.py mkdocs build must report no unrecognized relative link / no such anchor. Internal links use the .md form (how-to.md, ../../../community/contribute.md), never trailing-slash paths.
  5. Developer providers overview โ€” add one row for the broker to Providers List (favicon, code, formats, status, notes) so the developer-side catalogue stays in sync.